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 @@ -1412,6 +1412,7 @@ static inline void ext4_clear_state_flag
6 EXT4_FEATURE_INCOMPAT_EXTENTS| \
7 EXT4_FEATURE_INCOMPAT_64BIT| \
8 EXT4_FEATURE_INCOMPAT_FLEX_BG| \
9 + EXT4_FEATURE_INCOMPAT_EA_INODE| \
10 EXT4_FEATURE_INCOMPAT_MMP| \
11 EXT4_FEATURE_INCOMPAT_DIRDATA)
13 @@ -1775,6 +1776,12 @@ struct mmpd_data {
17 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
18 + * This limit is arbitrary, but is reasonable for the xattr API.
20 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE (1024 * 1024)
26 @@ -1786,6 +1793,10 @@ struct mmpd_data {
27 # define ATTRIB_NORET __attribute__((noreturn))
28 # define NORET_AND noreturn,
30 +struct ext4_xattr_ino_array {
31 + unsigned int xia_count; /* # of used item in the array */
32 + unsigned int xia_inodes[0];
35 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
37 @@ -1905,6 +1916,7 @@ extern void ext4_set_inode_flags(struct
38 extern void ext4_get_inode_flags(struct ext4_inode_info *);
39 extern int ext4_alloc_da_blocks(struct inode *inode);
40 extern void ext4_set_aops(struct inode *inode);
41 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
42 extern int ext4_writepage_trans_blocks(struct inode *);
43 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
44 extern int ext4_block_truncate_page(handle_t *handle,
45 Index: linux-stage/fs/ext4/inode.c
46 ===================================================================
47 --- linux-stage.orig/fs/ext4/inode.c
48 +++ linux-stage/fs/ext4/inode.c
49 @@ -187,6 +187,8 @@ void ext4_evict_inode(struct inode *inod
53 + int extra_credits = 3;
54 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
56 trace_ext4_evict_inode(inode);
58 @@ -207,7 +209,10 @@ void ext4_evict_inode(struct inode *inod
59 if (is_bad_inode(inode))
62 - handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
64 + * Delete xattr inode before deleting the main inode.
66 + handle = ext4_journal_start(inode, extra_credits);
68 ext4_std_error(inode->i_sb, PTR_ERR(handle));
70 @@ -218,9 +223,33 @@ void ext4_evict_inode(struct inode *inod
71 ext4_orphan_del(NULL, inode);
76 ext4_handle_sync(handle);
78 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
80 + ext4_warning(inode->i_sb,
81 + "couldn't delete inode's xattr (err %d)", err);
85 + if (!IS_NOQUOTA(inode))
86 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
88 + if (!ext4_handle_has_enough_credits(handle,
89 + blocks_for_truncate(inode) + extra_credits)) {
90 + err = ext4_journal_extend(handle,
91 + blocks_for_truncate(inode) + extra_credits);
93 + err = ext4_journal_restart(handle,
94 + blocks_for_truncate(inode) + extra_credits);
96 + ext4_warning(inode->i_sb,
97 + "couldn't extend journal (err %d)", err);
103 err = ext4_mark_inode_dirty(handle, inode);
105 @@ -237,10 +266,10 @@ void ext4_evict_inode(struct inode *inod
106 * enough credits left in the handle to remove the inode from
107 * the orphan list and set the dtime field.
109 - if (!ext4_handle_has_enough_credits(handle, 3)) {
110 - err = ext4_journal_extend(handle, 3);
111 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
112 + err = ext4_journal_extend(handle, extra_credits);
114 - err = ext4_journal_restart(handle, 3);
115 + err = ext4_journal_restart(handle, extra_credits);
117 ext4_warning(inode->i_sb,
118 "couldn't extend journal (err %d)", err);
119 @@ -274,7 +303,11 @@ void ext4_evict_inode(struct inode *inod
120 ext4_clear_inode(inode);
122 ext4_free_inode(handle, inode);
124 ext4_journal_stop(handle);
126 + if (lea_ino_array != NULL)
127 + ext4_xattr_inode_array_free(inode, lea_ino_array);
130 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
131 @@ -5551,7 +5584,7 @@ static int ext4_index_trans_blocks(struc
133 * Also account for superblock, inode, quota and xattr blocks
135 -static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
136 +int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
138 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
140 Index: linux-stage/fs/ext4/xattr.c
141 ===================================================================
142 --- linux-stage.orig/fs/ext4/xattr.c
143 +++ linux-stage/fs/ext4/xattr.c
144 @@ -168,19 +168,26 @@ ext4_xattr_check_block(struct buffer_hea
148 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
149 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
150 + struct inode *inode)
152 size_t value_size = le32_to_cpu(entry->e_value_size);
154 - if (entry->e_value_block != 0 || value_size > size ||
155 - le16_to_cpu(entry->e_value_offs) + value_size > size)
156 + if ((entry->e_value_inum == 0) &&
157 + (le16_to_cpu(entry->e_value_offs) + value_size > size))
159 + if (entry->e_value_inum != 0 &&
160 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
161 + le32_to_cpu(entry->e_value_inum) >
162 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
168 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
169 - const char *name, size_t size, int sorted)
170 + const char *name, size_t size, int sorted,
171 + struct inode *inode)
173 struct ext4_xattr_entry *entry;
175 @@ -200,11 +207,103 @@ ext4_xattr_find_entry(struct ext4_xattr_
179 - if (!cmp && ext4_xattr_check_entry(entry, size))
180 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
182 return cmp ? -ENODATA : 0;
186 + * Read the EA value from an inode.
189 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
191 + unsigned long block = 0;
192 + struct buffer_head *bh = NULL;
193 + int err, blocksize;
194 + size_t csize, ret_size = 0;
199 + blocksize = ea_inode->i_sb->s_blocksize;
201 + while (ret_size < *size) {
202 + csize = (*size - ret_size) > blocksize ? blocksize :
204 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
209 + memcpy(buf, bh->b_data, csize);
222 +struct inode *ext4_xattr_inode_iget(struct inode *parent, int ea_ino, int *err)
224 + struct inode *ea_inode = NULL;
226 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
227 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
228 + ext4_error(parent->i_sb, "error while reading EA inode %d",
234 + if (ea_inode->i_xattr_inode_parent != parent->i_ino ||
235 + ea_inode->i_generation != parent->i_generation) {
236 + ext4_error(parent->i_sb, "Backpointer from EA inode %d "
237 + "to parent invalid.", ea_ino);
242 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
243 + ext4_error(parent->i_sb, "EA inode %d does not have "
244 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
258 + * Read the value from the EA inode.
261 +ext4_xattr_inode_get(struct inode *inode, int ea_ino, void *buffer,
264 + struct inode *ea_inode = NULL;
267 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
271 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
278 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
279 void *buffer, size_t buffer_size)
280 @@ -235,7 +334,8 @@ bad_block:
282 ext4_xattr_cache_insert(bh);
284 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
285 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
290 @@ -245,8 +345,16 @@ bad_block:
292 if (size > buffer_size)
294 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
296 + if (entry->e_value_inum != 0) {
297 + error = ext4_xattr_inode_get(inode,
298 + le32_to_cpu(entry->e_value_inum),
303 + memcpy(buffer, bh->b_data +
304 + le16_to_cpu(entry->e_value_offs), size);
309 @@ -280,7 +388,7 @@ ext4_xattr_ibody_get(struct inode *inode
312 error = ext4_xattr_find_entry(&entry, name_index, name,
313 - end - (void *)entry, 0);
314 + end - (void *)entry, 0, inode);
317 size = le32_to_cpu(entry->e_value_size);
318 @@ -288,8 +396,16 @@ ext4_xattr_ibody_get(struct inode *inode
320 if (size > buffer_size)
322 - memcpy(buffer, (void *)IFIRST(header) +
323 - le16_to_cpu(entry->e_value_offs), size);
324 + if (entry->e_value_inum != 0) {
325 + error = ext4_xattr_inode_get(inode,
326 + le32_to_cpu(entry->e_value_inum),
331 + memcpy(buffer, (void *)IFIRST(header) +
332 + le16_to_cpu(entry->e_value_offs), size);
337 @@ -514,7 +630,7 @@ static size_t ext4_xattr_free_space(stru
339 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
340 *total += EXT4_XATTR_LEN(last->e_name_len);
341 - if (!last->e_value_block && last->e_value_size) {
342 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
343 size_t offs = le16_to_cpu(last->e_value_offs);
344 if (offs < *min_offs)
346 @@ -523,11 +639,162 @@ static size_t ext4_xattr_free_space(stru
347 return (*min_offs - ((void *)last - base) - sizeof(__u32));
351 + * Write the value of the EA in an inode.
354 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
355 + const void *buf, int bufsize)
357 + struct buffer_head *bh = NULL;
358 + struct ext4_map_blocks map;
359 + unsigned long block = 0;
360 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
361 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
362 + int csize, wsize = 0;
367 + while (ret >= 0 && ret < max_blocks) {
371 + map.m_lblk = block;
372 + map.m_len = max_blocks;
373 + ret = ext4_map_blocks(handle, ea_inode, &map,
374 + EXT4_GET_BLOCKS_CREATE);
376 + ext4_mark_inode_dirty(handle, ea_inode);
377 + if (ret == -ENOSPC &&
378 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
390 + while (wsize < bufsize) {
393 + csize = (bufsize - wsize) > blocksize ? blocksize :
395 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
398 + ret = ext4_journal_get_write_access(handle, bh);
402 + memcpy(bh->b_data, buf, csize);
403 + set_buffer_uptodate(bh);
404 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
411 + i_size_write(ea_inode, wsize);
412 + ext4_update_i_disksize(ea_inode, wsize);
414 + ext4_mark_inode_dirty(handle, ea_inode);
423 + * Create an inode to store the value of a large EA.
425 +static struct inode *
426 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
428 + struct inode *ea_inode = NULL;
431 + * Let the next inode be the goal, so we try and allocate the EA inode
432 + * in the same group, or nearby one.
434 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
435 + S_IFREG|0600, NULL, inode->i_ino + 1);
437 + if (!IS_ERR(ea_inode)) {
438 + ea_inode->i_op = &ext4_file_inode_operations;
439 + ea_inode->i_fop = &ext4_file_operations;
440 + ext4_set_aops(ea_inode);
441 + ea_inode->i_generation = inode->i_generation;
442 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
445 + * A back-pointer from EA inode to parent inode will be useful
448 + ea_inode->i_xattr_inode_parent = inode->i_ino;
449 + unlock_new_inode(ea_inode);
456 + * Unlink the inode storing the value of the EA.
459 +ext4_xattr_inode_unlink(struct inode *inode, int ea_ino)
461 + struct inode *ea_inode = NULL;
464 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
468 + ea_inode->i_nlink = 0;
475 + * Add value of the EA in an inode.
478 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, int *ea_ino,
479 + const void *value, size_t value_len)
481 + struct inode *ea_inode = NULL;
484 + /* Create an inode for the EA value */
485 + ea_inode = ext4_xattr_inode_create(handle, inode);
486 + if (IS_ERR(ea_inode))
489 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
491 + ea_inode->i_nlink = 0;
493 + *ea_ino = ea_inode->i_ino;
500 struct ext4_xattr_info {
509 struct ext4_xattr_search {
510 @@ -539,15 +806,23 @@ struct ext4_xattr_search {
514 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
515 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
516 + handle_t *handle, struct inode *inode)
518 struct ext4_xattr_entry *last;
519 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
520 + int in_inode = i->in_inode;
522 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
523 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
524 + (EXT4_XATTR_SIZE(i->value_len) >
525 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
528 /* Compute min_offs and last. */
530 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
531 - if (!last->e_value_block && last->e_value_size) {
532 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
533 size_t offs = le16_to_cpu(last->e_value_offs);
536 @@ -555,16 +830,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
538 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
540 - if (!s->here->e_value_block && s->here->e_value_size) {
541 + if (!in_inode && s->here->e_value_inum == 0 &&
542 + s->here->e_value_size > 0) {
543 size_t size = le32_to_cpu(s->here->e_value_size);
544 free += EXT4_XATTR_SIZE(size);
546 free += EXT4_XATTR_LEN(name_len);
549 - if (free < EXT4_XATTR_SIZE(i->value_len) ||
550 - free < EXT4_XATTR_LEN(name_len) +
551 - EXT4_XATTR_SIZE(i->value_len))
552 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
557 + if (free < value_len ||
558 + free < EXT4_XATTR_LEN(name_len) + value_len)
562 @@ -578,7 +858,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
563 s->here->e_name_len = name_len;
564 memcpy(s->here->e_name, i->name, name_len);
566 - if (!s->here->e_value_block && s->here->e_value_size) {
567 + if (s->here->e_value_offs > 0 && s->here->e_value_inum == 0 &&
568 + s->here->e_value_size > 0) {
569 void *first_val = s->base + min_offs;
570 size_t offs = le16_to_cpu(s->here->e_value_offs);
571 void *val = s->base + offs;
572 @@ -607,13 +888,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
574 while (!IS_LAST_ENTRY(last)) {
575 size_t o = le16_to_cpu(last->e_value_offs);
576 - if (!last->e_value_block &&
577 - last->e_value_size && o < offs)
578 + if (last->e_value_size > 0 && o < offs)
580 cpu_to_le16(o + size);
581 last = EXT4_XATTR_NEXT(last);
584 + if (s->here->e_value_inum != 0) {
585 + ext4_xattr_inode_unlink(inode,
586 + le32_to_cpu(s->here->e_value_inum));
587 + s->here->e_value_inum = 0;
590 /* Remove the old name. */
591 size_t size = EXT4_XATTR_LEN(name_len);
592 @@ -627,10 +912,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
594 /* Insert the new value. */
595 s->here->e_value_size = cpu_to_le32(i->value_len);
596 - if (i->value_len) {
598 + int ea_ino = le32_to_cpu(s->here->e_value_inum);
599 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
601 + s->here->e_value_inum = cpu_to_le32(ea_ino);
602 + s->here->e_value_offs = 0;
603 + } else if (i->value_len) {
604 size_t size = EXT4_XATTR_SIZE(i->value_len);
605 void *val = s->base + min_offs - size;
606 s->here->e_value_offs = cpu_to_le16(min_offs - size);
607 + s->here->e_value_inum = 0;
608 memset(val + size - EXT4_XATTR_PAD, 0,
609 EXT4_XATTR_PAD); /* Clear the pad bytes. */
610 memcpy(val, i->value, i->value_len);
611 @@ -675,7 +967,7 @@ ext4_xattr_block_find(struct inode *inod
612 bs->s.end = bs->bh->b_data + bs->bh->b_size;
613 bs->s.here = bs->s.first;
614 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
615 - i->name, bs->bh->b_size, 1);
616 + i->name, bs->bh->b_size, 1, inode);
617 if (error && error != -ENODATA)
619 bs->s.not_found = error;
620 @@ -699,8 +991,6 @@ ext4_xattr_block_set(handle_t *handle, s
622 #define header(x) ((struct ext4_xattr_header *)(x))
624 - if (i->value && i->value_len > sb->s_blocksize)
627 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
629 @@ -715,7 +1005,7 @@ ext4_xattr_block_set(handle_t *handle, s
632 ea_bdebug(bs->bh, "modifying in-place");
633 - error = ext4_xattr_set_entry(i, s);
634 + error = ext4_xattr_set_entry(i, s, handle, inode);
636 if (!IS_LAST_ENTRY(s->first))
637 ext4_xattr_rehash(header(s->base),
638 @@ -767,7 +1057,7 @@ ext4_xattr_block_set(handle_t *handle, s
639 s->end = s->base + sb->s_blocksize;
642 - error = ext4_xattr_set_entry(i, s);
643 + error = ext4_xattr_set_entry(i, s, handle, inode);
647 @@ -918,7 +1208,7 @@ ext4_xattr_ibody_find(struct inode *inod
648 /* Find the named attribute. */
649 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
651 - (void *)is->s.base, 0);
652 + (void *)is->s.base, 0, inode);
653 if (error && error != -ENODATA)
655 is->s.not_found = error;
656 @@ -937,7 +1227,7 @@ ext4_xattr_ibody_set(handle_t *handle, s
658 if (EXT4_I(inode)->i_extra_isize == 0)
660 - error = ext4_xattr_set_entry(i, s);
661 + error = ext4_xattr_set_entry(i, s, handle, inode);
664 header = IHDR(inode, ext4_raw_inode(&is->iloc));
665 @@ -973,7 +1263,7 @@ ext4_xattr_set_handle(handle_t *handle,
668 .value_len = value_len,
672 struct ext4_xattr_ibody_find is = {
673 .s = { .not_found = -ENODATA, },
674 @@ -1042,6 +1332,15 @@ ext4_xattr_set_handle(handle_t *handle,
677 error = ext4_xattr_block_set(handle, inode, &i, &bs);
678 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
679 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
680 + error == -ENOSPC) {
681 + /* xattr not fit to block, store at external
684 + error = ext4_xattr_ibody_set(handle, inode,
689 if (!is.s.not_found) {
690 @@ -1089,10 +1388,25 @@ ext4_xattr_set(struct inode *inode, int
691 const void *value, size_t value_len, int flags)
694 + struct super_block *sb = inode->i_sb;
695 + int buffer_credits;
696 int error, retries = 0;
698 + buffer_credits = EXT4_DATA_TRANS_BLOCKS(sb);
699 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
700 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
701 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
702 + sb->s_blocksize_bits;
704 + /* For new inode */
705 + buffer_credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
707 + /* For data blocks of EA inode */
708 + buffer_credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
712 - handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
713 + handle = ext4_journal_start(inode, buffer_credits);
714 if (IS_ERR(handle)) {
715 error = PTR_ERR(handle);
717 @@ -1102,7 +1416,7 @@ retry:
718 value, value_len, flags);
719 error2 = ext4_journal_stop(handle);
720 if (error == -ENOSPC &&
721 - ext4_should_retry_alloc(inode->i_sb, &retries))
722 + ext4_should_retry_alloc(sb, &retries))
726 @@ -1124,7 +1438,7 @@ static void ext4_xattr_shift_entries(str
728 /* Adjust the value offsets of the entries */
729 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
730 - if (!last->e_value_block && last->e_value_size) {
731 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
732 new_offs = le16_to_cpu(last->e_value_offs) +
734 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
735 @@ -1359,20 +1673,92 @@ cleanup:
740 +#define EIA_INCR 16 /* must be 2^n */
741 +#define EIA_MASK (EIA_INCR - 1)
742 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
743 + * If @lea_ino_array is new or full it will be grown and the old
744 + * contents copied over.
747 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
749 + if (*lea_ino_array == NULL) {
751 + * Start with 15 inodes, so it fits into a powr-of-two size.
752 + * If *lea_ino_array is NULL, this is essentially offsetof()
755 + kmalloc(offsetof(struct ext4_xattr_ino_array,
756 + xia_inodes[EIA_MASK]),
758 + if (*lea_ino_array == NULL)
760 + (*lea_ino_array)->xia_count = 0;
761 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
762 + /* expand the array once all 15 + n * 16 slots are full */
763 + struct ext4_xattr_ino_array *new_array = NULL;
764 + int count = (*lea_ino_array)->xia_count;
766 + /* if new_array is NULL, this is essentially offsetof() */
767 + new_array = kmalloc(
768 + offsetof(struct ext4_xattr_ino_array,
769 + xia_inodes[count + EIA_INCR]),
771 + if (new_array == NULL)
773 + memcpy(new_array, *lea_ino_array,
774 + offsetof(struct ext4_xattr_ino_array,
775 + xia_inodes[count]));
776 + kfree(*lea_ino_array);
777 + *lea_ino_array = new_array;
779 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
784 * ext4_xattr_delete_inode()
786 - * Free extended attribute resources associated with this inode. This
787 + * Free extended attribute resources associated with this inode. Traverse
788 + * all entries and unlink any xattr inodes associated with this inode. This
789 * is called immediately before an inode is freed. We have exclusive
790 - * access to the inode.
791 + * access to the inode. If an orphan inode is deleted it will also delete any
792 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
793 + * to ensure they belong to the parent inode and were not deleted already.
796 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
798 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
799 + struct ext4_xattr_ino_array **lea_ino_array)
801 struct buffer_head *bh = NULL;
802 + struct ext4_xattr_ibody_header *header;
803 + struct ext4_inode *raw_inode;
804 + struct ext4_iloc iloc;
805 + struct ext4_xattr_entry *entry;
806 + int credits = 3, error = 0;
808 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
809 + goto delete_external_ea;
811 + error = ext4_get_inode_loc(inode, &iloc);
814 + raw_inode = ext4_raw_inode(&iloc);
815 + header = IHDR(inode, raw_inode);
816 + entry = IFIRST(header);
817 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
818 + if (entry->e_value_inum == 0)
820 + if (ext4_expand_ino_array(lea_ino_array,
821 + entry->e_value_inum) != 0) {
825 + entry->e_value_inum = 0;
830 if (!EXT4_I(inode)->i_file_acl)
832 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
833 @@ -1387,11 +1770,88 @@ ext4_xattr_delete_inode(handle_t *handle
834 EXT4_I(inode)->i_file_acl);
838 + entry = BFIRST(bh);
839 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
840 + if (entry->e_value_inum == 0)
842 + if (ext4_expand_ino_array(lea_ino_array,
843 + entry->e_value_inum) != 0)
845 + entry->e_value_inum = 0;
848 + /* adding xattr inode to orphan list */
849 + if (*lea_ino_array != NULL) {
850 + struct inode *ea_inode = NULL;
851 + int idx = (*lea_ino_array)->xia_count;
853 + for (idx = 0; idx < (*lea_ino_array)->xia_count; ++idx) {
854 + if (!ext4_handle_has_enough_credits(handle, credits)) {
855 + error = ext4_journal_extend(handle, credits);
857 + error = ext4_journal_restart(handle, credits);
859 + ext4_warning(inode->i_sb,
860 + "couldn't extend journal "
861 + "(err %d)", error);
865 + ea_inode = ext4_xattr_inode_iget(inode,
866 + (*lea_ino_array)->xia_inodes[idx], &error);
869 + ext4_orphan_add(handle, ea_inode);
870 + /* the inode's i_count will be released by caller */
874 + if (!IS_NOQUOTA(inode))
875 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
877 + if (!ext4_handle_has_enough_credits(handle, credits)) {
878 + error = ext4_journal_extend(handle, credits);
880 + error = ext4_journal_restart(handle, credits);
882 + ext4_warning(inode->i_sb,
883 + "couldn't extend journal (err %d)", error);
888 ext4_xattr_release_block(handle, inode, bh);
889 EXT4_I(inode)->i_file_acl = 0;
898 +ext4_xattr_inode_array_free(struct inode *inode,
899 + struct ext4_xattr_ino_array *lea_ino_array)
901 + struct inode *ea_inode = NULL;
905 + if (lea_ino_array == NULL)
908 + for (; idx < lea_ino_array->xia_count; ++idx) {
909 + ea_inode = ext4_xattr_inode_iget(inode,
910 + lea_ino_array->xia_inodes[idx], &err);
913 + ea_inode->i_nlink = 0;
915 + /* for inode's i_count get from ext4_xattr_delete_inode */
918 + kfree(lea_ino_array);
922 @@ -1461,10 +1921,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
923 entry1->e_name_index != entry2->e_name_index ||
924 entry1->e_name_len != entry2->e_name_len ||
925 entry1->e_value_size != entry2->e_value_size ||
926 + entry1->e_value_inum != entry2->e_value_inum ||
927 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
929 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
931 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
932 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
933 le32_to_cpu(entry1->e_value_size)))
934 @@ -1548,7 +2007,7 @@ static inline void ext4_xattr_hash_entry
938 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
939 + if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
940 __le32 *value = (__le32 *)((char *)header +
941 le16_to_cpu(entry->e_value_offs));
942 for (n = (le32_to_cpu(entry->e_value_size) +
943 Index: linux-stage/fs/ext4/xattr.h
944 ===================================================================
945 --- linux-stage.orig/fs/ext4/xattr.h
946 +++ linux-stage/fs/ext4/xattr.h
947 @@ -38,7 +38,7 @@ struct ext4_xattr_entry {
948 __u8 e_name_len; /* length of name */
949 __u8 e_name_index; /* attribute name index */
950 __le16 e_value_offs; /* offset in disk block of value */
951 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
952 + __le32 e_value_inum; /* inode in which the value is stored */
953 __le32 e_value_size; /* size of attribute value */
954 __le32 e_hash; /* hash value of name and value */
955 char e_name[0]; /* attribute name */
956 @@ -63,6 +63,15 @@ struct ext4_xattr_entry {
957 EXT4_I(inode)->i_extra_isize))
958 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
960 +#define i_xattr_inode_parent i_mtime.tv_sec
963 + * The minimum size of EA value when you start storing it in an external inode
964 + * size of block - size of header - size of 1 entry - 4 null bytes
966 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
967 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
969 # ifdef CONFIG_EXT4_FS_XATTR
971 extern const struct xattr_handler ext4_xattr_user_handler;
972 @@ -77,7 +86,13 @@ extern int ext4_xattr_get(struct inode *
973 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
974 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
976 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
977 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, int ea_ino,
979 +extern int ext4_xattr_inode_unlink(struct inode *inode, int ea_ino);
980 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
981 + struct ext4_xattr_ino_array **array);
982 +extern void ext4_xattr_inode_array_free(struct inode *inode,
983 + struct ext4_xattr_ino_array *array);
984 extern void ext4_xattr_put_super(struct super_block *);
986 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
987 @@ -111,9 +126,11 @@ ext4_xattr_set_handle(handle_t *handle,
992 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
994 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
995 + struct ext4_xattr_ino_array *array)
997 + return -EOPNOTSUPP;
1001 Index: linux-stage/fs/ext4/ialloc.c
1002 ===================================================================
1003 --- linux-stage.orig/fs/ext4/ialloc.c
1004 +++ linux-stage/fs/ext4/ialloc.c
1005 @@ -222,7 +222,6 @@ void ext4_free_inode(handle_t *handle, s
1006 * as writing the quota to disk may need the lock as well.
1008 dquot_initialize(inode);
1009 - ext4_xattr_delete_inode(handle, inode);
1010 dquot_free_inode(inode);