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