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>
64 /* These symbols may be needed by a module. */
65 EXPORT_SYMBOL(ext2_xattr_register);
66 EXPORT_SYMBOL(ext2_xattr_unregister);
67 EXPORT_SYMBOL(ext2_xattr_get);
68 EXPORT_SYMBOL(ext2_xattr_list);
69 EXPORT_SYMBOL(ext2_xattr_set);
71 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
72 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
73 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
74 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
76 #ifdef EXT2_XATTR_DEBUG
77 # define ea_idebug(inode, f...) do { \
78 printk(KERN_DEBUG "inode %s:%ld: ", \
79 inode->i_sb->s_id, inode->i_ino); \
83 # define ea_bdebug(bh, f...) do { \
84 char b[BDEVNAME_SIZE]; \
85 printk(KERN_DEBUG "block %s:%lu: ", \
86 bdevname(bh->b_bdev, b), \
87 (unsigned long) bh->b_blocknr); \
92 # define ea_idebug(f...)
93 # define ea_bdebug(f...)
96 static int ext2_xattr_set2(struct inode *, struct buffer_head *,
97 struct ext2_xattr_header *);
99 static int ext2_xattr_cache_insert(struct buffer_head *);
100 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
101 struct ext2_xattr_header *);
102 static void ext2_xattr_cache_remove(struct buffer_head *);
103 static void ext2_xattr_rehash(struct ext2_xattr_header *,
104 struct ext2_xattr_entry *);
106 static struct mb_cache *ext2_xattr_cache;
107 static struct ext2_xattr_handler *ext2_xattr_handlers[EXT2_XATTR_INDEX_MAX];
108 static rwlock_t ext2_handler_lock = RW_LOCK_UNLOCKED;
111 ext2_xattr_register(int name_index, struct ext2_xattr_handler *handler)
115 if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
116 write_lock(&ext2_handler_lock);
117 if (!ext2_xattr_handlers[name_index-1]) {
118 ext2_xattr_handlers[name_index-1] = handler;
121 write_unlock(&ext2_handler_lock);
127 ext2_xattr_unregister(int name_index, struct ext2_xattr_handler *handler)
129 if (name_index > 0 || name_index <= EXT2_XATTR_INDEX_MAX) {
130 write_lock(&ext2_handler_lock);
131 ext2_xattr_handlers[name_index-1] = NULL;
132 write_unlock(&ext2_handler_lock);
136 static inline const char *
137 strcmp_prefix(const char *a, const char *a_prefix)
139 while (*a_prefix && *a == *a_prefix) {
143 return *a_prefix ? NULL : a;
147 * Decode the extended attribute name, and translate it into
148 * the name_index and name suffix.
150 static struct ext2_xattr_handler *
151 ext2_xattr_resolve_name(const char **name)
153 struct ext2_xattr_handler *handler = NULL;
158 read_lock(&ext2_handler_lock);
159 for (i=0; i<EXT2_XATTR_INDEX_MAX; i++) {
160 if (ext2_xattr_handlers[i]) {
161 const char *n = strcmp_prefix(*name,
162 ext2_xattr_handlers[i]->prefix);
164 handler = ext2_xattr_handlers[i];
170 read_unlock(&ext2_handler_lock);
174 static inline struct ext2_xattr_handler *
175 ext2_xattr_handler(int name_index)
177 struct ext2_xattr_handler *handler = NULL;
178 if (name_index > 0 && name_index <= EXT2_XATTR_INDEX_MAX) {
179 read_lock(&ext2_handler_lock);
180 handler = ext2_xattr_handlers[name_index-1];
181 read_unlock(&ext2_handler_lock);
187 * Inode operation getxattr()
189 * dentry->d_inode->i_sem: don't care
192 ext2_getxattr(struct dentry *dentry, const char *name,
193 void *buffer, size_t size)
195 struct ext2_xattr_handler *handler;
196 struct inode *inode = dentry->d_inode;
198 handler = ext2_xattr_resolve_name(&name);
201 return handler->get(inode, name, buffer, size);
205 * Inode operation listxattr()
207 * dentry->d_inode->i_sem: don't care
210 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
212 return ext2_xattr_list(dentry->d_inode, buffer, size);
216 * Inode operation setxattr()
218 * dentry->d_inode->i_sem: down
221 ext2_setxattr(struct dentry *dentry, const char *name,
222 const void *value, size_t size, int flags)
224 struct ext2_xattr_handler *handler;
225 struct inode *inode = dentry->d_inode;
228 value = ""; /* empty EA, do not remove */
229 handler = ext2_xattr_resolve_name(&name);
232 return handler->set(inode, name, value, size, flags);
236 * Inode operation removexattr()
238 * dentry->d_inode->i_sem: down
241 ext2_removexattr(struct dentry *dentry, const char *name)
243 struct ext2_xattr_handler *handler;
244 struct inode *inode = dentry->d_inode;
246 handler = ext2_xattr_resolve_name(&name);
249 return handler->set(inode, name, NULL, 0, XATTR_REPLACE);
255 * Copy an extended attribute into the buffer
256 * provided, or compute the buffer size required.
257 * Buffer is NULL to compute the size of the buffer required.
259 * Returns a negative error number on failure, or the number of bytes
260 * used / required on success.
263 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
264 void *buffer, size_t buffer_size)
266 struct buffer_head *bh = NULL;
267 struct ext2_xattr_entry *entry;
268 size_t name_len, size;
272 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
273 name_index, name, buffer, (long)buffer_size);
277 down_read(&EXT2_I(inode)->xattr_sem);
279 if (!EXT2_I(inode)->i_file_acl)
281 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
282 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
286 ea_bdebug(bh, "b_count=%d, refcount=%d",
287 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
288 end = bh->b_data + bh->b_size;
289 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
290 HDR(bh)->h_blocks != cpu_to_le32(1)) {
291 bad_block: ext2_error(inode->i_sb, "ext2_xattr_get",
292 "inode %ld: bad block %d", inode->i_ino,
293 EXT2_I(inode)->i_file_acl);
297 /* find named attribute */
298 name_len = strlen(name);
303 entry = FIRST_ENTRY(bh);
304 while (!IS_LAST_ENTRY(entry)) {
305 struct ext2_xattr_entry *next =
306 EXT2_XATTR_NEXT(entry);
307 if ((char *)next >= end)
309 if (name_index == entry->e_name_index &&
310 name_len == entry->e_name_len &&
311 memcmp(name, entry->e_name, name_len) == 0)
315 /* Check the remaining name entries */
316 while (!IS_LAST_ENTRY(entry)) {
317 struct ext2_xattr_entry *next =
318 EXT2_XATTR_NEXT(entry);
319 if ((char *)next >= end)
323 if (ext2_xattr_cache_insert(bh))
324 ea_idebug(inode, "cache insert failed");
328 /* check the buffer size */
329 if (entry->e_value_block != 0)
331 size = le32_to_cpu(entry->e_value_size);
332 if (size > inode->i_sb->s_blocksize ||
333 le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
336 if (ext2_xattr_cache_insert(bh))
337 ea_idebug(inode, "cache insert failed");
340 if (size > buffer_size)
342 /* return value of attribute */
343 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
350 up_read(&EXT2_I(inode)->xattr_sem);
358 * Copy a list of attribute names into the buffer
359 * provided, or compute the buffer size required.
360 * Buffer is NULL to compute the size of the buffer required.
362 * Returns a negative error number on failure, or the number of bytes
363 * used / required on success.
366 ext2_xattr_list(struct inode *inode, char *buffer, size_t buffer_size)
368 struct buffer_head *bh = NULL;
369 struct ext2_xattr_entry *entry;
374 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
375 buffer, (long)buffer_size);
377 down_read(&EXT2_I(inode)->xattr_sem);
379 if (!EXT2_I(inode)->i_file_acl)
381 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
382 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
386 ea_bdebug(bh, "b_count=%d, refcount=%d",
387 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
388 end = bh->b_data + bh->b_size;
389 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
390 HDR(bh)->h_blocks != cpu_to_le32(1)) {
391 bad_block: ext2_error(inode->i_sb, "ext2_xattr_list",
392 "inode %ld: bad block %d", inode->i_ino,
393 EXT2_I(inode)->i_file_acl);
397 /* compute the size required for the list of attribute names */
398 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
399 entry = EXT2_XATTR_NEXT(entry)) {
400 struct ext2_xattr_handler *handler;
401 struct ext2_xattr_entry *next =
402 EXT2_XATTR_NEXT(entry);
403 if ((char *)next >= end)
406 handler = ext2_xattr_handler(entry->e_name_index);
408 size += handler->list(NULL, inode, entry->e_name,
412 if (ext2_xattr_cache_insert(bh))
413 ea_idebug(inode, "cache insert failed");
419 if (size > buffer_size)
423 /* list the attribute names */
425 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
426 entry = EXT2_XATTR_NEXT(entry)) {
427 struct ext2_xattr_handler *handler;
429 handler = ext2_xattr_handler(entry->e_name_index);
431 buf += handler->list(buf, inode, entry->e_name,
438 up_read(&EXT2_I(inode)->xattr_sem);
444 * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
447 static void ext2_xattr_update_super_block(struct super_block *sb)
449 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
453 EXT2_SB(sb)->s_es->s_feature_compat |=
454 cpu_to_le32(EXT2_FEATURE_COMPAT_EXT_ATTR);
456 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
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 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
474 const void *value, size_t value_len, int flags)
476 struct super_block *sb = inode->i_sb;
477 struct buffer_head *bh = NULL;
478 struct ext2_xattr_header *header = NULL;
479 struct ext2_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(&EXT2_I(inode)->xattr_sem);
511 if (EXT2_I(inode)->i_file_acl) {
512 /* The inode already has an extended attribute block. */
513 bh = sb_bread(sb, EXT2_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(EXT2_XATTR_MAGIC) ||
523 header->h_blocks != cpu_to_le32(1)) {
524 bad_block: ext2_error(sb, "ext2_xattr_set",
525 "inode %ld: bad block %d", inode->i_ino,
526 EXT2_I(inode)->i_file_acl);
530 /* Find the named attribute. */
531 here = FIRST_ENTRY(bh);
532 while (!IS_LAST_ENTRY(here)) {
533 struct ext2_xattr_entry *next = EXT2_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 ext2_xattr_entry *next = EXT2_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 ext2_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 += EXT2_XATTR_SIZE(size);
594 free += EXT2_XATTR_LEN(name_len);
597 if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
600 /* Here we know that we can set the new attribute. */
603 /* assert(header == HDR(bh)); */
605 if (header->h_refcount == cpu_to_le32(1)) {
606 ea_bdebug(bh, "modifying in-place");
607 ext2_xattr_cache_remove(bh);
608 /* keep the buffer locked while modifying it. */
613 ea_bdebug(bh, "cloning");
614 header = kmalloc(bh->b_size, GFP_KERNEL);
618 memcpy(header, HDR(bh), bh->b_size);
619 header->h_refcount = cpu_to_le32(1);
621 offset = (char *)here - bh->b_data;
622 here = ENTRY((char *)header + offset);
623 offset = (char *)last - bh->b_data;
624 last = ENTRY((char *)header + offset);
627 /* Allocate a buffer where we construct the new block. */
628 header = kmalloc(sb->s_blocksize, GFP_KERNEL);
632 memset(header, 0, sb->s_blocksize);
633 end = (char *)header + sb->s_blocksize;
634 header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
635 header->h_blocks = header->h_refcount = cpu_to_le32(1);
636 last = here = ENTRY(header+1);
639 /* Iff we are modifying the block in-place, bh is locked here. */
642 /* Insert the new name. */
643 size_t size = EXT2_XATTR_LEN(name_len);
644 size_t rest = (char *)last - (char *)here;
645 memmove((char *)here + size, here, rest);
646 memset(here, 0, size);
647 here->e_name_index = name_index;
648 here->e_name_len = name_len;
649 memcpy(here->e_name, name, name_len);
651 if (!here->e_value_block && here->e_value_size) {
652 char *first_val = (char *)header + min_offs;
653 size_t offs = le16_to_cpu(here->e_value_offs);
654 char *val = (char *)header + offs;
655 size_t size = EXT2_XATTR_SIZE(
656 le32_to_cpu(here->e_value_size));
658 if (size == EXT2_XATTR_SIZE(value_len)) {
659 /* The old and the new value have the same
660 size. Just replace. */
661 here->e_value_size = cpu_to_le32(value_len);
662 memset(val + size - EXT2_XATTR_PAD, 0,
663 EXT2_XATTR_PAD); /* Clear pad bytes. */
664 memcpy(val, value, value_len);
668 /* Remove the old value. */
669 memmove(first_val + size, first_val, val - first_val);
670 memset(first_val, 0, size);
671 here->e_value_offs = 0;
674 /* Adjust all value offsets. */
675 last = ENTRY(header+1);
676 while (!IS_LAST_ENTRY(last)) {
677 size_t o = le16_to_cpu(last->e_value_offs);
678 if (!last->e_value_block && o < offs)
680 cpu_to_le16(o + size);
681 last = EXT2_XATTR_NEXT(last);
685 /* Remove the old name. */
686 size_t size = EXT2_XATTR_LEN(name_len);
687 last = ENTRY((char *)last - size);
688 memmove(here, (char*)here + size,
689 (char*)last - (char*)here);
690 memset(last, 0, size);
695 /* Insert the new value. */
696 here->e_value_size = cpu_to_le32(value_len);
698 size_t size = EXT2_XATTR_SIZE(value_len);
699 char *val = (char *)header + min_offs - size;
701 cpu_to_le16((char *)val - (char *)header);
702 memset(val + size - EXT2_XATTR_PAD, 0,
703 EXT2_XATTR_PAD); /* Clear the pad bytes. */
704 memcpy(val, value, value_len);
709 if (IS_LAST_ENTRY(ENTRY(header+1))) {
710 /* This block is now empty. */
711 if (bh && header == HDR(bh))
712 unlock_buffer(bh); /* we were modifying in-place. */
713 error = ext2_xattr_set2(inode, bh, NULL);
715 ext2_xattr_rehash(header, here);
716 if (bh && header == HDR(bh))
717 unlock_buffer(bh); /* we were modifying in-place. */
718 error = ext2_xattr_set2(inode, bh, header);
723 if (!(bh && header == HDR(bh)))
725 up_write(&EXT2_I(inode)->xattr_sem);
731 * Second half of ext2_xattr_set(): Update the file system.
734 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
735 struct ext2_xattr_header *header)
737 struct super_block *sb = inode->i_sb;
738 struct buffer_head *new_bh = NULL;
742 new_bh = ext2_xattr_cache_find(inode, header);
744 /* We found an identical block in the cache. */
745 if (new_bh == old_bh) {
746 ea_bdebug(new_bh, "keeping this block");
748 /* The old block is released after updating
750 ea_bdebug(new_bh, "reusing block");
753 if (DQUOT_ALLOC_BLOCK(inode, 1)) {
754 unlock_buffer(new_bh);
757 HDR(new_bh)->h_refcount = cpu_to_le32(1 +
758 le32_to_cpu(HDR(new_bh)->h_refcount));
759 ea_bdebug(new_bh, "refcount now=%d",
760 le32_to_cpu(HDR(new_bh)->h_refcount));
762 unlock_buffer(new_bh);
763 } else if (old_bh && header == HDR(old_bh)) {
764 /* Keep this block. No need to lock the block as we
765 don't need to change the reference count. */
768 ext2_xattr_cache_insert(new_bh);
770 /* We need to allocate a new block */
771 int goal = le32_to_cpu(EXT2_SB(sb)->s_es->
772 s_first_data_block) +
773 EXT2_I(inode)->i_block_group *
774 EXT2_BLOCKS_PER_GROUP(sb);
775 int block = ext2_new_block(inode, goal, 0, 0, &error);
778 ea_idebug(inode, "creating block %d", block);
780 new_bh = sb_getblk(sb, block);
782 ext2_free_blocks(inode, block, 1);
787 memcpy(new_bh->b_data, header, new_bh->b_size);
788 set_buffer_uptodate(new_bh);
789 unlock_buffer(new_bh);
790 ext2_xattr_cache_insert(new_bh);
792 ext2_xattr_update_super_block(sb);
794 mark_buffer_dirty(new_bh);
795 if (IS_SYNC(inode)) {
796 sync_dirty_buffer(new_bh);
798 if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
803 /* Update the inode. */
804 EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
805 inode->i_ctime = CURRENT_TIME;
806 if (IS_SYNC(inode)) {
807 error = ext2_sync_inode (inode);
811 mark_inode_dirty(inode);
814 if (old_bh && old_bh != new_bh) {
816 * If there was an old block and we are no longer using it,
817 * release the old block.
820 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
821 /* Free the old block. */
822 ea_bdebug(old_bh, "freeing");
823 ext2_free_blocks(inode, old_bh->b_blocknr, 1);
824 /* We let our caller release old_bh, so we
825 * need to duplicate the buffer before. */
829 /* Decrement the refcount only. */
830 HDR(old_bh)->h_refcount = cpu_to_le32(
831 le32_to_cpu(HDR(old_bh)->h_refcount) - 1);
832 DQUOT_FREE_BLOCK(inode, 1);
833 mark_buffer_dirty(old_bh);
834 ea_bdebug(old_bh, "refcount now=%d",
835 le32_to_cpu(HDR(old_bh)->h_refcount));
837 unlock_buffer(old_bh);
847 * ext2_xattr_delete_inode()
849 * Free extended attribute resources associated with this inode. This
850 * is called immediately before an inode is freed.
853 ext2_xattr_delete_inode(struct inode *inode)
855 struct buffer_head *bh = NULL;
857 down_write(&EXT2_I(inode)->xattr_sem);
858 if (!EXT2_I(inode)->i_file_acl)
860 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
862 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
863 "inode %ld: block %d read error", inode->i_ino,
864 EXT2_I(inode)->i_file_acl);
867 ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
868 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
869 HDR(bh)->h_blocks != cpu_to_le32(1)) {
870 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
871 "inode %ld: bad block %d", inode->i_ino,
872 EXT2_I(inode)->i_file_acl);
876 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
877 ext2_xattr_cache_remove(bh);
878 ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
882 HDR(bh)->h_refcount = cpu_to_le32(
883 le32_to_cpu(HDR(bh)->h_refcount) - 1);
884 mark_buffer_dirty(bh);
886 sync_dirty_buffer(bh);
887 DQUOT_FREE_BLOCK(inode, 1);
889 ea_bdebug(bh, "refcount now=%d", le32_to_cpu(HDR(bh)->h_refcount) - 1);
891 EXT2_I(inode)->i_file_acl = 0;
895 up_write(&EXT2_I(inode)->xattr_sem);
899 * ext2_xattr_put_super()
901 * This is called when a file system is unmounted.
904 ext2_xattr_put_super(struct super_block *sb)
906 mb_cache_shrink(ext2_xattr_cache, sb->s_bdev);
911 * ext2_xattr_cache_insert()
913 * Create a new entry in the extended attribute cache, and insert
914 * it unless such an entry is already in the cache.
916 * Returns 0, or a negative error number on failure.
919 ext2_xattr_cache_insert(struct buffer_head *bh)
921 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
922 struct mb_cache_entry *ce;
925 ce = mb_cache_entry_alloc(ext2_xattr_cache);
928 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, &hash);
930 mb_cache_entry_free(ce);
931 if (error == -EBUSY) {
932 ea_bdebug(bh, "already in cache (%d cache entries)",
933 atomic_read(&ext2_xattr_cache->c_entry_count));
937 ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
938 atomic_read(&ext2_xattr_cache->c_entry_count));
939 mb_cache_entry_release(ce);
947 * Compare two extended attribute blocks for equality.
949 * Returns 0 if the blocks are equal, 1 if they differ, and
950 * a negative error number on errors.
953 ext2_xattr_cmp(struct ext2_xattr_header *header1,
954 struct ext2_xattr_header *header2)
956 struct ext2_xattr_entry *entry1, *entry2;
958 entry1 = ENTRY(header1+1);
959 entry2 = ENTRY(header2+1);
960 while (!IS_LAST_ENTRY(entry1)) {
961 if (IS_LAST_ENTRY(entry2))
963 if (entry1->e_hash != entry2->e_hash ||
964 entry1->e_name_len != entry2->e_name_len ||
965 entry1->e_value_size != entry2->e_value_size ||
966 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
968 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
970 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
971 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
972 le32_to_cpu(entry1->e_value_size)))
975 entry1 = EXT2_XATTR_NEXT(entry1);
976 entry2 = EXT2_XATTR_NEXT(entry2);
978 if (!IS_LAST_ENTRY(entry2))
984 * ext2_xattr_cache_find()
986 * Find an identical extended attribute block.
988 * Returns a locked buffer head to the block found, or NULL if such
989 * a block was not found or an error occurred.
991 static struct buffer_head *
992 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
994 __u32 hash = le32_to_cpu(header->h_hash);
995 struct mb_cache_entry *ce;
998 return NULL; /* never share */
999 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
1000 ce = mb_cache_entry_find_first(ext2_xattr_cache, 0,
1001 inode->i_sb->s_bdev, hash);
1003 struct buffer_head *bh = sb_bread(inode->i_sb, ce->e_block);
1006 ext2_error(inode->i_sb, "ext2_xattr_cache_find",
1007 "inode %ld: block %ld read error",
1008 inode->i_ino, (unsigned long) ce->e_block);
1011 if (le32_to_cpu(HDR(bh)->h_refcount) >
1012 EXT2_XATTR_REFCOUNT_MAX) {
1013 ea_idebug(inode, "block %ld refcount %d>%d",
1014 (unsigned long) ce->e_block,
1015 le32_to_cpu(HDR(bh)->h_refcount),
1016 EXT2_XATTR_REFCOUNT_MAX);
1017 } else if (!ext2_xattr_cmp(header, HDR(bh))) {
1018 ea_bdebug(bh, "b_count=%d",
1019 atomic_read(&(bh->b_count)));
1020 mb_cache_entry_release(ce);
1026 ce = mb_cache_entry_find_next(ce, 0, inode->i_sb->s_bdev, hash);
1032 * ext2_xattr_cache_remove()
1034 * Remove the cache entry of a block from the cache. Called when a
1035 * block becomes invalid.
1038 ext2_xattr_cache_remove(struct buffer_head *bh)
1040 struct mb_cache_entry *ce;
1042 ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev, bh->b_blocknr);
1044 ea_bdebug(bh, "removing (%d cache entries remaining)",
1045 atomic_read(&ext2_xattr_cache->c_entry_count)-1);
1046 mb_cache_entry_free(ce);
1048 ea_bdebug(bh, "no cache entry");
1051 #define NAME_HASH_SHIFT 5
1052 #define VALUE_HASH_SHIFT 16
1055 * ext2_xattr_hash_entry()
1057 * Compute the hash of an extended attribute.
1059 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
1060 struct ext2_xattr_entry *entry)
1063 char *name = entry->e_name;
1066 for (n=0; n < entry->e_name_len; n++) {
1067 hash = (hash << NAME_HASH_SHIFT) ^
1068 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
1072 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
1073 __u32 *value = (__u32 *)((char *)header +
1074 le16_to_cpu(entry->e_value_offs));
1075 for (n = (le32_to_cpu(entry->e_value_size) +
1076 EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
1077 hash = (hash << VALUE_HASH_SHIFT) ^
1078 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
1079 le32_to_cpu(*value++);
1082 entry->e_hash = cpu_to_le32(hash);
1085 #undef NAME_HASH_SHIFT
1086 #undef VALUE_HASH_SHIFT
1088 #define BLOCK_HASH_SHIFT 16
1091 * ext2_xattr_rehash()
1093 * Re-compute the extended attribute hash value after an entry has changed.
1095 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
1096 struct ext2_xattr_entry *entry)
1098 struct ext2_xattr_entry *here;
1101 ext2_xattr_hash_entry(header, entry);
1102 here = ENTRY(header+1);
1103 while (!IS_LAST_ENTRY(here)) {
1104 if (!here->e_hash) {
1105 /* Block is not shared if an entry's hash value == 0 */
1109 hash = (hash << BLOCK_HASH_SHIFT) ^
1110 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1111 le32_to_cpu(here->e_hash);
1112 here = EXT2_XATTR_NEXT(here);
1114 header->h_hash = cpu_to_le32(hash);
1117 #undef BLOCK_HASH_SHIFT
1120 init_ext2_xattr(void)
1124 err = ext2_xattr_register(EXT2_XATTR_INDEX_USER,
1125 &ext2_xattr_user_handler);
1128 err = ext2_xattr_register(EXT2_XATTR_INDEX_TRUSTED,
1129 &ext2_xattr_trusted_handler);
1132 #ifdef CONFIG_EXT2_FS_SECURITY
1133 err = ext2_xattr_register(EXT2_XATTR_INDEX_SECURITY,
1134 &ext2_xattr_security_handler);
1138 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1139 err = init_ext2_acl();
1143 ext2_xattr_cache = mb_cache_create("ext2_xattr", NULL,
1144 sizeof(struct mb_cache_entry) +
1145 sizeof(struct mb_cache_entry_index), 1, 6);
1146 if (!ext2_xattr_cache) {
1152 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1156 #ifdef CONFIG_EXT2_FS_SECURITY
1157 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY,
1158 &ext2_xattr_security_handler);
1161 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED,
1162 &ext2_xattr_trusted_handler);
1164 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1165 &ext2_xattr_user_handler);
1170 exit_ext2_xattr(void)
1172 mb_cache_destroy(ext2_xattr_cache);
1173 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1176 #ifdef CONFIG_EXT2_FS_SECURITY
1177 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY,
1178 &ext2_xattr_security_handler);
1180 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED,
1181 &ext2_xattr_trusted_handler);
1182 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER,
1183 &ext2_xattr_user_handler);