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