ext4_common.c 60 KB

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