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