xref: /linux/fs/9p/v9fs.h (revision 5670a84b5cda6b82016282accfd61ef36aceafbf)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * V9FS definitions.
4  *
5  *  Copyright (C) 2004-2008 by Eric Van Hensbergen <ericvh@gmail.com>
6  *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
7  */
8 #ifndef FS_9P_V9FS_H
9 #define FS_9P_V9FS_H
10 
11 #include <linux/backing-dev.h>
12 #include <linux/netfs.h>
13 #include <linux/fs_parser.h>
14 #include <net/9p/client.h>
15 #include <net/9p/transport.h>
16 
17 /**
18  * enum p9_session_flags - option flags for each 9P session
19  * @V9FS_PROTO_2000U: whether or not to use 9P2000.u extensions
20  * @V9FS_PROTO_2000L: whether or not to use 9P2000.l extensions
21  * @V9FS_ACCESS_SINGLE: only the mounting user can access the hierarchy
22  * @V9FS_ACCESS_USER: a new attach will be issued for every user (default)
23  * @V9FS_ACCESS_CLIENT: Just like user, but access check is performed on client.
24  * @V9FS_ACCESS_ANY: use a single attach for all users
25  * @V9FS_ACCESS_MASK: bit mask of different ACCESS options
26  * @V9FS_POSIX_ACL: POSIX ACLs are enforced
27  * @V9FS_NDENTRY_TIMEOUT_SET: Has negative dentry timeout retention time been
28  *                            overridden by negtimeout mount option
29  *
30  * Session flags reflect options selected by users at mount time
31  */
32 #define	V9FS_ACCESS_ANY (V9FS_ACCESS_SINGLE | \
33 			 V9FS_ACCESS_USER |   \
34 			 V9FS_ACCESS_CLIENT)
35 #define V9FS_ACCESS_MASK V9FS_ACCESS_ANY
36 #define V9FS_ACL_MASK V9FS_POSIX_ACL
37 
38 enum p9_session_flags {
39 	V9FS_PROTO_2000U         = 0x01,
40 	V9FS_PROTO_2000L         = 0x02,
41 	V9FS_ACCESS_SINGLE       = 0x04,
42 	V9FS_ACCESS_USER         = 0x08,
43 	V9FS_ACCESS_CLIENT       = 0x10,
44 	V9FS_POSIX_ACL           = 0x20,
45 	V9FS_NO_XATTR            = 0x40,
46 	V9FS_IGNORE_QV           = 0x80, /* ignore qid.version for cache hints */
47 	V9FS_DIRECT_IO           = 0x100,
48 	V9FS_SYNC                = 0x200,
49 	V9FS_NDENTRY_TIMEOUT_SET = 0x400,
50 };
51 
52 /**
53  * enum p9_cache_shortcuts - human readable cache preferences
54  * @CACHE_SC_NONE: disable all caches
55  * @CACHE_SC_READAHEAD: only provide caching for readahead
56  * @CACHE_SC_MMAP: provide caching to enable mmap
57  * @CACHE_SC_LOOSE: non-coherent caching for files and meta data
58  * @CACHE_SC_FSCACHE: persistent non-coherent caching for files and meta-data
59  *
60  */
61 
62 enum p9_cache_shortcuts {
63 	CACHE_SC_NONE       = 0b00000000,
64 	CACHE_SC_READAHEAD  = 0b00000001,
65 	CACHE_SC_MMAP       = 0b00000101,
66 	CACHE_SC_LOOSE      = 0b00001111,
67 	CACHE_SC_FSCACHE    = 0b10001111,
68 };
69 
70 /**
71  * enum p9_cache_bits - possible values of ->cache
72  * @CACHE_NONE: caches disabled
73  * @CACHE_FILE: file caching (open to close)
74  * @CACHE_META: meta-data and directory caching
75  * @CACHE_WRITEBACK: write-back caching for files
76  * @CACHE_LOOSE: don't check cache consistency
77  * @CACHE_FSCACHE: local persistent caches
78  *
79  */
80 
81 enum p9_cache_bits {
82 	CACHE_NONE          = 0b00000000,
83 	CACHE_FILE          = 0b00000001,
84 	CACHE_META          = 0b00000010,
85 	CACHE_WRITEBACK     = 0b00000100,
86 	CACHE_LOOSE         = 0b00001000,
87 	CACHE_FSCACHE       = 0b10000000,
88 };
89 
90 /**
91  * struct v9fs_session_info - per-instance session information
92  * @flags: session options of type &p9_session_flags
93  * @nodev: set to 1 to disable device mapping
94  * @debug: debug level
95  * @afid: authentication handle
96  * @cache: cache mode of type &p9_cache_bits
97  * @ndentry_timeout: Negative dentry lookup cache retention time in ms
98  * @cachetag: the tag of the cache associated with this session
99  * @fscache: session cookie associated with FS-Cache
100  * @uname: string user name to mount hierarchy as
101  * @aname: mount specifier for remote hierarchy
102  * @maxdata: maximum data to be sent/recvd per protocol message
103  * @dfltuid: default numeric userid to mount hierarchy as
104  * @dfltgid: default numeric groupid to mount hierarchy as
105  * @uid: if %V9FS_ACCESS_SINGLE, the numeric uid which mounted the hierarchy
106  * @clnt: reference to 9P network client instantiated for this session
107  * @slist: reference to list of registered 9p sessions
108  * @ndentry_timeout_ms: Negative dentry caching retention time
109  *
110  * This structure holds state for each session instance established during
111  * a sys_mount() .
112  *
113  * Bugs: there seems to be a lot of state which could be condensed and/or
114  * removed.
115  */
116 
117 struct v9fs_session_info {
118 	/* options */
119 	unsigned int flags;
120 	unsigned char nodev;
121 	unsigned short debug;
122 	unsigned int afid;
123 	unsigned int cache;
124 	unsigned int ndentry_timeout_ms;
125 #ifdef CONFIG_9P_FSCACHE
126 	char *cachetag;
127 	struct fscache_volume *fscache;
128 #endif
129 
130 	char *uname;		/* user name to mount as */
131 	char *aname;		/* name of remote hierarchy being mounted */
132 	unsigned int maxdata;	/* max data for client interface */
133 	kuid_t dfltuid;		/* default uid/muid for legacy support */
134 	kgid_t dfltgid;		/* default gid for legacy support */
135 	kuid_t uid;		/* if ACCESS_SINGLE, the uid that has access */
136 	struct p9_client *clnt;	/* 9p client */
137 	struct list_head slist; /* list of sessions registered with v9fs */
138 	struct rw_semaphore rename_sem;
139 	long session_lock_timeout; /* retry interval for blocking locks */
140 };
141 
142 #define NDENTRY_TIMEOUT_NEVER (-1U)
143 
144 /* cache_validity flags */
145 #define V9FS_INO_INVALID_ATTR 0x01
146 
147 struct v9fs_inode {
148 	struct netfs_inode netfs; /* Netfslib context and vfs inode */
149 	struct p9_qid qid;
150 	unsigned int cache_validity;
151 	struct mutex v_mutex;
152 };
153 
154 static inline struct v9fs_inode *V9FS_I(const struct inode *inode)
155 {
156 	return container_of(inode, struct v9fs_inode, netfs.inode);
157 }
158 
159 static inline struct fscache_cookie *v9fs_inode_cookie(struct v9fs_inode *v9inode)
160 {
161 #ifdef CONFIG_9P_FSCACHE
162 	return netfs_i_cookie(&v9inode->netfs);
163 #else
164 	return NULL;
165 #endif
166 }
167 
168 static inline struct fscache_volume *v9fs_session_cache(struct v9fs_session_info *v9ses)
169 {
170 #ifdef CONFIG_9P_FSCACHE
171 	return v9ses->fscache;
172 #else
173 	return NULL;
174 #endif
175 }
176 
177 extern const struct fs_parameter_spec v9fs_param_spec[];
178 
179 extern int v9fs_parse_param(struct fs_context *fc, struct fs_parameter *param);
180 extern int v9fs_show_options(struct seq_file *m, struct dentry *root);
181 
182 struct p9_fid *v9fs_session_init(struct v9fs_session_info *v9ses,
183 				 struct fs_context *fc);
184 extern void v9fs_session_close(struct v9fs_session_info *v9ses);
185 extern void v9fs_session_cancel(struct v9fs_session_info *v9ses);
186 extern void v9fs_session_begin_cancel(struct v9fs_session_info *v9ses);
187 extern struct dentry *v9fs_vfs_lookup(struct inode *dir, struct dentry *dentry,
188 				      unsigned int flags);
189 extern int v9fs_vfs_unlink(struct inode *i, struct dentry *d);
190 extern int v9fs_vfs_rmdir(struct inode *i, struct dentry *d);
191 extern int v9fs_vfs_rename(struct mnt_idmap *idmap,
192 			   struct inode *old_dir, struct dentry *old_dentry,
193 			   struct inode *new_dir, struct dentry *new_dentry,
194 			   unsigned int flags);
195 extern struct inode *v9fs_inode_from_fid(struct v9fs_session_info *v9ses,
196 					 struct p9_fid *fid,
197 					 struct super_block *sb, int new);
198 extern const struct inode_operations v9fs_dir_inode_operations_dotl;
199 extern const struct inode_operations v9fs_file_inode_operations_dotl;
200 extern const struct inode_operations v9fs_symlink_inode_operations_dotl;
201 extern const struct netfs_request_ops v9fs_req_ops;
202 extern struct inode *v9fs_inode_from_fid_dotl(struct v9fs_session_info *v9ses,
203 					      struct p9_fid *fid,
204 					      struct super_block *sb, int new);
205 
206 /* other default globals */
207 #define V9FS_PORT	564
208 #define V9FS_DEFUSER	"nobody"
209 #define V9FS_DEFANAME	""
210 #define V9FS_DEFUID	KUIDT_INIT(-2)
211 #define V9FS_DEFGID	KGIDT_INIT(-2)
212 
213 static inline struct v9fs_session_info *v9fs_inode2v9ses(struct inode *inode)
214 {
215 	return inode->i_sb->s_fs_info;
216 }
217 
218 static inline struct v9fs_session_info *v9fs_dentry2v9ses(const struct dentry *dentry)
219 {
220 	return dentry->d_sb->s_fs_info;
221 }
222 
223 static inline int v9fs_proto_dotu(struct v9fs_session_info *v9ses)
224 {
225 	return v9ses->flags & V9FS_PROTO_2000U;
226 }
227 
228 static inline int v9fs_proto_dotl(struct v9fs_session_info *v9ses)
229 {
230 	return v9ses->flags & V9FS_PROTO_2000L;
231 }
232 
233 /**
234  * v9fs_get_inode_from_fid - Helper routine to populate an inode by
235  * issuing a attribute request
236  * @v9ses: session information
237  * @fid: fid to issue attribute request for
238  * @sb: superblock on which to create inode
239  *
240  */
241 static inline struct inode *
242 v9fs_get_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
243 			struct super_block *sb)
244 {
245 	if (v9fs_proto_dotl(v9ses))
246 		return v9fs_inode_from_fid_dotl(v9ses, fid, sb, 0);
247 	else
248 		return v9fs_inode_from_fid(v9ses, fid, sb, 0);
249 }
250 
251 /**
252  * v9fs_get_new_inode_from_fid - Helper routine to populate an inode by
253  * issuing a attribute request
254  * @v9ses: session information
255  * @fid: fid to issue attribute request for
256  * @sb: superblock on which to create inode
257  *
258  */
259 static inline struct inode *
260 v9fs_get_new_inode_from_fid(struct v9fs_session_info *v9ses, struct p9_fid *fid,
261 			    struct super_block *sb)
262 {
263 	if (v9fs_proto_dotl(v9ses))
264 		return v9fs_inode_from_fid_dotl(v9ses, fid, sb, 1);
265 	else
266 		return v9fs_inode_from_fid(v9ses, fid, sb, 1);
267 }
268 
269 #endif
270