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 "ext4_jbd2.h"
63 #ifdef EXT4_XATTR_DEBUG
64 # define ea_idebug(inode, f...) do { \
65 printk(KERN_DEBUG "inode %s:%lu: ", \
66 inode->i_sb->s_id, inode->i_ino); \
70 # define ea_bdebug(bh, f...) do { \
71 char b[BDEVNAME_SIZE]; \
72 printk(KERN_DEBUG "block %s:%lu: ", \
73 bdevname(bh->b_bdev, b), \
74 (unsigned long) bh->b_blocknr); \
79 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
80 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
83 static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
84 static struct buffer_head *ext4_xattr_cache_find(struct inode *,
85 struct ext4_xattr_header *,
86 struct mb_cache_entry **);
87 static void ext4_xattr_rehash(struct ext4_xattr_header *,
88 struct ext4_xattr_entry *);
89 static int ext4_xattr_list(struct dentry *dentry, char *buffer,
92 static const struct xattr_handler *ext4_xattr_handler_map[] = {
93 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
94 #ifdef CONFIG_EXT4_FS_POSIX_ACL
95 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
96 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
98 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
99 #ifdef CONFIG_EXT4_FS_SECURITY
100 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
104 const struct xattr_handler *ext4_xattr_handlers[] = {
105 &ext4_xattr_user_handler,
106 &ext4_xattr_trusted_handler,
107 #ifdef CONFIG_EXT4_FS_POSIX_ACL
108 &posix_acl_access_xattr_handler,
109 &posix_acl_default_xattr_handler,
111 #ifdef CONFIG_EXT4_FS_SECURITY
112 &ext4_xattr_security_handler,
117 #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
118 inode->i_sb->s_fs_info)->s_mb_cache)
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_metadata_csum(inode->i_sb) &&
145 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
150 static void ext4_xattr_block_csum_set(struct inode *inode,
152 struct ext4_xattr_header *hdr)
154 if (!ext4_has_metadata_csum(inode->i_sb))
157 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
160 static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
162 struct buffer_head *bh)
164 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
165 return ext4_handle_dirty_metadata(handle, inode, bh);
168 static inline const struct xattr_handler *
169 ext4_xattr_handler(int name_index)
171 const struct xattr_handler *handler = NULL;
173 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
174 handler = ext4_xattr_handler_map[name_index];
179 * Inode operation listxattr()
181 * dentry->d_inode->i_mutex: don't care
184 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
186 return ext4_xattr_list(dentry, buffer, size);
190 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
193 struct ext4_xattr_entry *e = entry;
195 while (!IS_LAST_ENTRY(e)) {
196 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
197 if ((void *)next >= end)
202 while (!IS_LAST_ENTRY(entry)) {
203 if (entry->e_value_size != 0 &&
204 (value_start + le16_to_cpu(entry->e_value_offs) <
205 (void *)e + sizeof(__u32) ||
206 value_start + le16_to_cpu(entry->e_value_offs) +
207 le32_to_cpu(entry->e_value_size) > end))
209 entry = EXT4_XATTR_NEXT(entry);
216 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
220 if (buffer_verified(bh))
223 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
224 BHDR(bh)->h_blocks != cpu_to_le32(1))
226 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
228 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
231 set_buffer_verified(bh);
236 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
238 size_t value_size = le32_to_cpu(entry->e_value_size);
240 if (entry->e_value_block != 0 || value_size > size ||
241 le16_to_cpu(entry->e_value_offs) + value_size > size)
247 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
248 const char *name, size_t size, int sorted)
250 struct ext4_xattr_entry *entry;
256 name_len = strlen(name);
258 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
259 cmp = name_index - entry->e_name_index;
261 cmp = name_len - entry->e_name_len;
263 cmp = memcmp(name, entry->e_name, name_len);
264 if (cmp <= 0 && (sorted || cmp == 0))
268 if (!cmp && ext4_xattr_check_entry(entry, size))
270 return cmp ? -ENODATA : 0;
274 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
275 void *buffer, size_t buffer_size)
277 struct buffer_head *bh = NULL;
278 struct ext4_xattr_entry *entry;
281 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
283 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
284 name_index, name, buffer, (long)buffer_size);
287 if (!EXT4_I(inode)->i_file_acl)
289 ea_idebug(inode, "reading block %llu",
290 (unsigned long long)EXT4_I(inode)->i_file_acl);
291 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
294 ea_bdebug(bh, "b_count=%d, refcount=%d",
295 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
296 if (ext4_xattr_check_block(inode, bh)) {
298 EXT4_ERROR_INODE(inode, "bad block %llu",
299 EXT4_I(inode)->i_file_acl);
303 ext4_xattr_cache_insert(ext4_mb_cache, bh);
305 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
310 size = le32_to_cpu(entry->e_value_size);
313 if (size > buffer_size)
315 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
326 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
327 void *buffer, size_t buffer_size)
329 struct ext4_xattr_ibody_header *header;
330 struct ext4_xattr_entry *entry;
331 struct ext4_inode *raw_inode;
332 struct ext4_iloc iloc;
337 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
339 error = ext4_get_inode_loc(inode, &iloc);
342 raw_inode = ext4_raw_inode(&iloc);
343 header = IHDR(inode, raw_inode);
344 entry = IFIRST(header);
345 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
346 error = ext4_xattr_check_names(entry, end, entry);
349 error = ext4_xattr_find_entry(&entry, name_index, name,
350 end - (void *)entry, 0);
353 size = le32_to_cpu(entry->e_value_size);
356 if (size > buffer_size)
358 memcpy(buffer, (void *)IFIRST(header) +
359 le16_to_cpu(entry->e_value_offs), size);
371 * Copy an extended attribute into the buffer
372 * provided, or compute the buffer size required.
373 * Buffer is NULL to compute the size of the buffer required.
375 * Returns a negative error number on failure, or the number of bytes
376 * used / required on success.
379 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
380 void *buffer, size_t buffer_size)
384 if (strlen(name) > 255)
387 down_read(&EXT4_I(inode)->xattr_sem);
388 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
390 if (error == -ENODATA)
391 error = ext4_xattr_block_get(inode, name_index, name, buffer,
393 up_read(&EXT4_I(inode)->xattr_sem);
398 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
399 char *buffer, size_t buffer_size)
401 size_t rest = buffer_size;
403 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
404 const struct xattr_handler *handler =
405 ext4_xattr_handler(entry->e_name_index);
408 size_t size = handler->list(dentry, buffer, rest,
420 return buffer_size - rest;
424 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
426 struct inode *inode = dentry->d_inode;
427 struct buffer_head *bh = NULL;
429 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
431 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
432 buffer, (long)buffer_size);
435 if (!EXT4_I(inode)->i_file_acl)
437 ea_idebug(inode, "reading block %llu",
438 (unsigned long long)EXT4_I(inode)->i_file_acl);
439 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
443 ea_bdebug(bh, "b_count=%d, refcount=%d",
444 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
445 if (ext4_xattr_check_block(inode, bh)) {
446 EXT4_ERROR_INODE(inode, "bad block %llu",
447 EXT4_I(inode)->i_file_acl);
451 ext4_xattr_cache_insert(ext4_mb_cache, bh);
452 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
461 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
463 struct inode *inode = dentry->d_inode;
464 struct ext4_xattr_ibody_header *header;
465 struct ext4_inode *raw_inode;
466 struct ext4_iloc iloc;
470 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
472 error = ext4_get_inode_loc(inode, &iloc);
475 raw_inode = ext4_raw_inode(&iloc);
476 header = IHDR(inode, raw_inode);
477 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
478 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
481 error = ext4_xattr_list_entries(dentry, IFIRST(header),
482 buffer, buffer_size);
492 * Copy a list of attribute names into the buffer
493 * provided, or compute the buffer size required.
494 * Buffer is NULL to compute the size of the buffer required.
496 * Returns a negative error number on failure, or the number of bytes
497 * used / required on success.
500 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
504 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
505 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
512 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
517 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
522 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
525 static void ext4_xattr_update_super_block(handle_t *handle,
526 struct super_block *sb)
528 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
531 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
532 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
533 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
534 ext4_handle_dirty_super(handle, sb);
539 * Release the xattr block BH: If the reference count is > 1, decrement it;
540 * otherwise free the block.
543 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
544 struct buffer_head *bh)
546 struct mb_cache_entry *ce = NULL;
548 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
550 ce = mb_cache_entry_get(ext4_mb_cache, bh->b_bdev, bh->b_blocknr);
551 BUFFER_TRACE(bh, "get_write_access");
552 error = ext4_journal_get_write_access(handle, bh);
557 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
558 ea_bdebug(bh, "refcount now=0; freeing");
560 mb_cache_entry_free(ce);
563 ext4_free_blocks(handle, inode, bh, 0, 1,
564 EXT4_FREE_BLOCKS_METADATA |
565 EXT4_FREE_BLOCKS_FORGET);
567 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
569 mb_cache_entry_release(ce);
571 * Beware of this ugliness: Releasing of xattr block references
572 * from different inodes can race and so we have to protect
573 * from a race where someone else frees the block (and releases
574 * its journal_head) before we are done dirtying the buffer. In
575 * nojournal mode this race is harmless and we actually cannot
576 * call ext4_handle_dirty_xattr_block() with locked buffer as
577 * that function can call sync_dirty_buffer() so for that case
578 * we handle the dirtying after unlocking the buffer.
580 if (ext4_handle_valid(handle))
581 error = ext4_handle_dirty_xattr_block(handle, inode,
584 if (!ext4_handle_valid(handle))
585 error = ext4_handle_dirty_xattr_block(handle, inode,
588 ext4_handle_sync(handle);
589 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
590 ea_bdebug(bh, "refcount now=%d; releasing",
591 le32_to_cpu(BHDR(bh)->h_refcount));
594 ext4_std_error(inode->i_sb, error);
599 * Find the available free space for EAs. This also returns the total number of
600 * bytes used by EA entries.
602 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
603 size_t *min_offs, void *base, int *total)
605 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
606 if (!last->e_value_block && last->e_value_size) {
607 size_t offs = le16_to_cpu(last->e_value_offs);
608 if (offs < *min_offs)
612 *total += EXT4_XATTR_LEN(last->e_name_len);
614 return (*min_offs - ((void *)last - base) - sizeof(__u32));
618 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
620 struct ext4_xattr_entry *last;
621 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
623 /* Compute min_offs and last. */
625 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
626 if (!last->e_value_block && last->e_value_size) {
627 size_t offs = le16_to_cpu(last->e_value_offs);
632 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
634 if (!s->here->e_value_block && s->here->e_value_size) {
635 size_t size = le32_to_cpu(s->here->e_value_size);
636 free += EXT4_XATTR_SIZE(size);
638 free += EXT4_XATTR_LEN(name_len);
641 if (free < EXT4_XATTR_SIZE(i->value_len) ||
642 free < EXT4_XATTR_LEN(name_len) +
643 EXT4_XATTR_SIZE(i->value_len))
647 if (i->value && s->not_found) {
648 /* Insert the new name. */
649 size_t size = EXT4_XATTR_LEN(name_len);
650 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
651 memmove((void *)s->here + size, s->here, rest);
652 memset(s->here, 0, size);
653 s->here->e_name_index = i->name_index;
654 s->here->e_name_len = name_len;
655 memcpy(s->here->e_name, i->name, name_len);
657 if (!s->here->e_value_block && s->here->e_value_size) {
658 void *first_val = s->base + min_offs;
659 size_t offs = le16_to_cpu(s->here->e_value_offs);
660 void *val = s->base + offs;
661 size_t size = EXT4_XATTR_SIZE(
662 le32_to_cpu(s->here->e_value_size));
664 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
665 /* The old and the new value have the same
666 size. Just replace. */
667 s->here->e_value_size =
668 cpu_to_le32(i->value_len);
669 if (i->value == EXT4_ZERO_XATTR_VALUE) {
670 memset(val, 0, size);
672 /* Clear pad bytes first. */
673 memset(val + size - EXT4_XATTR_PAD, 0,
675 memcpy(val, i->value, i->value_len);
680 /* Remove the old value. */
681 memmove(first_val + size, first_val, val - first_val);
682 memset(first_val, 0, size);
683 s->here->e_value_size = 0;
684 s->here->e_value_offs = 0;
687 /* Adjust all value offsets. */
689 while (!IS_LAST_ENTRY(last)) {
690 size_t o = le16_to_cpu(last->e_value_offs);
691 if (!last->e_value_block &&
692 last->e_value_size && o < offs)
694 cpu_to_le16(o + size);
695 last = EXT4_XATTR_NEXT(last);
699 /* Remove the old name. */
700 size_t size = EXT4_XATTR_LEN(name_len);
701 last = ENTRY((void *)last - size);
702 memmove(s->here, (void *)s->here + size,
703 (void *)last - (void *)s->here + sizeof(__u32));
704 memset(last, 0, size);
709 /* Insert the new value. */
710 s->here->e_value_size = cpu_to_le32(i->value_len);
712 size_t size = EXT4_XATTR_SIZE(i->value_len);
713 void *val = s->base + min_offs - size;
714 s->here->e_value_offs = cpu_to_le16(min_offs - size);
715 if (i->value == EXT4_ZERO_XATTR_VALUE) {
716 memset(val, 0, size);
718 /* Clear the pad bytes first. */
719 memset(val + size - EXT4_XATTR_PAD, 0,
721 memcpy(val, i->value, i->value_len);
728 struct ext4_xattr_block_find {
729 struct ext4_xattr_search s;
730 struct buffer_head *bh;
734 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
735 struct ext4_xattr_block_find *bs)
737 struct super_block *sb = inode->i_sb;
740 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
741 i->name_index, i->name, i->value, (long)i->value_len);
743 if (EXT4_I(inode)->i_file_acl) {
744 /* The inode already has an extended attribute block. */
745 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
749 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
750 atomic_read(&(bs->bh->b_count)),
751 le32_to_cpu(BHDR(bs->bh)->h_refcount));
752 if (ext4_xattr_check_block(inode, bs->bh)) {
753 EXT4_ERROR_INODE(inode, "bad block %llu",
754 EXT4_I(inode)->i_file_acl);
758 /* Find the named attribute. */
759 bs->s.base = BHDR(bs->bh);
760 bs->s.first = BFIRST(bs->bh);
761 bs->s.end = bs->bh->b_data + bs->bh->b_size;
762 bs->s.here = bs->s.first;
763 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
764 i->name, bs->bh->b_size, 1);
765 if (error && error != -ENODATA)
767 bs->s.not_found = error;
776 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
777 struct ext4_xattr_info *i,
778 struct ext4_xattr_block_find *bs)
780 struct super_block *sb = inode->i_sb;
781 struct buffer_head *new_bh = NULL;
782 struct ext4_xattr_search *s = &bs->s;
783 struct mb_cache_entry *ce = NULL;
785 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
787 #define header(x) ((struct ext4_xattr_header *)(x))
789 if (i->value && i->value_len > sb->s_blocksize)
792 ce = mb_cache_entry_get(ext4_mb_cache, bs->bh->b_bdev,
794 BUFFER_TRACE(bs->bh, "get_write_access");
795 error = ext4_journal_get_write_access(handle, bs->bh);
800 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
802 mb_cache_entry_free(ce);
805 ea_bdebug(bs->bh, "modifying in-place");
806 error = ext4_xattr_set_entry(i, s);
808 if (!IS_LAST_ENTRY(s->first))
809 ext4_xattr_rehash(header(s->base),
811 ext4_xattr_cache_insert(ext4_mb_cache,
814 unlock_buffer(bs->bh);
818 error = ext4_handle_dirty_xattr_block(handle,
825 int offset = (char *)s->here - bs->bh->b_data;
827 unlock_buffer(bs->bh);
829 mb_cache_entry_release(ce);
832 ea_bdebug(bs->bh, "cloning");
833 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
837 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
838 s->first = ENTRY(header(s->base)+1);
839 header(s->base)->h_refcount = cpu_to_le32(1);
840 s->here = ENTRY(s->base + offset);
841 s->end = s->base + bs->bh->b_size;
844 /* Allocate a buffer where we construct the new block. */
845 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
846 /* assert(header == s->base) */
850 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
851 header(s->base)->h_blocks = cpu_to_le32(1);
852 header(s->base)->h_refcount = cpu_to_le32(1);
853 s->first = ENTRY(header(s->base)+1);
854 s->here = ENTRY(header(s->base)+1);
855 s->end = s->base + sb->s_blocksize;
858 error = ext4_xattr_set_entry(i, s);
863 if (!IS_LAST_ENTRY(s->first))
864 ext4_xattr_rehash(header(s->base), s->here);
867 if (!IS_LAST_ENTRY(s->first)) {
868 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
870 /* We found an identical block in the cache. */
871 if (new_bh == bs->bh)
872 ea_bdebug(new_bh, "keeping");
874 /* The old block is released after updating
876 error = dquot_alloc_block(inode,
877 EXT4_C2B(EXT4_SB(sb), 1));
880 BUFFER_TRACE(new_bh, "get_write_access");
881 error = ext4_journal_get_write_access(handle,
886 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
887 ea_bdebug(new_bh, "reusing; refcount now=%d",
888 le32_to_cpu(BHDR(new_bh)->h_refcount));
889 unlock_buffer(new_bh);
890 error = ext4_handle_dirty_xattr_block(handle,
896 mb_cache_entry_release(ce);
898 } else if (bs->bh && s->base == bs->bh->b_data) {
899 /* We were modifying this block in-place. */
900 ea_bdebug(bs->bh, "keeping this block");
904 /* We need to allocate a new block */
905 ext4_fsblk_t goal, block;
907 goal = ext4_group_first_block_no(sb,
908 EXT4_I(inode)->i_block_group);
910 /* non-extent files can't have physical blocks past 2^32 */
911 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
912 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
914 block = ext4_new_meta_blocks(handle, inode, goal, 0,
919 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
920 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
922 ea_idebug(inode, "creating block %llu",
923 (unsigned long long)block);
925 new_bh = sb_getblk(sb, block);
926 if (unlikely(!new_bh)) {
929 ext4_free_blocks(handle, inode, NULL, block, 1,
930 EXT4_FREE_BLOCKS_METADATA);
934 error = ext4_journal_get_create_access(handle, new_bh);
936 unlock_buffer(new_bh);
940 memcpy(new_bh->b_data, s->base, new_bh->b_size);
941 set_buffer_uptodate(new_bh);
942 unlock_buffer(new_bh);
943 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
944 error = ext4_handle_dirty_xattr_block(handle,
951 /* Update the inode. */
952 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
954 /* Drop the previous xattr block. */
955 if (bs->bh && bs->bh != new_bh)
956 ext4_xattr_release_block(handle, inode, bs->bh);
961 mb_cache_entry_release(ce);
963 if (!(bs->bh && s->base == bs->bh->b_data))
969 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
973 EXT4_ERROR_INODE(inode, "bad block %llu",
974 EXT4_I(inode)->i_file_acl);
980 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
981 struct ext4_xattr_ibody_find *is)
983 struct ext4_xattr_ibody_header *header;
984 struct ext4_inode *raw_inode;
987 if (EXT4_I(inode)->i_extra_isize == 0)
989 raw_inode = ext4_raw_inode(&is->iloc);
990 header = IHDR(inode, raw_inode);
991 is->s.base = is->s.first = IFIRST(header);
992 is->s.here = is->s.first;
993 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
994 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
995 error = ext4_xattr_check_names(IFIRST(header), is->s.end,
999 /* Find the named attribute. */
1000 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
1001 i->name, is->s.end -
1002 (void *)is->s.base, 0);
1003 if (error && error != -ENODATA)
1005 is->s.not_found = error;
1010 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1011 struct ext4_xattr_info *i,
1012 struct ext4_xattr_ibody_find *is)
1014 struct ext4_xattr_ibody_header *header;
1015 struct ext4_xattr_search *s = &is->s;
1018 if (EXT4_I(inode)->i_extra_isize == 0)
1020 error = ext4_xattr_set_entry(i, s);
1022 if (error == -ENOSPC &&
1023 ext4_has_inline_data(inode)) {
1024 error = ext4_try_to_evict_inline_data(handle, inode,
1025 EXT4_XATTR_LEN(strlen(i->name) +
1026 EXT4_XATTR_SIZE(i->value_len)));
1029 error = ext4_xattr_ibody_find(inode, i, is);
1032 error = ext4_xattr_set_entry(i, s);
1037 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1038 if (!IS_LAST_ENTRY(s->first)) {
1039 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1040 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1042 header->h_magic = cpu_to_le32(0);
1043 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1048 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1049 struct ext4_xattr_info *i,
1050 struct ext4_xattr_ibody_find *is)
1052 struct ext4_xattr_ibody_header *header;
1053 struct ext4_xattr_search *s = &is->s;
1056 if (EXT4_I(inode)->i_extra_isize == 0)
1058 error = ext4_xattr_set_entry(i, s);
1061 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1062 if (!IS_LAST_ENTRY(s->first)) {
1063 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1064 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1066 header->h_magic = cpu_to_le32(0);
1067 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1073 * ext4_xattr_set_handle()
1075 * Create, replace or remove an extended attribute for this inode. Value
1076 * is NULL to remove an existing extended attribute, and non-NULL to
1077 * either replace an existing extended attribute, or create a new extended
1078 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1079 * specify that an extended attribute must exist and must not exist
1080 * previous to the call, respectively.
1082 * Returns 0, or a negative error number on failure.
1085 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1086 const char *name, const void *value, size_t value_len,
1089 struct ext4_xattr_info i = {
1090 .name_index = name_index,
1093 .value_len = value_len,
1096 struct ext4_xattr_ibody_find is = {
1097 .s = { .not_found = -ENODATA, },
1099 struct ext4_xattr_block_find bs = {
1100 .s = { .not_found = -ENODATA, },
1102 unsigned long no_expand;
1107 if (strlen(name) > 255)
1109 down_write(&EXT4_I(inode)->xattr_sem);
1110 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1111 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1113 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1117 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1118 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1119 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1120 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1123 error = ext4_xattr_ibody_find(inode, &i, &is);
1127 error = ext4_xattr_block_find(inode, &i, &bs);
1130 if (is.s.not_found && bs.s.not_found) {
1132 if (flags & XATTR_REPLACE)
1139 if (flags & XATTR_CREATE)
1143 if (!is.s.not_found)
1144 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1145 else if (!bs.s.not_found)
1146 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1148 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1149 if (!error && !bs.s.not_found) {
1151 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1152 } else if (error == -ENOSPC) {
1153 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1154 error = ext4_xattr_block_find(inode, &i, &bs);
1158 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1161 if (!is.s.not_found) {
1163 error = ext4_xattr_ibody_set(handle, inode, &i,
1169 ext4_xattr_update_super_block(handle, inode->i_sb);
1170 inode->i_ctime = ext4_current_time(inode);
1172 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1173 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1175 * The bh is consumed by ext4_mark_iloc_dirty, even with
1180 ext4_handle_sync(handle);
1187 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1188 up_write(&EXT4_I(inode)->xattr_sem);
1195 * Like ext4_xattr_set_handle, but start from an inode. This extended
1196 * attribute modification is a filesystem transaction by itself.
1198 * Returns 0, or a negative error number on failure.
1201 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1202 const void *value, size_t value_len, int flags)
1205 int error, retries = 0;
1206 int credits = ext4_jbd2_credits_xattr(inode);
1209 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1210 if (IS_ERR(handle)) {
1211 error = PTR_ERR(handle);
1215 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1216 value, value_len, flags);
1217 error2 = ext4_journal_stop(handle);
1218 if (error == -ENOSPC &&
1219 ext4_should_retry_alloc(inode->i_sb, &retries))
1229 * Shift the EA entries in the inode to create space for the increased
1232 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1233 int value_offs_shift, void *to,
1234 void *from, size_t n, int blocksize)
1236 struct ext4_xattr_entry *last = entry;
1239 /* Adjust the value offsets of the entries */
1240 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1241 if (!last->e_value_block && last->e_value_size) {
1242 new_offs = le16_to_cpu(last->e_value_offs) +
1244 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1246 last->e_value_offs = cpu_to_le16(new_offs);
1249 /* Shift the entries by n bytes */
1250 memmove(to, from, n);
1254 * Expand an inode by new_extra_isize bytes when EAs are present.
1255 * Returns 0 on success or negative error number on failure.
1257 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1258 struct ext4_inode *raw_inode, handle_t *handle)
1260 struct ext4_xattr_ibody_header *header;
1261 struct ext4_xattr_entry *entry, *last, *first;
1262 struct buffer_head *bh = NULL;
1263 struct ext4_xattr_ibody_find *is = NULL;
1264 struct ext4_xattr_block_find *bs = NULL;
1265 char *buffer = NULL, *b_entry_name = NULL;
1266 size_t min_offs, free;
1268 void *base, *start, *end;
1269 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1270 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1272 down_write(&EXT4_I(inode)->xattr_sem);
1274 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1275 up_write(&EXT4_I(inode)->xattr_sem);
1279 header = IHDR(inode, raw_inode);
1280 entry = IFIRST(header);
1283 * Check if enough free space is available in the inode to shift the
1284 * entries ahead by new_extra_isize.
1287 base = start = entry;
1288 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1289 min_offs = end - base;
1291 total_ino = sizeof(struct ext4_xattr_ibody_header);
1293 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1294 if (free >= new_extra_isize) {
1295 entry = IFIRST(header);
1296 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1297 - new_extra_isize, (void *)raw_inode +
1298 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1299 (void *)header, total_ino,
1300 inode->i_sb->s_blocksize);
1301 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1307 * Enough free space isn't available in the inode, check if
1308 * EA block can hold new_extra_isize bytes.
1310 if (EXT4_I(inode)->i_file_acl) {
1311 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1315 if (ext4_xattr_check_block(inode, bh)) {
1316 EXT4_ERROR_INODE(inode, "bad block %llu",
1317 EXT4_I(inode)->i_file_acl);
1323 end = bh->b_data + bh->b_size;
1324 min_offs = end - base;
1325 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
1326 if (free < new_extra_isize) {
1327 if (!tried_min_extra_isize && s_min_extra_isize) {
1328 tried_min_extra_isize++;
1329 new_extra_isize = s_min_extra_isize;
1337 free = inode->i_sb->s_blocksize;
1340 while (new_extra_isize > 0) {
1341 size_t offs, size, entry_size;
1342 struct ext4_xattr_entry *small_entry = NULL;
1343 struct ext4_xattr_info i = {
1347 unsigned int total_size; /* EA entry size + value size */
1348 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1349 unsigned int min_total_size = ~0U;
1351 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1352 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1358 is->s.not_found = -ENODATA;
1359 bs->s.not_found = -ENODATA;
1363 last = IFIRST(header);
1364 /* Find the entry best suited to be pushed into EA block */
1366 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1368 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1369 EXT4_XATTR_LEN(last->e_name_len);
1370 if (total_size <= free && total_size < min_total_size) {
1371 if (total_size < new_extra_isize) {
1375 min_total_size = total_size;
1380 if (entry == NULL) {
1382 entry = small_entry;
1384 if (!tried_min_extra_isize &&
1385 s_min_extra_isize) {
1386 tried_min_extra_isize++;
1387 new_extra_isize = s_min_extra_isize;
1388 kfree(is); is = NULL;
1389 kfree(bs); bs = NULL;
1397 offs = le16_to_cpu(entry->e_value_offs);
1398 size = le32_to_cpu(entry->e_value_size);
1399 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1400 i.name_index = entry->e_name_index,
1401 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1402 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1403 if (!buffer || !b_entry_name) {
1407 /* Save the entry name and the entry value */
1408 memcpy(buffer, (void *)IFIRST(header) + offs,
1409 EXT4_XATTR_SIZE(size));
1410 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1411 b_entry_name[entry->e_name_len] = '\0';
1412 i.name = b_entry_name;
1414 error = ext4_get_inode_loc(inode, &is->iloc);
1418 error = ext4_xattr_ibody_find(inode, &i, is);
1422 /* Remove the chosen entry from the inode */
1423 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1427 entry = IFIRST(header);
1428 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1429 shift_bytes = new_extra_isize;
1431 shift_bytes = entry_size + size;
1432 /* Adjust the offsets and shift the remaining entries ahead */
1433 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1434 shift_bytes, (void *)raw_inode +
1435 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1436 (void *)header, total_ino - entry_size,
1437 inode->i_sb->s_blocksize);
1439 extra_isize += shift_bytes;
1440 new_extra_isize -= shift_bytes;
1441 EXT4_I(inode)->i_extra_isize = extra_isize;
1443 i.name = b_entry_name;
1446 error = ext4_xattr_block_find(inode, &i, bs);
1450 /* Add entry which was removed from the inode into the block */
1451 error = ext4_xattr_block_set(handle, inode, &i, bs);
1454 kfree(b_entry_name);
1456 b_entry_name = NULL;
1458 brelse(is->iloc.bh);
1463 up_write(&EXT4_I(inode)->xattr_sem);
1467 kfree(b_entry_name);
1470 brelse(is->iloc.bh);
1474 up_write(&EXT4_I(inode)->xattr_sem);
1481 * ext4_xattr_delete_inode()
1483 * Free extended attribute resources associated with this inode. This
1484 * is called immediately before an inode is freed. We have exclusive
1485 * access to the inode.
1488 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1490 struct buffer_head *bh = NULL;
1492 if (!EXT4_I(inode)->i_file_acl)
1494 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1496 EXT4_ERROR_INODE(inode, "block %llu read error",
1497 EXT4_I(inode)->i_file_acl);
1500 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1501 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1502 EXT4_ERROR_INODE(inode, "bad block %llu",
1503 EXT4_I(inode)->i_file_acl);
1506 ext4_xattr_release_block(handle, inode, bh);
1507 EXT4_I(inode)->i_file_acl = 0;
1514 * ext4_xattr_put_super()
1516 * This is called when a file system is unmounted.
1519 ext4_xattr_put_super(struct super_block *sb)
1521 mb_cache_shrink(sb->s_bdev);
1525 * ext4_xattr_cache_insert()
1527 * Create a new entry in the extended attribute cache, and insert
1528 * it unless such an entry is already in the cache.
1530 * Returns 0, or a negative error number on failure.
1533 ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
1535 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1536 struct mb_cache_entry *ce;
1539 ce = mb_cache_entry_alloc(ext4_mb_cache, GFP_NOFS);
1541 ea_bdebug(bh, "out of memory");
1544 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1546 mb_cache_entry_free(ce);
1547 if (error == -EBUSY) {
1548 ea_bdebug(bh, "already in cache");
1552 ea_bdebug(bh, "inserting [%x]", (int)hash);
1553 mb_cache_entry_release(ce);
1560 * Compare two extended attribute blocks for equality.
1562 * Returns 0 if the blocks are equal, 1 if they differ, and
1563 * a negative error number on errors.
1566 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1567 struct ext4_xattr_header *header2)
1569 struct ext4_xattr_entry *entry1, *entry2;
1571 entry1 = ENTRY(header1+1);
1572 entry2 = ENTRY(header2+1);
1573 while (!IS_LAST_ENTRY(entry1)) {
1574 if (IS_LAST_ENTRY(entry2))
1576 if (entry1->e_hash != entry2->e_hash ||
1577 entry1->e_name_index != entry2->e_name_index ||
1578 entry1->e_name_len != entry2->e_name_len ||
1579 entry1->e_value_size != entry2->e_value_size ||
1580 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1582 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1584 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1585 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1586 le32_to_cpu(entry1->e_value_size)))
1589 entry1 = EXT4_XATTR_NEXT(entry1);
1590 entry2 = EXT4_XATTR_NEXT(entry2);
1592 if (!IS_LAST_ENTRY(entry2))
1598 * ext4_xattr_cache_find()
1600 * Find an identical extended attribute block.
1602 * Returns a pointer to the block found, or NULL if such a block was
1603 * not found or an error occurred.
1605 static struct buffer_head *
1606 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1607 struct mb_cache_entry **pce)
1609 __u32 hash = le32_to_cpu(header->h_hash);
1610 struct mb_cache_entry *ce;
1611 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1613 if (!header->h_hash)
1614 return NULL; /* never share */
1615 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1617 ce = mb_cache_entry_find_first(ext4_mb_cache, inode->i_sb->s_bdev,
1620 struct buffer_head *bh;
1623 if (PTR_ERR(ce) == -EAGAIN)
1627 bh = sb_bread(inode->i_sb, ce->e_block);
1629 EXT4_ERROR_INODE(inode, "block %lu read error",
1630 (unsigned long) ce->e_block);
1631 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1632 EXT4_XATTR_REFCOUNT_MAX) {
1633 ea_idebug(inode, "block %lu refcount %d>=%d",
1634 (unsigned long) ce->e_block,
1635 le32_to_cpu(BHDR(bh)->h_refcount),
1636 EXT4_XATTR_REFCOUNT_MAX);
1637 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1642 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1647 #define NAME_HASH_SHIFT 5
1648 #define VALUE_HASH_SHIFT 16
1651 * ext4_xattr_hash_entry()
1653 * Compute the hash of an extended attribute.
1655 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1656 struct ext4_xattr_entry *entry)
1659 char *name = entry->e_name;
1662 for (n = 0; n < entry->e_name_len; n++) {
1663 hash = (hash << NAME_HASH_SHIFT) ^
1664 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1668 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1669 __le32 *value = (__le32 *)((char *)header +
1670 le16_to_cpu(entry->e_value_offs));
1671 for (n = (le32_to_cpu(entry->e_value_size) +
1672 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1673 hash = (hash << VALUE_HASH_SHIFT) ^
1674 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1675 le32_to_cpu(*value++);
1678 entry->e_hash = cpu_to_le32(hash);
1681 #undef NAME_HASH_SHIFT
1682 #undef VALUE_HASH_SHIFT
1684 #define BLOCK_HASH_SHIFT 16
1687 * ext4_xattr_rehash()
1689 * Re-compute the extended attribute hash value after an entry has changed.
1691 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1692 struct ext4_xattr_entry *entry)
1694 struct ext4_xattr_entry *here;
1697 ext4_xattr_hash_entry(header, entry);
1698 here = ENTRY(header+1);
1699 while (!IS_LAST_ENTRY(here)) {
1700 if (!here->e_hash) {
1701 /* Block is not shared if an entry's hash value == 0 */
1705 hash = (hash << BLOCK_HASH_SHIFT) ^
1706 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1707 le32_to_cpu(here->e_hash);
1708 here = EXT4_XATTR_NEXT(here);
1710 header->h_hash = cpu_to_le32(hash);
1713 #undef BLOCK_HASH_SHIFT
1715 #define HASH_BUCKET_BITS 10
1718 ext4_xattr_create_cache(char *name)
1720 return mb_cache_create(name, HASH_BUCKET_BITS);
1723 void ext4_xattr_destroy_cache(struct mb_cache *cache)
1726 mb_cache_destroy(cache);