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