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