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