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