ext4_common.c 54 KB

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