ext4_common.c 57 KB

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