ext4_common.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262
  1. /*
  2. * (C) Copyright 2011 - 2012 Samsung Electronics
  3. * EXT4 filesystem implementation in Uboot by
  4. * Uma Shankar <uma.shankar@samsung.com>
  5. * Manjunatha C Achar <a.manjunatha@samsung.com>
  6. *
  7. * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
  8. *
  9. * (C) Copyright 2004
  10. * esd gmbh <www.esd-electronics.com>
  11. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  12. *
  13. * based on code from grub2 fs/ext2.c and fs/fshelp.c by
  14. * GRUB -- GRand Unified Bootloader
  15. * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  16. *
  17. * ext4write : Based on generic ext4 protocol.
  18. *
  19. * SPDX-License-Identifier: GPL-2.0+
  20. */
  21. #include <common.h>
  22. #include <ext_common.h>
  23. #include <ext4fs.h>
  24. #include <inttypes.h>
  25. #include <malloc.h>
  26. #include <memalign.h>
  27. #include <stddef.h>
  28. #include <linux/stat.h>
  29. #include <linux/time.h>
  30. #include <asm/byteorder.h>
  31. #include "ext4_common.h"
  32. struct ext2_data *ext4fs_root;
  33. struct ext2fs_node *ext4fs_file;
  34. __le32 *ext4fs_indir1_block;
  35. int ext4fs_indir1_size;
  36. int ext4fs_indir1_blkno = -1;
  37. __le32 *ext4fs_indir2_block;
  38. int ext4fs_indir2_size;
  39. int ext4fs_indir2_blkno = -1;
  40. __le32 *ext4fs_indir3_block;
  41. int ext4fs_indir3_size;
  42. int ext4fs_indir3_blkno = -1;
  43. struct ext2_inode *g_parent_inode;
  44. static int symlinknest;
  45. #if defined(CONFIG_EXT4_WRITE)
  46. static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
  47. {
  48. sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
  49. }
  50. static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
  51. {
  52. sb->free_blocks = cpu_to_le32(le32_to_cpu(sb->free_blocks) - 1);
  53. }
  54. static inline void ext4fs_bg_free_inodes_dec(struct ext2_block_group *bg)
  55. {
  56. bg->free_inodes = cpu_to_le16(le16_to_cpu(bg->free_inodes) - 1);
  57. }
  58. static inline void ext4fs_bg_free_blocks_dec(struct ext2_block_group *bg)
  59. {
  60. bg->free_blocks = cpu_to_le16(le16_to_cpu(bg->free_blocks) - 1);
  61. }
  62. static inline void ext4fs_bg_itable_unused_dec(struct ext2_block_group *bg)
  63. {
  64. bg->bg_itable_unused = cpu_to_le16(le16_to_cpu(bg->bg_itable_unused) - 1);
  65. }
  66. uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
  67. {
  68. uint32_t res = size / n;
  69. if (res * n != size)
  70. res++;
  71. return res;
  72. }
  73. void put_ext4(uint64_t off, void *buf, uint32_t size)
  74. {
  75. uint64_t startblock;
  76. uint64_t remainder;
  77. unsigned char *temp_ptr = NULL;
  78. struct ext_filesystem *fs = get_fs();
  79. int log2blksz = fs->dev_desc->log2blksz;
  80. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
  81. startblock = off >> log2blksz;
  82. startblock += part_offset;
  83. remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
  84. if (fs->dev_desc == NULL)
  85. return;
  86. if ((startblock + (size >> log2blksz)) >
  87. (part_offset + fs->total_sect)) {
  88. printf("part_offset is " LBAFU "\n", part_offset);
  89. printf("total_sector is %" PRIu64 "\n", fs->total_sect);
  90. printf("error: overflow occurs\n");
  91. return;
  92. }
  93. if (remainder) {
  94. blk_dread(fs->dev_desc, startblock, 1, sec_buf);
  95. temp_ptr = sec_buf;
  96. memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
  97. blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
  98. } else {
  99. if (size >> log2blksz != 0) {
  100. blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
  101. (unsigned long *)buf);
  102. } else {
  103. blk_dread(fs->dev_desc, startblock, 1, sec_buf);
  104. temp_ptr = sec_buf;
  105. memcpy(temp_ptr, buf, size);
  106. blk_dwrite(fs->dev_desc, startblock, 1,
  107. (unsigned long *)sec_buf);
  108. }
  109. }
  110. }
  111. static int _get_new_inode_no(unsigned char *buffer)
  112. {
  113. struct ext_filesystem *fs = get_fs();
  114. unsigned char input;
  115. int operand, status;
  116. int count = 1;
  117. int j = 0;
  118. /* get the blocksize of the filesystem */
  119. unsigned char *ptr = buffer;
  120. while (*ptr == 255) {
  121. ptr++;
  122. count += 8;
  123. if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
  124. return -1;
  125. }
  126. for (j = 0; j < fs->blksz; j++) {
  127. input = *ptr;
  128. int i = 0;
  129. while (i <= 7) {
  130. operand = 1 << i;
  131. status = input & operand;
  132. if (status) {
  133. i++;
  134. count++;
  135. } else {
  136. *ptr |= operand;
  137. return count;
  138. }
  139. }
  140. ptr = ptr + 1;
  141. }
  142. return -1;
  143. }
  144. static int _get_new_blk_no(unsigned char *buffer)
  145. {
  146. int operand;
  147. int count = 0;
  148. int i;
  149. unsigned char *ptr = buffer;
  150. struct ext_filesystem *fs = get_fs();
  151. while (*ptr == 255) {
  152. ptr++;
  153. count += 8;
  154. if (count == (fs->blksz * 8))
  155. return -1;
  156. }
  157. if (fs->blksz == 1024)
  158. count += 1;
  159. for (i = 0; i <= 7; i++) {
  160. operand = 1 << i;
  161. if (*ptr & operand) {
  162. count++;
  163. } else {
  164. *ptr |= operand;
  165. return count;
  166. }
  167. }
  168. return -1;
  169. }
  170. int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
  171. {
  172. int i, remainder, status;
  173. unsigned char *ptr = buffer;
  174. unsigned char operand;
  175. i = blockno / 8;
  176. remainder = blockno % 8;
  177. int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
  178. i = i - (index * blocksize);
  179. if (blocksize != 1024) {
  180. ptr = ptr + i;
  181. operand = 1 << remainder;
  182. status = *ptr & operand;
  183. if (status)
  184. return -1;
  185. *ptr = *ptr | operand;
  186. return 0;
  187. } else {
  188. if (remainder == 0) {
  189. ptr = ptr + i - 1;
  190. operand = (1 << 7);
  191. } else {
  192. ptr = ptr + i;
  193. operand = (1 << (remainder - 1));
  194. }
  195. status = *ptr & operand;
  196. if (status)
  197. return -1;
  198. *ptr = *ptr | operand;
  199. return 0;
  200. }
  201. }
  202. void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
  203. {
  204. int i, remainder, status;
  205. unsigned char *ptr = buffer;
  206. unsigned char operand;
  207. i = blockno / 8;
  208. remainder = blockno % 8;
  209. int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
  210. i = i - (index * blocksize);
  211. if (blocksize != 1024) {
  212. ptr = ptr + i;
  213. operand = (1 << remainder);
  214. status = *ptr & operand;
  215. if (status)
  216. *ptr = *ptr & ~(operand);
  217. } else {
  218. if (remainder == 0) {
  219. ptr = ptr + i - 1;
  220. operand = (1 << 7);
  221. } else {
  222. ptr = ptr + i;
  223. operand = (1 << (remainder - 1));
  224. }
  225. status = *ptr & operand;
  226. if (status)
  227. *ptr = *ptr & ~(operand);
  228. }
  229. }
  230. int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
  231. {
  232. int i, remainder, status;
  233. unsigned char *ptr = buffer;
  234. unsigned char operand;
  235. inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
  236. i = inode_no / 8;
  237. remainder = inode_no % 8;
  238. if (remainder == 0) {
  239. ptr = ptr + i - 1;
  240. operand = (1 << 7);
  241. } else {
  242. ptr = ptr + i;
  243. operand = (1 << (remainder - 1));
  244. }
  245. status = *ptr & operand;
  246. if (status)
  247. return -1;
  248. *ptr = *ptr | operand;
  249. return 0;
  250. }
  251. void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
  252. {
  253. int i, remainder, status;
  254. unsigned char *ptr = buffer;
  255. unsigned char operand;
  256. inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
  257. i = inode_no / 8;
  258. remainder = inode_no % 8;
  259. if (remainder == 0) {
  260. ptr = ptr + i - 1;
  261. operand = (1 << 7);
  262. } else {
  263. ptr = ptr + i;
  264. operand = (1 << (remainder - 1));
  265. }
  266. status = *ptr & operand;
  267. if (status)
  268. *ptr = *ptr & ~(operand);
  269. }
  270. uint16_t ext4fs_checksum_update(uint32_t i)
  271. {
  272. struct ext2_block_group *desc;
  273. struct ext_filesystem *fs = get_fs();
  274. uint16_t crc = 0;
  275. __le32 le32_i = cpu_to_le32(i);
  276. desc = (struct ext2_block_group *)&fs->bgd[i];
  277. if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
  278. int offset = offsetof(struct ext2_block_group, bg_checksum);
  279. crc = ext2fs_crc16(~0, fs->sb->unique_id,
  280. sizeof(fs->sb->unique_id));
  281. crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
  282. crc = ext2fs_crc16(crc, desc, offset);
  283. offset += sizeof(desc->bg_checksum); /* skip checksum */
  284. assert(offset == sizeof(*desc));
  285. }
  286. return crc;
  287. }
  288. static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
  289. {
  290. int dentry_length;
  291. int sizeof_void_space;
  292. int new_entry_byte_reqd;
  293. short padding_factor = 0;
  294. if (dir->namelen % 4 != 0)
  295. padding_factor = 4 - (dir->namelen % 4);
  296. dentry_length = sizeof(struct ext2_dirent) +
  297. dir->namelen + padding_factor;
  298. sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
  299. if (sizeof_void_space == 0)
  300. return 0;
  301. padding_factor = 0;
  302. if (strlen(filename) % 4 != 0)
  303. padding_factor = 4 - (strlen(filename) % 4);
  304. new_entry_byte_reqd = strlen(filename) +
  305. sizeof(struct ext2_dirent) + padding_factor;
  306. if (sizeof_void_space >= new_entry_byte_reqd) {
  307. dir->direntlen = cpu_to_le16(dentry_length);
  308. return sizeof_void_space;
  309. }
  310. return 0;
  311. }
  312. int ext4fs_update_parent_dentry(char *filename, int file_type)
  313. {
  314. unsigned int *zero_buffer = NULL;
  315. char *root_first_block_buffer = NULL;
  316. int blk_idx;
  317. long int first_block_no_of_root = 0;
  318. int totalbytes = 0;
  319. unsigned int new_entry_byte_reqd;
  320. int sizeof_void_space = 0;
  321. int templength = 0;
  322. int inodeno = -1;
  323. int status;
  324. struct ext_filesystem *fs = get_fs();
  325. /* directory entry */
  326. struct ext2_dirent *dir;
  327. char *temp_dir = NULL;
  328. uint32_t new_blk_no;
  329. uint32_t new_size;
  330. uint32_t new_blockcnt;
  331. uint32_t directory_blocks;
  332. zero_buffer = zalloc(fs->blksz);
  333. if (!zero_buffer) {
  334. printf("No Memory\n");
  335. return -1;
  336. }
  337. root_first_block_buffer = zalloc(fs->blksz);
  338. if (!root_first_block_buffer) {
  339. free(zero_buffer);
  340. printf("No Memory\n");
  341. return -1;
  342. }
  343. new_entry_byte_reqd = ROUND(strlen(filename) +
  344. sizeof(struct ext2_dirent), 4);
  345. restart:
  346. directory_blocks = le32_to_cpu(g_parent_inode->size) >>
  347. LOG2_BLOCK_SIZE(ext4fs_root);
  348. blk_idx = directory_blocks - 1;
  349. restart_read:
  350. /* read the block no allocated to a file */
  351. first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
  352. if (first_block_no_of_root <= 0)
  353. goto fail;
  354. status = ext4fs_devread((lbaint_t)first_block_no_of_root
  355. * fs->sect_perblk,
  356. 0, fs->blksz, root_first_block_buffer);
  357. if (status == 0)
  358. goto fail;
  359. if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
  360. goto fail;
  361. dir = (struct ext2_dirent *)root_first_block_buffer;
  362. totalbytes = 0;
  363. while (le16_to_cpu(dir->direntlen) > 0) {
  364. unsigned short used_len = ROUND(dir->namelen +
  365. sizeof(struct ext2_dirent), 4);
  366. /* last entry of block */
  367. if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
  368. /* check if new entry fits */
  369. if ((used_len + new_entry_byte_reqd) <=
  370. le16_to_cpu(dir->direntlen)) {
  371. dir->direntlen = cpu_to_le16(used_len);
  372. break;
  373. } else {
  374. if (blk_idx > 0) {
  375. printf("Block full, trying previous\n");
  376. blk_idx--;
  377. goto restart_read;
  378. }
  379. printf("All blocks full: Allocate new\n");
  380. if (le32_to_cpu(g_parent_inode->flags) &
  381. EXT4_EXTENTS_FL) {
  382. printf("Directory uses extents\n");
  383. goto fail;
  384. }
  385. if (directory_blocks >= INDIRECT_BLOCKS) {
  386. printf("Directory exceeds limit\n");
  387. goto fail;
  388. }
  389. new_blk_no = ext4fs_get_new_blk_no();
  390. if (new_blk_no == -1) {
  391. printf("no block left to assign\n");
  392. goto fail;
  393. }
  394. put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
  395. g_parent_inode->b.blocks.
  396. dir_blocks[directory_blocks] =
  397. cpu_to_le32(new_blk_no);
  398. new_size = le32_to_cpu(g_parent_inode->size);
  399. new_size += fs->blksz;
  400. g_parent_inode->size = cpu_to_le32(new_size);
  401. new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
  402. new_blockcnt += fs->sect_perblk;
  403. g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
  404. if (ext4fs_put_metadata
  405. (root_first_block_buffer,
  406. first_block_no_of_root))
  407. goto fail;
  408. goto restart;
  409. }
  410. }
  411. templength = le16_to_cpu(dir->direntlen);
  412. totalbytes = totalbytes + templength;
  413. sizeof_void_space = check_void_in_dentry(dir, filename);
  414. if (sizeof_void_space)
  415. break;
  416. dir = (struct ext2_dirent *)((char *)dir + templength);
  417. }
  418. /* make a pointer ready for creating next directory entry */
  419. templength = le16_to_cpu(dir->direntlen);
  420. totalbytes = totalbytes + templength;
  421. dir = (struct ext2_dirent *)((char *)dir + templength);
  422. /* get the next available inode number */
  423. inodeno = ext4fs_get_new_inode_no();
  424. if (inodeno == -1) {
  425. printf("no inode left to assign\n");
  426. goto fail;
  427. }
  428. dir->inode = cpu_to_le32(inodeno);
  429. if (sizeof_void_space)
  430. dir->direntlen = cpu_to_le16(sizeof_void_space);
  431. else
  432. dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
  433. dir->namelen = strlen(filename);
  434. dir->filetype = FILETYPE_REG; /* regular file */
  435. temp_dir = (char *)dir;
  436. temp_dir = temp_dir + sizeof(struct ext2_dirent);
  437. memcpy(temp_dir, filename, strlen(filename));
  438. /* update or write the 1st block of root inode */
  439. if (ext4fs_put_metadata(root_first_block_buffer,
  440. first_block_no_of_root))
  441. goto fail;
  442. fail:
  443. free(zero_buffer);
  444. free(root_first_block_buffer);
  445. return inodeno;
  446. }
  447. static int search_dir(struct ext2_inode *parent_inode, char *dirname)
  448. {
  449. int status;
  450. int inodeno = 0;
  451. int offset;
  452. int blk_idx;
  453. long int blknr;
  454. char *block_buffer = NULL;
  455. struct ext2_dirent *dir = NULL;
  456. struct ext_filesystem *fs = get_fs();
  457. uint32_t directory_blocks;
  458. char *direntname;
  459. directory_blocks = le32_to_cpu(parent_inode->size) >>
  460. LOG2_BLOCK_SIZE(ext4fs_root);
  461. block_buffer = zalloc(fs->blksz);
  462. if (!block_buffer)
  463. goto fail;
  464. /* get the block no allocated to a file */
  465. for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
  466. blknr = read_allocated_block(parent_inode, blk_idx);
  467. if (blknr == 0)
  468. goto fail;
  469. /* read the directory block */
  470. status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
  471. 0, fs->blksz, (char *)block_buffer);
  472. if (status == 0)
  473. goto fail;
  474. offset = 0;
  475. do {
  476. dir = (struct ext2_dirent *)(block_buffer + offset);
  477. direntname = (char*)(dir) + sizeof(struct ext2_dirent);
  478. int direntlen = le16_to_cpu(dir->direntlen);
  479. if (direntlen < sizeof(struct ext2_dirent))
  480. break;
  481. if (dir->inode && (strlen(dirname) == dir->namelen) &&
  482. (strncmp(dirname, direntname, dir->namelen) == 0)) {
  483. inodeno = le32_to_cpu(dir->inode);
  484. break;
  485. }
  486. offset += direntlen;
  487. } while (offset < fs->blksz);
  488. if (inodeno > 0) {
  489. free(block_buffer);
  490. return inodeno;
  491. }
  492. }
  493. fail:
  494. free(block_buffer);
  495. return -1;
  496. }
  497. static int find_dir_depth(char *dirname)
  498. {
  499. char *token = strtok(dirname, "/");
  500. int count = 0;
  501. while (token != NULL) {
  502. token = strtok(NULL, "/");
  503. count++;
  504. }
  505. return count + 1 + 1;
  506. /*
  507. * for example for string /home/temp
  508. * depth=home(1)+temp(1)+1 extra for NULL;
  509. * so count is 4;
  510. */
  511. }
  512. static int parse_path(char **arr, char *dirname)
  513. {
  514. char *token = strtok(dirname, "/");
  515. int i = 0;
  516. /* add root */
  517. arr[i] = zalloc(strlen("/") + 1);
  518. if (!arr[i])
  519. return -ENOMEM;
  520. memcpy(arr[i++], "/", strlen("/"));
  521. /* add each path entry after root */
  522. while (token != NULL) {
  523. arr[i] = zalloc(strlen(token) + 1);
  524. if (!arr[i])
  525. return -ENOMEM;
  526. memcpy(arr[i++], token, strlen(token));
  527. token = strtok(NULL, "/");
  528. }
  529. arr[i] = NULL;
  530. return 0;
  531. }
  532. int ext4fs_iget(int inode_no, struct ext2_inode *inode)
  533. {
  534. if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
  535. return -1;
  536. return 0;
  537. }
  538. /*
  539. * Function: ext4fs_get_parent_inode_num
  540. * Return Value: inode Number of the parent directory of file/Directory to be
  541. * created
  542. * dirname : Input parmater, input path name of the file/directory to be created
  543. * dname : Output parameter, to be filled with the name of the directory
  544. * extracted from dirname
  545. */
  546. int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
  547. {
  548. int i;
  549. int depth = 0;
  550. int matched_inode_no;
  551. int result_inode_no = -1;
  552. char **ptr = NULL;
  553. char *depth_dirname = NULL;
  554. char *parse_dirname = NULL;
  555. struct ext2_inode *parent_inode = NULL;
  556. struct ext2_inode *first_inode = NULL;
  557. struct ext2_inode temp_inode;
  558. if (*dirname != '/') {
  559. printf("Please supply Absolute path\n");
  560. return -1;
  561. }
  562. /* TODO: input validation make equivalent to linux */
  563. depth_dirname = zalloc(strlen(dirname) + 1);
  564. if (!depth_dirname)
  565. return -ENOMEM;
  566. memcpy(depth_dirname, dirname, strlen(dirname));
  567. depth = find_dir_depth(depth_dirname);
  568. parse_dirname = zalloc(strlen(dirname) + 1);
  569. if (!parse_dirname)
  570. goto fail;
  571. memcpy(parse_dirname, dirname, strlen(dirname));
  572. /* allocate memory for each directory level */
  573. ptr = zalloc((depth) * sizeof(char *));
  574. if (!ptr)
  575. goto fail;
  576. if (parse_path(ptr, parse_dirname))
  577. goto fail;
  578. parent_inode = zalloc(sizeof(struct ext2_inode));
  579. if (!parent_inode)
  580. goto fail;
  581. first_inode = zalloc(sizeof(struct ext2_inode));
  582. if (!first_inode)
  583. goto fail;
  584. memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
  585. memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
  586. if (flags & F_FILE)
  587. result_inode_no = EXT2_ROOT_INO;
  588. for (i = 1; i < depth; i++) {
  589. matched_inode_no = search_dir(parent_inode, ptr[i]);
  590. if (matched_inode_no == -1) {
  591. if (ptr[i + 1] == NULL && i == 1) {
  592. result_inode_no = EXT2_ROOT_INO;
  593. goto end;
  594. } else {
  595. if (ptr[i + 1] == NULL)
  596. break;
  597. printf("Invalid path\n");
  598. result_inode_no = -1;
  599. goto fail;
  600. }
  601. } else {
  602. if (ptr[i + 1] != NULL) {
  603. memset(parent_inode, '\0',
  604. sizeof(struct ext2_inode));
  605. if (ext4fs_iget(matched_inode_no,
  606. parent_inode)) {
  607. result_inode_no = -1;
  608. goto fail;
  609. }
  610. result_inode_no = matched_inode_no;
  611. } else {
  612. break;
  613. }
  614. }
  615. }
  616. end:
  617. if (i == 1)
  618. matched_inode_no = search_dir(first_inode, ptr[i]);
  619. else
  620. matched_inode_no = search_dir(parent_inode, ptr[i]);
  621. if (matched_inode_no != -1) {
  622. ext4fs_iget(matched_inode_no, &temp_inode);
  623. if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
  624. printf("It is a Directory\n");
  625. result_inode_no = -1;
  626. goto fail;
  627. }
  628. }
  629. if (strlen(ptr[i]) > 256) {
  630. result_inode_no = -1;
  631. goto fail;
  632. }
  633. memcpy(dname, ptr[i], strlen(ptr[i]));
  634. fail:
  635. free(depth_dirname);
  636. free(parse_dirname);
  637. for (i = 0; i < depth; i++) {
  638. if (!ptr[i])
  639. break;
  640. free(ptr[i]);
  641. }
  642. free(ptr);
  643. free(parent_inode);
  644. free(first_inode);
  645. return result_inode_no;
  646. }
  647. static int unlink_filename(char *filename, unsigned int blknr)
  648. {
  649. int totalbytes = 0;
  650. int templength = 0;
  651. int status, inodeno;
  652. int found = 0;
  653. char *root_first_block_buffer = NULL;
  654. struct ext2_dirent *dir = NULL;
  655. struct ext2_dirent *previous_dir = NULL;
  656. char *ptr = NULL;
  657. struct ext_filesystem *fs = get_fs();
  658. int ret = -1;
  659. /* get the first block of root */
  660. root_first_block_buffer = zalloc(fs->blksz);
  661. if (!root_first_block_buffer)
  662. return -ENOMEM;
  663. status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
  664. fs->blksz, root_first_block_buffer);
  665. if (status == 0)
  666. goto fail;
  667. if (ext4fs_log_journal(root_first_block_buffer, blknr))
  668. goto fail;
  669. dir = (struct ext2_dirent *)root_first_block_buffer;
  670. ptr = (char *)dir;
  671. totalbytes = 0;
  672. while (le16_to_cpu(dir->direntlen) >= 0) {
  673. /*
  674. * blocksize-totalbytes because last
  675. * directory length i.e., *dir->direntlen
  676. * is free availble space in the block that
  677. * means it is a last entry of directory entry
  678. */
  679. if (dir->inode && (strlen(filename) == dir->namelen) &&
  680. (strncmp(ptr + sizeof(struct ext2_dirent),
  681. filename, dir->namelen) == 0)) {
  682. printf("file found, deleting\n");
  683. inodeno = le32_to_cpu(dir->inode);
  684. if (previous_dir) {
  685. uint16_t new_len;
  686. new_len = le16_to_cpu(previous_dir->direntlen);
  687. new_len += le16_to_cpu(dir->direntlen);
  688. previous_dir->direntlen = cpu_to_le16(new_len);
  689. } else {
  690. dir->inode = 0;
  691. }
  692. found = 1;
  693. break;
  694. }
  695. if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen))
  696. break;
  697. /* traversing the each directory entry */
  698. templength = le16_to_cpu(dir->direntlen);
  699. totalbytes = totalbytes + templength;
  700. previous_dir = dir;
  701. dir = (struct ext2_dirent *)((char *)dir + templength);
  702. ptr = (char *)dir;
  703. }
  704. if (found == 1) {
  705. if (ext4fs_put_metadata(root_first_block_buffer, blknr))
  706. goto fail;
  707. ret = inodeno;
  708. }
  709. fail:
  710. free(root_first_block_buffer);
  711. return ret;
  712. }
  713. int ext4fs_filename_unlink(char *filename)
  714. {
  715. int blk_idx;
  716. long int blknr = -1;
  717. int inodeno = -1;
  718. uint32_t directory_blocks;
  719. directory_blocks = le32_to_cpu(g_parent_inode->size) >>
  720. LOG2_BLOCK_SIZE(ext4fs_root);
  721. /* read the block no allocated to a file */
  722. for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
  723. blknr = read_allocated_block(g_parent_inode, blk_idx);
  724. if (blknr == 0)
  725. break;
  726. inodeno = unlink_filename(filename, blknr);
  727. if (inodeno != -1)
  728. return inodeno;
  729. }
  730. return -1;
  731. }
  732. uint32_t ext4fs_get_new_blk_no(void)
  733. {
  734. short i;
  735. short status;
  736. int remainder;
  737. unsigned int bg_idx;
  738. static int prev_bg_bitmap_index = -1;
  739. unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
  740. struct ext_filesystem *fs = get_fs();
  741. char *journal_buffer = zalloc(fs->blksz);
  742. char *zero_buffer = zalloc(fs->blksz);
  743. if (!journal_buffer || !zero_buffer)
  744. goto fail;
  745. struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
  746. if (fs->first_pass_bbmap == 0) {
  747. for (i = 0; i < fs->no_blkgrp; i++) {
  748. if (le16_to_cpu(bgd[i].free_blocks)) {
  749. if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
  750. uint16_t new_flags;
  751. put_ext4((uint64_t)le32_to_cpu(bgd[i].block_id) * fs->blksz,
  752. zero_buffer, fs->blksz);
  753. new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
  754. bgd[i].bg_flags = cpu_to_le16(new_flags);
  755. memcpy(fs->blk_bmaps[i], zero_buffer,
  756. fs->blksz);
  757. }
  758. fs->curr_blkno =
  759. _get_new_blk_no(fs->blk_bmaps[i]);
  760. if (fs->curr_blkno == -1)
  761. /* if block bitmap is completely fill */
  762. continue;
  763. fs->curr_blkno = fs->curr_blkno +
  764. (i * fs->blksz * 8);
  765. fs->first_pass_bbmap++;
  766. ext4fs_bg_free_blocks_dec(&bgd[i]);
  767. ext4fs_sb_free_blocks_dec(fs->sb);
  768. status = ext4fs_devread(
  769. (lbaint_t)le32_to_cpu(bgd[i].block_id) *
  770. fs->sect_perblk, 0,
  771. fs->blksz,
  772. journal_buffer);
  773. if (status == 0)
  774. goto fail;
  775. if (ext4fs_log_journal(journal_buffer,
  776. le32_to_cpu(bgd[i].block_id)))
  777. goto fail;
  778. goto success;
  779. } else {
  780. debug("no space left on block group %d\n", i);
  781. }
  782. }
  783. goto fail;
  784. } else {
  785. fs->curr_blkno++;
  786. restart:
  787. /* get the blockbitmap index respective to blockno */
  788. bg_idx = fs->curr_blkno / blk_per_grp;
  789. if (fs->blksz == 1024) {
  790. remainder = fs->curr_blkno % blk_per_grp;
  791. if (!remainder)
  792. bg_idx--;
  793. }
  794. /*
  795. * To skip completely filled block group bitmaps
  796. * Optimize the block allocation
  797. */
  798. if (bg_idx >= fs->no_blkgrp)
  799. goto fail;
  800. if (bgd[bg_idx].free_blocks == 0) {
  801. debug("block group %u is full. Skipping\n", bg_idx);
  802. fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
  803. if (fs->blksz == 1024)
  804. fs->curr_blkno += 1;
  805. goto restart;
  806. }
  807. if (le16_to_cpu(bgd[bg_idx].bg_flags) & EXT4_BG_BLOCK_UNINIT) {
  808. uint16_t new_flags;
  809. put_ext4((uint64_t)le32_to_cpu(bgd[bg_idx].block_id) * fs->blksz,
  810. zero_buffer, fs->blksz);
  811. memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
  812. new_flags = le16_to_cpu(bgd[bg_idx].bg_flags) & ~EXT4_BG_BLOCK_UNINIT;
  813. bgd[bg_idx].bg_flags = cpu_to_le16(new_flags);
  814. }
  815. if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
  816. bg_idx) != 0) {
  817. debug("going for restart for the block no %ld %u\n",
  818. fs->curr_blkno, bg_idx);
  819. fs->curr_blkno++;
  820. goto restart;
  821. }
  822. /* journal backup */
  823. if (prev_bg_bitmap_index != bg_idx) {
  824. status = ext4fs_devread(
  825. (lbaint_t)le32_to_cpu(bgd[bg_idx].block_id)
  826. * fs->sect_perblk,
  827. 0, fs->blksz, journal_buffer);
  828. if (status == 0)
  829. goto fail;
  830. if (ext4fs_log_journal(journal_buffer,
  831. le32_to_cpu(bgd[bg_idx].block_id)))
  832. goto fail;
  833. prev_bg_bitmap_index = bg_idx;
  834. }
  835. ext4fs_bg_free_blocks_dec(&bgd[bg_idx]);
  836. ext4fs_sb_free_blocks_dec(fs->sb);
  837. goto success;
  838. }
  839. success:
  840. free(journal_buffer);
  841. free(zero_buffer);
  842. return fs->curr_blkno;
  843. fail:
  844. free(journal_buffer);
  845. free(zero_buffer);
  846. return -1;
  847. }
  848. int ext4fs_get_new_inode_no(void)
  849. {
  850. short i;
  851. short status;
  852. unsigned int ibmap_idx;
  853. static int prev_inode_bitmap_index = -1;
  854. unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
  855. struct ext_filesystem *fs = get_fs();
  856. char *journal_buffer = zalloc(fs->blksz);
  857. char *zero_buffer = zalloc(fs->blksz);
  858. if (!journal_buffer || !zero_buffer)
  859. goto fail;
  860. struct ext2_block_group *bgd = (struct ext2_block_group *)fs->gdtable;
  861. int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
  862. EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
  863. if (fs->first_pass_ibmap == 0) {
  864. for (i = 0; i < fs->no_blkgrp; i++) {
  865. if (bgd[i].free_inodes) {
  866. if (has_gdt_chksum)
  867. bgd[i].bg_itable_unused =
  868. bgd[i].free_inodes;
  869. if (le16_to_cpu(bgd[i].bg_flags) & EXT4_BG_INODE_UNINIT) {
  870. int new_flags;
  871. put_ext4((uint64_t)le32_to_cpu(bgd[i].inode_id) * fs->blksz,
  872. zero_buffer, fs->blksz);
  873. new_flags = le16_to_cpu(bgd[i].bg_flags) & ~EXT4_BG_INODE_UNINIT;
  874. bgd[i].bg_flags = cpu_to_le16(new_flags);
  875. memcpy(fs->inode_bmaps[i],
  876. zero_buffer, fs->blksz);
  877. }
  878. fs->curr_inode_no =
  879. _get_new_inode_no(fs->inode_bmaps[i]);
  880. if (fs->curr_inode_no == -1)
  881. /* if block bitmap is completely fill */
  882. continue;
  883. fs->curr_inode_no = fs->curr_inode_no +
  884. (i * inodes_per_grp);
  885. fs->first_pass_ibmap++;
  886. ext4fs_bg_free_inodes_dec(&bgd[i]);
  887. if (has_gdt_chksum)
  888. ext4fs_bg_itable_unused_dec(&bgd[i]);
  889. ext4fs_sb_free_inodes_dec(fs->sb);
  890. status = ext4fs_devread(
  891. (lbaint_t)le32_to_cpu(bgd[i].inode_id) *
  892. fs->sect_perblk, 0,
  893. fs->blksz,
  894. journal_buffer);
  895. if (status == 0)
  896. goto fail;
  897. if (ext4fs_log_journal(journal_buffer,
  898. le32_to_cpu(bgd[i].inode_id)))
  899. goto fail;
  900. goto success;
  901. } else
  902. debug("no inode left on block group %d\n", i);
  903. }
  904. goto fail;
  905. } else {
  906. restart:
  907. fs->curr_inode_no++;
  908. /* get the blockbitmap index respective to blockno */
  909. ibmap_idx = fs->curr_inode_no / inodes_per_grp;
  910. if (le16_to_cpu(bgd[ibmap_idx].bg_flags) & EXT4_BG_INODE_UNINIT) {
  911. int new_flags;
  912. put_ext4((uint64_t)le32_to_cpu(bgd[ibmap_idx].inode_id) * fs->blksz,
  913. zero_buffer, fs->blksz);
  914. new_flags = le16_to_cpu(bgd[ibmap_idx].bg_flags) & ~EXT4_BG_INODE_UNINIT;
  915. bgd[ibmap_idx].bg_flags = cpu_to_le16(new_flags);
  916. memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
  917. fs->blksz);
  918. }
  919. if (ext4fs_set_inode_bmap(fs->curr_inode_no,
  920. fs->inode_bmaps[ibmap_idx],
  921. ibmap_idx) != 0) {
  922. debug("going for restart for the block no %d %u\n",
  923. fs->curr_inode_no, ibmap_idx);
  924. goto restart;
  925. }
  926. /* journal backup */
  927. if (prev_inode_bitmap_index != ibmap_idx) {
  928. memset(journal_buffer, '\0', fs->blksz);
  929. status = ext4fs_devread(
  930. (lbaint_t)le32_to_cpu(bgd[ibmap_idx].inode_id)
  931. * fs->sect_perblk,
  932. 0, fs->blksz, journal_buffer);
  933. if (status == 0)
  934. goto fail;
  935. if (ext4fs_log_journal(journal_buffer,
  936. le32_to_cpu(bgd[ibmap_idx].inode_id)))
  937. goto fail;
  938. prev_inode_bitmap_index = ibmap_idx;
  939. }
  940. ext4fs_bg_free_inodes_dec(&bgd[ibmap_idx]);
  941. if (has_gdt_chksum)
  942. bgd[ibmap_idx].bg_itable_unused =
  943. bgd[ibmap_idx].free_inodes;
  944. ext4fs_sb_free_inodes_dec(fs->sb);
  945. goto success;
  946. }
  947. success:
  948. free(journal_buffer);
  949. free(zero_buffer);
  950. return fs->curr_inode_no;
  951. fail:
  952. free(journal_buffer);
  953. free(zero_buffer);
  954. return -1;
  955. }
  956. static void alloc_single_indirect_block(struct ext2_inode *file_inode,
  957. unsigned int *total_remaining_blocks,
  958. unsigned int *no_blks_reqd)
  959. {
  960. short i;
  961. short status;
  962. long int actual_block_no;
  963. long int si_blockno;
  964. /* si :single indirect */
  965. __le32 *si_buffer = NULL;
  966. __le32 *si_start_addr = NULL;
  967. struct ext_filesystem *fs = get_fs();
  968. if (*total_remaining_blocks != 0) {
  969. si_buffer = zalloc(fs->blksz);
  970. if (!si_buffer) {
  971. printf("No Memory\n");
  972. return;
  973. }
  974. si_start_addr = si_buffer;
  975. si_blockno = ext4fs_get_new_blk_no();
  976. if (si_blockno == -1) {
  977. printf("no block left to assign\n");
  978. goto fail;
  979. }
  980. (*no_blks_reqd)++;
  981. debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
  982. status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
  983. 0, fs->blksz, (char *)si_buffer);
  984. memset(si_buffer, '\0', fs->blksz);
  985. if (status == 0)
  986. goto fail;
  987. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  988. actual_block_no = ext4fs_get_new_blk_no();
  989. if (actual_block_no == -1) {
  990. printf("no block left to assign\n");
  991. goto fail;
  992. }
  993. *si_buffer = cpu_to_le32(actual_block_no);
  994. debug("SIAB %u: %u\n", *si_buffer,
  995. *total_remaining_blocks);
  996. si_buffer++;
  997. (*total_remaining_blocks)--;
  998. if (*total_remaining_blocks == 0)
  999. break;
  1000. }
  1001. /* write the block to disk */
  1002. put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
  1003. si_start_addr, fs->blksz);
  1004. file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
  1005. }
  1006. fail:
  1007. free(si_start_addr);
  1008. }
  1009. static void alloc_double_indirect_block(struct ext2_inode *file_inode,
  1010. unsigned int *total_remaining_blocks,
  1011. unsigned int *no_blks_reqd)
  1012. {
  1013. short i;
  1014. short j;
  1015. short status;
  1016. long int actual_block_no;
  1017. /* di:double indirect */
  1018. long int di_blockno_parent;
  1019. long int di_blockno_child;
  1020. __le32 *di_parent_buffer = NULL;
  1021. __le32 *di_child_buff = NULL;
  1022. __le32 *di_block_start_addr = NULL;
  1023. __le32 *di_child_buff_start = NULL;
  1024. struct ext_filesystem *fs = get_fs();
  1025. if (*total_remaining_blocks != 0) {
  1026. /* double indirect parent block connecting to inode */
  1027. di_blockno_parent = ext4fs_get_new_blk_no();
  1028. if (di_blockno_parent == -1) {
  1029. printf("no block left to assign\n");
  1030. goto fail;
  1031. }
  1032. di_parent_buffer = zalloc(fs->blksz);
  1033. if (!di_parent_buffer)
  1034. goto fail;
  1035. di_block_start_addr = di_parent_buffer;
  1036. (*no_blks_reqd)++;
  1037. debug("DIPB %ld: %u\n", di_blockno_parent,
  1038. *total_remaining_blocks);
  1039. status = ext4fs_devread((lbaint_t)di_blockno_parent *
  1040. fs->sect_perblk, 0,
  1041. fs->blksz, (char *)di_parent_buffer);
  1042. if (!status) {
  1043. printf("%s: Device read error!\n", __func__);
  1044. goto fail;
  1045. }
  1046. memset(di_parent_buffer, '\0', fs->blksz);
  1047. /*
  1048. * start:for each double indirect parent
  1049. * block create one more block
  1050. */
  1051. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  1052. di_blockno_child = ext4fs_get_new_blk_no();
  1053. if (di_blockno_child == -1) {
  1054. printf("no block left to assign\n");
  1055. goto fail;
  1056. }
  1057. di_child_buff = zalloc(fs->blksz);
  1058. if (!di_child_buff)
  1059. goto fail;
  1060. di_child_buff_start = di_child_buff;
  1061. *di_parent_buffer = cpu_to_le32(di_blockno_child);
  1062. di_parent_buffer++;
  1063. (*no_blks_reqd)++;
  1064. debug("DICB %ld: %u\n", di_blockno_child,
  1065. *total_remaining_blocks);
  1066. status = ext4fs_devread((lbaint_t)di_blockno_child *
  1067. fs->sect_perblk, 0,
  1068. fs->blksz,
  1069. (char *)di_child_buff);
  1070. if (!status) {
  1071. printf("%s: Device read error!\n", __func__);
  1072. goto fail;
  1073. }
  1074. memset(di_child_buff, '\0', fs->blksz);
  1075. /* filling of actual datablocks for each child */
  1076. for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
  1077. actual_block_no = ext4fs_get_new_blk_no();
  1078. if (actual_block_no == -1) {
  1079. printf("no block left to assign\n");
  1080. goto fail;
  1081. }
  1082. *di_child_buff = cpu_to_le32(actual_block_no);
  1083. debug("DIAB %ld: %u\n", actual_block_no,
  1084. *total_remaining_blocks);
  1085. di_child_buff++;
  1086. (*total_remaining_blocks)--;
  1087. if (*total_remaining_blocks == 0)
  1088. break;
  1089. }
  1090. /* write the block table */
  1091. put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
  1092. di_child_buff_start, fs->blksz);
  1093. free(di_child_buff_start);
  1094. di_child_buff_start = NULL;
  1095. if (*total_remaining_blocks == 0)
  1096. break;
  1097. }
  1098. put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
  1099. di_block_start_addr, fs->blksz);
  1100. file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
  1101. }
  1102. fail:
  1103. free(di_block_start_addr);
  1104. }
  1105. static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
  1106. unsigned int *total_remaining_blocks,
  1107. unsigned int *no_blks_reqd)
  1108. {
  1109. short i;
  1110. short j;
  1111. short k;
  1112. long int actual_block_no;
  1113. /* ti: Triple Indirect */
  1114. long int ti_gp_blockno;
  1115. long int ti_parent_blockno;
  1116. long int ti_child_blockno;
  1117. __le32 *ti_gp_buff = NULL;
  1118. __le32 *ti_parent_buff = NULL;
  1119. __le32 *ti_child_buff = NULL;
  1120. __le32 *ti_gp_buff_start_addr = NULL;
  1121. __le32 *ti_pbuff_start_addr = NULL;
  1122. __le32 *ti_cbuff_start_addr = NULL;
  1123. struct ext_filesystem *fs = get_fs();
  1124. if (*total_remaining_blocks != 0) {
  1125. /* triple indirect grand parent block connecting to inode */
  1126. ti_gp_blockno = ext4fs_get_new_blk_no();
  1127. if (ti_gp_blockno == -1) {
  1128. printf("no block left to assign\n");
  1129. return;
  1130. }
  1131. ti_gp_buff = zalloc(fs->blksz);
  1132. if (!ti_gp_buff)
  1133. return;
  1134. ti_gp_buff_start_addr = ti_gp_buff;
  1135. (*no_blks_reqd)++;
  1136. debug("TIGPB %ld: %u\n", ti_gp_blockno,
  1137. *total_remaining_blocks);
  1138. /* for each 4 byte grand parent entry create one more block */
  1139. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  1140. ti_parent_blockno = ext4fs_get_new_blk_no();
  1141. if (ti_parent_blockno == -1) {
  1142. printf("no block left to assign\n");
  1143. goto fail;
  1144. }
  1145. ti_parent_buff = zalloc(fs->blksz);
  1146. if (!ti_parent_buff)
  1147. goto fail;
  1148. ti_pbuff_start_addr = ti_parent_buff;
  1149. *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
  1150. ti_gp_buff++;
  1151. (*no_blks_reqd)++;
  1152. debug("TIPB %ld: %u\n", ti_parent_blockno,
  1153. *total_remaining_blocks);
  1154. /* for each 4 byte entry parent create one more block */
  1155. for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
  1156. ti_child_blockno = ext4fs_get_new_blk_no();
  1157. if (ti_child_blockno == -1) {
  1158. printf("no block left assign\n");
  1159. goto fail1;
  1160. }
  1161. ti_child_buff = zalloc(fs->blksz);
  1162. if (!ti_child_buff)
  1163. goto fail1;
  1164. ti_cbuff_start_addr = ti_child_buff;
  1165. *ti_parent_buff = cpu_to_le32(ti_child_blockno);
  1166. ti_parent_buff++;
  1167. (*no_blks_reqd)++;
  1168. debug("TICB %ld: %u\n", ti_parent_blockno,
  1169. *total_remaining_blocks);
  1170. /* fill actual datablocks for each child */
  1171. for (k = 0; k < (fs->blksz / sizeof(int));
  1172. k++) {
  1173. actual_block_no =
  1174. ext4fs_get_new_blk_no();
  1175. if (actual_block_no == -1) {
  1176. printf("no block left\n");
  1177. free(ti_cbuff_start_addr);
  1178. goto fail1;
  1179. }
  1180. *ti_child_buff = cpu_to_le32(actual_block_no);
  1181. debug("TIAB %ld: %u\n", actual_block_no,
  1182. *total_remaining_blocks);
  1183. ti_child_buff++;
  1184. (*total_remaining_blocks)--;
  1185. if (*total_remaining_blocks == 0)
  1186. break;
  1187. }
  1188. /* write the child block */
  1189. put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
  1190. (uint64_t)fs->blksz)),
  1191. ti_cbuff_start_addr, fs->blksz);
  1192. free(ti_cbuff_start_addr);
  1193. if (*total_remaining_blocks == 0)
  1194. break;
  1195. }
  1196. /* write the parent block */
  1197. put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
  1198. ti_pbuff_start_addr, fs->blksz);
  1199. free(ti_pbuff_start_addr);
  1200. if (*total_remaining_blocks == 0)
  1201. break;
  1202. }
  1203. /* write the grand parent block */
  1204. put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
  1205. ti_gp_buff_start_addr, fs->blksz);
  1206. file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
  1207. free(ti_gp_buff_start_addr);
  1208. return;
  1209. }
  1210. fail1:
  1211. free(ti_pbuff_start_addr);
  1212. fail:
  1213. free(ti_gp_buff_start_addr);
  1214. }
  1215. void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
  1216. unsigned int total_remaining_blocks,
  1217. unsigned int *total_no_of_block)
  1218. {
  1219. short i;
  1220. long int direct_blockno;
  1221. unsigned int no_blks_reqd = 0;
  1222. /* allocation of direct blocks */
  1223. for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
  1224. direct_blockno = ext4fs_get_new_blk_no();
  1225. if (direct_blockno == -1) {
  1226. printf("no block left to assign\n");
  1227. return;
  1228. }
  1229. file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
  1230. debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
  1231. total_remaining_blocks--;
  1232. }
  1233. alloc_single_indirect_block(file_inode, &total_remaining_blocks,
  1234. &no_blks_reqd);
  1235. alloc_double_indirect_block(file_inode, &total_remaining_blocks,
  1236. &no_blks_reqd);
  1237. alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
  1238. &no_blks_reqd);
  1239. *total_no_of_block += no_blks_reqd;
  1240. }
  1241. #endif
  1242. static struct ext4_extent_header *ext4fs_get_extent_block
  1243. (struct ext2_data *data, char *buf,
  1244. struct ext4_extent_header *ext_block,
  1245. uint32_t fileblock, int log2_blksz)
  1246. {
  1247. struct ext4_extent_idx *index;
  1248. unsigned long long block;
  1249. int blksz = EXT2_BLOCK_SIZE(data);
  1250. int i;
  1251. while (1) {
  1252. index = (struct ext4_extent_idx *)(ext_block + 1);
  1253. if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
  1254. return NULL;
  1255. if (ext_block->eh_depth == 0)
  1256. return ext_block;
  1257. i = -1;
  1258. do {
  1259. i++;
  1260. if (i >= le16_to_cpu(ext_block->eh_entries))
  1261. break;
  1262. } while (fileblock >= le32_to_cpu(index[i].ei_block));
  1263. if (--i < 0)
  1264. return NULL;
  1265. block = le16_to_cpu(index[i].ei_leaf_hi);
  1266. block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
  1267. if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
  1268. buf))
  1269. ext_block = (struct ext4_extent_header *)buf;
  1270. else
  1271. return NULL;
  1272. }
  1273. }
  1274. static int ext4fs_blockgroup
  1275. (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
  1276. {
  1277. long int blkno;
  1278. unsigned int blkoff, desc_per_blk;
  1279. int log2blksz = get_fs()->dev_desc->log2blksz;
  1280. desc_per_blk = EXT2_BLOCK_SIZE(data) / sizeof(struct ext2_block_group);
  1281. blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
  1282. group / desc_per_blk;
  1283. blkoff = (group % desc_per_blk) * sizeof(struct ext2_block_group);
  1284. debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
  1285. group, blkno, blkoff);
  1286. return ext4fs_devread((lbaint_t)blkno <<
  1287. (LOG2_BLOCK_SIZE(data) - log2blksz),
  1288. blkoff, sizeof(struct ext2_block_group),
  1289. (char *)blkgrp);
  1290. }
  1291. int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
  1292. {
  1293. struct ext2_block_group blkgrp;
  1294. struct ext2_sblock *sblock = &data->sblock;
  1295. struct ext_filesystem *fs = get_fs();
  1296. int log2blksz = get_fs()->dev_desc->log2blksz;
  1297. int inodes_per_block, status;
  1298. long int blkno;
  1299. unsigned int blkoff;
  1300. /* It is easier to calculate if the first inode is 0. */
  1301. ino--;
  1302. status = ext4fs_blockgroup(data, ino / le32_to_cpu
  1303. (sblock->inodes_per_group), &blkgrp);
  1304. if (status == 0)
  1305. return 0;
  1306. inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
  1307. blkno = le32_to_cpu(blkgrp.inode_table_id) +
  1308. (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
  1309. blkoff = (ino % inodes_per_block) * fs->inodesz;
  1310. /* Read the inode. */
  1311. status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
  1312. log2blksz), blkoff,
  1313. sizeof(struct ext2_inode), (char *)inode);
  1314. if (status == 0)
  1315. return 0;
  1316. return 1;
  1317. }
  1318. long int read_allocated_block(struct ext2_inode *inode, int fileblock)
  1319. {
  1320. long int blknr;
  1321. int blksz;
  1322. int log2_blksz;
  1323. int status;
  1324. long int rblock;
  1325. long int perblock_parent;
  1326. long int perblock_child;
  1327. unsigned long long start;
  1328. /* get the blocksize of the filesystem */
  1329. blksz = EXT2_BLOCK_SIZE(ext4fs_root);
  1330. log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
  1331. - get_fs()->dev_desc->log2blksz;
  1332. if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
  1333. char *buf = zalloc(blksz);
  1334. if (!buf)
  1335. return -ENOMEM;
  1336. struct ext4_extent_header *ext_block;
  1337. struct ext4_extent *extent;
  1338. int i = -1;
  1339. ext_block =
  1340. ext4fs_get_extent_block(ext4fs_root, buf,
  1341. (struct ext4_extent_header *)
  1342. inode->b.blocks.dir_blocks,
  1343. fileblock, log2_blksz);
  1344. if (!ext_block) {
  1345. printf("invalid extent block\n");
  1346. free(buf);
  1347. return -EINVAL;
  1348. }
  1349. extent = (struct ext4_extent *)(ext_block + 1);
  1350. do {
  1351. i++;
  1352. if (i >= le16_to_cpu(ext_block->eh_entries))
  1353. break;
  1354. } while (fileblock >= le32_to_cpu(extent[i].ee_block));
  1355. if (--i >= 0) {
  1356. fileblock -= le32_to_cpu(extent[i].ee_block);
  1357. if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
  1358. free(buf);
  1359. return 0;
  1360. }
  1361. start = le16_to_cpu(extent[i].ee_start_hi);
  1362. start = (start << 32) +
  1363. le32_to_cpu(extent[i].ee_start_lo);
  1364. free(buf);
  1365. return fileblock + start;
  1366. }
  1367. printf("Extent Error\n");
  1368. free(buf);
  1369. return -1;
  1370. }
  1371. /* Direct blocks. */
  1372. if (fileblock < INDIRECT_BLOCKS)
  1373. blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
  1374. /* Indirect. */
  1375. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
  1376. if (ext4fs_indir1_block == NULL) {
  1377. ext4fs_indir1_block = zalloc(blksz);
  1378. if (ext4fs_indir1_block == NULL) {
  1379. printf("** SI ext2fs read block (indir 1)"
  1380. "malloc failed. **\n");
  1381. return -1;
  1382. }
  1383. ext4fs_indir1_size = blksz;
  1384. ext4fs_indir1_blkno = -1;
  1385. }
  1386. if (blksz != ext4fs_indir1_size) {
  1387. free(ext4fs_indir1_block);
  1388. ext4fs_indir1_block = NULL;
  1389. ext4fs_indir1_size = 0;
  1390. ext4fs_indir1_blkno = -1;
  1391. ext4fs_indir1_block = zalloc(blksz);
  1392. if (ext4fs_indir1_block == NULL) {
  1393. printf("** SI ext2fs read block (indir 1):"
  1394. "malloc failed. **\n");
  1395. return -1;
  1396. }
  1397. ext4fs_indir1_size = blksz;
  1398. }
  1399. if ((le32_to_cpu(inode->b.blocks.indir_block) <<
  1400. log2_blksz) != ext4fs_indir1_blkno) {
  1401. status =
  1402. ext4fs_devread((lbaint_t)le32_to_cpu
  1403. (inode->b.blocks.
  1404. indir_block) << log2_blksz, 0,
  1405. blksz, (char *)ext4fs_indir1_block);
  1406. if (status == 0) {
  1407. printf("** SI ext2fs read block (indir 1)"
  1408. "failed. **\n");
  1409. return 0;
  1410. }
  1411. ext4fs_indir1_blkno =
  1412. le32_to_cpu(inode->b.blocks.
  1413. indir_block) << log2_blksz;
  1414. }
  1415. blknr = le32_to_cpu(ext4fs_indir1_block
  1416. [fileblock - INDIRECT_BLOCKS]);
  1417. }
  1418. /* Double indirect. */
  1419. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
  1420. (blksz / 4 + 1)))) {
  1421. long int perblock = blksz / 4;
  1422. long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
  1423. if (ext4fs_indir1_block == NULL) {
  1424. ext4fs_indir1_block = zalloc(blksz);
  1425. if (ext4fs_indir1_block == NULL) {
  1426. printf("** DI ext2fs read block (indir 2 1)"
  1427. "malloc failed. **\n");
  1428. return -1;
  1429. }
  1430. ext4fs_indir1_size = blksz;
  1431. ext4fs_indir1_blkno = -1;
  1432. }
  1433. if (blksz != ext4fs_indir1_size) {
  1434. free(ext4fs_indir1_block);
  1435. ext4fs_indir1_block = NULL;
  1436. ext4fs_indir1_size = 0;
  1437. ext4fs_indir1_blkno = -1;
  1438. ext4fs_indir1_block = zalloc(blksz);
  1439. if (ext4fs_indir1_block == NULL) {
  1440. printf("** DI ext2fs read block (indir 2 1)"
  1441. "malloc failed. **\n");
  1442. return -1;
  1443. }
  1444. ext4fs_indir1_size = blksz;
  1445. }
  1446. if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
  1447. log2_blksz) != ext4fs_indir1_blkno) {
  1448. status =
  1449. ext4fs_devread((lbaint_t)le32_to_cpu
  1450. (inode->b.blocks.
  1451. double_indir_block) << log2_blksz,
  1452. 0, blksz,
  1453. (char *)ext4fs_indir1_block);
  1454. if (status == 0) {
  1455. printf("** DI ext2fs read block (indir 2 1)"
  1456. "failed. **\n");
  1457. return -1;
  1458. }
  1459. ext4fs_indir1_blkno =
  1460. le32_to_cpu(inode->b.blocks.double_indir_block) <<
  1461. log2_blksz;
  1462. }
  1463. if (ext4fs_indir2_block == NULL) {
  1464. ext4fs_indir2_block = zalloc(blksz);
  1465. if (ext4fs_indir2_block == NULL) {
  1466. printf("** DI ext2fs read block (indir 2 2)"
  1467. "malloc failed. **\n");
  1468. return -1;
  1469. }
  1470. ext4fs_indir2_size = blksz;
  1471. ext4fs_indir2_blkno = -1;
  1472. }
  1473. if (blksz != ext4fs_indir2_size) {
  1474. free(ext4fs_indir2_block);
  1475. ext4fs_indir2_block = NULL;
  1476. ext4fs_indir2_size = 0;
  1477. ext4fs_indir2_blkno = -1;
  1478. ext4fs_indir2_block = zalloc(blksz);
  1479. if (ext4fs_indir2_block == NULL) {
  1480. printf("** DI ext2fs read block (indir 2 2)"
  1481. "malloc failed. **\n");
  1482. return -1;
  1483. }
  1484. ext4fs_indir2_size = blksz;
  1485. }
  1486. if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
  1487. log2_blksz) != ext4fs_indir2_blkno) {
  1488. status = ext4fs_devread((lbaint_t)le32_to_cpu
  1489. (ext4fs_indir1_block
  1490. [rblock /
  1491. perblock]) << log2_blksz, 0,
  1492. blksz,
  1493. (char *)ext4fs_indir2_block);
  1494. if (status == 0) {
  1495. printf("** DI ext2fs read block (indir 2 2)"
  1496. "failed. **\n");
  1497. return -1;
  1498. }
  1499. ext4fs_indir2_blkno =
  1500. le32_to_cpu(ext4fs_indir1_block[rblock
  1501. /
  1502. perblock]) <<
  1503. log2_blksz;
  1504. }
  1505. blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
  1506. }
  1507. /* Tripple indirect. */
  1508. else {
  1509. rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
  1510. (blksz / 4 * blksz / 4));
  1511. perblock_child = blksz / 4;
  1512. perblock_parent = ((blksz / 4) * (blksz / 4));
  1513. if (ext4fs_indir1_block == NULL) {
  1514. ext4fs_indir1_block = zalloc(blksz);
  1515. if (ext4fs_indir1_block == NULL) {
  1516. printf("** TI ext2fs read block (indir 2 1)"
  1517. "malloc failed. **\n");
  1518. return -1;
  1519. }
  1520. ext4fs_indir1_size = blksz;
  1521. ext4fs_indir1_blkno = -1;
  1522. }
  1523. if (blksz != ext4fs_indir1_size) {
  1524. free(ext4fs_indir1_block);
  1525. ext4fs_indir1_block = NULL;
  1526. ext4fs_indir1_size = 0;
  1527. ext4fs_indir1_blkno = -1;
  1528. ext4fs_indir1_block = zalloc(blksz);
  1529. if (ext4fs_indir1_block == NULL) {
  1530. printf("** TI ext2fs read block (indir 2 1)"
  1531. "malloc failed. **\n");
  1532. return -1;
  1533. }
  1534. ext4fs_indir1_size = blksz;
  1535. }
  1536. if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
  1537. log2_blksz) != ext4fs_indir1_blkno) {
  1538. status = ext4fs_devread
  1539. ((lbaint_t)
  1540. le32_to_cpu(inode->b.blocks.triple_indir_block)
  1541. << log2_blksz, 0, blksz,
  1542. (char *)ext4fs_indir1_block);
  1543. if (status == 0) {
  1544. printf("** TI ext2fs read block (indir 2 1)"
  1545. "failed. **\n");
  1546. return -1;
  1547. }
  1548. ext4fs_indir1_blkno =
  1549. le32_to_cpu(inode->b.blocks.triple_indir_block) <<
  1550. log2_blksz;
  1551. }
  1552. if (ext4fs_indir2_block == NULL) {
  1553. ext4fs_indir2_block = zalloc(blksz);
  1554. if (ext4fs_indir2_block == NULL) {
  1555. printf("** TI ext2fs read block (indir 2 2)"
  1556. "malloc failed. **\n");
  1557. return -1;
  1558. }
  1559. ext4fs_indir2_size = blksz;
  1560. ext4fs_indir2_blkno = -1;
  1561. }
  1562. if (blksz != ext4fs_indir2_size) {
  1563. free(ext4fs_indir2_block);
  1564. ext4fs_indir2_block = NULL;
  1565. ext4fs_indir2_size = 0;
  1566. ext4fs_indir2_blkno = -1;
  1567. ext4fs_indir2_block = zalloc(blksz);
  1568. if (ext4fs_indir2_block == NULL) {
  1569. printf("** TI ext2fs read block (indir 2 2)"
  1570. "malloc failed. **\n");
  1571. return -1;
  1572. }
  1573. ext4fs_indir2_size = blksz;
  1574. }
  1575. if ((le32_to_cpu(ext4fs_indir1_block[rblock /
  1576. perblock_parent]) <<
  1577. log2_blksz)
  1578. != ext4fs_indir2_blkno) {
  1579. status = ext4fs_devread((lbaint_t)le32_to_cpu
  1580. (ext4fs_indir1_block
  1581. [rblock /
  1582. perblock_parent]) <<
  1583. log2_blksz, 0, blksz,
  1584. (char *)ext4fs_indir2_block);
  1585. if (status == 0) {
  1586. printf("** TI ext2fs read block (indir 2 2)"
  1587. "failed. **\n");
  1588. return -1;
  1589. }
  1590. ext4fs_indir2_blkno =
  1591. le32_to_cpu(ext4fs_indir1_block[rblock /
  1592. perblock_parent])
  1593. << log2_blksz;
  1594. }
  1595. if (ext4fs_indir3_block == NULL) {
  1596. ext4fs_indir3_block = zalloc(blksz);
  1597. if (ext4fs_indir3_block == NULL) {
  1598. printf("** TI ext2fs read block (indir 2 2)"
  1599. "malloc failed. **\n");
  1600. return -1;
  1601. }
  1602. ext4fs_indir3_size = blksz;
  1603. ext4fs_indir3_blkno = -1;
  1604. }
  1605. if (blksz != ext4fs_indir3_size) {
  1606. free(ext4fs_indir3_block);
  1607. ext4fs_indir3_block = NULL;
  1608. ext4fs_indir3_size = 0;
  1609. ext4fs_indir3_blkno = -1;
  1610. ext4fs_indir3_block = zalloc(blksz);
  1611. if (ext4fs_indir3_block == NULL) {
  1612. printf("** TI ext2fs read block (indir 2 2)"
  1613. "malloc failed. **\n");
  1614. return -1;
  1615. }
  1616. ext4fs_indir3_size = blksz;
  1617. }
  1618. if ((le32_to_cpu(ext4fs_indir2_block[rblock
  1619. /
  1620. perblock_child]) <<
  1621. log2_blksz) != ext4fs_indir3_blkno) {
  1622. status =
  1623. ext4fs_devread((lbaint_t)le32_to_cpu
  1624. (ext4fs_indir2_block
  1625. [(rblock / perblock_child)
  1626. % (blksz / 4)]) << log2_blksz, 0,
  1627. blksz, (char *)ext4fs_indir3_block);
  1628. if (status == 0) {
  1629. printf("** TI ext2fs read block (indir 2 2)"
  1630. "failed. **\n");
  1631. return -1;
  1632. }
  1633. ext4fs_indir3_blkno =
  1634. le32_to_cpu(ext4fs_indir2_block[(rblock /
  1635. perblock_child) %
  1636. (blksz /
  1637. 4)]) <<
  1638. log2_blksz;
  1639. }
  1640. blknr = le32_to_cpu(ext4fs_indir3_block
  1641. [rblock % perblock_child]);
  1642. }
  1643. debug("read_allocated_block %ld\n", blknr);
  1644. return blknr;
  1645. }
  1646. /**
  1647. * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
  1648. * global pointers
  1649. *
  1650. * This function assures that for a file with the same name but different size
  1651. * the sequential store on the ext4 filesystem will be correct.
  1652. *
  1653. * In this function the global data, responsible for internal representation
  1654. * of the ext4 data are initialized to the reset state. Without this, during
  1655. * replacement of the smaller file with the bigger truncation of new file was
  1656. * performed.
  1657. */
  1658. void ext4fs_reinit_global(void)
  1659. {
  1660. if (ext4fs_indir1_block != NULL) {
  1661. free(ext4fs_indir1_block);
  1662. ext4fs_indir1_block = NULL;
  1663. ext4fs_indir1_size = 0;
  1664. ext4fs_indir1_blkno = -1;
  1665. }
  1666. if (ext4fs_indir2_block != NULL) {
  1667. free(ext4fs_indir2_block);
  1668. ext4fs_indir2_block = NULL;
  1669. ext4fs_indir2_size = 0;
  1670. ext4fs_indir2_blkno = -1;
  1671. }
  1672. if (ext4fs_indir3_block != NULL) {
  1673. free(ext4fs_indir3_block);
  1674. ext4fs_indir3_block = NULL;
  1675. ext4fs_indir3_size = 0;
  1676. ext4fs_indir3_blkno = -1;
  1677. }
  1678. }
  1679. void ext4fs_close(void)
  1680. {
  1681. if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
  1682. ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
  1683. ext4fs_file = NULL;
  1684. }
  1685. if (ext4fs_root != NULL) {
  1686. free(ext4fs_root);
  1687. ext4fs_root = NULL;
  1688. }
  1689. ext4fs_reinit_global();
  1690. }
  1691. int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
  1692. struct ext2fs_node **fnode, int *ftype)
  1693. {
  1694. unsigned int fpos = 0;
  1695. int status;
  1696. loff_t actread;
  1697. struct ext2fs_node *diro = (struct ext2fs_node *) dir;
  1698. #ifdef DEBUG
  1699. if (name != NULL)
  1700. printf("Iterate dir %s\n", name);
  1701. #endif /* of DEBUG */
  1702. if (!diro->inode_read) {
  1703. status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
  1704. if (status == 0)
  1705. return 0;
  1706. }
  1707. /* Search the file. */
  1708. while (fpos < le32_to_cpu(diro->inode.size)) {
  1709. struct ext2_dirent dirent;
  1710. status = ext4fs_read_file(diro, fpos,
  1711. sizeof(struct ext2_dirent),
  1712. (char *)&dirent, &actread);
  1713. if (status < 0)
  1714. return 0;
  1715. if (dirent.direntlen == 0) {
  1716. printf("Failed to iterate over directory %s\n", name);
  1717. return 0;
  1718. }
  1719. if (dirent.namelen != 0) {
  1720. char filename[dirent.namelen + 1];
  1721. struct ext2fs_node *fdiro;
  1722. int type = FILETYPE_UNKNOWN;
  1723. status = ext4fs_read_file(diro,
  1724. fpos +
  1725. sizeof(struct ext2_dirent),
  1726. dirent.namelen, filename,
  1727. &actread);
  1728. if (status < 0)
  1729. return 0;
  1730. fdiro = zalloc(sizeof(struct ext2fs_node));
  1731. if (!fdiro)
  1732. return 0;
  1733. fdiro->data = diro->data;
  1734. fdiro->ino = le32_to_cpu(dirent.inode);
  1735. filename[dirent.namelen] = '\0';
  1736. if (dirent.filetype != FILETYPE_UNKNOWN) {
  1737. fdiro->inode_read = 0;
  1738. if (dirent.filetype == FILETYPE_DIRECTORY)
  1739. type = FILETYPE_DIRECTORY;
  1740. else if (dirent.filetype == FILETYPE_SYMLINK)
  1741. type = FILETYPE_SYMLINK;
  1742. else if (dirent.filetype == FILETYPE_REG)
  1743. type = FILETYPE_REG;
  1744. } else {
  1745. status = ext4fs_read_inode(diro->data,
  1746. le32_to_cpu
  1747. (dirent.inode),
  1748. &fdiro->inode);
  1749. if (status == 0) {
  1750. free(fdiro);
  1751. return 0;
  1752. }
  1753. fdiro->inode_read = 1;
  1754. if ((le16_to_cpu(fdiro->inode.mode) &
  1755. FILETYPE_INO_MASK) ==
  1756. FILETYPE_INO_DIRECTORY) {
  1757. type = FILETYPE_DIRECTORY;
  1758. } else if ((le16_to_cpu(fdiro->inode.mode)
  1759. & FILETYPE_INO_MASK) ==
  1760. FILETYPE_INO_SYMLINK) {
  1761. type = FILETYPE_SYMLINK;
  1762. } else if ((le16_to_cpu(fdiro->inode.mode)
  1763. & FILETYPE_INO_MASK) ==
  1764. FILETYPE_INO_REG) {
  1765. type = FILETYPE_REG;
  1766. }
  1767. }
  1768. #ifdef DEBUG
  1769. printf("iterate >%s<\n", filename);
  1770. #endif /* of DEBUG */
  1771. if ((name != NULL) && (fnode != NULL)
  1772. && (ftype != NULL)) {
  1773. if (strcmp(filename, name) == 0) {
  1774. *ftype = type;
  1775. *fnode = fdiro;
  1776. return 1;
  1777. }
  1778. } else {
  1779. if (fdiro->inode_read == 0) {
  1780. status = ext4fs_read_inode(diro->data,
  1781. le32_to_cpu(
  1782. dirent.inode),
  1783. &fdiro->inode);
  1784. if (status == 0) {
  1785. free(fdiro);
  1786. return 0;
  1787. }
  1788. fdiro->inode_read = 1;
  1789. }
  1790. switch (type) {
  1791. case FILETYPE_DIRECTORY:
  1792. printf("<DIR> ");
  1793. break;
  1794. case FILETYPE_SYMLINK:
  1795. printf("<SYM> ");
  1796. break;
  1797. case FILETYPE_REG:
  1798. printf(" ");
  1799. break;
  1800. default:
  1801. printf("< ? > ");
  1802. break;
  1803. }
  1804. printf("%10u %s\n",
  1805. le32_to_cpu(fdiro->inode.size),
  1806. filename);
  1807. }
  1808. free(fdiro);
  1809. }
  1810. fpos += le16_to_cpu(dirent.direntlen);
  1811. }
  1812. return 0;
  1813. }
  1814. static char *ext4fs_read_symlink(struct ext2fs_node *node)
  1815. {
  1816. char *symlink;
  1817. struct ext2fs_node *diro = node;
  1818. int status;
  1819. loff_t actread;
  1820. if (!diro->inode_read) {
  1821. status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
  1822. if (status == 0)
  1823. return NULL;
  1824. }
  1825. symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
  1826. if (!symlink)
  1827. return NULL;
  1828. if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
  1829. strncpy(symlink, diro->inode.b.symlink,
  1830. le32_to_cpu(diro->inode.size));
  1831. } else {
  1832. status = ext4fs_read_file(diro, 0,
  1833. le32_to_cpu(diro->inode.size),
  1834. symlink, &actread);
  1835. if ((status < 0) || (actread == 0)) {
  1836. free(symlink);
  1837. return NULL;
  1838. }
  1839. }
  1840. symlink[le32_to_cpu(diro->inode.size)] = '\0';
  1841. return symlink;
  1842. }
  1843. static int ext4fs_find_file1(const char *currpath,
  1844. struct ext2fs_node *currroot,
  1845. struct ext2fs_node **currfound, int *foundtype)
  1846. {
  1847. char fpath[strlen(currpath) + 1];
  1848. char *name = fpath;
  1849. char *next;
  1850. int status;
  1851. int type = FILETYPE_DIRECTORY;
  1852. struct ext2fs_node *currnode = currroot;
  1853. struct ext2fs_node *oldnode = currroot;
  1854. strncpy(fpath, currpath, strlen(currpath) + 1);
  1855. /* Remove all leading slashes. */
  1856. while (*name == '/')
  1857. name++;
  1858. if (!*name) {
  1859. *currfound = currnode;
  1860. return 1;
  1861. }
  1862. for (;;) {
  1863. int found;
  1864. /* Extract the actual part from the pathname. */
  1865. next = strchr(name, '/');
  1866. if (next) {
  1867. /* Remove all leading slashes. */
  1868. while (*next == '/')
  1869. *(next++) = '\0';
  1870. }
  1871. if (type != FILETYPE_DIRECTORY) {
  1872. ext4fs_free_node(currnode, currroot);
  1873. return 0;
  1874. }
  1875. oldnode = currnode;
  1876. /* Iterate over the directory. */
  1877. found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
  1878. if (found == 0)
  1879. return 0;
  1880. if (found == -1)
  1881. break;
  1882. /* Read in the symlink and follow it. */
  1883. if (type == FILETYPE_SYMLINK) {
  1884. char *symlink;
  1885. /* Test if the symlink does not loop. */
  1886. if (++symlinknest == 8) {
  1887. ext4fs_free_node(currnode, currroot);
  1888. ext4fs_free_node(oldnode, currroot);
  1889. return 0;
  1890. }
  1891. symlink = ext4fs_read_symlink(currnode);
  1892. ext4fs_free_node(currnode, currroot);
  1893. if (!symlink) {
  1894. ext4fs_free_node(oldnode, currroot);
  1895. return 0;
  1896. }
  1897. debug("Got symlink >%s<\n", symlink);
  1898. if (symlink[0] == '/') {
  1899. ext4fs_free_node(oldnode, currroot);
  1900. oldnode = &ext4fs_root->diropen;
  1901. }
  1902. /* Lookup the node the symlink points to. */
  1903. status = ext4fs_find_file1(symlink, oldnode,
  1904. &currnode, &type);
  1905. free(symlink);
  1906. if (status == 0) {
  1907. ext4fs_free_node(oldnode, currroot);
  1908. return 0;
  1909. }
  1910. }
  1911. ext4fs_free_node(oldnode, currroot);
  1912. /* Found the node! */
  1913. if (!next || *next == '\0') {
  1914. *currfound = currnode;
  1915. *foundtype = type;
  1916. return 1;
  1917. }
  1918. name = next;
  1919. }
  1920. return -1;
  1921. }
  1922. int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
  1923. struct ext2fs_node **foundnode, int expecttype)
  1924. {
  1925. int status;
  1926. int foundtype = FILETYPE_DIRECTORY;
  1927. symlinknest = 0;
  1928. if (!path)
  1929. return 0;
  1930. status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
  1931. if (status == 0)
  1932. return 0;
  1933. /* Check if the node that was found was of the expected type. */
  1934. if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
  1935. return 0;
  1936. else if ((expecttype == FILETYPE_DIRECTORY)
  1937. && (foundtype != expecttype))
  1938. return 0;
  1939. return 1;
  1940. }
  1941. int ext4fs_open(const char *filename, loff_t *len)
  1942. {
  1943. struct ext2fs_node *fdiro = NULL;
  1944. int status;
  1945. if (ext4fs_root == NULL)
  1946. return -1;
  1947. ext4fs_file = NULL;
  1948. status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
  1949. FILETYPE_REG);
  1950. if (status == 0)
  1951. goto fail;
  1952. if (!fdiro->inode_read) {
  1953. status = ext4fs_read_inode(fdiro->data, fdiro->ino,
  1954. &fdiro->inode);
  1955. if (status == 0)
  1956. goto fail;
  1957. }
  1958. *len = le32_to_cpu(fdiro->inode.size);
  1959. ext4fs_file = fdiro;
  1960. return 0;
  1961. fail:
  1962. ext4fs_free_node(fdiro, &ext4fs_root->diropen);
  1963. return -1;
  1964. }
  1965. int ext4fs_mount(unsigned part_length)
  1966. {
  1967. struct ext2_data *data;
  1968. int status;
  1969. struct ext_filesystem *fs = get_fs();
  1970. data = zalloc(SUPERBLOCK_SIZE);
  1971. if (!data)
  1972. return 0;
  1973. /* Read the superblock. */
  1974. status = ext4_read_superblock((char *)&data->sblock);
  1975. if (status == 0)
  1976. goto fail;
  1977. /* Make sure this is an ext2 filesystem. */
  1978. if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
  1979. goto fail;
  1980. /*
  1981. * The 64bit feature was enabled when metadata_csum was enabled
  1982. * and we do not support metadata_csum (and cannot reliably find
  1983. * files when it is set. Refuse to mount.
  1984. */
  1985. if (le32_to_cpu(data->sblock.feature_incompat) & EXT4_FEATURE_INCOMPAT_64BIT) {
  1986. printf("Unsupported feature found (64bit, possibly metadata_csum), not mounting\n");
  1987. goto fail;
  1988. }
  1989. if (le32_to_cpu(data->sblock.revision_level) == 0)
  1990. fs->inodesz = 128;
  1991. else
  1992. fs->inodesz = le16_to_cpu(data->sblock.inode_size);
  1993. debug("EXT2 rev %d, inode_size %d\n",
  1994. le32_to_cpu(data->sblock.revision_level), fs->inodesz);
  1995. data->diropen.data = data;
  1996. data->diropen.ino = 2;
  1997. data->diropen.inode_read = 1;
  1998. data->inode = &data->diropen.inode;
  1999. status = ext4fs_read_inode(data, 2, data->inode);
  2000. if (status == 0)
  2001. goto fail;
  2002. ext4fs_root = data;
  2003. return 1;
  2004. fail:
  2005. printf("Failed to mount ext2 filesystem...\n");
  2006. free(data);
  2007. ext4fs_root = NULL;
  2008. return 0;
  2009. }