xref: /openbmc/qemu/hw/9pfs/9p.h (revision 2149675b195f2d9a1a4e3b966d45aba234def69b)
12a6a4076SMarkus Armbruster #ifndef QEMU_9P_H
22a6a4076SMarkus Armbruster #define QEMU_9P_H
3ebe74f8bSWei Liu 
4ebe74f8bSWei Liu #include <dirent.h>
5ebe74f8bSWei Liu #include <utime.h>
6ebe74f8bSWei Liu #include <sys/resource.h>
7ebe74f8bSWei Liu #include "fsdev/file-op-9p.h"
8ebe74f8bSWei Liu #include "fsdev/9p-iov-marshal.h"
9ebe74f8bSWei Liu #include "qemu/thread.h"
10ebe74f8bSWei Liu #include "qemu/coroutine.h"
111a6ed33cSAntonios Motakis #include "qemu/qht.h"
12ebe74f8bSWei Liu 
13ebe74f8bSWei Liu enum {
14ebe74f8bSWei Liu     P9_TLERROR = 6,
15ebe74f8bSWei Liu     P9_RLERROR,
16ebe74f8bSWei Liu     P9_TSTATFS = 8,
17ebe74f8bSWei Liu     P9_RSTATFS,
18ebe74f8bSWei Liu     P9_TLOPEN = 12,
19ebe74f8bSWei Liu     P9_RLOPEN,
20ebe74f8bSWei Liu     P9_TLCREATE = 14,
21ebe74f8bSWei Liu     P9_RLCREATE,
22ebe74f8bSWei Liu     P9_TSYMLINK = 16,
23ebe74f8bSWei Liu     P9_RSYMLINK,
24ebe74f8bSWei Liu     P9_TMKNOD = 18,
25ebe74f8bSWei Liu     P9_RMKNOD,
26ebe74f8bSWei Liu     P9_TRENAME = 20,
27ebe74f8bSWei Liu     P9_RRENAME,
28ebe74f8bSWei Liu     P9_TREADLINK = 22,
29ebe74f8bSWei Liu     P9_RREADLINK,
30ebe74f8bSWei Liu     P9_TGETATTR = 24,
31ebe74f8bSWei Liu     P9_RGETATTR,
32ebe74f8bSWei Liu     P9_TSETATTR = 26,
33ebe74f8bSWei Liu     P9_RSETATTR,
34ebe74f8bSWei Liu     P9_TXATTRWALK = 30,
35ebe74f8bSWei Liu     P9_RXATTRWALK,
36ebe74f8bSWei Liu     P9_TXATTRCREATE = 32,
37ebe74f8bSWei Liu     P9_RXATTRCREATE,
38ebe74f8bSWei Liu     P9_TREADDIR = 40,
39ebe74f8bSWei Liu     P9_RREADDIR,
40ebe74f8bSWei Liu     P9_TFSYNC = 50,
41ebe74f8bSWei Liu     P9_RFSYNC,
42ebe74f8bSWei Liu     P9_TLOCK = 52,
43ebe74f8bSWei Liu     P9_RLOCK,
44ebe74f8bSWei Liu     P9_TGETLOCK = 54,
45ebe74f8bSWei Liu     P9_RGETLOCK,
46ebe74f8bSWei Liu     P9_TLINK = 70,
47ebe74f8bSWei Liu     P9_RLINK,
48ebe74f8bSWei Liu     P9_TMKDIR = 72,
49ebe74f8bSWei Liu     P9_RMKDIR,
50ebe74f8bSWei Liu     P9_TRENAMEAT = 74,
51ebe74f8bSWei Liu     P9_RRENAMEAT,
52ebe74f8bSWei Liu     P9_TUNLINKAT = 76,
53ebe74f8bSWei Liu     P9_RUNLINKAT,
54ebe74f8bSWei Liu     P9_TVERSION = 100,
55ebe74f8bSWei Liu     P9_RVERSION,
56ebe74f8bSWei Liu     P9_TAUTH = 102,
57ebe74f8bSWei Liu     P9_RAUTH,
58ebe74f8bSWei Liu     P9_TATTACH = 104,
59ebe74f8bSWei Liu     P9_RATTACH,
60ebe74f8bSWei Liu     P9_TERROR = 106,
61ebe74f8bSWei Liu     P9_RERROR,
62ebe74f8bSWei Liu     P9_TFLUSH = 108,
63ebe74f8bSWei Liu     P9_RFLUSH,
64ebe74f8bSWei Liu     P9_TWALK = 110,
65ebe74f8bSWei Liu     P9_RWALK,
66ebe74f8bSWei Liu     P9_TOPEN = 112,
67ebe74f8bSWei Liu     P9_ROPEN,
68ebe74f8bSWei Liu     P9_TCREATE = 114,
69ebe74f8bSWei Liu     P9_RCREATE,
70ebe74f8bSWei Liu     P9_TREAD = 116,
71ebe74f8bSWei Liu     P9_RREAD,
72ebe74f8bSWei Liu     P9_TWRITE = 118,
73ebe74f8bSWei Liu     P9_RWRITE,
74ebe74f8bSWei Liu     P9_TCLUNK = 120,
75ebe74f8bSWei Liu     P9_RCLUNK,
76ebe74f8bSWei Liu     P9_TREMOVE = 122,
77ebe74f8bSWei Liu     P9_RREMOVE,
78ebe74f8bSWei Liu     P9_TSTAT = 124,
79ebe74f8bSWei Liu     P9_RSTAT,
80ebe74f8bSWei Liu     P9_TWSTAT = 126,
81ebe74f8bSWei Liu     P9_RWSTAT,
82ebe74f8bSWei Liu };
83ebe74f8bSWei Liu 
84ebe74f8bSWei Liu 
85ebe74f8bSWei Liu /* qid.types */
86ebe74f8bSWei Liu enum {
87ebe74f8bSWei Liu     P9_QTDIR = 0x80,
88ebe74f8bSWei Liu     P9_QTAPPEND = 0x40,
89ebe74f8bSWei Liu     P9_QTEXCL = 0x20,
90ebe74f8bSWei Liu     P9_QTMOUNT = 0x10,
91ebe74f8bSWei Liu     P9_QTAUTH = 0x08,
92ebe74f8bSWei Liu     P9_QTTMP = 0x04,
93ebe74f8bSWei Liu     P9_QTSYMLINK = 0x02,
94ebe74f8bSWei Liu     P9_QTLINK = 0x01,
95ebe74f8bSWei Liu     P9_QTFILE = 0x00,
96ebe74f8bSWei Liu };
97ebe74f8bSWei Liu 
988e71b96cSGreg Kurz typedef enum P9ProtoVersion {
99ebe74f8bSWei Liu     V9FS_PROTO_2000U = 0x01,
100ebe74f8bSWei Liu     V9FS_PROTO_2000L = 0x02,
1018e71b96cSGreg Kurz } P9ProtoVersion;
102ebe74f8bSWei Liu 
103e16453a3SChristian Schoenebeck /**
104e16453a3SChristian Schoenebeck  * @brief Minimum message size supported by this 9pfs server.
105e16453a3SChristian Schoenebeck  *
106e16453a3SChristian Schoenebeck  * A client establishes a session by sending a Tversion request along with a
107e16453a3SChristian Schoenebeck  * 'msize' parameter which suggests the server a maximum message size ever to be
108e16453a3SChristian Schoenebeck  * used for communication (for both requests and replies) between client and
109e16453a3SChristian Schoenebeck  * server during that session. If client suggests a 'msize' smaller than this
110e16453a3SChristian Schoenebeck  * value then session is denied by server with an error response.
111e16453a3SChristian Schoenebeck  */
112e16453a3SChristian Schoenebeck #define P9_MIN_MSIZE    4096
113e16453a3SChristian Schoenebeck 
114baecbde6SGreg Kurz #define P9_NOTAG    UINT16_MAX
115baecbde6SGreg Kurz #define P9_NOFID    UINT32_MAX
116ebe74f8bSWei Liu #define P9_MAXWELEM 16
117ebe74f8bSWei Liu 
118ebe74f8bSWei Liu #define FID_REFERENCED          0x1
119ebe74f8bSWei Liu #define FID_NON_RECLAIMABLE     0x2
120ebe74f8bSWei Liu static inline char *rpath(FsContext *ctx, const char *path)
121ebe74f8bSWei Liu {
122ebe74f8bSWei Liu     return g_strdup_printf("%s/%s", ctx->fs_root, path);
123ebe74f8bSWei Liu }
124ebe74f8bSWei Liu 
125ebe74f8bSWei Liu /*
126ebe74f8bSWei Liu  * ample room for Twrite/Rread header
127ebe74f8bSWei Liu  * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
128ebe74f8bSWei Liu  */
129ebe74f8bSWei Liu #define P9_IOHDRSZ 24
130ebe74f8bSWei Liu 
131ebe74f8bSWei Liu typedef struct V9fsPDU V9fsPDU;
13221cf9edfSGreg Kurz typedef struct V9fsState V9fsState;
1338e71b96cSGreg Kurz typedef struct V9fsTransport V9fsTransport;
134ebe74f8bSWei Liu 
135c9fb47e7SStefano Stabellini typedef struct {
136c9fb47e7SStefano Stabellini     uint32_t size_le;
137c9fb47e7SStefano Stabellini     uint8_t id;
138c9fb47e7SStefano Stabellini     uint16_t tag_le;
139c9fb47e7SStefano Stabellini } QEMU_PACKED P9MsgHeader;
140a4d99854SGreg Kurz /* According to the specification, 9p messages start with a 7-byte header.
141a4d99854SGreg Kurz  * Since most of the code uses this header size in literal form, we must be
142a4d99854SGreg Kurz  * sure this is indeed the case.
143a4d99854SGreg Kurz  */
144a4d99854SGreg Kurz QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader) != 7);
145c9fb47e7SStefano Stabellini 
146ebe74f8bSWei Liu struct V9fsPDU
147ebe74f8bSWei Liu {
148ebe74f8bSWei Liu     uint32_t size;
149ebe74f8bSWei Liu     uint16_t tag;
150ebe74f8bSWei Liu     uint8_t id;
151ebe74f8bSWei Liu     uint8_t cancelled;
152ebe74f8bSWei Liu     CoQueue complete;
15321cf9edfSGreg Kurz     V9fsState *s;
154ebe74f8bSWei Liu     QLIST_ENTRY(V9fsPDU) next;
15500588a0aSWei Liu     uint32_t idx;
156ebe74f8bSWei Liu };
157ebe74f8bSWei Liu 
158ebe74f8bSWei Liu 
159ebe74f8bSWei Liu /* FIXME
160ebe74f8bSWei Liu  * 1) change user needs to set groups and stuff
161ebe74f8bSWei Liu  */
162ebe74f8bSWei Liu 
163ebe74f8bSWei Liu #define MAX_REQ         128
164ebe74f8bSWei Liu #define MAX_TAG_LEN     32
165ebe74f8bSWei Liu 
166ebe74f8bSWei Liu #define BUG_ON(cond) assert(!(cond))
167ebe74f8bSWei Liu 
168ebe74f8bSWei Liu typedef struct V9fsFidState V9fsFidState;
169ebe74f8bSWei Liu 
170ebe74f8bSWei Liu enum {
171ebe74f8bSWei Liu     P9_FID_NONE = 0,
172ebe74f8bSWei Liu     P9_FID_FILE,
173ebe74f8bSWei Liu     P9_FID_DIR,
174ebe74f8bSWei Liu     P9_FID_XATTR,
175ebe74f8bSWei Liu };
176ebe74f8bSWei Liu 
177ebe74f8bSWei Liu typedef struct V9fsConf
178ebe74f8bSWei Liu {
179ebe74f8bSWei Liu     /* tag name for the device */
180ebe74f8bSWei Liu     char *tag;
181ebe74f8bSWei Liu     char *fsdev_id;
182ebe74f8bSWei Liu } V9fsConf;
183ebe74f8bSWei Liu 
184aca6897fSKeno Fischer /* 9p2000.L xattr flags (matches Linux values) */
185aca6897fSKeno Fischer #define P9_XATTR_CREATE 1
186aca6897fSKeno Fischer #define P9_XATTR_REPLACE 2
187aca6897fSKeno Fischer 
188ebe74f8bSWei Liu typedef struct V9fsXattr
189ebe74f8bSWei Liu {
1908495f9adSLi Qiang     uint64_t copied_len;
1918495f9adSLi Qiang     uint64_t len;
192ebe74f8bSWei Liu     void *value;
193ebe74f8bSWei Liu     V9fsString name;
194ebe74f8bSWei Liu     int flags;
195dd28fbbcSLi Qiang     bool xattrwalk_fid;
196ebe74f8bSWei Liu } V9fsXattr;
197ebe74f8bSWei Liu 
198f314ea4eSGreg Kurz typedef struct V9fsDir {
199f314ea4eSGreg Kurz     DIR *stream;
200ed463454SGreg Kurz     CoMutex readdir_mutex;
201f314ea4eSGreg Kurz } V9fsDir;
202f314ea4eSGreg Kurz 
2037cde47d4SGreg Kurz static inline void v9fs_readdir_lock(V9fsDir *dir)
2047cde47d4SGreg Kurz {
205ed463454SGreg Kurz     qemu_co_mutex_lock(&dir->readdir_mutex);
2067cde47d4SGreg Kurz }
2077cde47d4SGreg Kurz 
2087cde47d4SGreg Kurz static inline void v9fs_readdir_unlock(V9fsDir *dir)
2097cde47d4SGreg Kurz {
210ed463454SGreg Kurz     qemu_co_mutex_unlock(&dir->readdir_mutex);
2117cde47d4SGreg Kurz }
2127cde47d4SGreg Kurz 
2137cde47d4SGreg Kurz static inline void v9fs_readdir_init(V9fsDir *dir)
2147cde47d4SGreg Kurz {
215ed463454SGreg Kurz     qemu_co_mutex_init(&dir->readdir_mutex);
2167cde47d4SGreg Kurz }
2177cde47d4SGreg Kurz 
218*2149675bSChristian Schoenebeck /**
219*2149675bSChristian Schoenebeck  * Type for 9p fs drivers' (a.k.a. 9p backends) result of readdir requests,
220*2149675bSChristian Schoenebeck  * which is a chained list of directory entries.
221*2149675bSChristian Schoenebeck  */
222*2149675bSChristian Schoenebeck typedef struct V9fsDirEnt {
223*2149675bSChristian Schoenebeck     /* mandatory (must not be NULL) information for all readdir requests */
224*2149675bSChristian Schoenebeck     struct dirent *dent;
225*2149675bSChristian Schoenebeck     /*
226*2149675bSChristian Schoenebeck      * optional (may be NULL): A full stat of each directory entry is just
227*2149675bSChristian Schoenebeck      * done if explicitly told to fs driver.
228*2149675bSChristian Schoenebeck      */
229*2149675bSChristian Schoenebeck     struct stat *st;
230*2149675bSChristian Schoenebeck     /*
231*2149675bSChristian Schoenebeck      * instead of an array, directory entries are always returned as
232*2149675bSChristian Schoenebeck      * chained list, that's because the amount of entries retrieved by fs
233*2149675bSChristian Schoenebeck      * drivers is dependent on the individual entries' name (since response
234*2149675bSChristian Schoenebeck      * messages are size limited), so the final amount cannot be estimated
235*2149675bSChristian Schoenebeck      * before hand
236*2149675bSChristian Schoenebeck      */
237*2149675bSChristian Schoenebeck     struct V9fsDirEnt *next;
238*2149675bSChristian Schoenebeck } V9fsDirEnt;
239*2149675bSChristian Schoenebeck 
240ebe74f8bSWei Liu /*
241ebe74f8bSWei Liu  * Filled by fs driver on open and other
242ebe74f8bSWei Liu  * calls.
243ebe74f8bSWei Liu  */
244ebe74f8bSWei Liu union V9fsFidOpenState {
245ebe74f8bSWei Liu     int fd;
246f314ea4eSGreg Kurz     V9fsDir dir;
247ebe74f8bSWei Liu     V9fsXattr xattr;
248ebe74f8bSWei Liu     /*
249ebe74f8bSWei Liu      * private pointer for fs drivers, that
250ebe74f8bSWei Liu      * have its own internal representation of
251ebe74f8bSWei Liu      * open files.
252ebe74f8bSWei Liu      */
253ebe74f8bSWei Liu     void *private;
254ebe74f8bSWei Liu };
255ebe74f8bSWei Liu 
256ebe74f8bSWei Liu struct V9fsFidState
257ebe74f8bSWei Liu {
258ebe74f8bSWei Liu     int fid_type;
259ebe74f8bSWei Liu     int32_t fid;
260ebe74f8bSWei Liu     V9fsPath path;
261ebe74f8bSWei Liu     V9fsFidOpenState fs;
262ebe74f8bSWei Liu     V9fsFidOpenState fs_reclaim;
263ebe74f8bSWei Liu     int flags;
264ebe74f8bSWei Liu     int open_flags;
265ebe74f8bSWei Liu     uid_t uid;
266ebe74f8bSWei Liu     int ref;
267ebe74f8bSWei Liu     int clunked;
268ebe74f8bSWei Liu     V9fsFidState *next;
269ebe74f8bSWei Liu     V9fsFidState *rclm_lst;
270ebe74f8bSWei Liu };
271ebe74f8bSWei Liu 
2726b6aa828SChristian Schoenebeck typedef enum AffixType_t {
2736b6aa828SChristian Schoenebeck     AffixType_Prefix,
2746b6aa828SChristian Schoenebeck     AffixType_Suffix, /* A.k.a. postfix. */
2756b6aa828SChristian Schoenebeck } AffixType_t;
2766b6aa828SChristian Schoenebeck 
2776b6aa828SChristian Schoenebeck /**
2786b6aa828SChristian Schoenebeck  * @brief Unique affix of variable length.
2796b6aa828SChristian Schoenebeck  *
2806b6aa828SChristian Schoenebeck  * An affix is (currently) either a suffix or a prefix, which is either
2816b6aa828SChristian Schoenebeck  * going to be prepended (prefix) or appended (suffix) with some other
2826b6aa828SChristian Schoenebeck  * number for the goal to generate unique numbers. Accordingly the
2836b6aa828SChristian Schoenebeck  * suffixes (or prefixes) we generate @b must all have the mathematical
2846b6aa828SChristian Schoenebeck  * property of being suffix-free (or prefix-free in case of prefixes)
2856b6aa828SChristian Schoenebeck  * so that no matter what number we concatenate the affix with, that we
2866b6aa828SChristian Schoenebeck  * always reliably get unique numbers as result after concatenation.
2876b6aa828SChristian Schoenebeck  */
2886b6aa828SChristian Schoenebeck typedef struct VariLenAffix {
2896b6aa828SChristian Schoenebeck     AffixType_t type; /* Whether this affix is a suffix or a prefix. */
2906b6aa828SChristian Schoenebeck     uint64_t value; /* Actual numerical value of this affix. */
2916b6aa828SChristian Schoenebeck     /*
2926b6aa828SChristian Schoenebeck      * Lenght of the affix, that is how many (of the lowest) bits of @c value
2936b6aa828SChristian Schoenebeck      * must be used for appending/prepending this affix to its final resulting,
2946b6aa828SChristian Schoenebeck      * unique number.
2956b6aa828SChristian Schoenebeck      */
2966b6aa828SChristian Schoenebeck     int bits;
2976b6aa828SChristian Schoenebeck } VariLenAffix;
2986b6aa828SChristian Schoenebeck 
2996b6aa828SChristian Schoenebeck /* See qid_inode_prefix_hash_bits(). */
3006b6aa828SChristian Schoenebeck typedef struct {
3016b6aa828SChristian Schoenebeck     dev_t dev; /* FS device on host. */
3026b6aa828SChristian Schoenebeck     /*
3036b6aa828SChristian Schoenebeck      * How many (high) bits of the original inode number shall be used for
3046b6aa828SChristian Schoenebeck      * hashing.
3056b6aa828SChristian Schoenebeck      */
3066b6aa828SChristian Schoenebeck     int prefix_bits;
3076b6aa828SChristian Schoenebeck } QpdEntry;
3081a6ed33cSAntonios Motakis 
3091a6ed33cSAntonios Motakis /* QID path prefix entry, see stat_to_qid */
3101a6ed33cSAntonios Motakis typedef struct {
3111a6ed33cSAntonios Motakis     dev_t dev;
3121a6ed33cSAntonios Motakis     uint16_t ino_prefix;
3136b6aa828SChristian Schoenebeck     uint32_t qp_affix_index;
3146b6aa828SChristian Schoenebeck     VariLenAffix qp_affix;
3151a6ed33cSAntonios Motakis } QppEntry;
3161a6ed33cSAntonios Motakis 
317f3fe4a2dSAntonios Motakis /* QID path full entry, as above */
318f3fe4a2dSAntonios Motakis typedef struct {
319f3fe4a2dSAntonios Motakis     dev_t dev;
320f3fe4a2dSAntonios Motakis     ino_t ino;
321f3fe4a2dSAntonios Motakis     uint64_t path;
322f3fe4a2dSAntonios Motakis } QpfEntry;
323f3fe4a2dSAntonios Motakis 
32421cf9edfSGreg Kurz struct V9fsState
325ebe74f8bSWei Liu {
326ebe74f8bSWei Liu     QLIST_HEAD(, V9fsPDU) free_list;
327ebe74f8bSWei Liu     QLIST_HEAD(, V9fsPDU) active_list;
328ebe74f8bSWei Liu     V9fsFidState *fid_list;
329ebe74f8bSWei Liu     FileOperations *ops;
330ebe74f8bSWei Liu     FsContext ctx;
331ebe74f8bSWei Liu     char *tag;
3328e71b96cSGreg Kurz     P9ProtoVersion proto_version;
333ebe74f8bSWei Liu     int32_t msize;
334583f21f8SStefano Stabellini     V9fsPDU pdus[MAX_REQ];
3358e71b96cSGreg Kurz     const V9fsTransport *transport;
336ebe74f8bSWei Liu     /*
337ebe74f8bSWei Liu      * lock ensuring atomic path update
338ebe74f8bSWei Liu      * on rename.
339ebe74f8bSWei Liu      */
340ebe74f8bSWei Liu     CoRwlock rename_lock;
341ebe74f8bSWei Liu     int32_t root_fid;
342ebe74f8bSWei Liu     Error *migration_blocker;
343ebe74f8bSWei Liu     V9fsConf fsconf;
34456f101ecSGreg Kurz     V9fsQID root_qid;
3453b5ee9e8SAntonios Motakis     dev_t dev_id;
3466b6aa828SChristian Schoenebeck     struct qht qpd_table;
3471a6ed33cSAntonios Motakis     struct qht qpp_table;
348f3fe4a2dSAntonios Motakis     struct qht qpf_table;
3496b6aa828SChristian Schoenebeck     uint64_t qp_ndevices; /* Amount of entries in qpd_table. */
3506b6aa828SChristian Schoenebeck     uint16_t qp_affix_next;
351f3fe4a2dSAntonios Motakis     uint64_t qp_fullpath_next;
35221cf9edfSGreg Kurz };
353ebe74f8bSWei Liu 
354ebe74f8bSWei Liu /* 9p2000.L open flags */
355ebe74f8bSWei Liu #define P9_DOTL_RDONLY        00000000
356ebe74f8bSWei Liu #define P9_DOTL_WRONLY        00000001
357ebe74f8bSWei Liu #define P9_DOTL_RDWR          00000002
358ebe74f8bSWei Liu #define P9_DOTL_NOACCESS      00000003
359ebe74f8bSWei Liu #define P9_DOTL_CREATE        00000100
360ebe74f8bSWei Liu #define P9_DOTL_EXCL          00000200
361ebe74f8bSWei Liu #define P9_DOTL_NOCTTY        00000400
362ebe74f8bSWei Liu #define P9_DOTL_TRUNC         00001000
363ebe74f8bSWei Liu #define P9_DOTL_APPEND        00002000
364ebe74f8bSWei Liu #define P9_DOTL_NONBLOCK      00004000
365ebe74f8bSWei Liu #define P9_DOTL_DSYNC         00010000
366ebe74f8bSWei Liu #define P9_DOTL_FASYNC        00020000
367ebe74f8bSWei Liu #define P9_DOTL_DIRECT        00040000
368ebe74f8bSWei Liu #define P9_DOTL_LARGEFILE     00100000
369ebe74f8bSWei Liu #define P9_DOTL_DIRECTORY     00200000
370ebe74f8bSWei Liu #define P9_DOTL_NOFOLLOW      00400000
371ebe74f8bSWei Liu #define P9_DOTL_NOATIME       01000000
372ebe74f8bSWei Liu #define P9_DOTL_CLOEXEC       02000000
373ebe74f8bSWei Liu #define P9_DOTL_SYNC          04000000
374ebe74f8bSWei Liu 
375ebe74f8bSWei Liu /* 9p2000.L at flags */
376ebe74f8bSWei Liu #define P9_DOTL_AT_REMOVEDIR         0x200
377ebe74f8bSWei Liu 
378ebe74f8bSWei Liu /* 9P2000.L lock type */
379ebe74f8bSWei Liu #define P9_LOCK_TYPE_RDLCK 0
380ebe74f8bSWei Liu #define P9_LOCK_TYPE_WRLCK 1
381ebe74f8bSWei Liu #define P9_LOCK_TYPE_UNLCK 2
382ebe74f8bSWei Liu 
383ebe74f8bSWei Liu #define P9_LOCK_SUCCESS 0
384ebe74f8bSWei Liu #define P9_LOCK_BLOCKED 1
385ebe74f8bSWei Liu #define P9_LOCK_ERROR 2
386ebe74f8bSWei Liu #define P9_LOCK_GRACE 3
387ebe74f8bSWei Liu 
388ebe74f8bSWei Liu #define P9_LOCK_FLAGS_BLOCK 1
389ebe74f8bSWei Liu #define P9_LOCK_FLAGS_RECLAIM 2
390ebe74f8bSWei Liu 
391ebe74f8bSWei Liu typedef struct V9fsFlock
392ebe74f8bSWei Liu {
393ebe74f8bSWei Liu     uint8_t type;
394ebe74f8bSWei Liu     uint32_t flags;
395ebe74f8bSWei Liu     uint64_t start; /* absolute offset */
396ebe74f8bSWei Liu     uint64_t length;
397ebe74f8bSWei Liu     uint32_t proc_id;
398ebe74f8bSWei Liu     V9fsString client_id;
399ebe74f8bSWei Liu } V9fsFlock;
400ebe74f8bSWei Liu 
401ebe74f8bSWei Liu typedef struct V9fsGetlock
402ebe74f8bSWei Liu {
403ebe74f8bSWei Liu     uint8_t type;
404ebe74f8bSWei Liu     uint64_t start; /* absolute offset */
405ebe74f8bSWei Liu     uint64_t length;
406ebe74f8bSWei Liu     uint32_t proc_id;
407ebe74f8bSWei Liu     V9fsString client_id;
408ebe74f8bSWei Liu } V9fsGetlock;
409ebe74f8bSWei Liu 
410ebe74f8bSWei Liu extern int open_fd_hw;
411ebe74f8bSWei Liu extern int total_open_fd;
412ebe74f8bSWei Liu 
413ebe74f8bSWei Liu static inline void v9fs_path_write_lock(V9fsState *s)
414ebe74f8bSWei Liu {
415ebe74f8bSWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
416ebe74f8bSWei Liu         qemu_co_rwlock_wrlock(&s->rename_lock);
417ebe74f8bSWei Liu     }
418ebe74f8bSWei Liu }
419ebe74f8bSWei Liu 
420ebe74f8bSWei Liu static inline void v9fs_path_read_lock(V9fsState *s)
421ebe74f8bSWei Liu {
422ebe74f8bSWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
423ebe74f8bSWei Liu         qemu_co_rwlock_rdlock(&s->rename_lock);
424ebe74f8bSWei Liu     }
425ebe74f8bSWei Liu }
426ebe74f8bSWei Liu 
427ebe74f8bSWei Liu static inline void v9fs_path_unlock(V9fsState *s)
428ebe74f8bSWei Liu {
429ebe74f8bSWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
430ebe74f8bSWei Liu         qemu_co_rwlock_unlock(&s->rename_lock);
431ebe74f8bSWei Liu     }
432ebe74f8bSWei Liu }
433ebe74f8bSWei Liu 
434ebe74f8bSWei Liu static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
435ebe74f8bSWei Liu {
436ebe74f8bSWei Liu     return pdu->cancelled;
437ebe74f8bSWei Liu }
438ebe74f8bSWei Liu 
4398440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu);
440bc70a592SGreg Kurz void v9fs_path_init(V9fsPath *path);
441bc70a592SGreg Kurz void v9fs_path_free(V9fsPath *path);
442bc70a592SGreg Kurz void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
443e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src);
44429c9d2caSChristian Schoenebeck size_t v9fs_readdir_response_size(V9fsString *name);
445bc70a592SGreg Kurz int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
446ebe74f8bSWei Liu                       const char *name, V9fsPath *path);
447066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
448066eb006SGreg Kurz                                Error **errp);
449b69c3c21SMarkus Armbruster void v9fs_device_unrealize_common(V9fsState *s);
450ebe74f8bSWei Liu 
4514b311c5fSWei Liu V9fsPDU *pdu_alloc(V9fsState *s);
4524b311c5fSWei Liu void pdu_free(V9fsPDU *pdu);
453506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr);
4540e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s);
455ebe74f8bSWei Liu 
456ea83441cSStefano Stabellini struct V9fsTransport {
457ea83441cSStefano Stabellini     ssize_t     (*pdu_vmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
458ea83441cSStefano Stabellini                                 va_list ap);
459ea83441cSStefano Stabellini     ssize_t     (*pdu_vunmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
460ea83441cSStefano Stabellini                                   va_list ap);
46188da0b03SStefano Stabellini     void        (*init_in_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
462cf45183bSStefano Stabellini                                         unsigned int *pniov, size_t size);
46388da0b03SStefano Stabellini     void        (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
4648d37de41SGreg Kurz                                          unsigned int *pniov, size_t size);
465ea83441cSStefano Stabellini     void        (*push_and_notify)(V9fsPDU *pdu);
466ea83441cSStefano Stabellini };
467ea83441cSStefano Stabellini 
468ebe74f8bSWei Liu #endif
469