ext4_common.c 59 KB

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