ext4_common.c 55 KB

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