ext4_common.c 60 KB

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