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