1 This patch implements the large EA support in ext4. If the size of
2 an EA value is larger than the blocksize, then the EA value would
3 not be saved in the external EA block, instead it would be saved
4 in an external EA inode. So, the patch also helps support a larger
7 Index: linux-stage/fs/ext4/ext4.h
8 ===================================================================
9 --- linux-stage.orig/fs/ext4/ext4.h
10 +++ linux-stage/fs/ext4/ext4.h
11 @@ -1579,6 +1579,7 @@ static inline void ext4_clear_state_flag
12 EXT4_FEATURE_INCOMPAT_EXTENTS| \
13 EXT4_FEATURE_INCOMPAT_64BIT| \
14 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
15 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
16 EXT4_FEATURE_INCOMPAT_MMP | \
17 EXT4_FEATURE_INCOMPAT_DIRDATA| \
18 EXT4_FEATURE_INCOMPAT_INLINE_DATA)
19 @@ -1979,6 +1980,12 @@ struct mmpd_data {
20 #define EXT4_MMP_MAX_CHECK_INTERVAL 300UL
23 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1MB
24 + * This limit is arbitrary, but is reasonable for the xattr API.
26 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE (1024 * 1024)
32 @@ -1990,6 +1997,10 @@ struct mmpd_data {
33 # define ATTRIB_NORET __attribute__((noreturn))
34 # define NORET_AND noreturn,
36 +struct ext4_xattr_ino_array {
37 + unsigned int xia_count; /* # of used item in the array */
38 + unsigned int xia_inodes[0];
41 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
42 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
43 @@ -2194,6 +2205,7 @@ extern void ext4_set_inode_flags(struct
44 extern void ext4_get_inode_flags(struct ext4_inode_info *);
45 extern int ext4_alloc_da_blocks(struct inode *inode);
46 extern void ext4_set_aops(struct inode *inode);
47 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
48 extern int ext4_writepage_trans_blocks(struct inode *);
49 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
50 extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
51 Index: linux-stage/fs/ext4/inode.c
52 ===================================================================
53 --- linux-stage.orig/fs/ext4/inode.c
54 +++ linux-stage/fs/ext4/inode.c
55 @@ -134,8 +134,6 @@ static void ext4_invalidatepage(struct p
57 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
58 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
59 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
63 * Test whether an inode is a fast symlink.
64 @@ -184,6 +182,8 @@ void ext4_evict_inode(struct inode *inod
68 + int extra_credits = 3;
69 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
71 trace_ext4_evict_inode(inode);
73 @@ -236,8 +236,8 @@ void ext4_evict_inode(struct inode *inod
74 * protection against it
76 sb_start_intwrite(inode->i_sb);
77 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
78 - ext4_blocks_for_truncate(inode)+3);
80 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
82 ext4_std_error(inode->i_sb, PTR_ERR(handle));
84 @@ -252,6 +252,32 @@ void ext4_evict_inode(struct inode *inod
87 ext4_handle_sync(handle);
89 + /* Delete xattr inode before deleting the main inode. */
90 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
92 + ext4_warning(inode->i_sb,
93 + "couldn't delete inode's xattr (err %d)", err);
97 + if (!IS_NOQUOTA(inode))
98 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
100 + if (!ext4_handle_has_enough_credits(handle,
101 + ext4_blocks_for_truncate(inode) + extra_credits)) {
102 + err = ext4_journal_extend(handle,
103 + ext4_blocks_for_truncate(inode) + extra_credits);
105 + err = ext4_journal_restart(handle,
106 + ext4_blocks_for_truncate(inode) + extra_credits);
108 + ext4_warning(inode->i_sb,
109 + "couldn't extend journal (err %d)", err);
115 err = ext4_mark_inode_dirty(handle, inode);
117 @@ -269,10 +296,10 @@ void ext4_evict_inode(struct inode *inod
118 * enough credits left in the handle to remove the inode from
119 * the orphan list and set the dtime field.
121 - if (!ext4_handle_has_enough_credits(handle, 3)) {
122 - err = ext4_journal_extend(handle, 3);
123 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
124 + err = ext4_journal_extend(handle, extra_credits);
126 - err = ext4_journal_restart(handle, 3);
127 + err = ext4_journal_restart(handle, extra_credits);
129 ext4_warning(inode->i_sb,
130 "couldn't extend journal (err %d)", err);
131 @@ -308,6 +335,9 @@ void ext4_evict_inode(struct inode *inod
132 ext4_free_inode(handle, inode);
133 ext4_journal_stop(handle);
134 sb_end_intwrite(inode->i_sb);
136 + if (lea_ino_array != NULL)
137 + ext4_xattr_inode_array_free(inode, lea_ino_array);
140 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
141 @@ -4681,7 +4711,7 @@ static int ext4_index_trans_blocks(struc
143 * Also account for superblock, inode, quota and xattr blocks
145 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
146 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
149 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
150 Index: linux-stage/fs/ext4/xattr.c
151 ===================================================================
152 --- linux-stage.orig/fs/ext4/xattr.c
153 +++ linux-stage/fs/ext4/xattr.c
154 @@ -201,6 +201,7 @@ ext4_xattr_check_names(struct ext4_xattr
156 while (!IS_LAST_ENTRY(entry)) {
157 if (entry->e_value_size != 0 &&
158 + entry->e_value_inum == 0 &&
159 (value_start + le16_to_cpu(entry->e_value_offs) <
160 (void *)e + sizeof(__u32) ||
161 value_start + le16_to_cpu(entry->e_value_offs) +
162 @@ -233,19 +234,26 @@ ext4_xattr_check_block(struct inode *ino
166 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
167 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
168 + struct inode *inode)
170 size_t value_size = le32_to_cpu(entry->e_value_size);
172 - if (entry->e_value_block != 0 || value_size > size ||
173 + if (!entry->e_value_inum &&
174 le16_to_cpu(entry->e_value_offs) + value_size > size)
176 + if (entry->e_value_inum &&
177 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
178 + le32_to_cpu(entry->e_value_inum) >
179 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
185 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
186 - const char *name, size_t size, int sorted)
187 + const char *name, size_t size, int sorted,
188 + struct inode *inode)
190 struct ext4_xattr_entry *entry;
192 @@ -265,11 +273,109 @@ ext4_xattr_find_entry(struct ext4_xattr_
196 - if (!cmp && ext4_xattr_check_entry(entry, size))
197 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
199 return cmp ? -ENODATA : 0;
203 + * Read the EA value from an inode.
205 +static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
207 + unsigned long block = 0;
208 + struct buffer_head *bh = NULL;
209 + int err, blocksize;
210 + size_t csize, ret_size = 0;
215 + blocksize = ea_inode->i_sb->s_blocksize;
217 + while (ret_size < *size) {
218 + csize = (*size - ret_size) > blocksize ? blocksize :
220 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
225 + memcpy(buf, bh->b_data, csize);
239 + * Fetch the xattr inode from disk.
241 + * The xattr inode stores the parent inode number and generation so that
242 + * the kernel and e2fsck can verify the xattr inode is valid upon access.
244 +struct inode *ext4_xattr_inode_iget(struct inode *parent,
245 + unsigned long ea_ino, int *err)
247 + struct inode *ea_inode = NULL;
249 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
250 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
251 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
252 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
253 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
254 + *err = rc != 0 ? rc : -EIO;
258 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
259 + ea_inode->i_generation != parent->i_generation) {
260 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
261 + "to parent invalid.", ea_ino);
266 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
267 + ext4_error(parent->i_sb, "EA inode %lu does not have "
268 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
282 + * Read the value from the EA inode.
284 +static int ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino,
285 + void *buffer, size_t *size)
287 + struct inode *ea_inode = NULL;
290 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
294 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
301 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
302 void *buffer, size_t buffer_size)
303 @@ -301,7 +401,8 @@ bad_block:
305 ext4_xattr_cache_insert(bh);
307 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
308 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
313 @@ -311,8 +412,16 @@ bad_block:
315 if (size > buffer_size)
317 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
319 + if (entry->e_value_inum) {
320 + error = ext4_xattr_inode_get(inode,
321 + le32_to_cpu(entry->e_value_inum),
326 + memcpy(buffer, bh->b_data +
327 + le16_to_cpu(entry->e_value_offs), size);
332 @@ -346,7 +455,7 @@ ext4_xattr_ibody_get(struct inode *inode
335 error = ext4_xattr_find_entry(&entry, name_index, name,
336 - end - (void *)entry, 0);
337 + end - (void *)entry, 0, inode);
340 size = le32_to_cpu(entry->e_value_size);
341 @@ -354,8 +463,16 @@ ext4_xattr_ibody_get(struct inode *inode
343 if (size > buffer_size)
345 - memcpy(buffer, (void *)IFIRST(header) +
346 - le16_to_cpu(entry->e_value_offs), size);
347 + if (entry->e_value_inum) {
348 + error = ext4_xattr_inode_get(inode,
349 + le32_to_cpu(entry->e_value_inum),
354 + memcpy(buffer, (void *)IFIRST(header) +
355 + le16_to_cpu(entry->e_value_offs), size);
360 @@ -600,7 +717,7 @@ static size_t ext4_xattr_free_space(stru
361 size_t *min_offs, void *base, int *total)
363 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
364 - if (!last->e_value_block && last->e_value_size) {
365 + if (!last->e_value_inum && last->e_value_size) {
366 size_t offs = le16_to_cpu(last->e_value_offs);
367 if (offs < *min_offs)
369 @@ -611,16 +728,198 @@ static size_t ext4_xattr_free_space(stru
370 return (*min_offs - ((void *)last - base) - sizeof(__u32));
374 + * Write the value of the EA in an inode.
377 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
378 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
379 + const void *buf, int bufsize)
381 + struct buffer_head *bh = NULL;
382 + unsigned long block = 0;
383 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
384 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
385 + int csize, wsize = 0;
390 + while (ret >= 0 && ret < max_blocks) {
391 + struct ext4_map_blocks map;
392 + map.m_lblk = block += ret;
393 + map.m_len = max_blocks -= ret;
395 + ret = ext4_map_blocks(handle, ea_inode, &map,
396 + EXT4_GET_BLOCKS_CREATE);
398 + ext4_mark_inode_dirty(handle, ea_inode);
399 + if (ret == -ENOSPC &&
400 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
412 + while (wsize < bufsize) {
415 + csize = (bufsize - wsize) > blocksize ? blocksize :
417 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
420 + ret = ext4_journal_get_write_access(handle, bh);
424 + memcpy(bh->b_data, buf, csize);
425 + set_buffer_uptodate(bh);
426 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
433 + mutex_lock(&ea_inode->i_mutex);
434 + i_size_write(ea_inode, wsize);
435 + ext4_update_i_disksize(ea_inode, wsize);
436 + mutex_unlock(&ea_inode->i_mutex);
438 + ext4_mark_inode_dirty(handle, ea_inode);
446 +static void ext4_xattr_inode_set_ref(struct inode *ea_inode, __u64 ref_count)
448 + ea_inode->i_ctime.tv_sec = (__u32)(ref_count >> 32);
449 + ea_inode->i_version = (__u32)ref_count;
452 +static void ext4_xattr_inode_set_hash(struct inode *ea_inode, __u32 hash)
454 + ea_inode->i_atime.tv_sec = hash;
458 + * Create an inode to store the value of a large EA.
460 +static struct inode *
461 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode, __u32 hash)
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);
472 + if (!IS_ERR(ea_inode)) {
473 + ea_inode->i_op = &ext4_file_inode_operations;
474 + ea_inode->i_fop = &ext4_file_operations;
475 + ext4_set_aops(ea_inode);
476 + ea_inode->i_generation = inode->i_generation;
477 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
480 + * A back-pointer from EA inode to parent inode will be useful
483 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
484 + unlock_new_inode(ea_inode);
486 + ext4_xattr_inode_set_ref(ea_inode, 1);
487 + ext4_xattr_inode_set_hash(ea_inode, hash);
494 + * Unlink the inode storing the value of the EA.
497 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
499 + struct inode *ea_inode = NULL;
502 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
506 + clear_nlink(ea_inode);
513 +ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
515 + if (ext4_has_metadata_csum(sbi->s_sb))
516 + return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
521 + * Add value of the EA in an inode.
524 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
525 + const void *value, size_t value_len)
527 + struct inode *ea_inode = NULL;
531 + /* Create an inode for the EA value */
532 + hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
533 + ea_inode = ext4_xattr_inode_create(handle, inode, hash);
534 + if (IS_ERR(ea_inode))
537 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
539 + clear_nlink(ea_inode);
541 + *ea_ino = ea_inode->i_ino;
549 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
550 + handle_t *handle, struct inode *inode)
552 struct ext4_xattr_entry *last;
553 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
554 + int in_inode = i->in_inode;
556 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
557 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
558 + (EXT4_XATTR_SIZE(i->value_len) >
559 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
562 /* Compute min_offs and last. */
564 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
565 - if (!last->e_value_block && last->e_value_size) {
566 + if (!last->e_value_inum && last->e_value_size) {
567 size_t offs = le16_to_cpu(last->e_value_offs);
570 @@ -628,15 +927,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
572 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
574 - if (!s->here->e_value_block && s->here->e_value_size) {
576 + !s->here->e_value_inum && s->here->e_value_size) {
577 size_t size = le32_to_cpu(s->here->e_value_size);
578 free += EXT4_XATTR_SIZE(size);
580 free += EXT4_XATTR_LEN(name_len);
583 - if (free < EXT4_XATTR_LEN(name_len) +
584 - EXT4_XATTR_SIZE(i->value_len))
585 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
590 + if (free < value_len ||
591 + free < EXT4_XATTR_LEN(name_len) + value_len)
595 @@ -651,7 +955,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
596 s->here->e_name_len = name_len;
597 memcpy(s->here->e_name, i->name, name_len);
599 - if (!s->here->e_value_block && s->here->e_value_size) {
600 + if (!s->here->e_value_inum && s->here->e_value_size &&
601 + s->here->e_value_offs > 0) {
602 void *first_val = s->base + min_offs;
603 size_t offs = le16_to_cpu(s->here->e_value_offs);
604 void *val = s->base + offs;
605 @@ -685,13 +990,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
607 while (!IS_LAST_ENTRY(last)) {
608 size_t o = le16_to_cpu(last->e_value_offs);
609 - if (!last->e_value_block &&
610 + if (!last->e_value_inum &&
611 last->e_value_size && o < offs)
613 cpu_to_le16(o + size);
614 last = EXT4_XATTR_NEXT(last);
617 + if (s->here->e_value_inum) {
618 + ext4_xattr_inode_unlink(inode,
619 + le32_to_cpu(s->here->e_value_inum));
620 + s->here->e_value_inum = 0;
623 /* Remove the old name. */
624 size_t size = EXT4_XATTR_LEN(name_len);
625 @@ -705,10 +1014,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
627 /* Insert the new value. */
628 s->here->e_value_size = cpu_to_le32(i->value_len);
629 - if (i->value_len) {
631 + unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
632 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
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 @@ -758,7 +1074,7 @@ ext4_xattr_block_find(struct inode *inod
645 bs->s.end = bs->bh->b_data + bs->bh->b_size;
646 bs->s.here = bs->s.first;
647 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
648 - i->name, bs->bh->b_size, 1);
649 + i->name, bs->bh->b_size, 1, inode);
650 if (error && error != -ENODATA)
652 bs->s.not_found = error;
653 @@ -782,8 +1098,6 @@ ext4_xattr_block_set(handle_t *handle, s
655 #define header(x) ((struct ext4_xattr_header *)(x))
657 - if (i->value && i->value_len > sb->s_blocksize)
660 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
662 @@ -799,7 +1113,7 @@ ext4_xattr_block_set(handle_t *handle, s
665 ea_bdebug(bs->bh, "modifying in-place");
666 - error = ext4_xattr_set_entry(i, s);
667 + error = ext4_xattr_set_entry(i, s, handle, inode);
669 if (!IS_LAST_ENTRY(s->first))
670 ext4_xattr_rehash(header(s->base),
671 @@ -850,7 +1164,7 @@ ext4_xattr_block_set(handle_t *handle, s
672 s->end = s->base + sb->s_blocksize;
675 - error = ext4_xattr_set_entry(i, s);
676 + error = ext4_xattr_set_entry(i, s, handle, inode);
680 @@ -1000,7 +1314,7 @@ int ext4_xattr_ibody_find(struct inode *
681 /* Find the named attribute. */
682 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
684 - (void *)is->s.base, 0);
685 + (void *)is->s.base, 0, inode);
686 if (error && error != -ENODATA)
688 is->s.not_found = error;
689 @@ -1018,7 +1332,7 @@ int ext4_xattr_ibody_inline_set(handle_t
691 if (EXT4_I(inode)->i_extra_isize == 0)
693 - error = ext4_xattr_set_entry(i, s);
694 + error = ext4_xattr_set_entry(i, s, handle, inode);
696 if (error == -ENOSPC &&
697 ext4_has_inline_data(inode)) {
698 @@ -1030,7 +1344,7 @@ int ext4_xattr_ibody_inline_set(handle_t
699 error = ext4_xattr_ibody_find(inode, i, is);
702 - error = ext4_xattr_set_entry(i, s);
703 + error = ext4_xattr_set_entry(i, s, handle, inode);
707 @@ -1056,7 +1370,7 @@ static int ext4_xattr_ibody_set(handle_t
709 if (EXT4_I(inode)->i_extra_isize == 0)
711 - error = ext4_xattr_set_entry(i, s);
712 + error = ext4_xattr_set_entry(i, s, handle, inode);
715 header = IHDR(inode, ext4_raw_inode(&is->iloc));
716 @@ -1092,7 +1406,7 @@ ext4_xattr_set_handle(handle_t *handle,
719 .value_len = value_len,
723 struct ext4_xattr_ibody_find is = {
724 .s = { .not_found = -ENODATA, },
725 @@ -1157,6 +1471,15 @@ ext4_xattr_set_handle(handle_t *handle,
728 error = ext4_xattr_block_set(handle, inode, &i, &bs);
729 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
730 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
731 + error == -ENOSPC) {
732 + /* xattr not fit to block, store at external
735 + error = ext4_xattr_ibody_set(handle, inode,
740 if (!is.s.not_found) {
741 @@ -1203,9 +1526,22 @@ ext4_xattr_set(struct inode *inode, int
742 const void *value, size_t value_len, int flags)
745 + struct super_block *sb = inode->i_sb;
746 int error, retries = 0;
747 int credits = ext4_jbd2_credits_xattr(inode);
749 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
750 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
751 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
752 + sb->s_blocksize_bits;
754 + /* For new inode */
755 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
757 + /* For data blocks of EA inode */
758 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
762 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
763 if (IS_ERR(handle)) {
764 @@ -1217,7 +1553,7 @@ retry:
765 value, value_len, flags);
766 error2 = ext4_journal_stop(handle);
767 if (error == -ENOSPC &&
768 - ext4_should_retry_alloc(inode->i_sb, &retries))
769 + ext4_should_retry_alloc(sb, &retries))
773 @@ -1239,7 +1575,7 @@ static void ext4_xattr_shift_entries(str
775 /* Adjust the value offsets of the entries */
776 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
777 - if (!last->e_value_block && last->e_value_size) {
778 + if (!last->e_value_inum && last->e_value_size) {
779 new_offs = le16_to_cpu(last->e_value_offs) +
781 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
782 @@ -1477,21 +1813,135 @@ cleanup:
786 +#define EIA_INCR 16 /* must be 2^n */
787 +#define EIA_MASK (EIA_INCR - 1)
788 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
789 + * If @lea_ino_array is new or full it will be grown and the old
790 + * contents copied over.
793 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
795 + if (*lea_ino_array == NULL) {
797 + * Start with 15 inodes, so it fits into a power-of-two size.
798 + * If *lea_ino_array is NULL, this is essentially offsetof()
801 + kmalloc(offsetof(struct ext4_xattr_ino_array,
802 + xia_inodes[EIA_MASK]),
804 + if (*lea_ino_array == NULL)
806 + (*lea_ino_array)->xia_count = 0;
807 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
808 + /* expand the array once all 15 + n * 16 slots are full */
809 + struct ext4_xattr_ino_array *new_array = NULL;
810 + int count = (*lea_ino_array)->xia_count;
812 + /* if new_array is NULL, this is essentially offsetof() */
813 + new_array = kmalloc(
814 + offsetof(struct ext4_xattr_ino_array,
815 + xia_inodes[count + EIA_INCR]),
817 + if (new_array == NULL)
819 + memcpy(new_array, *lea_ino_array,
820 + offsetof(struct ext4_xattr_ino_array,
821 + xia_inodes[count]));
822 + kfree(*lea_ino_array);
823 + *lea_ino_array = new_array;
825 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
830 + * Add xattr inode to orphan list
833 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
834 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
836 + struct inode *ea_inode = NULL;
837 + int idx = 0, error = 0;
839 + if (lea_ino_array == NULL)
842 + for (; idx < lea_ino_array->xia_count; ++idx) {
843 + if (!ext4_handle_has_enough_credits(handle, credits)) {
844 + error = ext4_journal_extend(handle, credits);
846 + error = ext4_journal_restart(handle, credits);
849 + ext4_warning(inode->i_sb,
850 + "couldn't extend journal "
851 + "(err %d)", error);
855 + ea_inode = ext4_xattr_inode_iget(inode,
856 + lea_ino_array->xia_inodes[idx], &error);
859 + ext4_orphan_add(handle, ea_inode);
860 + /* the inode's i_count will be released by caller */
867 * ext4_xattr_delete_inode()
869 - * Free extended attribute resources associated with this inode. This
870 + * Free extended attribute resources associated with this inode. Traverse
871 + * all entries and unlink any xattr inodes associated with this inode. This
872 * is called immediately before an inode is freed. We have exclusive
873 - * access to the inode.
874 + * access to the inode. If an orphan inode is deleted it will also delete any
875 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
876 + * to ensure they belong to the parent inode and were not deleted already.
879 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
881 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
882 + struct ext4_xattr_ino_array **lea_ino_array)
884 struct buffer_head *bh = NULL;
885 + struct ext4_xattr_ibody_header *header;
886 + struct ext4_inode *raw_inode;
887 + struct ext4_iloc iloc;
888 + struct ext4_xattr_entry *entry;
889 + int credits = 3, error = 0;
891 - if (!EXT4_I(inode)->i_file_acl)
892 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
893 + goto delete_external_ea;
895 + error = ext4_get_inode_loc(inode, &iloc);
898 + raw_inode = ext4_raw_inode(&iloc);
899 + header = IHDR(inode, raw_inode);
900 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
901 + entry = EXT4_XATTR_NEXT(entry)) {
902 + if (!entry->e_value_inum)
904 + if (ext4_expand_ino_array(lea_ino_array,
905 + entry->e_value_inum) != 0) {
909 + entry->e_value_inum = 0;
914 + if (!EXT4_I(inode)->i_file_acl) {
915 + /* add xattr inode to orphan list */
916 + ext4_xattr_inode_orphan_add(handle, inode, credits,
920 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
922 EXT4_ERROR_INODE(inode, "block %llu read error",
923 @@ -1504,11 +1954,69 @@ ext4_xattr_delete_inode(handle_t *handle
924 EXT4_I(inode)->i_file_acl);
928 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
929 + entry = EXT4_XATTR_NEXT(entry)) {
930 + if (!entry->e_value_inum)
932 + if (ext4_expand_ino_array(lea_ino_array,
933 + entry->e_value_inum) != 0)
935 + entry->e_value_inum = 0;
938 + /* add xattr inode to orphan list */
939 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
944 + if (!IS_NOQUOTA(inode))
945 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
947 + if (!ext4_handle_has_enough_credits(handle, credits)) {
948 + error = ext4_journal_extend(handle, credits);
950 + error = ext4_journal_restart(handle, credits);
952 + ext4_warning(inode->i_sb,
953 + "couldn't extend journal (err %d)", error);
958 ext4_xattr_release_block(handle, inode, bh);
959 EXT4_I(inode)->i_file_acl = 0;
968 +ext4_xattr_inode_array_free(struct inode *inode,
969 + struct ext4_xattr_ino_array *lea_ino_array)
971 + struct inode *ea_inode = NULL;
975 + if (lea_ino_array == NULL)
978 + for (; idx < lea_ino_array->xia_count; ++idx) {
979 + ea_inode = ext4_xattr_inode_iget(inode,
980 + lea_ino_array->xia_inodes[idx], &err);
983 + /* for inode's i_count get from ext4_xattr_delete_inode */
984 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
986 + clear_nlink(ea_inode);
989 + kfree(lea_ino_array);
993 @@ -1578,10 +2086,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
994 entry1->e_name_index != entry2->e_name_index ||
995 entry1->e_name_len != entry2->e_name_len ||
996 entry1->e_value_size != entry2->e_value_size ||
997 + entry1->e_value_inum != entry2->e_value_inum ||
998 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1000 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1002 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1003 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1004 le32_to_cpu(entry1->e_value_size)))
1005 @@ -1665,7 +2172,7 @@ static inline void ext4_xattr_hash_entry
1009 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1010 + if (!entry->e_value_inum && entry->e_value_size) {
1011 __le32 *value = (__le32 *)((char *)header +
1012 le16_to_cpu(entry->e_value_offs));
1013 for (n = (le32_to_cpu(entry->e_value_size) +
1014 Index: linux-stage/fs/ext4/xattr.h
1015 ===================================================================
1016 --- linux-stage.orig/fs/ext4/xattr.h
1017 +++ linux-stage/fs/ext4/xattr.h
1018 @@ -42,7 +42,7 @@ struct ext4_xattr_entry {
1019 __u8 e_name_len; /* length of name */
1020 __u8 e_name_index; /* attribute name index */
1021 __le16 e_value_offs; /* offset in disk block of value */
1022 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1023 + __le32 e_value_inum; /* inode in which the value is stored */
1024 __le32 e_value_size; /* size of attribute value */
1025 __le32 e_hash; /* hash value of name and value */
1026 char e_name[0]; /* attribute name */
1027 @@ -67,6 +67,26 @@ struct ext4_xattr_entry {
1028 EXT4_I(inode)->i_extra_isize))
1029 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1032 + * Link EA inode back to parent one using i_mtime field.
1033 + * Extra integer type conversion added to ignore higher
1034 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1036 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1038 + (inode)->i_mtime.tv_sec = inum; \
1041 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1042 + ((__u32)(inode)->i_mtime.tv_sec)
1045 + * The minimum size of EA value when you start storing it in an external inode
1046 + * size of block - size of header - size of 1 entry - 4 null bytes
1048 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1049 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1051 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1052 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1053 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1054 @@ -75,10 +84,11 @@ struct ext4_xattr_entry {
1055 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1057 struct ext4_xattr_info {
1066 struct ext4_xattr_search {
1067 @@ -106,7 +116,13 @@ extern int ext4_xattr_get(struct inode *
1068 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1069 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1071 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1072 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1074 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1075 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1076 + struct ext4_xattr_ino_array **array);
1077 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1078 + struct ext4_xattr_ino_array *array);
1079 extern void ext4_xattr_put_super(struct super_block *);
1081 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1082 Index: linux-stage/fs/ext4/ialloc.c
1083 ===================================================================
1084 --- linux-stage.orig/fs/ext4/ialloc.c
1085 +++ linux-stage/fs/ext4/ialloc.c
1086 @@ -269,7 +269,6 @@ void ext4_free_inode(handle_t *handle, s
1087 * as writing the quota to disk may need the lock as well.
1089 dquot_initialize(inode);
1090 - ext4_xattr_delete_inode(handle, inode);
1091 dquot_free_inode(inode);
1094 Index: linux-stage/fs/ext4/inline.c
1095 ===================================================================
1096 --- linux-stage.orig/fs/ext4/inline.c
1097 +++ linux-stage/fs/ext4/inline.c
1098 @@ -59,7 +59,7 @@ static int get_max_inline_xattr_value_si
1100 /* Compute min_offs. */
1101 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
1102 - if (!entry->e_value_block && entry->e_value_size) {
1103 + if (!entry->e_value_inum && entry->e_value_size) {
1104 size_t offs = le16_to_cpu(entry->e_value_offs);
1105 if (offs < min_offs)