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