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