blob: c746b5d8a3361c86fd498ced6439b49e2cea702c [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>
Michael Halcrow237fead2006-10-04 02:16:22 -070034#include "ecryptfs_kernel.h"
35
36static struct dentry *lock_parent(struct dentry *dentry)
37{
38 struct dentry *dir;
39
40 dir = dget(dentry->d_parent);
Peter Zijlstra908e0a82007-03-07 20:41:30 -080041 mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
Michael Halcrow237fead2006-10-04 02:16:22 -070042 return dir;
43}
44
45static void unlock_parent(struct dentry *dentry)
46{
47 mutex_unlock(&(dentry->d_parent->d_inode->i_mutex));
48 dput(dentry->d_parent);
49}
50
51static void unlock_dir(struct dentry *dir)
52{
53 mutex_unlock(&dir->d_inode->i_mutex);
54 dput(dir);
55}
56
Michael Halcrow237fead2006-10-04 02:16:22 -070057/**
58 * ecryptfs_create_underlying_file
59 * @lower_dir_inode: inode of the parent in the lower fs of the new file
60 * @lower_dentry: New file's dentry in the lower fs
61 * @ecryptfs_dentry: New file's dentry in ecryptfs
62 * @mode: The mode of the new file
63 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
64 *
65 * Creates the file in the lower file system.
66 *
67 * Returns zero on success; non-zero on error condition
68 */
69static int
70ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
71 struct dentry *dentry, int mode,
72 struct nameidata *nd)
73{
74 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
75 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
76 struct dentry *dentry_save;
77 struct vfsmount *vfsmount_save;
78 int rc;
79
80 dentry_save = nd->dentry;
81 vfsmount_save = nd->mnt;
82 nd->dentry = lower_dentry;
83 nd->mnt = lower_mnt;
84 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
85 nd->dentry = dentry_save;
86 nd->mnt = vfsmount_save;
87 return rc;
88}
89
90/**
91 * ecryptfs_do_create
92 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
93 * @ecryptfs_dentry: New file's dentry in ecryptfs
94 * @mode: The mode of the new file
95 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
96 *
97 * Creates the underlying file and the eCryptfs inode which will link to
98 * it. It will also update the eCryptfs directory inode to mimic the
99 * stat of the lower directory inode.
100 *
101 * Returns zero on success; non-zero on error condition
102 */
103static int
104ecryptfs_do_create(struct inode *directory_inode,
105 struct dentry *ecryptfs_dentry, int mode,
106 struct nameidata *nd)
107{
108 int rc;
109 struct dentry *lower_dentry;
110 struct dentry *lower_dir_dentry;
111
112 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
113 lower_dir_dentry = lock_parent(lower_dentry);
114 if (unlikely(IS_ERR(lower_dir_dentry))) {
115 ecryptfs_printk(KERN_ERR, "Error locking directory of "
116 "dentry\n");
117 rc = PTR_ERR(lower_dir_dentry);
118 goto out;
119 }
120 rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
121 ecryptfs_dentry, mode, nd);
Michael Halcrow4981e082007-10-16 01:28:09 -0700122 if (rc) {
123 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
124 struct ecryptfs_inode_info *inode_info =
125 ecryptfs_inode_to_private(ecryptfs_inode);
126
127 printk(KERN_WARNING "%s: Error creating underlying file; "
128 "rc = [%d]; checking for existing\n", __FUNCTION__, rc);
129 if (inode_info) {
130 mutex_lock(&inode_info->lower_file_mutex);
131 if (!inode_info->lower_file) {
132 mutex_unlock(&inode_info->lower_file_mutex);
133 printk(KERN_ERR "%s: Failure to set underlying "
134 "file; rc = [%d]\n", __FUNCTION__, rc);
135 goto out_lock;
136 }
137 mutex_unlock(&inode_info->lower_file_mutex);
138 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700139 }
140 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
141 directory_inode->i_sb, 0);
142 if (rc) {
143 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
144 goto out_lock;
145 }
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800146 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
147 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700148out_lock:
149 unlock_dir(lower_dir_dentry);
150out:
151 return rc;
152}
153
154/**
155 * grow_file
156 * @ecryptfs_dentry: the ecryptfs dentry
157 * @lower_file: The lower file
158 * @inode: The ecryptfs inode
159 * @lower_inode: The lower inode
160 *
161 * This is the code which will grow the file to its correct size.
162 */
163static int grow_file(struct dentry *ecryptfs_dentry, struct file *lower_file,
164 struct inode *inode, struct inode *lower_inode)
165{
166 int rc = 0;
167 struct file fake_file;
168 struct ecryptfs_file_info tmp_file_info;
169
170 memset(&fake_file, 0, sizeof(fake_file));
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800171 fake_file.f_path.dentry = ecryptfs_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700172 memset(&tmp_file_info, 0, sizeof(tmp_file_info));
173 ecryptfs_set_file_private(&fake_file, &tmp_file_info);
174 ecryptfs_set_file_lower(&fake_file, lower_file);
175 rc = ecryptfs_fill_zeros(&fake_file, 1);
176 if (rc) {
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800177 ecryptfs_inode_to_private(inode)->crypt_stat.flags |=
178 ECRYPTFS_SECURITY_WARNING;
Michael Halcrow237fead2006-10-04 02:16:22 -0700179 ecryptfs_printk(KERN_WARNING, "Error attempting to fill zeros "
180 "in file; rc = [%d]\n", rc);
181 goto out;
182 }
183 i_size_write(inode, 0);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700184 rc = ecryptfs_write_inode_size_to_metadata(inode);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800185 ecryptfs_inode_to_private(inode)->crypt_stat.flags |= ECRYPTFS_NEW_FILE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700186out:
187 return rc;
188}
189
190/**
191 * ecryptfs_initialize_file
192 *
193 * Cause the file to be changed from a basic empty file to an ecryptfs
194 * file with a header and first data page.
195 *
196 * Returns zero on success
197 */
198static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
199{
200 int rc = 0;
201 int lower_flags;
202 struct ecryptfs_crypt_stat *crypt_stat;
203 struct dentry *lower_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700204 struct file *lower_file;
205 struct inode *inode, *lower_inode;
206 struct vfsmount *lower_mnt;
207
208 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
209 ecryptfs_printk(KERN_DEBUG, "lower_dentry->d_name.name = [%s]\n",
210 lower_dentry->d_name.name);
211 inode = ecryptfs_dentry->d_inode;
212 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
Michael Halcrow70456602007-02-12 00:53:48 -0800213 lower_flags = ((O_CREAT | O_TRUNC) & O_ACCMODE) | O_RDWR;
Michael Halcrow237fead2006-10-04 02:16:22 -0700214 lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700215 /* Corresponding fput() at end of this function */
Michael Halcrow5dda6992007-10-16 01:28:06 -0700216 rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
217 lower_flags);
218 if (rc) {
Michael Halcrow237fead2006-10-04 02:16:22 -0700219 ecryptfs_printk(KERN_ERR,
220 "Error opening dentry; rc = [%i]\n", rc);
221 goto out;
222 }
Michael Halcrow7ff1d742006-10-30 22:07:19 -0800223 lower_inode = lower_dentry->d_inode;
Michael Halcrow237fead2006-10-04 02:16:22 -0700224 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
225 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800226 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrow237fead2006-10-04 02:16:22 -0700227 goto out_fput;
228 }
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800229 crypt_stat->flags |= ECRYPTFS_NEW_FILE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700230 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
231 rc = ecryptfs_new_file_context(ecryptfs_dentry);
232 if (rc) {
233 ecryptfs_printk(KERN_DEBUG, "Error creating new file "
234 "context\n");
235 goto out_fput;
236 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800237 rc = ecryptfs_write_metadata(ecryptfs_dentry, lower_file);
Michael Halcrow237fead2006-10-04 02:16:22 -0700238 if (rc) {
239 ecryptfs_printk(KERN_DEBUG, "Error writing headers\n");
240 goto out_fput;
241 }
242 rc = grow_file(ecryptfs_dentry, lower_file, inode, lower_inode);
243out_fput:
Michael Halcrow5dda6992007-10-16 01:28:06 -0700244 rc = ecryptfs_close_lower_file(lower_file);
245 if (rc)
Michael Halcrow7ff1d742006-10-30 22:07:19 -0800246 printk(KERN_ERR "Error closing lower_file\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700247out:
248 return rc;
249}
250
251/**
252 * ecryptfs_create
253 * @dir: The inode of the directory in which to create the file.
254 * @dentry: The eCryptfs dentry
255 * @mode: The mode of the new file.
256 * @nd: nameidata
257 *
258 * Creates a new file.
259 *
260 * Returns zero on success; non-zero on error condition
261 */
262static int
263ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
264 int mode, struct nameidata *nd)
265{
266 int rc;
267
Michael Halcrow4981e082007-10-16 01:28:09 -0700268 /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
269 * the crypt_stat->lower_file (persistent file) */
Michael Halcrow237fead2006-10-04 02:16:22 -0700270 rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
271 if (unlikely(rc)) {
272 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
273 "lower filesystem\n");
274 goto out;
275 }
276 /* At this point, a file exists on "disk"; we need to make sure
277 * that this on disk file is prepared to be an ecryptfs file */
278 rc = ecryptfs_initialize_file(ecryptfs_dentry);
279out:
280 return rc;
281}
282
283/**
284 * ecryptfs_lookup
285 * @dir: inode
286 * @dentry: The dentry
287 * @nd: nameidata, may be NULL
288 *
289 * Find a file on disk. If the file does not exist, then we'll add it to the
290 * dentry cache and continue on to read it from the disk.
291 */
292static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
293 struct nameidata *nd)
294{
295 int rc = 0;
296 struct dentry *lower_dir_dentry;
297 struct dentry *lower_dentry;
298 struct vfsmount *lower_mnt;
Michael Halcrow237fead2006-10-04 02:16:22 -0700299 char *encoded_name;
Mika Kukkonenc381bfc2007-07-17 04:04:53 -0700300 int encoded_namelen;
Michael Halcrow237fead2006-10-04 02:16:22 -0700301 struct ecryptfs_crypt_stat *crypt_stat = NULL;
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800302 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
Michael Halcrow237fead2006-10-04 02:16:22 -0700303 char *page_virt = NULL;
304 struct inode *lower_inode;
305 u64 file_size;
306
307 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
308 dentry->d_op = &ecryptfs_dops;
309 if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800310 || (dentry->d_name.len == 2
311 && !strcmp(dentry->d_name.name, ".."))) {
312 d_drop(dentry);
313 goto out;
314 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700315 encoded_namelen = ecryptfs_encode_filename(crypt_stat,
316 dentry->d_name.name,
317 dentry->d_name.len,
318 &encoded_name);
319 if (encoded_namelen < 0) {
320 rc = encoded_namelen;
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800321 d_drop(dentry);
322 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700323 }
324 ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
325 "= [%d]\n", encoded_name, encoded_namelen);
326 lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
327 encoded_namelen - 1);
328 kfree(encoded_name);
Michael Halcrow237fead2006-10-04 02:16:22 -0700329 if (IS_ERR(lower_dentry)) {
330 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
331 rc = PTR_ERR(lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800332 d_drop(dentry);
333 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -0700334 }
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800335 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
Michael Halcrow237fead2006-10-04 02:16:22 -0700336 ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
337 "d_name.name = [%s]\n", lower_dentry,
338 lower_dentry->d_name.name);
339 lower_inode = lower_dentry->d_inode;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800340 fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700341 BUG_ON(!atomic_read(&lower_dentry->d_count));
342 ecryptfs_set_dentry_private(dentry,
343 kmem_cache_alloc(ecryptfs_dentry_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800344 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700345 if (!ecryptfs_dentry_to_private(dentry)) {
346 rc = -ENOMEM;
347 ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
348 "to allocate ecryptfs_dentry_info struct\n");
349 goto out_dput;
350 }
351 ecryptfs_set_dentry_lower(dentry, lower_dentry);
352 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
353 if (!lower_dentry->d_inode) {
354 /* We want to add because we couldn't find in lower */
355 d_add(dentry, NULL);
356 goto out;
357 }
358 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
359 if (rc) {
360 ecryptfs_printk(KERN_ERR, "Error interposing\n");
361 goto out_dput;
362 }
363 if (S_ISDIR(lower_inode->i_mode)) {
364 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
365 goto out;
366 }
367 if (S_ISLNK(lower_inode->i_mode)) {
368 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
369 goto out;
370 }
Ryusuke Konishi202a21d2007-08-10 13:00:51 -0700371 if (special_file(lower_inode->i_mode)) {
372 ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n");
373 goto out;
374 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700375 if (!nd) {
376 ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
377 "as we *think* we are about to unlink\n");
378 goto out;
379 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700380 /* Released in this function */
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800381 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800382 GFP_USER);
Michael Halcrow237fead2006-10-04 02:16:22 -0700383 if (!page_virt) {
384 rc = -ENOMEM;
385 ecryptfs_printk(KERN_ERR,
386 "Cannot ecryptfs_kmalloc a page\n");
387 goto out_dput;
388 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700389 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800390 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
Michael Halcrow237fead2006-10-04 02:16:22 -0700391 ecryptfs_set_default_sizes(crypt_stat);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800392 rc = ecryptfs_read_and_validate_header_region(page_virt, lower_dentry,
393 nd->mnt);
Michael Halcrow237fead2006-10-04 02:16:22 -0700394 if (rc) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800395 rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
396 if (rc) {
397 printk(KERN_DEBUG "Valid metadata not found in header "
398 "region or xattr region; treating file as "
399 "unencrypted\n");
400 rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700401 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
402 goto out;
403 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800404 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
Michael Halcrow237fead2006-10-04 02:16:22 -0700405 }
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800406 mount_crypt_stat = &ecryptfs_superblock_to_private(
407 dentry->d_sb)->mount_crypt_stat;
408 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
409 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
Michael Halcrow45eaab72007-10-16 01:28:05 -0700410 file_size = ((crypt_stat->extent_size
411 * crypt_stat->num_header_extents_at_front)
Michael Halcrowe77a56d2007-02-12 00:53:47 -0800412 + i_size_read(lower_dentry->d_inode));
413 else
414 file_size = i_size_read(lower_dentry->d_inode);
415 } else {
416 memcpy(&file_size, page_virt, sizeof(file_size));
417 file_size = be64_to_cpu(file_size);
418 }
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800419 i_size_write(dentry->d_inode, (loff_t)file_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700420 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
421 goto out;
422
423out_dput:
424 dput(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700425 d_drop(dentry);
426out:
427 return ERR_PTR(rc);
428}
429
430static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
431 struct dentry *new_dentry)
432{
433 struct dentry *lower_old_dentry;
434 struct dentry *lower_new_dentry;
435 struct dentry *lower_dir_dentry;
436 u64 file_size_save;
437 int rc;
438
439 file_size_save = i_size_read(old_dentry->d_inode);
440 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
441 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
442 dget(lower_old_dentry);
443 dget(lower_new_dentry);
444 lower_dir_dentry = lock_parent(lower_new_dentry);
445 rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
446 lower_new_dentry);
447 if (rc || !lower_new_dentry->d_inode)
448 goto out_lock;
449 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
450 if (rc)
451 goto out_lock;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800452 fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
453 fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700454 old_dentry->d_inode->i_nlink =
455 ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
456 i_size_write(new_dentry->d_inode, file_size_save);
457out_lock:
458 unlock_dir(lower_dir_dentry);
459 dput(lower_new_dentry);
460 dput(lower_old_dentry);
Michael Halcrowae56fb12006-11-16 01:19:30 -0800461 d_drop(lower_old_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800462 d_drop(new_dentry);
463 d_drop(old_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700464 return rc;
465}
466
467static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
468{
469 int rc = 0;
470 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
471 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
472
473 lock_parent(lower_dentry);
474 rc = vfs_unlink(lower_dir_inode, lower_dentry);
475 if (rc) {
Michael Halcrowae56fb12006-11-16 01:19:30 -0800476 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700477 goto out_unlock;
478 }
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800479 fsstack_copy_attr_times(dir, lower_dir_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700480 dentry->d_inode->i_nlink =
481 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
482 dentry->d_inode->i_ctime = dir->i_ctime;
483out_unlock:
484 unlock_parent(lower_dentry);
485 return rc;
486}
487
488static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
489 const char *symname)
490{
491 int rc;
492 struct dentry *lower_dentry;
493 struct dentry *lower_dir_dentry;
494 umode_t mode;
495 char *encoded_symname;
Mika Kukkonenc381bfc2007-07-17 04:04:53 -0700496 int encoded_symlen;
Michael Halcrow237fead2006-10-04 02:16:22 -0700497 struct ecryptfs_crypt_stat *crypt_stat = NULL;
498
499 lower_dentry = ecryptfs_dentry_to_lower(dentry);
500 dget(lower_dentry);
501 lower_dir_dentry = lock_parent(lower_dentry);
502 mode = S_IALLUGO;
503 encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
504 strlen(symname),
505 &encoded_symname);
506 if (encoded_symlen < 0) {
507 rc = encoded_symlen;
508 goto out_lock;
509 }
510 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
511 encoded_symname, mode);
512 kfree(encoded_symname);
513 if (rc || !lower_dentry->d_inode)
514 goto out_lock;
515 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
516 if (rc)
517 goto out_lock;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800518 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
519 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700520out_lock:
521 unlock_dir(lower_dir_dentry);
522 dput(lower_dentry);
523 if (!dentry->d_inode)
524 d_drop(dentry);
525 return rc;
526}
527
528static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
529{
530 int rc;
531 struct dentry *lower_dentry;
532 struct dentry *lower_dir_dentry;
533
534 lower_dentry = ecryptfs_dentry_to_lower(dentry);
535 lower_dir_dentry = lock_parent(lower_dentry);
536 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
537 if (rc || !lower_dentry->d_inode)
538 goto out;
539 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
540 if (rc)
541 goto out;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800542 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
543 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700544 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
545out:
546 unlock_dir(lower_dir_dentry);
547 if (!dentry->d_inode)
548 d_drop(dentry);
549 return rc;
550}
551
552static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
553{
Michael Halcrow237fead2006-10-04 02:16:22 -0700554 struct dentry *lower_dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700555 struct dentry *lower_dir_dentry;
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800556 int rc;
Michael Halcrow237fead2006-10-04 02:16:22 -0700557
558 lower_dentry = ecryptfs_dentry_to_lower(dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800559 dget(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700560 lower_dir_dentry = lock_parent(lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800561 dget(lower_dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700562 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800563 dput(lower_dentry);
564 if (!rc)
565 d_delete(lower_dentry);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800566 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700567 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
568 unlock_dir(lower_dir_dentry);
569 if (!rc)
570 d_drop(dentry);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -0800571 dput(dentry);
Michael Halcrow237fead2006-10-04 02:16:22 -0700572 return rc;
573}
574
575static int
576ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
577{
578 int rc;
579 struct dentry *lower_dentry;
580 struct dentry *lower_dir_dentry;
581
582 lower_dentry = ecryptfs_dentry_to_lower(dentry);
583 lower_dir_dentry = lock_parent(lower_dentry);
584 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
585 if (rc || !lower_dentry->d_inode)
586 goto out;
587 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
588 if (rc)
589 goto out;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800590 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
591 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700592out:
593 unlock_dir(lower_dir_dentry);
594 if (!dentry->d_inode)
595 d_drop(dentry);
596 return rc;
597}
598
599static int
600ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
601 struct inode *new_dir, struct dentry *new_dentry)
602{
603 int rc;
604 struct dentry *lower_old_dentry;
605 struct dentry *lower_new_dentry;
606 struct dentry *lower_old_dir_dentry;
607 struct dentry *lower_new_dir_dentry;
608
609 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
610 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
611 dget(lower_old_dentry);
612 dget(lower_new_dentry);
613 lower_old_dir_dentry = dget_parent(lower_old_dentry);
614 lower_new_dir_dentry = dget_parent(lower_new_dentry);
615 lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
616 rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
617 lower_new_dir_dentry->d_inode, lower_new_dentry);
618 if (rc)
619 goto out_lock;
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800620 fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700621 if (new_dir != old_dir)
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800622 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700623out_lock:
624 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
Michael Halcrowa9083082006-11-16 01:19:16 -0800625 dput(lower_new_dentry->d_parent);
626 dput(lower_old_dentry->d_parent);
Michael Halcrow237fead2006-10-04 02:16:22 -0700627 dput(lower_new_dentry);
628 dput(lower_old_dentry);
629 return rc;
630}
631
632static int
633ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
634{
635 int rc;
636 struct dentry *lower_dentry;
637 char *decoded_name;
638 char *lower_buf;
639 mm_segment_t old_fs;
640 struct ecryptfs_crypt_stat *crypt_stat;
641
642 lower_dentry = ecryptfs_dentry_to_lower(dentry);
643 if (!lower_dentry->d_inode->i_op ||
644 !lower_dentry->d_inode->i_op->readlink) {
645 rc = -EINVAL;
646 goto out;
647 }
648 /* Released in this function */
649 lower_buf = kmalloc(bufsiz, GFP_KERNEL);
650 if (lower_buf == NULL) {
651 ecryptfs_printk(KERN_ERR, "Out of memory\n");
652 rc = -ENOMEM;
653 goto out;
654 }
655 old_fs = get_fs();
656 set_fs(get_ds());
657 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
658 "lower_dentry->d_name.name = [%s]\n",
659 lower_dentry->d_name.name);
660 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
661 (char __user *)lower_buf,
662 bufsiz);
663 set_fs(old_fs);
664 if (rc >= 0) {
665 crypt_stat = NULL;
666 rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
667 &decoded_name);
668 if (rc == -ENOMEM)
669 goto out_free_lower_buf;
670 if (rc > 0) {
671 ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
672 "to userspace: [%*s]\n", rc,
673 decoded_name);
674 if (copy_to_user(buf, decoded_name, rc))
675 rc = -EFAULT;
676 }
677 kfree(decoded_name);
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800678 fsstack_copy_attr_atime(dentry->d_inode,
679 lower_dentry->d_inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700680 }
681out_free_lower_buf:
682 kfree(lower_buf);
683out:
684 return rc;
685}
686
687static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
688{
689 char *buf;
690 int len = PAGE_SIZE, rc;
691 mm_segment_t old_fs;
692
693 /* Released in ecryptfs_put_link(); only release here on error */
694 buf = kmalloc(len, GFP_KERNEL);
695 if (!buf) {
696 rc = -ENOMEM;
697 goto out;
698 }
699 old_fs = get_fs();
700 set_fs(get_ds());
701 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
702 "dentry->d_name.name = [%s]\n", dentry->d_name.name);
703 rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
704 buf[rc] = '\0';
705 set_fs(old_fs);
706 if (rc < 0)
707 goto out_free;
708 rc = 0;
709 nd_set_link(nd, buf);
710 goto out;
711out_free:
712 kfree(buf);
713out:
714 return ERR_PTR(rc);
715}
716
717static void
718ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
719{
720 /* Free the char* */
721 kfree(nd_get_link(nd));
722}
723
724/**
725 * upper_size_to_lower_size
726 * @crypt_stat: Crypt_stat associated with file
727 * @upper_size: Size of the upper file
728 *
729 * Calculate the requried size of the lower file based on the
730 * specified size of the upper file. This calculation is based on the
731 * number of headers in the underlying file and the extent size.
732 *
733 * Returns Calculated size of the lower file.
734 */
735static loff_t
736upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
737 loff_t upper_size)
738{
739 loff_t lower_size;
740
Michael Halcrow45eaab72007-10-16 01:28:05 -0700741 lower_size = (crypt_stat->extent_size
742 * crypt_stat->num_header_extents_at_front);
Michael Halcrow237fead2006-10-04 02:16:22 -0700743 if (upper_size != 0) {
744 loff_t num_extents;
745
746 num_extents = upper_size >> crypt_stat->extent_shift;
747 if (upper_size & ~crypt_stat->extent_mask)
748 num_extents++;
749 lower_size += (num_extents * crypt_stat->extent_size);
750 }
751 return lower_size;
752}
753
754/**
755 * ecryptfs_truncate
756 * @dentry: The ecryptfs layer dentry
757 * @new_length: The length to expand the file to
758 *
759 * Function to handle truncations modifying the size of the file. Note
760 * that the file sizes are interpolated. When expanding, we are simply
761 * writing strings of 0's out. When truncating, we need to modify the
762 * underlying file size according to the page index interpolations.
763 *
764 * Returns zero on success; non-zero otherwise
765 */
766int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
767{
768 int rc = 0;
769 struct inode *inode = dentry->d_inode;
770 struct dentry *lower_dentry;
771 struct vfsmount *lower_mnt;
772 struct file fake_ecryptfs_file, *lower_file = NULL;
773 struct ecryptfs_crypt_stat *crypt_stat;
774 loff_t i_size = i_size_read(inode);
775 loff_t lower_size_before_truncate;
776 loff_t lower_size_after_truncate;
777
778 if (unlikely((new_length == i_size)))
779 goto out;
780 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
781 /* Set up a fake ecryptfs file, this is used to interface with
782 * the file in the underlying filesystem so that the
783 * truncation has an effect there as well. */
784 memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
Josef "Jeff" Sipekbd243a42006-12-08 02:36:48 -0800785 fake_ecryptfs_file.f_path.dentry = dentry;
Michael Halcrow237fead2006-10-04 02:16:22 -0700786 /* Released at out_free: label */
787 ecryptfs_set_file_private(&fake_ecryptfs_file,
788 kmem_cache_alloc(ecryptfs_file_info_cache,
Christoph Lametere94b1762006-12-06 20:33:17 -0800789 GFP_KERNEL));
Michael Halcrow237fead2006-10-04 02:16:22 -0700790 if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
791 rc = -ENOMEM;
792 goto out;
793 }
794 lower_dentry = ecryptfs_dentry_to_lower(dentry);
795 /* This dget & mntget is released through fput at out_fput: */
Michael Halcrow237fead2006-10-04 02:16:22 -0700796 lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700797 rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
798 O_RDWR);
799 if (rc) {
Michael Halcrow7ff1d742006-10-30 22:07:19 -0800800 ecryptfs_printk(KERN_ERR,
801 "Error opening dentry; rc = [%i]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700802 goto out_free;
803 }
804 ecryptfs_set_file_lower(&fake_ecryptfs_file, lower_file);
805 /* Switch on growing or shrinking file */
806 if (new_length > i_size) {
807 rc = ecryptfs_fill_zeros(&fake_ecryptfs_file, new_length);
808 if (rc) {
809 ecryptfs_printk(KERN_ERR,
810 "Problem with fill_zeros\n");
811 goto out_fput;
812 }
813 i_size_write(inode, new_length);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700814 rc = ecryptfs_write_inode_size_to_metadata(inode);
Michael Halcrow237fead2006-10-04 02:16:22 -0700815 if (rc) {
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800816 printk(KERN_ERR "Problem with "
817 "ecryptfs_write_inode_size_to_metadata; "
818 "rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700819 goto out_fput;
820 }
821 } else { /* new_length < i_size_read(inode) */
Michael Halcrow240e2df2007-06-27 14:09:44 -0700822 pgoff_t index = 0;
823 int end_pos_in_page = -1;
824
825 if (new_length != 0) {
826 index = ((new_length - 1) >> PAGE_CACHE_SHIFT);
827 end_pos_in_page = ((new_length - 1) & ~PAGE_CACHE_MASK);
828 }
829 if (end_pos_in_page != (PAGE_CACHE_SIZE - 1)) {
Michael Halcrow5dda6992007-10-16 01:28:06 -0700830 rc = ecryptfs_write_zeros(&fake_ecryptfs_file,
831 index,
832 (end_pos_in_page + 1),
833 ((PAGE_CACHE_SIZE - 1)
834 - end_pos_in_page));
835 if (rc) {
Michael Halcrow240e2df2007-06-27 14:09:44 -0700836 printk(KERN_ERR "Error attempting to zero out "
837 "the remainder of the end page on "
838 "reducing truncate; rc = [%d]\n", rc);
839 goto out_fput;
840 }
841 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700842 vmtruncate(inode, new_length);
Michael Halcrow0216f7f2007-10-16 01:28:08 -0700843 rc = ecryptfs_write_inode_size_to_metadata(inode);
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800844 if (rc) {
845 printk(KERN_ERR "Problem with "
846 "ecryptfs_write_inode_size_to_metadata; "
847 "rc = [%d]\n", rc);
848 goto out_fput;
849 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700850 /* We are reducing the size of the ecryptfs file, and need to
851 * know if we need to reduce the size of the lower file. */
852 lower_size_before_truncate =
853 upper_size_to_lower_size(crypt_stat, i_size);
854 lower_size_after_truncate =
855 upper_size_to_lower_size(crypt_stat, new_length);
856 if (lower_size_after_truncate < lower_size_before_truncate)
857 vmtruncate(lower_dentry->d_inode,
858 lower_size_after_truncate);
859 }
860 /* Update the access times */
861 lower_dentry->d_inode->i_mtime = lower_dentry->d_inode->i_ctime
862 = CURRENT_TIME;
863 mark_inode_dirty_sync(inode);
864out_fput:
Michael Halcrow5dda6992007-10-16 01:28:06 -0700865 rc = ecryptfs_close_lower_file(lower_file);
866 if (rc)
Michael Halcrow7ff1d742006-10-30 22:07:19 -0800867 printk(KERN_ERR "Error closing lower_file\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700868out_free:
869 if (ecryptfs_file_to_private(&fake_ecryptfs_file))
870 kmem_cache_free(ecryptfs_file_info_cache,
871 ecryptfs_file_to_private(&fake_ecryptfs_file));
872out:
873 return rc;
874}
875
876static int
877ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
878{
879 int rc;
880
881 if (nd) {
882 struct vfsmount *vfsmnt_save = nd->mnt;
883 struct dentry *dentry_save = nd->dentry;
884
885 nd->mnt = ecryptfs_dentry_to_lower_mnt(nd->dentry);
886 nd->dentry = ecryptfs_dentry_to_lower(nd->dentry);
887 rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
888 nd->mnt = vfsmnt_save;
889 nd->dentry = dentry_save;
890 } else
891 rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
892 return rc;
893}
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;
911 struct inode *inode;
912 struct inode *lower_inode;
913 struct ecryptfs_crypt_stat *crypt_stat;
914
915 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
Michael Halcrowe10f2812007-06-27 14:09:44 -0700916 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
917 ecryptfs_init_crypt_stat(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700918 inode = dentry->d_inode;
919 lower_inode = ecryptfs_inode_to_lower(inode);
Michael Halcrowe10f2812007-06-27 14:09:44 -0700920 lower_dentry = ecryptfs_dentry_to_lower(dentry);
921 mutex_lock(&crypt_stat->cs_mutex);
922 if (S_ISDIR(dentry->d_inode->i_mode))
923 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
Michael Halcrow64ee4802007-07-19 01:47:54 -0700924 else if (S_ISREG(dentry->d_inode->i_mode)
925 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
926 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700927 struct vfsmount *lower_mnt;
928 struct file *lower_file = NULL;
929 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
930 int lower_flags;
931
932 lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
933 lower_flags = O_RDONLY;
Michael Halcrow5dda6992007-10-16 01:28:06 -0700934 rc = ecryptfs_open_lower_file(&lower_file, lower_dentry,
935 lower_mnt, lower_flags);
936 if (rc) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700937 printk(KERN_ERR
938 "Error opening lower file; rc = [%d]\n", rc);
939 mutex_unlock(&crypt_stat->cs_mutex);
940 goto out;
941 }
942 mount_crypt_stat = &ecryptfs_superblock_to_private(
943 dentry->d_sb)->mount_crypt_stat;
Michael Halcrow5dda6992007-10-16 01:28:06 -0700944 rc = ecryptfs_read_metadata(dentry, lower_file);
945 if (rc) {
Michael Halcrowe10f2812007-06-27 14:09:44 -0700946 if (!(mount_crypt_stat->flags
947 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
948 rc = -EIO;
949 printk(KERN_WARNING "Attempt to read file that "
950 "is not in a valid eCryptfs format, "
951 "and plaintext passthrough mode is not "
952 "enabled; returning -EIO\n");
953
954 mutex_unlock(&crypt_stat->cs_mutex);
955 fput(lower_file);
956 goto out;
957 }
958 rc = 0;
959 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
960 mutex_unlock(&crypt_stat->cs_mutex);
961 fput(lower_file);
962 goto out;
963 }
964 fput(lower_file);
965 }
966 mutex_unlock(&crypt_stat->cs_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -0700967 if (ia->ia_valid & ATTR_SIZE) {
968 ecryptfs_printk(KERN_DEBUG,
969 "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
970 ia->ia_valid, ATTR_SIZE);
971 rc = ecryptfs_truncate(dentry, ia->ia_size);
972 /* ecryptfs_truncate handles resizing of the lower file */
973 ia->ia_valid &= ~ATTR_SIZE;
974 ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
975 ia->ia_valid);
976 if (rc < 0)
977 goto out;
978 }
979 rc = notify_change(lower_dentry, ia);
980out:
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -0800981 fsstack_copy_attr_all(inode, lower_inode, NULL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700982 return rc;
983}
984
Michael Halcrowdd2a3b72007-02-12 00:53:46 -0800985int
Michael Halcrow237fead2006-10-04 02:16:22 -0700986ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
987 size_t size, int flags)
988{
989 int rc = 0;
990 struct dentry *lower_dentry;
991
992 lower_dentry = ecryptfs_dentry_to_lower(dentry);
993 if (!lower_dentry->d_inode->i_op->setxattr) {
994 rc = -ENOSYS;
995 goto out;
996 }
997 mutex_lock(&lower_dentry->d_inode->i_mutex);
998 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
999 size, flags);
1000 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1001out:
1002 return rc;
1003}
1004
Michael Halcrowdd2a3b72007-02-12 00:53:46 -08001005ssize_t
Michael Halcrow237fead2006-10-04 02:16:22 -07001006ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
1007 size_t size)
1008{
1009 int rc = 0;
1010 struct dentry *lower_dentry;
1011
1012 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1013 if (!lower_dentry->d_inode->i_op->getxattr) {
1014 rc = -ENOSYS;
1015 goto out;
1016 }
1017 mutex_lock(&lower_dentry->d_inode->i_mutex);
1018 rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
1019 size);
1020 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1021out:
1022 return rc;
1023}
1024
1025static ssize_t
1026ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
1027{
1028 int rc = 0;
1029 struct dentry *lower_dentry;
1030
1031 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1032 if (!lower_dentry->d_inode->i_op->listxattr) {
1033 rc = -ENOSYS;
1034 goto out;
1035 }
1036 mutex_lock(&lower_dentry->d_inode->i_mutex);
1037 rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
1038 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1039out:
1040 return rc;
1041}
1042
1043static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
1044{
1045 int rc = 0;
1046 struct dentry *lower_dentry;
1047
1048 lower_dentry = ecryptfs_dentry_to_lower(dentry);
1049 if (!lower_dentry->d_inode->i_op->removexattr) {
1050 rc = -ENOSYS;
1051 goto out;
1052 }
1053 mutex_lock(&lower_dentry->d_inode->i_mutex);
1054 rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
1055 mutex_unlock(&lower_dentry->d_inode->i_mutex);
1056out:
1057 return rc;
1058}
1059
1060int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
1061{
1062 if ((ecryptfs_inode_to_lower(inode)
1063 == (struct inode *)candidate_lower_inode))
1064 return 1;
1065 else
1066 return 0;
1067}
1068
1069int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
1070{
1071 ecryptfs_init_inode(inode, (struct inode *)lower_inode);
1072 return 0;
1073}
1074
Arjan van de Ven754661f2007-02-12 00:55:38 -08001075const struct inode_operations ecryptfs_symlink_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001076 .readlink = ecryptfs_readlink,
1077 .follow_link = ecryptfs_follow_link,
1078 .put_link = ecryptfs_put_link,
1079 .permission = ecryptfs_permission,
1080 .setattr = ecryptfs_setattr,
1081 .setxattr = ecryptfs_setxattr,
1082 .getxattr = ecryptfs_getxattr,
1083 .listxattr = ecryptfs_listxattr,
1084 .removexattr = ecryptfs_removexattr
1085};
1086
Arjan van de Ven754661f2007-02-12 00:55:38 -08001087const struct inode_operations ecryptfs_dir_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001088 .create = ecryptfs_create,
1089 .lookup = ecryptfs_lookup,
1090 .link = ecryptfs_link,
1091 .unlink = ecryptfs_unlink,
1092 .symlink = ecryptfs_symlink,
1093 .mkdir = ecryptfs_mkdir,
1094 .rmdir = ecryptfs_rmdir,
1095 .mknod = ecryptfs_mknod,
1096 .rename = ecryptfs_rename,
1097 .permission = ecryptfs_permission,
1098 .setattr = ecryptfs_setattr,
1099 .setxattr = ecryptfs_setxattr,
1100 .getxattr = ecryptfs_getxattr,
1101 .listxattr = ecryptfs_listxattr,
1102 .removexattr = ecryptfs_removexattr
1103};
1104
Arjan van de Ven754661f2007-02-12 00:55:38 -08001105const struct inode_operations ecryptfs_main_iops = {
Michael Halcrow237fead2006-10-04 02:16:22 -07001106 .permission = ecryptfs_permission,
1107 .setattr = ecryptfs_setattr,
1108 .setxattr = ecryptfs_setxattr,
1109 .getxattr = ecryptfs_getxattr,
1110 .listxattr = ecryptfs_listxattr,
1111 .removexattr = ecryptfs_removexattr
1112};