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