]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ecryptfs/inode.c
ecryptfs: crypto.c use unaligned byteorder helpers
[net-next-2.6.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>
237fead6
MH
34#include "ecryptfs_kernel.h"
35
36static struct dentry *lock_parent(struct dentry *dentry)
37{
38 struct dentry *dir;
39
8dc4e373 40 dir = dget_parent(dentry);
908e0a8a 41 mutex_lock_nested(&(dir->d_inode->i_mutex), I_MUTEX_PARENT);
237fead6
MH
42 return dir;
43}
44
237fead6
MH
45static void unlock_dir(struct dentry *dir)
46{
47 mutex_unlock(&dir->d_inode->i_mutex);
48 dput(dir);
49}
50
237fead6
MH
51/**
52 * ecryptfs_create_underlying_file
53 * @lower_dir_inode: inode of the parent in the lower fs of the new file
54 * @lower_dentry: New file's dentry in the lower fs
55 * @ecryptfs_dentry: New file's dentry in ecryptfs
56 * @mode: The mode of the new file
57 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
58 *
59 * Creates the file in the lower file system.
60 *
61 * Returns zero on success; non-zero on error condition
62 */
63static int
64ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
65 struct dentry *dentry, int mode,
66 struct nameidata *nd)
67{
68 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
69 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
70 struct dentry *dentry_save;
71 struct vfsmount *vfsmount_save;
72 int rc;
73
4ac91378
JB
74 dentry_save = nd->path.dentry;
75 vfsmount_save = nd->path.mnt;
76 nd->path.dentry = lower_dentry;
77 nd->path.mnt = lower_mnt;
237fead6 78 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
4ac91378
JB
79 nd->path.dentry = dentry_save;
80 nd->path.mnt = vfsmount_save;
237fead6
MH
81 return rc;
82}
83
84/**
85 * ecryptfs_do_create
86 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
87 * @ecryptfs_dentry: New file's dentry in ecryptfs
88 * @mode: The mode of the new file
89 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
90 *
91 * Creates the underlying file and the eCryptfs inode which will link to
92 * it. It will also update the eCryptfs directory inode to mimic the
93 * stat of the lower directory inode.
94 *
95 * Returns zero on success; non-zero on error condition
96 */
97static int
98ecryptfs_do_create(struct inode *directory_inode,
99 struct dentry *ecryptfs_dentry, int mode,
100 struct nameidata *nd)
101{
102 int rc;
103 struct dentry *lower_dentry;
104 struct dentry *lower_dir_dentry;
105
106 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
107 lower_dir_dentry = lock_parent(lower_dentry);
801678c5 108 if (IS_ERR(lower_dir_dentry)) {
237fead6
MH
109 ecryptfs_printk(KERN_ERR, "Error locking directory of "
110 "dentry\n");
111 rc = PTR_ERR(lower_dir_dentry);
112 goto out;
113 }
114 rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
115 ecryptfs_dentry, mode, nd);
4981e081 116 if (rc) {
caeeeecf 117 printk(KERN_ERR "%s: Failure to create dentry in lower fs; "
18d1dbf1 118 "rc = [%d]\n", __func__, rc);
caeeeecf 119 goto out_lock;
237fead6
MH
120 }
121 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
122 directory_inode->i_sb, 0);
123 if (rc) {
124 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
125 goto out_lock;
126 }
0cc72dc7
JJS
127 fsstack_copy_attr_times(directory_inode, lower_dir_dentry->d_inode);
128 fsstack_copy_inode_size(directory_inode, lower_dir_dentry->d_inode);
237fead6
MH
129out_lock:
130 unlock_dir(lower_dir_dentry);
131out:
132 return rc;
133}
134
135/**
136 * grow_file
d7cdc5fe 137 * @ecryptfs_dentry: the eCryptfs dentry
237fead6
MH
138 *
139 * This is the code which will grow the file to its correct size.
140 */
d7cdc5fe 141static int grow_file(struct dentry *ecryptfs_dentry)
237fead6 142{
d7cdc5fe 143 struct inode *ecryptfs_inode = ecryptfs_dentry->d_inode;
237fead6
MH
144 struct file fake_file;
145 struct ecryptfs_file_info tmp_file_info;
d7cdc5fe
MH
146 char zero_virt[] = { 0x00 };
147 int rc = 0;
237fead6
MH
148
149 memset(&fake_file, 0, sizeof(fake_file));
bd243a4b 150 fake_file.f_path.dentry = ecryptfs_dentry;
237fead6
MH
151 memset(&tmp_file_info, 0, sizeof(tmp_file_info));
152 ecryptfs_set_file_private(&fake_file, &tmp_file_info);
d7cdc5fe
MH
153 ecryptfs_set_file_lower(
154 &fake_file,
155 ecryptfs_inode_to_private(ecryptfs_inode)->lower_file);
156 rc = ecryptfs_write(&fake_file, zero_virt, 0, 1);
157 i_size_write(ecryptfs_inode, 0);
158 rc = ecryptfs_write_inode_size_to_metadata(ecryptfs_inode);
159 ecryptfs_inode_to_private(ecryptfs_inode)->crypt_stat.flags |=
160 ECRYPTFS_NEW_FILE;
237fead6
MH
161 return rc;
162}
163
164/**
165 * ecryptfs_initialize_file
166 *
167 * Cause the file to be changed from a basic empty file to an ecryptfs
168 * file with a header and first data page.
169 *
170 * Returns zero on success
171 */
172static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
173{
d7cdc5fe
MH
174 struct ecryptfs_crypt_stat *crypt_stat =
175 &ecryptfs_inode_to_private(ecryptfs_dentry->d_inode)->crypt_stat;
237fead6 176 int rc = 0;
237fead6 177
237fead6
MH
178 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
179 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
e2bd99ec 180 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
d7cdc5fe 181 goto out;
237fead6 182 }
e2bd99ec 183 crypt_stat->flags |= ECRYPTFS_NEW_FILE;
237fead6
MH
184 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
185 rc = ecryptfs_new_file_context(ecryptfs_dentry);
186 if (rc) {
d7cdc5fe
MH
187 ecryptfs_printk(KERN_ERR, "Error creating new file "
188 "context; rc = [%d]\n", rc);
189 goto out;
237fead6 190 }
d7cdc5fe 191 rc = ecryptfs_write_metadata(ecryptfs_dentry);
237fead6 192 if (rc) {
d7cdc5fe
MH
193 printk(KERN_ERR "Error writing headers; rc = [%d]\n", rc);
194 goto out;
237fead6 195 }
d7cdc5fe 196 rc = grow_file(ecryptfs_dentry);
5dda6992 197 if (rc)
d7cdc5fe 198 printk(KERN_ERR "Error growing file; rc = [%d]\n", rc);
237fead6
MH
199out:
200 return rc;
201}
202
203/**
204 * ecryptfs_create
205 * @dir: The inode of the directory in which to create the file.
206 * @dentry: The eCryptfs dentry
207 * @mode: The mode of the new file.
208 * @nd: nameidata
209 *
210 * Creates a new file.
211 *
212 * Returns zero on success; non-zero on error condition
213 */
214static int
215ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
216 int mode, struct nameidata *nd)
217{
218 int rc;
219
4981e081
MH
220 /* ecryptfs_do_create() calls ecryptfs_interpose(), which opens
221 * the crypt_stat->lower_file (persistent file) */
237fead6
MH
222 rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
223 if (unlikely(rc)) {
224 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
225 "lower filesystem\n");
226 goto out;
227 }
228 /* At this point, a file exists on "disk"; we need to make sure
229 * that this on disk file is prepared to be an ecryptfs file */
230 rc = ecryptfs_initialize_file(ecryptfs_dentry);
231out:
232 return rc;
233}
234
235/**
236 * ecryptfs_lookup
237 * @dir: inode
238 * @dentry: The dentry
239 * @nd: nameidata, may be NULL
240 *
241 * Find a file on disk. If the file does not exist, then we'll add it to the
242 * dentry cache and continue on to read it from the disk.
243 */
244static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
245 struct nameidata *nd)
246{
247 int rc = 0;
248 struct dentry *lower_dir_dentry;
249 struct dentry *lower_dentry;
250 struct vfsmount *lower_mnt;
237fead6 251 char *encoded_name;
c381bfcf 252 int encoded_namelen;
237fead6 253 struct ecryptfs_crypt_stat *crypt_stat = NULL;
e77a56dd 254 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
237fead6
MH
255 char *page_virt = NULL;
256 struct inode *lower_inode;
257 u64 file_size;
258
259 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
260 dentry->d_op = &ecryptfs_dops;
261 if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
45ec4aba
MH
262 || (dentry->d_name.len == 2
263 && !strcmp(dentry->d_name.name, ".."))) {
264 d_drop(dentry);
265 goto out;
266 }
237fead6
MH
267 encoded_namelen = ecryptfs_encode_filename(crypt_stat,
268 dentry->d_name.name,
269 dentry->d_name.len,
270 &encoded_name);
271 if (encoded_namelen < 0) {
272 rc = encoded_namelen;
45ec4aba
MH
273 d_drop(dentry);
274 goto out;
237fead6
MH
275 }
276 ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
277 "= [%d]\n", encoded_name, encoded_namelen);
278 lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
279 encoded_namelen - 1);
280 kfree(encoded_name);
237fead6
MH
281 if (IS_ERR(lower_dentry)) {
282 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
283 rc = PTR_ERR(lower_dentry);
45ec4aba
MH
284 d_drop(dentry);
285 goto out;
237fead6 286 }
45ec4aba 287 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
237fead6
MH
288 ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
289 "d_name.name = [%s]\n", lower_dentry,
290 lower_dentry->d_name.name);
291 lower_inode = lower_dentry->d_inode;
0cc72dc7 292 fsstack_copy_attr_atime(dir, lower_dir_dentry->d_inode);
237fead6
MH
293 BUG_ON(!atomic_read(&lower_dentry->d_count));
294 ecryptfs_set_dentry_private(dentry,
295 kmem_cache_alloc(ecryptfs_dentry_info_cache,
e94b1766 296 GFP_KERNEL));
237fead6
MH
297 if (!ecryptfs_dentry_to_private(dentry)) {
298 rc = -ENOMEM;
299 ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
300 "to allocate ecryptfs_dentry_info struct\n");
301 goto out_dput;
302 }
303 ecryptfs_set_dentry_lower(dentry, lower_dentry);
304 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
305 if (!lower_dentry->d_inode) {
306 /* We want to add because we couldn't find in lower */
307 d_add(dentry, NULL);
308 goto out;
309 }
310 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
311 if (rc) {
312 ecryptfs_printk(KERN_ERR, "Error interposing\n");
313 goto out_dput;
314 }
315 if (S_ISDIR(lower_inode->i_mode)) {
316 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
317 goto out;
318 }
319 if (S_ISLNK(lower_inode->i_mode)) {
320 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
321 goto out;
322 }
202a21d6
RK
323 if (special_file(lower_inode->i_mode)) {
324 ecryptfs_printk(KERN_DEBUG, "Is a special file; returning\n");
325 goto out;
326 }
237fead6
MH
327 if (!nd) {
328 ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
329 "as we *think* we are about to unlink\n");
330 goto out;
331 }
237fead6 332 /* Released in this function */
c3762229 333 page_virt = kmem_cache_zalloc(ecryptfs_header_cache_2,
dd2a3b7a 334 GFP_USER);
237fead6
MH
335 if (!page_virt) {
336 rc = -ENOMEM;
337 ecryptfs_printk(KERN_ERR,
338 "Cannot ecryptfs_kmalloc a page\n");
339 goto out_dput;
340 }
237fead6 341 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
e2bd99ec 342 if (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED))
237fead6 343 ecryptfs_set_default_sizes(crypt_stat);
d7cdc5fe
MH
344 rc = ecryptfs_read_and_validate_header_region(page_virt,
345 dentry->d_inode);
237fead6 346 if (rc) {
dd2a3b7a
MH
347 rc = ecryptfs_read_and_validate_xattr_region(page_virt, dentry);
348 if (rc) {
349 printk(KERN_DEBUG "Valid metadata not found in header "
350 "region or xattr region; treating file as "
351 "unencrypted\n");
352 rc = 0;
237fead6
MH
353 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
354 goto out;
355 }
dd2a3b7a 356 crypt_stat->flags |= ECRYPTFS_METADATA_IN_XATTR;
237fead6 357 }
e77a56dd
MH
358 mount_crypt_stat = &ecryptfs_superblock_to_private(
359 dentry->d_sb)->mount_crypt_stat;
360 if (mount_crypt_stat->flags & ECRYPTFS_ENCRYPTED_VIEW_ENABLED) {
361 if (crypt_stat->flags & ECRYPTFS_METADATA_IN_XATTR)
cc11beff 362 file_size = (crypt_stat->num_header_bytes_at_front
e77a56dd
MH
363 + i_size_read(lower_dentry->d_inode));
364 else
365 file_size = i_size_read(lower_dentry->d_inode);
366 } else {
367 memcpy(&file_size, page_virt, sizeof(file_size));
368 file_size = be64_to_cpu(file_size);
369 }
dd2a3b7a 370 i_size_write(dentry->d_inode, (loff_t)file_size);
237fead6
MH
371 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
372 goto out;
373
374out_dput:
375 dput(lower_dentry);
237fead6
MH
376 d_drop(dentry);
377out:
378 return ERR_PTR(rc);
379}
380
381static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
382 struct dentry *new_dentry)
383{
384 struct dentry *lower_old_dentry;
385 struct dentry *lower_new_dentry;
386 struct dentry *lower_dir_dentry;
387 u64 file_size_save;
388 int rc;
389
390 file_size_save = i_size_read(old_dentry->d_inode);
391 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
392 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
393 dget(lower_old_dentry);
394 dget(lower_new_dentry);
395 lower_dir_dentry = lock_parent(lower_new_dentry);
396 rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
397 lower_new_dentry);
398 if (rc || !lower_new_dentry->d_inode)
399 goto out_lock;
400 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
401 if (rc)
402 goto out_lock;
0cc72dc7
JJS
403 fsstack_copy_attr_times(dir, lower_new_dentry->d_inode);
404 fsstack_copy_inode_size(dir, lower_new_dentry->d_inode);
237fead6
MH
405 old_dentry->d_inode->i_nlink =
406 ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
407 i_size_write(new_dentry->d_inode, file_size_save);
408out_lock:
409 unlock_dir(lower_dir_dentry);
410 dput(lower_new_dentry);
411 dput(lower_old_dentry);
ae56fb16 412 d_drop(lower_old_dentry);
45ec4aba
MH
413 d_drop(new_dentry);
414 d_drop(old_dentry);
237fead6
MH
415 return rc;
416}
417
418static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
419{
420 int rc = 0;
421 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
422 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
8dc4e373 423 struct dentry *lower_dir_dentry;
237fead6 424
8dc4e373 425 lower_dir_dentry = lock_parent(lower_dentry);
237fead6
MH
426 rc = vfs_unlink(lower_dir_inode, lower_dentry);
427 if (rc) {
ae56fb16 428 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
237fead6
MH
429 goto out_unlock;
430 }
0cc72dc7 431 fsstack_copy_attr_times(dir, lower_dir_inode);
237fead6
MH
432 dentry->d_inode->i_nlink =
433 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
434 dentry->d_inode->i_ctime = dir->i_ctime;
caeeeecf 435 d_drop(dentry);
237fead6 436out_unlock:
8dc4e373 437 unlock_dir(lower_dir_dentry);
237fead6
MH
438 return rc;
439}
440
441static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
442 const char *symname)
443{
444 int rc;
445 struct dentry *lower_dentry;
446 struct dentry *lower_dir_dentry;
447 umode_t mode;
448 char *encoded_symname;
c381bfcf 449 int encoded_symlen;
237fead6
MH
450 struct ecryptfs_crypt_stat *crypt_stat = NULL;
451
452 lower_dentry = ecryptfs_dentry_to_lower(dentry);
453 dget(lower_dentry);
454 lower_dir_dentry = lock_parent(lower_dentry);
455 mode = S_IALLUGO;
456 encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
457 strlen(symname),
458 &encoded_symname);
459 if (encoded_symlen < 0) {
460 rc = encoded_symlen;
461 goto out_lock;
462 }
463 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
464 encoded_symname, mode);
465 kfree(encoded_symname);
466 if (rc || !lower_dentry->d_inode)
467 goto out_lock;
468 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
469 if (rc)
470 goto out_lock;
0cc72dc7
JJS
471 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
472 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
237fead6
MH
473out_lock:
474 unlock_dir(lower_dir_dentry);
475 dput(lower_dentry);
476 if (!dentry->d_inode)
477 d_drop(dentry);
478 return rc;
479}
480
481static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
482{
483 int rc;
484 struct dentry *lower_dentry;
485 struct dentry *lower_dir_dentry;
486
487 lower_dentry = ecryptfs_dentry_to_lower(dentry);
488 lower_dir_dentry = lock_parent(lower_dentry);
489 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
490 if (rc || !lower_dentry->d_inode)
491 goto out;
492 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
493 if (rc)
494 goto out;
0cc72dc7
JJS
495 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
496 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
237fead6
MH
497 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
498out:
499 unlock_dir(lower_dir_dentry);
500 if (!dentry->d_inode)
501 d_drop(dentry);
502 return rc;
503}
504
505static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
506{
237fead6 507 struct dentry *lower_dentry;
237fead6 508 struct dentry *lower_dir_dentry;
45ec4aba 509 int rc;
237fead6
MH
510
511 lower_dentry = ecryptfs_dentry_to_lower(dentry);
45ec4aba 512 dget(dentry);
237fead6 513 lower_dir_dentry = lock_parent(lower_dentry);
45ec4aba 514 dget(lower_dentry);
237fead6 515 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
45ec4aba
MH
516 dput(lower_dentry);
517 if (!rc)
518 d_delete(lower_dentry);
0cc72dc7 519 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
237fead6
MH
520 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
521 unlock_dir(lower_dir_dentry);
522 if (!rc)
523 d_drop(dentry);
45ec4aba 524 dput(dentry);
237fead6
MH
525 return rc;
526}
527
528static int
529ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
530{
531 int rc;
532 struct dentry *lower_dentry;
533 struct dentry *lower_dir_dentry;
534
535 lower_dentry = ecryptfs_dentry_to_lower(dentry);
536 lower_dir_dentry = lock_parent(lower_dentry);
537 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
538 if (rc || !lower_dentry->d_inode)
539 goto out;
540 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
541 if (rc)
542 goto out;
0cc72dc7
JJS
543 fsstack_copy_attr_times(dir, lower_dir_dentry->d_inode);
544 fsstack_copy_inode_size(dir, lower_dir_dentry->d_inode);
237fead6
MH
545out:
546 unlock_dir(lower_dir_dentry);
547 if (!dentry->d_inode)
548 d_drop(dentry);
549 return rc;
550}
551
552static int
553ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
554 struct inode *new_dir, struct dentry *new_dentry)
555{
556 int rc;
557 struct dentry *lower_old_dentry;
558 struct dentry *lower_new_dentry;
559 struct dentry *lower_old_dir_dentry;
560 struct dentry *lower_new_dir_dentry;
561
562 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
563 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
564 dget(lower_old_dentry);
565 dget(lower_new_dentry);
566 lower_old_dir_dentry = dget_parent(lower_old_dentry);
567 lower_new_dir_dentry = dget_parent(lower_new_dentry);
568 lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
569 rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
570 lower_new_dir_dentry->d_inode, lower_new_dentry);
571 if (rc)
572 goto out_lock;
0cc72dc7 573 fsstack_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode, NULL);
237fead6 574 if (new_dir != old_dir)
0cc72dc7 575 fsstack_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode, NULL);
237fead6
MH
576out_lock:
577 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
a9083081
MH
578 dput(lower_new_dentry->d_parent);
579 dput(lower_old_dentry->d_parent);
237fead6
MH
580 dput(lower_new_dentry);
581 dput(lower_old_dentry);
582 return rc;
583}
584
585static int
586ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
587{
588 int rc;
589 struct dentry *lower_dentry;
590 char *decoded_name;
591 char *lower_buf;
592 mm_segment_t old_fs;
593 struct ecryptfs_crypt_stat *crypt_stat;
594
595 lower_dentry = ecryptfs_dentry_to_lower(dentry);
596 if (!lower_dentry->d_inode->i_op ||
597 !lower_dentry->d_inode->i_op->readlink) {
598 rc = -EINVAL;
599 goto out;
600 }
601 /* Released in this function */
602 lower_buf = kmalloc(bufsiz, GFP_KERNEL);
603 if (lower_buf == NULL) {
604 ecryptfs_printk(KERN_ERR, "Out of memory\n");
605 rc = -ENOMEM;
606 goto out;
607 }
608 old_fs = get_fs();
609 set_fs(get_ds());
610 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
611 "lower_dentry->d_name.name = [%s]\n",
612 lower_dentry->d_name.name);
613 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
614 (char __user *)lower_buf,
615 bufsiz);
616 set_fs(old_fs);
617 if (rc >= 0) {
618 crypt_stat = NULL;
619 rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
620 &decoded_name);
621 if (rc == -ENOMEM)
622 goto out_free_lower_buf;
623 if (rc > 0) {
624 ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
625 "to userspace: [%*s]\n", rc,
626 decoded_name);
627 if (copy_to_user(buf, decoded_name, rc))
628 rc = -EFAULT;
629 }
630 kfree(decoded_name);
0cc72dc7
JJS
631 fsstack_copy_attr_atime(dentry->d_inode,
632 lower_dentry->d_inode);
237fead6
MH
633 }
634out_free_lower_buf:
635 kfree(lower_buf);
636out:
637 return rc;
638}
639
640static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
641{
642 char *buf;
643 int len = PAGE_SIZE, rc;
644 mm_segment_t old_fs;
645
646 /* Released in ecryptfs_put_link(); only release here on error */
647 buf = kmalloc(len, GFP_KERNEL);
648 if (!buf) {
649 rc = -ENOMEM;
650 goto out;
651 }
652 old_fs = get_fs();
653 set_fs(get_ds());
654 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
655 "dentry->d_name.name = [%s]\n", dentry->d_name.name);
656 rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
657 buf[rc] = '\0';
658 set_fs(old_fs);
659 if (rc < 0)
660 goto out_free;
661 rc = 0;
662 nd_set_link(nd, buf);
663 goto out;
664out_free:
665 kfree(buf);
666out:
667 return ERR_PTR(rc);
668}
669
670static void
671ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
672{
673 /* Free the char* */
674 kfree(nd_get_link(nd));
675}
676
677/**
678 * upper_size_to_lower_size
679 * @crypt_stat: Crypt_stat associated with file
680 * @upper_size: Size of the upper file
681 *
cc11beff 682 * Calculate the required size of the lower file based on the
237fead6
MH
683 * specified size of the upper file. This calculation is based on the
684 * number of headers in the underlying file and the extent size.
685 *
686 * Returns Calculated size of the lower file.
687 */
688static loff_t
689upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
690 loff_t upper_size)
691{
692 loff_t lower_size;
693
cc11beff 694 lower_size = crypt_stat->num_header_bytes_at_front;
237fead6
MH
695 if (upper_size != 0) {
696 loff_t num_extents;
697
698 num_extents = upper_size >> crypt_stat->extent_shift;
699 if (upper_size & ~crypt_stat->extent_mask)
700 num_extents++;
701 lower_size += (num_extents * crypt_stat->extent_size);
702 }
703 return lower_size;
704}
705
706/**
707 * ecryptfs_truncate
708 * @dentry: The ecryptfs layer dentry
709 * @new_length: The length to expand the file to
710 *
711 * Function to handle truncations modifying the size of the file. Note
712 * that the file sizes are interpolated. When expanding, we are simply
713 * writing strings of 0's out. When truncating, we need to modify the
714 * underlying file size according to the page index interpolations.
715 *
716 * Returns zero on success; non-zero otherwise
717 */
718int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
719{
720 int rc = 0;
721 struct inode *inode = dentry->d_inode;
722 struct dentry *lower_dentry;
2ed92554 723 struct file fake_ecryptfs_file;
237fead6
MH
724 struct ecryptfs_crypt_stat *crypt_stat;
725 loff_t i_size = i_size_read(inode);
726 loff_t lower_size_before_truncate;
727 loff_t lower_size_after_truncate;
728
729 if (unlikely((new_length == i_size)))
730 goto out;
731 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
732 /* Set up a fake ecryptfs file, this is used to interface with
733 * the file in the underlying filesystem so that the
734 * truncation has an effect there as well. */
735 memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
bd243a4b 736 fake_ecryptfs_file.f_path.dentry = dentry;
237fead6
MH
737 /* Released at out_free: label */
738 ecryptfs_set_file_private(&fake_ecryptfs_file,
739 kmem_cache_alloc(ecryptfs_file_info_cache,
e94b1766 740 GFP_KERNEL));
237fead6
MH
741 if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
742 rc = -ENOMEM;
743 goto out;
744 }
745 lower_dentry = ecryptfs_dentry_to_lower(dentry);
2ed92554
MH
746 ecryptfs_set_file_lower(
747 &fake_ecryptfs_file,
748 ecryptfs_inode_to_private(dentry->d_inode)->lower_file);
237fead6
MH
749 /* Switch on growing or shrinking file */
750 if (new_length > i_size) {
2ed92554
MH
751 char zero[] = { 0x00 };
752
753 /* Write a single 0 at the last position of the file;
754 * this triggers code that will fill in 0's throughout
755 * the intermediate portion of the previous end of the
756 * file and the new and of the file */
757 rc = ecryptfs_write(&fake_ecryptfs_file, zero,
758 (new_length - 1), 1);
237fead6 759 } else { /* new_length < i_size_read(inode) */
2ed92554
MH
760 /* We're chopping off all the pages down do the page
761 * in which new_length is located. Fill in the end of
762 * that page from (new_length & ~PAGE_CACHE_MASK) to
763 * PAGE_CACHE_SIZE with zeros. */
764 size_t num_zeros = (PAGE_CACHE_SIZE
765 - (new_length & ~PAGE_CACHE_MASK));
766
767 if (num_zeros) {
768 char *zeros_virt;
769
770 zeros_virt = kzalloc(num_zeros, GFP_KERNEL);
771 if (!zeros_virt) {
772 rc = -ENOMEM;
773 goto out_free;
774 }
775 rc = ecryptfs_write(&fake_ecryptfs_file, zeros_virt,
776 new_length, num_zeros);
777 kfree(zeros_virt);
5dda6992 778 if (rc) {
240e2df5
MH
779 printk(KERN_ERR "Error attempting to zero out "
780 "the remainder of the end page on "
781 "reducing truncate; rc = [%d]\n", rc);
2ed92554 782 goto out_free;
240e2df5
MH
783 }
784 }
237fead6 785 vmtruncate(inode, new_length);
0216f7f7 786 rc = ecryptfs_write_inode_size_to_metadata(inode);
dd2a3b7a
MH
787 if (rc) {
788 printk(KERN_ERR "Problem with "
789 "ecryptfs_write_inode_size_to_metadata; "
790 "rc = [%d]\n", rc);
2ed92554 791 goto out_free;
dd2a3b7a 792 }
237fead6
MH
793 /* We are reducing the size of the ecryptfs file, and need to
794 * know if we need to reduce the size of the lower file. */
795 lower_size_before_truncate =
796 upper_size_to_lower_size(crypt_stat, i_size);
797 lower_size_after_truncate =
798 upper_size_to_lower_size(crypt_stat, new_length);
799 if (lower_size_after_truncate < lower_size_before_truncate)
800 vmtruncate(lower_dentry->d_inode,
801 lower_size_after_truncate);
802 }
237fead6
MH
803out_free:
804 if (ecryptfs_file_to_private(&fake_ecryptfs_file))
805 kmem_cache_free(ecryptfs_file_info_cache,
806 ecryptfs_file_to_private(&fake_ecryptfs_file));
807out:
808 return rc;
809}
810
811static int
812ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
813{
814 int rc;
815
816 if (nd) {
4ac91378
JB
817 struct vfsmount *vfsmnt_save = nd->path.mnt;
818 struct dentry *dentry_save = nd->path.dentry;
237fead6 819
4ac91378
JB
820 nd->path.mnt = ecryptfs_dentry_to_lower_mnt(nd->path.dentry);
821 nd->path.dentry = ecryptfs_dentry_to_lower(nd->path.dentry);
237fead6 822 rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
4ac91378
JB
823 nd->path.mnt = vfsmnt_save;
824 nd->path.dentry = dentry_save;
237fead6
MH
825 } else
826 rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
827 return rc;
828}
829
830/**
831 * ecryptfs_setattr
832 * @dentry: dentry handle to the inode to modify
833 * @ia: Structure with flags of what to change and values
834 *
835 * Updates the metadata of an inode. If the update is to the size
836 * i.e. truncation, then ecryptfs_truncate will handle the size modification
837 * of both the ecryptfs inode and the lower inode.
838 *
839 * All other metadata changes will be passed right to the lower filesystem,
840 * and we will just update our inode to look like the lower.
841 */
842static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
843{
844 int rc = 0;
845 struct dentry *lower_dentry;
846 struct inode *inode;
847 struct inode *lower_inode;
848 struct ecryptfs_crypt_stat *crypt_stat;
849
850 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
e10f281b
MH
851 if (!(crypt_stat->flags & ECRYPTFS_STRUCT_INITIALIZED))
852 ecryptfs_init_crypt_stat(crypt_stat);
237fead6
MH
853 inode = dentry->d_inode;
854 lower_inode = ecryptfs_inode_to_lower(inode);
e10f281b
MH
855 lower_dentry = ecryptfs_dentry_to_lower(dentry);
856 mutex_lock(&crypt_stat->cs_mutex);
857 if (S_ISDIR(dentry->d_inode->i_mode))
858 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
64ee4808
MH
859 else if (S_ISREG(dentry->d_inode->i_mode)
860 && (!(crypt_stat->flags & ECRYPTFS_POLICY_APPLIED)
861 || !(crypt_stat->flags & ECRYPTFS_KEY_VALID))) {
e10f281b 862 struct ecryptfs_mount_crypt_stat *mount_crypt_stat;
e10f281b 863
e10f281b
MH
864 mount_crypt_stat = &ecryptfs_superblock_to_private(
865 dentry->d_sb)->mount_crypt_stat;
d7cdc5fe 866 rc = ecryptfs_read_metadata(dentry);
5dda6992 867 if (rc) {
e10f281b
MH
868 if (!(mount_crypt_stat->flags
869 & ECRYPTFS_PLAINTEXT_PASSTHROUGH_ENABLED)) {
870 rc = -EIO;
25bd8174 871 printk(KERN_WARNING "Either the lower file "
e10f281b 872 "is not in a valid eCryptfs format, "
25bd8174
MH
873 "or the key could not be retrieved. "
874 "Plaintext passthrough mode is not "
e10f281b 875 "enabled; returning -EIO\n");
e10f281b 876 mutex_unlock(&crypt_stat->cs_mutex);
e10f281b
MH
877 goto out;
878 }
879 rc = 0;
880 crypt_stat->flags &= ~(ECRYPTFS_ENCRYPTED);
881 mutex_unlock(&crypt_stat->cs_mutex);
e10f281b
MH
882 goto out;
883 }
e10f281b
MH
884 }
885 mutex_unlock(&crypt_stat->cs_mutex);
237fead6
MH
886 if (ia->ia_valid & ATTR_SIZE) {
887 ecryptfs_printk(KERN_DEBUG,
888 "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
889 ia->ia_valid, ATTR_SIZE);
890 rc = ecryptfs_truncate(dentry, ia->ia_size);
891 /* ecryptfs_truncate handles resizing of the lower file */
892 ia->ia_valid &= ~ATTR_SIZE;
893 ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
894 ia->ia_valid);
895 if (rc < 0)
896 goto out;
897 }
1ac564ec
JL
898
899 /*
900 * mode change is for clearing setuid/setgid bits. Allow lower fs
901 * to interpret this in its own way.
902 */
903 if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
904 ia->ia_valid &= ~ATTR_MODE;
905
9c3580aa 906 mutex_lock(&lower_dentry->d_inode->i_mutex);
237fead6 907 rc = notify_change(lower_dentry, ia);
9c3580aa 908 mutex_unlock(&lower_dentry->d_inode->i_mutex);
237fead6 909out:
0cc72dc7 910 fsstack_copy_attr_all(inode, lower_inode, NULL);
237fead6
MH
911 return rc;
912}
913
dd2a3b7a 914int
237fead6
MH
915ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
916 size_t size, int flags)
917{
918 int rc = 0;
919 struct dentry *lower_dentry;
920
921 lower_dentry = ecryptfs_dentry_to_lower(dentry);
922 if (!lower_dentry->d_inode->i_op->setxattr) {
923 rc = -ENOSYS;
924 goto out;
925 }
926 mutex_lock(&lower_dentry->d_inode->i_mutex);
927 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
928 size, flags);
929 mutex_unlock(&lower_dentry->d_inode->i_mutex);
930out:
931 return rc;
932}
933
d7cdc5fe
MH
934ssize_t
935ecryptfs_getxattr_lower(struct dentry *lower_dentry, const char *name,
936 void *value, size_t size)
937{
938 int rc = 0;
939
940 if (!lower_dentry->d_inode->i_op->getxattr) {
941 rc = -ENOSYS;
942 goto out;
943 }
944 mutex_lock(&lower_dentry->d_inode->i_mutex);
945 rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
946 size);
947 mutex_unlock(&lower_dentry->d_inode->i_mutex);
948out:
949 return rc;
950}
951
7896b631 952static ssize_t
237fead6
MH
953ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
954 size_t size)
955{
2ed92554
MH
956 return ecryptfs_getxattr_lower(ecryptfs_dentry_to_lower(dentry), name,
957 value, size);
237fead6
MH
958}
959
960static ssize_t
961ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
962{
963 int rc = 0;
964 struct dentry *lower_dentry;
965
966 lower_dentry = ecryptfs_dentry_to_lower(dentry);
967 if (!lower_dentry->d_inode->i_op->listxattr) {
968 rc = -ENOSYS;
969 goto out;
970 }
971 mutex_lock(&lower_dentry->d_inode->i_mutex);
972 rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
973 mutex_unlock(&lower_dentry->d_inode->i_mutex);
974out:
975 return rc;
976}
977
978static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
979{
980 int rc = 0;
981 struct dentry *lower_dentry;
982
983 lower_dentry = ecryptfs_dentry_to_lower(dentry);
984 if (!lower_dentry->d_inode->i_op->removexattr) {
985 rc = -ENOSYS;
986 goto out;
987 }
988 mutex_lock(&lower_dentry->d_inode->i_mutex);
989 rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
990 mutex_unlock(&lower_dentry->d_inode->i_mutex);
991out:
992 return rc;
993}
994
995int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
996{
997 if ((ecryptfs_inode_to_lower(inode)
998 == (struct inode *)candidate_lower_inode))
999 return 1;
1000 else
1001 return 0;
1002}
1003
1004int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
1005{
1006 ecryptfs_init_inode(inode, (struct inode *)lower_inode);
1007 return 0;
1008}
1009
754661f1 1010const struct inode_operations ecryptfs_symlink_iops = {
237fead6
MH
1011 .readlink = ecryptfs_readlink,
1012 .follow_link = ecryptfs_follow_link,
1013 .put_link = ecryptfs_put_link,
1014 .permission = ecryptfs_permission,
1015 .setattr = ecryptfs_setattr,
1016 .setxattr = ecryptfs_setxattr,
1017 .getxattr = ecryptfs_getxattr,
1018 .listxattr = ecryptfs_listxattr,
1019 .removexattr = ecryptfs_removexattr
1020};
1021
754661f1 1022const struct inode_operations ecryptfs_dir_iops = {
237fead6
MH
1023 .create = ecryptfs_create,
1024 .lookup = ecryptfs_lookup,
1025 .link = ecryptfs_link,
1026 .unlink = ecryptfs_unlink,
1027 .symlink = ecryptfs_symlink,
1028 .mkdir = ecryptfs_mkdir,
1029 .rmdir = ecryptfs_rmdir,
1030 .mknod = ecryptfs_mknod,
1031 .rename = ecryptfs_rename,
1032 .permission = ecryptfs_permission,
1033 .setattr = ecryptfs_setattr,
1034 .setxattr = ecryptfs_setxattr,
1035 .getxattr = ecryptfs_getxattr,
1036 .listxattr = ecryptfs_listxattr,
1037 .removexattr = ecryptfs_removexattr
1038};
1039
754661f1 1040const struct inode_operations ecryptfs_main_iops = {
237fead6
MH
1041 .permission = ecryptfs_permission,
1042 .setattr = ecryptfs_setattr,
1043 .setxattr = ecryptfs_setxattr,
1044 .getxattr = ecryptfs_getxattr,
1045 .listxattr = ecryptfs_listxattr,
1046 .removexattr = ecryptfs_removexattr
1047};