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