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_block_cache_insert(struct mb_cache
*,
76 struct buffer_head
*);
77 static struct buffer_head
*
78 ext4_xattr_block_cache_find(struct inode
*, struct ext4_xattr_header
*,
79 struct mb_cache_entry
**);
80 static __le32
ext4_xattr_hash_entry(char *name
, size_t name_len
, __le32
*value
,
82 static void ext4_xattr_rehash(struct ext4_xattr_header
*);
84 static const struct xattr_handler
* const 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 EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
110 inode->i_sb->s_fs_info)->s_ea_block_cache)
112 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
113 inode->i_sb->s_fs_info)->s_ea_inode_cache)
116 ext4_expand_inode_array(struct ext4_xattr_inode_array
**ea_inode_array
,
117 struct inode
*inode
);
119 #ifdef CONFIG_LOCKDEP
120 void ext4_xattr_inode_set_class(struct inode
*ea_inode
)
122 lockdep_set_subclass(&ea_inode
->i_rwsem
, 1);
126 static __le32
ext4_xattr_block_csum(struct inode
*inode
,
128 struct ext4_xattr_header
*hdr
)
130 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
132 __le64 dsk_block_nr
= cpu_to_le64(block_nr
);
133 __u32 dummy_csum
= 0;
134 int offset
= offsetof(struct ext4_xattr_header
, h_checksum
);
136 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&dsk_block_nr
,
137 sizeof(dsk_block_nr
));
138 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
, offset
);
139 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, sizeof(dummy_csum
));
140 offset
+= sizeof(dummy_csum
);
141 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
+ offset
,
142 EXT4_BLOCK_SIZE(inode
->i_sb
) - offset
);
144 return cpu_to_le32(csum
);
147 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
148 struct buffer_head
*bh
)
150 struct ext4_xattr_header
*hdr
= BHDR(bh
);
153 if (ext4_has_metadata_csum(inode
->i_sb
)) {
155 ret
= (hdr
->h_checksum
== ext4_xattr_block_csum(inode
,
156 bh
->b_blocknr
, hdr
));
162 static void ext4_xattr_block_csum_set(struct inode
*inode
,
163 struct buffer_head
*bh
)
165 if (ext4_has_metadata_csum(inode
->i_sb
))
166 BHDR(bh
)->h_checksum
= ext4_xattr_block_csum(inode
,
167 bh
->b_blocknr
, BHDR(bh
));
170 static inline const struct xattr_handler
*
171 ext4_xattr_handler(int name_index
)
173 const struct xattr_handler
*handler
= NULL
;
175 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
176 handler
= ext4_xattr_handler_map
[name_index
];
181 ext4_xattr_check_entries(struct ext4_xattr_entry
*entry
, void *end
,
184 struct ext4_xattr_entry
*e
= entry
;
186 /* Find the end of the names list */
187 while (!IS_LAST_ENTRY(e
)) {
188 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(e
);
189 if ((void *)next
>= end
)
190 return -EFSCORRUPTED
;
194 /* Check the values */
195 while (!IS_LAST_ENTRY(entry
)) {
196 if (entry
->e_value_size
!= 0 &&
197 entry
->e_value_inum
== 0) {
198 u16 offs
= le16_to_cpu(entry
->e_value_offs
);
199 u32 size
= le32_to_cpu(entry
->e_value_size
);
203 * The value cannot overlap the names, and the value
204 * with padding cannot extend beyond 'end'. Check both
205 * the padded and unpadded sizes, since the size may
206 * overflow to 0 when adding padding.
208 if (offs
> end
- value_start
)
209 return -EFSCORRUPTED
;
210 value
= value_start
+ offs
;
211 if (value
< (void *)e
+ sizeof(u32
) ||
212 size
> end
- value
||
213 EXT4_XATTR_SIZE(size
) > end
- value
)
214 return -EFSCORRUPTED
;
216 entry
= EXT4_XATTR_NEXT(entry
);
223 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
227 if (buffer_verified(bh
))
230 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
231 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
232 return -EFSCORRUPTED
;
233 if (!ext4_xattr_block_csum_verify(inode
, bh
))
235 error
= ext4_xattr_check_entries(BFIRST(bh
), bh
->b_data
+ bh
->b_size
,
238 set_buffer_verified(bh
);
243 __xattr_check_inode(struct inode
*inode
, struct ext4_xattr_ibody_header
*header
,
244 void *end
, const char *function
, unsigned int line
)
246 int error
= -EFSCORRUPTED
;
248 if (end
- (void *)header
< sizeof(*header
) + sizeof(u32
) ||
249 (header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)))
251 error
= ext4_xattr_check_entries(IFIRST(header
), end
, IFIRST(header
));
254 __ext4_error_inode(inode
, function
, line
, 0,
255 "corrupted in-inode xattr");
259 #define xattr_check_inode(inode, header, end) \
260 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
263 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
264 const char *name
, 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 return cmp
? -ENODATA
: 0;
288 ext4_xattr_inode_hash(struct ext4_sb_info
*sbi
, const void *buffer
, size_t size
)
290 return ext4_chksum(sbi
, sbi
->s_csum_seed
, buffer
, size
);
293 static u64
ext4_xattr_inode_get_ref(struct inode
*ea_inode
)
295 return ((u64
)ea_inode
->i_ctime
.tv_sec
<< 32) |
296 ((u32
)ea_inode
->i_version
);
299 static void ext4_xattr_inode_set_ref(struct inode
*ea_inode
, u64 ref_count
)
301 ea_inode
->i_ctime
.tv_sec
= (u32
)(ref_count
>> 32);
302 ea_inode
->i_version
= (u32
)ref_count
;
305 static u32
ext4_xattr_inode_get_hash(struct inode
*ea_inode
)
307 return (u32
)ea_inode
->i_atime
.tv_sec
;
310 static void ext4_xattr_inode_set_hash(struct inode
*ea_inode
, u32 hash
)
312 ea_inode
->i_atime
.tv_sec
= hash
;
316 * Read the EA value from an inode.
318 static int ext4_xattr_inode_read(struct inode
*ea_inode
, void *buf
, size_t size
)
320 int blocksize
= 1 << ea_inode
->i_blkbits
;
321 int bh_count
= (size
+ blocksize
- 1) >> ea_inode
->i_blkbits
;
322 int tail_size
= (size
% blocksize
) ?: blocksize
;
323 struct buffer_head
*bhs_inline
[8];
324 struct buffer_head
**bhs
= bhs_inline
;
327 if (bh_count
> ARRAY_SIZE(bhs_inline
)) {
328 bhs
= kmalloc_array(bh_count
, sizeof(*bhs
), GFP_NOFS
);
333 ret
= ext4_bread_batch(ea_inode
, 0 /* block */, bh_count
,
334 true /* wait */, bhs
);
338 for (i
= 0; i
< bh_count
; i
++) {
339 /* There shouldn't be any holes in ea_inode. */
344 memcpy((char *)buf
+ blocksize
* i
, bhs
[i
]->b_data
,
345 i
< bh_count
- 1 ? blocksize
: tail_size
);
349 for (i
= 0; i
< bh_count
; i
++)
352 if (bhs
!= bhs_inline
)
357 static int ext4_xattr_inode_iget(struct inode
*parent
, unsigned long ea_ino
,
358 struct inode
**ea_inode
)
363 inode
= ext4_iget(parent
->i_sb
, ea_ino
);
365 err
= PTR_ERR(inode
);
366 ext4_error(parent
->i_sb
,
367 "error while reading EA inode %lu err=%d", ea_ino
,
372 if (is_bad_inode(inode
)) {
373 ext4_error(parent
->i_sb
,
374 "error while reading EA inode %lu is_bad_inode",
380 if (!(EXT4_I(inode
)->i_flags
& EXT4_EA_INODE_FL
)) {
381 ext4_error(parent
->i_sb
,
382 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
396 ext4_xattr_inode_verify_hashes(struct inode
*ea_inode
,
397 struct ext4_xattr_entry
*entry
, void *buffer
,
402 /* Verify stored hash matches calculated hash. */
403 hash
= ext4_xattr_inode_hash(EXT4_SB(ea_inode
->i_sb
), buffer
, size
);
404 if (hash
!= ext4_xattr_inode_get_hash(ea_inode
))
405 return -EFSCORRUPTED
;
408 __le32 e_hash
, tmp_data
;
410 /* Verify entry hash. */
411 tmp_data
= cpu_to_le32(hash
);
412 e_hash
= ext4_xattr_hash_entry(entry
->e_name
, entry
->e_name_len
,
414 if (e_hash
!= entry
->e_hash
)
415 return -EFSCORRUPTED
;
420 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
423 * Read xattr value from the EA inode.
426 ext4_xattr_inode_get(struct inode
*inode
, struct ext4_xattr_entry
*entry
,
427 void *buffer
, size_t size
)
429 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(inode
);
430 struct inode
*ea_inode
;
433 err
= ext4_xattr_inode_iget(inode
, le32_to_cpu(entry
->e_value_inum
),
440 if (i_size_read(ea_inode
) != size
) {
441 ext4_warning_inode(ea_inode
,
442 "ea_inode file size=%llu entry size=%zu",
443 i_size_read(ea_inode
), size
);
448 err
= ext4_xattr_inode_read(ea_inode
, buffer
, size
);
452 err
= ext4_xattr_inode_verify_hashes(ea_inode
, entry
, buffer
, size
);
454 * Compatibility check for old Lustre ea_inode implementation. Old
455 * version does not have hash validation, but it has a backpointer
456 * from ea_inode to the parent inode.
458 if (err
== -EFSCORRUPTED
) {
459 if (EXT4_XATTR_INODE_GET_PARENT(ea_inode
) != inode
->i_ino
||
460 ea_inode
->i_generation
!= inode
->i_generation
) {
461 ext4_warning_inode(ea_inode
,
462 "EA inode hash validation failed");
465 /* Do not add ea_inode to the cache. */
466 ea_inode_cache
= NULL
;
472 mb_cache_entry_create(ea_inode_cache
, GFP_NOFS
,
473 ext4_xattr_inode_get_hash(ea_inode
),
474 ea_inode
->i_ino
, true /* reusable */);
481 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
482 void *buffer
, size_t buffer_size
)
484 struct buffer_head
*bh
= NULL
;
485 struct ext4_xattr_entry
*entry
;
488 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
490 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
491 name_index
, name
, buffer
, (long)buffer_size
);
494 if (!EXT4_I(inode
)->i_file_acl
)
496 ea_idebug(inode
, "reading block %llu",
497 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
498 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
501 ea_bdebug(bh
, "b_count=%d, refcount=%d",
502 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
503 if (ext4_xattr_check_block(inode
, bh
)) {
504 EXT4_ERROR_INODE(inode
, "bad block %llu",
505 EXT4_I(inode
)->i_file_acl
);
506 error
= -EFSCORRUPTED
;
509 ext4_xattr_block_cache_insert(ea_block_cache
, bh
);
511 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, 1);
514 size
= le32_to_cpu(entry
->e_value_size
);
517 if (size
> buffer_size
)
519 if (entry
->e_value_inum
) {
520 error
= ext4_xattr_inode_get(inode
, entry
, buffer
,
525 memcpy(buffer
, bh
->b_data
+
526 le16_to_cpu(entry
->e_value_offs
), size
);
537 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
538 void *buffer
, size_t buffer_size
)
540 struct ext4_xattr_ibody_header
*header
;
541 struct ext4_xattr_entry
*entry
;
542 struct ext4_inode
*raw_inode
;
543 struct ext4_iloc iloc
;
548 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
550 error
= ext4_get_inode_loc(inode
, &iloc
);
553 raw_inode
= ext4_raw_inode(&iloc
);
554 header
= IHDR(inode
, raw_inode
);
555 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
556 error
= xattr_check_inode(inode
, header
, end
);
559 entry
= IFIRST(header
);
560 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, 0);
563 size
= le32_to_cpu(entry
->e_value_size
);
566 if (size
> buffer_size
)
568 if (entry
->e_value_inum
) {
569 error
= ext4_xattr_inode_get(inode
, entry
, buffer
,
574 memcpy(buffer
, (void *)IFIRST(header
) +
575 le16_to_cpu(entry
->e_value_offs
), size
);
588 * Copy an extended attribute into the buffer
589 * provided, or compute the buffer size required.
590 * Buffer is NULL to compute the size of the buffer required.
592 * Returns a negative error number on failure, or the number of bytes
593 * used / required on success.
596 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
597 void *buffer
, size_t buffer_size
)
601 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
604 if (strlen(name
) > 255)
607 down_read(&EXT4_I(inode
)->xattr_sem
);
608 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
610 if (error
== -ENODATA
)
611 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
613 up_read(&EXT4_I(inode
)->xattr_sem
);
618 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
619 char *buffer
, size_t buffer_size
)
621 size_t rest
= buffer_size
;
623 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
624 const struct xattr_handler
*handler
=
625 ext4_xattr_handler(entry
->e_name_index
);
627 if (handler
&& (!handler
->list
|| handler
->list(dentry
))) {
628 const char *prefix
= handler
->prefix
?: handler
->name
;
629 size_t prefix_len
= strlen(prefix
);
630 size_t size
= prefix_len
+ entry
->e_name_len
+ 1;
635 memcpy(buffer
, prefix
, prefix_len
);
636 buffer
+= prefix_len
;
637 memcpy(buffer
, entry
->e_name
, entry
->e_name_len
);
638 buffer
+= entry
->e_name_len
;
644 return buffer_size
- rest
; /* total size */
648 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
650 struct inode
*inode
= d_inode(dentry
);
651 struct buffer_head
*bh
= NULL
;
654 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
655 buffer
, (long)buffer_size
);
658 if (!EXT4_I(inode
)->i_file_acl
)
660 ea_idebug(inode
, "reading block %llu",
661 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
662 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
666 ea_bdebug(bh
, "b_count=%d, refcount=%d",
667 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
668 if (ext4_xattr_check_block(inode
, bh
)) {
669 EXT4_ERROR_INODE(inode
, "bad block %llu",
670 EXT4_I(inode
)->i_file_acl
);
671 error
= -EFSCORRUPTED
;
674 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode
), bh
);
675 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
684 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
686 struct inode
*inode
= d_inode(dentry
);
687 struct ext4_xattr_ibody_header
*header
;
688 struct ext4_inode
*raw_inode
;
689 struct ext4_iloc iloc
;
693 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
695 error
= ext4_get_inode_loc(inode
, &iloc
);
698 raw_inode
= ext4_raw_inode(&iloc
);
699 header
= IHDR(inode
, raw_inode
);
700 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
701 error
= xattr_check_inode(inode
, header
, end
);
704 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
705 buffer
, buffer_size
);
713 * Inode operation listxattr()
715 * d_inode(dentry)->i_rwsem: don't care
717 * Copy a list of attribute names into the buffer
718 * provided, or compute the buffer size required.
719 * Buffer is NULL to compute the size of the buffer required.
721 * Returns a negative error number on failure, or the number of bytes
722 * used / required on success.
725 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
729 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
730 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
737 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
742 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
747 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
750 static void ext4_xattr_update_super_block(handle_t
*handle
,
751 struct super_block
*sb
)
753 if (ext4_has_feature_xattr(sb
))
756 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
757 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
758 ext4_set_feature_xattr(sb
);
759 ext4_handle_dirty_super(handle
, sb
);
763 int ext4_get_inode_usage(struct inode
*inode
, qsize_t
*usage
)
765 struct ext4_iloc iloc
= { .bh
= NULL
};
766 struct buffer_head
*bh
= NULL
;
767 struct ext4_inode
*raw_inode
;
768 struct ext4_xattr_ibody_header
*header
;
769 struct ext4_xattr_entry
*entry
;
770 qsize_t ea_inode_refs
= 0;
774 lockdep_assert_held_read(&EXT4_I(inode
)->xattr_sem
);
776 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
777 ret
= ext4_get_inode_loc(inode
, &iloc
);
780 raw_inode
= ext4_raw_inode(&iloc
);
781 header
= IHDR(inode
, raw_inode
);
782 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
783 ret
= xattr_check_inode(inode
, header
, end
);
787 for (entry
= IFIRST(header
); !IS_LAST_ENTRY(entry
);
788 entry
= EXT4_XATTR_NEXT(entry
))
789 if (entry
->e_value_inum
)
793 if (EXT4_I(inode
)->i_file_acl
) {
794 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
800 if (ext4_xattr_check_block(inode
, bh
)) {
805 for (entry
= BFIRST(bh
); !IS_LAST_ENTRY(entry
);
806 entry
= EXT4_XATTR_NEXT(entry
))
807 if (entry
->e_value_inum
)
810 *usage
= ea_inode_refs
+ 1;
818 static inline size_t round_up_cluster(struct inode
*inode
, size_t length
)
820 struct super_block
*sb
= inode
->i_sb
;
821 size_t cluster_size
= 1 << (EXT4_SB(sb
)->s_cluster_bits
+
823 size_t mask
= ~(cluster_size
- 1);
825 return (length
+ cluster_size
- 1) & mask
;
828 static int ext4_xattr_inode_alloc_quota(struct inode
*inode
, size_t len
)
832 err
= dquot_alloc_inode(inode
);
835 err
= dquot_alloc_space_nodirty(inode
, round_up_cluster(inode
, len
));
837 dquot_free_inode(inode
);
841 static void ext4_xattr_inode_free_quota(struct inode
*inode
, size_t len
)
843 dquot_free_space_nodirty(inode
, round_up_cluster(inode
, len
));
844 dquot_free_inode(inode
);
847 int __ext4_xattr_set_credits(struct super_block
*sb
, struct inode
*inode
,
848 struct buffer_head
*block_bh
, size_t value_len
,
855 * 1) Owner inode update
856 * 2) Ref count update on old xattr block
858 * 4) block bitmap update for new xattr block
859 * 5) group descriptor for new xattr block
860 * 6) block bitmap update for old xattr block
861 * 7) group descriptor for old block
863 * 6 & 7 can happen if we have two racing threads T_a and T_b
864 * which are each trying to set an xattr on inodes I_a and I_b
865 * which were both initially sharing an xattr block.
870 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(sb
);
873 * In case of inline data, we may push out the data to a block,
874 * so we need to reserve credits for this eventuality
876 if (inode
&& ext4_has_inline_data(inode
))
877 credits
+= ext4_writepage_trans_blocks(inode
) + 1;
879 /* We are done if ea_inode feature is not enabled. */
880 if (!ext4_has_feature_ea_inode(sb
))
883 /* New ea_inode, inode map, block bitmap, group descriptor. */
887 blocks
= (value_len
+ sb
->s_blocksize
- 1) >> sb
->s_blocksize_bits
;
889 /* Indirection block or one level of extent tree. */
892 /* Block bitmap and group descriptor updates for each block. */
893 credits
+= blocks
* 2;
895 /* Blocks themselves. */
899 /* Dereference ea_inode holding old xattr value.
900 * Old ea_inode, inode map, block bitmap, group descriptor.
904 /* Data blocks for old ea_inode. */
905 blocks
= XATTR_SIZE_MAX
>> sb
->s_blocksize_bits
;
907 /* Indirection block or one level of extent tree for old
912 /* Block bitmap and group descriptor updates for each block. */
913 credits
+= blocks
* 2;
916 /* We may need to clone the existing xattr block in which case we need
917 * to increment ref counts for existing ea_inodes referenced by it.
920 struct ext4_xattr_entry
*entry
= BFIRST(block_bh
);
922 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
))
923 if (entry
->e_value_inum
)
924 /* Ref count update on ea_inode. */
930 static int ext4_xattr_ensure_credits(handle_t
*handle
, struct inode
*inode
,
931 int credits
, struct buffer_head
*bh
,
932 bool dirty
, bool block_csum
)
936 if (!ext4_handle_valid(handle
))
939 if (handle
->h_buffer_credits
>= credits
)
942 error
= ext4_journal_extend(handle
, credits
- handle
->h_buffer_credits
);
946 ext4_warning(inode
->i_sb
, "Extend journal (error %d)", error
);
952 ext4_xattr_block_csum_set(inode
, bh
);
953 error
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
955 ext4_warning(inode
->i_sb
, "Handle metadata (error %d)",
961 error
= ext4_journal_restart(handle
, credits
);
963 ext4_warning(inode
->i_sb
, "Restart journal (error %d)", error
);
968 error
= ext4_journal_get_write_access(handle
, bh
);
970 ext4_warning(inode
->i_sb
,
971 "Get write access failed (error %d)",
979 static int ext4_xattr_inode_update_ref(handle_t
*handle
, struct inode
*ea_inode
,
982 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(ea_inode
);
983 struct ext4_iloc iloc
;
988 inode_lock(ea_inode
);
990 ret
= ext4_reserve_inode_write(handle
, ea_inode
, &iloc
);
996 ref_count
= ext4_xattr_inode_get_ref(ea_inode
);
997 ref_count
+= ref_change
;
998 ext4_xattr_inode_set_ref(ea_inode
, ref_count
);
1000 if (ref_change
> 0) {
1001 WARN_ONCE(ref_count
<= 0, "EA inode %lu ref_count=%lld",
1002 ea_inode
->i_ino
, ref_count
);
1004 if (ref_count
== 1) {
1005 WARN_ONCE(ea_inode
->i_nlink
, "EA inode %lu i_nlink=%u",
1006 ea_inode
->i_ino
, ea_inode
->i_nlink
);
1008 set_nlink(ea_inode
, 1);
1009 ext4_orphan_del(handle
, ea_inode
);
1011 if (ea_inode_cache
) {
1012 hash
= ext4_xattr_inode_get_hash(ea_inode
);
1013 mb_cache_entry_create(ea_inode_cache
,
1016 true /* reusable */);
1020 WARN_ONCE(ref_count
< 0, "EA inode %lu ref_count=%lld",
1021 ea_inode
->i_ino
, ref_count
);
1023 if (ref_count
== 0) {
1024 WARN_ONCE(ea_inode
->i_nlink
!= 1,
1025 "EA inode %lu i_nlink=%u",
1026 ea_inode
->i_ino
, ea_inode
->i_nlink
);
1028 clear_nlink(ea_inode
);
1029 ext4_orphan_add(handle
, ea_inode
);
1031 if (ea_inode_cache
) {
1032 hash
= ext4_xattr_inode_get_hash(ea_inode
);
1033 mb_cache_entry_delete(ea_inode_cache
, hash
,
1039 ret
= ext4_mark_iloc_dirty(handle
, ea_inode
, &iloc
);
1042 ext4_warning_inode(ea_inode
,
1043 "ext4_mark_iloc_dirty() failed ret=%d", ret
);
1046 inode_unlock(ea_inode
);
1050 static int ext4_xattr_inode_inc_ref(handle_t
*handle
, struct inode
*ea_inode
)
1052 return ext4_xattr_inode_update_ref(handle
, ea_inode
, 1);
1055 static int ext4_xattr_inode_dec_ref(handle_t
*handle
, struct inode
*ea_inode
)
1057 return ext4_xattr_inode_update_ref(handle
, ea_inode
, -1);
1060 static int ext4_xattr_inode_inc_ref_all(handle_t
*handle
, struct inode
*parent
,
1061 struct ext4_xattr_entry
*first
)
1063 struct inode
*ea_inode
;
1064 struct ext4_xattr_entry
*entry
;
1065 struct ext4_xattr_entry
*failed_entry
;
1066 unsigned int ea_ino
;
1069 for (entry
= first
; !IS_LAST_ENTRY(entry
);
1070 entry
= EXT4_XATTR_NEXT(entry
)) {
1071 if (!entry
->e_value_inum
)
1073 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1074 err
= ext4_xattr_inode_iget(parent
, ea_ino
, &ea_inode
);
1077 err
= ext4_xattr_inode_inc_ref(handle
, ea_inode
);
1079 ext4_warning_inode(ea_inode
, "inc ref error %d", err
);
1089 failed_entry
= entry
;
1091 for (entry
= first
; entry
!= failed_entry
;
1092 entry
= EXT4_XATTR_NEXT(entry
)) {
1093 if (!entry
->e_value_inum
)
1095 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1096 err
= ext4_xattr_inode_iget(parent
, ea_ino
, &ea_inode
);
1098 ext4_warning(parent
->i_sb
,
1099 "cleanup ea_ino %u iget error %d", ea_ino
,
1103 err
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1105 ext4_warning_inode(ea_inode
, "cleanup dec ref error %d",
1113 ext4_xattr_inode_dec_ref_all(handle_t
*handle
, struct inode
*parent
,
1114 struct buffer_head
*bh
,
1115 struct ext4_xattr_entry
*first
, bool block_csum
,
1116 struct ext4_xattr_inode_array
**ea_inode_array
,
1117 int extra_credits
, bool skip_quota
)
1119 struct inode
*ea_inode
;
1120 struct ext4_xattr_entry
*entry
;
1122 unsigned int ea_ino
;
1126 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1127 credits
= 2 + extra_credits
;
1129 for (entry
= first
; !IS_LAST_ENTRY(entry
);
1130 entry
= EXT4_XATTR_NEXT(entry
)) {
1131 if (!entry
->e_value_inum
)
1133 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1134 err
= ext4_xattr_inode_iget(parent
, ea_ino
, &ea_inode
);
1138 err
= ext4_expand_inode_array(ea_inode_array
, ea_inode
);
1140 ext4_warning_inode(ea_inode
,
1141 "Expand inode array err=%d", err
);
1146 err
= ext4_xattr_ensure_credits(handle
, parent
, credits
, bh
,
1149 ext4_warning_inode(ea_inode
, "Ensure credits err=%d",
1154 err
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1156 ext4_warning_inode(ea_inode
, "ea_inode dec ref err=%d",
1162 ext4_xattr_inode_free_quota(parent
,
1163 le32_to_cpu(entry
->e_value_size
));
1166 * Forget about ea_inode within the same transaction that
1167 * decrements the ref count. This avoids duplicate decrements in
1168 * case the rest of the work spills over to subsequent
1171 entry
->e_value_inum
= 0;
1172 entry
->e_value_size
= 0;
1179 * Note that we are deliberately skipping csum calculation for
1180 * the final update because we do not expect any journal
1181 * restarts until xattr block is freed.
1184 err
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
1186 ext4_warning_inode(parent
,
1187 "handle dirty metadata err=%d", err
);
1192 * Release the xattr block BH: If the reference count is > 1, decrement it;
1193 * otherwise free the block.
1196 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
1197 struct buffer_head
*bh
,
1198 struct ext4_xattr_inode_array
**ea_inode_array
,
1201 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
1205 BUFFER_TRACE(bh
, "get_write_access");
1206 error
= ext4_journal_get_write_access(handle
, bh
);
1211 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1212 ref
= le32_to_cpu(BHDR(bh
)->h_refcount
);
1214 ea_bdebug(bh
, "refcount now=0; freeing");
1216 * This must happen under buffer lock for
1217 * ext4_xattr_block_set() to reliably detect freed block
1220 mb_cache_entry_delete(ea_block_cache
, hash
,
1225 if (ext4_has_feature_ea_inode(inode
->i_sb
))
1226 ext4_xattr_inode_dec_ref_all(handle
, inode
, bh
,
1228 true /* block_csum */,
1231 true /* skip_quota */);
1232 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
1233 EXT4_FREE_BLOCKS_METADATA
|
1234 EXT4_FREE_BLOCKS_FORGET
);
1237 BHDR(bh
)->h_refcount
= cpu_to_le32(ref
);
1238 if (ref
== EXT4_XATTR_REFCOUNT_MAX
- 1) {
1239 struct mb_cache_entry
*ce
;
1241 if (ea_block_cache
) {
1242 ce
= mb_cache_entry_get(ea_block_cache
, hash
,
1246 mb_cache_entry_put(ea_block_cache
, ce
);
1251 ext4_xattr_block_csum_set(inode
, bh
);
1253 * Beware of this ugliness: Releasing of xattr block references
1254 * from different inodes can race and so we have to protect
1255 * from a race where someone else frees the block (and releases
1256 * its journal_head) before we are done dirtying the buffer. In
1257 * nojournal mode this race is harmless and we actually cannot
1258 * call ext4_handle_dirty_metadata() with locked buffer as
1259 * that function can call sync_dirty_buffer() so for that case
1260 * we handle the dirtying after unlocking the buffer.
1262 if (ext4_handle_valid(handle
))
1263 error
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1265 if (!ext4_handle_valid(handle
))
1266 error
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1268 ext4_handle_sync(handle
);
1269 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
1270 ea_bdebug(bh
, "refcount now=%d; releasing",
1271 le32_to_cpu(BHDR(bh
)->h_refcount
));
1274 ext4_std_error(inode
->i_sb
, error
);
1279 * Find the available free space for EAs. This also returns the total number of
1280 * bytes used by EA entries.
1282 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
1283 size_t *min_offs
, void *base
, int *total
)
1285 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1286 if (!last
->e_value_inum
&& last
->e_value_size
) {
1287 size_t offs
= le16_to_cpu(last
->e_value_offs
);
1288 if (offs
< *min_offs
)
1292 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
1294 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
1298 * Write the value of the EA in an inode.
1300 static int ext4_xattr_inode_write(handle_t
*handle
, struct inode
*ea_inode
,
1301 const void *buf
, int bufsize
)
1303 struct buffer_head
*bh
= NULL
;
1304 unsigned long block
= 0;
1305 int blocksize
= ea_inode
->i_sb
->s_blocksize
;
1306 int max_blocks
= (bufsize
+ blocksize
- 1) >> ea_inode
->i_blkbits
;
1307 int csize
, wsize
= 0;
1312 while (ret
>= 0 && ret
< max_blocks
) {
1313 struct ext4_map_blocks map
;
1314 map
.m_lblk
= block
+= ret
;
1315 map
.m_len
= max_blocks
-= ret
;
1317 ret
= ext4_map_blocks(handle
, ea_inode
, &map
,
1318 EXT4_GET_BLOCKS_CREATE
);
1320 ext4_mark_inode_dirty(handle
, ea_inode
);
1321 if (ret
== -ENOSPC
&&
1322 ext4_should_retry_alloc(ea_inode
->i_sb
, &retries
)) {
1334 while (wsize
< bufsize
) {
1337 csize
= (bufsize
- wsize
) > blocksize
? blocksize
:
1339 bh
= ext4_getblk(handle
, ea_inode
, block
, 0);
1342 ret
= ext4_journal_get_write_access(handle
, bh
);
1346 memcpy(bh
->b_data
, buf
, csize
);
1347 set_buffer_uptodate(bh
);
1348 ext4_handle_dirty_metadata(handle
, ea_inode
, bh
);
1355 inode_lock(ea_inode
);
1356 i_size_write(ea_inode
, wsize
);
1357 ext4_update_i_disksize(ea_inode
, wsize
);
1358 inode_unlock(ea_inode
);
1360 ext4_mark_inode_dirty(handle
, ea_inode
);
1369 * Create an inode to store the value of a large EA.
1371 static struct inode
*ext4_xattr_inode_create(handle_t
*handle
,
1372 struct inode
*inode
, u32 hash
)
1374 struct inode
*ea_inode
= NULL
;
1375 uid_t owner
[2] = { i_uid_read(inode
), i_gid_read(inode
) };
1379 * Let the next inode be the goal, so we try and allocate the EA inode
1380 * in the same group, or nearby one.
1382 ea_inode
= ext4_new_inode(handle
, inode
->i_sb
->s_root
->d_inode
,
1383 S_IFREG
| 0600, NULL
, inode
->i_ino
+ 1, owner
,
1385 if (!IS_ERR(ea_inode
)) {
1386 ea_inode
->i_op
= &ext4_file_inode_operations
;
1387 ea_inode
->i_fop
= &ext4_file_operations
;
1388 ext4_set_aops(ea_inode
);
1389 ext4_xattr_inode_set_class(ea_inode
);
1390 unlock_new_inode(ea_inode
);
1391 ext4_xattr_inode_set_ref(ea_inode
, 1);
1392 ext4_xattr_inode_set_hash(ea_inode
, hash
);
1393 err
= ext4_mark_inode_dirty(handle
, ea_inode
);
1395 err
= ext4_inode_attach_jinode(ea_inode
);
1398 return ERR_PTR(err
);
1402 * Xattr inodes are shared therefore quota charging is performed
1403 * at a higher level.
1405 dquot_free_inode(ea_inode
);
1406 dquot_drop(ea_inode
);
1407 inode_lock(ea_inode
);
1408 ea_inode
->i_flags
|= S_NOQUOTA
;
1409 inode_unlock(ea_inode
);
1415 static struct inode
*
1416 ext4_xattr_inode_cache_find(struct inode
*inode
, const void *value
,
1417 size_t value_len
, u32 hash
)
1419 struct inode
*ea_inode
;
1420 struct mb_cache_entry
*ce
;
1421 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(inode
);
1424 if (!ea_inode_cache
)
1427 ce
= mb_cache_entry_find_first(ea_inode_cache
, hash
);
1431 ea_data
= ext4_kvmalloc(value_len
, GFP_NOFS
);
1433 mb_cache_entry_put(ea_inode_cache
, ce
);
1438 ea_inode
= ext4_iget(inode
->i_sb
, ce
->e_value
);
1439 if (!IS_ERR(ea_inode
) &&
1440 !is_bad_inode(ea_inode
) &&
1441 (EXT4_I(ea_inode
)->i_flags
& EXT4_EA_INODE_FL
) &&
1442 i_size_read(ea_inode
) == value_len
&&
1443 !ext4_xattr_inode_read(ea_inode
, ea_data
, value_len
) &&
1444 !ext4_xattr_inode_verify_hashes(ea_inode
, NULL
, ea_data
,
1446 !memcmp(value
, ea_data
, value_len
)) {
1447 mb_cache_entry_touch(ea_inode_cache
, ce
);
1448 mb_cache_entry_put(ea_inode_cache
, ce
);
1453 if (!IS_ERR(ea_inode
))
1455 ce
= mb_cache_entry_find_next(ea_inode_cache
, ce
);
1462 * Add value of the EA in an inode.
1464 static int ext4_xattr_inode_lookup_create(handle_t
*handle
, struct inode
*inode
,
1465 const void *value
, size_t value_len
,
1466 struct inode
**ret_inode
)
1468 struct inode
*ea_inode
;
1472 hash
= ext4_xattr_inode_hash(EXT4_SB(inode
->i_sb
), value
, value_len
);
1473 ea_inode
= ext4_xattr_inode_cache_find(inode
, value
, value_len
, hash
);
1475 err
= ext4_xattr_inode_inc_ref(handle
, ea_inode
);
1481 *ret_inode
= ea_inode
;
1485 /* Create an inode for the EA value */
1486 ea_inode
= ext4_xattr_inode_create(handle
, inode
, hash
);
1487 if (IS_ERR(ea_inode
))
1488 return PTR_ERR(ea_inode
);
1490 err
= ext4_xattr_inode_write(handle
, ea_inode
, value
, value_len
);
1492 ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1497 if (EA_INODE_CACHE(inode
))
1498 mb_cache_entry_create(EA_INODE_CACHE(inode
), GFP_NOFS
, hash
,
1499 ea_inode
->i_ino
, true /* reusable */);
1501 *ret_inode
= ea_inode
;
1506 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1507 * feature is enabled.
1509 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1511 static int ext4_xattr_set_entry(struct ext4_xattr_info
*i
,
1512 struct ext4_xattr_search
*s
,
1513 handle_t
*handle
, struct inode
*inode
,
1516 struct ext4_xattr_entry
*last
;
1517 struct ext4_xattr_entry
*here
= s
->here
;
1518 size_t min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
1519 int in_inode
= i
->in_inode
;
1520 struct inode
*old_ea_inode
= NULL
;
1521 struct inode
*new_ea_inode
= NULL
;
1522 size_t old_size
, new_size
;
1525 /* Space used by old and new values. */
1526 old_size
= (!s
->not_found
&& !here
->e_value_inum
) ?
1527 EXT4_XATTR_SIZE(le32_to_cpu(here
->e_value_size
)) : 0;
1528 new_size
= (i
->value
&& !in_inode
) ? EXT4_XATTR_SIZE(i
->value_len
) : 0;
1531 * Optimization for the simple case when old and new values have the
1532 * same padded sizes. Not applicable if external inodes are involved.
1534 if (new_size
&& new_size
== old_size
) {
1535 size_t offs
= le16_to_cpu(here
->e_value_offs
);
1536 void *val
= s
->base
+ offs
;
1538 here
->e_value_size
= cpu_to_le32(i
->value_len
);
1539 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
1540 memset(val
, 0, new_size
);
1542 memcpy(val
, i
->value
, i
->value_len
);
1543 /* Clear padding bytes. */
1544 memset(val
+ i
->value_len
, 0, new_size
- i
->value_len
);
1549 /* Compute min_offs and last. */
1551 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1552 if (!last
->e_value_inum
&& last
->e_value_size
) {
1553 size_t offs
= le16_to_cpu(last
->e_value_offs
);
1554 if (offs
< min_offs
)
1559 /* Check whether we have enough space. */
1563 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
1565 free
+= EXT4_XATTR_LEN(name_len
) + old_size
;
1567 if (free
< EXT4_XATTR_LEN(name_len
) + new_size
) {
1573 * If storing the value in an external inode is an option,
1574 * reserve space for xattr entries/names in the external
1575 * attribute block so that a long value does not occupy the
1576 * whole space and prevent futher entries being added.
1578 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
1579 new_size
&& is_block
&&
1580 (min_offs
+ old_size
- new_size
) <
1581 EXT4_XATTR_BLOCK_RESERVE(inode
)) {
1588 * Getting access to old and new ea inodes is subject to failures.
1589 * Finish that work before doing any modifications to the xattr data.
1591 if (!s
->not_found
&& here
->e_value_inum
) {
1592 ret
= ext4_xattr_inode_iget(inode
,
1593 le32_to_cpu(here
->e_value_inum
),
1596 old_ea_inode
= NULL
;
1600 if (i
->value
&& in_inode
) {
1601 WARN_ON_ONCE(!i
->value_len
);
1603 ret
= ext4_xattr_inode_alloc_quota(inode
, i
->value_len
);
1607 ret
= ext4_xattr_inode_lookup_create(handle
, inode
, i
->value
,
1611 new_ea_inode
= NULL
;
1612 ext4_xattr_inode_free_quota(inode
, i
->value_len
);
1618 /* We are ready to release ref count on the old_ea_inode. */
1619 ret
= ext4_xattr_inode_dec_ref(handle
, old_ea_inode
);
1621 /* Release newly required ref count on new_ea_inode. */
1625 err
= ext4_xattr_inode_dec_ref(handle
,
1628 ext4_warning_inode(new_ea_inode
,
1629 "dec ref new_ea_inode err=%d",
1631 ext4_xattr_inode_free_quota(inode
,
1637 ext4_xattr_inode_free_quota(inode
,
1638 le32_to_cpu(here
->e_value_size
));
1641 /* No failures allowed past this point. */
1643 if (!s
->not_found
&& here
->e_value_offs
) {
1644 /* Remove the old value. */
1645 void *first_val
= s
->base
+ min_offs
;
1646 size_t offs
= le16_to_cpu(here
->e_value_offs
);
1647 void *val
= s
->base
+ offs
;
1649 memmove(first_val
+ old_size
, first_val
, val
- first_val
);
1650 memset(first_val
, 0, old_size
);
1651 min_offs
+= old_size
;
1653 /* Adjust all value offsets. */
1655 while (!IS_LAST_ENTRY(last
)) {
1656 size_t o
= le16_to_cpu(last
->e_value_offs
);
1658 if (!last
->e_value_inum
&&
1659 last
->e_value_size
&& o
< offs
)
1660 last
->e_value_offs
= cpu_to_le16(o
+ old_size
);
1661 last
= EXT4_XATTR_NEXT(last
);
1666 /* Remove old name. */
1667 size_t size
= EXT4_XATTR_LEN(name_len
);
1669 last
= ENTRY((void *)last
- size
);
1670 memmove(here
, (void *)here
+ size
,
1671 (void *)last
- (void *)here
+ sizeof(__u32
));
1672 memset(last
, 0, size
);
1673 } else if (s
->not_found
) {
1674 /* Insert new name. */
1675 size_t size
= EXT4_XATTR_LEN(name_len
);
1676 size_t rest
= (void *)last
- (void *)here
+ sizeof(__u32
);
1678 memmove((void *)here
+ size
, here
, rest
);
1679 memset(here
, 0, size
);
1680 here
->e_name_index
= i
->name_index
;
1681 here
->e_name_len
= name_len
;
1682 memcpy(here
->e_name
, i
->name
, name_len
);
1684 /* This is an update, reset value info. */
1685 here
->e_value_inum
= 0;
1686 here
->e_value_offs
= 0;
1687 here
->e_value_size
= 0;
1691 /* Insert new value. */
1693 here
->e_value_inum
= cpu_to_le32(new_ea_inode
->i_ino
);
1694 } else if (i
->value_len
) {
1695 void *val
= s
->base
+ min_offs
- new_size
;
1697 here
->e_value_offs
= cpu_to_le16(min_offs
- new_size
);
1698 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
1699 memset(val
, 0, new_size
);
1701 memcpy(val
, i
->value
, i
->value_len
);
1702 /* Clear padding bytes. */
1703 memset(val
+ i
->value_len
, 0,
1704 new_size
- i
->value_len
);
1707 here
->e_value_size
= cpu_to_le32(i
->value_len
);
1713 /* Entry hash calculation. */
1718 * Feed crc32c hash instead of the raw value for entry
1719 * hash calculation. This is to avoid walking
1720 * potentially long value buffer again.
1722 crc32c_hash
= cpu_to_le32(
1723 ext4_xattr_inode_get_hash(new_ea_inode
));
1724 hash
= ext4_xattr_hash_entry(here
->e_name
,
1727 } else if (is_block
) {
1728 __le32
*value
= s
->base
+ min_offs
- new_size
;
1730 hash
= ext4_xattr_hash_entry(here
->e_name
,
1731 here
->e_name_len
, value
,
1734 here
->e_hash
= hash
;
1738 ext4_xattr_rehash((struct ext4_xattr_header
*)s
->base
);
1747 struct ext4_xattr_block_find
{
1748 struct ext4_xattr_search s
;
1749 struct buffer_head
*bh
;
1753 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
1754 struct ext4_xattr_block_find
*bs
)
1756 struct super_block
*sb
= inode
->i_sb
;
1759 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
1760 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
1762 if (EXT4_I(inode
)->i_file_acl
) {
1763 /* The inode already has an extended attribute block. */
1764 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
1768 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
1769 atomic_read(&(bs
->bh
->b_count
)),
1770 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
1771 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
1772 EXT4_ERROR_INODE(inode
, "bad block %llu",
1773 EXT4_I(inode
)->i_file_acl
);
1774 error
= -EFSCORRUPTED
;
1777 /* Find the named attribute. */
1778 bs
->s
.base
= BHDR(bs
->bh
);
1779 bs
->s
.first
= BFIRST(bs
->bh
);
1780 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
1781 bs
->s
.here
= bs
->s
.first
;
1782 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
1784 if (error
&& error
!= -ENODATA
)
1786 bs
->s
.not_found
= error
;
1795 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
1796 struct ext4_xattr_info
*i
,
1797 struct ext4_xattr_block_find
*bs
)
1799 struct super_block
*sb
= inode
->i_sb
;
1800 struct buffer_head
*new_bh
= NULL
;
1801 struct ext4_xattr_search s_copy
= bs
->s
;
1802 struct ext4_xattr_search
*s
= &s_copy
;
1803 struct mb_cache_entry
*ce
= NULL
;
1805 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
1806 struct inode
*ea_inode
= NULL
;
1807 size_t old_ea_inode_size
= 0;
1809 #define header(x) ((struct ext4_xattr_header *)(x))
1812 BUFFER_TRACE(bs
->bh
, "get_write_access");
1813 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
1816 lock_buffer(bs
->bh
);
1818 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
1819 __u32 hash
= le32_to_cpu(BHDR(bs
->bh
)->h_hash
);
1822 * This must happen under buffer lock for
1823 * ext4_xattr_block_set() to reliably detect modified
1827 mb_cache_entry_delete(ea_block_cache
, hash
,
1829 ea_bdebug(bs
->bh
, "modifying in-place");
1830 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
,
1831 true /* is_block */);
1832 ext4_xattr_block_csum_set(inode
, bs
->bh
);
1833 unlock_buffer(bs
->bh
);
1834 if (error
== -EFSCORRUPTED
)
1837 error
= ext4_handle_dirty_metadata(handle
,
1844 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
1846 unlock_buffer(bs
->bh
);
1847 ea_bdebug(bs
->bh
, "cloning");
1848 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
1850 if (s
->base
== NULL
)
1852 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
1853 s
->first
= ENTRY(header(s
->base
)+1);
1854 header(s
->base
)->h_refcount
= cpu_to_le32(1);
1855 s
->here
= ENTRY(s
->base
+ offset
);
1856 s
->end
= s
->base
+ bs
->bh
->b_size
;
1859 * If existing entry points to an xattr inode, we need
1860 * to prevent ext4_xattr_set_entry() from decrementing
1861 * ref count on it because the reference belongs to the
1862 * original block. In this case, make the entry look
1863 * like it has an empty value.
1865 if (!s
->not_found
&& s
->here
->e_value_inum
) {
1867 * Defer quota free call for previous inode
1868 * until success is guaranteed.
1870 old_ea_inode_size
= le32_to_cpu(
1871 s
->here
->e_value_size
);
1872 s
->here
->e_value_inum
= 0;
1873 s
->here
->e_value_size
= 0;
1877 /* Allocate a buffer where we construct the new block. */
1878 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
1879 /* assert(header == s->base) */
1881 if (s
->base
== NULL
)
1883 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1884 header(s
->base
)->h_blocks
= cpu_to_le32(1);
1885 header(s
->base
)->h_refcount
= cpu_to_le32(1);
1886 s
->first
= ENTRY(header(s
->base
)+1);
1887 s
->here
= ENTRY(header(s
->base
)+1);
1888 s
->end
= s
->base
+ sb
->s_blocksize
;
1891 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
, true /* is_block */);
1892 if (error
== -EFSCORRUPTED
)
1897 if (i
->value
&& s
->here
->e_value_inum
) {
1898 unsigned int ea_ino
;
1901 * A ref count on ea_inode has been taken as part of the call to
1902 * ext4_xattr_set_entry() above. We would like to drop this
1903 * extra ref but we have to wait until the xattr block is
1904 * initialized and has its own ref count on the ea_inode.
1906 ea_ino
= le32_to_cpu(s
->here
->e_value_inum
);
1907 error
= ext4_xattr_inode_iget(inode
, ea_ino
, &ea_inode
);
1915 if (!IS_LAST_ENTRY(s
->first
)) {
1916 new_bh
= ext4_xattr_block_cache_find(inode
, header(s
->base
),
1919 /* We found an identical block in the cache. */
1920 if (new_bh
== bs
->bh
)
1921 ea_bdebug(new_bh
, "keeping");
1925 WARN_ON_ONCE(dquot_initialize_needed(inode
));
1927 /* The old block is released after updating
1929 error
= dquot_alloc_block(inode
,
1930 EXT4_C2B(EXT4_SB(sb
), 1));
1933 BUFFER_TRACE(new_bh
, "get_write_access");
1934 error
= ext4_journal_get_write_access(handle
,
1938 lock_buffer(new_bh
);
1940 * We have to be careful about races with
1941 * freeing, rehashing or adding references to
1942 * xattr block. Once we hold buffer lock xattr
1943 * block's state is stable so we can check
1944 * whether the block got freed / rehashed or
1945 * not. Since we unhash mbcache entry under
1946 * buffer lock when freeing / rehashing xattr
1947 * block, checking whether entry is still
1948 * hashed is reliable. Same rules hold for
1949 * e_reusable handling.
1951 if (hlist_bl_unhashed(&ce
->e_hash_list
) ||
1954 * Undo everything and check mbcache
1957 unlock_buffer(new_bh
);
1958 dquot_free_block(inode
,
1959 EXT4_C2B(EXT4_SB(sb
),
1962 mb_cache_entry_put(ea_block_cache
, ce
);
1967 ref
= le32_to_cpu(BHDR(new_bh
)->h_refcount
) + 1;
1968 BHDR(new_bh
)->h_refcount
= cpu_to_le32(ref
);
1969 if (ref
>= EXT4_XATTR_REFCOUNT_MAX
)
1971 ea_bdebug(new_bh
, "reusing; refcount now=%d",
1973 ext4_xattr_block_csum_set(inode
, new_bh
);
1974 unlock_buffer(new_bh
);
1975 error
= ext4_handle_dirty_metadata(handle
,
1981 mb_cache_entry_touch(ea_block_cache
, ce
);
1982 mb_cache_entry_put(ea_block_cache
, ce
);
1984 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
1985 /* We were modifying this block in-place. */
1986 ea_bdebug(bs
->bh
, "keeping this block");
1987 ext4_xattr_block_cache_insert(ea_block_cache
, bs
->bh
);
1991 /* We need to allocate a new block */
1992 ext4_fsblk_t goal
, block
;
1994 WARN_ON_ONCE(dquot_initialize_needed(inode
));
1996 goal
= ext4_group_first_block_no(sb
,
1997 EXT4_I(inode
)->i_block_group
);
1999 /* non-extent files can't have physical blocks past 2^32 */
2000 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
2001 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
2003 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
2008 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
2009 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
2011 ea_idebug(inode
, "creating block %llu",
2012 (unsigned long long)block
);
2014 new_bh
= sb_getblk(sb
, block
);
2015 if (unlikely(!new_bh
)) {
2018 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
2019 EXT4_FREE_BLOCKS_METADATA
);
2022 error
= ext4_xattr_inode_inc_ref_all(handle
, inode
,
2023 ENTRY(header(s
->base
)+1));
2027 /* Drop the extra ref on ea_inode. */
2028 error
= ext4_xattr_inode_dec_ref(handle
,
2031 ext4_warning_inode(ea_inode
,
2038 lock_buffer(new_bh
);
2039 error
= ext4_journal_get_create_access(handle
, new_bh
);
2041 unlock_buffer(new_bh
);
2045 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
2046 ext4_xattr_block_csum_set(inode
, new_bh
);
2047 set_buffer_uptodate(new_bh
);
2048 unlock_buffer(new_bh
);
2049 ext4_xattr_block_cache_insert(ea_block_cache
, new_bh
);
2050 error
= ext4_handle_dirty_metadata(handle
, inode
,
2057 if (old_ea_inode_size
)
2058 ext4_xattr_inode_free_quota(inode
, old_ea_inode_size
);
2060 /* Update the inode. */
2061 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
2063 /* Drop the previous xattr block. */
2064 if (bs
->bh
&& bs
->bh
!= new_bh
) {
2065 struct ext4_xattr_inode_array
*ea_inode_array
= NULL
;
2067 ext4_xattr_release_block(handle
, inode
, bs
->bh
,
2069 0 /* extra_credits */);
2070 ext4_xattr_inode_array_free(ea_inode_array
);
2078 error2
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
2080 ext4_warning_inode(ea_inode
, "dec ref error=%d",
2083 /* If there was an error, revert the quota charge. */
2085 ext4_xattr_inode_free_quota(inode
,
2086 i_size_read(ea_inode
));
2090 mb_cache_entry_put(ea_block_cache
, ce
);
2092 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
2098 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
2102 EXT4_ERROR_INODE(inode
, "bad block %llu",
2103 EXT4_I(inode
)->i_file_acl
);
2109 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
2110 struct ext4_xattr_ibody_find
*is
)
2112 struct ext4_xattr_ibody_header
*header
;
2113 struct ext4_inode
*raw_inode
;
2116 if (EXT4_I(inode
)->i_extra_isize
== 0)
2118 raw_inode
= ext4_raw_inode(&is
->iloc
);
2119 header
= IHDR(inode
, raw_inode
);
2120 is
->s
.base
= is
->s
.first
= IFIRST(header
);
2121 is
->s
.here
= is
->s
.first
;
2122 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
2123 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
2124 error
= xattr_check_inode(inode
, header
, is
->s
.end
);
2127 /* Find the named attribute. */
2128 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
2130 if (error
&& error
!= -ENODATA
)
2132 is
->s
.not_found
= error
;
2137 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
2138 struct ext4_xattr_info
*i
,
2139 struct ext4_xattr_ibody_find
*is
)
2141 struct ext4_xattr_ibody_header
*header
;
2142 struct ext4_xattr_search
*s
= &is
->s
;
2145 if (EXT4_I(inode
)->i_extra_isize
== 0)
2147 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
, false /* is_block */);
2149 if (error
== -ENOSPC
&&
2150 ext4_has_inline_data(inode
)) {
2151 error
= ext4_try_to_evict_inline_data(handle
, inode
,
2152 EXT4_XATTR_LEN(strlen(i
->name
) +
2153 EXT4_XATTR_SIZE(i
->value_len
)));
2156 error
= ext4_xattr_ibody_find(inode
, i
, is
);
2159 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
,
2160 false /* is_block */);
2165 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
2166 if (!IS_LAST_ENTRY(s
->first
)) {
2167 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
2168 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
2170 header
->h_magic
= cpu_to_le32(0);
2171 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
2176 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
2177 struct ext4_xattr_info
*i
,
2178 struct ext4_xattr_ibody_find
*is
)
2180 struct ext4_xattr_ibody_header
*header
;
2181 struct ext4_xattr_search
*s
= &is
->s
;
2184 if (EXT4_I(inode
)->i_extra_isize
== 0)
2186 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
, false /* is_block */);
2189 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
2190 if (!IS_LAST_ENTRY(s
->first
)) {
2191 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
2192 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
2194 header
->h_magic
= cpu_to_le32(0);
2195 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
2200 static int ext4_xattr_value_same(struct ext4_xattr_search
*s
,
2201 struct ext4_xattr_info
*i
)
2205 /* When e_value_inum is set the value is stored externally. */
2206 if (s
->here
->e_value_inum
)
2208 if (le32_to_cpu(s
->here
->e_value_size
) != i
->value_len
)
2210 value
= ((void *)s
->base
) + le16_to_cpu(s
->here
->e_value_offs
);
2211 return !memcmp(value
, i
->value
, i
->value_len
);
2214 static struct buffer_head
*ext4_xattr_get_block(struct inode
*inode
)
2216 struct buffer_head
*bh
;
2219 if (!EXT4_I(inode
)->i_file_acl
)
2221 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2223 return ERR_PTR(-EIO
);
2224 error
= ext4_xattr_check_block(inode
, bh
);
2226 return ERR_PTR(error
);
2231 * ext4_xattr_set_handle()
2233 * Create, replace or remove an extended attribute for this inode. Value
2234 * is NULL to remove an existing extended attribute, and non-NULL to
2235 * either replace an existing extended attribute, or create a new extended
2236 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2237 * specify that an extended attribute must exist and must not exist
2238 * previous to the call, respectively.
2240 * Returns 0, or a negative error number on failure.
2243 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
2244 const char *name
, const void *value
, size_t value_len
,
2247 struct ext4_xattr_info i
= {
2248 .name_index
= name_index
,
2251 .value_len
= value_len
,
2254 struct ext4_xattr_ibody_find is
= {
2255 .s
= { .not_found
= -ENODATA
, },
2257 struct ext4_xattr_block_find bs
= {
2258 .s
= { .not_found
= -ENODATA
, },
2265 if (strlen(name
) > 255)
2268 ext4_write_lock_xattr(inode
, &no_expand
);
2270 /* Check journal credits under write lock. */
2271 if (ext4_handle_valid(handle
)) {
2272 struct buffer_head
*bh
;
2275 bh
= ext4_xattr_get_block(inode
);
2277 error
= PTR_ERR(bh
);
2281 credits
= __ext4_xattr_set_credits(inode
->i_sb
, inode
, bh
,
2283 flags
& XATTR_CREATE
);
2286 if (!ext4_handle_has_enough_credits(handle
, credits
)) {
2292 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
2296 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
2297 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
2298 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
2299 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
2302 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
2306 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
2309 if (is
.s
.not_found
&& bs
.s
.not_found
) {
2311 if (flags
& XATTR_REPLACE
)
2318 if (flags
& XATTR_CREATE
)
2323 if (!is
.s
.not_found
)
2324 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
2325 else if (!bs
.s
.not_found
)
2326 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2329 /* Xattr value did not change? Save us some work and bail out */
2330 if (!is
.s
.not_found
&& ext4_xattr_value_same(&is
.s
, &i
))
2332 if (!bs
.s
.not_found
&& ext4_xattr_value_same(&bs
.s
, &i
))
2335 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2336 (EXT4_XATTR_SIZE(i
.value_len
) >
2337 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode
->i_sb
->s_blocksize
)))
2340 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
2341 if (!error
&& !bs
.s
.not_found
) {
2343 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2344 } else if (error
== -ENOSPC
) {
2345 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
2346 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
2350 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2351 if (!error
&& !is
.s
.not_found
) {
2353 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
2355 } else if (error
== -ENOSPC
) {
2357 * Xattr does not fit in the block, store at
2358 * external inode if possible.
2360 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2369 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
2370 inode
->i_ctime
= current_time(inode
);
2373 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
2375 * The bh is consumed by ext4_mark_iloc_dirty, even with
2380 ext4_handle_sync(handle
);
2386 ext4_write_unlock_xattr(inode
, &no_expand
);
2390 int ext4_xattr_set_credits(struct inode
*inode
, size_t value_len
,
2391 bool is_create
, int *credits
)
2393 struct buffer_head
*bh
;
2398 if (!EXT4_SB(inode
->i_sb
)->s_journal
)
2401 down_read(&EXT4_I(inode
)->xattr_sem
);
2403 bh
= ext4_xattr_get_block(inode
);
2407 *credits
= __ext4_xattr_set_credits(inode
->i_sb
, inode
, bh
,
2408 value_len
, is_create
);
2413 up_read(&EXT4_I(inode
)->xattr_sem
);
2420 * Like ext4_xattr_set_handle, but start from an inode. This extended
2421 * attribute modification is a filesystem transaction by itself.
2423 * Returns 0, or a negative error number on failure.
2426 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
2427 const void *value
, size_t value_len
, int flags
)
2430 struct super_block
*sb
= inode
->i_sb
;
2431 int error
, retries
= 0;
2434 error
= dquot_initialize(inode
);
2439 error
= ext4_xattr_set_credits(inode
, value_len
, flags
& XATTR_CREATE
,
2444 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
2445 if (IS_ERR(handle
)) {
2446 error
= PTR_ERR(handle
);
2450 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
2451 value
, value_len
, flags
);
2452 error2
= ext4_journal_stop(handle
);
2453 if (error
== -ENOSPC
&&
2454 ext4_should_retry_alloc(sb
, &retries
))
2464 * Shift the EA entries in the inode to create space for the increased
2467 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
2468 int value_offs_shift
, void *to
,
2469 void *from
, size_t n
)
2471 struct ext4_xattr_entry
*last
= entry
;
2474 /* We always shift xattr headers further thus offsets get lower */
2475 BUG_ON(value_offs_shift
> 0);
2477 /* Adjust the value offsets of the entries */
2478 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
2479 if (!last
->e_value_inum
&& last
->e_value_size
) {
2480 new_offs
= le16_to_cpu(last
->e_value_offs
) +
2482 last
->e_value_offs
= cpu_to_le16(new_offs
);
2485 /* Shift the entries by n bytes */
2486 memmove(to
, from
, n
);
2490 * Move xattr pointed to by 'entry' from inode into external xattr block
2492 static int ext4_xattr_move_to_block(handle_t
*handle
, struct inode
*inode
,
2493 struct ext4_inode
*raw_inode
,
2494 struct ext4_xattr_entry
*entry
)
2496 struct ext4_xattr_ibody_find
*is
= NULL
;
2497 struct ext4_xattr_block_find
*bs
= NULL
;
2498 char *buffer
= NULL
, *b_entry_name
= NULL
;
2499 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
2500 struct ext4_xattr_info i
= {
2503 .name_index
= entry
->e_name_index
,
2504 .in_inode
= !!entry
->e_value_inum
,
2506 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
2509 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
2510 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
2511 buffer
= kmalloc(value_size
, GFP_NOFS
);
2512 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
2513 if (!is
|| !bs
|| !buffer
|| !b_entry_name
) {
2518 is
->s
.not_found
= -ENODATA
;
2519 bs
->s
.not_found
= -ENODATA
;
2523 /* Save the entry name and the entry value */
2524 if (entry
->e_value_inum
) {
2525 error
= ext4_xattr_inode_get(inode
, entry
, buffer
, value_size
);
2529 size_t value_offs
= le16_to_cpu(entry
->e_value_offs
);
2530 memcpy(buffer
, (void *)IFIRST(header
) + value_offs
, value_size
);
2533 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
2534 b_entry_name
[entry
->e_name_len
] = '\0';
2535 i
.name
= b_entry_name
;
2537 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
2541 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
2545 /* Remove the chosen entry from the inode */
2546 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
2551 i
.value_len
= value_size
;
2552 error
= ext4_xattr_block_find(inode
, &i
, bs
);
2556 /* Add entry which was removed from the inode into the block */
2557 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
2562 kfree(b_entry_name
);
2565 brelse(is
->iloc
.bh
);
2572 static int ext4_xattr_make_inode_space(handle_t
*handle
, struct inode
*inode
,
2573 struct ext4_inode
*raw_inode
,
2574 int isize_diff
, size_t ifree
,
2575 size_t bfree
, int *total_ino
)
2577 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
2578 struct ext4_xattr_entry
*small_entry
;
2579 struct ext4_xattr_entry
*entry
;
2580 struct ext4_xattr_entry
*last
;
2581 unsigned int entry_size
; /* EA entry size */
2582 unsigned int total_size
; /* EA entry size + value size */
2583 unsigned int min_total_size
;
2586 while (isize_diff
> ifree
) {
2589 min_total_size
= ~0U;
2590 last
= IFIRST(header
);
2591 /* Find the entry best suited to be pushed into EA block */
2592 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
2593 total_size
= EXT4_XATTR_LEN(last
->e_name_len
);
2594 if (!last
->e_value_inum
)
2595 total_size
+= EXT4_XATTR_SIZE(
2596 le32_to_cpu(last
->e_value_size
));
2597 if (total_size
<= bfree
&&
2598 total_size
< min_total_size
) {
2599 if (total_size
+ ifree
< isize_diff
) {
2603 min_total_size
= total_size
;
2608 if (entry
== NULL
) {
2609 if (small_entry
== NULL
)
2611 entry
= small_entry
;
2614 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
2615 total_size
= entry_size
;
2616 if (!entry
->e_value_inum
)
2617 total_size
+= EXT4_XATTR_SIZE(
2618 le32_to_cpu(entry
->e_value_size
));
2619 error
= ext4_xattr_move_to_block(handle
, inode
, raw_inode
,
2624 *total_ino
-= entry_size
;
2625 ifree
+= total_size
;
2626 bfree
-= total_size
;
2633 * Expand an inode by new_extra_isize bytes when EAs are present.
2634 * Returns 0 on success or negative error number on failure.
2636 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
2637 struct ext4_inode
*raw_inode
, handle_t
*handle
)
2639 struct ext4_xattr_ibody_header
*header
;
2640 struct buffer_head
*bh
= NULL
;
2642 size_t ifree
, bfree
;
2645 int error
= 0, tried_min_extra_isize
= 0;
2646 int s_min_extra_isize
= le16_to_cpu(EXT4_SB(inode
->i_sb
)->s_es
->s_min_extra_isize
);
2647 int isize_diff
; /* How much do we need to grow i_extra_isize */
2650 if (ext4_write_trylock_xattr(inode
, &no_expand
) == 0)
2654 isize_diff
= new_extra_isize
- EXT4_I(inode
)->i_extra_isize
;
2655 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
2658 header
= IHDR(inode
, raw_inode
);
2661 * Check if enough free space is available in the inode to shift the
2662 * entries ahead by new_extra_isize.
2665 base
= IFIRST(header
);
2666 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
2667 min_offs
= end
- base
;
2668 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
2670 error
= xattr_check_inode(inode
, header
, end
);
2674 ifree
= ext4_xattr_free_space(base
, &min_offs
, base
, &total_ino
);
2675 if (ifree
>= isize_diff
)
2679 * Enough free space isn't available in the inode, check if
2680 * EA block can hold new_extra_isize bytes.
2682 if (EXT4_I(inode
)->i_file_acl
) {
2683 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2687 if (ext4_xattr_check_block(inode
, bh
)) {
2688 EXT4_ERROR_INODE(inode
, "bad block %llu",
2689 EXT4_I(inode
)->i_file_acl
);
2690 error
= -EFSCORRUPTED
;
2694 end
= bh
->b_data
+ bh
->b_size
;
2695 min_offs
= end
- base
;
2696 bfree
= ext4_xattr_free_space(BFIRST(bh
), &min_offs
, base
,
2698 if (bfree
+ ifree
< isize_diff
) {
2699 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
2700 tried_min_extra_isize
++;
2701 new_extra_isize
= s_min_extra_isize
;
2709 bfree
= inode
->i_sb
->s_blocksize
;
2712 error
= ext4_xattr_make_inode_space(handle
, inode
, raw_inode
,
2713 isize_diff
, ifree
, bfree
,
2716 if (error
== -ENOSPC
&& !tried_min_extra_isize
&&
2717 s_min_extra_isize
) {
2718 tried_min_extra_isize
++;
2719 new_extra_isize
= s_min_extra_isize
;
2726 /* Adjust the offsets and shift the remaining entries ahead */
2727 ext4_xattr_shift_entries(IFIRST(header
), EXT4_I(inode
)->i_extra_isize
2728 - new_extra_isize
, (void *)raw_inode
+
2729 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
2730 (void *)header
, total_ino
);
2731 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
2734 ext4_write_unlock_xattr(inode
, &no_expand
);
2740 * Inode size expansion failed; don't try again
2743 ext4_write_unlock_xattr(inode
, &no_expand
);
2747 #define EIA_INCR 16 /* must be 2^n */
2748 #define EIA_MASK (EIA_INCR - 1)
2750 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2751 * If @ea_inode_array is new or full it will be grown and the old
2752 * contents copied over.
2755 ext4_expand_inode_array(struct ext4_xattr_inode_array
**ea_inode_array
,
2756 struct inode
*inode
)
2758 if (*ea_inode_array
== NULL
) {
2760 * Start with 15 inodes, so it fits into a power-of-two size.
2761 * If *ea_inode_array is NULL, this is essentially offsetof()
2764 kmalloc(offsetof(struct ext4_xattr_inode_array
,
2767 if (*ea_inode_array
== NULL
)
2769 (*ea_inode_array
)->count
= 0;
2770 } else if (((*ea_inode_array
)->count
& EIA_MASK
) == EIA_MASK
) {
2771 /* expand the array once all 15 + n * 16 slots are full */
2772 struct ext4_xattr_inode_array
*new_array
= NULL
;
2773 int count
= (*ea_inode_array
)->count
;
2775 /* if new_array is NULL, this is essentially offsetof() */
2776 new_array
= kmalloc(
2777 offsetof(struct ext4_xattr_inode_array
,
2778 inodes
[count
+ EIA_INCR
]),
2780 if (new_array
== NULL
)
2782 memcpy(new_array
, *ea_inode_array
,
2783 offsetof(struct ext4_xattr_inode_array
, inodes
[count
]));
2784 kfree(*ea_inode_array
);
2785 *ea_inode_array
= new_array
;
2787 (*ea_inode_array
)->inodes
[(*ea_inode_array
)->count
++] = inode
;
2792 * ext4_xattr_delete_inode()
2794 * Free extended attribute resources associated with this inode. Traverse
2795 * all entries and decrement reference on any xattr inodes associated with this
2796 * inode. This is called immediately before an inode is freed. We have exclusive
2797 * access to the inode. If an orphan inode is deleted it will also release its
2798 * references on xattr block and xattr inodes.
2800 int ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
,
2801 struct ext4_xattr_inode_array
**ea_inode_array
,
2804 struct buffer_head
*bh
= NULL
;
2805 struct ext4_xattr_ibody_header
*header
;
2806 struct ext4_iloc iloc
= { .bh
= NULL
};
2807 struct ext4_xattr_entry
*entry
;
2810 error
= ext4_xattr_ensure_credits(handle
, inode
, extra_credits
,
2813 false /* block_csum */);
2815 EXT4_ERROR_INODE(inode
, "ensure credits (error %d)", error
);
2819 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2820 ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
2822 error
= ext4_get_inode_loc(inode
, &iloc
);
2824 EXT4_ERROR_INODE(inode
, "inode loc (error %d)", error
);
2828 error
= ext4_journal_get_write_access(handle
, iloc
.bh
);
2830 EXT4_ERROR_INODE(inode
, "write access (error %d)",
2835 header
= IHDR(inode
, ext4_raw_inode(&iloc
));
2836 if (header
->h_magic
== cpu_to_le32(EXT4_XATTR_MAGIC
))
2837 ext4_xattr_inode_dec_ref_all(handle
, inode
, iloc
.bh
,
2839 false /* block_csum */,
2842 false /* skip_quota */);
2845 if (EXT4_I(inode
)->i_file_acl
) {
2846 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2848 EXT4_ERROR_INODE(inode
, "block %llu read error",
2849 EXT4_I(inode
)->i_file_acl
);
2853 error
= ext4_xattr_check_block(inode
, bh
);
2855 EXT4_ERROR_INODE(inode
, "bad block %llu (error %d)",
2856 EXT4_I(inode
)->i_file_acl
, error
);
2860 if (ext4_has_feature_ea_inode(inode
->i_sb
)) {
2861 for (entry
= BFIRST(bh
); !IS_LAST_ENTRY(entry
);
2862 entry
= EXT4_XATTR_NEXT(entry
))
2863 if (entry
->e_value_inum
)
2864 ext4_xattr_inode_free_quota(inode
,
2865 le32_to_cpu(entry
->e_value_size
));
2869 ext4_xattr_release_block(handle
, inode
, bh
, ea_inode_array
,
2872 * Update i_file_acl value in the same transaction that releases
2875 EXT4_I(inode
)->i_file_acl
= 0;
2876 error
= ext4_mark_inode_dirty(handle
, inode
);
2878 EXT4_ERROR_INODE(inode
, "mark inode dirty (error %d)",
2890 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array
*ea_inode_array
)
2894 if (ea_inode_array
== NULL
)
2897 for (idx
= 0; idx
< ea_inode_array
->count
; ++idx
)
2898 iput(ea_inode_array
->inodes
[idx
]);
2899 kfree(ea_inode_array
);
2903 * ext4_xattr_block_cache_insert()
2905 * Create a new entry in the extended attribute block cache, and insert
2906 * it unless such an entry is already in the cache.
2908 * Returns 0, or a negative error number on failure.
2911 ext4_xattr_block_cache_insert(struct mb_cache
*ea_block_cache
,
2912 struct buffer_head
*bh
)
2914 struct ext4_xattr_header
*header
= BHDR(bh
);
2915 __u32 hash
= le32_to_cpu(header
->h_hash
);
2916 int reusable
= le32_to_cpu(header
->h_refcount
) <
2917 EXT4_XATTR_REFCOUNT_MAX
;
2920 if (!ea_block_cache
)
2922 error
= mb_cache_entry_create(ea_block_cache
, GFP_NOFS
, hash
,
2923 bh
->b_blocknr
, reusable
);
2925 if (error
== -EBUSY
)
2926 ea_bdebug(bh
, "already in cache");
2928 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
2934 * Compare two extended attribute blocks for equality.
2936 * Returns 0 if the blocks are equal, 1 if they differ, and
2937 * a negative error number on errors.
2940 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
2941 struct ext4_xattr_header
*header2
)
2943 struct ext4_xattr_entry
*entry1
, *entry2
;
2945 entry1
= ENTRY(header1
+1);
2946 entry2
= ENTRY(header2
+1);
2947 while (!IS_LAST_ENTRY(entry1
)) {
2948 if (IS_LAST_ENTRY(entry2
))
2950 if (entry1
->e_hash
!= entry2
->e_hash
||
2951 entry1
->e_name_index
!= entry2
->e_name_index
||
2952 entry1
->e_name_len
!= entry2
->e_name_len
||
2953 entry1
->e_value_size
!= entry2
->e_value_size
||
2954 entry1
->e_value_inum
!= entry2
->e_value_inum
||
2955 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
2957 if (!entry1
->e_value_inum
&&
2958 memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
2959 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
2960 le32_to_cpu(entry1
->e_value_size
)))
2963 entry1
= EXT4_XATTR_NEXT(entry1
);
2964 entry2
= EXT4_XATTR_NEXT(entry2
);
2966 if (!IS_LAST_ENTRY(entry2
))
2972 * ext4_xattr_block_cache_find()
2974 * Find an identical extended attribute block.
2976 * Returns a pointer to the block found, or NULL if such a block was
2977 * not found or an error occurred.
2979 static struct buffer_head
*
2980 ext4_xattr_block_cache_find(struct inode
*inode
,
2981 struct ext4_xattr_header
*header
,
2982 struct mb_cache_entry
**pce
)
2984 __u32 hash
= le32_to_cpu(header
->h_hash
);
2985 struct mb_cache_entry
*ce
;
2986 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
2988 if (!ea_block_cache
)
2990 if (!header
->h_hash
)
2991 return NULL
; /* never share */
2992 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
2993 ce
= mb_cache_entry_find_first(ea_block_cache
, hash
);
2995 struct buffer_head
*bh
;
2997 bh
= sb_bread(inode
->i_sb
, ce
->e_value
);
2999 EXT4_ERROR_INODE(inode
, "block %lu read error",
3000 (unsigned long)ce
->e_value
);
3001 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
3006 ce
= mb_cache_entry_find_next(ea_block_cache
, ce
);
3011 #define NAME_HASH_SHIFT 5
3012 #define VALUE_HASH_SHIFT 16
3015 * ext4_xattr_hash_entry()
3017 * Compute the hash of an extended attribute.
3019 static __le32
ext4_xattr_hash_entry(char *name
, size_t name_len
, __le32
*value
,
3024 while (name_len
--) {
3025 hash
= (hash
<< NAME_HASH_SHIFT
) ^
3026 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
3029 while (value_count
--) {
3030 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
3031 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
3032 le32_to_cpu(*value
++);
3034 return cpu_to_le32(hash
);
3037 #undef NAME_HASH_SHIFT
3038 #undef VALUE_HASH_SHIFT
3040 #define BLOCK_HASH_SHIFT 16
3043 * ext4_xattr_rehash()
3045 * Re-compute the extended attribute hash value after an entry has changed.
3047 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
)
3049 struct ext4_xattr_entry
*here
;
3052 here
= ENTRY(header
+1);
3053 while (!IS_LAST_ENTRY(here
)) {
3054 if (!here
->e_hash
) {
3055 /* Block is not shared if an entry's hash value == 0 */
3059 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
3060 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
3061 le32_to_cpu(here
->e_hash
);
3062 here
= EXT4_XATTR_NEXT(here
);
3064 header
->h_hash
= cpu_to_le32(hash
);
3067 #undef BLOCK_HASH_SHIFT
3069 #define HASH_BUCKET_BITS 10
3072 ext4_xattr_create_cache(void)
3074 return mb_cache_create(HASH_BUCKET_BITS
);
3077 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
3080 mb_cache_destroy(cache
);