1 Date: Fri, 8 Oct 2021 12:28:37 +0700
2 Subject: [PATCH] This patch implements the large EA support in ext4. If the
3 size of an EA value is larger than the blocksize, then the EA value would not
4 be saved in the external EA block, instead it would be saved in an external
5 EA inode. So, the patch also helps support a larger number of EAs.
9 fs/ext4/extents.c | 3 +-
10 fs/ext4/ialloc.c | 1 -
11 fs/ext4/indirect.c | 3 +-
12 fs/ext4/inline.c | 2 +-
13 fs/ext4/inode.c | 45 +++-
14 fs/ext4/xattr.c | 605 ++++++++++++++++++++++++++++++++++++++++++---
15 fs/ext4/xattr.h | 33 ++-
16 8 files changed, 645 insertions(+), 53 deletions(-)
18 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
19 index ba54a96..e28bcdb 100644
22 @@ -1617,6 +1617,7 @@ static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
23 EXT4_FEATURE_INCOMPAT_EXTENTS| \
24 EXT4_FEATURE_INCOMPAT_64BIT| \
25 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
26 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
27 EXT4_FEATURE_INCOMPAT_MMP | \
28 EXT4_FEATURE_INCOMPAT_DIRDATA| \
29 EXT4_FEATURE_INCOMPAT_INLINE_DATA)
30 @@ -2028,6 +2029,10 @@ struct mmpd_data {
31 # define ATTRIB_NORET __attribute__((noreturn))
32 # define NORET_AND noreturn,
34 +struct ext4_xattr_ino_array {
35 + unsigned int xia_count; /* # of used item in the array */
36 + unsigned int xia_inodes[0];
39 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
40 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
41 @@ -2233,6 +2238,7 @@ extern void ext4_set_inode_flags(struct inode *);
42 extern void ext4_get_inode_flags(struct ext4_inode_info *);
43 extern int ext4_alloc_da_blocks(struct inode *inode);
44 extern void ext4_set_aops(struct inode *inode);
45 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
46 extern int ext4_writepage_trans_blocks(struct inode *);
47 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
48 extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
49 diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
50 index d8434f2..8263aa4 100644
51 --- a/fs/ext4/extents.c
52 +++ b/fs/ext4/extents.c
53 @@ -2461,7 +2461,8 @@ int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
55 static inline int get_default_free_blocks_flags(struct inode *inode)
57 - if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
58 + if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
59 + ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
60 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
61 else if (ext4_should_journal_data(inode))
62 return EXT4_FREE_BLOCKS_FORGET;
63 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
64 index a739f71..472bb41 100644
65 --- a/fs/ext4/ialloc.c
66 +++ b/fs/ext4/ialloc.c
67 @@ -247,7 +247,6 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
68 * as writing the quota to disk may need the lock as well.
70 dquot_initialize(inode);
71 - ext4_xattr_delete_inode(handle, inode);
72 dquot_free_inode(inode);
75 diff --git a/fs/ext4/indirect.c b/fs/ext4/indirect.c
76 index 68163c3..b7cf6e0 100644
77 --- a/fs/ext4/indirect.c
78 +++ b/fs/ext4/indirect.c
79 @@ -959,7 +959,8 @@ static int ext4_clear_blocks(handle_t *handle, struct inode *inode,
80 int flags = EXT4_FREE_BLOCKS_VALIDATED;
83 - if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
84 + if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
85 + ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
86 flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
87 else if (ext4_should_journal_data(inode))
88 flags |= EXT4_FREE_BLOCKS_FORGET;
89 diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
90 index 0e7433b..b1295be 100644
91 --- a/fs/ext4/inline.c
92 +++ b/fs/ext4/inline.c
93 @@ -59,7 +59,7 @@ static int get_max_inline_xattr_value_size(struct inode *inode,
95 /* Compute min_offs. */
96 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
97 - if (!entry->e_value_block && entry->e_value_size) {
98 + if (!entry->e_value_inum && entry->e_value_size) {
99 size_t offs = le16_to_cpu(entry->e_value_offs);
102 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
103 index 6c6ac63..2086792 100644
104 --- a/fs/ext4/inode.c
105 +++ b/fs/ext4/inode.c
106 @@ -136,8 +136,6 @@ static void ext4_invalidatepage(struct page *page, unsigned int offset,
107 unsigned int length);
108 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
109 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
110 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
114 * Test whether an inode is a fast symlink.
115 @@ -186,6 +184,8 @@ void ext4_evict_inode(struct inode *inode)
119 + int extra_credits = 3;
120 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
122 trace_ext4_evict_inode(inode);
124 @@ -235,8 +235,8 @@ void ext4_evict_inode(struct inode *inode)
125 * protection against it
127 sb_start_intwrite(inode->i_sb);
128 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
129 - ext4_blocks_for_truncate(inode)+3);
131 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
132 if (IS_ERR(handle)) {
133 ext4_std_error(inode->i_sb, PTR_ERR(handle));
135 @@ -251,6 +251,32 @@ void ext4_evict_inode(struct inode *inode)
138 ext4_handle_sync(handle);
140 + /* Delete xattr inode before deleting the main inode. */
141 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
143 + ext4_warning(inode->i_sb,
144 + "couldn't delete inode's xattr (err %d)", err);
148 + if (!IS_NOQUOTA(inode))
149 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
151 + if (!ext4_handle_has_enough_credits(handle,
152 + ext4_blocks_for_truncate(inode) + extra_credits)) {
153 + err = ext4_journal_extend(handle,
154 + ext4_blocks_for_truncate(inode) + extra_credits);
156 + err = ext4_journal_restart(handle,
157 + ext4_blocks_for_truncate(inode) + extra_credits);
159 + ext4_warning(inode->i_sb,
160 + "couldn't extend journal (err %d)", err);
166 err = ext4_mark_inode_dirty(handle, inode);
168 @@ -267,10 +293,10 @@ void ext4_evict_inode(struct inode *inode)
169 * enough credits left in the handle to remove the inode from
170 * the orphan list and set the dtime field.
172 - if (!ext4_handle_has_enough_credits(handle, 3)) {
173 - err = ext4_journal_extend(handle, 3);
174 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
175 + err = ext4_journal_extend(handle, extra_credits);
177 - err = ext4_journal_restart(handle, 3);
178 + err = ext4_journal_restart(handle, extra_credits);
180 ext4_warning(inode->i_sb,
181 "couldn't extend journal (err %d)", err);
182 @@ -307,6 +333,9 @@ void ext4_evict_inode(struct inode *inode)
183 ext4_free_inode(handle, inode);
184 ext4_journal_stop(handle);
185 sb_end_intwrite(inode->i_sb);
187 + if (lea_ino_array != NULL)
188 + ext4_xattr_inode_array_free(inode, lea_ino_array);
191 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
192 @@ -5132,7 +5161,7 @@ static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
194 * Also account for superblock, inode, quota and xattr blocks
196 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
197 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
200 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
201 diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
202 index c10e37f..2288410 100644
203 --- a/fs/ext4/xattr.c
204 +++ b/fs/ext4/xattr.c
205 @@ -201,6 +201,7 @@ ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
207 while (!IS_LAST_ENTRY(entry)) {
208 if (entry->e_value_size != 0 &&
209 + entry->e_value_inum == 0 &&
210 (value_start + le16_to_cpu(entry->e_value_offs) <
211 (void *)e + sizeof(__u32) ||
212 value_start + le16_to_cpu(entry->e_value_offs) +
213 @@ -233,19 +234,26 @@ ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
217 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
218 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
219 + struct inode *inode)
221 size_t value_size = le32_to_cpu(entry->e_value_size);
223 - if (entry->e_value_block != 0 || value_size > size ||
224 + if (!entry->e_value_inum &&
225 le16_to_cpu(entry->e_value_offs) + value_size > size)
227 + if (entry->e_value_inum &&
228 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
229 + le32_to_cpu(entry->e_value_inum) >
230 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
236 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
237 - const char *name, size_t size, int sorted)
238 + const char *name, size_t size, int sorted,
239 + struct inode *inode)
241 struct ext4_xattr_entry *entry;
243 @@ -265,11 +273,109 @@ ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
247 - if (!cmp && ext4_xattr_check_entry(entry, size))
248 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
250 return cmp ? -ENODATA : 0;
254 + * Read the EA value from an inode.
256 +static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
258 + unsigned long block = 0;
259 + struct buffer_head *bh = NULL;
260 + int err, blocksize;
261 + size_t csize, ret_size = 0;
266 + blocksize = ea_inode->i_sb->s_blocksize;
268 + while (ret_size < *size) {
269 + csize = (*size - ret_size) > blocksize ? blocksize :
271 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
276 + memcpy(buf, bh->b_data, csize);
290 + * Fetch the xattr inode from disk.
292 + * The xattr inode stores the parent inode number and generation so that
293 + * the kernel and e2fsck can verify the xattr inode is valid upon access.
295 +struct inode *ext4_xattr_inode_iget(struct inode *parent,
296 + unsigned long ea_ino, int *err)
298 + struct inode *ea_inode = NULL;
300 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
301 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
302 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
303 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
304 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
305 + *err = rc != 0 ? rc : -EIO;
309 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
310 + ea_inode->i_generation != parent->i_generation) {
311 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
312 + "to parent invalid.", ea_ino);
317 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
318 + ext4_error(parent->i_sb, "EA inode %lu does not have "
319 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
333 + * Read the value from the EA inode.
335 +static int ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino,
336 + void *buffer, size_t *size)
338 + struct inode *ea_inode = NULL;
341 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
345 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
352 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
353 void *buffer, size_t buffer_size)
354 @@ -301,7 +407,8 @@ bad_block:
356 ext4_xattr_cache_insert(bh);
358 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
359 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
364 @@ -311,8 +418,16 @@ bad_block:
366 if (size > buffer_size)
368 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
370 + if (entry->e_value_inum) {
371 + error = ext4_xattr_inode_get(inode,
372 + le32_to_cpu(entry->e_value_inum),
377 + memcpy(buffer, bh->b_data +
378 + le16_to_cpu(entry->e_value_offs), size);
383 @@ -346,7 +461,7 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
386 error = ext4_xattr_find_entry(&entry, name_index, name,
387 - end - (void *)entry, 0);
388 + end - (void *)entry, 0, inode);
391 size = le32_to_cpu(entry->e_value_size);
392 @@ -354,8 +469,16 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
394 if (size > buffer_size)
396 - memcpy(buffer, (void *)IFIRST(header) +
397 - le16_to_cpu(entry->e_value_offs), size);
398 + if (entry->e_value_inum) {
399 + error = ext4_xattr_inode_get(inode,
400 + le32_to_cpu(entry->e_value_inum),
405 + memcpy(buffer, (void *)IFIRST(header) +
406 + le16_to_cpu(entry->e_value_offs), size);
411 @@ -600,7 +723,7 @@ static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
412 size_t *min_offs, void *base, int *total)
414 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
415 - if (!last->e_value_block && last->e_value_size) {
416 + if (!last->e_value_inum && last->e_value_size) {
417 size_t offs = le16_to_cpu(last->e_value_offs);
418 if (offs < *min_offs)
420 @@ -611,11 +734,193 @@ static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
421 return (*min_offs - ((void *)last - base) - sizeof(__u32));
425 + * Write the value of the EA in an inode.
428 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
429 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
430 + const void *buf, int bufsize)
432 + struct buffer_head *bh = NULL;
433 + unsigned long block = 0;
434 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
435 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
436 + int csize, wsize = 0;
441 + while (ret >= 0 && ret < max_blocks) {
442 + struct ext4_map_blocks map;
443 + map.m_lblk = block += ret;
444 + map.m_len = max_blocks -= ret;
446 + ret = ext4_map_blocks(handle, ea_inode, &map,
447 + EXT4_GET_BLOCKS_CREATE);
449 + ext4_mark_inode_dirty(handle, ea_inode);
450 + if (ret == -ENOSPC &&
451 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
463 + while (wsize < bufsize) {
466 + csize = (bufsize - wsize) > blocksize ? blocksize :
468 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
471 + ret = ext4_journal_get_write_access(handle, bh);
475 + memcpy(bh->b_data, buf, csize);
476 + set_buffer_uptodate(bh);
477 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
484 + mutex_lock(&ea_inode->i_mutex);
485 + i_size_write(ea_inode, wsize);
486 + ext4_update_i_disksize(ea_inode, wsize);
487 + mutex_unlock(&ea_inode->i_mutex);
489 + ext4_mark_inode_dirty(handle, ea_inode);
497 +static void ext4_xattr_inode_set_ref(struct inode *ea_inode, __u64 ref_count)
499 + ea_inode->i_ctime.tv_sec = (__u32)(ref_count >> 32);
500 + ea_inode->i_version = (__u32)ref_count;
503 +static void ext4_xattr_inode_set_hash(struct inode *ea_inode, __u32 hash)
505 + ea_inode->i_atime.tv_sec = hash;
509 + * Create an inode to store the value of a large EA.
511 +static struct inode *
512 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode, __u32 hash)
514 + struct inode *ea_inode = NULL;
517 + * Let the next inode be the goal, so we try and allocate the EA inode
518 + * in the same group, or nearby one.
520 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
521 + S_IFREG|0600, NULL, inode->i_ino + 1, NULL);
523 + if (!IS_ERR(ea_inode)) {
524 + ea_inode->i_op = &ext4_file_inode_operations;
525 + ea_inode->i_fop = &ext4_file_operations.kabi_fops;
526 + ext4_set_aops(ea_inode);
527 + ea_inode->i_generation = inode->i_generation;
528 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
531 + * A back-pointer from EA inode to parent inode will be useful
534 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
535 + unlock_new_inode(ea_inode);
537 + ext4_xattr_inode_set_ref(ea_inode, 1);
538 + ext4_xattr_inode_set_hash(ea_inode, hash);
545 + * Unlink the inode storing the value of the EA.
548 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
550 + struct inode *ea_inode = NULL;
553 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
557 + clear_nlink(ea_inode);
564 +ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
566 + if (ext4_has_metadata_csum(sbi->s_sb))
567 + return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
572 + * Add value of the EA in an inode.
575 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
576 + const void *value, size_t value_len)
578 + struct inode *ea_inode = NULL;
582 + /* Create an inode for the EA value */
583 + hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
584 + ea_inode = ext4_xattr_inode_create(handle, inode, hash);
585 + if (IS_ERR(ea_inode))
588 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
590 + clear_nlink(ea_inode);
592 + *ea_ino = ea_inode->i_ino;
600 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
601 + handle_t *handle, struct inode *inode)
603 struct ext4_xattr_entry *last, *next;
604 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
605 + int in_inode = i->in_inode;
607 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
608 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
609 + (EXT4_XATTR_SIZE(i->value_len) >
610 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
613 /* Compute min_offs and last. */
615 @@ -624,7 +929,7 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
616 if ((void *)next >= s->end) {
619 - if (!last->e_value_block && last->e_value_size) {
620 + if (!last->e_value_inum && last->e_value_size) {
621 size_t offs = le16_to_cpu(last->e_value_offs);
624 @@ -632,15 +937,21 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
626 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
628 - if (!s->here->e_value_block && s->here->e_value_size) {
630 + !s->here->e_value_inum && s->here->e_value_size) {
631 size_t size = le32_to_cpu(s->here->e_value_size);
632 free += EXT4_XATTR_SIZE(size);
634 free += EXT4_XATTR_LEN(name_len);
637 - if (free < EXT4_XATTR_LEN(name_len) +
638 - EXT4_XATTR_SIZE(i->value_len))
639 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
644 + if (free < value_len ||
645 + free < EXT4_XATTR_LEN(name_len) + value_len)
649 @@ -654,7 +965,8 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
650 s->here->e_name_len = name_len;
651 memcpy(s->here->e_name, i->name, name_len);
653 - if (!s->here->e_value_block && s->here->e_value_size) {
654 + if (!s->here->e_value_inum && s->here->e_value_size &&
655 + s->here->e_value_offs > 0) {
656 void *first_val = s->base + min_offs;
657 size_t offs = le16_to_cpu(s->here->e_value_offs);
658 void *val = s->base + offs;
659 @@ -688,13 +1000,18 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
661 while (!IS_LAST_ENTRY(last)) {
662 size_t o = le16_to_cpu(last->e_value_offs);
663 - if (!last->e_value_block &&
664 + if (!last->e_value_inum &&
665 last->e_value_size && o < offs)
667 cpu_to_le16(o + size);
668 last = EXT4_XATTR_NEXT(last);
671 + if (s->here->e_value_inum) {
672 + ext4_xattr_inode_unlink(inode,
673 + le32_to_cpu(s->here->e_value_inum));
674 + s->here->e_value_inum = 0;
677 /* Remove the old name. */
678 size_t size = EXT4_XATTR_LEN(name_len);
679 @@ -708,10 +1025,17 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
681 /* Insert the new value. */
682 s->here->e_value_size = cpu_to_le32(i->value_len);
683 - if (i->value_len) {
685 + unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
686 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
688 + s->here->e_value_inum = cpu_to_le32(ea_ino);
689 + s->here->e_value_offs = 0;
690 + } else if (i->value_len) {
691 size_t size = EXT4_XATTR_SIZE(i->value_len);
692 void *val = s->base + min_offs - size;
693 s->here->e_value_offs = cpu_to_le16(min_offs - size);
694 + s->here->e_value_inum = 0;
695 if (i->value == EXT4_ZERO_XATTR_VALUE) {
696 memset(val, 0, size);
698 @@ -761,7 +1085,7 @@ ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
699 bs->s.end = bs->bh->b_data + bs->bh->b_size;
700 bs->s.here = bs->s.first;
701 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
702 - i->name, bs->bh->b_size, 1);
703 + i->name, bs->bh->b_size, 1, inode);
704 if (error && error != -ENODATA)
706 bs->s.not_found = error;
707 @@ -785,8 +1109,6 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
709 #define header(x) ((struct ext4_xattr_header *)(x))
711 - if (i->value && i->value_len > sb->s_blocksize)
714 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
716 @@ -802,7 +1124,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
719 ea_bdebug(bs->bh, "modifying in-place");
720 - error = ext4_xattr_set_entry(i, s);
721 + error = ext4_xattr_set_entry(i, s, handle, inode);
723 if (!IS_LAST_ENTRY(s->first))
724 ext4_xattr_rehash(header(s->base),
725 @@ -853,7 +1175,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
726 s->end = s->base + sb->s_blocksize;
729 - error = ext4_xattr_set_entry(i, s);
730 + error = ext4_xattr_set_entry(i, s, handle, inode);
734 @@ -997,7 +1319,7 @@ int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
735 /* Find the named attribute. */
736 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
738 - (void *)is->s.base, 0);
739 + (void *)is->s.base, 0, inode);
740 if (error && error != -ENODATA)
742 is->s.not_found = error;
743 @@ -1015,7 +1337,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
745 if (EXT4_I(inode)->i_extra_isize == 0)
747 - error = ext4_xattr_set_entry(i, s);
748 + error = ext4_xattr_set_entry(i, s, handle, inode);
751 header = IHDR(inode, ext4_raw_inode(&is->iloc));
752 @@ -1039,7 +1361,7 @@ static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
754 if (EXT4_I(inode)->i_extra_isize == 0)
756 - error = ext4_xattr_set_entry(i, s);
757 + error = ext4_xattr_set_entry(i, s, handle, inode);
760 header = IHDR(inode, ext4_raw_inode(&is->iloc));
761 @@ -1075,7 +1397,7 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
764 .value_len = value_len,
768 struct ext4_xattr_ibody_find is = {
769 .s = { .not_found = -ENODATA, },
770 @@ -1140,6 +1462,15 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
773 error = ext4_xattr_block_set(handle, inode, &i, &bs);
774 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
775 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
776 + error == -ENOSPC) {
777 + /* xattr not fit to block, store at external
780 + error = ext4_xattr_ibody_set(handle, inode,
785 if (!is.s.not_found) {
786 @@ -1186,9 +1517,22 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
787 const void *value, size_t value_len, int flags)
790 + struct super_block *sb = inode->i_sb;
791 int error, retries = 0;
792 int credits = ext4_jbd2_credits_xattr(inode);
794 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
795 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
796 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
797 + sb->s_blocksize_bits;
799 + /* For new inode */
800 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
802 + /* For data blocks of EA inode */
803 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
807 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
808 if (IS_ERR(handle)) {
809 @@ -1200,7 +1544,7 @@ retry:
810 value, value_len, flags);
811 error2 = ext4_journal_stop(handle);
812 if (error == -ENOSPC &&
813 - ext4_should_retry_alloc(inode->i_sb, &retries))
814 + ext4_should_retry_alloc(sb, &retries))
818 @@ -1222,7 +1566,7 @@ static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
820 /* Adjust the value offsets of the entries */
821 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
822 - if (!last->e_value_block && last->e_value_size) {
823 + if (!last->e_value_inum && last->e_value_size) {
824 new_offs = le16_to_cpu(last->e_value_offs) +
826 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
827 @@ -1469,21 +1813,140 @@ cleanup:
831 +#define EIA_INCR 16 /* must be 2^n */
832 +#define EIA_MASK (EIA_INCR - 1)
833 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
834 + * If @lea_ino_array is new or full it will be grown and the old
835 + * contents copied over.
838 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
840 + if (*lea_ino_array == NULL) {
842 + * Start with 15 inodes, so it fits into a power-of-two size.
843 + * If *lea_ino_array is NULL, this is essentially offsetof()
846 + kmalloc(offsetof(struct ext4_xattr_ino_array,
847 + xia_inodes[EIA_MASK]),
849 + if (*lea_ino_array == NULL)
851 + (*lea_ino_array)->xia_count = 0;
852 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
853 + /* expand the array once all 15 + n * 16 slots are full */
854 + struct ext4_xattr_ino_array *new_array = NULL;
855 + int count = (*lea_ino_array)->xia_count;
857 + /* if new_array is NULL, this is essentially offsetof() */
858 + new_array = kmalloc(
859 + offsetof(struct ext4_xattr_ino_array,
860 + xia_inodes[count + EIA_INCR]),
862 + if (new_array == NULL)
864 + memcpy(new_array, *lea_ino_array,
865 + offsetof(struct ext4_xattr_ino_array,
866 + xia_inodes[count]));
867 + kfree(*lea_ino_array);
868 + *lea_ino_array = new_array;
870 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
875 + * Add xattr inode to orphan list
878 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
879 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
881 + struct inode *ea_inode = NULL;
882 + int idx = 0, error = 0;
884 + if (lea_ino_array == NULL)
887 + for (; idx < lea_ino_array->xia_count; ++idx) {
888 + if (!ext4_handle_has_enough_credits(handle, credits)) {
889 + error = ext4_journal_extend(handle, credits);
891 + error = ext4_journal_restart(handle, credits);
894 + ext4_warning(inode->i_sb,
895 + "couldn't extend journal "
896 + "(err %d)", error);
900 + ea_inode = ext4_xattr_inode_iget(inode,
901 + lea_ino_array->xia_inodes[idx], &error);
904 + ext4_orphan_add(handle, ea_inode);
905 + /* the inode's i_count will be released by caller */
912 * ext4_xattr_delete_inode()
914 - * Free extended attribute resources associated with this inode. This
915 + * Free extended attribute resources associated with this inode. Traverse
916 + * all entries and unlink any xattr inodes associated with this inode. This
917 * is called immediately before an inode is freed. We have exclusive
918 - * access to the inode.
919 + * access to the inode. If an orphan inode is deleted it will also delete any
920 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
921 + * to ensure they belong to the parent inode and were not deleted already.
924 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
926 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
927 + struct ext4_xattr_ino_array **lea_ino_array)
929 struct buffer_head *bh = NULL;
930 + struct ext4_xattr_ibody_header *header;
931 + struct ext4_inode *raw_inode;
932 + struct ext4_iloc iloc = { .bh = NULL };
933 + struct ext4_xattr_entry *entry;
934 + int credits = 3, error = 0;
936 - if (!EXT4_I(inode)->i_file_acl)
937 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
938 + goto delete_external_ea;
940 + error = ext4_get_inode_loc(inode, &iloc);
943 + raw_inode = ext4_raw_inode(&iloc);
944 + header = IHDR(inode, raw_inode);
945 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
946 + entry = EXT4_XATTR_NEXT(entry)) {
947 + if (!entry->e_value_inum)
949 + if (ext4_expand_ino_array(lea_ino_array,
950 + entry->e_value_inum) != 0)
953 + error = ext4_journal_get_write_access(handle, iloc.bh);
956 + entry->e_value_inum = 0;
957 + entry->e_value_size = 0;
958 + error = ext4_handle_dirty_metadata(handle, inode, iloc.bh);
964 + if (!EXT4_I(inode)->i_file_acl) {
965 + /* add xattr inode to orphan list */
966 + ext4_xattr_inode_orphan_add(handle, inode, credits,
970 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
972 EXT4_ERROR_INODE(inode, "block %llu read error",
973 @@ -1496,11 +1959,78 @@ ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
974 EXT4_I(inode)->i_file_acl);
978 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
979 + entry = EXT4_XATTR_NEXT(entry)) {
980 + if (!entry->e_value_inum)
982 + if (ext4_expand_ino_array(lea_ino_array,
983 + entry->e_value_inum) != 0)
986 + error = ext4_journal_get_write_access(handle, bh);
989 + entry->e_value_inum = 0;
990 + entry->e_value_size = 0;
991 + error = ext4_handle_dirty_metadata(handle, inode, bh);
996 + /* add xattr inode to orphan list */
997 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
1002 + if (!IS_NOQUOTA(inode))
1003 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
1005 + if (!ext4_handle_has_enough_credits(handle, credits)) {
1006 + error = ext4_journal_extend(handle, credits);
1008 + error = ext4_journal_restart(handle, credits);
1010 + ext4_warning(inode->i_sb,
1011 + "couldn't extend journal (err %d)", error);
1016 ext4_xattr_release_block(handle, inode, bh);
1017 EXT4_I(inode)->i_file_acl = 0;
1027 +ext4_xattr_inode_array_free(struct inode *inode,
1028 + struct ext4_xattr_ino_array *lea_ino_array)
1030 + struct inode *ea_inode = NULL;
1034 + if (lea_ino_array == NULL)
1037 + for (; idx < lea_ino_array->xia_count; ++idx) {
1038 + ea_inode = ext4_xattr_inode_iget(inode,
1039 + lea_ino_array->xia_inodes[idx], &err);
1042 + /* for inode's i_count get from ext4_xattr_delete_inode */
1043 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
1045 + clear_nlink(ea_inode);
1048 + kfree(lea_ino_array);
1052 @@ -1570,10 +2100,9 @@ ext4_xattr_cmp(struct ext4_xattr_header *header1,
1053 entry1->e_name_index != entry2->e_name_index ||
1054 entry1->e_name_len != entry2->e_name_len ||
1055 entry1->e_value_size != entry2->e_value_size ||
1056 + entry1->e_value_inum != entry2->e_value_inum ||
1057 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1059 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1061 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1062 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1063 le32_to_cpu(entry1->e_value_size)))
1064 @@ -1657,7 +2186,7 @@ static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1068 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1069 + if (!entry->e_value_inum && entry->e_value_size) {
1070 __le32 *value = (__le32 *)((char *)header +
1071 le16_to_cpu(entry->e_value_offs));
1072 for (n = (le32_to_cpu(entry->e_value_size) +
1073 diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
1074 index c767dbd..8312412 100644
1075 --- a/fs/ext4/xattr.h
1076 +++ b/fs/ext4/xattr.h
1077 @@ -42,7 +42,7 @@ struct ext4_xattr_entry {
1078 __u8 e_name_len; /* length of name */
1079 __u8 e_name_index; /* attribute name index */
1080 __le16 e_value_offs; /* offset in disk block of value */
1081 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1082 + __le32 e_value_inum; /* inode in which the value is stored */
1083 __le32 e_value_size; /* size of attribute value */
1084 __le32 e_hash; /* hash value of name and value */
1085 char e_name[0]; /* attribute name */
1086 @@ -67,6 +67,26 @@ struct ext4_xattr_entry {
1087 EXT4_I(inode)->i_extra_isize))
1088 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1091 + * Link EA inode back to parent one using i_mtime field.
1092 + * Extra integer type conversion added to ignore higher
1093 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1095 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1097 + (inode)->i_mtime.tv_sec = inum; \
1100 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1101 + ((__u32)(inode)->i_mtime.tv_sec)
1104 + * The minimum size of EA value when you start storing it in an external inode
1105 + * size of block - size of header - size of 1 entry - 4 null bytes
1107 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1108 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1110 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1111 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1112 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1113 @@ -75,10 +95,11 @@ struct ext4_xattr_entry {
1114 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1116 struct ext4_xattr_info {
1125 struct ext4_xattr_search {
1126 @@ -106,7 +127,13 @@ extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
1127 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1128 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1130 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1131 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1133 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1134 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1135 + struct ext4_xattr_ino_array **array);
1136 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1137 + struct ext4_xattr_ino_array *array);
1138 extern void ext4_xattr_put_super(struct super_block *);
1140 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,