Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1 | /** |
| 2 | * eCryptfs: Linux filesystem encryption layer |
| 3 | * |
| 4 | * Copyright (C) 1997-2004 Erez Zadok |
| 5 | * Copyright (C) 2001-2004 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. Thompsion <mcthomps@us.ibm.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of the |
| 13 | * License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, but |
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 23 | * 02111-1307, USA. |
| 24 | */ |
| 25 | |
| 26 | #include <linux/file.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include <linux/pagemap.h> |
| 29 | #include <linux/dcache.h> |
| 30 | #include <linux/namei.h> |
| 31 | #include <linux/mount.h> |
| 32 | #include <linux/crypto.h> |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 33 | #include <linux/fs_stack.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include <linux/slab.h> |
Roberto Sassu | 48b512e | 2010-10-05 18:53:45 +0200 | [diff] [blame] | 35 | #include <linux/xattr.h> |
Harvey Harrison | 0a688ad | 2008-07-23 21:30:07 -0700 | [diff] [blame] | 36 | #include <asm/unaligned.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 37 | #include "ecryptfs_kernel.h" |
| 38 | |
| 39 | static struct dentry *lock_parent(struct dentry *dentry) |
| 40 | { |
| 41 | struct dentry *dir; |
| 42 | |
Miklos Szeredi | 8dc4e37 | 2008-05-12 14:02:04 -0700 | [diff] [blame] | 43 | dir = dget_parent(dentry); |
Peter Zijlstra | 908e0a8 | 2007-03-07 20:41:30 -0800 | [diff] [blame] | 44 | mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 45 | return dir; |
| 46 | } |
| 47 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 48 | static void unlock_dir(struct dentry *dir) |
| 49 | { |
| 50 | mutex_unlock(&dir->d_inode->i_mutex); |
| 51 | dput(dir); |
| 52 | } |
| 53 | |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 54 | static int ecryptfs_inode_test(struct inode *inode, void *lower_inode) |
| 55 | { |
| 56 | if (ecryptfs_inode_to_lower(inode) == (struct inode *)lower_inode) |
| 57 | return 1; |
| 58 | return 0; |
| 59 | } |
| 60 | |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 61 | static int ecryptfs_inode_set(struct inode *inode, void *opaque) |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 62 | { |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 63 | struct inode *lower_inode = opaque; |
| 64 | |
| 65 | ecryptfs_set_inode_lower(inode, lower_inode); |
| 66 | fsstack_copy_attr_all(inode, lower_inode); |
| 67 | /* i_size will be overwritten for encrypted regular files */ |
| 68 | fsstack_copy_inode_size(inode, lower_inode); |
| 69 | inode->i_ino = lower_inode->i_ino; |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 70 | inode->i_version++; |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 71 | inode->i_mapping->a_ops = &ecryptfs_aops; |
Thieu Le | a21353b | 2011-07-26 16:15:10 -0700 | [diff] [blame] | 72 | inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi; |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 73 | |
| 74 | if (S_ISLNK(inode->i_mode)) |
| 75 | inode->i_op = &ecryptfs_symlink_iops; |
| 76 | else if (S_ISDIR(inode->i_mode)) |
| 77 | inode->i_op = &ecryptfs_dir_iops; |
| 78 | else |
| 79 | inode->i_op = &ecryptfs_main_iops; |
| 80 | |
| 81 | if (S_ISDIR(inode->i_mode)) |
| 82 | inode->i_fop = &ecryptfs_dir_fops; |
| 83 | else if (special_file(inode->i_mode)) |
| 84 | init_special_inode(inode, inode->i_mode, inode->i_rdev); |
| 85 | else |
| 86 | inode->i_fop = &ecryptfs_main_fops; |
| 87 | |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 91 | static struct inode *__ecryptfs_get_inode(struct inode *lower_inode, |
| 92 | struct super_block *sb) |
| 93 | { |
| 94 | struct inode *inode; |
| 95 | |
| 96 | if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb)) |
| 97 | return ERR_PTR(-EXDEV); |
| 98 | if (!igrab(lower_inode)) |
| 99 | return ERR_PTR(-ESTALE); |
| 100 | inode = iget5_locked(sb, (unsigned long)lower_inode, |
| 101 | ecryptfs_inode_test, ecryptfs_inode_set, |
| 102 | lower_inode); |
| 103 | if (!inode) { |
| 104 | iput(lower_inode); |
| 105 | return ERR_PTR(-EACCES); |
| 106 | } |
| 107 | if (!(inode->i_state & I_NEW)) |
| 108 | iput(lower_inode); |
| 109 | |
| 110 | return inode; |
| 111 | } |
| 112 | |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 113 | struct inode *ecryptfs_get_inode(struct inode *lower_inode, |
| 114 | struct super_block *sb) |
| 115 | { |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 116 | struct inode *inode = __ecryptfs_get_inode(lower_inode, sb); |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 117 | |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 118 | if (!IS_ERR(inode) && (inode->i_state & I_NEW)) |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 119 | unlock_new_inode(inode); |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 120 | |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 121 | return inode; |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 124 | /** |
| 125 | * ecryptfs_interpose |
| 126 | * @lower_dentry: Existing dentry in the lower filesystem |
| 127 | * @dentry: ecryptfs' dentry |
| 128 | * @sb: ecryptfs's super_block |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 129 | * |
| 130 | * Interposes upper and lower dentries. |
| 131 | * |
| 132 | * Returns zero on success; non-zero otherwise |
| 133 | */ |
| 134 | static int ecryptfs_interpose(struct dentry *lower_dentry, |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 135 | struct dentry *dentry, struct super_block *sb) |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 136 | { |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 137 | struct inode *inode = ecryptfs_get_inode(lower_dentry->d_inode, sb); |
| 138 | |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 139 | if (IS_ERR(inode)) |
| 140 | return PTR_ERR(inode); |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 141 | d_instantiate(dentry, inode); |
| 142 | |
Tyler Hicks | c4f7907 | 2011-05-23 21:18:20 -0500 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 146 | /** |
| 147 | * ecryptfs_create_underlying_file |
| 148 | * @lower_dir_inode: inode of the parent in the lower fs of the new file |
Qinghuang Feng | f70f582 | 2009-01-06 14:42:05 -0800 | [diff] [blame] | 149 | * @dentry: New file's dentry |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 150 | * @mode: The mode of the new file |
| 151 | * @nd: nameidata of ecryptfs' parent's dentry & vfsmount |
| 152 | * |
| 153 | * Creates the file in the lower file system. |
| 154 | * |
| 155 | * Returns zero on success; non-zero on error condition |
| 156 | */ |
| 157 | static int |
| 158 | ecryptfs_create_underlying_file(struct inode *lower_dir_inode, |
| 159 | struct dentry *dentry, int mode, |
| 160 | struct nameidata *nd) |
| 161 | { |
| 162 | struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 163 | struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry); |
| 164 | struct dentry *dentry_save; |
| 165 | struct vfsmount *vfsmount_save; |
Tyler Hicks | 2e21b3f | 2010-09-23 02:35:04 -0500 | [diff] [blame] | 166 | unsigned int flags_save; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 167 | int rc; |
| 168 | |
Tyler Hicks | 70b8902 | 2011-02-17 17:35:20 -0600 | [diff] [blame] | 169 | if (nd) { |
| 170 | dentry_save = nd->path.dentry; |
| 171 | vfsmount_save = nd->path.mnt; |
| 172 | flags_save = nd->flags; |
| 173 | nd->path.dentry = lower_dentry; |
| 174 | nd->path.mnt = lower_mnt; |
| 175 | nd->flags &= ~LOOKUP_OPEN; |
| 176 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 177 | rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd); |
Tyler Hicks | 70b8902 | 2011-02-17 17:35:20 -0600 | [diff] [blame] | 178 | if (nd) { |
| 179 | nd->path.dentry = dentry_save; |
| 180 | nd->path.mnt = vfsmount_save; |
| 181 | nd->flags = flags_save; |
| 182 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 183 | return rc; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * ecryptfs_do_create |
| 188 | * @directory_inode: inode of the new file's dentry's parent in ecryptfs |
| 189 | * @ecryptfs_dentry: New file's dentry in ecryptfs |
| 190 | * @mode: The mode of the new file |
| 191 | * @nd: nameidata of ecryptfs' parent's dentry & vfsmount |
| 192 | * |
| 193 | * Creates the underlying file and the eCryptfs inode which will link to |
| 194 | * it. It will also update the eCryptfs directory inode to mimic the |
| 195 | * stat of the lower directory inode. |
| 196 | * |
| 197 | * Returns zero on success; non-zero on error condition |
| 198 | */ |
| 199 | static int |
| 200 | ecryptfs_do_create(struct inode *directory_inode, |
| 201 | struct dentry *ecryptfs_dentry, int mode, |
| 202 | struct nameidata *nd) |
| 203 | { |
| 204 | int rc; |
| 205 | struct dentry *lower_dentry; |
| 206 | struct dentry *lower_dir_dentry; |
| 207 | |
| 208 | lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry); |
| 209 | lower_dir_dentry = lock_parent(lower_dentry); |
Hirofumi Nakagawa | 801678c | 2008-04-29 01:03:09 -0700 | [diff] [blame] | 210 | if (IS_ERR(lower_dir_dentry)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 211 | ecryptfs_printk(KERN_ERR, "Error locking directory of " |
| 212 | "dentry\n"); |
| 213 | rc = PTR_ERR(lower_dir_dentry); |
| 214 | goto out; |
| 215 | } |
| 216 | rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode, |
| 217 | ecryptfs_dentry, mode, nd); |
Michael Halcrow | 4981e08 | 2007-10-16 01:28:09 -0700 | [diff] [blame] | 218 | if (rc) { |
Michael Halcrow | caeeeec | 2008-01-08 15:33:02 -0800 | [diff] [blame] | 219 | printk(KERN_ERR "%s: Failure to create dentry in lower fs; " |
Harvey Harrison | 18d1dbf | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 220 | "rc = [%d]\n", __func__, rc); |
Michael Halcrow | caeeeec | 2008-01-08 15:33:02 -0800 | [diff] [blame] | 221 | goto out_lock; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 222 | } |
| 223 | rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry, |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 224 | directory_inode->i_sb); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 225 | if (rc) { |
| 226 | ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n"); |
| 227 | goto out_lock; |
| 228 | } |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 229 | fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode); |
| 230 | fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 231 | out_lock: |
| 232 | unlock_dir(lower_dir_dentry); |
| 233 | out: |
| 234 | return rc; |
| 235 | } |
| 236 | |
| 237 | /** |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 238 | * ecryptfs_initialize_file |
| 239 | * |
| 240 | * Cause the file to be changed from a basic empty file to an ecryptfs |
| 241 | * file with a header and first data page. |
| 242 | * |
| 243 | * Returns zero on success |
| 244 | */ |
| 245 | static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry) |
| 246 | { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 247 | struct ecryptfs_crypt_stat *crypt_stat = |
| 248 | &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 249 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 250 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 251 | if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) { |
| 252 | ecryptfs_printk(KERN_DEBUG, "This is a directory\n"); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 253 | crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 254 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 255 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 256 | ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n"); |
| 257 | rc = ecryptfs_new_file_context(ecryptfs_dentry); |
| 258 | if (rc) { |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 259 | ecryptfs_printk(KERN_ERR, "Error creating new file " |
| 260 | "context; rc = [%d]\n", rc); |
| 261 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 262 | } |
Tyler Hicks | 3b06b3e | 2011-05-24 03:49:02 -0500 | [diff] [blame] | 263 | rc = ecryptfs_get_lower_file(ecryptfs_dentry, |
| 264 | ecryptfs_dentry->d_inode); |
Roberto Sassu | 2799289 | 2010-11-03 11:11:28 +0100 | [diff] [blame] | 265 | if (rc) { |
| 266 | printk(KERN_ERR "%s: Error attempting to initialize " |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 267 | "the lower file for the dentry with name " |
Roberto Sassu | 2799289 | 2010-11-03 11:11:28 +0100 | [diff] [blame] | 268 | "[%s]; rc = [%d]\n", __func__, |
| 269 | ecryptfs_dentry->d_name.name, rc); |
| 270 | goto out; |
Michael Halcrow | 391b52f | 2008-07-23 21:30:08 -0700 | [diff] [blame] | 271 | } |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 272 | rc = ecryptfs_write_metadata(ecryptfs_dentry); |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 273 | if (rc) |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 274 | printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc); |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 275 | ecryptfs_put_lower_file(ecryptfs_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 276 | out: |
| 277 | return rc; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * ecryptfs_create |
| 282 | * @dir: The inode of the directory in which to create the file. |
| 283 | * @dentry: The eCryptfs dentry |
| 284 | * @mode: The mode of the new file. |
| 285 | * @nd: nameidata |
| 286 | * |
| 287 | * Creates a new file. |
| 288 | * |
| 289 | * Returns zero on success; non-zero on error condition |
| 290 | */ |
| 291 | static int |
| 292 | ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry, |
| 293 | int mode, struct nameidata *nd) |
| 294 | { |
| 295 | int rc; |
| 296 | |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 297 | /* ecryptfs_do_create() calls ecryptfs_interpose() */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 298 | rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd); |
| 299 | if (unlikely(rc)) { |
| 300 | ecryptfs_printk(KERN_WARNING, "Failed to create file in" |
| 301 | "lower filesystem\n"); |
| 302 | goto out; |
| 303 | } |
| 304 | /* At this point, a file exists on "disk"; we need to make sure |
| 305 | * that this on disk file is prepared to be an ecryptfs file */ |
| 306 | rc = ecryptfs_initialize_file(ecryptfs_dentry); |
| 307 | out: |
| 308 | return rc; |
| 309 | } |
| 310 | |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 311 | static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 312 | { |
Tyler Hicks | 2aac0cf | 2009-03-20 02:23:57 -0500 | [diff] [blame] | 313 | struct ecryptfs_crypt_stat *crypt_stat; |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 314 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 315 | |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 316 | rc = ecryptfs_get_lower_file(dentry, inode); |
Roberto Sassu | 2799289 | 2010-11-03 11:11:28 +0100 | [diff] [blame] | 317 | if (rc) { |
| 318 | printk(KERN_ERR "%s: Error attempting to initialize " |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 319 | "the lower file for the dentry with name " |
Roberto Sassu | 2799289 | 2010-11-03 11:11:28 +0100 | [diff] [blame] | 320 | "[%s]; rc = [%d]\n", __func__, |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 321 | dentry->d_name.name, rc); |
| 322 | return rc; |
Michael Halcrow | 391b52f | 2008-07-23 21:30:08 -0700 | [diff] [blame] | 323 | } |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 324 | |
Tyler Hicks | 3b06b3e | 2011-05-24 03:49:02 -0500 | [diff] [blame] | 325 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; |
Tyler Hicks | 2aac0cf | 2009-03-20 02:23:57 -0500 | [diff] [blame] | 326 | /* TODO: lock for crypt_stat comparison */ |
| 327 | if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)) |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 328 | ecryptfs_set_default_sizes(crypt_stat); |
| 329 | |
| 330 | rc = ecryptfs_read_and_validate_header_region(inode); |
| 331 | ecryptfs_put_lower_file(inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 332 | if (rc) { |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 333 | rc = ecryptfs_read_and_validate_xattr_region(dentry, inode); |
| 334 | if (!rc) |
| 335 | crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 336 | } |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 337 | |
| 338 | /* Must return 0 to allow non-eCryptfs files to be looked up, too */ |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * ecryptfs_lookup_interpose - Dentry interposition for a lookup |
| 344 | */ |
| 345 | static int ecryptfs_lookup_interpose(struct dentry *dentry, |
| 346 | struct dentry *lower_dentry, |
| 347 | struct inode *dir_inode) |
| 348 | { |
| 349 | struct inode *inode, *lower_inode = lower_dentry->d_inode; |
| 350 | struct ecryptfs_dentry_info *dentry_info; |
| 351 | struct vfsmount *lower_mnt; |
| 352 | int rc = 0; |
| 353 | |
| 354 | lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent)); |
| 355 | fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode); |
| 356 | BUG_ON(!lower_dentry->d_count); |
| 357 | |
| 358 | dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL); |
| 359 | ecryptfs_set_dentry_private(dentry, dentry_info); |
| 360 | if (!dentry_info) { |
| 361 | printk(KERN_ERR "%s: Out of memory whilst attempting " |
| 362 | "to allocate ecryptfs_dentry_info struct\n", |
| 363 | __func__); |
| 364 | dput(lower_dentry); |
| 365 | mntput(lower_mnt); |
| 366 | d_drop(dentry); |
| 367 | return -ENOMEM; |
| 368 | } |
| 369 | ecryptfs_set_dentry_lower(dentry, lower_dentry); |
| 370 | ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt); |
| 371 | |
| 372 | if (!lower_dentry->d_inode) { |
| 373 | /* We want to add because we couldn't find in lower */ |
| 374 | d_add(dentry, NULL); |
| 375 | return 0; |
| 376 | } |
| 377 | inode = __ecryptfs_get_inode(lower_inode, dir_inode->i_sb); |
| 378 | if (IS_ERR(inode)) { |
| 379 | printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n", |
| 380 | __func__, PTR_ERR(inode)); |
| 381 | return PTR_ERR(inode); |
| 382 | } |
| 383 | if (S_ISREG(inode->i_mode)) { |
| 384 | rc = ecryptfs_i_size_read(dentry, inode); |
| 385 | if (rc) { |
| 386 | make_bad_inode(inode); |
| 387 | return rc; |
| 388 | } |
| 389 | } |
| 390 | |
Tyler Hicks | 3b06b3e | 2011-05-24 03:49:02 -0500 | [diff] [blame] | 391 | if (inode->i_state & I_NEW) |
| 392 | unlock_new_inode(inode); |
Tyler Hicks | 778aeb4 | 2011-05-24 04:56:23 -0500 | [diff] [blame] | 393 | d_add(dentry, inode); |
| 394 | |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 395 | return rc; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * ecryptfs_lookup |
| 400 | * @ecryptfs_dir_inode: The eCryptfs directory inode |
| 401 | * @ecryptfs_dentry: The eCryptfs dentry that we are looking up |
| 402 | * @ecryptfs_nd: nameidata; may be NULL |
| 403 | * |
| 404 | * Find a file on disk. If the file does not exist, then we'll add it to the |
| 405 | * dentry cache and continue on to read it from the disk. |
| 406 | */ |
| 407 | static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode, |
| 408 | struct dentry *ecryptfs_dentry, |
| 409 | struct nameidata *ecryptfs_nd) |
| 410 | { |
| 411 | char *encrypted_and_encoded_name = NULL; |
Michael Halcrow | a8f1286 | 2009-01-06 14:42:03 -0800 | [diff] [blame] | 412 | size_t encrypted_and_encoded_name_size; |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 413 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL; |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 414 | struct dentry *lower_dir_dentry, *lower_dentry; |
| 415 | int rc = 0; |
| 416 | |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 417 | if ((ecryptfs_dentry->d_name.len == 1 |
| 418 | && !strcmp(ecryptfs_dentry->d_name.name, ".")) |
| 419 | || (ecryptfs_dentry->d_name.len == 2 |
| 420 | && !strcmp(ecryptfs_dentry->d_name.name, ".."))) { |
| 421 | goto out_d_drop; |
| 422 | } |
| 423 | lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent); |
Tyler Hicks | 8787c7a | 2011-02-17 18:51:24 -0600 | [diff] [blame] | 424 | mutex_lock(&lower_dir_dentry->d_inode->i_mutex); |
| 425 | lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name, |
| 426 | lower_dir_dentry, |
| 427 | ecryptfs_dentry->d_name.len); |
| 428 | mutex_unlock(&lower_dir_dentry->d_inode->i_mutex); |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 429 | if (IS_ERR(lower_dentry)) { |
| 430 | rc = PTR_ERR(lower_dentry); |
Tyler Hicks | 8787c7a | 2011-02-17 18:51:24 -0600 | [diff] [blame] | 431 | ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned " |
Tyler Hicks | 9f37622 | 2010-03-25 11:16:56 -0500 | [diff] [blame] | 432 | "[%d] on lower_dentry = [%s]\n", __func__, rc, |
| 433 | encrypted_and_encoded_name); |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 434 | goto out_d_drop; |
| 435 | } |
| 436 | if (lower_dentry->d_inode) |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 437 | goto interpose; |
Tyler Hicks | 2aac0cf | 2009-03-20 02:23:57 -0500 | [diff] [blame] | 438 | mount_crypt_stat = &ecryptfs_superblock_to_private( |
| 439 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
| 440 | if (!(mount_crypt_stat |
| 441 | && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES))) |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 442 | goto interpose; |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 443 | dput(lower_dentry); |
| 444 | rc = ecryptfs_encrypt_and_encode_filename( |
| 445 | &encrypted_and_encoded_name, &encrypted_and_encoded_name_size, |
Tyler Hicks | 2aac0cf | 2009-03-20 02:23:57 -0500 | [diff] [blame] | 446 | NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name, |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 447 | ecryptfs_dentry->d_name.len); |
| 448 | if (rc) { |
| 449 | printk(KERN_ERR "%s: Error attempting to encrypt and encode " |
| 450 | "filename; rc = [%d]\n", __func__, rc); |
| 451 | goto out_d_drop; |
| 452 | } |
Tyler Hicks | 8787c7a | 2011-02-17 18:51:24 -0600 | [diff] [blame] | 453 | mutex_lock(&lower_dir_dentry->d_inode->i_mutex); |
| 454 | lower_dentry = lookup_one_len(encrypted_and_encoded_name, |
| 455 | lower_dir_dentry, |
| 456 | encrypted_and_encoded_name_size); |
| 457 | mutex_unlock(&lower_dir_dentry->d_inode->i_mutex); |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 458 | if (IS_ERR(lower_dentry)) { |
| 459 | rc = PTR_ERR(lower_dentry); |
Tyler Hicks | 8787c7a | 2011-02-17 18:51:24 -0600 | [diff] [blame] | 460 | ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned " |
Tyler Hicks | 9f37622 | 2010-03-25 11:16:56 -0500 | [diff] [blame] | 461 | "[%d] on lower_dentry = [%s]\n", __func__, rc, |
| 462 | encrypted_and_encoded_name); |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 463 | goto out_d_drop; |
| 464 | } |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 465 | interpose: |
| 466 | rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry, |
| 467 | ecryptfs_dir_inode); |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 468 | goto out; |
| 469 | out_d_drop: |
| 470 | d_drop(ecryptfs_dentry); |
| 471 | out: |
| 472 | kfree(encrypted_and_encoded_name); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 473 | return ERR_PTR(rc); |
| 474 | } |
| 475 | |
| 476 | static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir, |
| 477 | struct dentry *new_dentry) |
| 478 | { |
| 479 | struct dentry *lower_old_dentry; |
| 480 | struct dentry *lower_new_dentry; |
| 481 | struct dentry *lower_dir_dentry; |
| 482 | u64 file_size_save; |
| 483 | int rc; |
| 484 | |
| 485 | file_size_save = i_size_read(old_dentry->d_inode); |
| 486 | lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry); |
| 487 | lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry); |
| 488 | dget(lower_old_dentry); |
| 489 | dget(lower_new_dentry); |
| 490 | lower_dir_dentry = lock_parent(lower_new_dentry); |
| 491 | rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode, |
| 492 | lower_new_dentry); |
| 493 | if (rc || !lower_new_dentry->d_inode) |
| 494 | goto out_lock; |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 495 | rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 496 | if (rc) |
| 497 | goto out_lock; |
Tyler Hicks | 3a8380c | 2010-03-23 18:09:02 -0500 | [diff] [blame] | 498 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
| 499 | fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 500 | old_dentry->d_inode->i_nlink = |
| 501 | ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink; |
| 502 | i_size_write(new_dentry->d_inode, file_size_save); |
| 503 | out_lock: |
| 504 | unlock_dir(lower_dir_dentry); |
| 505 | dput(lower_new_dentry); |
| 506 | dput(lower_old_dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 507 | return rc; |
| 508 | } |
| 509 | |
| 510 | static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry) |
| 511 | { |
| 512 | int rc = 0; |
| 513 | struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 514 | struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir); |
Miklos Szeredi | 8dc4e37 | 2008-05-12 14:02:04 -0700 | [diff] [blame] | 515 | struct dentry *lower_dir_dentry; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 516 | |
Tyler Hicks | 9c2d205 | 2009-09-22 12:52:17 -0500 | [diff] [blame] | 517 | dget(lower_dentry); |
Miklos Szeredi | 8dc4e37 | 2008-05-12 14:02:04 -0700 | [diff] [blame] | 518 | lower_dir_dentry = lock_parent(lower_dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 519 | rc = vfs_unlink(lower_dir_inode, lower_dentry); |
| 520 | if (rc) { |
Michael Halcrow | ae56fb1 | 2006-11-16 01:19:30 -0800 | [diff] [blame] | 521 | printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 522 | goto out_unlock; |
| 523 | } |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 524 | fsstack_copy_attr_times(dir, lower_dir_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 525 | dentry->d_inode->i_nlink = |
| 526 | ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink; |
| 527 | dentry->d_inode->i_ctime = dir->i_ctime; |
Michael Halcrow | caeeeec | 2008-01-08 15:33:02 -0800 | [diff] [blame] | 528 | d_drop(dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 529 | out_unlock: |
Miklos Szeredi | 8dc4e37 | 2008-05-12 14:02:04 -0700 | [diff] [blame] | 530 | unlock_dir(lower_dir_dentry); |
Tyler Hicks | 9c2d205 | 2009-09-22 12:52:17 -0500 | [diff] [blame] | 531 | dput(lower_dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 532 | return rc; |
| 533 | } |
| 534 | |
| 535 | static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry, |
| 536 | const char *symname) |
| 537 | { |
| 538 | int rc; |
| 539 | struct dentry *lower_dentry; |
| 540 | struct dentry *lower_dir_dentry; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 541 | char *encoded_symname; |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 542 | size_t encoded_symlen; |
| 543 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 544 | |
| 545 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 546 | dget(lower_dentry); |
| 547 | lower_dir_dentry = lock_parent(lower_dentry); |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 548 | mount_crypt_stat = &ecryptfs_superblock_to_private( |
| 549 | dir->i_sb)->mount_crypt_stat; |
| 550 | rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname, |
| 551 | &encoded_symlen, |
| 552 | NULL, |
| 553 | mount_crypt_stat, symname, |
| 554 | strlen(symname)); |
| 555 | if (rc) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 556 | goto out_lock; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 557 | rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry, |
Miklos Szeredi | db2e747 | 2008-06-24 16:50:16 +0200 | [diff] [blame] | 558 | encoded_symname); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 559 | kfree(encoded_symname); |
| 560 | if (rc || !lower_dentry->d_inode) |
| 561 | goto out_lock; |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 562 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 563 | if (rc) |
| 564 | goto out_lock; |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 565 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
| 566 | fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 567 | out_lock: |
| 568 | unlock_dir(lower_dir_dentry); |
| 569 | dput(lower_dentry); |
| 570 | if (!dentry->d_inode) |
| 571 | d_drop(dentry); |
| 572 | return rc; |
| 573 | } |
| 574 | |
| 575 | static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 576 | { |
| 577 | int rc; |
| 578 | struct dentry *lower_dentry; |
| 579 | struct dentry *lower_dir_dentry; |
| 580 | |
| 581 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 582 | lower_dir_dentry = lock_parent(lower_dentry); |
| 583 | rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode); |
| 584 | if (rc || !lower_dentry->d_inode) |
| 585 | goto out; |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 586 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 587 | if (rc) |
| 588 | goto out; |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 589 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
| 590 | fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 591 | dir->i_nlink = lower_dir_dentry->d_inode->i_nlink; |
| 592 | out: |
| 593 | unlock_dir(lower_dir_dentry); |
| 594 | if (!dentry->d_inode) |
| 595 | d_drop(dentry); |
| 596 | return rc; |
| 597 | } |
| 598 | |
| 599 | static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 600 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 601 | struct dentry *lower_dentry; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 602 | struct dentry *lower_dir_dentry; |
Michael Halcrow | 45ec4aba | 2006-10-30 22:07:20 -0800 | [diff] [blame] | 603 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 604 | |
| 605 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
Michael Halcrow | 45ec4aba | 2006-10-30 22:07:20 -0800 | [diff] [blame] | 606 | dget(dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 607 | lower_dir_dentry = lock_parent(lower_dentry); |
Michael Halcrow | 45ec4aba | 2006-10-30 22:07:20 -0800 | [diff] [blame] | 608 | dget(lower_dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 609 | rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry); |
Michael Halcrow | 45ec4aba | 2006-10-30 22:07:20 -0800 | [diff] [blame] | 610 | dput(lower_dentry); |
Tyler Hicks | 0785055 | 2011-04-29 16:26:27 -0500 | [diff] [blame] | 611 | if (!rc && dentry->d_inode) |
| 612 | clear_nlink(dentry->d_inode); |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 613 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 614 | dir->i_nlink = lower_dir_dentry->d_inode->i_nlink; |
| 615 | unlock_dir(lower_dir_dentry); |
| 616 | if (!rc) |
| 617 | d_drop(dentry); |
Michael Halcrow | 45ec4aba | 2006-10-30 22:07:20 -0800 | [diff] [blame] | 618 | dput(dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 619 | return rc; |
| 620 | } |
| 621 | |
| 622 | static int |
| 623 | ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) |
| 624 | { |
| 625 | int rc; |
| 626 | struct dentry *lower_dentry; |
| 627 | struct dentry *lower_dir_dentry; |
| 628 | |
| 629 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 630 | lower_dir_dentry = lock_parent(lower_dentry); |
| 631 | rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev); |
| 632 | if (rc || !lower_dentry->d_inode) |
| 633 | goto out; |
Tyler Hicks | 5ccf920 | 2011-05-24 02:16:51 -0500 | [diff] [blame] | 634 | rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 635 | if (rc) |
| 636 | goto out; |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 637 | fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode); |
| 638 | fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 639 | out: |
| 640 | unlock_dir(lower_dir_dentry); |
| 641 | if (!dentry->d_inode) |
| 642 | d_drop(dentry); |
| 643 | return rc; |
| 644 | } |
| 645 | |
| 646 | static int |
| 647 | ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 648 | struct inode *new_dir, struct dentry *new_dentry) |
| 649 | { |
| 650 | int rc; |
| 651 | struct dentry *lower_old_dentry; |
| 652 | struct dentry *lower_new_dentry; |
| 653 | struct dentry *lower_old_dir_dentry; |
| 654 | struct dentry *lower_new_dir_dentry; |
Erez Zadok | 0d132f7 | 2009-12-05 21:17:09 -0500 | [diff] [blame] | 655 | struct dentry *trap = NULL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 656 | |
| 657 | lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry); |
| 658 | lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry); |
| 659 | dget(lower_old_dentry); |
| 660 | dget(lower_new_dentry); |
| 661 | lower_old_dir_dentry = dget_parent(lower_old_dentry); |
| 662 | lower_new_dir_dentry = dget_parent(lower_new_dentry); |
Erez Zadok | 0d132f7 | 2009-12-05 21:17:09 -0500 | [diff] [blame] | 663 | trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry); |
| 664 | /* source should not be ancestor of target */ |
| 665 | if (trap == lower_old_dentry) { |
| 666 | rc = -EINVAL; |
| 667 | goto out_lock; |
| 668 | } |
| 669 | /* target should not be ancestor of source */ |
| 670 | if (trap == lower_new_dentry) { |
| 671 | rc = -ENOTEMPTY; |
| 672 | goto out_lock; |
| 673 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 674 | rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry, |
| 675 | lower_new_dir_dentry->d_inode, lower_new_dentry); |
| 676 | if (rc) |
| 677 | goto out_lock; |
Erez Zadok | 9afa2fb | 2009-12-02 19:51:54 -0500 | [diff] [blame] | 678 | fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 679 | if (new_dir != old_dir) |
Erez Zadok | 9afa2fb | 2009-12-02 19:51:54 -0500 | [diff] [blame] | 680 | fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 681 | out_lock: |
| 682 | unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry); |
Tyler Hicks | dd55c89 | 2011-04-12 11:23:09 -0500 | [diff] [blame] | 683 | dput(lower_new_dir_dentry); |
| 684 | dput(lower_old_dir_dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 685 | dput(lower_new_dentry); |
| 686 | dput(lower_old_dentry); |
| 687 | return rc; |
| 688 | } |
| 689 | |
Tyler Hicks | 3a60a16 | 2010-03-22 00:41:35 -0500 | [diff] [blame] | 690 | static int ecryptfs_readlink_lower(struct dentry *dentry, char **buf, |
| 691 | size_t *bufsiz) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 692 | { |
Tyler Hicks | 3a60a16 | 2010-03-22 00:41:35 -0500 | [diff] [blame] | 693 | struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 694 | char *lower_buf; |
Tyler Hicks | 3a60a16 | 2010-03-22 00:41:35 -0500 | [diff] [blame] | 695 | size_t lower_bufsiz = PATH_MAX; |
Michael Halcrow | addd65ad | 2009-01-06 14:42:00 -0800 | [diff] [blame] | 696 | mm_segment_t old_fs; |
| 697 | int rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 698 | |
Tyler Hicks | 3a6b42c | 2009-04-16 18:35:37 -0500 | [diff] [blame] | 699 | lower_buf = kmalloc(lower_bufsiz, GFP_KERNEL); |
Tyler Hicks | 3a60a16 | 2010-03-22 00:41:35 -0500 | [diff] [blame] | 700 | if (!lower_buf) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 701 | rc = -ENOMEM; |
| 702 | goto out; |
| 703 | } |
| 704 | old_fs = get_fs(); |
| 705 | set_fs(get_ds()); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 706 | rc = lower_dentry->d_inode->i_op->readlink(lower_dentry, |
| 707 | (char __user *)lower_buf, |
Tyler Hicks | 3a6b42c | 2009-04-16 18:35:37 -0500 | [diff] [blame] | 708 | lower_bufsiz); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 709 | set_fs(old_fs); |
Tyler Hicks | 3a60a16 | 2010-03-22 00:41:35 -0500 | [diff] [blame] | 710 | if (rc < 0) |
| 711 | goto out; |
| 712 | lower_bufsiz = rc; |
| 713 | rc = ecryptfs_decode_and_decrypt_filename(buf, bufsiz, dentry, |
| 714 | lower_buf, lower_bufsiz); |
| 715 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 716 | kfree(lower_buf); |
Tyler Hicks | 3a60a16 | 2010-03-22 00:41:35 -0500 | [diff] [blame] | 717 | return rc; |
| 718 | } |
| 719 | |
| 720 | static int |
| 721 | ecryptfs_readlink(struct dentry *dentry, char __user *buf, int bufsiz) |
| 722 | { |
| 723 | char *kbuf; |
| 724 | size_t kbufsiz, copied; |
| 725 | int rc; |
| 726 | |
| 727 | rc = ecryptfs_readlink_lower(dentry, &kbuf, &kbufsiz); |
| 728 | if (rc) |
| 729 | goto out; |
| 730 | copied = min_t(size_t, bufsiz, kbufsiz); |
| 731 | rc = copy_to_user(buf, kbuf, copied) ? -EFAULT : copied; |
| 732 | kfree(kbuf); |
| 733 | fsstack_copy_attr_atime(dentry->d_inode, |
| 734 | ecryptfs_dentry_to_lower(dentry)->d_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 735 | out: |
| 736 | return rc; |
| 737 | } |
| 738 | |
| 739 | static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 740 | { |
| 741 | char *buf; |
| 742 | int len = PAGE_SIZE, rc; |
| 743 | mm_segment_t old_fs; |
| 744 | |
| 745 | /* Released in ecryptfs_put_link(); only release here on error */ |
| 746 | buf = kmalloc(len, GFP_KERNEL); |
| 747 | if (!buf) { |
OGAWA Hirofumi | 806892e | 2010-01-12 03:36:14 +0900 | [diff] [blame] | 748 | buf = ERR_PTR(-ENOMEM); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 749 | goto out; |
| 750 | } |
| 751 | old_fs = get_fs(); |
| 752 | set_fs(get_ds()); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 753 | rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 754 | set_fs(old_fs); |
OGAWA Hirofumi | 806892e | 2010-01-12 03:36:14 +0900 | [diff] [blame] | 755 | if (rc < 0) { |
| 756 | kfree(buf); |
| 757 | buf = ERR_PTR(rc); |
| 758 | } else |
Duane Griffin | a17d523 | 2008-12-19 20:47:10 +0000 | [diff] [blame] | 759 | buf[rc] = '\0'; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 760 | out: |
OGAWA Hirofumi | 806892e | 2010-01-12 03:36:14 +0900 | [diff] [blame] | 761 | nd_set_link(nd, buf); |
| 762 | return NULL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static void |
| 766 | ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr) |
| 767 | { |
OGAWA Hirofumi | 806892e | 2010-01-12 03:36:14 +0900 | [diff] [blame] | 768 | char *buf = nd_get_link(nd); |
| 769 | if (!IS_ERR(buf)) { |
| 770 | /* Free the char* */ |
| 771 | kfree(buf); |
| 772 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | /** |
| 776 | * upper_size_to_lower_size |
| 777 | * @crypt_stat: Crypt_stat associated with file |
| 778 | * @upper_size: Size of the upper file |
| 779 | * |
Michael Halcrow | cc11bef | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 780 | * Calculate the required size of the lower file based on the |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 781 | * specified size of the upper file. This calculation is based on the |
| 782 | * number of headers in the underlying file and the extent size. |
| 783 | * |
| 784 | * Returns Calculated size of the lower file. |
| 785 | */ |
| 786 | static loff_t |
| 787 | upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat, |
| 788 | loff_t upper_size) |
| 789 | { |
| 790 | loff_t lower_size; |
| 791 | |
Tyler Hicks | 157f107 | 2010-02-11 07:10:38 -0600 | [diff] [blame] | 792 | lower_size = ecryptfs_lower_header_size(crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 793 | if (upper_size != 0) { |
| 794 | loff_t num_extents; |
| 795 | |
| 796 | num_extents = upper_size >> crypt_stat->extent_shift; |
| 797 | if (upper_size & ~crypt_stat->extent_mask) |
| 798 | num_extents++; |
| 799 | lower_size += (num_extents * crypt_stat->extent_size); |
| 800 | } |
| 801 | return lower_size; |
| 802 | } |
| 803 | |
| 804 | /** |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 805 | * truncate_upper |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 806 | * @dentry: The ecryptfs layer dentry |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 807 | * @ia: Address of the ecryptfs inode's attributes |
| 808 | * @lower_ia: Address of the lower inode's attributes |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 809 | * |
| 810 | * Function to handle truncations modifying the size of the file. Note |
| 811 | * that the file sizes are interpolated. When expanding, we are simply |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 812 | * writing strings of 0's out. When truncating, we truncate the upper |
| 813 | * inode and update the lower_ia according to the page index |
| 814 | * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return, |
| 815 | * the caller must use lower_ia in a call to notify_change() to perform |
| 816 | * the truncation of the lower inode. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 817 | * |
| 818 | * Returns zero on success; non-zero otherwise |
| 819 | */ |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 820 | static int truncate_upper(struct dentry *dentry, struct iattr *ia, |
| 821 | struct iattr *lower_ia) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 822 | { |
| 823 | int rc = 0; |
| 824 | struct inode *inode = dentry->d_inode; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 825 | struct ecryptfs_crypt_stat *crypt_stat; |
| 826 | loff_t i_size = i_size_read(inode); |
| 827 | loff_t lower_size_before_truncate; |
| 828 | loff_t lower_size_after_truncate; |
| 829 | |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 830 | if (unlikely((ia->ia_size == i_size))) { |
| 831 | lower_ia->ia_valid &= ~ATTR_SIZE; |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 832 | return 0; |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 833 | } |
Tyler Hicks | 3b06b3e | 2011-05-24 03:49:02 -0500 | [diff] [blame] | 834 | rc = ecryptfs_get_lower_file(dentry, inode); |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 835 | if (rc) |
| 836 | return rc; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 837 | crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 838 | /* Switch on growing or shrinking file */ |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 839 | if (ia->ia_size > i_size) { |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 840 | char zero[] = { 0x00 }; |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 841 | |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 842 | lower_ia->ia_valid &= ~ATTR_SIZE; |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 843 | /* Write a single 0 at the last position of the file; |
| 844 | * this triggers code that will fill in 0's throughout |
| 845 | * the intermediate portion of the previous end of the |
| 846 | * file and the new and of the file */ |
Al Viro | 48c1e44 | 2010-05-21 11:09:58 -0400 | [diff] [blame] | 847 | rc = ecryptfs_write(inode, zero, |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 848 | (ia->ia_size - 1), 1); |
| 849 | } else { /* ia->ia_size < i_size_read(inode) */ |
| 850 | /* We're chopping off all the pages down to the page |
| 851 | * in which ia->ia_size is located. Fill in the end of |
| 852 | * that page from (ia->ia_size & ~PAGE_CACHE_MASK) to |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 853 | * PAGE_CACHE_SIZE with zeros. */ |
| 854 | size_t num_zeros = (PAGE_CACHE_SIZE |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 855 | - (ia->ia_size & ~PAGE_CACHE_MASK)); |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 856 | |
Tyler Hicks | 13a791b | 2009-04-13 15:29:27 -0500 | [diff] [blame] | 857 | if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) { |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 858 | truncate_setsize(inode, ia->ia_size); |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 859 | lower_ia->ia_size = ia->ia_size; |
| 860 | lower_ia->ia_valid |= ATTR_SIZE; |
Al Viro | 48c1e44 | 2010-05-21 11:09:58 -0400 | [diff] [blame] | 861 | goto out; |
Tyler Hicks | 13a791b | 2009-04-13 15:29:27 -0500 | [diff] [blame] | 862 | } |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 863 | if (num_zeros) { |
| 864 | char *zeros_virt; |
| 865 | |
| 866 | zeros_virt = kzalloc(num_zeros, GFP_KERNEL); |
| 867 | if (!zeros_virt) { |
| 868 | rc = -ENOMEM; |
Al Viro | 48c1e44 | 2010-05-21 11:09:58 -0400 | [diff] [blame] | 869 | goto out; |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 870 | } |
Al Viro | 48c1e44 | 2010-05-21 11:09:58 -0400 | [diff] [blame] | 871 | rc = ecryptfs_write(inode, zeros_virt, |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 872 | ia->ia_size, num_zeros); |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 873 | kfree(zeros_virt); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 874 | if (rc) { |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 875 | printk(KERN_ERR "Error attempting to zero out " |
| 876 | "the remainder of the end page on " |
| 877 | "reducing truncate; rc = [%d]\n", rc); |
Al Viro | 48c1e44 | 2010-05-21 11:09:58 -0400 | [diff] [blame] | 878 | goto out; |
Michael Halcrow | 240e2df | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 879 | } |
| 880 | } |
Christoph Hellwig | 2c27c65 | 2010-06-04 11:30:04 +0200 | [diff] [blame] | 881 | truncate_setsize(inode, ia->ia_size); |
Michael Halcrow | 0216f7f | 2007-10-16 01:28:08 -0700 | [diff] [blame] | 882 | rc = ecryptfs_write_inode_size_to_metadata(inode); |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 883 | if (rc) { |
| 884 | printk(KERN_ERR "Problem with " |
| 885 | "ecryptfs_write_inode_size_to_metadata; " |
| 886 | "rc = [%d]\n", rc); |
Al Viro | 48c1e44 | 2010-05-21 11:09:58 -0400 | [diff] [blame] | 887 | goto out; |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 888 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 889 | /* We are reducing the size of the ecryptfs file, and need to |
| 890 | * know if we need to reduce the size of the lower file. */ |
| 891 | lower_size_before_truncate = |
| 892 | upper_size_to_lower_size(crypt_stat, i_size); |
| 893 | lower_size_after_truncate = |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 894 | upper_size_to_lower_size(crypt_stat, ia->ia_size); |
| 895 | if (lower_size_after_truncate < lower_size_before_truncate) { |
| 896 | lower_ia->ia_size = lower_size_after_truncate; |
| 897 | lower_ia->ia_valid |= ATTR_SIZE; |
| 898 | } else |
| 899 | lower_ia->ia_valid &= ~ATTR_SIZE; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 900 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 901 | out: |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 902 | ecryptfs_put_lower_file(inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 903 | return rc; |
| 904 | } |
| 905 | |
Tyler Hicks | 714ca4e | 2012-01-19 20:33:44 -0600 | [diff] [blame] | 906 | static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset) |
| 907 | { |
| 908 | struct ecryptfs_crypt_stat *crypt_stat; |
| 909 | loff_t lower_oldsize, lower_newsize; |
| 910 | |
| 911 | crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat; |
| 912 | lower_oldsize = upper_size_to_lower_size(crypt_stat, |
| 913 | i_size_read(inode)); |
| 914 | lower_newsize = upper_size_to_lower_size(crypt_stat, offset); |
| 915 | if (lower_newsize > lower_oldsize) { |
| 916 | /* |
| 917 | * The eCryptfs inode and the new *lower* size are mixed here |
| 918 | * because we may not have the lower i_mutex held and/or it may |
| 919 | * not be appropriate to call inode_newsize_ok() with inodes |
| 920 | * from other filesystems. |
| 921 | */ |
| 922 | return inode_newsize_ok(inode, lower_newsize); |
| 923 | } |
| 924 | |
| 925 | return 0; |
| 926 | } |
| 927 | |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 928 | /** |
| 929 | * ecryptfs_truncate |
| 930 | * @dentry: The ecryptfs layer dentry |
| 931 | * @new_length: The length to expand the file to |
| 932 | * |
| 933 | * Simple function that handles the truncation of an eCryptfs inode and |
| 934 | * its corresponding lower inode. |
| 935 | * |
| 936 | * Returns zero on success; non-zero otherwise |
| 937 | */ |
| 938 | int ecryptfs_truncate(struct dentry *dentry, loff_t new_length) |
| 939 | { |
| 940 | struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length }; |
| 941 | struct iattr lower_ia = { .ia_valid = 0 }; |
| 942 | int rc; |
| 943 | |
Tyler Hicks | 714ca4e | 2012-01-19 20:33:44 -0600 | [diff] [blame] | 944 | rc = ecryptfs_inode_newsize_ok(dentry->d_inode, new_length); |
| 945 | if (rc) |
| 946 | return rc; |
| 947 | |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 948 | rc = truncate_upper(dentry, &ia, &lower_ia); |
| 949 | if (!rc && lower_ia.ia_valid & ATTR_SIZE) { |
| 950 | struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 951 | |
| 952 | mutex_lock(&lower_dentry->d_inode->i_mutex); |
| 953 | rc = notify_change(lower_dentry, &lower_ia); |
| 954 | mutex_unlock(&lower_dentry->d_inode->i_mutex); |
| 955 | } |
| 956 | return rc; |
| 957 | } |
| 958 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 959 | static int |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 960 | ecryptfs_permission(struct inode *inode, int mask, unsigned int flags) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 961 | { |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 962 | if (flags & IPERM_FLAG_RCU) |
| 963 | return -ECHILD; |
Al Viro | f419a2e | 2008-07-22 00:07:17 -0400 | [diff] [blame] | 964 | return inode_permission(ecryptfs_inode_to_lower(inode), mask); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /** |
| 968 | * ecryptfs_setattr |
| 969 | * @dentry: dentry handle to the inode to modify |
| 970 | * @ia: Structure with flags of what to change and values |
| 971 | * |
| 972 | * Updates the metadata of an inode. If the update is to the size |
| 973 | * i.e. truncation, then ecryptfs_truncate will handle the size modification |
| 974 | * of both the ecryptfs inode and the lower inode. |
| 975 | * |
| 976 | * All other metadata changes will be passed right to the lower filesystem, |
| 977 | * and we will just update our inode to look like the lower. |
| 978 | */ |
| 979 | static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia) |
| 980 | { |
| 981 | int rc = 0; |
| 982 | struct dentry *lower_dentry; |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 983 | struct iattr lower_ia; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 984 | struct inode *inode; |
| 985 | struct inode *lower_inode; |
| 986 | struct ecryptfs_crypt_stat *crypt_stat; |
| 987 | |
| 988 | crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat; |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 989 | if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED)) |
| 990 | ecryptfs_init_crypt_stat(crypt_stat); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 991 | inode = dentry->d_inode; |
| 992 | lower_inode = ecryptfs_inode_to_lower(inode); |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 993 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 994 | mutex_lock(&crypt_stat->cs_mutex); |
| 995 | if (S_ISDIR(dentry->d_inode->i_mode)) |
| 996 | crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED); |
Michael Halcrow | 64ee480 | 2007-07-19 01:47:54 -0700 | [diff] [blame] | 997 | else if (S_ISREG(dentry->d_inode->i_mode) |
| 998 | && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED) |
| 999 | || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) { |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1000 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat; |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1001 | |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1002 | mount_crypt_stat = &ecryptfs_superblock_to_private( |
| 1003 | dentry->d_sb)->mount_crypt_stat; |
Tyler Hicks | 3b06b3e | 2011-05-24 03:49:02 -0500 | [diff] [blame] | 1004 | rc = ecryptfs_get_lower_file(dentry, inode); |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 1005 | if (rc) { |
| 1006 | mutex_unlock(&crypt_stat->cs_mutex); |
| 1007 | goto out; |
| 1008 | } |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1009 | rc = ecryptfs_read_metadata(dentry); |
Tyler Hicks | 332ab16 | 2011-04-14 15:35:11 -0500 | [diff] [blame] | 1010 | ecryptfs_put_lower_file(inode); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1011 | if (rc) { |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1012 | if (!(mount_crypt_stat->flags |
| 1013 | & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) { |
| 1014 | rc = -EIO; |
Michael Halcrow | 25bd817 | 2008-02-06 01:38:35 -0800 | [diff] [blame] | 1015 | printk(KERN_WARNING "Either the lower file " |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1016 | "is not in a valid eCryptfs format, " |
Michael Halcrow | 25bd817 | 2008-02-06 01:38:35 -0800 | [diff] [blame] | 1017 | "or the key could not be retrieved. " |
| 1018 | "Plaintext passthrough mode is not " |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1019 | "enabled; returning -EIO\n"); |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1020 | mutex_unlock(&crypt_stat->cs_mutex); |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1021 | goto out; |
| 1022 | } |
| 1023 | rc = 0; |
Tyler Hicks | 3aeb86e | 2011-03-15 14:54:00 -0500 | [diff] [blame] | 1024 | crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED |
| 1025 | | ECRYPTFS_ENCRYPTED); |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1026 | } |
Michael Halcrow | e10f281 | 2007-06-27 14:09:44 -0700 | [diff] [blame] | 1027 | } |
| 1028 | mutex_unlock(&crypt_stat->cs_mutex); |
Tyler Hicks | 714ca4e | 2012-01-19 20:33:44 -0600 | [diff] [blame] | 1029 | |
| 1030 | rc = inode_change_ok(inode, ia); |
| 1031 | if (rc) |
| 1032 | goto out; |
| 1033 | if (ia->ia_valid & ATTR_SIZE) { |
| 1034 | rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size); |
| 1035 | if (rc) |
| 1036 | goto out; |
| 1037 | } |
| 1038 | |
Tyler Hicks | 5be79de | 2011-04-22 13:08:00 -0500 | [diff] [blame] | 1039 | if (S_ISREG(inode->i_mode)) { |
| 1040 | rc = filemap_write_and_wait(inode->i_mapping); |
| 1041 | if (rc) |
| 1042 | goto out; |
| 1043 | fsstack_copy_attr_all(inode, lower_inode); |
| 1044 | } |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 1045 | memcpy(&lower_ia, ia, sizeof(lower_ia)); |
| 1046 | if (ia->ia_valid & ATTR_FILE) |
| 1047 | lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1048 | if (ia->ia_valid & ATTR_SIZE) { |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 1049 | rc = truncate_upper(dentry, ia, &lower_ia); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1050 | if (rc < 0) |
| 1051 | goto out; |
| 1052 | } |
Jeff Layton | 1ac564e | 2007-10-18 03:05:17 -0700 | [diff] [blame] | 1053 | |
| 1054 | /* |
| 1055 | * mode change is for clearing setuid/setgid bits. Allow lower fs |
| 1056 | * to interpret this in its own way. |
| 1057 | */ |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 1058 | if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) |
| 1059 | lower_ia.ia_valid &= ~ATTR_MODE; |
Jeff Layton | 1ac564e | 2007-10-18 03:05:17 -0700 | [diff] [blame] | 1060 | |
Miklos Szeredi | 9c3580a | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 1061 | mutex_lock(&lower_dentry->d_inode->i_mutex); |
Tyler Hicks | 5f3ef64 | 2009-10-14 16:18:27 -0500 | [diff] [blame] | 1062 | rc = notify_change(lower_dentry, &lower_ia); |
Miklos Szeredi | 9c3580a | 2008-04-29 00:59:48 -0700 | [diff] [blame] | 1063 | mutex_unlock(&lower_dentry->d_inode->i_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1064 | out: |
Erez Zadok | 9afa2fb | 2009-12-02 19:51:54 -0500 | [diff] [blame] | 1065 | fsstack_copy_attr_all(inode, lower_inode); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1066 | return rc; |
| 1067 | } |
| 1068 | |
Tyler Hicks | 3a60a16 | 2010-03-22 00:41:35 -0500 | [diff] [blame] | 1069 | int ecryptfs_getattr_link(struct vfsmount *mnt, struct dentry *dentry, |
| 1070 | struct kstat *stat) |
| 1071 | { |
| 1072 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat; |
| 1073 | int rc = 0; |
| 1074 | |
| 1075 | mount_crypt_stat = &ecryptfs_superblock_to_private( |
| 1076 | dentry->d_sb)->mount_crypt_stat; |
| 1077 | generic_fillattr(dentry->d_inode, stat); |
| 1078 | if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) { |
| 1079 | char *target; |
| 1080 | size_t targetsiz; |
| 1081 | |
| 1082 | rc = ecryptfs_readlink_lower(dentry, &target, &targetsiz); |
| 1083 | if (!rc) { |
| 1084 | kfree(target); |
| 1085 | stat->size = targetsiz; |
| 1086 | } |
| 1087 | } |
| 1088 | return rc; |
| 1089 | } |
| 1090 | |
Tyler Hicks | f8f484d | 2009-11-04 02:48:01 -0600 | [diff] [blame] | 1091 | int ecryptfs_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 1092 | struct kstat *stat) |
| 1093 | { |
| 1094 | struct kstat lower_stat; |
| 1095 | int rc; |
| 1096 | |
| 1097 | rc = vfs_getattr(ecryptfs_dentry_to_lower_mnt(dentry), |
| 1098 | ecryptfs_dentry_to_lower(dentry), &lower_stat); |
| 1099 | if (!rc) { |
Tyler Hicks | 55f9cf6 | 2011-01-11 12:43:42 -0600 | [diff] [blame] | 1100 | fsstack_copy_attr_all(dentry->d_inode, |
| 1101 | ecryptfs_inode_to_lower(dentry->d_inode)); |
Tyler Hicks | f8f484d | 2009-11-04 02:48:01 -0600 | [diff] [blame] | 1102 | generic_fillattr(dentry->d_inode, stat); |
| 1103 | stat->blocks = lower_stat.blocks; |
| 1104 | } |
| 1105 | return rc; |
| 1106 | } |
| 1107 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1108 | int |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1109 | ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value, |
| 1110 | size_t size, int flags) |
| 1111 | { |
| 1112 | int rc = 0; |
| 1113 | struct dentry *lower_dentry; |
| 1114 | |
| 1115 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 1116 | if (!lower_dentry->d_inode->i_op->setxattr) { |
Christian Pulvermacher | cfce08c | 2010-03-23 11:51:38 -0500 | [diff] [blame] | 1117 | rc = -EOPNOTSUPP; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1118 | goto out; |
| 1119 | } |
Roberto Sassu | 48b512e | 2010-10-05 18:53:45 +0200 | [diff] [blame] | 1120 | |
| 1121 | rc = vfs_setxattr(lower_dentry, name, value, size, flags); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1122 | out: |
| 1123 | return rc; |
| 1124 | } |
| 1125 | |
Michael Halcrow | dd2a3b7 | 2007-02-12 00:53:46 -0800 | [diff] [blame] | 1126 | ssize_t |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1127 | ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name, |
| 1128 | void *value, size_t size) |
| 1129 | { |
| 1130 | int rc = 0; |
| 1131 | |
| 1132 | if (!lower_dentry->d_inode->i_op->getxattr) { |
Christian Pulvermacher | cfce08c | 2010-03-23 11:51:38 -0500 | [diff] [blame] | 1133 | rc = -EOPNOTSUPP; |
Michael Halcrow | d7cdc5f | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1134 | goto out; |
| 1135 | } |
| 1136 | mutex_lock(&lower_dentry->d_inode->i_mutex); |
| 1137 | rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value, |
| 1138 | size); |
| 1139 | mutex_unlock(&lower_dentry->d_inode->i_mutex); |
| 1140 | out: |
| 1141 | return rc; |
| 1142 | } |
| 1143 | |
Adrian Bunk | 7896b63 | 2008-02-06 01:38:32 -0800 | [diff] [blame] | 1144 | static ssize_t |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1145 | ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value, |
| 1146 | size_t size) |
| 1147 | { |
Michael Halcrow | 2ed9255 | 2007-10-16 01:28:10 -0700 | [diff] [blame] | 1148 | return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name, |
| 1149 | value, size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | static ssize_t |
| 1153 | ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size) |
| 1154 | { |
| 1155 | int rc = 0; |
| 1156 | struct dentry *lower_dentry; |
| 1157 | |
| 1158 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 1159 | if (!lower_dentry->d_inode->i_op->listxattr) { |
Christian Pulvermacher | cfce08c | 2010-03-23 11:51:38 -0500 | [diff] [blame] | 1160 | rc = -EOPNOTSUPP; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1161 | goto out; |
| 1162 | } |
| 1163 | mutex_lock(&lower_dentry->d_inode->i_mutex); |
| 1164 | rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size); |
| 1165 | mutex_unlock(&lower_dentry->d_inode->i_mutex); |
| 1166 | out: |
| 1167 | return rc; |
| 1168 | } |
| 1169 | |
| 1170 | static int ecryptfs_removexattr(struct dentry *dentry, const char *name) |
| 1171 | { |
| 1172 | int rc = 0; |
| 1173 | struct dentry *lower_dentry; |
| 1174 | |
| 1175 | lower_dentry = ecryptfs_dentry_to_lower(dentry); |
| 1176 | if (!lower_dentry->d_inode->i_op->removexattr) { |
Christian Pulvermacher | cfce08c | 2010-03-23 11:51:38 -0500 | [diff] [blame] | 1177 | rc = -EOPNOTSUPP; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1178 | goto out; |
| 1179 | } |
| 1180 | mutex_lock(&lower_dentry->d_inode->i_mutex); |
| 1181 | rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name); |
| 1182 | mutex_unlock(&lower_dentry->d_inode->i_mutex); |
| 1183 | out: |
| 1184 | return rc; |
| 1185 | } |
| 1186 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1187 | const struct inode_operations ecryptfs_symlink_iops = { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1188 | .readlink = ecryptfs_readlink, |
| 1189 | .follow_link = ecryptfs_follow_link, |
| 1190 | .put_link = ecryptfs_put_link, |
| 1191 | .permission = ecryptfs_permission, |
| 1192 | .setattr = ecryptfs_setattr, |
Tyler Hicks | 3a60a16 | 2010-03-22 00:41:35 -0500 | [diff] [blame] | 1193 | .getattr = ecryptfs_getattr_link, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1194 | .setxattr = ecryptfs_setxattr, |
| 1195 | .getxattr = ecryptfs_getxattr, |
| 1196 | .listxattr = ecryptfs_listxattr, |
| 1197 | .removexattr = ecryptfs_removexattr |
| 1198 | }; |
| 1199 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1200 | const struct inode_operations ecryptfs_dir_iops = { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1201 | .create = ecryptfs_create, |
| 1202 | .lookup = ecryptfs_lookup, |
| 1203 | .link = ecryptfs_link, |
| 1204 | .unlink = ecryptfs_unlink, |
| 1205 | .symlink = ecryptfs_symlink, |
| 1206 | .mkdir = ecryptfs_mkdir, |
| 1207 | .rmdir = ecryptfs_rmdir, |
| 1208 | .mknod = ecryptfs_mknod, |
| 1209 | .rename = ecryptfs_rename, |
| 1210 | .permission = ecryptfs_permission, |
| 1211 | .setattr = ecryptfs_setattr, |
| 1212 | .setxattr = ecryptfs_setxattr, |
| 1213 | .getxattr = ecryptfs_getxattr, |
| 1214 | .listxattr = ecryptfs_listxattr, |
| 1215 | .removexattr = ecryptfs_removexattr |
| 1216 | }; |
| 1217 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 1218 | const struct inode_operations ecryptfs_main_iops = { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1219 | .permission = ecryptfs_permission, |
| 1220 | .setattr = ecryptfs_setattr, |
Tyler Hicks | f8f484d | 2009-11-04 02:48:01 -0600 | [diff] [blame] | 1221 | .getattr = ecryptfs_getattr, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1222 | .setxattr = ecryptfs_setxattr, |
| 1223 | .getxattr = ecryptfs_getxattr, |
| 1224 | .listxattr = ecryptfs_listxattr, |
| 1225 | .removexattr = ecryptfs_removexattr |
| 1226 | }; |