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