]> git.ipfire.org Git - people/ms/linux.git/blame - fs/ext4/xattr.c
ext4: attach jinode after creation of xattr inode
[people/ms/linux.git] / fs / ext4 / xattr.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/xattr.c
ac27a0ec
DK
3 *
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
5 *
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
617ba13b 7 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
ac27a0ec
DK
8 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
11 * Red Hat Inc.
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
14 */
15
16/*
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
24 *
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
27 *
28 * +------------------+
29 * | header |
30 * | entry 1 | |
31 * | entry 2 | | growing downwards
32 * | entry 3 | v
33 * | four null bytes |
34 * | . . . |
35 * | value 1 | ^
36 * | value 3 | | growing upwards
37 * | value 2 | |
38 * +------------------+
39 *
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
43 *
44 * Locking strategy
45 * ----------------
617ba13b 46 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
ac27a0ec
DK
47 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
50 * by the buffer lock.
51 */
52
53#include <linux/init.h>
54#include <linux/fs.h>
55#include <linux/slab.h>
7a2508e1 56#include <linux/mbcache.h>
ac27a0ec 57#include <linux/quotaops.h>
3dcf5451
CH
58#include "ext4_jbd2.h"
59#include "ext4.h"
ac27a0ec
DK
60#include "xattr.h"
61#include "acl.h"
62
617ba13b 63#ifdef EXT4_XATTR_DEBUG
d74f3d25
JP
64# define ea_idebug(inode, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67# define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
ac27a0ec 70#else
ace36ad4
JP
71# define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72# define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
ac27a0ec
DK
73#endif
74
7a2508e1 75static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
617ba13b
MC
76static struct buffer_head *ext4_xattr_cache_find(struct inode *,
77 struct ext4_xattr_header *,
7a2508e1 78 struct mb_cache_entry **);
617ba13b
MC
79static void ext4_xattr_rehash(struct ext4_xattr_header *,
80 struct ext4_xattr_entry *);
ac27a0ec 81
d6006186 82static const struct xattr_handler * const ext4_xattr_handler_map[] = {
617ba13b 83 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
03010a33 84#ifdef CONFIG_EXT4_FS_POSIX_ACL
64e178a7
CH
85 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
86 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
ac27a0ec 87#endif
617ba13b 88 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
03010a33 89#ifdef CONFIG_EXT4_FS_SECURITY
617ba13b 90 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
ac27a0ec
DK
91#endif
92};
93
11e27528 94const struct xattr_handler *ext4_xattr_handlers[] = {
617ba13b
MC
95 &ext4_xattr_user_handler,
96 &ext4_xattr_trusted_handler,
03010a33 97#ifdef CONFIG_EXT4_FS_POSIX_ACL
64e178a7
CH
98 &posix_acl_access_xattr_handler,
99 &posix_acl_default_xattr_handler,
ac27a0ec 100#endif
03010a33 101#ifdef CONFIG_EXT4_FS_SECURITY
617ba13b 102 &ext4_xattr_security_handler,
ac27a0ec
DK
103#endif
104 NULL
105};
106
9c191f70
M
107#define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
108 inode->i_sb->s_fs_info)->s_mb_cache)
109
33d201e0
TE
110#ifdef CONFIG_LOCKDEP
111void ext4_xattr_inode_set_class(struct inode *ea_inode)
112{
113 lockdep_set_subclass(&ea_inode->i_rwsem, 1);
114}
115#endif
116
cc8e94fd
DW
117static __le32 ext4_xattr_block_csum(struct inode *inode,
118 sector_t block_nr,
119 struct ext4_xattr_header *hdr)
120{
121 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
d6a77105 122 __u32 csum;
d6a77105 123 __le64 dsk_block_nr = cpu_to_le64(block_nr);
b47820ed
DJ
124 __u32 dummy_csum = 0;
125 int offset = offsetof(struct ext4_xattr_header, h_checksum);
cc8e94fd 126
d6a77105
TT
127 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
128 sizeof(dsk_block_nr));
b47820ed
DJ
129 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
130 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
131 offset += sizeof(dummy_csum);
132 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
133 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
41eb70dd 134
cc8e94fd
DW
135 return cpu_to_le32(csum);
136}
137
138static int ext4_xattr_block_csum_verify(struct inode *inode,
dac7a4b4 139 struct buffer_head *bh)
cc8e94fd 140{
dac7a4b4
TT
141 struct ext4_xattr_header *hdr = BHDR(bh);
142 int ret = 1;
cc8e94fd 143
dac7a4b4
TT
144 if (ext4_has_metadata_csum(inode->i_sb)) {
145 lock_buffer(bh);
146 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
147 bh->b_blocknr, hdr));
148 unlock_buffer(bh);
149 }
150 return ret;
cc8e94fd
DW
151}
152
dac7a4b4
TT
153static void ext4_xattr_block_csum_set(struct inode *inode,
154 struct buffer_head *bh)
cc8e94fd 155{
dac7a4b4
TT
156 if (ext4_has_metadata_csum(inode->i_sb))
157 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
158 bh->b_blocknr, BHDR(bh));
cc8e94fd
DW
159}
160
11e27528 161static inline const struct xattr_handler *
617ba13b 162ext4_xattr_handler(int name_index)
ac27a0ec 163{
11e27528 164 const struct xattr_handler *handler = NULL;
ac27a0ec 165
617ba13b
MC
166 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
167 handler = ext4_xattr_handler_map[name_index];
ac27a0ec
DK
168 return handler;
169}
170
ac27a0ec 171static int
2c4f9923
EB
172ext4_xattr_check_entries(struct ext4_xattr_entry *entry, void *end,
173 void *value_start)
ac27a0ec 174{
a0626e75
DW
175 struct ext4_xattr_entry *e = entry;
176
d7614cc1 177 /* Find the end of the names list */
a0626e75
DW
178 while (!IS_LAST_ENTRY(e)) {
179 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
ac27a0ec 180 if ((void *)next >= end)
6a797d27 181 return -EFSCORRUPTED;
a0626e75 182 e = next;
ac27a0ec 183 }
a0626e75 184
d7614cc1 185 /* Check the values */
a0626e75 186 while (!IS_LAST_ENTRY(entry)) {
e50e5129
AD
187 if (entry->e_value_size != 0 &&
188 entry->e_value_inum == 0) {
d7614cc1
EB
189 u16 offs = le16_to_cpu(entry->e_value_offs);
190 u32 size = le32_to_cpu(entry->e_value_size);
191 void *value;
192
193 /*
194 * The value cannot overlap the names, and the value
195 * with padding cannot extend beyond 'end'. Check both
196 * the padded and unpadded sizes, since the size may
197 * overflow to 0 when adding padding.
198 */
199 if (offs > end - value_start)
200 return -EFSCORRUPTED;
201 value = value_start + offs;
202 if (value < (void *)e + sizeof(u32) ||
203 size > end - value ||
204 EXT4_XATTR_SIZE(size) > end - value)
205 return -EFSCORRUPTED;
206 }
a0626e75
DW
207 entry = EXT4_XATTR_NEXT(entry);
208 }
209
ac27a0ec
DK
210 return 0;
211}
212
213static inline int
cc8e94fd 214ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
ac27a0ec 215{
cc8e94fd
DW
216 int error;
217
218 if (buffer_verified(bh))
219 return 0;
220
617ba13b 221 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
ac27a0ec 222 BHDR(bh)->h_blocks != cpu_to_le32(1))
6a797d27 223 return -EFSCORRUPTED;
dac7a4b4 224 if (!ext4_xattr_block_csum_verify(inode, bh))
6a797d27 225 return -EFSBADCRC;
2c4f9923
EB
226 error = ext4_xattr_check_entries(BFIRST(bh), bh->b_data + bh->b_size,
227 bh->b_data);
cc8e94fd
DW
228 if (!error)
229 set_buffer_verified(bh);
230 return error;
ac27a0ec
DK
231}
232
9e92f48c
TT
233static int
234__xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
235 void *end, const char *function, unsigned int line)
236{
9e92f48c
TT
237 int error = -EFSCORRUPTED;
238
290ab230 239 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
19962509 240 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
9e92f48c 241 goto errout;
2c4f9923 242 error = ext4_xattr_check_entries(IFIRST(header), end, IFIRST(header));
9e92f48c
TT
243errout:
244 if (error)
245 __ext4_error_inode(inode, function, line, 0,
246 "corrupted in-inode xattr");
247 return error;
248}
249
250#define xattr_check_inode(inode, header, end) \
251 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
252
ac27a0ec 253static int
617ba13b 254ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
6ba644b9 255 const char *name, int sorted)
ac27a0ec 256{
617ba13b 257 struct ext4_xattr_entry *entry;
ac27a0ec
DK
258 size_t name_len;
259 int cmp = 1;
260
261 if (name == NULL)
262 return -EINVAL;
263 name_len = strlen(name);
264 entry = *pentry;
617ba13b 265 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
ac27a0ec
DK
266 cmp = name_index - entry->e_name_index;
267 if (!cmp)
268 cmp = name_len - entry->e_name_len;
269 if (!cmp)
270 cmp = memcmp(name, entry->e_name, name_len);
271 if (cmp <= 0 && (sorted || cmp == 0))
272 break;
273 }
274 *pentry = entry;
ac27a0ec
DK
275 return cmp ? -ENODATA : 0;
276}
277
e50e5129
AD
278/*
279 * Read the EA value from an inode.
280 */
281static int
282ext4_xattr_inode_read(struct inode *ea_inode, void *buf, size_t *size)
283{
284 unsigned long block = 0;
285 struct buffer_head *bh = NULL;
286 int blocksize;
287 size_t csize, ret_size = 0;
288
289 if (*size == 0)
290 return 0;
291
292 blocksize = ea_inode->i_sb->s_blocksize;
293
294 while (ret_size < *size) {
295 csize = (*size - ret_size) > blocksize ? blocksize :
296 *size - ret_size;
297 bh = ext4_bread(NULL, ea_inode, block, 0);
298 if (IS_ERR(bh)) {
299 *size = ret_size;
300 return PTR_ERR(bh);
301 }
302 memcpy(buf, bh->b_data, csize);
303 brelse(bh);
304
305 buf += csize;
306 block += 1;
307 ret_size += csize;
308 }
309
310 *size = ret_size;
311
312 return 0;
313}
314
315struct inode *ext4_xattr_inode_iget(struct inode *parent, unsigned long ea_ino, int *err)
316{
317 struct inode *ea_inode = NULL;
318
319 ea_inode = ext4_iget(parent->i_sb, ea_ino);
320 if (IS_ERR(ea_inode) || is_bad_inode(ea_inode)) {
321 int rc = IS_ERR(ea_inode) ? PTR_ERR(ea_inode) : 0;
322 ext4_error(parent->i_sb, "error while reading EA inode %lu "
323 "/ %d %d", ea_ino, rc, is_bad_inode(ea_inode));
324 *err = rc != 0 ? rc : -EIO;
325 return NULL;
326 }
327
328 if (EXT4_XATTR_INODE_GET_PARENT(ea_inode) != parent->i_ino ||
329 ea_inode->i_generation != parent->i_generation) {
330 ext4_error(parent->i_sb, "Backpointer from EA inode %lu "
331 "to parent invalid.", ea_ino);
332 *err = -EINVAL;
333 goto error;
334 }
335
336 if (!(EXT4_I(ea_inode)->i_flags & EXT4_EA_INODE_FL)) {
337 ext4_error(parent->i_sb, "EA inode %lu does not have "
338 "EXT4_EA_INODE_FL flag set.\n", ea_ino);
339 *err = -EINVAL;
340 goto error;
341 }
342
343 *err = 0;
344 return ea_inode;
345
346error:
347 iput(ea_inode);
348 return NULL;
349}
350
351/*
352 * Read the value from the EA inode.
353 */
354static int
355ext4_xattr_inode_get(struct inode *inode, unsigned long ea_ino, void *buffer,
356 size_t *size)
357{
358 struct inode *ea_inode = NULL;
359 int err;
360
361 ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
362 if (err)
363 return err;
364
365 err = ext4_xattr_inode_read(ea_inode, buffer, size);
366 iput(ea_inode);
367
368 return err;
369}
370
ac27a0ec 371static int
617ba13b 372ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
373 void *buffer, size_t buffer_size)
374{
375 struct buffer_head *bh = NULL;
617ba13b 376 struct ext4_xattr_entry *entry;
ac27a0ec
DK
377 size_t size;
378 int error;
7a2508e1 379 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
ac27a0ec
DK
380
381 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
382 name_index, name, buffer, (long)buffer_size);
383
384 error = -ENODATA;
617ba13b 385 if (!EXT4_I(inode)->i_file_acl)
ac27a0ec 386 goto cleanup;
ace36ad4
JP
387 ea_idebug(inode, "reading block %llu",
388 (unsigned long long)EXT4_I(inode)->i_file_acl);
617ba13b 389 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
390 if (!bh)
391 goto cleanup;
392 ea_bdebug(bh, "b_count=%d, refcount=%d",
393 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
cc8e94fd 394 if (ext4_xattr_check_block(inode, bh)) {
24676da4
TT
395 EXT4_ERROR_INODE(inode, "bad block %llu",
396 EXT4_I(inode)->i_file_acl);
6a797d27 397 error = -EFSCORRUPTED;
ac27a0ec
DK
398 goto cleanup;
399 }
9c191f70 400 ext4_xattr_cache_insert(ext4_mb_cache, bh);
ac27a0ec 401 entry = BFIRST(bh);
6ba644b9 402 error = ext4_xattr_find_entry(&entry, name_index, name, 1);
ac27a0ec
DK
403 if (error)
404 goto cleanup;
405 size = le32_to_cpu(entry->e_value_size);
406 if (buffer) {
407 error = -ERANGE;
408 if (size > buffer_size)
409 goto cleanup;
e50e5129
AD
410 if (entry->e_value_inum) {
411 error = ext4_xattr_inode_get(inode,
412 le32_to_cpu(entry->e_value_inum),
413 buffer, &size);
414 if (error)
415 goto cleanup;
416 } else {
417 memcpy(buffer, bh->b_data +
418 le16_to_cpu(entry->e_value_offs), size);
419 }
ac27a0ec
DK
420 }
421 error = size;
422
423cleanup:
424 brelse(bh);
425 return error;
426}
427
879b3825 428int
617ba13b 429ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
430 void *buffer, size_t buffer_size)
431{
617ba13b
MC
432 struct ext4_xattr_ibody_header *header;
433 struct ext4_xattr_entry *entry;
434 struct ext4_inode *raw_inode;
435 struct ext4_iloc iloc;
ac27a0ec
DK
436 size_t size;
437 void *end;
438 int error;
439
19f5fb7a 440 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
ac27a0ec 441 return -ENODATA;
617ba13b 442 error = ext4_get_inode_loc(inode, &iloc);
ac27a0ec
DK
443 if (error)
444 return error;
617ba13b 445 raw_inode = ext4_raw_inode(&iloc);
ac27a0ec 446 header = IHDR(inode, raw_inode);
617ba13b 447 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
9e92f48c 448 error = xattr_check_inode(inode, header, end);
ac27a0ec
DK
449 if (error)
450 goto cleanup;
6ba644b9
EB
451 entry = IFIRST(header);
452 error = ext4_xattr_find_entry(&entry, name_index, name, 0);
ac27a0ec
DK
453 if (error)
454 goto cleanup;
455 size = le32_to_cpu(entry->e_value_size);
456 if (buffer) {
457 error = -ERANGE;
458 if (size > buffer_size)
459 goto cleanup;
e50e5129
AD
460 if (entry->e_value_inum) {
461 error = ext4_xattr_inode_get(inode,
462 le32_to_cpu(entry->e_value_inum),
463 buffer, &size);
464 if (error)
465 goto cleanup;
466 } else {
467 memcpy(buffer, (void *)IFIRST(header) +
468 le16_to_cpu(entry->e_value_offs), size);
469 }
ac27a0ec
DK
470 }
471 error = size;
472
473cleanup:
474 brelse(iloc.bh);
475 return error;
476}
477
478/*
617ba13b 479 * ext4_xattr_get()
ac27a0ec
DK
480 *
481 * Copy an extended attribute into the buffer
482 * provided, or compute the buffer size required.
483 * Buffer is NULL to compute the size of the buffer required.
484 *
485 * Returns a negative error number on failure, or the number of bytes
486 * used / required on success.
487 */
488int
617ba13b 489ext4_xattr_get(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
490 void *buffer, size_t buffer_size)
491{
492 int error;
493
0db1ff22
TT
494 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
495 return -EIO;
496
230b8c1a
ZZ
497 if (strlen(name) > 255)
498 return -ERANGE;
499
617ba13b
MC
500 down_read(&EXT4_I(inode)->xattr_sem);
501 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
ac27a0ec
DK
502 buffer_size);
503 if (error == -ENODATA)
617ba13b 504 error = ext4_xattr_block_get(inode, name_index, name, buffer,
ac27a0ec 505 buffer_size);
617ba13b 506 up_read(&EXT4_I(inode)->xattr_sem);
ac27a0ec
DK
507 return error;
508}
509
510static int
431547b3 511ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
ac27a0ec
DK
512 char *buffer, size_t buffer_size)
513{
514 size_t rest = buffer_size;
515
617ba13b 516 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
11e27528 517 const struct xattr_handler *handler =
617ba13b 518 ext4_xattr_handler(entry->e_name_index);
ac27a0ec 519
764a5c6b
AG
520 if (handler && (!handler->list || handler->list(dentry))) {
521 const char *prefix = handler->prefix ?: handler->name;
522 size_t prefix_len = strlen(prefix);
523 size_t size = prefix_len + entry->e_name_len + 1;
524
ac27a0ec
DK
525 if (buffer) {
526 if (size > rest)
527 return -ERANGE;
764a5c6b
AG
528 memcpy(buffer, prefix, prefix_len);
529 buffer += prefix_len;
530 memcpy(buffer, entry->e_name, entry->e_name_len);
531 buffer += entry->e_name_len;
532 *buffer++ = 0;
ac27a0ec
DK
533 }
534 rest -= size;
535 }
536 }
764a5c6b 537 return buffer_size - rest; /* total size */
ac27a0ec
DK
538}
539
540static int
431547b3 541ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 542{
2b0143b5 543 struct inode *inode = d_inode(dentry);
ac27a0ec
DK
544 struct buffer_head *bh = NULL;
545 int error;
7a2508e1 546 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
ac27a0ec
DK
547
548 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
549 buffer, (long)buffer_size);
550
551 error = 0;
617ba13b 552 if (!EXT4_I(inode)->i_file_acl)
ac27a0ec 553 goto cleanup;
ace36ad4
JP
554 ea_idebug(inode, "reading block %llu",
555 (unsigned long long)EXT4_I(inode)->i_file_acl);
617ba13b 556 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
557 error = -EIO;
558 if (!bh)
559 goto cleanup;
560 ea_bdebug(bh, "b_count=%d, refcount=%d",
561 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
cc8e94fd 562 if (ext4_xattr_check_block(inode, bh)) {
24676da4
TT
563 EXT4_ERROR_INODE(inode, "bad block %llu",
564 EXT4_I(inode)->i_file_acl);
6a797d27 565 error = -EFSCORRUPTED;
ac27a0ec
DK
566 goto cleanup;
567 }
9c191f70 568 ext4_xattr_cache_insert(ext4_mb_cache, bh);
431547b3 569 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
ac27a0ec
DK
570
571cleanup:
572 brelse(bh);
573
574 return error;
575}
576
577static int
431547b3 578ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 579{
2b0143b5 580 struct inode *inode = d_inode(dentry);
617ba13b
MC
581 struct ext4_xattr_ibody_header *header;
582 struct ext4_inode *raw_inode;
583 struct ext4_iloc iloc;
ac27a0ec
DK
584 void *end;
585 int error;
586
19f5fb7a 587 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
ac27a0ec 588 return 0;
617ba13b 589 error = ext4_get_inode_loc(inode, &iloc);
ac27a0ec
DK
590 if (error)
591 return error;
617ba13b 592 raw_inode = ext4_raw_inode(&iloc);
ac27a0ec 593 header = IHDR(inode, raw_inode);
617ba13b 594 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
9e92f48c 595 error = xattr_check_inode(inode, header, end);
ac27a0ec
DK
596 if (error)
597 goto cleanup;
431547b3 598 error = ext4_xattr_list_entries(dentry, IFIRST(header),
ac27a0ec
DK
599 buffer, buffer_size);
600
601cleanup:
602 brelse(iloc.bh);
603 return error;
604}
605
606/*
ba7ea1d8
EB
607 * Inode operation listxattr()
608 *
609 * d_inode(dentry)->i_rwsem: don't care
ac27a0ec
DK
610 *
611 * Copy a list of attribute names into the buffer
612 * provided, or compute the buffer size required.
613 * Buffer is NULL to compute the size of the buffer required.
614 *
615 * Returns a negative error number on failure, or the number of bytes
616 * used / required on success.
617 */
ba7ea1d8
EB
618ssize_t
619ext4_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
ac27a0ec 620{
eaeef867 621 int ret, ret2;
ac27a0ec 622
2b0143b5 623 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
eaeef867
TT
624 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
625 if (ret < 0)
626 goto errout;
627 if (buffer) {
628 buffer += ret;
629 buffer_size -= ret;
ac27a0ec 630 }
eaeef867
TT
631 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
632 if (ret < 0)
633 goto errout;
634 ret += ret2;
635errout:
2b0143b5 636 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
eaeef867 637 return ret;
ac27a0ec
DK
638}
639
640/*
617ba13b 641 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
ac27a0ec
DK
642 * not set, set it.
643 */
617ba13b 644static void ext4_xattr_update_super_block(handle_t *handle,
ac27a0ec
DK
645 struct super_block *sb)
646{
e2b911c5 647 if (ext4_has_feature_xattr(sb))
ac27a0ec
DK
648 return;
649
5d601255 650 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
617ba13b 651 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
e2b911c5 652 ext4_set_feature_xattr(sb);
a0375156 653 ext4_handle_dirty_super(handle, sb);
ac27a0ec 654 }
ac27a0ec
DK
655}
656
657/*
ec4cb1aa
JK
658 * Release the xattr block BH: If the reference count is > 1, decrement it;
659 * otherwise free the block.
ac27a0ec
DK
660 */
661static void
617ba13b 662ext4_xattr_release_block(handle_t *handle, struct inode *inode,
ac27a0ec
DK
663 struct buffer_head *bh)
664{
6048c64b
AG
665 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
666 u32 hash, ref;
8a2bfdcb 667 int error = 0;
ac27a0ec 668
5d601255 669 BUFFER_TRACE(bh, "get_write_access");
8a2bfdcb
MC
670 error = ext4_journal_get_write_access(handle, bh);
671 if (error)
672 goto out;
673
674 lock_buffer(bh);
6048c64b
AG
675 hash = le32_to_cpu(BHDR(bh)->h_hash);
676 ref = le32_to_cpu(BHDR(bh)->h_refcount);
677 if (ref == 1) {
ac27a0ec 678 ea_bdebug(bh, "refcount now=0; freeing");
82939d79
JK
679 /*
680 * This must happen under buffer lock for
681 * ext4_xattr_block_set() to reliably detect freed block
682 */
6048c64b 683 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
ac27a0ec 684 get_bh(bh);
ec4cb1aa 685 unlock_buffer(bh);
e6362609
TT
686 ext4_free_blocks(handle, inode, bh, 0, 1,
687 EXT4_FREE_BLOCKS_METADATA |
688 EXT4_FREE_BLOCKS_FORGET);
ac27a0ec 689 } else {
6048c64b
AG
690 ref--;
691 BHDR(bh)->h_refcount = cpu_to_le32(ref);
692 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
693 struct mb_cache_entry *ce;
694
695 ce = mb_cache_entry_get(ext4_mb_cache, hash,
696 bh->b_blocknr);
697 if (ce) {
698 ce->e_reusable = 1;
699 mb_cache_entry_put(ext4_mb_cache, ce);
700 }
701 }
702
dac7a4b4 703 ext4_xattr_block_csum_set(inode, bh);
ec4cb1aa
JK
704 /*
705 * Beware of this ugliness: Releasing of xattr block references
706 * from different inodes can race and so we have to protect
707 * from a race where someone else frees the block (and releases
708 * its journal_head) before we are done dirtying the buffer. In
709 * nojournal mode this race is harmless and we actually cannot
dac7a4b4 710 * call ext4_handle_dirty_metadata() with locked buffer as
ec4cb1aa
JK
711 * that function can call sync_dirty_buffer() so for that case
712 * we handle the dirtying after unlocking the buffer.
713 */
714 if (ext4_handle_valid(handle))
dac7a4b4 715 error = ext4_handle_dirty_metadata(handle, inode, bh);
c1bb05a6 716 unlock_buffer(bh);
ec4cb1aa 717 if (!ext4_handle_valid(handle))
dac7a4b4 718 error = ext4_handle_dirty_metadata(handle, inode, bh);
8a2bfdcb 719 if (IS_SYNC(inode))
0390131b 720 ext4_handle_sync(handle);
1231b3a1 721 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
8a2bfdcb
MC
722 ea_bdebug(bh, "refcount now=%d; releasing",
723 le32_to_cpu(BHDR(bh)->h_refcount));
ac27a0ec 724 }
8a2bfdcb
MC
725out:
726 ext4_std_error(inode->i_sb, error);
727 return;
ac27a0ec
DK
728}
729
6dd4ee7c
KS
730/*
731 * Find the available free space for EAs. This also returns the total number of
732 * bytes used by EA entries.
733 */
734static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
735 size_t *min_offs, void *base, int *total)
736{
737 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
e50e5129 738 if (!last->e_value_inum && last->e_value_size) {
6dd4ee7c
KS
739 size_t offs = le16_to_cpu(last->e_value_offs);
740 if (offs < *min_offs)
741 *min_offs = offs;
742 }
7b1b2c1b
TT
743 if (total)
744 *total += EXT4_XATTR_LEN(last->e_name_len);
6dd4ee7c
KS
745 }
746 return (*min_offs - ((void *)last - base) - sizeof(__u32));
747}
748
e50e5129
AD
749/*
750 * Write the value of the EA in an inode.
751 */
752static int ext4_xattr_inode_write(handle_t *handle, struct inode *ea_inode,
753 const void *buf, int bufsize)
754{
755 struct buffer_head *bh = NULL;
756 unsigned long block = 0;
757 unsigned blocksize = ea_inode->i_sb->s_blocksize;
758 unsigned max_blocks = (bufsize + blocksize - 1) >> ea_inode->i_blkbits;
759 int csize, wsize = 0;
760 int ret = 0;
761 int retries = 0;
762
763retry:
764 while (ret >= 0 && ret < max_blocks) {
765 struct ext4_map_blocks map;
766 map.m_lblk = block += ret;
767 map.m_len = max_blocks -= ret;
768
769 ret = ext4_map_blocks(handle, ea_inode, &map,
770 EXT4_GET_BLOCKS_CREATE);
771 if (ret <= 0) {
772 ext4_mark_inode_dirty(handle, ea_inode);
773 if (ret == -ENOSPC &&
774 ext4_should_retry_alloc(ea_inode->i_sb, &retries)) {
775 ret = 0;
776 goto retry;
777 }
778 break;
779 }
780 }
781
782 if (ret < 0)
783 return ret;
784
785 block = 0;
786 while (wsize < bufsize) {
787 if (bh != NULL)
788 brelse(bh);
789 csize = (bufsize - wsize) > blocksize ? blocksize :
790 bufsize - wsize;
791 bh = ext4_getblk(handle, ea_inode, block, 0);
792 if (IS_ERR(bh))
793 return PTR_ERR(bh);
794 ret = ext4_journal_get_write_access(handle, bh);
795 if (ret)
796 goto out;
797
798 memcpy(bh->b_data, buf, csize);
799 set_buffer_uptodate(bh);
800 ext4_handle_dirty_metadata(handle, ea_inode, bh);
801
802 buf += csize;
803 wsize += csize;
804 block += 1;
805 }
806
807 inode_lock(ea_inode);
808 i_size_write(ea_inode, wsize);
809 ext4_update_i_disksize(ea_inode, wsize);
810 inode_unlock(ea_inode);
811
812 ext4_mark_inode_dirty(handle, ea_inode);
813
814out:
815 brelse(bh);
816
817 return ret;
818}
819
820/*
821 * Create an inode to store the value of a large EA.
822 */
823static struct inode *ext4_xattr_inode_create(handle_t *handle,
824 struct inode *inode)
825{
826 struct inode *ea_inode = NULL;
bd3b963b 827 int err;
e50e5129
AD
828
829 /*
830 * Let the next inode be the goal, so we try and allocate the EA inode
831 * in the same group, or nearby one.
832 */
833 ea_inode = ext4_new_inode(handle, inode->i_sb->s_root->d_inode,
1b917ed8
TE
834 S_IFREG | 0600, NULL, inode->i_ino + 1, NULL,
835 EXT4_EA_INODE_FL);
e50e5129
AD
836 if (!IS_ERR(ea_inode)) {
837 ea_inode->i_op = &ext4_file_inode_operations;
838 ea_inode->i_fop = &ext4_file_operations;
839 ext4_set_aops(ea_inode);
33d201e0 840 ext4_xattr_inode_set_class(ea_inode);
e50e5129
AD
841 ea_inode->i_generation = inode->i_generation;
842 EXT4_I(ea_inode)->i_flags |= EXT4_EA_INODE_FL;
843
844 /*
845 * A back-pointer from EA inode to parent inode will be useful
846 * for e2fsck.
847 */
848 EXT4_XATTR_INODE_SET_PARENT(ea_inode, inode->i_ino);
849 unlock_new_inode(ea_inode);
bd3b963b
TE
850 err = ext4_inode_attach_jinode(ea_inode);
851 if (err) {
852 iput(ea_inode);
853 return ERR_PTR(err);
854 }
e50e5129
AD
855 }
856
857 return ea_inode;
858}
859
860/*
861 * Unlink the inode storing the value of the EA.
862 */
863int ext4_xattr_inode_unlink(struct inode *inode, unsigned long ea_ino)
864{
865 struct inode *ea_inode = NULL;
866 int err;
867
868 ea_inode = ext4_xattr_inode_iget(inode, ea_ino, &err);
869 if (err)
870 return err;
871
872 clear_nlink(ea_inode);
873 iput(ea_inode);
874
875 return 0;
876}
877
878/*
879 * Add value of the EA in an inode.
880 */
881static int ext4_xattr_inode_set(handle_t *handle, struct inode *inode,
882 unsigned long *ea_ino, const void *value,
883 size_t value_len)
884{
885 struct inode *ea_inode;
886 int err;
887
888 /* Create an inode for the EA value */
889 ea_inode = ext4_xattr_inode_create(handle, inode);
890 if (IS_ERR(ea_inode))
891 return PTR_ERR(ea_inode);
892
893 err = ext4_xattr_inode_write(handle, ea_inode, value, value_len);
894 if (err)
895 clear_nlink(ea_inode);
896 else
897 *ea_ino = ea_inode->i_ino;
898
899 iput(ea_inode);
900
901 return err;
902}
903
904static int ext4_xattr_set_entry(struct ext4_xattr_info *i,
905 struct ext4_xattr_search *s,
906 handle_t *handle, struct inode *inode)
ac27a0ec 907{
617ba13b 908 struct ext4_xattr_entry *last;
ac27a0ec 909 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
e50e5129
AD
910 int in_inode = i->in_inode;
911 int rc;
912
913 if (ext4_has_feature_ea_inode(inode->i_sb) &&
914 (EXT4_XATTR_SIZE(i->value_len) >
915 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode->i_sb->s_blocksize)))
916 in_inode = 1;
ac27a0ec
DK
917
918 /* Compute min_offs and last. */
919 last = s->first;
617ba13b 920 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
e50e5129 921 if (!last->e_value_inum && last->e_value_size) {
ac27a0ec
DK
922 size_t offs = le16_to_cpu(last->e_value_offs);
923 if (offs < min_offs)
924 min_offs = offs;
925 }
926 }
927 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
928 if (!s->not_found) {
e50e5129
AD
929 if (!in_inode &&
930 !s->here->e_value_inum && s->here->e_value_size) {
ac27a0ec 931 size_t size = le32_to_cpu(s->here->e_value_size);
617ba13b 932 free += EXT4_XATTR_SIZE(size);
ac27a0ec 933 }
617ba13b 934 free += EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
935 }
936 if (i->value) {
e50e5129
AD
937 size_t value_len = EXT4_XATTR_SIZE(i->value_len);
938
939 if (in_inode)
940 value_len = 0;
941
942 if (free < EXT4_XATTR_LEN(name_len) + value_len)
ac27a0ec
DK
943 return -ENOSPC;
944 }
945
946 if (i->value && s->not_found) {
947 /* Insert the new name. */
617ba13b 948 size_t size = EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
949 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
950 memmove((void *)s->here + size, s->here, rest);
951 memset(s->here, 0, size);
952 s->here->e_name_index = i->name_index;
953 s->here->e_name_len = name_len;
954 memcpy(s->here->e_name, i->name, name_len);
955 } else {
e50e5129
AD
956 if (!s->here->e_value_inum && s->here->e_value_size &&
957 s->here->e_value_offs > 0) {
ac27a0ec
DK
958 void *first_val = s->base + min_offs;
959 size_t offs = le16_to_cpu(s->here->e_value_offs);
960 void *val = s->base + offs;
617ba13b 961 size_t size = EXT4_XATTR_SIZE(
ac27a0ec
DK
962 le32_to_cpu(s->here->e_value_size));
963
617ba13b 964 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
ac27a0ec
DK
965 /* The old and the new value have the same
966 size. Just replace. */
967 s->here->e_value_size =
968 cpu_to_le32(i->value_len);
bd9926e8
TT
969 if (i->value == EXT4_ZERO_XATTR_VALUE) {
970 memset(val, 0, size);
971 } else {
972 /* Clear pad bytes first. */
973 memset(val + size - EXT4_XATTR_PAD, 0,
974 EXT4_XATTR_PAD);
975 memcpy(val, i->value, i->value_len);
976 }
ac27a0ec
DK
977 return 0;
978 }
979
980 /* Remove the old value. */
981 memmove(first_val + size, first_val, val - first_val);
982 memset(first_val, 0, size);
983 s->here->e_value_size = 0;
984 s->here->e_value_offs = 0;
985 min_offs += size;
986
987 /* Adjust all value offsets. */
988 last = s->first;
989 while (!IS_LAST_ENTRY(last)) {
990 size_t o = le16_to_cpu(last->e_value_offs);
e50e5129
AD
991 if (!last->e_value_inum &&
992 last->e_value_size && o < offs)
ac27a0ec
DK
993 last->e_value_offs =
994 cpu_to_le16(o + size);
617ba13b 995 last = EXT4_XATTR_NEXT(last);
ac27a0ec
DK
996 }
997 }
e50e5129
AD
998 if (s->here->e_value_inum) {
999 ext4_xattr_inode_unlink(inode,
1000 le32_to_cpu(s->here->e_value_inum));
1001 s->here->e_value_inum = 0;
1002 }
ac27a0ec
DK
1003 if (!i->value) {
1004 /* Remove the old name. */
617ba13b 1005 size_t size = EXT4_XATTR_LEN(name_len);
ac27a0ec
DK
1006 last = ENTRY((void *)last - size);
1007 memmove(s->here, (void *)s->here + size,
1008 (void *)last - (void *)s->here + sizeof(__u32));
1009 memset(last, 0, size);
1010 }
1011 }
1012
1013 if (i->value) {
1014 /* Insert the new value. */
e50e5129
AD
1015 if (in_inode) {
1016 unsigned long ea_ino =
1017 le32_to_cpu(s->here->e_value_inum);
1018 rc = ext4_xattr_inode_set(handle, inode, &ea_ino,
1019 i->value, i->value_len);
1020 if (rc)
1021 goto out;
1022 s->here->e_value_inum = cpu_to_le32(ea_ino);
1023 s->here->e_value_offs = 0;
1024 } else if (i->value_len) {
617ba13b 1025 size_t size = EXT4_XATTR_SIZE(i->value_len);
ac27a0ec
DK
1026 void *val = s->base + min_offs - size;
1027 s->here->e_value_offs = cpu_to_le16(min_offs - size);
e50e5129 1028 s->here->e_value_inum = 0;
bd9926e8
TT
1029 if (i->value == EXT4_ZERO_XATTR_VALUE) {
1030 memset(val, 0, size);
1031 } else {
1032 /* Clear the pad bytes first. */
1033 memset(val + size - EXT4_XATTR_PAD, 0,
1034 EXT4_XATTR_PAD);
1035 memcpy(val, i->value, i->value_len);
1036 }
ac27a0ec 1037 }
e50e5129 1038 s->here->e_value_size = cpu_to_le32(i->value_len);
ac27a0ec 1039 }
e50e5129
AD
1040
1041out:
1042 return rc;
ac27a0ec
DK
1043}
1044
617ba13b
MC
1045struct ext4_xattr_block_find {
1046 struct ext4_xattr_search s;
ac27a0ec
DK
1047 struct buffer_head *bh;
1048};
1049
1050static int
617ba13b
MC
1051ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
1052 struct ext4_xattr_block_find *bs)
ac27a0ec
DK
1053{
1054 struct super_block *sb = inode->i_sb;
1055 int error;
1056
1057 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
1058 i->name_index, i->name, i->value, (long)i->value_len);
1059
617ba13b 1060 if (EXT4_I(inode)->i_file_acl) {
ac27a0ec 1061 /* The inode already has an extended attribute block. */
617ba13b 1062 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
1063 error = -EIO;
1064 if (!bs->bh)
1065 goto cleanup;
1066 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
1067 atomic_read(&(bs->bh->b_count)),
1068 le32_to_cpu(BHDR(bs->bh)->h_refcount));
cc8e94fd 1069 if (ext4_xattr_check_block(inode, bs->bh)) {
24676da4
TT
1070 EXT4_ERROR_INODE(inode, "bad block %llu",
1071 EXT4_I(inode)->i_file_acl);
6a797d27 1072 error = -EFSCORRUPTED;
ac27a0ec
DK
1073 goto cleanup;
1074 }
1075 /* Find the named attribute. */
1076 bs->s.base = BHDR(bs->bh);
1077 bs->s.first = BFIRST(bs->bh);
1078 bs->s.end = bs->bh->b_data + bs->bh->b_size;
1079 bs->s.here = bs->s.first;
617ba13b 1080 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
6ba644b9 1081 i->name, 1);
ac27a0ec
DK
1082 if (error && error != -ENODATA)
1083 goto cleanup;
1084 bs->s.not_found = error;
1085 }
1086 error = 0;
1087
1088cleanup:
1089 return error;
1090}
1091
1092static int
617ba13b
MC
1093ext4_xattr_block_set(handle_t *handle, struct inode *inode,
1094 struct ext4_xattr_info *i,
1095 struct ext4_xattr_block_find *bs)
ac27a0ec
DK
1096{
1097 struct super_block *sb = inode->i_sb;
1098 struct buffer_head *new_bh = NULL;
617ba13b 1099 struct ext4_xattr_search *s = &bs->s;
7a2508e1 1100 struct mb_cache_entry *ce = NULL;
8a2bfdcb 1101 int error = 0;
7a2508e1 1102 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
ac27a0ec 1103
617ba13b 1104#define header(x) ((struct ext4_xattr_header *)(x))
ac27a0ec 1105
ac27a0ec 1106 if (s->base) {
5d601255 1107 BUFFER_TRACE(bs->bh, "get_write_access");
8a2bfdcb
MC
1108 error = ext4_journal_get_write_access(handle, bs->bh);
1109 if (error)
1110 goto cleanup;
1111 lock_buffer(bs->bh);
1112
ac27a0ec 1113 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
82939d79
JK
1114 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
1115
1116 /*
1117 * This must happen under buffer lock for
1118 * ext4_xattr_block_set() to reliably detect modified
1119 * block
1120 */
7a2508e1
JK
1121 mb_cache_entry_delete_block(ext4_mb_cache, hash,
1122 bs->bh->b_blocknr);
ac27a0ec 1123 ea_bdebug(bs->bh, "modifying in-place");
e50e5129 1124 error = ext4_xattr_set_entry(i, s, handle, inode);
ac27a0ec
DK
1125 if (!error) {
1126 if (!IS_LAST_ENTRY(s->first))
617ba13b 1127 ext4_xattr_rehash(header(s->base),
ac27a0ec 1128 s->here);
9c191f70
M
1129 ext4_xattr_cache_insert(ext4_mb_cache,
1130 bs->bh);
ac27a0ec 1131 }
dac7a4b4 1132 ext4_xattr_block_csum_set(inode, bs->bh);
ac27a0ec 1133 unlock_buffer(bs->bh);
6a797d27 1134 if (error == -EFSCORRUPTED)
ac27a0ec
DK
1135 goto bad_block;
1136 if (!error)
dac7a4b4
TT
1137 error = ext4_handle_dirty_metadata(handle,
1138 inode,
1139 bs->bh);
ac27a0ec
DK
1140 if (error)
1141 goto cleanup;
1142 goto inserted;
1143 } else {
1144 int offset = (char *)s->here - bs->bh->b_data;
1145
8a2bfdcb 1146 unlock_buffer(bs->bh);
ac27a0ec 1147 ea_bdebug(bs->bh, "cloning");
216553c4 1148 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
ac27a0ec
DK
1149 error = -ENOMEM;
1150 if (s->base == NULL)
1151 goto cleanup;
1152 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
1153 s->first = ENTRY(header(s->base)+1);
1154 header(s->base)->h_refcount = cpu_to_le32(1);
1155 s->here = ENTRY(s->base + offset);
1156 s->end = s->base + bs->bh->b_size;
1157 }
1158 } else {
1159 /* Allocate a buffer where we construct the new block. */
216553c4 1160 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
ac27a0ec
DK
1161 /* assert(header == s->base) */
1162 error = -ENOMEM;
1163 if (s->base == NULL)
1164 goto cleanup;
617ba13b 1165 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
ac27a0ec
DK
1166 header(s->base)->h_blocks = cpu_to_le32(1);
1167 header(s->base)->h_refcount = cpu_to_le32(1);
1168 s->first = ENTRY(header(s->base)+1);
1169 s->here = ENTRY(header(s->base)+1);
1170 s->end = s->base + sb->s_blocksize;
1171 }
1172
e50e5129 1173 error = ext4_xattr_set_entry(i, s, handle, inode);
6a797d27 1174 if (error == -EFSCORRUPTED)
ac27a0ec
DK
1175 goto bad_block;
1176 if (error)
1177 goto cleanup;
1178 if (!IS_LAST_ENTRY(s->first))
617ba13b 1179 ext4_xattr_rehash(header(s->base), s->here);
ac27a0ec
DK
1180
1181inserted:
1182 if (!IS_LAST_ENTRY(s->first)) {
617ba13b 1183 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
ac27a0ec
DK
1184 if (new_bh) {
1185 /* We found an identical block in the cache. */
1186 if (new_bh == bs->bh)
1187 ea_bdebug(new_bh, "keeping");
1188 else {
6048c64b
AG
1189 u32 ref;
1190
b8cb5a54
TE
1191 WARN_ON_ONCE(dquot_initialize_needed(inode));
1192
ac27a0ec
DK
1193 /* The old block is released after updating
1194 the inode. */
1231b3a1
LC
1195 error = dquot_alloc_block(inode,
1196 EXT4_C2B(EXT4_SB(sb), 1));
5dd4056d 1197 if (error)
ac27a0ec 1198 goto cleanup;
5d601255 1199 BUFFER_TRACE(new_bh, "get_write_access");
617ba13b 1200 error = ext4_journal_get_write_access(handle,
ac27a0ec
DK
1201 new_bh);
1202 if (error)
1203 goto cleanup_dquot;
1204 lock_buffer(new_bh);
82939d79
JK
1205 /*
1206 * We have to be careful about races with
6048c64b
AG
1207 * freeing, rehashing or adding references to
1208 * xattr block. Once we hold buffer lock xattr
1209 * block's state is stable so we can check
1210 * whether the block got freed / rehashed or
1211 * not. Since we unhash mbcache entry under
1212 * buffer lock when freeing / rehashing xattr
1213 * block, checking whether entry is still
1214 * hashed is reliable. Same rules hold for
1215 * e_reusable handling.
82939d79 1216 */
6048c64b
AG
1217 if (hlist_bl_unhashed(&ce->e_hash_list) ||
1218 !ce->e_reusable) {
82939d79
JK
1219 /*
1220 * Undo everything and check mbcache
1221 * again.
1222 */
1223 unlock_buffer(new_bh);
1224 dquot_free_block(inode,
1225 EXT4_C2B(EXT4_SB(sb),
1226 1));
1227 brelse(new_bh);
7a2508e1 1228 mb_cache_entry_put(ext4_mb_cache, ce);
82939d79
JK
1229 ce = NULL;
1230 new_bh = NULL;
1231 goto inserted;
1232 }
6048c64b
AG
1233 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
1234 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
1235 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
1236 ce->e_reusable = 0;
ac27a0ec 1237 ea_bdebug(new_bh, "reusing; refcount now=%d",
6048c64b 1238 ref);
dac7a4b4 1239 ext4_xattr_block_csum_set(inode, new_bh);
ac27a0ec 1240 unlock_buffer(new_bh);
dac7a4b4
TT
1241 error = ext4_handle_dirty_metadata(handle,
1242 inode,
1243 new_bh);
ac27a0ec
DK
1244 if (error)
1245 goto cleanup_dquot;
1246 }
7a2508e1
JK
1247 mb_cache_entry_touch(ext4_mb_cache, ce);
1248 mb_cache_entry_put(ext4_mb_cache, ce);
ac27a0ec
DK
1249 ce = NULL;
1250 } else if (bs->bh && s->base == bs->bh->b_data) {
1251 /* We were modifying this block in-place. */
1252 ea_bdebug(bs->bh, "keeping this block");
1253 new_bh = bs->bh;
1254 get_bh(new_bh);
1255 } else {
1256 /* We need to allocate a new block */
fb0a387d
ES
1257 ext4_fsblk_t goal, block;
1258
b8cb5a54
TE
1259 WARN_ON_ONCE(dquot_initialize_needed(inode));
1260
fb0a387d 1261 goal = ext4_group_first_block_no(sb,
d00a6d7b 1262 EXT4_I(inode)->i_block_group);
fb0a387d
ES
1263
1264 /* non-extent files can't have physical blocks past 2^32 */
12e9b892 1265 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
fb0a387d
ES
1266 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
1267
55f020db
AH
1268 block = ext4_new_meta_blocks(handle, inode, goal, 0,
1269 NULL, &error);
ac27a0ec
DK
1270 if (error)
1271 goto cleanup;
fb0a387d 1272
12e9b892 1273 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
fb0a387d
ES
1274 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1275
ace36ad4
JP
1276 ea_idebug(inode, "creating block %llu",
1277 (unsigned long long)block);
ac27a0ec
DK
1278
1279 new_bh = sb_getblk(sb, block);
aebf0243 1280 if (unlikely(!new_bh)) {
860d21e2 1281 error = -ENOMEM;
ac27a0ec 1282getblk_failed:
7dc57615 1283 ext4_free_blocks(handle, inode, NULL, block, 1,
e6362609 1284 EXT4_FREE_BLOCKS_METADATA);
ac27a0ec
DK
1285 goto cleanup;
1286 }
1287 lock_buffer(new_bh);
617ba13b 1288 error = ext4_journal_get_create_access(handle, new_bh);
ac27a0ec
DK
1289 if (error) {
1290 unlock_buffer(new_bh);
860d21e2 1291 error = -EIO;
ac27a0ec
DK
1292 goto getblk_failed;
1293 }
1294 memcpy(new_bh->b_data, s->base, new_bh->b_size);
dac7a4b4 1295 ext4_xattr_block_csum_set(inode, new_bh);
ac27a0ec
DK
1296 set_buffer_uptodate(new_bh);
1297 unlock_buffer(new_bh);
9c191f70 1298 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
dac7a4b4
TT
1299 error = ext4_handle_dirty_metadata(handle, inode,
1300 new_bh);
ac27a0ec
DK
1301 if (error)
1302 goto cleanup;
1303 }
1304 }
1305
1306 /* Update the inode. */
617ba13b 1307 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
ac27a0ec
DK
1308
1309 /* Drop the previous xattr block. */
1310 if (bs->bh && bs->bh != new_bh)
617ba13b 1311 ext4_xattr_release_block(handle, inode, bs->bh);
ac27a0ec
DK
1312 error = 0;
1313
1314cleanup:
1315 if (ce)
7a2508e1 1316 mb_cache_entry_put(ext4_mb_cache, ce);
ac27a0ec
DK
1317 brelse(new_bh);
1318 if (!(bs->bh && s->base == bs->bh->b_data))
1319 kfree(s->base);
1320
1321 return error;
1322
1323cleanup_dquot:
1231b3a1 1324 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
ac27a0ec
DK
1325 goto cleanup;
1326
1327bad_block:
24676da4
TT
1328 EXT4_ERROR_INODE(inode, "bad block %llu",
1329 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
1330 goto cleanup;
1331
1332#undef header
1333}
1334
879b3825
TM
1335int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1336 struct ext4_xattr_ibody_find *is)
ac27a0ec 1337{
617ba13b
MC
1338 struct ext4_xattr_ibody_header *header;
1339 struct ext4_inode *raw_inode;
ac27a0ec
DK
1340 int error;
1341
617ba13b 1342 if (EXT4_I(inode)->i_extra_isize == 0)
ac27a0ec 1343 return 0;
617ba13b 1344 raw_inode = ext4_raw_inode(&is->iloc);
ac27a0ec
DK
1345 header = IHDR(inode, raw_inode);
1346 is->s.base = is->s.first = IFIRST(header);
1347 is->s.here = is->s.first;
617ba13b 1348 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
19f5fb7a 1349 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
9e92f48c 1350 error = xattr_check_inode(inode, header, is->s.end);
ac27a0ec
DK
1351 if (error)
1352 return error;
1353 /* Find the named attribute. */
617ba13b 1354 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
6ba644b9 1355 i->name, 0);
ac27a0ec
DK
1356 if (error && error != -ENODATA)
1357 return error;
1358 is->s.not_found = error;
1359 }
1360 return 0;
1361}
1362
0d812f77
TM
1363int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1364 struct ext4_xattr_info *i,
1365 struct ext4_xattr_ibody_find *is)
1366{
1367 struct ext4_xattr_ibody_header *header;
1368 struct ext4_xattr_search *s = &is->s;
1369 int error;
1370
1371 if (EXT4_I(inode)->i_extra_isize == 0)
1372 return -ENOSPC;
e50e5129 1373 error = ext4_xattr_set_entry(i, s, handle, inode);
0d812f77
TM
1374 if (error) {
1375 if (error == -ENOSPC &&
1376 ext4_has_inline_data(inode)) {
1377 error = ext4_try_to_evict_inline_data(handle, inode,
1378 EXT4_XATTR_LEN(strlen(i->name) +
1379 EXT4_XATTR_SIZE(i->value_len)));
1380 if (error)
1381 return error;
1382 error = ext4_xattr_ibody_find(inode, i, is);
1383 if (error)
1384 return error;
e50e5129 1385 error = ext4_xattr_set_entry(i, s, handle, inode);
0d812f77
TM
1386 }
1387 if (error)
1388 return error;
1389 }
1390 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1391 if (!IS_LAST_ENTRY(s->first)) {
1392 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1393 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1394 } else {
1395 header->h_magic = cpu_to_le32(0);
1396 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1397 }
1398 return 0;
1399}
1400
e50e5129 1401static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
0d812f77
TM
1402 struct ext4_xattr_info *i,
1403 struct ext4_xattr_ibody_find *is)
ac27a0ec 1404{
617ba13b
MC
1405 struct ext4_xattr_ibody_header *header;
1406 struct ext4_xattr_search *s = &is->s;
ac27a0ec
DK
1407 int error;
1408
617ba13b 1409 if (EXT4_I(inode)->i_extra_isize == 0)
ac27a0ec 1410 return -ENOSPC;
e50e5129 1411 error = ext4_xattr_set_entry(i, s, handle, inode);
ac27a0ec
DK
1412 if (error)
1413 return error;
617ba13b 1414 header = IHDR(inode, ext4_raw_inode(&is->iloc));
ac27a0ec 1415 if (!IS_LAST_ENTRY(s->first)) {
617ba13b 1416 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
19f5fb7a 1417 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
ac27a0ec
DK
1418 } else {
1419 header->h_magic = cpu_to_le32(0);
19f5fb7a 1420 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
ac27a0ec
DK
1421 }
1422 return 0;
1423}
1424
3fd16462
JK
1425static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1426 struct ext4_xattr_info *i)
1427{
1428 void *value;
1429
1430 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1431 return 0;
1432 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1433 return !memcmp(value, i->value, i->value_len);
1434}
1435
ac27a0ec 1436/*
617ba13b 1437 * ext4_xattr_set_handle()
ac27a0ec 1438 *
6e9510b0 1439 * Create, replace or remove an extended attribute for this inode. Value
ac27a0ec
DK
1440 * is NULL to remove an existing extended attribute, and non-NULL to
1441 * either replace an existing extended attribute, or create a new extended
1442 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1443 * specify that an extended attribute must exist and must not exist
1444 * previous to the call, respectively.
1445 *
1446 * Returns 0, or a negative error number on failure.
1447 */
1448int
617ba13b 1449ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
ac27a0ec
DK
1450 const char *name, const void *value, size_t value_len,
1451 int flags)
1452{
617ba13b 1453 struct ext4_xattr_info i = {
ac27a0ec
DK
1454 .name_index = name_index,
1455 .name = name,
1456 .value = value,
1457 .value_len = value_len,
e50e5129 1458 .in_inode = 0,
ac27a0ec 1459 };
617ba13b 1460 struct ext4_xattr_ibody_find is = {
ac27a0ec
DK
1461 .s = { .not_found = -ENODATA, },
1462 };
617ba13b 1463 struct ext4_xattr_block_find bs = {
ac27a0ec
DK
1464 .s = { .not_found = -ENODATA, },
1465 };
c755e251 1466 int no_expand;
ac27a0ec
DK
1467 int error;
1468
1469 if (!name)
1470 return -EINVAL;
1471 if (strlen(name) > 255)
1472 return -ERANGE;
b8cb5a54 1473
c755e251 1474 ext4_write_lock_xattr(inode, &no_expand);
4d20c685 1475
66543617 1476 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
86ebfd08
ES
1477 if (error)
1478 goto cleanup;
1479
19f5fb7a 1480 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
617ba13b
MC
1481 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1482 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
19f5fb7a 1483 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
ac27a0ec
DK
1484 }
1485
617ba13b 1486 error = ext4_xattr_ibody_find(inode, &i, &is);
ac27a0ec
DK
1487 if (error)
1488 goto cleanup;
1489 if (is.s.not_found)
617ba13b 1490 error = ext4_xattr_block_find(inode, &i, &bs);
ac27a0ec
DK
1491 if (error)
1492 goto cleanup;
1493 if (is.s.not_found && bs.s.not_found) {
1494 error = -ENODATA;
1495 if (flags & XATTR_REPLACE)
1496 goto cleanup;
1497 error = 0;
1498 if (!value)
1499 goto cleanup;
1500 } else {
1501 error = -EEXIST;
1502 if (flags & XATTR_CREATE)
1503 goto cleanup;
1504 }
ac27a0ec
DK
1505 if (!value) {
1506 if (!is.s.not_found)
e50e5129 1507 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
ac27a0ec 1508 else if (!bs.s.not_found)
617ba13b 1509 error = ext4_xattr_block_set(handle, inode, &i, &bs);
ac27a0ec 1510 } else {
3fd16462
JK
1511 error = 0;
1512 /* Xattr value did not change? Save us some work and bail out */
1513 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1514 goto cleanup;
1515 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1516 goto cleanup;
1517
e50e5129 1518 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
ac27a0ec
DK
1519 if (!error && !bs.s.not_found) {
1520 i.value = NULL;
617ba13b 1521 error = ext4_xattr_block_set(handle, inode, &i, &bs);
ac27a0ec 1522 } else if (error == -ENOSPC) {
7e01c8e5
TY
1523 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1524 error = ext4_xattr_block_find(inode, &i, &bs);
1525 if (error)
1526 goto cleanup;
1527 }
617ba13b 1528 error = ext4_xattr_block_set(handle, inode, &i, &bs);
e50e5129
AD
1529 if (ext4_has_feature_ea_inode(inode->i_sb) &&
1530 error == -ENOSPC) {
1531 /* xattr not fit to block, store at external
1532 * inode */
1533 i.in_inode = 1;
1534 error = ext4_xattr_ibody_set(handle, inode,
1535 &i, &is);
1536 }
ac27a0ec
DK
1537 if (error)
1538 goto cleanup;
1539 if (!is.s.not_found) {
1540 i.value = NULL;
e50e5129
AD
1541 error = ext4_xattr_ibody_set(handle, inode, &i,
1542 &is);
ac27a0ec
DK
1543 }
1544 }
1545 }
1546 if (!error) {
617ba13b 1547 ext4_xattr_update_super_block(handle, inode->i_sb);
eeca7ea1 1548 inode->i_ctime = current_time(inode);
6dd4ee7c 1549 if (!value)
c755e251 1550 no_expand = 0;
617ba13b 1551 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
ac27a0ec 1552 /*
617ba13b 1553 * The bh is consumed by ext4_mark_iloc_dirty, even with
ac27a0ec
DK
1554 * error != 0.
1555 */
1556 is.iloc.bh = NULL;
1557 if (IS_SYNC(inode))
0390131b 1558 ext4_handle_sync(handle);
ac27a0ec
DK
1559 }
1560
1561cleanup:
1562 brelse(is.iloc.bh);
1563 brelse(bs.bh);
c755e251 1564 ext4_write_unlock_xattr(inode, &no_expand);
ac27a0ec
DK
1565 return error;
1566}
1567
1568/*
617ba13b 1569 * ext4_xattr_set()
ac27a0ec 1570 *
617ba13b 1571 * Like ext4_xattr_set_handle, but start from an inode. This extended
ac27a0ec
DK
1572 * attribute modification is a filesystem transaction by itself.
1573 *
1574 * Returns 0, or a negative error number on failure.
1575 */
1576int
617ba13b 1577ext4_xattr_set(struct inode *inode, int name_index, const char *name,
ac27a0ec
DK
1578 const void *value, size_t value_len, int flags)
1579{
1580 handle_t *handle;
e50e5129 1581 struct super_block *sb = inode->i_sb;
ac27a0ec 1582 int error, retries = 0;
95eaefbd 1583 int credits = ext4_jbd2_credits_xattr(inode);
ac27a0ec 1584
b8cb5a54
TE
1585 error = dquot_initialize(inode);
1586 if (error)
1587 return error;
e50e5129
AD
1588
1589 if ((value_len >= EXT4_XATTR_MIN_LARGE_EA_SIZE(sb->s_blocksize)) &&
1590 ext4_has_feature_ea_inode(sb)) {
1591 int nrblocks = (value_len + sb->s_blocksize - 1) >>
1592 sb->s_blocksize_bits;
1593
1594 /* For new inode */
1595 credits += EXT4_SINGLEDATA_TRANS_BLOCKS(sb) + 3;
1596
1597 /* For data blocks of EA inode */
1598 credits += ext4_meta_trans_blocks(inode, nrblocks, 0);
1599 }
1600
ac27a0ec 1601retry:
9924a92a 1602 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
ac27a0ec
DK
1603 if (IS_ERR(handle)) {
1604 error = PTR_ERR(handle);
1605 } else {
1606 int error2;
1607
617ba13b 1608 error = ext4_xattr_set_handle(handle, inode, name_index, name,
ac27a0ec 1609 value, value_len, flags);
617ba13b 1610 error2 = ext4_journal_stop(handle);
ac27a0ec 1611 if (error == -ENOSPC &&
e50e5129 1612 ext4_should_retry_alloc(sb, &retries))
ac27a0ec
DK
1613 goto retry;
1614 if (error == 0)
1615 error = error2;
1616 }
1617
1618 return error;
1619}
1620
6dd4ee7c
KS
1621/*
1622 * Shift the EA entries in the inode to create space for the increased
1623 * i_extra_isize.
1624 */
1625static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1626 int value_offs_shift, void *to,
94405713 1627 void *from, size_t n)
6dd4ee7c
KS
1628{
1629 struct ext4_xattr_entry *last = entry;
1630 int new_offs;
1631
94405713
JK
1632 /* We always shift xattr headers further thus offsets get lower */
1633 BUG_ON(value_offs_shift > 0);
1634
6dd4ee7c
KS
1635 /* Adjust the value offsets of the entries */
1636 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
e50e5129 1637 if (!last->e_value_inum && last->e_value_size) {
6dd4ee7c
KS
1638 new_offs = le16_to_cpu(last->e_value_offs) +
1639 value_offs_shift;
6dd4ee7c
KS
1640 last->e_value_offs = cpu_to_le16(new_offs);
1641 }
1642 }
1643 /* Shift the entries by n bytes */
1644 memmove(to, from, n);
1645}
1646
3f2571c1
JK
1647/*
1648 * Move xattr pointed to by 'entry' from inode into external xattr block
1649 */
1650static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1651 struct ext4_inode *raw_inode,
1652 struct ext4_xattr_entry *entry)
1653{
1654 struct ext4_xattr_ibody_find *is = NULL;
1655 struct ext4_xattr_block_find *bs = NULL;
1656 char *buffer = NULL, *b_entry_name = NULL;
1657 size_t value_offs, value_size;
1658 struct ext4_xattr_info i = {
1659 .value = NULL,
1660 .value_len = 0,
1661 .name_index = entry->e_name_index,
1662 };
1663 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1664 int error;
1665
1666 value_offs = le16_to_cpu(entry->e_value_offs);
1667 value_size = le32_to_cpu(entry->e_value_size);
1668
1669 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1670 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1671 buffer = kmalloc(value_size, GFP_NOFS);
1672 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1673 if (!is || !bs || !buffer || !b_entry_name) {
1674 error = -ENOMEM;
1675 goto out;
1676 }
1677
1678 is->s.not_found = -ENODATA;
1679 bs->s.not_found = -ENODATA;
1680 is->iloc.bh = NULL;
1681 bs->bh = NULL;
1682
1683 /* Save the entry name and the entry value */
1684 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1685 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1686 b_entry_name[entry->e_name_len] = '\0';
1687 i.name = b_entry_name;
1688
1689 error = ext4_get_inode_loc(inode, &is->iloc);
1690 if (error)
1691 goto out;
1692
1693 error = ext4_xattr_ibody_find(inode, &i, is);
1694 if (error)
1695 goto out;
1696
1697 /* Remove the chosen entry from the inode */
e50e5129 1698 error = ext4_xattr_ibody_set(handle, inode, &i, is);
3f2571c1
JK
1699 if (error)
1700 goto out;
1701
1702 i.name = b_entry_name;
1703 i.value = buffer;
1704 i.value_len = value_size;
1705 error = ext4_xattr_block_find(inode, &i, bs);
1706 if (error)
1707 goto out;
1708
1709 /* Add entry which was removed from the inode into the block */
1710 error = ext4_xattr_block_set(handle, inode, &i, bs);
1711 if (error)
1712 goto out;
1713 error = 0;
1714out:
1715 kfree(b_entry_name);
1716 kfree(buffer);
1717 if (is)
1718 brelse(is->iloc.bh);
1719 kfree(is);
1720 kfree(bs);
1721
1722 return error;
1723}
1724
dfa2064b
JK
1725static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1726 struct ext4_inode *raw_inode,
1727 int isize_diff, size_t ifree,
1728 size_t bfree, int *total_ino)
1729{
1730 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1731 struct ext4_xattr_entry *small_entry;
1732 struct ext4_xattr_entry *entry;
1733 struct ext4_xattr_entry *last;
1734 unsigned int entry_size; /* EA entry size */
1735 unsigned int total_size; /* EA entry size + value size */
1736 unsigned int min_total_size;
1737 int error;
1738
1739 while (isize_diff > ifree) {
1740 entry = NULL;
1741 small_entry = NULL;
1742 min_total_size = ~0U;
1743 last = IFIRST(header);
1744 /* Find the entry best suited to be pushed into EA block */
1745 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1746 total_size =
1747 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1748 EXT4_XATTR_LEN(last->e_name_len);
1749 if (total_size <= bfree &&
1750 total_size < min_total_size) {
1751 if (total_size + ifree < isize_diff) {
1752 small_entry = last;
1753 } else {
1754 entry = last;
1755 min_total_size = total_size;
1756 }
1757 }
1758 }
1759
1760 if (entry == NULL) {
1761 if (small_entry == NULL)
1762 return -ENOSPC;
1763 entry = small_entry;
1764 }
1765
1766 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1767 total_size = entry_size +
1768 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1769 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1770 entry);
1771 if (error)
1772 return error;
1773
1774 *total_ino -= entry_size;
1775 ifree += total_size;
1776 bfree -= total_size;
1777 }
1778
1779 return 0;
1780}
1781
6dd4ee7c
KS
1782/*
1783 * Expand an inode by new_extra_isize bytes when EAs are present.
1784 * Returns 0 on success or negative error number on failure.
1785 */
1786int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1787 struct ext4_inode *raw_inode, handle_t *handle)
1788{
1789 struct ext4_xattr_ibody_header *header;
6dd4ee7c 1790 struct buffer_head *bh = NULL;
e3014d14
JK
1791 size_t min_offs;
1792 size_t ifree, bfree;
7b1b2c1b 1793 int total_ino;
6e0cd088 1794 void *base, *end;
d0141191 1795 int error = 0, tried_min_extra_isize = 0;
ac39849d 1796 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
d0141191 1797 int isize_diff; /* How much do we need to grow i_extra_isize */
c755e251
TT
1798 int no_expand;
1799
1800 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1801 return 0;
6dd4ee7c 1802
6dd4ee7c 1803retry:
d0141191 1804 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
2e81a4ee
JK
1805 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1806 goto out;
6dd4ee7c
KS
1807
1808 header = IHDR(inode, raw_inode);
6dd4ee7c
KS
1809
1810 /*
1811 * Check if enough free space is available in the inode to shift the
1812 * entries ahead by new_extra_isize.
1813 */
1814
6e0cd088 1815 base = IFIRST(header);
6dd4ee7c
KS
1816 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1817 min_offs = end - base;
6dd4ee7c
KS
1818 total_ino = sizeof(struct ext4_xattr_ibody_header);
1819
9e92f48c
TT
1820 error = xattr_check_inode(inode, header, end);
1821 if (error)
1822 goto cleanup;
1823
6e0cd088 1824 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
e3014d14
JK
1825 if (ifree >= isize_diff)
1826 goto shift;
6dd4ee7c
KS
1827
1828 /*
1829 * Enough free space isn't available in the inode, check if
1830 * EA block can hold new_extra_isize bytes.
1831 */
1832 if (EXT4_I(inode)->i_file_acl) {
1833 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1834 error = -EIO;
1835 if (!bh)
1836 goto cleanup;
cc8e94fd 1837 if (ext4_xattr_check_block(inode, bh)) {
24676da4
TT
1838 EXT4_ERROR_INODE(inode, "bad block %llu",
1839 EXT4_I(inode)->i_file_acl);
6a797d27 1840 error = -EFSCORRUPTED;
6dd4ee7c
KS
1841 goto cleanup;
1842 }
1843 base = BHDR(bh);
6dd4ee7c
KS
1844 end = bh->b_data + bh->b_size;
1845 min_offs = end - base;
6e0cd088
JK
1846 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1847 NULL);
e3014d14 1848 if (bfree + ifree < isize_diff) {
6dd4ee7c
KS
1849 if (!tried_min_extra_isize && s_min_extra_isize) {
1850 tried_min_extra_isize++;
1851 new_extra_isize = s_min_extra_isize;
1852 brelse(bh);
1853 goto retry;
1854 }
dfa2064b 1855 error = -ENOSPC;
6dd4ee7c
KS
1856 goto cleanup;
1857 }
1858 } else {
e3014d14 1859 bfree = inode->i_sb->s_blocksize;
6dd4ee7c
KS
1860 }
1861
dfa2064b
JK
1862 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1863 isize_diff, ifree, bfree,
1864 &total_ino);
1865 if (error) {
1866 if (error == -ENOSPC && !tried_min_extra_isize &&
1867 s_min_extra_isize) {
1868 tried_min_extra_isize++;
1869 new_extra_isize = s_min_extra_isize;
1870 brelse(bh);
1871 goto retry;
6dd4ee7c 1872 }
dfa2064b 1873 goto cleanup;
6dd4ee7c 1874 }
e3014d14
JK
1875shift:
1876 /* Adjust the offsets and shift the remaining entries ahead */
6e0cd088 1877 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
e3014d14
JK
1878 - new_extra_isize, (void *)raw_inode +
1879 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
94405713 1880 (void *)header, total_ino);
e3014d14 1881 EXT4_I(inode)->i_extra_isize = new_extra_isize;
6dd4ee7c 1882 brelse(bh);
2e81a4ee 1883out:
c755e251 1884 ext4_write_unlock_xattr(inode, &no_expand);
6dd4ee7c
KS
1885 return 0;
1886
1887cleanup:
6dd4ee7c 1888 brelse(bh);
2e81a4ee 1889 /*
c755e251 1890 * Inode size expansion failed; don't try again
2e81a4ee 1891 */
c755e251
TT
1892 no_expand = 1;
1893 ext4_write_unlock_xattr(inode, &no_expand);
6dd4ee7c
KS
1894 return error;
1895}
1896
1897
e50e5129
AD
1898#define EIA_INCR 16 /* must be 2^n */
1899#define EIA_MASK (EIA_INCR - 1)
1900/* Add the large xattr @ino into @lea_ino_array for later deletion.
1901 * If @lea_ino_array is new or full it will be grown and the old
1902 * contents copied over.
1903 */
1904static int
1905ext4_expand_ino_array(struct ext4_xattr_ino_array **lea_ino_array, __u32 ino)
1906{
1907 if (*lea_ino_array == NULL) {
1908 /*
1909 * Start with 15 inodes, so it fits into a power-of-two size.
1910 * If *lea_ino_array is NULL, this is essentially offsetof()
1911 */
1912 (*lea_ino_array) =
1913 kmalloc(offsetof(struct ext4_xattr_ino_array,
1914 xia_inodes[EIA_MASK]),
1915 GFP_NOFS);
1916 if (*lea_ino_array == NULL)
1917 return -ENOMEM;
1918 (*lea_ino_array)->xia_count = 0;
1919 } else if (((*lea_ino_array)->xia_count & EIA_MASK) == EIA_MASK) {
1920 /* expand the array once all 15 + n * 16 slots are full */
1921 struct ext4_xattr_ino_array *new_array = NULL;
1922 int count = (*lea_ino_array)->xia_count;
1923
1924 /* if new_array is NULL, this is essentially offsetof() */
1925 new_array = kmalloc(
1926 offsetof(struct ext4_xattr_ino_array,
1927 xia_inodes[count + EIA_INCR]),
1928 GFP_NOFS);
1929 if (new_array == NULL)
1930 return -ENOMEM;
1931 memcpy(new_array, *lea_ino_array,
1932 offsetof(struct ext4_xattr_ino_array,
1933 xia_inodes[count]));
1934 kfree(*lea_ino_array);
1935 *lea_ino_array = new_array;
1936 }
1937 (*lea_ino_array)->xia_inodes[(*lea_ino_array)->xia_count++] = ino;
1938 return 0;
1939}
1940
1941/**
1942 * Add xattr inode to orphan list
1943 */
1944static int
1945ext4_xattr_inode_orphan_add(handle_t *handle, struct inode *inode,
1946 int credits, struct ext4_xattr_ino_array *lea_ino_array)
1947{
1948 struct inode *ea_inode = NULL;
1949 int idx = 0, error = 0;
1950
1951 if (lea_ino_array == NULL)
1952 return 0;
1953
1954 for (; idx < lea_ino_array->xia_count; ++idx) {
1955 if (!ext4_handle_has_enough_credits(handle, credits)) {
1956 error = ext4_journal_extend(handle, credits);
1957 if (error > 0)
1958 error = ext4_journal_restart(handle, credits);
1959
1960 if (error != 0) {
1961 ext4_warning(inode->i_sb,
1962 "couldn't extend journal "
1963 "(err %d)", error);
1964 return error;
1965 }
1966 }
1967 ea_inode = ext4_xattr_inode_iget(inode,
1968 lea_ino_array->xia_inodes[idx], &error);
1969 if (error)
1970 continue;
0de5983d 1971 inode_lock(ea_inode);
e50e5129 1972 ext4_orphan_add(handle, ea_inode);
0de5983d 1973 inode_unlock(ea_inode);
e50e5129
AD
1974 /* the inode's i_count will be released by caller */
1975 }
1976
1977 return 0;
1978}
6dd4ee7c 1979
ac27a0ec 1980/*
617ba13b 1981 * ext4_xattr_delete_inode()
ac27a0ec 1982 *
e50e5129
AD
1983 * Free extended attribute resources associated with this inode. Traverse
1984 * all entries and unlink any xattr inodes associated with this inode. This
ac27a0ec 1985 * is called immediately before an inode is freed. We have exclusive
e50e5129
AD
1986 * access to the inode. If an orphan inode is deleted it will also delete any
1987 * xattr block and all xattr inodes. They are checked by ext4_xattr_inode_iget()
1988 * to ensure they belong to the parent inode and were not deleted already.
ac27a0ec 1989 */
e50e5129
AD
1990int
1991ext4_xattr_delete_inode(handle_t *handle, struct inode *inode,
1992 struct ext4_xattr_ino_array **lea_ino_array)
ac27a0ec
DK
1993{
1994 struct buffer_head *bh = NULL;
e50e5129
AD
1995 struct ext4_xattr_ibody_header *header;
1996 struct ext4_inode *raw_inode;
1997 struct ext4_iloc iloc;
1998 struct ext4_xattr_entry *entry;
1999 int credits = 3, error = 0;
ac27a0ec 2000
e50e5129
AD
2001 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
2002 goto delete_external_ea;
2003
2004 error = ext4_get_inode_loc(inode, &iloc);
2005 if (error)
2006 goto cleanup;
2007 raw_inode = ext4_raw_inode(&iloc);
2008 header = IHDR(inode, raw_inode);
2009 for (entry = IFIRST(header); !IS_LAST_ENTRY(entry);
2010 entry = EXT4_XATTR_NEXT(entry)) {
2011 if (!entry->e_value_inum)
2012 continue;
2013 if (ext4_expand_ino_array(lea_ino_array,
2014 entry->e_value_inum) != 0) {
2015 brelse(iloc.bh);
2016 goto cleanup;
2017 }
2018 entry->e_value_inum = 0;
2019 }
2020 brelse(iloc.bh);
2021
2022delete_external_ea:
2023 if (!EXT4_I(inode)->i_file_acl) {
2024 /* add xattr inode to orphan list */
2025 ext4_xattr_inode_orphan_add(handle, inode, credits,
2026 *lea_ino_array);
ac27a0ec 2027 goto cleanup;
e50e5129 2028 }
617ba13b 2029 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
ac27a0ec 2030 if (!bh) {
24676da4
TT
2031 EXT4_ERROR_INODE(inode, "block %llu read error",
2032 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
2033 goto cleanup;
2034 }
617ba13b 2035 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
ac27a0ec 2036 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
24676da4
TT
2037 EXT4_ERROR_INODE(inode, "bad block %llu",
2038 EXT4_I(inode)->i_file_acl);
ac27a0ec
DK
2039 goto cleanup;
2040 }
e50e5129
AD
2041
2042 for (entry = BFIRST(bh); !IS_LAST_ENTRY(entry);
2043 entry = EXT4_XATTR_NEXT(entry)) {
2044 if (!entry->e_value_inum)
2045 continue;
2046 if (ext4_expand_ino_array(lea_ino_array,
2047 entry->e_value_inum) != 0)
2048 goto cleanup;
2049 entry->e_value_inum = 0;
2050 }
2051
2052 /* add xattr inode to orphan list */
2053 error = ext4_xattr_inode_orphan_add(handle, inode, credits,
2054 *lea_ino_array);
2055 if (error != 0)
2056 goto cleanup;
2057
2058 if (!IS_NOQUOTA(inode))
2059 credits += 2 * EXT4_QUOTA_DEL_BLOCKS(inode->i_sb);
2060
2061 if (!ext4_handle_has_enough_credits(handle, credits)) {
2062 error = ext4_journal_extend(handle, credits);
2063 if (error > 0)
2064 error = ext4_journal_restart(handle, credits);
2065 if (error != 0) {
2066 ext4_warning(inode->i_sb,
2067 "couldn't extend journal (err %d)", error);
2068 goto cleanup;
2069 }
2070 }
2071
617ba13b
MC
2072 ext4_xattr_release_block(handle, inode, bh);
2073 EXT4_I(inode)->i_file_acl = 0;
ac27a0ec
DK
2074
2075cleanup:
2076 brelse(bh);
e50e5129
AD
2077
2078 return error;
2079}
2080
2081void
2082ext4_xattr_inode_array_free(struct inode *inode,
2083 struct ext4_xattr_ino_array *lea_ino_array)
2084{
2085 struct inode *ea_inode = NULL;
2086 int idx = 0;
2087 int err;
2088
2089 if (lea_ino_array == NULL)
2090 return;
2091
2092 for (; idx < lea_ino_array->xia_count; ++idx) {
2093 ea_inode = ext4_xattr_inode_iget(inode,
2094 lea_ino_array->xia_inodes[idx], &err);
2095 if (err)
2096 continue;
2097 /* for inode's i_count get from ext4_xattr_delete_inode */
2098 if (!list_empty(&EXT4_I(ea_inode)->i_orphan))
2099 iput(ea_inode);
2100 clear_nlink(ea_inode);
2101 iput(ea_inode);
2102 }
2103 kfree(lea_ino_array);
ac27a0ec
DK
2104}
2105
ac27a0ec 2106/*
617ba13b 2107 * ext4_xattr_cache_insert()
ac27a0ec
DK
2108 *
2109 * Create a new entry in the extended attribute cache, and insert
2110 * it unless such an entry is already in the cache.
2111 *
2112 * Returns 0, or a negative error number on failure.
2113 */
2114static void
7a2508e1 2115ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
ac27a0ec 2116{
6048c64b
AG
2117 struct ext4_xattr_header *header = BHDR(bh);
2118 __u32 hash = le32_to_cpu(header->h_hash);
2119 int reusable = le32_to_cpu(header->h_refcount) <
2120 EXT4_XATTR_REFCOUNT_MAX;
ac27a0ec
DK
2121 int error;
2122
7a2508e1 2123 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
6048c64b 2124 bh->b_blocknr, reusable);
ac27a0ec 2125 if (error) {
82939d79 2126 if (error == -EBUSY)
ac27a0ec 2127 ea_bdebug(bh, "already in cache");
82939d79 2128 } else
ac27a0ec 2129 ea_bdebug(bh, "inserting [%x]", (int)hash);
ac27a0ec
DK
2130}
2131
2132/*
617ba13b 2133 * ext4_xattr_cmp()
ac27a0ec
DK
2134 *
2135 * Compare two extended attribute blocks for equality.
2136 *
2137 * Returns 0 if the blocks are equal, 1 if they differ, and
2138 * a negative error number on errors.
2139 */
2140static int
617ba13b
MC
2141ext4_xattr_cmp(struct ext4_xattr_header *header1,
2142 struct ext4_xattr_header *header2)
ac27a0ec 2143{
617ba13b 2144 struct ext4_xattr_entry *entry1, *entry2;
ac27a0ec
DK
2145
2146 entry1 = ENTRY(header1+1);
2147 entry2 = ENTRY(header2+1);
2148 while (!IS_LAST_ENTRY(entry1)) {
2149 if (IS_LAST_ENTRY(entry2))
2150 return 1;
2151 if (entry1->e_hash != entry2->e_hash ||
2152 entry1->e_name_index != entry2->e_name_index ||
2153 entry1->e_name_len != entry2->e_name_len ||
2154 entry1->e_value_size != entry2->e_value_size ||
e50e5129 2155 entry1->e_value_inum != entry2->e_value_inum ||
ac27a0ec
DK
2156 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
2157 return 1;
ac27a0ec
DK
2158 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
2159 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
2160 le32_to_cpu(entry1->e_value_size)))
2161 return 1;
2162
617ba13b
MC
2163 entry1 = EXT4_XATTR_NEXT(entry1);
2164 entry2 = EXT4_XATTR_NEXT(entry2);
ac27a0ec
DK
2165 }
2166 if (!IS_LAST_ENTRY(entry2))
2167 return 1;
2168 return 0;
2169}
2170
2171/*
617ba13b 2172 * ext4_xattr_cache_find()
ac27a0ec
DK
2173 *
2174 * Find an identical extended attribute block.
2175 *
2176 * Returns a pointer to the block found, or NULL if such a block was
2177 * not found or an error occurred.
2178 */
2179static struct buffer_head *
617ba13b 2180ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
7a2508e1 2181 struct mb_cache_entry **pce)
ac27a0ec
DK
2182{
2183 __u32 hash = le32_to_cpu(header->h_hash);
7a2508e1
JK
2184 struct mb_cache_entry *ce;
2185 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
ac27a0ec
DK
2186
2187 if (!header->h_hash)
2188 return NULL; /* never share */
2189 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
7a2508e1 2190 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
ac27a0ec
DK
2191 while (ce) {
2192 struct buffer_head *bh;
2193
ac27a0ec
DK
2194 bh = sb_bread(inode->i_sb, ce->e_block);
2195 if (!bh) {
24676da4
TT
2196 EXT4_ERROR_INODE(inode, "block %lu read error",
2197 (unsigned long) ce->e_block);
617ba13b 2198 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
ac27a0ec
DK
2199 *pce = ce;
2200 return bh;
2201 }
2202 brelse(bh);
7a2508e1 2203 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
ac27a0ec
DK
2204 }
2205 return NULL;
2206}
2207
2208#define NAME_HASH_SHIFT 5
2209#define VALUE_HASH_SHIFT 16
2210
2211/*
617ba13b 2212 * ext4_xattr_hash_entry()
ac27a0ec
DK
2213 *
2214 * Compute the hash of an extended attribute.
2215 */
617ba13b
MC
2216static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
2217 struct ext4_xattr_entry *entry)
ac27a0ec
DK
2218{
2219 __u32 hash = 0;
2220 char *name = entry->e_name;
2221 int n;
2222
2b2d6d01 2223 for (n = 0; n < entry->e_name_len; n++) {
ac27a0ec
DK
2224 hash = (hash << NAME_HASH_SHIFT) ^
2225 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
2226 *name++;
2227 }
2228
e50e5129 2229 if (!entry->e_value_inum && entry->e_value_size) {
ac27a0ec
DK
2230 __le32 *value = (__le32 *)((char *)header +
2231 le16_to_cpu(entry->e_value_offs));
2232 for (n = (le32_to_cpu(entry->e_value_size) +
617ba13b 2233 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
ac27a0ec
DK
2234 hash = (hash << VALUE_HASH_SHIFT) ^
2235 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
2236 le32_to_cpu(*value++);
2237 }
2238 }
2239 entry->e_hash = cpu_to_le32(hash);
2240}
2241
2242#undef NAME_HASH_SHIFT
2243#undef VALUE_HASH_SHIFT
2244
2245#define BLOCK_HASH_SHIFT 16
2246
2247/*
617ba13b 2248 * ext4_xattr_rehash()
ac27a0ec
DK
2249 *
2250 * Re-compute the extended attribute hash value after an entry has changed.
2251 */
617ba13b
MC
2252static void ext4_xattr_rehash(struct ext4_xattr_header *header,
2253 struct ext4_xattr_entry *entry)
ac27a0ec 2254{
617ba13b 2255 struct ext4_xattr_entry *here;
ac27a0ec
DK
2256 __u32 hash = 0;
2257
617ba13b 2258 ext4_xattr_hash_entry(header, entry);
ac27a0ec
DK
2259 here = ENTRY(header+1);
2260 while (!IS_LAST_ENTRY(here)) {
2261 if (!here->e_hash) {
2262 /* Block is not shared if an entry's hash value == 0 */
2263 hash = 0;
2264 break;
2265 }
2266 hash = (hash << BLOCK_HASH_SHIFT) ^
2267 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
2268 le32_to_cpu(here->e_hash);
617ba13b 2269 here = EXT4_XATTR_NEXT(here);
ac27a0ec
DK
2270 }
2271 header->h_hash = cpu_to_le32(hash);
2272}
2273
2274#undef BLOCK_HASH_SHIFT
2275
9c191f70
M
2276#define HASH_BUCKET_BITS 10
2277
7a2508e1 2278struct mb_cache *
82939d79 2279ext4_xattr_create_cache(void)
ac27a0ec 2280{
7a2508e1 2281 return mb_cache_create(HASH_BUCKET_BITS);
ac27a0ec
DK
2282}
2283
7a2508e1 2284void ext4_xattr_destroy_cache(struct mb_cache *cache)
ac27a0ec 2285{
9c191f70 2286 if (cache)
7a2508e1 2287 mb_cache_destroy(cache);
ac27a0ec 2288}
9c191f70 2289