]> git.proxmox.com Git - qemu.git/blame - hw/9pfs/virtio-9p.h
hw/9pfs: Add fs driver specific details to fscontext
[qemu.git] / hw / 9pfs / virtio-9p.h
CommitLineData
9f107513
AL
1#ifndef _QEMU_VIRTIO_9P_H
2#define _QEMU_VIRTIO_9P_H
3
4#include <sys/types.h>
5#include <dirent.h>
6#include <sys/time.h>
7#include <utime.h>
7a462745 8#include <sys/resource.h>
ff06030f 9#include "hw/virtio.h"
353ac78d 10#include "fsdev/file-op-9p.h"
02cb7f3a
AK
11#include "qemu-thread.h"
12#include "qemu-coroutine.h"
9f107513
AL
13
14/* The feature bitmap for virtio 9P */
15/* The mount point is specified in a config variable */
16#define VIRTIO_9P_MOUNT_TAG 0
17
18enum {
8f4d1ca5
AB
19 P9_TLERROR = 6,
20 P9_RLERROR,
be940c87
MK
21 P9_TSTATFS = 8,
22 P9_RSTATFS,
771e9d4c
MK
23 P9_TLOPEN = 12,
24 P9_RLOPEN,
c1568af5
VJJ
25 P9_TLCREATE = 14,
26 P9_RLCREATE,
08c60fc9
VJJ
27 P9_TSYMLINK = 16,
28 P9_RSYMLINK,
5268cecc
MK
29 P9_TMKNOD = 18,
30 P9_RMKNOD,
c7b4b0b3
MK
31 P9_TRENAME = 20,
32 P9_RRENAME,
df0973a4
MK
33 P9_TREADLINK = 22,
34 P9_RREADLINK,
00ede4c2
SK
35 P9_TGETATTR = 24,
36 P9_RGETATTR,
c79ce737
SK
37 P9_TSETATTR = 26,
38 P9_RSETATTR,
fa32ef88
AK
39 P9_TXATTRWALK = 30,
40 P9_RXATTRWALK,
10b468bd
AK
41 P9_TXATTRCREATE = 32,
42 P9_RXATTRCREATE,
c18e2f94
SK
43 P9_TREADDIR = 40,
44 P9_RREADDIR,
b41e95d3
VJJ
45 P9_TFSYNC = 50,
46 P9_RFSYNC,
82cc3ee8
MK
47 P9_TLOCK = 52,
48 P9_RLOCK,
8f354003
MK
49 P9_TGETLOCK = 54,
50 P9_RGETLOCK,
b2c224be
VJJ
51 P9_TLINK = 70,
52 P9_RLINK,
b67592ea
MK
53 P9_TMKDIR = 72,
54 P9_RMKDIR,
89bf6593
AK
55 P9_TRENAMEAT = 74,
56 P9_RRENAMEAT,
7834cf77
AK
57 P9_TUNLINKAT = 76,
58 P9_RUNLINKAT,
9f107513
AL
59 P9_TVERSION = 100,
60 P9_RVERSION,
61 P9_TAUTH = 102,
62 P9_RAUTH,
63 P9_TATTACH = 104,
64 P9_RATTACH,
65 P9_TERROR = 106,
66 P9_RERROR,
67 P9_TFLUSH = 108,
68 P9_RFLUSH,
69 P9_TWALK = 110,
70 P9_RWALK,
71 P9_TOPEN = 112,
72 P9_ROPEN,
73 P9_TCREATE = 114,
74 P9_RCREATE,
75 P9_TREAD = 116,
76 P9_RREAD,
77 P9_TWRITE = 118,
78 P9_RWRITE,
79 P9_TCLUNK = 120,
80 P9_RCLUNK,
81 P9_TREMOVE = 122,
82 P9_RREMOVE,
83 P9_TSTAT = 124,
84 P9_RSTAT,
85 P9_TWSTAT = 126,
86 P9_RWSTAT,
87};
88
89
90/* qid.types */
91enum {
92 P9_QTDIR = 0x80,
93 P9_QTAPPEND = 0x40,
94 P9_QTEXCL = 0x20,
95 P9_QTMOUNT = 0x10,
96 P9_QTAUTH = 0x08,
97 P9_QTTMP = 0x04,
98 P9_QTSYMLINK = 0x02,
99 P9_QTLINK = 0x01,
100 P9_QTFILE = 0x00,
101};
102
84151514
MK
103enum p9_proto_version {
104 V9FS_PROTO_2000U = 0x01,
105 V9FS_PROTO_2000L = 0x02,
106};
107
9f107513
AL
108#define P9_NOTAG (u16)(~0)
109#define P9_NOFID (u32)(~0)
110#define P9_MAXWELEM 16
7a462745
AK
111
112#define FID_REFERENCED 0x1
113#define FID_NON_RECLAIMABLE 0x2
faa44e3d
VJJ
114static inline const char *rpath(FsContext *ctx, const char *path, char *buffer)
115{
116 snprintf(buffer, PATH_MAX, "%s/%s", ctx->fs_root, path);
117 return buffer;
118}
9f107513 119
5e94c103
MK
120/*
121 * ample room for Twrite/Rread header
122 * size[4] Tread/Twrite tag[2] fid[4] offset[8] count[4]
123 */
124#define P9_IOHDRSZ 24
125
9f107513 126typedef struct V9fsPDU V9fsPDU;
ff06030f 127struct V9fsState;
9f107513
AL
128
129struct V9fsPDU
130{
131 uint32_t size;
132 uint16_t tag;
133 uint8_t id;
134 VirtQueueElement elem;
ff06030f 135 struct V9fsState *s;
9f107513
AL
136 QLIST_ENTRY(V9fsPDU) next;
137};
138
139
140/* FIXME
141 * 1) change user needs to set groups and stuff
142 */
143
144/* from Linux's linux/virtio_9p.h */
145
146/* The ID for virtio console */
147#define VIRTIO_ID_9P 9
148#define MAX_REQ 128
149#define MAX_TAG_LEN 32
150
151#define BUG_ON(cond) assert(!(cond))
152
153typedef struct V9fsFidState V9fsFidState;
154
155typedef struct V9fsString
156{
157 int16_t size;
158 char *data;
159} V9fsString;
160
161typedef struct V9fsQID
162{
163 int8_t type;
164 int32_t version;
165 int64_t path;
166} V9fsQID;
167
168typedef struct V9fsStat
169{
170 int16_t size;
171 int16_t type;
172 int32_t dev;
173 V9fsQID qid;
174 int32_t mode;
175 int32_t atime;
176 int32_t mtime;
177 int64_t length;
178 V9fsString name;
179 V9fsString uid;
180 V9fsString gid;
181 V9fsString muid;
182 /* 9p2000.u */
183 V9fsString extension;
184 int32_t n_uid;
185 int32_t n_gid;
186 int32_t n_muid;
187} V9fsStat;
188
d62dbb51
AK
189enum {
190 P9_FID_NONE = 0,
191 P9_FID_FILE,
192 P9_FID_DIR,
193 P9_FID_XATTR,
194};
195
196typedef struct V9fsXattr
197{
198 int64_t copied_len;
199 int64_t len;
200 void *value;
201 V9fsString name;
202 int flags;
203} V9fsXattr;
204
9f107513
AL
205struct V9fsFidState
206{
d62dbb51 207 int fid_type;
9f107513 208 int32_t fid;
2289be19 209 V9fsPath path;
d62dbb51 210 union {
7a462745
AK
211 int fd;
212 DIR *dir;
213 V9fsXattr xattr;
d62dbb51 214 } fs;
7a462745
AK
215 union {
216 int fd;
217 DIR *dir;
218 } fs_reclaim;
219 int flags;
220 int open_flags;
9f107513 221 uid_t uid;
84dfb926
AK
222 int ref;
223 int clunked;
9f107513 224 V9fsFidState *next;
7a462745 225 V9fsFidState *rclm_lst;
9f107513
AL
226};
227
228typedef struct V9fsState
229{
230 VirtIODevice vdev;
231 VirtQueue *vq;
232 V9fsPDU pdus[MAX_REQ];
233 QLIST_HEAD(, V9fsPDU) free_list;
234 V9fsFidState *fid_list;
235 FileOperations *ops;
236 FsContext ctx;
237 uint16_t tag_len;
238 uint8_t *tag;
239 size_t config_size;
84151514 240 enum p9_proto_version proto_version;
5e94c103 241 int32_t msize;
02cb7f3a
AK
242 /*
243 * lock ensuring atomic path update
244 * on rename.
245 */
246 CoRwlock rename_lock;
9f107513
AL
247} V9fsState;
248
fac4f111
VJJ
249typedef struct V9fsStatState {
250 V9fsPDU *pdu;
251 size_t offset;
252 V9fsStat v9stat;
253 V9fsFidState *fidp;
254 struct stat stbuf;
255} V9fsStatState;
256
00ede4c2
SK
257typedef struct V9fsStatDotl {
258 uint64_t st_result_mask;
259 V9fsQID qid;
260 uint32_t st_mode;
261 uint32_t st_uid;
262 uint32_t st_gid;
263 uint64_t st_nlink;
264 uint64_t st_rdev;
265 uint64_t st_size;
266 uint64_t st_blksize;
267 uint64_t st_blocks;
268 uint64_t st_atime_sec;
269 uint64_t st_atime_nsec;
270 uint64_t st_mtime_sec;
271 uint64_t st_mtime_nsec;
272 uint64_t st_ctime_sec;
273 uint64_t st_ctime_nsec;
274 uint64_t st_btime_sec;
275 uint64_t st_btime_nsec;
276 uint64_t st_gen;
277 uint64_t st_data_version;
278} V9fsStatDotl;
279
fac4f111
VJJ
280typedef struct V9fsOpenState {
281 V9fsPDU *pdu;
282 size_t offset;
771e9d4c 283 int32_t mode;
fac4f111
VJJ
284 V9fsFidState *fidp;
285 V9fsQID qid;
286 struct stat stbuf;
5e94c103 287 int iounit;
fac4f111
VJJ
288} V9fsOpenState;
289
290typedef struct V9fsReadState {
291 V9fsPDU *pdu;
292 size_t offset;
293 int32_t count;
294 int32_t total;
295 int64_t off;
296 V9fsFidState *fidp;
297 struct iovec iov[128]; /* FIXME: bad, bad, bad */
298 struct iovec *sg;
299 off_t dir_pos;
300 struct dirent *dent;
301 struct stat stbuf;
302 V9fsString name;
303 V9fsStat v9stat;
304 int32_t len;
305 int32_t cnt;
306 int32_t max_count;
307} V9fsReadState;
308
309typedef struct V9fsWriteState {
310 V9fsPDU *pdu;
311 size_t offset;
312 int32_t len;
313 int32_t count;
314 int32_t total;
315 int64_t off;
316 V9fsFidState *fidp;
317 struct iovec iov[128]; /* FIXME: bad, bad, bad */
318 struct iovec *sg;
319 int cnt;
320} V9fsWriteState;
321
c79ce737
SK
322typedef struct V9fsIattr
323{
324 int32_t valid;
325 int32_t mode;
326 int32_t uid;
327 int32_t gid;
328 int64_t size;
329 int64_t atime_sec;
330 int64_t atime_nsec;
331 int64_t mtime_sec;
332 int64_t mtime_nsec;
333} V9fsIattr;
334
9f107513
AL
335struct virtio_9p_config
336{
337 /* number of characters in tag */
338 uint16_t tag_len;
339 /* Variable size tag name */
340 uint8_t tag[0];
541dc0d4 341} QEMU_PACKED;
9f107513 342
5268cecc
MK
343typedef struct V9fsMkState {
344 V9fsPDU *pdu;
345 size_t offset;
346 V9fsQID qid;
347 struct stat stbuf;
348 V9fsString name;
349 V9fsString fullname;
350} V9fsMkState;
351
82cc3ee8
MK
352#define P9_LOCK_SUCCESS 0
353#define P9_LOCK_BLOCKED 1
354#define P9_LOCK_ERROR 2
355#define P9_LOCK_GRACE 3
356
357#define P9_LOCK_FLAGS_BLOCK 1
358#define P9_LOCK_FLAGS_RECLAIM 2
359
360typedef struct V9fsFlock
361{
362 uint8_t type;
363 uint32_t flags;
364 uint64_t start; /* absolute offset */
365 uint64_t length;
366 uint32_t proc_id;
367 V9fsString client_id;
368} V9fsFlock;
369
8f354003
MK
370typedef struct V9fsGetlock
371{
372 uint8_t type;
373 uint64_t start; /* absolute offset */
374 uint64_t length;
375 uint32_t proc_id;
376 V9fsString client_id;
377} V9fsGetlock;
378
7a462745
AK
379extern int open_fd_hw;
380extern int total_open_fd;
381
64b85a8f
BS
382size_t pdu_packunpack(void *addr, struct iovec *sg, int sg_count,
383 size_t offset, size_t size, int pack);
9f107513
AL
384
385static inline size_t do_pdu_unpack(void *dst, struct iovec *sg, int sg_count,
386 size_t offset, size_t size)
387{
388 return pdu_packunpack(dst, sg, sg_count, offset, size, 0);
389}
390
532decb7
AK
391static inline void v9fs_path_write_lock(V9fsState *s)
392{
393 if (s->ctx.flags & PATHNAME_FSCONTEXT) {
394 qemu_co_rwlock_wrlock(&s->rename_lock);
395 }
396}
397
398static inline void v9fs_path_read_lock(V9fsState *s)
399{
400 if (s->ctx.flags & PATHNAME_FSCONTEXT) {
401 qemu_co_rwlock_rdlock(&s->rename_lock);
402 }
403}
404
405static inline void v9fs_path_unlock(V9fsState *s)
406{
407 if (s->ctx.flags & PATHNAME_FSCONTEXT) {
408 qemu_co_rwlock_unlock(&s->rename_lock);
409 }
410}
411
f4f61d27 412extern void handle_9p_output(VirtIODevice *vdev, VirtQueue *vq);
7a462745
AK
413extern void virtio_9p_set_fd_limit(void);
414extern void v9fs_reclaim_fd(V9fsState *s);
70c18fc0
AK
415extern void v9fs_string_init(V9fsString *str);
416extern void v9fs_string_free(V9fsString *str);
417extern void v9fs_string_null(V9fsString *str);
418extern void v9fs_string_sprintf(V9fsString *str, const char *fmt, ...);
419extern void v9fs_string_copy(V9fsString *lhs, V9fsString *rhs);
2289be19
AK
420extern void v9fs_path_init(V9fsPath *path);
421extern void v9fs_path_free(V9fsPath *path);
422extern void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs);
423extern int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
424 const char *name, V9fsPath *path);
9f107513 425#endif