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