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 @@ -1990,6 +1997,10 @@ struct mmpd_data {
20 # define ATTRIB_NORET __attribute__((noreturn))
21 # define NORET_AND noreturn,
23 +struct ext4_xattr_ino_array {
24 + unsigned int xia_count; /* # of used item in the array */
25 + unsigned int xia_inodes[0];
28 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
29 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
30 @@ -2194,6 +2205,7 @@ extern void ext4_set_inode_flags(struct
31 extern void ext4_get_inode_flags(struct ext4_inode_info *);
32 extern int ext4_alloc_da_blocks(struct inode *inode);
33 extern void ext4_set_aops(struct inode *inode);
34 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
35 extern int ext4_writepage_trans_blocks(struct inode *);
36 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
37 extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
38 Index: linux-stage/fs/ext4/inode.c
39 ===================================================================
40 --- linux-stage.orig/fs/ext4/inode.c
41 +++ linux-stage/fs/ext4/inode.c
42 @@ -134,8 +134,6 @@ static void ext4_invalidatepage(struct p
44 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
45 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
46 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
50 * Test whether an inode is a fast symlink.
51 @@ -184,6 +182,8 @@ void ext4_evict_inode(struct inode *inod
55 + int extra_credits = 3;
56 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
58 trace_ext4_evict_inode(inode);
60 @@ -236,8 +236,8 @@ void ext4_evict_inode(struct inode *inod
61 * protection against it
63 sb_start_intwrite(inode->i_sb);
64 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
65 - ext4_blocks_for_truncate(inode)+3);
67 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
69 ext4_std_error(inode->i_sb, PTR_ERR(handle));
71 @@ -252,6 +252,32 @@ void ext4_evict_inode(struct inode *inod
74 ext4_handle_sync(handle);
76 + /* Delete xattr inode before deleting the main inode. */
77 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
79 + ext4_warning(inode->i_sb,
80 + "couldn't delete inode's xattr (err %d)", err);
84 + if (!IS_NOQUOTA(inode))
85 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
87 + if (!ext4_handle_has_enough_credits(handle,
88 + ext4_blocks_for_truncate(inode) + extra_credits)) {
89 + err = ext4_journal_extend(handle,
90 + ext4_blocks_for_truncate(inode) + extra_credits);
92 + err = ext4_journal_restart(handle,
93 + ext4_blocks_for_truncate(inode) + extra_credits);
95 + ext4_warning(inode->i_sb,
96 + "couldn't extend journal (err %d)", err);
102 err = ext4_mark_inode_dirty(handle, inode);
104 @@ -269,10 +296,10 @@ void ext4_evict_inode(struct inode *inod
105 * enough credits left in the handle to remove the inode from
106 * the orphan list and set the dtime field.
108 - if (!ext4_handle_has_enough_credits(handle, 3)) {
109 - err = ext4_journal_extend(handle, 3);
110 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
111 + err = ext4_journal_extend(handle, extra_credits);
113 - err = ext4_journal_restart(handle, 3);
114 + err = ext4_journal_restart(handle, extra_credits);
116 ext4_warning(inode->i_sb,
117 "couldn't extend journal (err %d)", err);
118 @@ -308,6 +335,9 @@ void ext4_evict_inode(struct inode *inod
119 ext4_free_inode(handle, inode);
120 ext4_journal_stop(handle);
121 sb_end_intwrite(inode->i_sb);
123 + if (lea_ino_array != NULL)
124 + ext4_xattr_inode_array_free(inode, lea_ino_array);
127 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
128 @@ -4681,7 +4711,7 @@ static int ext4_index_trans_blocks(struc
130 * Also account for superblock, inode, quota and xattr blocks
132 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
133 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
136 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
137 Index: linux-stage/fs/ext4/xattr.c
138 ===================================================================
139 --- linux-stage.orig/fs/ext4/xattr.c
140 +++ linux-stage/fs/ext4/xattr.c
141 @@ -201,6 +201,7 @@ ext4_xattr_check_names(struct ext4_xattr
143 while (!IS_LAST_ENTRY(entry)) {
144 if (entry->e_value_size != 0 &&
145 + entry->e_value_inum == 0 &&
146 (value_start + le16_to_cpu(entry->e_value_offs) <
147 (void *)e + sizeof(__u32) ||
148 value_start + le16_to_cpu(entry->e_value_offs) +
149 @@ -233,19 +234,26 @@ ext4_xattr_check_block(struct inode *ino
153 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
154 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
155 + struct inode *inode)
157 size_t value_size = le32_to_cpu(entry->e_value_size);
159 - if (entry->e_value_block != 0 || value_size > size ||
160 + if (!entry->e_value_inum &&
161 le16_to_cpu(entry->e_value_offs) + value_size > size)
163 + if (entry->e_value_inum &&
164 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
165 + le32_to_cpu(entry->e_value_inum) >
166 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
172 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
173 - const char *name, size_t size, int sorted)
174 + const char *name, size_t size, int sorted,
175 + struct inode *inode)
177 struct ext4_xattr_entry *entry;
179 @@ -265,11 +273,109 @@ ext4_xattr_find_entry(struct ext4_xattr_
183 - if (!cmp && ext4_xattr_check_entry(entry, size))
184 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
186 return cmp ? -ENODATA : 0;
190 + * Read the EA value from an inode.
192 +static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
194 + unsigned long block = 0;
195 + struct buffer_head *bh = NULL;
196 + int err, blocksize;
197 + size_t csize, ret_size = 0;
202 + blocksize = ea_inode->i_sb->s_blocksize;
204 + while (ret_size < *size) {
205 + csize = (*size - ret_size) > blocksize ? blocksize :
207 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
212 + memcpy(buf, bh->b_data, csize);
226 + * Fetch the xattr inode from disk.
228 + * The xattr inode stores the parent inode number and generation so that
229 + * the kernel and e2fsck can verify the xattr inode is valid upon access.
231 +struct inode *ext4_xattr_inode_iget(struct inode *parent,
232 + unsigned long ea_ino, int *err)
234 + struct inode *ea_inode = NULL;
236 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
237 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
238 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
239 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
240 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
241 + *err = rc != 0 ? rc : -EIO;
245 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
246 + ea_inode->i_generation != parent->i_generation) {
247 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
248 + "to parent invalid.", ea_ino);
253 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
254 + ext4_error(parent->i_sb, "EA inode %lu does not have "
255 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
269 + * Read the value from the EA inode.
271 +static int ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino,
272 + void *buffer, size_t *size)
274 + struct inode *ea_inode = NULL;
277 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
281 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
288 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
289 void *buffer, size_t buffer_size)
290 @@ -301,7 +401,8 @@ bad_block:
292 ext4_xattr_cache_insert(bh);
294 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
295 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
300 @@ -311,8 +412,16 @@ bad_block:
302 if (size > buffer_size)
304 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
306 + if (entry->e_value_inum) {
307 + error = ext4_xattr_inode_get(inode,
308 + le32_to_cpu(entry->e_value_inum),
313 + memcpy(buffer, bh->b_data +
314 + le16_to_cpu(entry->e_value_offs), size);
319 @@ -346,7 +455,7 @@ ext4_xattr_ibody_get(struct inode *inode
322 error = ext4_xattr_find_entry(&entry, name_index, name,
323 - end - (void *)entry, 0);
324 + end - (void *)entry, 0, inode);
327 size = le32_to_cpu(entry->e_value_size);
328 @@ -354,8 +463,16 @@ ext4_xattr_ibody_get(struct inode *inode
330 if (size > buffer_size)
332 - memcpy(buffer, (void *)IFIRST(header) +
333 - le16_to_cpu(entry->e_value_offs), size);
334 + if (entry->e_value_inum) {
335 + error = ext4_xattr_inode_get(inode,
336 + le32_to_cpu(entry->e_value_inum),
341 + memcpy(buffer, (void *)IFIRST(header) +
342 + le16_to_cpu(entry->e_value_offs), size);
347 @@ -600,7 +717,7 @@ static size_t ext4_xattr_free_space(stru
348 size_t *min_offs, void *base, int *total)
350 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
351 - if (!last->e_value_block && last->e_value_size) {
352 + if (!last->e_value_inum && last->e_value_size) {
353 size_t offs = le16_to_cpu(last->e_value_offs);
354 if (offs < *min_offs)
356 @@ -611,16 +728,198 @@ static size_t ext4_xattr_free_space(stru
357 return (*min_offs - ((void *)last - base) - sizeof(__u32));
361 + * Write the value of the EA in an inode.
364 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
365 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
366 + const void *buf, int bufsize)
368 + struct buffer_head *bh = NULL;
369 + unsigned long block = 0;
370 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
371 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
372 + int csize, wsize = 0;
377 + while (ret >= 0 && ret < max_blocks) {
378 + struct ext4_map_blocks map;
379 + map.m_lblk = block += ret;
380 + map.m_len = max_blocks -= ret;
382 + ret = ext4_map_blocks(handle, ea_inode, &map,
383 + EXT4_GET_BLOCKS_CREATE);
385 + ext4_mark_inode_dirty(handle, ea_inode);
386 + if (ret == -ENOSPC &&
387 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
399 + while (wsize < bufsize) {
402 + csize = (bufsize - wsize) > blocksize ? blocksize :
404 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
407 + ret = ext4_journal_get_write_access(handle, bh);
411 + memcpy(bh->b_data, buf, csize);
412 + set_buffer_uptodate(bh);
413 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
420 + mutex_lock(&ea_inode->i_mutex);
421 + i_size_write(ea_inode, wsize);
422 + ext4_update_i_disksize(ea_inode, wsize);
423 + mutex_unlock(&ea_inode->i_mutex);
425 + ext4_mark_inode_dirty(handle, ea_inode);
433 +static void ext4_xattr_inode_set_ref(struct inode *ea_inode, __u64 ref_count)
435 + ea_inode->i_ctime.tv_sec = (__u32)(ref_count >> 32);
436 + ea_inode->i_version = (__u32)ref_count;
439 +static void ext4_xattr_inode_set_hash(struct inode *ea_inode, __u32 hash)
441 + ea_inode->i_atime.tv_sec = hash;
445 + * Create an inode to store the value of a large EA.
447 +static struct inode *
448 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode, __u32 hash)
450 + struct inode *ea_inode = NULL;
453 + * Let the next inode be the goal, so we try and allocate the EA inode
454 + * in the same group, or nearby one.
456 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
457 + S_IFREG|0600, NULL, inode->i_ino + 1, NULL);
459 + if (!IS_ERR(ea_inode)) {
460 + ea_inode->i_op = &ext4_file_inode_operations;
461 + ea_inode->i_fop = &ext4_file_operations;
462 + ext4_set_aops(ea_inode);
463 + ea_inode->i_generation = inode->i_generation;
464 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
467 + * A back-pointer from EA inode to parent inode will be useful
470 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
471 + unlock_new_inode(ea_inode);
473 + ext4_xattr_inode_set_ref(ea_inode, 1);
474 + ext4_xattr_inode_set_hash(ea_inode, hash);
481 + * Unlink the inode storing the value of the EA.
484 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
486 + struct inode *ea_inode = NULL;
489 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
493 + clear_nlink(ea_inode);
500 +ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
502 + if (ext4_has_metadata_csum(sbi->s_sb))
503 + return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
508 + * Add value of the EA in an inode.
511 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
512 + const void *value, size_t value_len)
514 + struct inode *ea_inode = NULL;
518 + /* Create an inode for the EA value */
519 + hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
520 + ea_inode = ext4_xattr_inode_create(handle, inode, hash);
521 + if (IS_ERR(ea_inode))
524 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
526 + clear_nlink(ea_inode);
528 + *ea_ino = ea_inode->i_ino;
536 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
537 + handle_t *handle, struct inode *inode)
539 struct ext4_xattr_entry *last;
540 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
541 + int in_inode = i->in_inode;
543 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
544 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
545 + (EXT4_XATTR_SIZE(i->value_len) >
546 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
549 /* Compute min_offs and last. */
551 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
552 - if (!last->e_value_block && last->e_value_size) {
553 + if (!last->e_value_inum && last->e_value_size) {
554 size_t offs = le16_to_cpu(last->e_value_offs);
557 @@ -628,15 +927,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
559 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
561 - if (!s->here->e_value_block && s->here->e_value_size) {
563 + !s->here->e_value_inum && s->here->e_value_size) {
564 size_t size = le32_to_cpu(s->here->e_value_size);
565 free += EXT4_XATTR_SIZE(size);
567 free += EXT4_XATTR_LEN(name_len);
570 - if (free < EXT4_XATTR_LEN(name_len) +
571 - EXT4_XATTR_SIZE(i->value_len))
572 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
577 + if (free < value_len ||
578 + free < EXT4_XATTR_LEN(name_len) + value_len)
582 @@ -651,7 +955,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_block && 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 @@ -685,13 +990,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_block &&
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 @@ -705,10 +1014,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
614 /* Insert the new value. */
615 s->here->e_value_size = cpu_to_le32(i->value_len);
616 - if (i->value_len) {
618 + unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
619 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
621 + s->here->e_value_inum = cpu_to_le32(ea_ino);
622 + s->here->e_value_offs = 0;
623 + } else if (i->value_len) {
624 size_t size = EXT4_XATTR_SIZE(i->value_len);
625 void *val = s->base + min_offs - size;
626 s->here->e_value_offs = cpu_to_le16(min_offs - size);
627 + s->here->e_value_inum = 0;
628 if (i->value == EXT4_ZERO_XATTR_VALUE) {
629 memset(val, 0, size);
631 @@ -758,7 +1074,7 @@ ext4_xattr_block_find(struct inode *inod
632 bs->s.end = bs->bh->b_data + bs->bh->b_size;
633 bs->s.here = bs->s.first;
634 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
635 - i->name, bs->bh->b_size, 1);
636 + i->name, bs->bh->b_size, 1, inode);
637 if (error && error != -ENODATA)
639 bs->s.not_found = error;
640 @@ -782,8 +1098,6 @@ ext4_xattr_block_set(handle_t *handle, s
642 #define header(x) ((struct ext4_xattr_header *)(x))
644 - if (i->value && i->value_len > sb->s_blocksize)
647 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
649 @@ -799,7 +1113,7 @@ ext4_xattr_block_set(handle_t *handle, s
652 ea_bdebug(bs->bh, "modifying in-place");
653 - error = ext4_xattr_set_entry(i, s);
654 + error = ext4_xattr_set_entry(i, s, handle, inode);
656 if (!IS_LAST_ENTRY(s->first))
657 ext4_xattr_rehash(header(s->base),
658 @@ -850,7 +1164,7 @@ ext4_xattr_block_set(handle_t *handle, s
659 s->end = s->base + sb->s_blocksize;
662 - error = ext4_xattr_set_entry(i, s);
663 + error = ext4_xattr_set_entry(i, s, handle, inode);
667 @@ -1000,7 +1314,7 @@ int ext4_xattr_ibody_find(struct inode *
668 /* Find the named attribute. */
669 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
671 - (void *)is->s.base, 0);
672 + (void *)is->s.base, 0, inode);
673 if (error && error != -ENODATA)
675 is->s.not_found = error;
676 @@ -1018,7 +1332,7 @@ int ext4_xattr_ibody_inline_set(handle_t
678 if (EXT4_I(inode)->i_extra_isize == 0)
680 - error = ext4_xattr_set_entry(i, s);
681 + error = ext4_xattr_set_entry(i, s, handle, inode);
683 if (error == -ENOSPC &&
684 ext4_has_inline_data(inode)) {
685 @@ -1030,7 +1344,7 @@ int ext4_xattr_ibody_inline_set(handle_t
686 error = ext4_xattr_ibody_find(inode, i, is);
689 - error = ext4_xattr_set_entry(i, s);
690 + error = ext4_xattr_set_entry(i, s, handle, inode);
694 @@ -1056,7 +1370,7 @@ static int ext4_xattr_ibody_set(handle_t
696 if (EXT4_I(inode)->i_extra_isize == 0)
698 - error = ext4_xattr_set_entry(i, s);
699 + error = ext4_xattr_set_entry(i, s, handle, inode);
702 header = IHDR(inode, ext4_raw_inode(&is->iloc));
703 @@ -1092,7 +1406,7 @@ ext4_xattr_set_handle(handle_t *handle,
706 .value_len = value_len,
710 struct ext4_xattr_ibody_find is = {
711 .s = { .not_found = -ENODATA, },
712 @@ -1157,6 +1471,15 @@ ext4_xattr_set_handle(handle_t *handle,
715 error = ext4_xattr_block_set(handle, inode, &i, &bs);
716 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
717 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
718 + error == -ENOSPC) {
719 + /* xattr not fit to block, store at external
722 + error = ext4_xattr_ibody_set(handle, inode,
727 if (!is.s.not_found) {
728 @@ -1203,9 +1526,22 @@ ext4_xattr_set(struct inode *inode, int
729 const void *value, size_t value_len, int flags)
732 + struct super_block *sb = inode->i_sb;
733 int error, retries = 0;
734 int credits = ext4_jbd2_credits_xattr(inode);
736 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
737 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
738 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
739 + sb->s_blocksize_bits;
741 + /* For new inode */
742 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
744 + /* For data blocks of EA inode */
745 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
749 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
750 if (IS_ERR(handle)) {
751 @@ -1217,7 +1553,7 @@ retry:
752 value, value_len, flags);
753 error2 = ext4_journal_stop(handle);
754 if (error == -ENOSPC &&
755 - ext4_should_retry_alloc(inode->i_sb, &retries))
756 + ext4_should_retry_alloc(sb, &retries))
760 @@ -1239,7 +1575,7 @@ static void ext4_xattr_shift_entries(str
762 /* Adjust the value offsets of the entries */
763 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
764 - if (!last->e_value_block && last->e_value_size) {
765 + if (!last->e_value_inum && last->e_value_size) {
766 new_offs = le16_to_cpu(last->e_value_offs) +
768 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
769 @@ -1477,21 +1813,135 @@ cleanup:
773 +#define EIA_INCR 16 /* must be 2^n */
774 +#define EIA_MASK (EIA_INCR - 1)
775 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
776 + * If @lea_ino_array is new or full it will be grown and the old
777 + * contents copied over.
780 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
782 + if (*lea_ino_array == NULL) {
784 + * Start with 15 inodes, so it fits into a power-of-two size.
785 + * If *lea_ino_array is NULL, this is essentially offsetof()
788 + kmalloc(offsetof(struct ext4_xattr_ino_array,
789 + xia_inodes[EIA_MASK]),
791 + if (*lea_ino_array == NULL)
793 + (*lea_ino_array)->xia_count = 0;
794 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
795 + /* expand the array once all 15 + n * 16 slots are full */
796 + struct ext4_xattr_ino_array *new_array = NULL;
797 + int count = (*lea_ino_array)->xia_count;
799 + /* if new_array is NULL, this is essentially offsetof() */
800 + new_array = kmalloc(
801 + offsetof(struct ext4_xattr_ino_array,
802 + xia_inodes[count + EIA_INCR]),
804 + if (new_array == NULL)
806 + memcpy(new_array, *lea_ino_array,
807 + offsetof(struct ext4_xattr_ino_array,
808 + xia_inodes[count]));
809 + kfree(*lea_ino_array);
810 + *lea_ino_array = new_array;
812 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
817 + * Add xattr inode to orphan list
820 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
821 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
823 + struct inode *ea_inode = NULL;
824 + int idx = 0, error = 0;
826 + if (lea_ino_array == NULL)
829 + for (; idx < lea_ino_array->xia_count; ++idx) {
830 + if (!ext4_handle_has_enough_credits(handle, credits)) {
831 + error = ext4_journal_extend(handle, credits);
833 + error = ext4_journal_restart(handle, credits);
836 + ext4_warning(inode->i_sb,
837 + "couldn't extend journal "
838 + "(err %d)", error);
842 + ea_inode = ext4_xattr_inode_iget(inode,
843 + lea_ino_array->xia_inodes[idx], &error);
846 + ext4_orphan_add(handle, ea_inode);
847 + /* the inode's i_count will be released by caller */
854 * ext4_xattr_delete_inode()
856 - * Free extended attribute resources associated with this inode. This
857 + * Free extended attribute resources associated with this inode. Traverse
858 + * all entries and unlink any xattr inodes associated with this inode. This
859 * is called immediately before an inode is freed. We have exclusive
860 - * access to the inode.
861 + * access to the inode. If an orphan inode is deleted it will also delete any
862 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
863 + * to ensure they belong to the parent inode and were not deleted already.
866 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
868 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
869 + struct ext4_xattr_ino_array **lea_ino_array)
871 struct buffer_head *bh = NULL;
872 + struct ext4_xattr_ibody_header *header;
873 + struct ext4_inode *raw_inode;
874 + struct ext4_iloc iloc;
875 + struct ext4_xattr_entry *entry;
876 + int credits = 3, error = 0;
878 - if (!EXT4_I(inode)->i_file_acl)
879 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
880 + goto delete_external_ea;
882 + error = ext4_get_inode_loc(inode, &iloc);
885 + raw_inode = ext4_raw_inode(&iloc);
886 + header = IHDR(inode, raw_inode);
887 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
888 + entry = EXT4_XATTR_NEXT(entry)) {
889 + if (!entry->e_value_inum)
891 + if (ext4_expand_ino_array(lea_ino_array,
892 + entry->e_value_inum) != 0) {
896 + entry->e_value_inum = 0;
901 + if (!EXT4_I(inode)->i_file_acl) {
902 + /* add xattr inode to orphan list */
903 + ext4_xattr_inode_orphan_add(handle, inode, credits,
907 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
909 EXT4_ERROR_INODE(inode, "block %llu read error",
910 @@ -1504,11 +1954,69 @@ ext4_xattr_delete_inode(handle_t *handle
911 EXT4_I(inode)->i_file_acl);
915 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
916 + entry = EXT4_XATTR_NEXT(entry)) {
917 + if (!entry->e_value_inum)
919 + if (ext4_expand_ino_array(lea_ino_array,
920 + entry->e_value_inum) != 0)
922 + entry->e_value_inum = 0;
925 + /* add xattr inode to orphan list */
926 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
931 + if (!IS_NOQUOTA(inode))
932 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
934 + if (!ext4_handle_has_enough_credits(handle, credits)) {
935 + error = ext4_journal_extend(handle, credits);
937 + error = ext4_journal_restart(handle, credits);
939 + ext4_warning(inode->i_sb,
940 + "couldn't extend journal (err %d)", error);
945 ext4_xattr_release_block(handle, inode, bh);
946 EXT4_I(inode)->i_file_acl = 0;
955 +ext4_xattr_inode_array_free(struct inode *inode,
956 + struct ext4_xattr_ino_array *lea_ino_array)
958 + struct inode *ea_inode = NULL;
962 + if (lea_ino_array == NULL)
965 + for (; idx < lea_ino_array->xia_count; ++idx) {
966 + ea_inode = ext4_xattr_inode_iget(inode,
967 + lea_ino_array->xia_inodes[idx], &err);
970 + /* for inode's i_count get from ext4_xattr_delete_inode */
971 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
973 + clear_nlink(ea_inode);
976 + kfree(lea_ino_array);
980 @@ -1578,10 +2086,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
981 entry1->e_name_index != entry2->e_name_index ||
982 entry1->e_name_len != entry2->e_name_len ||
983 entry1->e_value_size != entry2->e_value_size ||
984 + entry1->e_value_inum != entry2->e_value_inum ||
985 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
987 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
989 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
990 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
991 le32_to_cpu(entry1->e_value_size)))
992 @@ -1665,7 +2172,7 @@ static inline void ext4_xattr_hash_entry
996 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
997 + if (!entry->e_value_inum && entry->e_value_size) {
998 __le32 *value = (__le32 *)((char *)header +
999 le16_to_cpu(entry->e_value_offs));
1000 for (n = (le32_to_cpu(entry->e_value_size) +
1001 Index: linux-stage/fs/ext4/xattr.h
1002 ===================================================================
1003 --- linux-stage.orig/fs/ext4/xattr.h
1004 +++ linux-stage/fs/ext4/xattr.h
1005 @@ -42,7 +42,7 @@ struct ext4_xattr_entry {
1006 __u8 e_name_len; /* length of name */
1007 __u8 e_name_index; /* attribute name index */
1008 __le16 e_value_offs; /* offset in disk block of value */
1009 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1010 + __le32 e_value_inum; /* inode in which the value is stored */
1011 __le32 e_value_size; /* size of attribute value */
1012 __le32 e_hash; /* hash value of name and value */
1013 char e_name[0]; /* attribute name */
1014 @@ -67,6 +67,26 @@ struct ext4_xattr_entry {
1015 EXT4_I(inode)->i_extra_isize))
1016 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1019 + * Link EA inode back to parent one using i_mtime field.
1020 + * Extra integer type conversion added to ignore higher
1021 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1023 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1025 + (inode)->i_mtime.tv_sec = inum; \
1028 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1029 + ((__u32)(inode)->i_mtime.tv_sec)
1032 + * The minimum size of EA value when you start storing it in an external inode
1033 + * size of block - size of header - size of 1 entry - 4 null bytes
1035 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1036 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1038 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1039 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1040 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1041 @@ -75,10 +84,11 @@ struct ext4_xattr_entry {
1042 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1044 struct ext4_xattr_info {
1053 struct ext4_xattr_search {
1054 @@ -106,7 +116,13 @@ extern int ext4_xattr_get(struct inode *
1055 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1056 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1058 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1059 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1061 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1062 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1063 + struct ext4_xattr_ino_array **array);
1064 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1065 + struct ext4_xattr_ino_array *array);
1066 extern void ext4_xattr_put_super(struct super_block *);
1068 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1069 Index: linux-stage/fs/ext4/ialloc.c
1070 ===================================================================
1071 --- linux-stage.orig/fs/ext4/ialloc.c
1072 +++ linux-stage/fs/ext4/ialloc.c
1073 @@ -269,7 +269,6 @@ void ext4_free_inode(handle_t *handle, s
1074 * as writing the quota to disk may need the lock as well.
1076 dquot_initialize(inode);
1077 - ext4_xattr_delete_inode(handle, inode);
1078 dquot_free_inode(inode);
1081 Index: linux-stage/fs/ext4/inline.c
1082 ===================================================================
1083 --- linux-stage.orig/fs/ext4/inline.c
1084 +++ linux-stage/fs/ext4/inline.c
1085 @@ -59,7 +59,7 @@ static int get_max_inline_xattr_value_si
1087 /* Compute min_offs. */
1088 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
1089 - if (!entry->e_value_block && entry->e_value_size) {
1090 + if (!entry->e_value_inum && entry->e_value_size) {
1091 size_t offs = le16_to_cpu(entry->e_value_offs);
1092 if (offs < min_offs)