Skip to content

Commit 72f63f4

Browse files
riteshharjanitytso
authored andcommitted
ext4: refactor and move ext4_ioctl_get_encryption_pwsalt()
This patch move code for FS_IOC_GET_ENCRYPTION_PWSALT case into ext4's crypto.c file, i.e. ext4_ioctl_get_encryption_pwsalt() and uuid_is_zero(). This is mostly refactoring logic and should not affect any functionality change. Suggested-by: Eric Biggers <[email protected]> Reviewed-by: Eric Biggers <[email protected]> Signed-off-by: Ritesh Harjani <[email protected]> Link: https://lore.kernel.org/r/5af98b17152a96b245b4f7d2dfb8607fc93e36aa.1652595565.git.ritesh.list@gmail.com Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 3030b59 commit 72f63f4

File tree

3 files changed

+64
-57
lines changed

3 files changed

+64
-57
lines changed

fs/ext4/crypto.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0
22

33
#include <linux/quotaops.h>
4+
#include <linux/uuid.h>
45

56
#include "ext4.h"
67
#include "xattr.h"
@@ -71,6 +72,59 @@ void ext4_fname_free_filename(struct ext4_filename *fname)
7172
#endif
7273
}
7374

75+
static bool uuid_is_zero(__u8 u[16])
76+
{
77+
int i;
78+
79+
for (i = 0; i < 16; i++)
80+
if (u[i])
81+
return false;
82+
return true;
83+
}
84+
85+
int ext4_ioctl_get_encryption_pwsalt(struct file *filp, void __user *arg)
86+
{
87+
struct super_block *sb = file_inode(filp)->i_sb;
88+
struct ext4_sb_info *sbi = EXT4_SB(sb);
89+
int err, err2;
90+
handle_t *handle;
91+
92+
if (!ext4_has_feature_encrypt(sb))
93+
return -EOPNOTSUPP;
94+
95+
if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
96+
err = mnt_want_write_file(filp);
97+
if (err)
98+
return err;
99+
handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
100+
if (IS_ERR(handle)) {
101+
err = PTR_ERR(handle);
102+
goto pwsalt_err_exit;
103+
}
104+
err = ext4_journal_get_write_access(handle, sb, sbi->s_sbh,
105+
EXT4_JTR_NONE);
106+
if (err)
107+
goto pwsalt_err_journal;
108+
lock_buffer(sbi->s_sbh);
109+
generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
110+
ext4_superblock_csum_set(sb);
111+
unlock_buffer(sbi->s_sbh);
112+
err = ext4_handle_dirty_metadata(handle, NULL, sbi->s_sbh);
113+
pwsalt_err_journal:
114+
err2 = ext4_journal_stop(handle);
115+
if (err2 && !err)
116+
err = err2;
117+
pwsalt_err_exit:
118+
mnt_drop_write_file(filp);
119+
if (err)
120+
return err;
121+
}
122+
123+
if (copy_to_user(arg, sbi->s_es->s_encrypt_pw_salt, 16))
124+
return -EFAULT;
125+
return 0;
126+
}
127+
74128
static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
75129
{
76130
return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,

fs/ext4/ext4.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2745,6 +2745,8 @@ int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry,
27452745

27462746
void ext4_fname_free_filename(struct ext4_filename *fname);
27472747

2748+
int ext4_ioctl_get_encryption_pwsalt(struct file *filp, void __user *arg);
2749+
27482750
#else /* !CONFIG_FS_ENCRYPTION */
27492751
static inline int ext4_fname_setup_filename(struct inode *dir,
27502752
const struct qstr *iname,
@@ -2777,6 +2779,12 @@ static inline void ext4_fname_free_filename(struct ext4_filename *fname)
27772779
fname->cf_name.name = NULL;
27782780
#endif
27792781
}
2782+
2783+
static inline int ext4_ioctl_get_encryption_pwsalt(struct file *filp,
2784+
void __user *arg)
2785+
{
2786+
return -EOPNOTSUPP;
2787+
}
27802788
#endif /* !CONFIG_FS_ENCRYPTION */
27812789

27822790
/* dir.c */

fs/ext4/ioctl.c

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include <linux/file.h>
1717
#include <linux/quotaops.h>
1818
#include <linux/random.h>
19-
#include <linux/uuid.h>
2019
#include <linux/uaccess.h>
2120
#include <linux/delay.h>
2221
#include <linux/iversion.h>
@@ -504,18 +503,6 @@ static long swap_inode_boot_loader(struct super_block *sb,
504503
return err;
505504
}
506505

507-
#ifdef CONFIG_FS_ENCRYPTION
508-
static int uuid_is_zero(__u8 u[16])
509-
{
510-
int i;
511-
512-
for (i = 0; i < 16; i++)
513-
if (u[i])
514-
return 0;
515-
return 1;
516-
}
517-
#endif
518-
519506
/*
520507
* If immutable is set and we are not clearing it, we're not allowed to change
521508
* anything else in the inode. Don't error out if we're only trying to set
@@ -1432,51 +1419,9 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
14321419
return -EOPNOTSUPP;
14331420
return fscrypt_ioctl_set_policy(filp, (const void __user *)arg);
14341421

1435-
case FS_IOC_GET_ENCRYPTION_PWSALT: {
1436-
#ifdef CONFIG_FS_ENCRYPTION
1437-
int err, err2;
1438-
struct ext4_sb_info *sbi = EXT4_SB(sb);
1439-
handle_t *handle;
1422+
case FS_IOC_GET_ENCRYPTION_PWSALT:
1423+
return ext4_ioctl_get_encryption_pwsalt(filp, (void __user *)arg);
14401424

1441-
if (!ext4_has_feature_encrypt(sb))
1442-
return -EOPNOTSUPP;
1443-
if (uuid_is_zero(sbi->s_es->s_encrypt_pw_salt)) {
1444-
err = mnt_want_write_file(filp);
1445-
if (err)
1446-
return err;
1447-
handle = ext4_journal_start_sb(sb, EXT4_HT_MISC, 1);
1448-
if (IS_ERR(handle)) {
1449-
err = PTR_ERR(handle);
1450-
goto pwsalt_err_exit;
1451-
}
1452-
err = ext4_journal_get_write_access(handle, sb,
1453-
sbi->s_sbh,
1454-
EXT4_JTR_NONE);
1455-
if (err)
1456-
goto pwsalt_err_journal;
1457-
lock_buffer(sbi->s_sbh);
1458-
generate_random_uuid(sbi->s_es->s_encrypt_pw_salt);
1459-
ext4_superblock_csum_set(sb);
1460-
unlock_buffer(sbi->s_sbh);
1461-
err = ext4_handle_dirty_metadata(handle, NULL,
1462-
sbi->s_sbh);
1463-
pwsalt_err_journal:
1464-
err2 = ext4_journal_stop(handle);
1465-
if (err2 && !err)
1466-
err = err2;
1467-
pwsalt_err_exit:
1468-
mnt_drop_write_file(filp);
1469-
if (err)
1470-
return err;
1471-
}
1472-
if (copy_to_user((void __user *) arg,
1473-
sbi->s_es->s_encrypt_pw_salt, 16))
1474-
return -EFAULT;
1475-
return 0;
1476-
#else
1477-
return -EOPNOTSUPP;
1478-
#endif
1479-
}
14801425
case FS_IOC_GET_ENCRYPTION_POLICY:
14811426
if (!ext4_has_feature_encrypt(sb))
14821427
return -EOPNOTSUPP;

0 commit comments

Comments
 (0)