License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-2.6-block.git] / include / linux / fscrypt_common.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
46f47e48
EB
2/*
3 * fscrypt_common.h: common declarations for per-file encryption
4 *
5 * Copyright (C) 2015, Google, Inc.
6 *
7 * Written by Michael Halcrow, 2015.
8 * Modified by Jaegeuk Kim, 2015.
9 */
10
11#ifndef _LINUX_FSCRYPT_COMMON_H
12#define _LINUX_FSCRYPT_COMMON_H
13
14#include <linux/key.h>
15#include <linux/fs.h>
16#include <linux/mm.h>
17#include <linux/bio.h>
18#include <linux/dcache.h>
19#include <crypto/skcipher.h>
20#include <uapi/linux/fs.h>
21
22#define FS_CRYPTO_BLOCK_SIZE 16
23
24struct fscrypt_info;
25
26struct fscrypt_ctx {
27 union {
28 struct {
29 struct page *bounce_page; /* Ciphertext page */
30 struct page *control_page; /* Original page */
31 } w;
32 struct {
33 struct bio *bio;
34 struct work_struct work;
35 } r;
36 struct list_head free_list; /* Free list */
37 };
38 u8 flags; /* Flags */
39};
40
41/**
42 * For encrypted symlinks, the ciphertext length is stored at the beginning
43 * of the string in little-endian format.
44 */
45struct fscrypt_symlink_data {
46 __le16 len;
47 char encrypted_path[1];
48} __packed;
49
46f47e48
EB
50struct fscrypt_str {
51 unsigned char *name;
52 u32 len;
53};
54
55struct fscrypt_name {
56 const struct qstr *usr_fname;
57 struct fscrypt_str disk_name;
58 u32 hash;
59 u32 minor_hash;
60 struct fscrypt_str crypto_buf;
61};
62
63#define FSTR_INIT(n, l) { .name = n, .len = l }
64#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
65#define fname_name(p) ((p)->disk_name.name)
66#define fname_len(p) ((p)->disk_name.len)
67
68/*
69 * fscrypt superblock flags
70 */
71#define FS_CFLG_OWN_PAGES (1U << 1)
72
73/*
74 * crypto opertions for filesystems
75 */
76struct fscrypt_operations {
77 unsigned int flags;
78 const char *key_prefix;
79 int (*get_context)(struct inode *, void *, size_t);
46f47e48 80 int (*set_context)(struct inode *, const void *, size_t, void *);
c250b7dd 81 bool (*dummy_context)(struct inode *);
46f47e48
EB
82 bool (*is_encrypted)(struct inode *);
83 bool (*empty_dir)(struct inode *);
84 unsigned (*max_namelen)(struct inode *);
85};
86
af65207c
TE
87/* Maximum value for the third parameter of fscrypt_operations.set_context(). */
88#define FSCRYPT_SET_CONTEXT_MAX_SIZE 28
89
46f47e48
EB
90static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
91{
92 if (inode->i_sb->s_cop->dummy_context &&
93 inode->i_sb->s_cop->dummy_context(inode))
94 return true;
95 return false;
96}
97
b7e7cf7a
DW
98static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
99 u32 filenames_mode)
46f47e48 100{
b7e7cf7a
DW
101 if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC &&
102 filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS)
103 return true;
46f47e48 104
b7e7cf7a
DW
105 if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS &&
106 filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS)
107 return true;
108
109 return false;
46f47e48
EB
110}
111
112static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
113{
114 if (str->len == 1 && str->name[0] == '.')
115 return true;
116
117 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
118 return true;
119
120 return false;
121}
122
123static inline struct page *fscrypt_control_page(struct page *page)
124{
125#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
126 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
127#else
128 WARN_ON_ONCE(1);
129 return ERR_PTR(-EINVAL);
130#endif
131}
132
133static inline int fscrypt_has_encryption_key(const struct inode *inode)
134{
135#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
136 return (inode->i_crypt_info != NULL);
137#else
138 return 0;
139#endif
140}
141
142#endif /* _LINUX_FSCRYPT_COMMON_H */