add patch add-ext4_should_use_dax
[ext4-patch-queue.git] / add-sanity-check-for-encryption-and-dax
blob9562990e500f5bd30a89020a62c3ba73318e1154
1 ext4: add sanity check for encryption + DAX
3 From: Ross Zwisler <ross.zwisler@linux.intel.com>
5 We prevent DAX from being used on inodes which are using ext4's built in
6 encryption via a check in ext4_set_inode_flags().  We do have what appears
7 to be an unsafe transition of S_DAX in ext4_set_context(), though, where
8 S_DAX can get disabled without us doing a proper writeback + invalidate.
10 There are also issues with mm-level races when changing the value of S_DAX,
11 as well as issues with the VM_MIXEDMAP flag:
13 https://www.spinics.net/lists/linux-xfs/msg09859.html
15 I actually think we are safe in this case because of the following:
17 1) You can't encrypt an existing file.  Encryption can only be set on an
18 empty directory, with new inodes in that directory being created with
19 encryption turned on, so I don't think it's possible to turn encryption on
20 for a file that has open DAX mmaps or outstanding I/Os.
22 2) There is no way to turn encryption off on a given file.  Once an inode
23 is encrypted, it stays encrypted for the life of that inode, so we don't
24 have to worry about the case where we turn encryption off and S_DAX
25 suddenly turns on.
27 3) The only way we end up in ext4_set_context() to turn on encryption is
28 when we are creating a new file in the encrypted directory.  This happens
29 as part of ext4_create() before the inode has been allowed to do any I/O.
30 Here's the call tree:
32  ext4_create()
33    __ext4_new_inode()
34          ext4_set_inode_flags() // sets S_DAX
35          fscrypt_inherit_context()
36                 fscrypt_get_encryption_info();
37                 ext4_set_context() // sets EXT4_INODE_ENCRYPT, clears S_DAX
39 So, I actually think it's safe to transition S_DAX in ext4_set_context()
40 without any locking, writebacks or invalidations.  I've added a
41 WARN_ON_ONCE() sanity check to make sure that we are notified if we ever
42 encounter a case where we are encrypting an inode that already has data,
43 in which case we need to add code to safely transition S_DAX.
45 Signed-off-by: Ross Zwisler <ross.zwisler@linux.intel.com>
46 Signed-off-by: Theodore Ts'o <tytso@mit.edu>
47 Reviewed-by: Jan Kara <jack@suse.cz>
48 ---
49  fs/ext4/super.c | 3 +++
50  1 file changed, 3 insertions(+)
52 diff --git a/fs/ext4/super.c b/fs/ext4/super.c
53 index 4251e50..c090780 100644
54 --- a/fs/ext4/super.c
55 +++ b/fs/ext4/super.c
56 @@ -1159,6 +1159,9 @@ static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
57         if (inode->i_ino == EXT4_ROOT_INO)
58                 return -EPERM;
60 +       if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
61 +               return -EINVAL;
63         res = ext4_convert_inline_data(inode);
64         if (res)
65                 return res;
66 -- 
67 2.9.5