]> asedeno.scripts.mit.edu Git - linux.git/blob - include/linux/fscrypt.h
fscrypt: rename keyinfo.c to keysetup.c
[linux.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 struct fscrypt_ctx;
24 struct fscrypt_info;
25
26 struct fscrypt_str {
27         unsigned char *name;
28         u32 len;
29 };
30
31 struct fscrypt_name {
32         const struct qstr *usr_fname;
33         struct fscrypt_str disk_name;
34         u32 hash;
35         u32 minor_hash;
36         struct fscrypt_str crypto_buf;
37         bool is_ciphertext_name;
38 };
39
40 #define FSTR_INIT(n, l)         { .name = n, .len = l }
41 #define FSTR_TO_QSTR(f)         QSTR_INIT((f)->name, (f)->len)
42 #define fname_name(p)           ((p)->disk_name.name)
43 #define fname_len(p)            ((p)->disk_name.len)
44
45 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
46 #define FSCRYPT_SET_CONTEXT_MAX_SIZE    28
47
48 #ifdef CONFIG_FS_ENCRYPTION
49 /*
50  * fscrypt superblock flags
51  */
52 #define FS_CFLG_OWN_PAGES (1U << 1)
53
54 /*
55  * crypto operations for filesystems
56  */
57 struct fscrypt_operations {
58         unsigned int flags;
59         const char *key_prefix;
60         int (*get_context)(struct inode *, void *, size_t);
61         int (*set_context)(struct inode *, const void *, size_t, void *);
62         bool (*dummy_context)(struct inode *);
63         bool (*empty_dir)(struct inode *);
64         unsigned int max_namelen;
65 };
66
67 /* Decryption work */
68 struct fscrypt_ctx {
69         union {
70                 struct {
71                         struct bio *bio;
72                         struct work_struct work;
73                 };
74                 struct list_head free_list;     /* Free list */
75         };
76         u8 flags;                               /* Flags */
77 };
78
79 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
80 {
81         /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
82         return READ_ONCE(inode->i_crypt_info) != NULL;
83 }
84
85 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
86 {
87         return inode->i_sb->s_cop->dummy_context &&
88                 inode->i_sb->s_cop->dummy_context(inode);
89 }
90
91 /*
92  * When d_splice_alias() moves a directory's encrypted alias to its decrypted
93  * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
94  * must be cleared.  Note that we don't have to support arbitrary moves of this
95  * flag because fscrypt doesn't allow encrypted aliases to be the source or
96  * target of a rename().
97  */
98 static inline void fscrypt_handle_d_move(struct dentry *dentry)
99 {
100         dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
101 }
102
103 /* crypto.c */
104 extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
105 extern struct fscrypt_ctx *fscrypt_get_ctx(gfp_t);
106 extern void fscrypt_release_ctx(struct fscrypt_ctx *);
107
108 extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
109                                                      unsigned int len,
110                                                      unsigned int offs,
111                                                      gfp_t gfp_flags);
112 extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
113                                          struct page *page, unsigned int len,
114                                          unsigned int offs, u64 lblk_num,
115                                          gfp_t gfp_flags);
116
117 extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
118                                             unsigned int offs);
119 extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
120                                          struct page *page, unsigned int len,
121                                          unsigned int offs, u64 lblk_num);
122
123 static inline bool fscrypt_is_bounce_page(struct page *page)
124 {
125         return page->mapping == NULL;
126 }
127
128 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
129 {
130         return (struct page *)page_private(bounce_page);
131 }
132
133 extern void fscrypt_free_bounce_page(struct page *bounce_page);
134
135 /* policy.c */
136 extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
137 extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
138 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
139 extern int fscrypt_inherit_context(struct inode *, struct inode *,
140                                         void *, bool);
141 /* keysetup.c */
142 extern int fscrypt_get_encryption_info(struct inode *);
143 extern void fscrypt_put_encryption_info(struct inode *);
144 extern void fscrypt_free_inode(struct inode *);
145
146 /* fname.c */
147 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
148                                 int lookup, struct fscrypt_name *);
149
150 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
151 {
152         kfree(fname->crypto_buf.name);
153 }
154
155 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
156                                 struct fscrypt_str *);
157 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
158 extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
159                         const struct fscrypt_str *, struct fscrypt_str *);
160
161 #define FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE       32
162
163 /* Extracts the second-to-last ciphertext block; see explanation below */
164 #define FSCRYPT_FNAME_DIGEST(name, len) \
165         ((name) + round_down((len) - FS_CRYPTO_BLOCK_SIZE - 1, \
166                              FS_CRYPTO_BLOCK_SIZE))
167
168 #define FSCRYPT_FNAME_DIGEST_SIZE       FS_CRYPTO_BLOCK_SIZE
169
170 /**
171  * fscrypt_digested_name - alternate identifier for an on-disk filename
172  *
173  * When userspace lists an encrypted directory without access to the key,
174  * filenames whose ciphertext is longer than FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE
175  * bytes are shown in this abbreviated form (base64-encoded) rather than as the
176  * full ciphertext (base64-encoded).  This is necessary to allow supporting
177  * filenames up to NAME_MAX bytes, since base64 encoding expands the length.
178  *
179  * To make it possible for filesystems to still find the correct directory entry
180  * despite not knowing the full on-disk name, we encode any filesystem-specific
181  * 'hash' and/or 'minor_hash' which the filesystem may need for its lookups,
182  * followed by the second-to-last ciphertext block of the filename.  Due to the
183  * use of the CBC-CTS encryption mode, the second-to-last ciphertext block
184  * depends on the full plaintext.  (Note that ciphertext stealing causes the
185  * last two blocks to appear "flipped".)  This makes accidental collisions very
186  * unlikely: just a 1 in 2^128 chance for two filenames to collide even if they
187  * share the same filesystem-specific hashes.
188  *
189  * However, this scheme isn't immune to intentional collisions, which can be
190  * created by anyone able to create arbitrary plaintext filenames and view them
191  * without the key.  Making the "digest" be a real cryptographic hash like
192  * SHA-256 over the full ciphertext would prevent this, although it would be
193  * less efficient and harder to implement, especially since the filesystem would
194  * need to calculate it for each directory entry examined during a search.
195  */
196 struct fscrypt_digested_name {
197         u32 hash;
198         u32 minor_hash;
199         u8 digest[FSCRYPT_FNAME_DIGEST_SIZE];
200 };
201
202 /**
203  * fscrypt_match_name() - test whether the given name matches a directory entry
204  * @fname: the name being searched for
205  * @de_name: the name from the directory entry
206  * @de_name_len: the length of @de_name in bytes
207  *
208  * Normally @fname->disk_name will be set, and in that case we simply compare
209  * that to the name stored in the directory entry.  The only exception is that
210  * if we don't have the key for an encrypted directory and a filename in it is
211  * very long, then we won't have the full disk_name and we'll instead need to
212  * match against the fscrypt_digested_name.
213  *
214  * Return: %true if the name matches, otherwise %false.
215  */
216 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
217                                       const u8 *de_name, u32 de_name_len)
218 {
219         if (unlikely(!fname->disk_name.name)) {
220                 const struct fscrypt_digested_name *n =
221                         (const void *)fname->crypto_buf.name;
222                 if (WARN_ON_ONCE(fname->usr_fname->name[0] != '_'))
223                         return false;
224                 if (de_name_len <= FSCRYPT_FNAME_MAX_UNDIGESTED_SIZE)
225                         return false;
226                 return !memcmp(FSCRYPT_FNAME_DIGEST(de_name, de_name_len),
227                                n->digest, FSCRYPT_FNAME_DIGEST_SIZE);
228         }
229
230         if (de_name_len != fname->disk_name.len)
231                 return false;
232         return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
233 }
234
235 /* bio.c */
236 extern void fscrypt_decrypt_bio(struct bio *);
237 extern void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
238                                         struct bio *bio);
239 extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
240                                  unsigned int);
241
242 /* hooks.c */
243 extern int fscrypt_file_open(struct inode *inode, struct file *filp);
244 extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
245                                   struct dentry *dentry);
246 extern int __fscrypt_prepare_rename(struct inode *old_dir,
247                                     struct dentry *old_dentry,
248                                     struct inode *new_dir,
249                                     struct dentry *new_dentry,
250                                     unsigned int flags);
251 extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
252                                     struct fscrypt_name *fname);
253 extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
254                                      unsigned int max_len,
255                                      struct fscrypt_str *disk_link);
256 extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
257                                      unsigned int len,
258                                      struct fscrypt_str *disk_link);
259 extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
260                                        unsigned int max_size,
261                                        struct delayed_call *done);
262 static inline void fscrypt_set_ops(struct super_block *sb,
263                                    const struct fscrypt_operations *s_cop)
264 {
265         sb->s_cop = s_cop;
266 }
267 #else  /* !CONFIG_FS_ENCRYPTION */
268
269 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
270 {
271         return false;
272 }
273
274 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
275 {
276         return false;
277 }
278
279 static inline void fscrypt_handle_d_move(struct dentry *dentry)
280 {
281 }
282
283 /* crypto.c */
284 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
285 {
286 }
287
288 static inline struct fscrypt_ctx *fscrypt_get_ctx(gfp_t gfp_flags)
289 {
290         return ERR_PTR(-EOPNOTSUPP);
291 }
292
293 static inline void fscrypt_release_ctx(struct fscrypt_ctx *ctx)
294 {
295         return;
296 }
297
298 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
299                                                             unsigned int len,
300                                                             unsigned int offs,
301                                                             gfp_t gfp_flags)
302 {
303         return ERR_PTR(-EOPNOTSUPP);
304 }
305
306 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
307                                                 struct page *page,
308                                                 unsigned int len,
309                                                 unsigned int offs, u64 lblk_num,
310                                                 gfp_t gfp_flags)
311 {
312         return -EOPNOTSUPP;
313 }
314
315 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
316                                                    unsigned int len,
317                                                    unsigned int offs)
318 {
319         return -EOPNOTSUPP;
320 }
321
322 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
323                                                 struct page *page,
324                                                 unsigned int len,
325                                                 unsigned int offs, u64 lblk_num)
326 {
327         return -EOPNOTSUPP;
328 }
329
330 static inline bool fscrypt_is_bounce_page(struct page *page)
331 {
332         return false;
333 }
334
335 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
336 {
337         WARN_ON_ONCE(1);
338         return ERR_PTR(-EINVAL);
339 }
340
341 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
342 {
343 }
344
345 /* policy.c */
346 static inline int fscrypt_ioctl_set_policy(struct file *filp,
347                                            const void __user *arg)
348 {
349         return -EOPNOTSUPP;
350 }
351
352 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
353 {
354         return -EOPNOTSUPP;
355 }
356
357 static inline int fscrypt_has_permitted_context(struct inode *parent,
358                                                 struct inode *child)
359 {
360         return 0;
361 }
362
363 static inline int fscrypt_inherit_context(struct inode *parent,
364                                           struct inode *child,
365                                           void *fs_data, bool preload)
366 {
367         return -EOPNOTSUPP;
368 }
369
370 /* keysetup.c */
371 static inline int fscrypt_get_encryption_info(struct inode *inode)
372 {
373         return -EOPNOTSUPP;
374 }
375
376 static inline void fscrypt_put_encryption_info(struct inode *inode)
377 {
378         return;
379 }
380
381 static inline void fscrypt_free_inode(struct inode *inode)
382 {
383 }
384
385  /* fname.c */
386 static inline int fscrypt_setup_filename(struct inode *dir,
387                                          const struct qstr *iname,
388                                          int lookup, struct fscrypt_name *fname)
389 {
390         if (IS_ENCRYPTED(dir))
391                 return -EOPNOTSUPP;
392
393         memset(fname, 0, sizeof(*fname));
394         fname->usr_fname = iname;
395         fname->disk_name.name = (unsigned char *)iname->name;
396         fname->disk_name.len = iname->len;
397         return 0;
398 }
399
400 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
401 {
402         return;
403 }
404
405 static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
406                                              u32 max_encrypted_len,
407                                              struct fscrypt_str *crypto_str)
408 {
409         return -EOPNOTSUPP;
410 }
411
412 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
413 {
414         return;
415 }
416
417 static inline int fscrypt_fname_disk_to_usr(struct inode *inode,
418                                             u32 hash, u32 minor_hash,
419                                             const struct fscrypt_str *iname,
420                                             struct fscrypt_str *oname)
421 {
422         return -EOPNOTSUPP;
423 }
424
425 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
426                                       const u8 *de_name, u32 de_name_len)
427 {
428         /* Encryption support disabled; use standard comparison */
429         if (de_name_len != fname->disk_name.len)
430                 return false;
431         return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
432 }
433
434 /* bio.c */
435 static inline void fscrypt_decrypt_bio(struct bio *bio)
436 {
437 }
438
439 static inline void fscrypt_enqueue_decrypt_bio(struct fscrypt_ctx *ctx,
440                                                struct bio *bio)
441 {
442 }
443
444 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
445                                         sector_t pblk, unsigned int len)
446 {
447         return -EOPNOTSUPP;
448 }
449
450 /* hooks.c */
451
452 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
453 {
454         if (IS_ENCRYPTED(inode))
455                 return -EOPNOTSUPP;
456         return 0;
457 }
458
459 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
460                                          struct dentry *dentry)
461 {
462         return -EOPNOTSUPP;
463 }
464
465 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
466                                            struct dentry *old_dentry,
467                                            struct inode *new_dir,
468                                            struct dentry *new_dentry,
469                                            unsigned int flags)
470 {
471         return -EOPNOTSUPP;
472 }
473
474 static inline int __fscrypt_prepare_lookup(struct inode *dir,
475                                            struct dentry *dentry,
476                                            struct fscrypt_name *fname)
477 {
478         return -EOPNOTSUPP;
479 }
480
481 static inline int __fscrypt_prepare_symlink(struct inode *dir,
482                                             unsigned int len,
483                                             unsigned int max_len,
484                                             struct fscrypt_str *disk_link)
485 {
486         return -EOPNOTSUPP;
487 }
488
489
490 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
491                                             const char *target,
492                                             unsigned int len,
493                                             struct fscrypt_str *disk_link)
494 {
495         return -EOPNOTSUPP;
496 }
497
498 static inline const char *fscrypt_get_symlink(struct inode *inode,
499                                               const void *caddr,
500                                               unsigned int max_size,
501                                               struct delayed_call *done)
502 {
503         return ERR_PTR(-EOPNOTSUPP);
504 }
505
506 static inline void fscrypt_set_ops(struct super_block *sb,
507                                    const struct fscrypt_operations *s_cop)
508 {
509 }
510
511 #endif  /* !CONFIG_FS_ENCRYPTION */
512
513 /**
514  * fscrypt_require_key - require an inode's encryption key
515  * @inode: the inode we need the key for
516  *
517  * If the inode is encrypted, set up its encryption key if not already done.
518  * Then require that the key be present and return -ENOKEY otherwise.
519  *
520  * No locks are needed, and the key will live as long as the struct inode --- so
521  * it won't go away from under you.
522  *
523  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
524  * if a problem occurred while setting up the encryption key.
525  */
526 static inline int fscrypt_require_key(struct inode *inode)
527 {
528         if (IS_ENCRYPTED(inode)) {
529                 int err = fscrypt_get_encryption_info(inode);
530
531                 if (err)
532                         return err;
533                 if (!fscrypt_has_encryption_key(inode))
534                         return -ENOKEY;
535         }
536         return 0;
537 }
538
539 /**
540  * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
541  * @old_dentry: an existing dentry for the inode being linked
542  * @dir: the target directory
543  * @dentry: negative dentry for the target filename
544  *
545  * A new link can only be added to an encrypted directory if the directory's
546  * encryption key is available --- since otherwise we'd have no way to encrypt
547  * the filename.  Therefore, we first set up the directory's encryption key (if
548  * not already done) and return an error if it's unavailable.
549  *
550  * We also verify that the link will not violate the constraint that all files
551  * in an encrypted directory tree use the same encryption policy.
552  *
553  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
554  * -EXDEV if the link would result in an inconsistent encryption policy, or
555  * another -errno code.
556  */
557 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
558                                        struct inode *dir,
559                                        struct dentry *dentry)
560 {
561         if (IS_ENCRYPTED(dir))
562                 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
563         return 0;
564 }
565
566 /**
567  * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
568  * @old_dir: source directory
569  * @old_dentry: dentry for source file
570  * @new_dir: target directory
571  * @new_dentry: dentry for target location (may be negative unless exchanging)
572  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
573  *
574  * Prepare for ->rename() where the source and/or target directories may be
575  * encrypted.  A new link can only be added to an encrypted directory if the
576  * directory's encryption key is available --- since otherwise we'd have no way
577  * to encrypt the filename.  A rename to an existing name, on the other hand,
578  * *is* cryptographically possible without the key.  However, we take the more
579  * conservative approach and just forbid all no-key renames.
580  *
581  * We also verify that the rename will not violate the constraint that all files
582  * in an encrypted directory tree use the same encryption policy.
583  *
584  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
585  * rename would cause inconsistent encryption policies, or another -errno code.
586  */
587 static inline int fscrypt_prepare_rename(struct inode *old_dir,
588                                          struct dentry *old_dentry,
589                                          struct inode *new_dir,
590                                          struct dentry *new_dentry,
591                                          unsigned int flags)
592 {
593         if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
594                 return __fscrypt_prepare_rename(old_dir, old_dentry,
595                                                 new_dir, new_dentry, flags);
596         return 0;
597 }
598
599 /**
600  * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
601  * @dir: directory being searched
602  * @dentry: filename being looked up
603  * @fname: (output) the name to use to search the on-disk directory
604  *
605  * Prepare for ->lookup() in a directory which may be encrypted by determining
606  * the name that will actually be used to search the directory on-disk.  Lookups
607  * can be done with or without the directory's encryption key; without the key,
608  * filenames are presented in encrypted form.  Therefore, we'll try to set up
609  * the directory's encryption key, but even without it the lookup can continue.
610  *
611  * This also installs a custom ->d_revalidate() method which will invalidate the
612  * dentry if it was created without the key and the key is later added.
613  *
614  * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
615  * correctly formed encoded ciphertext name, so a negative dentry should be
616  * created; or another -errno code.
617  */
618 static inline int fscrypt_prepare_lookup(struct inode *dir,
619                                          struct dentry *dentry,
620                                          struct fscrypt_name *fname)
621 {
622         if (IS_ENCRYPTED(dir))
623                 return __fscrypt_prepare_lookup(dir, dentry, fname);
624
625         memset(fname, 0, sizeof(*fname));
626         fname->usr_fname = &dentry->d_name;
627         fname->disk_name.name = (unsigned char *)dentry->d_name.name;
628         fname->disk_name.len = dentry->d_name.len;
629         return 0;
630 }
631
632 /**
633  * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
634  * @dentry: dentry through which the inode is being changed
635  * @attr: attributes to change
636  *
637  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
638  * most attribute changes are allowed even without the encryption key.  However,
639  * without the encryption key we do have to forbid truncates.  This is needed
640  * because the size being truncated to may not be a multiple of the filesystem
641  * block size, and in that case we'd have to decrypt the final block, zero the
642  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
643  * filesystem block boundary, but it's simpler to just forbid all truncates ---
644  * and we already forbid all other contents modifications without the key.)
645  *
646  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
647  * if a problem occurred while setting up the encryption key.
648  */
649 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
650                                           struct iattr *attr)
651 {
652         if (attr->ia_valid & ATTR_SIZE)
653                 return fscrypt_require_key(d_inode(dentry));
654         return 0;
655 }
656
657 /**
658  * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
659  * @dir: directory in which the symlink is being created
660  * @target: plaintext symlink target
661  * @len: length of @target excluding null terminator
662  * @max_len: space the filesystem has available to store the symlink target
663  * @disk_link: (out) the on-disk symlink target being prepared
664  *
665  * This function computes the size the symlink target will require on-disk,
666  * stores it in @disk_link->len, and validates it against @max_len.  An
667  * encrypted symlink may be longer than the original.
668  *
669  * Additionally, @disk_link->name is set to @target if the symlink will be
670  * unencrypted, but left NULL if the symlink will be encrypted.  For encrypted
671  * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
672  * on-disk target later.  (The reason for the two-step process is that some
673  * filesystems need to know the size of the symlink target before creating the
674  * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
675  *
676  * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
677  * -ENOKEY if the encryption key is missing, or another -errno code if a problem
678  * occurred while setting up the encryption key.
679  */
680 static inline int fscrypt_prepare_symlink(struct inode *dir,
681                                           const char *target,
682                                           unsigned int len,
683                                           unsigned int max_len,
684                                           struct fscrypt_str *disk_link)
685 {
686         if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
687                 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
688
689         disk_link->name = (unsigned char *)target;
690         disk_link->len = len + 1;
691         if (disk_link->len > max_len)
692                 return -ENAMETOOLONG;
693         return 0;
694 }
695
696 /**
697  * fscrypt_encrypt_symlink - encrypt the symlink target if needed
698  * @inode: symlink inode
699  * @target: plaintext symlink target
700  * @len: length of @target excluding null terminator
701  * @disk_link: (in/out) the on-disk symlink target being prepared
702  *
703  * If the symlink target needs to be encrypted, then this function encrypts it
704  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
705  * previously to compute @disk_link->len.  If the filesystem did not allocate a
706  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
707  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
708  *
709  * Return: 0 on success, -errno on failure
710  */
711 static inline int fscrypt_encrypt_symlink(struct inode *inode,
712                                           const char *target,
713                                           unsigned int len,
714                                           struct fscrypt_str *disk_link)
715 {
716         if (IS_ENCRYPTED(inode))
717                 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
718         return 0;
719 }
720
721 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
722 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
723 {
724         struct page *page = *pagep;
725
726         if (fscrypt_is_bounce_page(page)) {
727                 *pagep = fscrypt_pagecache_page(page);
728                 fscrypt_free_bounce_page(page);
729         }
730 }
731
732 #endif  /* _LINUX_FSCRYPT_H */