1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/ext4/xattr.c
5 * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
7 * Fix by Harrison Xing <harrison@mountainviewdata.com>.
8 * Ext4 code with a lot of help from Eric Jarman <ejarman@acm.org>.
9 * Extended attributes for symlinks and special files added per
10 * suggestion of Luka Renko <luka.renko@hermes.si>.
11 * xattr consolidation Copyright (c) 2004 James Morris <jmorris@redhat.com>,
13 * ea-in-inode support by Alex Tomas <alex@clusterfs.com> aka bzzz
14 * and Andreas Gruenbacher <agruen@suse.de>.
18 * Extended attributes are stored directly in inodes (on file systems with
19 * inodes bigger than 128 bytes) and on additional disk blocks. The i_file_acl
20 * field contains the block number if an inode uses an additional block. All
21 * attributes must fit in the inode and one additional block. Blocks that
22 * contain the identical set of attributes may be shared among several inodes.
23 * Identical blocks are detected by keeping a cache of blocks that have
24 * recently been accessed.
26 * The attributes in inodes and on blocks have a different header; the entries
27 * are stored in the same format:
29 * +------------------+
32 * | entry 2 | | growing downwards
37 * | value 3 | | growing upwards
39 * +------------------+
41 * The header is followed by multiple entry descriptors. In disk blocks, the
42 * entry descriptors are kept sorted. In inodes, they are unsorted. The
43 * attribute values are aligned to the end of the block in no specific order.
47 * EXT4_I(inode)->i_file_acl is protected by EXT4_I(inode)->xattr_sem.
48 * EA blocks are only changed if they are exclusive to an inode, so
49 * holding xattr_sem also means that nothing but the EA block's reference
50 * count can change. Multiple writers to the same block are synchronized
54 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/mbcache.h>
58 #include <linux/quotaops.h>
59 #include <linux/iversion.h>
60 #include "ext4_jbd2.h"
65 #ifdef EXT4_XATTR_DEBUG
66 # define ea_idebug(inode, fmt, ...) \
67 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
68 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
69 # define ea_bdebug(bh, fmt, ...) \
70 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
71 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
73 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
74 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
77 static void ext4_xattr_block_cache_insert(struct mb_cache
*,
78 struct buffer_head
*);
79 static struct buffer_head
*
80 ext4_xattr_block_cache_find(struct inode
*, struct ext4_xattr_header
*,
81 struct mb_cache_entry
**);
82 static __le32
ext4_xattr_hash_entry(char *name
, size_t name_len
, __le32
*value
,
84 static void ext4_xattr_rehash(struct ext4_xattr_header
*);
86 static const struct xattr_handler
* const ext4_xattr_handler_map
[] = {
87 [EXT4_XATTR_INDEX_USER
] = &ext4_xattr_user_handler
,
88 #ifdef CONFIG_EXT4_FS_POSIX_ACL
89 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
] = &posix_acl_access_xattr_handler
,
90 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &posix_acl_default_xattr_handler
,
92 [EXT4_XATTR_INDEX_TRUSTED
] = &ext4_xattr_trusted_handler
,
93 #ifdef CONFIG_EXT4_FS_SECURITY
94 [EXT4_XATTR_INDEX_SECURITY
] = &ext4_xattr_security_handler
,
98 const struct xattr_handler
*ext4_xattr_handlers
[] = {
99 &ext4_xattr_user_handler
,
100 &ext4_xattr_trusted_handler
,
101 #ifdef CONFIG_EXT4_FS_POSIX_ACL
102 &posix_acl_access_xattr_handler
,
103 &posix_acl_default_xattr_handler
,
105 #ifdef CONFIG_EXT4_FS_SECURITY
106 &ext4_xattr_security_handler
,
111 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
112 inode->i_sb->s_fs_info)->s_ea_block_cache)
114 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
115 inode->i_sb->s_fs_info)->s_ea_inode_cache)
118 ext4_expand_inode_array(struct ext4_xattr_inode_array
**ea_inode_array
,
119 struct inode
*inode
);
121 #ifdef CONFIG_LOCKDEP
122 void ext4_xattr_inode_set_class(struct inode
*ea_inode
)
124 lockdep_set_subclass(&ea_inode
->i_rwsem
, 1);
128 static __le32
ext4_xattr_block_csum(struct inode
*inode
,
130 struct ext4_xattr_header
*hdr
)
132 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
134 __le64 dsk_block_nr
= cpu_to_le64(block_nr
);
135 __u32 dummy_csum
= 0;
136 int offset
= offsetof(struct ext4_xattr_header
, h_checksum
);
138 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&dsk_block_nr
,
139 sizeof(dsk_block_nr
));
140 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
, offset
);
141 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, sizeof(dummy_csum
));
142 offset
+= sizeof(dummy_csum
);
143 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
+ offset
,
144 EXT4_BLOCK_SIZE(inode
->i_sb
) - offset
);
146 return cpu_to_le32(csum
);
149 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
150 struct buffer_head
*bh
)
152 struct ext4_xattr_header
*hdr
= BHDR(bh
);
155 if (ext4_has_metadata_csum(inode
->i_sb
)) {
157 ret
= (hdr
->h_checksum
== ext4_xattr_block_csum(inode
,
158 bh
->b_blocknr
, hdr
));
164 static void ext4_xattr_block_csum_set(struct inode
*inode
,
165 struct buffer_head
*bh
)
167 if (ext4_has_metadata_csum(inode
->i_sb
))
168 BHDR(bh
)->h_checksum
= ext4_xattr_block_csum(inode
,
169 bh
->b_blocknr
, BHDR(bh
));
172 static inline const struct xattr_handler
*
173 ext4_xattr_handler(int name_index
)
175 const struct xattr_handler
*handler
= NULL
;
177 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
178 handler
= ext4_xattr_handler_map
[name_index
];
183 ext4_xattr_check_entries(struct ext4_xattr_entry
*entry
, void *end
,
186 struct ext4_xattr_entry
*e
= entry
;
188 /* Find the end of the names list */
189 while (!IS_LAST_ENTRY(e
)) {
190 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(e
);
191 if ((void *)next
>= end
)
192 return -EFSCORRUPTED
;
196 /* Check the values */
197 while (!IS_LAST_ENTRY(entry
)) {
198 if (entry
->e_value_size
!= 0 &&
199 entry
->e_value_inum
== 0) {
200 u16 offs
= le16_to_cpu(entry
->e_value_offs
);
201 u32 size
= le32_to_cpu(entry
->e_value_size
);
205 * The value cannot overlap the names, and the value
206 * with padding cannot extend beyond 'end'. Check both
207 * the padded and unpadded sizes, since the size may
208 * overflow to 0 when adding padding.
210 if (offs
> end
- value_start
)
211 return -EFSCORRUPTED
;
212 value
= value_start
+ offs
;
213 if (value
< (void *)e
+ sizeof(u32
) ||
214 size
> end
- value
||
215 EXT4_XATTR_SIZE(size
) > end
- value
)
216 return -EFSCORRUPTED
;
218 entry
= EXT4_XATTR_NEXT(entry
);
225 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
229 if (buffer_verified(bh
))
232 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
233 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
234 return -EFSCORRUPTED
;
235 if (!ext4_xattr_block_csum_verify(inode
, bh
))
237 error
= ext4_xattr_check_entries(BFIRST(bh
), bh
->b_data
+ bh
->b_size
,
240 set_buffer_verified(bh
);
245 __xattr_check_inode(struct inode
*inode
, struct ext4_xattr_ibody_header
*header
,
246 void *end
, const char *function
, unsigned int line
)
248 int error
= -EFSCORRUPTED
;
250 if (end
- (void *)header
< sizeof(*header
) + sizeof(u32
) ||
251 (header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)))
253 error
= ext4_xattr_check_entries(IFIRST(header
), end
, IFIRST(header
));
256 __ext4_error_inode(inode
, function
, line
, 0,
257 "corrupted in-inode xattr");
261 #define xattr_check_inode(inode, header, end) \
262 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
265 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
266 const char *name
, int sorted
)
268 struct ext4_xattr_entry
*entry
;
274 name_len
= strlen(name
);
276 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
277 cmp
= name_index
- entry
->e_name_index
;
279 cmp
= name_len
- entry
->e_name_len
;
281 cmp
= memcmp(name
, entry
->e_name
, name_len
);
282 if (cmp
<= 0 && (sorted
|| cmp
== 0))
286 return cmp
? -ENODATA
: 0;
290 ext4_xattr_inode_hash(struct ext4_sb_info
*sbi
, const void *buffer
, size_t size
)
292 return ext4_chksum(sbi
, sbi
->s_csum_seed
, buffer
, size
);
295 static u64
ext4_xattr_inode_get_ref(struct inode
*ea_inode
)
297 return ((u64
)ea_inode
->i_ctime
.tv_sec
<< 32) |
298 (u32
) inode_peek_iversion_raw(ea_inode
);
301 static void ext4_xattr_inode_set_ref(struct inode
*ea_inode
, u64 ref_count
)
303 ea_inode
->i_ctime
.tv_sec
= (u32
)(ref_count
>> 32);
304 inode_set_iversion_raw(ea_inode
, ref_count
& 0xffffffff);
307 static u32
ext4_xattr_inode_get_hash(struct inode
*ea_inode
)
309 return (u32
)ea_inode
->i_atime
.tv_sec
;
312 static void ext4_xattr_inode_set_hash(struct inode
*ea_inode
, u32 hash
)
314 ea_inode
->i_atime
.tv_sec
= hash
;
318 * Read the EA value from an inode.
320 static int ext4_xattr_inode_read(struct inode
*ea_inode
, void *buf
, size_t size
)
322 int blocksize
= 1 << ea_inode
->i_blkbits
;
323 int bh_count
= (size
+ blocksize
- 1) >> ea_inode
->i_blkbits
;
324 int tail_size
= (size
% blocksize
) ?: blocksize
;
325 struct buffer_head
*bhs_inline
[8];
326 struct buffer_head
**bhs
= bhs_inline
;
329 if (bh_count
> ARRAY_SIZE(bhs_inline
)) {
330 bhs
= kmalloc_array(bh_count
, sizeof(*bhs
), GFP_NOFS
);
335 ret
= ext4_bread_batch(ea_inode
, 0 /* block */, bh_count
,
336 true /* wait */, bhs
);
340 for (i
= 0; i
< bh_count
; i
++) {
341 /* There shouldn't be any holes in ea_inode. */
346 memcpy((char *)buf
+ blocksize
* i
, bhs
[i
]->b_data
,
347 i
< bh_count
- 1 ? blocksize
: tail_size
);
351 for (i
= 0; i
< bh_count
; i
++)
354 if (bhs
!= bhs_inline
)
359 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
361 static int ext4_xattr_inode_iget(struct inode
*parent
, unsigned long ea_ino
,
362 u32 ea_inode_hash
, struct inode
**ea_inode
)
367 inode
= ext4_iget(parent
->i_sb
, ea_ino
);
369 err
= PTR_ERR(inode
);
370 ext4_error(parent
->i_sb
,
371 "error while reading EA inode %lu err=%d", ea_ino
,
376 if (is_bad_inode(inode
)) {
377 ext4_error(parent
->i_sb
,
378 "error while reading EA inode %lu is_bad_inode",
384 if (!(EXT4_I(inode
)->i_flags
& EXT4_EA_INODE_FL
)) {
385 ext4_error(parent
->i_sb
,
386 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
392 ext4_xattr_inode_set_class(inode
);
395 * Check whether this is an old Lustre-style xattr inode. Lustre
396 * implementation does not have hash validation, rather it has a
397 * backpointer from ea_inode to the parent inode.
399 if (ea_inode_hash
!= ext4_xattr_inode_get_hash(inode
) &&
400 EXT4_XATTR_INODE_GET_PARENT(inode
) == parent
->i_ino
&&
401 inode
->i_generation
== parent
->i_generation
) {
402 ext4_set_inode_state(inode
, EXT4_STATE_LUSTRE_EA_INODE
);
403 ext4_xattr_inode_set_ref(inode
, 1);
406 inode
->i_flags
|= S_NOQUOTA
;
418 ext4_xattr_inode_verify_hashes(struct inode
*ea_inode
,
419 struct ext4_xattr_entry
*entry
, void *buffer
,
424 /* Verify stored hash matches calculated hash. */
425 hash
= ext4_xattr_inode_hash(EXT4_SB(ea_inode
->i_sb
), buffer
, size
);
426 if (hash
!= ext4_xattr_inode_get_hash(ea_inode
))
427 return -EFSCORRUPTED
;
430 __le32 e_hash
, tmp_data
;
432 /* Verify entry hash. */
433 tmp_data
= cpu_to_le32(hash
);
434 e_hash
= ext4_xattr_hash_entry(entry
->e_name
, entry
->e_name_len
,
436 if (e_hash
!= entry
->e_hash
)
437 return -EFSCORRUPTED
;
443 * Read xattr value from the EA inode.
446 ext4_xattr_inode_get(struct inode
*inode
, struct ext4_xattr_entry
*entry
,
447 void *buffer
, size_t size
)
449 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(inode
);
450 struct inode
*ea_inode
;
453 err
= ext4_xattr_inode_iget(inode
, le32_to_cpu(entry
->e_value_inum
),
454 le32_to_cpu(entry
->e_hash
), &ea_inode
);
460 if (i_size_read(ea_inode
) != size
) {
461 ext4_warning_inode(ea_inode
,
462 "ea_inode file size=%llu entry size=%zu",
463 i_size_read(ea_inode
), size
);
468 err
= ext4_xattr_inode_read(ea_inode
, buffer
, size
);
472 if (!ext4_test_inode_state(ea_inode
, EXT4_STATE_LUSTRE_EA_INODE
)) {
473 err
= ext4_xattr_inode_verify_hashes(ea_inode
, entry
, buffer
,
476 ext4_warning_inode(ea_inode
,
477 "EA inode hash validation failed");
482 mb_cache_entry_create(ea_inode_cache
, GFP_NOFS
,
483 ext4_xattr_inode_get_hash(ea_inode
),
484 ea_inode
->i_ino
, true /* reusable */);
492 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
493 void *buffer
, size_t buffer_size
)
495 struct buffer_head
*bh
= NULL
;
496 struct ext4_xattr_entry
*entry
;
499 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
501 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
502 name_index
, name
, buffer
, (long)buffer_size
);
505 if (!EXT4_I(inode
)->i_file_acl
)
507 ea_idebug(inode
, "reading block %llu",
508 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
509 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
512 ea_bdebug(bh
, "b_count=%d, refcount=%d",
513 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
514 if (ext4_xattr_check_block(inode
, bh
)) {
515 EXT4_ERROR_INODE(inode
, "bad block %llu",
516 EXT4_I(inode
)->i_file_acl
);
517 error
= -EFSCORRUPTED
;
520 ext4_xattr_block_cache_insert(ea_block_cache
, bh
);
522 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, 1);
525 size
= le32_to_cpu(entry
->e_value_size
);
528 if (size
> buffer_size
)
530 if (entry
->e_value_inum
) {
531 error
= ext4_xattr_inode_get(inode
, entry
, buffer
,
536 memcpy(buffer
, bh
->b_data
+
537 le16_to_cpu(entry
->e_value_offs
), size
);
548 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
549 void *buffer
, size_t buffer_size
)
551 struct ext4_xattr_ibody_header
*header
;
552 struct ext4_xattr_entry
*entry
;
553 struct ext4_inode
*raw_inode
;
554 struct ext4_iloc iloc
;
559 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
561 error
= ext4_get_inode_loc(inode
, &iloc
);
564 raw_inode
= ext4_raw_inode(&iloc
);
565 header
= IHDR(inode
, raw_inode
);
566 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
567 error
= xattr_check_inode(inode
, header
, end
);
570 entry
= IFIRST(header
);
571 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, 0);
574 size
= le32_to_cpu(entry
->e_value_size
);
577 if (size
> buffer_size
)
579 if (entry
->e_value_inum
) {
580 error
= ext4_xattr_inode_get(inode
, entry
, buffer
,
585 memcpy(buffer
, (void *)IFIRST(header
) +
586 le16_to_cpu(entry
->e_value_offs
), size
);
599 * Copy an extended attribute into the buffer
600 * provided, or compute the buffer size required.
601 * Buffer is NULL to compute the size of the buffer required.
603 * Returns a negative error number on failure, or the number of bytes
604 * used / required on success.
607 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
608 void *buffer
, size_t buffer_size
)
612 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
615 if (strlen(name
) > 255)
618 down_read(&EXT4_I(inode
)->xattr_sem
);
619 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
621 if (error
== -ENODATA
)
622 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
624 up_read(&EXT4_I(inode
)->xattr_sem
);
629 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
630 char *buffer
, size_t buffer_size
)
632 size_t rest
= buffer_size
;
634 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
635 const struct xattr_handler
*handler
=
636 ext4_xattr_handler(entry
->e_name_index
);
638 if (handler
&& (!handler
->list
|| handler
->list(dentry
))) {
639 const char *prefix
= handler
->prefix
?: handler
->name
;
640 size_t prefix_len
= strlen(prefix
);
641 size_t size
= prefix_len
+ entry
->e_name_len
+ 1;
646 memcpy(buffer
, prefix
, prefix_len
);
647 buffer
+= prefix_len
;
648 memcpy(buffer
, entry
->e_name
, entry
->e_name_len
);
649 buffer
+= entry
->e_name_len
;
655 return buffer_size
- rest
; /* total size */
659 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
661 struct inode
*inode
= d_inode(dentry
);
662 struct buffer_head
*bh
= NULL
;
665 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
666 buffer
, (long)buffer_size
);
669 if (!EXT4_I(inode
)->i_file_acl
)
671 ea_idebug(inode
, "reading block %llu",
672 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
673 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
677 ea_bdebug(bh
, "b_count=%d, refcount=%d",
678 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
679 if (ext4_xattr_check_block(inode
, bh
)) {
680 EXT4_ERROR_INODE(inode
, "bad block %llu",
681 EXT4_I(inode
)->i_file_acl
);
682 error
= -EFSCORRUPTED
;
685 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode
), bh
);
686 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
695 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
697 struct inode
*inode
= d_inode(dentry
);
698 struct ext4_xattr_ibody_header
*header
;
699 struct ext4_inode
*raw_inode
;
700 struct ext4_iloc iloc
;
704 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
706 error
= ext4_get_inode_loc(inode
, &iloc
);
709 raw_inode
= ext4_raw_inode(&iloc
);
710 header
= IHDR(inode
, raw_inode
);
711 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
712 error
= xattr_check_inode(inode
, header
, end
);
715 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
716 buffer
, buffer_size
);
724 * Inode operation listxattr()
726 * d_inode(dentry)->i_rwsem: don't care
728 * Copy a list of attribute names into the buffer
729 * provided, or compute the buffer size required.
730 * Buffer is NULL to compute the size of the buffer required.
732 * Returns a negative error number on failure, or the number of bytes
733 * used / required on success.
736 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
740 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
741 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
748 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
753 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
758 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
761 static void ext4_xattr_update_super_block(handle_t
*handle
,
762 struct super_block
*sb
)
764 if (ext4_has_feature_xattr(sb
))
767 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
768 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
769 ext4_set_feature_xattr(sb
);
770 ext4_handle_dirty_super(handle
, sb
);
774 int ext4_get_inode_usage(struct inode
*inode
, qsize_t
*usage
)
776 struct ext4_iloc iloc
= { .bh
= NULL
};
777 struct buffer_head
*bh
= NULL
;
778 struct ext4_inode
*raw_inode
;
779 struct ext4_xattr_ibody_header
*header
;
780 struct ext4_xattr_entry
*entry
;
781 qsize_t ea_inode_refs
= 0;
785 lockdep_assert_held_read(&EXT4_I(inode
)->xattr_sem
);
787 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
788 ret
= ext4_get_inode_loc(inode
, &iloc
);
791 raw_inode
= ext4_raw_inode(&iloc
);
792 header
= IHDR(inode
, raw_inode
);
793 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
794 ret
= xattr_check_inode(inode
, header
, end
);
798 for (entry
= IFIRST(header
); !IS_LAST_ENTRY(entry
);
799 entry
= EXT4_XATTR_NEXT(entry
))
800 if (entry
->e_value_inum
)
804 if (EXT4_I(inode
)->i_file_acl
) {
805 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
811 if (ext4_xattr_check_block(inode
, bh
)) {
816 for (entry
= BFIRST(bh
); !IS_LAST_ENTRY(entry
);
817 entry
= EXT4_XATTR_NEXT(entry
))
818 if (entry
->e_value_inum
)
821 *usage
= ea_inode_refs
+ 1;
829 static inline size_t round_up_cluster(struct inode
*inode
, size_t length
)
831 struct super_block
*sb
= inode
->i_sb
;
832 size_t cluster_size
= 1 << (EXT4_SB(sb
)->s_cluster_bits
+
834 size_t mask
= ~(cluster_size
- 1);
836 return (length
+ cluster_size
- 1) & mask
;
839 static int ext4_xattr_inode_alloc_quota(struct inode
*inode
, size_t len
)
843 err
= dquot_alloc_inode(inode
);
846 err
= dquot_alloc_space_nodirty(inode
, round_up_cluster(inode
, len
));
848 dquot_free_inode(inode
);
852 static void ext4_xattr_inode_free_quota(struct inode
*parent
,
853 struct inode
*ea_inode
,
857 ext4_test_inode_state(ea_inode
, EXT4_STATE_LUSTRE_EA_INODE
))
859 dquot_free_space_nodirty(parent
, round_up_cluster(parent
, len
));
860 dquot_free_inode(parent
);
863 int __ext4_xattr_set_credits(struct super_block
*sb
, struct inode
*inode
,
864 struct buffer_head
*block_bh
, size_t value_len
,
871 * 1) Owner inode update
872 * 2) Ref count update on old xattr block
874 * 4) block bitmap update for new xattr block
875 * 5) group descriptor for new xattr block
876 * 6) block bitmap update for old xattr block
877 * 7) group descriptor for old block
879 * 6 & 7 can happen if we have two racing threads T_a and T_b
880 * which are each trying to set an xattr on inodes I_a and I_b
881 * which were both initially sharing an xattr block.
886 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(sb
);
889 * In case of inline data, we may push out the data to a block,
890 * so we need to reserve credits for this eventuality
892 if (inode
&& ext4_has_inline_data(inode
))
893 credits
+= ext4_writepage_trans_blocks(inode
) + 1;
895 /* We are done if ea_inode feature is not enabled. */
896 if (!ext4_has_feature_ea_inode(sb
))
899 /* New ea_inode, inode map, block bitmap, group descriptor. */
903 blocks
= (value_len
+ sb
->s_blocksize
- 1) >> sb
->s_blocksize_bits
;
905 /* Indirection block or one level of extent tree. */
908 /* Block bitmap and group descriptor updates for each block. */
909 credits
+= blocks
* 2;
911 /* Blocks themselves. */
915 /* Dereference ea_inode holding old xattr value.
916 * Old ea_inode, inode map, block bitmap, group descriptor.
920 /* Data blocks for old ea_inode. */
921 blocks
= XATTR_SIZE_MAX
>> sb
->s_blocksize_bits
;
923 /* Indirection block or one level of extent tree for old
928 /* Block bitmap and group descriptor updates for each block. */
929 credits
+= blocks
* 2;
932 /* We may need to clone the existing xattr block in which case we need
933 * to increment ref counts for existing ea_inodes referenced by it.
936 struct ext4_xattr_entry
*entry
= BFIRST(block_bh
);
938 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
))
939 if (entry
->e_value_inum
)
940 /* Ref count update on ea_inode. */
946 static int ext4_xattr_ensure_credits(handle_t
*handle
, struct inode
*inode
,
947 int credits
, struct buffer_head
*bh
,
948 bool dirty
, bool block_csum
)
952 if (!ext4_handle_valid(handle
))
955 if (handle
->h_buffer_credits
>= credits
)
958 error
= ext4_journal_extend(handle
, credits
- handle
->h_buffer_credits
);
962 ext4_warning(inode
->i_sb
, "Extend journal (error %d)", error
);
968 ext4_xattr_block_csum_set(inode
, bh
);
969 error
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
971 ext4_warning(inode
->i_sb
, "Handle metadata (error %d)",
977 error
= ext4_journal_restart(handle
, credits
);
979 ext4_warning(inode
->i_sb
, "Restart journal (error %d)", error
);
984 error
= ext4_journal_get_write_access(handle
, bh
);
986 ext4_warning(inode
->i_sb
,
987 "Get write access failed (error %d)",
995 static int ext4_xattr_inode_update_ref(handle_t
*handle
, struct inode
*ea_inode
,
998 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(ea_inode
);
999 struct ext4_iloc iloc
;
1004 inode_lock(ea_inode
);
1006 ret
= ext4_reserve_inode_write(handle
, ea_inode
, &iloc
);
1012 ref_count
= ext4_xattr_inode_get_ref(ea_inode
);
1013 ref_count
+= ref_change
;
1014 ext4_xattr_inode_set_ref(ea_inode
, ref_count
);
1016 if (ref_change
> 0) {
1017 WARN_ONCE(ref_count
<= 0, "EA inode %lu ref_count=%lld",
1018 ea_inode
->i_ino
, ref_count
);
1020 if (ref_count
== 1) {
1021 WARN_ONCE(ea_inode
->i_nlink
, "EA inode %lu i_nlink=%u",
1022 ea_inode
->i_ino
, ea_inode
->i_nlink
);
1024 set_nlink(ea_inode
, 1);
1025 ext4_orphan_del(handle
, ea_inode
);
1027 if (ea_inode_cache
) {
1028 hash
= ext4_xattr_inode_get_hash(ea_inode
);
1029 mb_cache_entry_create(ea_inode_cache
,
1032 true /* reusable */);
1036 WARN_ONCE(ref_count
< 0, "EA inode %lu ref_count=%lld",
1037 ea_inode
->i_ino
, ref_count
);
1039 if (ref_count
== 0) {
1040 WARN_ONCE(ea_inode
->i_nlink
!= 1,
1041 "EA inode %lu i_nlink=%u",
1042 ea_inode
->i_ino
, ea_inode
->i_nlink
);
1044 clear_nlink(ea_inode
);
1045 ext4_orphan_add(handle
, ea_inode
);
1047 if (ea_inode_cache
) {
1048 hash
= ext4_xattr_inode_get_hash(ea_inode
);
1049 mb_cache_entry_delete(ea_inode_cache
, hash
,
1055 ret
= ext4_mark_iloc_dirty(handle
, ea_inode
, &iloc
);
1058 ext4_warning_inode(ea_inode
,
1059 "ext4_mark_iloc_dirty() failed ret=%d", ret
);
1062 inode_unlock(ea_inode
);
1066 static int ext4_xattr_inode_inc_ref(handle_t
*handle
, struct inode
*ea_inode
)
1068 return ext4_xattr_inode_update_ref(handle
, ea_inode
, 1);
1071 static int ext4_xattr_inode_dec_ref(handle_t
*handle
, struct inode
*ea_inode
)
1073 return ext4_xattr_inode_update_ref(handle
, ea_inode
, -1);
1076 static int ext4_xattr_inode_inc_ref_all(handle_t
*handle
, struct inode
*parent
,
1077 struct ext4_xattr_entry
*first
)
1079 struct inode
*ea_inode
;
1080 struct ext4_xattr_entry
*entry
;
1081 struct ext4_xattr_entry
*failed_entry
;
1082 unsigned int ea_ino
;
1085 for (entry
= first
; !IS_LAST_ENTRY(entry
);
1086 entry
= EXT4_XATTR_NEXT(entry
)) {
1087 if (!entry
->e_value_inum
)
1089 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1090 err
= ext4_xattr_inode_iget(parent
, ea_ino
,
1091 le32_to_cpu(entry
->e_hash
),
1095 err
= ext4_xattr_inode_inc_ref(handle
, ea_inode
);
1097 ext4_warning_inode(ea_inode
, "inc ref error %d", err
);
1107 failed_entry
= entry
;
1109 for (entry
= first
; entry
!= failed_entry
;
1110 entry
= EXT4_XATTR_NEXT(entry
)) {
1111 if (!entry
->e_value_inum
)
1113 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1114 err
= ext4_xattr_inode_iget(parent
, ea_ino
,
1115 le32_to_cpu(entry
->e_hash
),
1118 ext4_warning(parent
->i_sb
,
1119 "cleanup ea_ino %u iget error %d", ea_ino
,
1123 err
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1125 ext4_warning_inode(ea_inode
, "cleanup dec ref error %d",
1133 ext4_xattr_inode_dec_ref_all(handle_t
*handle
, struct inode
*parent
,
1134 struct buffer_head
*bh
,
1135 struct ext4_xattr_entry
*first
, bool block_csum
,
1136 struct ext4_xattr_inode_array
**ea_inode_array
,
1137 int extra_credits
, bool skip_quota
)
1139 struct inode
*ea_inode
;
1140 struct ext4_xattr_entry
*entry
;
1142 unsigned int ea_ino
;
1146 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1147 credits
= 2 + extra_credits
;
1149 for (entry
= first
; !IS_LAST_ENTRY(entry
);
1150 entry
= EXT4_XATTR_NEXT(entry
)) {
1151 if (!entry
->e_value_inum
)
1153 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1154 err
= ext4_xattr_inode_iget(parent
, ea_ino
,
1155 le32_to_cpu(entry
->e_hash
),
1160 err
= ext4_expand_inode_array(ea_inode_array
, ea_inode
);
1162 ext4_warning_inode(ea_inode
,
1163 "Expand inode array err=%d", err
);
1168 err
= ext4_xattr_ensure_credits(handle
, parent
, credits
, bh
,
1171 ext4_warning_inode(ea_inode
, "Ensure credits err=%d",
1176 err
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1178 ext4_warning_inode(ea_inode
, "ea_inode dec ref err=%d",
1184 ext4_xattr_inode_free_quota(parent
, ea_inode
,
1185 le32_to_cpu(entry
->e_value_size
));
1188 * Forget about ea_inode within the same transaction that
1189 * decrements the ref count. This avoids duplicate decrements in
1190 * case the rest of the work spills over to subsequent
1193 entry
->e_value_inum
= 0;
1194 entry
->e_value_size
= 0;
1201 * Note that we are deliberately skipping csum calculation for
1202 * the final update because we do not expect any journal
1203 * restarts until xattr block is freed.
1206 err
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
1208 ext4_warning_inode(parent
,
1209 "handle dirty metadata err=%d", err
);
1214 * Release the xattr block BH: If the reference count is > 1, decrement it;
1215 * otherwise free the block.
1218 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
1219 struct buffer_head
*bh
,
1220 struct ext4_xattr_inode_array
**ea_inode_array
,
1223 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
1227 BUFFER_TRACE(bh
, "get_write_access");
1228 error
= ext4_journal_get_write_access(handle
, bh
);
1233 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1234 ref
= le32_to_cpu(BHDR(bh
)->h_refcount
);
1236 ea_bdebug(bh
, "refcount now=0; freeing");
1238 * This must happen under buffer lock for
1239 * ext4_xattr_block_set() to reliably detect freed block
1242 mb_cache_entry_delete(ea_block_cache
, hash
,
1247 if (ext4_has_feature_ea_inode(inode
->i_sb
))
1248 ext4_xattr_inode_dec_ref_all(handle
, inode
, bh
,
1250 true /* block_csum */,
1253 true /* skip_quota */);
1254 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
1255 EXT4_FREE_BLOCKS_METADATA
|
1256 EXT4_FREE_BLOCKS_FORGET
);
1259 BHDR(bh
)->h_refcount
= cpu_to_le32(ref
);
1260 if (ref
== EXT4_XATTR_REFCOUNT_MAX
- 1) {
1261 struct mb_cache_entry
*ce
;
1263 if (ea_block_cache
) {
1264 ce
= mb_cache_entry_get(ea_block_cache
, hash
,
1268 mb_cache_entry_put(ea_block_cache
, ce
);
1273 ext4_xattr_block_csum_set(inode
, bh
);
1275 * Beware of this ugliness: Releasing of xattr block references
1276 * from different inodes can race and so we have to protect
1277 * from a race where someone else frees the block (and releases
1278 * its journal_head) before we are done dirtying the buffer. In
1279 * nojournal mode this race is harmless and we actually cannot
1280 * call ext4_handle_dirty_metadata() with locked buffer as
1281 * that function can call sync_dirty_buffer() so for that case
1282 * we handle the dirtying after unlocking the buffer.
1284 if (ext4_handle_valid(handle
))
1285 error
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1287 if (!ext4_handle_valid(handle
))
1288 error
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1290 ext4_handle_sync(handle
);
1291 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
1292 ea_bdebug(bh
, "refcount now=%d; releasing",
1293 le32_to_cpu(BHDR(bh
)->h_refcount
));
1296 ext4_std_error(inode
->i_sb
, error
);
1301 * Find the available free space for EAs. This also returns the total number of
1302 * bytes used by EA entries.
1304 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
1305 size_t *min_offs
, void *base
, int *total
)
1307 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1308 if (!last
->e_value_inum
&& last
->e_value_size
) {
1309 size_t offs
= le16_to_cpu(last
->e_value_offs
);
1310 if (offs
< *min_offs
)
1314 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
1316 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
1320 * Write the value of the EA in an inode.
1322 static int ext4_xattr_inode_write(handle_t
*handle
, struct inode
*ea_inode
,
1323 const void *buf
, int bufsize
)
1325 struct buffer_head
*bh
= NULL
;
1326 unsigned long block
= 0;
1327 int blocksize
= ea_inode
->i_sb
->s_blocksize
;
1328 int max_blocks
= (bufsize
+ blocksize
- 1) >> ea_inode
->i_blkbits
;
1329 int csize
, wsize
= 0;
1334 while (ret
>= 0 && ret
< max_blocks
) {
1335 struct ext4_map_blocks map
;
1336 map
.m_lblk
= block
+= ret
;
1337 map
.m_len
= max_blocks
-= ret
;
1339 ret
= ext4_map_blocks(handle
, ea_inode
, &map
,
1340 EXT4_GET_BLOCKS_CREATE
);
1342 ext4_mark_inode_dirty(handle
, ea_inode
);
1343 if (ret
== -ENOSPC
&&
1344 ext4_should_retry_alloc(ea_inode
->i_sb
, &retries
)) {
1356 while (wsize
< bufsize
) {
1359 csize
= (bufsize
- wsize
) > blocksize
? blocksize
:
1361 bh
= ext4_getblk(handle
, ea_inode
, block
, 0);
1364 ret
= ext4_journal_get_write_access(handle
, bh
);
1368 memcpy(bh
->b_data
, buf
, csize
);
1369 set_buffer_uptodate(bh
);
1370 ext4_handle_dirty_metadata(handle
, ea_inode
, bh
);
1377 inode_lock(ea_inode
);
1378 i_size_write(ea_inode
, wsize
);
1379 ext4_update_i_disksize(ea_inode
, wsize
);
1380 inode_unlock(ea_inode
);
1382 ext4_mark_inode_dirty(handle
, ea_inode
);
1391 * Create an inode to store the value of a large EA.
1393 static struct inode
*ext4_xattr_inode_create(handle_t
*handle
,
1394 struct inode
*inode
, u32 hash
)
1396 struct inode
*ea_inode
= NULL
;
1397 uid_t owner
[2] = { i_uid_read(inode
), i_gid_read(inode
) };
1401 * Let the next inode be the goal, so we try and allocate the EA inode
1402 * in the same group, or nearby one.
1404 ea_inode
= ext4_new_inode(handle
, inode
->i_sb
->s_root
->d_inode
,
1405 S_IFREG
| 0600, NULL
, inode
->i_ino
+ 1, owner
,
1407 if (!IS_ERR(ea_inode
)) {
1408 ea_inode
->i_op
= &ext4_file_inode_operations
;
1409 ea_inode
->i_fop
= &ext4_file_operations
;
1410 ext4_set_aops(ea_inode
);
1411 ext4_xattr_inode_set_class(ea_inode
);
1412 unlock_new_inode(ea_inode
);
1413 ext4_xattr_inode_set_ref(ea_inode
, 1);
1414 ext4_xattr_inode_set_hash(ea_inode
, hash
);
1415 err
= ext4_mark_inode_dirty(handle
, ea_inode
);
1417 err
= ext4_inode_attach_jinode(ea_inode
);
1420 return ERR_PTR(err
);
1424 * Xattr inodes are shared therefore quota charging is performed
1425 * at a higher level.
1427 dquot_free_inode(ea_inode
);
1428 dquot_drop(ea_inode
);
1429 inode_lock(ea_inode
);
1430 ea_inode
->i_flags
|= S_NOQUOTA
;
1431 inode_unlock(ea_inode
);
1437 static struct inode
*
1438 ext4_xattr_inode_cache_find(struct inode
*inode
, const void *value
,
1439 size_t value_len
, u32 hash
)
1441 struct inode
*ea_inode
;
1442 struct mb_cache_entry
*ce
;
1443 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(inode
);
1446 if (!ea_inode_cache
)
1449 ce
= mb_cache_entry_find_first(ea_inode_cache
, hash
);
1453 ea_data
= ext4_kvmalloc(value_len
, GFP_NOFS
);
1455 mb_cache_entry_put(ea_inode_cache
, ce
);
1460 ea_inode
= ext4_iget(inode
->i_sb
, ce
->e_value
);
1461 if (!IS_ERR(ea_inode
) &&
1462 !is_bad_inode(ea_inode
) &&
1463 (EXT4_I(ea_inode
)->i_flags
& EXT4_EA_INODE_FL
) &&
1464 i_size_read(ea_inode
) == value_len
&&
1465 !ext4_xattr_inode_read(ea_inode
, ea_data
, value_len
) &&
1466 !ext4_xattr_inode_verify_hashes(ea_inode
, NULL
, ea_data
,
1468 !memcmp(value
, ea_data
, value_len
)) {
1469 mb_cache_entry_touch(ea_inode_cache
, ce
);
1470 mb_cache_entry_put(ea_inode_cache
, ce
);
1475 if (!IS_ERR(ea_inode
))
1477 ce
= mb_cache_entry_find_next(ea_inode_cache
, ce
);
1484 * Add value of the EA in an inode.
1486 static int ext4_xattr_inode_lookup_create(handle_t
*handle
, struct inode
*inode
,
1487 const void *value
, size_t value_len
,
1488 struct inode
**ret_inode
)
1490 struct inode
*ea_inode
;
1494 hash
= ext4_xattr_inode_hash(EXT4_SB(inode
->i_sb
), value
, value_len
);
1495 ea_inode
= ext4_xattr_inode_cache_find(inode
, value
, value_len
, hash
);
1497 err
= ext4_xattr_inode_inc_ref(handle
, ea_inode
);
1503 *ret_inode
= ea_inode
;
1507 /* Create an inode for the EA value */
1508 ea_inode
= ext4_xattr_inode_create(handle
, inode
, hash
);
1509 if (IS_ERR(ea_inode
))
1510 return PTR_ERR(ea_inode
);
1512 err
= ext4_xattr_inode_write(handle
, ea_inode
, value
, value_len
);
1514 ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1519 if (EA_INODE_CACHE(inode
))
1520 mb_cache_entry_create(EA_INODE_CACHE(inode
), GFP_NOFS
, hash
,
1521 ea_inode
->i_ino
, true /* reusable */);
1523 *ret_inode
= ea_inode
;
1528 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1529 * feature is enabled.
1531 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1533 static int ext4_xattr_set_entry(struct ext4_xattr_info
*i
,
1534 struct ext4_xattr_search
*s
,
1535 handle_t
*handle
, struct inode
*inode
,
1538 struct ext4_xattr_entry
*last
;
1539 struct ext4_xattr_entry
*here
= s
->here
;
1540 size_t min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
1541 int in_inode
= i
->in_inode
;
1542 struct inode
*old_ea_inode
= NULL
;
1543 struct inode
*new_ea_inode
= NULL
;
1544 size_t old_size
, new_size
;
1547 /* Space used by old and new values. */
1548 old_size
= (!s
->not_found
&& !here
->e_value_inum
) ?
1549 EXT4_XATTR_SIZE(le32_to_cpu(here
->e_value_size
)) : 0;
1550 new_size
= (i
->value
&& !in_inode
) ? EXT4_XATTR_SIZE(i
->value_len
) : 0;
1553 * Optimization for the simple case when old and new values have the
1554 * same padded sizes. Not applicable if external inodes are involved.
1556 if (new_size
&& new_size
== old_size
) {
1557 size_t offs
= le16_to_cpu(here
->e_value_offs
);
1558 void *val
= s
->base
+ offs
;
1560 here
->e_value_size
= cpu_to_le32(i
->value_len
);
1561 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
1562 memset(val
, 0, new_size
);
1564 memcpy(val
, i
->value
, i
->value_len
);
1565 /* Clear padding bytes. */
1566 memset(val
+ i
->value_len
, 0, new_size
- i
->value_len
);
1571 /* Compute min_offs and last. */
1573 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1574 if (!last
->e_value_inum
&& last
->e_value_size
) {
1575 size_t offs
= le16_to_cpu(last
->e_value_offs
);
1576 if (offs
< min_offs
)
1581 /* Check whether we have enough space. */
1585 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
1587 free
+= EXT4_XATTR_LEN(name_len
) + old_size
;
1589 if (free
< EXT4_XATTR_LEN(name_len
) + new_size
) {
1595 * If storing the value in an external inode is an option,
1596 * reserve space for xattr entries/names in the external
1597 * attribute block so that a long value does not occupy the
1598 * whole space and prevent futher entries being added.
1600 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
1601 new_size
&& is_block
&&
1602 (min_offs
+ old_size
- new_size
) <
1603 EXT4_XATTR_BLOCK_RESERVE(inode
)) {
1610 * Getting access to old and new ea inodes is subject to failures.
1611 * Finish that work before doing any modifications to the xattr data.
1613 if (!s
->not_found
&& here
->e_value_inum
) {
1614 ret
= ext4_xattr_inode_iget(inode
,
1615 le32_to_cpu(here
->e_value_inum
),
1616 le32_to_cpu(here
->e_hash
),
1619 old_ea_inode
= NULL
;
1623 if (i
->value
&& in_inode
) {
1624 WARN_ON_ONCE(!i
->value_len
);
1626 ret
= ext4_xattr_inode_alloc_quota(inode
, i
->value_len
);
1630 ret
= ext4_xattr_inode_lookup_create(handle
, inode
, i
->value
,
1634 new_ea_inode
= NULL
;
1635 ext4_xattr_inode_free_quota(inode
, NULL
, i
->value_len
);
1641 /* We are ready to release ref count on the old_ea_inode. */
1642 ret
= ext4_xattr_inode_dec_ref(handle
, old_ea_inode
);
1644 /* Release newly required ref count on new_ea_inode. */
1648 err
= ext4_xattr_inode_dec_ref(handle
,
1651 ext4_warning_inode(new_ea_inode
,
1652 "dec ref new_ea_inode err=%d",
1654 ext4_xattr_inode_free_quota(inode
, new_ea_inode
,
1660 ext4_xattr_inode_free_quota(inode
, old_ea_inode
,
1661 le32_to_cpu(here
->e_value_size
));
1664 /* No failures allowed past this point. */
1666 if (!s
->not_found
&& here
->e_value_offs
) {
1667 /* Remove the old value. */
1668 void *first_val
= s
->base
+ min_offs
;
1669 size_t offs
= le16_to_cpu(here
->e_value_offs
);
1670 void *val
= s
->base
+ offs
;
1672 memmove(first_val
+ old_size
, first_val
, val
- first_val
);
1673 memset(first_val
, 0, old_size
);
1674 min_offs
+= old_size
;
1676 /* Adjust all value offsets. */
1678 while (!IS_LAST_ENTRY(last
)) {
1679 size_t o
= le16_to_cpu(last
->e_value_offs
);
1681 if (!last
->e_value_inum
&&
1682 last
->e_value_size
&& o
< offs
)
1683 last
->e_value_offs
= cpu_to_le16(o
+ old_size
);
1684 last
= EXT4_XATTR_NEXT(last
);
1689 /* Remove old name. */
1690 size_t size
= EXT4_XATTR_LEN(name_len
);
1692 last
= ENTRY((void *)last
- size
);
1693 memmove(here
, (void *)here
+ size
,
1694 (void *)last
- (void *)here
+ sizeof(__u32
));
1695 memset(last
, 0, size
);
1696 } else if (s
->not_found
) {
1697 /* Insert new name. */
1698 size_t size
= EXT4_XATTR_LEN(name_len
);
1699 size_t rest
= (void *)last
- (void *)here
+ sizeof(__u32
);
1701 memmove((void *)here
+ size
, here
, rest
);
1702 memset(here
, 0, size
);
1703 here
->e_name_index
= i
->name_index
;
1704 here
->e_name_len
= name_len
;
1705 memcpy(here
->e_name
, i
->name
, name_len
);
1707 /* This is an update, reset value info. */
1708 here
->e_value_inum
= 0;
1709 here
->e_value_offs
= 0;
1710 here
->e_value_size
= 0;
1714 /* Insert new value. */
1716 here
->e_value_inum
= cpu_to_le32(new_ea_inode
->i_ino
);
1717 } else if (i
->value_len
) {
1718 void *val
= s
->base
+ min_offs
- new_size
;
1720 here
->e_value_offs
= cpu_to_le16(min_offs
- new_size
);
1721 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
1722 memset(val
, 0, new_size
);
1724 memcpy(val
, i
->value
, i
->value_len
);
1725 /* Clear padding bytes. */
1726 memset(val
+ i
->value_len
, 0,
1727 new_size
- i
->value_len
);
1730 here
->e_value_size
= cpu_to_le32(i
->value_len
);
1737 /* Entry hash calculation. */
1742 * Feed crc32c hash instead of the raw value for entry
1743 * hash calculation. This is to avoid walking
1744 * potentially long value buffer again.
1746 crc32c_hash
= cpu_to_le32(
1747 ext4_xattr_inode_get_hash(new_ea_inode
));
1748 hash
= ext4_xattr_hash_entry(here
->e_name
,
1751 } else if (is_block
) {
1752 __le32
*value
= s
->base
+ le16_to_cpu(
1753 here
->e_value_offs
);
1755 hash
= ext4_xattr_hash_entry(here
->e_name
,
1756 here
->e_name_len
, value
,
1759 here
->e_hash
= hash
;
1763 ext4_xattr_rehash((struct ext4_xattr_header
*)s
->base
);
1772 struct ext4_xattr_block_find
{
1773 struct ext4_xattr_search s
;
1774 struct buffer_head
*bh
;
1778 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
1779 struct ext4_xattr_block_find
*bs
)
1781 struct super_block
*sb
= inode
->i_sb
;
1784 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
1785 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
1787 if (EXT4_I(inode
)->i_file_acl
) {
1788 /* The inode already has an extended attribute block. */
1789 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
1793 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
1794 atomic_read(&(bs
->bh
->b_count
)),
1795 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
1796 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
1797 EXT4_ERROR_INODE(inode
, "bad block %llu",
1798 EXT4_I(inode
)->i_file_acl
);
1799 error
= -EFSCORRUPTED
;
1802 /* Find the named attribute. */
1803 bs
->s
.base
= BHDR(bs
->bh
);
1804 bs
->s
.first
= BFIRST(bs
->bh
);
1805 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
1806 bs
->s
.here
= bs
->s
.first
;
1807 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
1809 if (error
&& error
!= -ENODATA
)
1811 bs
->s
.not_found
= error
;
1820 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
1821 struct ext4_xattr_info
*i
,
1822 struct ext4_xattr_block_find
*bs
)
1824 struct super_block
*sb
= inode
->i_sb
;
1825 struct buffer_head
*new_bh
= NULL
;
1826 struct ext4_xattr_search s_copy
= bs
->s
;
1827 struct ext4_xattr_search
*s
= &s_copy
;
1828 struct mb_cache_entry
*ce
= NULL
;
1830 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
1831 struct inode
*ea_inode
= NULL
, *tmp_inode
;
1832 size_t old_ea_inode_quota
= 0;
1833 unsigned int ea_ino
;
1836 #define header(x) ((struct ext4_xattr_header *)(x))
1839 BUFFER_TRACE(bs
->bh
, "get_write_access");
1840 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
1843 lock_buffer(bs
->bh
);
1845 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
1846 __u32 hash
= le32_to_cpu(BHDR(bs
->bh
)->h_hash
);
1849 * This must happen under buffer lock for
1850 * ext4_xattr_block_set() to reliably detect modified
1854 mb_cache_entry_delete(ea_block_cache
, hash
,
1856 ea_bdebug(bs
->bh
, "modifying in-place");
1857 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
,
1858 true /* is_block */);
1859 ext4_xattr_block_csum_set(inode
, bs
->bh
);
1860 unlock_buffer(bs
->bh
);
1861 if (error
== -EFSCORRUPTED
)
1864 error
= ext4_handle_dirty_metadata(handle
,
1871 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
1873 unlock_buffer(bs
->bh
);
1874 ea_bdebug(bs
->bh
, "cloning");
1875 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
1877 if (s
->base
== NULL
)
1879 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
1880 s
->first
= ENTRY(header(s
->base
)+1);
1881 header(s
->base
)->h_refcount
= cpu_to_le32(1);
1882 s
->here
= ENTRY(s
->base
+ offset
);
1883 s
->end
= s
->base
+ bs
->bh
->b_size
;
1886 * If existing entry points to an xattr inode, we need
1887 * to prevent ext4_xattr_set_entry() from decrementing
1888 * ref count on it because the reference belongs to the
1889 * original block. In this case, make the entry look
1890 * like it has an empty value.
1892 if (!s
->not_found
&& s
->here
->e_value_inum
) {
1893 ea_ino
= le32_to_cpu(s
->here
->e_value_inum
);
1894 error
= ext4_xattr_inode_iget(inode
, ea_ino
,
1895 le32_to_cpu(s
->here
->e_hash
),
1900 if (!ext4_test_inode_state(tmp_inode
,
1901 EXT4_STATE_LUSTRE_EA_INODE
)) {
1903 * Defer quota free call for previous
1904 * inode until success is guaranteed.
1906 old_ea_inode_quota
= le32_to_cpu(
1907 s
->here
->e_value_size
);
1911 s
->here
->e_value_inum
= 0;
1912 s
->here
->e_value_size
= 0;
1916 /* Allocate a buffer where we construct the new block. */
1917 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
1918 /* assert(header == s->base) */
1920 if (s
->base
== NULL
)
1922 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1923 header(s
->base
)->h_blocks
= cpu_to_le32(1);
1924 header(s
->base
)->h_refcount
= cpu_to_le32(1);
1925 s
->first
= ENTRY(header(s
->base
)+1);
1926 s
->here
= ENTRY(header(s
->base
)+1);
1927 s
->end
= s
->base
+ sb
->s_blocksize
;
1930 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
, true /* is_block */);
1931 if (error
== -EFSCORRUPTED
)
1936 if (i
->value
&& s
->here
->e_value_inum
) {
1938 * A ref count on ea_inode has been taken as part of the call to
1939 * ext4_xattr_set_entry() above. We would like to drop this
1940 * extra ref but we have to wait until the xattr block is
1941 * initialized and has its own ref count on the ea_inode.
1943 ea_ino
= le32_to_cpu(s
->here
->e_value_inum
);
1944 error
= ext4_xattr_inode_iget(inode
, ea_ino
,
1945 le32_to_cpu(s
->here
->e_hash
),
1954 if (!IS_LAST_ENTRY(s
->first
)) {
1955 new_bh
= ext4_xattr_block_cache_find(inode
, header(s
->base
),
1958 /* We found an identical block in the cache. */
1959 if (new_bh
== bs
->bh
)
1960 ea_bdebug(new_bh
, "keeping");
1964 WARN_ON_ONCE(dquot_initialize_needed(inode
));
1966 /* The old block is released after updating
1968 error
= dquot_alloc_block(inode
,
1969 EXT4_C2B(EXT4_SB(sb
), 1));
1972 BUFFER_TRACE(new_bh
, "get_write_access");
1973 error
= ext4_journal_get_write_access(handle
,
1977 lock_buffer(new_bh
);
1979 * We have to be careful about races with
1980 * freeing, rehashing or adding references to
1981 * xattr block. Once we hold buffer lock xattr
1982 * block's state is stable so we can check
1983 * whether the block got freed / rehashed or
1984 * not. Since we unhash mbcache entry under
1985 * buffer lock when freeing / rehashing xattr
1986 * block, checking whether entry is still
1987 * hashed is reliable. Same rules hold for
1988 * e_reusable handling.
1990 if (hlist_bl_unhashed(&ce
->e_hash_list
) ||
1993 * Undo everything and check mbcache
1996 unlock_buffer(new_bh
);
1997 dquot_free_block(inode
,
1998 EXT4_C2B(EXT4_SB(sb
),
2001 mb_cache_entry_put(ea_block_cache
, ce
);
2006 ref
= le32_to_cpu(BHDR(new_bh
)->h_refcount
) + 1;
2007 BHDR(new_bh
)->h_refcount
= cpu_to_le32(ref
);
2008 if (ref
>= EXT4_XATTR_REFCOUNT_MAX
)
2010 ea_bdebug(new_bh
, "reusing; refcount now=%d",
2012 ext4_xattr_block_csum_set(inode
, new_bh
);
2013 unlock_buffer(new_bh
);
2014 error
= ext4_handle_dirty_metadata(handle
,
2020 mb_cache_entry_touch(ea_block_cache
, ce
);
2021 mb_cache_entry_put(ea_block_cache
, ce
);
2023 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
2024 /* We were modifying this block in-place. */
2025 ea_bdebug(bs
->bh
, "keeping this block");
2026 ext4_xattr_block_cache_insert(ea_block_cache
, bs
->bh
);
2030 /* We need to allocate a new block */
2031 ext4_fsblk_t goal
, block
;
2033 WARN_ON_ONCE(dquot_initialize_needed(inode
));
2035 goal
= ext4_group_first_block_no(sb
,
2036 EXT4_I(inode
)->i_block_group
);
2038 /* non-extent files can't have physical blocks past 2^32 */
2039 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
2040 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
2042 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
2047 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
2048 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
2050 ea_idebug(inode
, "creating block %llu",
2051 (unsigned long long)block
);
2053 new_bh
= sb_getblk(sb
, block
);
2054 if (unlikely(!new_bh
)) {
2057 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
2058 EXT4_FREE_BLOCKS_METADATA
);
2061 error
= ext4_xattr_inode_inc_ref_all(handle
, inode
,
2062 ENTRY(header(s
->base
)+1));
2066 /* Drop the extra ref on ea_inode. */
2067 error
= ext4_xattr_inode_dec_ref(handle
,
2070 ext4_warning_inode(ea_inode
,
2077 lock_buffer(new_bh
);
2078 error
= ext4_journal_get_create_access(handle
, new_bh
);
2080 unlock_buffer(new_bh
);
2084 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
2085 ext4_xattr_block_csum_set(inode
, new_bh
);
2086 set_buffer_uptodate(new_bh
);
2087 unlock_buffer(new_bh
);
2088 ext4_xattr_block_cache_insert(ea_block_cache
, new_bh
);
2089 error
= ext4_handle_dirty_metadata(handle
, inode
,
2096 if (old_ea_inode_quota
)
2097 ext4_xattr_inode_free_quota(inode
, NULL
, old_ea_inode_quota
);
2099 /* Update the inode. */
2100 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
2102 /* Drop the previous xattr block. */
2103 if (bs
->bh
&& bs
->bh
!= new_bh
) {
2104 struct ext4_xattr_inode_array
*ea_inode_array
= NULL
;
2106 ext4_xattr_release_block(handle
, inode
, bs
->bh
,
2108 0 /* extra_credits */);
2109 ext4_xattr_inode_array_free(ea_inode_array
);
2117 error2
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
2119 ext4_warning_inode(ea_inode
, "dec ref error=%d",
2122 /* If there was an error, revert the quota charge. */
2124 ext4_xattr_inode_free_quota(inode
, ea_inode
,
2125 i_size_read(ea_inode
));
2129 mb_cache_entry_put(ea_block_cache
, ce
);
2131 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
2137 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
2141 EXT4_ERROR_INODE(inode
, "bad block %llu",
2142 EXT4_I(inode
)->i_file_acl
);
2148 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
2149 struct ext4_xattr_ibody_find
*is
)
2151 struct ext4_xattr_ibody_header
*header
;
2152 struct ext4_inode
*raw_inode
;
2155 if (EXT4_I(inode
)->i_extra_isize
== 0)
2157 raw_inode
= ext4_raw_inode(&is
->iloc
);
2158 header
= IHDR(inode
, raw_inode
);
2159 is
->s
.base
= is
->s
.first
= IFIRST(header
);
2160 is
->s
.here
= is
->s
.first
;
2161 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
2162 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
2163 error
= xattr_check_inode(inode
, header
, is
->s
.end
);
2166 /* Find the named attribute. */
2167 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
2169 if (error
&& error
!= -ENODATA
)
2171 is
->s
.not_found
= error
;
2176 int ext4_xattr_ibody_inline_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 */);
2188 if (error
== -ENOSPC
&&
2189 ext4_has_inline_data(inode
)) {
2190 error
= ext4_try_to_evict_inline_data(handle
, inode
,
2191 EXT4_XATTR_LEN(strlen(i
->name
) +
2192 EXT4_XATTR_SIZE(i
->value_len
)));
2195 error
= ext4_xattr_ibody_find(inode
, i
, is
);
2198 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
,
2199 false /* is_block */);
2204 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
2205 if (!IS_LAST_ENTRY(s
->first
)) {
2206 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
2207 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
2209 header
->h_magic
= cpu_to_le32(0);
2210 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
2215 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
2216 struct ext4_xattr_info
*i
,
2217 struct ext4_xattr_ibody_find
*is
)
2219 struct ext4_xattr_ibody_header
*header
;
2220 struct ext4_xattr_search
*s
= &is
->s
;
2223 if (EXT4_I(inode
)->i_extra_isize
== 0)
2225 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
, false /* is_block */);
2228 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
2229 if (!IS_LAST_ENTRY(s
->first
)) {
2230 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
2231 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
2233 header
->h_magic
= cpu_to_le32(0);
2234 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
2239 static int ext4_xattr_value_same(struct ext4_xattr_search
*s
,
2240 struct ext4_xattr_info
*i
)
2244 /* When e_value_inum is set the value is stored externally. */
2245 if (s
->here
->e_value_inum
)
2247 if (le32_to_cpu(s
->here
->e_value_size
) != i
->value_len
)
2249 value
= ((void *)s
->base
) + le16_to_cpu(s
->here
->e_value_offs
);
2250 return !memcmp(value
, i
->value
, i
->value_len
);
2253 static struct buffer_head
*ext4_xattr_get_block(struct inode
*inode
)
2255 struct buffer_head
*bh
;
2258 if (!EXT4_I(inode
)->i_file_acl
)
2260 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2262 return ERR_PTR(-EIO
);
2263 error
= ext4_xattr_check_block(inode
, bh
);
2265 return ERR_PTR(error
);
2270 * ext4_xattr_set_handle()
2272 * Create, replace or remove an extended attribute for this inode. Value
2273 * is NULL to remove an existing extended attribute, and non-NULL to
2274 * either replace an existing extended attribute, or create a new extended
2275 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2276 * specify that an extended attribute must exist and must not exist
2277 * previous to the call, respectively.
2279 * Returns 0, or a negative error number on failure.
2282 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
2283 const char *name
, const void *value
, size_t value_len
,
2286 struct ext4_xattr_info i
= {
2287 .name_index
= name_index
,
2290 .value_len
= value_len
,
2293 struct ext4_xattr_ibody_find is
= {
2294 .s
= { .not_found
= -ENODATA
, },
2296 struct ext4_xattr_block_find bs
= {
2297 .s
= { .not_found
= -ENODATA
, },
2304 if (strlen(name
) > 255)
2307 ext4_write_lock_xattr(inode
, &no_expand
);
2309 /* Check journal credits under write lock. */
2310 if (ext4_handle_valid(handle
)) {
2311 struct buffer_head
*bh
;
2314 bh
= ext4_xattr_get_block(inode
);
2316 error
= PTR_ERR(bh
);
2320 credits
= __ext4_xattr_set_credits(inode
->i_sb
, inode
, bh
,
2322 flags
& XATTR_CREATE
);
2325 if (!ext4_handle_has_enough_credits(handle
, credits
)) {
2331 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
2335 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
2336 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
2337 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
2338 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
2341 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
2345 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
2348 if (is
.s
.not_found
&& bs
.s
.not_found
) {
2350 if (flags
& XATTR_REPLACE
)
2357 if (flags
& XATTR_CREATE
)
2362 if (!is
.s
.not_found
)
2363 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
2364 else if (!bs
.s
.not_found
)
2365 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2368 /* Xattr value did not change? Save us some work and bail out */
2369 if (!is
.s
.not_found
&& ext4_xattr_value_same(&is
.s
, &i
))
2371 if (!bs
.s
.not_found
&& ext4_xattr_value_same(&bs
.s
, &i
))
2374 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2375 (EXT4_XATTR_SIZE(i
.value_len
) >
2376 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode
->i_sb
->s_blocksize
)))
2379 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
2380 if (!error
&& !bs
.s
.not_found
) {
2382 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2383 } else if (error
== -ENOSPC
) {
2384 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
2385 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
2389 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2390 if (!error
&& !is
.s
.not_found
) {
2392 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
2394 } else if (error
== -ENOSPC
) {
2396 * Xattr does not fit in the block, store at
2397 * external inode if possible.
2399 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2408 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
2409 inode
->i_ctime
= current_time(inode
);
2412 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
2414 * The bh is consumed by ext4_mark_iloc_dirty, even with
2419 ext4_handle_sync(handle
);
2425 ext4_write_unlock_xattr(inode
, &no_expand
);
2429 int ext4_xattr_set_credits(struct inode
*inode
, size_t value_len
,
2430 bool is_create
, int *credits
)
2432 struct buffer_head
*bh
;
2437 if (!EXT4_SB(inode
->i_sb
)->s_journal
)
2440 down_read(&EXT4_I(inode
)->xattr_sem
);
2442 bh
= ext4_xattr_get_block(inode
);
2446 *credits
= __ext4_xattr_set_credits(inode
->i_sb
, inode
, bh
,
2447 value_len
, is_create
);
2452 up_read(&EXT4_I(inode
)->xattr_sem
);
2459 * Like ext4_xattr_set_handle, but start from an inode. This extended
2460 * attribute modification is a filesystem transaction by itself.
2462 * Returns 0, or a negative error number on failure.
2465 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
2466 const void *value
, size_t value_len
, int flags
)
2469 struct super_block
*sb
= inode
->i_sb
;
2470 int error
, retries
= 0;
2473 error
= dquot_initialize(inode
);
2478 error
= ext4_xattr_set_credits(inode
, value_len
, flags
& XATTR_CREATE
,
2483 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
2484 if (IS_ERR(handle
)) {
2485 error
= PTR_ERR(handle
);
2489 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
2490 value
, value_len
, flags
);
2491 error2
= ext4_journal_stop(handle
);
2492 if (error
== -ENOSPC
&&
2493 ext4_should_retry_alloc(sb
, &retries
))
2503 * Shift the EA entries in the inode to create space for the increased
2506 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
2507 int value_offs_shift
, void *to
,
2508 void *from
, size_t n
)
2510 struct ext4_xattr_entry
*last
= entry
;
2513 /* We always shift xattr headers further thus offsets get lower */
2514 BUG_ON(value_offs_shift
> 0);
2516 /* Adjust the value offsets of the entries */
2517 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
2518 if (!last
->e_value_inum
&& last
->e_value_size
) {
2519 new_offs
= le16_to_cpu(last
->e_value_offs
) +
2521 last
->e_value_offs
= cpu_to_le16(new_offs
);
2524 /* Shift the entries by n bytes */
2525 memmove(to
, from
, n
);
2529 * Move xattr pointed to by 'entry' from inode into external xattr block
2531 static int ext4_xattr_move_to_block(handle_t
*handle
, struct inode
*inode
,
2532 struct ext4_inode
*raw_inode
,
2533 struct ext4_xattr_entry
*entry
)
2535 struct ext4_xattr_ibody_find
*is
= NULL
;
2536 struct ext4_xattr_block_find
*bs
= NULL
;
2537 char *buffer
= NULL
, *b_entry_name
= NULL
;
2538 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
2539 struct ext4_xattr_info i
= {
2542 .name_index
= entry
->e_name_index
,
2543 .in_inode
= !!entry
->e_value_inum
,
2545 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
2548 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
2549 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
2550 buffer
= kmalloc(value_size
, GFP_NOFS
);
2551 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
2552 if (!is
|| !bs
|| !buffer
|| !b_entry_name
) {
2557 is
->s
.not_found
= -ENODATA
;
2558 bs
->s
.not_found
= -ENODATA
;
2562 /* Save the entry name and the entry value */
2563 if (entry
->e_value_inum
) {
2564 error
= ext4_xattr_inode_get(inode
, entry
, buffer
, value_size
);
2568 size_t value_offs
= le16_to_cpu(entry
->e_value_offs
);
2569 memcpy(buffer
, (void *)IFIRST(header
) + value_offs
, value_size
);
2572 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
2573 b_entry_name
[entry
->e_name_len
] = '\0';
2574 i
.name
= b_entry_name
;
2576 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
2580 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
2584 /* Remove the chosen entry from the inode */
2585 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
2590 i
.value_len
= value_size
;
2591 error
= ext4_xattr_block_find(inode
, &i
, bs
);
2595 /* Add entry which was removed from the inode into the block */
2596 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
2601 kfree(b_entry_name
);
2604 brelse(is
->iloc
.bh
);
2611 static int ext4_xattr_make_inode_space(handle_t
*handle
, struct inode
*inode
,
2612 struct ext4_inode
*raw_inode
,
2613 int isize_diff
, size_t ifree
,
2614 size_t bfree
, int *total_ino
)
2616 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
2617 struct ext4_xattr_entry
*small_entry
;
2618 struct ext4_xattr_entry
*entry
;
2619 struct ext4_xattr_entry
*last
;
2620 unsigned int entry_size
; /* EA entry size */
2621 unsigned int total_size
; /* EA entry size + value size */
2622 unsigned int min_total_size
;
2625 while (isize_diff
> ifree
) {
2628 min_total_size
= ~0U;
2629 last
= IFIRST(header
);
2630 /* Find the entry best suited to be pushed into EA block */
2631 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
2632 total_size
= EXT4_XATTR_LEN(last
->e_name_len
);
2633 if (!last
->e_value_inum
)
2634 total_size
+= EXT4_XATTR_SIZE(
2635 le32_to_cpu(last
->e_value_size
));
2636 if (total_size
<= bfree
&&
2637 total_size
< min_total_size
) {
2638 if (total_size
+ ifree
< isize_diff
) {
2642 min_total_size
= total_size
;
2647 if (entry
== NULL
) {
2648 if (small_entry
== NULL
)
2650 entry
= small_entry
;
2653 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
2654 total_size
= entry_size
;
2655 if (!entry
->e_value_inum
)
2656 total_size
+= EXT4_XATTR_SIZE(
2657 le32_to_cpu(entry
->e_value_size
));
2658 error
= ext4_xattr_move_to_block(handle
, inode
, raw_inode
,
2663 *total_ino
-= entry_size
;
2664 ifree
+= total_size
;
2665 bfree
-= total_size
;
2672 * Expand an inode by new_extra_isize bytes when EAs are present.
2673 * Returns 0 on success or negative error number on failure.
2675 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
2676 struct ext4_inode
*raw_inode
, handle_t
*handle
)
2678 struct ext4_xattr_ibody_header
*header
;
2679 struct buffer_head
*bh
;
2680 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2681 static unsigned int mnt_count
;
2683 size_t ifree
, bfree
;
2686 int error
= 0, tried_min_extra_isize
= 0;
2687 int s_min_extra_isize
= le16_to_cpu(sbi
->s_es
->s_min_extra_isize
);
2688 int isize_diff
; /* How much do we need to grow i_extra_isize */
2691 isize_diff
= new_extra_isize
- EXT4_I(inode
)->i_extra_isize
;
2692 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
2695 header
= IHDR(inode
, raw_inode
);
2698 * Check if enough free space is available in the inode to shift the
2699 * entries ahead by new_extra_isize.
2702 base
= IFIRST(header
);
2703 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
2704 min_offs
= end
- base
;
2705 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
2707 error
= xattr_check_inode(inode
, header
, end
);
2711 ifree
= ext4_xattr_free_space(base
, &min_offs
, base
, &total_ino
);
2712 if (ifree
>= isize_diff
)
2716 * Enough free space isn't available in the inode, check if
2717 * EA block can hold new_extra_isize bytes.
2719 if (EXT4_I(inode
)->i_file_acl
) {
2720 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2724 if (ext4_xattr_check_block(inode
, bh
)) {
2725 EXT4_ERROR_INODE(inode
, "bad block %llu",
2726 EXT4_I(inode
)->i_file_acl
);
2727 error
= -EFSCORRUPTED
;
2732 end
= bh
->b_data
+ bh
->b_size
;
2733 min_offs
= end
- base
;
2734 bfree
= ext4_xattr_free_space(BFIRST(bh
), &min_offs
, base
,
2737 if (bfree
+ ifree
< isize_diff
) {
2738 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
2739 tried_min_extra_isize
++;
2740 new_extra_isize
= s_min_extra_isize
;
2747 bfree
= inode
->i_sb
->s_blocksize
;
2750 error
= ext4_xattr_make_inode_space(handle
, inode
, raw_inode
,
2751 isize_diff
, ifree
, bfree
,
2754 if (error
== -ENOSPC
&& !tried_min_extra_isize
&&
2755 s_min_extra_isize
) {
2756 tried_min_extra_isize
++;
2757 new_extra_isize
= s_min_extra_isize
;
2763 /* Adjust the offsets and shift the remaining entries ahead */
2764 ext4_xattr_shift_entries(IFIRST(header
), EXT4_I(inode
)->i_extra_isize
2765 - new_extra_isize
, (void *)raw_inode
+
2766 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
2767 (void *)header
, total_ino
);
2768 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
2771 if (error
&& (mnt_count
!= le16_to_cpu(sbi
->s_es
->s_mnt_count
))) {
2772 ext4_warning(inode
->i_sb
, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2774 mnt_count
= le16_to_cpu(sbi
->s_es
->s_mnt_count
);
2779 #define EIA_INCR 16 /* must be 2^n */
2780 #define EIA_MASK (EIA_INCR - 1)
2782 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2783 * If @ea_inode_array is new or full it will be grown and the old
2784 * contents copied over.
2787 ext4_expand_inode_array(struct ext4_xattr_inode_array
**ea_inode_array
,
2788 struct inode
*inode
)
2790 if (*ea_inode_array
== NULL
) {
2792 * Start with 15 inodes, so it fits into a power-of-two size.
2793 * If *ea_inode_array is NULL, this is essentially offsetof()
2796 kmalloc(offsetof(struct ext4_xattr_inode_array
,
2799 if (*ea_inode_array
== NULL
)
2801 (*ea_inode_array
)->count
= 0;
2802 } else if (((*ea_inode_array
)->count
& EIA_MASK
) == EIA_MASK
) {
2803 /* expand the array once all 15 + n * 16 slots are full */
2804 struct ext4_xattr_inode_array
*new_array
= NULL
;
2805 int count
= (*ea_inode_array
)->count
;
2807 /* if new_array is NULL, this is essentially offsetof() */
2808 new_array
= kmalloc(
2809 offsetof(struct ext4_xattr_inode_array
,
2810 inodes
[count
+ EIA_INCR
]),
2812 if (new_array
== NULL
)
2814 memcpy(new_array
, *ea_inode_array
,
2815 offsetof(struct ext4_xattr_inode_array
, inodes
[count
]));
2816 kfree(*ea_inode_array
);
2817 *ea_inode_array
= new_array
;
2819 (*ea_inode_array
)->inodes
[(*ea_inode_array
)->count
++] = inode
;
2824 * ext4_xattr_delete_inode()
2826 * Free extended attribute resources associated with this inode. Traverse
2827 * all entries and decrement reference on any xattr inodes associated with this
2828 * inode. This is called immediately before an inode is freed. We have exclusive
2829 * access to the inode. If an orphan inode is deleted it will also release its
2830 * references on xattr block and xattr inodes.
2832 int ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
,
2833 struct ext4_xattr_inode_array
**ea_inode_array
,
2836 struct buffer_head
*bh
= NULL
;
2837 struct ext4_xattr_ibody_header
*header
;
2838 struct ext4_iloc iloc
= { .bh
= NULL
};
2839 struct ext4_xattr_entry
*entry
;
2840 struct inode
*ea_inode
;
2843 error
= ext4_xattr_ensure_credits(handle
, inode
, extra_credits
,
2846 false /* block_csum */);
2848 EXT4_ERROR_INODE(inode
, "ensure credits (error %d)", error
);
2852 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2853 ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
2855 error
= ext4_get_inode_loc(inode
, &iloc
);
2857 EXT4_ERROR_INODE(inode
, "inode loc (error %d)", error
);
2861 error
= ext4_journal_get_write_access(handle
, iloc
.bh
);
2863 EXT4_ERROR_INODE(inode
, "write access (error %d)",
2868 header
= IHDR(inode
, ext4_raw_inode(&iloc
));
2869 if (header
->h_magic
== cpu_to_le32(EXT4_XATTR_MAGIC
))
2870 ext4_xattr_inode_dec_ref_all(handle
, inode
, iloc
.bh
,
2872 false /* block_csum */,
2875 false /* skip_quota */);
2878 if (EXT4_I(inode
)->i_file_acl
) {
2879 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2881 EXT4_ERROR_INODE(inode
, "block %llu read error",
2882 EXT4_I(inode
)->i_file_acl
);
2886 error
= ext4_xattr_check_block(inode
, bh
);
2888 EXT4_ERROR_INODE(inode
, "bad block %llu (error %d)",
2889 EXT4_I(inode
)->i_file_acl
, error
);
2893 if (ext4_has_feature_ea_inode(inode
->i_sb
)) {
2894 for (entry
= BFIRST(bh
); !IS_LAST_ENTRY(entry
);
2895 entry
= EXT4_XATTR_NEXT(entry
)) {
2896 if (!entry
->e_value_inum
)
2898 error
= ext4_xattr_inode_iget(inode
,
2899 le32_to_cpu(entry
->e_value_inum
),
2900 le32_to_cpu(entry
->e_hash
),
2904 ext4_xattr_inode_free_quota(inode
, ea_inode
,
2905 le32_to_cpu(entry
->e_value_size
));
2911 ext4_xattr_release_block(handle
, inode
, bh
, ea_inode_array
,
2914 * Update i_file_acl value in the same transaction that releases
2917 EXT4_I(inode
)->i_file_acl
= 0;
2918 error
= ext4_mark_inode_dirty(handle
, inode
);
2920 EXT4_ERROR_INODE(inode
, "mark inode dirty (error %d)",
2932 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array
*ea_inode_array
)
2936 if (ea_inode_array
== NULL
)
2939 for (idx
= 0; idx
< ea_inode_array
->count
; ++idx
)
2940 iput(ea_inode_array
->inodes
[idx
]);
2941 kfree(ea_inode_array
);
2945 * ext4_xattr_block_cache_insert()
2947 * Create a new entry in the extended attribute block cache, and insert
2948 * it unless such an entry is already in the cache.
2950 * Returns 0, or a negative error number on failure.
2953 ext4_xattr_block_cache_insert(struct mb_cache
*ea_block_cache
,
2954 struct buffer_head
*bh
)
2956 struct ext4_xattr_header
*header
= BHDR(bh
);
2957 __u32 hash
= le32_to_cpu(header
->h_hash
);
2958 int reusable
= le32_to_cpu(header
->h_refcount
) <
2959 EXT4_XATTR_REFCOUNT_MAX
;
2962 if (!ea_block_cache
)
2964 error
= mb_cache_entry_create(ea_block_cache
, GFP_NOFS
, hash
,
2965 bh
->b_blocknr
, reusable
);
2967 if (error
== -EBUSY
)
2968 ea_bdebug(bh
, "already in cache");
2970 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
2976 * Compare two extended attribute blocks for equality.
2978 * Returns 0 if the blocks are equal, 1 if they differ, and
2979 * a negative error number on errors.
2982 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
2983 struct ext4_xattr_header
*header2
)
2985 struct ext4_xattr_entry
*entry1
, *entry2
;
2987 entry1
= ENTRY(header1
+1);
2988 entry2
= ENTRY(header2
+1);
2989 while (!IS_LAST_ENTRY(entry1
)) {
2990 if (IS_LAST_ENTRY(entry2
))
2992 if (entry1
->e_hash
!= entry2
->e_hash
||
2993 entry1
->e_name_index
!= entry2
->e_name_index
||
2994 entry1
->e_name_len
!= entry2
->e_name_len
||
2995 entry1
->e_value_size
!= entry2
->e_value_size
||
2996 entry1
->e_value_inum
!= entry2
->e_value_inum
||
2997 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
2999 if (!entry1
->e_value_inum
&&
3000 memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
3001 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
3002 le32_to_cpu(entry1
->e_value_size
)))
3005 entry1
= EXT4_XATTR_NEXT(entry1
);
3006 entry2
= EXT4_XATTR_NEXT(entry2
);
3008 if (!IS_LAST_ENTRY(entry2
))
3014 * ext4_xattr_block_cache_find()
3016 * Find an identical extended attribute block.
3018 * Returns a pointer to the block found, or NULL if such a block was
3019 * not found or an error occurred.
3021 static struct buffer_head
*
3022 ext4_xattr_block_cache_find(struct inode
*inode
,
3023 struct ext4_xattr_header
*header
,
3024 struct mb_cache_entry
**pce
)
3026 __u32 hash
= le32_to_cpu(header
->h_hash
);
3027 struct mb_cache_entry
*ce
;
3028 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
3030 if (!ea_block_cache
)
3032 if (!header
->h_hash
)
3033 return NULL
; /* never share */
3034 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
3035 ce
= mb_cache_entry_find_first(ea_block_cache
, hash
);
3037 struct buffer_head
*bh
;
3039 bh
= sb_bread(inode
->i_sb
, ce
->e_value
);
3041 EXT4_ERROR_INODE(inode
, "block %lu read error",
3042 (unsigned long)ce
->e_value
);
3043 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
3048 ce
= mb_cache_entry_find_next(ea_block_cache
, ce
);
3053 #define NAME_HASH_SHIFT 5
3054 #define VALUE_HASH_SHIFT 16
3057 * ext4_xattr_hash_entry()
3059 * Compute the hash of an extended attribute.
3061 static __le32
ext4_xattr_hash_entry(char *name
, size_t name_len
, __le32
*value
,
3066 while (name_len
--) {
3067 hash
= (hash
<< NAME_HASH_SHIFT
) ^
3068 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
3071 while (value_count
--) {
3072 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
3073 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
3074 le32_to_cpu(*value
++);
3076 return cpu_to_le32(hash
);
3079 #undef NAME_HASH_SHIFT
3080 #undef VALUE_HASH_SHIFT
3082 #define BLOCK_HASH_SHIFT 16
3085 * ext4_xattr_rehash()
3087 * Re-compute the extended attribute hash value after an entry has changed.
3089 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
)
3091 struct ext4_xattr_entry
*here
;
3094 here
= ENTRY(header
+1);
3095 while (!IS_LAST_ENTRY(here
)) {
3096 if (!here
->e_hash
) {
3097 /* Block is not shared if an entry's hash value == 0 */
3101 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
3102 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
3103 le32_to_cpu(here
->e_hash
);
3104 here
= EXT4_XATTR_NEXT(here
);
3106 header
->h_hash
= cpu_to_le32(hash
);
3109 #undef BLOCK_HASH_SHIFT
3111 #define HASH_BUCKET_BITS 10
3114 ext4_xattr_create_cache(void)
3116 return mb_cache_create(HASH_BUCKET_BITS
);
3119 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
3122 mb_cache_destroy(cache
);