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
53 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/ext3_jbd.h>
57 #include <linux/ext3_fs.h>
58 #include <linux/mbcache.h>
59 #include <linux/quotaops.h>
60 #include <linux/rwsem.h>
61 #include <linux/vs_dlimit.h>
65 #define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
66 #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
67 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
68 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
70 #define IHDR(inode, raw_inode) \
71 ((struct ext3_xattr_ibody_header *) \
72 ((void *)raw_inode + \
73 EXT3_GOOD_OLD_INODE_SIZE + \
74 EXT3_I(inode)->i_extra_isize))
75 #define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
77 #ifdef EXT3_XATTR_DEBUG
78 # define ea_idebug(inode, f...) do { \
79 printk(KERN_DEBUG "inode %s:%ld: ", \
80 inode->i_sb->s_id, inode->i_ino); \
84 # define ea_bdebug(bh, f...) do { \
85 char b[BDEVNAME_SIZE]; \
86 printk(KERN_DEBUG "block %s:%lu: ", \
87 bdevname(bh->b_bdev, b), \
88 (unsigned long) bh->b_blocknr); \
93 # define ea_idebug(f...)
94 # define ea_bdebug(f...)
97 static void ext3_xattr_cache_insert(struct buffer_head *);
98 static struct buffer_head *ext3_xattr_cache_find(struct inode *,
99 struct ext3_xattr_header *,
100 struct mb_cache_entry **);
101 static void ext3_xattr_rehash(struct ext3_xattr_header *,
102 struct ext3_xattr_entry *);
104 static struct mb_cache *ext3_xattr_cache;
106 static struct xattr_handler *ext3_xattr_handler_map[] = {
107 [EXT3_XATTR_INDEX_USER] = &ext3_xattr_user_handler,
108 #ifdef CONFIG_EXT3_FS_POSIX_ACL
109 [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS] = &ext3_xattr_acl_access_handler,
110 [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_handler,
112 [EXT3_XATTR_INDEX_TRUSTED] = &ext3_xattr_trusted_handler,
113 #ifdef CONFIG_EXT3_FS_SECURITY
114 [EXT3_XATTR_INDEX_SECURITY] = &ext3_xattr_security_handler,
118 struct xattr_handler *ext3_xattr_handlers[] = {
119 &ext3_xattr_user_handler,
120 &ext3_xattr_trusted_handler,
121 #ifdef CONFIG_EXT3_FS_POSIX_ACL
122 &ext3_xattr_acl_access_handler,
123 &ext3_xattr_acl_default_handler,
125 #ifdef CONFIG_EXT3_FS_SECURITY
126 &ext3_xattr_security_handler,
131 static inline struct xattr_handler *
132 ext3_xattr_handler(int name_index)
134 struct xattr_handler *handler = NULL;
136 if (name_index > 0 && name_index < ARRAY_SIZE(ext3_xattr_handler_map))
137 handler = ext3_xattr_handler_map[name_index];
142 * Inode operation listxattr()
144 * dentry->d_inode->i_mutex: don't care
147 ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
149 return ext3_xattr_list(dentry->d_inode, buffer, size);
153 ext3_xattr_check_names(struct ext3_xattr_entry *entry, void *end)
155 while (!IS_LAST_ENTRY(entry)) {
156 struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(entry);
157 if ((void *)next >= end)
165 ext3_xattr_check_block(struct buffer_head *bh)
169 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
170 BHDR(bh)->h_blocks != cpu_to_le32(1))
172 error = ext3_xattr_check_names(BFIRST(bh), bh->b_data + bh->b_size);
177 ext3_xattr_check_entry(struct ext3_xattr_entry *entry, size_t size)
179 size_t value_size = le32_to_cpu(entry->e_value_size);
181 if (entry->e_value_block != 0 || value_size > size ||
182 le16_to_cpu(entry->e_value_offs) + value_size > size)
188 ext3_xattr_find_entry(struct ext3_xattr_entry **pentry, int name_index,
189 const char *name, size_t size, int sorted)
191 struct ext3_xattr_entry *entry;
197 name_len = strlen(name);
199 for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
200 cmp = name_index - entry->e_name_index;
202 cmp = name_len - entry->e_name_len;
204 cmp = memcmp(name, entry->e_name, name_len);
205 if (cmp <= 0 && (sorted || cmp == 0))
209 if (!cmp && ext3_xattr_check_entry(entry, size))
211 return cmp ? -ENODATA : 0;
215 ext3_xattr_block_get(struct inode *inode, int name_index, const char *name,
216 void *buffer, size_t buffer_size)
218 struct buffer_head *bh = NULL;
219 struct ext3_xattr_entry *entry;
223 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
224 name_index, name, buffer, (long)buffer_size);
227 if (!EXT3_I(inode)->i_file_acl)
229 ea_idebug(inode, "reading block %d", EXT3_I(inode)->i_file_acl);
230 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
233 ea_bdebug(bh, "b_count=%d, refcount=%d",
234 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
235 if (ext3_xattr_check_block(bh)) {
236 bad_block: ext3_error(inode->i_sb, __FUNCTION__,
237 "inode %ld: bad block %d", inode->i_ino,
238 EXT3_I(inode)->i_file_acl);
242 ext3_xattr_cache_insert(bh);
244 error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
249 size = le32_to_cpu(entry->e_value_size);
252 if (size > buffer_size)
254 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
265 ext3_xattr_ibody_get(struct inode *inode, int name_index, const char *name,
266 void *buffer, size_t buffer_size)
268 struct ext3_xattr_ibody_header *header;
269 struct ext3_xattr_entry *entry;
270 struct ext3_inode *raw_inode;
271 struct ext3_iloc iloc;
276 if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
278 error = ext3_get_inode_loc(inode, &iloc);
281 raw_inode = ext3_raw_inode(&iloc);
282 header = IHDR(inode, raw_inode);
283 entry = IFIRST(header);
284 end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
285 error = ext3_xattr_check_names(entry, end);
288 error = ext3_xattr_find_entry(&entry, name_index, name,
289 end - (void *)entry, 0);
292 size = le32_to_cpu(entry->e_value_size);
295 if (size > buffer_size)
297 memcpy(buffer, (void *)IFIRST(header) +
298 le16_to_cpu(entry->e_value_offs), size);
310 * Copy an extended attribute into the buffer
311 * provided, or compute the buffer size required.
312 * Buffer is NULL to compute the size of the buffer required.
314 * Returns a negative error number on failure, or the number of bytes
315 * used / required on success.
318 ext3_xattr_get(struct inode *inode, int name_index, const char *name,
319 void *buffer, size_t buffer_size)
323 down_read(&EXT3_I(inode)->xattr_sem);
324 error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
326 if (error == -ENODATA)
327 error = ext3_xattr_block_get(inode, name_index, name, buffer,
329 up_read(&EXT3_I(inode)->xattr_sem);
334 ext3_xattr_list_entries(struct inode *inode, struct ext3_xattr_entry *entry,
335 char *buffer, size_t buffer_size)
337 size_t rest = buffer_size;
339 for (; !IS_LAST_ENTRY(entry); entry = EXT3_XATTR_NEXT(entry)) {
340 struct xattr_handler *handler =
341 ext3_xattr_handler(entry->e_name_index);
344 size_t size = handler->list(inode, buffer, rest,
355 return buffer_size - rest;
359 ext3_xattr_block_list(struct inode *inode, char *buffer, size_t buffer_size)
361 struct buffer_head *bh = NULL;
364 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
365 buffer, (long)buffer_size);
368 if (!EXT3_I(inode)->i_file_acl)
370 ea_idebug(inode, "reading block %d", EXT3_I(inode)->i_file_acl);
371 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
375 ea_bdebug(bh, "b_count=%d, refcount=%d",
376 atomic_read(&(bh->b_count)), le32_to_cpu(BHDR(bh)->h_refcount));
377 if (ext3_xattr_check_block(bh)) {
378 ext3_error(inode->i_sb, __FUNCTION__,
379 "inode %ld: bad block %d", inode->i_ino,
380 EXT3_I(inode)->i_file_acl);
384 ext3_xattr_cache_insert(bh);
385 error = ext3_xattr_list_entries(inode, BFIRST(bh), buffer, buffer_size);
394 ext3_xattr_ibody_list(struct inode *inode, char *buffer, size_t buffer_size)
396 struct ext3_xattr_ibody_header *header;
397 struct ext3_inode *raw_inode;
398 struct ext3_iloc iloc;
402 if (!(EXT3_I(inode)->i_state & EXT3_STATE_XATTR))
404 error = ext3_get_inode_loc(inode, &iloc);
407 raw_inode = ext3_raw_inode(&iloc);
408 header = IHDR(inode, raw_inode);
409 end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
410 error = ext3_xattr_check_names(IFIRST(header), end);
413 error = ext3_xattr_list_entries(inode, IFIRST(header),
414 buffer, buffer_size);
424 * Copy a list of attribute names into the buffer
425 * provided, or compute the buffer size required.
426 * Buffer is NULL to compute the size of the buffer required.
428 * Returns a negative error number on failure, or the number of bytes
429 * used / required on success.
432 ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
434 int i_error, b_error;
436 down_read(&EXT3_I(inode)->xattr_sem);
437 i_error = ext3_xattr_ibody_list(inode, buffer, buffer_size);
443 buffer_size -= i_error;
445 b_error = ext3_xattr_block_list(inode, buffer, buffer_size);
449 up_read(&EXT3_I(inode)->xattr_sem);
450 return i_error + b_error;
454 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
457 static void ext3_xattr_update_super_block(handle_t *handle,
458 struct super_block *sb)
460 if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
464 if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
465 EXT3_SB(sb)->s_es->s_feature_compat |=
466 cpu_to_le32(EXT3_FEATURE_COMPAT_EXT_ATTR);
468 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
474 * Release the xattr block BH: If the reference count is > 1, decrement
475 * it; otherwise free the block.
478 ext3_xattr_release_block(handle_t *handle, struct inode *inode,
479 struct buffer_head *bh)
481 struct mb_cache_entry *ce = NULL;
483 ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev, bh->b_blocknr);
484 if (BHDR(bh)->h_refcount == cpu_to_le32(1)) {
485 ea_bdebug(bh, "refcount now=0; freeing");
487 mb_cache_entry_free(ce);
488 ext3_free_blocks(handle, inode, bh->b_blocknr, 1);
490 ext3_forget(handle, 1, inode, bh, bh->b_blocknr);
492 if (ext3_journal_get_write_access(handle, bh) == 0) {
494 BHDR(bh)->h_refcount = cpu_to_le32(
495 le32_to_cpu(BHDR(bh)->h_refcount) - 1);
496 ext3_journal_dirty_metadata(handle, bh);
499 DLIMIT_FREE_BLOCK(inode, 1);
500 DQUOT_FREE_BLOCK(inode, 1);
502 ea_bdebug(bh, "refcount now=%d; releasing",
503 le32_to_cpu(BHDR(bh)->h_refcount));
506 mb_cache_entry_release(ce);
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_SIZE(i->value_len) ||
550 free < EXT3_XATTR_LEN(name_len) +
551 EXT3_XATTR_SIZE(i->value_len))
555 if (i->value && s->not_found) {
556 /* Insert the new name. */
557 size_t size = EXT3_XATTR_LEN(name_len);
558 size_t rest = (void *)last - (void *)s->here + sizeof(__u32);
559 memmove((void *)s->here + size, s->here, rest);
560 memset(s->here, 0, size);
561 s->here->e_name_index = i->name_index;
562 s->here->e_name_len = name_len;
563 memcpy(s->here->e_name, i->name, name_len);
565 if (!s->here->e_value_block && s->here->e_value_size) {
566 void *first_val = s->base + min_offs;
567 size_t offs = le16_to_cpu(s->here->e_value_offs);
568 void *val = s->base + offs;
569 size_t size = EXT3_XATTR_SIZE(
570 le32_to_cpu(s->here->e_value_size));
572 if (i->value && size == EXT3_XATTR_SIZE(i->value_len)) {
573 /* The old and the new value have the same
574 size. Just replace. */
575 s->here->e_value_size =
576 cpu_to_le32(i->value_len);
577 memset(val + size - EXT3_XATTR_PAD, 0,
578 EXT3_XATTR_PAD); /* Clear pad bytes. */
579 memcpy(val, i->value, i->value_len);
583 /* Remove the old value. */
584 memmove(first_val + size, first_val, val - first_val);
585 memset(first_val, 0, size);
586 s->here->e_value_size = 0;
587 s->here->e_value_offs = 0;
590 /* Adjust all value offsets. */
592 while (!IS_LAST_ENTRY(last)) {
593 size_t o = le16_to_cpu(last->e_value_offs);
594 if (!last->e_value_block &&
595 last->e_value_size && o < offs)
597 cpu_to_le16(o + size);
598 last = EXT3_XATTR_NEXT(last);
602 /* Remove the old name. */
603 size_t size = EXT3_XATTR_LEN(name_len);
604 last = ENTRY((void *)last - size);
605 memmove(s->here, (void *)s->here + size,
606 (void *)last - (void *)s->here + sizeof(__u32));
607 memset(last, 0, size);
612 /* Insert the new value. */
613 s->here->e_value_size = cpu_to_le32(i->value_len);
615 size_t size = EXT3_XATTR_SIZE(i->value_len);
616 void *val = s->base + min_offs - size;
617 s->here->e_value_offs = cpu_to_le16(min_offs - size);
618 memset(val + size - EXT3_XATTR_PAD, 0,
619 EXT3_XATTR_PAD); /* Clear the pad bytes. */
620 memcpy(val, i->value, i->value_len);
626 struct ext3_xattr_block_find {
627 struct ext3_xattr_search s;
628 struct buffer_head *bh;
632 ext3_xattr_block_find(struct inode *inode, struct ext3_xattr_info *i,
633 struct ext3_xattr_block_find *bs)
635 struct super_block *sb = inode->i_sb;
638 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
639 i->name_index, i->name, i->value, (long)i->value_len);
641 if (EXT3_I(inode)->i_file_acl) {
642 /* The inode already has an extended attribute block. */
643 bs->bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
647 ea_bdebug(bs->bh, "b_count=%d, refcount=%d",
648 atomic_read(&(bs->bh->b_count)),
649 le32_to_cpu(BHDR(bs->bh)->h_refcount));
650 if (ext3_xattr_check_block(bs->bh)) {
651 ext3_error(sb, __FUNCTION__,
652 "inode %ld: bad block %d", inode->i_ino,
653 EXT3_I(inode)->i_file_acl);
657 /* Find the named attribute. */
658 bs->s.base = BHDR(bs->bh);
659 bs->s.first = BFIRST(bs->bh);
660 bs->s.end = bs->bh->b_data + bs->bh->b_size;
661 bs->s.here = bs->s.first;
662 error = ext3_xattr_find_entry(&bs->s.here, i->name_index,
663 i->name, bs->bh->b_size, 1);
664 if (error && error != -ENODATA)
666 bs->s.not_found = error;
675 ext3_xattr_block_set(handle_t *handle, struct inode *inode,
676 struct ext3_xattr_info *i,
677 struct ext3_xattr_block_find *bs)
679 struct super_block *sb = inode->i_sb;
680 struct buffer_head *new_bh = NULL;
681 struct ext3_xattr_search *s = &bs->s;
682 struct mb_cache_entry *ce = NULL;
685 #define header(x) ((struct ext3_xattr_header *)(x))
687 if (i->value && i->value_len > sb->s_blocksize)
690 ce = mb_cache_entry_get(ext3_xattr_cache, bs->bh->b_bdev,
692 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
694 mb_cache_entry_free(ce);
697 ea_bdebug(bs->bh, "modifying in-place");
698 error = ext3_journal_get_write_access(handle, bs->bh);
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;
722 mb_cache_entry_release(ce);
725 ea_bdebug(bs->bh, "cloning");
726 s->base = kmalloc(bs->bh->b_size, GFP_KERNEL);
730 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
731 s->first = ENTRY(header(s->base)+1);
732 header(s->base)->h_refcount = cpu_to_le32(1);
733 s->here = ENTRY(s->base + offset);
734 s->end = s->base + bs->bh->b_size;
737 /* Allocate a buffer where we construct the new block. */
738 s->base = kmalloc(sb->s_blocksize, GFP_KERNEL);
739 /* assert(header == s->base) */
743 memset(s->base, 0, sb->s_blocksize);
744 header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
745 header(s->base)->h_blocks = cpu_to_le32(1);
746 header(s->base)->h_refcount = cpu_to_le32(1);
747 s->first = ENTRY(header(s->base)+1);
748 s->here = ENTRY(header(s->base)+1);
749 s->end = s->base + sb->s_blocksize;
752 error = ext3_xattr_set_entry(i, s);
757 if (!IS_LAST_ENTRY(s->first))
758 ext3_xattr_rehash(header(s->base), s->here);
761 if (!IS_LAST_ENTRY(s->first)) {
762 new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
764 /* We found an identical block in the cache. */
765 if (new_bh == bs->bh)
766 ea_bdebug(new_bh, "keeping");
769 if (DLIMIT_ALLOC_BLOCK(inode, 1))
771 /* The old block is released after updating
774 if (DQUOT_ALLOC_BLOCK(inode, 1))
776 error = ext3_journal_get_write_access(handle,
781 BHDR(new_bh)->h_refcount = cpu_to_le32(1 +
782 le32_to_cpu(BHDR(new_bh)->h_refcount));
783 ea_bdebug(new_bh, "reusing; refcount now=%d",
784 le32_to_cpu(BHDR(new_bh)->h_refcount));
785 unlock_buffer(new_bh);
786 error = ext3_journal_dirty_metadata(handle,
791 mb_cache_entry_release(ce);
793 } else if (bs->bh && s->base == bs->bh->b_data) {
794 /* We were modifying this block in-place. */
795 ea_bdebug(bs->bh, "keeping this block");
799 /* We need to allocate a new block */
800 int goal = le32_to_cpu(
801 EXT3_SB(sb)->s_es->s_first_data_block) +
802 EXT3_I(inode)->i_block_group *
803 EXT3_BLOCKS_PER_GROUP(sb);
804 int block = ext3_new_block(handle, inode, goal, &error);
807 ea_idebug(inode, "creating block %d", block);
809 new_bh = sb_getblk(sb, block);
812 ext3_free_blocks(handle, inode, block, 1);
817 error = ext3_journal_get_create_access(handle, new_bh);
819 unlock_buffer(new_bh);
822 memcpy(new_bh->b_data, s->base, new_bh->b_size);
823 set_buffer_uptodate(new_bh);
824 unlock_buffer(new_bh);
825 ext3_xattr_cache_insert(new_bh);
826 error = ext3_journal_dirty_metadata(handle, new_bh);
832 /* Update the inode. */
833 EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
835 /* Drop the previous xattr block. */
836 if (bs->bh && bs->bh != new_bh)
837 ext3_xattr_release_block(handle, inode, bs->bh);
842 mb_cache_entry_release(ce);
844 if (!(bs->bh && s->base == bs->bh->b_data))
850 DQUOT_FREE_BLOCK(inode, 1);
852 DLIMIT_FREE_BLOCK(inode, 1);
856 ext3_error(inode->i_sb, __FUNCTION__,
857 "inode %ld: bad block %d", inode->i_ino,
858 EXT3_I(inode)->i_file_acl);
864 struct ext3_xattr_ibody_find {
865 struct ext3_xattr_search s;
866 struct ext3_iloc iloc;
870 ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
871 struct ext3_xattr_ibody_find *is)
873 struct ext3_xattr_ibody_header *header;
874 struct ext3_inode *raw_inode;
877 if (EXT3_I(inode)->i_extra_isize == 0)
879 raw_inode = ext3_raw_inode(&is->iloc);
880 header = IHDR(inode, raw_inode);
881 is->s.base = is->s.first = IFIRST(header);
882 is->s.here = is->s.first;
883 is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
884 if (EXT3_I(inode)->i_state & EXT3_STATE_XATTR) {
885 error = ext3_xattr_check_names(IFIRST(header), is->s.end);
888 /* Find the named attribute. */
889 error = ext3_xattr_find_entry(&is->s.here, i->name_index,
891 (void *)is->s.base, 0);
892 if (error && error != -ENODATA)
894 is->s.not_found = error;
900 ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
901 struct ext3_xattr_info *i,
902 struct ext3_xattr_ibody_find *is)
904 struct ext3_xattr_ibody_header *header;
905 struct ext3_xattr_search *s = &is->s;
908 if (EXT3_I(inode)->i_extra_isize == 0)
910 error = ext3_xattr_set_entry(i, s);
913 header = IHDR(inode, ext3_raw_inode(&is->iloc));
914 if (!IS_LAST_ENTRY(s->first)) {
915 header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
916 EXT3_I(inode)->i_state |= EXT3_STATE_XATTR;
918 header->h_magic = cpu_to_le32(0);
919 EXT3_I(inode)->i_state &= ~EXT3_STATE_XATTR;
925 * ext3_xattr_set_handle()
927 * Create, replace or remove an extended attribute for this inode. Buffer
928 * is NULL to remove an existing extended attribute, and non-NULL to
929 * either replace an existing extended attribute, or create a new extended
930 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
931 * specify that an extended attribute must exist and must not exist
932 * previous to the call, respectively.
934 * Returns 0, or a negative error number on failure.
937 ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
938 const char *name, const void *value, size_t value_len,
941 struct ext3_xattr_info i = {
942 .name_index = name_index,
945 .value_len = value_len,
948 struct ext3_xattr_ibody_find is = {
949 .s = { .not_found = -ENODATA, },
951 struct ext3_xattr_block_find bs = {
952 .s = { .not_found = -ENODATA, },
958 if (strlen(name) > 255)
960 down_write(&EXT3_I(inode)->xattr_sem);
961 error = ext3_get_inode_loc(inode, &is.iloc);
965 if (EXT3_I(inode)->i_state & EXT3_STATE_NEW) {
966 struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
967 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
968 EXT3_I(inode)->i_state &= ~EXT3_STATE_NEW;
971 error = ext3_xattr_ibody_find(inode, &i, &is);
975 error = ext3_xattr_block_find(inode, &i, &bs);
978 if (is.s.not_found && bs.s.not_found) {
980 if (flags & XATTR_REPLACE)
987 if (flags & XATTR_CREATE)
990 error = ext3_journal_get_write_access(handle, is.iloc.bh);
995 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
996 else if (!bs.s.not_found)
997 error = ext3_xattr_block_set(handle, inode, &i, &bs);
999 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1000 if (!error && !bs.s.not_found) {
1002 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1003 } else if (error == -ENOSPC) {
1004 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1007 if (!is.s.not_found) {
1009 error = ext3_xattr_ibody_set(handle, inode, &i,
1015 ext3_xattr_update_super_block(handle, inode->i_sb);
1016 inode->i_ctime = CURRENT_TIME_SEC;
1017 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1019 * The bh is consumed by ext3_mark_iloc_dirty, even with
1030 up_write(&EXT3_I(inode)->xattr_sem);
1037 * Like ext3_xattr_set_handle, but start from an inode. This extended
1038 * attribute modification is a filesystem transaction by itself.
1040 * Returns 0, or a negative error number on failure.
1043 ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1044 const void *value, size_t value_len, int flags)
1047 int error, retries = 0;
1050 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1051 if (IS_ERR(handle)) {
1052 error = PTR_ERR(handle);
1056 error = ext3_xattr_set_handle(handle, inode, name_index, name,
1057 value, value_len, flags);
1058 error2 = ext3_journal_stop(handle);
1059 if (error == -ENOSPC &&
1060 ext3_should_retry_alloc(inode->i_sb, &retries))
1070 * ext3_xattr_delete_inode()
1072 * Free extended attribute resources associated with this inode. This
1073 * is called immediately before an inode is freed. We have exclusive
1074 * access to the inode.
1077 ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1079 struct buffer_head *bh = NULL;
1081 if (!EXT3_I(inode)->i_file_acl)
1083 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1085 ext3_error(inode->i_sb, __FUNCTION__,
1086 "inode %ld: block %d read error", inode->i_ino,
1087 EXT3_I(inode)->i_file_acl);
1090 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1091 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1092 ext3_error(inode->i_sb, __FUNCTION__,
1093 "inode %ld: bad block %d", inode->i_ino,
1094 EXT3_I(inode)->i_file_acl);
1097 ext3_xattr_release_block(handle, inode, bh);
1098 EXT3_I(inode)->i_file_acl = 0;
1105 * ext3_xattr_put_super()
1107 * This is called when a file system is unmounted.
1110 ext3_xattr_put_super(struct super_block *sb)
1112 mb_cache_shrink(sb->s_bdev);
1116 * ext3_xattr_cache_insert()
1118 * Create a new entry in the extended attribute cache, and insert
1119 * it unless such an entry is already in the cache.
1121 * Returns 0, or a negative error number on failure.
1124 ext3_xattr_cache_insert(struct buffer_head *bh)
1126 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1127 struct mb_cache_entry *ce;
1130 ce = mb_cache_entry_alloc(ext3_xattr_cache);
1132 ea_bdebug(bh, "out of memory");
1135 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
1137 mb_cache_entry_free(ce);
1138 if (error == -EBUSY) {
1139 ea_bdebug(bh, "already in cache");
1143 ea_bdebug(bh, "inserting [%x]", (int)hash);
1144 mb_cache_entry_release(ce);
1151 * Compare two extended attribute blocks for equality.
1153 * Returns 0 if the blocks are equal, 1 if they differ, and
1154 * a negative error number on errors.
1157 ext3_xattr_cmp(struct ext3_xattr_header *header1,
1158 struct ext3_xattr_header *header2)
1160 struct ext3_xattr_entry *entry1, *entry2;
1162 entry1 = ENTRY(header1+1);
1163 entry2 = ENTRY(header2+1);
1164 while (!IS_LAST_ENTRY(entry1)) {
1165 if (IS_LAST_ENTRY(entry2))
1167 if (entry1->e_hash != entry2->e_hash ||
1168 entry1->e_name_index != entry2->e_name_index ||
1169 entry1->e_name_len != entry2->e_name_len ||
1170 entry1->e_value_size != entry2->e_value_size ||
1171 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1173 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1175 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1176 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1177 le32_to_cpu(entry1->e_value_size)))
1180 entry1 = EXT3_XATTR_NEXT(entry1);
1181 entry2 = EXT3_XATTR_NEXT(entry2);
1183 if (!IS_LAST_ENTRY(entry2))
1189 * ext3_xattr_cache_find()
1191 * Find an identical extended attribute block.
1193 * Returns a pointer to the block found, or NULL if such a block was
1194 * not found or an error occurred.
1196 static struct buffer_head *
1197 ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1198 struct mb_cache_entry **pce)
1200 __u32 hash = le32_to_cpu(header->h_hash);
1201 struct mb_cache_entry *ce;
1203 if (!header->h_hash)
1204 return NULL; /* never share */
1205 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1207 ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
1208 inode->i_sb->s_bdev, hash);
1210 struct buffer_head *bh;
1213 if (PTR_ERR(ce) == -EAGAIN)
1217 bh = sb_bread(inode->i_sb, ce->e_block);
1219 ext3_error(inode->i_sb, __FUNCTION__,
1220 "inode %ld: block %ld read error",
1221 inode->i_ino, (unsigned long) ce->e_block);
1222 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1223 EXT3_XATTR_REFCOUNT_MAX) {
1224 ea_idebug(inode, "block %ld refcount %d>=%d",
1225 (unsigned long) ce->e_block,
1226 le32_to_cpu(BHDR(bh)->h_refcount),
1227 EXT3_XATTR_REFCOUNT_MAX);
1228 } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1233 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1238 #define NAME_HASH_SHIFT 5
1239 #define VALUE_HASH_SHIFT 16
1242 * ext3_xattr_hash_entry()
1244 * Compute the hash of an extended attribute.
1246 static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1247 struct ext3_xattr_entry *entry)
1250 char *name = entry->e_name;
1253 for (n=0; n < entry->e_name_len; n++) {
1254 hash = (hash << NAME_HASH_SHIFT) ^
1255 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1259 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1260 __le32 *value = (__le32 *)((char *)header +
1261 le16_to_cpu(entry->e_value_offs));
1262 for (n = (le32_to_cpu(entry->e_value_size) +
1263 EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1264 hash = (hash << VALUE_HASH_SHIFT) ^
1265 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1266 le32_to_cpu(*value++);
1269 entry->e_hash = cpu_to_le32(hash);
1272 #undef NAME_HASH_SHIFT
1273 #undef VALUE_HASH_SHIFT
1275 #define BLOCK_HASH_SHIFT 16
1278 * ext3_xattr_rehash()
1280 * Re-compute the extended attribute hash value after an entry has changed.
1282 static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1283 struct ext3_xattr_entry *entry)
1285 struct ext3_xattr_entry *here;
1288 ext3_xattr_hash_entry(header, entry);
1289 here = ENTRY(header+1);
1290 while (!IS_LAST_ENTRY(here)) {
1291 if (!here->e_hash) {
1292 /* Block is not shared if an entry's hash value == 0 */
1296 hash = (hash << BLOCK_HASH_SHIFT) ^
1297 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1298 le32_to_cpu(here->e_hash);
1299 here = EXT3_XATTR_NEXT(here);
1301 header->h_hash = cpu_to_le32(hash);
1304 #undef BLOCK_HASH_SHIFT
1307 init_ext3_xattr(void)
1309 ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
1310 sizeof(struct mb_cache_entry) +
1311 sizeof(((struct mb_cache_entry *) 0)->e_indexes[0]), 1, 6);
1312 if (!ext3_xattr_cache)
1318 exit_ext3_xattr(void)
1320 if (ext3_xattr_cache)
1321 mb_cache_destroy(ext3_xattr_cache);
1322 ext3_xattr_cache = NULL;