blob: 8bbe9486e3a6228c40c46971d23c305127e2007b [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);
Jia-Ju Bai4c2b46c2021-03-09 00:00:20 -080029 if (!new_op) {
30 ret = -ENOMEM;
Mike Marshall5db11c22015-07-17 10:38:12 -040031 goto out_put_parent;
Jia-Ju Bai4c2b46c2021-03-09 00:00:20 -080032 }
Mike Marshall5db11c22015-07-17 10:38:12 -040033
Yi Liu8bb8aef2015-11-24 15:12:14 -050034 new_op->upcall.req.lookup.sym_follow = ORANGEFS_LOOKUP_LINK_NO_FOLLOW;
Mike Marshall5db11c22015-07-17 10:38:12 -040035 new_op->upcall.req.lookup.parent_refn = parent->refn;
36 strncpy(new_op->upcall.req.lookup.d_name,
37 dentry->d_name.name,
Xiongfeng Wang6bdfb482018-01-08 20:22:33 +080038 ORANGEFS_NAME_MAX - 1);
Mike Marshall5db11c22015-07-17 10:38:12 -040039
40 gossip_debug(GOSSIP_DCACHE_DEBUG,
41 "%s:%s:%d interrupt flag [%d]\n",
42 __FILE__,
43 __func__,
44 __LINE__,
45 get_interruptible_flag(parent_inode));
46
Yi Liu8bb8aef2015-11-24 15:12:14 -050047 err = service_operation(new_op, "orangefs_lookup",
Mike Marshall5db11c22015-07-17 10:38:12 -040048 get_interruptible_flag(parent_inode));
Mike Marshall5db11c22015-07-17 10:38:12 -040049
Martin Brandenburg99109822016-01-28 10:19:40 -050050 /* Positive dentry: reject if error or not the same inode. */
51 if (inode) {
52 if (err) {
53 gossip_debug(GOSSIP_DCACHE_DEBUG,
54 "%s:%s:%d lookup failure.\n",
55 __FILE__, __func__, __LINE__);
56 goto out_drop;
57 }
58 if (!match_handle(new_op->downcall.resp.lookup.refn.khandle,
59 inode)) {
60 gossip_debug(GOSSIP_DCACHE_DEBUG,
61 "%s:%s:%d no match.\n",
62 __FILE__, __func__, __LINE__);
63 goto out_drop;
64 }
65
66 /* Negative dentry: reject if success or error other than ENOENT. */
67 } else {
68 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s: negative dentry.\n",
69 __func__);
70 if (!err || err != -ENOENT) {
71 if (new_op->downcall.status != 0)
72 gossip_debug(GOSSIP_DCACHE_DEBUG,
73 "%s:%s:%d lookup failure.\n",
74 __FILE__, __func__, __LINE__);
75 goto out_drop;
76 }
Mike Marshall5db11c22015-07-17 10:38:12 -040077 }
78
Miklos Szeredi804b1732016-10-17 10:14:23 +020079 orangefs_set_timeout(dentry);
Mike Marshall5db11c22015-07-17 10:38:12 -040080 ret = 1;
81out_release_op:
82 op_release(new_op);
83out_put_parent:
84 dput(parent_dentry);
85 return ret;
86out_drop:
Martin Brandenburg99109822016-01-28 10:19:40 -050087 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s:%s:%d revalidate failed\n",
88 __FILE__, __func__, __LINE__);
Mike Marshall5db11c22015-07-17 10:38:12 -040089 goto out_release_op;
90}
91
92/*
93 * Verify that dentry is valid.
94 *
Mike Marshallf987f4c2015-12-30 13:04:28 -050095 * Should return 1 if dentry can still be trusted, else 0.
Mike Marshall5db11c22015-07-17 10:38:12 -040096 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050097static int orangefs_d_revalidate(struct dentry *dentry, unsigned int flags)
Mike Marshall5db11c22015-07-17 10:38:12 -040098{
Martin Brandenburg99109822016-01-28 10:19:40 -050099 int ret;
Miklos Szeredi804b1732016-10-17 10:14:23 +0200100 unsigned long time = (unsigned long) dentry->d_fsdata;
Mike Marshall5db11c22015-07-17 10:38:12 -0400101
Miklos Szeredi804b1732016-10-17 10:14:23 +0200102 if (time_before(jiffies, time))
Martin Brandenburg31b7c1a2016-02-08 17:01:29 -0500103 return 1;
104
Mike Marshall5db11c22015-07-17 10:38:12 -0400105 if (flags & LOOKUP_RCU)
106 return -ECHILD;
107
108 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s: called on dentry %p.\n",
109 __func__, dentry);
110
Mike Marshallf987f4c2015-12-30 13:04:28 -0500111 /* skip root handle lookups. */
Martin Brandenburg99109822016-01-28 10:19:40 -0500112 if (dentry->d_inode && is_root_handle(dentry->d_inode))
113 return 1;
114
115 /*
116 * If this passes, the positive dentry still exists or the negative
117 * dentry still does not exist.
118 */
Martin Brandenburgee70fca2016-02-20 13:10:47 -0500119 if (!orangefs_revalidate_lookup(dentry))
Martin Brandenburg99109822016-01-28 10:19:40 -0500120 return 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400121
Martin Brandenburg99109822016-01-28 10:19:40 -0500122 /* We do not need to continue with negative dentries. */
Martin Brandenburg74e938c2018-02-06 14:01:26 +0000123 if (!dentry->d_inode) {
124 gossip_debug(GOSSIP_DCACHE_DEBUG,
125 "%s: negative dentry or positive dentry and inode valid.\n",
126 __func__);
127 return 1;
128 }
Martin Brandenburg99109822016-01-28 10:19:40 -0500129
130 /* Now we must perform a getattr to validate the inode contents. */
Martin Brandenburg933287d2016-01-30 13:46:54 -0500131
Martin Brandenburg5859d772016-03-17 15:15:16 -0400132 ret = orangefs_inode_check_changed(dentry->d_inode);
Martin Brandenburg99109822016-01-28 10:19:40 -0500133 if (ret < 0) {
134 gossip_debug(GOSSIP_DCACHE_DEBUG, "%s:%s:%d getattr failure.\n",
135 __FILE__, __func__, __LINE__);
Martin Brandenburg99109822016-01-28 10:19:40 -0500136 return 0;
137 }
Martin Brandenburg74e938c2018-02-06 14:01:26 +0000138 return !ret;
Mike Marshall5db11c22015-07-17 10:38:12 -0400139}
140
Yi Liu8bb8aef2015-11-24 15:12:14 -0500141const struct dentry_operations orangefs_dentry_operations = {
142 .d_revalidate = orangefs_d_revalidate,
Mike Marshall5db11c22015-07-17 10:38:12 -0400143};