blob: a27121d35b231c76e4de81f87248fd6e5c7375f5 [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{
Himangi Saraogic4cf3ba2014-06-27 01:11:59 +053056 return ecryptfs_inode_to_lower(inode) == lower_inode;
Tyler Hicksc4f79072011-05-23 21:18:20 -050057}
58
Tyler Hicks5ccf9202011-05-24 02:16:51 -050059static int ecryptfs_inode_set(struct inode *inode, void *opaque)
Tyler Hicksc4f79072011-05-23 21:18:20 -050060{
Tyler Hicks5ccf9202011-05-24 02:16:51 -050061 struct inode *lower_inode = opaque;
62
63 ecryptfs_set_inode_lower(inode, lower_inode);
64 fsstack_copy_attr_all(inode, lower_inode);
65 /* i_size will be overwritten for encrypted regular files */
66 fsstack_copy_inode_size(inode, lower_inode);
67 inode->i_ino = lower_inode->i_ino;
Tyler Hicksc4f79072011-05-23 21:18:20 -050068 inode->i_version++;
Tyler Hicksc4f79072011-05-23 21:18:20 -050069 inode->i_mapping->a_ops = &ecryptfs_aops;
Thieu Le985ca0e2011-07-26 16:15:10 -070070 inode->i_mapping->backing_dev_info = inode->i_sb->s_bdi;
Tyler Hicks5ccf9202011-05-24 02:16:51 -050071
72 if (S_ISLNK(inode->i_mode))
73 inode->i_op = &ecryptfs_symlink_iops;
74 else if (S_ISDIR(inode->i_mode))
75 inode->i_op = &ecryptfs_dir_iops;
76 else
77 inode->i_op = &ecryptfs_main_iops;
78
79 if (S_ISDIR(inode->i_mode))
80 inode->i_fop = &ecryptfs_dir_fops;
81 else if (special_file(inode->i_mode))
82 init_special_inode(inode, inode->i_mode, inode->i_rdev);
83 else
84 inode->i_fop = &ecryptfs_main_fops;
85
Tyler Hicksc4f79072011-05-23 21:18:20 -050086 return 0;
87}
88
Tyler Hicks5ccf9202011-05-24 02:16:51 -050089static struct inode *__ecryptfs_get_inode(struct inode *lower_inode,
90 struct super_block *sb)
91{
92 struct inode *inode;
93
94 if (lower_inode->i_sb != ecryptfs_superblock_to_lower(sb))
95 return ERR_PTR(-EXDEV);
96 if (!igrab(lower_inode))
97 return ERR_PTR(-ESTALE);
98 inode = iget5_locked(sb, (unsigned long)lower_inode,
99 ecryptfs_inode_test, ecryptfs_inode_set,
100 lower_inode);
101 if (!inode) {
102 iput(lower_inode);
103 return ERR_PTR(-EACCES);
104 }
105 if (!(inode->i_state & I_NEW))
106 iput(lower_inode);
107
108 return inode;
109}
110
Tyler Hicksc4f79072011-05-23 21:18:20 -0500111struct inode *ecryptfs_get_inode(struct inode *lower_inode,
112 struct super_block *sb)
113{
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500114 struct inode *inode = __ecryptfs_get_inode(lower_inode, sb);
Tyler Hicksc4f79072011-05-23 21:18:20 -0500115
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500116 if (!IS_ERR(inode) && (inode->i_state & I_NEW))
Tyler Hicksc4f79072011-05-23 21:18:20 -0500117 unlock_new_inode(inode);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500118
Tyler Hicksc4f79072011-05-23 21:18:20 -0500119 return inode;
Tyler Hicksc4f79072011-05-23 21:18:20 -0500120}
121
Tyler Hicksc4f79072011-05-23 21:18:20 -0500122/**
123 * ecryptfs_interpose
124 * @lower_dentry: Existing dentry in the lower filesystem
125 * @dentry: ecryptfs' dentry
126 * @sb: ecryptfs's super_block
Tyler Hicksc4f79072011-05-23 21:18:20 -0500127 *
128 * Interposes upper and lower dentries.
129 *
130 * Returns zero on success; non-zero otherwise
131 */
132static int ecryptfs_interpose(struct dentry *lower_dentry,
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500133 struct dentry *dentry, struct super_block *sb)
Tyler Hicksc4f79072011-05-23 21:18:20 -0500134{
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500135 struct inode *inode = ecryptfs_get_inode(lower_dentry->d_inode, sb);
136
Tyler Hicksc4f79072011-05-23 21:18:20 -0500137 if (IS_ERR(inode))
138 return PTR_ERR(inode);
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500139 d_instantiate(dentry, inode);
140
Tyler Hicksc4f79072011-05-23 21:18:20 -0500141 return 0;
142}
143
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500144static int ecryptfs_do_unlink(struct inode *dir, struct dentry *dentry,
145 struct inode *inode)
146{
147 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
148 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
149 struct dentry *lower_dir_dentry;
150 int rc;
151
152 dget(lower_dentry);
153 lower_dir_dentry = lock_parent(lower_dentry);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -0400154 rc = vfs_unlink(lower_dir_inode, lower_dentry, NULL);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500155 if (rc) {
156 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
157 goto out_unlock;
158 }
159 fsstack_copy_attr_times(dir, lower_dir_inode);
160 set_nlink(inode, ecryptfs_inode_to_lower(inode)->i_nlink);
161 inode->i_ctime = dir->i_ctime;
162 d_drop(dentry);
163out_unlock:
164 unlock_dir(lower_dir_dentry);
165 dput(lower_dentry);
166 return rc;
167}
168
Michael Halcrow237fead2006-10-04 02:16:22 -0700169/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700170 * ecryptfs_do_create
171 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
172 * @ecryptfs_dentry: New file's dentry in ecryptfs
173 * @mode: The mode of the new file
174 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
175 *
176 * Creates the underlying file and the eCryptfs inode which will link to
177 * it. It will also update the eCryptfs directory inode to mimic the
178 * stat of the lower directory inode.
179 *
Tyler Hicksb59db432011-11-21 17:31:02 -0600180 * Returns the new eCryptfs inode on success; an ERR_PTR on error condition
Michael Halcrow237fead2006-10-04 02:16:22 -0700181 */
Tyler Hicksb59db432011-11-21 17:31:02 -0600182static struct inode *
Michael Halcrow237fead2006-10-04 02:16:22 -0700183ecryptfs_do_create(struct inode *directory_inode,
Al Viro175a4eb2011-07-26 03:30:54 -0400184 struct dentry *ecryptfs_dentry, umode_t mode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700185{
186 int rc;
187 struct dentry *lower_dentry;
188 struct dentry *lower_dir_dentry;
Tyler Hicksb59db432011-11-21 17:31:02 -0600189 struct inode *inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700190
191 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
192 lower_dir_dentry = lock_parent(lower_dentry);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700193 if (IS_ERR(lower_dir_dentry)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700194 ecryptfs_printk(KERN_ERR, "Error locking directory of "
195 "dentry\n");
Tyler Hicksb59db432011-11-21 17:31:02 -0600196 inode = ERR_CAST(lower_dir_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700197 goto out;
198 }
Al Viro312b63f2012-06-10 18:09:36 -0400199 rc = vfs_create(lower_dir_dentry->d_inode, lower_dentry, mode, true);
Michael Halcrow4981e082007-10-16 01:28:09 -0700200 if (rc) {
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800201 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700202 "rc = [%d]\n", __func__, rc);
Tyler Hicksb59db432011-11-21 17:31:02 -0600203 inode = ERR_PTR(rc);
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800204 goto out_lock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700205 }
Tyler Hicksb59db432011-11-21 17:31:02 -0600206 inode = __ecryptfs_get_inode(lower_dentry->d_inode,
207 directory_inode->i_sb);
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500208 if (IS_ERR(inode)) {
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -0400209 vfs_unlink(lower_dir_dentry->d_inode, lower_dentry, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700210 goto out_lock;
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500211 }
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800212 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
213 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700214out_lock:
215 unlock_dir(lower_dir_dentry);
216out:
Tyler Hicksb59db432011-11-21 17:31:02 -0600217 return inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700218}
219
220/**
Michael Halcrow237fead2006-10-04 02:16:22 -0700221 * ecryptfs_initialize_file
222 *
223 * Cause the file to be changed from a basic empty file to an ecryptfs
224 * file with a header and first data page.
225 *
226 * Returns zero on success
227 */
Tyler Hickse3ccaa92012-06-20 23:50:59 -0700228int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry,
229 struct inode *ecryptfs_inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700230{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700231 struct ecryptfs_crypt_stat *crypt_stat =
Tyler Hicksb59db432011-11-21 17:31:02 -0600232 &ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700233 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700234
Tyler Hicksb59db432011-11-21 17:31:02 -0600235 if (S_ISDIR(ecryptfs_inode->i_mode)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700236 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800237 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700238 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700239 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700240 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
Tyler Hicksb59db432011-11-21 17:31:02 -0600241 rc = ecryptfs_new_file_context(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700242 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700243 ecryptfs_printk(KERN_ERR, "Error creating new file "
244 "context; rc = [%d]\n", rc);
245 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700246 }
Tyler Hicksb59db432011-11-21 17:31:02 -0600247 rc = ecryptfs_get_lower_file(ecryptfs_dentry, ecryptfs_inode);
Roberto Sassu27992892010-11-03 11:11:28 +0100248 if (rc) {
249 printk(KERN_ERR "%s: Error attempting to initialize "
Tyler Hicks332ab162011-04-14 15:35:11 -0500250 "the lower file for the dentry with name "
David Howells9e78d142013-12-10 15:26:48 +0000251 "[%pd]; rc = [%d]\n", __func__,
252 ecryptfs_dentry, rc);
Roberto Sassu27992892010-11-03 11:11:28 +0100253 goto out;
Michael Halcrow391b52f2008-07-23 21:30:08 -0700254 }
Tyler Hicksb59db432011-11-21 17:31:02 -0600255 rc = ecryptfs_write_metadata(ecryptfs_dentry, ecryptfs_inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500256 if (rc)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700257 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
Tyler Hicksb59db432011-11-21 17:31:02 -0600258 ecryptfs_put_lower_file(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700259out:
260 return rc;
261}
262
263/**
264 * ecryptfs_create
265 * @dir: The inode of the directory in which to create the file.
266 * @dentry: The eCryptfs dentry
267 * @mode: The mode of the new file.
Michael Halcrow237fead2006-10-04 02:16:22 -0700268 *
269 * Creates a new file.
270 *
271 * Returns zero on success; non-zero on error condition
272 */
273static int
274ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
Al Viroebfc3b42012-06-10 18:05:36 -0400275 umode_t mode, bool excl)
Michael Halcrow237fead2006-10-04 02:16:22 -0700276{
Tyler Hicksb59db432011-11-21 17:31:02 -0600277 struct inode *ecryptfs_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700278 int rc;
279
Tyler Hicksb59db432011-11-21 17:31:02 -0600280 ecryptfs_inode = ecryptfs_do_create(directory_inode, ecryptfs_dentry,
281 mode);
282 if (unlikely(IS_ERR(ecryptfs_inode))) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700283 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
284 "lower filesystem\n");
Tyler Hicksb59db432011-11-21 17:31:02 -0600285 rc = PTR_ERR(ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700286 goto out;
287 }
288 /* At this point, a file exists on "disk"; we need to make sure
289 * that this on disk file is prepared to be an ecryptfs file */
Tyler Hicksb59db432011-11-21 17:31:02 -0600290 rc = ecryptfs_initialize_file(ecryptfs_dentry, ecryptfs_inode);
291 if (rc) {
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500292 ecryptfs_do_unlink(directory_inode, ecryptfs_dentry,
293 ecryptfs_inode);
294 make_bad_inode(ecryptfs_inode);
Tyler Hicksb59db432011-11-21 17:31:02 -0600295 unlock_new_inode(ecryptfs_inode);
296 iput(ecryptfs_inode);
297 goto out;
298 }
Tyler Hicksb59db432011-11-21 17:31:02 -0600299 unlock_new_inode(ecryptfs_inode);
Al Viro8fc37ec2012-07-19 09:18:15 +0400300 d_instantiate(ecryptfs_dentry, ecryptfs_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700301out:
302 return rc;
303}
304
Tyler Hicks778aeb42011-05-24 04:56:23 -0500305static int ecryptfs_i_size_read(struct dentry *dentry, struct inode *inode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700306{
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500307 struct ecryptfs_crypt_stat *crypt_stat;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500308 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700309
Tyler Hicks778aeb42011-05-24 04:56:23 -0500310 rc = ecryptfs_get_lower_file(dentry, inode);
Roberto Sassu27992892010-11-03 11:11:28 +0100311 if (rc) {
312 printk(KERN_ERR "%s: Error attempting to initialize "
Tyler Hicks332ab162011-04-14 15:35:11 -0500313 "the lower file for the dentry with name "
David Howells9e78d142013-12-10 15:26:48 +0000314 "[%pd]; rc = [%d]\n", __func__,
315 dentry, rc);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500316 return rc;
Michael Halcrow391b52f2008-07-23 21:30:08 -0700317 }
Tyler Hicks778aeb42011-05-24 04:56:23 -0500318
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500319 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500320 /* TODO: lock for crypt_stat comparison */
321 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
Tyler Hicks778aeb42011-05-24 04:56:23 -0500322 ecryptfs_set_default_sizes(crypt_stat);
323
324 rc = ecryptfs_read_and_validate_header_region(inode);
325 ecryptfs_put_lower_file(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700326 if (rc) {
Tyler Hicks778aeb42011-05-24 04:56:23 -0500327 rc = ecryptfs_read_and_validate_xattr_region(dentry, inode);
328 if (!rc)
329 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
Michael Halcrow237fead2006-10-04 02:16:22 -0700330 }
Tyler Hicks778aeb42011-05-24 04:56:23 -0500331
332 /* Must return 0 to allow non-eCryptfs files to be looked up, too */
333 return 0;
334}
335
336/**
337 * ecryptfs_lookup_interpose - Dentry interposition for a lookup
338 */
339static int ecryptfs_lookup_interpose(struct dentry *dentry,
340 struct dentry *lower_dentry,
341 struct inode *dir_inode)
342{
343 struct inode *inode, *lower_inode = lower_dentry->d_inode;
344 struct ecryptfs_dentry_info *dentry_info;
345 struct vfsmount *lower_mnt;
346 int rc = 0;
347
Tyler Hicks778aeb42011-05-24 04:56:23 -0500348 dentry_info = kmem_cache_alloc(ecryptfs_dentry_info_cache, GFP_KERNEL);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500349 if (!dentry_info) {
350 printk(KERN_ERR "%s: Out of memory whilst attempting "
351 "to allocate ecryptfs_dentry_info struct\n",
352 __func__);
353 dput(lower_dentry);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500354 return -ENOMEM;
355 }
Al Viro0b1d9012012-07-20 12:09:19 +0400356
357 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
358 fsstack_copy_attr_atime(dir_inode, lower_dentry->d_parent->d_inode);
Al Viro84d08fa2013-07-05 18:59:33 +0400359 BUG_ON(!d_count(lower_dentry));
Al Viro0b1d9012012-07-20 12:09:19 +0400360
361 ecryptfs_set_dentry_private(dentry, dentry_info);
Al Viro92dd1232013-09-15 20:50:13 -0400362 dentry_info->lower_path.mnt = lower_mnt;
363 dentry_info->lower_path.dentry = lower_dentry;
Tyler Hicks778aeb42011-05-24 04:56:23 -0500364
365 if (!lower_dentry->d_inode) {
366 /* We want to add because we couldn't find in lower */
367 d_add(dentry, NULL);
368 return 0;
369 }
370 inode = __ecryptfs_get_inode(lower_inode, dir_inode->i_sb);
371 if (IS_ERR(inode)) {
372 printk(KERN_ERR "%s: Error interposing; rc = [%ld]\n",
373 __func__, PTR_ERR(inode));
374 return PTR_ERR(inode);
375 }
376 if (S_ISREG(inode->i_mode)) {
377 rc = ecryptfs_i_size_read(dentry, inode);
378 if (rc) {
379 make_bad_inode(inode);
380 return rc;
381 }
382 }
383
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500384 if (inode->i_state & I_NEW)
385 unlock_new_inode(inode);
Tyler Hicks778aeb42011-05-24 04:56:23 -0500386 d_add(dentry, inode);
387
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800388 return rc;
389}
390
391/**
392 * ecryptfs_lookup
393 * @ecryptfs_dir_inode: The eCryptfs directory inode
394 * @ecryptfs_dentry: The eCryptfs dentry that we are looking up
395 * @ecryptfs_nd: nameidata; may be NULL
396 *
397 * Find a file on disk. If the file does not exist, then we'll add it to the
398 * dentry cache and continue on to read it from the disk.
399 */
400static struct dentry *ecryptfs_lookup(struct inode *ecryptfs_dir_inode,
401 struct dentry *ecryptfs_dentry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400402 unsigned int flags)
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800403{
404 char *encrypted_and_encoded_name = NULL;
Michael Halcrowa8f12862009-01-06 14:42:03 -0800405 size_t encrypted_and_encoded_name_size;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800406 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800407 struct dentry *lower_dir_dentry, *lower_dentry;
408 int rc = 0;
409
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800410 lower_dir_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry->d_parent);
Tyler Hicks8787c7a2011-02-17 18:51:24 -0600411 mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
412 lower_dentry = lookup_one_len(ecryptfs_dentry->d_name.name,
413 lower_dir_dentry,
414 ecryptfs_dentry->d_name.len);
415 mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800416 if (IS_ERR(lower_dentry)) {
417 rc = PTR_ERR(lower_dentry);
Tyler Hicks8787c7a2011-02-17 18:51:24 -0600418 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
David Howells9e78d142013-12-10 15:26:48 +0000419 "[%d] on lower_dentry = [%pd]\n", __func__, rc,
420 ecryptfs_dentry);
Al Virobc65a122012-07-20 12:03:41 +0400421 goto out;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800422 }
423 if (lower_dentry->d_inode)
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500424 goto interpose;
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500425 mount_crypt_stat = &ecryptfs_superblock_to_private(
426 ecryptfs_dentry->d_sb)->mount_crypt_stat;
427 if (!(mount_crypt_stat
428 && (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES)))
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500429 goto interpose;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800430 dput(lower_dentry);
431 rc = ecryptfs_encrypt_and_encode_filename(
432 &encrypted_and_encoded_name, &encrypted_and_encoded_name_size,
Tyler Hicks2aac0cf2009-03-20 02:23:57 -0500433 NULL, mount_crypt_stat, ecryptfs_dentry->d_name.name,
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800434 ecryptfs_dentry->d_name.len);
435 if (rc) {
436 printk(KERN_ERR "%s: Error attempting to encrypt and encode "
437 "filename; rc = [%d]\n", __func__, rc);
Al Virobc65a122012-07-20 12:03:41 +0400438 goto out;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800439 }
Tyler Hicks8787c7a2011-02-17 18:51:24 -0600440 mutex_lock(&lower_dir_dentry->d_inode->i_mutex);
441 lower_dentry = lookup_one_len(encrypted_and_encoded_name,
442 lower_dir_dentry,
443 encrypted_and_encoded_name_size);
444 mutex_unlock(&lower_dir_dentry->d_inode->i_mutex);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800445 if (IS_ERR(lower_dentry)) {
446 rc = PTR_ERR(lower_dentry);
Tyler Hicks8787c7a2011-02-17 18:51:24 -0600447 ecryptfs_printk(KERN_DEBUG, "%s: lookup_one_len() returned "
Tyler Hicks9f376222010-03-25 11:16:56 -0500448 "[%d] on lower_dentry = [%s]\n", __func__, rc,
449 encrypted_and_encoded_name);
Al Virobc65a122012-07-20 12:03:41 +0400450 goto out;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800451 }
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500452interpose:
453 rc = ecryptfs_lookup_interpose(ecryptfs_dentry, lower_dentry,
454 ecryptfs_dir_inode);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800455out:
456 kfree(encrypted_and_encoded_name);
Michael Halcrow237fead2006-10-04 02:16:22 -0700457 return ERR_PTR(rc);
458}
459
460static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
461 struct dentry *new_dentry)
462{
463 struct dentry *lower_old_dentry;
464 struct dentry *lower_new_dentry;
465 struct dentry *lower_dir_dentry;
466 u64 file_size_save;
467 int rc;
468
469 file_size_save = i_size_read(old_dentry->d_inode);
470 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
471 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
472 dget(lower_old_dentry);
473 dget(lower_new_dentry);
474 lower_dir_dentry = lock_parent(lower_new_dentry);
475 rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
J. Bruce Fields146a8592011-09-20 17:14:31 -0400476 lower_new_dentry, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700477 if (rc || !lower_new_dentry->d_inode)
478 goto out_lock;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500479 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700480 if (rc)
481 goto out_lock;
Tyler Hicks3a8380c2010-03-23 18:09:02 -0500482 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
483 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200484 set_nlink(old_dentry->d_inode,
485 ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink);
Michael Halcrow237fead2006-10-04 02:16:22 -0700486 i_size_write(new_dentry->d_inode, file_size_save);
487out_lock:
488 unlock_dir(lower_dir_dentry);
489 dput(lower_new_dentry);
490 dput(lower_old_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700491 return rc;
492}
493
494static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
495{
Tyler Hicks8bc2d3c2012-05-22 15:09:50 -0500496 return ecryptfs_do_unlink(dir, dentry, dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700497}
498
499static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
500 const char *symname)
501{
502 int rc;
503 struct dentry *lower_dentry;
504 struct dentry *lower_dir_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700505 char *encoded_symname;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800506 size_t encoded_symlen;
507 struct ecryptfs_mount_crypt_stat *mount_crypt_stat = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700508
509 lower_dentry = ecryptfs_dentry_to_lower(dentry);
510 dget(lower_dentry);
511 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800512 mount_crypt_stat = &ecryptfs_superblock_to_private(
513 dir->i_sb)->mount_crypt_stat;
514 rc = ecryptfs_encrypt_and_encode_filename(&encoded_symname,
515 &encoded_symlen,
516 NULL,
517 mount_crypt_stat, symname,
518 strlen(symname));
519 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700520 goto out_lock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700521 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
Miklos Szeredidb2e7472008-06-24 16:50:16 +0200522 encoded_symname);
Michael Halcrow237fead2006-10-04 02:16:22 -0700523 kfree(encoded_symname);
524 if (rc || !lower_dentry->d_inode)
525 goto out_lock;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500526 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700527 if (rc)
528 goto out_lock;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800529 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
530 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700531out_lock:
532 unlock_dir(lower_dir_dentry);
533 dput(lower_dentry);
534 if (!dentry->d_inode)
535 d_drop(dentry);
536 return rc;
537}
538
Al Viro18bb1db2011-07-26 01:41:39 -0400539static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Michael Halcrow237fead2006-10-04 02:16:22 -0700540{
541 int rc;
542 struct dentry *lower_dentry;
543 struct dentry *lower_dir_dentry;
544
545 lower_dentry = ecryptfs_dentry_to_lower(dentry);
546 lower_dir_dentry = lock_parent(lower_dentry);
547 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
548 if (rc || !lower_dentry->d_inode)
549 goto out;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500550 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700551 if (rc)
552 goto out;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800553 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
554 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200555 set_nlink(dir, lower_dir_dentry->d_inode->i_nlink);
Michael Halcrow237fead2006-10-04 02:16:22 -0700556out:
557 unlock_dir(lower_dir_dentry);
558 if (!dentry->d_inode)
559 d_drop(dentry);
560 return rc;
561}
562
563static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
564{
Michael Halcrow237fead2006-10-04 02:16:22 -0700565 struct dentry *lower_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700566 struct dentry *lower_dir_dentry;
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800567 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700568
569 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800570 dget(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700571 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800572 dget(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700573 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800574 dput(lower_dentry);
Tyler Hicks07850552011-04-29 16:26:27 -0500575 if (!rc && dentry->d_inode)
576 clear_nlink(dentry->d_inode);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800577 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
Miklos Szeredibfe86842011-10-28 14:13:29 +0200578 set_nlink(dir, lower_dir_dentry->d_inode->i_nlink);
Michael Halcrow237fead2006-10-04 02:16:22 -0700579 unlock_dir(lower_dir_dentry);
580 if (!rc)
581 d_drop(dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800582 dput(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700583 return rc;
584}
585
586static int
Al Viro1a67aaf2011-07-26 01:52:52 -0400587ecryptfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Michael Halcrow237fead2006-10-04 02:16:22 -0700588{
589 int rc;
590 struct dentry *lower_dentry;
591 struct dentry *lower_dir_dentry;
592
593 lower_dentry = ecryptfs_dentry_to_lower(dentry);
594 lower_dir_dentry = lock_parent(lower_dentry);
595 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
596 if (rc || !lower_dentry->d_inode)
597 goto out;
Tyler Hicks5ccf9202011-05-24 02:16:51 -0500598 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb);
Michael Halcrow237fead2006-10-04 02:16:22 -0700599 if (rc)
600 goto out;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800601 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
602 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700603out:
604 unlock_dir(lower_dir_dentry);
605 if (!dentry->d_inode)
606 d_drop(dentry);
607 return rc;
608}
609
610static int
611ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
612 struct inode *new_dir, struct dentry *new_dentry)
613{
614 int rc;
615 struct dentry *lower_old_dentry;
616 struct dentry *lower_new_dentry;
617 struct dentry *lower_old_dir_dentry;
618 struct dentry *lower_new_dir_dentry;
Erez Zadok0d132f72009-12-05 21:17:09 -0500619 struct dentry *trap = NULL;
Tyler Hicks8335eaf2012-09-13 12:00:56 -0700620 struct inode *target_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700621
622 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
623 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
624 dget(lower_old_dentry);
625 dget(lower_new_dentry);
626 lower_old_dir_dentry = dget_parent(lower_old_dentry);
627 lower_new_dir_dentry = dget_parent(lower_new_dentry);
Tyler Hicks8335eaf2012-09-13 12:00:56 -0700628 target_inode = new_dentry->d_inode;
Erez Zadok0d132f72009-12-05 21:17:09 -0500629 trap = lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
630 /* source should not be ancestor of target */
631 if (trap == lower_old_dentry) {
632 rc = -EINVAL;
633 goto out_lock;
634 }
635 /* target should not be ancestor of source */
636 if (trap == lower_new_dentry) {
637 rc = -ENOTEMPTY;
638 goto out_lock;
639 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700640 rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -0400641 lower_new_dir_dentry->d_inode, lower_new_dentry,
642 NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700643 if (rc)
644 goto out_lock;
Tyler Hicks8335eaf2012-09-13 12:00:56 -0700645 if (target_inode)
646 fsstack_copy_attr_all(target_inode,
647 ecryptfs_inode_to_lower(target_inode));
Erez Zadok9afa2fb2009-12-02 19:51:54 -0500648 fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700649 if (new_dir != old_dir)
Erez Zadok9afa2fb2009-12-02 19:51:54 -0500650 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700651out_lock:
652 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
Tyler Hicksdd55c892011-04-12 11:23:09 -0500653 dput(lower_new_dir_dentry);
654 dput(lower_old_dir_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700655 dput(lower_new_dentry);
656 dput(lower_old_dentry);
657 return rc;
658}
659
Tyler Hicks3a60a162010-03-22 00:41:35 -0500660static int ecryptfs_readlink_lower(struct dentry *dentry, char **buf,
661 size_t *bufsiz)
Michael Halcrow237fead2006-10-04 02:16:22 -0700662{
Tyler Hicks3a60a162010-03-22 00:41:35 -0500663 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700664 char *lower_buf;
Michael Halcrowaddd65ad2009-01-06 14:42:00 -0800665 mm_segment_t old_fs;
666 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700667
Al Viro408bd622012-05-03 09:34:20 -0400668 lower_buf = kmalloc(PATH_MAX, GFP_KERNEL);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500669 if (!lower_buf) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700670 rc = -ENOMEM;
671 goto out;
672 }
673 old_fs = get_fs();
674 set_fs(get_ds());
Michael Halcrow237fead2006-10-04 02:16:22 -0700675 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
676 (char __user *)lower_buf,
Al Viro408bd622012-05-03 09:34:20 -0400677 PATH_MAX);
Michael Halcrow237fead2006-10-04 02:16:22 -0700678 set_fs(old_fs);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500679 if (rc < 0)
680 goto out;
Al Viro0747fdb2013-06-16 20:05:38 +0400681 rc = ecryptfs_decode_and_decrypt_filename(buf, bufsiz, dentry->d_sb,
Al Viro408bd622012-05-03 09:34:20 -0400682 lower_buf, rc);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500683out:
Michael Halcrow237fead2006-10-04 02:16:22 -0700684 kfree(lower_buf);
Tyler Hicks3a60a162010-03-22 00:41:35 -0500685 return rc;
686}
687
Michael Halcrow237fead2006-10-04 02:16:22 -0700688static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
689{
690 char *buf;
Al Viro408bd622012-05-03 09:34:20 -0400691 size_t len = PATH_MAX;
692 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700693
Al Viro408bd622012-05-03 09:34:20 -0400694 rc = ecryptfs_readlink_lower(dentry, &buf, &len);
695 if (rc)
Michael Halcrow237fead2006-10-04 02:16:22 -0700696 goto out;
Al Viro408bd622012-05-03 09:34:20 -0400697 fsstack_copy_attr_atime(dentry->d_inode,
698 ecryptfs_dentry_to_lower(dentry)->d_inode);
699 buf[len] = '\0';
Michael Halcrow237fead2006-10-04 02:16:22 -0700700out:
OGAWA Hirofumi806892e2010-01-12 03:36:14 +0900701 nd_set_link(nd, buf);
702 return NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700703}
704
Michael Halcrow237fead2006-10-04 02:16:22 -0700705/**
706 * upper_size_to_lower_size
707 * @crypt_stat: Crypt_stat associated with file
708 * @upper_size: Size of the upper file
709 *
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800710 * Calculate the required size of the lower file based on the
Michael Halcrow237fead2006-10-04 02:16:22 -0700711 * specified size of the upper file. This calculation is based on the
712 * number of headers in the underlying file and the extent size.
713 *
714 * Returns Calculated size of the lower file.
715 */
716static loff_t
717upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
718 loff_t upper_size)
719{
720 loff_t lower_size;
721
Tyler Hicks157f1072010-02-11 07:10:38 -0600722 lower_size = ecryptfs_lower_header_size(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700723 if (upper_size != 0) {
724 loff_t num_extents;
725
726 num_extents = upper_size >> crypt_stat->extent_shift;
727 if (upper_size & ~crypt_stat->extent_mask)
728 num_extents++;
729 lower_size += (num_extents * crypt_stat->extent_size);
730 }
731 return lower_size;
732}
733
734/**
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500735 * truncate_upper
Michael Halcrow237fead2006-10-04 02:16:22 -0700736 * @dentry: The ecryptfs layer dentry
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500737 * @ia: Address of the ecryptfs inode's attributes
738 * @lower_ia: Address of the lower inode's attributes
Michael Halcrow237fead2006-10-04 02:16:22 -0700739 *
740 * Function to handle truncations modifying the size of the file. Note
741 * that the file sizes are interpolated. When expanding, we are simply
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500742 * writing strings of 0's out. When truncating, we truncate the upper
743 * inode and update the lower_ia according to the page index
744 * interpolations. If ATTR_SIZE is set in lower_ia->ia_valid upon return,
745 * the caller must use lower_ia in a call to notify_change() to perform
746 * the truncation of the lower inode.
Michael Halcrow237fead2006-10-04 02:16:22 -0700747 *
748 * Returns zero on success; non-zero otherwise
749 */
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500750static int truncate_upper(struct dentry *dentry, struct iattr *ia,
751 struct iattr *lower_ia)
Michael Halcrow237fead2006-10-04 02:16:22 -0700752{
753 int rc = 0;
754 struct inode *inode = dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700755 struct ecryptfs_crypt_stat *crypt_stat;
756 loff_t i_size = i_size_read(inode);
757 loff_t lower_size_before_truncate;
758 loff_t lower_size_after_truncate;
759
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500760 if (unlikely((ia->ia_size == i_size))) {
761 lower_ia->ia_valid &= ~ATTR_SIZE;
Tyler Hicks332ab162011-04-14 15:35:11 -0500762 return 0;
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500763 }
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500764 rc = ecryptfs_get_lower_file(dentry, inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500765 if (rc)
766 return rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700767 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700768 /* Switch on growing or shrinking file */
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500769 if (ia->ia_size > i_size) {
Michael Halcrow2ed92552007-10-16 01:28:10 -0700770 char zero[] = { 0x00 };
Michael Halcrow240e2df2007-06-27 14:09:44 -0700771
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500772 lower_ia->ia_valid &= ~ATTR_SIZE;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700773 /* Write a single 0 at the last position of the file;
774 * this triggers code that will fill in 0's throughout
775 * the intermediate portion of the previous end of the
776 * file and the new and of the file */
Al Viro48c1e442010-05-21 11:09:58 -0400777 rc = ecryptfs_write(inode, zero,
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500778 (ia->ia_size - 1), 1);
779 } else { /* ia->ia_size < i_size_read(inode) */
780 /* We're chopping off all the pages down to the page
781 * in which ia->ia_size is located. Fill in the end of
782 * that page from (ia->ia_size & ~PAGE_CACHE_MASK) to
Michael Halcrow2ed92552007-10-16 01:28:10 -0700783 * PAGE_CACHE_SIZE with zeros. */
784 size_t num_zeros = (PAGE_CACHE_SIZE
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500785 - (ia->ia_size & ~PAGE_CACHE_MASK));
Michael Halcrow2ed92552007-10-16 01:28:10 -0700786
Tyler Hicks13a791b2009-04-13 15:29:27 -0500787 if (!(crypt_stat->flags & ECRYPTFS_ENCRYPTED)) {
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200788 truncate_setsize(inode, ia->ia_size);
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500789 lower_ia->ia_size = ia->ia_size;
790 lower_ia->ia_valid |= ATTR_SIZE;
Al Viro48c1e442010-05-21 11:09:58 -0400791 goto out;
Tyler Hicks13a791b2009-04-13 15:29:27 -0500792 }
Michael Halcrow2ed92552007-10-16 01:28:10 -0700793 if (num_zeros) {
794 char *zeros_virt;
795
796 zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
797 if (!zeros_virt) {
798 rc = -ENOMEM;
Al Viro48c1e442010-05-21 11:09:58 -0400799 goto out;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700800 }
Al Viro48c1e442010-05-21 11:09:58 -0400801 rc = ecryptfs_write(inode, zeros_virt,
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500802 ia->ia_size, num_zeros);
Michael Halcrow2ed92552007-10-16 01:28:10 -0700803 kfree(zeros_virt);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700804 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700805 printk(KERN_ERR "Error attempting to zero out "
806 "the remainder of the end page on "
807 "reducing truncate; rc = [%d]\n", rc);
Al Viro48c1e442010-05-21 11:09:58 -0400808 goto out;
Michael Halcrow240e2df2007-06-27 14:09:44 -0700809 }
810 }
Christoph Hellwig2c27c652010-06-04 11:30:04 +0200811 truncate_setsize(inode, ia->ia_size);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700812 rc = ecryptfs_write_inode_size_to_metadata(inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800813 if (rc) {
814 printk(KERN_ERR "Problem with "
815 "ecryptfs_write_inode_size_to_metadata; "
816 "rc = [%d]\n", rc);
Al Viro48c1e442010-05-21 11:09:58 -0400817 goto out;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800818 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700819 /* We are reducing the size of the ecryptfs file, and need to
820 * know if we need to reduce the size of the lower file. */
821 lower_size_before_truncate =
822 upper_size_to_lower_size(crypt_stat, i_size);
823 lower_size_after_truncate =
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500824 upper_size_to_lower_size(crypt_stat, ia->ia_size);
825 if (lower_size_after_truncate < lower_size_before_truncate) {
826 lower_ia->ia_size = lower_size_after_truncate;
827 lower_ia->ia_valid |= ATTR_SIZE;
828 } else
829 lower_ia->ia_valid &= ~ATTR_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700830 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700831out:
Tyler Hicks332ab162011-04-14 15:35:11 -0500832 ecryptfs_put_lower_file(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700833 return rc;
834}
835
Tyler Hicksa261a032012-01-19 20:33:44 -0600836static int ecryptfs_inode_newsize_ok(struct inode *inode, loff_t offset)
837{
838 struct ecryptfs_crypt_stat *crypt_stat;
839 loff_t lower_oldsize, lower_newsize;
840
841 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
842 lower_oldsize = upper_size_to_lower_size(crypt_stat,
843 i_size_read(inode));
844 lower_newsize = upper_size_to_lower_size(crypt_stat, offset);
845 if (lower_newsize > lower_oldsize) {
846 /*
847 * The eCryptfs inode and the new *lower* size are mixed here
848 * because we may not have the lower i_mutex held and/or it may
849 * not be appropriate to call inode_newsize_ok() with inodes
850 * from other filesystems.
851 */
852 return inode_newsize_ok(inode, lower_newsize);
853 }
854
855 return 0;
856}
857
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500858/**
859 * ecryptfs_truncate
860 * @dentry: The ecryptfs layer dentry
861 * @new_length: The length to expand the file to
862 *
863 * Simple function that handles the truncation of an eCryptfs inode and
864 * its corresponding lower inode.
865 *
866 * Returns zero on success; non-zero otherwise
867 */
868int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
869{
870 struct iattr ia = { .ia_valid = ATTR_SIZE, .ia_size = new_length };
871 struct iattr lower_ia = { .ia_valid = 0 };
872 int rc;
873
Tyler Hicksa261a032012-01-19 20:33:44 -0600874 rc = ecryptfs_inode_newsize_ok(dentry->d_inode, new_length);
875 if (rc)
876 return rc;
877
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500878 rc = truncate_upper(dentry, &ia, &lower_ia);
879 if (!rc && lower_ia.ia_valid & ATTR_SIZE) {
880 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
881
882 mutex_lock(&lower_dentry->d_inode->i_mutex);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400883 rc = notify_change(lower_dentry, &lower_ia, NULL);
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500884 mutex_unlock(&lower_dentry->d_inode->i_mutex);
885 }
886 return rc;
887}
888
Michael Halcrow237fead2006-10-04 02:16:22 -0700889static int
Al Viro10556cb2011-06-20 19:28:19 -0400890ecryptfs_permission(struct inode *inode, int mask)
Michael Halcrow237fead2006-10-04 02:16:22 -0700891{
Al Virof419a2e2008-07-22 00:07:17 -0400892 return inode_permission(ecryptfs_inode_to_lower(inode), mask);
Michael Halcrow237fead2006-10-04 02:16:22 -0700893}
894
895/**
896 * ecryptfs_setattr
897 * @dentry: dentry handle to the inode to modify
898 * @ia: Structure with flags of what to change and values
899 *
900 * Updates the metadata of an inode. If the update is to the size
901 * i.e. truncation, then ecryptfs_truncate will handle the size modification
902 * of both the ecryptfs inode and the lower inode.
903 *
904 * All other metadata changes will be passed right to the lower filesystem,
905 * and we will just update our inode to look like the lower.
906 */
907static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
908{
909 int rc = 0;
910 struct dentry *lower_dentry;
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500911 struct iattr lower_ia;
Michael Halcrow237fead2006-10-04 02:16:22 -0700912 struct inode *inode;
913 struct inode *lower_inode;
914 struct ecryptfs_crypt_stat *crypt_stat;
915
916 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
Michael Halcrowe10f2812007-06-27 14:09:44 -0700917 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
918 ecryptfs_init_crypt_stat(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700919 inode = dentry->d_inode;
920 lower_inode = ecryptfs_inode_to_lower(inode);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700921 lower_dentry = ecryptfs_dentry_to_lower(dentry);
922 mutex_lock(&crypt_stat->cs_mutex);
923 if (S_ISDIR(dentry->d_inode->i_mode))
924 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrow64ee4802007-07-19 01:47:54 -0700925 else if (S_ISREG(dentry->d_inode->i_mode)
926 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
927 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700928 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrowe10f2812007-06-27 14:09:44 -0700929
Michael Halcrowe10f2812007-06-27 14:09:44 -0700930 mount_crypt_stat = &ecryptfs_superblock_to_private(
931 dentry->d_sb)->mount_crypt_stat;
Tyler Hicks3b06b3e2011-05-24 03:49:02 -0500932 rc = ecryptfs_get_lower_file(dentry, inode);
Tyler Hicks332ab162011-04-14 15:35:11 -0500933 if (rc) {
934 mutex_unlock(&crypt_stat->cs_mutex);
935 goto out;
936 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700937 rc = ecryptfs_read_metadata(dentry);
Tyler Hicks332ab162011-04-14 15:35:11 -0500938 ecryptfs_put_lower_file(inode);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700939 if (rc) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700940 if (!(mount_crypt_stat->flags
941 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
942 rc = -EIO;
Michael Halcrow25bd8172008-02-06 01:38:35 -0800943 printk(KERN_WARNING "Either the lower file "
Michael Halcrowe10f2812007-06-27 14:09:44 -0700944 "is not in a valid eCryptfs format, "
Michael Halcrow25bd8172008-02-06 01:38:35 -0800945 "or the key could not be retrieved. "
946 "Plaintext passthrough mode is not "
Michael Halcrowe10f2812007-06-27 14:09:44 -0700947 "enabled; returning -EIO\n");
Michael Halcrowe10f2812007-06-27 14:09:44 -0700948 mutex_unlock(&crypt_stat->cs_mutex);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700949 goto out;
950 }
951 rc = 0;
Tyler Hicks3aeb86e2011-03-15 14:54:00 -0500952 crypt_stat->flags &= ~(ECRYPTFS_I_SIZE_INITIALIZED
953 | ECRYPTFS_ENCRYPTED);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700954 }
Michael Halcrowe10f2812007-06-27 14:09:44 -0700955 }
956 mutex_unlock(&crypt_stat->cs_mutex);
Tyler Hicksa261a032012-01-19 20:33:44 -0600957
958 rc = inode_change_ok(inode, ia);
959 if (rc)
960 goto out;
961 if (ia->ia_valid & ATTR_SIZE) {
962 rc = ecryptfs_inode_newsize_ok(inode, ia->ia_size);
963 if (rc)
964 goto out;
965 }
966
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500967 memcpy(&lower_ia, ia, sizeof(lower_ia));
968 if (ia->ia_valid & ATTR_FILE)
969 lower_ia.ia_file = ecryptfs_file_to_lower(ia->ia_file);
Michael Halcrow237fead2006-10-04 02:16:22 -0700970 if (ia->ia_valid & ATTR_SIZE) {
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500971 rc = truncate_upper(dentry, ia, &lower_ia);
Michael Halcrow237fead2006-10-04 02:16:22 -0700972 if (rc < 0)
973 goto out;
974 }
Jeff Layton1ac564e2007-10-18 03:05:17 -0700975
976 /*
977 * mode change is for clearing setuid/setgid bits. Allow lower fs
978 * to interpret this in its own way.
979 */
Tyler Hicks5f3ef64f2009-10-14 16:18:27 -0500980 if (lower_ia.ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
981 lower_ia.ia_valid &= ~ATTR_MODE;
Jeff Layton1ac564e2007-10-18 03:05:17 -0700982
Miklos Szeredi9c3580a2008-04-29 00:59:48 -0700983 mutex_lock(&lower_dentry->d_inode->i_mutex);
J. Bruce Fields27ac0ff2011-09-20 17:19:26 -0400984 rc = notify_change(lower_dentry, &lower_ia, NULL);
Miklos Szeredi9c3580a2008-04-29 00:59:48 -0700985 mutex_unlock(&lower_dentry->d_inode->i_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700986out:
Erez Zadok9afa2fb2009-12-02 19:51:54 -0500987 fsstack_copy_attr_all(inode, lower_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700988 return rc;
989}
990
Tyler Hicks111d61a2013-01-17 12:19:35 -0800991static int ecryptfs_getattr_link(struct vfsmount *mnt, struct dentry *dentry,
992 struct kstat *stat)
Tyler Hicks3a60a162010-03-22 00:41:35 -0500993{
994 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
995 int rc = 0;
996
997 mount_crypt_stat = &ecryptfs_superblock_to_private(
998 dentry->d_sb)->mount_crypt_stat;
999 generic_fillattr(dentry->d_inode, stat);
1000 if (mount_crypt_stat->flags & ECRYPTFS_GLOBAL_ENCRYPT_FILENAMES) {
1001 char *target;
1002 size_t targetsiz;
1003
1004 rc = ecryptfs_readlink_lower(dentry, &target, &targetsiz);
1005 if (!rc) {
1006 kfree(target);
1007 stat->size = targetsiz;
1008 }
1009 }
1010 return rc;
1011}
1012
Tyler Hicks111d61a2013-01-17 12:19:35 -08001013static int ecryptfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
1014 struct kstat *stat)
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001015{
1016 struct kstat lower_stat;
1017 int rc;
1018
Al Viro3dadecc2013-01-24 02:18:08 -05001019 rc = vfs_getattr(ecryptfs_dentry_to_lower_path(dentry), &lower_stat);
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001020 if (!rc) {
Tyler Hicks55f9cf62011-01-11 12:43:42 -06001021 fsstack_copy_attr_all(dentry->d_inode,
1022 ecryptfs_inode_to_lower(dentry->d_inode));
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001023 generic_fillattr(dentry->d_inode, stat);
1024 stat->blocks = lower_stat.blocks;
1025 }
1026 return rc;
1027}
1028
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001029int
Michael Halcrow237fead2006-10-04 02:16:22 -07001030ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
1031 size_t size, int flags)
1032{
1033 int rc = 0;
1034 struct dentry *lower_dentry;
1035
1036 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1037 if (!lower_dentry->d_inode->i_op->setxattr) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001038 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001039 goto out;
1040 }
Roberto Sassu48b512e2010-10-05 18:53:45 +02001041
1042 rc = vfs_setxattr(lower_dentry, name, value, size, flags);
Tyler Hicks545d6802012-02-07 17:55:40 -06001043 if (!rc)
1044 fsstack_copy_attr_all(dentry->d_inode, lower_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -07001045out:
1046 return rc;
1047}
1048
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001049ssize_t
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001050ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
1051 void *value, size_t size)
1052{
1053 int rc = 0;
1054
1055 if (!lower_dentry->d_inode->i_op->getxattr) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001056 rc = -EOPNOTSUPP;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -07001057 goto out;
1058 }
1059 mutex_lock(&lower_dentry->d_inode->i_mutex);
1060 rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
1061 size);
1062 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1063out:
1064 return rc;
1065}
1066
Adrian Bunk7896b632008-02-06 01:38:32 -08001067static ssize_t
Michael Halcrow237fead2006-10-04 02:16:22 -07001068ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
1069 size_t size)
1070{
Michael Halcrow2ed92552007-10-16 01:28:10 -07001071 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
1072 value, size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001073}
1074
1075static ssize_t
1076ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1077{
1078 int rc = 0;
1079 struct dentry *lower_dentry;
1080
1081 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1082 if (!lower_dentry->d_inode->i_op->listxattr) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001083 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001084 goto out;
1085 }
1086 mutex_lock(&lower_dentry->d_inode->i_mutex);
1087 rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
1088 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1089out:
1090 return rc;
1091}
1092
1093static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
1094{
1095 int rc = 0;
1096 struct dentry *lower_dentry;
1097
1098 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1099 if (!lower_dentry->d_inode->i_op->removexattr) {
Christian Pulvermachercfce08c2010-03-23 11:51:38 -05001100 rc = -EOPNOTSUPP;
Michael Halcrow237fead2006-10-04 02:16:22 -07001101 goto out;
1102 }
1103 mutex_lock(&lower_dentry->d_inode->i_mutex);
1104 rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
1105 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1106out:
1107 return rc;
1108}
1109
Arjan van de Ven754661f2007-02-12 00:55:38 -08001110const struct inode_operations ecryptfs_symlink_iops = {
Al Viro408bd622012-05-03 09:34:20 -04001111 .readlink = generic_readlink,
Michael Halcrow237fead2006-10-04 02:16:22 -07001112 .follow_link = ecryptfs_follow_link,
Al Viro87dc8002013-09-16 10:30:04 -04001113 .put_link = kfree_put_link,
Michael Halcrow237fead2006-10-04 02:16:22 -07001114 .permission = ecryptfs_permission,
1115 .setattr = ecryptfs_setattr,
Tyler Hicks3a60a162010-03-22 00:41:35 -05001116 .getattr = ecryptfs_getattr_link,
Michael Halcrow237fead2006-10-04 02:16:22 -07001117 .setxattr = ecryptfs_setxattr,
1118 .getxattr = ecryptfs_getxattr,
1119 .listxattr = ecryptfs_listxattr,
1120 .removexattr = ecryptfs_removexattr
1121};
1122
Arjan van de Ven754661f2007-02-12 00:55:38 -08001123const struct inode_operations ecryptfs_dir_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001124 .create = ecryptfs_create,
1125 .lookup = ecryptfs_lookup,
1126 .link = ecryptfs_link,
1127 .unlink = ecryptfs_unlink,
1128 .symlink = ecryptfs_symlink,
1129 .mkdir = ecryptfs_mkdir,
1130 .rmdir = ecryptfs_rmdir,
1131 .mknod = ecryptfs_mknod,
1132 .rename = ecryptfs_rename,
1133 .permission = ecryptfs_permission,
1134 .setattr = ecryptfs_setattr,
1135 .setxattr = ecryptfs_setxattr,
1136 .getxattr = ecryptfs_getxattr,
1137 .listxattr = ecryptfs_listxattr,
1138 .removexattr = ecryptfs_removexattr
1139};
1140
Arjan van de Ven754661f2007-02-12 00:55:38 -08001141const struct inode_operations ecryptfs_main_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001142 .permission = ecryptfs_permission,
1143 .setattr = ecryptfs_setattr,
Tyler Hicksf8f484d2009-11-04 02:48:01 -06001144 .getattr = ecryptfs_getattr,
Michael Halcrow237fead2006-10-04 02:16:22 -07001145 .setxattr = ecryptfs_setxattr,
1146 .getxattr = ecryptfs_getxattr,
1147 .listxattr = ecryptfs_listxattr,
1148 .removexattr = ecryptfs_removexattr
1149};