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 - le16_to_cpu(entry->e_value_offs) + value_size > size)
162 + if ((entry->e_value_inum == 0) &&
163 + (le16_to_cpu(entry->e_value_offs) + value_size > size))
165 + if (entry->e_value_inum != 0 &&
166 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
167 + le32_to_cpu(entry->e_value_inum) >
168 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
174 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
175 - const char *name, size_t size, int sorted)
176 + const char *name, size_t size, int sorted,
177 + struct inode *inode)
179 struct ext4_xattr_entry *entry;
181 @@ -200,11 +207,104 @@ ext4_xattr_find_entry(struct ext4_xattr_
185 - if (!cmp && ext4_xattr_check_entry(entry, size))
186 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
188 return cmp ? -ENODATA : 0;
192 + * Read the EA value from an inode.
195 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
197 + unsigned long block = 0;
198 + struct buffer_head *bh = NULL;
199 + int err, blocksize;
200 + size_t csize, ret_size = 0;
205 + blocksize = ea_inode->i_sb->s_blocksize;
207 + while (ret_size < *size) {
208 + csize = (*size - ret_size) > blocksize ? blocksize :
210 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
215 + memcpy(buf, bh->b_data, csize);
228 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
230 + struct inode *ea_inode = NULL;
232 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
233 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
234 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
235 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
236 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
237 + *err = rc != 0 ? rc : -EIO;
241 + if (ea_inode->i_xattr_inode_parent != parent->i_ino ||
242 + ea_inode->i_generation != parent->i_generation) {
243 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
244 + "to parent invalid.", ea_ino);
249 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
250 + ext4_error(parent->i_sb, "EA inode %lu does not have "
251 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
265 + * Read the value from the EA inode.
268 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
271 + struct inode *ea_inode = NULL;
274 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
278 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
285 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
286 void *buffer, size_t buffer_size)
287 @@ -235,7 +334,8 @@ bad_block:
289 ext4_xattr_cache_insert(bh);
291 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
292 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
297 @@ -245,8 +345,16 @@ bad_block:
299 if (size > buffer_size)
301 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
303 + if (entry->e_value_inum != 0) {
304 + error = ext4_xattr_inode_get(inode,
305 + le32_to_cpu(entry->e_value_inum),
310 + memcpy(buffer, bh->b_data +
311 + le16_to_cpu(entry->e_value_offs), size);
316 @@ -280,7 +388,7 @@ ext4_xattr_ibody_get(struct inode *inode
319 error = ext4_xattr_find_entry(&entry, name_index, name,
320 - end - (void *)entry, 0);
321 + end - (void *)entry, 0, inode);
324 size = le32_to_cpu(entry->e_value_size);
325 @@ -288,8 +396,16 @@ ext4_xattr_ibody_get(struct inode *inode
327 if (size > buffer_size)
329 - memcpy(buffer, (void *)IFIRST(header) +
330 - le16_to_cpu(entry->e_value_offs), size);
331 + if (entry->e_value_inum != 0) {
332 + error = ext4_xattr_inode_get(inode,
333 + le32_to_cpu(entry->e_value_inum),
338 + memcpy(buffer, (void *)IFIRST(header) +
339 + le16_to_cpu(entry->e_value_offs), size);
344 @@ -514,7 +630,7 @@ static size_t ext4_xattr_free_space(stru
346 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
347 *total += EXT4_XATTR_LEN(last->e_name_len);
348 - if (!last->e_value_block && last->e_value_size) {
349 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
350 size_t offs = le16_to_cpu(last->e_value_offs);
351 if (offs < *min_offs)
353 @@ -523,11 +639,162 @@ static size_t ext4_xattr_free_space(stru
354 return (*min_offs - ((void *)last - base) - sizeof(__u32));
358 + * Write the value of the EA in an inode.
361 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
362 + const void *buf, int bufsize)
364 + struct buffer_head *bh = NULL;
365 + struct ext4_map_blocks map;
366 + unsigned long block = 0;
367 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
368 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
369 + int csize, wsize = 0;
374 + while (ret >= 0 && ret < max_blocks) {
378 + map.m_lblk = block;
379 + map.m_len = max_blocks;
380 + ret = ext4_map_blocks(handle, ea_inode, &map,
381 + EXT4_GET_BLOCKS_CREATE);
383 + ext4_mark_inode_dirty(handle, ea_inode);
384 + if (ret == -ENOSPC &&
385 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
397 + while (wsize < bufsize) {
400 + csize = (bufsize - wsize) > blocksize ? blocksize :
402 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
405 + ret = ext4_journal_get_write_access(handle, bh);
409 + memcpy(bh->b_data, buf, csize);
410 + set_buffer_uptodate(bh);
411 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
418 + i_size_write(ea_inode, wsize);
419 + ext4_update_i_disksize(ea_inode, wsize);
421 + ext4_mark_inode_dirty(handle, ea_inode);
430 + * Create an inode to store the value of a large EA.
432 +static struct inode *
433 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
435 + struct inode *ea_inode = NULL;
438 + * Let the next inode be the goal, so we try and allocate the EA inode
439 + * in the same group, or nearby one.
441 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
442 + S_IFREG|0600, NULL, inode->i_ino + 1);
444 + if (!IS_ERR(ea_inode)) {
445 + ea_inode->i_op = &ext4_file_inode_operations;
446 + ea_inode->i_fop = &ext4_file_operations;
447 + ext4_set_aops(ea_inode);
448 + ea_inode->i_generation = inode->i_generation;
449 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
452 + * A back-pointer from EA inode to parent inode will be useful
455 + ea_inode->i_xattr_inode_parent = inode->i_ino;
456 + unlock_new_inode(ea_inode);
463 + * Unlink the inode storing the value of the EA.
466 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
468 + struct inode *ea_inode = NULL;
471 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
475 + ea_inode->i_nlink = 0;
482 + * Add value of the EA in an inode.
485 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
486 + const void *value, size_t value_len)
488 + struct inode *ea_inode = NULL;
491 + /* Create an inode for the EA value */
492 + ea_inode = ext4_xattr_inode_create(handle, inode);
493 + if (IS_ERR(ea_inode))
496 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
498 + ea_inode->i_nlink = 0;
500 + *ea_ino = ea_inode->i_ino;
507 struct ext4_xattr_info {
516 struct ext4_xattr_search {
517 @@ -539,15 +806,23 @@ struct ext4_xattr_search {
521 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
522 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
523 + handle_t *handle, struct inode *inode)
525 struct ext4_xattr_entry *last;
526 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
527 + int in_inode = i->in_inode;
529 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
530 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
531 + (EXT4_XATTR_SIZE(i->value_len) >
532 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
535 /* Compute min_offs and last. */
537 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
538 - if (!last->e_value_block && last->e_value_size) {
539 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
540 size_t offs = le16_to_cpu(last->e_value_offs);
543 @@ -555,16 +830,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
545 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
547 - if (!s->here->e_value_block && s->here->e_value_size) {
548 + if (!in_inode && s->here->e_value_inum == 0 &&
549 + s->here->e_value_size > 0) {
550 size_t size = le32_to_cpu(s->here->e_value_size);
551 free += EXT4_XATTR_SIZE(size);
553 free += EXT4_XATTR_LEN(name_len);
556 - if (free < EXT4_XATTR_SIZE(i->value_len) ||
557 - free < EXT4_XATTR_LEN(name_len) +
558 - EXT4_XATTR_SIZE(i->value_len))
559 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
564 + if (free < value_len ||
565 + free < EXT4_XATTR_LEN(name_len) + value_len)
569 @@ -578,7 +858,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
570 s->here->e_name_len = name_len;
571 memcpy(s->here->e_name, i->name, name_len);
573 - if (!s->here->e_value_block && s->here->e_value_size) {
574 + if (s->here->e_value_offs > 0 && s->here->e_value_inum == 0 &&
575 + s->here->e_value_size > 0) {
576 void *first_val = s->base + min_offs;
577 size_t offs = le16_to_cpu(s->here->e_value_offs);
578 void *val = s->base + offs;
579 @@ -607,13 +888,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
581 while (!IS_LAST_ENTRY(last)) {
582 size_t o = le16_to_cpu(last->e_value_offs);
583 - if (!last->e_value_block &&
584 + if (!last->e_value_inum &&
585 last->e_value_size && o < offs)
587 cpu_to_le16(o + size);
588 last = EXT4_XATTR_NEXT(last);
591 + if (s->here->e_value_inum != 0) {
592 + ext4_xattr_inode_unlink(inode,
593 + le32_to_cpu(s->here->e_value_inum));
594 + s->here->e_value_inum = 0;
597 /* Remove the old name. */
598 size_t size = EXT4_XATTR_LEN(name_len);
599 @@ -627,10 +912,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
601 /* Insert the new value. */
602 s->here->e_value_size = cpu_to_le32(i->value_len);
603 - if (i->value_len) {
605 + unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
606 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
608 + s->here->e_value_inum = cpu_to_le32(ea_ino);
609 + s->here->e_value_offs = 0;
610 + } else if (i->value_len) {
611 size_t size = EXT4_XATTR_SIZE(i->value_len);
612 void *val = s->base + min_offs - size;
613 s->here->e_value_offs = cpu_to_le16(min_offs - size);
614 + s->here->e_value_inum = 0;
615 memset(val + size - EXT4_XATTR_PAD, 0,
616 EXT4_XATTR_PAD); /* Clear the pad bytes. */
617 memcpy(val, i->value, i->value_len);
618 @@ -675,7 +967,7 @@ ext4_xattr_block_find(struct inode *inod
619 bs->s.end = bs->bh->b_data + bs->bh->b_size;
620 bs->s.here = bs->s.first;
621 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
622 - i->name, bs->bh->b_size, 1);
623 + i->name, bs->bh->b_size, 1, inode);
624 if (error && error != -ENODATA)
626 bs->s.not_found = error;
627 @@ -699,8 +991,6 @@ ext4_xattr_block_set(handle_t *handle, s
629 #define header(x) ((struct ext4_xattr_header *)(x))
631 - if (i->value && i->value_len > sb->s_blocksize)
634 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
636 @@ -715,7 +1005,7 @@ ext4_xattr_block_set(handle_t *handle, s
639 ea_bdebug(bs->bh, "modifying in-place");
640 - error = ext4_xattr_set_entry(i, s);
641 + error = ext4_xattr_set_entry(i, s, handle, inode);
643 if (!IS_LAST_ENTRY(s->first))
644 ext4_xattr_rehash(header(s->base),
645 @@ -767,7 +1057,7 @@ ext4_xattr_block_set(handle_t *handle, s
646 s->end = s->base + sb->s_blocksize;
649 - error = ext4_xattr_set_entry(i, s);
650 + error = ext4_xattr_set_entry(i, s, handle, inode);
654 @@ -918,7 +1208,7 @@ ext4_xattr_ibody_find(struct inode *inod
655 /* Find the named attribute. */
656 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
658 - (void *)is->s.base, 0);
659 + (void *)is->s.base, 0, inode);
660 if (error && error != -ENODATA)
662 is->s.not_found = error;
663 @@ -937,7 +1227,7 @@ ext4_xattr_ibody_set(handle_t *handle, s
665 if (EXT4_I(inode)->i_extra_isize == 0)
667 - error = ext4_xattr_set_entry(i, s);
668 + error = ext4_xattr_set_entry(i, s, handle, inode);
671 header = IHDR(inode, ext4_raw_inode(&is->iloc));
672 @@ -973,7 +1263,7 @@ ext4_xattr_set_handle(handle_t *handle,
675 .value_len = value_len,
679 struct ext4_xattr_ibody_find is = {
680 .s = { .not_found = -ENODATA, },
681 @@ -1042,6 +1332,15 @@ ext4_xattr_set_handle(handle_t *handle,
684 error = ext4_xattr_block_set(handle, inode, &i, &bs);
685 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
686 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
687 + error == -ENOSPC) {
688 + /* xattr not fit to block, store at external
691 + error = ext4_xattr_ibody_set(handle, inode,
696 if (!is.s.not_found) {
697 @@ -1088,10 +1387,25 @@ ext4_xattr_set(struct inode *inode, int
698 const void *value, size_t value_len, int flags)
701 + struct super_block *sb = inode->i_sb;
702 + int buffer_credits;
703 int error, retries = 0;
705 + buffer_credits = EXT4_DATA_TRANS_BLOCKS(sb);
706 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
707 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
708 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
709 + sb->s_blocksize_bits;
711 + /* For new inode */
712 + buffer_credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
714 + /* For data blocks of EA inode */
715 + buffer_credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
719 - handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
720 + handle = ext4_journal_start(inode, buffer_credits);
721 if (IS_ERR(handle)) {
722 error = PTR_ERR(handle);
724 @@ -1101,7 +1415,7 @@ retry:
725 value, value_len, flags);
726 error2 = ext4_journal_stop(handle);
727 if (error == -ENOSPC &&
728 - ext4_should_retry_alloc(inode->i_sb, &retries))
729 + ext4_should_retry_alloc(sb, &retries))
733 @@ -1123,7 +1437,7 @@ static void ext4_xattr_shift_entries(str
735 /* Adjust the value offsets of the entries */
736 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
737 - if (!last->e_value_block && last->e_value_size) {
738 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
739 new_offs = le16_to_cpu(last->e_value_offs) +
741 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
742 @@ -1358,22 +1672,135 @@ cleanup:
746 +#define EIA_INCR 16 /* must be 2^n */
747 +#define EIA_MASK (EIA_INCR - 1)
748 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
749 + * If @lea_ino_array is new or full it will be grown and the old
750 + * contents copied over.
753 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
755 + if (*lea_ino_array == NULL) {
757 + * Start with 15 inodes, so it fits into a powr-of-two size.
758 + * If *lea_ino_array is NULL, this is essentially offsetof()
761 + kmalloc(offsetof(struct ext4_xattr_ino_array,
762 + xia_inodes[EIA_MASK]),
764 + if (*lea_ino_array == NULL)
766 + (*lea_ino_array)->xia_count = 0;
767 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
768 + /* expand the array once all 15 + n * 16 slots are full */
769 + struct ext4_xattr_ino_array *new_array = NULL;
770 + int count = (*lea_ino_array)->xia_count;
772 + /* if new_array is NULL, this is essentially offsetof() */
773 + new_array = kmalloc(
774 + offsetof(struct ext4_xattr_ino_array,
775 + xia_inodes[count + EIA_INCR]),
777 + if (new_array == NULL)
779 + memcpy(new_array, *lea_ino_array,
780 + offsetof(struct ext4_xattr_ino_array,
781 + xia_inodes[count]));
782 + kfree(*lea_ino_array);
783 + *lea_ino_array = new_array;
785 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
790 + * Add xattr inode to orphan list
793 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
794 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
796 + struct inode *ea_inode = NULL;
797 + int idx = 0, error = 0;
799 + if (lea_ino_array == NULL)
802 + for (; idx < lea_ino_array->xia_count; ++idx) {
803 + if (!ext4_handle_has_enough_credits(handle, credits)) {
804 + error = ext4_journal_extend(handle, credits);
806 + error = ext4_journal_restart(handle, credits);
809 + ext4_warning(inode->i_sb,
810 + "couldn't extend journal "
811 + "(err %d)", error);
815 + ea_inode = ext4_xattr_inode_iget(inode,
816 + lea_ino_array->xia_inodes[idx], &error);
819 + ext4_orphan_add(handle, ea_inode);
820 + /* the inode's i_count will be released by caller */
827 * ext4_xattr_delete_inode()
829 - * Free extended attribute resources associated with this inode. This
830 + * Free extended attribute resources associated with this inode. Traverse
831 + * all entries and unlink any xattr inodes associated with this inode. This
832 * is called immediately before an inode is freed. We have exclusive
833 - * access to the inode.
834 + * access to the inode. If an orphan inode is deleted it will also delete any
835 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
836 + * to ensure they belong to the parent inode and were not deleted already.
839 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
841 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
842 + struct ext4_xattr_ino_array **lea_ino_array)
844 struct buffer_head *bh = NULL;
845 + struct ext4_xattr_ibody_header *header;
846 + struct ext4_inode *raw_inode;
847 + struct ext4_iloc iloc;
848 + struct ext4_xattr_entry *entry;
849 + int credits = 3, error = 0;
851 - if (!EXT4_I(inode)->i_file_acl)
852 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
853 + goto delete_external_ea;
855 + error = ext4_get_inode_loc(inode, &iloc);
858 + raw_inode = ext4_raw_inode(&iloc);
859 + header = IHDR(inode, raw_inode);
860 + entry = IFIRST(header);
861 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
862 + if (entry->e_value_inum == 0)
864 + if (ext4_expand_ino_array(lea_ino_array,
865 + entry->e_value_inum) != 0) {
869 + entry->e_value_inum = 0;
874 + if (!EXT4_I(inode)->i_file_acl) {
875 + /* add xattr inode to orphan list */
876 + ext4_xattr_inode_orphan_add(handle, inode, credits,
880 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
882 EXT4_ERROR_INODE(inode, "block %llu read error",
883 @@ -1386,11 +1813,69 @@ ext4_xattr_delete_inode(handle_t *handle
884 EXT4_I(inode)->i_file_acl);
888 + entry = BFIRST(bh);
889 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
890 + if (entry->e_value_inum == 0)
892 + if (ext4_expand_ino_array(lea_ino_array,
893 + entry->e_value_inum) != 0)
895 + entry->e_value_inum = 0;
898 + /* adding xattr inode to orphan list */
899 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
904 + if (!IS_NOQUOTA(inode))
905 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
907 + if (!ext4_handle_has_enough_credits(handle, credits)) {
908 + error = ext4_journal_extend(handle, credits);
910 + error = ext4_journal_restart(handle, credits);
912 + ext4_warning(inode->i_sb,
913 + "couldn't extend journal (err %d)", error);
918 ext4_xattr_release_block(handle, inode, bh);
919 EXT4_I(inode)->i_file_acl = 0;
928 +ext4_xattr_inode_array_free(struct inode *inode,
929 + struct ext4_xattr_ino_array *lea_ino_array)
931 + struct inode *ea_inode = NULL;
935 + if (lea_ino_array == NULL)
938 + for (; idx < lea_ino_array->xia_count; ++idx) {
939 + ea_inode = ext4_xattr_inode_iget(inode,
940 + lea_ino_array->xia_inodes[idx], &err);
943 + /* for inode's i_count get from ext4_xattr_delete_inode */
944 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
946 + ea_inode->i_nlink = 0;
949 + kfree(lea_ino_array);
953 @@ -1460,10 +1945,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
954 entry1->e_name_index != entry2->e_name_index ||
955 entry1->e_name_len != entry2->e_name_len ||
956 entry1->e_value_size != entry2->e_value_size ||
957 + entry1->e_value_inum != entry2->e_value_inum ||
958 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
960 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
962 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
963 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
964 le32_to_cpu(entry1->e_value_size)))
965 @@ -1547,7 +2031,7 @@ static inline void ext4_xattr_hash_entry
969 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
970 + if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
971 __le32 *value = (__le32 *)((char *)header +
972 le16_to_cpu(entry->e_value_offs));
973 for (n = (le32_to_cpu(entry->e_value_size) +
974 Index: linux-stage/fs/ext4/xattr.h
975 ===================================================================
976 --- linux-stage.orig/fs/ext4/xattr.h
977 +++ linux-stage/fs/ext4/xattr.h
978 @@ -38,7 +38,7 @@ struct ext4_xattr_entry {
979 __u8 e_name_len; /* length of name */
980 __u8 e_name_index; /* attribute name index */
981 __le16 e_value_offs; /* offset in disk block of value */
982 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
983 + __le32 e_value_inum; /* inode in which the value is stored */
984 __le32 e_value_size; /* size of attribute value */
985 __le32 e_hash; /* hash value of name and value */
986 char e_name[0]; /* attribute name */
987 @@ -63,6 +63,15 @@ struct ext4_xattr_entry {
988 EXT4_I(inode)->i_extra_isize))
989 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
991 +#define i_xattr_inode_parent i_mtime.tv_sec
994 + * The minimum size of EA value when you start storing it in an external inode
995 + * size of block - size of header - size of 1 entry - 4 null bytes
997 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
998 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1000 # ifdef CONFIG_EXT4_FS_XATTR
1002 extern const struct xattr_handler ext4_xattr_user_handler;
1003 @@ -77,7 +86,13 @@ extern int ext4_xattr_get(struct inode *
1004 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1005 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1007 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1008 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1010 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1011 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1012 + struct ext4_xattr_ino_array **array);
1013 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1014 + struct ext4_xattr_ino_array *array);
1015 extern void ext4_xattr_put_super(struct super_block *);
1017 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1018 @@ -111,9 +126,11 @@ ext4_xattr_set_handle(handle_t *handle,
1023 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1025 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1026 + struct ext4_xattr_ino_array *array)
1028 + return -EOPNOTSUPP;
1032 Index: linux-stage/fs/ext4/ialloc.c
1033 ===================================================================
1034 --- linux-stage.orig/fs/ext4/ialloc.c
1035 +++ linux-stage/fs/ext4/ialloc.c
1036 @@ -221,7 +221,6 @@ void ext4_free_inode(handle_t *handle, s
1037 * as writing the quota to disk may need the lock as well.
1039 dquot_initialize(inode);
1040 - ext4_xattr_delete_inode(handle, inode);
1041 dquot_free_inode(inode);