client.h 12 KB

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