ext4_common.c 59 KB

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