blob: ae782df5c063e460918b149136dd396ad49d51b1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Mike Marshall5db11c22015-07-17 10:38:12 -04002/*
3 * (C) 2001 Clemson University and The University of Chicago
4 *
5 * See COPYING in top-level directory.
6 */
7
8/*
9 * Implementation of dentry (directory cache) functions.
10 */
11
12#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -050013#include "orangefs-kernel.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040014
15/* Returns 1 if dentry can still be trusted, else 0. */
Yi Liu8bb8aef2015-11-24 15:12:14 -050016static int orangefs_revalidate_lookup(struct dentry *dentry)
Mike Marshall5db11c22015-07-17 10:38:12 -040017{
18 struct dentry *parent_dentry = dget_parent(dentry);
19 struct inode *parent_inode = parent_dentry->d_inode;
Yi Liu8bb8aef2015-11-24 15:12:14 -050020 struct orangefs_inode_s *parent = ORANGEFS_I(parent_inode);
Mike Marshall5db11c22015-07-17 10:38:12 -040021 struct inode *inode = dentry->d_inode;
Yi Liu8bb8aef2015-11-24 15:12:14 -050022 struct orangefs_kernel_op_s *new_op;
Mike Marshall5db11c22015-07-17 10:38:12 -040023 int ret = 0;
24 int err = 0;
25
26 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s: attempting lookup.\n", __func__);
27
Yi Liu8bb8aef2015-11-24 15:12:14 -050028 new_op = op_alloc(ORANGEFS_VFS_OP_LOOKUP);
Mike Marshall5db11c22015-07-17 10:38:12 -040029 if (!new_op)
30 goto out_put_parent;
31
Yi Liu8bb8aef2015-11-24 15:12:14 -050032 new_op->upcall.req.lookup.sym_follow = ORANGEFS_LOOKUP_LINK_NO_FOLLOW;
Mike Marshall5db11c22015-07-17 10:38:12 -040033 new_op->upcall.req.lookup.parent_refn = parent->refn;
34 strncpy(new_op->upcall.req.lookup.d_name,
35 dentry->d_name.name,
Martin Brandenburg47b49482016-02-20 14:22:40 -050036 ORANGEFS_NAME_MAX);
Mike Marshall5db11c22015-07-17 10:38:12 -040037
38 gossip_debug(GOSSIP_DCACHE_DEBUG,
39 "%s:%s:%d interrupt flag [%d]\n",
40 __FILE__,
41 __func__,
42 __LINE__,
43 get_interruptible_flag(parent_inode));
44
Yi Liu8bb8aef2015-11-24 15:12:14 -050045 err = service_operation(new_op, "orangefs_lookup",
Mike Marshall5db11c22015-07-17 10:38:12 -040046 get_interruptible_flag(parent_inode));
Mike Marshall5db11c22015-07-17 10:38:12 -040047
Martin Brandenburg99109822016-01-28 10:19:40 -050048 /* Positive dentry: reject if error or not the same inode. */
49 if (inode) {
50 if (err) {
51 gossip_debug(GOSSIP_DCACHE_DEBUG,
52 "%s:%s:%d lookup failure.\n",
53 __FILE__, __func__, __LINE__);
54 goto out_drop;
55 }
56 if (!match_handle(new_op->downcall.resp.lookup.refn.khandle,
57 inode)) {
58 gossip_debug(GOSSIP_DCACHE_DEBUG,
59 "%s:%s:%d no match.\n",
60 __FILE__, __func__, __LINE__);
61 goto out_drop;
62 }
63
64 /* Negative dentry: reject if success or error other than ENOENT. */
65 } else {
66 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s: negative dentry.\n",
67 __func__);
68 if (!err || err != -ENOENT) {
69 if (new_op->downcall.status != 0)
70 gossip_debug(GOSSIP_DCACHE_DEBUG,
71 "%s:%s:%d lookup failure.\n",
72 __FILE__, __func__, __LINE__);
73 goto out_drop;
74 }
Mike Marshall5db11c22015-07-17 10:38:12 -040075 }
76
Miklos Szeredi804b1732016-10-17 10:14:23 +020077 orangefs_set_timeout(dentry);
Mike Marshall5db11c22015-07-17 10:38:12 -040078 ret = 1;
79out_release_op:
80 op_release(new_op);
81out_put_parent:
82 dput(parent_dentry);
83 return ret;
84out_drop:
Martin Brandenburg99109822016-01-28 10:19:40 -050085 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s:%s:%d revalidate failed\n",
86 __FILE__, __func__, __LINE__);
Mike Marshall5db11c22015-07-17 10:38:12 -040087 goto out_release_op;
88}
89
90/*
91 * Verify that dentry is valid.
92 *
Mike Marshallf987f4c2015-12-30 13:04:28 -050093 * Should return 1 if dentry can still be trusted, else 0.
Mike Marshall5db11c22015-07-17 10:38:12 -040094 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050095static int orangefs_d_revalidate(struct dentry *dentry, unsigned int flags)
Mike Marshall5db11c22015-07-17 10:38:12 -040096{
Martin Brandenburg99109822016-01-28 10:19:40 -050097 int ret;
Miklos Szeredi804b1732016-10-17 10:14:23 +020098 unsigned long time = (unsigned long) dentry->d_fsdata;
Mike Marshall5db11c22015-07-17 10:38:12 -040099
Miklos Szeredi804b1732016-10-17 10:14:23 +0200100 if (time_before(jiffies, time))
Martin Brandenburg31b7c1a2016-02-08 17:01:29 -0500101 return 1;
102
Mike Marshall5db11c22015-07-17 10:38:12 -0400103 if (flags & LOOKUP_RCU)
104 return -ECHILD;
105
106 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s: called on dentry %p.\n",
107 __func__, dentry);
108
Mike Marshallf987f4c2015-12-30 13:04:28 -0500109 /* skip root handle lookups. */
Martin Brandenburg99109822016-01-28 10:19:40 -0500110 if (dentry->d_inode && is_root_handle(dentry->d_inode))
111 return 1;
112
113 /*
114 * If this passes, the positive dentry still exists or the negative
115 * dentry still does not exist.
116 */
Martin Brandenburgee70fca2016-02-20 13:10:47 -0500117 if (!orangefs_revalidate_lookup(dentry))
Martin Brandenburg99109822016-01-28 10:19:40 -0500118 return 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400119
Martin Brandenburg99109822016-01-28 10:19:40 -0500120 /* We do not need to continue with negative dentries. */
121 if (!dentry->d_inode)
122 goto out;
123
124 /* Now we must perform a getattr to validate the inode contents. */
Martin Brandenburg933287d2016-01-30 13:46:54 -0500125
Martin Brandenburg5859d772016-03-17 15:15:16 -0400126 ret = orangefs_inode_check_changed(dentry->d_inode);
Martin Brandenburg99109822016-01-28 10:19:40 -0500127 if (ret < 0) {
128 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s:%s:%d getattr failure.\n",
129 __FILE__, __func__, __LINE__);
Martin Brandenburg99109822016-01-28 10:19:40 -0500130 return 0;
131 }
Martin Brandenburgee70fca2016-02-20 13:10:47 -0500132 if (ret == 0)
Martin Brandenburg99109822016-01-28 10:19:40 -0500133 return 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400134
Mike Marshallf987f4c2015-12-30 13:04:28 -0500135out:
Martin Brandenburg99109822016-01-28 10:19:40 -0500136 gossip_debug(GOSSIP_DCACHE_DEBUG,
137 "%s: negative dentry or positive dentry and inode valid.\n",
138 __func__);
139 return 1;
Mike Marshall5db11c22015-07-17 10:38:12 -0400140}
141
Yi Liu8bb8aef2015-11-24 15:12:14 -0500142const struct dentry_operations orangefs_dentry_operations = {
143 .d_revalidate = orangefs_d_revalidate,
Mike Marshall5db11c22015-07-17 10:38:12 -0400144};