2 * linux/fs/ext3/xattr.c
8 * Extended attributes for symlinks and special files added per
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
28 * +------------------+
31 * | entry 2 | | growing downwards
36 * | value 3 | | growing upwards
38 * +------------------+
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
46 * 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] = &ext3_xattr_acl_access_handler,
106 [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT] = &ext3_xattr_acl_default_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 &ext3_xattr_acl_access_handler,
119 &ext3_xattr_acl_default_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 * dentry->d_inode->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 = dentry->d_inode;
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 = dentry->d_inode;
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(dentry->d_inode)->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(dentry->d_inode)->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_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, __func__,
652 "inode %lu: bad block "E3FSBLK, 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 error = ext3_journal_get_write_access(handle, bs->bh);
697 if (header(s->base)->h_refcount == cpu_to_le32(1)) {
699 mb_cache_entry_free(ce);
702 ea_bdebug(bs->bh, "modifying in-place");
703 error = ext3_xattr_set_entry(i, s);
705 if (!IS_LAST_ENTRY(s->first))
706 ext3_xattr_rehash(header(s->base),
708 ext3_xattr_cache_insert(bs->bh);
710 unlock_buffer(bs->bh);
714 error = ext3_journal_dirty_metadata(handle,
720 int offset = (char *)s->here - bs->bh->b_data;
722 unlock_buffer(bs->bh);
723 journal_release_buffer(handle, bs->bh);
726 mb_cache_entry_release(ce);
729 ea_bdebug(bs->bh, "cloning");
730 s->base = kmalloc(bs->bh->b_size, GFP_NOFS);
734 memcpy(s->base, BHDR(bs->bh), bs->bh->b_size);
735 s->first = ENTRY(header(s->base)+1);
736 header(s->base)->h_refcount = cpu_to_le32(1);
737 s->here = ENTRY(s->base + offset);
738 s->end = s->base + bs->bh->b_size;
741 /* Allocate a buffer where we construct the new block. */
742 s->base = kzalloc(sb->s_blocksize, GFP_NOFS);
743 /* assert(header == s->base) */
747 header(s->base)->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
748 header(s->base)->h_blocks = cpu_to_le32(1);
749 header(s->base)->h_refcount = cpu_to_le32(1);
750 s->first = ENTRY(header(s->base)+1);
751 s->here = ENTRY(header(s->base)+1);
752 s->end = s->base + sb->s_blocksize;
755 error = ext3_xattr_set_entry(i, s);
760 if (!IS_LAST_ENTRY(s->first))
761 ext3_xattr_rehash(header(s->base), s->here);
764 if (!IS_LAST_ENTRY(s->first)) {
765 new_bh = ext3_xattr_cache_find(inode, header(s->base), &ce);
767 /* We found an identical block in the cache. */
768 if (new_bh == bs->bh)
769 ea_bdebug(new_bh, "keeping");
771 /* The old block is released after updating
773 error = dquot_alloc_block(inode, 1);
776 error = ext3_journal_get_write_access(handle,
781 le32_add_cpu(&BHDR(new_bh)->h_refcount, 1);
782 ea_bdebug(new_bh, "reusing; refcount now=%d",
783 le32_to_cpu(BHDR(new_bh)->h_refcount));
784 unlock_buffer(new_bh);
785 error = ext3_journal_dirty_metadata(handle,
790 mb_cache_entry_release(ce);
792 } else if (bs->bh && s->base == bs->bh->b_data) {
793 /* We were modifying this block in-place. */
794 ea_bdebug(bs->bh, "keeping this block");
798 /* We need to allocate a new block */
799 ext3_fsblk_t goal = ext3_group_first_block_no(sb,
800 EXT3_I(inode)->i_block_group);
804 * Protect us agaist concurrent allocations to the
805 * same inode from ext3_..._writepage(). Reservation
806 * code does not expect racing allocations.
808 mutex_lock(&EXT3_I(inode)->truncate_mutex);
809 block = ext3_new_block(handle, inode, goal, &error);
810 mutex_unlock(&EXT3_I(inode)->truncate_mutex);
813 ea_idebug(inode, "creating block %d", block);
815 new_bh = sb_getblk(sb, block);
818 ext3_free_blocks(handle, inode, block, 1);
823 error = ext3_journal_get_create_access(handle, new_bh);
825 unlock_buffer(new_bh);
828 memcpy(new_bh->b_data, s->base, new_bh->b_size);
829 set_buffer_uptodate(new_bh);
830 unlock_buffer(new_bh);
831 ext3_xattr_cache_insert(new_bh);
832 error = ext3_journal_dirty_metadata(handle, new_bh);
838 /* Update the inode. */
839 EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
841 /* Drop the previous xattr block. */
842 if (bs->bh && bs->bh != new_bh)
843 ext3_xattr_release_block(handle, inode, bs->bh);
848 mb_cache_entry_release(ce);
850 if (!(bs->bh && s->base == bs->bh->b_data))
856 dquot_free_block(inode, 1);
860 ext3_error(inode->i_sb, __func__,
861 "inode %lu: bad block "E3FSBLK, inode->i_ino,
862 EXT3_I(inode)->i_file_acl);
868 struct ext3_xattr_ibody_find {
869 struct ext3_xattr_search s;
870 struct ext3_iloc iloc;
874 ext3_xattr_ibody_find(struct inode *inode, struct ext3_xattr_info *i,
875 struct ext3_xattr_ibody_find *is)
877 struct ext3_xattr_ibody_header *header;
878 struct ext3_inode *raw_inode;
881 if (EXT3_I(inode)->i_extra_isize == 0)
883 raw_inode = ext3_raw_inode(&is->iloc);
884 header = IHDR(inode, raw_inode);
885 is->s.base = is->s.first = IFIRST(header);
886 is->s.here = is->s.first;
887 is->s.end = (void *)raw_inode + EXT3_SB(inode->i_sb)->s_inode_size;
888 if (ext3_test_inode_state(inode, EXT3_STATE_XATTR)) {
889 error = ext3_xattr_check_names(IFIRST(header), is->s.end);
892 /* Find the named attribute. */
893 error = ext3_xattr_find_entry(&is->s.here, i->name_index,
895 (void *)is->s.base, 0);
896 if (error && error != -ENODATA)
898 is->s.not_found = error;
904 ext3_xattr_ibody_set(handle_t *handle, struct inode *inode,
905 struct ext3_xattr_info *i,
906 struct ext3_xattr_ibody_find *is)
908 struct ext3_xattr_ibody_header *header;
909 struct ext3_xattr_search *s = &is->s;
912 if (EXT3_I(inode)->i_extra_isize == 0)
914 error = ext3_xattr_set_entry(i, s);
917 header = IHDR(inode, ext3_raw_inode(&is->iloc));
918 if (!IS_LAST_ENTRY(s->first)) {
919 header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
920 ext3_set_inode_state(inode, EXT3_STATE_XATTR);
922 header->h_magic = cpu_to_le32(0);
923 ext3_clear_inode_state(inode, EXT3_STATE_XATTR);
929 * ext3_xattr_set_handle()
931 * Create, replace or remove an extended attribute for this inode. Value
932 * is NULL to remove an existing extended attribute, and non-NULL to
933 * either replace an existing extended attribute, or create a new extended
934 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
935 * specify that an extended attribute must exist and must not exist
936 * previous to the call, respectively.
938 * Returns 0, or a negative error number on failure.
941 ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
942 const char *name, const void *value, size_t value_len,
945 struct ext3_xattr_info i = {
946 .name_index = name_index,
949 .value_len = value_len,
952 struct ext3_xattr_ibody_find is = {
953 .s = { .not_found = -ENODATA, },
955 struct ext3_xattr_block_find bs = {
956 .s = { .not_found = -ENODATA, },
962 if (strlen(name) > 255)
964 down_write(&EXT3_I(inode)->xattr_sem);
965 error = ext3_get_inode_loc(inode, &is.iloc);
969 error = ext3_journal_get_write_access(handle, is.iloc.bh);
973 if (ext3_test_inode_state(inode, EXT3_STATE_NEW)) {
974 struct ext3_inode *raw_inode = ext3_raw_inode(&is.iloc);
975 memset(raw_inode, 0, EXT3_SB(inode->i_sb)->s_inode_size);
976 ext3_clear_inode_state(inode, EXT3_STATE_NEW);
979 error = ext3_xattr_ibody_find(inode, &i, &is);
983 error = ext3_xattr_block_find(inode, &i, &bs);
986 if (is.s.not_found && bs.s.not_found) {
988 if (flags & XATTR_REPLACE)
995 if (flags & XATTR_CREATE)
1000 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1001 else if (!bs.s.not_found)
1002 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1004 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1005 if (!error && !bs.s.not_found) {
1007 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1008 } else if (error == -ENOSPC) {
1009 if (EXT3_I(inode)->i_file_acl && !bs.s.base) {
1010 error = ext3_xattr_block_find(inode, &i, &bs);
1014 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1017 if (!is.s.not_found) {
1019 error = ext3_xattr_ibody_set(handle, inode, &i,
1025 ext3_xattr_update_super_block(handle, inode->i_sb);
1026 inode->i_ctime = CURRENT_TIME_SEC;
1027 error = ext3_mark_iloc_dirty(handle, inode, &is.iloc);
1029 * The bh is consumed by ext3_mark_iloc_dirty, even with
1040 up_write(&EXT3_I(inode)->xattr_sem);
1047 * Like ext3_xattr_set_handle, but start from an inode. This extended
1048 * attribute modification is a filesystem transaction by itself.
1050 * Returns 0, or a negative error number on failure.
1053 ext3_xattr_set(struct inode *inode, int name_index, const char *name,
1054 const void *value, size_t value_len, int flags)
1057 int error, retries = 0;
1060 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS(inode->i_sb));
1061 if (IS_ERR(handle)) {
1062 error = PTR_ERR(handle);
1066 error = ext3_xattr_set_handle(handle, inode, name_index, name,
1067 value, value_len, flags);
1068 error2 = ext3_journal_stop(handle);
1069 if (error == -ENOSPC &&
1070 ext3_should_retry_alloc(inode->i_sb, &retries))
1080 * ext3_xattr_delete_inode()
1082 * Free extended attribute resources associated with this inode. This
1083 * is called immediately before an inode is freed. We have exclusive
1084 * access to the inode.
1087 ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
1089 struct buffer_head *bh = NULL;
1091 if (!EXT3_I(inode)->i_file_acl)
1093 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
1095 ext3_error(inode->i_sb, __func__,
1096 "inode %lu: block "E3FSBLK" read error", inode->i_ino,
1097 EXT3_I(inode)->i_file_acl);
1100 if (BHDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
1101 BHDR(bh)->h_blocks != cpu_to_le32(1)) {
1102 ext3_error(inode->i_sb, __func__,
1103 "inode %lu: bad block "E3FSBLK, inode->i_ino,
1104 EXT3_I(inode)->i_file_acl);
1107 ext3_xattr_release_block(handle, inode, bh);
1108 EXT3_I(inode)->i_file_acl = 0;
1115 * ext3_xattr_put_super()
1117 * This is called when a file system is unmounted.
1120 ext3_xattr_put_super(struct super_block *sb)
1122 mb_cache_shrink(sb->s_bdev);
1126 * ext3_xattr_cache_insert()
1128 * Create a new entry in the extended attribute cache, and insert
1129 * it unless such an entry is already in the cache.
1131 * Returns 0, or a negative error number on failure.
1134 ext3_xattr_cache_insert(struct buffer_head *bh)
1136 __u32 hash = le32_to_cpu(BHDR(bh)->h_hash);
1137 struct mb_cache_entry *ce;
1140 ce = mb_cache_entry_alloc(ext3_xattr_cache, GFP_NOFS);
1142 ea_bdebug(bh, "out of memory");
1145 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
1147 mb_cache_entry_free(ce);
1148 if (error == -EBUSY) {
1149 ea_bdebug(bh, "already in cache");
1153 ea_bdebug(bh, "inserting [%x]", (int)hash);
1154 mb_cache_entry_release(ce);
1161 * Compare two extended attribute blocks for equality.
1163 * Returns 0 if the blocks are equal, 1 if they differ, and
1164 * a negative error number on errors.
1167 ext3_xattr_cmp(struct ext3_xattr_header *header1,
1168 struct ext3_xattr_header *header2)
1170 struct ext3_xattr_entry *entry1, *entry2;
1172 entry1 = ENTRY(header1+1);
1173 entry2 = ENTRY(header2+1);
1174 while (!IS_LAST_ENTRY(entry1)) {
1175 if (IS_LAST_ENTRY(entry2))
1177 if (entry1->e_hash != entry2->e_hash ||
1178 entry1->e_name_index != entry2->e_name_index ||
1179 entry1->e_name_len != entry2->e_name_len ||
1180 entry1->e_value_size != entry2->e_value_size ||
1181 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1183 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1185 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1186 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1187 le32_to_cpu(entry1->e_value_size)))
1190 entry1 = EXT3_XATTR_NEXT(entry1);
1191 entry2 = EXT3_XATTR_NEXT(entry2);
1193 if (!IS_LAST_ENTRY(entry2))
1199 * ext3_xattr_cache_find()
1201 * Find an identical extended attribute block.
1203 * Returns a pointer to the block found, or NULL if such a block was
1204 * not found or an error occurred.
1206 static struct buffer_head *
1207 ext3_xattr_cache_find(struct inode *inode, struct ext3_xattr_header *header,
1208 struct mb_cache_entry **pce)
1210 __u32 hash = le32_to_cpu(header->h_hash);
1211 struct mb_cache_entry *ce;
1213 if (!header->h_hash)
1214 return NULL; /* never share */
1215 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1217 ce = mb_cache_entry_find_first(ext3_xattr_cache, inode->i_sb->s_bdev,
1220 struct buffer_head *bh;
1223 if (PTR_ERR(ce) == -EAGAIN)
1227 bh = sb_bread(inode->i_sb, ce->e_block);
1229 ext3_error(inode->i_sb, __func__,
1230 "inode %lu: block %lu read error",
1231 inode->i_ino, (unsigned long) ce->e_block);
1232 } else if (le32_to_cpu(BHDR(bh)->h_refcount) >=
1233 EXT3_XATTR_REFCOUNT_MAX) {
1234 ea_idebug(inode, "block %lu refcount %d>=%d",
1235 (unsigned long) ce->e_block,
1236 le32_to_cpu(BHDR(bh)->h_refcount),
1237 EXT3_XATTR_REFCOUNT_MAX);
1238 } else if (ext3_xattr_cmp(header, BHDR(bh)) == 0) {
1243 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
1248 #define NAME_HASH_SHIFT 5
1249 #define VALUE_HASH_SHIFT 16
1252 * ext3_xattr_hash_entry()
1254 * Compute the hash of an extended attribute.
1256 static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1257 struct ext3_xattr_entry *entry)
1260 char *name = entry->e_name;
1263 for (n=0; n < entry->e_name_len; n++) {
1264 hash = (hash << NAME_HASH_SHIFT) ^
1265 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1269 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1270 __le32 *value = (__le32 *)((char *)header +
1271 le16_to_cpu(entry->e_value_offs));
1272 for (n = (le32_to_cpu(entry->e_value_size) +
1273 EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1274 hash = (hash << VALUE_HASH_SHIFT) ^
1275 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1276 le32_to_cpu(*value++);
1279 entry->e_hash = cpu_to_le32(hash);
1282 #undef NAME_HASH_SHIFT
1283 #undef VALUE_HASH_SHIFT
1285 #define BLOCK_HASH_SHIFT 16
1288 * ext3_xattr_rehash()
1290 * Re-compute the extended attribute hash value after an entry has changed.
1292 static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1293 struct ext3_xattr_entry *entry)
1295 struct ext3_xattr_entry *here;
1298 ext3_xattr_hash_entry(header, entry);
1299 here = ENTRY(header+1);
1300 while (!IS_LAST_ENTRY(here)) {
1301 if (!here->e_hash) {
1302 /* Block is not shared if an entry's hash value == 0 */
1306 hash = (hash << BLOCK_HASH_SHIFT) ^
1307 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1308 le32_to_cpu(here->e_hash);
1309 here = EXT3_XATTR_NEXT(here);
1311 header->h_hash = cpu_to_le32(hash);
1314 #undef BLOCK_HASH_SHIFT
1317 init_ext3_xattr(void)
1319 ext3_xattr_cache = mb_cache_create("ext3_xattr", 6);
1320 if (!ext3_xattr_cache)
1326 exit_ext3_xattr(void)
1328 if (ext3_xattr_cache)
1329 mb_cache_destroy(ext3_xattr_cache);
1330 ext3_xattr_cache = NULL;