ext4: don't read out of bounds when checking for in-inode xattrs
[linux-block.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
7a2508e1 75static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
617ba13b
MC
76static struct buffer_head *ext4_xattr_cache_find(struct inode *,
77 struct ext4_xattr_header *,
7a2508e1 78 struct mb_cache_entry **);
617ba13b
MC
79static void ext4_xattr_rehash(struct ext4_xattr_header *,
80 struct ext4_xattr_entry *);
431547b3 81static int ext4_xattr_list(struct dentry *dentry, char *buffer,
d3a95d47 82 size_t buffer_size);
ac27a0ec 83
11e27528 84static const struct xattr_handler *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
9c191f70
M
109#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_mb_cache)
111
cc8e94fd
DW
112static __le32 ext4_xattr_block_csum(struct inode *inode,
113 sector_t block_nr,
114 struct ext4_xattr_header *hdr)
115{
116 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
d6a77105 117 __u32 csum;
d6a77105 118 __le64 dsk_block_nr = cpu_to_le64(block_nr);
b47820ed
DJ
119 __u32 dummy_csum = 0;
120 int offset = offsetof(struct ext4_xattr_header, h_checksum);
cc8e94fd 121
d6a77105
TT
122 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
123 sizeof(dsk_block_nr));
b47820ed
DJ
124 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
125 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
126 offset += sizeof(dummy_csum);
127 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
128 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
41eb70dd 129
cc8e94fd
DW
130 return cpu_to_le32(csum);
131}
132
133static int ext4_xattr_block_csum_verify(struct inode *inode,
134 sector_t block_nr,
135 struct ext4_xattr_header *hdr)
136{
9aa5d32b 137 if (ext4_has_metadata_csum(inode->i_sb) &&
cc8e94fd
DW
138 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
139 return 0;
140 return 1;
141}
142
143static void ext4_xattr_block_csum_set(struct inode *inode,
144 sector_t block_nr,
145 struct ext4_xattr_header *hdr)
146{
9aa5d32b 147 if (!ext4_has_metadata_csum(inode->i_sb))
cc8e94fd
DW
148 return;
149
150 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
151}
152
153static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
154 struct inode *inode,
155 struct buffer_head *bh)
156{
157 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
158 return ext4_handle_dirty_metadata(handle, inode, bh);
159}
160
11e27528 161static inline const struct xattr_handler *
617ba13b 162ext4_xattr_handler(int name_index)
ac27a0ec 163{
11e27528 164 const struct xattr_handler *handler = NULL;
ac27a0ec 165
617ba13b
MC
166 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
167 handler = ext4_xattr_handler_map[name_index];
ac27a0ec
DK
168 return handler;
169}
170
171/*
172 * Inode operation listxattr()
173 *
2b0143b5 174 * d_inode(dentry)->i_mutex: don't care
ac27a0ec
DK
175 */
176ssize_t
617ba13b 177ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
ac27a0ec 178{
431547b3 179 return ext4_xattr_list(dentry, buffer, size);
ac27a0ec
DK
180}
181
182static int
a0626e75
DW
183ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
184 void *value_start)
ac27a0ec 185{
a0626e75
DW
186 struct ext4_xattr_entry *e = entry;
187
188 while (!IS_LAST_ENTRY(e)) {
189 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
ac27a0ec 190 if ((void *)next >= end)
6a797d27 191 return -EFSCORRUPTED;
a0626e75 192 e = next;
ac27a0ec 193 }
a0626e75
DW
194
195 while (!IS_LAST_ENTRY(entry)) {
2de58f11
JK
196 if (entry->e_value_block != 0)
197 return -EFSCORRUPTED;
a0626e75
DW
198 if (entry->e_value_size != 0 &&
199 (value_start + le16_to_cpu(entry->e_value_offs) <
200 (void *)e + sizeof(__u32) ||
201 value_start + le16_to_cpu(entry->e_value_offs) +
202 le32_to_cpu(entry->e_value_size) > end))
6a797d27 203 return -EFSCORRUPTED;
a0626e75
DW
204 entry = EXT4_XATTR_NEXT(entry);
205 }
206
ac27a0ec
DK
207 return 0;
208}
209
210static inline int
cc8e94fd 211ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
ac27a0ec 212{
cc8e94fd
DW
213 int error;
214
215 if (buffer_verified(bh))
216 return 0;
217
617ba13b 218 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
ac27a0ec 219 BHDR(bh)->h_blocks != cpu_to_le32(1))
6a797d27 220 return -EFSCORRUPTED;
cc8e94fd 221 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
6a797d27 222 return -EFSBADCRC;
a0626e75
DW
223 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
224 bh->b_data);
cc8e94fd
DW
225 if (!error)
226 set_buffer_verified(bh);
227 return error;
ac27a0ec
DK
228}
229
9e92f48c
TT
230static int
231__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
232 void *end, const char *function, unsigned int line)
233{
9e92f48c
TT
234 int error = -EFSCORRUPTED;
235
290ab230 236 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
19962509 237 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
9e92f48c 238 goto errout;
290ab230 239 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
9e92f48c
TT
240errout:
241 if (error)
242 __ext4_error_inode(inode, function, line, 0,
243 "corrupted in-inode xattr");
244 return error;
245}
246
247#define xattr_check_inode(inode, header, end) \
248 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
249
ac27a0ec 250static inline int
617ba13b 251ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
ac27a0ec
DK
252{
253 size_t value_size = le32_to_cpu(entry->e_value_size);
254
255 if (entry->e_value_block != 0 || value_size > size ||
256 le16_to_cpu(entry->e_value_offs) + value_size > size)
6a797d27 257 return -EFSCORRUPTED;
ac27a0ec
DK
258 return 0;
259}
260
261static int
617ba13b 262ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
ac27a0ec
DK
263 const char *name, size_t size, int sorted)
264{
617ba13b 265 struct ext4_xattr_entry *entry;
ac27a0ec
DK
266 size_t name_len;
267 int cmp = 1;
268
269 if (name == NULL)
270 return -EINVAL;
271 name_len = strlen(name);
272 entry = *pentry;
617ba13b 273 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
ac27a0ec
DK
274 cmp = name_index - entry->e_name_index;
275 if (!cmp)
276 cmp = name_len - entry->e_name_len;
277 if (!cmp)
278 cmp = memcmp(name, entry->e_name, name_len);
279 if (cmp <= 0 && (sorted || cmp == 0))
280 break;
281 }
282 *pentry = entry;
617ba13b 283 if (!cmp && ext4_xattr_check_entry(entry, size))
6a797d27 284 return -EFSCORRUPTED;
ac27a0ec
DK
285 return cmp ? -ENODATA : 0;
286}
287
288static int
617ba13b 289ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
290 void *buffer, size_t buffer_size)
291{
292 struct buffer_head *bh = NULL;
617ba13b 293 struct ext4_xattr_entry *entry;
ac27a0ec
DK
294 size_t size;
295 int error;
7a2508e1 296 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
ac27a0ec
DK
297
298 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
299 name_index, name, buffer, (long)buffer_size);
300
301 error = -ENODATA;
617ba13b 302 if (!EXT4_I(inode)->i_file_acl)
ac27a0ec 303 goto cleanup;
ace36ad4
JP
304 ea_idebug(inode, "reading block %llu",
305 (unsigned long long)EXT4_I(inode)->i_file_acl);
617ba13b 306 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
307 if (!bh)
308 goto cleanup;
309 ea_bdebug(bh, "b_count=%d, refcount=%d",
310 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
cc8e94fd 311 if (ext4_xattr_check_block(inode, bh)) {
12062ddd 312bad_block:
24676da4
TT
313 EXT4_ERROR_INODE(inode, "bad block %llu",
314 EXT4_I(inode)->i_file_acl);
6a797d27 315 error = -EFSCORRUPTED;
ac27a0ec
DK
316 goto cleanup;
317 }
9c191f70 318 ext4_xattr_cache_insert(ext4_mb_cache, bh);
ac27a0ec 319 entry = BFIRST(bh);
617ba13b 320 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
6a797d27 321 if (error == -EFSCORRUPTED)
ac27a0ec
DK
322 goto bad_block;
323 if (error)
324 goto cleanup;
325 size = le32_to_cpu(entry->e_value_size);
326 if (buffer) {
327 error = -ERANGE;
328 if (size > buffer_size)
329 goto cleanup;
330 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
331 size);
332 }
333 error = size;
334
335cleanup:
336 brelse(bh);
337 return error;
338}
339
879b3825 340int
617ba13b 341ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
342 void *buffer, size_t buffer_size)
343{
617ba13b
MC
344 struct ext4_xattr_ibody_header *header;
345 struct ext4_xattr_entry *entry;
346 struct ext4_inode *raw_inode;
347 struct ext4_iloc iloc;
ac27a0ec
DK
348 size_t size;
349 void *end;
350 int error;
351
19f5fb7a 352 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
ac27a0ec 353 return -ENODATA;
617ba13b 354 error = ext4_get_inode_loc(inode, &iloc);
ac27a0ec
DK
355 if (error)
356 return error;
617ba13b 357 raw_inode = ext4_raw_inode(&iloc);
ac27a0ec
DK
358 header = IHDR(inode, raw_inode);
359 entry = IFIRST(header);
617ba13b 360 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
9e92f48c 361 error = xattr_check_inode(inode, header, end);
ac27a0ec
DK
362 if (error)
363 goto cleanup;
617ba13b 364 error = ext4_xattr_find_entry(&entry, name_index, name,
ac27a0ec
DK
365 end - (void *)entry, 0);
366 if (error)
367 goto cleanup;
368 size = le32_to_cpu(entry->e_value_size);
369 if (buffer) {
370 error = -ERANGE;
371 if (size > buffer_size)
372 goto cleanup;
373 memcpy(buffer, (void *)IFIRST(header) +
374 le16_to_cpu(entry->e_value_offs), size);
375 }
376 error = size;
377
378cleanup:
379 brelse(iloc.bh);
380 return error;
381}
382
383/*
617ba13b 384 * ext4_xattr_get()
ac27a0ec
DK
385 *
386 * Copy an extended attribute into the buffer
387 * provided, or compute the buffer size required.
388 * Buffer is NULL to compute the size of the buffer required.
389 *
390 * Returns a negative error number on failure, or the number of bytes
391 * used / required on success.
392 */
393int
617ba13b 394ext4_xattr_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
395 void *buffer, size_t buffer_size)
396{
397 int error;
398
230b8c1a
ZZ
399 if (strlen(name) > 255)
400 return -ERANGE;
401
617ba13b
MC
402 down_read(&EXT4_I(inode)->xattr_sem);
403 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
ac27a0ec
DK
404 buffer_size);
405 if (error == -ENODATA)
617ba13b 406 error = ext4_xattr_block_get(inode, name_index, name, buffer,
ac27a0ec 407 buffer_size);
617ba13b 408 up_read(&EXT4_I(inode)->xattr_sem);
ac27a0ec
DK
409 return error;
410}
411
412static int
431547b3 413ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
ac27a0ec
DK
414 char *buffer, size_t buffer_size)
415{
416 size_t rest = buffer_size;
417
617ba13b 418 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
11e27528 419 const struct xattr_handler *handler =
617ba13b 420 ext4_xattr_handler(entry->e_name_index);
ac27a0ec 421
764a5c6b
AG
422 if (handler && (!handler->list || handler->list(dentry))) {
423 const char *prefix = handler->prefix ?: handler->name;
424 size_t prefix_len = strlen(prefix);
425 size_t size = prefix_len + entry->e_name_len + 1;
426
ac27a0ec
DK
427 if (buffer) {
428 if (size > rest)
429 return -ERANGE;
764a5c6b
AG
430 memcpy(buffer, prefix, prefix_len);
431 buffer += prefix_len;
432 memcpy(buffer, entry->e_name, entry->e_name_len);
433 buffer += entry->e_name_len;
434 *buffer++ = 0;
ac27a0ec
DK
435 }
436 rest -= size;
437 }
438 }
764a5c6b 439 return buffer_size - rest; /* total size */
ac27a0ec
DK
440}
441
442static int
431547b3 443ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 444{
2b0143b5 445 struct inode *inode = d_inode(dentry);
ac27a0ec
DK
446 struct buffer_head *bh = NULL;
447 int error;
7a2508e1 448 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
ac27a0ec
DK
449
450 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
451 buffer, (long)buffer_size);
452
453 error = 0;
617ba13b 454 if (!EXT4_I(inode)->i_file_acl)
ac27a0ec 455 goto cleanup;
ace36ad4
JP
456 ea_idebug(inode, "reading block %llu",
457 (unsigned long long)EXT4_I(inode)->i_file_acl);
617ba13b 458 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
459 error = -EIO;
460 if (!bh)
461 goto cleanup;
462 ea_bdebug(bh, "b_count=%d, refcount=%d",
463 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
cc8e94fd 464 if (ext4_xattr_check_block(inode, bh)) {
24676da4
TT
465 EXT4_ERROR_INODE(inode, "bad block %llu",
466 EXT4_I(inode)->i_file_acl);
6a797d27 467 error = -EFSCORRUPTED;
ac27a0ec
DK
468 goto cleanup;
469 }
9c191f70 470 ext4_xattr_cache_insert(ext4_mb_cache, bh);
431547b3 471 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
ac27a0ec
DK
472
473cleanup:
474 brelse(bh);
475
476 return error;
477}
478
479static int
431547b3 480ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 481{
2b0143b5 482 struct inode *inode = d_inode(dentry);
617ba13b
MC
483 struct ext4_xattr_ibody_header *header;
484 struct ext4_inode *raw_inode;
485 struct ext4_iloc iloc;
ac27a0ec
DK
486 void *end;
487 int error;
488
19f5fb7a 489 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
ac27a0ec 490 return 0;
617ba13b 491 error = ext4_get_inode_loc(inode, &iloc);
ac27a0ec
DK
492 if (error)
493 return error;
617ba13b 494 raw_inode = ext4_raw_inode(&iloc);
ac27a0ec 495 header = IHDR(inode, raw_inode);
617ba13b 496 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
9e92f48c 497 error = xattr_check_inode(inode, header, end);
ac27a0ec
DK
498 if (error)
499 goto cleanup;
431547b3 500 error = ext4_xattr_list_entries(dentry, IFIRST(header),
ac27a0ec
DK
501 buffer, buffer_size);
502
503cleanup:
504 brelse(iloc.bh);
505 return error;
506}
507
508/*
617ba13b 509 * ext4_xattr_list()
ac27a0ec
DK
510 *
511 * Copy a list of attribute names into the buffer
512 * provided, or compute the buffer size required.
513 * Buffer is NULL to compute the size of the buffer required.
514 *
515 * Returns a negative error number on failure, or the number of bytes
516 * used / required on success.
517 */
d3a95d47 518static int
431547b3 519ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 520{
eaeef867 521 int ret, ret2;
ac27a0ec 522
2b0143b5 523 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
eaeef867
TT
524 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
525 if (ret < 0)
526 goto errout;
527 if (buffer) {
528 buffer += ret;
529 buffer_size -= ret;
ac27a0ec 530 }
eaeef867
TT
531 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
532 if (ret < 0)
533 goto errout;
534 ret += ret2;
535errout:
2b0143b5 536 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
eaeef867 537 return ret;
ac27a0ec
DK
538}
539
540/*
617ba13b 541 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
ac27a0ec
DK
542 * not set, set it.
543 */
617ba13b 544static void ext4_xattr_update_super_block(handle_t *handle,
ac27a0ec
DK
545 struct super_block *sb)
546{
e2b911c5 547 if (ext4_has_feature_xattr(sb))
ac27a0ec
DK
548 return;
549
5d601255 550 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
617ba13b 551 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
e2b911c5 552 ext4_set_feature_xattr(sb);
a0375156 553 ext4_handle_dirty_super(handle, sb);
ac27a0ec 554 }
ac27a0ec
DK
555}
556
557/*
ec4cb1aa
JK
558 * Release the xattr block BH: If the reference count is > 1, decrement it;
559 * otherwise free the block.
ac27a0ec
DK
560 */
561static void
617ba13b 562ext4_xattr_release_block(handle_t *handle, struct inode *inode,
ac27a0ec
DK
563 struct buffer_head *bh)
564{
6048c64b
AG
565 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
566 u32 hash, ref;
8a2bfdcb 567 int error = 0;
ac27a0ec 568
5d601255 569 BUFFER_TRACE(bh, "get_write_access");
8a2bfdcb
MC
570 error = ext4_journal_get_write_access(handle, bh);
571 if (error)
572 goto out;
573
574 lock_buffer(bh);
6048c64b
AG
575 hash = le32_to_cpu(BHDR(bh)->h_hash);
576 ref = le32_to_cpu(BHDR(bh)->h_refcount);
577 if (ref == 1) {
ac27a0ec 578 ea_bdebug(bh, "refcount now=0; freeing");
82939d79
JK
579 /*
580 * This must happen under buffer lock for
581 * ext4_xattr_block_set() to reliably detect freed block
582 */
6048c64b 583 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
ac27a0ec 584 get_bh(bh);
ec4cb1aa 585 unlock_buffer(bh);
e6362609
TT
586 ext4_free_blocks(handle, inode, bh, 0, 1,
587 EXT4_FREE_BLOCKS_METADATA |
588 EXT4_FREE_BLOCKS_FORGET);
ac27a0ec 589 } else {
6048c64b
AG
590 ref--;
591 BHDR(bh)->h_refcount = cpu_to_le32(ref);
592 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
593 struct mb_cache_entry *ce;
594
595 ce = mb_cache_entry_get(ext4_mb_cache, hash,
596 bh->b_blocknr);
597 if (ce) {
598 ce->e_reusable = 1;
599 mb_cache_entry_put(ext4_mb_cache, ce);
600 }
601 }
602
ec4cb1aa
JK
603 /*
604 * Beware of this ugliness: Releasing of xattr block references
605 * from different inodes can race and so we have to protect
606 * from a race where someone else frees the block (and releases
607 * its journal_head) before we are done dirtying the buffer. In
608 * nojournal mode this race is harmless and we actually cannot
609 * call ext4_handle_dirty_xattr_block() with locked buffer as
610 * that function can call sync_dirty_buffer() so for that case
611 * we handle the dirtying after unlocking the buffer.
612 */
613 if (ext4_handle_valid(handle))
614 error = ext4_handle_dirty_xattr_block(handle, inode,
615 bh);
c1bb05a6 616 unlock_buffer(bh);
ec4cb1aa
JK
617 if (!ext4_handle_valid(handle))
618 error = ext4_handle_dirty_xattr_block(handle, inode,
619 bh);
8a2bfdcb 620 if (IS_SYNC(inode))
0390131b 621 ext4_handle_sync(handle);
1231b3a1 622 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
8a2bfdcb
MC
623 ea_bdebug(bh, "refcount now=%d; releasing",
624 le32_to_cpu(BHDR(bh)->h_refcount));
ac27a0ec 625 }
8a2bfdcb
MC
626out:
627 ext4_std_error(inode->i_sb, error);
628 return;
ac27a0ec
DK
629}
630
6dd4ee7c
KS
631/*
632 * Find the available free space for EAs. This also returns the total number of
633 * bytes used by EA entries.
634 */
635static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
636 size_t *min_offs, void *base, int *total)
637{
638 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1cba4237 639 if (last->e_value_size) {
6dd4ee7c
KS
640 size_t offs = le16_to_cpu(last->e_value_offs);
641 if (offs < *min_offs)
642 *min_offs = offs;
643 }
7b1b2c1b
TT
644 if (total)
645 *total += EXT4_XATTR_LEN(last->e_name_len);
6dd4ee7c
KS
646 }
647 return (*min_offs - ((void *)last - base) - sizeof(__u32));
648}
649
ac27a0ec 650static int
617ba13b 651ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
ac27a0ec 652{
617ba13b 653 struct ext4_xattr_entry *last;
ac27a0ec
DK
654 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
655
656 /* Compute min_offs and last. */
657 last = s->first;
617ba13b 658 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1cba4237 659 if (last->e_value_size) {
ac27a0ec
DK
660 size_t offs = le16_to_cpu(last->e_value_offs);
661 if (offs < min_offs)
662 min_offs = offs;
663 }
664 }
665 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
666 if (!s->not_found) {
1cba4237 667 if (s->here->e_value_size) {
ac27a0ec 668 size_t size = le32_to_cpu(s->here->e_value_size);
617ba13b 669 free += EXT4_XATTR_SIZE(size);
ac27a0ec 670 }
617ba13b 671 free += EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
672 }
673 if (i->value) {
5f80f62a 674 if (free < EXT4_XATTR_LEN(name_len) +
617ba13b 675 EXT4_XATTR_SIZE(i->value_len))
ac27a0ec
DK
676 return -ENOSPC;
677 }
678
679 if (i->value && s->not_found) {
680 /* Insert the new name. */
617ba13b 681 size_t size = EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
682 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
683 memmove((void *)s->here + size, s->here, rest);
684 memset(s->here, 0, size);
685 s->here->e_name_index = i->name_index;
686 s->here->e_name_len = name_len;
687 memcpy(s->here->e_name, i->name, name_len);
688 } else {
1cba4237 689 if (s->here->e_value_size) {
ac27a0ec
DK
690 void *first_val = s->base + min_offs;
691 size_t offs = le16_to_cpu(s->here->e_value_offs);
692 void *val = s->base + offs;
617ba13b 693 size_t size = EXT4_XATTR_SIZE(
ac27a0ec
DK
694 le32_to_cpu(s->here->e_value_size));
695
617ba13b 696 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
ac27a0ec
DK
697 /* The old and the new value have the same
698 size. Just replace. */
699 s->here->e_value_size =
700 cpu_to_le32(i->value_len);
bd9926e8
TT
701 if (i->value == EXT4_ZERO_XATTR_VALUE) {
702 memset(val, 0, size);
703 } else {
704 /* Clear pad bytes first. */
705 memset(val + size - EXT4_XATTR_PAD, 0,
706 EXT4_XATTR_PAD);
707 memcpy(val, i->value, i->value_len);
708 }
ac27a0ec
DK
709 return 0;
710 }
711
712 /* Remove the old value. */
713 memmove(first_val + size, first_val, val - first_val);
714 memset(first_val, 0, size);
715 s->here->e_value_size = 0;
716 s->here->e_value_offs = 0;
717 min_offs += size;
718
719 /* Adjust all value offsets. */
720 last = s->first;
721 while (!IS_LAST_ENTRY(last)) {
722 size_t o = le16_to_cpu(last->e_value_offs);
1cba4237 723 if (last->e_value_size && o < offs)
ac27a0ec
DK
724 last->e_value_offs =
725 cpu_to_le16(o + size);
617ba13b 726 last = EXT4_XATTR_NEXT(last);
ac27a0ec
DK
727 }
728 }
729 if (!i->value) {
730 /* Remove the old name. */
617ba13b 731 size_t size = EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
732 last = ENTRY((void *)last - size);
733 memmove(s->here, (void *)s->here + size,
734 (void *)last - (void *)s->here + sizeof(__u32));
735 memset(last, 0, size);
736 }
737 }
738
739 if (i->value) {
740 /* Insert the new value. */
741 s->here->e_value_size = cpu_to_le32(i->value_len);
742 if (i->value_len) {
617ba13b 743 size_t size = EXT4_XATTR_SIZE(i->value_len);
ac27a0ec
DK
744 void *val = s->base + min_offs - size;
745 s->here->e_value_offs = cpu_to_le16(min_offs - size);
bd9926e8
TT
746 if (i->value == EXT4_ZERO_XATTR_VALUE) {
747 memset(val, 0, size);
748 } else {
749 /* Clear the pad bytes first. */
750 memset(val + size - EXT4_XATTR_PAD, 0,
751 EXT4_XATTR_PAD);
752 memcpy(val, i->value, i->value_len);
753 }
ac27a0ec
DK
754 }
755 }
756 return 0;
757}
758
617ba13b
MC
759struct ext4_xattr_block_find {
760 struct ext4_xattr_search s;
ac27a0ec
DK
761 struct buffer_head *bh;
762};
763
764static int
617ba13b
MC
765ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
766 struct ext4_xattr_block_find *bs)
ac27a0ec
DK
767{
768 struct super_block *sb = inode->i_sb;
769 int error;
770
771 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
772 i->name_index, i->name, i->value, (long)i->value_len);
773
617ba13b 774 if (EXT4_I(inode)->i_file_acl) {
ac27a0ec 775 /* The inode already has an extended attribute block. */
617ba13b 776 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
777 error = -EIO;
778 if (!bs->bh)
779 goto cleanup;
780 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
781 atomic_read(&(bs->bh->b_count)),
782 le32_to_cpu(BHDR(bs->bh)->h_refcount));
cc8e94fd 783 if (ext4_xattr_check_block(inode, bs->bh)) {
24676da4
TT
784 EXT4_ERROR_INODE(inode, "bad block %llu",
785 EXT4_I(inode)->i_file_acl);
6a797d27 786 error = -EFSCORRUPTED;
ac27a0ec
DK
787 goto cleanup;
788 }
789 /* Find the named attribute. */
790 bs->s.base = BHDR(bs->bh);
791 bs->s.first = BFIRST(bs->bh);
792 bs->s.end = bs->bh->b_data + bs->bh->b_size;
793 bs->s.here = bs->s.first;
617ba13b 794 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
ac27a0ec
DK
795 i->name, bs->bh->b_size, 1);
796 if (error && error != -ENODATA)
797 goto cleanup;
798 bs->s.not_found = error;
799 }
800 error = 0;
801
802cleanup:
803 return error;
804}
805
806static int
617ba13b
MC
807ext4_xattr_block_set(handle_t *handle, struct inode *inode,
808 struct ext4_xattr_info *i,
809 struct ext4_xattr_block_find *bs)
ac27a0ec
DK
810{
811 struct super_block *sb = inode->i_sb;
812 struct buffer_head *new_bh = NULL;
617ba13b 813 struct ext4_xattr_search *s = &bs->s;
7a2508e1 814 struct mb_cache_entry *ce = NULL;
8a2bfdcb 815 int error = 0;
7a2508e1 816 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
ac27a0ec 817
617ba13b 818#define header(x) ((struct ext4_xattr_header *)(x))
ac27a0ec
DK
819
820 if (i->value && i->value_len > sb->s_blocksize)
821 return -ENOSPC;
822 if (s->base) {
5d601255 823 BUFFER_TRACE(bs->bh, "get_write_access");
8a2bfdcb
MC
824 error = ext4_journal_get_write_access(handle, bs->bh);
825 if (error)
826 goto cleanup;
827 lock_buffer(bs->bh);
828
ac27a0ec 829 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
82939d79
JK
830 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
831
832 /*
833 * This must happen under buffer lock for
834 * ext4_xattr_block_set() to reliably detect modified
835 * block
836 */
7a2508e1
JK
837 mb_cache_entry_delete_block(ext4_mb_cache, hash,
838 bs->bh->b_blocknr);
ac27a0ec 839 ea_bdebug(bs->bh, "modifying in-place");
617ba13b 840 error = ext4_xattr_set_entry(i, s);
ac27a0ec
DK
841 if (!error) {
842 if (!IS_LAST_ENTRY(s->first))
617ba13b 843 ext4_xattr_rehash(header(s->base),
ac27a0ec 844 s->here);
9c191f70
M
845 ext4_xattr_cache_insert(ext4_mb_cache,
846 bs->bh);
ac27a0ec
DK
847 }
848 unlock_buffer(bs->bh);
6a797d27 849 if (error == -EFSCORRUPTED)
ac27a0ec
DK
850 goto bad_block;
851 if (!error)
cc8e94fd
DW
852 error = ext4_handle_dirty_xattr_block(handle,
853 inode,
854 bs->bh);
ac27a0ec
DK
855 if (error)
856 goto cleanup;
857 goto inserted;
858 } else {
859 int offset = (char *)s->here - bs->bh->b_data;
860
8a2bfdcb 861 unlock_buffer(bs->bh);
ac27a0ec 862 ea_bdebug(bs->bh, "cloning");
216553c4 863 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
ac27a0ec
DK
864 error = -ENOMEM;
865 if (s->base == NULL)
866 goto cleanup;
867 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
868 s->first = ENTRY(header(s->base)+1);
869 header(s->base)->h_refcount = cpu_to_le32(1);
870 s->here = ENTRY(s->base + offset);
871 s->end = s->base + bs->bh->b_size;
872 }
873 } else {
874 /* Allocate a buffer where we construct the new block. */
216553c4 875 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
ac27a0ec
DK
876 /* assert(header == s->base) */
877 error = -ENOMEM;
878 if (s->base == NULL)
879 goto cleanup;
617ba13b 880 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
ac27a0ec
DK
881 header(s->base)->h_blocks = cpu_to_le32(1);
882 header(s->base)->h_refcount = cpu_to_le32(1);
883 s->first = ENTRY(header(s->base)+1);
884 s->here = ENTRY(header(s->base)+1);
885 s->end = s->base + sb->s_blocksize;
886 }
887
617ba13b 888 error = ext4_xattr_set_entry(i, s);
6a797d27 889 if (error == -EFSCORRUPTED)
ac27a0ec
DK
890 goto bad_block;
891 if (error)
892 goto cleanup;
893 if (!IS_LAST_ENTRY(s->first))
617ba13b 894 ext4_xattr_rehash(header(s->base), s->here);
ac27a0ec
DK
895
896inserted:
897 if (!IS_LAST_ENTRY(s->first)) {
617ba13b 898 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
ac27a0ec
DK
899 if (new_bh) {
900 /* We found an identical block in the cache. */
901 if (new_bh == bs->bh)
902 ea_bdebug(new_bh, "keeping");
903 else {
6048c64b
AG
904 u32 ref;
905
ac27a0ec
DK
906 /* The old block is released after updating
907 the inode. */
1231b3a1
LC
908 error = dquot_alloc_block(inode,
909 EXT4_C2B(EXT4_SB(sb), 1));
5dd4056d 910 if (error)
ac27a0ec 911 goto cleanup;
5d601255 912 BUFFER_TRACE(new_bh, "get_write_access");
617ba13b 913 error = ext4_journal_get_write_access(handle,
ac27a0ec
DK
914 new_bh);
915 if (error)
916 goto cleanup_dquot;
917 lock_buffer(new_bh);
82939d79
JK
918 /*
919 * We have to be careful about races with
6048c64b
AG
920 * freeing, rehashing or adding references to
921 * xattr block. Once we hold buffer lock xattr
922 * block's state is stable so we can check
923 * whether the block got freed / rehashed or
924 * not. Since we unhash mbcache entry under
925 * buffer lock when freeing / rehashing xattr
926 * block, checking whether entry is still
927 * hashed is reliable. Same rules hold for
928 * e_reusable handling.
82939d79 929 */
6048c64b
AG
930 if (hlist_bl_unhashed(&ce->e_hash_list) ||
931 !ce->e_reusable) {
82939d79
JK
932 /*
933 * Undo everything and check mbcache
934 * again.
935 */
936 unlock_buffer(new_bh);
937 dquot_free_block(inode,
938 EXT4_C2B(EXT4_SB(sb),
939 1));
940 brelse(new_bh);
7a2508e1 941 mb_cache_entry_put(ext4_mb_cache, ce);
82939d79
JK
942 ce = NULL;
943 new_bh = NULL;
944 goto inserted;
945 }
6048c64b
AG
946 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
947 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
948 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
949 ce->e_reusable = 0;
ac27a0ec 950 ea_bdebug(new_bh, "reusing; refcount now=%d",
6048c64b 951 ref);
ac27a0ec 952 unlock_buffer(new_bh);
cc8e94fd
DW
953 error = ext4_handle_dirty_xattr_block(handle,
954 inode,
955 new_bh);
ac27a0ec
DK
956 if (error)
957 goto cleanup_dquot;
958 }
7a2508e1
JK
959 mb_cache_entry_touch(ext4_mb_cache, ce);
960 mb_cache_entry_put(ext4_mb_cache, ce);
ac27a0ec
DK
961 ce = NULL;
962 } else if (bs->bh && s->base == bs->bh->b_data) {
963 /* We were modifying this block in-place. */
964 ea_bdebug(bs->bh, "keeping this block");
965 new_bh = bs->bh;
966 get_bh(new_bh);
967 } else {
968 /* We need to allocate a new block */
fb0a387d
ES
969 ext4_fsblk_t goal, block;
970
971 goal = ext4_group_first_block_no(sb,
d00a6d7b 972 EXT4_I(inode)->i_block_group);
fb0a387d
ES
973
974 /* non-extent files can't have physical blocks past 2^32 */
12e9b892 975 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
fb0a387d
ES
976 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
977
55f020db
AH
978 block = ext4_new_meta_blocks(handle, inode, goal, 0,
979 NULL, &error);
ac27a0ec
DK
980 if (error)
981 goto cleanup;
fb0a387d 982
12e9b892 983 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
fb0a387d
ES
984 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
985
ace36ad4
JP
986 ea_idebug(inode, "creating block %llu",
987 (unsigned long long)block);
ac27a0ec
DK
988
989 new_bh = sb_getblk(sb, block);
aebf0243 990 if (unlikely(!new_bh)) {
860d21e2 991 error = -ENOMEM;
ac27a0ec 992getblk_failed:
7dc57615 993 ext4_free_blocks(handle, inode, NULL, block, 1,
e6362609 994 EXT4_FREE_BLOCKS_METADATA);
ac27a0ec
DK
995 goto cleanup;
996 }
997 lock_buffer(new_bh);
617ba13b 998 error = ext4_journal_get_create_access(handle, new_bh);
ac27a0ec
DK
999 if (error) {
1000 unlock_buffer(new_bh);
860d21e2 1001 error = -EIO;
ac27a0ec
DK
1002 goto getblk_failed;
1003 }
1004 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1005 set_buffer_uptodate(new_bh);
1006 unlock_buffer(new_bh);
9c191f70 1007 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
cc8e94fd
DW
1008 error = ext4_handle_dirty_xattr_block(handle,
1009 inode, new_bh);
ac27a0ec
DK
1010 if (error)
1011 goto cleanup;
1012 }
1013 }
1014
1015 /* Update the inode. */
617ba13b 1016 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
ac27a0ec
DK
1017
1018 /* Drop the previous xattr block. */
1019 if (bs->bh && bs->bh != new_bh)
617ba13b 1020 ext4_xattr_release_block(handle, inode, bs->bh);
ac27a0ec
DK
1021 error = 0;
1022
1023cleanup:
1024 if (ce)
7a2508e1 1025 mb_cache_entry_put(ext4_mb_cache, ce);
ac27a0ec
DK
1026 brelse(new_bh);
1027 if (!(bs->bh && s->base == bs->bh->b_data))
1028 kfree(s->base);
1029
1030 return error;
1031
1032cleanup_dquot:
1231b3a1 1033 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
ac27a0ec
DK
1034 goto cleanup;
1035
1036bad_block:
24676da4
TT
1037 EXT4_ERROR_INODE(inode, "bad block %llu",
1038 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
1039 goto cleanup;
1040
1041#undef header
1042}
1043
879b3825
TM
1044int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1045 struct ext4_xattr_ibody_find *is)
ac27a0ec 1046{
617ba13b
MC
1047 struct ext4_xattr_ibody_header *header;
1048 struct ext4_inode *raw_inode;
ac27a0ec
DK
1049 int error;
1050
617ba13b 1051 if (EXT4_I(inode)->i_extra_isize == 0)
ac27a0ec 1052 return 0;
617ba13b 1053 raw_inode = ext4_raw_inode(&is->iloc);
ac27a0ec
DK
1054 header = IHDR(inode, raw_inode);
1055 is->s.base = is->s.first = IFIRST(header);
1056 is->s.here = is->s.first;
617ba13b 1057 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
19f5fb7a 1058 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
9e92f48c 1059 error = xattr_check_inode(inode, header, is->s.end);
ac27a0ec
DK
1060 if (error)
1061 return error;
1062 /* Find the named attribute. */
617ba13b 1063 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
ac27a0ec
DK
1064 i->name, is->s.end -
1065 (void *)is->s.base, 0);
1066 if (error && error != -ENODATA)
1067 return error;
1068 is->s.not_found = error;
1069 }
1070 return 0;
1071}
1072
0d812f77
TM
1073int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1074 struct ext4_xattr_info *i,
1075 struct ext4_xattr_ibody_find *is)
1076{
1077 struct ext4_xattr_ibody_header *header;
1078 struct ext4_xattr_search *s = &is->s;
1079 int error;
1080
1081 if (EXT4_I(inode)->i_extra_isize == 0)
1082 return -ENOSPC;
1083 error = ext4_xattr_set_entry(i, s);
1084 if (error) {
1085 if (error == -ENOSPC &&
1086 ext4_has_inline_data(inode)) {
1087 error = ext4_try_to_evict_inline_data(handle, inode,
1088 EXT4_XATTR_LEN(strlen(i->name) +
1089 EXT4_XATTR_SIZE(i->value_len)));
1090 if (error)
1091 return error;
1092 error = ext4_xattr_ibody_find(inode, i, is);
1093 if (error)
1094 return error;
1095 error = ext4_xattr_set_entry(i, s);
1096 }
1097 if (error)
1098 return error;
1099 }
1100 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1101 if (!IS_LAST_ENTRY(s->first)) {
1102 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1103 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1104 } else {
1105 header->h_magic = cpu_to_le32(0);
1106 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1107 }
1108 return 0;
1109}
1110
d5c8dab6 1111static int ext4_xattr_ibody_set(struct inode *inode,
0d812f77
TM
1112 struct ext4_xattr_info *i,
1113 struct ext4_xattr_ibody_find *is)
ac27a0ec 1114{
617ba13b
MC
1115 struct ext4_xattr_ibody_header *header;
1116 struct ext4_xattr_search *s = &is->s;
ac27a0ec
DK
1117 int error;
1118
617ba13b 1119 if (EXT4_I(inode)->i_extra_isize == 0)
ac27a0ec 1120 return -ENOSPC;
617ba13b 1121 error = ext4_xattr_set_entry(i, s);
ac27a0ec
DK
1122 if (error)
1123 return error;
617ba13b 1124 header = IHDR(inode, ext4_raw_inode(&is->iloc));
ac27a0ec 1125 if (!IS_LAST_ENTRY(s->first)) {
617ba13b 1126 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
19f5fb7a 1127 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
ac27a0ec
DK
1128 } else {
1129 header->h_magic = cpu_to_le32(0);
19f5fb7a 1130 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
ac27a0ec
DK
1131 }
1132 return 0;
1133}
1134
3fd16462
JK
1135static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1136 struct ext4_xattr_info *i)
1137{
1138 void *value;
1139
1140 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1141 return 0;
1142 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1143 return !memcmp(value, i->value, i->value_len);
1144}
1145
ac27a0ec 1146/*
617ba13b 1147 * ext4_xattr_set_handle()
ac27a0ec 1148 *
6e9510b0 1149 * Create, replace or remove an extended attribute for this inode. Value
ac27a0ec
DK
1150 * is NULL to remove an existing extended attribute, and non-NULL to
1151 * either replace an existing extended attribute, or create a new extended
1152 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1153 * specify that an extended attribute must exist and must not exist
1154 * previous to the call, respectively.
1155 *
1156 * Returns 0, or a negative error number on failure.
1157 */
1158int
617ba13b 1159ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ac27a0ec
DK
1160 const char *name, const void *value, size_t value_len,
1161 int flags)
1162{
617ba13b 1163 struct ext4_xattr_info i = {
ac27a0ec
DK
1164 .name_index = name_index,
1165 .name = name,
1166 .value = value,
1167 .value_len = value_len,
1168
1169 };
617ba13b 1170 struct ext4_xattr_ibody_find is = {
ac27a0ec
DK
1171 .s = { .not_found = -ENODATA, },
1172 };
617ba13b 1173 struct ext4_xattr_block_find bs = {
ac27a0ec
DK
1174 .s = { .not_found = -ENODATA, },
1175 };
4d20c685 1176 unsigned long no_expand;
ac27a0ec
DK
1177 int error;
1178
1179 if (!name)
1180 return -EINVAL;
1181 if (strlen(name) > 255)
1182 return -ERANGE;
617ba13b 1183 down_write(&EXT4_I(inode)->xattr_sem);
19f5fb7a
TT
1184 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1185 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
4d20c685 1186
66543617 1187 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
86ebfd08
ES
1188 if (error)
1189 goto cleanup;
1190
19f5fb7a 1191 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
617ba13b
MC
1192 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1193 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
19f5fb7a 1194 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
ac27a0ec
DK
1195 }
1196
617ba13b 1197 error = ext4_xattr_ibody_find(inode, &i, &is);
ac27a0ec
DK
1198 if (error)
1199 goto cleanup;
1200 if (is.s.not_found)
617ba13b 1201 error = ext4_xattr_block_find(inode, &i, &bs);
ac27a0ec
DK
1202 if (error)
1203 goto cleanup;
1204 if (is.s.not_found && bs.s.not_found) {
1205 error = -ENODATA;
1206 if (flags & XATTR_REPLACE)
1207 goto cleanup;
1208 error = 0;
1209 if (!value)
1210 goto cleanup;
1211 } else {
1212 error = -EEXIST;
1213 if (flags & XATTR_CREATE)
1214 goto cleanup;
1215 }
ac27a0ec
DK
1216 if (!value) {
1217 if (!is.s.not_found)
d5c8dab6 1218 error = ext4_xattr_ibody_set(inode, &i, &is);
ac27a0ec 1219 else if (!bs.s.not_found)
617ba13b 1220 error = ext4_xattr_block_set(handle, inode, &i, &bs);
ac27a0ec 1221 } else {
3fd16462
JK
1222 error = 0;
1223 /* Xattr value did not change? Save us some work and bail out */
1224 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1225 goto cleanup;
1226 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1227 goto cleanup;
1228
d5c8dab6 1229 error = ext4_xattr_ibody_set(inode, &i, &is);
ac27a0ec
DK
1230 if (!error && !bs.s.not_found) {
1231 i.value = NULL;
617ba13b 1232 error = ext4_xattr_block_set(handle, inode, &i, &bs);
ac27a0ec 1233 } else if (error == -ENOSPC) {
7e01c8e5
TY
1234 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1235 error = ext4_xattr_block_find(inode, &i, &bs);
1236 if (error)
1237 goto cleanup;
1238 }
617ba13b 1239 error = ext4_xattr_block_set(handle, inode, &i, &bs);
ac27a0ec
DK
1240 if (error)
1241 goto cleanup;
1242 if (!is.s.not_found) {
1243 i.value = NULL;
d5c8dab6 1244 error = ext4_xattr_ibody_set(inode, &i, &is);
ac27a0ec
DK
1245 }
1246 }
1247 }
1248 if (!error) {
617ba13b 1249 ext4_xattr_update_super_block(handle, inode->i_sb);
eeca7ea1 1250 inode->i_ctime = current_time(inode);
6dd4ee7c 1251 if (!value)
19f5fb7a 1252 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
617ba13b 1253 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
ac27a0ec 1254 /*
617ba13b 1255 * The bh is consumed by ext4_mark_iloc_dirty, even with
ac27a0ec
DK
1256 * error != 0.
1257 */
1258 is.iloc.bh = NULL;
1259 if (IS_SYNC(inode))
0390131b 1260 ext4_handle_sync(handle);
ac27a0ec
DK
1261 }
1262
1263cleanup:
1264 brelse(is.iloc.bh);
1265 brelse(bs.bh);
4d20c685 1266 if (no_expand == 0)
19f5fb7a 1267 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
617ba13b 1268 up_write(&EXT4_I(inode)->xattr_sem);
ac27a0ec
DK
1269 return error;
1270}
1271
1272/*
617ba13b 1273 * ext4_xattr_set()
ac27a0ec 1274 *
617ba13b 1275 * Like ext4_xattr_set_handle, but start from an inode. This extended
ac27a0ec
DK
1276 * attribute modification is a filesystem transaction by itself.
1277 *
1278 * Returns 0, or a negative error number on failure.
1279 */
1280int
617ba13b 1281ext4_xattr_set(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
1282 const void *value, size_t value_len, int flags)
1283{
1284 handle_t *handle;
1285 int error, retries = 0;
95eaefbd 1286 int credits = ext4_jbd2_credits_xattr(inode);
ac27a0ec
DK
1287
1288retry:
9924a92a 1289 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
ac27a0ec
DK
1290 if (IS_ERR(handle)) {
1291 error = PTR_ERR(handle);
1292 } else {
1293 int error2;
1294
617ba13b 1295 error = ext4_xattr_set_handle(handle, inode, name_index, name,
ac27a0ec 1296 value, value_len, flags);
617ba13b 1297 error2 = ext4_journal_stop(handle);
ac27a0ec 1298 if (error == -ENOSPC &&
617ba13b 1299 ext4_should_retry_alloc(inode->i_sb, &retries))
ac27a0ec
DK
1300 goto retry;
1301 if (error == 0)
1302 error = error2;
1303 }
1304
1305 return error;
1306}
1307
6dd4ee7c
KS
1308/*
1309 * Shift the EA entries in the inode to create space for the increased
1310 * i_extra_isize.
1311 */
1312static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1313 int value_offs_shift, void *to,
94405713 1314 void *from, size_t n)
6dd4ee7c
KS
1315{
1316 struct ext4_xattr_entry *last = entry;
1317 int new_offs;
1318
94405713
JK
1319 /* We always shift xattr headers further thus offsets get lower */
1320 BUG_ON(value_offs_shift > 0);
1321
6dd4ee7c
KS
1322 /* Adjust the value offsets of the entries */
1323 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1cba4237 1324 if (last->e_value_size) {
6dd4ee7c
KS
1325 new_offs = le16_to_cpu(last->e_value_offs) +
1326 value_offs_shift;
6dd4ee7c
KS
1327 last->e_value_offs = cpu_to_le16(new_offs);
1328 }
1329 }
1330 /* Shift the entries by n bytes */
1331 memmove(to, from, n);
1332}
1333
3f2571c1
JK
1334/*
1335 * Move xattr pointed to by 'entry' from inode into external xattr block
1336 */
1337static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1338 struct ext4_inode *raw_inode,
1339 struct ext4_xattr_entry *entry)
1340{
1341 struct ext4_xattr_ibody_find *is = NULL;
1342 struct ext4_xattr_block_find *bs = NULL;
1343 char *buffer = NULL, *b_entry_name = NULL;
1344 size_t value_offs, value_size;
1345 struct ext4_xattr_info i = {
1346 .value = NULL,
1347 .value_len = 0,
1348 .name_index = entry->e_name_index,
1349 };
1350 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1351 int error;
1352
1353 value_offs = le16_to_cpu(entry->e_value_offs);
1354 value_size = le32_to_cpu(entry->e_value_size);
1355
1356 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1357 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1358 buffer = kmalloc(value_size, GFP_NOFS);
1359 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1360 if (!is || !bs || !buffer || !b_entry_name) {
1361 error = -ENOMEM;
1362 goto out;
1363 }
1364
1365 is->s.not_found = -ENODATA;
1366 bs->s.not_found = -ENODATA;
1367 is->iloc.bh = NULL;
1368 bs->bh = NULL;
1369
1370 /* Save the entry name and the entry value */
1371 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1372 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1373 b_entry_name[entry->e_name_len] = '\0';
1374 i.name = b_entry_name;
1375
1376 error = ext4_get_inode_loc(inode, &is->iloc);
1377 if (error)
1378 goto out;
1379
1380 error = ext4_xattr_ibody_find(inode, &i, is);
1381 if (error)
1382 goto out;
1383
1384 /* Remove the chosen entry from the inode */
d5c8dab6 1385 error = ext4_xattr_ibody_set(inode, &i, is);
3f2571c1
JK
1386 if (error)
1387 goto out;
1388
1389 i.name = b_entry_name;
1390 i.value = buffer;
1391 i.value_len = value_size;
1392 error = ext4_xattr_block_find(inode, &i, bs);
1393 if (error)
1394 goto out;
1395
1396 /* Add entry which was removed from the inode into the block */
1397 error = ext4_xattr_block_set(handle, inode, &i, bs);
1398 if (error)
1399 goto out;
1400 error = 0;
1401out:
1402 kfree(b_entry_name);
1403 kfree(buffer);
1404 if (is)
1405 brelse(is->iloc.bh);
1406 kfree(is);
1407 kfree(bs);
1408
1409 return error;
1410}
1411
dfa2064b
JK
1412static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1413 struct ext4_inode *raw_inode,
1414 int isize_diff, size_t ifree,
1415 size_t bfree, int *total_ino)
1416{
1417 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1418 struct ext4_xattr_entry *small_entry;
1419 struct ext4_xattr_entry *entry;
1420 struct ext4_xattr_entry *last;
1421 unsigned int entry_size; /* EA entry size */
1422 unsigned int total_size; /* EA entry size + value size */
1423 unsigned int min_total_size;
1424 int error;
1425
1426 while (isize_diff > ifree) {
1427 entry = NULL;
1428 small_entry = NULL;
1429 min_total_size = ~0U;
1430 last = IFIRST(header);
1431 /* Find the entry best suited to be pushed into EA block */
1432 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1433 total_size =
1434 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1435 EXT4_XATTR_LEN(last->e_name_len);
1436 if (total_size <= bfree &&
1437 total_size < min_total_size) {
1438 if (total_size + ifree < isize_diff) {
1439 small_entry = last;
1440 } else {
1441 entry = last;
1442 min_total_size = total_size;
1443 }
1444 }
1445 }
1446
1447 if (entry == NULL) {
1448 if (small_entry == NULL)
1449 return -ENOSPC;
1450 entry = small_entry;
1451 }
1452
1453 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1454 total_size = entry_size +
1455 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1456 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1457 entry);
1458 if (error)
1459 return error;
1460
1461 *total_ino -= entry_size;
1462 ifree += total_size;
1463 bfree -= total_size;
1464 }
1465
1466 return 0;
1467}
1468
6dd4ee7c
KS
1469/*
1470 * Expand an inode by new_extra_isize bytes when EAs are present.
1471 * Returns 0 on success or negative error number on failure.
1472 */
1473int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1474 struct ext4_inode *raw_inode, handle_t *handle)
1475{
1476 struct ext4_xattr_ibody_header *header;
6dd4ee7c 1477 struct buffer_head *bh = NULL;
e3014d14
JK
1478 size_t min_offs;
1479 size_t ifree, bfree;
7b1b2c1b 1480 int total_ino;
6e0cd088 1481 void *base, *end;
d0141191 1482 int error = 0, tried_min_extra_isize = 0;
ac39849d 1483 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
d0141191 1484 int isize_diff; /* How much do we need to grow i_extra_isize */
6dd4ee7c
KS
1485
1486 down_write(&EXT4_I(inode)->xattr_sem);
2e81a4ee
JK
1487 /*
1488 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1489 */
1490 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
6dd4ee7c 1491retry:
d0141191 1492 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2e81a4ee
JK
1493 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1494 goto out;
6dd4ee7c
KS
1495
1496 header = IHDR(inode, raw_inode);
6dd4ee7c
KS
1497
1498 /*
1499 * Check if enough free space is available in the inode to shift the
1500 * entries ahead by new_extra_isize.
1501 */
1502
6e0cd088 1503 base = IFIRST(header);
6dd4ee7c
KS
1504 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1505 min_offs = end - base;
6dd4ee7c
KS
1506 total_ino = sizeof(struct ext4_xattr_ibody_header);
1507
9e92f48c
TT
1508 error = xattr_check_inode(inode, header, end);
1509 if (error)
1510 goto cleanup;
1511
6e0cd088 1512 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
e3014d14
JK
1513 if (ifree >= isize_diff)
1514 goto shift;
6dd4ee7c
KS
1515
1516 /*
1517 * Enough free space isn't available in the inode, check if
1518 * EA block can hold new_extra_isize bytes.
1519 */
1520 if (EXT4_I(inode)->i_file_acl) {
1521 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1522 error = -EIO;
1523 if (!bh)
1524 goto cleanup;
cc8e94fd 1525 if (ext4_xattr_check_block(inode, bh)) {
24676da4
TT
1526 EXT4_ERROR_INODE(inode, "bad block %llu",
1527 EXT4_I(inode)->i_file_acl);
6a797d27 1528 error = -EFSCORRUPTED;
6dd4ee7c
KS
1529 goto cleanup;
1530 }
1531 base = BHDR(bh);
6dd4ee7c
KS
1532 end = bh->b_data + bh->b_size;
1533 min_offs = end - base;
6e0cd088
JK
1534 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1535 NULL);
e3014d14 1536 if (bfree + ifree < isize_diff) {
6dd4ee7c
KS
1537 if (!tried_min_extra_isize && s_min_extra_isize) {
1538 tried_min_extra_isize++;
1539 new_extra_isize = s_min_extra_isize;
1540 brelse(bh);
1541 goto retry;
1542 }
dfa2064b 1543 error = -ENOSPC;
6dd4ee7c
KS
1544 goto cleanup;
1545 }
1546 } else {
e3014d14 1547 bfree = inode->i_sb->s_blocksize;
6dd4ee7c
KS
1548 }
1549
dfa2064b
JK
1550 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1551 isize_diff, ifree, bfree,
1552 &total_ino);
1553 if (error) {
1554 if (error == -ENOSPC && !tried_min_extra_isize &&
1555 s_min_extra_isize) {
1556 tried_min_extra_isize++;
1557 new_extra_isize = s_min_extra_isize;
1558 brelse(bh);
1559 goto retry;
6dd4ee7c 1560 }
dfa2064b 1561 goto cleanup;
6dd4ee7c 1562 }
e3014d14
JK
1563shift:
1564 /* Adjust the offsets and shift the remaining entries ahead */
6e0cd088 1565 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
e3014d14
JK
1566 - new_extra_isize, (void *)raw_inode +
1567 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
94405713 1568 (void *)header, total_ino);
e3014d14 1569 EXT4_I(inode)->i_extra_isize = new_extra_isize;
6dd4ee7c 1570 brelse(bh);
2e81a4ee
JK
1571out:
1572 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
6dd4ee7c
KS
1573 up_write(&EXT4_I(inode)->xattr_sem);
1574 return 0;
1575
1576cleanup:
6dd4ee7c 1577 brelse(bh);
2e81a4ee
JK
1578 /*
1579 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1580 * size expansion failed.
1581 */
6dd4ee7c
KS
1582 up_write(&EXT4_I(inode)->xattr_sem);
1583 return error;
1584}
1585
1586
1587
ac27a0ec 1588/*
617ba13b 1589 * ext4_xattr_delete_inode()
ac27a0ec
DK
1590 *
1591 * Free extended attribute resources associated with this inode. This
1592 * is called immediately before an inode is freed. We have exclusive
1593 * access to the inode.
1594 */
1595void
617ba13b 1596ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
ac27a0ec
DK
1597{
1598 struct buffer_head *bh = NULL;
1599
617ba13b 1600 if (!EXT4_I(inode)->i_file_acl)
ac27a0ec 1601 goto cleanup;
617ba13b 1602 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec 1603 if (!bh) {
24676da4
TT
1604 EXT4_ERROR_INODE(inode, "block %llu read error",
1605 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
1606 goto cleanup;
1607 }
617ba13b 1608 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
ac27a0ec 1609 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
24676da4
TT
1610 EXT4_ERROR_INODE(inode, "bad block %llu",
1611 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
1612 goto cleanup;
1613 }
617ba13b
MC
1614 ext4_xattr_release_block(handle, inode, bh);
1615 EXT4_I(inode)->i_file_acl = 0;
ac27a0ec
DK
1616
1617cleanup:
1618 brelse(bh);
1619}
1620
ac27a0ec 1621/*
617ba13b 1622 * ext4_xattr_cache_insert()
ac27a0ec
DK
1623 *
1624 * Create a new entry in the extended attribute cache, and insert
1625 * it unless such an entry is already in the cache.
1626 *
1627 * Returns 0, or a negative error number on failure.
1628 */
1629static void
7a2508e1 1630ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
ac27a0ec 1631{
6048c64b
AG
1632 struct ext4_xattr_header *header = BHDR(bh);
1633 __u32 hash = le32_to_cpu(header->h_hash);
1634 int reusable = le32_to_cpu(header->h_refcount) <
1635 EXT4_XATTR_REFCOUNT_MAX;
ac27a0ec
DK
1636 int error;
1637
7a2508e1 1638 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
6048c64b 1639 bh->b_blocknr, reusable);
ac27a0ec 1640 if (error) {
82939d79 1641 if (error == -EBUSY)
ac27a0ec 1642 ea_bdebug(bh, "already in cache");
82939d79 1643 } else
ac27a0ec 1644 ea_bdebug(bh, "inserting [%x]", (int)hash);
ac27a0ec
DK
1645}
1646
1647/*
617ba13b 1648 * ext4_xattr_cmp()
ac27a0ec
DK
1649 *
1650 * Compare two extended attribute blocks for equality.
1651 *
1652 * Returns 0 if the blocks are equal, 1 if they differ, and
1653 * a negative error number on errors.
1654 */
1655static int
617ba13b
MC
1656ext4_xattr_cmp(struct ext4_xattr_header *header1,
1657 struct ext4_xattr_header *header2)
ac27a0ec 1658{
617ba13b 1659 struct ext4_xattr_entry *entry1, *entry2;
ac27a0ec
DK
1660
1661 entry1 = ENTRY(header1+1);
1662 entry2 = ENTRY(header2+1);
1663 while (!IS_LAST_ENTRY(entry1)) {
1664 if (IS_LAST_ENTRY(entry2))
1665 return 1;
1666 if (entry1->e_hash != entry2->e_hash ||
1667 entry1->e_name_index != entry2->e_name_index ||
1668 entry1->e_name_len != entry2->e_name_len ||
1669 entry1->e_value_size != entry2->e_value_size ||
1670 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1671 return 1;
1672 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
6a797d27 1673 return -EFSCORRUPTED;
ac27a0ec
DK
1674 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1675 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1676 le32_to_cpu(entry1->e_value_size)))
1677 return 1;
1678
617ba13b
MC
1679 entry1 = EXT4_XATTR_NEXT(entry1);
1680 entry2 = EXT4_XATTR_NEXT(entry2);
ac27a0ec
DK
1681 }
1682 if (!IS_LAST_ENTRY(entry2))
1683 return 1;
1684 return 0;
1685}
1686
1687/*
617ba13b 1688 * ext4_xattr_cache_find()
ac27a0ec
DK
1689 *
1690 * Find an identical extended attribute block.
1691 *
1692 * Returns a pointer to the block found, or NULL if such a block was
1693 * not found or an error occurred.
1694 */
1695static struct buffer_head *
617ba13b 1696ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
7a2508e1 1697 struct mb_cache_entry **pce)
ac27a0ec
DK
1698{
1699 __u32 hash = le32_to_cpu(header->h_hash);
7a2508e1
JK
1700 struct mb_cache_entry *ce;
1701 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
ac27a0ec
DK
1702
1703 if (!header->h_hash)
1704 return NULL; /* never share */
1705 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
7a2508e1 1706 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
ac27a0ec
DK
1707 while (ce) {
1708 struct buffer_head *bh;
1709
ac27a0ec
DK
1710 bh = sb_bread(inode->i_sb, ce->e_block);
1711 if (!bh) {
24676da4
TT
1712 EXT4_ERROR_INODE(inode, "block %lu read error",
1713 (unsigned long) ce->e_block);
617ba13b 1714 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
ac27a0ec
DK
1715 *pce = ce;
1716 return bh;
1717 }
1718 brelse(bh);
7a2508e1 1719 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
ac27a0ec
DK
1720 }
1721 return NULL;
1722}
1723
1724#define NAME_HASH_SHIFT 5
1725#define VALUE_HASH_SHIFT 16
1726
1727/*
617ba13b 1728 * ext4_xattr_hash_entry()
ac27a0ec
DK
1729 *
1730 * Compute the hash of an extended attribute.
1731 */
617ba13b
MC
1732static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1733 struct ext4_xattr_entry *entry)
ac27a0ec
DK
1734{
1735 __u32 hash = 0;
1736 char *name = entry->e_name;
1737 int n;
1738
2b2d6d01 1739 for (n = 0; n < entry->e_name_len; n++) {
ac27a0ec
DK
1740 hash = (hash << NAME_HASH_SHIFT) ^
1741 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1742 *name++;
1743 }
1744
1cba4237 1745 if (entry->e_value_size != 0) {
ac27a0ec
DK
1746 __le32 *value = (__le32 *)((char *)header +
1747 le16_to_cpu(entry->e_value_offs));
1748 for (n = (le32_to_cpu(entry->e_value_size) +
617ba13b 1749 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
ac27a0ec
DK
1750 hash = (hash << VALUE_HASH_SHIFT) ^
1751 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1752 le32_to_cpu(*value++);
1753 }
1754 }
1755 entry->e_hash = cpu_to_le32(hash);
1756}
1757
1758#undef NAME_HASH_SHIFT
1759#undef VALUE_HASH_SHIFT
1760
1761#define BLOCK_HASH_SHIFT 16
1762
1763/*
617ba13b 1764 * ext4_xattr_rehash()
ac27a0ec
DK
1765 *
1766 * Re-compute the extended attribute hash value after an entry has changed.
1767 */
617ba13b
MC
1768static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1769 struct ext4_xattr_entry *entry)
ac27a0ec 1770{
617ba13b 1771 struct ext4_xattr_entry *here;
ac27a0ec
DK
1772 __u32 hash = 0;
1773
617ba13b 1774 ext4_xattr_hash_entry(header, entry);
ac27a0ec
DK
1775 here = ENTRY(header+1);
1776 while (!IS_LAST_ENTRY(here)) {
1777 if (!here->e_hash) {
1778 /* Block is not shared if an entry's hash value == 0 */
1779 hash = 0;
1780 break;
1781 }
1782 hash = (hash << BLOCK_HASH_SHIFT) ^
1783 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1784 le32_to_cpu(here->e_hash);
617ba13b 1785 here = EXT4_XATTR_NEXT(here);
ac27a0ec
DK
1786 }
1787 header->h_hash = cpu_to_le32(hash);
1788}
1789
1790#undef BLOCK_HASH_SHIFT
1791
9c191f70
M
1792#define HASH_BUCKET_BITS 10
1793
7a2508e1 1794struct mb_cache *
82939d79 1795ext4_xattr_create_cache(void)
ac27a0ec 1796{
7a2508e1 1797 return mb_cache_create(HASH_BUCKET_BITS);
ac27a0ec
DK
1798}
1799
7a2508e1 1800void ext4_xattr_destroy_cache(struct mb_cache *cache)
ac27a0ec 1801{
9c191f70 1802 if (cache)
7a2508e1 1803 mb_cache_destroy(cache);
ac27a0ec 1804}
9c191f70 1805