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