1 ext4: xattr-in-inode support
3 From: Andreas Dilger <andreas.dilger@intel.com>
5 Large xattr support is implemented for EXT4_FEATURE_INCOMPAT_EA_INODE.
7 If the size of an xattr value is larger than will fit in a single
8 external block, then the xattr value will be saved into the body
9 of an external xattr inode.
11 The also helps support a larger number of xattr, since only the headers
12 will be stored in the in-inode space or the single external block.
14 The inode is referenced from the xattr header via "e_value_inum",
15 which was formerly "e_value_block", but that field was never used.
16 The e_value_size still contains the xattr size so that listing
17 xattrs does not need to look up the inode if the data is not accessed.
19 struct ext4_xattr_entry {
20 __u8 e_name_len; /* length of name */
21 __u8 e_name_index; /* attribute name index */
22 __le16 e_value_offs; /* offset in disk block of value */
23 __le32 e_value_inum; /* inode in which value is stored */
24 __le32 e_value_size; /* size of attribute value */
25 __le32 e_hash; /* hash value of name and value */
26 char e_name[0]; /* attribute name */
29 The xattr inode is marked with the EXT4_EA_INODE_FL flag and also
30 holds a back-reference to the owning inode in its i_mtime field,
31 allowing the ext4/e2fsck to verify the correct inode is accessed.
33 Lustre-Jira: https://jira.hpdd.intel.com/browse/LU-80
34 Lustre-bugzilla: https://bugzilla.lustre.org/show_bug.cgi?id=4424
35 Signed-off-by: Kalpak Shah <kalpak.shah@sun.com>
36 Signed-off-by: James Simmons <uja.ornl@gmail.com>
37 Signed-off-by: Andreas Dilger <andreas.dilger@intel.com>
38 Signed-off-by: Tahsin Erdogan <tahsin@google.com>
39 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
41 fs/ext4/ext4.h | 12 ++
42 fs/ext4/ialloc.c | 1 -
43 fs/ext4/inline.c | 2 +-
44 fs/ext4/inode.c | 49 ++++-
45 fs/ext4/xattr.c | 565 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
46 fs/ext4/xattr.h | 33 +++-
47 6 files changed, 606 insertions(+), 56 deletions(-)
49 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
50 index 32191548abed..24ef56b4572f 100644
53 @@ -1797,6 +1797,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(encrypt, ENCRYPT)
54 EXT4_FEATURE_INCOMPAT_EXTENTS| \
55 EXT4_FEATURE_INCOMPAT_64BIT| \
56 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
57 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
58 EXT4_FEATURE_INCOMPAT_MMP | \
59 EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
60 EXT4_FEATURE_INCOMPAT_ENCRYPT | \
61 @@ -2219,6 +2220,12 @@ struct mmpd_data {
63 #define EXT4_MMP_MAX_CHECK_INTERVAL 300UL
66 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
67 + * This limit is arbitrary, but is reasonable for the xattr API.
69 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE (1024 * 1024)
74 @@ -2231,6 +2238,10 @@ struct mmpd_data {
75 # define ATTRIB_NORET __attribute__((noreturn))
76 # define NORET_AND noreturn,
78 +struct ext4_xattr_ino_array {
79 + unsigned int xia_count; /* # of used item in the array */
80 + unsigned int xia_inodes[0];
83 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
84 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
85 @@ -2478,6 +2489,7 @@ extern int ext4_truncate_restart_trans(handle_t *, struct inode *, int nblocks);
86 extern void ext4_set_inode_flags(struct inode *);
87 extern int ext4_alloc_da_blocks(struct inode *inode);
88 extern void ext4_set_aops(struct inode *inode);
89 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
90 extern int ext4_writepage_trans_blocks(struct inode *);
91 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
92 extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
93 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
94 index 98ac2f1f23b3..e2eb3cc06820 100644
95 --- a/fs/ext4/ialloc.c
96 +++ b/fs/ext4/ialloc.c
97 @@ -294,7 +294,6 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
98 * as writing the quota to disk may need the lock as well.
100 dquot_initialize(inode);
101 - ext4_xattr_delete_inode(handle, inode);
102 dquot_free_inode(inode);
105 diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
106 index 8d141c0c8ff9..28c5c3abddb3 100644
107 --- a/fs/ext4/inline.c
108 +++ b/fs/ext4/inline.c
109 @@ -61,7 +61,7 @@ static int get_max_inline_xattr_value_size(struct inode *inode,
111 /* Compute min_offs. */
112 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
113 - if (!entry->e_value_block && entry->e_value_size) {
114 + if (!entry->e_value_inum && entry->e_value_size) {
115 size_t offs = le16_to_cpu(entry->e_value_offs);
118 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
119 index 5cf82d03968c..e5535e5b3dc5 100644
120 --- a/fs/ext4/inode.c
121 +++ b/fs/ext4/inode.c
122 @@ -139,8 +139,6 @@ static void ext4_invalidatepage(struct page *page, unsigned int offset,
123 unsigned int length);
124 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
125 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
126 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
130 * Test whether an inode is a fast symlink.
131 @@ -189,6 +187,8 @@ void ext4_evict_inode(struct inode *inode)
135 + int extra_credits = 3;
136 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
138 trace_ext4_evict_inode(inode);
140 @@ -238,8 +238,8 @@ void ext4_evict_inode(struct inode *inode)
141 * protection against it
143 sb_start_intwrite(inode->i_sb);
144 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
145 - ext4_blocks_for_truncate(inode)+3);
147 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
148 if (IS_ERR(handle)) {
149 ext4_std_error(inode->i_sb, PTR_ERR(handle));
151 @@ -251,9 +251,36 @@ void ext4_evict_inode(struct inode *inode)
152 sb_end_intwrite(inode->i_sb);
157 ext4_handle_sync(handle);
160 + * Delete xattr inode before deleting the main inode.
162 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
164 + ext4_warning(inode->i_sb,
165 + "couldn't delete inode's xattr (err %d)", err);
169 + if (!IS_NOQUOTA(inode))
170 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
172 + if (!ext4_handle_has_enough_credits(handle,
173 + ext4_blocks_for_truncate(inode) + extra_credits)) {
174 + err = ext4_journal_extend(handle,
175 + ext4_blocks_for_truncate(inode) + extra_credits);
177 + err = ext4_journal_restart(handle,
178 + ext4_blocks_for_truncate(inode) + extra_credits);
180 + ext4_warning(inode->i_sb,
181 + "couldn't extend journal (err %d)", err);
187 err = ext4_mark_inode_dirty(handle, inode);
189 @@ -277,10 +304,10 @@ void ext4_evict_inode(struct inode *inode)
190 * enough credits left in the handle to remove the inode from
191 * the orphan list and set the dtime field.
193 - if (!ext4_handle_has_enough_credits(handle, 3)) {
194 - err = ext4_journal_extend(handle, 3);
195 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
196 + err = ext4_journal_extend(handle, extra_credits);
198 - err = ext4_journal_restart(handle, 3);
199 + err = ext4_journal_restart(handle, extra_credits);
201 ext4_warning(inode->i_sb,
202 "couldn't extend journal (err %d)", err);
203 @@ -315,8 +342,12 @@ void ext4_evict_inode(struct inode *inode)
204 ext4_clear_inode(inode);
206 ext4_free_inode(handle, inode);
208 ext4_journal_stop(handle);
209 sb_end_intwrite(inode->i_sb);
211 + if (lea_ino_array != NULL)
212 + ext4_xattr_inode_array_free(inode, lea_ino_array);
215 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
216 @@ -5504,7 +5535,7 @@ static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
218 * Also account for superblock, inode, quota and xattr blocks
220 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
221 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
224 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
225 diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
226 index 5d3c2536641c..444be5c7a1d5 100644
227 --- a/fs/ext4/xattr.c
228 +++ b/fs/ext4/xattr.c
229 @@ -177,9 +177,8 @@ ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
231 /* Check the values */
232 while (!IS_LAST_ENTRY(entry)) {
233 - if (entry->e_value_block != 0)
234 - return -EFSCORRUPTED;
235 - if (entry->e_value_size != 0) {
236 + if (entry->e_value_size != 0 &&
237 + entry->e_value_inum == 0) {
238 u16 offs = le16_to_cpu(entry->e_value_offs);
239 u32 size = le32_to_cpu(entry->e_value_size);
241 @@ -269,6 +268,99 @@ ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
242 return cmp ? -ENODATA : 0;
246 + * Read the EA value from an inode.
249 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
251 + unsigned long block = 0;
252 + struct buffer_head *bh = NULL;
254 + size_t csize, ret_size = 0;
259 + blocksize = ea_inode->i_sb->s_blocksize;
261 + while (ret_size < *size) {
262 + csize = (*size - ret_size) > blocksize ? blocksize :
264 + bh = ext4_bread(NULL, ea_inode, block, 0);
267 + return PTR_ERR(bh);
269 + memcpy(buf, bh->b_data, csize);
282 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
284 + struct inode *ea_inode = NULL;
286 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
287 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
288 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
289 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
290 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
291 + *err = rc != 0 ? rc : -EIO;
295 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
296 + ea_inode->i_generation != parent->i_generation) {
297 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
298 + "to parent invalid.", ea_ino);
303 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
304 + ext4_error(parent->i_sb, "EA inode %lu does not have "
305 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
319 + * Read the value from the EA inode.
322 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
325 + struct inode *ea_inode = NULL;
328 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
332 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
339 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
340 void *buffer, size_t buffer_size)
341 @@ -308,8 +400,16 @@ ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
343 if (size > buffer_size)
345 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
347 + if (entry->e_value_inum) {
348 + error = ext4_xattr_inode_get(inode,
349 + le32_to_cpu(entry->e_value_inum),
354 + memcpy(buffer, bh->b_data +
355 + le16_to_cpu(entry->e_value_offs), size);
360 @@ -350,8 +450,16 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
362 if (size > buffer_size)
364 - memcpy(buffer, (void *)IFIRST(header) +
365 - le16_to_cpu(entry->e_value_offs), size);
366 + if (entry->e_value_inum) {
367 + error = ext4_xattr_inode_get(inode,
368 + le32_to_cpu(entry->e_value_inum),
373 + memcpy(buffer, (void *)IFIRST(header) +
374 + le16_to_cpu(entry->e_value_offs), size);
379 @@ -620,7 +728,7 @@ static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
380 size_t *min_offs, void *base, int *total)
382 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
383 - if (last->e_value_size) {
384 + if (!last->e_value_inum && last->e_value_size) {
385 size_t offs = le16_to_cpu(last->e_value_offs);
386 if (offs < *min_offs)
388 @@ -631,16 +739,173 @@ static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
389 return (*min_offs - ((void *)last - base) - sizeof(__u32));
393 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
395 + * Write the value of the EA in an inode.
397 +static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
398 + const void *buf, int bufsize)
400 + struct buffer_head *bh = NULL;
401 + unsigned long block = 0;
402 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
403 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
404 + int csize, wsize = 0;
409 + while (ret >= 0 && ret < max_blocks) {
410 + struct ext4_map_blocks map;
411 + map.m_lblk = block += ret;
412 + map.m_len = max_blocks -= ret;
414 + ret = ext4_map_blocks(handle, ea_inode, &map,
415 + EXT4_GET_BLOCKS_CREATE);
417 + ext4_mark_inode_dirty(handle, ea_inode);
418 + if (ret == -ENOSPC &&
419 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
431 + while (wsize < bufsize) {
434 + csize = (bufsize - wsize) > blocksize ? blocksize :
436 + bh = ext4_getblk(handle, ea_inode, block, 0);
441 + ret = ext4_journal_get_write_access(handle, bh);
445 + memcpy(bh->b_data, buf, csize);
446 + set_buffer_uptodate(bh);
447 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
454 + inode_lock(ea_inode);
455 + i_size_write(ea_inode, wsize);
456 + ext4_update_i_disksize(ea_inode, wsize);
457 + inode_unlock(ea_inode);
459 + ext4_mark_inode_dirty(handle, ea_inode);
468 + * Create an inode to store the value of a large EA.
470 +static struct inode *ext4_xattr_inode_create(handle_t *handle,
471 + struct inode *inode)
473 + struct inode *ea_inode = NULL;
476 + * Let the next inode be the goal, so we try and allocate the EA inode
477 + * in the same group, or nearby one.
479 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
480 + S_IFREG | 0600, NULL, inode->i_ino + 1, NULL);
481 + if (!IS_ERR(ea_inode)) {
482 + ea_inode->i_op = &ext4_file_inode_operations;
483 + ea_inode->i_fop = &ext4_file_operations;
484 + ext4_set_aops(ea_inode);
485 + ea_inode->i_generation = inode->i_generation;
486 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
489 + * A back-pointer from EA inode to parent inode will be useful
492 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
493 + unlock_new_inode(ea_inode);
500 + * Unlink the inode storing the value of the EA.
502 +int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
504 + struct inode *ea_inode = NULL;
507 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
511 + clear_nlink(ea_inode);
518 + * Add value of the EA in an inode.
520 +static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
521 + unsigned long *ea_ino, const void *value,
524 + struct inode *ea_inode;
527 + /* Create an inode for the EA value */
528 + ea_inode = ext4_xattr_inode_create(handle, inode);
529 + if (IS_ERR(ea_inode))
530 + return PTR_ERR(ea_inode);
532 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
534 + clear_nlink(ea_inode);
536 + *ea_ino = ea_inode->i_ino;
543 +static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
544 + struct ext4_xattr_search *s,
545 + handle_t *handle, struct inode *inode)
547 struct ext4_xattr_entry *last;
548 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
549 + int in_inode = i->in_inode;
552 + if (ext4_has_feature_ea_inode(inode->i_sb) &&
553 + (EXT4_XATTR_SIZE(i->value_len) >
554 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
557 /* Compute min_offs and last. */
559 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
560 - if (last->e_value_size) {
561 + if (!last->e_value_inum && last->e_value_size) {
562 size_t offs = le16_to_cpu(last->e_value_offs);
565 @@ -648,15 +913,20 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
567 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
569 - if (s->here->e_value_size) {
571 + !s->here->e_value_inum && s->here->e_value_size) {
572 size_t size = le32_to_cpu(s->here->e_value_size);
573 free += EXT4_XATTR_SIZE(size);
575 free += EXT4_XATTR_LEN(name_len);
578 - if (free < EXT4_XATTR_LEN(name_len) +
579 - EXT4_XATTR_SIZE(i->value_len))
580 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
585 + if (free < EXT4_XATTR_LEN(name_len) + value_len)
589 @@ -670,7 +940,8 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
590 s->here->e_name_len = name_len;
591 memcpy(s->here->e_name, i->name, name_len);
593 - if (s->here->e_value_size) {
594 + if (!s->here->e_value_inum && s->here->e_value_size &&
595 + s->here->e_value_offs > 0) {
596 void *first_val = s->base + min_offs;
597 size_t offs = le16_to_cpu(s->here->e_value_offs);
598 void *val = s->base + offs;
599 @@ -704,12 +975,18 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
601 while (!IS_LAST_ENTRY(last)) {
602 size_t o = le16_to_cpu(last->e_value_offs);
603 - if (last->e_value_size && o < offs)
604 + if (!last->e_value_inum &&
605 + last->e_value_size && o < offs)
607 cpu_to_le16(o + size);
608 last = EXT4_XATTR_NEXT(last);
611 + if (s->here->e_value_inum) {
612 + ext4_xattr_inode_unlink(inode,
613 + le32_to_cpu(s->here->e_value_inum));
614 + s->here->e_value_inum = 0;
617 /* Remove the old name. */
618 size_t size = EXT4_XATTR_LEN(name_len);
619 @@ -722,11 +999,20 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
622 /* Insert the new value. */
623 - s->here->e_value_size = cpu_to_le32(i->value_len);
624 - if (i->value_len) {
626 + unsigned long ea_ino =
627 + le32_to_cpu(s->here->e_value_inum);
628 + rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
629 + i->value, i->value_len);
632 + s->here->e_value_inum = cpu_to_le32(ea_ino);
633 + s->here->e_value_offs = 0;
634 + } else if (i->value_len) {
635 size_t size = EXT4_XATTR_SIZE(i->value_len);
636 void *val = s->base + min_offs - size;
637 s->here->e_value_offs = cpu_to_le16(min_offs - size);
638 + s->here->e_value_inum = 0;
639 if (i->value == EXT4_ZERO_XATTR_VALUE) {
640 memset(val, 0, size);
642 @@ -736,8 +1022,11 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
643 memcpy(val, i->value, i->value_len);
646 + s->here->e_value_size = cpu_to_le32(i->value_len);
654 struct ext4_xattr_block_find {
655 @@ -801,8 +1090,6 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
657 #define header(x) ((struct ext4_xattr_header *)(x))
659 - if (i->value && i->value_len > sb->s_blocksize)
662 BUFFER_TRACE(bs->bh, "get_write_access");
663 error = ext4_journal_get_write_access(handle, bs->bh);
664 @@ -821,7 +1108,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
665 mb_cache_entry_delete_block(ext4_mb_cache, hash,
667 ea_bdebug(bs->bh, "modifying in-place");
668 - error = ext4_xattr_set_entry(i, s);
669 + error = ext4_xattr_set_entry(i, s, handle, inode);
671 if (!IS_LAST_ENTRY(s->first))
672 ext4_xattr_rehash(header(s->base),
673 @@ -870,7 +1157,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
674 s->end = s->base + sb->s_blocksize;
677 - error = ext4_xattr_set_entry(i, s);
678 + error = ext4_xattr_set_entry(i, s, handle, inode);
679 if (error == -EFSCORRUPTED)
682 @@ -1070,7 +1357,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
684 if (EXT4_I(inode)->i_extra_isize == 0)
686 - error = ext4_xattr_set_entry(i, s);
687 + error = ext4_xattr_set_entry(i, s, handle, inode);
689 if (error == -ENOSPC &&
690 ext4_has_inline_data(inode)) {
691 @@ -1082,7 +1369,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
692 error = ext4_xattr_ibody_find(inode, i, is);
695 - error = ext4_xattr_set_entry(i, s);
696 + error = ext4_xattr_set_entry(i, s, handle, inode);
700 @@ -1098,7 +1385,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
704 -static int ext4_xattr_ibody_set(struct inode *inode,
705 +static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
706 struct ext4_xattr_info *i,
707 struct ext4_xattr_ibody_find *is)
709 @@ -1108,7 +1395,7 @@ static int ext4_xattr_ibody_set(struct inode *inode,
711 if (EXT4_I(inode)->i_extra_isize == 0)
713 - error = ext4_xattr_set_entry(i, s);
714 + error = ext4_xattr_set_entry(i, s, handle, inode);
717 header = IHDR(inode, ext4_raw_inode(&is->iloc));
718 @@ -1155,7 +1442,7 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
721 .value_len = value_len,
725 struct ext4_xattr_ibody_find is = {
726 .s = { .not_found = -ENODATA, },
727 @@ -1204,7 +1491,7 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
731 - error = ext4_xattr_ibody_set(inode, &i, &is);
732 + error = ext4_xattr_ibody_set(handle, inode, &i, &is);
733 else if (!bs.s.not_found)
734 error = ext4_xattr_block_set(handle, inode, &i, &bs);
736 @@ -1215,7 +1502,7 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
737 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
740 - error = ext4_xattr_ibody_set(inode, &i, &is);
741 + error = ext4_xattr_ibody_set(handle, inode, &i, &is);
742 if (!error && !bs.s.not_found) {
744 error = ext4_xattr_block_set(handle, inode, &i, &bs);
745 @@ -1226,11 +1513,20 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
748 error = ext4_xattr_block_set(handle, inode, &i, &bs);
749 + if (ext4_has_feature_ea_inode(inode->i_sb) &&
750 + error == -ENOSPC) {
751 + /* xattr not fit to block, store at external
754 + error = ext4_xattr_ibody_set(handle, inode,
759 if (!is.s.not_found) {
761 - error = ext4_xattr_ibody_set(inode, &i, &is);
762 + error = ext4_xattr_ibody_set(handle, inode, &i,
767 @@ -1269,12 +1565,26 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
768 const void *value, size_t value_len, int flags)
771 + struct super_block *sb = inode->i_sb;
772 int error, retries = 0;
773 int credits = ext4_jbd2_credits_xattr(inode);
775 error = dquot_initialize(inode);
779 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
780 + ext4_has_feature_ea_inode(sb)) {
781 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
782 + sb->s_blocksize_bits;
784 + /* For new inode */
785 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
787 + /* For data blocks of EA inode */
788 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
792 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
793 if (IS_ERR(handle)) {
794 @@ -1286,7 +1596,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
795 value, value_len, flags);
796 error2 = ext4_journal_stop(handle);
797 if (error == -ENOSPC &&
798 - ext4_should_retry_alloc(inode->i_sb, &retries))
799 + ext4_should_retry_alloc(sb, &retries))
803 @@ -1311,7 +1621,7 @@ static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
805 /* Adjust the value offsets of the entries */
806 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
807 - if (last->e_value_size) {
808 + if (!last->e_value_inum && last->e_value_size) {
809 new_offs = le16_to_cpu(last->e_value_offs) +
811 last->e_value_offs = cpu_to_le16(new_offs);
812 @@ -1372,7 +1682,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
815 /* Remove the chosen entry from the inode */
816 - error = ext4_xattr_ibody_set(inode, &i, is);
817 + error = ext4_xattr_ibody_set(handle, inode, &i, is);
821 @@ -1572,21 +1882,135 @@ int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
825 +#define EIA_INCR 16 /* must be 2^n */
826 +#define EIA_MASK (EIA_INCR - 1)
827 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
828 + * If @lea_ino_array is new or full it will be grown and the old
829 + * contents copied over.
832 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
834 + if (*lea_ino_array == NULL) {
836 + * Start with 15 inodes, so it fits into a power-of-two size.
837 + * If *lea_ino_array is NULL, this is essentially offsetof()
840 + kmalloc(offsetof(struct ext4_xattr_ino_array,
841 + xia_inodes[EIA_MASK]),
843 + if (*lea_ino_array == NULL)
845 + (*lea_ino_array)->xia_count = 0;
846 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
847 + /* expand the array once all 15 + n * 16 slots are full */
848 + struct ext4_xattr_ino_array *new_array = NULL;
849 + int count = (*lea_ino_array)->xia_count;
851 + /* if new_array is NULL, this is essentially offsetof() */
852 + new_array = kmalloc(
853 + offsetof(struct ext4_xattr_ino_array,
854 + xia_inodes[count + EIA_INCR]),
856 + if (new_array == NULL)
858 + memcpy(new_array, *lea_ino_array,
859 + offsetof(struct ext4_xattr_ino_array,
860 + xia_inodes[count]));
861 + kfree(*lea_ino_array);
862 + *lea_ino_array = new_array;
864 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
869 + * Add xattr inode to orphan list
872 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
873 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
875 + struct inode *ea_inode = NULL;
876 + int idx = 0, error = 0;
878 + if (lea_ino_array == NULL)
881 + for (; idx < lea_ino_array->xia_count; ++idx) {
882 + if (!ext4_handle_has_enough_credits(handle, credits)) {
883 + error = ext4_journal_extend(handle, credits);
885 + error = ext4_journal_restart(handle, credits);
888 + ext4_warning(inode->i_sb,
889 + "couldn't extend journal "
890 + "(err %d)", error);
894 + ea_inode = ext4_xattr_inode_iget(inode,
895 + lea_ino_array->xia_inodes[idx], &error);
898 + ext4_orphan_add(handle, ea_inode);
899 + /* the inode's i_count will be released by caller */
906 * ext4_xattr_delete_inode()
908 - * Free extended attribute resources associated with this inode. This
909 + * Free extended attribute resources associated with this inode. Traverse
910 + * all entries and unlink any xattr inodes associated with this inode. This
911 * is called immediately before an inode is freed. We have exclusive
912 - * access to the inode.
913 + * access to the inode. If an orphan inode is deleted it will also delete any
914 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
915 + * to ensure they belong to the parent inode and were not deleted already.
918 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
920 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
921 + struct ext4_xattr_ino_array **lea_ino_array)
923 struct buffer_head *bh = NULL;
924 + struct ext4_xattr_ibody_header *header;
925 + struct ext4_inode *raw_inode;
926 + struct ext4_iloc iloc;
927 + struct ext4_xattr_entry *entry;
928 + int credits = 3, error = 0;
930 - if (!EXT4_I(inode)->i_file_acl)
931 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
932 + goto delete_external_ea;
934 + error = ext4_get_inode_loc(inode, &iloc);
937 + raw_inode = ext4_raw_inode(&iloc);
938 + header = IHDR(inode, raw_inode);
939 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
940 + entry = EXT4_XATTR_NEXT(entry)) {
941 + if (!entry->e_value_inum)
943 + if (ext4_expand_ino_array(lea_ino_array,
944 + entry->e_value_inum) != 0) {
948 + entry->e_value_inum = 0;
953 + if (!EXT4_I(inode)->i_file_acl) {
954 + /* add xattr inode to orphan list */
955 + ext4_xattr_inode_orphan_add(handle, inode, credits,
959 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
961 EXT4_ERROR_INODE(inode, "block %llu read error",
962 @@ -1599,11 +2023,69 @@ ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
963 EXT4_I(inode)->i_file_acl);
967 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
968 + entry = EXT4_XATTR_NEXT(entry)) {
969 + if (!entry->e_value_inum)
971 + if (ext4_expand_ino_array(lea_ino_array,
972 + entry->e_value_inum) != 0)
974 + entry->e_value_inum = 0;
977 + /* add xattr inode to orphan list */
978 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
983 + if (!IS_NOQUOTA(inode))
984 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
986 + if (!ext4_handle_has_enough_credits(handle, credits)) {
987 + error = ext4_journal_extend(handle, credits);
989 + error = ext4_journal_restart(handle, credits);
991 + ext4_warning(inode->i_sb,
992 + "couldn't extend journal (err %d)", error);
997 ext4_xattr_release_block(handle, inode, bh);
998 EXT4_I(inode)->i_file_acl = 0;
1007 +ext4_xattr_inode_array_free(struct inode *inode,
1008 + struct ext4_xattr_ino_array *lea_ino_array)
1010 + struct inode *ea_inode = NULL;
1014 + if (lea_ino_array == NULL)
1017 + for (; idx < lea_ino_array->xia_count; ++idx) {
1018 + ea_inode = ext4_xattr_inode_iget(inode,
1019 + lea_ino_array->xia_inodes[idx], &err);
1022 + /* for inode's i_count get from ext4_xattr_delete_inode */
1023 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
1025 + clear_nlink(ea_inode);
1028 + kfree(lea_ino_array);
1032 @@ -1655,10 +2137,9 @@ ext4_xattr_cmp(struct ext4_xattr_header *header1,
1033 entry1->e_name_index != entry2->e_name_index ||
1034 entry1->e_name_len != entry2->e_name_len ||
1035 entry1->e_value_size != entry2->e_value_size ||
1036 + entry1->e_value_inum != entry2->e_value_inum ||
1037 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1039 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1040 - return -EFSCORRUPTED;
1041 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1042 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1043 le32_to_cpu(entry1->e_value_size)))
1044 @@ -1730,7 +2211,7 @@ static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1048 - if (entry->e_value_size != 0) {
1049 + if (!entry->e_value_inum && entry->e_value_size) {
1050 __le32 *value = (__le32 *)((char *)header +
1051 le16_to_cpu(entry->e_value_offs));
1052 for (n = (le32_to_cpu(entry->e_value_size) +
1053 diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
1054 index 099c8b670ef5..6e10ff9393d4 100644
1055 --- a/fs/ext4/xattr.h
1056 +++ b/fs/ext4/xattr.h
1057 @@ -44,7 +44,7 @@ struct ext4_xattr_entry {
1058 __u8 e_name_len; /* length of name */
1059 __u8 e_name_index; /* attribute name index */
1060 __le16 e_value_offs; /* offset in disk block of value */
1061 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1062 + __le32 e_value_inum; /* inode in which the value is stored */
1063 __le32 e_value_size; /* size of attribute value */
1064 __le32 e_hash; /* hash value of name and value */
1065 char e_name[0]; /* attribute name */
1066 @@ -69,6 +69,26 @@ struct ext4_xattr_entry {
1067 EXT4_I(inode)->i_extra_isize))
1068 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1071 + * Link EA inode back to parent one using i_mtime field.
1072 + * Extra integer type conversion added to ignore higher
1073 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1075 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1077 + (inode)->i_mtime.tv_sec = inum; \
1080 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1081 +((__u32)(inode)->i_mtime.tv_sec)
1084 + * The minimum size of EA value when you start storing it in an external inode
1085 + * size of block - size of header - size of 1 entry - 4 null bytes
1087 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1088 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1090 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1091 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1092 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1093 @@ -77,10 +97,11 @@ struct ext4_xattr_entry {
1094 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1096 struct ext4_xattr_info {
1105 struct ext4_xattr_search {
1106 @@ -140,7 +161,13 @@ extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
1107 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1108 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1110 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1111 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1113 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1114 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1115 + struct ext4_xattr_ino_array **array);
1116 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1117 + struct ext4_xattr_ino_array *array);
1119 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1120 struct ext4_inode *raw_inode, handle_t *handle);
1122 2.13.1.611.g7e3b11ae1-goog