2 * linux/fs/ext3/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext3 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 * EXT3_I(inode)->i_file_acl is protected by EXT3_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
54 #include <linux/mbcache.h>
55 #include <linux/quotaops.h>
59 #define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
60 #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
61 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
62 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
64 #define IHDR(inode, raw_inode) \
65 ((struct ext3_xattr_ibody_header *) \
66 ((void *)raw_inode + \
67 EXT3_GOOD_OLD_INODE_SIZE + \
68 EXT3_I(inode)->i_extra_isize))
69 #define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
71 #ifdef EXT3_XATTR_DEBUG
72 # define ea_idebug(inode, f...) do { \
73 printk(KERN_DEBUG "inode %s:%lu: ", \
74 inode->i_sb->s_id, inode->i_ino); \
78 # define ea_bdebug(bh, f...) do { \
79 char b[BDEVNAME_SIZE]; \
80 printk(KERN_DEBUG "block %s:%lu: ", \
81 bdevname(bh->b_bdev, b), \
82 (unsigned long) bh->b_blocknr); \
87 # define ea_idebug(f...)
88 # define ea_bdebug(f...)
91 static void ext3_xattr_cache_insert(struct buffer_head *);
92 static struct buffer_head *ext3_xattr_cache_find(struct inode *,
93 struct ext3_xattr_header *,
94 struct mb_cache_entry **);
95 static void ext3_xattr_rehash(struct ext3_xattr_header *,
96 struct ext3_xattr_entry *);
97 static int ext3_xattr_list(struct dentry *dentry, char *buffer,
100 static struct mb_cache *ext3_xattr_cache;
102 static const struct xattr_handler *ext3_xattr_handler_map[] = {
103 [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
104 #ifdef CONFIG_EXT3_FS_POSIX_ACL
105 [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
106 [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
108 [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
109 #ifdef CONFIG_EXT3_FS_SECURITY
110 [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
114 const struct xattr_handler *ext3_xattr_handlers[] = {
115 &ext3_xattr_user_handler,
116 &ext3_xattr_trusted_handler,
117 #ifdef CONFIG_EXT3_FS_POSIX_ACL
118 &posix_acl_access_xattr_handler,
119 &posix_acl_default_xattr_handler,
121 #ifdef CONFIG_EXT3_FS_SECURITY
122 &ext3_xattr_security_handler,
127 static inline const struct xattr_handler *
128 ext3_xattr_handler(int name_index)
130 const struct xattr_handler *handler = NULL;
132 if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
133 handler = ext3_xattr_handler_map[name_index];
138 * Inode operation listxattr()
140 * d_inode(dentry)->i_mutex: don't care
143 ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
145 return ext3_xattr_list(dentry, buffer, size);
149 ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
151 while (!IS_LAST_ENTRY(entry)) {
152 struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
153 if ((void *)next >= end)
161 ext3_xattr_check_block(struct buffer_head *bh)
165 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
166 BHDR(bh)->h_blocks != cpu_to_le32(1))
168 error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
173 ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
175 size_t value_size = le32_to_cpu(entry->e_value_size);
177 if (entry->e_value_block != 0 || value_size > size ||
178 le16_to_cpu(entry->e_value_offs) + value_size > size)
184 ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
185 const char *name, size_t size, int sorted)
187 struct ext3_xattr_entry *entry;
193 name_len = strlen(name);
195 for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
196 cmp = name_index - entry->e_name_index;
198 cmp = name_len - entry->e_name_len;
200 cmp = memcmp(name, entry->e_name, name_len);
201 if (cmp <= 0 && (sorted || cmp == 0))
205 if (!cmp && ext3_xattr_check_entry(entry, size))
207 return cmp ? -ENODATA : 0;
211 ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
212 void *buffer, size_t buffer_size)
214 struct buffer_head *bh = NULL;
215 struct ext3_xattr_entry *entry;
219 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
220 name_index, name, buffer, (long)buffer_size);
223 if (!EXT3_I(inode)->i_file_acl)
225 ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
226 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
229 ea_bdebug(bh, "b_count=%d, refcount=%d",
230 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
231 if (ext3_xattr_check_block(bh)) {
232 bad_block: ext3_error(inode->i_sb, __func__,
233 "inode %lu: bad block "E3FSBLK, inode->i_ino,
234 EXT3_I(inode)->i_file_acl);
238 ext3_xattr_cache_insert(bh);
240 error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
245 size = le32_to_cpu(entry->e_value_size);
248 if (size > buffer_size)
250 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
261 ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
262 void *buffer, size_t buffer_size)
264 struct ext3_xattr_ibody_header *header;
265 struct ext3_xattr_entry *entry;
266 struct ext3_inode *raw_inode;
267 struct ext3_iloc iloc;
272 if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
274 error = ext3_get_inode_loc(inode, &iloc);
277 raw_inode = ext3_raw_inode(&iloc);
278 header = IHDR(inode, raw_inode);
279 entry = IFIRST(header);
280 end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
281 error = ext3_xattr_check_names(entry, end);
284 error = ext3_xattr_find_entry(&entry, name_index, name,
285 end - (void *)entry, 0);
288 size = le32_to_cpu(entry->e_value_size);
291 if (size > buffer_size)
293 memcpy(buffer, (void *)IFIRST(header) +
294 le16_to_cpu(entry->e_value_offs), size);
306 * Copy an extended attribute into the buffer
307 * provided, or compute the buffer size required.
308 * Buffer is NULL to compute the size of the buffer required.
310 * Returns a negative error number on failure, or the number of bytes
311 * used / required on success.
314 ext3_xattr_get(struct inode *inode, int name_index, const char *name,
315 void *buffer, size_t buffer_size)
319 down_read(&EXT3_I(inode)->xattr_sem);
320 error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
322 if (error == -ENODATA)
323 error = ext3_xattr_block_get(inode, name_index, name, buffer,
325 up_read(&EXT3_I(inode)->xattr_sem);
330 ext3_xattr_list_entries(struct dentry *dentry, struct ext3_xattr_entry *entry,
331 char *buffer, size_t buffer_size)
333 size_t rest = buffer_size;
335 for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
336 const struct xattr_handler *handler =
337 ext3_xattr_handler(entry->e_name_index);
340 size_t size = handler->list(dentry, buffer, rest,
352 return buffer_size - rest;
356 ext3_xattr_block_list(struct dentry *dentry, char *buffer, size_t buffer_size)
358 struct inode *inode = d_inode(dentry);
359 struct buffer_head *bh = NULL;
362 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
363 buffer, (long)buffer_size);
366 if (!EXT3_I(inode)->i_file_acl)
368 ea_idebug(inode, "reading block %u", EXT3_I(inode)->i_file_acl);
369 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
373 ea_bdebug(bh, "b_count=%d, refcount=%d",
374 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
375 if (ext3_xattr_check_block(bh)) {
376 ext3_error(inode->i_sb, __func__,
377 "inode %lu: bad block "E3FSBLK, inode->i_ino,
378 EXT3_I(inode)->i_file_acl);
382 ext3_xattr_cache_insert(bh);
383 error = ext3_xattr_list_entries(dentry, BFIRST(bh), buffer, buffer_size);
392 ext3_xattr_ibody_list(struct dentry *dentry, char *buffer, size_t buffer_size)
394 struct inode *inode = d_inode(dentry);
395 struct ext3_xattr_ibody_header *header;
396 struct ext3_inode *raw_inode;
397 struct ext3_iloc iloc;
401 if (!ext3_test_inode_state(inode, EXT3_STATE_XATTR))
403 error = ext3_get_inode_loc(inode, &iloc);
406 raw_inode = ext3_raw_inode(&iloc);
407 header = IHDR(inode, raw_inode);
408 end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
409 error = ext3_xattr_check_names(IFIRST(header), end);
412 error = ext3_xattr_list_entries(dentry, IFIRST(header),
413 buffer, buffer_size);
423 * Copy a list of attribute names into the buffer
424 * provided, or compute the buffer size required.
425 * Buffer is NULL to compute the size of the buffer required.
427 * Returns a negative error number on failure, or the number of bytes
428 * used / required on success.
431 ext3_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
433 int i_error, b_error;
435 down_read(&EXT3_I(d_inode(dentry))->xattr_sem);
436 i_error = ext3_xattr_ibody_list(dentry, buffer, buffer_size);
442 buffer_size -= i_error;
444 b_error = ext3_xattr_block_list(dentry, buffer, buffer_size);
448 up_read(&EXT3_I(d_inode(dentry))->xattr_sem);
449 return i_error + b_error;
453 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
456 static void ext3_xattr_update_super_block(handle_t *handle,
457 struct super_block *sb)
459 if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
462 if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
463 EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR);
464 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
469 * Release the xattr block BH: If the reference count is > 1, decrement
470 * it; otherwise free the block.
473 ext3_xattr_release_block(handle_t *handle, struct inode *inode,
474 struct buffer_head *bh)
476 struct mb_cache_entry *ce = NULL;
479 ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
480 error = ext3_journal_get_write_access(handle, bh);
486 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
487 ea_bdebug(bh, "refcount now=0; freeing");
489 mb_cache_entry_free(ce);
490 ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
492 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
494 le32_add_cpu(&BHDR(bh)->h_refcount, -1);
495 error = ext3_journal_dirty_metadata(handle, bh);
498 dquot_free_block(inode, 1);
499 ea_bdebug(bh, "refcount now=%d; releasing",
500 le32_to_cpu(BHDR(bh)->h_refcount));
502 mb_cache_entry_release(ce);
506 ext3_std_error(inode->i_sb, error);
510 struct ext3_xattr_info {
517 struct ext3_xattr_search {
518 struct ext3_xattr_entry *first;
521 struct ext3_xattr_entry *here;
526 ext3_xattr_set_entry(struct ext3_xattr_info *i, struct ext3_xattr_search *s)
528 struct ext3_xattr_entry *last;
529 size_t free, min_offs = s->end - s->base, name_len = strlen(i->name);
531 /* Compute min_offs and last. */
533 for (; !IS_LAST_ENTRY(last); last = EXT3_XATTR_NEXT(last)) {
534 if (!last->e_value_block && last->e_value_size) {
535 size_t offs = le16_to_cpu(last->e_value_offs);
540 free = min_offs - ((void *)last - s->base) - sizeof(__u32);
542 if (!s->here->e_value_block && s->here->e_value_size) {
543 size_t size = le32_to_cpu(s->here->e_value_size);
544 free += EXT3_XATTR_SIZE(size);
546 free += EXT3_XATTR_LEN(name_len);
549 if (free < EXT3_XATTR_LEN(name_len) +
550 EXT3_XATTR_SIZE(i->value_len))
554 if (i->value && s->not_found) {
555 /* Insert the new name. */
556 size_t size = EXT3_XATTR_LEN(name_len);
557 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
558 memmove((void *)s->here + size, s->here, rest);
559 memset(s->here, 0, size);
560 s->here->e_name_index = i->name_index;
561 s->here->e_name_len = name_len;
562 memcpy(s->here->e_name, i->name, name_len);
564 if (!s->here->e_value_block && s->here->e_value_size) {
565 void *first_val = s->base + min_offs;
566 size_t offs = le16_to_cpu(s->here->e_value_offs);
567 void *val = s->base + offs;
568 size_t size = EXT3_XATTR_SIZE(
569 le32_to_cpu(s->here->e_value_size));
571 if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
572 /* The old and the new value have the same
573 size. Just replace. */
574 s->here->e_value_size =
575 cpu_to_le32(i->value_len);
576 memset(val + size - EXT3_XATTR_PAD, 0,
577 EXT3_XATTR_PAD); /* Clear pad bytes. */
578 memcpy(val, i->value, i->value_len);
582 /* Remove the old value. */
583 memmove(first_val + size, first_val, val - first_val);
584 memset(first_val, 0, size);
585 s->here->e_value_size = 0;
586 s->here->e_value_offs = 0;
589 /* Adjust all value offsets. */
591 while (!IS_LAST_ENTRY(last)) {
592 size_t o = le16_to_cpu(last->e_value_offs);
593 if (!last->e_value_block &&
594 last->e_value_size && o < offs)
596 cpu_to_le16(o + size);
597 last = EXT3_XATTR_NEXT(last);
601 /* Remove the old name. */
602 size_t size = EXT3_XATTR_LEN(name_len);
603 last = ENTRY((void *)last - size);
604 memmove(s->here, (void *)s->here + size,
605 (void *)last - (void *)s->here + sizeof(__u32));
606 memset(last, 0, size);
611 /* Insert the new value. */
612 s->here->e_value_size = cpu_to_le32(i->value_len);
614 size_t size = EXT3_XATTR_SIZE(i->value_len);
615 void *val = s->base + min_offs - size;
616 s->here->e_value_offs = cpu_to_le16(min_offs - size);
617 memset(val + size - EXT3_XATTR_PAD, 0,
618 EXT3_XATTR_PAD); /* Clear the pad bytes. */
619 memcpy(val, i->value, i->value_len);
625 struct ext3_xattr_block_find {
626 struct ext3_xattr_search s;
627 struct buffer_head *bh;
631 ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
632 struct ext3_xattr_block_find *bs)
634 struct super_block *sb = inode->i_sb;
637 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
638 i->name_index, i->name, i->value, (long)i->value_len);
640 if (EXT3_I(inode)->i_file_acl) {
641 /* The inode already has an extended attribute block. */
642 bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
646 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
647 atomic_read(&(bs->bh->b_count)),
648 le32_to_cpu(BHDR(bs->bh)->h_refcount));
649 if (ext3_xattr_check_block(bs->bh)) {
650 ext3_error(sb, __func__,
651 "inode %lu: bad block "E3FSBLK, inode->i_ino,
652 EXT3_I(inode)->i_file_acl);
656 /* Find the named attribute. */
657 bs->s.base = BHDR(bs->bh);
658 bs->s.first = BFIRST(bs->bh);
659 bs->s.end = bs->bh->b_data + bs->bh->b_size;
660 bs->s.here = bs->s.first;
661 error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
662 i->name, bs->bh->b_size, 1);
663 if (error && error != -ENODATA)
665 bs->s.not_found = error;
674 ext3_xattr_block_set(handle_t *handle, struct inode *inode,
675 struct ext3_xattr_info *i,
676 struct ext3_xattr_block_find *bs)
678 struct super_block *sb = inode->i_sb;
679 struct buffer_head *new_bh = NULL;
680 struct ext3_xattr_search *s = &bs->s;
681 struct mb_cache_entry *ce = NULL;
684 #define header(x) ((struct ext3_xattr_header *)(x))
686 if (i->value && i->value_len > sb->s_blocksize)
689 ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
691 error = ext3_journal_get_write_access(handle, bs->bh);
696 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
698 mb_cache_entry_free(ce);
701 ea_bdebug(bs->bh, "modifying in-place");
702 error = ext3_xattr_set_entry(i, s);
704 if (!IS_LAST_ENTRY(s->first))
705 ext3_xattr_rehash(header(s->base),
707 ext3_xattr_cache_insert(bs->bh);
709 unlock_buffer(bs->bh);
713 error = ext3_journal_dirty_metadata(handle,
719 int offset = (char *)s->here - bs->bh->b_data;
721 unlock_buffer(bs->bh);
722 journal_release_buffer(handle, bs->bh);
725 mb_cache_entry_release(ce);
728 ea_bdebug(bs->bh, "cloning");
729 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
733 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
734 s->first = ENTRY(header(s->base)+1);
735 header(s->base)->h_refcount = cpu_to_le32(1);
736 s->here = ENTRY(s->base + offset);
737 s->end = s->base + bs->bh->b_size;
740 /* Allocate a buffer where we construct the new block. */
741 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
742 /* assert(header == s->base) */
746 header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
747 header(s->base)->h_blocks = cpu_to_le32(1);
748 header(s->base)->h_refcount = cpu_to_le32(1);
749 s->first = ENTRY(header(s->base)+1);
750 s->here = ENTRY(header(s->base)+1);
751 s->end = s->base + sb->s_blocksize;
754 error = ext3_xattr_set_entry(i, s);
759 if (!IS_LAST_ENTRY(s->first))
760 ext3_xattr_rehash(header(s->base), s->here);
763 if (!IS_LAST_ENTRY(s->first)) {
764 new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
766 /* We found an identical block in the cache. */
767 if (new_bh == bs->bh)
768 ea_bdebug(new_bh, "keeping");
770 /* The old block is released after updating
772 error = dquot_alloc_block(inode, 1);
775 error = ext3_journal_get_write_access(handle,
780 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
781 ea_bdebug(new_bh, "reusing; refcount now=%d",
782 le32_to_cpu(BHDR(new_bh)->h_refcount));
783 unlock_buffer(new_bh);
784 error = ext3_journal_dirty_metadata(handle,
789 mb_cache_entry_release(ce);
791 } else if (bs->bh && s->base == bs->bh->b_data) {
792 /* We were modifying this block in-place. */
793 ea_bdebug(bs->bh, "keeping this block");
797 /* We need to allocate a new block */
798 ext3_fsblk_t goal = ext3_group_first_block_no(sb,
799 EXT3_I(inode)->i_block_group);
803 * Protect us agaist concurrent allocations to the
804 * same inode from ext3_..._writepage(). Reservation
805 * code does not expect racing allocations.
807 mutex_lock(&EXT3_I(inode)->truncate_mutex);
808 block = ext3_new_block(handle, inode, goal, &error);
809 mutex_unlock(&EXT3_I(inode)->truncate_mutex);
812 ea_idebug(inode, "creating block %d", block);
814 new_bh = sb_getblk(sb, block);
815 if (unlikely(!new_bh)) {
817 ext3_free_blocks(handle, inode, block, 1);
822 error = ext3_journal_get_create_access(handle, new_bh);
824 unlock_buffer(new_bh);
827 memcpy(new_bh->b_data, s->base, new_bh->b_size);
828 set_buffer_uptodate(new_bh);
829 unlock_buffer(new_bh);
830 ext3_xattr_cache_insert(new_bh);
831 error = ext3_journal_dirty_metadata(handle, new_bh);
837 /* Update the inode. */
838 EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
840 /* Drop the previous xattr block. */
841 if (bs->bh && bs->bh != new_bh)
842 ext3_xattr_release_block(handle, inode, bs->bh);
847 mb_cache_entry_release(ce);
849 if (!(bs->bh && s->base == bs->bh->b_data))
855 dquot_free_block(inode, 1);
859 ext3_error(inode->i_sb, __func__,
860 "inode %lu: bad block "E3FSBLK, inode->i_ino,
861 EXT3_I(inode)->i_file_acl);
867 struct ext3_xattr_ibody_find {
868 struct ext3_xattr_search s;
869 struct ext3_iloc iloc;
873 ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
874 struct ext3_xattr_ibody_find *is)
876 struct ext3_xattr_ibody_header *header;
877 struct ext3_inode *raw_inode;
880 if (EXT3_I(inode)->i_extra_isize == 0)
882 raw_inode = ext3_raw_inode(&is->iloc);
883 header = IHDR(inode, raw_inode);
884 is->s.base = is->s.first = IFIRST(header);
885 is->s.here = is->s.first;
886 is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
887 if (ext3_test_inode_state(inode, EXT3_STATE_XATTR)) {
888 error = ext3_xattr_check_names(IFIRST(header), is->s.end);
891 /* Find the named attribute. */
892 error = ext3_xattr_find_entry(&is->s.here, i->name_index,
894 (void *)is->s.base, 0);
895 if (error && error != -ENODATA)
897 is->s.not_found = error;
903 ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
904 struct ext3_xattr_info *i,
905 struct ext3_xattr_ibody_find *is)
907 struct ext3_xattr_ibody_header *header;
908 struct ext3_xattr_search *s = &is->s;
911 if (EXT3_I(inode)->i_extra_isize == 0)
913 error = ext3_xattr_set_entry(i, s);
916 header = IHDR(inode, ext3_raw_inode(&is->iloc));
917 if (!IS_LAST_ENTRY(s->first)) {
918 header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
919 ext3_set_inode_state(inode, EXT3_STATE_XATTR);
921 header->h_magic = cpu_to_le32(0);
922 ext3_clear_inode_state(inode, EXT3_STATE_XATTR);
928 * ext3_xattr_set_handle()
930 * Create, replace or remove an extended attribute for this inode. Value
931 * is NULL to remove an existing extended attribute, and non-NULL to
932 * either replace an existing extended attribute, or create a new extended
933 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
934 * specify that an extended attribute must exist and must not exist
935 * previous to the call, respectively.
937 * Returns 0, or a negative error number on failure.
940 ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
941 const char *name, const void *value, size_t value_len,
944 struct ext3_xattr_info i = {
945 .name_index = name_index,
948 .value_len = value_len,
951 struct ext3_xattr_ibody_find is = {
952 .s = { .not_found = -ENODATA, },
954 struct ext3_xattr_block_find bs = {
955 .s = { .not_found = -ENODATA, },
961 if (strlen(name) > 255)
963 down_write(&EXT3_I(inode)->xattr_sem);
964 error = ext3_get_inode_loc(inode, &is.iloc);
968 error = ext3_journal_get_write_access(handle, is.iloc.bh);
972 if (ext3_test_inode_state(inode, EXT3_STATE_NEW)) {
973 struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
974 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
975 ext3_clear_inode_state(inode, EXT3_STATE_NEW);
978 error = ext3_xattr_ibody_find(inode, &i, &is);
982 error = ext3_xattr_block_find(inode, &i, &bs);
985 if (is.s.not_found && bs.s.not_found) {
987 if (flags & XATTR_REPLACE)
994 if (flags & XATTR_CREATE)
999 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1000 else if (!bs.s.not_found)
1001 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1003 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1004 if (!error && !bs.s.not_found) {
1006 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1007 } else if (error == -ENOSPC) {
1008 if (EXT3_I(inode)->i_file_acl && !bs.s.base) {
1009 error = ext3_xattr_block_find(inode, &i, &bs);
1013 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1016 if (!is.s.not_found) {
1018 error = ext3_xattr_ibody_set(handle, inode, &i,
1024 ext3_xattr_update_super_block(handle, inode->i_sb);
1025 inode->i_ctime = CURRENT_TIME_SEC;
1026 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1028 * The bh is consumed by ext3_mark_iloc_dirty, even with
1039 up_write(&EXT3_I(inode)->xattr_sem);
1046 * Like ext3_xattr_set_handle, but start from an inode. This extended
1047 * attribute modification is a filesystem transaction by itself.
1049 * Returns 0, or a negative error number on failure.
1052 ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1053 const void *value, size_t value_len, int flags)
1056 int error, retries = 0;
1059 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1060 if (IS_ERR(handle)) {
1061 error = PTR_ERR(handle);
1065 error = ext3_xattr_set_handle(handle, inode, name_index, name,
1066 value, value_len, flags);
1067 error2 = ext3_journal_stop(handle);
1068 if (error == -ENOSPC &&
1069 ext3_should_retry_alloc(inode->i_sb, &retries))
1079 * ext3_xattr_delete_inode()
1081 * Free extended attribute resources associated with this inode. This
1082 * is called immediately before an inode is freed. We have exclusive
1083 * access to the inode.
1086 ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1088 struct buffer_head *bh = NULL;
1090 if (!EXT3_I(inode)->i_file_acl)
1092 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1094 ext3_error(inode->i_sb, __func__,
1095 "inode %lu: block "E3FSBLK" read error", inode->i_ino,
1096 EXT3_I(inode)->i_file_acl);
1099 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1100 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1101 ext3_error(inode->i_sb, __func__,
1102 "inode %lu: bad block "E3FSBLK, inode->i_ino,
1103 EXT3_I(inode)->i_file_acl);
1106 ext3_xattr_release_block(handle, inode, bh);
1107 EXT3_I(inode)->i_file_acl = 0;
1114 * ext3_xattr_put_super()
1116 * This is called when a file system is unmounted.
1119 ext3_xattr_put_super(struct super_block *sb)
1121 mb_cache_shrink(sb->s_bdev);
1125 * ext3_xattr_cache_insert()
1127 * Create a new entry in the extended attribute cache, and insert
1128 * it unless such an entry is already in the cache.
1130 * Returns 0, or a negative error number on failure.
1133 ext3_xattr_cache_insert(struct buffer_head *bh)
1135 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1136 struct mb_cache_entry *ce;
1139 ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
1141 ea_bdebug(bh, "out of memory");
1144 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1146 mb_cache_entry_free(ce);
1147 if (error == -EBUSY) {
1148 ea_bdebug(bh, "already in cache");
1152 ea_bdebug(bh, "inserting [%x]", (int)hash);
1153 mb_cache_entry_release(ce);
1160 * Compare two extended attribute blocks for equality.
1162 * Returns 0 if the blocks are equal, 1 if they differ, and
1163 * a negative error number on errors.
1166 ext3_xattr_cmp(struct ext3_xattr_header *header1,
1167 struct ext3_xattr_header *header2)
1169 struct ext3_xattr_entry *entry1, *entry2;
1171 entry1 = ENTRY(header1+1);
1172 entry2 = ENTRY(header2+1);
1173 while (!IS_LAST_ENTRY(entry1)) {
1174 if (IS_LAST_ENTRY(entry2))
1176 if (entry1->e_hash != entry2->e_hash ||
1177 entry1->e_name_index != entry2->e_name_index ||
1178 entry1->e_name_len != entry2->e_name_len ||
1179 entry1->e_value_size != entry2->e_value_size ||
1180 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1182 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1184 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1185 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1186 le32_to_cpu(entry1->e_value_size)))
1189 entry1 = EXT3_XATTR_NEXT(entry1);
1190 entry2 = EXT3_XATTR_NEXT(entry2);
1192 if (!IS_LAST_ENTRY(entry2))
1198 * ext3_xattr_cache_find()
1200 * Find an identical extended attribute block.
1202 * Returns a pointer to the block found, or NULL if such a block was
1203 * not found or an error occurred.
1205 static struct buffer_head *
1206 ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1207 struct mb_cache_entry **pce)
1209 __u32 hash = le32_to_cpu(header->h_hash);
1210 struct mb_cache_entry *ce;
1212 if (!header->h_hash)
1213 return NULL; /* never share */
1214 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1216 ce = mb_cache_entry_find_first(ext3_xattr_cache, inode->i_sb->s_bdev,
1219 struct buffer_head *bh;
1222 if (PTR_ERR(ce) == -EAGAIN)
1226 bh = sb_bread(inode->i_sb, ce->e_block);
1228 ext3_error(inode->i_sb, __func__,
1229 "inode %lu: block %lu read error",
1230 inode->i_ino, (unsigned long) ce->e_block);
1231 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1232 EXT3_XATTR_REFCOUNT_MAX) {
1233 ea_idebug(inode, "block %lu refcount %d>=%d",
1234 (unsigned long) ce->e_block,
1235 le32_to_cpu(BHDR(bh)->h_refcount),
1236 EXT3_XATTR_REFCOUNT_MAX);
1237 } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1242 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1247 #define NAME_HASH_SHIFT 5
1248 #define VALUE_HASH_SHIFT 16
1251 * ext3_xattr_hash_entry()
1253 * Compute the hash of an extended attribute.
1255 static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1256 struct ext3_xattr_entry *entry)
1259 char *name = entry->e_name;
1262 for (n=0; n < entry->e_name_len; n++) {
1263 hash = (hash << NAME_HASH_SHIFT) ^
1264 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1268 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1269 __le32 *value = (__le32 *)((char *)header +
1270 le16_to_cpu(entry->e_value_offs));
1271 for (n = (le32_to_cpu(entry->e_value_size) +
1272 EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1273 hash = (hash << VALUE_HASH_SHIFT) ^
1274 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1275 le32_to_cpu(*value++);
1278 entry->e_hash = cpu_to_le32(hash);
1281 #undef NAME_HASH_SHIFT
1282 #undef VALUE_HASH_SHIFT
1284 #define BLOCK_HASH_SHIFT 16
1287 * ext3_xattr_rehash()
1289 * Re-compute the extended attribute hash value after an entry has changed.
1291 static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1292 struct ext3_xattr_entry *entry)
1294 struct ext3_xattr_entry *here;
1297 ext3_xattr_hash_entry(header, entry);
1298 here = ENTRY(header+1);
1299 while (!IS_LAST_ENTRY(here)) {
1300 if (!here->e_hash) {
1301 /* Block is not shared if an entry's hash value == 0 */
1305 hash = (hash << BLOCK_HASH_SHIFT) ^
1306 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1307 le32_to_cpu(here->e_hash);
1308 here = EXT3_XATTR_NEXT(here);
1310 header->h_hash = cpu_to_le32(hash);
1313 #undef BLOCK_HASH_SHIFT
1316 init_ext3_xattr(void)
1318 ext3_xattr_cache = mb_cache_create("ext3_xattr", 6);
1319 if (!ext3_xattr_cache)
1325 exit_ext3_xattr(void)
1327 if (ext3_xattr_cache)
1328 mb_cache_destroy(ext3_xattr_cache);
1329 ext3_xattr_cache = NULL;