client.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * 9P Client Definitions
  4. *
  5. * Copyright (C) 2008 by Eric Van Hensbergen <[email protected]>
  6. * Copyright (C) 2007 by Latchesar Ionkov <[email protected]>
  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. /** enum p9_proto_versions - 9P protocol versions
  16. * @p9_proto_legacy: 9P Legacy mode, pre-9P2000.u
  17. * @p9_proto_2000u: 9P2000.u extension
  18. * @p9_proto_2000L: 9P2000.L extension
  19. */
  20. enum p9_proto_versions {
  21. p9_proto_legacy,
  22. p9_proto_2000u,
  23. p9_proto_2000L,
  24. };
  25. /**
  26. * enum p9_trans_status - different states of underlying transports
  27. * @Connected: transport is connected and healthy
  28. * @Disconnected: transport has been disconnected
  29. * @Hung: transport is connected by wedged
  30. *
  31. * This enumeration details the various states a transport
  32. * instatiation can be in.
  33. */
  34. enum p9_trans_status {
  35. Connected,
  36. BeginDisconnect,
  37. Disconnected,
  38. Hung,
  39. };
  40. /**
  41. * enum p9_req_status_t - status of a request
  42. * @REQ_STATUS_ALLOC: request has been allocated but not sent
  43. * @REQ_STATUS_UNSENT: request waiting to be sent
  44. * @REQ_STATUS_SENT: request sent to server
  45. * @REQ_STATUS_RCVD: response received from server
  46. * @REQ_STATUS_FLSHD: request has been flushed
  47. * @REQ_STATUS_ERROR: request encountered an error on the client side
  48. */
  49. enum p9_req_status_t {
  50. REQ_STATUS_ALLOC,
  51. REQ_STATUS_UNSENT,
  52. REQ_STATUS_SENT,
  53. REQ_STATUS_RCVD,
  54. REQ_STATUS_FLSHD,
  55. REQ_STATUS_ERROR,
  56. };
  57. /**
  58. * struct p9_req_t - request slots
  59. * @status: status of this request slot
  60. * @t_err: transport error
  61. * @wq: wait_queue for the client to block on for this request
  62. * @tc: the request fcall structure
  63. * @rc: the response fcall structure
  64. * @req_list: link for higher level objects to chain requests
  65. */
  66. struct p9_req_t {
  67. int status;
  68. int t_err;
  69. refcount_t refcount;
  70. wait_queue_head_t wq;
  71. struct p9_fcall tc;
  72. struct p9_fcall rc;
  73. struct list_head req_list;
  74. };
  75. /**
  76. * struct p9_client - per client instance state
  77. * @lock: protect @fids and @reqs
  78. * @msize: maximum data size negotiated by protocol
  79. * @proto_version: 9P protocol version to use
  80. * @trans_mod: module API instantiated with this client
  81. * @status: connection state
  82. * @trans: tranport instance state and API
  83. * @fids: All active FID handles
  84. * @reqs: All active requests.
  85. * @name: node name used as client id
  86. *
  87. * The client structure is used to keep track of various per-client
  88. * state that has been instantiated.
  89. */
  90. struct p9_client {
  91. spinlock_t lock;
  92. unsigned int msize;
  93. unsigned char proto_version;
  94. struct p9_trans_module *trans_mod;
  95. enum p9_trans_status status;
  96. void *trans;
  97. struct kmem_cache *fcall_cache;
  98. union {
  99. struct {
  100. int rfd;
  101. int wfd;
  102. } fd;
  103. struct {
  104. u16 port;
  105. bool privport;
  106. } tcp;
  107. } trans_opts;
  108. struct idr fids;
  109. struct idr reqs;
  110. char name[__NEW_UTS_LEN + 1];
  111. };
  112. /**
  113. * struct p9_fid - file system entity handle
  114. * @clnt: back pointer to instantiating &p9_client
  115. * @fid: numeric identifier for this handle
  116. * @mode: current mode of this fid (enum?)
  117. * @qid: the &p9_qid server identifier this handle points to
  118. * @iounit: the server reported maximum transaction size for this file
  119. * @uid: the numeric uid of the local user who owns this handle
  120. * @rdir: readdir accounting structure (allocated on demand)
  121. * @dlist: per-dentry fid tracking
  122. *
  123. * TODO: This needs lots of explanation.
  124. */
  125. enum fid_source {
  126. FID_FROM_OTHER,
  127. FID_FROM_INODE,
  128. FID_FROM_DENTRY,
  129. };
  130. struct p9_fid {
  131. struct p9_client *clnt;
  132. u32 fid;
  133. refcount_t count;
  134. int mode;
  135. struct p9_qid qid;
  136. u32 iounit;
  137. kuid_t uid;
  138. void *rdir;
  139. struct hlist_node dlist; /* list of all fids attached to a dentry */
  140. struct hlist_node ilist;
  141. };
  142. /**
  143. * struct p9_dirent - directory entry structure
  144. * @qid: The p9 server qid for this dirent
  145. * @d_off: offset to the next dirent
  146. * @d_type: type of file
  147. * @d_name: file name
  148. */
  149. struct p9_dirent {
  150. struct p9_qid qid;
  151. u64 d_off;
  152. unsigned char d_type;
  153. char d_name[256];
  154. };
  155. struct iov_iter;
  156. int p9_show_client_options(struct seq_file *m, struct p9_client *clnt);
  157. int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb);
  158. int p9_client_rename(struct p9_fid *fid, struct p9_fid *newdirfid,
  159. const char *name);
  160. int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,
  161. struct p9_fid *newdirfid, const char *new_name);
  162. struct p9_client *p9_client_create(const char *dev_name, char *options);
  163. void p9_client_destroy(struct p9_client *clnt);
  164. void p9_client_disconnect(struct p9_client *clnt);
  165. void p9_client_begin_disconnect(struct p9_client *clnt);
  166. struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,
  167. const char *uname, kuid_t n_uname, const char *aname);
  168. struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,
  169. const unsigned char * const *wnames, int clone);
  170. int p9_client_open(struct p9_fid *fid, int mode);
  171. int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,
  172. char *extension);
  173. int p9_client_link(struct p9_fid *fid, struct p9_fid *oldfid, const char *newname);
  174. int p9_client_symlink(struct p9_fid *fid, const char *name, const char *symname,
  175. kgid_t gid, struct p9_qid *qid);
  176. int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags, u32 mode,
  177. kgid_t gid, struct p9_qid *qid);
  178. int p9_client_clunk(struct p9_fid *fid);
  179. int p9_client_fsync(struct p9_fid *fid, int datasync);
  180. int p9_client_remove(struct p9_fid *fid);
  181. int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags);
  182. int p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err);
  183. int p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to,
  184. int *err);
  185. int p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err);
  186. int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset);
  187. int p9dirent_read(struct p9_client *clnt, char *buf, int len,
  188. struct p9_dirent *dirent);
  189. struct p9_wstat *p9_client_stat(struct p9_fid *fid);
  190. int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst);
  191. int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *attr);
  192. struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,
  193. u64 request_mask);
  194. int p9_client_mknod_dotl(struct p9_fid *oldfid, const char *name, int mode,
  195. dev_t rdev, kgid_t gid, struct p9_qid *qid);
  196. int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode,
  197. kgid_t gid, struct p9_qid *qid);
  198. int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status);
  199. int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *fl);
  200. void p9_fcall_fini(struct p9_fcall *fc);
  201. struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag);
  202. static inline void p9_req_get(struct p9_req_t *r)
  203. {
  204. refcount_inc(&r->refcount);
  205. }
  206. static inline int p9_req_try_get(struct p9_req_t *r)
  207. {
  208. return refcount_inc_not_zero(&r->refcount);
  209. }
  210. int p9_req_put(struct p9_client *c, struct p9_req_t *r);
  211. /* We cannot have the real tracepoints in header files,
  212. * use a wrapper function */
  213. DECLARE_TRACEPOINT(9p_fid_ref);
  214. void do_trace_9p_fid_get(struct p9_fid *fid);
  215. void do_trace_9p_fid_put(struct p9_fid *fid);
  216. /* fid reference counting helpers:
  217. * - fids used for any length of time should always be referenced through
  218. * p9_fid_get(), and released with p9_fid_put()
  219. * - v9fs_fid_lookup() or similar will automatically call get for you
  220. * and also require a put
  221. * - the *_fid_add() helpers will stash the fid in the inode,
  222. * at which point it is the responsibility of evict_inode()
  223. * to call the put
  224. * - the last put will automatically send a clunk to the server
  225. */
  226. static inline struct p9_fid *p9_fid_get(struct p9_fid *fid)
  227. {
  228. if (tracepoint_enabled(9p_fid_ref))
  229. do_trace_9p_fid_get(fid);
  230. refcount_inc(&fid->count);
  231. return fid;
  232. }
  233. static inline int p9_fid_put(struct p9_fid *fid)
  234. {
  235. if (!fid || IS_ERR(fid))
  236. return 0;
  237. if (tracepoint_enabled(9p_fid_ref))
  238. do_trace_9p_fid_put(fid);
  239. if (!refcount_dec_and_test(&fid->count))
  240. return 0;
  241. return p9_client_clunk(fid);
  242. }
  243. void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status);
  244. int p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type,
  245. int16_t *tag, int rewind);
  246. int p9stat_read(struct p9_client *clnt, char *buf, int len,
  247. struct p9_wstat *st);
  248. void p9stat_free(struct p9_wstat *stbuf);
  249. int p9_is_proto_dotu(struct p9_client *clnt);
  250. int p9_is_proto_dotl(struct p9_client *clnt);
  251. struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,
  252. const char *attr_name, u64 *attr_size);
  253. int p9_client_xattrcreate(struct p9_fid *fid, const char *name,
  254. u64 attr_size, int flags);
  255. int p9_client_readlink(struct p9_fid *fid, char **target);
  256. int p9_client_init(void);
  257. void p9_client_exit(void);
  258. #endif /* NET_9P_CLIENT_H */