1 Index: linux-stage/fs/ext4/ext4.h
2 ===================================================================
3 --- linux-stage.orig/fs/ext4/ext4.h
4 +++ linux-stage/fs/ext4/ext4.h
5 @@ -1329,6 +1329,7 @@ EXT4_INODE_BIT_FNS(state, state_flags)
6 #define EXT4_FEATURE_INCOMPAT_64BIT 0x0080
7 #define EXT4_FEATURE_INCOMPAT_MMP 0x0100
8 #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
9 +#define EXT4_FEATURE_INCOMPAT_EA_INODE 0x0400
10 #define EXT4_FEATURE_INCOMPAT_DIRDATA 0x1000
12 #define EXT4_FEATURE_COMPAT_SUPP EXT2_FEATURE_COMPAT_EXT_ATTR
13 @@ -1338,6 +1339,7 @@ EXT4_INODE_BIT_FNS(state, state_flags)
14 EXT4_FEATURE_INCOMPAT_EXTENTS| \
15 EXT4_FEATURE_INCOMPAT_64BIT| \
16 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
17 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
18 EXT4_FEATURE_INCOMPAT_MMP| \
19 EXT4_FEATURE_INCOMPAT_DIRDATA)
21 @@ -1695,6 +1697,12 @@ struct mmpd_data {
22 #define EXT4_MMP_MAX_CHECK_INTERVAL 300UL
25 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
26 + * This limit is arbitrary, but is reasonable for the xattr API.
28 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE (1024 * 1024)
34 @@ -1706,6 +1714,10 @@ struct mmpd_data {
35 # define ATTRIB_NORET __attribute__((noreturn))
36 # define NORET_AND noreturn,
38 +struct ext4_xattr_ino_array {
39 + unsigned int xia_count; /* # of used item in the array */
40 + unsigned int xia_inodes[0];
43 extern unsigned int ext4_count_free(struct buffer_head *, unsigned);
45 Index: linux-stage/fs/ext4/xattr.c
46 ===================================================================
47 --- linux-stage.orig/fs/ext4/xattr.c
48 +++ linux-stage/fs/ext4/xattr.c
49 @@ -168,19 +168,26 @@ ext4_xattr_check_block(struct buffer_hea
53 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
54 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
55 + struct inode *inode)
57 size_t value_size = le32_to_cpu(entry->e_value_size);
59 - if (entry->e_value_block != 0 || value_size > size ||
60 - le16_to_cpu(entry->e_value_offs) + value_size > size)
61 + if ((entry->e_value_inum == 0) &&
62 + (le16_to_cpu(entry->e_value_offs) + value_size > size))
64 + if (entry->e_value_inum != 0 &&
65 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
66 + le32_to_cpu(entry->e_value_inum) >
67 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
73 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
74 - const char *name, size_t size, int sorted)
75 + const char *name, size_t size, int sorted,
76 + struct inode *inode)
78 struct ext4_xattr_entry *entry;
80 @@ -200,11 +207,103 @@ ext4_xattr_find_entry(struct ext4_xattr_
84 - if (!cmp && ext4_xattr_check_entry(entry, size))
85 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
87 return cmp ? -ENODATA : 0;
91 + * Read the EA value from an inode.
94 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
96 + unsigned long block = 0;
97 + struct buffer_head *bh = NULL;
99 + size_t csize, ret_size = 0;
104 + blocksize = ea_inode->i_sb->s_blocksize;
106 + while (ret_size < *size) {
107 + csize = (*size - ret_size) > blocksize ? blocksize :
109 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
114 + memcpy(buf, bh->b_data, csize);
127 +struct inode *ext4_xattr_inode_iget(struct inode *parent, int ea_ino, int *err)
129 + struct inode *ea_inode = NULL;
131 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
132 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
133 + ext4_error(parent->i_sb, "error while reading EA inode %d",
139 + if (ea_inode->i_xattr_inode_parent != parent->i_ino ||
140 + ea_inode->i_generation != parent->i_generation) {
141 + ext4_error(parent->i_sb, "Backpointer from EA inode %d "
142 + "to parent invalid.", ea_ino);
147 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
148 + ext4_error(parent->i_sb, "EA inode %d does not have "
149 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
163 + * Read the value from the EA inode.
166 +ext4_xattr_inode_get(struct inode *inode, int ea_ino, void *buffer,
169 + struct inode *ea_inode = NULL;
172 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
176 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
183 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
184 void *buffer, size_t buffer_size)
185 @@ -236,7 +335,8 @@ bad_block:
187 ext4_xattr_cache_insert(bh);
189 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
190 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
195 @@ -246,8 +346,16 @@ bad_block:
197 if (size > buffer_size)
199 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
201 + if (entry->e_value_inum != 0) {
202 + error = ext4_xattr_inode_get(inode,
203 + le32_to_cpu(entry->e_value_inum),
208 + memcpy(buffer, bh->b_data +
209 + le16_to_cpu(entry->e_value_offs), size);
214 @@ -281,7 +389,7 @@ ext4_xattr_ibody_get(struct inode *inode
217 error = ext4_xattr_find_entry(&entry, name_index, name,
218 - end - (void *)entry, 0);
219 + end - (void *)entry, 0, inode);
222 size = le32_to_cpu(entry->e_value_size);
223 @@ -289,8 +397,16 @@ ext4_xattr_ibody_get(struct inode *inode
225 if (size > buffer_size)
227 - memcpy(buffer, (void *)IFIRST(header) +
228 - le16_to_cpu(entry->e_value_offs), size);
229 + if (entry->e_value_inum != 0) {
230 + error = ext4_xattr_inode_get(inode,
231 + le32_to_cpu(entry->e_value_inum),
236 + memcpy(buffer, (void *)IFIRST(header) +
237 + le16_to_cpu(entry->e_value_offs), size);
242 @@ -513,7 +629,7 @@ static size_t ext4_xattr_free_space(stru
244 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
245 *total += EXT4_XATTR_LEN(last->e_name_len);
246 - if (!last->e_value_block && last->e_value_size) {
247 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
248 size_t offs = le16_to_cpu(last->e_value_offs);
249 if (offs < *min_offs)
251 @@ -522,11 +638,159 @@ static size_t ext4_xattr_free_space(stru
252 return (*min_offs - ((void *)last - base) - sizeof(__u32));
256 + * Write the value of the EA in an inode.
259 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
260 + const void *buf, int bufsize)
262 + struct buffer_head *bh = NULL, dummy;
263 + unsigned long block = 0;
264 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
265 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
266 + int csize, wsize = 0;
271 + while (ret >= 0 && ret < max_blocks) {
275 + ret = ext4_get_blocks(handle, ea_inode, block, max_blocks,
276 + &dummy, EXT4_GET_BLOCKS_CREATE);
278 + ext4_mark_inode_dirty(handle, ea_inode);
279 + if (ret == -ENOSPC &&
280 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
292 + while (wsize < bufsize) {
295 + csize = (bufsize - wsize) > blocksize ? blocksize :
297 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
300 + ret = ext4_journal_get_write_access(handle, bh);
304 + memcpy(bh->b_data, buf, csize);
305 + set_buffer_uptodate(bh);
306 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
313 + i_size_write(ea_inode, wsize);
314 + ext4_update_i_disksize(ea_inode, wsize);
316 + ext4_mark_inode_dirty(handle, ea_inode);
325 + * Create an inode to store the value of a large EA.
327 +static struct inode *
328 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
330 + struct inode *ea_inode = NULL;
333 + * Let the next inode be the goal, so we try and allocate the EA inode
334 + * in the same group, or nearby one.
336 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
337 + S_IFREG|0600, NULL, inode->i_ino + 1);
339 + if (!IS_ERR(ea_inode)) {
340 + ea_inode->i_op = &ext4_file_inode_operations;
341 + ea_inode->i_fop = &ext4_file_operations;
342 + ext4_set_aops(ea_inode);
343 + ea_inode->i_generation = inode->i_generation;
344 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
347 + * A back-pointer from EA inode to parent inode will be useful
350 + ea_inode->i_xattr_inode_parent = inode->i_ino;
351 + unlock_new_inode(ea_inode);
358 + * Unlink the inode storing the value of the EA.
361 +ext4_xattr_inode_unlink(struct inode *inode, int ea_ino)
363 + struct inode *ea_inode = NULL;
366 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
370 + ea_inode->i_nlink = 0;
377 + * Add value of the EA in an inode.
380 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, int *ea_ino,
381 + const void *value, size_t value_len)
383 + struct inode *ea_inode = NULL;
386 + /* Create an inode for the EA value */
387 + ea_inode = ext4_xattr_inode_create(handle, inode);
388 + if (IS_ERR(ea_inode))
391 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
393 + ea_inode->i_nlink = 0;
395 + *ea_ino = ea_inode->i_ino;
402 struct ext4_xattr_info {
411 struct ext4_xattr_search {
412 @@ -538,15 +802,23 @@ struct ext4_xattr_search {
416 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
417 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
418 + handle_t *handle, struct inode *inode)
420 struct ext4_xattr_entry *last;
421 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
422 + int in_inode = i->in_inode;
424 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
425 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
426 + (EXT4_XATTR_SIZE(i->value_len) >
427 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
430 /* Compute min_offs and last. */
432 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
433 - if (!last->e_value_block && last->e_value_size) {
434 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
435 size_t offs = le16_to_cpu(last->e_value_offs);
438 @@ -554,16 +826,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
440 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
442 - if (!s->here->e_value_block && s->here->e_value_size) {
443 + if (!in_inode && s->here->e_value_inum == 0 &&
444 + s->here->e_value_size > 0) {
445 size_t size = le32_to_cpu(s->here->e_value_size);
446 free += EXT4_XATTR_SIZE(size);
448 free += EXT4_XATTR_LEN(name_len);
451 - if (free < EXT4_XATTR_SIZE(i->value_len) ||
452 - free < EXT4_XATTR_LEN(name_len) +
453 - EXT4_XATTR_SIZE(i->value_len))
454 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
459 + if (free < value_len ||
460 + free < EXT4_XATTR_LEN(name_len) + value_len)
464 @@ -577,7 +854,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
465 s->here->e_name_len = name_len;
466 memcpy(s->here->e_name, i->name, name_len);
468 - if (!s->here->e_value_block && s->here->e_value_size) {
469 + if (s->here->e_value_offs > 0 && s->here->e_value_inum == 0 &&
470 + s->here->e_value_size > 0) {
471 void *first_val = s->base + min_offs;
472 size_t offs = le16_to_cpu(s->here->e_value_offs);
473 void *val = s->base + offs;
474 @@ -606,13 +884,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
476 while (!IS_LAST_ENTRY(last)) {
477 size_t o = le16_to_cpu(last->e_value_offs);
478 - if (!last->e_value_block &&
479 + if (!last->e_value_inum &&
480 last->e_value_size && o < offs)
482 cpu_to_le16(o + size);
483 last = EXT4_XATTR_NEXT(last);
486 + if (s->here->e_value_inum != 0) {
487 + ext4_xattr_inode_unlink(inode,
488 + le32_to_cpu(s->here->e_value_inum));
489 + s->here->e_value_inum = 0;
492 /* Remove the old name. */
493 size_t size = EXT4_XATTR_LEN(name_len);
494 @@ -626,10 +908,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
496 /* Insert the new value. */
497 s->here->e_value_size = cpu_to_le32(i->value_len);
498 - if (i->value_len) {
500 + int ea_ino = le32_to_cpu(s->here->e_value_inum);
501 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
503 + s->here->e_value_inum = cpu_to_le32(ea_ino);
504 + s->here->e_value_offs = 0;
505 + } else if (i->value_len) {
506 size_t size = EXT4_XATTR_SIZE(i->value_len);
507 void *val = s->base + min_offs - size;
508 s->here->e_value_offs = cpu_to_le16(min_offs - size);
509 + s->here->e_value_inum = 0;
510 memset(val + size - EXT4_XATTR_PAD, 0,
511 EXT4_XATTR_PAD); /* Clear the pad bytes. */
512 memcpy(val, i->value, i->value_len);
513 @@ -674,7 +963,7 @@ ext4_xattr_block_find(struct inode *inod
514 bs->s.end = bs->bh->b_data + bs->bh->b_size;
515 bs->s.here = bs->s.first;
516 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
517 - i->name, bs->bh->b_size, 1);
518 + i->name, bs->bh->b_size, 1, inode);
519 if (error && error != -ENODATA)
521 bs->s.not_found = error;
522 @@ -698,8 +987,6 @@ ext4_xattr_block_set(handle_t *handle, s
524 #define header(x) ((struct ext4_xattr_header *)(x))
526 - if (i->value && i->value_len > sb->s_blocksize)
529 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
531 @@ -714,7 +1001,7 @@ ext4_xattr_block_set(handle_t *handle, s
534 ea_bdebug(bs->bh, "modifying in-place");
535 - error = ext4_xattr_set_entry(i, s);
536 + error = ext4_xattr_set_entry(i, s, handle, inode);
538 if (!IS_LAST_ENTRY(s->first))
539 ext4_xattr_rehash(header(s->base),
540 @@ -766,7 +1053,7 @@ ext4_xattr_block_set(handle_t *handle, s
541 s->end = s->base + sb->s_blocksize;
544 - error = ext4_xattr_set_entry(i, s);
545 + error = ext4_xattr_set_entry(i, s, handle, inode);
549 @@ -917,7 +1204,7 @@ ext4_xattr_ibody_find(struct inode *inod
550 /* Find the named attribute. */
551 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
553 - (void *)is->s.base, 0);
554 + (void *)is->s.base, 0, inode);
555 if (error && error != -ENODATA)
557 is->s.not_found = error;
558 @@ -936,7 +1223,7 @@ ext4_xattr_ibody_set(handle_t *handle, s
560 if (EXT4_I(inode)->i_extra_isize == 0)
562 - error = ext4_xattr_set_entry(i, s);
563 + error = ext4_xattr_set_entry(i, s, handle, inode);
566 header = IHDR(inode, ext4_raw_inode(&is->iloc));
567 @@ -972,7 +1259,7 @@ ext4_xattr_set_handle(handle_t *handle,
570 .value_len = value_len,
574 struct ext4_xattr_ibody_find is = {
575 .s = { .not_found = -ENODATA, },
576 @@ -1041,6 +1328,15 @@ ext4_xattr_set_handle(handle_t *handle,
579 error = ext4_xattr_block_set(handle, inode, &i, &bs);
580 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
581 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
582 + error == -ENOSPC) {
583 + /* xattr not fit to block, store at external
586 + error = ext4_xattr_ibody_set(handle, inode,
591 if (!is.s.not_found) {
592 @@ -1087,10 +1383,25 @@ ext4_xattr_set(struct inode *inode, int
593 const void *value, size_t value_len, int flags)
596 + struct super_block *sb = inode->i_sb;
597 + int buffer_credits;
598 int error, retries = 0;
600 + buffer_credits = EXT4_DATA_TRANS_BLOCKS(sb);
601 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
602 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
603 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
604 + sb->s_blocksize_bits;
606 + /* For new inode */
607 + buffer_credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
609 + /* For data blocks of EA inode */
610 + buffer_credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
614 - handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
615 + handle = ext4_journal_start(inode, buffer_credits);
616 if (IS_ERR(handle)) {
617 error = PTR_ERR(handle);
619 @@ -1100,7 +1411,7 @@ retry:
620 value, value_len, flags);
621 error2 = ext4_journal_stop(handle);
622 if (error == -ENOSPC &&
623 - ext4_should_retry_alloc(inode->i_sb, &retries))
624 + ext4_should_retry_alloc(sb, &retries))
628 @@ -1122,7 +1433,7 @@ static void ext4_xattr_shift_entries(str
630 /* Adjust the value offsets of the entries */
631 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
632 - if (!last->e_value_block && last->e_value_size) {
633 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
634 new_offs = le16_to_cpu(last->e_value_offs) +
636 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
637 @@ -1355,22 +1666,135 @@ cleanup:
641 +#define EIA_INCR 16 /* must be 2^n */
642 +#define EIA_MASK (EIA_INCR - 1)
643 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
644 + * If @lea_ino_array is new or full it will be grown and the old
645 + * contents copied over.
648 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
650 + if (*lea_ino_array == NULL) {
652 + * Start with 15 inodes, so it fits into a power-of-two size.
653 + * If *lea_ino_array is NULL, this is essentially offsetof()
656 + kmalloc(offsetof(struct ext4_xattr_ino_array,
657 + xia_inodes[EIA_MASK]),
659 + if (*lea_ino_array == NULL)
661 + (*lea_ino_array)->xia_count = 0;
662 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
663 + /* expand the array once all 15 + n * 16 slots are full */
664 + struct ext4_xattr_ino_array *new_array = NULL;
665 + int count = (*lea_ino_array)->xia_count;
667 + /* if new_array is NULL, this is essentially offsetof() */
668 + new_array = kmalloc(
669 + offsetof(struct ext4_xattr_ino_array,
670 + xia_inodes[count + EIA_INCR]),
672 + if (new_array == NULL)
674 + memcpy(new_array, *lea_ino_array,
675 + offsetof(struct ext4_xattr_ino_array,
676 + xia_inodes[count]));
677 + kfree(*lea_ino_array);
678 + *lea_ino_array = new_array;
680 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
685 + * Add xattr inode to orphan list
688 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
689 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
691 + struct inode *ea_inode = NULL;
692 + int idx = 0, error = 0;
694 + if (lea_ino_array == NULL)
697 + for (; idx < lea_ino_array->xia_count; ++idx) {
698 + if (!ext4_handle_has_enough_credits(handle, credits)) {
699 + error = ext4_journal_extend(handle, credits);
701 + error = ext4_journal_restart(handle, credits);
704 + ext4_warning(inode->i_sb,
705 + "couldn't extend journal "
706 + "(err %d)", error);
710 + ea_inode = ext4_xattr_inode_iget(inode,
711 + lea_ino_array->xia_inodes[idx], &error);
714 + ext4_orphan_add(handle, ea_inode);
715 + /* the inode's i_count will be released by caller */
722 * ext4_xattr_delete_inode()
724 - * Free extended attribute resources associated with this inode. This
725 + * Free extended attribute resources associated with this inode. Traverse
726 + * all entries and unlink any xattr inodes associated with this inode. This
727 * is called immediately before an inode is freed. We have exclusive
728 - * access to the inode.
729 + * access to the inode. If an orphan inode is deleted it will also delete any
730 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
731 + * to ensure they belong to the parent inode and were not deleted already.
734 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
736 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
737 + struct ext4_xattr_ino_array **lea_ino_array)
739 struct buffer_head *bh = NULL;
740 + struct ext4_xattr_ibody_header *header;
741 + struct ext4_inode *raw_inode;
742 + struct ext4_iloc iloc;
743 + struct ext4_xattr_entry *entry;
744 + int credits = 3, error = 0;
746 - if (!EXT4_I(inode)->i_file_acl)
747 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
748 + goto delete_external_ea;
750 + error = ext4_get_inode_loc(inode, &iloc);
753 + raw_inode = ext4_raw_inode(&iloc);
754 + header = IHDR(inode, raw_inode);
755 + entry = IFIRST(header);
756 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
757 + if (entry->e_value_inum == 0)
759 + if (ext4_expand_ino_array(lea_ino_array,
760 + entry->e_value_inum) != 0) {
764 + entry->e_value_inum = 0;
769 + if (!EXT4_I(inode)->i_file_acl) {
770 + /* add xattr inode to orphan list */
771 + ext4_xattr_inode_orphan_add(handle, inode, credits,
775 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
777 ext4_error(inode->i_sb, "inode %lu: block %llu read error",
778 @@ -1383,11 +1807,71 @@ ext4_xattr_delete_inode(handle_t *handle
779 inode->i_ino, EXT4_I(inode)->i_file_acl);
783 + entry = BFIRST(bh);
784 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
785 + if (entry->e_value_inum == 0)
787 + if (ext4_expand_ino_array(lea_ino_array,
788 + entry->e_value_inum) != 0)
790 + entry->e_value_inum = 0;
793 + /* add xattr inode to orphan list */
794 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
799 + if (!IS_NOQUOTA(inode))
800 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
802 + if (!ext4_handle_has_enough_credits(handle, credits)) {
803 + error = ext4_journal_extend(handle, credits);
805 + error = ext4_journal_restart(handle, credits);
807 + ext4_warning(inode->i_sb,
808 + "couldn't extend journal (err %d)", error);
813 ext4_xattr_release_block(handle, inode, bh);
814 EXT4_I(inode)->i_file_acl = 0;
823 +ext4_xattr_inode_array_free(struct inode *inode,
824 + struct ext4_xattr_ino_array *lea_ino_array)
826 + struct inode *ea_inode = NULL;
830 + if (lea_ino_array == NULL)
833 + for (; idx < lea_ino_array->xia_count; ++idx) {
834 + ea_inode = ext4_xattr_inode_iget(inode,
835 + lea_ino_array->xia_inodes[idx], &err);
839 + /* for inode's i_count get from ext4_xattr_delete_inode */
840 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
843 + ea_inode->i_nlink = 0;
846 + kfree(lea_ino_array);
850 @@ -1457,10 +1941,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
851 entry1->e_name_index != entry2->e_name_index ||
852 entry1->e_name_len != entry2->e_name_len ||
853 entry1->e_value_size != entry2->e_value_size ||
854 + entry1->e_value_inum != entry2->e_value_inum ||
855 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
857 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
859 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
860 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
861 le32_to_cpu(entry1->e_value_size)))
862 @@ -1545,7 +2028,7 @@ static inline void ext4_xattr_hash_entry
866 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
867 + if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
868 __le32 *value = (__le32 *)((char *)header +
869 le16_to_cpu(entry->e_value_offs));
870 for (n = (le32_to_cpu(entry->e_value_size) +
871 Index: linux-stage/fs/ext4/xattr.h
872 ===================================================================
873 --- linux-stage.orig/fs/ext4/xattr.h
874 +++ linux-stage/fs/ext4/xattr.h
875 @@ -38,7 +38,7 @@ struct ext4_xattr_entry {
876 __u8 e_name_len; /* length of name */
877 __u8 e_name_index; /* attribute name index */
878 __le16 e_value_offs; /* offset in disk block of value */
879 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
880 + __le32 e_value_inum; /* inode in which the value is stored */
881 __le32 e_value_size; /* size of attribute value */
882 __le32 e_hash; /* hash value of name and value */
883 char e_name[0]; /* attribute name */
884 @@ -63,6 +63,15 @@ struct ext4_xattr_entry {
885 EXT4_I(inode)->i_extra_isize))
886 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
888 +#define i_xattr_inode_parent i_mtime.tv_sec
891 + * The minimum size of EA value when you start storing it in an external inode
892 + * size of block - size of header - size of 1 entry - 4 null bytes
894 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
895 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
897 # ifdef CONFIG_EXT4_FS_XATTR
899 extern struct xattr_handler ext4_xattr_user_handler;
900 @@ -77,7 +86,13 @@ extern int ext4_xattr_get(struct inode *
901 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
902 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
904 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
905 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, int ea_ino,
907 +extern int ext4_xattr_inode_unlink(struct inode *inode, int ea_ino);
908 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
909 + struct ext4_xattr_ino_array **array);
910 +extern void ext4_xattr_inode_array_free(struct inode *inode,
911 + struct ext4_xattr_ino_array *array);
912 extern void ext4_xattr_put_super(struct super_block *);
914 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
915 @@ -111,9 +126,11 @@ ext4_xattr_set_handle(handle_t *handle,
920 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
922 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
923 + struct ext4_xattr_ino_array **array)
925 + return -EOPNOTSUPP;
929 Index: linux-stage/fs/ext4/inode.c
930 ===================================================================
931 --- linux-stage.orig/fs/ext4/inode.c
932 +++ linux-stage/fs/ext4/inode.c
933 @@ -222,6 +222,8 @@ void ext4_delete_inode(struct inode *ino
937 + int extra_credits = 3;
938 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
940 if (ext4_should_order_data(inode))
941 ext4_begin_ordered_truncate(inode, 0);
942 @@ -235,7 +237,8 @@ void ext4_delete_inode(struct inode *ino
943 * protection against it
945 sb_start_intwrite(inode->i_sb);
946 - handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
948 + handle = ext4_journal_start(inode, extra_credits);
949 if (IS_ERR(handle)) {
950 ext4_std_error(inode->i_sb, PTR_ERR(handle));
952 @@ -247,9 +250,36 @@ void ext4_delete_inode(struct inode *ino
953 sb_end_intwrite(inode->i_sb);
958 ext4_handle_sync(handle);
961 + * Delete xattr inode before deleting the main inode.
963 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
965 + ext4_warning(inode->i_sb,
966 + "couldn't delete inode's xattr (err %d)", err);
970 + if (!IS_NOQUOTA(inode))
971 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
973 + if (!ext4_handle_has_enough_credits(handle,
974 + blocks_for_truncate(inode) + extra_credits)) {
975 + err = ext4_journal_extend(handle,
976 + blocks_for_truncate(inode) + extra_credits);
978 + err = ext4_journal_restart(handle,
979 + blocks_for_truncate(inode) + extra_credits);
981 + ext4_warning(inode->i_sb,
982 + "couldn't extend journal (err %d)", err);
988 err = ext4_mark_inode_dirty(handle, inode);
990 @@ -303,8 +333,12 @@ void ext4_delete_inode(struct inode *ino
993 ext4_free_inode(handle, inode);
995 ext4_journal_stop(handle);
996 sb_end_intwrite(inode->i_sb);
998 + if (lea_ino_array != NULL)
999 + ext4_xattr_inode_array_free(inode, lea_ino_array);
1002 clear_inode(inode); /* We must guarantee clearing of inode... */
1003 Index: linux-stage/fs/ext4/ialloc.c
1004 ===================================================================
1005 --- linux-stage.orig/fs/ext4/ialloc.c
1006 +++ linux-stage/fs/ext4/ialloc.c
1007 @@ -219,7 +219,6 @@ void ext4_free_inode(handle_t *handle, s
1008 * as writing the quota to disk may need the lock as well.
1011 - ext4_xattr_delete_inode(handle, inode);
1012 vfs_dq_free_inode(inode);