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>.
9 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
15 * Extended attributes are stored on disk blocks allocated outside of
16 * any inode. The i_file_acl field is then made to point to this allocated
17 * block. If all extended attributes of an inode are identical, these
18 * inodes may share the same extended attribute block. Such situations
19 * are automatically detected by keeping a cache of recent attribute block
20 * numbers and hashes over the block's contents in memory.
23 * Extended attribute block layout:
25 * +------------------+
28 * | entry 2 | | growing downwards
33 * | value 3 | | growing upwards
35 * +------------------+
37 * The block header is followed by multiple entry descriptors. These entry
38 * descriptors are variable in size, and aligned to EXT2_XATTR_PAD
39 * byte boundaries. The entry descriptors are sorted by attribute name,
40 * so that two extended attribute blocks can be compared efficiently.
42 * Attribute values are aligned to the end of the block, stored in
43 * no specific order. They are also padded to EXT2_XATTR_PAD byte
44 * boundaries. No additional gaps are left between them.
48 * EXT2_I(inode)->i_file_acl is protected by EXT2_I(inode)->xattr_sem.
49 * EA blocks are only changed if they are exclusive to an inode, so
50 * holding xattr_sem also means that nothing but the EA block's reference
51 * count will change. Multiple writers to an EA block are synchronized
52 * by the bh lock. No more than a single bh lock is held at any time
56 #include <linux/buffer_head.h>
57 #include <linux/init.h>
58 #include <linux/slab.h>
59 #include <linux/mbcache.h>
60 #include <linux/quotaops.h>
61 #include <linux/rwsem.h>
62 #include <linux/security.h>
67 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
68 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
69 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
70 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
72 #ifdef EXT2_XATTR_DEBUG
73 # define ea_idebug(inode, f...) do { \
74 printk(KERN_DEBUG "inode %s:%ld: ", \
75 inode->i_sb->s_id, inode->i_ino); \
79 # define ea_bdebug(bh, f...) do { \
80 printk(KERN_DEBUG "block %pg:%lu: ", \
81 bh->b_bdev, (unsigned long) bh->b_blocknr); \
86 # define ea_idebug(f...)
87 # define ea_bdebug(f...)
90 static int ext2_xattr_set2(struct inode *, struct buffer_head *,
91 struct ext2_xattr_header *);
93 static int ext2_xattr_cache_insert(struct buffer_head *);
94 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
95 struct ext2_xattr_header *);
96 static void ext2_xattr_rehash(struct ext2_xattr_header *,
97 struct ext2_xattr_entry *);
99 static struct mb_cache *ext2_xattr_cache;
101 static const struct xattr_handler *ext2_xattr_handler_map[] = {
102 [EXT2_XATTR_INDEX_USER] = &ext2_xattr_user_handler,
103 #ifdef CONFIG_EXT2_FS_POSIX_ACL
104 [EXT2_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
105 [EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
107 [EXT2_XATTR_INDEX_TRUSTED] = &ext2_xattr_trusted_handler,
108 #ifdef CONFIG_EXT2_FS_SECURITY
109 [EXT2_XATTR_INDEX_SECURITY] = &ext2_xattr_security_handler,
113 const struct xattr_handler *ext2_xattr_handlers[] = {
114 &ext2_xattr_user_handler,
115 &ext2_xattr_trusted_handler,
116 #ifdef CONFIG_EXT2_FS_POSIX_ACL
117 &posix_acl_access_xattr_handler,
118 &posix_acl_default_xattr_handler,
120 #ifdef CONFIG_EXT2_FS_SECURITY
121 &ext2_xattr_security_handler,
126 static inline const struct xattr_handler *
127 ext2_xattr_handler(int name_index)
129 const struct xattr_handler *handler = NULL;
131 if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
132 handler = ext2_xattr_handler_map[name_index];
139 * Copy an extended attribute into the buffer
140 * provided, or compute the buffer size required.
141 * Buffer is NULL to compute the size of the buffer required.
143 * Returns a negative error number on failure, or the number of bytes
144 * used / required on success.
147 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
148 void *buffer, size_t buffer_size)
150 struct buffer_head *bh = NULL;
151 struct ext2_xattr_entry *entry;
152 size_t name_len, size;
156 ea_idebug(inode, "name=%d.%s, buffer=%p, buffer_size=%ld",
157 name_index, name, buffer, (long)buffer_size);
161 name_len = strlen(name);
165 down_read(&EXT2_I(inode)->xattr_sem);
167 if (!EXT2_I(inode)->i_file_acl)
169 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
170 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
174 ea_bdebug(bh, "b_count=%d, refcount=%d",
175 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
176 end = bh->b_data + bh->b_size;
177 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
178 HDR(bh)->h_blocks != cpu_to_le32(1)) {
179 bad_block: ext2_error(inode->i_sb, "ext2_xattr_get",
180 "inode %ld: bad block %d", inode->i_ino,
181 EXT2_I(inode)->i_file_acl);
186 /* find named attribute */
187 entry = FIRST_ENTRY(bh);
188 while (!IS_LAST_ENTRY(entry)) {
189 struct ext2_xattr_entry *next =
190 EXT2_XATTR_NEXT(entry);
191 if ((char *)next >= end)
193 if (name_index == entry->e_name_index &&
194 name_len == entry->e_name_len &&
195 memcmp(name, entry->e_name, name_len) == 0)
199 if (ext2_xattr_cache_insert(bh))
200 ea_idebug(inode, "cache insert failed");
204 /* check the buffer size */
205 if (entry->e_value_block != 0)
207 size = le32_to_cpu(entry->e_value_size);
208 if (size > inode->i_sb->s_blocksize ||
209 le16_to_cpu(entry->e_value_offs) + size > inode->i_sb->s_blocksize)
212 if (ext2_xattr_cache_insert(bh))
213 ea_idebug(inode, "cache insert failed");
216 if (size > buffer_size)
218 /* return value of attribute */
219 memcpy(buffer, bh->b_data + le16_to_cpu(entry->e_value_offs),
226 up_read(&EXT2_I(inode)->xattr_sem);
234 * Copy a list of attribute names into the buffer
235 * provided, or compute the buffer size required.
236 * Buffer is NULL to compute the size of the buffer required.
238 * Returns a negative error number on failure, or the number of bytes
239 * used / required on success.
242 ext2_xattr_list(struct dentry *dentry, char *buffer, size_t buffer_size)
244 struct inode *inode = d_inode(dentry);
245 struct buffer_head *bh = NULL;
246 struct ext2_xattr_entry *entry;
248 size_t rest = buffer_size;
251 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
252 buffer, (long)buffer_size);
254 down_read(&EXT2_I(inode)->xattr_sem);
256 if (!EXT2_I(inode)->i_file_acl)
258 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
259 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
263 ea_bdebug(bh, "b_count=%d, refcount=%d",
264 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
265 end = bh->b_data + bh->b_size;
266 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
267 HDR(bh)->h_blocks != cpu_to_le32(1)) {
268 bad_block: ext2_error(inode->i_sb, "ext2_xattr_list",
269 "inode %ld: bad block %d", inode->i_ino,
270 EXT2_I(inode)->i_file_acl);
275 /* check the on-disk data structure */
276 entry = FIRST_ENTRY(bh);
277 while (!IS_LAST_ENTRY(entry)) {
278 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(entry);
280 if ((char *)next >= end)
284 if (ext2_xattr_cache_insert(bh))
285 ea_idebug(inode, "cache insert failed");
287 /* list the attribute names */
288 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
289 entry = EXT2_XATTR_NEXT(entry)) {
290 const struct xattr_handler *handler =
291 ext2_xattr_handler(entry->e_name_index);
294 size_t size = handler->list(handler, dentry, buffer,
307 error = buffer_size - rest; /* total size */
311 up_read(&EXT2_I(inode)->xattr_sem);
317 * Inode operation listxattr()
319 * d_inode(dentry)->i_mutex: don't care
322 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
324 return ext2_xattr_list(dentry, buffer, size);
328 * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
331 static void ext2_xattr_update_super_block(struct super_block *sb)
333 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
336 spin_lock(&EXT2_SB(sb)->s_lock);
337 EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
338 spin_unlock(&EXT2_SB(sb)->s_lock);
339 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
345 * Create, replace or remove an extended attribute for this inode. Value
346 * is NULL to remove an existing extended attribute, and non-NULL to
347 * either replace an existing extended attribute, or create a new extended
348 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
349 * specify that an extended attribute must exist and must not exist
350 * previous to the call, respectively.
352 * Returns 0, or a negative error number on failure.
355 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
356 const void *value, size_t value_len, int flags)
358 struct super_block *sb = inode->i_sb;
359 struct buffer_head *bh = NULL;
360 struct ext2_xattr_header *header = NULL;
361 struct ext2_xattr_entry *here, *last;
362 size_t name_len, free, min_offs = sb->s_blocksize;
363 int not_found = 1, error;
367 * header -- Points either into bh, or to a temporarily
369 * here -- The named entry found, or the place for inserting, within
370 * the block pointed to by header.
371 * last -- Points right after the last named entry within the block
372 * pointed to by header.
373 * min_offs -- The offset of the first value (values are aligned
374 * towards the end of the block).
375 * end -- Points right after the block pointed to by header.
378 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
379 name_index, name, value, (long)value_len);
385 name_len = strlen(name);
386 if (name_len > 255 || value_len > sb->s_blocksize)
388 down_write(&EXT2_I(inode)->xattr_sem);
389 if (EXT2_I(inode)->i_file_acl) {
390 /* The inode already has an extended attribute block. */
391 bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
395 ea_bdebug(bh, "b_count=%d, refcount=%d",
396 atomic_read(&(bh->b_count)),
397 le32_to_cpu(HDR(bh)->h_refcount));
399 end = bh->b_data + bh->b_size;
400 if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
401 header->h_blocks != cpu_to_le32(1)) {
402 bad_block: ext2_error(sb, "ext2_xattr_set",
403 "inode %ld: bad block %d", inode->i_ino,
404 EXT2_I(inode)->i_file_acl);
408 /* Find the named attribute. */
409 here = FIRST_ENTRY(bh);
410 while (!IS_LAST_ENTRY(here)) {
411 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(here);
412 if ((char *)next >= end)
414 if (!here->e_value_block && here->e_value_size) {
415 size_t offs = le16_to_cpu(here->e_value_offs);
419 not_found = name_index - here->e_name_index;
421 not_found = name_len - here->e_name_len;
423 not_found = memcmp(name, here->e_name,name_len);
429 /* We still need to compute min_offs and last. */
430 while (!IS_LAST_ENTRY(last)) {
431 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
432 if ((char *)next >= end)
434 if (!last->e_value_block && last->e_value_size) {
435 size_t offs = le16_to_cpu(last->e_value_offs);
442 /* Check whether we have enough space left. */
443 free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
445 /* We will use a new extended attribute block. */
446 free = sb->s_blocksize -
447 sizeof(struct ext2_xattr_header) - sizeof(__u32);
448 here = last = NULL; /* avoid gcc uninitialized warning. */
452 /* Request to remove a nonexistent attribute? */
454 if (flags & XATTR_REPLACE)
460 /* Request to create an existing attribute? */
462 if (flags & XATTR_CREATE)
464 if (!here->e_value_block && here->e_value_size) {
465 size_t size = le32_to_cpu(here->e_value_size);
467 if (le16_to_cpu(here->e_value_offs) + size >
468 sb->s_blocksize || size > sb->s_blocksize)
470 free += EXT2_XATTR_SIZE(size);
472 free += EXT2_XATTR_LEN(name_len);
475 if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
478 /* Here we know that we can set the new attribute. */
481 struct mb_cache_entry *ce;
483 /* assert(header == HDR(bh)); */
484 ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev,
487 if (header->h_refcount == cpu_to_le32(1)) {
488 ea_bdebug(bh, "modifying in-place");
490 mb_cache_entry_free(ce);
491 /* keep the buffer locked while modifying it. */
496 mb_cache_entry_release(ce);
498 ea_bdebug(bh, "cloning");
499 header = kmalloc(bh->b_size, GFP_KERNEL);
503 memcpy(header, HDR(bh), bh->b_size);
504 header->h_refcount = cpu_to_le32(1);
506 offset = (char *)here - bh->b_data;
507 here = ENTRY((char *)header + offset);
508 offset = (char *)last - bh->b_data;
509 last = ENTRY((char *)header + offset);
512 /* Allocate a buffer where we construct the new block. */
513 header = kzalloc(sb->s_blocksize, GFP_KERNEL);
517 end = (char *)header + sb->s_blocksize;
518 header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
519 header->h_blocks = header->h_refcount = cpu_to_le32(1);
520 last = here = ENTRY(header+1);
523 /* Iff we are modifying the block in-place, bh is locked here. */
526 /* Insert the new name. */
527 size_t size = EXT2_XATTR_LEN(name_len);
528 size_t rest = (char *)last - (char *)here;
529 memmove((char *)here + size, here, rest);
530 memset(here, 0, size);
531 here->e_name_index = name_index;
532 here->e_name_len = name_len;
533 memcpy(here->e_name, name, name_len);
535 if (!here->e_value_block && here->e_value_size) {
536 char *first_val = (char *)header + min_offs;
537 size_t offs = le16_to_cpu(here->e_value_offs);
538 char *val = (char *)header + offs;
539 size_t size = EXT2_XATTR_SIZE(
540 le32_to_cpu(here->e_value_size));
542 if (size == EXT2_XATTR_SIZE(value_len)) {
543 /* The old and the new value have the same
544 size. Just replace. */
545 here->e_value_size = cpu_to_le32(value_len);
546 memset(val + size - EXT2_XATTR_PAD, 0,
547 EXT2_XATTR_PAD); /* Clear pad bytes. */
548 memcpy(val, value, value_len);
552 /* Remove the old value. */
553 memmove(first_val + size, first_val, val - first_val);
554 memset(first_val, 0, size);
555 here->e_value_offs = 0;
558 /* Adjust all value offsets. */
559 last = ENTRY(header+1);
560 while (!IS_LAST_ENTRY(last)) {
561 size_t o = le16_to_cpu(last->e_value_offs);
562 if (!last->e_value_block && o < offs)
564 cpu_to_le16(o + size);
565 last = EXT2_XATTR_NEXT(last);
569 /* Remove the old name. */
570 size_t size = EXT2_XATTR_LEN(name_len);
571 last = ENTRY((char *)last - size);
572 memmove(here, (char*)here + size,
573 (char*)last - (char*)here);
574 memset(last, 0, size);
579 /* Insert the new value. */
580 here->e_value_size = cpu_to_le32(value_len);
582 size_t size = EXT2_XATTR_SIZE(value_len);
583 char *val = (char *)header + min_offs - size;
585 cpu_to_le16((char *)val - (char *)header);
586 memset(val + size - EXT2_XATTR_PAD, 0,
587 EXT2_XATTR_PAD); /* Clear the pad bytes. */
588 memcpy(val, value, value_len);
593 if (IS_LAST_ENTRY(ENTRY(header+1))) {
594 /* This block is now empty. */
595 if (bh && header == HDR(bh))
596 unlock_buffer(bh); /* we were modifying in-place. */
597 error = ext2_xattr_set2(inode, bh, NULL);
599 ext2_xattr_rehash(header, here);
600 if (bh && header == HDR(bh))
601 unlock_buffer(bh); /* we were modifying in-place. */
602 error = ext2_xattr_set2(inode, bh, header);
607 if (!(bh && header == HDR(bh)))
609 up_write(&EXT2_I(inode)->xattr_sem);
615 * Second half of ext2_xattr_set(): Update the file system.
618 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
619 struct ext2_xattr_header *header)
621 struct super_block *sb = inode->i_sb;
622 struct buffer_head *new_bh = NULL;
626 new_bh = ext2_xattr_cache_find(inode, header);
628 /* We found an identical block in the cache. */
629 if (new_bh == old_bh) {
630 ea_bdebug(new_bh, "keeping this block");
632 /* The old block is released after updating
634 ea_bdebug(new_bh, "reusing block");
636 error = dquot_alloc_block(inode, 1);
638 unlock_buffer(new_bh);
641 le32_add_cpu(&HDR(new_bh)->h_refcount, 1);
642 ea_bdebug(new_bh, "refcount now=%d",
643 le32_to_cpu(HDR(new_bh)->h_refcount));
645 unlock_buffer(new_bh);
646 } else if (old_bh && header == HDR(old_bh)) {
647 /* Keep this block. No need to lock the block as we
648 don't need to change the reference count. */
651 ext2_xattr_cache_insert(new_bh);
653 /* We need to allocate a new block */
654 ext2_fsblk_t goal = ext2_group_first_block_no(sb,
655 EXT2_I(inode)->i_block_group);
656 int block = ext2_new_block(inode, goal, &error);
659 ea_idebug(inode, "creating block %d", block);
661 new_bh = sb_getblk(sb, block);
662 if (unlikely(!new_bh)) {
663 ext2_free_blocks(inode, block, 1);
664 mark_inode_dirty(inode);
669 memcpy(new_bh->b_data, header, new_bh->b_size);
670 set_buffer_uptodate(new_bh);
671 unlock_buffer(new_bh);
672 ext2_xattr_cache_insert(new_bh);
674 ext2_xattr_update_super_block(sb);
676 mark_buffer_dirty(new_bh);
677 if (IS_SYNC(inode)) {
678 sync_dirty_buffer(new_bh);
680 if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
685 /* Update the inode. */
686 EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
687 inode->i_ctime = CURRENT_TIME_SEC;
688 if (IS_SYNC(inode)) {
689 error = sync_inode_metadata(inode, 1);
690 /* In case sync failed due to ENOSPC the inode was actually
691 * written (only some dirty data were not) so we just proceed
692 * as if nothing happened and cleanup the unused block */
693 if (error && error != -ENOSPC) {
694 if (new_bh && new_bh != old_bh) {
695 dquot_free_block_nodirty(inode, 1);
696 mark_inode_dirty(inode);
701 mark_inode_dirty(inode);
704 if (old_bh && old_bh != new_bh) {
705 struct mb_cache_entry *ce;
708 * If there was an old block and we are no longer using it,
709 * release the old block.
711 ce = mb_cache_entry_get(ext2_xattr_cache, old_bh->b_bdev,
714 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
715 /* Free the old block. */
717 mb_cache_entry_free(ce);
718 ea_bdebug(old_bh, "freeing");
719 ext2_free_blocks(inode, old_bh->b_blocknr, 1);
720 mark_inode_dirty(inode);
721 /* We let our caller release old_bh, so we
722 * need to duplicate the buffer before. */
726 /* Decrement the refcount only. */
727 le32_add_cpu(&HDR(old_bh)->h_refcount, -1);
729 mb_cache_entry_release(ce);
730 dquot_free_block_nodirty(inode, 1);
731 mark_inode_dirty(inode);
732 mark_buffer_dirty(old_bh);
733 ea_bdebug(old_bh, "refcount now=%d",
734 le32_to_cpu(HDR(old_bh)->h_refcount));
736 unlock_buffer(old_bh);
746 * ext2_xattr_delete_inode()
748 * Free extended attribute resources associated with this inode. This
749 * is called immediately before an inode is freed.
752 ext2_xattr_delete_inode(struct inode *inode)
754 struct buffer_head *bh = NULL;
755 struct mb_cache_entry *ce;
757 down_write(&EXT2_I(inode)->xattr_sem);
758 if (!EXT2_I(inode)->i_file_acl)
760 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
762 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
763 "inode %ld: block %d read error", inode->i_ino,
764 EXT2_I(inode)->i_file_acl);
767 ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
768 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
769 HDR(bh)->h_blocks != cpu_to_le32(1)) {
770 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
771 "inode %ld: bad block %d", inode->i_ino,
772 EXT2_I(inode)->i_file_acl);
775 ce = mb_cache_entry_get(ext2_xattr_cache, bh->b_bdev, bh->b_blocknr);
777 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
779 mb_cache_entry_free(ce);
780 ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
785 le32_add_cpu(&HDR(bh)->h_refcount, -1);
787 mb_cache_entry_release(ce);
788 ea_bdebug(bh, "refcount now=%d",
789 le32_to_cpu(HDR(bh)->h_refcount));
791 mark_buffer_dirty(bh);
793 sync_dirty_buffer(bh);
794 dquot_free_block_nodirty(inode, 1);
796 EXT2_I(inode)->i_file_acl = 0;
800 up_write(&EXT2_I(inode)->xattr_sem);
804 * ext2_xattr_put_super()
806 * This is called when a file system is unmounted.
809 ext2_xattr_put_super(struct super_block *sb)
811 mb_cache_shrink(sb->s_bdev);
816 * ext2_xattr_cache_insert()
818 * Create a new entry in the extended attribute cache, and insert
819 * it unless such an entry is already in the cache.
821 * Returns 0, or a negative error number on failure.
824 ext2_xattr_cache_insert(struct buffer_head *bh)
826 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
827 struct mb_cache_entry *ce;
830 ce = mb_cache_entry_alloc(ext2_xattr_cache, GFP_NOFS);
833 error = mb_cache_entry_insert(ce, bh->b_bdev, bh->b_blocknr, hash);
835 mb_cache_entry_free(ce);
836 if (error == -EBUSY) {
837 ea_bdebug(bh, "already in cache (%d cache entries)",
838 atomic_read(&ext2_xattr_cache->c_entry_count));
842 ea_bdebug(bh, "inserting [%x] (%d cache entries)", (int)hash,
843 atomic_read(&ext2_xattr_cache->c_entry_count));
844 mb_cache_entry_release(ce);
852 * Compare two extended attribute blocks for equality.
854 * Returns 0 if the blocks are equal, 1 if they differ, and
855 * a negative error number on errors.
858 ext2_xattr_cmp(struct ext2_xattr_header *header1,
859 struct ext2_xattr_header *header2)
861 struct ext2_xattr_entry *entry1, *entry2;
863 entry1 = ENTRY(header1+1);
864 entry2 = ENTRY(header2+1);
865 while (!IS_LAST_ENTRY(entry1)) {
866 if (IS_LAST_ENTRY(entry2))
868 if (entry1->e_hash != entry2->e_hash ||
869 entry1->e_name_index != entry2->e_name_index ||
870 entry1->e_name_len != entry2->e_name_len ||
871 entry1->e_value_size != entry2->e_value_size ||
872 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
874 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
876 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
877 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
878 le32_to_cpu(entry1->e_value_size)))
881 entry1 = EXT2_XATTR_NEXT(entry1);
882 entry2 = EXT2_XATTR_NEXT(entry2);
884 if (!IS_LAST_ENTRY(entry2))
890 * ext2_xattr_cache_find()
892 * Find an identical extended attribute block.
894 * Returns a locked buffer head to the block found, or NULL if such
895 * a block was not found or an error occurred.
897 static struct buffer_head *
898 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
900 __u32 hash = le32_to_cpu(header->h_hash);
901 struct mb_cache_entry *ce;
904 return NULL; /* never share */
905 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
907 ce = mb_cache_entry_find_first(ext2_xattr_cache, inode->i_sb->s_bdev,
910 struct buffer_head *bh;
913 if (PTR_ERR(ce) == -EAGAIN)
918 bh = sb_bread(inode->i_sb, ce->e_block);
920 ext2_error(inode->i_sb, "ext2_xattr_cache_find",
921 "inode %ld: block %ld read error",
922 inode->i_ino, (unsigned long) ce->e_block);
925 if (le32_to_cpu(HDR(bh)->h_refcount) >
926 EXT2_XATTR_REFCOUNT_MAX) {
927 ea_idebug(inode, "block %ld refcount %d>%d",
928 (unsigned long) ce->e_block,
929 le32_to_cpu(HDR(bh)->h_refcount),
930 EXT2_XATTR_REFCOUNT_MAX);
931 } else if (!ext2_xattr_cmp(header, HDR(bh))) {
932 ea_bdebug(bh, "b_count=%d",
933 atomic_read(&(bh->b_count)));
934 mb_cache_entry_release(ce);
940 ce = mb_cache_entry_find_next(ce, inode->i_sb->s_bdev, hash);
945 #define NAME_HASH_SHIFT 5
946 #define VALUE_HASH_SHIFT 16
949 * ext2_xattr_hash_entry()
951 * Compute the hash of an extended attribute.
953 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
954 struct ext2_xattr_entry *entry)
957 char *name = entry->e_name;
960 for (n=0; n < entry->e_name_len; n++) {
961 hash = (hash << NAME_HASH_SHIFT) ^
962 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
966 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
967 __le32 *value = (__le32 *)((char *)header +
968 le16_to_cpu(entry->e_value_offs));
969 for (n = (le32_to_cpu(entry->e_value_size) +
970 EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
971 hash = (hash << VALUE_HASH_SHIFT) ^
972 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
973 le32_to_cpu(*value++);
976 entry->e_hash = cpu_to_le32(hash);
979 #undef NAME_HASH_SHIFT
980 #undef VALUE_HASH_SHIFT
982 #define BLOCK_HASH_SHIFT 16
985 * ext2_xattr_rehash()
987 * Re-compute the extended attribute hash value after an entry has changed.
989 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
990 struct ext2_xattr_entry *entry)
992 struct ext2_xattr_entry *here;
995 ext2_xattr_hash_entry(header, entry);
996 here = ENTRY(header+1);
997 while (!IS_LAST_ENTRY(here)) {
999 /* Block is not shared if an entry's hash value == 0 */
1003 hash = (hash << BLOCK_HASH_SHIFT) ^
1004 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1005 le32_to_cpu(here->e_hash);
1006 here = EXT2_XATTR_NEXT(here);
1008 header->h_hash = cpu_to_le32(hash);
1011 #undef BLOCK_HASH_SHIFT
1014 init_ext2_xattr(void)
1016 ext2_xattr_cache = mb_cache_create("ext2_xattr", 6);
1017 if (!ext2_xattr_cache)
1023 exit_ext2_xattr(void)
1025 mb_cache_destroy(ext2_xattr_cache);