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