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