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, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67 # define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
71 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
75 static void ext4_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
76 static struct buffer_head *ext4_xattr_cache_find(struct inode *,
77 struct ext4_xattr_header *,
78 struct mb_cache_entry **);
79 static void ext4_xattr_rehash(struct ext4_xattr_header *,
80 struct ext4_xattr_entry *);
81 static int ext4_xattr_list(struct dentry *dentry, char *buffer,
84 static const struct xattr_handler *ext4_xattr_handler_map[] = {
85 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
86 #ifdef CONFIG_EXT4_FS_POSIX_ACL
87 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
88 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
90 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
91 #ifdef CONFIG_EXT4_FS_SECURITY
92 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
96 const struct xattr_handler *ext4_xattr_handlers[] = {
97 &ext4_xattr_user_handler,
98 &ext4_xattr_trusted_handler,
99 #ifdef CONFIG_EXT4_FS_POSIX_ACL
100 &posix_acl_access_xattr_handler,
101 &posix_acl_default_xattr_handler,
103 #ifdef CONFIG_EXT4_FS_SECURITY
104 &ext4_xattr_security_handler,
109 #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_mb_cache)
112 static __le32 ext4_xattr_block_csum(struct inode *inode,
114 struct ext4_xattr_header *hdr)
116 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
118 __le64 dsk_block_nr = cpu_to_le64(block_nr);
119 __u32 dummy_csum = 0;
120 int offset = offsetof(struct ext4_xattr_header, h_checksum);
122 csum = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&dsk_block_nr,
123 sizeof(dsk_block_nr));
124 csum = ext4_chksum(sbi, csum, (__u8 *)hdr, offset);
125 csum = ext4_chksum(sbi, csum, (__u8 *)&dummy_csum, sizeof(dummy_csum));
126 offset += sizeof(dummy_csum);
127 csum = ext4_chksum(sbi, csum, (__u8 *)hdr + offset,
128 EXT4_BLOCK_SIZE(inode->i_sb) - offset);
130 return cpu_to_le32(csum);
133 static int ext4_xattr_block_csum_verify(struct inode *inode,
134 struct buffer_head *bh)
136 struct ext4_xattr_header *hdr = BHDR(bh);
139 if (ext4_has_metadata_csum(inode->i_sb)) {
141 ret = (hdr->h_checksum == ext4_xattr_block_csum(inode,
142 bh->b_blocknr, hdr));
148 static void ext4_xattr_block_csum_set(struct inode *inode,
149 struct buffer_head *bh)
151 if (ext4_has_metadata_csum(inode->i_sb))
152 BHDR(bh)->h_checksum = ext4_xattr_block_csum(inode,
153 bh->b_blocknr, BHDR(bh));
156 static inline const struct xattr_handler *
157 ext4_xattr_handler(int name_index)
159 const struct xattr_handler *handler = NULL;
161 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
162 handler = ext4_xattr_handler_map[name_index];
167 * Inode operation listxattr()
169 * d_inode(dentry)->i_mutex: don't care
172 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
174 return ext4_xattr_list(dentry, buffer, size);
178 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
181 struct ext4_xattr_entry *e = entry;
183 while (!IS_LAST_ENTRY(e)) {
184 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
185 if ((void *)next >= end)
186 return -EFSCORRUPTED;
187 if (strnlen(e->e_name, e->e_name_len) != e->e_name_len)
188 return -EFSCORRUPTED;
192 while (!IS_LAST_ENTRY(entry)) {
193 if (entry->e_value_block != 0)
194 return -EFSCORRUPTED;
195 if (entry->e_value_size != 0 &&
196 (value_start + le16_to_cpu(entry->e_value_offs) <
197 (void *)e + sizeof(__u32) ||
198 value_start + le16_to_cpu(entry->e_value_offs) +
199 le32_to_cpu(entry->e_value_size) > end))
200 return -EFSCORRUPTED;
201 entry = EXT4_XATTR_NEXT(entry);
208 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
212 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
213 BHDR(bh)->h_blocks != cpu_to_le32(1))
214 return -EFSCORRUPTED;
215 if (buffer_verified(bh))
218 if (!ext4_xattr_block_csum_verify(inode, bh))
220 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
223 set_buffer_verified(bh);
228 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
229 void *end, const char *function, unsigned int line)
231 struct ext4_xattr_entry *entry = IFIRST(header);
232 int error = -EFSCORRUPTED;
234 if (((void *) header >= end) ||
235 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
237 error = ext4_xattr_check_names(entry, end, entry);
240 __ext4_error_inode(inode, function, line, 0,
241 "corrupted in-inode xattr");
245 #define xattr_check_inode(inode, header, end) \
246 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
249 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
251 size_t value_size = le32_to_cpu(entry->e_value_size);
253 if (entry->e_value_block != 0 || value_size > size ||
254 le16_to_cpu(entry->e_value_offs) + value_size > size)
255 return -EFSCORRUPTED;
260 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
261 const char *name, size_t size, int sorted)
263 struct ext4_xattr_entry *entry;
269 name_len = strlen(name);
271 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
272 cmp = name_index - entry->e_name_index;
274 cmp = name_len - entry->e_name_len;
276 cmp = memcmp(name, entry->e_name, name_len);
277 if (cmp <= 0 && (sorted || cmp == 0))
281 if (!cmp && ext4_xattr_check_entry(entry, size))
282 return -EFSCORRUPTED;
283 return cmp ? -ENODATA : 0;
287 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
288 void *buffer, size_t buffer_size)
290 struct buffer_head *bh = NULL;
291 struct ext4_xattr_entry *entry;
294 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
296 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
297 name_index, name, buffer, (long)buffer_size);
300 if (!EXT4_I(inode)->i_file_acl)
302 ea_idebug(inode, "reading block %llu",
303 (unsigned long long)EXT4_I(inode)->i_file_acl);
304 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
307 ea_bdebug(bh, "b_count=%d, refcount=%d",
308 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
309 if (ext4_xattr_check_block(inode, bh)) {
311 EXT4_ERROR_INODE(inode, "bad block %llu",
312 EXT4_I(inode)->i_file_acl);
313 error = -EFSCORRUPTED;
316 ext4_xattr_cache_insert(ext4_mb_cache, bh);
318 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
319 if (error == -EFSCORRUPTED)
323 size = le32_to_cpu(entry->e_value_size);
326 if (size > buffer_size)
328 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
339 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
340 void *buffer, size_t buffer_size)
342 struct ext4_xattr_ibody_header *header;
343 struct ext4_xattr_entry *entry;
344 struct ext4_inode *raw_inode;
345 struct ext4_iloc iloc;
350 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
352 error = ext4_get_inode_loc(inode, &iloc);
355 raw_inode = ext4_raw_inode(&iloc);
356 header = IHDR(inode, raw_inode);
357 entry = IFIRST(header);
358 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
359 error = xattr_check_inode(inode, header, end);
362 error = ext4_xattr_find_entry(&entry, name_index, name,
363 end - (void *)entry, 0);
366 size = le32_to_cpu(entry->e_value_size);
369 if (size > buffer_size)
371 memcpy(buffer, (void *)IFIRST(header) +
372 le16_to_cpu(entry->e_value_offs), size);
384 * Copy an extended attribute into the buffer
385 * provided, or compute the buffer size required.
386 * Buffer is NULL to compute the size of the buffer required.
388 * Returns a negative error number on failure, or the number of bytes
389 * used / required on success.
392 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
393 void *buffer, size_t buffer_size)
397 if (strlen(name) > 255)
400 down_read(&EXT4_I(inode)->xattr_sem);
401 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
403 if (error == -ENODATA)
404 error = ext4_xattr_block_get(inode, name_index, name, buffer,
406 up_read(&EXT4_I(inode)->xattr_sem);
411 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
412 char *buffer, size_t buffer_size)
414 size_t rest = buffer_size;
416 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
417 const struct xattr_handler *handler =
418 ext4_xattr_handler(entry->e_name_index);
420 if (handler && (!handler->list || handler->list(dentry))) {
421 const char *prefix = handler->prefix ?: handler->name;
422 size_t prefix_len = strlen(prefix);
423 size_t size = prefix_len + entry->e_name_len + 1;
428 memcpy(buffer, prefix, prefix_len);
429 buffer += prefix_len;
430 memcpy(buffer, entry->e_name, entry->e_name_len);
431 buffer += entry->e_name_len;
437 return buffer_size - rest; /* total size */
441 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
443 struct inode *inode = d_inode(dentry);
444 struct buffer_head *bh = NULL;
446 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
448 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
449 buffer, (long)buffer_size);
452 if (!EXT4_I(inode)->i_file_acl)
454 ea_idebug(inode, "reading block %llu",
455 (unsigned long long)EXT4_I(inode)->i_file_acl);
456 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
460 ea_bdebug(bh, "b_count=%d, refcount=%d",
461 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
462 if (ext4_xattr_check_block(inode, bh)) {
463 EXT4_ERROR_INODE(inode, "bad block %llu",
464 EXT4_I(inode)->i_file_acl);
465 error = -EFSCORRUPTED;
468 ext4_xattr_cache_insert(ext4_mb_cache, bh);
469 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
478 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
480 struct inode *inode = d_inode(dentry);
481 struct ext4_xattr_ibody_header *header;
482 struct ext4_inode *raw_inode;
483 struct ext4_iloc iloc;
487 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
489 error = ext4_get_inode_loc(inode, &iloc);
492 raw_inode = ext4_raw_inode(&iloc);
493 header = IHDR(inode, raw_inode);
494 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
495 error = xattr_check_inode(inode, header, end);
498 error = ext4_xattr_list_entries(dentry, IFIRST(header),
499 buffer, buffer_size);
509 * Copy a list of attribute names into the buffer
510 * provided, or compute the buffer size required.
511 * Buffer is NULL to compute the size of the buffer required.
513 * Returns a negative error number on failure, or the number of bytes
514 * used / required on success.
517 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
521 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
522 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
529 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
534 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
539 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
542 static void ext4_xattr_update_super_block(handle_t *handle,
543 struct super_block *sb)
545 if (ext4_has_feature_xattr(sb))
548 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
549 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
550 ext4_set_feature_xattr(sb);
551 ext4_handle_dirty_super(handle, sb);
556 * Release the xattr block BH: If the reference count is > 1, decrement it;
557 * otherwise free the block.
560 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
561 struct buffer_head *bh)
563 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
567 BUFFER_TRACE(bh, "get_write_access");
568 error = ext4_journal_get_write_access(handle, bh);
573 hash = le32_to_cpu(BHDR(bh)->h_hash);
574 ref = le32_to_cpu(BHDR(bh)->h_refcount);
576 ea_bdebug(bh, "refcount now=0; freeing");
578 * This must happen under buffer lock for
579 * ext4_xattr_block_set() to reliably detect freed block
581 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
584 ext4_free_blocks(handle, inode, bh, 0, 1,
585 EXT4_FREE_BLOCKS_METADATA |
586 EXT4_FREE_BLOCKS_FORGET);
589 BHDR(bh)->h_refcount = cpu_to_le32(ref);
590 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
591 struct mb_cache_entry *ce;
593 ce = mb_cache_entry_get(ext4_mb_cache, hash,
597 mb_cache_entry_put(ext4_mb_cache, ce);
601 ext4_xattr_block_csum_set(inode, bh);
603 * Beware of this ugliness: Releasing of xattr block references
604 * from different inodes can race and so we have to protect
605 * from a race where someone else frees the block (and releases
606 * its journal_head) before we are done dirtying the buffer. In
607 * nojournal mode this race is harmless and we actually cannot
608 * call ext4_handle_dirty_metadata() with locked buffer as
609 * that function can call sync_dirty_buffer() so for that case
610 * we handle the dirtying after unlocking the buffer.
612 if (ext4_handle_valid(handle))
613 error = ext4_handle_dirty_metadata(handle, inode, bh);
615 if (!ext4_handle_valid(handle))
616 error = ext4_handle_dirty_metadata(handle, inode, bh);
618 ext4_handle_sync(handle);
619 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
620 ea_bdebug(bh, "refcount now=%d; releasing",
621 le32_to_cpu(BHDR(bh)->h_refcount));
624 ext4_std_error(inode->i_sb, error);
629 * Find the available free space for EAs. This also returns the total number of
630 * bytes used by EA entries.
632 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
633 size_t *min_offs, void *base, int *total)
635 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
636 if (last->e_value_size) {
637 size_t offs = le16_to_cpu(last->e_value_offs);
638 if (offs < *min_offs)
642 *total += EXT4_XATTR_LEN(last->e_name_len);
644 return (*min_offs - ((void *)last - base) - sizeof(__u32));
648 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s,
651 struct ext4_xattr_entry *last, *next;
652 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
654 /* Compute min_offs and last. */
656 for (; !IS_LAST_ENTRY(last); last = next) {
657 next = EXT4_XATTR_NEXT(last);
658 if ((void *)next >= s->end) {
659 EXT4_ERROR_INODE(inode, "corrupted xattr entries");
660 return -EFSCORRUPTED;
662 if (last->e_value_size) {
663 size_t offs = le16_to_cpu(last->e_value_offs);
668 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
670 if (s->here->e_value_size) {
671 size_t size = le32_to_cpu(s->here->e_value_size);
672 free += EXT4_XATTR_SIZE(size);
674 free += EXT4_XATTR_LEN(name_len);
677 if (free < EXT4_XATTR_LEN(name_len) +
678 EXT4_XATTR_SIZE(i->value_len))
682 if (i->value && s->not_found) {
683 /* Insert the new name. */
684 size_t size = EXT4_XATTR_LEN(name_len);
685 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
686 memmove((void *)s->here + size, s->here, rest);
687 memset(s->here, 0, size);
688 s->here->e_name_index = i->name_index;
689 s->here->e_name_len = name_len;
690 memcpy(s->here->e_name, i->name, name_len);
692 if (s->here->e_value_size) {
693 void *first_val = s->base + min_offs;
694 size_t offs = le16_to_cpu(s->here->e_value_offs);
695 void *val = s->base + offs;
696 size_t size = EXT4_XATTR_SIZE(
697 le32_to_cpu(s->here->e_value_size));
699 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
700 /* The old and the new value have the same
701 size. Just replace. */
702 s->here->e_value_size =
703 cpu_to_le32(i->value_len);
704 if (i->value == EXT4_ZERO_XATTR_VALUE) {
705 memset(val, 0, size);
707 /* Clear pad bytes first. */
708 memset(val + size - EXT4_XATTR_PAD, 0,
710 memcpy(val, i->value, i->value_len);
715 /* Remove the old value. */
716 memmove(first_val + size, first_val, val - first_val);
717 memset(first_val, 0, size);
718 s->here->e_value_size = 0;
719 s->here->e_value_offs = 0;
722 /* Adjust all value offsets. */
724 while (!IS_LAST_ENTRY(last)) {
725 size_t o = le16_to_cpu(last->e_value_offs);
726 if (last->e_value_size && o < offs)
728 cpu_to_le16(o + size);
729 last = EXT4_XATTR_NEXT(last);
733 /* Remove the old name. */
734 size_t size = EXT4_XATTR_LEN(name_len);
735 last = ENTRY((void *)last - size);
736 memmove(s->here, (void *)s->here + size,
737 (void *)last - (void *)s->here + sizeof(__u32));
738 memset(last, 0, size);
743 /* Insert the new value. */
744 s->here->e_value_size = cpu_to_le32(i->value_len);
746 size_t size = EXT4_XATTR_SIZE(i->value_len);
747 void *val = s->base + min_offs - size;
748 s->here->e_value_offs = cpu_to_le16(min_offs - size);
749 if (i->value == EXT4_ZERO_XATTR_VALUE) {
750 memset(val, 0, size);
752 /* Clear the pad bytes first. */
753 memset(val + size - EXT4_XATTR_PAD, 0,
755 memcpy(val, i->value, i->value_len);
762 struct ext4_xattr_block_find {
763 struct ext4_xattr_search s;
764 struct buffer_head *bh;
768 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
769 struct ext4_xattr_block_find *bs)
771 struct super_block *sb = inode->i_sb;
774 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
775 i->name_index, i->name, i->value, (long)i->value_len);
777 if (EXT4_I(inode)->i_file_acl) {
778 /* The inode already has an extended attribute block. */
779 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
783 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
784 atomic_read(&(bs->bh->b_count)),
785 le32_to_cpu(BHDR(bs->bh)->h_refcount));
786 if (ext4_xattr_check_block(inode, bs->bh)) {
787 EXT4_ERROR_INODE(inode, "bad block %llu",
788 EXT4_I(inode)->i_file_acl);
789 error = -EFSCORRUPTED;
792 /* Find the named attribute. */
793 bs->s.base = BHDR(bs->bh);
794 bs->s.first = BFIRST(bs->bh);
795 bs->s.end = bs->bh->b_data + bs->bh->b_size;
796 bs->s.here = bs->s.first;
797 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
798 i->name, bs->bh->b_size, 1);
799 if (error && error != -ENODATA)
801 bs->s.not_found = error;
810 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
811 struct ext4_xattr_info *i,
812 struct ext4_xattr_block_find *bs)
814 struct super_block *sb = inode->i_sb;
815 struct buffer_head *new_bh = NULL;
816 struct ext4_xattr_search *s = &bs->s;
817 struct mb_cache_entry *ce = NULL;
819 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
821 #define header(x) ((struct ext4_xattr_header *)(x))
823 if (i->value && i->value_len > sb->s_blocksize)
826 BUFFER_TRACE(bs->bh, "get_write_access");
827 error = ext4_journal_get_write_access(handle, bs->bh);
832 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
833 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
836 * This must happen under buffer lock for
837 * ext4_xattr_block_set() to reliably detect modified
840 mb_cache_entry_delete_block(ext4_mb_cache, hash,
842 ea_bdebug(bs->bh, "modifying in-place");
843 error = ext4_xattr_set_entry(i, s, inode);
845 if (!IS_LAST_ENTRY(s->first))
846 ext4_xattr_rehash(header(s->base),
849 ext4_xattr_block_csum_set(inode, bs->bh);
850 unlock_buffer(bs->bh);
851 if (error == -EFSCORRUPTED)
854 error = ext4_handle_dirty_metadata(handle,
861 int offset = (char *)s->here - bs->bh->b_data;
863 unlock_buffer(bs->bh);
864 ea_bdebug(bs->bh, "cloning");
865 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
869 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
870 s->first = ENTRY(header(s->base)+1);
871 header(s->base)->h_refcount = cpu_to_le32(1);
872 s->here = ENTRY(s->base + offset);
873 s->end = s->base + bs->bh->b_size;
876 /* Allocate a buffer where we construct the new block. */
877 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
878 /* assert(header == s->base) */
882 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
883 header(s->base)->h_blocks = cpu_to_le32(1);
884 header(s->base)->h_refcount = cpu_to_le32(1);
885 s->first = ENTRY(header(s->base)+1);
886 s->here = ENTRY(header(s->base)+1);
887 s->end = s->base + sb->s_blocksize;
890 error = ext4_xattr_set_entry(i, s, inode);
891 if (error == -EFSCORRUPTED)
895 if (!IS_LAST_ENTRY(s->first))
896 ext4_xattr_rehash(header(s->base), s->here);
899 if (!IS_LAST_ENTRY(s->first)) {
900 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
902 /* We found an identical block in the cache. */
903 if (new_bh == bs->bh)
904 ea_bdebug(new_bh, "keeping");
908 /* The old block is released after updating
910 error = dquot_alloc_block(inode,
911 EXT4_C2B(EXT4_SB(sb), 1));
914 BUFFER_TRACE(new_bh, "get_write_access");
915 error = ext4_journal_get_write_access(handle,
921 * We have to be careful about races with
922 * freeing, rehashing or adding references to
923 * xattr block. Once we hold buffer lock xattr
924 * block's state is stable so we can check
925 * whether the block got freed / rehashed or
926 * not. Since we unhash mbcache entry under
927 * buffer lock when freeing / rehashing xattr
928 * block, checking whether entry is still
929 * hashed is reliable. Same rules hold for
930 * e_reusable handling.
932 if (hlist_bl_unhashed(&ce->e_hash_list) ||
935 * Undo everything and check mbcache
938 unlock_buffer(new_bh);
939 dquot_free_block(inode,
940 EXT4_C2B(EXT4_SB(sb),
943 mb_cache_entry_put(ext4_mb_cache, ce);
948 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
949 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
950 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
952 ea_bdebug(new_bh, "reusing; refcount now=%d",
954 ext4_xattr_block_csum_set(inode, new_bh);
955 unlock_buffer(new_bh);
956 error = ext4_handle_dirty_metadata(handle,
962 mb_cache_entry_touch(ext4_mb_cache, ce);
963 mb_cache_entry_put(ext4_mb_cache, ce);
965 } else if (bs->bh && s->base == bs->bh->b_data) {
966 /* We were modifying this block in-place. */
967 ea_bdebug(bs->bh, "keeping this block");
968 ext4_xattr_cache_insert(ext4_mb_cache, bs->bh);
972 /* We need to allocate a new block */
973 ext4_fsblk_t goal, block;
975 goal = ext4_group_first_block_no(sb,
976 EXT4_I(inode)->i_block_group);
978 /* non-extent files can't have physical blocks past 2^32 */
979 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
980 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
982 block = ext4_new_meta_blocks(handle, inode, goal, 0,
987 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
988 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
990 ea_idebug(inode, "creating block %llu",
991 (unsigned long long)block);
993 new_bh = sb_getblk(sb, block);
994 if (unlikely(!new_bh)) {
997 ext4_free_blocks(handle, inode, NULL, block, 1,
998 EXT4_FREE_BLOCKS_METADATA);
1001 lock_buffer(new_bh);
1002 error = ext4_journal_get_create_access(handle, new_bh);
1004 unlock_buffer(new_bh);
1008 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1009 ext4_xattr_block_csum_set(inode, new_bh);
1010 set_buffer_uptodate(new_bh);
1011 unlock_buffer(new_bh);
1012 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
1013 error = ext4_handle_dirty_metadata(handle, inode,
1020 /* Update the inode. */
1021 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
1023 /* Drop the previous xattr block. */
1024 if (bs->bh && bs->bh != new_bh)
1025 ext4_xattr_release_block(handle, inode, bs->bh);
1030 mb_cache_entry_put(ext4_mb_cache, ce);
1032 if (!(bs->bh && s->base == bs->bh->b_data))
1038 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
1042 EXT4_ERROR_INODE(inode, "bad block %llu",
1043 EXT4_I(inode)->i_file_acl);
1049 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1050 struct ext4_xattr_ibody_find *is)
1052 struct ext4_xattr_ibody_header *header;
1053 struct ext4_inode *raw_inode;
1056 if (EXT4_I(inode)->i_extra_isize == 0)
1058 raw_inode = ext4_raw_inode(&is->iloc);
1059 header = IHDR(inode, raw_inode);
1060 is->s.base = is->s.first = IFIRST(header);
1061 is->s.here = is->s.first;
1062 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1063 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
1064 error = xattr_check_inode(inode, header, is->s.end);
1067 /* Find the named attribute. */
1068 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
1069 i->name, is->s.end -
1070 (void *)is->s.base, 0);
1071 if (error && error != -ENODATA)
1073 is->s.not_found = error;
1078 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1079 struct ext4_xattr_info *i,
1080 struct ext4_xattr_ibody_find *is)
1082 struct ext4_xattr_ibody_header *header;
1083 struct ext4_xattr_search *s = &is->s;
1086 if (EXT4_I(inode)->i_extra_isize == 0)
1088 error = ext4_xattr_set_entry(i, s, inode);
1091 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1092 if (!IS_LAST_ENTRY(s->first)) {
1093 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1094 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1096 header->h_magic = cpu_to_le32(0);
1097 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1102 static int ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
1103 struct ext4_xattr_info *i,
1104 struct ext4_xattr_ibody_find *is)
1106 struct ext4_xattr_ibody_header *header;
1107 struct ext4_xattr_search *s = &is->s;
1110 if (EXT4_I(inode)->i_extra_isize == 0)
1112 error = ext4_xattr_set_entry(i, s, inode);
1115 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1116 if (!IS_LAST_ENTRY(s->first)) {
1117 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1118 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1120 header->h_magic = cpu_to_le32(0);
1121 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1126 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1127 struct ext4_xattr_info *i)
1131 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1133 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1134 return !memcmp(value, i->value, i->value_len);
1138 * ext4_xattr_set_handle()
1140 * Create, replace or remove an extended attribute for this inode. Value
1141 * is NULL to remove an existing extended attribute, and non-NULL to
1142 * either replace an existing extended attribute, or create a new extended
1143 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1144 * specify that an extended attribute must exist and must not exist
1145 * previous to the call, respectively.
1147 * Returns 0, or a negative error number on failure.
1150 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1151 const char *name, const void *value, size_t value_len,
1154 struct ext4_xattr_info i = {
1155 .name_index = name_index,
1158 .value_len = value_len,
1161 struct ext4_xattr_ibody_find is = {
1162 .s = { .not_found = -ENODATA, },
1164 struct ext4_xattr_block_find bs = {
1165 .s = { .not_found = -ENODATA, },
1172 if (strlen(name) > 255)
1174 ext4_write_lock_xattr(inode, &no_expand);
1176 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1180 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1181 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1182 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1183 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1186 error = ext4_xattr_ibody_find(inode, &i, &is);
1190 error = ext4_xattr_block_find(inode, &i, &bs);
1193 if (is.s.not_found && bs.s.not_found) {
1195 if (flags & XATTR_REPLACE)
1202 if (flags & XATTR_CREATE)
1206 if (!is.s.not_found)
1207 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1208 else if (!bs.s.not_found)
1209 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1212 /* Xattr value did not change? Save us some work and bail out */
1213 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1215 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1218 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1219 if (!error && !bs.s.not_found) {
1221 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1222 } else if (error == -ENOSPC) {
1223 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1226 error = ext4_xattr_block_find(inode, &i, &bs);
1230 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1233 if (!is.s.not_found) {
1235 error = ext4_xattr_ibody_set(handle, inode, &i,
1241 ext4_xattr_update_super_block(handle, inode->i_sb);
1242 inode->i_ctime = ext4_current_time(inode);
1245 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1247 * The bh is consumed by ext4_mark_iloc_dirty, even with
1252 ext4_handle_sync(handle);
1258 ext4_write_unlock_xattr(inode, &no_expand);
1265 * Like ext4_xattr_set_handle, but start from an inode. This extended
1266 * attribute modification is a filesystem transaction by itself.
1268 * Returns 0, or a negative error number on failure.
1271 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1272 const void *value, size_t value_len, int flags)
1275 int error, retries = 0;
1276 int credits = ext4_jbd2_credits_xattr(inode);
1279 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1280 if (IS_ERR(handle)) {
1281 error = PTR_ERR(handle);
1285 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1286 value, value_len, flags);
1287 error2 = ext4_journal_stop(handle);
1288 if (error == -ENOSPC &&
1289 ext4_should_retry_alloc(inode->i_sb, &retries))
1299 * Shift the EA entries in the inode to create space for the increased
1302 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1303 int value_offs_shift, void *to,
1304 void *from, size_t n)
1306 struct ext4_xattr_entry *last = entry;
1309 /* We always shift xattr headers further thus offsets get lower */
1310 BUG_ON(value_offs_shift > 0);
1312 /* Adjust the value offsets of the entries */
1313 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1314 if (last->e_value_size) {
1315 new_offs = le16_to_cpu(last->e_value_offs) +
1317 last->e_value_offs = cpu_to_le16(new_offs);
1320 /* Shift the entries by n bytes */
1321 memmove(to, from, n);
1325 * Move xattr pointed to by 'entry' from inode into external xattr block
1327 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1328 struct ext4_inode *raw_inode,
1329 struct ext4_xattr_entry *entry)
1331 struct ext4_xattr_ibody_find *is = NULL;
1332 struct ext4_xattr_block_find *bs = NULL;
1333 char *buffer = NULL, *b_entry_name = NULL;
1334 size_t value_offs, value_size;
1335 struct ext4_xattr_info i = {
1338 .name_index = entry->e_name_index,
1340 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1343 value_offs = le16_to_cpu(entry->e_value_offs);
1344 value_size = le32_to_cpu(entry->e_value_size);
1346 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1347 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1348 buffer = kmalloc(value_size, GFP_NOFS);
1349 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1350 if (!is || !bs || !buffer || !b_entry_name) {
1355 is->s.not_found = -ENODATA;
1356 bs->s.not_found = -ENODATA;
1360 /* Save the entry name and the entry value */
1361 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1362 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1363 b_entry_name[entry->e_name_len] = '\0';
1364 i.name = b_entry_name;
1366 error = ext4_get_inode_loc(inode, &is->iloc);
1370 error = ext4_xattr_ibody_find(inode, &i, is);
1374 /* Remove the chosen entry from the inode */
1375 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1379 i.name = b_entry_name;
1381 i.value_len = value_size;
1382 error = ext4_xattr_block_find(inode, &i, bs);
1386 /* Add entry which was removed from the inode into the block */
1387 error = ext4_xattr_block_set(handle, inode, &i, bs);
1392 kfree(b_entry_name);
1395 brelse(is->iloc.bh);
1404 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1405 struct ext4_inode *raw_inode,
1406 int isize_diff, size_t ifree,
1407 size_t bfree, int *total_ino)
1409 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1410 struct ext4_xattr_entry *small_entry;
1411 struct ext4_xattr_entry *entry;
1412 struct ext4_xattr_entry *last;
1413 unsigned int entry_size; /* EA entry size */
1414 unsigned int total_size; /* EA entry size + value size */
1415 unsigned int min_total_size;
1418 while (isize_diff > ifree) {
1421 min_total_size = ~0U;
1422 last = IFIRST(header);
1423 /* Find the entry best suited to be pushed into EA block */
1424 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1425 /* never move system.data out of the inode */
1426 if ((last->e_name_len == 4) &&
1427 (last->e_name_index == EXT4_XATTR_INDEX_SYSTEM) &&
1428 !memcmp(last->e_name, "data", 4))
1431 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1432 EXT4_XATTR_LEN(last->e_name_len);
1433 if (total_size <= bfree &&
1434 total_size < min_total_size) {
1435 if (total_size + ifree < isize_diff) {
1439 min_total_size = total_size;
1444 if (entry == NULL) {
1445 if (small_entry == NULL)
1447 entry = small_entry;
1450 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1451 total_size = entry_size +
1452 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1453 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1458 *total_ino -= entry_size;
1459 ifree += total_size;
1460 bfree -= total_size;
1467 * Expand an inode by new_extra_isize bytes when EAs are present.
1468 * Returns 0 on success or negative error number on failure.
1470 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1471 struct ext4_inode *raw_inode, handle_t *handle)
1473 struct ext4_xattr_ibody_header *header;
1474 struct buffer_head *bh = NULL;
1476 size_t ifree, bfree;
1479 int error = 0, tried_min_extra_isize = 0;
1480 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1481 int isize_diff; /* How much do we need to grow i_extra_isize */
1484 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1488 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
1489 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1492 header = IHDR(inode, raw_inode);
1495 * Check if enough free space is available in the inode to shift the
1496 * entries ahead by new_extra_isize.
1499 base = IFIRST(header);
1500 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1501 min_offs = end - base;
1502 total_ino = sizeof(struct ext4_xattr_ibody_header) + sizeof(u32);
1504 error = xattr_check_inode(inode, header, end);
1508 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
1509 if (ifree >= isize_diff)
1513 * Enough free space isn't available in the inode, check if
1514 * EA block can hold new_extra_isize bytes.
1516 if (EXT4_I(inode)->i_file_acl) {
1517 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1521 if (ext4_xattr_check_block(inode, bh)) {
1522 EXT4_ERROR_INODE(inode, "bad block %llu",
1523 EXT4_I(inode)->i_file_acl);
1524 error = -EFSCORRUPTED;
1528 end = bh->b_data + bh->b_size;
1529 min_offs = end - base;
1530 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1532 if (bfree + ifree < isize_diff) {
1533 if (!tried_min_extra_isize && s_min_extra_isize) {
1534 tried_min_extra_isize++;
1535 new_extra_isize = s_min_extra_isize;
1543 bfree = inode->i_sb->s_blocksize;
1546 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1547 isize_diff, ifree, bfree,
1550 if (error == -ENOSPC && !tried_min_extra_isize &&
1551 s_min_extra_isize) {
1552 tried_min_extra_isize++;
1553 new_extra_isize = s_min_extra_isize;
1560 /* Adjust the offsets and shift the remaining entries ahead */
1561 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
1562 - new_extra_isize, (void *)raw_inode +
1563 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1564 (void *)header, total_ino);
1565 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1568 ext4_write_unlock_xattr(inode, &no_expand);
1574 * Inode size expansion failed; don't try again
1577 ext4_write_unlock_xattr(inode, &no_expand);
1584 * ext4_xattr_delete_inode()
1586 * Free extended attribute resources associated with this inode. This
1587 * is called immediately before an inode is freed. We have exclusive
1588 * access to the inode.
1591 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1593 struct buffer_head *bh = NULL;
1595 if (!EXT4_I(inode)->i_file_acl)
1597 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1599 EXT4_ERROR_INODE(inode, "block %llu read error",
1600 EXT4_I(inode)->i_file_acl);
1603 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1604 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1605 EXT4_ERROR_INODE(inode, "bad block %llu",
1606 EXT4_I(inode)->i_file_acl);
1609 ext4_xattr_release_block(handle, inode, bh);
1610 EXT4_I(inode)->i_file_acl = 0;
1617 * ext4_xattr_cache_insert()
1619 * Create a new entry in the extended attribute cache, and insert
1620 * it unless such an entry is already in the cache.
1622 * Returns 0, or a negative error number on failure.
1625 ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
1627 struct ext4_xattr_header *header = BHDR(bh);
1628 __u32 hash = le32_to_cpu(header->h_hash);
1629 int reusable = le32_to_cpu(header->h_refcount) <
1630 EXT4_XATTR_REFCOUNT_MAX;
1633 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
1634 bh->b_blocknr, reusable);
1636 if (error == -EBUSY)
1637 ea_bdebug(bh, "already in cache");
1639 ea_bdebug(bh, "inserting [%x]", (int)hash);
1645 * Compare two extended attribute blocks for equality.
1647 * Returns 0 if the blocks are equal, 1 if they differ, and
1648 * a negative error number on errors.
1651 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1652 struct ext4_xattr_header *header2)
1654 struct ext4_xattr_entry *entry1, *entry2;
1656 entry1 = ENTRY(header1+1);
1657 entry2 = ENTRY(header2+1);
1658 while (!IS_LAST_ENTRY(entry1)) {
1659 if (IS_LAST_ENTRY(entry2))
1661 if (entry1->e_hash != entry2->e_hash ||
1662 entry1->e_name_index != entry2->e_name_index ||
1663 entry1->e_name_len != entry2->e_name_len ||
1664 entry1->e_value_size != entry2->e_value_size ||
1665 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1667 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1668 return -EFSCORRUPTED;
1669 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1670 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1671 le32_to_cpu(entry1->e_value_size)))
1674 entry1 = EXT4_XATTR_NEXT(entry1);
1675 entry2 = EXT4_XATTR_NEXT(entry2);
1677 if (!IS_LAST_ENTRY(entry2))
1683 * ext4_xattr_cache_find()
1685 * Find an identical extended attribute block.
1687 * Returns a pointer to the block found, or NULL if such a block was
1688 * not found or an error occurred.
1690 static struct buffer_head *
1691 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1692 struct mb_cache_entry **pce)
1694 __u32 hash = le32_to_cpu(header->h_hash);
1695 struct mb_cache_entry *ce;
1696 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1698 if (!header->h_hash)
1699 return NULL; /* never share */
1700 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1701 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
1703 struct buffer_head *bh;
1705 bh = sb_bread(inode->i_sb, ce->e_block);
1707 EXT4_ERROR_INODE(inode, "block %lu read error",
1708 (unsigned long) ce->e_block);
1709 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1714 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
1719 #define NAME_HASH_SHIFT 5
1720 #define VALUE_HASH_SHIFT 16
1723 * ext4_xattr_hash_entry()
1725 * Compute the hash of an extended attribute.
1727 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1728 struct ext4_xattr_entry *entry)
1731 char *name = entry->e_name;
1734 for (n = 0; n < entry->e_name_len; n++) {
1735 hash = (hash << NAME_HASH_SHIFT) ^
1736 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1740 if (entry->e_value_size != 0) {
1741 __le32 *value = (__le32 *)((char *)header +
1742 le16_to_cpu(entry->e_value_offs));
1743 for (n = (le32_to_cpu(entry->e_value_size) +
1744 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1745 hash = (hash << VALUE_HASH_SHIFT) ^
1746 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1747 le32_to_cpu(*value++);
1750 entry->e_hash = cpu_to_le32(hash);
1753 #undef NAME_HASH_SHIFT
1754 #undef VALUE_HASH_SHIFT
1756 #define BLOCK_HASH_SHIFT 16
1759 * ext4_xattr_rehash()
1761 * Re-compute the extended attribute hash value after an entry has changed.
1763 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1764 struct ext4_xattr_entry *entry)
1766 struct ext4_xattr_entry *here;
1769 ext4_xattr_hash_entry(header, entry);
1770 here = ENTRY(header+1);
1771 while (!IS_LAST_ENTRY(here)) {
1772 if (!here->e_hash) {
1773 /* Block is not shared if an entry's hash value == 0 */
1777 hash = (hash << BLOCK_HASH_SHIFT) ^
1778 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1779 le32_to_cpu(here->e_hash);
1780 here = EXT4_XATTR_NEXT(here);
1782 header->h_hash = cpu_to_le32(hash);
1785 #undef BLOCK_HASH_SHIFT
1787 #define HASH_BUCKET_BITS 10
1790 ext4_xattr_create_cache(void)
1792 return mb_cache_create(HASH_BUCKET_BITS);
1795 void ext4_xattr_destroy_cache(struct mb_cache *cache)
1798 mb_cache_destroy(cache);