Commit fc0fc50f38a4d7d0554558076a79dfe8b0d78cd5

Authored by Ionut Nicu
Committed by Tom Rini
1 parent ee456337c6

ext4fs: Add ext4 extent cache for read operations

In an ext4 filesystem, the inode corresponding to a file has a 60-byte
area which contains an extent header structure and up to 4 extent
structures (5 x 12 bytes).

For files that need more than 4 extents to be represented (either files
larger than 4 x 128MB = 512MB or smaller files but very fragmented),
ext4 creates extent index structures. Each extent index points to a 4KB
physical block where one extent header and additional 340 extents could
be stored.

The current u-boot ext4 code is very inefficient when it tries to load a
file which has extent indexes. For each logical file block the code will
read over and over again the same blocks of 4096 bytes from the disk.

Since the extent tree in a file is always the same, we can cache the
extent structures in memory before actually starting to read the file.

This patch creates a simple linked list of structures holding information
about all the extents used to represent a file. The list is sorted by
the logical block number (ee_block) so that we can easily find the
proper extent information for any file block.

Without this patch, a 69MB file which had just one extent index pointing
to a block with another 6 extents was read in approximately 3 minutes.
With this patch applied the same file can be read in almost 20 seconds.

Signed-off-by: Ionut Nicu <ioan.nicu.ext@nsn.com>

Showing 3 changed files with 130 additions and 73 deletions Side-by-side Diff

fs/ext4/ext4_common.c
... ... @@ -26,6 +26,7 @@
26 26 #include <stddef.h>
27 27 #include <linux/stat.h>
28 28 #include <linux/time.h>
  29 +#include <linux/list.h>
29 30 #include <asm/byteorder.h>
30 31 #include "ext4_common.h"
31 32  
... ... @@ -44,6 +45,14 @@
44 45 struct ext2_inode *g_parent_inode;
45 46 static int symlinknest;
46 47  
  48 +struct ext4_extent_node {
  49 + uint32_t block;
  50 + uint16_t len;
  51 + uint64_t start;
  52 + struct list_head lh;
  53 +};
  54 +static LIST_HEAD(ext4_extent_lh);
  55 +
