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"
97 typedef enum P9ProtoVersion {
98 V9FS_PROTO_2000U = 0x01,
99 V9FS_PROTO_2000L = 0x02,
102 #define P9_NOTAG UINT16_MAX
103 #define P9_NOFID UINT32_MAX
104 #define P9_MAXWELEM 16
106 #define FID_REFERENCED 0x1
107 #define FID_NON_RECLAIMABLE 0x2
108 static inline char *rpath(FsContext *ctx, const char *path)
110 return g_strdup_printf("%s/%s", ctx->fs_root, path);
114 * ample room for Twrite/Rread header
115 * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
117 #define P9_IOHDRSZ 24
119 typedef struct V9fsPDU V9fsPDU;
120 typedef struct V9fsState V9fsState;
121 typedef struct V9fsTransport V9fsTransport;
127 } QEMU_PACKED P9MsgHeader;
128 /* According to the specification, 9p messages start with a 7-byte header.
129 * Since most of the code uses this header size in literal form, we must be
130 * sure this is indeed the case.
132 QEMU_BUILD_BUG_ON(sizeof(P9MsgHeader) != 7);
142 QLIST_ENTRY(V9fsPDU) next;
148 * 1) change user needs to set groups and stuff
152 #define MAX_TAG_LEN 32
154 #define BUG_ON(cond) assert(!(cond))
156 typedef struct V9fsFidState V9fsFidState;
165 typedef struct V9fsConf
167 /* tag name for the device */
172 /* 9p2000.L xattr flags (matches Linux values) */
173 #define P9_XATTR_CREATE 1
174 #define P9_XATTR_REPLACE 2
176 typedef struct V9fsXattr
186 typedef struct V9fsDir {
188 QemuMutex readdir_mutex;
191 static inline void v9fs_readdir_lock(V9fsDir *dir)
193 qemu_mutex_lock(&dir->readdir_mutex);
196 static inline void v9fs_readdir_unlock(V9fsDir *dir)
198 qemu_mutex_unlock(&dir->readdir_mutex);
201 static inline void v9fs_readdir_init(V9fsDir *dir)
203 qemu_mutex_init(&dir->readdir_mutex);
207 * Filled by fs driver on open and other
210 union V9fsFidOpenState {
215 * private pointer for fs drivers, that
216 * have its own internal representation of
228 V9fsFidOpenState fs_reclaim;
235 V9fsFidState *rclm_lst;
240 QLIST_HEAD(, V9fsPDU) free_list;
241 QLIST_HEAD(, V9fsPDU) active_list;
242 V9fsFidState *fid_list;
246 P9ProtoVersion proto_version;
248 V9fsPDU pdus[MAX_REQ];
249 const V9fsTransport *transport;
251 * lock ensuring atomic path update
254 CoRwlock rename_lock;
256 Error *migration_blocker;
261 /* 9p2000.L open flags */
262 #define P9_DOTL_RDONLY 00000000
263 #define P9_DOTL_WRONLY 00000001
264 #define P9_DOTL_RDWR 00000002
265 #define P9_DOTL_NOACCESS 00000003
266 #define P9_DOTL_CREATE 00000100
267 #define P9_DOTL_EXCL 00000200
268 #define P9_DOTL_NOCTTY 00000400
269 #define P9_DOTL_TRUNC 00001000
270 #define P9_DOTL_APPEND 00002000
271 #define P9_DOTL_NONBLOCK 00004000
272 #define P9_DOTL_DSYNC 00010000
273 #define P9_DOTL_FASYNC 00020000
274 #define P9_DOTL_DIRECT 00040000
275 #define P9_DOTL_LARGEFILE 00100000
276 #define P9_DOTL_DIRECTORY 00200000
277 #define P9_DOTL_NOFOLLOW 00400000
278 #define P9_DOTL_NOATIME 01000000
279 #define P9_DOTL_CLOEXEC 02000000
280 #define P9_DOTL_SYNC 04000000
282 /* 9p2000.L at flags */
283 #define P9_DOTL_AT_REMOVEDIR 0x200
285 /* 9P2000.L lock type */
286 #define P9_LOCK_TYPE_RDLCK 0
287 #define P9_LOCK_TYPE_WRLCK 1
288 #define P9_LOCK_TYPE_UNLCK 2
290 #define P9_LOCK_SUCCESS 0
291 #define P9_LOCK_BLOCKED 1
292 #define P9_LOCK_ERROR 2
293 #define P9_LOCK_GRACE 3
295 #define P9_LOCK_FLAGS_BLOCK 1
296 #define P9_LOCK_FLAGS_RECLAIM 2
298 typedef struct V9fsFlock
302 uint64_t start; /* absolute offset */
305 V9fsString client_id;
308 typedef struct V9fsGetlock
311 uint64_t start; /* absolute offset */
314 V9fsString client_id;
317 extern int open_fd_hw;
318 extern int total_open_fd;
320 static inline void v9fs_path_write_lock(V9fsState *s)
322 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
323 qemu_co_rwlock_wrlock(&s->rename_lock);
327 static inline void v9fs_path_read_lock(V9fsState *s)
329 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
330 qemu_co_rwlock_rdlock(&s->rename_lock);
334 static inline void v9fs_path_unlock(V9fsState *s)
336 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
337 qemu_co_rwlock_unlock(&s->rename_lock);
341 static inline uint8_t v9fs_request_cancelled(V9fsPDU *pdu)
343 return pdu->cancelled;
346 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu);
347 void v9fs_path_init(V9fsPath *path);
348 void v9fs_path_free(V9fsPath *path);
349 void v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...);
350 void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src);
351 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
352 const char *name, V9fsPath *path);
353 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
355 void v9fs_device_unrealize_common(V9fsState *s, Error **errp);
357 V9fsPDU *pdu_alloc(V9fsState *s);
358 void pdu_free(V9fsPDU *pdu);
359 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr);
360 void v9fs_reset(V9fsState *s);
362 struct V9fsTransport {
363 ssize_t (*pdu_vmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
365 ssize_t (*pdu_vunmarshal)(V9fsPDU *pdu, size_t offset, const char *fmt,
367 void (*init_in_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
368 unsigned int *pniov, size_t size);
369 void (*init_out_iov_from_pdu)(V9fsPDU *pdu, struct iovec **piov,
370 unsigned int *pniov, size_t size);
371 void (*push_and_notify)(V9fsPDU *pdu);