/* SPDX-License-Identifier: GPL-2.0-only */1/*2* 9P Client Definitions3*4* Copyright (C) 2008 by Eric Van Hensbergen <[email protected]>5* Copyright (C) 2007 by Latchesar Ionkov <[email protected]>6*/78#ifndef NET_9P_CLIENT_H9#define NET_9P_CLIENT_H1011#include <linux/utsname.h>12#include <linux/idr.h>13#include <linux/tracepoint-defs.h>1415/* Number of requests per row */16#define P9_ROW_MAXTAG 2551718/* DEFAULT MSIZE = 32 pages worth of payload + P9_HDRSZ +19* room for write (16 extra) or read (11 extra) operands.20*/2122#define DEFAULT_MSIZE ((128 * 1024) + P9_IOHDRSZ)2324/** enum p9_proto_versions - 9P protocol versions25* @p9_proto_legacy: 9P Legacy mode, pre-9P2000.u26* @p9_proto_2000u: 9P2000.u extension27* @p9_proto_2000L: 9P2000.L extension28*/2930enum p9_proto_versions {31p9_proto_legacy,32p9_proto_2000u,33p9_proto_2000L,34};353637/**38* enum p9_trans_status - different states of underlying transports39* @Connected: transport is connected and healthy40* @Disconnected: transport has been disconnected41* @Hung: transport is connected by wedged42*43* This enumeration details the various states a transport44* instatiation can be in.45*/4647enum p9_trans_status {48Connected,49BeginDisconnect,50Disconnected,51Hung,52};5354/**55* enum p9_req_status_t - status of a request56* @REQ_STATUS_ALLOC: request has been allocated but not sent57* @REQ_STATUS_UNSENT: request waiting to be sent58* @REQ_STATUS_SENT: request sent to server59* @REQ_STATUS_RCVD: response received from server60* @REQ_STATUS_FLSHD: request has been flushed61* @REQ_STATUS_ERROR: request encountered an error on the client side62*/6364enum p9_req_status_t {65REQ_STATUS_ALLOC,66REQ_STATUS_UNSENT,67REQ_STATUS_SENT,68REQ_STATUS_RCVD,69REQ_STATUS_FLSHD,70REQ_STATUS_ERROR,71};7273/**74* struct p9_req_t - request slots75* @status: status of this request slot76* @t_err: transport error77* @wq: wait_queue for the client to block on for this request78* @tc: the request fcall structure79* @rc: the response fcall structure80* @req_list: link for higher level objects to chain requests81*/82struct p9_req_t {83int status;84int t_err;85refcount_t refcount;86wait_queue_head_t wq;87struct p9_fcall tc;88struct p9_fcall rc;89struct list_head req_list;90};9192/**93* struct p9_client - per client instance state94* @lock: protect @fids and @reqs95* @msize: maximum data size negotiated by protocol96* @proto_version: 9P protocol version to use97* @trans_mod: module API instantiated with this client98* @status: connection state99* @trans: tranport instance state and API100* @fids: All active FID handles101* @reqs: All active requests.102* @name: node name used as client id103*104* The client structure is used to keep track of various per-client105* state that has been instantiated.106*/107struct p9_client {108spinlock_t lock;109unsigned int msize;110unsigned char proto_version;111struct p9_trans_module *trans_mod;112enum p9_trans_status status;113void *trans;114struct kmem_cache *fcall_cache;115116union {117struct {118int rfd;119int wfd;120} fd;121struct {122u16 port;123bool privport;124125} tcp;126} trans_opts;127128struct idr fids;129struct idr reqs;130131char name[__NEW_UTS_LEN + 1];132};133134/**135* struct p9_fd_opts - holds client options during parsing136* @msize: maximum data size negotiated by protocol137* @prot-Oversion: 9P protocol version to use138* @trans_mod: module API instantiated with this client139*140* These parsed options get transferred into client in141* apply_client_options()142*/143struct p9_client_opts {144unsigned int msize;145unsigned char proto_version;146struct p9_trans_module *trans_mod;147};148149/**150* struct p9_fd_opts - per-transport options for fd transport151* @rfd: file descriptor for reading (trans=fd)152* @wfd: file descriptor for writing (trans=fd)153* @port: port to connect to (trans=tcp)154* @privport: port is privileged155*/156struct p9_fd_opts {157int rfd;158int wfd;159u16 port;160bool privport;161};162163/**164* struct p9_rdma_opts - Collection of mount options for rdma transport165* @port: port of connection166* @privport: Whether a privileged port may be used167* @sq_depth: The requested depth of the SQ. This really doesn't need168* to be any deeper than the number of threads used in the client169* @rq_depth: The depth of the RQ. Should be greater than or equal to SQ depth170* @timeout: Time to wait in msecs for CM events171*/172struct p9_rdma_opts {173short port;174bool privport;175int sq_depth;176int rq_depth;177long timeout;178};179180/**181* struct p9_session_opts - holds parsed options for v9fs_session_info182* @flags: session options of type &p9_session_flags183* @nodev: set to 1 to disable device mapping184* @debug: debug level185* @afid: authentication handle186* @cache: cache mode of type &p9_cache_bits187* @cachetag: the tag of the cache associated with this session188* @uname: string user name to mount hierarchy as189* @aname: mount specifier for remote hierarchy190* @dfltuid: default numeric userid to mount hierarchy as191* @dfltgid: default numeric groupid to mount hierarchy as192* @uid: if %V9FS_ACCESS_SINGLE, the numeric uid which mounted the hierarchy193* @session_lock_timeout: retry interval for blocking locks194*195* This strucure holds options which are parsed and will be transferred196* to the v9fs_session_info structure when mounted, and therefore largely197* duplicates struct v9fs_session_info.198*/199struct p9_session_opts {200unsigned int flags;201unsigned char nodev;202unsigned short debug;203unsigned int afid;204unsigned int cache;205#ifdef CONFIG_9P_FSCACHE206char *cachetag;207#endif208char *uname;209char *aname;210kuid_t dfltuid;211kgid_t dfltgid;212kuid_t uid;213long session_lock_timeout;214};215216/* Used by mount API to store parsed mount options */217struct v9fs_context {218struct p9_client_opts client_opts;219struct p9_fd_opts fd_opts;220struct p9_rdma_opts rdma_opts;221struct p9_session_opts session_opts;222};223224/**225* struct p9_fid - file system entity handle226* @clnt: back pointer to instantiating &p9_client227* @fid: numeric identifier for this handle228* @mode: current mode of this fid (enum?)229* @qid: the &p9_qid server identifier this handle points to230* @iounit: the server reported maximum transaction size for this file231* @uid: the numeric uid of the local user who owns this handle232* @rdir: readdir accounting structure (allocated on demand)233* @dlist: per-dentry fid tracking234*235* TODO: This needs lots of explanation.236*/237enum fid_source {238FID_FROM_OTHER,239FID_FROM_INODE,240FID_FROM_DENTRY,241};242243struct p9_fid {244struct p9_client *clnt;245u32 fid;246refcount_t count;247int mode;248struct p9_qid qid;249u32 iounit;250kuid_t uid;251252void *rdir;253254struct hlist_node dlist; /* list of all fids attached to a dentry */255struct hlist_node ilist;256};257258/**259* struct p9_dirent - directory entry structure260* @qid: The p9 server qid for this dirent261* @d_off: offset to the next dirent262* @d_type: type of file263* @d_name: file name264*/265266struct p9_dirent {267struct p9_qid qid;268u64 d_off;269unsigned char d_type;270char d_name[256];271};272273struct iov_iter;274275int p9_show_client_options(struct seq_file *m, struct p9_client *clnt);276int p9_client_statfs(struct p9_fid *fid, struct p9_rstatfs *sb);277int p9_client_rename(struct p9_fid *fid, struct p9_fid *newdirfid,278const char *name);279int p9_client_renameat(struct p9_fid *olddirfid, const char *old_name,280struct p9_fid *newdirfid, const char *new_name);281struct p9_client *p9_client_create(struct fs_context *fc);282void p9_client_destroy(struct p9_client *clnt);283void p9_client_disconnect(struct p9_client *clnt);284void p9_client_begin_disconnect(struct p9_client *clnt);285struct p9_fid *p9_client_attach(struct p9_client *clnt, struct p9_fid *afid,286const char *uname, kuid_t n_uname, const char *aname);287struct p9_fid *p9_client_walk(struct p9_fid *oldfid, uint16_t nwname,288const unsigned char * const *wnames, int clone);289int p9_client_open(struct p9_fid *fid, int mode);290int p9_client_fcreate(struct p9_fid *fid, const char *name, u32 perm, int mode,291char *extension);292int p9_client_link(struct p9_fid *fid, struct p9_fid *oldfid, const char *newname);293int p9_client_symlink(struct p9_fid *fid, const char *name, const char *symname,294kgid_t gid, struct p9_qid *qid);295int p9_client_create_dotl(struct p9_fid *ofid, const char *name, u32 flags, u32 mode,296kgid_t gid, struct p9_qid *qid);297int p9_client_clunk(struct p9_fid *fid);298int p9_client_fsync(struct p9_fid *fid, int datasync);299int p9_client_remove(struct p9_fid *fid);300int p9_client_unlinkat(struct p9_fid *dfid, const char *name, int flags);301int p9_client_read(struct p9_fid *fid, u64 offset, struct iov_iter *to, int *err);302int p9_client_read_once(struct p9_fid *fid, u64 offset, struct iov_iter *to,303int *err);304int p9_client_write(struct p9_fid *fid, u64 offset, struct iov_iter *from, int *err);305struct netfs_io_subrequest;306void p9_client_write_subreq(struct netfs_io_subrequest *subreq);307int p9_client_readdir(struct p9_fid *fid, char *data, u32 count, u64 offset);308int p9dirent_read(struct p9_client *clnt, char *buf, int len,309struct p9_dirent *dirent);310struct p9_wstat *p9_client_stat(struct p9_fid *fid);311int p9_client_wstat(struct p9_fid *fid, struct p9_wstat *wst);312int p9_client_setattr(struct p9_fid *fid, struct p9_iattr_dotl *attr);313314struct p9_stat_dotl *p9_client_getattr_dotl(struct p9_fid *fid,315u64 request_mask);316317int p9_client_mknod_dotl(struct p9_fid *oldfid, const char *name, int mode,318dev_t rdev, kgid_t gid, struct p9_qid *qid);319int p9_client_mkdir_dotl(struct p9_fid *fid, const char *name, int mode,320kgid_t gid, struct p9_qid *qid);321int p9_client_lock_dotl(struct p9_fid *fid, struct p9_flock *flock, u8 *status);322int p9_client_getlock_dotl(struct p9_fid *fid, struct p9_getlock *fl);323void p9_fcall_fini(struct p9_fcall *fc);324struct p9_req_t *p9_tag_lookup(struct p9_client *c, u16 tag);325326static inline void p9_req_get(struct p9_req_t *r)327{328refcount_inc(&r->refcount);329}330331static inline int p9_req_try_get(struct p9_req_t *r)332{333return refcount_inc_not_zero(&r->refcount);334}335336int p9_req_put(struct p9_client *c, struct p9_req_t *r);337338/* We cannot have the real tracepoints in header files,339* use a wrapper function */340DECLARE_TRACEPOINT(9p_fid_ref);341void do_trace_9p_fid_get(struct p9_fid *fid);342void do_trace_9p_fid_put(struct p9_fid *fid);343344/* fid reference counting helpers:345* - fids used for any length of time should always be referenced through346* p9_fid_get(), and released with p9_fid_put()347* - v9fs_fid_lookup() or similar will automatically call get for you348* and also require a put349* - the *_fid_add() helpers will stash the fid in the inode,350* at which point it is the responsibility of evict_inode()351* to call the put352* - the last put will automatically send a clunk to the server353*/354static inline struct p9_fid *p9_fid_get(struct p9_fid *fid)355{356if (tracepoint_enabled(9p_fid_ref))357do_trace_9p_fid_get(fid);358359refcount_inc(&fid->count);360361return fid;362}363364static inline int p9_fid_put(struct p9_fid *fid)365{366if (!fid || IS_ERR(fid))367return 0;368369if (tracepoint_enabled(9p_fid_ref))370do_trace_9p_fid_put(fid);371372if (!refcount_dec_and_test(&fid->count))373return 0;374375return p9_client_clunk(fid);376}377378void p9_client_cb(struct p9_client *c, struct p9_req_t *req, int status);379380int p9_parse_header(struct p9_fcall *pdu, int32_t *size, int8_t *type,381int16_t *tag, int rewind);382int p9stat_read(struct p9_client *clnt, char *buf, int len,383struct p9_wstat *st);384void p9stat_free(struct p9_wstat *stbuf);385386int p9_is_proto_dotu(struct p9_client *clnt);387int p9_is_proto_dotl(struct p9_client *clnt);388struct p9_fid *p9_client_xattrwalk(struct p9_fid *file_fid,389const char *attr_name, u64 *attr_size);390int p9_client_xattrcreate(struct p9_fid *fid, const char *name,391u64 attr_size, int flags);392int p9_client_readlink(struct p9_fid *fid, char **target);393394int p9_client_init(void);395void p9_client_exit(void);396397#endif /* NET_9P_CLIENT_H */398399400