Skip to content

Commit b1241c8

Browse files
riteshharjanitytso
authored andcommitted
ext4: move ext4 crypto code to its own file crypto.c
This is to cleanup super.c file which has grown quite large. So, start moving ext4 crypto related code to where it should be in the first place i.e. fs/ext4/crypto.c Reviewed-by: Eric Biggers <[email protected]> Signed-off-by: Ritesh Harjani <[email protected]> Link: https://lore.kernel.org/r/7d637e093cbc34d727397e8d41a53a1b9ca7d7a4.1652595565.git.ritesh.list@gmail.com Signed-off-by: Theodore Ts'o <[email protected]>
1 parent c069db7 commit b1241c8

File tree

4 files changed

+131
-122
lines changed

4 files changed

+131
-122
lines changed

fs/ext4/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ ext4-$(CONFIG_EXT4_FS_SECURITY) += xattr_security.o
1717
ext4-inode-test-objs += inode-test.o
1818
obj-$(CONFIG_EXT4_KUNIT_TESTS) += ext4-inode-test.o
1919
ext4-$(CONFIG_FS_VERITY) += verity.o
20+
ext4-$(CONFIG_FS_ENCRYPTION) += crypto.o

fs/ext4/crypto.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/quotaops.h>
4+
5+
#include "ext4.h"
6+
#include "xattr.h"
7+
#include "ext4_jbd2.h"
8+
9+
static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
10+
{
11+
return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
12+
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
13+
}
14+
15+
static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
16+
void *fs_data)
17+
{
18+
handle_t *handle = fs_data;
19+
int res, res2, credits, retries = 0;
20+
21+
/*
22+
* Encrypting the root directory is not allowed because e2fsck expects
23+
* lost+found to exist and be unencrypted, and encrypting the root
24+
* directory would imply encrypting the lost+found directory as well as
25+
* the filename "lost+found" itself.
26+
*/
27+
if (inode->i_ino == EXT4_ROOT_INO)
28+
return -EPERM;
29+
30+
if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
31+
return -EINVAL;
32+
33+
if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
34+
return -EOPNOTSUPP;
35+
36+
res = ext4_convert_inline_data(inode);
37+
if (res)
38+
return res;
39+
40+
/*
41+
* If a journal handle was specified, then the encryption context is
42+
* being set on a new inode via inheritance and is part of a larger
43+
* transaction to create the inode. Otherwise the encryption context is
44+
* being set on an existing inode in its own transaction. Only in the
45+
* latter case should the "retry on ENOSPC" logic be used.
46+
*/
47+
48+
if (handle) {
49+
res = ext4_xattr_set_handle(handle, inode,
50+
EXT4_XATTR_INDEX_ENCRYPTION,
51+
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
52+
ctx, len, 0);
53+
if (!res) {
54+
ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
55+
ext4_clear_inode_state(inode,
56+
EXT4_STATE_MAY_INLINE_DATA);
57+
/*
58+
* Update inode->i_flags - S_ENCRYPTED will be enabled,
59+
* S_DAX may be disabled
60+
*/
61+
ext4_set_inode_flags(inode, false);
62+
}
63+
return res;
64+
}
65+
66+
res = dquot_initialize(inode);
67+
if (res)
68+
return res;
69+
retry:
70+
res = ext4_xattr_set_credits(inode, len, false /* is_create */,
71+
&credits);
72+
if (res)
73+
return res;
74+
75+
handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
76+
if (IS_ERR(handle))
77+
return PTR_ERR(handle);
78+
79+
res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
80+
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
81+
ctx, len, 0);
82+
if (!res) {
83+
ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
84+
/*
85+
* Update inode->i_flags - S_ENCRYPTED will be enabled,
86+
* S_DAX may be disabled
87+
*/
88+
ext4_set_inode_flags(inode, false);
89+
res = ext4_mark_inode_dirty(handle, inode);
90+
if (res)
91+
EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
92+
}
93+
res2 = ext4_journal_stop(handle);
94+
95+
if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
96+
goto retry;
97+
if (!res)
98+
res = res2;
99+
return res;
100+
}
101+
102+
static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
103+
{
104+
return EXT4_SB(sb)->s_dummy_enc_policy.policy;
105+
}
106+
107+
static bool ext4_has_stable_inodes(struct super_block *sb)
108+
{
109+
return ext4_has_feature_stable_inodes(sb);
110+
}
111+
112+
static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
113+
int *ino_bits_ret, int *lblk_bits_ret)
114+
{
115+
*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
116+
*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
117+
}
118+
119+
const struct fscrypt_operations ext4_cryptops = {
120+
.key_prefix = "ext4:",
121+
.get_context = ext4_get_context,
122+
.set_context = ext4_set_context,
123+
.get_dummy_policy = ext4_get_dummy_policy,
124+
.empty_dir = ext4_empty_dir,
125+
.has_stable_inodes = ext4_has_stable_inodes,
126+
.get_ino_and_lblk_bits = ext4_get_ino_and_lblk_bits,
127+
};

