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 <linux/rwsem.h>
59 #include "ext4_jbd2.h"
64 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
65 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
66 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
67 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
69 #ifdef EXT4_XATTR_DEBUG
70 # define ea_idebug(inode, f...) do { \
71 printk(KERN_DEBUG "inode %s:%lu: ", \
72 inode->i_sb->s_id, inode->i_ino); \
76 # define ea_bdebug(bh, f...) do { \
77 char b[BDEVNAME_SIZE]; \
78 printk(KERN_DEBUG "block %s:%lu: ", \
79 bdevname(bh->b_bdev, b), \
80 (unsigned long) bh->b_blocknr); \
85 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
86 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
89 static void ext4_xattr_cache_insert(struct buffer_head *);
90 static struct buffer_head *ext4_xattr_cache_find(struct inode *,
91 struct ext4_xattr_header *,
92 struct mb_cache_entry **);
93 static void ext4_xattr_rehash(struct ext4_xattr_header *,
94 struct ext4_xattr_entry *);
95 static int ext4_xattr_list(struct dentry *dentry, char *buffer,
98 static struct mb_cache *ext4_xattr_cache;
100 static const struct xattr_handler *ext4_xattr_handler_map[] = {
101 [EXT4_XATTR_INDEX_USER] = &ext4_xattr_user_handler,
102 #ifdef CONFIG_EXT4_FS_POSIX_ACL
103 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext4_xattr_acl_access_handler,
104 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext4_xattr_acl_default_handler,
106 [EXT4_XATTR_INDEX_TRUSTED] = &ext4_xattr_trusted_handler,
107 #ifdef CONFIG_EXT4_FS_SECURITY
108 [EXT4_XATTR_INDEX_SECURITY] = &ext4_xattr_security_handler,
110 #ifdef CONFIG_EXT4_FS_RICHACL
111 [EXT4_XATTR_INDEX_RICHACL] = &ext4_richacl_xattr_handler,
115 const struct xattr_handler *ext4_xattr_handlers[] = {
116 &ext4_xattr_user_handler,
117 &ext4_xattr_trusted_handler,
118 #ifdef CONFIG_EXT4_FS_POSIX_ACL
119 &ext4_xattr_acl_access_handler,
120 &ext4_xattr_acl_default_handler,
122 #ifdef CONFIG_EXT4_FS_SECURITY
123 &ext4_xattr_security_handler,
125 #ifdef CONFIG_EXT4_FS_RICHACL
126 &ext4_richacl_xattr_handler,
131 static inline const struct xattr_handler *
132 ext4_xattr_handler(int name_index)
134 const struct xattr_handler *handler = NULL;
136 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
137 handler = ext4_xattr_handler_map[name_index];
142 * Inode operation listxattr()
144 * dentry->d_inode->i_mutex: don't care
147 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
149 return ext4_xattr_list(dentry, buffer, size);
153 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end)
155 while (!IS_LAST_ENTRY(entry)) {
156 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(entry);
157 if ((void *)next >= end)
165 ext4_xattr_check_block(struct buffer_head *bh)
167 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
168 BHDR(bh)->h_blocks != cpu_to_le32(1))
170 return ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
174 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
176 size_t value_size = le32_to_cpu(entry->e_value_size);
178 if (entry->e_value_block != 0 || value_size > size ||
179 le16_to_cpu(entry->e_value_offs) + value_size > size)
185 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
186 const char *name, size_t size, int sorted)
188 struct ext4_xattr_entry *entry;
194 name_len = strlen(name);
196 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
197 cmp = name_index - entry->e_name_index;
199 cmp = name_len - entry->e_name_len;
201 cmp = memcmp(name, entry->e_name, name_len);
202 if (cmp <= 0 && (sorted || cmp == 0))
206 if (!cmp && ext4_xattr_check_entry(entry, size))
208 return cmp ? -ENODATA : 0;
212 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
213 void *buffer, size_t buffer_size)
215 struct buffer_head *bh = NULL;
216 struct ext4_xattr_entry *entry;
220 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
221 name_index, name, buffer, (long)buffer_size);
224 if (!EXT4_I(inode)->i_file_acl)
226 ea_idebug(inode, "reading block %llu",
227 (unsigned long long)EXT4_I(inode)->i_file_acl);
228 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
231 ea_bdebug(bh, "b_count=%d, refcount=%d",
232 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
233 if (ext4_xattr_check_block(bh)) {
235 EXT4_ERROR_INODE(inode, "bad block %llu",
236 EXT4_I(inode)->i_file_acl);
240 ext4_xattr_cache_insert(bh);
242 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
247 size = le32_to_cpu(entry->e_value_size);
250 if (size > buffer_size)
252 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
263 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
264 void *buffer, size_t buffer_size)
266 struct ext4_xattr_ibody_header *header;
267 struct ext4_xattr_entry *entry;
268 struct ext4_inode *raw_inode;
269 struct ext4_iloc iloc;
274 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
276 error = ext4_get_inode_loc(inode, &iloc);
279 raw_inode = ext4_raw_inode(&iloc);
280 header = IHDR(inode, raw_inode);
281 entry = IFIRST(header);
282 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
283 error = ext4_xattr_check_names(entry, end);
286 error = ext4_xattr_find_entry(&entry, name_index, name,
287 end - (void *)entry, 0);
290 size = le32_to_cpu(entry->e_value_size);
293 if (size > buffer_size)
295 memcpy(buffer, (void *)IFIRST(header) +
296 le16_to_cpu(entry->e_value_offs), size);
308 * Copy an extended attribute into the buffer
309 * provided, or compute the buffer size required.
310 * Buffer is NULL to compute the size of the buffer required.
312 * Returns a negative error number on failure, or the number of bytes
313 * used / required on success.
316 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
317 void *buffer, size_t buffer_size)
321 down_read(&EXT4_I(inode)->xattr_sem);
322 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
324 if (error == -ENODATA)
325 error = ext4_xattr_block_get(inode, name_index, name, buffer,
327 up_read(&EXT4_I(inode)->xattr_sem);
332 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
333 char *buffer, size_t buffer_size)
335 size_t rest = buffer_size;
337 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
338 const struct xattr_handler *handler =
339 ext4_xattr_handler(entry->e_name_index);
342 size_t size = handler->list(dentry, buffer, rest,
354 return buffer_size - rest;
358 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
360 struct inode *inode = dentry->d_inode;
361 struct buffer_head *bh = NULL;
364 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
365 buffer, (long)buffer_size);
368 if (!EXT4_I(inode)->i_file_acl)
370 ea_idebug(inode, "reading block %llu",
371 (unsigned long long)EXT4_I(inode)->i_file_acl);
372 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
376 ea_bdebug(bh, "b_count=%d, refcount=%d",
377 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
378 if (ext4_xattr_check_block(bh)) {
379 EXT4_ERROR_INODE(inode, "bad block %llu",
380 EXT4_I(inode)->i_file_acl);
384 ext4_xattr_cache_insert(bh);
385 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
394 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
396 struct inode *inode = dentry->d_inode;
397 struct ext4_xattr_ibody_header *header;
398 struct ext4_inode *raw_inode;
399 struct ext4_iloc iloc;
403 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
405 error = ext4_get_inode_loc(inode, &iloc);
408 raw_inode = ext4_raw_inode(&iloc);
409 header = IHDR(inode, raw_inode);
410 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
411 error = ext4_xattr_check_names(IFIRST(header), end);
414 error = ext4_xattr_list_entries(dentry, IFIRST(header),
415 buffer, buffer_size);
425 * Copy a list of attribute names into the buffer
426 * provided, or compute the buffer size required.
427 * Buffer is NULL to compute the size of the buffer required.
429 * Returns a negative error number on failure, or the number of bytes
430 * used / required on success.
433 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
437 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
438 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
445 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
450 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
455 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
458 static void ext4_xattr_update_super_block(handle_t *handle,
459 struct super_block *sb)
461 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
464 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
465 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
466 ext4_handle_dirty_super(handle, sb);
471 * Release the xattr block BH: If the reference count is > 1, decrement
472 * it; otherwise free the block.
475 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
476 struct buffer_head *bh)
478 struct mb_cache_entry *ce = NULL;
481 ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
482 error = ext4_journal_get_write_access(handle, bh);
487 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
488 ea_bdebug(bh, "refcount now=0; freeing");
490 mb_cache_entry_free(ce);
492 ext4_free_blocks(handle, inode, bh, 0, 1,
493 EXT4_FREE_BLOCKS_METADATA |
494 EXT4_FREE_BLOCKS_FORGET);
497 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
499 mb_cache_entry_release(ce);
501 error = ext4_handle_dirty_metadata(handle, inode, bh);
503 ext4_handle_sync(handle);
504 dquot_free_block(inode, 1);
505 ea_bdebug(bh, "refcount now=%d; releasing",
506 le32_to_cpu(BHDR(bh)->h_refcount));
509 ext4_std_error(inode->i_sb, error);
514 * Find the available free space for EAs. This also returns the total number of
515 * bytes used by EA entries.
517 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
518 size_t *min_offs, void *base, int *total)
520 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
521 *total += EXT4_XATTR_LEN(last->e_name_len);
522 if (!last->e_value_block && last->e_value_size) {
523 size_t offs = le16_to_cpu(last->e_value_offs);
524 if (offs < *min_offs)
528 return (*min_offs - ((void *)last - base) - sizeof(__u32));
531 struct ext4_xattr_info {
538 struct ext4_xattr_search {
539 struct ext4_xattr_entry *first;
542 struct ext4_xattr_entry *here;
547 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
549 struct ext4_xattr_entry *last;
550 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
552 /* Compute min_offs and last. */
554 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
555 if (!last->e_value_block && last->e_value_size) {
556 size_t offs = le16_to_cpu(last->e_value_offs);
561 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
563 if (!s->here->e_value_block && s->here->e_value_size) {
564 size_t size = le32_to_cpu(s->here->e_value_size);
565 free += EXT4_XATTR_SIZE(size);
567 free += EXT4_XATTR_LEN(name_len);
570 if (free < EXT4_XATTR_SIZE(i->value_len) ||
571 free < EXT4_XATTR_LEN(name_len) +
572 EXT4_XATTR_SIZE(i->value_len))
576 if (i->value && s->not_found) {
577 /* Insert the new name. */
578 size_t size = EXT4_XATTR_LEN(name_len);
579 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
580 memmove((void *)s->here + size, s->here, rest);
581 memset(s->here, 0, size);
582 s->here->e_name_index = i->name_index;
583 s->here->e_name_len = name_len;
584 memcpy(s->here->e_name, i->name, name_len);
586 if (!s->here->e_value_block && s->here->e_value_size) {
587 void *first_val = s->base + min_offs;
588 size_t offs = le16_to_cpu(s->here->e_value_offs);
589 void *val = s->base + offs;
590 size_t size = EXT4_XATTR_SIZE(
591 le32_to_cpu(s->here->e_value_size));
593 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
594 /* The old and the new value have the same
595 size. Just replace. */
596 s->here->e_value_size =
597 cpu_to_le32(i->value_len);
598 memset(val + size - EXT4_XATTR_PAD, 0,
599 EXT4_XATTR_PAD); /* Clear pad bytes. */
600 memcpy(val, i->value, i->value_len);
604 /* Remove the old value. */
605 memmove(first_val + size, first_val, val - first_val);
606 memset(first_val, 0, size);
607 s->here->e_value_size = 0;
608 s->here->e_value_offs = 0;
611 /* Adjust all value offsets. */
613 while (!IS_LAST_ENTRY(last)) {
614 size_t o = le16_to_cpu(last->e_value_offs);
615 if (!last->e_value_block &&
616 last->e_value_size && o < offs)
618 cpu_to_le16(o + size);
619 last = EXT4_XATTR_NEXT(last);
623 /* Remove the old name. */
624 size_t size = EXT4_XATTR_LEN(name_len);
625 last = ENTRY((void *)last - size);
626 memmove(s->here, (void *)s->here + size,
627 (void *)last - (void *)s->here + sizeof(__u32));
628 memset(last, 0, size);
633 /* Insert the new value. */
634 s->here->e_value_size = cpu_to_le32(i->value_len);
636 size_t size = EXT4_XATTR_SIZE(i->value_len);
637 void *val = s->base + min_offs - size;
638 s->here->e_value_offs = cpu_to_le16(min_offs - size);
639 memset(val + size - EXT4_XATTR_PAD, 0,
640 EXT4_XATTR_PAD); /* Clear the pad bytes. */
641 memcpy(val, i->value, i->value_len);
647 struct ext4_xattr_block_find {
648 struct ext4_xattr_search s;
649 struct buffer_head *bh;
653 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
654 struct ext4_xattr_block_find *bs)
656 struct super_block *sb = inode->i_sb;
659 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
660 i->name_index, i->name, i->value, (long)i->value_len);
662 if (EXT4_I(inode)->i_file_acl) {
663 /* The inode already has an extended attribute block. */
664 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
668 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
669 atomic_read(&(bs->bh->b_count)),
670 le32_to_cpu(BHDR(bs->bh)->h_refcount));
671 if (ext4_xattr_check_block(bs->bh)) {
672 EXT4_ERROR_INODE(inode, "bad block %llu",
673 EXT4_I(inode)->i_file_acl);
677 /* Find the named attribute. */
678 bs->s.base = BHDR(bs->bh);
679 bs->s.first = BFIRST(bs->bh);
680 bs->s.end = bs->bh->b_data + bs->bh->b_size;
681 bs->s.here = bs->s.first;
682 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
683 i->name, bs->bh->b_size, 1);
684 if (error && error != -ENODATA)
686 bs->s.not_found = error;
695 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
696 struct ext4_xattr_info *i,
697 struct ext4_xattr_block_find *bs)
699 struct super_block *sb = inode->i_sb;
700 struct buffer_head *new_bh = NULL;
701 struct ext4_xattr_search *s = &bs->s;
702 struct mb_cache_entry *ce = NULL;
705 #define header(x) ((struct ext4_xattr_header *)(x))
707 if (i->value && i->value_len > sb->s_blocksize)
710 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
712 error = ext4_journal_get_write_access(handle, bs->bh);
717 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
719 mb_cache_entry_free(ce);
722 ea_bdebug(bs->bh, "modifying in-place");
723 error = ext4_xattr_set_entry(i, s);
725 if (!IS_LAST_ENTRY(s->first))
726 ext4_xattr_rehash(header(s->base),
728 ext4_xattr_cache_insert(bs->bh);
730 unlock_buffer(bs->bh);
734 error = ext4_handle_dirty_metadata(handle,
741 int offset = (char *)s->here - bs->bh->b_data;
743 unlock_buffer(bs->bh);
744 ext4_handle_release_buffer(handle, bs->bh);
746 mb_cache_entry_release(ce);
749 ea_bdebug(bs->bh, "cloning");
750 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
754 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
755 s->first = ENTRY(header(s->base)+1);
756 header(s->base)->h_refcount = cpu_to_le32(1);
757 s->here = ENTRY(s->base + offset);
758 s->end = s->base + bs->bh->b_size;
761 /* Allocate a buffer where we construct the new block. */
762 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
763 /* assert(header == s->base) */
767 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
768 header(s->base)->h_blocks = cpu_to_le32(1);
769 header(s->base)->h_refcount = cpu_to_le32(1);
770 s->first = ENTRY(header(s->base)+1);
771 s->here = ENTRY(header(s->base)+1);
772 s->end = s->base + sb->s_blocksize;
775 error = ext4_xattr_set_entry(i, s);
780 if (!IS_LAST_ENTRY(s->first))
781 ext4_xattr_rehash(header(s->base), s->here);
784 if (!IS_LAST_ENTRY(s->first)) {
785 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
787 /* We found an identical block in the cache. */
788 if (new_bh == bs->bh)
789 ea_bdebug(new_bh, "keeping");
791 /* The old block is released after updating
793 error = dquot_alloc_block(inode, 1);
796 error = ext4_journal_get_write_access(handle,
801 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
802 ea_bdebug(new_bh, "reusing; refcount now=%d",
803 le32_to_cpu(BHDR(new_bh)->h_refcount));
804 unlock_buffer(new_bh);
805 error = ext4_handle_dirty_metadata(handle,
811 mb_cache_entry_release(ce);
813 } else if (bs->bh && s->base == bs->bh->b_data) {
814 /* We were modifying this block in-place. */
815 ea_bdebug(bs->bh, "keeping this block");
819 /* We need to allocate a new block */
820 ext4_fsblk_t goal, block;
822 goal = ext4_group_first_block_no(sb,
823 EXT4_I(inode)->i_block_group);
825 /* non-extent files can't have physical blocks past 2^32 */
826 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
827 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
830 * take i_data_sem because we will test
831 * i_delalloc_reserved_flag in ext4_mb_new_blocks
833 down_read((&EXT4_I(inode)->i_data_sem));
834 block = ext4_new_meta_blocks(handle, inode, goal, 0,
836 up_read((&EXT4_I(inode)->i_data_sem));
840 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
841 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
843 ea_idebug(inode, "creating block %llu",
844 (unsigned long long)block);
846 new_bh = sb_getblk(sb, block);
849 ext4_free_blocks(handle, inode, NULL, block, 1,
850 EXT4_FREE_BLOCKS_METADATA);
855 error = ext4_journal_get_create_access(handle, new_bh);
857 unlock_buffer(new_bh);
860 memcpy(new_bh->b_data, s->base, new_bh->b_size);
861 set_buffer_uptodate(new_bh);
862 unlock_buffer(new_bh);
863 ext4_xattr_cache_insert(new_bh);
864 error = ext4_handle_dirty_metadata(handle,
871 /* Update the inode. */
872 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
874 /* Drop the previous xattr block. */
875 if (bs->bh && bs->bh != new_bh)
876 ext4_xattr_release_block(handle, inode, bs->bh);
881 mb_cache_entry_release(ce);
883 if (!(bs->bh && s->base == bs->bh->b_data))
889 dquot_free_block(inode, 1);
893 EXT4_ERROR_INODE(inode, "bad block %llu",
894 EXT4_I(inode)->i_file_acl);
900 struct ext4_xattr_ibody_find {
901 struct ext4_xattr_search s;
902 struct ext4_iloc iloc;
906 ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
907 struct ext4_xattr_ibody_find *is)
909 struct ext4_xattr_ibody_header *header;
910 struct ext4_inode *raw_inode;
913 if (EXT4_I(inode)->i_extra_isize == 0)
915 raw_inode = ext4_raw_inode(&is->iloc);
916 header = IHDR(inode, raw_inode);
917 is->s.base = is->s.first = IFIRST(header);
918 is->s.here = is->s.first;
919 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
920 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
921 error = ext4_xattr_check_names(IFIRST(header), is->s.end);
924 /* Find the named attribute. */
925 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
927 (void *)is->s.base, 0);
928 if (error && error != -ENODATA)
930 is->s.not_found = error;
936 ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
937 struct ext4_xattr_info *i,
938 struct ext4_xattr_ibody_find *is)
940 struct ext4_xattr_ibody_header *header;
941 struct ext4_xattr_search *s = &is->s;
944 if (EXT4_I(inode)->i_extra_isize == 0)
946 error = ext4_xattr_set_entry(i, s);
949 header = IHDR(inode, ext4_raw_inode(&is->iloc));
950 if (!IS_LAST_ENTRY(s->first)) {
951 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
952 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
954 header->h_magic = cpu_to_le32(0);
955 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
961 * ext4_xattr_set_handle()
963 * Create, replace or remove an extended attribute for this inode. Value
964 * is NULL to remove an existing extended attribute, and non-NULL to
965 * either replace an existing extended attribute, or create a new extended
966 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
967 * specify that an extended attribute must exist and must not exist
968 * previous to the call, respectively.
970 * Returns 0, or a negative error number on failure.
973 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
974 const char *name, const void *value, size_t value_len,
977 struct ext4_xattr_info i = {
978 .name_index = name_index,
981 .value_len = value_len,
984 struct ext4_xattr_ibody_find is = {
985 .s = { .not_found = -ENODATA, },
987 struct ext4_xattr_block_find bs = {
988 .s = { .not_found = -ENODATA, },
990 unsigned long no_expand;
995 if (strlen(name) > 255)
997 down_write(&EXT4_I(inode)->xattr_sem);
998 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
999 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1001 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1005 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1006 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1007 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1008 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1011 error = ext4_xattr_ibody_find(inode, &i, &is);
1015 error = ext4_xattr_block_find(inode, &i, &bs);
1018 if (is.s.not_found && bs.s.not_found) {
1020 if (flags & XATTR_REPLACE)
1027 if (flags & XATTR_CREATE)
1031 if (!is.s.not_found)
1032 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1033 else if (!bs.s.not_found)
1034 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1036 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1037 if (!error && !bs.s.not_found) {
1039 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1040 } else if (error == -ENOSPC) {
1041 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1042 error = ext4_xattr_block_find(inode, &i, &bs);
1046 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1049 if (!is.s.not_found) {
1051 error = ext4_xattr_ibody_set(handle, inode, &i,
1057 ext4_xattr_update_super_block(handle, inode->i_sb);
1058 inode->i_ctime = ext4_current_time(inode);
1060 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1061 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1063 * The bh is consumed by ext4_mark_iloc_dirty, even with
1068 ext4_handle_sync(handle);
1075 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1076 up_write(&EXT4_I(inode)->xattr_sem);
1083 * Like ext4_xattr_set_handle, but start from an inode. This extended
1084 * attribute modification is a filesystem transaction by itself.
1086 * Returns 0, or a negative error number on failure.
1089 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1090 const void *value, size_t value_len, int flags)
1093 int error, retries = 0;
1096 handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
1097 if (IS_ERR(handle)) {
1098 error = PTR_ERR(handle);
1102 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1103 value, value_len, flags);
1104 error2 = ext4_journal_stop(handle);
1105 if (error == -ENOSPC &&
1106 ext4_should_retry_alloc(inode->i_sb, &retries))
1116 * Shift the EA entries in the inode to create space for the increased
1119 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1120 int value_offs_shift, void *to,
1121 void *from, size_t n, int blocksize)
1123 struct ext4_xattr_entry *last = entry;
1126 /* Adjust the value offsets of the entries */
1127 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1128 if (!last->e_value_block && last->e_value_size) {
1129 new_offs = le16_to_cpu(last->e_value_offs) +
1131 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1133 last->e_value_offs = cpu_to_le16(new_offs);
1136 /* Shift the entries by n bytes */
1137 memmove(to, from, n);
1141 * Expand an inode by new_extra_isize bytes when EAs are present.
1142 * Returns 0 on success or negative error number on failure.
1144 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1145 struct ext4_inode *raw_inode, handle_t *handle)
1147 struct ext4_xattr_ibody_header *header;
1148 struct ext4_xattr_entry *entry, *last, *first;
1149 struct buffer_head *bh = NULL;
1150 struct ext4_xattr_ibody_find *is = NULL;
1151 struct ext4_xattr_block_find *bs = NULL;
1152 char *buffer = NULL, *b_entry_name = NULL;
1153 size_t min_offs, free;
1154 int total_ino, total_blk;
1155 void *base, *start, *end;
1156 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1157 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1159 down_write(&EXT4_I(inode)->xattr_sem);
1161 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1162 up_write(&EXT4_I(inode)->xattr_sem);
1166 header = IHDR(inode, raw_inode);
1167 entry = IFIRST(header);
1170 * Check if enough free space is available in the inode to shift the
1171 * entries ahead by new_extra_isize.
1174 base = start = entry;
1175 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1176 min_offs = end - base;
1178 total_ino = sizeof(struct ext4_xattr_ibody_header);
1180 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1181 if (free >= new_extra_isize) {
1182 entry = IFIRST(header);
1183 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1184 - new_extra_isize, (void *)raw_inode +
1185 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1186 (void *)header, total_ino,
1187 inode->i_sb->s_blocksize);
1188 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1194 * Enough free space isn't available in the inode, check if
1195 * EA block can hold new_extra_isize bytes.
1197 if (EXT4_I(inode)->i_file_acl) {
1198 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1202 if (ext4_xattr_check_block(bh)) {
1203 EXT4_ERROR_INODE(inode, "bad block %llu",
1204 EXT4_I(inode)->i_file_acl);
1210 end = bh->b_data + bh->b_size;
1211 min_offs = end - base;
1212 free = ext4_xattr_free_space(first, &min_offs, base,
1214 if (free < new_extra_isize) {
1215 if (!tried_min_extra_isize && s_min_extra_isize) {
1216 tried_min_extra_isize++;
1217 new_extra_isize = s_min_extra_isize;
1225 free = inode->i_sb->s_blocksize;
1228 while (new_extra_isize > 0) {
1229 size_t offs, size, entry_size;
1230 struct ext4_xattr_entry *small_entry = NULL;
1231 struct ext4_xattr_info i = {
1235 unsigned int total_size; /* EA entry size + value size */
1236 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1237 unsigned int min_total_size = ~0U;
1239 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1240 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1246 is->s.not_found = -ENODATA;
1247 bs->s.not_found = -ENODATA;
1251 last = IFIRST(header);
1252 /* Find the entry best suited to be pushed into EA block */
1254 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1256 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1257 EXT4_XATTR_LEN(last->e_name_len);
1258 if (total_size <= free && total_size < min_total_size) {
1259 if (total_size < new_extra_isize) {
1263 min_total_size = total_size;
1268 if (entry == NULL) {
1270 entry = small_entry;
1272 if (!tried_min_extra_isize &&
1273 s_min_extra_isize) {
1274 tried_min_extra_isize++;
1275 new_extra_isize = s_min_extra_isize;
1282 offs = le16_to_cpu(entry->e_value_offs);
1283 size = le32_to_cpu(entry->e_value_size);
1284 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1285 i.name_index = entry->e_name_index,
1286 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1287 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1288 if (!buffer || !b_entry_name) {
1292 /* Save the entry name and the entry value */
1293 memcpy(buffer, (void *)IFIRST(header) + offs,
1294 EXT4_XATTR_SIZE(size));
1295 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1296 b_entry_name[entry->e_name_len] = '\0';
1297 i.name = b_entry_name;
1299 error = ext4_get_inode_loc(inode, &is->iloc);
1303 error = ext4_xattr_ibody_find(inode, &i, is);
1307 /* Remove the chosen entry from the inode */
1308 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1312 entry = IFIRST(header);
1313 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1314 shift_bytes = new_extra_isize;
1316 shift_bytes = entry_size + size;
1317 /* Adjust the offsets and shift the remaining entries ahead */
1318 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1319 shift_bytes, (void *)raw_inode +
1320 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1321 (void *)header, total_ino - entry_size,
1322 inode->i_sb->s_blocksize);
1324 extra_isize += shift_bytes;
1325 new_extra_isize -= shift_bytes;
1326 EXT4_I(inode)->i_extra_isize = extra_isize;
1328 i.name = b_entry_name;
1331 error = ext4_xattr_block_find(inode, &i, bs);
1335 /* Add entry which was removed from the inode into the block */
1336 error = ext4_xattr_block_set(handle, inode, &i, bs);
1339 kfree(b_entry_name);
1341 b_entry_name = NULL;
1343 brelse(is->iloc.bh);
1348 up_write(&EXT4_I(inode)->xattr_sem);
1352 kfree(b_entry_name);
1355 brelse(is->iloc.bh);
1359 up_write(&EXT4_I(inode)->xattr_sem);
1366 * ext4_xattr_delete_inode()
1368 * Free extended attribute resources associated with this inode. This
1369 * is called immediately before an inode is freed. We have exclusive
1370 * access to the inode.
1373 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1375 struct buffer_head *bh = NULL;
1377 if (!EXT4_I(inode)->i_file_acl)
1379 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1381 EXT4_ERROR_INODE(inode, "block %llu read error",
1382 EXT4_I(inode)->i_file_acl);
1385 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1386 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1387 EXT4_ERROR_INODE(inode, "bad block %llu",
1388 EXT4_I(inode)->i_file_acl);
1391 ext4_xattr_release_block(handle, inode, bh);
1392 EXT4_I(inode)->i_file_acl = 0;
1399 * ext4_xattr_put_super()
1401 * This is called when a file system is unmounted.
1404 ext4_xattr_put_super(struct super_block *sb)
1406 mb_cache_shrink(sb->s_bdev);
1410 * ext4_xattr_cache_insert()
1412 * Create a new entry in the extended attribute cache, and insert
1413 * it unless such an entry is already in the cache.
1415 * Returns 0, or a negative error number on failure.
1418 ext4_xattr_cache_insert(struct buffer_head *bh)
1420 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1421 struct mb_cache_entry *ce;
1424 ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
1426 ea_bdebug(bh, "out of memory");
1429 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1431 mb_cache_entry_free(ce);
1432 if (error == -EBUSY) {
1433 ea_bdebug(bh, "already in cache");
1437 ea_bdebug(bh, "inserting [%x]", (int)hash);
1438 mb_cache_entry_release(ce);
1445 * Compare two extended attribute blocks for equality.
1447 * Returns 0 if the blocks are equal, 1 if they differ, and
1448 * a negative error number on errors.
1451 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1452 struct ext4_xattr_header *header2)
1454 struct ext4_xattr_entry *entry1, *entry2;
1456 entry1 = ENTRY(header1+1);
1457 entry2 = ENTRY(header2+1);
1458 while (!IS_LAST_ENTRY(entry1)) {
1459 if (IS_LAST_ENTRY(entry2))
1461 if (entry1->e_hash != entry2->e_hash ||
1462 entry1->e_name_index != entry2->e_name_index ||
1463 entry1->e_name_len != entry2->e_name_len ||
1464 entry1->e_value_size != entry2->e_value_size ||
1465 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1467 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1469 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1470 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1471 le32_to_cpu(entry1->e_value_size)))
1474 entry1 = EXT4_XATTR_NEXT(entry1);
1475 entry2 = EXT4_XATTR_NEXT(entry2);
1477 if (!IS_LAST_ENTRY(entry2))
1483 * ext4_xattr_cache_find()
1485 * Find an identical extended attribute block.
1487 * Returns a pointer to the block found, or NULL if such a block was
1488 * not found or an error occurred.
1490 static struct buffer_head *
1491 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1492 struct mb_cache_entry **pce)
1494 __u32 hash = le32_to_cpu(header->h_hash);
1495 struct mb_cache_entry *ce;
1497 if (!header->h_hash)
1498 return NULL; /* never share */
1499 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1501 ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1504 struct buffer_head *bh;
1507 if (PTR_ERR(ce) == -EAGAIN)
1511 bh = sb_bread(inode->i_sb, ce->e_block);
1513 EXT4_ERROR_INODE(inode, "block %lu read error",
1514 (unsigned long) ce->e_block);
1515 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1516 EXT4_XATTR_REFCOUNT_MAX) {
1517 ea_idebug(inode, "block %lu refcount %d>=%d",
1518 (unsigned long) ce->e_block,
1519 le32_to_cpu(BHDR(bh)->h_refcount),
1520 EXT4_XATTR_REFCOUNT_MAX);
1521 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1526 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1531 #define NAME_HASH_SHIFT 5
1532 #define VALUE_HASH_SHIFT 16
1535 * ext4_xattr_hash_entry()
1537 * Compute the hash of an extended attribute.
1539 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1540 struct ext4_xattr_entry *entry)
1543 char *name = entry->e_name;
1546 for (n = 0; n < entry->e_name_len; n++) {
1547 hash = (hash << NAME_HASH_SHIFT) ^
1548 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1552 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1553 __le32 *value = (__le32 *)((char *)header +
1554 le16_to_cpu(entry->e_value_offs));
1555 for (n = (le32_to_cpu(entry->e_value_size) +
1556 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1557 hash = (hash << VALUE_HASH_SHIFT) ^
1558 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1559 le32_to_cpu(*value++);
1562 entry->e_hash = cpu_to_le32(hash);
1565 #undef NAME_HASH_SHIFT
1566 #undef VALUE_HASH_SHIFT
1568 #define BLOCK_HASH_SHIFT 16
1571 * ext4_xattr_rehash()
1573 * Re-compute the extended attribute hash value after an entry has changed.
1575 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1576 struct ext4_xattr_entry *entry)
1578 struct ext4_xattr_entry *here;
1581 ext4_xattr_hash_entry(header, entry);
1582 here = ENTRY(header+1);
1583 while (!IS_LAST_ENTRY(here)) {
1584 if (!here->e_hash) {
1585 /* Block is not shared if an entry's hash value == 0 */
1589 hash = (hash << BLOCK_HASH_SHIFT) ^
1590 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1591 le32_to_cpu(here->e_hash);
1592 here = EXT4_XATTR_NEXT(here);
1594 header->h_hash = cpu_to_le32(hash);
1597 #undef BLOCK_HASH_SHIFT
1600 ext4_init_xattr(void)
1602 ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
1603 if (!ext4_xattr_cache)
1609 ext4_exit_xattr(void)
1611 if (ext4_xattr_cache)
1612 mb_cache_destroy(ext4_xattr_cache);
1613 ext4_xattr_cache = NULL;