PageRenderTime 59ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/ext4/ext4_common.c

https://bitbucket.org/hldspb/uboot-sbc8600
C | 2376 lines | 1988 code | 273 blank | 115 comment | 502 complexity | caa7faabcd1320939a1c08bc41a9d59e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * (C) Copyright 2011 - 2012 Samsung Electronics
  3. * EXT4 filesystem implementation in Uboot by
  4. * Uma Shankar <uma.shankar@samsung.com>
  5. * Manjunatha C Achar <a.manjunatha@samsung.com>
  6. *
  7. * ext4ls and ext4load : Based on ext2 ls load support in Uboot.
  8. *
  9. * (C) Copyright 2004
  10. * esd gmbh <www.esd-electronics.com>
  11. * Reinhard Arlt <reinhard.arlt@esd-electronics.com>
  12. *
  13. * based on code from grub2 fs/ext2.c and fs/fshelp.c by
  14. * GRUB -- GRand Unified Bootloader
  15. * Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  16. *
  17. * ext4write : Based on generic ext4 protocol.
  18. *
  19. * SPDX-License-Identifier: GPL-2.0+
  20. */
  21. #include <common.h>
  22. #include <ext_common.h>
  23. #include <ext4fs.h>
  24. #include <inttypes.h>
  25. #include <malloc.h>
  26. #include <memalign.h>
  27. #include <stddef.h>
  28. #include <linux/stat.h>
  29. #include <linux/time.h>
  30. #include <asm/byteorder.h>
  31. #include "ext4_common.h"
  32. struct ext2_data *ext4fs_root;
  33. struct ext2fs_node *ext4fs_file;
  34. __le32 *ext4fs_indir1_block;
  35. int ext4fs_indir1_size;
  36. int ext4fs_indir1_blkno = -1;
  37. __le32 *ext4fs_indir2_block;
  38. int ext4fs_indir2_size;
  39. int ext4fs_indir2_blkno = -1;
  40. __le32 *ext4fs_indir3_block;
  41. int ext4fs_indir3_size;
  42. int ext4fs_indir3_blkno = -1;
  43. struct ext2_inode *g_parent_inode;
  44. static int symlinknest;
  45. #if defined(CONFIG_EXT4_WRITE)
  46. struct ext2_block_group *ext4fs_get_group_descriptor
  47. (const struct ext_filesystem *fs, uint32_t bg_idx)
  48. {
  49. return (struct ext2_block_group *)(fs->gdtable + (bg_idx * fs->gdsize));
  50. }
  51. static inline void ext4fs_sb_free_inodes_dec(struct ext2_sblock *sb)
  52. {
  53. sb->free_inodes = cpu_to_le32(le32_to_cpu(sb->free_inodes) - 1);
  54. }
  55. static inline void ext4fs_sb_free_blocks_dec(struct ext2_sblock *sb)
  56. {
  57. uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
  58. free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
  59. free_blocks--;
  60. sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
  61. sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
  62. }
  63. static inline void ext4fs_bg_free_inodes_dec
  64. (struct ext2_block_group *bg, const struct ext_filesystem *fs)
  65. {
  66. uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
  67. if (fs->gdsize == 64)
  68. free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
  69. free_inodes--;
  70. bg->free_inodes = cpu_to_le16(free_inodes & 0xffff);
  71. if (fs->gdsize == 64)
  72. bg->free_inodes_high = cpu_to_le16(free_inodes >> 16);
  73. }
  74. static inline void ext4fs_bg_free_blocks_dec
  75. (struct ext2_block_group *bg, const struct ext_filesystem *fs)
  76. {
  77. uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
  78. if (fs->gdsize == 64)
  79. free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
  80. free_blocks--;
  81. bg->free_blocks = cpu_to_le16(free_blocks & 0xffff);
  82. if (fs->gdsize == 64)
  83. bg->free_blocks_high = cpu_to_le16(free_blocks >> 16);
  84. }
  85. static inline void ext4fs_bg_itable_unused_dec
  86. (struct ext2_block_group *bg, const struct ext_filesystem *fs)
  87. {
  88. uint32_t free_inodes = le16_to_cpu(bg->bg_itable_unused);
  89. if (fs->gdsize == 64)
  90. free_inodes += le16_to_cpu(bg->bg_itable_unused_high) << 16;
  91. free_inodes--;
  92. bg->bg_itable_unused = cpu_to_le16(free_inodes & 0xffff);
  93. if (fs->gdsize == 64)
  94. bg->bg_itable_unused_high = cpu_to_le16(free_inodes >> 16);
  95. }
  96. uint64_t ext4fs_sb_get_free_blocks(const struct ext2_sblock *sb)
  97. {
  98. uint64_t free_blocks = le32_to_cpu(sb->free_blocks);
  99. free_blocks += (uint64_t)le32_to_cpu(sb->free_blocks_high) << 32;
  100. return free_blocks;
  101. }
  102. void ext4fs_sb_set_free_blocks(struct ext2_sblock *sb, uint64_t free_blocks)
  103. {
  104. sb->free_blocks = cpu_to_le32(free_blocks & 0xffffffff);
  105. sb->free_blocks_high = cpu_to_le16(free_blocks >> 32);
  106. }
  107. uint32_t ext4fs_bg_get_free_blocks(const struct ext2_block_group *bg,
  108. const struct ext_filesystem *fs)
  109. {
  110. uint32_t free_blocks = le16_to_cpu(bg->free_blocks);
  111. if (fs->gdsize == 64)
  112. free_blocks += le16_to_cpu(bg->free_blocks_high) << 16;
  113. return free_blocks;
  114. }
  115. static inline
  116. uint32_t ext4fs_bg_get_free_inodes(const struct ext2_block_group *bg,
  117. const struct ext_filesystem *fs)
  118. {
  119. uint32_t free_inodes = le16_to_cpu(bg->free_inodes);
  120. if (fs->gdsize == 64)
  121. free_inodes += le16_to_cpu(bg->free_inodes_high) << 16;
  122. return free_inodes;
  123. }
  124. static inline uint16_t ext4fs_bg_get_flags(const struct ext2_block_group *bg)
  125. {
  126. return le16_to_cpu(bg->bg_flags);
  127. }
  128. static inline void ext4fs_bg_set_flags(struct ext2_block_group *bg,
  129. uint16_t flags)
  130. {
  131. bg->bg_flags = cpu_to_le16(flags);
  132. }
  133. /* Block number of the block bitmap */
  134. uint64_t ext4fs_bg_get_block_id(const struct ext2_block_group *bg,
  135. const struct ext_filesystem *fs)
  136. {
  137. uint64_t block_nr = le32_to_cpu(bg->block_id);
  138. if (fs->gdsize == 64)
  139. block_nr += (uint64_t)le32_to_cpu(bg->block_id_high) << 32;
  140. return block_nr;
  141. }
  142. /* Block number of the inode bitmap */
  143. uint64_t ext4fs_bg_get_inode_id(const struct ext2_block_group *bg,
  144. const struct ext_filesystem *fs)
  145. {
  146. uint64_t block_nr = le32_to_cpu(bg->inode_id);
  147. if (fs->gdsize == 64)
  148. block_nr += (uint64_t)le32_to_cpu(bg->inode_id_high) << 32;
  149. return block_nr;
  150. }
  151. #endif
  152. /* Block number of the inode table */
  153. uint64_t ext4fs_bg_get_inode_table_id(const struct ext2_block_group *bg,
  154. const struct ext_filesystem *fs)
  155. {
  156. uint64_t block_nr = le32_to_cpu(bg->inode_table_id);
  157. if (fs->gdsize == 64)
  158. block_nr +=
  159. (uint64_t)le32_to_cpu(bg->inode_table_id_high) << 32;
  160. return block_nr;
  161. }
  162. #if defined(CONFIG_EXT4_WRITE)
  163. uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
  164. {
  165. uint32_t res = size / n;
  166. if (res * n != size)
  167. res++;
  168. return res;
  169. }
  170. void put_ext4(uint64_t off, void *buf, uint32_t size)
  171. {
  172. uint64_t startblock;
  173. uint64_t remainder;
  174. unsigned char *temp_ptr = NULL;
  175. struct ext_filesystem *fs = get_fs();
  176. int log2blksz = fs->dev_desc->log2blksz;
  177. ALLOC_CACHE_ALIGN_BUFFER(unsigned char, sec_buf, fs->dev_desc->blksz);
  178. startblock = off >> log2blksz;
  179. startblock += part_offset;
  180. remainder = off & (uint64_t)(fs->dev_desc->blksz - 1);
  181. if (fs->dev_desc == NULL)
  182. return;
  183. if ((startblock + (size >> log2blksz)) >
  184. (part_offset + fs->total_sect)) {
  185. printf("part_offset is " LBAFU "\n", part_offset);
  186. printf("total_sector is %" PRIu64 "\n", fs->total_sect);
  187. printf("error: overflow occurs\n");
  188. return;
  189. }
  190. if (remainder) {
  191. blk_dread(fs->dev_desc, startblock, 1, sec_buf);
  192. temp_ptr = sec_buf;
  193. memcpy((temp_ptr + remainder), (unsigned char *)buf, size);
  194. blk_dwrite(fs->dev_desc, startblock, 1, sec_buf);
  195. } else {
  196. if (size >> log2blksz != 0) {
  197. blk_dwrite(fs->dev_desc, startblock, size >> log2blksz,
  198. (unsigned long *)buf);
  199. } else {
  200. blk_dread(fs->dev_desc, startblock, 1, sec_buf);
  201. temp_ptr = sec_buf;
  202. memcpy(temp_ptr, buf, size);
  203. blk_dwrite(fs->dev_desc, startblock, 1,
  204. (unsigned long *)sec_buf);
  205. }
  206. }
  207. }
  208. static int _get_new_inode_no(unsigned char *buffer)
  209. {
  210. struct ext_filesystem *fs = get_fs();
  211. unsigned char input;
  212. int operand, status;
  213. int count = 1;
  214. int j = 0;
  215. /* get the blocksize of the filesystem */
  216. unsigned char *ptr = buffer;
  217. while (*ptr == 255) {
  218. ptr++;
  219. count += 8;
  220. if (count > le32_to_cpu(ext4fs_root->sblock.inodes_per_group))
  221. return -1;
  222. }
  223. for (j = 0; j < fs->blksz; j++) {
  224. input = *ptr;
  225. int i = 0;
  226. while (i <= 7) {
  227. operand = 1 << i;
  228. status = input & operand;
  229. if (status) {
  230. i++;
  231. count++;
  232. } else {
  233. *ptr |= operand;
  234. return count;
  235. }
  236. }
  237. ptr = ptr + 1;
  238. }
  239. return -1;
  240. }
  241. static int _get_new_blk_no(unsigned char *buffer)
  242. {
  243. int operand;
  244. int count = 0;
  245. int i;
  246. unsigned char *ptr = buffer;
  247. struct ext_filesystem *fs = get_fs();
  248. while (*ptr == 255) {
  249. ptr++;
  250. count += 8;
  251. if (count == (fs->blksz * 8))
  252. return -1;
  253. }
  254. if (fs->blksz == 1024)
  255. count += 1;
  256. for (i = 0; i <= 7; i++) {
  257. operand = 1 << i;
  258. if (*ptr & operand) {
  259. count++;
  260. } else {
  261. *ptr |= operand;
  262. return count;
  263. }
  264. }
  265. return -1;
  266. }
  267. int ext4fs_set_block_bmap(long int blockno, unsigned char *buffer, int index)
  268. {
  269. int i, remainder, status;
  270. unsigned char *ptr = buffer;
  271. unsigned char operand;
  272. i = blockno / 8;
  273. remainder = blockno % 8;
  274. int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
  275. i = i - (index * blocksize);
  276. if (blocksize != 1024) {
  277. ptr = ptr + i;
  278. operand = 1 << remainder;
  279. status = *ptr & operand;
  280. if (status)
  281. return -1;
  282. *ptr = *ptr | operand;
  283. return 0;
  284. } else {
  285. if (remainder == 0) {
  286. ptr = ptr + i - 1;
  287. operand = (1 << 7);
  288. } else {
  289. ptr = ptr + i;
  290. operand = (1 << (remainder - 1));
  291. }
  292. status = *ptr & operand;
  293. if (status)
  294. return -1;
  295. *ptr = *ptr | operand;
  296. return 0;
  297. }
  298. }
  299. void ext4fs_reset_block_bmap(long int blockno, unsigned char *buffer, int index)
  300. {
  301. int i, remainder, status;
  302. unsigned char *ptr = buffer;
  303. unsigned char operand;
  304. i = blockno / 8;
  305. remainder = blockno % 8;
  306. int blocksize = EXT2_BLOCK_SIZE(ext4fs_root);
  307. i = i - (index * blocksize);
  308. if (blocksize != 1024) {
  309. ptr = ptr + i;
  310. operand = (1 << remainder);
  311. status = *ptr & operand;
  312. if (status)
  313. *ptr = *ptr & ~(operand);
  314. } else {
  315. if (remainder == 0) {
  316. ptr = ptr + i - 1;
  317. operand = (1 << 7);
  318. } else {
  319. ptr = ptr + i;
  320. operand = (1 << (remainder - 1));
  321. }
  322. status = *ptr & operand;
  323. if (status)
  324. *ptr = *ptr & ~(operand);
  325. }
  326. }
  327. int ext4fs_set_inode_bmap(int inode_no, unsigned char *buffer, int index)
  328. {
  329. int i, remainder, status;
  330. unsigned char *ptr = buffer;
  331. unsigned char operand;
  332. inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
  333. i = inode_no / 8;
  334. remainder = inode_no % 8;
  335. if (remainder == 0) {
  336. ptr = ptr + i - 1;
  337. operand = (1 << 7);
  338. } else {
  339. ptr = ptr + i;
  340. operand = (1 << (remainder - 1));
  341. }
  342. status = *ptr & operand;
  343. if (status)
  344. return -1;
  345. *ptr = *ptr | operand;
  346. return 0;
  347. }
  348. void ext4fs_reset_inode_bmap(int inode_no, unsigned char *buffer, int index)
  349. {
  350. int i, remainder, status;
  351. unsigned char *ptr = buffer;
  352. unsigned char operand;
  353. inode_no -= (index * le32_to_cpu(ext4fs_root->sblock.inodes_per_group));
  354. i = inode_no / 8;
  355. remainder = inode_no % 8;
  356. if (remainder == 0) {
  357. ptr = ptr + i - 1;
  358. operand = (1 << 7);
  359. } else {
  360. ptr = ptr + i;
  361. operand = (1 << (remainder - 1));
  362. }
  363. status = *ptr & operand;
  364. if (status)
  365. *ptr = *ptr & ~(operand);
  366. }
  367. uint16_t ext4fs_checksum_update(uint32_t i)
  368. {
  369. struct ext2_block_group *desc;
  370. struct ext_filesystem *fs = get_fs();
  371. uint16_t crc = 0;
  372. __le32 le32_i = cpu_to_le32(i);
  373. desc = ext4fs_get_group_descriptor(fs, i);
  374. if (le32_to_cpu(fs->sb->feature_ro_compat) & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
  375. int offset = offsetof(struct ext2_block_group, bg_checksum);
  376. crc = ext2fs_crc16(~0, fs->sb->unique_id,
  377. sizeof(fs->sb->unique_id));
  378. crc = ext2fs_crc16(crc, &le32_i, sizeof(le32_i));
  379. crc = ext2fs_crc16(crc, desc, offset);
  380. offset += sizeof(desc->bg_checksum); /* skip checksum */
  381. assert(offset == sizeof(*desc));
  382. if (offset < fs->gdsize) {
  383. crc = ext2fs_crc16(crc, (__u8 *)desc + offset,
  384. fs->gdsize - offset);
  385. }
  386. }
  387. return crc;
  388. }
  389. static int check_void_in_dentry(struct ext2_dirent *dir, char *filename)
  390. {
  391. int dentry_length;
  392. int sizeof_void_space;
  393. int new_entry_byte_reqd;
  394. short padding_factor = 0;
  395. if (dir->namelen % 4 != 0)
  396. padding_factor = 4 - (dir->namelen % 4);
  397. dentry_length = sizeof(struct ext2_dirent) +
  398. dir->namelen + padding_factor;
  399. sizeof_void_space = le16_to_cpu(dir->direntlen) - dentry_length;
  400. if (sizeof_void_space == 0)
  401. return 0;
  402. padding_factor = 0;
  403. if (strlen(filename) % 4 != 0)
  404. padding_factor = 4 - (strlen(filename) % 4);
  405. new_entry_byte_reqd = strlen(filename) +
  406. sizeof(struct ext2_dirent) + padding_factor;
  407. if (sizeof_void_space >= new_entry_byte_reqd) {
  408. dir->direntlen = cpu_to_le16(dentry_length);
  409. return sizeof_void_space;
  410. }
  411. return 0;
  412. }
  413. int ext4fs_update_parent_dentry(char *filename, int file_type)
  414. {
  415. unsigned int *zero_buffer = NULL;
  416. char *root_first_block_buffer = NULL;
  417. int blk_idx;
  418. long int first_block_no_of_root = 0;
  419. int totalbytes = 0;
  420. unsigned int new_entry_byte_reqd;
  421. int sizeof_void_space = 0;
  422. int templength = 0;
  423. int inodeno = -1;
  424. int status;
  425. struct ext_filesystem *fs = get_fs();
  426. /* directory entry */
  427. struct ext2_dirent *dir;
  428. char *temp_dir = NULL;
  429. uint32_t new_blk_no;
  430. uint32_t new_size;
  431. uint32_t new_blockcnt;
  432. uint32_t directory_blocks;
  433. zero_buffer = zalloc(fs->blksz);
  434. if (!zero_buffer) {
  435. printf("No Memory\n");
  436. return -1;
  437. }
  438. root_first_block_buffer = zalloc(fs->blksz);
  439. if (!root_first_block_buffer) {
  440. free(zero_buffer);
  441. printf("No Memory\n");
  442. return -1;
  443. }
  444. new_entry_byte_reqd = ROUND(strlen(filename) +
  445. sizeof(struct ext2_dirent), 4);
  446. restart:
  447. directory_blocks = le32_to_cpu(g_parent_inode->size) >>
  448. LOG2_BLOCK_SIZE(ext4fs_root);
  449. blk_idx = directory_blocks - 1;
  450. restart_read:
  451. /* read the block no allocated to a file */
  452. first_block_no_of_root = read_allocated_block(g_parent_inode, blk_idx);
  453. if (first_block_no_of_root <= 0)
  454. goto fail;
  455. status = ext4fs_devread((lbaint_t)first_block_no_of_root
  456. * fs->sect_perblk,
  457. 0, fs->blksz, root_first_block_buffer);
  458. if (status == 0)
  459. goto fail;
  460. if (ext4fs_log_journal(root_first_block_buffer, first_block_no_of_root))
  461. goto fail;
  462. dir = (struct ext2_dirent *)root_first_block_buffer;
  463. totalbytes = 0;
  464. while (le16_to_cpu(dir->direntlen) > 0) {
  465. unsigned short used_len = ROUND(dir->namelen +
  466. sizeof(struct ext2_dirent), 4);
  467. /* last entry of block */
  468. if (fs->blksz - totalbytes == le16_to_cpu(dir->direntlen)) {
  469. /* check if new entry fits */
  470. if ((used_len + new_entry_byte_reqd) <=
  471. le16_to_cpu(dir->direntlen)) {
  472. dir->direntlen = cpu_to_le16(used_len);
  473. break;
  474. } else {
  475. if (blk_idx > 0) {
  476. printf("Block full, trying previous\n");
  477. blk_idx--;
  478. goto restart_read;
  479. }
  480. printf("All blocks full: Allocate new\n");
  481. if (le32_to_cpu(g_parent_inode->flags) &
  482. EXT4_EXTENTS_FL) {
  483. printf("Directory uses extents\n");
  484. goto fail;
  485. }
  486. if (directory_blocks >= INDIRECT_BLOCKS) {
  487. printf("Directory exceeds limit\n");
  488. goto fail;
  489. }
  490. new_blk_no = ext4fs_get_new_blk_no();
  491. if (new_blk_no == -1) {
  492. printf("no block left to assign\n");
  493. goto fail;
  494. }
  495. put_ext4((uint64_t)new_blk_no * fs->blksz, zero_buffer, fs->blksz);
  496. g_parent_inode->b.blocks.
  497. dir_blocks[directory_blocks] =
  498. cpu_to_le32(new_blk_no);
  499. new_size = le32_to_cpu(g_parent_inode->size);
  500. new_size += fs->blksz;
  501. g_parent_inode->size = cpu_to_le32(new_size);
  502. new_blockcnt = le32_to_cpu(g_parent_inode->blockcnt);
  503. new_blockcnt += fs->sect_perblk;
  504. g_parent_inode->blockcnt = cpu_to_le32(new_blockcnt);
  505. if (ext4fs_put_metadata
  506. (root_first_block_buffer,
  507. first_block_no_of_root))
  508. goto fail;
  509. goto restart;
  510. }
  511. }
  512. templength = le16_to_cpu(dir->direntlen);
  513. totalbytes = totalbytes + templength;
  514. sizeof_void_space = check_void_in_dentry(dir, filename);
  515. if (sizeof_void_space)
  516. break;
  517. dir = (struct ext2_dirent *)((char *)dir + templength);
  518. }
  519. /* make a pointer ready for creating next directory entry */
  520. templength = le16_to_cpu(dir->direntlen);
  521. totalbytes = totalbytes + templength;
  522. dir = (struct ext2_dirent *)((char *)dir + templength);
  523. /* get the next available inode number */
  524. inodeno = ext4fs_get_new_inode_no();
  525. if (inodeno == -1) {
  526. printf("no inode left to assign\n");
  527. goto fail;
  528. }
  529. dir->inode = cpu_to_le32(inodeno);
  530. if (sizeof_void_space)
  531. dir->direntlen = cpu_to_le16(sizeof_void_space);
  532. else
  533. dir->direntlen = cpu_to_le16(fs->blksz - totalbytes);
  534. dir->namelen = strlen(filename);
  535. dir->filetype = FILETYPE_REG; /* regular file */
  536. temp_dir = (char *)dir;
  537. temp_dir = temp_dir + sizeof(struct ext2_dirent);
  538. memcpy(temp_dir, filename, strlen(filename));
  539. /* update or write the 1st block of root inode */
  540. if (ext4fs_put_metadata(root_first_block_buffer,
  541. first_block_no_of_root))
  542. goto fail;
  543. fail:
  544. free(zero_buffer);
  545. free(root_first_block_buffer);
  546. return inodeno;
  547. }
  548. static int search_dir(struct ext2_inode *parent_inode, char *dirname)
  549. {
  550. int status;
  551. int inodeno = 0;
  552. int offset;
  553. int blk_idx;
  554. long int blknr;
  555. char *block_buffer = NULL;
  556. struct ext2_dirent *dir = NULL;
  557. struct ext_filesystem *fs = get_fs();
  558. uint32_t directory_blocks;
  559. char *direntname;
  560. directory_blocks = le32_to_cpu(parent_inode->size) >>
  561. LOG2_BLOCK_SIZE(ext4fs_root);
  562. block_buffer = zalloc(fs->blksz);
  563. if (!block_buffer)
  564. goto fail;
  565. /* get the block no allocated to a file */
  566. for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
  567. blknr = read_allocated_block(parent_inode, blk_idx);
  568. if (blknr <= 0)
  569. goto fail;
  570. /* read the directory block */
  571. status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk,
  572. 0, fs->blksz, (char *)block_buffer);
  573. if (status == 0)
  574. goto fail;
  575. offset = 0;
  576. do {
  577. dir = (struct ext2_dirent *)(block_buffer + offset);
  578. direntname = (char*)(dir) + sizeof(struct ext2_dirent);
  579. int direntlen = le16_to_cpu(dir->direntlen);
  580. if (direntlen < sizeof(struct ext2_dirent))
  581. break;
  582. if (dir->inode && (strlen(dirname) == dir->namelen) &&
  583. (strncmp(dirname, direntname, dir->namelen) == 0)) {
  584. inodeno = le32_to_cpu(dir->inode);
  585. break;
  586. }
  587. offset += direntlen;
  588. } while (offset < fs->blksz);
  589. if (inodeno > 0) {
  590. free(block_buffer);
  591. return inodeno;
  592. }
  593. }
  594. fail:
  595. free(block_buffer);
  596. return -1;
  597. }
  598. static int find_dir_depth(char *dirname)
  599. {
  600. char *token = strtok(dirname, "/");
  601. int count = 0;
  602. while (token != NULL) {
  603. token = strtok(NULL, "/");
  604. count++;
  605. }
  606. return count + 1 + 1;
  607. /*
  608. * for example for string /home/temp
  609. * depth=home(1)+temp(1)+1 extra for NULL;
  610. * so count is 4;
  611. */
  612. }
  613. static int parse_path(char **arr, char *dirname)
  614. {
  615. char *token = strtok(dirname, "/");
  616. int i = 0;
  617. /* add root */
  618. arr[i] = zalloc(strlen("/") + 1);
  619. if (!arr[i])
  620. return -ENOMEM;
  621. memcpy(arr[i++], "/", strlen("/"));
  622. /* add each path entry after root */
  623. while (token != NULL) {
  624. arr[i] = zalloc(strlen(token) + 1);
  625. if (!arr[i])
  626. return -ENOMEM;
  627. memcpy(arr[i++], token, strlen(token));
  628. token = strtok(NULL, "/");
  629. }
  630. arr[i] = NULL;
  631. return 0;
  632. }
  633. int ext4fs_iget(int inode_no, struct ext2_inode *inode)
  634. {
  635. if (ext4fs_read_inode(ext4fs_root, inode_no, inode) == 0)
  636. return -1;
  637. return 0;
  638. }
  639. /*
  640. * Function: ext4fs_get_parent_inode_num
  641. * Return Value: inode Number of the parent directory of file/Directory to be
  642. * created
  643. * dirname : Input parmater, input path name of the file/directory to be created
  644. * dname : Output parameter, to be filled with the name of the directory
  645. * extracted from dirname
  646. */
  647. int ext4fs_get_parent_inode_num(const char *dirname, char *dname, int flags)
  648. {
  649. int i;
  650. int depth = 0;
  651. int matched_inode_no;
  652. int result_inode_no = -1;
  653. char **ptr = NULL;
  654. char *depth_dirname = NULL;
  655. char *parse_dirname = NULL;
  656. struct ext2_inode *parent_inode = NULL;
  657. struct ext2_inode *first_inode = NULL;
  658. struct ext2_inode temp_inode;
  659. if (*dirname != '/') {
  660. printf("Please supply Absolute path\n");
  661. return -1;
  662. }
  663. /* TODO: input validation make equivalent to linux */
  664. depth_dirname = zalloc(strlen(dirname) + 1);
  665. if (!depth_dirname)
  666. return -ENOMEM;
  667. memcpy(depth_dirname, dirname, strlen(dirname));
  668. depth = find_dir_depth(depth_dirname);
  669. parse_dirname = zalloc(strlen(dirname) + 1);
  670. if (!parse_dirname)
  671. goto fail;
  672. memcpy(parse_dirname, dirname, strlen(dirname));
  673. /* allocate memory for each directory level */
  674. ptr = zalloc((depth) * sizeof(char *));
  675. if (!ptr)
  676. goto fail;
  677. if (parse_path(ptr, parse_dirname))
  678. goto fail;
  679. parent_inode = zalloc(sizeof(struct ext2_inode));
  680. if (!parent_inode)
  681. goto fail;
  682. first_inode = zalloc(sizeof(struct ext2_inode));
  683. if (!first_inode)
  684. goto fail;
  685. memcpy(parent_inode, ext4fs_root->inode, sizeof(struct ext2_inode));
  686. memcpy(first_inode, parent_inode, sizeof(struct ext2_inode));
  687. if (flags & F_FILE)
  688. result_inode_no = EXT2_ROOT_INO;
  689. for (i = 1; i < depth; i++) {
  690. matched_inode_no = search_dir(parent_inode, ptr[i]);
  691. if (matched_inode_no == -1) {
  692. if (ptr[i + 1] == NULL && i == 1) {
  693. result_inode_no = EXT2_ROOT_INO;
  694. goto end;
  695. } else {
  696. if (ptr[i + 1] == NULL)
  697. break;
  698. printf("Invalid path\n");
  699. result_inode_no = -1;
  700. goto fail;
  701. }
  702. } else {
  703. if (ptr[i + 1] != NULL) {
  704. memset(parent_inode, '\0',
  705. sizeof(struct ext2_inode));
  706. if (ext4fs_iget(matched_inode_no,
  707. parent_inode)) {
  708. result_inode_no = -1;
  709. goto fail;
  710. }
  711. result_inode_no = matched_inode_no;
  712. } else {
  713. break;
  714. }
  715. }
  716. }
  717. end:
  718. if (i == 1)
  719. matched_inode_no = search_dir(first_inode, ptr[i]);
  720. else
  721. matched_inode_no = search_dir(parent_inode, ptr[i]);
  722. if (matched_inode_no != -1) {
  723. ext4fs_iget(matched_inode_no, &temp_inode);
  724. if (le16_to_cpu(temp_inode.mode) & S_IFDIR) {
  725. printf("It is a Directory\n");
  726. result_inode_no = -1;
  727. goto fail;
  728. }
  729. }
  730. if (strlen(ptr[i]) > 256) {
  731. result_inode_no = -1;
  732. goto fail;
  733. }
  734. memcpy(dname, ptr[i], strlen(ptr[i]));
  735. fail:
  736. free(depth_dirname);
  737. free(parse_dirname);
  738. for (i = 0; i < depth; i++) {
  739. if (!ptr[i])
  740. break;
  741. free(ptr[i]);
  742. }
  743. free(ptr);
  744. free(parent_inode);
  745. free(first_inode);
  746. return result_inode_no;
  747. }
  748. static int unlink_filename(char *filename, unsigned int blknr)
  749. {
  750. int status;
  751. int inodeno = 0;
  752. int offset;
  753. char *block_buffer = NULL;
  754. struct ext2_dirent *dir = NULL;
  755. struct ext2_dirent *previous_dir;
  756. struct ext_filesystem *fs = get_fs();
  757. int ret = -1;
  758. char *direntname;
  759. block_buffer = zalloc(fs->blksz);
  760. if (!block_buffer)
  761. return -ENOMEM;
  762. /* read the directory block */
  763. status = ext4fs_devread((lbaint_t)blknr * fs->sect_perblk, 0,
  764. fs->blksz, block_buffer);
  765. if (status == 0)
  766. goto fail;
  767. offset = 0;
  768. do {
  769. previous_dir = dir;
  770. dir = (struct ext2_dirent *)(block_buffer + offset);
  771. direntname = (char *)(dir) + sizeof(struct ext2_dirent);
  772. int direntlen = le16_to_cpu(dir->direntlen);
  773. if (direntlen < sizeof(struct ext2_dirent))
  774. break;
  775. if (dir->inode && (strlen(filename) == dir->namelen) &&
  776. (strncmp(direntname, filename, dir->namelen) == 0)) {
  777. inodeno = le32_to_cpu(dir->inode);
  778. break;
  779. }
  780. offset += direntlen;
  781. } while (offset < fs->blksz);
  782. if (inodeno > 0) {
  783. printf("file found, deleting\n");
  784. if (ext4fs_log_journal(block_buffer, blknr))
  785. goto fail;
  786. if (previous_dir) {
  787. /* merge dir entry with predecessor */
  788. uint16_t new_len;
  789. new_len = le16_to_cpu(previous_dir->direntlen);
  790. new_len += le16_to_cpu(dir->direntlen);
  791. previous_dir->direntlen = cpu_to_le16(new_len);
  792. } else {
  793. /* invalidate dir entry */
  794. dir->inode = 0;
  795. }
  796. if (ext4fs_put_metadata(block_buffer, blknr))
  797. goto fail;
  798. ret = inodeno;
  799. }
  800. fail:
  801. free(block_buffer);
  802. return ret;
  803. }
  804. int ext4fs_filename_unlink(char *filename)
  805. {
  806. int blk_idx;
  807. long int blknr = -1;
  808. int inodeno = -1;
  809. uint32_t directory_blocks;
  810. directory_blocks = le32_to_cpu(g_parent_inode->size) >>
  811. LOG2_BLOCK_SIZE(ext4fs_root);
  812. /* read the block no allocated to a file */
  813. for (blk_idx = 0; blk_idx < directory_blocks; blk_idx++) {
  814. blknr = read_allocated_block(g_parent_inode, blk_idx);
  815. if (blknr <= 0)
  816. break;
  817. inodeno = unlink_filename(filename, blknr);
  818. if (inodeno != -1)
  819. return inodeno;
  820. }
  821. return -1;
  822. }
  823. uint32_t ext4fs_get_new_blk_no(void)
  824. {
  825. short i;
  826. short status;
  827. int remainder;
  828. unsigned int bg_idx;
  829. static int prev_bg_bitmap_index = -1;
  830. unsigned int blk_per_grp = le32_to_cpu(ext4fs_root->sblock.blocks_per_group);
  831. struct ext_filesystem *fs = get_fs();
  832. char *journal_buffer = zalloc(fs->blksz);
  833. char *zero_buffer = zalloc(fs->blksz);
  834. if (!journal_buffer || !zero_buffer)
  835. goto fail;
  836. if (fs->first_pass_bbmap == 0) {
  837. for (i = 0; i < fs->no_blkgrp; i++) {
  838. struct ext2_block_group *bgd = NULL;
  839. bgd = ext4fs_get_group_descriptor(fs, i);
  840. if (ext4fs_bg_get_free_blocks(bgd, fs)) {
  841. uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
  842. uint64_t b_bitmap_blk =
  843. ext4fs_bg_get_block_id(bgd, fs);
  844. if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
  845. memcpy(fs->blk_bmaps[i], zero_buffer,
  846. fs->blksz);
  847. put_ext4(b_bitmap_blk * fs->blksz,
  848. fs->blk_bmaps[i], fs->blksz);
  849. bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
  850. ext4fs_bg_set_flags(bgd, bg_flags);
  851. }
  852. fs->curr_blkno =
  853. _get_new_blk_no(fs->blk_bmaps[i]);
  854. if (fs->curr_blkno == -1)
  855. /* block bitmap is completely filled */
  856. continue;
  857. fs->curr_blkno = fs->curr_blkno +
  858. (i * fs->blksz * 8);
  859. fs->first_pass_bbmap++;
  860. ext4fs_bg_free_blocks_dec(bgd, fs);
  861. ext4fs_sb_free_blocks_dec(fs->sb);
  862. status = ext4fs_devread(b_bitmap_blk *
  863. fs->sect_perblk,
  864. 0, fs->blksz,
  865. journal_buffer);
  866. if (status == 0)
  867. goto fail;
  868. if (ext4fs_log_journal(journal_buffer,
  869. b_bitmap_blk))
  870. goto fail;
  871. goto success;
  872. } else {
  873. debug("no space left on block group %d\n", i);
  874. }
  875. }
  876. goto fail;
  877. } else {
  878. fs->curr_blkno++;
  879. restart:
  880. /* get the blockbitmap index respective to blockno */
  881. bg_idx = fs->curr_blkno / blk_per_grp;
  882. if (fs->blksz == 1024) {
  883. remainder = fs->curr_blkno % blk_per_grp;
  884. if (!remainder)
  885. bg_idx--;
  886. }
  887. /*
  888. * To skip completely filled block group bitmaps
  889. * Optimize the block allocation
  890. */
  891. if (bg_idx >= fs->no_blkgrp)
  892. goto fail;
  893. struct ext2_block_group *bgd = NULL;
  894. bgd = ext4fs_get_group_descriptor(fs, bg_idx);
  895. if (ext4fs_bg_get_free_blocks(bgd, fs) == 0) {
  896. debug("block group %u is full. Skipping\n", bg_idx);
  897. fs->curr_blkno = (bg_idx + 1) * blk_per_grp;
  898. if (fs->blksz == 1024)
  899. fs->curr_blkno += 1;
  900. goto restart;
  901. }
  902. uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
  903. uint64_t b_bitmap_blk = ext4fs_bg_get_block_id(bgd, fs);
  904. if (bg_flags & EXT4_BG_BLOCK_UNINIT) {
  905. memcpy(fs->blk_bmaps[bg_idx], zero_buffer, fs->blksz);
  906. put_ext4(b_bitmap_blk * fs->blksz,
  907. zero_buffer, fs->blksz);
  908. bg_flags &= ~EXT4_BG_BLOCK_UNINIT;
  909. ext4fs_bg_set_flags(bgd, bg_flags);
  910. }
  911. if (ext4fs_set_block_bmap(fs->curr_blkno, fs->blk_bmaps[bg_idx],
  912. bg_idx) != 0) {
  913. debug("going for restart for the block no %ld %u\n",
  914. fs->curr_blkno, bg_idx);
  915. fs->curr_blkno++;
  916. goto restart;
  917. }
  918. /* journal backup */
  919. if (prev_bg_bitmap_index != bg_idx) {
  920. status = ext4fs_devread(b_bitmap_blk * fs->sect_perblk,
  921. 0, fs->blksz, journal_buffer);
  922. if (status == 0)
  923. goto fail;
  924. if (ext4fs_log_journal(journal_buffer, b_bitmap_blk))
  925. goto fail;
  926. prev_bg_bitmap_index = bg_idx;
  927. }
  928. ext4fs_bg_free_blocks_dec(bgd, fs);
  929. ext4fs_sb_free_blocks_dec(fs->sb);
  930. goto success;
  931. }
  932. success:
  933. free(journal_buffer);
  934. free(zero_buffer);
  935. return fs->curr_blkno;
  936. fail:
  937. free(journal_buffer);
  938. free(zero_buffer);
  939. return -1;
  940. }
  941. int ext4fs_get_new_inode_no(void)
  942. {
  943. short i;
  944. short status;
  945. unsigned int ibmap_idx;
  946. static int prev_inode_bitmap_index = -1;
  947. unsigned int inodes_per_grp = le32_to_cpu(ext4fs_root->sblock.inodes_per_group);
  948. struct ext_filesystem *fs = get_fs();
  949. char *journal_buffer = zalloc(fs->blksz);
  950. char *zero_buffer = zalloc(fs->blksz);
  951. if (!journal_buffer || !zero_buffer)
  952. goto fail;
  953. int has_gdt_chksum = le32_to_cpu(fs->sb->feature_ro_compat) &
  954. EXT4_FEATURE_RO_COMPAT_GDT_CSUM ? 1 : 0;
  955. if (fs->first_pass_ibmap == 0) {
  956. for (i = 0; i < fs->no_blkgrp; i++) {
  957. uint32_t free_inodes;
  958. struct ext2_block_group *bgd = NULL;
  959. bgd = ext4fs_get_group_descriptor(fs, i);
  960. free_inodes = ext4fs_bg_get_free_inodes(bgd, fs);
  961. if (free_inodes) {
  962. uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
  963. uint64_t i_bitmap_blk =
  964. ext4fs_bg_get_inode_id(bgd, fs);
  965. if (has_gdt_chksum)
  966. bgd->bg_itable_unused = free_inodes;
  967. if (bg_flags & EXT4_BG_INODE_UNINIT) {
  968. put_ext4(i_bitmap_blk * fs->blksz,
  969. zero_buffer, fs->blksz);
  970. bg_flags &= ~EXT4_BG_INODE_UNINIT;
  971. ext4fs_bg_set_flags(bgd, bg_flags);
  972. memcpy(fs->inode_bmaps[i],
  973. zero_buffer, fs->blksz);
  974. }
  975. fs->curr_inode_no =
  976. _get_new_inode_no(fs->inode_bmaps[i]);
  977. if (fs->curr_inode_no == -1)
  978. /* inode bitmap is completely filled */
  979. continue;
  980. fs->curr_inode_no = fs->curr_inode_no +
  981. (i * inodes_per_grp);
  982. fs->first_pass_ibmap++;
  983. ext4fs_bg_free_inodes_dec(bgd, fs);
  984. if (has_gdt_chksum)
  985. ext4fs_bg_itable_unused_dec(bgd, fs);
  986. ext4fs_sb_free_inodes_dec(fs->sb);
  987. status = ext4fs_devread(i_bitmap_blk *
  988. fs->sect_perblk,
  989. 0, fs->blksz,
  990. journal_buffer);
  991. if (status == 0)
  992. goto fail;
  993. if (ext4fs_log_journal(journal_buffer,
  994. i_bitmap_blk))
  995. goto fail;
  996. goto success;
  997. } else
  998. debug("no inode left on block group %d\n", i);
  999. }
  1000. goto fail;
  1001. } else {
  1002. restart:
  1003. fs->curr_inode_no++;
  1004. /* get the blockbitmap index respective to blockno */
  1005. ibmap_idx = fs->curr_inode_no / inodes_per_grp;
  1006. struct ext2_block_group *bgd =
  1007. ext4fs_get_group_descriptor(fs, ibmap_idx);
  1008. uint16_t bg_flags = ext4fs_bg_get_flags(bgd);
  1009. uint64_t i_bitmap_blk = ext4fs_bg_get_inode_id(bgd, fs);
  1010. if (bg_flags & EXT4_BG_INODE_UNINIT) {
  1011. put_ext4(i_bitmap_blk * fs->blksz,
  1012. zero_buffer, fs->blksz);
  1013. bg_flags &= ~EXT4_BG_INODE_UNINIT;
  1014. ext4fs_bg_set_flags(bgd, bg_flags);
  1015. memcpy(fs->inode_bmaps[ibmap_idx], zero_buffer,
  1016. fs->blksz);
  1017. }
  1018. if (ext4fs_set_inode_bmap(fs->curr_inode_no,
  1019. fs->inode_bmaps[ibmap_idx],
  1020. ibmap_idx) != 0) {
  1021. debug("going for restart for the block no %d %u\n",
  1022. fs->curr_inode_no, ibmap_idx);
  1023. goto restart;
  1024. }
  1025. /* journal backup */
  1026. if (prev_inode_bitmap_index != ibmap_idx) {
  1027. status = ext4fs_devread(i_bitmap_blk * fs->sect_perblk,
  1028. 0, fs->blksz, journal_buffer);
  1029. if (status == 0)
  1030. goto fail;
  1031. if (ext4fs_log_journal(journal_buffer,
  1032. le32_to_cpu(bgd->inode_id)))
  1033. goto fail;
  1034. prev_inode_bitmap_index = ibmap_idx;
  1035. }
  1036. ext4fs_bg_free_inodes_dec(bgd, fs);
  1037. if (has_gdt_chksum)
  1038. bgd->bg_itable_unused = bgd->free_inodes;
  1039. ext4fs_sb_free_inodes_dec(fs->sb);
  1040. goto success;
  1041. }
  1042. success:
  1043. free(journal_buffer);
  1044. free(zero_buffer);
  1045. return fs->curr_inode_no;
  1046. fail:
  1047. free(journal_buffer);
  1048. free(zero_buffer);
  1049. return -1;
  1050. }
  1051. static void alloc_single_indirect_block(struct ext2_inode *file_inode,
  1052. unsigned int *total_remaining_blocks,
  1053. unsigned int *no_blks_reqd)
  1054. {
  1055. short i;
  1056. short status;
  1057. long int actual_block_no;
  1058. long int si_blockno;
  1059. /* si :single indirect */
  1060. __le32 *si_buffer = NULL;
  1061. __le32 *si_start_addr = NULL;
  1062. struct ext_filesystem *fs = get_fs();
  1063. if (*total_remaining_blocks != 0) {
  1064. si_buffer = zalloc(fs->blksz);
  1065. if (!si_buffer) {
  1066. printf("No Memory\n");
  1067. return;
  1068. }
  1069. si_start_addr = si_buffer;
  1070. si_blockno = ext4fs_get_new_blk_no();
  1071. if (si_blockno == -1) {
  1072. printf("no block left to assign\n");
  1073. goto fail;
  1074. }
  1075. (*no_blks_reqd)++;
  1076. debug("SIPB %ld: %u\n", si_blockno, *total_remaining_blocks);
  1077. status = ext4fs_devread((lbaint_t)si_blockno * fs->sect_perblk,
  1078. 0, fs->blksz, (char *)si_buffer);
  1079. memset(si_buffer, '\0', fs->blksz);
  1080. if (status == 0)
  1081. goto fail;
  1082. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  1083. actual_block_no = ext4fs_get_new_blk_no();
  1084. if (actual_block_no == -1) {
  1085. printf("no block left to assign\n");
  1086. goto fail;
  1087. }
  1088. *si_buffer = cpu_to_le32(actual_block_no);
  1089. debug("SIAB %u: %u\n", *si_buffer,
  1090. *total_remaining_blocks);
  1091. si_buffer++;
  1092. (*total_remaining_blocks)--;
  1093. if (*total_remaining_blocks == 0)
  1094. break;
  1095. }
  1096. /* write the block to disk */
  1097. put_ext4(((uint64_t) ((uint64_t)si_blockno * (uint64_t)fs->blksz)),
  1098. si_start_addr, fs->blksz);
  1099. file_inode->b.blocks.indir_block = cpu_to_le32(si_blockno);
  1100. }
  1101. fail:
  1102. free(si_start_addr);
  1103. }
  1104. static void alloc_double_indirect_block(struct ext2_inode *file_inode,
  1105. unsigned int *total_remaining_blocks,
  1106. unsigned int *no_blks_reqd)
  1107. {
  1108. short i;
  1109. short j;
  1110. short status;
  1111. long int actual_block_no;
  1112. /* di:double indirect */
  1113. long int di_blockno_parent;
  1114. long int di_blockno_child;
  1115. __le32 *di_parent_buffer = NULL;
  1116. __le32 *di_child_buff = NULL;
  1117. __le32 *di_block_start_addr = NULL;
  1118. __le32 *di_child_buff_start = NULL;
  1119. struct ext_filesystem *fs = get_fs();
  1120. if (*total_remaining_blocks != 0) {
  1121. /* double indirect parent block connecting to inode */
  1122. di_blockno_parent = ext4fs_get_new_blk_no();
  1123. if (di_blockno_parent == -1) {
  1124. printf("no block left to assign\n");
  1125. goto fail;
  1126. }
  1127. di_parent_buffer = zalloc(fs->blksz);
  1128. if (!di_parent_buffer)
  1129. goto fail;
  1130. di_block_start_addr = di_parent_buffer;
  1131. (*no_blks_reqd)++;
  1132. debug("DIPB %ld: %u\n", di_blockno_parent,
  1133. *total_remaining_blocks);
  1134. status = ext4fs_devread((lbaint_t)di_blockno_parent *
  1135. fs->sect_perblk, 0,
  1136. fs->blksz, (char *)di_parent_buffer);
  1137. if (!status) {
  1138. printf("%s: Device read error!\n", __func__);
  1139. goto fail;
  1140. }
  1141. memset(di_parent_buffer, '\0', fs->blksz);
  1142. /*
  1143. * start:for each double indirect parent
  1144. * block create one more block
  1145. */
  1146. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  1147. di_blockno_child = ext4fs_get_new_blk_no();
  1148. if (di_blockno_child == -1) {
  1149. printf("no block left to assign\n");
  1150. goto fail;
  1151. }
  1152. di_child_buff = zalloc(fs->blksz);
  1153. if (!di_child_buff)
  1154. goto fail;
  1155. di_child_buff_start = di_child_buff;
  1156. *di_parent_buffer = cpu_to_le32(di_blockno_child);
  1157. di_parent_buffer++;
  1158. (*no_blks_reqd)++;
  1159. debug("DICB %ld: %u\n", di_blockno_child,
  1160. *total_remaining_blocks);
  1161. status = ext4fs_devread((lbaint_t)di_blockno_child *
  1162. fs->sect_perblk, 0,
  1163. fs->blksz,
  1164. (char *)di_child_buff);
  1165. if (!status) {
  1166. printf("%s: Device read error!\n", __func__);
  1167. goto fail;
  1168. }
  1169. memset(di_child_buff, '\0', fs->blksz);
  1170. /* filling of actual datablocks for each child */
  1171. for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
  1172. actual_block_no = ext4fs_get_new_blk_no();
  1173. if (actual_block_no == -1) {
  1174. printf("no block left to assign\n");
  1175. goto fail;
  1176. }
  1177. *di_child_buff = cpu_to_le32(actual_block_no);
  1178. debug("DIAB %ld: %u\n", actual_block_no,
  1179. *total_remaining_blocks);
  1180. di_child_buff++;
  1181. (*total_remaining_blocks)--;
  1182. if (*total_remaining_blocks == 0)
  1183. break;
  1184. }
  1185. /* write the block table */
  1186. put_ext4(((uint64_t) ((uint64_t)di_blockno_child * (uint64_t)fs->blksz)),
  1187. di_child_buff_start, fs->blksz);
  1188. free(di_child_buff_start);
  1189. di_child_buff_start = NULL;
  1190. if (*total_remaining_blocks == 0)
  1191. break;
  1192. }
  1193. put_ext4(((uint64_t) ((uint64_t)di_blockno_parent * (uint64_t)fs->blksz)),
  1194. di_block_start_addr, fs->blksz);
  1195. file_inode->b.blocks.double_indir_block = cpu_to_le32(di_blockno_parent);
  1196. }
  1197. fail:
  1198. free(di_block_start_addr);
  1199. }
  1200. static void alloc_triple_indirect_block(struct ext2_inode *file_inode,
  1201. unsigned int *total_remaining_blocks,
  1202. unsigned int *no_blks_reqd)
  1203. {
  1204. short i;
  1205. short j;
  1206. short k;
  1207. long int actual_block_no;
  1208. /* ti: Triple Indirect */
  1209. long int ti_gp_blockno;
  1210. long int ti_parent_blockno;
  1211. long int ti_child_blockno;
  1212. __le32 *ti_gp_buff = NULL;
  1213. __le32 *ti_parent_buff = NULL;
  1214. __le32 *ti_child_buff = NULL;
  1215. __le32 *ti_gp_buff_start_addr = NULL;
  1216. __le32 *ti_pbuff_start_addr = NULL;
  1217. __le32 *ti_cbuff_start_addr = NULL;
  1218. struct ext_filesystem *fs = get_fs();
  1219. if (*total_remaining_blocks != 0) {
  1220. /* triple indirect grand parent block connecting to inode */
  1221. ti_gp_blockno = ext4fs_get_new_blk_no();
  1222. if (ti_gp_blockno == -1) {
  1223. printf("no block left to assign\n");
  1224. return;
  1225. }
  1226. ti_gp_buff = zalloc(fs->blksz);
  1227. if (!ti_gp_buff)
  1228. return;
  1229. ti_gp_buff_start_addr = ti_gp_buff;
  1230. (*no_blks_reqd)++;
  1231. debug("TIGPB %ld: %u\n", ti_gp_blockno,
  1232. *total_remaining_blocks);
  1233. /* for each 4 byte grand parent entry create one more block */
  1234. for (i = 0; i < (fs->blksz / sizeof(int)); i++) {
  1235. ti_parent_blockno = ext4fs_get_new_blk_no();
  1236. if (ti_parent_blockno == -1) {
  1237. printf("no block left to assign\n");
  1238. goto fail;
  1239. }
  1240. ti_parent_buff = zalloc(fs->blksz);
  1241. if (!ti_parent_buff)
  1242. goto fail;
  1243. ti_pbuff_start_addr = ti_parent_buff;
  1244. *ti_gp_buff = cpu_to_le32(ti_parent_blockno);
  1245. ti_gp_buff++;
  1246. (*no_blks_reqd)++;
  1247. debug("TIPB %ld: %u\n", ti_parent_blockno,
  1248. *total_remaining_blocks);
  1249. /* for each 4 byte entry parent create one more block */
  1250. for (j = 0; j < (fs->blksz / sizeof(int)); j++) {
  1251. ti_child_blockno = ext4fs_get_new_blk_no();
  1252. if (ti_child_blockno == -1) {
  1253. printf("no block left assign\n");
  1254. goto fail1;
  1255. }
  1256. ti_child_buff = zalloc(fs->blksz);
  1257. if (!ti_child_buff)
  1258. goto fail1;
  1259. ti_cbuff_start_addr = ti_child_buff;
  1260. *ti_parent_buff = cpu_to_le32(ti_child_blockno);
  1261. ti_parent_buff++;
  1262. (*no_blks_reqd)++;
  1263. debug("TICB %ld: %u\n", ti_parent_blockno,
  1264. *total_remaining_blocks);
  1265. /* fill actual datablocks for each child */
  1266. for (k = 0; k < (fs->blksz / sizeof(int));
  1267. k++) {
  1268. actual_block_no =
  1269. ext4fs_get_new_blk_no();
  1270. if (actual_block_no == -1) {
  1271. printf("no block left\n");
  1272. free(ti_cbuff_start_addr);
  1273. goto fail1;
  1274. }
  1275. *ti_child_buff = cpu_to_le32(actual_block_no);
  1276. debug("TIAB %ld: %u\n", actual_block_no,
  1277. *total_remaining_blocks);
  1278. ti_child_buff++;
  1279. (*total_remaining_blocks)--;
  1280. if (*total_remaining_blocks == 0)
  1281. break;
  1282. }
  1283. /* write the child block */
  1284. put_ext4(((uint64_t) ((uint64_t)ti_child_blockno *
  1285. (uint64_t)fs->blksz)),
  1286. ti_cbuff_start_addr, fs->blksz);
  1287. free(ti_cbuff_start_addr);
  1288. if (*total_remaining_blocks == 0)
  1289. break;
  1290. }
  1291. /* write the parent block */
  1292. put_ext4(((uint64_t) ((uint64_t)ti_parent_blockno * (uint64_t)fs->blksz)),
  1293. ti_pbuff_start_addr, fs->blksz);
  1294. free(ti_pbuff_start_addr);
  1295. if (*total_remaining_blocks == 0)
  1296. break;
  1297. }
  1298. /* write the grand parent block */
  1299. put_ext4(((uint64_t) ((uint64_t)ti_gp_blockno * (uint64_t)fs->blksz)),
  1300. ti_gp_buff_start_addr, fs->blksz);
  1301. file_inode->b.blocks.triple_indir_block = cpu_to_le32(ti_gp_blockno);
  1302. free(ti_gp_buff_start_addr);
  1303. return;
  1304. }
  1305. fail1:
  1306. free(ti_pbuff_start_addr);
  1307. fail:
  1308. free(ti_gp_buff_start_addr);
  1309. }
  1310. void ext4fs_allocate_blocks(struct ext2_inode *file_inode,
  1311. unsigned int total_remaining_blocks,
  1312. unsigned int *total_no_of_block)
  1313. {
  1314. short i;
  1315. long int direct_blockno;
  1316. unsigned int no_blks_reqd = 0;
  1317. /* allocation of direct blocks */
  1318. for (i = 0; total_remaining_blocks && i < INDIRECT_BLOCKS; i++) {
  1319. direct_blockno = ext4fs_get_new_blk_no();
  1320. if (direct_blockno == -1) {
  1321. printf("no block left to assign\n");
  1322. return;
  1323. }
  1324. file_inode->b.blocks.dir_blocks[i] = cpu_to_le32(direct_blockno);
  1325. debug("DB %ld: %u\n", direct_blockno, total_remaining_blocks);
  1326. total_remaining_blocks--;
  1327. }
  1328. alloc_single_indirect_block(file_inode, &total_remaining_blocks,
  1329. &no_blks_reqd);
  1330. alloc_double_indirect_block(file_inode, &total_remaining_blocks,
  1331. &no_blks_reqd);
  1332. alloc_triple_indirect_block(file_inode, &total_remaining_blocks,
  1333. &no_blks_reqd);
  1334. *total_no_of_block += no_blks_reqd;
  1335. }
  1336. #endif
  1337. static struct ext4_extent_header *ext4fs_get_extent_block
  1338. (struct ext2_data *data, char *buf,
  1339. struct ext4_extent_header *ext_block,
  1340. uint32_t fileblock, int log2_blksz)
  1341. {
  1342. struct ext4_extent_idx *index;
  1343. unsigned long long block;
  1344. int blksz = EXT2_BLOCK_SIZE(data);
  1345. int i;
  1346. while (1) {
  1347. index = (struct ext4_extent_idx *)(ext_block + 1);
  1348. if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
  1349. return NULL;
  1350. if (ext_block->eh_depth == 0)
  1351. return ext_block;
  1352. i = -1;
  1353. do {
  1354. i++;
  1355. if (i >= le16_to_cpu(ext_block->eh_entries))
  1356. break;
  1357. } while (fileblock >= le32_to_cpu(index[i].ei_block));
  1358. if (--i < 0)
  1359. return NULL;
  1360. block = le16_to_cpu(index[i].ei_leaf_hi);
  1361. block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
  1362. if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
  1363. buf))
  1364. ext_block = (struct ext4_extent_header *)buf;
  1365. else
  1366. return NULL;
  1367. }
  1368. }
  1369. static int ext4fs_blockgroup
  1370. (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
  1371. {
  1372. long int blkno;
  1373. unsigned int blkoff, desc_per_blk;
  1374. int log2blksz = get_fs()->dev_desc->log2blksz;
  1375. int desc_size = get_fs()->gdsize;
  1376. desc_per_blk = EXT2_BLOCK_SIZE(data) / desc_size;
  1377. blkno = le32_to_cpu(data->sblock.first_data_block) + 1 +
  1378. group / desc_per_blk;
  1379. blkoff = (group % desc_per_blk) * desc_size;
  1380. debug("ext4fs read %d group descriptor (blkno %ld blkoff %u)\n",
  1381. group, blkno, blkoff);
  1382. return ext4fs_devread((lbaint_t)blkno <<
  1383. (LOG2_BLOCK_SIZE(data) - log2blksz),
  1384. blkoff, desc_size, (char *)blkgrp);
  1385. }
  1386. int ext4fs_read_inode(struct ext2_data *data, int ino, struct ext2_inode *inode)
  1387. {
  1388. struct ext2_block_group blkgrp;
  1389. struct ext2_sblock *sblock = &data->sblock;
  1390. struct ext_filesystem *fs = get_fs();
  1391. int log2blksz = get_fs()->dev_desc->log2blksz;
  1392. int inodes_per_block, status;
  1393. long int blkno;
  1394. unsigned int blkoff;
  1395. /* It is easier to calculate if the first inode is 0. */
  1396. ino--;
  1397. status = ext4fs_blockgroup(data, ino / le32_to_cpu
  1398. (sblock->inodes_per_group), &blkgrp);
  1399. if (status == 0)
  1400. return 0;
  1401. inodes_per_block = EXT2_BLOCK_SIZE(data) / fs->inodesz;
  1402. blkno = ext4fs_bg_get_inode_table_id(&blkgrp, fs) +
  1403. (ino % le32_to_cpu(sblock->inodes_per_group)) / inodes_per_block;
  1404. blkoff = (ino % inodes_per_block) * fs->inodesz;
  1405. /* Read the inode. */
  1406. status = ext4fs_devread((lbaint_t)blkno << (LOG2_BLOCK_SIZE(data) -
  1407. log2blksz), blkoff,
  1408. sizeof(struct ext2_inode), (char *)inode);
  1409. if (status == 0)
  1410. return 0;
  1411. return 1;
  1412. }
  1413. long int read_allocated_block(struct ext2_inode *inode, int fileblock)
  1414. {
  1415. long int blknr;
  1416. int blksz;
  1417. int log2_blksz;
  1418. int status;
  1419. long int rblock;
  1420. long int perblock_parent;
  1421. long int perblock_child;
  1422. unsigned long long start;
  1423. /* get the blocksize of the filesystem */
  1424. blksz = EXT2_BLOCK_SIZE(ext4fs_root);
  1425. log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
  1426. - get_fs()->dev_desc->log2blksz;
  1427. if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
  1428. long int startblock, endblock;
  1429. char *buf = zalloc(blksz);
  1430. if (!buf)
  1431. return -ENOMEM;
  1432. struct ext4_extent_header *ext_block;
  1433. struct ext4_extent *extent;
  1434. int i;
  1435. ext_block =
  1436. ext4fs_get_extent_block(ext4fs_root, buf,
  1437. (struct ext4_extent_header *)
  1438. inode->b.blocks.dir_blocks,
  1439. fileblock, log2_blksz);
  1440. if (!ext_block) {
  1441. printf("invalid extent block\n");
  1442. free(buf);
  1443. return -EINVAL;
  1444. }
  1445. extent = (struct ext4_extent *)(ext_block + 1);
  1446. for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
  1447. startblock = le32_to_cpu(extent[i].ee_block);
  1448. endblock = startblock + le16_to_cpu(extent[i].ee_len);
  1449. if (startblock > fileblock) {
  1450. /* Sparse file */
  1451. free(buf);
  1452. return 0;
  1453. } else if (fileblock < endblock) {
  1454. start = le16_to_cpu(extent[i].ee_start_hi);
  1455. start = (start << 32) +
  1456. le32_to_cpu(extent[i].ee_start_lo);
  1457. free(buf);
  1458. return (fileblock - startblock) + start;
  1459. }
  1460. }
  1461. free(buf);
  1462. return 0;
  1463. }
  1464. /* Direct blocks. */
  1465. if (fileblock < INDIRECT_BLOCKS)
  1466. blknr = le32_to_cpu(inode->b.blocks.dir_blocks[fileblock]);
  1467. /* Indirect. */
  1468. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4))) {
  1469. if (ext4fs_indir1_block == NULL) {
  1470. ext4fs_indir1_block = zalloc(blksz);
  1471. if (ext4fs_indir1_block == NULL) {
  1472. printf("** SI ext2fs read block (indir 1)"
  1473. "malloc failed. **\n");
  1474. return -1;
  1475. }
  1476. ext4fs_indir1_size = blksz;
  1477. ext4fs_indir1_blkno = -1;
  1478. }
  1479. if (blksz != ext4fs_indir1_size) {
  1480. free(ext4fs_indir1_block);
  1481. ext4fs_indir1_block = NULL;
  1482. ext4fs_indir1_size = 0;
  1483. ext4fs_indir1_blkno = -1;
  1484. ext4fs_indir1_block = zalloc(blksz);
  1485. if (ext4fs_indir1_block == NULL) {
  1486. printf("** SI ext2fs read block (indir 1):"
  1487. "malloc failed. **\n");
  1488. return -1;
  1489. }
  1490. ext4fs_indir1_size = blksz;
  1491. }
  1492. if ((le32_to_cpu(inode->b.blocks.indir_block) <<
  1493. log2_blksz) != ext4fs_indir1_blkno) {
  1494. status =
  1495. ext4fs_devread((lbaint_t)le32_to_cpu
  1496. (inode->b.blocks.
  1497. indir_block) << log2_blksz, 0,
  1498. blksz, (char *)ext4fs_indir1_block);
  1499. if (status == 0) {
  1500. printf("** SI ext2fs read block (indir 1)"
  1501. "failed. **\n");
  1502. return -1;
  1503. }
  1504. ext4fs_indir1_blkno =
  1505. le32_to_cpu(inode->b.blocks.
  1506. indir_block) << log2_blksz;
  1507. }
  1508. blknr = le32_to_cpu(ext4fs_indir1_block
  1509. [fileblock - INDIRECT_BLOCKS]);
  1510. }
  1511. /* Double indirect. */
  1512. else if (fileblock < (INDIRECT_BLOCKS + (blksz / 4 *
  1513. (blksz / 4 + 1)))) {
  1514. long int perblock = blksz / 4;
  1515. long int rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4);
  1516. if (ext4fs_indir1_block == NULL) {
  1517. ext4fs_indir1_block = zalloc(blksz);
  1518. if (ext4fs_indir1_block == NULL) {
  1519. printf("** DI ext2fs read block (indir 2 1)"
  1520. "malloc failed. **\n");
  1521. return -1;
  1522. }
  1523. ext4fs_indir1_size = blksz;
  1524. ext4fs_indir1_blkno = -1;
  1525. }
  1526. if (blksz != ext4fs_indir1_size) {
  1527. free(ext4fs_indir1_block);
  1528. ext4fs_indir1_block = NULL;
  1529. ext4fs_indir1_size = 0;
  1530. ext4fs_indir1_blkno = -1;
  1531. ext4fs_indir1_block = zalloc(blksz);
  1532. if (ext4fs_indir1_block == NULL) {
  1533. printf("** DI ext2fs read block (indir 2 1)"
  1534. "malloc failed. **\n");
  1535. return -1;
  1536. }
  1537. ext4fs_indir1_size = blksz;
  1538. }
  1539. if ((le32_to_cpu(inode->b.blocks.double_indir_block) <<
  1540. log2_blksz) != ext4fs_indir1_blkno) {
  1541. status =
  1542. ext4fs_devread((lbaint_t)le32_to_cpu
  1543. (inode->b.blocks.
  1544. double_indir_block) << log2_blksz,
  1545. 0, blksz,
  1546. (char *)ext4fs_indir1_block);
  1547. if (status == 0) {
  1548. printf("** DI ext2fs read block (indir 2 1)"
  1549. "failed. **\n");
  1550. return -1;
  1551. }
  1552. ext4fs_indir1_blkno =
  1553. le32_to_cpu(inode->b.blocks.double_indir_block) <<
  1554. log2_blksz;
  1555. }
  1556. if (ext4fs_indir2_block == NULL) {
  1557. ext4fs_indir2_block = zalloc(blksz);
  1558. if (ext4fs_indir2_block == NULL) {
  1559. printf("** DI ext2fs read block (indir 2 2)"
  1560. "malloc failed. **\n");
  1561. return -1;
  1562. }
  1563. ext4fs_indir2_size = blksz;
  1564. ext4fs_indir2_blkno = -1;
  1565. }
  1566. if (blksz != ext4fs_indir2_size) {
  1567. free(ext4fs_indir2_block);
  1568. ext4fs_indir2_block = NULL;
  1569. ext4fs_indir2_size = 0;
  1570. ext4fs_indir2_blkno = -1;
  1571. ext4fs_indir2_block = zalloc(blksz);
  1572. if (ext4fs_indir2_block == NULL) {
  1573. printf("** DI ext2fs read block (indir 2 2)"
  1574. "malloc failed. **\n");
  1575. return -1;
  1576. }
  1577. ext4fs_indir2_size = blksz;
  1578. }
  1579. if ((le32_to_cpu(ext4fs_indir1_block[rblock / perblock]) <<
  1580. log2_blksz) != ext4fs_indir2_blkno) {
  1581. status = ext4fs_devread((lbaint_t)le32_to_cpu
  1582. (ext4fs_indir1_block
  1583. [rblock /
  1584. perblock]) << log2_blksz, 0,
  1585. blksz,
  1586. (char *)ext4fs_indir2_block);
  1587. if (status == 0) {
  1588. printf("** DI ext2fs read block (indir 2 2)"
  1589. "failed. **\n");
  1590. return -1;
  1591. }
  1592. ext4fs_indir2_blkno =
  1593. le32_to_cpu(ext4fs_indir1_block[rblock
  1594. /
  1595. perblock]) <<
  1596. log2_blksz;
  1597. }
  1598. blknr = le32_to_cpu(ext4fs_indir2_block[rblock % perblock]);
  1599. }
  1600. /* Tripple indirect. */
  1601. else {
  1602. rblock = fileblock - (INDIRECT_BLOCKS + blksz / 4 +
  1603. (blksz / 4 * blksz / 4));
  1604. perblock_child = blksz / 4;
  1605. perblock_parent = ((blksz / 4) * (blksz / 4));
  1606. if (ext4fs_indir1_block == NULL) {
  1607. ext4fs_indir1_block = zalloc(blksz);
  1608. if (ext4fs_indir1_block == NULL) {
  1609. printf("** TI ext2fs read block (indir 2 1)"
  1610. "malloc failed. **\n");
  1611. return -1;
  1612. }
  1613. ext4fs_indir1_size = blksz;
  1614. ext4fs_indir1_blkno = -1;
  1615. }
  1616. if (blksz != ext4fs_indir1_size) {
  1617. free(ext4fs_indir1_block);
  1618. ext4fs_indir1_block = NULL;
  1619. ext4fs_indir1_size = 0;
  1620. ext4fs_indir1_blkno = -1;
  1621. ext4fs_indir1_block = zalloc(blksz);
  1622. if (ext4fs_indir1_block == NULL) {
  1623. printf("** TI ext2fs read block (indir 2 1)"
  1624. "malloc failed. **\n");
  1625. return -1;
  1626. }
  1627. ext4fs_indir1_size = blksz;
  1628. }
  1629. if ((le32_to_cpu(inode->b.blocks.triple_indir_block) <<
  1630. log2_blksz) != ext4fs_indir1_blkno) {
  1631. status = ext4fs_devread
  1632. ((lbaint_t)
  1633. le32_to_cpu(inode->b.blocks.triple_indir_block)
  1634. << log2_blksz, 0, blksz,
  1635. (char *)ext4fs_indir1_block);
  1636. if (status == 0) {
  1637. printf("** TI ext2fs read block (indir 2 1)"
  1638. "failed. **\n");
  1639. return -1;
  1640. }
  1641. ext4fs_indir1_blkno =
  1642. le32_to_cpu(inode->b.blocks.triple_indir_block) <<
  1643. log2_blksz;
  1644. }
  1645. if (ext4fs_indir2_block == NULL) {
  1646. ext4fs_indir2_block = zalloc(blksz);
  1647. if (ext4fs_indir2_block == NULL) {
  1648. printf("** TI ext2fs read block (indir 2 2)"
  1649. "malloc failed. **\n");
  1650. return -1;
  1651. }
  1652. ext4fs_indir2_size = blksz;
  1653. ext4fs_indir2_blkno = -1;
  1654. }
  1655. if (blksz != ext4fs_indir2_size) {
  1656. free(ext4fs_indir2_block);
  1657. ext4fs_indir2_block = NULL;
  1658. ext4fs_indir2_size = 0;
  1659. ext4fs_indir2_blkno = -1;
  1660. ext4fs_indir2_block = zalloc(blksz);
  1661. if (ext4fs_indir2_block == NULL) {
  1662. printf("** TI ext2fs read block (indir 2 2)"
  1663. "malloc failed. **\n");
  1664. return -1;
  1665. }
  1666. ext4fs_indir2_size = blksz;
  1667. }
  1668. if ((le32_to_cpu(ext4fs_indir1_block[rblock /
  1669. perblock_parent]) <<
  1670. log2_blksz)
  1671. != ext4fs_indir2_blkno) {
  1672. status = ext4fs_devread((lbaint_t)le32_to_cpu
  1673. (ext4fs_indir1_block
  1674. [rblock /
  1675. perblock_parent]) <<
  1676. log2_blksz, 0, blksz,
  1677. (char *)ext4fs_indir2_block);
  1678. if (status == 0) {
  1679. printf("** TI ext2fs read block (indir 2 2)"
  1680. "failed. **\n");
  1681. return -1;
  1682. }
  1683. ext4fs_indir2_blkno =
  1684. le32_to_cpu(ext4fs_indir1_block[rblock /
  1685. perblock_parent])
  1686. << log2_blksz;
  1687. }
  1688. if (ext4fs_indir3_block == NULL) {
  1689. ext4fs_indir3_block = zalloc(blksz);
  1690. if (ext4fs_indir3_block == NULL) {
  1691. printf("** TI ext2fs read block (indir 2 2)"
  1692. "malloc failed. **\n");
  1693. return -1;
  1694. }
  1695. ext4fs_indir3_size = blksz;
  1696. ext4fs_indir3_blkno = -1;
  1697. }
  1698. if (blksz != ext4fs_indir3_size) {
  1699. free(ext4fs_indir3_block);
  1700. ext4fs_indir3_block = NULL;
  1701. ext4fs_indir3_size = 0;
  1702. ext4fs_indir3_blkno = -1;
  1703. ext4fs_indir3_block = zalloc(blksz);
  1704. if (ext4fs_indir3_block == NULL) {
  1705. printf("** TI ext2fs read block (indir 2 2)"
  1706. "malloc failed. **\n");

Large files files are truncated, but you can click here to view the full file