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 @@ -1408,6 +1408,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)
19 @@ -1764,6 +1765,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 @@ -1775,6 +1782,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);
43 @@ -1893,6 +1904,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_block_truncate_page(handle_t *handle,
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 @@ -187,6 +187,8 @@ void ext4_evict_inode(struct inode *inod
59 + int extra_credits = 3;
60 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
62 trace_ext4_evict_inode(inode);
64 @@ -207,7 +209,10 @@ void ext4_evict_inode(struct inode *inod
65 if (is_bad_inode(inode))
68 - handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
70 + * Delete xattr inode before deleting the main inode.
72 + handle = ext4_journal_start(inode, extra_credits);
74 ext4_std_error(inode->i_sb, PTR_ERR(handle));
76 @@ -218,9 +223,33 @@ void ext4_evict_inode(struct inode *inod
77 ext4_orphan_del(NULL, inode);
82 ext4_handle_sync(handle);
84 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
86 + ext4_warning(inode->i_sb,
87 + "couldn't delete inode's xattr (err %d)", err);
91 + if (!IS_NOQUOTA(inode))
92 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
94 + if (!ext4_handle_has_enough_credits(handle,
95 + blocks_for_truncate(inode) + extra_credits)) {
96 + err = ext4_journal_extend(handle,
97 + blocks_for_truncate(inode) + extra_credits);
99 + err = ext4_journal_restart(handle,
100 + blocks_for_truncate(inode) + extra_credits);
102 + ext4_warning(inode->i_sb,
103 + "couldn't extend journal (err %d)", err);
109 err = ext4_mark_inode_dirty(handle, inode);
111 @@ -237,10 +266,10 @@ void ext4_evict_inode(struct inode *inod
112 * enough credits left in the handle to remove the inode from
113 * the orphan list and set the dtime field.
115 - if (!ext4_handle_has_enough_credits(handle, 3)) {
116 - err = ext4_journal_extend(handle, 3);
117 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
118 + err = ext4_journal_extend(handle, extra_credits);
120 - err = ext4_journal_restart(handle, 3);
121 + err = ext4_journal_restart(handle, extra_credits);
123 ext4_warning(inode->i_sb,
124 "couldn't extend journal (err %d)", err);
125 @@ -274,7 +303,11 @@ void ext4_evict_inode(struct inode *inod
126 ext4_clear_inode(inode);
128 ext4_free_inode(handle, inode);
130 ext4_journal_stop(handle);
132 + if (lea_ino_array != NULL)
133 + ext4_xattr_inode_array_free(inode, lea_ino_array);
136 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
137 @@ -5552,7 +5585,7 @@ static int ext4_index_trans_blocks(struc
139 * Also account for superblock, inode, quota and xattr blocks
141 -static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
142 +int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
144 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
146 Index: linux-stage/fs/ext4/xattr.c
147 ===================================================================
148 --- linux-stage.orig/fs/ext4/xattr.c
149 +++ linux-stage/fs/ext4/xattr.c
150 @@ -168,19 +168,26 @@ ext4_xattr_check_block(struct buffer_hea
154 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
155 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
156 + struct inode *inode)
158 size_t value_size = le32_to_cpu(entry->e_value_size);
160 - if (entry->e_value_block != 0 || value_size > size ||
161 + if (!entry->e_value_inum &&
162 le16_to_cpu(entry->e_value_offs) + value_size > size)
164 + if (entry->e_value_inum &&
165 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
166 + le32_to_cpu(entry->e_value_inum) >
167 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
173 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
174 - const char *name, size_t size, int sorted)
175 + const char *name, size_t size, int sorted,
176 + struct inode *inode)
178 struct ext4_xattr_entry *entry;
180 @@ -200,11 +207,104 @@ ext4_xattr_find_entry(struct ext4_xattr_
184 - if (!cmp && ext4_xattr_check_entry(entry, size))
185 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
187 return cmp ? -ENODATA : 0;
191 + * Read the EA value from an inode.
194 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
196 + unsigned long block = 0;
197 + struct buffer_head *bh = NULL;
198 + int err, blocksize;
199 + size_t csize, ret_size = 0;
204 + blocksize = ea_inode->i_sb->s_blocksize;
206 + while (ret_size < *size) {
207 + csize = (*size - ret_size) > blocksize ? blocksize :
209 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
214 + memcpy(buf, bh->b_data, csize);
227 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
229 + struct inode *ea_inode = NULL;
231 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
232 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
233 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
234 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
235 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
236 + *err = rc != 0 ? rc : -EIO;
240 + if (ea_inode->i_xattr_inode_parent != parent->i_ino ||
241 + ea_inode->i_generation != parent->i_generation) {
242 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
243 + "to parent invalid.", ea_ino);
248 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
249 + ext4_error(parent->i_sb, "EA inode %lu does not have "
250 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
264 + * Read the value from the EA inode.
267 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
270 + struct inode *ea_inode = NULL;
273 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
277 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
284 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
285 void *buffer, size_t buffer_size)
286 @@ -235,7 +334,8 @@ bad_block:
288 ext4_xattr_cache_insert(bh);
290 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
291 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
296 @@ -245,8 +345,16 @@ bad_block:
298 if (size > buffer_size)
300 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
302 + if (entry->e_value_inum) {
303 + error = ext4_xattr_inode_get(inode,
304 + le32_to_cpu(entry->e_value_inum),
309 + memcpy(buffer, bh->b_data +
310 + le16_to_cpu(entry->e_value_offs), size);
315 @@ -280,7 +388,7 @@ ext4_xattr_ibody_get(struct inode *inode
318 error = ext4_xattr_find_entry(&entry, name_index, name,
319 - end - (void *)entry, 0);
320 + end - (void *)entry, 0, inode);
323 size = le32_to_cpu(entry->e_value_size);
324 @@ -288,8 +396,16 @@ ext4_xattr_ibody_get(struct inode *inode
326 if (size > buffer_size)
328 - memcpy(buffer, (void *)IFIRST(header) +
329 - le16_to_cpu(entry->e_value_offs), size);
330 + if (entry->e_value_inum) {
331 + error = ext4_xattr_inode_get(inode,
332 + le32_to_cpu(entry->e_value_inum),
337 + memcpy(buffer, (void *)IFIRST(header) +
338 + le16_to_cpu(entry->e_value_offs), size);
343 @@ -514,7 +630,7 @@ static size_t ext4_xattr_free_space(stru
345 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
346 *total += EXT4_XATTR_LEN(last->e_name_len);
347 - if (!last->e_value_block && last->e_value_size) {
348 + if (!last->e_value_inum && last->e_value_size) {
349 size_t offs = le16_to_cpu(last->e_value_offs);
350 if (offs < *min_offs)
352 @@ -523,11 +639,162 @@ static size_t ext4_xattr_free_space(stru
353 return (*min_offs - ((void *)last - base) - sizeof(__u32));
357 + * Write the value of the EA in an inode.
360 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
361 + const void *buf, int bufsize)
363 + struct buffer_head *bh = NULL;
364 + struct ext4_map_blocks map;
365 + unsigned long block = 0;
366 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
367 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
368 + int csize, wsize = 0;
373 + while (ret >= 0 && ret < max_blocks) {
377 + map.m_lblk = block;
378 + map.m_len = max_blocks;
379 + ret = ext4_map_blocks(handle, ea_inode, &map,
380 + EXT4_GET_BLOCKS_CREATE);
382 + ext4_mark_inode_dirty(handle, ea_inode);
383 + if (ret == -ENOSPC &&
384 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
396 + while (wsize < bufsize) {
399 + csize = (bufsize - wsize) > blocksize ? blocksize :
401 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
404 + ret = ext4_journal_get_write_access(handle, bh);
408 + memcpy(bh->b_data, buf, csize);
409 + set_buffer_uptodate(bh);
410 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
417 + i_size_write(ea_inode, wsize);
418 + ext4_update_i_disksize(ea_inode, wsize);
420 + ext4_mark_inode_dirty(handle, ea_inode);
429 + * Create an inode to store the value of a large EA.
431 +static struct inode *
432 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
434 + struct inode *ea_inode = NULL;
437 + * Let the next inode be the goal, so we try and allocate the EA inode
438 + * in the same group, or nearby one.
440 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
441 + S_IFREG|0600, NULL, inode->i_ino + 1);
443 + if (!IS_ERR(ea_inode)) {
444 + ea_inode->i_op = &ext4_file_inode_operations;
445 + ea_inode->i_fop = &ext4_file_operations;
446 + ext4_set_aops(ea_inode);
447 + ea_inode->i_generation = inode->i_generation;
448 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
451 + * A back-pointer from EA inode to parent inode will be useful
454 + ea_inode->i_xattr_inode_parent = inode->i_ino;
455 + unlock_new_inode(ea_inode);
462 + * Unlink the inode storing the value of the EA.
465 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
467 + struct inode *ea_inode = NULL;
470 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
474 + ea_inode->i_nlink = 0;
481 + * Add value of the EA in an inode.
484 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
485 + const void *value, size_t value_len)
487 + struct inode *ea_inode = NULL;
490 + /* Create an inode for the EA value */
491 + ea_inode = ext4_xattr_inode_create(handle, inode);
492 + if (IS_ERR(ea_inode))
495 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
497 + ea_inode->i_nlink = 0;
499 + *ea_ino = ea_inode->i_ino;
506 struct ext4_xattr_info {
515 struct ext4_xattr_search {
516 @@ -539,15 +806,23 @@ struct ext4_xattr_search {
520 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
521 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
522 + handle_t *handle, struct inode *inode)
524 struct ext4_xattr_entry *last;
525 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
526 + int in_inode = i->in_inode;
528 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
529 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
530 + (EXT4_XATTR_SIZE(i->value_len) >
531 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
534 /* Compute min_offs and last. */
536 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
537 - if (!last->e_value_block && last->e_value_size) {
538 + if (!last->e_value_inum && last->e_value_size) {
539 size_t offs = le16_to_cpu(last->e_value_offs);
542 @@ -555,16 +830,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
544 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
546 - if (!s->here->e_value_block && s->here->e_value_size) {
548 + !s->here->e_value_inum && s->here->e_value_size) {
549 size_t size = le32_to_cpu(s->here->e_value_size);
550 free += EXT4_XATTR_SIZE(size);
552 free += EXT4_XATTR_LEN(name_len);
555 - if (free < EXT4_XATTR_SIZE(i->value_len) ||
556 - free < EXT4_XATTR_LEN(name_len) +
557 - EXT4_XATTR_SIZE(i->value_len))
558 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
563 + if (free < value_len ||
564 + free < EXT4_XATTR_LEN(name_len) + value_len)
568 @@ -578,7 +858,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
569 s->here->e_name_len = name_len;
570 memcpy(s->here->e_name, i->name, name_len);
572 - if (!s->here->e_value_block && s->here->e_value_size) {
573 + if (!s->here->e_value_inum && s->here->e_value_size &&
574 + s->here->e_value_offs > 0) {
575 void *first_val = s->base + min_offs;
576 size_t offs = le16_to_cpu(s->here->e_value_offs);
577 void *val = s->base + offs;
578 @@ -607,13 +888,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
580 while (!IS_LAST_ENTRY(last)) {
581 size_t o = le16_to_cpu(last->e_value_offs);
582 - if (!last->e_value_block &&
583 + if (!last->e_value_inum &&
584 last->e_value_size && o < offs)
586 cpu_to_le16(o + size);
587 last = EXT4_XATTR_NEXT(last);
590 + if (s->here->e_value_inum) {
591 + ext4_xattr_inode_unlink(inode,
592 + le32_to_cpu(s->here->e_value_inum));
593 + s->here->e_value_inum = 0;
596 /* Remove the old name. */
597 size_t size = EXT4_XATTR_LEN(name_len);
598 @@ -627,10 +912,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
600 /* Insert the new value. */
601 s->here->e_value_size = cpu_to_le32(i->value_len);
602 - if (i->value_len) {
604 + unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
605 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
607 + s->here->e_value_inum = cpu_to_le32(ea_ino);
608 + s->here->e_value_offs = 0;
609 + } else if (i->value_len) {
610 size_t size = EXT4_XATTR_SIZE(i->value_len);
611 void *val = s->base + min_offs - size;
612 s->here->e_value_offs = cpu_to_le16(min_offs - size);
613 + s->here->e_value_inum = 0;
614 memset(val + size - EXT4_XATTR_PAD, 0,
615 EXT4_XATTR_PAD); /* Clear the pad bytes. */
616 memcpy(val, i->value, i->value_len);
617 @@ -675,7 +967,7 @@ ext4_xattr_block_find(struct inode *inod
618 bs->s.end = bs->bh->b_data + bs->bh->b_size;
619 bs->s.here = bs->s.first;
620 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
621 - i->name, bs->bh->b_size, 1);
622 + i->name, bs->bh->b_size, 1, inode);
623 if (error && error != -ENODATA)
625 bs->s.not_found = error;
626 @@ -699,8 +991,6 @@ ext4_xattr_block_set(handle_t *handle, s
628 #define header(x) ((struct ext4_xattr_header *)(x))
630 - if (i->value && i->value_len > sb->s_blocksize)
633 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
635 @@ -715,7 +1005,7 @@ ext4_xattr_block_set(handle_t *handle, s
638 ea_bdebug(bs->bh, "modifying in-place");
639 - error = ext4_xattr_set_entry(i, s);
640 + error = ext4_xattr_set_entry(i, s, handle, inode);
642 if (!IS_LAST_ENTRY(s->first))
643 ext4_xattr_rehash(header(s->base),
644 @@ -767,7 +1057,7 @@ ext4_xattr_block_set(handle_t *handle, s
645 s->end = s->base + sb->s_blocksize;
648 - error = ext4_xattr_set_entry(i, s);
649 + error = ext4_xattr_set_entry(i, s, handle, inode);
653 @@ -918,7 +1208,7 @@ ext4_xattr_ibody_find(struct inode *inod
654 /* Find the named attribute. */
655 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
657 - (void *)is->s.base, 0);
658 + (void *)is->s.base, 0, inode);
659 if (error && error != -ENODATA)
661 is->s.not_found = error;
662 @@ -937,7 +1227,7 @@ ext4_xattr_ibody_set(handle_t *handle, s
664 if (EXT4_I(inode)->i_extra_isize == 0)
666 - error = ext4_xattr_set_entry(i, s);
667 + error = ext4_xattr_set_entry(i, s, handle, inode);
670 header = IHDR(inode, ext4_raw_inode(&is->iloc));
671 @@ -973,7 +1263,7 @@ ext4_xattr_set_handle(handle_t *handle,
674 .value_len = value_len,
678 struct ext4_xattr_ibody_find is = {
679 .s = { .not_found = -ENODATA, },
680 @@ -1042,6 +1332,15 @@ ext4_xattr_set_handle(handle_t *handle,
683 error = ext4_xattr_block_set(handle, inode, &i, &bs);
684 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
685 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
686 + error == -ENOSPC) {
687 + /* xattr not fit to block, store at external
690 + error = ext4_xattr_ibody_set(handle, inode,
695 if (!is.s.not_found) {
696 @@ -1088,10 +1387,25 @@ ext4_xattr_set(struct inode *inode, int
697 const void *value, size_t value_len, int flags)
700 + struct super_block *sb = inode->i_sb;
701 + int buffer_credits;
702 int error, retries = 0;
704 + buffer_credits = EXT4_DATA_TRANS_BLOCKS(sb);
705 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
706 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
707 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
708 + sb->s_blocksize_bits;
710 + /* For new inode */
711 + buffer_credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
713 + /* For data blocks of EA inode */
714 + buffer_credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
718 - handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
719 + handle = ext4_journal_start(inode, buffer_credits);
720 if (IS_ERR(handle)) {
721 error = PTR_ERR(handle);
723 @@ -1101,7 +1415,7 @@ retry:
724 value, value_len, flags);
725 error2 = ext4_journal_stop(handle);
726 if (error == -ENOSPC &&
727 - ext4_should_retry_alloc(inode->i_sb, &retries))
728 + ext4_should_retry_alloc(sb, &retries))
732 @@ -1123,7 +1437,7 @@ static void ext4_xattr_shift_entries(str
734 /* Adjust the value offsets of the entries */
735 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
736 - if (!last->e_value_block && last->e_value_size) {
737 + if (!last->e_value_inum && last->e_value_size) {
738 new_offs = le16_to_cpu(last->e_value_offs) +
740 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
741 @@ -1358,22 +1672,135 @@ cleanup:
745 +#define EIA_INCR 16 /* must be 2^n */
746 +#define EIA_MASK (EIA_INCR - 1)
747 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
748 + * If @lea_ino_array is new or full it will be grown and the old
749 + * contents copied over.
752 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
754 + if (*lea_ino_array == NULL) {
756 + * Start with 15 inodes, so it fits into a powr-of-two size.
757 + * If *lea_ino_array is NULL, this is essentially offsetof()
760 + kmalloc(offsetof(struct ext4_xattr_ino_array,
761 + xia_inodes[EIA_MASK]),
763 + if (*lea_ino_array == NULL)
765 + (*lea_ino_array)->xia_count = 0;
766 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
767 + /* expand the array once all 15 + n * 16 slots are full */
768 + struct ext4_xattr_ino_array *new_array = NULL;
769 + int count = (*lea_ino_array)->xia_count;
771 + /* if new_array is NULL, this is essentially offsetof() */
772 + new_array = kmalloc(
773 + offsetof(struct ext4_xattr_ino_array,
774 + xia_inodes[count + EIA_INCR]),
776 + if (new_array == NULL)
778 + memcpy(new_array, *lea_ino_array,
779 + offsetof(struct ext4_xattr_ino_array,
780 + xia_inodes[count]));
781 + kfree(*lea_ino_array);
782 + *lea_ino_array = new_array;
784 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
789 + * Add xattr inode to orphan list
792 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
793 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
795 + struct inode *ea_inode = NULL;
796 + int idx = 0, error = 0;
798 + if (lea_ino_array == NULL)
801 + for (; idx < lea_ino_array->xia_count; ++idx) {
802 + if (!ext4_handle_has_enough_credits(handle, credits)) {
803 + error = ext4_journal_extend(handle, credits);
805 + error = ext4_journal_restart(handle, credits);
808 + ext4_warning(inode->i_sb,
809 + "couldn't extend journal "
810 + "(err %d)", error);
814 + ea_inode = ext4_xattr_inode_iget(inode,
815 + lea_ino_array->xia_inodes[idx], &error);
818 + ext4_orphan_add(handle, ea_inode);
819 + /* the inode's i_count will be released by caller */
826 * ext4_xattr_delete_inode()
828 - * Free extended attribute resources associated with this inode. This
829 + * Free extended attribute resources associated with this inode. Traverse
830 + * all entries and unlink any xattr inodes associated with this inode. This
831 * is called immediately before an inode is freed. We have exclusive
832 - * access to the inode.
833 + * access to the inode. If an orphan inode is deleted it will also delete any
834 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
835 + * to ensure they belong to the parent inode and were not deleted already.
838 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
840 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
841 + struct ext4_xattr_ino_array **lea_ino_array)
843 struct buffer_head *bh = NULL;
844 + struct ext4_xattr_ibody_header *header;
845 + struct ext4_inode *raw_inode;
846 + struct ext4_iloc iloc;
847 + struct ext4_xattr_entry *entry;
848 + int credits = 3, error = 0;
850 - if (!EXT4_I(inode)->i_file_acl)
851 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
852 + goto delete_external_ea;
854 + error = ext4_get_inode_loc(inode, &iloc);
857 + raw_inode = ext4_raw_inode(&iloc);
858 + header = IHDR(inode, raw_inode);
859 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
860 + entry = EXT4_XATTR_NEXT(entry)) {
861 + if (!entry->e_value_inum)
863 + if (ext4_expand_ino_array(lea_ino_array,
864 + entry->e_value_inum) != 0) {
868 + entry->e_value_inum = 0;
873 + if (!EXT4_I(inode)->i_file_acl) {
874 + /* add xattr inode to orphan list */
875 + ext4_xattr_inode_orphan_add(handle, inode, credits,
879 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
881 EXT4_ERROR_INODE(inode, "block %llu read error",
882 @@ -1386,11 +1813,69 @@ ext4_xattr_delete_inode(handle_t *handle
883 EXT4_I(inode)->i_file_acl);
887 + for (entry = BFIRST(bh); !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)
894 + entry->e_value_inum = 0;
897 + /* adding xattr inode to orphan list */
898 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
903 + if (!IS_NOQUOTA(inode))
904 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
906 + if (!ext4_handle_has_enough_credits(handle, credits)) {
907 + error = ext4_journal_extend(handle, credits);
909 + error = ext4_journal_restart(handle, credits);
911 + ext4_warning(inode->i_sb,
912 + "couldn't extend journal (err %d)", error);
917 ext4_xattr_release_block(handle, inode, bh);
918 EXT4_I(inode)->i_file_acl = 0;
927 +ext4_xattr_inode_array_free(struct inode *inode,
928 + struct ext4_xattr_ino_array *lea_ino_array)
930 + struct inode *ea_inode = NULL;
934 + if (lea_ino_array == NULL)
937 + for (; idx < lea_ino_array->xia_count; ++idx) {
938 + ea_inode = ext4_xattr_inode_iget(inode,
939 + lea_ino_array->xia_inodes[idx], &err);
942 + /* for inode's i_count get from ext4_xattr_delete_inode */
943 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
945 + ea_inode->i_nlink = 0;
948 + kfree(lea_ino_array);
952 @@ -1460,10 +1945,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
953 entry1->e_name_index != entry2->e_name_index ||
954 entry1->e_name_len != entry2->e_name_len ||
955 entry1->e_value_size != entry2->e_value_size ||
956 + entry1->e_value_inum != entry2->e_value_inum ||
957 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
959 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
961 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
962 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
963 le32_to_cpu(entry1->e_value_size)))
964 @@ -1547,7 +2031,7 @@ static inline void ext4_xattr_hash_entry
968 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
969 + if (!entry->e_value_inum && entry->e_value_size) {
970 __le32 *value = (__le32 *)((char *)header +
971 le16_to_cpu(entry->e_value_offs));
972 for (n = (le32_to_cpu(entry->e_value_size) +
973 Index: linux-stage/fs/ext4/xattr.h
974 ===================================================================
975 --- linux-stage.orig/fs/ext4/xattr.h
976 +++ linux-stage/fs/ext4/xattr.h
977 @@ -38,7 +38,7 @@ struct ext4_xattr_entry {
978 __u8 e_name_len; /* length of name */
979 __u8 e_name_index; /* attribute name index */
980 __le16 e_value_offs; /* offset in disk block of value */
981 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
982 + __le32 e_value_inum; /* inode in which the value is stored */
983 __le32 e_value_size; /* size of attribute value */
984 __le32 e_hash; /* hash value of name and value */
985 char e_name[0]; /* attribute name */
986 @@ -63,6 +63,15 @@ struct ext4_xattr_entry {
987 EXT4_I(inode)->i_extra_isize))
988 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
990 +#define i_xattr_inode_parent i_mtime.tv_sec
993 + * The minimum size of EA value when you start storing it in an external inode
994 + * size of block - size of header - size of 1 entry - 4 null bytes
996 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
997 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
999 # ifdef CONFIG_EXT4_FS_XATTR
1001 extern const struct xattr_handler ext4_xattr_user_handler;
1002 @@ -77,7 +86,13 @@ extern int ext4_xattr_get(struct inode *
1003 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1004 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1006 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1007 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1009 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1010 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1011 + struct ext4_xattr_ino_array **array);
1012 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1013 + struct ext4_xattr_ino_array *array);
1014 extern void ext4_xattr_put_super(struct super_block *);
1016 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1017 @@ -111,9 +126,11 @@ ext4_xattr_set_handle(handle_t *handle,
1022 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1024 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1025 + struct ext4_xattr_ino_array *array)
1027 + return -EOPNOTSUPP;
1031 Index: linux-stage/fs/ext4/ialloc.c
1032 ===================================================================
1033 --- linux-stage.orig/fs/ext4/ialloc.c
1034 +++ linux-stage/fs/ext4/ialloc.c
1035 @@ -221,7 +221,6 @@ void ext4_free_inode(handle_t *handle, s
1036 * as writing the quota to disk may need the lock as well.
1038 dquot_initialize(inode);
1039 - ext4_xattr_delete_inode(handle, inode);
1040 dquot_free_inode(inode);