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>
43 fs/ext4/ialloc.c | 1 -
44 fs/ext4/inline.c | 2 +-
45 fs/ext4/inode.c | 49 ++++-
46 fs/ext4/xattr.c | 561 +++++++++++++++++++++++++++++++++++++++++++----
47 fs/ext4/xattr.h | 33 ++-
48 6 files changed, 603 insertions(+), 55 deletions(-)
50 Index: linux-stage/fs/ext4/ext4.h
51 ===================================================================
52 --- linux-stage.orig/fs/ext4/ext4.h
53 +++ linux-stage/fs/ext4/ext4.h
54 @@ -1811,6 +1811,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(encrypt, EN
55 EXT4_FEATURE_INCOMPAT_EXTENTS| \
56 EXT4_FEATURE_INCOMPAT_64BIT| \
57 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
58 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
59 EXT4_FEATURE_INCOMPAT_MMP | \
60 EXT4_FEATURE_INCOMPAT_DIRDATA| \
61 EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
62 @@ -2305,6 +2306,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)
75 @@ -2316,6 +2323,10 @@ struct mmpd_data {
76 # define ATTRIB_NORET __attribute__((noreturn))
77 # define NORET_AND noreturn,
79 +struct ext4_xattr_ino_array {
80 + unsigned int xia_count; /* # of used item in the array */
81 + unsigned int xia_inodes[0];
84 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
85 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
86 @@ -2572,6 +2583,7 @@ extern int ext4_truncate_restart_trans(h
87 extern void ext4_set_inode_flags(struct inode *);
88 extern int ext4_alloc_da_blocks(struct inode *inode);
89 extern void ext4_set_aops(struct inode *inode);
90 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
91 extern int ext4_writepage_trans_blocks(struct inode *);
92 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
93 extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
94 Index: linux-stage/fs/ext4/ialloc.c
95 ===================================================================
96 --- linux-stage.orig/fs/ext4/ialloc.c
97 +++ linux-stage/fs/ext4/ialloc.c
98 @@ -273,7 +273,6 @@ void ext4_free_inode(handle_t *handle, s
99 * as writing the quota to disk may need the lock as well.
101 dquot_initialize(inode);
102 - ext4_xattr_delete_inode(handle, inode);
103 dquot_free_inode(inode);
106 Index: linux-stage/fs/ext4/inline.c
107 ===================================================================
108 --- linux-stage.orig/fs/ext4/inline.c
109 +++ linux-stage/fs/ext4/inline.c
110 @@ -62,7 +62,7 @@ static int get_max_inline_xattr_value_si
112 /* Compute min_offs. */
113 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
114 - if (!entry->e_value_block && entry->e_value_size) {
115 + if (!entry->e_value_inum && entry->e_value_size) {
116 size_t offs = le16_to_cpu(entry->e_value_offs);
119 Index: linux-stage/fs/ext4/inode.c
120 ===================================================================
121 --- linux-stage.orig/fs/ext4/inode.c
122 +++ linux-stage/fs/ext4/inode.c
123 @@ -140,8 +140,6 @@ static void ext4_invalidatepage(struct p
124 unsigned int length);
125 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
126 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
127 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
131 * Test whether an inode is a fast symlink.
132 @@ -190,6 +188,8 @@ void ext4_evict_inode(struct inode *inod
136 + int extra_credits = 3;
137 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
139 trace_ext4_evict_inode(inode);
141 @@ -240,8 +240,8 @@ void ext4_evict_inode(struct inode *inod
142 * protection against it
144 sb_start_intwrite(inode->i_sb);
145 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
146 - ext4_blocks_for_truncate(inode)+3);
148 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
149 if (IS_ERR(handle)) {
150 ext4_std_error(inode->i_sb, PTR_ERR(handle));
152 @@ -253,9 +253,36 @@ void ext4_evict_inode(struct inode *inod
153 sb_end_intwrite(inode->i_sb);
158 ext4_handle_sync(handle);
161 + * Delete xattr inode before deleting the main inode.
163 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
165 + ext4_warning(inode->i_sb,
166 + "couldn't delete inode's xattr (err %d)", err);
170 + if (!IS_NOQUOTA(inode))
171 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
173 + if (!ext4_handle_has_enough_credits(handle,
174 + ext4_blocks_for_truncate(inode) + extra_credits)) {
175 + err = ext4_journal_extend(handle,
176 + ext4_blocks_for_truncate(inode) + extra_credits);
178 + err = ext4_journal_restart(handle,
179 + ext4_blocks_for_truncate(inode) + extra_credits);
181 + ext4_warning(inode->i_sb,
182 + "couldn't extend journal (err %d)", err);
188 err = ext4_mark_inode_dirty(handle, inode);
190 @@ -279,10 +306,10 @@ void ext4_evict_inode(struct inode *inod
191 * enough credits left in the handle to remove the inode from
192 * the orphan list and set the dtime field.
194 - if (!ext4_handle_has_enough_credits(handle, 3)) {
195 - err = ext4_journal_extend(handle, 3);
196 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
197 + err = ext4_journal_extend(handle, extra_credits);
199 - err = ext4_journal_restart(handle, 3);
200 + err = ext4_journal_restart(handle, extra_credits);
202 ext4_warning(inode->i_sb,
203 "couldn't extend journal (err %d)", err);
204 @@ -317,8 +344,12 @@ void ext4_evict_inode(struct inode *inod
205 ext4_clear_inode(inode);
207 ext4_free_inode(handle, inode);
209 ext4_journal_stop(handle);
210 sb_end_intwrite(inode->i_sb);
212 + if (lea_ino_array != NULL)
213 + ext4_xattr_inode_array_free(inode, lea_ino_array);
216 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
217 @@ -5627,7 +5658,7 @@ static int ext4_index_trans_blocks(struc
219 * Also account for superblock, inode, quota and xattr blocks
221 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
222 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
225 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
226 Index: linux-stage/fs/ext4/xattr.c
227 ===================================================================
228 --- linux-stage.orig/fs/ext4/xattr.c
229 +++ linux-stage/fs/ext4/xattr.c
230 @@ -180,9 +180,8 @@ ext4_xattr_check_entries(struct ext4_xat
232 /* Check the values */
233 while (!IS_LAST_ENTRY(entry)) {
234 - if (entry->e_value_block != 0)
235 - return -EFSCORRUPTED;
236 - if (entry->e_value_size != 0) {
237 + if (entry->e_value_size != 0 &&
238 + entry->e_value_inum == 0) {
239 u16 offs = le16_to_cpu(entry->e_value_offs);
240 u32 size = le32_to_cpu(entry->e_value_size);
242 @@ -287,6 +286,99 @@ xattr_find_entry(struct inode *inode, st
243 return cmp ? -ENODATA : 0;
247 + * Read the EA value from an inode.
250 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
252 + unsigned long block = 0;
253 + struct buffer_head *bh = NULL;
255 + size_t csize, ret_size = 0;
260 + blocksize = ea_inode->i_sb->s_blocksize;
262 + while (ret_size < *size) {
263 + csize = (*size - ret_size) > blocksize ? blocksize :
265 + bh = ext4_bread(NULL, ea_inode, block, 0);
268 + return PTR_ERR(bh);
270 + memcpy(buf, bh->b_data, csize);
283 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
285 + struct inode *ea_inode = NULL;
287 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
288 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
289 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
290 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
291 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
292 + *err = rc != 0 ? rc : -EIO;
296 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
297 + ea_inode->i_generation != parent->i_generation) {
298 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
299 + "to parent invalid.", ea_ino);
304 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
305 + ext4_error(parent->i_sb, "EA inode %lu does not have "
306 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
320 + * Read the value from the EA inode.
323 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
326 + struct inode *ea_inode = NULL;
329 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
333 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
340 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
341 void *buffer, size_t buffer_size)
342 @@ -325,8 +417,16 @@ ext4_xattr_block_get(struct inode *inode
344 if (size > buffer_size)
346 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
348 + if (entry->e_value_inum) {
349 + error = ext4_xattr_inode_get(inode,
350 + le32_to_cpu(entry->e_value_inum),
355 + memcpy(buffer, bh->b_data +
356 + le16_to_cpu(entry->e_value_offs), size);
361 @@ -367,8 +467,16 @@ ext4_xattr_ibody_get(struct inode *inode
363 if (size > buffer_size)
365 - memcpy(buffer, (void *)IFIRST(header) +
366 - le16_to_cpu(entry->e_value_offs), size);
367 + if (entry->e_value_inum) {
368 + error = ext4_xattr_inode_get(inode,
369 + le32_to_cpu(entry->e_value_inum),
374 + memcpy(buffer, (void *)IFIRST(header) +
375 + le16_to_cpu(entry->e_value_offs), size);
380 @@ -634,7 +742,7 @@ static size_t ext4_xattr_free_space(stru
381 size_t *min_offs, void *base, int *total)
383 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
384 - if (last->e_value_size) {
385 + if (!last->e_value_inum && last->e_value_size) {
386 size_t offs = le16_to_cpu(last->e_value_offs);
387 if (offs < *min_offs)
389 @@ -645,11 +753,166 @@ static size_t ext4_xattr_free_space(stru
390 return (*min_offs - ((void *)last - base) - sizeof(__u32));
394 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
396 + * Write the value of the EA in an inode.
398 +static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
399 + const void *buf, int bufsize)
401 + struct buffer_head *bh = NULL;
402 + unsigned long block = 0;
403 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
404 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
405 + int csize, wsize = 0;
410 + while (ret >= 0 && ret < max_blocks) {
411 + struct ext4_map_blocks map;
412 + map.m_lblk = block += ret;
413 + map.m_len = max_blocks -= ret;
415 + ret = ext4_map_blocks(handle, ea_inode, &map,
416 + EXT4_GET_BLOCKS_CREATE);
418 + ext4_mark_inode_dirty(handle, ea_inode);
419 + if (ret == -ENOSPC &&
420 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
432 + while (wsize < bufsize) {
435 + csize = (bufsize - wsize) > blocksize ? blocksize :
437 + bh = ext4_getblk(handle, ea_inode, block, 0);
439 + return PTR_ERR(bh);
440 + ret = ext4_journal_get_write_access(handle, bh);
444 + memcpy(bh->b_data, buf, csize);
445 + set_buffer_uptodate(bh);
446 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
453 + inode_lock(ea_inode);
454 + i_size_write(ea_inode, wsize);
455 + ext4_update_i_disksize(ea_inode, wsize);
456 + inode_unlock(ea_inode);
458 + ext4_mark_inode_dirty(handle, ea_inode);
467 + * Create an inode to store the value of a large EA.
469 +static struct inode *ext4_xattr_inode_create(handle_t *handle,
470 + struct inode *inode)
472 + struct inode *ea_inode = NULL;
475 + * Let the next inode be the goal, so we try and allocate the EA inode
476 + * in the same group, or nearby one.
478 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
479 + S_IFREG | 0600, NULL, inode->i_ino + 1, NULL);
480 + if (!IS_ERR(ea_inode)) {
481 + ea_inode->i_op = &ext4_file_inode_operations;
482 + ea_inode->i_fop = &ext4_file_operations;
483 + ext4_set_aops(ea_inode);
484 + ea_inode->i_generation = inode->i_generation;
485 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
488 + * A back-pointer from EA inode to parent inode will be useful
491 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
492 + unlock_new_inode(ea_inode);
499 + * Unlink the inode storing the value of the EA.
501 +int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
503 + struct inode *ea_inode = NULL;
506 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
510 + clear_nlink(ea_inode);
517 + * Add value of the EA in an inode.
519 +static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
520 + unsigned long *ea_ino, const void *value,
523 + struct inode *ea_inode;
526 + /* Create an inode for the EA value */
527 + ea_inode = ext4_xattr_inode_create(handle, inode);
528 + if (IS_ERR(ea_inode))
529 + return PTR_ERR(ea_inode);
531 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
533 + clear_nlink(ea_inode);
535 + *ea_ino = ea_inode->i_ino;
542 +static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
543 + struct ext4_xattr_search *s,
544 + handle_t *handle, struct inode *inode)
546 struct ext4_xattr_entry *last, *next;
547 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
548 + int in_inode = i->in_inode;
551 + if (ext4_has_feature_ea_inode(inode->i_sb) &&
552 + (EXT4_XATTR_SIZE(i->value_len) >
553 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
556 /* Compute min_offs and last. */
558 @@ -657,7 +920,7 @@ ext4_xattr_set_entry(struct ext4_xattr_i
559 next = EXT4_XATTR_NEXT(last);
560 if ((void *)next >= s->end)
561 return -EFSCORRUPTED;
562 - if (last->e_value_size) {
563 + if (!last->e_value_inum && last->e_value_size) {
564 size_t offs = le16_to_cpu(last->e_value_offs);
567 @@ -665,15 +928,20 @@ ext4_xattr_set_entry(struct ext4_xattr_i
569 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
571 - if (s->here->e_value_size) {
573 + !s->here->e_value_inum && s->here->e_value_size) {
574 size_t size = le32_to_cpu(s->here->e_value_size);
575 free += EXT4_XATTR_SIZE(size);
577 free += EXT4_XATTR_LEN(name_len);
580 - if (free < EXT4_XATTR_LEN(name_len) +
581 - EXT4_XATTR_SIZE(i->value_len))
582 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
587 + if (free < EXT4_XATTR_LEN(name_len) + value_len)
591 @@ -687,7 +955,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
592 s->here->e_name_len = name_len;
593 memcpy(s->here->e_name, i->name, name_len);
595 - if (s->here->e_value_size) {
596 + if (!s->here->e_value_inum && s->here->e_value_size &&
597 + s->here->e_value_offs > 0) {
598 void *first_val = s->base + min_offs;
599 size_t offs = le16_to_cpu(s->here->e_value_offs);
600 void *val = s->base + offs;
601 @@ -721,12 +990,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
603 while (!IS_LAST_ENTRY(last)) {
604 size_t o = le16_to_cpu(last->e_value_offs);
605 - if (last->e_value_size && o < offs)
606 + if (!last->e_value_inum &&
607 + last->e_value_size && o < offs)
609 cpu_to_le16(o + size);
610 last = EXT4_XATTR_NEXT(last);
613 + if (s->here->e_value_inum) {
614 + ext4_xattr_inode_unlink(inode,
615 + le32_to_cpu(s->here->e_value_inum));
616 + s->here->e_value_inum = 0;
619 /* Remove the old name. */
620 size_t size = EXT4_XATTR_LEN(name_len);
621 @@ -739,11 +1014,20 @@ ext4_xattr_set_entry(struct ext4_xattr_i
624 /* Insert the new value. */
625 - s->here->e_value_size = cpu_to_le32(i->value_len);
626 - if (i->value_len) {
628 + unsigned long ea_ino =
629 + le32_to_cpu(s->here->e_value_inum);
630 + rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
631 + i->value, i->value_len);
634 + s->here->e_value_inum = cpu_to_le32(ea_ino);
635 + s->here->e_value_offs = 0;
636 + } else if (i->value_len) {
637 size_t size = EXT4_XATTR_SIZE(i->value_len);
638 void *val = s->base + min_offs - size;
639 s->here->e_value_offs = cpu_to_le16(min_offs - size);
640 + s->here->e_value_inum = 0;
641 if (i->value == EXT4_ZERO_XATTR_VALUE) {
642 memset(val, 0, size);
644 @@ -753,8 +1037,11 @@ ext4_xattr_set_entry(struct ext4_xattr_i
645 memcpy(val, i->value, i->value_len);
648 + s->here->e_value_size = cpu_to_le32(i->value_len);
656 struct ext4_xattr_block_find {
657 @@ -815,8 +1102,6 @@ ext4_xattr_block_set(handle_t *handle, s
659 #define header(x) ((struct ext4_xattr_header *)(x))
661 - if (i->value && i->value_len > sb->s_blocksize)
664 BUFFER_TRACE(bs->bh, "get_write_access");
665 error = ext4_journal_get_write_access(handle, bs->bh);
666 @@ -835,7 +1120,7 @@ ext4_xattr_block_set(handle_t *handle, s
667 mb_cache_entry_delete_block(ext4_mb_cache, hash,
669 ea_bdebug(bs->bh, "modifying in-place");
670 - error = ext4_xattr_set_entry(i, s);
671 + error = ext4_xattr_set_entry(i, s, handle, inode);
673 if (!IS_LAST_ENTRY(s->first))
674 ext4_xattr_rehash(header(s->base),
675 @@ -884,7 +1169,7 @@ ext4_xattr_block_set(handle_t *handle, s
676 s->end = s->base + sb->s_blocksize;
679 - error = ext4_xattr_set_entry(i, s);
680 + error = ext4_xattr_set_entry(i, s, handle, inode);
681 if (error == -EFSCORRUPTED)
684 @@ -1084,7 +1369,7 @@ int ext4_xattr_ibody_inline_set(handle_t
686 if (EXT4_I(inode)->i_extra_isize == 0)
688 - error = ext4_xattr_set_entry(i, s);
689 + error = ext4_xattr_set_entry(i, s, handle, inode);
692 header = IHDR(inode, ext4_raw_inode(&is->iloc));
693 @@ -1098,7 +1383,7 @@ int ext4_xattr_ibody_inline_set(handle_t
697 -static int ext4_xattr_ibody_set(struct inode *inode,
698 +static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
699 struct ext4_xattr_info *i,
700 struct ext4_xattr_ibody_find *is)
702 @@ -1108,7 +1393,7 @@ static int ext4_xattr_ibody_set(struct i
704 if (EXT4_I(inode)->i_extra_isize == 0)
706 - error = ext4_xattr_set_entry(i, s);
707 + error = ext4_xattr_set_entry(i, s, handle, inode);
710 header = IHDR(inode, ext4_raw_inode(&is->iloc));
711 @@ -1155,7 +1440,7 @@ ext4_xattr_set_handle(handle_t *handle,
714 .value_len = value_len,
718 struct ext4_xattr_ibody_find is = {
719 .s = { .not_found = -ENODATA, },
720 @@ -1204,7 +1489,7 @@ ext4_xattr_set_handle(handle_t *handle,
724 - error = ext4_xattr_ibody_set(inode, &i, &is);
725 + error = ext4_xattr_ibody_set(handle, inode, &i, &is);
726 else if (!bs.s.not_found)
727 error = ext4_xattr_block_set(handle, inode, &i, &bs);
729 @@ -1215,7 +1500,7 @@ ext4_xattr_set_handle(handle_t *handle,
730 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
733 - error = ext4_xattr_ibody_set(inode, &i, &is);
734 + error = ext4_xattr_ibody_set(handle, inode, &i, &is);
735 if (!error && !bs.s.not_found) {
737 error = ext4_xattr_block_set(handle, inode, &i, &bs);
738 @@ -1228,11 +1513,20 @@ ext4_xattr_set_handle(handle_t *handle,
741 error = ext4_xattr_block_set(handle, inode, &i, &bs);
742 + if (ext4_has_feature_ea_inode(inode->i_sb) &&
743 + error == -ENOSPC) {
744 + /* xattr not fit to block, store at external
747 + error = ext4_xattr_ibody_set(handle, inode,
752 if (!is.s.not_found) {
754 - error = ext4_xattr_ibody_set(inode, &i, &is);
755 + error = ext4_xattr_ibody_set(handle, inode, &i,
760 @@ -1271,12 +1565,26 @@ ext4_xattr_set(struct inode *inode, int
761 const void *value, size_t value_len, int flags)
764 + struct super_block *sb = inode->i_sb;
765 int error, retries = 0;
766 int credits = ext4_jbd2_credits_xattr(inode);
768 error = dquot_initialize(inode);
772 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
773 + ext4_has_feature_ea_inode(sb)) {
774 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
775 + sb->s_blocksize_bits;
777 + /* For new inode */
778 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
780 + /* For data blocks of EA inode */
781 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
785 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
786 if (IS_ERR(handle)) {
787 @@ -1288,7 +1596,7 @@ retry:
788 value, value_len, flags);
789 error2 = ext4_journal_stop(handle);
790 if (error == -ENOSPC &&
791 - ext4_should_retry_alloc(inode->i_sb, &retries))
792 + ext4_should_retry_alloc(sb, &retries))
796 @@ -1313,7 +1621,7 @@ static void ext4_xattr_shift_entries(str
798 /* Adjust the value offsets of the entries */
799 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
800 - if (last->e_value_size) {
801 + if (!last->e_value_inum && last->e_value_size) {
802 new_offs = le16_to_cpu(last->e_value_offs) +
804 last->e_value_offs = cpu_to_le16(new_offs);
805 @@ -1374,7 +1682,7 @@ static int ext4_xattr_move_to_block(hand
808 /* Remove the chosen entry from the inode */
809 - error = ext4_xattr_ibody_set(inode, &i, is);
810 + error = ext4_xattr_ibody_set(handle, inode, &i, is);
814 @@ -1578,21 +1886,135 @@ cleanup:
818 +#define EIA_INCR 16 /* must be 2^n */
819 +#define EIA_MASK (EIA_INCR - 1)
820 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
821 + * If @lea_ino_array is new or full it will be grown and the old
822 + * contents copied over.
825 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
827 + if (*lea_ino_array == NULL) {
829 + * Start with 15 inodes, so it fits into a power-of-two size.
830 + * If *lea_ino_array is NULL, this is essentially offsetof()
833 + kmalloc(offsetof(struct ext4_xattr_ino_array,
834 + xia_inodes[EIA_MASK]),
836 + if (*lea_ino_array == NULL)
838 + (*lea_ino_array)->xia_count = 0;
839 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
840 + /* expand the array once all 15 + n * 16 slots are full */
841 + struct ext4_xattr_ino_array *new_array = NULL;
842 + int count = (*lea_ino_array)->xia_count;
844 + /* if new_array is NULL, this is essentially offsetof() */
845 + new_array = kmalloc(
846 + offsetof(struct ext4_xattr_ino_array,
847 + xia_inodes[count + EIA_INCR]),
849 + if (new_array == NULL)
851 + memcpy(new_array, *lea_ino_array,
852 + offsetof(struct ext4_xattr_ino_array,
853 + xia_inodes[count]));
854 + kfree(*lea_ino_array);
855 + *lea_ino_array = new_array;
857 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
862 + * Add xattr inode to orphan list
865 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
866 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
868 + struct inode *ea_inode = NULL;
869 + int idx = 0, error = 0;
871 + if (lea_ino_array == NULL)
874 + for (; idx < lea_ino_array->xia_count; ++idx) {
875 + if (!ext4_handle_has_enough_credits(handle, credits)) {
876 + error = ext4_journal_extend(handle, credits);
878 + error = ext4_journal_restart(handle, credits);
881 + ext4_warning(inode->i_sb,
882 + "couldn't extend journal "
883 + "(err %d)", error);
887 + ea_inode = ext4_xattr_inode_iget(inode,
888 + lea_ino_array->xia_inodes[idx], &error);
891 + ext4_orphan_add(handle, ea_inode);
892 + /* the inode's i_count will be released by caller */
899 * ext4_xattr_delete_inode()
901 - * Free extended attribute resources associated with this inode. This
902 + * Free extended attribute resources associated with this inode. Traverse
903 + * all entries and unlink any xattr inodes associated with this inode. This
904 * is called immediately before an inode is freed. We have exclusive
905 - * access to the inode.
906 + * access to the inode. If an orphan inode is deleted it will also delete any
907 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
908 + * to ensure they belong to the parent inode and were not deleted already.
911 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
913 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
914 + struct ext4_xattr_ino_array **lea_ino_array)
916 struct buffer_head *bh = NULL;
917 + struct ext4_xattr_ibody_header *header;
918 + struct ext4_inode *raw_inode;
919 + struct ext4_iloc iloc;
920 + struct ext4_xattr_entry *entry;
921 + int credits = 3, error = 0;
923 - if (!EXT4_I(inode)->i_file_acl)
924 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
925 + goto delete_external_ea;
927 + error = ext4_get_inode_loc(inode, &iloc);
930 + raw_inode = ext4_raw_inode(&iloc);
931 + header = IHDR(inode, raw_inode);
932 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
933 + entry = EXT4_XATTR_NEXT(entry)) {
934 + if (!entry->e_value_inum)
936 + if (ext4_expand_ino_array(lea_ino_array,
937 + entry->e_value_inum) != 0) {
941 + entry->e_value_inum = 0;
946 + if (!EXT4_I(inode)->i_file_acl) {
947 + /* add xattr inode to orphan list */
948 + ext4_xattr_inode_orphan_add(handle, inode, credits,
952 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
954 EXT4_ERROR_INODE(inode, "block %llu read error",
955 @@ -1605,11 +2027,69 @@ ext4_xattr_delete_inode(handle_t *handle
956 EXT4_I(inode)->i_file_acl);
960 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
961 + entry = EXT4_XATTR_NEXT(entry)) {
962 + if (!entry->e_value_inum)
964 + if (ext4_expand_ino_array(lea_ino_array,
965 + entry->e_value_inum) != 0)
967 + entry->e_value_inum = 0;
970 + /* add xattr inode to orphan list */
971 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
976 + if (!IS_NOQUOTA(inode))
977 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
979 + if (!ext4_handle_has_enough_credits(handle, credits)) {
980 + error = ext4_journal_extend(handle, credits);
982 + error = ext4_journal_restart(handle, credits);
984 + ext4_warning(inode->i_sb,
985 + "couldn't extend journal (err %d)", error);
990 ext4_xattr_release_block(handle, inode, bh);
991 EXT4_I(inode)->i_file_acl = 0;
1000 +ext4_xattr_inode_array_free(struct inode *inode,
1001 + struct ext4_xattr_ino_array *lea_ino_array)
1003 + struct inode *ea_inode = NULL;
1007 + if (lea_ino_array == NULL)
1010 + for (; idx < lea_ino_array->xia_count; ++idx) {
1011 + ea_inode = ext4_xattr_inode_iget(inode,
1012 + lea_ino_array->xia_inodes[idx], &err);
1015 + /* for inode's i_count get from ext4_xattr_delete_inode */
1016 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
1018 + clear_nlink(ea_inode);
1021 + kfree(lea_ino_array);
1025 @@ -1661,10 +2141,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
1026 entry1->e_name_index != entry2->e_name_index ||
1027 entry1->e_name_len != entry2->e_name_len ||
1028 entry1->e_value_size != entry2->e_value_size ||
1029 + entry1->e_value_inum != entry2->e_value_inum ||
1030 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1032 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1033 - return -EFSCORRUPTED;
1034 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1035 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1036 le32_to_cpu(entry1->e_value_size)))
1037 @@ -1736,7 +2215,7 @@ static inline void ext4_xattr_hash_entry
1041 - if (entry->e_value_size != 0) {
1042 + if (!entry->e_value_inum && entry->e_value_size) {
1043 __le32 *value = (__le32 *)((char *)header +
1044 le16_to_cpu(entry->e_value_offs));
1045 for (n = (le32_to_cpu(entry->e_value_size) +
1046 Index: linux-stage/fs/ext4/xattr.h
1047 ===================================================================
1048 --- linux-stage.orig/fs/ext4/xattr.h
1049 +++ linux-stage/fs/ext4/xattr.h
1050 @@ -44,7 +44,7 @@ struct ext4_xattr_entry {
1051 __u8 e_name_len; /* length of name */
1052 __u8 e_name_index; /* attribute name index */
1053 __le16 e_value_offs; /* offset in disk block of value */
1054 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1055 + __le32 e_value_inum; /* inode in which the value is stored */
1056 __le32 e_value_size; /* size of attribute value */
1057 __le32 e_hash; /* hash value of name and value */
1058 char e_name[0]; /* attribute name */
1059 @@ -69,6 +69,26 @@ struct ext4_xattr_entry {
1060 EXT4_I(inode)->i_extra_isize))
1061 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1064 + * Link EA inode back to parent one using i_mtime field.
1065 + * Extra integer type conversion added to ignore higher
1066 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1068 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1070 + (inode)->i_mtime.tv_sec = inum; \
1073 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1074 +((__u32)(inode)->i_mtime.tv_sec)
1077 + * The minimum size of EA value when you start storing it in an external inode
1078 + * size of block - size of header - size of 1 entry - 4 null bytes
1080 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1081 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1083 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1084 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1085 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1086 @@ -77,10 +97,11 @@ struct ext4_xattr_entry {
1087 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1089 struct ext4_xattr_info {
1098 struct ext4_xattr_search {
1099 @@ -140,7 +161,13 @@ extern int ext4_xattr_get(struct inode *
1100 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1101 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1103 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1104 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1106 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1107 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1108 + struct ext4_xattr_ino_array **array);
1109 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1110 + struct ext4_xattr_ino_array *array);
1112 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1113 struct ext4_inode *raw_inode, handle_t *handle);
1114 Index: linux-stage/fs/ext4/extents.c
1115 ===================================================================
1116 --- linux-stage.orig/fs/ext4/extents.c
1117 +++ linux-stage/fs/ext4/extents.c
1118 @@ -2494,7 +2494,8 @@ int ext4_ext_index_trans_blocks(struct i
1120 static inline int get_default_free_blocks_flags(struct inode *inode)
1122 - if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1123 + if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
1124 + ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
1125 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
1126 else if (ext4_should_journal_data(inode))
1127 return EXT4_FREE_BLOCKS_FORGET;
1128 Index: linux-stage/fs/ext4/indirect.c
1129 ===================================================================
1130 --- linux-stage.orig/fs/ext4/indirect.c
1131 +++ linux-stage/fs/ext4/indirect.c
1132 @@ -835,7 +835,8 @@ static int ext4_clear_blocks(handle_t *h
1133 int flags = EXT4_FREE_BLOCKS_VALIDATED;
1136 - if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
1137 + if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
1138 + ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE))
1139 flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
1140 else if (ext4_should_journal_data(inode))
1141 flags |= EXT4_FREE_BLOCKS_FORGET;