ext4_common.c 60 KB

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