ext4_common.c 59 KB

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