1#ifndef QEMU_9P_H
2#define QEMU_9P_H
3
4#include <dirent.h>
5#include <utime.h>
6#include <sys/resource.h>
7#include "fsdev/file-op-9p.h"
8#include "fsdev/9p-iov-marshal.h"
9#include "qemu/thread.h"
10#include "qemu/coroutine.h"
11
12enum {
13 P9_TLERROR = 6,
14 P9_RLERROR,
15 P9_TSTATFS = 8,
16 P9_RSTATFS,
17 P9_TLOPEN = 12,
18 P9_RLOPEN,
19 P9_TLCREATE = 14,
20 P9_RLCREATE,
21 P9_TSYMLINK = 16,
22 P9_RSYMLINK,
23 P9_TMKNOD = 18,
24 P9_RMKNOD,
25 P9_TRENAME = 20,
26 P9_RRENAME,
27 P9_TREADLINK = 22,
28 P9_RREADLINK,
29 P9_TGETATTR = 24,
30 P9_RGETATTR,
31 P9_TSETATTR = 26,
32 P9_RSETATTR,
33 P9_TXATTRWALK = 30,
34 P9_RXATTRWALK,
35 P9_TXATTRCREATE = 32,
36 P9_RXATTRCREATE,
37 P9_TREADDIR = 40,
38 P9_RREADDIR,
39 P9_TFSYNC = 50,
40 P9_RFSYNC,
41 P9_TLOCK = 52,
42 P9_RLOCK,
43 P9_TGETLOCK = 54,
44 P9_RGETLOCK,
45 P9_TLINK = 70,
46 P9_RLINK,
47 P9_TMKDIR = 72,
48 P9_RMKDIR,
49 P9_TRENAMEAT = 74,
50 P9_RRENAMEAT,
51 P9_TUNLINKAT = 76,
52 P9_RUNLINKAT,
53 P9_TVERSION = 100,
54 P9_RVERSION,
55 P9_TAUTH = 102,
56 P9_RAUTH,
57 P9_TATTACH = 104,
58 P9_RATTACH,
59 P9_TERROR = 106,
60 P9_RERROR,
61 P9_TFLUSH = 108,
62 P9_RFLUSH,
63 P9_TWALK = 110,
64 P9_RWALK,
65 P9_TOPEN = 112,
66 P9_ROPEN,
67 P9_TCREATE = 114,
68 P9_RCREATE,
69 P9_TREAD = 116,
70 P9_RREAD,
71 P9_TWRITE = 118,
72 P9_RWRITE,
73 P9_TCLUNK = 120,
74 P9_RCLUNK,
75 P9_TREMOVE = 122,
76 P9_RREMOVE,
77 P9_TSTAT = 124,
78 P9_RSTAT,
79 P9_TWSTAT = 126,
80 P9_RWSTAT,
81};
82
83
84
85enum {
86 P9_QTDIR = 0x80,
87 P9_QTAPPEND = 0x40,
88 P9_QTEXCL = 0x20,
89 P9_QTMOUNT = 0x10,
90 P9_QTAUTH = 0x08,
91 P9_QTTMP = 0x04,
92 P9_QTSYMLINK = 0x02,
93 P9_QTLINK = 0x01,
94 P9_QTFILE = 0x00,
95};
96
97enum p9_proto_version {
98 V9FS_PROTO_2000U = 0x01,
99 V9FS_PROTO_2000L = 0x02,
100};
101
102#define P9_NOTAG UINT16_MAX
103#define P9_NOFID UINT32_MAX
104#define P9_MAXWELEM 16
105
106#define FID_REFERENCED 0x1
107#define FID_NON_RECLAIMABLE 0x2
108static inline char *rpath(FsContext *ctx, const char *path)
109{
110 return g_strdup_printf("%s/%s", ctx->fs_root, path);
111}
112
113
114
115
116
117#define P9_IOHDRSZ 24
118
119typedef struct V9fsPDU V9fsPDU;
120typedef struct V9fsState V9fsState;
121
122typedef struct {
123 uint32_t size_le;
124 uint8_t id;
125 uint16_t tag_le;
126} QEMU_PACKED P9MsgHeader;
127
128
129
130
131QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader) != 7);
132
133struct V9fsPDU
134{
135 uint32_t size;
136 uint16_t tag;
137 uint8_t id;
138 uint8_t cancelled;
139 CoQueue complete;
140 V9fsState *s;
141 QLIST_ENTRY(V9fsPDU) next;
142 uint32_t idx;
143};
144
145
146
147
148
149
150#define MAX_REQ 128
151#define MAX_TAG_LEN 32
152
153#define BUG_ON(cond) assert(!(cond))
154
155typedef struct V9fsFidState V9fsFidState;
156
157enum {
158 P9_FID_NONE = 0,
159 P9_FID_FILE,
160 P9_FID_DIR,
161 P9_FID_XATTR,
162};
163
164typedef struct V9fsConf
165{
166
167 char *tag;
168 char *fsdev_id;
169} V9fsConf;
170
171typedef struct V9fsXattr
172{
173 uint64_t copied_len;
174 uint64_t len;
175 void *value;
176 V9fsString name;
177 int flags;
178 bool xattrwalk_fid;
179} V9fsXattr;
180
181typedef struct V9fsDir {
182 DIR *stream;
183 QemuMutex readdir_mutex;
184} V9fsDir;
185
186static inline void v9fs_readdir_lock(V9fsDir *dir)
187{
188 qemu_mutex_lock(&dir->readdir_mutex);
189}
190
191static inline void v9fs_readdir_unlock(V9fsDir *dir)
192{
193 qemu_mutex_unlock(&dir->readdir_mutex);
194}
195
196static inline void v9fs_readdir_init(V9fsDir *dir)
197{
198 qemu_mutex_init(&dir->readdir_mutex);
199}
200
201
202
203
204
205union V9fsFidOpenState {
206 int fd;
207 V9fsDir dir;
208 V9fsXattr xattr;
209
210
211
212
213
214 void *private;
215};
216
217struct V9fsFidState
218{
219 int fid_type;
220 int32_t fid;
221 V9fsPath path;
222 V9fsFidOpenState fs;
223 V9fsFidOpenState fs_reclaim;
224 int flags;
225 int open_flags;
226 uid_t uid;
227 int ref;
228 int clunked;
229 V9fsFidState *next;
230 V9fsFidState *rclm_lst;
231};
232
233struct V9fsState
234{
235 QLIST_HEAD(, V9fsPDU) free_list;
236 QLIST_HEAD(, V9fsPDU) active_list;
237 V9fsFidState *fid_list;
238 FileOperations *ops;
239 FsContext ctx;
240 char *tag;
241 enum p9_proto_version proto_version;
242 int32_t msize;
243 V9fsPDU pdus[MAX_REQ];
244 const struct V9fsTransport *transport;
245
246
247
248
249 CoRwlock rename_lock;
250 int32_t root_fid;
251 Error *migration_blocker;
252 V9fsConf fsconf;
253 V9fsQID root_qid;
254};
255
256
257#define P9_DOTL_RDONLY 00000000
258#define P9_DOTL_WRONLY 00000001
259#define P9_DOTL_RDWR 00000002
260#define P9_DOTL_NOACCESS 00000003
261#define P9_DOTL_CREATE 00000100
262#define P9_DOTL_EXCL 00000200
263#define P9_DOTL_NOCTTY 00000400
264#define P9_DOTL_TRUNC 00001000
265#define P9_DOTL_APPEND 00002000
266#define P9_DOTL_NONBLOCK 00004000
267#define P9_DOTL_DSYNC 00010000
268#define P9_DOTL_FASYNC 00020000
269#define P9_DOTL_DIRECT 00040000
270#define P9_DOTL_LARGEFILE 00100000
271#define P9_DOTL_DIRECTORY 00200000
272#define P9_DOTL_NOFOLLOW 00400000
273#define P9_DOTL_NOATIME 01000000
274#define P9_DOTL_CLOEXEC 02000000
275#define P9_DOTL_SYNC 04000000
276
277
278#define P9_DOTL_AT_REMOVEDIR 0x200
279
280
281#define P9_LOCK_TYPE_RDLCK 0
282#define P9_LOCK_TYPE_WRLCK 1
283#define P9_LOCK_TYPE_UNLCK 2
284
285#define P9_LOCK_SUCCESS 0
286#define P9_LOCK_BLOCKED 1
287#define P9_LOCK_ERROR 2
288#define P9_LOCK_GRACE 3
289
290#define P9_LOCK_FLAGS_BLOCK 1
291#define P9_LOCK_FLAGS_RECLAIM 2
292
293typedef struct V9fsFlock
294{
295 uint8_t type;
296 uint32_t flags;
297 uint64_t start;
298 uint64_t length;
299 uint32_t proc_id;
300 V9fsString client_id;
301} V9fsFlock;
302
303typedef struct V9fsGetlock
304{
305 uint8_t type;
306 uint64_t start;
307 uint64_t length;
308 uint32_t proc_id;
309 V9fsString client_id;
310} V9fsGetlock;
311
312extern int open_fd_hw;
313extern int total_open_fd;
314
315static inline void v9fs_path_write_lock(V9fsState *s)
316{
317 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
318 qemu_co_rwlock_wrlock(&s->rename_lock);
319 }
320}
321
322static inline void v9fs_path_read_lock(V9fsState *s)
323{
324 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
325 qemu_co_rwlock_rdlock(&s->rename_lock);
326 }
327}
328
329static inline void v9fs_path_unlock(V9fsState *s)
330{
331 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
332 qemu_co_rwlock_unlock(&s->rename_lock);
333 }
334}
335
336static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
337{
338 return pdu->cancelled;
339}
340
341void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu);
342void v9fs_path_init(V9fsPath *path);
343void v9fs_path_free(V9fsPath *path);
344void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
345void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
346int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
347 const char *name, V9fsPath *path);
348int v9fs_device_realize_common(V9fsState *s, Error **errp);
349void v9fs_device_unrealize_common(V9fsState *s, Error **errp);
350
351ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...);
352ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...);
353V9fsPDU *pdu_alloc(V9fsState *s);
354void pdu_free(V9fsPDU *pdu);
355void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr);
356void v9fs_reset(V9fsState *s);
357
358struct V9fsTransport {
359 ssize_t (*pdu_vmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
360 va_list ap);
361 ssize_t (*pdu_vunmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
362 va_list ap);
363 void (*init_in_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
364 unsigned int *pniov, size_t size);
365 void (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
366 unsigned int *pniov, size_t size);
367 void (*push_and_notify)(V9fsPDU *pdu);
368};
369
370static inline int v9fs_register_transport(V9fsState *s,
371 const struct V9fsTransport *t)
372{
373 assert(!s->transport);
374 s->transport = t;
375 return 0;
376}
377
378#endif
379