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