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 @@ -1579,6 +1579,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| \
18 EXT4_FEATURE_INCOMPAT_INLINE_DATA)
19 @@ -1979,6 +1980,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 @@ -1990,6 +1997,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);
42 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
43 @@ -2194,6 +2205,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_zero_partial_blocks(handle_t *handle, struct inode *inode,
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 @@ -134,8 +134,6 @@ static void ext4_invalidatepage(struct p
57 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
58 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
59 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
63 * Test whether an inode is a fast symlink.
64 @@ -184,6 +182,8 @@ void ext4_evict_inode(struct inode *inod
68 + int extra_credits = 3;
69 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
71 trace_ext4_evict_inode(inode);
73 @@ -236,8 +236,8 @@ void ext4_evict_inode(struct inode *inod
74 * protection against it
76 sb_start_intwrite(inode->i_sb);
77 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
78 - ext4_blocks_for_truncate(inode)+3);
80 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
82 ext4_std_error(inode->i_sb, PTR_ERR(handle));
84 @@ -249,9 +249,36 @@ void ext4_evict_inode(struct inode *inod
85 sb_end_intwrite(inode->i_sb);
90 ext4_handle_sync(handle);
93 + * Delete xattr inode before deleting the main inode.
95 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
97 + ext4_warning(inode->i_sb,
98 + "couldn't delete inode's xattr (err %d)", err);
102 + if (!IS_NOQUOTA(inode))
103 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
105 + if (!ext4_handle_has_enough_credits(handle,
106 + ext4_blocks_for_truncate(inode) + extra_credits)) {
107 + err = ext4_journal_extend(handle,
108 + ext4_blocks_for_truncate(inode) + extra_credits);
110 + err = ext4_journal_restart(handle,
111 + ext4_blocks_for_truncate(inode) + extra_credits);
113 + ext4_warning(inode->i_sb,
114 + "couldn't extend journal (err %d)", err);
120 err = ext4_mark_inode_dirty(handle, inode);
122 @@ -306,8 +333,12 @@ void ext4_evict_inode(struct inode *inod
123 ext4_clear_inode(inode);
125 ext4_free_inode(handle, inode);
127 ext4_journal_stop(handle);
128 sb_end_intwrite(inode->i_sb);
130 + if (lea_ino_array != NULL)
131 + ext4_xattr_inode_array_free(inode, lea_ino_array);
134 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
135 @@ -4681,7 +4712,7 @@ static int ext4_index_trans_blocks(struc
137 * Also account for superblock, inode, quota and xattr blocks
139 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
140 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
143 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
144 Index: linux-stage/fs/ext4/xattr.c
145 ===================================================================
146 --- linux-stage.orig/fs/ext4/xattr.c
147 +++ linux-stage/fs/ext4/xattr.c
148 @@ -201,6 +201,7 @@ ext4_xattr_check_names(struct ext4_xattr
150 while (!IS_LAST_ENTRY(entry)) {
151 if (entry->e_value_size != 0 &&
152 + entry->e_value_inum == 0 &&
153 (value_start + le16_to_cpu(entry->e_value_offs) <
154 (void *)e + sizeof(__u32) ||
155 value_start + le16_to_cpu(entry->e_value_offs) +
156 @@ -233,19 +234,26 @@ ext4_xattr_check_block(struct inode *ino
160 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
161 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
162 + struct inode *inode)
164 size_t value_size = le32_to_cpu(entry->e_value_size);
166 - if (entry->e_value_block != 0 || value_size > size ||
167 - le16_to_cpu(entry->e_value_offs) + value_size > size)
168 + if ((entry->e_value_inum == 0) &&
169 + (le16_to_cpu(entry->e_value_offs) + value_size > size))
171 + if (entry->e_value_inum != 0 &&
172 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
173 + le32_to_cpu(entry->e_value_inum) >
174 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
180 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
181 - const char *name, size_t size, int sorted)
182 + const char *name, size_t size, int sorted,
183 + struct inode *inode)
185 struct ext4_xattr_entry *entry;
187 @@ -265,11 +273,103 @@ ext4_xattr_find_entry(struct ext4_xattr_
191 - if (!cmp && ext4_xattr_check_entry(entry, size))
192 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
194 return cmp ? -ENODATA : 0;
198 + * Read the EA value from an inode.
201 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
203 + unsigned long block = 0;
204 + struct buffer_head *bh = NULL;
205 + int err, blocksize;
206 + size_t csize, ret_size = 0;
211 + blocksize = ea_inode->i_sb->s_blocksize;
213 + while (ret_size < *size) {
214 + csize = (*size - ret_size) > blocksize ? blocksize :
216 + bh = ext4_bread(NULL, ea_inode, block, 0, &err);
221 + memcpy(buf, bh->b_data, csize);
234 +struct inode *ext4_xattr_inode_iget(struct inode *parent, int ea_ino, int *err)
236 + struct inode *ea_inode = NULL;
238 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
239 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
240 + ext4_error(parent->i_sb, "error while reading EA inode %d",
246 + if (ea_inode->i_xattr_inode_parent != parent->i_ino ||
247 + ea_inode->i_generation != parent->i_generation) {
248 + ext4_error(parent->i_sb, "Backpointer from EA inode %d "
249 + "to parent invalid.", ea_ino);
254 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
255 + ext4_error(parent->i_sb, "EA inode %d does not have "
256 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
270 + * Read the value from the EA inode.
273 +ext4_xattr_inode_get(struct inode *inode, int ea_ino, void *buffer,
276 + struct inode *ea_inode = NULL;
279 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
283 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
290 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
291 void *buffer, size_t buffer_size)
292 @@ -301,7 +401,8 @@ bad_block:
294 ext4_xattr_cache_insert(bh);
296 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
297 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
302 @@ -311,8 +412,16 @@ bad_block:
304 if (size > buffer_size)
306 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
308 + if (entry->e_value_inum != 0) {
309 + error = ext4_xattr_inode_get(inode,
310 + le32_to_cpu(entry->e_value_inum),
315 + memcpy(buffer, bh->b_data +
316 + le16_to_cpu(entry->e_value_offs), size);
321 @@ -346,7 +455,7 @@ ext4_xattr_ibody_get(struct inode *inode
324 error = ext4_xattr_find_entry(&entry, name_index, name,
325 - end - (void *)entry, 0);
326 + end - (void *)entry, 0, inode);
329 size = le32_to_cpu(entry->e_value_size);
330 @@ -354,8 +463,16 @@ ext4_xattr_ibody_get(struct inode *inode
332 if (size > buffer_size)
334 - memcpy(buffer, (void *)IFIRST(header) +
335 - le16_to_cpu(entry->e_value_offs), size);
336 + if (entry->e_value_inum != 0) {
337 + error = ext4_xattr_inode_get(inode,
338 + le32_to_cpu(entry->e_value_inum),
343 + memcpy(buffer, (void *)IFIRST(header) +
344 + le16_to_cpu(entry->e_value_offs), size);
349 @@ -600,7 +717,7 @@ static size_t ext4_xattr_free_space(stru
350 size_t *min_offs, void *base, int *total)
352 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
353 - if (!last->e_value_block && last->e_value_size) {
354 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
355 size_t offs = le16_to_cpu(last->e_value_offs);
356 if (offs < *min_offs)
358 @@ -611,16 +728,174 @@ static size_t ext4_xattr_free_space(stru
359 return (*min_offs - ((void *)last - base) - sizeof(__u32));
363 + * Write the value of the EA in an inode.
366 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
367 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
368 + const void *buf, int bufsize)
370 + struct buffer_head *bh = NULL;
371 + unsigned long block = 0;
372 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
373 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
374 + int csize, wsize = 0;
379 + while (ret >= 0 && ret < max_blocks) {
380 + struct ext4_map_blocks map;
381 + map.m_lblk = block += ret;
382 + map.m_len = max_blocks -= ret;
384 + ret = ext4_map_blocks(handle, ea_inode, &map,
385 + EXT4_GET_BLOCKS_CREATE);
387 + ext4_mark_inode_dirty(handle, ea_inode);
388 + if (ret == -ENOSPC &&
389 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
401 + while (wsize < bufsize) {
404 + csize = (bufsize - wsize) > blocksize ? blocksize :
406 + bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
409 + ret = ext4_journal_get_write_access(handle, bh);
413 + memcpy(bh->b_data, buf, csize);
414 + set_buffer_uptodate(bh);
415 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
422 + mutex_lock(&ea_inode->i_mutex);
423 + i_size_write(ea_inode, wsize);
424 + ext4_update_i_disksize(ea_inode, wsize);
425 + mutex_unlock(&ea_inode->i_mutex);
427 + ext4_mark_inode_dirty(handle, ea_inode);
436 + * Create an inode to store the value of a large EA.
438 +static struct inode *
439 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
441 + struct inode *ea_inode = NULL;
444 + * Let the next inode be the goal, so we try and allocate the EA inode
445 + * in the same group, or nearby one.
447 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
448 + S_IFREG|0600, NULL, inode->i_ino + 1, NULL);
450 + if (!IS_ERR(ea_inode)) {
451 + ea_inode->i_op = &ext4_file_inode_operations;
452 + ea_inode->i_fop = &ext4_file_operations;
453 + ext4_set_aops(ea_inode);
454 + ea_inode->i_generation = inode->i_generation;
455 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
458 + * A back-pointer from EA inode to parent inode will be useful
461 + ea_inode->i_xattr_inode_parent = inode->i_ino;
462 + unlock_new_inode(ea_inode);
469 + * Unlink the inode storing the value of the EA.
472 +ext4_xattr_inode_unlink(struct inode *inode, int ea_ino)
474 + struct inode *ea_inode = NULL;
477 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
481 + clear_nlink(ea_inode);
488 + * Add value of the EA in an inode.
491 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, int *ea_ino,
492 + const void *value, size_t value_len)
494 + struct inode *ea_inode = NULL;
497 + /* Create an inode for the EA value */
498 + ea_inode = ext4_xattr_inode_create(handle, inode);
499 + if (IS_ERR(ea_inode))
502 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
504 + clear_nlink(ea_inode);
506 + *ea_ino = ea_inode->i_ino;
514 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
515 + handle_t *handle, struct inode *inode)
517 struct ext4_xattr_entry *last;
518 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
519 + int in_inode = i->in_inode;
521 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
522 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
523 + (EXT4_XATTR_SIZE(i->value_len) >
524 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
527 /* Compute min_offs and last. */
529 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
530 - if (!last->e_value_block && last->e_value_size) {
531 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
532 size_t offs = le16_to_cpu(last->e_value_offs);
535 @@ -628,16 +903,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
537 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
539 - if (!s->here->e_value_block && s->here->e_value_size) {
540 + if (!in_inode && s->here->e_value_inum == 0 &&
541 + s->here->e_value_size > 0) {
542 size_t size = le32_to_cpu(s->here->e_value_size);
543 free += EXT4_XATTR_SIZE(size);
545 free += EXT4_XATTR_LEN(name_len);
548 - if (free < EXT4_XATTR_SIZE(i->value_len) ||
549 - free < EXT4_XATTR_LEN(name_len) +
550 - EXT4_XATTR_SIZE(i->value_len))
551 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
556 + if (free < value_len ||
557 + free < EXT4_XATTR_LEN(name_len) + value_len)
561 @@ -651,7 +931,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
562 s->here->e_name_len = name_len;
563 memcpy(s->here->e_name, i->name, name_len);
565 - if (!s->here->e_value_block && s->here->e_value_size) {
566 + if (s->here->e_value_offs > 0 && s->here->e_value_inum == 0 &&
567 + s->here->e_value_size > 0) {
568 void *first_val = s->base + min_offs;
569 size_t offs = le16_to_cpu(s->here->e_value_offs);
570 void *val = s->base + offs;
571 @@ -685,13 +966,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
573 while (!IS_LAST_ENTRY(last)) {
574 size_t o = le16_to_cpu(last->e_value_offs);
575 - if (!last->e_value_block &&
576 + if (!last->e_value_inum &&
577 last->e_value_size && o < offs)
579 cpu_to_le16(o + size);
580 last = EXT4_XATTR_NEXT(last);
583 + if (s->here->e_value_inum != 0) {
584 + ext4_xattr_inode_unlink(inode,
585 + le32_to_cpu(s->here->e_value_inum));
586 + s->here->e_value_inum = 0;
589 /* Remove the old name. */
590 size_t size = EXT4_XATTR_LEN(name_len);
591 @@ -705,10 +990,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
593 /* Insert the new value. */
594 s->here->e_value_size = cpu_to_le32(i->value_len);
595 - if (i->value_len) {
597 + int ea_ino = le32_to_cpu(s->here->e_value_inum);
598 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
600 + s->here->e_value_inum = cpu_to_le32(ea_ino);
601 + s->here->e_value_offs = 0;
602 + } else if (i->value_len) {
603 size_t size = EXT4_XATTR_SIZE(i->value_len);
604 void *val = s->base + min_offs - size;
605 s->here->e_value_offs = cpu_to_le16(min_offs - size);
606 + s->here->e_value_inum = 0;
607 if (i->value == EXT4_ZERO_XATTR_VALUE) {
608 memset(val, 0, size);
610 @@ -758,7 +1050,7 @@ ext4_xattr_block_find(struct inode *inod
611 bs->s.end = bs->bh->b_data + bs->bh->b_size;
612 bs->s.here = bs->s.first;
613 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
614 - i->name, bs->bh->b_size, 1);
615 + i->name, bs->bh->b_size, 1, inode);
616 if (error && error != -ENODATA)
618 bs->s.not_found = error;
619 @@ -782,8 +1074,6 @@ ext4_xattr_block_set(handle_t *handle, s
621 #define header(x) ((struct ext4_xattr_header *)(x))
623 - if (i->value && i->value_len > sb->s_blocksize)
626 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
628 @@ -799,7 +1089,7 @@ ext4_xattr_block_set(handle_t *handle, s
631 ea_bdebug(bs->bh, "modifying in-place");
632 - error = ext4_xattr_set_entry(i, s);
633 + error = ext4_xattr_set_entry(i, s, handle, inode);
635 if (!IS_LAST_ENTRY(s->first))
636 ext4_xattr_rehash(header(s->base),
637 @@ -850,7 +1140,7 @@ ext4_xattr_block_set(handle_t *handle, s
638 s->end = s->base + sb->s_blocksize;
641 - error = ext4_xattr_set_entry(i, s);
642 + error = ext4_xattr_set_entry(i, s, handle, inode);
646 @@ -1000,7 +1290,7 @@ int ext4_xattr_ibody_find(struct inode *
647 /* Find the named attribute. */
648 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
650 - (void *)is->s.base, 0);
651 + (void *)is->s.base, 0, inode);
652 if (error && error != -ENODATA)
654 is->s.not_found = error;
655 @@ -1018,7 +1308,7 @@ int ext4_xattr_ibody_inline_set(handle_t
657 if (EXT4_I(inode)->i_extra_isize == 0)
659 - error = ext4_xattr_set_entry(i, s);
660 + error = ext4_xattr_set_entry(i, s, handle, inode);
662 if (error == -ENOSPC &&
663 ext4_has_inline_data(inode)) {
664 @@ -1030,7 +1320,7 @@ int ext4_xattr_ibody_inline_set(handle_t
665 error = ext4_xattr_ibody_find(inode, i, is);
668 - error = ext4_xattr_set_entry(i, s);
669 + error = ext4_xattr_set_entry(i, s, handle, inode);
673 @@ -1056,7 +1346,7 @@ static int ext4_xattr_ibody_set(handle_t
675 if (EXT4_I(inode)->i_extra_isize == 0)
677 - error = ext4_xattr_set_entry(i, s);
678 + error = ext4_xattr_set_entry(i, s, handle, inode);
681 header = IHDR(inode, ext4_raw_inode(&is->iloc));
682 @@ -1092,7 +1382,7 @@ ext4_xattr_set_handle(handle_t *handle,
685 .value_len = value_len,
689 struct ext4_xattr_ibody_find is = {
690 .s = { .not_found = -ENODATA, },
691 @@ -1157,6 +1447,15 @@ ext4_xattr_set_handle(handle_t *handle,
694 error = ext4_xattr_block_set(handle, inode, &i, &bs);
695 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
696 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
697 + error == -ENOSPC) {
698 + /* xattr not fit to block, store at external
701 + error = ext4_xattr_ibody_set(handle, inode,
706 if (!is.s.not_found) {
707 @@ -1203,9 +1502,22 @@ ext4_xattr_set(struct inode *inode, int
708 const void *value, size_t value_len, int flags)
711 + struct super_block *sb = inode->i_sb;
712 int error, retries = 0;
713 int credits = ext4_jbd2_credits_xattr(inode);
715 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
716 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
717 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
718 + sb->s_blocksize_bits;
720 + /* For new inode */
721 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
723 + /* For data blocks of EA inode */
724 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
728 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
729 if (IS_ERR(handle)) {
730 @@ -1217,7 +1529,7 @@ retry:
731 value, value_len, flags);
732 error2 = ext4_journal_stop(handle);
733 if (error == -ENOSPC &&
734 - ext4_should_retry_alloc(inode->i_sb, &retries))
735 + ext4_should_retry_alloc(sb, &retries))
739 @@ -1239,7 +1551,7 @@ static void ext4_xattr_shift_entries(str
741 /* Adjust the value offsets of the entries */
742 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
743 - if (!last->e_value_block && last->e_value_size) {
744 + if (last->e_value_inum == 0 && last->e_value_size > 0) {
745 new_offs = le16_to_cpu(last->e_value_offs) +
747 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
748 @@ -1477,21 +1789,135 @@ cleanup:
752 +#define EIA_INCR 16 /* must be 2^n */
753 +#define EIA_MASK (EIA_INCR - 1)
754 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
755 + * If @lea_ino_array is new or full it will be grown and the old
756 + * contents copied over.
759 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
761 + if (*lea_ino_array == NULL) {
763 + * Start with 15 inodes, so it fits into a power-of-two size.
764 + * If *lea_ino_array is NULL, this is essentially offsetof()
767 + kmalloc(offsetof(struct ext4_xattr_ino_array,
768 + xia_inodes[EIA_MASK]),
770 + if (*lea_ino_array == NULL)
772 + (*lea_ino_array)->xia_count = 0;
773 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
774 + /* expand the array once all 15 + n * 16 slots are full */
775 + struct ext4_xattr_ino_array *new_array = NULL;
776 + int count = (*lea_ino_array)->xia_count;
778 + /* if new_array is NULL, this is essentially offsetof() */
779 + new_array = kmalloc(
780 + offsetof(struct ext4_xattr_ino_array,
781 + xia_inodes[count + EIA_INCR]),
783 + if (new_array == NULL)
785 + memcpy(new_array, *lea_ino_array,
786 + offsetof(struct ext4_xattr_ino_array,
787 + xia_inodes[count]));
788 + kfree(*lea_ino_array);
789 + *lea_ino_array = new_array;
791 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
796 + * Add xattr inode to orphan list
799 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
800 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
802 + struct inode *ea_inode = NULL;
803 + int idx = 0, error = 0;
805 + if (lea_ino_array == NULL)
808 + for (; idx < lea_ino_array->xia_count; ++idx) {
809 + if (!ext4_handle_has_enough_credits(handle, credits)) {
810 + error = ext4_journal_extend(handle, credits);
812 + error = ext4_journal_restart(handle, credits);
815 + ext4_warning(inode->i_sb,
816 + "couldn't extend journal "
817 + "(err %d)", error);
821 + ea_inode = ext4_xattr_inode_iget(inode,
822 + lea_ino_array->xia_inodes[idx], &error);
825 + ext4_orphan_add(handle, ea_inode);
826 + /* the inode's i_count will be released by caller */
833 * ext4_xattr_delete_inode()
835 - * Free extended attribute resources associated with this inode. This
836 + * Free extended attribute resources associated with this inode. Traverse
837 + * all entries and unlink any xattr inodes associated with this inode. This
838 * is called immediately before an inode is freed. We have exclusive
839 - * access to the inode.
840 + * access to the inode. If an orphan inode is deleted it will also delete any
841 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
842 + * to ensure they belong to the parent inode and were not deleted already.
845 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
847 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
848 + struct ext4_xattr_ino_array **lea_ino_array)
850 struct buffer_head *bh = NULL;
851 + struct ext4_xattr_ibody_header *header;
852 + struct ext4_inode *raw_inode;
853 + struct ext4_iloc iloc;
854 + struct ext4_xattr_entry *entry;
855 + int credits = 3, error = 0;
857 - if (!EXT4_I(inode)->i_file_acl)
858 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
859 + goto delete_external_ea;
861 + error = ext4_get_inode_loc(inode, &iloc);
864 + raw_inode = ext4_raw_inode(&iloc);
865 + header = IHDR(inode, raw_inode);
866 + entry = IFIRST(header);
867 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
868 + if (entry->e_value_inum == 0)
870 + if (ext4_expand_ino_array(lea_ino_array,
871 + entry->e_value_inum) != 0) {
875 + entry->e_value_inum = 0;
880 + if (!EXT4_I(inode)->i_file_acl) {
881 + /* add xattr inode to orphan list */
882 + ext4_xattr_inode_orphan_add(handle, inode, credits,
886 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
888 EXT4_ERROR_INODE(inode, "block %llu read error",
889 @@ -1504,11 +1930,69 @@ ext4_xattr_delete_inode(handle_t *handle
890 EXT4_I(inode)->i_file_acl);
894 + entry = BFIRST(bh);
895 + for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
896 + if (entry->e_value_inum == 0)
898 + if (ext4_expand_ino_array(lea_ino_array,
899 + entry->e_value_inum) != 0)
901 + entry->e_value_inum = 0;
904 + /* add xattr inode to orphan list */
905 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
910 + if (!IS_NOQUOTA(inode))
911 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
913 + if (!ext4_handle_has_enough_credits(handle, credits)) {
914 + error = ext4_journal_extend(handle, credits);
916 + error = ext4_journal_restart(handle, credits);
918 + ext4_warning(inode->i_sb,
919 + "couldn't extend journal (err %d)", error);
924 ext4_xattr_release_block(handle, inode, bh);
925 EXT4_I(inode)->i_file_acl = 0;
934 +ext4_xattr_inode_array_free(struct inode *inode,
935 + struct ext4_xattr_ino_array *lea_ino_array)
937 + struct inode *ea_inode = NULL;
941 + if (lea_ino_array == NULL)
944 + for (; idx < lea_ino_array->xia_count; ++idx) {
945 + ea_inode = ext4_xattr_inode_iget(inode,
946 + lea_ino_array->xia_inodes[idx], &err);
949 + /* for inode's i_count get from ext4_xattr_delete_inode */
950 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
952 + clear_nlink(ea_inode);
955 + kfree(lea_ino_array);
959 @@ -1578,10 +2062,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
960 entry1->e_name_index != entry2->e_name_index ||
961 entry1->e_name_len != entry2->e_name_len ||
962 entry1->e_value_size != entry2->e_value_size ||
963 + entry1->e_value_inum != entry2->e_value_inum ||
964 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
966 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
968 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
969 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
970 le32_to_cpu(entry1->e_value_size)))
971 @@ -1665,7 +2148,7 @@ static inline void ext4_xattr_hash_entry
975 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
976 + if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
977 __le32 *value = (__le32 *)((char *)header +
978 le16_to_cpu(entry->e_value_offs));
979 for (n = (le32_to_cpu(entry->e_value_size) +
980 Index: linux-stage/fs/ext4/xattr.h
981 ===================================================================
982 --- linux-stage.orig/fs/ext4/xattr.h
983 +++ linux-stage/fs/ext4/xattr.h
984 @@ -42,7 +42,7 @@ struct ext4_xattr_entry {
985 __u8 e_name_len; /* length of name */
986 __u8 e_name_index; /* attribute name index */
987 __le16 e_value_offs; /* offset in disk block of value */
988 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
989 + __le32 e_value_inum; /* inode in which the value is stored */
990 __le32 e_value_size; /* size of attribute value */
991 __le32 e_hash; /* hash value of name and value */
992 char e_name[0]; /* attribute name */
993 @@ -67,6 +67,15 @@ struct ext4_xattr_entry {
994 EXT4_I(inode)->i_extra_isize))
995 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
997 +#define i_xattr_inode_parent i_mtime.tv_sec
1000 + * The minimum size of EA value when you start storing it in an external inode
1001 + * size of block - size of header - size of 1 entry - 4 null bytes
1003 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1004 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1006 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1007 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1008 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1009 @@ -75,10 +84,11 @@ struct ext4_xattr_entry {
1010 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1012 struct ext4_xattr_info {
1021 struct ext4_xattr_search {
1022 @@ -106,7 +116,13 @@ extern int ext4_xattr_get(struct inode *
1023 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1024 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1026 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1027 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, int ea_ino,
1029 +extern int ext4_xattr_inode_unlink(struct inode *inode, int ea_ino);
1030 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1031 + struct ext4_xattr_ino_array **array);
1032 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1033 + struct ext4_xattr_ino_array *array);
1034 extern void ext4_xattr_put_super(struct super_block *);
1036 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1037 Index: linux-stage/fs/ext4/ialloc.c
1038 ===================================================================
1039 --- linux-stage.orig/fs/ext4/ialloc.c
1040 +++ linux-stage/fs/ext4/ialloc.c
1041 @@ -269,7 +269,6 @@ void ext4_free_inode(handle_t *handle, s
1042 * as writing the quota to disk may need the lock as well.
1044 dquot_initialize(inode);
1045 - ext4_xattr_delete_inode(handle, inode);
1046 dquot_free_inode(inode);
1049 Index: linux-stage/fs/ext4/inline.c
1050 ===================================================================
1051 --- linux-stage.orig/fs/ext4/inline.c
1052 +++ linux-stage/fs/ext4/inline.c
1053 @@ -59,7 +59,7 @@ static int get_max_inline_xattr_value_si
1055 /* Compute min_offs. */
1056 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
1057 - if (!entry->e_value_block && entry->e_value_size) {
1058 + if (!entry->e_value_inum && entry->e_value_size) {
1059 size_t offs = le16_to_cpu(entry->e_value_offs);
1060 if (offs < min_offs)