ext4_common.c 59 KB

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