59 #define BHDR(bh) ((struct ext3_xattr_header *)((bh)->b_data))
60 #define ENTRY(ptr) ((struct ext3_xattr_entry *)(ptr))
61 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
62 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
64 #define IHDR(inode, raw_inode) \
65 ((struct ext3_xattr_ibody_header *) \
66 ((void *)raw_inode + \
67 EXT3_GOOD_OLD_INODE_SIZE + \
68 EXT3_I(inode)->i_extra_isize))
69 #define IFIRST(hdr) ((struct ext3_xattr_entry *)((hdr)+1))
71 #ifdef EXT3_XATTR_DEBUG
72 # define ea_idebug(inode, f...) do { \
73 printk(KERN_DEBUG "inode %s:%lu: ", \
74 inode->i_sb->s_id, inode->i_ino); \
78 # define ea_bdebug(bh, f...) do { \
79 char b[BDEVNAME_SIZE]; \
80 printk(KERN_DEBUG "block %s:%lu: ", \
81 bdevname(bh->b_bdev, b), \
82 (unsigned long) bh->b_blocknr); \
87 # define ea_idebug(f...)
88 # define ea_bdebug(f...)
91 static void ext3_xattr_cache_insert(
struct buffer_head *);
92 static struct buffer_head *ext3_xattr_cache_find(
struct inode *,
100 static struct mb_cache *ext3_xattr_cache;
102 static const struct xattr_handler *ext3_xattr_handler_map[] = {
104 #ifdef CONFIG_EXT3_FS_POSIX_ACL
109 #ifdef CONFIG_EXT3_FS_SECURITY
117 #ifdef CONFIG_EXT3_FS_POSIX_ACL
121 #ifdef CONFIG_EXT3_FS_SECURITY
128 ext3_xattr_handler(
int name_index)
132 if (name_index > 0 && name_index <
ARRAY_SIZE(ext3_xattr_handler_map))
133 handler = ext3_xattr_handler_map[name_index];
145 return ext3_xattr_list(dentry, buffer, size);
153 if ((
void *)next >= end)
161 ext3_xattr_check_block(
struct buffer_head *bh)
168 error = ext3_xattr_check_names(
BFIRST(bh), bh->b_data + bh->b_size);
185 const char *
name,
size_t size,
int sorted)
201 if (cmp <= 0 && (sorted || cmp == 0))
205 if (!cmp && ext3_xattr_check_entry(entry, size))
211 ext3_xattr_block_get(
struct inode *
inode,
int name_index,
const char *name,
212 void *
buffer,
size_t buffer_size)
214 struct buffer_head *bh =
NULL;
219 ea_idebug(inode,
"name=%d.%s, buffer=%p, buffer_size=%ld",
220 name_index, name, buffer, (
long)buffer_size);
223 if (!EXT3_I(inode)->i_file_acl)
225 ea_idebug(inode,
"reading block %u", EXT3_I(inode)->i_file_acl);
226 bh = sb_bread(inode->
i_sb, EXT3_I(inode)->i_file_acl);
231 if (ext3_xattr_check_block(bh)) {
234 EXT3_I(inode)->i_file_acl);
238 ext3_xattr_cache_insert(bh);
240 error = ext3_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
248 if (size > buffer_size)
261 ext3_xattr_ibody_get(
struct inode *inode,
int name_index,
const char *name,
262 void *buffer,
size_t buffer_size)
277 raw_inode = ext3_raw_inode(&iloc);
278 header =
IHDR(inode, raw_inode);
280 end = (
void *)raw_inode + EXT3_SB(inode->
i_sb)->s_inode_size;
281 error = ext3_xattr_check_names(entry, end);
284 error = ext3_xattr_find_entry(&entry, name_index, name,
285 end - (
void *)entry, 0);
291 if (size > buffer_size)
315 void *buffer,
size_t buffer_size)
320 error = ext3_xattr_ibody_get(inode, name_index, name, buffer,
323 error = ext3_xattr_block_get(inode, name_index, name, buffer,
325 up_read(&EXT3_I(inode)->xattr_sem);
331 char *buffer,
size_t buffer_size)
333 size_t rest = buffer_size;
340 size_t size = handler->
list(dentry, buffer, rest,
352 return buffer_size -
rest;
356 ext3_xattr_block_list(
struct dentry *dentry,
char *buffer,
size_t buffer_size)
358 struct inode *inode = dentry->
d_inode;
359 struct buffer_head *bh =
NULL;
362 ea_idebug(inode,
"buffer=%p, buffer_size=%ld",
363 buffer, (
long)buffer_size);
366 if (!EXT3_I(inode)->i_file_acl)
368 ea_idebug(inode,
"reading block %u", EXT3_I(inode)->i_file_acl);
369 bh = sb_bread(inode->
i_sb, EXT3_I(inode)->i_file_acl);
375 if (ext3_xattr_check_block(bh)) {
378 EXT3_I(inode)->i_file_acl);
382 ext3_xattr_cache_insert(bh);
383 error = ext3_xattr_list_entries(dentry,
BFIRST(bh), buffer, buffer_size);
392 ext3_xattr_ibody_list(
struct dentry *dentry,
char *buffer,
size_t buffer_size)
394 struct inode *inode = dentry->
d_inode;
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(dentry,
IFIRST(header),
413 buffer, buffer_size);
431 ext3_xattr_list(
struct dentry *dentry,
char *buffer,
size_t buffer_size)
436 i_error = ext3_xattr_ibody_list(dentry, buffer, buffer_size);
442 buffer_size -= i_error;
444 b_error = ext3_xattr_block_list(dentry, buffer, buffer_size);
456 static void ext3_xattr_update_super_block(handle_t *
handle,
473 ext3_xattr_release_block(handle_t *handle,
struct inode *inode,
474 struct buffer_head *bh)
487 ea_bdebug(bh,
"refcount now=0; freeing");
494 le32_add_cpu(&
BHDR(bh)->h_refcount, -1);
498 dquot_free_block(inode, 1);
499 ea_bdebug(bh,
"refcount now=%d; releasing",
540 free = min_offs - ((
void *)last - s->
base) -
sizeof(
__u32);
542 if (!
s->here->e_value_block &&
s->here->e_value_size) {
555 if (
i->value &&
s->not_found) {
558 size_t rest = (
void *)last - (
void *)
s->here +
sizeof(
__u32);
559 memmove((
void *)
s->here + size,
s->here, rest);
561 s->here->e_name_index =
i->name_index;
563 memcpy(
s->here->e_name,
i->name, name_len);
565 if (!
s->here->e_value_block &&
s->here->e_value_size) {
566 void *first_val =
s->base + min_offs;
575 s->here->e_value_size =
579 memcpy(val,
i->value,
i->value_len);
584 memmove(first_val + size, first_val, val - first_val);
585 memset(first_val, 0, size);
586 s->here->e_value_size = 0;
587 s->here->e_value_offs = 0;
594 if (!
last->e_value_block &&
595 last->e_value_size && o < offs)
606 (
void *)last - (
void *)
s->here +
sizeof(
__u32));
616 void *val =
s->base + min_offs -
size;
620 memcpy(val,
i->value,
i->value_len);
628 struct buffer_head *
bh;
638 ea_idebug(inode,
"name=%d.%s, value=%p, value_len=%ld",
641 if (EXT3_I(inode)->i_file_acl) {
643 bs->
bh = sb_bread(sb, EXT3_I(inode)->i_file_acl);
650 if (ext3_xattr_check_block(bs->
bh)) {
653 EXT3_I(inode)->i_file_acl);
660 bs->
s.end = bs->
bh->b_data + bs->
bh->b_size;
661 bs->
s.here = bs->
s.first;
662 error = ext3_xattr_find_entry(&bs->
s.here, i->
name_index,
663 i->
name, bs->
bh->b_size, 1);
664 if (error && error != -
ENODATA)
675 ext3_xattr_block_set(handle_t *handle,
struct inode *inode,
680 struct buffer_head *new_bh =
NULL;
685 #define header(x) ((struct ext3_xattr_header *)(x))
703 error = ext3_xattr_set_entry(i, s);
706 ext3_xattr_rehash(header(s->
base),
708 ext3_xattr_cache_insert(bs->
bh);
755 error = ext3_xattr_set_entry(i, s);
761 ext3_xattr_rehash(header(s->
base), s->
here);
765 new_bh = ext3_xattr_cache_find(inode, header(s->
base), &ce);
768 if (new_bh == bs->
bh)
773 error = dquot_alloc_block(inode, 1);
781 le32_add_cpu(&
BHDR(new_bh)->h_refcount, 1);
782 ea_bdebug(new_bh,
"reusing; refcount now=%d",
792 }
else if (bs->
bh && s->
base == bs->
bh->b_data) {
800 EXT3_I(inode)->i_block_group);
813 ea_idebug(inode,
"creating block %d", block);
815 new_bh = sb_getblk(sb, block);
828 memcpy(new_bh->b_data, s->
base, new_bh->b_size);
829 set_buffer_uptodate(new_bh);
831 ext3_xattr_cache_insert(new_bh);
839 EXT3_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
842 if (bs->
bh && bs->
bh != new_bh)
843 ext3_xattr_release_block(handle, inode, bs->
bh);
850 if (!(bs->
bh && s->
base == bs->
bh->b_data))
856 dquot_free_block(inode, 1);
862 EXT3_I(inode)->i_file_acl);
883 raw_inode = ext3_raw_inode(&is->
iloc);
884 header =
IHDR(inode, raw_inode);
885 is->
s.base = is->
s.first =
IFIRST(header);
886 is->
s.here = is->
s.first;
887 is->
s.end = (
void *)raw_inode + EXT3_SB(inode->
i_sb)->s_inode_size;
889 error = ext3_xattr_check_names(
IFIRST(header), is->
s.end);
893 error = ext3_xattr_find_entry(&is->
s.here, i->
name_index,
895 (
void *)is->
s.base, 0);
896 if (error && error != -
ENODATA)
904 ext3_xattr_ibody_set(handle_t *handle,
struct inode *inode,
912 if (EXT3_I(inode)->i_extra_isize == 0)
914 error = ext3_xattr_set_entry(i, s);
917 header =
IHDR(inode, ext3_raw_inode(&is->
iloc));
975 memset(raw_inode, 0, EXT3_SB(inode->
i_sb)->s_inode_size);
986 if (is.
s.not_found && bs.
s.not_found) {
1000 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1001 else if (!bs.
s.not_found)
1002 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1004 error = ext3_xattr_ibody_set(handle, inode, &i, &is);
1005 if (!error && !bs.
s.not_found) {
1007 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1008 }
else if (error == -
ENOSPC) {
1014 error = ext3_xattr_block_set(handle, inode, &i, &bs);
1017 if (!is.
s.not_found) {
1019 error = ext3_xattr_ibody_set(handle, inode, &i,
1025 ext3_xattr_update_super_block(handle, inode->
i_sb);
1040 up_write(&EXT3_I(inode)->xattr_sem);
1061 if (IS_ERR(handle)) {
1062 error = PTR_ERR(handle);
1067 value, value_len, flags);
1089 struct buffer_head *bh =
NULL;
1091 if (!EXT3_I(inode)->i_file_acl)
1093 bh = sb_bread(inode->
i_sb, EXT3_I(inode)->i_file_acl);
1097 EXT3_I(inode)->i_file_acl);
1104 EXT3_I(inode)->i_file_acl);
1107 ext3_xattr_release_block(handle, inode, bh);
1108 EXT3_I(inode)->i_file_acl = 0;
1134 ext3_xattr_cache_insert(
struct buffer_head *bh)
1148 if (error == -
EBUSY) {
1153 ea_bdebug(bh,
"inserting [%x]", (
int)hash);
1172 entry1 =
ENTRY(header1+1);
1173 entry2 =
ENTRY(header2+1);
1206 static struct buffer_head *
1215 ea_idebug(inode,
"looking for cached blocks [%x]", (
int)hash);
1220 struct buffer_head *bh;
1223 if (PTR_ERR(ce) == -
EAGAIN)
1230 "inode %lu: block %lu read error",
1234 ea_idebug(inode,
"block %lu refcount %d>=%d",
1238 }
else if (ext3_xattr_cmp(header,
BHDR(bh)) == 0) {
1248 #define NAME_HASH_SHIFT 5
1249 #define VALUE_HASH_SHIFT 16
1260 char *name = entry->
e_name;
1282 #undef NAME_HASH_SHIFT
1283 #undef VALUE_HASH_SHIFT
1285 #define BLOCK_HASH_SHIFT 16
1298 ext3_xattr_hash_entry(header, entry);
1299 here =
ENTRY(header+1);
1314 #undef BLOCK_HASH_SHIFT
1320 if (!ext3_xattr_cache)
1328 if (ext3_xattr_cache)
1330 ext3_xattr_cache =
NULL;