]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/ecryptfs/inode.c
[PATCH] eCryptfs: Fix handling of lower d_count
[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
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>
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>
33#include "ecryptfs_kernel.h"
34
35static struct dentry *lock_parent(struct dentry *dentry)
36{
37 struct dentry *dir;
38
39 dir = dget(dentry->d_parent);
40 mutex_lock(&(dir->d_inode->i_mutex));
41 return dir;
42}
43
44static void unlock_parent(struct dentry *dentry)
45{
46 mutex_unlock(&(dentry->d_parent->d_inode->i_mutex));
47 dput(dentry->d_parent);
48}
49
50static void unlock_dir(struct dentry *dir)
51{
52 mutex_unlock(&dir->d_inode->i_mutex);
53 dput(dir);
54}
55
56void ecryptfs_copy_inode_size(struct inode *dst, const struct inode *src)
57{
58 i_size_write(dst, i_size_read((struct inode *)src));
59 dst->i_blocks = src->i_blocks;
60}
61
62void ecryptfs_copy_attr_atime(struct inode *dest, const struct inode *src)
63{
64 dest->i_atime = src->i_atime;
65}
66
67static void ecryptfs_copy_attr_times(struct inode *dest,
68 const struct inode *src)
69{
70 dest->i_atime = src->i_atime;
71 dest->i_mtime = src->i_mtime;
72 dest->i_ctime = src->i_ctime;
73}
74
75static void ecryptfs_copy_attr_timesizes(struct inode *dest,
76 const struct inode *src)
77{
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);
82}
83
84void ecryptfs_copy_attr_all(struct inode *dest, const struct inode *src)
85{
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;
96}
97
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
105 *
106 * Creates the file in the lower file system.
107 *
108 * Returns zero on success; non-zero on error condition
109 */
110static int
111ecryptfs_create_underlying_file(struct inode *lower_dir_inode,
112 struct dentry *dentry, int mode,
113 struct nameidata *nd)
114{
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;
120
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;
129}
130
131/**
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
137 *
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.
141 *
142 * Returns zero on success; non-zero on error condition
143 */
144static int
145ecryptfs_do_create(struct inode *directory_inode,
146 struct dentry *ecryptfs_dentry, int mode,
147 struct nameidata *nd)
148{
149 int rc;
150 struct dentry *lower_dentry;
151 struct dentry *lower_dir_dentry;
152
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;
160 }
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;
167 }
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;
173 }
174 ecryptfs_copy_attr_timesizes(directory_inode,
175 lower_dir_dentry->d_inode);
176out_lock:
177 unlock_dir(lower_dir_dentry);
178out:
179 return rc;
180}
181
182/**
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
188 *
189 * This is the code which will grow the file to its correct size.
190 */
191static int grow_file(struct dentry *ecryptfs_dentry, struct file *lower_file,
192 struct inode *inode, struct inode *lower_inode)
193{
194 int rc = 0;
195 struct file fake_file;
196 struct ecryptfs_file_info tmp_file_info;
197
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;
211 }
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);
216out:
217 return rc;
218}
219
220/**
221 * ecryptfs_initialize_file
222 *
223 * Cause the file to be changed from a basic empty file to an ecryptfs
224 * file with a header and first data page.
225 *
226 * Returns zero on success
227 */
228static int ecryptfs_initialize_file(struct dentry *ecryptfs_dentry)
229{
230 int rc = 0;
231 int lower_flags;
232 struct ecryptfs_crypt_stat *crypt_stat;
233 struct dentry *lower_dentry;
237fead6
MH
234 struct file *lower_file;
235 struct inode *inode, *lower_inode;
236 struct vfsmount *lower_mnt;
237
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;
237fead6
MH
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);
237fead6 248 /* Corresponding fput() at end of this function */
7ff1d74f
MH
249 if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
250 lower_flags))) {
237fead6
MH
251 ecryptfs_printk(KERN_ERR,
252 "Error opening dentry; rc = [%i]\n", rc);
253 goto out;
254 }
7ff1d74f 255 lower_inode = lower_dentry->d_inode;
237fead6
MH
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;
260 }
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;
268 }
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;
273 }
274 rc = grow_file(ecryptfs_dentry, lower_file, inode, lower_inode);
275out_fput:
7ff1d74f
MH
276 if ((rc = ecryptfs_close_lower_file(lower_file)))
277 printk(KERN_ERR "Error closing lower_file\n");
237fead6
MH
278out:
279 return rc;
280}
281
282/**
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
288 *
289 * Creates a new file.
290 *
291 * Returns zero on success; non-zero on error condition
292 */
293static int
294ecryptfs_create(struct inode *directory_inode, struct dentry *ecryptfs_dentry,
295 int mode, struct nameidata *nd)
296{
297 int rc;
298
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;
304 }
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);
308out:
309 return rc;
310}
311
312/**
313 * ecryptfs_lookup
314 * @dir: inode
315 * @dentry: The dentry
316 * @nd: nameidata, may be NULL
317 *
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.
320 */
321static struct dentry *ecryptfs_lookup(struct inode *dir, struct dentry *dentry,
322 struct nameidata *nd)
323{
324 int rc = 0;
325 struct dentry *lower_dir_dentry;
326 struct dentry *lower_dentry;
327 struct vfsmount *lower_mnt;
237fead6
MH
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;
334
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, "."))
45ec4aba
MH
338 || (dentry->d_name.len == 2
339 && !strcmp(dentry->d_name.name, ".."))) {
340 d_drop(dentry);
341 goto out;
342 }
237fead6
MH
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;
45ec4aba
MH
349 d_drop(dentry);
350 goto out;
237fead6
MH
351 }
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);
237fead6
MH
357 if (IS_ERR(lower_dentry)) {
358 ecryptfs_printk(KERN_ERR, "ERR from lower_dentry\n");
359 rc = PTR_ERR(lower_dentry);
45ec4aba
MH
360 d_drop(dentry);
361 goto out;
237fead6 362 }
45ec4aba 363 lower_mnt = mntget(ecryptfs_dentry_to_lower_mnt(dentry->d_parent));
237fead6
MH
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;
378 }
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;
385 }
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;
390 }
391 if (S_ISDIR(lower_inode->i_mode)) {
392 ecryptfs_printk(KERN_DEBUG, "Is a directory; returning\n");
393 goto out;
394 }
395 if (S_ISLNK(lower_inode->i_mode)) {
396 ecryptfs_printk(KERN_DEBUG, "Is a symlink; returning\n");
397 goto out;
398 }
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;
403 }
237fead6
MH
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;
413 }
414 memset(page_virt, 0, PAGE_CACHE_SIZE);
45ec4aba 415 rc = ecryptfs_read_header_region(page_virt, lower_dentry, nd->mnt);
237fead6
MH
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;
428 }
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);
432 }
433 kmem_cache_free(ecryptfs_header_cache_2, page_virt);
434 goto out;
435
436out_dput:
437 dput(lower_dentry);
237fead6
MH
438 d_drop(dentry);
439out:
440 return ERR_PTR(rc);
441}
442
443static int ecryptfs_link(struct dentry *old_dentry, struct inode *dir,
444 struct dentry *new_dentry)
445{
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;
451
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);
469out_lock:
470 unlock_dir(lower_dir_dentry);
471 dput(lower_new_dentry);
472 dput(lower_old_dentry);
45ec4aba
MH
473 d_drop(new_dentry);
474 d_drop(old_dentry);
237fead6
MH
475 return rc;
476}
477
478static int ecryptfs_unlink(struct inode *dir, struct dentry *dentry)
479{
480 int rc = 0;
481 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
482 struct inode *lower_dir_inode = ecryptfs_inode_to_lower(dir);
483
484 lock_parent(lower_dentry);
485 rc = vfs_unlink(lower_dir_inode, lower_dentry);
486 if (rc) {
487 ecryptfs_printk(KERN_ERR, "Error in vfs_unlink\n");
488 goto out_unlock;
489 }
490 ecryptfs_copy_attr_times(dir, lower_dir_inode);
491 dentry->d_inode->i_nlink =
492 ecryptfs_inode_to_lower(dentry->d_inode)->i_nlink;
493 dentry->d_inode->i_ctime = dir->i_ctime;
494out_unlock:
495 unlock_parent(lower_dentry);
496 return rc;
497}
498
499static int ecryptfs_symlink(struct inode *dir, struct dentry *dentry,
500 const char *symname)
501{
502 int rc;
503 struct dentry *lower_dentry;
504 struct dentry *lower_dir_dentry;
505 umode_t mode;
506 char *encoded_symname;
507 unsigned int encoded_symlen;
508 struct ecryptfs_crypt_stat *crypt_stat = NULL;
509
510 lower_dentry = ecryptfs_dentry_to_lower(dentry);
511 dget(lower_dentry);
512 lower_dir_dentry = lock_parent(lower_dentry);
513 mode = S_IALLUGO;
514 encoded_symlen = ecryptfs_encode_filename(crypt_stat, symname,
515 strlen(symname),
516 &encoded_symname);
517 if (encoded_symlen < 0) {
518 rc = encoded_symlen;
519 goto out_lock;
520 }
521 rc = vfs_symlink(lower_dir_dentry->d_inode, lower_dentry,
522 encoded_symname, mode);
523 kfree(encoded_symname);
524 if (rc || !lower_dentry->d_inode)
525 goto out_lock;
526 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
527 if (rc)
528 goto out_lock;
529 ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
530out_lock:
531 unlock_dir(lower_dir_dentry);
532 dput(lower_dentry);
533 if (!dentry->d_inode)
534 d_drop(dentry);
535 return rc;
536}
537
538static int ecryptfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
539{
540 int rc;
541 struct dentry *lower_dentry;
542 struct dentry *lower_dir_dentry;
543
544 lower_dentry = ecryptfs_dentry_to_lower(dentry);
545 lower_dir_dentry = lock_parent(lower_dentry);
546 rc = vfs_mkdir(lower_dir_dentry->d_inode, lower_dentry, mode);
547 if (rc || !lower_dentry->d_inode)
548 goto out;
549 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
550 if (rc)
551 goto out;
552 ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
553 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
554out:
555 unlock_dir(lower_dir_dentry);
556 if (!dentry->d_inode)
557 d_drop(dentry);
558 return rc;
559}
560
561static int ecryptfs_rmdir(struct inode *dir, struct dentry *dentry)
562{
237fead6 563 struct dentry *lower_dentry;
237fead6 564 struct dentry *lower_dir_dentry;
45ec4aba 565 int rc;
237fead6
MH
566
567 lower_dentry = ecryptfs_dentry_to_lower(dentry);
45ec4aba 568 dget(dentry);
237fead6 569 lower_dir_dentry = lock_parent(lower_dentry);
45ec4aba 570 dget(lower_dentry);
237fead6 571 rc = vfs_rmdir(lower_dir_dentry->d_inode, lower_dentry);
45ec4aba
MH
572 dput(lower_dentry);
573 if (!rc)
574 d_delete(lower_dentry);
237fead6
MH
575 ecryptfs_copy_attr_times(dir, lower_dir_dentry->d_inode);
576 dir->i_nlink = lower_dir_dentry->d_inode->i_nlink;
577 unlock_dir(lower_dir_dentry);
578 if (!rc)
579 d_drop(dentry);
45ec4aba 580 dput(dentry);
237fead6
MH
581 return rc;
582}
583
584static int
585ecryptfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
586{
587 int rc;
588 struct dentry *lower_dentry;
589 struct dentry *lower_dir_dentry;
590
591 lower_dentry = ecryptfs_dentry_to_lower(dentry);
592 lower_dir_dentry = lock_parent(lower_dentry);
593 rc = vfs_mknod(lower_dir_dentry->d_inode, lower_dentry, mode, dev);
594 if (rc || !lower_dentry->d_inode)
595 goto out;
596 rc = ecryptfs_interpose(lower_dentry, dentry, dir->i_sb, 0);
597 if (rc)
598 goto out;
599 ecryptfs_copy_attr_timesizes(dir, lower_dir_dentry->d_inode);
600out:
601 unlock_dir(lower_dir_dentry);
602 if (!dentry->d_inode)
603 d_drop(dentry);
604 return rc;
605}
606
607static int
608ecryptfs_rename(struct inode *old_dir, struct dentry *old_dentry,
609 struct inode *new_dir, struct dentry *new_dentry)
610{
611 int rc;
612 struct dentry *lower_old_dentry;
613 struct dentry *lower_new_dentry;
614 struct dentry *lower_old_dir_dentry;
615 struct dentry *lower_new_dir_dentry;
616
617 lower_old_dentry = ecryptfs_dentry_to_lower(old_dentry);
618 lower_new_dentry = ecryptfs_dentry_to_lower(new_dentry);
619 dget(lower_old_dentry);
620 dget(lower_new_dentry);
621 lower_old_dir_dentry = dget_parent(lower_old_dentry);
622 lower_new_dir_dentry = dget_parent(lower_new_dentry);
623 lock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
624 rc = vfs_rename(lower_old_dir_dentry->d_inode, lower_old_dentry,
625 lower_new_dir_dentry->d_inode, lower_new_dentry);
626 if (rc)
627 goto out_lock;
628 ecryptfs_copy_attr_all(new_dir, lower_new_dir_dentry->d_inode);
629 if (new_dir != old_dir)
630 ecryptfs_copy_attr_all(old_dir, lower_old_dir_dentry->d_inode);
631out_lock:
632 unlock_rename(lower_old_dir_dentry, lower_new_dir_dentry);
633 dput(lower_new_dentry);
634 dput(lower_old_dentry);
635 return rc;
636}
637
638static int
639ecryptfs_readlink(struct dentry *dentry, char __user * buf, int bufsiz)
640{
641 int rc;
642 struct dentry *lower_dentry;
643 char *decoded_name;
644 char *lower_buf;
645 mm_segment_t old_fs;
646 struct ecryptfs_crypt_stat *crypt_stat;
647
648 lower_dentry = ecryptfs_dentry_to_lower(dentry);
649 if (!lower_dentry->d_inode->i_op ||
650 !lower_dentry->d_inode->i_op->readlink) {
651 rc = -EINVAL;
652 goto out;
653 }
654 /* Released in this function */
655 lower_buf = kmalloc(bufsiz, GFP_KERNEL);
656 if (lower_buf == NULL) {
657 ecryptfs_printk(KERN_ERR, "Out of memory\n");
658 rc = -ENOMEM;
659 goto out;
660 }
661 old_fs = get_fs();
662 set_fs(get_ds());
663 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
664 "lower_dentry->d_name.name = [%s]\n",
665 lower_dentry->d_name.name);
666 rc = lower_dentry->d_inode->i_op->readlink(lower_dentry,
667 (char __user *)lower_buf,
668 bufsiz);
669 set_fs(old_fs);
670 if (rc >= 0) {
671 crypt_stat = NULL;
672 rc = ecryptfs_decode_filename(crypt_stat, lower_buf, rc,
673 &decoded_name);
674 if (rc == -ENOMEM)
675 goto out_free_lower_buf;
676 if (rc > 0) {
677 ecryptfs_printk(KERN_DEBUG, "Copying [%d] bytes "
678 "to userspace: [%*s]\n", rc,
679 decoded_name);
680 if (copy_to_user(buf, decoded_name, rc))
681 rc = -EFAULT;
682 }
683 kfree(decoded_name);
684 ecryptfs_copy_attr_atime(dentry->d_inode,
685 lower_dentry->d_inode);
686 }
687out_free_lower_buf:
688 kfree(lower_buf);
689out:
690 return rc;
691}
692
693static void *ecryptfs_follow_link(struct dentry *dentry, struct nameidata *nd)
694{
695 char *buf;
696 int len = PAGE_SIZE, rc;
697 mm_segment_t old_fs;
698
699 /* Released in ecryptfs_put_link(); only release here on error */
700 buf = kmalloc(len, GFP_KERNEL);
701 if (!buf) {
702 rc = -ENOMEM;
703 goto out;
704 }
705 old_fs = get_fs();
706 set_fs(get_ds());
707 ecryptfs_printk(KERN_DEBUG, "Calling readlink w/ "
708 "dentry->d_name.name = [%s]\n", dentry->d_name.name);
709 rc = dentry->d_inode->i_op->readlink(dentry, (char __user *)buf, len);
710 buf[rc] = '\0';
711 set_fs(old_fs);
712 if (rc < 0)
713 goto out_free;
714 rc = 0;
715 nd_set_link(nd, buf);
716 goto out;
717out_free:
718 kfree(buf);
719out:
720 return ERR_PTR(rc);
721}
722
723static void
724ecryptfs_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr)
725{
726 /* Free the char* */
727 kfree(nd_get_link(nd));
728}
729
730/**
731 * upper_size_to_lower_size
732 * @crypt_stat: Crypt_stat associated with file
733 * @upper_size: Size of the upper file
734 *
735 * Calculate the requried size of the lower file based on the
736 * specified size of the upper file. This calculation is based on the
737 * number of headers in the underlying file and the extent size.
738 *
739 * Returns Calculated size of the lower file.
740 */
741static loff_t
742upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
743 loff_t upper_size)
744{
745 loff_t lower_size;
746
747 lower_size = ( crypt_stat->header_extent_size
748 * crypt_stat->num_header_extents_at_front );
749 if (upper_size != 0) {
750 loff_t num_extents;
751
752 num_extents = upper_size >> crypt_stat->extent_shift;
753 if (upper_size & ~crypt_stat->extent_mask)
754 num_extents++;
755 lower_size += (num_extents * crypt_stat->extent_size);
756 }
757 return lower_size;
758}
759
760/**
761 * ecryptfs_truncate
762 * @dentry: The ecryptfs layer dentry
763 * @new_length: The length to expand the file to
764 *
765 * Function to handle truncations modifying the size of the file. Note
766 * that the file sizes are interpolated. When expanding, we are simply
767 * writing strings of 0's out. When truncating, we need to modify the
768 * underlying file size according to the page index interpolations.
769 *
770 * Returns zero on success; non-zero otherwise
771 */
772int ecryptfs_truncate(struct dentry *dentry, loff_t new_length)
773{
774 int rc = 0;
775 struct inode *inode = dentry->d_inode;
776 struct dentry *lower_dentry;
777 struct vfsmount *lower_mnt;
778 struct file fake_ecryptfs_file, *lower_file = NULL;
779 struct ecryptfs_crypt_stat *crypt_stat;
780 loff_t i_size = i_size_read(inode);
781 loff_t lower_size_before_truncate;
782 loff_t lower_size_after_truncate;
783
784 if (unlikely((new_length == i_size)))
785 goto out;
786 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
787 /* Set up a fake ecryptfs file, this is used to interface with
788 * the file in the underlying filesystem so that the
789 * truncation has an effect there as well. */
790 memset(&fake_ecryptfs_file, 0, sizeof(fake_ecryptfs_file));
791 fake_ecryptfs_file.f_dentry = dentry;
792 /* Released at out_free: label */
793 ecryptfs_set_file_private(&fake_ecryptfs_file,
794 kmem_cache_alloc(ecryptfs_file_info_cache,
795 SLAB_KERNEL));
796 if (unlikely(!ecryptfs_file_to_private(&fake_ecryptfs_file))) {
797 rc = -ENOMEM;
798 goto out;
799 }
800 lower_dentry = ecryptfs_dentry_to_lower(dentry);
801 /* This dget & mntget is released through fput at out_fput: */
237fead6 802 lower_mnt = ecryptfs_dentry_to_lower_mnt(dentry);
7ff1d74f
MH
803 if ((rc = ecryptfs_open_lower_file(&lower_file, lower_dentry, lower_mnt,
804 O_RDWR))) {
805 ecryptfs_printk(KERN_ERR,
806 "Error opening dentry; rc = [%i]\n", rc);
237fead6
MH
807 goto out_free;
808 }
809 ecryptfs_set_file_lower(&fake_ecryptfs_file, lower_file);
810 /* Switch on growing or shrinking file */
811 if (new_length > i_size) {
812 rc = ecryptfs_fill_zeros(&fake_ecryptfs_file, new_length);
813 if (rc) {
814 ecryptfs_printk(KERN_ERR,
815 "Problem with fill_zeros\n");
816 goto out_fput;
817 }
818 i_size_write(inode, new_length);
819 rc = ecryptfs_write_inode_size_to_header(lower_file,
820 lower_dentry->d_inode,
821 inode);
822 if (rc) {
823 ecryptfs_printk(KERN_ERR,
824 "Problem with ecryptfs_write"
825 "_inode_size\n");
826 goto out_fput;
827 }
828 } else { /* new_length < i_size_read(inode) */
829 vmtruncate(inode, new_length);
830 ecryptfs_write_inode_size_to_header(lower_file,
831 lower_dentry->d_inode,
832 inode);
833 /* We are reducing the size of the ecryptfs file, and need to
834 * know if we need to reduce the size of the lower file. */
835 lower_size_before_truncate =
836 upper_size_to_lower_size(crypt_stat, i_size);
837 lower_size_after_truncate =
838 upper_size_to_lower_size(crypt_stat, new_length);
839 if (lower_size_after_truncate < lower_size_before_truncate)
840 vmtruncate(lower_dentry->d_inode,
841 lower_size_after_truncate);
842 }
843 /* Update the access times */
844 lower_dentry->d_inode->i_mtime = lower_dentry->d_inode->i_ctime
845 = CURRENT_TIME;
846 mark_inode_dirty_sync(inode);
847out_fput:
7ff1d74f
MH
848 if ((rc = ecryptfs_close_lower_file(lower_file)))
849 printk(KERN_ERR "Error closing lower_file\n");
237fead6
MH
850out_free:
851 if (ecryptfs_file_to_private(&fake_ecryptfs_file))
852 kmem_cache_free(ecryptfs_file_info_cache,
853 ecryptfs_file_to_private(&fake_ecryptfs_file));
854out:
855 return rc;
856}
857
858static int
859ecryptfs_permission(struct inode *inode, int mask, struct nameidata *nd)
860{
861 int rc;
862
863 if (nd) {
864 struct vfsmount *vfsmnt_save = nd->mnt;
865 struct dentry *dentry_save = nd->dentry;
866
867 nd->mnt = ecryptfs_dentry_to_lower_mnt(nd->dentry);
868 nd->dentry = ecryptfs_dentry_to_lower(nd->dentry);
869 rc = permission(ecryptfs_inode_to_lower(inode), mask, nd);
870 nd->mnt = vfsmnt_save;
871 nd->dentry = dentry_save;
872 } else
873 rc = permission(ecryptfs_inode_to_lower(inode), mask, NULL);
874 return rc;
875}
876
877/**
878 * ecryptfs_setattr
879 * @dentry: dentry handle to the inode to modify
880 * @ia: Structure with flags of what to change and values
881 *
882 * Updates the metadata of an inode. If the update is to the size
883 * i.e. truncation, then ecryptfs_truncate will handle the size modification
884 * of both the ecryptfs inode and the lower inode.
885 *
886 * All other metadata changes will be passed right to the lower filesystem,
887 * and we will just update our inode to look like the lower.
888 */
889static int ecryptfs_setattr(struct dentry *dentry, struct iattr *ia)
890{
891 int rc = 0;
892 struct dentry *lower_dentry;
893 struct inode *inode;
894 struct inode *lower_inode;
895 struct ecryptfs_crypt_stat *crypt_stat;
896
897 crypt_stat = &ecryptfs_inode_to_private(dentry->d_inode)->crypt_stat;
898 lower_dentry = ecryptfs_dentry_to_lower(dentry);
899 inode = dentry->d_inode;
900 lower_inode = ecryptfs_inode_to_lower(inode);
901 if (ia->ia_valid & ATTR_SIZE) {
902 ecryptfs_printk(KERN_DEBUG,
903 "ia->ia_valid = [0x%x] ATTR_SIZE" " = [0x%x]\n",
904 ia->ia_valid, ATTR_SIZE);
905 rc = ecryptfs_truncate(dentry, ia->ia_size);
906 /* ecryptfs_truncate handles resizing of the lower file */
907 ia->ia_valid &= ~ATTR_SIZE;
908 ecryptfs_printk(KERN_DEBUG, "ia->ia_valid = [%x]\n",
909 ia->ia_valid);
910 if (rc < 0)
911 goto out;
912 }
913 rc = notify_change(lower_dentry, ia);
914out:
915 ecryptfs_copy_attr_all(inode, lower_inode);
916 return rc;
917}
918
919static int
920ecryptfs_setxattr(struct dentry *dentry, const char *name, const void *value,
921 size_t size, int flags)
922{
923 int rc = 0;
924 struct dentry *lower_dentry;
925
926 lower_dentry = ecryptfs_dentry_to_lower(dentry);
927 if (!lower_dentry->d_inode->i_op->setxattr) {
928 rc = -ENOSYS;
929 goto out;
930 }
931 mutex_lock(&lower_dentry->d_inode->i_mutex);
932 rc = lower_dentry->d_inode->i_op->setxattr(lower_dentry, name, value,
933 size, flags);
934 mutex_unlock(&lower_dentry->d_inode->i_mutex);
935out:
936 return rc;
937}
938
939static ssize_t
940ecryptfs_getxattr(struct dentry *dentry, const char *name, void *value,
941 size_t size)
942{
943 int rc = 0;
944 struct dentry *lower_dentry;
945
946 lower_dentry = ecryptfs_dentry_to_lower(dentry);
947 if (!lower_dentry->d_inode->i_op->getxattr) {
948 rc = -ENOSYS;
949 goto out;
950 }
951 mutex_lock(&lower_dentry->d_inode->i_mutex);
952 rc = lower_dentry->d_inode->i_op->getxattr(lower_dentry, name, value,
953 size);
954 mutex_unlock(&lower_dentry->d_inode->i_mutex);
955out:
956 return rc;
957}
958
959static ssize_t
960ecryptfs_listxattr(struct dentry *dentry, char *list, size_t size)
961{
962 int rc = 0;
963 struct dentry *lower_dentry;
964
965 lower_dentry = ecryptfs_dentry_to_lower(dentry);
966 if (!lower_dentry->d_inode->i_op->listxattr) {
967 rc = -ENOSYS;
968 goto out;
969 }
970 mutex_lock(&lower_dentry->d_inode->i_mutex);
971 rc = lower_dentry->d_inode->i_op->listxattr(lower_dentry, list, size);
972 mutex_unlock(&lower_dentry->d_inode->i_mutex);
973out:
974 return rc;
975}
976
977static int ecryptfs_removexattr(struct dentry *dentry, const char *name)
978{
979 int rc = 0;
980 struct dentry *lower_dentry;
981
982 lower_dentry = ecryptfs_dentry_to_lower(dentry);
983 if (!lower_dentry->d_inode->i_op->removexattr) {
984 rc = -ENOSYS;
985 goto out;
986 }
987 mutex_lock(&lower_dentry->d_inode->i_mutex);
988 rc = lower_dentry->d_inode->i_op->removexattr(lower_dentry, name);
989 mutex_unlock(&lower_dentry->d_inode->i_mutex);
990out:
991 return rc;
992}
993
994int ecryptfs_inode_test(struct inode *inode, void *candidate_lower_inode)
995{
996 if ((ecryptfs_inode_to_lower(inode)
997 == (struct inode *)candidate_lower_inode))
998 return 1;
999 else
1000 return 0;
1001}
1002
1003int ecryptfs_inode_set(struct inode *inode, void *lower_inode)
1004{
1005 ecryptfs_init_inode(inode, (struct inode *)lower_inode);
1006 return 0;
1007}
1008
1009struct inode_operations ecryptfs_symlink_iops = {
1010 .readlink = ecryptfs_readlink,
1011 .follow_link = ecryptfs_follow_link,
1012 .put_link = ecryptfs_put_link,
1013 .permission = ecryptfs_permission,
1014 .setattr = ecryptfs_setattr,
1015 .setxattr = ecryptfs_setxattr,
1016 .getxattr = ecryptfs_getxattr,
1017 .listxattr = ecryptfs_listxattr,
1018 .removexattr = ecryptfs_removexattr
1019};
1020
1021struct inode_operations ecryptfs_dir_iops = {
1022 .create = ecryptfs_create,
1023 .lookup = ecryptfs_lookup,
1024 .link = ecryptfs_link,
1025 .unlink = ecryptfs_unlink,
1026 .symlink = ecryptfs_symlink,
1027 .mkdir = ecryptfs_mkdir,
1028 .rmdir = ecryptfs_rmdir,
1029 .mknod = ecryptfs_mknod,
1030 .rename = ecryptfs_rename,
1031 .permission = ecryptfs_permission,
1032 .setattr = ecryptfs_setattr,
1033 .setxattr = ecryptfs_setxattr,
1034 .getxattr = ecryptfs_getxattr,
1035 .listxattr = ecryptfs_listxattr,
1036 .removexattr = ecryptfs_removexattr
1037};
1038
1039struct inode_operations ecryptfs_main_iops = {
1040 .permission = ecryptfs_permission,
1041 .setattr = ecryptfs_setattr,
1042 .setxattr = ecryptfs_setxattr,
1043 .getxattr = ecryptfs_getxattr,
1044 .listxattr = ecryptfs_listxattr,
1045 .removexattr = ecryptfs_removexattr
1046};