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"
98 typedef enum P9ProtoVersion
{
99 V9FS_PROTO_2000U
= 0x01,
100 V9FS_PROTO_2000L
= 0x02,
104 * @brief Minimum message size supported by this 9pfs server.
106 * A client establishes a session by sending a Tversion request along with a
107 * 'msize' parameter which suggests the server a maximum message size ever to be
108 * used for communication (for both requests and replies) between client and
109 * server during that session. If client suggests a 'msize' smaller than this
110 * value then session is denied by server with an error response.
112 #define P9_MIN_MSIZE 4096
114 #define P9_NOTAG UINT16_MAX
115 #define P9_NOFID UINT32_MAX
116 #define P9_MAXWELEM 16
118 #define FID_REFERENCED 0x1
119 #define FID_NON_RECLAIMABLE 0x2
120 static inline char *rpath(FsContext
*ctx
, const char *path
)
122 return g_strdup_printf("%s/%s", ctx
->fs_root
, path
);
126 * ample room for Twrite/Rread header
127 * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
129 #define P9_IOHDRSZ 24
131 typedef struct V9fsPDU V9fsPDU
;
132 typedef struct V9fsState V9fsState
;
133 typedef struct V9fsTransport V9fsTransport
;
139 } QEMU_PACKED P9MsgHeader
;
140 /* According to the specification, 9p messages start with a 7-byte header.
141 * Since most of the code uses this header size in literal form, we must be
142 * sure this is indeed the case.
144 QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader
) != 7);
153 QLIST_ENTRY(V9fsPDU
) next
;
159 * 1) change user needs to set groups and stuff
163 #define MAX_TAG_LEN 32
165 #define BUG_ON(cond) assert(!(cond))
167 typedef struct V9fsFidState V9fsFidState
;
176 typedef struct V9fsConf
178 /* tag name for the device */
183 /* 9p2000.L xattr flags (matches Linux values) */
184 #define P9_XATTR_CREATE 1
185 #define P9_XATTR_REPLACE 2
187 typedef struct V9fsXattr
197 typedef struct V9fsDir
{
199 P9ProtoVersion proto_version
;
200 /* readdir mutex type used for 9P2000.u protocol variant */
201 CoMutex readdir_mutex_u
;
202 /* readdir mutex type used for 9P2000.L protocol variant */
203 QemuMutex readdir_mutex_L
;
206 static inline void v9fs_readdir_lock(V9fsDir
*dir
)
208 if (dir
->proto_version
== V9FS_PROTO_2000U
) {
209 qemu_co_mutex_lock(&dir
->readdir_mutex_u
);
211 qemu_mutex_lock(&dir
->readdir_mutex_L
);
215 static inline void v9fs_readdir_unlock(V9fsDir
*dir
)
217 if (dir
->proto_version
== V9FS_PROTO_2000U
) {
218 qemu_co_mutex_unlock(&dir
->readdir_mutex_u
);
220 qemu_mutex_unlock(&dir
->readdir_mutex_L
);
224 static inline void v9fs_readdir_init(P9ProtoVersion proto_version
, V9fsDir
*dir
)
226 dir
->proto_version
= proto_version
;
227 if (proto_version
== V9FS_PROTO_2000U
) {
228 qemu_co_mutex_init(&dir
->readdir_mutex_u
);
230 qemu_mutex_init(&dir
->readdir_mutex_L
);
235 * Type for 9p fs drivers' (a.k.a. 9p backends) result of readdir requests,
236 * which is a chained list of directory entries.
238 typedef struct V9fsDirEnt
{
239 /* mandatory (must not be NULL) information for all readdir requests */
242 * optional (may be NULL): A full stat of each directory entry is just
243 * done if explicitly told to fs driver.
247 * instead of an array, directory entries are always returned as
248 * chained list, that's because the amount of entries retrieved by fs
249 * drivers is dependent on the individual entries' name (since response
250 * messages are size limited), so the final amount cannot be estimated
253 struct V9fsDirEnt
*next
;
257 * Filled by fs driver on open and other
260 union V9fsFidOpenState
{
265 * private pointer for fs drivers, that
266 * have its own internal representation of
272 struct V9fsFidState
{
277 V9fsFidOpenState fs_reclaim
;
283 QSIMPLEQ_ENTRY(V9fsFidState
) next
;
284 QSLIST_ENTRY(V9fsFidState
) reclaim_next
;
287 typedef enum AffixType_t
{
289 AffixType_Suffix
, /* A.k.a. postfix. */
293 * @brief Unique affix of variable length.
295 * An affix is (currently) either a suffix or a prefix, which is either
296 * going to be prepended (prefix) or appended (suffix) with some other
297 * number for the goal to generate unique numbers. Accordingly the
298 * suffixes (or prefixes) we generate @b must all have the mathematical
299 * property of being suffix-free (or prefix-free in case of prefixes)
300 * so that no matter what number we concatenate the affix with, that we
301 * always reliably get unique numbers as result after concatenation.
303 typedef struct VariLenAffix
{
304 AffixType_t type
; /* Whether this affix is a suffix or a prefix. */
305 uint64_t value
; /* Actual numerical value of this affix. */
307 * Lenght of the affix, that is how many (of the lowest) bits of @c value
308 * must be used for appending/prepending this affix to its final resulting,
314 /* See qid_inode_prefix_hash_bits(). */
316 dev_t dev
; /* FS device on host. */
318 * How many (high) bits of the original inode number shall be used for
324 /* QID path prefix entry, see stat_to_qid */
328 uint32_t qp_affix_index
;
329 VariLenAffix qp_affix
;
332 /* QID path full entry, as above */
340 QLIST_HEAD(, V9fsPDU
) free_list
;
341 QLIST_HEAD(, V9fsPDU
) active_list
;
342 QSIMPLEQ_HEAD(, V9fsFidState
) fid_list
;
346 P9ProtoVersion proto_version
;
348 V9fsPDU pdus
[MAX_REQ
];
349 const V9fsTransport
*transport
;
351 * lock ensuring atomic path update
354 CoRwlock rename_lock
;
356 Error
*migration_blocker
;
360 struct qht qpd_table
;
361 struct qht qpp_table
;
362 struct qht qpf_table
;
363 uint64_t qp_ndevices
; /* Amount of entries in qpd_table. */
364 uint16_t qp_affix_next
;
365 uint64_t qp_fullpath_next
;
368 /* 9p2000.L open flags */
369 #define P9_DOTL_RDONLY 00000000
370 #define P9_DOTL_WRONLY 00000001
371 #define P9_DOTL_RDWR 00000002
372 #define P9_DOTL_NOACCESS 00000003
373 #define P9_DOTL_CREATE 00000100
374 #define P9_DOTL_EXCL 00000200
375 #define P9_DOTL_NOCTTY 00000400
376 #define P9_DOTL_TRUNC 00001000
377 #define P9_DOTL_APPEND 00002000
378 #define P9_DOTL_NONBLOCK 00004000
379 #define P9_DOTL_DSYNC 00010000
380 #define P9_DOTL_FASYNC 00020000
381 #define P9_DOTL_DIRECT 00040000
382 #define P9_DOTL_LARGEFILE 00100000
383 #define P9_DOTL_DIRECTORY 00200000
384 #define P9_DOTL_NOFOLLOW 00400000
385 #define P9_DOTL_NOATIME 01000000
386 #define P9_DOTL_CLOEXEC 02000000
387 #define P9_DOTL_SYNC 04000000
389 /* 9p2000.L at flags */
390 #define P9_DOTL_AT_REMOVEDIR 0x200
392 /* 9P2000.L lock type */
393 #define P9_LOCK_TYPE_RDLCK 0
394 #define P9_LOCK_TYPE_WRLCK 1
395 #define P9_LOCK_TYPE_UNLCK 2
397 #define P9_LOCK_SUCCESS 0
398 #define P9_LOCK_BLOCKED 1
399 #define P9_LOCK_ERROR 2
400 #define P9_LOCK_GRACE 3
402 #define P9_LOCK_FLAGS_BLOCK 1
403 #define P9_LOCK_FLAGS_RECLAIM 2
405 typedef struct V9fsFlock
409 uint64_t start
; /* absolute offset */
412 V9fsString client_id
;
415 typedef struct V9fsGetlock
418 uint64_t start
; /* absolute offset */
421 V9fsString client_id
;
424 extern int open_fd_hw
;
425 extern int total_open_fd
;
427 static inline void v9fs_path_write_lock(V9fsState
*s
)
429 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
430 qemu_co_rwlock_wrlock(&s
->rename_lock
);
434 static inline void v9fs_path_read_lock(V9fsState
*s
)
436 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
437 qemu_co_rwlock_rdlock(&s
->rename_lock
);
441 static inline void v9fs_path_unlock(V9fsState
*s
)
443 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
444 qemu_co_rwlock_unlock(&s
->rename_lock
);
448 static inline uint8_t v9fs_request_cancelled(V9fsPDU
*pdu
)
450 return pdu
->cancelled
;
453 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
);
454 void v9fs_path_init(V9fsPath
*path
);
455 void v9fs_path_free(V9fsPath
*path
);
456 void v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...);
457 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
);
458 size_t v9fs_readdir_response_size(V9fsString
*name
);
459 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
460 const char *name
, V9fsPath
*path
);
461 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
463 void v9fs_device_unrealize_common(V9fsState
*s
);
465 V9fsPDU
*pdu_alloc(V9fsState
*s
);
466 void pdu_free(V9fsPDU
*pdu
);
467 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
);
468 void v9fs_reset(V9fsState
*s
);
470 struct V9fsTransport
{
471 ssize_t (*pdu_vmarshal
)(V9fsPDU
*pdu
, size_t offset
, const char *fmt
,
473 ssize_t (*pdu_vunmarshal
)(V9fsPDU
*pdu
, size_t offset
, const char *fmt
,
475 void (*init_in_iov_from_pdu
)(V9fsPDU
*pdu
, struct iovec
**piov
,
476 unsigned int *pniov
, size_t size
);
477 void (*init_out_iov_from_pdu
)(V9fsPDU
*pdu
, struct iovec
**piov
,
478 unsigned int *pniov
, size_t size
);
479 void (*push_and_notify
)(V9fsPDU
*pdu
);