2 * linux/fs/ext4/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
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>,
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
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.
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
28 * +------------------+
31 * | entry 2 | | growing downwards
36 * | value 3 | | growing upwards
38 * +------------------+
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.
46 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
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
53 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/mbcache.h>
57 #include <linux/quotaops.h>
58 #include <linux/rwsem.h>
59 #include "ext4_jbd2.h"
64 #ifdef EXT4_XATTR_DEBUG
65 # define ea_idebug(inode, f...) do { \
66 printk(KERN_DEBUG "inode %s:%lu: ", \
67 inode->i_sb->s_id, inode->i_ino); \
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); \
80 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
81 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
84 static void ext4_xattr_cache_insert(struct buffer_head *);
85 static struct buffer_head *ext4_xattr_cache_find(struct inode *,
86 struct ext4_xattr_header *,
87 struct mb_cache_entry **);
88 static void ext4_xattr_rehash(struct ext4_xattr_header *,
89 struct ext4_xattr_entry *);
90 static int ext4_xattr_list(struct dentry *dentry, char *buffer,
93 static struct mb_cache *ext4_xattr_cache;
95 static const struct xattr_handler *ext4_xattr_handler_map[] = {
96 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
97 #ifdef CONFIG_EXT4_FS_POSIX_ACL
98 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
99 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
101 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
102 #ifdef CONFIG_EXT4_FS_SECURITY
103 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
107 const struct xattr_handler *ext4_xattr_handlers[] = {
108 &ext4_xattr_user_handler,
109 &ext4_xattr_trusted_handler,
110 #ifdef CONFIG_EXT4_FS_POSIX_ACL
111 &ext4_xattr_acl_access_handler,
112 &ext4_xattr_acl_default_handler,
114 #ifdef CONFIG_EXT4_FS_SECURITY
115 &ext4_xattr_security_handler,
120 static __le32 ext4_xattr_block_csum(struct inode *inode,
122 struct ext4_xattr_header *hdr)
124 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
127 __le64 dsk_block_nr = cpu_to_le64(block_nr);
129 save_csum = hdr->h_checksum;
131 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
132 sizeof(dsk_block_nr));
133 csum = ext4_chksum(sbi, csum, (__u8 *)hdr,
134 EXT4_BLOCK_SIZE(inode->i_sb));
136 hdr->h_checksum = save_csum;
137 return cpu_to_le32(csum);
140 static int ext4_xattr_block_csum_verify(struct inode *inode,
142 struct ext4_xattr_header *hdr)
144 if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
145 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
146 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
151 static void ext4_xattr_block_csum_set(struct inode *inode,
153 struct ext4_xattr_header *hdr)
155 if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
156 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
159 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
162 static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
164 struct buffer_head *bh)
166 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
167 return ext4_handle_dirty_metadata(handle, inode, bh);
170 static inline const struct xattr_handler *
171 ext4_xattr_handler(int name_index)
173 const struct xattr_handler *handler = NULL;
175 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
176 handler = ext4_xattr_handler_map[name_index];
181 * Inode operation listxattr()
183 * dentry->d_inode->i_mutex: don't care
186 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
188 return ext4_xattr_list(dentry, buffer, size);
192 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
195 struct ext4_xattr_entry *e = entry;
197 while (!IS_LAST_ENTRY(e)) {
198 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
199 if ((void *)next >= end)
204 while (!IS_LAST_ENTRY(entry)) {
205 if (entry->e_value_size != 0 &&
206 (value_start + le16_to_cpu(entry->e_value_offs) <
207 (void *)e + sizeof(__u32) ||
208 value_start + le16_to_cpu(entry->e_value_offs) +
209 le32_to_cpu(entry->e_value_size) > end))
211 entry = EXT4_XATTR_NEXT(entry);
218 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
222 if (buffer_verified(bh))
225 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
226 BHDR(bh)->h_blocks != cpu_to_le32(1))
228 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
230 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
233 set_buffer_verified(bh);
238 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
240 size_t value_size = le32_to_cpu(entry->e_value_size);
242 if (entry->e_value_block != 0 || value_size > size ||
243 le16_to_cpu(entry->e_value_offs) + value_size > size)
249 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
250 const char *name, size_t size, int sorted)
252 struct ext4_xattr_entry *entry;
258 name_len = strlen(name);
260 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
261 cmp = name_index - entry->e_name_index;
263 cmp = name_len - entry->e_name_len;
265 cmp = memcmp(name, entry->e_name, name_len);
266 if (cmp <= 0 && (sorted || cmp == 0))
270 if (!cmp && ext4_xattr_check_entry(entry, size))
272 return cmp ? -ENODATA : 0;
276 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
277 void *buffer, size_t buffer_size)
279 struct buffer_head *bh = NULL;
280 struct ext4_xattr_entry *entry;
284 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
285 name_index, name, buffer, (long)buffer_size);
288 if (!EXT4_I(inode)->i_file_acl)
290 ea_idebug(inode, "reading block %llu",
291 (unsigned long long)EXT4_I(inode)->i_file_acl);
292 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
295 ea_bdebug(bh, "b_count=%d, refcount=%d",
296 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
297 if (ext4_xattr_check_block(inode, bh)) {
299 EXT4_ERROR_INODE(inode, "bad block %llu",
300 EXT4_I(inode)->i_file_acl);
304 ext4_xattr_cache_insert(bh);
306 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
311 size = le32_to_cpu(entry->e_value_size);
314 if (size > buffer_size)
316 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
327 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
328 void *buffer, size_t buffer_size)
330 struct ext4_xattr_ibody_header *header;
331 struct ext4_xattr_entry *entry;
332 struct ext4_inode *raw_inode;
333 struct ext4_iloc iloc;
338 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
340 error = ext4_get_inode_loc(inode, &iloc);
343 raw_inode = ext4_raw_inode(&iloc);
344 header = IHDR(inode, raw_inode);
345 entry = IFIRST(header);
346 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
347 error = ext4_xattr_check_names(entry, end, entry);
350 error = ext4_xattr_find_entry(&entry, name_index, name,
351 end - (void *)entry, 0);
354 size = le32_to_cpu(entry->e_value_size);
357 if (size > buffer_size)
359 memcpy(buffer, (void *)IFIRST(header) +
360 le16_to_cpu(entry->e_value_offs), size);
372 * Copy an extended attribute into the buffer
373 * provided, or compute the buffer size required.
374 * Buffer is NULL to compute the size of the buffer required.
376 * Returns a negative error number on failure, or the number of bytes
377 * used / required on success.
380 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
381 void *buffer, size_t buffer_size)
385 down_read(&EXT4_I(inode)->xattr_sem);
386 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
388 if (error == -ENODATA)
389 error = ext4_xattr_block_get(inode, name_index, name, buffer,
391 up_read(&EXT4_I(inode)->xattr_sem);
396 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
397 char *buffer, size_t buffer_size)
399 size_t rest = buffer_size;
401 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
402 const struct xattr_handler *handler =
403 ext4_xattr_handler(entry->e_name_index);
406 size_t size = handler->list(dentry, buffer, rest,
418 return buffer_size - rest;
422 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
424 struct inode *inode = dentry->d_inode;
425 struct buffer_head *bh = NULL;
428 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
429 buffer, (long)buffer_size);
432 if (!EXT4_I(inode)->i_file_acl)
434 ea_idebug(inode, "reading block %llu",
435 (unsigned long long)EXT4_I(inode)->i_file_acl);
436 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
440 ea_bdebug(bh, "b_count=%d, refcount=%d",
441 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
442 if (ext4_xattr_check_block(inode, bh)) {
443 EXT4_ERROR_INODE(inode, "bad block %llu",
444 EXT4_I(inode)->i_file_acl);
448 ext4_xattr_cache_insert(bh);
449 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
458 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
460 struct inode *inode = dentry->d_inode;
461 struct ext4_xattr_ibody_header *header;
462 struct ext4_inode *raw_inode;
463 struct ext4_iloc iloc;
467 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
469 error = ext4_get_inode_loc(inode, &iloc);
472 raw_inode = ext4_raw_inode(&iloc);
473 header = IHDR(inode, raw_inode);
474 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
475 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
478 error = ext4_xattr_list_entries(dentry, IFIRST(header),
479 buffer, buffer_size);
489 * Copy a list of attribute names into the buffer
490 * provided, or compute the buffer size required.
491 * Buffer is NULL to compute the size of the buffer required.
493 * Returns a negative error number on failure, or the number of bytes
494 * used / required on success.
497 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
501 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
502 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
509 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
514 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
519 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
522 static void ext4_xattr_update_super_block(handle_t *handle,
523 struct super_block *sb)
525 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
528 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
529 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
530 ext4_handle_dirty_super(handle, sb);
535 * Release the xattr block BH: If the reference count is > 1, decrement it;
536 * otherwise free the block.
539 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
540 struct buffer_head *bh)
542 struct mb_cache_entry *ce = NULL;
545 ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
546 error = ext4_journal_get_write_access(handle, bh);
551 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
552 ea_bdebug(bh, "refcount now=0; freeing");
554 mb_cache_entry_free(ce);
557 ext4_free_blocks(handle, inode, bh, 0, 1,
558 EXT4_FREE_BLOCKS_METADATA |
559 EXT4_FREE_BLOCKS_FORGET);
561 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
563 mb_cache_entry_release(ce);
565 * Beware of this ugliness: Releasing of xattr block references
566 * from different inodes can race and so we have to protect
567 * from a race where someone else frees the block (and releases
568 * its journal_head) before we are done dirtying the buffer. In
569 * nojournal mode this race is harmless and we actually cannot
570 * call ext4_handle_dirty_xattr_block() with locked buffer as
571 * that function can call sync_dirty_buffer() so for that case
572 * we handle the dirtying after unlocking the buffer.
574 if (ext4_handle_valid(handle))
575 error = ext4_handle_dirty_xattr_block(handle, inode,
578 if (!ext4_handle_valid(handle))
579 error = ext4_handle_dirty_xattr_block(handle, inode,
582 ext4_handle_sync(handle);
583 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
584 ea_bdebug(bh, "refcount now=%d; releasing",
585 le32_to_cpu(BHDR(bh)->h_refcount));
588 ext4_std_error(inode->i_sb, error);
593 * Find the available free space for EAs. This also returns the total number of
594 * bytes used by EA entries.
596 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
597 size_t *min_offs, void *base, int *total)
599 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
600 *total += EXT4_XATTR_LEN(last->e_name_len);
601 if (!last->e_value_block && last->e_value_size) {
602 size_t offs = le16_to_cpu(last->e_value_offs);
603 if (offs < *min_offs)
607 return (*min_offs - ((void *)last - base) - sizeof(__u32));
611 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
613 struct ext4_xattr_entry *last;
614 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
616 /* Compute min_offs and last. */
618 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
619 if (!last->e_value_block && last->e_value_size) {
620 size_t offs = le16_to_cpu(last->e_value_offs);
625 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
627 if (!s->here->e_value_block && s->here->e_value_size) {
628 size_t size = le32_to_cpu(s->here->e_value_size);
629 free += EXT4_XATTR_SIZE(size);
631 free += EXT4_XATTR_LEN(name_len);
634 if (free < EXT4_XATTR_SIZE(i->value_len) ||
635 free < EXT4_XATTR_LEN(name_len) +
636 EXT4_XATTR_SIZE(i->value_len))
640 if (i->value && s->not_found) {
641 /* Insert the new name. */
642 size_t size = EXT4_XATTR_LEN(name_len);
643 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
644 memmove((void *)s->here + size, s->here, rest);
645 memset(s->here, 0, size);
646 s->here->e_name_index = i->name_index;
647 s->here->e_name_len = name_len;
648 memcpy(s->here->e_name, i->name, name_len);
650 if (!s->here->e_value_block && s->here->e_value_size) {
651 void *first_val = s->base + min_offs;
652 size_t offs = le16_to_cpu(s->here->e_value_offs);
653 void *val = s->base + offs;
654 size_t size = EXT4_XATTR_SIZE(
655 le32_to_cpu(s->here->e_value_size));
657 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
658 /* The old and the new value have the same
659 size. Just replace. */
660 s->here->e_value_size =
661 cpu_to_le32(i->value_len);
662 if (i->value == EXT4_ZERO_XATTR_VALUE) {
663 memset(val, 0, size);
665 /* Clear pad bytes first. */
666 memset(val + size - EXT4_XATTR_PAD, 0,
668 memcpy(val, i->value, i->value_len);
673 /* Remove the old value. */
674 memmove(first_val + size, first_val, val - first_val);
675 memset(first_val, 0, size);
676 s->here->e_value_size = 0;
677 s->here->e_value_offs = 0;
680 /* Adjust all value offsets. */
682 while (!IS_LAST_ENTRY(last)) {
683 size_t o = le16_to_cpu(last->e_value_offs);
684 if (!last->e_value_block &&
685 last->e_value_size && o < offs)
687 cpu_to_le16(o + size);
688 last = EXT4_XATTR_NEXT(last);
692 /* Remove the old name. */
693 size_t size = EXT4_XATTR_LEN(name_len);
694 last = ENTRY((void *)last - size);
695 memmove(s->here, (void *)s->here + size,
696 (void *)last - (void *)s->here + sizeof(__u32));
697 memset(last, 0, size);
702 /* Insert the new value. */
703 s->here->e_value_size = cpu_to_le32(i->value_len);
705 size_t size = EXT4_XATTR_SIZE(i->value_len);
706 void *val = s->base + min_offs - size;
707 s->here->e_value_offs = cpu_to_le16(min_offs - size);
708 if (i->value == EXT4_ZERO_XATTR_VALUE) {
709 memset(val, 0, size);
711 /* Clear the pad bytes first. */
712 memset(val + size - EXT4_XATTR_PAD, 0,
714 memcpy(val, i->value, i->value_len);
721 struct ext4_xattr_block_find {
722 struct ext4_xattr_search s;
723 struct buffer_head *bh;
727 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
728 struct ext4_xattr_block_find *bs)
730 struct super_block *sb = inode->i_sb;
733 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
734 i->name_index, i->name, i->value, (long)i->value_len);
736 if (EXT4_I(inode)->i_file_acl) {
737 /* The inode already has an extended attribute block. */
738 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
742 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
743 atomic_read(&(bs->bh->b_count)),
744 le32_to_cpu(BHDR(bs->bh)->h_refcount));
745 if (ext4_xattr_check_block(inode, bs->bh)) {
746 EXT4_ERROR_INODE(inode, "bad block %llu",
747 EXT4_I(inode)->i_file_acl);
751 /* Find the named attribute. */
752 bs->s.base = BHDR(bs->bh);
753 bs->s.first = BFIRST(bs->bh);
754 bs->s.end = bs->bh->b_data + bs->bh->b_size;
755 bs->s.here = bs->s.first;
756 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
757 i->name, bs->bh->b_size, 1);
758 if (error && error != -ENODATA)
760 bs->s.not_found = error;
769 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
770 struct ext4_xattr_info *i,
771 struct ext4_xattr_block_find *bs)
773 struct super_block *sb = inode->i_sb;
774 struct buffer_head *new_bh = NULL;
775 struct ext4_xattr_search *s = &bs->s;
776 struct mb_cache_entry *ce = NULL;
779 #define header(x) ((struct ext4_xattr_header *)(x))
781 if (i->value && i->value_len > sb->s_blocksize)
784 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
786 error = ext4_journal_get_write_access(handle, bs->bh);
791 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
793 mb_cache_entry_free(ce);
796 ea_bdebug(bs->bh, "modifying in-place");
797 error = ext4_xattr_set_entry(i, s);
799 if (!IS_LAST_ENTRY(s->first))
800 ext4_xattr_rehash(header(s->base),
802 ext4_xattr_cache_insert(bs->bh);
804 unlock_buffer(bs->bh);
808 error = ext4_handle_dirty_xattr_block(handle,
815 int offset = (char *)s->here - bs->bh->b_data;
817 unlock_buffer(bs->bh);
819 mb_cache_entry_release(ce);
822 ea_bdebug(bs->bh, "cloning");
823 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
827 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
828 s->first = ENTRY(header(s->base)+1);
829 header(s->base)->h_refcount = cpu_to_le32(1);
830 s->here = ENTRY(s->base + offset);
831 s->end = s->base + bs->bh->b_size;
834 /* Allocate a buffer where we construct the new block. */
835 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
836 /* assert(header == s->base) */
840 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
841 header(s->base)->h_blocks = cpu_to_le32(1);
842 header(s->base)->h_refcount = cpu_to_le32(1);
843 s->first = ENTRY(header(s->base)+1);
844 s->here = ENTRY(header(s->base)+1);
845 s->end = s->base + sb->s_blocksize;
848 error = ext4_xattr_set_entry(i, s);
853 if (!IS_LAST_ENTRY(s->first))
854 ext4_xattr_rehash(header(s->base), s->here);
857 if (!IS_LAST_ENTRY(s->first)) {
858 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
860 /* We found an identical block in the cache. */
861 if (new_bh == bs->bh)
862 ea_bdebug(new_bh, "keeping");
864 /* The old block is released after updating
866 error = dquot_alloc_block(inode,
867 EXT4_C2B(EXT4_SB(sb), 1));
870 error = ext4_journal_get_write_access(handle,
875 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
876 ea_bdebug(new_bh, "reusing; refcount now=%d",
877 le32_to_cpu(BHDR(new_bh)->h_refcount));
878 unlock_buffer(new_bh);
879 error = ext4_handle_dirty_xattr_block(handle,
885 mb_cache_entry_release(ce);
887 } else if (bs->bh && s->base == bs->bh->b_data) {
888 /* We were modifying this block in-place. */
889 ea_bdebug(bs->bh, "keeping this block");
893 /* We need to allocate a new block */
894 ext4_fsblk_t goal, block;
896 goal = ext4_group_first_block_no(sb,
897 EXT4_I(inode)->i_block_group);
899 /* non-extent files can't have physical blocks past 2^32 */
900 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
901 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
904 * take i_data_sem because we will test
905 * i_delalloc_reserved_flag in ext4_mb_new_blocks
907 down_read((&EXT4_I(inode)->i_data_sem));
908 block = ext4_new_meta_blocks(handle, inode, goal, 0,
910 up_read((&EXT4_I(inode)->i_data_sem));
914 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
915 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
917 ea_idebug(inode, "creating block %llu",
918 (unsigned long long)block);
920 new_bh = sb_getblk(sb, block);
921 if (unlikely(!new_bh)) {
924 ext4_free_blocks(handle, inode, NULL, block, 1,
925 EXT4_FREE_BLOCKS_METADATA);
929 error = ext4_journal_get_create_access(handle, new_bh);
931 unlock_buffer(new_bh);
935 memcpy(new_bh->b_data, s->base, new_bh->b_size);
936 set_buffer_uptodate(new_bh);
937 unlock_buffer(new_bh);
938 ext4_xattr_cache_insert(new_bh);
939 error = ext4_handle_dirty_xattr_block(handle,
946 /* Update the inode. */
947 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
949 /* Drop the previous xattr block. */
950 if (bs->bh && bs->bh != new_bh)
951 ext4_xattr_release_block(handle, inode, bs->bh);
956 mb_cache_entry_release(ce);
958 if (!(bs->bh && s->base == bs->bh->b_data))
964 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
968 EXT4_ERROR_INODE(inode, "bad block %llu",
969 EXT4_I(inode)->i_file_acl);
975 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
976 struct ext4_xattr_ibody_find *is)
978 struct ext4_xattr_ibody_header *header;
979 struct ext4_inode *raw_inode;
982 if (EXT4_I(inode)->i_extra_isize == 0)
984 raw_inode = ext4_raw_inode(&is->iloc);
985 header = IHDR(inode, raw_inode);
986 is->s.base = is->s.first = IFIRST(header);
987 is->s.here = is->s.first;
988 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
989 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
990 error = ext4_xattr_check_names(IFIRST(header), is->s.end,
994 /* Find the named attribute. */
995 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
997 (void *)is->s.base, 0);
998 if (error && error != -ENODATA)
1000 is->s.not_found = error;
1005 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1006 struct ext4_xattr_info *i,
1007 struct ext4_xattr_ibody_find *is)
1009 struct ext4_xattr_ibody_header *header;
1010 struct ext4_xattr_search *s = &is->s;
1013 if (EXT4_I(inode)->i_extra_isize == 0)
1015 error = ext4_xattr_set_entry(i, s);
1017 if (error == -ENOSPC &&
1018 ext4_has_inline_data(inode)) {
1019 error = ext4_try_to_evict_inline_data(handle, inode,
1020 EXT4_XATTR_LEN(strlen(i->name) +
1021 EXT4_XATTR_SIZE(i->value_len)));
1024 error = ext4_xattr_ibody_find(inode, i, is);
1027 error = ext4_xattr_set_entry(i, s);
1032 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1033 if (!IS_LAST_ENTRY(s->first)) {
1034 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1035 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1037 header->h_magic = cpu_to_le32(0);
1038 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1043 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1044 struct ext4_xattr_info *i,
1045 struct ext4_xattr_ibody_find *is)
1047 struct ext4_xattr_ibody_header *header;
1048 struct ext4_xattr_search *s = &is->s;
1051 if (EXT4_I(inode)->i_extra_isize == 0)
1053 error = ext4_xattr_set_entry(i, s);
1056 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1057 if (!IS_LAST_ENTRY(s->first)) {
1058 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1059 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1061 header->h_magic = cpu_to_le32(0);
1062 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1068 * ext4_xattr_set_handle()
1070 * Create, replace or remove an extended attribute for this inode. Value
1071 * is NULL to remove an existing extended attribute, and non-NULL to
1072 * either replace an existing extended attribute, or create a new extended
1073 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1074 * specify that an extended attribute must exist and must not exist
1075 * previous to the call, respectively.
1077 * Returns 0, or a negative error number on failure.
1080 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1081 const char *name, const void *value, size_t value_len,
1084 struct ext4_xattr_info i = {
1085 .name_index = name_index,
1088 .value_len = value_len,
1091 struct ext4_xattr_ibody_find is = {
1092 .s = { .not_found = -ENODATA, },
1094 struct ext4_xattr_block_find bs = {
1095 .s = { .not_found = -ENODATA, },
1097 unsigned long no_expand;
1102 if (strlen(name) > 255)
1104 down_write(&EXT4_I(inode)->xattr_sem);
1105 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1106 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1108 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1112 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1113 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1114 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1115 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1118 error = ext4_xattr_ibody_find(inode, &i, &is);
1122 error = ext4_xattr_block_find(inode, &i, &bs);
1125 if (is.s.not_found && bs.s.not_found) {
1127 if (flags & XATTR_REPLACE)
1134 if (flags & XATTR_CREATE)
1138 if (!is.s.not_found)
1139 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1140 else if (!bs.s.not_found)
1141 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1143 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1144 if (!error && !bs.s.not_found) {
1146 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1147 } else if (error == -ENOSPC) {
1148 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1149 error = ext4_xattr_block_find(inode, &i, &bs);
1153 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1156 if (!is.s.not_found) {
1158 error = ext4_xattr_ibody_set(handle, inode, &i,
1164 ext4_xattr_update_super_block(handle, inode->i_sb);
1165 inode->i_ctime = ext4_current_time(inode);
1167 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1168 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1170 * The bh is consumed by ext4_mark_iloc_dirty, even with
1175 ext4_handle_sync(handle);
1182 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1183 up_write(&EXT4_I(inode)->xattr_sem);
1190 * Like ext4_xattr_set_handle, but start from an inode. This extended
1191 * attribute modification is a filesystem transaction by itself.
1193 * Returns 0, or a negative error number on failure.
1196 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1197 const void *value, size_t value_len, int flags)
1200 int error, retries = 0;
1201 int credits = ext4_jbd2_credits_xattr(inode);
1204 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1205 if (IS_ERR(handle)) {
1206 error = PTR_ERR(handle);
1210 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1211 value, value_len, flags);
1212 error2 = ext4_journal_stop(handle);
1213 if (error == -ENOSPC &&
1214 ext4_should_retry_alloc(inode->i_sb, &retries))
1224 * Shift the EA entries in the inode to create space for the increased
1227 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1228 int value_offs_shift, void *to,
1229 void *from, size_t n, int blocksize)
1231 struct ext4_xattr_entry *last = entry;
1234 /* Adjust the value offsets of the entries */
1235 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1236 if (!last->e_value_block && last->e_value_size) {
1237 new_offs = le16_to_cpu(last->e_value_offs) +
1239 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1241 last->e_value_offs = cpu_to_le16(new_offs);
1244 /* Shift the entries by n bytes */
1245 memmove(to, from, n);
1249 * Expand an inode by new_extra_isize bytes when EAs are present.
1250 * Returns 0 on success or negative error number on failure.
1252 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1253 struct ext4_inode *raw_inode, handle_t *handle)
1255 struct ext4_xattr_ibody_header *header;
1256 struct ext4_xattr_entry *entry, *last, *first;
1257 struct buffer_head *bh = NULL;
1258 struct ext4_xattr_ibody_find *is = NULL;
1259 struct ext4_xattr_block_find *bs = NULL;
1260 char *buffer = NULL, *b_entry_name = NULL;
1261 size_t min_offs, free;
1262 int total_ino, total_blk;
1263 void *base, *start, *end;
1264 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1265 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1267 down_write(&EXT4_I(inode)->xattr_sem);
1269 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1270 up_write(&EXT4_I(inode)->xattr_sem);
1274 header = IHDR(inode, raw_inode);
1275 entry = IFIRST(header);
1278 * Check if enough free space is available in the inode to shift the
1279 * entries ahead by new_extra_isize.
1282 base = start = entry;
1283 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1284 min_offs = end - base;
1286 total_ino = sizeof(struct ext4_xattr_ibody_header);
1288 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1289 if (free >= new_extra_isize) {
1290 entry = IFIRST(header);
1291 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1292 - new_extra_isize, (void *)raw_inode +
1293 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1294 (void *)header, total_ino,
1295 inode->i_sb->s_blocksize);
1296 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1302 * Enough free space isn't available in the inode, check if
1303 * EA block can hold new_extra_isize bytes.
1305 if (EXT4_I(inode)->i_file_acl) {
1306 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1310 if (ext4_xattr_check_block(inode, bh)) {
1311 EXT4_ERROR_INODE(inode, "bad block %llu",
1312 EXT4_I(inode)->i_file_acl);
1318 end = bh->b_data + bh->b_size;
1319 min_offs = end - base;
1320 free = ext4_xattr_free_space(first, &min_offs, base,
1322 if (free < new_extra_isize) {
1323 if (!tried_min_extra_isize && s_min_extra_isize) {
1324 tried_min_extra_isize++;
1325 new_extra_isize = s_min_extra_isize;
1333 free = inode->i_sb->s_blocksize;
1336 while (new_extra_isize > 0) {
1337 size_t offs, size, entry_size;
1338 struct ext4_xattr_entry *small_entry = NULL;
1339 struct ext4_xattr_info i = {
1343 unsigned int total_size; /* EA entry size + value size */
1344 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1345 unsigned int min_total_size = ~0U;
1347 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1348 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1354 is->s.not_found = -ENODATA;
1355 bs->s.not_found = -ENODATA;
1359 last = IFIRST(header);
1360 /* Find the entry best suited to be pushed into EA block */
1362 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1364 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1365 EXT4_XATTR_LEN(last->e_name_len);
1366 if (total_size <= free && total_size < min_total_size) {
1367 if (total_size < new_extra_isize) {
1371 min_total_size = total_size;
1376 if (entry == NULL) {
1378 entry = small_entry;
1380 if (!tried_min_extra_isize &&
1381 s_min_extra_isize) {
1382 tried_min_extra_isize++;
1383 new_extra_isize = s_min_extra_isize;
1384 kfree(is); is = NULL;
1385 kfree(bs); bs = NULL;
1393 offs = le16_to_cpu(entry->e_value_offs);
1394 size = le32_to_cpu(entry->e_value_size);
1395 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1396 i.name_index = entry->e_name_index,
1397 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1398 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1399 if (!buffer || !b_entry_name) {
1403 /* Save the entry name and the entry value */
1404 memcpy(buffer, (void *)IFIRST(header) + offs,
1405 EXT4_XATTR_SIZE(size));
1406 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1407 b_entry_name[entry->e_name_len] = '\0';
1408 i.name = b_entry_name;
1410 error = ext4_get_inode_loc(inode, &is->iloc);
1414 error = ext4_xattr_ibody_find(inode, &i, is);
1418 /* Remove the chosen entry from the inode */
1419 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1423 entry = IFIRST(header);
1424 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1425 shift_bytes = new_extra_isize;
1427 shift_bytes = entry_size + size;
1428 /* Adjust the offsets and shift the remaining entries ahead */
1429 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1430 shift_bytes, (void *)raw_inode +
1431 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1432 (void *)header, total_ino - entry_size,
1433 inode->i_sb->s_blocksize);
1435 extra_isize += shift_bytes;
1436 new_extra_isize -= shift_bytes;
1437 EXT4_I(inode)->i_extra_isize = extra_isize;
1439 i.name = b_entry_name;
1442 error = ext4_xattr_block_find(inode, &i, bs);
1446 /* Add entry which was removed from the inode into the block */
1447 error = ext4_xattr_block_set(handle, inode, &i, bs);
1450 kfree(b_entry_name);
1452 b_entry_name = NULL;
1454 brelse(is->iloc.bh);
1459 up_write(&EXT4_I(inode)->xattr_sem);
1463 kfree(b_entry_name);
1466 brelse(is->iloc.bh);
1470 up_write(&EXT4_I(inode)->xattr_sem);
1477 * ext4_xattr_delete_inode()
1479 * Free extended attribute resources associated with this inode. This
1480 * is called immediately before an inode is freed. We have exclusive
1481 * access to the inode.
1484 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1486 struct buffer_head *bh = NULL;
1488 if (!EXT4_I(inode)->i_file_acl)
1490 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1492 EXT4_ERROR_INODE(inode, "block %llu read error",
1493 EXT4_I(inode)->i_file_acl);
1496 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1497 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1498 EXT4_ERROR_INODE(inode, "bad block %llu",
1499 EXT4_I(inode)->i_file_acl);
1502 ext4_xattr_release_block(handle, inode, bh);
1503 EXT4_I(inode)->i_file_acl = 0;
1510 * ext4_xattr_put_super()
1512 * This is called when a file system is unmounted.
1515 ext4_xattr_put_super(struct super_block *sb)
1517 mb_cache_shrink(sb->s_bdev);
1521 * ext4_xattr_cache_insert()
1523 * Create a new entry in the extended attribute cache, and insert
1524 * it unless such an entry is already in the cache.
1526 * Returns 0, or a negative error number on failure.
1529 ext4_xattr_cache_insert(struct buffer_head *bh)
1531 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1532 struct mb_cache_entry *ce;
1535 ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
1537 ea_bdebug(bh, "out of memory");
1540 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1542 mb_cache_entry_free(ce);
1543 if (error == -EBUSY) {
1544 ea_bdebug(bh, "already in cache");
1548 ea_bdebug(bh, "inserting [%x]", (int)hash);
1549 mb_cache_entry_release(ce);
1556 * Compare two extended attribute blocks for equality.
1558 * Returns 0 if the blocks are equal, 1 if they differ, and
1559 * a negative error number on errors.
1562 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1563 struct ext4_xattr_header *header2)
1565 struct ext4_xattr_entry *entry1, *entry2;
1567 entry1 = ENTRY(header1+1);
1568 entry2 = ENTRY(header2+1);
1569 while (!IS_LAST_ENTRY(entry1)) {
1570 if (IS_LAST_ENTRY(entry2))
1572 if (entry1->e_hash != entry2->e_hash ||
1573 entry1->e_name_index != entry2->e_name_index ||
1574 entry1->e_name_len != entry2->e_name_len ||
1575 entry1->e_value_size != entry2->e_value_size ||
1576 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1578 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1580 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1581 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1582 le32_to_cpu(entry1->e_value_size)))
1585 entry1 = EXT4_XATTR_NEXT(entry1);
1586 entry2 = EXT4_XATTR_NEXT(entry2);
1588 if (!IS_LAST_ENTRY(entry2))
1594 * ext4_xattr_cache_find()
1596 * Find an identical extended attribute block.
1598 * Returns a pointer to the block found, or NULL if such a block was
1599 * not found or an error occurred.
1601 static struct buffer_head *
1602 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1603 struct mb_cache_entry **pce)
1605 __u32 hash = le32_to_cpu(header->h_hash);
1606 struct mb_cache_entry *ce;
1608 if (!header->h_hash)
1609 return NULL; /* never share */
1610 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1612 ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1615 struct buffer_head *bh;
1618 if (PTR_ERR(ce) == -EAGAIN)
1622 bh = sb_bread(inode->i_sb, ce->e_block);
1624 EXT4_ERROR_INODE(inode, "block %lu read error",
1625 (unsigned long) ce->e_block);
1626 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1627 EXT4_XATTR_REFCOUNT_MAX) {
1628 ea_idebug(inode, "block %lu refcount %d>=%d",
1629 (unsigned long) ce->e_block,
1630 le32_to_cpu(BHDR(bh)->h_refcount),
1631 EXT4_XATTR_REFCOUNT_MAX);
1632 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1637 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1642 #define NAME_HASH_SHIFT 5
1643 #define VALUE_HASH_SHIFT 16
1646 * ext4_xattr_hash_entry()
1648 * Compute the hash of an extended attribute.
1650 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1651 struct ext4_xattr_entry *entry)
1654 char *name = entry->e_name;
1657 for (n = 0; n < entry->e_name_len; n++) {
1658 hash = (hash << NAME_HASH_SHIFT) ^
1659 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1663 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1664 __le32 *value = (__le32 *)((char *)header +
1665 le16_to_cpu(entry->e_value_offs));
1666 for (n = (le32_to_cpu(entry->e_value_size) +
1667 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1668 hash = (hash << VALUE_HASH_SHIFT) ^
1669 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1670 le32_to_cpu(*value++);
1673 entry->e_hash = cpu_to_le32(hash);
1676 #undef NAME_HASH_SHIFT
1677 #undef VALUE_HASH_SHIFT
1679 #define BLOCK_HASH_SHIFT 16
1682 * ext4_xattr_rehash()
1684 * Re-compute the extended attribute hash value after an entry has changed.
1686 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1687 struct ext4_xattr_entry *entry)
1689 struct ext4_xattr_entry *here;
1692 ext4_xattr_hash_entry(header, entry);
1693 here = ENTRY(header+1);
1694 while (!IS_LAST_ENTRY(here)) {
1695 if (!here->e_hash) {
1696 /* Block is not shared if an entry's hash value == 0 */
1700 hash = (hash << BLOCK_HASH_SHIFT) ^
1701 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1702 le32_to_cpu(here->e_hash);
1703 here = EXT4_XATTR_NEXT(here);
1705 header->h_hash = cpu_to_le32(hash);
1708 #undef BLOCK_HASH_SHIFT
1711 ext4_init_xattr(void)
1713 ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
1714 if (!ext4_xattr_cache)
1720 ext4_exit_xattr(void)
1722 if (ext4_xattr_cache)
1723 mb_cache_destroy(ext4_xattr_cache);
1724 ext4_xattr_cache = NULL;