1 diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
2 index 10a2a86..217fdcc 100644
5 @@ -1781,6 +1781,7 @@ EXT4_FEATURE_INCOMPAT_FUNCS(encrypt, ENCRYPT)
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| \
12 EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
13 @@ -2252,6 +2259,10 @@ struct mmpd_data {
14 # define ATTRIB_NORET __attribute__((noreturn))
15 # define NORET_AND noreturn,
17 +struct ext4_xattr_ino_array {
18 + unsigned int xia_count; /* # of used item in the array */
19 + unsigned int xia_inodes[0];
22 extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
23 void ext4_inode_bitmap_csum_set(struct super_block *sb, ext4_group_t group,
24 @@ -2582,6 +2593,7 @@ extern void ext4_set_inode_flags(struct inode *);
25 extern void ext4_get_inode_flags(struct ext4_inode_info *);
26 extern int ext4_alloc_da_blocks(struct inode *inode);
27 extern void ext4_set_aops(struct inode *inode);
28 +extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int chunk);
29 extern int ext4_writepage_trans_blocks(struct inode *);
30 extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
31 extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
32 Index: linux-stage/fs/ext4/extents.c
33 ===================================================================
34 --- linux-stage.orig/fs/ext4/extents.c
35 +++ linux-stage/fs/ext4/extents.c
36 @@ -2461,7 +2461,8 @@ int ext4_ext_index_trans_blocks(struct i
38 static inline int get_default_free_blocks_flags(struct inode *inode)
40 - if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
41 + if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
42 + (EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
43 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
44 else if (ext4_should_journal_data(inode))
45 return EXT4_FREE_BLOCKS_FORGET;
46 diff --git a/fs/ext4/ialloc.c b/fs/ext4/ialloc.c
47 index 0cccda3..43ca376 100644
48 --- a/fs/ext4/ialloc.c
49 +++ b/fs/ext4/ialloc.c
50 @@ -293,7 +293,6 @@ void ext4_free_inode(handle_t *handle, struct inode *inode)
51 * as writing the quota to disk may need the lock as well.
53 dquot_initialize(inode);
54 - ext4_xattr_delete_inode(handle, inode);
55 dquot_free_inode(inode);
58 Index: linux-stage/fs/ext4/indirect.c
59 ===================================================================
60 --- linux-stage.orig/fs/ext4/indirect.c
61 +++ linux-stage/fs/ext4/indirect.c
62 @@ -959,7 +959,8 @@ static int ext4_clear_blocks(handle_t *h
63 int flags = EXT4_FREE_BLOCKS_VALIDATED;
66 - if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
67 + if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) ||
68 + (EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL))
69 flags |= EXT4_FREE_BLOCKS_FORGET | EXT4_FREE_BLOCKS_METADATA;
70 else if (ext4_should_journal_data(inode))
71 flags |= EXT4_FREE_BLOCKS_FORGET;
72 diff --git a/fs/ext4/inline.c b/fs/ext4/inline.c
73 index 53e8a88..5112c5a 100644
74 --- a/fs/ext4/inline.c
75 +++ b/fs/ext4/inline.c
76 @@ -61,7 +61,7 @@ static int get_max_inline_xattr_value_size(struct inode *inode,
78 /* Compute min_offs. */
79 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
80 - if (!entry->e_value_block && entry->e_value_size) {
81 + if (!entry->e_value_inum && entry->e_value_size) {
82 size_t offs = le16_to_cpu(entry->e_value_offs);
85 diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
86 index 04c5f63..3034ceb 100644
89 @@ -139,8 +139,6 @@ static void ext4_invalidatepage(struct page *page, unsigned int offset,
91 static int __ext4_journalled_writepage(struct page *page, unsigned int len);
92 static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
93 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
97 * Test whether an inode is a fast symlink.
98 @@ -189,6 +187,8 @@ void ext4_evict_inode(struct inode *inode)
102 + int extra_credits = 3;
103 + struct ext4_xattr_ino_array *lea_ino_array = NULL;
105 trace_ext4_evict_inode(inode);
107 @@ -241,8 +241,8 @@ void ext4_evict_inode(struct inode *inode)
108 * protection against it
110 sb_start_intwrite(inode->i_sb);
111 - handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
112 - ext4_blocks_for_truncate(inode)+3);
114 + handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
115 if (IS_ERR(handle)) {
116 ext4_std_error(inode->i_sb, PTR_ERR(handle));
118 @@ -254,9 +254,36 @@ void ext4_evict_inode(struct inode *inode)
119 sb_end_intwrite(inode->i_sb);
124 ext4_handle_sync(handle);
127 + * Delete xattr inode before deleting the main inode.
129 + err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
131 + ext4_warning(inode->i_sb,
132 + "couldn't delete inode's xattr (err %d)", err);
136 + if (!IS_NOQUOTA(inode))
137 + extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
139 + if (!ext4_handle_has_enough_credits(handle,
140 + ext4_blocks_for_truncate(inode) + extra_credits)) {
141 + err = ext4_journal_extend(handle,
142 + ext4_blocks_for_truncate(inode) + extra_credits);
144 + err = ext4_journal_restart(handle,
145 + ext4_blocks_for_truncate(inode) + extra_credits);
147 + ext4_warning(inode->i_sb,
148 + "couldn't extend journal (err %d)", err);
154 err = ext4_mark_inode_dirty(handle, inode);
156 @@ -273,10 +300,10 @@ void ext4_evict_inode(struct inode *inode)
157 * enough credits left in the handle to remove the inode from
158 * the orphan list and set the dtime field.
160 - if (!ext4_handle_has_enough_credits(handle, 3)) {
161 - err = ext4_journal_extend(handle, 3);
162 + if (!ext4_handle_has_enough_credits(handle, extra_credits)) {
163 + err = ext4_journal_extend(handle, extra_credits);
165 - err = ext4_journal_restart(handle, 3);
166 + err = ext4_journal_restart(handle, extra_credits);
168 ext4_warning(inode->i_sb,
169 "couldn't extend journal (err %d)", err);
170 @@ -311,8 +338,12 @@ void ext4_evict_inode(struct inode *inode)
171 ext4_clear_inode(inode);
173 ext4_free_inode(handle, inode);
175 ext4_journal_stop(handle);
176 sb_end_intwrite(inode->i_sb);
178 + if (lea_ino_array != NULL)
179 + ext4_xattr_inode_array_free(inode, lea_ino_array);
182 ext4_clear_inode(inode); /* We must guarantee clearing of inode... */
183 @@ -5008,7 +5039,7 @@ static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
185 * Also account for superblock, inode, quota and xattr blocks
187 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
188 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
191 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
192 diff --git a/fs/ext4/xattr.c b/fs/ext4/xattr.c
193 index 263002f..b69145b 100644
194 --- a/fs/ext4/xattr.c
195 +++ b/fs/ext4/xattr.c
196 @@ -202,6 +202,7 @@ ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
198 while (!IS_LAST_ENTRY(entry)) {
199 if (entry->e_value_size != 0 &&
200 + entry->e_value_inum == 0 &&
201 (value_start + le16_to_cpu(entry->e_value_offs) <
202 (void *)e + sizeof(__u32) ||
203 value_start + le16_to_cpu(entry->e_value_offs) +
204 @@ -234,19 +235,26 @@ ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
208 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
209 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
210 + struct inode *inode)
212 size_t value_size = le32_to_cpu(entry->e_value_size);
214 - if (entry->e_value_block != 0 || value_size > size ||
215 + if (!entry->e_value_inum &&
216 le16_to_cpu(entry->e_value_offs) + value_size > size)
217 return -EFSCORRUPTED;
218 + if (entry->e_value_inum &&
219 + (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
220 + le32_to_cpu(entry->e_value_inum) >
221 + le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
222 + return -EFSCORRUPTED;
227 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
228 - const char *name, size_t size, int sorted)
229 + const char *name, size_t size, int sorted,
230 + struct inode *inode)
232 struct ext4_xattr_entry *entry;
234 @@ -266,11 +274,104 @@ ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
238 - if (!cmp && ext4_xattr_check_entry(entry, size))
239 + if (!cmp && ext4_xattr_check_entry(entry, size, inode))
240 return -EFSCORRUPTED;
241 return cmp ? -ENODATA : 0;
245 + * Read the EA value from an inode.
248 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
250 + unsigned long block = 0;
251 + struct buffer_head *bh = NULL;
253 + size_t csize, ret_size = 0;
258 + blocksize = ea_inode->i_sb->s_blocksize;
260 + while (ret_size < *size) {
261 + csize = (*size - ret_size) > blocksize ? blocksize :
263 + bh = ext4_bread(NULL, ea_inode, block, 0);
266 + return PTR_ERR(bh);
268 + memcpy(buf, bh->b_data, csize);
281 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
283 + struct inode *ea_inode = NULL;
285 + ea_inode = ext4_iget(parent->i_sb, ea_ino);
286 + if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
287 + int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
288 + ext4_error(parent->i_sb, "error while reading EA inode %lu "
289 + "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
290 + *err = rc != 0 ? rc : -EIO;
294 + if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
295 + ea_inode->i_generation != parent->i_generation) {
296 + ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
297 + "to parent invalid.", ea_ino);
302 + if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
303 + ext4_error(parent->i_sb, "EA inode %lu does not have "
304 + "EXT4_EA_INODE_FL flag set.\n", ea_ino);
318 + * Read the value from the EA inode.
321 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
324 + struct inode *ea_inode = NULL;
327 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
331 + err = ext4_xattr_inode_read(ea_inode, buffer, size);
338 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
339 void *buffer, size_t buffer_size)
340 @@ -303,7 +404,8 @@ bad_block:
342 ext4_xattr_cache_insert(ext4_mb_cache, bh);
344 - error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
345 + error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
347 if (error == -EFSCORRUPTED)
350 @@ -313,8 +415,16 @@ bad_block:
352 if (size > buffer_size)
354 - memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
356 + if (entry->e_value_inum) {
357 + error = ext4_xattr_inode_get(inode,
358 + le32_to_cpu(entry->e_value_inum),
363 + memcpy(buffer, bh->b_data +
364 + le16_to_cpu(entry->e_value_offs), size);
369 @@ -348,7 +458,7 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
372 error = ext4_xattr_find_entry(&entry, name_index, name,
373 - end - (void *)entry, 0);
374 + end - (void *)entry, 0, inode);
377 size = le32_to_cpu(entry->e_value_size);
378 @@ -356,8 +466,16 @@ ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
380 if (size > buffer_size)
382 - memcpy(buffer, (void *)IFIRST(header) +
383 - le16_to_cpu(entry->e_value_offs), size);
384 + if (entry->e_value_inum) {
385 + error = ext4_xattr_inode_get(inode,
386 + le32_to_cpu(entry->e_value_inum),
391 + memcpy(buffer, (void *)IFIRST(header) +
392 + le16_to_cpu(entry->e_value_offs), size);
397 @@ -603,7 +721,7 @@ static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
398 size_t *min_offs, void *base, int *total)
400 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
401 - if (!last->e_value_block && last->e_value_size) {
402 + if (!last->e_value_inum && last->e_value_size) {
403 size_t offs = le16_to_cpu(last->e_value_offs);
404 if (offs < *min_offs)
406 @@ -614,16 +732,176 @@ static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
407 return (*min_offs - ((void *)last - base) - sizeof(__u32));
411 + * Write the value of the EA in an inode.
414 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
415 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
416 + const void *buf, int bufsize)
418 + struct buffer_head *bh = NULL;
419 + unsigned long block = 0;
420 + unsigned blocksize = ea_inode->i_sb->s_blocksize;
421 + unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
422 + int csize, wsize = 0;
427 + while (ret >= 0 && ret < max_blocks) {
428 + struct ext4_map_blocks map;
429 + map.m_lblk = block += ret;
430 + map.m_len = max_blocks -= ret;
432 + ret = ext4_map_blocks(handle, ea_inode, &map,
433 + EXT4_GET_BLOCKS_CREATE);
435 + ext4_mark_inode_dirty(handle, ea_inode);
436 + if (ret == -ENOSPC &&
437 + ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
449 + while (wsize < bufsize) {
452 + csize = (bufsize - wsize) > blocksize ? blocksize :
454 + bh = ext4_getblk(handle, ea_inode, block, 0);
459 + ret = ext4_journal_get_write_access(handle, bh);
463 + memcpy(bh->b_data, buf, csize);
464 + set_buffer_uptodate(bh);
465 + ext4_handle_dirty_metadata(handle, ea_inode, bh);
472 + mutex_lock(&ea_inode->i_mutex);
473 + i_size_write(ea_inode, wsize);
474 + ext4_update_i_disksize(ea_inode, wsize);
475 + mutex_unlock(&ea_inode->i_mutex);
477 + ext4_mark_inode_dirty(handle, ea_inode);
486 + * Create an inode to store the value of a large EA.
488 +static struct inode *
489 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
491 + struct inode *ea_inode = NULL;
494 + * Let the next inode be the goal, so we try and allocate the EA inode
495 + * in the same group, or nearby one.
497 + ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
498 + S_IFREG|0600, NULL, inode->i_ino + 1, NULL);
500 + if (!IS_ERR(ea_inode)) {
501 + ea_inode->i_op = &ext4_file_inode_operations;
502 + ea_inode->i_fop = &ext4_file_operations;
503 + ext4_set_aops(ea_inode);
504 + ea_inode->i_generation = inode->i_generation;
505 + EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
508 + * A back-pointer from EA inode to parent inode will be useful
511 + EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
512 + unlock_new_inode(ea_inode);
519 + * Unlink the inode storing the value of the EA.
522 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
524 + struct inode *ea_inode = NULL;
527 + ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
531 + clear_nlink(ea_inode);
538 + * Add value of the EA in an inode.
541 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
542 + const void *value, size_t value_len)
544 + struct inode *ea_inode = NULL;
547 + /* Create an inode for the EA value */
548 + ea_inode = ext4_xattr_inode_create(handle, inode);
549 + if (IS_ERR(ea_inode))
552 + err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
554 + clear_nlink(ea_inode);
556 + *ea_ino = ea_inode->i_ino;
564 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
565 + handle_t *handle, struct inode *inode)
567 struct ext4_xattr_entry *last;
568 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
569 + int in_inode = i->in_inode;
571 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
572 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
573 + (EXT4_XATTR_SIZE(i->value_len) >
574 + EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
577 /* Compute min_offs and last. */
579 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
580 - if (!last->e_value_block && last->e_value_size) {
581 + if (!last->e_value_inum && last->e_value_size) {
582 size_t offs = le16_to_cpu(last->e_value_offs);
585 @@ -631,15 +909,20 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
587 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
589 - if (!s->here->e_value_block && s->here->e_value_size) {
591 + !s->here->e_value_inum && s->here->e_value_size) {
592 size_t size = le32_to_cpu(s->here->e_value_size);
593 free += EXT4_XATTR_SIZE(size);
595 free += EXT4_XATTR_LEN(name_len);
598 - if (free < EXT4_XATTR_LEN(name_len) +
599 - EXT4_XATTR_SIZE(i->value_len))
600 + size_t value_len = EXT4_XATTR_SIZE(i->value_len);
605 + if (free < EXT4_XATTR_LEN(name_len) + value_len)
609 @@ -653,7 +936,8 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
610 s->here->e_name_len = name_len;
611 memcpy(s->here->e_name, i->name, name_len);
613 - if (!s->here->e_value_block && s->here->e_value_size) {
614 + if (!s->here->e_value_inum && s->here->e_value_size &&
615 + s->here->e_value_offs > 0) {
616 void *first_val = s->base + min_offs;
617 size_t offs = le16_to_cpu(s->here->e_value_offs);
618 void *val = s->base + offs;
619 @@ -687,13 +971,18 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
621 while (!IS_LAST_ENTRY(last)) {
622 size_t o = le16_to_cpu(last->e_value_offs);
623 - if (!last->e_value_block &&
624 + if (!last->e_value_inum &&
625 last->e_value_size && o < offs)
627 cpu_to_le16(o + size);
628 last = EXT4_XATTR_NEXT(last);
631 + if (s->here->e_value_inum) {
632 + ext4_xattr_inode_unlink(inode,
633 + le32_to_cpu(s->here->e_value_inum));
634 + s->here->e_value_inum = 0;
637 /* Remove the old name. */
638 size_t size = EXT4_XATTR_LEN(name_len);
639 @@ -707,10 +996,17 @@ ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
641 /* Insert the new value. */
642 s->here->e_value_size = cpu_to_le32(i->value_len);
643 - if (i->value_len) {
645 + unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
646 + ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
648 + s->here->e_value_inum = cpu_to_le32(ea_ino);
649 + s->here->e_value_offs = 0;
650 + } else if (i->value_len) {
651 size_t size = EXT4_XATTR_SIZE(i->value_len);
652 void *val = s->base + min_offs - size;
653 s->here->e_value_offs = cpu_to_le16(min_offs - size);
654 + s->here->e_value_inum = 0;
655 if (i->value == EXT4_ZERO_XATTR_VALUE) {
656 memset(val, 0, size);
658 @@ -760,7 +1056,7 @@ ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
659 bs->s.end = bs->bh->b_data + bs->bh->b_size;
660 bs->s.here = bs->s.first;
661 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
662 - i->name, bs->bh->b_size, 1);
663 + i->name, bs->bh->b_size, 1, inode);
664 if (error && error != -ENODATA)
666 bs->s.not_found = error;
667 @@ -785,8 +1081,6 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
669 #define header(x) ((struct ext4_xattr_header *)(x))
671 - if (i->value && i->value_len > sb->s_blocksize)
674 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
676 @@ -802,7 +1096,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
679 ea_bdebug(bs->bh, "modifying in-place");
680 - error = ext4_xattr_set_entry(i, s);
681 + error = ext4_xattr_set_entry(i, s, handle, inode);
683 if (!IS_LAST_ENTRY(s->first))
684 ext4_xattr_rehash(header(s->base),
685 @@ -854,7 +1148,7 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
686 s->end = s->base + sb->s_blocksize;
689 - error = ext4_xattr_set_entry(i, s);
690 + error = ext4_xattr_set_entry(i, s, handle, inode);
691 if (error == -EFSCORRUPTED)
694 @@ -998,7 +1292,7 @@ int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
695 /* Find the named attribute. */
696 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
698 - (void *)is->s.base, 0);
699 + (void *)is->s.base, 0, inode);
700 if (error && error != -ENODATA)
702 is->s.not_found = error;
703 @@ -1016,7 +1310,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
705 if (EXT4_I(inode)->i_extra_isize == 0)
707 - error = ext4_xattr_set_entry(i, s);
708 + error = ext4_xattr_set_entry(i, s, handle, inode);
710 if (error == -ENOSPC &&
711 ext4_has_inline_data(inode)) {
712 @@ -1028,7 +1322,7 @@ int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
713 error = ext4_xattr_ibody_find(inode, i, is);
716 - error = ext4_xattr_set_entry(i, s);
717 + error = ext4_xattr_set_entry(i, s, handle, inode);
721 @@ -1054,7 +1348,7 @@ static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
723 if (EXT4_I(inode)->i_extra_isize == 0)
725 - error = ext4_xattr_set_entry(i, s);
726 + error = ext4_xattr_set_entry(i, s, handle, inode);
729 header = IHDR(inode, ext4_raw_inode(&is->iloc));
730 @@ -1090,7 +1384,7 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
733 .value_len = value_len,
737 struct ext4_xattr_ibody_find is = {
738 .s = { .not_found = -ENODATA, },
739 @@ -1155,6 +1449,15 @@ ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
742 error = ext4_xattr_block_set(handle, inode, &i, &bs);
743 + if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
744 + EXT4_FEATURE_INCOMPAT_EA_INODE) &&
745 + error == -ENOSPC) {
746 + /* xattr not fit to block, store at external
749 + error = ext4_xattr_ibody_set(handle, inode,
754 if (!is.s.not_found) {
755 @@ -1201,9 +1504,22 @@ ext4_xattr_set(struct inode *inode, int name_index, const char *name,
756 const void *value, size_t value_len, int flags)
759 + struct super_block *sb = inode->i_sb;
760 int error, retries = 0;
761 int credits = ext4_jbd2_credits_xattr(inode);
763 + if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
764 + EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
765 + int nrblocks = (value_len + sb->s_blocksize - 1) >>
766 + sb->s_blocksize_bits;
768 + /* For new inode */
769 + credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
771 + /* For data blocks of EA inode */
772 + credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
776 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
777 if (IS_ERR(handle)) {
778 @@ -1215,7 +1531,7 @@ retry:
779 value, value_len, flags);
780 error2 = ext4_journal_stop(handle);
781 if (error == -ENOSPC &&
782 - ext4_should_retry_alloc(inode->i_sb, &retries))
783 + ext4_should_retry_alloc(sb, &retries))
787 @@ -1237,7 +1553,7 @@ static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
789 /* Adjust the value offsets of the entries */
790 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
791 - if (!last->e_value_block && last->e_value_size) {
792 + if (!last->e_value_inum && last->e_value_size) {
793 new_offs = le16_to_cpu(last->e_value_offs) +
795 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
796 @@ -1484,21 +1800,135 @@ cleanup:
800 +#define EIA_INCR 16 /* must be 2^n */
801 +#define EIA_MASK (EIA_INCR - 1)
802 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
803 + * If @lea_ino_array is new or full it will be grown and the old
804 + * contents copied over.
807 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
809 + if (*lea_ino_array == NULL) {
811 + * Start with 15 inodes, so it fits into a power-of-two size.
812 + * If *lea_ino_array is NULL, this is essentially offsetof()
815 + kmalloc(offsetof(struct ext4_xattr_ino_array,
816 + xia_inodes[EIA_MASK]),
818 + if (*lea_ino_array == NULL)
820 + (*lea_ino_array)->xia_count = 0;
821 + } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
822 + /* expand the array once all 15 + n * 16 slots are full */
823 + struct ext4_xattr_ino_array *new_array = NULL;
824 + int count = (*lea_ino_array)->xia_count;
826 + /* if new_array is NULL, this is essentially offsetof() */
827 + new_array = kmalloc(
828 + offsetof(struct ext4_xattr_ino_array,
829 + xia_inodes[count + EIA_INCR]),
831 + if (new_array == NULL)
833 + memcpy(new_array, *lea_ino_array,
834 + offsetof(struct ext4_xattr_ino_array,
835 + xia_inodes[count]));
836 + kfree(*lea_ino_array);
837 + *lea_ino_array = new_array;
839 + (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
844 + * Add xattr inode to orphan list
847 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
848 + int credits, struct ext4_xattr_ino_array *lea_ino_array)
850 + struct inode *ea_inode = NULL;
851 + int idx = 0, error = 0;
853 + if (lea_ino_array == NULL)
856 + for (; idx < lea_ino_array->xia_count; ++idx) {
857 + if (!ext4_handle_has_enough_credits(handle, credits)) {
858 + error = ext4_journal_extend(handle, credits);
860 + error = ext4_journal_restart(handle, credits);
863 + ext4_warning(inode->i_sb,
864 + "couldn't extend journal "
865 + "(err %d)", error);
869 + ea_inode = ext4_xattr_inode_iget(inode,
870 + lea_ino_array->xia_inodes[idx], &error);
873 + ext4_orphan_add(handle, ea_inode);
874 + /* the inode's i_count will be released by caller */
881 * ext4_xattr_delete_inode()
883 - * Free extended attribute resources associated with this inode. This
884 + * Free extended attribute resources associated with this inode. Traverse
885 + * all entries and unlink any xattr inodes associated with this inode. This
886 * is called immediately before an inode is freed. We have exclusive
887 - * access to the inode.
888 + * access to the inode. If an orphan inode is deleted it will also delete any
889 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
890 + * to ensure they belong to the parent inode and were not deleted already.
893 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
895 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
896 + struct ext4_xattr_ino_array **lea_ino_array)
898 struct buffer_head *bh = NULL;
899 + struct ext4_xattr_ibody_header *header;
900 + struct ext4_inode *raw_inode;
901 + struct ext4_iloc iloc;
902 + struct ext4_xattr_entry *entry;
903 + int credits = 3, error = 0;
905 - if (!EXT4_I(inode)->i_file_acl)
906 + if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
907 + goto delete_external_ea;
909 + error = ext4_get_inode_loc(inode, &iloc);
912 + raw_inode = ext4_raw_inode(&iloc);
913 + header = IHDR(inode, raw_inode);
914 + for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
915 + entry = EXT4_XATTR_NEXT(entry)) {
916 + if (!entry->e_value_inum)
918 + if (ext4_expand_ino_array(lea_ino_array,
919 + entry->e_value_inum) != 0) {
923 + entry->e_value_inum = 0;
928 + if (!EXT4_I(inode)->i_file_acl) {
929 + /* add xattr inode to orphan list */
930 + ext4_xattr_inode_orphan_add(handle, inode, credits,
934 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
936 EXT4_ERROR_INODE(inode, "block %llu read error",
937 @@ -1511,11 +1941,69 @@ ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
938 EXT4_I(inode)->i_file_acl);
942 + for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
943 + entry = EXT4_XATTR_NEXT(entry)) {
944 + if (!entry->e_value_inum)
946 + if (ext4_expand_ino_array(lea_ino_array,
947 + entry->e_value_inum) != 0)
949 + entry->e_value_inum = 0;
952 + /* add xattr inode to orphan list */
953 + error = ext4_xattr_inode_orphan_add(handle, inode, credits,
958 + if (!IS_NOQUOTA(inode))
959 + credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
961 + if (!ext4_handle_has_enough_credits(handle, credits)) {
962 + error = ext4_journal_extend(handle, credits);
964 + error = ext4_journal_restart(handle, credits);
966 + ext4_warning(inode->i_sb,
967 + "couldn't extend journal (err %d)", error);
972 ext4_xattr_release_block(handle, inode, bh);
973 EXT4_I(inode)->i_file_acl = 0;
982 +ext4_xattr_inode_array_free(struct inode *inode,
983 + struct ext4_xattr_ino_array *lea_ino_array)
985 + struct inode *ea_inode = NULL;
989 + if (lea_ino_array == NULL)
992 + for (; idx < lea_ino_array->xia_count; ++idx) {
993 + ea_inode = ext4_xattr_inode_iget(inode,
994 + lea_ino_array->xia_inodes[idx], &err);
997 + /* for inode's i_count get from ext4_xattr_delete_inode */
998 + if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
1000 + clear_nlink(ea_inode);
1003 + kfree(lea_ino_array);
1007 @@ -1585,10 +2073,9 @@ ext4_xattr_cmp(struct ext4_xattr_header *header1,
1008 entry1->e_name_index != entry2->e_name_index ||
1009 entry1->e_name_len != entry2->e_name_len ||
1010 entry1->e_value_size != entry2->e_value_size ||
1011 + entry1->e_value_inum != entry2->e_value_inum ||
1012 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1014 - if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1015 - return -EFSCORRUPTED;
1016 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1017 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1018 le32_to_cpu(entry1->e_value_size)))
1019 @@ -1673,7 +2160,7 @@ static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1023 - if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1024 + if (!entry->e_value_inum && entry->e_value_size) {
1025 __le32 *value = (__le32 *)((char *)header +
1026 le16_to_cpu(entry->e_value_offs));
1027 for (n = (le32_to_cpu(entry->e_value_size) +
1028 diff --git a/fs/ext4/xattr.h b/fs/ext4/xattr.h
1029 index ddc0957..57c7ad5 100644
1030 --- a/fs/ext4/xattr.h
1031 +++ b/fs/ext4/xattr.h
1032 @@ -43,7 +43,7 @@ struct ext4_xattr_entry {
1033 __u8 e_name_len; /* length of name */
1034 __u8 e_name_index; /* attribute name index */
1035 __le16 e_value_offs; /* offset in disk block of value */
1036 - __le32 e_value_block; /* disk block attribute is stored on (n/i) */
1037 + __le32 e_value_inum; /* inode in which the value is stored */
1038 __le32 e_value_size; /* size of attribute value */
1039 __le32 e_hash; /* hash value of name and value */
1040 char e_name[0]; /* attribute name */
1041 @@ -68,6 +68,26 @@ struct ext4_xattr_entry {
1042 EXT4_I(inode)->i_extra_isize))
1043 #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
1046 + * Link EA inode back to parent one using i_mtime field.
1047 + * Extra integer type conversion added to ignore higher
1048 + * bits in i_mtime.tv_sec which might be set by ext4_get()
1050 +#define EXT4_XATTR_INODE_SET_PARENT(inode, inum) \
1052 + (inode)->i_mtime.tv_sec = inum; \
1055 +#define EXT4_XATTR_INODE_GET_PARENT(inode) \
1056 +((__u32)(inode)->i_mtime.tv_sec)
1059 + * The minimum size of EA value when you start storing it in an external inode
1060 + * size of block - size of header - size of 1 entry - 4 null bytes
1062 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b) \
1063 + ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1065 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1066 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1067 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1068 @@ -76,10 +96,11 @@ struct ext4_xattr_entry {
1069 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1071 struct ext4_xattr_info {
1080 struct ext4_xattr_search {
1081 @@ -107,7 +128,14 @@ extern int ext4_xattr_get(struct inode *, int, const char *, void *, size_t);
1082 extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1083 extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1085 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1086 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1088 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1089 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1090 + struct ext4_xattr_ino_array **array);
1091 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1092 + struct ext4_xattr_ino_array *array);
1094 extern void ext4_xattr_put_super(struct super_block *);
1096 extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,