2 * linux/fs/ext4/xattr.c
4 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
6 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
7 * Ext4 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 * EXT4_I(inode)->i_file_acl is protected by EXT4_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/mbcache.h>
57 #include <linux/quotaops.h>
58 #include "ext4_jbd2.h"
63 #ifdef EXT4_XATTR_DEBUG
64 # define ea_idebug(inode, fmt, ...) \
65 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
66 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
67 # define ea_bdebug(bh, fmt, ...) \
68 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
69 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
71 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
72 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
75 static void ext4_xattr_cache_insert(struct mb_cache
*, struct buffer_head
*);
76 static struct buffer_head
*ext4_xattr_cache_find(struct inode
*,
77 struct ext4_xattr_header
*,
78 struct mb_cache_entry
**);
79 static void ext4_xattr_rehash(struct ext4_xattr_header
*,
80 struct ext4_xattr_entry
*);
81 static int ext4_xattr_list(struct dentry
*dentry
, char *buffer
,
84 static const struct xattr_handler
*ext4_xattr_handler_map
[] = {
85 [EXT4_XATTR_INDEX_USER
] = &ext4_xattr_user_handler
,
86 #ifdef CONFIG_EXT4_FS_POSIX_ACL
87 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
] = &posix_acl_access_xattr_handler
,
88 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &posix_acl_default_xattr_handler
,
90 [EXT4_XATTR_INDEX_TRUSTED
] = &ext4_xattr_trusted_handler
,
91 #ifdef CONFIG_EXT4_FS_SECURITY
92 [EXT4_XATTR_INDEX_SECURITY
] = &ext4_xattr_security_handler
,
96 const struct xattr_handler
*ext4_xattr_handlers
[] = {
97 &ext4_xattr_user_handler
,
98 &ext4_xattr_trusted_handler
,
99 #ifdef CONFIG_EXT4_FS_POSIX_ACL
100 &posix_acl_access_xattr_handler
,
101 &posix_acl_default_xattr_handler
,
103 #ifdef CONFIG_EXT4_FS_SECURITY
104 &ext4_xattr_security_handler
,
109 #define EXT4_GET_MB_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_mb_cache)
112 static __le32
ext4_xattr_block_csum(struct inode
*inode
,
114 struct ext4_xattr_header
*hdr
)
116 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
118 __le64 dsk_block_nr
= cpu_to_le64(block_nr
);
119 __u32 dummy_csum
= 0;
120 int offset
= offsetof(struct ext4_xattr_header
, h_checksum
);
122 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&dsk_block_nr
,
123 sizeof(dsk_block_nr
));
124 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
, offset
);
125 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, sizeof(dummy_csum
));
126 offset
+= sizeof(dummy_csum
);
127 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
+ offset
,
128 EXT4_BLOCK_SIZE(inode
->i_sb
) - offset
);
130 return cpu_to_le32(csum
);
133 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
135 struct ext4_xattr_header
*hdr
)
137 if (ext4_has_metadata_csum(inode
->i_sb
) &&
138 (hdr
->h_checksum
!= ext4_xattr_block_csum(inode
, block_nr
, hdr
)))
143 static void ext4_xattr_block_csum_set(struct inode
*inode
,
145 struct ext4_xattr_header
*hdr
)
147 if (!ext4_has_metadata_csum(inode
->i_sb
))
150 hdr
->h_checksum
= ext4_xattr_block_csum(inode
, block_nr
, hdr
);
153 static inline int ext4_handle_dirty_xattr_block(handle_t
*handle
,
155 struct buffer_head
*bh
)
157 ext4_xattr_block_csum_set(inode
, bh
->b_blocknr
, BHDR(bh
));
158 return ext4_handle_dirty_metadata(handle
, inode
, bh
);
161 static inline const struct xattr_handler
*
162 ext4_xattr_handler(int name_index
)
164 const struct xattr_handler
*handler
= NULL
;
166 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
167 handler
= ext4_xattr_handler_map
[name_index
];
172 * Inode operation listxattr()
174 * d_inode(dentry)->i_mutex: don't care
177 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t size
)
179 return ext4_xattr_list(dentry
, buffer
, size
);
183 ext4_xattr_check_names(struct ext4_xattr_entry
*entry
, void *end
,
186 struct ext4_xattr_entry
*e
= entry
;
188 while (!IS_LAST_ENTRY(e
)) {
189 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(e
);
190 if ((void *)next
>= end
)
191 return -EFSCORRUPTED
;
195 while (!IS_LAST_ENTRY(entry
)) {
196 if (entry
->e_value_block
!= 0)
197 return -EFSCORRUPTED
;
198 if (entry
->e_value_size
!= 0 &&
199 (value_start
+ le16_to_cpu(entry
->e_value_offs
) <
200 (void *)e
+ sizeof(__u32
) ||
201 value_start
+ le16_to_cpu(entry
->e_value_offs
) +
202 le32_to_cpu(entry
->e_value_size
) > end
))
203 return -EFSCORRUPTED
;
204 entry
= EXT4_XATTR_NEXT(entry
);
211 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
215 if (buffer_verified(bh
))
218 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
219 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
220 return -EFSCORRUPTED
;
221 if (!ext4_xattr_block_csum_verify(inode
, bh
->b_blocknr
, BHDR(bh
)))
223 error
= ext4_xattr_check_names(BFIRST(bh
), bh
->b_data
+ bh
->b_size
,
226 set_buffer_verified(bh
);
231 __xattr_check_inode(struct inode
*inode
, struct ext4_xattr_ibody_header
*header
,
232 void *end
, const char *function
, unsigned int line
)
234 struct ext4_xattr_entry
*entry
= IFIRST(header
);
235 int error
= -EFSCORRUPTED
;
237 if (((void *) header
>= end
) ||
238 (header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)))
240 error
= ext4_xattr_check_names(entry
, end
, entry
);
243 __ext4_error_inode(inode
, function
, line
, 0,
244 "corrupted in-inode xattr");
248 #define xattr_check_inode(inode, header, end) \
249 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
252 ext4_xattr_check_entry(struct ext4_xattr_entry
*entry
, size_t size
)
254 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
256 if (entry
->e_value_block
!= 0 || value_size
> size
||
257 le16_to_cpu(entry
->e_value_offs
) + value_size
> size
)
258 return -EFSCORRUPTED
;
263 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
264 const char *name
, size_t size
, int sorted
)
266 struct ext4_xattr_entry
*entry
;
272 name_len
= strlen(name
);
274 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
275 cmp
= name_index
- entry
->e_name_index
;
277 cmp
= name_len
- entry
->e_name_len
;
279 cmp
= memcmp(name
, entry
->e_name
, name_len
);
280 if (cmp
<= 0 && (sorted
|| cmp
== 0))
284 if (!cmp
&& ext4_xattr_check_entry(entry
, size
))
285 return -EFSCORRUPTED
;
286 return cmp
? -ENODATA
: 0;
290 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
291 void *buffer
, size_t buffer_size
)
293 struct buffer_head
*bh
= NULL
;
294 struct ext4_xattr_entry
*entry
;
297 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
299 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
300 name_index
, name
, buffer
, (long)buffer_size
);
303 if (!EXT4_I(inode
)->i_file_acl
)
305 ea_idebug(inode
, "reading block %llu",
306 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
307 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
310 ea_bdebug(bh
, "b_count=%d, refcount=%d",
311 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
312 if (ext4_xattr_check_block(inode
, bh
)) {
314 EXT4_ERROR_INODE(inode
, "bad block %llu",
315 EXT4_I(inode
)->i_file_acl
);
316 error
= -EFSCORRUPTED
;
319 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
321 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, bh
->b_size
, 1);
322 if (error
== -EFSCORRUPTED
)
326 size
= le32_to_cpu(entry
->e_value_size
);
329 if (size
> buffer_size
)
331 memcpy(buffer
, bh
->b_data
+ le16_to_cpu(entry
->e_value_offs
),
342 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
343 void *buffer
, size_t buffer_size
)
345 struct ext4_xattr_ibody_header
*header
;
346 struct ext4_xattr_entry
*entry
;
347 struct ext4_inode
*raw_inode
;
348 struct ext4_iloc iloc
;
353 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
355 error
= ext4_get_inode_loc(inode
, &iloc
);
358 raw_inode
= ext4_raw_inode(&iloc
);
359 header
= IHDR(inode
, raw_inode
);
360 entry
= IFIRST(header
);
361 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
362 error
= xattr_check_inode(inode
, header
, end
);
365 error
= ext4_xattr_find_entry(&entry
, name_index
, name
,
366 end
- (void *)entry
, 0);
369 size
= le32_to_cpu(entry
->e_value_size
);
372 if (size
> buffer_size
)
374 memcpy(buffer
, (void *)IFIRST(header
) +
375 le16_to_cpu(entry
->e_value_offs
), size
);
387 * Copy an extended attribute into the buffer
388 * provided, or compute the buffer size required.
389 * Buffer is NULL to compute the size of the buffer required.
391 * Returns a negative error number on failure, or the number of bytes
392 * used / required on success.
395 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
396 void *buffer
, size_t buffer_size
)
400 if (strlen(name
) > 255)
403 down_read(&EXT4_I(inode
)->xattr_sem
);
404 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
406 if (error
== -ENODATA
)
407 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
409 up_read(&EXT4_I(inode
)->xattr_sem
);
414 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
415 char *buffer
, size_t buffer_size
)
417 size_t rest
= buffer_size
;
419 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
420 const struct xattr_handler
*handler
=
421 ext4_xattr_handler(entry
->e_name_index
);
423 if (handler
&& (!handler
->list
|| handler
->list(dentry
))) {
424 const char *prefix
= handler
->prefix
?: handler
->name
;
425 size_t prefix_len
= strlen(prefix
);
426 size_t size
= prefix_len
+ entry
->e_name_len
+ 1;
431 memcpy(buffer
, prefix
, prefix_len
);
432 buffer
+= prefix_len
;
433 memcpy(buffer
, entry
->e_name
, entry
->e_name_len
);
434 buffer
+= entry
->e_name_len
;
440 return buffer_size
- rest
; /* total size */
444 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
446 struct inode
*inode
= d_inode(dentry
);
447 struct buffer_head
*bh
= NULL
;
449 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
451 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
452 buffer
, (long)buffer_size
);
455 if (!EXT4_I(inode
)->i_file_acl
)
457 ea_idebug(inode
, "reading block %llu",
458 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
459 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
463 ea_bdebug(bh
, "b_count=%d, refcount=%d",
464 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
465 if (ext4_xattr_check_block(inode
, bh
)) {
466 EXT4_ERROR_INODE(inode
, "bad block %llu",
467 EXT4_I(inode
)->i_file_acl
);
468 error
= -EFSCORRUPTED
;
471 ext4_xattr_cache_insert(ext4_mb_cache
, bh
);
472 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
481 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
483 struct inode
*inode
= d_inode(dentry
);
484 struct ext4_xattr_ibody_header
*header
;
485 struct ext4_inode
*raw_inode
;
486 struct ext4_iloc iloc
;
490 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
492 error
= ext4_get_inode_loc(inode
, &iloc
);
495 raw_inode
= ext4_raw_inode(&iloc
);
496 header
= IHDR(inode
, raw_inode
);
497 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
498 error
= xattr_check_inode(inode
, header
, end
);
501 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
502 buffer
, buffer_size
);
512 * Copy a list of attribute names into the buffer
513 * provided, or compute the buffer size required.
514 * Buffer is NULL to compute the size of the buffer required.
516 * Returns a negative error number on failure, or the number of bytes
517 * used / required on success.
520 ext4_xattr_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
524 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
525 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
532 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
537 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
542 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
545 static void ext4_xattr_update_super_block(handle_t
*handle
,
546 struct super_block
*sb
)
548 if (ext4_has_feature_xattr(sb
))
551 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
552 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
553 ext4_set_feature_xattr(sb
);
554 ext4_handle_dirty_super(handle
, sb
);
559 * Release the xattr block BH: If the reference count is > 1, decrement it;
560 * otherwise free the block.
563 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
564 struct buffer_head
*bh
)
566 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
570 BUFFER_TRACE(bh
, "get_write_access");
571 error
= ext4_journal_get_write_access(handle
, bh
);
576 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
577 ref
= le32_to_cpu(BHDR(bh
)->h_refcount
);
579 ea_bdebug(bh
, "refcount now=0; freeing");
581 * This must happen under buffer lock for
582 * ext4_xattr_block_set() to reliably detect freed block
584 mb_cache_entry_delete_block(ext4_mb_cache
, hash
, bh
->b_blocknr
);
587 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
588 EXT4_FREE_BLOCKS_METADATA
|
589 EXT4_FREE_BLOCKS_FORGET
);
592 BHDR(bh
)->h_refcount
= cpu_to_le32(ref
);
593 if (ref
== EXT4_XATTR_REFCOUNT_MAX
- 1) {
594 struct mb_cache_entry
*ce
;
596 ce
= mb_cache_entry_get(ext4_mb_cache
, hash
,
600 mb_cache_entry_put(ext4_mb_cache
, ce
);
605 * Beware of this ugliness: Releasing of xattr block references
606 * from different inodes can race and so we have to protect
607 * from a race where someone else frees the block (and releases
608 * its journal_head) before we are done dirtying the buffer. In
609 * nojournal mode this race is harmless and we actually cannot
610 * call ext4_handle_dirty_xattr_block() with locked buffer as
611 * that function can call sync_dirty_buffer() so for that case
612 * we handle the dirtying after unlocking the buffer.
614 if (ext4_handle_valid(handle
))
615 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
618 if (!ext4_handle_valid(handle
))
619 error
= ext4_handle_dirty_xattr_block(handle
, inode
,
622 ext4_handle_sync(handle
);
623 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
624 ea_bdebug(bh
, "refcount now=%d; releasing",
625 le32_to_cpu(BHDR(bh
)->h_refcount
));
628 ext4_std_error(inode
->i_sb
, error
);
633 * Find the available free space for EAs. This also returns the total number of
634 * bytes used by EA entries.
636 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
637 size_t *min_offs
, void *base
, int *total
)
639 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
640 if (last
->e_value_size
) {
641 size_t offs
= le16_to_cpu(last
->e_value_offs
);
642 if (offs
< *min_offs
)
646 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
648 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
652 ext4_xattr_set_entry(struct ext4_xattr_info
*i
, struct ext4_xattr_search
*s
)
654 struct ext4_xattr_entry
*last
;
655 size_t free
, min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
657 /* Compute min_offs and last. */
659 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
660 if (last
->e_value_size
) {
661 size_t offs
= le16_to_cpu(last
->e_value_offs
);
666 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
668 if (s
->here
->e_value_size
) {
669 size_t size
= le32_to_cpu(s
->here
->e_value_size
);
670 free
+= EXT4_XATTR_SIZE(size
);
672 free
+= EXT4_XATTR_LEN(name_len
);
675 if (free
< EXT4_XATTR_LEN(name_len
) +
676 EXT4_XATTR_SIZE(i
->value_len
))
680 if (i
->value
&& s
->not_found
) {
681 /* Insert the new name. */
682 size_t size
= EXT4_XATTR_LEN(name_len
);
683 size_t rest
= (void *)last
- (void *)s
->here
+ sizeof(__u32
);
684 memmove((void *)s
->here
+ size
, s
->here
, rest
);
685 memset(s
->here
, 0, size
);
686 s
->here
->e_name_index
= i
->name_index
;
687 s
->here
->e_name_len
= name_len
;
688 memcpy(s
->here
->e_name
, i
->name
, name_len
);
690 if (s
->here
->e_value_size
) {
691 void *first_val
= s
->base
+ min_offs
;
692 size_t offs
= le16_to_cpu(s
->here
->e_value_offs
);
693 void *val
= s
->base
+ offs
;
694 size_t size
= EXT4_XATTR_SIZE(
695 le32_to_cpu(s
->here
->e_value_size
));
697 if (i
->value
&& size
== EXT4_XATTR_SIZE(i
->value_len
)) {
698 /* The old and the new value have the same
699 size. Just replace. */
700 s
->here
->e_value_size
=
701 cpu_to_le32(i
->value_len
);
702 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
703 memset(val
, 0, size
);
705 /* Clear pad bytes first. */
706 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
708 memcpy(val
, i
->value
, i
->value_len
);
713 /* Remove the old value. */
714 memmove(first_val
+ size
, first_val
, val
- first_val
);
715 memset(first_val
, 0, size
);
716 s
->here
->e_value_size
= 0;
717 s
->here
->e_value_offs
= 0;
720 /* Adjust all value offsets. */
722 while (!IS_LAST_ENTRY(last
)) {
723 size_t o
= le16_to_cpu(last
->e_value_offs
);
724 if (last
->e_value_size
&& o
< offs
)
726 cpu_to_le16(o
+ size
);
727 last
= EXT4_XATTR_NEXT(last
);
731 /* Remove the old name. */
732 size_t size
= EXT4_XATTR_LEN(name_len
);
733 last
= ENTRY((void *)last
- size
);
734 memmove(s
->here
, (void *)s
->here
+ size
,
735 (void *)last
- (void *)s
->here
+ sizeof(__u32
));
736 memset(last
, 0, size
);
741 /* Insert the new value. */
742 s
->here
->e_value_size
= cpu_to_le32(i
->value_len
);
744 size_t size
= EXT4_XATTR_SIZE(i
->value_len
);
745 void *val
= s
->base
+ min_offs
- size
;
746 s
->here
->e_value_offs
= cpu_to_le16(min_offs
- size
);
747 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
748 memset(val
, 0, size
);
750 /* Clear the pad bytes first. */
751 memset(val
+ size
- EXT4_XATTR_PAD
, 0,
753 memcpy(val
, i
->value
, i
->value_len
);
760 struct ext4_xattr_block_find
{
761 struct ext4_xattr_search s
;
762 struct buffer_head
*bh
;
766 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
767 struct ext4_xattr_block_find
*bs
)
769 struct super_block
*sb
= inode
->i_sb
;
772 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
773 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
775 if (EXT4_I(inode
)->i_file_acl
) {
776 /* The inode already has an extended attribute block. */
777 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
781 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
782 atomic_read(&(bs
->bh
->b_count
)),
783 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
784 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
785 EXT4_ERROR_INODE(inode
, "bad block %llu",
786 EXT4_I(inode
)->i_file_acl
);
787 error
= -EFSCORRUPTED
;
790 /* Find the named attribute. */
791 bs
->s
.base
= BHDR(bs
->bh
);
792 bs
->s
.first
= BFIRST(bs
->bh
);
793 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
794 bs
->s
.here
= bs
->s
.first
;
795 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
796 i
->name
, bs
->bh
->b_size
, 1);
797 if (error
&& error
!= -ENODATA
)
799 bs
->s
.not_found
= error
;
808 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
809 struct ext4_xattr_info
*i
,
810 struct ext4_xattr_block_find
*bs
)
812 struct super_block
*sb
= inode
->i_sb
;
813 struct buffer_head
*new_bh
= NULL
;
814 struct ext4_xattr_search
*s
= &bs
->s
;
815 struct mb_cache_entry
*ce
= NULL
;
817 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
819 #define header(x) ((struct ext4_xattr_header *)(x))
821 if (i
->value
&& i
->value_len
> sb
->s_blocksize
)
824 BUFFER_TRACE(bs
->bh
, "get_write_access");
825 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
830 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
831 __u32 hash
= le32_to_cpu(BHDR(bs
->bh
)->h_hash
);
834 * This must happen under buffer lock for
835 * ext4_xattr_block_set() to reliably detect modified
838 mb_cache_entry_delete_block(ext4_mb_cache
, hash
,
840 ea_bdebug(bs
->bh
, "modifying in-place");
841 error
= ext4_xattr_set_entry(i
, s
);
843 if (!IS_LAST_ENTRY(s
->first
))
844 ext4_xattr_rehash(header(s
->base
),
846 ext4_xattr_cache_insert(ext4_mb_cache
,
849 unlock_buffer(bs
->bh
);
850 if (error
== -EFSCORRUPTED
)
853 error
= ext4_handle_dirty_xattr_block(handle
,
860 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
862 unlock_buffer(bs
->bh
);
863 ea_bdebug(bs
->bh
, "cloning");
864 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
868 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
869 s
->first
= ENTRY(header(s
->base
)+1);
870 header(s
->base
)->h_refcount
= cpu_to_le32(1);
871 s
->here
= ENTRY(s
->base
+ offset
);
872 s
->end
= s
->base
+ bs
->bh
->b_size
;
875 /* Allocate a buffer where we construct the new block. */
876 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
877 /* assert(header == s->base) */
881 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
882 header(s
->base
)->h_blocks
= cpu_to_le32(1);
883 header(s
->base
)->h_refcount
= cpu_to_le32(1);
884 s
->first
= ENTRY(header(s
->base
)+1);
885 s
->here
= ENTRY(header(s
->base
)+1);
886 s
->end
= s
->base
+ sb
->s_blocksize
;
889 error
= ext4_xattr_set_entry(i
, s
);
890 if (error
== -EFSCORRUPTED
)
894 if (!IS_LAST_ENTRY(s
->first
))
895 ext4_xattr_rehash(header(s
->base
), s
->here
);
898 if (!IS_LAST_ENTRY(s
->first
)) {
899 new_bh
= ext4_xattr_cache_find(inode
, header(s
->base
), &ce
);
901 /* We found an identical block in the cache. */
902 if (new_bh
== bs
->bh
)
903 ea_bdebug(new_bh
, "keeping");
907 /* The old block is released after updating
909 error
= dquot_alloc_block(inode
,
910 EXT4_C2B(EXT4_SB(sb
), 1));
913 BUFFER_TRACE(new_bh
, "get_write_access");
914 error
= ext4_journal_get_write_access(handle
,
920 * We have to be careful about races with
921 * freeing, rehashing or adding references to
922 * xattr block. Once we hold buffer lock xattr
923 * block's state is stable so we can check
924 * whether the block got freed / rehashed or
925 * not. Since we unhash mbcache entry under
926 * buffer lock when freeing / rehashing xattr
927 * block, checking whether entry is still
928 * hashed is reliable. Same rules hold for
929 * e_reusable handling.
931 if (hlist_bl_unhashed(&ce
->e_hash_list
) ||
934 * Undo everything and check mbcache
937 unlock_buffer(new_bh
);
938 dquot_free_block(inode
,
939 EXT4_C2B(EXT4_SB(sb
),
942 mb_cache_entry_put(ext4_mb_cache
, ce
);
947 ref
= le32_to_cpu(BHDR(new_bh
)->h_refcount
) + 1;
948 BHDR(new_bh
)->h_refcount
= cpu_to_le32(ref
);
949 if (ref
>= EXT4_XATTR_REFCOUNT_MAX
)
951 ea_bdebug(new_bh
, "reusing; refcount now=%d",
953 unlock_buffer(new_bh
);
954 error
= ext4_handle_dirty_xattr_block(handle
,
960 mb_cache_entry_touch(ext4_mb_cache
, ce
);
961 mb_cache_entry_put(ext4_mb_cache
, ce
);
963 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
964 /* We were modifying this block in-place. */
965 ea_bdebug(bs
->bh
, "keeping this block");
969 /* We need to allocate a new block */
970 ext4_fsblk_t goal
, block
;
972 goal
= ext4_group_first_block_no(sb
,
973 EXT4_I(inode
)->i_block_group
);
975 /* non-extent files can't have physical blocks past 2^32 */
976 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
977 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
979 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
984 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
985 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
987 ea_idebug(inode
, "creating block %llu",
988 (unsigned long long)block
);
990 new_bh
= sb_getblk(sb
, block
);
991 if (unlikely(!new_bh
)) {
994 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
995 EXT4_FREE_BLOCKS_METADATA
);
999 error
= ext4_journal_get_create_access(handle
, new_bh
);
1001 unlock_buffer(new_bh
);
1005 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
1006 set_buffer_uptodate(new_bh
);
1007 unlock_buffer(new_bh
);
1008 ext4_xattr_cache_insert(ext4_mb_cache
, new_bh
);
1009 error
= ext4_handle_dirty_xattr_block(handle
,
1016 /* Update the inode. */
1017 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
1019 /* Drop the previous xattr block. */
1020 if (bs
->bh
&& bs
->bh
!= new_bh
)
1021 ext4_xattr_release_block(handle
, inode
, bs
->bh
);
1026 mb_cache_entry_put(ext4_mb_cache
, ce
);
1028 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
1034 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
1038 EXT4_ERROR_INODE(inode
, "bad block %llu",
1039 EXT4_I(inode
)->i_file_acl
);
1045 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
1046 struct ext4_xattr_ibody_find
*is
)
1048 struct ext4_xattr_ibody_header
*header
;
1049 struct ext4_inode
*raw_inode
;
1052 if (EXT4_I(inode
)->i_extra_isize
== 0)
1054 raw_inode
= ext4_raw_inode(&is
->iloc
);
1055 header
= IHDR(inode
, raw_inode
);
1056 is
->s
.base
= is
->s
.first
= IFIRST(header
);
1057 is
->s
.here
= is
->s
.first
;
1058 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1059 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
1060 error
= xattr_check_inode(inode
, header
, is
->s
.end
);
1063 /* Find the named attribute. */
1064 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
1065 i
->name
, is
->s
.end
-
1066 (void *)is
->s
.base
, 0);
1067 if (error
&& error
!= -ENODATA
)
1069 is
->s
.not_found
= error
;
1074 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
1075 struct ext4_xattr_info
*i
,
1076 struct ext4_xattr_ibody_find
*is
)
1078 struct ext4_xattr_ibody_header
*header
;
1079 struct ext4_xattr_search
*s
= &is
->s
;
1082 if (EXT4_I(inode
)->i_extra_isize
== 0)
1084 error
= ext4_xattr_set_entry(i
, s
);
1086 if (error
== -ENOSPC
&&
1087 ext4_has_inline_data(inode
)) {
1088 error
= ext4_try_to_evict_inline_data(handle
, inode
,
1089 EXT4_XATTR_LEN(strlen(i
->name
) +
1090 EXT4_XATTR_SIZE(i
->value_len
)));
1093 error
= ext4_xattr_ibody_find(inode
, i
, is
);
1096 error
= ext4_xattr_set_entry(i
, s
);
1101 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1102 if (!IS_LAST_ENTRY(s
->first
)) {
1103 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1104 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1106 header
->h_magic
= cpu_to_le32(0);
1107 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1112 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
1113 struct ext4_xattr_info
*i
,
1114 struct ext4_xattr_ibody_find
*is
)
1116 struct ext4_xattr_ibody_header
*header
;
1117 struct ext4_xattr_search
*s
= &is
->s
;
1120 if (EXT4_I(inode
)->i_extra_isize
== 0)
1122 error
= ext4_xattr_set_entry(i
, s
);
1125 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
1126 if (!IS_LAST_ENTRY(s
->first
)) {
1127 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1128 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
1130 header
->h_magic
= cpu_to_le32(0);
1131 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
1136 static int ext4_xattr_value_same(struct ext4_xattr_search
*s
,
1137 struct ext4_xattr_info
*i
)
1141 if (le32_to_cpu(s
->here
->e_value_size
) != i
->value_len
)
1143 value
= ((void *)s
->base
) + le16_to_cpu(s
->here
->e_value_offs
);
1144 return !memcmp(value
, i
->value
, i
->value_len
);
1148 * ext4_xattr_set_handle()
1150 * Create, replace or remove an extended attribute for this inode. Value
1151 * is NULL to remove an existing extended attribute, and non-NULL to
1152 * either replace an existing extended attribute, or create a new extended
1153 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
1154 * specify that an extended attribute must exist and must not exist
1155 * previous to the call, respectively.
1157 * Returns 0, or a negative error number on failure.
1160 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
1161 const char *name
, const void *value
, size_t value_len
,
1164 struct ext4_xattr_info i
= {
1165 .name_index
= name_index
,
1168 .value_len
= value_len
,
1171 struct ext4_xattr_ibody_find is
= {
1172 .s
= { .not_found
= -ENODATA
, },
1174 struct ext4_xattr_block_find bs
= {
1175 .s
= { .not_found
= -ENODATA
, },
1177 unsigned long no_expand
;
1182 if (strlen(name
) > 255)
1184 down_write(&EXT4_I(inode
)->xattr_sem
);
1185 no_expand
= ext4_test_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1186 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1188 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
1192 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
1193 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
1194 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
1195 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
1198 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
1202 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1205 if (is
.s
.not_found
&& bs
.s
.not_found
) {
1207 if (flags
& XATTR_REPLACE
)
1214 if (flags
& XATTR_CREATE
)
1218 if (!is
.s
.not_found
)
1219 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1220 else if (!bs
.s
.not_found
)
1221 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1224 /* Xattr value did not change? Save us some work and bail out */
1225 if (!is
.s
.not_found
&& ext4_xattr_value_same(&is
.s
, &i
))
1227 if (!bs
.s
.not_found
&& ext4_xattr_value_same(&bs
.s
, &i
))
1230 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
1231 if (!error
&& !bs
.s
.not_found
) {
1233 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1234 } else if (error
== -ENOSPC
) {
1235 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
1236 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
1240 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
1243 if (!is
.s
.not_found
) {
1245 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
1251 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
1252 inode
->i_ctime
= ext4_current_time(inode
);
1254 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1255 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
1257 * The bh is consumed by ext4_mark_iloc_dirty, even with
1262 ext4_handle_sync(handle
);
1269 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1270 up_write(&EXT4_I(inode
)->xattr_sem
);
1277 * Like ext4_xattr_set_handle, but start from an inode. This extended
1278 * attribute modification is a filesystem transaction by itself.
1280 * Returns 0, or a negative error number on failure.
1283 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
1284 const void *value
, size_t value_len
, int flags
)
1287 int error
, retries
= 0;
1288 int credits
= ext4_jbd2_credits_xattr(inode
);
1291 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
1292 if (IS_ERR(handle
)) {
1293 error
= PTR_ERR(handle
);
1297 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
1298 value
, value_len
, flags
);
1299 error2
= ext4_journal_stop(handle
);
1300 if (error
== -ENOSPC
&&
1301 ext4_should_retry_alloc(inode
->i_sb
, &retries
))
1311 * Shift the EA entries in the inode to create space for the increased
1314 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
1315 int value_offs_shift
, void *to
,
1316 void *from
, size_t n
)
1318 struct ext4_xattr_entry
*last
= entry
;
1321 /* We always shift xattr headers further thus offsets get lower */
1322 BUG_ON(value_offs_shift
> 0);
1324 /* Adjust the value offsets of the entries */
1325 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1326 if (last
->e_value_size
) {
1327 new_offs
= le16_to_cpu(last
->e_value_offs
) +
1329 last
->e_value_offs
= cpu_to_le16(new_offs
);
1332 /* Shift the entries by n bytes */
1333 memmove(to
, from
, n
);
1337 * Move xattr pointed to by 'entry' from inode into external xattr block
1339 static int ext4_xattr_move_to_block(handle_t
*handle
, struct inode
*inode
,
1340 struct ext4_inode
*raw_inode
,
1341 struct ext4_xattr_entry
*entry
)
1343 struct ext4_xattr_ibody_find
*is
= NULL
;
1344 struct ext4_xattr_block_find
*bs
= NULL
;
1345 char *buffer
= NULL
, *b_entry_name
= NULL
;
1346 size_t value_offs
, value_size
;
1347 struct ext4_xattr_info i
= {
1350 .name_index
= entry
->e_name_index
,
1352 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
1355 value_offs
= le16_to_cpu(entry
->e_value_offs
);
1356 value_size
= le32_to_cpu(entry
->e_value_size
);
1358 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
1359 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
1360 buffer
= kmalloc(value_size
, GFP_NOFS
);
1361 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
1362 if (!is
|| !bs
|| !buffer
|| !b_entry_name
) {
1367 is
->s
.not_found
= -ENODATA
;
1368 bs
->s
.not_found
= -ENODATA
;
1372 /* Save the entry name and the entry value */
1373 memcpy(buffer
, (void *)IFIRST(header
) + value_offs
, value_size
);
1374 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
1375 b_entry_name
[entry
->e_name_len
] = '\0';
1376 i
.name
= b_entry_name
;
1378 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
1382 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
1386 /* Remove the chosen entry from the inode */
1387 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
1391 i
.name
= b_entry_name
;
1393 i
.value_len
= value_size
;
1394 error
= ext4_xattr_block_find(inode
, &i
, bs
);
1398 /* Add entry which was removed from the inode into the block */
1399 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
1404 kfree(b_entry_name
);
1407 brelse(is
->iloc
.bh
);
1414 static int ext4_xattr_make_inode_space(handle_t
*handle
, struct inode
*inode
,
1415 struct ext4_inode
*raw_inode
,
1416 int isize_diff
, size_t ifree
,
1417 size_t bfree
, int *total_ino
)
1419 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
1420 struct ext4_xattr_entry
*small_entry
;
1421 struct ext4_xattr_entry
*entry
;
1422 struct ext4_xattr_entry
*last
;
1423 unsigned int entry_size
; /* EA entry size */
1424 unsigned int total_size
; /* EA entry size + value size */
1425 unsigned int min_total_size
;
1428 while (isize_diff
> ifree
) {
1431 min_total_size
= ~0U;
1432 last
= IFIRST(header
);
1433 /* Find the entry best suited to be pushed into EA block */
1434 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1436 EXT4_XATTR_SIZE(le32_to_cpu(last
->e_value_size
)) +
1437 EXT4_XATTR_LEN(last
->e_name_len
);
1438 if (total_size
<= bfree
&&
1439 total_size
< min_total_size
) {
1440 if (total_size
+ ifree
< isize_diff
) {
1444 min_total_size
= total_size
;
1449 if (entry
== NULL
) {
1450 if (small_entry
== NULL
)
1452 entry
= small_entry
;
1455 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
1456 total_size
= entry_size
+
1457 EXT4_XATTR_SIZE(le32_to_cpu(entry
->e_value_size
));
1458 error
= ext4_xattr_move_to_block(handle
, inode
, raw_inode
,
1463 *total_ino
-= entry_size
;
1464 ifree
+= total_size
;
1465 bfree
-= total_size
;
1472 * Expand an inode by new_extra_isize bytes when EAs are present.
1473 * Returns 0 on success or negative error number on failure.
1475 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
1476 struct ext4_inode
*raw_inode
, handle_t
*handle
)
1478 struct ext4_xattr_ibody_header
*header
;
1479 struct buffer_head
*bh
= NULL
;
1481 size_t ifree
, bfree
;
1484 int error
= 0, tried_min_extra_isize
= 0;
1485 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
1486 int isize_diff
; /* How much do we need to grow i_extra_isize */
1488 down_write(&EXT4_I(inode
)->xattr_sem
);
1490 * Set EXT4_STATE_NO_EXPAND to avoid recursion when marking inode dirty
1492 ext4_set_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1494 isize_diff
= new_extra_isize
- EXT4_I(inode
)->i_extra_isize
;
1495 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
1498 header
= IHDR(inode
, raw_inode
);
1501 * Check if enough free space is available in the inode to shift the
1502 * entries ahead by new_extra_isize.
1505 base
= IFIRST(header
);
1506 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
1507 min_offs
= end
- base
;
1508 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
1510 error
= xattr_check_inode(inode
, header
, end
);
1514 ifree
= ext4_xattr_free_space(base
, &min_offs
, base
, &total_ino
);
1515 if (ifree
>= isize_diff
)
1519 * Enough free space isn't available in the inode, check if
1520 * EA block can hold new_extra_isize bytes.
1522 if (EXT4_I(inode
)->i_file_acl
) {
1523 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1527 if (ext4_xattr_check_block(inode
, bh
)) {
1528 EXT4_ERROR_INODE(inode
, "bad block %llu",
1529 EXT4_I(inode
)->i_file_acl
);
1530 error
= -EFSCORRUPTED
;
1534 end
= bh
->b_data
+ bh
->b_size
;
1535 min_offs
= end
- base
;
1536 bfree
= ext4_xattr_free_space(BFIRST(bh
), &min_offs
, base
,
1538 if (bfree
+ ifree
< isize_diff
) {
1539 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
1540 tried_min_extra_isize
++;
1541 new_extra_isize
= s_min_extra_isize
;
1549 bfree
= inode
->i_sb
->s_blocksize
;
1552 error
= ext4_xattr_make_inode_space(handle
, inode
, raw_inode
,
1553 isize_diff
, ifree
, bfree
,
1556 if (error
== -ENOSPC
&& !tried_min_extra_isize
&&
1557 s_min_extra_isize
) {
1558 tried_min_extra_isize
++;
1559 new_extra_isize
= s_min_extra_isize
;
1566 /* Adjust the offsets and shift the remaining entries ahead */
1567 ext4_xattr_shift_entries(IFIRST(header
), EXT4_I(inode
)->i_extra_isize
1568 - new_extra_isize
, (void *)raw_inode
+
1569 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
1570 (void *)header
, total_ino
);
1571 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
1574 ext4_clear_inode_state(inode
, EXT4_STATE_NO_EXPAND
);
1575 up_write(&EXT4_I(inode
)->xattr_sem
);
1581 * We deliberately leave EXT4_STATE_NO_EXPAND set here since inode
1582 * size expansion failed.
1584 up_write(&EXT4_I(inode
)->xattr_sem
);
1591 * ext4_xattr_delete_inode()
1593 * Free extended attribute resources associated with this inode. This
1594 * is called immediately before an inode is freed. We have exclusive
1595 * access to the inode.
1598 ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
)
1600 struct buffer_head
*bh
= NULL
;
1602 if (!EXT4_I(inode
)->i_file_acl
)
1604 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
1606 EXT4_ERROR_INODE(inode
, "block %llu read error",
1607 EXT4_I(inode
)->i_file_acl
);
1610 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
1611 BHDR(bh
)->h_blocks
!= cpu_to_le32(1)) {
1612 EXT4_ERROR_INODE(inode
, "bad block %llu",
1613 EXT4_I(inode
)->i_file_acl
);
1616 ext4_xattr_release_block(handle
, inode
, bh
);
1617 EXT4_I(inode
)->i_file_acl
= 0;
1624 * ext4_xattr_cache_insert()
1626 * Create a new entry in the extended attribute cache, and insert
1627 * it unless such an entry is already in the cache.
1629 * Returns 0, or a negative error number on failure.
1632 ext4_xattr_cache_insert(struct mb_cache
*ext4_mb_cache
, struct buffer_head
*bh
)
1634 struct ext4_xattr_header
*header
= BHDR(bh
);
1635 __u32 hash
= le32_to_cpu(header
->h_hash
);
1636 int reusable
= le32_to_cpu(header
->h_refcount
) <
1637 EXT4_XATTR_REFCOUNT_MAX
;
1640 error
= mb_cache_entry_create(ext4_mb_cache
, GFP_NOFS
, hash
,
1641 bh
->b_blocknr
, reusable
);
1643 if (error
== -EBUSY
)
1644 ea_bdebug(bh
, "already in cache");
1646 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
1652 * Compare two extended attribute blocks for equality.
1654 * Returns 0 if the blocks are equal, 1 if they differ, and
1655 * a negative error number on errors.
1658 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
1659 struct ext4_xattr_header
*header2
)
1661 struct ext4_xattr_entry
*entry1
, *entry2
;
1663 entry1
= ENTRY(header1
+1);
1664 entry2
= ENTRY(header2
+1);
1665 while (!IS_LAST_ENTRY(entry1
)) {
1666 if (IS_LAST_ENTRY(entry2
))
1668 if (entry1
->e_hash
!= entry2
->e_hash
||
1669 entry1
->e_name_index
!= entry2
->e_name_index
||
1670 entry1
->e_name_len
!= entry2
->e_name_len
||
1671 entry1
->e_value_size
!= entry2
->e_value_size
||
1672 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
1674 if (entry1
->e_value_block
!= 0 || entry2
->e_value_block
!= 0)
1675 return -EFSCORRUPTED
;
1676 if (memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
1677 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
1678 le32_to_cpu(entry1
->e_value_size
)))
1681 entry1
= EXT4_XATTR_NEXT(entry1
);
1682 entry2
= EXT4_XATTR_NEXT(entry2
);
1684 if (!IS_LAST_ENTRY(entry2
))
1690 * ext4_xattr_cache_find()
1692 * Find an identical extended attribute block.
1694 * Returns a pointer to the block found, or NULL if such a block was
1695 * not found or an error occurred.
1697 static struct buffer_head
*
1698 ext4_xattr_cache_find(struct inode
*inode
, struct ext4_xattr_header
*header
,
1699 struct mb_cache_entry
**pce
)
1701 __u32 hash
= le32_to_cpu(header
->h_hash
);
1702 struct mb_cache_entry
*ce
;
1703 struct mb_cache
*ext4_mb_cache
= EXT4_GET_MB_CACHE(inode
);
1705 if (!header
->h_hash
)
1706 return NULL
; /* never share */
1707 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
1708 ce
= mb_cache_entry_find_first(ext4_mb_cache
, hash
);
1710 struct buffer_head
*bh
;
1712 bh
= sb_bread(inode
->i_sb
, ce
->e_block
);
1714 EXT4_ERROR_INODE(inode
, "block %lu read error",
1715 (unsigned long) ce
->e_block
);
1716 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
1721 ce
= mb_cache_entry_find_next(ext4_mb_cache
, ce
);
1726 #define NAME_HASH_SHIFT 5
1727 #define VALUE_HASH_SHIFT 16
1730 * ext4_xattr_hash_entry()
1732 * Compute the hash of an extended attribute.
1734 static inline void ext4_xattr_hash_entry(struct ext4_xattr_header
*header
,
1735 struct ext4_xattr_entry
*entry
)
1738 char *name
= entry
->e_name
;
1741 for (n
= 0; n
< entry
->e_name_len
; n
++) {
1742 hash
= (hash
<< NAME_HASH_SHIFT
) ^
1743 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
1747 if (entry
->e_value_size
!= 0) {
1748 __le32
*value
= (__le32
*)((char *)header
+
1749 le16_to_cpu(entry
->e_value_offs
));
1750 for (n
= (le32_to_cpu(entry
->e_value_size
) +
1751 EXT4_XATTR_ROUND
) >> EXT4_XATTR_PAD_BITS
; n
; n
--) {
1752 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
1753 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
1754 le32_to_cpu(*value
++);
1757 entry
->e_hash
= cpu_to_le32(hash
);
1760 #undef NAME_HASH_SHIFT
1761 #undef VALUE_HASH_SHIFT
1763 #define BLOCK_HASH_SHIFT 16
1766 * ext4_xattr_rehash()
1768 * Re-compute the extended attribute hash value after an entry has changed.
1770 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
,
1771 struct ext4_xattr_entry
*entry
)
1773 struct ext4_xattr_entry
*here
;
1776 ext4_xattr_hash_entry(header
, entry
);
1777 here
= ENTRY(header
+1);
1778 while (!IS_LAST_ENTRY(here
)) {
1779 if (!here
->e_hash
) {
1780 /* Block is not shared if an entry's hash value == 0 */
1784 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
1785 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
1786 le32_to_cpu(here
->e_hash
);
1787 here
= EXT4_XATTR_NEXT(here
);
1789 header
->h_hash
= cpu_to_le32(hash
);
1792 #undef BLOCK_HASH_SHIFT
1794 #define HASH_BUCKET_BITS 10
1797 ext4_xattr_create_cache(void)
1799 return mb_cache_create(HASH_BUCKET_BITS
);
1802 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
1805 mb_cache_destroy(cache
);