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