1 #ifndef _QEMU_VIRTIO_9P_H
2 #define _QEMU_VIRTIO_9P_H
10 #include <sys/resource.h>
13 #include <sys/resource.h>
16 #include "hw/virtio/virtio.h"
17 #include "fsdev/file-op-9p.h"
18 #include "fsdev/virtio-9p-marshal.h"
19 #include "qemu/thread.h"
20 #include "block/coroutine.h"
22 /* The feature bitmap for virtio 9P */
23 /* The mount point is specified in a config variable */
24 #define VIRTIO_9P_MOUNT_TAG 0
111 enum p9_proto_version {
112 V9FS_PROTO_2000U = 0x01,
113 V9FS_PROTO_2000L = 0x02,
116 #define P9_NOTAG (u16)(~0)
117 #define P9_NOFID (u32)(~0)
118 #define P9_MAXWELEM 16
120 #define FID_REFERENCED 0x1
121 #define FID_NON_RECLAIMABLE 0x2
122 static inline char *rpath(FsContext *ctx, const char *path)
125 return g_strdup_printf("%s/%s", ctx->fs_root, path);
128 return g_strdup_printf("%s/%s", ctx->fs_root, path);
132 buffer = g_strdup_printf("%s\\%s", ctx->fs_root, path);
133 while(buffer[strlen(buffer)-1] == '\\'){
134 buffer[strlen(buffer)-1] = '\0';
142 * ample room for Twrite/Rread header
143 * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
145 #define P9_IOHDRSZ 24
147 typedef struct V9fsPDU V9fsPDU;
157 VirtQueueElement elem;
159 QLIST_ENTRY(V9fsPDU) next;
164 * 1) change user needs to set groups and stuff
167 /* from Linux's linux/virtio_9p.h */
169 /* The ID for virtio console */
170 #define VIRTIO_ID_9P 9
172 #define MAX_TAG_LEN 32
174 #define BUG_ON(cond) assert(!(cond))
176 typedef struct V9fsFidState V9fsFidState;
185 typedef struct V9fsXattr
195 * Filled by fs driver on open and other
198 union V9fsFidOpenState {
203 * private pointer for fs drivers, that
204 * have its own internal representation of
216 V9fsFidOpenState fs_reclaim;
223 V9fsFidState *rclm_lst;
226 typedef struct V9fsState
228 VirtIODevice parent_obj;
230 V9fsPDU pdus[MAX_REQ];
231 QLIST_HEAD(, V9fsPDU) free_list;
232 QLIST_HEAD(, V9fsPDU) active_list;
233 V9fsFidState *fid_list;
238 enum p9_proto_version proto_version;
241 * lock ensuring atomic path update
244 CoRwlock rename_lock;
246 Error *migration_blocker;
250 typedef struct V9fsStatState {
258 typedef struct V9fsOpenState {
268 typedef struct V9fsReadState {
275 struct iovec iov[128]; /* FIXME: bad, bad, bad */
287 typedef struct V9fsWriteState {
295 struct iovec iov[128]; /* FIXME: bad, bad, bad */
300 struct virtio_9p_config
302 /* number of characters in tag */
304 /* Variable size tag name */
308 typedef struct V9fsMkState {
317 /* 9p2000.L open flags */
318 #define P9_DOTL_RDONLY 00000000
319 #define P9_DOTL_WRONLY 00000001
320 #define P9_DOTL_RDWR 00000002
321 #define P9_DOTL_NOACCESS 00000003
322 #define P9_DOTL_CREATE 00000100
323 #define P9_DOTL_EXCL 00000200
324 #define P9_DOTL_NOCTTY 00000400
325 #define P9_DOTL_TRUNC 00001000
326 #define P9_DOTL_APPEND 00002000
327 #define P9_DOTL_NONBLOCK 00004000
328 #define P9_DOTL_DSYNC 00010000
329 #define P9_DOTL_FASYNC 00020000
330 #define P9_DOTL_DIRECT 00040000
331 #define P9_DOTL_LARGEFILE 00100000
332 #define P9_DOTL_DIRECTORY 00200000
333 #define P9_DOTL_NOFOLLOW 00400000
334 #define P9_DOTL_NOATIME 01000000
335 #define P9_DOTL_CLOEXEC 02000000
336 #define P9_DOTL_SYNC 04000000
338 /* 9p2000.L at flags */
339 #define P9_DOTL_AT_REMOVEDIR 0x200
341 /* 9P2000.L lock type */
342 #define P9_LOCK_TYPE_RDLCK 0
343 #define P9_LOCK_TYPE_WRLCK 1
344 #define P9_LOCK_TYPE_UNLCK 2
346 #define P9_LOCK_SUCCESS 0
347 #define P9_LOCK_BLOCKED 1
348 #define P9_LOCK_ERROR 2
349 #define P9_LOCK_GRACE 3
351 #define P9_LOCK_FLAGS_BLOCK 1
352 #define P9_LOCK_FLAGS_RECLAIM 2
354 typedef struct V9fsFlock
358 uint64_t start; /* absolute offset */
361 V9fsString client_id;
364 typedef struct V9fsGetlock
367 uint64_t start; /* absolute offset */
370 V9fsString client_id;
373 extern int open_fd_hw;
374 extern int total_open_fd;
376 size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
377 size_t offset, size_t size, int pack);
379 static inline size_t do_pdu_unpack(void *dst, struct iovec *sg, int sg_count,
380 size_t offset, size_t size)
382 return pdu_packunpack(dst, sg, sg_count, offset, size, 0);
385 static inline void v9fs_path_write_lock(V9fsState *s)
387 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
388 qemu_co_rwlock_wrlock(&s->rename_lock);
392 static inline void v9fs_path_read_lock(V9fsState *s)
394 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
395 qemu_co_rwlock_rdlock(&s->rename_lock);
399 static inline void v9fs_path_unlock(V9fsState *s)
401 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
402 qemu_co_rwlock_unlock(&s->rename_lock);
406 static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
408 return pdu->cancelled;
411 extern void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq);
412 extern void v9fs_reclaim_fd(V9fsPDU *pdu);
413 extern void v9fs_path_init(V9fsPath *path);
414 extern void v9fs_path_free(V9fsPath *path);
415 extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
416 extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
417 const char *name, V9fsPath *path);
419 #define pdu_marshal(pdu, offset, fmt, args...) \
420 v9fs_marshal(pdu->elem.in_sg, pdu->elem.in_num, offset, 1, fmt, ##args)
421 #define pdu_unmarshal(pdu, offset, fmt, args...) \
422 v9fs_unmarshal(pdu->elem.out_sg, pdu->elem.out_num, offset, 1, fmt, ##args)
424 #define TYPE_VIRTIO_9P "virtio-9p-device"
425 #define VIRTIO_9P(obj) \
426 OBJECT_CHECK(V9fsState, (obj), TYPE_VIRTIO_9P)
428 #define DEFINE_VIRTIO_9P_PROPERTIES(_state, _field) \
429 DEFINE_PROP_STRING("mount_tag", _state, _field.tag), \
430 DEFINE_PROP_STRING("fsdev", _state, _field.fsdev_id)