blob: acaa0825e9bbe4645301af1f2508726f4e9a596a [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Lee Jones64cbb652021-03-30 17:44:48 +01002/*
Michael Halcrow237fead2006-10-04 02:16:22 -07003 * 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 Halcrow237fead2006-10-04 02:16:22 -07009 */
10
11#include <linux/dcache.h>
12#include <linux/namei.h>
Michael Halcrow45ec4aba2006-10-30 22:07:20 -080013#include <linux/mount.h>
Josef "Jeff" Sipek0cc72dc2006-12-08 02:36:31 -080014#include <linux/fs_stack.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070016#include "ecryptfs_kernel.h"
17
18/**
19 * ecryptfs_d_revalidate - revalidate an ecryptfs dentry
20 * @dentry: The ecryptfs dentry
Al Viro0b728e12012-06-10 16:03:43 -040021 * @flags: lookup flags
Michael Halcrow237fead2006-10-04 02:16:22 -070022 *
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 Viro0b728e12012-06-10 16:03:43 -040031static int ecryptfs_d_revalidate(struct dentry *dentry, unsigned int flags)
Michael Halcrow237fead2006-10-04 02:16:22 -070032{
Al Viro2edbfbf2013-09-15 20:45:11 -040033 struct dentry *lower_dentry = ecryptfs_dentry_to_lower(dentry);
Tyler Hicks5556e7e2015-08-05 11:26:36 -050034 int rc = 1;
Michael Halcrow237fead2006-10-04 02:16:22 -070035
Al Viro0b728e12012-06-10 16:03:43 -040036 if (flags & LOOKUP_RCU)
Nick Piggin34286d62011-01-07 17:49:57 +110037 return -ECHILD;
38
Tyler Hicks5556e7e2015-08-05 11:26:36 -050039 if (lower_dentry->d_flags & DCACHE_OP_REVALIDATE)
40 rc = lower_dentry->d_op->d_revalidate(lower_dentry, flags);
Michael Halcrowae56fb12006-11-16 01:19:30 -080041
Tyler Hicks5556e7e2015-08-05 11:26:36 -050042 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 Halcrowae56fb12006-11-16 01:19:30 -080048 }
Michael Halcrow237fead2006-10-04 02:16:22 -070049 return rc;
50}
51
52struct kmem_cache *ecryptfs_dentry_info_cache;
53
Al Viro2edbfbf2013-09-15 20:45:11 -040054static 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 Halcrow237fead2006-10-04 02:16:22 -070060/**
61 * ecryptfs_d_release
62 * @dentry: The ecryptfs dentry
63 *
64 * Called when a dentry is really deallocated.
65 */
66static void ecryptfs_d_release(struct dentry *dentry)
67{
Al Viro2edbfbf2013-09-15 20:45:11 -040068 struct ecryptfs_dentry_info *p = dentry->d_fsdata;
69 if (p) {
Al Virocbe9c082013-09-15 20:54:18 -040070 path_put(&p->lower_path);
Al Viro2edbfbf2013-09-15 20:45:11 -040071 call_rcu(&p->rcu, ecryptfs_dentry_free_rcu);
Michael Halcrow45ec4aba2006-10-30 22:07:20 -080072 }
Michael Halcrow237fead2006-10-04 02:16:22 -070073}
74
Al Viro5a3fd052009-02-20 05:57:52 +000075const struct dentry_operations ecryptfs_dops = {
Michael Halcrow237fead2006-10-04 02:16:22 -070076 .d_revalidate = ecryptfs_d_revalidate,
77 .d_release = ecryptfs_d_release,
78};