1237fead6SMichael Halcrow /** 2237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 3237fead6SMichael Halcrow * 4237fead6SMichael Halcrow * Copyright (C) 1997-2003 Erez Zadok 5237fead6SMichael Halcrow * Copyright (C) 2001-2003 Stony Brook University 6237fead6SMichael Halcrow * Copyright (C) 2004-2006 International Business Machines Corp. 7237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> 8237fead6SMichael Halcrow * Michael C. Thompson <mcthomps@us.ibm.com> 9237fead6SMichael Halcrow * 10237fead6SMichael Halcrow * This program is free software; you can redistribute it and/or 11237fead6SMichael Halcrow * modify it under the terms of the GNU General Public License as 12237fead6SMichael Halcrow * published by the Free Software Foundation; either version 2 of the 13237fead6SMichael Halcrow * License, or (at your option) any later version. 14237fead6SMichael Halcrow * 15237fead6SMichael Halcrow * This program is distributed in the hope that it will be useful, but 16237fead6SMichael Halcrow * WITHOUT ANY WARRANTY; without even the implied warranty of 17237fead6SMichael Halcrow * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18237fead6SMichael Halcrow * General Public License for more details. 19237fead6SMichael Halcrow * 20237fead6SMichael Halcrow * You should have received a copy of the GNU General Public License 21237fead6SMichael Halcrow * along with this program; if not, write to the Free Software 22237fead6SMichael Halcrow * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 23237fead6SMichael Halcrow * 02111-1307, USA. 24237fead6SMichael Halcrow */ 25237fead6SMichael Halcrow 26237fead6SMichael Halcrow #include <linux/fs.h> 27237fead6SMichael Halcrow #include <linux/mount.h> 28237fead6SMichael Halcrow #include <linux/key.h> 295a0e3ad6STejun Heo #include <linux/slab.h> 30237fead6SMichael Halcrow #include <linux/seq_file.h> 314981e081SMichael Halcrow #include <linux/file.h> 324a26620dSTyler Hicks #include <linux/statfs.h> 334a26620dSTyler Hicks #include <linux/magic.h> 34237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 35237fead6SMichael Halcrow 36237fead6SMichael Halcrow struct kmem_cache *ecryptfs_inode_info_cache; 37237fead6SMichael Halcrow 38237fead6SMichael Halcrow /** 39237fead6SMichael Halcrow * ecryptfs_alloc_inode - allocate an ecryptfs inode 40237fead6SMichael Halcrow * @sb: Pointer to the ecryptfs super block 41237fead6SMichael Halcrow * 42237fead6SMichael Halcrow * Called to bring an inode into existence. 43237fead6SMichael Halcrow * 44237fead6SMichael Halcrow * Only handle allocation, setting up structures should be done in 45237fead6SMichael Halcrow * ecryptfs_read_inode. This is because the kernel, between now and 46237fead6SMichael Halcrow * then, will 0 out the private data pointer. 47237fead6SMichael Halcrow * 48237fead6SMichael Halcrow * Returns a pointer to a newly allocated inode, NULL otherwise 49237fead6SMichael Halcrow */ 50237fead6SMichael Halcrow static struct inode *ecryptfs_alloc_inode(struct super_block *sb) 51237fead6SMichael Halcrow { 52035241d3SMichael Halcrow struct ecryptfs_inode_info *inode_info; 53237fead6SMichael Halcrow struct inode *inode = NULL; 54237fead6SMichael Halcrow 55035241d3SMichael Halcrow inode_info = kmem_cache_alloc(ecryptfs_inode_info_cache, GFP_KERNEL); 56035241d3SMichael Halcrow if (unlikely(!inode_info)) 57237fead6SMichael Halcrow goto out; 58e81f3340SHerbert Xu if (ecryptfs_init_crypt_stat(&inode_info->crypt_stat)) { 59e81f3340SHerbert Xu kmem_cache_free(ecryptfs_inode_info_cache, inode_info); 60e81f3340SHerbert Xu goto out; 61e81f3340SHerbert Xu } 62332ab16fSTyler Hicks mutex_init(&inode_info->lower_file_mutex); 63332ab16fSTyler Hicks atomic_set(&inode_info->lower_file_count, 0); 64035241d3SMichael Halcrow inode_info->lower_file = NULL; 65035241d3SMichael Halcrow inode = &inode_info->vfs_inode; 66237fead6SMichael Halcrow out: 67237fead6SMichael Halcrow return inode; 68237fead6SMichael Halcrow } 69237fead6SMichael Halcrow 70586a94fdSAl Viro static void ecryptfs_free_inode(struct inode *inode) 71fa0d7e3dSNick Piggin { 72fa0d7e3dSNick Piggin struct ecryptfs_inode_info *inode_info; 73fa0d7e3dSNick Piggin inode_info = ecryptfs_inode_to_private(inode); 74fa0d7e3dSNick Piggin 75fa0d7e3dSNick Piggin kmem_cache_free(ecryptfs_inode_info_cache, inode_info); 76fa0d7e3dSNick Piggin } 77fa0d7e3dSNick Piggin 78237fead6SMichael Halcrow /** 79237fead6SMichael Halcrow * ecryptfs_destroy_inode 80237fead6SMichael Halcrow * @inode: The ecryptfs inode 81237fead6SMichael Halcrow * 824981e081SMichael Halcrow * This is used during the final destruction of the inode. All 834981e081SMichael Halcrow * allocation of memory related to the inode, including allocated 84332ab16fSTyler Hicks * memory in the crypt_stat struct, will be released here. 85237fead6SMichael Halcrow * There should be no chance that this deallocation will be missed. 86237fead6SMichael Halcrow */ 87237fead6SMichael Halcrow static void ecryptfs_destroy_inode(struct inode *inode) 88237fead6SMichael Halcrow { 89237fead6SMichael Halcrow struct ecryptfs_inode_info *inode_info; 90237fead6SMichael Halcrow 91237fead6SMichael Halcrow inode_info = ecryptfs_inode_to_private(inode); 92332ab16fSTyler Hicks BUG_ON(inode_info->lower_file); 93fcd12835SMichael Halcrow ecryptfs_destroy_crypt_stat(&inode_info->crypt_stat); 94237fead6SMichael Halcrow } 95237fead6SMichael Halcrow 96237fead6SMichael Halcrow /** 97237fead6SMichael Halcrow * ecryptfs_statfs 98237fead6SMichael Halcrow * @sb: The ecryptfs super block 99237fead6SMichael Halcrow * @buf: The struct kstatfs to fill in with stats 100237fead6SMichael Halcrow * 101237fead6SMichael Halcrow * Get the filesystem statistics. Currently, we let this pass right through 102237fead6SMichael Halcrow * to the lower filesystem and take no action ourselves. 103237fead6SMichael Halcrow */ 104237fead6SMichael Halcrow static int ecryptfs_statfs(struct dentry *dentry, struct kstatfs *buf) 105237fead6SMichael Halcrow { 106ebabe9a9SChristoph Hellwig struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); 1074a26620dSTyler Hicks int rc; 108ebabe9a9SChristoph Hellwig 109ebabe9a9SChristoph Hellwig if (!lower_dentry->d_sb->s_op->statfs) 110ebabe9a9SChristoph Hellwig return -ENOSYS; 1114a26620dSTyler Hicks 1124a26620dSTyler Hicks rc = lower_dentry->d_sb->s_op->statfs(lower_dentry, buf); 1134a26620dSTyler Hicks if (rc) 1144a26620dSTyler Hicks return rc; 1154a26620dSTyler Hicks 1164a26620dSTyler Hicks buf->f_type = ECRYPTFS_SUPER_MAGIC; 1174a26620dSTyler Hicks rc = ecryptfs_set_f_namelen(&buf->f_namelen, buf->f_namelen, 1184a26620dSTyler Hicks &ecryptfs_superblock_to_private(dentry->d_sb)->mount_crypt_stat); 1194a26620dSTyler Hicks 1204a26620dSTyler Hicks return rc; 121237fead6SMichael Halcrow } 122237fead6SMichael Halcrow 123237fead6SMichael Halcrow /** 124b57922d9SAl Viro * ecryptfs_evict_inode 125237fead6SMichael Halcrow * @inode - The ecryptfs inode 126237fead6SMichael Halcrow * 127237fead6SMichael Halcrow * Called by iput() when the inode reference count reached zero 128237fead6SMichael Halcrow * and the inode is not hashed anywhere. Used to clear anything 129237fead6SMichael Halcrow * that needs to be, before the inode is completely destroyed and put 130237fead6SMichael Halcrow * on the inode free list. We use this to drop out reference to the 131237fead6SMichael Halcrow * lower inode. 132237fead6SMichael Halcrow */ 133b57922d9SAl Viro static void ecryptfs_evict_inode(struct inode *inode) 134237fead6SMichael Halcrow { 13591b0abe3SJohannes Weiner truncate_inode_pages_final(&inode->i_data); 136dbd5768fSJan Kara clear_inode(inode); 137237fead6SMichael Halcrow iput(ecryptfs_inode_to_lower(inode)); 138237fead6SMichael Halcrow } 139237fead6SMichael Halcrow 140237fead6SMichael Halcrow /** 141237fead6SMichael Halcrow * ecryptfs_show_options 142237fead6SMichael Halcrow * 14399db6e4aSEric Sandeen * Prints the mount options for a given superblock. 14499db6e4aSEric Sandeen * Returns zero; does not fail. 145237fead6SMichael Halcrow */ 14634c80b1dSAl Viro static int ecryptfs_show_options(struct seq_file *m, struct dentry *root) 147237fead6SMichael Halcrow { 14834c80b1dSAl Viro struct super_block *sb = root->d_sb; 14999db6e4aSEric Sandeen struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 15099db6e4aSEric Sandeen &ecryptfs_superblock_to_private(sb)->mount_crypt_stat; 15199db6e4aSEric Sandeen struct ecryptfs_global_auth_tok *walker; 152237fead6SMichael Halcrow 15399db6e4aSEric Sandeen mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 15499db6e4aSEric Sandeen list_for_each_entry(walker, 15599db6e4aSEric Sandeen &mount_crypt_stat->global_auth_tok_list, 15699db6e4aSEric Sandeen mount_crypt_stat_list) { 1573a5203abSTyler Hicks if (walker->flags & ECRYPTFS_AUTH_TOK_FNEK) 1583a5203abSTyler Hicks seq_printf(m, ",ecryptfs_fnek_sig=%s", walker->sig); 1593a5203abSTyler Hicks else 16099db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_sig=%s", walker->sig); 161237fead6SMichael Halcrow } 16299db6e4aSEric Sandeen mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 16399db6e4aSEric Sandeen 16499db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_cipher=%s", 16599db6e4aSEric Sandeen mount_crypt_stat->global_default_cipher_name); 16699db6e4aSEric Sandeen 16799db6e4aSEric Sandeen if (mount_crypt_stat->global_default_cipher_key_size) 16899db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_key_bytes=%zd", 16999db6e4aSEric Sandeen mount_crypt_stat->global_default_cipher_key_size); 17099db6e4aSEric Sandeen if (mount_crypt_stat->flags & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED) 17199db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_passthrough"); 17299db6e4aSEric Sandeen if (mount_crypt_stat->flags & ECRYPTFS_XATTR_METADATA_ENABLED) 17399db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_xattr_metadata"); 17499db6e4aSEric Sandeen if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) 17599db6e4aSEric Sandeen seq_printf(m, ",ecryptfs_encrypted_view"); 176e77cc8d2STyler Hicks if (mount_crypt_stat->flags & ECRYPTFS_UNLINK_SIGS) 177e77cc8d2STyler Hicks seq_printf(m, ",ecryptfs_unlink_sigs"); 1788747f954STyler Hicks if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY) 1798747f954STyler Hicks seq_printf(m, ",ecryptfs_mount_auth_tok_only"); 18099db6e4aSEric Sandeen 18199db6e4aSEric Sandeen return 0; 182237fead6SMichael Halcrow } 183237fead6SMichael Halcrow 184ee9b6d61SJosef 'Jeff' Sipek const struct super_operations ecryptfs_sops = { 185237fead6SMichael Halcrow .alloc_inode = ecryptfs_alloc_inode, 186237fead6SMichael Halcrow .destroy_inode = ecryptfs_destroy_inode, 187586a94fdSAl Viro .free_inode = ecryptfs_free_inode, 188237fead6SMichael Halcrow .statfs = ecryptfs_statfs, 189237fead6SMichael Halcrow .remount_fs = NULL, 190b57922d9SAl Viro .evict_inode = ecryptfs_evict_inode, 191237fead6SMichael Halcrow .show_options = ecryptfs_show_options 192237fead6SMichael Halcrow }; 193