47 56 #if defined(CONFIG_EXT4_WRITE)
48 57 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n)
49 58 {
50 59  
51 60  
52 61  
53 62  
54 63  
55 64  
56 65  
57 66  
58 67  
59 68  
... ... @@ -1407,45 +1416,102 @@
1407 1416  
1408 1417 #endif
1409 1418  
1410   -static struct ext4_extent_header *ext4fs_get_extent_block
1411   - (struct ext2_data *data, char *buf,
1412   - struct ext4_extent_header *ext_block,
1413   - uint32_t fileblock, int log2_blksz)
  1419 +static void ext4fs_extent_cache_insert(struct ext4_extent_node *new)
1414 1420 {
  1421 + struct ext4_extent_node *node;
  1422 +
  1423 + list_for_each_entry(node, &ext4_extent_lh, lh)
  1424 + if (node->block > new->block) {
  1425 + list_add_tail(&new->lh, &node->lh);
  1426 + return;
  1427 + }
  1428 + list_add_tail(&new->lh, &ext4_extent_lh);
  1429 +}
  1430 +
  1431 +static int __ext4fs_build_extent_cache(struct ext2_data *data,
  1432 + struct ext4_extent_header *ext_block)
  1433 +{
  1434 + int blksz = EXT2_BLOCK_SIZE(data);
  1435 + int log2_blksz = LOG2_BLOCK_SIZE(data)
  1436 + - get_fs()->dev_desc->log2blksz;
  1437 + struct ext4_extent_node *node;
1415 1438 struct ext4_extent_idx *index;
  1439 + struct ext4_extent *extent;
1416 1440 unsigned long long block;
1417   - int blksz = EXT2_BLOCK_SIZE(data);
1418   - int i;
  1441 + char *buf;
  1442 + int i, err;
1419 1443  
1420   - while (1) {
1421   - index = (struct ext4_extent_idx *)(ext_block + 1);
  1444 + if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
  1445 + return -EINVAL;
1422 1446  
1423   - if (le16_to_cpu(ext_block->eh_magic) != EXT4_EXT_MAGIC)
1424   - return 0;
  1447 + if (ext_block->eh_depth == 0) {
  1448 + extent = (struct ext4_extent *)(ext_block + 1);
  1449 + for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
  1450 + node = malloc(sizeof(*node));
  1451 + if (!node)
  1452 + return -ENOMEM;
  1453 + node->block = le32_to_cpu(extent[i].ee_block);
  1454 + node->len = le16_to_cpu(extent[i].ee_len);
  1455 + node->start = le16_to_cpu(extent[i].ee_start_hi);
  1456 + node->start = (node->start << 32) +
  1457 + le32_to_cpu(extent[i].ee_start_lo);
  1458 + ext4fs_extent_cache_insert(node);
  1459 + }
  1460 + return 0;
  1461 + }
1425 1462  
1426   - if (ext_block->eh_depth == 0)
1427   - return ext_block;
1428   - i = -1;
1429   - do {
1430   - i++;
1431   - if (i >= le16_to_cpu(ext_block->eh_entries))
1432   - break;
1433   - } while (fileblock >= le32_to_cpu(index[i].ei_block));
  1463 + index = (struct ext4_extent_idx *)(ext_block + 1);
  1464 + for (i = 0; i < le16_to_cpu(ext_block->eh_entries); i++) {
  1465 + buf = malloc(blksz);
  1466 + if (!buf)
  1467 + return -ENOMEM;
1434 1468  
1435   - if (--i < 0)
1436   - return 0;
1437   -
1438 1469 block = le16_to_cpu(index[i].ei_leaf_hi);
1439 1470 block = (block << 32) + le32_to_cpu(index[i].ei_leaf_lo);
1440 1471  
1441   - if (ext4fs_devread((lbaint_t)block << log2_blksz, 0, blksz,
1442   - buf))
1443   - ext_block = (struct ext4_extent_header *)buf;
1444   - else
1445   - return 0;
  1472 + if (!ext4fs_devread(block << log2_blksz, 0, blksz, buf)) {
  1473 + free(buf);
  1474 + return -EIO;
  1475 + }
  1476 +
  1477 + err = __ext4fs_build_extent_cache(data,
  1478 + (struct ext4_extent_header *) buf);
  1479 + free(buf);
  1480 + if (err < 0)
  1481 + return err;
1446 1482 }
  1483 +
  1484 + return 0;
1447 1485 }
1448 1486  
  1487 +int ext4fs_build_extent_cache(struct ext2_inode *inode)
  1488 +{
  1489 + return __ext4fs_build_extent_cache(ext4fs_root,
  1490 + (struct ext4_extent_header *)
  1491 + inode->b.blocks.dir_blocks);
  1492 +}
  1493 +
  1494 +void ext4fs_free_extent_cache(void)
  1495 +{
  1496 + struct ext4_extent_node *node, *tmp;
  1497 +
  1498 + list_for_each_entry_safe(node, tmp, &ext4_extent_lh, lh) {
  1499 + list_del(&node->lh);
  1500 + free(node);
  1501 + }
  1502 +}
  1503 +
  1504 +static struct ext4_extent_node *ext4fs_extent_cache_get(uint32_t block)
  1505 +{
  1506 + struct ext4_extent_node *node;
  1507 +
  1508 + list_for_each_entry(node, &ext4_extent_lh, lh)
  1509 + if (block >= node->block && block < node->block + node->len)
  1510 + return node;
  1511 +
  1512 + return NULL;
  1513 +}
  1514 +
1449 1515 static int ext4fs_blockgroup
1450 1516 (struct ext2_data *data, int group, struct ext2_block_group *blkgrp)
1451 1517 {
1452 1518  
1453 1519  
1454 1520  
... ... @@ -1508,54 +1574,22 @@
1508 1574 long int rblock;
1509 1575 long int perblock_parent;
1510 1576 long int perblock_child;
1511   - unsigned long long start;
  1577 +
1512 1578 /* get the blocksize of the filesystem */
1513 1579 blksz = EXT2_BLOCK_SIZE(ext4fs_root);
1514 1580 log2_blksz = LOG2_BLOCK_SIZE(ext4fs_root)
1515 1581 - get_fs()->dev_desc->log2blksz;
1516 1582  
1517 1583 if (le32_to_cpu(inode->flags) & EXT4_EXTENTS_FL) {
1518   - char *buf = zalloc(blksz);
1519   - if (!buf)
1520   - return -ENOMEM;
1521   - struct ext4_extent_header *ext_block;
1522   - struct ext4_extent *extent;
1523   - int i = -1;
1524   - ext_block =
1525   - ext4fs_get_extent_block(ext4fs_root, buf,
1526   - (struct ext4_extent_header *)
1527   - inode->b.blocks.dir_blocks,
1528   - fileblock, log2_blksz);
1529   - if (!ext_block) {
1530   - printf("invalid extent block\n");
1531   - free(buf);
1532   - return -EINVAL;
1533   - }
  1584 + struct ext4_extent_node *node;
1534 1585  
1535   - extent = (struct ext4_extent *)(ext_block + 1);
1536   -
1537   - do {
1538   - i++;
1539   - if (i >= le16_to_cpu(ext_block->eh_entries))
1540   - break;
1541   - } while (fileblock >= le32_to_cpu(extent[i].ee_block));
1542   - if (--i >= 0) {
1543   - fileblock -= le32_to_cpu(extent[i].ee_block);
1544   - if (fileblock >= le16_to_cpu(extent[i].ee_len)) {
1545   - free(buf);
1546   - return 0;
1547   - }
1548   -
1549   - start = le16_to_cpu(extent[i].ee_start_hi);
1550   - start = (start << 32) +
1551   - le32_to_cpu(extent[i].ee_start_lo);
1552   - free(buf);
1553   - return fileblock + start;
  1586 + node = ext4fs_extent_cache_get(fileblock);
  1587 + if (!node) {
  1588 + printf("Extent Error\n");
  1589 + return -1;
1554 1590 }
1555 1591  
1556   - printf("Extent Error\n");
1557   - free(buf);
1558   - return -1;
  1592 + return fileblock - node->block + node->start;
1559 1593 }
1560 1594  
1561 1595 /* Direct blocks. */
fs/ext4/ext4_common.h
... ... @@ -57,6 +57,9 @@
57 57 int ext4fs_iterate_dir(struct ext2fs_node *dir, char *name,
58 58 struct ext2fs_node **fnode, int *ftype);
59 59  
  60 +int ext4fs_build_extent_cache(struct ext2_inode *inode);
  61 +void ext4fs_free_extent_cache(void);
  62 +
60 63 #if defined(CONFIG_EXT4_WRITE)
61 64 uint32_t ext4fs_div_roundup(uint32_t size, uint32_t n);
62 65 int ext4fs_checksum_update(unsigned int i);
... ... @@ -63,6 +63,14 @@
63 63 char *delayed_buf = NULL;
64 64 short status;
65 65  
  66 + if (le32_to_cpu(node->inode.flags) & EXT4_EXTENTS_FL) {
  67 + if (ext4fs_build_extent_cache(&node->inode)) {
  68 + printf("Error building extent cache!\n");
  69 + len = -1;
  70 + goto out_exit;
  71 + }
  72 + }
  73 +
66 74 /* Adjust len so it we can't read past the end of the file. */
67 75 if (len > filesize)
68 76 len = filesize;
... ... @@ -75,8 +83,10 @@
75 83 int blockend = blocksize;
76 84 int skipfirst = 0;
77 85 blknr = read_allocated_block(&(node->inode), i);
78   - if (blknr < 0)
79   - return -1;
  86 + if (blknr < 0) {
  87 + len = -1;
  88 + goto out_exit;
  89 + }
80 90  
81 91 blknr = blknr << log2_fs_blocksize;
82 92  
... ... @@ -106,8 +116,10 @@
106 116 delayed_skipfirst,
107 117 delayed_extent,
108 118 delayed_buf);
109   - if (status == 0)
110   - return -1;
  119 + if (status == 0) {
  120 + len = -1;
  121 + goto out_exit;
  122 + }
111 123 previous_block_number = blknr;
112 124 delayed_start = blknr;
113 125 delayed_extent = blockend;
... ... @@ -132,8 +144,10 @@
132 144 delayed_skipfirst,
133 145 delayed_extent,
134 146 delayed_buf);
135   - if (status == 0)
136   - return -1;
  147 + if (status == 0) {
  148 + len = -1;
  149 + goto out_exit;
  150 + }
137 151 previous_block_number = -1;
138 152 }
139 153 memset(buf, 0, blocksize - skipfirst);
140 154  
... ... @@ -145,10 +159,16 @@
145 159 status = ext4fs_devread(delayed_start,
146 160 delayed_skipfirst, delayed_extent,
147 161 delayed_buf);
148   - if (status == 0)
149   - return -1;
  162 + if (status == 0) {
  163 + len = -1;
  164 + goto out_exit;
  165 + }
150 166 previous_block_number = -1;
151 167 }
  168 +
  169 +
  170 +out_exit:
  171 + ext4fs_free_extent_cache();
152 172  
153 173 return len;
154 174 }