1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * 9P Client Definitions
5 * Copyright (C) 2008 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
9 #ifndef NET_9P_CLIENT_H
10 #define NET_9P_CLIENT_H
12 #include <linux/utsname.h>
13 #include <linux/idr.h>
14 #include <linux/tracepoint-defs.h>
16 /* Number of requests per row */
17 #define P9_ROW_MAXTAG 255
19 /** enum p9_proto_versions - 9P protocol versions
20 * @p9_proto_legacy: 9P Legacy mode, pre-9P2000.u
21 * @p9_proto_2000u: 9P2000.u extension
22 * @p9_proto_2000L: 9P2000.L extension
25 enum p9_proto_versions
{
33 * enum p9_trans_status - different states of underlying transports
34 * @Connected: transport is connected and healthy
35 * @Disconnected: transport has been disconnected
36 * @Hung: transport is connected by wedged
38 * This enumeration details the various states a transport
39 * instatiation can be in.
42 enum p9_trans_status
{
50 * enum p9_req_status_t - status of a request
51 * @REQ_STATUS_ALLOC: request has been allocated but not sent
52 * @REQ_STATUS_UNSENT: request waiting to be sent
53 * @REQ_STATUS_SENT: request sent to server
54 * @REQ_STATUS_RCVD: response received from server
55 * @REQ_STATUS_FLSHD: request has been flushed
56 * @REQ_STATUS_ERROR: request encountered an error on the client side
59 enum p9_req_status_t
{
69 * struct p9_req_t - request slots
70 * @status: status of this request slot
71 * @t_err: transport error
72 * @wq: wait_queue for the client to block on for this request
73 * @tc: the request fcall structure
74 * @rc: the response fcall structure
75 * @req_list: link for higher level objects to chain requests
84 struct list_head req_list
;
88 * struct p9_client - per client instance state
89 * @lock: protect @fids and @reqs
90 * @msize: maximum data size negotiated by protocol
91 * @proto_version: 9P protocol version to use
92 * @trans_mod: module API instantiated with this client
93 * @status: connection state
94 * @trans: tranport instance state and API
95 * @fids: All active FID handles
96 * @reqs: All active requests.
97 * @name: node name used as client id
99 * The client structure is used to keep track of various per-client
100 * state that has been instantiated.
105 unsigned char proto_version
;
106 struct p9_trans_module
*trans_mod
;
107 enum p9_trans_status status
;
109 struct kmem_cache
*fcall_cache
;
126 char name
[__NEW_UTS_LEN
+ 1];
130 * struct p9_fid - file system entity handle
131 * @clnt: back pointer to instantiating &p9_client
132 * @fid: numeric identifier for this handle
133 * @mode: current mode of this fid (enum?)
134 * @qid: the &p9_qid server identifier this handle points to
135 * @iounit: the server reported maximum transaction size for this file
136 * @uid: the numeric uid of the local user who owns this handle
137 * @rdir: readdir accounting structure (allocated on demand)
138 * @dlist: per-dentry fid tracking
140 * TODO: This needs lots of explanation.
149 struct p9_client
*clnt
;
159 struct hlist_node dlist
; /* list of all fids attached to a dentry */
160 struct hlist_node ilist
;
164 * struct p9_dirent - directory entry structure
165 * @qid: The p9 server qid for this dirent
166 * @d_off: offset to the next dirent
167 * @d_type: type of file
174 unsigned char d_type
;
180 int p9_show_client_options(struct seq_file
*m
, struct p9_client
*clnt
);
181 int p9_client_statfs(struct p9_fid
*fid
, struct p9_rstatfs
*sb
);
182 int p9_client_rename(struct p9_fid
*fid
, struct p9_fid
*newdirfid
,
184 int p9_client_renameat(struct p9_fid
*olddirfid
, const char *old_name
,
185 struct p9_fid
*newdirfid
, const char *new_name
);
186 struct p9_client
*p9_client_create(const char *dev_name
, char *options
);
187 void p9_client_destroy(struct p9_client
*clnt
);
188 void p9_client_disconnect(struct p9_client
*clnt
);
189 void p9_client_begin_disconnect(struct p9_client
*clnt
);
190 struct p9_fid
*p9_client_attach(struct p9_client
*clnt
, struct p9_fid
*afid
,
191 const char *uname
, kuid_t n_uname
, const char *aname
);
192 struct p9_fid
*p9_client_walk(struct p9_fid
*oldfid
, uint16_t nwname
,
193 const unsigned char * const *wnames
, int clone
);
194 int p9_client_open(struct p9_fid
*fid
, int mode
);
195 int p9_client_fcreate(struct p9_fid
*fid
, const char *name
, u32 perm
, int mode
,
197 int p9_client_link(struct p9_fid
*fid
, struct p9_fid
*oldfid
, const char *newname
);
198 int p9_client_symlink(struct p9_fid
*fid
, const char *name
, const char *symname
,
199 kgid_t gid
, struct p9_qid
*qid
);
200 int p9_client_create_dotl(struct p9_fid
*ofid
, const char *name
, u32 flags
, u32 mode
,
201 kgid_t gid
, struct p9_qid
*qid
);
202 int p9_client_clunk(struct p9_fid
*fid
);
203 int p9_client_fsync(struct p9_fid
*fid
, int datasync
);
204 int p9_client_remove(struct p9_fid
*fid
);
205 int p9_client_unlinkat(struct p9_fid
*dfid
, const char *name
, int flags
);
206 int p9_client_read(struct p9_fid
*fid
, u64 offset
, struct iov_iter
*to
, int *err
);
207 int p9_client_read_once(struct p9_fid
*fid
, u64 offset
, struct iov_iter
*to
,
209 int p9_client_write(struct p9_fid
*fid
, u64 offset
, struct iov_iter
*from
, int *err
);
210 struct netfs_io_subrequest
;
211 void p9_client_write_subreq(struct netfs_io_subrequest
*subreq
);
212 int p9_client_readdir(struct p9_fid
*fid
, char *data
, u32 count
, u64 offset
);
213 int p9dirent_read(struct p9_client
*clnt
, char *buf
, int len
,
214 struct p9_dirent
*dirent
);
215 struct p9_wstat
*p9_client_stat(struct p9_fid
*fid
);
216 int p9_client_wstat(struct p9_fid
*fid
, struct p9_wstat
*wst
);
217 int p9_client_setattr(struct p9_fid
*fid
, struct p9_iattr_dotl
*attr
);
219 struct p9_stat_dotl
*p9_client_getattr_dotl(struct p9_fid
*fid
,
222 int p9_client_mknod_dotl(struct p9_fid
*oldfid
, const char *name
, int mode
,
223 dev_t rdev
, kgid_t gid
, struct p9_qid
*qid
);
224 int p9_client_mkdir_dotl(struct p9_fid
*fid
, const char *name
, int mode
,
225 kgid_t gid
, struct p9_qid
*qid
);
226 int p9_client_lock_dotl(struct p9_fid
*fid
, struct p9_flock
*flock
, u8
*status
);
227 int p9_client_getlock_dotl(struct p9_fid
*fid
, struct p9_getlock
*fl
);
228 void p9_fcall_fini(struct p9_fcall
*fc
);
229 struct p9_req_t
*p9_tag_lookup(struct p9_client
*c
, u16 tag
);
231 static inline void p9_req_get(struct p9_req_t
*r
)
233 refcount_inc(&r
->refcount
);
236 static inline int p9_req_try_get(struct p9_req_t
*r
)
238 return refcount_inc_not_zero(&r
->refcount
);
241 int p9_req_put(struct p9_client
*c
, struct p9_req_t
*r
);
243 /* We cannot have the real tracepoints in header files,
244 * use a wrapper function */
245 DECLARE_TRACEPOINT(9p_fid_ref
);
246 void do_trace_9p_fid_get(struct p9_fid
*fid
);
247 void do_trace_9p_fid_put(struct p9_fid
*fid
);
249 /* fid reference counting helpers:
250 * - fids used for any length of time should always be referenced through
251 * p9_fid_get(), and released with p9_fid_put()
252 * - v9fs_fid_lookup() or similar will automatically call get for you
253 * and also require a put
254 * - the *_fid_add() helpers will stash the fid in the inode,
255 * at which point it is the responsibility of evict_inode()
257 * - the last put will automatically send a clunk to the server
259 static inline struct p9_fid
*p9_fid_get(struct p9_fid
*fid
)
261 if (tracepoint_enabled(9p_fid_ref
))
262 do_trace_9p_fid_get(fid
);
264 refcount_inc(&fid
->count
);
269 static inline int p9_fid_put(struct p9_fid
*fid
)
271 if (!fid
|| IS_ERR(fid
))
274 if (tracepoint_enabled(9p_fid_ref
))
275 do_trace_9p_fid_put(fid
);
277 if (!refcount_dec_and_test(&fid
->count
))
280 return p9_client_clunk(fid
);
283 void p9_client_cb(struct p9_client
*c
, struct p9_req_t
*req
, int status
);
285 int p9_parse_header(struct p9_fcall
*pdu
, int32_t *size
, int8_t *type
,
286 int16_t *tag
, int rewind
);
287 int p9stat_read(struct p9_client
*clnt
, char *buf
, int len
,
288 struct p9_wstat
*st
);
289 void p9stat_free(struct p9_wstat
*stbuf
);
291 int p9_is_proto_dotu(struct p9_client
*clnt
);
292 int p9_is_proto_dotl(struct p9_client
*clnt
);
293 struct p9_fid
*p9_client_xattrwalk(struct p9_fid
*file_fid
,
294 const char *attr_name
, u64
*attr_size
);
295 int p9_client_xattrcreate(struct p9_fid
*fid
, const char *name
,
296 u64 attr_size
, int flags
);
297 int p9_client_readlink(struct p9_fid
*fid
, char **target
);
299 int p9_client_init(void);
300 void p9_client_exit(void);
302 #endif /* NET_9P_CLIENT_H */