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