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