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 /* Find the end of the names list */
184 while (!IS_LAST_ENTRY(e)) {
185 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
186 if ((void *)next >= end)
187 return -EFSCORRUPTED;
191 /* Check the values */
192 while (!IS_LAST_ENTRY(entry)) {
193 if (entry->e_value_block != 0)
194 return -EFSCORRUPTED;
195 if (entry->e_value_size != 0) {
196 u16 offs = le16_to_cpu(entry->e_value_offs);
197 u32 size = le32_to_cpu(entry->e_value_size);
201 * The value cannot overlap the names, and the value
202 * with padding cannot extend beyond 'end'. Check both
203 * the padded and unpadded sizes, since the size may
204 * overflow to 0 when adding padding.
206 if (offs > end - value_start)
207 return -EFSCORRUPTED;
208 value = value_start + offs;
209 if (value < (void *)e + sizeof(u32) ||
210 size > end - value ||
211 EXT4_XATTR_SIZE(size) > end - value)
212 return -EFSCORRUPTED;
214 entry = EXT4_XATTR_NEXT(entry);
221 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
225 if (buffer_verified(bh))
228 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
229 BHDR(bh)->h_blocks != cpu_to_le32(1))
230 return -EFSCORRUPTED;
231 if (!ext4_xattr_block_csum_verify(inode, bh))
233 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
236 set_buffer_verified(bh);
241 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
242 void *end, const char *function, unsigned int line)
244 int error = -EFSCORRUPTED;
246 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
247 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
249 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
252 __ext4_error_inode(inode, function, line, 0,
253 "corrupted in-inode xattr");
257 #define xattr_check_inode(inode, header, end) \
258 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
261 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
263 size_t value_size = le32_to_cpu(entry->e_value_size);
265 if (entry->e_value_block != 0 || value_size > size ||
266 le16_to_cpu(entry->e_value_offs) + value_size > size)
267 return -EFSCORRUPTED;
272 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
273 const char *name, size_t size, int sorted)
275 struct ext4_xattr_entry *entry;
281 name_len = strlen(name);
283 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
284 cmp = name_index - entry->e_name_index;
286 cmp = name_len - entry->e_name_len;
288 cmp = memcmp(name, entry->e_name, name_len);
289 if (cmp <= 0 && (sorted || cmp == 0))
293 if (!cmp && ext4_xattr_check_entry(entry, size))
294 return -EFSCORRUPTED;
295 return cmp ? -ENODATA : 0;
299 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
300 void *buffer, size_t buffer_size)
302 struct buffer_head *bh = NULL;
303 struct ext4_xattr_entry *entry;
306 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
308 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
309 name_index, name, buffer, (long)buffer_size);
312 if (!EXT4_I(inode)->i_file_acl)
314 ea_idebug(inode, "reading block %llu",
315 (unsigned long long)EXT4_I(inode)->i_file_acl);
316 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
319 ea_bdebug(bh, "b_count=%d, refcount=%d",
320 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
321 if (ext4_xattr_check_block(inode, bh)) {
323 EXT4_ERROR_INODE(inode, "bad block %llu",
324 EXT4_I(inode)->i_file_acl);
325 error = -EFSCORRUPTED;
328 ext4_xattr_cache_insert(ext4_mb_cache, bh);
330 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
331 if (error == -EFSCORRUPTED)
335 size = le32_to_cpu(entry->e_value_size);
338 if (size > buffer_size)
340 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
351 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
352 void *buffer, size_t buffer_size)
354 struct ext4_xattr_ibody_header *header;
355 struct ext4_xattr_entry *entry;
356 struct ext4_inode *raw_inode;
357 struct ext4_iloc iloc;
362 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
364 error = ext4_get_inode_loc(inode, &iloc);
367 raw_inode = ext4_raw_inode(&iloc);
368 header = IHDR(inode, raw_inode);
369 entry = IFIRST(header);
370 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
371 error = xattr_check_inode(inode, header, end);
374 error = ext4_xattr_find_entry(&entry, name_index, name,
375 end - (void *)entry, 0);
378 size = le32_to_cpu(entry->e_value_size);
381 if (size > buffer_size)
383 memcpy(buffer, (void *)IFIRST(header) +
384 le16_to_cpu(entry->e_value_offs), size);
396 * Copy an extended attribute into the buffer
397 * provided, or compute the buffer size required.
398 * Buffer is NULL to compute the size of the buffer required.
400 * Returns a negative error number on failure, or the number of bytes
401 * used / required on success.
404 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
405 void *buffer, size_t buffer_size)
409 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
412 if (strlen(name) > 255)
415 down_read(&EXT4_I(inode)->xattr_sem);
416 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
418 if (error == -ENODATA)
419 error = ext4_xattr_block_get(inode, name_index, name, buffer,
421 up_read(&EXT4_I(inode)->xattr_sem);
426 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
427 char *buffer, size_t buffer_size)
429 size_t rest = buffer_size;
431 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
432 const struct xattr_handler *handler =
433 ext4_xattr_handler(entry->e_name_index);
435 if (handler && (!handler->list || handler->list(dentry))) {
436 const char *prefix = handler->prefix ?: handler->name;
437 size_t prefix_len = strlen(prefix);
438 size_t size = prefix_len + entry->e_name_len + 1;
443 memcpy(buffer, prefix, prefix_len);
444 buffer += prefix_len;
445 memcpy(buffer, entry->e_name, entry->e_name_len);
446 buffer += entry->e_name_len;
452 return buffer_size - rest; /* total size */
456 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
458 struct inode *inode = d_inode(dentry);
459 struct buffer_head *bh = NULL;
461 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
463 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
464 buffer, (long)buffer_size);
467 if (!EXT4_I(inode)->i_file_acl)
469 ea_idebug(inode, "reading block %llu",
470 (unsigned long long)EXT4_I(inode)->i_file_acl);
471 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
475 ea_bdebug(bh, "b_count=%d, refcount=%d",
476 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
477 if (ext4_xattr_check_block(inode, bh)) {
478 EXT4_ERROR_INODE(inode, "bad block %llu",
479 EXT4_I(inode)->i_file_acl);
480 error = -EFSCORRUPTED;
483 ext4_xattr_cache_insert(ext4_mb_cache, bh);
484 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
493 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
495 struct inode *inode = d_inode(dentry);
496 struct ext4_xattr_ibody_header *header;
497 struct ext4_inode *raw_inode;
498 struct ext4_iloc iloc;
502 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
504 error = ext4_get_inode_loc(inode, &iloc);
507 raw_inode = ext4_raw_inode(&iloc);
508 header = IHDR(inode, raw_inode);
509 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
510 error = xattr_check_inode(inode, header, end);
513 error = ext4_xattr_list_entries(dentry, IFIRST(header),
514 buffer, buffer_size);
524 * Copy a list of attribute names into the buffer
525 * provided, or compute the buffer size required.
526 * Buffer is NULL to compute the size of the buffer required.
528 * Returns a negative error number on failure, or the number of bytes
529 * used / required on success.
532 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
536 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
537 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
544 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
549 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
554 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
557 static void ext4_xattr_update_super_block(handle_t *handle,
558 struct super_block *sb)
560 if (ext4_has_feature_xattr(sb))
563 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
564 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
565 ext4_set_feature_xattr(sb);
566 ext4_handle_dirty_super(handle, sb);
571 * Release the xattr block BH: If the reference count is > 1, decrement it;
572 * otherwise free the block.
575 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
576 struct buffer_head *bh)
578 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
582 BUFFER_TRACE(bh, "get_write_access");
583 error = ext4_journal_get_write_access(handle, bh);
588 hash = le32_to_cpu(BHDR(bh)->h_hash);
589 ref = le32_to_cpu(BHDR(bh)->h_refcount);
591 ea_bdebug(bh, "refcount now=0; freeing");
593 * This must happen under buffer lock for
594 * ext4_xattr_block_set() to reliably detect freed block
596 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
599 ext4_free_blocks(handle, inode, bh, 0, 1,
600 EXT4_FREE_BLOCKS_METADATA |
601 EXT4_FREE_BLOCKS_FORGET);
604 BHDR(bh)->h_refcount = cpu_to_le32(ref);
605 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
606 struct mb_cache_entry *ce;
608 ce = mb_cache_entry_get(ext4_mb_cache, hash,
612 mb_cache_entry_put(ext4_mb_cache, ce);
616 ext4_xattr_block_csum_set(inode, bh);
618 * Beware of this ugliness: Releasing of xattr block references
619 * from different inodes can race and so we have to protect
620 * from a race where someone else frees the block (and releases
621 * its journal_head) before we are done dirtying the buffer. In
622 * nojournal mode this race is harmless and we actually cannot
623 * call ext4_handle_dirty_metadata() with locked buffer as
624 * that function can call sync_dirty_buffer() so for that case
625 * we handle the dirtying after unlocking the buffer.
627 if (ext4_handle_valid(handle))
628 error = ext4_handle_dirty_metadata(handle, inode, bh);
630 if (!ext4_handle_valid(handle))
631 error = ext4_handle_dirty_metadata(handle, inode, bh);
633 ext4_handle_sync(handle);
634 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
635 ea_bdebug(bh, "refcount now=%d; releasing",
636 le32_to_cpu(BHDR(bh)->h_refcount));
639 ext4_std_error(inode->i_sb, error);
644 * Find the available free space for EAs. This also returns the total number of
645 * bytes used by EA entries.
647 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
648 size_t *min_offs, void *base, int *total)
650 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
651 if (last->e_value_size) {
652 size_t offs = le16_to_cpu(last->e_value_offs);
653 if (offs < *min_offs)
657 *total += EXT4_XATTR_LEN(last->e_name_len);
659 return (*min_offs - ((void *)last - base) - sizeof(__u32));
663 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
665 struct ext4_xattr_entry *last;
666 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
668 /* Compute min_offs and last. */
670 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
671 if (last->e_value_size) {
672 size_t offs = le16_to_cpu(last->e_value_offs);
677 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
679 if (s->here->e_value_size) {
680 size_t size = le32_to_cpu(s->here->e_value_size);
681 free += EXT4_XATTR_SIZE(size);
683 free += EXT4_XATTR_LEN(name_len);
686 if (free < EXT4_XATTR_LEN(name_len) +
687 EXT4_XATTR_SIZE(i->value_len))
691 if (i->value && s->not_found) {
692 /* Insert the new name. */
693 size_t size = EXT4_XATTR_LEN(name_len);
694 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
695 memmove((void *)s->here + size, s->here, rest);
696 memset(s->here, 0, size);
697 s->here->e_name_index = i->name_index;
698 s->here->e_name_len = name_len;
699 memcpy(s->here->e_name, i->name, name_len);
701 if (s->here->e_value_size) {
702 void *first_val = s->base + min_offs;
703 size_t offs = le16_to_cpu(s->here->e_value_offs);
704 void *val = s->base + offs;
705 size_t size = EXT4_XATTR_SIZE(
706 le32_to_cpu(s->here->e_value_size));
708 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
709 /* The old and the new value have the same
710 size. Just replace. */
711 s->here->e_value_size =
712 cpu_to_le32(i->value_len);
713 if (i->value == EXT4_ZERO_XATTR_VALUE) {
714 memset(val, 0, size);
716 /* Clear pad bytes first. */
717 memset(val + size - EXT4_XATTR_PAD, 0,
719 memcpy(val, i->value, i->value_len);
724 /* Remove the old value. */
725 memmove(first_val + size, first_val, val - first_val);
726 memset(first_val, 0, size);
727 s->here->e_value_size = 0;
728 s->here->e_value_offs = 0;
731 /* Adjust all value offsets. */
733 while (!IS_LAST_ENTRY(last)) {
734 size_t o = le16_to_cpu(last->e_value_offs);
735 if (last->e_value_size && o < offs)
737 cpu_to_le16(o + size);
738 last = EXT4_XATTR_NEXT(last);
742 /* Remove the old name. */
743 size_t size = EXT4_XATTR_LEN(name_len);
744 last = ENTRY((void *)last - size);
745 memmove(s->here, (void *)s->here + size,
746 (void *)last - (void *)s->here + sizeof(__u32));
747 memset(last, 0, size);
752 /* Insert the new value. */
753 s->here->e_value_size = cpu_to_le32(i->value_len);
755 size_t size = EXT4_XATTR_SIZE(i->value_len);
756 void *val = s->base + min_offs - size;
757 s->here->e_value_offs = cpu_to_le16(min_offs - size);
758 if (i->value == EXT4_ZERO_XATTR_VALUE) {
759 memset(val, 0, size);
761 /* Clear the pad bytes first. */
762 memset(val + size - EXT4_XATTR_PAD, 0,
764 memcpy(val, i->value, i->value_len);
771 struct ext4_xattr_block_find {
772 struct ext4_xattr_search s;
773 struct buffer_head *bh;
777 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
778 struct ext4_xattr_block_find *bs)
780 struct super_block *sb = inode->i_sb;
783 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
784 i->name_index, i->name, i->value, (long)i->value_len);
786 if (EXT4_I(inode)->i_file_acl) {
787 /* The inode already has an extended attribute block. */
788 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
792 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
793 atomic_read(&(bs->bh->b_count)),
794 le32_to_cpu(BHDR(bs->bh)->h_refcount));
795 if (ext4_xattr_check_block(inode, bs->bh)) {
796 EXT4_ERROR_INODE(inode, "bad block %llu",
797 EXT4_I(inode)->i_file_acl);
798 error = -EFSCORRUPTED;
801 /* Find the named attribute. */
802 bs->s.base = BHDR(bs->bh);
803 bs->s.first = BFIRST(bs->bh);
804 bs->s.end = bs->bh->b_data + bs->bh->b_size;
805 bs->s.here = bs->s.first;
806 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
807 i->name, bs->bh->b_size, 1);
808 if (error && error != -ENODATA)
810 bs->s.not_found = error;
819 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
820 struct ext4_xattr_info *i,
821 struct ext4_xattr_block_find *bs)
823 struct super_block *sb = inode->i_sb;
824 struct buffer_head *new_bh = NULL;
825 struct ext4_xattr_search *s = &bs->s;
826 struct mb_cache_entry *ce = NULL;
828 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
830 #define header(x) ((struct ext4_xattr_header *)(x))
832 if (i->value && i->value_len > sb->s_blocksize)
835 BUFFER_TRACE(bs->bh, "get_write_access");
836 error = ext4_journal_get_write_access(handle, bs->bh);
841 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
842 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
845 * This must happen under buffer lock for
846 * ext4_xattr_block_set() to reliably detect modified
849 mb_cache_entry_delete_block(ext4_mb_cache, hash,
851 ea_bdebug(bs->bh, "modifying in-place");
852 error = ext4_xattr_set_entry(i, s);
854 if (!IS_LAST_ENTRY(s->first))
855 ext4_xattr_rehash(header(s->base),
857 ext4_xattr_cache_insert(ext4_mb_cache,
860 ext4_xattr_block_csum_set(inode, bs->bh);
861 unlock_buffer(bs->bh);
862 if (error == -EFSCORRUPTED)
865 error = ext4_handle_dirty_metadata(handle,
872 int offset = (char *)s->here - bs->bh->b_data;
874 unlock_buffer(bs->bh);
875 ea_bdebug(bs->bh, "cloning");
876 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
880 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
881 s->first = ENTRY(header(s->base)+1);
882 header(s->base)->h_refcount = cpu_to_le32(1);
883 s->here = ENTRY(s->base + offset);
884 s->end = s->base + bs->bh->b_size;
887 /* Allocate a buffer where we construct the new block. */
888 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
889 /* assert(header == s->base) */
893 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
894 header(s->base)->h_blocks = cpu_to_le32(1);
895 header(s->base)->h_refcount = cpu_to_le32(1);
896 s->first = ENTRY(header(s->base)+1);
897 s->here = ENTRY(header(s->base)+1);
898 s->end = s->base + sb->s_blocksize;
901 error = ext4_xattr_set_entry(i, s);
902 if (error == -EFSCORRUPTED)
906 if (!IS_LAST_ENTRY(s->first))
907 ext4_xattr_rehash(header(s->base), s->here);
910 if (!IS_LAST_ENTRY(s->first)) {
911 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
913 /* We found an identical block in the cache. */
914 if (new_bh == bs->bh)
915 ea_bdebug(new_bh, "keeping");
919 /* The old block is released after updating
921 error = dquot_alloc_block(inode,
922 EXT4_C2B(EXT4_SB(sb), 1));
925 BUFFER_TRACE(new_bh, "get_write_access");
926 error = ext4_journal_get_write_access(handle,
932 * We have to be careful about races with
933 * freeing, rehashing or adding references to
934 * xattr block. Once we hold buffer lock xattr
935 * block's state is stable so we can check
936 * whether the block got freed / rehashed or
937 * not. Since we unhash mbcache entry under
938 * buffer lock when freeing / rehashing xattr
939 * block, checking whether entry is still
940 * hashed is reliable. Same rules hold for
941 * e_reusable handling.
943 if (hlist_bl_unhashed(&ce->e_hash_list) ||
946 * Undo everything and check mbcache
949 unlock_buffer(new_bh);
950 dquot_free_block(inode,
951 EXT4_C2B(EXT4_SB(sb),
954 mb_cache_entry_put(ext4_mb_cache, ce);
959 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
960 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
961 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
963 ea_bdebug(new_bh, "reusing; refcount now=%d",
965 ext4_xattr_block_csum_set(inode, new_bh);
966 unlock_buffer(new_bh);
967 error = ext4_handle_dirty_metadata(handle,
973 mb_cache_entry_touch(ext4_mb_cache, ce);
974 mb_cache_entry_put(ext4_mb_cache, ce);
976 } else if (bs->bh && s->base == bs->bh->b_data) {
977 /* We were modifying this block in-place. */
978 ea_bdebug(bs->bh, "keeping this block");
982 /* We need to allocate a new block */
983 ext4_fsblk_t goal, block;
985 goal = ext4_group_first_block_no(sb,
986 EXT4_I(inode)->i_block_group);
988 /* non-extent files can't have physical blocks past 2^32 */
989 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
990 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
992 block = ext4_new_meta_blocks(handle, inode, goal, 0,
997 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
998 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1000 ea_idebug(inode, "creating block %llu",
1001 (unsigned long long)block);
1003 new_bh = sb_getblk(sb, block);
1004 if (unlikely(!new_bh)) {
1007 ext4_free_blocks(handle, inode, NULL, block, 1,
1008 EXT4_FREE_BLOCKS_METADATA);
1011 lock_buffer(new_bh);
1012 error = ext4_journal_get_create_access(handle, new_bh);
1014 unlock_buffer(new_bh);
1018 memcpy(new_bh->b_data, s->base, new_bh->b_size);
1019 ext4_xattr_block_csum_set(inode, new_bh);
1020 set_buffer_uptodate(new_bh);
1021 unlock_buffer(new_bh);
1022 ext4_xattr_cache_insert(ext4_mb_cache, new_bh);
1023 error = ext4_handle_dirty_metadata(handle, inode,
1030 /* Update the inode. */
1031 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
1033 /* Drop the previous xattr block. */
1034 if (bs->bh && bs->bh != new_bh)
1035 ext4_xattr_release_block(handle, inode, bs->bh);
1040 mb_cache_entry_put(ext4_mb_cache, ce);
1042 if (!(bs->bh && s->base == bs->bh->b_data))
1048 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
1052 EXT4_ERROR_INODE(inode, "bad block %llu",
1053 EXT4_I(inode)->i_file_acl);
1059 int ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
1060 struct ext4_xattr_ibody_find *is)
1062 struct ext4_xattr_ibody_header *header;
1063 struct ext4_inode *raw_inode;
1066 if (EXT4_I(inode)->i_extra_isize == 0)
1068 raw_inode = ext4_raw_inode(&is->iloc);
1069 header = IHDR(inode, raw_inode);
1070 is->s.base = is->s.first = IFIRST(header);
1071 is->s.here = is->s.first;
1072 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1073 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
1074 error = xattr_check_inode(inode, header, is->s.end);
1077 /* Find the named attribute. */
1078 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
1079 i->name, is->s.end -
1080 (void *)is->s.base, 0);
1081 if (error && error != -ENODATA)
1083 is->s.not_found = error;
1088 int ext4_xattr_ibody_inline_set(handle_t *handle, struct inode *inode,
1089 struct ext4_xattr_info *i,
1090 struct ext4_xattr_ibody_find *is)
1092 struct ext4_xattr_ibody_header *header;
1093 struct ext4_xattr_search *s = &is->s;
1096 if (EXT4_I(inode)->i_extra_isize == 0)
1098 error = ext4_xattr_set_entry(i, s);
1100 if (error == -ENOSPC &&
1101 ext4_has_inline_data(inode)) {
1102 error = ext4_try_to_evict_inline_data(handle, inode,
1103 EXT4_XATTR_LEN(strlen(i->name) +
1104 EXT4_XATTR_SIZE(i->value_len)));
1107 error = ext4_xattr_ibody_find(inode, i, is);
1110 error = ext4_xattr_set_entry(i, s);
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_ibody_set(struct inode *inode,
1127 struct ext4_xattr_info *i,
1128 struct ext4_xattr_ibody_find *is)
1130 struct ext4_xattr_ibody_header *header;
1131 struct ext4_xattr_search *s = &is->s;
1134 if (EXT4_I(inode)->i_extra_isize == 0)
1136 error = ext4_xattr_set_entry(i, s);
1139 header = IHDR(inode, ext4_raw_inode(&is->iloc));
1140 if (!IS_LAST_ENTRY(s->first)) {
1141 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
1142 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1144 header->h_magic = cpu_to_le32(0);
1145 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1150 static int ext4_xattr_value_same(struct ext4_xattr_search *s,
1151 struct ext4_xattr_info *i)
1155 if (le32_to_cpu(s->here->e_value_size) != i->value_len)
1157 value = ((void *)s->base) + le16_to_cpu(s->here->e_value_offs);
1158 return !memcmp(value, i->value, i->value_len);
1162 * ext4_xattr_set_handle()
1164 * Create, replace or remove an extended attribute for this inode. Value
1165 * is NULL to remove an existing extended attribute, and non-NULL to
1166 * either replace an existing extended attribute, or create a new extended
1167 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1168 * specify that an extended attribute must exist and must not exist
1169 * previous to the call, respectively.
1171 * Returns 0, or a negative error number on failure.
1174 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
1175 const char *name, const void *value, size_t value_len,
1178 struct ext4_xattr_info i = {
1179 .name_index = name_index,
1182 .value_len = value_len,
1185 struct ext4_xattr_ibody_find is = {
1186 .s = { .not_found = -ENODATA, },
1188 struct ext4_xattr_block_find bs = {
1189 .s = { .not_found = -ENODATA, },
1196 if (strlen(name) > 255)
1198 ext4_write_lock_xattr(inode, &no_expand);
1200 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1204 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1205 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1206 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1207 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1210 error = ext4_xattr_ibody_find(inode, &i, &is);
1214 error = ext4_xattr_block_find(inode, &i, &bs);
1217 if (is.s.not_found && bs.s.not_found) {
1219 if (flags & XATTR_REPLACE)
1226 if (flags & XATTR_CREATE)
1230 if (!is.s.not_found)
1231 error = ext4_xattr_ibody_set(inode, &i, &is);
1232 else if (!bs.s.not_found)
1233 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1236 /* Xattr value did not change? Save us some work and bail out */
1237 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1239 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1242 error = ext4_xattr_ibody_set(inode, &i, &is);
1243 if (!error && !bs.s.not_found) {
1245 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1246 } else if (error == -ENOSPC) {
1247 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1248 error = ext4_xattr_block_find(inode, &i, &bs);
1252 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1255 if (!is.s.not_found) {
1257 error = ext4_xattr_ibody_set(inode, &i, &is);
1262 ext4_xattr_update_super_block(handle, inode->i_sb);
1263 inode->i_ctime = current_time(inode);
1266 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1268 * The bh is consumed by ext4_mark_iloc_dirty, even with
1273 ext4_handle_sync(handle);
1279 ext4_write_unlock_xattr(inode, &no_expand);
1286 * Like ext4_xattr_set_handle, but start from an inode. This extended
1287 * attribute modification is a filesystem transaction by itself.
1289 * Returns 0, or a negative error number on failure.
1292 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1293 const void *value, size_t value_len, int flags)
1296 int error, retries = 0;
1297 int credits = ext4_jbd2_credits_xattr(inode);
1300 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1301 if (IS_ERR(handle)) {
1302 error = PTR_ERR(handle);
1306 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1307 value, value_len, flags);
1308 error2 = ext4_journal_stop(handle);
1309 if (error == -ENOSPC &&
1310 ext4_should_retry_alloc(inode->i_sb, &retries))
1320 * Shift the EA entries in the inode to create space for the increased
1323 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1324 int value_offs_shift, void *to,
1325 void *from, size_t n)
1327 struct ext4_xattr_entry *last = entry;
1330 /* We always shift xattr headers further thus offsets get lower */
1331 BUG_ON(value_offs_shift > 0);
1333 /* Adjust the value offsets of the entries */
1334 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1335 if (last->e_value_size) {
1336 new_offs = le16_to_cpu(last->e_value_offs) +
1338 last->e_value_offs = cpu_to_le16(new_offs);
1341 /* Shift the entries by n bytes */
1342 memmove(to, from, n);
1346 * Move xattr pointed to by 'entry' from inode into external xattr block
1348 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1349 struct ext4_inode *raw_inode,
1350 struct ext4_xattr_entry *entry)
1352 struct ext4_xattr_ibody_find *is = NULL;
1353 struct ext4_xattr_block_find *bs = NULL;
1354 char *buffer = NULL, *b_entry_name = NULL;
1355 size_t value_offs, value_size;
1356 struct ext4_xattr_info i = {
1359 .name_index = entry->e_name_index,
1361 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1364 value_offs = le16_to_cpu(entry->e_value_offs);
1365 value_size = le32_to_cpu(entry->e_value_size);
1367 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1368 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1369 buffer = kmalloc(value_size, GFP_NOFS);
1370 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1371 if (!is || !bs || !buffer || !b_entry_name) {
1376 is->s.not_found = -ENODATA;
1377 bs->s.not_found = -ENODATA;
1381 /* Save the entry name and the entry value */
1382 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1383 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1384 b_entry_name[entry->e_name_len] = '\0';
1385 i.name = b_entry_name;
1387 error = ext4_get_inode_loc(inode, &is->iloc);
1391 error = ext4_xattr_ibody_find(inode, &i, is);
1395 /* Remove the chosen entry from the inode */
1396 error = ext4_xattr_ibody_set(inode, &i, is);
1400 i.name = b_entry_name;
1402 i.value_len = value_size;
1403 error = ext4_xattr_block_find(inode, &i, bs);
1407 /* Add entry which was removed from the inode into the block */
1408 error = ext4_xattr_block_set(handle, inode, &i, bs);
1413 kfree(b_entry_name);
1416 brelse(is->iloc.bh);
1423 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1424 struct ext4_inode *raw_inode,
1425 int isize_diff, size_t ifree,
1426 size_t bfree, int *total_ino)
1428 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1429 struct ext4_xattr_entry *small_entry;
1430 struct ext4_xattr_entry *entry;
1431 struct ext4_xattr_entry *last;
1432 unsigned int entry_size; /* EA entry size */
1433 unsigned int total_size; /* EA entry size + value size */
1434 unsigned int min_total_size;
1437 while (isize_diff > ifree) {
1440 min_total_size = ~0U;
1441 last = IFIRST(header);
1442 /* Find the entry best suited to be pushed into EA block */
1443 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1445 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1446 EXT4_XATTR_LEN(last->e_name_len);
1447 if (total_size <= bfree &&
1448 total_size < min_total_size) {
1449 if (total_size + ifree < isize_diff) {
1453 min_total_size = total_size;
1458 if (entry == NULL) {
1459 if (small_entry == NULL)
1461 entry = small_entry;
1464 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1465 total_size = entry_size +
1466 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1467 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1472 *total_ino -= entry_size;
1473 ifree += total_size;
1474 bfree -= total_size;
1481 * Expand an inode by new_extra_isize bytes when EAs are present.
1482 * Returns 0 on success or negative error number on failure.
1484 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1485 struct ext4_inode *raw_inode, handle_t *handle)
1487 struct ext4_xattr_ibody_header *header;
1488 struct buffer_head *bh = NULL;
1490 size_t ifree, bfree;
1493 int error = 0, tried_min_extra_isize = 0;
1494 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1495 int isize_diff; /* How much do we need to grow i_extra_isize */
1498 if (ext4_write_trylock_xattr(inode, &no_expand) == 0)
1502 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
1503 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1506 header = IHDR(inode, raw_inode);
1509 * Check if enough free space is available in the inode to shift the
1510 * entries ahead by new_extra_isize.
1513 base = IFIRST(header);
1514 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1515 min_offs = end - base;
1516 total_ino = sizeof(struct ext4_xattr_ibody_header);
1518 error = xattr_check_inode(inode, header, end);
1522 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
1523 if (ifree >= isize_diff)
1527 * Enough free space isn't available in the inode, check if
1528 * EA block can hold new_extra_isize bytes.
1530 if (EXT4_I(inode)->i_file_acl) {
1531 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1535 if (ext4_xattr_check_block(inode, bh)) {
1536 EXT4_ERROR_INODE(inode, "bad block %llu",
1537 EXT4_I(inode)->i_file_acl);
1538 error = -EFSCORRUPTED;
1542 end = bh->b_data + bh->b_size;
1543 min_offs = end - base;
1544 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1546 if (bfree + ifree < isize_diff) {
1547 if (!tried_min_extra_isize && s_min_extra_isize) {
1548 tried_min_extra_isize++;
1549 new_extra_isize = s_min_extra_isize;
1557 bfree = inode->i_sb->s_blocksize;
1560 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1561 isize_diff, ifree, bfree,
1564 if (error == -ENOSPC && !tried_min_extra_isize &&
1565 s_min_extra_isize) {
1566 tried_min_extra_isize++;
1567 new_extra_isize = s_min_extra_isize;
1574 /* Adjust the offsets and shift the remaining entries ahead */
1575 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
1576 - new_extra_isize, (void *)raw_inode +
1577 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1578 (void *)header, total_ino);
1579 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1582 ext4_write_unlock_xattr(inode, &no_expand);
1588 * Inode size expansion failed; don't try again
1591 ext4_write_unlock_xattr(inode, &no_expand);
1598 * ext4_xattr_delete_inode()
1600 * Free extended attribute resources associated with this inode. This
1601 * is called immediately before an inode is freed. We have exclusive
1602 * access to the inode.
1605 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1607 struct buffer_head *bh = NULL;
1609 if (!EXT4_I(inode)->i_file_acl)
1611 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1613 EXT4_ERROR_INODE(inode, "block %llu read error",
1614 EXT4_I(inode)->i_file_acl);
1617 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1618 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1619 EXT4_ERROR_INODE(inode, "bad block %llu",
1620 EXT4_I(inode)->i_file_acl);
1623 ext4_xattr_release_block(handle, inode, bh);
1624 EXT4_I(inode)->i_file_acl = 0;
1631 * ext4_xattr_cache_insert()
1633 * Create a new entry in the extended attribute cache, and insert
1634 * it unless such an entry is already in the cache.
1636 * Returns 0, or a negative error number on failure.
1639 ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
1641 struct ext4_xattr_header *header = BHDR(bh);
1642 __u32 hash = le32_to_cpu(header->h_hash);
1643 int reusable = le32_to_cpu(header->h_refcount) <
1644 EXT4_XATTR_REFCOUNT_MAX;
1647 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
1648 bh->b_blocknr, reusable);
1650 if (error == -EBUSY)
1651 ea_bdebug(bh, "already in cache");
1653 ea_bdebug(bh, "inserting [%x]", (int)hash);
1659 * Compare two extended attribute blocks for equality.
1661 * Returns 0 if the blocks are equal, 1 if they differ, and
1662 * a negative error number on errors.
1665 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1666 struct ext4_xattr_header *header2)
1668 struct ext4_xattr_entry *entry1, *entry2;
1670 entry1 = ENTRY(header1+1);
1671 entry2 = ENTRY(header2+1);
1672 while (!IS_LAST_ENTRY(entry1)) {
1673 if (IS_LAST_ENTRY(entry2))
1675 if (entry1->e_hash != entry2->e_hash ||
1676 entry1->e_name_index != entry2->e_name_index ||
1677 entry1->e_name_len != entry2->e_name_len ||
1678 entry1->e_value_size != entry2->e_value_size ||
1679 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1681 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1682 return -EFSCORRUPTED;
1683 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1684 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1685 le32_to_cpu(entry1->e_value_size)))
1688 entry1 = EXT4_XATTR_NEXT(entry1);
1689 entry2 = EXT4_XATTR_NEXT(entry2);
1691 if (!IS_LAST_ENTRY(entry2))
1697 * ext4_xattr_cache_find()
1699 * Find an identical extended attribute block.
1701 * Returns a pointer to the block found, or NULL if such a block was
1702 * not found or an error occurred.
1704 static struct buffer_head *
1705 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1706 struct mb_cache_entry **pce)
1708 __u32 hash = le32_to_cpu(header->h_hash);
1709 struct mb_cache_entry *ce;
1710 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1712 if (!header->h_hash)
1713 return NULL; /* never share */
1714 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1715 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
1717 struct buffer_head *bh;
1719 bh = sb_bread(inode->i_sb, ce->e_block);
1721 EXT4_ERROR_INODE(inode, "block %lu read error",
1722 (unsigned long) ce->e_block);
1723 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1728 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
1733 #define NAME_HASH_SHIFT 5
1734 #define VALUE_HASH_SHIFT 16
1737 * ext4_xattr_hash_entry()
1739 * Compute the hash of an extended attribute.
1741 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1742 struct ext4_xattr_entry *entry)
1745 char *name = entry->e_name;
1748 for (n = 0; n < entry->e_name_len; n++) {
1749 hash = (hash << NAME_HASH_SHIFT) ^
1750 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1754 if (entry->e_value_size != 0) {
1755 __le32 *value = (__le32 *)((char *)header +
1756 le16_to_cpu(entry->e_value_offs));
1757 for (n = (le32_to_cpu(entry->e_value_size) +
1758 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1759 hash = (hash << VALUE_HASH_SHIFT) ^
1760 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1761 le32_to_cpu(*value++);
1764 entry->e_hash = cpu_to_le32(hash);
1767 #undef NAME_HASH_SHIFT
1768 #undef VALUE_HASH_SHIFT
1770 #define BLOCK_HASH_SHIFT 16
1773 * ext4_xattr_rehash()
1775 * Re-compute the extended attribute hash value after an entry has changed.
1777 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1778 struct ext4_xattr_entry *entry)
1780 struct ext4_xattr_entry *here;
1783 ext4_xattr_hash_entry(header, entry);
1784 here = ENTRY(header+1);
1785 while (!IS_LAST_ENTRY(here)) {
1786 if (!here->e_hash) {
1787 /* Block is not shared if an entry's hash value == 0 */
1791 hash = (hash << BLOCK_HASH_SHIFT) ^
1792 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1793 le32_to_cpu(here->e_hash);
1794 here = EXT4_XATTR_NEXT(here);
1796 header->h_hash = cpu_to_le32(hash);
1799 #undef BLOCK_HASH_SHIFT
1801 #define HASH_BUCKET_BITS 10
1804 ext4_xattr_create_cache(void)
1806 return mb_cache_create(HASH_BUCKET_BITS);
1809 void ext4_xattr_destroy_cache(struct mb_cache *cache)
1812 mb_cache_destroy(cache);