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(f...)
86 # define ea_bdebug(f...)
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,
112 const struct xattr_handler *ext4_xattr_handlers[] = {
113 &ext4_xattr_user_handler,
114 &ext4_xattr_trusted_handler,
115 #ifdef CONFIG_EXT4_FS_POSIX_ACL
116 &ext4_xattr_acl_access_handler,
117 &ext4_xattr_acl_default_handler,
119 #ifdef CONFIG_EXT4_FS_SECURITY
120 &ext4_xattr_security_handler,
125 static inline const struct xattr_handler *
126 ext4_xattr_handler(int name_index)
128 const struct xattr_handler *handler = NULL;
130 if (name_index > 0 && name_index < ARRAY_SIZE(ext4_xattr_handler_map))
131 handler = ext4_xattr_handler_map[name_index];
136 * Inode operation listxattr()
138 * dentry->d_inode->i_mutex: don't care
141 ext4_listxattr(struct dentry *dentry, char *buffer, size_t size)
143 return ext4_xattr_list(dentry, buffer, size);
147 ext4_xattr_check_names(struct ext4_xattr_entry *entry, void *end,
150 struct ext4_xattr_entry *e = entry;
152 while (!IS_LAST_ENTRY(e)) {
153 struct ext4_xattr_entry *next = EXT4_XATTR_NEXT(e);
154 if ((void *)next >= end)
159 while (!IS_LAST_ENTRY(entry)) {
160 if (entry->e_value_size != 0 &&
161 (value_start + le16_to_cpu(entry->e_value_offs) <
162 (void *)e + sizeof(__u32) ||
163 value_start + le16_to_cpu(entry->e_value_offs) +
164 le32_to_cpu(entry->e_value_size) > end))
166 entry = EXT4_XATTR_NEXT(entry);
173 ext4_xattr_check_block(struct buffer_head *bh)
177 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
178 BHDR(bh)->h_blocks != cpu_to_le32(1))
180 error = ext4_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size,
186 ext4_xattr_check_entry(struct ext4_xattr_entry *entry, size_t size)
188 size_t value_size = le32_to_cpu(entry->e_value_size);
190 if (entry->e_value_block != 0 || value_size > size ||
191 le16_to_cpu(entry->e_value_offs) + value_size > size)
197 ext4_xattr_find_entry(struct ext4_xattr_entry **pentry, int name_index,
198 const char *name, size_t size, int sorted)
200 struct ext4_xattr_entry *entry;
206 name_len = strlen(name);
208 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
209 cmp = name_index - entry->e_name_index;
211 cmp = name_len - entry->e_name_len;
213 cmp = memcmp(name, entry->e_name, name_len);
214 if (cmp <= 0 && (sorted || cmp == 0))
218 if (!cmp && ext4_xattr_check_entry(entry, size))
220 return cmp ? -ENODATA : 0;
224 ext4_xattr_block_get(struct inode *inode, int name_index, const char *name,
225 void *buffer, size_t buffer_size)
227 struct buffer_head *bh = NULL;
228 struct ext4_xattr_entry *entry;
232 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
233 name_index, name, buffer, (long)buffer_size);
236 if (!EXT4_I(inode)->i_file_acl)
238 ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
239 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
242 ea_bdebug(bh, "b_count=%d, refcount=%d",
243 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
244 if (ext4_xattr_check_block(bh)) {
246 EXT4_ERROR_INODE(inode, "bad block %llu",
247 EXT4_I(inode)->i_file_acl);
251 ext4_xattr_cache_insert(bh);
253 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
258 size = le32_to_cpu(entry->e_value_size);
261 if (size > buffer_size)
263 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
274 ext4_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
275 void *buffer, size_t buffer_size)
277 struct ext4_xattr_ibody_header *header;
278 struct ext4_xattr_entry *entry;
279 struct ext4_inode *raw_inode;
280 struct ext4_iloc iloc;
285 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
287 error = ext4_get_inode_loc(inode, &iloc);
290 raw_inode = ext4_raw_inode(&iloc);
291 header = IHDR(inode, raw_inode);
292 entry = IFIRST(header);
293 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
294 error = ext4_xattr_check_names(entry, end, entry);
297 error = ext4_xattr_find_entry(&entry, name_index, name,
298 end - (void *)entry, 0);
301 size = le32_to_cpu(entry->e_value_size);
304 if (size > buffer_size)
306 memcpy(buffer, (void *)IFIRST(header) +
307 le16_to_cpu(entry->e_value_offs), size);
319 * Copy an extended attribute into the buffer
320 * provided, or compute the buffer size required.
321 * Buffer is NULL to compute the size of the buffer required.
323 * Returns a negative error number on failure, or the number of bytes
324 * used / required on success.
327 ext4_xattr_get(struct inode *inode, int name_index, const char *name,
328 void *buffer, size_t buffer_size)
332 down_read(&EXT4_I(inode)->xattr_sem);
333 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
335 if (error == -ENODATA)
336 error = ext4_xattr_block_get(inode, name_index, name, buffer,
338 up_read(&EXT4_I(inode)->xattr_sem);
343 ext4_xattr_list_entries(struct dentry *dentry, struct ext4_xattr_entry *entry,
344 char *buffer, size_t buffer_size)
346 size_t rest = buffer_size;
348 for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) {
349 const struct xattr_handler *handler =
350 ext4_xattr_handler(entry->e_name_index);
353 size_t size = handler->list(dentry, buffer, rest,
365 return buffer_size - rest;
369 ext4_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
371 struct inode *inode = dentry->d_inode;
372 struct buffer_head *bh = NULL;
375 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
376 buffer, (long)buffer_size);
379 if (!EXT4_I(inode)->i_file_acl)
381 ea_idebug(inode, "reading block %u", EXT4_I(inode)->i_file_acl);
382 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
386 ea_bdebug(bh, "b_count=%d, refcount=%d",
387 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
388 if (ext4_xattr_check_block(bh)) {
389 EXT4_ERROR_INODE(inode, "bad block %llu",
390 EXT4_I(inode)->i_file_acl);
394 ext4_xattr_cache_insert(bh);
395 error = ext4_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
404 ext4_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
406 struct inode *inode = dentry->d_inode;
407 struct ext4_xattr_ibody_header *header;
408 struct ext4_inode *raw_inode;
409 struct ext4_iloc iloc;
413 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
415 error = ext4_get_inode_loc(inode, &iloc);
418 raw_inode = ext4_raw_inode(&iloc);
419 header = IHDR(inode, raw_inode);
420 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
421 error = ext4_xattr_check_names(IFIRST(header), end, IFIRST(header));
424 error = ext4_xattr_list_entries(dentry, IFIRST(header),
425 buffer, buffer_size);
435 * Copy a list of attribute names into the buffer
436 * provided, or compute the buffer size required.
437 * Buffer is NULL to compute the size of the buffer required.
439 * Returns a negative error number on failure, or the number of bytes
440 * used / required on success.
443 ext4_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
447 down_read(&EXT4_I(dentry->d_inode)->xattr_sem);
448 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
455 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
460 up_read(&EXT4_I(dentry->d_inode)->xattr_sem);
465 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
468 static void ext4_xattr_update_super_block(handle_t *handle,
469 struct super_block *sb)
471 if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR))
474 if (ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh) == 0) {
475 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_EXT_ATTR);
476 ext4_handle_dirty_super(handle, sb);
481 * Release the xattr block BH: If the reference count is > 1, decrement
482 * it; otherwise free the block.
485 ext4_xattr_release_block(handle_t *handle, struct inode *inode,
486 struct buffer_head *bh)
488 struct mb_cache_entry *ce = NULL;
491 ce = mb_cache_entry_get(ext4_xattr_cache, bh->b_bdev, bh->b_blocknr);
492 error = ext4_journal_get_write_access(handle, bh);
497 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
498 ea_bdebug(bh, "refcount now=0; freeing");
500 mb_cache_entry_free(ce);
502 ext4_free_blocks(handle, inode, bh, 0, 1,
503 EXT4_FREE_BLOCKS_METADATA |
504 EXT4_FREE_BLOCKS_FORGET);
507 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
509 mb_cache_entry_release(ce);
511 error = ext4_handle_dirty_metadata(handle, inode, bh);
513 ext4_handle_sync(handle);
514 dquot_free_block(inode, EXT4_C2B(EXT4_SB(inode->i_sb), 1));
515 ea_bdebug(bh, "refcount now=%d; releasing",
516 le32_to_cpu(BHDR(bh)->h_refcount));
519 ext4_std_error(inode->i_sb, error);
524 * Find the available free space for EAs. This also returns the total number of
525 * bytes used by EA entries.
527 static size_t ext4_xattr_free_space(struct ext4_xattr_entry *last,
528 size_t *min_offs, void *base, int *total)
530 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
531 *total += EXT4_XATTR_LEN(last->e_name_len);
532 if (!last->e_value_block && last->e_value_size) {
533 size_t offs = le16_to_cpu(last->e_value_offs);
534 if (offs < *min_offs)
538 return (*min_offs - ((void *)last - base) - sizeof(__u32));
541 struct ext4_xattr_info {
548 struct ext4_xattr_search {
549 struct ext4_xattr_entry *first;
552 struct ext4_xattr_entry *here;
557 ext4_xattr_set_entry(struct ext4_xattr_info *i, struct ext4_xattr_search *s)
559 struct ext4_xattr_entry *last;
560 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
562 /* Compute min_offs and last. */
564 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
565 if (!last->e_value_block && last->e_value_size) {
566 size_t offs = le16_to_cpu(last->e_value_offs);
571 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
573 if (!s->here->e_value_block && s->here->e_value_size) {
574 size_t size = le32_to_cpu(s->here->e_value_size);
575 free += EXT4_XATTR_SIZE(size);
577 free += EXT4_XATTR_LEN(name_len);
580 if (free < EXT4_XATTR_SIZE(i->value_len) ||
581 free < EXT4_XATTR_LEN(name_len) +
582 EXT4_XATTR_SIZE(i->value_len))
586 if (i->value && s->not_found) {
587 /* Insert the new name. */
588 size_t size = EXT4_XATTR_LEN(name_len);
589 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
590 memmove((void *)s->here + size, s->here, rest);
591 memset(s->here, 0, size);
592 s->here->e_name_index = i->name_index;
593 s->here->e_name_len = name_len;
594 memcpy(s->here->e_name, i->name, name_len);
596 if (!s->here->e_value_block && s->here->e_value_size) {
597 void *first_val = s->base + min_offs;
598 size_t offs = le16_to_cpu(s->here->e_value_offs);
599 void *val = s->base + offs;
600 size_t size = EXT4_XATTR_SIZE(
601 le32_to_cpu(s->here->e_value_size));
603 if (i->value && size == EXT4_XATTR_SIZE(i->value_len)) {
604 /* The old and the new value have the same
605 size. Just replace. */
606 s->here->e_value_size =
607 cpu_to_le32(i->value_len);
608 memset(val + size - EXT4_XATTR_PAD, 0,
609 EXT4_XATTR_PAD); /* Clear pad bytes. */
610 memcpy(val, i->value, i->value_len);
614 /* Remove the old value. */
615 memmove(first_val + size, first_val, val - first_val);
616 memset(first_val, 0, size);
617 s->here->e_value_size = 0;
618 s->here->e_value_offs = 0;
621 /* Adjust all value offsets. */
623 while (!IS_LAST_ENTRY(last)) {
624 size_t o = le16_to_cpu(last->e_value_offs);
625 if (!last->e_value_block &&
626 last->e_value_size && o < offs)
628 cpu_to_le16(o + size);
629 last = EXT4_XATTR_NEXT(last);
633 /* Remove the old name. */
634 size_t size = EXT4_XATTR_LEN(name_len);
635 last = ENTRY((void *)last - size);
636 memmove(s->here, (void *)s->here + size,
637 (void *)last - (void *)s->here + sizeof(__u32));
638 memset(last, 0, size);
643 /* Insert the new value. */
644 s->here->e_value_size = cpu_to_le32(i->value_len);
646 size_t size = EXT4_XATTR_SIZE(i->value_len);
647 void *val = s->base + min_offs - size;
648 s->here->e_value_offs = cpu_to_le16(min_offs - size);
649 memset(val + size - EXT4_XATTR_PAD, 0,
650 EXT4_XATTR_PAD); /* Clear the pad bytes. */
651 memcpy(val, i->value, i->value_len);
657 struct ext4_xattr_block_find {
658 struct ext4_xattr_search s;
659 struct buffer_head *bh;
663 ext4_xattr_block_find(struct inode *inode, struct ext4_xattr_info *i,
664 struct ext4_xattr_block_find *bs)
666 struct super_block *sb = inode->i_sb;
669 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
670 i->name_index, i->name, i->value, (long)i->value_len);
672 if (EXT4_I(inode)->i_file_acl) {
673 /* The inode already has an extended attribute block. */
674 bs->bh = sb_bread(sb, EXT4_I(inode)->i_file_acl);
678 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
679 atomic_read(&(bs->bh->b_count)),
680 le32_to_cpu(BHDR(bs->bh)->h_refcount));
681 if (ext4_xattr_check_block(bs->bh)) {
682 EXT4_ERROR_INODE(inode, "bad block %llu",
683 EXT4_I(inode)->i_file_acl);
687 /* Find the named attribute. */
688 bs->s.base = BHDR(bs->bh);
689 bs->s.first = BFIRST(bs->bh);
690 bs->s.end = bs->bh->b_data + bs->bh->b_size;
691 bs->s.here = bs->s.first;
692 error = ext4_xattr_find_entry(&bs->s.here, i->name_index,
693 i->name, bs->bh->b_size, 1);
694 if (error && error != -ENODATA)
696 bs->s.not_found = error;
705 ext4_xattr_block_set(handle_t *handle, struct inode *inode,
706 struct ext4_xattr_info *i,
707 struct ext4_xattr_block_find *bs)
709 struct super_block *sb = inode->i_sb;
710 struct buffer_head *new_bh = NULL;
711 struct ext4_xattr_search *s = &bs->s;
712 struct mb_cache_entry *ce = NULL;
715 #define header(x) ((struct ext4_xattr_header *)(x))
717 if (i->value && i->value_len > sb->s_blocksize)
720 ce = mb_cache_entry_get(ext4_xattr_cache, bs->bh->b_bdev,
722 error = ext4_journal_get_write_access(handle, bs->bh);
727 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
729 mb_cache_entry_free(ce);
732 ea_bdebug(bs->bh, "modifying in-place");
733 error = ext4_xattr_set_entry(i, s);
735 if (!IS_LAST_ENTRY(s->first))
736 ext4_xattr_rehash(header(s->base),
738 ext4_xattr_cache_insert(bs->bh);
740 unlock_buffer(bs->bh);
744 error = ext4_handle_dirty_metadata(handle,
751 int offset = (char *)s->here - bs->bh->b_data;
753 unlock_buffer(bs->bh);
754 ext4_handle_release_buffer(handle, bs->bh);
756 mb_cache_entry_release(ce);
759 ea_bdebug(bs->bh, "cloning");
760 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
764 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
765 s->first = ENTRY(header(s->base)+1);
766 header(s->base)->h_refcount = cpu_to_le32(1);
767 s->here = ENTRY(s->base + offset);
768 s->end = s->base + bs->bh->b_size;
771 /* Allocate a buffer where we construct the new block. */
772 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
773 /* assert(header == s->base) */
777 header(s->base)->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
778 header(s->base)->h_blocks = cpu_to_le32(1);
779 header(s->base)->h_refcount = cpu_to_le32(1);
780 s->first = ENTRY(header(s->base)+1);
781 s->here = ENTRY(header(s->base)+1);
782 s->end = s->base + sb->s_blocksize;
785 error = ext4_xattr_set_entry(i, s);
790 if (!IS_LAST_ENTRY(s->first))
791 ext4_xattr_rehash(header(s->base), s->here);
794 if (!IS_LAST_ENTRY(s->first)) {
795 new_bh = ext4_xattr_cache_find(inode, header(s->base), &ce);
797 /* We found an identical block in the cache. */
798 if (new_bh == bs->bh)
799 ea_bdebug(new_bh, "keeping");
801 /* The old block is released after updating
803 error = dquot_alloc_block(inode,
804 EXT4_C2B(EXT4_SB(sb), 1));
807 error = ext4_journal_get_write_access(handle,
812 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
813 ea_bdebug(new_bh, "reusing; refcount now=%d",
814 le32_to_cpu(BHDR(new_bh)->h_refcount));
815 unlock_buffer(new_bh);
816 error = ext4_handle_dirty_metadata(handle,
822 mb_cache_entry_release(ce);
824 } else if (bs->bh && s->base == bs->bh->b_data) {
825 /* We were modifying this block in-place. */
826 ea_bdebug(bs->bh, "keeping this block");
830 /* We need to allocate a new block */
831 ext4_fsblk_t goal, block;
833 goal = ext4_group_first_block_no(sb,
834 EXT4_I(inode)->i_block_group);
836 /* non-extent files can't have physical blocks past 2^32 */
837 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
838 goal = goal & EXT4_MAX_BLOCK_FILE_PHYS;
841 * take i_data_sem because we will test
842 * i_delalloc_reserved_flag in ext4_mb_new_blocks
844 down_read((&EXT4_I(inode)->i_data_sem));
845 block = ext4_new_meta_blocks(handle, inode, goal, 0,
847 up_read((&EXT4_I(inode)->i_data_sem));
851 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
852 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
854 ea_idebug(inode, "creating block %d", block);
856 new_bh = sb_getblk(sb, block);
860 ext4_free_blocks(handle, inode, NULL, block, 1,
861 EXT4_FREE_BLOCKS_METADATA);
865 error = ext4_journal_get_create_access(handle, new_bh);
867 unlock_buffer(new_bh);
871 memcpy(new_bh->b_data, s->base, new_bh->b_size);
872 set_buffer_uptodate(new_bh);
873 unlock_buffer(new_bh);
874 ext4_xattr_cache_insert(new_bh);
875 error = ext4_handle_dirty_metadata(handle,
882 /* Update the inode. */
883 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
885 /* Drop the previous xattr block. */
886 if (bs->bh && bs->bh != new_bh)
887 ext4_xattr_release_block(handle, inode, bs->bh);
892 mb_cache_entry_release(ce);
894 if (!(bs->bh && s->base == bs->bh->b_data))
900 dquot_free_block(inode, EXT4_C2B(EXT4_SB(sb), 1));
904 EXT4_ERROR_INODE(inode, "bad block %llu",
905 EXT4_I(inode)->i_file_acl);
911 struct ext4_xattr_ibody_find {
912 struct ext4_xattr_search s;
913 struct ext4_iloc iloc;
917 ext4_xattr_ibody_find(struct inode *inode, struct ext4_xattr_info *i,
918 struct ext4_xattr_ibody_find *is)
920 struct ext4_xattr_ibody_header *header;
921 struct ext4_inode *raw_inode;
924 if (EXT4_I(inode)->i_extra_isize == 0)
926 raw_inode = ext4_raw_inode(&is->iloc);
927 header = IHDR(inode, raw_inode);
928 is->s.base = is->s.first = IFIRST(header);
929 is->s.here = is->s.first;
930 is->s.end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
931 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
932 error = ext4_xattr_check_names(IFIRST(header), is->s.end,
936 /* Find the named attribute. */
937 error = ext4_xattr_find_entry(&is->s.here, i->name_index,
939 (void *)is->s.base, 0);
940 if (error && error != -ENODATA)
942 is->s.not_found = error;
948 ext4_xattr_ibody_set(handle_t *handle, struct inode *inode,
949 struct ext4_xattr_info *i,
950 struct ext4_xattr_ibody_find *is)
952 struct ext4_xattr_ibody_header *header;
953 struct ext4_xattr_search *s = &is->s;
956 if (EXT4_I(inode)->i_extra_isize == 0)
958 error = ext4_xattr_set_entry(i, s);
961 header = IHDR(inode, ext4_raw_inode(&is->iloc));
962 if (!IS_LAST_ENTRY(s->first)) {
963 header->h_magic = cpu_to_le32(EXT4_XATTR_MAGIC);
964 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
966 header->h_magic = cpu_to_le32(0);
967 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
973 * ext4_xattr_set_handle()
975 * Create, replace or remove an extended attribute for this inode. Value
976 * is NULL to remove an existing extended attribute, and non-NULL to
977 * either replace an existing extended attribute, or create a new extended
978 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
979 * specify that an extended attribute must exist and must not exist
980 * previous to the call, respectively.
982 * Returns 0, or a negative error number on failure.
985 ext4_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
986 const char *name, const void *value, size_t value_len,
989 struct ext4_xattr_info i = {
990 .name_index = name_index,
993 .value_len = value_len,
996 struct ext4_xattr_ibody_find is = {
997 .s = { .not_found = -ENODATA, },
999 struct ext4_xattr_block_find bs = {
1000 .s = { .not_found = -ENODATA, },
1002 unsigned long no_expand;
1007 if (strlen(name) > 255)
1009 down_write(&EXT4_I(inode)->xattr_sem);
1010 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1011 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1013 error = ext4_reserve_inode_write(handle, inode, &is.iloc);
1017 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1018 struct ext4_inode *raw_inode = ext4_raw_inode(&is.iloc);
1019 memset(raw_inode, 0, EXT4_SB(inode->i_sb)->s_inode_size);
1020 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1023 error = ext4_xattr_ibody_find(inode, &i, &is);
1027 error = ext4_xattr_block_find(inode, &i, &bs);
1030 if (is.s.not_found && bs.s.not_found) {
1032 if (flags & XATTR_REPLACE)
1039 if (flags & XATTR_CREATE)
1043 if (!is.s.not_found)
1044 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1045 else if (!bs.s.not_found)
1046 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1048 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1049 if (!error && !bs.s.not_found) {
1051 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1052 } else if (error == -ENOSPC) {
1053 if (EXT4_I(inode)->i_file_acl && !bs.s.base) {
1054 error = ext4_xattr_block_find(inode, &i, &bs);
1058 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1061 if (!is.s.not_found) {
1063 error = ext4_xattr_ibody_set(handle, inode, &i,
1069 ext4_xattr_update_super_block(handle, inode->i_sb);
1070 inode->i_ctime = ext4_current_time(inode);
1072 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1073 error = ext4_mark_iloc_dirty(handle, inode, &is.iloc);
1075 * The bh is consumed by ext4_mark_iloc_dirty, even with
1080 ext4_handle_sync(handle);
1087 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1088 up_write(&EXT4_I(inode)->xattr_sem);
1095 * Like ext4_xattr_set_handle, but start from an inode. This extended
1096 * attribute modification is a filesystem transaction by itself.
1098 * Returns 0, or a negative error number on failure.
1101 ext4_xattr_set(struct inode *inode, int name_index, const char *name,
1102 const void *value, size_t value_len, int flags)
1105 int error, retries = 0;
1108 handle = ext4_journal_start(inode, EXT4_DATA_TRANS_BLOCKS(inode->i_sb));
1109 if (IS_ERR(handle)) {
1110 error = PTR_ERR(handle);
1114 error = ext4_xattr_set_handle(handle, inode, name_index, name,
1115 value, value_len, flags);
1116 error2 = ext4_journal_stop(handle);
1117 if (error == -ENOSPC &&
1118 ext4_should_retry_alloc(inode->i_sb, &retries))
1128 * Shift the EA entries in the inode to create space for the increased
1131 static void ext4_xattr_shift_entries(struct ext4_xattr_entry *entry,
1132 int value_offs_shift, void *to,
1133 void *from, size_t n, int blocksize)
1135 struct ext4_xattr_entry *last = entry;
1138 /* Adjust the value offsets of the entries */
1139 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1140 if (!last->e_value_block && last->e_value_size) {
1141 new_offs = le16_to_cpu(last->e_value_offs) +
1143 BUG_ON(new_offs + le32_to_cpu(last->e_value_size)
1145 last->e_value_offs = cpu_to_le16(new_offs);
1148 /* Shift the entries by n bytes */
1149 memmove(to, from, n);
1153 * Expand an inode by new_extra_isize bytes when EAs are present.
1154 * Returns 0 on success or negative error number on failure.
1156 int ext4_expand_extra_isize_ea(struct inode *inode, int new_extra_isize,
1157 struct ext4_inode *raw_inode, handle_t *handle)
1159 struct ext4_xattr_ibody_header *header;
1160 struct ext4_xattr_entry *entry, *last, *first;
1161 struct buffer_head *bh = NULL;
1162 struct ext4_xattr_ibody_find *is = NULL;
1163 struct ext4_xattr_block_find *bs = NULL;
1164 char *buffer = NULL, *b_entry_name = NULL;
1165 size_t min_offs, free;
1166 int total_ino, total_blk;
1167 void *base, *start, *end;
1168 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1169 int s_min_extra_isize = le16_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_min_extra_isize);
1171 down_write(&EXT4_I(inode)->xattr_sem);
1173 if (EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1174 up_write(&EXT4_I(inode)->xattr_sem);
1178 header = IHDR(inode, raw_inode);
1179 entry = IFIRST(header);
1182 * Check if enough free space is available in the inode to shift the
1183 * entries ahead by new_extra_isize.
1186 base = start = entry;
1187 end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size;
1188 min_offs = end - base;
1190 total_ino = sizeof(struct ext4_xattr_ibody_header);
1192 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1193 if (free >= new_extra_isize) {
1194 entry = IFIRST(header);
1195 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize
1196 - new_extra_isize, (void *)raw_inode +
1197 EXT4_GOOD_OLD_INODE_SIZE + new_extra_isize,
1198 (void *)header, total_ino,
1199 inode->i_sb->s_blocksize);
1200 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1206 * Enough free space isn't available in the inode, check if
1207 * EA block can hold new_extra_isize bytes.
1209 if (EXT4_I(inode)->i_file_acl) {
1210 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1214 if (ext4_xattr_check_block(bh)) {
1215 EXT4_ERROR_INODE(inode, "bad block %llu",
1216 EXT4_I(inode)->i_file_acl);
1222 end = bh->b_data + bh->b_size;
1223 min_offs = end - base;
1224 free = ext4_xattr_free_space(first, &min_offs, base,
1226 if (free < new_extra_isize) {
1227 if (!tried_min_extra_isize && s_min_extra_isize) {
1228 tried_min_extra_isize++;
1229 new_extra_isize = s_min_extra_isize;
1237 free = inode->i_sb->s_blocksize;
1240 while (new_extra_isize > 0) {
1241 size_t offs, size, entry_size;
1242 struct ext4_xattr_entry *small_entry = NULL;
1243 struct ext4_xattr_info i = {
1247 unsigned int total_size; /* EA entry size + value size */
1248 unsigned int shift_bytes; /* No. of bytes to shift EAs by? */
1249 unsigned int min_total_size = ~0U;
1251 is = kzalloc(sizeof(struct ext4_xattr_ibody_find), GFP_NOFS);
1252 bs = kzalloc(sizeof(struct ext4_xattr_block_find), GFP_NOFS);
1258 is->s.not_found = -ENODATA;
1259 bs->s.not_found = -ENODATA;
1263 last = IFIRST(header);
1264 /* Find the entry best suited to be pushed into EA block */
1266 for (; !IS_LAST_ENTRY(last); last = EXT4_XATTR_NEXT(last)) {
1268 EXT4_XATTR_SIZE(le32_to_cpu(last->e_value_size)) +
1269 EXT4_XATTR_LEN(last->e_name_len);
1270 if (total_size <= free && total_size < min_total_size) {
1271 if (total_size < new_extra_isize) {
1275 min_total_size = total_size;
1280 if (entry == NULL) {
1282 entry = small_entry;
1284 if (!tried_min_extra_isize &&
1285 s_min_extra_isize) {
1286 tried_min_extra_isize++;
1287 new_extra_isize = s_min_extra_isize;
1288 kfree(is); is = NULL;
1289 kfree(bs); bs = NULL;
1297 offs = le16_to_cpu(entry->e_value_offs);
1298 size = le32_to_cpu(entry->e_value_size);
1299 entry_size = EXT4_XATTR_LEN(entry->e_name_len);
1300 i.name_index = entry->e_name_index,
1301 buffer = kmalloc(EXT4_XATTR_SIZE(size), GFP_NOFS);
1302 b_entry_name = kmalloc(entry->e_name_len + 1, GFP_NOFS);
1303 if (!buffer || !b_entry_name) {
1307 /* Save the entry name and the entry value */
1308 memcpy(buffer, (void *)IFIRST(header) + offs,
1309 EXT4_XATTR_SIZE(size));
1310 memcpy(b_entry_name, entry->e_name, entry->e_name_len);
1311 b_entry_name[entry->e_name_len] = '\0';
1312 i.name = b_entry_name;
1314 error = ext4_get_inode_loc(inode, &is->iloc);
1318 error = ext4_xattr_ibody_find(inode, &i, is);
1322 /* Remove the chosen entry from the inode */
1323 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1327 entry = IFIRST(header);
1328 if (entry_size + EXT4_XATTR_SIZE(size) >= new_extra_isize)
1329 shift_bytes = new_extra_isize;
1331 shift_bytes = entry_size + size;
1332 /* Adjust the offsets and shift the remaining entries ahead */
1333 ext4_xattr_shift_entries(entry, EXT4_I(inode)->i_extra_isize -
1334 shift_bytes, (void *)raw_inode +
1335 EXT4_GOOD_OLD_INODE_SIZE + extra_isize + shift_bytes,
1336 (void *)header, total_ino - entry_size,
1337 inode->i_sb->s_blocksize);
1339 extra_isize += shift_bytes;
1340 new_extra_isize -= shift_bytes;
1341 EXT4_I(inode)->i_extra_isize = extra_isize;
1343 i.name = b_entry_name;
1346 error = ext4_xattr_block_find(inode, &i, bs);
1350 /* Add entry which was removed from the inode into the block */
1351 error = ext4_xattr_block_set(handle, inode, &i, bs);
1354 kfree(b_entry_name);
1356 b_entry_name = NULL;
1358 brelse(is->iloc.bh);
1363 up_write(&EXT4_I(inode)->xattr_sem);
1367 kfree(b_entry_name);
1370 brelse(is->iloc.bh);
1374 up_write(&EXT4_I(inode)->xattr_sem);
1381 * ext4_xattr_delete_inode()
1383 * Free extended attribute resources associated with this inode. This
1384 * is called immediately before an inode is freed. We have exclusive
1385 * access to the inode.
1388 ext4_xattr_delete_inode(handle_t *handle, struct inode *inode)
1390 struct buffer_head *bh = NULL;
1392 if (!EXT4_I(inode)->i_file_acl)
1394 bh = sb_bread(inode->i_sb, EXT4_I(inode)->i_file_acl);
1396 EXT4_ERROR_INODE(inode, "block %llu read error",
1397 EXT4_I(inode)->i_file_acl);
1400 if (BHDR(bh)->h_magic != cpu_to_le32(EXT4_XATTR_MAGIC) ||
1401 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1402 EXT4_ERROR_INODE(inode, "bad block %llu",
1403 EXT4_I(inode)->i_file_acl);
1406 ext4_xattr_release_block(handle, inode, bh);
1407 EXT4_I(inode)->i_file_acl = 0;
1414 * ext4_xattr_put_super()
1416 * This is called when a file system is unmounted.
1419 ext4_xattr_put_super(struct super_block *sb)
1421 mb_cache_shrink(sb->s_bdev);
1425 * ext4_xattr_cache_insert()
1427 * Create a new entry in the extended attribute cache, and insert
1428 * it unless such an entry is already in the cache.
1430 * Returns 0, or a negative error number on failure.
1433 ext4_xattr_cache_insert(struct buffer_head *bh)
1435 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1436 struct mb_cache_entry *ce;
1439 ce = mb_cache_entry_alloc(ext4_xattr_cache, GFP_NOFS);
1441 ea_bdebug(bh, "out of memory");
1444 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1446 mb_cache_entry_free(ce);
1447 if (error == -EBUSY) {
1448 ea_bdebug(bh, "already in cache");
1452 ea_bdebug(bh, "inserting [%x]", (int)hash);
1453 mb_cache_entry_release(ce);
1460 * Compare two extended attribute blocks for equality.
1462 * Returns 0 if the blocks are equal, 1 if they differ, and
1463 * a negative error number on errors.
1466 ext4_xattr_cmp(struct ext4_xattr_header *header1,
1467 struct ext4_xattr_header *header2)
1469 struct ext4_xattr_entry *entry1, *entry2;
1471 entry1 = ENTRY(header1+1);
1472 entry2 = ENTRY(header2+1);
1473 while (!IS_LAST_ENTRY(entry1)) {
1474 if (IS_LAST_ENTRY(entry2))
1476 if (entry1->e_hash != entry2->e_hash ||
1477 entry1->e_name_index != entry2->e_name_index ||
1478 entry1->e_name_len != entry2->e_name_len ||
1479 entry1->e_value_size != entry2->e_value_size ||
1480 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1482 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1484 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1485 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1486 le32_to_cpu(entry1->e_value_size)))
1489 entry1 = EXT4_XATTR_NEXT(entry1);
1490 entry2 = EXT4_XATTR_NEXT(entry2);
1492 if (!IS_LAST_ENTRY(entry2))
1498 * ext4_xattr_cache_find()
1500 * Find an identical extended attribute block.
1502 * Returns a pointer to the block found, or NULL if such a block was
1503 * not found or an error occurred.
1505 static struct buffer_head *
1506 ext4_xattr_cache_find(struct inode *inode, struct ext4_xattr_header *header,
1507 struct mb_cache_entry **pce)
1509 __u32 hash = le32_to_cpu(header->h_hash);
1510 struct mb_cache_entry *ce;
1512 if (!header->h_hash)
1513 return NULL; /* never share */
1514 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1516 ce = mb_cache_entry_find_first(ext4_xattr_cache, inode->i_sb->s_bdev,
1519 struct buffer_head *bh;
1522 if (PTR_ERR(ce) == -EAGAIN)
1526 bh = sb_bread(inode->i_sb, ce->e_block);
1528 EXT4_ERROR_INODE(inode, "block %lu read error",
1529 (unsigned long) ce->e_block);
1530 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1531 EXT4_XATTR_REFCOUNT_MAX) {
1532 ea_idebug(inode, "block %lu refcount %d>=%d",
1533 (unsigned long) ce->e_block,
1534 le32_to_cpu(BHDR(bh)->h_refcount),
1535 EXT4_XATTR_REFCOUNT_MAX);
1536 } else if (ext4_xattr_cmp(header, BHDR(bh)) == 0) {
1541 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1546 #define NAME_HASH_SHIFT 5
1547 #define VALUE_HASH_SHIFT 16
1550 * ext4_xattr_hash_entry()
1552 * Compute the hash of an extended attribute.
1554 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header *header,
1555 struct ext4_xattr_entry *entry)
1558 char *name = entry->e_name;
1561 for (n = 0; n < entry->e_name_len; n++) {
1562 hash = (hash << NAME_HASH_SHIFT) ^
1563 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1567 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1568 __le32 *value = (__le32 *)((char *)header +
1569 le16_to_cpu(entry->e_value_offs));
1570 for (n = (le32_to_cpu(entry->e_value_size) +
1571 EXT4_XATTR_ROUND) >> EXT4_XATTR_PAD_BITS; n; n--) {
1572 hash = (hash << VALUE_HASH_SHIFT) ^
1573 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1574 le32_to_cpu(*value++);
1577 entry->e_hash = cpu_to_le32(hash);
1580 #undef NAME_HASH_SHIFT
1581 #undef VALUE_HASH_SHIFT
1583 #define BLOCK_HASH_SHIFT 16
1586 * ext4_xattr_rehash()
1588 * Re-compute the extended attribute hash value after an entry has changed.
1590 static void ext4_xattr_rehash(struct ext4_xattr_header *header,
1591 struct ext4_xattr_entry *entry)
1593 struct ext4_xattr_entry *here;
1596 ext4_xattr_hash_entry(header, entry);
1597 here = ENTRY(header+1);
1598 while (!IS_LAST_ENTRY(here)) {
1599 if (!here->e_hash) {
1600 /* Block is not shared if an entry's hash value == 0 */
1604 hash = (hash << BLOCK_HASH_SHIFT) ^
1605 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1606 le32_to_cpu(here->e_hash);
1607 here = EXT4_XATTR_NEXT(here);
1609 header->h_hash = cpu_to_le32(hash);
1612 #undef BLOCK_HASH_SHIFT
1615 ext4_init_xattr(void)
1617 ext4_xattr_cache = mb_cache_create("ext4_xattr", 6);
1618 if (!ext4_xattr_cache)
1624 ext4_exit_xattr(void)
1626 if (ext4_xattr_cache)
1627 mb_cache_destroy(ext4_xattr_cache);
1628 ext4_xattr_cache = NULL;