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