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 @@ -249,9 +249,36 @@ void ext4_evict_inode(struct inode *inod
72 sb_end_intwrite(inode->i_sb);
77 ext4_handle_sync(handle);
80 + * Delete xattr inode before deleting the main inode.
82 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
84 + ext4_warning(inode->i_sb,
85 + "couldn't delete inode's xattr (err %d)", err);
89 + if (!IS_NOQUOTA(inode))
90 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
92 + if (!ext4_handle_has_enough_credits(handle,
93 + ext4_blocks_for_truncate(inode) + extra_credits)) {
94 + err = ext4_journal_extend(handle,
95 + ext4_blocks_for_truncate(inode) + extra_credits);
97 + err = ext4_journal_restart(handle,
98 + ext4_blocks_for_truncate(inode) + extra_credits);
100 + ext4_warning(inode->i_sb,
101 + "couldn't extend journal (err %d)", err);
107 err = ext4_mark_inode_dirty(handle, inode);
109 @@ -269,10 +296,10 @@ void ext4_evict_inode(struct inode *inod
110 * enough credits left in the handle to remove the inode from
111 * the orphan list and set the dtime field.
113 - if (!ext4_handle_has_enough_credits(handle, 3)) {
114 - err = ext4_journal_extend(handle, 3);
115 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
116 + err = ext4_journal_extend(handle, extra_credits);
118 - err = ext4_journal_restart(handle, 3);
119 + err = ext4_journal_restart(handle, extra_credits);
121 ext4_warning(inode->i_sb,
122 "couldn't extend journal (err %d)", err);
123 @@ -306,8 +333,12 @@ void ext4_evict_inode(struct inode *inod
124 ext4_clear_inode(inode);
126 ext4_free_inode(handle, inode);
128 ext4_journal_stop(handle);
129 sb_end_intwrite(inode->i_sb);
131 + if (lea_ino_array != NULL)
132 + ext4_xattr_inode_array_free(inode, lea_ino_array);
135 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
136 @@ -4681,7 +4712,7 @@ static int ext4_index_trans_blocks(struc
138 * Also account for superblock, inode, quota and xattr blocks
140 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
141 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
144 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
145 Index: linux-stage/fs/ext4/xattr.c
146 ===================================================================
147 --- linux-stage.orig/fs/ext4/xattr.c
148 +++ linux-stage/fs/ext4/xattr.c
149 @@ -201,6 +201,7 @@ ext4_xattr_check_names(struct ext4_xattr
151 while (!IS_LAST_ENTRY(entry)) {
152 if (entry->e_value_size != 0 &&
153 + entry->e_value_inum == 0 &&
154 (value_start + le16_to_cpu(entry->e_value_offs) <
155 (void *)e + sizeof(__u32) ||
156 value_start + le16_to_cpu(entry->e_value_offs) +
157 @@ -233,19 +234,26 @@ ext4_xattr_check_block(struct inode *ino
161 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
162 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
163 + struct inode *inode)
165 size_t value_size = le32_to_cpu(entry->e_value_size);
167 - if (entry->e_value_block != 0 || value_size > size ||
168 + if (!entry->e_value_inum &&
169 le16_to_cpu(entry->e_value_offs) + value_size > size)
170 return -EFSCORRUPTED;
171 + if (entry->e_value_inum &&
172 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
173 + le32_to_cpu(entry->e_value_inum) >
174 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
175 + return -EFSCORRUPTED;
180 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
181 - const char *name, size_t size, int sorted)
182 + const char *name, size_t size, int sorted,
183 + struct inode *inode)
185 struct ext4_xattr_entry *entry;
187 @@ -265,11 +273,104 @@ ext4_xattr_find_entry(struct ext4_xattr_
191 - if (!cmp && ext4_xattr_check_entry(entry, size))
192 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
193 return -EFSCORRUPTED;
194 return cmp ? -ENODATA : 0;
198 + * Read the EA value from an inode.
201 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
203 + unsigned long block = 0;
204 + struct buffer_head *bh = NULL;
206 + size_t csize, ret_size = 0;
211 + blocksize = ea_inode->i_sb->s_blocksize;
213 + while (ret_size < *size) {
214 + csize = (*size - ret_size) > blocksize ? blocksize :
216 + bh = ext4_bread(NULL, ea_inode, block, 0);
219 + return PTR_ERR(bh);
221 + memcpy(buf, bh->b_data, csize);
234 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
236 + struct inode *ea_inode = NULL;
238 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
239 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
240 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
241 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
242 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
243 + *err = rc != 0 ? rc : -EIO;
247 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
248 + ea_inode->i_generation != parent->i_generation) {
249 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
250 + "to parent invalid.", ea_ino);
255 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
256 + ext4_error(parent->i_sb, "EA inode %lu does not have "
257 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
271 + * Read the value from the EA inode.
274 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
277 + struct inode *ea_inode = NULL;
280 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
284 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
291 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
292 void *buffer, size_t buffer_size)
293 @@ -301,7 +401,8 @@ bad_block:
295 ext4_xattr_cache_insert(bh);
297 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
298 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
300 if (error == -EFSCORRUPTED)
303 @@ -311,8 +412,16 @@ bad_block:
305 if (size > buffer_size)
307 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
309 + if (entry->e_value_inum) {
310 + error = ext4_xattr_inode_get(inode,
311 + le32_to_cpu(entry->e_value_inum),
316 + memcpy(buffer, bh->b_data +
317 + le16_to_cpu(entry->e_value_offs), size);
322 @@ -346,7 +455,7 @@ ext4_xattr_ibody_get(struct inode *inode
325 error = ext4_xattr_find_entry(&entry, name_index, name,
326 - end - (void *)entry, 0);
327 + end - (void *)entry, 0, inode);
330 size = le32_to_cpu(entry->e_value_size);
331 @@ -354,8 +463,16 @@ ext4_xattr_ibody_get(struct inode *inode
333 if (size > buffer_size)
335 - memcpy(buffer, (void *)IFIRST(header) +
336 - le16_to_cpu(entry->e_value_offs), size);
337 + if (entry->e_value_inum) {
338 + error = ext4_xattr_inode_get(inode,
339 + le32_to_cpu(entry->e_value_inum),
344 + memcpy(buffer, (void *)IFIRST(header) +
345 + le16_to_cpu(entry->e_value_offs), size);
350 @@ -600,7 +717,7 @@ static size_t ext4_xattr_free_space(stru
351 size_t *min_offs, void *base, int *total)
353 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
354 - if (!last->e_value_block && last->e_value_size) {
355 + if (!last->e_value_inum && last->e_value_size) {
356 size_t offs = le16_to_cpu(last->e_value_offs);
357 if (offs < *min_offs)
359 @@ -611,16 +728,200 @@ static size_t ext4_xattr_free_space(stru
360 return (*min_offs - ((void *)last - base) - sizeof(__u32));
364 + * Write the value of the EA in an inode.
367 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
368 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
369 + const void *buf, int bufsize)
371 + struct buffer_head *bh = NULL;
372 + unsigned long block = 0;
373 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
374 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
375 + int csize, wsize = 0;
380 + while (ret >= 0 && ret < max_blocks) {
381 + struct ext4_map_blocks map;
382 + map.m_lblk = block += ret;
383 + map.m_len = max_blocks -= ret;
385 + ret = ext4_map_blocks(handle, ea_inode, &map,
386 + EXT4_GET_BLOCKS_CREATE);
388 + ext4_mark_inode_dirty(handle, ea_inode);
389 + if (ret == -ENOSPC &&
390 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
402 + while (wsize < bufsize) {
405 + csize = (bufsize - wsize) > blocksize ? blocksize :
407 + bh = ext4_getblk(handle, ea_inode, block, 0);
412 + ret = ext4_journal_get_write_access(handle, bh);
416 + memcpy(bh->b_data, buf, csize);
417 + set_buffer_uptodate(bh);
418 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
425 + mutex_lock(&ea_inode->i_mutex);
426 + i_size_write(ea_inode, wsize);
427 + ext4_update_i_disksize(ea_inode, wsize);
428 + mutex_unlock(&ea_inode->i_mutex);
430 + ext4_mark_inode_dirty(handle, ea_inode);
438 +static void ext4_xattr_inode_set_ref(struct inode *ea_inode, __u64 ref_count)
440 + ea_inode->i_ctime.tv_sec = (__u32)(ref_count >> 32);
441 + ea_inode->i_version = (__u32)ref_count;
444 +static void ext4_xattr_inode_set_hash(struct inode *ea_inode, __u32 hash)
446 + ea_inode->i_atime.tv_sec = hash;
450 + * Create an inode to store the value of a large EA.
452 +static struct inode *
453 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode, __u32 hash)
455 + struct inode *ea_inode = NULL;
458 + * Let the next inode be the goal, so we try and allocate the EA inode
459 + * in the same group, or nearby one.
461 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
462 + S_IFREG|0600, NULL, inode->i_ino + 1, NULL);
464 + if (!IS_ERR(ea_inode)) {
465 + ea_inode->i_op = &ext4_file_inode_operations;
466 + ea_inode->i_fop = &ext4_file_operations;
467 + ext4_set_aops(ea_inode);
468 + ea_inode->i_generation = inode->i_generation;
469 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
472 + * A back-pointer from EA inode to parent inode will be useful
475 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
476 + unlock_new_inode(ea_inode);
478 + ext4_xattr_inode_set_ref(ea_inode, 1);
479 + ext4_xattr_inode_set_hash(ea_inode, hash);
486 + * Unlink the inode storing the value of the EA.
489 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
491 + struct inode *ea_inode = NULL;
494 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
498 + clear_nlink(ea_inode);
505 +ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
507 + if (ext4_has_metadata_csum(sbi->s_sb))
508 + return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
513 + * Add value of the EA in an inode.
516 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
517 + const void *value, size_t value_len)
519 + struct inode *ea_inode = NULL;
523 + /* Create an inode for the EA value */
524 + hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
525 + ea_inode = ext4_xattr_inode_create(handle, inode, hash);
526 + if (IS_ERR(ea_inode))
529 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
531 + clear_nlink(ea_inode);
533 + *ea_ino = ea_inode->i_ino;
541 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
542 + handle_t *handle, struct inode *inode)
544 struct ext4_xattr_entry *last;
545 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
546 + int in_inode = i->in_inode;
548 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
549 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
550 + (EXT4_XATTR_SIZE(i->value_len) >
551 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
554 /* Compute min_offs and last. */
556 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
557 - if (!last->e_value_block && last->e_value_size) {
558 + if (!last->e_value_inum && last->e_value_size) {
559 size_t offs = le16_to_cpu(last->e_value_offs);
562 @@ -628,15 +927,20 @@ ext4_xattr_set_entry(struct ext4_xattr_i
564 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
566 - if (!s->here->e_value_block && s->here->e_value_size) {
568 + !s->here->e_value_inum && s->here->e_value_size) {
569 size_t size = le32_to_cpu(s->here->e_value_size);
570 free += EXT4_XATTR_SIZE(size);
572 free += EXT4_XATTR_LEN(name_len);
575 - if (free < EXT4_XATTR_LEN(name_len) +
576 - EXT4_XATTR_SIZE(i->value_len))
577 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
582 + if (free < EXT4_XATTR_LEN(name_len) + value_len)
586 @@ -651,7 +955,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
587 s->here->e_name_len = name_len;
588 memcpy(s->here->e_name, i->name, name_len);
590 - if (!s->here->e_value_block && s->here->e_value_size) {
591 + if (!s->here->e_value_inum && s->here->e_value_size &&
592 + s->here->e_value_offs > 0) {
593 void *first_val = s->base + min_offs;
594 size_t offs = le16_to_cpu(s->here->e_value_offs);
595 void *val = s->base + offs;
596 @@ -685,13 +990,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
598 while (!IS_LAST_ENTRY(last)) {
599 size_t o = le16_to_cpu(last->e_value_offs);
600 - if (!last->e_value_block &&
601 + if (!last->e_value_inum &&
602 last->e_value_size && o < offs)
604 cpu_to_le16(o + size);
605 last = EXT4_XATTR_NEXT(last);
608 + if (s->here->e_value_inum) {
609 + ext4_xattr_inode_unlink(inode,
610 + le32_to_cpu(s->here->e_value_inum));
611 + s->here->e_value_inum = 0;
614 /* Remove the old name. */
615 size_t size = EXT4_XATTR_LEN(name_len);
616 @@ -705,10 +1014,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
618 /* Insert the new value. */
619 s->here->e_value_size = cpu_to_le32(i->value_len);
620 - if (i->value_len) {
622 + unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
623 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
625 + s->here->e_value_inum = cpu_to_le32(ea_ino);
626 + s->here->e_value_offs = 0;
627 + } else if (i->value_len) {
628 size_t size = EXT4_XATTR_SIZE(i->value_len);
629 void *val = s->base + min_offs - size;
630 s->here->e_value_offs = cpu_to_le16(min_offs - size);
631 + s->here->e_value_inum = 0;
632 if (i->value == EXT4_ZERO_XATTR_VALUE) {
633 memset(val, 0, size);
635 @@ -758,7 +1074,7 @@ ext4_xattr_block_find(struct inode *inod
636 bs->s.end = bs->bh->b_data + bs->bh->b_size;
637 bs->s.here = bs->s.first;
638 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
639 - i->name, bs->bh->b_size, 1);
640 + i->name, bs->bh->b_size, 1, inode);
641 if (error && error != -ENODATA)
643 bs->s.not_found = error;
644 @@ -782,8 +1098,6 @@ ext4_xattr_block_set(handle_t *handle, s
646 #define header(x) ((struct ext4_xattr_header *)(x))
648 - if (i->value && i->value_len > sb->s_blocksize)
651 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
653 @@ -799,7 +1113,7 @@ ext4_xattr_block_set(handle_t *handle, s
656 ea_bdebug(bs->bh, "modifying in-place");
657 - error = ext4_xattr_set_entry(i, s);
658 + error = ext4_xattr_set_entry(i, s, handle, inode);
660 if (!IS_LAST_ENTRY(s->first))
661 ext4_xattr_rehash(header(s->base),
662 @@ -850,7 +1164,7 @@ ext4_xattr_block_set(handle_t *handle, s
663 s->end = s->base + sb->s_blocksize;
666 - error = ext4_xattr_set_entry(i, s);
667 + error = ext4_xattr_set_entry(i, s, handle, inode);
668 if (error == -EFSCORRUPTED)
671 @@ -1000,7 +1314,7 @@ int ext4_xattr_ibody_find(struct inode *
672 /* Find the named attribute. */
673 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
675 - (void *)is->s.base, 0);
676 + (void *)is->s.base, 0, inode);
677 if (error && error != -ENODATA)
679 is->s.not_found = error;
680 @@ -1018,7 +1332,7 @@ int ext4_xattr_ibody_inline_set(handle_t
682 if (EXT4_I(inode)->i_extra_isize == 0)
684 - error = ext4_xattr_set_entry(i, s);
685 + error = ext4_xattr_set_entry(i, s, handle, inode);
687 if (error == -ENOSPC &&
688 ext4_has_inline_data(inode)) {
689 @@ -1030,7 +1344,7 @@ int ext4_xattr_ibody_inline_set(handle_t
690 error = ext4_xattr_ibody_find(inode, i, is);
693 - error = ext4_xattr_set_entry(i, s);
694 + error = ext4_xattr_set_entry(i, s, handle, inode);
698 @@ -1056,7 +1370,7 @@ static int ext4_xattr_ibody_set(handle_t
700 if (EXT4_I(inode)->i_extra_isize == 0)
702 - error = ext4_xattr_set_entry(i, s);
703 + error = ext4_xattr_set_entry(i, s, handle, inode);
706 header = IHDR(inode, ext4_raw_inode(&is->iloc));
707 @@ -1092,7 +1406,7 @@ ext4_xattr_set_handle(handle_t *handle,
710 .value_len = value_len,
714 struct ext4_xattr_ibody_find is = {
715 .s = { .not_found = -ENODATA, },
716 @@ -1157,6 +1471,15 @@ ext4_xattr_set_handle(handle_t *handle,
719 error = ext4_xattr_block_set(handle, inode, &i, &bs);
720 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
721 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
722 + error == -ENOSPC) {
723 + /* xattr not fit to block, store at external
726 + error = ext4_xattr_ibody_set(handle, inode,
731 if (!is.s.not_found) {
732 @@ -1203,9 +1526,22 @@ ext4_xattr_set(struct inode *inode, int
733 const void *value, size_t value_len, int flags)
736 + struct super_block *sb = inode->i_sb;
737 int error, retries = 0;
738 int credits = ext4_jbd2_credits_xattr(inode);
740 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
741 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
742 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
743 + sb->s_blocksize_bits;
745 + /* For new inode */
746 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
748 + /* For data blocks of EA inode */
749 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
753 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
754 if (IS_ERR(handle)) {
755 @@ -1217,7 +1553,7 @@ retry:
756 value, value_len, flags);
757 error2 = ext4_journal_stop(handle);
758 if (error == -ENOSPC &&
759 - ext4_should_retry_alloc(inode->i_sb, &retries))
760 + ext4_should_retry_alloc(sb, &retries))
764 @@ -1239,7 +1575,7 @@ static void ext4_xattr_shift_entries(str
766 /* Adjust the value offsets of the entries */
767 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
768 - if (!last->e_value_block && last->e_value_size) {
769 + if (!last->e_value_inum && last->e_value_size) {
770 new_offs = le16_to_cpu(last->e_value_offs) +
772 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
773 @@ -1477,21 +1813,135 @@ cleanup:
777 +#define EIA_INCR 16 /* must be 2^n */
778 +#define EIA_MASK (EIA_INCR - 1)
779 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
780 + * If @lea_ino_array is new or full it will be grown and the old
781 + * contents copied over.
784 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
786 + if (*lea_ino_array == NULL) {
788 + * Start with 15 inodes, so it fits into a power-of-two size.
789 + * If *lea_ino_array is NULL, this is essentially offsetof()
792 + kmalloc(offsetof(struct ext4_xattr_ino_array,
793 + xia_inodes[EIA_MASK]),
795 + if (*lea_ino_array == NULL)
797 + (*lea_ino_array)->xia_count = 0;
798 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
799 + /* expand the array once all 15 + n * 16 slots are full */
800 + struct ext4_xattr_ino_array *new_array = NULL;
801 + int count = (*lea_ino_array)->xia_count;
803 + /* if new_array is NULL, this is essentially offsetof() */
804 + new_array = kmalloc(
805 + offsetof(struct ext4_xattr_ino_array,
806 + xia_inodes[count + EIA_INCR]),
808 + if (new_array == NULL)
810 + memcpy(new_array, *lea_ino_array,
811 + offsetof(struct ext4_xattr_ino_array,
812 + xia_inodes[count]));
813 + kfree(*lea_ino_array);
814 + *lea_ino_array = new_array;
816 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
821 + * Add xattr inode to orphan list
824 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
825 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
827 + struct inode *ea_inode = NULL;
828 + int idx = 0, error = 0;
830 + if (lea_ino_array == NULL)
833 + for (; idx < lea_ino_array->xia_count; ++idx) {
834 + if (!ext4_handle_has_enough_credits(handle, credits)) {
835 + error = ext4_journal_extend(handle, credits);
837 + error = ext4_journal_restart(handle, credits);
840 + ext4_warning(inode->i_sb,
841 + "couldn't extend journal "
842 + "(err %d)", error);
846 + ea_inode = ext4_xattr_inode_iget(inode,
847 + lea_ino_array->xia_inodes[idx], &error);
850 + ext4_orphan_add(handle, ea_inode);
851 + /* the inode's i_count will be released by caller */
858 * ext4_xattr_delete_inode()
860 - * Free extended attribute resources associated with this inode. This
861 + * Free extended attribute resources associated with this inode. Traverse
862 + * all entries and unlink any xattr inodes associated with this inode. This
863 * is called immediately before an inode is freed. We have exclusive
864 - * access to the inode.
865 + * access to the inode. If an orphan inode is deleted it will also delete any
866 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
867 + * to ensure they belong to the parent inode and were not deleted already.
870 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
872 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
873 + struct ext4_xattr_ino_array **lea_ino_array)
875 struct buffer_head *bh = NULL;
876 + struct ext4_xattr_ibody_header *header;
877 + struct ext4_inode *raw_inode;
878 + struct ext4_iloc iloc;
879 + struct ext4_xattr_entry *entry;
880 + int credits = 3, error = 0;
882 - if (!EXT4_I(inode)->i_file_acl)
883 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
884 + goto delete_external_ea;
886 + error = ext4_get_inode_loc(inode, &iloc);
889 + raw_inode = ext4_raw_inode(&iloc);
890 + header = IHDR(inode, raw_inode);
891 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
892 + entry = EXT4_XATTR_NEXT(entry)) {
893 + if (!entry->e_value_inum)
895 + if (ext4_expand_ino_array(lea_ino_array,
896 + entry->e_value_inum) != 0) {
900 + entry->e_value_inum = 0;
905 + if (!EXT4_I(inode)->i_file_acl) {
906 + /* add xattr inode to orphan list */
907 + ext4_xattr_inode_orphan_add(handle, inode, credits,
911 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
913 EXT4_ERROR_INODE(inode, "block %llu read error",
914 @@ -1504,11 +1954,69 @@ ext4_xattr_delete_inode(handle_t *handle
915 EXT4_I(inode)->i_file_acl);
919 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
920 + entry = EXT4_XATTR_NEXT(entry)) {
921 + if (!entry->e_value_inum)
923 + if (ext4_expand_ino_array(lea_ino_array,
924 + entry->e_value_inum) != 0)
926 + entry->e_value_inum = 0;
929 + /* add xattr inode to orphan list */
930 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
935 + if (!IS_NOQUOTA(inode))
936 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
938 + if (!ext4_handle_has_enough_credits(handle, credits)) {
939 + error = ext4_journal_extend(handle, credits);
941 + error = ext4_journal_restart(handle, credits);
943 + ext4_warning(inode->i_sb,
944 + "couldn't extend journal (err %d)", error);
949 ext4_xattr_release_block(handle, inode, bh);
950 EXT4_I(inode)->i_file_acl = 0;
959 +ext4_xattr_inode_array_free(struct inode *inode,
960 + struct ext4_xattr_ino_array *lea_ino_array)
962 + struct inode *ea_inode = NULL;
966 + if (lea_ino_array == NULL)
969 + for (; idx < lea_ino_array->xia_count; ++idx) {
970 + ea_inode = ext4_xattr_inode_iget(inode,
971 + lea_ino_array->xia_inodes[idx], &err);
974 + /* for inode's i_count get from ext4_xattr_delete_inode */
975 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
977 + clear_nlink(ea_inode);
980 + kfree(lea_ino_array);
984 @@ -1578,10 +2086,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
985 entry1->e_name_index != entry2->e_name_index ||
986 entry1->e_name_len != entry2->e_name_len ||
987 entry1->e_value_size != entry2->e_value_size ||
988 + entry1->e_value_inum != entry2->e_value_inum ||
989 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
991 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
992 - return -EFSCORRUPTED;
993 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
994 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
995 le32_to_cpu(entry1->e_value_size)))
996 @@ -1665,7 +2172,7 @@ static inline void ext4_xattr_hash_entry
1000 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1001 + if (!entry->e_value_inum && entry->e_value_size) {
1002 __le32 *value = (__le32 *)((char *)header +
1003 le16_to_cpu(entry->e_value_offs));
1004 for (n = (le32_to_cpu(entry->e_value_size) +
1005 Index: linux-stage/fs/ext4/xattr.h
1006 ===================================================================
1007 --- linux-stage.orig/fs/ext4/xattr.h
1008 +++ linux-stage/fs/ext4/xattr.h
1009 @@ -42,7 +42,7 @@ struct ext4_xattr_entry {
1010 __u8 e_name_len; /* length of name */
1011 __u8 e_name_index; /* attribute name index */
1012 __le16 e_value_offs; /* offset in disk block of value */
1013 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1014 + __le32 e_value_inum; /* inode in which the value is stored */
1015 __le32 e_value_size; /* size of attribute value */
1016 __le32 e_hash; /* hash value of name and value */
1017 char e_name[0]; /* attribute name */
1018 @@ -67,6 +67,26 @@ struct ext4_xattr_entry {
1019 EXT4_I(inode)->i_extra_isize))
1020 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1023 + * Link EA inode back to parent one using i_mtime field.
1024 + * Extra integer type conversion added to ignore higher
1025 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1027 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1029 + (inode)->i_mtime.tv_sec = inum; \
1032 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1033 +((__u32)(inode)->i_mtime.tv_sec)
1036 + * The minimum size of EA value when you start storing it in an external inode
1037 + * size of block - size of header - size of 1 entry - 4 null bytes
1039 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1040 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1042 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1043 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1044 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1045 @@ -75,10 +84,11 @@ struct ext4_xattr_entry {
1046 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1048 struct ext4_xattr_info {
1057 struct ext4_xattr_search {
1058 @@ -106,7 +116,13 @@ extern int ext4_xattr_get(struct inode *
1059 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1060 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1062 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1063 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1065 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1066 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1067 + struct ext4_xattr_ino_array **array);
1068 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1069 + struct ext4_xattr_ino_array *array);
1071 extern void ext4_xattr_put_super(struct super_block *);
1072 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1073 Index: linux-stage/fs/ext4/ialloc.c
1074 ===================================================================
1075 --- linux-stage.orig/fs/ext4/ialloc.c
1076 +++ linux-stage/fs/ext4/ialloc.c
1077 @@ -269,7 +269,6 @@ void ext4_free_inode(handle_t *handle, s
1078 * as writing the quota to disk may need the lock as well.
1080 dquot_initialize(inode);
1081 - ext4_xattr_delete_inode(handle, inode);
1082 dquot_free_inode(inode);
1085 Index: linux-stage/fs/ext4/inline.c
1086 ===================================================================
1087 --- linux-stage.orig/fs/ext4/inline.c
1088 +++ linux-stage/fs/ext4/inline.c
1089 @@ -59,7 +59,7 @@ static int get_max_inline_xattr_value_si
1091 /* Compute min_offs. */
1092 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
1093 - if (!entry->e_value_block && entry->e_value_size) {
1094 + if (!entry->e_value_inum && entry->e_value_size) {
1095 size_t offs = le16_to_cpu(entry->e_value_offs);
1096 if (offs < min_offs)