ext4_common.c 55 KB

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