eCryptfs: do not try to open device files on mknod
[deliverable/linux.git] / fs / ecryptfs / inode.c
CommitLineData
237fead6
MH
1/**
2 * eCryptfs: Linux filesystem encryption layer
3 *
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
dd2a3b7a 6 * Copyright (C) 2004-2007 International Business Machines Corp.
237fead6
MH
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompsion <mcthomps@us.ibm.com>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 */
25
26#include <linux/file.h>
27#include <linux/vmalloc.h>
28#include <linux/pagemap.h>
29#include <linux/dcache.h>
30#include <linux/namei.h>
31#include <linux/mount.h>
32#include <linux/crypto.h>
0cc72dc7 33#include <linux/fs_stack.h>
0a688ad7 34#include <asm/unaligned.h>
237fead6
MH
35#include "ecryptfs_kernel.h"
36
37static struct dentry *lock_parent(struct dentry *dentry)
38{
39 struct dentry *dir;
40
8dc4e373 41 dir = dget_parent(dentry);
908e0a8a 42 mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
237fead6
MH
43 return dir;
44}
45
237fead6
MH
46static void unlock_dir(struct dentry *dir)
47{
48 mutex_unlock(&dir->d_inode->i_mutex);
49 dput(dir);
50}
51
237fead6
MH
52/**
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
4ac91378
JB
75 dentry_save = nd->path.dentry;
76 vfsmount_save = nd->path.mnt;
77 nd->path.dentry = lower_dentry;
78 nd->path.mnt = lower_mnt;
237fead6 79 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
4ac91378
JB
80 nd->path.dentry = dentry_save;
81 nd->path.mnt = vfsmount_save;
237fead6
MH
82 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);
801678c5 109 if (IS_ERR(lower_dir_dentry)) {
237fead6
MH
110 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);
4981e081 117 if (rc) {
caeeeecf 118 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
18d1dbf1 119 "rc = [%d]\n", __func__, rc);
caeeeecf 120 goto out_lock;
237fead6
MH
121 }
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 }
0cc72dc7
JJS
128 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
129 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
237fead6
MH
130out_lock:
131 unlock_dir(lower_dir_dentry);
132out:
133 return rc;
134}
135
136/**
137 * grow_file
d7cdc5fe 138 * @ecryptfs_dentry: the eCryptfs dentry
237fead6
MH
139 *
140 * This is the code which will grow the file to its correct size.
141 */
d7cdc5fe 142static int grow_file(struct dentry *ecryptfs_dentry)
237fead6 143{
d7cdc5fe 144 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
237fead6
MH
145 struct file fake_file;
146 struct ecryptfs_file_info tmp_file_info;
d7cdc5fe
MH
147 char zero_virt[] = { 0x00 };
148 int rc = 0;
237fead6
MH
149
150 memset(&fake_file, 0, sizeof(fake_file));
bd243a4b 151 fake_file.f_path.dentry = ecryptfs_dentry;
237fead6
MH
152 memset(&tmp_file_info, 0, sizeof(tmp_file_info));
153 ecryptfs_set_file_private(&fake_file, &tmp_file_info);
d7cdc5fe
MH
154 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;
237fead6
MH
162 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{
d7cdc5fe
MH
175 struct ecryptfs_crypt_stat *crypt_stat =
176 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
237fead6 177 int rc = 0;
237fead6 178
237fead6
MH
179 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
180 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
e2bd99ec 181 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
d7cdc5fe 182 goto out;
237fead6 183 }
e2bd99ec 184 crypt_stat->flags |= ECRYPTFS_NEW_FILE;
237fead6
MH
185 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
186 rc = ecryptfs_new_file_context(ecryptfs_dentry);
187 if (rc) {
d7cdc5fe
MH
188 ecryptfs_printk(KERN_ERR, "Error creating new file "
189 "context; rc = [%d]\n", rc);
190 goto out;
237fead6 191 }
d7cdc5fe 192 rc = ecryptfs_write_metadata(ecryptfs_dentry);
237fead6 193 if (rc) {
d7cdc5fe
MH
194 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
195 goto out;
237fead6 196 }
d7cdc5fe 197 rc = grow_file(ecryptfs_dentry);
5dda6992 198 if (rc)
d7cdc5fe 199 printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
237fead6
MH
200out:
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
4981e081
MH
221 /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
222 * the crypt_stat->lower_file (persistent file) */
237fead6
MH
223 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;
237fead6 252 char *encoded_name;
c381bfcf 253 int encoded_namelen;
237fead6 254 struct ecryptfs_crypt_stat *crypt_stat = NULL;
e77a56dd 255 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
237fead6
MH
256 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, "."))
45ec4aba
MH
263 || (dentry->d_name.len == 2
264 && !strcmp(dentry->d_name.name, ".."))) {
265 d_drop(dentry);
266 goto out;
267 }
237fead6
MH
268 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;
45ec4aba
MH
274 d_drop(dentry);
275 goto out;
237fead6
MH
276 }
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);
237fead6
MH
282 if (IS_ERR(lower_dentry)) {
283 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
284 rc = PTR_ERR(lower_dentry);
45ec4aba
MH
285 d_drop(dentry);
286 goto out;
237fead6 287 }
45ec4aba 288 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
237fead6
MH
289 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;
0cc72dc7 293 fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
237fead6
MH
294 BUG_ON(!atomic_read(&lower_dentry->d_count));
295 ecryptfs_set_dentry_private(dentry,
296 kmem_cache_alloc(ecryptfs_dentry_info_cache,
e94b1766 297 GFP_KERNEL));
237fead6
MH
298 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 }
72b55fff
MH
311 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb,
312 ECRYPTFS_INTERPOSE_FLAG_D_ADD);
237fead6
MH
313 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 }
202a21d6
RK
325 if (special_file(lower_inode->i_mode)) {
326 ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n");
327 goto out;
328 }
237fead6
MH
329 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 }
237fead6 334 /* Released in this function */
c3762229 335 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
dd2a3b7a 336 GFP_USER);
237fead6
MH
337 if (!page_virt) {
338 rc = -ENOMEM;
339 ecryptfs_printk(KERN_ERR,
340 "Cannot ecryptfs_kmalloc a page\n");
341 goto out_dput;
342 }
237fead6 343 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
e2bd99ec 344 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
237fead6 345 ecryptfs_set_default_sizes(crypt_stat);
d7cdc5fe
MH
346 rc = ecryptfs_read_and_validate_header_region(page_virt,
347 dentry->d_inode);
237fead6 348 if (rc) {
dd2a3b7a
MH
349 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;
237fead6
MH
355 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
356 goto out;
357 }
dd2a3b7a 358 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
237fead6 359 }
e77a56dd
MH
360 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)
cc11beff 364 file_size = (crypt_stat->num_header_bytes_at_front
e77a56dd
MH
365 + i_size_read(lower_dentry->d_inode));
366 else
367 file_size = i_size_read(lower_dentry->d_inode);
368 } else {
0a688ad7 369 file_size = get_unaligned_be64(page_virt);
e77a56dd 370 }
dd2a3b7a 371 i_size_write(dentry->d_inode, (loff_t)file_size);
237fead6
MH
372 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
373 goto out;
374
375out_dput:
376 dput(lower_dentry);
237fead6
MH
377 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;
0cc72dc7
JJS
404 fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
405 fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
237fead6
MH
406 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);
ae56fb16 413 d_drop(lower_old_dentry);
45ec4aba
MH
414 d_drop(new_dentry);
415 d_drop(old_dentry);
237fead6
MH
416 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);
8dc4e373 424 struct dentry *lower_dir_dentry;
237fead6 425
8dc4e373 426 lower_dir_dentry = lock_parent(lower_dentry);
237fead6
MH
427 rc = vfs_unlink(lower_dir_inode, lower_dentry);
428 if (rc) {
ae56fb16 429 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
237fead6
MH
430 goto out_unlock;
431 }
0cc72dc7 432 fsstack_copy_attr_times(dir, lower_dir_inode);
237fead6
MH
433 dentry->d_inode->i_nlink =
434 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
435 dentry->d_inode->i_ctime = dir->i_ctime;
caeeeecf 436 d_drop(dentry);
237fead6 437out_unlock:
8dc4e373 438 unlock_dir(lower_dir_dentry);
237fead6
MH
439 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;
c381bfcf 450 int encoded_symlen;
237fead6
MH
451 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;
0cc72dc7
JJS
472 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
473 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
237fead6
MH
474out_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;
0cc72dc7
JJS
496 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
497 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
237fead6
MH
498 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{
237fead6 508 struct dentry *lower_dentry;
237fead6 509 struct dentry *lower_dir_dentry;
45ec4aba 510 int rc;
237fead6
MH
511
512 lower_dentry = ecryptfs_dentry_to_lower(dentry);
45ec4aba 513 dget(dentry);
237fead6 514 lower_dir_dentry = lock_parent(lower_dentry);
45ec4aba 515 dget(lower_dentry);
237fead6 516 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
45ec4aba
MH
517 dput(lower_dentry);
518 if (!rc)
519 d_delete(lower_dentry);
0cc72dc7 520 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
237fead6
MH
521 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
522 unlock_dir(lower_dir_dentry);
523 if (!rc)
524 d_drop(dentry);
45ec4aba 525 dput(dentry);
237fead6
MH
526 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;
72b55fff
MH
541 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb,
542 ECRYPTFS_INTERPOSE_FLAG_DELAY_PERSISTENT_FILE);
237fead6
MH
543 if (rc)
544 goto out;
0cc72dc7
JJS
545 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
546 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
237fead6
MH
547out:
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;
0cc72dc7 575 fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
237fead6 576 if (new_dir != old_dir)
0cc72dc7 577 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
237fead6
MH
578out_lock:
579 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
a9083081
MH
580 dput(lower_new_dentry->d_parent);
581 dput(lower_old_dentry->d_parent);
237fead6
MH
582 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);
0cc72dc7
JJS
633 fsstack_copy_attr_atime(dentry->d_inode,
634 lower_dentry->d_inode);
237fead6
MH
635 }
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 *
cc11beff 684 * Calculate the required size of the lower file based on the
237fead6
MH
685 * 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
cc11beff 696 lower_size = crypt_stat->num_header_bytes_at_front;
237fead6
MH
697 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;
2ed92554 725 struct file fake_ecryptfs_file;
237fead6
MH
726 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));
bd243a4b 738 fake_ecryptfs_file.f_path.dentry = dentry;
237fead6
MH
739 /* Released at out_free: label */
740 ecryptfs_set_file_private(&fake_ecryptfs_file,
741 kmem_cache_alloc(ecryptfs_file_info_cache,
e94b1766 742 GFP_KERNEL));
237fead6
MH
743 if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
744 rc = -ENOMEM;
745 goto out;
746 }
747 lower_dentry = ecryptfs_dentry_to_lower(dentry);
2ed92554
MH
748 ecryptfs_set_file_lower(
749 &fake_ecryptfs_file,
750 ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
237fead6
MH
751 /* Switch on growing or shrinking file */
752 if (new_length > i_size) {
2ed92554
MH
753 char zero[] = { 0x00 };
754
755 /* 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);
237fead6 761 } else { /* new_length < i_size_read(inode) */
2ed92554
MH
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);
5dda6992 780 if (rc) {
240e2df5
MH
781 printk(KERN_ERR "Error attempting to zero out "
782 "the remainder of the end page on "
783 "reducing truncate; rc = [%d]\n", rc);
2ed92554 784 goto out_free;
240e2df5
MH
785 }
786 }
237fead6 787 vmtruncate(inode, new_length);
0216f7f7 788 rc = ecryptfs_write_inode_size_to_metadata(inode);
dd2a3b7a
MH
789 if (rc) {
790 printk(KERN_ERR "Problem with "
791 "ecryptfs_write_inode_size_to_metadata; "
792 "rc = [%d]\n", rc);
2ed92554 793 goto out_free;
dd2a3b7a 794 }
237fead6
MH
795 /* 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 }
237fead6
MH
805out_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) {
4ac91378
JB
819 struct vfsmount *vfsmnt_save = nd->path.mnt;
820 struct dentry *dentry_save = nd->path.dentry;
237fead6 821
4ac91378
JB
822 nd->path.mnt = ecryptfs_dentry_to_lower_mnt(nd->path.dentry);
823 nd->path.dentry = ecryptfs_dentry_to_lower(nd->path.dentry);
237fead6 824 rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
4ac91378
JB
825 nd->path.mnt = vfsmnt_save;
826 nd->path.dentry = dentry_save;
237fead6
MH
827 } 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;
e10f281b
MH
853 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
854 ecryptfs_init_crypt_stat(crypt_stat);
237fead6
MH
855 inode = dentry->d_inode;
856 lower_inode = ecryptfs_inode_to_lower(inode);
e10f281b
MH
857 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);
64ee4808
MH
861 else if (S_ISREG(dentry->d_inode->i_mode)
862 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
863 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
e10f281b 864 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
e10f281b 865
e10f281b
MH
866 mount_crypt_stat = &ecryptfs_superblock_to_private(
867 dentry->d_sb)->mount_crypt_stat;
d7cdc5fe 868 rc = ecryptfs_read_metadata(dentry);
5dda6992 869 if (rc) {
e10f281b
MH
870 if (!(mount_crypt_stat->flags
871 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
872 rc = -EIO;
25bd8174 873 printk(KERN_WARNING "Either the lower file "
e10f281b 874 "is not in a valid eCryptfs format, "
25bd8174
MH
875 "or the key could not be retrieved. "
876 "Plaintext passthrough mode is not "
e10f281b 877 "enabled; returning -EIO\n");
e10f281b 878 mutex_unlock(&crypt_stat->cs_mutex);
e10f281b
MH
879 goto out;
880 }
881 rc = 0;
882 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
883 mutex_unlock(&crypt_stat->cs_mutex);
e10f281b
MH
884 goto out;
885 }
e10f281b
MH
886 }
887 mutex_unlock(&crypt_stat->cs_mutex);
237fead6
MH
888 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 }
1ac564ec
JL
900
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
9c3580aa 908 mutex_lock(&lower_dentry->d_inode->i_mutex);
237fead6 909 rc = notify_change(lower_dentry, ia);
9c3580aa 910 mutex_unlock(&lower_dentry->d_inode->i_mutex);
237fead6 911out:
0cc72dc7 912 fsstack_copy_attr_all(inode, lower_inode, NULL);
237fead6
MH
913 return rc;
914}
915
dd2a3b7a 916int
237fead6
MH
917ecryptfs_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
d7cdc5fe
MH
936ssize_t
937ecryptfs_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
7896b631 954static ssize_t
237fead6
MH
955ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
956 size_t size)
957{
2ed92554
MH
958 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
959 value, size);
237fead6
MH
960}
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
754661f1 1012const struct inode_operations ecryptfs_symlink_iops = {
237fead6
MH
1013 .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
754661f1 1024const struct inode_operations ecryptfs_dir_iops = {
237fead6
MH
1025 .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
754661f1 1042const struct inode_operations ecryptfs_main_iops = {
237fead6
MH
1043 .permission = ecryptfs_permission,
1044 .setattr = ecryptfs_setattr,
1045 .setxattr = ecryptfs_setxattr,
1046 .getxattr = ecryptfs_getxattr,
1047 .listxattr = ecryptfs_listxattr,
1048 .removexattr = ecryptfs_removexattr
1049};
This page took 0.268761 seconds and 5 git commands to generate.