Whamcloud - gitweb
64d8547061c55afe9732930663e15e1307d13237
[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,104 @@ 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, unsigned long 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 +               int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
235 +               ext4_error(parent->i_sb, "error while reading EA inode %lu "
236 +                          "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
237 +               *err = rc != 0 ? rc : -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 %lu "
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 %lu 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, unsigned long 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, unsigned long 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, unsigned long *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,18 @@ 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 +                               if (!last->e_value_inum &&
585                                     last->e_value_size && 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 +                       unsigned long 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 @@ -1088,10 +1387,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 @@ -1101,7 +1415,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 @@ -1123,7 +1437,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 @@ -1358,22 +1672,135 @@ cleanup:
743         return error;
744  }
745  
746 +#define EIA_INCR 16 /* must be 2^n */
747 +#define EIA_MASK (EIA_INCR - 1)
748 +/* Add the large xattr @ino into @lea_ino_array for later deletion.
749 + * If @lea_ino_array is new or full it will be grown and the old
750 + * contents copied over.
751 + */
752 +static int
753 +ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
754 +{
755 +       if (*lea_ino_array == NULL) {
756 +               /*
757 +                * Start with 15 inodes, so it fits into a powr-of-two size.
758 +                * If *lea_ino_array is NULL, this is essentially offsetof()
759 +                */
760 +               (*lea_ino_array) =
761 +                       kmalloc(offsetof(struct ext4_xattr_ino_array,
762 +                                        xia_inodes[EIA_MASK]),
763 +                               GFP_NOFS);
764 +               if (*lea_ino_array == NULL)
765 +                       return -ENOMEM;
766 +               (*lea_ino_array)->xia_count = 0;
767 +       } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
768 +               /* expand the array once all 15 + n * 16 slots are full */
769 +               struct ext4_xattr_ino_array *new_array = NULL;
770 +               int count = (*lea_ino_array)->xia_count;
771 +
772 +               /* if new_array is NULL, this is essentially offsetof() */
773 +               new_array = kmalloc(
774 +                               offsetof(struct ext4_xattr_ino_array,
775 +                                        xia_inodes[count + EIA_INCR]),
776 +                               GFP_NOFS);
777 +               if (new_array == NULL)
778 +                       return -ENOMEM;
779 +               memcpy(new_array, *lea_ino_array,
780 +                      offsetof(struct ext4_xattr_ino_array,
781 +                               xia_inodes[count]));
782 +               kfree(*lea_ino_array);
783 +               *lea_ino_array = new_array;
784 +       }
785 +       (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
786 +       return 0;
787 +}
788 +
789 +/**
790 + * Add xattr inode to orphan list
791 + */
792 +static int
793 +ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
794 +                       int credits, struct ext4_xattr_ino_array *lea_ino_array)
795 +{
796 +       struct inode *ea_inode = NULL;
797 +       int idx = 0, error = 0;
798 +
799 +       if (lea_ino_array == NULL)
800 +               return 0;
801  
802 +       for (; idx < lea_ino_array->xia_count; ++idx) {
803 +               if (!ext4_handle_has_enough_credits(handle, credits)) {
804 +                       error = ext4_journal_extend(handle, credits);
805 +                       if (error > 0)
806 +                               error = ext4_journal_restart(handle, credits);
807 +
808 +                       if (error != 0) {
809 +                               ext4_warning(inode->i_sb,
810 +                                       "couldn't extend journal "
811 +                                       "(err %d)", error);
812 +                               return error;
813 +                       }
814 +               }
815 +               ea_inode = ext4_xattr_inode_iget(inode,
816 +                               lea_ino_array->xia_inodes[idx], &error);
817 +               if (error)
818 +                       continue;
819 +               ext4_orphan_add(handle, ea_inode);
820 +               /* the inode's i_count will be released by caller */
821 +       }
822 +
823 +       return 0;
824 +}
825  
826  /*
827   * ext4_xattr_delete_inode()
828   *
829 - * Free extended attribute resources associated with this inode. This
830 + * Free extended attribute resources associated with this inode. Traverse
831 + * all entries and unlink any xattr inodes associated with this inode. This
832   * is called immediately before an inode is freed. We have exclusive
833 - * access to the inode.
834 + * access to the inode. If an orphan inode is deleted it will also delete any
835 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
836 + * to ensure they belong to the parent inode and were not deleted already.
837   */
838 -void
839 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
840 +int
841 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
842 +                       struct ext4_xattr_ino_array **lea_ino_array)
843  {
844         struct buffer_head *bh = NULL;
845 +       struct ext4_xattr_ibody_header *header;
846 +       struct ext4_inode *raw_inode;
847 +       struct ext4_iloc iloc;
848 +       struct ext4_xattr_entry *entry;
849 +       int credits = 3, error = 0;
850  
851 -       if (!EXT4_I(inode)->i_file_acl)
852 +       if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
853 +               goto delete_external_ea;
854 +
855 +       error = ext4_get_inode_loc(inode, &iloc);
856 +       if (error)
857                 goto cleanup;
858 +       raw_inode = ext4_raw_inode(&iloc);
859 +       header = IHDR(inode, raw_inode);
860 +       entry = IFIRST(header);
861 +       for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
862 +               if (entry->e_value_inum == 0)
863 +                       continue;
864 +               if (ext4_expand_ino_array(lea_ino_array,
865 +                                         entry->e_value_inum) != 0) {
866 +                       brelse(iloc.bh);
867 +                       goto cleanup;
868 +               }
869 +               entry->e_value_inum = 0;
870 +       }
871 +       brelse(iloc.bh);
872 +
873 +delete_external_ea:
874 +       if (!EXT4_I(inode)->i_file_acl) {
875 +               /* add xattr inode to orphan list */
876 +               ext4_xattr_inode_orphan_add(handle, inode, credits,
877 +                                               *lea_ino_array);
878 +               goto cleanup;
879 +       }
880         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
881         if (!bh) {
882                 EXT4_ERROR_INODE(inode, "block %llu read error",
883 @@ -1386,11 +1813,69 @@ ext4_xattr_delete_inode(handle_t *handle
884                                  EXT4_I(inode)->i_file_acl);
885                 goto cleanup;
886         }
887 +
888 +       entry = BFIRST(bh);
889 +       for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
890 +               if (entry->e_value_inum == 0)
891 +                       continue;
892 +               if (ext4_expand_ino_array(lea_ino_array,
893 +                                         entry->e_value_inum) != 0)
894 +                       goto cleanup;
895 +               entry->e_value_inum = 0;
896 +       }
897 +
898 +       /* adding xattr inode to orphan list */
899 +       error = ext4_xattr_inode_orphan_add(handle, inode, credits,
900 +                                       *lea_ino_array);
901 +       if (error != 0)
902 +               goto cleanup;
903 +
904 +       if (!IS_NOQUOTA(inode))
905 +               credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
906 +
907 +       if (!ext4_handle_has_enough_credits(handle, credits)) {
908 +               error = ext4_journal_extend(handle, credits);
909 +               if (error > 0)
910 +                       error = ext4_journal_restart(handle, credits);
911 +               if (error != 0) {
912 +                       ext4_warning(inode->i_sb,
913 +                               "couldn't extend journal (err %d)", error);
914 +                       goto cleanup;
915 +               }
916 +       }
917 +
918         ext4_xattr_release_block(handle, inode, bh);
919         EXT4_I(inode)->i_file_acl = 0;
920  
921  cleanup:
922         brelse(bh);
923 +
924 +       return error;
925 +}
926 +
927 +void
928 +ext4_xattr_inode_array_free(struct inode *inode,
929 +                           struct ext4_xattr_ino_array *lea_ino_array)
930 +{
931 +       struct inode    *ea_inode = NULL;
932 +       int             idx = 0;
933 +       int             err;
934 +
935 +       if (lea_ino_array == NULL)
936 +               return;
937 +
938 +       for (; idx < lea_ino_array->xia_count; ++idx) {
939 +               ea_inode = ext4_xattr_inode_iget(inode,
940 +                               lea_ino_array->xia_inodes[idx], &err);
941 +               if (err)
942 +                       continue;
943 +               /* for inode's i_count get from ext4_xattr_delete_inode */
944 +               if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
945 +                       iput(ea_inode);
946 +               ea_inode->i_nlink = 0;
947 +               iput(ea_inode);
948 +       }
949 +       kfree(lea_ino_array);
950  }
951  
952  /*
953 @@ -1460,10 +1945,9 @@ ext4_xattr_cmp(struct ext4_xattr_header
954                     entry1->e_name_index != entry2->e_name_index ||
955                     entry1->e_name_len != entry2->e_name_len ||
956                     entry1->e_value_size != entry2->e_value_size ||
957 +                   entry1->e_value_inum != entry2->e_value_inum ||
958                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
959                         return 1;
960 -               if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
961 -                       return -EIO;
962                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
963                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
964                            le32_to_cpu(entry1->e_value_size)))
965 @@ -1547,7 +2031,7 @@ static inline void ext4_xattr_hash_entry
966                        *name++;
967         }
968  
969 -       if (entry->e_value_block == 0 && entry->e_value_size != 0) {
970 +       if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
971                 __le32 *value = (__le32 *)((char *)header +
972                         le16_to_cpu(entry->e_value_offs));
973                 for (n = (le32_to_cpu(entry->e_value_size) +
974 Index: linux-stage/fs/ext4/xattr.h
975 ===================================================================
976 --- linux-stage.orig/fs/ext4/xattr.h
977 +++ linux-stage/fs/ext4/xattr.h
978 @@ -38,7 +38,7 @@ struct ext4_xattr_entry {
979         __u8    e_name_len;     /* length of name */
980         __u8    e_name_index;   /* attribute name index */
981         __le16  e_value_offs;   /* offset in disk block of value */
982 -       __le32  e_value_block;  /* disk block attribute is stored on (n/i) */
983 +       __le32  e_value_inum;   /* inode in which the value is stored */
984         __le32  e_value_size;   /* size of attribute value */
985         __le32  e_hash;         /* hash value of name and value */
986         char    e_name[0];      /* attribute name */
987 @@ -63,6 +63,15 @@ struct ext4_xattr_entry {
988                 EXT4_I(inode)->i_extra_isize))
989  #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
990  
991 +#define i_xattr_inode_parent i_mtime.tv_sec
992 +
993 +/*
994 + * The minimum size of EA value when you start storing it in an external inode
995 + * size of block - size of header - size of 1 entry - 4 null bytes
996 +*/
997 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b)                                        \
998 +       ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
999 +
1000  # ifdef CONFIG_EXT4_FS_XATTR
1001  
1002  extern const struct xattr_handler ext4_xattr_user_handler;
1003 @@ -77,7 +86,13 @@ extern int ext4_xattr_get(struct inode *
1004  extern int ext4_xattr_set(struct inode *, int, const char *, const void *, size_t, int);
1005  extern int ext4_xattr_set_handle(handle_t *, struct inode *, int, const char *, const void *, size_t, int);
1006  
1007 -extern void ext4_xattr_delete_inode(handle_t *, struct inode *);
1008 +extern struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
1009 +                                          int *err);
1010 +extern int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino);
1011 +extern int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1012 +                                  struct ext4_xattr_ino_array **array);
1013 +extern void ext4_xattr_inode_array_free(struct inode *inode,
1014 +                                       struct ext4_xattr_ino_array *array);
1015  extern void ext4_xattr_put_super(struct super_block *);
1016  
1017  extern int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1018 @@ -111,9 +126,11 @@ ext4_xattr_set_handle(handle_t *handle,
1019         return -EOPNOTSUPP;
1020  }
1021  
1022 -static inline void
1023 -ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1024 +inline int
1025 +ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1026 +                       struct ext4_xattr_ino_array *array)
1027  {
1028 +       return -EOPNOTSUPP;
1029  }
1030  
1031  static inline void
1032 Index: linux-stage/fs/ext4/ialloc.c
1033 ===================================================================
1034 --- linux-stage.orig/fs/ext4/ialloc.c
1035 +++ linux-stage/fs/ext4/ialloc.c
1036 @@ -221,7 +221,6 @@ void ext4_free_inode(handle_t *handle, s
1037          * as writing the quota to disk may need the lock as well.
1038          */
1039         dquot_initialize(inode);
1040 -       ext4_xattr_delete_inode(handle, inode);
1041         dquot_free_inode(inode);
1042         dquot_drop(inode);
1043