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