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