ext4_common.c 57 KB

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