ext4_common.c 55 KB

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