2 * linux/fs/ext3/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext3 code with a lot of help from Eric Jarman <ejarman@acm.org>.
8 * Extended attributes for symlinks and special files added per
9 * suggestion of Luka Renko <luka.renko@hermes.si>.
10 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
12 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
13 * and Andreas Gruenbacher <agruen@suse.de>.
17 * Extended attributes are stored directly in inodes (on file systems with
18 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
19 * field contains the block number if an inode uses an additional block. All
20 * attributes must fit in the inode and one additional block. Blocks that
21 * contain the identical set of attributes may be shared among several inodes.
22 * Identical blocks are detected by keeping a cache of blocks that have
23 * recently been accessed.
25 * The attributes in inodes and on blocks have a different header; the entries
26 * are stored in the same format:
28 * +------------------+
31 * | entry 2 | | growing downwards
36 * | value 3 | | growing upwards
38 * +------------------+
40 * The header is followed by multiple entry descriptors. In disk blocks, the
41 * entry descriptors are kept sorted. In inodes, they are unsorted. The
42 * attribute values are aligned to the end of the block in no specific order.
46 * EXT3_I(inode)->i_file_acl is protected by EXT3_I(inode)->xattr_sem.
47 * EA blocks are only changed if they are exclusive to an inode, so
48 * holding xattr_sem also means that nothing but the EA block's reference
49 * count can change. Multiple writers to the same block are synchronized
53 #include <linux/init.h>
55 #include <linux/slab.h>
56 #include <linux/ext3_jbd.h>
57 #include <linux/ext3_fs.h>
58 #include <linux/mbcache.h>
59 #include <linux/quotaops.h>
60 #include <linux/rwsem.h>
64 #define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
65 #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
66 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
67 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
69 #define IHDR(inode, raw_inode) \
70 ((struct ext3_xattr_ibody_header *) \
71 ((void *)raw_inode + \
72 EXT3_GOOD_OLD_INODE_SIZE + \
73 EXT3_I(inode)->i_extra_isize))
74 #define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
76 #ifdef EXT3_XATTR_DEBUG
77 # define ea_idebug(inode, f...) do { \
78 printk(KERN_DEBUG "inode %s:%lu: ", \
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 void ext3_xattr_cache_insert(struct buffer_head
*);
97 static struct buffer_head
*ext3_xattr_cache_find(struct inode
*,
98 struct ext3_xattr_header
*,
99 struct mb_cache_entry
**);
100 static void ext3_xattr_rehash(struct ext3_xattr_header
*,
101 struct ext3_xattr_entry
*);
103 static struct mb_cache
*ext3_xattr_cache
;
105 static struct xattr_handler
*ext3_xattr_handler_map
[] = {
106 [EXT3_XATTR_INDEX_USER
] = &ext3_xattr_user_handler
,
107 #ifdef CONFIG_EXT3_FS_POSIX_ACL
108 [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS
] = &ext3_xattr_acl_access_handler
,
109 [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &ext3_xattr_acl_default_handler
,
111 [EXT3_XATTR_INDEX_TRUSTED
] = &ext3_xattr_trusted_handler
,
112 #ifdef CONFIG_EXT3_FS_SECURITY
113 [EXT3_XATTR_INDEX_SECURITY
] = &ext3_xattr_security_handler
,
117 struct xattr_handler
*ext3_xattr_handlers
[] = {
118 &ext3_xattr_user_handler
,
119 &ext3_xattr_trusted_handler
,
120 #ifdef CONFIG_EXT3_FS_POSIX_ACL
121 &ext3_xattr_acl_access_handler
,
122 &ext3_xattr_acl_default_handler
,
124 #ifdef CONFIG_EXT3_FS_SECURITY
125 &ext3_xattr_security_handler
,
130 static inline struct xattr_handler
*
131 ext3_xattr_handler(int name_index
)
133 struct xattr_handler
*handler
= NULL
;
135 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext3_xattr_handler_map
))
136 handler
= ext3_xattr_handler_map
[name_index
];
141 * Inode operation listxattr()
143 * dentry->d_inode->i_mutex: don't care
146 ext3_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
148 return ext3_xattr_list(dentry
->d_inode
, buffer
, size
);
152 ext3_xattr_check_names(struct ext3_xattr_entry
*entry
, void *end
)
154 while (!IS_LAST_ENTRY(entry
)) {
155 struct ext3_xattr_entry
*next
= EXT3_XATTR_NEXT(entry
);
156 if ((void *)next
>= end
)
164 ext3_xattr_check_block(struct buffer_head
*bh
)
168 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT3_XATTR_MAGIC
) ||
169 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
171 error
= ext3_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
);
176 ext3_xattr_check_entry(struct ext3_xattr_entry
*entry
, size_t size
)
178 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
180 if (entry
->e_value_block
!= 0 || value_size
> size
||
181 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
187 ext3_xattr_find_entry(struct ext3_xattr_entry
**pentry
, int name_index
,
188 const char *name
, size_t size
, int sorted
)
190 struct ext3_xattr_entry
*entry
;
196 name_len
= strlen(name
);
198 for (; !IS_LAST_ENTRY(entry
); entry
= EXT3_XATTR_NEXT(entry
)) {
199 cmp
= name_index
- entry
->e_name_index
;
201 cmp
= name_len
- entry
->e_name_len
;
203 cmp
= memcmp(name
, entry
->e_name
, name_len
);
204 if (cmp
<= 0 && (sorted
|| cmp
== 0))
208 if (!cmp
&& ext3_xattr_check_entry(entry
, size
))
210 return cmp
? -ENODATA
: 0;
214 ext3_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
215 void *buffer
, size_t buffer_size
)
217 struct buffer_head
*bh
= NULL
;
218 struct ext3_xattr_entry
*entry
;
222 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
223 name_index
, name
, buffer
, (long)buffer_size
);
226 if (!EXT3_I(inode
)->i_file_acl
)
228 ea_idebug(inode
, "reading block %u", EXT3_I(inode
)->i_file_acl
);
229 bh
= sb_bread(inode
->i_sb
, EXT3_I(inode
)->i_file_acl
);
232 ea_bdebug(bh
, "b_count=%d, refcount=%d",
233 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
234 if (ext3_xattr_check_block(bh
)) {
235 bad_block
: ext3_error(inode
->i_sb
, __FUNCTION__
,
236 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
237 EXT3_I(inode
)->i_file_acl
);
241 ext3_xattr_cache_insert(bh
);
243 error
= ext3_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
248 size
= le32_to_cpu(entry
->e_value_size
);
251 if (size
> buffer_size
)
253 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
264 ext3_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
265 void *buffer
, size_t buffer_size
)
267 struct ext3_xattr_ibody_header
*header
;
268 struct ext3_xattr_entry
*entry
;
269 struct ext3_inode
*raw_inode
;
270 struct ext3_iloc iloc
;
275 if (!(EXT3_I(inode
)->i_state
& EXT3_STATE_XATTR
))
277 error
= ext3_get_inode_loc(inode
, &iloc
);
280 raw_inode
= ext3_raw_inode(&iloc
);
281 header
= IHDR(inode
, raw_inode
);
282 entry
= IFIRST(header
);
283 end
= (void *)raw_inode
+ EXT3_SB(inode
->i_sb
)->s_inode_size
;
284 error
= ext3_xattr_check_names(entry
, end
);
287 error
= ext3_xattr_find_entry(&entry
, name_index
, name
,
288 end
- (void *)entry
, 0);
291 size
= le32_to_cpu(entry
->e_value_size
);
294 if (size
> buffer_size
)
296 memcpy(buffer
, (void *)IFIRST(header
) +
297 le16_to_cpu(entry
->e_value_offs
), size
);
309 * Copy an extended attribute into the buffer
310 * provided, or compute the buffer size required.
311 * Buffer is NULL to compute the size of the buffer required.
313 * Returns a negative error number on failure, or the number of bytes
314 * used / required on success.
317 ext3_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
318 void *buffer
, size_t buffer_size
)
322 down_read(&EXT3_I(inode
)->xattr_sem
);
323 error
= ext3_xattr_ibody_get(inode
, name_index
, name
, buffer
,
325 if (error
== -ENODATA
)
326 error
= ext3_xattr_block_get(inode
, name_index
, name
, buffer
,
328 up_read(&EXT3_I(inode
)->xattr_sem
);
333 ext3_xattr_list_entries(struct inode
*inode
, struct ext3_xattr_entry
*entry
,
334 char *buffer
, size_t buffer_size
)
336 size_t rest
= buffer_size
;
338 for (; !IS_LAST_ENTRY(entry
); entry
= EXT3_XATTR_NEXT(entry
)) {
339 struct xattr_handler
*handler
=
340 ext3_xattr_handler(entry
->e_name_index
);
343 size_t size
= handler
->list(inode
, buffer
, rest
,
354 return buffer_size
- rest
;
358 ext3_xattr_block_list(struct inode
*inode
, char *buffer
, size_t buffer_size
)
360 struct buffer_head
*bh
= NULL
;
363 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
364 buffer
, (long)buffer_size
);
367 if (!EXT3_I(inode
)->i_file_acl
)
369 ea_idebug(inode
, "reading block %u", EXT3_I(inode
)->i_file_acl
);
370 bh
= sb_bread(inode
->i_sb
, EXT3_I(inode
)->i_file_acl
);
374 ea_bdebug(bh
, "b_count=%d, refcount=%d",
375 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
376 if (ext3_xattr_check_block(bh
)) {
377 ext3_error(inode
->i_sb
, __FUNCTION__
,
378 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
379 EXT3_I(inode
)->i_file_acl
);
383 ext3_xattr_cache_insert(bh
);
384 error
= ext3_xattr_list_entries(inode
, BFIRST(bh
), buffer
, buffer_size
);
393 ext3_xattr_ibody_list(struct inode
*inode
, char *buffer
, size_t buffer_size
)
395 struct ext3_xattr_ibody_header
*header
;
396 struct ext3_inode
*raw_inode
;
397 struct ext3_iloc iloc
;
401 if (!(EXT3_I(inode
)->i_state
& EXT3_STATE_XATTR
))
403 error
= ext3_get_inode_loc(inode
, &iloc
);
406 raw_inode
= ext3_raw_inode(&iloc
);
407 header
= IHDR(inode
, raw_inode
);
408 end
= (void *)raw_inode
+ EXT3_SB(inode
->i_sb
)->s_inode_size
;
409 error
= ext3_xattr_check_names(IFIRST(header
), end
);
412 error
= ext3_xattr_list_entries(inode
, IFIRST(header
),
413 buffer
, buffer_size
);
423 * Copy a list of attribute names into the buffer
424 * provided, or compute the buffer size required.
425 * Buffer is NULL to compute the size of the buffer required.
427 * Returns a negative error number on failure, or the number of bytes
428 * used / required on success.
431 ext3_xattr_list(struct inode
*inode
, char *buffer
, size_t buffer_size
)
433 int i_error
, b_error
;
435 down_read(&EXT3_I(inode
)->xattr_sem
);
436 i_error
= ext3_xattr_ibody_list(inode
, buffer
, buffer_size
);
442 buffer_size
-= i_error
;
444 b_error
= ext3_xattr_block_list(inode
, buffer
, buffer_size
);
448 up_read(&EXT3_I(inode
)->xattr_sem
);
449 return i_error
+ b_error
;
453 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
456 static void ext3_xattr_update_super_block(handle_t
*handle
,
457 struct super_block
*sb
)
459 if (EXT3_HAS_COMPAT_FEATURE(sb
, EXT3_FEATURE_COMPAT_EXT_ATTR
))
463 if (ext3_journal_get_write_access(handle
, EXT3_SB(sb
)->s_sbh
) == 0) {
464 EXT3_SB(sb
)->s_es
->s_feature_compat
|=
465 cpu_to_le32(EXT3_FEATURE_COMPAT_EXT_ATTR
);
467 ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
473 * Release the xattr block BH: If the reference count is > 1, decrement
474 * it; otherwise free the block.
477 ext3_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
478 struct buffer_head
*bh
)
480 struct mb_cache_entry
*ce
= NULL
;
482 ce
= mb_cache_entry_get(ext3_xattr_cache
, bh
->b_bdev
, bh
->b_blocknr
);
483 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
484 ea_bdebug(bh
, "refcount now=0; freeing");
486 mb_cache_entry_free(ce
);
487 ext3_free_blocks(handle
, inode
, bh
->b_blocknr
, 1);
489 ext3_forget(handle
, 1, inode
, bh
, bh
->b_blocknr
);
491 if (ext3_journal_get_write_access(handle
, bh
) == 0) {
493 BHDR(bh
)->h_refcount
= cpu_to_le32(
494 le32_to_cpu(BHDR(bh
)->h_refcount
) - 1);
495 ext3_journal_dirty_metadata(handle
, bh
);
498 DQUOT_FREE_BLOCK(inode
, 1);
500 ea_bdebug(bh
, "refcount now=%d; releasing",
501 le32_to_cpu(BHDR(bh
)->h_refcount
));
504 mb_cache_entry_release(ce
);
508 struct ext3_xattr_info
{
515 struct ext3_xattr_search
{
516 struct ext3_xattr_entry
*first
;
519 struct ext3_xattr_entry
*here
;
524 ext3_xattr_set_entry(struct ext3_xattr_info
*i
, struct ext3_xattr_search
*s
)
526 struct ext3_xattr_entry
*last
;
527 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
529 /* Compute min_offs and last. */
531 for (; !IS_LAST_ENTRY(last
); last
= EXT3_XATTR_NEXT(last
)) {
532 if (!last
->e_value_block
&& last
->e_value_size
) {
533 size_t offs
= le16_to_cpu(last
->e_value_offs
);
538 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
540 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
541 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
542 free
+= EXT3_XATTR_SIZE(size
);
544 free
+= EXT3_XATTR_LEN(name_len
);
547 if (free
< EXT3_XATTR_SIZE(i
->value_len
) ||
548 free
< EXT3_XATTR_LEN(name_len
) +
549 EXT3_XATTR_SIZE(i
->value_len
))
553 if (i
->value
&& s
->not_found
) {
554 /* Insert the new name. */
555 size_t size
= EXT3_XATTR_LEN(name_len
);
556 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
557 memmove((void *)s
->here
+ size
, s
->here
, rest
);
558 memset(s
->here
, 0, size
);
559 s
->here
->e_name_index
= i
->name_index
;
560 s
->here
->e_name_len
= name_len
;
561 memcpy(s
->here
->e_name
, i
->name
, name_len
);
563 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
564 void *first_val
= s
->base
+ min_offs
;
565 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
566 void *val
= s
->base
+ offs
;
567 size_t size
= EXT3_XATTR_SIZE(
568 le32_to_cpu(s
->here
->e_value_size
));
570 if (i
->value
&& size
== EXT3_XATTR_SIZE(i
->value_len
)) {
571 /* The old and the new value have the same
572 size. Just replace. */
573 s
->here
->e_value_size
=
574 cpu_to_le32(i
->value_len
);
575 memset(val
+ size
- EXT3_XATTR_PAD
, 0,
576 EXT3_XATTR_PAD
); /* Clear pad bytes. */
577 memcpy(val
, i
->value
, i
->value_len
);
581 /* Remove the old value. */
582 memmove(first_val
+ size
, first_val
, val
- first_val
);
583 memset(first_val
, 0, size
);
584 s
->here
->e_value_size
= 0;
585 s
->here
->e_value_offs
= 0;
588 /* Adjust all value offsets. */
590 while (!IS_LAST_ENTRY(last
)) {
591 size_t o
= le16_to_cpu(last
->e_value_offs
);
592 if (!last
->e_value_block
&&
593 last
->e_value_size
&& o
< offs
)
595 cpu_to_le16(o
+ size
);
596 last
= EXT3_XATTR_NEXT(last
);
600 /* Remove the old name. */
601 size_t size
= EXT3_XATTR_LEN(name_len
);
602 last
= ENTRY((void *)last
- size
);
603 memmove(s
->here
, (void *)s
->here
+ size
,
604 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
605 memset(last
, 0, size
);
610 /* Insert the new value. */
611 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
613 size_t size
= EXT3_XATTR_SIZE(i
->value_len
);
614 void *val
= s
->base
+ min_offs
- size
;
615 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
616 memset(val
+ size
- EXT3_XATTR_PAD
, 0,
617 EXT3_XATTR_PAD
); /* Clear the pad bytes. */
618 memcpy(val
, i
->value
, i
->value_len
);
624 struct ext3_xattr_block_find
{
625 struct ext3_xattr_search s
;
626 struct buffer_head
*bh
;
630 ext3_xattr_block_find(struct inode
*inode
, struct ext3_xattr_info
*i
,
631 struct ext3_xattr_block_find
*bs
)
633 struct super_block
*sb
= inode
->i_sb
;
636 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
637 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
639 if (EXT3_I(inode
)->i_file_acl
) {
640 /* The inode already has an extended attribute block. */
641 bs
->bh
= sb_bread(sb
, EXT3_I(inode
)->i_file_acl
);
645 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
646 atomic_read(&(bs
->bh
->b_count
)),
647 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
648 if (ext3_xattr_check_block(bs
->bh
)) {
649 ext3_error(sb
, __FUNCTION__
,
650 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
651 EXT3_I(inode
)->i_file_acl
);
655 /* Find the named attribute. */
656 bs
->s
.base
= BHDR(bs
->bh
);
657 bs
->s
.first
= BFIRST(bs
->bh
);
658 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
659 bs
->s
.here
= bs
->s
.first
;
660 error
= ext3_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
661 i
->name
, bs
->bh
->b_size
, 1);
662 if (error
&& error
!= -ENODATA
)
664 bs
->s
.not_found
= error
;
673 ext3_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
674 struct ext3_xattr_info
*i
,
675 struct ext3_xattr_block_find
*bs
)
677 struct super_block
*sb
= inode
->i_sb
;
678 struct buffer_head
*new_bh
= NULL
;
679 struct ext3_xattr_search
*s
= &bs
->s
;
680 struct mb_cache_entry
*ce
= NULL
;
683 #define header(x) ((struct ext3_xattr_header *)(x))
685 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
688 ce
= mb_cache_entry_get(ext3_xattr_cache
, bs
->bh
->b_bdev
,
690 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
692 mb_cache_entry_free(ce
);
695 ea_bdebug(bs
->bh
, "modifying in-place");
696 error
= ext3_journal_get_write_access(handle
, bs
->bh
);
700 error
= ext3_xattr_set_entry(i
, s
);
702 if (!IS_LAST_ENTRY(s
->first
))
703 ext3_xattr_rehash(header(s
->base
),
705 ext3_xattr_cache_insert(bs
->bh
);
707 unlock_buffer(bs
->bh
);
711 error
= ext3_journal_dirty_metadata(handle
,
717 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
720 mb_cache_entry_release(ce
);
723 ea_bdebug(bs
->bh
, "cloning");
724 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_KERNEL
);
728 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
729 s
->first
= ENTRY(header(s
->base
)+1);
730 header(s
->base
)->h_refcount
= cpu_to_le32(1);
731 s
->here
= ENTRY(s
->base
+ offset
);
732 s
->end
= s
->base
+ bs
->bh
->b_size
;
735 /* Allocate a buffer where we construct the new block. */
736 s
->base
= kmalloc(sb
->s_blocksize
, GFP_KERNEL
);
737 /* assert(header == s->base) */
741 memset(s
->base
, 0, sb
->s_blocksize
);
742 header(s
->base
)->h_magic
= cpu_to_le32(EXT3_XATTR_MAGIC
);
743 header(s
->base
)->h_blocks
= cpu_to_le32(1);
744 header(s
->base
)->h_refcount
= cpu_to_le32(1);
745 s
->first
= ENTRY(header(s
->base
)+1);
746 s
->here
= ENTRY(header(s
->base
)+1);
747 s
->end
= s
->base
+ sb
->s_blocksize
;
750 error
= ext3_xattr_set_entry(i
, s
);
755 if (!IS_LAST_ENTRY(s
->first
))
756 ext3_xattr_rehash(header(s
->base
), s
->here
);
759 if (!IS_LAST_ENTRY(s
->first
)) {
760 new_bh
= ext3_xattr_cache_find(inode
, header(s
->base
), &ce
);
762 /* We found an identical block in the cache. */
763 if (new_bh
== bs
->bh
)
764 ea_bdebug(new_bh
, "keeping");
766 /* The old block is released after updating
769 if (DQUOT_ALLOC_BLOCK(inode
, 1))
771 error
= ext3_journal_get_write_access(handle
,
776 BHDR(new_bh
)->h_refcount
= cpu_to_le32(1 +
777 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
778 ea_bdebug(new_bh
, "reusing; refcount now=%d",
779 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
780 unlock_buffer(new_bh
);
781 error
= ext3_journal_dirty_metadata(handle
,
786 mb_cache_entry_release(ce
);
788 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
789 /* We were modifying this block in-place. */
790 ea_bdebug(bs
->bh
, "keeping this block");
794 /* We need to allocate a new block */
795 ext3_fsblk_t goal
= le32_to_cpu(
796 EXT3_SB(sb
)->s_es
->s_first_data_block
) +
797 (ext3_fsblk_t
)EXT3_I(inode
)->i_block_group
*
798 EXT3_BLOCKS_PER_GROUP(sb
);
799 ext3_fsblk_t block
= ext3_new_block(handle
, inode
,
803 ea_idebug(inode
, "creating block %d", block
);
805 new_bh
= sb_getblk(sb
, block
);
808 ext3_free_blocks(handle
, inode
, block
, 1);
813 error
= ext3_journal_get_create_access(handle
, new_bh
);
815 unlock_buffer(new_bh
);
818 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
819 set_buffer_uptodate(new_bh
);
820 unlock_buffer(new_bh
);
821 ext3_xattr_cache_insert(new_bh
);
822 error
= ext3_journal_dirty_metadata(handle
, new_bh
);
828 /* Update the inode. */
829 EXT3_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
831 /* Drop the previous xattr block. */
832 if (bs
->bh
&& bs
->bh
!= new_bh
)
833 ext3_xattr_release_block(handle
, inode
, bs
->bh
);
838 mb_cache_entry_release(ce
);
840 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
846 DQUOT_FREE_BLOCK(inode
, 1);
850 ext3_error(inode
->i_sb
, __FUNCTION__
,
851 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
852 EXT3_I(inode
)->i_file_acl
);
858 struct ext3_xattr_ibody_find
{
859 struct ext3_xattr_search s
;
860 struct ext3_iloc iloc
;
864 ext3_xattr_ibody_find(struct inode
*inode
, struct ext3_xattr_info
*i
,
865 struct ext3_xattr_ibody_find
*is
)
867 struct ext3_xattr_ibody_header
*header
;
868 struct ext3_inode
*raw_inode
;
871 if (EXT3_I(inode
)->i_extra_isize
== 0)
873 raw_inode
= ext3_raw_inode(&is
->iloc
);
874 header
= IHDR(inode
, raw_inode
);
875 is
->s
.base
= is
->s
.first
= IFIRST(header
);
876 is
->s
.here
= is
->s
.first
;
877 is
->s
.end
= (void *)raw_inode
+ EXT3_SB(inode
->i_sb
)->s_inode_size
;
878 if (EXT3_I(inode
)->i_state
& EXT3_STATE_XATTR
) {
879 error
= ext3_xattr_check_names(IFIRST(header
), is
->s
.end
);
882 /* Find the named attribute. */
883 error
= ext3_xattr_find_entry(&is
->s
.here
, i
->name_index
,
885 (void *)is
->s
.base
, 0);
886 if (error
&& error
!= -ENODATA
)
888 is
->s
.not_found
= error
;
894 ext3_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
895 struct ext3_xattr_info
*i
,
896 struct ext3_xattr_ibody_find
*is
)
898 struct ext3_xattr_ibody_header
*header
;
899 struct ext3_xattr_search
*s
= &is
->s
;
902 if (EXT3_I(inode
)->i_extra_isize
== 0)
904 error
= ext3_xattr_set_entry(i
, s
);
907 header
= IHDR(inode
, ext3_raw_inode(&is
->iloc
));
908 if (!IS_LAST_ENTRY(s
->first
)) {
909 header
->h_magic
= cpu_to_le32(EXT3_XATTR_MAGIC
);
910 EXT3_I(inode
)->i_state
|= EXT3_STATE_XATTR
;
912 header
->h_magic
= cpu_to_le32(0);
913 EXT3_I(inode
)->i_state
&= ~EXT3_STATE_XATTR
;
919 * ext3_xattr_set_handle()
921 * Create, replace or remove an extended attribute for this inode. Buffer
922 * is NULL to remove an existing extended attribute, and non-NULL to
923 * either replace an existing extended attribute, or create a new extended
924 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
925 * specify that an extended attribute must exist and must not exist
926 * previous to the call, respectively.
928 * Returns 0, or a negative error number on failure.
931 ext3_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
932 const char *name
, const void *value
, size_t value_len
,
935 struct ext3_xattr_info i
= {
936 .name_index
= name_index
,
939 .value_len
= value_len
,
942 struct ext3_xattr_ibody_find is
= {
943 .s
= { .not_found
= -ENODATA
, },
945 struct ext3_xattr_block_find bs
= {
946 .s
= { .not_found
= -ENODATA
, },
952 if (strlen(name
) > 255)
954 down_write(&EXT3_I(inode
)->xattr_sem
);
955 error
= ext3_get_inode_loc(inode
, &is
.iloc
);
959 if (EXT3_I(inode
)->i_state
& EXT3_STATE_NEW
) {
960 struct ext3_inode
*raw_inode
= ext3_raw_inode(&is
.iloc
);
961 memset(raw_inode
, 0, EXT3_SB(inode
->i_sb
)->s_inode_size
);
962 EXT3_I(inode
)->i_state
&= ~EXT3_STATE_NEW
;
965 error
= ext3_xattr_ibody_find(inode
, &i
, &is
);
969 error
= ext3_xattr_block_find(inode
, &i
, &bs
);
972 if (is
.s
.not_found
&& bs
.s
.not_found
) {
974 if (flags
& XATTR_REPLACE
)
981 if (flags
& XATTR_CREATE
)
984 error
= ext3_journal_get_write_access(handle
, is
.iloc
.bh
);
989 error
= ext3_xattr_ibody_set(handle
, inode
, &i
, &is
);
990 else if (!bs
.s
.not_found
)
991 error
= ext3_xattr_block_set(handle
, inode
, &i
, &bs
);
993 error
= ext3_xattr_ibody_set(handle
, inode
, &i
, &is
);
994 if (!error
&& !bs
.s
.not_found
) {
996 error
= ext3_xattr_block_set(handle
, inode
, &i
, &bs
);
997 } else if (error
== -ENOSPC
) {
998 error
= ext3_xattr_block_set(handle
, inode
, &i
, &bs
);
1001 if (!is
.s
.not_found
) {
1003 error
= ext3_xattr_ibody_set(handle
, inode
, &i
,
1009 ext3_xattr_update_super_block(handle
, inode
->i_sb
);
1010 inode
->i_ctime
= CURRENT_TIME_SEC
;
1011 error
= ext3_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1013 * The bh is consumed by ext3_mark_iloc_dirty, even with
1024 up_write(&EXT3_I(inode
)->xattr_sem
);
1031 * Like ext3_xattr_set_handle, but start from an inode. This extended
1032 * attribute modification is a filesystem transaction by itself.
1034 * Returns 0, or a negative error number on failure.
1037 ext3_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1038 const void *value
, size_t value_len
, int flags
)
1041 int error
, retries
= 0;
1044 handle
= ext3_journal_start(inode
, EXT3_DATA_TRANS_BLOCKS(inode
->i_sb
));
1045 if (IS_ERR(handle
)) {
1046 error
= PTR_ERR(handle
);
1050 error
= ext3_xattr_set_handle(handle
, inode
, name_index
, name
,
1051 value
, value_len
, flags
);
1052 error2
= ext3_journal_stop(handle
);
1053 if (error
== -ENOSPC
&&
1054 ext3_should_retry_alloc(inode
->i_sb
, &retries
))
1064 * ext3_xattr_delete_inode()
1066 * Free extended attribute resources associated with this inode. This
1067 * is called immediately before an inode is freed. We have exclusive
1068 * access to the inode.
1071 ext3_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1073 struct buffer_head
*bh
= NULL
;
1075 if (!EXT3_I(inode
)->i_file_acl
)
1077 bh
= sb_bread(inode
->i_sb
, EXT3_I(inode
)->i_file_acl
);
1079 ext3_error(inode
->i_sb
, __FUNCTION__
,
1080 "inode %lu: block "E3FSBLK
" read error", inode
->i_ino
,
1081 EXT3_I(inode
)->i_file_acl
);
1084 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT3_XATTR_MAGIC
) ||
1085 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1086 ext3_error(inode
->i_sb
, __FUNCTION__
,
1087 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
1088 EXT3_I(inode
)->i_file_acl
);
1091 ext3_xattr_release_block(handle
, inode
, bh
);
1092 EXT3_I(inode
)->i_file_acl
= 0;
1099 * ext3_xattr_put_super()
1101 * This is called when a file system is unmounted.
1104 ext3_xattr_put_super(struct super_block
*sb
)
1106 mb_cache_shrink(sb
->s_bdev
);
1110 * ext3_xattr_cache_insert()
1112 * Create a new entry in the extended attribute cache, and insert
1113 * it unless such an entry is already in the cache.
1115 * Returns 0, or a negative error number on failure.
1118 ext3_xattr_cache_insert(struct buffer_head
*bh
)
1120 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1121 struct mb_cache_entry
*ce
;
1124 ce
= mb_cache_entry_alloc(ext3_xattr_cache
);
1126 ea_bdebug(bh
, "out of memory");
1129 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, &hash
);
1131 mb_cache_entry_free(ce
);
1132 if (error
== -EBUSY
) {
1133 ea_bdebug(bh
, "already in cache");
1137 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1138 mb_cache_entry_release(ce
);
1145 * Compare two extended attribute blocks for equality.
1147 * Returns 0 if the blocks are equal, 1 if they differ, and
1148 * a negative error number on errors.
1151 ext3_xattr_cmp(struct ext3_xattr_header
*header1
,
1152 struct ext3_xattr_header
*header2
)
1154 struct ext3_xattr_entry
*entry1
, *entry2
;
1156 entry1
= ENTRY(header1
+1);
1157 entry2
= ENTRY(header2
+1);
1158 while (!IS_LAST_ENTRY(entry1
)) {
1159 if (IS_LAST_ENTRY(entry2
))
1161 if (entry1
->e_hash
!= entry2
->e_hash
||
1162 entry1
->e_name_index
!= entry2
->e_name_index
||
1163 entry1
->e_name_len
!= entry2
->e_name_len
||
1164 entry1
->e_value_size
!= entry2
->e_value_size
||
1165 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1167 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1169 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1170 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1171 le32_to_cpu(entry1
->e_value_size
)))
1174 entry1
= EXT3_XATTR_NEXT(entry1
);
1175 entry2
= EXT3_XATTR_NEXT(entry2
);
1177 if (!IS_LAST_ENTRY(entry2
))
1183 * ext3_xattr_cache_find()
1185 * Find an identical extended attribute block.
1187 * Returns a pointer to the block found, or NULL if such a block was
1188 * not found or an error occurred.
1190 static struct buffer_head
*
1191 ext3_xattr_cache_find(struct inode
*inode
, struct ext3_xattr_header
*header
,
1192 struct mb_cache_entry
**pce
)
1194 __u32 hash
= le32_to_cpu(header
->h_hash
);
1195 struct mb_cache_entry
*ce
;
1197 if (!header
->h_hash
)
1198 return NULL
; /* never share */
1199 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1201 ce
= mb_cache_entry_find_first(ext3_xattr_cache
, 0,
1202 inode
->i_sb
->s_bdev
, hash
);
1204 struct buffer_head
*bh
;
1207 if (PTR_ERR(ce
) == -EAGAIN
)
1211 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1213 ext3_error(inode
->i_sb
, __FUNCTION__
,
1214 "inode %lu: block %lu read error",
1215 inode
->i_ino
, (unsigned long) ce
->e_block
);
1216 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1217 EXT3_XATTR_REFCOUNT_MAX
) {
1218 ea_idebug(inode
, "block %lu refcount %d>=%d",
1219 (unsigned long) ce
->e_block
,
1220 le32_to_cpu(BHDR(bh
)->h_refcount
),
1221 EXT3_XATTR_REFCOUNT_MAX
);
1222 } else if (ext3_xattr_cmp(header
, BHDR(bh
)) == 0) {
1227 ce
= mb_cache_entry_find_next(ce
, 0, inode
->i_sb
->s_bdev
, hash
);
1232 #define NAME_HASH_SHIFT 5
1233 #define VALUE_HASH_SHIFT 16
1236 * ext3_xattr_hash_entry()
1238 * Compute the hash of an extended attribute.
1240 static inline void ext3_xattr_hash_entry(struct ext3_xattr_header
*header
,
1241 struct ext3_xattr_entry
*entry
)
1244 char *name
= entry
->e_name
;
1247 for (n
=0; n
< entry
->e_name_len
; n
++) {
1248 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1249 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1253 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1254 __le32
*value
= (__le32
*)((char *)header
+
1255 le16_to_cpu(entry
->e_value_offs
));
1256 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1257 EXT3_XATTR_ROUND
) >> EXT3_XATTR_PAD_BITS
; n
; n
--) {
1258 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1259 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1260 le32_to_cpu(*value
++);
1263 entry
->e_hash
= cpu_to_le32(hash
);
1266 #undef NAME_HASH_SHIFT
1267 #undef VALUE_HASH_SHIFT
1269 #define BLOCK_HASH_SHIFT 16
1272 * ext3_xattr_rehash()
1274 * Re-compute the extended attribute hash value after an entry has changed.
1276 static void ext3_xattr_rehash(struct ext3_xattr_header
*header
,
1277 struct ext3_xattr_entry
*entry
)
1279 struct ext3_xattr_entry
*here
;
1282 ext3_xattr_hash_entry(header
, entry
);
1283 here
= ENTRY(header
+1);
1284 while (!IS_LAST_ENTRY(here
)) {
1285 if (!here
->e_hash
) {
1286 /* Block is not shared if an entry's hash value == 0 */
1290 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1291 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1292 le32_to_cpu(here
->e_hash
);
1293 here
= EXT3_XATTR_NEXT(here
);
1295 header
->h_hash
= cpu_to_le32(hash
);
1298 #undef BLOCK_HASH_SHIFT
1301 init_ext3_xattr(void)
1303 ext3_xattr_cache
= mb_cache_create("ext3_xattr", NULL
,
1304 sizeof(struct mb_cache_entry
) +
1305 sizeof(((struct mb_cache_entry
*) 0)->e_indexes
[0]), 1, 6);
1306 if (!ext3_xattr_cache
)
1312 exit_ext3_xattr(void)
1314 if (ext3_xattr_cache
)
1315 mb_cache_destroy(ext3_xattr_cache
);
1316 ext3_xattr_cache
= NULL
;