1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * This file contians vfs dentry ops for the 9P2000 protocol.
4 *
5 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
7 */
8
9#include <linux/module.h>
10#include <linux/errno.h>
11#include <linux/fs.h>
12#include <linux/file.h>
13#include <linux/pagemap.h>
14#include <linux/stat.h>
15#include <linux/string.h>
16#include <linux/namei.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <net/9p/9p.h>
20#include <net/9p/client.h>
21
22#include "v9fs.h"
23#include "v9fs_vfs.h"
24#include "fid.h"
25
26/**
27 * v9fs_cached_dentry_delete - called when dentry refcount equals 0
28 * @dentry: dentry in question
29 *
30 */
31static int v9fs_cached_dentry_delete(const struct dentry *dentry)
32{
33 p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p)\n",
34 dentry, dentry);
35
36 /* Don't cache negative dentries */
37 if (d_really_is_negative(dentry))
38 return 1;
39 return 0;
40}
41
42/**
43 * v9fs_dentry_release - called when dentry is going to be freed
44 * @dentry: dentry that is being release
45 *
46 */
47
48static void v9fs_dentry_release(struct dentry *dentry)
49{
50 struct hlist_node *p, *n;
51 struct hlist_head head;
52
53 p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p)\n",
54 dentry, dentry);
55
56 spin_lock(lock: &dentry->d_lock);
57 hlist_move_list(old: (struct hlist_head *)&dentry->d_fsdata, new: &head);
58 spin_unlock(lock: &dentry->d_lock);
59
60 hlist_for_each_safe(p, n, &head)
61 p9_fid_put(hlist_entry(p, struct p9_fid, dlist));
62}
63
64static int __v9fs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
65{
66 struct p9_fid *fid;
67 struct inode *inode;
68 struct v9fs_inode *v9inode;
69 unsigned int cached;
70
71 if (flags & LOOKUP_RCU)
72 return -ECHILD;
73
74 inode = d_inode(dentry);
75 if (!inode)
76 goto out_valid;
77
78 v9inode = V9FS_I(inode);
79 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
80
81 cached = v9ses->cache & (CACHE_META | CACHE_LOOSE);
82
83 if (!cached || v9inode->cache_validity & V9FS_INO_INVALID_ATTR) {
84 int retval;
85 struct v9fs_session_info *v9ses;
86
87 fid = v9fs_fid_lookup(dentry);
88 if (IS_ERR(ptr: fid)) {
89 p9_debug(
90 P9_DEBUG_VFS,
91 "v9fs_fid_lookup: dentry = %pd (%p), got error %pe\n",
92 dentry, dentry, fid);
93 return PTR_ERR(ptr: fid);
94 }
95
96 v9ses = v9fs_inode2v9ses(inode);
97 if (v9fs_proto_dotl(v9ses))
98 retval = v9fs_refresh_inode_dotl(fid, inode);
99 else
100 retval = v9fs_refresh_inode(fid, inode);
101 p9_fid_put(fid);
102
103 if (retval == -ENOENT) {
104 p9_debug(P9_DEBUG_VFS, "dentry: %pd (%p) invalidated due to ENOENT\n",
105 dentry, dentry);
106 return 0;
107 }
108 if (v9inode->cache_validity & V9FS_INO_INVALID_ATTR) {
109 p9_debug(P9_DEBUG_VFS, "dentry: %pd (%p) invalidated due to type change\n",
110 dentry, dentry);
111 return 0;
112 }
113 if (retval < 0) {
114 p9_debug(P9_DEBUG_VFS,
115 "refresh inode: dentry = %pd (%p), got error %pe\n",
116 dentry, dentry, ERR_PTR(retval));
117 return retval;
118 }
119 }
120out_valid:
121 p9_debug(P9_DEBUG_VFS, "dentry: %pd (%p) is valid\n", dentry, dentry);
122 return 1;
123}
124
125static int v9fs_lookup_revalidate(struct inode *dir, const struct qstr *name,
126 struct dentry *dentry, unsigned int flags)
127{
128 return __v9fs_lookup_revalidate(dentry, flags);
129}
130
131static bool v9fs_dentry_unalias_trylock(const struct dentry *dentry)
132{
133 struct v9fs_session_info *v9ses = v9fs_dentry2v9ses(dentry);
134 return down_write_trylock(sem: &v9ses->rename_sem);
135}
136
137static void v9fs_dentry_unalias_unlock(const struct dentry *dentry)
138{
139 struct v9fs_session_info *v9ses = v9fs_dentry2v9ses(dentry);
140 up_write(sem: &v9ses->rename_sem);
141}
142
143const struct dentry_operations v9fs_cached_dentry_operations = {
144 .d_revalidate = v9fs_lookup_revalidate,
145 .d_weak_revalidate = __v9fs_lookup_revalidate,
146 .d_delete = v9fs_cached_dentry_delete,
147 .d_release = v9fs_dentry_release,
148 .d_unalias_trylock = v9fs_dentry_unalias_trylock,
149 .d_unalias_unlock = v9fs_dentry_unalias_unlock,
150};
151
152const struct dentry_operations v9fs_dentry_operations = {
153 .d_revalidate = v9fs_lookup_revalidate,
154 .d_weak_revalidate = __v9fs_lookup_revalidate,
155 .d_release = v9fs_dentry_release,
156 .d_unalias_trylock = v9fs_dentry_unalias_trylock,
157 .d_unalias_unlock = v9fs_dentry_unalias_unlock,
158};
159