blob: 26090878c930ba0a900a08ee54dee58eea7555fd [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>
Harvey Harrison0a688ad2008-07-23 21:30:07 -070034#include <asm/unaligned.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070035#include "ecryptfs_kernel.h"
36
37static struct dentry *lock_parent(struct dentry *dentry)
38{
39 struct dentry *dir;
40
Miklos Szeredi8dc4e372008-05-12 14:02:04 -070041 dir = dget_parent(dentry);
Peter Zijlstra908e0a82007-03-07 20:41:30 -080042 mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
Michael Halcrow237fead2006-10-04 02:16:22 -070043 return dir;
44}
45
Michael Halcrow237fead2006-10-04 02:16:22 -070046static void unlock_dir(struct dentry *dir)
47{
48 mutex_unlock(&dir->d_inode->i_mutex);
49 dput(dir);
50}
51
Michael Halcrow237fead2006-10-04 02:16:22 -070052/**
53 * ecryptfs_create_underlying_file
54 * @lower_dir_inode: inode of the parent in the lower fs of the new file
55 * @lower_dentry: New file's dentry in the lower fs
56 * @ecryptfs_dentry: New file's dentry in ecryptfs
57 * @mode: The mode of the new file
58 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
59 *
60 * Creates the file in the lower file system.
61 *
62 * Returns zero on success; non-zero on error condition
63 */
64static int
65ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
66 struct dentry *dentry, int mode,
67 struct nameidata *nd)
68{
69 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
70 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
71 struct dentry *dentry_save;
72 struct vfsmount *vfsmount_save;
73 int rc;
74
Jan Blunck4ac91372008-02-14 19:34:32 -080075 dentry_save = nd->path.dentry;
76 vfsmount_save = nd->path.mnt;
77 nd->path.dentry = lower_dentry;
78 nd->path.mnt = lower_mnt;
Michael Halcrow237fead2006-10-04 02:16:22 -070079 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -080080 nd->path.dentry = dentry_save;
81 nd->path.mnt = vfsmount_save;
Michael Halcrow237fead2006-10-04 02:16:22 -070082 return rc;
83}
84
85/**
86 * ecryptfs_do_create
87 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
88 * @ecryptfs_dentry: New file's dentry in ecryptfs
89 * @mode: The mode of the new file
90 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
91 *
92 * Creates the underlying file and the eCryptfs inode which will link to
93 * it. It will also update the eCryptfs directory inode to mimic the
94 * stat of the lower directory inode.
95 *
96 * Returns zero on success; non-zero on error condition
97 */
98static int
99ecryptfs_do_create(struct inode *directory_inode,
100 struct dentry *ecryptfs_dentry, int mode,
101 struct nameidata *nd)
102{
103 int rc;
104 struct dentry *lower_dentry;
105 struct dentry *lower_dir_dentry;
106
107 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
108 lower_dir_dentry = lock_parent(lower_dentry);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700109 if (IS_ERR(lower_dir_dentry)) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700110 ecryptfs_printk(KERN_ERR, "Error locking directory of "
111 "dentry\n");
112 rc = PTR_ERR(lower_dir_dentry);
113 goto out;
114 }
115 rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
116 ecryptfs_dentry, mode, nd);
Michael Halcrow4981e082007-10-16 01:28:09 -0700117 if (rc) {
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800118 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
Harvey Harrison18d1dbf2008-04-29 00:59:48 -0700119 "rc = [%d]\n", __func__, rc);
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800120 goto out_lock;
Michael Halcrow237fead2006-10-04 02:16:22 -0700121 }
122 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
123 directory_inode->i_sb, 0);
124 if (rc) {
125 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
126 goto out_lock;
127 }
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800128 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
129 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700130out_lock:
131 unlock_dir(lower_dir_dentry);
132out:
133 return rc;
134}
135
136/**
137 * grow_file
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700138 * @ecryptfs_dentry: the eCryptfs dentry
Michael Halcrow237fead2006-10-04 02:16:22 -0700139 *
140 * This is the code which will grow the file to its correct size.
141 */
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700142static int grow_file(struct dentry *ecryptfs_dentry)
Michael Halcrow237fead2006-10-04 02:16:22 -0700143{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700144 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700145 struct file fake_file;
146 struct ecryptfs_file_info tmp_file_info;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700147 char zero_virt[] = { 0x00 };
148 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700149
150 memset(&fake_file, 0, sizeof(fake_file));
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800151 fake_file.f_path.dentry = ecryptfs_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700152 memset(&tmp_file_info, 0, sizeof(tmp_file_info));
153 ecryptfs_set_file_private(&fake_file, &tmp_file_info);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700154 ecryptfs_set_file_lower(
155 &fake_file,
156 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file);
157 rc = ecryptfs_write(&fake_file, zero_virt, 0, 1);
158 i_size_write(ecryptfs_inode, 0);
159 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
160 ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
161 ECRYPTFS_NEW_FILE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700162 return rc;
163}
164
165/**
166 * ecryptfs_initialize_file
167 *
168 * Cause the file to be changed from a basic empty file to an ecryptfs
169 * file with a header and first data page.
170 *
171 * Returns zero on success
172 */
173static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
174{
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700175 struct ecryptfs_crypt_stat *crypt_stat =
176 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700177 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700178
Michael Halcrow237fead2006-10-04 02:16:22 -0700179 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
180 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800181 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700182 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700183 }
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800184 crypt_stat->flags |= ECRYPTFS_NEW_FILE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700185 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
186 rc = ecryptfs_new_file_context(ecryptfs_dentry);
187 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700188 ecryptfs_printk(KERN_ERR, "Error creating new file "
189 "context; rc = [%d]\n", rc);
190 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700191 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700192 rc = ecryptfs_write_metadata(ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700193 if (rc) {
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700194 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
195 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700196 }
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700197 rc = grow_file(ecryptfs_dentry);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700198 if (rc)
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700199 printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700200out:
201 return rc;
202}
203
204/**
205 * ecryptfs_create
206 * @dir: The inode of the directory in which to create the file.
207 * @dentry: The eCryptfs dentry
208 * @mode: The mode of the new file.
209 * @nd: nameidata
210 *
211 * Creates a new file.
212 *
213 * Returns zero on success; non-zero on error condition
214 */
215static int
216ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
217 int mode, struct nameidata *nd)
218{
219 int rc;
220
Michael Halcrow4981e082007-10-16 01:28:09 -0700221 /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
222 * the crypt_stat->lower_file (persistent file) */
Michael Halcrow237fead2006-10-04 02:16:22 -0700223 rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
224 if (unlikely(rc)) {
225 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
226 "lower filesystem\n");
227 goto out;
228 }
229 /* At this point, a file exists on "disk"; we need to make sure
230 * that this on disk file is prepared to be an ecryptfs file */
231 rc = ecryptfs_initialize_file(ecryptfs_dentry);
232out:
233 return rc;
234}
235
236/**
237 * ecryptfs_lookup
238 * @dir: inode
239 * @dentry: The dentry
240 * @nd: nameidata, may be NULL
241 *
242 * Find a file on disk. If the file does not exist, then we'll add it to the
243 * dentry cache and continue on to read it from the disk.
244 */
245static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
246 struct nameidata *nd)
247{
248 int rc = 0;
249 struct dentry *lower_dir_dentry;
250 struct dentry *lower_dentry;
251 struct vfsmount *lower_mnt;
Michael Halcrow237fead2006-10-04 02:16:22 -0700252 char *encoded_name;
Mika Kukkonenc381bfc2007-07-17 04:04:53 -0700253 int encoded_namelen;
Michael Halcrow237fead2006-10-04 02:16:22 -0700254 struct ecryptfs_crypt_stat *crypt_stat = NULL;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800255 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700256 char *page_virt = NULL;
257 struct inode *lower_inode;
258 u64 file_size;
259
260 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
261 dentry->d_op = &ecryptfs_dops;
262 if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800263 || (dentry->d_name.len == 2
264 && !strcmp(dentry->d_name.name, ".."))) {
265 d_drop(dentry);
266 goto out;
267 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700268 encoded_namelen = ecryptfs_encode_filename(crypt_stat,
269 dentry->d_name.name,
270 dentry->d_name.len,
271 &encoded_name);
272 if (encoded_namelen < 0) {
273 rc = encoded_namelen;
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800274 d_drop(dentry);
275 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700276 }
277 ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
278 "= [%d]\n", encoded_name, encoded_namelen);
279 lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
280 encoded_namelen - 1);
281 kfree(encoded_name);
Michael Halcrow237fead2006-10-04 02:16:22 -0700282 if (IS_ERR(lower_dentry)) {
283 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
284 rc = PTR_ERR(lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800285 d_drop(dentry);
286 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700287 }
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800288 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
Michael Halcrow237fead2006-10-04 02:16:22 -0700289 ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
290 "d_name.name = [%s]\n", lower_dentry,
291 lower_dentry->d_name.name);
292 lower_inode = lower_dentry->d_inode;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800293 fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700294 BUG_ON(!atomic_read(&lower_dentry->d_count));
295 ecryptfs_set_dentry_private(dentry,
296 kmem_cache_alloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800297 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700298 if (!ecryptfs_dentry_to_private(dentry)) {
299 rc = -ENOMEM;
300 ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
301 "to allocate ecryptfs_dentry_info struct\n");
302 goto out_dput;
303 }
304 ecryptfs_set_dentry_lower(dentry, lower_dentry);
305 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
306 if (!lower_dentry->d_inode) {
307 /* We want to add because we couldn't find in lower */
308 d_add(dentry, NULL);
309 goto out;
310 }
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700311 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb,
312 ECRYPTFS_INTERPOSE_FLAG_D_ADD);
Michael Halcrow237fead2006-10-04 02:16:22 -0700313 if (rc) {
314 ecryptfs_printk(KERN_ERR, "Error interposing\n");
315 goto out_dput;
316 }
317 if (S_ISDIR(lower_inode->i_mode)) {
318 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
319 goto out;
320 }
321 if (S_ISLNK(lower_inode->i_mode)) {
322 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
323 goto out;
324 }
Ryusuke Konishi202a21d2007-08-10 13:00:51 -0700325 if (special_file(lower_inode->i_mode)) {
326 ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n");
327 goto out;
328 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700329 if (!nd) {
330 ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
331 "as we *think* we are about to unlink\n");
332 goto out;
333 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700334 /* Released in this function */
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800335 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800336 GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -0700337 if (!page_virt) {
338 rc = -ENOMEM;
339 ecryptfs_printk(KERN_ERR,
340 "Cannot ecryptfs_kmalloc a page\n");
341 goto out_dput;
342 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700343 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800344 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
Michael Halcrow237fead2006-10-04 02:16:22 -0700345 ecryptfs_set_default_sizes(crypt_stat);
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700346 rc = ecryptfs_read_and_validate_header_region(page_virt,
347 dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700348 if (rc) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800349 rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
350 if (rc) {
351 printk(KERN_DEBUG "Valid metadata not found in header "
352 "region or xattr region; treating file as "
353 "unencrypted\n");
354 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700355 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
356 goto out;
357 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800358 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
Michael Halcrow237fead2006-10-04 02:16:22 -0700359 }
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800360 mount_crypt_stat = &ecryptfs_superblock_to_private(
361 dentry->d_sb)->mount_crypt_stat;
362 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
363 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800364 file_size = (crypt_stat->num_header_bytes_at_front
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800365 + i_size_read(lower_dentry->d_inode));
366 else
367 file_size = i_size_read(lower_dentry->d_inode);
368 } else {
Harvey Harrison0a688ad2008-07-23 21:30:07 -0700369 file_size = get_unaligned_be64(page_virt);
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800370 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800371 i_size_write(dentry->d_inode, (loff_t)file_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700372 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
373 goto out;
374
375out_dput:
376 dput(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700377 d_drop(dentry);
378out:
379 return ERR_PTR(rc);
380}
381
382static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
383 struct dentry *new_dentry)
384{
385 struct dentry *lower_old_dentry;
386 struct dentry *lower_new_dentry;
387 struct dentry *lower_dir_dentry;
388 u64 file_size_save;
389 int rc;
390
391 file_size_save = i_size_read(old_dentry->d_inode);
392 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
393 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
394 dget(lower_old_dentry);
395 dget(lower_new_dentry);
396 lower_dir_dentry = lock_parent(lower_new_dentry);
397 rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
398 lower_new_dentry);
399 if (rc || !lower_new_dentry->d_inode)
400 goto out_lock;
401 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
402 if (rc)
403 goto out_lock;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800404 fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
405 fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700406 old_dentry->d_inode->i_nlink =
407 ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
408 i_size_write(new_dentry->d_inode, file_size_save);
409out_lock:
410 unlock_dir(lower_dir_dentry);
411 dput(lower_new_dentry);
412 dput(lower_old_dentry);
Michael Halcrowae56fb12006-11-16 01:19:30 -0800413 d_drop(lower_old_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800414 d_drop(new_dentry);
415 d_drop(old_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700416 return rc;
417}
418
419static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
420{
421 int rc = 0;
422 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
423 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
Miklos Szeredi8dc4e372008-05-12 14:02:04 -0700424 struct dentry *lower_dir_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700425
Miklos Szeredi8dc4e372008-05-12 14:02:04 -0700426 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700427 rc = vfs_unlink(lower_dir_inode, lower_dentry);
428 if (rc) {
Michael Halcrowae56fb12006-11-16 01:19:30 -0800429 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700430 goto out_unlock;
431 }
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800432 fsstack_copy_attr_times(dir, lower_dir_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700433 dentry->d_inode->i_nlink =
434 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
435 dentry->d_inode->i_ctime = dir->i_ctime;
Michael Halcrowcaeeeec2008-01-08 15:33:02 -0800436 d_drop(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700437out_unlock:
Miklos Szeredi8dc4e372008-05-12 14:02:04 -0700438 unlock_dir(lower_dir_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700439 return rc;
440}
441
442static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
443 const char *symname)
444{
445 int rc;
446 struct dentry *lower_dentry;
447 struct dentry *lower_dir_dentry;
448 umode_t mode;
449 char *encoded_symname;
Mika Kukkonenc381bfc2007-07-17 04:04:53 -0700450 int encoded_symlen;
Michael Halcrow237fead2006-10-04 02:16:22 -0700451 struct ecryptfs_crypt_stat *crypt_stat = NULL;
452
453 lower_dentry = ecryptfs_dentry_to_lower(dentry);
454 dget(lower_dentry);
455 lower_dir_dentry = lock_parent(lower_dentry);
456 mode = S_IALLUGO;
457 encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
458 strlen(symname),
459 &encoded_symname);
460 if (encoded_symlen < 0) {
461 rc = encoded_symlen;
462 goto out_lock;
463 }
464 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
465 encoded_symname, mode);
466 kfree(encoded_symname);
467 if (rc || !lower_dentry->d_inode)
468 goto out_lock;
469 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
470 if (rc)
471 goto out_lock;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800472 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
473 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700474out_lock:
475 unlock_dir(lower_dir_dentry);
476 dput(lower_dentry);
477 if (!dentry->d_inode)
478 d_drop(dentry);
479 return rc;
480}
481
482static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
483{
484 int rc;
485 struct dentry *lower_dentry;
486 struct dentry *lower_dir_dentry;
487
488 lower_dentry = ecryptfs_dentry_to_lower(dentry);
489 lower_dir_dentry = lock_parent(lower_dentry);
490 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
491 if (rc || !lower_dentry->d_inode)
492 goto out;
493 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
494 if (rc)
495 goto out;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800496 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
497 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700498 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
499out:
500 unlock_dir(lower_dir_dentry);
501 if (!dentry->d_inode)
502 d_drop(dentry);
503 return rc;
504}
505
506static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
507{
Michael Halcrow237fead2006-10-04 02:16:22 -0700508 struct dentry *lower_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700509 struct dentry *lower_dir_dentry;
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800510 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700511
512 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800513 dget(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700514 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800515 dget(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700516 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800517 dput(lower_dentry);
518 if (!rc)
519 d_delete(lower_dentry);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800520 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700521 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
522 unlock_dir(lower_dir_dentry);
523 if (!rc)
524 d_drop(dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800525 dput(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700526 return rc;
527}
528
529static int
530ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
531{
532 int rc;
533 struct dentry *lower_dentry;
534 struct dentry *lower_dir_dentry;
535
536 lower_dentry = ecryptfs_dentry_to_lower(dentry);
537 lower_dir_dentry = lock_parent(lower_dentry);
538 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
539 if (rc || !lower_dentry->d_inode)
540 goto out;
Michael Halcrow72b55ff2008-07-23 21:30:07 -0700541 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb,
542 ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700543 if (rc)
544 goto out;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800545 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
546 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700547out:
548 unlock_dir(lower_dir_dentry);
549 if (!dentry->d_inode)
550 d_drop(dentry);
551 return rc;
552}
553
554static int
555ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
556 struct inode *new_dir, struct dentry *new_dentry)
557{
558 int rc;
559 struct dentry *lower_old_dentry;
560 struct dentry *lower_new_dentry;
561 struct dentry *lower_old_dir_dentry;
562 struct dentry *lower_new_dir_dentry;
563
564 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
565 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
566 dget(lower_old_dentry);
567 dget(lower_new_dentry);
568 lower_old_dir_dentry = dget_parent(lower_old_dentry);
569 lower_new_dir_dentry = dget_parent(lower_new_dentry);
570 lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
571 rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
572 lower_new_dir_dentry->d_inode, lower_new_dentry);
573 if (rc)
574 goto out_lock;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800575 fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700576 if (new_dir != old_dir)
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800577 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700578out_lock:
579 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
Michael Halcrowa9083082006-11-16 01:19:16 -0800580 dput(lower_new_dentry->d_parent);
581 dput(lower_old_dentry->d_parent);
Michael Halcrow237fead2006-10-04 02:16:22 -0700582 dput(lower_new_dentry);
583 dput(lower_old_dentry);
584 return rc;
585}
586
587static int
588ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
589{
590 int rc;
591 struct dentry *lower_dentry;
592 char *decoded_name;
593 char *lower_buf;
594 mm_segment_t old_fs;
595 struct ecryptfs_crypt_stat *crypt_stat;
596
597 lower_dentry = ecryptfs_dentry_to_lower(dentry);
598 if (!lower_dentry->d_inode->i_op ||
599 !lower_dentry->d_inode->i_op->readlink) {
600 rc = -EINVAL;
601 goto out;
602 }
603 /* Released in this function */
604 lower_buf = kmalloc(bufsiz, GFP_KERNEL);
605 if (lower_buf == NULL) {
606 ecryptfs_printk(KERN_ERR, "Out of memory\n");
607 rc = -ENOMEM;
608 goto out;
609 }
610 old_fs = get_fs();
611 set_fs(get_ds());
612 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
613 "lower_dentry->d_name.name = [%s]\n",
614 lower_dentry->d_name.name);
615 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
616 (char __user *)lower_buf,
617 bufsiz);
618 set_fs(old_fs);
619 if (rc >= 0) {
620 crypt_stat = NULL;
621 rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
622 &decoded_name);
623 if (rc == -ENOMEM)
624 goto out_free_lower_buf;
625 if (rc > 0) {
626 ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
627 "to userspace: [%*s]\n", rc,
628 decoded_name);
629 if (copy_to_user(buf, decoded_name, rc))
630 rc = -EFAULT;
631 }
632 kfree(decoded_name);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800633 fsstack_copy_attr_atime(dentry->d_inode,
634 lower_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700635 }
636out_free_lower_buf:
637 kfree(lower_buf);
638out:
639 return rc;
640}
641
642static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
643{
644 char *buf;
645 int len = PAGE_SIZE, rc;
646 mm_segment_t old_fs;
647
648 /* Released in ecryptfs_put_link(); only release here on error */
649 buf = kmalloc(len, GFP_KERNEL);
650 if (!buf) {
651 rc = -ENOMEM;
652 goto out;
653 }
654 old_fs = get_fs();
655 set_fs(get_ds());
656 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
657 "dentry->d_name.name = [%s]\n", dentry->d_name.name);
658 rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
659 buf[rc] = '\0';
660 set_fs(old_fs);
661 if (rc < 0)
662 goto out_free;
663 rc = 0;
664 nd_set_link(nd, buf);
665 goto out;
666out_free:
667 kfree(buf);
668out:
669 return ERR_PTR(rc);
670}
671
672static void
673ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
674{
675 /* Free the char* */
676 kfree(nd_get_link(nd));
677}
678
679/**
680 * upper_size_to_lower_size
681 * @crypt_stat: Crypt_stat associated with file
682 * @upper_size: Size of the upper file
683 *
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800684 * Calculate the required size of the lower file based on the
Michael Halcrow237fead2006-10-04 02:16:22 -0700685 * specified size of the upper file. This calculation is based on the
686 * number of headers in the underlying file and the extent size.
687 *
688 * Returns Calculated size of the lower file.
689 */
690static loff_t
691upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
692 loff_t upper_size)
693{
694 loff_t lower_size;
695
Michael Halcrowcc11bef2008-02-06 01:38:32 -0800696 lower_size = crypt_stat->num_header_bytes_at_front;
Michael Halcrow237fead2006-10-04 02:16:22 -0700697 if (upper_size != 0) {
698 loff_t num_extents;
699
700 num_extents = upper_size >> crypt_stat->extent_shift;
701 if (upper_size & ~crypt_stat->extent_mask)
702 num_extents++;
703 lower_size += (num_extents * crypt_stat->extent_size);
704 }
705 return lower_size;
706}
707
708/**
709 * ecryptfs_truncate
710 * @dentry: The ecryptfs layer dentry
711 * @new_length: The length to expand the file to
712 *
713 * Function to handle truncations modifying the size of the file. Note
714 * that the file sizes are interpolated. When expanding, we are simply
715 * writing strings of 0's out. When truncating, we need to modify the
716 * underlying file size according to the page index interpolations.
717 *
718 * Returns zero on success; non-zero otherwise
719 */
720int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
721{
722 int rc = 0;
723 struct inode *inode = dentry->d_inode;
724 struct dentry *lower_dentry;
Michael Halcrow2ed92552007-10-16 01:28:10 -0700725 struct file fake_ecryptfs_file;
Michael Halcrow237fead2006-10-04 02:16:22 -0700726 struct ecryptfs_crypt_stat *crypt_stat;
727 loff_t i_size = i_size_read(inode);
728 loff_t lower_size_before_truncate;
729 loff_t lower_size_after_truncate;
730
731 if (unlikely((new_length == i_size)))
732 goto out;
733 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
734 /* Set up a fake ecryptfs file, this is used to interface with
735 * the file in the underlying filesystem so that the
736 * truncation has an effect there as well. */
737 memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800738 fake_ecryptfs_file.f_path.dentry = dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700739 /* Released at out_free: label */
740 ecryptfs_set_file_private(&fake_ecryptfs_file,
741 kmem_cache_alloc(ecryptfs_file_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800742 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700743 if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
744 rc = -ENOMEM;
745 goto out;
746 }
747 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Michael Halcrow2ed92552007-10-16 01:28:10 -0700748 ecryptfs_set_file_lower(
749 &fake_ecryptfs_file,
750 ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
Michael Halcrow237fead2006-10-04 02:16:22 -0700751 /* Switch on growing or shrinking file */
752 if (new_length > i_size) {
Michael Halcrow2ed92552007-10-16 01:28:10 -0700753 char zero[] = { 0x00 };
Michael Halcrow240e2df2007-06-27 14:09:44 -0700754
Michael Halcrow2ed92552007-10-16 01:28:10 -0700755 /* Write a single 0 at the last position of the file;
756 * this triggers code that will fill in 0's throughout
757 * the intermediate portion of the previous end of the
758 * file and the new and of the file */
759 rc = ecryptfs_write(&fake_ecryptfs_file, zero,
760 (new_length - 1), 1);
761 } else { /* new_length < i_size_read(inode) */
762 /* We're chopping off all the pages down do the page
763 * in which new_length is located. Fill in the end of
764 * that page from (new_length & ~PAGE_CACHE_MASK) to
765 * PAGE_CACHE_SIZE with zeros. */
766 size_t num_zeros = (PAGE_CACHE_SIZE
767 - (new_length & ~PAGE_CACHE_MASK));
768
769 if (num_zeros) {
770 char *zeros_virt;
771
772 zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
773 if (!zeros_virt) {
774 rc = -ENOMEM;
775 goto out_free;
776 }
777 rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
778 new_length, num_zeros);
779 kfree(zeros_virt);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700780 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700781 printk(KERN_ERR "Error attempting to zero out "
782 "the remainder of the end page on "
783 "reducing truncate; rc = [%d]\n", rc);
Michael Halcrow2ed92552007-10-16 01:28:10 -0700784 goto out_free;
Michael Halcrow240e2df2007-06-27 14:09:44 -0700785 }
786 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700787 vmtruncate(inode, new_length);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700788 rc = ecryptfs_write_inode_size_to_metadata(inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800789 if (rc) {
790 printk(KERN_ERR "Problem with "
791 "ecryptfs_write_inode_size_to_metadata; "
792 "rc = [%d]\n", rc);
Michael Halcrow2ed92552007-10-16 01:28:10 -0700793 goto out_free;
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800794 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700795 /* We are reducing the size of the ecryptfs file, and need to
796 * know if we need to reduce the size of the lower file. */
797 lower_size_before_truncate =
798 upper_size_to_lower_size(crypt_stat, i_size);
799 lower_size_after_truncate =
800 upper_size_to_lower_size(crypt_stat, new_length);
801 if (lower_size_after_truncate < lower_size_before_truncate)
802 vmtruncate(lower_dentry->d_inode,
803 lower_size_after_truncate);
804 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700805out_free:
806 if (ecryptfs_file_to_private(&fake_ecryptfs_file))
807 kmem_cache_free(ecryptfs_file_info_cache,
808 ecryptfs_file_to_private(&fake_ecryptfs_file));
809out:
810 return rc;
811}
812
813static int
814ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
815{
816 int rc;
817
818 if (nd) {
Jan Blunck4ac91372008-02-14 19:34:32 -0800819 struct vfsmount *vfsmnt_save = nd->path.mnt;
820 struct dentry *dentry_save = nd->path.dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700821
Jan Blunck4ac91372008-02-14 19:34:32 -0800822 nd->path.mnt = ecryptfs_dentry_to_lower_mnt(nd->path.dentry);
823 nd->path.dentry = ecryptfs_dentry_to_lower(nd->path.dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700824 rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -0800825 nd->path.mnt = vfsmnt_save;
826 nd->path.dentry = dentry_save;
Michael Halcrow237fead2006-10-04 02:16:22 -0700827 } else
828 rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
829 return rc;
830}
831
832/**
833 * ecryptfs_setattr
834 * @dentry: dentry handle to the inode to modify
835 * @ia: Structure with flags of what to change and values
836 *
837 * Updates the metadata of an inode. If the update is to the size
838 * i.e. truncation, then ecryptfs_truncate will handle the size modification
839 * of both the ecryptfs inode and the lower inode.
840 *
841 * All other metadata changes will be passed right to the lower filesystem,
842 * and we will just update our inode to look like the lower.
843 */
844static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
845{
846 int rc = 0;
847 struct dentry *lower_dentry;
848 struct inode *inode;
849 struct inode *lower_inode;
850 struct ecryptfs_crypt_stat *crypt_stat;
851
852 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
Michael Halcrowe10f2812007-06-27 14:09:44 -0700853 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
854 ecryptfs_init_crypt_stat(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700855 inode = dentry->d_inode;
856 lower_inode = ecryptfs_inode_to_lower(inode);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700857 lower_dentry = ecryptfs_dentry_to_lower(dentry);
858 mutex_lock(&crypt_stat->cs_mutex);
859 if (S_ISDIR(dentry->d_inode->i_mode))
860 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrow64ee4802007-07-19 01:47:54 -0700861 else if (S_ISREG(dentry->d_inode->i_mode)
862 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
863 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700864 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrowe10f2812007-06-27 14:09:44 -0700865
Michael Halcrowe10f2812007-06-27 14:09:44 -0700866 mount_crypt_stat = &ecryptfs_superblock_to_private(
867 dentry->d_sb)->mount_crypt_stat;
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700868 rc = ecryptfs_read_metadata(dentry);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700869 if (rc) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700870 if (!(mount_crypt_stat->flags
871 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
872 rc = -EIO;
Michael Halcrow25bd8172008-02-06 01:38:35 -0800873 printk(KERN_WARNING "Either the lower file "
Michael Halcrowe10f2812007-06-27 14:09:44 -0700874 "is not in a valid eCryptfs format, "
Michael Halcrow25bd8172008-02-06 01:38:35 -0800875 "or the key could not be retrieved. "
876 "Plaintext passthrough mode is not "
Michael Halcrowe10f2812007-06-27 14:09:44 -0700877 "enabled; returning -EIO\n");
Michael Halcrowe10f2812007-06-27 14:09:44 -0700878 mutex_unlock(&crypt_stat->cs_mutex);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700879 goto out;
880 }
881 rc = 0;
882 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
883 mutex_unlock(&crypt_stat->cs_mutex);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700884 goto out;
885 }
Michael Halcrowe10f2812007-06-27 14:09:44 -0700886 }
887 mutex_unlock(&crypt_stat->cs_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700888 if (ia->ia_valid & ATTR_SIZE) {
889 ecryptfs_printk(KERN_DEBUG,
890 "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
891 ia->ia_valid, ATTR_SIZE);
892 rc = ecryptfs_truncate(dentry, ia->ia_size);
893 /* ecryptfs_truncate handles resizing of the lower file */
894 ia->ia_valid &= ~ATTR_SIZE;
895 ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
896 ia->ia_valid);
897 if (rc < 0)
898 goto out;
899 }
Jeff Layton1ac564e2007-10-18 03:05:17 -0700900
901 /*
902 * mode change is for clearing setuid/setgid bits. Allow lower fs
903 * to interpret this in its own way.
904 */
905 if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
906 ia->ia_valid &= ~ATTR_MODE;
907
Miklos Szeredi9c3580a2008-04-29 00:59:48 -0700908 mutex_lock(&lower_dentry->d_inode->i_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700909 rc = notify_change(lower_dentry, ia);
Miklos Szeredi9c3580a2008-04-29 00:59:48 -0700910 mutex_unlock(&lower_dentry->d_inode->i_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700911out:
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800912 fsstack_copy_attr_all(inode, lower_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700913 return rc;
914}
915
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800916int
Michael Halcrow237fead2006-10-04 02:16:22 -0700917ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
918 size_t size, int flags)
919{
920 int rc = 0;
921 struct dentry *lower_dentry;
922
923 lower_dentry = ecryptfs_dentry_to_lower(dentry);
924 if (!lower_dentry->d_inode->i_op->setxattr) {
925 rc = -ENOSYS;
926 goto out;
927 }
928 mutex_lock(&lower_dentry->d_inode->i_mutex);
929 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
930 size, flags);
931 mutex_unlock(&lower_dentry->d_inode->i_mutex);
932out:
933 return rc;
934}
935
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800936ssize_t
Michael Halcrowd7cdc5f2007-10-16 01:28:10 -0700937ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
938 void *value, size_t size)
939{
940 int rc = 0;
941
942 if (!lower_dentry->d_inode->i_op->getxattr) {
943 rc = -ENOSYS;
944 goto out;
945 }
946 mutex_lock(&lower_dentry->d_inode->i_mutex);
947 rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
948 size);
949 mutex_unlock(&lower_dentry->d_inode->i_mutex);
950out:
951 return rc;
952}
953
Adrian Bunk7896b632008-02-06 01:38:32 -0800954static ssize_t
Michael Halcrow237fead2006-10-04 02:16:22 -0700955ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
956 size_t size)
957{
Michael Halcrow2ed92552007-10-16 01:28:10 -0700958 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
959 value, size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700960}
961
962static ssize_t
963ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
964{
965 int rc = 0;
966 struct dentry *lower_dentry;
967
968 lower_dentry = ecryptfs_dentry_to_lower(dentry);
969 if (!lower_dentry->d_inode->i_op->listxattr) {
970 rc = -ENOSYS;
971 goto out;
972 }
973 mutex_lock(&lower_dentry->d_inode->i_mutex);
974 rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
975 mutex_unlock(&lower_dentry->d_inode->i_mutex);
976out:
977 return rc;
978}
979
980static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
981{
982 int rc = 0;
983 struct dentry *lower_dentry;
984
985 lower_dentry = ecryptfs_dentry_to_lower(dentry);
986 if (!lower_dentry->d_inode->i_op->removexattr) {
987 rc = -ENOSYS;
988 goto out;
989 }
990 mutex_lock(&lower_dentry->d_inode->i_mutex);
991 rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
992 mutex_unlock(&lower_dentry->d_inode->i_mutex);
993out:
994 return rc;
995}
996
997int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
998{
999 if ((ecryptfs_inode_to_lower(inode)
1000 == (struct inode *)candidate_lower_inode))
1001 return 1;
1002 else
1003 return 0;
1004}
1005
1006int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
1007{
1008 ecryptfs_init_inode(inode, (struct inode *)lower_inode);
1009 return 0;
1010}
1011
Arjan van de Ven754661f2007-02-12 00:55:38 -08001012const struct inode_operations ecryptfs_symlink_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001013 .readlink = ecryptfs_readlink,
1014 .follow_link = ecryptfs_follow_link,
1015 .put_link = ecryptfs_put_link,
1016 .permission = ecryptfs_permission,
1017 .setattr = ecryptfs_setattr,
1018 .setxattr = ecryptfs_setxattr,
1019 .getxattr = ecryptfs_getxattr,
1020 .listxattr = ecryptfs_listxattr,
1021 .removexattr = ecryptfs_removexattr
1022};
1023
Arjan van de Ven754661f2007-02-12 00:55:38 -08001024const struct inode_operations ecryptfs_dir_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001025 .create = ecryptfs_create,
1026 .lookup = ecryptfs_lookup,
1027 .link = ecryptfs_link,
1028 .unlink = ecryptfs_unlink,
1029 .symlink = ecryptfs_symlink,
1030 .mkdir = ecryptfs_mkdir,
1031 .rmdir = ecryptfs_rmdir,
1032 .mknod = ecryptfs_mknod,
1033 .rename = ecryptfs_rename,
1034 .permission = ecryptfs_permission,
1035 .setattr = ecryptfs_setattr,
1036 .setxattr = ecryptfs_setxattr,
1037 .getxattr = ecryptfs_getxattr,
1038 .listxattr = ecryptfs_listxattr,
1039 .removexattr = ecryptfs_removexattr
1040};
1041
Arjan van de Ven754661f2007-02-12 00:55:38 -08001042const struct inode_operations ecryptfs_main_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001043 .permission = ecryptfs_permission,
1044 .setattr = ecryptfs_setattr,
1045 .setxattr = ecryptfs_setxattr,
1046 .getxattr = ecryptfs_getxattr,
1047 .listxattr = ecryptfs_listxattr,
1048 .removexattr = ecryptfs_removexattr
1049};