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