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