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
,
779 ea_idebug(inode
, "creating block %d", block
);
781 new_bh
= sb_getblk(sb
, block
);
783 ext2_free_blocks(inode
, block
, 1);
788 memcpy(new_bh
->b_data
, header
, new_bh
->b_size
);
789 set_buffer_uptodate(new_bh
);
790 unlock_buffer(new_bh
);
791 ext2_xattr_cache_insert(new_bh
);
793 ext2_xattr_update_super_block(sb
);
795 mark_buffer_dirty(new_bh
);
796 if (IS_SYNC(inode
)) {
797 sync_dirty_buffer(new_bh
);
799 if (buffer_req(new_bh
) && !buffer_uptodate(new_bh
))
804 /* Update the inode. */
805 EXT2_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
806 inode
->i_ctime
= CURRENT_TIME
;
807 if (IS_SYNC(inode
)) {
808 error
= ext2_sync_inode (inode
);
812 mark_inode_dirty(inode
);
815 if (old_bh
&& old_bh
!= new_bh
) {
817 * If there was an old block and we are no longer using it,
818 * release the old block.
821 if (HDR(old_bh
)->h_refcount
== cpu_to_le32(1)) {
822 /* Free the old block. */
823 ea_bdebug(old_bh
, "freeing");
824 ext2_free_blocks(inode
, old_bh
->b_blocknr
, 1);
825 /* We let our caller release old_bh, so we
826 * need to duplicate the buffer before. */
830 /* Decrement the refcount only. */
831 HDR(old_bh
)->h_refcount
= cpu_to_le32(
832 le32_to_cpu(HDR(old_bh
)->h_refcount
) - 1);
833 DQUOT_FREE_BLOCK(inode
, 1);
834 mark_buffer_dirty(old_bh
);
835 ea_bdebug(old_bh
, "refcount now=%d",
836 le32_to_cpu(HDR(old_bh
)->h_refcount
));
838 unlock_buffer(old_bh
);
848 * ext2_xattr_delete_inode()
850 * Free extended attribute resources associated with this inode. This
851 * is called immediately before an inode is freed.
854 ext2_xattr_delete_inode(struct inode
*inode
)
856 struct buffer_head
*bh
= NULL
;
858 down_write(&EXT2_I(inode
)->xattr_sem
);
859 if (!EXT2_I(inode
)->i_file_acl
)
861 bh
= sb_bread(inode
->i_sb
, EXT2_I(inode
)->i_file_acl
);
863 ext2_error(inode
->i_sb
, "ext2_xattr_delete_inode",
864 "inode %ld: block %d read error", inode
->i_ino
,
865 EXT2_I(inode
)->i_file_acl
);
868 ea_bdebug(bh
, "b_count=%d", atomic_read(&(bh
->b_count
)));
869 if (HDR(bh
)->h_magic
!= cpu_to_le32(EXT2_XATTR_MAGIC
) ||
870 HDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
871 ext2_error(inode
->i_sb
, "ext2_xattr_delete_inode",
872 "inode %ld: bad block %d", inode
->i_ino
,
873 EXT2_I(inode
)->i_file_acl
);
877 if (HDR(bh
)->h_refcount
== cpu_to_le32(1)) {
878 ext2_xattr_cache_remove(bh
);
879 ext2_free_blocks(inode
, EXT2_I(inode
)->i_file_acl
, 1);
883 HDR(bh
)->h_refcount
= cpu_to_le32(
884 le32_to_cpu(HDR(bh
)->h_refcount
) - 1);
885 mark_buffer_dirty(bh
);
887 sync_dirty_buffer(bh
);
888 DQUOT_FREE_BLOCK(inode
, 1);
890 ea_bdebug(bh
, "refcount now=%d", le32_to_cpu(HDR(bh
)->h_refcount
) - 1);
892 EXT2_I(inode
)->i_file_acl
= 0;
896 up_write(&EXT2_I(inode
)->xattr_sem
);
900 * ext2_xattr_put_super()
902 * This is called when a file system is unmounted.
905 ext2_xattr_put_super(struct super_block
*sb
)
907 mb_cache_shrink(ext2_xattr_cache
, sb
->s_bdev
);
912 * ext2_xattr_cache_insert()
914 * Create a new entry in the extended attribute cache, and insert
915 * it unless such an entry is already in the cache.
917 * Returns 0, or a negative error number on failure.
920 ext2_xattr_cache_insert(struct buffer_head
*bh
)
922 __u32 hash
= le32_to_cpu(HDR(bh
)->h_hash
);
923 struct mb_cache_entry
*ce
;
926 ce
= mb_cache_entry_alloc(ext2_xattr_cache
);
929 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, &hash
);
931 mb_cache_entry_free(ce
);
932 if (error
== -EBUSY
) {
933 ea_bdebug(bh
, "already in cache (%d cache entries)",
934 atomic_read(&ext2_xattr_cache
->c_entry_count
));
938 ea_bdebug(bh
, "inserting [%x] (%d cache entries)", (int)hash
,
939 atomic_read(&ext2_xattr_cache
->c_entry_count
));
940 mb_cache_entry_release(ce
);
948 * Compare two extended attribute blocks for equality.
950 * Returns 0 if the blocks are equal, 1 if they differ, and
951 * a negative error number on errors.
954 ext2_xattr_cmp(struct ext2_xattr_header
*header1
,
955 struct ext2_xattr_header
*header2
)
957 struct ext2_xattr_entry
*entry1
, *entry2
;
959 entry1
= ENTRY(header1
+1);
960 entry2
= ENTRY(header2
+1);
961 while (!IS_LAST_ENTRY(entry1
)) {
962 if (IS_LAST_ENTRY(entry2
))
964 if (entry1
->e_hash
!= entry2
->e_hash
||
965 entry1
->e_name_len
!= entry2
->e_name_len
||
966 entry1
->e_value_size
!= entry2
->e_value_size
||
967 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
969 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
971 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
972 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
973 le32_to_cpu(entry1
->e_value_size
)))
976 entry1
= EXT2_XATTR_NEXT(entry1
);
977 entry2
= EXT2_XATTR_NEXT(entry2
);
979 if (!IS_LAST_ENTRY(entry2
))
985 * ext2_xattr_cache_find()
987 * Find an identical extended attribute block.
989 * Returns a locked buffer head to the block found, or NULL if such
990 * a block was not found or an error occurred.
992 static struct buffer_head
*
993 ext2_xattr_cache_find(struct inode
*inode
, struct ext2_xattr_header
*header
)
995 __u32 hash
= le32_to_cpu(header
->h_hash
);
996 struct mb_cache_entry
*ce
;
999 return NULL
; /* never share */
1000 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1001 ce
= mb_cache_entry_find_first(ext2_xattr_cache
, 0,
1002 inode
->i_sb
->s_bdev
, hash
);
1004 struct buffer_head
*bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1007 ext2_error(inode
->i_sb
, "ext2_xattr_cache_find",
1008 "inode %ld: block %ld read error",
1009 inode
->i_ino
, (unsigned long) ce
->e_block
);
1012 if (le32_to_cpu(HDR(bh
)->h_refcount
) >
1013 EXT2_XATTR_REFCOUNT_MAX
) {
1014 ea_idebug(inode
, "block %ld refcount %d>%d",
1015 (unsigned long) ce
->e_block
,
1016 le32_to_cpu(HDR(bh
)->h_refcount
),
1017 EXT2_XATTR_REFCOUNT_MAX
);
1018 } else if (!ext2_xattr_cmp(header
, HDR(bh
))) {
1019 ea_bdebug(bh
, "b_count=%d",
1020 atomic_read(&(bh
->b_count
)));
1021 mb_cache_entry_release(ce
);
1027 ce
= mb_cache_entry_find_next(ce
, 0, inode
->i_sb
->s_bdev
, hash
);
1033 * ext2_xattr_cache_remove()
1035 * Remove the cache entry of a block from the cache. Called when a
1036 * block becomes invalid.
1039 ext2_xattr_cache_remove(struct buffer_head
*bh
)
1041 struct mb_cache_entry
*ce
;
1043 ce
= mb_cache_entry_get(ext2_xattr_cache
, bh
->b_bdev
, bh
->b_blocknr
);
1045 ea_bdebug(bh
, "removing (%d cache entries remaining)",
1046 atomic_read(&ext2_xattr_cache
->c_entry_count
)-1);
1047 mb_cache_entry_free(ce
);
1049 ea_bdebug(bh
, "no cache entry");
1052 #define NAME_HASH_SHIFT 5
1053 #define VALUE_HASH_SHIFT 16
1056 * ext2_xattr_hash_entry()
1058 * Compute the hash of an extended attribute.
1060 static inline void ext2_xattr_hash_entry(struct ext2_xattr_header
*header
,
1061 struct ext2_xattr_entry
*entry
)
1064 char *name
= entry
->e_name
;
1067 for (n
=0; n
< entry
->e_name_len
; n
++) {
1068 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1069 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1073 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1074 __le32
*value
= (__le32
*)((char *)header
+
1075 le16_to_cpu(entry
->e_value_offs
));
1076 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1077 EXT2_XATTR_ROUND
) >> EXT2_XATTR_PAD_BITS
; n
; n
--) {
1078 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1079 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1080 le32_to_cpu(*value
++);
1083 entry
->e_hash
= cpu_to_le32(hash
);
1086 #undef NAME_HASH_SHIFT
1087 #undef VALUE_HASH_SHIFT
1089 #define BLOCK_HASH_SHIFT 16
1092 * ext2_xattr_rehash()
1094 * Re-compute the extended attribute hash value after an entry has changed.
1096 static void ext2_xattr_rehash(struct ext2_xattr_header
*header
,
1097 struct ext2_xattr_entry
*entry
)
1099 struct ext2_xattr_entry
*here
;
1102 ext2_xattr_hash_entry(header
, entry
);
1103 here
= ENTRY(header
+1);
1104 while (!IS_LAST_ENTRY(here
)) {
1105 if (!here
->e_hash
) {
1106 /* Block is not shared if an entry's hash value == 0 */
1110 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1111 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1112 le32_to_cpu(here
->e_hash
);
1113 here
= EXT2_XATTR_NEXT(here
);
1115 header
->h_hash
= cpu_to_le32(hash
);
1118 #undef BLOCK_HASH_SHIFT
1121 init_ext2_xattr(void)
1125 err
= ext2_xattr_register(EXT2_XATTR_INDEX_USER
,
1126 &ext2_xattr_user_handler
);
1129 err
= ext2_xattr_register(EXT2_XATTR_INDEX_TRUSTED
,
1130 &ext2_xattr_trusted_handler
);
1133 #ifdef CONFIG_EXT2_FS_SECURITY
1134 err
= ext2_xattr_register(EXT2_XATTR_INDEX_SECURITY
,
1135 &ext2_xattr_security_handler
);
1139 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1140 err
= init_ext2_acl();
1144 ext2_xattr_cache
= mb_cache_create("ext2_xattr", NULL
,
1145 sizeof(struct mb_cache_entry
) +
1146 sizeof(struct mb_cache_entry_index
), 1, 6);
1147 if (!ext2_xattr_cache
) {
1153 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1157 #ifdef CONFIG_EXT2_FS_SECURITY
1158 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY
,
1159 &ext2_xattr_security_handler
);
1162 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED
,
1163 &ext2_xattr_trusted_handler
);
1165 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER
,
1166 &ext2_xattr_user_handler
);
1171 exit_ext2_xattr(void)
1173 mb_cache_destroy(ext2_xattr_cache
);
1174 #ifdef CONFIG_EXT2_FS_POSIX_ACL
1177 #ifdef CONFIG_EXT2_FS_SECURITY
1178 ext2_xattr_unregister(EXT2_XATTR_INDEX_SECURITY
,
1179 &ext2_xattr_security_handler
);
1181 ext2_xattr_unregister(EXT2_XATTR_INDEX_TRUSTED
,
1182 &ext2_xattr_trusted_handler
);
1183 ext2_xattr_unregister(EXT2_XATTR_INDEX_USER
,
1184 &ext2_xattr_user_handler
);