1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "qemu/osdep.h"
16#include "virtio-9p-client.h"
17
18#define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
19static QGuestAllocator *alloc;
20
21void v9fs_set_allocator(QGuestAllocator *t_alloc)
22{
23 alloc = t_alloc;
24}
25
26
27
28
29
30static uint32_t fid_generator = 1000;
31
32static uint32_t genfid(void)
33{
34 return fid_generator++;
35}
36
37
38
39
40
41
42
43
44
45
46static int split(const char *in, const char *delim, char ***out)
47{
48 int n = 0, i = 0;
49 char *tmp, *p;
50
51 tmp = g_strdup(in);
52 for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) {
53 if (strlen(p) > 0) {
54 ++n;
55 }
56 }
57 g_free(tmp);
58
59 *out = g_new0(char *, n + 1);
60
61 tmp = g_strdup(in);
62 for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) {
63 if (strlen(p) > 0) {
64 (*out)[i++] = g_strdup(p);
65 }
66 }
67 g_free(tmp);
68
69 return n;
70}
71
72static void split_free(char ***out)
73{
74 int i;
75 if (!*out) {
76 return;
77 }
78 for (i = 0; (*out)[i]; ++i) {
79 g_free((*out)[i]);
80 }
81 g_free(*out);
82 *out = NULL;
83}
84
85void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
86{
87 qtest_memwrite(req->qts, req->t_msg + req->t_off, addr, len);
88 req->t_off += len;
89}
90
91void v9fs_memskip(P9Req *req, size_t len)
92{
93 req->r_off += len;
94}
95
96void v9fs_memread(P9Req *req, void *addr, size_t len)
97{
98 qtest_memread(req->qts, req->r_msg + req->r_off, addr, len);
99 req->r_off += len;
100}
101
102void v9fs_uint8_read(P9Req *req, uint8_t *val)
103{
104 v9fs_memread(req, val, 1);
105}
106
107void v9fs_uint16_write(P9Req *req, uint16_t val)
108{
109 uint16_t le_val = cpu_to_le16(val);
110
111 v9fs_memwrite(req, &le_val, 2);
112}
113
114void v9fs_uint16_read(P9Req *req, uint16_t *val)
115{
116 v9fs_memread(req, val, 2);
117 le16_to_cpus(val);
118}
119
120void v9fs_uint32_write(P9Req *req, uint32_t val)
121{
122 uint32_t le_val = cpu_to_le32(val);
123
124 v9fs_memwrite(req, &le_val, 4);
125}
126
127void v9fs_uint64_write(P9Req *req, uint64_t val)
128{
129 uint64_t le_val = cpu_to_le64(val);
130
131 v9fs_memwrite(req, &le_val, 8);
132}
133
134void v9fs_uint32_read(P9Req *req, uint32_t *val)
135{
136 v9fs_memread(req, val, 4);
137 le32_to_cpus(val);
138}
139
140void v9fs_uint64_read(P9Req *req, uint64_t *val)
141{
142 v9fs_memread(req, val, 8);
143 le64_to_cpus(val);
144}
145
146
147uint16_t v9fs_string_size(const char *string)
148{
149 size_t len = strlen(string);
150
151 g_assert_cmpint(len, <=, UINT16_MAX - 2);
152
153 return 2 + len;
154}
155
156void v9fs_string_write(P9Req *req, const char *string)
157{
158 int len = strlen(string);
159
160 g_assert_cmpint(len, <=, UINT16_MAX);
161
162 v9fs_uint16_write(req, (uint16_t) len);
163 v9fs_memwrite(req, string, len);
164}
165
166void v9fs_string_read(P9Req *req, uint16_t *len, char **string)
167{
168 uint16_t local_len;
169
170 v9fs_uint16_read(req, &local_len);
171 if (len) {
172 *len = local_len;
173 }
174 if (string) {
175 *string = g_malloc(local_len + 1);
176 v9fs_memread(req, *string, local_len);
177 (*string)[local_len] = 0;
178 } else {
179 v9fs_memskip(req, local_len);
180 }
181}
182
183typedef struct {
184 uint32_t size;
185 uint8_t id;
186 uint16_t tag;
187} QEMU_PACKED P9Hdr;
188
189P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id,
190 uint16_t tag)
191{
192 P9Req *req = g_new0(P9Req, 1);
193 uint32_t total_size = 7;
194 P9Hdr hdr = {
195 .id = id,
196 .tag = cpu_to_le16(tag)
197 };
198
199 g_assert_cmpint(total_size, <=, UINT32_MAX - size);
200 total_size += size;
201 hdr.size = cpu_to_le32(total_size);
202
203 g_assert_cmpint(total_size, <=, P9_MAX_SIZE);
204
205 req->qts = global_qtest;
206 req->v9p = v9p;
207 req->t_size = total_size;
208 req->t_msg = guest_alloc(alloc, req->t_size);
209 v9fs_memwrite(req, &hdr, 7);
210 req->tag = tag;
211 return req;
212}
213
214void v9fs_req_send(P9Req *req)
215{
216 QVirtio9P *v9p = req->v9p;
217
218 req->r_msg = guest_alloc(alloc, P9_MAX_SIZE);
219 req->free_head = qvirtqueue_add(req->qts, v9p->vq, req->t_msg, req->t_size,
220 false, true);
221 qvirtqueue_add(req->qts, v9p->vq, req->r_msg, P9_MAX_SIZE, true, false);
222 qvirtqueue_kick(req->qts, v9p->vdev, v9p->vq, req->free_head);
223 req->t_off = 0;
224}
225
226static const char *rmessage_name(uint8_t id)
227{
228 return
229 id == P9_RLERROR ? "RLERROR" :
230 id == P9_RVERSION ? "RVERSION" :
231 id == P9_RATTACH ? "RATTACH" :
232 id == P9_RWALK ? "RWALK" :
233 id == P9_RLOPEN ? "RLOPEN" :
234 id == P9_RWRITE ? "RWRITE" :
235 id == P9_RMKDIR ? "RMKDIR" :
236 id == P9_RLCREATE ? "RLCREATE" :
237 id == P9_RSYMLINK ? "RSYMLINK" :
238 id == P9_RLINK ? "RLINK" :
239 id == P9_RUNLINKAT ? "RUNLINKAT" :
240 id == P9_RFLUSH ? "RFLUSH" :
241 id == P9_RREADDIR ? "READDIR" :
242 "<unknown>";
243}
244
245void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len)
246{
247 QVirtio9P *v9p = req->v9p;
248
249 qvirtio_wait_used_elem(req->qts, v9p->vdev, v9p->vq, req->free_head, len,
250 QVIRTIO_9P_TIMEOUT_US);
251}
252
253void v9fs_req_recv(P9Req *req, uint8_t id)
254{
255 P9Hdr hdr;
256
257 v9fs_memread(req, &hdr, 7);
258 hdr.size = ldl_le_p(&hdr.size);
259 hdr.tag = lduw_le_p(&hdr.tag);
260
261 g_assert_cmpint(hdr.size, >=, 7);
262 g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE);
263 g_assert_cmpint(hdr.tag, ==, req->tag);
264
265 if (hdr.id != id) {
266 g_printerr("Received response %d (%s) instead of %d (%s)\n",
267 hdr.id, rmessage_name(hdr.id), id, rmessage_name(id));
268
269 if (hdr.id == P9_RLERROR) {
270 uint32_t err;
271 v9fs_uint32_read(req, &err);
272 g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err));
273 }
274 }
275 g_assert_cmpint(hdr.id, ==, id);
276}
277
278void v9fs_req_free(P9Req *req)
279{
280 guest_free(alloc, req->t_msg);
281 guest_free(alloc, req->r_msg);
282 g_free(req);
283}
284
285
286void v9fs_rlerror(P9Req *req, uint32_t *err)
287{
288 v9fs_req_recv(req, P9_RLERROR);
289 v9fs_uint32_read(req, err);
290 v9fs_req_free(req);
291}
292
293
294TVersionRes v9fs_tversion(TVersionOpt opt)
295{
296 P9Req *req;
297 uint32_t err;
298 uint32_t body_size = 4;
299 uint16_t string_size;
300 uint16_t server_len;
301 g_autofree char *server_version = NULL;
302
303 g_assert(opt.client);
304
305 if (!opt.msize) {
306 opt.msize = P9_MAX_SIZE;
307 }
308
309 if (!opt.tag) {
310 opt.tag = P9_NOTAG;
311 }
312
313 if (!opt.version) {
314 opt.version = "9P2000.L";
315 }
316
317 string_size = v9fs_string_size(opt.version);
318 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
319 body_size += string_size;
320 req = v9fs_req_init(opt.client, body_size, P9_TVERSION, opt.tag);
321
322 v9fs_uint32_write(req, opt.msize);
323 v9fs_string_write(req, opt.version);
324 v9fs_req_send(req);
325
326 if (!opt.requestOnly) {
327 v9fs_req_wait_for_reply(req, NULL);
328 if (opt.expectErr) {
329 v9fs_rlerror(req, &err);
330 g_assert_cmpint(err, ==, opt.expectErr);
331 } else {
332 v9fs_rversion(req, &server_len, &server_version);
333 g_assert_cmpmem(server_version, server_len,
334 opt.version, strlen(opt.version));
335 }
336 req = NULL;
337 }
338
339 return (TVersionRes) {
340 .req = req,
341 };
342}
343
344
345void v9fs_rversion(P9Req *req, uint16_t *len, char **version)
346{
347 uint32_t msize;
348
349 v9fs_req_recv(req, P9_RVERSION);
350 v9fs_uint32_read(req, &msize);
351
352 g_assert_cmpint(msize, ==, P9_MAX_SIZE);
353
354 if (len || version) {
355 v9fs_string_read(req, len, version);
356 }
357
358 v9fs_req_free(req);
359}
360
361
362TAttachRes v9fs_tattach(TAttachOpt opt)
363{
364 uint32_t err;
365 const char *uname = "";
366 const char *aname = "";
367
368 g_assert(opt.client);
369
370 g_assert(!opt.expectErr || !opt.rattach.qid);
371
372 if (!opt.requestOnly) {
373 v9fs_tversion((TVersionOpt) { .client = opt.client });
374 }
375
376 if (!opt.n_uname) {
377 opt.n_uname = getuid();
378 }
379
380 P9Req *req = v9fs_req_init(opt.client, 4 + 4 + 2 + 2 + 4, P9_TATTACH,
381 opt.tag);
382
383 v9fs_uint32_write(req, opt.fid);
384 v9fs_uint32_write(req, P9_NOFID);
385 v9fs_string_write(req, uname);
386 v9fs_string_write(req, aname);
387 v9fs_uint32_write(req, opt.n_uname);
388 v9fs_req_send(req);
389
390 if (!opt.requestOnly) {
391 v9fs_req_wait_for_reply(req, NULL);
392 if (opt.expectErr) {
393 v9fs_rlerror(req, &err);
394 g_assert_cmpint(err, ==, opt.expectErr);
395 } else {
396 v9fs_rattach(req, opt.rattach.qid);
397 }
398 req = NULL;
399 }
400
401 return (TAttachRes) {
402 .req = req,
403 };
404}
405
406
407void v9fs_rattach(P9Req *req, v9fs_qid *qid)
408{
409 v9fs_req_recv(req, P9_RATTACH);
410 if (qid) {
411 v9fs_memread(req, qid, 13);
412 }
413 v9fs_req_free(req);
414}
415
416
417TWalkRes v9fs_twalk(TWalkOpt opt)
418{
419 P9Req *req;
420 int i;
421 uint32_t body_size = 4 + 4 + 2;
422 uint32_t err;
423 char **wnames = NULL;
424
425 g_assert(opt.client);
426
427 g_assert(!opt.path || !(opt.nwname || opt.wnames));
428
429 g_assert(!opt.expectErr || !(opt.rwalk.nwqid || opt.rwalk.wqid));
430
431 if (!opt.newfid) {
432 opt.newfid = genfid();
433 }
434
435 if (opt.path) {
436 opt.nwname = split(opt.path, "/", &wnames);
437 opt.wnames = wnames;
438 }
439
440 for (i = 0; i < opt.nwname; i++) {
441 uint16_t wname_size = v9fs_string_size(opt.wnames[i]);
442
443 g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size);
444 body_size += wname_size;
445 }
446 req = v9fs_req_init(opt.client, body_size, P9_TWALK, opt.tag);
447 v9fs_uint32_write(req, opt.fid);
448 v9fs_uint32_write(req, opt.newfid);
449 v9fs_uint16_write(req, opt.nwname);
450 for (i = 0; i < opt.nwname; i++) {
451 v9fs_string_write(req, opt.wnames[i]);
452 }
453 v9fs_req_send(req);
454
455 if (!opt.requestOnly) {
456 v9fs_req_wait_for_reply(req, NULL);
457 if (opt.expectErr) {
458 v9fs_rlerror(req, &err);
459 g_assert_cmpint(err, ==, opt.expectErr);
460 } else {
461 v9fs_rwalk(req, opt.rwalk.nwqid, opt.rwalk.wqid);
462 }
463 req = NULL;
464 }
465
466 split_free(&wnames);
467
468 return (TWalkRes) {
469 .newfid = opt.newfid,
470 .req = req,
471 };
472}
473
474
475void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid)
476{
477 uint16_t local_nwqid;
478
479 v9fs_req_recv(req, P9_RWALK);
480 v9fs_uint16_read(req, &local_nwqid);
481 if (nwqid) {
482 *nwqid = local_nwqid;
483 }
484 if (wqid) {
485 *wqid = g_malloc(local_nwqid * 13);
486 v9fs_memread(req, *wqid, local_nwqid * 13);
487 }
488 v9fs_req_free(req);
489}
490
491
492TGetAttrRes v9fs_tgetattr(TGetAttrOpt opt)
493{
494 P9Req *req;
495 uint32_t err;
496
497 g_assert(opt.client);
498
499 g_assert(!opt.expectErr || !opt.rgetattr.attr);
500
501 if (!opt.request_mask) {
502 opt.request_mask = P9_GETATTR_ALL;
503 }
504
505 req = v9fs_req_init(opt.client, 4 + 8, P9_TGETATTR, opt.tag);
506 v9fs_uint32_write(req, opt.fid);
507 v9fs_uint64_write(req, opt.request_mask);
508 v9fs_req_send(req);
509
510 if (!opt.requestOnly) {
511 v9fs_req_wait_for_reply(req, NULL);
512 if (opt.expectErr) {
513 v9fs_rlerror(req, &err);
514 g_assert_cmpint(err, ==, opt.expectErr);
515 } else {
516 v9fs_rgetattr(req, opt.rgetattr.attr);
517 }
518 req = NULL;
519 }
520
521 return (TGetAttrRes) { .req = req };
522}
523
524
525
526
527
528
529
530
531void v9fs_rgetattr(P9Req *req, v9fs_attr *attr)
532{
533 v9fs_req_recv(req, P9_RGETATTR);
534
535 v9fs_uint64_read(req, &attr->valid);
536 v9fs_memread(req, &attr->qid, 13);
537 v9fs_uint32_read(req, &attr->mode);
538 v9fs_uint32_read(req, &attr->uid);
539 v9fs_uint32_read(req, &attr->gid);
540 v9fs_uint64_read(req, &attr->nlink);
541 v9fs_uint64_read(req, &attr->rdev);
542 v9fs_uint64_read(req, &attr->size);
543 v9fs_uint64_read(req, &attr->blksize);
544 v9fs_uint64_read(req, &attr->blocks);
545 v9fs_uint64_read(req, &attr->atime_sec);
546 v9fs_uint64_read(req, &attr->atime_nsec);
547 v9fs_uint64_read(req, &attr->mtime_sec);
548 v9fs_uint64_read(req, &attr->mtime_nsec);
549 v9fs_uint64_read(req, &attr->ctime_sec);
550 v9fs_uint64_read(req, &attr->ctime_nsec);
551 v9fs_uint64_read(req, &attr->btime_sec);
552 v9fs_uint64_read(req, &attr->btime_nsec);
553 v9fs_uint64_read(req, &attr->gen);
554 v9fs_uint64_read(req, &attr->data_version);
555
556 v9fs_req_free(req);
557}
558
559
560TReadDirRes v9fs_treaddir(TReadDirOpt opt)
561{
562 P9Req *req;
563 uint32_t err;
564
565 g_assert(opt.client);
566
567 g_assert(!opt.expectErr || !(opt.rreaddir.count ||
568 opt.rreaddir.nentries || opt.rreaddir.entries));
569
570 req = v9fs_req_init(opt.client, 4 + 8 + 4, P9_TREADDIR, opt.tag);
571 v9fs_uint32_write(req, opt.fid);
572 v9fs_uint64_write(req, opt.offset);
573 v9fs_uint32_write(req, opt.count);
574 v9fs_req_send(req);
575
576 if (!opt.requestOnly) {
577 v9fs_req_wait_for_reply(req, NULL);
578 if (opt.expectErr) {
579 v9fs_rlerror(req, &err);
580 g_assert_cmpint(err, ==, opt.expectErr);
581 } else {
582 v9fs_rreaddir(req, opt.rreaddir.count, opt.rreaddir.nentries,
583 opt.rreaddir.entries);
584 }
585 req = NULL;
586 }
587
588 return (TReadDirRes) { .req = req };
589}
590
591
592void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries,
593 struct V9fsDirent **entries)
594{
595 uint32_t local_count;
596 struct V9fsDirent *e = NULL;
597
598 struct V9fsDirent *unused_entries = NULL;
599 uint16_t slen;
600 uint32_t n = 0;
601
602 v9fs_req_recv(req, P9_RREADDIR);
603 v9fs_uint32_read(req, &local_count);
604
605 if (count) {
606 *count = local_count;
607 }
608
609 for (int32_t togo = (int32_t)local_count;
610 togo >= 13 + 8 + 1 + 2;
611 togo -= 13 + 8 + 1 + 2 + slen, ++n)
612 {
613 if (!e) {
614 e = g_new(struct V9fsDirent, 1);
615 if (entries) {
616 *entries = e;
617 } else {
618 unused_entries = e;
619 }
620 } else {
621 e = e->next = g_new(struct V9fsDirent, 1);
622 }
623 e->next = NULL;
624
625 v9fs_memread(req, &e->qid, 13);
626 v9fs_uint64_read(req, &e->offset);
627 v9fs_uint8_read(req, &e->type);
628 v9fs_string_read(req, &slen, &e->name);
629 }
630
631 if (nentries) {
632 *nentries = n;
633 }
634
635 v9fs_free_dirents(unused_entries);
636 v9fs_req_free(req);
637}
638
639void v9fs_free_dirents(struct V9fsDirent *e)
640{
641 struct V9fsDirent *next = NULL;
642
643 for (; e; e = next) {
644 next = e->next;
645 g_free(e->name);
646 g_free(e);
647 }
648}
649
650
651TLOpenRes v9fs_tlopen(TLOpenOpt opt)
652{
653 P9Req *req;
654 uint32_t err;
655
656 g_assert(opt.client);
657
658 g_assert(!opt.expectErr || !(opt.rlopen.qid || opt.rlopen.iounit));
659
660 req = v9fs_req_init(opt.client, 4 + 4, P9_TLOPEN, opt.tag);
661 v9fs_uint32_write(req, opt.fid);
662 v9fs_uint32_write(req, opt.flags);
663 v9fs_req_send(req);
664
665 if (!opt.requestOnly) {
666 v9fs_req_wait_for_reply(req, NULL);
667 if (opt.expectErr) {
668 v9fs_rlerror(req, &err);
669 g_assert_cmpint(err, ==, opt.expectErr);
670 } else {
671 v9fs_rlopen(req, opt.rlopen.qid, opt.rlopen.iounit);
672 }
673 req = NULL;
674 }
675
676 return (TLOpenRes) { .req = req };
677}
678
679
680void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
681{
682 v9fs_req_recv(req, P9_RLOPEN);
683 if (qid) {
684 v9fs_memread(req, qid, 13);
685 } else {
686 v9fs_memskip(req, 13);
687 }
688 if (iounit) {
689 v9fs_uint32_read(req, iounit);
690 }
691 v9fs_req_free(req);
692}
693
694
695TWriteRes v9fs_twrite(TWriteOpt opt)
696{
697 P9Req *req;
698 uint32_t err;
699 uint32_t body_size = 4 + 8 + 4;
700 uint32_t written = 0;
701
702 g_assert(opt.client);
703
704 g_assert_cmpint(body_size, <=, UINT32_MAX - opt.count);
705 body_size += opt.count;
706 req = v9fs_req_init(opt.client, body_size, P9_TWRITE, opt.tag);
707 v9fs_uint32_write(req, opt.fid);
708 v9fs_uint64_write(req, opt.offset);
709 v9fs_uint32_write(req, opt.count);
710 v9fs_memwrite(req, opt.data, opt.count);
711 v9fs_req_send(req);
712
713 if (!opt.requestOnly) {
714 v9fs_req_wait_for_reply(req, NULL);
715 if (opt.expectErr) {
716 v9fs_rlerror(req, &err);
717 g_assert_cmpint(err, ==, opt.expectErr);
718 } else {
719 v9fs_rwrite(req, &written);
720 }
721 req = NULL;
722 }
723
724 return (TWriteRes) {
725 .req = req,
726 .count = written
727 };
728}
729
730
731void v9fs_rwrite(P9Req *req, uint32_t *count)
732{
733 v9fs_req_recv(req, P9_RWRITE);
734 if (count) {
735 v9fs_uint32_read(req, count);
736 }
737 v9fs_req_free(req);
738}
739
740
741TFlushRes v9fs_tflush(TFlushOpt opt)
742{
743 P9Req *req;
744 uint32_t err;
745
746 g_assert(opt.client);
747
748 req = v9fs_req_init(opt.client, 2, P9_TFLUSH, opt.tag);
749 v9fs_uint32_write(req, opt.oldtag);
750 v9fs_req_send(req);
751
752 if (!opt.requestOnly) {
753 v9fs_req_wait_for_reply(req, NULL);
754 if (opt.expectErr) {
755 v9fs_rlerror(req, &err);
756 g_assert_cmpint(err, ==, opt.expectErr);
757 } else {
758 v9fs_rflush(req);
759 }
760 req = NULL;
761 }
762
763 return (TFlushRes) { .req = req };
764}
765
766
767void v9fs_rflush(P9Req *req)
768{
769 v9fs_req_recv(req, P9_RFLUSH);
770 v9fs_req_free(req);
771}
772
773
774TMkdirRes v9fs_tmkdir(TMkdirOpt opt)
775{
776 P9Req *req;
777 uint32_t err;
778
779 g_assert(opt.client);
780
781 g_assert(!opt.atPath || !opt.dfid);
782
783 g_assert(!opt.expectErr || !opt.rmkdir.qid);
784
785 if (opt.atPath) {
786 opt.dfid = v9fs_twalk((TWalkOpt) { .client = opt.client,
787 .path = opt.atPath }).newfid;
788 }
789
790 if (!opt.mode) {
791 opt.mode = 0750;
792 }
793
794 uint32_t body_size = 4 + 4 + 4;
795 uint16_t string_size = v9fs_string_size(opt.name);
796
797 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
798 body_size += string_size;
799
800 req = v9fs_req_init(opt.client, body_size, P9_TMKDIR, opt.tag);
801 v9fs_uint32_write(req, opt.dfid);
802 v9fs_string_write(req, opt.name);
803 v9fs_uint32_write(req, opt.mode);
804 v9fs_uint32_write(req, opt.gid);
805 v9fs_req_send(req);
806
807 if (!opt.requestOnly) {
808 v9fs_req_wait_for_reply(req, NULL);
809 if (opt.expectErr) {
810 v9fs_rlerror(req, &err);
811 g_assert_cmpint(err, ==, opt.expectErr);
812 } else {
813 v9fs_rmkdir(req, opt.rmkdir.qid);
814 }
815 req = NULL;
816 }
817
818 return (TMkdirRes) { .req = req };
819}
820
821
822void v9fs_rmkdir(P9Req *req, v9fs_qid *qid)
823{
824 v9fs_req_recv(req, P9_RMKDIR);
825 if (qid) {
826 v9fs_memread(req, qid, 13);
827 } else {
828 v9fs_memskip(req, 13);
829 }
830 v9fs_req_free(req);
831}
832
833
834TlcreateRes v9fs_tlcreate(TlcreateOpt opt)
835{
836 P9Req *req;
837 uint32_t err;
838
839 g_assert(opt.client);
840
841 g_assert(!opt.atPath || !opt.fid);
842
843 g_assert(!opt.expectErr || !(opt.rlcreate.qid || opt.rlcreate.iounit));
844
845 if (opt.atPath) {
846 opt.fid = v9fs_twalk((TWalkOpt) { .client = opt.client,
847 .path = opt.atPath }).newfid;
848 }
849
850 if (!opt.mode) {
851 opt.mode = 0750;
852 }
853
854 uint32_t body_size = 4 + 4 + 4 + 4;
855 uint16_t string_size = v9fs_string_size(opt.name);
856
857 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
858 body_size += string_size;
859
860 req = v9fs_req_init(opt.client, body_size, P9_TLCREATE, opt.tag);
861 v9fs_uint32_write(req, opt.fid);
862 v9fs_string_write(req, opt.name);
863 v9fs_uint32_write(req, opt.flags);
864 v9fs_uint32_write(req, opt.mode);
865 v9fs_uint32_write(req, opt.gid);
866 v9fs_req_send(req);
867
868 if (!opt.requestOnly) {
869 v9fs_req_wait_for_reply(req, NULL);
870 if (opt.expectErr) {
871 v9fs_rlerror(req, &err);
872 g_assert_cmpint(err, ==, opt.expectErr);
873 } else {
874 v9fs_rlcreate(req, opt.rlcreate.qid, opt.rlcreate.iounit);
875 }
876 req = NULL;
877 }
878
879 return (TlcreateRes) { .req = req };
880}
881
882
883void v9fs_rlcreate(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
884{
885 v9fs_req_recv(req, P9_RLCREATE);
886 if (qid) {
887 v9fs_memread(req, qid, 13);
888 } else {
889 v9fs_memskip(req, 13);
890 }
891 if (iounit) {
892 v9fs_uint32_read(req, iounit);
893 }
894 v9fs_req_free(req);
895}
896
897
898TsymlinkRes v9fs_tsymlink(TsymlinkOpt opt)
899{
900 P9Req *req;
901 uint32_t err;
902
903 g_assert(opt.client);
904
905 g_assert(!opt.atPath || !opt.fid);
906
907 g_assert(!opt.expectErr || !opt.rsymlink.qid);
908
909 if (opt.atPath) {
910 opt.fid = v9fs_twalk((TWalkOpt) { .client = opt.client,
911 .path = opt.atPath }).newfid;
912 }
913
914 uint32_t body_size = 4 + 4;
915 uint16_t string_size = v9fs_string_size(opt.name) +
916 v9fs_string_size(opt.symtgt);
917
918 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
919 body_size += string_size;
920
921 req = v9fs_req_init(opt.client, body_size, P9_TSYMLINK, opt.tag);
922 v9fs_uint32_write(req, opt.fid);
923 v9fs_string_write(req, opt.name);
924 v9fs_string_write(req, opt.symtgt);
925 v9fs_uint32_write(req, opt.gid);
926 v9fs_req_send(req);
927
928 if (!opt.requestOnly) {
929 v9fs_req_wait_for_reply(req, NULL);
930 if (opt.expectErr) {
931 v9fs_rlerror(req, &err);
932 g_assert_cmpint(err, ==, opt.expectErr);
933 } else {
934 v9fs_rsymlink(req, opt.rsymlink.qid);
935 }
936 req = NULL;
937 }
938
939 return (TsymlinkRes) { .req = req };
940}
941
942
943void v9fs_rsymlink(P9Req *req, v9fs_qid *qid)
944{
945 v9fs_req_recv(req, P9_RSYMLINK);
946 if (qid) {
947 v9fs_memread(req, qid, 13);
948 } else {
949 v9fs_memskip(req, 13);
950 }
951 v9fs_req_free(req);
952}
953
954
955TlinkRes v9fs_tlink(TlinkOpt opt)
956{
957 P9Req *req;
958 uint32_t err;
959
960 g_assert(opt.client);
961
962 g_assert(!opt.atPath || !opt.dfid);
963
964 g_assert(!opt.toPath || !opt.fid);
965
966 if (opt.atPath) {
967 opt.dfid = v9fs_twalk((TWalkOpt) { .client = opt.client,
968 .path = opt.atPath }).newfid;
969 }
970 if (opt.toPath) {
971 opt.fid = v9fs_twalk((TWalkOpt) { .client = opt.client,
972 .path = opt.toPath }).newfid;
973 }
974
975 uint32_t body_size = 4 + 4;
976 uint16_t string_size = v9fs_string_size(opt.name);
977
978 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
979 body_size += string_size;
980
981 req = v9fs_req_init(opt.client, body_size, P9_TLINK, opt.tag);
982 v9fs_uint32_write(req, opt.dfid);
983 v9fs_uint32_write(req, opt.fid);
984 v9fs_string_write(req, opt.name);
985 v9fs_req_send(req);
986
987 if (!opt.requestOnly) {
988 v9fs_req_wait_for_reply(req, NULL);
989 if (opt.expectErr) {
990 v9fs_rlerror(req, &err);
991 g_assert_cmpint(err, ==, opt.expectErr);
992 } else {
993 v9fs_rlink(req);
994 }
995 req = NULL;
996 }
997
998 return (TlinkRes) { .req = req };
999}
1000
1001
1002void v9fs_rlink(P9Req *req)
1003{
1004 v9fs_req_recv(req, P9_RLINK);
1005 v9fs_req_free(req);
1006}
1007
1008
1009TunlinkatRes v9fs_tunlinkat(TunlinkatOpt opt)
1010{
1011 P9Req *req;
1012 uint32_t err;
1013
1014 g_assert(opt.client);
1015
1016 g_assert(!opt.atPath || !opt.dirfd);
1017
1018 if (opt.atPath) {
1019 opt.dirfd = v9fs_twalk((TWalkOpt) { .client = opt.client,
1020 .path = opt.atPath }).newfid;
1021 }
1022
1023 uint32_t body_size = 4 + 4;
1024 uint16_t string_size = v9fs_string_size(opt.name);
1025
1026 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
1027 body_size += string_size;
1028
1029 req = v9fs_req_init(opt.client, body_size, P9_TUNLINKAT, opt.tag);
1030 v9fs_uint32_write(req, opt.dirfd);
1031 v9fs_string_write(req, opt.name);
1032 v9fs_uint32_write(req, opt.flags);
1033 v9fs_req_send(req);
1034
1035 if (!opt.requestOnly) {
1036 v9fs_req_wait_for_reply(req, NULL);
1037 if (opt.expectErr) {
1038 v9fs_rlerror(req, &err);
1039 g_assert_cmpint(err, ==, opt.expectErr);
1040 } else {
1041 v9fs_runlinkat(req);
1042 }
1043 req = NULL;
1044 }
1045
1046 return (TunlinkatRes) { .req = req };
1047}
1048
1049
1050void v9fs_runlinkat(P9Req *req)
1051{
1052 v9fs_req_recv(req, P9_RUNLINKAT);
1053 v9fs_req_free(req);
1054}
1055