xref: /openbmc/linux/fs/9p/vfs_dentry.c (revision 6636b6dcc3db2258cd0585b8078c1c225c4b6dde)
11f327613SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e69e7fe5SEric Van Hensbergen /*
3e69e7fe5SEric Van Hensbergen  *  linux/fs/9p/vfs_dentry.c
4e69e7fe5SEric Van Hensbergen  *
5e69e7fe5SEric Van Hensbergen  * This file contians vfs dentry ops for the 9P2000 protocol.
6e69e7fe5SEric Van Hensbergen  *
7e69e7fe5SEric Van Hensbergen  *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
8e69e7fe5SEric Van Hensbergen  *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9e69e7fe5SEric Van Hensbergen  */
10e69e7fe5SEric Van Hensbergen 
11e69e7fe5SEric Van Hensbergen #include <linux/module.h>
12e69e7fe5SEric Van Hensbergen #include <linux/errno.h>
13e69e7fe5SEric Van Hensbergen #include <linux/fs.h>
14e69e7fe5SEric Van Hensbergen #include <linux/file.h>
15e69e7fe5SEric Van Hensbergen #include <linux/pagemap.h>
16e69e7fe5SEric Van Hensbergen #include <linux/stat.h>
17e69e7fe5SEric Van Hensbergen #include <linux/string.h>
18e69e7fe5SEric Van Hensbergen #include <linux/inet.h>
19e69e7fe5SEric Van Hensbergen #include <linux/namei.h>
20e69e7fe5SEric Van Hensbergen #include <linux/idr.h>
21e8edc6e0SAlexey Dobriyan #include <linux/sched.h>
225a0e3ad6STejun Heo #include <linux/slab.h>
23bd238fb4SLatchesar Ionkov #include <net/9p/9p.h>
24bd238fb4SLatchesar Ionkov #include <net/9p/client.h>
25e69e7fe5SEric Van Hensbergen 
26e69e7fe5SEric Van Hensbergen #include "v9fs.h"
27e69e7fe5SEric Van Hensbergen #include "v9fs_vfs.h"
28e69e7fe5SEric Van Hensbergen #include "fid.h"
29e69e7fe5SEric Van Hensbergen 
30e69e7fe5SEric Van Hensbergen /**
31e03abc0cSEric Van Hensbergen  * v9fs_cached_dentry_delete - called when dentry refcount equals 0
32e03abc0cSEric Van Hensbergen  * @dentry:  dentry in question
33e03abc0cSEric Van Hensbergen  *
34e03abc0cSEric Van Hensbergen  */
35fe15ce44SNick Piggin static int v9fs_cached_dentry_delete(const struct dentry *dentry)
36e03abc0cSEric Van Hensbergen {
374b8e9923SAl Viro 	p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p)\n",
384b8e9923SAl Viro 		 dentry, dentry);
39e03abc0cSEric Van Hensbergen 
40a950a652SAneesh Kumar K.V 	/* Don't cache negative dentries */
412b0143b5SDavid Howells 	if (d_really_is_negative(dentry))
42e03abc0cSEric Van Hensbergen 		return 1;
43e03abc0cSEric Van Hensbergen 	return 0;
44e03abc0cSEric Van Hensbergen }
45e03abc0cSEric Van Hensbergen 
46e03abc0cSEric Van Hensbergen /**
47e69e7fe5SEric Van Hensbergen  * v9fs_dentry_release - called when dentry is going to be freed
48e69e7fe5SEric Van Hensbergen  * @dentry:  dentry that is being release
49e69e7fe5SEric Van Hensbergen  *
50e69e7fe5SEric Van Hensbergen  */
51e69e7fe5SEric Van Hensbergen 
5298cd3fb0SAl Viro static void v9fs_dentry_release(struct dentry *dentry)
53e69e7fe5SEric Van Hensbergen {
54aaeb7ecfSAl Viro 	struct hlist_node *p, *n;
554b8e9923SAl Viro 	p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p)\n",
564b8e9923SAl Viro 		 dentry, dentry);
57aaeb7ecfSAl Viro 	hlist_for_each_safe(p, n, (struct hlist_head *)&dentry->d_fsdata)
58c4d30967SAl Viro 		p9_client_clunk(hlist_entry(p, struct p9_fid, dlist));
59bd238fb4SLatchesar Ionkov 	dentry->d_fsdata = NULL;
60e69e7fe5SEric Van Hensbergen }
61e69e7fe5SEric Van Hensbergen 
620b728e19SAl Viro static int v9fs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
63b3cbea03SAneesh Kumar K.V {
64b3cbea03SAneesh Kumar K.V 	struct p9_fid *fid;
65b3cbea03SAneesh Kumar K.V 	struct inode *inode;
66b3cbea03SAneesh Kumar K.V 	struct v9fs_inode *v9inode;
67b3cbea03SAneesh Kumar K.V 
680b728e19SAl Viro 	if (flags & LOOKUP_RCU)
69b3cbea03SAneesh Kumar K.V 		return -ECHILD;
70b3cbea03SAneesh Kumar K.V 
712b0143b5SDavid Howells 	inode = d_inode(dentry);
72b3cbea03SAneesh Kumar K.V 	if (!inode)
73b3cbea03SAneesh Kumar K.V 		goto out_valid;
74b3cbea03SAneesh Kumar K.V 
75b3cbea03SAneesh Kumar K.V 	v9inode = V9FS_I(inode);
76b3cbea03SAneesh Kumar K.V 	if (v9inode->cache_validity & V9FS_INO_INVALID_ATTR) {
77b3cbea03SAneesh Kumar K.V 		int retval;
78b3cbea03SAneesh Kumar K.V 		struct v9fs_session_info *v9ses;
79b3cbea03SAneesh Kumar K.V 		fid = v9fs_fid_lookup(dentry);
80b3cbea03SAneesh Kumar K.V 		if (IS_ERR(fid))
81b3cbea03SAneesh Kumar K.V 			return PTR_ERR(fid);
82b3cbea03SAneesh Kumar K.V 
83b3cbea03SAneesh Kumar K.V 		v9ses = v9fs_inode2v9ses(inode);
84b3cbea03SAneesh Kumar K.V 		if (v9fs_proto_dotl(v9ses))
85b3cbea03SAneesh Kumar K.V 			retval = v9fs_refresh_inode_dotl(fid, inode);
86b3cbea03SAneesh Kumar K.V 		else
87b3cbea03SAneesh Kumar K.V 			retval = v9fs_refresh_inode(fid, inode);
88*6636b6dcSJianyong Wu 		p9_client_clunk(fid);
89*6636b6dcSJianyong Wu 
90f2eda2c6SAneesh Kumar K.V 		if (retval == -ENOENT)
91f2eda2c6SAneesh Kumar K.V 			return 0;
92f2eda2c6SAneesh Kumar K.V 		if (retval < 0)
93b3cbea03SAneesh Kumar K.V 			return retval;
94b3cbea03SAneesh Kumar K.V 	}
95b3cbea03SAneesh Kumar K.V out_valid:
96b3cbea03SAneesh Kumar K.V 	return 1;
97b3cbea03SAneesh Kumar K.V }
98b3cbea03SAneesh Kumar K.V 
99a488257cSAl Viro const struct dentry_operations v9fs_cached_dentry_operations = {
100b3cbea03SAneesh Kumar K.V 	.d_revalidate = v9fs_lookup_revalidate,
101ecf3d1f1SJeff Layton 	.d_weak_revalidate = v9fs_lookup_revalidate,
102e03abc0cSEric Van Hensbergen 	.d_delete = v9fs_cached_dentry_delete,
103e03abc0cSEric Van Hensbergen 	.d_release = v9fs_dentry_release,
104e03abc0cSEric Van Hensbergen };
105e03abc0cSEric Van Hensbergen 
106a488257cSAl Viro const struct dentry_operations v9fs_dentry_operations = {
107b26d4cd3SAl Viro 	.d_delete = always_delete_dentry,
108e69e7fe5SEric Van Hensbergen 	.d_release = v9fs_dentry_release,
109e69e7fe5SEric Van Hensbergen };
110