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 [ Applied fix by Dan Carpenter to avoid freeing an ERR_PTR. ]
35 Lustre-Jira: https://jira.hpdd.intel.com/browse/LU-80
36 Lustre-bugzilla: https://bugzilla.lustre.org/show_bug.cgi?id=4424
37 Signed-off-by: Kalpak Shah <kalpak.shah@sun.com>
38 Signed-off-by: James Simmons <uja.ornl@gmail.com>
39 Signed-off-by: Andreas Dilger <andreas.dilger@intel.com>
40 Signed-off-by: Tahsin Erdogan <tahsin@google.com>
41 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
42 Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
44 fs/ext4/ext4.h | 12 ++
45 fs/ext4/ialloc.c | 1 -
46 fs/ext4/inline.c | 2 +-
47 fs/ext4/inode.c | 49 +++++--
48 fs/ext4/xattr.c | 563 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
49 fs/ext4/xattr.h | 33 ++++-
50 6 files changed, 604 insertions(+), 56 deletions(-)
52 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
53 index f17a4e7075be..41e26ad86fc3 100644
56 @@ -1797,6 +1797,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(encrypt, ENCRYPT)
57 EXT4_FEATURE_INCOMPAT_EXTENTS| \
58 EXT4_FEATURE_INCOMPAT_64BIT| \
59 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
60 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
61 EXT4_FEATURE_INCOMPAT_MMP | \
62 EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
63 EXT4_FEATURE_INCOMPAT_ENCRYPT | \
64 @@ -2231,6 +2232,12 @@ struct mmpd_data {
65 #define EXT4_MMP_MAX_CHECK_INTERVAL 300UL
68 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
69 + * This limit is arbitrary, but is reasonable for the xattr API.
71 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE (1024 * 1024)
77 @@ -2242,6 +2249,10 @@ struct mmpd_data {
78 # define ATTRIB_NORET __attribute__((noreturn))
79 # define NORET_AND noreturn,
81 +struct ext4_xattr_ino_array {
82 + unsigned int xia_count; /* # of used item in the array */
83 + unsigned int xia_inodes[0];
86 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
87 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
88 @@ -2489,6 +2500,7 @@ extern int ext4_truncate_restart_trans(handle_t *, struct inode *, int nblocks);
89 extern void ext4_set_inode_flags(struct inode *);
90 extern int ext4_alloc_da_blocks(struct inode *inode);
91 extern void ext4_set_aops(struct inode *inode);
92 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
93 extern int ext4_writepage_trans_blocks(struct inode *);
94 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
95 extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
96 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
97 index 98ac2f1f23b3..e2eb3cc06820 100644
98 --- a/fs/ext4/ialloc.c
99 +++ b/fs/ext4/ialloc.c
100 @@ -294,7 +294,6 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
101 * as writing the quota to disk may need the lock as well.
103 dquot_initialize(inode);
104 - ext4_xattr_delete_inode(handle, inode);
105 dquot_free_inode(inode);
108 diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
109 index 8d141c0c8ff9..28c5c3abddb3 100644
110 --- a/fs/ext4/inline.c
111 +++ b/fs/ext4/inline.c
112 @@ -61,7 +61,7 @@ static int get_max_inline_xattr_value_size(struct inode *inode,
114 /* Compute min_offs. */
115 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
116 - if (!entry->e_value_block && entry->e_value_size) {
117 + if (!entry->e_value_inum && entry->e_value_size) {
118 size_t offs = le16_to_cpu(entry->e_value_offs);
121 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
122 index 47604d1352fc..986efd9511ac 100644
123 --- a/fs/ext4/inode.c
124 +++ b/fs/ext4/inode.c
125 @@ -139,8 +139,6 @@ static void ext4_invalidatepage(struct page *page, unsigned int offset,
126 unsigned int length);
127 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
128 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
129 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
133 * Test whether an inode is a fast symlink.
134 @@ -189,6 +187,8 @@ void ext4_evict_inode(struct inode *inode)
138 + int extra_credits = 3;
139 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
141 trace_ext4_evict_inode(inode);
143 @@ -238,8 +238,8 @@ void ext4_evict_inode(struct inode *inode)
144 * protection against it
146 sb_start_intwrite(inode->i_sb);
147 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
148 - ext4_blocks_for_truncate(inode)+3);
150 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
151 if (IS_ERR(handle)) {
152 ext4_std_error(inode->i_sb, PTR_ERR(handle));
154 @@ -251,9 +251,36 @@ void ext4_evict_inode(struct inode *inode)
155 sb_end_intwrite(inode->i_sb);
160 ext4_handle_sync(handle);
163 + * Delete xattr inode before deleting the main inode.
165 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
167 + ext4_warning(inode->i_sb,
168 + "couldn't delete inode's xattr (err %d)", err);
172 + if (!IS_NOQUOTA(inode))
173 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
175 + if (!ext4_handle_has_enough_credits(handle,
176 + ext4_blocks_for_truncate(inode) + extra_credits)) {
177 + err = ext4_journal_extend(handle,
178 + ext4_blocks_for_truncate(inode) + extra_credits);
180 + err = ext4_journal_restart(handle,
181 + ext4_blocks_for_truncate(inode) + extra_credits);
183 + ext4_warning(inode->i_sb,
184 + "couldn't extend journal (err %d)", err);
190 err = ext4_mark_inode_dirty(handle, inode);
192 @@ -277,10 +304,10 @@ void ext4_evict_inode(struct inode *inode)
193 * enough credits left in the handle to remove the inode from
194 * the orphan list and set the dtime field.
196 - if (!ext4_handle_has_enough_credits(handle, 3)) {
197 - err = ext4_journal_extend(handle, 3);
198 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
199 + err = ext4_journal_extend(handle, extra_credits);
201 - err = ext4_journal_restart(handle, 3);
202 + err = ext4_journal_restart(handle, extra_credits);
204 ext4_warning(inode->i_sb,
205 "couldn't extend journal (err %d)", err);
206 @@ -315,8 +342,12 @@ void ext4_evict_inode(struct inode *inode)
207 ext4_clear_inode(inode);
209 ext4_free_inode(handle, inode);
211 ext4_journal_stop(handle);
212 sb_end_intwrite(inode->i_sb);
214 + if (lea_ino_array != NULL)
215 + ext4_xattr_inode_array_free(inode, lea_ino_array);
218 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
219 @@ -5504,7 +5535,7 @@ static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
221 * Also account for superblock, inode, quota and xattr blocks
223 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
224 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
227 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
228 diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
229 index 5d3c2536641c..7dd80d16f98e 100644
230 --- a/fs/ext4/xattr.c
231 +++ b/fs/ext4/xattr.c
232 @@ -177,9 +177,8 @@ ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
234 /* Check the values */
235 while (!IS_LAST_ENTRY(entry)) {
236 - if (entry->e_value_block != 0)
237 - return -EFSCORRUPTED;
238 - if (entry->e_value_size != 0) {
239 + if (entry->e_value_size != 0 &&
240 + entry->e_value_inum == 0) {
241 u16 offs = le16_to_cpu(entry->e_value_offs);
242 u32 size = le32_to_cpu(entry->e_value_size);
244 @@ -269,6 +268,99 @@ ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
245 return cmp ? -ENODATA : 0;
249 + * Read the EA value from an inode.
252 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
254 + unsigned long block = 0;
255 + struct buffer_head *bh = NULL;
257 + size_t csize, ret_size = 0;
262 + blocksize = ea_inode->i_sb->s_blocksize;
264 + while (ret_size < *size) {
265 + csize = (*size - ret_size) > blocksize ? blocksize :
267 + bh = ext4_bread(NULL, ea_inode, block, 0);
270 + return PTR_ERR(bh);
272 + memcpy(buf, bh->b_data, csize);
285 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
287 + struct inode *ea_inode = NULL;
289 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
290 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
291 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
292 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
293 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
294 + *err = rc != 0 ? rc : -EIO;
298 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
299 + ea_inode->i_generation != parent->i_generation) {
300 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
301 + "to parent invalid.", ea_ino);
306 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
307 + ext4_error(parent->i_sb, "EA inode %lu does not have "
308 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
322 + * Read the value from the EA inode.
325 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
328 + struct inode *ea_inode = NULL;
331 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
335 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
342 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
343 void *buffer, size_t buffer_size)
344 @@ -308,8 +400,16 @@ ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
346 if (size > buffer_size)
348 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
350 + if (entry->e_value_inum) {
351 + error = ext4_xattr_inode_get(inode,
352 + le32_to_cpu(entry->e_value_inum),
357 + memcpy(buffer, bh->b_data +
358 + le16_to_cpu(entry->e_value_offs), size);
363 @@ -350,8 +450,16 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
365 if (size > buffer_size)
367 - memcpy(buffer, (void *)IFIRST(header) +
368 - le16_to_cpu(entry->e_value_offs), size);
369 + if (entry->e_value_inum) {
370 + error = ext4_xattr_inode_get(inode,
371 + le32_to_cpu(entry->e_value_inum),
376 + memcpy(buffer, (void *)IFIRST(header) +
377 + le16_to_cpu(entry->e_value_offs), size);
382 @@ -620,7 +728,7 @@ static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
383 size_t *min_offs, void *base, int *total)
385 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
386 - if (last->e_value_size) {
387 + if (!last->e_value_inum && last->e_value_size) {
388 size_t offs = le16_to_cpu(last->e_value_offs);
389 if (offs < *min_offs)
391 @@ -631,16 +739,171 @@ static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
392 return (*min_offs - ((void *)last - base) - sizeof(__u32));
396 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
398 + * Write the value of the EA in an inode.
400 +static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
401 + const void *buf, int bufsize)
403 + struct buffer_head *bh = NULL;
404 + unsigned long block = 0;
405 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
406 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
407 + int csize, wsize = 0;
412 + while (ret >= 0 && ret < max_blocks) {
413 + struct ext4_map_blocks map;
414 + map.m_lblk = block += ret;
415 + map.m_len = max_blocks -= ret;
417 + ret = ext4_map_blocks(handle, ea_inode, &map,
418 + EXT4_GET_BLOCKS_CREATE);
420 + ext4_mark_inode_dirty(handle, ea_inode);
421 + if (ret == -ENOSPC &&
422 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
434 + while (wsize < bufsize) {
437 + csize = (bufsize - wsize) > blocksize ? blocksize :
439 + bh = ext4_getblk(handle, ea_inode, block, 0);
441 + return PTR_ERR(bh);
442 + ret = ext4_journal_get_write_access(handle, bh);
446 + memcpy(bh->b_data, buf, csize);
447 + set_buffer_uptodate(bh);
448 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
455 + inode_lock(ea_inode);
456 + i_size_write(ea_inode, wsize);
457 + ext4_update_i_disksize(ea_inode, wsize);
458 + inode_unlock(ea_inode);
460 + ext4_mark_inode_dirty(handle, ea_inode);
469 + * Create an inode to store the value of a large EA.
471 +static struct inode *ext4_xattr_inode_create(handle_t *handle,
472 + struct inode *inode)
474 + struct inode *ea_inode = NULL;
477 + * Let the next inode be the goal, so we try and allocate the EA inode
478 + * in the same group, or nearby one.
480 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
481 + S_IFREG | 0600, NULL, inode->i_ino + 1, NULL);
482 + if (!IS_ERR(ea_inode)) {
483 + ea_inode->i_op = &ext4_file_inode_operations;
484 + ea_inode->i_fop = &ext4_file_operations;
485 + ext4_set_aops(ea_inode);
486 + ea_inode->i_generation = inode->i_generation;
487 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
490 + * A back-pointer from EA inode to parent inode will be useful
493 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
494 + unlock_new_inode(ea_inode);
501 + * Unlink the inode storing the value of the EA.
503 +int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
505 + struct inode *ea_inode = NULL;
508 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
512 + clear_nlink(ea_inode);
519 + * Add value of the EA in an inode.
521 +static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
522 + unsigned long *ea_ino, const void *value,
525 + struct inode *ea_inode;
528 + /* Create an inode for the EA value */
529 + ea_inode = ext4_xattr_inode_create(handle, inode);
530 + if (IS_ERR(ea_inode))
531 + return PTR_ERR(ea_inode);
533 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
535 + clear_nlink(ea_inode);
537 + *ea_ino = ea_inode->i_ino;
544 +static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
545 + struct ext4_xattr_search *s,
546 + handle_t *handle, struct inode *inode)
548 struct ext4_xattr_entry *last;
549 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
550 + int in_inode = i->in_inode;
553 + if (ext4_has_feature_ea_inode(inode->i_sb) &&
554 + (EXT4_XATTR_SIZE(i->value_len) >
555 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
558 /* Compute min_offs and last. */
560 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
561 - if (last->e_value_size) {
562 + if (!last->e_value_inum && last->e_value_size) {
563 size_t offs = le16_to_cpu(last->e_value_offs);
566 @@ -648,15 +911,20 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
568 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
570 - if (s->here->e_value_size) {
572 + !s->here->e_value_inum && s->here->e_value_size) {
573 size_t size = le32_to_cpu(s->here->e_value_size);
574 free += EXT4_XATTR_SIZE(size);
576 free += EXT4_XATTR_LEN(name_len);
579 - if (free < EXT4_XATTR_LEN(name_len) +
580 - EXT4_XATTR_SIZE(i->value_len))
581 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
586 + if (free < EXT4_XATTR_LEN(name_len) + value_len)
590 @@ -670,7 +938,8 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
591 s->here->e_name_len = name_len;
592 memcpy(s->here->e_name, i->name, name_len);
594 - if (s->here->e_value_size) {
595 + if (!s->here->e_value_inum && s->here->e_value_size &&
596 + s->here->e_value_offs > 0) {
597 void *first_val = s->base + min_offs;
598 size_t offs = le16_to_cpu(s->here->e_value_offs);
599 void *val = s->base + offs;
600 @@ -704,12 +973,18 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
602 while (!IS_LAST_ENTRY(last)) {
603 size_t o = le16_to_cpu(last->e_value_offs);
604 - if (last->e_value_size && o < offs)
605 + if (!last->e_value_inum &&
606 + last->e_value_size && o < offs)
608 cpu_to_le16(o + size);
609 last = EXT4_XATTR_NEXT(last);
612 + if (s->here->e_value_inum) {
613 + ext4_xattr_inode_unlink(inode,
614 + le32_to_cpu(s->here->e_value_inum));
615 + s->here->e_value_inum = 0;
618 /* Remove the old name. */
619 size_t size = EXT4_XATTR_LEN(name_len);
620 @@ -722,11 +997,20 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
623 /* Insert the new value. */
624 - s->here->e_value_size = cpu_to_le32(i->value_len);
625 - if (i->value_len) {
627 + unsigned long ea_ino =
628 + le32_to_cpu(s->here->e_value_inum);
629 + rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
630 + i->value, i->value_len);
633 + s->here->e_value_inum = cpu_to_le32(ea_ino);
634 + s->here->e_value_offs = 0;
635 + } else if (i->value_len) {
636 size_t size = EXT4_XATTR_SIZE(i->value_len);
637 void *val = s->base + min_offs - size;
638 s->here->e_value_offs = cpu_to_le16(min_offs - size);
639 + s->here->e_value_inum = 0;
640 if (i->value == EXT4_ZERO_XATTR_VALUE) {
641 memset(val, 0, size);
643 @@ -736,8 +1020,11 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
644 memcpy(val, i->value, i->value_len);
647 + s->here->e_value_size = cpu_to_le32(i->value_len);
655 struct ext4_xattr_block_find {
656 @@ -801,8 +1088,6 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
658 #define header(x) ((struct ext4_xattr_header *)(x))
660 - if (i->value && i->value_len > sb->s_blocksize)
663 BUFFER_TRACE(bs->bh, "get_write_access");
664 error = ext4_journal_get_write_access(handle, bs->bh);
665 @@ -821,7 +1106,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
666 mb_cache_entry_delete_block(ext4_mb_cache, hash,
668 ea_bdebug(bs->bh, "modifying in-place");
669 - error = ext4_xattr_set_entry(i, s);
670 + error = ext4_xattr_set_entry(i, s, handle, inode);
672 if (!IS_LAST_ENTRY(s->first))
673 ext4_xattr_rehash(header(s->base),
674 @@ -870,7 +1155,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
675 s->end = s->base + sb->s_blocksize;
678 - error = ext4_xattr_set_entry(i, s);
679 + error = ext4_xattr_set_entry(i, s, handle, inode);
680 if (error == -EFSCORRUPTED)
683 @@ -1070,7 +1355,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
685 if (EXT4_I(inode)->i_extra_isize == 0)
687 - error = ext4_xattr_set_entry(i, s);
688 + error = ext4_xattr_set_entry(i, s, handle, inode);
690 if (error == -ENOSPC &&
691 ext4_has_inline_data(inode)) {
692 @@ -1082,7 +1367,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
693 error = ext4_xattr_ibody_find(inode, i, is);
696 - error = ext4_xattr_set_entry(i, s);
697 + error = ext4_xattr_set_entry(i, s, handle, inode);
701 @@ -1098,7 +1383,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
705 -static int ext4_xattr_ibody_set(struct inode *inode,
706 +static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
707 struct ext4_xattr_info *i,
708 struct ext4_xattr_ibody_find *is)
710 @@ -1108,7 +1393,7 @@ static int ext4_xattr_ibody_set(struct inode *inode,
712 if (EXT4_I(inode)->i_extra_isize == 0)
714 - error = ext4_xattr_set_entry(i, s);
715 + error = ext4_xattr_set_entry(i, s, handle, inode);
718 header = IHDR(inode, ext4_raw_inode(&is->iloc));
719 @@ -1155,7 +1440,7 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
722 .value_len = value_len,
726 struct ext4_xattr_ibody_find is = {
727 .s = { .not_found = -ENODATA, },
728 @@ -1204,7 +1489,7 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
732 - error = ext4_xattr_ibody_set(inode, &i, &is);
733 + error = ext4_xattr_ibody_set(handle, inode, &i, &is);
734 else if (!bs.s.not_found)
735 error = ext4_xattr_block_set(handle, inode, &i, &bs);
737 @@ -1215,7 +1500,7 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
738 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
741 - error = ext4_xattr_ibody_set(inode, &i, &is);
742 + error = ext4_xattr_ibody_set(handle, inode, &i, &is);
743 if (!error && !bs.s.not_found) {
745 error = ext4_xattr_block_set(handle, inode, &i, &bs);
746 @@ -1226,11 +1511,20 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
749 error = ext4_xattr_block_set(handle, inode, &i, &bs);
750 + if (ext4_has_feature_ea_inode(inode->i_sb) &&
751 + error == -ENOSPC) {
752 + /* xattr not fit to block, store at external
755 + error = ext4_xattr_ibody_set(handle, inode,
760 if (!is.s.not_found) {
762 - error = ext4_xattr_ibody_set(inode, &i, &is);
763 + error = ext4_xattr_ibody_set(handle, inode, &i,
768 @@ -1269,12 +1563,26 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
769 const void *value, size_t value_len, int flags)
772 + struct super_block *sb = inode->i_sb;
773 int error, retries = 0;
774 int credits = ext4_jbd2_credits_xattr(inode);
776 error = dquot_initialize(inode);
780 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
781 + ext4_has_feature_ea_inode(sb)) {
782 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
783 + sb->s_blocksize_bits;
785 + /* For new inode */
786 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
788 + /* For data blocks of EA inode */
789 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
793 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
794 if (IS_ERR(handle)) {
795 @@ -1286,7 +1594,7 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
796 value, value_len, flags);
797 error2 = ext4_journal_stop(handle);
798 if (error == -ENOSPC &&
799 - ext4_should_retry_alloc(inode->i_sb, &retries))
800 + ext4_should_retry_alloc(sb, &retries))
804 @@ -1311,7 +1619,7 @@ static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
806 /* Adjust the value offsets of the entries */
807 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
808 - if (last->e_value_size) {
809 + if (!last->e_value_inum && last->e_value_size) {
810 new_offs = le16_to_cpu(last->e_value_offs) +
812 last->e_value_offs = cpu_to_le16(new_offs);
813 @@ -1372,7 +1680,7 @@ static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
816 /* Remove the chosen entry from the inode */
817 - error = ext4_xattr_ibody_set(inode, &i, is);
818 + error = ext4_xattr_ibody_set(handle, inode, &i, is);
822 @@ -1572,21 +1880,135 @@ int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
826 +#define EIA_INCR 16 /* must be 2^n */
827 +#define EIA_MASK (EIA_INCR - 1)
828 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
829 + * If @lea_ino_array is new or full it will be grown and the old
830 + * contents copied over.
833 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
835 + if (*lea_ino_array == NULL) {
837 + * Start with 15 inodes, so it fits into a power-of-two size.
838 + * If *lea_ino_array is NULL, this is essentially offsetof()
841 + kmalloc(offsetof(struct ext4_xattr_ino_array,
842 + xia_inodes[EIA_MASK]),
844 + if (*lea_ino_array == NULL)
846 + (*lea_ino_array)->xia_count = 0;
847 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
848 + /* expand the array once all 15 + n * 16 slots are full */
849 + struct ext4_xattr_ino_array *new_array = NULL;
850 + int count = (*lea_ino_array)->xia_count;
852 + /* if new_array is NULL, this is essentially offsetof() */
853 + new_array = kmalloc(
854 + offsetof(struct ext4_xattr_ino_array,
855 + xia_inodes[count + EIA_INCR]),
857 + if (new_array == NULL)
859 + memcpy(new_array, *lea_ino_array,
860 + offsetof(struct ext4_xattr_ino_array,
861 + xia_inodes[count]));
862 + kfree(*lea_ino_array);
863 + *lea_ino_array = new_array;
865 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
870 + * Add xattr inode to orphan list
873 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
874 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
876 + struct inode *ea_inode = NULL;
877 + int idx = 0, error = 0;
879 + if (lea_ino_array == NULL)
882 + for (; idx < lea_ino_array->xia_count; ++idx) {
883 + if (!ext4_handle_has_enough_credits(handle, credits)) {
884 + error = ext4_journal_extend(handle, credits);
886 + error = ext4_journal_restart(handle, credits);
889 + ext4_warning(inode->i_sb,
890 + "couldn't extend journal "
891 + "(err %d)", error);
895 + ea_inode = ext4_xattr_inode_iget(inode,
896 + lea_ino_array->xia_inodes[idx], &error);
899 + ext4_orphan_add(handle, ea_inode);
900 + /* the inode's i_count will be released by caller */
907 * ext4_xattr_delete_inode()
909 - * Free extended attribute resources associated with this inode. This
910 + * Free extended attribute resources associated with this inode. Traverse
911 + * all entries and unlink any xattr inodes associated with this inode. This
912 * is called immediately before an inode is freed. We have exclusive
913 - * access to the inode.
914 + * access to the inode. If an orphan inode is deleted it will also delete any
915 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
916 + * to ensure they belong to the parent inode and were not deleted already.
919 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
921 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
922 + struct ext4_xattr_ino_array **lea_ino_array)
924 struct buffer_head *bh = NULL;
925 + struct ext4_xattr_ibody_header *header;
926 + struct ext4_inode *raw_inode;
927 + struct ext4_iloc iloc;
928 + struct ext4_xattr_entry *entry;
929 + int credits = 3, error = 0;
931 - if (!EXT4_I(inode)->i_file_acl)
932 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
933 + goto delete_external_ea;
935 + error = ext4_get_inode_loc(inode, &iloc);
938 + raw_inode = ext4_raw_inode(&iloc);
939 + header = IHDR(inode, raw_inode);
940 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
941 + entry = EXT4_XATTR_NEXT(entry)) {
942 + if (!entry->e_value_inum)
944 + if (ext4_expand_ino_array(lea_ino_array,
945 + entry->e_value_inum) != 0) {
949 + entry->e_value_inum = 0;
954 + if (!EXT4_I(inode)->i_file_acl) {
955 + /* add xattr inode to orphan list */
956 + ext4_xattr_inode_orphan_add(handle, inode, credits,
960 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
962 EXT4_ERROR_INODE(inode, "block %llu read error",
963 @@ -1599,11 +2021,69 @@ ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
964 EXT4_I(inode)->i_file_acl);
968 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
969 + entry = EXT4_XATTR_NEXT(entry)) {
970 + if (!entry->e_value_inum)
972 + if (ext4_expand_ino_array(lea_ino_array,
973 + entry->e_value_inum) != 0)
975 + entry->e_value_inum = 0;
978 + /* add xattr inode to orphan list */
979 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
984 + if (!IS_NOQUOTA(inode))
985 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
987 + if (!ext4_handle_has_enough_credits(handle, credits)) {
988 + error = ext4_journal_extend(handle, credits);
990 + error = ext4_journal_restart(handle, credits);
992 + ext4_warning(inode->i_sb,
993 + "couldn't extend journal (err %d)", error);
998 ext4_xattr_release_block(handle, inode, bh);
999 EXT4_I(inode)->i_file_acl = 0;
1008 +ext4_xattr_inode_array_free(struct inode *inode,
1009 + struct ext4_xattr_ino_array *lea_ino_array)
1011 + struct inode *ea_inode = NULL;
1015 + if (lea_ino_array == NULL)
1018 + for (; idx < lea_ino_array->xia_count; ++idx) {
1019 + ea_inode = ext4_xattr_inode_iget(inode,
1020 + lea_ino_array->xia_inodes[idx], &err);
1023 + /* for inode's i_count get from ext4_xattr_delete_inode */
1024 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
1026 + clear_nlink(ea_inode);
1029 + kfree(lea_ino_array);
1033 @@ -1655,10 +2135,9 @@ ext4_xattr_cmp(struct ext4_xattr_header *header1,
1034 entry1->e_name_index != entry2->e_name_index ||
1035 entry1->e_name_len != entry2->e_name_len ||
1036 entry1->e_value_size != entry2->e_value_size ||
1037 + entry1->e_value_inum != entry2->e_value_inum ||
1038 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1040 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1041 - return -EFSCORRUPTED;
1042 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1043 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1044 le32_to_cpu(entry1->e_value_size)))
1045 @@ -1730,7 +2209,7 @@ static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1049 - if (entry->e_value_size != 0) {
1050 + if (!entry->e_value_inum && entry->e_value_size) {
1051 __le32 *value = (__le32 *)((char *)header +
1052 le16_to_cpu(entry->e_value_offs));
1053 for (n = (le32_to_cpu(entry->e_value_size) +
1054 diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
1055 index 099c8b670ef5..6e10ff9393d4 100644
1056 --- a/fs/ext4/xattr.h
1057 +++ b/fs/ext4/xattr.h
1058 @@ -44,7 +44,7 @@ struct ext4_xattr_entry {
1059 __u8 e_name_len; /* length of name */
1060 __u8 e_name_index; /* attribute name index */
1061 __le16 e_value_offs; /* offset in disk block of value */
1062 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1063 + __le32 e_value_inum; /* inode in which the value is stored */
1064 __le32 e_value_size; /* size of attribute value */
1065 __le32 e_hash; /* hash value of name and value */
1066 char e_name[0]; /* attribute name */
1067 @@ -69,6 +69,26 @@ struct ext4_xattr_entry {
1068 EXT4_I(inode)->i_extra_isize))
1069 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1072 + * Link EA inode back to parent one using i_mtime field.
1073 + * Extra integer type conversion added to ignore higher
1074 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1076 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1078 + (inode)->i_mtime.tv_sec = inum; \
1081 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1082 +((__u32)(inode)->i_mtime.tv_sec)
1085 + * The minimum size of EA value when you start storing it in an external inode
1086 + * size of block - size of header - size of 1 entry - 4 null bytes
1088 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1089 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1091 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1092 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1093 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1094 @@ -77,10 +97,11 @@ struct ext4_xattr_entry {
1095 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1097 struct ext4_xattr_info {
1106 struct ext4_xattr_search {
1107 @@ -140,7 +161,13 @@ extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
1108 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1109 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1111 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1112 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1114 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1115 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1116 + struct ext4_xattr_ino_array **array);
1117 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1118 + struct ext4_xattr_ino_array *array);
1120 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1121 struct ext4_inode *raw_inode, handle_t *handle);