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