Merge tag 'nfsd-6.3-6' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux
[linux-block.git] / include / linux / fscrypt.h
CommitLineData
32190f0a 1/* SPDX-License-Identifier: GPL-2.0 */
46f47e48 2/*
734f0d24
DC
3 * fscrypt.h: declarations for per-file encryption
4 *
643fa961
CR
5 * Filesystems that implement per-file encryption must include this header
6 * file.
46f47e48
EB
7 *
8 * Copyright (C) 2015, Google, Inc.
9 *
10 * Written by Michael Halcrow, 2015.
11 * Modified by Jaegeuk Kim, 2015.
12 */
734f0d24
DC
13#ifndef _LINUX_FSCRYPT_H
14#define _LINUX_FSCRYPT_H
46f47e48 15
46f47e48 16#include <linux/fs.h>
643fa961
CR
17#include <linux/mm.h>
18#include <linux/slab.h>
7af0ab0d 19#include <uapi/linux/fscrypt.h>
46f47e48 20
63cec138
EB
21/*
22 * The lengths of all file contents blocks must be divisible by this value.
23 * This is needed to ensure that all contents encryption modes will work, as
24 * some of the supported modes don't support arbitrarily byte-aligned messages.
25 *
26 * Since the needed alignment is 16 bytes, most filesystems will meet this
27 * requirement naturally, as typical block sizes are powers of 2. However, if a
28 * filesystem can generate arbitrarily byte-aligned block lengths (e.g., via
29 * compression), then it will need to pad to this alignment before encryption.
30 */
31#define FSCRYPT_CONTENTS_ALIGNMENT 16
46f47e48 32
ac4acb1f 33union fscrypt_policy;
46f47e48 34struct fscrypt_info;
218d921b 35struct fs_parameter;
ed318a6c 36struct seq_file;
46f47e48 37
46f47e48
EB
38struct fscrypt_str {
39 unsigned char *name;
40 u32 len;
41};
42
43struct fscrypt_name {
44 const struct qstr *usr_fname;
45 struct fscrypt_str disk_name;
46 u32 hash;
47 u32 minor_hash;
48 struct fscrypt_str crypto_buf;
70fb2612 49 bool is_nokey_name;
46f47e48
EB
50};
51
52#define FSTR_INIT(n, l) { .name = n, .len = l }
53#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
54#define fname_name(p) ((p)->disk_name.name)
55#define fname_len(p) ((p)->disk_name.len)
56
af65207c 57/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
5dae460c 58#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
af65207c 59
643fa961 60#ifdef CONFIG_FS_ENCRYPTION
38ef66b0 61
643fa961 62/*
38ef66b0
EB
63 * If set, the fscrypt bounce page pool won't be allocated (unless another
64 * filesystem needs it). Set this if the filesystem always uses its own bounce
65 * pages for writes and therefore won't need the fscrypt bounce page pool.
643fa961
CR
66 */
67#define FS_CFLG_OWN_PAGES (1U << 1)
68
38ef66b0 69/* Crypto operations for filesystems */
643fa961 70struct fscrypt_operations {
38ef66b0
EB
71
72 /* Set of optional flags; see above for allowed flags */
643fa961 73 unsigned int flags;
38ef66b0
EB
74
75 /*
76 * If set, this is a filesystem-specific key description prefix that
77 * will be accepted for "logon" keys for v1 fscrypt policies, in
78 * addition to the generic prefix "fscrypt:". This functionality is
79 * deprecated, so new filesystems shouldn't set this field.
80 */
643fa961 81 const char *key_prefix;
38ef66b0
EB
82
83 /*
84 * Get the fscrypt context of the given inode.
85 *
86 * @inode: the inode whose context to get
87 * @ctx: the buffer into which to get the context
88 * @len: length of the @ctx buffer in bytes
89 *
90 * Return: On success, returns the length of the context in bytes; this
91 * may be less than @len. On failure, returns -ENODATA if the
92 * inode doesn't have a context, -ERANGE if the context is
93 * longer than @len, or another -errno code.
94 */
fe015a78 95 int (*get_context)(struct inode *inode, void *ctx, size_t len);
38ef66b0
EB
96
97 /*
98 * Set an fscrypt context on the given inode.
99 *
100 * @inode: the inode whose context to set. The inode won't already have
101 * an fscrypt context.
102 * @ctx: the context to set
103 * @len: length of @ctx in bytes (at most FSCRYPT_SET_CONTEXT_MAX_SIZE)
104 * @fs_data: If called from fscrypt_set_context(), this will be the
105 * value the filesystem passed to fscrypt_set_context().
106 * Otherwise (i.e. when called from
107 * FS_IOC_SET_ENCRYPTION_POLICY) this will be NULL.
108 *
109 * i_rwsem will be held for write.
110 *
111 * Return: 0 on success, -errno on failure.
112 */
fe015a78
EB
113 int (*set_context)(struct inode *inode, const void *ctx, size_t len,
114 void *fs_data);
38ef66b0
EB
115
116 /*
117 * Get the dummy fscrypt policy in use on the filesystem (if any).
118 *
119 * Filesystems only need to implement this function if they support the
120 * test_dummy_encryption mount option.
121 *
122 * Return: A pointer to the dummy fscrypt policy, if the filesystem is
123 * mounted with test_dummy_encryption; otherwise NULL.
124 */
ac4acb1f 125 const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
38ef66b0
EB
126
127 /*
128 * Check whether a directory is empty. i_rwsem will be held for write.
129 */
fe015a78 130 bool (*empty_dir)(struct inode *inode);
38ef66b0 131
38ef66b0
EB
132 /*
133 * Check whether the filesystem's inode numbers and UUID are stable,
134 * meaning that they will never be changed even by offline operations
135 * such as filesystem shrinking and therefore can be used in the
136 * encryption without the possibility of files becoming unreadable.
137 *
138 * Filesystems only need to implement this function if they want to
139 * support the FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags. These
140 * flags are designed to work around the limitations of UFS and eMMC
141 * inline crypto hardware, and they shouldn't be used in scenarios where
142 * such hardware isn't being used.
143 *
144 * Leaving this NULL is equivalent to always returning false.
145 */
b103fb76 146 bool (*has_stable_inodes)(struct super_block *sb);
38ef66b0
EB
147
148 /*
149 * Get the number of bits that the filesystem uses to represent inode
150 * numbers and file logical block numbers.
151 *
152 * By default, both of these are assumed to be 64-bit. This function
153 * can be implemented to declare that either or both of these numbers is
154 * shorter, which may allow the use of the
155 * FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags and/or the use of
156 * inline crypto hardware whose maximum DUN length is less than 64 bits
157 * (e.g., eMMC v5.2 spec compliant hardware). This function only needs
158 * to be implemented if support for one of these features is needed.
159 */
b103fb76
EB
160 void (*get_ino_and_lblk_bits)(struct super_block *sb,
161 int *ino_bits_ret, int *lblk_bits_ret);
38ef66b0
EB
162
163 /*
0e91fc1e
CH
164 * Return an array of pointers to the block devices to which the
165 * filesystem may write encrypted file contents, NULL if the filesystem
166 * only has a single such block device, or an ERR_PTR() on error.
167 *
168 * On successful non-NULL return, *num_devs is set to the number of
169 * devices in the returned array. The caller must free the returned
170 * array using kfree().
38ef66b0
EB
171 *
172 * If the filesystem can use multiple block devices (other than block
173 * devices that aren't used for encrypted file contents, such as
174 * external journal devices), and wants to support inline encryption,
175 * then it must implement this function. Otherwise it's not needed.
176 */
0e91fc1e
CH
177 struct block_device **(*get_devices)(struct super_block *sb,
178 unsigned int *num_devs);
643fa961
CR
179};
180
ab673b98 181static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
643fa961 182{
ab673b98 183 /*
5b421f08 184 * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
ab673b98
EB
185 * I.e., another task may publish ->i_crypt_info concurrently, executing
186 * a RELEASE barrier. We need to use smp_load_acquire() here to safely
187 * ACQUIRE the memory the other task published.
188 */
189 return smp_load_acquire(&inode->i_crypt_info);
643fa961
CR
190}
191
56dce717
EB
192/**
193 * fscrypt_needs_contents_encryption() - check whether an inode needs
194 * contents encryption
d2fe9754 195 * @inode: the inode to check
56dce717
EB
196 *
197 * Return: %true iff the inode is an encrypted regular file and the kernel was
198 * built with fscrypt support.
199 *
200 * If you need to know whether the encrypt bit is set even when the kernel was
201 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
202 */
203static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
204{
205 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
206}
207
0bf3d5c1 208/*
501e43fb
EB
209 * When d_splice_alias() moves a directory's no-key alias to its plaintext alias
210 * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be
211 * cleared. Note that we don't have to support arbitrary moves of this flag
212 * because fscrypt doesn't allow no-key names to be the source or target of a
213 * rename().
0bf3d5c1
EB
214 */
215static inline void fscrypt_handle_d_move(struct dentry *dentry)
216{
501e43fb 217 dentry->d_flags &= ~DCACHE_NOKEY_NAME;
0bf3d5c1
EB
218}
219
159e1de2
EB
220/**
221 * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
222 * @dentry: the dentry to check
223 *
224 * This returns true if the dentry is a no-key dentry. A no-key dentry is a
225 * dentry that was created in an encrypted directory that hasn't had its
226 * encryption key added yet. Such dentries may be either positive or negative.
227 *
228 * When a filesystem is asked to create a new filename in an encrypted directory
229 * and the new filename's dentry is a no-key dentry, it must fail the operation
230 * with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
231 * ->rename(), and ->link(). (However, ->rename() and ->link() are already
232 * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
233 *
234 * This is necessary because creating a filename requires the directory's
235 * encryption key, but just checking for the key on the directory inode during
236 * the final filesystem operation doesn't guarantee that the key was available
237 * during the preceding dentry lookup. And the key must have already been
238 * available during the dentry lookup in order for it to have been checked
239 * whether the filename already exists in the directory and for the new file's
240 * dentry not to be invalidated due to it incorrectly having the no-key flag.
241 *
242 * Return: %true if the dentry is a no-key name
243 */
244static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
245{
246 return dentry->d_flags & DCACHE_NOKEY_NAME;
247}
248
643fa961 249/* crypto.c */
60700902
EB
250void fscrypt_enqueue_decrypt_work(struct work_struct *);
251
252struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
253 unsigned int len,
254 unsigned int offs,
255 gfp_t gfp_flags);
256int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
257 unsigned int len, unsigned int offs,
258 u64 lblk_num, gfp_t gfp_flags);
259
51e4e315
EB
260int fscrypt_decrypt_pagecache_blocks(struct folio *folio, size_t len,
261 size_t offs);
60700902
EB
262int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
263 unsigned int len, unsigned int offs,
264 u64 lblk_num);
643fa961 265
d2d0727b 266static inline bool fscrypt_is_bounce_page(struct page *page)
643fa961 267{
d2d0727b 268 return page->mapping == NULL;
643fa961
CR
269}
270
d2d0727b
EB
271static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
272{
273 return (struct page *)page_private(bounce_page);
274}
275
60700902 276void fscrypt_free_bounce_page(struct page *bounce_page);
643fa961
CR
277
278/* policy.c */
60700902
EB
279int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
280int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
281int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
282int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
283int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
637fa738 284int fscrypt_context_for_new_inode(void *ctx, struct inode *inode);
a992b20c 285int fscrypt_set_context(struct inode *inode, void *fs_data);
fe015a78 286
ac4acb1f
EB
287struct fscrypt_dummy_policy {
288 const union fscrypt_policy *policy;
ed318a6c
EB
289};
290
218d921b
EB
291int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
292 struct fscrypt_dummy_policy *dummy_policy);
293bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
294 const struct fscrypt_dummy_policy *p2);
ed318a6c
EB
295void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
296 struct super_block *sb);
218d921b
EB
297static inline bool
298fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
299{
300 return dummy_policy->policy != NULL;
301}
ed318a6c 302static inline void
ac4acb1f 303fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
ed318a6c 304{
ac4acb1f
EB
305 kfree(dummy_policy->policy);
306 dummy_policy->policy = NULL;
ed318a6c
EB
307}
308
22d94f49 309/* keyring.c */
ccd30a47 310void fscrypt_destroy_keyring(struct super_block *sb);
60700902
EB
311int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
312int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
313int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
314int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
22d94f49 315
feed8258 316/* keysetup.c */
a992b20c
EB
317int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
318 bool *encrypt_ret);
60700902
EB
319void fscrypt_put_encryption_info(struct inode *inode);
320void fscrypt_free_inode(struct inode *inode);
321int fscrypt_drop_inode(struct inode *inode);
643fa961
CR
322
323/* fname.c */
d3e94fdc
JL
324int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
325 u8 *out, unsigned int olen);
326bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
327 u32 max_len, u32 *encrypted_len_ret);
60700902
EB
328int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
329 int lookup, struct fscrypt_name *fname);
643fa961
CR
330
331static inline void fscrypt_free_filename(struct fscrypt_name *fname)
332{
333 kfree(fname->crypto_buf.name);
334}
335
8b10fe68 336int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
60700902
EB
337 struct fscrypt_str *crypto_str);
338void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
339int fscrypt_fname_disk_to_usr(const struct inode *inode,
340 u32 hash, u32 minor_hash,
341 const struct fscrypt_str *iname,
342 struct fscrypt_str *oname);
343bool fscrypt_match_name(const struct fscrypt_name *fname,
344 const u8 *de_name, u32 de_name_len);
345u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
5b2a828b 346int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
aa408f83 347
643fa961 348/* bio.c */
14db0b3c 349bool fscrypt_decrypt_bio(struct bio *bio);
60700902
EB
350int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
351 sector_t pblk, unsigned int len);
643fa961
CR
352
353/* hooks.c */
60700902
EB
354int fscrypt_file_open(struct inode *inode, struct file *filp);
355int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
356 struct dentry *dentry);
357int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
358 struct inode *new_dir, struct dentry *new_dentry,
359 unsigned int flags);
360int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
361 struct fscrypt_name *fname);
ec0caa97 362int __fscrypt_prepare_readdir(struct inode *dir);
7622350e 363int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
60700902
EB
364int fscrypt_prepare_setflags(struct inode *inode,
365 unsigned int oldflags, unsigned int flags);
31114726
EB
366int fscrypt_prepare_symlink(struct inode *dir, const char *target,
367 unsigned int len, unsigned int max_len,
368 struct fscrypt_str *disk_link);
60700902
EB
369int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
370 unsigned int len, struct fscrypt_str *disk_link);
371const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
372 unsigned int max_size,
373 struct delayed_call *done);
d1876056 374int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
eea2c05d
SH
375static inline void fscrypt_set_ops(struct super_block *sb,
376 const struct fscrypt_operations *s_cop)
377{
378 sb->s_cop = s_cop;
379}
643fa961
CR
380#else /* !CONFIG_FS_ENCRYPTION */
381
ab673b98 382static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
643fa961 383{
ab673b98 384 return NULL;
643fa961
CR
385}
386
56dce717
EB
387static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
388{
389 return false;
390}
391
0bf3d5c1
EB
392static inline void fscrypt_handle_d_move(struct dentry *dentry)
393{
394}
395
159e1de2
EB
396static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
397{
398 return false;
399}
400
643fa961
CR
401/* crypto.c */
402static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
403{
404}
405
53bc1d85
EB
406static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
407 unsigned int len,
408 unsigned int offs,
409 gfp_t gfp_flags)
643fa961
CR
410{
411 return ERR_PTR(-EOPNOTSUPP);
412}
413
03569f2f
EB
414static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
415 struct page *page,
416 unsigned int len,
417 unsigned int offs, u64 lblk_num,
418 gfp_t gfp_flags)
419{
420 return -EOPNOTSUPP;
421}
422
51e4e315
EB
423static inline int fscrypt_decrypt_pagecache_blocks(struct folio *folio,
424 size_t len, size_t offs)
643fa961
CR
425{
426 return -EOPNOTSUPP;
427}
428
41adbcb7
EB
429static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
430 struct page *page,
431 unsigned int len,
432 unsigned int offs, u64 lblk_num)
433{
434 return -EOPNOTSUPP;
435}
436
d2d0727b
EB
437static inline bool fscrypt_is_bounce_page(struct page *page)
438{
439 return false;
440}
441
442static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
643fa961
CR
443{
444 WARN_ON_ONCE(1);
445 return ERR_PTR(-EINVAL);
446}
447
d2d0727b 448static inline void fscrypt_free_bounce_page(struct page *bounce_page)
643fa961 449{
643fa961
CR
450}
451
452/* policy.c */
453static inline int fscrypt_ioctl_set_policy(struct file *filp,
454 const void __user *arg)
455{
456 return -EOPNOTSUPP;
457}
458
459static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
460{
461 return -EOPNOTSUPP;
462}
463
5dae460c
EB
464static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
465 void __user *arg)
466{
467 return -EOPNOTSUPP;
468}
469
e98ad464
EB
470static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
471{
472 return -EOPNOTSUPP;
473}
474
643fa961
CR
475static inline int fscrypt_has_permitted_context(struct inode *parent,
476 struct inode *child)
477{
478 return 0;
479}
480
a992b20c
EB
481static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
482{
483 return -EOPNOTSUPP;
484}
485
ac4acb1f 486struct fscrypt_dummy_policy {
ed318a6c
EB
487};
488
218d921b
EB
489static inline int
490fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
491 struct fscrypt_dummy_policy *dummy_policy)
492{
493 return -EINVAL;
494}
495
496static inline bool
497fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
498 const struct fscrypt_dummy_policy *p2)
499{
500 return true;
501}
502
ed318a6c
EB
503static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
504 char sep,
505 struct super_block *sb)
506{
507}
508
218d921b
EB
509static inline bool
510fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
511{
512 return false;
513}
514
ed318a6c 515static inline void
ac4acb1f 516fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
ed318a6c
EB
517{
518}
519
22d94f49 520/* keyring.c */
ccd30a47 521static inline void fscrypt_destroy_keyring(struct super_block *sb)
22d94f49
EB
522{
523}
524
525static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
526{
527 return -EOPNOTSUPP;
528}
529
b1c0ec35 530static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
78a1b96b
EB
531{
532 return -EOPNOTSUPP;
533}
534
535static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
536 void __user *arg)
b1c0ec35
EB
537{
538 return -EOPNOTSUPP;
539}
540
5a7e2992
EB
541static inline int fscrypt_ioctl_get_key_status(struct file *filp,
542 void __user *arg)
543{
544 return -EOPNOTSUPP;
545}
546
feed8258 547/* keysetup.c */
643fa961 548
a992b20c
EB
549static inline int fscrypt_prepare_new_inode(struct inode *dir,
550 struct inode *inode,
551 bool *encrypt_ret)
552{
553 if (IS_ENCRYPTED(dir))
554 return -EOPNOTSUPP;
555 return 0;
556}
557
643fa961
CR
558static inline void fscrypt_put_encryption_info(struct inode *inode)
559{
560 return;
561}
562
2c58d548
EB
563static inline void fscrypt_free_inode(struct inode *inode)
564{
565}
566
b1c0ec35
EB
567static inline int fscrypt_drop_inode(struct inode *inode)
568{
569 return 0;
570}
571
643fa961
CR
572 /* fname.c */
573static inline int fscrypt_setup_filename(struct inode *dir,
574 const struct qstr *iname,
575 int lookup, struct fscrypt_name *fname)
576{
577 if (IS_ENCRYPTED(dir))
578 return -EOPNOTSUPP;
579
b01531db 580 memset(fname, 0, sizeof(*fname));
643fa961
CR
581 fname->usr_fname = iname;
582 fname->disk_name.name = (unsigned char *)iname->name;
583 fname->disk_name.len = iname->len;
584 return 0;
585}
586
587static inline void fscrypt_free_filename(struct fscrypt_name *fname)
588{
589 return;
590}
591
8b10fe68 592static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
643fa961
CR
593 struct fscrypt_str *crypto_str)
594{
595 return -EOPNOTSUPP;
596}
597
598static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
599{
600 return;
601}
602
8a4ab0b8 603static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
643fa961
CR
604 u32 hash, u32 minor_hash,
605 const struct fscrypt_str *iname,
606 struct fscrypt_str *oname)
607{
608 return -EOPNOTSUPP;
609}
610
611static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
612 const u8 *de_name, u32 de_name_len)
613{
614 /* Encryption support disabled; use standard comparison */
615 if (de_name_len != fname->disk_name.len)
616 return false;
617 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
618}
619
aa408f83
DR
620static inline u64 fscrypt_fname_siphash(const struct inode *dir,
621 const struct qstr *name)
622{
623 WARN_ON_ONCE(1);
624 return 0;
625}
626
5b2a828b
EB
627static inline int fscrypt_d_revalidate(struct dentry *dentry,
628 unsigned int flags)
629{
630 return 1;
631}
632
643fa961 633/* bio.c */
14db0b3c 634static inline bool fscrypt_decrypt_bio(struct bio *bio)
643fa961 635{
14db0b3c 636 return true;
643fa961
CR
637}
638
643fa961
CR
639static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
640 sector_t pblk, unsigned int len)
641{
642 return -EOPNOTSUPP;
643}
644
645/* hooks.c */
646
647static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
648{
649 if (IS_ENCRYPTED(inode))
650 return -EOPNOTSUPP;
651 return 0;
652}
653
968dd6d0
EB
654static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
655 struct dentry *dentry)
643fa961
CR
656{
657 return -EOPNOTSUPP;
658}
659
660static inline int __fscrypt_prepare_rename(struct inode *old_dir,
661 struct dentry *old_dentry,
662 struct inode *new_dir,
663 struct dentry *new_dentry,
664 unsigned int flags)
665{
666 return -EOPNOTSUPP;
667}
668
669static inline int __fscrypt_prepare_lookup(struct inode *dir,
b01531db
EB
670 struct dentry *dentry,
671 struct fscrypt_name *fname)
643fa961
CR
672{
673 return -EOPNOTSUPP;
674}
675
ec0caa97
EB
676static inline int __fscrypt_prepare_readdir(struct inode *dir)
677{
678 return -EOPNOTSUPP;
679}
680
7622350e
EB
681static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
682 struct iattr *attr)
683{
684 return -EOPNOTSUPP;
685}
686
6e1918cf
DR
687static inline int fscrypt_prepare_setflags(struct inode *inode,
688 unsigned int oldflags,
689 unsigned int flags)
690{
691 return 0;
692}
693
31114726
EB
694static inline int fscrypt_prepare_symlink(struct inode *dir,
695 const char *target,
696 unsigned int len,
697 unsigned int max_len,
698 struct fscrypt_str *disk_link)
643fa961 699{
31114726
EB
700 if (IS_ENCRYPTED(dir))
701 return -EOPNOTSUPP;
702 disk_link->name = (unsigned char *)target;
703 disk_link->len = len + 1;
704 if (disk_link->len > max_len)
705 return -ENAMETOOLONG;
706 return 0;
643fa961
CR
707}
708
643fa961
CR
709static inline int __fscrypt_encrypt_symlink(struct inode *inode,
710 const char *target,
711 unsigned int len,
712 struct fscrypt_str *disk_link)
713{
714 return -EOPNOTSUPP;
715}
716
717static inline const char *fscrypt_get_symlink(struct inode *inode,
718 const void *caddr,
719 unsigned int max_size,
720 struct delayed_call *done)
721{
722 return ERR_PTR(-EOPNOTSUPP);
723}
eea2c05d 724
d1876056
EB
725static inline int fscrypt_symlink_getattr(const struct path *path,
726 struct kstat *stat)
727{
728 return -EOPNOTSUPP;
729}
730
eea2c05d
SH
731static inline void fscrypt_set_ops(struct super_block *sb,
732 const struct fscrypt_operations *s_cop)
733{
734}
735
643fa961 736#endif /* !CONFIG_FS_ENCRYPTION */
734f0d24 737
5fee3609
ST
738/* inline_crypt.c */
739#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
740
741bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
742
743void fscrypt_set_bio_crypt_ctx(struct bio *bio,
744 const struct inode *inode, u64 first_lblk,
745 gfp_t gfp_mask);
746
747void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
748 const struct buffer_head *first_bh,
749 gfp_t gfp_mask);
750
751bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
752 u64 next_lblk);
753
754bool fscrypt_mergeable_bio_bh(struct bio *bio,
755 const struct buffer_head *next_bh);
756
53dd3f80 757bool fscrypt_dio_supported(struct inode *inode);
c6c89783
EB
758
759u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
760
5fee3609
ST
761#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
762
763static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
764{
765 return false;
766}
767
768static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
769 const struct inode *inode,
770 u64 first_lblk, gfp_t gfp_mask) { }
771
772static inline void fscrypt_set_bio_crypt_ctx_bh(
773 struct bio *bio,
774 const struct buffer_head *first_bh,
775 gfp_t gfp_mask) { }
776
777static inline bool fscrypt_mergeable_bio(struct bio *bio,
778 const struct inode *inode,
779 u64 next_lblk)
780{
781 return true;
782}
783
784static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
785 const struct buffer_head *next_bh)
786{
787 return true;
788}
c6c89783 789
53dd3f80 790static inline bool fscrypt_dio_supported(struct inode *inode)
c6c89783 791{
c6c89783
EB
792 return !fscrypt_needs_contents_encryption(inode);
793}
794
795static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
796 u64 nr_blocks)
797{
798 return nr_blocks;
799}
5fee3609
ST
800#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
801
802/**
803 * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
804 * encryption
805 * @inode: an inode. If encrypted, its key must be set up.
806 *
807 * Return: true if the inode requires file contents encryption and if the
808 * encryption should be done in the block layer via blk-crypto rather
809 * than in the filesystem layer.
810 */
811static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
812{
813 return fscrypt_needs_contents_encryption(inode) &&
814 __fscrypt_inode_uses_inline_crypto(inode);
815}
816
817/**
818 * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
819 * encryption
820 * @inode: an inode. If encrypted, its key must be set up.
821 *
822 * Return: true if the inode requires file contents encryption and if the
823 * encryption should be done in the filesystem layer rather than in the
824 * block layer via blk-crypto.
825 */
826static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
827{
828 return fscrypt_needs_contents_encryption(inode) &&
829 !__fscrypt_inode_uses_inline_crypto(inode);
830}
831
ab673b98
EB
832/**
833 * fscrypt_has_encryption_key() - check whether an inode has had its key set up
834 * @inode: the inode to check
835 *
836 * Return: %true if the inode has had its encryption key set up, else %false.
837 *
838 * Usually this should be preceded by fscrypt_get_encryption_info() to try to
839 * set up the key first.
840 */
841static inline bool fscrypt_has_encryption_key(const struct inode *inode)
842{
843 return fscrypt_get_info(inode) != NULL;
844}
845
0ea87a96 846/**
d2fe9754
EB
847 * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
848 * directory
0ea87a96
EB
849 * @old_dentry: an existing dentry for the inode being linked
850 * @dir: the target directory
851 * @dentry: negative dentry for the target filename
852 *
853 * A new link can only be added to an encrypted directory if the directory's
854 * encryption key is available --- since otherwise we'd have no way to encrypt
234f1b7f 855 * the filename.
0ea87a96
EB
856 *
857 * We also verify that the link will not violate the constraint that all files
858 * in an encrypted directory tree use the same encryption policy.
859 *
860 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
f5e55e77 861 * -EXDEV if the link would result in an inconsistent encryption policy, or
0ea87a96
EB
862 * another -errno code.
863 */
864static inline int fscrypt_prepare_link(struct dentry *old_dentry,
865 struct inode *dir,
866 struct dentry *dentry)
867{
868 if (IS_ENCRYPTED(dir))
968dd6d0 869 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
0ea87a96
EB
870 return 0;
871}
872
94b26f36 873/**
d2fe9754
EB
874 * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
875 * directories
94b26f36
EB
876 * @old_dir: source directory
877 * @old_dentry: dentry for source file
878 * @new_dir: target directory
879 * @new_dentry: dentry for target location (may be negative unless exchanging)
880 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
881 *
882 * Prepare for ->rename() where the source and/or target directories may be
883 * encrypted. A new link can only be added to an encrypted directory if the
884 * directory's encryption key is available --- since otherwise we'd have no way
885 * to encrypt the filename. A rename to an existing name, on the other hand,
886 * *is* cryptographically possible without the key. However, we take the more
887 * conservative approach and just forbid all no-key renames.
888 *
889 * We also verify that the rename will not violate the constraint that all files
890 * in an encrypted directory tree use the same encryption policy.
891 *
f5e55e77 892 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
94b26f36
EB
893 * rename would cause inconsistent encryption policies, or another -errno code.
894 */
895static inline int fscrypt_prepare_rename(struct inode *old_dir,
896 struct dentry *old_dentry,
897 struct inode *new_dir,
898 struct dentry *new_dentry,
899 unsigned int flags)
900{
901 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
902 return __fscrypt_prepare_rename(old_dir, old_dentry,
903 new_dir, new_dentry, flags);
904 return 0;
905}
906
32c3cf02 907/**
d2fe9754
EB
908 * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
909 * directory
32c3cf02
EB
910 * @dir: directory being searched
911 * @dentry: filename being looked up
b01531db 912 * @fname: (output) the name to use to search the on-disk directory
32c3cf02 913 *
b01531db 914 * Prepare for ->lookup() in a directory which may be encrypted by determining
70fb2612 915 * the name that will actually be used to search the directory on-disk. If the
a14d0b67
EB
916 * directory's encryption policy is supported by this kernel and its encryption
917 * key is available, then the lookup is assumed to be by plaintext name;
918 * otherwise, it is assumed to be by no-key name.
32c3cf02 919 *
bb9cd910
DR
920 * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
921 * name. In this case the filesystem must assign the dentry a dentry_operations
922 * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
923 * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
924 * directory's encryption key is later added.
32c3cf02 925 *
70fb2612
EB
926 * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
927 * filename isn't a valid no-key name, so a negative dentry should be created;
928 * or another -errno code.
32c3cf02
EB
929 */
930static inline int fscrypt_prepare_lookup(struct inode *dir,
931 struct dentry *dentry,
b01531db 932 struct fscrypt_name *fname)
32c3cf02
EB
933{
934 if (IS_ENCRYPTED(dir))
b01531db
EB
935 return __fscrypt_prepare_lookup(dir, dentry, fname);
936
937 memset(fname, 0, sizeof(*fname));
938 fname->usr_fname = &dentry->d_name;
939 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
940 fname->disk_name.len = dentry->d_name.len;
32c3cf02
EB
941 return 0;
942}
943
ec0caa97
EB
944/**
945 * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
946 * @dir: the directory inode
947 *
948 * If the directory is encrypted and it doesn't already have its encryption key
949 * set up, try to set it up so that the filenames will be listed in plaintext
950 * form rather than in no-key form.
951 *
952 * Return: 0 on success; -errno on error. Note that the encryption key being
a14d0b67
EB
953 * unavailable is not considered an error. It is also not an error if
954 * the encryption policy is unsupported by this kernel; that is treated
955 * like the key being unavailable, so that files can still be deleted.
ec0caa97
EB
956 */
957static inline int fscrypt_prepare_readdir(struct inode *dir)
958{
959 if (IS_ENCRYPTED(dir))
960 return __fscrypt_prepare_readdir(dir);
961 return 0;
962}
963
815dac33 964/**
d2fe9754
EB
965 * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
966 * attributes
815dac33
EB
967 * @dentry: dentry through which the inode is being changed
968 * @attr: attributes to change
969 *
970 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
971 * most attribute changes are allowed even without the encryption key. However,
972 * without the encryption key we do have to forbid truncates. This is needed
973 * because the size being truncated to may not be a multiple of the filesystem
974 * block size, and in that case we'd have to decrypt the final block, zero the
975 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
976 * filesystem block boundary, but it's simpler to just forbid all truncates ---
977 * and we already forbid all other contents modifications without the key.)
978 *
979 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
980 * if a problem occurred while setting up the encryption key.
981 */
982static inline int fscrypt_prepare_setattr(struct dentry *dentry,
983 struct iattr *attr)
984{
7622350e
EB
985 if (IS_ENCRYPTED(d_inode(dentry)))
986 return __fscrypt_prepare_setattr(dentry, attr);
815dac33
EB
987 return 0;
988}
989
76e81d6d 990/**
d2fe9754 991 * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
76e81d6d
EB
992 * @inode: symlink inode
993 * @target: plaintext symlink target
994 * @len: length of @target excluding null terminator
995 * @disk_link: (in/out) the on-disk symlink target being prepared
996 *
997 * If the symlink target needs to be encrypted, then this function encrypts it
998 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
999 * previously to compute @disk_link->len. If the filesystem did not allocate a
1000 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
1001 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
1002 *
1003 * Return: 0 on success, -errno on failure
1004 */
1005static inline int fscrypt_encrypt_symlink(struct inode *inode,
1006 const char *target,
1007 unsigned int len,
1008 struct fscrypt_str *disk_link)
1009{
1010 if (IS_ENCRYPTED(inode))
1011 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
1012 return 0;
1013}
1014
d2d0727b
EB
1015/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
1016static inline void fscrypt_finalize_bounce_page(struct page **pagep)
1017{
1018 struct page *page = *pagep;
1019
1020 if (fscrypt_is_bounce_page(page)) {
1021 *pagep = fscrypt_pagecache_page(page);
1022 fscrypt_free_bounce_page(page);
1023 }
1024}
1025
734f0d24 1026#endif /* _LINUX_FSCRYPT_H */