fs/ext4/ext4.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,10 @@ extern int ext4_fname_setup_ci_filename(struct inode *dir,
27332733
struct ext4_filename *fname);
27342734
#endif
27352735

2736+
/* ext4 encryption related stuff goes here crypto.c */
27362737
#ifdef CONFIG_FS_ENCRYPTION
2738+
extern const struct fscrypt_operations ext4_cryptops;
2739+
27372740
static inline void ext4_fname_from_fscrypt_name(struct ext4_filename *dst,
27382741
const struct fscrypt_name *src)
27392742
{

fs/ext4/super.c

Lines changed: 0 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,128 +1495,6 @@ static int ext4_nfs_commit_metadata(struct inode *inode)
14951495
return ext4_write_inode(inode, &wbc);
14961496
}
14971497

1498-
#ifdef CONFIG_FS_ENCRYPTION
1499-
static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1500-
{
1501-
return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1502-
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1503-
}
1504-
1505-
static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1506-
void *fs_data)
1507-
{
1508-
handle_t *handle = fs_data;
1509-
int res, res2, credits, retries = 0;
1510-
1511-
/*
1512-
* Encrypting the root directory is not allowed because e2fsck expects
1513-
* lost+found to exist and be unencrypted, and encrypting the root
1514-
* directory would imply encrypting the lost+found directory as well as
1515-
* the filename "lost+found" itself.
1516-
*/
1517-
if (inode->i_ino == EXT4_ROOT_INO)
1518-
return -EPERM;
1519-
1520-
if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
1521-
return -EINVAL;
1522-
1523-
if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1524-
return -EOPNOTSUPP;
1525-
1526-
res = ext4_convert_inline_data(inode);
1527-
if (res)
1528-
return res;
1529-
1530-
/*
1531-
* If a journal handle was specified, then the encryption context is
1532-
* being set on a new inode via inheritance and is part of a larger
1533-
* transaction to create the inode. Otherwise the encryption context is
1534-
* being set on an existing inode in its own transaction. Only in the
1535-
* latter case should the "retry on ENOSPC" logic be used.
1536-
*/
1537-
1538-
if (handle) {
1539-
res = ext4_xattr_set_handle(handle, inode,
1540-
EXT4_XATTR_INDEX_ENCRYPTION,
1541-
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1542-
ctx, len, 0);
1543-
if (!res) {
1544-
ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1545-
ext4_clear_inode_state(inode,
1546-
EXT4_STATE_MAY_INLINE_DATA);
1547-
/*
1548-
* Update inode->i_flags - S_ENCRYPTED will be enabled,
1549-
* S_DAX may be disabled
1550-
*/
1551-
ext4_set_inode_flags(inode, false);
1552-
}
1553-
return res;
1554-
}
1555-
1556-
res = dquot_initialize(inode);
1557-
if (res)
1558-
return res;
1559-
retry:
1560-
res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1561-
&credits);
1562-
if (res)
1563-
return res;
1564-
1565-
handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
1566-
if (IS_ERR(handle))
1567-
return PTR_ERR(handle);
1568-
1569-
res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
1570-
EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1571-
ctx, len, 0);
1572-
if (!res) {
1573-
ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1574-
/*
1575-
* Update inode->i_flags - S_ENCRYPTED will be enabled,
1576-
* S_DAX may be disabled
1577-
*/
1578-
ext4_set_inode_flags(inode, false);
1579-
res = ext4_mark_inode_dirty(handle, inode);
1580-
if (res)
1581-
EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1582-
}
1583-
res2 = ext4_journal_stop(handle);
1584-
1585-
if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
1586-
goto retry;
1587-
if (!res)
1588-
res = res2;
1589-
return res;
1590-
}
1591-
1592-
static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
1593-
{
1594-
return EXT4_SB(sb)->s_dummy_enc_policy.policy;
1595-
}
1596-
1597-
static bool ext4_has_stable_inodes(struct super_block *sb)
1598-
{
1599-
return ext4_has_feature_stable_inodes(sb);
1600-
}
1601-
1602-
static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1603-
int *ino_bits_ret, int *lblk_bits_ret)
1604-
{
1605-
*ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1606-
*lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1607-
}
1608-
1609-
static const struct fscrypt_operations ext4_cryptops = {
1610-
.key_prefix = "ext4:",
1611-
.get_context = ext4_get_context,
1612-
.set_context = ext4_set_context,
1613-
.get_dummy_policy = ext4_get_dummy_policy,
1614-
.empty_dir = ext4_empty_dir,
1615-
.has_stable_inodes = ext4_has_stable_inodes,
1616-
.get_ino_and_lblk_bits = ext4_get_ino_and_lblk_bits,
1617-
};
1618-
#endif
1619-
16201498
#ifdef CONFIG_QUOTA
16211499
static const char * const quotatypes[] = INITQFNAMES;
16221500
#define QTYPE2NAME(t) (quotatypes[t])

0 commit comments

Comments
 (0)