ext4_common.c 55 KB

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