Linux 2.6.19.4
[linux/fpc-iii.git] / fs / ecryptfs / inode.c
blobdfcc68484f47068ccb3d256283cfea67857518f6
1 /**
2 * eCryptfs: Linux filesystem encryption layer
4 * Copyright (C) 1997-2004 Erez Zadok
5 * Copyright (C) 2001-2004 Stony Brook University
6 * Copyright (C) 2004-2006 International Business Machines Corp.
7 * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com>
8 * Michael C. Thompsion <mcthomps@us.ibm.com>
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.
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.
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.
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>
33 #include "ecryptfs_kernel.h"
35 static struct dentry *lock_parent(struct dentry *dentry)
37 struct dentry *dir;
39 dir = dget(dentry->d_parent);
40 mutex_lock(&(dir->d_inode->i_mutex));
41 return dir;
44 static void unlock_parent(struct dentry *dentry)
46 mutex_unlock(&(dentry->d_parent->d_inode->i_mutex));
47 dput(dentry->d_parent);
50 static void unlock_dir(struct dentry *dir)
52 mutex_unlock(&dir->d_inode->i_mutex);
53 dput(dir);
56 void ecryptfs_copy_inode_size(struct inode *dst, const struct inode *src)
58 i_size_write(dst, i_size_read((struct inode *)src));
59 dst->i_blocks = src->i_blocks;
62 void ecryptfs_copy_attr_atime(struct inode *dest, const struct inode *src)
64 dest->i_atime = src->i_atime;
67 static void ecryptfs_copy_attr_times(struct inode *dest,
68 const struct inode *src)
70 dest->i_atime = src->i_atime;
71 dest->i_mtime = src->i_mtime;
72 dest->i_ctime = src->i_ctime;
75 static void ecryptfs_copy_attr_timesizes(struct inode *dest,
76 const struct inode *src)
78 dest->i_atime = src->i_atime;
79 dest->i_mtime = src->i_mtime;
80 dest->i_ctime = src->i_ctime;
81 ecryptfs_copy_inode_size(dest, src);
84 void ecryptfs_copy_attr_all(struct inode *dest, const struct inode *src)
86 dest->i_mode = src->i_mode;
87 dest->i_nlink = src->i_nlink;
88 dest->i_uid = src->i_uid;
89 dest->i_gid = src->i_gid;
90 dest->i_rdev = src->i_rdev;
91 dest->i_atime = src->i_atime;
92 dest->i_mtime = src->i_mtime;
93 dest->i_ctime = src->i_ctime;
94 dest->i_blkbits = src->i_blkbits;
95 dest->i_flags = src->i_flags;
98 /**
99 * ecryptfs_create_underlying_file
100 * @lower_dir_inode: inode of the parent in the lower fs of the new file
101 * @lower_dentry: New file's dentry in the lower fs
102 * @ecryptfs_dentry: New file's dentry in ecryptfs
103 * @mode: The mode of the new file
104 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
106 * Creates the file in the lower file system.
108 * Returns zero on success; non-zero on error condition
110 static int
111 ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
112 struct dentry *dentry, int mode,
113 struct nameidata *nd)
115 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
116 struct vfsmount *lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
117 struct dentry *dentry_save;
118 struct vfsmount *vfsmount_save;
119 int rc;
121 dentry_save = nd->dentry;
122 vfsmount_save = nd->mnt;
123 nd->dentry = lower_dentry;
124 nd->mnt = lower_mnt;
125 rc = vfs_create(lower_dir_inode, lower_dentry, mode, nd);
126 nd->dentry = dentry_save;
127 nd->mnt = vfsmount_save;
128 return rc;
132 * ecryptfs_do_create
133 * @directory_inode: inode of the new file's dentry's parent in ecryptfs
134 * @ecryptfs_dentry: New file's dentry in ecryptfs
135 * @mode: The mode of the new file
136 * @nd: nameidata of ecryptfs' parent's dentry & vfsmount
138 * Creates the underlying file and the eCryptfs inode which will link to
139 * it. It will also update the eCryptfs directory inode to mimic the
140 * stat of the lower directory inode.
142 * Returns zero on success; non-zero on error condition
144 static int
145 ecryptfs_do_create(struct inode *directory_inode,
146 struct dentry *ecryptfs_dentry, int mode,
147 struct nameidata *nd)
149 int rc;
150 struct dentry *lower_dentry;
151 struct dentry *lower_dir_dentry;
153 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
154 lower_dir_dentry = lock_parent(lower_dentry);
155 if (unlikely(IS_ERR(lower_dir_dentry))) {
156 ecryptfs_printk(KERN_ERR, "Error locking directory of "
157 "dentry\n");
158 rc = PTR_ERR(lower_dir_dentry);
159 goto out;
161 rc = ecryptfs_create_underlying_file(lower_dir_dentry->d_inode,
162 ecryptfs_dentry, mode, nd);
163 if (unlikely(rc)) {
164 ecryptfs_printk(KERN_ERR,
165 "Failure to create underlying file\n");
166 goto out_lock;
168 rc = ecryptfs_interpose(lower_dentry, ecryptfs_dentry,
169 directory_inode->i_sb, 0);
170 if (rc) {
171 ecryptfs_printk(KERN_ERR, "Failure in ecryptfs_interpose\n");
172 goto out_lock;
174 ecryptfs_copy_attr_timesizes(directory_inode,
175 lower_dir_dentry->d_inode);
176 out_lock:
177 unlock_dir(lower_dir_dentry);
178 out:
179 return rc;
183 * grow_file
184 * @ecryptfs_dentry: the ecryptfs dentry
185 * @lower_file: The lower file
186 * @inode: The ecryptfs inode
187 * @lower_inode: The lower inode
189 * This is the code which will grow the file to its correct size.
191 static int grow_file(struct dentry *ecryptfs_dentry, struct file *lower_file,
192 struct inode *inode, struct inode *lower_inode)
194 int rc = 0;
195 struct file fake_file;
196 struct ecryptfs_file_info tmp_file_info;
198 memset(&fake_file, 0, sizeof(fake_file));
199 fake_file.f_dentry = ecryptfs_dentry;
200 memset(&tmp_file_info, 0, sizeof(tmp_file_info));
201 ecryptfs_set_file_private(&fake_file, &tmp_file_info);
202 ecryptfs_set_file_lower(&fake_file, lower_file);
203 rc = ecryptfs_fill_zeros(&fake_file, 1);
204 if (rc) {
205 ECRYPTFS_SET_FLAG(
206 ecryptfs_inode_to_private(inode)->crypt_stat.flags,
207 ECRYPTFS_SECURITY_WARNING);
208 ecryptfs_printk(KERN_WARNING, "Error attempting to fill zeros "
209 "in file; rc = [%d]\n", rc);
210 goto out;
212 i_size_write(inode, 0);
213 ecryptfs_write_inode_size_to_header(lower_file, lower_inode, inode);
214 ECRYPTFS_SET_FLAG(ecryptfs_inode_to_private(inode)->crypt_stat.flags,
215 ECRYPTFS_NEW_FILE);
216 out:
217 return rc;
221 * ecryptfs_initialize_file
223 * Cause the file to be changed from a basic empty file to an ecryptfs
224 * file with a header and first data page.
226 * Returns zero on success
228 static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
230 int rc = 0;
231 int lower_flags;
232 struct ecryptfs_crypt_stat *crypt_stat;
233 struct dentry *lower_dentry;
234 struct file *lower_file;
235 struct inode *inode, *lower_inode;
236 struct vfsmount *lower_mnt;
238 lower_dentry = ecryptfs_dentry_to_lower(ecryptfs_dentry);
239 ecryptfs_printk(KERN_DEBUG, "lower_dentry->d_name.name = [%s]\n",
240 lower_dentry->d_name.name);
241 inode = ecryptfs_dentry->d_inode;
242 crypt_stat = &ecryptfs_inode_to_private(inode)->crypt_stat;
243 lower_flags = ((O_CREAT | O_WRONLY | O_TRUNC) & O_ACCMODE) | O_RDWR;
244 #if BITS_PER_LONG != 32
245 lower_flags |= O_LARGEFILE;
246 #endif
247 lower_mnt = ecryptfs_dentry_to_lower_mnt(ecryptfs_dentry);
248 /* Corresponding fput() at end of this function */
249 if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
250 lower_flags))) {
251 ecryptfs_printk(KERN_ERR,
252 "Error opening dentry; rc = [%i]\n", rc);
253 goto out;
255 lower_inode = lower_dentry->d_inode;
256 if (S_ISDIR(ecryptfs_dentry->d_inode->i_mode)) {
257 ecryptfs_printk(KERN_DEBUG, "This is a directory\n");
258 ECRYPTFS_CLEAR_FLAG(crypt_stat->flags, ECRYPTFS_ENCRYPTED);
259 goto out_fput;
261 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_NEW_FILE);
262 ecryptfs_printk(KERN_DEBUG, "Initializing crypto context\n");
263 rc = ecryptfs_new_file_context(ecryptfs_dentry);
264 if (rc) {
265 ecryptfs_printk(KERN_DEBUG, "Error creating new file "
266 "context\n");
267 goto out_fput;
269 rc = ecryptfs_write_headers(ecryptfs_dentry, lower_file);
270 if (rc) {
271 ecryptfs_printk(KERN_DEBUG, "Error writing headers\n");
272 goto out_fput;
274 rc = grow_file(ecryptfs_dentry, lower_file, inode, lower_inode);
275 out_fput:
276 if ((rc = ecryptfs_close_lower_file(lower_file)))
277 printk(KERN_ERR "Error closing lower_file\n");
278 out:
279 return rc;
283 * ecryptfs_create
284 * @dir: The inode of the directory in which to create the file.
285 * @dentry: The eCryptfs dentry
286 * @mode: The mode of the new file.
287 * @nd: nameidata
289 * Creates a new file.
291 * Returns zero on success; non-zero on error condition
293 static int
294 ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
295 int mode, struct nameidata *nd)
297 int rc;
299 rc = ecryptfs_do_create(directory_inode, ecryptfs_dentry, mode, nd);
300 if (unlikely(rc)) {
301 ecryptfs_printk(KERN_WARNING, "Failed to create file in"
302 "lower filesystem\n");
303 goto out;
305 /* At this point, a file exists on "disk"; we need to make sure
306 * that this on disk file is prepared to be an ecryptfs file */
307 rc = ecryptfs_initialize_file(ecryptfs_dentry);
308 out:
309 return rc;
313 * ecryptfs_lookup
314 * @dir: inode
315 * @dentry: The dentry
316 * @nd: nameidata, may be NULL
318 * Find a file on disk. If the file does not exist, then we'll add it to the
319 * dentry cache and continue on to read it from the disk.
321 static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
322 struct nameidata *nd)
324 int rc = 0;
325 struct dentry *lower_dir_dentry;
326 struct dentry *lower_dentry;
327 struct vfsmount *lower_mnt;
328 char *encoded_name;
329 unsigned int encoded_namelen;
330 struct ecryptfs_crypt_stat *crypt_stat = NULL;
331 char *page_virt = NULL;
332 struct inode *lower_inode;
333 u64 file_size;
335 lower_dir_dentry = ecryptfs_dentry_to_lower(dentry->d_parent);
336 dentry->d_op = &ecryptfs_dops;
337 if ((dentry->d_name.len == 1 && !strcmp(dentry->d_name.name, "."))
338 || (dentry->d_name.len == 2
339 && !strcmp(dentry->d_name.name, ".."))) {
340 d_drop(dentry);
341 goto out;
343 encoded_namelen = ecryptfs_encode_filename(crypt_stat,
344 dentry->d_name.name,
345 dentry->d_name.len,
346 &encoded_name);
347 if (encoded_namelen < 0) {
348 rc = encoded_namelen;
349 d_drop(dentry);
350 goto out;
352 ecryptfs_printk(KERN_DEBUG, "encoded_name = [%s]; encoded_namelen "
353 "= [%d]\n", encoded_name, encoded_namelen);
354 lower_dentry = lookup_one_len(encoded_name, lower_dir_dentry,
355 encoded_namelen - 1);
356 kfree(encoded_name);
357 if (IS_ERR(lower_dentry)) {
358 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
359 rc = PTR_ERR(lower_dentry);
360 d_drop(dentry);
361 goto out;
363 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
364 ecryptfs_printk(KERN_DEBUG, "lower_dentry = [%p]; lower_dentry->"
365 "d_name.name = [%s]\n", lower_dentry,
366 lower_dentry->d_name.name);
367 lower_inode = lower_dentry->d_inode;
368 ecryptfs_copy_attr_atime(dir, lower_dir_dentry->d_inode);
369 BUG_ON(!atomic_read(&lower_dentry->d_count));
370 ecryptfs_set_dentry_private(dentry,
371 kmem_cache_alloc(ecryptfs_dentry_info_cache,
372 SLAB_KERNEL));
373 if (!ecryptfs_dentry_to_private(dentry)) {
374 rc = -ENOMEM;
375 ecryptfs_printk(KERN_ERR, "Out of memory whilst attempting "
376 "to allocate ecryptfs_dentry_info struct\n");
377 goto out_dput;
379 ecryptfs_set_dentry_lower(dentry, lower_dentry);
380 ecryptfs_set_dentry_lower_mnt(dentry, lower_mnt);
381 if (!lower_dentry->d_inode) {
382 /* We want to add because we couldn't find in lower */
383 d_add(dentry, NULL);
384 goto out;
386 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 1);
387 if (rc) {
388 ecryptfs_printk(KERN_ERR, "Error interposing\n");
389 goto out_dput;
391 if (S_ISDIR(lower_inode->i_mode)) {
392 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
393 goto out;
395 if (S_ISLNK(lower_inode->i_mode)) {
396 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
397 goto out;
399 if (!nd) {
400 ecryptfs_printk(KERN_DEBUG, "We have a NULL nd, just leave"
401 "as we *think* we are about to unlink\n");
402 goto out;
404 /* Released in this function */
405 page_virt =
406 (char *)kmem_cache_alloc(ecryptfs_header_cache_2,
407 SLAB_USER);
408 if (!page_virt) {
409 rc = -ENOMEM;
410 ecryptfs_printk(KERN_ERR,
411 "Cannot ecryptfs_kmalloc a page\n");
412 goto out_dput;
414 memset(page_virt, 0, PAGE_CACHE_SIZE);
415 rc = ecryptfs_read_header_region(page_virt, lower_dentry, nd->mnt);
416 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
417 if (!ECRYPTFS_CHECK_FLAG(crypt_stat->flags, ECRYPTFS_POLICY_APPLIED))
418 ecryptfs_set_default_sizes(crypt_stat);
419 if (rc) {
420 rc = 0;
421 ecryptfs_printk(KERN_WARNING, "Error reading header region;"
422 " assuming unencrypted\n");
423 } else {
424 if (!contains_ecryptfs_marker(page_virt
425 + ECRYPTFS_FILE_SIZE_BYTES)) {
426 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
427 goto out;
429 memcpy(&file_size, page_virt, sizeof(file_size));
430 file_size = be64_to_cpu(file_size);
431 i_size_write(dentry->d_inode, (loff_t)file_size);
433 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
434 goto out;
436 out_dput:
437 dput(lower_dentry);
438 d_drop(dentry);
439 out:
440 return ERR_PTR(rc);
443 static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
444 struct dentry *new_dentry)
446 struct dentry *lower_old_dentry;
447 struct dentry *lower_new_dentry;
448 struct dentry *lower_dir_dentry;
449 u64 file_size_save;
450 int rc;
452 file_size_save = i_size_read(old_dentry->d_inode);
453 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
454 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
455 dget(lower_old_dentry);
456 dget(lower_new_dentry);
457 lower_dir_dentry = lock_parent(lower_new_dentry);
458 rc = vfs_link(lower_old_dentry, lower_dir_dentry->d_inode,
459 lower_new_dentry);
460 if (rc || !lower_new_dentry->d_inode)
461 goto out_lock;
462 rc = ecryptfs_interpose(lower_new_dentry, new_dentry, dir->i_sb, 0);
463 if (rc)
464 goto out_lock;
465 ecryptfs_copy_attr_timesizes(dir, lower_new_dentry->d_inode);
466 old_dentry->d_inode->i_nlink =
467 ecryptfs_inode_to_lower(old_dentry->d_inode)->i_nlink;
468 i_size_write(new_dentry->d_inode, file_size_save);
469 out_lock:
470 unlock_dir(lower_dir_dentry);
471 dput(lower_new_dentry);
472 dput(lower_old_dentry);
473 d_drop(lower_old_dentry);
474 d_drop(new_dentry);
475 d_drop(old_dentry);
476 return rc;
479 static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
481 int rc = 0;
482 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
483 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
485 lock_parent(lower_dentry);
486 rc = vfs_unlink(lower_dir_inode, lower_dentry);
487 if (rc) {
488 printk(KERN_ERR "Error in vfs_unlink; rc = [%d]\n", rc);
489 goto out_unlock;
491 ecryptfs_copy_attr_times(dir, lower_dir_inode);
492 dentry->d_inode->i_nlink =
493 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
494 dentry->d_inode->i_ctime = dir->i_ctime;
495 out_unlock:
496 unlock_parent(lower_dentry);
497 return rc;
500 static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
501 const char *symname)
503 int rc;
504 struct dentry *lower_dentry;
505 struct dentry *lower_dir_dentry;
506 umode_t mode;
507 char *encoded_symname;
508 unsigned int encoded_symlen;
509 struct ecryptfs_crypt_stat *crypt_stat = NULL;
511 lower_dentry = ecryptfs_dentry_to_lower(dentry);
512 dget(lower_dentry);
513 lower_dir_dentry = lock_parent(lower_dentry);
514 mode = S_IALLUGO;
515 encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
516 strlen(symname),
517 &encoded_symname);
518 if (encoded_symlen < 0) {
519 rc = encoded_symlen;
520 goto out_lock;
522 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
523 encoded_symname, mode);
524 kfree(encoded_symname);
525 if (rc || !lower_dentry->d_inode)
526 goto out_lock;
527 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
528 if (rc)
529 goto out_lock;
530 ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
531 out_lock:
532 unlock_dir(lower_dir_dentry);
533 dput(lower_dentry);
534 if (!dentry->d_inode)
535 d_drop(dentry);
536 return rc;
539 static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
541 int rc;
542 struct dentry *lower_dentry;
543 struct dentry *lower_dir_dentry;
545 lower_dentry = ecryptfs_dentry_to_lower(dentry);
546 lower_dir_dentry = lock_parent(lower_dentry);
547 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
548 if (rc || !lower_dentry->d_inode)
549 goto out;
550 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
551 if (rc)
552 goto out;
553 ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
554 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
555 out:
556 unlock_dir(lower_dir_dentry);
557 if (!dentry->d_inode)
558 d_drop(dentry);
559 return rc;
562 static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
564 struct dentry *lower_dentry;
565 struct dentry *lower_dir_dentry;
566 int rc;
568 lower_dentry = ecryptfs_dentry_to_lower(dentry);
569 dget(dentry);
570 lower_dir_dentry = lock_parent(lower_dentry);
571 dget(lower_dentry);
572 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
573 dput(lower_dentry);
574 if (!rc)
575 d_delete(lower_dentry);
576 ecryptfs_copy_attr_times(dir, lower_dir_dentry->d_inode);
577 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
578 unlock_dir(lower_dir_dentry);
579 if (!rc)
580 d_drop(dentry);
581 dput(dentry);
582 return rc;
585 static int
586 ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
588 int rc;
589 struct dentry *lower_dentry;
590 struct dentry *lower_dir_dentry;
592 lower_dentry = ecryptfs_dentry_to_lower(dentry);
593 lower_dir_dentry = lock_parent(lower_dentry);
594 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
595 if (rc || !lower_dentry->d_inode)
596 goto out;
597 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
598 if (rc)
599 goto out;
600 ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
601 out:
602 unlock_dir(lower_dir_dentry);
603 if (!dentry->d_inode)
604 d_drop(dentry);
605 return rc;
608 static int
609 ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
610 struct inode *new_dir, struct dentry *new_dentry)
612 int rc;
613 struct dentry *lower_old_dentry;
614 struct dentry *lower_new_dentry;
615 struct dentry *lower_old_dir_dentry;
616 struct dentry *lower_new_dir_dentry;
618 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
619 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
620 dget(lower_old_dentry);
621 dget(lower_new_dentry);
622 lower_old_dir_dentry = dget_parent(lower_old_dentry);
623 lower_new_dir_dentry = dget_parent(lower_new_dentry);
624 lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
625 rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
626 lower_new_dir_dentry->d_inode, lower_new_dentry);
627 if (rc)
628 goto out_lock;
629 ecryptfs_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
630 if (new_dir != old_dir)
631 ecryptfs_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
632 out_lock:
633 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
634 dput(lower_new_dentry->d_parent);
635 dput(lower_old_dentry->d_parent);
636 dput(lower_new_dentry);
637 dput(lower_old_dentry);
638 return rc;
641 static int
642 ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
644 int rc;
645 struct dentry *lower_dentry;
646 char *decoded_name;
647 char *lower_buf;
648 mm_segment_t old_fs;
649 struct ecryptfs_crypt_stat *crypt_stat;
651 lower_dentry = ecryptfs_dentry_to_lower(dentry);
652 if (!lower_dentry->d_inode->i_op ||
653 !lower_dentry->d_inode->i_op->readlink) {
654 rc = -EINVAL;
655 goto out;
657 /* Released in this function */
658 lower_buf = kmalloc(bufsiz, GFP_KERNEL);
659 if (lower_buf == NULL) {
660 ecryptfs_printk(KERN_ERR, "Out of memory\n");
661 rc = -ENOMEM;
662 goto out;
664 old_fs = get_fs();
665 set_fs(get_ds());
666 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
667 "lower_dentry->d_name.name = [%s]\n",
668 lower_dentry->d_name.name);
669 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
670 (char __user *)lower_buf,
671 bufsiz);
672 set_fs(old_fs);
673 if (rc >= 0) {
674 crypt_stat = NULL;
675 rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
676 &decoded_name);
677 if (rc == -ENOMEM)
678 goto out_free_lower_buf;
679 if (rc > 0) {
680 ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
681 "to userspace: [%*s]\n", rc,
682 decoded_name);
683 if (copy_to_user(buf, decoded_name, rc))
684 rc = -EFAULT;
686 kfree(decoded_name);
687 ecryptfs_copy_attr_atime(dentry->d_inode,
688 lower_dentry->d_inode);
690 out_free_lower_buf:
691 kfree(lower_buf);
692 out:
693 return rc;
696 static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
698 char *buf;
699 int len = PAGE_SIZE, rc;
700 mm_segment_t old_fs;
702 /* Released in ecryptfs_put_link(); only release here on error */
703 buf = kmalloc(len, GFP_KERNEL);
704 if (!buf) {
705 rc = -ENOMEM;
706 goto out;
708 old_fs = get_fs();
709 set_fs(get_ds());
710 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
711 "dentry->d_name.name = [%s]\n", dentry->d_name.name);
712 rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
713 buf[rc] = '\0';
714 set_fs(old_fs);
715 if (rc < 0)
716 goto out_free;
717 rc = 0;
718 nd_set_link(nd, buf);
719 goto out;
720 out_free:
721 kfree(buf);
722 out:
723 return ERR_PTR(rc);
726 static void
727 ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
729 /* Free the char* */
730 kfree(nd_get_link(nd));
734 * upper_size_to_lower_size
735 * @crypt_stat: Crypt_stat associated with file
736 * @upper_size: Size of the upper file
738 * Calculate the requried size of the lower file based on the
739 * specified size of the upper file. This calculation is based on the
740 * number of headers in the underlying file and the extent size.
742 * Returns Calculated size of the lower file.
744 static loff_t
745 upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
746 loff_t upper_size)
748 loff_t lower_size;
750 lower_size = ( crypt_stat->header_extent_size
751 * crypt_stat->num_header_extents_at_front );
752 if (upper_size != 0) {
753 loff_t num_extents;
755 num_extents = upper_size >> crypt_stat->extent_shift;
756 if (upper_size & ~crypt_stat->extent_mask)
757 num_extents++;
758 lower_size += (num_extents * crypt_stat->extent_size);
760 return lower_size;
764 * ecryptfs_truncate
765 * @dentry: The ecryptfs layer dentry
766 * @new_length: The length to expand the file to
768 * Function to handle truncations modifying the size of the file. Note
769 * that the file sizes are interpolated. When expanding, we are simply
770 * writing strings of 0's out. When truncating, we need to modify the
771 * underlying file size according to the page index interpolations.
773 * Returns zero on success; non-zero otherwise
775 int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
777 int rc = 0;
778 struct inode *inode = dentry->d_inode;
779 struct dentry *lower_dentry;
780 struct vfsmount *lower_mnt;
781 struct file fake_ecryptfs_file, *lower_file = NULL;
782 struct ecryptfs_crypt_stat *crypt_stat;
783 loff_t i_size = i_size_read(inode);
784 loff_t lower_size_before_truncate;
785 loff_t lower_size_after_truncate;
787 if (unlikely((new_length == i_size)))
788 goto out;
789 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
790 /* Set up a fake ecryptfs file, this is used to interface with
791 * the file in the underlying filesystem so that the
792 * truncation has an effect there as well. */
793 memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
794 fake_ecryptfs_file.f_dentry = dentry;
795 /* Released at out_free: label */
796 ecryptfs_set_file_private(&fake_ecryptfs_file,
797 kmem_cache_alloc(ecryptfs_file_info_cache,
798 SLAB_KERNEL));
799 if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
800 rc = -ENOMEM;
801 goto out;
803 lower_dentry = ecryptfs_dentry_to_lower(dentry);
804 /* This dget & mntget is released through fput at out_fput: */
805 lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
806 if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
807 O_RDWR))) {
808 ecryptfs_printk(KERN_ERR,
809 "Error opening dentry; rc = [%i]\n", rc);
810 goto out_free;
812 ecryptfs_set_file_lower(&fake_ecryptfs_file, lower_file);
813 /* Switch on growing or shrinking file */
814 if (new_length > i_size) {
815 rc = ecryptfs_fill_zeros(&fake_ecryptfs_file, new_length);
816 if (rc) {
817 ecryptfs_printk(KERN_ERR,
818 "Problem with fill_zeros\n");
819 goto out_fput;
821 i_size_write(inode, new_length);
822 rc = ecryptfs_write_inode_size_to_header(lower_file,
823 lower_dentry->d_inode,
824 inode);
825 if (rc) {
826 ecryptfs_printk(KERN_ERR,
827 "Problem with ecryptfs_write"
828 "_inode_size\n");
829 goto out_fput;
831 } else { /* new_length < i_size_read(inode) */
832 vmtruncate(inode, new_length);
833 ecryptfs_write_inode_size_to_header(lower_file,
834 lower_dentry->d_inode,
835 inode);
836 /* We are reducing the size of the ecryptfs file, and need to
837 * know if we need to reduce the size of the lower file. */
838 lower_size_before_truncate =
839 upper_size_to_lower_size(crypt_stat, i_size);
840 lower_size_after_truncate =
841 upper_size_to_lower_size(crypt_stat, new_length);
842 if (lower_size_after_truncate < lower_size_before_truncate)
843 vmtruncate(lower_dentry->d_inode,
844 lower_size_after_truncate);
846 /* Update the access times */
847 lower_dentry->d_inode->i_mtime = lower_dentry->d_inode->i_ctime
848 = CURRENT_TIME;
849 mark_inode_dirty_sync(inode);
850 out_fput:
851 if ((rc = ecryptfs_close_lower_file(lower_file)))
852 printk(KERN_ERR "Error closing lower_file\n");
853 out_free:
854 if (ecryptfs_file_to_private(&fake_ecryptfs_file))
855 kmem_cache_free(ecryptfs_file_info_cache,
856 ecryptfs_file_to_private(&fake_ecryptfs_file));
857 out:
858 return rc;
861 static int
862 ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
864 int rc;
866 if (nd) {
867 struct vfsmount *vfsmnt_save = nd->mnt;
868 struct dentry *dentry_save = nd->dentry;
870 nd->mnt = ecryptfs_dentry_to_lower_mnt(nd->dentry);
871 nd->dentry = ecryptfs_dentry_to_lower(nd->dentry);
872 rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
873 nd->mnt = vfsmnt_save;
874 nd->dentry = dentry_save;
875 } else
876 rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
877 return rc;
881 * ecryptfs_setattr
882 * @dentry: dentry handle to the inode to modify
883 * @ia: Structure with flags of what to change and values
885 * Updates the metadata of an inode. If the update is to the size
886 * i.e. truncation, then ecryptfs_truncate will handle the size modification
887 * of both the ecryptfs inode and the lower inode.
889 * All other metadata changes will be passed right to the lower filesystem,
890 * and we will just update our inode to look like the lower.
892 static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
894 int rc = 0;
895 struct dentry *lower_dentry;
896 struct inode *inode;
897 struct inode *lower_inode;
898 struct ecryptfs_crypt_stat *crypt_stat;
900 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
901 lower_dentry = ecryptfs_dentry_to_lower(dentry);
902 inode = dentry->d_inode;
903 lower_inode = ecryptfs_inode_to_lower(inode);
904 if (ia->ia_valid & ATTR_SIZE) {
905 ecryptfs_printk(KERN_DEBUG,
906 "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
907 ia->ia_valid, ATTR_SIZE);
908 rc = ecryptfs_truncate(dentry, ia->ia_size);
909 /* ecryptfs_truncate handles resizing of the lower file */
910 ia->ia_valid &= ~ATTR_SIZE;
911 ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
912 ia->ia_valid);
913 if (rc < 0)
914 goto out;
916 rc = notify_change(lower_dentry, ia);
917 out:
918 ecryptfs_copy_attr_all(inode, lower_inode);
919 return rc;
922 static int
923 ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
924 size_t size, int flags)
926 int rc = 0;
927 struct dentry *lower_dentry;
929 lower_dentry = ecryptfs_dentry_to_lower(dentry);
930 if (!lower_dentry->d_inode->i_op->setxattr) {
931 rc = -ENOSYS;
932 goto out;
934 mutex_lock(&lower_dentry->d_inode->i_mutex);
935 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
936 size, flags);
937 mutex_unlock(&lower_dentry->d_inode->i_mutex);
938 out:
939 return rc;
942 static ssize_t
943 ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
944 size_t size)
946 int rc = 0;
947 struct dentry *lower_dentry;
949 lower_dentry = ecryptfs_dentry_to_lower(dentry);
950 if (!lower_dentry->d_inode->i_op->getxattr) {
951 rc = -ENOSYS;
952 goto out;
954 mutex_lock(&lower_dentry->d_inode->i_mutex);
955 rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
956 size);
957 mutex_unlock(&lower_dentry->d_inode->i_mutex);
958 out:
959 return rc;
962 static ssize_t
963 ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
965 int rc = 0;
966 struct dentry *lower_dentry;
968 lower_dentry = ecryptfs_dentry_to_lower(dentry);
969 if (!lower_dentry->d_inode->i_op->listxattr) {
970 rc = -ENOSYS;
971 goto out;
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);
976 out:
977 return rc;
980 static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
982 int rc = 0;
983 struct dentry *lower_dentry;
985 lower_dentry = ecryptfs_dentry_to_lower(dentry);
986 if (!lower_dentry->d_inode->i_op->removexattr) {
987 rc = -ENOSYS;
988 goto out;
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);
993 out:
994 return rc;
997 int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
999 if ((ecryptfs_inode_to_lower(inode)
1000 == (struct inode *)candidate_lower_inode))
1001 return 1;
1002 else
1003 return 0;
1006 int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
1008 ecryptfs_init_inode(inode, (struct inode *)lower_inode);
1009 return 0;
1012 struct inode_operations ecryptfs_symlink_iops = {
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
1024 struct inode_operations ecryptfs_dir_iops = {
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
1042 struct inode_operations ecryptfs_main_iops = {
1043 .permission = ecryptfs_permission,
1044 .setattr = ecryptfs_setattr,
1045 .setxattr = ecryptfs_setxattr,
1046 .getxattr = ecryptfs_getxattr,
1047 .listxattr = ecryptfs_listxattr,
1048 .removexattr = ecryptfs_removexattr