55 #include <linux/slab.h>
64 #define BHDR(bh) ((struct ext4_xattr_header *)((bh)->b_data))
65 #define ENTRY(ptr) ((struct ext4_xattr_entry *)(ptr))
66 #define BFIRST(bh) ENTRY(BHDR(bh)+1)
67 #define IS_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
69 #ifdef EXT4_XATTR_DEBUG
70 # define ea_idebug(inode, f...) do { \
71 printk(KERN_DEBUG "inode %s:%lu: ", \
72 inode->i_sb->s_id, inode->i_ino); \
76 # define ea_bdebug(bh, f...) do { \
77 char b[BDEVNAME_SIZE]; \
78 printk(KERN_DEBUG "block %s:%lu: ", \
79 bdevname(bh->b_bdev, b), \
80 (unsigned long) bh->b_blocknr); \
85 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
86 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
89 static void ext4_xattr_cache_insert(
struct buffer_head *);
90 static struct buffer_head *ext4_xattr_cache_find(
struct inode *,
98 static struct mb_cache *ext4_xattr_cache;
100 static const struct xattr_handler *ext4_xattr_handler_map[] = {
102 #ifdef CONFIG_EXT4_FS_POSIX_ACL
107 #ifdef CONFIG_EXT4_FS_SECURITY
115 #ifdef CONFIG_EXT4_FS_POSIX_ACL
119 #ifdef CONFIG_EXT4_FS_SECURITY
135 csum = ext4_chksum(sbi, sbi->s_csum_seed, (
__u8 *)&block_nr,
137 csum = ext4_chksum(sbi, csum, (
__u8 *)hdr,
144 static int ext4_xattr_block_csum_verify(
struct inode *inode,
150 (hdr->
h_checksum != ext4_xattr_block_csum(inode, block_nr, hdr)))
155 static void ext4_xattr_block_csum_set(
struct inode *inode,
163 hdr->
h_checksum = ext4_xattr_block_csum(inode, block_nr, hdr);
166 static inline int ext4_handle_dirty_xattr_block(handle_t *
handle,
168 struct buffer_head *bh)
170 ext4_xattr_block_csum_set(inode, bh->b_blocknr,
BHDR(bh));
175 ext4_xattr_handler(
int name_index)
179 if (name_index > 0 && name_index <
ARRAY_SIZE(ext4_xattr_handler_map))
180 handler = ext4_xattr_handler_map[name_index];
192 return ext4_xattr_list(dentry, buffer, size);
200 if ((
void *)next >= end)
208 ext4_xattr_check_block(
struct inode *inode,
struct buffer_head *bh)
212 if (buffer_verified(bh))
218 if (!ext4_xattr_block_csum_verify(inode, bh->b_blocknr,
BHDR(bh)))
220 error = ext4_xattr_check_names(
BFIRST(bh), bh->b_data + bh->b_size);
222 set_buffer_verified(bh);
239 const char *
name,
size_t size,
int sorted)
255 if (cmp <= 0 && (sorted || cmp == 0))
259 if (!cmp && ext4_xattr_check_entry(entry, size))
265 ext4_xattr_block_get(
struct inode *inode,
int name_index,
const char *name,
266 void *
buffer,
size_t buffer_size)
268 struct buffer_head *bh =
NULL;
273 ea_idebug(inode,
"name=%d.%s, buffer=%p, buffer_size=%ld",
274 name_index, name, buffer, (
long)buffer_size);
277 if (!
EXT4_I(inode)->i_file_acl)
280 (
unsigned long long)
EXT4_I(inode)->i_file_acl);
281 bh = sb_bread(inode->
i_sb,
EXT4_I(inode)->i_file_acl);
286 if (ext4_xattr_check_block(inode, bh)) {
289 EXT4_I(inode)->i_file_acl);
293 ext4_xattr_cache_insert(bh);
295 error = ext4_xattr_find_entry(&entry, name_index, name, bh->b_size, 1);
303 if (size > buffer_size)
316 ext4_xattr_ibody_get(
struct inode *inode,
int name_index,
const char *name,
317 void *buffer,
size_t buffer_size)
322 struct ext4_iloc iloc;
327 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
332 raw_inode = ext4_raw_inode(&iloc);
333 header =
IHDR(inode, raw_inode);
335 end = (
void *)raw_inode +
EXT4_SB(inode->
i_sb)->s_inode_size;
336 error = ext4_xattr_check_names(entry, end);
339 error = ext4_xattr_find_entry(&entry, name_index, name,
340 end - (
void *)entry, 0);
346 if (size > buffer_size)
370 void *buffer,
size_t buffer_size)
375 error = ext4_xattr_ibody_get(inode, name_index, name, buffer,
378 error = ext4_xattr_block_get(inode, name_index, name, buffer,
386 char *buffer,
size_t buffer_size)
388 size_t rest = buffer_size;
395 size_t size = handler->
list(dentry, buffer, rest,
407 return buffer_size -
rest;
411 ext4_xattr_block_list(
struct dentry *dentry,
char *buffer,
size_t buffer_size)
413 struct inode *inode = dentry->
d_inode;
414 struct buffer_head *bh =
NULL;
417 ea_idebug(inode,
"buffer=%p, buffer_size=%ld",
418 buffer, (
long)buffer_size);
421 if (!
EXT4_I(inode)->i_file_acl)
424 (
unsigned long long)
EXT4_I(inode)->i_file_acl);
425 bh = sb_bread(inode->
i_sb,
EXT4_I(inode)->i_file_acl);
431 if (ext4_xattr_check_block(inode, bh)) {
433 EXT4_I(inode)->i_file_acl);
437 ext4_xattr_cache_insert(bh);
438 error = ext4_xattr_list_entries(dentry,
BFIRST(bh), buffer, buffer_size);
447 ext4_xattr_ibody_list(
struct dentry *dentry,
char *buffer,
size_t buffer_size)
449 struct inode *inode = dentry->
d_inode;
452 struct ext4_iloc iloc;
456 if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR))
461 raw_inode = ext4_raw_inode(&iloc);
462 header =
IHDR(inode, raw_inode);
463 end = (
void *)raw_inode +
EXT4_SB(inode->
i_sb)->s_inode_size;
464 error = ext4_xattr_check_names(
IFIRST(header), end);
467 error = ext4_xattr_list_entries(dentry,
IFIRST(header),
468 buffer, buffer_size);
486 ext4_xattr_list(
struct dentry *dentry,
char *buffer,
size_t buffer_size)
491 ret = ret2 = ext4_xattr_ibody_list(dentry, buffer, buffer_size);
498 ret = ext4_xattr_block_list(dentry, buffer, buffer_size);
511 static void ext4_xattr_update_super_block(handle_t *handle,
528 ext4_xattr_release_block(handle_t *handle,
struct inode *inode,
529 struct buffer_head *bh)
541 ea_bdebug(bh,
"refcount now=0; freeing");
550 le32_add_cpu(&
BHDR(bh)->h_refcount, -1);
554 error = ext4_handle_dirty_xattr_block(handle, inode, bh);
556 ext4_handle_sync(handle);
557 dquot_free_block(inode, 1);
558 ea_bdebug(bh,
"refcount now=%d; releasing",
562 ext4_std_error(inode->
i_sb, error);
571 size_t *min_offs,
void *base,
int *total)
577 if (offs < *min_offs)
581 return (*min_offs - ((
void *)last - base) -
sizeof(
__u32));
614 free = min_offs - ((
void *)last - s->
base) -
sizeof(
__u32);
616 if (!
s->here->e_value_block &&
s->here->e_value_size) {
629 if (
i->value &&
s->not_found) {
632 size_t rest = (
void *)last - (
void *)
s->here +
sizeof(
__u32);
633 memmove((
void *)
s->here + size,
s->here, rest);
635 s->here->e_name_index =
i->name_index;
637 memcpy(
s->here->e_name,
i->name, name_len);
639 if (!
s->here->e_value_block &&
s->here->e_value_size) {
640 void *first_val =
s->base + min_offs;
649 s->here->e_value_size =
653 memcpy(val,
i->value,
i->value_len);
658 memmove(first_val + size, first_val, val - first_val);
659 memset(first_val, 0, size);
660 s->here->e_value_size = 0;
661 s->here->e_value_offs = 0;
678 last =
ENTRY((
void *)last - size);
680 (
void *)last - (
void *)
s->here +
sizeof(
__u32));
690 void *val =
s->base + min_offs -
size;
694 memcpy(val,
i->value,
i->value_len);
702 struct buffer_head *
bh;
712 ea_idebug(inode,
"name=%d.%s, value=%p, value_len=%ld",
715 if (
EXT4_I(inode)->i_file_acl) {
717 bs->
bh = sb_bread(sb,
EXT4_I(inode)->i_file_acl);
724 if (ext4_xattr_check_block(inode, bs->
bh)) {
726 EXT4_I(inode)->i_file_acl);
733 bs->
s.end = bs->
bh->b_data + bs->
bh->b_size;
734 bs->
s.here = bs->
s.first;
735 error = ext4_xattr_find_entry(&bs->
s.here, i->
name_index,
736 i->
name, bs->
bh->b_size, 1);
737 if (error && error != -
ENODATA)
748 ext4_xattr_block_set(handle_t *handle,
struct inode *inode,
753 struct buffer_head *new_bh =
NULL;
758 #define header(x) ((struct ext4_xattr_header *)(x))
776 error = ext4_xattr_set_entry(i, s);
779 ext4_xattr_rehash(header(s->
base),
781 ext4_xattr_cache_insert(bs->
bh);
787 error = ext4_handle_dirty_xattr_block(handle,
797 ext4_handle_release_buffer(handle, bs->
bh);
828 error = ext4_xattr_set_entry(i, s);
834 ext4_xattr_rehash(header(s->
base), s->
here);
838 new_bh = ext4_xattr_cache_find(inode, header(s->
base), &ce);
841 if (new_bh == bs->
bh)
846 error = dquot_alloc_block(inode, 1);
854 le32_add_cpu(&
BHDR(new_bh)->h_refcount, 1);
855 ea_bdebug(new_bh,
"reusing; refcount now=%d",
858 error = ext4_handle_dirty_xattr_block(handle,
866 }
else if (bs->
bh && s->
base == bs->
bh->b_data) {
875 goal = ext4_group_first_block_no(sb,
876 EXT4_I(inode)->i_block_group);
894 BUG_ON(block > EXT4_MAX_BLOCK_FILE_PHYS);
897 (
unsigned long long)block);
899 new_bh = sb_getblk(sb, block);
913 memcpy(new_bh->b_data, s->
base, new_bh->b_size);
914 set_buffer_uptodate(new_bh);
916 ext4_xattr_cache_insert(new_bh);
917 error = ext4_handle_dirty_xattr_block(handle,
925 EXT4_I(inode)->i_file_acl = new_bh ? new_bh->b_blocknr : 0;
928 if (bs->
bh && bs->
bh != new_bh)
929 ext4_xattr_release_block(handle, inode, bs->
bh);
936 if (!(bs->
bh && s->
base == bs->
bh->b_data))
942 dquot_free_block(inode, 1);
947 EXT4_I(inode)->i_file_acl);
968 raw_inode = ext4_raw_inode(&is->
iloc);
969 header =
IHDR(inode, raw_inode);
970 is->
s.base = is->
s.first =
IFIRST(header);
971 is->
s.here = is->
s.first;
972 is->
s.end = (
void *)raw_inode +
EXT4_SB(inode->
i_sb)->s_inode_size;
973 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
974 error = ext4_xattr_check_names(
IFIRST(header), is->
s.end);
978 error = ext4_xattr_find_entry(&is->
s.here, i->
name_index,
980 (
void *)is->
s.base, 0);
981 if (error && error != -
ENODATA)
989 ext4_xattr_ibody_set(handle_t *handle,
struct inode *inode,
997 if (
EXT4_I(inode)->i_extra_isize == 0)
999 error = ext4_xattr_set_entry(i, s);
1002 header =
IHDR(inode, ext4_raw_inode(&is->
iloc));
1005 ext4_set_inode_state(inode, EXT4_STATE_XATTR);
1008 ext4_clear_inode_state(inode, EXT4_STATE_XATTR);
1043 unsigned long no_expand;
1051 no_expand = ext4_test_inode_state(inode, EXT4_STATE_NO_EXPAND);
1052 ext4_set_inode_state(inode, EXT4_STATE_NO_EXPAND);
1058 if (ext4_test_inode_state(inode, EXT4_STATE_NEW)) {
1061 ext4_clear_inode_state(inode, EXT4_STATE_NEW);
1071 if (is.
s.not_found && bs.
s.not_found) {
1084 if (!is.
s.not_found)
1085 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1086 else if (!bs.
s.not_found)
1087 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1089 error = ext4_xattr_ibody_set(handle, inode, &i, &is);
1090 if (!error && !bs.
s.not_found) {
1092 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1093 }
else if (error == -
ENOSPC) {
1094 if (
EXT4_I(inode)->i_file_acl && !bs.
s.base) {
1099 error = ext4_xattr_block_set(handle, inode, &i, &bs);
1102 if (!is.
s.not_found) {
1104 error = ext4_xattr_ibody_set(handle, inode, &i,
1110 ext4_xattr_update_super_block(handle, inode->
i_sb);
1111 inode->
i_ctime = ext4_current_time(inode);
1113 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1121 ext4_handle_sync(handle);
1128 ext4_clear_inode_state(inode, EXT4_STATE_NO_EXPAND);
1150 if (IS_ERR(handle)) {
1151 error = PTR_ERR(handle);
1156 value, value_len, flags);
1173 int value_offs_shift,
void *to,
1174 void *
from,
size_t n,
int blocksize)
1198 struct ext4_inode *raw_inode, handle_t *handle)
1202 struct buffer_head *bh =
NULL;
1205 char *buffer =
NULL, *b_entry_name =
NULL;
1206 size_t min_offs,
free;
1207 int total_ino, total_blk;
1209 int extra_isize = 0, error = 0, tried_min_extra_isize = 0;
1214 if (
EXT4_I(inode)->i_extra_isize >= new_extra_isize) {
1219 header =
IHDR(inode, raw_inode);
1227 base = start =
entry;
1228 end = (
void *)raw_inode +
EXT4_SB(inode->
i_sb)->s_inode_size;
1229 min_offs = end - base;
1233 free = ext4_xattr_free_space(last, &min_offs, base, &total_ino);
1234 if (free >= new_extra_isize) {
1236 ext4_xattr_shift_entries(entry,
EXT4_I(inode)->i_extra_isize
1237 - new_extra_isize, (
void *)raw_inode +
1239 (
void *)header, total_ino,
1240 inode->
i_sb->s_blocksize);
1241 EXT4_I(inode)->i_extra_isize = new_extra_isize;
1250 if (
EXT4_I(inode)->i_file_acl) {
1251 bh = sb_bread(inode->
i_sb,
EXT4_I(inode)->i_file_acl);
1255 if (ext4_xattr_check_block(inode, bh)) {
1257 EXT4_I(inode)->i_file_acl);
1263 end = bh->b_data + bh->b_size;
1264 min_offs = end - base;
1265 free = ext4_xattr_free_space(first, &min_offs, base,
1267 if (free < new_extra_isize) {
1268 if (!tried_min_extra_isize && s_min_extra_isize) {
1269 tried_min_extra_isize++;
1270 new_extra_isize = s_min_extra_isize;
1278 free = inode->
i_sb->s_blocksize;
1281 while (new_extra_isize > 0) {
1289 unsigned int shift_bytes;
1290 unsigned int min_total_size = ~0
U;
1311 if (total_size <= free && total_size < min_total_size) {
1312 if (total_size < new_extra_isize) {
1321 if (entry ==
NULL) {
1323 entry = small_entry;
1325 if (!tried_min_extra_isize &&
1326 s_min_extra_isize) {
1327 tried_min_extra_isize++;
1328 new_extra_isize = s_min_extra_isize;
1341 if (!buffer || !b_entry_name) {
1350 i.
name = b_entry_name;
1361 error = ext4_xattr_ibody_set(handle, inode, &i, is);
1367 shift_bytes = new_extra_isize;
1369 shift_bytes = entry_size +
size;
1371 ext4_xattr_shift_entries(entry,
EXT4_I(inode)->i_extra_isize -
1372 shift_bytes, (
void *)raw_inode +
1374 (
void *)header, total_ino - entry_size,
1375 inode->
i_sb->s_blocksize);
1377 extra_isize += shift_bytes;
1378 new_extra_isize -= shift_bytes;
1379 EXT4_I(inode)->i_extra_isize = extra_isize;
1381 i.
name = b_entry_name;
1389 error = ext4_xattr_block_set(handle, inode, &i, bs);
1392 kfree(b_entry_name);
1394 b_entry_name =
NULL;
1396 brelse(is->
iloc.bh);
1405 kfree(b_entry_name);
1408 brelse(is->
iloc.bh);
1428 struct buffer_head *bh =
NULL;
1430 if (!
EXT4_I(inode)->i_file_acl)
1432 bh = sb_bread(inode->
i_sb,
EXT4_I(inode)->i_file_acl);
1435 EXT4_I(inode)->i_file_acl);
1441 EXT4_I(inode)->i_file_acl);
1444 ext4_xattr_release_block(handle, inode, bh);
1445 EXT4_I(inode)->i_file_acl = 0;
1471 ext4_xattr_cache_insert(
struct buffer_head *bh)
1485 if (error == -
EBUSY) {
1490 ea_bdebug(bh,
"inserting [%x]", (
int)hash);
1509 entry1 =
ENTRY(header1+1);
1510 entry2 =
ENTRY(header2+1);
1543 static struct buffer_head *
1552 ea_idebug(inode,
"looking for cached blocks [%x]", (
int)hash);
1557 struct buffer_head *bh;
1560 if (PTR_ERR(ce) == -
EAGAIN)
1570 ea_idebug(inode,
"block %lu refcount %d>=%d",
1574 }
else if (ext4_xattr_cmp(header,
BHDR(bh)) == 0) {
1584 #define NAME_HASH_SHIFT 5
1585 #define VALUE_HASH_SHIFT 16
1596 char *name = entry->
e_name;
1618 #undef NAME_HASH_SHIFT
1619 #undef VALUE_HASH_SHIFT
1621 #define BLOCK_HASH_SHIFT 16
1634 ext4_xattr_hash_entry(header, entry);
1635 here =
ENTRY(header+1);
1650 #undef BLOCK_HASH_SHIFT
1656 if (!ext4_xattr_cache)
1664 if (ext4_xattr_cache)
1666 ext4_xattr_cache =
NULL;