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_fid - file system entity handle 137 * @clnt: back pointer to instantiating &p9_client 138 * @fid: numeric identifier for this handle 139 * @mode: current mode of this fid (enum?) 140 * @qid: the &p9_qid server identifier this handle points to 141 * @iounit: the server reported maximum transaction size for this file 142 * @uid: the numeric uid of the local user who owns this handle 143 * @rdir: readdir accounting structure (allocated on demand) 144 * @dlist: per-dentry fid tracking 145 * 146 * TODO: This needs lots of explanation. 147 */ 148 enum fid_source { 149 FID_FROM_OTHER, 150 FID_FROM_INODE, 151 FID_FROM_DENTRY, 152 }; 153 154 struct p9_fid { 155 struct p9_client *clnt; 156 u32 fid; 157 refcount_t count; 158 int mode; 159 struct p9_qid qid; 160 u32 iounit; 161 kuid_t uid; 162 163 void *rdir; 164 165 struct hlist_node dlist; /* list of all fids attached to a dentry */ 166 struct hlist_node ilist; 167 }; 168 169 /** 170 * struct p9_dirent - directory entry structure 171 * @qid: The p9 server qid for this dirent 172 * @d_off: offset to the next dirent 173 * @d_type: type of file 174 * @d_name: file name 175 */ 176 177 struct p9_dirent { 178 struct p9_qid qid; 179 u64 d_off; 180 unsigned char d_type; 181 char d_name[256]; 182 }; 183 184 struct iov_iter; 185 186 int p9_show_client_options(struct seq_file *m, struct p9_client *clnt); 187 int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb); 188 int p9_client_rename(struct p9_fid *fid, struct p9_fid *newdirfid, 189 const char *name); 190 int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name, 191 struct p9_fid *newdirfid, const char *new_name); 192 struct p9_client *p9_client_create(const char *dev_name, char *options); 193 void p9_client_destroy(struct p9_client *clnt); 194 void p9_client_disconnect(struct p9_client *clnt); 195 void p9_client_begin_disconnect(struct p9_client *clnt); 196 struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid, 197 const char *uname, kuid_t n_uname, const char *aname); 198 struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname, 199 const unsigned char * const *wnames, int clone); 200 int p9_client_open(struct p9_fid *fid, int mode); 201 int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode, 202 char *extension); 203 int p9_client_link(struct p9_fid *fid, struct p9_fid *oldfid, const char *newname); 204 int p9_client_symlink(struct p9_fid *fid, const char *name, const char *symname, 205 kgid_t gid, struct p9_qid *qid); 206 int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags, u32 mode, 207 kgid_t gid, struct p9_qid *qid); 208 int p9_client_clunk(struct p9_fid *fid); 209 int p9_client_fsync(struct p9_fid *fid, int datasync); 210 int p9_client_remove(struct p9_fid *fid); 211 int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags); 212 int p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err); 213 int p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to, 214 int *err); 215 int p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err); 216 struct netfs_io_subrequest; 217 void p9_client_write_subreq(struct netfs_io_subrequest *subreq); 218 int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset); 219 int p9dirent_read(struct p9_client *clnt, char *buf, int len, 220 struct p9_dirent *dirent); 221 struct p9_wstat *p9_client_stat(struct p9_fid *fid); 222 int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst); 223 int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *attr); 224 225 struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid, 226 u64 request_mask); 227 228 int p9_client_mknod_dotl(struct p9_fid *oldfid, const char *name, int mode, 229 dev_t rdev, kgid_t gid, struct p9_qid *qid); 230 int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode, 231 kgid_t gid, struct p9_qid *qid); 232 int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status); 233 int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *fl); 234 void p9_fcall_fini(struct p9_fcall *fc); 235 struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag); 236 237 static inline void p9_req_get(struct p9_req_t *r) 238 { 239 refcount_inc(&r->refcount); 240 } 241 242 static inline int p9_req_try_get(struct p9_req_t *r) 243 { 244 return refcount_inc_not_zero(&r->refcount); 245 } 246 247 int p9_req_put(struct p9_client *c, struct p9_req_t *r); 248 249 /* We cannot have the real tracepoints in header files, 250 * use a wrapper function */ 251 DECLARE_TRACEPOINT(9p_fid_ref); 252 void do_trace_9p_fid_get(struct p9_fid *fid); 253 void do_trace_9p_fid_put(struct p9_fid *fid); 254 255 /* fid reference counting helpers: 256 * - fids used for any length of time should always be referenced through 257 * p9_fid_get(), and released with p9_fid_put() 258 * - v9fs_fid_lookup() or similar will automatically call get for you 259 * and also require a put 260 * - the *_fid_add() helpers will stash the fid in the inode, 261 * at which point it is the responsibility of evict_inode() 262 * to call the put 263 * - the last put will automatically send a clunk to the server 264 */ 265 static inline struct p9_fid *p9_fid_get(struct p9_fid *fid) 266 { 267 if (tracepoint_enabled(9p_fid_ref)) 268 do_trace_9p_fid_get(fid); 269 270 refcount_inc(&fid->count); 271 272 return fid; 273 } 274 275 static inline int p9_fid_put(struct p9_fid *fid) 276 { 277 if (!fid || IS_ERR(fid)) 278 return 0; 279 280 if (tracepoint_enabled(9p_fid_ref)) 281 do_trace_9p_fid_put(fid); 282 283 if (!refcount_dec_and_test(&fid->count)) 284 return 0; 285 286 return p9_client_clunk(fid); 287 } 288 289 void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status); 290 291 int p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type, 292 int16_t *tag, int rewind); 293 int p9stat_read(struct p9_client *clnt, char *buf, int len, 294 struct p9_wstat *st); 295 void p9stat_free(struct p9_wstat *stbuf); 296 297 int p9_is_proto_dotu(struct p9_client *clnt); 298 int p9_is_proto_dotl(struct p9_client *clnt); 299 struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid, 300 const char *attr_name, u64 *attr_size); 301 int p9_client_xattrcreate(struct p9_fid *fid, const char *name, 302 u64 attr_size, int flags); 303 int p9_client_readlink(struct p9_fid *fid, char **target); 304 305 int p9_client_init(void); 306 void p9_client_exit(void); 307 308 #endif /* NET_9P_CLIENT_H */ 309