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