1 Subject: [PATCH] ext4: xattr-in-inode support
3 Large xattr support is implemented for EXT4_FEATURE_INCOMPAT_EA_INODE.
5 If the size of an xattr value is larger than will fit in a single
6 external block, then the xattr value will be saved into the body
7 of an external xattr inode.
9 The also helps support a larger number of xattr, since only the headers
10 will be stored in the in-inode space or the single external block.
12 The inode is referenced from the xattr header via "e_value_inum",
13 which was formerly "e_value_block", but that field was never used.
14 The e_value_size still contains the xattr size so that listing
15 xattrs does not need to look up the inode if the data is not accessed.
17 struct ext4_xattr_entry {
18 __u8 e_name_len; /* length of name */
19 __u8 e_name_index; /* attribute name index */
20 __le16 e_value_offs; /* offset in disk block of value */
21 __le32 e_value_inum; /* inode in which value is stored */
22 __le32 e_value_size; /* size of attribute value */
23 __le32 e_hash; /* hash value of name and value */
24 char e_name[0]; /* attribute name */
27 The xattr inode is marked with the EXT4_EA_INODE_FL flag and also
28 holds a back-reference to the owning inode in its i_mtime field,
29 allowing the ext4/e2fsck to verify the correct inode is accessed.
31 [ Applied fix by Dan Carpenter to avoid freeing an ERR_PTR. ]
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>
40 Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
45 fs/ext4/inode.c | 49 +++-
46 fs/ext4/xattr.c | 562 ++++++++++++++++++++++++++++++++++++++++++++++++++-----
47 fs/ext4/xattr.h | 33 ++-
48 6 files changed, 604 insertions(+), 55 deletions(-)
52 @@ -1811,6 +1811,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(encrypt, EN
53 EXT4_FEATURE_INCOMPAT_EXTENTS| \
54 EXT4_FEATURE_INCOMPAT_64BIT| \
55 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
56 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
57 EXT4_FEATURE_INCOMPAT_MMP | \
58 EXT4_FEATURE_INCOMPAT_DIRDATA| \
59 EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
60 @@ -2305,6 +2306,12 @@ struct mmpd_data {
61 #define EXT4_MMP_MAX_CHECK_INTERVAL 300UL
64 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
65 + * This limit is arbitrary, but is reasonable for the xattr API.
67 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE (1024 * 1024)
73 @@ -2316,6 +2323,10 @@ struct mmpd_data {
74 # define ATTRIB_NORET __attribute__((noreturn))
75 # define NORET_AND noreturn,
77 +struct ext4_xattr_ino_array {
78 + unsigned int xia_count; /* # of used item in the array */
79 + unsigned int xia_inodes[0];
82 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
83 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
84 @@ -2586,6 +2597,7 @@ extern int ext4_truncate_restart_trans(h
85 extern void ext4_set_inode_flags(struct inode *);
86 extern int ext4_alloc_da_blocks(struct inode *inode);
87 extern void ext4_set_aops(struct inode *inode);
88 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
89 extern int ext4_writepage_trans_blocks(struct inode *);
90 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
91 extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
92 --- a/fs/ext4/ialloc.c
93 +++ b/fs/ext4/ialloc.c
94 @@ -273,7 +273,6 @@ void ext4_free_inode(handle_t *handle, s
95 * as writing the quota to disk may need the lock as well.
97 dquot_initialize(inode);
98 - ext4_xattr_delete_inode(handle, inode);
99 dquot_free_inode(inode);
102 --- a/fs/ext4/inline.c
103 +++ b/fs/ext4/inline.c
104 @@ -62,7 +62,7 @@ static int get_max_inline_xattr_value_si
106 /* Compute min_offs. */
107 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
108 - if (!entry->e_value_block && entry->e_value_size) {
109 + if (!entry->e_value_inum && entry->e_value_size) {
110 size_t offs = le16_to_cpu(entry->e_value_offs);
113 --- a/fs/ext4/inode.c
114 +++ b/fs/ext4/inode.c
115 @@ -140,8 +140,6 @@ static void ext4_invalidatepage(struct p
116 unsigned int length);
117 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
118 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
119 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
123 * Test whether an inode is a fast symlink.
124 @@ -190,6 +188,8 @@ void ext4_evict_inode(struct inode *inod
128 + int extra_credits = 3;
129 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
131 trace_ext4_evict_inode(inode);
133 @@ -240,8 +240,8 @@ void ext4_evict_inode(struct inode *inod
134 * protection against it
136 sb_start_intwrite(inode->i_sb);
137 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
138 - ext4_blocks_for_truncate(inode)+3);
140 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
141 if (IS_ERR(handle)) {
142 ext4_std_error(inode->i_sb, PTR_ERR(handle));
144 @@ -253,9 +253,36 @@ void ext4_evict_inode(struct inode *inod
145 sb_end_intwrite(inode->i_sb);
150 ext4_handle_sync(handle);
153 + * Delete xattr inode before deleting the main inode.
155 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
157 + ext4_warning(inode->i_sb,
158 + "couldn't delete inode's xattr (err %d)", err);
162 + if (!IS_NOQUOTA(inode))
163 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
165 + if (!ext4_handle_has_enough_credits(handle,
166 + ext4_blocks_for_truncate(inode) + extra_credits)) {
167 + err = ext4_journal_extend(handle,
168 + ext4_blocks_for_truncate(inode) + extra_credits);
170 + err = ext4_journal_restart(handle,
171 + ext4_blocks_for_truncate(inode) + extra_credits);
173 + ext4_warning(inode->i_sb,
174 + "couldn't extend journal (err %d)", err);
180 err = ext4_mark_inode_dirty(handle, inode);
182 @@ -279,10 +306,10 @@ void ext4_evict_inode(struct inode *inod
183 * enough credits left in the handle to remove the inode from
184 * the orphan list and set the dtime field.
186 - if (!ext4_handle_has_enough_credits(handle, 3)) {
187 - err = ext4_journal_extend(handle, 3);
188 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
189 + err = ext4_journal_extend(handle, extra_credits);
191 - err = ext4_journal_restart(handle, 3);
192 + err = ext4_journal_restart(handle, extra_credits);
194 ext4_warning(inode->i_sb,
195 "couldn't extend journal (err %d)", err);
196 @@ -317,8 +344,12 @@ void ext4_evict_inode(struct inode *inod
197 ext4_clear_inode(inode);
199 ext4_free_inode(handle, inode);
201 ext4_journal_stop(handle);
202 sb_end_intwrite(inode->i_sb);
204 + if (lea_ino_array != NULL)
205 + ext4_xattr_inode_array_free(inode, lea_ino_array);
208 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
209 @@ -5685,7 +5716,7 @@ static int ext4_index_trans_blocks(struc
211 * Also account for superblock, inode, quota and xattr blocks
213 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
214 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
217 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
218 --- a/fs/ext4/xattr.c
219 +++ b/fs/ext4/xattr.c
220 @@ -180,9 +180,8 @@ ext4_xattr_check_entries(struct ext4_xat
222 /* Check the values */
223 while (!IS_LAST_ENTRY(entry)) {
224 - if (entry->e_value_block != 0)
225 - return -EFSCORRUPTED;
226 - if (entry->e_value_size != 0) {
227 + if (entry->e_value_size != 0 &&
228 + entry->e_value_inum == 0) {
229 u16 offs = le16_to_cpu(entry->e_value_offs);
230 u32 size = le32_to_cpu(entry->e_value_size);
232 @@ -287,6 +286,100 @@ xattr_find_entry(struct inode *inode, st
233 return cmp ? -ENODATA : 0;
237 + * Read the EA value from an inode.
240 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
242 + unsigned long block = 0;
243 + struct buffer_head *bh = NULL;
245 + size_t csize, ret_size = 0;
250 + blocksize = ea_inode->i_sb->s_blocksize;
252 + while (ret_size < *size) {
253 + csize = (*size - ret_size) > blocksize ? blocksize :
255 + bh = ext4_bread(NULL, ea_inode, block, 0);
258 + return PTR_ERR(bh);
260 + memcpy(buf, bh->b_data, csize);
273 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
275 + struct inode *ea_inode = NULL;
277 + ea_inode = ext4_iget(parent->i_sb, ea_ino,
278 + LDISKFS_IGET_HANDLE | LDISKFS_IGET_SPECIAL);
279 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
280 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
281 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
282 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
283 + *err = rc != 0 ? rc : -EIO;
287 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
288 + ea_inode->i_generation != parent->i_generation) {
289 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
290 + "to parent invalid.", ea_ino);
295 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
296 + ext4_error(parent->i_sb, "EA inode %lu does not have "
297 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
311 + * Read the value from the EA inode.
314 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
317 + struct inode *ea_inode = NULL;
320 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
324 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
331 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
332 void *buffer, size_t buffer_size)
333 @@ -325,8 +418,16 @@ ext4_xattr_block_get(struct inode *inode
335 if (size > buffer_size)
337 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
339 + if (entry->e_value_inum) {
340 + error = ext4_xattr_inode_get(inode,
341 + le32_to_cpu(entry->e_value_inum),
346 + memcpy(buffer, bh->b_data +
347 + le16_to_cpu(entry->e_value_offs), size);
352 @@ -367,8 +468,16 @@ ext4_xattr_ibody_get(struct inode *inode
354 if (size > buffer_size)
356 - memcpy(buffer, (void *)IFIRST(header) +
357 - le16_to_cpu(entry->e_value_offs), size);
358 + if (entry->e_value_inum) {
359 + error = ext4_xattr_inode_get(inode,
360 + le32_to_cpu(entry->e_value_inum),
365 + memcpy(buffer, (void *)IFIRST(header) +
366 + le16_to_cpu(entry->e_value_offs), size);
371 @@ -634,7 +743,7 @@ static size_t ext4_xattr_free_space(stru
372 size_t *min_offs, void *base, int *total)
374 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
375 - if (last->e_value_size) {
376 + if (!last->e_value_inum && last->e_value_size) {
377 size_t offs = le16_to_cpu(last->e_value_offs);
378 if (offs < *min_offs)
380 @@ -645,11 +754,166 @@ static size_t ext4_xattr_free_space(stru
381 return (*min_offs - ((void *)last - base) - sizeof(__u32));
385 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
387 + * Write the value of the EA in an inode.
389 +static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
390 + const void *buf, int bufsize)
392 + struct buffer_head *bh = NULL;
393 + unsigned long block = 0;
394 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
395 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
396 + int csize, wsize = 0;
401 + while (ret >= 0 && ret < max_blocks) {
402 + struct ext4_map_blocks map;
403 + map.m_lblk = block += ret;
404 + map.m_len = max_blocks -= ret;
406 + ret = ext4_map_blocks(handle, ea_inode, &map,
407 + EXT4_GET_BLOCKS_CREATE);
409 + ext4_mark_inode_dirty(handle, ea_inode);
410 + if (ret == -ENOSPC &&
411 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
423 + while (wsize < bufsize) {
426 + csize = (bufsize - wsize) > blocksize ? blocksize :
428 + bh = ext4_getblk(handle, ea_inode, block, 0);
430 + return PTR_ERR(bh);
431 + ret = ext4_journal_get_write_access(handle, bh);
435 + memcpy(bh->b_data, buf, csize);
436 + set_buffer_uptodate(bh);
437 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
444 + inode_lock(ea_inode);
445 + i_size_write(ea_inode, wsize);
446 + ext4_update_i_disksize(ea_inode, wsize);
447 + inode_unlock(ea_inode);
449 + ext4_mark_inode_dirty(handle, ea_inode);
458 + * Create an inode to store the value of a large EA.
460 +static struct inode *ext4_xattr_inode_create(handle_t *handle,
461 + struct inode *inode)
463 + struct inode *ea_inode = NULL;
466 + * Let the next inode be the goal, so we try and allocate the EA inode
467 + * in the same group, or nearby one.
469 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
470 + S_IFREG | 0600, NULL, inode->i_ino + 1, NULL);
471 + if (!IS_ERR(ea_inode)) {
472 + ea_inode->i_op = &ext4_file_inode_operations;
473 + ea_inode->i_fop = &ext4_file_operations;
474 + ext4_set_aops(ea_inode);
475 + ea_inode->i_generation = inode->i_generation;
476 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
479 + * A back-pointer from EA inode to parent inode will be useful
482 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
483 + unlock_new_inode(ea_inode);
490 + * Unlink the inode storing the value of the EA.
492 +int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
494 + struct inode *ea_inode = NULL;
497 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
501 + clear_nlink(ea_inode);
508 + * Add value of the EA in an inode.
510 +static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
511 + unsigned long *ea_ino, const void *value,
514 + struct inode *ea_inode;
517 + /* Create an inode for the EA value */
518 + ea_inode = ext4_xattr_inode_create(handle, inode);
519 + if (IS_ERR(ea_inode))
520 + return PTR_ERR(ea_inode);
522 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
524 + clear_nlink(ea_inode);
526 + *ea_ino = ea_inode->i_ino;
533 +static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
534 + struct ext4_xattr_search *s,
535 + handle_t *handle, struct inode *inode)
537 struct ext4_xattr_entry *last, *next;
538 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
539 + int in_inode = i->in_inode;
542 + if (ext4_has_feature_ea_inode(inode->i_sb) &&
543 + (EXT4_XATTR_SIZE(i->value_len) >
544 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
547 /* Compute min_offs and last. */
549 @@ -657,7 +921,7 @@ ext4_xattr_set_entry(struct ext4_xattr_i
550 next = EXT4_XATTR_NEXT(last);
551 if ((void *)next >= s->end)
552 return -EFSCORRUPTED;
553 - if (last->e_value_size) {
554 + if (!last->e_value_inum && last->e_value_size) {
555 size_t offs = le16_to_cpu(last->e_value_offs);
558 @@ -665,15 +929,20 @@ ext4_xattr_set_entry(struct ext4_xattr_i
560 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
562 - if (s->here->e_value_size) {
564 + !s->here->e_value_inum && s->here->e_value_size) {
565 size_t size = le32_to_cpu(s->here->e_value_size);
566 free += EXT4_XATTR_SIZE(size);
568 free += EXT4_XATTR_LEN(name_len);
571 - if (free < EXT4_XATTR_LEN(name_len) +
572 - EXT4_XATTR_SIZE(i->value_len))
573 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
578 + if (free < EXT4_XATTR_LEN(name_len) + value_len)
582 @@ -687,7 +956,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
583 s->here->e_name_len = name_len;
584 memcpy(s->here->e_name, i->name, name_len);
586 - if (s->here->e_value_size) {
587 + if (!s->here->e_value_inum && s->here->e_value_size &&
588 + s->here->e_value_offs > 0) {
589 void *first_val = s->base + min_offs;
590 size_t offs = le16_to_cpu(s->here->e_value_offs);
591 void *val = s->base + offs;
592 @@ -721,12 +991,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
594 while (!IS_LAST_ENTRY(last)) {
595 size_t o = le16_to_cpu(last->e_value_offs);
596 - if (last->e_value_size && o < offs)
597 + if (!last->e_value_inum &&
598 + last->e_value_size && o < offs)
600 cpu_to_le16(o + size);
601 last = EXT4_XATTR_NEXT(last);
604 + if (s->here->e_value_inum) {
605 + ext4_xattr_inode_unlink(inode,
606 + le32_to_cpu(s->here->e_value_inum));
607 + s->here->e_value_inum = 0;
610 /* Remove the old name. */
611 size_t size = EXT4_XATTR_LEN(name_len);
612 @@ -739,11 +1015,20 @@ ext4_xattr_set_entry(struct ext4_xattr_i
615 /* Insert the new value. */
616 - s->here->e_value_size = cpu_to_le32(i->value_len);
617 - if (i->value_len) {
619 + unsigned long ea_ino =
620 + le32_to_cpu(s->here->e_value_inum);
621 + rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
622 + i->value, i->value_len);
625 + s->here->e_value_inum = cpu_to_le32(ea_ino);
626 + s->here->e_value_offs = 0;
627 + } else if (i->value_len) {
628 size_t size = EXT4_XATTR_SIZE(i->value_len);
629 void *val = s->base + min_offs - size;
630 s->here->e_value_offs = cpu_to_le16(min_offs - size);
631 + s->here->e_value_inum = 0;
632 if (i->value == EXT4_ZERO_XATTR_VALUE) {
633 memset(val, 0, size);
635 @@ -753,8 +1038,11 @@ ext4_xattr_set_entry(struct ext4_xattr_i
636 memcpy(val, i->value, i->value_len);
639 + s->here->e_value_size = cpu_to_le32(i->value_len);
647 struct ext4_xattr_block_find {
648 @@ -815,8 +1103,6 @@ ext4_xattr_block_set(handle_t *handle, s
650 #define header(x) ((struct ext4_xattr_header *)(x))
652 - if (i->value && i->value_len > sb->s_blocksize)
655 BUFFER_TRACE(bs->bh, "get_write_access");
656 error = ext4_journal_get_write_access(handle, bs->bh);
657 @@ -835,7 +1121,7 @@ ext4_xattr_block_set(handle_t *handle, s
658 mb_cache_entry_delete_block(ext4_mb_cache, hash,
660 ea_bdebug(bs->bh, "modifying in-place");
661 - error = ext4_xattr_set_entry(i, s);
662 + error = ext4_xattr_set_entry(i, s, handle, inode);
664 if (!IS_LAST_ENTRY(s->first))
665 ext4_xattr_rehash(header(s->base),
666 @@ -884,7 +1170,7 @@ ext4_xattr_block_set(handle_t *handle, s
667 s->end = s->base + sb->s_blocksize;
670 - error = ext4_xattr_set_entry(i, s);
671 + error = ext4_xattr_set_entry(i, s, handle, inode);
672 if (error == -EFSCORRUPTED)
675 @@ -1084,7 +1370,7 @@ int ext4_xattr_ibody_inline_set(handle_t
677 if (EXT4_I(inode)->i_extra_isize == 0)
679 - error = ext4_xattr_set_entry(i, s);
680 + error = ext4_xattr_set_entry(i, s, handle, inode);
683 header = IHDR(inode, ext4_raw_inode(&is->iloc));
684 @@ -1098,7 +1384,7 @@ int ext4_xattr_ibody_inline_set(handle_t
688 -static int ext4_xattr_ibody_set(struct inode *inode,
689 +static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
690 struct ext4_xattr_info *i,
691 struct ext4_xattr_ibody_find *is)
693 @@ -1108,7 +1394,7 @@ static int ext4_xattr_ibody_set(struct i
695 if (EXT4_I(inode)->i_extra_isize == 0)
697 - error = ext4_xattr_set_entry(i, s);
698 + error = ext4_xattr_set_entry(i, s, handle, inode);
701 header = IHDR(inode, ext4_raw_inode(&is->iloc));
702 @@ -1155,7 +1441,7 @@ ext4_xattr_set_handle(handle_t *handle,
705 .value_len = value_len,
709 struct ext4_xattr_ibody_find is = {
710 .s = { .not_found = -ENODATA, },
711 @@ -1204,7 +1490,7 @@ ext4_xattr_set_handle(handle_t *handle,
715 - error = ext4_xattr_ibody_set(inode, &i, &is);
716 + error = ext4_xattr_ibody_set(handle, inode, &i, &is);
717 else if (!bs.s.not_found)
718 error = ext4_xattr_block_set(handle, inode, &i, &bs);
720 @@ -1215,7 +1501,7 @@ ext4_xattr_set_handle(handle_t *handle,
721 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
724 - error = ext4_xattr_ibody_set(inode, &i, &is);
725 + error = ext4_xattr_ibody_set(handle, inode, &i, &is);
726 if (!error && !bs.s.not_found) {
728 error = ext4_xattr_block_set(handle, inode, &i, &bs);
729 @@ -1228,11 +1514,20 @@ ext4_xattr_set_handle(handle_t *handle,
732 error = ext4_xattr_block_set(handle, inode, &i, &bs);
733 + if (ext4_has_feature_ea_inode(inode->i_sb) &&
734 + error == -ENOSPC) {
735 + /* xattr not fit to block, store at external
738 + error = ext4_xattr_ibody_set(handle, inode,
743 if (!is.s.not_found) {
745 - error = ext4_xattr_ibody_set(inode, &i, &is);
746 + error = ext4_xattr_ibody_set(handle, inode, &i,
751 @@ -1271,12 +1566,26 @@ ext4_xattr_set(struct inode *inode, int
752 const void *value, size_t value_len, int flags)
755 + struct super_block *sb = inode->i_sb;
756 int error, retries = 0;
757 int credits = ext4_jbd2_credits_xattr(inode);
759 error = dquot_initialize(inode);
763 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
764 + ext4_has_feature_ea_inode(sb)) {
765 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
766 + sb->s_blocksize_bits;
768 + /* For new inode */
769 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
771 + /* For data blocks of EA inode */
772 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
776 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
777 if (IS_ERR(handle)) {
778 @@ -1288,7 +1597,7 @@ retry:
779 value, value_len, flags);
780 error2 = ext4_journal_stop(handle);
781 if (error == -ENOSPC &&
782 - ext4_should_retry_alloc(inode->i_sb, &retries))
783 + ext4_should_retry_alloc(sb, &retries))
787 @@ -1313,7 +1622,7 @@ static void ext4_xattr_shift_entries(str
789 /* Adjust the value offsets of the entries */
790 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
791 - if (last->e_value_size) {
792 + if (!last->e_value_inum && last->e_value_size) {
793 new_offs = le16_to_cpu(last->e_value_offs) +
795 last->e_value_offs = cpu_to_le16(new_offs);
796 @@ -1374,7 +1683,7 @@ static int ext4_xattr_move_to_block(hand
799 /* Remove the chosen entry from the inode */
800 - error = ext4_xattr_ibody_set(inode, &i, is);
801 + error = ext4_xattr_ibody_set(handle, inode, &i, is);
805 @@ -1578,21 +1887,135 @@ cleanup:
809 +#define EIA_INCR 16 /* must be 2^n */
810 +#define EIA_MASK (EIA_INCR - 1)
811 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
812 + * If @lea_ino_array is new or full it will be grown and the old
813 + * contents copied over.
816 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
818 + if (*lea_ino_array == NULL) {
820 + * Start with 15 inodes, so it fits into a power-of-two size.
821 + * If *lea_ino_array is NULL, this is essentially offsetof()
824 + kmalloc(offsetof(struct ext4_xattr_ino_array,
825 + xia_inodes[EIA_MASK]),
827 + if (*lea_ino_array == NULL)
829 + (*lea_ino_array)->xia_count = 0;
830 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
831 + /* expand the array once all 15 + n * 16 slots are full */
832 + struct ext4_xattr_ino_array *new_array = NULL;
833 + int count = (*lea_ino_array)->xia_count;
835 + /* if new_array is NULL, this is essentially offsetof() */
836 + new_array = kmalloc(
837 + offsetof(struct ext4_xattr_ino_array,
838 + xia_inodes[count + EIA_INCR]),
840 + if (new_array == NULL)
842 + memcpy(new_array, *lea_ino_array,
843 + offsetof(struct ext4_xattr_ino_array,
844 + xia_inodes[count]));
845 + kfree(*lea_ino_array);
846 + *lea_ino_array = new_array;
848 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
853 + * Add xattr inode to orphan list
856 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
857 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
859 + struct inode *ea_inode = NULL;
860 + int idx = 0, error = 0;
862 + if (lea_ino_array == NULL)
865 + for (; idx < lea_ino_array->xia_count; ++idx) {
866 + if (!ext4_handle_has_enough_credits(handle, credits)) {
867 + error = ext4_journal_extend(handle, credits);
869 + error = ext4_journal_restart(handle, credits);
872 + ext4_warning(inode->i_sb,
873 + "couldn't extend journal "
874 + "(err %d)", error);
878 + ea_inode = ext4_xattr_inode_iget(inode,
879 + lea_ino_array->xia_inodes[idx], &error);
882 + ext4_orphan_add(handle, ea_inode);
883 + /* the inode's i_count will be released by caller */
890 * ext4_xattr_delete_inode()
892 - * Free extended attribute resources associated with this inode. This
893 + * Free extended attribute resources associated with this inode. Traverse
894 + * all entries and unlink any xattr inodes associated with this inode. This
895 * is called immediately before an inode is freed. We have exclusive
896 - * access to the inode.
897 + * access to the inode. If an orphan inode is deleted it will also delete any
898 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
899 + * to ensure they belong to the parent inode and were not deleted already.
902 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
904 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
905 + struct ext4_xattr_ino_array **lea_ino_array)
907 struct buffer_head *bh = NULL;
908 + struct ext4_xattr_ibody_header *header;
909 + struct ext4_inode *raw_inode;
910 + struct ext4_iloc iloc;
911 + struct ext4_xattr_entry *entry;
912 + int credits = 3, error = 0;
914 - if (!EXT4_I(inode)->i_file_acl)
915 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
916 + goto delete_external_ea;
918 + error = ext4_get_inode_loc(inode, &iloc);
921 + raw_inode = ext4_raw_inode(&iloc);
922 + header = IHDR(inode, raw_inode);
923 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
924 + entry = EXT4_XATTR_NEXT(entry)) {
925 + if (!entry->e_value_inum)
927 + if (ext4_expand_ino_array(lea_ino_array,
928 + entry->e_value_inum) != 0) {
932 + entry->e_value_inum = 0;
937 + if (!EXT4_I(inode)->i_file_acl) {
938 + /* add xattr inode to orphan list */
939 + ext4_xattr_inode_orphan_add(handle, inode, credits,
943 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
945 EXT4_ERROR_INODE(inode, "block %llu read error",
946 @@ -1605,11 +2028,69 @@ ext4_xattr_delete_inode(handle_t *handle
947 EXT4_I(inode)->i_file_acl);
951 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
952 + entry = EXT4_XATTR_NEXT(entry)) {
953 + if (!entry->e_value_inum)
955 + if (ext4_expand_ino_array(lea_ino_array,
956 + entry->e_value_inum) != 0)
958 + entry->e_value_inum = 0;
961 + /* add xattr inode to orphan list */
962 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
967 + if (!IS_NOQUOTA(inode))
968 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
970 + if (!ext4_handle_has_enough_credits(handle, credits)) {
971 + error = ext4_journal_extend(handle, credits);
973 + error = ext4_journal_restart(handle, credits);
975 + ext4_warning(inode->i_sb,
976 + "couldn't extend journal (err %d)", error);
981 ext4_xattr_release_block(handle, inode, bh);
982 EXT4_I(inode)->i_file_acl = 0;
991 +ext4_xattr_inode_array_free(struct inode *inode,
992 + struct ext4_xattr_ino_array *lea_ino_array)
994 + struct inode *ea_inode = NULL;
998 + if (lea_ino_array == NULL)
1001 + for (; idx < lea_ino_array->xia_count; ++idx) {
1002 + ea_inode = ext4_xattr_inode_iget(inode,
1003 + lea_ino_array->xia_inodes[idx], &err);
1006 + /* for inode's i_count get from ext4_xattr_delete_inode */
1007 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
1009 + clear_nlink(ea_inode);
1012 + kfree(lea_ino_array);
1016 @@ -1661,10 +2142,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
1017 entry1->e_name_index != entry2->e_name_index ||
1018 entry1->e_name_len != entry2->e_name_len ||
1019 entry1->e_value_size != entry2->e_value_size ||
1020 + entry1->e_value_inum != entry2->e_value_inum ||
1021 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1023 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1024 - return -EFSCORRUPTED;
1025 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1026 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1027 le32_to_cpu(entry1->e_value_size)))
1028 @@ -1736,7 +2216,7 @@ static inline void ext4_xattr_hash_entry
1032 - if (entry->e_value_size != 0) {
1033 + if (!entry->e_value_inum && entry->e_value_size) {
1034 __le32 *value = (__le32 *)((char *)header +
1035 le16_to_cpu(entry->e_value_offs));
1036 for (n = (le32_to_cpu(entry->e_value_size) +
1037 --- a/fs/ext4/xattr.h
1038 +++ b/fs/ext4/xattr.h
1039 @@ -44,7 +44,7 @@ struct ext4_xattr_entry {
1040 __u8 e_name_len; /* length of name */
1041 __u8 e_name_index; /* attribute name index */
1042 __le16 e_value_offs; /* offset in disk block of value */
1043 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1044 + __le32 e_value_inum; /* inode in which the value is stored */
1045 __le32 e_value_size; /* size of attribute value */
1046 __le32 e_hash; /* hash value of name and value */
1047 char e_name[0]; /* attribute name */
1048 @@ -69,6 +69,26 @@ struct ext4_xattr_entry {
1049 EXT4_I(inode)->i_extra_isize))
1050 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1053 + * Link EA inode back to parent one using i_mtime field.
1054 + * Extra integer type conversion added to ignore higher
1055 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1057 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1059 + (inode)->i_mtime.tv_sec = inum; \
1062 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1063 +((__u32)(inode)->i_mtime.tv_sec)
1066 + * The minimum size of EA value when you start storing it in an external inode
1067 + * size of block - size of header - size of 1 entry - 4 null bytes
1069 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1070 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1072 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1073 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1074 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1075 @@ -77,10 +97,11 @@ struct ext4_xattr_entry {
1076 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1078 struct ext4_xattr_info {
1087 struct ext4_xattr_search {
1088 @@ -140,7 +161,13 @@ extern int ext4_xattr_get(struct inode *
1089 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1090 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1092 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1093 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1095 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1096 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1097 + struct ext4_xattr_ino_array **array);
1098 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1099 + struct ext4_xattr_ino_array *array);
1101 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1102 struct ext4_inode *raw_inode, handle_t *handle);