Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 /*
0003  * fscrypt.h: declarations for per-file encryption
0004  *
0005  * Filesystems that implement per-file encryption must include this header
0006  * file.
0007  *
0008  * Copyright (C) 2015, Google, Inc.
0009  *
0010  * Written by Michael Halcrow, 2015.
0011  * Modified by Jaegeuk Kim, 2015.
0012  */
0013 #ifndef _LINUX_FSCRYPT_H
0014 #define _LINUX_FSCRYPT_H
0015 
0016 #include <linux/fs.h>
0017 #include <linux/mm.h>
0018 #include <linux/slab.h>
0019 #include <uapi/linux/fscrypt.h>
0020 
0021 /*
0022  * The lengths of all file contents blocks must be divisible by this value.
0023  * This is needed to ensure that all contents encryption modes will work, as
0024  * some of the supported modes don't support arbitrarily byte-aligned messages.
0025  *
0026  * Since the needed alignment is 16 bytes, most filesystems will meet this
0027  * requirement naturally, as typical block sizes are powers of 2.  However, if a
0028  * filesystem can generate arbitrarily byte-aligned block lengths (e.g., via
0029  * compression), then it will need to pad to this alignment before encryption.
0030  */
0031 #define FSCRYPT_CONTENTS_ALIGNMENT 16
0032 
0033 union fscrypt_policy;
0034 struct fscrypt_info;
0035 struct fs_parameter;
0036 struct seq_file;
0037 
0038 struct fscrypt_str {
0039     unsigned char *name;
0040     u32 len;
0041 };
0042 
0043 struct fscrypt_name {
0044     const struct qstr *usr_fname;
0045     struct fscrypt_str disk_name;
0046     u32 hash;
0047     u32 minor_hash;
0048     struct fscrypt_str crypto_buf;
0049     bool is_nokey_name;
0050 };
0051 
0052 #define FSTR_INIT(n, l)     { .name = n, .len = l }
0053 #define FSTR_TO_QSTR(f)     QSTR_INIT((f)->name, (f)->len)
0054 #define fname_name(p)       ((p)->disk_name.name)
0055 #define fname_len(p)        ((p)->disk_name.len)
0056 
0057 /* Maximum value for the third parameter of fscrypt_operations.set_context(). */
0058 #define FSCRYPT_SET_CONTEXT_MAX_SIZE    40
0059 
0060 #ifdef CONFIG_FS_ENCRYPTION
0061 
0062 /*
0063  * If set, the fscrypt bounce page pool won't be allocated (unless another
0064  * filesystem needs it).  Set this if the filesystem always uses its own bounce
0065  * pages for writes and therefore won't need the fscrypt bounce page pool.
0066  */
0067 #define FS_CFLG_OWN_PAGES (1U << 1)
0068 
0069 /* Crypto operations for filesystems */
0070 struct fscrypt_operations {
0071 
0072     /* Set of optional flags; see above for allowed flags */
0073     unsigned int flags;
0074 
0075     /*
0076      * If set, this is a filesystem-specific key description prefix that
0077      * will be accepted for "logon" keys for v1 fscrypt policies, in
0078      * addition to the generic prefix "fscrypt:".  This functionality is
0079      * deprecated, so new filesystems shouldn't set this field.
0080      */
0081     const char *key_prefix;
0082 
0083     /*
0084      * Get the fscrypt context of the given inode.
0085      *
0086      * @inode: the inode whose context to get
0087      * @ctx: the buffer into which to get the context
0088      * @len: length of the @ctx buffer in bytes
0089      *
0090      * Return: On success, returns the length of the context in bytes; this
0091      *     may be less than @len.  On failure, returns -ENODATA if the
0092      *     inode doesn't have a context, -ERANGE if the context is
0093      *     longer than @len, or another -errno code.
0094      */
0095     int (*get_context)(struct inode *inode, void *ctx, size_t len);
0096 
0097     /*
0098      * Set an fscrypt context on the given inode.
0099      *
0100      * @inode: the inode whose context to set.  The inode won't already have
0101      *     an fscrypt context.
0102      * @ctx: the context to set
0103      * @len: length of @ctx in bytes (at most FSCRYPT_SET_CONTEXT_MAX_SIZE)
0104      * @fs_data: If called from fscrypt_set_context(), this will be the
0105      *       value the filesystem passed to fscrypt_set_context().
0106      *       Otherwise (i.e. when called from
0107      *       FS_IOC_SET_ENCRYPTION_POLICY) this will be NULL.
0108      *
0109      * i_rwsem will be held for write.
0110      *
0111      * Return: 0 on success, -errno on failure.
0112      */
0113     int (*set_context)(struct inode *inode, const void *ctx, size_t len,
0114                void *fs_data);
0115 
0116     /*
0117      * Get the dummy fscrypt policy in use on the filesystem (if any).
0118      *
0119      * Filesystems only need to implement this function if they support the
0120      * test_dummy_encryption mount option.
0121      *
0122      * Return: A pointer to the dummy fscrypt policy, if the filesystem is
0123      *     mounted with test_dummy_encryption; otherwise NULL.
0124      */
0125     const union fscrypt_policy *(*get_dummy_policy)(struct super_block *sb);
0126 
0127     /*
0128      * Check whether a directory is empty.  i_rwsem will be held for write.
0129      */
0130     bool (*empty_dir)(struct inode *inode);
0131 
0132     /*
0133      * Check whether the filesystem's inode numbers and UUID are stable,
0134      * meaning that they will never be changed even by offline operations
0135      * such as filesystem shrinking and therefore can be used in the
0136      * encryption without the possibility of files becoming unreadable.
0137      *
0138      * Filesystems only need to implement this function if they want to
0139      * support the FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags.  These
0140      * flags are designed to work around the limitations of UFS and eMMC
0141      * inline crypto hardware, and they shouldn't be used in scenarios where
0142      * such hardware isn't being used.
0143      *
0144      * Leaving this NULL is equivalent to always returning false.
0145      */
0146     bool (*has_stable_inodes)(struct super_block *sb);
0147 
0148     /*
0149      * Get the number of bits that the filesystem uses to represent inode
0150      * numbers and file logical block numbers.
0151      *
0152      * By default, both of these are assumed to be 64-bit.  This function
0153      * can be implemented to declare that either or both of these numbers is
0154      * shorter, which may allow the use of the
0155      * FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{32,64} flags and/or the use of
0156      * inline crypto hardware whose maximum DUN length is less than 64 bits
0157      * (e.g., eMMC v5.2 spec compliant hardware).  This function only needs
0158      * to be implemented if support for one of these features is needed.
0159      */
0160     void (*get_ino_and_lblk_bits)(struct super_block *sb,
0161                       int *ino_bits_ret, int *lblk_bits_ret);
0162 
0163     /*
0164      * Return the number of block devices to which the filesystem may write
0165      * encrypted file contents.
0166      *
0167      * If the filesystem can use multiple block devices (other than block
0168      * devices that aren't used for encrypted file contents, such as
0169      * external journal devices), and wants to support inline encryption,
0170      * then it must implement this function.  Otherwise it's not needed.
0171      */
0172     int (*get_num_devices)(struct super_block *sb);
0173 
0174     /*
0175      * If ->get_num_devices() returns a value greater than 1, then this
0176      * function is called to get the array of request_queues that the
0177      * filesystem is using -- one per block device.  (There may be duplicate
0178      * entries in this array, as block devices can share a request_queue.)
0179      */
0180     void (*get_devices)(struct super_block *sb,
0181                 struct request_queue **devs);
0182 };
0183 
0184 static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
0185 {
0186     /*
0187      * Pairs with the cmpxchg_release() in fscrypt_setup_encryption_info().
0188      * I.e., another task may publish ->i_crypt_info concurrently, executing
0189      * a RELEASE barrier.  We need to use smp_load_acquire() here to safely
0190      * ACQUIRE the memory the other task published.
0191      */
0192     return smp_load_acquire(&inode->i_crypt_info);
0193 }
0194 
0195 /**
0196  * fscrypt_needs_contents_encryption() - check whether an inode needs
0197  *                   contents encryption
0198  * @inode: the inode to check
0199  *
0200  * Return: %true iff the inode is an encrypted regular file and the kernel was
0201  * built with fscrypt support.
0202  *
0203  * If you need to know whether the encrypt bit is set even when the kernel was
0204  * built without fscrypt support, you must use IS_ENCRYPTED() directly instead.
0205  */
0206 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
0207 {
0208     return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
0209 }
0210 
0211 /*
0212  * When d_splice_alias() moves a directory's no-key alias to its plaintext alias
0213  * as a result of the encryption key being added, DCACHE_NOKEY_NAME must be
0214  * cleared.  Note that we don't have to support arbitrary moves of this flag
0215  * because fscrypt doesn't allow no-key names to be the source or target of a
0216  * rename().
0217  */
0218 static inline void fscrypt_handle_d_move(struct dentry *dentry)
0219 {
0220     dentry->d_flags &= ~DCACHE_NOKEY_NAME;
0221 }
0222 
0223 /**
0224  * fscrypt_is_nokey_name() - test whether a dentry is a no-key name
0225  * @dentry: the dentry to check
0226  *
0227  * This returns true if the dentry is a no-key dentry.  A no-key dentry is a
0228  * dentry that was created in an encrypted directory that hasn't had its
0229  * encryption key added yet.  Such dentries may be either positive or negative.
0230  *
0231  * When a filesystem is asked to create a new filename in an encrypted directory
0232  * and the new filename's dentry is a no-key dentry, it must fail the operation
0233  * with ENOKEY.  This includes ->create(), ->mkdir(), ->mknod(), ->symlink(),
0234  * ->rename(), and ->link().  (However, ->rename() and ->link() are already
0235  * handled by fscrypt_prepare_rename() and fscrypt_prepare_link().)
0236  *
0237  * This is necessary because creating a filename requires the directory's
0238  * encryption key, but just checking for the key on the directory inode during
0239  * the final filesystem operation doesn't guarantee that the key was available
0240  * during the preceding dentry lookup.  And the key must have already been
0241  * available during the dentry lookup in order for it to have been checked
0242  * whether the filename already exists in the directory and for the new file's
0243  * dentry not to be invalidated due to it incorrectly having the no-key flag.
0244  *
0245  * Return: %true if the dentry is a no-key name
0246  */
0247 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
0248 {
0249     return dentry->d_flags & DCACHE_NOKEY_NAME;
0250 }
0251 
0252 /* crypto.c */
0253 void fscrypt_enqueue_decrypt_work(struct work_struct *);
0254 
0255 struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
0256                           unsigned int len,
0257                           unsigned int offs,
0258                           gfp_t gfp_flags);
0259 int fscrypt_encrypt_block_inplace(const struct inode *inode, struct page *page,
0260                   unsigned int len, unsigned int offs,
0261                   u64 lblk_num, gfp_t gfp_flags);
0262 
0263 int fscrypt_decrypt_pagecache_blocks(struct page *page, unsigned int len,
0264                      unsigned int offs);
0265 int fscrypt_decrypt_block_inplace(const struct inode *inode, struct page *page,
0266                   unsigned int len, unsigned int offs,
0267                   u64 lblk_num);
0268 
0269 static inline bool fscrypt_is_bounce_page(struct page *page)
0270 {
0271     return page->mapping == NULL;
0272 }
0273 
0274 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
0275 {
0276     return (struct page *)page_private(bounce_page);
0277 }
0278 
0279 void fscrypt_free_bounce_page(struct page *bounce_page);
0280 
0281 /* policy.c */
0282 int fscrypt_ioctl_set_policy(struct file *filp, const void __user *arg);
0283 int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg);
0284 int fscrypt_ioctl_get_policy_ex(struct file *filp, void __user *arg);
0285 int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg);
0286 int fscrypt_has_permitted_context(struct inode *parent, struct inode *child);
0287 int fscrypt_context_for_new_inode(void *ctx, struct inode *inode);
0288 int fscrypt_set_context(struct inode *inode, void *fs_data);
0289 
0290 struct fscrypt_dummy_policy {
0291     const union fscrypt_policy *policy;
0292 };
0293 
0294 int fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
0295                     struct fscrypt_dummy_policy *dummy_policy);
0296 bool fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
0297                   const struct fscrypt_dummy_policy *p2);
0298 int fscrypt_set_test_dummy_encryption(struct super_block *sb, const char *arg,
0299                 struct fscrypt_dummy_policy *dummy_policy);
0300 void fscrypt_show_test_dummy_encryption(struct seq_file *seq, char sep,
0301                     struct super_block *sb);
0302 static inline bool
0303 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
0304 {
0305     return dummy_policy->policy != NULL;
0306 }
0307 static inline void
0308 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
0309 {
0310     kfree(dummy_policy->policy);
0311     dummy_policy->policy = NULL;
0312 }
0313 
0314 /* keyring.c */
0315 void fscrypt_sb_free(struct super_block *sb);
0316 int fscrypt_ioctl_add_key(struct file *filp, void __user *arg);
0317 int fscrypt_add_test_dummy_key(struct super_block *sb,
0318                    const struct fscrypt_dummy_policy *dummy_policy);
0319 int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg);
0320 int fscrypt_ioctl_remove_key_all_users(struct file *filp, void __user *arg);
0321 int fscrypt_ioctl_get_key_status(struct file *filp, void __user *arg);
0322 
0323 /* keysetup.c */
0324 int fscrypt_prepare_new_inode(struct inode *dir, struct inode *inode,
0325                   bool *encrypt_ret);
0326 void fscrypt_put_encryption_info(struct inode *inode);
0327 void fscrypt_free_inode(struct inode *inode);
0328 int fscrypt_drop_inode(struct inode *inode);
0329 
0330 /* fname.c */
0331 int fscrypt_fname_encrypt(const struct inode *inode, const struct qstr *iname,
0332               u8 *out, unsigned int olen);
0333 bool fscrypt_fname_encrypted_size(const struct inode *inode, u32 orig_len,
0334                   u32 max_len, u32 *encrypted_len_ret);
0335 int fscrypt_setup_filename(struct inode *inode, const struct qstr *iname,
0336                int lookup, struct fscrypt_name *fname);
0337 
0338 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
0339 {
0340     kfree(fname->crypto_buf.name);
0341 }
0342 
0343 int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
0344                    struct fscrypt_str *crypto_str);
0345 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str);
0346 int fscrypt_fname_disk_to_usr(const struct inode *inode,
0347                   u32 hash, u32 minor_hash,
0348                   const struct fscrypt_str *iname,
0349                   struct fscrypt_str *oname);
0350 bool fscrypt_match_name(const struct fscrypt_name *fname,
0351             const u8 *de_name, u32 de_name_len);
0352 u64 fscrypt_fname_siphash(const struct inode *dir, const struct qstr *name);
0353 int fscrypt_d_revalidate(struct dentry *dentry, unsigned int flags);
0354 
0355 /* bio.c */
0356 void fscrypt_decrypt_bio(struct bio *bio);
0357 int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
0358               sector_t pblk, unsigned int len);
0359 
0360 /* hooks.c */
0361 int fscrypt_file_open(struct inode *inode, struct file *filp);
0362 int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
0363                struct dentry *dentry);
0364 int __fscrypt_prepare_rename(struct inode *old_dir, struct dentry *old_dentry,
0365                  struct inode *new_dir, struct dentry *new_dentry,
0366                  unsigned int flags);
0367 int __fscrypt_prepare_lookup(struct inode *dir, struct dentry *dentry,
0368                  struct fscrypt_name *fname);
0369 int __fscrypt_prepare_readdir(struct inode *dir);
0370 int __fscrypt_prepare_setattr(struct dentry *dentry, struct iattr *attr);
0371 int fscrypt_prepare_setflags(struct inode *inode,
0372                  unsigned int oldflags, unsigned int flags);
0373 int fscrypt_prepare_symlink(struct inode *dir, const char *target,
0374                 unsigned int len, unsigned int max_len,
0375                 struct fscrypt_str *disk_link);
0376 int __fscrypt_encrypt_symlink(struct inode *inode, const char *target,
0377                   unsigned int len, struct fscrypt_str *disk_link);
0378 const char *fscrypt_get_symlink(struct inode *inode, const void *caddr,
0379                 unsigned int max_size,
0380                 struct delayed_call *done);
0381 int fscrypt_symlink_getattr(const struct path *path, struct kstat *stat);
0382 static inline void fscrypt_set_ops(struct super_block *sb,
0383                    const struct fscrypt_operations *s_cop)
0384 {
0385     sb->s_cop = s_cop;
0386 }
0387 #else  /* !CONFIG_FS_ENCRYPTION */
0388 
0389 static inline struct fscrypt_info *fscrypt_get_info(const struct inode *inode)
0390 {
0391     return NULL;
0392 }
0393 
0394 static inline bool fscrypt_needs_contents_encryption(const struct inode *inode)
0395 {
0396     return false;
0397 }
0398 
0399 static inline void fscrypt_handle_d_move(struct dentry *dentry)
0400 {
0401 }
0402 
0403 static inline bool fscrypt_is_nokey_name(const struct dentry *dentry)
0404 {
0405     return false;
0406 }
0407 
0408 /* crypto.c */
0409 static inline void fscrypt_enqueue_decrypt_work(struct work_struct *work)
0410 {
0411 }
0412 
0413 static inline struct page *fscrypt_encrypt_pagecache_blocks(struct page *page,
0414                                 unsigned int len,
0415                                 unsigned int offs,
0416                                 gfp_t gfp_flags)
0417 {
0418     return ERR_PTR(-EOPNOTSUPP);
0419 }
0420 
0421 static inline int fscrypt_encrypt_block_inplace(const struct inode *inode,
0422                         struct page *page,
0423                         unsigned int len,
0424                         unsigned int offs, u64 lblk_num,
0425                         gfp_t gfp_flags)
0426 {
0427     return -EOPNOTSUPP;
0428 }
0429 
0430 static inline int fscrypt_decrypt_pagecache_blocks(struct page *page,
0431                            unsigned int len,
0432                            unsigned int offs)
0433 {
0434     return -EOPNOTSUPP;
0435 }
0436 
0437 static inline int fscrypt_decrypt_block_inplace(const struct inode *inode,
0438                         struct page *page,
0439                         unsigned int len,
0440                         unsigned int offs, u64 lblk_num)
0441 {
0442     return -EOPNOTSUPP;
0443 }
0444 
0445 static inline bool fscrypt_is_bounce_page(struct page *page)
0446 {
0447     return false;
0448 }
0449 
0450 static inline struct page *fscrypt_pagecache_page(struct page *bounce_page)
0451 {
0452     WARN_ON_ONCE(1);
0453     return ERR_PTR(-EINVAL);
0454 }
0455 
0456 static inline void fscrypt_free_bounce_page(struct page *bounce_page)
0457 {
0458 }
0459 
0460 /* policy.c */
0461 static inline int fscrypt_ioctl_set_policy(struct file *filp,
0462                        const void __user *arg)
0463 {
0464     return -EOPNOTSUPP;
0465 }
0466 
0467 static inline int fscrypt_ioctl_get_policy(struct file *filp, void __user *arg)
0468 {
0469     return -EOPNOTSUPP;
0470 }
0471 
0472 static inline int fscrypt_ioctl_get_policy_ex(struct file *filp,
0473                           void __user *arg)
0474 {
0475     return -EOPNOTSUPP;
0476 }
0477 
0478 static inline int fscrypt_ioctl_get_nonce(struct file *filp, void __user *arg)
0479 {
0480     return -EOPNOTSUPP;
0481 }
0482 
0483 static inline int fscrypt_has_permitted_context(struct inode *parent,
0484                         struct inode *child)
0485 {
0486     return 0;
0487 }
0488 
0489 static inline int fscrypt_set_context(struct inode *inode, void *fs_data)
0490 {
0491     return -EOPNOTSUPP;
0492 }
0493 
0494 struct fscrypt_dummy_policy {
0495 };
0496 
0497 static inline int
0498 fscrypt_parse_test_dummy_encryption(const struct fs_parameter *param,
0499                     struct fscrypt_dummy_policy *dummy_policy)
0500 {
0501     return -EINVAL;
0502 }
0503 
0504 static inline bool
0505 fscrypt_dummy_policies_equal(const struct fscrypt_dummy_policy *p1,
0506                  const struct fscrypt_dummy_policy *p2)
0507 {
0508     return true;
0509 }
0510 
0511 static inline void fscrypt_show_test_dummy_encryption(struct seq_file *seq,
0512                               char sep,
0513                               struct super_block *sb)
0514 {
0515 }
0516 
0517 static inline bool
0518 fscrypt_is_dummy_policy_set(const struct fscrypt_dummy_policy *dummy_policy)
0519 {
0520     return false;
0521 }
0522 
0523 static inline void
0524 fscrypt_free_dummy_policy(struct fscrypt_dummy_policy *dummy_policy)
0525 {
0526 }
0527 
0528 /* keyring.c */
0529 static inline void fscrypt_sb_free(struct super_block *sb)
0530 {
0531 }
0532 
0533 static inline int fscrypt_ioctl_add_key(struct file *filp, void __user *arg)
0534 {
0535     return -EOPNOTSUPP;
0536 }
0537 
0538 static inline int
0539 fscrypt_add_test_dummy_key(struct super_block *sb,
0540                const struct fscrypt_dummy_policy *dummy_policy)
0541 {
0542     return 0;
0543 }
0544 
0545 static inline int fscrypt_ioctl_remove_key(struct file *filp, void __user *arg)
0546 {
0547     return -EOPNOTSUPP;
0548 }
0549 
0550 static inline int fscrypt_ioctl_remove_key_all_users(struct file *filp,
0551                              void __user *arg)
0552 {
0553     return -EOPNOTSUPP;
0554 }
0555 
0556 static inline int fscrypt_ioctl_get_key_status(struct file *filp,
0557                            void __user *arg)
0558 {
0559     return -EOPNOTSUPP;
0560 }
0561 
0562 /* keysetup.c */
0563 
0564 static inline int fscrypt_prepare_new_inode(struct inode *dir,
0565                         struct inode *inode,
0566                         bool *encrypt_ret)
0567 {
0568     if (IS_ENCRYPTED(dir))
0569         return -EOPNOTSUPP;
0570     return 0;
0571 }
0572 
0573 static inline void fscrypt_put_encryption_info(struct inode *inode)
0574 {
0575     return;
0576 }
0577 
0578 static inline void fscrypt_free_inode(struct inode *inode)
0579 {
0580 }
0581 
0582 static inline int fscrypt_drop_inode(struct inode *inode)
0583 {
0584     return 0;
0585 }
0586 
0587  /* fname.c */
0588 static inline int fscrypt_setup_filename(struct inode *dir,
0589                      const struct qstr *iname,
0590                      int lookup, struct fscrypt_name *fname)
0591 {
0592     if (IS_ENCRYPTED(dir))
0593         return -EOPNOTSUPP;
0594 
0595     memset(fname, 0, sizeof(*fname));
0596     fname->usr_fname = iname;
0597     fname->disk_name.name = (unsigned char *)iname->name;
0598     fname->disk_name.len = iname->len;
0599     return 0;
0600 }
0601 
0602 static inline void fscrypt_free_filename(struct fscrypt_name *fname)
0603 {
0604     return;
0605 }
0606 
0607 static inline int fscrypt_fname_alloc_buffer(u32 max_encrypted_len,
0608                          struct fscrypt_str *crypto_str)
0609 {
0610     return -EOPNOTSUPP;
0611 }
0612 
0613 static inline void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
0614 {
0615     return;
0616 }
0617 
0618 static inline int fscrypt_fname_disk_to_usr(const struct inode *inode,
0619                         u32 hash, u32 minor_hash,
0620                         const struct fscrypt_str *iname,
0621                         struct fscrypt_str *oname)
0622 {
0623     return -EOPNOTSUPP;
0624 }
0625 
0626 static inline bool fscrypt_match_name(const struct fscrypt_name *fname,
0627                       const u8 *de_name, u32 de_name_len)
0628 {
0629     /* Encryption support disabled; use standard comparison */
0630     if (de_name_len != fname->disk_name.len)
0631         return false;
0632     return !memcmp(de_name, fname->disk_name.name, fname->disk_name.len);
0633 }
0634 
0635 static inline u64 fscrypt_fname_siphash(const struct inode *dir,
0636                     const struct qstr *name)
0637 {
0638     WARN_ON_ONCE(1);
0639     return 0;
0640 }
0641 
0642 static inline int fscrypt_d_revalidate(struct dentry *dentry,
0643                        unsigned int flags)
0644 {
0645     return 1;
0646 }
0647 
0648 /* bio.c */
0649 static inline void fscrypt_decrypt_bio(struct bio *bio)
0650 {
0651 }
0652 
0653 static inline int fscrypt_zeroout_range(const struct inode *inode, pgoff_t lblk,
0654                     sector_t pblk, unsigned int len)
0655 {
0656     return -EOPNOTSUPP;
0657 }
0658 
0659 /* hooks.c */
0660 
0661 static inline int fscrypt_file_open(struct inode *inode, struct file *filp)
0662 {
0663     if (IS_ENCRYPTED(inode))
0664         return -EOPNOTSUPP;
0665     return 0;
0666 }
0667 
0668 static inline int __fscrypt_prepare_link(struct inode *inode, struct inode *dir,
0669                      struct dentry *dentry)
0670 {
0671     return -EOPNOTSUPP;
0672 }
0673 
0674 static inline int __fscrypt_prepare_rename(struct inode *old_dir,
0675                        struct dentry *old_dentry,
0676                        struct inode *new_dir,
0677                        struct dentry *new_dentry,
0678                        unsigned int flags)
0679 {
0680     return -EOPNOTSUPP;
0681 }
0682 
0683 static inline int __fscrypt_prepare_lookup(struct inode *dir,
0684                        struct dentry *dentry,
0685                        struct fscrypt_name *fname)
0686 {
0687     return -EOPNOTSUPP;
0688 }
0689 
0690 static inline int __fscrypt_prepare_readdir(struct inode *dir)
0691 {
0692     return -EOPNOTSUPP;
0693 }
0694 
0695 static inline int __fscrypt_prepare_setattr(struct dentry *dentry,
0696                         struct iattr *attr)
0697 {
0698     return -EOPNOTSUPP;
0699 }
0700 
0701 static inline int fscrypt_prepare_setflags(struct inode *inode,
0702                        unsigned int oldflags,
0703                        unsigned int flags)
0704 {
0705     return 0;
0706 }
0707 
0708 static inline int fscrypt_prepare_symlink(struct inode *dir,
0709                       const char *target,
0710                       unsigned int len,
0711                       unsigned int max_len,
0712                       struct fscrypt_str *disk_link)
0713 {
0714     if (IS_ENCRYPTED(dir))
0715         return -EOPNOTSUPP;
0716     disk_link->name = (unsigned char *)target;
0717     disk_link->len = len + 1;
0718     if (disk_link->len > max_len)
0719         return -ENAMETOOLONG;
0720     return 0;
0721 }
0722 
0723 static inline int __fscrypt_encrypt_symlink(struct inode *inode,
0724                         const char *target,
0725                         unsigned int len,
0726                         struct fscrypt_str *disk_link)
0727 {
0728     return -EOPNOTSUPP;
0729 }
0730 
0731 static inline const char *fscrypt_get_symlink(struct inode *inode,
0732                           const void *caddr,
0733                           unsigned int max_size,
0734                           struct delayed_call *done)
0735 {
0736     return ERR_PTR(-EOPNOTSUPP);
0737 }
0738 
0739 static inline int fscrypt_symlink_getattr(const struct path *path,
0740                       struct kstat *stat)
0741 {
0742     return -EOPNOTSUPP;
0743 }
0744 
0745 static inline void fscrypt_set_ops(struct super_block *sb,
0746                    const struct fscrypt_operations *s_cop)
0747 {
0748 }
0749 
0750 #endif  /* !CONFIG_FS_ENCRYPTION */
0751 
0752 /* inline_crypt.c */
0753 #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
0754 
0755 bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode);
0756 
0757 void fscrypt_set_bio_crypt_ctx(struct bio *bio,
0758                    const struct inode *inode, u64 first_lblk,
0759                    gfp_t gfp_mask);
0760 
0761 void fscrypt_set_bio_crypt_ctx_bh(struct bio *bio,
0762                   const struct buffer_head *first_bh,
0763                   gfp_t gfp_mask);
0764 
0765 bool fscrypt_mergeable_bio(struct bio *bio, const struct inode *inode,
0766                u64 next_lblk);
0767 
0768 bool fscrypt_mergeable_bio_bh(struct bio *bio,
0769                   const struct buffer_head *next_bh);
0770 
0771 bool fscrypt_dio_supported(struct kiocb *iocb, struct iov_iter *iter);
0772 
0773 u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk, u64 nr_blocks);
0774 
0775 #else /* CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
0776 
0777 static inline bool __fscrypt_inode_uses_inline_crypto(const struct inode *inode)
0778 {
0779     return false;
0780 }
0781 
0782 static inline void fscrypt_set_bio_crypt_ctx(struct bio *bio,
0783                          const struct inode *inode,
0784                          u64 first_lblk, gfp_t gfp_mask) { }
0785 
0786 static inline void fscrypt_set_bio_crypt_ctx_bh(
0787                      struct bio *bio,
0788                      const struct buffer_head *first_bh,
0789                      gfp_t gfp_mask) { }
0790 
0791 static inline bool fscrypt_mergeable_bio(struct bio *bio,
0792                      const struct inode *inode,
0793                      u64 next_lblk)
0794 {
0795     return true;
0796 }
0797 
0798 static inline bool fscrypt_mergeable_bio_bh(struct bio *bio,
0799                         const struct buffer_head *next_bh)
0800 {
0801     return true;
0802 }
0803 
0804 static inline bool fscrypt_dio_supported(struct kiocb *iocb,
0805                      struct iov_iter *iter)
0806 {
0807     const struct inode *inode = file_inode(iocb->ki_filp);
0808 
0809     return !fscrypt_needs_contents_encryption(inode);
0810 }
0811 
0812 static inline u64 fscrypt_limit_io_blocks(const struct inode *inode, u64 lblk,
0813                       u64 nr_blocks)
0814 {
0815     return nr_blocks;
0816 }
0817 #endif /* !CONFIG_FS_ENCRYPTION_INLINE_CRYPT */
0818 
0819 /**
0820  * fscrypt_inode_uses_inline_crypto() - test whether an inode uses inline
0821  *                  encryption
0822  * @inode: an inode. If encrypted, its key must be set up.
0823  *
0824  * Return: true if the inode requires file contents encryption and if the
0825  *     encryption should be done in the block layer via blk-crypto rather
0826  *     than in the filesystem layer.
0827  */
0828 static inline bool fscrypt_inode_uses_inline_crypto(const struct inode *inode)
0829 {
0830     return fscrypt_needs_contents_encryption(inode) &&
0831            __fscrypt_inode_uses_inline_crypto(inode);
0832 }
0833 
0834 /**
0835  * fscrypt_inode_uses_fs_layer_crypto() - test whether an inode uses fs-layer
0836  *                    encryption
0837  * @inode: an inode. If encrypted, its key must be set up.
0838  *
0839  * Return: true if the inode requires file contents encryption and if the
0840  *     encryption should be done in the filesystem layer rather than in the
0841  *     block layer via blk-crypto.
0842  */
0843 static inline bool fscrypt_inode_uses_fs_layer_crypto(const struct inode *inode)
0844 {
0845     return fscrypt_needs_contents_encryption(inode) &&
0846            !__fscrypt_inode_uses_inline_crypto(inode);
0847 }
0848 
0849 /**
0850  * fscrypt_has_encryption_key() - check whether an inode has had its key set up
0851  * @inode: the inode to check
0852  *
0853  * Return: %true if the inode has had its encryption key set up, else %false.
0854  *
0855  * Usually this should be preceded by fscrypt_get_encryption_info() to try to
0856  * set up the key first.
0857  */
0858 static inline bool fscrypt_has_encryption_key(const struct inode *inode)
0859 {
0860     return fscrypt_get_info(inode) != NULL;
0861 }
0862 
0863 /**
0864  * fscrypt_prepare_link() - prepare to link an inode into a possibly-encrypted
0865  *              directory
0866  * @old_dentry: an existing dentry for the inode being linked
0867  * @dir: the target directory
0868  * @dentry: negative dentry for the target filename
0869  *
0870  * A new link can only be added to an encrypted directory if the directory's
0871  * encryption key is available --- since otherwise we'd have no way to encrypt
0872  * the filename.
0873  *
0874  * We also verify that the link will not violate the constraint that all files
0875  * in an encrypted directory tree use the same encryption policy.
0876  *
0877  * Return: 0 on success, -ENOKEY if the directory's encryption key is missing,
0878  * -EXDEV if the link would result in an inconsistent encryption policy, or
0879  * another -errno code.
0880  */
0881 static inline int fscrypt_prepare_link(struct dentry *old_dentry,
0882                        struct inode *dir,
0883                        struct dentry *dentry)
0884 {
0885     if (IS_ENCRYPTED(dir))
0886         return __fscrypt_prepare_link(d_inode(old_dentry), dir, dentry);
0887     return 0;
0888 }
0889 
0890 /**
0891  * fscrypt_prepare_rename() - prepare for a rename between possibly-encrypted
0892  *                directories
0893  * @old_dir: source directory
0894  * @old_dentry: dentry for source file
0895  * @new_dir: target directory
0896  * @new_dentry: dentry for target location (may be negative unless exchanging)
0897  * @flags: rename flags (we care at least about %RENAME_EXCHANGE)
0898  *
0899  * Prepare for ->rename() where the source and/or target directories may be
0900  * encrypted.  A new link can only be added to an encrypted directory if the
0901  * directory's encryption key is available --- since otherwise we'd have no way
0902  * to encrypt the filename.  A rename to an existing name, on the other hand,
0903  * *is* cryptographically possible without the key.  However, we take the more
0904  * conservative approach and just forbid all no-key renames.
0905  *
0906  * We also verify that the rename will not violate the constraint that all files
0907  * in an encrypted directory tree use the same encryption policy.
0908  *
0909  * Return: 0 on success, -ENOKEY if an encryption key is missing, -EXDEV if the
0910  * rename would cause inconsistent encryption policies, or another -errno code.
0911  */
0912 static inline int fscrypt_prepare_rename(struct inode *old_dir,
0913                      struct dentry *old_dentry,
0914                      struct inode *new_dir,
0915                      struct dentry *new_dentry,
0916                      unsigned int flags)
0917 {
0918     if (IS_ENCRYPTED(old_dir) || IS_ENCRYPTED(new_dir))
0919         return __fscrypt_prepare_rename(old_dir, old_dentry,
0920                         new_dir, new_dentry, flags);
0921     return 0;
0922 }
0923 
0924 /**
0925  * fscrypt_prepare_lookup() - prepare to lookup a name in a possibly-encrypted
0926  *                directory
0927  * @dir: directory being searched
0928  * @dentry: filename being looked up
0929  * @fname: (output) the name to use to search the on-disk directory
0930  *
0931  * Prepare for ->lookup() in a directory which may be encrypted by determining
0932  * the name that will actually be used to search the directory on-disk.  If the
0933  * directory's encryption policy is supported by this kernel and its encryption
0934  * key is available, then the lookup is assumed to be by plaintext name;
0935  * otherwise, it is assumed to be by no-key name.
0936  *
0937  * This will set DCACHE_NOKEY_NAME on the dentry if the lookup is by no-key
0938  * name.  In this case the filesystem must assign the dentry a dentry_operations
0939  * which contains fscrypt_d_revalidate (or contains a d_revalidate method that
0940  * calls fscrypt_d_revalidate), so that the dentry will be invalidated if the
0941  * directory's encryption key is later added.
0942  *
0943  * Return: 0 on success; -ENOENT if the directory's key is unavailable but the
0944  * filename isn't a valid no-key name, so a negative dentry should be created;
0945  * or another -errno code.
0946  */
0947 static inline int fscrypt_prepare_lookup(struct inode *dir,
0948                      struct dentry *dentry,
0949                      struct fscrypt_name *fname)
0950 {
0951     if (IS_ENCRYPTED(dir))
0952         return __fscrypt_prepare_lookup(dir, dentry, fname);
0953 
0954     memset(fname, 0, sizeof(*fname));
0955     fname->usr_fname = &dentry->d_name;
0956     fname->disk_name.name = (unsigned char *)dentry->d_name.name;
0957     fname->disk_name.len = dentry->d_name.len;
0958     return 0;
0959 }
0960 
0961 /**
0962  * fscrypt_prepare_readdir() - prepare to read a possibly-encrypted directory
0963  * @dir: the directory inode
0964  *
0965  * If the directory is encrypted and it doesn't already have its encryption key
0966  * set up, try to set it up so that the filenames will be listed in plaintext
0967  * form rather than in no-key form.
0968  *
0969  * Return: 0 on success; -errno on error.  Note that the encryption key being
0970  *     unavailable is not considered an error.  It is also not an error if
0971  *     the encryption policy is unsupported by this kernel; that is treated
0972  *     like the key being unavailable, so that files can still be deleted.
0973  */
0974 static inline int fscrypt_prepare_readdir(struct inode *dir)
0975 {
0976     if (IS_ENCRYPTED(dir))
0977         return __fscrypt_prepare_readdir(dir);
0978     return 0;
0979 }
0980 
0981 /**
0982  * fscrypt_prepare_setattr() - prepare to change a possibly-encrypted inode's
0983  *                 attributes
0984  * @dentry: dentry through which the inode is being changed
0985  * @attr: attributes to change
0986  *
0987  * Prepare for ->setattr() on a possibly-encrypted inode.  On an encrypted file,
0988  * most attribute changes are allowed even without the encryption key.  However,
0989  * without the encryption key we do have to forbid truncates.  This is needed
0990  * because the size being truncated to may not be a multiple of the filesystem
0991  * block size, and in that case we'd have to decrypt the final block, zero the
0992  * portion past i_size, and re-encrypt it.  (We *could* allow truncating to a
0993  * filesystem block boundary, but it's simpler to just forbid all truncates ---
0994  * and we already forbid all other contents modifications without the key.)
0995  *
0996  * Return: 0 on success, -ENOKEY if the key is missing, or another -errno code
0997  * if a problem occurred while setting up the encryption key.
0998  */
0999 static inline int fscrypt_prepare_setattr(struct dentry *dentry,
1000                       struct iattr *attr)
1001 {
1002     if (IS_ENCRYPTED(d_inode(dentry)))
1003         return __fscrypt_prepare_setattr(dentry, attr);
1004     return 0;
1005 }
1006 
1007 /**
1008  * fscrypt_encrypt_symlink() - encrypt the symlink target if needed
1009  * @inode: symlink inode
1010  * @target: plaintext symlink target
1011  * @len: length of @target excluding null terminator
1012  * @disk_link: (in/out) the on-disk symlink target being prepared
1013  *
1014  * If the symlink target needs to be encrypted, then this function encrypts it
1015  * into @disk_link->name.  fscrypt_prepare_symlink() must have been called
1016  * previously to compute @disk_link->len.  If the filesystem did not allocate a
1017  * buffer for @disk_link->name after calling fscrypt_prepare_link(), then one
1018  * will be kmalloc()'ed and the filesystem will be responsible for freeing it.
1019  *
1020  * Return: 0 on success, -errno on failure
1021  */
1022 static inline int fscrypt_encrypt_symlink(struct inode *inode,
1023                       const char *target,
1024                       unsigned int len,
1025                       struct fscrypt_str *disk_link)
1026 {
1027     if (IS_ENCRYPTED(inode))
1028         return __fscrypt_encrypt_symlink(inode, target, len, disk_link);
1029     return 0;
1030 }
1031 
1032 /* If *pagep is a bounce page, free it and set *pagep to the pagecache page */
1033 static inline void fscrypt_finalize_bounce_page(struct page **pagep)
1034 {
1035     struct page *page = *pagep;
1036 
1037     if (fscrypt_is_bounce_page(page)) {
1038         *pagep = fscrypt_pagecache_page(page);
1039         fscrypt_free_bounce_page(page);
1040     }
1041 }
1042 
1043 #endif  /* _LINUX_FSCRYPT_H */