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>.
13 * Extended attributes are stored on disk blocks allocated outside of
14 * any inode. The i_file_acl field is then made to point to this allocated
15 * block. If all extended attributes of an inode are identical, these
16 * inodes may share the same extended attribute block. Such situations
17 * are automatically detected by keeping a cache of recent attribute block
18 * numbers and hashes over the block's contents in memory.
21 * Extended attribute block layout:
23 * +------------------+
26 * | entry 2 | | growing downwards
31 * | value 3 | | growing upwards
33 * +------------------+
35 * The block header is followed by multiple entry descriptors. These entry
36 * descriptors are variable in size, and alligned to EXT3_XATTR_PAD
37 * byte boundaries. The entry descriptors are sorted by attribute name,
38 * so that two extended attribute blocks can be compared efficiently.
40 * Attribute values are aligned to the end of the block, stored in
41 * no specific order. They are also padded to EXT3_XATTR_PAD byte
42 * boundaries. No additional gaps are left between them.
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 will change. Multiple writers to an EA block are synchronized
50 * by the bh lock. No more than a single bh lock is held at any time
54 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/ext3_jbd.h>
58 #include <linux/ext3_fs.h>
59 #include <linux/mbcache.h>
60 #include <linux/quotaops.h>
61 #include <linux/rwsem.h>
65 #define HDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
66 #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
67 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
68 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
70 #ifdef EXT3_XATTR_DEBUG
71 # define ea_idebug(inode, f...) do { \
72 printk(KERN_DEBUG "inode %s:%ld: ", \
73 inode->i_sb->s_id, inode->i_ino); \
77 # define ea_bdebug(bh, f...) do { \
78 char b[BDEVNAME_SIZE]; \
79 printk(KERN_DEBUG "block %s:%lu: ", \
80 bdevname(bh->b_bdev, b), \
81 (unsigned long) bh->b_blocknr); \
86 # define ea_idebug(f...)
87 # define ea_bdebug(f...)
90 static int ext3_xattr_set_handle2(handle_t *, struct inode *,
92 struct ext3_xattr_header *);
94 static int ext3_xattr_cache_insert(struct buffer_head *);
95 static struct buffer_head *ext3_xattr_cache_find(handle_t *, struct inode *,
96 struct ext3_xattr_header *,
98 static void ext3_xattr_cache_remove(struct buffer_head *);
99 static void ext3_xattr_rehash(struct ext3_xattr_header *,
100 struct ext3_xattr_entry *);
102 static struct mb_cache *ext3_xattr_cache;
103 static struct ext3_xattr_handler *ext3_xattr_handlers[EXT3_XATTR_INDEX_MAX];
104 static rwlock_t ext3_handler_lock = RW_LOCK_UNLOCKED;
107 ext3_xattr_register(int name_index, struct ext3_xattr_handler *handler)
111 if (name_index > 0 && name_index <= EXT3_XATTR_INDEX_MAX) {
112 write_lock(&ext3_handler_lock);
113 if (!ext3_xattr_handlers[name_index-1]) {
114 ext3_xattr_handlers[name_index-1] = handler;
117 write_unlock(&ext3_handler_lock);
123 ext3_xattr_unregister(int name_index, struct ext3_xattr_handler *handler)
125 if (name_index > 0 || name_index <= EXT3_XATTR_INDEX_MAX) {
126 write_lock(&ext3_handler_lock);
127 ext3_xattr_handlers[name_index-1] = NULL;
128 write_unlock(&ext3_handler_lock);
132 static inline const char *
133 strcmp_prefix(const char *a, const char *a_prefix)
135 while (*a_prefix && *a == *a_prefix) {
139 return *a_prefix ? NULL : a;
143 * Decode the extended attribute name, and translate it into
144 * the name_index and name suffix.
146 static inline struct ext3_xattr_handler *
147 ext3_xattr_resolve_name(const char **name)
149 struct ext3_xattr_handler *handler = NULL;
154 read_lock(&ext3_handler_lock);
155 for (i=0; i<EXT3_XATTR_INDEX_MAX; i++) {
156 if (ext3_xattr_handlers[i]) {
157 const char *n = strcmp_prefix(*name,
158 ext3_xattr_handlers[i]->prefix);
160 handler = ext3_xattr_handlers[i];
166 read_unlock(&ext3_handler_lock);
170 static inline struct ext3_xattr_handler *
171 ext3_xattr_handler(int name_index)
173 struct ext3_xattr_handler *handler = NULL;
174 if (name_index > 0 && name_index <= EXT3_XATTR_INDEX_MAX) {
175 read_lock(&ext3_handler_lock);
176 handler = ext3_xattr_handlers[name_index-1];
177 read_unlock(&ext3_handler_lock);
183 * Inode operation getxattr()
185 * dentry->d_inode->i_sem: don't care
188 ext3_getxattr(struct dentry *dentry, const char *name,
189 void *buffer, size_t size)
191 struct ext3_xattr_handler *handler;
192 struct inode *inode = dentry->d_inode;
194 handler = ext3_xattr_resolve_name(&name);
197 return handler->get(inode, name, buffer, size);
201 * Inode operation listxattr()
203 * dentry->d_inode->i_sem: don't care
206 ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
208 return ext3_xattr_list(dentry->d_inode, buffer, size);
212 * Inode operation setxattr()
214 * dentry->d_inode->i_sem: down
217 ext3_setxattr(struct dentry *dentry, const char *name,
218 const void *value, size_t size, int flags)
220 struct ext3_xattr_handler *handler;
221 struct inode *inode = dentry->d_inode;
224 value = ""; /* empty EA, do not remove */
225 handler = ext3_xattr_resolve_name(&name);
228 return handler->set(inode, name, value, size, flags);
232 * Inode operation removexattr()
234 * dentry->d_inode->i_sem: down
237 ext3_removexattr(struct dentry *dentry, const char *name)
239 struct ext3_xattr_handler *handler;
240 struct inode *inode = dentry->d_inode;
242 handler = ext3_xattr_resolve_name(&name);
245 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
251 * Copy an extended attribute into the buffer
252 * provided, or compute the buffer size required.
253 * Buffer is NULL to compute the size of the buffer required.
255 * Returns a negative error number on failure, or the number of bytes
256 * used / required on success.
259 ext3_xattr_get(struct inode *inode, int name_index, const char *name,
260 void *buffer, size_t buffer_size)
262 struct buffer_head *bh = NULL;
263 struct ext3_xattr_entry *entry;
264 size_t name_len, size;
268 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
269 name_index, name, buffer, (long)buffer_size);
273 down_read(&EXT3_I(inode)->xattr_sem);
275 if (!EXT3_I(inode)->i_file_acl)
277 ea_idebug(inode, "reading block %d", EXT3_I(inode)->i_file_acl);
278 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
282 ea_bdebug(bh, "b_count=%d, refcount=%d",
283 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
284 end = bh->b_data + bh->b_size;
285 if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
286 HDR(bh)->h_blocks != cpu_to_le32(1)) {
287 bad_block: ext3_error(inode->i_sb, "ext3_xattr_get",
288 "inode %ld: bad block %d", inode->i_ino,
289 EXT3_I(inode)->i_file_acl);
293 /* find named attribute */
294 name_len = strlen(name);
299 entry = FIRST_ENTRY(bh);
300 while (!IS_LAST_ENTRY(entry)) {
301 struct ext3_xattr_entry *next =
302 EXT3_XATTR_NEXT(entry);
303 if ((char *)next >= end)
305 if (name_index == entry->e_name_index &&
306 name_len == entry->e_name_len &&
307 memcmp(name, entry->e_name, name_len) == 0)
311 /* Check the remaining name entries */
312 while (!IS_LAST_ENTRY(entry)) {
313 struct ext3_xattr_entry *next =
314 EXT3_XATTR_NEXT(entry);
315 if ((char *)next >= end)
319 if (ext3_xattr_cache_insert(bh))
320 ea_idebug(inode, "cache insert failed");
324 /* check the buffer size */
325 if (entry->e_value_block != 0)
327 size = le32_to_cpu(entry->e_value_size);
328 if (size > inode->i_sb->s_blocksize ||
329 le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
332 if (ext3_xattr_cache_insert(bh))
333 ea_idebug(inode, "cache insert failed");
336 if (size > buffer_size)
338 /* return value of attribute */
339 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
346 up_read(&EXT3_I(inode)->xattr_sem);
354 * Copy a list of attribute names into the buffer
355 * provided, or compute the buffer size required.
356 * Buffer is NULL to compute the size of the buffer required.
358 * Returns a negative error number on failure, or the number of bytes
359 * used / required on success.
362 ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
364 struct buffer_head *bh = NULL;
365 struct ext3_xattr_entry *entry;
370 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
371 buffer, (long)buffer_size);
373 down_read(&EXT3_I(inode)->xattr_sem);
375 if (!EXT3_I(inode)->i_file_acl)
377 ea_idebug(inode, "reading block %d", EXT3_I(inode)->i_file_acl);
378 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
382 ea_bdebug(bh, "b_count=%d, refcount=%d",
383 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
384 end = bh->b_data + bh->b_size;
385 if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
386 HDR(bh)->h_blocks != cpu_to_le32(1)) {
387 bad_block: ext3_error(inode->i_sb, "ext3_xattr_list",
388 "inode %ld: bad block %d", inode->i_ino,
389 EXT3_I(inode)->i_file_acl);
393 /* compute the size required for the list of attribute names */
394 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
395 entry = EXT3_XATTR_NEXT(entry)) {
396 struct ext3_xattr_handler *handler;
397 struct ext3_xattr_entry *next =
398 EXT3_XATTR_NEXT(entry);
399 if ((char *)next >= end)
402 handler = ext3_xattr_handler(entry->e_name_index);
404 size += handler->list(NULL, inode, entry->e_name,
408 if (ext3_xattr_cache_insert(bh))
409 ea_idebug(inode, "cache insert failed");
415 if (size > buffer_size)
419 /* list the attribute names */
421 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
422 entry = EXT3_XATTR_NEXT(entry)) {
423 struct ext3_xattr_handler *handler;
425 handler = ext3_xattr_handler(entry->e_name_index);
427 buf += handler->list(buf, inode, entry->e_name,
434 up_read(&EXT3_I(inode)->xattr_sem);
440 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
443 static void ext3_xattr_update_super_block(handle_t *handle,
444 struct super_block *sb)
446 if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
450 if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
451 EXT3_SB(sb)->s_es->s_feature_compat |=
452 cpu_to_le32(EXT3_FEATURE_COMPAT_EXT_ATTR);
454 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
460 * ext3_xattr_set_handle()
462 * Create, replace or remove an extended attribute for this inode. Buffer
463 * is NULL to remove an existing extended attribute, and non-NULL to
464 * either replace an existing extended attribute, or create a new extended
465 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
466 * specify that an extended attribute must exist and must not exist
467 * previous to the call, respectively.
469 * Returns 0, or a negative error number on failure.
472 ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
473 const char *name, const void *value, size_t value_len,
476 struct super_block *sb = inode->i_sb;
477 struct buffer_head *bh = NULL;
478 struct ext3_xattr_header *header = NULL;
479 struct ext3_xattr_entry *here, *last;
480 size_t name_len, free, min_offs = sb->s_blocksize;
481 int not_found = 1, error;
485 * header -- Points either into bh, or to a temporarily
487 * here -- The named entry found, or the place for inserting, within
488 * the block pointed to by header.
489 * last -- Points right after the last named entry within the block
490 * pointed to by header.
491 * min_offs -- The offset of the first value (values are aligned
492 * towards the end of the block).
493 * end -- Points right after the block pointed to by header.
496 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
497 name_index, name, value, (long)value_len);
499 if (IS_RDONLY(inode))
501 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
507 name_len = strlen(name);
508 if (name_len > 255 || value_len > sb->s_blocksize)
510 down_write(&EXT3_I(inode)->xattr_sem);
511 if (EXT3_I(inode)->i_file_acl) {
512 /* The inode already has an extended attribute block. */
513 bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
517 ea_bdebug(bh, "b_count=%d, refcount=%d",
518 atomic_read(&(bh->b_count)),
519 le32_to_cpu(HDR(bh)->h_refcount));
521 end = bh->b_data + bh->b_size;
522 if (header->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
523 header->h_blocks != cpu_to_le32(1)) {
524 bad_block: ext3_error(sb, "ext3_xattr_set",
525 "inode %ld: bad block %d", inode->i_ino,
526 EXT3_I(inode)->i_file_acl);
530 /* Find the named attribute. */
531 here = FIRST_ENTRY(bh);
532 while (!IS_LAST_ENTRY(here)) {
533 struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(here);
534 if ((char *)next >= end)
536 if (!here->e_value_block && here->e_value_size) {
537 size_t offs = le16_to_cpu(here->e_value_offs);
541 not_found = name_index - here->e_name_index;
543 not_found = name_len - here->e_name_len;
545 not_found = memcmp(name, here->e_name,name_len);
551 /* We still need to compute min_offs and last. */
552 while (!IS_LAST_ENTRY(last)) {
553 struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(last);
554 if ((char *)next >= end)
556 if (!last->e_value_block && last->e_value_size) {
557 size_t offs = le16_to_cpu(last->e_value_offs);
564 /* Check whether we have enough space left. */
565 free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
567 /* We will use a new extended attribute block. */
568 free = sb->s_blocksize -
569 sizeof(struct ext3_xattr_header) - sizeof(__u32);
570 here = last = NULL; /* avoid gcc uninitialized warning. */
574 /* Request to remove a nonexistent attribute? */
576 if (flags & XATTR_REPLACE)
582 /* Request to create an existing attribute? */
584 if (flags & XATTR_CREATE)
586 if (!here->e_value_block && here->e_value_size) {
587 size_t size = le32_to_cpu(here->e_value_size);
589 if (le16_to_cpu(here->e_value_offs) + size >
590 sb->s_blocksize || size > sb->s_blocksize)
592 free += EXT3_XATTR_SIZE(size);
594 free += EXT3_XATTR_LEN(name_len);
597 if (free < EXT3_XATTR_LEN(name_len) + EXT3_XATTR_SIZE(value_len))
600 /* Here we know that we can set the new attribute. */
605 /* assert(header == HDR(bh)); */
606 if (header->h_refcount != cpu_to_le32(1))
607 goto skip_get_write_access;
608 /* ext3_journal_get_write_access() requires an unlocked bh,
609 which complicates things here. */
610 error = ext3_journal_get_write_access_credits(handle, bh,
615 if (header->h_refcount == cpu_to_le32(1)) {
616 ea_bdebug(bh, "modifying in-place");
617 ext3_xattr_cache_remove(bh);
618 /* keep the buffer locked while modifying it. */
623 journal_release_buffer(handle, bh, credits);
624 skip_get_write_access:
625 ea_bdebug(bh, "cloning");
626 header = kmalloc(bh->b_size, GFP_KERNEL);
630 memcpy(header, HDR(bh), bh->b_size);
631 header->h_refcount = cpu_to_le32(1);
632 offset = (char *)here - bh->b_data;
633 here = ENTRY((char *)header + offset);
634 offset = (char *)last - bh->b_data;
635 last = ENTRY((char *)header + offset);
638 /* Allocate a buffer where we construct the new block. */
639 header = kmalloc(sb->s_blocksize, GFP_KERNEL);
643 memset(header, 0, sb->s_blocksize);
644 end = (char *)header + sb->s_blocksize;
645 header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
646 header->h_blocks = header->h_refcount = cpu_to_le32(1);
647 last = here = ENTRY(header+1);
650 /* Iff we are modifying the block in-place, bh is locked here. */
653 /* Insert the new name. */
654 size_t size = EXT3_XATTR_LEN(name_len);
655 size_t rest = (char *)last - (char *)here;
656 memmove((char *)here + size, here, rest);
657 memset(here, 0, size);
658 here->e_name_index = name_index;
659 here->e_name_len = name_len;
660 memcpy(here->e_name, name, name_len);
662 if (!here->e_value_block && here->e_value_size) {
663 char *first_val = (char *)header + min_offs;
664 size_t offs = le16_to_cpu(here->e_value_offs);
665 char *val = (char *)header + offs;
666 size_t size = EXT3_XATTR_SIZE(
667 le32_to_cpu(here->e_value_size));
669 if (size == EXT3_XATTR_SIZE(value_len)) {
670 /* The old and the new value have the same
671 size. Just replace. */
672 here->e_value_size = cpu_to_le32(value_len);
673 memset(val + size - EXT3_XATTR_PAD, 0,
674 EXT3_XATTR_PAD); /* Clear pad bytes. */
675 memcpy(val, value, value_len);
679 /* Remove the old value. */
680 memmove(first_val + size, first_val, val - first_val);
681 memset(first_val, 0, size);
682 here->e_value_offs = 0;
685 /* Adjust all value offsets. */
686 last = ENTRY(header+1);
687 while (!IS_LAST_ENTRY(last)) {
688 size_t o = le16_to_cpu(last->e_value_offs);
689 if (!last->e_value_block && o < offs)
691 cpu_to_le16(o + size);
692 last = EXT3_XATTR_NEXT(last);
696 /* Remove the old name. */
697 size_t size = EXT3_XATTR_LEN(name_len);
698 last = ENTRY((char *)last - size);
699 memmove(here, (char*)here + size,
700 (char*)last - (char*)here);
701 memset(last, 0, size);
706 /* Insert the new value. */
707 here->e_value_size = cpu_to_le32(value_len);
709 size_t size = EXT3_XATTR_SIZE(value_len);
710 char *val = (char *)header + min_offs - size;
712 cpu_to_le16((char *)val - (char *)header);
713 memset(val + size - EXT3_XATTR_PAD, 0,
714 EXT3_XATTR_PAD); /* Clear the pad bytes. */
715 memcpy(val, value, value_len);
720 if (IS_LAST_ENTRY(ENTRY(header+1))) {
721 /* This block is now empty. */
722 if (bh && header == HDR(bh))
723 unlock_buffer(bh); /* we were modifying in-place. */
724 error = ext3_xattr_set_handle2(handle, inode, bh, NULL);
726 ext3_xattr_rehash(header, here);
727 if (bh && header == HDR(bh))
728 unlock_buffer(bh); /* we were modifying in-place. */
729 error = ext3_xattr_set_handle2(handle, inode, bh, header);
734 if (!(bh && header == HDR(bh)))
736 up_write(&EXT3_I(inode)->xattr_sem);
742 * Second half of ext3_xattr_set_handle(): Update the file system.
745 ext3_xattr_set_handle2(handle_t *handle, struct inode *inode,
746 struct buffer_head *old_bh,
747 struct ext3_xattr_header *header)
749 struct super_block *sb = inode->i_sb;
750 struct buffer_head *new_bh = NULL;
751 int credits = 0, error;
754 new_bh = ext3_xattr_cache_find(handle, inode, header, &credits);
756 /* We found an identical block in the cache. */
757 if (new_bh == old_bh)
758 ea_bdebug(new_bh, "keeping this block");
760 /* The old block is released after updating
762 ea_bdebug(new_bh, "reusing block");
765 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
766 unlock_buffer(new_bh);
767 journal_release_buffer(handle, new_bh,
771 HDR(new_bh)->h_refcount = cpu_to_le32(1 +
772 le32_to_cpu(HDR(new_bh)->h_refcount));
773 ea_bdebug(new_bh, "refcount now=%d",
774 le32_to_cpu(HDR(new_bh)->h_refcount));
776 unlock_buffer(new_bh);
777 } else if (old_bh && header == HDR(old_bh)) {
778 /* Keep this block. No need to lock the block as we
779 * don't need to change the reference count. */
782 ext3_xattr_cache_insert(new_bh);
784 /* We need to allocate a new block */
785 int goal = le32_to_cpu(
786 EXT3_SB(sb)->s_es->s_first_data_block) +
787 EXT3_I(inode)->i_block_group *
788 EXT3_BLOCKS_PER_GROUP(sb);
789 int block = ext3_new_block(handle, inode, goal,
793 ea_idebug(inode, "creating block %d", block);
795 new_bh = sb_getblk(sb, block);
798 ext3_free_blocks(handle, inode, block, 1);
803 error = ext3_journal_get_create_access(handle, new_bh);
805 unlock_buffer(new_bh);
808 memcpy(new_bh->b_data, header, new_bh->b_size);
809 set_buffer_uptodate(new_bh);
810 unlock_buffer(new_bh);
811 ext3_xattr_cache_insert(new_bh);
813 ext3_xattr_update_super_block(handle, sb);
815 error = ext3_journal_dirty_metadata(handle, new_bh);
820 /* Update the inode. */
821 EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
822 inode->i_ctime = CURRENT_TIME;
823 ext3_mark_inode_dirty(handle, inode);
828 if (old_bh && old_bh != new_bh) {
830 * If there was an old block, and we are no longer using it,
831 * release the old block.
833 error = ext3_journal_get_write_access(handle, old_bh);
837 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
838 /* Free the old block. */
839 ea_bdebug(old_bh, "freeing");
840 ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1);
842 /* ext3_forget() calls bforget() for us, but we
843 let our caller release old_bh, so we need to
844 duplicate the handle before. */
846 ext3_forget(handle, 1, inode, old_bh,old_bh->b_blocknr);
848 /* Decrement the refcount only. */
849 HDR(old_bh)->h_refcount = cpu_to_le32(
850 le32_to_cpu(HDR(old_bh)->h_refcount) - 1);
851 DQUOT_FREE_BLOCK(inode, 1);
852 ext3_journal_dirty_metadata(handle, old_bh);
853 ea_bdebug(old_bh, "refcount now=%d",
854 le32_to_cpu(HDR(old_bh)->h_refcount));
856 unlock_buffer(old_bh);
868 * Like ext3_xattr_set_handle, but start from an inode. This extended
869 * attribute modification is a filesystem transaction by itself.
871 * Returns 0, or a negative error number on failure.
874 ext3_xattr_set(struct inode *inode, int name_index, const char *name,
875 const void *value, size_t value_len, int flags)
878 int error, retries = 0;
881 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS);
882 if (IS_ERR(handle)) {
883 error = PTR_ERR(handle);
887 error = ext3_xattr_set_handle(handle, inode, name_index, name,
888 value, value_len, flags);
889 error2 = ext3_journal_stop(handle);
890 if (error == -ENOSPC &&
891 ext3_should_retry_alloc(inode->i_sb, &retries))
901 * ext3_xattr_delete_inode()
903 * Free extended attribute resources associated with this inode. This
904 * is called immediately before an inode is freed.
907 ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
909 struct buffer_head *bh = NULL;
911 down_write(&EXT3_I(inode)->xattr_sem);
912 if (!EXT3_I(inode)->i_file_acl)
914 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
916 ext3_error(inode->i_sb, "ext3_xattr_delete_inode",
917 "inode %ld: block %d read error", inode->i_ino,
918 EXT3_I(inode)->i_file_acl);
921 if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
922 HDR(bh)->h_blocks != cpu_to_le32(1)) {
923 ext3_error(inode->i_sb, "ext3_xattr_delete_inode",
924 "inode %ld: bad block %d", inode->i_ino,
925 EXT3_I(inode)->i_file_acl);
928 if (ext3_journal_get_write_access(handle, bh) != 0)
931 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
932 ext3_xattr_cache_remove(bh);
933 ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1);
935 ext3_forget(handle, 1, inode, bh, EXT3_I(inode)->i_file_acl);
937 HDR(bh)->h_refcount = cpu_to_le32(
938 le32_to_cpu(HDR(bh)->h_refcount) - 1);
939 ext3_journal_dirty_metadata(handle, bh);
942 DQUOT_FREE_BLOCK(inode, 1);
944 ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
946 EXT3_I(inode)->i_file_acl = 0;
950 up_write(&EXT3_I(inode)->xattr_sem);
954 * ext3_xattr_put_super()
956 * This is called when a file system is unmounted.
959 ext3_xattr_put_super(struct super_block *sb)
961 mb_cache_shrink(ext3_xattr_cache, sb->s_bdev);
965 * ext3_xattr_cache_insert()
967 * Create a new entry in the extended attribute cache, and insert
968 * it unless such an entry is already in the cache.
970 * Returns 0, or a negative error number on failure.
973 ext3_xattr_cache_insert(struct buffer_head *bh)
975 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
976 struct mb_cache_entry *ce;
979 ce = mb_cache_entry_alloc(ext3_xattr_cache);
982 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
984 mb_cache_entry_free(ce);
985 if (error == -EBUSY) {
986 ea_bdebug(bh, "already in cache (%d cache entries)",
987 atomic_read(&ext3_xattr_cache->c_entry_count));
991 ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
992 atomic_read(&ext3_xattr_cache->c_entry_count));
993 mb_cache_entry_release(ce);
1001 * Compare two extended attribute blocks for equality.
1003 * Returns 0 if the blocks are equal, 1 if they differ, and
1004 * a negative error number on errors.
1007 ext3_xattr_cmp(struct ext3_xattr_header *header1,
1008 struct ext3_xattr_header *header2)
1010 struct ext3_xattr_entry *entry1, *entry2;
1012 entry1 = ENTRY(header1+1);
1013 entry2 = ENTRY(header2+1);
1014 while (!IS_LAST_ENTRY(entry1)) {
1015 if (IS_LAST_ENTRY(entry2))
1017 if (entry1->e_hash != entry2->e_hash ||
1018 entry1->e_name_len != entry2->e_name_len ||
1019 entry1->e_value_size != entry2->e_value_size ||
1020 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1022 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1024 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1025 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1026 le32_to_cpu(entry1->e_value_size)))
1029 entry1 = EXT3_XATTR_NEXT(entry1);
1030 entry2 = EXT3_XATTR_NEXT(entry2);
1032 if (!IS_LAST_ENTRY(entry2))
1038 * ext3_xattr_cache_find()
1040 * Find an identical extended attribute block.
1042 * Returns a pointer to the block found, or NULL if such a block was
1043 * not found or an error occurred.
1045 static struct buffer_head *
1046 ext3_xattr_cache_find(handle_t *handle, struct inode *inode,
1047 struct ext3_xattr_header *header, int *credits)
1049 __u32 hash = le32_to_cpu(header->h_hash);
1050 struct mb_cache_entry *ce;
1052 if (!header->h_hash)
1053 return NULL; /* never share */
1054 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1055 ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
1056 inode->i_sb->s_bdev, hash);
1058 struct buffer_head *bh = sb_bread(inode->i_sb, ce->e_block);
1061 ext3_error(inode->i_sb, "ext3_xattr_cache_find",
1062 "inode %ld: block %ld read error",
1063 inode->i_ino, (unsigned long) ce->e_block);
1064 } else if (ext3_journal_get_write_access_credits(
1065 handle, bh, credits) == 0) {
1066 /* ext3_journal_get_write_access() requires an unlocked
1067 * bh, which complicates things here. */
1069 if (le32_to_cpu(HDR(bh)->h_refcount) >
1070 EXT3_XATTR_REFCOUNT_MAX) {
1071 ea_idebug(inode, "block %ld refcount %d>%d",
1072 (unsigned long) ce->e_block,
1073 le32_to_cpu(HDR(bh)->h_refcount),
1074 EXT3_XATTR_REFCOUNT_MAX);
1075 } else if (!ext3_xattr_cmp(header, HDR(bh))) {
1076 mb_cache_entry_release(ce);
1077 /* buffer will be unlocked by caller */
1081 journal_release_buffer(handle, bh, *credits);
1085 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1091 * ext3_xattr_cache_remove()
1093 * Remove the cache entry of a block from the cache. Called when a
1094 * block becomes invalid.
1097 ext3_xattr_cache_remove(struct buffer_head *bh)
1099 struct mb_cache_entry *ce;
1101 ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev,
1104 ea_bdebug(bh, "removing (%d cache entries remaining)",
1105 atomic_read(&ext3_xattr_cache->c_entry_count)-1);
1106 mb_cache_entry_free(ce);
1108 ea_bdebug(bh, "no cache entry");
1111 #define NAME_HASH_SHIFT 5
1112 #define VALUE_HASH_SHIFT 16
1115 * ext3_xattr_hash_entry()
1117 * Compute the hash of an extended attribute.
1119 static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1120 struct ext3_xattr_entry *entry)
1123 char *name = entry->e_name;
1126 for (n=0; n < entry->e_name_len; n++) {
1127 hash = (hash << NAME_HASH_SHIFT) ^
1128 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1132 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1133 __u32 *value = (__u32 *)((char *)header +
1134 le16_to_cpu(entry->e_value_offs));
1135 for (n = (le32_to_cpu(entry->e_value_size) +
1136 EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1137 hash = (hash << VALUE_HASH_SHIFT) ^
1138 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1139 le32_to_cpu(*value++);
1142 entry->e_hash = cpu_to_le32(hash);
1145 #undef NAME_HASH_SHIFT
1146 #undef VALUE_HASH_SHIFT
1148 #define BLOCK_HASH_SHIFT 16
1151 * ext3_xattr_rehash()
1153 * Re-compute the extended attribute hash value after an entry has changed.
1155 static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1156 struct ext3_xattr_entry *entry)
1158 struct ext3_xattr_entry *here;
1161 ext3_xattr_hash_entry(header, entry);
1162 here = ENTRY(header+1);
1163 while (!IS_LAST_ENTRY(here)) {
1164 if (!here->e_hash) {
1165 /* Block is not shared if an entry's hash value == 0 */
1169 hash = (hash << BLOCK_HASH_SHIFT) ^
1170 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1171 le32_to_cpu(here->e_hash);
1172 here = EXT3_XATTR_NEXT(here);
1174 header->h_hash = cpu_to_le32(hash);
1177 #undef BLOCK_HASH_SHIFT
1180 init_ext3_xattr(void)
1184 err = ext3_xattr_register(EXT3_XATTR_INDEX_USER,
1185 &ext3_xattr_user_handler);
1188 err = ext3_xattr_register(EXT3_XATTR_INDEX_TRUSTED,
1189 &ext3_xattr_trusted_handler);
1192 #ifdef CONFIG_EXT3_FS_SECURITY
1193 err = ext3_xattr_register(EXT3_XATTR_INDEX_SECURITY,
1194 &ext3_xattr_security_handler);
1198 #ifdef CONFIG_EXT3_FS_POSIX_ACL
1199 err = init_ext3_acl();
1203 ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
1204 sizeof(struct mb_cache_entry) +
1205 sizeof(struct mb_cache_entry_index), 1, 6);
1206 if (!ext3_xattr_cache) {
1212 #ifdef CONFIG_EXT3_FS_POSIX_ACL
1216 #ifdef CONFIG_EXT3_FS_SECURITY
1217 ext3_xattr_unregister(EXT3_XATTR_INDEX_SECURITY,
1218 &ext3_xattr_security_handler);
1221 ext3_xattr_unregister(EXT3_XATTR_INDEX_TRUSTED,
1222 &ext3_xattr_trusted_handler);
1224 ext3_xattr_unregister(EXT3_XATTR_INDEX_USER,
1225 &ext3_xattr_user_handler);
1230 exit_ext3_xattr(void)
1232 if (ext3_xattr_cache)
1233 mb_cache_destroy(ext3_xattr_cache);
1234 ext3_xattr_cache = NULL;
1235 #ifdef CONFIG_EXT3_FS_POSIX_ACL
1238 #ifdef CONFIG_EXT3_FS_SECURITY
1239 ext3_xattr_unregister(EXT3_XATTR_INDEX_SECURITY,
1240 &ext3_xattr_security_handler);
1242 ext3_xattr_unregister(EXT3_XATTR_INDEX_TRUSTED,
1243 &ext3_xattr_trusted_handler);
1244 ext3_xattr_unregister(EXT3_XATTR_INDEX_USER,
1245 &ext3_xattr_user_handler);