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