ext4_common.c 55 KB

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