xref: /linux/include/net/9p/client.h (revision f00e6c1d282578b077b87b88f42e701bc40f2fff)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * 9P Client Definitions
4  *
5  *  Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
6  *  Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
7  */
8 
9 #ifndef NET_9P_CLIENT_H
10 #define NET_9P_CLIENT_H
11 
12 #include <linux/utsname.h>
13 #include <linux/idr.h>
14 #include <linux/tracepoint-defs.h>
15 
16 /* Number of requests per row */
17 #define P9_ROW_MAXTAG 255
18 
19 /* DEFAULT MSIZE = 32 pages worth of payload + P9_HDRSZ +
20  * room for write (16 extra) or read (11 extra) operands.
21  */
22 
23 #define DEFAULT_MSIZE ((128 * 1024) + P9_IOHDRSZ)
24 
25 /** enum p9_proto_versions - 9P protocol versions
26  * @p9_proto_legacy: 9P Legacy mode, pre-9P2000.u
27  * @p9_proto_2000u: 9P2000.u extension
28  * @p9_proto_2000L: 9P2000.L extension
29  */
30 
31 enum p9_proto_versions {
32 	p9_proto_legacy,
33 	p9_proto_2000u,
34 	p9_proto_2000L,
35 };
36 
37 
38 /**
39  * enum p9_trans_status - different states of underlying transports
40  * @Connected: transport is connected and healthy
41  * @Disconnected: transport has been disconnected
42  * @Hung: transport is connected by wedged
43  *
44  * This enumeration details the various states a transport
45  * instatiation can be in.
46  */
47 
48 enum p9_trans_status {
49 	Connected,
50 	BeginDisconnect,
51 	Disconnected,
52 	Hung,
53 };
54 
55 /**
56  * enum p9_req_status_t - status of a request
57  * @REQ_STATUS_ALLOC: request has been allocated but not sent
58  * @REQ_STATUS_UNSENT: request waiting to be sent
59  * @REQ_STATUS_SENT: request sent to server
60  * @REQ_STATUS_RCVD: response received from server
61  * @REQ_STATUS_FLSHD: request has been flushed
62  * @REQ_STATUS_ERROR: request encountered an error on the client side
63  */
64 
65 enum p9_req_status_t {
66 	REQ_STATUS_ALLOC,
67 	REQ_STATUS_UNSENT,
68 	REQ_STATUS_SENT,
69 	REQ_STATUS_RCVD,
70 	REQ_STATUS_FLSHD,
71 	REQ_STATUS_ERROR,
72 };
73 
74 /**
75  * struct p9_req_t - request slots
76  * @status: status of this request slot
77  * @t_err: transport error
78  * @wq: wait_queue for the client to block on for this request
79  * @tc: the request fcall structure
80  * @rc: the response fcall structure
81  * @req_list: link for higher level objects to chain requests
82  */
83 struct p9_req_t {
84 	int status;
85 	int t_err;
86 	refcount_t refcount;
87 	wait_queue_head_t wq;
88 	struct p9_fcall tc;
89 	struct p9_fcall rc;
90 	struct list_head req_list;
91 };
92 
93 /**
94  * struct p9_client - per client instance state
95  * @lock: protect @fids and @reqs
96  * @msize: maximum data size negotiated by protocol
97  * @proto_version: 9P protocol version to use
98  * @trans_mod: module API instantiated with this client
99  * @status: connection state
100  * @trans: tranport instance state and API
101  * @fids: All active FID handles
102  * @reqs: All active requests.
103  * @name: node name used as client id
104  *
105  * The client structure is used to keep track of various per-client
106  * state that has been instantiated.
107  */
108 struct p9_client {
109 	spinlock_t lock;
110 	unsigned int msize;
111 	unsigned char proto_version;
112 	struct p9_trans_module *trans_mod;
113 	enum p9_trans_status status;
114 	void *trans;
115 	struct kmem_cache *fcall_cache;
116 
117 	union {
118 		struct {
119 			int rfd;
120 			int wfd;
121 		} fd;
122 		struct {
123 			u16 port;
124 			bool privport;
125 
126 		} tcp;
127 	} trans_opts;
128 
129 	struct idr fids;
130 	struct idr reqs;
131 
132 	char name[__NEW_UTS_LEN + 1];
133 };
134 
135 /**
136  * struct p9_fd_opts - holds client options during parsing
137  * @msize: maximum data size negotiated by protocol
138  * @prot-Oversion: 9P protocol version to use
139  * @trans_mod: module API instantiated with this client
140  *
141  * These parsed options get transferred into client in
142  * apply_client_options()
143  */
144 struct p9_client_opts {
145 	unsigned int msize;
146 	unsigned char proto_version;
147 	struct p9_trans_module *trans_mod;
148 };
149 
150 /**
151  * struct p9_fd_opts - per-transport options for fd transport
152  * @rfd: file descriptor for reading (trans=fd)
153  * @wfd: file descriptor for writing (trans=fd)
154  * @port: port to connect to (trans=tcp)
155  * @privport: port is privileged
156  */
157 struct p9_fd_opts {
158 	int rfd;
159 	int wfd;
160 	u16 port;
161 	bool privport;
162 };
163 
164 /**
165  * struct p9_rdma_opts - Collection of mount options for rdma transport
166  * @port: port of connection
167  * @privport: Whether a privileged port may be used
168  * @sq_depth: The requested depth of the SQ. This really doesn't need
169  * to be any deeper than the number of threads used in the client
170  * @rq_depth: The depth of the RQ. Should be greater than or equal to SQ depth
171  * @timeout: Time to wait in msecs for CM events
172  */
173 struct p9_rdma_opts {
174 	short port;
175 	bool privport;
176 	int sq_depth;
177 	int rq_depth;
178 	long timeout;
179 };
180 
181 /**
182  * struct p9_session_opts - holds parsed options for v9fs_session_info
183  * @flags: session options of type &p9_session_flags
184  * @nodev: set to 1 to disable device mapping
185  * @debug: debug level
186  * @afid: authentication handle
187  * @cache: cache mode of type &p9_cache_bits
188  * @cachetag: the tag of the cache associated with this session
189  * @uname: string user name to mount hierarchy as
190  * @aname: mount specifier for remote hierarchy
191  * @dfltuid: default numeric userid to mount hierarchy as
192  * @dfltgid: default numeric groupid to mount hierarchy as
193  * @uid: if %V9FS_ACCESS_SINGLE, the numeric uid which mounted the hierarchy
194  * @session_lock_timeout: retry interval for blocking locks
195  * @ndentry_timeout_ms: Negative dentry lookup cache retention time in ms
196  *
197  * This strucure holds options which are parsed and will be transferred
198  * to the v9fs_session_info structure when mounted, and therefore largely
199  * duplicates struct v9fs_session_info.
200  */
201 struct p9_session_opts {
202 	unsigned int flags;
203 	unsigned char nodev;
204 	unsigned short debug;
205 	unsigned int afid;
206 	unsigned int cache;
207 	unsigned int ndentry_timeout_ms;
208 #ifdef CONFIG_9P_FSCACHE
209 	char *cachetag;
210 #endif
211 	char *uname;
212 	char *aname;
213 	kuid_t dfltuid;
214 	kgid_t dfltgid;
215 	kuid_t uid;
216 	long session_lock_timeout;
217 };
218 
219 /* Used by mount API to store parsed mount options */
220 struct v9fs_context {
221 	struct p9_client_opts	client_opts;
222 	struct p9_fd_opts	fd_opts;
223 	struct p9_rdma_opts	rdma_opts;
224 	struct p9_session_opts	session_opts;
225 };
226 
227 /**
228  * struct p9_fid - file system entity handle
229  * @clnt: back pointer to instantiating &p9_client
230  * @fid: numeric identifier for this handle
231  * @mode: current mode of this fid (enum?)
232  * @qid: the &p9_qid server identifier this handle points to
233  * @iounit: the server reported maximum transaction size for this file
234  * @uid: the numeric uid of the local user who owns this handle
235  * @rdir: readdir accounting structure (allocated on demand)
236  * @dlist: per-dentry fid tracking
237  *
238  * TODO: This needs lots of explanation.
239  */
240 enum fid_source {
241 	FID_FROM_OTHER,
242 	FID_FROM_INODE,
243 	FID_FROM_DENTRY,
244 };
245 
246 struct p9_fid {
247 	struct p9_client *clnt;
248 	u32 fid;
249 	refcount_t count;
250 	int mode;
251 	struct p9_qid qid;
252 	u32 iounit;
253 	kuid_t uid;
254 
255 	void *rdir;
256 
257 	struct hlist_node dlist;	/* list of all fids attached to a dentry */
258 	struct hlist_node ilist;
259 };
260 
261 /**
262  * struct p9_dirent - directory entry structure
263  * @qid: The p9 server qid for this dirent
264  * @d_off: offset to the next dirent
265  * @d_type: type of file
266  * @d_name: file name
267  */
268 
269 struct p9_dirent {
270 	struct p9_qid qid;
271 	u64 d_off;
272 	unsigned char d_type;
273 	char d_name[256];
274 };
275 
276 struct iov_iter;
277 
278 int p9_show_client_options(struct seq_file *m, struct p9_client *clnt);
279 int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb);
280 int p9_client_rename(struct p9_fid *fid, struct p9_fid *newdirfid,
281 		     const char *name);
282 int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
283 		       struct p9_fid *newdirfid, const char *new_name);
284 struct p9_client *p9_client_create(struct fs_context *fc);
285 void p9_client_destroy(struct p9_client *clnt);
286 void p9_client_disconnect(struct p9_client *clnt);
287 void p9_client_begin_disconnect(struct p9_client *clnt);
288 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
289 				const char *uname, kuid_t n_uname, const char *aname);
290 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
291 		const unsigned char * const *wnames, int clone);
292 int p9_client_open(struct p9_fid *fid, int mode);
293 int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,
294 							char *extension);
295 int p9_client_link(struct p9_fid *fid, struct p9_fid *oldfid, const char *newname);
296 int p9_client_symlink(struct p9_fid *fid, const char *name, const char *symname,
297 		kgid_t gid, struct p9_qid *qid);
298 int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags, u32 mode,
299 		kgid_t gid, struct p9_qid *qid);
300 int p9_client_clunk(struct p9_fid *fid);
301 int p9_client_fsync(struct p9_fid *fid, int datasync);
302 int p9_client_remove(struct p9_fid *fid);
303 int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags);
304 int p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err);
305 int p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to,
306 		int *err);
307 int p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err);
308 struct netfs_io_subrequest;
309 void p9_client_write_subreq(struct netfs_io_subrequest *subreq);
310 int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset);
311 int p9dirent_read(struct p9_client *clnt, char *buf, int len,
312 		  struct p9_dirent *dirent);
313 struct p9_wstat *p9_client_stat(struct p9_fid *fid);
314 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst);
315 int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *attr);
316 
317 struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
318 							u64 request_mask);
319 
320 int p9_client_mknod_dotl(struct p9_fid *oldfid, const char *name, int mode,
321 			dev_t rdev, kgid_t gid, struct p9_qid *qid);
322 int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode,
323 				kgid_t gid, struct p9_qid *qid);
324 int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status);
325 int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *fl);
326 void p9_fcall_fini(struct p9_fcall *fc);
327 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag);
328 
329 static inline void p9_req_get(struct p9_req_t *r)
330 {
331 	refcount_inc(&r->refcount);
332 }
333 
334 static inline int p9_req_try_get(struct p9_req_t *r)
335 {
336 	return refcount_inc_not_zero(&r->refcount);
337 }
338 
339 int p9_req_put(struct p9_client *c, struct p9_req_t *r);
340 
341 /* We cannot have the real tracepoints in header files,
342  * use a wrapper function */
343 DECLARE_TRACEPOINT(9p_fid_ref);
344 void do_trace_9p_fid_get(struct p9_fid *fid);
345 void do_trace_9p_fid_put(struct p9_fid *fid);
346 
347 /* fid reference counting helpers:
348  *  - fids used for any length of time should always be referenced through
349  *    p9_fid_get(), and released with p9_fid_put()
350  *  - v9fs_fid_lookup() or similar will automatically call get for you
351  *    and also require a put
352  *  - the *_fid_add() helpers will stash the fid in the inode,
353  *    at which point it is the responsibility of evict_inode()
354  *    to call the put
355  *  - the last put will automatically send a clunk to the server
356  */
357 static inline struct p9_fid *p9_fid_get(struct p9_fid *fid)
358 {
359 	if (tracepoint_enabled(9p_fid_ref))
360 		do_trace_9p_fid_get(fid);
361 
362 	refcount_inc(&fid->count);
363 
364 	return fid;
365 }
366 
367 static inline int p9_fid_put(struct p9_fid *fid)
368 {
369 	if (!fid || IS_ERR(fid))
370 		return 0;
371 
372 	if (tracepoint_enabled(9p_fid_ref))
373 		do_trace_9p_fid_put(fid);
374 
375 	if (!refcount_dec_and_test(&fid->count))
376 		return 0;
377 
378 	return p9_client_clunk(fid);
379 }
380 
381 void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status);
382 
383 int p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type,
384 		    int16_t *tag, int rewind);
385 int p9stat_read(struct p9_client *clnt, char *buf, int len,
386 		struct p9_wstat *st);
387 void p9stat_free(struct p9_wstat *stbuf);
388 
389 int p9_is_proto_dotu(struct p9_client *clnt);
390 int p9_is_proto_dotl(struct p9_client *clnt);
391 struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
392 				   const char *attr_name, u64 *attr_size);
393 int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
394 			  u64 attr_size, int flags);
395 int p9_client_readlink(struct p9_fid *fid, char **target);
396 
397 int p9_client_init(void);
398 void p9_client_exit(void);
399 
400 #endif /* NET_9P_CLIENT_H */
401