ext4_common.c 55 KB

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