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/printk.h>
59 #include <linux/slab.h>
60 #include <linux/mbcache.h>
61 #include <linux/quotaops.h>
62 #include <linux/rwsem.h>
63 #include <linux/security.h>
68 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
69 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
70 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
71 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
73 #ifdef EXT2_XATTR_DEBUG
74 # define ea_idebug(inode, f...) do { \
75 printk(KERN_DEBUG "inode %s:%ld: ", \
76 inode->i_sb->s_id, inode->i_ino); \
80 # define ea_bdebug(bh, f...) do { \
81 printk(KERN_DEBUG "block %pg:%lu: ", \
82 bh->b_bdev, (unsigned long) bh->b_blocknr); \
87 # define ea_idebug(inode, f...) no_printk(f)
88 # define ea_bdebug(bh, f...) no_printk(f)
91 static int ext2_xattr_set2(struct inode *, struct buffer_head *,
92 struct ext2_xattr_header *);
94 static int ext2_xattr_cache_insert(struct mb_cache *, struct buffer_head *);
95 static struct buffer_head *ext2_xattr_cache_find(struct inode *,
96 struct ext2_xattr_header *);
97 static void ext2_xattr_rehash(struct ext2_xattr_header *,
98 struct ext2_xattr_entry *);
100 static const struct xattr_handler *ext2_xattr_handler_map[] = {
101 [EXT2_XATTR_INDEX_USER] = &ext2_xattr_user_handler,
102 #ifdef CONFIG_EXT2_FS_POSIX_ACL
103 [EXT2_XATTR_INDEX_POSIX_ACL_ACCESS] = &posix_acl_access_xattr_handler,
104 [EXT2_XATTR_INDEX_POSIX_ACL_DEFAULT] = &posix_acl_default_xattr_handler,
106 [EXT2_XATTR_INDEX_TRUSTED] = &ext2_xattr_trusted_handler,
107 #ifdef CONFIG_EXT2_FS_SECURITY
108 [EXT2_XATTR_INDEX_SECURITY] = &ext2_xattr_security_handler,
112 const struct xattr_handler *ext2_xattr_handlers[] = {
113 &ext2_xattr_user_handler,
114 &ext2_xattr_trusted_handler,
115 #ifdef CONFIG_EXT2_FS_POSIX_ACL
116 &posix_acl_access_xattr_handler,
117 &posix_acl_default_xattr_handler,
119 #ifdef CONFIG_EXT2_FS_SECURITY
120 &ext2_xattr_security_handler,
125 static inline const struct xattr_handler *
126 ext2_xattr_handler(int name_index)
128 const struct xattr_handler *handler = NULL;
130 if (name_index > 0 && name_index < ARRAY_SIZE(ext2_xattr_handler_map))
131 handler = ext2_xattr_handler_map[name_index];
138 * Copy an extended attribute into the buffer
139 * provided, or compute the buffer size required.
140 * Buffer is NULL to compute the size of the buffer required.
142 * Returns a negative error number on failure, or the number of bytes
143 * used / required on success.
146 ext2_xattr_get(struct inode *inode, int name_index, const char *name,
147 void *buffer, size_t buffer_size)
149 struct buffer_head *bh = NULL;
150 struct ext2_xattr_entry *entry;
151 size_t name_len, size;
154 struct mb_cache *ext2_mb_cache = EXT2_SB(inode->i_sb)->s_mb_cache;
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(ext2_mb_cache, 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(ext2_mb_cache, 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;
250 struct mb_cache *ext2_mb_cache = EXT2_SB(inode->i_sb)->s_mb_cache;
252 ea_idebug(inode, "buffer=%p, buffer_size=%ld",
253 buffer, (long)buffer_size);
255 down_read(&EXT2_I(inode)->xattr_sem);
257 if (!EXT2_I(inode)->i_file_acl)
259 ea_idebug(inode, "reading block %d", EXT2_I(inode)->i_file_acl);
260 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
264 ea_bdebug(bh, "b_count=%d, refcount=%d",
265 atomic_read(&(bh->b_count)), le32_to_cpu(HDR(bh)->h_refcount));
266 end = bh->b_data + bh->b_size;
267 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
268 HDR(bh)->h_blocks != cpu_to_le32(1)) {
269 bad_block: ext2_error(inode->i_sb, "ext2_xattr_list",
270 "inode %ld: bad block %d", inode->i_ino,
271 EXT2_I(inode)->i_file_acl);
276 /* check the on-disk data structure */
277 entry = FIRST_ENTRY(bh);
278 while (!IS_LAST_ENTRY(entry)) {
279 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(entry);
281 if ((char *)next >= end)
285 if (ext2_xattr_cache_insert(ext2_mb_cache, bh))
286 ea_idebug(inode, "cache insert failed");
288 /* list the attribute names */
289 for (entry = FIRST_ENTRY(bh); !IS_LAST_ENTRY(entry);
290 entry = EXT2_XATTR_NEXT(entry)) {
291 const struct xattr_handler *handler =
292 ext2_xattr_handler(entry->e_name_index);
294 if (handler && (!handler->list || handler->list(dentry))) {
295 const char *prefix = handler->prefix ?: handler->name;
296 size_t prefix_len = strlen(prefix);
297 size_t size = prefix_len + entry->e_name_len + 1;
304 memcpy(buffer, prefix, prefix_len);
305 buffer += prefix_len;
306 memcpy(buffer, entry->e_name, entry->e_name_len);
307 buffer += entry->e_name_len;
313 error = buffer_size - rest; /* total size */
317 up_read(&EXT2_I(inode)->xattr_sem);
323 * Inode operation listxattr()
325 * d_inode(dentry)->i_mutex: don't care
328 ext2_listxattr(struct dentry *dentry, char *buffer, size_t size)
330 return ext2_xattr_list(dentry, buffer, size);
334 * If the EXT2_FEATURE_COMPAT_EXT_ATTR feature of this file system is
337 static void ext2_xattr_update_super_block(struct super_block *sb)
339 if (EXT2_HAS_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR))
342 spin_lock(&EXT2_SB(sb)->s_lock);
343 EXT2_SET_COMPAT_FEATURE(sb, EXT2_FEATURE_COMPAT_EXT_ATTR);
344 spin_unlock(&EXT2_SB(sb)->s_lock);
345 mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
351 * Create, replace or remove an extended attribute for this inode. Value
352 * is NULL to remove an existing extended attribute, and non-NULL to
353 * either replace an existing extended attribute, or create a new extended
354 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
355 * specify that an extended attribute must exist and must not exist
356 * previous to the call, respectively.
358 * Returns 0, or a negative error number on failure.
361 ext2_xattr_set(struct inode *inode, int name_index, const char *name,
362 const void *value, size_t value_len, int flags)
364 struct super_block *sb = inode->i_sb;
365 struct buffer_head *bh = NULL;
366 struct ext2_xattr_header *header = NULL;
367 struct ext2_xattr_entry *here, *last;
368 size_t name_len, free, min_offs = sb->s_blocksize;
369 int not_found = 1, error;
373 * header -- Points either into bh, or to a temporarily
375 * here -- The named entry found, or the place for inserting, within
376 * the block pointed to by header.
377 * last -- Points right after the last named entry within the block
378 * pointed to by header.
379 * min_offs -- The offset of the first value (values are aligned
380 * towards the end of the block).
381 * end -- Points right after the block pointed to by header.
384 ea_idebug(inode, "name=%d.%s, value=%p, value_len=%ld",
385 name_index, name, value, (long)value_len);
391 name_len = strlen(name);
392 if (name_len > 255 || value_len > sb->s_blocksize)
394 down_write(&EXT2_I(inode)->xattr_sem);
395 if (EXT2_I(inode)->i_file_acl) {
396 /* The inode already has an extended attribute block. */
397 bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
401 ea_bdebug(bh, "b_count=%d, refcount=%d",
402 atomic_read(&(bh->b_count)),
403 le32_to_cpu(HDR(bh)->h_refcount));
405 end = bh->b_data + bh->b_size;
406 if (header->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
407 header->h_blocks != cpu_to_le32(1)) {
408 bad_block: ext2_error(sb, "ext2_xattr_set",
409 "inode %ld: bad block %d", inode->i_ino,
410 EXT2_I(inode)->i_file_acl);
414 /* Find the named attribute. */
415 here = FIRST_ENTRY(bh);
416 while (!IS_LAST_ENTRY(here)) {
417 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(here);
418 if ((char *)next >= end)
420 if (!here->e_value_block && here->e_value_size) {
421 size_t offs = le16_to_cpu(here->e_value_offs);
425 not_found = name_index - here->e_name_index;
427 not_found = name_len - here->e_name_len;
429 not_found = memcmp(name, here->e_name,name_len);
435 /* We still need to compute min_offs and last. */
436 while (!IS_LAST_ENTRY(last)) {
437 struct ext2_xattr_entry *next = EXT2_XATTR_NEXT(last);
438 if ((char *)next >= end)
440 if (!last->e_value_block && last->e_value_size) {
441 size_t offs = le16_to_cpu(last->e_value_offs);
448 /* Check whether we have enough space left. */
449 free = min_offs - ((char*)last - (char*)header) - sizeof(__u32);
451 /* We will use a new extended attribute block. */
452 free = sb->s_blocksize -
453 sizeof(struct ext2_xattr_header) - sizeof(__u32);
454 here = last = NULL; /* avoid gcc uninitialized warning. */
458 /* Request to remove a nonexistent attribute? */
460 if (flags & XATTR_REPLACE)
466 /* Request to create an existing attribute? */
468 if (flags & XATTR_CREATE)
470 if (!here->e_value_block && here->e_value_size) {
471 size_t size = le32_to_cpu(here->e_value_size);
473 if (le16_to_cpu(here->e_value_offs) + size >
474 sb->s_blocksize || size > sb->s_blocksize)
476 free += EXT2_XATTR_SIZE(size);
478 free += EXT2_XATTR_LEN(name_len);
481 if (free < EXT2_XATTR_LEN(name_len) + EXT2_XATTR_SIZE(value_len))
484 /* Here we know that we can set the new attribute. */
487 /* assert(header == HDR(bh)); */
489 if (header->h_refcount == cpu_to_le32(1)) {
490 __u32 hash = le32_to_cpu(header->h_hash);
492 ea_bdebug(bh, "modifying in-place");
494 * This must happen under buffer lock for
495 * ext2_xattr_set2() to reliably detect modified block
497 mb_cache_entry_delete_block(EXT2_SB(sb)->s_mb_cache,
498 hash, bh->b_blocknr);
500 /* keep the buffer locked while modifying it. */
505 ea_bdebug(bh, "cloning");
506 header = kmalloc(bh->b_size, GFP_KERNEL);
510 memcpy(header, HDR(bh), bh->b_size);
511 header->h_refcount = cpu_to_le32(1);
513 offset = (char *)here - bh->b_data;
514 here = ENTRY((char *)header + offset);
515 offset = (char *)last - bh->b_data;
516 last = ENTRY((char *)header + offset);
519 /* Allocate a buffer where we construct the new block. */
520 header = kzalloc(sb->s_blocksize, GFP_KERNEL);
524 end = (char *)header + sb->s_blocksize;
525 header->h_magic = cpu_to_le32(EXT2_XATTR_MAGIC);
526 header->h_blocks = header->h_refcount = cpu_to_le32(1);
527 last = here = ENTRY(header+1);
530 /* Iff we are modifying the block in-place, bh is locked here. */
533 /* Insert the new name. */
534 size_t size = EXT2_XATTR_LEN(name_len);
535 size_t rest = (char *)last - (char *)here;
536 memmove((char *)here + size, here, rest);
537 memset(here, 0, size);
538 here->e_name_index = name_index;
539 here->e_name_len = name_len;
540 memcpy(here->e_name, name, name_len);
542 if (!here->e_value_block && here->e_value_size) {
543 char *first_val = (char *)header + min_offs;
544 size_t offs = le16_to_cpu(here->e_value_offs);
545 char *val = (char *)header + offs;
546 size_t size = EXT2_XATTR_SIZE(
547 le32_to_cpu(here->e_value_size));
549 if (size == EXT2_XATTR_SIZE(value_len)) {
550 /* The old and the new value have the same
551 size. Just replace. */
552 here->e_value_size = cpu_to_le32(value_len);
553 memset(val + size - EXT2_XATTR_PAD, 0,
554 EXT2_XATTR_PAD); /* Clear pad bytes. */
555 memcpy(val, value, value_len);
559 /* Remove the old value. */
560 memmove(first_val + size, first_val, val - first_val);
561 memset(first_val, 0, size);
562 here->e_value_offs = 0;
565 /* Adjust all value offsets. */
566 last = ENTRY(header+1);
567 while (!IS_LAST_ENTRY(last)) {
568 size_t o = le16_to_cpu(last->e_value_offs);
569 if (!last->e_value_block && o < offs)
571 cpu_to_le16(o + size);
572 last = EXT2_XATTR_NEXT(last);
576 /* Remove the old name. */
577 size_t size = EXT2_XATTR_LEN(name_len);
578 last = ENTRY((char *)last - size);
579 memmove(here, (char*)here + size,
580 (char*)last - (char*)here);
581 memset(last, 0, size);
586 /* Insert the new value. */
587 here->e_value_size = cpu_to_le32(value_len);
589 size_t size = EXT2_XATTR_SIZE(value_len);
590 char *val = (char *)header + min_offs - size;
592 cpu_to_le16((char *)val - (char *)header);
593 memset(val + size - EXT2_XATTR_PAD, 0,
594 EXT2_XATTR_PAD); /* Clear the pad bytes. */
595 memcpy(val, value, value_len);
600 if (IS_LAST_ENTRY(ENTRY(header+1))) {
601 /* This block is now empty. */
602 if (bh && header == HDR(bh))
603 unlock_buffer(bh); /* we were modifying in-place. */
604 error = ext2_xattr_set2(inode, bh, NULL);
606 ext2_xattr_rehash(header, here);
607 if (bh && header == HDR(bh))
608 unlock_buffer(bh); /* we were modifying in-place. */
609 error = ext2_xattr_set2(inode, bh, header);
613 if (!(bh && header == HDR(bh)))
616 up_write(&EXT2_I(inode)->xattr_sem);
622 * Second half of ext2_xattr_set(): Update the file system.
625 ext2_xattr_set2(struct inode *inode, struct buffer_head *old_bh,
626 struct ext2_xattr_header *header)
628 struct super_block *sb = inode->i_sb;
629 struct buffer_head *new_bh = NULL;
631 struct mb_cache *ext2_mb_cache = EXT2_SB(sb)->s_mb_cache;
634 new_bh = ext2_xattr_cache_find(inode, header);
636 /* We found an identical block in the cache. */
637 if (new_bh == old_bh) {
638 ea_bdebug(new_bh, "keeping this block");
640 /* The old block is released after updating
642 ea_bdebug(new_bh, "reusing block");
644 error = dquot_alloc_block(inode, 1);
646 unlock_buffer(new_bh);
649 le32_add_cpu(&HDR(new_bh)->h_refcount, 1);
650 ea_bdebug(new_bh, "refcount now=%d",
651 le32_to_cpu(HDR(new_bh)->h_refcount));
653 unlock_buffer(new_bh);
654 } else if (old_bh && header == HDR(old_bh)) {
655 /* Keep this block. No need to lock the block as we
656 don't need to change the reference count. */
659 ext2_xattr_cache_insert(ext2_mb_cache, new_bh);
661 /* We need to allocate a new block */
662 ext2_fsblk_t goal = ext2_group_first_block_no(sb,
663 EXT2_I(inode)->i_block_group);
664 int block = ext2_new_block(inode, goal, &error);
667 ea_idebug(inode, "creating block %d", block);
669 new_bh = sb_getblk(sb, block);
670 if (unlikely(!new_bh)) {
671 ext2_free_blocks(inode, block, 1);
672 mark_inode_dirty(inode);
677 memcpy(new_bh->b_data, header, new_bh->b_size);
678 set_buffer_uptodate(new_bh);
679 unlock_buffer(new_bh);
680 ext2_xattr_cache_insert(ext2_mb_cache, new_bh);
682 ext2_xattr_update_super_block(sb);
684 mark_buffer_dirty(new_bh);
685 if (IS_SYNC(inode)) {
686 sync_dirty_buffer(new_bh);
688 if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
693 /* Update the inode. */
694 EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
695 inode->i_ctime = current_time(inode);
696 if (IS_SYNC(inode)) {
697 error = sync_inode_metadata(inode, 1);
698 /* In case sync failed due to ENOSPC the inode was actually
699 * written (only some dirty data were not) so we just proceed
700 * as if nothing happened and cleanup the unused block */
701 if (error && error != -ENOSPC) {
702 if (new_bh && new_bh != old_bh) {
703 dquot_free_block_nodirty(inode, 1);
704 mark_inode_dirty(inode);
709 mark_inode_dirty(inode);
712 if (old_bh && old_bh != new_bh) {
714 * If there was an old block and we are no longer using it,
715 * release the old block.
718 if (HDR(old_bh)->h_refcount == cpu_to_le32(1)) {
719 __u32 hash = le32_to_cpu(HDR(old_bh)->h_hash);
722 * This must happen under buffer lock for
723 * ext2_xattr_set2() to reliably detect freed block
725 mb_cache_entry_delete_block(ext2_mb_cache,
726 hash, old_bh->b_blocknr);
727 /* Free the old block. */
728 ea_bdebug(old_bh, "freeing");
729 ext2_free_blocks(inode, old_bh->b_blocknr, 1);
730 mark_inode_dirty(inode);
731 /* We let our caller release old_bh, so we
732 * need to duplicate the buffer before. */
736 /* Decrement the refcount only. */
737 le32_add_cpu(&HDR(old_bh)->h_refcount, -1);
738 dquot_free_block_nodirty(inode, 1);
739 mark_inode_dirty(inode);
740 mark_buffer_dirty(old_bh);
741 ea_bdebug(old_bh, "refcount now=%d",
742 le32_to_cpu(HDR(old_bh)->h_refcount));
744 unlock_buffer(old_bh);
754 * ext2_xattr_delete_inode()
756 * Free extended attribute resources associated with this inode. This
757 * is called immediately before an inode is freed.
760 ext2_xattr_delete_inode(struct inode *inode)
762 struct buffer_head *bh = NULL;
763 struct ext2_sb_info *sbi = EXT2_SB(inode->i_sb);
765 down_write(&EXT2_I(inode)->xattr_sem);
766 if (!EXT2_I(inode)->i_file_acl)
769 if (!ext2_data_block_valid(sbi, EXT2_I(inode)->i_file_acl, 0)) {
770 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
771 "inode %ld: xattr block %d is out of data blocks range",
772 inode->i_ino, EXT2_I(inode)->i_file_acl);
776 bh = sb_bread(inode->i_sb, EXT2_I(inode)->i_file_acl);
778 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
779 "inode %ld: block %d read error", inode->i_ino,
780 EXT2_I(inode)->i_file_acl);
783 ea_bdebug(bh, "b_count=%d", atomic_read(&(bh->b_count)));
784 if (HDR(bh)->h_magic != cpu_to_le32(EXT2_XATTR_MAGIC) ||
785 HDR(bh)->h_blocks != cpu_to_le32(1)) {
786 ext2_error(inode->i_sb, "ext2_xattr_delete_inode",
787 "inode %ld: bad block %d", inode->i_ino,
788 EXT2_I(inode)->i_file_acl);
792 if (HDR(bh)->h_refcount == cpu_to_le32(1)) {
793 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
796 * This must happen under buffer lock for ext2_xattr_set2() to
797 * reliably detect freed block
799 mb_cache_entry_delete_block(EXT2_SB(inode->i_sb)->s_mb_cache,
800 hash, bh->b_blocknr);
801 ext2_free_blocks(inode, EXT2_I(inode)->i_file_acl, 1);
806 le32_add_cpu(&HDR(bh)->h_refcount, -1);
807 ea_bdebug(bh, "refcount now=%d",
808 le32_to_cpu(HDR(bh)->h_refcount));
810 mark_buffer_dirty(bh);
812 sync_dirty_buffer(bh);
813 dquot_free_block_nodirty(inode, 1);
815 EXT2_I(inode)->i_file_acl = 0;
819 up_write(&EXT2_I(inode)->xattr_sem);
823 * ext2_xattr_cache_insert()
825 * Create a new entry in the extended attribute cache, and insert
826 * it unless such an entry is already in the cache.
828 * Returns 0, or a negative error number on failure.
831 ext2_xattr_cache_insert(struct mb_cache *cache, struct buffer_head *bh)
833 __u32 hash = le32_to_cpu(HDR(bh)->h_hash);
836 error = mb_cache_entry_create(cache, GFP_NOFS, hash, bh->b_blocknr, 1);
838 if (error == -EBUSY) {
839 ea_bdebug(bh, "already in cache");
843 ea_bdebug(bh, "inserting [%x]", (int)hash);
850 * Compare two extended attribute blocks for equality.
852 * Returns 0 if the blocks are equal, 1 if they differ, and
853 * a negative error number on errors.
856 ext2_xattr_cmp(struct ext2_xattr_header *header1,
857 struct ext2_xattr_header *header2)
859 struct ext2_xattr_entry *entry1, *entry2;
861 entry1 = ENTRY(header1+1);
862 entry2 = ENTRY(header2+1);
863 while (!IS_LAST_ENTRY(entry1)) {
864 if (IS_LAST_ENTRY(entry2))
866 if (entry1->e_hash != entry2->e_hash ||
867 entry1->e_name_index != entry2->e_name_index ||
868 entry1->e_name_len != entry2->e_name_len ||
869 entry1->e_value_size != entry2->e_value_size ||
870 memcmp(entry1->e_name, entry2->e_name, entry1->e_name_len))
872 if (entry1->e_value_block != 0 || entry2->e_value_block != 0)
874 if (memcmp((char *)header1 + le16_to_cpu(entry1->e_value_offs),
875 (char *)header2 + le16_to_cpu(entry2->e_value_offs),
876 le32_to_cpu(entry1->e_value_size)))
879 entry1 = EXT2_XATTR_NEXT(entry1);
880 entry2 = EXT2_XATTR_NEXT(entry2);
882 if (!IS_LAST_ENTRY(entry2))
888 * ext2_xattr_cache_find()
890 * Find an identical extended attribute block.
892 * Returns a locked buffer head to the block found, or NULL if such
893 * a block was not found or an error occurred.
895 static struct buffer_head *
896 ext2_xattr_cache_find(struct inode *inode, struct ext2_xattr_header *header)
898 __u32 hash = le32_to_cpu(header->h_hash);
899 struct mb_cache_entry *ce;
900 struct mb_cache *ext2_mb_cache = EXT2_SB(inode->i_sb)->s_mb_cache;
903 return NULL; /* never share */
904 ea_idebug(inode, "looking for cached blocks [%x]", (int)hash);
906 ce = mb_cache_entry_find_first(ext2_mb_cache, hash);
908 struct buffer_head *bh;
910 bh = sb_bread(inode->i_sb, ce->e_block);
912 ext2_error(inode->i_sb, "ext2_xattr_cache_find",
913 "inode %ld: block %ld read error",
914 inode->i_ino, (unsigned long) ce->e_block);
918 * We have to be careful about races with freeing or
919 * rehashing of xattr block. Once we hold buffer lock
920 * xattr block's state is stable so we can check
921 * whether the block got freed / rehashed or not.
922 * Since we unhash mbcache entry under buffer lock when
923 * freeing / rehashing xattr block, checking whether
924 * entry is still hashed is reliable.
926 if (hlist_bl_unhashed(&ce->e_hash_list)) {
927 mb_cache_entry_put(ext2_mb_cache, ce);
931 } else if (le32_to_cpu(HDR(bh)->h_refcount) >
932 EXT2_XATTR_REFCOUNT_MAX) {
933 ea_idebug(inode, "block %ld refcount %d>%d",
934 (unsigned long) ce->e_block,
935 le32_to_cpu(HDR(bh)->h_refcount),
936 EXT2_XATTR_REFCOUNT_MAX);
937 } else if (!ext2_xattr_cmp(header, HDR(bh))) {
938 ea_bdebug(bh, "b_count=%d",
939 atomic_read(&(bh->b_count)));
940 mb_cache_entry_touch(ext2_mb_cache, ce);
941 mb_cache_entry_put(ext2_mb_cache, ce);
947 ce = mb_cache_entry_find_next(ext2_mb_cache, ce);
952 #define NAME_HASH_SHIFT 5
953 #define VALUE_HASH_SHIFT 16
956 * ext2_xattr_hash_entry()
958 * Compute the hash of an extended attribute.
960 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header *header,
961 struct ext2_xattr_entry *entry)
964 char *name = entry->e_name;
967 for (n=0; n < entry->e_name_len; n++) {
968 hash = (hash << NAME_HASH_SHIFT) ^
969 (hash >> (8*sizeof(hash) - NAME_HASH_SHIFT)) ^
973 if (entry->e_value_block == 0 && entry->e_value_size != 0) {
974 __le32 *value = (__le32 *)((char *)header +
975 le16_to_cpu(entry->e_value_offs));
976 for (n = (le32_to_cpu(entry->e_value_size) +
977 EXT2_XATTR_ROUND) >> EXT2_XATTR_PAD_BITS; n; n--) {
978 hash = (hash << VALUE_HASH_SHIFT) ^
979 (hash >> (8*sizeof(hash) - VALUE_HASH_SHIFT)) ^
980 le32_to_cpu(*value++);
983 entry->e_hash = cpu_to_le32(hash);
986 #undef NAME_HASH_SHIFT
987 #undef VALUE_HASH_SHIFT
989 #define BLOCK_HASH_SHIFT 16
992 * ext2_xattr_rehash()
994 * Re-compute the extended attribute hash value after an entry has changed.
996 static void ext2_xattr_rehash(struct ext2_xattr_header *header,
997 struct ext2_xattr_entry *entry)
999 struct ext2_xattr_entry *here;
1002 ext2_xattr_hash_entry(header, entry);
1003 here = ENTRY(header+1);
1004 while (!IS_LAST_ENTRY(here)) {
1005 if (!here->e_hash) {
1006 /* Block is not shared if an entry's hash value == 0 */
1010 hash = (hash << BLOCK_HASH_SHIFT) ^
1011 (hash >> (8*sizeof(hash) - BLOCK_HASH_SHIFT)) ^
1012 le32_to_cpu(here->e_hash);
1013 here = EXT2_XATTR_NEXT(here);
1015 header->h_hash = cpu_to_le32(hash);
1018 #undef BLOCK_HASH_SHIFT
1020 #define HASH_BUCKET_BITS 10
1022 struct mb_cache *ext2_xattr_create_cache(void)
1024 return mb_cache_create(HASH_BUCKET_BITS);
1027 void ext2_xattr_destroy_cache(struct mb_cache *cache)
1030 mb_cache_destroy(cache);