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