ext4_common.c 55 KB

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