add patch return-eio-on-read-error-in-ext4_find_dentry
[ext4-patch-queue.git] / reserve-space-for-xattr-entries_names
blob541b763364d3ed56e4df4d279307a18f8a629dfd
1 ext4: reserve space for xattr entries/names
3 From: Tahsin Erdogan <tahsin@google.com>
5 New ea_inode feature allows putting large xattr values into external
6 inodes.  struct ext4_xattr_entry and the attribute name however have to
7 remain in the inode extra space or external attribute block.  Once that
8 space is exhausted, no further entries can be added.  Some of that space
9 could also be used by values that fit in there at the time of addition.
11 So, a single xattr entry whose value barely fits in the external block
12 could prevent further entries being added.
14 To mitigate the problem, this patch introduces a notion of reserved
15 space in the external attribute block that cannot be used by value data.
16 This reserve is enforced when ea_inode feature is enabled.  The amount
17 of reserve is arbitrarily chosen to be min(block_size/8, 1024).  The
18 table below shows how much space is reserved for each block size and the
19 guaranteed mininum number of entries that can be placed in the external
20 attribute block.
22 block size     reserved bytes  entries (name length = 16)
23  1k            128              3
24  2k            256              7
25  4k            512             15
26  8k            1024            31
27 16k            1024            31
28 32k            1024            31
29 64k            1024            31
31 Signed-off-by: Tahsin Erdogan <tahsin@google.com>
32 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
33 ---
34  fs/ext4/xattr.c | 20 ++++++++++++++++++++
35  1 file changed, 20 insertions(+)
37 diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
38 index a3f8bf4558f3..835b3dca5b65 100644
39 --- a/fs/ext4/xattr.c
40 +++ b/fs/ext4/xattr.c
41 @@ -1446,6 +1446,12 @@ static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
42         return 0;
43  }
45 +/*
46 + * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
47 + * feature is enabled.
48 + */
49 +#define EXT4_XATTR_BLOCK_RESERVE(inode)        min(i_blocksize(inode)/8, 1024U)
51  static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
52                                 struct ext4_xattr_search *s,
53                                 handle_t *handle, struct inode *inode)
54 @@ -1505,6 +1511,20 @@ static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
55                         ret = -ENOSPC;
56                         goto out;
57                 }
59 +               /*
60 +                * If storing the value in an external inode is an option,
61 +                * reserve space for xattr entries/names in the external
62 +                * attribute block so that a long value does not occupy the
63 +                * whole space and prevent futher entries being added.
64 +                */
65 +               if (ext4_has_feature_ea_inode(inode->i_sb) && new_size &&
66 +                   (s->end - s->base) == i_blocksize(inode) &&
67 +                   (min_offs + old_size - new_size) <
68 +                                       EXT4_XATTR_BLOCK_RESERVE(inode)) {
69 +                       ret = -ENOSPC;
70 +                       goto out;
71 +               }
72         }
74         /*
75 -- 
76 2.13.1.611.g7e3b11ae1-goog