Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1 | /** |
| 2 | * eCryptfs: Linux filesystem encryption layer |
| 3 | * |
| 4 | * Copyright (C) 1997-2003 Erez Zadok |
| 5 | * Copyright (C) 2001-2003 Stony Brook University |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 6 | * Copyright (C) 2004-2007 International Business Machines Corp. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 7 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
| 8 | * Michael C. Thompson <mcthomps@us.ibm.com> |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 9 | * Tyler Hicks <tyhicks@ou.edu> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of the |
| 14 | * License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 24 | * 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | #include <linux/dcache.h> |
| 28 | #include <linux/file.h> |
| 29 | #include <linux/module.h> |
| 30 | #include <linux/namei.h> |
| 31 | #include <linux/skbuff.h> |
| 32 | #include <linux/crypto.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 33 | #include <linux/mount.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 34 | #include <linux/pagemap.h> |
| 35 | #include <linux/key.h> |
| 36 | #include <linux/parser.h> |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 37 | #include <linux/fs_stack.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 38 | #include "ecryptfs_kernel.h" |
| 39 | |
| 40 | /** |
| 41 | * Module parameter that defines the ecryptfs_verbosity level. |
| 42 | */ |
| 43 | int ecryptfs_verbosity = 0; |
| 44 | |
| 45 | module_param(ecryptfs_verbosity, int, 0); |
| 46 | MODULE_PARM_DESC(ecryptfs_verbosity, |
| 47 | "Initial verbosity level (0 or 1; defaults to " |
| 48 | "0, which is Quiet)"); |
| 49 | |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 50 | /** |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 51 | * Module parameter that defines the number of message buffer elements |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 52 | */ |
| 53 | unsigned int ecryptfs_message_buf_len = ECRYPTFS_DEFAULT_MSG_CTX_ELEMS; |
| 54 | |
| 55 | module_param(ecryptfs_message_buf_len, uint, 0); |
| 56 | MODULE_PARM_DESC(ecryptfs_message_buf_len, |
| 57 | "Number of message buffer elements"); |
| 58 | |
| 59 | /** |
| 60 | * Module parameter that defines the maximum guaranteed amount of time to wait |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 61 | * for a response from ecryptfsd. The actual sleep time will be, more than |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 62 | * likely, a small amount greater than this specified value, but only less if |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 63 | * the message successfully arrives. |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 64 | */ |
| 65 | signed long ecryptfs_message_wait_timeout = ECRYPTFS_MAX_MSG_CTX_TTL / HZ; |
| 66 | |
| 67 | module_param(ecryptfs_message_wait_timeout, long, 0); |
| 68 | MODULE_PARM_DESC(ecryptfs_message_wait_timeout, |
| 69 | "Maximum number of seconds that an operation will " |
| 70 | "sleep while waiting for a message response from " |
| 71 | "userspace"); |
| 72 | |
| 73 | /** |
| 74 | * Module parameter that is an estimate of the maximum number of users |
| 75 | * that will be concurrently using eCryptfs. Set this to the right |
| 76 | * value to balance performance and memory use. |
| 77 | */ |
| 78 | unsigned int ecryptfs_number_of_users = ECRYPTFS_DEFAULT_NUM_USERS; |
| 79 | |
| 80 | module_param(ecryptfs_number_of_users, uint, 0); |
| 81 | MODULE_PARM_DESC(ecryptfs_number_of_users, "An estimate of the number of " |
| 82 | "concurrent users of eCryptfs"); |
| 83 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 84 | void __ecryptfs_printk(const char *fmt, ...) |
| 85 | { |
| 86 | va_list args; |
| 87 | va_start(args, fmt); |
| 88 | if (fmt[1] == '7') { /* KERN_DEBUG */ |
| 89 | if (ecryptfs_verbosity >= 1) |
| 90 | vprintk(fmt, args); |
| 91 | } else |
| 92 | vprintk(fmt, args); |
| 93 | va_end(args); |
| 94 | } |
| 95 | |
| 96 | /** |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 97 | * ecryptfs_init_persistent_file |
| 98 | * @ecryptfs_dentry: Fully initialized eCryptfs dentry object, with |
| 99 | * the lower dentry and the lower mount set |
| 100 | * |
| 101 | * eCryptfs only ever keeps a single open file for every lower |
| 102 | * inode. All I/O operations to the lower inode occur through that |
| 103 | * file. When the first eCryptfs dentry that interposes with the first |
| 104 | * lower dentry for that inode is created, this function creates the |
| 105 | * persistent file struct and associates it with the eCryptfs |
| 106 | * inode. When the eCryptfs inode is destroyed, the file is closed. |
| 107 | * |
| 108 | * The persistent file will be opened with read/write permissions, if |
| 109 | * possible. Otherwise, it is opened read-only. |
| 110 | * |
| 111 | * This function does nothing if a lower persistent file is already |
| 112 | * associated with the eCryptfs inode. |
| 113 | * |
| 114 | * Returns zero on success; non-zero otherwise |
| 115 | */ |
Michael Halcrow | 72b55ff | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 116 | int ecryptfs_init_persistent_file(struct dentry *ecryptfs_dentry) |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 117 | { |
| 118 | struct ecryptfs_inode_info *inode_info = |
| 119 | ecryptfs_inode_to_private(ecryptfs_dentry->d_inode); |
| 120 | int rc = 0; |
| 121 | |
| 122 | mutex_lock(&inode_info->lower_file_mutex); |
| 123 | if (!inode_info->lower_file) { |
| 124 | struct dentry *lower_dentry; |
| 125 | struct vfsmount *lower_mnt = |
| 126 | ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry); |
| 127 | |
| 128 | lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 129 | rc = ecryptfs_privileged_open(&inode_info->lower_file, |
| 130 | lower_dentry, lower_mnt); |
| 131 | if (rc || IS_ERR(inode_info->lower_file)) { |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 132 | printk(KERN_ERR "Error opening lower persistent file " |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 133 | "for lower_dentry [0x%p] and lower_mnt [0x%p]; " |
| 134 | "rc = [%d]\n", lower_dentry, lower_mnt, rc); |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 135 | rc = PTR_ERR(inode_info->lower_file); |
| 136 | inode_info->lower_file = NULL; |
| 137 | } |
| 138 | } |
| 139 | mutex_unlock(&inode_info->lower_file_mutex); |
| 140 | return rc; |
| 141 | } |
| 142 | |
| 143 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 144 | * ecryptfs_interpose |
| 145 | * @lower_dentry: Existing dentry in the lower filesystem |
| 146 | * @dentry: ecryptfs' dentry |
| 147 | * @sb: ecryptfs's super_block |
Michael Halcrow | 72b55ff | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 148 | * @flags: flags to govern behavior of interpose procedure |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 149 | * |
| 150 | * Interposes upper and lower dentries. |
| 151 | * |
| 152 | * Returns zero on success; non-zero otherwise |
| 153 | */ |
| 154 | int ecryptfs_interpose(struct dentry *lower_dentry, struct dentry *dentry, |
Michael Halcrow | 72b55ff | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 155 | struct super_block *sb, u32 flags) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 156 | { |
| 157 | struct inode *lower_inode; |
| 158 | struct inode *inode; |
| 159 | int rc = 0; |
| 160 | |
| 161 | lower_inode = lower_dentry->d_inode; |
| 162 | if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) { |
| 163 | rc = -EXDEV; |
| 164 | goto out; |
| 165 | } |
| 166 | if (!igrab(lower_inode)) { |
| 167 | rc = -ESTALE; |
| 168 | goto out; |
| 169 | } |
| 170 | inode = iget5_locked(sb, (unsigned long)lower_inode, |
| 171 | ecryptfs_inode_test, ecryptfs_inode_set, |
| 172 | lower_inode); |
| 173 | if (!inode) { |
| 174 | rc = -EACCES; |
| 175 | iput(lower_inode); |
| 176 | goto out; |
| 177 | } |
| 178 | if (inode->i_state & I_NEW) |
| 179 | unlock_new_inode(inode); |
| 180 | else |
| 181 | iput(lower_inode); |
| 182 | if (S_ISLNK(lower_inode->i_mode)) |
| 183 | inode->i_op = &ecryptfs_symlink_iops; |
| 184 | else if (S_ISDIR(lower_inode->i_mode)) |
| 185 | inode->i_op = &ecryptfs_dir_iops; |
| 186 | if (S_ISDIR(lower_inode->i_mode)) |
| 187 | inode->i_fop = &ecryptfs_dir_fops; |
Pekka Enberg | 26da820 | 2006-10-19 23:28:14 -0700 | [diff] [blame] | 188 | if (special_file(lower_inode->i_mode)) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 189 | init_special_inode(inode, lower_inode->i_mode, |
| 190 | lower_inode->i_rdev); |
| 191 | dentry->d_op = &ecryptfs_dops; |
Michael Halcrow | 72b55ff | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 192 | if (flags & ECRYPTFS_INTERPOSE_FLAG_D_ADD) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 193 | d_add(dentry, inode); |
| 194 | else |
| 195 | d_instantiate(dentry, inode); |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 196 | fsstack_copy_attr_all(inode, lower_inode, NULL); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 197 | /* This size will be overwritten for real files w/ headers and |
| 198 | * other metadata */ |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 199 | fsstack_copy_inode_size(inode, lower_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 200 | out: |
| 201 | return rc; |
| 202 | } |
| 203 | |
Eric Sandeen | 2830bfd | 2008-02-06 01:38:34 -0800 | [diff] [blame] | 204 | enum { ecryptfs_opt_sig, ecryptfs_opt_ecryptfs_sig, |
| 205 | ecryptfs_opt_cipher, ecryptfs_opt_ecryptfs_cipher, |
| 206 | ecryptfs_opt_ecryptfs_key_bytes, |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 207 | ecryptfs_opt_passthrough, ecryptfs_opt_xattr_metadata, |
| 208 | ecryptfs_opt_encrypted_view, ecryptfs_opt_err }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 209 | |
Steven Whitehouse | a447c09 | 2008-10-13 10:46:57 +0100 | [diff] [blame] | 210 | static const match_table_t tokens = { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 211 | {ecryptfs_opt_sig, "sig=%s"}, |
| 212 | {ecryptfs_opt_ecryptfs_sig, "ecryptfs_sig=%s"}, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 213 | {ecryptfs_opt_cipher, "cipher=%s"}, |
| 214 | {ecryptfs_opt_ecryptfs_cipher, "ecryptfs_cipher=%s"}, |
| 215 | {ecryptfs_opt_ecryptfs_key_bytes, "ecryptfs_key_bytes=%u"}, |
| 216 | {ecryptfs_opt_passthrough, "ecryptfs_passthrough"}, |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 217 | {ecryptfs_opt_xattr_metadata, "ecryptfs_xattr_metadata"}, |
| 218 | {ecryptfs_opt_encrypted_view, "ecryptfs_encrypted_view"}, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 219 | {ecryptfs_opt_err, NULL} |
| 220 | }; |
| 221 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 222 | static int ecryptfs_init_global_auth_toks( |
| 223 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 224 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 225 | struct ecryptfs_global_auth_tok *global_auth_tok; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 226 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 227 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 228 | list_for_each_entry(global_auth_tok, |
| 229 | &mount_crypt_stat->global_auth_tok_list, |
| 230 | mount_crypt_stat_list) { |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 231 | rc = ecryptfs_keyring_auth_tok_for_sig( |
| 232 | &global_auth_tok->global_auth_tok_key, |
| 233 | &global_auth_tok->global_auth_tok, |
| 234 | global_auth_tok->sig); |
| 235 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 236 | printk(KERN_ERR "Could not find valid key in user " |
| 237 | "session keyring for sig specified in mount " |
| 238 | "option: [%s]\n", global_auth_tok->sig); |
| 239 | global_auth_tok->flags |= ECRYPTFS_AUTH_TOK_INVALID; |
Eric Sandeen | 982363c | 2008-07-23 21:30:04 -0700 | [diff] [blame] | 240 | goto out; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 241 | } else |
| 242 | global_auth_tok->flags &= ~ECRYPTFS_AUTH_TOK_INVALID; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 243 | } |
Eric Sandeen | 982363c | 2008-07-23 21:30:04 -0700 | [diff] [blame] | 244 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 245 | return rc; |
| 246 | } |
| 247 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 248 | static void ecryptfs_init_mount_crypt_stat( |
| 249 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat) |
| 250 | { |
| 251 | memset((void *)mount_crypt_stat, 0, |
| 252 | sizeof(struct ecryptfs_mount_crypt_stat)); |
| 253 | INIT_LIST_HEAD(&mount_crypt_stat->global_auth_tok_list); |
| 254 | mutex_init(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 255 | mount_crypt_stat->flags |= ECRYPTFS_MOUNT_CRYPT_STAT_INITIALIZED; |
| 256 | } |
| 257 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 258 | /** |
| 259 | * ecryptfs_parse_options |
| 260 | * @sb: The ecryptfs super block |
| 261 | * @options: The options pased to the kernel |
| 262 | * |
| 263 | * Parse mount options: |
| 264 | * debug=N - ecryptfs_verbosity level for debug output |
| 265 | * sig=XXX - description(signature) of the key to use |
| 266 | * |
| 267 | * Returns the dentry object of the lower-level (lower/interposed) |
| 268 | * directory; We want to mount our stackable file system on top of |
| 269 | * that lower directory. |
| 270 | * |
| 271 | * The signature of the key to use must be the description of a key |
| 272 | * already in the keyring. Mounting will fail if the key can not be |
| 273 | * found. |
| 274 | * |
| 275 | * Returns zero on success; non-zero on error |
| 276 | */ |
| 277 | static int ecryptfs_parse_options(struct super_block *sb, char *options) |
| 278 | { |
| 279 | char *p; |
| 280 | int rc = 0; |
| 281 | int sig_set = 0; |
| 282 | int cipher_name_set = 0; |
| 283 | int cipher_key_bytes; |
| 284 | int cipher_key_bytes_set = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 285 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 286 | &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; |
| 287 | substring_t args[MAX_OPT_ARGS]; |
| 288 | int token; |
| 289 | char *sig_src; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 290 | char *cipher_name_dst; |
| 291 | char *cipher_name_src; |
| 292 | char *cipher_key_bytes_src; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 293 | |
| 294 | if (!options) { |
| 295 | rc = -EINVAL; |
| 296 | goto out; |
| 297 | } |
Michael Halcrow | 956159c | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 298 | ecryptfs_init_mount_crypt_stat(mount_crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 299 | while ((p = strsep(&options, ",")) != NULL) { |
| 300 | if (!*p) |
| 301 | continue; |
| 302 | token = match_token(p, tokens, args); |
| 303 | switch (token) { |
| 304 | case ecryptfs_opt_sig: |
| 305 | case ecryptfs_opt_ecryptfs_sig: |
| 306 | sig_src = args[0].from; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 307 | rc = ecryptfs_add_global_auth_tok(mount_crypt_stat, |
| 308 | sig_src); |
| 309 | if (rc) { |
| 310 | printk(KERN_ERR "Error attempting to register " |
| 311 | "global sig; rc = [%d]\n", rc); |
| 312 | goto out; |
| 313 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 314 | sig_set = 1; |
| 315 | break; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 316 | case ecryptfs_opt_cipher: |
| 317 | case ecryptfs_opt_ecryptfs_cipher: |
| 318 | cipher_name_src = args[0].from; |
| 319 | cipher_name_dst = |
| 320 | mount_crypt_stat-> |
| 321 | global_default_cipher_name; |
| 322 | strncpy(cipher_name_dst, cipher_name_src, |
| 323 | ECRYPTFS_MAX_CIPHER_NAME_SIZE); |
| 324 | ecryptfs_printk(KERN_DEBUG, |
| 325 | "The mount_crypt_stat " |
| 326 | "global_default_cipher_name set to: " |
| 327 | "[%s]\n", cipher_name_dst); |
| 328 | cipher_name_set = 1; |
| 329 | break; |
| 330 | case ecryptfs_opt_ecryptfs_key_bytes: |
| 331 | cipher_key_bytes_src = args[0].from; |
| 332 | cipher_key_bytes = |
| 333 | (int)simple_strtol(cipher_key_bytes_src, |
| 334 | &cipher_key_bytes_src, 0); |
| 335 | mount_crypt_stat->global_default_cipher_key_size = |
| 336 | cipher_key_bytes; |
| 337 | ecryptfs_printk(KERN_DEBUG, |
| 338 | "The mount_crypt_stat " |
| 339 | "global_default_cipher_key_size " |
| 340 | "set to: [%d]\n", mount_crypt_stat-> |
| 341 | global_default_cipher_key_size); |
| 342 | cipher_key_bytes_set = 1; |
| 343 | break; |
| 344 | case ecryptfs_opt_passthrough: |
| 345 | mount_crypt_stat->flags |= |
| 346 | ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED; |
| 347 | break; |
Michael Halcrow | 1739895 | 2007-02-12 00:53:45 -0800 | [diff] [blame] | 348 | case ecryptfs_opt_xattr_metadata: |
| 349 | mount_crypt_stat->flags |= |
| 350 | ECRYPTFS_XATTR_METADATA_ENABLED; |
| 351 | break; |
| 352 | case ecryptfs_opt_encrypted_view: |
| 353 | mount_crypt_stat->flags |= |
| 354 | ECRYPTFS_XATTR_METADATA_ENABLED; |
| 355 | mount_crypt_stat->flags |= |
| 356 | ECRYPTFS_ENCRYPTED_VIEW_ENABLED; |
| 357 | break; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 358 | case ecryptfs_opt_err: |
| 359 | default: |
| 360 | ecryptfs_printk(KERN_WARNING, |
| 361 | "eCryptfs: unrecognized option '%s'\n", |
| 362 | p); |
| 363 | } |
| 364 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 365 | if (!sig_set) { |
| 366 | rc = -EINVAL; |
Michael Halcrow | 956159c | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 367 | ecryptfs_printk(KERN_ERR, "You must supply at least one valid " |
| 368 | "auth tok signature as a mount " |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 369 | "parameter; see the eCryptfs README\n"); |
| 370 | goto out; |
| 371 | } |
| 372 | if (!cipher_name_set) { |
Miklos Szeredi | 8f23680 | 2008-07-23 21:30:05 -0700 | [diff] [blame] | 373 | int cipher_name_len = strlen(ECRYPTFS_DEFAULT_CIPHER); |
| 374 | |
| 375 | BUG_ON(cipher_name_len >= ECRYPTFS_MAX_CIPHER_NAME_SIZE); |
| 376 | |
| 377 | strcpy(mount_crypt_stat->global_default_cipher_name, |
| 378 | ECRYPTFS_DEFAULT_CIPHER); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 379 | } |
| 380 | if (!cipher_key_bytes_set) { |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 381 | mount_crypt_stat->global_default_cipher_key_size = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 382 | } |
Eric Sandeen | af440f5 | 2008-02-06 01:38:37 -0800 | [diff] [blame] | 383 | mutex_lock(&key_tfm_list_mutex); |
| 384 | if (!ecryptfs_tfm_exists(mount_crypt_stat->global_default_cipher_name, |
| 385 | NULL)) |
| 386 | rc = ecryptfs_add_new_key_tfm( |
| 387 | NULL, mount_crypt_stat->global_default_cipher_name, |
| 388 | mount_crypt_stat->global_default_cipher_key_size); |
| 389 | mutex_unlock(&key_tfm_list_mutex); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 390 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 391 | printk(KERN_ERR "Error attempting to initialize cipher with " |
Andrew Morton | 81acbcd | 2007-10-16 01:27:59 -0700 | [diff] [blame] | 392 | "name = [%s] and key size = [%td]; rc = [%d]\n", |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 393 | mount_crypt_stat->global_default_cipher_name, |
| 394 | mount_crypt_stat->global_default_cipher_key_size, rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 395 | rc = -EINVAL; |
| 396 | goto out; |
| 397 | } |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 398 | rc = ecryptfs_init_global_auth_toks(mount_crypt_stat); |
| 399 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 400 | printk(KERN_WARNING "One or more global auth toks could not " |
| 401 | "properly register; rc = [%d]\n", rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 402 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 403 | out: |
| 404 | return rc; |
| 405 | } |
| 406 | |
| 407 | struct kmem_cache *ecryptfs_sb_info_cache; |
| 408 | |
| 409 | /** |
| 410 | * ecryptfs_fill_super |
| 411 | * @sb: The ecryptfs super block |
| 412 | * @raw_data: The options passed to mount |
| 413 | * @silent: Not used but required by function prototype |
| 414 | * |
| 415 | * Sets up what we can of the sb, rest is done in ecryptfs_read_super |
| 416 | * |
| 417 | * Returns zero on success; non-zero otherwise |
| 418 | */ |
| 419 | static int |
| 420 | ecryptfs_fill_super(struct super_block *sb, void *raw_data, int silent) |
| 421 | { |
| 422 | int rc = 0; |
| 423 | |
| 424 | /* Released in ecryptfs_put_super() */ |
| 425 | ecryptfs_set_superblock_private(sb, |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 426 | kmem_cache_zalloc(ecryptfs_sb_info_cache, |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 427 | GFP_KERNEL)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 428 | if (!ecryptfs_superblock_to_private(sb)) { |
| 429 | ecryptfs_printk(KERN_WARNING, "Out of memory\n"); |
| 430 | rc = -ENOMEM; |
| 431 | goto out; |
| 432 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 433 | sb->s_op = &ecryptfs_sops; |
| 434 | /* Released through deactivate_super(sb) from get_sb_nodev */ |
| 435 | sb->s_root = d_alloc(NULL, &(const struct qstr) { |
| 436 | .hash = 0,.name = "/",.len = 1}); |
| 437 | if (!sb->s_root) { |
| 438 | ecryptfs_printk(KERN_ERR, "d_alloc failed\n"); |
| 439 | rc = -ENOMEM; |
| 440 | goto out; |
| 441 | } |
| 442 | sb->s_root->d_op = &ecryptfs_dops; |
| 443 | sb->s_root->d_sb = sb; |
| 444 | sb->s_root->d_parent = sb->s_root; |
| 445 | /* Released in d_release when dput(sb->s_root) is called */ |
| 446 | /* through deactivate_super(sb) from get_sb_nodev() */ |
| 447 | ecryptfs_set_dentry_private(sb->s_root, |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 448 | kmem_cache_zalloc(ecryptfs_dentry_info_cache, |
Christoph Lameter | e94b176 | 2006-12-06 20:33:17 -0800 | [diff] [blame] | 449 | GFP_KERNEL)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 450 | if (!ecryptfs_dentry_to_private(sb->s_root)) { |
| 451 | ecryptfs_printk(KERN_ERR, |
| 452 | "dentry_info_cache alloc failed\n"); |
| 453 | rc = -ENOMEM; |
| 454 | goto out; |
| 455 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 456 | rc = 0; |
| 457 | out: |
| 458 | /* Should be able to rely on deactivate_super called from |
| 459 | * get_sb_nodev */ |
| 460 | return rc; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * ecryptfs_read_super |
| 465 | * @sb: The ecryptfs super block |
| 466 | * @dev_name: The path to mount over |
| 467 | * |
| 468 | * Read the super block of the lower filesystem, and use |
| 469 | * ecryptfs_interpose to create our initial inode and super block |
| 470 | * struct. |
| 471 | */ |
| 472 | static int ecryptfs_read_super(struct super_block *sb, const char *dev_name) |
| 473 | { |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 474 | struct path path; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 475 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 476 | |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 477 | rc = kern_path(dev_name, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 478 | if (rc) { |
| 479 | ecryptfs_printk(KERN_WARNING, "path_lookup() failed\n"); |
Michael Halcrow | 65dc814 | 2007-02-28 20:12:57 -0800 | [diff] [blame] | 480 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 481 | } |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 482 | ecryptfs_set_superblock_lower(sb, path.dentry->d_sb); |
| 483 | sb->s_maxbytes = path.dentry->d_sb->s_maxbytes; |
| 484 | sb->s_blocksize = path.dentry->d_sb->s_blocksize; |
| 485 | ecryptfs_set_dentry_lower(sb->s_root, path.dentry); |
| 486 | ecryptfs_set_dentry_lower_mnt(sb->s_root, path.mnt); |
| 487 | rc = ecryptfs_interpose(path.dentry, sb->s_root, sb, 0); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 488 | if (rc) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 489 | goto out_free; |
| 490 | rc = 0; |
| 491 | goto out; |
| 492 | out_free: |
Al Viro | 421748e | 2008-08-02 01:04:36 -0400 | [diff] [blame] | 493 | path_put(&path); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 494 | out: |
| 495 | return rc; |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * ecryptfs_get_sb |
| 500 | * @fs_type |
| 501 | * @flags |
| 502 | * @dev_name: The path to mount over |
| 503 | * @raw_data: The options passed into the kernel |
| 504 | * |
| 505 | * The whole ecryptfs_get_sb process is broken into 4 functions: |
| 506 | * ecryptfs_parse_options(): handle options passed to ecryptfs, if any |
| 507 | * ecryptfs_fill_super(): used by get_sb_nodev, fills out the super_block |
| 508 | * with as much information as it can before needing |
| 509 | * the lower filesystem. |
| 510 | * ecryptfs_read_super(): this accesses the lower filesystem and uses |
| 511 | * ecryptfs_interpolate to perform most of the linking |
| 512 | * ecryptfs_interpolate(): links the lower filesystem into ecryptfs |
| 513 | */ |
| 514 | static int ecryptfs_get_sb(struct file_system_type *fs_type, int flags, |
| 515 | const char *dev_name, void *raw_data, |
| 516 | struct vfsmount *mnt) |
| 517 | { |
| 518 | int rc; |
| 519 | struct super_block *sb; |
| 520 | |
| 521 | rc = get_sb_nodev(fs_type, flags, raw_data, ecryptfs_fill_super, mnt); |
| 522 | if (rc < 0) { |
| 523 | printk(KERN_ERR "Getting sb failed; rc = [%d]\n", rc); |
| 524 | goto out; |
| 525 | } |
| 526 | sb = mnt->mnt_sb; |
| 527 | rc = ecryptfs_parse_options(sb, raw_data); |
| 528 | if (rc) { |
| 529 | printk(KERN_ERR "Error parsing options; rc = [%d]\n", rc); |
| 530 | goto out_abort; |
| 531 | } |
| 532 | rc = ecryptfs_read_super(sb, dev_name); |
| 533 | if (rc) { |
| 534 | printk(KERN_ERR "Reading sb failed; rc = [%d]\n", rc); |
| 535 | goto out_abort; |
| 536 | } |
| 537 | goto out; |
| 538 | out_abort: |
| 539 | dput(sb->s_root); |
| 540 | up_write(&sb->s_umount); |
| 541 | deactivate_super(sb); |
| 542 | out: |
| 543 | return rc; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * ecryptfs_kill_block_super |
| 548 | * @sb: The ecryptfs super block |
| 549 | * |
| 550 | * Used to bring the superblock down and free the private data. |
| 551 | * Private data is free'd in ecryptfs_put_super() |
| 552 | */ |
| 553 | static void ecryptfs_kill_block_super(struct super_block *sb) |
| 554 | { |
| 555 | generic_shutdown_super(sb); |
| 556 | } |
| 557 | |
| 558 | static struct file_system_type ecryptfs_fs_type = { |
| 559 | .owner = THIS_MODULE, |
| 560 | .name = "ecryptfs", |
| 561 | .get_sb = ecryptfs_get_sb, |
| 562 | .kill_sb = ecryptfs_kill_block_super, |
| 563 | .fs_flags = 0 |
| 564 | }; |
| 565 | |
| 566 | /** |
| 567 | * inode_info_init_once |
| 568 | * |
| 569 | * Initializes the ecryptfs_inode_info_cache when it is created |
| 570 | */ |
| 571 | static void |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 572 | inode_info_init_once(void *vptr) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 573 | { |
| 574 | struct ecryptfs_inode_info *ei = (struct ecryptfs_inode_info *)vptr; |
| 575 | |
Christoph Lameter | a35afb8 | 2007-05-16 22:10:57 -0700 | [diff] [blame] | 576 | inode_init_once(&ei->vfs_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | static struct ecryptfs_cache_info { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 580 | struct kmem_cache **cache; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 581 | const char *name; |
| 582 | size_t size; |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 583 | void (*ctor)(void *obj); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 584 | } ecryptfs_cache_infos[] = { |
| 585 | { |
| 586 | .cache = &ecryptfs_auth_tok_list_item_cache, |
| 587 | .name = "ecryptfs_auth_tok_list_item", |
| 588 | .size = sizeof(struct ecryptfs_auth_tok_list_item), |
| 589 | }, |
| 590 | { |
| 591 | .cache = &ecryptfs_file_info_cache, |
| 592 | .name = "ecryptfs_file_cache", |
| 593 | .size = sizeof(struct ecryptfs_file_info), |
| 594 | }, |
| 595 | { |
| 596 | .cache = &ecryptfs_dentry_info_cache, |
| 597 | .name = "ecryptfs_dentry_info_cache", |
| 598 | .size = sizeof(struct ecryptfs_dentry_info), |
| 599 | }, |
| 600 | { |
| 601 | .cache = &ecryptfs_inode_info_cache, |
| 602 | .name = "ecryptfs_inode_cache", |
| 603 | .size = sizeof(struct ecryptfs_inode_info), |
| 604 | .ctor = inode_info_init_once, |
| 605 | }, |
| 606 | { |
| 607 | .cache = &ecryptfs_sb_info_cache, |
| 608 | .name = "ecryptfs_sb_cache", |
| 609 | .size = sizeof(struct ecryptfs_sb_info), |
| 610 | }, |
| 611 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 612 | .cache = &ecryptfs_header_cache_1, |
| 613 | .name = "ecryptfs_headers_1", |
| 614 | .size = PAGE_CACHE_SIZE, |
| 615 | }, |
| 616 | { |
| 617 | .cache = &ecryptfs_header_cache_2, |
| 618 | .name = "ecryptfs_headers_2", |
| 619 | .size = PAGE_CACHE_SIZE, |
| 620 | }, |
| 621 | { |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 622 | .cache = &ecryptfs_xattr_cache, |
| 623 | .name = "ecryptfs_xattr_cache", |
| 624 | .size = PAGE_CACHE_SIZE, |
| 625 | }, |
| 626 | { |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 627 | .cache = &ecryptfs_key_record_cache, |
| 628 | .name = "ecryptfs_key_record_cache", |
| 629 | .size = sizeof(struct ecryptfs_key_record), |
| 630 | }, |
Michael Halcrow | 956159c | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 631 | { |
| 632 | .cache = &ecryptfs_key_sig_cache, |
| 633 | .name = "ecryptfs_key_sig_cache", |
| 634 | .size = sizeof(struct ecryptfs_key_sig), |
| 635 | }, |
| 636 | { |
| 637 | .cache = &ecryptfs_global_auth_tok_cache, |
| 638 | .name = "ecryptfs_global_auth_tok_cache", |
| 639 | .size = sizeof(struct ecryptfs_global_auth_tok), |
| 640 | }, |
| 641 | { |
| 642 | .cache = &ecryptfs_key_tfm_cache, |
| 643 | .name = "ecryptfs_key_tfm_cache", |
| 644 | .size = sizeof(struct ecryptfs_key_tfm), |
| 645 | }, |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 646 | { |
| 647 | .cache = &ecryptfs_open_req_cache, |
| 648 | .name = "ecryptfs_open_req_cache", |
| 649 | .size = sizeof(struct ecryptfs_open_req), |
| 650 | }, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 651 | }; |
| 652 | |
| 653 | static void ecryptfs_free_kmem_caches(void) |
| 654 | { |
| 655 | int i; |
| 656 | |
| 657 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { |
| 658 | struct ecryptfs_cache_info *info; |
| 659 | |
| 660 | info = &ecryptfs_cache_infos[i]; |
| 661 | if (*(info->cache)) |
| 662 | kmem_cache_destroy(*(info->cache)); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | /** |
| 667 | * ecryptfs_init_kmem_caches |
| 668 | * |
| 669 | * Returns zero on success; non-zero otherwise |
| 670 | */ |
| 671 | static int ecryptfs_init_kmem_caches(void) |
| 672 | { |
| 673 | int i; |
| 674 | |
| 675 | for (i = 0; i < ARRAY_SIZE(ecryptfs_cache_infos); i++) { |
| 676 | struct ecryptfs_cache_info *info; |
| 677 | |
| 678 | info = &ecryptfs_cache_infos[i]; |
| 679 | *(info->cache) = kmem_cache_create(info->name, info->size, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 680 | 0, SLAB_HWCACHE_ALIGN, info->ctor); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 681 | if (!*(info->cache)) { |
| 682 | ecryptfs_free_kmem_caches(); |
| 683 | ecryptfs_printk(KERN_WARNING, "%s: " |
| 684 | "kmem_cache_create failed\n", |
| 685 | info->name); |
| 686 | return -ENOMEM; |
| 687 | } |
| 688 | } |
| 689 | return 0; |
| 690 | } |
| 691 | |
Greg Kroah-Hartman | 6e90aa9 | 2007-11-06 15:08:08 -0800 | [diff] [blame] | 692 | static struct kobject *ecryptfs_kobj; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 693 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 694 | static ssize_t version_show(struct kobject *kobj, |
| 695 | struct kobj_attribute *attr, char *buff) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 696 | { |
| 697 | return snprintf(buff, PAGE_SIZE, "%d\n", ECRYPTFS_VERSIONING_MASK); |
| 698 | } |
| 699 | |
Kay Sievers | 386f275 | 2007-11-02 13:47:53 +0100 | [diff] [blame] | 700 | static struct kobj_attribute version_attr = __ATTR_RO(version); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 701 | |
Greg Kroah-Hartman | 30a468b | 2007-10-15 15:01:24 -0700 | [diff] [blame] | 702 | static struct attribute *attributes[] = { |
| 703 | &version_attr.attr, |
Greg Kroah-Hartman | 30a468b | 2007-10-15 15:01:24 -0700 | [diff] [blame] | 704 | NULL, |
| 705 | }; |
| 706 | |
| 707 | static struct attribute_group attr_group = { |
| 708 | .attrs = attributes, |
| 709 | }; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 710 | |
| 711 | static int do_sysfs_registration(void) |
| 712 | { |
| 713 | int rc; |
| 714 | |
Greg Kroah-Hartman | 6e90aa9 | 2007-11-06 15:08:08 -0800 | [diff] [blame] | 715 | ecryptfs_kobj = kobject_create_and_add("ecryptfs", fs_kobj); |
| 716 | if (!ecryptfs_kobj) { |
Greg Kroah-Hartman | 917e865 | 2007-10-29 20:13:17 +0100 | [diff] [blame] | 717 | printk(KERN_ERR "Unable to create ecryptfs kset\n"); |
| 718 | rc = -ENOMEM; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 719 | goto out; |
| 720 | } |
Greg Kroah-Hartman | 6e90aa9 | 2007-11-06 15:08:08 -0800 | [diff] [blame] | 721 | rc = sysfs_create_group(ecryptfs_kobj, &attr_group); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 722 | if (rc) { |
| 723 | printk(KERN_ERR |
Greg Kroah-Hartman | 30a468b | 2007-10-15 15:01:24 -0700 | [diff] [blame] | 724 | "Unable to create ecryptfs version attributes\n"); |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 725 | kobject_put(ecryptfs_kobj); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 726 | } |
| 727 | out: |
| 728 | return rc; |
| 729 | } |
| 730 | |
Ryusuke Konishi | a75de1b | 2007-08-10 13:00:56 -0700 | [diff] [blame] | 731 | static void do_sysfs_unregistration(void) |
| 732 | { |
Greg Kroah-Hartman | 6e90aa9 | 2007-11-06 15:08:08 -0800 | [diff] [blame] | 733 | sysfs_remove_group(ecryptfs_kobj, &attr_group); |
Greg Kroah-Hartman | 197b12d | 2007-12-20 08:13:05 -0800 | [diff] [blame] | 734 | kobject_put(ecryptfs_kobj); |
Ryusuke Konishi | a75de1b | 2007-08-10 13:00:56 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 737 | static int __init ecryptfs_init(void) |
| 738 | { |
| 739 | int rc; |
| 740 | |
| 741 | if (ECRYPTFS_DEFAULT_EXTENT_SIZE > PAGE_CACHE_SIZE) { |
| 742 | rc = -EINVAL; |
| 743 | ecryptfs_printk(KERN_ERR, "The eCryptfs extent size is " |
| 744 | "larger than the host's page size, and so " |
| 745 | "eCryptfs cannot run on this system. The " |
| 746 | "default eCryptfs extent size is [%d] bytes; " |
| 747 | "the page size is [%d] bytes.\n", |
| 748 | ECRYPTFS_DEFAULT_EXTENT_SIZE, PAGE_CACHE_SIZE); |
| 749 | goto out; |
| 750 | } |
| 751 | rc = ecryptfs_init_kmem_caches(); |
| 752 | if (rc) { |
| 753 | printk(KERN_ERR |
| 754 | "Failed to allocate one or more kmem_cache objects\n"); |
| 755 | goto out; |
| 756 | } |
| 757 | rc = register_filesystem(&ecryptfs_fs_type); |
| 758 | if (rc) { |
| 759 | printk(KERN_ERR "Failed to register filesystem\n"); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 760 | goto out_free_kmem_caches; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 761 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 762 | rc = do_sysfs_registration(); |
| 763 | if (rc) { |
| 764 | printk(KERN_ERR "sysfs registration failed\n"); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 765 | goto out_unregister_filesystem; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 766 | } |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 767 | rc = ecryptfs_init_kthread(); |
| 768 | if (rc) { |
| 769 | printk(KERN_ERR "%s: kthread initialization failed; " |
| 770 | "rc = [%d]\n", __func__, rc); |
| 771 | goto out_do_sysfs_unregistration; |
| 772 | } |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 773 | rc = ecryptfs_init_messaging(); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 774 | if (rc) { |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 775 | printk(KERN_ERR "Failure occured while attempting to " |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 776 | "initialize the communications channel to " |
| 777 | "ecryptfsd\n"); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 778 | goto out_destroy_kthread; |
Michael Halcrow | 956159c | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 779 | } |
| 780 | rc = ecryptfs_init_crypto(); |
| 781 | if (rc) { |
| 782 | printk(KERN_ERR "Failure whilst attempting to init crypto; " |
| 783 | "rc = [%d]\n", rc); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 784 | goto out_release_messaging; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 785 | } |
Eric Sandeen | 2830bfd | 2008-02-06 01:38:34 -0800 | [diff] [blame] | 786 | if (ecryptfs_verbosity > 0) |
| 787 | printk(KERN_CRIT "eCryptfs verbosity set to %d. Secret values " |
| 788 | "will be written to the syslog!\n", ecryptfs_verbosity); |
| 789 | |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 790 | goto out; |
| 791 | out_release_messaging: |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 792 | ecryptfs_release_messaging(); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 793 | out_destroy_kthread: |
| 794 | ecryptfs_destroy_kthread(); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 795 | out_do_sysfs_unregistration: |
| 796 | do_sysfs_unregistration(); |
| 797 | out_unregister_filesystem: |
| 798 | unregister_filesystem(&ecryptfs_fs_type); |
| 799 | out_free_kmem_caches: |
| 800 | ecryptfs_free_kmem_caches(); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 801 | out: |
| 802 | return rc; |
| 803 | } |
| 804 | |
| 805 | static void __exit ecryptfs_exit(void) |
| 806 | { |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 807 | int rc; |
| 808 | |
| 809 | rc = ecryptfs_destroy_crypto(); |
| 810 | if (rc) |
| 811 | printk(KERN_ERR "Failure whilst attempting to destroy crypto; " |
| 812 | "rc = [%d]\n", rc); |
Tyler Hicks | 624ae52 | 2008-10-15 22:02:51 -0700 | [diff] [blame] | 813 | ecryptfs_release_messaging(); |
Michael Halcrow | 746f1e5 | 2008-07-23 21:30:02 -0700 | [diff] [blame] | 814 | ecryptfs_destroy_kthread(); |
Michael Halcrow | cf81f89 | 2007-10-16 01:28:07 -0700 | [diff] [blame] | 815 | do_sysfs_unregistration(); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 816 | unregister_filesystem(&ecryptfs_fs_type); |
| 817 | ecryptfs_free_kmem_caches(); |
| 818 | } |
| 819 | |
| 820 | MODULE_AUTHOR("Michael A. Halcrow <mhalcrow@us.ibm.com>"); |
| 821 | MODULE_DESCRIPTION("eCryptfs"); |
| 822 | |
| 823 | MODULE_LICENSE("GPL"); |
| 824 | |
| 825 | module_init(ecryptfs_init) |
| 826 | module_exit(ecryptfs_exit) |