2 * linux/fs/ext4/xattr.c
8 * Extended attributes for symlinks and special files added per
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,
135 struct ext4_xattr_header *hdr)
137 if (ext4_has_metadata_csum(inode->i_sb) &&
138 (hdr->h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
143 static void ext4_xattr_block_csum_set(struct inode *inode,
145 struct ext4_xattr_header *hdr)
147 if (!ext4_has_metadata_csum(inode->i_sb))
150 hdr->h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
153 static inline int ext4_handle_dirty_xattr_block(handle_t *handle,
155 struct buffer_head *bh)
157 ext4_xattr_block_csum_set(inode, bh->b_blocknr, BHDR(bh));
158 return ext4_handle_dirty_metadata(handle, inode, bh);
161 static inline const struct xattr_handler *
162 ext4_xattr_handler(int name_index)
164 const struct xattr_handler *handler = NULL;
166 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
167 handler = ext4_xattr_handler_map[name_index];
172 * Inode operation listxattr()
174 * d_inode(dentry)->i_mutex: don't care
177 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
179 return ext4_xattr_list(dentry, buffer, size);
183 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
186 struct ext4_xattr_entry *e = entry;
188 /* Find the end of the names list */
189 while (!IS_LAST_ENTRY(e)) {
190 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
191 if ((void *)next >= end)
192 return -EFSCORRUPTED;
196 /* Check the values */
197 while (!IS_LAST_ENTRY(entry)) {
198 if (entry->e_value_block != 0)
199 return -EFSCORRUPTED;
200 if (entry->e_value_size != 0) {
201 u16 offs = le16_to_cpu(entry->e_value_offs);
202 u32 size = le32_to_cpu(entry->e_value_size);
206 * The value cannot overlap the names, and the value
207 * with padding cannot extend beyond 'end'. Check both
208 * the padded and unpadded sizes, since the size may
209 * overflow to 0 when adding padding.
211 if (offs > end - value_start)
212 return -EFSCORRUPTED;
213 value = value_start + offs;
214 if (value < (void *)e + sizeof(u32) ||
215 size > end - value ||
216 EXT4_XATTR_SIZE(size) > end - value)
217 return -EFSCORRUPTED;
219 entry = EXT4_XATTR_NEXT(entry);
226 ext4_xattr_check_block(struct inode *inode, struct buffer_head *bh)
230 if (buffer_verified(bh))
233 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
234 BHDR(bh)->h_blocks != cpu_to_le32(1))
235 return -EFSCORRUPTED;
236 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr, BHDR(bh)))
238 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
241 set_buffer_verified(bh);
246 __xattr_check_inode(struct inode *inode, struct ext4_xattr_ibody_header *header,
247 void *end, const char *function, unsigned int line)
249 int error = -EFSCORRUPTED;
251 if (end - (void *)header < sizeof(*header) + sizeof(u32) ||
252 (header->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC)))
254 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
257 __ext4_error_inode(inode, function, line, 0,
258 "corrupted in-inode xattr");
262 #define xattr_check_inode(inode, header, end) \
263 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
266 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
268 size_t value_size = le32_to_cpu(entry->e_value_size);
270 if (entry->e_value_block != 0 || value_size > size ||
271 le16_to_cpu(entry->e_value_offs) + value_size > size)
272 return -EFSCORRUPTED;
277 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
278 const char *name, size_t size, int sorted)
280 struct ext4_xattr_entry *entry;
286 name_len = strlen(name);
288 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
289 cmp = name_index - entry->e_name_index;
291 cmp = name_len - entry->e_name_len;
293 cmp = memcmp(name, entry->e_name, name_len);
294 if (cmp <= 0 && (sorted || cmp == 0))
298 if (!cmp && ext4_xattr_check_entry(entry, size))
299 return -EFSCORRUPTED;
300 return cmp ? -ENODATA : 0;
304 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
305 void *buffer, size_t buffer_size)
307 struct buffer_head *bh = NULL;
308 struct ext4_xattr_entry *entry;
311 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
313 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
314 name_index, name, buffer, (long)buffer_size);
317 if (!EXT4_I(inode)->i_file_acl)
319 ea_idebug(inode, "reading block %llu",
320 (unsigned long long)EXT4_I(inode)->i_file_acl);
321 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
324 ea_bdebug(bh, "b_count=%d, refcount=%d",
325 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
326 if (ext4_xattr_check_block(inode, bh)) {
328 EXT4_ERROR_INODE(inode, "bad block %llu",
329 EXT4_I(inode)->i_file_acl);
330 error = -EFSCORRUPTED;
333 ext4_xattr_cache_insert(ext4_mb_cache, bh);
335 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
336 if (error == -EFSCORRUPTED)
340 size = le32_to_cpu(entry->e_value_size);
343 if (size > buffer_size)
345 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
356 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
357 void *buffer, size_t buffer_size)
359 struct ext4_xattr_ibody_header *header;
360 struct ext4_xattr_entry *entry;
361 struct ext4_inode *raw_inode;
362 struct ext4_iloc iloc;
367 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
369 error = ext4_get_inode_loc(inode, &iloc);
372 raw_inode = ext4_raw_inode(&iloc);
373 header = IHDR(inode, raw_inode);
374 entry = IFIRST(header);
375 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
376 error = xattr_check_inode(inode, header, end);
379 error = ext4_xattr_find_entry(&entry, name_index, name,
380 end - (void *)entry, 0);
383 size = le32_to_cpu(entry->e_value_size);
386 if (size > buffer_size)
388 memcpy(buffer, (void *)IFIRST(header) +
389 le16_to_cpu(entry->e_value_offs), size);
401 * Copy an extended attribute into the buffer
402 * provided, or compute the buffer size required.
403 * Buffer is NULL to compute the size of the buffer required.
405 * Returns a negative error number on failure, or the number of bytes
406 * used / required on success.
409 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
410 void *buffer, size_t buffer_size)
414 if (strlen(name) > 255)
417 down_read(&EXT4_I(inode)->xattr_sem);
418 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
420 if (error == -ENODATA)
421 error = ext4_xattr_block_get(inode, name_index, name, buffer,
423 up_read(&EXT4_I(inode)->xattr_sem);
428 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
429 char *buffer, size_t buffer_size)
431 size_t rest = buffer_size;
433 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
434 const struct xattr_handler *handler =
435 ext4_xattr_handler(entry->e_name_index);
437 if (handler && (!handler->list || handler->list(dentry))) {
438 const char *prefix = handler->prefix ?: handler->name;
439 size_t prefix_len = strlen(prefix);
440 size_t size = prefix_len + entry->e_name_len + 1;
445 memcpy(buffer, prefix, prefix_len);
446 buffer += prefix_len;
447 memcpy(buffer, entry->e_name, entry->e_name_len);
448 buffer += entry->e_name_len;
454 return buffer_size - rest; /* total size */
458 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
460 struct inode *inode = d_inode(dentry);
461 struct buffer_head *bh = NULL;
463 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
465 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
466 buffer, (long)buffer_size);
469 if (!EXT4_I(inode)->i_file_acl)
471 ea_idebug(inode, "reading block %llu",
472 (unsigned long long)EXT4_I(inode)->i_file_acl);
473 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
477 ea_bdebug(bh, "b_count=%d, refcount=%d",
478 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
479 if (ext4_xattr_check_block(inode, bh)) {
480 EXT4_ERROR_INODE(inode, "bad block %llu",
481 EXT4_I(inode)->i_file_acl);
482 error = -EFSCORRUPTED;
485 ext4_xattr_cache_insert(ext4_mb_cache, bh);
486 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
495 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
497 struct inode *inode = d_inode(dentry);
498 struct ext4_xattr_ibody_header *header;
499 struct ext4_inode *raw_inode;
500 struct ext4_iloc iloc;
504 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
506 error = ext4_get_inode_loc(inode, &iloc);
509 raw_inode = ext4_raw_inode(&iloc);
510 header = IHDR(inode, raw_inode);
511 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
512 error = xattr_check_inode(inode, header, end);
515 error = ext4_xattr_list_entries(dentry, IFIRST(header),
516 buffer, buffer_size);
526 * Copy a list of attribute names into the buffer
527 * provided, or compute the buffer size required.
528 * Buffer is NULL to compute the size of the buffer required.
530 * Returns a negative error number on failure, or the number of bytes
531 * used / required on success.
534 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
538 down_read(&EXT4_I(d_inode(dentry))->xattr_sem);
539 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
546 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
551 up_read(&EXT4_I(d_inode(dentry))->xattr_sem);
556 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
559 static void ext4_xattr_update_super_block(handle_t *handle,
560 struct super_block *sb)
562 if (ext4_has_feature_xattr(sb))
565 BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access");
566 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
567 ext4_set_feature_xattr(sb);
568 ext4_handle_dirty_super(handle, sb);
573 * Release the xattr block BH: If the reference count is > 1, decrement it;
574 * otherwise free the block.
577 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
578 struct buffer_head *bh)
580 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
584 BUFFER_TRACE(bh, "get_write_access");
585 error = ext4_journal_get_write_access(handle, bh);
590 hash = le32_to_cpu(BHDR(bh)->h_hash);
591 ref = le32_to_cpu(BHDR(bh)->h_refcount);
593 ea_bdebug(bh, "refcount now=0; freeing");
595 * This must happen under buffer lock for
596 * ext4_xattr_block_set() to reliably detect freed block
598 mb_cache_entry_delete_block(ext4_mb_cache, hash, bh->b_blocknr);
601 ext4_free_blocks(handle, inode, bh, 0, 1,
602 EXT4_FREE_BLOCKS_METADATA |
603 EXT4_FREE_BLOCKS_FORGET);
606 BHDR(bh)->h_refcount = cpu_to_le32(ref);
607 if (ref == EXT4_XATTR_REFCOUNT_MAX - 1) {
608 struct mb_cache_entry *ce;
610 ce = mb_cache_entry_get(ext4_mb_cache, hash,
614 mb_cache_entry_put(ext4_mb_cache, ce);
619 * Beware of this ugliness: Releasing of xattr block references
620 * from different inodes can race and so we have to protect
621 * from a race where someone else frees the block (and releases
622 * its journal_head) before we are done dirtying the buffer. In
623 * nojournal mode this race is harmless and we actually cannot
624 * call ext4_handle_dirty_xattr_block() with locked buffer as
625 * that function can call sync_dirty_buffer() so for that case
626 * we handle the dirtying after unlocking the buffer.
628 if (ext4_handle_valid(handle))
629 error = ext4_handle_dirty_xattr_block(handle, inode,
632 if (!ext4_handle_valid(handle))
633 error = ext4_handle_dirty_xattr_block(handle, inode,
636 ext4_handle_sync(handle);
637 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
638 ea_bdebug(bh, "refcount now=%d; releasing",
639 le32_to_cpu(BHDR(bh)->h_refcount));
642 ext4_std_error(inode->i_sb, error);
647 * Find the available free space for EAs. This also returns the total number of
648 * bytes used by EA entries.
650 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
651 size_t *min_offs, void *base, int *total)
653 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
654 if (last->e_value_size) {
655 size_t offs = le16_to_cpu(last->e_value_offs);
656 if (offs < *min_offs)
660 *total += EXT4_XATTR_LEN(last->e_name_len);
662 return (*min_offs - ((void *)last - base) - sizeof(__u32));
666 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
668 struct ext4_xattr_entry *last;
669 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
671 /* Compute min_offs and last. */
673 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
674 if (last->e_value_size) {
675 size_t offs = le16_to_cpu(last->e_value_offs);
680 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
682 if (s->here->e_value_size) {
683 size_t size = le32_to_cpu(s->here->e_value_size);
684 free += EXT4_XATTR_SIZE(size);
686 free += EXT4_XATTR_LEN(name_len);
689 if (free < EXT4_XATTR_LEN(name_len) +
690 EXT4_XATTR_SIZE(i->value_len))
694 if (i->value && s->not_found) {
695 /* Insert the new name. */
696 size_t size = EXT4_XATTR_LEN(name_len);
697 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
698 memmove((void *)s->here + size, s->here, rest);
699 memset(s->here, 0, size);
700 s->here->e_name_index = i->name_index;
701 s->here->e_name_len = name_len;
702 memcpy(s->here->e_name, i->name, name_len);
704 if (s->here->e_value_size) {
705 void *first_val = s->base + min_offs;
706 size_t offs = le16_to_cpu(s->here->e_value_offs);
707 void *val = s->base + offs;
708 size_t size = EXT4_XATTR_SIZE(
709 le32_to_cpu(s->here->e_value_size));
711 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
712 /* The old and the new value have the same
713 size. Just replace. */
714 s->here->e_value_size =
715 cpu_to_le32(i->value_len);
716 if (i->value == EXT4_ZERO_XATTR_VALUE) {
717 memset(val, 0, size);
719 /* Clear pad bytes first. */
720 memset(val + size - EXT4_XATTR_PAD, 0,
722 memcpy(val, i->value, i->value_len);
727 /* Remove the old value. */
728 memmove(first_val + size, first_val, val - first_val);
729 memset(first_val, 0, size);
730 s->here->e_value_size = 0;
731 s->here->e_value_offs = 0;
734 /* Adjust all value offsets. */
736 while (!IS_LAST_ENTRY(last)) {
737 size_t o = le16_to_cpu(last->e_value_offs);
738 if (last->e_value_size && o < offs)
740 cpu_to_le16(o + size);
741 last = EXT4_XATTR_NEXT(last);
745 /* Remove the old name. */
746 size_t size = EXT4_XATTR_LEN(name_len);
747 last = ENTRY((void *)last - size);
748 memmove(s->here, (void *)s->here + size,
749 (void *)last - (void *)s->here + sizeof(__u32));
750 memset(last, 0, size);
755 /* Insert the new value. */
756 s->here->e_value_size = cpu_to_le32(i->value_len);
758 size_t size = EXT4_XATTR_SIZE(i->value_len);
759 void *val = s->base + min_offs - size;
760 s->here->e_value_offs = cpu_to_le16(min_offs - size);
761 if (i->value == EXT4_ZERO_XATTR_VALUE) {
762 memset(val, 0, size);
764 /* Clear the pad bytes first. */
765 memset(val + size - EXT4_XATTR_PAD, 0,
767 memcpy(val, i->value, i->value_len);
774 struct ext4_xattr_block_find {
775 struct ext4_xattr_search s;
776 struct buffer_head *bh;
780 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
781 struct ext4_xattr_block_find *bs)
783 struct super_block *sb = inode->i_sb;
786 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
787 i->name_index, i->name, i->value, (long)i->value_len);
789 if (EXT4_I(inode)->i_file_acl) {
790 /* The inode already has an extended attribute block. */
791 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
795 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
796 atomic_read(&(bs->bh->b_count)),
797 le32_to_cpu(BHDR(bs->bh)->h_refcount));
798 if (ext4_xattr_check_block(inode, bs->bh)) {
799 EXT4_ERROR_INODE(inode, "bad block %llu",
800 EXT4_I(inode)->i_file_acl);
801 error = -EFSCORRUPTED;
804 /* Find the named attribute. */
805 bs->s.base = BHDR(bs->bh);
806 bs->s.first = BFIRST(bs->bh);
807 bs->s.end = bs->bh->b_data + bs->bh->b_size;
808 bs->s.here = bs->s.first;
809 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
810 i->name, bs->bh->b_size, 1);
811 if (error && error != -ENODATA)
813 bs->s.not_found = error;
822 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
823 struct ext4_xattr_info *i,
824 struct ext4_xattr_block_find *bs)
826 struct super_block *sb = inode->i_sb;
827 struct buffer_head *new_bh = NULL;
828 struct ext4_xattr_search *s = &bs->s;
829 struct mb_cache_entry *ce = NULL;
831 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
833 #define header(x) ((struct ext4_xattr_header *)(x))
835 if (i->value && i->value_len > sb->s_blocksize)
838 BUFFER_TRACE(bs->bh, "get_write_access");
839 error = ext4_journal_get_write_access(handle, bs->bh);
844 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
845 __u32 hash = le32_to_cpu(BHDR(bs->bh)->h_hash);
848 * This must happen under buffer lock for
849 * ext4_xattr_block_set() to reliably detect modified
852 mb_cache_entry_delete_block(ext4_mb_cache, hash,
854 ea_bdebug(bs->bh, "modifying in-place");
855 error = ext4_xattr_set_entry(i, s);
857 if (!IS_LAST_ENTRY(s->first))
858 ext4_xattr_rehash(header(s->base),
860 ext4_xattr_cache_insert(ext4_mb_cache,
863 unlock_buffer(bs->bh);
864 if (error == -EFSCORRUPTED)
867 error = ext4_handle_dirty_xattr_block(handle,
874 int offset = (char *)s->here - bs->bh->b_data;
876 unlock_buffer(bs->bh);
877 ea_bdebug(bs->bh, "cloning");
878 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
882 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
883 s->first = ENTRY(header(s->base)+1);
884 header(s->base)->h_refcount = cpu_to_le32(1);
885 s->here = ENTRY(s->base + offset);
886 s->end = s->base + bs->bh->b_size;
889 /* Allocate a buffer where we construct the new block. */
890 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
891 /* assert(header == s->base) */
895 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
896 header(s->base)->h_blocks = cpu_to_le32(1);
897 header(s->base)->h_refcount = cpu_to_le32(1);
898 s->first = ENTRY(header(s->base)+1);
899 s->here = ENTRY(header(s->base)+1);
900 s->end = s->base + sb->s_blocksize;
903 error = ext4_xattr_set_entry(i, s);
904 if (error == -EFSCORRUPTED)
908 if (!IS_LAST_ENTRY(s->first))
909 ext4_xattr_rehash(header(s->base), s->here);
912 if (!IS_LAST_ENTRY(s->first)) {
913 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
915 /* We found an identical block in the cache. */
916 if (new_bh == bs->bh)
917 ea_bdebug(new_bh, "keeping");
921 /* The old block is released after updating
923 error = dquot_alloc_block(inode,
924 EXT4_C2B(EXT4_SB(sb), 1));
927 BUFFER_TRACE(new_bh, "get_write_access");
928 error = ext4_journal_get_write_access(handle,
934 * We have to be careful about races with
935 * freeing, rehashing or adding references to
936 * xattr block. Once we hold buffer lock xattr
937 * block's state is stable so we can check
938 * whether the block got freed / rehashed or
939 * not. Since we unhash mbcache entry under
940 * buffer lock when freeing / rehashing xattr
941 * block, checking whether entry is still
942 * hashed is reliable. Same rules hold for
943 * e_reusable handling.
945 if (hlist_bl_unhashed(&ce->e_hash_list) ||
948 * Undo everything and check mbcache
951 unlock_buffer(new_bh);
952 dquot_free_block(inode,
953 EXT4_C2B(EXT4_SB(sb),
956 mb_cache_entry_put(ext4_mb_cache, ce);
961 ref = le32_to_cpu(BHDR(new_bh)->h_refcount) + 1;
962 BHDR(new_bh)->h_refcount = cpu_to_le32(ref);
963 if (ref >= EXT4_XATTR_REFCOUNT_MAX)
965 ea_bdebug(new_bh, "reusing; refcount now=%d",
967 unlock_buffer(new_bh);
968 error = ext4_handle_dirty_xattr_block(handle,
974 mb_cache_entry_touch(ext4_mb_cache, ce);
975 mb_cache_entry_put(ext4_mb_cache, ce);
977 } else if (bs->bh && s->base == bs->bh->b_data) {
978 /* We were modifying this block in-place. */
979 ea_bdebug(bs->bh, "keeping this block");
983 /* We need to allocate a new block */
984 ext4_fsblk_t goal, block;
986 goal = ext4_group_first_block_no(sb,
987 EXT4_I(inode)->i_block_group);
989 /* non-extent files can't have physical blocks past 2^32 */
990 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
991 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
993 block = ext4_new_meta_blocks(handle, inode, goal, 0,
998 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
999 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
1001 ea_idebug(inode, "creating block %llu",
1002 (unsigned long long)block);
1004 new_bh = sb_getblk(sb, block);
1005 if (unlikely(!new_bh)) {
1008 ext4_free_blocks(handle, inode, NULL, block, 1,
1009 EXT4_FREE_BLOCKS_METADATA);
1012 lock_buffer(new_bh);
1013 error = ext4_journal_get_create_access(handle, new_bh);
1015 unlock_buffer(new_bh);
1019 memcpy(new_bh->b_data, s->base, new_bh->b_size);
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_xattr_block(handle,
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, },
1191 unsigned long no_expand;
1196 if (strlen(name) > 255)
1198 down_write(&EXT4_I(inode)->xattr_sem);
1199 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1200 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1202 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1206 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1207 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1208 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1209 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1212 error = ext4_xattr_ibody_find(inode, &i, &is);
1216 error = ext4_xattr_block_find(inode, &i, &bs);
1219 if (is.s.not_found && bs.s.not_found) {
1221 if (flags & XATTR_REPLACE)
1228 if (flags & XATTR_CREATE)
1232 if (!is.s.not_found)
1233 error = ext4_xattr_ibody_set(inode, &i, &is);
1234 else if (!bs.s.not_found)
1235 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1238 /* Xattr value did not change? Save us some work and bail out */
1239 if (!is.s.not_found && ext4_xattr_value_same(&is.s, &i))
1241 if (!bs.s.not_found && ext4_xattr_value_same(&bs.s, &i))
1244 error = ext4_xattr_ibody_set(inode, &i, &is);
1245 if (!error && !bs.s.not_found) {
1247 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1248 } else if (error == -ENOSPC) {
1249 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1250 error = ext4_xattr_block_find(inode, &i, &bs);
1254 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1257 if (!is.s.not_found) {
1259 error = ext4_xattr_ibody_set(inode, &i, &is);
1264 ext4_xattr_update_super_block(handle, inode->i_sb);
1265 inode->i_ctime = current_time(inode);
1267 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1268 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1270 * The bh is consumed by ext4_mark_iloc_dirty, even with
1275 ext4_handle_sync(handle);
1282 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1283 up_write(&EXT4_I(inode)->xattr_sem);
1290 * Like ext4_xattr_set_handle, but start from an inode. This extended
1291 * attribute modification is a filesystem transaction by itself.
1293 * Returns 0, or a negative error number on failure.
1296 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1297 const void *value, size_t value_len, int flags)
1300 int error, retries = 0;
1301 int credits = ext4_jbd2_credits_xattr(inode);
1304 handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
1305 if (IS_ERR(handle)) {
1306 error = PTR_ERR(handle);
1310 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1311 value, value_len, flags);
1312 error2 = ext4_journal_stop(handle);
1313 if (error == -ENOSPC &&
1314 ext4_should_retry_alloc(inode->i_sb, &retries))
1324 * Shift the EA entries in the inode to create space for the increased
1327 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1328 int value_offs_shift, void *to,
1329 void *from, size_t n)
1331 struct ext4_xattr_entry *last = entry;
1334 /* We always shift xattr headers further thus offsets get lower */
1335 BUG_ON(value_offs_shift > 0);
1337 /* Adjust the value offsets of the entries */
1338 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1339 if (last->e_value_size) {
1340 new_offs = le16_to_cpu(last->e_value_offs) +
1342 last->e_value_offs = cpu_to_le16(new_offs);
1345 /* Shift the entries by n bytes */
1346 memmove(to, from, n);
1350 * Move xattr pointed to by 'entry' from inode into external xattr block
1352 static int ext4_xattr_move_to_block(handle_t *handle, struct inode *inode,
1353 struct ext4_inode *raw_inode,
1354 struct ext4_xattr_entry *entry)
1356 struct ext4_xattr_ibody_find *is = NULL;
1357 struct ext4_xattr_block_find *bs = NULL;
1358 char *buffer = NULL, *b_entry_name = NULL;
1359 size_t value_offs, value_size;
1360 struct ext4_xattr_info i = {
1363 .name_index = entry->e_name_index,
1365 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1368 value_offs = le16_to_cpu(entry->e_value_offs);
1369 value_size = le32_to_cpu(entry->e_value_size);
1371 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1372 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1373 buffer = kmalloc(value_size, GFP_NOFS);
1374 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1375 if (!is || !bs || !buffer || !b_entry_name) {
1380 is->s.not_found = -ENODATA;
1381 bs->s.not_found = -ENODATA;
1385 /* Save the entry name and the entry value */
1386 memcpy(buffer, (void *)IFIRST(header) + value_offs, value_size);
1387 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1388 b_entry_name[entry->e_name_len] = '\0';
1389 i.name = b_entry_name;
1391 error = ext4_get_inode_loc(inode, &is->iloc);
1395 error = ext4_xattr_ibody_find(inode, &i, is);
1399 /* Remove the chosen entry from the inode */
1400 error = ext4_xattr_ibody_set(inode, &i, is);
1404 i.name = b_entry_name;
1406 i.value_len = value_size;
1407 error = ext4_xattr_block_find(inode, &i, bs);
1411 /* Add entry which was removed from the inode into the block */
1412 error = ext4_xattr_block_set(handle, inode, &i, bs);
1417 kfree(b_entry_name);
1420 brelse(is->iloc.bh);
1427 static int ext4_xattr_make_inode_space(handle_t *handle, struct inode *inode,
1428 struct ext4_inode *raw_inode,
1429 int isize_diff, size_t ifree,
1430 size_t bfree, int *total_ino)
1432 struct ext4_xattr_ibody_header *header = IHDR(inode, raw_inode);
1433 struct ext4_xattr_entry *small_entry;
1434 struct ext4_xattr_entry *entry;
1435 struct ext4_xattr_entry *last;
1436 unsigned int entry_size; /* EA entry size */
1437 unsigned int total_size; /* EA entry size + value size */
1438 unsigned int min_total_size;
1441 while (isize_diff > ifree) {
1444 min_total_size = ~0U;
1445 last = IFIRST(header);
1446 /* Find the entry best suited to be pushed into EA block */
1447 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1449 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1450 EXT4_XATTR_LEN(last->e_name_len);
1451 if (total_size <= bfree &&
1452 total_size < min_total_size) {
1453 if (total_size + ifree < isize_diff) {
1457 min_total_size = total_size;
1462 if (entry == NULL) {
1463 if (small_entry == NULL)
1465 entry = small_entry;
1468 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1469 total_size = entry_size +
1470 EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size));
1471 error = ext4_xattr_move_to_block(handle, inode, raw_inode,
1476 *total_ino -= entry_size;
1477 ifree += total_size;
1478 bfree -= total_size;
1485 * Expand an inode by new_extra_isize bytes when EAs are present.
1486 * Returns 0 on success or negative error number on failure.
1488 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1489 struct ext4_inode *raw_inode, handle_t *handle)
1491 struct ext4_xattr_ibody_header *header;
1492 struct buffer_head *bh = NULL;
1494 size_t ifree, bfree;
1497 int error = 0, tried_min_extra_isize = 0;
1498 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1499 int isize_diff; /* How much do we need to grow i_extra_isize */
1501 down_write(&EXT4_I(inode)->xattr_sem);
1503 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1505 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1507 isize_diff = new_extra_isize - EXT4_I(inode)->i_extra_isize;
1508 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize)
1511 header = IHDR(inode, raw_inode);
1514 * Check if enough free space is available in the inode to shift the
1515 * entries ahead by new_extra_isize.
1518 base = IFIRST(header);
1519 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1520 min_offs = end - base;
1521 total_ino = sizeof(struct ext4_xattr_ibody_header);
1523 error = xattr_check_inode(inode, header, end);
1527 ifree = ext4_xattr_free_space(base, &min_offs, base, &total_ino);
1528 if (ifree >= isize_diff)
1532 * Enough free space isn't available in the inode, check if
1533 * EA block can hold new_extra_isize bytes.
1535 if (EXT4_I(inode)->i_file_acl) {
1536 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1540 if (ext4_xattr_check_block(inode, bh)) {
1541 EXT4_ERROR_INODE(inode, "bad block %llu",
1542 EXT4_I(inode)->i_file_acl);
1543 error = -EFSCORRUPTED;
1547 end = bh->b_data + bh->b_size;
1548 min_offs = end - base;
1549 bfree = ext4_xattr_free_space(BFIRST(bh), &min_offs, base,
1551 if (bfree + ifree < isize_diff) {
1552 if (!tried_min_extra_isize && s_min_extra_isize) {
1553 tried_min_extra_isize++;
1554 new_extra_isize = s_min_extra_isize;
1562 bfree = inode->i_sb->s_blocksize;
1565 error = ext4_xattr_make_inode_space(handle, inode, raw_inode,
1566 isize_diff, ifree, bfree,
1569 if (error == -ENOSPC && !tried_min_extra_isize &&
1570 s_min_extra_isize) {
1571 tried_min_extra_isize++;
1572 new_extra_isize = s_min_extra_isize;
1579 /* Adjust the offsets and shift the remaining entries ahead */
1580 ext4_xattr_shift_entries(IFIRST(header), EXT4_I(inode)->i_extra_isize
1581 - new_extra_isize, (void *)raw_inode +
1582 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1583 (void *)header, total_ino);
1584 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1587 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1588 up_write(&EXT4_I(inode)->xattr_sem);
1594 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1595 * size expansion failed.
1597 up_write(&EXT4_I(inode)->xattr_sem);
1604 * ext4_xattr_delete_inode()
1606 * Free extended attribute resources associated with this inode. This
1607 * is called immediately before an inode is freed. We have exclusive
1608 * access to the inode.
1611 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1613 struct buffer_head *bh = NULL;
1615 if (!EXT4_I(inode)->i_file_acl)
1617 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1619 EXT4_ERROR_INODE(inode, "block %llu read error",
1620 EXT4_I(inode)->i_file_acl);
1623 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1624 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1625 EXT4_ERROR_INODE(inode, "bad block %llu",
1626 EXT4_I(inode)->i_file_acl);
1629 ext4_xattr_release_block(handle, inode, bh);
1630 EXT4_I(inode)->i_file_acl = 0;
1637 * ext4_xattr_cache_insert()
1639 * Create a new entry in the extended attribute cache, and insert
1640 * it unless such an entry is already in the cache.
1642 * Returns 0, or a negative error number on failure.
1645 ext4_xattr_cache_insert(struct mb_cache *ext4_mb_cache, struct buffer_head *bh)
1647 struct ext4_xattr_header *header = BHDR(bh);
1648 __u32 hash = le32_to_cpu(header->h_hash);
1649 int reusable = le32_to_cpu(header->h_refcount) <
1650 EXT4_XATTR_REFCOUNT_MAX;
1653 error = mb_cache_entry_create(ext4_mb_cache, GFP_NOFS, hash,
1654 bh->b_blocknr, reusable);
1656 if (error == -EBUSY)
1657 ea_bdebug(bh, "already in cache");
1659 ea_bdebug(bh, "inserting [%x]", (int)hash);
1665 * Compare two extended attribute blocks for equality.
1667 * Returns 0 if the blocks are equal, 1 if they differ, and
1668 * a negative error number on errors.
1671 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1672 struct ext4_xattr_header *header2)
1674 struct ext4_xattr_entry *entry1, *entry2;
1676 entry1 = ENTRY(header1+1);
1677 entry2 = ENTRY(header2+1);
1678 while (!IS_LAST_ENTRY(entry1)) {
1679 if (IS_LAST_ENTRY(entry2))
1681 if (entry1->e_hash != entry2->e_hash ||
1682 entry1->e_name_index != entry2->e_name_index ||
1683 entry1->e_name_len != entry2->e_name_len ||
1684 entry1->e_value_size != entry2->e_value_size ||
1685 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1687 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1688 return -EFSCORRUPTED;
1689 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1690 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1691 le32_to_cpu(entry1->e_value_size)))
1694 entry1 = EXT4_XATTR_NEXT(entry1);
1695 entry2 = EXT4_XATTR_NEXT(entry2);
1697 if (!IS_LAST_ENTRY(entry2))
1703 * ext4_xattr_cache_find()
1705 * Find an identical extended attribute block.
1707 * Returns a pointer to the block found, or NULL if such a block was
1708 * not found or an error occurred.
1710 static struct buffer_head *
1711 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1712 struct mb_cache_entry **pce)
1714 __u32 hash = le32_to_cpu(header->h_hash);
1715 struct mb_cache_entry *ce;
1716 struct mb_cache *ext4_mb_cache = EXT4_GET_MB_CACHE(inode);
1718 if (!header->h_hash)
1719 return NULL; /* never share */
1720 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1721 ce = mb_cache_entry_find_first(ext4_mb_cache, hash);
1723 struct buffer_head *bh;
1725 bh = sb_bread(inode->i_sb, ce->e_block);
1727 EXT4_ERROR_INODE(inode, "block %lu read error",
1728 (unsigned long) ce->e_block);
1729 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1734 ce = mb_cache_entry_find_next(ext4_mb_cache, ce);
1739 #define NAME_HASH_SHIFT 5
1740 #define VALUE_HASH_SHIFT 16
1743 * ext4_xattr_hash_entry()
1745 * Compute the hash of an extended attribute.
1747 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1748 struct ext4_xattr_entry *entry)
1751 char *name = entry->e_name;
1754 for (n = 0; n < entry->e_name_len; n++) {
1755 hash = (hash << NAME_HASH_SHIFT) ^
1756 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1760 if (entry->e_value_size != 0) {
1761 __le32 *value = (__le32 *)((char *)header +
1762 le16_to_cpu(entry->e_value_offs));
1763 for (n = (le32_to_cpu(entry->e_value_size) +
1764 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1765 hash = (hash << VALUE_HASH_SHIFT) ^
1766 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1767 le32_to_cpu(*value++);
1770 entry->e_hash = cpu_to_le32(hash);
1773 #undef NAME_HASH_SHIFT
1774 #undef VALUE_HASH_SHIFT
1776 #define BLOCK_HASH_SHIFT 16
1779 * ext4_xattr_rehash()
1781 * Re-compute the extended attribute hash value after an entry has changed.
1783 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1784 struct ext4_xattr_entry *entry)
1786 struct ext4_xattr_entry *here;
1789 ext4_xattr_hash_entry(header, entry);
1790 here = ENTRY(header+1);
1791 while (!IS_LAST_ENTRY(here)) {
1792 if (!here->e_hash) {
1793 /* Block is not shared if an entry's hash value == 0 */
1797 hash = (hash << BLOCK_HASH_SHIFT) ^
1798 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1799 le32_to_cpu(here->e_hash);
1800 here = EXT4_XATTR_NEXT(here);
1802 header->h_hash = cpu_to_le32(hash);
1805 #undef BLOCK_HASH_SHIFT
1807 #define HASH_BUCKET_BITS 10
1810 ext4_xattr_create_cache(void)
1812 return mb_cache_create(HASH_BUCKET_BITS);
1815 void ext4_xattr_destroy_cache(struct mb_cache *cache)
1818 mb_cache_destroy(cache);