2 * linux/fs/ext2/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Extended attributes for symlinks and special files added per
8 * suggestion of Luka Renko <luka.renko@hermes.si>.
12 * Extended attributes are stored on disk blocks allocated outside of
13 * any inode. The i_file_acl field is then made to point to this allocated
14 * block. If all extended attributes of an inode are identical, these
15 * inodes may share the same extended attribute block. Such situations
16 * are automatically detected by keeping a cache of recent attribute block
17 * numbers and hashes over the block's contents in memory.
20 * Extended attribute block layout:
22 * +------------------+
25 * | entry 2 | | growing downwards
30 * | value 3 | | growing upwards
32 * +------------------+
34 * The block header is followed by multiple entry descriptors. These entry
35 * descriptors are variable in size, and alligned to EXT2_XATTR_PAD
36 * byte boundaries. The entry descriptors are sorted by attribute name,
37 * so that two extended attribute blocks can be compared efficiently.
39 * Attribute values are aligned to the end of the block, stored in
40 * no specific order. They are also padded to EXT2_XATTR_PAD byte
41 * boundaries. No additional gaps are left between them.
45 * EXT2_I(inode)->i_file_acl is protected by EXT2_I(inode)->xattr_sem.
46 * EA blocks are only changed if they are exclusive to an inode, so
47 * holding xattr_sem also means that nothing but the EA block's reference
48 * count will change. Multiple writers to an EA block are synchronized
49 * by the bh lock. No more than a single bh lock is held at any time
53 #include <linux/buffer_head.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/mbcache.h>
58 #include <linux/quotaops.h>
59 #include <linux/rwsem.h>
60 #include <linux/vs_dlimit.h>
65 /* These symbols may be needed by a module. */
66 EXPORT_SYMBOL(ext2_xattr_register);
67 EXPORT_SYMBOL(ext2_xattr_unregister);
68 EXPORT_SYMBOL(ext2_xattr_get);
69 EXPORT_SYMBOL(ext2_xattr_list);
70 EXPORT_SYMBOL(ext2_xattr_set);
72 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
73 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
74 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
75 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
77 #ifdef EXT2_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 int ext2_xattr_set2(struct inode *, struct buffer_head *,
98 struct ext2_xattr_header *);
100 static int ext2_xattr_cache_insert(struct buffer_head *);
101 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
102 struct ext2_xattr_header *);
103 static void ext2_xattr_cache_remove(struct buffer_head *);
104 static void ext2_xattr_rehash(struct ext2_xattr_header *,
105 struct ext2_xattr_entry *);
107 static struct mb_cache *ext2_xattr_cache;
108 static struct ext2_xattr_handler *ext2_xattr_handlers[EXT2_XATTR_INDEX_MAX];
109 static rwlock_t ext2_handler_lock = RW_LOCK_UNLOCKED;
112 ext2_xattr_register(int name_index, struct ext2_xattr_handler *handler)
116 if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
117 write_lock(&ext2_handler_lock);
118 if (!ext2_xattr_handlers[name_index-1]) {
119 ext2_xattr_handlers[name_index-1] = handler;
122 write_unlock(&ext2_handler_lock);
128 ext2_xattr_unregister(int name_index, struct ext2_xattr_handler *handler)
130 if (name_index > 0 || name_index <= EXT2_XATTR_INDEX_MAX) {
131 write_lock(&ext2_handler_lock);
132 ext2_xattr_handlers[name_index-1] = NULL;
133 write_unlock(&ext2_handler_lock);
137 static inline const char *
138 strcmp_prefix(const char *a, const char *a_prefix)
140 while (*a_prefix && *a == *a_prefix) {
144 return *a_prefix ? NULL : a;
148 * Decode the extended attribute name, and translate it into
149 * the name_index and name suffix.
151 static struct ext2_xattr_handler *
152 ext2_xattr_resolve_name(const char **name)
154 struct ext2_xattr_handler *handler = NULL;
159 read_lock(&ext2_handler_lock);
160 for (i=0; i<EXT2_XATTR_INDEX_MAX; i++) {
161 if (ext2_xattr_handlers[i]) {
162 const char *n = strcmp_prefix(*name,
163 ext2_xattr_handlers[i]->prefix);
165 handler = ext2_xattr_handlers[i];
171 read_unlock(&ext2_handler_lock);
175 static inline struct ext2_xattr_handler *
176 ext2_xattr_handler(int name_index)
178 struct ext2_xattr_handler *handler = NULL;
179 if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
180 read_lock(&ext2_handler_lock);
181 handler = ext2_xattr_handlers[name_index-1];
182 read_unlock(&ext2_handler_lock);
188 * Inode operation getxattr()
190 * dentry->d_inode->i_sem: don't care
193 ext2_getxattr(struct dentry *dentry, const char *name,
194 void *buffer, size_t size)
196 struct ext2_xattr_handler *handler;
197 struct inode *inode = dentry->d_inode;
199 handler = ext2_xattr_resolve_name(&name);
202 return handler->get(inode, name, buffer, size);
206 * Inode operation listxattr()
208 * dentry->d_inode->i_sem: don't care
211 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
213 return ext2_xattr_list(dentry->d_inode, buffer, size);
217 * Inode operation setxattr()
219 * dentry->d_inode->i_sem: down
222 ext2_setxattr(struct dentry *dentry, const char *name,
223 const void *value, size_t size, int flags)
225 struct ext2_xattr_handler *handler;
226 struct inode *inode = dentry->d_inode;
229 value = ""; /* empty EA, do not remove */
230 handler = ext2_xattr_resolve_name(&name);
233 return handler->set(inode, name, value, size, flags);
237 * Inode operation removexattr()
239 * dentry->d_inode->i_sem: down
242 ext2_removexattr(struct dentry *dentry, const char *name)
244 struct ext2_xattr_handler *handler;
245 struct inode *inode = dentry->d_inode;
247 handler = ext2_xattr_resolve_name(&name);
250 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
256 * Copy an extended attribute into the buffer
257 * provided, or compute the buffer size required.
258 * Buffer is NULL to compute the size of the buffer required.
260 * Returns a negative error number on failure, or the number of bytes
261 * used / required on success.
264 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
265 void *buffer, size_t buffer_size)
267 struct buffer_head *bh = NULL;
268 struct ext2_xattr_entry *entry;
269 size_t name_len, size;
273 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
274 name_index, name, buffer, (long)buffer_size);
278 down_read(&EXT2_I(inode)->xattr_sem);
280 if (!EXT2_I(inode)->i_file_acl)
282 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
283 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
287 ea_bdebug(bh, "b_count=%d, refcount=%d",
288 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
289 end = bh->b_data + bh->b_size;
290 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
291 HDR(bh)->h_blocks != cpu_to_le32(1)) {
292 bad_block: ext2_error(inode->i_sb, "ext2_xattr_get",
293 "inode %ld: bad block %d", inode->i_ino,
294 EXT2_I(inode)->i_file_acl);
298 /* find named attribute */
299 name_len = strlen(name);
304 entry = FIRST_ENTRY(bh);
305 while (!IS_LAST_ENTRY(entry)) {
306 struct ext2_xattr_entry *next =
307 EXT2_XATTR_NEXT(entry);
308 if ((char *)next >= end)
310 if (name_index == entry->e_name_index &&
311 name_len == entry->e_name_len &&
312 memcmp(name, entry->e_name, name_len) == 0)
316 /* Check the remaining name entries */
317 while (!IS_LAST_ENTRY(entry)) {
318 struct ext2_xattr_entry *next =
319 EXT2_XATTR_NEXT(entry);
320 if ((char *)next >= end)
324 if (ext2_xattr_cache_insert(bh))
325 ea_idebug(inode, "cache insert failed");
329 /* check the buffer size */
330 if (entry->e_value_block != 0)
332 size = le32_to_cpu(entry->e_value_size);
333 if (size > inode->i_sb->s_blocksize ||
334 le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
337 if (ext2_xattr_cache_insert(bh))
338 ea_idebug(inode, "cache insert failed");
341 if (size > buffer_size)
343 /* return value of attribute */
344 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
351 up_read(&EXT2_I(inode)->xattr_sem);
359 * Copy a list of attribute names into the buffer
360 * provided, or compute the buffer size required.
361 * Buffer is NULL to compute the size of the buffer required.
363 * Returns a negative error number on failure, or the number of bytes
364 * used / required on success.
367 ext2_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
369 struct buffer_head *bh = NULL;
370 struct ext2_xattr_entry *entry;
375 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
376 buffer, (long)buffer_size);
378 down_read(&EXT2_I(inode)->xattr_sem);
380 if (!EXT2_I(inode)->i_file_acl)
382 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
383 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
387 ea_bdebug(bh, "b_count=%d, refcount=%d",
388 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
389 end = bh->b_data + bh->b_size;
390 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
391 HDR(bh)->h_blocks != cpu_to_le32(1)) {
392 bad_block: ext2_error(inode->i_sb, "ext2_xattr_list",
393 "inode %ld: bad block %d", inode->i_ino,
394 EXT2_I(inode)->i_file_acl);
398 /* compute the size required for the list of attribute names */
399 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
400 entry = EXT2_XATTR_NEXT(entry)) {
401 struct ext2_xattr_handler *handler;
402 struct ext2_xattr_entry *next =
403 EXT2_XATTR_NEXT(entry);
404 if ((char *)next >= end)
407 handler = ext2_xattr_handler(entry->e_name_index);
409 size += handler->list(NULL, inode, entry->e_name,
413 if (ext2_xattr_cache_insert(bh))
414 ea_idebug(inode, "cache insert failed");
420 if (size > buffer_size)
424 /* list the attribute names */
426 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
427 entry = EXT2_XATTR_NEXT(entry)) {
428 struct ext2_xattr_handler *handler;
430 handler = ext2_xattr_handler(entry->e_name_index);
432 buf += handler->list(buf, inode, entry->e_name,
439 up_read(&EXT2_I(inode)->xattr_sem);
445 * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
448 static void ext2_xattr_update_super_block(struct super_block *sb)
450 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
454 EXT2_SB(sb)->s_es->s_feature_compat |=
455 cpu_to_le32(EXT2_FEATURE_COMPAT_EXT_ATTR);
457 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
464 * Create, replace or remove an extended attribute for this inode. Buffer
465 * is NULL to remove an existing extended attribute, and non-NULL to
466 * either replace an existing extended attribute, or create a new extended
467 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
468 * specify that an extended attribute must exist and must not exist
469 * previous to the call, respectively.
471 * Returns 0, or a negative error number on failure.
474 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
475 const void *value, size_t value_len, int flags)
477 struct super_block *sb = inode->i_sb;
478 struct buffer_head *bh = NULL;
479 struct ext2_xattr_header *header = NULL;
480 struct ext2_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(&EXT2_I(inode)->xattr_sem);
512 if (EXT2_I(inode)->i_file_acl) {
513 /* The inode already has an extended attribute block. */
514 bh = sb_bread(sb, EXT2_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(EXT2_XATTR_MAGIC) ||
524 header->h_blocks != cpu_to_le32(1)) {
525 bad_block: ext2_error(sb, "ext2_xattr_set",
526 "inode %ld: bad block %d", inode->i_ino,
527 EXT2_I(inode)->i_file_acl);
531 /* Find the named attribute. */
532 here = FIRST_ENTRY(bh);
533 while (!IS_LAST_ENTRY(here)) {
534 struct ext2_xattr_entry *next = EXT2_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 ext2_xattr_entry *next = EXT2_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 ext2_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 += EXT2_XATTR_SIZE(size);
595 free += EXT2_XATTR_LEN(name_len);
598 if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
601 /* Here we know that we can set the new attribute. */
604 /* assert(header == HDR(bh)); */
606 if (header->h_refcount == cpu_to_le32(1)) {
607 ea_bdebug(bh, "modifying in-place");
608 ext2_xattr_cache_remove(bh);
609 /* keep the buffer locked while modifying it. */
614 ea_bdebug(bh, "cloning");
615 header = kmalloc(bh->b_size, GFP_KERNEL);
619 memcpy(header, HDR(bh), bh->b_size);
620 header->h_refcount = cpu_to_le32(1);
622 offset = (char *)here - bh->b_data;
623 here = ENTRY((char *)header + offset);
624 offset = (char *)last - bh->b_data;
625 last = ENTRY((char *)header + offset);
628 /* Allocate a buffer where we construct the new block. */
629 header = kmalloc(sb->s_blocksize, GFP_KERNEL);
633 memset(header, 0, sb->s_blocksize);
634 end = (char *)header + sb->s_blocksize;
635 header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
636 header->h_blocks = header->h_refcount = cpu_to_le32(1);
637 last = here = ENTRY(header+1);
640 /* Iff we are modifying the block in-place, bh is locked here. */
643 /* Insert the new name. */
644 size_t size = EXT2_XATTR_LEN(name_len);
645 size_t rest = (char *)last - (char *)here;
646 memmove((char *)here + size, here, rest);
647 memset(here, 0, size);
648 here->e_name_index = name_index;
649 here->e_name_len = name_len;
650 memcpy(here->e_name, name, name_len);
652 if (!here->e_value_block && here->e_value_size) {
653 char *first_val = (char *)header + min_offs;
654 size_t offs = le16_to_cpu(here->e_value_offs);
655 char *val = (char *)header + offs;
656 size_t size = EXT2_XATTR_SIZE(
657 le32_to_cpu(here->e_value_size));
659 if (size == EXT2_XATTR_SIZE(value_len)) {
660 /* The old and the new value have the same
661 size. Just replace. */
662 here->e_value_size = cpu_to_le32(value_len);
663 memset(val + size - EXT2_XATTR_PAD, 0,
664 EXT2_XATTR_PAD); /* Clear pad bytes. */
665 memcpy(val, value, value_len);
669 /* Remove the old value. */
670 memmove(first_val + size, first_val, val - first_val);
671 memset(first_val, 0, size);
672 here->e_value_offs = 0;
675 /* Adjust all value offsets. */
676 last = ENTRY(header+1);
677 while (!IS_LAST_ENTRY(last)) {
678 size_t o = le16_to_cpu(last->e_value_offs);
679 if (!last->e_value_block && o < offs)
681 cpu_to_le16(o + size);
682 last = EXT2_XATTR_NEXT(last);
686 /* Remove the old name. */
687 size_t size = EXT2_XATTR_LEN(name_len);
688 last = ENTRY((char *)last - size);
689 memmove(here, (char*)here + size,
690 (char*)last - (char*)here);
691 memset(last, 0, size);
696 /* Insert the new value. */
697 here->e_value_size = cpu_to_le32(value_len);
699 size_t size = EXT2_XATTR_SIZE(value_len);
700 char *val = (char *)header + min_offs - size;
702 cpu_to_le16((char *)val - (char *)header);
703 memset(val + size - EXT2_XATTR_PAD, 0,
704 EXT2_XATTR_PAD); /* Clear the pad bytes. */
705 memcpy(val, value, value_len);
710 if (IS_LAST_ENTRY(ENTRY(header+1))) {
711 /* This block is now empty. */
712 if (bh && header == HDR(bh))
713 unlock_buffer(bh); /* we were modifying in-place. */
714 error = ext2_xattr_set2(inode, bh, NULL);
716 ext2_xattr_rehash(header, here);
717 if (bh && header == HDR(bh))
718 unlock_buffer(bh); /* we were modifying in-place. */
719 error = ext2_xattr_set2(inode, bh, header);
724 if (!(bh && header == HDR(bh)))
726 up_write(&EXT2_I(inode)->xattr_sem);
732 * Second half of ext2_xattr_set(): Update the file system.
735 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
736 struct ext2_xattr_header *header)
738 struct super_block *sb = inode->i_sb;
739 struct buffer_head *new_bh = NULL;
743 new_bh = ext2_xattr_cache_find(inode, header);
745 /* We found an identical block in the cache. */
746 if (new_bh == old_bh) {
747 ea_bdebug(new_bh, "keeping this block");
749 /* The old block is released after updating
751 ea_bdebug(new_bh, "reusing block");
754 if (DLIMIT_ALLOC_BLOCK(sb, inode->i_xid, 1))
757 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
758 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
759 unlock_buffer(new_bh);
762 HDR(new_bh)->h_refcount = cpu_to_le32(1 +
763 le32_to_cpu(HDR(new_bh)->h_refcount));
764 ea_bdebug(new_bh, "refcount now=%d",
765 le32_to_cpu(HDR(new_bh)->h_refcount));
767 unlock_buffer(new_bh);
768 } else if (old_bh && header == HDR(old_bh)) {
769 /* Keep this block. No need to lock the block as we
770 don't need to change the reference count. */
773 ext2_xattr_cache_insert(new_bh);
775 /* We need to allocate a new block */
776 int goal = le32_to_cpu(EXT2_SB(sb)->s_es->
777 s_first_data_block) +
778 EXT2_I(inode)->i_block_group *
779 EXT2_BLOCKS_PER_GROUP(sb);
780 int block = ext2_new_block(inode, goal, 0, 0, &error);
783 ea_idebug(inode, "creating block %d", block);
785 new_bh = sb_getblk(sb, block);
787 ext2_free_blocks(inode, block, 1);
792 memcpy(new_bh->b_data, header, new_bh->b_size);
793 set_buffer_uptodate(new_bh);
794 unlock_buffer(new_bh);
795 ext2_xattr_cache_insert(new_bh);
797 ext2_xattr_update_super_block(sb);
799 mark_buffer_dirty(new_bh);
800 if (IS_SYNC(inode)) {
801 sync_dirty_buffer(new_bh);
803 if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
808 /* Update the inode. */
809 EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
810 inode->i_ctime = CURRENT_TIME;
811 if (IS_SYNC(inode)) {
812 error = ext2_sync_inode (inode);
816 mark_inode_dirty(inode);
819 if (old_bh && old_bh != new_bh) {
821 * If there was an old block and we are no longer using it,
822 * release the old block.
825 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
826 /* Free the old block. */
827 ea_bdebug(old_bh, "freeing");
828 ext2_free_blocks(inode, old_bh->b_blocknr, 1);
829 /* We let our caller release old_bh, so we
830 * need to duplicate the buffer before. */
834 /* Decrement the refcount only. */
835 HDR(old_bh)->h_refcount = cpu_to_le32(
836 le32_to_cpu(HDR(old_bh)->h_refcount) - 1);
837 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
838 DQUOT_FREE_BLOCK(inode, 1);
839 mark_buffer_dirty(old_bh);
840 ea_bdebug(old_bh, "refcount now=%d",
841 le32_to_cpu(HDR(old_bh)->h_refcount));
843 unlock_buffer(old_bh);
853 * ext2_xattr_delete_inode()
855 * Free extended attribute resources associated with this inode. This
856 * is called immediately before an inode is freed.
859 ext2_xattr_delete_inode(struct inode *inode)
861 struct buffer_head *bh = NULL;
863 down_write(&EXT2_I(inode)->xattr_sem);
864 if (!EXT2_I(inode)->i_file_acl)
866 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
868 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
869 "inode %ld: block %d read error", inode->i_ino,
870 EXT2_I(inode)->i_file_acl);
873 ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
874 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
875 HDR(bh)->h_blocks != cpu_to_le32(1)) {
876 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
877 "inode %ld: bad block %d", inode->i_ino,
878 EXT2_I(inode)->i_file_acl);
882 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
883 ext2_xattr_cache_remove(bh);
884 ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
888 HDR(bh)->h_refcount = cpu_to_le32(
889 le32_to_cpu(HDR(bh)->h_refcount) - 1);
890 mark_buffer_dirty(bh);
892 sync_dirty_buffer(bh);
893 DLIMIT_FREE_BLOCK(inode->i_sb, inode->i_xid, 1);
894 DQUOT_FREE_BLOCK(inode, 1);
896 ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
898 EXT2_I(inode)->i_file_acl = 0;
902 up_write(&EXT2_I(inode)->xattr_sem);
906 * ext2_xattr_put_super()
908 * This is called when a file system is unmounted.
911 ext2_xattr_put_super(struct super_block *sb)
913 mb_cache_shrink(ext2_xattr_cache, sb->s_bdev);
918 * ext2_xattr_cache_insert()
920 * Create a new entry in the extended attribute cache, and insert
921 * it unless such an entry is already in the cache.
923 * Returns 0, or a negative error number on failure.
926 ext2_xattr_cache_insert(struct buffer_head *bh)
928 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
929 struct mb_cache_entry *ce;
932 ce = mb_cache_entry_alloc(ext2_xattr_cache);
935 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
937 mb_cache_entry_free(ce);
938 if (error == -EBUSY) {
939 ea_bdebug(bh, "already in cache (%d cache entries)",
940 atomic_read(&ext2_xattr_cache->c_entry_count));
944 ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
945 atomic_read(&ext2_xattr_cache->c_entry_count));
946 mb_cache_entry_release(ce);
954 * Compare two extended attribute blocks for equality.
956 * Returns 0 if the blocks are equal, 1 if they differ, and
957 * a negative error number on errors.
960 ext2_xattr_cmp(struct ext2_xattr_header *header1,
961 struct ext2_xattr_header *header2)
963 struct ext2_xattr_entry *entry1, *entry2;
965 entry1 = ENTRY(header1+1);
966 entry2 = ENTRY(header2+1);
967 while (!IS_LAST_ENTRY(entry1)) {
968 if (IS_LAST_ENTRY(entry2))
970 if (entry1->e_hash != entry2->e_hash ||
971 entry1->e_name_len != entry2->e_name_len ||
972 entry1->e_value_size != entry2->e_value_size ||
973 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
975 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
977 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
978 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
979 le32_to_cpu(entry1->e_value_size)))
982 entry1 = EXT2_XATTR_NEXT(entry1);
983 entry2 = EXT2_XATTR_NEXT(entry2);
985 if (!IS_LAST_ENTRY(entry2))
991 * ext2_xattr_cache_find()
993 * Find an identical extended attribute block.
995 * Returns a locked buffer head to the block found, or NULL if such
996 * a block was not found or an error occurred.
998 static struct buffer_head *
999 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
1001 __u32 hash = le32_to_cpu(header->h_hash);
1002 struct mb_cache_entry *ce;
1004 if (!header->h_hash)
1005 return NULL; /* never share */
1006 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1007 ce = mb_cache_entry_find_first(ext2_xattr_cache, 0,
1008 inode->i_sb->s_bdev, hash);
1010 struct buffer_head *bh = sb_bread(inode->i_sb, ce->e_block);
1013 ext2_error(inode->i_sb, "ext2_xattr_cache_find",
1014 "inode %ld: block %ld read error",
1015 inode->i_ino, (unsigned long) ce->e_block);
1018 if (le32_to_cpu(HDR(bh)->h_refcount) >
1019 EXT2_XATTR_REFCOUNT_MAX) {
1020 ea_idebug(inode, "block %ld refcount %d>%d",
1021 (unsigned long) ce->e_block,
1022 le32_to_cpu(HDR(bh)->h_refcount),
1023 EXT2_XATTR_REFCOUNT_MAX);
1024 } else if (!ext2_xattr_cmp(header, HDR(bh))) {
1025 ea_bdebug(bh, "b_count=%d",
1026 atomic_read(&(bh->b_count)));
1027 mb_cache_entry_release(ce);
1033 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1039 * ext2_xattr_cache_remove()
1041 * Remove the cache entry of a block from the cache. Called when a
1042 * block becomes invalid.
1045 ext2_xattr_cache_remove(struct buffer_head *bh)
1047 struct mb_cache_entry *ce;
1049 ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev, bh->b_blocknr);
1051 ea_bdebug(bh, "removing (%d cache entries remaining)",
1052 atomic_read(&ext2_xattr_cache->c_entry_count)-1);
1053 mb_cache_entry_free(ce);
1055 ea_bdebug(bh, "no cache entry");
1058 #define NAME_HASH_SHIFT 5
1059 #define VALUE_HASH_SHIFT 16
1062 * ext2_xattr_hash_entry()
1064 * Compute the hash of an extended attribute.
1066 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
1067 struct ext2_xattr_entry *entry)
1070 char *name = entry->e_name;
1073 for (n=0; n < entry->e_name_len; n++) {
1074 hash = (hash << NAME_HASH_SHIFT) ^
1075 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1079 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1080 __u32 *value = (__u32 *)((char *)header +
1081 le16_to_cpu(entry->e_value_offs));
1082 for (n = (le32_to_cpu(entry->e_value_size) +
1083 EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
1084 hash = (hash << VALUE_HASH_SHIFT) ^
1085 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1086 le32_to_cpu(*value++);
1089 entry->e_hash = cpu_to_le32(hash);
1092 #undef NAME_HASH_SHIFT
1093 #undef VALUE_HASH_SHIFT
1095 #define BLOCK_HASH_SHIFT 16
1098 * ext2_xattr_rehash()
1100 * Re-compute the extended attribute hash value after an entry has changed.
1102 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
1103 struct ext2_xattr_entry *entry)
1105 struct ext2_xattr_entry *here;
1108 ext2_xattr_hash_entry(header, entry);
1109 here = ENTRY(header+1);
1110 while (!IS_LAST_ENTRY(here)) {
1111 if (!here->e_hash) {
1112 /* Block is not shared if an entry's hash value == 0 */
1116 hash = (hash << BLOCK_HASH_SHIFT) ^
1117 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1118 le32_to_cpu(here->e_hash);
1119 here = EXT2_XATTR_NEXT(here);
1121 header->h_hash = cpu_to_le32(hash);
1124 #undef BLOCK_HASH_SHIFT
1127 init_ext2_xattr(void)
1131 err = ext2_xattr_register(EXT2_XATTR_INDEX_USER,
1132 &ext2_xattr_user_handler);
1135 err = ext2_xattr_register(EXT2_XATTR_INDEX_TRUSTED,
1136 &ext2_xattr_trusted_handler);
1139 #ifdef CONFIG_EXT2_FS_SECURITY
1140 err = ext2_xattr_register(EXT2_XATTR_INDEX_SECURITY,
1141 &ext2_xattr_security_handler);
1145 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1146 err = init_ext2_acl();
1150 ext2_xattr_cache = mb_cache_create("ext2_xattr", NULL,
1151 sizeof(struct mb_cache_entry) +
1152 sizeof(struct mb_cache_entry_index), 1, 6);
1153 if (!ext2_xattr_cache) {
1159 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1163 #ifdef CONFIG_EXT2_FS_SECURITY
1164 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY,
1165 &ext2_xattr_security_handler);
1168 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED,
1169 &ext2_xattr_trusted_handler);
1171 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1172 &ext2_xattr_user_handler);
1177 exit_ext2_xattr(void)
1179 mb_cache_destroy(ext2_xattr_cache);
1180 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1183 #ifdef CONFIG_EXT2_FS_SECURITY
1184 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY,
1185 &ext2_xattr_security_handler);
1187 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED,
1188 &ext2_xattr_trusted_handler);
1189 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1190 &ext2_xattr_user_handler);