xref: /openbmc/qemu/hw/9pfs/9p.h (revision 3b5ee9e86b68fff5db5f68499311239485776ea9)
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"
11ebe74f8bSWei Liu 
12ebe74f8bSWei Liu enum {
13ebe74f8bSWei Liu     P9_TLERROR = 6,
14ebe74f8bSWei Liu     P9_RLERROR,
15ebe74f8bSWei Liu     P9_TSTATFS = 8,
16ebe74f8bSWei Liu     P9_RSTATFS,
17ebe74f8bSWei Liu     P9_TLOPEN = 12,
18ebe74f8bSWei Liu     P9_RLOPEN,
19ebe74f8bSWei Liu     P9_TLCREATE = 14,
20ebe74f8bSWei Liu     P9_RLCREATE,
21ebe74f8bSWei Liu     P9_TSYMLINK = 16,
22ebe74f8bSWei Liu     P9_RSYMLINK,
23ebe74f8bSWei Liu     P9_TMKNOD = 18,
24ebe74f8bSWei Liu     P9_RMKNOD,
25ebe74f8bSWei Liu     P9_TRENAME = 20,
26ebe74f8bSWei Liu     P9_RRENAME,
27ebe74f8bSWei Liu     P9_TREADLINK = 22,
28ebe74f8bSWei Liu     P9_RREADLINK,
29ebe74f8bSWei Liu     P9_TGETATTR = 24,
30ebe74f8bSWei Liu     P9_RGETATTR,
31ebe74f8bSWei Liu     P9_TSETATTR = 26,
32ebe74f8bSWei Liu     P9_RSETATTR,
33ebe74f8bSWei Liu     P9_TXATTRWALK = 30,
34ebe74f8bSWei Liu     P9_RXATTRWALK,
35ebe74f8bSWei Liu     P9_TXATTRCREATE = 32,
36ebe74f8bSWei Liu     P9_RXATTRCREATE,
37ebe74f8bSWei Liu     P9_TREADDIR = 40,
38ebe74f8bSWei Liu     P9_RREADDIR,
39ebe74f8bSWei Liu     P9_TFSYNC = 50,
40ebe74f8bSWei Liu     P9_RFSYNC,
41ebe74f8bSWei Liu     P9_TLOCK = 52,
42ebe74f8bSWei Liu     P9_RLOCK,
43ebe74f8bSWei Liu     P9_TGETLOCK = 54,
44ebe74f8bSWei Liu     P9_RGETLOCK,
45ebe74f8bSWei Liu     P9_TLINK = 70,
46ebe74f8bSWei Liu     P9_RLINK,
47ebe74f8bSWei Liu     P9_TMKDIR = 72,
48ebe74f8bSWei Liu     P9_RMKDIR,
49ebe74f8bSWei Liu     P9_TRENAMEAT = 74,
50ebe74f8bSWei Liu     P9_RRENAMEAT,
51ebe74f8bSWei Liu     P9_TUNLINKAT = 76,
52ebe74f8bSWei Liu     P9_RUNLINKAT,
53ebe74f8bSWei Liu     P9_TVERSION = 100,
54ebe74f8bSWei Liu     P9_RVERSION,
55ebe74f8bSWei Liu     P9_TAUTH = 102,
56ebe74f8bSWei Liu     P9_RAUTH,
57ebe74f8bSWei Liu     P9_TATTACH = 104,
58ebe74f8bSWei Liu     P9_RATTACH,
59ebe74f8bSWei Liu     P9_TERROR = 106,
60ebe74f8bSWei Liu     P9_RERROR,
61ebe74f8bSWei Liu     P9_TFLUSH = 108,
62ebe74f8bSWei Liu     P9_RFLUSH,
63ebe74f8bSWei Liu     P9_TWALK = 110,
64ebe74f8bSWei Liu     P9_RWALK,
65ebe74f8bSWei Liu     P9_TOPEN = 112,
66ebe74f8bSWei Liu     P9_ROPEN,
67ebe74f8bSWei Liu     P9_TCREATE = 114,
68ebe74f8bSWei Liu     P9_RCREATE,
69ebe74f8bSWei Liu     P9_TREAD = 116,
70ebe74f8bSWei Liu     P9_RREAD,
71ebe74f8bSWei Liu     P9_TWRITE = 118,
72ebe74f8bSWei Liu     P9_RWRITE,
73ebe74f8bSWei Liu     P9_TCLUNK = 120,
74ebe74f8bSWei Liu     P9_RCLUNK,
75ebe74f8bSWei Liu     P9_TREMOVE = 122,
76ebe74f8bSWei Liu     P9_RREMOVE,
77ebe74f8bSWei Liu     P9_TSTAT = 124,
78ebe74f8bSWei Liu     P9_RSTAT,
79ebe74f8bSWei Liu     P9_TWSTAT = 126,
80ebe74f8bSWei Liu     P9_RWSTAT,
81ebe74f8bSWei Liu };
82ebe74f8bSWei Liu 
83ebe74f8bSWei Liu 
84ebe74f8bSWei Liu /* qid.types */
85ebe74f8bSWei Liu enum {
86ebe74f8bSWei Liu     P9_QTDIR = 0x80,
87ebe74f8bSWei Liu     P9_QTAPPEND = 0x40,
88ebe74f8bSWei Liu     P9_QTEXCL = 0x20,
89ebe74f8bSWei Liu     P9_QTMOUNT = 0x10,
90ebe74f8bSWei Liu     P9_QTAUTH = 0x08,
91ebe74f8bSWei Liu     P9_QTTMP = 0x04,
92ebe74f8bSWei Liu     P9_QTSYMLINK = 0x02,
93ebe74f8bSWei Liu     P9_QTLINK = 0x01,
94ebe74f8bSWei Liu     P9_QTFILE = 0x00,
95ebe74f8bSWei Liu };
96ebe74f8bSWei Liu 
978e71b96cSGreg Kurz typedef enum P9ProtoVersion {
98ebe74f8bSWei Liu     V9FS_PROTO_2000U = 0x01,
99ebe74f8bSWei Liu     V9FS_PROTO_2000L = 0x02,
1008e71b96cSGreg Kurz } P9ProtoVersion;
101ebe74f8bSWei Liu 
102baecbde6SGreg Kurz #define P9_NOTAG    UINT16_MAX
103baecbde6SGreg Kurz #define P9_NOFID    UINT32_MAX
104ebe74f8bSWei Liu #define P9_MAXWELEM 16
105ebe74f8bSWei Liu 
106ebe74f8bSWei Liu #define FID_REFERENCED          0x1
107ebe74f8bSWei Liu #define FID_NON_RECLAIMABLE     0x2
108ebe74f8bSWei Liu static inline char *rpath(FsContext *ctx, const char *path)
109ebe74f8bSWei Liu {
110ebe74f8bSWei Liu     return g_strdup_printf("%s/%s", ctx->fs_root, path);
111ebe74f8bSWei Liu }
112ebe74f8bSWei Liu 
113ebe74f8bSWei Liu /*
114ebe74f8bSWei Liu  * ample room for Twrite/Rread header
115ebe74f8bSWei Liu  * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
116ebe74f8bSWei Liu  */
117ebe74f8bSWei Liu #define P9_IOHDRSZ 24
118ebe74f8bSWei Liu 
119ebe74f8bSWei Liu typedef struct V9fsPDU V9fsPDU;
12021cf9edfSGreg Kurz typedef struct V9fsState V9fsState;
1218e71b96cSGreg Kurz typedef struct V9fsTransport V9fsTransport;
122ebe74f8bSWei Liu 
123c9fb47e7SStefano Stabellini typedef struct {
124c9fb47e7SStefano Stabellini     uint32_t size_le;
125c9fb47e7SStefano Stabellini     uint8_t id;
126c9fb47e7SStefano Stabellini     uint16_t tag_le;
127c9fb47e7SStefano Stabellini } QEMU_PACKED P9MsgHeader;
128a4d99854SGreg Kurz /* According to the specification, 9p messages start with a 7-byte header.
129a4d99854SGreg Kurz  * Since most of the code uses this header size in literal form, we must be
130a4d99854SGreg Kurz  * sure this is indeed the case.
131a4d99854SGreg Kurz  */
132a4d99854SGreg Kurz QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader) != 7);
133c9fb47e7SStefano Stabellini 
134ebe74f8bSWei Liu struct V9fsPDU
135ebe74f8bSWei Liu {
136ebe74f8bSWei Liu     uint32_t size;
137ebe74f8bSWei Liu     uint16_t tag;
138ebe74f8bSWei Liu     uint8_t id;
139ebe74f8bSWei Liu     uint8_t cancelled;
140ebe74f8bSWei Liu     CoQueue complete;
14121cf9edfSGreg Kurz     V9fsState *s;
142ebe74f8bSWei Liu     QLIST_ENTRY(V9fsPDU) next;
14300588a0aSWei Liu     uint32_t idx;
144ebe74f8bSWei Liu };
145ebe74f8bSWei Liu 
146ebe74f8bSWei Liu 
147ebe74f8bSWei Liu /* FIXME
148ebe74f8bSWei Liu  * 1) change user needs to set groups and stuff
149ebe74f8bSWei Liu  */
150ebe74f8bSWei Liu 
151ebe74f8bSWei Liu #define MAX_REQ         128
152ebe74f8bSWei Liu #define MAX_TAG_LEN     32
153ebe74f8bSWei Liu 
154ebe74f8bSWei Liu #define BUG_ON(cond) assert(!(cond))
155ebe74f8bSWei Liu 
156ebe74f8bSWei Liu typedef struct V9fsFidState V9fsFidState;
157ebe74f8bSWei Liu 
158ebe74f8bSWei Liu enum {
159ebe74f8bSWei Liu     P9_FID_NONE = 0,
160ebe74f8bSWei Liu     P9_FID_FILE,
161ebe74f8bSWei Liu     P9_FID_DIR,
162ebe74f8bSWei Liu     P9_FID_XATTR,
163ebe74f8bSWei Liu };
164ebe74f8bSWei Liu 
165ebe74f8bSWei Liu typedef struct V9fsConf
166ebe74f8bSWei Liu {
167ebe74f8bSWei Liu     /* tag name for the device */
168ebe74f8bSWei Liu     char *tag;
169ebe74f8bSWei Liu     char *fsdev_id;
170ebe74f8bSWei Liu } V9fsConf;
171ebe74f8bSWei Liu 
172aca6897fSKeno Fischer /* 9p2000.L xattr flags (matches Linux values) */
173aca6897fSKeno Fischer #define P9_XATTR_CREATE 1
174aca6897fSKeno Fischer #define P9_XATTR_REPLACE 2
175aca6897fSKeno Fischer 
176ebe74f8bSWei Liu typedef struct V9fsXattr
177ebe74f8bSWei Liu {
1788495f9adSLi Qiang     uint64_t copied_len;
1798495f9adSLi Qiang     uint64_t len;
180ebe74f8bSWei Liu     void *value;
181ebe74f8bSWei Liu     V9fsString name;
182ebe74f8bSWei Liu     int flags;
183dd28fbbcSLi Qiang     bool xattrwalk_fid;
184ebe74f8bSWei Liu } V9fsXattr;
185ebe74f8bSWei Liu 
186f314ea4eSGreg Kurz typedef struct V9fsDir {
187f314ea4eSGreg Kurz     DIR *stream;
1887cde47d4SGreg Kurz     QemuMutex readdir_mutex;
189f314ea4eSGreg Kurz } V9fsDir;
190f314ea4eSGreg Kurz 
1917cde47d4SGreg Kurz static inline void v9fs_readdir_lock(V9fsDir *dir)
1927cde47d4SGreg Kurz {
1937cde47d4SGreg Kurz     qemu_mutex_lock(&dir->readdir_mutex);
1947cde47d4SGreg Kurz }
1957cde47d4SGreg Kurz 
1967cde47d4SGreg Kurz static inline void v9fs_readdir_unlock(V9fsDir *dir)
1977cde47d4SGreg Kurz {
1987cde47d4SGreg Kurz     qemu_mutex_unlock(&dir->readdir_mutex);
1997cde47d4SGreg Kurz }
2007cde47d4SGreg Kurz 
2017cde47d4SGreg Kurz static inline void v9fs_readdir_init(V9fsDir *dir)
2027cde47d4SGreg Kurz {
2037cde47d4SGreg Kurz     qemu_mutex_init(&dir->readdir_mutex);
2047cde47d4SGreg Kurz }
2057cde47d4SGreg Kurz 
206ebe74f8bSWei Liu /*
207ebe74f8bSWei Liu  * Filled by fs driver on open and other
208ebe74f8bSWei Liu  * calls.
209ebe74f8bSWei Liu  */
210ebe74f8bSWei Liu union V9fsFidOpenState {
211ebe74f8bSWei Liu     int fd;
212f314ea4eSGreg Kurz     V9fsDir dir;
213ebe74f8bSWei Liu     V9fsXattr xattr;
214ebe74f8bSWei Liu     /*
215ebe74f8bSWei Liu      * private pointer for fs drivers, that
216ebe74f8bSWei Liu      * have its own internal representation of
217ebe74f8bSWei Liu      * open files.
218ebe74f8bSWei Liu      */
219ebe74f8bSWei Liu     void *private;
220ebe74f8bSWei Liu };
221ebe74f8bSWei Liu 
222ebe74f8bSWei Liu struct V9fsFidState
223ebe74f8bSWei Liu {
224ebe74f8bSWei Liu     int fid_type;
225ebe74f8bSWei Liu     int32_t fid;
226ebe74f8bSWei Liu     V9fsPath path;
227ebe74f8bSWei Liu     V9fsFidOpenState fs;
228ebe74f8bSWei Liu     V9fsFidOpenState fs_reclaim;
229ebe74f8bSWei Liu     int flags;
230ebe74f8bSWei Liu     int open_flags;
231ebe74f8bSWei Liu     uid_t uid;
232ebe74f8bSWei Liu     int ref;
233ebe74f8bSWei Liu     int clunked;
234ebe74f8bSWei Liu     V9fsFidState *next;
235ebe74f8bSWei Liu     V9fsFidState *rclm_lst;
236ebe74f8bSWei Liu };
237ebe74f8bSWei Liu 
23821cf9edfSGreg Kurz struct V9fsState
239ebe74f8bSWei Liu {
240ebe74f8bSWei Liu     QLIST_HEAD(, V9fsPDU) free_list;
241ebe74f8bSWei Liu     QLIST_HEAD(, V9fsPDU) active_list;
242ebe74f8bSWei Liu     V9fsFidState *fid_list;
243ebe74f8bSWei Liu     FileOperations *ops;
244ebe74f8bSWei Liu     FsContext ctx;
245ebe74f8bSWei Liu     char *tag;
2468e71b96cSGreg Kurz     P9ProtoVersion proto_version;
247ebe74f8bSWei Liu     int32_t msize;
248583f21f8SStefano Stabellini     V9fsPDU pdus[MAX_REQ];
2498e71b96cSGreg Kurz     const V9fsTransport *transport;
250ebe74f8bSWei Liu     /*
251ebe74f8bSWei Liu      * lock ensuring atomic path update
252ebe74f8bSWei Liu      * on rename.
253ebe74f8bSWei Liu      */
254ebe74f8bSWei Liu     CoRwlock rename_lock;
255ebe74f8bSWei Liu     int32_t root_fid;
256ebe74f8bSWei Liu     Error *migration_blocker;
257ebe74f8bSWei Liu     V9fsConf fsconf;
25856f101ecSGreg Kurz     V9fsQID root_qid;
259*3b5ee9e8SAntonios Motakis     dev_t dev_id;
26021cf9edfSGreg Kurz };
261ebe74f8bSWei Liu 
262ebe74f8bSWei Liu /* 9p2000.L open flags */
263ebe74f8bSWei Liu #define P9_DOTL_RDONLY        00000000
264ebe74f8bSWei Liu #define P9_DOTL_WRONLY        00000001
265ebe74f8bSWei Liu #define P9_DOTL_RDWR          00000002
266ebe74f8bSWei Liu #define P9_DOTL_NOACCESS      00000003
267ebe74f8bSWei Liu #define P9_DOTL_CREATE        00000100
268ebe74f8bSWei Liu #define P9_DOTL_EXCL          00000200
269ebe74f8bSWei Liu #define P9_DOTL_NOCTTY        00000400
270ebe74f8bSWei Liu #define P9_DOTL_TRUNC         00001000
271ebe74f8bSWei Liu #define P9_DOTL_APPEND        00002000
272ebe74f8bSWei Liu #define P9_DOTL_NONBLOCK      00004000
273ebe74f8bSWei Liu #define P9_DOTL_DSYNC         00010000
274ebe74f8bSWei Liu #define P9_DOTL_FASYNC        00020000
275ebe74f8bSWei Liu #define P9_DOTL_DIRECT        00040000
276ebe74f8bSWei Liu #define P9_DOTL_LARGEFILE     00100000
277ebe74f8bSWei Liu #define P9_DOTL_DIRECTORY     00200000
278ebe74f8bSWei Liu #define P9_DOTL_NOFOLLOW      00400000
279ebe74f8bSWei Liu #define P9_DOTL_NOATIME       01000000
280ebe74f8bSWei Liu #define P9_DOTL_CLOEXEC       02000000
281ebe74f8bSWei Liu #define P9_DOTL_SYNC          04000000
282ebe74f8bSWei Liu 
283ebe74f8bSWei Liu /* 9p2000.L at flags */
284ebe74f8bSWei Liu #define P9_DOTL_AT_REMOVEDIR         0x200
285ebe74f8bSWei Liu 
286ebe74f8bSWei Liu /* 9P2000.L lock type */
287ebe74f8bSWei Liu #define P9_LOCK_TYPE_RDLCK 0
288ebe74f8bSWei Liu #define P9_LOCK_TYPE_WRLCK 1
289ebe74f8bSWei Liu #define P9_LOCK_TYPE_UNLCK 2
290ebe74f8bSWei Liu 
291ebe74f8bSWei Liu #define P9_LOCK_SUCCESS 0
292ebe74f8bSWei Liu #define P9_LOCK_BLOCKED 1
293ebe74f8bSWei Liu #define P9_LOCK_ERROR 2
294ebe74f8bSWei Liu #define P9_LOCK_GRACE 3
295ebe74f8bSWei Liu 
296ebe74f8bSWei Liu #define P9_LOCK_FLAGS_BLOCK 1
297ebe74f8bSWei Liu #define P9_LOCK_FLAGS_RECLAIM 2
298ebe74f8bSWei Liu 
299ebe74f8bSWei Liu typedef struct V9fsFlock
300ebe74f8bSWei Liu {
301ebe74f8bSWei Liu     uint8_t type;
302ebe74f8bSWei Liu     uint32_t flags;
303ebe74f8bSWei Liu     uint64_t start; /* absolute offset */
304ebe74f8bSWei Liu     uint64_t length;
305ebe74f8bSWei Liu     uint32_t proc_id;
306ebe74f8bSWei Liu     V9fsString client_id;
307ebe74f8bSWei Liu } V9fsFlock;
308ebe74f8bSWei Liu 
309ebe74f8bSWei Liu typedef struct V9fsGetlock
310ebe74f8bSWei Liu {
311ebe74f8bSWei Liu     uint8_t type;
312ebe74f8bSWei Liu     uint64_t start; /* absolute offset */
313ebe74f8bSWei Liu     uint64_t length;
314ebe74f8bSWei Liu     uint32_t proc_id;
315ebe74f8bSWei Liu     V9fsString client_id;
316ebe74f8bSWei Liu } V9fsGetlock;
317ebe74f8bSWei Liu 
318ebe74f8bSWei Liu extern int open_fd_hw;
319ebe74f8bSWei Liu extern int total_open_fd;
320ebe74f8bSWei Liu 
321ebe74f8bSWei Liu static inline void v9fs_path_write_lock(V9fsState *s)
322ebe74f8bSWei Liu {
323ebe74f8bSWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
324ebe74f8bSWei Liu         qemu_co_rwlock_wrlock(&s->rename_lock);
325ebe74f8bSWei Liu     }
326ebe74f8bSWei Liu }
327ebe74f8bSWei Liu 
328ebe74f8bSWei Liu static inline void v9fs_path_read_lock(V9fsState *s)
329ebe74f8bSWei Liu {
330ebe74f8bSWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
331ebe74f8bSWei Liu         qemu_co_rwlock_rdlock(&s->rename_lock);
332ebe74f8bSWei Liu     }
333ebe74f8bSWei Liu }
334ebe74f8bSWei Liu 
335ebe74f8bSWei Liu static inline void v9fs_path_unlock(V9fsState *s)
336ebe74f8bSWei Liu {
337ebe74f8bSWei Liu     if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
338ebe74f8bSWei Liu         qemu_co_rwlock_unlock(&s->rename_lock);
339ebe74f8bSWei Liu     }
340ebe74f8bSWei Liu }
341ebe74f8bSWei Liu 
342ebe74f8bSWei Liu static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
343ebe74f8bSWei Liu {
344ebe74f8bSWei Liu     return pdu->cancelled;
345ebe74f8bSWei Liu }
346ebe74f8bSWei Liu 
3478440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu);
348bc70a592SGreg Kurz void v9fs_path_init(V9fsPath *path);
349bc70a592SGreg Kurz void v9fs_path_free(V9fsPath *path);
350bc70a592SGreg Kurz void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
351e446a1ebSMarc-André Lureau void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src);
352bc70a592SGreg Kurz int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
353ebe74f8bSWei Liu                       const char *name, V9fsPath *path);
354066eb006SGreg Kurz int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
355066eb006SGreg Kurz                                Error **errp);
356bc70a592SGreg Kurz void v9fs_device_unrealize_common(V9fsState *s, Error **errp);
357ebe74f8bSWei Liu 
3584b311c5fSWei Liu V9fsPDU *pdu_alloc(V9fsState *s);
3594b311c5fSWei Liu void pdu_free(V9fsPDU *pdu);
360506f3275SGreg Kurz void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr);
3610e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s);
362ebe74f8bSWei Liu 
363ea83441cSStefano Stabellini struct V9fsTransport {
364ea83441cSStefano Stabellini     ssize_t     (*pdu_vmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
365ea83441cSStefano Stabellini                                 va_list ap);
366ea83441cSStefano Stabellini     ssize_t     (*pdu_vunmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
367ea83441cSStefano Stabellini                                   va_list ap);
36888da0b03SStefano Stabellini     void        (*init_in_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
36988da0b03SStefano Stabellini                                         unsigned int *pniov, size_t size);
37088da0b03SStefano Stabellini     void        (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
3718d37de41SGreg Kurz                                          unsigned int *pniov, size_t size);
372ea83441cSStefano Stabellini     void        (*push_and_notify)(V9fsPDU *pdu);
373ea83441cSStefano Stabellini };
374ea83441cSStefano Stabellini 
375ebe74f8bSWei Liu #endif
376