eaed9fd2f890c5e6104ca8a92de143fe5ef73e13
[releases.git] / xattr.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * linux/fs/ext4/xattr.c
4  *
5  * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6  *
7  * Fix by Harrison Xing <harrison@mountainviewdata.com>.
8  * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
9  * Extended attributes for symlinks and special files added per
10  *  suggestion of Luka Renko <luka.renko@hermes.si>.
11  * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12  *  Red Hat Inc.
13  * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
14  *  and Andreas Gruenbacher <agruen@suse.de>.
15  */
16
17 /*
18  * Extended attributes are stored directly in inodes (on file systems with
19  * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
20  * field contains the block number if an inode uses an additional block. All
21  * attributes must fit in the inode and one additional block. Blocks that
22  * contain the identical set of attributes may be shared among several inodes.
23  * Identical blocks are detected by keeping a cache of blocks that have
24  * recently been accessed.
25  *
26  * The attributes in inodes and on blocks have a different header; the entries
27  * are stored in the same format:
28  *
29  *   +------------------+
30  *   | header           |
31  *   | entry 1          | |
32  *   | entry 2          | | growing downwards
33  *   | entry 3          | v
34  *   | four null bytes  |
35  *   | . . .            |
36  *   | value 1          | ^
37  *   | value 3          | | growing upwards
38  *   | value 2          | |
39  *   +------------------+
40  *
41  * The header is followed by multiple entry descriptors. In disk blocks, the
42  * entry descriptors are kept sorted. In inodes, they are unsorted. The
43  * attribute values are aligned to the end of the block in no specific order.
44  *
45  * Locking strategy
46  * ----------------
47  * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
48  * EA blocks are only changed if they are exclusive to an inode, so
49  * holding xattr_sem also means that nothing but the EA block's reference
50  * count can change. Multiple writers to the same block are synchronized
51  * by the buffer lock.
52  */
53
54 #include <linux/init.h>
55 #include <linux/fs.h>
56 #include <linux/slab.h>
57 #include <linux/mbcache.h>
58 #include <linux/quotaops.h>
59 #include <linux/iversion.h>
60 #include "ext4_jbd2.h"
61 #include "ext4.h"
62 #include "xattr.h"
63 #include "acl.h"
64
65 #ifdef EXT4_XATTR_DEBUG
66 # define ea_idebug(inode, fmt, ...)                                     \
67         printk(KERN_DEBUG "inode %s:%lu: " fmt "\n",                    \
68                inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
69 # define ea_bdebug(bh, fmt, ...)                                        \
70         printk(KERN_DEBUG "block %pg:%lu: " fmt "\n",                   \
71                bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
72 #else
73 # define ea_idebug(inode, fmt, ...)     no_printk(fmt, ##__VA_ARGS__)
74 # define ea_bdebug(bh, fmt, ...)        no_printk(fmt, ##__VA_ARGS__)
75 #endif
76
77 static void ext4_xattr_block_cache_insert(struct mb_cache *,
78                                           struct buffer_head *);
79 static struct buffer_head *
80 ext4_xattr_block_cache_find(struct inode *, struct ext4_xattr_header *,
81                             struct mb_cache_entry **);
82 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
83                                     size_t value_count);
84 static void ext4_xattr_rehash(struct ext4_xattr_header *);
85
86 static const struct xattr_handler * const ext4_xattr_handler_map[] = {
87         [EXT4_XATTR_INDEX_USER]              = &ext4_xattr_user_handler,
88 #ifdef CONFIG_EXT4_FS_POSIX_ACL
89         [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS]  = &posix_acl_access_xattr_handler,
90         [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
91 #endif
92         [EXT4_XATTR_INDEX_TRUSTED]           = &ext4_xattr_trusted_handler,
93 #ifdef CONFIG_EXT4_FS_SECURITY
94         [EXT4_XATTR_INDEX_SECURITY]          = &ext4_xattr_security_handler,
95 #endif
96         [EXT4_XATTR_INDEX_HURD]              = &ext4_xattr_hurd_handler,
97 };
98
99 const struct xattr_handler *ext4_xattr_handlers[] = {
100         &ext4_xattr_user_handler,
101         &ext4_xattr_trusted_handler,
102 #ifdef CONFIG_EXT4_FS_POSIX_ACL
103         &posix_acl_access_xattr_handler,
104         &posix_acl_default_xattr_handler,
105 #endif
106 #ifdef CONFIG_EXT4_FS_SECURITY
107         &ext4_xattr_security_handler,
108 #endif
109         &ext4_xattr_hurd_handler,
110         NULL
111 };
112
113 #define EA_BLOCK_CACHE(inode)   (((struct ext4_sb_info *) \
114                                 inode->i_sb->s_fs_info)->s_ea_block_cache)
115
116 #define EA_INODE_CACHE(inode)   (((struct ext4_sb_info *) \
117                                 inode->i_sb->s_fs_info)->s_ea_inode_cache)
118
119 static int
120 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
121                         struct inode *inode);
122
123 #ifdef CONFIG_LOCKDEP
124 void ext4_xattr_inode_set_class(struct inode *ea_inode)
125 {
126         struct ext4_inode_info *ei = EXT4_I(ea_inode);
127
128         lockdep_set_subclass(&ea_inode->i_rwsem, 1);
129         (void) ei;      /* shut up clang warning if !CONFIG_LOCKDEP */
130         lockdep_set_subclass(&ei->i_data_sem, I_DATA_SEM_EA);
131 }
132 #endif
133
134 static __le32 ext4_xattr_block_csum(struct inode *inode,
135                                     sector_t block_nr,
136                                     struct ext4_xattr_header *hdr)
137 {
138         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
139         __u32 csum;
140         __le64 dsk_block_nr = cpu_to_le64(block_nr);
141         __u32 dummy_csum = 0;
142         int offset = offsetof(struct ext4_xattr_header, h_checksum);
143
144         csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
145                            sizeof(dsk_block_nr));
146         csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
147         csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
148         offset += sizeof(dummy_csum);
149         csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
150                            EXT4_BLOCK_SIZE(inode->i_sb) - offset);
151
152         return cpu_to_le32(csum);
153 }
154
155 static int ext4_xattr_block_csum_verify(struct inode *inode,
156                                         struct buffer_head *bh)
157 {
158         struct ext4_xattr_header *hdr = BHDR(bh);
159         int ret = 1;
160
161         if (ext4_has_metadata_csum(inode->i_sb)) {
162                 lock_buffer(bh);
163                 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
164                                                         bh->b_blocknr, hdr));
165                 unlock_buffer(bh);
166         }
167         return ret;
168 }
169
170 static void ext4_xattr_block_csum_set(struct inode *inode,
171                                       struct buffer_head *bh)
172 {
173         if (ext4_has_metadata_csum(inode->i_sb))
174                 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
175                                                 bh->b_blocknr, BHDR(bh));
176 }
177
178 static inline const struct xattr_handler *
179 ext4_xattr_handler(int name_index)
180 {
181         const struct xattr_handler *handler = NULL;
182
183         if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
184                 handler = ext4_xattr_handler_map[name_index];
185         return handler;
186 }
187
188 static int
189 ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
190                          void *value_start)
191 {
192         struct ext4_xattr_entry *e = entry;
193
194         /* Find the end of the names list */
195         while (!IS_LAST_ENTRY(e)) {
196                 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
197                 if ((void *)next >= end)
198                         return -EFSCORRUPTED;
199                 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
200                         return -EFSCORRUPTED;
201                 e = next;
202         }
203
204         /* Check the values */
205         while (!IS_LAST_ENTRY(entry)) {
206                 u32 size = le32_to_cpu(entry->e_value_size);
207
208                 if (size > EXT4_XATTR_SIZE_MAX)
209                         return -EFSCORRUPTED;
210
211                 if (size != 0 && entry->e_value_inum == 0) {
212                         u16 offs = le16_to_cpu(entry->e_value_offs);
213                         void *value;
214
215                         /*
216                          * The value cannot overlap the names, and the value
217                          * with padding cannot extend beyond 'end'.  Check both
218                          * the padded and unpadded sizes, since the size may
219                          * overflow to 0 when adding padding.
220                          */
221                         if (offs > end - value_start)
222                                 return -EFSCORRUPTED;
223                         value = value_start + offs;
224                         if (value < (void *)e + sizeof(u32) ||
225                             size > end - value ||
226                             EXT4_XATTR_SIZE(size) > end - value)
227                                 return -EFSCORRUPTED;
228                 }
229                 entry = EXT4_XATTR_NEXT(entry);
230         }
231
232         return 0;
233 }
234
235 static inline int
236 __ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh,
237                          const char *function, unsigned int line)
238 {
239         int error = -EFSCORRUPTED;
240
241         if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
242             BHDR(bh)->h_blocks != cpu_to_le32(1))
243                 goto errout;
244         if (buffer_verified(bh))
245                 return 0;
246
247         error = -EFSBADCRC;
248         if (!ext4_xattr_block_csum_verify(inode, bh))
249                 goto errout;
250         error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
251                                          bh->b_data);
252 errout:
253         if (error)
254                 __ext4_error_inode(inode, function, line, 0, -error,
255                                    "corrupted xattr block %llu",
256                                    (unsigned long long) bh->b_blocknr);
257         else
258                 set_buffer_verified(bh);
259         return error;
260 }
261
262 #define ext4_xattr_check_block(inode, bh) \
263         __ext4_xattr_check_block((inode), (bh),  __func__, __LINE__)
264
265
266 static int
267 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
268                          void *end, const char *function, unsigned int line)
269 {
270         int error = -EFSCORRUPTED;
271
272         if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
273             (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
274                 goto errout;
275         error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
276 errout:
277         if (error)
278                 __ext4_error_inode(inode, function, line, 0, -error,
279                                    "corrupted in-inode xattr");
280         return error;
281 }
282
283 #define xattr_check_inode(inode, header, end) \
284         __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
285
286 static int
287 xattr_find_entry(struct inode *inode, struct ext4_xattr_entry **pentry,
288                  void *end, int name_index, const char *name, int sorted)
289 {
290         struct ext4_xattr_entry *entry, *next;
291         size_t name_len;
292         int cmp = 1;
293
294         if (name == NULL)
295                 return -EINVAL;
296         name_len = strlen(name);
297         for (entry = *pentry; !IS_LAST_ENTRY(entry); entry = next) {
298                 next = EXT4_XATTR_NEXT(entry);
299                 if ((void *) next >= end) {
300                         EXT4_ERROR_INODE(inode, "corrupted xattr entries");
301                         return -EFSCORRUPTED;
302                 }
303                 cmp = name_index - entry->e_name_index;
304                 if (!cmp)
305                         cmp = name_len - entry->e_name_len;
306                 if (!cmp)
307                         cmp = memcmp(name, entry->e_name, name_len);
308                 if (cmp <= 0 && (sorted || cmp == 0))
309                         break;
310         }
311         *pentry = entry;
312         return cmp ? -ENODATA : 0;
313 }
314
315 static u32
316 ext4_xattr_inode_hash(struct ext4_sb_info *sbi, const void *buffer, size_t size)
317 {
318         return ext4_chksum(sbi, sbi->s_csum_seed, buffer, size);
319 }
320
321 static u64 ext4_xattr_inode_get_ref(struct inode *ea_inode)
322 {
323         return ((u64)ea_inode->i_ctime.tv_sec << 32) |
324                 (u32) inode_peek_iversion_raw(ea_inode);
325 }
326
327 static void ext4_xattr_inode_set_ref(struct inode *ea_inode, u64 ref_count)
328 {
329         ea_inode->i_ctime.tv_sec = (u32)(ref_count >> 32);
330         inode_set_iversion_raw(ea_inode, ref_count & 0xffffffff);
331 }
332
333 static u32 ext4_xattr_inode_get_hash(struct inode *ea_inode)
334 {
335         return (u32)ea_inode->i_atime.tv_sec;
336 }
337
338 static void ext4_xattr_inode_set_hash(struct inode *ea_inode, u32 hash)
339 {
340         ea_inode->i_atime.tv_sec = hash;
341 }
342
343 /*
344  * Read the EA value from an inode.
345  */
346 static int ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t size)
347 {
348         int blocksize = 1 << ea_inode->i_blkbits;
349         int bh_count = (size + blocksize - 1) >> ea_inode->i_blkbits;
350         int tail_size = (size % blocksize) ?: blocksize;
351         struct buffer_head *bhs_inline[8];
352         struct buffer_head **bhs = bhs_inline;
353         int i, ret;
354
355         if (bh_count > ARRAY_SIZE(bhs_inline)) {
356                 bhs = kmalloc_array(bh_count, sizeof(*bhs), GFP_NOFS);
357                 if (!bhs)
358                         return -ENOMEM;
359         }
360
361         ret = ext4_bread_batch(ea_inode, 0 /* block */, bh_count,
362                                true /* wait */, bhs);
363         if (ret)
364                 goto free_bhs;
365
366         for (i = 0; i < bh_count; i++) {
367                 /* There shouldn't be any holes in ea_inode. */
368                 if (!bhs[i]) {
369                         ret = -EFSCORRUPTED;
370                         goto put_bhs;
371                 }
372                 memcpy((char *)buf + blocksize * i, bhs[i]->b_data,
373                        i < bh_count - 1 ? blocksize : tail_size);
374         }
375         ret = 0;
376 put_bhs:
377         for (i = 0; i < bh_count; i++)
378                 brelse(bhs[i]);
379 free_bhs:
380         if (bhs != bhs_inline)
381                 kfree(bhs);
382         return ret;
383 }
384
385 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
386
387 static int ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino,
388                                  u32 ea_inode_hash, struct inode **ea_inode)
389 {
390         struct inode *inode;
391         int err;
392
393         /*
394          * We have to check for this corruption early as otherwise
395          * iget_locked() could wait indefinitely for the state of our
396          * parent inode.
397          */
398         if (parent->i_ino == ea_ino) {
399                 ext4_error(parent->i_sb,
400                            "Parent and EA inode have the same ino %lu", ea_ino);
401                 return -EFSCORRUPTED;
402         }
403
404         inode = ext4_iget(parent->i_sb, ea_ino, EXT4_IGET_EA_INODE);
405         if (IS_ERR(inode)) {
406                 err = PTR_ERR(inode);
407                 ext4_error(parent->i_sb,
408                            "error while reading EA inode %lu err=%d", ea_ino,
409                            err);
410                 return err;
411         }
412         ext4_xattr_inode_set_class(inode);
413
414         /*
415          * Check whether this is an old Lustre-style xattr inode. Lustre
416          * implementation does not have hash validation, rather it has a
417          * backpointer from ea_inode to the parent inode.
418          */
419         if (ea_inode_hash != ext4_xattr_inode_get_hash(inode) &&
420             EXT4_XATTR_INODE_GET_PARENT(inode) == parent->i_ino &&
421             inode->i_generation == parent->i_generation) {
422                 ext4_set_inode_state(inode, EXT4_STATE_LUSTRE_EA_INODE);
423                 ext4_xattr_inode_set_ref(inode, 1);
424         } else {
425                 inode_lock(inode);
426                 inode->i_flags |= S_NOQUOTA;
427                 inode_unlock(inode);
428         }
429
430         *ea_inode = inode;
431         return 0;
432 }
433
434 /* Remove entry from mbcache when EA inode is getting evicted */
435 void ext4_evict_ea_inode(struct inode *inode)
436 {
437         struct mb_cache_entry *oe;
438
439         if (!EA_INODE_CACHE(inode))
440                 return;
441         /* Wait for entry to get unused so that we can remove it */
442         while ((oe = mb_cache_entry_delete_or_get(EA_INODE_CACHE(inode),
443                         ext4_xattr_inode_get_hash(inode), inode->i_ino))) {
444                 mb_cache_entry_wait_unused(oe);
445                 mb_cache_entry_put(EA_INODE_CACHE(inode), oe);
446         }
447 }
448
449 static int
450 ext4_xattr_inode_verify_hashes(struct inode *ea_inode,
451                                struct ext4_xattr_entry *entry, void *buffer,
452                                size_t size)
453 {
454         u32 hash;
455
456         /* Verify stored hash matches calculated hash. */
457         hash = ext4_xattr_inode_hash(EXT4_SB(ea_inode->i_sb), buffer, size);
458         if (hash != ext4_xattr_inode_get_hash(ea_inode))
459                 return -EFSCORRUPTED;
460
461         if (entry) {
462                 __le32 e_hash, tmp_data;
463
464                 /* Verify entry hash. */
465                 tmp_data = cpu_to_le32(hash);
466                 e_hash = ext4_xattr_hash_entry(entry->e_name, entry->e_name_len,
467                                                &tmp_data, 1);
468                 if (e_hash != entry->e_hash)
469                         return -EFSCORRUPTED;
470         }
471         return 0;
472 }
473
474 /*
475  * Read xattr value from the EA inode.
476  */
477 static int
478 ext4_xattr_inode_get(struct inode *inode, struct ext4_xattr_entry *entry,
479                      void *buffer, size_t size)
480 {
481         struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
482         struct inode *ea_inode;
483         int err;
484
485         err = ext4_xattr_inode_iget(inode, le32_to_cpu(entry->e_value_inum),
486                                     le32_to_cpu(entry->e_hash), &ea_inode);
487         if (err) {
488                 ea_inode = NULL;
489                 goto out;
490         }
491
492         if (i_size_read(ea_inode) != size) {
493                 ext4_warning_inode(ea_inode,
494                                    "ea_inode file size=%llu entry size=%zu",
495                                    i_size_read(ea_inode), size);
496                 err = -EFSCORRUPTED;
497                 goto out;
498         }
499
500         err = ext4_xattr_inode_read(ea_inode, buffer, size);
501         if (err)
502                 goto out;
503
504         if (!ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE)) {
505                 err = ext4_xattr_inode_verify_hashes(ea_inode, entry, buffer,
506                                                      size);
507                 if (err) {
508                         ext4_warning_inode(ea_inode,
509                                            "EA inode hash validation failed");
510                         goto out;
511                 }
512
513                 if (ea_inode_cache)
514                         mb_cache_entry_create(ea_inode_cache, GFP_NOFS,
515                                         ext4_xattr_inode_get_hash(ea_inode),
516                                         ea_inode->i_ino, true /* reusable */);
517         }
518 out:
519         iput(ea_inode);
520         return err;
521 }
522
523 static int
524 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
525                      void *buffer, size_t buffer_size)
526 {
527         struct buffer_head *bh = NULL;
528         struct ext4_xattr_entry *entry;
529         size_t size;
530         void *end;
531         int error;
532         struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
533
534         ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
535                   name_index, name, buffer, (long)buffer_size);
536
537         if (!EXT4_I(inode)->i_file_acl)
538                 return -ENODATA;
539         ea_idebug(inode, "reading block %llu",
540                   (unsigned long long)EXT4_I(inode)->i_file_acl);
541         bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
542         if (IS_ERR(bh))
543                 return PTR_ERR(bh);
544         ea_bdebug(bh, "b_count=%d, refcount=%d",
545                 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
546         error = ext4_xattr_check_block(inode, bh);
547         if (error)
548                 goto cleanup;
549         ext4_xattr_block_cache_insert(ea_block_cache, bh);
550         entry = BFIRST(bh);
551         end = bh->b_data + bh->b_size;
552         error = xattr_find_entry(inode, &entry, end, name_index, name, 1);
553         if (error)
554                 goto cleanup;
555         size = le32_to_cpu(entry->e_value_size);
556         error = -ERANGE;
557         if (unlikely(size > EXT4_XATTR_SIZE_MAX))
558                 goto cleanup;
559         if (buffer) {
560                 if (size > buffer_size)
561                         goto cleanup;
562                 if (entry->e_value_inum) {
563                         error = ext4_xattr_inode_get(inode, entry, buffer,
564                                                      size);
565                         if (error)
566                                 goto cleanup;
567                 } else {
568                         u16 offset = le16_to_cpu(entry->e_value_offs);
569                         void *p = bh->b_data + offset;
570
571                         if (unlikely(p + size > end))
572                                 goto cleanup;
573                         memcpy(buffer, p, size);
574                 }
575         }
576         error = size;
577
578 cleanup:
579         brelse(bh);
580         return error;
581 }
582
583 int
584 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
585                      void *buffer, size_t buffer_size)
586 {
587         struct ext4_xattr_ibody_header *header;
588         struct ext4_xattr_entry *entry;
589         struct ext4_inode *raw_inode;
590         struct ext4_iloc iloc;
591         size_t size;
592         void *end;
593         int error;
594
595         if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
596                 return -ENODATA;
597         error = ext4_get_inode_loc(inode, &iloc);
598         if (error)
599                 return error;
600         raw_inode = ext4_raw_inode(&iloc);
601         header = IHDR(inode, raw_inode);
602         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
603         error = xattr_check_inode(inode, header, end);
604         if (error)
605                 goto cleanup;
606         entry = IFIRST(header);
607         error = xattr_find_entry(inode, &entry, end, name_index, name, 0);
608         if (error)
609                 goto cleanup;
610         size = le32_to_cpu(entry->e_value_size);
611         error = -ERANGE;
612         if (unlikely(size > EXT4_XATTR_SIZE_MAX))
613                 goto cleanup;
614         if (buffer) {
615                 if (size > buffer_size)
616                         goto cleanup;
617                 if (entry->e_value_inum) {
618                         error = ext4_xattr_inode_get(inode, entry, buffer,
619                                                      size);
620                         if (error)
621                                 goto cleanup;
622                 } else {
623                         u16 offset = le16_to_cpu(entry->e_value_offs);
624                         void *p = (void *)IFIRST(header) + offset;
625
626                         if (unlikely(p + size > end))
627                                 goto cleanup;
628                         memcpy(buffer, p, size);
629                 }
630         }
631         error = size;
632
633 cleanup:
634         brelse(iloc.bh);
635         return error;
636 }
637
638 /*
639  * ext4_xattr_get()
640  *
641  * Copy an extended attribute into the buffer
642  * provided, or compute the buffer size required.
643  * Buffer is NULL to compute the size of the buffer required.
644  *
645  * Returns a negative error number on failure, or the number of bytes
646  * used / required on success.
647  */
648 int
649 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
650                void *buffer, size_t buffer_size)
651 {
652         int error;
653
654         if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
655                 return -EIO;
656
657         if (strlen(name) > 255)
658                 return -ERANGE;
659
660         down_read(&EXT4_I(inode)->xattr_sem);
661         error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
662                                      buffer_size);
663         if (error == -ENODATA)
664                 error = ext4_xattr_block_get(inode, name_index, name, buffer,
665                                              buffer_size);
666         up_read(&EXT4_I(inode)->xattr_sem);
667         return error;
668 }
669
670 static int
671 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
672                         char *buffer, size_t buffer_size)
673 {
674         size_t rest = buffer_size;
675
676         for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
677                 const struct xattr_handler *handler =
678                         ext4_xattr_handler(entry->e_name_index);
679
680                 if (handler && (!handler->list || handler->list(dentry))) {
681                         const char *prefix = handler->prefix ?: handler->name;
682                         size_t prefix_len = strlen(prefix);
683                         size_t size = prefix_len + entry->e_name_len + 1;
684
685                         if (buffer) {
686                                 if (size > rest)
687                                         return -ERANGE;
688                                 memcpy(buffer, prefix, prefix_len);
689                                 buffer += prefix_len;
690                                 memcpy(buffer, entry->e_name, entry->e_name_len);
691                                 buffer += entry->e_name_len;
692                                 *buffer++ = 0;
693                         }
694                         rest -= size;
695                 }
696         }
697         return buffer_size - rest;  /* total size */
698 }
699
700 static int
701 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
702 {
703         struct inode *inode = d_inode(dentry);
704         struct buffer_head *bh = NULL;
705         int error;
706
707         ea_idebug(inode, "buffer=%p, buffer_size=%ld",
708                   buffer, (long)buffer_size);
709
710         if (!EXT4_I(inode)->i_file_acl)
711                 return 0;
712         ea_idebug(inode, "reading block %llu",
713                   (unsigned long long)EXT4_I(inode)->i_file_acl);
714         bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
715         if (IS_ERR(bh))
716                 return PTR_ERR(bh);
717         ea_bdebug(bh, "b_count=%d, refcount=%d",
718                 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
719         error = ext4_xattr_check_block(inode, bh);
720         if (error)
721                 goto cleanup;
722         ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode), bh);
723         error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer,
724                                         buffer_size);
725 cleanup:
726         brelse(bh);
727         return error;
728 }
729
730 static int
731 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
732 {
733         struct inode *inode = d_inode(dentry);
734         struct ext4_xattr_ibody_header *header;
735         struct ext4_inode *raw_inode;
736         struct ext4_iloc iloc;
737         void *end;
738         int error;
739
740         if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
741                 return 0;
742         error = ext4_get_inode_loc(inode, &iloc);
743         if (error)
744                 return error;
745         raw_inode = ext4_raw_inode(&iloc);
746         header = IHDR(inode, raw_inode);
747         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
748         error = xattr_check_inode(inode, header, end);
749         if (error)
750                 goto cleanup;
751         error = ext4_xattr_list_entries(dentry, IFIRST(header),
752                                         buffer, buffer_size);
753
754 cleanup:
755         brelse(iloc.bh);
756         return error;
757 }
758
759 /*
760  * Inode operation listxattr()
761  *
762  * d_inode(dentry)->i_rwsem: don't care
763  *
764  * Copy a list of attribute names into the buffer
765  * provided, or compute the buffer size required.
766  * Buffer is NULL to compute the size of the buffer required.
767  *
768  * Returns a negative error number on failure, or the number of bytes
769  * used / required on success.
770  */
771 ssize_t
772 ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
773 {
774         int ret, ret2;
775
776         down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
777         ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
778         if (ret < 0)
779                 goto errout;
780         if (buffer) {
781                 buffer += ret;
782                 buffer_size -= ret;
783         }
784         ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
785         if (ret < 0)
786                 goto errout;
787         ret += ret2;
788 errout:
789         up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
790         return ret;
791 }
792
793 /*
794  * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
795  * not set, set it.
796  */
797 static void ext4_xattr_update_super_block(handle_t *handle,
798                                           struct super_block *sb)
799 {
800         if (ext4_has_feature_xattr(sb))
801                 return;
802
803         BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
804         if (ext4_journal_get_write_access(handle, sb, EXT4_SB(sb)->s_sbh,
805                                           EXT4_JTR_NONE) == 0) {
806                 lock_buffer(EXT4_SB(sb)->s_sbh);
807                 ext4_set_feature_xattr(sb);
808                 ext4_superblock_csum_set(sb);
809                 unlock_buffer(EXT4_SB(sb)->s_sbh);
810                 ext4_handle_dirty_metadata(handle, NULL, EXT4_SB(sb)->s_sbh);
811         }
812 }
813
814 int ext4_get_inode_usage(struct inode *inode, qsize_t *usage)
815 {
816         struct ext4_iloc iloc = { .bh = NULL };
817         struct buffer_head *bh = NULL;
818         struct ext4_inode *raw_inode;
819         struct ext4_xattr_ibody_header *header;
820         struct ext4_xattr_entry *entry;
821         qsize_t ea_inode_refs = 0;
822         void *end;
823         int ret;
824
825         lockdep_assert_held_read(&EXT4_I(inode)->xattr_sem);
826
827         if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
828                 ret = ext4_get_inode_loc(inode, &iloc);
829                 if (ret)
830                         goto out;
831                 raw_inode = ext4_raw_inode(&iloc);
832                 header = IHDR(inode, raw_inode);
833                 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
834                 ret = xattr_check_inode(inode, header, end);
835                 if (ret)
836                         goto out;
837
838                 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
839                      entry = EXT4_XATTR_NEXT(entry))
840                         if (entry->e_value_inum)
841                                 ea_inode_refs++;
842         }
843
844         if (EXT4_I(inode)->i_file_acl) {
845                 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
846                 if (IS_ERR(bh)) {
847                         ret = PTR_ERR(bh);
848                         bh = NULL;
849                         goto out;
850                 }
851
852                 ret = ext4_xattr_check_block(inode, bh);
853                 if (ret)
854                         goto out;
855
856                 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
857                      entry = EXT4_XATTR_NEXT(entry))
858                         if (entry->e_value_inum)
859                                 ea_inode_refs++;
860         }
861         *usage = ea_inode_refs + 1;
862         ret = 0;
863 out:
864         brelse(iloc.bh);
865         brelse(bh);
866         return ret;
867 }
868
869 static inline size_t round_up_cluster(struct inode *inode, size_t length)
870 {
871         struct super_block *sb = inode->i_sb;
872         size_t cluster_size = 1 << (EXT4_SB(sb)->s_cluster_bits +
873                                     inode->i_blkbits);
874         size_t mask = ~(cluster_size - 1);
875
876         return (length + cluster_size - 1) & mask;
877 }
878
879 static int ext4_xattr_inode_alloc_quota(struct inode *inode, size_t len)
880 {
881         int err;
882
883         err = dquot_alloc_inode(inode);
884         if (err)
885                 return err;
886         err = dquot_alloc_space_nodirty(inode, round_up_cluster(inode, len));
887         if (err)
888                 dquot_free_inode(inode);
889         return err;
890 }
891
892 static void ext4_xattr_inode_free_quota(struct inode *parent,
893                                         struct inode *ea_inode,
894                                         size_t len)
895 {
896         if (ea_inode &&
897             ext4_test_inode_state(ea_inode, EXT4_STATE_LUSTRE_EA_INODE))
898                 return;
899         dquot_free_space_nodirty(parent, round_up_cluster(parent, len));
900         dquot_free_inode(parent);
901 }
902
903 int __ext4_xattr_set_credits(struct super_block *sb, struct inode *inode,
904                              struct buffer_head *block_bh, size_t value_len,
905                              bool is_create)
906 {
907         int credits;
908         int blocks;
909
910         /*
911          * 1) Owner inode update
912          * 2) Ref count update on old xattr block
913          * 3) new xattr block
914          * 4) block bitmap update for new xattr block
915          * 5) group descriptor for new xattr block
916          * 6) block bitmap update for old xattr block
917          * 7) group descriptor for old block
918          *
919          * 6 & 7 can happen if we have two racing threads T_a and T_b
920          * which are each trying to set an xattr on inodes I_a and I_b
921          * which were both initially sharing an xattr block.
922          */
923         credits = 7;
924
925         /* Quota updates. */
926         credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(sb);
927
928         /*
929          * In case of inline data, we may push out the data to a block,
930          * so we need to reserve credits for this eventuality
931          */
932         if (inode && ext4_has_inline_data(inode))
933                 credits += ext4_writepage_trans_blocks(inode) + 1;
934
935         /* We are done if ea_inode feature is not enabled. */
936         if (!ext4_has_feature_ea_inode(sb))
937                 return credits;
938
939         /* New ea_inode, inode map, block bitmap, group descriptor. */
940         credits += 4;
941
942         /* Data blocks. */
943         blocks = (value_len + sb->s_blocksize - 1) >> sb->s_blocksize_bits;
944
945         /* Indirection block or one level of extent tree. */
946         blocks += 1;
947
948         /* Block bitmap and group descriptor updates for each block. */
949         credits += blocks * 2;
950
951         /* Blocks themselves. */
952         credits += blocks;
953
954         if (!is_create) {
955                 /* Dereference ea_inode holding old xattr value.
956                  * Old ea_inode, inode map, block bitmap, group descriptor.
957                  */
958                 credits += 4;
959
960                 /* Data blocks for old ea_inode. */
961                 blocks = XATTR_SIZE_MAX >> sb->s_blocksize_bits;
962
963                 /* Indirection block or one level of extent tree for old
964                  * ea_inode.
965                  */
966                 blocks += 1;
967
968                 /* Block bitmap and group descriptor updates for each block. */
969                 credits += blocks * 2;
970         }
971
972         /* We may need to clone the existing xattr block in which case we need
973          * to increment ref counts for existing ea_inodes referenced by it.
974          */
975         if (block_bh) {
976                 struct ext4_xattr_entry *entry = BFIRST(block_bh);
977
978                 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry))
979                         if (entry->e_value_inum)
980                                 /* Ref count update on ea_inode. */
981                                 credits += 1;
982         }
983         return credits;
984 }
985
986 static int ext4_xattr_inode_update_ref(handle_t *handle, struct inode *ea_inode,
987                                        int ref_change)
988 {
989         struct ext4_iloc iloc;
990         s64 ref_count;
991         int ret;
992
993         inode_lock(ea_inode);
994
995         ret = ext4_reserve_inode_write(handle, ea_inode, &iloc);
996         if (ret)
997                 goto out;
998
999         ref_count = ext4_xattr_inode_get_ref(ea_inode);
1000         ref_count += ref_change;
1001         ext4_xattr_inode_set_ref(ea_inode, ref_count);
1002
1003         if (ref_change > 0) {
1004                 WARN_ONCE(ref_count <= 0, "EA inode %lu ref_count=%lld",
1005                           ea_inode->i_ino, ref_count);
1006
1007                 if (ref_count == 1) {
1008                         WARN_ONCE(ea_inode->i_nlink, "EA inode %lu i_nlink=%u",
1009                                   ea_inode->i_ino, ea_inode->i_nlink);
1010
1011                         set_nlink(ea_inode, 1);
1012                         ext4_orphan_del(handle, ea_inode);
1013                 }
1014         } else {
1015                 WARN_ONCE(ref_count < 0, "EA inode %lu ref_count=%lld",
1016                           ea_inode->i_ino, ref_count);
1017
1018                 if (ref_count == 0) {
1019                         WARN_ONCE(ea_inode->i_nlink != 1,
1020                                   "EA inode %lu i_nlink=%u",
1021                                   ea_inode->i_ino, ea_inode->i_nlink);
1022
1023                         clear_nlink(ea_inode);
1024                         ext4_orphan_add(handle, ea_inode);
1025                 }
1026         }
1027
1028         ret = ext4_mark_iloc_dirty(handle, ea_inode, &iloc);
1029         if (ret)
1030                 ext4_warning_inode(ea_inode,
1031                                    "ext4_mark_iloc_dirty() failed ret=%d", ret);
1032 out:
1033         inode_unlock(ea_inode);
1034         return ret;
1035 }
1036
1037 static int ext4_xattr_inode_inc_ref(handle_t *handle, struct inode *ea_inode)
1038 {
1039         return ext4_xattr_inode_update_ref(handle, ea_inode, 1);
1040 }
1041
1042 static int ext4_xattr_inode_dec_ref(handle_t *handle, struct inode *ea_inode)
1043 {
1044         return ext4_xattr_inode_update_ref(handle, ea_inode, -1);
1045 }
1046
1047 static int ext4_xattr_inode_inc_ref_all(handle_t *handle, struct inode *parent,
1048                                         struct ext4_xattr_entry *first)
1049 {
1050         struct inode *ea_inode;
1051         struct ext4_xattr_entry *entry;
1052         struct ext4_xattr_entry *failed_entry;
1053         unsigned int ea_ino;
1054         int err, saved_err;
1055
1056         for (entry = first; !IS_LAST_ENTRY(entry);
1057              entry = EXT4_XATTR_NEXT(entry)) {
1058                 if (!entry->e_value_inum)
1059                         continue;
1060                 ea_ino = le32_to_cpu(entry->e_value_inum);
1061                 err = ext4_xattr_inode_iget(parent, ea_ino,
1062                                             le32_to_cpu(entry->e_hash),
1063                                             &ea_inode);
1064                 if (err)
1065                         goto cleanup;
1066                 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1067                 if (err) {
1068                         ext4_warning_inode(ea_inode, "inc ref error %d", err);
1069                         iput(ea_inode);
1070                         goto cleanup;
1071                 }
1072                 iput(ea_inode);
1073         }
1074         return 0;
1075
1076 cleanup:
1077         saved_err = err;
1078         failed_entry = entry;
1079
1080         for (entry = first; entry != failed_entry;
1081              entry = EXT4_XATTR_NEXT(entry)) {
1082                 if (!entry->e_value_inum)
1083                         continue;
1084                 ea_ino = le32_to_cpu(entry->e_value_inum);
1085                 err = ext4_xattr_inode_iget(parent, ea_ino,
1086                                             le32_to_cpu(entry->e_hash),
1087                                             &ea_inode);
1088                 if (err) {
1089                         ext4_warning(parent->i_sb,
1090                                      "cleanup ea_ino %u iget error %d", ea_ino,
1091                                      err);
1092                         continue;
1093                 }
1094                 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1095                 if (err)
1096                         ext4_warning_inode(ea_inode, "cleanup dec ref error %d",
1097                                            err);
1098                 iput(ea_inode);
1099         }
1100         return saved_err;
1101 }
1102
1103 static int ext4_xattr_restart_fn(handle_t *handle, struct inode *inode,
1104                         struct buffer_head *bh, bool block_csum, bool dirty)
1105 {
1106         int error;
1107
1108         if (bh && dirty) {
1109                 if (block_csum)
1110                         ext4_xattr_block_csum_set(inode, bh);
1111                 error = ext4_handle_dirty_metadata(handle, NULL, bh);
1112                 if (error) {
1113                         ext4_warning(inode->i_sb, "Handle metadata (error %d)",
1114                                      error);
1115                         return error;
1116                 }
1117         }
1118         return 0;
1119 }
1120
1121 static void
1122 ext4_xattr_inode_dec_ref_all(handle_t *handle, struct inode *parent,
1123                              struct buffer_head *bh,
1124                              struct ext4_xattr_entry *first, bool block_csum,
1125                              struct ext4_xattr_inode_array **ea_inode_array,
1126                              int extra_credits, bool skip_quota)
1127 {
1128         struct inode *ea_inode;
1129         struct ext4_xattr_entry *entry;
1130         bool dirty = false;
1131         unsigned int ea_ino;
1132         int err;
1133         int credits;
1134
1135         /* One credit for dec ref on ea_inode, one for orphan list addition, */
1136         credits = 2 + extra_credits;
1137
1138         for (entry = first; !IS_LAST_ENTRY(entry);
1139              entry = EXT4_XATTR_NEXT(entry)) {
1140                 if (!entry->e_value_inum)
1141                         continue;
1142                 ea_ino = le32_to_cpu(entry->e_value_inum);
1143                 err = ext4_xattr_inode_iget(parent, ea_ino,
1144                                             le32_to_cpu(entry->e_hash),
1145                                             &ea_inode);
1146                 if (err)
1147                         continue;
1148
1149                 err = ext4_expand_inode_array(ea_inode_array, ea_inode);
1150                 if (err) {
1151                         ext4_warning_inode(ea_inode,
1152                                            "Expand inode array err=%d", err);
1153                         iput(ea_inode);
1154                         continue;
1155                 }
1156
1157                 err = ext4_journal_ensure_credits_fn(handle, credits, credits,
1158                         ext4_free_metadata_revoke_credits(parent->i_sb, 1),
1159                         ext4_xattr_restart_fn(handle, parent, bh, block_csum,
1160                                               dirty));
1161                 if (err < 0) {
1162                         ext4_warning_inode(ea_inode, "Ensure credits err=%d",
1163                                            err);
1164                         continue;
1165                 }
1166                 if (err > 0) {
1167                         err = ext4_journal_get_write_access(handle,
1168                                         parent->i_sb, bh, EXT4_JTR_NONE);
1169                         if (err) {
1170                                 ext4_warning_inode(ea_inode,
1171                                                 "Re-get write access err=%d",
1172                                                 err);
1173                                 continue;
1174                         }
1175                 }
1176
1177                 err = ext4_xattr_inode_dec_ref(handle, ea_inode);
1178                 if (err) {
1179                         ext4_warning_inode(ea_inode, "ea_inode dec ref err=%d",
1180                                            err);
1181                         continue;
1182                 }
1183
1184                 if (!skip_quota)
1185                         ext4_xattr_inode_free_quota(parent, ea_inode,
1186                                               le32_to_cpu(entry->e_value_size));
1187
1188                 /*
1189                  * Forget about ea_inode within the same transaction that
1190                  * decrements the ref count. This avoids duplicate decrements in
1191                  * case the rest of the work spills over to subsequent
1192                  * transactions.
1193                  */
1194                 entry->e_value_inum = 0;
1195                 entry->e_value_size = 0;
1196
1197                 dirty = true;
1198         }
1199
1200         if (dirty) {
1201                 /*
1202                  * Note that we are deliberately skipping csum calculation for
1203                  * the final update because we do not expect any journal
1204                  * restarts until xattr block is freed.
1205                  */
1206
1207                 err = ext4_handle_dirty_metadata(handle, NULL, bh);
1208                 if (err)
1209                         ext4_warning_inode(parent,
1210                                            "handle dirty metadata err=%d", err);
1211         }
1212 }
1213
1214 /*
1215  * Release the xattr block BH: If the reference count is > 1, decrement it;
1216  * otherwise free the block.
1217  */
1218 static void
1219 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
1220                          struct buffer_head *bh,
1221                          struct ext4_xattr_inode_array **ea_inode_array,
1222                          int extra_credits)
1223 {
1224         struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1225         u32 hash, ref;
1226         int error = 0;
1227
1228         BUFFER_TRACE(bh, "get_write_access");
1229         error = ext4_journal_get_write_access(handle, inode->i_sb, bh,
1230                                               EXT4_JTR_NONE);
1231         if (error)
1232                 goto out;
1233
1234 retry_ref:
1235         lock_buffer(bh);
1236         hash = le32_to_cpu(BHDR(bh)->h_hash);
1237         ref = le32_to_cpu(BHDR(bh)->h_refcount);
1238         if (ref == 1) {
1239                 ea_bdebug(bh, "refcount now=0; freeing");
1240                 /*
1241                  * This must happen under buffer lock for
1242                  * ext4_xattr_block_set() to reliably detect freed block
1243                  */
1244                 if (ea_block_cache) {
1245                         struct mb_cache_entry *oe;
1246
1247                         oe = mb_cache_entry_delete_or_get(ea_block_cache, hash,
1248                                                           bh->b_blocknr);
1249                         if (oe) {
1250                                 unlock_buffer(bh);
1251                                 mb_cache_entry_wait_unused(oe);
1252                                 mb_cache_entry_put(ea_block_cache, oe);
1253                                 goto retry_ref;
1254                         }
1255                 }
1256                 get_bh(bh);
1257                 unlock_buffer(bh);
1258
1259                 if (ext4_has_feature_ea_inode(inode->i_sb))
1260                         ext4_xattr_inode_dec_ref_all(handle, inode, bh,
1261                                                      BFIRST(bh),
1262                                                      true /* block_csum */,
1263                                                      ea_inode_array,
1264                                                      extra_credits,
1265                                                      true /* skip_quota */);
1266                 ext4_free_blocks(handle, inode, bh, 0, 1,
1267                                  EXT4_FREE_BLOCKS_METADATA |
1268                                  EXT4_FREE_BLOCKS_FORGET);
1269         } else {
1270                 ref--;
1271                 BHDR(bh)->h_refcount = cpu_to_le32(ref);
1272                 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
1273                         struct mb_cache_entry *ce;
1274
1275                         if (ea_block_cache) {
1276                                 ce = mb_cache_entry_get(ea_block_cache, hash,
1277                                                         bh->b_blocknr);
1278                                 if (ce) {
1279                                         set_bit(MBE_REUSABLE_B, &ce->e_flags);
1280                                         mb_cache_entry_put(ea_block_cache, ce);
1281                                 }
1282                         }
1283                 }
1284
1285                 ext4_xattr_block_csum_set(inode, bh);
1286                 /*
1287                  * Beware of this ugliness: Releasing of xattr block references
1288                  * from different inodes can race and so we have to protect
1289                  * from a race where someone else frees the block (and releases
1290                  * its journal_head) before we are done dirtying the buffer. In
1291                  * nojournal mode this race is harmless and we actually cannot
1292                  * call ext4_handle_dirty_metadata() with locked buffer as
1293                  * that function can call sync_dirty_buffer() so for that case
1294                  * we handle the dirtying after unlocking the buffer.
1295                  */
1296                 if (ext4_handle_valid(handle))
1297                         error = ext4_handle_dirty_metadata(handle, inode, bh);
1298                 unlock_buffer(bh);
1299                 if (!ext4_handle_valid(handle))
1300                         error = ext4_handle_dirty_metadata(handle, inode, bh);
1301                 if (IS_SYNC(inode))
1302                         ext4_handle_sync(handle);
1303                 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
1304                 ea_bdebug(bh, "refcount now=%d; releasing",
1305                           le32_to_cpu(BHDR(bh)->h_refcount));
1306         }
1307 out:
1308         ext4_std_error(inode->i_sb, error);
1309         return;
1310 }
1311
1312 /*
1313  * Find the available free space for EAs. This also returns the total number of
1314  * bytes used by EA entries.
1315  */
1316 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
1317                                     size_t *min_offs, void *base, int *total)
1318 {
1319         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1320                 if (!last->e_value_inum && last->e_value_size) {
1321                         size_t offs = le16_to_cpu(last->e_value_offs);
1322                         if (offs < *min_offs)
1323                                 *min_offs = offs;
1324                 }
1325                 if (total)
1326                         *total += EXT4_XATTR_LEN(last->e_name_len);
1327         }
1328         return (*min_offs - ((void *)last - base) - sizeof(__u32));
1329 }
1330
1331 /*
1332  * Write the value of the EA in an inode.
1333  */
1334 static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
1335                                   const void *buf, int bufsize)
1336 {
1337         struct buffer_head *bh = NULL;
1338         unsigned long block = 0;
1339         int blocksize = ea_inode->i_sb->s_blocksize;
1340         int max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
1341         int csize, wsize = 0;
1342         int ret = 0, ret2 = 0;
1343         int retries = 0;
1344
1345 retry:
1346         while (ret >= 0 && ret < max_blocks) {
1347                 struct ext4_map_blocks map;
1348                 map.m_lblk = block += ret;
1349                 map.m_len = max_blocks -= ret;
1350
1351                 ret = ext4_map_blocks(handle, ea_inode, &map,
1352                                       EXT4_GET_BLOCKS_CREATE);
1353                 if (ret <= 0) {
1354                         ext4_mark_inode_dirty(handle, ea_inode);
1355                         if (ret == -ENOSPC &&
1356                             ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
1357                                 ret = 0;
1358                                 goto retry;
1359                         }
1360                         break;
1361                 }
1362         }
1363
1364         if (ret < 0)
1365                 return ret;
1366
1367         block = 0;
1368         while (wsize < bufsize) {
1369                 brelse(bh);
1370                 csize = (bufsize - wsize) > blocksize ? blocksize :
1371                                                                 bufsize - wsize;
1372                 bh = ext4_getblk(handle, ea_inode, block, 0);
1373                 if (IS_ERR(bh))
1374                         return PTR_ERR(bh);
1375                 if (!bh) {
1376                         WARN_ON_ONCE(1);
1377                         EXT4_ERROR_INODE(ea_inode,
1378                                          "ext4_getblk() return bh = NULL");
1379                         return -EFSCORRUPTED;
1380                 }
1381                 ret = ext4_journal_get_write_access(handle, ea_inode->i_sb, bh,
1382                                                    EXT4_JTR_NONE);
1383                 if (ret)
1384                         goto out;
1385
1386                 memcpy(bh->b_data, buf, csize);
1387                 set_buffer_uptodate(bh);
1388                 ext4_handle_dirty_metadata(handle, ea_inode, bh);
1389
1390                 buf += csize;
1391                 wsize += csize;
1392                 block += 1;
1393         }
1394
1395         inode_lock(ea_inode);
1396         i_size_write(ea_inode, wsize);
1397         ext4_update_i_disksize(ea_inode, wsize);
1398         inode_unlock(ea_inode);
1399
1400         ret2 = ext4_mark_inode_dirty(handle, ea_inode);
1401         if (unlikely(ret2 && !ret))
1402                 ret = ret2;
1403
1404 out:
1405         brelse(bh);
1406
1407         return ret;
1408 }
1409
1410 /*
1411  * Create an inode to store the value of a large EA.
1412  */
1413 static struct inode *ext4_xattr_inode_create(handle_t *handle,
1414                                              struct inode *inode, u32 hash)
1415 {
1416         struct inode *ea_inode = NULL;
1417         uid_t owner[2] = { i_uid_read(inode), i_gid_read(inode) };
1418         int err;
1419
1420         if (inode->i_sb->s_root == NULL) {
1421                 ext4_warning(inode->i_sb,
1422                              "refuse to create EA inode when umounting");
1423                 WARN_ON(1);
1424                 return ERR_PTR(-EINVAL);
1425         }
1426
1427         /*
1428          * Let the next inode be the goal, so we try and allocate the EA inode
1429          * in the same group, or nearby one.
1430          */
1431         ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1432                                   S_IFREG | 0600, NULL, inode->i_ino + 1, owner,
1433                                   EXT4_EA_INODE_FL);
1434         if (!IS_ERR(ea_inode)) {
1435                 ea_inode->i_op = &ext4_file_inode_operations;
1436                 ea_inode->i_fop = &ext4_file_operations;
1437                 ext4_set_aops(ea_inode);
1438                 ext4_xattr_inode_set_class(ea_inode);
1439                 unlock_new_inode(ea_inode);
1440                 ext4_xattr_inode_set_ref(ea_inode, 1);
1441                 ext4_xattr_inode_set_hash(ea_inode, hash);
1442                 err = ext4_mark_inode_dirty(handle, ea_inode);
1443                 if (!err)
1444                         err = ext4_inode_attach_jinode(ea_inode);
1445                 if (err) {
1446                         if (ext4_xattr_inode_dec_ref(handle, ea_inode))
1447                                 ext4_warning_inode(ea_inode,
1448                                         "cleanup dec ref error %d", err);
1449                         iput(ea_inode);
1450                         return ERR_PTR(err);
1451                 }
1452
1453                 /*
1454                  * Xattr inodes are shared therefore quota charging is performed
1455                  * at a higher level.
1456                  */
1457                 dquot_free_inode(ea_inode);
1458                 dquot_drop(ea_inode);
1459                 inode_lock(ea_inode);
1460                 ea_inode->i_flags |= S_NOQUOTA;
1461                 inode_unlock(ea_inode);
1462         }
1463
1464         return ea_inode;
1465 }
1466
1467 static struct inode *
1468 ext4_xattr_inode_cache_find(struct inode *inode, const void *value,
1469                             size_t value_len, u32 hash)
1470 {
1471         struct inode *ea_inode;
1472         struct mb_cache_entry *ce;
1473         struct mb_cache *ea_inode_cache = EA_INODE_CACHE(inode);
1474         void *ea_data;
1475
1476         if (!ea_inode_cache)
1477                 return NULL;
1478
1479         ce = mb_cache_entry_find_first(ea_inode_cache, hash);
1480         if (!ce)
1481                 return NULL;
1482
1483         WARN_ON_ONCE(ext4_handle_valid(journal_current_handle()) &&
1484                      !(current->flags & PF_MEMALLOC_NOFS));
1485
1486         ea_data = kvmalloc(value_len, GFP_KERNEL);
1487         if (!ea_data) {
1488                 mb_cache_entry_put(ea_inode_cache, ce);
1489                 return NULL;
1490         }
1491
1492         while (ce) {
1493                 ea_inode = ext4_iget(inode->i_sb, ce->e_value,
1494                                      EXT4_IGET_EA_INODE);
1495                 if (IS_ERR(ea_inode))
1496                         goto next_entry;
1497                 ext4_xattr_inode_set_class(ea_inode);
1498                 if (i_size_read(ea_inode) == value_len &&
1499                     !ext4_xattr_inode_read(ea_inode, ea_data, value_len) &&
1500                     !ext4_xattr_inode_verify_hashes(ea_inode, NULL, ea_data,
1501                                                     value_len) &&
1502                     !memcmp(value, ea_data, value_len)) {
1503                         mb_cache_entry_touch(ea_inode_cache, ce);
1504                         mb_cache_entry_put(ea_inode_cache, ce);
1505                         kvfree(ea_data);
1506                         return ea_inode;
1507                 }
1508                 iput(ea_inode);
1509         next_entry:
1510                 ce = mb_cache_entry_find_next(ea_inode_cache, ce);
1511         }
1512         kvfree(ea_data);
1513         return NULL;
1514 }
1515
1516 /*
1517  * Add value of the EA in an inode.
1518  */
1519 static int ext4_xattr_inode_lookup_create(handle_t *handle, struct inode *inode,
1520                                           const void *value, size_t value_len,
1521                                           struct inode **ret_inode)
1522 {
1523         struct inode *ea_inode;
1524         u32 hash;
1525         int err;
1526
1527         hash = ext4_xattr_inode_hash(EXT4_SB(inode->i_sb), value, value_len);
1528         ea_inode = ext4_xattr_inode_cache_find(inode, value, value_len, hash);
1529         if (ea_inode) {
1530                 err = ext4_xattr_inode_inc_ref(handle, ea_inode);
1531                 if (err) {
1532                         iput(ea_inode);
1533                         return err;
1534                 }
1535
1536                 *ret_inode = ea_inode;
1537                 return 0;
1538         }
1539
1540         /* Create an inode for the EA value */
1541         ea_inode = ext4_xattr_inode_create(handle, inode, hash);
1542         if (IS_ERR(ea_inode))
1543                 return PTR_ERR(ea_inode);
1544
1545         err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
1546         if (err) {
1547                 ext4_xattr_inode_dec_ref(handle, ea_inode);
1548                 iput(ea_inode);
1549                 return err;
1550         }
1551
1552         if (EA_INODE_CACHE(inode))
1553                 mb_cache_entry_create(EA_INODE_CACHE(inode), GFP_NOFS, hash,
1554                                       ea_inode->i_ino, true /* reusable */);
1555
1556         *ret_inode = ea_inode;
1557         return 0;
1558 }
1559
1560 /*
1561  * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1562  * feature is enabled.
1563  */
1564 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1565
1566 static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
1567                                 struct ext4_xattr_search *s,
1568                                 handle_t *handle, struct inode *inode,
1569                                 bool is_block)
1570 {
1571         struct ext4_xattr_entry *last, *next;
1572         struct ext4_xattr_entry *here = s->here;
1573         size_t min_offs = s->end - s->base, name_len = strlen(i->name);
1574         int in_inode = i->in_inode;
1575         struct inode *old_ea_inode = NULL;
1576         struct inode *new_ea_inode = NULL;
1577         size_t old_size, new_size;
1578         int ret;
1579
1580         /* Space used by old and new values. */
1581         old_size = (!s->not_found && !here->e_value_inum) ?
1582                         EXT4_XATTR_SIZE(le32_to_cpu(here->e_value_size)) : 0;
1583         new_size = (i->value && !in_inode) ? EXT4_XATTR_SIZE(i->value_len) : 0;
1584
1585         /*
1586          * Optimization for the simple case when old and new values have the
1587          * same padded sizes. Not applicable if external inodes are involved.
1588          */
1589         if (new_size && new_size == old_size) {
1590                 size_t offs = le16_to_cpu(here->e_value_offs);
1591                 void *val = s->base + offs;
1592
1593                 here->e_value_size = cpu_to_le32(i->value_len);
1594                 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1595                         memset(val, 0, new_size);
1596                 } else {
1597                         memcpy(val, i->value, i->value_len);
1598                         /* Clear padding bytes. */
1599                         memset(val + i->value_len, 0, new_size - i->value_len);
1600                 }
1601                 goto update_hash;
1602         }
1603
1604         /* Compute min_offs and last. */
1605         last = s->first;
1606         for (; !IS_LAST_ENTRY(last); last = next) {
1607                 next = EXT4_XATTR_NEXT(last);
1608                 if ((void *)next >= s->end) {
1609                         EXT4_ERROR_INODE(inode, "corrupted xattr entries");
1610                         ret = -EFSCORRUPTED;
1611                         goto out;
1612                 }
1613                 if (!last->e_value_inum && last->e_value_size) {
1614                         size_t offs = le16_to_cpu(last->e_value_offs);
1615                         if (offs < min_offs)
1616                                 min_offs = offs;
1617                 }
1618         }
1619
1620         /* Check whether we have enough space. */
1621         if (i->value) {
1622                 size_t free;
1623
1624                 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
1625                 if (!s->not_found)
1626                         free += EXT4_XATTR_LEN(name_len) + old_size;
1627
1628                 if (free < EXT4_XATTR_LEN(name_len) + new_size) {
1629                         ret = -ENOSPC;
1630                         goto out;
1631                 }
1632
1633                 /*
1634                  * If storing the value in an external inode is an option,
1635                  * reserve space for xattr entries/names in the external
1636                  * attribute block so that a long value does not occupy the
1637                  * whole space and prevent further entries being added.
1638                  */
1639                 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1640                     new_size && is_block &&
1641                     (min_offs + old_size - new_size) <
1642                                         EXT4_XATTR_BLOCK_RESERVE(inode)) {
1643                         ret = -ENOSPC;
1644                         goto out;
1645                 }
1646         }
1647
1648         /*
1649          * Getting access to old and new ea inodes is subject to failures.
1650          * Finish that work before doing any modifications to the xattr data.
1651          */
1652         if (!s->not_found && here->e_value_inum) {
1653                 ret = ext4_xattr_inode_iget(inode,
1654                                             le32_to_cpu(here->e_value_inum),
1655                                             le32_to_cpu(here->e_hash),
1656                                             &old_ea_inode);
1657                 if (ret) {
1658                         old_ea_inode = NULL;
1659                         goto out;
1660                 }
1661         }
1662         if (i->value && in_inode) {
1663                 WARN_ON_ONCE(!i->value_len);
1664
1665                 ret = ext4_xattr_inode_alloc_quota(inode, i->value_len);
1666                 if (ret)
1667                         goto out;
1668
1669                 ret = ext4_xattr_inode_lookup_create(handle, inode, i->value,
1670                                                      i->value_len,
1671                                                      &new_ea_inode);
1672                 if (ret) {
1673                         new_ea_inode = NULL;
1674                         ext4_xattr_inode_free_quota(inode, NULL, i->value_len);
1675                         goto out;
1676                 }
1677         }
1678
1679         if (old_ea_inode) {
1680                 /* We are ready to release ref count on the old_ea_inode. */
1681                 ret = ext4_xattr_inode_dec_ref(handle, old_ea_inode);
1682                 if (ret) {
1683                         /* Release newly required ref count on new_ea_inode. */
1684                         if (new_ea_inode) {
1685                                 int err;
1686
1687                                 err = ext4_xattr_inode_dec_ref(handle,
1688                                                                new_ea_inode);
1689                                 if (err)
1690                                         ext4_warning_inode(new_ea_inode,
1691                                                   "dec ref new_ea_inode err=%d",
1692                                                   err);
1693                                 ext4_xattr_inode_free_quota(inode, new_ea_inode,
1694                                                             i->value_len);
1695                         }
1696                         goto out;
1697                 }
1698
1699                 ext4_xattr_inode_free_quota(inode, old_ea_inode,
1700                                             le32_to_cpu(here->e_value_size));
1701         }
1702
1703         /* No failures allowed past this point. */
1704
1705         if (!s->not_found && here->e_value_size && !here->e_value_inum) {
1706                 /* Remove the old value. */
1707                 void *first_val = s->base + min_offs;
1708                 size_t offs = le16_to_cpu(here->e_value_offs);
1709                 void *val = s->base + offs;
1710
1711                 memmove(first_val + old_size, first_val, val - first_val);
1712                 memset(first_val, 0, old_size);
1713                 min_offs += old_size;
1714
1715                 /* Adjust all value offsets. */
1716                 last = s->first;
1717                 while (!IS_LAST_ENTRY(last)) {
1718                         size_t o = le16_to_cpu(last->e_value_offs);
1719
1720                         if (!last->e_value_inum &&
1721                             last->e_value_size && o < offs)
1722                                 last->e_value_offs = cpu_to_le16(o + old_size);
1723                         last = EXT4_XATTR_NEXT(last);
1724                 }
1725         }
1726
1727         if (!i->value) {
1728                 /* Remove old name. */
1729                 size_t size = EXT4_XATTR_LEN(name_len);
1730
1731                 last = ENTRY((void *)last - size);
1732                 memmove(here, (void *)here + size,
1733                         (void *)last - (void *)here + sizeof(__u32));
1734                 memset(last, 0, size);
1735
1736                 /*
1737                  * Update i_inline_off - moved ibody region might contain
1738                  * system.data attribute.  Handling a failure here won't
1739                  * cause other complications for setting an xattr.
1740                  */
1741                 if (!is_block && ext4_has_inline_data(inode)) {
1742                         ret = ext4_find_inline_data_nolock(inode);
1743                         if (ret) {
1744                                 ext4_warning_inode(inode,
1745                                         "unable to update i_inline_off");
1746                                 goto out;
1747                         }
1748                 }
1749         } else if (s->not_found) {
1750                 /* Insert new name. */
1751                 size_t size = EXT4_XATTR_LEN(name_len);
1752                 size_t rest = (void *)last - (void *)here + sizeof(__u32);
1753
1754                 memmove((void *)here + size, here, rest);
1755                 memset(here, 0, size);
1756                 here->e_name_index = i->name_index;
1757                 here->e_name_len = name_len;
1758                 memcpy(here->e_name, i->name, name_len);
1759         } else {
1760                 /* This is an update, reset value info. */
1761                 here->e_value_inum = 0;
1762                 here->e_value_offs = 0;
1763                 here->e_value_size = 0;
1764         }
1765
1766         if (i->value) {
1767                 /* Insert new value. */
1768                 if (in_inode) {
1769                         here->e_value_inum = cpu_to_le32(new_ea_inode->i_ino);
1770                 } else if (i->value_len) {
1771                         void *val = s->base + min_offs - new_size;
1772
1773                         here->e_value_offs = cpu_to_le16(min_offs - new_size);
1774                         if (i->value == EXT4_ZERO_XATTR_VALUE) {
1775                                 memset(val, 0, new_size);
1776                         } else {
1777                                 memcpy(val, i->value, i->value_len);
1778                                 /* Clear padding bytes. */
1779                                 memset(val + i->value_len, 0,
1780                                        new_size - i->value_len);
1781                         }
1782                 }
1783                 here->e_value_size = cpu_to_le32(i->value_len);
1784         }
1785
1786 update_hash:
1787         if (i->value) {
1788                 __le32 hash = 0;
1789
1790                 /* Entry hash calculation. */
1791                 if (in_inode) {
1792                         __le32 crc32c_hash;
1793
1794                         /*
1795                          * Feed crc32c hash instead of the raw value for entry
1796                          * hash calculation. This is to avoid walking
1797                          * potentially long value buffer again.
1798                          */
1799                         crc32c_hash = cpu_to_le32(
1800                                        ext4_xattr_inode_get_hash(new_ea_inode));
1801                         hash = ext4_xattr_hash_entry(here->e_name,
1802                                                      here->e_name_len,
1803                                                      &crc32c_hash, 1);
1804                 } else if (is_block) {
1805                         __le32 *value = s->base + le16_to_cpu(
1806                                                         here->e_value_offs);
1807
1808                         hash = ext4_xattr_hash_entry(here->e_name,
1809                                                      here->e_name_len, value,
1810                                                      new_size >> 2);
1811                 }
1812                 here->e_hash = hash;
1813         }
1814
1815         if (is_block)
1816                 ext4_xattr_rehash((struct ext4_xattr_header *)s->base);
1817
1818         ret = 0;
1819 out:
1820         iput(old_ea_inode);
1821         iput(new_ea_inode);
1822         return ret;
1823 }
1824
1825 struct ext4_xattr_block_find {
1826         struct ext4_xattr_search s;
1827         struct buffer_head *bh;
1828 };
1829
1830 static int
1831 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1832                       struct ext4_xattr_block_find *bs)
1833 {
1834         struct super_block *sb = inode->i_sb;
1835         int error;
1836
1837         ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1838                   i->name_index, i->name, i->value, (long)i->value_len);
1839
1840         if (EXT4_I(inode)->i_file_acl) {
1841                 /* The inode already has an extended attribute block. */
1842                 bs->bh = ext4_sb_bread(sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
1843                 if (IS_ERR(bs->bh)) {
1844                         error = PTR_ERR(bs->bh);
1845                         bs->bh = NULL;
1846                         return error;
1847                 }
1848                 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1849                         atomic_read(&(bs->bh->b_count)),
1850                         le32_to_cpu(BHDR(bs->bh)->h_refcount));
1851                 error = ext4_xattr_check_block(inode, bs->bh);
1852                 if (error)
1853                         return error;
1854                 /* Find the named attribute. */
1855                 bs->s.base = BHDR(bs->bh);
1856                 bs->s.first = BFIRST(bs->bh);
1857                 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1858                 bs->s.here = bs->s.first;
1859                 error = xattr_find_entry(inode, &bs->s.here, bs->s.end,
1860                                          i->name_index, i->name, 1);
1861                 if (error && error != -ENODATA)
1862                         return error;
1863                 bs->s.not_found = error;
1864         }
1865         return 0;
1866 }
1867
1868 static int
1869 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1870                      struct ext4_xattr_info *i,
1871                      struct ext4_xattr_block_find *bs)
1872 {
1873         struct super_block *sb = inode->i_sb;
1874         struct buffer_head *new_bh = NULL;
1875         struct ext4_xattr_search s_copy = bs->s;
1876         struct ext4_xattr_search *s = &s_copy;
1877         struct mb_cache_entry *ce = NULL;
1878         int error = 0;
1879         struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
1880         struct inode *ea_inode = NULL, *tmp_inode;
1881         size_t old_ea_inode_quota = 0;
1882         unsigned int ea_ino;
1883
1884
1885 #define header(x) ((struct ext4_xattr_header *)(x))
1886
1887         if (s->base) {
1888                 int offset = (char *)s->here - bs->bh->b_data;
1889
1890                 BUFFER_TRACE(bs->bh, "get_write_access");
1891                 error = ext4_journal_get_write_access(handle, sb, bs->bh,
1892                                                       EXT4_JTR_NONE);
1893                 if (error)
1894                         goto cleanup;
1895                 lock_buffer(bs->bh);
1896
1897                 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
1898                         __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1899
1900                         /*
1901                          * This must happen under buffer lock for
1902                          * ext4_xattr_block_set() to reliably detect modified
1903                          * block
1904                          */
1905                         if (ea_block_cache) {
1906                                 struct mb_cache_entry *oe;
1907
1908                                 oe = mb_cache_entry_delete_or_get(ea_block_cache,
1909                                         hash, bs->bh->b_blocknr);
1910                                 if (oe) {
1911                                         /*
1912                                          * Xattr block is getting reused. Leave
1913                                          * it alone.
1914                                          */
1915                                         mb_cache_entry_put(ea_block_cache, oe);
1916                                         goto clone_block;
1917                                 }
1918                         }
1919                         ea_bdebug(bs->bh, "modifying in-place");
1920                         error = ext4_xattr_set_entry(i, s, handle, inode,
1921                                                      true /* is_block */);
1922                         ext4_xattr_block_csum_set(inode, bs->bh);
1923                         unlock_buffer(bs->bh);
1924                         if (error == -EFSCORRUPTED)
1925                                 goto bad_block;
1926                         if (!error)
1927                                 error = ext4_handle_dirty_metadata(handle,
1928                                                                    inode,
1929                                                                    bs->bh);
1930                         if (error)
1931                                 goto cleanup;
1932                         goto inserted;
1933                 }
1934 clone_block:
1935                 unlock_buffer(bs->bh);
1936                 ea_bdebug(bs->bh, "cloning");
1937                 s->base = kmemdup(BHDR(bs->bh), bs->bh->b_size, GFP_NOFS);
1938                 error = -ENOMEM;
1939                 if (s->base == NULL)
1940                         goto cleanup;
1941                 s->first = ENTRY(header(s->base)+1);
1942                 header(s->base)->h_refcount = cpu_to_le32(1);
1943                 s->here = ENTRY(s->base + offset);
1944                 s->end = s->base + bs->bh->b_size;
1945
1946                 /*
1947                  * If existing entry points to an xattr inode, we need
1948                  * to prevent ext4_xattr_set_entry() from decrementing
1949                  * ref count on it because the reference belongs to the
1950                  * original block. In this case, make the entry look
1951                  * like it has an empty value.
1952                  */
1953                 if (!s->not_found && s->here->e_value_inum) {
1954                         ea_ino = le32_to_cpu(s->here->e_value_inum);
1955                         error = ext4_xattr_inode_iget(inode, ea_ino,
1956                                       le32_to_cpu(s->here->e_hash),
1957                                       &tmp_inode);
1958                         if (error)
1959                                 goto cleanup;
1960
1961                         if (!ext4_test_inode_state(tmp_inode,
1962                                         EXT4_STATE_LUSTRE_EA_INODE)) {
1963                                 /*
1964                                  * Defer quota free call for previous
1965                                  * inode until success is guaranteed.
1966                                  */
1967                                 old_ea_inode_quota = le32_to_cpu(
1968                                                 s->here->e_value_size);
1969                         }
1970                         iput(tmp_inode);
1971
1972                         s->here->e_value_inum = 0;
1973                         s->here->e_value_size = 0;
1974                 }
1975         } else {
1976                 /* Allocate a buffer where we construct the new block. */
1977                 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
1978                 error = -ENOMEM;
1979                 if (s->base == NULL)
1980                         goto cleanup;
1981                 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1982                 header(s->base)->h_blocks = cpu_to_le32(1);
1983                 header(s->base)->h_refcount = cpu_to_le32(1);
1984                 s->first = ENTRY(header(s->base)+1);
1985                 s->here = ENTRY(header(s->base)+1);
1986                 s->end = s->base + sb->s_blocksize;
1987         }
1988
1989         error = ext4_xattr_set_entry(i, s, handle, inode, true /* is_block */);
1990         if (error == -EFSCORRUPTED)
1991                 goto bad_block;
1992         if (error)
1993                 goto cleanup;
1994
1995         if (i->value && s->here->e_value_inum) {
1996                 /*
1997                  * A ref count on ea_inode has been taken as part of the call to
1998                  * ext4_xattr_set_entry() above. We would like to drop this
1999                  * extra ref but we have to wait until the xattr block is
2000                  * initialized and has its own ref count on the ea_inode.
2001                  */
2002                 ea_ino = le32_to_cpu(s->here->e_value_inum);
2003                 error = ext4_xattr_inode_iget(inode, ea_ino,
2004                                               le32_to_cpu(s->here->e_hash),
2005                                               &ea_inode);
2006                 if (error) {
2007                         ea_inode = NULL;
2008                         goto cleanup;
2009                 }
2010         }
2011
2012 inserted:
2013         if (!IS_LAST_ENTRY(s->first)) {
2014                 new_bh = ext4_xattr_block_cache_find(inode, header(s->base),
2015                                                      &ce);
2016                 if (new_bh) {
2017                         /* We found an identical block in the cache. */
2018                         if (new_bh == bs->bh)
2019                                 ea_bdebug(new_bh, "keeping");
2020                         else {
2021                                 u32 ref;
2022
2023 #ifdef EXT4_XATTR_DEBUG
2024                                 WARN_ON_ONCE(dquot_initialize_needed(inode));
2025 #endif
2026                                 /* The old block is released after updating
2027                                    the inode. */
2028                                 error = dquot_alloc_block(inode,
2029                                                 EXT4_C2B(EXT4_SB(sb), 1));
2030                                 if (error)
2031                                         goto cleanup;
2032                                 BUFFER_TRACE(new_bh, "get_write_access");
2033                                 error = ext4_journal_get_write_access(
2034                                                 handle, sb, new_bh,
2035                                                 EXT4_JTR_NONE);
2036                                 if (error)
2037                                         goto cleanup_dquot;
2038                                 lock_buffer(new_bh);
2039                                 /*
2040                                  * We have to be careful about races with
2041                                  * adding references to xattr block. Once we
2042                                  * hold buffer lock xattr block's state is
2043                                  * stable so we can check the additional
2044                                  * reference fits.
2045                                  */
2046                                 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
2047                                 if (ref > EXT4_XATTR_REFCOUNT_MAX) {
2048                                         /*
2049                                          * Undo everything and check mbcache
2050                                          * again.
2051                                          */
2052                                         unlock_buffer(new_bh);
2053                                         dquot_free_block(inode,
2054                                                          EXT4_C2B(EXT4_SB(sb),
2055                                                                   1));
2056                                         brelse(new_bh);
2057                                         mb_cache_entry_put(ea_block_cache, ce);
2058                                         ce = NULL;
2059                                         new_bh = NULL;
2060                                         goto inserted;
2061                                 }
2062                                 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
2063                                 if (ref == EXT4_XATTR_REFCOUNT_MAX)
2064                                         clear_bit(MBE_REUSABLE_B, &ce->e_flags);
2065                                 ea_bdebug(new_bh, "reusing; refcount now=%d",
2066                                           ref);
2067                                 ext4_xattr_block_csum_set(inode, new_bh);
2068                                 unlock_buffer(new_bh);
2069                                 error = ext4_handle_dirty_metadata(handle,
2070                                                                    inode,
2071                                                                    new_bh);
2072                                 if (error)
2073                                         goto cleanup_dquot;
2074                         }
2075                         mb_cache_entry_touch(ea_block_cache, ce);
2076                         mb_cache_entry_put(ea_block_cache, ce);
2077                         ce = NULL;
2078                 } else if (bs->bh && s->base == bs->bh->b_data) {
2079                         /* We were modifying this block in-place. */
2080                         ea_bdebug(bs->bh, "keeping this block");
2081                         ext4_xattr_block_cache_insert(ea_block_cache, bs->bh);
2082                         new_bh = bs->bh;
2083                         get_bh(new_bh);
2084                 } else {
2085                         /* We need to allocate a new block */
2086                         ext4_fsblk_t goal, block;
2087
2088 #ifdef EXT4_XATTR_DEBUG
2089                         WARN_ON_ONCE(dquot_initialize_needed(inode));
2090 #endif
2091                         goal = ext4_group_first_block_no(sb,
2092                                                 EXT4_I(inode)->i_block_group);
2093                         block = ext4_new_meta_blocks(handle, inode, goal, 0,
2094                                                      NULL, &error);
2095                         if (error)
2096                                 goto cleanup;
2097
2098                         ea_idebug(inode, "creating block %llu",
2099                                   (unsigned long long)block);
2100
2101                         new_bh = sb_getblk(sb, block);
2102                         if (unlikely(!new_bh)) {
2103                                 error = -ENOMEM;
2104 getblk_failed:
2105                                 ext4_free_blocks(handle, inode, NULL, block, 1,
2106                                                  EXT4_FREE_BLOCKS_METADATA);
2107                                 goto cleanup;
2108                         }
2109                         error = ext4_xattr_inode_inc_ref_all(handle, inode,
2110                                                       ENTRY(header(s->base)+1));
2111                         if (error)
2112                                 goto getblk_failed;
2113                         if (ea_inode) {
2114                                 /* Drop the extra ref on ea_inode. */
2115                                 error = ext4_xattr_inode_dec_ref(handle,
2116                                                                  ea_inode);
2117                                 if (error)
2118                                         ext4_warning_inode(ea_inode,
2119                                                            "dec ref error=%d",
2120                                                            error);
2121                                 iput(ea_inode);
2122                                 ea_inode = NULL;
2123                         }
2124
2125                         lock_buffer(new_bh);
2126                         error = ext4_journal_get_create_access(handle, sb,
2127                                                         new_bh, EXT4_JTR_NONE);
2128                         if (error) {
2129                                 unlock_buffer(new_bh);
2130                                 error = -EIO;
2131                                 goto getblk_failed;
2132                         }
2133                         memcpy(new_bh->b_data, s->base, new_bh->b_size);
2134                         ext4_xattr_block_csum_set(inode, new_bh);
2135                         set_buffer_uptodate(new_bh);
2136                         unlock_buffer(new_bh);
2137                         ext4_xattr_block_cache_insert(ea_block_cache, new_bh);
2138                         error = ext4_handle_dirty_metadata(handle, inode,
2139                                                            new_bh);
2140                         if (error)
2141                                 goto cleanup;
2142                 }
2143         }
2144
2145         if (old_ea_inode_quota)
2146                 ext4_xattr_inode_free_quota(inode, NULL, old_ea_inode_quota);
2147
2148         /* Update the inode. */
2149         EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
2150
2151         /* Drop the previous xattr block. */
2152         if (bs->bh && bs->bh != new_bh) {
2153                 struct ext4_xattr_inode_array *ea_inode_array = NULL;
2154
2155                 ext4_xattr_release_block(handle, inode, bs->bh,
2156                                          &ea_inode_array,
2157                                          0 /* extra_credits */);
2158                 ext4_xattr_inode_array_free(ea_inode_array);
2159         }
2160         error = 0;
2161
2162 cleanup:
2163         if (ea_inode) {
2164                 int error2;
2165
2166                 error2 = ext4_xattr_inode_dec_ref(handle, ea_inode);
2167                 if (error2)
2168                         ext4_warning_inode(ea_inode, "dec ref error=%d",
2169                                            error2);
2170
2171                 /* If there was an error, revert the quota charge. */
2172                 if (error)
2173                         ext4_xattr_inode_free_quota(inode, ea_inode,
2174                                                     i_size_read(ea_inode));
2175                 iput(ea_inode);
2176         }
2177         if (ce)
2178                 mb_cache_entry_put(ea_block_cache, ce);
2179         brelse(new_bh);
2180         if (!(bs->bh && s->base == bs->bh->b_data))
2181                 kfree(s->base);
2182
2183         return error;
2184
2185 cleanup_dquot:
2186         dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
2187         goto cleanup;
2188
2189 bad_block:
2190         EXT4_ERROR_INODE(inode, "bad block %llu",
2191                          EXT4_I(inode)->i_file_acl);
2192         goto cleanup;
2193
2194 #undef header
2195 }
2196
2197 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
2198                           struct ext4_xattr_ibody_find *is)
2199 {
2200         struct ext4_xattr_ibody_header *header;
2201         struct ext4_inode *raw_inode;
2202         int error;
2203
2204         if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2205                 return 0;
2206
2207         raw_inode = ext4_raw_inode(&is->iloc);
2208         header = IHDR(inode, raw_inode);
2209         is->s.base = is->s.first = IFIRST(header);
2210         is->s.here = is->s.first;
2211         is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2212         if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2213                 error = xattr_check_inode(inode, header, is->s.end);
2214                 if (error)
2215                         return error;
2216                 /* Find the named attribute. */
2217                 error = xattr_find_entry(inode, &is->s.here, is->s.end,
2218                                          i->name_index, i->name, 0);
2219                 if (error && error != -ENODATA)
2220                         return error;
2221                 is->s.not_found = error;
2222         }
2223         return 0;
2224 }
2225
2226 int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
2227                                 struct ext4_xattr_info *i,
2228                                 struct ext4_xattr_ibody_find *is)
2229 {
2230         struct ext4_xattr_ibody_header *header;
2231         struct ext4_xattr_search *s = &is->s;
2232         int error;
2233
2234         if (!EXT4_INODE_HAS_XATTR_SPACE(inode))
2235                 return -ENOSPC;
2236
2237         error = ext4_xattr_set_entry(i, s, handle, inode, false /* is_block */);
2238         if (error)
2239                 return error;
2240         header = IHDR(inode, ext4_raw_inode(&is->iloc));
2241         if (!IS_LAST_ENTRY(s->first)) {
2242                 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
2243                 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
2244         } else {
2245                 header->h_magic = cpu_to_le32(0);
2246                 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
2247         }
2248         return 0;
2249 }
2250
2251 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
2252                                  struct ext4_xattr_info *i)
2253 {
2254         void *value;
2255
2256         /* When e_value_inum is set the value is stored externally. */
2257         if (s->here->e_value_inum)
2258                 return 0;
2259         if (le32_to_cpu(s->here->e_value_size) != i->value_len)
2260                 return 0;
2261         value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
2262         return !memcmp(value, i->value, i->value_len);
2263 }
2264
2265 static struct buffer_head *ext4_xattr_get_block(struct inode *inode)
2266 {
2267         struct buffer_head *bh;
2268         int error;
2269
2270         if (!EXT4_I(inode)->i_file_acl)
2271                 return NULL;
2272         bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2273         if (IS_ERR(bh))
2274                 return bh;
2275         error = ext4_xattr_check_block(inode, bh);
2276         if (error) {
2277                 brelse(bh);
2278                 return ERR_PTR(error);
2279         }
2280         return bh;
2281 }
2282
2283 /*
2284  * ext4_xattr_set_handle()
2285  *
2286  * Create, replace or remove an extended attribute for this inode.  Value
2287  * is NULL to remove an existing extended attribute, and non-NULL to
2288  * either replace an existing extended attribute, or create a new extended
2289  * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2290  * specify that an extended attribute must exist and must not exist
2291  * previous to the call, respectively.
2292  *
2293  * Returns 0, or a negative error number on failure.
2294  */
2295 int
2296 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
2297                       const char *name, const void *value, size_t value_len,
2298                       int flags)
2299 {
2300         struct ext4_xattr_info i = {
2301                 .name_index = name_index,
2302                 .name = name,
2303                 .value = value,
2304                 .value_len = value_len,
2305                 .in_inode = 0,
2306         };
2307         struct ext4_xattr_ibody_find is = {
2308                 .s = { .not_found = -ENODATA, },
2309         };
2310         struct ext4_xattr_block_find bs = {
2311                 .s = { .not_found = -ENODATA, },
2312         };
2313         int no_expand;
2314         int error;
2315
2316         if (!name)
2317                 return -EINVAL;
2318         if (strlen(name) > 255)
2319                 return -ERANGE;
2320
2321         ext4_write_lock_xattr(inode, &no_expand);
2322
2323         /* Check journal credits under write lock. */
2324         if (ext4_handle_valid(handle)) {
2325                 struct buffer_head *bh;
2326                 int credits;
2327
2328                 bh = ext4_xattr_get_block(inode);
2329                 if (IS_ERR(bh)) {
2330                         error = PTR_ERR(bh);
2331                         goto cleanup;
2332                 }
2333
2334                 credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2335                                                    value_len,
2336                                                    flags & XATTR_CREATE);
2337                 brelse(bh);
2338
2339                 if (jbd2_handle_buffer_credits(handle) < credits) {
2340                         error = -ENOSPC;
2341                         goto cleanup;
2342                 }
2343                 WARN_ON_ONCE(!(current->flags & PF_MEMALLOC_NOFS));
2344         }
2345
2346         error = ext4_reserve_inode_write(handle, inode, &is.iloc);
2347         if (error)
2348                 goto cleanup;
2349
2350         if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
2351                 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
2352                 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
2353                 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
2354         }
2355
2356         error = ext4_xattr_ibody_find(inode, &i, &is);
2357         if (error)
2358                 goto cleanup;
2359         if (is.s.not_found)
2360                 error = ext4_xattr_block_find(inode, &i, &bs);
2361         if (error)
2362                 goto cleanup;
2363         if (is.s.not_found && bs.s.not_found) {
2364                 error = -ENODATA;
2365                 if (flags & XATTR_REPLACE)
2366                         goto cleanup;
2367                 error = 0;
2368                 if (!value)
2369                         goto cleanup;
2370         } else {
2371                 error = -EEXIST;
2372                 if (flags & XATTR_CREATE)
2373                         goto cleanup;
2374         }
2375
2376         if (!value) {
2377                 if (!is.s.not_found)
2378                         error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2379                 else if (!bs.s.not_found)
2380                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
2381         } else {
2382                 error = 0;
2383                 /* Xattr value did not change? Save us some work and bail out */
2384                 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
2385                         goto cleanup;
2386                 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
2387                         goto cleanup;
2388
2389                 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2390                     (EXT4_XATTR_SIZE(i.value_len) >
2391                         EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
2392                         i.in_inode = 1;
2393 retry_inode:
2394                 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
2395                 if (!error && !bs.s.not_found) {
2396                         i.value = NULL;
2397                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
2398                 } else if (error == -ENOSPC) {
2399                         if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
2400                                 brelse(bs.bh);
2401                                 bs.bh = NULL;
2402                                 error = ext4_xattr_block_find(inode, &i, &bs);
2403                                 if (error)
2404                                         goto cleanup;
2405                         }
2406                         error = ext4_xattr_block_set(handle, inode, &i, &bs);
2407                         if (!error && !is.s.not_found) {
2408                                 i.value = NULL;
2409                                 error = ext4_xattr_ibody_set(handle, inode, &i,
2410                                                              &is);
2411                         } else if (error == -ENOSPC) {
2412                                 /*
2413                                  * Xattr does not fit in the block, store at
2414                                  * external inode if possible.
2415                                  */
2416                                 if (ext4_has_feature_ea_inode(inode->i_sb) &&
2417                                     i.value_len && !i.in_inode) {
2418                                         i.in_inode = 1;
2419                                         goto retry_inode;
2420                                 }
2421                         }
2422                 }
2423         }
2424         if (!error) {
2425                 ext4_xattr_update_super_block(handle, inode->i_sb);
2426                 inode->i_ctime = current_time(inode);
2427                 inode_inc_iversion(inode);
2428                 if (!value)
2429                         no_expand = 0;
2430                 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
2431                 /*
2432                  * The bh is consumed by ext4_mark_iloc_dirty, even with
2433                  * error != 0.
2434                  */
2435                 is.iloc.bh = NULL;
2436                 if (IS_SYNC(inode))
2437                         ext4_handle_sync(handle);
2438         }
2439         ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2440
2441 cleanup:
2442         brelse(is.iloc.bh);
2443         brelse(bs.bh);
2444         ext4_write_unlock_xattr(inode, &no_expand);
2445         return error;
2446 }
2447
2448 int ext4_xattr_set_credits(struct inode *inode, size_t value_len,
2449                            bool is_create, int *credits)
2450 {
2451         struct buffer_head *bh;
2452         int err;
2453
2454         *credits = 0;
2455
2456         if (!EXT4_SB(inode->i_sb)->s_journal)
2457                 return 0;
2458
2459         down_read(&EXT4_I(inode)->xattr_sem);
2460
2461         bh = ext4_xattr_get_block(inode);
2462         if (IS_ERR(bh)) {
2463                 err = PTR_ERR(bh);
2464         } else {
2465                 *credits = __ext4_xattr_set_credits(inode->i_sb, inode, bh,
2466                                                     value_len, is_create);
2467                 brelse(bh);
2468                 err = 0;
2469         }
2470
2471         up_read(&EXT4_I(inode)->xattr_sem);
2472         return err;
2473 }
2474
2475 /*
2476  * ext4_xattr_set()
2477  *
2478  * Like ext4_xattr_set_handle, but start from an inode. This extended
2479  * attribute modification is a filesystem transaction by itself.
2480  *
2481  * Returns 0, or a negative error number on failure.
2482  */
2483 int
2484 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
2485                const void *value, size_t value_len, int flags)
2486 {
2487         handle_t *handle;
2488         struct super_block *sb = inode->i_sb;
2489         int error, retries = 0;
2490         int credits;
2491
2492         error = dquot_initialize(inode);
2493         if (error)
2494                 return error;
2495
2496 retry:
2497         error = ext4_xattr_set_credits(inode, value_len, flags & XATTR_CREATE,
2498                                        &credits);
2499         if (error)
2500                 return error;
2501
2502         handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
2503         if (IS_ERR(handle)) {
2504                 error = PTR_ERR(handle);
2505         } else {
2506                 int error2;
2507
2508                 error = ext4_xattr_set_handle(handle, inode, name_index, name,
2509                                               value, value_len, flags);
2510                 error2 = ext4_journal_stop(handle);
2511                 if (error == -ENOSPC &&
2512                     ext4_should_retry_alloc(sb, &retries))
2513                         goto retry;
2514                 if (error == 0)
2515                         error = error2;
2516         }
2517         ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, NULL);
2518
2519         return error;
2520 }
2521
2522 /*
2523  * Shift the EA entries in the inode to create space for the increased
2524  * i_extra_isize.
2525  */
2526 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
2527                                      int value_offs_shift, void *to,
2528                                      void *from, size_t n)
2529 {
2530         struct ext4_xattr_entry *last = entry;
2531         int new_offs;
2532
2533         /* We always shift xattr headers further thus offsets get lower */
2534         BUG_ON(value_offs_shift > 0);
2535
2536         /* Adjust the value offsets of the entries */
2537         for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2538                 if (!last->e_value_inum && last->e_value_size) {
2539                         new_offs = le16_to_cpu(last->e_value_offs) +
2540                                                         value_offs_shift;
2541                         last->e_value_offs = cpu_to_le16(new_offs);
2542                 }
2543         }
2544         /* Shift the entries by n bytes */
2545         memmove(to, from, n);
2546 }
2547
2548 /*
2549  * Move xattr pointed to by 'entry' from inode into external xattr block
2550  */
2551 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
2552                                     struct ext4_inode *raw_inode,
2553                                     struct ext4_xattr_entry *entry)
2554 {
2555         struct ext4_xattr_ibody_find *is = NULL;
2556         struct ext4_xattr_block_find *bs = NULL;
2557         char *buffer = NULL, *b_entry_name = NULL;
2558         size_t value_size = le32_to_cpu(entry->e_value_size);
2559         struct ext4_xattr_info i = {
2560                 .value = NULL,
2561                 .value_len = 0,
2562                 .name_index = entry->e_name_index,
2563                 .in_inode = !!entry->e_value_inum,
2564         };
2565         struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2566         int needs_kvfree = 0;
2567         int error;
2568
2569         is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
2570         bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
2571         b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
2572         if (!is || !bs || !b_entry_name) {
2573                 error = -ENOMEM;
2574                 goto out;
2575         }
2576
2577         is->s.not_found = -ENODATA;
2578         bs->s.not_found = -ENODATA;
2579         is->iloc.bh = NULL;
2580         bs->bh = NULL;
2581
2582         /* Save the entry name and the entry value */
2583         if (entry->e_value_inum) {
2584                 buffer = kvmalloc(value_size, GFP_NOFS);
2585                 if (!buffer) {
2586                         error = -ENOMEM;
2587                         goto out;
2588                 }
2589                 needs_kvfree = 1;
2590                 error = ext4_xattr_inode_get(inode, entry, buffer, value_size);
2591                 if (error)
2592                         goto out;
2593         } else {
2594                 size_t value_offs = le16_to_cpu(entry->e_value_offs);
2595                 buffer = (void *)IFIRST(header) + value_offs;
2596         }
2597
2598         memcpy(b_entry_name, entry->e_name, entry->e_name_len);
2599         b_entry_name[entry->e_name_len] = '\0';
2600         i.name = b_entry_name;
2601
2602         error = ext4_get_inode_loc(inode, &is->iloc);
2603         if (error)
2604                 goto out;
2605
2606         error = ext4_xattr_ibody_find(inode, &i, is);
2607         if (error)
2608                 goto out;
2609
2610         i.value = buffer;
2611         i.value_len = value_size;
2612         error = ext4_xattr_block_find(inode, &i, bs);
2613         if (error)
2614                 goto out;
2615
2616         /* Move ea entry from the inode into the block */
2617         error = ext4_xattr_block_set(handle, inode, &i, bs);
2618         if (error)
2619                 goto out;
2620
2621         /* Remove the chosen entry from the inode */
2622         i.value = NULL;
2623         i.value_len = 0;
2624         error = ext4_xattr_ibody_set(handle, inode, &i, is);
2625
2626 out:
2627         kfree(b_entry_name);
2628         if (needs_kvfree && buffer)
2629                 kvfree(buffer);
2630         if (is)
2631                 brelse(is->iloc.bh);
2632         if (bs)
2633                 brelse(bs->bh);
2634         kfree(is);
2635         kfree(bs);
2636
2637         return error;
2638 }
2639
2640 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
2641                                        struct ext4_inode *raw_inode,
2642                                        int isize_diff, size_t ifree,
2643                                        size_t bfree, int *total_ino)
2644 {
2645         struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
2646         struct ext4_xattr_entry *small_entry;
2647         struct ext4_xattr_entry *entry;
2648         struct ext4_xattr_entry *last;
2649         unsigned int entry_size;        /* EA entry size */
2650         unsigned int total_size;        /* EA entry size + value size */
2651         unsigned int min_total_size;
2652         int error;
2653
2654         while (isize_diff > ifree) {
2655                 entry = NULL;
2656                 small_entry = NULL;
2657                 min_total_size = ~0U;
2658                 last = IFIRST(header);
2659                 /* Find the entry best suited to be pushed into EA block */
2660                 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
2661                         /* never move system.data out of the inode */
2662                         if ((last->e_name_len == 4) &&
2663                             (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
2664                             !memcmp(last->e_name, "data", 4))
2665                                 continue;
2666                         total_size = EXT4_XATTR_LEN(last->e_name_len);
2667                         if (!last->e_value_inum)
2668                                 total_size += EXT4_XATTR_SIZE(
2669                                                le32_to_cpu(last->e_value_size));
2670                         if (total_size <= bfree &&
2671                             total_size < min_total_size) {
2672                                 if (total_size + ifree < isize_diff) {
2673                                         small_entry = last;
2674                                 } else {
2675                                         entry = last;
2676                                         min_total_size = total_size;
2677                                 }
2678                         }
2679                 }
2680
2681                 if (entry == NULL) {
2682                         if (small_entry == NULL)
2683                                 return -ENOSPC;
2684                         entry = small_entry;
2685                 }
2686
2687                 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
2688                 total_size = entry_size;
2689                 if (!entry->e_value_inum)
2690                         total_size += EXT4_XATTR_SIZE(
2691                                               le32_to_cpu(entry->e_value_size));
2692                 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
2693                                                  entry);
2694                 if (error)
2695                         return error;
2696
2697                 *total_ino -= entry_size;
2698                 ifree += total_size;
2699                 bfree -= total_size;
2700         }
2701
2702         return 0;
2703 }
2704
2705 /*
2706  * Expand an inode by new_extra_isize bytes when EAs are present.
2707  * Returns 0 on success or negative error number on failure.
2708  */
2709 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
2710                                struct ext4_inode *raw_inode, handle_t *handle)
2711 {
2712         struct ext4_xattr_ibody_header *header;
2713         struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
2714         static unsigned int mnt_count;
2715         size_t min_offs;
2716         size_t ifree, bfree;
2717         int total_ino;
2718         void *base, *end;
2719         int error = 0, tried_min_extra_isize = 0;
2720         int s_min_extra_isize = le16_to_cpu(sbi->s_es->s_min_extra_isize);
2721         int isize_diff; /* How much do we need to grow i_extra_isize */
2722
2723 retry:
2724         isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2725         if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
2726                 return 0;
2727
2728         header = IHDR(inode, raw_inode);
2729
2730         /*
2731          * Check if enough free space is available in the inode to shift the
2732          * entries ahead by new_extra_isize.
2733          */
2734
2735         base = IFIRST(header);
2736         end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
2737         min_offs = end - base;
2738         total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
2739
2740         error = xattr_check_inode(inode, header, end);
2741         if (error)
2742                 goto cleanup;
2743
2744         ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
2745         if (ifree >= isize_diff)
2746                 goto shift;
2747
2748         /*
2749          * Enough free space isn't available in the inode, check if
2750          * EA block can hold new_extra_isize bytes.
2751          */
2752         if (EXT4_I(inode)->i_file_acl) {
2753                 struct buffer_head *bh;
2754
2755                 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2756                 if (IS_ERR(bh)) {
2757                         error = PTR_ERR(bh);
2758                         goto cleanup;
2759                 }
2760                 error = ext4_xattr_check_block(inode, bh);
2761                 if (error) {
2762                         brelse(bh);
2763                         goto cleanup;
2764                 }
2765                 base = BHDR(bh);
2766                 end = bh->b_data + bh->b_size;
2767                 min_offs = end - base;
2768                 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
2769                                               NULL);
2770                 brelse(bh);
2771                 if (bfree + ifree < isize_diff) {
2772                         if (!tried_min_extra_isize && s_min_extra_isize) {
2773                                 tried_min_extra_isize++;
2774                                 new_extra_isize = s_min_extra_isize;
2775                                 goto retry;
2776                         }
2777                         error = -ENOSPC;
2778                         goto cleanup;
2779                 }
2780         } else {
2781                 bfree = inode->i_sb->s_blocksize;
2782         }
2783
2784         error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
2785                                             isize_diff, ifree, bfree,
2786                                             &total_ino);
2787         if (error) {
2788                 if (error == -ENOSPC && !tried_min_extra_isize &&
2789                     s_min_extra_isize) {
2790                         tried_min_extra_isize++;
2791                         new_extra_isize = s_min_extra_isize;
2792                         goto retry;
2793                 }
2794                 goto cleanup;
2795         }
2796 shift:
2797         /* Adjust the offsets and shift the remaining entries ahead */
2798         ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
2799                         - new_extra_isize, (void *)raw_inode +
2800                         EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
2801                         (void *)header, total_ino);
2802         EXT4_I(inode)->i_extra_isize = new_extra_isize;
2803
2804         if (ext4_has_inline_data(inode))
2805                 error = ext4_find_inline_data_nolock(inode);
2806
2807 cleanup:
2808         if (error && (mnt_count != le16_to_cpu(sbi->s_es->s_mnt_count))) {
2809                 ext4_warning(inode->i_sb, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2810                              inode->i_ino);
2811                 mnt_count = le16_to_cpu(sbi->s_es->s_mnt_count);
2812         }
2813         return error;
2814 }
2815
2816 #define EIA_INCR 16 /* must be 2^n */
2817 #define EIA_MASK (EIA_INCR - 1)
2818
2819 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2820  * If @ea_inode_array is new or full it will be grown and the old
2821  * contents copied over.
2822  */
2823 static int
2824 ext4_expand_inode_array(struct ext4_xattr_inode_array **ea_inode_array,
2825                         struct inode *inode)
2826 {
2827         if (*ea_inode_array == NULL) {
2828                 /*
2829                  * Start with 15 inodes, so it fits into a power-of-two size.
2830                  * If *ea_inode_array is NULL, this is essentially offsetof()
2831                  */
2832                 (*ea_inode_array) =
2833                         kmalloc(offsetof(struct ext4_xattr_inode_array,
2834                                          inodes[EIA_MASK]),
2835                                 GFP_NOFS);
2836                 if (*ea_inode_array == NULL)
2837                         return -ENOMEM;
2838                 (*ea_inode_array)->count = 0;
2839         } else if (((*ea_inode_array)->count & EIA_MASK) == EIA_MASK) {
2840                 /* expand the array once all 15 + n * 16 slots are full */
2841                 struct ext4_xattr_inode_array *new_array = NULL;
2842                 int count = (*ea_inode_array)->count;
2843
2844                 /* if new_array is NULL, this is essentially offsetof() */
2845                 new_array = kmalloc(
2846                                 offsetof(struct ext4_xattr_inode_array,
2847                                          inodes[count + EIA_INCR]),
2848                                 GFP_NOFS);
2849                 if (new_array == NULL)
2850                         return -ENOMEM;
2851                 memcpy(new_array, *ea_inode_array,
2852                        offsetof(struct ext4_xattr_inode_array, inodes[count]));
2853                 kfree(*ea_inode_array);
2854                 *ea_inode_array = new_array;
2855         }
2856         (*ea_inode_array)->inodes[(*ea_inode_array)->count++] = inode;
2857         return 0;
2858 }
2859
2860 /*
2861  * ext4_xattr_delete_inode()
2862  *
2863  * Free extended attribute resources associated with this inode. Traverse
2864  * all entries and decrement reference on any xattr inodes associated with this
2865  * inode. This is called immediately before an inode is freed. We have exclusive
2866  * access to the inode. If an orphan inode is deleted it will also release its
2867  * references on xattr block and xattr inodes.
2868  */
2869 int ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
2870                             struct ext4_xattr_inode_array **ea_inode_array,
2871                             int extra_credits)
2872 {
2873         struct buffer_head *bh = NULL;
2874         struct ext4_xattr_ibody_header *header;
2875         struct ext4_iloc iloc = { .bh = NULL };
2876         struct ext4_xattr_entry *entry;
2877         struct inode *ea_inode;
2878         int error;
2879
2880         error = ext4_journal_ensure_credits(handle, extra_credits,
2881                         ext4_free_metadata_revoke_credits(inode->i_sb, 1));
2882         if (error < 0) {
2883                 EXT4_ERROR_INODE(inode, "ensure credits (error %d)", error);
2884                 goto cleanup;
2885         }
2886
2887         if (ext4_has_feature_ea_inode(inode->i_sb) &&
2888             ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
2889
2890                 error = ext4_get_inode_loc(inode, &iloc);
2891                 if (error) {
2892                         EXT4_ERROR_INODE(inode, "inode loc (error %d)", error);
2893                         goto cleanup;
2894                 }
2895
2896                 error = ext4_journal_get_write_access(handle, inode->i_sb,
2897                                                 iloc.bh, EXT4_JTR_NONE);
2898                 if (error) {
2899                         EXT4_ERROR_INODE(inode, "write access (error %d)",
2900                                          error);
2901                         goto cleanup;
2902                 }
2903
2904                 header = IHDR(inode, ext4_raw_inode(&iloc));
2905                 if (header->h_magic == cpu_to_le32(EXT4_XATTR_MAGIC))
2906                         ext4_xattr_inode_dec_ref_all(handle, inode, iloc.bh,
2907                                                      IFIRST(header),
2908                                                      false /* block_csum */,
2909                                                      ea_inode_array,
2910                                                      extra_credits,
2911                                                      false /* skip_quota */);
2912         }
2913
2914         if (EXT4_I(inode)->i_file_acl) {
2915                 bh = ext4_sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl, REQ_PRIO);
2916                 if (IS_ERR(bh)) {
2917                         error = PTR_ERR(bh);
2918                         if (error == -EIO) {
2919                                 EXT4_ERROR_INODE_ERR(inode, EIO,
2920                                                      "block %llu read error",
2921                                                      EXT4_I(inode)->i_file_acl);
2922                         }
2923                         bh = NULL;
2924                         goto cleanup;
2925                 }
2926                 error = ext4_xattr_check_block(inode, bh);
2927                 if (error)
2928                         goto cleanup;
2929
2930                 if (ext4_has_feature_ea_inode(inode->i_sb)) {
2931                         for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2932                              entry = EXT4_XATTR_NEXT(entry)) {
2933                                 if (!entry->e_value_inum)
2934                                         continue;
2935                                 error = ext4_xattr_inode_iget(inode,
2936                                               le32_to_cpu(entry->e_value_inum),
2937                                               le32_to_cpu(entry->e_hash),
2938                                               &ea_inode);
2939                                 if (error)
2940                                         continue;
2941                                 ext4_xattr_inode_free_quota(inode, ea_inode,
2942                                               le32_to_cpu(entry->e_value_size));
2943                                 iput(ea_inode);
2944                         }
2945
2946                 }
2947
2948                 ext4_xattr_release_block(handle, inode, bh, ea_inode_array,
2949                                          extra_credits);
2950                 /*
2951                  * Update i_file_acl value in the same transaction that releases
2952                  * block.
2953                  */
2954                 EXT4_I(inode)->i_file_acl = 0;
2955                 error = ext4_mark_inode_dirty(handle, inode);
2956                 if (error) {
2957                         EXT4_ERROR_INODE(inode, "mark inode dirty (error %d)",
2958                                          error);
2959                         goto cleanup;
2960                 }
2961                 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_XATTR, handle);
2962         }
2963         error = 0;
2964 cleanup:
2965         brelse(iloc.bh);
2966         brelse(bh);
2967         return error;
2968 }
2969
2970 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array *ea_inode_array)
2971 {
2972         int idx;
2973
2974         if (ea_inode_array == NULL)
2975                 return;
2976
2977         for (idx = 0; idx < ea_inode_array->count; ++idx)
2978                 iput(ea_inode_array->inodes[idx]);
2979         kfree(ea_inode_array);
2980 }
2981
2982 /*
2983  * ext4_xattr_block_cache_insert()
2984  *
2985  * Create a new entry in the extended attribute block cache, and insert
2986  * it unless such an entry is already in the cache.
2987  *
2988  * Returns 0, or a negative error number on failure.
2989  */
2990 static void
2991 ext4_xattr_block_cache_insert(struct mb_cache *ea_block_cache,
2992                               struct buffer_head *bh)
2993 {
2994         struct ext4_xattr_header *header = BHDR(bh);
2995         __u32 hash = le32_to_cpu(header->h_hash);
2996         int reusable = le32_to_cpu(header->h_refcount) <
2997                        EXT4_XATTR_REFCOUNT_MAX;
2998         int error;
2999
3000         if (!ea_block_cache)
3001                 return;
3002         error = mb_cache_entry_create(ea_block_cache, GFP_NOFS, hash,
3003                                       bh->b_blocknr, reusable);
3004         if (error) {
3005                 if (error == -EBUSY)
3006                         ea_bdebug(bh, "already in cache");
3007         } else
3008                 ea_bdebug(bh, "inserting [%x]", (int)hash);
3009 }
3010
3011 /*
3012  * ext4_xattr_cmp()
3013  *
3014  * Compare two extended attribute blocks for equality.
3015  *
3016  * Returns 0 if the blocks are equal, 1 if they differ, and
3017  * a negative error number on errors.
3018  */
3019 static int
3020 ext4_xattr_cmp(struct ext4_xattr_header *header1,
3021                struct ext4_xattr_header *header2)
3022 {
3023         struct ext4_xattr_entry *entry1, *entry2;
3024
3025         entry1 = ENTRY(header1+1);
3026         entry2 = ENTRY(header2+1);
3027         while (!IS_LAST_ENTRY(entry1)) {
3028                 if (IS_LAST_ENTRY(entry2))
3029                         return 1;
3030                 if (entry1->e_hash != entry2->e_hash ||
3031                     entry1->e_name_index != entry2->e_name_index ||
3032                     entry1->e_name_len != entry2->e_name_len ||
3033                     entry1->e_value_size != entry2->e_value_size ||
3034                     entry1->e_value_inum != entry2->e_value_inum ||
3035                     memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
3036                         return 1;
3037                 if (!entry1->e_value_inum &&
3038                     memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
3039                            (char *)header2 + le16_to_cpu(entry2->e_value_offs),
3040                            le32_to_cpu(entry1->e_value_size)))
3041                         return 1;
3042
3043                 entry1 = EXT4_XATTR_NEXT(entry1);
3044                 entry2 = EXT4_XATTR_NEXT(entry2);
3045         }
3046         if (!IS_LAST_ENTRY(entry2))
3047                 return 1;
3048         return 0;
3049 }
3050
3051 /*
3052  * ext4_xattr_block_cache_find()
3053  *
3054  * Find an identical extended attribute block.
3055  *
3056  * Returns a pointer to the block found, or NULL if such a block was
3057  * not found or an error occurred.
3058  */
3059 static struct buffer_head *
3060 ext4_xattr_block_cache_find(struct inode *inode,
3061                             struct ext4_xattr_header *header,
3062                             struct mb_cache_entry **pce)
3063 {
3064         __u32 hash = le32_to_cpu(header->h_hash);
3065         struct mb_cache_entry *ce;
3066         struct mb_cache *ea_block_cache = EA_BLOCK_CACHE(inode);
3067
3068         if (!ea_block_cache)
3069                 return NULL;
3070         if (!header->h_hash)
3071                 return NULL;  /* never share */
3072         ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
3073         ce = mb_cache_entry_find_first(ea_block_cache, hash);
3074         while (ce) {
3075                 struct buffer_head *bh;
3076
3077                 bh = ext4_sb_bread(inode->i_sb, ce->e_value, REQ_PRIO);
3078                 if (IS_ERR(bh)) {
3079                         if (PTR_ERR(bh) == -ENOMEM)
3080                                 return NULL;
3081                         bh = NULL;
3082                         EXT4_ERROR_INODE(inode, "block %lu read error",
3083                                          (unsigned long)ce->e_value);
3084                 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
3085                         *pce = ce;
3086                         return bh;
3087                 }
3088                 brelse(bh);
3089                 ce = mb_cache_entry_find_next(ea_block_cache, ce);
3090         }
3091         return NULL;
3092 }
3093
3094 #define NAME_HASH_SHIFT 5
3095 #define VALUE_HASH_SHIFT 16
3096
3097 /*
3098  * ext4_xattr_hash_entry()
3099  *
3100  * Compute the hash of an extended attribute.
3101  */
3102 static __le32 ext4_xattr_hash_entry(char *name, size_t name_len, __le32 *value,
3103                                     size_t value_count)
3104 {
3105         __u32 hash = 0;
3106
3107         while (name_len--) {
3108                 hash = (hash << NAME_HASH_SHIFT) ^
3109                        (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
3110                        *name++;
3111         }
3112         while (value_count--) {
3113                 hash = (hash << VALUE_HASH_SHIFT) ^
3114                        (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
3115                        le32_to_cpu(*value++);
3116         }
3117         return cpu_to_le32(hash);
3118 }
3119
3120 #undef NAME_HASH_SHIFT
3121 #undef VALUE_HASH_SHIFT
3122
3123 #define BLOCK_HASH_SHIFT 16
3124
3125 /*
3126  * ext4_xattr_rehash()
3127  *
3128  * Re-compute the extended attribute hash value after an entry has changed.
3129  */
3130 static void ext4_xattr_rehash(struct ext4_xattr_header *header)
3131 {
3132         struct ext4_xattr_entry *here;
3133         __u32 hash = 0;
3134
3135         here = ENTRY(header+1);
3136         while (!IS_LAST_ENTRY(here)) {
3137                 if (!here->e_hash) {
3138                         /* Block is not shared if an entry's hash value == 0 */
3139                         hash = 0;
3140                         break;
3141                 }
3142                 hash = (hash << BLOCK_HASH_SHIFT) ^
3143                        (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
3144                        le32_to_cpu(here->e_hash);
3145                 here = EXT4_XATTR_NEXT(here);
3146         }
3147         header->h_hash = cpu_to_le32(hash);
3148 }
3149
3150 #undef BLOCK_HASH_SHIFT
3151
3152 #define HASH_BUCKET_BITS        10
3153
3154 struct mb_cache *
3155 ext4_xattr_create_cache(void)
3156 {
3157         return mb_cache_create(HASH_BUCKET_BITS);
3158 }
3159
3160 void ext4_xattr_destroy_cache(struct mb_cache *cache)
3161 {
3162         if (cache)
3163                 mb_cache_destroy(cache);
3164 }
3165