Whamcloud - gitweb
LU-7379 kernel: kernel update RHEL7.1 [3.10.0-229.20.1.el7]
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel7 / ext4-large-eas.patch
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
5 number of EAs.
6
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
21  
22  /*
23 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
24 + * This limit is arbitrary, but is reasonable for the xattr API.
25 + */
26 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE    (1024 * 1024)
27 +
28 +/*
29   * Function prototypes
30   */
31  
32 @@ -1990,6 +1997,10 @@ struct mmpd_data {
33  # define ATTRIB_NORET  __attribute__((noreturn))
34  # define NORET_AND     noreturn,
35  
36 +struct ext4_xattr_ino_array {
37 +       unsigned int xia_count;         /* # of used item in the array */
38 +       unsigned int xia_inodes[0];
39 +};
40  /* bitmap.c */
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
56                                 unsigned int length);
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,
60 -                                 int pextents);
61  
62  /*
63   * Test whether an inode is a fast symlink.
64 @@ -184,6 +182,8 @@ void ext4_evict_inode(struct inode *inod
65  {
66         handle_t *handle;
67         int err;
68 +       int extra_credits = 3;
69 +       struct ext4_xattr_ino_array *lea_ino_array = NULL;
70  
71         trace_ext4_evict_inode(inode);
72  
73 @@ -236,8 +236,8 @@ void ext4_evict_inode(struct inode *inod
74          * protection against it
75          */
76         sb_start_intwrite(inode->i_sb);
77 -       handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE,
78 -                                   ext4_blocks_for_truncate(inode)+3);
79 +
80 +       handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, extra_credits);
81         if (IS_ERR(handle)) {
82                 ext4_std_error(inode->i_sb, PTR_ERR(handle));
83                 /*
84 @@ -249,9 +249,36 @@ void ext4_evict_inode(struct inode *inod
85                 sb_end_intwrite(inode->i_sb);
86                 goto no_delete;
87         }
88 -
89         if (IS_SYNC(inode))
90                 ext4_handle_sync(handle);
91 +
92 +       /*
93 +        * Delete xattr inode before deleting the main inode.
94 +        */
95 +       err = ext4_xattr_delete_inode(handle, inode, &lea_ino_array);
96 +       if (err) {
97 +               ext4_warning(inode->i_sb,
98 +                            "couldn't delete inode's xattr (err %d)", err);
99 +               goto stop_handle;
100 +       }
101 +
102 +       if (!IS_NOQUOTA(inode))
103 +               extra_credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
104 +
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);
109 +               if (err > 0)
110 +                       err = ext4_journal_restart(handle,
111 +                       ext4_blocks_for_truncate(inode) + extra_credits);
112 +               if (err != 0) {
113 +                       ext4_warning(inode->i_sb,
114 +                                    "couldn't extend journal (err %d)", err);
115 +                       goto stop_handle;
116 +               }
117 +       }
118 +
119         inode->i_size = 0;
120         err = ext4_mark_inode_dirty(handle, inode);
121         if (err) {
122 @@ -306,8 +333,12 @@ void ext4_evict_inode(struct inode *inod
123                 ext4_clear_inode(inode);
124         else
125                 ext4_free_inode(handle, inode);
126 +
127         ext4_journal_stop(handle);
128         sb_end_intwrite(inode->i_sb);
129 +
130 +       if (lea_ino_array != NULL)
131 +               ext4_xattr_inode_array_free(inode, lea_ino_array);
132         return;
133  no_delete:
134         ext4_clear_inode(inode);        /* We must guarantee clearing of inode... */
135 @@ -4681,7 +4712,7 @@ static int ext4_index_trans_blocks(struc
136   *
137   * Also account for superblock, inode, quota and xattr blocks
138   */
139 -static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
140 +int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
141                                   int pextents)
142  {
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
149  
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
157  }
158  
159  static inline int
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)
163  {
164         size_t value_size = le32_to_cpu(entry->e_value_size);
165  
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))
170 +               return -EIO;
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)))
175                 return -EIO;
176         return 0;
177  }
178  
179  static int
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)
184  {
185         struct ext4_xattr_entry *entry;
186         size_t name_len;
187 @@ -265,11 +273,104 @@ ext4_xattr_find_entry(struct ext4_xattr_
188                         break;
189         }
190         *pentry = entry;
191 -       if (!cmp && ext4_xattr_check_entry(entry, size))
192 +       if (!cmp && ext4_xattr_check_entry(entry, size, inode))
193                         return -EIO;
194         return cmp ? -ENODATA : 0;
195  }
196  
197 +/*
198 + * Read the EA value from an inode.
199 + */
200 +static int
201 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
202 +{
203 +       unsigned long block = 0;
204 +       struct buffer_head *bh = NULL;
205 +       int err, blocksize;
206 +       size_t csize, ret_size = 0;
207 +
208 +       if (*size == 0)
209 +               return 0;
210 +
211 +       blocksize = ea_inode->i_sb->s_blocksize;
212 +
213 +       while (ret_size < *size) {
214 +               csize = (*size - ret_size) > blocksize ? blocksize :
215 +                                                       *size - ret_size;
216 +               bh = ext4_bread(NULL, ea_inode, block, 0, &err);
217 +               if (!bh) {
218 +                       *size = ret_size;
219 +                       return err;
220 +               }
221 +               memcpy(buf, bh->b_data, csize);
222 +               brelse(bh);
223 +
224 +               buf += csize;
225 +               block += 1;
226 +               ret_size += csize;
227 +       }
228 +
229 +       *size = ret_size;
230 +
231 +       return err;
232 +}
233 +
234 +struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
235 +{
236 +       struct inode *ea_inode = NULL;
237 +
238 +       ea_inode = ext4_iget(parent->i_sb, ea_ino);
239 +       if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
240 +               int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
241 +               ext4_error(parent->i_sb, "error while reading EA inode %lu "
242 +                          "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
243 +               *err = rc != 0 ? rc : -EIO;
244 +               return NULL;
245 +       }
246 +
247 +       if (ea_inode->i_xattr_inode_parent != parent->i_ino ||
248 +           ea_inode->i_generation != parent->i_generation) {
249 +               ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
250 +                          "to parent invalid.", ea_ino);
251 +               *err = -EINVAL;
252 +               goto error;
253 +       }
254 +
255 +       if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
256 +               ext4_error(parent->i_sb, "EA inode %lu does not have "
257 +                          "EXT4_EA_INODE_FL flag set.\n", ea_ino);
258 +               *err = -EINVAL;
259 +               goto error;
260 +       }
261 +
262 +       *err = 0;
263 +       return ea_inode;
264 +
265 +error:
266 +       iput(ea_inode);
267 +       return NULL;
268 +}
269 +
270 +/*
271 + * Read the value from the EA inode.
272 + */
273 +static int
274 +ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
275 +                    size_t *size)
276 +{
277 +       struct inode *ea_inode = NULL;
278 +       int err;
279 +
280 +       ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
281 +       if (err)
282 +               return err;
283 +
284 +       err = ext4_xattr_inode_read(ea_inode, buffer, size);
285 +       iput(ea_inode);
286 +
287 +       return err;
288 +}
289 +
290  static int
291  ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
292                      void *buffer, size_t buffer_size)
293 @@ -301,7 +401,8 @@ bad_block:
294         }
295         ext4_xattr_cache_insert(bh);
296         entry = BFIRST(bh);
297 -       error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
298 +       error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
299 +                                     inode);
300         if (error == -EIO)
301                 goto bad_block;
302         if (error)
303 @@ -311,8 +412,16 @@ bad_block:
304                 error = -ERANGE;
305                 if (size > buffer_size)
306                         goto cleanup;
307 -               memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
308 -                      size);
309 +               if (entry->e_value_inum != 0) {
310 +                       error = ext4_xattr_inode_get(inode,
311 +                                            le32_to_cpu(entry->e_value_inum),
312 +                                            buffer, &size);
313 +                       if (error)
314 +                               goto cleanup;
315 +               } else {
316 +                       memcpy(buffer, bh->b_data +
317 +                              le16_to_cpu(entry->e_value_offs), size);
318 +               }
319         }
320         error = size;
321  
322 @@ -346,7 +455,7 @@ ext4_xattr_ibody_get(struct inode *inode
323         if (error)
324                 goto cleanup;
325         error = ext4_xattr_find_entry(&entry, name_index, name,
326 -                                     end - (void *)entry, 0);
327 +                                     end - (void *)entry, 0, inode);
328         if (error)
329                 goto cleanup;
330         size = le32_to_cpu(entry->e_value_size);
331 @@ -354,8 +463,16 @@ ext4_xattr_ibody_get(struct inode *inode
332                 error = -ERANGE;
333                 if (size > buffer_size)
334                         goto cleanup;
335 -               memcpy(buffer, (void *)IFIRST(header) +
336 -                      le16_to_cpu(entry->e_value_offs), size);
337 +               if (entry->e_value_inum != 0) {
338 +                       error = ext4_xattr_inode_get(inode,
339 +                                            le32_to_cpu(entry->e_value_inum),
340 +                                            buffer, &size);
341 +                       if (error)
342 +                               goto cleanup;
343 +               } else {
344 +                       memcpy(buffer, (void *)IFIRST(header) +
345 +                              le16_to_cpu(entry->e_value_offs), size);
346 +               }
347         }
348         error = size;
349  
350 @@ -600,7 +717,7 @@ static size_t ext4_xattr_free_space(stru
351                                     size_t *min_offs, void *base, int *total)
352  {
353         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
354 -               if (!last->e_value_block && last->e_value_size) {
355 +               if (last->e_value_inum == 0 && last->e_value_size > 0) {
356                         size_t offs = le16_to_cpu(last->e_value_offs);
357                         if (offs < *min_offs)
358                                 *min_offs = offs;
359 @@ -611,16 +728,174 @@ static size_t ext4_xattr_free_space(stru
360         return (*min_offs - ((void *)last - base) - sizeof(__u32));
361  }
362  
363 +/*
364 + * Write the value of the EA in an inode.
365 + */
366  static int
367 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
368 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
369 +                      const void *buf, int bufsize)
370 +{
371 +       struct buffer_head *bh = NULL;
372 +       unsigned long block = 0;
373 +       unsigned blocksize = ea_inode->i_sb->s_blocksize;
374 +       unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
375 +       int csize, wsize = 0;
376 +       int ret = 0;
377 +       int retries = 0;
378 +
379 +retry:
380 +       while (ret >= 0 && ret < max_blocks) {
381 +               struct ext4_map_blocks map;
382 +               map.m_lblk = block += ret;
383 +               map.m_len = max_blocks -= ret;
384 +
385 +               ret = ext4_map_blocks(handle, ea_inode, &map,
386 +                                     EXT4_GET_BLOCKS_CREATE);
387 +               if (ret <= 0) {
388 +                       ext4_mark_inode_dirty(handle, ea_inode);
389 +                       if (ret == -ENOSPC &&
390 +                           ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
391 +                               ret = 0;
392 +                               goto retry;
393 +                       }
394 +                       break;
395 +               }
396 +       }
397 +
398 +       if (ret < 0)
399 +               return ret;
400 +
401 +       block = 0;
402 +       while (wsize < bufsize) {
403 +               if (bh != NULL)
404 +                       brelse(bh);
405 +               csize = (bufsize - wsize) > blocksize ? blocksize :
406 +                                                               bufsize - wsize;
407 +               bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
408 +               if (!bh)
409 +                       goto out;
410 +               ret = ext4_journal_get_write_access(handle, bh);
411 +               if (ret)
412 +                       goto out;
413 +
414 +               memcpy(bh->b_data, buf, csize);
415 +               set_buffer_uptodate(bh);
416 +               ext4_handle_dirty_metadata(handle, ea_inode, bh);
417 +
418 +               buf += csize;
419 +               wsize += csize;
420 +               block += 1;
421 +       }
422 +
423 +       mutex_lock(&ea_inode->i_mutex);
424 +       i_size_write(ea_inode, wsize);
425 +       ext4_update_i_disksize(ea_inode, wsize);
426 +       mutex_unlock(&ea_inode->i_mutex);
427 +
428 +       ext4_mark_inode_dirty(handle, ea_inode);
429 +
430 +out:
431 +       brelse(bh);
432 +
433 +       return ret;
434 +}
435 +
436 +/*
437 + * Create an inode to store the value of a large EA.
438 + */
439 +static struct inode *
440 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
441 +{
442 +       struct inode *ea_inode = NULL;
443 +
444 +       /*
445 +        * Let the next inode be the goal, so we try and allocate the EA inode
446 +        * in the same group, or nearby one.
447 +        */
448 +       ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
449 +                                 S_IFREG|0600, NULL, inode->i_ino + 1, NULL);
450 +
451 +       if (!IS_ERR(ea_inode)) {
452 +               ea_inode->i_op = &ext4_file_inode_operations;
453 +               ea_inode->i_fop = &ext4_file_operations;
454 +               ext4_set_aops(ea_inode);
455 +               ea_inode->i_generation = inode->i_generation;
456 +               EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
457 +
458 +               /*
459 +                * A back-pointer from EA inode to parent inode will be useful
460 +                * for e2fsck.
461 +                */
462 +               ea_inode->i_xattr_inode_parent = inode->i_ino;
463 +               unlock_new_inode(ea_inode);
464 +       }
465 +
466 +       return ea_inode;
467 +}
468 +
469 +/*
470 + * Unlink the inode storing the value of the EA.
471 + */
472 +int
473 +ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
474 +{
475 +       struct inode *ea_inode = NULL;
476 +       int err;
477 +
478 +       ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
479 +       if (err)
480 +               return err;
481 +
482 +       clear_nlink(ea_inode);
483 +       iput(ea_inode);
484 +
485 +       return 0;
486 +}
487 +
488 +/*
489 + * Add value of the EA in an inode.
490 + */
491 +static int
492 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, unsigned long *ea_ino,
493 +                    const void *value, size_t value_len)
494 +{
495 +       struct inode *ea_inode = NULL;
496 +       int err;
497 +
498 +       /* Create an inode for the EA value */
499 +       ea_inode = ext4_xattr_inode_create(handle, inode);
500 +       if (IS_ERR(ea_inode))
501 +               return -1;
502 +
503 +       err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
504 +       if (err)
505 +               clear_nlink(ea_inode);
506 +       else
507 +               *ea_ino = ea_inode->i_ino;
508 +
509 +       iput(ea_inode);
510 +
511 +       return err;
512 +}
513 +
514 +static int
515 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
516 +                    handle_t *handle, struct inode *inode)
517  {
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;
521 +
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)))
526 +               in_inode = 1;
527  
528         /* Compute min_offs and last. */
529         last = s->first;
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);
534                         if (offs < min_offs)
535                                 min_offs = offs;
536 @@ -628,16 +903,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
537         }
538         free = min_offs - ((void *)last - s->base) - sizeof(__u32);
539         if (!s->not_found) {
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);
545                 }
546                 free += EXT4_XATTR_LEN(name_len);
547         }
548         if (i->value) {
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);
553 +
554 +               if (in_inode)
555 +                       value_len = 0;
556 +
557 +               if (free < value_len ||
558 +                   free < EXT4_XATTR_LEN(name_len) + value_len)
559                         return -ENOSPC;
560         }
561  
562 @@ -651,7 +931,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);
565         } else {
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 @@ -685,13 +966,18 @@ ext4_xattr_set_entry(struct ext4_xattr_i
573                         last = s->first;
574                         while (!IS_LAST_ENTRY(last)) {
575                                 size_t o = le16_to_cpu(last->e_value_offs);
576 -                               if (!last->e_value_block &&
577 +                               if (!last->e_value_inum &&
578                                     last->e_value_size && o < offs)
579                                         last->e_value_offs =
580                                                 cpu_to_le16(o + size);
581                                 last = EXT4_XATTR_NEXT(last);
582                         }
583                 }
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;
588 +               }
589                 if (!i->value) {
590                         /* Remove the old name. */
591                         size_t size = EXT4_XATTR_LEN(name_len);
592 @@ -705,10 +990,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
593         if (i->value) {
594                 /* Insert the new value. */
595                 s->here->e_value_size = cpu_to_le32(i->value_len);
596 -               if (i->value_len) {
597 +               if (in_inode) {
598 +                       unsigned long ea_ino = le32_to_cpu(s->here->e_value_inum);
599 +                       ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
600 +                                            i->value_len);
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                         if (i->value == EXT4_ZERO_XATTR_VALUE) {
609                                 memset(val, 0, size);
610                         } else {
611 @@ -758,7 +1050,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)
618                         goto cleanup;
619                 bs->s.not_found = error;
620 @@ -782,8 +1074,6 @@ ext4_xattr_block_set(handle_t *handle, s
621  
622  #define header(x) ((struct ext4_xattr_header *)(x))
623  
624 -       if (i->value && i->value_len > sb->s_blocksize)
625 -               return -ENOSPC;
626         if (s->base) {
627                 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
628                                         bs->bh->b_blocknr);
629 @@ -799,7 +1089,7 @@ ext4_xattr_block_set(handle_t *handle, s
630                                 ce = NULL;
631                         }
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);
635                         if (!error) {
636                                 if (!IS_LAST_ENTRY(s->first))
637                                         ext4_xattr_rehash(header(s->base),
638 @@ -850,7 +1140,7 @@ ext4_xattr_block_set(handle_t *handle, s
639                 s->end = s->base + sb->s_blocksize;
640         }
641  
642 -       error = ext4_xattr_set_entry(i, s);
643 +       error = ext4_xattr_set_entry(i, s, handle, inode);
644         if (error == -EIO)
645                 goto bad_block;
646         if (error)
647 @@ -1000,7 +1290,7 @@ int ext4_xattr_ibody_find(struct inode *
648                 /* Find the named attribute. */
649                 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
650                                               i->name, is->s.end -
651 -                                             (void *)is->s.base, 0);
652 +                                             (void *)is->s.base, 0, inode);
653                 if (error && error != -ENODATA)
654                         return error;
655                 is->s.not_found = error;
656 @@ -1018,7 +1308,7 @@ int ext4_xattr_ibody_inline_set(handle_t
657  
658         if (EXT4_I(inode)->i_extra_isize == 0)
659                 return -ENOSPC;
660 -       error = ext4_xattr_set_entry(i, s);
661 +       error = ext4_xattr_set_entry(i, s, handle, inode);
662         if (error) {
663                 if (error == -ENOSPC &&
664                     ext4_has_inline_data(inode)) {
665 @@ -1030,7 +1320,7 @@ int ext4_xattr_ibody_inline_set(handle_t
666                         error = ext4_xattr_ibody_find(inode, i, is);
667                         if (error)
668                                 return error;
669 -                       error = ext4_xattr_set_entry(i, s);
670 +                       error = ext4_xattr_set_entry(i, s, handle, inode);
671                 }
672                 if (error)
673                         return error;
674 @@ -1056,7 +1346,7 @@ static int ext4_xattr_ibody_set(handle_t
675  
676         if (EXT4_I(inode)->i_extra_isize == 0)
677                 return -ENOSPC;
678 -       error = ext4_xattr_set_entry(i, s);
679 +       error = ext4_xattr_set_entry(i, s, handle, inode);
680         if (error)
681                 return error;
682         header = IHDR(inode, ext4_raw_inode(&is->iloc));
683 @@ -1092,7 +1382,7 @@ ext4_xattr_set_handle(handle_t *handle, 
684                 .name = name,
685                 .value = value,
686                 .value_len = value_len,
687 -
688 +               .in_inode = 0,
689         };
690         struct ext4_xattr_ibody_find is = {
691                 .s = { .not_found = -ENODATA, },
692 @@ -1157,6 +1447,15 @@ ext4_xattr_set_handle(handle_t *handle, 
693                                         goto cleanup;
694                         }
695                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
696 +                       if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
697 +                                       EXT4_FEATURE_INCOMPAT_EA_INODE) &&
698 +                           error == -ENOSPC) {
699 +                               /* xattr not fit to block, store at external
700 +                                * inode */
701 +                               i.in_inode = 1;
702 +                               error = ext4_xattr_ibody_set(handle, inode,
703 +                                                            &i, &is);
704 +                       }
705                         if (error)
706                                 goto cleanup;
707                         if (!is.s.not_found) {
708 @@ -1203,9 +1502,22 @@ ext4_xattr_set(struct inode *inode, int 
709                const void *value, size_t value_len, int flags)
710  {
711         handle_t *handle;
712 +       struct super_block *sb = inode->i_sb;
713         int error, retries = 0;
714         int credits = ext4_jbd2_credits_xattr(inode);
715  
716 +       if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
717 +           EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
718 +               int nrblocks = (value_len + sb->s_blocksize - 1) >>
719 +                                       sb->s_blocksize_bits;
720 +
721 +               /* For new inode */
722 +               credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
723 +
724 +               /* For data blocks of EA inode */
725 +               credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
726 +       }
727 +
728  retry:
729         handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
730         if (IS_ERR(handle)) {
731 @@ -1217,7 +1529,7 @@ retry:
732                                               value, value_len, flags);
733                 error2 = ext4_journal_stop(handle);
734                 if (error == -ENOSPC &&
735 -                   ext4_should_retry_alloc(inode->i_sb, &retries))
736 +                   ext4_should_retry_alloc(sb, &retries))
737                         goto retry;
738                 if (error == 0)
739                         error = error2;
740 @@ -1239,7 +1551,7 @@ static void ext4_xattr_shift_entries(str
741  
742         /* Adjust the value offsets of the entries */
743         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
744 -               if (!last->e_value_block && last->e_value_size) {
745 +               if (last->e_value_inum == 0 && last->e_value_size > 0) {
746                         new_offs = le16_to_cpu(last->e_value_offs) +
747                                                         value_offs_shift;
748                         BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
749 @@ -1477,21 +1789,135 @@ cleanup:
750  }
751  
752  
753 +#define EIA_INCR 16 /* must be 2^n */
754 +#define EIA_MASK (EIA_INCR - 1)
755 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
756 + * If @lea_ino_array is new or full it will be grown and the old
757 + * contents copied over.
758 + */
759 +static int
760 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
761 +{
762 +       if (*lea_ino_array == NULL) {
763 +               /*
764 +                * Start with 15 inodes, so it fits into a power-of-two size.
765 +                * If *lea_ino_array is NULL, this is essentially offsetof()
766 +                */
767 +               (*lea_ino_array) =
768 +                       kmalloc(offsetof(struct ext4_xattr_ino_array,
769 +                                        xia_inodes[EIA_MASK]),
770 +                               GFP_NOFS);
771 +               if (*lea_ino_array == NULL)
772 +                       return -ENOMEM;
773 +               (*lea_ino_array)->xia_count = 0;
774 +       } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
775 +               /* expand the array once all 15 + n * 16 slots are full */
776 +               struct ext4_xattr_ino_array *new_array = NULL;
777 +               int count = (*lea_ino_array)->xia_count;
778 +
779 +               /* if new_array is NULL, this is essentially offsetof() */
780 +               new_array = kmalloc(
781 +                               offsetof(struct ext4_xattr_ino_array,
782 +                                        xia_inodes[count + EIA_INCR]),
783 +                               GFP_NOFS);
784 +               if (new_array == NULL)
785 +                       return -ENOMEM;
786 +               memcpy(new_array, *lea_ino_array,
787 +                      offsetof(struct ext4_xattr_ino_array,
788 +                               xia_inodes[count]));
789 +               kfree(*lea_ino_array);
790 +               *lea_ino_array = new_array;
791 +       }
792 +       (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
793 +       return 0;
794 +}
795 +
796 +/**
797 + * Add xattr inode to orphan list
798 + */
799 +static int
800 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
801 +                       int credits, struct ext4_xattr_ino_array *lea_ino_array)
802 +{
803 +       struct inode *ea_inode = NULL;
804 +       int idx = 0, error = 0;
805 +
806 +       if (lea_ino_array == NULL)
807 +               return 0;
808 +
809 +       for (; idx < lea_ino_array->xia_count; ++idx) {
810 +               if (!ext4_handle_has_enough_credits(handle, credits)) {
811 +                       error = ext4_journal_extend(handle, credits);
812 +                       if (error > 0)
813 +                               error = ext4_journal_restart(handle, credits);
814 +
815 +                       if (error != 0) {
816 +                               ext4_warning(inode->i_sb,
817 +                                       "couldn't extend journal "
818 +                                       "(err %d)", error);
819 +                               return error;
820 +                       }
821 +               }
822 +               ea_inode = ext4_xattr_inode_iget(inode,
823 +                               lea_ino_array->xia_inodes[idx], &error);
824 +               if (error)
825 +                       continue;
826 +               ext4_orphan_add(handle, ea_inode);
827 +               /* the inode's i_count will be released by caller */
828 +       }
829 +
830 +       return 0;
831 +}
832  
833  /*
834   * ext4_xattr_delete_inode()
835   *
836 - * Free extended attribute resources associated with this inode. This
837 + * Free extended attribute resources associated with this inode. Traverse
838 + * all entries and unlink any xattr inodes associated with this inode. This
839   * is called immediately before an inode is freed. We have exclusive
840 - * access to the inode.
841 + * access to the inode. If an orphan inode is deleted it will also delete any
842 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
843 + * to ensure they belong to the parent inode and were not deleted already.
844   */
845 -void
846 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
847 +int
848 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
849 +                       struct ext4_xattr_ino_array **lea_ino_array)
850  {
851         struct buffer_head *bh = NULL;
852 +       struct ext4_xattr_ibody_header *header;
853 +       struct ext4_inode *raw_inode;
854 +       struct ext4_iloc iloc;
855 +       struct ext4_xattr_entry *entry;
856 +       int credits = 3, error = 0;
857  
858 -       if (!EXT4_I(inode)->i_file_acl)
859 +       if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
860 +               goto delete_external_ea;
861 +
862 +       error = ext4_get_inode_loc(inode, &iloc);
863 +       if (error)
864                 goto cleanup;
865 +       raw_inode = ext4_raw_inode(&iloc);
866 +       header = IHDR(inode, raw_inode);
867 +       entry = IFIRST(header);
868 +       for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
869 +               if (entry->e_value_inum == 0)
870 +                       continue;
871 +               if (ext4_expand_ino_array(lea_ino_array,
872 +                                         entry->e_value_inum) != 0) {
873 +                       brelse(iloc.bh);
874 +                       goto cleanup;
875 +               }
876 +               entry->e_value_inum = 0;
877 +       }
878 +       brelse(iloc.bh);
879 +
880 +delete_external_ea:
881 +       if (!EXT4_I(inode)->i_file_acl) {
882 +               /* add xattr inode to orphan list */
883 +               ext4_xattr_inode_orphan_add(handle, inode, credits,
884 +                                               *lea_ino_array);
885 +               goto cleanup;
886 +       }
887         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
888         if (!bh) {
889                 EXT4_ERROR_INODE(inode, "block %llu read error",
890 @@ -1504,11 +1930,69 @@ ext4_xattr_delete_inode(handle_t *handle
891                                  EXT4_I(inode)->i_file_acl);
892                 goto cleanup;
893         }
894 +
895 +       entry = BFIRST(bh);
896 +       for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
897 +               if (entry->e_value_inum == 0)
898 +                       continue;
899 +               if (ext4_expand_ino_array(lea_ino_array,
900 +                                         entry->e_value_inum) != 0)
901 +                       goto cleanup;
902 +               entry->e_value_inum = 0;
903 +       }
904 +
905 +       /* add xattr inode to orphan list */
906 +       error = ext4_xattr_inode_orphan_add(handle, inode, credits,
907 +                                       *lea_ino_array);
908 +       if (error != 0)
909 +               goto cleanup;
910 +
911 +       if (!IS_NOQUOTA(inode))
912 +               credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
913 +
914 +       if (!ext4_handle_has_enough_credits(handle, credits)) {
915 +               error = ext4_journal_extend(handle, credits);
916 +               if (error > 0)
917 +                       error = ext4_journal_restart(handle, credits);
918 +               if (error != 0) {
919 +                       ext4_warning(inode->i_sb,
920 +                               "couldn't extend journal (err %d)", error);
921 +                       goto cleanup;
922 +               }
923 +       }
924 +
925         ext4_xattr_release_block(handle, inode, bh);
926         EXT4_I(inode)->i_file_acl = 0;
927  
928  cleanup:
929         brelse(bh);
930 +
931 +       return error;
932 +}
933 +
934 +void
935 +ext4_xattr_inode_array_free(struct inode *inode,
936 +                           struct ext4_xattr_ino_array *lea_ino_array)
937 +{
938 +       struct inode    *ea_inode = NULL;
939 +       int             idx = 0;
940 +       int             err;
941 +
942 +       if (lea_ino_array == NULL)
943 +               return;
944 +
945 +       for (; idx < lea_ino_array->xia_count; ++idx) {
946 +               ea_inode = ext4_xattr_inode_iget(inode,
947 +                               lea_ino_array->xia_inodes[idx], &err);
948 +               if (err)
949 +                       continue;
950 +               /* for inode's i_count get from ext4_xattr_delete_inode */
951 +               if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
952 +                       iput(ea_inode);
953 +               clear_nlink(ea_inode);
954 +               iput(ea_inode);
955 +       }
956 +       kfree(lea_ino_array);
957  }
958  
959  /*
960 @@ -1578,10 +2062,9 @@ ext4_xattr_cmp(struct ext4_xattr_header 
961                     entry1->e_name_index != entry2->e_name_index ||
962                     entry1->e_name_len != entry2->e_name_len ||
963                     entry1->e_value_size != entry2->e_value_size ||
964 +                   entry1->e_value_inum != entry2->e_value_inum ||
965                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
966                         return 1;
967 -               if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
968 -                       return -EIO;
969                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
970                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
971                            le32_to_cpu(entry1->e_value_size)))
972 @@ -1665,7 +2148,7 @@ static inline void ext4_xattr_hash_entry
973                        *name++;
974         }
975  
976 -       if (entry->e_value_block == 0 && entry->e_value_size != 0) {
977 +       if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
978                 __le32 *value = (__le32 *)((char *)header +
979                         le16_to_cpu(entry->e_value_offs));
980                 for (n = (le32_to_cpu(entry->e_value_size) +
981 Index: linux-stage/fs/ext4/xattr.h
982 ===================================================================
983 --- linux-stage.orig/fs/ext4/xattr.h
984 +++ linux-stage/fs/ext4/xattr.h
985 @@ -42,7 +42,7 @@ struct ext4_xattr_entry {
986         __u8    e_name_len;     /* length of name */
987         __u8    e_name_index;   /* attribute name index */
988         __le16  e_value_offs;   /* offset in disk block of value */
989 -       __le32  e_value_block;  /* disk block attribute is stored on (n/i) */
990 +       __le32  e_value_inum;   /* inode in which the value is stored */
991         __le32  e_value_size;   /* size of attribute value */
992         __le32  e_hash;         /* hash value of name and value */
993         char    e_name[0];      /* attribute name */
994 @@ -67,6 +67,15 @@ struct ext4_xattr_entry {
995                 EXT4_I(inode)->i_extra_isize))
996  #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
997  
998 +#define i_xattr_inode_parent i_mtime.tv_sec
999 +
1000 +/*
1001 + * The minimum size of EA value when you start storing it in an external inode
1002 + * size of block - size of header - size of 1 entry - 4 null bytes
1003 +*/
1004 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b)                                        \
1005 +       ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
1006 +
1007  #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
1008  #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
1009  #define BFIRST(bh) ENTRY(BHDR(bh)+1)
1010 @@ -75,10 +84,11 @@ struct ext4_xattr_entry {
1011  #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
1012  
1013  struct ext4_xattr_info {
1014 -       int name_index;
1015         const char *name;
1016         const void *value;
1017         size_t value_len;
1018 +       int name_index;
1019 +       int in_inode;
1020  };
1021  
1022  struct ext4_xattr_search {
1023 @@ -106,7 +116,13 @@ extern int ext4_xattr_get(struct inode *
1024  extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1025  extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1026  
1027 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1028 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1029 +                                          int *err);
1030 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1031 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1032 +                                  struct ext4_xattr_ino_array **array);
1033 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1034 +                                       struct ext4_xattr_ino_array *array);
1035  extern void ext4_xattr_put_super(struct super_block *);
1036  
1037  extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1038 Index: linux-stage/fs/ext4/ialloc.c
1039 ===================================================================
1040 --- linux-stage.orig/fs/ext4/ialloc.c
1041 +++ linux-stage/fs/ext4/ialloc.c
1042 @@ -269,7 +269,6 @@ void ext4_free_inode(handle_t *handle, s
1043          * as writing the quota to disk may need the lock as well.
1044          */
1045         dquot_initialize(inode);
1046 -       ext4_xattr_delete_inode(handle, inode);
1047         dquot_free_inode(inode);
1048         dquot_drop(inode);
1049  
1050 Index: linux-stage/fs/ext4/inline.c
1051 ===================================================================
1052 --- linux-stage.orig/fs/ext4/inline.c
1053 +++ linux-stage/fs/ext4/inline.c
1054 @@ -59,7 +59,7 @@ static int get_max_inline_xattr_value_si
1055  
1056         /* Compute min_offs. */
1057         for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
1058 -               if (!entry->e_value_block && entry->e_value_size) {
1059 +               if (!entry->e_value_inum && entry->e_value_size) {
1060                         size_t offs = le16_to_cpu(entry->e_value_offs);
1061                         if (offs < min_offs)
1062                                 min_offs = offs;