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 printk(KERN_DEBUG "block %pg:%lu: ", \
72 bh->b_bdev, (unsigned long) bh->b_blocknr); \
77 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
78 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
81 static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
82 static struct buffer_head *ext4_xattr_cache_find(struct inode *,
83 struct ext4_xattr_header *,
84 struct mb_cache_entry **);
85 static void ext4_xattr_rehash(struct ext4_xattr_header *,
86 struct ext4_xattr_entry *);
87 static int ext4_xattr_list(struct dentry *dentry, char *buffer,
90 static const struct xattr_handler *ext4_xattr_handler_map[] = {
91 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
92 #ifdef CONFIG_EXT4_FS_POSIX_ACL
93 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
94 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
96 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
97 #ifdef CONFIG_EXT4_FS_SECURITY
98 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
102 const struct xattr_handler *ext4_xattr_handlers[] = {
103 &ext4_xattr_user_handler,
104 &ext4_xattr_trusted_handler,
105 #ifdef CONFIG_EXT4_FS_POSIX_ACL
106 &posix_acl_access_xattr_handler,
107 &posix_acl_default_xattr_handler,
109 #ifdef CONFIG_EXT4_FS_SECURITY
110 &ext4_xattr_security_handler,
115 #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
116 inode->i_sb->s_fs_info)->s_mb_cache)
118 static __le32 ext4_xattr_block_csum(struct inode *inode,
120 struct ext4_xattr_header *hdr)
122 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
124 __le64 dsk_block_nr = cpu_to_le64(block_nr);
125 __u32 dummy_csum = 0;
126 int offset = offsetof(struct ext4_xattr_header, h_checksum);
128 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
129 sizeof(dsk_block_nr));
130 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
131 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
132 offset += sizeof(dummy_csum);
133 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
134 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
136 return cpu_to_le32(csum);
139 static int ext4_xattr_block_csum_verify(struct inode *inode,
141 struct ext4_xattr_header *hdr)
143 if (ext4_has_metadata_csum(inode->i_sb) &&
144 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
149 static void ext4_xattr_block_csum_set(struct inode *inode,
151 struct ext4_xattr_header *hdr)
153 if (!ext4_has_metadata_csum(inode->i_sb))
156 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
159 static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
161 struct buffer_head *bh)
163 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
164 return ext4_handle_dirty_metadata(handle, inode, bh);
167 static inline const struct xattr_handler *
168 ext4_xattr_handler(int name_index)
170 const struct xattr_handler *handler = NULL;
172 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
173 handler = ext4_xattr_handler_map[name_index];
178 * Inode operation listxattr()
180 * d_inode(dentry)->i_mutex: don't care
183 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
185 return ext4_xattr_list(dentry, buffer, size);
189 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
192 struct ext4_xattr_entry *e = entry;
194 while (!IS_LAST_ENTRY(e)) {
195 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
196 if ((void *)next >= end)
197 return -EFSCORRUPTED;
201 while (!IS_LAST_ENTRY(entry)) {
202 if (entry->e_value_size != 0 &&
203 (value_start + le16_to_cpu(entry->e_value_offs) <
204 (void *)e + sizeof(__u32) ||
205 value_start + le16_to_cpu(entry->e_value_offs) +
206 le32_to_cpu(entry->e_value_size) > end))
207 return -EFSCORRUPTED;
208 entry = EXT4_XATTR_NEXT(entry);
215 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
219 if (buffer_verified(bh))
222 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
223 BHDR(bh)->h_blocks != cpu_to_le32(1))
224 return -EFSCORRUPTED;
225 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
227 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
230 set_buffer_verified(bh);
235 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
236 void *end, const char *function, unsigned int line)
238 struct ext4_xattr_entry *entry = IFIRST(header);
239 int error = -EFSCORRUPTED;
241 if (((void *) header >= end) ||
242 (header->h_magic != le32_to_cpu(EXT4_XATTR_MAGIC)))
244 error = ext4_xattr_check_names(entry, end, entry);
247 __ext4_error_inode(inode, function, line, 0,
248 "corrupted in-inode xattr");
252 #define xattr_check_inode(inode, header, end) \
253 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
256 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
258 size_t value_size = le32_to_cpu(entry->e_value_size);
260 if (entry->e_value_block != 0 || value_size > size ||
261 le16_to_cpu(entry->e_value_offs) + value_size > size)
262 return -EFSCORRUPTED;
267 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
268 const char *name, size_t size, int sorted)
270 struct ext4_xattr_entry *entry;
276 name_len = strlen(name);
278 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
279 cmp = name_index - entry->e_name_index;
281 cmp = name_len - entry->e_name_len;
283 cmp = memcmp(name, entry->e_name, name_len);
284 if (cmp <= 0 && (sorted || cmp == 0))
288 if (!cmp && ext4_xattr_check_entry(entry, size))
289 return -EFSCORRUPTED;
290 return cmp ? -ENODATA : 0;
294 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
295 void *buffer, size_t buffer_size)
297 struct buffer_head *bh = NULL;
298 struct ext4_xattr_entry *entry;
301 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
303 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
304 name_index, name, buffer, (long)buffer_size);
307 if (!EXT4_I(inode)->i_file_acl)
309 ea_idebug(inode, "reading block %llu",
310 (unsigned long long)EXT4_I(inode)->i_file_acl);
311 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
314 ea_bdebug(bh, "b_count=%d, refcount=%d",
315 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
316 if (ext4_xattr_check_block(inode, bh)) {
318 EXT4_ERROR_INODE(inode, "bad block %llu",
319 EXT4_I(inode)->i_file_acl);
320 error = -EFSCORRUPTED;
323 ext4_xattr_cache_insert(ext4_mb_cache, bh);
325 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
326 if (error == -EFSCORRUPTED)
330 size = le32_to_cpu(entry->e_value_size);
333 if (size > buffer_size)
335 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
346 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
347 void *buffer, size_t buffer_size)
349 struct ext4_xattr_ibody_header *header;
350 struct ext4_xattr_entry *entry;
351 struct ext4_inode *raw_inode;
352 struct ext4_iloc iloc;
357 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
359 error = ext4_get_inode_loc(inode, &iloc);
362 raw_inode = ext4_raw_inode(&iloc);
363 header = IHDR(inode, raw_inode);
364 entry = IFIRST(header);
365 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
366 error = xattr_check_inode(inode, header, end);
369 error = ext4_xattr_find_entry(&entry, name_index, name,
370 end - (void *)entry, 0);
373 size = le32_to_cpu(entry->e_value_size);
376 if (size > buffer_size)
378 memcpy(buffer, (void *)IFIRST(header) +
379 le16_to_cpu(entry->e_value_offs), size);
391 * Copy an extended attribute into the buffer
392 * provided, or compute the buffer size required.
393 * Buffer is NULL to compute the size of the buffer required.
395 * Returns a negative error number on failure, or the number of bytes
396 * used / required on success.
399 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
400 void *buffer, size_t buffer_size)
404 if (strlen(name) > 255)
407 down_read(&EXT4_I(inode)->xattr_sem);
408 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
410 if (error == -ENODATA)
411 error = ext4_xattr_block_get(inode, name_index, name, buffer,
413 up_read(&EXT4_I(inode)->xattr_sem);
418 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
419 char *buffer, size_t buffer_size)
421 size_t rest = buffer_size;
423 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
424 const struct xattr_handler *handler =
425 ext4_xattr_handler(entry->e_name_index);
427 if (handler && (!handler->list || handler->list(dentry))) {
428 const char *prefix = handler->prefix ?: handler->name;
429 size_t prefix_len = strlen(prefix);
430 size_t size = prefix_len + entry->e_name_len + 1;
435 memcpy(buffer, prefix, prefix_len);
436 buffer += prefix_len;
437 memcpy(buffer, entry->e_name, entry->e_name_len);
438 buffer += entry->e_name_len;
444 return buffer_size - rest; /* total size */
448 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
450 struct inode *inode = d_inode(dentry);
451 struct buffer_head *bh = NULL;
453 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
455 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
456 buffer, (long)buffer_size);
459 if (!EXT4_I(inode)->i_file_acl)
461 ea_idebug(inode, "reading block %llu",
462 (unsigned long long)EXT4_I(inode)->i_file_acl);
463 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
467 ea_bdebug(bh, "b_count=%d, refcount=%d",
468 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
469 if (ext4_xattr_check_block(inode, bh)) {
470 EXT4_ERROR_INODE(inode, "bad block %llu",
471 EXT4_I(inode)->i_file_acl);
472 error = -EFSCORRUPTED;
475 ext4_xattr_cache_insert(ext4_mb_cache, bh);
476 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
485 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
487 struct inode *inode = d_inode(dentry);
488 struct ext4_xattr_ibody_header *header;
489 struct ext4_inode *raw_inode;
490 struct ext4_iloc iloc;
494 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
496 error = ext4_get_inode_loc(inode, &iloc);
499 raw_inode = ext4_raw_inode(&iloc);
500 header = IHDR(inode, raw_inode);
501 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
502 error = xattr_check_inode(inode, header, end);
505 error = ext4_xattr_list_entries(dentry, IFIRST(header),
506 buffer, buffer_size);
516 * Copy a list of attribute names into the buffer
517 * provided, or compute the buffer size required.
518 * Buffer is NULL to compute the size of the buffer required.
520 * Returns a negative error number on failure, or the number of bytes
521 * used / required on success.
524 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
528 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
529 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
536 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
541 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
546 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
549 static void ext4_xattr_update_super_block(handle_t *handle,
550 struct super_block *sb)
552 if (ext4_has_feature_xattr(sb))
555 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
556 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
557 ext4_set_feature_xattr(sb);
558 ext4_handle_dirty_super(handle, sb);
563 * Release the xattr block BH: If the reference count is > 1, decrement it;
564 * otherwise free the block.
567 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
568 struct buffer_head *bh)
570 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
574 BUFFER_TRACE(bh, "get_write_access");
575 error = ext4_journal_get_write_access(handle, bh);
580 hash = le32_to_cpu(BHDR(bh)->h_hash);
581 ref = le32_to_cpu(BHDR(bh)->h_refcount);
583 ea_bdebug(bh, "refcount now=0; freeing");
585 * This must happen under buffer lock for
586 * ext4_xattr_block_set() to reliably detect freed block
588 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
591 ext4_free_blocks(handle, inode, bh, 0, 1,
592 EXT4_FREE_BLOCKS_METADATA |
593 EXT4_FREE_BLOCKS_FORGET);
596 BHDR(bh)->h_refcount = cpu_to_le32(ref);
597 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
598 struct mb_cache_entry *ce;
600 ce = mb_cache_entry_get(ext4_mb_cache, hash,
604 mb_cache_entry_put(ext4_mb_cache, ce);
609 * Beware of this ugliness: Releasing of xattr block references
610 * from different inodes can race and so we have to protect
611 * from a race where someone else frees the block (and releases
612 * its journal_head) before we are done dirtying the buffer. In
613 * nojournal mode this race is harmless and we actually cannot
614 * call ext4_handle_dirty_xattr_block() with locked buffer as
615 * that function can call sync_dirty_buffer() so for that case
616 * we handle the dirtying after unlocking the buffer.
618 if (ext4_handle_valid(handle))
619 error = ext4_handle_dirty_xattr_block(handle, inode,
622 if (!ext4_handle_valid(handle))
623 error = ext4_handle_dirty_xattr_block(handle, inode,
626 ext4_handle_sync(handle);
627 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
628 ea_bdebug(bh, "refcount now=%d; releasing",
629 le32_to_cpu(BHDR(bh)->h_refcount));
632 ext4_std_error(inode->i_sb, error);
637 * Find the available free space for EAs. This also returns the total number of
638 * bytes used by EA entries.
640 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
641 size_t *min_offs, void *base, int *total)
643 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
644 if (!last->e_value_block && last->e_value_size) {
645 size_t offs = le16_to_cpu(last->e_value_offs);
646 if (offs < *min_offs)
650 *total += EXT4_XATTR_LEN(last->e_name_len);
652 return (*min_offs - ((void *)last - base) - sizeof(__u32));
656 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
658 struct ext4_xattr_entry *last;
659 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
661 /* Compute min_offs and last. */
663 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
664 if (!last->e_value_block && last->e_value_size) {
665 size_t offs = le16_to_cpu(last->e_value_offs);
670 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
672 if (!s->here->e_value_block && s->here->e_value_size) {
673 size_t size = le32_to_cpu(s->here->e_value_size);
674 free += EXT4_XATTR_SIZE(size);
676 free += EXT4_XATTR_LEN(name_len);
679 if (free < EXT4_XATTR_LEN(name_len) +
680 EXT4_XATTR_SIZE(i->value_len))
684 if (i->value && s->not_found) {
685 /* Insert the new name. */
686 size_t size = EXT4_XATTR_LEN(name_len);
687 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
688 memmove((void *)s->here + size, s->here, rest);
689 memset(s->here, 0, size);
690 s->here->e_name_index = i->name_index;
691 s->here->e_name_len = name_len;
692 memcpy(s->here->e_name, i->name, name_len);
694 if (!s->here->e_value_block && s->here->e_value_size) {
695 void *first_val = s->base + min_offs;
696 size_t offs = le16_to_cpu(s->here->e_value_offs);
697 void *val = s->base + offs;
698 size_t size = EXT4_XATTR_SIZE(
699 le32_to_cpu(s->here->e_value_size));
701 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
702 /* The old and the new value have the same
703 size. Just replace. */
704 s->here->e_value_size =
705 cpu_to_le32(i->value_len);
706 if (i->value == EXT4_ZERO_XATTR_VALUE) {
707 memset(val, 0, size);
709 /* Clear pad bytes first. */
710 memset(val + size - EXT4_XATTR_PAD, 0,
712 memcpy(val, i->value, i->value_len);
717 /* Remove the old value. */
718 memmove(first_val + size, first_val, val - first_val);
719 memset(first_val, 0, size);
720 s->here->e_value_size = 0;
721 s->here->e_value_offs = 0;
724 /* Adjust all value offsets. */
726 while (!IS_LAST_ENTRY(last)) {
727 size_t o = le16_to_cpu(last->e_value_offs);
728 if (!last->e_value_block &&
729 last->e_value_size && o < offs)
731 cpu_to_le16(o + size);
732 last = EXT4_XATTR_NEXT(last);
736 /* Remove the old name. */
737 size_t size = EXT4_XATTR_LEN(name_len);
738 last = ENTRY((void *)last - size);
739 memmove(s->here, (void *)s->here + size,
740 (void *)last - (void *)s->here + sizeof(__u32));
741 memset(last, 0, size);
746 /* Insert the new value. */
747 s->here->e_value_size = cpu_to_le32(i->value_len);
749 size_t size = EXT4_XATTR_SIZE(i->value_len);
750 void *val = s->base + min_offs - size;
751 s->here->e_value_offs = cpu_to_le16(min_offs - size);
752 if (i->value == EXT4_ZERO_XATTR_VALUE) {
753 memset(val, 0, size);
755 /* Clear the pad bytes first. */
756 memset(val + size - EXT4_XATTR_PAD, 0,
758 memcpy(val, i->value, i->value_len);
765 struct ext4_xattr_block_find {
766 struct ext4_xattr_search s;
767 struct buffer_head *bh;
771 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
772 struct ext4_xattr_block_find *bs)
774 struct super_block *sb = inode->i_sb;
777 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
778 i->name_index, i->name, i->value, (long)i->value_len);
780 if (EXT4_I(inode)->i_file_acl) {
781 /* The inode already has an extended attribute block. */
782 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
786 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
787 atomic_read(&(bs->bh->b_count)),
788 le32_to_cpu(BHDR(bs->bh)->h_refcount));
789 if (ext4_xattr_check_block(inode, bs->bh)) {
790 EXT4_ERROR_INODE(inode, "bad block %llu",
791 EXT4_I(inode)->i_file_acl);
792 error = -EFSCORRUPTED;
795 /* Find the named attribute. */
796 bs->s.base = BHDR(bs->bh);
797 bs->s.first = BFIRST(bs->bh);
798 bs->s.end = bs->bh->b_data + bs->bh->b_size;
799 bs->s.here = bs->s.first;
800 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
801 i->name, bs->bh->b_size, 1);
802 if (error && error != -ENODATA)
804 bs->s.not_found = error;
813 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
814 struct ext4_xattr_info *i,
815 struct ext4_xattr_block_find *bs)
817 struct super_block *sb = inode->i_sb;
818 struct buffer_head *new_bh = NULL;
819 struct ext4_xattr_search *s = &bs->s;
820 struct mb_cache_entry *ce = NULL;
822 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
824 #define header(x) ((struct ext4_xattr_header *)(x))
826 if (i->value && i->value_len > sb->s_blocksize)
829 BUFFER_TRACE(bs->bh, "get_write_access");
830 error = ext4_journal_get_write_access(handle, bs->bh);
835 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
836 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
839 * This must happen under buffer lock for
840 * ext4_xattr_block_set() to reliably detect modified
843 mb_cache_entry_delete_block(ext4_mb_cache, hash,
845 ea_bdebug(bs->bh, "modifying in-place");
846 error = ext4_xattr_set_entry(i, s);
848 if (!IS_LAST_ENTRY(s->first))
849 ext4_xattr_rehash(header(s->base),
851 ext4_xattr_cache_insert(ext4_mb_cache,
854 unlock_buffer(bs->bh);
855 if (error == -EFSCORRUPTED)
858 error = ext4_handle_dirty_xattr_block(handle,
865 int offset = (char *)s->here - bs->bh->b_data;
867 unlock_buffer(bs->bh);
868 ea_bdebug(bs->bh, "cloning");
869 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
873 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
874 s->first = ENTRY(header(s->base)+1);
875 header(s->base)->h_refcount = cpu_to_le32(1);
876 s->here = ENTRY(s->base + offset);
877 s->end = s->base + bs->bh->b_size;
880 /* Allocate a buffer where we construct the new block. */
881 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
882 /* assert(header == s->base) */
886 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
887 header(s->base)->h_blocks = cpu_to_le32(1);
888 header(s->base)->h_refcount = cpu_to_le32(1);
889 s->first = ENTRY(header(s->base)+1);
890 s->here = ENTRY(header(s->base)+1);
891 s->end = s->base + sb->s_blocksize;
894 error = ext4_xattr_set_entry(i, s);
895 if (error == -EFSCORRUPTED)
899 if (!IS_LAST_ENTRY(s->first))
900 ext4_xattr_rehash(header(s->base), s->here);
903 if (!IS_LAST_ENTRY(s->first)) {
904 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
906 /* We found an identical block in the cache. */
907 if (new_bh == bs->bh)
908 ea_bdebug(new_bh, "keeping");
912 /* The old block is released after updating
914 error = dquot_alloc_block(inode,
915 EXT4_C2B(EXT4_SB(sb), 1));
918 BUFFER_TRACE(new_bh, "get_write_access");
919 error = ext4_journal_get_write_access(handle,
925 * We have to be careful about races with
926 * freeing, rehashing or adding references to
927 * xattr block. Once we hold buffer lock xattr
928 * block's state is stable so we can check
929 * whether the block got freed / rehashed or
930 * not. Since we unhash mbcache entry under
931 * buffer lock when freeing / rehashing xattr
932 * block, checking whether entry is still
933 * hashed is reliable. Same rules hold for
934 * e_reusable handling.
936 if (hlist_bl_unhashed(&ce->e_hash_list) ||
939 * Undo everything and check mbcache
942 unlock_buffer(new_bh);
943 dquot_free_block(inode,
944 EXT4_C2B(EXT4_SB(sb),
947 mb_cache_entry_put(ext4_mb_cache, ce);
952 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
953 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
954 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
956 ea_bdebug(new_bh, "reusing; refcount now=%d",
958 unlock_buffer(new_bh);
959 error = ext4_handle_dirty_xattr_block(handle,
965 mb_cache_entry_touch(ext4_mb_cache, ce);
966 mb_cache_entry_put(ext4_mb_cache, ce);
968 } else if (bs->bh && s->base == bs->bh->b_data) {
969 /* We were modifying this block in-place. */
970 ea_bdebug(bs->bh, "keeping this block");
974 /* We need to allocate a new block */
975 ext4_fsblk_t goal, block;
977 goal = ext4_group_first_block_no(sb,
978 EXT4_I(inode)->i_block_group);
980 /* non-extent files can't have physical blocks past 2^32 */
981 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
982 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
984 block = ext4_new_meta_blocks(handle, inode, goal, 0,
989 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
990 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
992 ea_idebug(inode, "creating block %llu",
993 (unsigned long long)block);
995 new_bh = sb_getblk(sb, block);
996 if (unlikely(!new_bh)) {
999 ext4_free_blocks(handle, inode, NULL, block, 1,
1000 EXT4_FREE_BLOCKS_METADATA);
1003 lock_buffer(new_bh);
1004 error = ext4_journal_get_create_access(handle, new_bh);
1006 unlock_buffer(new_bh);
1010 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1011 set_buffer_uptodate(new_bh);
1012 unlock_buffer(new_bh);
1013 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
1014 error = ext4_handle_dirty_xattr_block(handle,
1021 /* Update the inode. */
1022 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
1024 /* Drop the previous xattr block. */
1025 if (bs->bh && bs->bh != new_bh)
1026 ext4_xattr_release_block(handle, inode, bs->bh);
1031 mb_cache_entry_put(ext4_mb_cache, ce);
1033 if (!(bs->bh && s->base == bs->bh->b_data))
1039 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
1043 EXT4_ERROR_INODE(inode, "bad block %llu",
1044 EXT4_I(inode)->i_file_acl);
1050 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1051 struct ext4_xattr_ibody_find *is)
1053 struct ext4_xattr_ibody_header *header;
1054 struct ext4_inode *raw_inode;
1057 if (EXT4_I(inode)->i_extra_isize == 0)
1059 raw_inode = ext4_raw_inode(&is->iloc);
1060 header = IHDR(inode, raw_inode);
1061 is->s.base = is->s.first = IFIRST(header);
1062 is->s.here = is->s.first;
1063 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1064 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
1065 error = xattr_check_inode(inode, header, is->s.end);
1068 /* Find the named attribute. */
1069 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
1070 i->name, is->s.end -
1071 (void *)is->s.base, 0);
1072 if (error && error != -ENODATA)
1074 is->s.not_found = error;
1079 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1080 struct ext4_xattr_info *i,
1081 struct ext4_xattr_ibody_find *is)
1083 struct ext4_xattr_ibody_header *header;
1084 struct ext4_xattr_search *s = &is->s;
1087 if (EXT4_I(inode)->i_extra_isize == 0)
1089 error = ext4_xattr_set_entry(i, s);
1091 if (error == -ENOSPC &&
1092 ext4_has_inline_data(inode)) {
1093 error = ext4_try_to_evict_inline_data(handle, inode,
1094 EXT4_XATTR_LEN(strlen(i->name) +
1095 EXT4_XATTR_SIZE(i->value_len)));
1098 error = ext4_xattr_ibody_find(inode, i, is);
1101 error = ext4_xattr_set_entry(i, s);
1106 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1107 if (!IS_LAST_ENTRY(s->first)) {
1108 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1109 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1111 header->h_magic = cpu_to_le32(0);
1112 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1117 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1118 struct ext4_xattr_info *i,
1119 struct ext4_xattr_ibody_find *is)
1121 struct ext4_xattr_ibody_header *header;
1122 struct ext4_xattr_search *s = &is->s;
1125 if (EXT4_I(inode)->i_extra_isize == 0)
1127 error = ext4_xattr_set_entry(i, s);
1130 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1131 if (!IS_LAST_ENTRY(s->first)) {
1132 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1133 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1135 header->h_magic = cpu_to_le32(0);
1136 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1141 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1142 struct ext4_xattr_info *i)
1146 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1148 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1149 return !memcmp(value, i->value, i->value_len);
1153 * ext4_xattr_set_handle()
1155 * Create, replace or remove an extended attribute for this inode. Value
1156 * is NULL to remove an existing extended attribute, and non-NULL to
1157 * either replace an existing extended attribute, or create a new extended
1158 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1159 * specify that an extended attribute must exist and must not exist
1160 * previous to the call, respectively.
1162 * Returns 0, or a negative error number on failure.
1165 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1166 const char *name, const void *value, size_t value_len,
1169 struct ext4_xattr_info i = {
1170 .name_index = name_index,
1173 .value_len = value_len,
1176 struct ext4_xattr_ibody_find is = {
1177 .s = { .not_found = -ENODATA, },
1179 struct ext4_xattr_block_find bs = {
1180 .s = { .not_found = -ENODATA, },
1182 unsigned long no_expand;
1187 if (strlen(name) > 255)
1189 down_write(&EXT4_I(inode)->xattr_sem);
1190 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1191 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1193 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1197 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1198 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1199 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1200 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1203 error = ext4_xattr_ibody_find(inode, &i, &is);
1207 error = ext4_xattr_block_find(inode, &i, &bs);
1210 if (is.s.not_found && bs.s.not_found) {
1212 if (flags & XATTR_REPLACE)
1219 if (flags & XATTR_CREATE)
1223 if (!is.s.not_found)
1224 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1225 else if (!bs.s.not_found)
1226 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1229 /* Xattr value did not change? Save us some work and bail out */
1230 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1232 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1235 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1236 if (!error && !bs.s.not_found) {
1238 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1239 } else if (error == -ENOSPC) {
1240 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1241 error = ext4_xattr_block_find(inode, &i, &bs);
1245 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1248 if (!is.s.not_found) {
1250 error = ext4_xattr_ibody_set(handle, inode, &i,
1256 ext4_xattr_update_super_block(handle, inode->i_sb);
1257 inode->i_ctime = ext4_current_time(inode);
1259 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1260 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1262 * The bh is consumed by ext4_mark_iloc_dirty, even with
1267 ext4_handle_sync(handle);
1274 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1275 up_write(&EXT4_I(inode)->xattr_sem);
1282 * Like ext4_xattr_set_handle, but start from an inode. This extended
1283 * attribute modification is a filesystem transaction by itself.
1285 * Returns 0, or a negative error number on failure.
1288 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1289 const void *value, size_t value_len, int flags)
1292 int error, retries = 0;
1293 int credits = ext4_jbd2_credits_xattr(inode);
1296 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1297 if (IS_ERR(handle)) {
1298 error = PTR_ERR(handle);
1302 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1303 value, value_len, flags);
1304 error2 = ext4_journal_stop(handle);
1305 if (error == -ENOSPC &&
1306 ext4_should_retry_alloc(inode->i_sb, &retries))
1316 * Shift the EA entries in the inode to create space for the increased
1319 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1320 int value_offs_shift, void *to,
1321 void *from, size_t n, int blocksize)
1323 struct ext4_xattr_entry *last = entry;
1326 /* Adjust the value offsets of the entries */
1327 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1328 if (!last->e_value_block && last->e_value_size) {
1329 new_offs = le16_to_cpu(last->e_value_offs) +
1331 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1333 last->e_value_offs = cpu_to_le16(new_offs);
1336 /* Shift the entries by n bytes */
1337 memmove(to, from, n);
1341 * Expand an inode by new_extra_isize bytes when EAs are present.
1342 * Returns 0 on success or negative error number on failure.
1344 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1345 struct ext4_inode *raw_inode, handle_t *handle)
1347 struct ext4_xattr_ibody_header *header;
1348 struct ext4_xattr_entry *entry, *last, *first;
1349 struct buffer_head *bh = NULL;
1350 struct ext4_xattr_ibody_find *is = NULL;
1351 struct ext4_xattr_block_find *bs = NULL;
1352 char *buffer = NULL, *b_entry_name = NULL;
1353 size_t min_offs, free;
1355 void *base, *start, *end;
1356 int error = 0, tried_min_extra_isize = 0;
1357 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1358 int isize_diff; /* How much do we need to grow i_extra_isize */
1360 down_write(&EXT4_I(inode)->xattr_sem);
1362 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1364 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1366 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
1367 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1370 header = IHDR(inode, raw_inode);
1371 entry = IFIRST(header);
1374 * Check if enough free space is available in the inode to shift the
1375 * entries ahead by new_extra_isize.
1378 base = start = entry;
1379 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1380 min_offs = end - base;
1382 total_ino = sizeof(struct ext4_xattr_ibody_header);
1384 error = xattr_check_inode(inode, header, end);
1388 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1389 if (free >= isize_diff) {
1390 entry = IFIRST(header);
1391 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1392 - new_extra_isize, (void *)raw_inode +
1393 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1394 (void *)header, total_ino,
1395 inode->i_sb->s_blocksize);
1396 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1401 * Enough free space isn't available in the inode, check if
1402 * EA block can hold new_extra_isize bytes.
1404 if (EXT4_I(inode)->i_file_acl) {
1405 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1409 if (ext4_xattr_check_block(inode, bh)) {
1410 EXT4_ERROR_INODE(inode, "bad block %llu",
1411 EXT4_I(inode)->i_file_acl);
1412 error = -EFSCORRUPTED;
1417 end = bh->b_data + bh->b_size;
1418 min_offs = end - base;
1419 free = ext4_xattr_free_space(first, &min_offs, base, NULL);
1420 if (free < isize_diff) {
1421 if (!tried_min_extra_isize && s_min_extra_isize) {
1422 tried_min_extra_isize++;
1423 new_extra_isize = s_min_extra_isize;
1431 free = inode->i_sb->s_blocksize;
1434 while (isize_diff > 0) {
1435 size_t offs, size, entry_size;
1436 struct ext4_xattr_entry *small_entry = NULL;
1437 struct ext4_xattr_info i = {
1441 unsigned int total_size; /* EA entry size + value size */
1442 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1443 unsigned int min_total_size = ~0U;
1445 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1446 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1452 is->s.not_found = -ENODATA;
1453 bs->s.not_found = -ENODATA;
1457 last = IFIRST(header);
1458 /* Find the entry best suited to be pushed into EA block */
1460 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1462 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1463 EXT4_XATTR_LEN(last->e_name_len);
1464 if (total_size <= free && total_size < min_total_size) {
1465 if (total_size < isize_diff) {
1469 min_total_size = total_size;
1474 if (entry == NULL) {
1476 entry = small_entry;
1478 if (!tried_min_extra_isize &&
1479 s_min_extra_isize) {
1480 tried_min_extra_isize++;
1481 new_extra_isize = s_min_extra_isize;
1482 kfree(is); is = NULL;
1483 kfree(bs); bs = NULL;
1491 offs = le16_to_cpu(entry->e_value_offs);
1492 size = le32_to_cpu(entry->e_value_size);
1493 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1494 i.name_index = entry->e_name_index,
1495 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1496 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1497 if (!buffer || !b_entry_name) {
1501 /* Save the entry name and the entry value */
1502 memcpy(buffer, (void *)IFIRST(header) + offs,
1503 EXT4_XATTR_SIZE(size));
1504 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1505 b_entry_name[entry->e_name_len] = '\0';
1506 i.name = b_entry_name;
1508 error = ext4_get_inode_loc(inode, &is->iloc);
1512 error = ext4_xattr_ibody_find(inode, &i, is);
1516 /* Remove the chosen entry from the inode */
1517 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1520 total_ino -= entry_size;
1522 entry = IFIRST(header);
1523 if (entry_size + EXT4_XATTR_SIZE(size) >= isize_diff)
1524 shift_bytes = isize_diff;
1526 shift_bytes = entry_size + EXT4_XATTR_SIZE(size);
1527 /* Adjust the offsets and shift the remaining entries ahead */
1528 ext4_xattr_shift_entries(entry, -shift_bytes,
1529 (void *)raw_inode + EXT4_GOOD_OLD_INODE_SIZE +
1530 EXT4_I(inode)->i_extra_isize + shift_bytes,
1531 (void *)header, total_ino, inode->i_sb->s_blocksize);
1533 isize_diff -= shift_bytes;
1534 EXT4_I(inode)->i_extra_isize += shift_bytes;
1535 header = IHDR(inode, raw_inode);
1537 i.name = b_entry_name;
1540 error = ext4_xattr_block_find(inode, &i, bs);
1544 /* Add entry which was removed from the inode into the block */
1545 error = ext4_xattr_block_set(handle, inode, &i, bs);
1548 kfree(b_entry_name);
1550 b_entry_name = NULL;
1552 brelse(is->iloc.bh);
1558 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1559 up_write(&EXT4_I(inode)->xattr_sem);
1563 kfree(b_entry_name);
1566 brelse(is->iloc.bh);
1571 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1572 * size expansion failed.
1574 up_write(&EXT4_I(inode)->xattr_sem);
1581 * ext4_xattr_delete_inode()
1583 * Free extended attribute resources associated with this inode. This
1584 * is called immediately before an inode is freed. We have exclusive
1585 * access to the inode.
1588 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1590 struct buffer_head *bh = NULL;
1592 if (!EXT4_I(inode)->i_file_acl)
1594 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1596 EXT4_ERROR_INODE(inode, "block %llu read error",
1597 EXT4_I(inode)->i_file_acl);
1600 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1601 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1602 EXT4_ERROR_INODE(inode, "bad block %llu",
1603 EXT4_I(inode)->i_file_acl);
1606 ext4_xattr_release_block(handle, inode, bh);
1607 EXT4_I(inode)->i_file_acl = 0;
1614 * ext4_xattr_cache_insert()
1616 * Create a new entry in the extended attribute cache, and insert
1617 * it unless such an entry is already in the cache.
1619 * Returns 0, or a negative error number on failure.
1622 ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
1624 struct ext4_xattr_header *header = BHDR(bh);
1625 __u32 hash = le32_to_cpu(header->h_hash);
1626 int reusable = le32_to_cpu(header->h_refcount) <
1627 EXT4_XATTR_REFCOUNT_MAX;
1630 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
1631 bh->b_blocknr, reusable);
1633 if (error == -EBUSY)
1634 ea_bdebug(bh, "already in cache");
1636 ea_bdebug(bh, "inserting [%x]", (int)hash);
1642 * Compare two extended attribute blocks for equality.
1644 * Returns 0 if the blocks are equal, 1 if they differ, and
1645 * a negative error number on errors.
1648 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1649 struct ext4_xattr_header *header2)
1651 struct ext4_xattr_entry *entry1, *entry2;
1653 entry1 = ENTRY(header1+1);
1654 entry2 = ENTRY(header2+1);
1655 while (!IS_LAST_ENTRY(entry1)) {
1656 if (IS_LAST_ENTRY(entry2))
1658 if (entry1->e_hash != entry2->e_hash ||
1659 entry1->e_name_index != entry2->e_name_index ||
1660 entry1->e_name_len != entry2->e_name_len ||
1661 entry1->e_value_size != entry2->e_value_size ||
1662 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1664 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1665 return -EFSCORRUPTED;
1666 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1667 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1668 le32_to_cpu(entry1->e_value_size)))
1671 entry1 = EXT4_XATTR_NEXT(entry1);
1672 entry2 = EXT4_XATTR_NEXT(entry2);
1674 if (!IS_LAST_ENTRY(entry2))
1680 * ext4_xattr_cache_find()
1682 * Find an identical extended attribute block.
1684 * Returns a pointer to the block found, or NULL if such a block was
1685 * not found or an error occurred.
1687 static struct buffer_head *
1688 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1689 struct mb_cache_entry **pce)
1691 __u32 hash = le32_to_cpu(header->h_hash);
1692 struct mb_cache_entry *ce;
1693 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1695 if (!header->h_hash)
1696 return NULL; /* never share */
1697 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1698 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
1700 struct buffer_head *bh;
1702 bh = sb_bread(inode->i_sb, ce->e_block);
1704 EXT4_ERROR_INODE(inode, "block %lu read error",
1705 (unsigned long) ce->e_block);
1706 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1711 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
1716 #define NAME_HASH_SHIFT 5
1717 #define VALUE_HASH_SHIFT 16
1720 * ext4_xattr_hash_entry()
1722 * Compute the hash of an extended attribute.
1724 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1725 struct ext4_xattr_entry *entry)
1728 char *name = entry->e_name;
1731 for (n = 0; n < entry->e_name_len; n++) {
1732 hash = (hash << NAME_HASH_SHIFT) ^
1733 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1737 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1738 __le32 *value = (__le32 *)((char *)header +
1739 le16_to_cpu(entry->e_value_offs));
1740 for (n = (le32_to_cpu(entry->e_value_size) +
1741 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1742 hash = (hash << VALUE_HASH_SHIFT) ^
1743 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1744 le32_to_cpu(*value++);
1747 entry->e_hash = cpu_to_le32(hash);
1750 #undef NAME_HASH_SHIFT
1751 #undef VALUE_HASH_SHIFT
1753 #define BLOCK_HASH_SHIFT 16
1756 * ext4_xattr_rehash()
1758 * Re-compute the extended attribute hash value after an entry has changed.
1760 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1761 struct ext4_xattr_entry *entry)
1763 struct ext4_xattr_entry *here;
1766 ext4_xattr_hash_entry(header, entry);
1767 here = ENTRY(header+1);
1768 while (!IS_LAST_ENTRY(here)) {
1769 if (!here->e_hash) {
1770 /* Block is not shared if an entry's hash value == 0 */
1774 hash = (hash << BLOCK_HASH_SHIFT) ^
1775 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1776 le32_to_cpu(here->e_hash);
1777 here = EXT4_XATTR_NEXT(here);
1779 header->h_hash = cpu_to_le32(hash);
1782 #undef BLOCK_HASH_SHIFT
1784 #define HASH_BUCKET_BITS 10
1787 ext4_xattr_create_cache(void)
1789 return mb_cache_create(HASH_BUCKET_BITS);
1792 void ext4_xattr_destroy_cache(struct mb_cache *cache)
1795 mb_cache_destroy(cache);