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 97ebe74f8bSWei Liu enum p9_proto_version { 98ebe74f8bSWei Liu V9FS_PROTO_2000U = 0x01, 99ebe74f8bSWei Liu V9FS_PROTO_2000L = 0x02, 100ebe74f8bSWei Liu }; 101ebe74f8bSWei Liu 102*baecbde6SGreg Kurz #define P9_NOTAG UINT16_MAX 103*baecbde6SGreg 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; 120ebe74f8bSWei Liu struct V9fsState; 121ebe74f8bSWei Liu 122ebe74f8bSWei Liu struct V9fsPDU 123ebe74f8bSWei Liu { 124ebe74f8bSWei Liu uint32_t size; 125ebe74f8bSWei Liu uint16_t tag; 126ebe74f8bSWei Liu uint8_t id; 127ebe74f8bSWei Liu uint8_t cancelled; 128ebe74f8bSWei Liu CoQueue complete; 129ebe74f8bSWei Liu struct V9fsState *s; 130ebe74f8bSWei Liu QLIST_ENTRY(V9fsPDU) next; 13100588a0aSWei Liu uint32_t idx; 132ebe74f8bSWei Liu }; 133ebe74f8bSWei Liu 134ebe74f8bSWei Liu 135ebe74f8bSWei Liu /* FIXME 136ebe74f8bSWei Liu * 1) change user needs to set groups and stuff 137ebe74f8bSWei Liu */ 138ebe74f8bSWei Liu 139ebe74f8bSWei Liu #define MAX_REQ 128 140ebe74f8bSWei Liu #define MAX_TAG_LEN 32 141ebe74f8bSWei Liu 142ebe74f8bSWei Liu #define BUG_ON(cond) assert(!(cond)) 143ebe74f8bSWei Liu 144ebe74f8bSWei Liu typedef struct V9fsFidState V9fsFidState; 145ebe74f8bSWei Liu 146ebe74f8bSWei Liu enum { 147ebe74f8bSWei Liu P9_FID_NONE = 0, 148ebe74f8bSWei Liu P9_FID_FILE, 149ebe74f8bSWei Liu P9_FID_DIR, 150ebe74f8bSWei Liu P9_FID_XATTR, 151ebe74f8bSWei Liu }; 152ebe74f8bSWei Liu 153ebe74f8bSWei Liu typedef struct V9fsConf 154ebe74f8bSWei Liu { 155ebe74f8bSWei Liu /* tag name for the device */ 156ebe74f8bSWei Liu char *tag; 157ebe74f8bSWei Liu char *fsdev_id; 158ebe74f8bSWei Liu } V9fsConf; 159ebe74f8bSWei Liu 160ebe74f8bSWei Liu typedef struct V9fsXattr 161ebe74f8bSWei Liu { 1628495f9adSLi Qiang uint64_t copied_len; 1638495f9adSLi Qiang uint64_t len; 164ebe74f8bSWei Liu void *value; 165ebe74f8bSWei Liu V9fsString name; 166ebe74f8bSWei Liu int flags; 167dd28fbbcSLi Qiang bool xattrwalk_fid; 168ebe74f8bSWei Liu } V9fsXattr; 169ebe74f8bSWei Liu 170f314ea4eSGreg Kurz typedef struct V9fsDir { 171f314ea4eSGreg Kurz DIR *stream; 1727cde47d4SGreg Kurz QemuMutex readdir_mutex; 173f314ea4eSGreg Kurz } V9fsDir; 174f314ea4eSGreg Kurz 1757cde47d4SGreg Kurz static inline void v9fs_readdir_lock(V9fsDir *dir) 1767cde47d4SGreg Kurz { 1777cde47d4SGreg Kurz qemu_mutex_lock(&dir->readdir_mutex); 1787cde47d4SGreg Kurz } 1797cde47d4SGreg Kurz 1807cde47d4SGreg Kurz static inline void v9fs_readdir_unlock(V9fsDir *dir) 1817cde47d4SGreg Kurz { 1827cde47d4SGreg Kurz qemu_mutex_unlock(&dir->readdir_mutex); 1837cde47d4SGreg Kurz } 1847cde47d4SGreg Kurz 1857cde47d4SGreg Kurz static inline void v9fs_readdir_init(V9fsDir *dir) 1867cde47d4SGreg Kurz { 1877cde47d4SGreg Kurz qemu_mutex_init(&dir->readdir_mutex); 1887cde47d4SGreg Kurz } 1897cde47d4SGreg Kurz 190ebe74f8bSWei Liu /* 191ebe74f8bSWei Liu * Filled by fs driver on open and other 192ebe74f8bSWei Liu * calls. 193ebe74f8bSWei Liu */ 194ebe74f8bSWei Liu union V9fsFidOpenState { 195ebe74f8bSWei Liu int fd; 196f314ea4eSGreg Kurz V9fsDir dir; 197ebe74f8bSWei Liu V9fsXattr xattr; 198ebe74f8bSWei Liu /* 199ebe74f8bSWei Liu * private pointer for fs drivers, that 200ebe74f8bSWei Liu * have its own internal representation of 201ebe74f8bSWei Liu * open files. 202ebe74f8bSWei Liu */ 203ebe74f8bSWei Liu void *private; 204ebe74f8bSWei Liu }; 205ebe74f8bSWei Liu 206ebe74f8bSWei Liu struct V9fsFidState 207ebe74f8bSWei Liu { 208ebe74f8bSWei Liu int fid_type; 209ebe74f8bSWei Liu int32_t fid; 210ebe74f8bSWei Liu V9fsPath path; 211ebe74f8bSWei Liu V9fsFidOpenState fs; 212ebe74f8bSWei Liu V9fsFidOpenState fs_reclaim; 213ebe74f8bSWei Liu int flags; 214ebe74f8bSWei Liu int open_flags; 215ebe74f8bSWei Liu uid_t uid; 216ebe74f8bSWei Liu int ref; 217ebe74f8bSWei Liu int clunked; 218ebe74f8bSWei Liu V9fsFidState *next; 219ebe74f8bSWei Liu V9fsFidState *rclm_lst; 220ebe74f8bSWei Liu }; 221ebe74f8bSWei Liu 222ebe74f8bSWei Liu typedef struct V9fsState 223ebe74f8bSWei Liu { 224ebe74f8bSWei Liu QLIST_HEAD(, V9fsPDU) free_list; 225ebe74f8bSWei Liu QLIST_HEAD(, V9fsPDU) active_list; 226ebe74f8bSWei Liu V9fsFidState *fid_list; 227ebe74f8bSWei Liu FileOperations *ops; 228ebe74f8bSWei Liu FsContext ctx; 229ebe74f8bSWei Liu char *tag; 230ebe74f8bSWei Liu enum p9_proto_version proto_version; 231ebe74f8bSWei Liu int32_t msize; 232583f21f8SStefano Stabellini V9fsPDU pdus[MAX_REQ]; 233ea83441cSStefano Stabellini const struct V9fsTransport *transport; 234ebe74f8bSWei Liu /* 235ebe74f8bSWei Liu * lock ensuring atomic path update 236ebe74f8bSWei Liu * on rename. 237ebe74f8bSWei Liu */ 238ebe74f8bSWei Liu CoRwlock rename_lock; 239ebe74f8bSWei Liu int32_t root_fid; 240ebe74f8bSWei Liu Error *migration_blocker; 241ebe74f8bSWei Liu V9fsConf fsconf; 24256f101ecSGreg Kurz V9fsQID root_qid; 243ebe74f8bSWei Liu } V9fsState; 244ebe74f8bSWei Liu 245ebe74f8bSWei Liu /* 9p2000.L open flags */ 246ebe74f8bSWei Liu #define P9_DOTL_RDONLY 00000000 247ebe74f8bSWei Liu #define P9_DOTL_WRONLY 00000001 248ebe74f8bSWei Liu #define P9_DOTL_RDWR 00000002 249ebe74f8bSWei Liu #define P9_DOTL_NOACCESS 00000003 250ebe74f8bSWei Liu #define P9_DOTL_CREATE 00000100 251ebe74f8bSWei Liu #define P9_DOTL_EXCL 00000200 252ebe74f8bSWei Liu #define P9_DOTL_NOCTTY 00000400 253ebe74f8bSWei Liu #define P9_DOTL_TRUNC 00001000 254ebe74f8bSWei Liu #define P9_DOTL_APPEND 00002000 255ebe74f8bSWei Liu #define P9_DOTL_NONBLOCK 00004000 256ebe74f8bSWei Liu #define P9_DOTL_DSYNC 00010000 257ebe74f8bSWei Liu #define P9_DOTL_FASYNC 00020000 258ebe74f8bSWei Liu #define P9_DOTL_DIRECT 00040000 259ebe74f8bSWei Liu #define P9_DOTL_LARGEFILE 00100000 260ebe74f8bSWei Liu #define P9_DOTL_DIRECTORY 00200000 261ebe74f8bSWei Liu #define P9_DOTL_NOFOLLOW 00400000 262ebe74f8bSWei Liu #define P9_DOTL_NOATIME 01000000 263ebe74f8bSWei Liu #define P9_DOTL_CLOEXEC 02000000 264ebe74f8bSWei Liu #define P9_DOTL_SYNC 04000000 265ebe74f8bSWei Liu 266ebe74f8bSWei Liu /* 9p2000.L at flags */ 267ebe74f8bSWei Liu #define P9_DOTL_AT_REMOVEDIR 0x200 268ebe74f8bSWei Liu 269ebe74f8bSWei Liu /* 9P2000.L lock type */ 270ebe74f8bSWei Liu #define P9_LOCK_TYPE_RDLCK 0 271ebe74f8bSWei Liu #define P9_LOCK_TYPE_WRLCK 1 272ebe74f8bSWei Liu #define P9_LOCK_TYPE_UNLCK 2 273ebe74f8bSWei Liu 274ebe74f8bSWei Liu #define P9_LOCK_SUCCESS 0 275ebe74f8bSWei Liu #define P9_LOCK_BLOCKED 1 276ebe74f8bSWei Liu #define P9_LOCK_ERROR 2 277ebe74f8bSWei Liu #define P9_LOCK_GRACE 3 278ebe74f8bSWei Liu 279ebe74f8bSWei Liu #define P9_LOCK_FLAGS_BLOCK 1 280ebe74f8bSWei Liu #define P9_LOCK_FLAGS_RECLAIM 2 281ebe74f8bSWei Liu 282ebe74f8bSWei Liu typedef struct V9fsFlock 283ebe74f8bSWei Liu { 284ebe74f8bSWei Liu uint8_t type; 285ebe74f8bSWei Liu uint32_t flags; 286ebe74f8bSWei Liu uint64_t start; /* absolute offset */ 287ebe74f8bSWei Liu uint64_t length; 288ebe74f8bSWei Liu uint32_t proc_id; 289ebe74f8bSWei Liu V9fsString client_id; 290ebe74f8bSWei Liu } V9fsFlock; 291ebe74f8bSWei Liu 292ebe74f8bSWei Liu typedef struct V9fsGetlock 293ebe74f8bSWei Liu { 294ebe74f8bSWei Liu uint8_t type; 295ebe74f8bSWei Liu uint64_t start; /* absolute offset */ 296ebe74f8bSWei Liu uint64_t length; 297ebe74f8bSWei Liu uint32_t proc_id; 298ebe74f8bSWei Liu V9fsString client_id; 299ebe74f8bSWei Liu } V9fsGetlock; 300ebe74f8bSWei Liu 301ebe74f8bSWei Liu extern int open_fd_hw; 302ebe74f8bSWei Liu extern int total_open_fd; 303ebe74f8bSWei Liu 304ebe74f8bSWei Liu static inline void v9fs_path_write_lock(V9fsState *s) 305ebe74f8bSWei Liu { 306ebe74f8bSWei Liu if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { 307ebe74f8bSWei Liu qemu_co_rwlock_wrlock(&s->rename_lock); 308ebe74f8bSWei Liu } 309ebe74f8bSWei Liu } 310ebe74f8bSWei Liu 311ebe74f8bSWei Liu static inline void v9fs_path_read_lock(V9fsState *s) 312ebe74f8bSWei Liu { 313ebe74f8bSWei Liu if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { 314ebe74f8bSWei Liu qemu_co_rwlock_rdlock(&s->rename_lock); 315ebe74f8bSWei Liu } 316ebe74f8bSWei Liu } 317ebe74f8bSWei Liu 318ebe74f8bSWei Liu static inline void v9fs_path_unlock(V9fsState *s) 319ebe74f8bSWei Liu { 320ebe74f8bSWei Liu if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { 321ebe74f8bSWei Liu qemu_co_rwlock_unlock(&s->rename_lock); 322ebe74f8bSWei Liu } 323ebe74f8bSWei Liu } 324ebe74f8bSWei Liu 325ebe74f8bSWei Liu static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu) 326ebe74f8bSWei Liu { 327ebe74f8bSWei Liu return pdu->cancelled; 328ebe74f8bSWei Liu } 329ebe74f8bSWei Liu 3308440e22eSGreg Kurz void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu); 331bc70a592SGreg Kurz void v9fs_path_init(V9fsPath *path); 332bc70a592SGreg Kurz void v9fs_path_free(V9fsPath *path); 333bc70a592SGreg Kurz void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...); 334bc70a592SGreg Kurz void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs); 335bc70a592SGreg Kurz int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, 336ebe74f8bSWei Liu const char *name, V9fsPath *path); 337bc70a592SGreg Kurz int v9fs_device_realize_common(V9fsState *s, Error **errp); 338bc70a592SGreg Kurz void v9fs_device_unrealize_common(V9fsState *s, Error **errp); 339ebe74f8bSWei Liu 340ebe74f8bSWei Liu ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...); 341ebe74f8bSWei Liu ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...); 3424b311c5fSWei Liu V9fsPDU *pdu_alloc(V9fsState *s); 3434b311c5fSWei Liu void pdu_free(V9fsPDU *pdu); 3444b311c5fSWei Liu void pdu_submit(V9fsPDU *pdu); 3450e44a0fdSGreg Kurz void v9fs_reset(V9fsState *s); 346ebe74f8bSWei Liu 347ea83441cSStefano Stabellini struct V9fsTransport { 348ea83441cSStefano Stabellini ssize_t (*pdu_vmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt, 349ea83441cSStefano Stabellini va_list ap); 350ea83441cSStefano Stabellini ssize_t (*pdu_vunmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt, 351ea83441cSStefano Stabellini va_list ap); 35288da0b03SStefano Stabellini void (*init_in_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov, 35388da0b03SStefano Stabellini unsigned int *pniov, size_t size); 35488da0b03SStefano Stabellini void (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov, 35588da0b03SStefano Stabellini unsigned int *pniov); 356ea83441cSStefano Stabellini void (*push_and_notify)(V9fsPDU *pdu); 357ea83441cSStefano Stabellini }; 358ea83441cSStefano Stabellini 359ea83441cSStefano Stabellini static inline int v9fs_register_transport(V9fsState *s, 360ea83441cSStefano Stabellini const struct V9fsTransport *t) 361ea83441cSStefano Stabellini { 362ea83441cSStefano Stabellini assert(!s->transport); 363ea83441cSStefano Stabellini s->transport = t; 364ea83441cSStefano Stabellini return 0; 365ea83441cSStefano Stabellini } 366ea83441cSStefano Stabellini 367ebe74f8bSWei Liu #endif 368