blob: e3562f2a59dc10e1243e05e7ba56651a44e68740 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08006 * Copyright (C) 2004-2007 International Business Machines Corp.
Michael Halcrow237fead2006-10-04 02:16:22 -07007 * 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" Sipek0cc72dc2006-12-08 02:36:31 -080033#include <linux/fs_stack.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Roberto Sassu48b512e2010-10-05 18:53:45 +020035#include <linux/xattr.h>
Harvey Harrison0a688ad2008-07-23 21:30:07 -070036#include <asm/unaligned.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070037#include "ecryptfs_kernel.h"
38
39static struct dentry *lock_parent(struct dentry *dentry)
40{
41 struct dentry *dir;
42
Miklos Szeredi8dc4e372008-05-12 14:02:04 -070043 dir = dget_parent(dentry);
Peter Zijlstra908e0a82007-03-07 20:41:30 -080044 mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
Michael Halcrow237fead2006-10-04 02:16:22 -070045 return dir;
46}
47
Michael Halcrow237fead2006-10-04 02:16:22 -070048static void unlock_dir(struct dentry *dir)
49{
50 mutex_unlock(&dir->d_inode->i_mutex);
51 dput(dir);
52}
53
Tyler Hicksc4f79072011-05-23 21:18:20 -050054static 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 Hicks5ccf9202011-05-24 02:16:51 -050061static int ecryptfs_inode_set(struct inode *inode, void *opaque)
Tyler Hicksc4f79072011-05-23 21:18:20 -050062{
Tyler Hicks5ccf9202011-05-24 02:16:51 -050063 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 Hicksc4f79072011-05-23 21:18:20 -050070 inode->i_version++;
Tyler Hicksc4f79072011-05-23 21:18:20 -050071 inode->i_mapping->a_ops = &ecryptfs_aops;
Thieu Lea21353b2011-07-26 16:15:10 -070072 inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
Tyler Hicks5ccf9202011-05-24 02:16:51 -050073
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 Hicksc4f79072011-05-23 21:18:20 -050088 return 0;
89}
90
Tyler Hicks5ccf9202011-05-24 02:16:51 -050091static 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 Hicksc4f79072011-05-23 21:18:20 -0500113struct inode *ecryptfs_get_inode(struct inode *lower_inode,
114 struct super_block *sb)
115{
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500116 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
Tyler Hicksc4f79072011-05-23 21:18:20 -0500117
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500118 if (!IS_ERR(inode) && (inode->i_state & I_NEW))
Tyler Hicksc4f79072011-05-23 21:18:20 -0500119 unlock_new_inode(inode);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500120
Tyler Hicksc4f79072011-05-23 21:18:20 -0500121 return inode;
Tyler Hicksc4f79072011-05-23 21:18:20 -0500122}
123
Tyler Hicksc4f79072011-05-23 21:18:20 -0500124/**
125 * ecryptfs_interpose
126 * @lower_dentry: Existing dentry in the lower filesystem
127 * @dentry: ecryptfs' dentry
128 * @sb: ecryptfs's super_block
Tyler Hicksc4f79072011-05-23 21:18:20 -0500129 *
130 * Interposes upper and lower dentries.
131 *
132 * Returns zero on success; non-zero otherwise
133 */
134static int ecryptfs_interpose(struct dentry *lower_dentry,
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500135 struct dentry *dentry, struct super_block *sb)
Tyler Hicksc4f79072011-05-23 21:18:20 -0500136{
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500137 struct inode *inode = ecryptfs_get_inode(lower_dentry->d_inode, sb);
138
Tyler Hicksc4f79072011-05-23 21:18:20 -0500139 if (IS_ERR(inode))
140 return PTR_ERR(inode);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500141 d_instantiate(dentry, inode);
142
Tyler Hicksc4f79072011-05-23 21:18:20 -0500143 return 0;
144}
145
Michael Halcrow237fead2006-10-04 02:16:22 -0700146/**
147 * ecryptfs_create_underlying_file
148 * @lower_dir_inode: inode of the parent in the lower fs of the new file
Qinghuang Fengf70f5822009-01-06 14:42:05 -0800149 * @dentry: New file's dentry
Michael Halcrow237fead2006-10-04 02:16:22 -0700150 * @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 */
157static int
158ecryptfs_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 Hicks2e21b3f2010-09-23 02:35:04 -0500166 unsigned int flags_save;
Michael Halcrow237fead2006-10-04 02:16:22 -0700167 int rc;
168
Tyler Hicks70b89022011-02-17 17:35:20 -0600169 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 Halcrow237fead2006-10-04 02:16:22 -0700177 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
Tyler Hicks70b89022011-02-17 17:35:20 -0600178 if (nd) {
179 nd->path.dentry = dentry_save;
180 nd->path.mnt = vfsmount_save;
181 nd->flags = flags_save;
182 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700183 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 */
199static int
200ecryptfs_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 Nakagawa801678c2008-04-29 01:03:09 -0700210 if (IS_ERR(lower_dir_dentry)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700211 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 Halcrow4981e082007-10-16 01:28:09 -0700218 if (rc) {
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800219 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700220 "rc = [%d]\n", __func__, rc);
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800221 goto out_lock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700222 }
223 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500224 directory_inode->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700225 if (rc) {
226 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
227 goto out_lock;
228 }
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800229 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
230 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700231out_lock:
232 unlock_dir(lower_dir_dentry);
233out:
234 return rc;
235}
236
237/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700238 * 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 */
245static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
246{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700247 struct ecryptfs_crypt_stat *crypt_stat =
248 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700249 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700250
Michael Halcrow237fead2006-10-04 02:16:22 -0700251 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
252 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800253 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700254 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700255 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700256 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
257 rc = ecryptfs_new_file_context(ecryptfs_dentry);
258 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700259 ecryptfs_printk(KERN_ERR, "Error creating new file "
260 "context; rc = [%d]\n", rc);
261 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700262 }
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500263 rc = ecryptfs_get_lower_file(ecryptfs_dentry,
264 ecryptfs_dentry->d_inode);
Roberto Sassu27992892010-11-03 11:11:28 +0100265 if (rc) {
266 printk(KERN_ERR "%s: Error attempting to initialize "
Tyler Hicks332ab162011-04-14 15:35:11 -0500267 "the lower file for the dentry with name "
Roberto Sassu27992892010-11-03 11:11:28 +0100268 "[%s]; rc = [%d]\n", __func__,
269 ecryptfs_dentry->d_name.name, rc);
270 goto out;
Michael Halcrow391b52f2008-07-23 21:30:08 -0700271 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700272 rc = ecryptfs_write_metadata(ecryptfs_dentry);
Tyler Hicks332ab162011-04-14 15:35:11 -0500273 if (rc)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700274 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
Tyler Hicks332ab162011-04-14 15:35:11 -0500275 ecryptfs_put_lower_file(ecryptfs_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700276out:
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 */
291static int
292ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
293 int mode, struct nameidata *nd)
294{
295 int rc;
296
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800297 /* ecryptfs_do_create() calls ecryptfs_interpose() */
Michael Halcrow237fead2006-10-04 02:16:22 -0700298 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);
307out:
308 return rc;
309}
310
Tyler Hicks778aeb42011-05-24 04:56:23 -0500311static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700312{
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500313 struct ecryptfs_crypt_stat *crypt_stat;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500314 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700315
Tyler Hicks778aeb42011-05-24 04:56:23 -0500316 rc = ecryptfs_get_lower_file(dentry, inode);
Roberto Sassu27992892010-11-03 11:11:28 +0100317 if (rc) {
318 printk(KERN_ERR "%s: Error attempting to initialize "
Tyler Hicks332ab162011-04-14 15:35:11 -0500319 "the lower file for the dentry with name "
Roberto Sassu27992892010-11-03 11:11:28 +0100320 "[%s]; rc = [%d]\n", __func__,
Tyler Hicks778aeb42011-05-24 04:56:23 -0500321 dentry->d_name.name, rc);
322 return rc;
Michael Halcrow391b52f2008-07-23 21:30:08 -0700323 }
Tyler Hicks778aeb42011-05-24 04:56:23 -0500324
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500325 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500326 /* TODO: lock for crypt_stat comparison */
327 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
Tyler Hicks778aeb42011-05-24 04:56:23 -0500328 ecryptfs_set_default_sizes(crypt_stat);
329
330 rc = ecryptfs_read_and_validate_header_region(inode);
331 ecryptfs_put_lower_file(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700332 if (rc) {
Tyler Hicks778aeb42011-05-24 04:56:23 -0500333 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
334 if (!rc)
335 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
Michael Halcrow237fead2006-10-04 02:16:22 -0700336 }
Tyler Hicks778aeb42011-05-24 04:56:23 -0500337
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 */
345static 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 Hicks3b06b3e2011-05-24 03:49:02 -0500391 if (inode->i_state & I_NEW)
392 unlock_new_inode(inode);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500393 d_add(dentry, inode);
394
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800395 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 */
407static 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 Halcrowa8f12862009-01-06 14:42:03 -0800412 size_t encrypted_and_encoded_name_size;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800413 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800414 struct dentry *lower_dir_dentry, *lower_dentry;
415 int rc = 0;
416
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800417 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 Hicks8787c7a2011-02-17 18:51:24 -0600424 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 Halcrowaddd65ad2009-01-06 14:42:00 -0800429 if (IS_ERR(lower_dentry)) {
430 rc = PTR_ERR(lower_dentry);
Tyler Hicks8787c7a2011-02-17 18:51:24 -0600431 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
Tyler Hicks9f376222010-03-25 11:16:56 -0500432 "[%d] on lower_dentry = [%s]\n", __func__, rc,
433 encrypted_and_encoded_name);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800434 goto out_d_drop;
435 }
436 if (lower_dentry->d_inode)
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500437 goto interpose;
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500438 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 Hicks5ccf9202011-05-24 02:16:51 -0500442 goto interpose;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800443 dput(lower_dentry);
444 rc = ecryptfs_encrypt_and_encode_filename(
445 &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500446 NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name,
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800447 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 Hicks8787c7a2011-02-17 18:51:24 -0600453 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 Halcrowaddd65ad2009-01-06 14:42:00 -0800458 if (IS_ERR(lower_dentry)) {
459 rc = PTR_ERR(lower_dentry);
Tyler Hicks8787c7a2011-02-17 18:51:24 -0600460 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
Tyler Hicks9f376222010-03-25 11:16:56 -0500461 "[%d] on lower_dentry = [%s]\n", __func__, rc,
462 encrypted_and_encoded_name);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800463 goto out_d_drop;
464 }
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500465interpose:
466 rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
467 ecryptfs_dir_inode);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800468 goto out;
469out_d_drop:
470 d_drop(ecryptfs_dentry);
471out:
472 kfree(encrypted_and_encoded_name);
Michael Halcrow237fead2006-10-04 02:16:22 -0700473 return ERR_PTR(rc);
474}
475
476static 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 Hicks5ccf9202011-05-24 02:16:51 -0500495 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700496 if (rc)
497 goto out_lock;
Tyler Hicks3a8380c2010-03-23 18:09:02 -0500498 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
499 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700500 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);
503out_lock:
504 unlock_dir(lower_dir_dentry);
505 dput(lower_new_dentry);
506 dput(lower_old_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700507 return rc;
508}
509
510static 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 Szeredi8dc4e372008-05-12 14:02:04 -0700515 struct dentry *lower_dir_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700516
Tyler Hicks9c2d2052009-09-22 12:52:17 -0500517 dget(lower_dentry);
Miklos Szeredi8dc4e372008-05-12 14:02:04 -0700518 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700519 rc = vfs_unlink(lower_dir_inode, lower_dentry);
520 if (rc) {
Michael Halcrowae56fb12006-11-16 01:19:30 -0800521 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700522 goto out_unlock;
523 }
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800524 fsstack_copy_attr_times(dir, lower_dir_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700525 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 Halcrowcaeeeec2008-01-08 15:33:02 -0800528 d_drop(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700529out_unlock:
Miklos Szeredi8dc4e372008-05-12 14:02:04 -0700530 unlock_dir(lower_dir_dentry);
Tyler Hicks9c2d2052009-09-22 12:52:17 -0500531 dput(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700532 return rc;
533}
534
535static 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 Halcrow237fead2006-10-04 02:16:22 -0700541 char *encoded_symname;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800542 size_t encoded_symlen;
543 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700544
545 lower_dentry = ecryptfs_dentry_to_lower(dentry);
546 dget(lower_dentry);
547 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800548 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 Halcrow237fead2006-10-04 02:16:22 -0700556 goto out_lock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700557 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
Miklos Szeredidb2e7472008-06-24 16:50:16 +0200558 encoded_symname);
Michael Halcrow237fead2006-10-04 02:16:22 -0700559 kfree(encoded_symname);
560 if (rc || !lower_dentry->d_inode)
561 goto out_lock;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500562 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700563 if (rc)
564 goto out_lock;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800565 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
566 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700567out_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
575static 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 Hicks5ccf9202011-05-24 02:16:51 -0500586 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700587 if (rc)
588 goto out;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800589 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
590 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700591 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
592out:
593 unlock_dir(lower_dir_dentry);
594 if (!dentry->d_inode)
595 d_drop(dentry);
596 return rc;
597}
598
599static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
600{
Michael Halcrow237fead2006-10-04 02:16:22 -0700601 struct dentry *lower_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700602 struct dentry *lower_dir_dentry;
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800603 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700604
605 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800606 dget(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700607 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800608 dget(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700609 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800610 dput(lower_dentry);
Tyler Hicks07850552011-04-29 16:26:27 -0500611 if (!rc && dentry->d_inode)
612 clear_nlink(dentry->d_inode);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800613 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700614 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
615 unlock_dir(lower_dir_dentry);
616 if (!rc)
617 d_drop(dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800618 dput(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700619 return rc;
620}
621
622static int
623ecryptfs_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 Hicks5ccf9202011-05-24 02:16:51 -0500634 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700635 if (rc)
636 goto out;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800637 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
638 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700639out:
640 unlock_dir(lower_dir_dentry);
641 if (!dentry->d_inode)
642 d_drop(dentry);
643 return rc;
644}
645
646static int
647ecryptfs_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 Zadok0d132f72009-12-05 21:17:09 -0500655 struct dentry *trap = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700656
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 Zadok0d132f72009-12-05 21:17:09 -0500663 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 Halcrow237fead2006-10-04 02:16:22 -0700674 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 Zadok9afa2fb2009-12-02 19:51:54 -0500678 fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700679 if (new_dir != old_dir)
Erez Zadok9afa2fb2009-12-02 19:51:54 -0500680 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700681out_lock:
682 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
Tyler Hicksdd55c892011-04-12 11:23:09 -0500683 dput(lower_new_dir_dentry);
684 dput(lower_old_dir_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700685 dput(lower_new_dentry);
686 dput(lower_old_dentry);
687 return rc;
688}
689
Tyler Hicks3a60a162010-03-22 00:41:35 -0500690static int ecryptfs_readlink_lower(struct dentry *dentry, char **buf,
691 size_t *bufsiz)
Michael Halcrow237fead2006-10-04 02:16:22 -0700692{
Tyler Hicks3a60a162010-03-22 00:41:35 -0500693 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700694 char *lower_buf;
Tyler Hicks3a60a162010-03-22 00:41:35 -0500695 size_t lower_bufsiz = PATH_MAX;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800696 mm_segment_t old_fs;
697 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700698
Tyler Hicks3a6b42c2009-04-16 18:35:37 -0500699 lower_buf = kmalloc(lower_bufsiz, GFP_KERNEL);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500700 if (!lower_buf) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700701 rc = -ENOMEM;
702 goto out;
703 }
704 old_fs = get_fs();
705 set_fs(get_ds());
Michael Halcrow237fead2006-10-04 02:16:22 -0700706 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
707 (char __user *)lower_buf,
Tyler Hicks3a6b42c2009-04-16 18:35:37 -0500708 lower_bufsiz);
Michael Halcrow237fead2006-10-04 02:16:22 -0700709 set_fs(old_fs);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500710 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);
715out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700716 kfree(lower_buf);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500717 return rc;
718}
719
720static int
721ecryptfs_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 Halcrow237fead2006-10-04 02:16:22 -0700735out:
736 return rc;
737}
738
739static 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 Hirofumi806892e2010-01-12 03:36:14 +0900748 buf = ERR_PTR(-ENOMEM);
Michael Halcrow237fead2006-10-04 02:16:22 -0700749 goto out;
750 }
751 old_fs = get_fs();
752 set_fs(get_ds());
Michael Halcrow237fead2006-10-04 02:16:22 -0700753 rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
Michael Halcrow237fead2006-10-04 02:16:22 -0700754 set_fs(old_fs);
OGAWA Hirofumi806892e2010-01-12 03:36:14 +0900755 if (rc < 0) {
756 kfree(buf);
757 buf = ERR_PTR(rc);
758 } else
Duane Griffina17d5232008-12-19 20:47:10 +0000759 buf[rc] = '\0';
Michael Halcrow237fead2006-10-04 02:16:22 -0700760out:
OGAWA Hirofumi806892e2010-01-12 03:36:14 +0900761 nd_set_link(nd, buf);
762 return NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700763}
764
765static void
766ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
767{
OGAWA Hirofumi806892e2010-01-12 03:36:14 +0900768 char *buf = nd_get_link(nd);
769 if (!IS_ERR(buf)) {
770 /* Free the char* */
771 kfree(buf);
772 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700773}
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 Halcrowcc11bef2008-02-06 01:38:32 -0800780 * Calculate the required size of the lower file based on the
Michael Halcrow237fead2006-10-04 02:16:22 -0700781 * 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 */
786static loff_t
787upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
788 loff_t upper_size)
789{
790 loff_t lower_size;
791
Tyler Hicks157f1072010-02-11 07:10:38 -0600792 lower_size = ecryptfs_lower_header_size(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700793 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 Hicks5f3ef642009-10-14 16:18:27 -0500805 * truncate_upper
Michael Halcrow237fead2006-10-04 02:16:22 -0700806 * @dentry: The ecryptfs layer dentry
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500807 * @ia: Address of the ecryptfs inode's attributes
808 * @lower_ia: Address of the lower inode's attributes
Michael Halcrow237fead2006-10-04 02:16:22 -0700809 *
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 Hicks5f3ef642009-10-14 16:18:27 -0500812 * 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 Halcrow237fead2006-10-04 02:16:22 -0700817 *
818 * Returns zero on success; non-zero otherwise
819 */
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500820static int truncate_upper(struct dentry *dentry, struct iattr *ia,
821 struct iattr *lower_ia)
Michael Halcrow237fead2006-10-04 02:16:22 -0700822{
823 int rc = 0;
824 struct inode *inode = dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700825 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 Hicks5f3ef642009-10-14 16:18:27 -0500830 if (unlikely((ia->ia_size == i_size))) {
831 lower_ia->ia_valid &= ~ATTR_SIZE;
Tyler Hicks332ab162011-04-14 15:35:11 -0500832 return 0;
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500833 }
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500834 rc = ecryptfs_get_lower_file(dentry, inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500835 if (rc)
836 return rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700837 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700838 /* Switch on growing or shrinking file */
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500839 if (ia->ia_size > i_size) {
Michael Halcrow2ed92552007-10-16 01:28:10 -0700840 char zero[] = { 0x00 };
Michael Halcrow240e2df2007-06-27 14:09:44 -0700841
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500842 lower_ia->ia_valid &= ~ATTR_SIZE;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700843 /* 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 Viro48c1e442010-05-21 11:09:58 -0400847 rc = ecryptfs_write(inode, zero,
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500848 (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 Halcrow2ed92552007-10-16 01:28:10 -0700853 * PAGE_CACHE_SIZE with zeros. */
854 size_t num_zeros = (PAGE_CACHE_SIZE
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500855 - (ia->ia_size & ~PAGE_CACHE_MASK));
Michael Halcrow2ed92552007-10-16 01:28:10 -0700856
Tyler Hicks13a791b2009-04-13 15:29:27 -0500857 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200858 truncate_setsize(inode, ia->ia_size);
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500859 lower_ia->ia_size = ia->ia_size;
860 lower_ia->ia_valid |= ATTR_SIZE;
Al Viro48c1e442010-05-21 11:09:58 -0400861 goto out;
Tyler Hicks13a791b2009-04-13 15:29:27 -0500862 }
Michael Halcrow2ed92552007-10-16 01:28:10 -0700863 if (num_zeros) {
864 char *zeros_virt;
865
866 zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
867 if (!zeros_virt) {
868 rc = -ENOMEM;
Al Viro48c1e442010-05-21 11:09:58 -0400869 goto out;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700870 }
Al Viro48c1e442010-05-21 11:09:58 -0400871 rc = ecryptfs_write(inode, zeros_virt,
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500872 ia->ia_size, num_zeros);
Michael Halcrow2ed92552007-10-16 01:28:10 -0700873 kfree(zeros_virt);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700874 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700875 printk(KERN_ERR "Error attempting to zero out "
876 "the remainder of the end page on "
877 "reducing truncate; rc = [%d]\n", rc);
Al Viro48c1e442010-05-21 11:09:58 -0400878 goto out;
Michael Halcrow240e2df2007-06-27 14:09:44 -0700879 }
880 }
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200881 truncate_setsize(inode, ia->ia_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700882 rc = ecryptfs_write_inode_size_to_metadata(inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800883 if (rc) {
884 printk(KERN_ERR "Problem with "
885 "ecryptfs_write_inode_size_to_metadata; "
886 "rc = [%d]\n", rc);
Al Viro48c1e442010-05-21 11:09:58 -0400887 goto out;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800888 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700889 /* 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 Hicks5f3ef642009-10-14 16:18:27 -0500894 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 Halcrow237fead2006-10-04 02:16:22 -0700900 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700901out:
Tyler Hicks332ab162011-04-14 15:35:11 -0500902 ecryptfs_put_lower_file(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700903 return rc;
904}
905
Tyler Hicks714ca4e2012-01-19 20:33:44 -0600906static 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 Hicks5f3ef642009-10-14 16:18:27 -0500928/**
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 */
938int 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 Hicks714ca4e2012-01-19 20:33:44 -0600944 rc = ecryptfs_inode_newsize_ok(dentry->d_inode, new_length);
945 if (rc)
946 return rc;
947
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500948 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 Halcrow237fead2006-10-04 02:16:22 -0700959static int
Nick Pigginb74c79e2011-01-07 17:49:58 +1100960ecryptfs_permission(struct inode *inode, int mask, unsigned int flags)
Michael Halcrow237fead2006-10-04 02:16:22 -0700961{
Nick Pigginb74c79e2011-01-07 17:49:58 +1100962 if (flags & IPERM_FLAG_RCU)
963 return -ECHILD;
Al Virof419a2e2008-07-22 00:07:17 -0400964 return inode_permission(ecryptfs_inode_to_lower(inode), mask);
Michael Halcrow237fead2006-10-04 02:16:22 -0700965}
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 */
979static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
980{
981 int rc = 0;
982 struct dentry *lower_dentry;
Tyler Hicks5f3ef642009-10-14 16:18:27 -0500983 struct iattr lower_ia;
Michael Halcrow237fead2006-10-04 02:16:22 -0700984 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 Halcrowe10f2812007-06-27 14:09:44 -0700989 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
990 ecryptfs_init_crypt_stat(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700991 inode = dentry->d_inode;
992 lower_inode = ecryptfs_inode_to_lower(inode);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700993 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 Halcrow64ee4802007-07-19 01:47:54 -0700997 else if (S_ISREG(dentry->d_inode->i_mode)
998 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
999 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
Michael Halcrowe10f2812007-06-27 14:09:44 -07001000 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrowe10f2812007-06-27 14:09:44 -07001001
Michael Halcrowe10f2812007-06-27 14:09:44 -07001002 mount_crypt_stat = &ecryptfs_superblock_to_private(
1003 dentry->d_sb)->mount_crypt_stat;
Tyler Hicks3b06b3e2011-05-24 03:49:02 -05001004 rc = ecryptfs_get_lower_file(dentry, inode);
Tyler Hicks332ab162011-04-14 15:35:11 -05001005 if (rc) {
1006 mutex_unlock(&crypt_stat->cs_mutex);
1007 goto out;
1008 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001009 rc = ecryptfs_read_metadata(dentry);
Tyler Hicks332ab162011-04-14 15:35:11 -05001010 ecryptfs_put_lower_file(inode);
Michael Halcrow5dda6992007-10-16 01:28:06 -07001011 if (rc) {
Michael Halcrowe10f2812007-06-27 14:09:44 -07001012 if (!(mount_crypt_stat->flags
1013 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
1014 rc = -EIO;
Michael Halcrow25bd8172008-02-06 01:38:35 -08001015 printk(KERN_WARNING "Either the lower file "
Michael Halcrowe10f2812007-06-27 14:09:44 -07001016 "is not in a valid eCryptfs format, "
Michael Halcrow25bd8172008-02-06 01:38:35 -08001017 "or the key could not be retrieved. "
1018 "Plaintext passthrough mode is not "
Michael Halcrowe10f2812007-06-27 14:09:44 -07001019 "enabled; returning -EIO\n");
Michael Halcrowe10f2812007-06-27 14:09:44 -07001020 mutex_unlock(&crypt_stat->cs_mutex);
Michael Halcrowe10f2812007-06-27 14:09:44 -07001021 goto out;
1022 }
1023 rc = 0;
Tyler Hicks3aeb86e2011-03-15 14:54:00 -05001024 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
1025 | ECRYPTFS_ENCRYPTED);
Michael Halcrowe10f2812007-06-27 14:09:44 -07001026 }
Michael Halcrowe10f2812007-06-27 14:09:44 -07001027 }
1028 mutex_unlock(&crypt_stat->cs_mutex);
Tyler Hicks714ca4e2012-01-19 20:33:44 -06001029
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 Hicks5be79de2011-04-22 13:08:00 -05001039 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 Hicks5f3ef642009-10-14 16:18:27 -05001045 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 Halcrow237fead2006-10-04 02:16:22 -07001048 if (ia->ia_valid & ATTR_SIZE) {
Tyler Hicks5f3ef642009-10-14 16:18:27 -05001049 rc = truncate_upper(dentry, ia, &lower_ia);
Michael Halcrow237fead2006-10-04 02:16:22 -07001050 if (rc < 0)
1051 goto out;
1052 }
Jeff Layton1ac564e2007-10-18 03:05:17 -07001053
1054 /*
1055 * mode change is for clearing setuid/setgid bits. Allow lower fs
1056 * to interpret this in its own way.
1057 */
Tyler Hicks5f3ef642009-10-14 16:18:27 -05001058 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
1059 lower_ia.ia_valid &= ~ATTR_MODE;
Jeff Layton1ac564e2007-10-18 03:05:17 -07001060
Miklos Szeredi9c3580a2008-04-29 00:59:48 -07001061 mutex_lock(&lower_dentry->d_inode->i_mutex);
Tyler Hicks5f3ef642009-10-14 16:18:27 -05001062 rc = notify_change(lower_dentry, &lower_ia);
Miklos Szeredi9c3580a2008-04-29 00:59:48 -07001063 mutex_unlock(&lower_dentry->d_inode->i_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001064out:
Erez Zadok9afa2fb2009-12-02 19:51:54 -05001065 fsstack_copy_attr_all(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -07001066 return rc;
1067}
1068
Tyler Hicks3a60a162010-03-22 00:41:35 -05001069int 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 Hicksf8f484d2009-11-04 02:48:01 -06001091int 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 Hicks55f9cf62011-01-11 12:43:42 -06001100 fsstack_copy_attr_all(dentry->d_inode,
1101 ecryptfs_inode_to_lower(dentry->d_inode));
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001102 generic_fillattr(dentry->d_inode, stat);
1103 stat->blocks = lower_stat.blocks;
1104 }
1105 return rc;
1106}
1107
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001108int
Michael Halcrow237fead2006-10-04 02:16:22 -07001109ecryptfs_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 Pulvermachercfce08c2010-03-23 11:51:38 -05001117 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001118 goto out;
1119 }
Roberto Sassu48b512e2010-10-05 18:53:45 +02001120
1121 rc = vfs_setxattr(lower_dentry, name, value, size, flags);
Michael Halcrow237fead2006-10-04 02:16:22 -07001122out:
1123 return rc;
1124}
1125
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001126ssize_t
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001127ecryptfs_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 Pulvermachercfce08c2010-03-23 11:51:38 -05001133 rc = -EOPNOTSUPP;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001134 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);
1140out:
1141 return rc;
1142}
1143
Adrian Bunk7896b632008-02-06 01:38:32 -08001144static ssize_t
Michael Halcrow237fead2006-10-04 02:16:22 -07001145ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
1146 size_t size)
1147{
Michael Halcrow2ed92552007-10-16 01:28:10 -07001148 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
1149 value, size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001150}
1151
1152static ssize_t
1153ecryptfs_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 Pulvermachercfce08c2010-03-23 11:51:38 -05001160 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001161 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);
1166out:
1167 return rc;
1168}
1169
1170static 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 Pulvermachercfce08c2010-03-23 11:51:38 -05001177 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001178 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);
1183out:
1184 return rc;
1185}
1186
Arjan van de Ven754661f2007-02-12 00:55:38 -08001187const struct inode_operations ecryptfs_symlink_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001188 .readlink = ecryptfs_readlink,
1189 .follow_link = ecryptfs_follow_link,
1190 .put_link = ecryptfs_put_link,
1191 .permission = ecryptfs_permission,
1192 .setattr = ecryptfs_setattr,
Tyler Hicks3a60a162010-03-22 00:41:35 -05001193 .getattr = ecryptfs_getattr_link,
Michael Halcrow237fead2006-10-04 02:16:22 -07001194 .setxattr = ecryptfs_setxattr,
1195 .getxattr = ecryptfs_getxattr,
1196 .listxattr = ecryptfs_listxattr,
1197 .removexattr = ecryptfs_removexattr
1198};
1199
Arjan van de Ven754661f2007-02-12 00:55:38 -08001200const struct inode_operations ecryptfs_dir_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001201 .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 Ven754661f2007-02-12 00:55:38 -08001218const struct inode_operations ecryptfs_main_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001219 .permission = ecryptfs_permission,
1220 .setattr = ecryptfs_setattr,
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001221 .getattr = ecryptfs_getattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001222 .setxattr = ecryptfs_setxattr,
1223 .getxattr = ecryptfs_getxattr,
1224 .listxattr = ecryptfs_listxattr,
1225 .removexattr = ecryptfs_removexattr
1226};