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 @@ -1775,6 +1782,10 @@ struct mmpd_data {
20 # define ATTRIB_NORET __attribute__((noreturn))
21 # define NORET_AND noreturn,
23 +struct ext4_xattr_ino_array {
24 + unsigned int xia_count; /* # of used item in the array */
25 + unsigned int xia_inodes[0];
28 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
30 @@ -1893,6 +1904,7 @@ extern void ext4_set_inode_flags(struct
31 extern void ext4_get_inode_flags(struct ext4_inode_info *);
32 extern int ext4_alloc_da_blocks(struct inode *inode);
33 extern void ext4_set_aops(struct inode *inode);
34 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
35 extern int ext4_writepage_trans_blocks(struct inode *);
36 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
37 extern int ext4_block_truncate_page(handle_t *handle,
38 Index: linux-stage/fs/ext4/inode.c
39 ===================================================================
40 --- linux-stage.orig/fs/ext4/inode.c
41 +++ linux-stage/fs/ext4/inode.c
42 @@ -187,6 +187,8 @@ void ext4_evict_inode(struct inode *inod
46 + int extra_credits = 3;
47 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
49 trace_ext4_evict_inode(inode);
51 @@ -207,7 +209,10 @@ void ext4_evict_inode(struct inode *inod
52 if (is_bad_inode(inode))
55 - handle = ext4_journal_start(inode, blocks_for_truncate(inode)+3);
57 + * Delete xattr inode before deleting the main inode.
59 + handle = ext4_journal_start(inode, extra_credits);
61 ext4_std_error(inode->i_sb, PTR_ERR(handle));
63 @@ -218,9 +223,33 @@ void ext4_evict_inode(struct inode *inod
64 ext4_orphan_del(NULL, inode);
69 ext4_handle_sync(handle);
71 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
73 + ext4_warning(inode->i_sb,
74 + "couldn't delete inode's xattr (err %d)", err);
78 + if (!IS_NOQUOTA(inode))
79 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
81 + if (!ext4_handle_has_enough_credits(handle,
82 + blocks_for_truncate(inode) + extra_credits)) {
83 + err = ext4_journal_extend(handle,
84 + blocks_for_truncate(inode) + extra_credits);
86 + err = ext4_journal_restart(handle,
87 + blocks_for_truncate(inode) + extra_credits);
89 + ext4_warning(inode->i_sb,
90 + "couldn't extend journal (err %d)", err);
96 err = ext4_mark_inode_dirty(handle, inode);
98 @@ -237,10 +266,10 @@ void ext4_evict_inode(struct inode *inod
99 * enough credits left in the handle to remove the inode from
100 * the orphan list and set the dtime field.
102 - if (!ext4_handle_has_enough_credits(handle, 3)) {
103 - err = ext4_journal_extend(handle, 3);
104 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
105 + err = ext4_journal_extend(handle, extra_credits);
107 - err = ext4_journal_restart(handle, 3);
108 + err = ext4_journal_restart(handle, extra_credits);
110 ext4_warning(inode->i_sb,
111 "couldn't extend journal (err %d)", err);
112 @@ -274,7 +303,11 @@ void ext4_evict_inode(struct inode *inod
113 ext4_clear_inode(inode);
115 ext4_free_inode(handle, inode);
117 ext4_journal_stop(handle);
119 + if (lea_ino_array != NULL)
120 + ext4_xattr_inode_array_free(inode, lea_ino_array);
123 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
124 @@ -5552,7 +5585,7 @@ static int ext4_index_trans_blocks(struc
126 * Also account for superblock, inode, quota and xattr blocks
128 -static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
129 +int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
131 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
133 Index: linux-stage/fs/ext4/xattr.c
134 ===================================================================
135 --- linux-stage.orig/fs/ext4/xattr.c
136 +++ linux-stage/fs/ext4/xattr.c
137 @@ -168,19 +168,26 @@ ext4_xattr_check_block(struct buffer_hea
141 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
142 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
143 + struct inode *inode)
145 size_t value_size = le32_to_cpu(entry->e_value_size);
147 - if (entry->e_value_block != 0 || value_size > size ||
148 + if (!entry->e_value_inum &&
149 le16_to_cpu(entry->e_value_offs) + value_size > size)
151 + if (entry->e_value_inum &&
152 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
153 + le32_to_cpu(entry->e_value_inum) >
154 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
160 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
161 - const char *name, size_t size, int sorted)
162 + const char *name, size_t size, int sorted,
163 + struct inode *inode)
165 struct ext4_xattr_entry *entry;
167 @@ -200,11 +207,104 @@ ext4_xattr_find_entry(struct ext4_xattr_
171 - if (!cmp && ext4_xattr_check_entry(entry, size))
172 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
174 return cmp ? -ENODATA : 0;
178 + * Read the EA value from an inode.
181 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
183 + unsigned long block = 0;
184 + struct buffer_head *bh = NULL;
185 + int err, blocksize;
186 + size_t csize, ret_size = 0;
191 + blocksize = ea_inode->i_sb->s_blocksize;
193 + while (ret_size < *size) {
194 + csize = (*size - ret_size) > blocksize ? blocksize :
196 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
201 + memcpy(buf, bh->b_data, csize);
214 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
216 + struct inode *ea_inode = NULL;
218 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
219 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
220 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
221 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
222 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
223 + *err = rc != 0 ? rc : -EIO;
227 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
228 + ea_inode->i_generation != parent->i_generation) {
229 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
230 + "to parent invalid.", ea_ino);
235 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
236 + ext4_error(parent->i_sb, "EA inode %lu does not have "
237 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
251 + * Read the value from the EA inode.
254 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
257 + struct inode *ea_inode = NULL;
260 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
264 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
271 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
272 void *buffer, size_t buffer_size)
273 @@ -235,7 +334,8 @@ bad_block:
275 ext4_xattr_cache_insert(bh);
277 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
278 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
283 @@ -245,8 +345,16 @@ bad_block:
285 if (size > buffer_size)
287 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
289 + if (entry->e_value_inum) {
290 + error = ext4_xattr_inode_get(inode,
291 + le32_to_cpu(entry->e_value_inum),
296 + memcpy(buffer, bh->b_data +
297 + le16_to_cpu(entry->e_value_offs), size);
302 @@ -280,7 +388,7 @@ ext4_xattr_ibody_get(struct inode *inode
305 error = ext4_xattr_find_entry(&entry, name_index, name,
306 - end - (void *)entry, 0);
307 + end - (void *)entry, 0, inode);
310 size = le32_to_cpu(entry->e_value_size);
311 @@ -288,8 +396,16 @@ ext4_xattr_ibody_get(struct inode *inode
313 if (size > buffer_size)
315 - memcpy(buffer, (void *)IFIRST(header) +
316 - le16_to_cpu(entry->e_value_offs), size);
317 + if (entry->e_value_inum) {
318 + error = ext4_xattr_inode_get(inode,
319 + le32_to_cpu(entry->e_value_inum),
324 + memcpy(buffer, (void *)IFIRST(header) +
325 + le16_to_cpu(entry->e_value_offs), size);
330 @@ -514,7 +630,7 @@ static size_t ext4_xattr_free_space(stru
332 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
333 *total += EXT4_XATTR_LEN(last->e_name_len);
334 - if (!last->e_value_block && last->e_value_size) {
335 + if (!last->e_value_inum && last->e_value_size) {
336 size_t offs = le16_to_cpu(last->e_value_offs);
337 if (offs < *min_offs)
339 @@ -523,11 +639,162 @@ static size_t ext4_xattr_free_space(stru
340 return (*min_offs - ((void *)last - base) - sizeof(__u32));
344 + * Write the value of the EA in an inode.
347 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
348 + const void *buf, int bufsize)
350 + struct buffer_head *bh = NULL;
351 + struct ext4_map_blocks map;
352 + unsigned long block = 0;
353 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
354 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
355 + int csize, wsize = 0;
360 + while (ret >= 0 && ret < max_blocks) {
364 + map.m_lblk = block;
365 + map.m_len = max_blocks;
366 + ret = ext4_map_blocks(handle, ea_inode, &map,
367 + EXT4_GET_BLOCKS_CREATE);
369 + ext4_mark_inode_dirty(handle, ea_inode);
370 + if (ret == -ENOSPC &&
371 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
383 + while (wsize < bufsize) {
386 + csize = (bufsize - wsize) > blocksize ? blocksize :
388 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
391 + ret = ext4_journal_get_write_access(handle, bh);
395 + memcpy(bh->b_data, buf, csize);
396 + set_buffer_uptodate(bh);
397 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
404 + i_size_write(ea_inode, wsize);
405 + ext4_update_i_disksize(ea_inode, wsize);
407 + ext4_mark_inode_dirty(handle, ea_inode);
416 + * Create an inode to store the value of a large EA.
418 +static struct inode *
419 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
421 + struct inode *ea_inode = NULL;
424 + * Let the next inode be the goal, so we try and allocate the EA inode
425 + * in the same group, or nearby one.
427 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
428 + S_IFREG|0600, NULL, inode->i_ino + 1);
430 + if (!IS_ERR(ea_inode)) {
431 + ea_inode->i_op = &ext4_file_inode_operations;
432 + ea_inode->i_fop = &ext4_file_operations;
433 + ext4_set_aops(ea_inode);
434 + ea_inode->i_generation = inode->i_generation;
435 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
438 + * A back-pointer from EA inode to parent inode will be useful
441 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
442 + unlock_new_inode(ea_inode);
449 + * Unlink the inode storing the value of the EA.
452 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
454 + struct inode *ea_inode = NULL;
457 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
461 + ea_inode->i_nlink = 0;
468 + * Add value of the EA in an inode.
471 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
472 + const void *value, size_t value_len)
474 + struct inode *ea_inode = NULL;
477 + /* Create an inode for the EA value */
478 + ea_inode = ext4_xattr_inode_create(handle, inode);
479 + if (IS_ERR(ea_inode))
482 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
484 + ea_inode->i_nlink = 0;
486 + *ea_ino = ea_inode->i_ino;
493 struct ext4_xattr_info {
502 struct ext4_xattr_search {
503 @@ -539,15 +806,23 @@ struct ext4_xattr_search {
507 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
508 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
509 + handle_t *handle, struct inode *inode)
511 struct ext4_xattr_entry *last;
512 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
513 + int in_inode = i->in_inode;
515 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
516 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
517 + (EXT4_XATTR_SIZE(i->value_len) >
518 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
521 /* Compute min_offs and last. */
523 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
524 - if (!last->e_value_block && last->e_value_size) {
525 + if (!last->e_value_inum && last->e_value_size) {
526 size_t offs = le16_to_cpu(last->e_value_offs);
529 @@ -555,16 +830,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
531 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
533 - if (!s->here->e_value_block && s->here->e_value_size) {
535 + !s->here->e_value_inum && s->here->e_value_size) {
536 size_t size = le32_to_cpu(s->here->e_value_size);
537 free += EXT4_XATTR_SIZE(size);
539 free += EXT4_XATTR_LEN(name_len);
542 - if (free < EXT4_XATTR_SIZE(i->value_len) ||
543 - free < EXT4_XATTR_LEN(name_len) +
544 - EXT4_XATTR_SIZE(i->value_len))
545 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
550 + if (free < value_len ||
551 + free < EXT4_XATTR_LEN(name_len) + value_len)
555 @@ -578,7 +858,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
556 s->here->e_name_len = name_len;
557 memcpy(s->here->e_name, i->name, name_len);
559 - if (!s->here->e_value_block && s->here->e_value_size) {
560 + if (!s->here->e_value_inum && s->here->e_value_size &&
561 + s->here->e_value_offs > 0) {
562 void *first_val = s->base + min_offs;
563 size_t offs = le16_to_cpu(s->here->e_value_offs);
564 void *val = s->base + offs;
565 @@ -607,13 +888,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
567 while (!IS_LAST_ENTRY(last)) {
568 size_t o = le16_to_cpu(last->e_value_offs);
569 - if (!last->e_value_block &&
570 + if (!last->e_value_inum &&
571 last->e_value_size && o < offs)
573 cpu_to_le16(o + size);
574 last = EXT4_XATTR_NEXT(last);
577 + if (s->here->e_value_inum) {
578 + ext4_xattr_inode_unlink(inode,
579 + le32_to_cpu(s->here->e_value_inum));
580 + s->here->e_value_inum = 0;
583 /* Remove the old name. */
584 size_t size = EXT4_XATTR_LEN(name_len);
585 @@ -627,10 +912,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
587 /* Insert the new value. */
588 s->here->e_value_size = cpu_to_le32(i->value_len);
589 - if (i->value_len) {
591 + unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
592 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
594 + s->here->e_value_inum = cpu_to_le32(ea_ino);
595 + s->here->e_value_offs = 0;
596 + } else if (i->value_len) {
597 size_t size = EXT4_XATTR_SIZE(i->value_len);
598 void *val = s->base + min_offs - size;
599 s->here->e_value_offs = cpu_to_le16(min_offs - size);
600 + s->here->e_value_inum = 0;
601 memset(val + size - EXT4_XATTR_PAD, 0,
602 EXT4_XATTR_PAD); /* Clear the pad bytes. */
603 memcpy(val, i->value, i->value_len);
604 @@ -675,7 +967,7 @@ ext4_xattr_block_find(struct inode *inod
605 bs->s.end = bs->bh->b_data + bs->bh->b_size;
606 bs->s.here = bs->s.first;
607 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
608 - i->name, bs->bh->b_size, 1);
609 + i->name, bs->bh->b_size, 1, inode);
610 if (error && error != -ENODATA)
612 bs->s.not_found = error;
613 @@ -699,8 +991,6 @@ ext4_xattr_block_set(handle_t *handle, s
615 #define header(x) ((struct ext4_xattr_header *)(x))
617 - if (i->value && i->value_len > sb->s_blocksize)
620 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
622 @@ -715,7 +1005,7 @@ ext4_xattr_block_set(handle_t *handle, s
625 ea_bdebug(bs->bh, "modifying in-place");
626 - error = ext4_xattr_set_entry(i, s);
627 + error = ext4_xattr_set_entry(i, s, handle, inode);
629 if (!IS_LAST_ENTRY(s->first))
630 ext4_xattr_rehash(header(s->base),
631 @@ -767,7 +1057,7 @@ ext4_xattr_block_set(handle_t *handle, s
632 s->end = s->base + sb->s_blocksize;
635 - error = ext4_xattr_set_entry(i, s);
636 + error = ext4_xattr_set_entry(i, s, handle, inode);
640 @@ -918,7 +1208,7 @@ ext4_xattr_ibody_find(struct inode *inod
641 /* Find the named attribute. */
642 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
644 - (void *)is->s.base, 0);
645 + (void *)is->s.base, 0, inode);
646 if (error && error != -ENODATA)
648 is->s.not_found = error;
649 @@ -937,7 +1227,7 @@ ext4_xattr_ibody_set(handle_t *handle, s
651 if (EXT4_I(inode)->i_extra_isize == 0)
653 - error = ext4_xattr_set_entry(i, s);
654 + error = ext4_xattr_set_entry(i, s, handle, inode);
657 header = IHDR(inode, ext4_raw_inode(&is->iloc));
658 @@ -973,7 +1263,7 @@ ext4_xattr_set_handle(handle_t *handle,
661 .value_len = value_len,
665 struct ext4_xattr_ibody_find is = {
666 .s = { .not_found = -ENODATA, },
667 @@ -1042,6 +1332,15 @@ ext4_xattr_set_handle(handle_t *handle,
670 error = ext4_xattr_block_set(handle, inode, &i, &bs);
671 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
672 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
673 + error == -ENOSPC) {
674 + /* xattr not fit to block, store at external
677 + error = ext4_xattr_ibody_set(handle, inode,
682 if (!is.s.not_found) {
683 @@ -1088,10 +1387,25 @@ ext4_xattr_set(struct inode *inode, int
684 const void *value, size_t value_len, int flags)
687 + struct super_block *sb = inode->i_sb;
688 + int buffer_credits;
689 int error, retries = 0;
691 + buffer_credits = EXT4_DATA_TRANS_BLOCKS(sb);
692 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
693 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
694 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
695 + sb->s_blocksize_bits;
697 + /* For new inode */
698 + buffer_credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
700 + /* For data blocks of EA inode */
701 + buffer_credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
705 - handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
706 + handle = ext4_journal_start(inode, buffer_credits);
707 if (IS_ERR(handle)) {
708 error = PTR_ERR(handle);
710 @@ -1101,7 +1415,7 @@ retry:
711 value, value_len, flags);
712 error2 = ext4_journal_stop(handle);
713 if (error == -ENOSPC &&
714 - ext4_should_retry_alloc(inode->i_sb, &retries))
715 + ext4_should_retry_alloc(sb, &retries))
719 @@ -1123,7 +1437,7 @@ static void ext4_xattr_shift_entries(str
721 /* Adjust the value offsets of the entries */
722 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
723 - if (!last->e_value_block && last->e_value_size) {
724 + if (!last->e_value_inum && last->e_value_size) {
725 new_offs = le16_to_cpu(last->e_value_offs) +
727 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
728 @@ -1358,22 +1672,135 @@ cleanup:
732 +#define EIA_INCR 16 /* must be 2^n */
733 +#define EIA_MASK (EIA_INCR - 1)
734 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
735 + * If @lea_ino_array is new or full it will be grown and the old
736 + * contents copied over.
739 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
741 + if (*lea_ino_array == NULL) {
743 + * Start with 15 inodes, so it fits into a powr-of-two size.
744 + * If *lea_ino_array is NULL, this is essentially offsetof()
747 + kmalloc(offsetof(struct ext4_xattr_ino_array,
748 + xia_inodes[EIA_MASK]),
750 + if (*lea_ino_array == NULL)
752 + (*lea_ino_array)->xia_count = 0;
753 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
754 + /* expand the array once all 15 + n * 16 slots are full */
755 + struct ext4_xattr_ino_array *new_array = NULL;
756 + int count = (*lea_ino_array)->xia_count;
758 + /* if new_array is NULL, this is essentially offsetof() */
759 + new_array = kmalloc(
760 + offsetof(struct ext4_xattr_ino_array,
761 + xia_inodes[count + EIA_INCR]),
763 + if (new_array == NULL)
765 + memcpy(new_array, *lea_ino_array,
766 + offsetof(struct ext4_xattr_ino_array,
767 + xia_inodes[count]));
768 + kfree(*lea_ino_array);
769 + *lea_ino_array = new_array;
771 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
776 + * Add xattr inode to orphan list
779 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
780 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
782 + struct inode *ea_inode = NULL;
783 + int idx = 0, error = 0;
785 + if (lea_ino_array == NULL)
788 + for (; idx < lea_ino_array->xia_count; ++idx) {
789 + if (!ext4_handle_has_enough_credits(handle, credits)) {
790 + error = ext4_journal_extend(handle, credits);
792 + error = ext4_journal_restart(handle, credits);
795 + ext4_warning(inode->i_sb,
796 + "couldn't extend journal "
797 + "(err %d)", error);
801 + ea_inode = ext4_xattr_inode_iget(inode,
802 + lea_ino_array->xia_inodes[idx], &error);
805 + ext4_orphan_add(handle, ea_inode);
806 + /* the inode's i_count will be released by caller */
813 * ext4_xattr_delete_inode()
815 - * Free extended attribute resources associated with this inode. This
816 + * Free extended attribute resources associated with this inode. Traverse
817 + * all entries and unlink any xattr inodes associated with this inode. This
818 * is called immediately before an inode is freed. We have exclusive
819 - * access to the inode.
820 + * access to the inode. If an orphan inode is deleted it will also delete any
821 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
822 + * to ensure they belong to the parent inode and were not deleted already.
825 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
827 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
828 + struct ext4_xattr_ino_array **lea_ino_array)
830 struct buffer_head *bh = NULL;
831 + struct ext4_xattr_ibody_header *header;
832 + struct ext4_inode *raw_inode;
833 + struct ext4_iloc iloc;
834 + struct ext4_xattr_entry *entry;
835 + int credits = 3, error = 0;
837 - if (!EXT4_I(inode)->i_file_acl)
838 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
839 + goto delete_external_ea;
841 + error = ext4_get_inode_loc(inode, &iloc);
844 + raw_inode = ext4_raw_inode(&iloc);
845 + header = IHDR(inode, raw_inode);
846 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
847 + entry = EXT4_XATTR_NEXT(entry)) {
848 + if (!entry->e_value_inum)
850 + if (ext4_expand_ino_array(lea_ino_array,
851 + entry->e_value_inum) != 0) {
855 + entry->e_value_inum = 0;
860 + if (!EXT4_I(inode)->i_file_acl) {
861 + /* add xattr inode to orphan list */
862 + ext4_xattr_inode_orphan_add(handle, inode, credits,
866 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
868 EXT4_ERROR_INODE(inode, "block %llu read error",
869 @@ -1386,11 +1813,69 @@ ext4_xattr_delete_inode(handle_t *handle
870 EXT4_I(inode)->i_file_acl);
874 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
875 + entry = EXT4_XATTR_NEXT(entry)) {
876 + if (!entry->e_value_inum)
878 + if (ext4_expand_ino_array(lea_ino_array,
879 + entry->e_value_inum) != 0)
881 + entry->e_value_inum = 0;
884 + /* adding xattr inode to orphan list */
885 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
890 + if (!IS_NOQUOTA(inode))
891 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
893 + if (!ext4_handle_has_enough_credits(handle, credits)) {
894 + error = ext4_journal_extend(handle, credits);
896 + error = ext4_journal_restart(handle, credits);
898 + ext4_warning(inode->i_sb,
899 + "couldn't extend journal (err %d)", error);
904 ext4_xattr_release_block(handle, inode, bh);
905 EXT4_I(inode)->i_file_acl = 0;
914 +ext4_xattr_inode_array_free(struct inode *inode,
915 + struct ext4_xattr_ino_array *lea_ino_array)
917 + struct inode *ea_inode = NULL;
921 + if (lea_ino_array == NULL)
924 + for (; idx < lea_ino_array->xia_count; ++idx) {
925 + ea_inode = ext4_xattr_inode_iget(inode,
926 + lea_ino_array->xia_inodes[idx], &err);
929 + /* for inode's i_count get from ext4_xattr_delete_inode */
930 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
932 + ea_inode->i_nlink = 0;
935 + kfree(lea_ino_array);
939 @@ -1460,10 +1945,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
940 entry1->e_name_index != entry2->e_name_index ||
941 entry1->e_name_len != entry2->e_name_len ||
942 entry1->e_value_size != entry2->e_value_size ||
943 + entry1->e_value_inum != entry2->e_value_inum ||
944 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
946 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
948 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
949 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
950 le32_to_cpu(entry1->e_value_size)))
951 @@ -1547,7 +2031,7 @@ static inline void ext4_xattr_hash_entry
955 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
956 + if (!entry->e_value_inum && entry->e_value_size) {
957 __le32 *value = (__le32 *)((char *)header +
958 le16_to_cpu(entry->e_value_offs));
959 for (n = (le32_to_cpu(entry->e_value_size) +
960 Index: linux-stage/fs/ext4/xattr.h
961 ===================================================================
962 --- linux-stage.orig/fs/ext4/xattr.h
963 +++ linux-stage/fs/ext4/xattr.h
964 @@ -38,7 +38,7 @@ struct ext4_xattr_entry {
965 __u8 e_name_len; /* length of name */
966 __u8 e_name_index; /* attribute name index */
967 __le16 e_value_offs; /* offset in disk block of value */
968 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
969 + __le32 e_value_inum; /* inode in which the value is stored */
970 __le32 e_value_size; /* size of attribute value */
971 __le32 e_hash; /* hash value of name and value */
972 char e_name[0]; /* attribute name */
973 @@ -63,6 +63,26 @@ struct ext4_xattr_entry {
974 EXT4_I(inode)->i_extra_isize))
975 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
978 + * Link EA inode back to parent one using i_mtime field.
979 + * Extra integer type conversion added to ignore higher
980 + * bits in i_mtime.tv_sec which might be set by ext4_get()
982 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
984 + (inode)->i_mtime.tv_sec = inum; \
987 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
988 +((__u32)(inode)->i_mtime.tv_sec)
991 + * The minimum size of EA value when you start storing it in an external inode
992 + * size of block - size of header - size of 1 entry - 4 null bytes
994 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
995 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
997 # ifdef CONFIG_EXT4_FS_XATTR
999 extern const struct xattr_handler ext4_xattr_user_handler;
1000 @@ -77,7 +86,13 @@ extern int ext4_xattr_get(struct inode *
1001 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1002 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1004 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1005 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1007 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1008 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1009 + struct ext4_xattr_ino_array **array);
1010 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1011 + struct ext4_xattr_ino_array *array);
1012 extern void ext4_xattr_put_super(struct super_block *);
1014 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1015 @@ -111,9 +126,11 @@ ext4_xattr_set_handle(handle_t *handle,
1020 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1022 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1023 + struct ext4_xattr_ino_array *array)
1025 + return -EOPNOTSUPP;
1029 Index: linux-stage/fs/ext4/ialloc.c
1030 ===================================================================
1031 --- linux-stage.orig/fs/ext4/ialloc.c
1032 +++ linux-stage/fs/ext4/ialloc.c
1033 @@ -221,7 +221,6 @@ void ext4_free_inode(handle_t *handle, s
1034 * as writing the quota to disk may need the lock as well.
1036 dquot_initialize(inode);
1037 - ext4_xattr_delete_inode(handle, inode);
1038 dquot_free_inode(inode);