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
*);
102 static int ext3_xattr_list(struct inode
*inode
, char *buffer
,
105 static struct mb_cache
*ext3_xattr_cache
;
107 static struct xattr_handler
*ext3_xattr_handler_map
[] = {
108 [EXT3_XATTR_INDEX_USER
] = &ext3_xattr_user_handler
,
109 #ifdef CONFIG_EXT3_FS_POSIX_ACL
110 [EXT3_XATTR_INDEX_POSIX_ACL_ACCESS
] = &ext3_xattr_acl_access_handler
,
111 [EXT3_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &ext3_xattr_acl_default_handler
,
113 [EXT3_XATTR_INDEX_TRUSTED
] = &ext3_xattr_trusted_handler
,
114 #ifdef CONFIG_EXT3_FS_SECURITY
115 [EXT3_XATTR_INDEX_SECURITY
] = &ext3_xattr_security_handler
,
119 struct xattr_handler
*ext3_xattr_handlers
[] = {
120 &ext3_xattr_user_handler
,
121 &ext3_xattr_trusted_handler
,
122 #ifdef CONFIG_EXT3_FS_POSIX_ACL
123 &ext3_xattr_acl_access_handler
,
124 &ext3_xattr_acl_default_handler
,
126 #ifdef CONFIG_EXT3_FS_SECURITY
127 &ext3_xattr_security_handler
,
132 static inline struct xattr_handler
*
133 ext3_xattr_handler(int name_index
)
135 struct xattr_handler
*handler
= NULL
;
137 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext3_xattr_handler_map
))
138 handler
= ext3_xattr_handler_map
[name_index
];
143 * Inode operation listxattr()
145 * dentry->d_inode->i_mutex: don't care
148 ext3_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
150 return ext3_xattr_list(dentry
->d_inode
, buffer
, size
);
154 ext3_xattr_check_names(struct ext3_xattr_entry
*entry
, void *end
)
156 while (!IS_LAST_ENTRY(entry
)) {
157 struct ext3_xattr_entry
*next
= EXT3_XATTR_NEXT(entry
);
158 if ((void *)next
>= end
)
166 ext3_xattr_check_block(struct buffer_head
*bh
)
170 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT3_XATTR_MAGIC
) ||
171 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
173 error
= ext3_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
);
178 ext3_xattr_check_entry(struct ext3_xattr_entry
*entry
, size_t size
)
180 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
182 if (entry
->e_value_block
!= 0 || value_size
> size
||
183 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
189 ext3_xattr_find_entry(struct ext3_xattr_entry
**pentry
, int name_index
,
190 const char *name
, size_t size
, int sorted
)
192 struct ext3_xattr_entry
*entry
;
198 name_len
= strlen(name
);
200 for (; !IS_LAST_ENTRY(entry
); entry
= EXT3_XATTR_NEXT(entry
)) {
201 cmp
= name_index
- entry
->e_name_index
;
203 cmp
= name_len
- entry
->e_name_len
;
205 cmp
= memcmp(name
, entry
->e_name
, name_len
);
206 if (cmp
<= 0 && (sorted
|| cmp
== 0))
210 if (!cmp
&& ext3_xattr_check_entry(entry
, size
))
212 return cmp
? -ENODATA
: 0;
216 ext3_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
217 void *buffer
, size_t buffer_size
)
219 struct buffer_head
*bh
= NULL
;
220 struct ext3_xattr_entry
*entry
;
224 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
225 name_index
, name
, buffer
, (long)buffer_size
);
228 if (!EXT3_I(inode
)->i_file_acl
)
230 ea_idebug(inode
, "reading block %u", EXT3_I(inode
)->i_file_acl
);
231 bh
= sb_bread(inode
->i_sb
, EXT3_I(inode
)->i_file_acl
);
234 ea_bdebug(bh
, "b_count=%d, refcount=%d",
235 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
236 if (ext3_xattr_check_block(bh
)) {
237 bad_block
: ext3_error(inode
->i_sb
, __func__
,
238 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
239 EXT3_I(inode
)->i_file_acl
);
243 ext3_xattr_cache_insert(bh
);
245 error
= ext3_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
250 size
= le32_to_cpu(entry
->e_value_size
);
253 if (size
> buffer_size
)
255 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
266 ext3_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
267 void *buffer
, size_t buffer_size
)
269 struct ext3_xattr_ibody_header
*header
;
270 struct ext3_xattr_entry
*entry
;
271 struct ext3_inode
*raw_inode
;
272 struct ext3_iloc iloc
;
277 if (!(EXT3_I(inode
)->i_state
& EXT3_STATE_XATTR
))
279 error
= ext3_get_inode_loc(inode
, &iloc
);
282 raw_inode
= ext3_raw_inode(&iloc
);
283 header
= IHDR(inode
, raw_inode
);
284 entry
= IFIRST(header
);
285 end
= (void *)raw_inode
+ EXT3_SB(inode
->i_sb
)->s_inode_size
;
286 error
= ext3_xattr_check_names(entry
, end
);
289 error
= ext3_xattr_find_entry(&entry
, name_index
, name
,
290 end
- (void *)entry
, 0);
293 size
= le32_to_cpu(entry
->e_value_size
);
296 if (size
> buffer_size
)
298 memcpy(buffer
, (void *)IFIRST(header
) +
299 le16_to_cpu(entry
->e_value_offs
), size
);
311 * Copy an extended attribute into the buffer
312 * provided, or compute the buffer size required.
313 * Buffer is NULL to compute the size of the buffer required.
315 * Returns a negative error number on failure, or the number of bytes
316 * used / required on success.
319 ext3_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
320 void *buffer
, size_t buffer_size
)
324 down_read(&EXT3_I(inode
)->xattr_sem
);
325 error
= ext3_xattr_ibody_get(inode
, name_index
, name
, buffer
,
327 if (error
== -ENODATA
)
328 error
= ext3_xattr_block_get(inode
, name_index
, name
, buffer
,
330 up_read(&EXT3_I(inode
)->xattr_sem
);
335 ext3_xattr_list_entries(struct inode
*inode
, struct ext3_xattr_entry
*entry
,
336 char *buffer
, size_t buffer_size
)
338 size_t rest
= buffer_size
;
340 for (; !IS_LAST_ENTRY(entry
); entry
= EXT3_XATTR_NEXT(entry
)) {
341 struct xattr_handler
*handler
=
342 ext3_xattr_handler(entry
->e_name_index
);
345 size_t size
= handler
->list(inode
, buffer
, rest
,
356 return buffer_size
- rest
;
360 ext3_xattr_block_list(struct inode
*inode
, char *buffer
, size_t buffer_size
)
362 struct buffer_head
*bh
= NULL
;
365 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
366 buffer
, (long)buffer_size
);
369 if (!EXT3_I(inode
)->i_file_acl
)
371 ea_idebug(inode
, "reading block %u", EXT3_I(inode
)->i_file_acl
);
372 bh
= sb_bread(inode
->i_sb
, EXT3_I(inode
)->i_file_acl
);
376 ea_bdebug(bh
, "b_count=%d, refcount=%d",
377 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
378 if (ext3_xattr_check_block(bh
)) {
379 ext3_error(inode
->i_sb
, __func__
,
380 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
381 EXT3_I(inode
)->i_file_acl
);
385 ext3_xattr_cache_insert(bh
);
386 error
= ext3_xattr_list_entries(inode
, BFIRST(bh
), buffer
, buffer_size
);
395 ext3_xattr_ibody_list(struct inode
*inode
, char *buffer
, size_t buffer_size
)
397 struct ext3_xattr_ibody_header
*header
;
398 struct ext3_inode
*raw_inode
;
399 struct ext3_iloc iloc
;
403 if (!(EXT3_I(inode
)->i_state
& EXT3_STATE_XATTR
))
405 error
= ext3_get_inode_loc(inode
, &iloc
);
408 raw_inode
= ext3_raw_inode(&iloc
);
409 header
= IHDR(inode
, raw_inode
);
410 end
= (void *)raw_inode
+ EXT3_SB(inode
->i_sb
)->s_inode_size
;
411 error
= ext3_xattr_check_names(IFIRST(header
), end
);
414 error
= ext3_xattr_list_entries(inode
, IFIRST(header
),
415 buffer
, buffer_size
);
425 * Copy a list of attribute names into the buffer
426 * provided, or compute the buffer size required.
427 * Buffer is NULL to compute the size of the buffer required.
429 * Returns a negative error number on failure, or the number of bytes
430 * used / required on success.
433 ext3_xattr_list(struct inode
*inode
, char *buffer
, size_t buffer_size
)
435 int i_error
, b_error
;
437 down_read(&EXT3_I(inode
)->xattr_sem
);
438 i_error
= ext3_xattr_ibody_list(inode
, buffer
, buffer_size
);
444 buffer_size
-= i_error
;
446 b_error
= ext3_xattr_block_list(inode
, buffer
, buffer_size
);
450 up_read(&EXT3_I(inode
)->xattr_sem
);
451 return i_error
+ b_error
;
455 * If the EXT3_FEATURE_COMPAT_EXT_ATTR feature of this file system is
458 static void ext3_xattr_update_super_block(handle_t
*handle
,
459 struct super_block
*sb
)
461 if (EXT3_HAS_COMPAT_FEATURE(sb
, EXT3_FEATURE_COMPAT_EXT_ATTR
))
464 if (ext3_journal_get_write_access(handle
, EXT3_SB(sb
)->s_sbh
) == 0) {
465 EXT3_SET_COMPAT_FEATURE(sb
, EXT3_FEATURE_COMPAT_EXT_ATTR
);
466 ext3_journal_dirty_metadata(handle
, EXT3_SB(sb
)->s_sbh
);
471 * Release the xattr block BH: If the reference count is > 1, decrement
472 * it; otherwise free the block.
475 ext3_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
476 struct buffer_head
*bh
)
478 struct mb_cache_entry
*ce
= NULL
;
481 ce
= mb_cache_entry_get(ext3_xattr_cache
, bh
->b_bdev
, bh
->b_blocknr
);
482 error
= ext3_journal_get_write_access(handle
, bh
);
488 if (BHDR(bh
)->h_refcount
== cpu_to_le32(1)) {
489 ea_bdebug(bh
, "refcount now=0; freeing");
491 mb_cache_entry_free(ce
);
492 ext3_free_blocks(handle
, inode
, bh
->b_blocknr
, 1);
494 ext3_forget(handle
, 1, inode
, bh
, bh
->b_blocknr
);
496 le32_add_cpu(&BHDR(bh
)->h_refcount
, -1);
497 error
= ext3_journal_dirty_metadata(handle
, bh
);
500 vfs_dq_free_block(inode
, 1);
501 ea_bdebug(bh
, "refcount now=%d; releasing",
502 le32_to_cpu(BHDR(bh
)->h_refcount
));
504 mb_cache_entry_release(ce
);
508 ext3_std_error(inode
->i_sb
, error
);
512 struct ext3_xattr_info
{
519 struct ext3_xattr_search
{
520 struct ext3_xattr_entry
*first
;
523 struct ext3_xattr_entry
*here
;
528 ext3_xattr_set_entry(struct ext3_xattr_info
*i
, struct ext3_xattr_search
*s
)
530 struct ext3_xattr_entry
*last
;
531 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
533 /* Compute min_offs and last. */
535 for (; !IS_LAST_ENTRY(last
); last
= EXT3_XATTR_NEXT(last
)) {
536 if (!last
->e_value_block
&& last
->e_value_size
) {
537 size_t offs
= le16_to_cpu(last
->e_value_offs
);
542 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
544 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
545 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
546 free
+= EXT3_XATTR_SIZE(size
);
548 free
+= EXT3_XATTR_LEN(name_len
);
551 if (free
< EXT3_XATTR_SIZE(i
->value_len
) ||
552 free
< EXT3_XATTR_LEN(name_len
) +
553 EXT3_XATTR_SIZE(i
->value_len
))
557 if (i
->value
&& s
->not_found
) {
558 /* Insert the new name. */
559 size_t size
= EXT3_XATTR_LEN(name_len
);
560 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
561 memmove((void *)s
->here
+ size
, s
->here
, rest
);
562 memset(s
->here
, 0, size
);
563 s
->here
->e_name_index
= i
->name_index
;
564 s
->here
->e_name_len
= name_len
;
565 memcpy(s
->here
->e_name
, i
->name
, name_len
);
567 if (!s
->here
->e_value_block
&& s
->here
->e_value_size
) {
568 void *first_val
= s
->base
+ min_offs
;
569 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
570 void *val
= s
->base
+ offs
;
571 size_t size
= EXT3_XATTR_SIZE(
572 le32_to_cpu(s
->here
->e_value_size
));
574 if (i
->value
&& size
== EXT3_XATTR_SIZE(i
->value_len
)) {
575 /* The old and the new value have the same
576 size. Just replace. */
577 s
->here
->e_value_size
=
578 cpu_to_le32(i
->value_len
);
579 memset(val
+ size
- EXT3_XATTR_PAD
, 0,
580 EXT3_XATTR_PAD
); /* Clear pad bytes. */
581 memcpy(val
, i
->value
, i
->value_len
);
585 /* Remove the old value. */
586 memmove(first_val
+ size
, first_val
, val
- first_val
);
587 memset(first_val
, 0, size
);
588 s
->here
->e_value_size
= 0;
589 s
->here
->e_value_offs
= 0;
592 /* Adjust all value offsets. */
594 while (!IS_LAST_ENTRY(last
)) {
595 size_t o
= le16_to_cpu(last
->e_value_offs
);
596 if (!last
->e_value_block
&&
597 last
->e_value_size
&& o
< offs
)
599 cpu_to_le16(o
+ size
);
600 last
= EXT3_XATTR_NEXT(last
);
604 /* Remove the old name. */
605 size_t size
= EXT3_XATTR_LEN(name_len
);
606 last
= ENTRY((void *)last
- size
);
607 memmove(s
->here
, (void *)s
->here
+ size
,
608 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
609 memset(last
, 0, size
);
614 /* Insert the new value. */
615 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
617 size_t size
= EXT3_XATTR_SIZE(i
->value_len
);
618 void *val
= s
->base
+ min_offs
- size
;
619 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
620 memset(val
+ size
- EXT3_XATTR_PAD
, 0,
621 EXT3_XATTR_PAD
); /* Clear the pad bytes. */
622 memcpy(val
, i
->value
, i
->value_len
);
628 struct ext3_xattr_block_find
{
629 struct ext3_xattr_search s
;
630 struct buffer_head
*bh
;
634 ext3_xattr_block_find(struct inode
*inode
, struct ext3_xattr_info
*i
,
635 struct ext3_xattr_block_find
*bs
)
637 struct super_block
*sb
= inode
->i_sb
;
640 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
641 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
643 if (EXT3_I(inode
)->i_file_acl
) {
644 /* The inode already has an extended attribute block. */
645 bs
->bh
= sb_bread(sb
, EXT3_I(inode
)->i_file_acl
);
649 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
650 atomic_read(&(bs
->bh
->b_count
)),
651 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
652 if (ext3_xattr_check_block(bs
->bh
)) {
653 ext3_error(sb
, __func__
,
654 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
655 EXT3_I(inode
)->i_file_acl
);
659 /* Find the named attribute. */
660 bs
->s
.base
= BHDR(bs
->bh
);
661 bs
->s
.first
= BFIRST(bs
->bh
);
662 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
663 bs
->s
.here
= bs
->s
.first
;
664 error
= ext3_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
665 i
->name
, bs
->bh
->b_size
, 1);
666 if (error
&& error
!= -ENODATA
)
668 bs
->s
.not_found
= error
;
677 ext3_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
678 struct ext3_xattr_info
*i
,
679 struct ext3_xattr_block_find
*bs
)
681 struct super_block
*sb
= inode
->i_sb
;
682 struct buffer_head
*new_bh
= NULL
;
683 struct ext3_xattr_search
*s
= &bs
->s
;
684 struct mb_cache_entry
*ce
= NULL
;
687 #define header(x) ((struct ext3_xattr_header *)(x))
689 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
692 ce
= mb_cache_entry_get(ext3_xattr_cache
, bs
->bh
->b_bdev
,
694 error
= ext3_journal_get_write_access(handle
, bs
->bh
);
699 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
701 mb_cache_entry_free(ce
);
704 ea_bdebug(bs
->bh
, "modifying in-place");
705 error
= ext3_xattr_set_entry(i
, s
);
707 if (!IS_LAST_ENTRY(s
->first
))
708 ext3_xattr_rehash(header(s
->base
),
710 ext3_xattr_cache_insert(bs
->bh
);
712 unlock_buffer(bs
->bh
);
716 error
= ext3_journal_dirty_metadata(handle
,
722 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
724 unlock_buffer(bs
->bh
);
725 journal_release_buffer(handle
, bs
->bh
);
728 mb_cache_entry_release(ce
);
731 ea_bdebug(bs
->bh
, "cloning");
732 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
736 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
737 s
->first
= ENTRY(header(s
->base
)+1);
738 header(s
->base
)->h_refcount
= cpu_to_le32(1);
739 s
->here
= ENTRY(s
->base
+ offset
);
740 s
->end
= s
->base
+ bs
->bh
->b_size
;
743 /* Allocate a buffer where we construct the new block. */
744 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
745 /* assert(header == s->base) */
749 header(s
->base
)->h_magic
= cpu_to_le32(EXT3_XATTR_MAGIC
);
750 header(s
->base
)->h_blocks
= cpu_to_le32(1);
751 header(s
->base
)->h_refcount
= cpu_to_le32(1);
752 s
->first
= ENTRY(header(s
->base
)+1);
753 s
->here
= ENTRY(header(s
->base
)+1);
754 s
->end
= s
->base
+ sb
->s_blocksize
;
757 error
= ext3_xattr_set_entry(i
, s
);
762 if (!IS_LAST_ENTRY(s
->first
))
763 ext3_xattr_rehash(header(s
->base
), s
->here
);
766 if (!IS_LAST_ENTRY(s
->first
)) {
767 new_bh
= ext3_xattr_cache_find(inode
, header(s
->base
), &ce
);
769 /* We found an identical block in the cache. */
770 if (new_bh
== bs
->bh
)
771 ea_bdebug(new_bh
, "keeping");
773 /* The old block is released after updating
776 if (vfs_dq_alloc_block(inode
, 1))
778 error
= ext3_journal_get_write_access(handle
,
783 le32_add_cpu(&BHDR(new_bh
)->h_refcount
, 1);
784 ea_bdebug(new_bh
, "reusing; refcount now=%d",
785 le32_to_cpu(BHDR(new_bh
)->h_refcount
));
786 unlock_buffer(new_bh
);
787 error
= ext3_journal_dirty_metadata(handle
,
792 mb_cache_entry_release(ce
);
794 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
795 /* We were modifying this block in-place. */
796 ea_bdebug(bs
->bh
, "keeping this block");
800 /* We need to allocate a new block */
801 ext3_fsblk_t goal
= ext3_group_first_block_no(sb
,
802 EXT3_I(inode
)->i_block_group
);
803 ext3_fsblk_t block
= ext3_new_block(handle
, inode
,
807 ea_idebug(inode
, "creating block %d", block
);
809 new_bh
= sb_getblk(sb
, block
);
812 ext3_free_blocks(handle
, inode
, block
, 1);
817 error
= ext3_journal_get_create_access(handle
, new_bh
);
819 unlock_buffer(new_bh
);
822 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
823 set_buffer_uptodate(new_bh
);
824 unlock_buffer(new_bh
);
825 ext3_xattr_cache_insert(new_bh
);
826 error
= ext3_journal_dirty_metadata(handle
, new_bh
);
832 /* Update the inode. */
833 EXT3_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
835 /* Drop the previous xattr block. */
836 if (bs
->bh
&& bs
->bh
!= new_bh
)
837 ext3_xattr_release_block(handle
, inode
, bs
->bh
);
842 mb_cache_entry_release(ce
);
844 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
850 vfs_dq_free_block(inode
, 1);
854 ext3_error(inode
->i_sb
, __func__
,
855 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
856 EXT3_I(inode
)->i_file_acl
);
862 struct ext3_xattr_ibody_find
{
863 struct ext3_xattr_search s
;
864 struct ext3_iloc iloc
;
868 ext3_xattr_ibody_find(struct inode
*inode
, struct ext3_xattr_info
*i
,
869 struct ext3_xattr_ibody_find
*is
)
871 struct ext3_xattr_ibody_header
*header
;
872 struct ext3_inode
*raw_inode
;
875 if (EXT3_I(inode
)->i_extra_isize
== 0)
877 raw_inode
= ext3_raw_inode(&is
->iloc
);
878 header
= IHDR(inode
, raw_inode
);
879 is
->s
.base
= is
->s
.first
= IFIRST(header
);
880 is
->s
.here
= is
->s
.first
;
881 is
->s
.end
= (void *)raw_inode
+ EXT3_SB(inode
->i_sb
)->s_inode_size
;
882 if (EXT3_I(inode
)->i_state
& EXT3_STATE_XATTR
) {
883 error
= ext3_xattr_check_names(IFIRST(header
), is
->s
.end
);
886 /* Find the named attribute. */
887 error
= ext3_xattr_find_entry(&is
->s
.here
, i
->name_index
,
889 (void *)is
->s
.base
, 0);
890 if (error
&& error
!= -ENODATA
)
892 is
->s
.not_found
= error
;
898 ext3_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
899 struct ext3_xattr_info
*i
,
900 struct ext3_xattr_ibody_find
*is
)
902 struct ext3_xattr_ibody_header
*header
;
903 struct ext3_xattr_search
*s
= &is
->s
;
906 if (EXT3_I(inode
)->i_extra_isize
== 0)
908 error
= ext3_xattr_set_entry(i
, s
);
911 header
= IHDR(inode
, ext3_raw_inode(&is
->iloc
));
912 if (!IS_LAST_ENTRY(s
->first
)) {
913 header
->h_magic
= cpu_to_le32(EXT3_XATTR_MAGIC
);
914 EXT3_I(inode
)->i_state
|= EXT3_STATE_XATTR
;
916 header
->h_magic
= cpu_to_le32(0);
917 EXT3_I(inode
)->i_state
&= ~EXT3_STATE_XATTR
;
923 * ext3_xattr_set_handle()
925 * Create, replace or remove an extended attribute for this inode. Buffer
926 * is NULL to remove an existing extended attribute, and non-NULL to
927 * either replace an existing extended attribute, or create a new extended
928 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
929 * specify that an extended attribute must exist and must not exist
930 * previous to the call, respectively.
932 * Returns 0, or a negative error number on failure.
935 ext3_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
936 const char *name
, const void *value
, size_t value_len
,
939 struct ext3_xattr_info i
= {
940 .name_index
= name_index
,
943 .value_len
= value_len
,
946 struct ext3_xattr_ibody_find is
= {
947 .s
= { .not_found
= -ENODATA
, },
949 struct ext3_xattr_block_find bs
= {
950 .s
= { .not_found
= -ENODATA
, },
956 if (strlen(name
) > 255)
958 down_write(&EXT3_I(inode
)->xattr_sem
);
959 error
= ext3_get_inode_loc(inode
, &is
.iloc
);
963 error
= ext3_journal_get_write_access(handle
, is
.iloc
.bh
);
967 if (EXT3_I(inode
)->i_state
& EXT3_STATE_NEW
) {
968 struct ext3_inode
*raw_inode
= ext3_raw_inode(&is
.iloc
);
969 memset(raw_inode
, 0, EXT3_SB(inode
->i_sb
)->s_inode_size
);
970 EXT3_I(inode
)->i_state
&= ~EXT3_STATE_NEW
;
973 error
= ext3_xattr_ibody_find(inode
, &i
, &is
);
977 error
= ext3_xattr_block_find(inode
, &i
, &bs
);
980 if (is
.s
.not_found
&& bs
.s
.not_found
) {
982 if (flags
& XATTR_REPLACE
)
989 if (flags
& XATTR_CREATE
)
994 error
= ext3_xattr_ibody_set(handle
, inode
, &i
, &is
);
995 else if (!bs
.s
.not_found
)
996 error
= ext3_xattr_block_set(handle
, inode
, &i
, &bs
);
998 error
= ext3_xattr_ibody_set(handle
, inode
, &i
, &is
);
999 if (!error
&& !bs
.s
.not_found
) {
1001 error
= ext3_xattr_block_set(handle
, inode
, &i
, &bs
);
1002 } else if (error
== -ENOSPC
) {
1003 if (EXT3_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1004 error
= ext3_xattr_block_find(inode
, &i
, &bs
);
1008 error
= ext3_xattr_block_set(handle
, inode
, &i
, &bs
);
1011 if (!is
.s
.not_found
) {
1013 error
= ext3_xattr_ibody_set(handle
, inode
, &i
,
1019 ext3_xattr_update_super_block(handle
, inode
->i_sb
);
1020 inode
->i_ctime
= CURRENT_TIME_SEC
;
1021 error
= ext3_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1023 * The bh is consumed by ext3_mark_iloc_dirty, even with
1034 up_write(&EXT3_I(inode
)->xattr_sem
);
1041 * Like ext3_xattr_set_handle, but start from an inode. This extended
1042 * attribute modification is a filesystem transaction by itself.
1044 * Returns 0, or a negative error number on failure.
1047 ext3_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1048 const void *value
, size_t value_len
, int flags
)
1051 int error
, retries
= 0;
1054 handle
= ext3_journal_start(inode
, EXT3_DATA_TRANS_BLOCKS(inode
->i_sb
));
1055 if (IS_ERR(handle
)) {
1056 error
= PTR_ERR(handle
);
1060 error
= ext3_xattr_set_handle(handle
, inode
, name_index
, name
,
1061 value
, value_len
, flags
);
1062 error2
= ext3_journal_stop(handle
);
1063 if (error
== -ENOSPC
&&
1064 ext3_should_retry_alloc(inode
->i_sb
, &retries
))
1074 * ext3_xattr_delete_inode()
1076 * Free extended attribute resources associated with this inode. This
1077 * is called immediately before an inode is freed. We have exclusive
1078 * access to the inode.
1081 ext3_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1083 struct buffer_head
*bh
= NULL
;
1085 if (!EXT3_I(inode
)->i_file_acl
)
1087 bh
= sb_bread(inode
->i_sb
, EXT3_I(inode
)->i_file_acl
);
1089 ext3_error(inode
->i_sb
, __func__
,
1090 "inode %lu: block "E3FSBLK
" read error", inode
->i_ino
,
1091 EXT3_I(inode
)->i_file_acl
);
1094 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT3_XATTR_MAGIC
) ||
1095 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1096 ext3_error(inode
->i_sb
, __func__
,
1097 "inode %lu: bad block "E3FSBLK
, inode
->i_ino
,
1098 EXT3_I(inode
)->i_file_acl
);
1101 ext3_xattr_release_block(handle
, inode
, bh
);
1102 EXT3_I(inode
)->i_file_acl
= 0;
1109 * ext3_xattr_put_super()
1111 * This is called when a file system is unmounted.
1114 ext3_xattr_put_super(struct super_block
*sb
)
1116 mb_cache_shrink(sb
->s_bdev
);
1120 * ext3_xattr_cache_insert()
1122 * Create a new entry in the extended attribute cache, and insert
1123 * it unless such an entry is already in the cache.
1125 * Returns 0, or a negative error number on failure.
1128 ext3_xattr_cache_insert(struct buffer_head
*bh
)
1130 __u32 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1131 struct mb_cache_entry
*ce
;
1134 ce
= mb_cache_entry_alloc(ext3_xattr_cache
, GFP_NOFS
);
1136 ea_bdebug(bh
, "out of memory");
1139 error
= mb_cache_entry_insert(ce
, bh
->b_bdev
, bh
->b_blocknr
, &hash
);
1141 mb_cache_entry_free(ce
);
1142 if (error
== -EBUSY
) {
1143 ea_bdebug(bh
, "already in cache");
1147 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1148 mb_cache_entry_release(ce
);
1155 * Compare two extended attribute blocks for equality.
1157 * Returns 0 if the blocks are equal, 1 if they differ, and
1158 * a negative error number on errors.
1161 ext3_xattr_cmp(struct ext3_xattr_header
*header1
,
1162 struct ext3_xattr_header
*header2
)
1164 struct ext3_xattr_entry
*entry1
, *entry2
;
1166 entry1
= ENTRY(header1
+1);
1167 entry2
= ENTRY(header2
+1);
1168 while (!IS_LAST_ENTRY(entry1
)) {
1169 if (IS_LAST_ENTRY(entry2
))
1171 if (entry1
->e_hash
!= entry2
->e_hash
||
1172 entry1
->e_name_index
!= entry2
->e_name_index
||
1173 entry1
->e_name_len
!= entry2
->e_name_len
||
1174 entry1
->e_value_size
!= entry2
->e_value_size
||
1175 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1177 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1179 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1180 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1181 le32_to_cpu(entry1
->e_value_size
)))
1184 entry1
= EXT3_XATTR_NEXT(entry1
);
1185 entry2
= EXT3_XATTR_NEXT(entry2
);
1187 if (!IS_LAST_ENTRY(entry2
))
1193 * ext3_xattr_cache_find()
1195 * Find an identical extended attribute block.
1197 * Returns a pointer to the block found, or NULL if such a block was
1198 * not found or an error occurred.
1200 static struct buffer_head
*
1201 ext3_xattr_cache_find(struct inode
*inode
, struct ext3_xattr_header
*header
,
1202 struct mb_cache_entry
**pce
)
1204 __u32 hash
= le32_to_cpu(header
->h_hash
);
1205 struct mb_cache_entry
*ce
;
1207 if (!header
->h_hash
)
1208 return NULL
; /* never share */
1209 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1211 ce
= mb_cache_entry_find_first(ext3_xattr_cache
, 0,
1212 inode
->i_sb
->s_bdev
, hash
);
1214 struct buffer_head
*bh
;
1217 if (PTR_ERR(ce
) == -EAGAIN
)
1221 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1223 ext3_error(inode
->i_sb
, __func__
,
1224 "inode %lu: block %lu read error",
1225 inode
->i_ino
, (unsigned long) ce
->e_block
);
1226 } else if (le32_to_cpu(BHDR(bh
)->h_refcount
) >=
1227 EXT3_XATTR_REFCOUNT_MAX
) {
1228 ea_idebug(inode
, "block %lu refcount %d>=%d",
1229 (unsigned long) ce
->e_block
,
1230 le32_to_cpu(BHDR(bh
)->h_refcount
),
1231 EXT3_XATTR_REFCOUNT_MAX
);
1232 } else if (ext3_xattr_cmp(header
, BHDR(bh
)) == 0) {
1237 ce
= mb_cache_entry_find_next(ce
, 0, inode
->i_sb
->s_bdev
, hash
);
1242 #define NAME_HASH_SHIFT 5
1243 #define VALUE_HASH_SHIFT 16
1246 * ext3_xattr_hash_entry()
1248 * Compute the hash of an extended attribute.
1250 static inline void ext3_xattr_hash_entry(struct ext3_xattr_header
*header
,
1251 struct ext3_xattr_entry
*entry
)
1254 char *name
= entry
->e_name
;
1257 for (n
=0; n
< entry
->e_name_len
; n
++) {
1258 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1259 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1263 if (entry
->e_value_block
== 0 && entry
->e_value_size
!= 0) {
1264 __le32
*value
= (__le32
*)((char *)header
+
1265 le16_to_cpu(entry
->e_value_offs
));
1266 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1267 EXT3_XATTR_ROUND
) >> EXT3_XATTR_PAD_BITS
; n
; n
--) {
1268 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1269 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1270 le32_to_cpu(*value
++);
1273 entry
->e_hash
= cpu_to_le32(hash
);
1276 #undef NAME_HASH_SHIFT
1277 #undef VALUE_HASH_SHIFT
1279 #define BLOCK_HASH_SHIFT 16
1282 * ext3_xattr_rehash()
1284 * Re-compute the extended attribute hash value after an entry has changed.
1286 static void ext3_xattr_rehash(struct ext3_xattr_header
*header
,
1287 struct ext3_xattr_entry
*entry
)
1289 struct ext3_xattr_entry
*here
;
1292 ext3_xattr_hash_entry(header
, entry
);
1293 here
= ENTRY(header
+1);
1294 while (!IS_LAST_ENTRY(here
)) {
1295 if (!here
->e_hash
) {
1296 /* Block is not shared if an entry's hash value == 0 */
1300 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1301 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1302 le32_to_cpu(here
->e_hash
);
1303 here
= EXT3_XATTR_NEXT(here
);
1305 header
->h_hash
= cpu_to_le32(hash
);
1308 #undef BLOCK_HASH_SHIFT
1311 init_ext3_xattr(void)
1313 ext3_xattr_cache
= mb_cache_create("ext3_xattr", NULL
,
1314 sizeof(struct mb_cache_entry
) +
1315 sizeof(((struct mb_cache_entry
*) 0)->e_indexes
[0]), 1, 6);
1316 if (!ext3_xattr_cache
)
1322 exit_ext3_xattr(void)
1324 if (ext3_xattr_cache
)
1325 mb_cache_destroy(ext3_xattr_cache
);
1326 ext3_xattr_cache
= NULL
;