ext4_common.c 55 KB

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