58 #include <linux/slab.h>
67 #define HDR(bh) ((struct ext2_xattr_header *)((bh)->b_data))
68 #define ENTRY(ptr) ((struct ext2_xattr_entry *)(ptr))
69 #define FIRST_ENTRY(bh) ENTRY(HDR(bh)+1)
70 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
72 #ifdef EXT2_XATTR_DEBUG
73 # define ea_idebug(inode, f...) do { \
74 printk(KERN_DEBUG "inode %s:%ld: ", \
75 inode->i_sb->s_id, inode->i_ino); \
79 # define ea_bdebug(bh, f...) do { \
80 char b[BDEVNAME_SIZE]; \
81 printk(KERN_DEBUG "block %s:%lu: ", \
82 bdevname(bh->b_bdev, b), \
83 (unsigned long) bh->b_blocknr); \
88 # define ea_idebug(f...)
89 # define ea_bdebug(f...)
92 static int ext2_xattr_set2(
struct inode *,
struct buffer_head *,
95 static int ext2_xattr_cache_insert(
struct buffer_head *);
96 static struct buffer_head *ext2_xattr_cache_find(
struct inode *,
101 static struct mb_cache *ext2_xattr_cache;
103 static const struct xattr_handler *ext2_xattr_handler_map[] = {
105 #ifdef CONFIG_EXT2_FS_POSIX_ACL
110 #ifdef CONFIG_EXT2_FS_SECURITY
118 #ifdef CONFIG_EXT2_FS_POSIX_ACL
122 #ifdef CONFIG_EXT2_FS_SECURITY
129 ext2_xattr_handler(
int name_index)
133 if (name_index > 0 && name_index <
ARRAY_SIZE(ext2_xattr_handler_map))
134 handler = ext2_xattr_handler_map[name_index];
150 void *
buffer,
size_t buffer_size)
152 struct buffer_head *bh =
NULL;
158 ea_idebug(inode,
"name=%d.%s, buffer=%p, buffer_size=%ld",
159 name_index, name, buffer, (
long)buffer_size);
169 if (!EXT2_I(inode)->i_file_acl)
171 ea_idebug(inode,
"reading block %d", EXT2_I(inode)->i_file_acl);
172 bh = sb_bread(inode->
i_sb, EXT2_I(inode)->i_file_acl);
178 end = bh->b_data + bh->b_size;
182 "inode %ld: bad block %d", inode->
i_ino,
183 EXT2_I(inode)->i_file_acl);
193 if ((
char *)next >= end)
201 if (ext2_xattr_cache_insert(bh))
210 if (size > inode->
i_sb->s_blocksize ||
214 if (ext2_xattr_cache_insert(bh))
218 if (size > buffer_size)
228 up_read(&EXT2_I(inode)->xattr_sem);
247 struct buffer_head *bh =
NULL;
250 size_t rest = buffer_size;
253 ea_idebug(inode,
"buffer=%p, buffer_size=%ld",
254 buffer, (
long)buffer_size);
258 if (!EXT2_I(inode)->i_file_acl)
260 ea_idebug(inode,
"reading block %d", EXT2_I(inode)->i_file_acl);
261 bh = sb_bread(inode->
i_sb, EXT2_I(inode)->i_file_acl);
267 end = bh->b_data + bh->b_size;
271 "inode %ld: bad block %d", inode->
i_ino,
272 EXT2_I(inode)->i_file_acl);
282 if ((
char *)next >= end)
286 if (ext2_xattr_cache_insert(bh))
296 size_t size = handler->
list(dentry, buffer, rest,
310 error = buffer_size -
rest;
314 up_read(&EXT2_I(inode)->xattr_sem);
327 return ext2_xattr_list(dentry, buffer, size);
334 static void ext2_xattr_update_super_block(
struct super_block *
sb)
339 spin_lock(&EXT2_SB(sb)->s_lock);
341 spin_unlock(&EXT2_SB(sb)->s_lock);
362 struct buffer_head *bh =
NULL;
366 int not_found = 1,
error;
381 ea_idebug(inode,
"name=%d.%s, value=%p, value_len=%ld",
382 name_index, name, value, (
long)value_len);
392 if (EXT2_I(inode)->i_file_acl) {
394 bh = sb_bread(sb, EXT2_I(inode)->i_file_acl);
402 end = bh->b_data + bh->b_size;
406 "inode %ld: bad block %d", inode->
i_ino,
407 EXT2_I(inode)->i_file_acl);
415 if ((
char *)next >= end)
435 if ((
char *)next >= end)
446 free = min_offs - ((
char*)last - (
char*)header) -
sizeof(
__u32);
509 offset = (
char *)here - bh->b_data;
511 offset = (
char *)last - bh->b_data;
523 last = here =
ENTRY(header+1);
531 size_t rest = (
char *)last - (
char *)
here;
532 memmove((
char *)here + size, here, rest);
539 char *first_val = (
char *)header + min_offs;
541 char *
val = (
char *)header + offs;
551 memcpy(val, value, value_len);
556 memmove(first_val + size, first_val, val - first_val);
557 memset(first_val, 0, size);
562 last =
ENTRY(header+1);
574 last =
ENTRY((
char *)last - size);
575 memmove(here, (
char*)here + size,
576 (
char*)last - (
char*)here);
586 char *
val = (
char *)header + min_offs - size;
591 memcpy(val, value, value_len);
598 if (bh && header ==
HDR(bh))
600 error = ext2_xattr_set2(inode, bh,
NULL);
602 ext2_xattr_rehash(header, here);
603 if (bh && header ==
HDR(bh))
605 error = ext2_xattr_set2(inode, bh, header);
610 if (!(bh && header ==
HDR(bh)))
612 up_write(&EXT2_I(inode)->xattr_sem);
621 ext2_xattr_set2(
struct inode *inode,
struct buffer_head *old_bh,
625 struct buffer_head *new_bh =
NULL;
629 new_bh = ext2_xattr_cache_find(inode, header);
632 if (new_bh == old_bh) {
639 error = dquot_alloc_block(inode, 1);
644 le32_add_cpu(&
HDR(new_bh)->h_refcount, 1);
649 }
else if (old_bh && header ==
HDR(old_bh)) {
654 ext2_xattr_cache_insert(new_bh);
658 EXT2_I(inode)->i_block_group);
662 ea_idebug(inode,
"creating block %d", block);
664 new_bh = sb_getblk(sb, block);
667 mark_inode_dirty(inode);
672 memcpy(new_bh->b_data, header, new_bh->b_size);
673 set_buffer_uptodate(new_bh);
675 ext2_xattr_cache_insert(new_bh);
677 ext2_xattr_update_super_block(sb);
683 if (buffer_req(new_bh) && !buffer_uptodate(new_bh))
689 EXT2_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
696 if (error && error != -
ENOSPC) {
697 if (new_bh && new_bh != old_bh) {
698 dquot_free_block_nodirty(inode, 1);
699 mark_inode_dirty(inode);
704 mark_inode_dirty(inode);
707 if (old_bh && old_bh != new_bh) {
723 mark_inode_dirty(inode);
730 le32_add_cpu(&
HDR(old_bh)->h_refcount, -1);
733 dquot_free_block_nodirty(inode, 1);
734 mark_inode_dirty(inode);
757 struct buffer_head *bh =
NULL;
761 if (!EXT2_I(inode)->i_file_acl)
763 bh = sb_bread(inode->
i_sb, EXT2_I(inode)->i_file_acl);
766 "inode %ld: block %d read error", inode->
i_ino,
767 EXT2_I(inode)->i_file_acl);
774 "inode %ld: bad block %d", inode->
i_ino,
775 EXT2_I(inode)->i_file_acl);
788 le32_add_cpu(&
HDR(bh)->h_refcount, -1);
797 dquot_free_block_nodirty(inode, 1);
799 EXT2_I(inode)->i_file_acl = 0;
803 up_write(&EXT2_I(inode)->xattr_sem);
827 ext2_xattr_cache_insert(
struct buffer_head *bh)
839 if (error == -
EBUSY) {
840 ea_bdebug(bh,
"already in cache (%d cache entries)",
845 ea_bdebug(bh,
"inserting [%x] (%d cache entries)", (
int)hash,
866 entry1 =
ENTRY(header1+1);
867 entry2 =
ENTRY(header2+1);
900 static struct buffer_head *
908 ea_idebug(inode,
"looking for cached blocks [%x]", (
int)hash);
913 struct buffer_head *bh;
916 if (PTR_ERR(ce) == -
EAGAIN)
924 "inode %ld: block %ld read error",
930 ea_idebug(inode,
"block %ld refcount %d>%d",
934 }
else if (!ext2_xattr_cmp(header,
HDR(bh))) {
948 #define NAME_HASH_SHIFT 5
949 #define VALUE_HASH_SHIFT 16
982 #undef NAME_HASH_SHIFT
983 #undef VALUE_HASH_SHIFT
985 #define BLOCK_HASH_SHIFT 16
998 ext2_xattr_hash_entry(header, entry);
999 here =
ENTRY(header+1);
1014 #undef BLOCK_HASH_SHIFT
1020 if (!ext2_xattr_cache)