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