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