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>
62 #include <linux/vs_dlimit.h>
66 #define HDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
67 #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
68 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
69 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
71 #ifdef EXT3_XATTR_DEBUG
72 # define ea_idebug(inode, f...) do { \
73 printk(KERN_DEBUG "inode %s:%ld: ", \
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 int ext3_xattr_set_handle2(handle_t *, struct inode *,
93 struct ext3_xattr_header *);
95 static int ext3_xattr_cache_insert(struct buffer_head *);
96 static struct buffer_head *ext3_xattr_cache_find(handle_t *, struct inode *,
97 struct ext3_xattr_header *,
99 static void ext3_xattr_cache_remove(struct buffer_head *);
100 static void ext3_xattr_rehash(struct ext3_xattr_header *,
101 struct ext3_xattr_entry *);
103 static struct mb_cache *ext3_xattr_cache;
104 static struct ext3_xattr_handler *ext3_xattr_handlers[EXT3_XATTR_INDEX_MAX];
105 static rwlock_t ext3_handler_lock = RW_LOCK_UNLOCKED;
108 ext3_xattr_register(int name_index, struct ext3_xattr_handler *handler)
112 if (name_index > 0 && name_index <= EXT3_XATTR_INDEX_MAX) {
113 write_lock(&ext3_handler_lock);
114 if (!ext3_xattr_handlers[name_index-1]) {
115 ext3_xattr_handlers[name_index-1] = handler;
118 write_unlock(&ext3_handler_lock);
124 ext3_xattr_unregister(int name_index, struct ext3_xattr_handler *handler)
126 if (name_index > 0 || name_index <= EXT3_XATTR_INDEX_MAX) {
127 write_lock(&ext3_handler_lock);
128 ext3_xattr_handlers[name_index-1] = NULL;
129 write_unlock(&ext3_handler_lock);
133 static inline const char *
134 strcmp_prefix(const char *a, const char *a_prefix)
136 while (*a_prefix && *a == *a_prefix) {
140 return *a_prefix ? NULL : a;
144 * Decode the extended attribute name, and translate it into
145 * the name_index and name suffix.
147 static inline struct ext3_xattr_handler *
148 ext3_xattr_resolve_name(const char **name)
150 struct ext3_xattr_handler *handler = NULL;
155 read_lock(&ext3_handler_lock);
156 for (i=0; i<EXT3_XATTR_INDEX_MAX; i++) {
157 if (ext3_xattr_handlers[i]) {
158 const char *n = strcmp_prefix(*name,
159 ext3_xattr_handlers[i]->prefix);
161 handler = ext3_xattr_handlers[i];
167 read_unlock(&ext3_handler_lock);
171 static inline struct ext3_xattr_handler *
172 ext3_xattr_handler(int name_index)
174 struct ext3_xattr_handler *handler = NULL;
175 if (name_index > 0 && name_index <= EXT3_XATTR_INDEX_MAX) {
176 read_lock(&ext3_handler_lock);
177 handler = ext3_xattr_handlers[name_index-1];
178 read_unlock(&ext3_handler_lock);
184 * Inode operation getxattr()
186 * dentry->d_inode->i_sem: don't care
189 ext3_getxattr(struct dentry *dentry, const char *name,
190 void *buffer, size_t size)
192 struct ext3_xattr_handler *handler;
193 struct inode *inode = dentry->d_inode;
195 handler = ext3_xattr_resolve_name(&name);
198 return handler->get(inode, name, buffer, size);
202 * Inode operation listxattr()
204 * dentry->d_inode->i_sem: don't care
207 ext3_listxattr(struct dentry *dentry, char *buffer, size_t size)
209 return ext3_xattr_list(dentry->d_inode, buffer, size);
213 * Inode operation setxattr()
215 * dentry->d_inode->i_sem: down
218 ext3_setxattr(struct dentry *dentry, const char *name,
219 const void *value, size_t size, int flags)
221 struct ext3_xattr_handler *handler;
222 struct inode *inode = dentry->d_inode;
225 value = ""; /* empty EA, do not remove */
226 handler = ext3_xattr_resolve_name(&name);
229 return handler->set(inode, name, value, size, flags);
233 * Inode operation removexattr()
235 * dentry->d_inode->i_sem: down
238 ext3_removexattr(struct dentry *dentry, const char *name)
240 struct ext3_xattr_handler *handler;
241 struct inode *inode = dentry->d_inode;
243 handler = ext3_xattr_resolve_name(&name);
246 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
252 * Copy an extended attribute into the buffer
253 * provided, or compute the buffer size required.
254 * Buffer is NULL to compute the size of the buffer required.
256 * Returns a negative error number on failure, or the number of bytes
257 * used / required on success.
260 ext3_xattr_get(struct inode *inode, int name_index, const char *name,
261 void *buffer, size_t buffer_size)
263 struct buffer_head *bh = NULL;
264 struct ext3_xattr_entry *entry;
265 size_t name_len, size;
269 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
270 name_index, name, buffer, (long)buffer_size);
274 down_read(&EXT3_I(inode)->xattr_sem);
276 if (!EXT3_I(inode)->i_file_acl)
278 ea_idebug(inode, "reading block %d", EXT3_I(inode)->i_file_acl);
279 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
283 ea_bdebug(bh, "b_count=%d, refcount=%d",
284 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
285 end = bh->b_data + bh->b_size;
286 if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
287 HDR(bh)->h_blocks != cpu_to_le32(1)) {
288 bad_block: ext3_error(inode->i_sb, "ext3_xattr_get",
289 "inode %ld: bad block %d", inode->i_ino,
290 EXT3_I(inode)->i_file_acl);
294 /* find named attribute */
295 name_len = strlen(name);
300 entry = FIRST_ENTRY(bh);
301 while (!IS_LAST_ENTRY(entry)) {
302 struct ext3_xattr_entry *next =
303 EXT3_XATTR_NEXT(entry);
304 if ((char *)next >= end)
306 if (name_index == entry->e_name_index &&
307 name_len == entry->e_name_len &&
308 memcmp(name, entry->e_name, name_len) == 0)
312 /* Check the remaining name entries */
313 while (!IS_LAST_ENTRY(entry)) {
314 struct ext3_xattr_entry *next =
315 EXT3_XATTR_NEXT(entry);
316 if ((char *)next >= end)
320 if (ext3_xattr_cache_insert(bh))
321 ea_idebug(inode, "cache insert failed");
325 /* check the buffer size */
326 if (entry->e_value_block != 0)
328 size = le32_to_cpu(entry->e_value_size);
329 if (size > inode->i_sb->s_blocksize ||
330 le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
333 if (ext3_xattr_cache_insert(bh))
334 ea_idebug(inode, "cache insert failed");
337 if (size > buffer_size)
339 /* return value of attribute */
340 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
347 up_read(&EXT3_I(inode)->xattr_sem);
355 * Copy a list of attribute names into the buffer
356 * provided, or compute the buffer size required.
357 * Buffer is NULL to compute the size of the buffer required.
359 * Returns a negative error number on failure, or the number of bytes
360 * used / required on success.
363 ext3_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
365 struct buffer_head *bh = NULL;
366 struct ext3_xattr_entry *entry;
371 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
372 buffer, (long)buffer_size);
374 down_read(&EXT3_I(inode)->xattr_sem);
376 if (!EXT3_I(inode)->i_file_acl)
378 ea_idebug(inode, "reading block %d", EXT3_I(inode)->i_file_acl);
379 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
383 ea_bdebug(bh, "b_count=%d, refcount=%d",
384 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
385 end = bh->b_data + bh->b_size;
386 if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
387 HDR(bh)->h_blocks != cpu_to_le32(1)) {
388 bad_block: ext3_error(inode->i_sb, "ext3_xattr_list",
389 "inode %ld: bad block %d", inode->i_ino,
390 EXT3_I(inode)->i_file_acl);
394 /* compute the size required for the list of attribute names */
395 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
396 entry = EXT3_XATTR_NEXT(entry)) {
397 struct ext3_xattr_handler *handler;
398 struct ext3_xattr_entry *next =
399 EXT3_XATTR_NEXT(entry);
400 if ((char *)next >= end)
403 handler = ext3_xattr_handler(entry->e_name_index);
405 size += handler->list(NULL, inode, entry->e_name,
409 if (ext3_xattr_cache_insert(bh))
410 ea_idebug(inode, "cache insert failed");
416 if (size > buffer_size)
420 /* list the attribute names */
422 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
423 entry = EXT3_XATTR_NEXT(entry)) {
424 struct ext3_xattr_handler *handler;
426 handler = ext3_xattr_handler(entry->e_name_index);
428 buf += handler->list(buf, inode, entry->e_name,
435 up_read(&EXT3_I(inode)->xattr_sem);
441 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
444 static void ext3_xattr_update_super_block(handle_t *handle,
445 struct super_block *sb)
447 if (EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_EXT_ATTR))
451 if (ext3_journal_get_write_access(handle, EXT3_SB(sb)->s_sbh) == 0) {
452 EXT3_SB(sb)->s_es->s_feature_compat |=
453 cpu_to_le32(EXT3_FEATURE_COMPAT_EXT_ATTR);
455 ext3_journal_dirty_metadata(handle, EXT3_SB(sb)->s_sbh);
461 * ext3_xattr_set_handle()
463 * Create, replace or remove an extended attribute for this inode. Buffer
464 * is NULL to remove an existing extended attribute, and non-NULL to
465 * either replace an existing extended attribute, or create a new extended
466 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
467 * specify that an extended attribute must exist and must not exist
468 * previous to the call, respectively.
470 * Returns 0, or a negative error number on failure.
473 ext3_xattr_set_handle(handle_t *handle, struct inode *inode, int name_index,
474 const char *name, const void *value, size_t value_len,
477 struct super_block *sb = inode->i_sb;
478 struct buffer_head *bh = NULL;
479 struct ext3_xattr_header *header = NULL;
480 struct ext3_xattr_entry *here, *last;
481 size_t name_len, free, min_offs = sb->s_blocksize;
482 int not_found = 1, error;
486 * header -- Points either into bh, or to a temporarily
488 * here -- The named entry found, or the place for inserting, within
489 * the block pointed to by header.
490 * last -- Points right after the last named entry within the block
491 * pointed to by header.
492 * min_offs -- The offset of the first value (values are aligned
493 * towards the end of the block).
494 * end -- Points right after the block pointed to by header.
497 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
498 name_index, name, value, (long)value_len);
500 if (IS_RDONLY(inode))
502 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
508 name_len = strlen(name);
509 if (name_len > 255 || value_len > sb->s_blocksize)
511 down_write(&EXT3_I(inode)->xattr_sem);
512 if (EXT3_I(inode)->i_file_acl) {
513 /* The inode already has an extended attribute block. */
514 bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
518 ea_bdebug(bh, "b_count=%d, refcount=%d",
519 atomic_read(&(bh->b_count)),
520 le32_to_cpu(HDR(bh)->h_refcount));
522 end = bh->b_data + bh->b_size;
523 if (header->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
524 header->h_blocks != cpu_to_le32(1)) {
525 bad_block: ext3_error(sb, "ext3_xattr_set",
526 "inode %ld: bad block %d", inode->i_ino,
527 EXT3_I(inode)->i_file_acl);
531 /* Find the named attribute. */
532 here = FIRST_ENTRY(bh);
533 while (!IS_LAST_ENTRY(here)) {
534 struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(here);
535 if ((char *)next >= end)
537 if (!here->e_value_block && here->e_value_size) {
538 size_t offs = le16_to_cpu(here->e_value_offs);
542 not_found = name_index - here->e_name_index;
544 not_found = name_len - here->e_name_len;
546 not_found = memcmp(name, here->e_name,name_len);
552 /* We still need to compute min_offs and last. */
553 while (!IS_LAST_ENTRY(last)) {
554 struct ext3_xattr_entry *next = EXT3_XATTR_NEXT(last);
555 if ((char *)next >= end)
557 if (!last->e_value_block && last->e_value_size) {
558 size_t offs = le16_to_cpu(last->e_value_offs);
565 /* Check whether we have enough space left. */
566 free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
568 /* We will use a new extended attribute block. */
569 free = sb->s_blocksize -
570 sizeof(struct ext3_xattr_header) - sizeof(__u32);
571 here = last = NULL; /* avoid gcc uninitialized warning. */
575 /* Request to remove a nonexistent attribute? */
577 if (flags & XATTR_REPLACE)
583 /* Request to create an existing attribute? */
585 if (flags & XATTR_CREATE)
587 if (!here->e_value_block && here->e_value_size) {
588 size_t size = le32_to_cpu(here->e_value_size);
590 if (le16_to_cpu(here->e_value_offs) + size >
591 sb->s_blocksize || size > sb->s_blocksize)
593 free += EXT3_XATTR_SIZE(size);
595 free += EXT3_XATTR_LEN(name_len);
598 if (free < EXT3_XATTR_LEN(name_len) + EXT3_XATTR_SIZE(value_len))
601 /* Here we know that we can set the new attribute. */
606 /* assert(header == HDR(bh)); */
607 if (header->h_refcount != cpu_to_le32(1))
608 goto skip_get_write_access;
609 /* ext3_journal_get_write_access() requires an unlocked bh,
610 which complicates things here. */
611 error = ext3_journal_get_write_access_credits(handle, bh,
616 if (header->h_refcount == cpu_to_le32(1)) {
617 ea_bdebug(bh, "modifying in-place");
618 ext3_xattr_cache_remove(bh);
619 /* keep the buffer locked while modifying it. */
624 journal_release_buffer(handle, bh, credits);
625 skip_get_write_access:
626 ea_bdebug(bh, "cloning");
627 header = kmalloc(bh->b_size, GFP_KERNEL);
631 memcpy(header, HDR(bh), bh->b_size);
632 header->h_refcount = cpu_to_le32(1);
633 offset = (char *)here - bh->b_data;
634 here = ENTRY((char *)header + offset);
635 offset = (char *)last - bh->b_data;
636 last = ENTRY((char *)header + offset);
639 /* Allocate a buffer where we construct the new block. */
640 header = kmalloc(sb->s_blocksize, GFP_KERNEL);
644 memset(header, 0, sb->s_blocksize);
645 end = (char *)header + sb->s_blocksize;
646 header->h_magic = cpu_to_le32(EXT3_XATTR_MAGIC);
647 header->h_blocks = header->h_refcount = cpu_to_le32(1);
648 last = here = ENTRY(header+1);
651 /* Iff we are modifying the block in-place, bh is locked here. */
654 /* Insert the new name. */
655 size_t size = EXT3_XATTR_LEN(name_len);
656 size_t rest = (char *)last - (char *)here;
657 memmove((char *)here + size, here, rest);
658 memset(here, 0, size);
659 here->e_name_index = name_index;
660 here->e_name_len = name_len;
661 memcpy(here->e_name, name, name_len);
663 if (!here->e_value_block && here->e_value_size) {
664 char *first_val = (char *)header + min_offs;
665 size_t offs = le16_to_cpu(here->e_value_offs);
666 char *val = (char *)header + offs;
667 size_t size = EXT3_XATTR_SIZE(
668 le32_to_cpu(here->e_value_size));
670 if (size == EXT3_XATTR_SIZE(value_len)) {
671 /* The old and the new value have the same
672 size. Just replace. */
673 here->e_value_size = cpu_to_le32(value_len);
674 memset(val + size - EXT3_XATTR_PAD, 0,
675 EXT3_XATTR_PAD); /* Clear pad bytes. */
676 memcpy(val, value, value_len);
680 /* Remove the old value. */
681 memmove(first_val + size, first_val, val - first_val);
682 memset(first_val, 0, size);
683 here->e_value_offs = 0;
686 /* Adjust all value offsets. */
687 last = ENTRY(header+1);
688 while (!IS_LAST_ENTRY(last)) {
689 size_t o = le16_to_cpu(last->e_value_offs);
690 if (!last->e_value_block && o < offs)
692 cpu_to_le16(o + size);
693 last = EXT3_XATTR_NEXT(last);
697 /* Remove the old name. */
698 size_t size = EXT3_XATTR_LEN(name_len);
699 last = ENTRY((char *)last - size);
700 memmove(here, (char*)here + size,
701 (char*)last - (char*)here);
702 memset(last, 0, size);
707 /* Insert the new value. */
708 here->e_value_size = cpu_to_le32(value_len);
710 size_t size = EXT3_XATTR_SIZE(value_len);
711 char *val = (char *)header + min_offs - size;
713 cpu_to_le16((char *)val - (char *)header);
714 memset(val + size - EXT3_XATTR_PAD, 0,
715 EXT3_XATTR_PAD); /* Clear the pad bytes. */
716 memcpy(val, value, value_len);
721 if (IS_LAST_ENTRY(ENTRY(header+1))) {
722 /* This block is now empty. */
723 if (bh && header == HDR(bh))
724 unlock_buffer(bh); /* we were modifying in-place. */
725 error = ext3_xattr_set_handle2(handle, inode, bh, NULL);
727 ext3_xattr_rehash(header, here);
728 if (bh && header == HDR(bh))
729 unlock_buffer(bh); /* we were modifying in-place. */
730 error = ext3_xattr_set_handle2(handle, inode, bh, header);
735 if (!(bh && header == HDR(bh)))
737 up_write(&EXT3_I(inode)->xattr_sem);
743 * Second half of ext3_xattr_set_handle(): Update the file system.
746 ext3_xattr_set_handle2(handle_t *handle, struct inode *inode,
747 struct buffer_head *old_bh,
748 struct ext3_xattr_header *header)
750 struct super_block *sb = inode->i_sb;
751 struct buffer_head *new_bh = NULL;
752 int credits = 0, error;
755 new_bh = ext3_xattr_cache_find(handle, inode, header, &credits);
757 /* We found an identical block in the cache. */
758 if (new_bh == old_bh)
759 ea_bdebug(new_bh, "keeping this block");
761 /* The old block is released after updating
763 ea_bdebug(new_bh, "reusing block");
766 if (DLIMIT_ALLOC_BLOCK(sb, inode->i_xid, 1))
769 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
770 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
771 unlock_buffer(new_bh);
772 journal_release_buffer(handle, new_bh,
776 HDR(new_bh)->h_refcount = cpu_to_le32(1 +
777 le32_to_cpu(HDR(new_bh)->h_refcount));
778 ea_bdebug(new_bh, "refcount now=%d",
779 le32_to_cpu(HDR(new_bh)->h_refcount));
781 unlock_buffer(new_bh);
782 } else if (old_bh && header == HDR(old_bh)) {
783 /* Keep this block. No need to lock the block as we
784 * don't need to change the reference count. */
787 ext3_xattr_cache_insert(new_bh);
789 /* We need to allocate a new block */
790 int goal = le32_to_cpu(
791 EXT3_SB(sb)->s_es->s_first_data_block) +
792 EXT3_I(inode)->i_block_group *
793 EXT3_BLOCKS_PER_GROUP(sb);
794 int block = ext3_new_block(handle, inode, goal,
798 ea_idebug(inode, "creating block %d", block);
800 new_bh = sb_getblk(sb, block);
803 ext3_free_blocks(handle, inode, block, 1);
808 error = ext3_journal_get_create_access(handle, new_bh);
810 unlock_buffer(new_bh);
813 memcpy(new_bh->b_data, header, new_bh->b_size);
814 set_buffer_uptodate(new_bh);
815 unlock_buffer(new_bh);
816 ext3_xattr_cache_insert(new_bh);
818 ext3_xattr_update_super_block(handle, sb);
820 error = ext3_journal_dirty_metadata(handle, new_bh);
825 /* Update the inode. */
826 EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
827 inode->i_ctime = CURRENT_TIME;
828 ext3_mark_inode_dirty(handle, inode);
833 if (old_bh && old_bh != new_bh) {
835 * If there was an old block, and we are no longer using it,
836 * release the old block.
838 error = ext3_journal_get_write_access(handle, old_bh);
842 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
843 /* Free the old block. */
844 ea_bdebug(old_bh, "freeing");
845 ext3_free_blocks(handle, inode, old_bh->b_blocknr, 1);
847 /* ext3_forget() calls bforget() for us, but we
848 let our caller release old_bh, so we need to
849 duplicate the handle before. */
851 ext3_forget(handle, 1, inode, old_bh,old_bh->b_blocknr);
853 /* Decrement the refcount only. */
854 HDR(old_bh)->h_refcount = cpu_to_le32(
855 le32_to_cpu(HDR(old_bh)->h_refcount) - 1);
856 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
857 DQUOT_FREE_BLOCK(inode, 1);
858 ext3_journal_dirty_metadata(handle, old_bh);
859 ea_bdebug(old_bh, "refcount now=%d",
860 le32_to_cpu(HDR(old_bh)->h_refcount));
862 unlock_buffer(old_bh);
874 * Like ext3_xattr_set_handle, but start from an inode. This extended
875 * attribute modification is a filesystem transaction by itself.
877 * Returns 0, or a negative error number on failure.
880 ext3_xattr_set(struct inode *inode, int name_index, const char *name,
881 const void *value, size_t value_len, int flags)
884 int error, retries = 0;
887 handle = ext3_journal_start(inode, EXT3_DATA_TRANS_BLOCKS);
888 if (IS_ERR(handle)) {
889 error = PTR_ERR(handle);
893 error = ext3_xattr_set_handle(handle, inode, name_index, name,
894 value, value_len, flags);
895 error2 = ext3_journal_stop(handle);
896 if (error == -ENOSPC &&
897 ext3_should_retry_alloc(inode->i_sb, &retries))
907 * ext3_xattr_delete_inode()
909 * Free extended attribute resources associated with this inode. This
910 * is called immediately before an inode is freed.
913 ext3_xattr_delete_inode(handle_t *handle, struct inode *inode)
915 struct buffer_head *bh = NULL;
917 down_write(&EXT3_I(inode)->xattr_sem);
918 if (!EXT3_I(inode)->i_file_acl)
920 bh = sb_bread(inode->i_sb, EXT3_I(inode)->i_file_acl);
922 ext3_error(inode->i_sb, "ext3_xattr_delete_inode",
923 "inode %ld: block %d read error", inode->i_ino,
924 EXT3_I(inode)->i_file_acl);
927 if (HDR(bh)->h_magic != cpu_to_le32(EXT3_XATTR_MAGIC) ||
928 HDR(bh)->h_blocks != cpu_to_le32(1)) {
929 ext3_error(inode->i_sb, "ext3_xattr_delete_inode",
930 "inode %ld: bad block %d", inode->i_ino,
931 EXT3_I(inode)->i_file_acl);
934 if (ext3_journal_get_write_access(handle, bh) != 0)
937 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
938 ext3_xattr_cache_remove(bh);
939 ext3_free_blocks(handle, inode, EXT3_I(inode)->i_file_acl, 1);
941 ext3_forget(handle, 1, inode, bh, EXT3_I(inode)->i_file_acl);
943 HDR(bh)->h_refcount = cpu_to_le32(
944 le32_to_cpu(HDR(bh)->h_refcount) - 1);
945 ext3_journal_dirty_metadata(handle, bh);
948 DLIMIT_FREE_BLOCK(inode->i_sb, inode->i_xid, 1);
949 DQUOT_FREE_BLOCK(inode, 1);
951 ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
953 EXT3_I(inode)->i_file_acl = 0;
957 up_write(&EXT3_I(inode)->xattr_sem);
961 * ext3_xattr_put_super()
963 * This is called when a file system is unmounted.
966 ext3_xattr_put_super(struct super_block *sb)
968 mb_cache_shrink(ext3_xattr_cache, sb->s_bdev);
972 * ext3_xattr_cache_insert()
974 * Create a new entry in the extended attribute cache, and insert
975 * it unless such an entry is already in the cache.
977 * Returns 0, or a negative error number on failure.
980 ext3_xattr_cache_insert(struct buffer_head *bh)
982 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
983 struct mb_cache_entry *ce;
986 ce = mb_cache_entry_alloc(ext3_xattr_cache);
989 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
991 mb_cache_entry_free(ce);
992 if (error == -EBUSY) {
993 ea_bdebug(bh, "already in cache (%d cache entries)",
994 atomic_read(&ext3_xattr_cache->c_entry_count));
998 ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
999 atomic_read(&ext3_xattr_cache->c_entry_count));
1000 mb_cache_entry_release(ce);
1008 * Compare two extended attribute blocks for equality.
1010 * Returns 0 if the blocks are equal, 1 if they differ, and
1011 * a negative error number on errors.
1014 ext3_xattr_cmp(struct ext3_xattr_header *header1,
1015 struct ext3_xattr_header *header2)
1017 struct ext3_xattr_entry *entry1, *entry2;
1019 entry1 = ENTRY(header1+1);
1020 entry2 = ENTRY(header2+1);
1021 while (!IS_LAST_ENTRY(entry1)) {
1022 if (IS_LAST_ENTRY(entry2))
1024 if (entry1->e_hash != entry2->e_hash ||
1025 entry1->e_name_len != entry2->e_name_len ||
1026 entry1->e_value_size != entry2->e_value_size ||
1027 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
1029 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
1031 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
1032 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
1033 le32_to_cpu(entry1->e_value_size)))
1036 entry1 = EXT3_XATTR_NEXT(entry1);
1037 entry2 = EXT3_XATTR_NEXT(entry2);
1039 if (!IS_LAST_ENTRY(entry2))
1045 * ext3_xattr_cache_find()
1047 * Find an identical extended attribute block.
1049 * Returns a pointer to the block found, or NULL if such a block was
1050 * not found or an error occurred.
1052 static struct buffer_head *
1053 ext3_xattr_cache_find(handle_t *handle, struct inode *inode,
1054 struct ext3_xattr_header *header, int *credits)
1056 __u32 hash = le32_to_cpu(header->h_hash);
1057 struct mb_cache_entry *ce;
1059 if (!header->h_hash)
1060 return NULL; /* never share */
1061 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1062 ce = mb_cache_entry_find_first(ext3_xattr_cache, 0,
1063 inode->i_sb->s_bdev, hash);
1065 struct buffer_head *bh = sb_bread(inode->i_sb, ce->e_block);
1068 ext3_error(inode->i_sb, "ext3_xattr_cache_find",
1069 "inode %ld: block %ld read error",
1070 inode->i_ino, (unsigned long) ce->e_block);
1071 } else if (ext3_journal_get_write_access_credits(
1072 handle, bh, credits) == 0) {
1073 /* ext3_journal_get_write_access() requires an unlocked
1074 * bh, which complicates things here. */
1076 if (le32_to_cpu(HDR(bh)->h_refcount) >
1077 EXT3_XATTR_REFCOUNT_MAX) {
1078 ea_idebug(inode, "block %ld refcount %d>%d",
1079 (unsigned long) ce->e_block,
1080 le32_to_cpu(HDR(bh)->h_refcount),
1081 EXT3_XATTR_REFCOUNT_MAX);
1082 } else if (!ext3_xattr_cmp(header, HDR(bh))) {
1083 mb_cache_entry_release(ce);
1084 /* buffer will be unlocked by caller */
1088 journal_release_buffer(handle, bh, *credits);
1092 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1098 * ext3_xattr_cache_remove()
1100 * Remove the cache entry of a block from the cache. Called when a
1101 * block becomes invalid.
1104 ext3_xattr_cache_remove(struct buffer_head *bh)
1106 struct mb_cache_entry *ce;
1108 ce = mb_cache_entry_get(ext3_xattr_cache, bh->b_bdev,
1111 ea_bdebug(bh, "removing (%d cache entries remaining)",
1112 atomic_read(&ext3_xattr_cache->c_entry_count)-1);
1113 mb_cache_entry_free(ce);
1115 ea_bdebug(bh, "no cache entry");
1118 #define NAME_HASH_SHIFT 5
1119 #define VALUE_HASH_SHIFT 16
1122 * ext3_xattr_hash_entry()
1124 * Compute the hash of an extended attribute.
1126 static inline void ext3_xattr_hash_entry(struct ext3_xattr_header *header,
1127 struct ext3_xattr_entry *entry)
1130 char *name = entry->e_name;
1133 for (n=0; n < entry->e_name_len; n++) {
1134 hash = (hash << NAME_HASH_SHIFT) ^
1135 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1139 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1140 __le32 *value = (__le32 *)((char *)header +
1141 le16_to_cpu(entry->e_value_offs));
1142 for (n = (le32_to_cpu(entry->e_value_size) +
1143 EXT3_XATTR_ROUND) >> EXT3_XATTR_PAD_BITS; n; n--) {
1144 hash = (hash << VALUE_HASH_SHIFT) ^
1145 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1146 le32_to_cpu(*value++);
1149 entry->e_hash = cpu_to_le32(hash);
1152 #undef NAME_HASH_SHIFT
1153 #undef VALUE_HASH_SHIFT
1155 #define BLOCK_HASH_SHIFT 16
1158 * ext3_xattr_rehash()
1160 * Re-compute the extended attribute hash value after an entry has changed.
1162 static void ext3_xattr_rehash(struct ext3_xattr_header *header,
1163 struct ext3_xattr_entry *entry)
1165 struct ext3_xattr_entry *here;
1168 ext3_xattr_hash_entry(header, entry);
1169 here = ENTRY(header+1);
1170 while (!IS_LAST_ENTRY(here)) {
1171 if (!here->e_hash) {
1172 /* Block is not shared if an entry's hash value == 0 */
1176 hash = (hash << BLOCK_HASH_SHIFT) ^
1177 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1178 le32_to_cpu(here->e_hash);
1179 here = EXT3_XATTR_NEXT(here);
1181 header->h_hash = cpu_to_le32(hash);
1184 #undef BLOCK_HASH_SHIFT
1187 init_ext3_xattr(void)
1191 err = ext3_xattr_register(EXT3_XATTR_INDEX_USER,
1192 &ext3_xattr_user_handler);
1195 err = ext3_xattr_register(EXT3_XATTR_INDEX_TRUSTED,
1196 &ext3_xattr_trusted_handler);
1199 #ifdef CONFIG_EXT3_FS_SECURITY
1200 err = ext3_xattr_register(EXT3_XATTR_INDEX_SECURITY,
1201 &ext3_xattr_security_handler);
1205 #ifdef CONFIG_EXT3_FS_POSIX_ACL
1206 err = init_ext3_acl();
1210 ext3_xattr_cache = mb_cache_create("ext3_xattr", NULL,
1211 sizeof(struct mb_cache_entry) +
1212 sizeof(struct mb_cache_entry_index), 1, 6);
1213 if (!ext3_xattr_cache) {
1219 #ifdef CONFIG_EXT3_FS_POSIX_ACL
1223 #ifdef CONFIG_EXT3_FS_SECURITY
1224 ext3_xattr_unregister(EXT3_XATTR_INDEX_SECURITY,
1225 &ext3_xattr_security_handler);
1228 ext3_xattr_unregister(EXT3_XATTR_INDEX_TRUSTED,
1229 &ext3_xattr_trusted_handler);
1231 ext3_xattr_unregister(EXT3_XATTR_INDEX_USER,
1232 &ext3_xattr_user_handler);
1237 exit_ext3_xattr(void)
1239 if (ext3_xattr_cache)
1240 mb_cache_destroy(ext3_xattr_cache);
1241 ext3_xattr_cache = NULL;
1242 #ifdef CONFIG_EXT3_FS_POSIX_ACL
1245 #ifdef CONFIG_EXT3_FS_SECURITY
1246 ext3_xattr_unregister(EXT3_XATTR_INDEX_SECURITY,
1247 &ext3_xattr_security_handler);
1249 ext3_xattr_unregister(EXT3_XATTR_INDEX_TRUSTED,
1250 &ext3_xattr_trusted_handler);
1251 ext3_xattr_unregister(EXT3_XATTR_INDEX_USER,
1252 &ext3_xattr_user_handler);