]>
Commit | Line | Data |
---|---|---|
2d888c09 AF |
1 | /* |
2 | * QTest testcase for VirtIO 9P | |
3 | * | |
4 | * Copyright (c) 2014 SUSE LINUX Products GmbH | |
5 | * | |
6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
7 | * See the COPYING file in the top-level directory. | |
8 | */ | |
9 | ||
fbc04127 | 10 | #include "qemu/osdep.h" |
dd210749 | 11 | #include "libqtest-single.h" |
0b8fa32f | 12 | #include "qemu/module.h" |
6cc9906b | 13 | #include "hw/9pfs/9p.h" |
2893ddd5 | 14 | #include "hw/9pfs/9p-synth.h" |
dfbe8b43 EGE |
15 | #include "libqos/virtio-9p.h" |
16 | #include "libqos/qgraph.h" | |
2d888c09 | 17 | |
65b70fc7 | 18 | #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000) |
dfbe8b43 | 19 | static QGuestAllocator *alloc; |
65b70fc7 | 20 | |
653daf38 CS |
21 | /* |
22 | * Used to auto generate new fids. Start with arbitrary high value to avoid | |
23 | * collision with hard coded fids in basic test code. | |
24 | */ | |
25 | static uint32_t fid_generator = 1000; | |
26 | ||
27 | static uint32_t genfid(void) | |
28 | { | |
29 | return fid_generator++; | |
30 | } | |
31 | ||
32 | /** | |
33 | * Splits the @a in string by @a delim into individual (non empty) strings | |
34 | * and outputs them to @a out. The output array @a out is NULL terminated. | |
35 | * | |
36 | * Output array @a out must be freed by calling split_free(). | |
37 | * | |
38 | * @returns number of individual elements in output array @a out (without the | |
39 | * final NULL terminating element) | |
40 | */ | |
41 | static int split(const char *in, const char *delim, char ***out) | |
42 | { | |
43 | int n = 0, i = 0; | |
44 | char *tmp, *p; | |
45 | ||
46 | tmp = g_strdup(in); | |
47 | for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) { | |
48 | if (strlen(p) > 0) { | |
49 | ++n; | |
50 | } | |
51 | } | |
52 | g_free(tmp); | |
53 | ||
54 | *out = g_new0(char *, n + 1); /* last element NULL delimiter */ | |
55 | ||
56 | tmp = g_strdup(in); | |
57 | for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) { | |
58 | if (strlen(p) > 0) { | |
59 | (*out)[i++] = g_strdup(p); | |
60 | } | |
61 | } | |
62 | g_free(tmp); | |
63 | ||
64 | return n; | |
65 | } | |
66 | ||
67 | static void split_free(char ***out) | |
68 | { | |
69 | int i; | |
70 | for (i = 0; (*out)[i]; ++i) { | |
71 | g_free((*out)[i]); | |
72 | } | |
73 | g_free(*out); | |
74 | *out = NULL; | |
75 | } | |
76 | ||
dfbe8b43 | 77 | static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc) |
557a4cc0 | 78 | { |
dfbe8b43 EGE |
79 | QVirtio9P *v9p = obj; |
80 | alloc = t_alloc; | |
81 | size_t tag_len = qvirtio_config_readw(v9p->vdev, 0); | |
557a4cc0 GK |
82 | char *tag; |
83 | int i; | |
557a4cc0 | 84 | |
dfbe8b43 | 85 | g_assert_cmpint(tag_len, ==, strlen(MOUNT_TAG)); |
557a4cc0 GK |
86 | |
87 | tag = g_malloc(tag_len); | |
88 | for (i = 0; i < tag_len; i++) { | |
dfbe8b43 | 89 | tag[i] = qvirtio_config_readb(v9p->vdev, i + 2); |
557a4cc0 | 90 | } |
dfbe8b43 | 91 | g_assert_cmpmem(tag, tag_len, MOUNT_TAG, tag_len); |
557a4cc0 | 92 | g_free(tag); |
1211d81b GK |
93 | } |
94 | ||
6cc9906b GK |
95 | #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */ |
96 | ||
97 | typedef struct { | |
1999a70a | 98 | QTestState *qts; |
dfbe8b43 | 99 | QVirtio9P *v9p; |
6cc9906b GK |
100 | uint16_t tag; |
101 | uint64_t t_msg; | |
102 | uint32_t t_size; | |
103 | uint64_t r_msg; | |
104 | /* No r_size, it is hardcoded to P9_MAX_SIZE */ | |
105 | size_t t_off; | |
106 | size_t r_off; | |
65b70fc7 | 107 | uint32_t free_head; |
6cc9906b GK |
108 | } P9Req; |
109 | ||
110 | static void v9fs_memwrite(P9Req *req, const void *addr, size_t len) | |
111 | { | |
1999a70a | 112 | qtest_memwrite(req->qts, req->t_msg + req->t_off, addr, len); |
6cc9906b GK |
113 | req->t_off += len; |
114 | } | |
115 | ||
116 | static void v9fs_memskip(P9Req *req, size_t len) | |
117 | { | |
118 | req->r_off += len; | |
119 | } | |
120 | ||
6cc9906b GK |
121 | static void v9fs_memread(P9Req *req, void *addr, size_t len) |
122 | { | |
1999a70a | 123 | qtest_memread(req->qts, req->r_msg + req->r_off, addr, len); |
6cc9906b GK |
124 | req->r_off += len; |
125 | } | |
126 | ||
4829469f CS |
127 | static void v9fs_uint8_read(P9Req *req, uint8_t *val) |
128 | { | |
129 | v9fs_memread(req, val, 1); | |
130 | } | |
131 | ||
6cc9906b GK |
132 | static void v9fs_uint16_write(P9Req *req, uint16_t val) |
133 | { | |
134 | uint16_t le_val = cpu_to_le16(val); | |
135 | ||
136 | v9fs_memwrite(req, &le_val, 2); | |
137 | } | |
138 | ||
139 | static void v9fs_uint16_read(P9Req *req, uint16_t *val) | |
140 | { | |
141 | v9fs_memread(req, val, 2); | |
142 | le16_to_cpus(val); | |
143 | } | |
144 | ||
145 | static void v9fs_uint32_write(P9Req *req, uint32_t val) | |
146 | { | |
147 | uint32_t le_val = cpu_to_le32(val); | |
148 | ||
149 | v9fs_memwrite(req, &le_val, 4); | |
150 | } | |
151 | ||
354b86f8 GK |
152 | static void v9fs_uint64_write(P9Req *req, uint64_t val) |
153 | { | |
154 | uint64_t le_val = cpu_to_le64(val); | |
155 | ||
156 | v9fs_memwrite(req, &le_val, 8); | |
157 | } | |
158 | ||
6cc9906b GK |
159 | static void v9fs_uint32_read(P9Req *req, uint32_t *val) |
160 | { | |
161 | v9fs_memread(req, val, 4); | |
162 | le32_to_cpus(val); | |
163 | } | |
164 | ||
4829469f CS |
165 | static void v9fs_uint64_read(P9Req *req, uint64_t *val) |
166 | { | |
167 | v9fs_memread(req, val, 8); | |
168 | le64_to_cpus(val); | |
169 | } | |
170 | ||
6cc9906b GK |
171 | /* len[2] string[len] */ |
172 | static uint16_t v9fs_string_size(const char *string) | |
173 | { | |
174 | size_t len = strlen(string); | |
175 | ||
9ea776ee | 176 | g_assert_cmpint(len, <=, UINT16_MAX - 2); |
6cc9906b GK |
177 | |
178 | return 2 + len; | |
179 | } | |
180 | ||
181 | static void v9fs_string_write(P9Req *req, const char *string) | |
182 | { | |
183 | int len = strlen(string); | |
184 | ||
185 | g_assert_cmpint(len, <=, UINT16_MAX); | |
186 | ||
187 | v9fs_uint16_write(req, (uint16_t) len); | |
188 | v9fs_memwrite(req, string, len); | |
189 | } | |
190 | ||
191 | static void v9fs_string_read(P9Req *req, uint16_t *len, char **string) | |
192 | { | |
193 | uint16_t local_len; | |
194 | ||
195 | v9fs_uint16_read(req, &local_len); | |
196 | if (len) { | |
197 | *len = local_len; | |
198 | } | |
199 | if (string) { | |
2e2293c2 | 200 | *string = g_malloc(local_len + 1); |
6cc9906b | 201 | v9fs_memread(req, *string, local_len); |
2e2293c2 | 202 | (*string)[local_len] = 0; |
6cc9906b GK |
203 | } else { |
204 | v9fs_memskip(req, local_len); | |
205 | } | |
206 | } | |
207 | ||
208 | typedef struct { | |
209 | uint32_t size; | |
210 | uint8_t id; | |
211 | uint16_t tag; | |
212 | } QEMU_PACKED P9Hdr; | |
213 | ||
dfbe8b43 | 214 | static P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id, |
6cc9906b GK |
215 | uint16_t tag) |
216 | { | |
217 | P9Req *req = g_new0(P9Req, 1); | |
9ea776ee | 218 | uint32_t total_size = 7; /* 9P header has well-known size of 7 bytes */ |
6cc9906b | 219 | P9Hdr hdr = { |
6cc9906b GK |
220 | .id = id, |
221 | .tag = cpu_to_le16(tag) | |
222 | }; | |
223 | ||
9ea776ee GK |
224 | g_assert_cmpint(total_size, <=, UINT32_MAX - size); |
225 | total_size += size; | |
226 | hdr.size = cpu_to_le32(total_size); | |
227 | ||
228 | g_assert_cmpint(total_size, <=, P9_MAX_SIZE); | |
6cc9906b | 229 | |
1999a70a | 230 | req->qts = global_qtest; |
6cc9906b | 231 | req->v9p = v9p; |
9ea776ee | 232 | req->t_size = total_size; |
dfbe8b43 | 233 | req->t_msg = guest_alloc(alloc, req->t_size); |
6cc9906b GK |
234 | v9fs_memwrite(req, &hdr, 7); |
235 | req->tag = tag; | |
236 | return req; | |
237 | } | |
238 | ||
239 | static void v9fs_req_send(P9Req *req) | |
240 | { | |
dfbe8b43 | 241 | QVirtio9P *v9p = req->v9p; |
6cc9906b | 242 | |
dfbe8b43 | 243 | req->r_msg = guest_alloc(alloc, P9_MAX_SIZE); |
1999a70a TH |
244 | req->free_head = qvirtqueue_add(req->qts, v9p->vq, req->t_msg, req->t_size, |
245 | false, true); | |
246 | qvirtqueue_add(req->qts, v9p->vq, req->r_msg, P9_MAX_SIZE, true, false); | |
247 | qvirtqueue_kick(req->qts, v9p->vdev, v9p->vq, req->free_head); | |
6cc9906b GK |
248 | req->t_off = 0; |
249 | } | |
250 | ||
6e37f458 GK |
251 | static const char *rmessage_name(uint8_t id) |
252 | { | |
253 | return | |
254 | id == P9_RLERROR ? "RLERROR" : | |
255 | id == P9_RVERSION ? "RVERSION" : | |
256 | id == P9_RATTACH ? "RATTACH" : | |
257 | id == P9_RWALK ? "RWALK" : | |
82469aae | 258 | id == P9_RLOPEN ? "RLOPEN" : |
354b86f8 | 259 | id == P9_RWRITE ? "RWRITE" : |
653daf38 | 260 | id == P9_RMKDIR ? "RMKDIR" : |
b09dbfdd | 261 | id == P9_RLCREATE ? "RLCREATE" : |
b37d62d6 | 262 | id == P9_RUNLINKAT ? "RUNLINKAT" : |
357e2f7f | 263 | id == P9_RFLUSH ? "RFLUSH" : |
4829469f | 264 | id == P9_RREADDIR ? "READDIR" : |
6e37f458 GK |
265 | "<unknown>"; |
266 | } | |
267 | ||
357e2f7f | 268 | static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len) |
6cc9906b | 269 | { |
dfbe8b43 | 270 | QVirtio9P *v9p = req->v9p; |
6cc9906b | 271 | |
1999a70a | 272 | qvirtio_wait_used_elem(req->qts, v9p->vdev, v9p->vq, req->free_head, len, |
65b70fc7 | 273 | QVIRTIO_9P_TIMEOUT_US); |
60b1fa9d GK |
274 | } |
275 | ||
276 | static void v9fs_req_recv(P9Req *req, uint8_t id) | |
277 | { | |
278 | P9Hdr hdr; | |
6cc9906b | 279 | |
65b70fc7 GK |
280 | v9fs_memread(req, &hdr, 7); |
281 | hdr.size = ldl_le_p(&hdr.size); | |
282 | hdr.tag = lduw_le_p(&hdr.tag); | |
6cc9906b GK |
283 | |
284 | g_assert_cmpint(hdr.size, >=, 7); | |
285 | g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE); | |
286 | g_assert_cmpint(hdr.tag, ==, req->tag); | |
287 | ||
6e37f458 GK |
288 | if (hdr.id != id) { |
289 | g_printerr("Received response %d (%s) instead of %d (%s)\n", | |
290 | hdr.id, rmessage_name(hdr.id), id, rmessage_name(id)); | |
291 | ||
292 | if (hdr.id == P9_RLERROR) { | |
293 | uint32_t err; | |
294 | v9fs_uint32_read(req, &err); | |
295 | g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err)); | |
296 | } | |
6cc9906b GK |
297 | } |
298 | g_assert_cmpint(hdr.id, ==, id); | |
299 | } | |
300 | ||
301 | static void v9fs_req_free(P9Req *req) | |
302 | { | |
dfbe8b43 EGE |
303 | guest_free(alloc, req->t_msg); |
304 | guest_free(alloc, req->r_msg); | |
6cc9906b GK |
305 | g_free(req); |
306 | } | |
307 | ||
ba0d1037 GK |
308 | /* size[4] Rlerror tag[2] ecode[4] */ |
309 | static void v9fs_rlerror(P9Req *req, uint32_t *err) | |
310 | { | |
311 | v9fs_req_recv(req, P9_RLERROR); | |
312 | v9fs_uint32_read(req, err); | |
313 | v9fs_req_free(req); | |
314 | } | |
315 | ||
6cc9906b | 316 | /* size[4] Tversion tag[2] msize[4] version[s] */ |
dfbe8b43 | 317 | static P9Req *v9fs_tversion(QVirtio9P *v9p, uint32_t msize, const char *version, |
693b21d2 | 318 | uint16_t tag) |
6cc9906b | 319 | { |
9ea776ee GK |
320 | P9Req *req; |
321 | uint32_t body_size = 4; | |
322 | uint16_t string_size = v9fs_string_size(version); | |
323 | ||
324 | g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); | |
325 | body_size += string_size; | |
326 | req = v9fs_req_init(v9p, body_size, P9_TVERSION, tag); | |
6cc9906b GK |
327 | |
328 | v9fs_uint32_write(req, msize); | |
329 | v9fs_string_write(req, version); | |
330 | v9fs_req_send(req); | |
331 | return req; | |
332 | } | |
333 | ||
334 | /* size[4] Rversion tag[2] msize[4] version[s] */ | |
335 | static void v9fs_rversion(P9Req *req, uint16_t *len, char **version) | |
336 | { | |
337 | uint32_t msize; | |
338 | ||
339 | v9fs_req_recv(req, P9_RVERSION); | |
340 | v9fs_uint32_read(req, &msize); | |
341 | ||
342 | g_assert_cmpint(msize, ==, P9_MAX_SIZE); | |
343 | ||
344 | if (len || version) { | |
345 | v9fs_string_read(req, len, version); | |
346 | } | |
347 | ||
348 | v9fs_req_free(req); | |
349 | } | |
350 | ||
5c3df1f0 | 351 | /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */ |
dfbe8b43 | 352 | static P9Req *v9fs_tattach(QVirtio9P *v9p, uint32_t fid, uint32_t n_uname, |
693b21d2 | 353 | uint16_t tag) |
5c3df1f0 GK |
354 | { |
355 | const char *uname = ""; /* ignored by QEMU */ | |
356 | const char *aname = ""; /* ignored by QEMU */ | |
693b21d2 | 357 | P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag); |
5c3df1f0 GK |
358 | |
359 | v9fs_uint32_write(req, fid); | |
360 | v9fs_uint32_write(req, P9_NOFID); | |
361 | v9fs_string_write(req, uname); | |
362 | v9fs_string_write(req, aname); | |
363 | v9fs_uint32_write(req, n_uname); | |
364 | v9fs_req_send(req); | |
365 | return req; | |
366 | } | |
367 | ||
368 | typedef char v9fs_qid[13]; | |
369 | ||
370 | /* size[4] Rattach tag[2] qid[13] */ | |
371 | static void v9fs_rattach(P9Req *req, v9fs_qid *qid) | |
372 | { | |
373 | v9fs_req_recv(req, P9_RATTACH); | |
374 | if (qid) { | |
375 | v9fs_memread(req, qid, 13); | |
376 | } | |
377 | v9fs_req_free(req); | |
378 | } | |
379 | ||
04b88c84 | 380 | /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */ |
dfbe8b43 | 381 | static P9Req *v9fs_twalk(QVirtio9P *v9p, uint32_t fid, uint32_t newfid, |
693b21d2 | 382 | uint16_t nwname, char *const wnames[], uint16_t tag) |
04b88c84 GK |
383 | { |
384 | P9Req *req; | |
385 | int i; | |
9ea776ee | 386 | uint32_t body_size = 4 + 4 + 2; |
04b88c84 GK |
387 | |
388 | for (i = 0; i < nwname; i++) { | |
9ea776ee GK |
389 | uint16_t wname_size = v9fs_string_size(wnames[i]); |
390 | ||
391 | g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size); | |
392 | body_size += wname_size; | |
04b88c84 | 393 | } |
9ea776ee | 394 | req = v9fs_req_init(v9p, body_size, P9_TWALK, tag); |
04b88c84 GK |
395 | v9fs_uint32_write(req, fid); |
396 | v9fs_uint32_write(req, newfid); | |
397 | v9fs_uint16_write(req, nwname); | |
398 | for (i = 0; i < nwname; i++) { | |
399 | v9fs_string_write(req, wnames[i]); | |
400 | } | |
401 | v9fs_req_send(req); | |
402 | return req; | |
403 | } | |
404 | ||
405 | /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */ | |
406 | static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid) | |
407 | { | |
408 | uint16_t local_nwqid; | |
409 | ||
410 | v9fs_req_recv(req, P9_RWALK); | |
411 | v9fs_uint16_read(req, &local_nwqid); | |
412 | if (nwqid) { | |
413 | *nwqid = local_nwqid; | |
414 | } | |
415 | if (wqid) { | |
416 | *wqid = g_malloc(local_nwqid * 13); | |
417 | v9fs_memread(req, *wqid, local_nwqid * 13); | |
418 | } | |
419 | v9fs_req_free(req); | |
420 | } | |
421 | ||
4829469f CS |
422 | /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */ |
423 | static P9Req *v9fs_treaddir(QVirtio9P *v9p, uint32_t fid, uint64_t offset, | |
424 | uint32_t count, uint16_t tag) | |
425 | { | |
426 | P9Req *req; | |
427 | ||
428 | req = v9fs_req_init(v9p, 4 + 8 + 4, P9_TREADDIR, tag); | |
429 | v9fs_uint32_write(req, fid); | |
430 | v9fs_uint64_write(req, offset); | |
431 | v9fs_uint32_write(req, count); | |
432 | v9fs_req_send(req); | |
433 | return req; | |
434 | } | |
435 | ||
436 | struct V9fsDirent { | |
437 | v9fs_qid qid; | |
438 | uint64_t offset; | |
439 | uint8_t type; | |
440 | char *name; | |
441 | struct V9fsDirent *next; | |
442 | }; | |
443 | ||
444 | /* size[4] Rreaddir tag[2] count[4] data[count] */ | |
445 | static void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries, | |
446 | struct V9fsDirent **entries) | |
447 | { | |
448 | uint32_t local_count; | |
449 | struct V9fsDirent *e = NULL; | |
450 | uint16_t slen; | |
451 | uint32_t n = 0; | |
452 | ||
453 | v9fs_req_recv(req, P9_RREADDIR); | |
454 | v9fs_uint32_read(req, &local_count); | |
455 | ||
456 | if (count) { | |
457 | *count = local_count; | |
458 | } | |
459 | ||
460 | for (int32_t togo = (int32_t)local_count; | |
461 | togo >= 13 + 8 + 1 + 2; | |
462 | togo -= 13 + 8 + 1 + 2 + slen, ++n) | |
463 | { | |
464 | if (!e) { | |
465 | e = g_malloc(sizeof(struct V9fsDirent)); | |
466 | if (entries) { | |
467 | *entries = e; | |
468 | } | |
469 | } else { | |
470 | e = e->next = g_malloc(sizeof(struct V9fsDirent)); | |
471 | } | |
472 | e->next = NULL; | |
473 | /* qid[13] offset[8] type[1] name[s] */ | |
474 | v9fs_memread(req, &e->qid, 13); | |
475 | v9fs_uint64_read(req, &e->offset); | |
476 | v9fs_uint8_read(req, &e->type); | |
477 | v9fs_string_read(req, &slen, &e->name); | |
478 | } | |
479 | ||
480 | if (nentries) { | |
481 | *nentries = n; | |
482 | } | |
483 | ||
484 | v9fs_req_free(req); | |
485 | } | |
486 | ||
487 | static void v9fs_free_dirents(struct V9fsDirent *e) | |
488 | { | |
489 | struct V9fsDirent *next = NULL; | |
490 | ||
491 | for (; e; e = next) { | |
492 | next = e->next; | |
493 | g_free(e->name); | |
494 | g_free(e); | |
495 | } | |
496 | } | |
497 | ||
82469aae | 498 | /* size[4] Tlopen tag[2] fid[4] flags[4] */ |
dfbe8b43 | 499 | static P9Req *v9fs_tlopen(QVirtio9P *v9p, uint32_t fid, uint32_t flags, |
82469aae GK |
500 | uint16_t tag) |
501 | { | |
502 | P9Req *req; | |
503 | ||
504 | req = v9fs_req_init(v9p, 4 + 4, P9_TLOPEN, tag); | |
505 | v9fs_uint32_write(req, fid); | |
506 | v9fs_uint32_write(req, flags); | |
507 | v9fs_req_send(req); | |
508 | return req; | |
509 | } | |
510 | ||
511 | /* size[4] Rlopen tag[2] qid[13] iounit[4] */ | |
512 | static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit) | |
513 | { | |
514 | v9fs_req_recv(req, P9_RLOPEN); | |
515 | if (qid) { | |
516 | v9fs_memread(req, qid, 13); | |
517 | } else { | |
518 | v9fs_memskip(req, 13); | |
519 | } | |
520 | if (iounit) { | |
521 | v9fs_uint32_read(req, iounit); | |
522 | } | |
523 | v9fs_req_free(req); | |
524 | } | |
525 | ||
354b86f8 | 526 | /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */ |
dfbe8b43 | 527 | static P9Req *v9fs_twrite(QVirtio9P *v9p, uint32_t fid, uint64_t offset, |
354b86f8 GK |
528 | uint32_t count, const void *data, uint16_t tag) |
529 | { | |
530 | P9Req *req; | |
531 | uint32_t body_size = 4 + 8 + 4; | |
532 | ||
533 | g_assert_cmpint(body_size, <=, UINT32_MAX - count); | |
534 | body_size += count; | |
535 | req = v9fs_req_init(v9p, body_size, P9_TWRITE, tag); | |
536 | v9fs_uint32_write(req, fid); | |
537 | v9fs_uint64_write(req, offset); | |
538 | v9fs_uint32_write(req, count); | |
539 | v9fs_memwrite(req, data, count); | |
540 | v9fs_req_send(req); | |
541 | return req; | |
542 | } | |
543 | ||
544 | /* size[4] Rwrite tag[2] count[4] */ | |
545 | static void v9fs_rwrite(P9Req *req, uint32_t *count) | |
546 | { | |
547 | v9fs_req_recv(req, P9_RWRITE); | |
548 | if (count) { | |
549 | v9fs_uint32_read(req, count); | |
550 | } | |
551 | v9fs_req_free(req); | |
552 | } | |
553 | ||
357e2f7f | 554 | /* size[4] Tflush tag[2] oldtag[2] */ |
dfbe8b43 | 555 | static P9Req *v9fs_tflush(QVirtio9P *v9p, uint16_t oldtag, uint16_t tag) |
357e2f7f GK |
556 | { |
557 | P9Req *req; | |
558 | ||
559 | req = v9fs_req_init(v9p, 2, P9_TFLUSH, tag); | |
560 | v9fs_uint32_write(req, oldtag); | |
561 | v9fs_req_send(req); | |
562 | return req; | |
563 | } | |
564 | ||
565 | /* size[4] Rflush tag[2] */ | |
566 | static void v9fs_rflush(P9Req *req) | |
567 | { | |
568 | v9fs_req_recv(req, P9_RFLUSH); | |
569 | v9fs_req_free(req); | |
570 | } | |
571 | ||
1c450e6e | 572 | static void do_version(QVirtio9P *v9p) |
6cc9906b GK |
573 | { |
574 | const char *version = "9P2000.L"; | |
575 | uint16_t server_len; | |
576 | char *server_version; | |
577 | P9Req *req; | |
578 | ||
693b21d2 | 579 | req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG); |
357e2f7f | 580 | v9fs_req_wait_for_reply(req, NULL); |
6cc9906b GK |
581 | v9fs_rversion(req, &server_len, &server_version); |
582 | ||
583 | g_assert_cmpmem(server_version, server_len, version, strlen(version)); | |
584 | ||
585 | g_free(server_version); | |
586 | } | |
587 | ||
20018805 CS |
588 | /* utility function: walk to requested dir and return fid for that dir */ |
589 | static uint32_t do_walk(QVirtio9P *v9p, const char *path) | |
590 | { | |
591 | char **wnames; | |
592 | P9Req *req; | |
593 | const uint32_t fid = genfid(); | |
594 | ||
595 | int nwnames = split(path, "/", &wnames); | |
596 | ||
597 | req = v9fs_twalk(v9p, 0, fid, nwnames, wnames, 0); | |
598 | v9fs_req_wait_for_reply(req, NULL); | |
599 | v9fs_rwalk(req, NULL, NULL); | |
600 | ||
601 | split_free(&wnames); | |
602 | return fid; | |
603 | } | |
604 | ||
1c450e6e GK |
605 | static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc) |
606 | { | |
607 | alloc = t_alloc; | |
608 | do_version(obj); | |
609 | } | |
610 | ||
3fe4baf4 | 611 | static void do_attach(QVirtio9P *v9p) |
5c3df1f0 GK |
612 | { |
613 | P9Req *req; | |
614 | ||
1c450e6e | 615 | do_version(v9p); |
693b21d2 | 616 | req = v9fs_tattach(v9p, 0, getuid(), 0); |
357e2f7f | 617 | v9fs_req_wait_for_reply(req, NULL); |
5c3df1f0 GK |
618 | v9fs_rattach(req, NULL); |
619 | } | |
620 | ||
3fe4baf4 GK |
621 | static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc) |
622 | { | |
623 | alloc = t_alloc; | |
624 | do_attach(obj); | |
625 | } | |
626 | ||
dfbe8b43 | 627 | static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc) |
04b88c84 | 628 | { |
dfbe8b43 EGE |
629 | QVirtio9P *v9p = obj; |
630 | alloc = t_alloc; | |
2893ddd5 | 631 | char *wnames[P9_MAXWELEM]; |
04b88c84 GK |
632 | uint16_t nwqid; |
633 | v9fs_qid *wqid; | |
634 | int i; | |
635 | P9Req *req; | |
636 | ||
637 | for (i = 0; i < P9_MAXWELEM; i++) { | |
2893ddd5 | 638 | wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i); |
04b88c84 GK |
639 | } |
640 | ||
3fe4baf4 | 641 | do_attach(v9p); |
693b21d2 | 642 | req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0); |
357e2f7f | 643 | v9fs_req_wait_for_reply(req, NULL); |
04b88c84 GK |
644 | v9fs_rwalk(req, &nwqid, &wqid); |
645 | ||
646 | g_assert_cmpint(nwqid, ==, P9_MAXWELEM); | |
647 | ||
648 | for (i = 0; i < P9_MAXWELEM; i++) { | |
04b88c84 GK |
649 | g_free(wnames[i]); |
650 | } | |
651 | ||
652 | g_free(wqid); | |
653 | } | |
654 | ||
4829469f CS |
655 | static bool fs_dirents_contain_name(struct V9fsDirent *e, const char* name) |
656 | { | |
657 | for (; e; e = e->next) { | |
658 | if (!strcmp(e->name, name)) { | |
659 | return true; | |
660 | } | |
661 | } | |
662 | return false; | |
663 | } | |
664 | ||
653daf38 CS |
665 | /* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */ |
666 | static P9Req *v9fs_tmkdir(QVirtio9P *v9p, uint32_t dfid, const char *name, | |
667 | uint32_t mode, uint32_t gid, uint16_t tag) | |
668 | { | |
669 | P9Req *req; | |
670 | ||
671 | uint32_t body_size = 4 + 4 + 4; | |
672 | uint16_t string_size = v9fs_string_size(name); | |
673 | ||
674 | g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); | |
675 | body_size += string_size; | |
676 | ||
677 | req = v9fs_req_init(v9p, body_size, P9_TMKDIR, tag); | |
678 | v9fs_uint32_write(req, dfid); | |
679 | v9fs_string_write(req, name); | |
680 | v9fs_uint32_write(req, mode); | |
681 | v9fs_uint32_write(req, gid); | |
682 | v9fs_req_send(req); | |
683 | return req; | |
684 | } | |
685 | ||
686 | /* size[4] Rmkdir tag[2] qid[13] */ | |
687 | static void v9fs_rmkdir(P9Req *req, v9fs_qid *qid) | |
688 | { | |
689 | v9fs_req_recv(req, P9_RMKDIR); | |
690 | if (qid) { | |
691 | v9fs_memread(req, qid, 13); | |
692 | } else { | |
693 | v9fs_memskip(req, 13); | |
694 | } | |
695 | v9fs_req_free(req); | |
696 | } | |
697 | ||
b09dbfdd CS |
698 | /* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */ |
699 | static P9Req *v9fs_tlcreate(QVirtio9P *v9p, uint32_t fid, const char *name, | |
700 | uint32_t flags, uint32_t mode, uint32_t gid, | |
701 | uint16_t tag) | |
702 | { | |
703 | P9Req *req; | |
704 | ||
705 | uint32_t body_size = 4 + 4 + 4 + 4; | |
706 | uint16_t string_size = v9fs_string_size(name); | |
707 | ||
708 | g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); | |
709 | body_size += string_size; | |
710 | ||
711 | req = v9fs_req_init(v9p, body_size, P9_TLCREATE, tag); | |
712 | v9fs_uint32_write(req, fid); | |
713 | v9fs_string_write(req, name); | |
714 | v9fs_uint32_write(req, flags); | |
715 | v9fs_uint32_write(req, mode); | |
716 | v9fs_uint32_write(req, gid); | |
717 | v9fs_req_send(req); | |
718 | return req; | |
719 | } | |
720 | ||
721 | /* size[4] Rlcreate tag[2] qid[13] iounit[4] */ | |
722 | static void v9fs_rlcreate(P9Req *req, v9fs_qid *qid, uint32_t *iounit) | |
723 | { | |
724 | v9fs_req_recv(req, P9_RLCREATE); | |
725 | if (qid) { | |
726 | v9fs_memread(req, qid, 13); | |
727 | } else { | |
728 | v9fs_memskip(req, 13); | |
729 | } | |
730 | if (iounit) { | |
731 | v9fs_uint32_read(req, iounit); | |
732 | } | |
733 | v9fs_req_free(req); | |
734 | } | |
735 | ||
b37d62d6 CS |
736 | /* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */ |
737 | static P9Req *v9fs_tunlinkat(QVirtio9P *v9p, uint32_t dirfd, const char *name, | |
738 | uint32_t flags, uint16_t tag) | |
739 | { | |
740 | P9Req *req; | |
741 | ||
742 | uint32_t body_size = 4 + 4; | |
743 | uint16_t string_size = v9fs_string_size(name); | |
744 | ||
745 | g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); | |
746 | body_size += string_size; | |
747 | ||
748 | req = v9fs_req_init(v9p, body_size, P9_TUNLINKAT, tag); | |
749 | v9fs_uint32_write(req, dirfd); | |
750 | v9fs_string_write(req, name); | |
751 | v9fs_uint32_write(req, flags); | |
752 | v9fs_req_send(req); | |
753 | return req; | |
754 | } | |
755 | ||
756 | /* size[4] Runlinkat tag[2] */ | |
757 | static void v9fs_runlinkat(P9Req *req) | |
758 | { | |
759 | v9fs_req_recv(req, P9_RUNLINKAT); | |
760 | v9fs_req_free(req); | |
761 | } | |
762 | ||
46488b62 | 763 | /* basic readdir test where reply fits into a single response message */ |
4829469f CS |
764 | static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc) |
765 | { | |
766 | QVirtio9P *v9p = obj; | |
767 | alloc = t_alloc; | |
768 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) }; | |
769 | uint16_t nqid; | |
770 | v9fs_qid qid; | |
771 | uint32_t count, nentries; | |
772 | struct V9fsDirent *entries = NULL; | |
773 | P9Req *req; | |
774 | ||
3fe4baf4 | 775 | do_attach(v9p); |
4829469f CS |
776 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
777 | v9fs_req_wait_for_reply(req, NULL); | |
778 | v9fs_rwalk(req, &nqid, NULL); | |
779 | g_assert_cmpint(nqid, ==, 1); | |
780 | ||
781 | req = v9fs_tlopen(v9p, 1, O_DIRECTORY, 0); | |
782 | v9fs_req_wait_for_reply(req, NULL); | |
783 | v9fs_rlopen(req, &qid, NULL); | |
784 | ||
785 | /* | |
786 | * submit count = msize - 11, because 11 is the header size of Rreaddir | |
787 | */ | |
788 | req = v9fs_treaddir(v9p, 1, 0, P9_MAX_SIZE - 11, 0); | |
789 | v9fs_req_wait_for_reply(req, NULL); | |
790 | v9fs_rreaddir(req, &count, &nentries, &entries); | |
791 | ||
792 | /* | |
793 | * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all | |
794 | * dir entries with only one readdir request. | |
795 | */ | |
796 | g_assert_cmpint( | |
797 | nentries, ==, | |
798 | QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */ | |
799 | ); | |
800 | ||
801 | /* | |
802 | * Check all file names exist in returned entries, ignore their order | |
803 | * though. | |
804 | */ | |
805 | g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true); | |
806 | g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true); | |
807 | for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) { | |
808 | char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i); | |
809 | g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true); | |
810 | g_free(name); | |
811 | } | |
812 | ||
813 | v9fs_free_dirents(entries); | |
814 | g_free(wnames[0]); | |
815 | } | |
816 | ||
46488b62 | 817 | /* readdir test where overall request is split over several messages */ |
1d98613d | 818 | static void do_readdir_split(QVirtio9P *v9p, uint32_t count) |
46488b62 | 819 | { |
46488b62 CS |
820 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) }; |
821 | uint16_t nqid; | |
822 | v9fs_qid qid; | |
823 | uint32_t nentries, npartialentries; | |
824 | struct V9fsDirent *entries, *tail, *partialentries; | |
825 | P9Req *req; | |
826 | int fid; | |
827 | uint64_t offset; | |
828 | ||
3fe4baf4 | 829 | do_attach(v9p); |
46488b62 CS |
830 | |
831 | fid = 1; | |
832 | offset = 0; | |
833 | entries = NULL; | |
834 | nentries = 0; | |
835 | tail = NULL; | |
836 | ||
837 | req = v9fs_twalk(v9p, 0, fid, 1, wnames, 0); | |
838 | v9fs_req_wait_for_reply(req, NULL); | |
839 | v9fs_rwalk(req, &nqid, NULL); | |
840 | g_assert_cmpint(nqid, ==, 1); | |
841 | ||
842 | req = v9fs_tlopen(v9p, fid, O_DIRECTORY, 0); | |
843 | v9fs_req_wait_for_reply(req, NULL); | |
844 | v9fs_rlopen(req, &qid, NULL); | |
845 | ||
846 | /* | |
847 | * send as many Treaddir requests as required to get all directory | |
848 | * entries | |
849 | */ | |
850 | while (true) { | |
851 | npartialentries = 0; | |
852 | partialentries = NULL; | |
853 | ||
854 | req = v9fs_treaddir(v9p, fid, offset, count, 0); | |
855 | v9fs_req_wait_for_reply(req, NULL); | |
856 | v9fs_rreaddir(req, &count, &npartialentries, &partialentries); | |
857 | if (npartialentries > 0 && partialentries) { | |
858 | if (!entries) { | |
859 | entries = partialentries; | |
860 | nentries = npartialentries; | |
861 | tail = partialentries; | |
862 | } else { | |
863 | tail->next = partialentries; | |
864 | nentries += npartialentries; | |
865 | } | |
866 | while (tail->next) { | |
867 | tail = tail->next; | |
868 | } | |
869 | offset = tail->offset; | |
870 | } else { | |
871 | break; | |
872 | } | |
873 | } | |
874 | ||
875 | g_assert_cmpint( | |
876 | nentries, ==, | |
877 | QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */ | |
878 | ); | |
879 | ||
880 | /* | |
881 | * Check all file names exist in returned entries, ignore their order | |
882 | * though. | |
883 | */ | |
884 | g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true); | |
885 | g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true); | |
886 | for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) { | |
887 | char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i); | |
888 | g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true); | |
889 | g_free(name); | |
890 | } | |
891 | ||
892 | v9fs_free_dirents(entries); | |
893 | ||
894 | g_free(wnames[0]); | |
895 | } | |
896 | ||
dfbe8b43 | 897 | static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc) |
ba0d1037 | 898 | { |
dfbe8b43 EGE |
899 | QVirtio9P *v9p = obj; |
900 | alloc = t_alloc; | |
ba0d1037 GK |
901 | char *const wnames[] = { g_strdup(" /") }; |
902 | P9Req *req; | |
903 | uint32_t err; | |
904 | ||
3fe4baf4 | 905 | do_attach(v9p); |
693b21d2 | 906 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
357e2f7f | 907 | v9fs_req_wait_for_reply(req, NULL); |
ba0d1037 GK |
908 | v9fs_rlerror(req, &err); |
909 | ||
910 | g_assert_cmpint(err, ==, ENOENT); | |
911 | ||
912 | g_free(wnames[0]); | |
913 | } | |
914 | ||
dfbe8b43 | 915 | static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc) |
a37c0702 | 916 | { |
dfbe8b43 EGE |
917 | QVirtio9P *v9p = obj; |
918 | alloc = t_alloc; | |
a37c0702 GK |
919 | char *const wnames[] = { g_strdup("..") }; |
920 | v9fs_qid root_qid, *wqid; | |
921 | P9Req *req; | |
922 | ||
1c450e6e | 923 | do_version(v9p); |
693b21d2 | 924 | req = v9fs_tattach(v9p, 0, getuid(), 0); |
357e2f7f | 925 | v9fs_req_wait_for_reply(req, NULL); |
a37c0702 GK |
926 | v9fs_rattach(req, &root_qid); |
927 | ||
693b21d2 | 928 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
357e2f7f | 929 | v9fs_req_wait_for_reply(req, NULL); |
a37c0702 GK |
930 | v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */ |
931 | ||
932 | g_assert_cmpmem(&root_qid, 13, wqid[0], 13); | |
933 | ||
934 | g_free(wqid); | |
935 | g_free(wnames[0]); | |
936 | } | |
937 | ||
dfbe8b43 | 938 | static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc) |
82469aae | 939 | { |
dfbe8b43 EGE |
940 | QVirtio9P *v9p = obj; |
941 | alloc = t_alloc; | |
82469aae GK |
942 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) }; |
943 | P9Req *req; | |
944 | ||
3fe4baf4 | 945 | do_attach(v9p); |
82469aae | 946 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
357e2f7f | 947 | v9fs_req_wait_for_reply(req, NULL); |
82469aae GK |
948 | v9fs_rwalk(req, NULL, NULL); |
949 | ||
950 | req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); | |
357e2f7f | 951 | v9fs_req_wait_for_reply(req, NULL); |
82469aae GK |
952 | v9fs_rlopen(req, NULL, NULL); |
953 | ||
954 | g_free(wnames[0]); | |
955 | } | |
956 | ||
dfbe8b43 | 957 | static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc) |
354b86f8 | 958 | { |
dfbe8b43 EGE |
959 | QVirtio9P *v9p = obj; |
960 | alloc = t_alloc; | |
354b86f8 GK |
961 | static const uint32_t write_count = P9_MAX_SIZE / 2; |
962 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) }; | |
963 | char *buf = g_malloc0(write_count); | |
964 | uint32_t count; | |
965 | P9Req *req; | |
966 | ||
3fe4baf4 | 967 | do_attach(v9p); |
354b86f8 | 968 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
357e2f7f | 969 | v9fs_req_wait_for_reply(req, NULL); |
354b86f8 GK |
970 | v9fs_rwalk(req, NULL, NULL); |
971 | ||
972 | req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); | |
357e2f7f | 973 | v9fs_req_wait_for_reply(req, NULL); |
354b86f8 GK |
974 | v9fs_rlopen(req, NULL, NULL); |
975 | ||
976 | req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0); | |
357e2f7f | 977 | v9fs_req_wait_for_reply(req, NULL); |
354b86f8 GK |
978 | v9fs_rwrite(req, &count); |
979 | g_assert_cmpint(count, ==, write_count); | |
980 | ||
981 | g_free(buf); | |
982 | g_free(wnames[0]); | |
983 | } | |
984 | ||
dfbe8b43 | 985 | static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc) |
357e2f7f | 986 | { |
dfbe8b43 EGE |
987 | QVirtio9P *v9p = obj; |
988 | alloc = t_alloc; | |
357e2f7f GK |
989 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; |
990 | P9Req *req, *flush_req; | |
991 | uint32_t reply_len; | |
992 | uint8_t should_block; | |
993 | ||
3fe4baf4 | 994 | do_attach(v9p); |
357e2f7f GK |
995 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
996 | v9fs_req_wait_for_reply(req, NULL); | |
997 | v9fs_rwalk(req, NULL, NULL); | |
998 | ||
999 | req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); | |
1000 | v9fs_req_wait_for_reply(req, NULL); | |
1001 | v9fs_rlopen(req, NULL, NULL); | |
1002 | ||
1003 | /* This will cause the 9p server to try to write data to the backend, | |
1004 | * until the write request gets cancelled. | |
1005 | */ | |
1006 | should_block = 1; | |
1007 | req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); | |
1008 | ||
1009 | flush_req = v9fs_tflush(v9p, req->tag, 1); | |
1010 | ||
1011 | /* The write request is supposed to be flushed: the server should just | |
1012 | * mark the write request as used and reply to the flush request. | |
1013 | */ | |
1014 | v9fs_req_wait_for_reply(req, &reply_len); | |
1015 | g_assert_cmpint(reply_len, ==, 0); | |
1016 | v9fs_req_free(req); | |
1017 | v9fs_rflush(flush_req); | |
1018 | ||
1019 | g_free(wnames[0]); | |
1020 | } | |
1021 | ||
dfbe8b43 | 1022 | static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc) |
357e2f7f | 1023 | { |
dfbe8b43 EGE |
1024 | QVirtio9P *v9p = obj; |
1025 | alloc = t_alloc; | |
357e2f7f GK |
1026 | char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; |
1027 | P9Req *req, *flush_req; | |
1028 | uint32_t count; | |
1029 | uint8_t should_block; | |
1030 | ||
3fe4baf4 | 1031 | do_attach(v9p); |
357e2f7f GK |
1032 | req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); |
1033 | v9fs_req_wait_for_reply(req, NULL); | |
1034 | v9fs_rwalk(req, NULL, NULL); | |
1035 | ||
1036 | req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); | |
1037 | v9fs_req_wait_for_reply(req, NULL); | |
1038 | v9fs_rlopen(req, NULL, NULL); | |
1039 | ||
1040 | /* This will cause the write request to complete right away, before it | |
1041 | * could be actually cancelled. | |
1042 | */ | |
1043 | should_block = 0; | |
1044 | req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); | |
1045 | ||
1046 | flush_req = v9fs_tflush(v9p, req->tag, 1); | |
1047 | ||
1048 | /* The write request is supposed to complete. The server should | |
1049 | * reply to the write request and the flush request. | |
1050 | */ | |
1051 | v9fs_req_wait_for_reply(req, NULL); | |
1052 | v9fs_rwrite(req, &count); | |
1053 | g_assert_cmpint(count, ==, sizeof(should_block)); | |
1054 | v9fs_rflush(flush_req); | |
1055 | ||
1056 | g_free(wnames[0]); | |
1057 | } | |
1058 | ||
c1934f63 | 1059 | static void do_mkdir(QVirtio9P *v9p, const char *path, const char *cname) |
653daf38 | 1060 | { |
653daf38 | 1061 | char *const name = g_strdup(cname); |
20018805 | 1062 | uint32_t fid; |
653daf38 | 1063 | P9Req *req; |
653daf38 | 1064 | |
20018805 | 1065 | fid = do_walk(v9p, path); |
653daf38 CS |
1066 | |
1067 | req = v9fs_tmkdir(v9p, fid, name, 0750, 0, 0); | |
1068 | v9fs_req_wait_for_reply(req, NULL); | |
1069 | v9fs_rmkdir(req, NULL); | |
1070 | ||
1071 | g_free(name); | |
653daf38 CS |
1072 | } |
1073 | ||
b09dbfdd CS |
1074 | /* create a regular file with Tlcreate and return file's fid */ |
1075 | static uint32_t do_lcreate(QVirtio9P *v9p, const char *path, | |
1076 | const char *cname) | |
1077 | { | |
1078 | char *const name = g_strdup(cname); | |
1079 | uint32_t fid; | |
1080 | P9Req *req; | |
1081 | ||
1082 | fid = do_walk(v9p, path); | |
1083 | ||
1084 | req = v9fs_tlcreate(v9p, fid, name, 0, 0750, 0, 0); | |
1085 | v9fs_req_wait_for_reply(req, NULL); | |
1086 | v9fs_rlcreate(req, NULL, NULL); | |
1087 | ||
1088 | g_free(name); | |
1089 | return fid; | |
1090 | } | |
1091 | ||
b37d62d6 CS |
1092 | static void do_unlinkat(QVirtio9P *v9p, const char *atpath, const char *rpath, |
1093 | uint32_t flags) | |
1094 | { | |
1095 | char *const name = g_strdup(rpath); | |
1096 | uint32_t fid; | |
1097 | P9Req *req; | |
1098 | ||
1099 | fid = do_walk(v9p, atpath); | |
1100 | ||
1101 | req = v9fs_tunlinkat(v9p, fid, name, flags, 0); | |
1102 | v9fs_req_wait_for_reply(req, NULL); | |
1103 | v9fs_runlinkat(req); | |
1104 | ||
1105 | g_free(name); | |
1106 | } | |
1107 | ||
46488b62 CS |
1108 | static void fs_readdir_split_128(void *obj, void *data, |
1109 | QGuestAllocator *t_alloc) | |
1110 | { | |
1d98613d GK |
1111 | alloc = t_alloc; |
1112 | do_readdir_split(obj, 128); | |
46488b62 CS |
1113 | } |
1114 | ||
1115 | static void fs_readdir_split_256(void *obj, void *data, | |
1116 | QGuestAllocator *t_alloc) | |
1117 | { | |
1d98613d GK |
1118 | alloc = t_alloc; |
1119 | do_readdir_split(obj, 256); | |
46488b62 CS |
1120 | } |
1121 | ||
1122 | static void fs_readdir_split_512(void *obj, void *data, | |
1123 | QGuestAllocator *t_alloc) | |
1124 | { | |
1d98613d GK |
1125 | alloc = t_alloc; |
1126 | do_readdir_split(obj, 512); | |
46488b62 CS |
1127 | } |
1128 | ||
653daf38 CS |
1129 | |
1130 | /* tests using the 9pfs 'local' fs driver */ | |
1131 | ||
1132 | static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc) | |
1133 | { | |
1134 | QVirtio9P *v9p = obj; | |
382619ef | 1135 | alloc = t_alloc; |
653daf38 CS |
1136 | struct stat st; |
1137 | char *root_path = virtio_9p_test_path(""); | |
1138 | char *new_dir = virtio_9p_test_path("01"); | |
1139 | ||
1140 | g_assert(root_path != NULL); | |
1141 | ||
3fe4baf4 | 1142 | do_attach(v9p); |
c1934f63 | 1143 | do_mkdir(v9p, "/", "01"); |
653daf38 CS |
1144 | |
1145 | /* check if created directory really exists now ... */ | |
1146 | g_assert(stat(new_dir, &st) == 0); | |
1147 | /* ... and is actually a directory */ | |
1148 | g_assert((st.st_mode & S_IFMT) == S_IFDIR); | |
1149 | ||
1150 | g_free(new_dir); | |
1151 | g_free(root_path); | |
1152 | } | |
1153 | ||
b37d62d6 CS |
1154 | static void fs_unlinkat_dir(void *obj, void *data, QGuestAllocator *t_alloc) |
1155 | { | |
1156 | QVirtio9P *v9p = obj; | |
1157 | alloc = t_alloc; | |
1158 | struct stat st; | |
1159 | char *root_path = virtio_9p_test_path(""); | |
1160 | char *new_dir = virtio_9p_test_path("02"); | |
1161 | ||
1162 | g_assert(root_path != NULL); | |
1163 | ||
1164 | do_attach(v9p); | |
1165 | do_mkdir(v9p, "/", "02"); | |
1166 | ||
1167 | /* check if created directory really exists now ... */ | |
1168 | g_assert(stat(new_dir, &st) == 0); | |
1169 | /* ... and is actually a directory */ | |
1170 | g_assert((st.st_mode & S_IFMT) == S_IFDIR); | |
1171 | ||
1172 | do_unlinkat(v9p, "/", "02", AT_REMOVEDIR); | |
1173 | /* directory should be gone now */ | |
1174 | g_assert(stat(new_dir, &st) != 0); | |
1175 | ||
1176 | g_free(new_dir); | |
1177 | g_free(root_path); | |
1178 | } | |
1179 | ||
b09dbfdd CS |
1180 | static void fs_create_file(void *obj, void *data, QGuestAllocator *t_alloc) |
1181 | { | |
1182 | QVirtio9P *v9p = obj; | |
1183 | alloc = t_alloc; | |
1184 | struct stat st; | |
1185 | char *new_file = virtio_9p_test_path("03/1st_file"); | |
1186 | ||
1187 | do_attach(v9p); | |
1188 | do_mkdir(v9p, "/", "03"); | |
1189 | do_lcreate(v9p, "03", "1st_file"); | |
1190 | ||
1191 | /* check if created file exists now ... */ | |
1192 | g_assert(stat(new_file, &st) == 0); | |
1193 | /* ... and is a regular file */ | |
1194 | g_assert((st.st_mode & S_IFMT) == S_IFREG); | |
1195 | ||
1196 | g_free(new_file); | |
1197 | } | |
1198 | ||
3a565c64 CS |
1199 | static void *assign_9p_local_driver(GString *cmd_line, void *arg) |
1200 | { | |
1201 | virtio_9p_assign_local_driver(cmd_line, "security_model=mapped-xattr"); | |
1202 | return arg; | |
1203 | } | |
1204 | ||
dfbe8b43 | 1205 | static void register_virtio_9p_test(void) |
1211d81b | 1206 | { |
3a565c64 CS |
1207 | |
1208 | QOSGraphTestOptions opts = { | |
1209 | }; | |
1210 | ||
1211 | /* 9pfs test cases using the 'synth' filesystem driver */ | |
1212 | qos_add_test("synth/config", "virtio-9p", pci_config, &opts); | |
1213 | qos_add_test("synth/version/basic", "virtio-9p", fs_version, &opts); | |
1214 | qos_add_test("synth/attach/basic", "virtio-9p", fs_attach, &opts); | |
1215 | qos_add_test("synth/walk/basic", "virtio-9p", fs_walk, &opts); | |
eefd2394 | 1216 | qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash, |
3a565c64 | 1217 | &opts); |
eefd2394 | 1218 | qos_add_test("synth/walk/dotdot_from_root", "virtio-9p", |
3a565c64 CS |
1219 | fs_walk_dotdot, &opts); |
1220 | qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen, &opts); | |
1221 | qos_add_test("synth/write/basic", "virtio-9p", fs_write, &opts); | |
eefd2394 | 1222 | qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success, |
3a565c64 | 1223 | &opts); |
eefd2394 | 1224 | qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored, |
3a565c64 CS |
1225 | &opts); |
1226 | qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir, &opts); | |
eefd2394 | 1227 | qos_add_test("synth/readdir/split_512", "virtio-9p", |
3a565c64 | 1228 | fs_readdir_split_512, &opts); |
eefd2394 | 1229 | qos_add_test("synth/readdir/split_256", "virtio-9p", |
3a565c64 | 1230 | fs_readdir_split_256, &opts); |
eefd2394 | 1231 | qos_add_test("synth/readdir/split_128", "virtio-9p", |
3a565c64 CS |
1232 | fs_readdir_split_128, &opts); |
1233 | ||
1234 | ||
1235 | /* 9pfs test cases using the 'local' filesystem driver */ | |
1236 | opts.before = assign_9p_local_driver; | |
1237 | qos_add_test("local/config", "virtio-9p", pci_config, &opts); | |
653daf38 | 1238 | qos_add_test("local/create_dir", "virtio-9p", fs_create_dir, &opts); |
b37d62d6 | 1239 | qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir, &opts); |
b09dbfdd | 1240 | qos_add_test("local/create_file", "virtio-9p", fs_create_file, &opts); |
557a4cc0 GK |
1241 | } |
1242 | ||
dfbe8b43 | 1243 | libqos_init(register_virtio_9p_test); |
136b7af2 CS |
1244 | |
1245 | static void __attribute__((constructor)) construct_9p_test(void) | |
1246 | { | |
1247 | /* make sure test dir for the 'local' tests exists */ | |
1248 | virtio_9p_create_local_test_dir(); | |
1249 | } | |
1250 | ||
1251 | static void __attribute__((destructor)) destruct_9p_test(void) | |
1252 | { | |
1253 | /* remove previously created test dir when test suite completed */ | |
1254 | virtio_9p_remove_local_test_dir(); | |
1255 | } |