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 "ext4_jbd2.h"
64 #ifdef EXT4_XATTR_DEBUG
65 # define ea_idebug(inode, fmt, ...) \
66 printk(KERN_DEBUG "inode %s:%lu: " fmt "\n", \
67 inode->i_sb->s_id, inode->i_ino, ##__VA_ARGS__)
68 # define ea_bdebug(bh, fmt, ...) \
69 printk(KERN_DEBUG "block %pg:%lu: " fmt "\n", \
70 bh->b_bdev, (unsigned long)bh->b_blocknr, ##__VA_ARGS__)
72 # define ea_idebug(inode, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
73 # define ea_bdebug(bh, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
76 static void ext4_xattr_block_cache_insert(struct mb_cache
*,
77 struct buffer_head
*);
78 static struct buffer_head
*
79 ext4_xattr_block_cache_find(struct inode
*, struct ext4_xattr_header
*,
80 struct mb_cache_entry
**);
81 static __le32
ext4_xattr_hash_entry(char *name
, size_t name_len
, __le32
*value
,
83 static void ext4_xattr_rehash(struct ext4_xattr_header
*);
85 static const struct xattr_handler
* const ext4_xattr_handler_map
[] = {
86 [EXT4_XATTR_INDEX_USER
] = &ext4_xattr_user_handler
,
87 #ifdef CONFIG_EXT4_FS_POSIX_ACL
88 [EXT4_XATTR_INDEX_POSIX_ACL_ACCESS
] = &posix_acl_access_xattr_handler
,
89 [EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT
] = &posix_acl_default_xattr_handler
,
91 [EXT4_XATTR_INDEX_TRUSTED
] = &ext4_xattr_trusted_handler
,
92 #ifdef CONFIG_EXT4_FS_SECURITY
93 [EXT4_XATTR_INDEX_SECURITY
] = &ext4_xattr_security_handler
,
97 const struct xattr_handler
*ext4_xattr_handlers
[] = {
98 &ext4_xattr_user_handler
,
99 &ext4_xattr_trusted_handler
,
100 #ifdef CONFIG_EXT4_FS_POSIX_ACL
101 &posix_acl_access_xattr_handler
,
102 &posix_acl_default_xattr_handler
,
104 #ifdef CONFIG_EXT4_FS_SECURITY
105 &ext4_xattr_security_handler
,
110 #define EA_BLOCK_CACHE(inode) (((struct ext4_sb_info *) \
111 inode->i_sb->s_fs_info)->s_ea_block_cache)
113 #define EA_INODE_CACHE(inode) (((struct ext4_sb_info *) \
114 inode->i_sb->s_fs_info)->s_ea_inode_cache)
117 ext4_expand_inode_array(struct ext4_xattr_inode_array
**ea_inode_array
,
118 struct inode
*inode
);
120 #ifdef CONFIG_LOCKDEP
121 void ext4_xattr_inode_set_class(struct inode
*ea_inode
)
123 lockdep_set_subclass(&ea_inode
->i_rwsem
, 1);
127 static __le32
ext4_xattr_block_csum(struct inode
*inode
,
129 struct ext4_xattr_header
*hdr
)
131 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
133 __le64 dsk_block_nr
= cpu_to_le64(block_nr
);
134 __u32 dummy_csum
= 0;
135 int offset
= offsetof(struct ext4_xattr_header
, h_checksum
);
137 csum
= ext4_chksum(sbi
, sbi
->s_csum_seed
, (__u8
*)&dsk_block_nr
,
138 sizeof(dsk_block_nr
));
139 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
, offset
);
140 csum
= ext4_chksum(sbi
, csum
, (__u8
*)&dummy_csum
, sizeof(dummy_csum
));
141 offset
+= sizeof(dummy_csum
);
142 csum
= ext4_chksum(sbi
, csum
, (__u8
*)hdr
+ offset
,
143 EXT4_BLOCK_SIZE(inode
->i_sb
) - offset
);
145 return cpu_to_le32(csum
);
148 static int ext4_xattr_block_csum_verify(struct inode
*inode
,
149 struct buffer_head
*bh
)
151 struct ext4_xattr_header
*hdr
= BHDR(bh
);
154 if (ext4_has_metadata_csum(inode
->i_sb
)) {
156 ret
= (hdr
->h_checksum
== ext4_xattr_block_csum(inode
,
157 bh
->b_blocknr
, hdr
));
163 static void ext4_xattr_block_csum_set(struct inode
*inode
,
164 struct buffer_head
*bh
)
166 if (ext4_has_metadata_csum(inode
->i_sb
))
167 BHDR(bh
)->h_checksum
= ext4_xattr_block_csum(inode
,
168 bh
->b_blocknr
, BHDR(bh
));
171 static inline const struct xattr_handler
*
172 ext4_xattr_handler(int name_index
)
174 const struct xattr_handler
*handler
= NULL
;
176 if (name_index
> 0 && name_index
< ARRAY_SIZE(ext4_xattr_handler_map
))
177 handler
= ext4_xattr_handler_map
[name_index
];
182 ext4_xattr_check_entries(struct ext4_xattr_entry
*entry
, void *end
,
185 struct ext4_xattr_entry
*e
= entry
;
187 /* Find the end of the names list */
188 while (!IS_LAST_ENTRY(e
)) {
189 struct ext4_xattr_entry
*next
= EXT4_XATTR_NEXT(e
);
190 if ((void *)next
>= end
)
191 return -EFSCORRUPTED
;
195 /* Check the values */
196 while (!IS_LAST_ENTRY(entry
)) {
197 if (entry
->e_value_size
!= 0 &&
198 entry
->e_value_inum
== 0) {
199 u16 offs
= le16_to_cpu(entry
->e_value_offs
);
200 u32 size
= le32_to_cpu(entry
->e_value_size
);
204 * The value cannot overlap the names, and the value
205 * with padding cannot extend beyond 'end'. Check both
206 * the padded and unpadded sizes, since the size may
207 * overflow to 0 when adding padding.
209 if (offs
> end
- value_start
)
210 return -EFSCORRUPTED
;
211 value
= value_start
+ offs
;
212 if (value
< (void *)e
+ sizeof(u32
) ||
213 size
> end
- value
||
214 EXT4_XATTR_SIZE(size
) > end
- value
)
215 return -EFSCORRUPTED
;
217 entry
= EXT4_XATTR_NEXT(entry
);
224 ext4_xattr_check_block(struct inode
*inode
, struct buffer_head
*bh
)
228 if (buffer_verified(bh
))
231 if (BHDR(bh
)->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
) ||
232 BHDR(bh
)->h_blocks
!= cpu_to_le32(1))
233 return -EFSCORRUPTED
;
234 if (!ext4_xattr_block_csum_verify(inode
, bh
))
236 error
= ext4_xattr_check_entries(BFIRST(bh
), bh
->b_data
+ bh
->b_size
,
239 set_buffer_verified(bh
);
244 __xattr_check_inode(struct inode
*inode
, struct ext4_xattr_ibody_header
*header
,
245 void *end
, const char *function
, unsigned int line
)
247 int error
= -EFSCORRUPTED
;
249 if (end
- (void *)header
< sizeof(*header
) + sizeof(u32
) ||
250 (header
->h_magic
!= cpu_to_le32(EXT4_XATTR_MAGIC
)))
252 error
= ext4_xattr_check_entries(IFIRST(header
), end
, IFIRST(header
));
255 __ext4_error_inode(inode
, function
, line
, 0,
256 "corrupted in-inode xattr");
260 #define xattr_check_inode(inode, header, end) \
261 __xattr_check_inode((inode), (header), (end), __func__, __LINE__)
264 ext4_xattr_find_entry(struct ext4_xattr_entry
**pentry
, int name_index
,
265 const char *name
, int sorted
)
267 struct ext4_xattr_entry
*entry
;
273 name_len
= strlen(name
);
275 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
276 cmp
= name_index
- entry
->e_name_index
;
278 cmp
= name_len
- entry
->e_name_len
;
280 cmp
= memcmp(name
, entry
->e_name
, name_len
);
281 if (cmp
<= 0 && (sorted
|| cmp
== 0))
285 return cmp
? -ENODATA
: 0;
289 ext4_xattr_inode_hash(struct ext4_sb_info
*sbi
, const void *buffer
, size_t size
)
291 return ext4_chksum(sbi
, sbi
->s_csum_seed
, buffer
, size
);
294 static u64
ext4_xattr_inode_get_ref(struct inode
*ea_inode
)
296 return ((u64
)ea_inode
->i_ctime
.tv_sec
<< 32) |
297 ((u32
)ea_inode
->i_version
);
300 static void ext4_xattr_inode_set_ref(struct inode
*ea_inode
, u64 ref_count
)
302 ea_inode
->i_ctime
.tv_sec
= (u32
)(ref_count
>> 32);
303 ea_inode
->i_version
= (u32
)ref_count
;
306 static u32
ext4_xattr_inode_get_hash(struct inode
*ea_inode
)
308 return (u32
)ea_inode
->i_atime
.tv_sec
;
311 static void ext4_xattr_inode_set_hash(struct inode
*ea_inode
, u32 hash
)
313 ea_inode
->i_atime
.tv_sec
= hash
;
317 * Read the EA value from an inode.
319 static int ext4_xattr_inode_read(struct inode
*ea_inode
, void *buf
, size_t size
)
321 int blocksize
= 1 << ea_inode
->i_blkbits
;
322 int bh_count
= (size
+ blocksize
- 1) >> ea_inode
->i_blkbits
;
323 int tail_size
= (size
% blocksize
) ?: blocksize
;
324 struct buffer_head
*bhs_inline
[8];
325 struct buffer_head
**bhs
= bhs_inline
;
328 if (bh_count
> ARRAY_SIZE(bhs_inline
)) {
329 bhs
= kmalloc_array(bh_count
, sizeof(*bhs
), GFP_NOFS
);
334 ret
= ext4_bread_batch(ea_inode
, 0 /* block */, bh_count
,
335 true /* wait */, bhs
);
339 for (i
= 0; i
< bh_count
; i
++) {
340 /* There shouldn't be any holes in ea_inode. */
345 memcpy((char *)buf
+ blocksize
* i
, bhs
[i
]->b_data
,
346 i
< bh_count
- 1 ? blocksize
: tail_size
);
350 for (i
= 0; i
< bh_count
; i
++)
353 if (bhs
!= bhs_inline
)
358 #define EXT4_XATTR_INODE_GET_PARENT(inode) ((__u32)(inode)->i_mtime.tv_sec)
360 static int ext4_xattr_inode_iget(struct inode
*parent
, unsigned long ea_ino
,
361 u32 ea_inode_hash
, struct inode
**ea_inode
)
366 inode
= ext4_iget(parent
->i_sb
, ea_ino
);
368 err
= PTR_ERR(inode
);
369 ext4_error(parent
->i_sb
,
370 "error while reading EA inode %lu err=%d", ea_ino
,
375 if (is_bad_inode(inode
)) {
376 ext4_error(parent
->i_sb
,
377 "error while reading EA inode %lu is_bad_inode",
383 if (!(EXT4_I(inode
)->i_flags
& EXT4_EA_INODE_FL
)) {
384 ext4_error(parent
->i_sb
,
385 "EA inode %lu does not have EXT4_EA_INODE_FL flag",
391 ext4_xattr_inode_set_class(inode
);
394 * Check whether this is an old Lustre-style xattr inode. Lustre
395 * implementation does not have hash validation, rather it has a
396 * backpointer from ea_inode to the parent inode.
398 if (ea_inode_hash
!= ext4_xattr_inode_get_hash(inode
) &&
399 EXT4_XATTR_INODE_GET_PARENT(inode
) == parent
->i_ino
&&
400 inode
->i_generation
== parent
->i_generation
) {
401 ext4_set_inode_state(inode
, EXT4_STATE_LUSTRE_EA_INODE
);
402 ext4_xattr_inode_set_ref(inode
, 1);
405 inode
->i_flags
|= S_NOQUOTA
;
417 ext4_xattr_inode_verify_hashes(struct inode
*ea_inode
,
418 struct ext4_xattr_entry
*entry
, void *buffer
,
423 /* Verify stored hash matches calculated hash. */
424 hash
= ext4_xattr_inode_hash(EXT4_SB(ea_inode
->i_sb
), buffer
, size
);
425 if (hash
!= ext4_xattr_inode_get_hash(ea_inode
))
426 return -EFSCORRUPTED
;
429 __le32 e_hash
, tmp_data
;
431 /* Verify entry hash. */
432 tmp_data
= cpu_to_le32(hash
);
433 e_hash
= ext4_xattr_hash_entry(entry
->e_name
, entry
->e_name_len
,
435 if (e_hash
!= entry
->e_hash
)
436 return -EFSCORRUPTED
;
442 * Read xattr value from the EA inode.
445 ext4_xattr_inode_get(struct inode
*inode
, struct ext4_xattr_entry
*entry
,
446 void *buffer
, size_t size
)
448 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(inode
);
449 struct inode
*ea_inode
;
452 err
= ext4_xattr_inode_iget(inode
, le32_to_cpu(entry
->e_value_inum
),
453 le32_to_cpu(entry
->e_hash
), &ea_inode
);
459 if (i_size_read(ea_inode
) != size
) {
460 ext4_warning_inode(ea_inode
,
461 "ea_inode file size=%llu entry size=%zu",
462 i_size_read(ea_inode
), size
);
467 err
= ext4_xattr_inode_read(ea_inode
, buffer
, size
);
471 if (!ext4_test_inode_state(ea_inode
, EXT4_STATE_LUSTRE_EA_INODE
)) {
472 err
= ext4_xattr_inode_verify_hashes(ea_inode
, entry
, buffer
,
475 ext4_warning_inode(ea_inode
,
476 "EA inode hash validation failed");
481 mb_cache_entry_create(ea_inode_cache
, GFP_NOFS
,
482 ext4_xattr_inode_get_hash(ea_inode
),
483 ea_inode
->i_ino
, true /* reusable */);
491 ext4_xattr_block_get(struct inode
*inode
, int name_index
, const char *name
,
492 void *buffer
, size_t buffer_size
)
494 struct buffer_head
*bh
= NULL
;
495 struct ext4_xattr_entry
*entry
;
498 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
500 ea_idebug(inode
, "name=%d.%s, buffer=%p, buffer_size=%ld",
501 name_index
, name
, buffer
, (long)buffer_size
);
504 if (!EXT4_I(inode
)->i_file_acl
)
506 ea_idebug(inode
, "reading block %llu",
507 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
508 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
511 ea_bdebug(bh
, "b_count=%d, refcount=%d",
512 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
513 if (ext4_xattr_check_block(inode
, bh
)) {
514 EXT4_ERROR_INODE(inode
, "bad block %llu",
515 EXT4_I(inode
)->i_file_acl
);
516 error
= -EFSCORRUPTED
;
519 ext4_xattr_block_cache_insert(ea_block_cache
, bh
);
521 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, 1);
524 size
= le32_to_cpu(entry
->e_value_size
);
527 if (size
> buffer_size
)
529 if (entry
->e_value_inum
) {
530 error
= ext4_xattr_inode_get(inode
, entry
, buffer
,
535 memcpy(buffer
, bh
->b_data
+
536 le16_to_cpu(entry
->e_value_offs
), size
);
547 ext4_xattr_ibody_get(struct inode
*inode
, int name_index
, const char *name
,
548 void *buffer
, size_t buffer_size
)
550 struct ext4_xattr_ibody_header
*header
;
551 struct ext4_xattr_entry
*entry
;
552 struct ext4_inode
*raw_inode
;
553 struct ext4_iloc iloc
;
558 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
560 error
= ext4_get_inode_loc(inode
, &iloc
);
563 raw_inode
= ext4_raw_inode(&iloc
);
564 header
= IHDR(inode
, raw_inode
);
565 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
566 error
= xattr_check_inode(inode
, header
, end
);
569 entry
= IFIRST(header
);
570 error
= ext4_xattr_find_entry(&entry
, name_index
, name
, 0);
573 size
= le32_to_cpu(entry
->e_value_size
);
576 if (size
> buffer_size
)
578 if (entry
->e_value_inum
) {
579 error
= ext4_xattr_inode_get(inode
, entry
, buffer
,
584 memcpy(buffer
, (void *)IFIRST(header
) +
585 le16_to_cpu(entry
->e_value_offs
), size
);
598 * Copy an extended attribute into the buffer
599 * provided, or compute the buffer size required.
600 * Buffer is NULL to compute the size of the buffer required.
602 * Returns a negative error number on failure, or the number of bytes
603 * used / required on success.
606 ext4_xattr_get(struct inode
*inode
, int name_index
, const char *name
,
607 void *buffer
, size_t buffer_size
)
611 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode
->i_sb
))))
614 if (strlen(name
) > 255)
617 down_read(&EXT4_I(inode
)->xattr_sem
);
618 error
= ext4_xattr_ibody_get(inode
, name_index
, name
, buffer
,
620 if (error
== -ENODATA
)
621 error
= ext4_xattr_block_get(inode
, name_index
, name
, buffer
,
623 up_read(&EXT4_I(inode
)->xattr_sem
);
628 ext4_xattr_list_entries(struct dentry
*dentry
, struct ext4_xattr_entry
*entry
,
629 char *buffer
, size_t buffer_size
)
631 size_t rest
= buffer_size
;
633 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
)) {
634 const struct xattr_handler
*handler
=
635 ext4_xattr_handler(entry
->e_name_index
);
637 if (handler
&& (!handler
->list
|| handler
->list(dentry
))) {
638 const char *prefix
= handler
->prefix
?: handler
->name
;
639 size_t prefix_len
= strlen(prefix
);
640 size_t size
= prefix_len
+ entry
->e_name_len
+ 1;
645 memcpy(buffer
, prefix
, prefix_len
);
646 buffer
+= prefix_len
;
647 memcpy(buffer
, entry
->e_name
, entry
->e_name_len
);
648 buffer
+= entry
->e_name_len
;
654 return buffer_size
- rest
; /* total size */
658 ext4_xattr_block_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
660 struct inode
*inode
= d_inode(dentry
);
661 struct buffer_head
*bh
= NULL
;
664 ea_idebug(inode
, "buffer=%p, buffer_size=%ld",
665 buffer
, (long)buffer_size
);
668 if (!EXT4_I(inode
)->i_file_acl
)
670 ea_idebug(inode
, "reading block %llu",
671 (unsigned long long)EXT4_I(inode
)->i_file_acl
);
672 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
676 ea_bdebug(bh
, "b_count=%d, refcount=%d",
677 atomic_read(&(bh
->b_count
)), le32_to_cpu(BHDR(bh
)->h_refcount
));
678 if (ext4_xattr_check_block(inode
, bh
)) {
679 EXT4_ERROR_INODE(inode
, "bad block %llu",
680 EXT4_I(inode
)->i_file_acl
);
681 error
= -EFSCORRUPTED
;
684 ext4_xattr_block_cache_insert(EA_BLOCK_CACHE(inode
), bh
);
685 error
= ext4_xattr_list_entries(dentry
, BFIRST(bh
), buffer
, buffer_size
);
694 ext4_xattr_ibody_list(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
696 struct inode
*inode
= d_inode(dentry
);
697 struct ext4_xattr_ibody_header
*header
;
698 struct ext4_inode
*raw_inode
;
699 struct ext4_iloc iloc
;
703 if (!ext4_test_inode_state(inode
, EXT4_STATE_XATTR
))
705 error
= ext4_get_inode_loc(inode
, &iloc
);
708 raw_inode
= ext4_raw_inode(&iloc
);
709 header
= IHDR(inode
, raw_inode
);
710 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
711 error
= xattr_check_inode(inode
, header
, end
);
714 error
= ext4_xattr_list_entries(dentry
, IFIRST(header
),
715 buffer
, buffer_size
);
723 * Inode operation listxattr()
725 * d_inode(dentry)->i_rwsem: don't care
727 * Copy a list of attribute names into the buffer
728 * provided, or compute the buffer size required.
729 * Buffer is NULL to compute the size of the buffer required.
731 * Returns a negative error number on failure, or the number of bytes
732 * used / required on success.
735 ext4_listxattr(struct dentry
*dentry
, char *buffer
, size_t buffer_size
)
739 down_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
740 ret
= ret2
= ext4_xattr_ibody_list(dentry
, buffer
, buffer_size
);
747 ret
= ext4_xattr_block_list(dentry
, buffer
, buffer_size
);
752 up_read(&EXT4_I(d_inode(dentry
))->xattr_sem
);
757 * If the EXT4_FEATURE_COMPAT_EXT_ATTR feature of this file system is
760 static void ext4_xattr_update_super_block(handle_t
*handle
,
761 struct super_block
*sb
)
763 if (ext4_has_feature_xattr(sb
))
766 BUFFER_TRACE(EXT4_SB(sb
)->s_sbh
, "get_write_access");
767 if (ext4_journal_get_write_access(handle
, EXT4_SB(sb
)->s_sbh
) == 0) {
768 ext4_set_feature_xattr(sb
);
769 ext4_handle_dirty_super(handle
, sb
);
773 int ext4_get_inode_usage(struct inode
*inode
, qsize_t
*usage
)
775 struct ext4_iloc iloc
= { .bh
= NULL
};
776 struct buffer_head
*bh
= NULL
;
777 struct ext4_inode
*raw_inode
;
778 struct ext4_xattr_ibody_header
*header
;
779 struct ext4_xattr_entry
*entry
;
780 qsize_t ea_inode_refs
= 0;
784 lockdep_assert_held_read(&EXT4_I(inode
)->xattr_sem
);
786 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
787 ret
= ext4_get_inode_loc(inode
, &iloc
);
790 raw_inode
= ext4_raw_inode(&iloc
);
791 header
= IHDR(inode
, raw_inode
);
792 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
793 ret
= xattr_check_inode(inode
, header
, end
);
797 for (entry
= IFIRST(header
); !IS_LAST_ENTRY(entry
);
798 entry
= EXT4_XATTR_NEXT(entry
))
799 if (entry
->e_value_inum
)
803 if (EXT4_I(inode
)->i_file_acl
) {
804 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
810 if (ext4_xattr_check_block(inode
, bh
)) {
815 for (entry
= BFIRST(bh
); !IS_LAST_ENTRY(entry
);
816 entry
= EXT4_XATTR_NEXT(entry
))
817 if (entry
->e_value_inum
)
820 *usage
= ea_inode_refs
+ 1;
828 static inline size_t round_up_cluster(struct inode
*inode
, size_t length
)
830 struct super_block
*sb
= inode
->i_sb
;
831 size_t cluster_size
= 1 << (EXT4_SB(sb
)->s_cluster_bits
+
833 size_t mask
= ~(cluster_size
- 1);
835 return (length
+ cluster_size
- 1) & mask
;
838 static int ext4_xattr_inode_alloc_quota(struct inode
*inode
, size_t len
)
842 err
= dquot_alloc_inode(inode
);
845 err
= dquot_alloc_space_nodirty(inode
, round_up_cluster(inode
, len
));
847 dquot_free_inode(inode
);
851 static void ext4_xattr_inode_free_quota(struct inode
*parent
,
852 struct inode
*ea_inode
,
856 ext4_test_inode_state(ea_inode
, EXT4_STATE_LUSTRE_EA_INODE
))
858 dquot_free_space_nodirty(parent
, round_up_cluster(parent
, len
));
859 dquot_free_inode(parent
);
862 int __ext4_xattr_set_credits(struct super_block
*sb
, struct inode
*inode
,
863 struct buffer_head
*block_bh
, size_t value_len
,
870 * 1) Owner inode update
871 * 2) Ref count update on old xattr block
873 * 4) block bitmap update for new xattr block
874 * 5) group descriptor for new xattr block
875 * 6) block bitmap update for old xattr block
876 * 7) group descriptor for old block
878 * 6 & 7 can happen if we have two racing threads T_a and T_b
879 * which are each trying to set an xattr on inodes I_a and I_b
880 * which were both initially sharing an xattr block.
885 credits
+= EXT4_MAXQUOTAS_TRANS_BLOCKS(sb
);
888 * In case of inline data, we may push out the data to a block,
889 * so we need to reserve credits for this eventuality
891 if (inode
&& ext4_has_inline_data(inode
))
892 credits
+= ext4_writepage_trans_blocks(inode
) + 1;
894 /* We are done if ea_inode feature is not enabled. */
895 if (!ext4_has_feature_ea_inode(sb
))
898 /* New ea_inode, inode map, block bitmap, group descriptor. */
902 blocks
= (value_len
+ sb
->s_blocksize
- 1) >> sb
->s_blocksize_bits
;
904 /* Indirection block or one level of extent tree. */
907 /* Block bitmap and group descriptor updates for each block. */
908 credits
+= blocks
* 2;
910 /* Blocks themselves. */
914 /* Dereference ea_inode holding old xattr value.
915 * Old ea_inode, inode map, block bitmap, group descriptor.
919 /* Data blocks for old ea_inode. */
920 blocks
= XATTR_SIZE_MAX
>> sb
->s_blocksize_bits
;
922 /* Indirection block or one level of extent tree for old
927 /* Block bitmap and group descriptor updates for each block. */
928 credits
+= blocks
* 2;
931 /* We may need to clone the existing xattr block in which case we need
932 * to increment ref counts for existing ea_inodes referenced by it.
935 struct ext4_xattr_entry
*entry
= BFIRST(block_bh
);
937 for (; !IS_LAST_ENTRY(entry
); entry
= EXT4_XATTR_NEXT(entry
))
938 if (entry
->e_value_inum
)
939 /* Ref count update on ea_inode. */
945 static int ext4_xattr_ensure_credits(handle_t
*handle
, struct inode
*inode
,
946 int credits
, struct buffer_head
*bh
,
947 bool dirty
, bool block_csum
)
951 if (!ext4_handle_valid(handle
))
954 if (handle
->h_buffer_credits
>= credits
)
957 error
= ext4_journal_extend(handle
, credits
- handle
->h_buffer_credits
);
961 ext4_warning(inode
->i_sb
, "Extend journal (error %d)", error
);
967 ext4_xattr_block_csum_set(inode
, bh
);
968 error
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
970 ext4_warning(inode
->i_sb
, "Handle metadata (error %d)",
976 error
= ext4_journal_restart(handle
, credits
);
978 ext4_warning(inode
->i_sb
, "Restart journal (error %d)", error
);
983 error
= ext4_journal_get_write_access(handle
, bh
);
985 ext4_warning(inode
->i_sb
,
986 "Get write access failed (error %d)",
994 static int ext4_xattr_inode_update_ref(handle_t
*handle
, struct inode
*ea_inode
,
997 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(ea_inode
);
998 struct ext4_iloc iloc
;
1003 inode_lock(ea_inode
);
1005 ret
= ext4_reserve_inode_write(handle
, ea_inode
, &iloc
);
1011 ref_count
= ext4_xattr_inode_get_ref(ea_inode
);
1012 ref_count
+= ref_change
;
1013 ext4_xattr_inode_set_ref(ea_inode
, ref_count
);
1015 if (ref_change
> 0) {
1016 WARN_ONCE(ref_count
<= 0, "EA inode %lu ref_count=%lld",
1017 ea_inode
->i_ino
, ref_count
);
1019 if (ref_count
== 1) {
1020 WARN_ONCE(ea_inode
->i_nlink
, "EA inode %lu i_nlink=%u",
1021 ea_inode
->i_ino
, ea_inode
->i_nlink
);
1023 set_nlink(ea_inode
, 1);
1024 ext4_orphan_del(handle
, ea_inode
);
1026 if (ea_inode_cache
) {
1027 hash
= ext4_xattr_inode_get_hash(ea_inode
);
1028 mb_cache_entry_create(ea_inode_cache
,
1031 true /* reusable */);
1035 WARN_ONCE(ref_count
< 0, "EA inode %lu ref_count=%lld",
1036 ea_inode
->i_ino
, ref_count
);
1038 if (ref_count
== 0) {
1039 WARN_ONCE(ea_inode
->i_nlink
!= 1,
1040 "EA inode %lu i_nlink=%u",
1041 ea_inode
->i_ino
, ea_inode
->i_nlink
);
1043 clear_nlink(ea_inode
);
1044 ext4_orphan_add(handle
, ea_inode
);
1046 if (ea_inode_cache
) {
1047 hash
= ext4_xattr_inode_get_hash(ea_inode
);
1048 mb_cache_entry_delete(ea_inode_cache
, hash
,
1054 ret
= ext4_mark_iloc_dirty(handle
, ea_inode
, &iloc
);
1057 ext4_warning_inode(ea_inode
,
1058 "ext4_mark_iloc_dirty() failed ret=%d", ret
);
1061 inode_unlock(ea_inode
);
1065 static int ext4_xattr_inode_inc_ref(handle_t
*handle
, struct inode
*ea_inode
)
1067 return ext4_xattr_inode_update_ref(handle
, ea_inode
, 1);
1070 static int ext4_xattr_inode_dec_ref(handle_t
*handle
, struct inode
*ea_inode
)
1072 return ext4_xattr_inode_update_ref(handle
, ea_inode
, -1);
1075 static int ext4_xattr_inode_inc_ref_all(handle_t
*handle
, struct inode
*parent
,
1076 struct ext4_xattr_entry
*first
)
1078 struct inode
*ea_inode
;
1079 struct ext4_xattr_entry
*entry
;
1080 struct ext4_xattr_entry
*failed_entry
;
1081 unsigned int ea_ino
;
1084 for (entry
= first
; !IS_LAST_ENTRY(entry
);
1085 entry
= EXT4_XATTR_NEXT(entry
)) {
1086 if (!entry
->e_value_inum
)
1088 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1089 err
= ext4_xattr_inode_iget(parent
, ea_ino
,
1090 le32_to_cpu(entry
->e_hash
),
1094 err
= ext4_xattr_inode_inc_ref(handle
, ea_inode
);
1096 ext4_warning_inode(ea_inode
, "inc ref error %d", err
);
1106 failed_entry
= entry
;
1108 for (entry
= first
; entry
!= failed_entry
;
1109 entry
= EXT4_XATTR_NEXT(entry
)) {
1110 if (!entry
->e_value_inum
)
1112 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1113 err
= ext4_xattr_inode_iget(parent
, ea_ino
,
1114 le32_to_cpu(entry
->e_hash
),
1117 ext4_warning(parent
->i_sb
,
1118 "cleanup ea_ino %u iget error %d", ea_ino
,
1122 err
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1124 ext4_warning_inode(ea_inode
, "cleanup dec ref error %d",
1132 ext4_xattr_inode_dec_ref_all(handle_t
*handle
, struct inode
*parent
,
1133 struct buffer_head
*bh
,
1134 struct ext4_xattr_entry
*first
, bool block_csum
,
1135 struct ext4_xattr_inode_array
**ea_inode_array
,
1136 int extra_credits
, bool skip_quota
)
1138 struct inode
*ea_inode
;
1139 struct ext4_xattr_entry
*entry
;
1141 unsigned int ea_ino
;
1145 /* One credit for dec ref on ea_inode, one for orphan list addition, */
1146 credits
= 2 + extra_credits
;
1148 for (entry
= first
; !IS_LAST_ENTRY(entry
);
1149 entry
= EXT4_XATTR_NEXT(entry
)) {
1150 if (!entry
->e_value_inum
)
1152 ea_ino
= le32_to_cpu(entry
->e_value_inum
);
1153 err
= ext4_xattr_inode_iget(parent
, ea_ino
,
1154 le32_to_cpu(entry
->e_hash
),
1159 err
= ext4_expand_inode_array(ea_inode_array
, ea_inode
);
1161 ext4_warning_inode(ea_inode
,
1162 "Expand inode array err=%d", err
);
1167 err
= ext4_xattr_ensure_credits(handle
, parent
, credits
, bh
,
1170 ext4_warning_inode(ea_inode
, "Ensure credits err=%d",
1175 err
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1177 ext4_warning_inode(ea_inode
, "ea_inode dec ref err=%d",
1183 ext4_xattr_inode_free_quota(parent
, ea_inode
,
1184 le32_to_cpu(entry
->e_value_size
));
1187 * Forget about ea_inode within the same transaction that
1188 * decrements the ref count. This avoids duplicate decrements in
1189 * case the rest of the work spills over to subsequent
1192 entry
->e_value_inum
= 0;
1193 entry
->e_value_size
= 0;
1200 * Note that we are deliberately skipping csum calculation for
1201 * the final update because we do not expect any journal
1202 * restarts until xattr block is freed.
1205 err
= ext4_handle_dirty_metadata(handle
, NULL
, bh
);
1207 ext4_warning_inode(parent
,
1208 "handle dirty metadata err=%d", err
);
1213 * Release the xattr block BH: If the reference count is > 1, decrement it;
1214 * otherwise free the block.
1217 ext4_xattr_release_block(handle_t
*handle
, struct inode
*inode
,
1218 struct buffer_head
*bh
,
1219 struct ext4_xattr_inode_array
**ea_inode_array
,
1222 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
1226 BUFFER_TRACE(bh
, "get_write_access");
1227 error
= ext4_journal_get_write_access(handle
, bh
);
1232 hash
= le32_to_cpu(BHDR(bh
)->h_hash
);
1233 ref
= le32_to_cpu(BHDR(bh
)->h_refcount
);
1235 ea_bdebug(bh
, "refcount now=0; freeing");
1237 * This must happen under buffer lock for
1238 * ext4_xattr_block_set() to reliably detect freed block
1241 mb_cache_entry_delete(ea_block_cache
, hash
,
1246 if (ext4_has_feature_ea_inode(inode
->i_sb
))
1247 ext4_xattr_inode_dec_ref_all(handle
, inode
, bh
,
1249 true /* block_csum */,
1252 true /* skip_quota */);
1253 ext4_free_blocks(handle
, inode
, bh
, 0, 1,
1254 EXT4_FREE_BLOCKS_METADATA
|
1255 EXT4_FREE_BLOCKS_FORGET
);
1258 BHDR(bh
)->h_refcount
= cpu_to_le32(ref
);
1259 if (ref
== EXT4_XATTR_REFCOUNT_MAX
- 1) {
1260 struct mb_cache_entry
*ce
;
1262 if (ea_block_cache
) {
1263 ce
= mb_cache_entry_get(ea_block_cache
, hash
,
1267 mb_cache_entry_put(ea_block_cache
, ce
);
1272 ext4_xattr_block_csum_set(inode
, bh
);
1274 * Beware of this ugliness: Releasing of xattr block references
1275 * from different inodes can race and so we have to protect
1276 * from a race where someone else frees the block (and releases
1277 * its journal_head) before we are done dirtying the buffer. In
1278 * nojournal mode this race is harmless and we actually cannot
1279 * call ext4_handle_dirty_metadata() with locked buffer as
1280 * that function can call sync_dirty_buffer() so for that case
1281 * we handle the dirtying after unlocking the buffer.
1283 if (ext4_handle_valid(handle
))
1284 error
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1286 if (!ext4_handle_valid(handle
))
1287 error
= ext4_handle_dirty_metadata(handle
, inode
, bh
);
1289 ext4_handle_sync(handle
);
1290 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(inode
->i_sb
), 1));
1291 ea_bdebug(bh
, "refcount now=%d; releasing",
1292 le32_to_cpu(BHDR(bh
)->h_refcount
));
1295 ext4_std_error(inode
->i_sb
, error
);
1300 * Find the available free space for EAs. This also returns the total number of
1301 * bytes used by EA entries.
1303 static size_t ext4_xattr_free_space(struct ext4_xattr_entry
*last
,
1304 size_t *min_offs
, void *base
, int *total
)
1306 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1307 if (!last
->e_value_inum
&& last
->e_value_size
) {
1308 size_t offs
= le16_to_cpu(last
->e_value_offs
);
1309 if (offs
< *min_offs
)
1313 *total
+= EXT4_XATTR_LEN(last
->e_name_len
);
1315 return (*min_offs
- ((void *)last
- base
) - sizeof(__u32
));
1319 * Write the value of the EA in an inode.
1321 static int ext4_xattr_inode_write(handle_t
*handle
, struct inode
*ea_inode
,
1322 const void *buf
, int bufsize
)
1324 struct buffer_head
*bh
= NULL
;
1325 unsigned long block
= 0;
1326 int blocksize
= ea_inode
->i_sb
->s_blocksize
;
1327 int max_blocks
= (bufsize
+ blocksize
- 1) >> ea_inode
->i_blkbits
;
1328 int csize
, wsize
= 0;
1333 while (ret
>= 0 && ret
< max_blocks
) {
1334 struct ext4_map_blocks map
;
1335 map
.m_lblk
= block
+= ret
;
1336 map
.m_len
= max_blocks
-= ret
;
1338 ret
= ext4_map_blocks(handle
, ea_inode
, &map
,
1339 EXT4_GET_BLOCKS_CREATE
);
1341 ext4_mark_inode_dirty(handle
, ea_inode
);
1342 if (ret
== -ENOSPC
&&
1343 ext4_should_retry_alloc(ea_inode
->i_sb
, &retries
)) {
1355 while (wsize
< bufsize
) {
1358 csize
= (bufsize
- wsize
) > blocksize
? blocksize
:
1360 bh
= ext4_getblk(handle
, ea_inode
, block
, 0);
1363 ret
= ext4_journal_get_write_access(handle
, bh
);
1367 memcpy(bh
->b_data
, buf
, csize
);
1368 set_buffer_uptodate(bh
);
1369 ext4_handle_dirty_metadata(handle
, ea_inode
, bh
);
1376 inode_lock(ea_inode
);
1377 i_size_write(ea_inode
, wsize
);
1378 ext4_update_i_disksize(ea_inode
, wsize
);
1379 inode_unlock(ea_inode
);
1381 ext4_mark_inode_dirty(handle
, ea_inode
);
1390 * Create an inode to store the value of a large EA.
1392 static struct inode
*ext4_xattr_inode_create(handle_t
*handle
,
1393 struct inode
*inode
, u32 hash
)
1395 struct inode
*ea_inode
= NULL
;
1396 uid_t owner
[2] = { i_uid_read(inode
), i_gid_read(inode
) };
1400 * Let the next inode be the goal, so we try and allocate the EA inode
1401 * in the same group, or nearby one.
1403 ea_inode
= ext4_new_inode(handle
, inode
->i_sb
->s_root
->d_inode
,
1404 S_IFREG
| 0600, NULL
, inode
->i_ino
+ 1, owner
,
1406 if (!IS_ERR(ea_inode
)) {
1407 ea_inode
->i_op
= &ext4_file_inode_operations
;
1408 ea_inode
->i_fop
= &ext4_file_operations
;
1409 ext4_set_aops(ea_inode
);
1410 ext4_xattr_inode_set_class(ea_inode
);
1411 unlock_new_inode(ea_inode
);
1412 ext4_xattr_inode_set_ref(ea_inode
, 1);
1413 ext4_xattr_inode_set_hash(ea_inode
, hash
);
1414 err
= ext4_mark_inode_dirty(handle
, ea_inode
);
1416 err
= ext4_inode_attach_jinode(ea_inode
);
1419 return ERR_PTR(err
);
1423 * Xattr inodes are shared therefore quota charging is performed
1424 * at a higher level.
1426 dquot_free_inode(ea_inode
);
1427 dquot_drop(ea_inode
);
1428 inode_lock(ea_inode
);
1429 ea_inode
->i_flags
|= S_NOQUOTA
;
1430 inode_unlock(ea_inode
);
1436 static struct inode
*
1437 ext4_xattr_inode_cache_find(struct inode
*inode
, const void *value
,
1438 size_t value_len
, u32 hash
)
1440 struct inode
*ea_inode
;
1441 struct mb_cache_entry
*ce
;
1442 struct mb_cache
*ea_inode_cache
= EA_INODE_CACHE(inode
);
1445 if (!ea_inode_cache
)
1448 ce
= mb_cache_entry_find_first(ea_inode_cache
, hash
);
1452 ea_data
= ext4_kvmalloc(value_len
, GFP_NOFS
);
1454 mb_cache_entry_put(ea_inode_cache
, ce
);
1459 ea_inode
= ext4_iget(inode
->i_sb
, ce
->e_value
);
1460 if (!IS_ERR(ea_inode
) &&
1461 !is_bad_inode(ea_inode
) &&
1462 (EXT4_I(ea_inode
)->i_flags
& EXT4_EA_INODE_FL
) &&
1463 i_size_read(ea_inode
) == value_len
&&
1464 !ext4_xattr_inode_read(ea_inode
, ea_data
, value_len
) &&
1465 !ext4_xattr_inode_verify_hashes(ea_inode
, NULL
, ea_data
,
1467 !memcmp(value
, ea_data
, value_len
)) {
1468 mb_cache_entry_touch(ea_inode_cache
, ce
);
1469 mb_cache_entry_put(ea_inode_cache
, ce
);
1474 if (!IS_ERR(ea_inode
))
1476 ce
= mb_cache_entry_find_next(ea_inode_cache
, ce
);
1483 * Add value of the EA in an inode.
1485 static int ext4_xattr_inode_lookup_create(handle_t
*handle
, struct inode
*inode
,
1486 const void *value
, size_t value_len
,
1487 struct inode
**ret_inode
)
1489 struct inode
*ea_inode
;
1493 hash
= ext4_xattr_inode_hash(EXT4_SB(inode
->i_sb
), value
, value_len
);
1494 ea_inode
= ext4_xattr_inode_cache_find(inode
, value
, value_len
, hash
);
1496 err
= ext4_xattr_inode_inc_ref(handle
, ea_inode
);
1502 *ret_inode
= ea_inode
;
1506 /* Create an inode for the EA value */
1507 ea_inode
= ext4_xattr_inode_create(handle
, inode
, hash
);
1508 if (IS_ERR(ea_inode
))
1509 return PTR_ERR(ea_inode
);
1511 err
= ext4_xattr_inode_write(handle
, ea_inode
, value
, value_len
);
1513 ext4_xattr_inode_dec_ref(handle
, ea_inode
);
1518 if (EA_INODE_CACHE(inode
))
1519 mb_cache_entry_create(EA_INODE_CACHE(inode
), GFP_NOFS
, hash
,
1520 ea_inode
->i_ino
, true /* reusable */);
1522 *ret_inode
= ea_inode
;
1527 * Reserve min(block_size/8, 1024) bytes for xattr entries/names if ea_inode
1528 * feature is enabled.
1530 #define EXT4_XATTR_BLOCK_RESERVE(inode) min(i_blocksize(inode)/8, 1024U)
1532 static int ext4_xattr_set_entry(struct ext4_xattr_info
*i
,
1533 struct ext4_xattr_search
*s
,
1534 handle_t
*handle
, struct inode
*inode
,
1537 struct ext4_xattr_entry
*last
;
1538 struct ext4_xattr_entry
*here
= s
->here
;
1539 size_t min_offs
= s
->end
- s
->base
, name_len
= strlen(i
->name
);
1540 int in_inode
= i
->in_inode
;
1541 struct inode
*old_ea_inode
= NULL
;
1542 struct inode
*new_ea_inode
= NULL
;
1543 size_t old_size
, new_size
;
1546 /* Space used by old and new values. */
1547 old_size
= (!s
->not_found
&& !here
->e_value_inum
) ?
1548 EXT4_XATTR_SIZE(le32_to_cpu(here
->e_value_size
)) : 0;
1549 new_size
= (i
->value
&& !in_inode
) ? EXT4_XATTR_SIZE(i
->value_len
) : 0;
1552 * Optimization for the simple case when old and new values have the
1553 * same padded sizes. Not applicable if external inodes are involved.
1555 if (new_size
&& new_size
== old_size
) {
1556 size_t offs
= le16_to_cpu(here
->e_value_offs
);
1557 void *val
= s
->base
+ offs
;
1559 here
->e_value_size
= cpu_to_le32(i
->value_len
);
1560 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
1561 memset(val
, 0, new_size
);
1563 memcpy(val
, i
->value
, i
->value_len
);
1564 /* Clear padding bytes. */
1565 memset(val
+ i
->value_len
, 0, new_size
- i
->value_len
);
1570 /* Compute min_offs and last. */
1572 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
1573 if (!last
->e_value_inum
&& last
->e_value_size
) {
1574 size_t offs
= le16_to_cpu(last
->e_value_offs
);
1575 if (offs
< min_offs
)
1580 /* Check whether we have enough space. */
1584 free
= min_offs
- ((void *)last
- s
->base
) - sizeof(__u32
);
1586 free
+= EXT4_XATTR_LEN(name_len
) + old_size
;
1588 if (free
< EXT4_XATTR_LEN(name_len
) + new_size
) {
1594 * If storing the value in an external inode is an option,
1595 * reserve space for xattr entries/names in the external
1596 * attribute block so that a long value does not occupy the
1597 * whole space and prevent futher entries being added.
1599 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
1600 new_size
&& is_block
&&
1601 (min_offs
+ old_size
- new_size
) <
1602 EXT4_XATTR_BLOCK_RESERVE(inode
)) {
1609 * Getting access to old and new ea inodes is subject to failures.
1610 * Finish that work before doing any modifications to the xattr data.
1612 if (!s
->not_found
&& here
->e_value_inum
) {
1613 ret
= ext4_xattr_inode_iget(inode
,
1614 le32_to_cpu(here
->e_value_inum
),
1615 le32_to_cpu(here
->e_hash
),
1618 old_ea_inode
= NULL
;
1622 if (i
->value
&& in_inode
) {
1623 WARN_ON_ONCE(!i
->value_len
);
1625 ret
= ext4_xattr_inode_alloc_quota(inode
, i
->value_len
);
1629 ret
= ext4_xattr_inode_lookup_create(handle
, inode
, i
->value
,
1633 new_ea_inode
= NULL
;
1634 ext4_xattr_inode_free_quota(inode
, NULL
, i
->value_len
);
1640 /* We are ready to release ref count on the old_ea_inode. */
1641 ret
= ext4_xattr_inode_dec_ref(handle
, old_ea_inode
);
1643 /* Release newly required ref count on new_ea_inode. */
1647 err
= ext4_xattr_inode_dec_ref(handle
,
1650 ext4_warning_inode(new_ea_inode
,
1651 "dec ref new_ea_inode err=%d",
1653 ext4_xattr_inode_free_quota(inode
, new_ea_inode
,
1659 ext4_xattr_inode_free_quota(inode
, old_ea_inode
,
1660 le32_to_cpu(here
->e_value_size
));
1663 /* No failures allowed past this point. */
1665 if (!s
->not_found
&& here
->e_value_offs
) {
1666 /* Remove the old value. */
1667 void *first_val
= s
->base
+ min_offs
;
1668 size_t offs
= le16_to_cpu(here
->e_value_offs
);
1669 void *val
= s
->base
+ offs
;
1671 memmove(first_val
+ old_size
, first_val
, val
- first_val
);
1672 memset(first_val
, 0, old_size
);
1673 min_offs
+= old_size
;
1675 /* Adjust all value offsets. */
1677 while (!IS_LAST_ENTRY(last
)) {
1678 size_t o
= le16_to_cpu(last
->e_value_offs
);
1680 if (!last
->e_value_inum
&&
1681 last
->e_value_size
&& o
< offs
)
1682 last
->e_value_offs
= cpu_to_le16(o
+ old_size
);
1683 last
= EXT4_XATTR_NEXT(last
);
1688 /* Remove old name. */
1689 size_t size
= EXT4_XATTR_LEN(name_len
);
1691 last
= ENTRY((void *)last
- size
);
1692 memmove(here
, (void *)here
+ size
,
1693 (void *)last
- (void *)here
+ sizeof(__u32
));
1694 memset(last
, 0, size
);
1695 } else if (s
->not_found
) {
1696 /* Insert new name. */
1697 size_t size
= EXT4_XATTR_LEN(name_len
);
1698 size_t rest
= (void *)last
- (void *)here
+ sizeof(__u32
);
1700 memmove((void *)here
+ size
, here
, rest
);
1701 memset(here
, 0, size
);
1702 here
->e_name_index
= i
->name_index
;
1703 here
->e_name_len
= name_len
;
1704 memcpy(here
->e_name
, i
->name
, name_len
);
1706 /* This is an update, reset value info. */
1707 here
->e_value_inum
= 0;
1708 here
->e_value_offs
= 0;
1709 here
->e_value_size
= 0;
1713 /* Insert new value. */
1715 here
->e_value_inum
= cpu_to_le32(new_ea_inode
->i_ino
);
1716 } else if (i
->value_len
) {
1717 void *val
= s
->base
+ min_offs
- new_size
;
1719 here
->e_value_offs
= cpu_to_le16(min_offs
- new_size
);
1720 if (i
->value
== EXT4_ZERO_XATTR_VALUE
) {
1721 memset(val
, 0, new_size
);
1723 memcpy(val
, i
->value
, i
->value_len
);
1724 /* Clear padding bytes. */
1725 memset(val
+ i
->value_len
, 0,
1726 new_size
- i
->value_len
);
1729 here
->e_value_size
= cpu_to_le32(i
->value_len
);
1736 /* Entry hash calculation. */
1741 * Feed crc32c hash instead of the raw value for entry
1742 * hash calculation. This is to avoid walking
1743 * potentially long value buffer again.
1745 crc32c_hash
= cpu_to_le32(
1746 ext4_xattr_inode_get_hash(new_ea_inode
));
1747 hash
= ext4_xattr_hash_entry(here
->e_name
,
1750 } else if (is_block
) {
1751 __le32
*value
= s
->base
+ le16_to_cpu(
1752 here
->e_value_offs
);
1754 hash
= ext4_xattr_hash_entry(here
->e_name
,
1755 here
->e_name_len
, value
,
1758 here
->e_hash
= hash
;
1762 ext4_xattr_rehash((struct ext4_xattr_header
*)s
->base
);
1771 struct ext4_xattr_block_find
{
1772 struct ext4_xattr_search s
;
1773 struct buffer_head
*bh
;
1777 ext4_xattr_block_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
1778 struct ext4_xattr_block_find
*bs
)
1780 struct super_block
*sb
= inode
->i_sb
;
1783 ea_idebug(inode
, "name=%d.%s, value=%p, value_len=%ld",
1784 i
->name_index
, i
->name
, i
->value
, (long)i
->value_len
);
1786 if (EXT4_I(inode
)->i_file_acl
) {
1787 /* The inode already has an extended attribute block. */
1788 bs
->bh
= sb_bread(sb
, EXT4_I(inode
)->i_file_acl
);
1792 ea_bdebug(bs
->bh
, "b_count=%d, refcount=%d",
1793 atomic_read(&(bs
->bh
->b_count
)),
1794 le32_to_cpu(BHDR(bs
->bh
)->h_refcount
));
1795 if (ext4_xattr_check_block(inode
, bs
->bh
)) {
1796 EXT4_ERROR_INODE(inode
, "bad block %llu",
1797 EXT4_I(inode
)->i_file_acl
);
1798 error
= -EFSCORRUPTED
;
1801 /* Find the named attribute. */
1802 bs
->s
.base
= BHDR(bs
->bh
);
1803 bs
->s
.first
= BFIRST(bs
->bh
);
1804 bs
->s
.end
= bs
->bh
->b_data
+ bs
->bh
->b_size
;
1805 bs
->s
.here
= bs
->s
.first
;
1806 error
= ext4_xattr_find_entry(&bs
->s
.here
, i
->name_index
,
1808 if (error
&& error
!= -ENODATA
)
1810 bs
->s
.not_found
= error
;
1819 ext4_xattr_block_set(handle_t
*handle
, struct inode
*inode
,
1820 struct ext4_xattr_info
*i
,
1821 struct ext4_xattr_block_find
*bs
)
1823 struct super_block
*sb
= inode
->i_sb
;
1824 struct buffer_head
*new_bh
= NULL
;
1825 struct ext4_xattr_search s_copy
= bs
->s
;
1826 struct ext4_xattr_search
*s
= &s_copy
;
1827 struct mb_cache_entry
*ce
= NULL
;
1829 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
1830 struct inode
*ea_inode
= NULL
, *tmp_inode
;
1831 size_t old_ea_inode_quota
= 0;
1832 unsigned int ea_ino
;
1835 #define header(x) ((struct ext4_xattr_header *)(x))
1838 BUFFER_TRACE(bs
->bh
, "get_write_access");
1839 error
= ext4_journal_get_write_access(handle
, bs
->bh
);
1842 lock_buffer(bs
->bh
);
1844 if (header(s
->base
)->h_refcount
== cpu_to_le32(1)) {
1845 __u32 hash
= le32_to_cpu(BHDR(bs
->bh
)->h_hash
);
1848 * This must happen under buffer lock for
1849 * ext4_xattr_block_set() to reliably detect modified
1853 mb_cache_entry_delete(ea_block_cache
, hash
,
1855 ea_bdebug(bs
->bh
, "modifying in-place");
1856 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
,
1857 true /* is_block */);
1858 ext4_xattr_block_csum_set(inode
, bs
->bh
);
1859 unlock_buffer(bs
->bh
);
1860 if (error
== -EFSCORRUPTED
)
1863 error
= ext4_handle_dirty_metadata(handle
,
1870 int offset
= (char *)s
->here
- bs
->bh
->b_data
;
1872 unlock_buffer(bs
->bh
);
1873 ea_bdebug(bs
->bh
, "cloning");
1874 s
->base
= kmalloc(bs
->bh
->b_size
, GFP_NOFS
);
1876 if (s
->base
== NULL
)
1878 memcpy(s
->base
, BHDR(bs
->bh
), bs
->bh
->b_size
);
1879 s
->first
= ENTRY(header(s
->base
)+1);
1880 header(s
->base
)->h_refcount
= cpu_to_le32(1);
1881 s
->here
= ENTRY(s
->base
+ offset
);
1882 s
->end
= s
->base
+ bs
->bh
->b_size
;
1885 * If existing entry points to an xattr inode, we need
1886 * to prevent ext4_xattr_set_entry() from decrementing
1887 * ref count on it because the reference belongs to the
1888 * original block. In this case, make the entry look
1889 * like it has an empty value.
1891 if (!s
->not_found
&& s
->here
->e_value_inum
) {
1892 ea_ino
= le32_to_cpu(s
->here
->e_value_inum
);
1893 error
= ext4_xattr_inode_iget(inode
, ea_ino
,
1894 le32_to_cpu(s
->here
->e_hash
),
1899 if (!ext4_test_inode_state(tmp_inode
,
1900 EXT4_STATE_LUSTRE_EA_INODE
)) {
1902 * Defer quota free call for previous
1903 * inode until success is guaranteed.
1905 old_ea_inode_quota
= le32_to_cpu(
1906 s
->here
->e_value_size
);
1910 s
->here
->e_value_inum
= 0;
1911 s
->here
->e_value_size
= 0;
1915 /* Allocate a buffer where we construct the new block. */
1916 s
->base
= kzalloc(sb
->s_blocksize
, GFP_NOFS
);
1917 /* assert(header == s->base) */
1919 if (s
->base
== NULL
)
1921 header(s
->base
)->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
1922 header(s
->base
)->h_blocks
= cpu_to_le32(1);
1923 header(s
->base
)->h_refcount
= cpu_to_le32(1);
1924 s
->first
= ENTRY(header(s
->base
)+1);
1925 s
->here
= ENTRY(header(s
->base
)+1);
1926 s
->end
= s
->base
+ sb
->s_blocksize
;
1929 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
, true /* is_block */);
1930 if (error
== -EFSCORRUPTED
)
1935 if (i
->value
&& s
->here
->e_value_inum
) {
1937 * A ref count on ea_inode has been taken as part of the call to
1938 * ext4_xattr_set_entry() above. We would like to drop this
1939 * extra ref but we have to wait until the xattr block is
1940 * initialized and has its own ref count on the ea_inode.
1942 ea_ino
= le32_to_cpu(s
->here
->e_value_inum
);
1943 error
= ext4_xattr_inode_iget(inode
, ea_ino
,
1944 le32_to_cpu(s
->here
->e_hash
),
1953 if (!IS_LAST_ENTRY(s
->first
)) {
1954 new_bh
= ext4_xattr_block_cache_find(inode
, header(s
->base
),
1957 /* We found an identical block in the cache. */
1958 if (new_bh
== bs
->bh
)
1959 ea_bdebug(new_bh
, "keeping");
1963 WARN_ON_ONCE(dquot_initialize_needed(inode
));
1965 /* The old block is released after updating
1967 error
= dquot_alloc_block(inode
,
1968 EXT4_C2B(EXT4_SB(sb
), 1));
1971 BUFFER_TRACE(new_bh
, "get_write_access");
1972 error
= ext4_journal_get_write_access(handle
,
1976 lock_buffer(new_bh
);
1978 * We have to be careful about races with
1979 * freeing, rehashing or adding references to
1980 * xattr block. Once we hold buffer lock xattr
1981 * block's state is stable so we can check
1982 * whether the block got freed / rehashed or
1983 * not. Since we unhash mbcache entry under
1984 * buffer lock when freeing / rehashing xattr
1985 * block, checking whether entry is still
1986 * hashed is reliable. Same rules hold for
1987 * e_reusable handling.
1989 if (hlist_bl_unhashed(&ce
->e_hash_list
) ||
1992 * Undo everything and check mbcache
1995 unlock_buffer(new_bh
);
1996 dquot_free_block(inode
,
1997 EXT4_C2B(EXT4_SB(sb
),
2000 mb_cache_entry_put(ea_block_cache
, ce
);
2005 ref
= le32_to_cpu(BHDR(new_bh
)->h_refcount
) + 1;
2006 BHDR(new_bh
)->h_refcount
= cpu_to_le32(ref
);
2007 if (ref
>= EXT4_XATTR_REFCOUNT_MAX
)
2009 ea_bdebug(new_bh
, "reusing; refcount now=%d",
2011 ext4_xattr_block_csum_set(inode
, new_bh
);
2012 unlock_buffer(new_bh
);
2013 error
= ext4_handle_dirty_metadata(handle
,
2019 mb_cache_entry_touch(ea_block_cache
, ce
);
2020 mb_cache_entry_put(ea_block_cache
, ce
);
2022 } else if (bs
->bh
&& s
->base
== bs
->bh
->b_data
) {
2023 /* We were modifying this block in-place. */
2024 ea_bdebug(bs
->bh
, "keeping this block");
2025 ext4_xattr_block_cache_insert(ea_block_cache
, bs
->bh
);
2029 /* We need to allocate a new block */
2030 ext4_fsblk_t goal
, block
;
2032 WARN_ON_ONCE(dquot_initialize_needed(inode
));
2034 goal
= ext4_group_first_block_no(sb
,
2035 EXT4_I(inode
)->i_block_group
);
2037 /* non-extent files can't have physical blocks past 2^32 */
2038 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
2039 goal
= goal
& EXT4_MAX_BLOCK_FILE_PHYS
;
2041 block
= ext4_new_meta_blocks(handle
, inode
, goal
, 0,
2046 if (!(ext4_test_inode_flag(inode
, EXT4_INODE_EXTENTS
)))
2047 BUG_ON(block
> EXT4_MAX_BLOCK_FILE_PHYS
);
2049 ea_idebug(inode
, "creating block %llu",
2050 (unsigned long long)block
);
2052 new_bh
= sb_getblk(sb
, block
);
2053 if (unlikely(!new_bh
)) {
2056 ext4_free_blocks(handle
, inode
, NULL
, block
, 1,
2057 EXT4_FREE_BLOCKS_METADATA
);
2060 error
= ext4_xattr_inode_inc_ref_all(handle
, inode
,
2061 ENTRY(header(s
->base
)+1));
2065 /* Drop the extra ref on ea_inode. */
2066 error
= ext4_xattr_inode_dec_ref(handle
,
2069 ext4_warning_inode(ea_inode
,
2076 lock_buffer(new_bh
);
2077 error
= ext4_journal_get_create_access(handle
, new_bh
);
2079 unlock_buffer(new_bh
);
2083 memcpy(new_bh
->b_data
, s
->base
, new_bh
->b_size
);
2084 ext4_xattr_block_csum_set(inode
, new_bh
);
2085 set_buffer_uptodate(new_bh
);
2086 unlock_buffer(new_bh
);
2087 ext4_xattr_block_cache_insert(ea_block_cache
, new_bh
);
2088 error
= ext4_handle_dirty_metadata(handle
, inode
,
2095 if (old_ea_inode_quota
)
2096 ext4_xattr_inode_free_quota(inode
, NULL
, old_ea_inode_quota
);
2098 /* Update the inode. */
2099 EXT4_I(inode
)->i_file_acl
= new_bh
? new_bh
->b_blocknr
: 0;
2101 /* Drop the previous xattr block. */
2102 if (bs
->bh
&& bs
->bh
!= new_bh
) {
2103 struct ext4_xattr_inode_array
*ea_inode_array
= NULL
;
2105 ext4_xattr_release_block(handle
, inode
, bs
->bh
,
2107 0 /* extra_credits */);
2108 ext4_xattr_inode_array_free(ea_inode_array
);
2116 error2
= ext4_xattr_inode_dec_ref(handle
, ea_inode
);
2118 ext4_warning_inode(ea_inode
, "dec ref error=%d",
2121 /* If there was an error, revert the quota charge. */
2123 ext4_xattr_inode_free_quota(inode
, ea_inode
,
2124 i_size_read(ea_inode
));
2128 mb_cache_entry_put(ea_block_cache
, ce
);
2130 if (!(bs
->bh
&& s
->base
== bs
->bh
->b_data
))
2136 dquot_free_block(inode
, EXT4_C2B(EXT4_SB(sb
), 1));
2140 EXT4_ERROR_INODE(inode
, "bad block %llu",
2141 EXT4_I(inode
)->i_file_acl
);
2147 int ext4_xattr_ibody_find(struct inode
*inode
, struct ext4_xattr_info
*i
,
2148 struct ext4_xattr_ibody_find
*is
)
2150 struct ext4_xattr_ibody_header
*header
;
2151 struct ext4_inode
*raw_inode
;
2154 if (EXT4_I(inode
)->i_extra_isize
== 0)
2156 raw_inode
= ext4_raw_inode(&is
->iloc
);
2157 header
= IHDR(inode
, raw_inode
);
2158 is
->s
.base
= is
->s
.first
= IFIRST(header
);
2159 is
->s
.here
= is
->s
.first
;
2160 is
->s
.end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
2161 if (ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
2162 error
= xattr_check_inode(inode
, header
, is
->s
.end
);
2165 /* Find the named attribute. */
2166 error
= ext4_xattr_find_entry(&is
->s
.here
, i
->name_index
,
2168 if (error
&& error
!= -ENODATA
)
2170 is
->s
.not_found
= error
;
2175 int ext4_xattr_ibody_inline_set(handle_t
*handle
, struct inode
*inode
,
2176 struct ext4_xattr_info
*i
,
2177 struct ext4_xattr_ibody_find
*is
)
2179 struct ext4_xattr_ibody_header
*header
;
2180 struct ext4_xattr_search
*s
= &is
->s
;
2183 if (EXT4_I(inode
)->i_extra_isize
== 0)
2185 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
, false /* is_block */);
2187 if (error
== -ENOSPC
&&
2188 ext4_has_inline_data(inode
)) {
2189 error
= ext4_try_to_evict_inline_data(handle
, inode
,
2190 EXT4_XATTR_LEN(strlen(i
->name
) +
2191 EXT4_XATTR_SIZE(i
->value_len
)));
2194 error
= ext4_xattr_ibody_find(inode
, i
, is
);
2197 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
,
2198 false /* is_block */);
2203 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
2204 if (!IS_LAST_ENTRY(s
->first
)) {
2205 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
2206 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
2208 header
->h_magic
= cpu_to_le32(0);
2209 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
2214 static int ext4_xattr_ibody_set(handle_t
*handle
, struct inode
*inode
,
2215 struct ext4_xattr_info
*i
,
2216 struct ext4_xattr_ibody_find
*is
)
2218 struct ext4_xattr_ibody_header
*header
;
2219 struct ext4_xattr_search
*s
= &is
->s
;
2222 if (EXT4_I(inode
)->i_extra_isize
== 0)
2224 error
= ext4_xattr_set_entry(i
, s
, handle
, inode
, false /* is_block */);
2227 header
= IHDR(inode
, ext4_raw_inode(&is
->iloc
));
2228 if (!IS_LAST_ENTRY(s
->first
)) {
2229 header
->h_magic
= cpu_to_le32(EXT4_XATTR_MAGIC
);
2230 ext4_set_inode_state(inode
, EXT4_STATE_XATTR
);
2232 header
->h_magic
= cpu_to_le32(0);
2233 ext4_clear_inode_state(inode
, EXT4_STATE_XATTR
);
2238 static int ext4_xattr_value_same(struct ext4_xattr_search
*s
,
2239 struct ext4_xattr_info
*i
)
2243 /* When e_value_inum is set the value is stored externally. */
2244 if (s
->here
->e_value_inum
)
2246 if (le32_to_cpu(s
->here
->e_value_size
) != i
->value_len
)
2248 value
= ((void *)s
->base
) + le16_to_cpu(s
->here
->e_value_offs
);
2249 return !memcmp(value
, i
->value
, i
->value_len
);
2252 static struct buffer_head
*ext4_xattr_get_block(struct inode
*inode
)
2254 struct buffer_head
*bh
;
2257 if (!EXT4_I(inode
)->i_file_acl
)
2259 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2261 return ERR_PTR(-EIO
);
2262 error
= ext4_xattr_check_block(inode
, bh
);
2264 return ERR_PTR(error
);
2269 * ext4_xattr_set_handle()
2271 * Create, replace or remove an extended attribute for this inode. Value
2272 * is NULL to remove an existing extended attribute, and non-NULL to
2273 * either replace an existing extended attribute, or create a new extended
2274 * attribute. The flags XATTR_REPLACE and XATTR_CREATE
2275 * specify that an extended attribute must exist and must not exist
2276 * previous to the call, respectively.
2278 * Returns 0, or a negative error number on failure.
2281 ext4_xattr_set_handle(handle_t
*handle
, struct inode
*inode
, int name_index
,
2282 const char *name
, const void *value
, size_t value_len
,
2285 struct ext4_xattr_info i
= {
2286 .name_index
= name_index
,
2289 .value_len
= value_len
,
2292 struct ext4_xattr_ibody_find is
= {
2293 .s
= { .not_found
= -ENODATA
, },
2295 struct ext4_xattr_block_find bs
= {
2296 .s
= { .not_found
= -ENODATA
, },
2303 if (strlen(name
) > 255)
2306 ext4_write_lock_xattr(inode
, &no_expand
);
2308 /* Check journal credits under write lock. */
2309 if (ext4_handle_valid(handle
)) {
2310 struct buffer_head
*bh
;
2313 bh
= ext4_xattr_get_block(inode
);
2315 error
= PTR_ERR(bh
);
2319 credits
= __ext4_xattr_set_credits(inode
->i_sb
, inode
, bh
,
2321 flags
& XATTR_CREATE
);
2324 if (!ext4_handle_has_enough_credits(handle
, credits
)) {
2330 error
= ext4_reserve_inode_write(handle
, inode
, &is
.iloc
);
2334 if (ext4_test_inode_state(inode
, EXT4_STATE_NEW
)) {
2335 struct ext4_inode
*raw_inode
= ext4_raw_inode(&is
.iloc
);
2336 memset(raw_inode
, 0, EXT4_SB(inode
->i_sb
)->s_inode_size
);
2337 ext4_clear_inode_state(inode
, EXT4_STATE_NEW
);
2340 error
= ext4_xattr_ibody_find(inode
, &i
, &is
);
2344 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
2347 if (is
.s
.not_found
&& bs
.s
.not_found
) {
2349 if (flags
& XATTR_REPLACE
)
2356 if (flags
& XATTR_CREATE
)
2361 if (!is
.s
.not_found
)
2362 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
2363 else if (!bs
.s
.not_found
)
2364 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2367 /* Xattr value did not change? Save us some work and bail out */
2368 if (!is
.s
.not_found
&& ext4_xattr_value_same(&is
.s
, &i
))
2370 if (!bs
.s
.not_found
&& ext4_xattr_value_same(&bs
.s
, &i
))
2373 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2374 (EXT4_XATTR_SIZE(i
.value_len
) >
2375 EXT4_XATTR_MIN_LARGE_EA_SIZE(inode
->i_sb
->s_blocksize
)))
2378 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, &is
);
2379 if (!error
&& !bs
.s
.not_found
) {
2381 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2382 } else if (error
== -ENOSPC
) {
2383 if (EXT4_I(inode
)->i_file_acl
&& !bs
.s
.base
) {
2384 error
= ext4_xattr_block_find(inode
, &i
, &bs
);
2388 error
= ext4_xattr_block_set(handle
, inode
, &i
, &bs
);
2389 if (!error
&& !is
.s
.not_found
) {
2391 error
= ext4_xattr_ibody_set(handle
, inode
, &i
,
2393 } else if (error
== -ENOSPC
) {
2395 * Xattr does not fit in the block, store at
2396 * external inode if possible.
2398 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2407 ext4_xattr_update_super_block(handle
, inode
->i_sb
);
2408 inode
->i_ctime
= current_time(inode
);
2411 error
= ext4_mark_iloc_dirty(handle
, inode
, &is
.iloc
);
2413 * The bh is consumed by ext4_mark_iloc_dirty, even with
2418 ext4_handle_sync(handle
);
2424 ext4_write_unlock_xattr(inode
, &no_expand
);
2428 int ext4_xattr_set_credits(struct inode
*inode
, size_t value_len
,
2429 bool is_create
, int *credits
)
2431 struct buffer_head
*bh
;
2436 if (!EXT4_SB(inode
->i_sb
)->s_journal
)
2439 down_read(&EXT4_I(inode
)->xattr_sem
);
2441 bh
= ext4_xattr_get_block(inode
);
2445 *credits
= __ext4_xattr_set_credits(inode
->i_sb
, inode
, bh
,
2446 value_len
, is_create
);
2451 up_read(&EXT4_I(inode
)->xattr_sem
);
2458 * Like ext4_xattr_set_handle, but start from an inode. This extended
2459 * attribute modification is a filesystem transaction by itself.
2461 * Returns 0, or a negative error number on failure.
2464 ext4_xattr_set(struct inode
*inode
, int name_index
, const char *name
,
2465 const void *value
, size_t value_len
, int flags
)
2468 struct super_block
*sb
= inode
->i_sb
;
2469 int error
, retries
= 0;
2472 error
= dquot_initialize(inode
);
2477 error
= ext4_xattr_set_credits(inode
, value_len
, flags
& XATTR_CREATE
,
2482 handle
= ext4_journal_start(inode
, EXT4_HT_XATTR
, credits
);
2483 if (IS_ERR(handle
)) {
2484 error
= PTR_ERR(handle
);
2488 error
= ext4_xattr_set_handle(handle
, inode
, name_index
, name
,
2489 value
, value_len
, flags
);
2490 error2
= ext4_journal_stop(handle
);
2491 if (error
== -ENOSPC
&&
2492 ext4_should_retry_alloc(sb
, &retries
))
2502 * Shift the EA entries in the inode to create space for the increased
2505 static void ext4_xattr_shift_entries(struct ext4_xattr_entry
*entry
,
2506 int value_offs_shift
, void *to
,
2507 void *from
, size_t n
)
2509 struct ext4_xattr_entry
*last
= entry
;
2512 /* We always shift xattr headers further thus offsets get lower */
2513 BUG_ON(value_offs_shift
> 0);
2515 /* Adjust the value offsets of the entries */
2516 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
2517 if (!last
->e_value_inum
&& last
->e_value_size
) {
2518 new_offs
= le16_to_cpu(last
->e_value_offs
) +
2520 last
->e_value_offs
= cpu_to_le16(new_offs
);
2523 /* Shift the entries by n bytes */
2524 memmove(to
, from
, n
);
2528 * Move xattr pointed to by 'entry' from inode into external xattr block
2530 static int ext4_xattr_move_to_block(handle_t
*handle
, struct inode
*inode
,
2531 struct ext4_inode
*raw_inode
,
2532 struct ext4_xattr_entry
*entry
)
2534 struct ext4_xattr_ibody_find
*is
= NULL
;
2535 struct ext4_xattr_block_find
*bs
= NULL
;
2536 char *buffer
= NULL
, *b_entry_name
= NULL
;
2537 size_t value_size
= le32_to_cpu(entry
->e_value_size
);
2538 struct ext4_xattr_info i
= {
2541 .name_index
= entry
->e_name_index
,
2542 .in_inode
= !!entry
->e_value_inum
,
2544 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
2547 is
= kzalloc(sizeof(struct ext4_xattr_ibody_find
), GFP_NOFS
);
2548 bs
= kzalloc(sizeof(struct ext4_xattr_block_find
), GFP_NOFS
);
2549 buffer
= kmalloc(value_size
, GFP_NOFS
);
2550 b_entry_name
= kmalloc(entry
->e_name_len
+ 1, GFP_NOFS
);
2551 if (!is
|| !bs
|| !buffer
|| !b_entry_name
) {
2556 is
->s
.not_found
= -ENODATA
;
2557 bs
->s
.not_found
= -ENODATA
;
2561 /* Save the entry name and the entry value */
2562 if (entry
->e_value_inum
) {
2563 error
= ext4_xattr_inode_get(inode
, entry
, buffer
, value_size
);
2567 size_t value_offs
= le16_to_cpu(entry
->e_value_offs
);
2568 memcpy(buffer
, (void *)IFIRST(header
) + value_offs
, value_size
);
2571 memcpy(b_entry_name
, entry
->e_name
, entry
->e_name_len
);
2572 b_entry_name
[entry
->e_name_len
] = '\0';
2573 i
.name
= b_entry_name
;
2575 error
= ext4_get_inode_loc(inode
, &is
->iloc
);
2579 error
= ext4_xattr_ibody_find(inode
, &i
, is
);
2583 /* Remove the chosen entry from the inode */
2584 error
= ext4_xattr_ibody_set(handle
, inode
, &i
, is
);
2589 i
.value_len
= value_size
;
2590 error
= ext4_xattr_block_find(inode
, &i
, bs
);
2594 /* Add entry which was removed from the inode into the block */
2595 error
= ext4_xattr_block_set(handle
, inode
, &i
, bs
);
2600 kfree(b_entry_name
);
2603 brelse(is
->iloc
.bh
);
2610 static int ext4_xattr_make_inode_space(handle_t
*handle
, struct inode
*inode
,
2611 struct ext4_inode
*raw_inode
,
2612 int isize_diff
, size_t ifree
,
2613 size_t bfree
, int *total_ino
)
2615 struct ext4_xattr_ibody_header
*header
= IHDR(inode
, raw_inode
);
2616 struct ext4_xattr_entry
*small_entry
;
2617 struct ext4_xattr_entry
*entry
;
2618 struct ext4_xattr_entry
*last
;
2619 unsigned int entry_size
; /* EA entry size */
2620 unsigned int total_size
; /* EA entry size + value size */
2621 unsigned int min_total_size
;
2624 while (isize_diff
> ifree
) {
2627 min_total_size
= ~0U;
2628 last
= IFIRST(header
);
2629 /* Find the entry best suited to be pushed into EA block */
2630 for (; !IS_LAST_ENTRY(last
); last
= EXT4_XATTR_NEXT(last
)) {
2631 total_size
= EXT4_XATTR_LEN(last
->e_name_len
);
2632 if (!last
->e_value_inum
)
2633 total_size
+= EXT4_XATTR_SIZE(
2634 le32_to_cpu(last
->e_value_size
));
2635 if (total_size
<= bfree
&&
2636 total_size
< min_total_size
) {
2637 if (total_size
+ ifree
< isize_diff
) {
2641 min_total_size
= total_size
;
2646 if (entry
== NULL
) {
2647 if (small_entry
== NULL
)
2649 entry
= small_entry
;
2652 entry_size
= EXT4_XATTR_LEN(entry
->e_name_len
);
2653 total_size
= entry_size
;
2654 if (!entry
->e_value_inum
)
2655 total_size
+= EXT4_XATTR_SIZE(
2656 le32_to_cpu(entry
->e_value_size
));
2657 error
= ext4_xattr_move_to_block(handle
, inode
, raw_inode
,
2662 *total_ino
-= entry_size
;
2663 ifree
+= total_size
;
2664 bfree
-= total_size
;
2671 * Expand an inode by new_extra_isize bytes when EAs are present.
2672 * Returns 0 on success or negative error number on failure.
2674 int ext4_expand_extra_isize_ea(struct inode
*inode
, int new_extra_isize
,
2675 struct ext4_inode
*raw_inode
, handle_t
*handle
)
2677 struct ext4_xattr_ibody_header
*header
;
2678 struct buffer_head
*bh
;
2679 struct ext4_sb_info
*sbi
= EXT4_SB(inode
->i_sb
);
2680 static unsigned int mnt_count
;
2682 size_t ifree
, bfree
;
2685 int error
= 0, tried_min_extra_isize
= 0;
2686 int s_min_extra_isize
= le16_to_cpu(sbi
->s_es
->s_min_extra_isize
);
2687 int isize_diff
; /* How much do we need to grow i_extra_isize */
2690 isize_diff
= new_extra_isize
- EXT4_I(inode
)->i_extra_isize
;
2691 if (EXT4_I(inode
)->i_extra_isize
>= new_extra_isize
)
2694 header
= IHDR(inode
, raw_inode
);
2697 * Check if enough free space is available in the inode to shift the
2698 * entries ahead by new_extra_isize.
2701 base
= IFIRST(header
);
2702 end
= (void *)raw_inode
+ EXT4_SB(inode
->i_sb
)->s_inode_size
;
2703 min_offs
= end
- base
;
2704 total_ino
= sizeof(struct ext4_xattr_ibody_header
);
2706 error
= xattr_check_inode(inode
, header
, end
);
2710 ifree
= ext4_xattr_free_space(base
, &min_offs
, base
, &total_ino
);
2711 if (ifree
>= isize_diff
)
2715 * Enough free space isn't available in the inode, check if
2716 * EA block can hold new_extra_isize bytes.
2718 if (EXT4_I(inode
)->i_file_acl
) {
2719 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2723 if (ext4_xattr_check_block(inode
, bh
)) {
2724 EXT4_ERROR_INODE(inode
, "bad block %llu",
2725 EXT4_I(inode
)->i_file_acl
);
2726 error
= -EFSCORRUPTED
;
2731 end
= bh
->b_data
+ bh
->b_size
;
2732 min_offs
= end
- base
;
2733 bfree
= ext4_xattr_free_space(BFIRST(bh
), &min_offs
, base
,
2736 if (bfree
+ ifree
< isize_diff
) {
2737 if (!tried_min_extra_isize
&& s_min_extra_isize
) {
2738 tried_min_extra_isize
++;
2739 new_extra_isize
= s_min_extra_isize
;
2746 bfree
= inode
->i_sb
->s_blocksize
;
2749 error
= ext4_xattr_make_inode_space(handle
, inode
, raw_inode
,
2750 isize_diff
, ifree
, bfree
,
2753 if (error
== -ENOSPC
&& !tried_min_extra_isize
&&
2754 s_min_extra_isize
) {
2755 tried_min_extra_isize
++;
2756 new_extra_isize
= s_min_extra_isize
;
2762 /* Adjust the offsets and shift the remaining entries ahead */
2763 ext4_xattr_shift_entries(IFIRST(header
), EXT4_I(inode
)->i_extra_isize
2764 - new_extra_isize
, (void *)raw_inode
+
2765 EXT4_GOOD_OLD_INODE_SIZE
+ new_extra_isize
,
2766 (void *)header
, total_ino
);
2767 EXT4_I(inode
)->i_extra_isize
= new_extra_isize
;
2770 if (error
&& (mnt_count
!= le16_to_cpu(sbi
->s_es
->s_mnt_count
))) {
2771 ext4_warning(inode
->i_sb
, "Unable to expand inode %lu. Delete some EAs or run e2fsck.",
2773 mnt_count
= le16_to_cpu(sbi
->s_es
->s_mnt_count
);
2778 #define EIA_INCR 16 /* must be 2^n */
2779 #define EIA_MASK (EIA_INCR - 1)
2781 /* Add the large xattr @inode into @ea_inode_array for deferred iput().
2782 * If @ea_inode_array is new or full it will be grown and the old
2783 * contents copied over.
2786 ext4_expand_inode_array(struct ext4_xattr_inode_array
**ea_inode_array
,
2787 struct inode
*inode
)
2789 if (*ea_inode_array
== NULL
) {
2791 * Start with 15 inodes, so it fits into a power-of-two size.
2792 * If *ea_inode_array is NULL, this is essentially offsetof()
2795 kmalloc(offsetof(struct ext4_xattr_inode_array
,
2798 if (*ea_inode_array
== NULL
)
2800 (*ea_inode_array
)->count
= 0;
2801 } else if (((*ea_inode_array
)->count
& EIA_MASK
) == EIA_MASK
) {
2802 /* expand the array once all 15 + n * 16 slots are full */
2803 struct ext4_xattr_inode_array
*new_array
= NULL
;
2804 int count
= (*ea_inode_array
)->count
;
2806 /* if new_array is NULL, this is essentially offsetof() */
2807 new_array
= kmalloc(
2808 offsetof(struct ext4_xattr_inode_array
,
2809 inodes
[count
+ EIA_INCR
]),
2811 if (new_array
== NULL
)
2813 memcpy(new_array
, *ea_inode_array
,
2814 offsetof(struct ext4_xattr_inode_array
, inodes
[count
]));
2815 kfree(*ea_inode_array
);
2816 *ea_inode_array
= new_array
;
2818 (*ea_inode_array
)->inodes
[(*ea_inode_array
)->count
++] = inode
;
2823 * ext4_xattr_delete_inode()
2825 * Free extended attribute resources associated with this inode. Traverse
2826 * all entries and decrement reference on any xattr inodes associated with this
2827 * inode. This is called immediately before an inode is freed. We have exclusive
2828 * access to the inode. If an orphan inode is deleted it will also release its
2829 * references on xattr block and xattr inodes.
2831 int ext4_xattr_delete_inode(handle_t
*handle
, struct inode
*inode
,
2832 struct ext4_xattr_inode_array
**ea_inode_array
,
2835 struct buffer_head
*bh
= NULL
;
2836 struct ext4_xattr_ibody_header
*header
;
2837 struct ext4_iloc iloc
= { .bh
= NULL
};
2838 struct ext4_xattr_entry
*entry
;
2839 struct inode
*ea_inode
;
2842 error
= ext4_xattr_ensure_credits(handle
, inode
, extra_credits
,
2845 false /* block_csum */);
2847 EXT4_ERROR_INODE(inode
, "ensure credits (error %d)", error
);
2851 if (ext4_has_feature_ea_inode(inode
->i_sb
) &&
2852 ext4_test_inode_state(inode
, EXT4_STATE_XATTR
)) {
2854 error
= ext4_get_inode_loc(inode
, &iloc
);
2856 EXT4_ERROR_INODE(inode
, "inode loc (error %d)", error
);
2860 error
= ext4_journal_get_write_access(handle
, iloc
.bh
);
2862 EXT4_ERROR_INODE(inode
, "write access (error %d)",
2867 header
= IHDR(inode
, ext4_raw_inode(&iloc
));
2868 if (header
->h_magic
== cpu_to_le32(EXT4_XATTR_MAGIC
))
2869 ext4_xattr_inode_dec_ref_all(handle
, inode
, iloc
.bh
,
2871 false /* block_csum */,
2874 false /* skip_quota */);
2877 if (EXT4_I(inode
)->i_file_acl
) {
2878 bh
= sb_bread(inode
->i_sb
, EXT4_I(inode
)->i_file_acl
);
2880 EXT4_ERROR_INODE(inode
, "block %llu read error",
2881 EXT4_I(inode
)->i_file_acl
);
2885 error
= ext4_xattr_check_block(inode
, bh
);
2887 EXT4_ERROR_INODE(inode
, "bad block %llu (error %d)",
2888 EXT4_I(inode
)->i_file_acl
, error
);
2892 if (ext4_has_feature_ea_inode(inode
->i_sb
)) {
2893 for (entry
= BFIRST(bh
); !IS_LAST_ENTRY(entry
);
2894 entry
= EXT4_XATTR_NEXT(entry
)) {
2895 if (!entry
->e_value_inum
)
2897 error
= ext4_xattr_inode_iget(inode
,
2898 le32_to_cpu(entry
->e_value_inum
),
2899 le32_to_cpu(entry
->e_hash
),
2903 ext4_xattr_inode_free_quota(inode
, ea_inode
,
2904 le32_to_cpu(entry
->e_value_size
));
2910 ext4_xattr_release_block(handle
, inode
, bh
, ea_inode_array
,
2913 * Update i_file_acl value in the same transaction that releases
2916 EXT4_I(inode
)->i_file_acl
= 0;
2917 error
= ext4_mark_inode_dirty(handle
, inode
);
2919 EXT4_ERROR_INODE(inode
, "mark inode dirty (error %d)",
2931 void ext4_xattr_inode_array_free(struct ext4_xattr_inode_array
*ea_inode_array
)
2935 if (ea_inode_array
== NULL
)
2938 for (idx
= 0; idx
< ea_inode_array
->count
; ++idx
)
2939 iput(ea_inode_array
->inodes
[idx
]);
2940 kfree(ea_inode_array
);
2944 * ext4_xattr_block_cache_insert()
2946 * Create a new entry in the extended attribute block cache, and insert
2947 * it unless such an entry is already in the cache.
2949 * Returns 0, or a negative error number on failure.
2952 ext4_xattr_block_cache_insert(struct mb_cache
*ea_block_cache
,
2953 struct buffer_head
*bh
)
2955 struct ext4_xattr_header
*header
= BHDR(bh
);
2956 __u32 hash
= le32_to_cpu(header
->h_hash
);
2957 int reusable
= le32_to_cpu(header
->h_refcount
) <
2958 EXT4_XATTR_REFCOUNT_MAX
;
2961 if (!ea_block_cache
)
2963 error
= mb_cache_entry_create(ea_block_cache
, GFP_NOFS
, hash
,
2964 bh
->b_blocknr
, reusable
);
2966 if (error
== -EBUSY
)
2967 ea_bdebug(bh
, "already in cache");
2969 ea_bdebug(bh
, "inserting [%x]", (int)hash
);
2975 * Compare two extended attribute blocks for equality.
2977 * Returns 0 if the blocks are equal, 1 if they differ, and
2978 * a negative error number on errors.
2981 ext4_xattr_cmp(struct ext4_xattr_header
*header1
,
2982 struct ext4_xattr_header
*header2
)
2984 struct ext4_xattr_entry
*entry1
, *entry2
;
2986 entry1
= ENTRY(header1
+1);
2987 entry2
= ENTRY(header2
+1);
2988 while (!IS_LAST_ENTRY(entry1
)) {
2989 if (IS_LAST_ENTRY(entry2
))
2991 if (entry1
->e_hash
!= entry2
->e_hash
||
2992 entry1
->e_name_index
!= entry2
->e_name_index
||
2993 entry1
->e_name_len
!= entry2
->e_name_len
||
2994 entry1
->e_value_size
!= entry2
->e_value_size
||
2995 entry1
->e_value_inum
!= entry2
->e_value_inum
||
2996 memcmp(entry1
->e_name
, entry2
->e_name
, entry1
->e_name_len
))
2998 if (!entry1
->e_value_inum
&&
2999 memcmp((char *)header1
+ le16_to_cpu(entry1
->e_value_offs
),
3000 (char *)header2
+ le16_to_cpu(entry2
->e_value_offs
),
3001 le32_to_cpu(entry1
->e_value_size
)))
3004 entry1
= EXT4_XATTR_NEXT(entry1
);
3005 entry2
= EXT4_XATTR_NEXT(entry2
);
3007 if (!IS_LAST_ENTRY(entry2
))
3013 * ext4_xattr_block_cache_find()
3015 * Find an identical extended attribute block.
3017 * Returns a pointer to the block found, or NULL if such a block was
3018 * not found or an error occurred.
3020 static struct buffer_head
*
3021 ext4_xattr_block_cache_find(struct inode
*inode
,
3022 struct ext4_xattr_header
*header
,
3023 struct mb_cache_entry
**pce
)
3025 __u32 hash
= le32_to_cpu(header
->h_hash
);
3026 struct mb_cache_entry
*ce
;
3027 struct mb_cache
*ea_block_cache
= EA_BLOCK_CACHE(inode
);
3029 if (!ea_block_cache
)
3031 if (!header
->h_hash
)
3032 return NULL
; /* never share */
3033 ea_idebug(inode
, "looking for cached blocks [%x]", (int)hash
);
3034 ce
= mb_cache_entry_find_first(ea_block_cache
, hash
);
3036 struct buffer_head
*bh
;
3038 bh
= sb_bread(inode
->i_sb
, ce
->e_value
);
3040 EXT4_ERROR_INODE(inode
, "block %lu read error",
3041 (unsigned long)ce
->e_value
);
3042 } else if (ext4_xattr_cmp(header
, BHDR(bh
)) == 0) {
3047 ce
= mb_cache_entry_find_next(ea_block_cache
, ce
);
3052 #define NAME_HASH_SHIFT 5
3053 #define VALUE_HASH_SHIFT 16
3056 * ext4_xattr_hash_entry()
3058 * Compute the hash of an extended attribute.
3060 static __le32
ext4_xattr_hash_entry(char *name
, size_t name_len
, __le32
*value
,
3065 while (name_len
--) {
3066 hash
= (hash
<< NAME_HASH_SHIFT
) ^
3067 (hash
>> (8*sizeof(hash
) - NAME_HASH_SHIFT
)) ^
3070 while (value_count
--) {
3071 hash
= (hash
<< VALUE_HASH_SHIFT
) ^
3072 (hash
>> (8*sizeof(hash
) - VALUE_HASH_SHIFT
)) ^
3073 le32_to_cpu(*value
++);
3075 return cpu_to_le32(hash
);
3078 #undef NAME_HASH_SHIFT
3079 #undef VALUE_HASH_SHIFT
3081 #define BLOCK_HASH_SHIFT 16
3084 * ext4_xattr_rehash()
3086 * Re-compute the extended attribute hash value after an entry has changed.
3088 static void ext4_xattr_rehash(struct ext4_xattr_header
*header
)
3090 struct ext4_xattr_entry
*here
;
3093 here
= ENTRY(header
+1);
3094 while (!IS_LAST_ENTRY(here
)) {
3095 if (!here
->e_hash
) {
3096 /* Block is not shared if an entry's hash value == 0 */
3100 hash
= (hash
<< BLOCK_HASH_SHIFT
) ^
3101 (hash
>> (8*sizeof(hash
) - BLOCK_HASH_SHIFT
)) ^
3102 le32_to_cpu(here
->e_hash
);
3103 here
= EXT4_XATTR_NEXT(here
);
3105 header
->h_hash
= cpu_to_le32(hash
);
3108 #undef BLOCK_HASH_SHIFT
3110 #define HASH_BUCKET_BITS 10
3113 ext4_xattr_create_cache(void)
3115 return mb_cache_create(HASH_BUCKET_BITS
);
3118 void ext4_xattr_destroy_cache(struct mb_cache
*cache
)
3121 mb_cache_destroy(cache
);