mfd: ab8500-debugfs: Remove the racy fiddling with irq_desc
[linux-2.6-microblaze.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_get_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 /* crypto.c */
115 void fscrypt_enqueue_decrypt_work(struct work_struct *);
116
117 struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
118                                               unsigned int len,
119                                               unsigned int offs,
120                                               gfp_t gfp_flags);
121 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
122                                   unsigned int len, unsigned int offs,
123                                   u64 lblk_num, gfp_t gfp_flags);
124
125 int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
126                                      unsigned int offs);
127 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
128                                   unsigned int len, unsigned int offs,
129                                   u64 lblk_num);
130
131 static inline bool fscrypt_is_bounce_page(struct page *page)
132 {
133         return page->mapping == NULL;
134 }
135
136 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
137 {
138         return (struct page *)page_private(bounce_page);
139 }
140
141 void fscrypt_free_bounce_page(struct page *bounce_page);
142
143 /* policy.c */
144 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
145 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
146 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
147 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
148 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
149 int fscrypt_set_context(struct inode *inode, void *fs_data);
150
151 struct fscrypt_dummy_policy {
152         const union fscrypt_policy *policy;
153 };
154
155 int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
156                                 struct fscrypt_dummy_policy *dummy_policy);
157 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
158                                         struct super_block *sb);
159 static inline void
160 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
161 {
162         kfree(dummy_policy->policy);
163         dummy_policy->policy = NULL;
164 }
165
166 /* keyring.c */
167 void fscrypt_sb_free(struct super_block *sb);
168 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
169 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
170 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
171 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
172
173 /* keysetup.c */
174 int fscrypt_get_encryption_info(struct inode *inode);
175 int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
176                               bool *encrypt_ret);
177 void fscrypt_put_encryption_info(struct inode *inode);
178 void fscrypt_free_inode(struct inode *inode);
179 int fscrypt_drop_inode(struct inode *inode);
180
181 /* fname.c */
182 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
183                            int lookup, struct fscrypt_name *fname);
184
185 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
186 {
187         kfree(fname->crypto_buf.name);
188 }
189
190 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
191                                struct fscrypt_str *crypto_str);
192 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
193 int fscrypt_fname_disk_to_usr(const struct inode *inode,
194                               u32 hash, u32 minor_hash,
195                               const struct fscrypt_str *iname,
196                               struct fscrypt_str *oname);
197 bool fscrypt_match_name(const struct fscrypt_name *fname,
198                         const u8 *de_name, u32 de_name_len);
199 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
200 int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
201
202 /* bio.c */
203 void fscrypt_decrypt_bio(struct bio *bio);
204 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
205                           sector_t pblk, unsigned int len);
206
207 /* hooks.c */
208 int fscrypt_file_open(struct inode *inode, struct file *filp);
209 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
210                            struct dentry *dentry);
211 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
212                              struct inode *new_dir, struct dentry *new_dentry,
213                              unsigned int flags);
214 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
215                              struct fscrypt_name *fname);
216 int fscrypt_prepare_setflags(struct inode *inode,
217                              unsigned int oldflags, unsigned int flags);
218 int fscrypt_prepare_symlink(struct inode *dir, const char *target,
219                             unsigned int len, unsigned int max_len,
220                             struct fscrypt_str *disk_link);
221 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
222                               unsigned int len, struct fscrypt_str *disk_link);
223 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
224                                 unsigned int max_size,
225                                 struct delayed_call *done);
226 static inline void fscrypt_set_ops(struct super_block *sb,
227                                    const struct fscrypt_operations *s_cop)
228 {
229         sb->s_cop = s_cop;
230 }
231 #else  /* !CONFIG_FS_ENCRYPTION */
232
233 static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
234 {
235         return NULL;
236 }
237
238 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
239 {
240         return false;
241 }
242
243 static inline void fscrypt_handle_d_move(struct dentry *dentry)
244 {
245 }
246
247 /* crypto.c */
248 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
249 {
250 }
251
252 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
253                                                             unsigned int len,
254                                                             unsigned int offs,
255                                                             gfp_t gfp_flags)
256 {
257         return ERR_PTR(-EOPNOTSUPP);
258 }
259
260 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
261                                                 struct page *page,
262                                                 unsigned int len,
263                                                 unsigned int offs, u64 lblk_num,
264                                                 gfp_t gfp_flags)
265 {
266         return -EOPNOTSUPP;
267 }
268
269 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
270                                                    unsigned int len,
271                                                    unsigned int offs)
272 {
273         return -EOPNOTSUPP;
274 }
275
276 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
277                                                 struct page *page,
278                                                 unsigned int len,
279                                                 unsigned int offs, u64 lblk_num)
280 {
281         return -EOPNOTSUPP;
282 }
283
284 static inline bool fscrypt_is_bounce_page(struct page *page)
285 {
286         return false;
287 }
288
289 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
290 {
291         WARN_ON_ONCE(1);
292         return ERR_PTR(-EINVAL);
293 }
294
295 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
296 {
297 }
298
299 /* policy.c */
300 static inline int fscrypt_ioctl_set_policy(struct file *filp,
301                                            const void __user *arg)
302 {
303         return -EOPNOTSUPP;
304 }
305
306 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
307 {
308         return -EOPNOTSUPP;
309 }
310
311 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
312                                               void __user *arg)
313 {
314         return -EOPNOTSUPP;
315 }
316
317 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
318 {
319         return -EOPNOTSUPP;
320 }
321
322 static inline int fscrypt_has_permitted_context(struct inode *parent,
323                                                 struct inode *child)
324 {
325         return 0;
326 }
327
328 static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
329 {
330         return -EOPNOTSUPP;
331 }
332
333 struct fscrypt_dummy_policy {
334 };
335
336 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
337                                                       char sep,
338                                                       struct super_block *sb)
339 {
340 }
341
342 static inline void
343 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
344 {
345 }
346
347 /* keyring.c */
348 static inline void fscrypt_sb_free(struct super_block *sb)
349 {
350 }
351
352 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
353 {
354         return -EOPNOTSUPP;
355 }
356
357 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
358 {
359         return -EOPNOTSUPP;
360 }
361
362 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
363                                                      void __user *arg)
364 {
365         return -EOPNOTSUPP;
366 }
367
368 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
369                                                void __user *arg)
370 {
371         return -EOPNOTSUPP;
372 }
373
374 /* keysetup.c */
375 static inline int fscrypt_get_encryption_info(struct inode *inode)
376 {
377         return -EOPNOTSUPP;
378 }
379
380 static inline int fscrypt_prepare_new_inode(struct inode *dir,
381                                             struct inode *inode,
382                                             bool *encrypt_ret)
383 {
384         if (IS_ENCRYPTED(dir))
385                 return -EOPNOTSUPP;
386         return 0;
387 }
388
389 static inline void fscrypt_put_encryption_info(struct inode *inode)
390 {
391         return;
392 }
393
394 static inline void fscrypt_free_inode(struct inode *inode)
395 {
396 }
397
398 static inline int fscrypt_drop_inode(struct inode *inode)
399 {
400         return 0;
401 }
402
403  /* fname.c */
404 static inline int fscrypt_setup_filename(struct inode *dir,
405                                          const struct qstr *iname,
406                                          int lookup, struct fscrypt_name *fname)
407 {
408         if (IS_ENCRYPTED(dir))
409                 return -EOPNOTSUPP;
410
411         memset(fname, 0, sizeof(*fname));
412         fname->usr_fname = iname;
413         fname->disk_name.name = (unsigned char *)iname->name;
414         fname->disk_name.len = iname->len;
415         return 0;
416 }
417
418 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
419 {
420         return;
421 }
422
423 static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
424                                              struct fscrypt_str *crypto_str)
425 {
426         return -EOPNOTSUPP;
427 }
428
429 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
430 {
431         return;
432 }
433
434 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
435                                             u32 hash, u32 minor_hash,
436                                             const struct fscrypt_str *iname,
437                                             struct fscrypt_str *oname)
438 {
439         return -EOPNOTSUPP;
440 }
441
442 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
443                                       const u8 *de_name, u32 de_name_len)
444 {
445         /* Encryption support disabled; use standard comparison */
446         if (de_name_len != fname->disk_name.len)
447                 return false;
448         return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
449 }
450
451 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
452                                         const struct qstr *name)
453 {
454         WARN_ON_ONCE(1);
455         return 0;
456 }
457
458 static inline int fscrypt_d_revalidate(struct dentry *dentry,
459                                        unsigned int flags)
460 {
461         return 1;
462 }
463
464 /* bio.c */
465 static inline void fscrypt_decrypt_bio(struct bio *bio)
466 {
467 }
468
469 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
470                                         sector_t pblk, unsigned int len)
471 {
472         return -EOPNOTSUPP;
473 }
474
475 /* hooks.c */
476
477 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
478 {
479         if (IS_ENCRYPTED(inode))
480                 return -EOPNOTSUPP;
481         return 0;
482 }
483
484 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
485                                          struct dentry *dentry)
486 {
487         return -EOPNOTSUPP;
488 }
489
490 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
491                                            struct dentry *old_dentry,
492                                            struct inode *new_dir,
493                                            struct dentry *new_dentry,
494                                            unsigned int flags)
495 {
496         return -EOPNOTSUPP;
497 }
498
499 static inline int __fscrypt_prepare_lookup(struct inode *dir,
500                                            struct dentry *dentry,
501                                            struct fscrypt_name *fname)
502 {
503         return -EOPNOTSUPP;
504 }
505
506 static inline int fscrypt_prepare_setflags(struct inode *inode,
507                                            unsigned int oldflags,
508                                            unsigned int flags)
509 {
510         return 0;
511 }
512
513 static inline int fscrypt_prepare_symlink(struct inode *dir,
514                                           const char *target,
515                                           unsigned int len,
516                                           unsigned int max_len,
517                                           struct fscrypt_str *disk_link)
518 {
519         if (IS_ENCRYPTED(dir))
520                 return -EOPNOTSUPP;
521         disk_link->name = (unsigned char *)target;
522         disk_link->len = len + 1;
523         if (disk_link->len > max_len)
524                 return -ENAMETOOLONG;
525         return 0;
526 }
527
528 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
529                                             const char *target,
530                                             unsigned int len,
531                                             struct fscrypt_str *disk_link)
532 {
533         return -EOPNOTSUPP;
534 }
535
536 static inline const char *fscrypt_get_symlink(struct inode *inode,
537                                               const void *caddr,
538                                               unsigned int max_size,
539                                               struct delayed_call *done)
540 {
541         return ERR_PTR(-EOPNOTSUPP);
542 }
543
544 static inline void fscrypt_set_ops(struct super_block *sb,
545                                    const struct fscrypt_operations *s_cop)
546 {
547 }
548
549 #endif  /* !CONFIG_FS_ENCRYPTION */
550
551 /* inline_crypt.c */
552 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
553
554 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
555
556 void fscrypt_set_bio_crypt_ctx(struct bio *bio,
557                                const struct inode *inode, u64 first_lblk,
558                                gfp_t gfp_mask);
559
560 void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
561                                   const struct buffer_head *first_bh,
562                                   gfp_t gfp_mask);
563
564 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
565                            u64 next_lblk);
566
567 bool fscrypt_mergeable_bio_bh(struct bio *bio,
568                               const struct buffer_head *next_bh);
569
570 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
571
572 static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
573 {
574         return false;
575 }
576
577 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
578                                              const struct inode *inode,
579                                              u64 first_lblk, gfp_t gfp_mask) { }
580
581 static inline void fscrypt_set_bio_crypt_ctx_bh(
582                                          struct bio *bio,
583                                          const struct buffer_head *first_bh,
584                                          gfp_t gfp_mask) { }
585
586 static inline bool fscrypt_mergeable_bio(struct bio *bio,
587                                          const struct inode *inode,
588                                          u64 next_lblk)
589 {
590         return true;
591 }
592
593 static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
594                                             const struct buffer_head *next_bh)
595 {
596         return true;
597 }
598 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
599
600 /**
601  * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
602  *                                      encryption
603  * @inode: an inode. If encrypted, its key must be set up.
604  *
605  * Return: true if the inode requires file contents encryption and if the
606  *         encryption should be done in the block layer via blk-crypto rather
607  *         than in the filesystem layer.
608  */
609 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
610 {
611         return fscrypt_needs_contents_encryption(inode) &&
612                __fscrypt_inode_uses_inline_crypto(inode);
613 }
614
615 /**
616  * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
617  *                                        encryption
618  * @inode: an inode. If encrypted, its key must be set up.
619  *
620  * Return: true if the inode requires file contents encryption and if the
621  *         encryption should be done in the filesystem layer rather than in the
622  *         block layer via blk-crypto.
623  */
624 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
625 {
626         return fscrypt_needs_contents_encryption(inode) &&
627                !__fscrypt_inode_uses_inline_crypto(inode);
628 }
629
630 /**
631  * fscrypt_has_encryption_key() - check whether an inode has had its key set up
632  * @inode: the inode to check
633  *
634  * Return: %true if the inode has had its encryption key set up, else %false.
635  *
636  * Usually this should be preceded by fscrypt_get_encryption_info() to try to
637  * set up the key first.
638  */
639 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
640 {
641         return fscrypt_get_info(inode) != NULL;
642 }
643
644 /**
645  * fscrypt_require_key() - require an inode's encryption key
646  * @inode: the inode we need the key for
647  *
648  * If the inode is encrypted, set up its encryption key if not already done.
649  * Then require that the key be present and return -ENOKEY otherwise.
650  *
651  * No locks are needed, and the key will live as long as the struct inode --- so
652  * it won't go away from under you.
653  *
654  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
655  * if a problem occurred while setting up the encryption key.
656  */
657 static inline int fscrypt_require_key(struct inode *inode)
658 {
659         if (IS_ENCRYPTED(inode)) {
660                 int err = fscrypt_get_encryption_info(inode);
661
662                 if (err)
663                         return err;
664                 if (!fscrypt_has_encryption_key(inode))
665                         return -ENOKEY;
666         }
667         return 0;
668 }
669
670 /**
671  * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
672  *                          directory
673  * @old_dentry: an existing dentry for the inode being linked
674  * @dir: the target directory
675  * @dentry: negative dentry for the target filename
676  *
677  * A new link can only be added to an encrypted directory if the directory's
678  * encryption key is available --- since otherwise we'd have no way to encrypt
679  * the filename.  Therefore, we first set up the directory's encryption key (if
680  * not already done) and return an error if it's unavailable.
681  *
682  * We also verify that the link will not violate the constraint that all files
683  * in an encrypted directory tree use the same encryption policy.
684  *
685  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
686  * -EXDEV if the link would result in an inconsistent encryption policy, or
687  * another -errno code.
688  */
689 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
690                                        struct inode *dir,
691                                        struct dentry *dentry)
692 {
693         if (IS_ENCRYPTED(dir))
694                 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
695         return 0;
696 }
697
698 /**
699  * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
700  *                            directories
701  * @old_dir: source directory
702  * @old_dentry: dentry for source file
703  * @new_dir: target directory
704  * @new_dentry: dentry for target location (may be negative unless exchanging)
705  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
706  *
707  * Prepare for ->rename() where the source and/or target directories may be
708  * encrypted.  A new link can only be added to an encrypted directory if the
709  * directory's encryption key is available --- since otherwise we'd have no way
710  * to encrypt the filename.  A rename to an existing name, on the other hand,
711  * *is* cryptographically possible without the key.  However, we take the more
712  * conservative approach and just forbid all no-key renames.
713  *
714  * We also verify that the rename will not violate the constraint that all files
715  * in an encrypted directory tree use the same encryption policy.
716  *
717  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
718  * rename would cause inconsistent encryption policies, or another -errno code.
719  */
720 static inline int fscrypt_prepare_rename(struct inode *old_dir,
721                                          struct dentry *old_dentry,
722                                          struct inode *new_dir,
723                                          struct dentry *new_dentry,
724                                          unsigned int flags)
725 {
726         if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
727                 return __fscrypt_prepare_rename(old_dir, old_dentry,
728                                                 new_dir, new_dentry, flags);
729         return 0;
730 }
731
732 /**
733  * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
734  *                            directory
735  * @dir: directory being searched
736  * @dentry: filename being looked up
737  * @fname: (output) the name to use to search the on-disk directory
738  *
739  * Prepare for ->lookup() in a directory which may be encrypted by determining
740  * the name that will actually be used to search the directory on-disk.  If the
741  * directory's encryption key is available, then the lookup is assumed to be by
742  * plaintext name; otherwise, it is assumed to be by no-key name.
743  *
744  * This also installs a custom ->d_revalidate() method which will invalidate the
745  * dentry if it was created without the key and the key is later added.
746  *
747  * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
748  * filename isn't a valid no-key name, so a negative dentry should be created;
749  * or another -errno code.
750  */
751 static inline int fscrypt_prepare_lookup(struct inode *dir,
752                                          struct dentry *dentry,
753                                          struct fscrypt_name *fname)
754 {
755         if (IS_ENCRYPTED(dir))
756                 return __fscrypt_prepare_lookup(dir, dentry, fname);
757
758         memset(fname, 0, sizeof(*fname));
759         fname->usr_fname = &dentry->d_name;
760         fname->disk_name.name = (unsigned char *)dentry->d_name.name;
761         fname->disk_name.len = dentry->d_name.len;
762         return 0;
763 }
764
765 /**
766  * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
767  *                             attributes
768  * @dentry: dentry through which the inode is being changed
769  * @attr: attributes to change
770  *
771  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
772  * most attribute changes are allowed even without the encryption key.  However,
773  * without the encryption key we do have to forbid truncates.  This is needed
774  * because the size being truncated to may not be a multiple of the filesystem
775  * block size, and in that case we'd have to decrypt the final block, zero the
776  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
777  * filesystem block boundary, but it's simpler to just forbid all truncates ---
778  * and we already forbid all other contents modifications without the key.)
779  *
780  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
781  * if a problem occurred while setting up the encryption key.
782  */
783 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
784                                           struct iattr *attr)
785 {
786         if (attr->ia_valid & ATTR_SIZE)
787                 return fscrypt_require_key(d_inode(dentry));
788         return 0;
789 }
790
791 /**
792  * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
793  * @inode: symlink inode
794  * @target: plaintext symlink target
795  * @len: length of @target excluding null terminator
796  * @disk_link: (in/out) the on-disk symlink target being prepared
797  *
798  * If the symlink target needs to be encrypted, then this function encrypts it
799  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
800  * previously to compute @disk_link->len.  If the filesystem did not allocate a
801  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
802  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
803  *
804  * Return: 0 on success, -errno on failure
805  */
806 static inline int fscrypt_encrypt_symlink(struct inode *inode,
807                                           const char *target,
808                                           unsigned int len,
809                                           struct fscrypt_str *disk_link)
810 {
811         if (IS_ENCRYPTED(inode))
812                 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
813         return 0;
814 }
815
816 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
817 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
818 {
819         struct page *page = *pagep;
820
821         if (fscrypt_is_bounce_page(page)) {
822                 *pagep = fscrypt_pagecache_page(page);
823                 fscrypt_free_bounce_page(page);
824         }
825 }
826
827 #endif  /* _LINUX_FSCRYPT_H */