mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-18 22:59:29 +02:00
Pull ext4 updates from Ted Ts'o:
- Improve performance by allowing parallel DIO writes when we were
previously being overly conservative when checking whether it was
safe to avoid requiring an exclusive lock
- Improve the performance of ext4_mb_prefetch() used by fallocate() by
avoiding work when it is not needed
- Remove the unnecessary custom end_io function
ext4_end_buffer_io_sync()
- Improve performance when performing an overwrite to an already
uptodate folio
- Clean up how we handle deallocating EA inodes to avoid a potential
lock ordering issue when there is a failed mount while an EA inode is
still being evicted
- Use str_plural() instead of a custom macro
- Avoid soft lockups or RCU stalls if there are many busy buffers
(caused by heavy I/O) while checkpointing
- Use scoped NOFS when starting a handle in nojournal mode
- Align fields in handle structure to optimize setting and getting the
h_type and h_line_no fields
- Fix documentation of the meta_bg block group layout
- Bug fixes:
- Fix a potential out-of-bounds read in ext4_read_inline_dir()
- Fix a potential deadlock when concurrent xattr operations are
racing with each other when some of the xattrs are using the
ea_inode feature
- Fix a spurious warning with data=journal that can be triggered
when writeback races with remounting the file system read-only
- Fix a potential deadlock when EXT4_IOC_MIGRATE races with a file
system freeze operation
- Make sure all in-flight direct I/O operations are complete before
falling back to buffered I/O
- Handle IOCB_NOWAIT properly when performing a extending DAX write
- Prevent potentially sleeping on a block allocation when
IOCB_NOWAIT is set
- Fix potential races when racing an inline data write with a page
fault
- Propagate errors when adding or removing extent ranges during a
fast commit replay
- Avoid trying to expand an inode's extra size when it is being
evicted to avoid a number of corner case or deadlocks
- Avoid spurious error when retrying inode extra size expansion
- Fix corner cases where we underestimate the number of journal
credits needed
- Avoid hangs/crashes/WARNINGS caused by maliciously corrupted file
systems
- Don't issue spurious orphan clean message on RO file systems
- Avoid leaving the file system in an inconsistent state after a
crash when a WRITE_ZEROS in progress converting an unwritten
extent to a written extent
- Handle WRITE_ZEROS correctly when there are some partially dirtied
regions in the page cache
- Pass errors during zero-rage, truncate, or punch hole to the
caller if ext4_get_block() fails
- Wait for writeback to finish when triggered by zero-range or
zero-range for those devices that require stable writes
- If the reserved gid superblock field is set, set the reserved gid
instead of the reserved uid
* tag 'ext4_for_linus-7.3-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4: (56 commits)
ext4: fix estimate extent index blocks in ext4_ext_index_trans_blocks()
ext4: fix transaction overflow during writeback
ext4: teach ext4_meta_trans_blocks() about number of allocated extents
ext4: guard against NULL s_group_info in ext4_get_group_info
ext4: fix spurious message about orphan cleanup on RO fs
ext4: stop retrying saturated xattr cache entries
ext4: don't enable DAX on new encrypted files
ext4: protect WRITE_ZEROES written extents with orphan list
ext4: export converted block count from ext4_convert_unwritten_extents()
ext4: fix incorrect function call when initializing s_resgid
ext4: validate EA inode i_nlink in ext4_xattr_inode_iget
jbd2: align h_type and h_line_no in the handle structure on byte boundaries
ext4: enable scoped NOFS when starting a handle in nojournal mode
ext4: write back partial-zeroed edges in WRITE_ZEROES
ext4: zero out whole block for clean edges in WRITE_ZEROES
ext4: track partial-zero outcome per edge in ext4_zero_partial_blocks()
ext4: clarify return semantics of ext4_load_tail_bh()
ext4: move partial block zeroing earlier in ext4_zero_range()
ext4: check return value of ext4_get_block() in ext4_load_tail_bh()
ext4: skip tail block zeroing for inline data files
...
249 lines
6.3 KiB
C
249 lines
6.3 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
|
|
#include <linux/quotaops.h>
|
|
#include <linux/uuid.h>
|
|
|
|
#include "ext4.h"
|
|
#include "xattr.h"
|
|
#include "ext4_jbd2.h"
|
|
|
|
static void ext4_fname_from_fscrypt_name(struct ext4_filename *dst,
|
|
const struct fscrypt_name *src)
|
|
{
|
|
memset(dst, 0, sizeof(*dst));
|
|
|
|
dst->usr_fname = src->usr_fname;
|
|
dst->disk_name = src->disk_name;
|
|
dst->hinfo.hash = src->hash;
|
|
dst->hinfo.minor_hash = src->minor_hash;
|
|
dst->crypto_buf = src->crypto_buf;
|
|
}
|
|
|
|
int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
|
|
int lookup, struct ext4_filename *fname)
|
|
{
|
|
struct fscrypt_name name;
|
|
int err;
|
|
|
|
err = fscrypt_setup_filename(dir, iname, lookup, &name);
|
|
if (err)
|
|
return err;
|
|
|
|
ext4_fname_from_fscrypt_name(fname, &name);
|
|
|
|
err = ext4_fname_setup_ci_filename(dir, iname, fname);
|
|
if (err)
|
|
ext4_fname_free_filename(fname);
|
|
|
|
return err;
|
|
}
|
|
|
|
int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry,
|
|
struct ext4_filename *fname)
|
|
{
|
|
struct fscrypt_name name;
|
|
int err;
|
|
|
|
err = fscrypt_prepare_lookup(dir, dentry, &name);
|
|
if (err)
|
|
return err;
|
|
|
|
ext4_fname_from_fscrypt_name(fname, &name);
|
|
|
|
err = ext4_fname_setup_ci_filename(dir, &dentry->d_name, fname);
|
|
if (err)
|
|
ext4_fname_free_filename(fname);
|
|
return err;
|
|
}
|
|
|
|
void ext4_fname_free_filename(struct ext4_filename *fname)
|
|
{
|
|
struct fscrypt_name name;
|
|
|
|
name.crypto_buf = fname->crypto_buf;
|
|
fscrypt_free_filename(&name);
|
|
|
|
fname->crypto_buf.name = NULL;
|
|
fname->usr_fname = NULL;
|
|
fname->disk_name.name = NULL;
|
|
|
|
ext4_fname_free_ci_filename(fname);
|
|
}
|
|
|
|
static bool uuid_is_zero(__u8 u[16])
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < 16; i++)
|
|
if (u[i])
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
int ext4_ioctl_get_encryption_pwsalt(struct file *filp, void __user *arg)
|
|
{
|
|
struct super_block *sb = file_inode(filp)->i_sb;
|
|
struct ext4_sb_info *sbi = EXT4_SB(sb);
|
|
int err, err2;
|
|
handle_t *handle;
|
|
|
|
if (!ext4_has_feature_encrypt(sb))
|
|
return -EOPNOTSUPP;
|
|
|
|
if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
|
|
err = mnt_want_write_file(filp);
|
|
if (err)
|
|
return err;
|
|
handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
|
|
if (IS_ERR(handle)) {
|
|
err = PTR_ERR(handle);
|
|
goto pwsalt_err_exit;
|
|
}
|
|
err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
|
|
EXT4_JTR_NONE);
|
|
if (err)
|
|
goto pwsalt_err_journal;
|
|
lock_buffer(sbi->s_sbh);
|
|
generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
|
|
ext4_superblock_csum_set(sb);
|
|
unlock_buffer(sbi->s_sbh);
|
|
err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
|
|
pwsalt_err_journal:
|
|
err2 = ext4_journal_stop(handle);
|
|
if (err2 && !err)
|
|
err = err2;
|
|
pwsalt_err_exit:
|
|
mnt_drop_write_file(filp);
|
|
if (err)
|
|
return err;
|
|
}
|
|
|
|
if (copy_to_user(arg, sbi->s_es->s_encrypt_pw_salt, 16))
|
|
return -EFAULT;
|
|
return 0;
|
|
}
|
|
|
|
static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
|
|
{
|
|
return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
|
|
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
|
|
}
|
|
|
|
static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
|
|
void *fs_data)
|
|
{
|
|
handle_t *handle = fs_data;
|
|
int res, res2, credits, retries = 0;
|
|
|
|
/*
|
|
* Encrypting the root directory is not allowed because e2fsck expects
|
|
* lost+found to exist and be unencrypted, and encrypting the root
|
|
* directory would imply encrypting the lost+found directory as well as
|
|
* the filename "lost+found" itself.
|
|
*/
|
|
if (inode->i_ino == EXT4_ROOT_INO)
|
|
return -EPERM;
|
|
|
|
/*
|
|
* For new encrypted inodes, S_DAX is never set in the first place.
|
|
*
|
|
* For existing inodes, this is called only on empty directories. ext4
|
|
* never sets S_DAX on directories.
|
|
*/
|
|
if (WARN_ON_ONCE(IS_DAX(inode)))
|
|
return -EINVAL;
|
|
|
|
if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
|
|
return -EOPNOTSUPP;
|
|
|
|
res = ext4_convert_inline_data(inode);
|
|
if (res)
|
|
return res;
|
|
|
|
/*
|
|
* If a journal handle was specified, then the encryption context is
|
|
* being set on a new inode via inheritance and is part of a larger
|
|
* transaction to create the inode. Otherwise the encryption context is
|
|
* being set on an existing inode in its own transaction. Only in the
|
|
* latter case should the "retry on ENOSPC" logic be used.
|
|
*/
|
|
|
|
if (handle) {
|
|
/*
|
|
* __ext4_new_inode() should have already set the encrypt flag
|
|
* on the inode and avoided enabling inline data.
|
|
*/
|
|
if (WARN_ON_ONCE(!IS_ENCRYPTED(inode)))
|
|
return -EINVAL;
|
|
if (WARN_ON_ONCE(ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)))
|
|
return -EINVAL;
|
|
/*
|
|
* Since the inode is new it is ok to pass the
|
|
* XATTR_CREATE flag. This is necessary to match the
|
|
* remaining journal credits check in the set_handle
|
|
* function with the credits allocated for the new
|
|
* inode.
|
|
*/
|
|
return ext4_xattr_set_handle(handle, inode,
|
|
EXT4_XATTR_INDEX_ENCRYPTION,
|
|
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
|
|
ctx, len, XATTR_CREATE);
|
|
}
|
|
|
|
res = dquot_initialize(inode);
|
|
if (res)
|
|
return res;
|
|
retry:
|
|
res = ext4_xattr_set_credits(inode, len, false /* is_create */,
|
|
&credits);
|
|
if (res)
|
|
return res;
|
|
|
|
handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
|
|
if (IS_ERR(handle))
|
|
return PTR_ERR(handle);
|
|
|
|
res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
|
|
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
|
|
ctx, len, 0);
|
|
if (!res) {
|
|
ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
|
|
/* Update inode->i_flags to set S_ENCRYPTED. */
|
|
ext4_set_inode_flags(inode, false);
|
|
res = ext4_mark_inode_dirty(handle, inode);
|
|
if (res)
|
|
EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
|
|
}
|
|
res2 = ext4_journal_stop(handle);
|
|
|
|
if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
|
|
goto retry;
|
|
if (!res)
|
|
res = res2;
|
|
return res;
|
|
}
|
|
|
|
static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
|
|
{
|
|
return EXT4_SB(sb)->s_dummy_enc_policy.policy;
|
|
}
|
|
|
|
static bool ext4_has_stable_inodes(struct super_block *sb)
|
|
{
|
|
return ext4_has_feature_stable_inodes(sb);
|
|
}
|
|
|
|
const struct fscrypt_operations ext4_cryptops = {
|
|
.inode_info_offs = (int)offsetof(struct ext4_inode_info, i_crypt_info) -
|
|
(int)offsetof(struct ext4_inode_info, vfs_inode),
|
|
.is_block_based = 1,
|
|
.has_32bit_inodes = 1,
|
|
.supports_subblock_data_units = 1,
|
|
.legacy_key_prefix = "ext4:",
|
|
.get_context = ext4_get_context,
|
|
.set_context = ext4_set_context,
|
|
.get_dummy_policy = ext4_get_dummy_policy,
|
|
.empty_dir = ext4_empty_dir,
|
|
.has_stable_inodes = ext4_has_stable_inodes,
|
|
};
|