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