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 *
196 * This strucure holds options which are parsed and will be transferred
197 * to the v9fs_session_info structure when mounted, and therefore largely
198 * duplicates struct v9fs_session_info.
199 */
200 struct p9_session_opts {
201 unsigned int flags;
202 unsigned char nodev;
203 unsigned short debug;
204 unsigned int afid;
205 unsigned int cache;
206 #ifdef CONFIG_9P_FSCACHE
207 char *cachetag;
208 #endif
209 char *uname;
210 char *aname;
211 kuid_t dfltuid;
212 kgid_t dfltgid;
213 kuid_t uid;
214 long session_lock_timeout;
215 };
216
217 /* Used by mount API to store parsed mount options */
218 struct v9fs_context {
219 struct p9_client_opts client_opts;
220 struct p9_fd_opts fd_opts;
221 struct p9_rdma_opts rdma_opts;
222 struct p9_session_opts session_opts;
223 };
224
225 /**
226 * struct p9_fid - file system entity handle
227 * @clnt: back pointer to instantiating &p9_client
228 * @fid: numeric identifier for this handle
229 * @mode: current mode of this fid (enum?)
230 * @qid: the &p9_qid server identifier this handle points to
231 * @iounit: the server reported maximum transaction size for this file
232 * @uid: the numeric uid of the local user who owns this handle
233 * @rdir: readdir accounting structure (allocated on demand)
234 * @dlist: per-dentry fid tracking
235 *
236 * TODO: This needs lots of explanation.
237 */
238 enum fid_source {
239 FID_FROM_OTHER,
240 FID_FROM_INODE,
241 FID_FROM_DENTRY,
242 };
243
244 struct p9_fid {
245 struct p9_client *clnt;
246 u32 fid;
247 refcount_t count;
248 int mode;
249 struct p9_qid qid;
250 u32 iounit;
251 kuid_t uid;
252
253 void *rdir;
254
255 struct hlist_node dlist; /* list of all fids attached to a dentry */
256 struct hlist_node ilist;
257 };
258
259 /**
260 * struct p9_dirent - directory entry structure
261 * @qid: The p9 server qid for this dirent
262 * @d_off: offset to the next dirent
263 * @d_type: type of file
264 * @d_name: file name
265 */
266
267 struct p9_dirent {
268 struct p9_qid qid;
269 u64 d_off;
270 unsigned char d_type;
271 char d_name[256];
272 };
273
274 struct iov_iter;
275
276 int p9_show_client_options(struct seq_file *m, struct p9_client *clnt);
277 int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb);
278 int p9_client_rename(struct p9_fid *fid, struct p9_fid *newdirfid,
279 const char *name);
280 int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
281 struct p9_fid *newdirfid, const char *new_name);
282 struct p9_client *p9_client_create(struct fs_context *fc);
283 void p9_client_destroy(struct p9_client *clnt);
284 void p9_client_disconnect(struct p9_client *clnt);
285 void p9_client_begin_disconnect(struct p9_client *clnt);
286 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
287 const char *uname, kuid_t n_uname, const char *aname);
288 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
289 const unsigned char * const *wnames, int clone);
290 int p9_client_open(struct p9_fid *fid, int mode);
291 int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,
292 char *extension);
293 int p9_client_link(struct p9_fid *fid, struct p9_fid *oldfid, const char *newname);
294 int p9_client_symlink(struct p9_fid *fid, const char *name, const char *symname,
295 kgid_t gid, struct p9_qid *qid);
296 int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags, u32 mode,
297 kgid_t gid, struct p9_qid *qid);
298 int p9_client_clunk(struct p9_fid *fid);
299 int p9_client_fsync(struct p9_fid *fid, int datasync);
300 int p9_client_remove(struct p9_fid *fid);
301 int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags);
302 int p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err);
303 int p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to,
304 int *err);
305 int p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err);
306 struct netfs_io_subrequest;
307 void p9_client_write_subreq(struct netfs_io_subrequest *subreq);
308 int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset);
309 int p9dirent_read(struct p9_client *clnt, char *buf, int len,
310 struct p9_dirent *dirent);
311 struct p9_wstat *p9_client_stat(struct p9_fid *fid);
312 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst);
313 int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *attr);
314
315 struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
316 u64 request_mask);
317
318 int p9_client_mknod_dotl(struct p9_fid *oldfid, const char *name, int mode,
319 dev_t rdev, kgid_t gid, struct p9_qid *qid);
320 int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode,
321 kgid_t gid, struct p9_qid *qid);
322 int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status);
323 int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *fl);
324 void p9_fcall_fini(struct p9_fcall *fc);
325 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag);
326
p9_req_get(struct p9_req_t * r)327 static inline void p9_req_get(struct p9_req_t *r)
328 {
329 refcount_inc(&r->refcount);
330 }
331
p9_req_try_get(struct p9_req_t * r)332 static inline int p9_req_try_get(struct p9_req_t *r)
333 {
334 return refcount_inc_not_zero(&r->refcount);
335 }
336
337 int p9_req_put(struct p9_client *c, struct p9_req_t *r);
338
339 /* We cannot have the real tracepoints in header files,
340 * use a wrapper function */
341 DECLARE_TRACEPOINT(9p_fid_ref);
342 void do_trace_9p_fid_get(struct p9_fid *fid);
343 void do_trace_9p_fid_put(struct p9_fid *fid);
344
345 /* fid reference counting helpers:
346 * - fids used for any length of time should always be referenced through
347 * p9_fid_get(), and released with p9_fid_put()
348 * - v9fs_fid_lookup() or similar will automatically call get for you
349 * and also require a put
350 * - the *_fid_add() helpers will stash the fid in the inode,
351 * at which point it is the responsibility of evict_inode()
352 * to call the put
353 * - the last put will automatically send a clunk to the server
354 */
p9_fid_get(struct p9_fid * fid)355 static inline struct p9_fid *p9_fid_get(struct p9_fid *fid)
356 {
357 if (tracepoint_enabled(9p_fid_ref))
358 do_trace_9p_fid_get(fid);
359
360 refcount_inc(&fid->count);
361
362 return fid;
363 }
364
p9_fid_put(struct p9_fid * fid)365 static inline int p9_fid_put(struct p9_fid *fid)
366 {
367 if (!fid || IS_ERR(fid))
368 return 0;
369
370 if (tracepoint_enabled(9p_fid_ref))
371 do_trace_9p_fid_put(fid);
372
373 if (!refcount_dec_and_test(&fid->count))
374 return 0;
375
376 return p9_client_clunk(fid);
377 }
378
379 void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status);
380
381 int p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type,
382 int16_t *tag, int rewind);
383 int p9stat_read(struct p9_client *clnt, char *buf, int len,
384 struct p9_wstat *st);
385 void p9stat_free(struct p9_wstat *stbuf);
386
387 int p9_is_proto_dotu(struct p9_client *clnt);
388 int p9_is_proto_dotl(struct p9_client *clnt);
389 struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
390 const char *attr_name, u64 *attr_size);
391 int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
392 u64 attr_size, int flags);
393 int p9_client_readlink(struct p9_fid *fid, char **target);
394
395 int p9_client_init(void);
396 void p9_client_exit(void);
397
398 #endif /* NET_9P_CLIENT_H */
399