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,
103 #define P9_NOTAG UINT16_MAX
104 #define P9_NOFID UINT32_MAX
105 #define P9_MAXWELEM 16
107 #define FID_REFERENCED 0x1
108 #define FID_NON_RECLAIMABLE 0x2
109 static inline char *rpath(FsContext
*ctx
, const char *path
)
111 return g_strdup_printf("%s/%s", ctx
->fs_root
, path
);
115 * ample room for Twrite/Rread header
116 * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
118 #define P9_IOHDRSZ 24
120 typedef struct V9fsPDU V9fsPDU
;
121 typedef struct V9fsState V9fsState
;
122 typedef struct V9fsTransport V9fsTransport
;
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.
133 QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader
) != 7);
143 QLIST_ENTRY(V9fsPDU
) next
;
149 * 1) change user needs to set groups and stuff
153 #define MAX_TAG_LEN 32
155 #define BUG_ON(cond) assert(!(cond))
157 typedef struct V9fsFidState V9fsFidState
;
166 typedef struct V9fsConf
168 /* tag name for the device */
173 /* 9p2000.L xattr flags (matches Linux values) */
174 #define P9_XATTR_CREATE 1
175 #define P9_XATTR_REPLACE 2
177 typedef struct V9fsXattr
187 typedef struct V9fsDir
{
189 QemuMutex readdir_mutex
;
192 static inline void v9fs_readdir_lock(V9fsDir
*dir
)
194 qemu_mutex_lock(&dir
->readdir_mutex
);
197 static inline void v9fs_readdir_unlock(V9fsDir
*dir
)
199 qemu_mutex_unlock(&dir
->readdir_mutex
);
202 static inline void v9fs_readdir_init(V9fsDir
*dir
)
204 qemu_mutex_init(&dir
->readdir_mutex
);
208 * Filled by fs driver on open and other
211 union V9fsFidOpenState
{
216 * private pointer for fs drivers, that
217 * have its own internal representation of
229 V9fsFidOpenState fs_reclaim
;
236 V9fsFidState
*rclm_lst
;
239 typedef enum AffixType_t
{
241 AffixType_Suffix
, /* A.k.a. postfix. */
245 * @brief Unique affix of variable length.
247 * An affix is (currently) either a suffix or a prefix, which is either
248 * going to be prepended (prefix) or appended (suffix) with some other
249 * number for the goal to generate unique numbers. Accordingly the
250 * suffixes (or prefixes) we generate @b must all have the mathematical
251 * property of being suffix-free (or prefix-free in case of prefixes)
252 * so that no matter what number we concatenate the affix with, that we
253 * always reliably get unique numbers as result after concatenation.
255 typedef struct VariLenAffix
{
256 AffixType_t type
; /* Whether this affix is a suffix or a prefix. */
257 uint64_t value
; /* Actual numerical value of this affix. */
259 * Lenght of the affix, that is how many (of the lowest) bits of @c value
260 * must be used for appending/prepending this affix to its final resulting,
266 /* See qid_inode_prefix_hash_bits(). */
268 dev_t dev
; /* FS device on host. */
270 * How many (high) bits of the original inode number shall be used for
276 /* QID path prefix entry, see stat_to_qid */
280 uint32_t qp_affix_index
;
281 VariLenAffix qp_affix
;
284 /* QID path full entry, as above */
293 QLIST_HEAD(, V9fsPDU
) free_list
;
294 QLIST_HEAD(, V9fsPDU
) active_list
;
295 V9fsFidState
*fid_list
;
299 P9ProtoVersion proto_version
;
301 V9fsPDU pdus
[MAX_REQ
];
302 const V9fsTransport
*transport
;
304 * lock ensuring atomic path update
307 CoRwlock rename_lock
;
309 Error
*migration_blocker
;
313 struct qht qpd_table
;
314 struct qht qpp_table
;
315 struct qht qpf_table
;
316 uint64_t qp_ndevices
; /* Amount of entries in qpd_table. */
317 uint16_t qp_affix_next
;
318 uint64_t qp_fullpath_next
;
321 /* 9p2000.L open flags */
322 #define P9_DOTL_RDONLY 00000000
323 #define P9_DOTL_WRONLY 00000001
324 #define P9_DOTL_RDWR 00000002
325 #define P9_DOTL_NOACCESS 00000003
326 #define P9_DOTL_CREATE 00000100
327 #define P9_DOTL_EXCL 00000200
328 #define P9_DOTL_NOCTTY 00000400
329 #define P9_DOTL_TRUNC 00001000
330 #define P9_DOTL_APPEND 00002000
331 #define P9_DOTL_NONBLOCK 00004000
332 #define P9_DOTL_DSYNC 00010000
333 #define P9_DOTL_FASYNC 00020000
334 #define P9_DOTL_DIRECT 00040000
335 #define P9_DOTL_LARGEFILE 00100000
336 #define P9_DOTL_DIRECTORY 00200000
337 #define P9_DOTL_NOFOLLOW 00400000
338 #define P9_DOTL_NOATIME 01000000
339 #define P9_DOTL_CLOEXEC 02000000
340 #define P9_DOTL_SYNC 04000000
342 /* 9p2000.L at flags */
343 #define P9_DOTL_AT_REMOVEDIR 0x200
345 /* 9P2000.L lock type */
346 #define P9_LOCK_TYPE_RDLCK 0
347 #define P9_LOCK_TYPE_WRLCK 1
348 #define P9_LOCK_TYPE_UNLCK 2
350 #define P9_LOCK_SUCCESS 0
351 #define P9_LOCK_BLOCKED 1
352 #define P9_LOCK_ERROR 2
353 #define P9_LOCK_GRACE 3
355 #define P9_LOCK_FLAGS_BLOCK 1
356 #define P9_LOCK_FLAGS_RECLAIM 2
358 typedef struct V9fsFlock
362 uint64_t start
; /* absolute offset */
365 V9fsString client_id
;
368 typedef struct V9fsGetlock
371 uint64_t start
; /* absolute offset */
374 V9fsString client_id
;
377 extern int open_fd_hw
;
378 extern int total_open_fd
;
380 static inline void v9fs_path_write_lock(V9fsState
*s
)
382 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
383 qemu_co_rwlock_wrlock(&s
->rename_lock
);
387 static inline void v9fs_path_read_lock(V9fsState
*s
)
389 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
390 qemu_co_rwlock_rdlock(&s
->rename_lock
);
394 static inline void v9fs_path_unlock(V9fsState
*s
)
396 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
397 qemu_co_rwlock_unlock(&s
->rename_lock
);
401 static inline uint8_t v9fs_request_cancelled(V9fsPDU
*pdu
)
403 return pdu
->cancelled
;
406 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
);
407 void v9fs_path_init(V9fsPath
*path
);
408 void v9fs_path_free(V9fsPath
*path
);
409 void v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...);
410 void v9fs_path_copy(V9fsPath
*dst
, const V9fsPath
*src
);
411 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
412 const char *name
, V9fsPath
*path
);
413 int v9fs_device_realize_common(V9fsState
*s
, const V9fsTransport
*t
,
415 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
);
417 V9fsPDU
*pdu_alloc(V9fsState
*s
);
418 void pdu_free(V9fsPDU
*pdu
);
419 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
);
420 void v9fs_reset(V9fsState
*s
);
422 struct V9fsTransport
{
423 ssize_t (*pdu_vmarshal
)(V9fsPDU
*pdu
, size_t offset
, const char *fmt
,
425 ssize_t (*pdu_vunmarshal
)(V9fsPDU
*pdu
, size_t offset
, const char *fmt
,
427 void (*init_in_iov_from_pdu
)(V9fsPDU
*pdu
, struct iovec
**piov
,
428 unsigned int *pniov
, size_t size
);
429 void (*init_out_iov_from_pdu
)(V9fsPDU
*pdu
, struct iovec
**piov
,
430 unsigned int *pniov
, size_t size
);
431 void (*push_and_notify
)(V9fsPDU
*pdu
);