Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Lee Jones | 64cbb65 | 2021-03-30 17:44:48 +0100 | [diff] [blame] | 2 | /* |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 3 | * eCryptfs: Linux filesystem encryption layer |
| 4 | * |
| 5 | * Copyright (C) 1997-2003 Erez Zadok |
| 6 | * Copyright (C) 2001-2003 Stony Brook University |
| 7 | * Copyright (C) 2004-2006 International Business Machines Corp. |
| 8 | * Author(s): Michael A. Halcrow <mahalcro@us.ibm.com> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/dcache.h> |
| 12 | #include <linux/namei.h> |
Michael Halcrow | 45ec4aba | 2006-10-30 22:07:20 -0800 | [diff] [blame] | 13 | #include <linux/mount.h> |
Josef "Jeff" Sipek | 0cc72dc | 2006-12-08 02:36:31 -0800 | [diff] [blame] | 14 | #include <linux/fs_stack.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 16 | #include "ecryptfs_kernel.h" |
| 17 | |
| 18 | /** |
| 19 | * ecryptfs_d_revalidate - revalidate an ecryptfs dentry |
| 20 | * @dentry: The ecryptfs dentry |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 21 | * @flags: lookup flags |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 22 | * |
| 23 | * Called when the VFS needs to revalidate a dentry. This |
| 24 | * is called whenever a name lookup finds a dentry in the |
| 25 | * dcache. Most filesystems leave this as NULL, because all their |
| 26 | * dentries in the dcache are valid. |
| 27 | * |
| 28 | * Returns 1 if valid, 0 otherwise. |
| 29 | * |
| 30 | */ |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 31 | static int ecryptfs_d_revalidate(struct dentry *dentry, unsigned int flags) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 32 | { |
Al Viro | 2edbfbf | 2013-09-15 20:45:11 -0400 | [diff] [blame] | 33 | struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry); |
Tyler Hicks | 5556e7e | 2015-08-05 11:26:36 -0500 | [diff] [blame] | 34 | int rc = 1; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 35 | |
Al Viro | 0b728e1 | 2012-06-10 16:03:43 -0400 | [diff] [blame] | 36 | if (flags & LOOKUP_RCU) |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 37 | return -ECHILD; |
| 38 | |
Tyler Hicks | 5556e7e | 2015-08-05 11:26:36 -0500 | [diff] [blame] | 39 | if (lower_dentry->d_flags & DCACHE_OP_REVALIDATE) |
| 40 | rc = lower_dentry->d_op->d_revalidate(lower_dentry, flags); |
Michael Halcrow | ae56fb1 | 2006-11-16 01:19:30 -0800 | [diff] [blame] | 41 | |
Tyler Hicks | 5556e7e | 2015-08-05 11:26:36 -0500 | [diff] [blame] | 42 | if (d_really_is_positive(dentry)) { |
| 43 | struct inode *inode = d_inode(dentry); |
| 44 | |
| 45 | fsstack_copy_attr_all(inode, ecryptfs_inode_to_lower(inode)); |
| 46 | if (!inode->i_nlink) |
| 47 | return 0; |
Michael Halcrow | ae56fb1 | 2006-11-16 01:19:30 -0800 | [diff] [blame] | 48 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 49 | return rc; |
| 50 | } |
| 51 | |
| 52 | struct kmem_cache *ecryptfs_dentry_info_cache; |
| 53 | |
Al Viro | 2edbfbf | 2013-09-15 20:45:11 -0400 | [diff] [blame] | 54 | static void ecryptfs_dentry_free_rcu(struct rcu_head *head) |
| 55 | { |
| 56 | kmem_cache_free(ecryptfs_dentry_info_cache, |
| 57 | container_of(head, struct ecryptfs_dentry_info, rcu)); |
| 58 | } |
| 59 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 60 | /** |
| 61 | * ecryptfs_d_release |
| 62 | * @dentry: The ecryptfs dentry |
| 63 | * |
| 64 | * Called when a dentry is really deallocated. |
| 65 | */ |
| 66 | static void ecryptfs_d_release(struct dentry *dentry) |
| 67 | { |
Al Viro | 2edbfbf | 2013-09-15 20:45:11 -0400 | [diff] [blame] | 68 | struct ecryptfs_dentry_info *p = dentry->d_fsdata; |
| 69 | if (p) { |
Al Viro | cbe9c08 | 2013-09-15 20:54:18 -0400 | [diff] [blame] | 70 | path_put(&p->lower_path); |
Al Viro | 2edbfbf | 2013-09-15 20:45:11 -0400 | [diff] [blame] | 71 | call_rcu(&p->rcu, ecryptfs_dentry_free_rcu); |
Michael Halcrow | 45ec4aba | 2006-10-30 22:07:20 -0800 | [diff] [blame] | 72 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Al Viro | 5a3fd05 | 2009-02-20 05:57:52 +0000 | [diff] [blame] | 75 | const struct dentry_operations ecryptfs_dops = { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 76 | .d_revalidate = ecryptfs_d_revalidate, |
| 77 | .d_release = ecryptfs_d_release, |
| 78 | }; |