fscrypt: move fscrypt_d_revalidate() to fname.c
[linux-block.git] / include / linux / fscrypt.h
CommitLineData
32190f0a 1/* SPDX-License-Identifier: GPL-2.0 */
46f47e48 2/*
734f0d24
DC
3 * fscrypt.h: declarations for per-file encryption
4 *
643fa961
CR
5 * Filesystems that implement per-file encryption must include this header
6 * file.
46f47e48
EB
7 *
8 * Copyright (C) 2015, Google, Inc.
9 *
10 * Written by Michael Halcrow, 2015.
11 * Modified by Jaegeuk Kim, 2015.
12 */
734f0d24
DC
13#ifndef _LINUX_FSCRYPT_H
14#define _LINUX_FSCRYPT_H
46f47e48 15
46f47e48 16#include <linux/fs.h>
643fa961
CR
17#include <linux/mm.h>
18#include <linux/slab.h>
7af0ab0d 19#include <uapi/linux/fscrypt.h>
46f47e48
EB
20
21#define FS_CRYPTO_BLOCK_SIZE 16
22
23struct fscrypt_info;
24
46f47e48
EB
25struct fscrypt_str {
26 unsigned char *name;
27 u32 len;
28};
29
30struct 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;
b01531db 36 bool is_ciphertext_name;
46f47e48
EB
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
af65207c 44/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
5dae460c 45#define FSCRYPT_SET_CONTEXT_MAX_SIZE 40
af65207c 46
643fa961
CR
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 */
56struct 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;
b103fb76
EB
64 bool (*has_stable_inodes)(struct super_block *sb);
65 void (*get_ino_and_lblk_bits)(struct super_block *sb,
66 int *ino_bits_ret, int *lblk_bits_ret);
643fa961
CR
67};
68
643fa961
CR
69static inline bool fscrypt_has_encryption_key(const struct inode *inode)
70{
e37a784d
EB
71 /* pairs with cmpxchg_release() in fscrypt_get_encryption_info() */
72 return READ_ONCE(inode->i_crypt_info) != NULL;
643fa961
CR
73}
74
75static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
76{
77 return inode->i_sb->s_cop->dummy_context &&
78 inode->i_sb->s_cop->dummy_context(inode);
79}
80
0bf3d5c1
EB
81/*
82 * When d_splice_alias() moves a directory's encrypted alias to its decrypted
83 * alias as a result of the encryption key being added, DCACHE_ENCRYPTED_NAME
84 * must be cleared. Note that we don't have to support arbitrary moves of this
85 * flag because fscrypt doesn't allow encrypted aliases to be the source or
86 * target of a rename().
87 */
88static inline void fscrypt_handle_d_move(struct dentry *dentry)
89{
90 dentry->d_flags &= ~DCACHE_ENCRYPTED_NAME;
91}
92
643fa961
CR
93/* crypto.c */
94extern void fscrypt_enqueue_decrypt_work(struct work_struct *);
53bc1d85
EB
95
96extern struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
97 unsigned int len,
98 unsigned int offs,
99 gfp_t gfp_flags);
03569f2f
EB
100extern int fscrypt_encrypt_block_inplace(const struct inode *inode,
101 struct page *page, unsigned int len,
102 unsigned int offs, u64 lblk_num,
103 gfp_t gfp_flags);
aa8bc1ac
EB
104
105extern int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
106 unsigned int offs);
41adbcb7
EB
107extern int fscrypt_decrypt_block_inplace(const struct inode *inode,
108 struct page *page, unsigned int len,
109 unsigned int offs, u64 lblk_num);
643fa961 110
d2d0727b 111static inline bool fscrypt_is_bounce_page(struct page *page)
643fa961 112{
d2d0727b 113 return page->mapping == NULL;
643fa961
CR
114}
115
d2d0727b
EB
116static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
117{
118 return (struct page *)page_private(bounce_page);
119}
120
121extern void fscrypt_free_bounce_page(struct page *bounce_page);
643fa961
CR
122
123/* policy.c */
124extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
125extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
5dae460c 126extern int fscrypt_ioctl_get_policy_ex(struct file *, void __user *);
643fa961
CR
127extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
128extern int fscrypt_inherit_context(struct inode *, struct inode *,
129 void *, bool);
22d94f49
EB
130/* keyring.c */
131extern void fscrypt_sb_free(struct super_block *sb);
132extern int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
b1c0ec35 133extern int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
78a1b96b
EB
134extern int fscrypt_ioctl_remove_key_all_users(struct file *filp,
135 void __user *arg);
5a7e2992 136extern int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
22d94f49 137
feed8258 138/* keysetup.c */
643fa961
CR
139extern int fscrypt_get_encryption_info(struct inode *);
140extern void fscrypt_put_encryption_info(struct inode *);
2c58d548 141extern void fscrypt_free_inode(struct inode *);
b1c0ec35 142extern int fscrypt_drop_inode(struct inode *inode);
643fa961
CR
143
144/* fname.c */
145extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
146 int lookup, struct fscrypt_name *);
147
148static inline void fscrypt_free_filename(struct fscrypt_name *fname)
149{
150 kfree(fname->crypto_buf.name);
151}
152
153extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
154 struct fscrypt_str *);
155extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
8a4ab0b8
EB
156extern int fscrypt_fname_disk_to_usr(const struct inode *inode,
157 u32 hash, u32 minor_hash,
158 const struct fscrypt_str *iname,
159 struct fscrypt_str *oname);
643fa961
CR
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 */
196struct 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 */
216static 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 */
236extern void fscrypt_decrypt_bio(struct bio *);
643fa961
CR
237extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
238 unsigned int);
239
240/* hooks.c */
241extern int fscrypt_file_open(struct inode *inode, struct file *filp);
968dd6d0
EB
242extern int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
243 struct dentry *dentry);
643fa961
CR
244extern int __fscrypt_prepare_rename(struct inode *old_dir,
245 struct dentry *old_dentry,
246 struct inode *new_dir,
247 struct dentry *new_dentry,
248 unsigned int flags);
b01531db
EB
249extern int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
250 struct fscrypt_name *fname);
643fa961
CR
251extern int __fscrypt_prepare_symlink(struct inode *dir, unsigned int len,
252 unsigned int max_len,
253 struct fscrypt_str *disk_link);
254extern int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
255 unsigned int len,
256 struct fscrypt_str *disk_link);
257extern const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
258 unsigned int max_size,
259 struct delayed_call *done);
eea2c05d
SH
260static inline void fscrypt_set_ops(struct super_block *sb,
261 const struct fscrypt_operations *s_cop)
262{
263 sb->s_cop = s_cop;
264}
643fa961
CR
265#else /* !CONFIG_FS_ENCRYPTION */
266
267static inline bool fscrypt_has_encryption_key(const struct inode *inode)
268{
269 return false;
270}
271
272static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
273{
274 return false;
275}
276
0bf3d5c1
EB
277static inline void fscrypt_handle_d_move(struct dentry *dentry)
278{
279}
280
643fa961
CR
281/* crypto.c */
282static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
283{
284}
285
53bc1d85
EB
286static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
287 unsigned int len,
288 unsigned int offs,
289 gfp_t gfp_flags)
643fa961
CR
290{
291 return ERR_PTR(-EOPNOTSUPP);
292}
293
03569f2f
EB
294static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
295 struct page *page,
296 unsigned int len,
297 unsigned int offs, u64 lblk_num,
298 gfp_t gfp_flags)
299{
300 return -EOPNOTSUPP;
301}
302
aa8bc1ac
EB
303static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
304 unsigned int len,
305 unsigned int offs)
643fa961
CR
306{
307 return -EOPNOTSUPP;
308}
309
41adbcb7
EB
310static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
311 struct page *page,
312 unsigned int len,
313 unsigned int offs, u64 lblk_num)
314{
315 return -EOPNOTSUPP;
316}
317
d2d0727b
EB
318static inline bool fscrypt_is_bounce_page(struct page *page)
319{
320 return false;
321}
322
323static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
643fa961
CR
324{
325 WARN_ON_ONCE(1);
326 return ERR_PTR(-EINVAL);
327}
328
d2d0727b 329static inline void fscrypt_free_bounce_page(struct page *bounce_page)
643fa961 330{
643fa961
CR
331}
332
333/* policy.c */
334static inline int fscrypt_ioctl_set_policy(struct file *filp,
335 const void __user *arg)
336{
337 return -EOPNOTSUPP;
338}
339
340static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
341{
342 return -EOPNOTSUPP;
343}
344
5dae460c
EB
345static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
346 void __user *arg)
347{
348 return -EOPNOTSUPP;
349}
350
643fa961
CR
351static inline int fscrypt_has_permitted_context(struct inode *parent,
352 struct inode *child)
353{
354 return 0;
355}
356
357static inline int fscrypt_inherit_context(struct inode *parent,
358 struct inode *child,
359 void *fs_data, bool preload)
360{
361 return -EOPNOTSUPP;
362}
363
22d94f49
EB
364/* keyring.c */
365static inline void fscrypt_sb_free(struct super_block *sb)
366{
367}
368
369static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
370{
371 return -EOPNOTSUPP;
372}
373
b1c0ec35 374static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
78a1b96b
EB
375{
376 return -EOPNOTSUPP;
377}
378
379static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
380 void __user *arg)
b1c0ec35
EB
381{
382 return -EOPNOTSUPP;
383}
384
5a7e2992
EB
385static inline int fscrypt_ioctl_get_key_status(struct file *filp,
386 void __user *arg)
387{
388 return -EOPNOTSUPP;
389}
390
feed8258 391/* keysetup.c */
643fa961
CR
392static inline int fscrypt_get_encryption_info(struct inode *inode)
393{
394 return -EOPNOTSUPP;
395}
396
397static inline void fscrypt_put_encryption_info(struct inode *inode)
398{
399 return;
400}
401
2c58d548
EB
402static inline void fscrypt_free_inode(struct inode *inode)
403{
404}
405
b1c0ec35
EB
406static inline int fscrypt_drop_inode(struct inode *inode)
407{
408 return 0;
409}
410
643fa961
CR
411 /* fname.c */
412static inline int fscrypt_setup_filename(struct inode *dir,
413 const struct qstr *iname,
414 int lookup, struct fscrypt_name *fname)
415{
416 if (IS_ENCRYPTED(dir))
417 return -EOPNOTSUPP;
418
b01531db 419 memset(fname, 0, sizeof(*fname));
643fa961
CR
420 fname->usr_fname = iname;
421 fname->disk_name.name = (unsigned char *)iname->name;
422 fname->disk_name.len = iname->len;
423 return 0;
424}
425
426static inline void fscrypt_free_filename(struct fscrypt_name *fname)
427{
428 return;
429}
430
431static inline int fscrypt_fname_alloc_buffer(const struct inode *inode,
432 u32 max_encrypted_len,
433 struct fscrypt_str *crypto_str)
434{
435 return -EOPNOTSUPP;
436}
437
438static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
439{
440 return;
441}
442
8a4ab0b8 443static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
643fa961
CR
444 u32 hash, u32 minor_hash,
445 const struct fscrypt_str *iname,
446 struct fscrypt_str *oname)
447{
448 return -EOPNOTSUPP;
449}
450
451static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
452 const u8 *de_name, u32 de_name_len)
453{
454 /* Encryption support disabled; use standard comparison */
455 if (de_name_len != fname->disk_name.len)
456 return false;
457 return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
458}
459
460/* bio.c */
461static inline void fscrypt_decrypt_bio(struct bio *bio)
462{
463}
464
643fa961
CR
465static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
466 sector_t pblk, unsigned int len)
467{
468 return -EOPNOTSUPP;
469}
470
471/* hooks.c */
472
473static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
474{
475 if (IS_ENCRYPTED(inode))
476 return -EOPNOTSUPP;
477 return 0;
478}
479
968dd6d0
EB
480static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
481 struct dentry *dentry)
643fa961
CR
482{
483 return -EOPNOTSUPP;
484}
485
486static inline int __fscrypt_prepare_rename(struct inode *old_dir,
487 struct dentry *old_dentry,
488 struct inode *new_dir,
489 struct dentry *new_dentry,
490 unsigned int flags)
491{
492 return -EOPNOTSUPP;
493}
494
495static inline int __fscrypt_prepare_lookup(struct inode *dir,
b01531db
EB
496 struct dentry *dentry,
497 struct fscrypt_name *fname)
643fa961
CR
498{
499 return -EOPNOTSUPP;
500}
501
502static inline int __fscrypt_prepare_symlink(struct inode *dir,
503 unsigned int len,
504 unsigned int max_len,
505 struct fscrypt_str *disk_link)
506{
507 return -EOPNOTSUPP;
508}
509
510
511static inline int __fscrypt_encrypt_symlink(struct inode *inode,
512 const char *target,
513 unsigned int len,
514 struct fscrypt_str *disk_link)
515{
516 return -EOPNOTSUPP;
517}
518
519static inline const char *fscrypt_get_symlink(struct inode *inode,
520 const void *caddr,
521 unsigned int max_size,
522 struct delayed_call *done)
523{
524 return ERR_PTR(-EOPNOTSUPP);
525}
eea2c05d
SH
526
527static inline void fscrypt_set_ops(struct super_block *sb,
528 const struct fscrypt_operations *s_cop)
529{
530}
531
643fa961 532#endif /* !CONFIG_FS_ENCRYPTION */
734f0d24 533
d293c3e4
EB
534/**
535 * fscrypt_require_key - require an inode's encryption key
536 * @inode: the inode we need the key for
537 *
538 * If the inode is encrypted, set up its encryption key if not already done.
539 * Then require that the key be present and return -ENOKEY otherwise.
540 *
541 * No locks are needed, and the key will live as long as the struct inode --- so
542 * it won't go away from under you.
543 *
544 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
545 * if a problem occurred while setting up the encryption key.
546 */
547static inline int fscrypt_require_key(struct inode *inode)
548{
549 if (IS_ENCRYPTED(inode)) {
550 int err = fscrypt_get_encryption_info(inode);
551
552 if (err)
553 return err;
554 if (!fscrypt_has_encryption_key(inode))
555 return -ENOKEY;
556 }
557 return 0;
558}
734f0d24 559
0ea87a96
EB
560/**
561 * fscrypt_prepare_link - prepare to link an inode into a possibly-encrypted directory
562 * @old_dentry: an existing dentry for the inode being linked
563 * @dir: the target directory
564 * @dentry: negative dentry for the target filename
565 *
566 * A new link can only be added to an encrypted directory if the directory's
567 * encryption key is available --- since otherwise we'd have no way to encrypt
568 * the filename. Therefore, we first set up the directory's encryption key (if
569 * not already done) and return an error if it's unavailable.
570 *
571 * We also verify that the link will not violate the constraint that all files
572 * in an encrypted directory tree use the same encryption policy.
573 *
574 * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
f5e55e77 575 * -EXDEV if the link would result in an inconsistent encryption policy, or
0ea87a96
EB
576 * another -errno code.
577 */
578static inline int fscrypt_prepare_link(struct dentry *old_dentry,
579 struct inode *dir,
580 struct dentry *dentry)
581{
582 if (IS_ENCRYPTED(dir))
968dd6d0 583 return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
0ea87a96
EB
584 return 0;
585}
586
94b26f36
EB
587/**
588 * fscrypt_prepare_rename - prepare for a rename between possibly-encrypted directories
589 * @old_dir: source directory
590 * @old_dentry: dentry for source file
591 * @new_dir: target directory
592 * @new_dentry: dentry for target location (may be negative unless exchanging)
593 * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
594 *
595 * Prepare for ->rename() where the source and/or target directories may be
596 * encrypted. A new link can only be added to an encrypted directory if the
597 * directory's encryption key is available --- since otherwise we'd have no way
598 * to encrypt the filename. A rename to an existing name, on the other hand,
599 * *is* cryptographically possible without the key. However, we take the more
600 * conservative approach and just forbid all no-key renames.
601 *
602 * We also verify that the rename will not violate the constraint that all files
603 * in an encrypted directory tree use the same encryption policy.
604 *
f5e55e77 605 * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
94b26f36
EB
606 * rename would cause inconsistent encryption policies, or another -errno code.
607 */
608static inline int fscrypt_prepare_rename(struct inode *old_dir,
609 struct dentry *old_dentry,
610 struct inode *new_dir,
611 struct dentry *new_dentry,
612 unsigned int flags)
613{
614 if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
615 return __fscrypt_prepare_rename(old_dir, old_dentry,
616 new_dir, new_dentry, flags);
617 return 0;
618}
619
32c3cf02
EB
620/**
621 * fscrypt_prepare_lookup - prepare to lookup a name in a possibly-encrypted directory
622 * @dir: directory being searched
623 * @dentry: filename being looked up
b01531db 624 * @fname: (output) the name to use to search the on-disk directory
32c3cf02 625 *
b01531db
EB
626 * Prepare for ->lookup() in a directory which may be encrypted by determining
627 * the name that will actually be used to search the directory on-disk. Lookups
628 * can be done with or without the directory's encryption key; without the key,
32c3cf02
EB
629 * filenames are presented in encrypted form. Therefore, we'll try to set up
630 * the directory's encryption key, but even without it the lookup can continue.
631 *
6cc24868
EB
632 * This also installs a custom ->d_revalidate() method which will invalidate the
633 * dentry if it was created without the key and the key is later added.
32c3cf02 634 *
b01531db
EB
635 * Return: 0 on success; -ENOENT if key is unavailable but the filename isn't a
636 * correctly formed encoded ciphertext name, so a negative dentry should be
637 * created; or another -errno code.
32c3cf02
EB
638 */
639static inline int fscrypt_prepare_lookup(struct inode *dir,
640 struct dentry *dentry,
b01531db 641 struct fscrypt_name *fname)
32c3cf02
EB
642{
643 if (IS_ENCRYPTED(dir))
b01531db
EB
644 return __fscrypt_prepare_lookup(dir, dentry, fname);
645
646 memset(fname, 0, sizeof(*fname));
647 fname->usr_fname = &dentry->d_name;
648 fname->disk_name.name = (unsigned char *)dentry->d_name.name;
649 fname->disk_name.len = dentry->d_name.len;
32c3cf02
EB
650 return 0;
651}
652
815dac33
EB
653/**
654 * fscrypt_prepare_setattr - prepare to change a possibly-encrypted inode's attributes
655 * @dentry: dentry through which the inode is being changed
656 * @attr: attributes to change
657 *
658 * Prepare for ->setattr() on a possibly-encrypted inode. On an encrypted file,
659 * most attribute changes are allowed even without the encryption key. However,
660 * without the encryption key we do have to forbid truncates. This is needed
661 * because the size being truncated to may not be a multiple of the filesystem
662 * block size, and in that case we'd have to decrypt the final block, zero the
663 * portion past i_size, and re-encrypt it. (We *could* allow truncating to a
664 * filesystem block boundary, but it's simpler to just forbid all truncates ---
665 * and we already forbid all other contents modifications without the key.)
666 *
667 * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
668 * if a problem occurred while setting up the encryption key.
669 */
670static inline int fscrypt_prepare_setattr(struct dentry *dentry,
671 struct iattr *attr)
672{
673 if (attr->ia_valid & ATTR_SIZE)
674 return fscrypt_require_key(d_inode(dentry));
675 return 0;
676}
677
76e81d6d
EB
678/**
679 * fscrypt_prepare_symlink - prepare to create a possibly-encrypted symlink
680 * @dir: directory in which the symlink is being created
681 * @target: plaintext symlink target
682 * @len: length of @target excluding null terminator
683 * @max_len: space the filesystem has available to store the symlink target
684 * @disk_link: (out) the on-disk symlink target being prepared
685 *
686 * This function computes the size the symlink target will require on-disk,
687 * stores it in @disk_link->len, and validates it against @max_len. An
688 * encrypted symlink may be longer than the original.
689 *
690 * Additionally, @disk_link->name is set to @target if the symlink will be
691 * unencrypted, but left NULL if the symlink will be encrypted. For encrypted
692 * symlinks, the filesystem must call fscrypt_encrypt_symlink() to create the
693 * on-disk target later. (The reason for the two-step process is that some
694 * filesystems need to know the size of the symlink target before creating the
695 * inode, e.g. to determine whether it will be a "fast" or "slow" symlink.)
696 *
697 * Return: 0 on success, -ENAMETOOLONG if the symlink target is too long,
698 * -ENOKEY if the encryption key is missing, or another -errno code if a problem
699 * occurred while setting up the encryption key.
700 */
701static inline int fscrypt_prepare_symlink(struct inode *dir,
702 const char *target,
703 unsigned int len,
704 unsigned int max_len,
705 struct fscrypt_str *disk_link)
706{
707 if (IS_ENCRYPTED(dir) || fscrypt_dummy_context_enabled(dir))
708 return __fscrypt_prepare_symlink(dir, len, max_len, disk_link);
709
710 disk_link->name = (unsigned char *)target;
711 disk_link->len = len + 1;
712 if (disk_link->len > max_len)
713 return -ENAMETOOLONG;
714 return 0;
715}
716
717/**
718 * fscrypt_encrypt_symlink - encrypt the symlink target if needed
719 * @inode: symlink inode
720 * @target: plaintext symlink target
721 * @len: length of @target excluding null terminator
722 * @disk_link: (in/out) the on-disk symlink target being prepared
723 *
724 * If the symlink target needs to be encrypted, then this function encrypts it
725 * into @disk_link->name. fscrypt_prepare_symlink() must have been called
726 * previously to compute @disk_link->len. If the filesystem did not allocate a
727 * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
728 * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
729 *
730 * Return: 0 on success, -errno on failure
731 */
732static inline int fscrypt_encrypt_symlink(struct inode *inode,
733 const char *target,
734 unsigned int len,
735 struct fscrypt_str *disk_link)
736{
737 if (IS_ENCRYPTED(inode))
738 return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
739 return 0;
740}
741
d2d0727b
EB
742/* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
743static inline void fscrypt_finalize_bounce_page(struct page **pagep)
744{
745 struct page *page = *pagep;
746
747 if (fscrypt_is_bounce_page(page)) {
748 *pagep = fscrypt_pagecache_page(page);
749 fscrypt_free_bounce_page(page);
750 }
751}
752
734f0d24 753#endif /* _LINUX_FSCRYPT_H */