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,
784 ea_idebug(inode, "creating block %d", block);
786 new_bh = sb_getblk(sb, block);
788 ext2_free_blocks(inode, block, 1);
793 memcpy(new_bh->b_data, header, new_bh->b_size);
794 set_buffer_uptodate(new_bh);
795 unlock_buffer(new_bh);
796 ext2_xattr_cache_insert(new_bh);
798 ext2_xattr_update_super_block(sb);
800 mark_buffer_dirty(new_bh);
801 if (IS_SYNC(inode)) {
802 sync_dirty_buffer(new_bh);
804 if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
809 /* Update the inode. */
810 EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
811 inode->i_ctime = CURRENT_TIME;
812 if (IS_SYNC(inode)) {
813 error = ext2_sync_inode (inode);
817 mark_inode_dirty(inode);
820 if (old_bh && old_bh != new_bh) {
822 * If there was an old block and we are no longer using it,
823 * release the old block.
826 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
827 /* Free the old block. */
828 ea_bdebug(old_bh, "freeing");
829 ext2_free_blocks(inode, old_bh->b_blocknr, 1);
830 /* We let our caller release old_bh, so we
831 * need to duplicate the buffer before. */
835 /* Decrement the refcount only. */
836 HDR(old_bh)->h_refcount = cpu_to_le32(
837 le32_to_cpu(HDR(old_bh)->h_refcount) - 1);
838 DLIMIT_FREE_BLOCK(sb, inode->i_xid, 1);
839 DQUOT_FREE_BLOCK(inode, 1);
840 mark_buffer_dirty(old_bh);
841 ea_bdebug(old_bh, "refcount now=%d",
842 le32_to_cpu(HDR(old_bh)->h_refcount));
844 unlock_buffer(old_bh);
854 * ext2_xattr_delete_inode()
856 * Free extended attribute resources associated with this inode. This
857 * is called immediately before an inode is freed.
860 ext2_xattr_delete_inode(struct inode *inode)
862 struct buffer_head *bh = NULL;
864 down_write(&EXT2_I(inode)->xattr_sem);
865 if (!EXT2_I(inode)->i_file_acl)
867 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
869 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
870 "inode %ld: block %d read error", inode->i_ino,
871 EXT2_I(inode)->i_file_acl);
874 ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
875 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
876 HDR(bh)->h_blocks != cpu_to_le32(1)) {
877 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
878 "inode %ld: bad block %d", inode->i_ino,
879 EXT2_I(inode)->i_file_acl);
883 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
884 ext2_xattr_cache_remove(bh);
885 ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
889 HDR(bh)->h_refcount = cpu_to_le32(
890 le32_to_cpu(HDR(bh)->h_refcount) - 1);
891 mark_buffer_dirty(bh);
893 sync_dirty_buffer(bh);
894 DLIMIT_FREE_BLOCK(inode->i_sb, inode->i_xid, 1);
895 DQUOT_FREE_BLOCK(inode, 1);
897 ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
899 EXT2_I(inode)->i_file_acl = 0;
903 up_write(&EXT2_I(inode)->xattr_sem);
907 * ext2_xattr_put_super()
909 * This is called when a file system is unmounted.
912 ext2_xattr_put_super(struct super_block *sb)
914 mb_cache_shrink(ext2_xattr_cache, sb->s_bdev);
919 * ext2_xattr_cache_insert()
921 * Create a new entry in the extended attribute cache, and insert
922 * it unless such an entry is already in the cache.
924 * Returns 0, or a negative error number on failure.
927 ext2_xattr_cache_insert(struct buffer_head *bh)
929 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
930 struct mb_cache_entry *ce;
933 ce = mb_cache_entry_alloc(ext2_xattr_cache);
936 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
938 mb_cache_entry_free(ce);
939 if (error == -EBUSY) {
940 ea_bdebug(bh, "already in cache (%d cache entries)",
941 atomic_read(&ext2_xattr_cache->c_entry_count));
945 ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
946 atomic_read(&ext2_xattr_cache->c_entry_count));
947 mb_cache_entry_release(ce);
955 * Compare two extended attribute blocks for equality.
957 * Returns 0 if the blocks are equal, 1 if they differ, and
958 * a negative error number on errors.
961 ext2_xattr_cmp(struct ext2_xattr_header *header1,
962 struct ext2_xattr_header *header2)
964 struct ext2_xattr_entry *entry1, *entry2;
966 entry1 = ENTRY(header1+1);
967 entry2 = ENTRY(header2+1);
968 while (!IS_LAST_ENTRY(entry1)) {
969 if (IS_LAST_ENTRY(entry2))
971 if (entry1->e_hash != entry2->e_hash ||
972 entry1->e_name_len != entry2->e_name_len ||
973 entry1->e_value_size != entry2->e_value_size ||
974 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
976 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
978 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
979 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
980 le32_to_cpu(entry1->e_value_size)))
983 entry1 = EXT2_XATTR_NEXT(entry1);
984 entry2 = EXT2_XATTR_NEXT(entry2);
986 if (!IS_LAST_ENTRY(entry2))
992 * ext2_xattr_cache_find()
994 * Find an identical extended attribute block.
996 * Returns a locked buffer head to the block found, or NULL if such
997 * a block was not found or an error occurred.
999 static struct buffer_head *
1000 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
1002 __u32 hash = le32_to_cpu(header->h_hash);
1003 struct mb_cache_entry *ce;
1005 if (!header->h_hash)
1006 return NULL; /* never share */
1007 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1008 ce = mb_cache_entry_find_first(ext2_xattr_cache, 0,
1009 inode->i_sb->s_bdev, hash);
1011 struct buffer_head *bh = sb_bread(inode->i_sb, ce->e_block);
1014 ext2_error(inode->i_sb, "ext2_xattr_cache_find",
1015 "inode %ld: block %ld read error",
1016 inode->i_ino, (unsigned long) ce->e_block);
1019 if (le32_to_cpu(HDR(bh)->h_refcount) >
1020 EXT2_XATTR_REFCOUNT_MAX) {
1021 ea_idebug(inode, "block %ld refcount %d>%d",
1022 (unsigned long) ce->e_block,
1023 le32_to_cpu(HDR(bh)->h_refcount),
1024 EXT2_XATTR_REFCOUNT_MAX);
1025 } else if (!ext2_xattr_cmp(header, HDR(bh))) {
1026 ea_bdebug(bh, "b_count=%d",
1027 atomic_read(&(bh->b_count)));
1028 mb_cache_entry_release(ce);
1034 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1040 * ext2_xattr_cache_remove()
1042 * Remove the cache entry of a block from the cache. Called when a
1043 * block becomes invalid.
1046 ext2_xattr_cache_remove(struct buffer_head *bh)
1048 struct mb_cache_entry *ce;
1050 ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev, bh->b_blocknr);
1052 ea_bdebug(bh, "removing (%d cache entries remaining)",
1053 atomic_read(&ext2_xattr_cache->c_entry_count)-1);
1054 mb_cache_entry_free(ce);
1056 ea_bdebug(bh, "no cache entry");
1059 #define NAME_HASH_SHIFT 5
1060 #define VALUE_HASH_SHIFT 16
1063 * ext2_xattr_hash_entry()
1065 * Compute the hash of an extended attribute.
1067 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
1068 struct ext2_xattr_entry *entry)
1071 char *name = entry->e_name;
1074 for (n=0; n < entry->e_name_len; n++) {
1075 hash = (hash << NAME_HASH_SHIFT) ^
1076 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1080 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1081 __le32 *value = (__le32 *)((char *)header +
1082 le16_to_cpu(entry->e_value_offs));
1083 for (n = (le32_to_cpu(entry->e_value_size) +
1084 EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
1085 hash = (hash << VALUE_HASH_SHIFT) ^
1086 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1087 le32_to_cpu(*value++);
1090 entry->e_hash = cpu_to_le32(hash);
1093 #undef NAME_HASH_SHIFT
1094 #undef VALUE_HASH_SHIFT
1096 #define BLOCK_HASH_SHIFT 16
1099 * ext2_xattr_rehash()
1101 * Re-compute the extended attribute hash value after an entry has changed.
1103 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
1104 struct ext2_xattr_entry *entry)
1106 struct ext2_xattr_entry *here;
1109 ext2_xattr_hash_entry(header, entry);
1110 here = ENTRY(header+1);
1111 while (!IS_LAST_ENTRY(here)) {
1112 if (!here->e_hash) {
1113 /* Block is not shared if an entry's hash value == 0 */
1117 hash = (hash << BLOCK_HASH_SHIFT) ^
1118 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1119 le32_to_cpu(here->e_hash);
1120 here = EXT2_XATTR_NEXT(here);
1122 header->h_hash = cpu_to_le32(hash);
1125 #undef BLOCK_HASH_SHIFT
1128 init_ext2_xattr(void)
1132 err = ext2_xattr_register(EXT2_XATTR_INDEX_USER,
1133 &ext2_xattr_user_handler);
1136 err = ext2_xattr_register(EXT2_XATTR_INDEX_TRUSTED,
1137 &ext2_xattr_trusted_handler);
1140 #ifdef CONFIG_EXT2_FS_SECURITY
1141 err = ext2_xattr_register(EXT2_XATTR_INDEX_SECURITY,
1142 &ext2_xattr_security_handler);
1146 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1147 err = init_ext2_acl();
1151 ext2_xattr_cache = mb_cache_create("ext2_xattr", NULL,
1152 sizeof(struct mb_cache_entry) +
1153 sizeof(struct mb_cache_entry_index), 1, 6);
1154 if (!ext2_xattr_cache) {
1160 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1164 #ifdef CONFIG_EXT2_FS_SECURITY
1165 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY,
1166 &ext2_xattr_security_handler);
1169 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED,
1170 &ext2_xattr_trusted_handler);
1172 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1173 &ext2_xattr_user_handler);
1178 exit_ext2_xattr(void)
1180 mb_cache_destroy(ext2_xattr_cache);
1181 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1184 #ifdef CONFIG_EXT2_FS_SECURITY
1185 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY,
1186 &ext2_xattr_security_handler);
1188 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED,
1189 &ext2_xattr_trusted_handler);
1190 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1191 &ext2_xattr_user_handler);