Whamcloud - gitweb
6c7802c78820c99112f101b7fe1e49b78c242945
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / rhel6.3 / 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 @@ -1267,6 +1267,7 @@ EXT4_INODE_BIT_FNS(state, state_flags)
6  #define EXT4_FEATURE_INCOMPAT_64BIT            0x0080
7  #define EXT4_FEATURE_INCOMPAT_MMP               0x0100
8  #define EXT4_FEATURE_INCOMPAT_FLEX_BG          0x0200
9 +#define EXT4_FEATURE_INCOMPAT_EA_INODE         0x0400
10  #define EXT4_FEATURE_INCOMPAT_DIRDATA          0x1000
11  
12  #define EXT4_FEATURE_COMPAT_SUPP       EXT2_FEATURE_COMPAT_EXT_ATTR
13 @@ -1276,6 +1277,7 @@ EXT4_INODE_BIT_FNS(state, state_flags)
14                                          EXT4_FEATURE_INCOMPAT_EXTENTS| \
15                                          EXT4_FEATURE_INCOMPAT_64BIT| \
16                                          EXT4_FEATURE_INCOMPAT_FLEX_BG| \
17 +                                        EXT4_FEATURE_INCOMPAT_EA_INODE| \
18                                          EXT4_FEATURE_INCOMPAT_MMP| \
19                                          EXT4_FEATURE_INCOMPAT_DIRDATA)
20  
21 @@ -1607,6 +1609,12 @@ struct mmpd_data {
22  #endif
23  
24  /*
25 + * Maximum size of xattr attributes for FEATURE_INCOMPAT_EA_INODE 1Mb
26 + * This limit is arbitrary, but is reasonable for the xattr API.
27 + */
28 +#define EXT4_XATTR_MAX_LARGE_EA_SIZE    (1024 * 1024)
29 +
30 +/*
31   * Function prototypes
32   */
33  
34 Index: linux-stage/fs/ext4/xattr.c
35 ===================================================================
36 --- linux-stage.orig/fs/ext4/xattr.c
37 +++ linux-stage/fs/ext4/xattr.c
38 @@ -168,19 +168,26 @@ ext4_xattr_check_block(struct buffer_hea
39  }
40  
41  static inline int
42 -ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
43 +ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size,
44 +                      struct inode *inode)
45  {
46         size_t value_size = le32_to_cpu(entry->e_value_size);
47  
48 -       if (entry->e_value_block != 0 || value_size > size ||
49 -           le16_to_cpu(entry->e_value_offs) + value_size > size)
50 +       if ((entry->e_value_inum == 0) &&
51 +          (le16_to_cpu(entry->e_value_offs) + value_size > size))
52 +               return -EIO;
53 +       if (entry->e_value_inum != 0 &&
54 +           (le32_to_cpu(entry->e_value_inum) < EXT4_FIRST_INO(inode->i_sb) ||
55 +            le32_to_cpu(entry->e_value_inum) >
56 +            le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_inodes_count)))
57                 return -EIO;
58         return 0;
59  }
60  
61  static int
62  ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
63 -                     const char *name, size_t size, int sorted)
64 +                     const char *name, size_t size, int sorted,
65 +                     struct inode *inode)
66  {
67         struct ext4_xattr_entry *entry;
68         size_t name_len;
69 @@ -200,11 +207,103 @@ ext4_xattr_find_entry(struct ext4_xattr_
70                         break;
71         }
72         *pentry = entry;
73 -       if (!cmp && ext4_xattr_check_entry(entry, size))
74 +       if (!cmp && ext4_xattr_check_entry(entry, size, inode))
75                         return -EIO;
76         return cmp ? -ENODATA : 0;
77  }
78  
79 +/*
80 + * Read the EA value from an inode.
81 + */
82 +static int
83 +ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
84 +{
85 +       unsigned long block = 0;
86 +       struct buffer_head *bh = NULL;
87 +       int err, blocksize;
88 +       size_t csize, ret_size = 0;
89 +
90 +       if (*size == 0)
91 +               return 0;
92 +
93 +       blocksize = ea_inode->i_sb->s_blocksize;
94 +
95 +       while (ret_size < *size) {
96 +               csize = (*size - ret_size) > blocksize ? blocksize :
97 +                                                       *size - ret_size;
98 +               bh = ext4_bread(NULL, ea_inode, block, 0, &err);
99 +               if (!bh) {
100 +                       *size = ret_size;
101 +                       return err;
102 +               }
103 +               memcpy(buf, bh->b_data, csize);
104 +               brelse(bh);
105 +
106 +               buf += csize;
107 +               block += 1;
108 +               ret_size += csize;
109 +       }
110 +
111 +       *size = ret_size;
112 +
113 +       return err;
114 +}
115 +
116 +struct inode *ext4_xattr_inode_iget(struct inode *parent, int ea_ino, int *err)
117 +{
118 +       struct inode *ea_inode = NULL;
119 +
120 +       ea_inode = ext4_iget(parent->i_sb, ea_ino);
121 +       if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
122 +               ext4_error(parent->i_sb, "error while reading EA inode %d",
123 +                          ea_ino);
124 +               *err = -EIO;
125 +               return NULL;
126 +       }
127 +
128 +       if (ea_inode->i_xattr_inode_parent != parent->i_ino ||
129 +           ea_inode->i_generation != parent->i_generation) {
130 +               ext4_error(parent->i_sb, "Backpointer from EA inode %d "
131 +                          "to parent invalid.", ea_ino);
132 +               *err = -EINVAL;
133 +               goto error;
134 +       }
135 +
136 +       if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
137 +               ext4_error(parent->i_sb, "EA inode %d does not have "
138 +                          "EXT4_EA_INODE_FL flag set.\n", ea_ino);
139 +               *err = -EINVAL;
140 +               goto error;
141 +       }
142 +
143 +       *err = 0;
144 +       return ea_inode;
145 +
146 +error:
147 +       iput(ea_inode);
148 +       return NULL;
149 +}
150 +
151 +/*
152 + * Read the value from the EA inode.
153 + */
154 +static int
155 +ext4_xattr_inode_get(struct inode *inode, int ea_ino, void *buffer,
156 +                    size_t *size)
157 +{
158 +       struct inode *ea_inode = NULL;
159 +       int err;
160 +
161 +       ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
162 +       if (err)
163 +               return err;
164 +
165 +       err = ext4_xattr_inode_read(ea_inode, buffer, size);
166 +       iput(ea_inode);
167 +
168 +       return err;
169 +}
170 +
171  static int
172  ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
173                      void *buffer, size_t buffer_size)
174 @@ -236,7 +335,8 @@ bad_block:
175         }
176         ext4_xattr_cache_insert(bh);
177         entry = BFIRST(bh);
178 -       error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
179 +       error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1,
180 +                                     inode);
181         if (error == -EIO)
182                 goto bad_block;
183         if (error)
184 @@ -246,8 +346,16 @@ bad_block:
185                 error = -ERANGE;
186                 if (size > buffer_size)
187                         goto cleanup;
188 -               memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
189 -                      size);
190 +               if (entry->e_value_inum != 0) {
191 +                       error = ext4_xattr_inode_get(inode,
192 +                                            le32_to_cpu(entry->e_value_inum),
193 +                                            buffer, &size);
194 +                       if (error)
195 +                               goto cleanup;
196 +               } else {
197 +                       memcpy(buffer, bh->b_data +
198 +                              le16_to_cpu(entry->e_value_offs), size);
199 +               }
200         }
201         error = size;
202  
203 @@ -281,7 +389,7 @@ ext4_xattr_ibody_get(struct inode *inode
204         if (error)
205                 goto cleanup;
206         error = ext4_xattr_find_entry(&entry, name_index, name,
207 -                                     end - (void *)entry, 0);
208 +                                     end - (void *)entry, 0, inode);
209         if (error)
210                 goto cleanup;
211         size = le32_to_cpu(entry->e_value_size);
212 @@ -289,8 +397,16 @@ ext4_xattr_ibody_get(struct inode *inode
213                 error = -ERANGE;
214                 if (size > buffer_size)
215                         goto cleanup;
216 -               memcpy(buffer, (void *)IFIRST(header) +
217 -                      le16_to_cpu(entry->e_value_offs), size);
218 +               if (entry->e_value_inum != 0) {
219 +                       error = ext4_xattr_inode_get(inode,
220 +                                            le32_to_cpu(entry->e_value_inum),
221 +                                            buffer, &size);
222 +                       if (error)
223 +                               goto cleanup;
224 +               } else {
225 +                       memcpy(buffer, (void *)IFIRST(header) +
226 +                              le16_to_cpu(entry->e_value_offs), size);
227 +               }
228         }
229         error = size;
230  
231 @@ -512,7 +628,7 @@ static size_t ext4_xattr_free_space(stru
232  {
233         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
234                 *total += EXT4_XATTR_LEN(last->e_name_len);
235 -               if (!last->e_value_block && last->e_value_size) {
236 +               if (last->e_value_inum == 0 && last->e_value_size > 0) {
237                         size_t offs = le16_to_cpu(last->e_value_offs);
238                         if (offs < *min_offs)
239                                 *min_offs = offs;
240 @@ -521,11 +637,159 @@ static size_t ext4_xattr_free_space(stru
241         return (*min_offs - ((void *)last - base) - sizeof(__u32));
242  }
243  
244 +/*
245 + * Write the value of the EA in an inode.
246 + */
247 +static int
248 +ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
249 +                      const void *buf, int bufsize)
250 +{
251 +       struct buffer_head *bh = NULL, dummy;
252 +       unsigned long block = 0;
253 +       unsigned blocksize = ea_inode->i_sb->s_blocksize;
254 +       unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
255 +       int csize, wsize = 0;
256 +       int ret = 0;
257 +       int retries = 0;
258 +
259 +retry:
260 +       while (ret >= 0 && ret < max_blocks) {
261 +               block += ret;
262 +               max_blocks -= ret;
263 +
264 +               ret = ext4_get_blocks(handle, ea_inode, block, max_blocks,
265 +                                     &dummy, EXT4_GET_BLOCKS_CREATE);
266 +               if (ret <= 0) {
267 +                       ext4_mark_inode_dirty(handle, ea_inode);
268 +                       if (ret == -ENOSPC &&
269 +                           ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
270 +                               ret = 0;
271 +                               goto retry;
272 +                       }
273 +                       break;
274 +               }
275 +       }
276 +
277 +       if (ret < 0)
278 +               return ret;
279 +
280 +       block = 0;
281 +       while (wsize < bufsize) {
282 +               if (bh != NULL)
283 +                       brelse(bh);
284 +               csize = (bufsize - wsize) > blocksize ? blocksize :
285 +                                                               bufsize - wsize;
286 +               bh = ext4_getblk(handle, ea_inode, block, 0, &ret);
287 +               if (!bh)
288 +                       goto out;
289 +               ret = ext4_journal_get_write_access(handle, bh);
290 +               if (ret)
291 +                       goto out;
292 +
293 +               memcpy(bh->b_data, buf, csize);
294 +               set_buffer_uptodate(bh);
295 +               ext4_journal_dirty_metadata(handle, bh);
296 +
297 +               buf += csize;
298 +               wsize += csize;
299 +               block += 1;
300 +       }
301 +
302 +       i_size_write(ea_inode, wsize);
303 +       ext4_update_i_disksize(ea_inode, wsize);
304 +
305 +       ext4_mark_inode_dirty(handle, ea_inode);
306 +
307 +out:
308 +       brelse(bh);
309 +
310 +       return ret;
311 +}
312 +
313 +/*
314 + * Create an inode to store the value of a large EA.
315 + */
316 +static struct inode *
317 +ext4_xattr_inode_create(handle_t *handle, struct inode *inode)
318 +{
319 +       struct inode *ea_inode = NULL;
320 +
321 +       /*
322 +        * Let the next inode be the goal, so we try and allocate the EA inode
323 +        * in the same group, or nearby one.
324 +        */
325 +       ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
326 +                                 S_IFREG|0600, NULL, inode->i_ino + 1);
327 +
328 +       if (!IS_ERR(ea_inode)) {
329 +               ea_inode->i_op = &ext4_file_inode_operations;
330 +               ea_inode->i_fop = &ext4_file_operations;
331 +               ext4_set_aops(ea_inode);
332 +               ea_inode->i_generation = inode->i_generation;
333 +               EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
334 +
335 +               /*
336 +                * A back-pointer from EA inode to parent inode will be useful
337 +                * for e2fsck.
338 +                */
339 +               ea_inode->i_xattr_inode_parent = inode->i_ino;
340 +               unlock_new_inode(ea_inode);
341 +       }
342 +
343 +       return ea_inode;
344 +}
345 +
346 +/*
347 + * Unlink the inode storing the value of the EA.
348 + */
349 +static int
350 +ext4_xattr_inode_unlink(struct inode *inode, int ea_ino)
351 +{
352 +       struct inode *ea_inode = NULL;
353 +       int err;
354 +
355 +       ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
356 +       if (err)
357 +               return err;
358 +
359 +       ea_inode->i_nlink = 0;
360 +       iput(ea_inode);
361 +
362 +       return 0;
363 +}
364 +
365 +/*
366 + * Add value of the EA in an inode.
367 + */
368 +static int
369 +ext4_xattr_inode_set(handle_t *handle, struct inode *inode, int *ea_ino,
370 +                    const void *value, size_t value_len)
371 +{
372 +       struct inode *ea_inode = NULL;
373 +       int err;
374 +
375 +       /* Create an inode for the EA value */
376 +       ea_inode = ext4_xattr_inode_create(handle, inode);
377 +       if (IS_ERR(ea_inode))
378 +               return -1;
379 +
380 +       err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
381 +       if (err)
382 +               ea_inode->i_nlink = 0;
383 +       else
384 +               *ea_ino = ea_inode->i_ino;
385 +
386 +       iput(ea_inode);
387 +
388 +       return err;
389 +}
390 +
391  struct ext4_xattr_info {
392 -       int name_index;
393         const char *name;
394         const void *value;
395         size_t value_len;
396 +       int name_index;
397 +       int in_inode;
398  };
399  
400  struct ext4_xattr_search {
401 @@ -537,15 +801,23 @@ struct ext4_xattr_search {
402  };
403  
404  static int
405 -ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
406 +ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
407 +                    handle_t *handle, struct inode *inode)
408  {
409         struct ext4_xattr_entry *last;
410         size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
411 +       int in_inode = i->in_inode;
412 +
413 +       if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
414 +                EXT4_FEATURE_INCOMPAT_EA_INODE) &&
415 +           (EXT4_XATTR_SIZE(i->value_len) >
416 +            EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
417 +               in_inode = 1;
418  
419         /* Compute min_offs and last. */
420         last = s->first;
421         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
422 -               if (!last->e_value_block && last->e_value_size) {
423 +               if (last->e_value_inum == 0 && last->e_value_size > 0) {
424                         size_t offs = le16_to_cpu(last->e_value_offs);
425                         if (offs < min_offs)
426                                 min_offs = offs;
427 @@ -553,16 +825,21 @@ ext4_xattr_set_entry(struct ext4_xattr_i
428         }
429         free = min_offs - ((void *)last - s->base) - sizeof(__u32);
430         if (!s->not_found) {
431 -               if (!s->here->e_value_block && s->here->e_value_size) {
432 +               if (!in_inode && s->here->e_value_inum == 0 &&
433 +                   s->here->e_value_size > 0) {
434                         size_t size = le32_to_cpu(s->here->e_value_size);
435                         free += EXT4_XATTR_SIZE(size);
436                 }
437                 free += EXT4_XATTR_LEN(name_len);
438         }
439         if (i->value) {
440 -               if (free < EXT4_XATTR_SIZE(i->value_len) ||
441 -                   free < EXT4_XATTR_LEN(name_len) +
442 -                          EXT4_XATTR_SIZE(i->value_len))
443 +               size_t value_len = EXT4_XATTR_SIZE(i->value_len);
444 +
445 +               if (in_inode)
446 +                       value_len = 0;
447 +
448 +               if (free < value_len ||
449 +                   free < EXT4_XATTR_LEN(name_len) + value_len)
450                         return -ENOSPC;
451         }
452  
453 @@ -576,7 +853,8 @@ ext4_xattr_set_entry(struct ext4_xattr_i
454                 s->here->e_name_len = name_len;
455                 memcpy(s->here->e_name, i->name, name_len);
456         } else {
457 -               if (!s->here->e_value_block && s->here->e_value_size) {
458 +               if (s->here->e_value_offs > 0 && s->here->e_value_inum == 0 &&
459 +                   s->here->e_value_size > 0) {
460                         void *first_val = s->base + min_offs;
461                         size_t offs = le16_to_cpu(s->here->e_value_offs);
462                         void *val = s->base + offs;
463 @@ -605,13 +883,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
464                         last = s->first;
465                         while (!IS_LAST_ENTRY(last)) {
466                                 size_t o = le16_to_cpu(last->e_value_offs);
467 -                               if (!last->e_value_block &&
468 -                                   last->e_value_size && o < offs)
469 +                               if (last->e_value_size > 0 && o < offs)
470                                         last->e_value_offs =
471                                                 cpu_to_le16(o + size);
472                                 last = EXT4_XATTR_NEXT(last);
473                         }
474                 }
475 +               if (s->here->e_value_inum != 0) {
476 +                       ext4_xattr_inode_unlink(inode,
477 +                                       le32_to_cpu(s->here->e_value_inum));
478 +                       s->here->e_value_inum = 0;
479 +               }
480                 if (!i->value) {
481                         /* Remove the old name. */
482                         size_t size = EXT4_XATTR_LEN(name_len);
483 @@ -625,10 +907,17 @@ ext4_xattr_set_entry(struct ext4_xattr_i
484         if (i->value) {
485                 /* Insert the new value. */
486                 s->here->e_value_size = cpu_to_le32(i->value_len);
487 -               if (i->value_len) {
488 +               if (in_inode) {
489 +                       int ea_ino = le32_to_cpu(s->here->e_value_inum);
490 +                       ext4_xattr_inode_set(handle, inode, &ea_ino, i->value,
491 +                                            i->value_len);
492 +                       s->here->e_value_inum = cpu_to_le32(ea_ino);
493 +                       s->here->e_value_offs = 0;
494 +               } else if (i->value_len) {
495                         size_t size = EXT4_XATTR_SIZE(i->value_len);
496                         void *val = s->base + min_offs - size;
497                         s->here->e_value_offs = cpu_to_le16(min_offs - size);
498 +                       s->here->e_value_inum = 0;
499                         memset(val + size - EXT4_XATTR_PAD, 0,
500                                EXT4_XATTR_PAD); /* Clear the pad bytes. */
501                         memcpy(val, i->value, i->value_len);
502 @@ -673,7 +962,7 @@ ext4_xattr_block_find(struct inode *inod
503                 bs->s.end = bs->bh->b_data + bs->bh->b_size;
504                 bs->s.here = bs->s.first;
505                 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
506 -                                             i->name, bs->bh->b_size, 1);
507 +                                            i->name, bs->bh->b_size, 1, inode);
508                 if (error && error != -ENODATA)
509                         goto cleanup;
510                 bs->s.not_found = error;
511 @@ -697,8 +986,6 @@ ext4_xattr_block_set(handle_t *handle, s
512  
513  #define header(x) ((struct ext4_xattr_header *)(x))
514  
515 -       if (i->value && i->value_len > sb->s_blocksize)
516 -               return -ENOSPC;
517         if (s->base) {
518                 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
519                                         bs->bh->b_blocknr);
520 @@ -713,7 +1000,7 @@ ext4_xattr_block_set(handle_t *handle, s
521                                 ce = NULL;
522                         }
523                         ea_bdebug(bs->bh, "modifying in-place");
524 -                       error = ext4_xattr_set_entry(i, s);
525 +                       error = ext4_xattr_set_entry(i, s, handle, inode);
526                         if (!error) {
527                                 if (!IS_LAST_ENTRY(s->first))
528                                         ext4_xattr_rehash(header(s->base),
529 @@ -765,7 +1052,7 @@ ext4_xattr_block_set(handle_t *handle, s
530                 s->end = s->base + sb->s_blocksize;
531         }
532  
533 -       error = ext4_xattr_set_entry(i, s);
534 +       error = ext4_xattr_set_entry(i, s, handle, inode);
535         if (error == -EIO)
536                 goto bad_block;
537         if (error)
538 @@ -909,7 +1196,7 @@ ext4_xattr_ibody_find(struct inode *inod
539                 /* Find the named attribute. */
540                 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
541                                               i->name, is->s.end -
542 -                                             (void *)is->s.base, 0);
543 +                                             (void *)is->s.base, 0, inode);
544                 if (error && error != -ENODATA)
545                         return error;
546                 is->s.not_found = error;
547 @@ -928,7 +1215,7 @@ ext4_xattr_ibody_set(handle_t *handle, s
548  
549         if (EXT4_I(inode)->i_extra_isize == 0)
550                 return -ENOSPC;
551 -       error = ext4_xattr_set_entry(i, s);
552 +       error = ext4_xattr_set_entry(i, s, handle, inode);
553         if (error)
554                 return error;
555         header = IHDR(inode, ext4_raw_inode(&is->iloc));
556 @@ -964,7 +1251,7 @@ ext4_xattr_set_handle(handle_t *handle, 
557                 .name = name,
558                 .value = value,
559                 .value_len = value_len,
560 -
561 +               .in_inode = 0,
562         };
563         struct ext4_xattr_ibody_find is = {
564                 .s = { .not_found = -ENODATA, },
565 @@ -1033,6 +1320,15 @@ ext4_xattr_set_handle(handle_t *handle, 
566                                         goto cleanup;
567                         }
568                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
569 +                       if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb,
570 +                                       EXT4_FEATURE_INCOMPAT_EA_INODE) &&
571 +                           error == -ENOSPC) {
572 +                               /* xattr not fit to block, store at external
573 +                                * inode */
574 +                               i.in_inode = 1;
575 +                               error = ext4_xattr_ibody_set(handle, inode,
576 +                                                            &i, &is);
577 +                       }
578                         if (error)
579                                 goto cleanup;
580                         if (!is.s.not_found) {
581 @@ -1080,10 +1376,25 @@ ext4_xattr_set(struct inode *inode, int 
582                const void *value, size_t value_len, int flags)
583  {
584         handle_t *handle;
585 +       struct super_block *sb = inode->i_sb;
586 +       int buffer_credits;
587         int error, retries = 0;
588  
589 +       buffer_credits = EXT4_DATA_TRANS_BLOCKS(sb);
590 +       if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
591 +           EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EA_INODE)) {
592 +               int nrblocks = (value_len + sb->s_blocksize - 1) >>
593 +                                       sb->s_blocksize_bits;
594 +
595 +               /* For new inode */
596 +               buffer_credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
597 +
598 +               /* For data blocks of EA inode */
599 +               buffer_credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
600 +       }
601 +
602  retry:
603 -       handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
604 +       handle = ext4_journal_start(inode, buffer_credits);
605         if (IS_ERR(handle)) {
606                 error = PTR_ERR(handle);
607         } else {
608 @@ -1093,7 +1404,7 @@ retry:
609                                               value, value_len, flags);
610                 error2 = ext4_journal_stop(handle);
611                 if (error == -ENOSPC &&
612 -                   ext4_should_retry_alloc(inode->i_sb, &retries))
613 +                   ext4_should_retry_alloc(sb, &retries))
614                         goto retry;
615                 if (error == 0)
616                         error = error2;
617 @@ -1115,7 +1426,7 @@ static void ext4_xattr_shift_entries(str
618  
619         /* Adjust the value offsets of the entries */
620         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
621 -               if (!last->e_value_block && last->e_value_size) {
622 +               if (last->e_value_inum == 0 && last->e_value_size > 0) {
623                         new_offs = le16_to_cpu(last->e_value_offs) +
624                                                         value_offs_shift;
625                         BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
626 @@ -1353,15 +1664,41 @@ cleanup:
627  /*
628   * ext4_xattr_delete_inode()
629   *
630 - * Free extended attribute resources associated with this inode. This
631 + * Free extended attribute resources associated with this inode. Traverse
632 + * all entries and unlink any xattr inodes associated with this inode. This
633   * is called immediately before an inode is freed. We have exclusive
634 - * access to the inode.
635 + * access to the inode. If an orphan inode is deleted it will also delete any
636 + * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
637 + * to ensure they belong to the parent inode and were not deleted already.
638   */
639  void
640  ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
641  {
642         struct buffer_head *bh = NULL;
643 +       struct ext4_xattr_ibody_header *header;
644 +       struct ext4_inode *raw_inode;
645 +       struct ext4_iloc iloc;
646 +       struct ext4_xattr_entry *entry;
647 +       int error;
648 +
649 +       if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
650 +               goto delete_external_ea;
651 +
652 +       error = ext4_get_inode_loc(inode, &iloc);
653 +       if (error)
654 +               goto cleanup;
655 +       raw_inode = ext4_raw_inode(&iloc);
656 +       header = IHDR(inode, raw_inode);
657 +       entry = IFIRST(header);
658 +       for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
659 +               if (entry->e_value_inum != 0) {
660 +                       ext4_xattr_inode_unlink(inode,
661 +                                       le32_to_cpu(entry->e_value_inum));
662 +                       entry->e_value_inum = 0;
663 +               }
664 +       }
665  
666 +delete_external_ea:
667         if (!EXT4_I(inode)->i_file_acl)
668                 goto cleanup;
669         bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
670 @@ -1376,6 +1713,16 @@ ext4_xattr_delete_inode(handle_t *handle
671                            inode->i_ino, EXT4_I(inode)->i_file_acl);
672                 goto cleanup;
673         }
674 +
675 +       entry = BFIRST(bh);
676 +       for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
677 +               if (entry->e_value_inum != 0) {
678 +                       ext4_xattr_inode_unlink(inode,
679 +                                       le32_to_cpu(entry->e_value_inum));
680 +                       entry->e_value_inum = 0;
681 +               }
682 +       }
683 +
684         ext4_xattr_release_block(handle, inode, bh);
685         EXT4_I(inode)->i_file_acl = 0;
686  
687 @@ -1450,10 +1797,9 @@ ext4_xattr_cmp(struct ext4_xattr_header 
688                     entry1->e_name_index != entry2->e_name_index ||
689                     entry1->e_name_len != entry2->e_name_len ||
690                     entry1->e_value_size != entry2->e_value_size ||
691 +                   entry1->e_value_inum != entry2->e_value_inum ||
692                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
693                         return 1;
694 -               if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
695 -                       return -EIO;
696                 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
697                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
698                            le32_to_cpu(entry1->e_value_size)))
699 @@ -1538,7 +1884,7 @@ static inline void ext4_xattr_hash_entry
700                        *name++;
701         }
702  
703 -       if (entry->e_value_block == 0 && entry->e_value_size != 0) {
704 +       if (entry->e_value_inum == 0 && entry->e_value_size != 0) {
705                 __le32 *value = (__le32 *)((char *)header +
706                         le16_to_cpu(entry->e_value_offs));
707                 for (n = (le32_to_cpu(entry->e_value_size) +
708 Index: linux-stage/fs/ext4/xattr.h
709 ===================================================================
710 --- linux-stage.orig/fs/ext4/xattr.h
711 +++ linux-stage/fs/ext4/xattr.h
712 @@ -38,7 +38,7 @@ struct ext4_xattr_entry {
713         __u8    e_name_len;     /* length of name */
714         __u8    e_name_index;   /* attribute name index */
715         __le16  e_value_offs;   /* offset in disk block of value */
716 -       __le32  e_value_block;  /* disk block attribute is stored on (n/i) */
717 +       __le32  e_value_inum;   /* inode in which the value is stored */
718         __le32  e_value_size;   /* size of attribute value */
719         __le32  e_hash;         /* hash value of name and value */
720         char    e_name[0];      /* attribute name */
721 @@ -63,6 +63,15 @@ struct ext4_xattr_entry {
722                 EXT4_I(inode)->i_extra_isize))
723  #define IFIRST(hdr) ((struct ext4_xattr_entry *)((hdr)+1))
724  
725 +#define i_xattr_inode_parent i_mtime.tv_sec
726 +
727 +/*
728 + * The minimum size of EA value when you start storing it in an external inode
729 + * size of block - size of header - size of 1 entry - 4 null bytes
730 +*/
731 +#define EXT4_XATTR_MIN_LARGE_EA_SIZE(b)                                        \
732 +       ((b) - EXT4_XATTR_LEN(3) - sizeof(struct ext4_xattr_header) - 4)
733 +
734  # ifdef CONFIG_EXT4_FS_XATTR
735  
736  extern struct xattr_handler ext4_xattr_user_handler;