ext4_common.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424
  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 = ext2fs_crc16(~0, fs->sb->unique_id,
  378. sizeof(fs->sb->unique_id));
  379. crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
  380. crc = ext2fs_crc16(crc, desc, offset);
  381. offset += sizeof(desc->bg_checksum); /* skip checksum */
  382. assert(offset == sizeof(*desc));
  383. if (offset < fs->gdsize) {
  384. crc = ext2fs_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. free(parse_dirname);
  744. for (i = 0; i < depth; i++) {
  745. if (!ptr[i])
  746. break;
  747. free(ptr[i]);
  748. }
  749. free(ptr);
  750. free(parent_inode);
  751. free(first_inode);
  752. return result_inode_no;
  753. }
  754. static int unlink_filename(char *filename, unsigned int blknr)
  755. {
  756. int status;
  757. int inodeno = 0;
  758. int offset;
  759. char *block_buffer = NULL;
  760. struct ext2_dirent *dir = NULL;
  761. struct ext2_dirent *previous_dir;
  762. struct ext_filesystem *fs = get_fs();
  763. int ret = -1;
  764. char *direntname;
  765. block_buffer = zalloc(fs->blksz);
  766. if (!block_buffer)
  767. return -ENOMEM;
  768. /* read the directory block */
  769. status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
  770. fs->blksz, block_buffer);
  771. if (status == 0)
  772. goto fail;
  773. offset = 0;
  774. do {
  775. if (offset & 3) {
  776. printf("Badly aligned ext2_dirent\n");
  777. break;
  778. }
  779. previous_dir = dir;
  780. dir = (struct ext2_dirent *)(block_buffer + offset);
  781. direntname = (char *)(dir) + sizeof(struct ext2_dirent);
  782. int direntlen = le16_to_cpu(dir->direntlen);
  783. if (direntlen < sizeof(struct ext2_dirent))
  784. break;
  785. if (dir->inode && (strlen(filename) == dir->namelen) &&
  786. (strncmp(direntname, filename, dir->namelen) == 0)) {
  787. inodeno = le32_to_cpu(dir->inode);
  788. break;
  789. }
  790. offset += direntlen;
  791. } while (offset < fs->blksz);
  792. if (inodeno > 0) {
  793. printf("file found, deleting\n");
  794. if (ext4fs_log_journal(block_buffer, blknr))
  795. goto fail;
  796. if (previous_dir) {
  797. /* merge dir entry with predecessor */
  798. uint16_t new_len;
  799. new_len = le16_to_cpu(previous_dir->direntlen);
  800. new_len += le16_to_cpu(dir->direntlen);
  801. previous_dir->direntlen = cpu_to_le16(new_len);
  802. } else {
  803. /* invalidate dir entry */
  804. dir->inode = 0;
  805. }
  806. if (ext4fs_put_metadata(block_buffer, blknr))
  807. goto fail;
  808. ret = inodeno;
  809. }
  810. fail:
  811. free(block_buffer);
  812. return ret;
  813. }
  814. int ext4fs_filename_unlink(char *filename)
  815. {
  816. int blk_idx;
  817. long int blknr = -1;
  818. int inodeno = -1;
  819. uint32_t directory_blocks;
  820. directory_blocks = le32_to_cpu(g_parent_inode->size) >>
  821. LOG2_BLOCK_SIZE(ext4fs_root);
  822. /* read the block no allocated to a file */
  823. for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
  824. blknr = read_allocated_block(g_parent_inode, blk_idx, NULL);
  825. if (blknr <= 0)
  826. break;
  827. inodeno = unlink_filename(filename, blknr);
  828. if (inodeno != -1)
  829. return inodeno;
  830. }
  831. return -1;
  832. }
  833. uint32_t ext4fs_get_new_blk_no(void)
  834. {
  835. short i;
  836. short status;
  837. int remainder;
  838. unsigned int bg_idx;
  839. static int prev_bg_bitmap_index = -1;
  840. unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
  841. struct ext_filesystem *fs = get_fs();
  842. char *journal_buffer = zalloc(fs->blksz);
  843. char *zero_buffer = zalloc(fs->blksz);
  844. if (!journal_buffer || !zero_buffer)
  845. goto fail;
  846. if (fs->first_pass_bbmap == 0) {
  847. for (i = 0; i < fs->no_blkgrp; i++) {
  848. struct ext2_block_group *bgd = NULL;
  849. bgd = ext4fs_get_group_descriptor(fs, i);
  850. if (ext4fs_bg_get_free_blocks(bgd, fs)) {
  851. uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
  852. uint64_t b_bitmap_blk =
  853. ext4fs_bg_get_block_id(bgd, fs);
  854. if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
  855. memcpy(fs->blk_bmaps[i], zero_buffer,
  856. fs->blksz);
  857. put_ext4(b_bitmap_blk * fs->blksz,
  858. fs->blk_bmaps[i], fs->blksz);
  859. bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
  860. ext4fs_bg_set_flags(bgd, bg_flags);
  861. }
  862. fs->curr_blkno =
  863. _get_new_blk_no(fs->blk_bmaps[i]);
  864. if (fs->curr_blkno == -1)
  865. /* block bitmap is completely filled */
  866. continue;
  867. fs->curr_blkno = fs->curr_blkno +
  868. (i * fs->blksz * 8);
  869. fs->first_pass_bbmap++;
  870. ext4fs_bg_free_blocks_dec(bgd, fs);
  871. ext4fs_sb_free_blocks_dec(fs->sb);
  872. status = ext4fs_devread(b_bitmap_blk *
  873. fs->sect_perblk,
  874. 0, fs->blksz,
  875. journal_buffer);
  876. if (status == 0)
  877. goto fail;
  878. if (ext4fs_log_journal(journal_buffer,
  879. b_bitmap_blk))
  880. goto fail;
  881. goto success;
  882. } else {
  883. debug("no space left on block group %d\n", i);
  884. }
  885. }
  886. goto fail;
  887. } else {
  888. fs->curr_blkno++;
  889. restart:
  890. /* get the blockbitmap index respective to blockno */
  891. bg_idx = fs->curr_blkno / blk_per_grp;
  892. if (fs->blksz == 1024) {
  893. remainder = fs->curr_blkno % blk_per_grp;
  894. if (!remainder)
  895. bg_idx--;
  896. }
  897. /*
  898. * To skip completely filled block group bitmaps
  899. * Optimize the block allocation
  900. */
  901. if (bg_idx >= fs->no_blkgrp)
  902. goto fail;
  903. struct ext2_block_group *bgd = NULL;
  904. bgd = ext4fs_get_group_descriptor(fs, bg_idx);
  905. if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
  906. debug("block group %u is full. Skipping\n", bg_idx);
  907. fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
  908. if (fs->blksz == 1024)
  909. fs->curr_blkno += 1;
  910. goto restart;
  911. }
  912. uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
  913. uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
  914. if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
  915. memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
  916. put_ext4(b_bitmap_blk * fs->blksz,
  917. zero_buffer, fs->blksz);
  918. bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
  919. ext4fs_bg_set_flags(bgd, bg_flags);
  920. }
  921. if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
  922. bg_idx) != 0) {
  923. debug("going for restart for the block no %ld %u\n",
  924. fs->curr_blkno, bg_idx);
  925. fs->curr_blkno++;
  926. goto restart;
  927. }
  928. /* journal backup */
  929. if (prev_bg_bitmap_index != bg_idx) {
  930. status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
  931. 0, fs->blksz, journal_buffer);
  932. if (status == 0)
  933. goto fail;
  934. if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
  935. goto fail;
  936. prev_bg_bitmap_index = bg_idx;
  937. }
  938. ext4fs_bg_free_blocks_dec(bgd, fs);
  939. ext4fs_sb_free_blocks_dec(fs->sb);
  940. goto success;
  941. }
  942. success:
  943. free(journal_buffer);
  944. free(zero_buffer);
  945. return fs->curr_blkno;
  946. fail:
  947. free(journal_buffer);
  948. free(zero_buffer);
  949. return -1;
  950. }
  951. int ext4fs_get_new_inode_no(void)
  952. {
  953. short i;
  954. short status;
  955. unsigned int ibmap_idx;
  956. static int prev_inode_bitmap_index = -1;
  957. unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
  958. struct ext_filesystem *fs = get_fs();
  959. char *journal_buffer = zalloc(fs->blksz);
  960. char *zero_buffer = zalloc(fs->blksz);
  961. if (!journal_buffer || !zero_buffer)
  962. goto fail;
  963. int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
  964. EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
  965. if (fs->first_pass_ibmap == 0) {
  966. for (i = 0; i < fs->no_blkgrp; i++) {
  967. uint32_t free_inodes;
  968. struct ext2_block_group *bgd = NULL;
  969. bgd = ext4fs_get_group_descriptor(fs, i);
  970. free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
  971. if (free_inodes) {
  972. uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
  973. uint64_t i_bitmap_blk =
  974. ext4fs_bg_get_inode_id(bgd, fs);
  975. if (has_gdt_chksum)
  976. bgd->bg_itable_unused = free_inodes;
  977. if (bg_flags & EXT4_BG_INODE_UNINIT) {
  978. put_ext4(i_bitmap_blk * fs->blksz,
  979. zero_buffer, fs->blksz);
  980. bg_flags &= ~EXT4_BG_INODE_UNINIT;
  981. ext4fs_bg_set_flags(bgd, bg_flags);
  982. memcpy(fs->inode_bmaps[i],
  983. zero_buffer, fs->blksz);
  984. }
  985. fs->curr_inode_no =
  986. _get_new_inode_no(fs->inode_bmaps[i]);
  987. if (fs->curr_inode_no == -1)
  988. /* inode bitmap is completely filled */
  989. continue;
  990. fs->curr_inode_no = fs->curr_inode_no +
  991. (i * inodes_per_grp);
  992. fs->first_pass_ibmap++;
  993. ext4fs_bg_free_inodes_dec(bgd, fs);
  994. if (has_gdt_chksum)
  995. ext4fs_bg_itable_unused_dec(bgd, fs);
  996. ext4fs_sb_free_inodes_dec(fs->sb);
  997. status = ext4fs_devread(i_bitmap_blk *
  998. fs->sect_perblk,
  999. 0, fs->blksz,
  1000. journal_buffer);
  1001. if (status == 0)
  1002. goto fail;
  1003. if (ext4fs_log_journal(journal_buffer,
  1004. i_bitmap_blk))
  1005. goto fail;
  1006. goto success;
  1007. } else
  1008. debug("no inode left on block group %d\n", i);
  1009. }
  1010. goto fail;
  1011. } else {
  1012. restart:
  1013. fs->curr_inode_no++;
  1014. /* get the blockbitmap index respective to blockno */
  1015. ibmap_idx = fs->curr_inode_no / inodes_per_grp;
  1016. struct ext2_block_group *bgd =
  1017. ext4fs_get_group_descriptor(fs, ibmap_idx);
  1018. uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
  1019. uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
  1020. if (bg_flags & EXT4_BG_INODE_UNINIT) {
  1021. put_ext4(i_bitmap_blk * fs->blksz,
  1022. zero_buffer, fs->blksz);
  1023. bg_flags &= ~EXT4_BG_INODE_UNINIT;
  1024. ext4fs_bg_set_flags(bgd, bg_flags);
  1025. memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
  1026. fs->blksz);
  1027. }
  1028. if (ext4fs_set_inode_bmap(fs->curr_inode_no,
  1029. fs->inode_bmaps[ibmap_idx],
  1030. ibmap_idx) != 0) {
  1031. debug("going for restart for the block no %d %u\n",
  1032. fs->curr_inode_no, ibmap_idx);
  1033. goto restart;
  1034. }
  1035. /* journal backup */
  1036. if (prev_inode_bitmap_index != ibmap_idx) {
  1037. status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
  1038. 0, fs->blksz, journal_buffer);
  1039. if (status == 0)
  1040. goto fail;
  1041. if (ext4fs_log_journal(journal_buffer,
  1042. le32_to_cpu(bgd->inode_id)))
  1043. goto fail;
  1044. prev_inode_bitmap_index = ibmap_idx;
  1045. }
  1046. ext4fs_bg_free_inodes_dec(bgd, fs);
  1047. if (has_gdt_chksum)
  1048. bgd->bg_itable_unused = bgd->free_inodes;
  1049. ext4fs_sb_free_inodes_dec(fs->sb);
  1050. goto success;
  1051. }
  1052. success:
  1053. free(journal_buffer);
  1054. free(zero_buffer);
  1055. return fs->curr_inode_no;
  1056. fail:
  1057. free(journal_buffer);
  1058. free(zero_buffer);
  1059. return -1;
  1060. }
  1061. static void alloc_single_indirect_block(struct ext2_inode *file_inode,
  1062. unsigned int *total_remaining_blocks,
  1063. unsigned int *no_blks_reqd)
  1064. {
  1065. short i;
  1066. short status;
  1067. long int actual_block_no;
  1068. long int si_blockno;
  1069. /* si :single indirect */
  1070. __le32 *si_buffer = NULL;
  1071. __le32 *si_start_addr = NULL;
  1072. struct ext_filesystem *fs = get_fs();
  1073. if (*total_remaining_blocks != 0) {
  1074. si_buffer = zalloc(fs->blksz);
  1075. if (!si_buffer) {
  1076. printf("No Memory\n");
  1077. return;
  1078. }
  1079. si_start_addr = si_buffer;
  1080. si_blockno = ext4fs_get_new_blk_no();
  1081. if (si_blockno == -1) {
  1082. printf("no block left to assign\n");
  1083. goto fail;
  1084. }
  1085. (*no_blks_reqd)++;
  1086. debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
  1087. status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
  1088. 0, fs->blksz, (char *)si_buffer);
  1089. memset(si_buffer, '\0', fs->blksz);
  1090. if (status == 0)
  1091. goto fail;
  1092. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  1093. actual_block_no = ext4fs_get_new_blk_no();
  1094. if (actual_block_no == -1) {
  1095. printf("no block left to assign\n");
  1096. goto fail;
  1097. }
  1098. *si_buffer = cpu_to_le32(actual_block_no);
  1099. debug("SIAB %u: %u\n", *si_buffer,
  1100. *total_remaining_blocks);
  1101. si_buffer++;
  1102. (*total_remaining_blocks)--;
  1103. if (*total_remaining_blocks == 0)
  1104. break;
  1105. }
  1106. /* write the block to disk */
  1107. put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
  1108. si_start_addr, fs->blksz);
  1109. file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
  1110. }
  1111. fail:
  1112. free(si_start_addr);
  1113. }
  1114. static void alloc_double_indirect_block(struct ext2_inode *file_inode,
  1115. unsigned int *total_remaining_blocks,
  1116. unsigned int *no_blks_reqd)
  1117. {
  1118. short i;
  1119. short j;
  1120. short status;
  1121. long int actual_block_no;
  1122. /* di:double indirect */
  1123. long int di_blockno_parent;
  1124. long int di_blockno_child;
  1125. __le32 *di_parent_buffer = NULL;
  1126. __le32 *di_child_buff = NULL;
  1127. __le32 *di_block_start_addr = NULL;
  1128. __le32 *di_child_buff_start = NULL;
  1129. struct ext_filesystem *fs = get_fs();
  1130. if (*total_remaining_blocks != 0) {
  1131. /* double indirect parent block connecting to inode */
  1132. di_blockno_parent = ext4fs_get_new_blk_no();
  1133. if (di_blockno_parent == -1) {
  1134. printf("no block left to assign\n");
  1135. goto fail;
  1136. }
  1137. di_parent_buffer = zalloc(fs->blksz);
  1138. if (!di_parent_buffer)
  1139. goto fail;
  1140. di_block_start_addr = di_parent_buffer;
  1141. (*no_blks_reqd)++;
  1142. debug("DIPB %ld: %u\n", di_blockno_parent,
  1143. *total_remaining_blocks);
  1144. status = ext4fs_devread((lbaint_t)di_blockno_parent *
  1145. fs->sect_perblk, 0,
  1146. fs->blksz, (char *)di_parent_buffer);
  1147. if (!status) {
  1148. printf("%s: Device read error!\n", __func__);
  1149. goto fail;
  1150. }
  1151. memset(di_parent_buffer, '\0', fs->blksz);
  1152. /*
  1153. * start:for each double indirect parent
  1154. * block create one more block
  1155. */
  1156. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  1157. di_blockno_child = ext4fs_get_new_blk_no();
  1158. if (di_blockno_child == -1) {
  1159. printf("no block left to assign\n");
  1160. goto fail;
  1161. }
  1162. di_child_buff = zalloc(fs->blksz);
  1163. if (!di_child_buff)
  1164. goto fail;
  1165. di_child_buff_start = di_child_buff;
  1166. *di_parent_buffer = cpu_to_le32(di_blockno_child);
  1167. di_parent_buffer++;
  1168. (*no_blks_reqd)++;
  1169. debug("DICB %ld: %u\n", di_blockno_child,
  1170. *total_remaining_blocks);
  1171. status = ext4fs_devread((lbaint_t)di_blockno_child *
  1172. fs->sect_perblk, 0,
  1173. fs->blksz,
  1174. (char *)di_child_buff);
  1175. if (!status) {
  1176. printf("%s: Device read error!\n", __func__);
  1177. goto fail;
  1178. }
  1179. memset(di_child_buff, '\0', fs->blksz);
  1180. /* filling of actual datablocks for each child */
  1181. for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
  1182. actual_block_no = ext4fs_get_new_blk_no();
  1183. if (actual_block_no == -1) {
  1184. printf("no block left to assign\n");
  1185. goto fail;
  1186. }
  1187. *di_child_buff = cpu_to_le32(actual_block_no);
  1188. debug("DIAB %ld: %u\n", actual_block_no,
  1189. *total_remaining_blocks);
  1190. di_child_buff++;
  1191. (*total_remaining_blocks)--;
  1192. if (*total_remaining_blocks == 0)
  1193. break;
  1194. }
  1195. /* write the block table */
  1196. put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
  1197. di_child_buff_start, fs->blksz);
  1198. free(di_child_buff_start);
  1199. di_child_buff_start = NULL;
  1200. if (*total_remaining_blocks == 0)
  1201. break;
  1202. }
  1203. put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
  1204. di_block_start_addr, fs->blksz);
  1205. file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
  1206. }
  1207. fail:
  1208. free(di_block_start_addr);
  1209. }
  1210. static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
  1211. unsigned int *total_remaining_blocks,
  1212. unsigned int *no_blks_reqd)
  1213. {
  1214. short i;
  1215. short j;
  1216. short k;
  1217. long int actual_block_no;
  1218. /* ti: Triple Indirect */
  1219. long int ti_gp_blockno;
  1220. long int ti_parent_blockno;
  1221. long int ti_child_blockno;
  1222. __le32 *ti_gp_buff = NULL;
  1223. __le32 *ti_parent_buff = NULL;
  1224. __le32 *ti_child_buff = NULL;
  1225. __le32 *ti_gp_buff_start_addr = NULL;
  1226. __le32 *ti_pbuff_start_addr = NULL;
  1227. __le32 *ti_cbuff_start_addr = NULL;
  1228. struct ext_filesystem *fs = get_fs();
  1229. if (*total_remaining_blocks != 0) {
  1230. /* triple indirect grand parent block connecting to inode */
  1231. ti_gp_blockno = ext4fs_get_new_blk_no();
  1232. if (ti_gp_blockno == -1) {
  1233. printf("no block left to assign\n");
  1234. return;
  1235. }
  1236. ti_gp_buff = zalloc(fs->blksz);
  1237. if (!ti_gp_buff)
  1238. return;
  1239. ti_gp_buff_start_addr = ti_gp_buff;
  1240. (*no_blks_reqd)++;
  1241. debug("TIGPB %ld: %u\n", ti_gp_blockno,
  1242. *total_remaining_blocks);
  1243. /* for each 4 byte grand parent entry create one more block */
  1244. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  1245. ti_parent_blockno = ext4fs_get_new_blk_no();
  1246. if (ti_parent_blockno == -1) {
  1247. printf("no block left to assign\n");
  1248. goto fail;
  1249. }
  1250. ti_parent_buff = zalloc(fs->blksz);
  1251. if (!ti_parent_buff)
  1252. goto fail;
  1253. ti_pbuff_start_addr = ti_parent_buff;
  1254. *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
  1255. ti_gp_buff++;
  1256. (*no_blks_reqd)++;
  1257. debug("TIPB %ld: %u\n", ti_parent_blockno,
  1258. *total_remaining_blocks);
  1259. /* for each 4 byte entry parent create one more block */
  1260. for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
  1261. ti_child_blockno = ext4fs_get_new_blk_no();
  1262. if (ti_child_blockno == -1) {
  1263. printf("no block left assign\n");
  1264. goto fail1;
  1265. }
  1266. ti_child_buff = zalloc(fs->blksz);
  1267. if (!ti_child_buff)
  1268. goto fail1;
  1269. ti_cbuff_start_addr = ti_child_buff;
  1270. *ti_parent_buff = cpu_to_le32(ti_child_blockno);
  1271. ti_parent_buff++;
  1272. (*no_blks_reqd)++;
  1273. debug("TICB %ld: %u\n", ti_parent_blockno,
  1274. *total_remaining_blocks);
  1275. /* fill actual datablocks for each child */
  1276. for (k = 0; k < (fs->blksz / sizeof(int));
  1277. k++) {
  1278. actual_block_no =
  1279. ext4fs_get_new_blk_no();
  1280. if (actual_block_no == -1) {
  1281. printf("no block left\n");
  1282. free(ti_cbuff_start_addr);
  1283. goto fail1;
  1284. }
  1285. *ti_child_buff = cpu_to_le32(actual_block_no);
  1286. debug("TIAB %ld: %u\n", actual_block_no,
  1287. *total_remaining_blocks);
  1288. ti_child_buff++;
  1289. (*total_remaining_blocks)--;
  1290. if (*total_remaining_blocks == 0)
  1291. break;
  1292. }
  1293. /* write the child block */
  1294. put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
  1295. (uint64_t)fs->blksz)),
  1296. ti_cbuff_start_addr, fs->blksz);
  1297. free(ti_cbuff_start_addr);
  1298. if (*total_remaining_blocks == 0)
  1299. break;
  1300. }
  1301. /* write the parent block */
  1302. put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
  1303. ti_pbuff_start_addr, fs->blksz);
  1304. free(ti_pbuff_start_addr);
  1305. if (*total_remaining_blocks == 0)
  1306. break;
  1307. }
  1308. /* write the grand parent block */
  1309. put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
  1310. ti_gp_buff_start_addr, fs->blksz);
  1311. file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
  1312. free(ti_gp_buff_start_addr);
  1313. return;
  1314. }
  1315. fail1:
  1316. free(ti_pbuff_start_addr);
  1317. fail:
  1318. free(ti_gp_buff_start_addr);
  1319. }
  1320. void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
  1321. unsigned int total_remaining_blocks,
  1322. unsigned int *total_no_of_block)
  1323. {
  1324. short i;
  1325. long int direct_blockno;
  1326. unsigned int no_blks_reqd = 0;
  1327. /* allocation of direct blocks */
  1328. for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
  1329. direct_blockno = ext4fs_get_new_blk_no();
  1330. if (direct_blockno == -1) {
  1331. printf("no block left to assign\n");
  1332. return;
  1333. }
  1334. file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
  1335. debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
  1336. total_remaining_blocks--;
  1337. }
  1338. alloc_single_indirect_block(file_inode, &total_remaining_blocks,
  1339. &no_blks_reqd);
  1340. alloc_double_indirect_block(file_inode, &total_remaining_blocks,
  1341. &no_blks_reqd);
  1342. alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
  1343. &no_blks_reqd);
  1344. *total_no_of_block += no_blks_reqd;
  1345. }
  1346. #endif
  1347. static struct ext4_extent_header *ext4fs_get_extent_block
  1348. (struct ext2_data *data, struct ext_block_cache *cache,
  1349. struct ext4_extent_header *ext_block,
  1350. uint32_t fileblock, int log2_blksz)
  1351. {
  1352. struct ext4_extent_idx *index;
  1353. unsigned long long block;
  1354. int blksz = EXT2_BLOCK_SIZE(data);
  1355. int i;
  1356. while (1) {
  1357. index = (struct ext4_extent_idx *)(ext_block + 1);
  1358. if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
  1359. return NULL;
  1360. if (ext_block->eh_depth == 0)
  1361. return ext_block;
  1362. i = -1;
  1363. do {
  1364. i++;
  1365. if (i >= le16_to_cpu(ext_block->eh_entries))
  1366. break;
  1367. } while (fileblock >= le32_to_cpu(index[i].ei_block));
  1368. /*
  1369. * If first logical block number is higher than requested fileblock,
  1370. * it is a sparse file. This is handled on upper layer.
  1371. */
  1372. if (i > 0)
  1373. i--;
  1374. block = le16_to_cpu(index[i].ei_leaf_hi);
  1375. block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
  1376. block <<= log2_blksz;
  1377. if (!ext_cache_read(cache, (lbaint_t)block, blksz))
  1378. return NULL;
  1379. ext_block = (struct ext4_extent_header *)cache->buf;
  1380. }
  1381. }
  1382. static int ext4fs_blockgroup
  1383. (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
  1384. {
  1385. long int blkno;
  1386. unsigned int blkoff, desc_per_blk;
  1387. int log2blksz = get_fs()->dev_desc->log2blksz;
  1388. int desc_size = get_fs()->gdsize;
  1389. if (desc_size == 0)
  1390. return 0;
  1391. desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
  1392. if (desc_per_blk == 0)
  1393. return 0;
  1394. blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
  1395. group / desc_per_blk;
  1396. blkoff = (group % desc_per_blk) * desc_size;
  1397. debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
  1398. group, blkno, blkoff);
  1399. return ext4fs_devread((lbaint_t)blkno <<
  1400. (LOG2_BLOCK_SIZE(data) - log2blksz),
  1401. blkoff, desc_size, (char *)blkgrp);
  1402. }
  1403. int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
  1404. {
  1405. struct ext2_block_group *blkgrp;
  1406. struct ext2_sblock *sblock = &data->sblock;
  1407. struct ext_filesystem *fs = get_fs();
  1408. int log2blksz = get_fs()->dev_desc->log2blksz;
  1409. int inodes_per_block, status;
  1410. long int blkno;
  1411. unsigned int blkoff;
  1412. /* Allocate blkgrp based on gdsize (for 64-bit support). */
  1413. blkgrp = zalloc(get_fs()->gdsize);
  1414. if (!blkgrp)
  1415. return 0;
  1416. /* It is easier to calculate if the first inode is 0. */
  1417. ino--;
  1418. if ( le32_to_cpu(sblock->inodes_per_group) == 0 || fs->inodesz == 0) {
  1419. free(blkgrp);
  1420. return 0;
  1421. }
  1422. status = ext4fs_blockgroup(data, ino / le32_to_cpu
  1423. (sblock->inodes_per_group), blkgrp);
  1424. if (status == 0) {
  1425. free(blkgrp);
  1426. return 0;
  1427. }
  1428. inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
  1429. if ( inodes_per_block == 0 ) {
  1430. free(blkgrp);
  1431. return 0;
  1432. }
  1433. blkno = ext4fs_bg_get_inode_table_id(blkgrp, fs) +
  1434. (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
  1435. blkoff = (ino % inodes_per_block) * fs->inodesz;
  1436. /* Free blkgrp as it is no longer required. */
  1437. free(blkgrp);
  1438. /* Read the inode. */
  1439. status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
  1440. log2blksz), blkoff,
  1441. sizeof(struct ext2_inode), (char *)inode);
  1442. if (status == 0)
  1443. return 0;
  1444. return 1;
  1445. }
  1446. long int read_allocated_block(struct ext2_inode *inode, int fileblock,
  1447. struct ext_block_cache *cache)
  1448. {
  1449. long int blknr;
  1450. int blksz;
  1451. int log2_blksz;
  1452. int status;
  1453. long int rblock;
  1454. long int perblock_parent;
  1455. long int perblock_child;
  1456. unsigned long long start;
  1457. /* get the blocksize of the filesystem */
  1458. blksz = EXT2_BLOCK_SIZE(ext4fs_root);
  1459. log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
  1460. - get_fs()->dev_desc->log2blksz;
  1461. if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
  1462. long int startblock, endblock;
  1463. struct ext_block_cache *c, cd;
  1464. struct ext4_extent_header *ext_block;
  1465. struct ext4_extent *extent;
  1466. int i;
  1467. if (cache) {
  1468. c = cache;
  1469. } else {
  1470. c = &cd;
  1471. ext_cache_init(c);
  1472. }
  1473. ext_block =
  1474. ext4fs_get_extent_block(ext4fs_root, c,
  1475. (struct ext4_extent_header *)
  1476. inode->b.blocks.dir_blocks,
  1477. fileblock, log2_blksz);
  1478. if (!ext_block) {
  1479. printf("invalid extent block\n");
  1480. if (!cache)
  1481. ext_cache_fini(c);
  1482. return -EINVAL;
  1483. }
  1484. extent = (struct ext4_extent *)(ext_block + 1);
  1485. for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
  1486. startblock = le32_to_cpu(extent[i].ee_block);
  1487. endblock = startblock + le16_to_cpu(extent[i].ee_len);
  1488. if (startblock > fileblock) {
  1489. /* Sparse file */
  1490. if (!cache)
  1491. ext_cache_fini(c);
  1492. return 0;
  1493. } else if (fileblock < endblock) {
  1494. start = le16_to_cpu(extent[i].ee_start_hi);
  1495. start = (start << 32) +
  1496. le32_to_cpu(extent[i].ee_start_lo);
  1497. if (!cache)
  1498. ext_cache_fini(c);
  1499. return (fileblock - startblock) + start;
  1500. }
  1501. }
  1502. if (!cache)
  1503. ext_cache_fini(c);
  1504. return 0;
  1505. }
  1506. /* Direct blocks. */
  1507. if (fileblock < INDIRECT_BLOCKS)
  1508. blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
  1509. /* Indirect. */
  1510. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
  1511. if (ext4fs_indir1_block == NULL) {
  1512. ext4fs_indir1_block = zalloc(blksz);
  1513. if (ext4fs_indir1_block == NULL) {
  1514. printf("** SI ext2fs read block (indir 1)"
  1515. "malloc failed. **\n");
  1516. return -1;
  1517. }
  1518. ext4fs_indir1_size = blksz;
  1519. ext4fs_indir1_blkno = -1;
  1520. }
  1521. if (blksz != ext4fs_indir1_size) {
  1522. free(ext4fs_indir1_block);
  1523. ext4fs_indir1_block = NULL;
  1524. ext4fs_indir1_size = 0;
  1525. ext4fs_indir1_blkno = -1;
  1526. ext4fs_indir1_block = zalloc(blksz);
  1527. if (ext4fs_indir1_block == NULL) {
  1528. printf("** SI ext2fs read block (indir 1):"
  1529. "malloc failed. **\n");
  1530. return -1;
  1531. }
  1532. ext4fs_indir1_size = blksz;
  1533. }
  1534. if ((le32_to_cpu(inode->b.blocks.indir_block) <<
  1535. log2_blksz) != ext4fs_indir1_blkno) {
  1536. status =
  1537. ext4fs_devread((lbaint_t)le32_to_cpu
  1538. (inode->b.blocks.
  1539. indir_block) << log2_blksz, 0,
  1540. blksz, (char *)ext4fs_indir1_block);
  1541. if (status == 0) {
  1542. printf("** SI ext2fs read block (indir 1)"
  1543. "failed. **\n");
  1544. return -1;
  1545. }
  1546. ext4fs_indir1_blkno =
  1547. le32_to_cpu(inode->b.blocks.
  1548. indir_block) << log2_blksz;
  1549. }
  1550. blknr = le32_to_cpu(ext4fs_indir1_block
  1551. [fileblock - INDIRECT_BLOCKS]);
  1552. }
  1553. /* Double indirect. */
  1554. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
  1555. (blksz / 4 + 1)))) {
  1556. long int perblock = blksz / 4;
  1557. long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
  1558. if (ext4fs_indir1_block == NULL) {
  1559. ext4fs_indir1_block = zalloc(blksz);
  1560. if (ext4fs_indir1_block == NULL) {
  1561. printf("** DI ext2fs read block (indir 2 1)"
  1562. "malloc failed. **\n");
  1563. return -1;
  1564. }
  1565. ext4fs_indir1_size = blksz;
  1566. ext4fs_indir1_blkno = -1;
  1567. }
  1568. if (blksz != ext4fs_indir1_size) {
  1569. free(ext4fs_indir1_block);
  1570. ext4fs_indir1_block = NULL;
  1571. ext4fs_indir1_size = 0;
  1572. ext4fs_indir1_blkno = -1;
  1573. ext4fs_indir1_block = zalloc(blksz);
  1574. if (ext4fs_indir1_block == NULL) {
  1575. printf("** DI ext2fs read block (indir 2 1)"
  1576. "malloc failed. **\n");
  1577. return -1;
  1578. }
  1579. ext4fs_indir1_size = blksz;
  1580. }
  1581. if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
  1582. log2_blksz) != ext4fs_indir1_blkno) {
  1583. status =
  1584. ext4fs_devread((lbaint_t)le32_to_cpu
  1585. (inode->b.blocks.
  1586. double_indir_block) << log2_blksz,
  1587. 0, blksz,
  1588. (char *)ext4fs_indir1_block);
  1589. if (status == 0) {
  1590. printf("** DI ext2fs read block (indir 2 1)"
  1591. "failed. **\n");
  1592. return -1;
  1593. }
  1594. ext4fs_indir1_blkno =
  1595. le32_to_cpu(inode->b.blocks.double_indir_block) <<
  1596. log2_blksz;
  1597. }
  1598. if (ext4fs_indir2_block == NULL) {
  1599. ext4fs_indir2_block = zalloc(blksz);
  1600. if (ext4fs_indir2_block == NULL) {
  1601. printf("** DI ext2fs read block (indir 2 2)"
  1602. "malloc failed. **\n");
  1603. return -1;
  1604. }
  1605. ext4fs_indir2_size = blksz;
  1606. ext4fs_indir2_blkno = -1;
  1607. }
  1608. if (blksz != ext4fs_indir2_size) {
  1609. free(ext4fs_indir2_block);
  1610. ext4fs_indir2_block = NULL;
  1611. ext4fs_indir2_size = 0;
  1612. ext4fs_indir2_blkno = -1;
  1613. ext4fs_indir2_block = zalloc(blksz);
  1614. if (ext4fs_indir2_block == NULL) {
  1615. printf("** DI ext2fs read block (indir 2 2)"
  1616. "malloc failed. **\n");
  1617. return -1;
  1618. }
  1619. ext4fs_indir2_size = blksz;
  1620. }
  1621. if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
  1622. log2_blksz) != ext4fs_indir2_blkno) {
  1623. status = ext4fs_devread((lbaint_t)le32_to_cpu
  1624. (ext4fs_indir1_block
  1625. [rblock /
  1626. perblock]) << log2_blksz, 0,
  1627. blksz,
  1628. (char *)ext4fs_indir2_block);
  1629. if (status == 0) {
  1630. printf("** DI ext2fs read block (indir 2 2)"
  1631. "failed. **\n");
  1632. return -1;
  1633. }
  1634. ext4fs_indir2_blkno =
  1635. le32_to_cpu(ext4fs_indir1_block[rblock
  1636. /
  1637. perblock]) <<
  1638. log2_blksz;
  1639. }
  1640. blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
  1641. }
  1642. /* Tripple indirect. */
  1643. else {
  1644. rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
  1645. (blksz / 4 * blksz / 4));
  1646. perblock_child = blksz / 4;
  1647. perblock_parent = ((blksz / 4) * (blksz / 4));
  1648. if (ext4fs_indir1_block == NULL) {
  1649. ext4fs_indir1_block = zalloc(blksz);
  1650. if (ext4fs_indir1_block == NULL) {
  1651. printf("** TI ext2fs read block (indir 2 1)"
  1652. "malloc failed. **\n");
  1653. return -1;
  1654. }
  1655. ext4fs_indir1_size = blksz;
  1656. ext4fs_indir1_blkno = -1;
  1657. }
  1658. if (blksz != ext4fs_indir1_size) {
  1659. free(ext4fs_indir1_block);
  1660. ext4fs_indir1_block = NULL;
  1661. ext4fs_indir1_size = 0;
  1662. ext4fs_indir1_blkno = -1;
  1663. ext4fs_indir1_block = zalloc(blksz);
  1664. if (ext4fs_indir1_block == NULL) {
  1665. printf("** TI ext2fs read block (indir 2 1)"
  1666. "malloc failed. **\n");
  1667. return -1;
  1668. }
  1669. ext4fs_indir1_size = blksz;
  1670. }
  1671. if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
  1672. log2_blksz) != ext4fs_indir1_blkno) {
  1673. status = ext4fs_devread
  1674. ((lbaint_t)
  1675. le32_to_cpu(inode->b.blocks.triple_indir_block)
  1676. << log2_blksz, 0, blksz,
  1677. (char *)ext4fs_indir1_block);
  1678. if (status == 0) {
  1679. printf("** TI ext2fs read block (indir 2 1)"
  1680. "failed. **\n");
  1681. return -1;
  1682. }
  1683. ext4fs_indir1_blkno =
  1684. le32_to_cpu(inode->b.blocks.triple_indir_block) <<
  1685. log2_blksz;
  1686. }
  1687. if (ext4fs_indir2_block == NULL) {
  1688. ext4fs_indir2_block = zalloc(blksz);
  1689. if (ext4fs_indir2_block == NULL) {
  1690. printf("** TI ext2fs read block (indir 2 2)"
  1691. "malloc failed. **\n");
  1692. return -1;
  1693. }
  1694. ext4fs_indir2_size = blksz;
  1695. ext4fs_indir2_blkno = -1;
  1696. }
  1697. if (blksz != ext4fs_indir2_size) {
  1698. free(ext4fs_indir2_block);
  1699. ext4fs_indir2_block = NULL;
  1700. ext4fs_indir2_size = 0;
  1701. ext4fs_indir2_blkno = -1;
  1702. ext4fs_indir2_block = zalloc(blksz);
  1703. if (ext4fs_indir2_block == NULL) {
  1704. printf("** TI ext2fs read block (indir 2 2)"
  1705. "malloc failed. **\n");
  1706. return -1;
  1707. }
  1708. ext4fs_indir2_size = blksz;
  1709. }
  1710. if ((le32_to_cpu(ext4fs_indir1_block[rblock /
  1711. perblock_parent]) <<
  1712. log2_blksz)
  1713. != ext4fs_indir2_blkno) {
  1714. status = ext4fs_devread((lbaint_t)le32_to_cpu
  1715. (ext4fs_indir1_block
  1716. [rblock /
  1717. perblock_parent]) <<
  1718. log2_blksz, 0, blksz,
  1719. (char *)ext4fs_indir2_block);
  1720. if (status == 0) {
  1721. printf("** TI ext2fs read block (indir 2 2)"
  1722. "failed. **\n");
  1723. return -1;
  1724. }
  1725. ext4fs_indir2_blkno =
  1726. le32_to_cpu(ext4fs_indir1_block[rblock /
  1727. perblock_parent])
  1728. << log2_blksz;
  1729. }
  1730. if (ext4fs_indir3_block == NULL) {
  1731. ext4fs_indir3_block = zalloc(blksz);
  1732. if (ext4fs_indir3_block == NULL) {
  1733. printf("** TI ext2fs read block (indir 2 2)"
  1734. "malloc failed. **\n");
  1735. return -1;
  1736. }
  1737. ext4fs_indir3_size = blksz;
  1738. ext4fs_indir3_blkno = -1;
  1739. }
  1740. if (blksz != ext4fs_indir3_size) {
  1741. free(ext4fs_indir3_block);
  1742. ext4fs_indir3_block = NULL;
  1743. ext4fs_indir3_size = 0;
  1744. ext4fs_indir3_blkno = -1;
  1745. ext4fs_indir3_block = zalloc(blksz);
  1746. if (ext4fs_indir3_block == NULL) {
  1747. printf("** TI ext2fs read block (indir 2 2)"
  1748. "malloc failed. **\n");
  1749. return -1;
  1750. }
  1751. ext4fs_indir3_size = blksz;
  1752. }
  1753. if ((le32_to_cpu(ext4fs_indir2_block[rblock
  1754. /
  1755. perblock_child]) <<
  1756. log2_blksz) != ext4fs_indir3_blkno) {
  1757. status =
  1758. ext4fs_devread((lbaint_t)le32_to_cpu
  1759. (ext4fs_indir2_block
  1760. [(rblock / perblock_child)
  1761. % (blksz / 4)]) << log2_blksz, 0,
  1762. blksz, (char *)ext4fs_indir3_block);
  1763. if (status == 0) {
  1764. printf("** TI ext2fs read block (indir 2 2)"
  1765. "failed. **\n");
  1766. return -1;
  1767. }
  1768. ext4fs_indir3_blkno =
  1769. le32_to_cpu(ext4fs_indir2_block[(rblock /
  1770. perblock_child) %
  1771. (blksz /
  1772. 4)]) <<
  1773. log2_blksz;
  1774. }
  1775. blknr = le32_to_cpu(ext4fs_indir3_block
  1776. [rblock % perblock_child]);
  1777. }
  1778. debug("read_allocated_block %ld\n", blknr);
  1779. return blknr;
  1780. }
  1781. /**
  1782. * ext4fs_reinit_global() - Reinitialize values of ext4 write implementation's
  1783. * global pointers
  1784. *
  1785. * This function assures that for a file with the same name but different size
  1786. * the sequential store on the ext4 filesystem will be correct.
  1787. *
  1788. * In this function the global data, responsible for internal representation
  1789. * of the ext4 data are initialized to the reset state. Without this, during
  1790. * replacement of the smaller file with the bigger truncation of new file was
  1791. * performed.
  1792. */
  1793. void ext4fs_reinit_global(void)
  1794. {
  1795. if (ext4fs_indir1_block != NULL) {
  1796. free(ext4fs_indir1_block);
  1797. ext4fs_indir1_block = NULL;
  1798. ext4fs_indir1_size = 0;
  1799. ext4fs_indir1_blkno = -1;
  1800. }
  1801. if (ext4fs_indir2_block != NULL) {
  1802. free(ext4fs_indir2_block);
  1803. ext4fs_indir2_block = NULL;
  1804. ext4fs_indir2_size = 0;
  1805. ext4fs_indir2_blkno = -1;
  1806. }
  1807. if (ext4fs_indir3_block != NULL) {
  1808. free(ext4fs_indir3_block);
  1809. ext4fs_indir3_block = NULL;
  1810. ext4fs_indir3_size = 0;
  1811. ext4fs_indir3_blkno = -1;
  1812. }
  1813. }
  1814. void ext4fs_close(void)
  1815. {
  1816. if ((ext4fs_file != NULL) && (ext4fs_root != NULL)) {
  1817. ext4fs_free_node(ext4fs_file, &ext4fs_root->diropen);
  1818. ext4fs_file = NULL;
  1819. }
  1820. if (ext4fs_root != NULL) {
  1821. free(ext4fs_root);
  1822. ext4fs_root = NULL;
  1823. }
  1824. ext4fs_reinit_global();
  1825. }
  1826. int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
  1827. struct ext2fs_node **fnode, int *ftype)
  1828. {
  1829. unsigned int fpos = 0;
  1830. int status;
  1831. loff_t actread;
  1832. struct ext2fs_node *diro = (struct ext2fs_node *) dir;
  1833. #ifdef DEBUG
  1834. if (name != NULL)
  1835. printf("Iterate dir %s\n", name);
  1836. #endif /* of DEBUG */
  1837. if (!diro->inode_read) {
  1838. status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
  1839. if (status == 0)
  1840. return 0;
  1841. }
  1842. /* Search the file. */
  1843. while (fpos < le32_to_cpu(diro->inode.size)) {
  1844. struct ext2_dirent dirent;
  1845. status = ext4fs_read_file(diro, fpos,
  1846. sizeof(struct ext2_dirent),
  1847. (char *)&dirent, &actread);
  1848. if (status < 0)
  1849. return 0;
  1850. if (dirent.direntlen == 0) {
  1851. printf("Failed to iterate over directory %s\n", name);
  1852. return 0;
  1853. }
  1854. if (dirent.namelen != 0) {
  1855. char filename[dirent.namelen + 1];
  1856. struct ext2fs_node *fdiro;
  1857. int type = FILETYPE_UNKNOWN;
  1858. status = ext4fs_read_file(diro,
  1859. fpos +
  1860. sizeof(struct ext2_dirent),
  1861. dirent.namelen, filename,
  1862. &actread);
  1863. if (status < 0)
  1864. return 0;
  1865. fdiro = zalloc(sizeof(struct ext2fs_node));
  1866. if (!fdiro)
  1867. return 0;
  1868. fdiro->data = diro->data;
  1869. fdiro->ino = le32_to_cpu(dirent.inode);
  1870. filename[dirent.namelen] = '\0';
  1871. if (dirent.filetype != FILETYPE_UNKNOWN) {
  1872. fdiro->inode_read = 0;
  1873. if (dirent.filetype == FILETYPE_DIRECTORY)
  1874. type = FILETYPE_DIRECTORY;
  1875. else if (dirent.filetype == FILETYPE_SYMLINK)
  1876. type = FILETYPE_SYMLINK;
  1877. else if (dirent.filetype == FILETYPE_REG)
  1878. type = FILETYPE_REG;
  1879. } else {
  1880. status = ext4fs_read_inode(diro->data,
  1881. le32_to_cpu
  1882. (dirent.inode),
  1883. &fdiro->inode);
  1884. if (status == 0) {
  1885. free(fdiro);
  1886. return 0;
  1887. }
  1888. fdiro->inode_read = 1;
  1889. if ((le16_to_cpu(fdiro->inode.mode) &
  1890. FILETYPE_INO_MASK) ==
  1891. FILETYPE_INO_DIRECTORY) {
  1892. type = FILETYPE_DIRECTORY;
  1893. } else if ((le16_to_cpu(fdiro->inode.mode)
  1894. & FILETYPE_INO_MASK) ==
  1895. FILETYPE_INO_SYMLINK) {
  1896. type = FILETYPE_SYMLINK;
  1897. } else if ((le16_to_cpu(fdiro->inode.mode)
  1898. & FILETYPE_INO_MASK) ==
  1899. FILETYPE_INO_REG) {
  1900. type = FILETYPE_REG;
  1901. }
  1902. }
  1903. #ifdef DEBUG
  1904. printf("iterate >%s<\n", filename);
  1905. #endif /* of DEBUG */
  1906. if ((name != NULL) && (fnode != NULL)
  1907. && (ftype != NULL)) {
  1908. if (strcmp(filename, name) == 0) {
  1909. *ftype = type;
  1910. *fnode = fdiro;
  1911. return 1;
  1912. }
  1913. } else {
  1914. if (fdiro->inode_read == 0) {
  1915. status = ext4fs_read_inode(diro->data,
  1916. le32_to_cpu(
  1917. dirent.inode),
  1918. &fdiro->inode);
  1919. if (status == 0) {
  1920. free(fdiro);
  1921. return 0;
  1922. }
  1923. fdiro->inode_read = 1;
  1924. }
  1925. switch (type) {
  1926. case FILETYPE_DIRECTORY:
  1927. printf("<DIR> ");
  1928. break;
  1929. case FILETYPE_SYMLINK:
  1930. printf("<SYM> ");
  1931. break;
  1932. case FILETYPE_REG:
  1933. printf(" ");
  1934. break;
  1935. default:
  1936. printf("< ? > ");
  1937. break;
  1938. }
  1939. printf("%10u %s\n",
  1940. le32_to_cpu(fdiro->inode.size),
  1941. filename);
  1942. }
  1943. free(fdiro);
  1944. }
  1945. fpos += le16_to_cpu(dirent.direntlen);
  1946. }
  1947. return 0;
  1948. }
  1949. static char *ext4fs_read_symlink(struct ext2fs_node *node)
  1950. {
  1951. char *symlink;
  1952. struct ext2fs_node *diro = node;
  1953. int status;
  1954. loff_t actread;
  1955. if (!diro->inode_read) {
  1956. status = ext4fs_read_inode(diro->data, diro->ino, &diro->inode);
  1957. if (status == 0)
  1958. return NULL;
  1959. }
  1960. symlink = zalloc(le32_to_cpu(diro->inode.size) + 1);
  1961. if (!symlink)
  1962. return NULL;
  1963. if (le32_to_cpu(diro->inode.size) < sizeof(diro->inode.b.symlink)) {
  1964. strncpy(symlink, diro->inode.b.symlink,
  1965. le32_to_cpu(diro->inode.size));
  1966. } else {
  1967. status = ext4fs_read_file(diro, 0,
  1968. le32_to_cpu(diro->inode.size),
  1969. symlink, &actread);
  1970. if ((status < 0) || (actread == 0)) {
  1971. free(symlink);
  1972. return NULL;
  1973. }
  1974. }
  1975. symlink[le32_to_cpu(diro->inode.size)] = '\0';
  1976. return symlink;
  1977. }
  1978. static int ext4fs_find_file1(const char *currpath,
  1979. struct ext2fs_node *currroot,
  1980. struct ext2fs_node **currfound, int *foundtype)
  1981. {
  1982. char fpath[strlen(currpath) + 1];
  1983. char *name = fpath;
  1984. char *next;
  1985. int status;
  1986. int type = FILETYPE_DIRECTORY;
  1987. struct ext2fs_node *currnode = currroot;
  1988. struct ext2fs_node *oldnode = currroot;
  1989. strncpy(fpath, currpath, strlen(currpath) + 1);
  1990. /* Remove all leading slashes. */
  1991. while (*name == '/')
  1992. name++;
  1993. if (!*name) {
  1994. *currfound = currnode;
  1995. return 1;
  1996. }
  1997. for (;;) {
  1998. int found;
  1999. /* Extract the actual part from the pathname. */
  2000. next = strchr(name, '/');
  2001. if (next) {
  2002. /* Remove all leading slashes. */
  2003. while (*next == '/')
  2004. *(next++) = '\0';
  2005. }
  2006. if (type != FILETYPE_DIRECTORY) {
  2007. ext4fs_free_node(currnode, currroot);
  2008. return 0;
  2009. }
  2010. oldnode = currnode;
  2011. /* Iterate over the directory. */
  2012. found = ext4fs_iterate_dir(currnode, name, &currnode, &type);
  2013. if (found == 0)
  2014. return 0;
  2015. if (found == -1)
  2016. break;
  2017. /* Read in the symlink and follow it. */
  2018. if (type == FILETYPE_SYMLINK) {
  2019. char *symlink;
  2020. /* Test if the symlink does not loop. */
  2021. if (++symlinknest == 8) {
  2022. ext4fs_free_node(currnode, currroot);
  2023. ext4fs_free_node(oldnode, currroot);
  2024. return 0;
  2025. }
  2026. symlink = ext4fs_read_symlink(currnode);
  2027. ext4fs_free_node(currnode, currroot);
  2028. if (!symlink) {
  2029. ext4fs_free_node(oldnode, currroot);
  2030. return 0;
  2031. }
  2032. debug("Got symlink >%s<\n", symlink);
  2033. if (symlink[0] == '/') {
  2034. ext4fs_free_node(oldnode, currroot);
  2035. oldnode = &ext4fs_root->diropen;
  2036. }
  2037. /* Lookup the node the symlink points to. */
  2038. status = ext4fs_find_file1(symlink, oldnode,
  2039. &currnode, &type);
  2040. free(symlink);
  2041. if (status == 0) {
  2042. ext4fs_free_node(oldnode, currroot);
  2043. return 0;
  2044. }
  2045. }
  2046. ext4fs_free_node(oldnode, currroot);
  2047. /* Found the node! */
  2048. if (!next || *next == '\0') {
  2049. *currfound = currnode;
  2050. *foundtype = type;
  2051. return 1;
  2052. }
  2053. name = next;
  2054. }
  2055. return -1;
  2056. }
  2057. int ext4fs_find_file(const char *path, struct ext2fs_node *rootnode,
  2058. struct ext2fs_node **foundnode, int expecttype)
  2059. {
  2060. int status;
  2061. int foundtype = FILETYPE_DIRECTORY;
  2062. symlinknest = 0;
  2063. if (!path)
  2064. return 0;
  2065. status = ext4fs_find_file1(path, rootnode, foundnode, &foundtype);
  2066. if (status == 0)
  2067. return 0;
  2068. /* Check if the node that was found was of the expected type. */
  2069. if ((expecttype == FILETYPE_REG) && (foundtype != expecttype))
  2070. return 0;
  2071. else if ((expecttype == FILETYPE_DIRECTORY)
  2072. && (foundtype != expecttype))
  2073. return 0;
  2074. return 1;
  2075. }
  2076. int ext4fs_open(const char *filename, loff_t *len)
  2077. {
  2078. struct ext2fs_node *fdiro = NULL;
  2079. int status;
  2080. if (ext4fs_root == NULL)
  2081. return -1;
  2082. ext4fs_file = NULL;
  2083. status = ext4fs_find_file(filename, &ext4fs_root->diropen, &fdiro,
  2084. FILETYPE_REG);
  2085. if (status == 0)
  2086. goto fail;
  2087. if (!fdiro->inode_read) {
  2088. status = ext4fs_read_inode(fdiro->data, fdiro->ino,
  2089. &fdiro->inode);
  2090. if (status == 0)
  2091. goto fail;
  2092. }
  2093. *len = le32_to_cpu(fdiro->inode.size);
  2094. ext4fs_file = fdiro;
  2095. return 0;
  2096. fail:
  2097. ext4fs_free_node(fdiro, &ext4fs_root->diropen);
  2098. return -1;
  2099. }
  2100. int ext4fs_mount(unsigned part_length)
  2101. {
  2102. struct ext2_data *data;
  2103. int status;
  2104. struct ext_filesystem *fs = get_fs();
  2105. data = zalloc(SUPERBLOCK_SIZE);
  2106. if (!data)
  2107. return 0;
  2108. /* Read the superblock. */
  2109. status = ext4_read_superblock((char *)&data->sblock);
  2110. if (status == 0)
  2111. goto fail;
  2112. /* Make sure this is an ext2 filesystem. */
  2113. if (le16_to_cpu(data->sblock.magic) != EXT2_MAGIC)
  2114. goto fail_noerr;
  2115. if (le32_to_cpu(data->sblock.revision_level) == 0) {
  2116. fs->inodesz = 128;
  2117. fs->gdsize = 32;
  2118. } else {
  2119. debug("EXT4 features COMPAT: %08x INCOMPAT: %08x RO_COMPAT: %08x\n",
  2120. __le32_to_cpu(data->sblock.feature_compatibility),
  2121. __le32_to_cpu(data->sblock.feature_incompat),
  2122. __le32_to_cpu(data->sblock.feature_ro_compat));
  2123. fs->inodesz = le16_to_cpu(data->sblock.inode_size);
  2124. fs->gdsize = le32_to_cpu(data->sblock.feature_incompat) &
  2125. EXT4_FEATURE_INCOMPAT_64BIT ?
  2126. le16_to_cpu(data->sblock.descriptor_size) : 32;
  2127. }
  2128. debug("EXT2 rev %d, inode_size %d, descriptor size %d\n",
  2129. le32_to_cpu(data->sblock.revision_level),
  2130. fs->inodesz, fs->gdsize);
  2131. data->diropen.data = data;
  2132. data->diropen.ino = 2;
  2133. data->diropen.inode_read = 1;
  2134. data->inode = &data->diropen.inode;
  2135. status = ext4fs_read_inode(data, 2, data->inode);
  2136. if (status == 0)
  2137. goto fail;
  2138. ext4fs_root = data;
  2139. return 1;
  2140. fail:
  2141. printf("Failed to mount ext2 filesystem...\n");
  2142. fail_noerr:
  2143. free(data);
  2144. ext4fs_root = NULL;
  2145. return 0;
  2146. }