Merge tag '5.19-rc-ksmbd-server-fixes' of git://git.samba.org/ksmbd
[linux-2.6-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 /*
164 * Return the number of block devices to which the filesystem may write
165 * encrypted file contents.
166 *
167 * If the filesystem can use multiple block devices (other than block
168 * devices that aren't used for encrypted file contents, such as
169 * external journal devices), and wants to support inline encryption,
170 * then it must implement this function. Otherwise it's not needed.
171 */
5fee3609 172 int (*get_num_devices)(struct super_block *sb);
38ef66b0
EB
173
174 /*
175 * If ->get_num_devices() returns a value greater than 1, then this
176 * function is called to get the array of request_queues that the
177 * filesystem is using -- one per block device. (There may be duplicate
178 * entries in this array, as block devices can share a request_queue.)
179 */
5fee3609
ST
180 void (*get_devices)(struct super_block *sb,
181 struct request_queue **devs);
643fa961
CR
182};
183
ab673b98 184static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
643fa961 185{
ab673b98 186 /*
5b421f08 187 * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
ab673b98
EB
188 * I.e., another task may publish ->i_crypt_info concurrently, executing
189 * a RELEASE barrier. We need to use smp_load_acquire() here to safely
190 * ACQUIRE the memory the other task published.
191 */
192 return smp_load_acquire(&inode->i_crypt_info);
643fa961
CR
193}
194
56dce717
EB
195/**
196 * fscrypt_needs_contents_encryption() - check whether an inode needs
197 * contents encryption
d2fe9754 198 * @inode: the inode to check
56dce717
EB
199 *
200 * Return: %true iff the inode is an encrypted regular file and the kernel was
201 * built with fscrypt support.
202 *
203 * If you need to know whether the encrypt bit is set even when the kernel was
204 * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
205 */
206static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
207{
208 return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
209}
210
0bf3d5c1 211/*
501e43fb
EB
212 * When d_splice_alias() moves a directory's no-key alias to its plaintext alias
213 * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be
214 * cleared. Note that we don't have to support arbitrary moves of this flag
215 * because fscrypt doesn't allow no-key names to be the source or target of a
216 * rename().
0bf3d5c1
EB
217 */
218static inline void fscrypt_handle_d_move(struct dentry *dentry)
219{
501e43fb 220 dentry->d_flags &= ~DCACHE_NOKEY_NAME;
0bf3d5c1
EB
221}
222
159e1de2
EB
223/**
224 * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
225 * @dentry: the dentry to check
226 *
227 * This returns true if the dentry is a no-key dentry. A no-key dentry is a
228 * dentry that was created in an encrypted directory that hasn't had its
229 * encryption key added yet. Such dentries may be either positive or negative.
230 *
231 * When a filesystem is asked to create a new filename in an encrypted directory
232 * and the new filename's dentry is a no-key dentry, it must fail the operation
233 * with ENOKEY. This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
234 * ->rename(), and ->link(). (However, ->rename() and ->link() are already
235 * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
236 *
237 * This is necessary because creating a filename requires the directory's
238 * encryption key, but just checking for the key on the directory inode during
239 * the final filesystem operation doesn't guarantee that the key was available
240 * during the preceding dentry lookup. And the key must have already been
241 * available during the dentry lookup in order for it to have been checked
242 * whether the filename already exists in the directory and for the new file's
243 * dentry not to be invalidated due to it incorrectly having the no-key flag.
244 *
245 * Return: %true if the dentry is a no-key name
246 */
247static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
248{
249 return dentry->d_flags & DCACHE_NOKEY_NAME;
250}
251
643fa961 252/* crypto.c */
60700902
EB
253void fscrypt_enqueue_decrypt_work(struct work_struct *);
254
255struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
256 unsigned int len,
257 unsigned int offs,
258 gfp_t gfp_flags);
259int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
260 unsigned int len, unsigned int offs,
261 u64 lblk_num, gfp_t gfp_flags);
262
263int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
264 unsigned int offs);
265int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
266 unsigned int len, unsigned int offs,
267 u64 lblk_num);
643fa961 268
d2d0727b 269static inline bool fscrypt_is_bounce_page(struct page *page)
643fa961 270{
d2d0727b 271 return page->mapping == NULL;
643fa961
CR
272}
273
d2d0727b
EB
274static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
275{
276 return (struct page *)page_private(bounce_page);
277}
278
60700902 279void fscrypt_free_bounce_page(struct page *bounce_page);
643fa961
CR
280
281/* policy.c */
60700902
EB
282int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
283int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
284int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
285int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
286int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
a992b20c 287int fscrypt_set_context(struct inode *inode, void *fs_data);
fe015a78 288
ac4acb1f
EB
289struct fscrypt_dummy_policy {
290 const union fscrypt_policy *policy;
ed318a6c
EB
291};
292
218d921b
EB
293int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
294 struct fscrypt_dummy_policy *dummy_policy);
295bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
296 const struct fscrypt_dummy_policy *p2);
c8c868ab 297int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
ac4acb1f 298 struct fscrypt_dummy_policy *dummy_policy);
ed318a6c
EB
299void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
300 struct super_block *sb);
218d921b
EB
301static inline bool
302fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
303{
304 return dummy_policy->policy != NULL;
305}
ed318a6c 306static inline void
ac4acb1f 307fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
ed318a6c 308{
ac4acb1f
EB
309 kfree(dummy_policy->policy);
310 dummy_policy->policy = NULL;
ed318a6c
EB
311}
312
22d94f49 313/* keyring.c */
60700902
EB
314void fscrypt_sb_free(struct super_block *sb);
315int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
218d921b
EB
316int fscrypt_add_test_dummy_key(struct super_block *sb,
317 const struct fscrypt_dummy_policy *dummy_policy);
60700902
EB
318int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
319int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
320int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
22d94f49 321
feed8258 322/* keysetup.c */
a992b20c
EB
323int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
324 bool *encrypt_ret);
60700902
EB
325void fscrypt_put_encryption_info(struct inode *inode);
326void fscrypt_free_inode(struct inode *inode);
327int fscrypt_drop_inode(struct inode *inode);
643fa961
CR
328
329/* fname.c */
60700902
EB
330int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
331 int lookup, struct fscrypt_name *fname);
643fa961
CR
332
333static inline void fscrypt_free_filename(struct fscrypt_name *fname)
334{
335 kfree(fname->crypto_buf.name);
336}
337
8b10fe68 338int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
60700902
EB
339 struct fscrypt_str *crypto_str);
340void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
341int fscrypt_fname_disk_to_usr(const struct inode *inode,
342 u32 hash, u32 minor_hash,
343 const struct fscrypt_str *iname,
344 struct fscrypt_str *oname);
345bool fscrypt_match_name(const struct fscrypt_name *fname,
346 const u8 *de_name, u32 de_name_len);
347u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
5b2a828b 348int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
aa408f83 349
643fa961 350/* bio.c */
60700902
EB
351void fscrypt_decrypt_bio(struct bio *bio);
352int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
353 sector_t pblk, unsigned int len);
643fa961
CR
354
355/* hooks.c */
60700902
EB
356int fscrypt_file_open(struct inode *inode, struct file *filp);
357int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
358 struct dentry *dentry);
359int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
360 struct inode *new_dir, struct dentry *new_dentry,
361 unsigned int flags);
362int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
363 struct fscrypt_name *fname);
ec0caa97 364int __fscrypt_prepare_readdir(struct inode *dir);
7622350e 365int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
60700902
EB
366int fscrypt_prepare_setflags(struct inode *inode,
367 unsigned int oldflags, unsigned int flags);
31114726
EB
368int fscrypt_prepare_symlink(struct inode *dir, const char *target,
369 unsigned int len, unsigned int max_len,
370 struct fscrypt_str *disk_link);
60700902
EB
371int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
372 unsigned int len, struct fscrypt_str *disk_link);
373const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
374 unsigned int max_size,
375 struct delayed_call *done);
d1876056 376int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
eea2c05d
SH
377static inline void fscrypt_set_ops(struct super_block *sb,
378 const struct fscrypt_operations *s_cop)
379{
380 sb->s_cop = s_cop;
381}
643fa961
CR
382#else /* !CONFIG_FS_ENCRYPTION */
383
ab673b98 384static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
643fa961 385{
ab673b98 386 return NULL;
643fa961
CR
387}
388
56dce717
EB
389static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
390{
391 return false;
392}
393
0bf3d5c1
EB
394static inline void fscrypt_handle_d_move(struct dentry *dentry)
395{
396}
397
159e1de2
EB
398static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
399{
400 return false;
401}
402
643fa961
CR
403/* crypto.c */
404static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
405{
406}
407
53bc1d85
EB
408static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
409 unsigned int len,
410 unsigned int offs,
411 gfp_t gfp_flags)
643fa961
CR
412{
413 return ERR_PTR(-EOPNOTSUPP);
414}
415
03569f2f
EB
416static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
417 struct page *page,
418 unsigned int len,
419 unsigned int offs, u64 lblk_num,
420 gfp_t gfp_flags)
421{
422 return -EOPNOTSUPP;
423}
424
aa8bc1ac
EB
425static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
426 unsigned int len,
427 unsigned int offs)
643fa961
CR
428{
429 return -EOPNOTSUPP;
430}
431
41adbcb7
EB
432static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
433 struct page *page,
434 unsigned int len,
435 unsigned int offs, u64 lblk_num)
436{
437 return -EOPNOTSUPP;
438}
439
d2d0727b
EB
440static inline bool fscrypt_is_bounce_page(struct page *page)
441{
442 return false;
443}
444
445static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
643fa961
CR
446{
447 WARN_ON_ONCE(1);
448 return ERR_PTR(-EINVAL);
449}
450
d2d0727b 451static inline void fscrypt_free_bounce_page(struct page *bounce_page)
643fa961 452{
643fa961
CR
453}
454
455/* policy.c */
456static inline int fscrypt_ioctl_set_policy(struct file *filp,
457 const void __user *arg)
458{
459 return -EOPNOTSUPP;
460}
461
462static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
463{
464 return -EOPNOTSUPP;
465}
466
5dae460c
EB
467static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
468 void __user *arg)
469{
470 return -EOPNOTSUPP;
471}
472
e98ad464
EB
473static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
474{
475 return -EOPNOTSUPP;
476}
477
643fa961
CR
478static inline int fscrypt_has_permitted_context(struct inode *parent,
479 struct inode *child)
480{
481 return 0;
482}
483
a992b20c
EB
484static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
485{
486 return -EOPNOTSUPP;
487}
488
ac4acb1f 489struct fscrypt_dummy_policy {
ed318a6c
EB
490};
491
218d921b
EB
492static inline int
493fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
494 struct fscrypt_dummy_policy *dummy_policy)
495{
496 return -EINVAL;
497}
498
499static inline bool
500fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
501 const struct fscrypt_dummy_policy *p2)
502{
503 return true;
504}
505
ed318a6c
EB
506static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
507 char sep,
508 struct super_block *sb)
509{
510}
511
218d921b
EB
512static inline bool
513fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
514{
515 return false;
516}
517
ed318a6c 518static inline void
ac4acb1f 519fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
ed318a6c
EB
520{
521}
522
22d94f49
EB
523/* keyring.c */
524static inline void fscrypt_sb_free(struct super_block *sb)
525{
526}
527
528static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
529{
530 return -EOPNOTSUPP;
531}
532
218d921b
EB
533static inline int
534fscrypt_add_test_dummy_key(struct super_block *sb,
535 const struct fscrypt_dummy_policy *dummy_policy)
536{
537 return 0;
538}
539
b1c0ec35 540static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
78a1b96b
EB
541{
542 return -EOPNOTSUPP;
543}
544
545static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
546 void __user *arg)
b1c0ec35
EB
547{
548 return -EOPNOTSUPP;
549}
550
5a7e2992
EB
551static inline int fscrypt_ioctl_get_key_status(struct file *filp,
552 void __user *arg)
553{
554 return -EOPNOTSUPP;
555}
556
feed8258 557/* keysetup.c */
643fa961 558
a992b20c
EB
559static inline int fscrypt_prepare_new_inode(struct inode *dir,
560 struct inode *inode,
561 bool *encrypt_ret)
562{
563 if (IS_ENCRYPTED(dir))
564 return -EOPNOTSUPP;
565 return 0;
566}
567
643fa961
CR
568static inline void fscrypt_put_encryption_info(struct inode *inode)
569{
570 return;
571}
572
2c58d548
EB
573static inline void fscrypt_free_inode(struct inode *inode)
574{
575}
576
b1c0ec35
EB
577static inline int fscrypt_drop_inode(struct inode *inode)
578{
579 return 0;
580}
581
643fa961
CR
582 /* fname.c */
583static inline int fscrypt_setup_filename(struct inode *dir,
584 const struct qstr *iname,
585 int lookup, struct fscrypt_name *fname)
586{
587 if (IS_ENCRYPTED(dir))
588 return -EOPNOTSUPP;
589
b01531db 590 memset(fname, 0, sizeof(*fname));
643fa961
CR
591 fname->usr_fname = iname;
592 fname->disk_name.name = (unsigned char *)iname->name;
593 fname->disk_name.len = iname->len;
594 return 0;
595}
596
597static inline void fscrypt_free_filename(struct fscrypt_name *fname)
598{
599 return;
600}
601
8b10fe68 602static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
643fa961
CR
603 struct fscrypt_str *crypto_str)
604{
605 return -EOPNOTSUPP;
606}
607
608static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
609{
610 return;
611}
612
8a4ab0b8 613static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
643fa961
CR
614 u32 hash, u32 minor_hash,
615 const struct fscrypt_str *iname,
616 struct fscrypt_str *oname)
617{
618 return -EOPNOTSUPP;
619}
620
621static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
622 const u8 *de_name, u32 de_name_len)
623{
624 /* Encryption support disabled; use standard comparison */
625 if (de_name_len != fname->disk_name.len)
626 return false;
627 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
628}
629
aa408f83
DR
630static inline u64 fscrypt_fname_siphash(const struct inode *dir,
631 const struct qstr *name)
632{
633 WARN_ON_ONCE(1);
634 return 0;
635}
636
5b2a828b
EB
637static inline int fscrypt_d_revalidate(struct dentry *dentry,
638 unsigned int flags)
639{
640 return 1;
641}
642
643fa961
CR
643/* bio.c */
644static inline void fscrypt_decrypt_bio(struct bio *bio)
645{
646}
647
643fa961
CR
648static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
649 sector_t pblk, unsigned int len)
650{
651 return -EOPNOTSUPP;
652}
653
654/* hooks.c */
655
656static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
657{
658 if (IS_ENCRYPTED(inode))
659 return -EOPNOTSUPP;
660 return 0;
661}
662
968dd6d0
EB
663static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
664 struct dentry *dentry)
643fa961
CR
665{
666 return -EOPNOTSUPP;
667}
668
669static inline int __fscrypt_prepare_rename(struct inode *old_dir,
670 struct dentry *old_dentry,
671 struct inode *new_dir,
672 struct dentry *new_dentry,
673 unsigned int flags)
674{
675 return -EOPNOTSUPP;
676}
677
678static inline int __fscrypt_prepare_lookup(struct inode *dir,
b01531db
EB
679 struct dentry *dentry,
680 struct fscrypt_name *fname)
643fa961
CR
681{
682 return -EOPNOTSUPP;
683}
684
ec0caa97
EB
685static inline int __fscrypt_prepare_readdir(struct inode *dir)
686{
687 return -EOPNOTSUPP;
688}
689
7622350e
EB
690static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
691 struct iattr *attr)
692{
693 return -EOPNOTSUPP;
694}
695
6e1918cf
DR
696static inline int fscrypt_prepare_setflags(struct inode *inode,
697 unsigned int oldflags,
698 unsigned int flags)
699{
700 return 0;
701}
702
31114726
EB
703static inline int fscrypt_prepare_symlink(struct inode *dir,
704 const char *target,
705 unsigned int len,
706 unsigned int max_len,
707 struct fscrypt_str *disk_link)
643fa961 708{
31114726
EB
709 if (IS_ENCRYPTED(dir))
710 return -EOPNOTSUPP;
711 disk_link->name = (unsigned char *)target;
712 disk_link->len = len + 1;
713 if (disk_link->len > max_len)
714 return -ENAMETOOLONG;
715 return 0;
643fa961
CR
716}
717
643fa961
CR
718static inline int __fscrypt_encrypt_symlink(struct inode *inode,
719 const char *target,
720 unsigned int len,
721 struct fscrypt_str *disk_link)
722{
723 return -EOPNOTSUPP;
724}
725
726static inline const char *fscrypt_get_symlink(struct inode *inode,
727 const void *caddr,
728 unsigned int max_size,
729 struct delayed_call *done)
730{
731 return ERR_PTR(-EOPNOTSUPP);
732}
eea2c05d 733
d1876056
EB
734static inline int fscrypt_symlink_getattr(const struct path *path,
735 struct kstat *stat)
736{
737 return -EOPNOTSUPP;
738}
739
eea2c05d
SH
740static inline void fscrypt_set_ops(struct super_block *sb,
741 const struct fscrypt_operations *s_cop)
742{
743}
744
643fa961 745#endif /* !CONFIG_FS_ENCRYPTION */
734f0d24 746
5fee3609
ST
747/* inline_crypt.c */
748#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
749
750bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
751
752void fscrypt_set_bio_crypt_ctx(struct bio *bio,
753 const struct inode *inode, u64 first_lblk,
754 gfp_t gfp_mask);
755
756void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
757 const struct buffer_head *first_bh,
758 gfp_t gfp_mask);
759
760bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
761 u64 next_lblk);
762
763bool fscrypt_mergeable_bio_bh(struct bio *bio,
764 const struct buffer_head *next_bh);
765
c6c89783
EB
766bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter);
767
768u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
769
5fee3609
ST
770#else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
771
772static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
773{
774 return false;
775}
776
777static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
778 const struct inode *inode,
779 u64 first_lblk, gfp_t gfp_mask) { }
780
781static inline void fscrypt_set_bio_crypt_ctx_bh(
782 struct bio *bio,
783 const struct buffer_head *first_bh,
784 gfp_t gfp_mask) { }
785
786static inline bool fscrypt_mergeable_bio(struct bio *bio,
787 const struct inode *inode,
788 u64 next_lblk)
789{
790 return true;
791}
792
793static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
794 const struct buffer_head *next_bh)
795{
796 return true;
797}
c6c89783
EB
798
799static inline bool fscrypt_dio_supported(struct kiocb *iocb,
800 struct iov_iter *iter)
801{
802 const struct inode *inode = file_inode(iocb->ki_filp);
803
804 return !fscrypt_needs_contents_encryption(inode);
805}
806
807static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
808 u64 nr_blocks)
809{
810 return nr_blocks;
811}
5fee3609
ST
812#endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
813
814/**
815 * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
816 * encryption
817 * @inode: an inode. If encrypted, its key must be set up.
818 *
819 * Return: true if the inode requires file contents encryption and if the
820 * encryption should be done in the block layer via blk-crypto rather
821 * than in the filesystem layer.
822 */
823static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
824{
825 return fscrypt_needs_contents_encryption(inode) &&
826 __fscrypt_inode_uses_inline_crypto(inode);
827}
828
829/**
830 * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
831 * encryption
832 * @inode: an inode. If encrypted, its key must be set up.
833 *
834 * Return: true if the inode requires file contents encryption and if the
835 * encryption should be done in the filesystem layer rather than in the
836 * block layer via blk-crypto.
837 */
838static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
839{
840 return fscrypt_needs_contents_encryption(inode) &&
841 !__fscrypt_inode_uses_inline_crypto(inode);
842}
843
ab673b98
EB
844/**
845 * fscrypt_has_encryption_key() - check whether an inode has had its key set up
846 * @inode: the inode to check
847 *
848 * Return: %true if the inode has had its encryption key set up, else %false.
849 *
850 * Usually this should be preceded by fscrypt_get_encryption_info() to try to
851 * set up the key first.
852 */
853static inline bool fscrypt_has_encryption_key(const struct inode *inode)
854{
855 return fscrypt_get_info(inode) != NULL;
856}
857
0ea87a96 858/**
d2fe9754
EB
859 * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
860 * directory
0ea87a96
EB
861 * @old_dentry: an existing dentry for the inode being linked
862 * @dir: the target directory
863 * @dentry: negative dentry for the target filename
864 *
865 * A new link can only be added to an encrypted directory if the directory's
866 * encryption key is available --- since otherwise we'd have no way to encrypt
234f1b7f 867 * the filename.
0ea87a96
EB
868 *
869 * We also verify that the link will not violate the constraint that all files
870 * in an encrypted directory tree use the same encryption policy.
871 *
872 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
f5e55e77 873 * -EXDEV if the link would result in an inconsistent encryption policy, or
0ea87a96
EB
874 * another -errno code.
875 */
876static inline int fscrypt_prepare_link(struct dentry *old_dentry,
877 struct inode *dir,
878 struct dentry *dentry)
879{
880 if (IS_ENCRYPTED(dir))
968dd6d0 881 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
0ea87a96
EB
882 return 0;
883}
884
94b26f36 885/**
d2fe9754
EB
886 * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
887 * directories
94b26f36
EB
888 * @old_dir: source directory
889 * @old_dentry: dentry for source file
890 * @new_dir: target directory
891 * @new_dentry: dentry for target location (may be negative unless exchanging)
892 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
893 *
894 * Prepare for ->rename() where the source and/or target directories may be
895 * encrypted. A new link can only be added to an encrypted directory if the
896 * directory's encryption key is available --- since otherwise we'd have no way
897 * to encrypt the filename. A rename to an existing name, on the other hand,
898 * *is* cryptographically possible without the key. However, we take the more
899 * conservative approach and just forbid all no-key renames.
900 *
901 * We also verify that the rename will not violate the constraint that all files
902 * in an encrypted directory tree use the same encryption policy.
903 *
f5e55e77 904 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
94b26f36
EB
905 * rename would cause inconsistent encryption policies, or another -errno code.
906 */
907static inline int fscrypt_prepare_rename(struct inode *old_dir,
908 struct dentry *old_dentry,
909 struct inode *new_dir,
910 struct dentry *new_dentry,
911 unsigned int flags)
912{
913 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
914 return __fscrypt_prepare_rename(old_dir, old_dentry,
915 new_dir, new_dentry, flags);
916 return 0;
917}
918
32c3cf02 919/**
d2fe9754
EB
920 * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
921 * directory
32c3cf02
EB
922 * @dir: directory being searched
923 * @dentry: filename being looked up
b01531db 924 * @fname: (output) the name to use to search the on-disk directory
32c3cf02 925 *
b01531db 926 * Prepare for ->lookup() in a directory which may be encrypted by determining
70fb2612 927 * the name that will actually be used to search the directory on-disk. If the
a14d0b67
EB
928 * directory's encryption policy is supported by this kernel and its encryption
929 * key is available, then the lookup is assumed to be by plaintext name;
930 * otherwise, it is assumed to be by no-key name.
32c3cf02 931 *
bb9cd910
DR
932 * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
933 * name. In this case the filesystem must assign the dentry a dentry_operations
934 * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
935 * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
936 * directory's encryption key is later added.
32c3cf02 937 *
70fb2612
EB
938 * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
939 * filename isn't a valid no-key name, so a negative dentry should be created;
940 * or another -errno code.
32c3cf02
EB
941 */
942static inline int fscrypt_prepare_lookup(struct inode *dir,
943 struct dentry *dentry,
b01531db 944 struct fscrypt_name *fname)
32c3cf02
EB
945{
946 if (IS_ENCRYPTED(dir))
b01531db
EB
947 return __fscrypt_prepare_lookup(dir, dentry, fname);
948
949 memset(fname, 0, sizeof(*fname));
950 fname->usr_fname = &dentry->d_name;
951 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
952 fname->disk_name.len = dentry->d_name.len;
32c3cf02
EB
953 return 0;
954}
955
ec0caa97
EB
956/**
957 * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
958 * @dir: the directory inode
959 *
960 * If the directory is encrypted and it doesn't already have its encryption key
961 * set up, try to set it up so that the filenames will be listed in plaintext
962 * form rather than in no-key form.
963 *
964 * Return: 0 on success; -errno on error. Note that the encryption key being
a14d0b67
EB
965 * unavailable is not considered an error. It is also not an error if
966 * the encryption policy is unsupported by this kernel; that is treated
967 * like the key being unavailable, so that files can still be deleted.
ec0caa97
EB
968 */
969static inline int fscrypt_prepare_readdir(struct inode *dir)
970{
971 if (IS_ENCRYPTED(dir))
972 return __fscrypt_prepare_readdir(dir);
973 return 0;
974}
975
815dac33 976/**
d2fe9754
EB
977 * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
978 * attributes
815dac33
EB
979 * @dentry: dentry through which the inode is being changed
980 * @attr: attributes to change
981 *
982 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
983 * most attribute changes are allowed even without the encryption key. However,
984 * without the encryption key we do have to forbid truncates. This is needed
985 * because the size being truncated to may not be a multiple of the filesystem
986 * block size, and in that case we'd have to decrypt the final block, zero the
987 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
988 * filesystem block boundary, but it's simpler to just forbid all truncates ---
989 * and we already forbid all other contents modifications without the key.)
990 *
991 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
992 * if a problem occurred while setting up the encryption key.
993 */
994static inline int fscrypt_prepare_setattr(struct dentry *dentry,
995 struct iattr *attr)
996{
7622350e
EB
997 if (IS_ENCRYPTED(d_inode(dentry)))
998 return __fscrypt_prepare_setattr(dentry, attr);
815dac33
EB
999 return 0;
1000}
1001
76e81d6d 1002/**
d2fe9754 1003 * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
76e81d6d
EB
1004 * @inode: symlink inode
1005 * @target: plaintext symlink target
1006 * @len: length of @target excluding null terminator
1007 * @disk_link: (in/out) the on-disk symlink target being prepared
1008 *
1009 * If the symlink target needs to be encrypted, then this function encrypts it
1010 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
1011 * previously to compute @disk_link->len. If the filesystem did not allocate a
1012 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
1013 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
1014 *
1015 * Return: 0 on success, -errno on failure
1016 */
1017static inline int fscrypt_encrypt_symlink(struct inode *inode,
1018 const char *target,
1019 unsigned int len,
1020 struct fscrypt_str *disk_link)
1021{
1022 if (IS_ENCRYPTED(inode))
1023 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
1024 return 0;
1025}
1026
d2d0727b
EB
1027/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
1028static inline void fscrypt_finalize_bounce_page(struct page **pagep)
1029{
1030 struct page *page = *pagep;
1031
1032 if (fscrypt_is_bounce_page(page)) {
1033 *pagep = fscrypt_pagecache_page(page);
1034 fscrypt_free_bounce_page(page);
1035 }
1036}
1037
734f0d24 1038#endif /* _LINUX_FSCRYPT_H */