2 * General per-file encryption definition
4 * Copyright (C) 2015, Google, Inc.
6 * Written by Michael Halcrow, 2015.
7 * Modified by Jaegeuk Kim, 2015.
10 #ifndef _LINUX_FSCRYPTO_H
11 #define _LINUX_FSCRYPTO_H
13 #include <linux/key.h>
16 #include <linux/bio.h>
17 #include <linux/dcache.h>
18 #include <crypto/skcipher.h>
19 #include <uapi/linux/fs.h>
21 #define FS_CRYPTO_BLOCK_SIZE 16
28 struct page *bounce_page; /* Ciphertext page */
29 struct page *control_page; /* Original page */
33 struct work_struct work;
35 struct list_head free_list; /* Free list */
41 * For encrypted symlinks, the ciphertext length is stored at the beginning
42 * of the string in little-endian format.
44 struct fscrypt_symlink_data {
46 char encrypted_path[1];
50 * This function is used to calculate the disk space required to
51 * store a filename of length l in encrypted symlink format.
53 static inline u32 fscrypt_symlink_data_len(u32 l)
55 if (l < FS_CRYPTO_BLOCK_SIZE)
56 l = FS_CRYPTO_BLOCK_SIZE;
57 return (l + sizeof(struct fscrypt_symlink_data) - 1);
66 const struct qstr *usr_fname;
67 struct fscrypt_str disk_name;
70 struct fscrypt_str crypto_buf;
73 #define FSTR_INIT(n, l) { .name = n, .len = l }
74 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
75 #define fname_name(p) ((p)->disk_name.name)
76 #define fname_len(p) ((p)->disk_name.len)
79 * fscrypt superblock flags
81 #define FS_CFLG_OWN_PAGES (1U << 1)
84 * crypto opertions for filesystems
86 struct fscrypt_operations {
88 const char *key_prefix;
89 int (*get_context)(struct inode *, void *, size_t);
90 int (*prepare_context)(struct inode *);
91 int (*set_context)(struct inode *, const void *, size_t, void *);
92 int (*dummy_context)(struct inode *);
93 bool (*is_encrypted)(struct inode *);
94 bool (*empty_dir)(struct inode *);
95 unsigned (*max_namelen)(struct inode *);
98 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
100 if (inode->i_sb->s_cop->dummy_context &&
101 inode->i_sb->s_cop->dummy_context(inode))
106 static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
108 return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
111 static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
113 return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
116 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
118 if (str->len == 1 && str->name[0] == '.')
121 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
127 static inline struct page *fscrypt_control_page(struct page *page)
129 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
130 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
133 return ERR_PTR(-EINVAL);
137 static inline int fscrypt_has_encryption_key(const struct inode *inode)
139 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
140 return (inode->i_crypt_info != NULL);
146 static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
148 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
149 spin_lock(&dentry->d_lock);
150 dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
151 spin_unlock(&dentry->d_lock);
155 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
156 extern const struct dentry_operations fscrypt_d_ops;
159 static inline void fscrypt_set_d_op(struct dentry *dentry)
161 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
162 d_set_d_op(dentry, &fscrypt_d_ops);
166 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
168 extern struct kmem_cache *fscrypt_info_cachep;
169 extern struct fscrypt_ctx *fscrypt_get_ctx(const struct inode *, gfp_t);
170 extern void fscrypt_release_ctx(struct fscrypt_ctx *);
171 extern struct page *fscrypt_encrypt_page(const struct inode *, struct page *,
172 unsigned int, unsigned int,
174 extern int fscrypt_decrypt_page(const struct inode *, struct page *, unsigned int,
176 extern void fscrypt_restore_control_page(struct page *);
179 extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
180 extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
181 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
182 extern int fscrypt_inherit_context(struct inode *, struct inode *,
185 extern int fscrypt_get_encryption_info(struct inode *);
186 extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
189 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
190 int lookup, struct fscrypt_name *);
191 extern void fscrypt_free_filename(struct fscrypt_name *);
192 extern u32 fscrypt_fname_encrypted_size(const struct inode *, u32);
193 extern int fscrypt_fname_alloc_buffer(const struct inode *, u32,
194 struct fscrypt_str *);
195 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
196 extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
197 const struct fscrypt_str *, struct fscrypt_str *);
198 extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
199 struct fscrypt_str *);
202 extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
203 extern void fscrypt_pullback_bio_page(struct page **, bool);
204 extern int fscrypt_zeroout_range(const struct inode *, pgoff_t, sector_t,
209 static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(const struct inode *i,
212 return ERR_PTR(-EOPNOTSUPP);
215 static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
220 static inline struct page *fscrypt_notsupp_encrypt_page(const struct inode *i,
224 u64 lblk_num, gfp_t f)
226 return ERR_PTR(-EOPNOTSUPP);
229 static inline int fscrypt_notsupp_decrypt_page(const struct inode *i, struct page *p,
230 unsigned int len, unsigned int offs,
236 static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c,
242 static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b)
247 static inline void fscrypt_notsupp_restore_control_page(struct page *p)
252 static inline int fscrypt_notsupp_zeroout_range(const struct inode *i, pgoff_t p,
253 sector_t s, unsigned int f)
259 static inline int fscrypt_notsupp_ioctl_set_policy(struct file *f,
260 const void __user *arg)
265 static inline int fscrypt_notsupp_ioctl_get_policy(struct file *f,
271 static inline int fscrypt_notsupp_has_permitted_context(struct inode *p,
277 static inline int fscrypt_notsupp_inherit_context(struct inode *p,
278 struct inode *i, void *v, bool b)
284 static inline int fscrypt_notsupp_get_encryption_info(struct inode *i)
289 static inline void fscrypt_notsupp_put_encryption_info(struct inode *i,
290 struct fscrypt_info *f)
296 static inline int fscrypt_notsupp_setup_filename(struct inode *dir,
297 const struct qstr *iname,
298 int lookup, struct fscrypt_name *fname)
300 if (dir->i_sb->s_cop->is_encrypted(dir))
303 memset(fname, 0, sizeof(struct fscrypt_name));
304 fname->usr_fname = iname;
305 fname->disk_name.name = (unsigned char *)iname->name;
306 fname->disk_name.len = iname->len;
310 static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname)
315 static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s)
322 static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode,
323 u32 ilen, struct fscrypt_str *crypto_str)
328 static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c)
333 static inline int fscrypt_notsupp_fname_disk_to_usr(struct inode *inode,
334 u32 hash, u32 minor_hash,
335 const struct fscrypt_str *iname,
336 struct fscrypt_str *oname)
341 static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode,
342 const struct qstr *iname,
343 struct fscrypt_str *oname)
347 #endif /* _LINUX_FSCRYPTO_H */