]> git.proxmox.com Git - mirror_qemu.git/blob - hw/9pfs/9p.c
9p: Treat multiple devices on one export as an error
[mirror_qemu.git] / hw / 9pfs / 9p.c
1 /*
2 * Virtio 9p backend
3 *
4 * Copyright IBM, Corp. 2010
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14 #include "qemu/osdep.h"
15 #include <glib/gprintf.h>
16 #include "hw/virtio/virtio.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "qemu/iov.h"
20 #include "qemu/main-loop.h"
21 #include "qemu/sockets.h"
22 #include "virtio-9p.h"
23 #include "fsdev/qemu-fsdev.h"
24 #include "9p-xattr.h"
25 #include "coth.h"
26 #include "trace.h"
27 #include "migration/blocker.h"
28 #include "sysemu/qtest.h"
29
30 int open_fd_hw;
31 int total_open_fd;
32 static int open_fd_rc;
33
34 enum {
35 Oread = 0x00,
36 Owrite = 0x01,
37 Ordwr = 0x02,
38 Oexec = 0x03,
39 Oexcl = 0x04,
40 Otrunc = 0x10,
41 Orexec = 0x20,
42 Orclose = 0x40,
43 Oappend = 0x80,
44 };
45
46 static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
47 {
48 ssize_t ret;
49 va_list ap;
50
51 va_start(ap, fmt);
52 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
53 va_end(ap);
54
55 return ret;
56 }
57
58 static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
59 {
60 ssize_t ret;
61 va_list ap;
62
63 va_start(ap, fmt);
64 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
65 va_end(ap);
66
67 return ret;
68 }
69
70 static int omode_to_uflags(int8_t mode)
71 {
72 int ret = 0;
73
74 switch (mode & 3) {
75 case Oread:
76 ret = O_RDONLY;
77 break;
78 case Ordwr:
79 ret = O_RDWR;
80 break;
81 case Owrite:
82 ret = O_WRONLY;
83 break;
84 case Oexec:
85 ret = O_RDONLY;
86 break;
87 }
88
89 if (mode & Otrunc) {
90 ret |= O_TRUNC;
91 }
92
93 if (mode & Oappend) {
94 ret |= O_APPEND;
95 }
96
97 if (mode & Oexcl) {
98 ret |= O_EXCL;
99 }
100
101 return ret;
102 }
103
104 typedef struct DotlOpenflagMap {
105 int dotl_flag;
106 int open_flag;
107 } DotlOpenflagMap;
108
109 static int dotl_to_open_flags(int flags)
110 {
111 int i;
112 /*
113 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
114 * and P9_DOTL_NOACCESS
115 */
116 int oflags = flags & O_ACCMODE;
117
118 DotlOpenflagMap dotl_oflag_map[] = {
119 { P9_DOTL_CREATE, O_CREAT },
120 { P9_DOTL_EXCL, O_EXCL },
121 { P9_DOTL_NOCTTY , O_NOCTTY },
122 { P9_DOTL_TRUNC, O_TRUNC },
123 { P9_DOTL_APPEND, O_APPEND },
124 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
125 { P9_DOTL_DSYNC, O_DSYNC },
126 { P9_DOTL_FASYNC, FASYNC },
127 { P9_DOTL_DIRECT, O_DIRECT },
128 { P9_DOTL_LARGEFILE, O_LARGEFILE },
129 { P9_DOTL_DIRECTORY, O_DIRECTORY },
130 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
131 { P9_DOTL_NOATIME, O_NOATIME },
132 { P9_DOTL_SYNC, O_SYNC },
133 };
134
135 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
136 if (flags & dotl_oflag_map[i].dotl_flag) {
137 oflags |= dotl_oflag_map[i].open_flag;
138 }
139 }
140
141 return oflags;
142 }
143
144 void cred_init(FsCred *credp)
145 {
146 credp->fc_uid = -1;
147 credp->fc_gid = -1;
148 credp->fc_mode = -1;
149 credp->fc_rdev = -1;
150 }
151
152 static int get_dotl_openflags(V9fsState *s, int oflags)
153 {
154 int flags;
155 /*
156 * Filter the client open flags
157 */
158 flags = dotl_to_open_flags(oflags);
159 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
160 /*
161 * Ignore direct disk access hint until the server supports it.
162 */
163 flags &= ~O_DIRECT;
164 return flags;
165 }
166
167 void v9fs_path_init(V9fsPath *path)
168 {
169 path->data = NULL;
170 path->size = 0;
171 }
172
173 void v9fs_path_free(V9fsPath *path)
174 {
175 g_free(path->data);
176 path->data = NULL;
177 path->size = 0;
178 }
179
180
181 void GCC_FMT_ATTR(2, 3)
182 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
183 {
184 va_list ap;
185
186 v9fs_path_free(path);
187
188 va_start(ap, fmt);
189 /* Bump the size for including terminating NULL */
190 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
191 va_end(ap);
192 }
193
194 void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src)
195 {
196 v9fs_path_free(dst);
197 dst->size = src->size;
198 dst->data = g_memdup(src->data, src->size);
199 }
200
201 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
202 const char *name, V9fsPath *path)
203 {
204 int err;
205 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
206 if (err < 0) {
207 err = -errno;
208 }
209 return err;
210 }
211
212 /*
213 * Return TRUE if s1 is an ancestor of s2.
214 *
215 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
216 * As a special case, We treat s1 as ancestor of s2 if they are same!
217 */
218 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
219 {
220 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
221 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
222 return 1;
223 }
224 }
225 return 0;
226 }
227
228 static size_t v9fs_string_size(V9fsString *str)
229 {
230 return str->size;
231 }
232
233 /*
234 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
235 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
236 {
237 int err = 1;
238 if (f->fid_type == P9_FID_FILE) {
239 if (f->fs.fd == -1) {
240 do {
241 err = v9fs_co_open(pdu, f, f->open_flags);
242 } while (err == -EINTR && !pdu->cancelled);
243 }
244 } else if (f->fid_type == P9_FID_DIR) {
245 if (f->fs.dir.stream == NULL) {
246 do {
247 err = v9fs_co_opendir(pdu, f);
248 } while (err == -EINTR && !pdu->cancelled);
249 }
250 }
251 return err;
252 }
253
254 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
255 {
256 int err;
257 V9fsFidState *f;
258 V9fsState *s = pdu->s;
259
260 for (f = s->fid_list; f; f = f->next) {
261 BUG_ON(f->clunked);
262 if (f->fid == fid) {
263 /*
264 * Update the fid ref upfront so that
265 * we don't get reclaimed when we yield
266 * in open later.
267 */
268 f->ref++;
269 /*
270 * check whether we need to reopen the
271 * file. We might have closed the fd
272 * while trying to free up some file
273 * descriptors.
274 */
275 err = v9fs_reopen_fid(pdu, f);
276 if (err < 0) {
277 f->ref--;
278 return NULL;
279 }
280 /*
281 * Mark the fid as referenced so that the LRU
282 * reclaim won't close the file descriptor
283 */
284 f->flags |= FID_REFERENCED;
285 return f;
286 }
287 }
288 return NULL;
289 }
290
291 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
292 {
293 V9fsFidState *f;
294
295 for (f = s->fid_list; f; f = f->next) {
296 /* If fid is already there return NULL */
297 BUG_ON(f->clunked);
298 if (f->fid == fid) {
299 return NULL;
300 }
301 }
302 f = g_malloc0(sizeof(V9fsFidState));
303 f->fid = fid;
304 f->fid_type = P9_FID_NONE;
305 f->ref = 1;
306 /*
307 * Mark the fid as referenced so that the LRU
308 * reclaim won't close the file descriptor
309 */
310 f->flags |= FID_REFERENCED;
311 f->next = s->fid_list;
312 s->fid_list = f;
313
314 v9fs_readdir_init(&f->fs.dir);
315 v9fs_readdir_init(&f->fs_reclaim.dir);
316
317 return f;
318 }
319
320 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
321 {
322 int retval = 0;
323
324 if (fidp->fs.xattr.xattrwalk_fid) {
325 /* getxattr/listxattr fid */
326 goto free_value;
327 }
328 /*
329 * if this is fid for setxattr. clunk should
330 * result in setxattr localcall
331 */
332 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
333 /* clunk after partial write */
334 retval = -EINVAL;
335 goto free_out;
336 }
337 if (fidp->fs.xattr.len) {
338 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
339 fidp->fs.xattr.value,
340 fidp->fs.xattr.len,
341 fidp->fs.xattr.flags);
342 } else {
343 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
344 }
345 free_out:
346 v9fs_string_free(&fidp->fs.xattr.name);
347 free_value:
348 g_free(fidp->fs.xattr.value);
349 return retval;
350 }
351
352 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
353 {
354 int retval = 0;
355
356 if (fidp->fid_type == P9_FID_FILE) {
357 /* If we reclaimed the fd no need to close */
358 if (fidp->fs.fd != -1) {
359 retval = v9fs_co_close(pdu, &fidp->fs);
360 }
361 } else if (fidp->fid_type == P9_FID_DIR) {
362 if (fidp->fs.dir.stream != NULL) {
363 retval = v9fs_co_closedir(pdu, &fidp->fs);
364 }
365 } else if (fidp->fid_type == P9_FID_XATTR) {
366 retval = v9fs_xattr_fid_clunk(pdu, fidp);
367 }
368 v9fs_path_free(&fidp->path);
369 g_free(fidp);
370 return retval;
371 }
372
373 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
374 {
375 BUG_ON(!fidp->ref);
376 fidp->ref--;
377 /*
378 * Don't free the fid if it is in reclaim list
379 */
380 if (!fidp->ref && fidp->clunked) {
381 if (fidp->fid == pdu->s->root_fid) {
382 /*
383 * if the clunked fid is root fid then we
384 * have unmounted the fs on the client side.
385 * delete the migration blocker. Ideally, this
386 * should be hooked to transport close notification
387 */
388 if (pdu->s->migration_blocker) {
389 migrate_del_blocker(pdu->s->migration_blocker);
390 error_free(pdu->s->migration_blocker);
391 pdu->s->migration_blocker = NULL;
392 }
393 }
394 return free_fid(pdu, fidp);
395 }
396 return 0;
397 }
398
399 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
400 {
401 V9fsFidState **fidpp, *fidp;
402
403 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
404 if ((*fidpp)->fid == fid) {
405 break;
406 }
407 }
408 if (*fidpp == NULL) {
409 return NULL;
410 }
411 fidp = *fidpp;
412 *fidpp = fidp->next;
413 fidp->clunked = 1;
414 return fidp;
415 }
416
417 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
418 {
419 int reclaim_count = 0;
420 V9fsState *s = pdu->s;
421 V9fsFidState *f, *reclaim_list = NULL;
422
423 for (f = s->fid_list; f; f = f->next) {
424 /*
425 * Unlink fids cannot be reclaimed. Check
426 * for them and skip them. Also skip fids
427 * currently being operated on.
428 */
429 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
430 continue;
431 }
432 /*
433 * if it is a recently referenced fid
434 * we leave the fid untouched and clear the
435 * reference bit. We come back to it later
436 * in the next iteration. (a simple LRU without
437 * moving list elements around)
438 */
439 if (f->flags & FID_REFERENCED) {
440 f->flags &= ~FID_REFERENCED;
441 continue;
442 }
443 /*
444 * Add fids to reclaim list.
445 */
446 if (f->fid_type == P9_FID_FILE) {
447 if (f->fs.fd != -1) {
448 /*
449 * Up the reference count so that
450 * a clunk request won't free this fid
451 */
452 f->ref++;
453 f->rclm_lst = reclaim_list;
454 reclaim_list = f;
455 f->fs_reclaim.fd = f->fs.fd;
456 f->fs.fd = -1;
457 reclaim_count++;
458 }
459 } else if (f->fid_type == P9_FID_DIR) {
460 if (f->fs.dir.stream != NULL) {
461 /*
462 * Up the reference count so that
463 * a clunk request won't free this fid
464 */
465 f->ref++;
466 f->rclm_lst = reclaim_list;
467 reclaim_list = f;
468 f->fs_reclaim.dir.stream = f->fs.dir.stream;
469 f->fs.dir.stream = NULL;
470 reclaim_count++;
471 }
472 }
473 if (reclaim_count >= open_fd_rc) {
474 break;
475 }
476 }
477 /*
478 * Now close the fid in reclaim list. Free them if they
479 * are already clunked.
480 */
481 while (reclaim_list) {
482 f = reclaim_list;
483 reclaim_list = f->rclm_lst;
484 if (f->fid_type == P9_FID_FILE) {
485 v9fs_co_close(pdu, &f->fs_reclaim);
486 } else if (f->fid_type == P9_FID_DIR) {
487 v9fs_co_closedir(pdu, &f->fs_reclaim);
488 }
489 f->rclm_lst = NULL;
490 /*
491 * Now drop the fid reference, free it
492 * if clunked.
493 */
494 put_fid(pdu, f);
495 }
496 }
497
498 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
499 {
500 int err;
501 V9fsState *s = pdu->s;
502 V9fsFidState *fidp, head_fid;
503
504 head_fid.next = s->fid_list;
505 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
506 if (fidp->path.size != path->size) {
507 continue;
508 }
509 if (!memcmp(fidp->path.data, path->data, path->size)) {
510 /* Mark the fid non reclaimable. */
511 fidp->flags |= FID_NON_RECLAIMABLE;
512
513 /* reopen the file/dir if already closed */
514 err = v9fs_reopen_fid(pdu, fidp);
515 if (err < 0) {
516 return err;
517 }
518 /*
519 * Go back to head of fid list because
520 * the list could have got updated when
521 * switched to the worker thread
522 */
523 if (err == 0) {
524 fidp = &head_fid;
525 }
526 }
527 }
528 return 0;
529 }
530
531 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
532 {
533 V9fsState *s = pdu->s;
534 V9fsFidState *fidp;
535
536 /* Free all fids */
537 while (s->fid_list) {
538 /* Get fid */
539 fidp = s->fid_list;
540 fidp->ref++;
541
542 /* Clunk fid */
543 s->fid_list = fidp->next;
544 fidp->clunked = 1;
545
546 put_fid(pdu, fidp);
547 }
548 }
549
550 #define P9_QID_TYPE_DIR 0x80
551 #define P9_QID_TYPE_SYMLINK 0x02
552
553 #define P9_STAT_MODE_DIR 0x80000000
554 #define P9_STAT_MODE_APPEND 0x40000000
555 #define P9_STAT_MODE_EXCL 0x20000000
556 #define P9_STAT_MODE_MOUNT 0x10000000
557 #define P9_STAT_MODE_AUTH 0x08000000
558 #define P9_STAT_MODE_TMP 0x04000000
559 #define P9_STAT_MODE_SYMLINK 0x02000000
560 #define P9_STAT_MODE_LINK 0x01000000
561 #define P9_STAT_MODE_DEVICE 0x00800000
562 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
563 #define P9_STAT_MODE_SOCKET 0x00100000
564 #define P9_STAT_MODE_SETUID 0x00080000
565 #define P9_STAT_MODE_SETGID 0x00040000
566 #define P9_STAT_MODE_SETVTX 0x00010000
567
568 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
569 P9_STAT_MODE_SYMLINK | \
570 P9_STAT_MODE_LINK | \
571 P9_STAT_MODE_DEVICE | \
572 P9_STAT_MODE_NAMED_PIPE | \
573 P9_STAT_MODE_SOCKET)
574
575 /* This is the algorithm from ufs in spfs */
576 static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp)
577 {
578 size_t size;
579
580 if (pdu->s->dev_id != stbuf->st_dev) {
581 warn_report_once(
582 "9p: Multiple devices detected in same VirtFS export, "
583 "which might lead to file ID collisions and severe "
584 "misbehaviours on guest! You should use a separate "
585 "export for each device shared from host."
586 );
587 }
588
589 memset(&qidp->path, 0, sizeof(qidp->path));
590 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
591 memcpy(&qidp->path, &stbuf->st_ino, size);
592 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
593 qidp->type = 0;
594 if (S_ISDIR(stbuf->st_mode)) {
595 qidp->type |= P9_QID_TYPE_DIR;
596 }
597 if (S_ISLNK(stbuf->st_mode)) {
598 qidp->type |= P9_QID_TYPE_SYMLINK;
599 }
600
601 return 0;
602 }
603
604 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
605 V9fsQID *qidp)
606 {
607 struct stat stbuf;
608 int err;
609
610 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
611 if (err < 0) {
612 return err;
613 }
614 err = stat_to_qid(pdu, &stbuf, qidp);
615 if (err < 0) {
616 return err;
617 }
618 return 0;
619 }
620
621 V9fsPDU *pdu_alloc(V9fsState *s)
622 {
623 V9fsPDU *pdu = NULL;
624
625 if (!QLIST_EMPTY(&s->free_list)) {
626 pdu = QLIST_FIRST(&s->free_list);
627 QLIST_REMOVE(pdu, next);
628 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
629 }
630 return pdu;
631 }
632
633 void pdu_free(V9fsPDU *pdu)
634 {
635 V9fsState *s = pdu->s;
636
637 g_assert(!pdu->cancelled);
638 QLIST_REMOVE(pdu, next);
639 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
640 }
641
642 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
643 {
644 int8_t id = pdu->id + 1; /* Response */
645 V9fsState *s = pdu->s;
646 int ret;
647
648 /*
649 * The 9p spec requires that successfully cancelled pdus receive no reply.
650 * Sending a reply would confuse clients because they would
651 * assume that any EINTR is the actual result of the operation,
652 * rather than a consequence of the cancellation. However, if
653 * the operation completed (succesfully or with an error other
654 * than caused be cancellation), we do send out that reply, both
655 * for efficiency and to avoid confusing the rest of the state machine
656 * that assumes passing a non-error here will mean a successful
657 * transmission of the reply.
658 */
659 bool discard = pdu->cancelled && len == -EINTR;
660 if (discard) {
661 trace_v9fs_rcancel(pdu->tag, pdu->id);
662 pdu->size = 0;
663 goto out_notify;
664 }
665
666 if (len < 0) {
667 int err = -len;
668 len = 7;
669
670 if (s->proto_version != V9FS_PROTO_2000L) {
671 V9fsString str;
672
673 str.data = strerror(err);
674 str.size = strlen(str.data);
675
676 ret = pdu_marshal(pdu, len, "s", &str);
677 if (ret < 0) {
678 goto out_notify;
679 }
680 len += ret;
681 id = P9_RERROR;
682 }
683
684 ret = pdu_marshal(pdu, len, "d", err);
685 if (ret < 0) {
686 goto out_notify;
687 }
688 len += ret;
689
690 if (s->proto_version == V9FS_PROTO_2000L) {
691 id = P9_RLERROR;
692 }
693 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
694 }
695
696 /* fill out the header */
697 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
698 goto out_notify;
699 }
700
701 /* keep these in sync */
702 pdu->size = len;
703 pdu->id = id;
704
705 out_notify:
706 pdu->s->transport->push_and_notify(pdu);
707
708 /* Now wakeup anybody waiting in flush for this request */
709 if (!qemu_co_queue_next(&pdu->complete)) {
710 pdu_free(pdu);
711 }
712 }
713
714 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
715 {
716 mode_t ret;
717
718 ret = mode & 0777;
719 if (mode & P9_STAT_MODE_DIR) {
720 ret |= S_IFDIR;
721 }
722
723 if (mode & P9_STAT_MODE_SYMLINK) {
724 ret |= S_IFLNK;
725 }
726 if (mode & P9_STAT_MODE_SOCKET) {
727 ret |= S_IFSOCK;
728 }
729 if (mode & P9_STAT_MODE_NAMED_PIPE) {
730 ret |= S_IFIFO;
731 }
732 if (mode & P9_STAT_MODE_DEVICE) {
733 if (extension->size && extension->data[0] == 'c') {
734 ret |= S_IFCHR;
735 } else {
736 ret |= S_IFBLK;
737 }
738 }
739
740 if (!(ret&~0777)) {
741 ret |= S_IFREG;
742 }
743
744 if (mode & P9_STAT_MODE_SETUID) {
745 ret |= S_ISUID;
746 }
747 if (mode & P9_STAT_MODE_SETGID) {
748 ret |= S_ISGID;
749 }
750 if (mode & P9_STAT_MODE_SETVTX) {
751 ret |= S_ISVTX;
752 }
753
754 return ret;
755 }
756
757 static int donttouch_stat(V9fsStat *stat)
758 {
759 if (stat->type == -1 &&
760 stat->dev == -1 &&
761 stat->qid.type == 0xff &&
762 stat->qid.version == (uint32_t) -1 &&
763 stat->qid.path == (uint64_t) -1 &&
764 stat->mode == -1 &&
765 stat->atime == -1 &&
766 stat->mtime == -1 &&
767 stat->length == -1 &&
768 !stat->name.size &&
769 !stat->uid.size &&
770 !stat->gid.size &&
771 !stat->muid.size &&
772 stat->n_uid == -1 &&
773 stat->n_gid == -1 &&
774 stat->n_muid == -1) {
775 return 1;
776 }
777
778 return 0;
779 }
780
781 static void v9fs_stat_init(V9fsStat *stat)
782 {
783 v9fs_string_init(&stat->name);
784 v9fs_string_init(&stat->uid);
785 v9fs_string_init(&stat->gid);
786 v9fs_string_init(&stat->muid);
787 v9fs_string_init(&stat->extension);
788 }
789
790 static void v9fs_stat_free(V9fsStat *stat)
791 {
792 v9fs_string_free(&stat->name);
793 v9fs_string_free(&stat->uid);
794 v9fs_string_free(&stat->gid);
795 v9fs_string_free(&stat->muid);
796 v9fs_string_free(&stat->extension);
797 }
798
799 static uint32_t stat_to_v9mode(const struct stat *stbuf)
800 {
801 uint32_t mode;
802
803 mode = stbuf->st_mode & 0777;
804 if (S_ISDIR(stbuf->st_mode)) {
805 mode |= P9_STAT_MODE_DIR;
806 }
807
808 if (S_ISLNK(stbuf->st_mode)) {
809 mode |= P9_STAT_MODE_SYMLINK;
810 }
811
812 if (S_ISSOCK(stbuf->st_mode)) {
813 mode |= P9_STAT_MODE_SOCKET;
814 }
815
816 if (S_ISFIFO(stbuf->st_mode)) {
817 mode |= P9_STAT_MODE_NAMED_PIPE;
818 }
819
820 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
821 mode |= P9_STAT_MODE_DEVICE;
822 }
823
824 if (stbuf->st_mode & S_ISUID) {
825 mode |= P9_STAT_MODE_SETUID;
826 }
827
828 if (stbuf->st_mode & S_ISGID) {
829 mode |= P9_STAT_MODE_SETGID;
830 }
831
832 if (stbuf->st_mode & S_ISVTX) {
833 mode |= P9_STAT_MODE_SETVTX;
834 }
835
836 return mode;
837 }
838
839 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
840 const char *basename,
841 const struct stat *stbuf,
842 V9fsStat *v9stat)
843 {
844 int err;
845
846 memset(v9stat, 0, sizeof(*v9stat));
847
848 err = stat_to_qid(pdu, stbuf, &v9stat->qid);
849 if (err < 0) {
850 return err;
851 }
852 v9stat->mode = stat_to_v9mode(stbuf);
853 v9stat->atime = stbuf->st_atime;
854 v9stat->mtime = stbuf->st_mtime;
855 v9stat->length = stbuf->st_size;
856
857 v9fs_string_free(&v9stat->uid);
858 v9fs_string_free(&v9stat->gid);
859 v9fs_string_free(&v9stat->muid);
860
861 v9stat->n_uid = stbuf->st_uid;
862 v9stat->n_gid = stbuf->st_gid;
863 v9stat->n_muid = 0;
864
865 v9fs_string_free(&v9stat->extension);
866
867 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
868 err = v9fs_co_readlink(pdu, path, &v9stat->extension);
869 if (err < 0) {
870 return err;
871 }
872 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
873 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
874 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
875 major(stbuf->st_rdev), minor(stbuf->st_rdev));
876 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
877 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
878 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
879 }
880
881 v9fs_string_sprintf(&v9stat->name, "%s", basename);
882
883 v9stat->size = 61 +
884 v9fs_string_size(&v9stat->name) +
885 v9fs_string_size(&v9stat->uid) +
886 v9fs_string_size(&v9stat->gid) +
887 v9fs_string_size(&v9stat->muid) +
888 v9fs_string_size(&v9stat->extension);
889 return 0;
890 }
891
892 #define P9_STATS_MODE 0x00000001ULL
893 #define P9_STATS_NLINK 0x00000002ULL
894 #define P9_STATS_UID 0x00000004ULL
895 #define P9_STATS_GID 0x00000008ULL
896 #define P9_STATS_RDEV 0x00000010ULL
897 #define P9_STATS_ATIME 0x00000020ULL
898 #define P9_STATS_MTIME 0x00000040ULL
899 #define P9_STATS_CTIME 0x00000080ULL
900 #define P9_STATS_INO 0x00000100ULL
901 #define P9_STATS_SIZE 0x00000200ULL
902 #define P9_STATS_BLOCKS 0x00000400ULL
903
904 #define P9_STATS_BTIME 0x00000800ULL
905 #define P9_STATS_GEN 0x00001000ULL
906 #define P9_STATS_DATA_VERSION 0x00002000ULL
907
908 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
909 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
910
911
912 static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf,
913 V9fsStatDotl *v9lstat)
914 {
915 memset(v9lstat, 0, sizeof(*v9lstat));
916
917 v9lstat->st_mode = stbuf->st_mode;
918 v9lstat->st_nlink = stbuf->st_nlink;
919 v9lstat->st_uid = stbuf->st_uid;
920 v9lstat->st_gid = stbuf->st_gid;
921 v9lstat->st_rdev = stbuf->st_rdev;
922 v9lstat->st_size = stbuf->st_size;
923 v9lstat->st_blksize = stbuf->st_blksize;
924 v9lstat->st_blocks = stbuf->st_blocks;
925 v9lstat->st_atime_sec = stbuf->st_atime;
926 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
927 v9lstat->st_mtime_sec = stbuf->st_mtime;
928 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
929 v9lstat->st_ctime_sec = stbuf->st_ctime;
930 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
931 /* Currently we only support BASIC fields in stat */
932 v9lstat->st_result_mask = P9_STATS_BASIC;
933
934 return stat_to_qid(pdu, stbuf, &v9lstat->qid);
935 }
936
937 static void print_sg(struct iovec *sg, int cnt)
938 {
939 int i;
940
941 printf("sg[%d]: {", cnt);
942 for (i = 0; i < cnt; i++) {
943 if (i) {
944 printf(", ");
945 }
946 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
947 }
948 printf("}\n");
949 }
950
951 /* Will call this only for path name based fid */
952 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
953 {
954 V9fsPath str;
955 v9fs_path_init(&str);
956 v9fs_path_copy(&str, dst);
957 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
958 v9fs_path_free(&str);
959 }
960
961 static inline bool is_ro_export(FsContext *ctx)
962 {
963 return ctx->export_flags & V9FS_RDONLY;
964 }
965
966 static void coroutine_fn v9fs_version(void *opaque)
967 {
968 ssize_t err;
969 V9fsPDU *pdu = opaque;
970 V9fsState *s = pdu->s;
971 V9fsString version;
972 size_t offset = 7;
973
974 v9fs_string_init(&version);
975 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
976 if (err < 0) {
977 goto out;
978 }
979 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
980
981 virtfs_reset(pdu);
982
983 if (!strcmp(version.data, "9P2000.u")) {
984 s->proto_version = V9FS_PROTO_2000U;
985 } else if (!strcmp(version.data, "9P2000.L")) {
986 s->proto_version = V9FS_PROTO_2000L;
987 } else {
988 v9fs_string_sprintf(&version, "unknown");
989 }
990
991 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
992 if (err < 0) {
993 goto out;
994 }
995 err += offset;
996 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
997 out:
998 pdu_complete(pdu, err);
999 v9fs_string_free(&version);
1000 }
1001
1002 static void coroutine_fn v9fs_attach(void *opaque)
1003 {
1004 V9fsPDU *pdu = opaque;
1005 V9fsState *s = pdu->s;
1006 int32_t fid, afid, n_uname;
1007 V9fsString uname, aname;
1008 V9fsFidState *fidp;
1009 size_t offset = 7;
1010 V9fsQID qid;
1011 ssize_t err;
1012 Error *local_err = NULL;
1013
1014 v9fs_string_init(&uname);
1015 v9fs_string_init(&aname);
1016 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1017 &afid, &uname, &aname, &n_uname);
1018 if (err < 0) {
1019 goto out_nofid;
1020 }
1021 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1022
1023 fidp = alloc_fid(s, fid);
1024 if (fidp == NULL) {
1025 err = -EINVAL;
1026 goto out_nofid;
1027 }
1028 fidp->uid = n_uname;
1029 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1030 if (err < 0) {
1031 err = -EINVAL;
1032 clunk_fid(s, fid);
1033 goto out;
1034 }
1035 err = fid_to_qid(pdu, fidp, &qid);
1036 if (err < 0) {
1037 err = -EINVAL;
1038 clunk_fid(s, fid);
1039 goto out;
1040 }
1041
1042 /*
1043 * disable migration if we haven't done already.
1044 * attach could get called multiple times for the same export.
1045 */
1046 if (!s->migration_blocker) {
1047 error_setg(&s->migration_blocker,
1048 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1049 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1050 err = migrate_add_blocker(s->migration_blocker, &local_err);
1051 if (local_err) {
1052 error_free(local_err);
1053 error_free(s->migration_blocker);
1054 s->migration_blocker = NULL;
1055 clunk_fid(s, fid);
1056 goto out;
1057 }
1058 s->root_fid = fid;
1059 }
1060
1061 err = pdu_marshal(pdu, offset, "Q", &qid);
1062 if (err < 0) {
1063 clunk_fid(s, fid);
1064 goto out;
1065 }
1066 err += offset;
1067
1068 memcpy(&s->root_qid, &qid, sizeof(qid));
1069 trace_v9fs_attach_return(pdu->tag, pdu->id,
1070 qid.type, qid.version, qid.path);
1071 out:
1072 put_fid(pdu, fidp);
1073 out_nofid:
1074 pdu_complete(pdu, err);
1075 v9fs_string_free(&uname);
1076 v9fs_string_free(&aname);
1077 }
1078
1079 static void coroutine_fn v9fs_stat(void *opaque)
1080 {
1081 int32_t fid;
1082 V9fsStat v9stat;
1083 ssize_t err = 0;
1084 size_t offset = 7;
1085 struct stat stbuf;
1086 V9fsFidState *fidp;
1087 V9fsPDU *pdu = opaque;
1088 char *basename;
1089
1090 err = pdu_unmarshal(pdu, offset, "d", &fid);
1091 if (err < 0) {
1092 goto out_nofid;
1093 }
1094 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1095
1096 fidp = get_fid(pdu, fid);
1097 if (fidp == NULL) {
1098 err = -ENOENT;
1099 goto out_nofid;
1100 }
1101 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1102 if (err < 0) {
1103 goto out;
1104 }
1105 basename = g_path_get_basename(fidp->path.data);
1106 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1107 g_free(basename);
1108 if (err < 0) {
1109 goto out;
1110 }
1111 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1112 if (err < 0) {
1113 v9fs_stat_free(&v9stat);
1114 goto out;
1115 }
1116 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1117 v9stat.atime, v9stat.mtime, v9stat.length);
1118 err += offset;
1119 v9fs_stat_free(&v9stat);
1120 out:
1121 put_fid(pdu, fidp);
1122 out_nofid:
1123 pdu_complete(pdu, err);
1124 }
1125
1126 static void coroutine_fn v9fs_getattr(void *opaque)
1127 {
1128 int32_t fid;
1129 size_t offset = 7;
1130 ssize_t retval = 0;
1131 struct stat stbuf;
1132 V9fsFidState *fidp;
1133 uint64_t request_mask;
1134 V9fsStatDotl v9stat_dotl;
1135 V9fsPDU *pdu = opaque;
1136
1137 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1138 if (retval < 0) {
1139 goto out_nofid;
1140 }
1141 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1142
1143 fidp = get_fid(pdu, fid);
1144 if (fidp == NULL) {
1145 retval = -ENOENT;
1146 goto out_nofid;
1147 }
1148 /*
1149 * Currently we only support BASIC fields in stat, so there is no
1150 * need to look at request_mask.
1151 */
1152 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1153 if (retval < 0) {
1154 goto out;
1155 }
1156 retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl);
1157 if (retval < 0) {
1158 goto out;
1159 }
1160
1161 /* fill st_gen if requested and supported by underlying fs */
1162 if (request_mask & P9_STATS_GEN) {
1163 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1164 switch (retval) {
1165 case 0:
1166 /* we have valid st_gen: update result mask */
1167 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1168 break;
1169 case -EINTR:
1170 /* request cancelled, e.g. by Tflush */
1171 goto out;
1172 default:
1173 /* failed to get st_gen: not fatal, ignore */
1174 break;
1175 }
1176 }
1177 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1178 if (retval < 0) {
1179 goto out;
1180 }
1181 retval += offset;
1182 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1183 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1184 v9stat_dotl.st_gid);
1185 out:
1186 put_fid(pdu, fidp);
1187 out_nofid:
1188 pdu_complete(pdu, retval);
1189 }
1190
1191 /* Attribute flags */
1192 #define P9_ATTR_MODE (1 << 0)
1193 #define P9_ATTR_UID (1 << 1)
1194 #define P9_ATTR_GID (1 << 2)
1195 #define P9_ATTR_SIZE (1 << 3)
1196 #define P9_ATTR_ATIME (1 << 4)
1197 #define P9_ATTR_MTIME (1 << 5)
1198 #define P9_ATTR_CTIME (1 << 6)
1199 #define P9_ATTR_ATIME_SET (1 << 7)
1200 #define P9_ATTR_MTIME_SET (1 << 8)
1201
1202 #define P9_ATTR_MASK 127
1203
1204 static void coroutine_fn v9fs_setattr(void *opaque)
1205 {
1206 int err = 0;
1207 int32_t fid;
1208 V9fsFidState *fidp;
1209 size_t offset = 7;
1210 V9fsIattr v9iattr;
1211 V9fsPDU *pdu = opaque;
1212
1213 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1214 if (err < 0) {
1215 goto out_nofid;
1216 }
1217
1218 trace_v9fs_setattr(pdu->tag, pdu->id, fid,
1219 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
1220 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
1221
1222 fidp = get_fid(pdu, fid);
1223 if (fidp == NULL) {
1224 err = -EINVAL;
1225 goto out_nofid;
1226 }
1227 if (v9iattr.valid & P9_ATTR_MODE) {
1228 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1229 if (err < 0) {
1230 goto out;
1231 }
1232 }
1233 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1234 struct timespec times[2];
1235 if (v9iattr.valid & P9_ATTR_ATIME) {
1236 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1237 times[0].tv_sec = v9iattr.atime_sec;
1238 times[0].tv_nsec = v9iattr.atime_nsec;
1239 } else {
1240 times[0].tv_nsec = UTIME_NOW;
1241 }
1242 } else {
1243 times[0].tv_nsec = UTIME_OMIT;
1244 }
1245 if (v9iattr.valid & P9_ATTR_MTIME) {
1246 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1247 times[1].tv_sec = v9iattr.mtime_sec;
1248 times[1].tv_nsec = v9iattr.mtime_nsec;
1249 } else {
1250 times[1].tv_nsec = UTIME_NOW;
1251 }
1252 } else {
1253 times[1].tv_nsec = UTIME_OMIT;
1254 }
1255 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1256 if (err < 0) {
1257 goto out;
1258 }
1259 }
1260 /*
1261 * If the only valid entry in iattr is ctime we can call
1262 * chown(-1,-1) to update the ctime of the file
1263 */
1264 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1265 ((v9iattr.valid & P9_ATTR_CTIME)
1266 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1267 if (!(v9iattr.valid & P9_ATTR_UID)) {
1268 v9iattr.uid = -1;
1269 }
1270 if (!(v9iattr.valid & P9_ATTR_GID)) {
1271 v9iattr.gid = -1;
1272 }
1273 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1274 v9iattr.gid);
1275 if (err < 0) {
1276 goto out;
1277 }
1278 }
1279 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1280 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1281 if (err < 0) {
1282 goto out;
1283 }
1284 }
1285 err = offset;
1286 trace_v9fs_setattr_return(pdu->tag, pdu->id);
1287 out:
1288 put_fid(pdu, fidp);
1289 out_nofid:
1290 pdu_complete(pdu, err);
1291 }
1292
1293 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1294 {
1295 int i;
1296 ssize_t err;
1297 size_t offset = 7;
1298
1299 err = pdu_marshal(pdu, offset, "w", nwnames);
1300 if (err < 0) {
1301 return err;
1302 }
1303 offset += err;
1304 for (i = 0; i < nwnames; i++) {
1305 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1306 if (err < 0) {
1307 return err;
1308 }
1309 offset += err;
1310 }
1311 return offset;
1312 }
1313
1314 static bool name_is_illegal(const char *name)
1315 {
1316 return !*name || strchr(name, '/') != NULL;
1317 }
1318
1319 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1320 {
1321 return
1322 qid1->type != qid2->type ||
1323 qid1->version != qid2->version ||
1324 qid1->path != qid2->path;
1325 }
1326
1327 static void coroutine_fn v9fs_walk(void *opaque)
1328 {
1329 int name_idx;
1330 V9fsQID *qids = NULL;
1331 int i, err = 0;
1332 V9fsPath dpath, path;
1333 uint16_t nwnames;
1334 struct stat stbuf;
1335 size_t offset = 7;
1336 int32_t fid, newfid;
1337 V9fsString *wnames = NULL;
1338 V9fsFidState *fidp;
1339 V9fsFidState *newfidp = NULL;
1340 V9fsPDU *pdu = opaque;
1341 V9fsState *s = pdu->s;
1342 V9fsQID qid;
1343
1344 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1345 if (err < 0) {
1346 pdu_complete(pdu, err);
1347 return ;
1348 }
1349 offset += err;
1350
1351 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1352
1353 if (nwnames && nwnames <= P9_MAXWELEM) {
1354 wnames = g_new0(V9fsString, nwnames);
1355 qids = g_new0(V9fsQID, nwnames);
1356 for (i = 0; i < nwnames; i++) {
1357 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1358 if (err < 0) {
1359 goto out_nofid;
1360 }
1361 if (name_is_illegal(wnames[i].data)) {
1362 err = -ENOENT;
1363 goto out_nofid;
1364 }
1365 offset += err;
1366 }
1367 } else if (nwnames > P9_MAXWELEM) {
1368 err = -EINVAL;
1369 goto out_nofid;
1370 }
1371 fidp = get_fid(pdu, fid);
1372 if (fidp == NULL) {
1373 err = -ENOENT;
1374 goto out_nofid;
1375 }
1376
1377 v9fs_path_init(&dpath);
1378 v9fs_path_init(&path);
1379
1380 err = fid_to_qid(pdu, fidp, &qid);
1381 if (err < 0) {
1382 goto out;
1383 }
1384
1385 /*
1386 * Both dpath and path initially poin to fidp.
1387 * Needed to handle request with nwnames == 0
1388 */
1389 v9fs_path_copy(&dpath, &fidp->path);
1390 v9fs_path_copy(&path, &fidp->path);
1391 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1392 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1393 strcmp("..", wnames[name_idx].data)) {
1394 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1395 &path);
1396 if (err < 0) {
1397 goto out;
1398 }
1399
1400 err = v9fs_co_lstat(pdu, &path, &stbuf);
1401 if (err < 0) {
1402 goto out;
1403 }
1404 err = stat_to_qid(pdu, &stbuf, &qid);
1405 if (err < 0) {
1406 goto out;
1407 }
1408 v9fs_path_copy(&dpath, &path);
1409 }
1410 memcpy(&qids[name_idx], &qid, sizeof(qid));
1411 }
1412 if (fid == newfid) {
1413 if (fidp->fid_type != P9_FID_NONE) {
1414 err = -EINVAL;
1415 goto out;
1416 }
1417 v9fs_path_write_lock(s);
1418 v9fs_path_copy(&fidp->path, &path);
1419 v9fs_path_unlock(s);
1420 } else {
1421 newfidp = alloc_fid(s, newfid);
1422 if (newfidp == NULL) {
1423 err = -EINVAL;
1424 goto out;
1425 }
1426 newfidp->uid = fidp->uid;
1427 v9fs_path_copy(&newfidp->path, &path);
1428 }
1429 err = v9fs_walk_marshal(pdu, nwnames, qids);
1430 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1431 out:
1432 put_fid(pdu, fidp);
1433 if (newfidp) {
1434 put_fid(pdu, newfidp);
1435 }
1436 v9fs_path_free(&dpath);
1437 v9fs_path_free(&path);
1438 out_nofid:
1439 pdu_complete(pdu, err);
1440 if (nwnames && nwnames <= P9_MAXWELEM) {
1441 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1442 v9fs_string_free(&wnames[name_idx]);
1443 }
1444 g_free(wnames);
1445 g_free(qids);
1446 }
1447 }
1448
1449 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1450 {
1451 struct statfs stbuf;
1452 int32_t iounit = 0;
1453 V9fsState *s = pdu->s;
1454
1455 /*
1456 * iounit should be multiples of f_bsize (host filesystem block size
1457 * and as well as less than (client msize - P9_IOHDRSZ))
1458 */
1459 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1460 iounit = stbuf.f_bsize;
1461 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1462 }
1463 if (!iounit) {
1464 iounit = s->msize - P9_IOHDRSZ;
1465 }
1466 return iounit;
1467 }
1468
1469 static void coroutine_fn v9fs_open(void *opaque)
1470 {
1471 int flags;
1472 int32_t fid;
1473 int32_t mode;
1474 V9fsQID qid;
1475 int iounit = 0;
1476 ssize_t err = 0;
1477 size_t offset = 7;
1478 struct stat stbuf;
1479 V9fsFidState *fidp;
1480 V9fsPDU *pdu = opaque;
1481 V9fsState *s = pdu->s;
1482
1483 if (s->proto_version == V9FS_PROTO_2000L) {
1484 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1485 } else {
1486 uint8_t modebyte;
1487 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1488 mode = modebyte;
1489 }
1490 if (err < 0) {
1491 goto out_nofid;
1492 }
1493 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1494
1495 fidp = get_fid(pdu, fid);
1496 if (fidp == NULL) {
1497 err = -ENOENT;
1498 goto out_nofid;
1499 }
1500 if (fidp->fid_type != P9_FID_NONE) {
1501 err = -EINVAL;
1502 goto out;
1503 }
1504
1505 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1506 if (err < 0) {
1507 goto out;
1508 }
1509 err = stat_to_qid(pdu, &stbuf, &qid);
1510 if (err < 0) {
1511 goto out;
1512 }
1513 if (S_ISDIR(stbuf.st_mode)) {
1514 err = v9fs_co_opendir(pdu, fidp);
1515 if (err < 0) {
1516 goto out;
1517 }
1518 fidp->fid_type = P9_FID_DIR;
1519 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1520 if (err < 0) {
1521 goto out;
1522 }
1523 err += offset;
1524 } else {
1525 if (s->proto_version == V9FS_PROTO_2000L) {
1526 flags = get_dotl_openflags(s, mode);
1527 } else {
1528 flags = omode_to_uflags(mode);
1529 }
1530 if (is_ro_export(&s->ctx)) {
1531 if (mode & O_WRONLY || mode & O_RDWR ||
1532 mode & O_APPEND || mode & O_TRUNC) {
1533 err = -EROFS;
1534 goto out;
1535 }
1536 }
1537 err = v9fs_co_open(pdu, fidp, flags);
1538 if (err < 0) {
1539 goto out;
1540 }
1541 fidp->fid_type = P9_FID_FILE;
1542 fidp->open_flags = flags;
1543 if (flags & O_EXCL) {
1544 /*
1545 * We let the host file system do O_EXCL check
1546 * We should not reclaim such fd
1547 */
1548 fidp->flags |= FID_NON_RECLAIMABLE;
1549 }
1550 iounit = get_iounit(pdu, &fidp->path);
1551 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1552 if (err < 0) {
1553 goto out;
1554 }
1555 err += offset;
1556 }
1557 trace_v9fs_open_return(pdu->tag, pdu->id,
1558 qid.type, qid.version, qid.path, iounit);
1559 out:
1560 put_fid(pdu, fidp);
1561 out_nofid:
1562 pdu_complete(pdu, err);
1563 }
1564
1565 static void coroutine_fn v9fs_lcreate(void *opaque)
1566 {
1567 int32_t dfid, flags, mode;
1568 gid_t gid;
1569 ssize_t err = 0;
1570 ssize_t offset = 7;
1571 V9fsString name;
1572 V9fsFidState *fidp;
1573 struct stat stbuf;
1574 V9fsQID qid;
1575 int32_t iounit;
1576 V9fsPDU *pdu = opaque;
1577
1578 v9fs_string_init(&name);
1579 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1580 &name, &flags, &mode, &gid);
1581 if (err < 0) {
1582 goto out_nofid;
1583 }
1584 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1585
1586 if (name_is_illegal(name.data)) {
1587 err = -ENOENT;
1588 goto out_nofid;
1589 }
1590
1591 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1592 err = -EEXIST;
1593 goto out_nofid;
1594 }
1595
1596 fidp = get_fid(pdu, dfid);
1597 if (fidp == NULL) {
1598 err = -ENOENT;
1599 goto out_nofid;
1600 }
1601 if (fidp->fid_type != P9_FID_NONE) {
1602 err = -EINVAL;
1603 goto out;
1604 }
1605
1606 flags = get_dotl_openflags(pdu->s, flags);
1607 err = v9fs_co_open2(pdu, fidp, &name, gid,
1608 flags | O_CREAT, mode, &stbuf);
1609 if (err < 0) {
1610 goto out;
1611 }
1612 fidp->fid_type = P9_FID_FILE;
1613 fidp->open_flags = flags;
1614 if (flags & O_EXCL) {
1615 /*
1616 * We let the host file system do O_EXCL check
1617 * We should not reclaim such fd
1618 */
1619 fidp->flags |= FID_NON_RECLAIMABLE;
1620 }
1621 iounit = get_iounit(pdu, &fidp->path);
1622 err = stat_to_qid(pdu, &stbuf, &qid);
1623 if (err < 0) {
1624 goto out;
1625 }
1626 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1627 if (err < 0) {
1628 goto out;
1629 }
1630 err += offset;
1631 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1632 qid.type, qid.version, qid.path, iounit);
1633 out:
1634 put_fid(pdu, fidp);
1635 out_nofid:
1636 pdu_complete(pdu, err);
1637 v9fs_string_free(&name);
1638 }
1639
1640 static void coroutine_fn v9fs_fsync(void *opaque)
1641 {
1642 int err;
1643 int32_t fid;
1644 int datasync;
1645 size_t offset = 7;
1646 V9fsFidState *fidp;
1647 V9fsPDU *pdu = opaque;
1648
1649 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1650 if (err < 0) {
1651 goto out_nofid;
1652 }
1653 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1654
1655 fidp = get_fid(pdu, fid);
1656 if (fidp == NULL) {
1657 err = -ENOENT;
1658 goto out_nofid;
1659 }
1660 err = v9fs_co_fsync(pdu, fidp, datasync);
1661 if (!err) {
1662 err = offset;
1663 }
1664 put_fid(pdu, fidp);
1665 out_nofid:
1666 pdu_complete(pdu, err);
1667 }
1668
1669 static void coroutine_fn v9fs_clunk(void *opaque)
1670 {
1671 int err;
1672 int32_t fid;
1673 size_t offset = 7;
1674 V9fsFidState *fidp;
1675 V9fsPDU *pdu = opaque;
1676 V9fsState *s = pdu->s;
1677
1678 err = pdu_unmarshal(pdu, offset, "d", &fid);
1679 if (err < 0) {
1680 goto out_nofid;
1681 }
1682 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1683
1684 fidp = clunk_fid(s, fid);
1685 if (fidp == NULL) {
1686 err = -ENOENT;
1687 goto out_nofid;
1688 }
1689 /*
1690 * Bump the ref so that put_fid will
1691 * free the fid.
1692 */
1693 fidp->ref++;
1694 err = put_fid(pdu, fidp);
1695 if (!err) {
1696 err = offset;
1697 }
1698 out_nofid:
1699 pdu_complete(pdu, err);
1700 }
1701
1702 /*
1703 * Create a QEMUIOVector for a sub-region of PDU iovecs
1704 *
1705 * @qiov: uninitialized QEMUIOVector
1706 * @skip: number of bytes to skip from beginning of PDU
1707 * @size: number of bytes to include
1708 * @is_write: true - write, false - read
1709 *
1710 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1711 * with qemu_iovec_destroy().
1712 */
1713 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1714 size_t skip, size_t size,
1715 bool is_write)
1716 {
1717 QEMUIOVector elem;
1718 struct iovec *iov;
1719 unsigned int niov;
1720
1721 if (is_write) {
1722 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
1723 } else {
1724 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
1725 }
1726
1727 qemu_iovec_init_external(&elem, iov, niov);
1728 qemu_iovec_init(qiov, niov);
1729 qemu_iovec_concat(qiov, &elem, skip, size);
1730 }
1731
1732 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1733 uint64_t off, uint32_t max_count)
1734 {
1735 ssize_t err;
1736 size_t offset = 7;
1737 uint64_t read_count;
1738 QEMUIOVector qiov_full;
1739
1740 if (fidp->fs.xattr.len < off) {
1741 read_count = 0;
1742 } else {
1743 read_count = fidp->fs.xattr.len - off;
1744 }
1745 if (read_count > max_count) {
1746 read_count = max_count;
1747 }
1748 err = pdu_marshal(pdu, offset, "d", read_count);
1749 if (err < 0) {
1750 return err;
1751 }
1752 offset += err;
1753
1754 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1755 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
1756 ((char *)fidp->fs.xattr.value) + off,
1757 read_count);
1758 qemu_iovec_destroy(&qiov_full);
1759 if (err < 0) {
1760 return err;
1761 }
1762 offset += err;
1763 return offset;
1764 }
1765
1766 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1767 V9fsFidState *fidp,
1768 uint32_t max_count)
1769 {
1770 V9fsPath path;
1771 V9fsStat v9stat;
1772 int len, err = 0;
1773 int32_t count = 0;
1774 struct stat stbuf;
1775 off_t saved_dir_pos;
1776 struct dirent *dent;
1777
1778 /* save the directory position */
1779 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1780 if (saved_dir_pos < 0) {
1781 return saved_dir_pos;
1782 }
1783
1784 while (1) {
1785 v9fs_path_init(&path);
1786
1787 v9fs_readdir_lock(&fidp->fs.dir);
1788
1789 err = v9fs_co_readdir(pdu, fidp, &dent);
1790 if (err || !dent) {
1791 break;
1792 }
1793 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1794 if (err < 0) {
1795 break;
1796 }
1797 err = v9fs_co_lstat(pdu, &path, &stbuf);
1798 if (err < 0) {
1799 break;
1800 }
1801 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
1802 if (err < 0) {
1803 break;
1804 }
1805 if ((count + v9stat.size + 2) > max_count) {
1806 v9fs_readdir_unlock(&fidp->fs.dir);
1807
1808 /* Ran out of buffer. Set dir back to old position and return */
1809 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1810 v9fs_stat_free(&v9stat);
1811 v9fs_path_free(&path);
1812 return count;
1813 }
1814
1815 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1816 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1817
1818 v9fs_readdir_unlock(&fidp->fs.dir);
1819
1820 if (len < 0) {
1821 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1822 v9fs_stat_free(&v9stat);
1823 v9fs_path_free(&path);
1824 return len;
1825 }
1826 count += len;
1827 v9fs_stat_free(&v9stat);
1828 v9fs_path_free(&path);
1829 saved_dir_pos = dent->d_off;
1830 }
1831
1832 v9fs_readdir_unlock(&fidp->fs.dir);
1833
1834 v9fs_path_free(&path);
1835 if (err < 0) {
1836 return err;
1837 }
1838 return count;
1839 }
1840
1841 static void coroutine_fn v9fs_read(void *opaque)
1842 {
1843 int32_t fid;
1844 uint64_t off;
1845 ssize_t err = 0;
1846 int32_t count = 0;
1847 size_t offset = 7;
1848 uint32_t max_count;
1849 V9fsFidState *fidp;
1850 V9fsPDU *pdu = opaque;
1851 V9fsState *s = pdu->s;
1852
1853 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1854 if (err < 0) {
1855 goto out_nofid;
1856 }
1857 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1858
1859 fidp = get_fid(pdu, fid);
1860 if (fidp == NULL) {
1861 err = -EINVAL;
1862 goto out_nofid;
1863 }
1864 if (fidp->fid_type == P9_FID_DIR) {
1865
1866 if (off == 0) {
1867 v9fs_co_rewinddir(pdu, fidp);
1868 }
1869 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1870 if (count < 0) {
1871 err = count;
1872 goto out;
1873 }
1874 err = pdu_marshal(pdu, offset, "d", count);
1875 if (err < 0) {
1876 goto out;
1877 }
1878 err += offset + count;
1879 } else if (fidp->fid_type == P9_FID_FILE) {
1880 QEMUIOVector qiov_full;
1881 QEMUIOVector qiov;
1882 int32_t len;
1883
1884 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1885 qemu_iovec_init(&qiov, qiov_full.niov);
1886 do {
1887 qemu_iovec_reset(&qiov);
1888 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1889 if (0) {
1890 print_sg(qiov.iov, qiov.niov);
1891 }
1892 /* Loop in case of EINTR */
1893 do {
1894 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1895 if (len >= 0) {
1896 off += len;
1897 count += len;
1898 }
1899 } while (len == -EINTR && !pdu->cancelled);
1900 if (len < 0) {
1901 /* IO error return the error */
1902 err = len;
1903 goto out_free_iovec;
1904 }
1905 } while (count < max_count && len > 0);
1906 err = pdu_marshal(pdu, offset, "d", count);
1907 if (err < 0) {
1908 goto out_free_iovec;
1909 }
1910 err += offset + count;
1911 out_free_iovec:
1912 qemu_iovec_destroy(&qiov);
1913 qemu_iovec_destroy(&qiov_full);
1914 } else if (fidp->fid_type == P9_FID_XATTR) {
1915 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1916 } else {
1917 err = -EINVAL;
1918 }
1919 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1920 out:
1921 put_fid(pdu, fidp);
1922 out_nofid:
1923 pdu_complete(pdu, err);
1924 }
1925
1926 static size_t v9fs_readdir_data_size(V9fsString *name)
1927 {
1928 /*
1929 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1930 * size of type (1) + size of name.size (2) + strlen(name.data)
1931 */
1932 return 24 + v9fs_string_size(name);
1933 }
1934
1935 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1936 int32_t max_count)
1937 {
1938 size_t size;
1939 V9fsQID qid;
1940 V9fsString name;
1941 int len, err = 0;
1942 int32_t count = 0;
1943 off_t saved_dir_pos;
1944 struct dirent *dent;
1945
1946 /* save the directory position */
1947 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1948 if (saved_dir_pos < 0) {
1949 return saved_dir_pos;
1950 }
1951
1952 while (1) {
1953 v9fs_readdir_lock(&fidp->fs.dir);
1954
1955 err = v9fs_co_readdir(pdu, fidp, &dent);
1956 if (err || !dent) {
1957 break;
1958 }
1959 v9fs_string_init(&name);
1960 v9fs_string_sprintf(&name, "%s", dent->d_name);
1961 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1962 v9fs_readdir_unlock(&fidp->fs.dir);
1963
1964 /* Ran out of buffer. Set dir back to old position and return */
1965 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1966 v9fs_string_free(&name);
1967 return count;
1968 }
1969 /*
1970 * Fill up just the path field of qid because the client uses
1971 * only that. To fill the entire qid structure we will have
1972 * to stat each dirent found, which is expensive
1973 */
1974 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1975 memcpy(&qid.path, &dent->d_ino, size);
1976 /* Fill the other fields with dummy values */
1977 qid.type = 0;
1978 qid.version = 0;
1979
1980 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1981 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1982 &qid, dent->d_off,
1983 dent->d_type, &name);
1984
1985 v9fs_readdir_unlock(&fidp->fs.dir);
1986
1987 if (len < 0) {
1988 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1989 v9fs_string_free(&name);
1990 return len;
1991 }
1992 count += len;
1993 v9fs_string_free(&name);
1994 saved_dir_pos = dent->d_off;
1995 }
1996
1997 v9fs_readdir_unlock(&fidp->fs.dir);
1998
1999 if (err < 0) {
2000 return err;
2001 }
2002 return count;
2003 }
2004
2005 static void coroutine_fn v9fs_readdir(void *opaque)
2006 {
2007 int32_t fid;
2008 V9fsFidState *fidp;
2009 ssize_t retval = 0;
2010 size_t offset = 7;
2011 uint64_t initial_offset;
2012 int32_t count;
2013 uint32_t max_count;
2014 V9fsPDU *pdu = opaque;
2015
2016 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
2017 &initial_offset, &max_count);
2018 if (retval < 0) {
2019 goto out_nofid;
2020 }
2021 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
2022
2023 fidp = get_fid(pdu, fid);
2024 if (fidp == NULL) {
2025 retval = -EINVAL;
2026 goto out_nofid;
2027 }
2028 if (!fidp->fs.dir.stream) {
2029 retval = -EINVAL;
2030 goto out;
2031 }
2032 if (initial_offset == 0) {
2033 v9fs_co_rewinddir(pdu, fidp);
2034 } else {
2035 v9fs_co_seekdir(pdu, fidp, initial_offset);
2036 }
2037 count = v9fs_do_readdir(pdu, fidp, max_count);
2038 if (count < 0) {
2039 retval = count;
2040 goto out;
2041 }
2042 retval = pdu_marshal(pdu, offset, "d", count);
2043 if (retval < 0) {
2044 goto out;
2045 }
2046 retval += count + offset;
2047 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2048 out:
2049 put_fid(pdu, fidp);
2050 out_nofid:
2051 pdu_complete(pdu, retval);
2052 }
2053
2054 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2055 uint64_t off, uint32_t count,
2056 struct iovec *sg, int cnt)
2057 {
2058 int i, to_copy;
2059 ssize_t err = 0;
2060 uint64_t write_count;
2061 size_t offset = 7;
2062
2063
2064 if (fidp->fs.xattr.len < off) {
2065 err = -ENOSPC;
2066 goto out;
2067 }
2068 write_count = fidp->fs.xattr.len - off;
2069 if (write_count > count) {
2070 write_count = count;
2071 }
2072 err = pdu_marshal(pdu, offset, "d", write_count);
2073 if (err < 0) {
2074 return err;
2075 }
2076 err += offset;
2077 fidp->fs.xattr.copied_len += write_count;
2078 /*
2079 * Now copy the content from sg list
2080 */
2081 for (i = 0; i < cnt; i++) {
2082 if (write_count > sg[i].iov_len) {
2083 to_copy = sg[i].iov_len;
2084 } else {
2085 to_copy = write_count;
2086 }
2087 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2088 /* updating vs->off since we are not using below */
2089 off += to_copy;
2090 write_count -= to_copy;
2091 }
2092 out:
2093 return err;
2094 }
2095
2096 static void coroutine_fn v9fs_write(void *opaque)
2097 {
2098 ssize_t err;
2099 int32_t fid;
2100 uint64_t off;
2101 uint32_t count;
2102 int32_t len = 0;
2103 int32_t total = 0;
2104 size_t offset = 7;
2105 V9fsFidState *fidp;
2106 V9fsPDU *pdu = opaque;
2107 V9fsState *s = pdu->s;
2108 QEMUIOVector qiov_full;
2109 QEMUIOVector qiov;
2110
2111 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2112 if (err < 0) {
2113 pdu_complete(pdu, err);
2114 return;
2115 }
2116 offset += err;
2117 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2118 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2119
2120 fidp = get_fid(pdu, fid);
2121 if (fidp == NULL) {
2122 err = -EINVAL;
2123 goto out_nofid;
2124 }
2125 if (fidp->fid_type == P9_FID_FILE) {
2126 if (fidp->fs.fd == -1) {
2127 err = -EINVAL;
2128 goto out;
2129 }
2130 } else if (fidp->fid_type == P9_FID_XATTR) {
2131 /*
2132 * setxattr operation
2133 */
2134 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2135 qiov_full.iov, qiov_full.niov);
2136 goto out;
2137 } else {
2138 err = -EINVAL;
2139 goto out;
2140 }
2141 qemu_iovec_init(&qiov, qiov_full.niov);
2142 do {
2143 qemu_iovec_reset(&qiov);
2144 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2145 if (0) {
2146 print_sg(qiov.iov, qiov.niov);
2147 }
2148 /* Loop in case of EINTR */
2149 do {
2150 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2151 if (len >= 0) {
2152 off += len;
2153 total += len;
2154 }
2155 } while (len == -EINTR && !pdu->cancelled);
2156 if (len < 0) {
2157 /* IO error return the error */
2158 err = len;
2159 goto out_qiov;
2160 }
2161 } while (total < count && len > 0);
2162
2163 offset = 7;
2164 err = pdu_marshal(pdu, offset, "d", total);
2165 if (err < 0) {
2166 goto out_qiov;
2167 }
2168 err += offset;
2169 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2170 out_qiov:
2171 qemu_iovec_destroy(&qiov);
2172 out:
2173 put_fid(pdu, fidp);
2174 out_nofid:
2175 qemu_iovec_destroy(&qiov_full);
2176 pdu_complete(pdu, err);
2177 }
2178
2179 static void coroutine_fn v9fs_create(void *opaque)
2180 {
2181 int32_t fid;
2182 int err = 0;
2183 size_t offset = 7;
2184 V9fsFidState *fidp;
2185 V9fsQID qid;
2186 int32_t perm;
2187 int8_t mode;
2188 V9fsPath path;
2189 struct stat stbuf;
2190 V9fsString name;
2191 V9fsString extension;
2192 int iounit;
2193 V9fsPDU *pdu = opaque;
2194 V9fsState *s = pdu->s;
2195
2196 v9fs_path_init(&path);
2197 v9fs_string_init(&name);
2198 v9fs_string_init(&extension);
2199 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2200 &perm, &mode, &extension);
2201 if (err < 0) {
2202 goto out_nofid;
2203 }
2204 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2205
2206 if (name_is_illegal(name.data)) {
2207 err = -ENOENT;
2208 goto out_nofid;
2209 }
2210
2211 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2212 err = -EEXIST;
2213 goto out_nofid;
2214 }
2215
2216 fidp = get_fid(pdu, fid);
2217 if (fidp == NULL) {
2218 err = -EINVAL;
2219 goto out_nofid;
2220 }
2221 if (fidp->fid_type != P9_FID_NONE) {
2222 err = -EINVAL;
2223 goto out;
2224 }
2225 if (perm & P9_STAT_MODE_DIR) {
2226 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2227 fidp->uid, -1, &stbuf);
2228 if (err < 0) {
2229 goto out;
2230 }
2231 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2232 if (err < 0) {
2233 goto out;
2234 }
2235 v9fs_path_write_lock(s);
2236 v9fs_path_copy(&fidp->path, &path);
2237 v9fs_path_unlock(s);
2238 err = v9fs_co_opendir(pdu, fidp);
2239 if (err < 0) {
2240 goto out;
2241 }
2242 fidp->fid_type = P9_FID_DIR;
2243 } else if (perm & P9_STAT_MODE_SYMLINK) {
2244 err = v9fs_co_symlink(pdu, fidp, &name,
2245 extension.data, -1 , &stbuf);
2246 if (err < 0) {
2247 goto out;
2248 }
2249 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2250 if (err < 0) {
2251 goto out;
2252 }
2253 v9fs_path_write_lock(s);
2254 v9fs_path_copy(&fidp->path, &path);
2255 v9fs_path_unlock(s);
2256 } else if (perm & P9_STAT_MODE_LINK) {
2257 int32_t ofid = atoi(extension.data);
2258 V9fsFidState *ofidp = get_fid(pdu, ofid);
2259 if (ofidp == NULL) {
2260 err = -EINVAL;
2261 goto out;
2262 }
2263 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2264 put_fid(pdu, ofidp);
2265 if (err < 0) {
2266 goto out;
2267 }
2268 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2269 if (err < 0) {
2270 fidp->fid_type = P9_FID_NONE;
2271 goto out;
2272 }
2273 v9fs_path_write_lock(s);
2274 v9fs_path_copy(&fidp->path, &path);
2275 v9fs_path_unlock(s);
2276 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2277 if (err < 0) {
2278 fidp->fid_type = P9_FID_NONE;
2279 goto out;
2280 }
2281 } else if (perm & P9_STAT_MODE_DEVICE) {
2282 char ctype;
2283 uint32_t major, minor;
2284 mode_t nmode = 0;
2285
2286 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2287 err = -errno;
2288 goto out;
2289 }
2290
2291 switch (ctype) {
2292 case 'c':
2293 nmode = S_IFCHR;
2294 break;
2295 case 'b':
2296 nmode = S_IFBLK;
2297 break;
2298 default:
2299 err = -EIO;
2300 goto out;
2301 }
2302
2303 nmode |= perm & 0777;
2304 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2305 makedev(major, minor), nmode, &stbuf);
2306 if (err < 0) {
2307 goto out;
2308 }
2309 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2310 if (err < 0) {
2311 goto out;
2312 }
2313 v9fs_path_write_lock(s);
2314 v9fs_path_copy(&fidp->path, &path);
2315 v9fs_path_unlock(s);
2316 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2317 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2318 0, S_IFIFO | (perm & 0777), &stbuf);
2319 if (err < 0) {
2320 goto out;
2321 }
2322 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2323 if (err < 0) {
2324 goto out;
2325 }
2326 v9fs_path_write_lock(s);
2327 v9fs_path_copy(&fidp->path, &path);
2328 v9fs_path_unlock(s);
2329 } else if (perm & P9_STAT_MODE_SOCKET) {
2330 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2331 0, S_IFSOCK | (perm & 0777), &stbuf);
2332 if (err < 0) {
2333 goto out;
2334 }
2335 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2336 if (err < 0) {
2337 goto out;
2338 }
2339 v9fs_path_write_lock(s);
2340 v9fs_path_copy(&fidp->path, &path);
2341 v9fs_path_unlock(s);
2342 } else {
2343 err = v9fs_co_open2(pdu, fidp, &name, -1,
2344 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2345 if (err < 0) {
2346 goto out;
2347 }
2348 fidp->fid_type = P9_FID_FILE;
2349 fidp->open_flags = omode_to_uflags(mode);
2350 if (fidp->open_flags & O_EXCL) {
2351 /*
2352 * We let the host file system do O_EXCL check
2353 * We should not reclaim such fd
2354 */
2355 fidp->flags |= FID_NON_RECLAIMABLE;
2356 }
2357 }
2358 iounit = get_iounit(pdu, &fidp->path);
2359 err = stat_to_qid(pdu, &stbuf, &qid);
2360 if (err < 0) {
2361 goto out;
2362 }
2363 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2364 if (err < 0) {
2365 goto out;
2366 }
2367 err += offset;
2368 trace_v9fs_create_return(pdu->tag, pdu->id,
2369 qid.type, qid.version, qid.path, iounit);
2370 out:
2371 put_fid(pdu, fidp);
2372 out_nofid:
2373 pdu_complete(pdu, err);
2374 v9fs_string_free(&name);
2375 v9fs_string_free(&extension);
2376 v9fs_path_free(&path);
2377 }
2378
2379 static void coroutine_fn v9fs_symlink(void *opaque)
2380 {
2381 V9fsPDU *pdu = opaque;
2382 V9fsString name;
2383 V9fsString symname;
2384 V9fsFidState *dfidp;
2385 V9fsQID qid;
2386 struct stat stbuf;
2387 int32_t dfid;
2388 int err = 0;
2389 gid_t gid;
2390 size_t offset = 7;
2391
2392 v9fs_string_init(&name);
2393 v9fs_string_init(&symname);
2394 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2395 if (err < 0) {
2396 goto out_nofid;
2397 }
2398 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2399
2400 if (name_is_illegal(name.data)) {
2401 err = -ENOENT;
2402 goto out_nofid;
2403 }
2404
2405 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2406 err = -EEXIST;
2407 goto out_nofid;
2408 }
2409
2410 dfidp = get_fid(pdu, dfid);
2411 if (dfidp == NULL) {
2412 err = -EINVAL;
2413 goto out_nofid;
2414 }
2415 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2416 if (err < 0) {
2417 goto out;
2418 }
2419 err = stat_to_qid(pdu, &stbuf, &qid);
2420 if (err < 0) {
2421 goto out;
2422 }
2423 err = pdu_marshal(pdu, offset, "Q", &qid);
2424 if (err < 0) {
2425 goto out;
2426 }
2427 err += offset;
2428 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2429 qid.type, qid.version, qid.path);
2430 out:
2431 put_fid(pdu, dfidp);
2432 out_nofid:
2433 pdu_complete(pdu, err);
2434 v9fs_string_free(&name);
2435 v9fs_string_free(&symname);
2436 }
2437
2438 static void coroutine_fn v9fs_flush(void *opaque)
2439 {
2440 ssize_t err;
2441 int16_t tag;
2442 size_t offset = 7;
2443 V9fsPDU *cancel_pdu = NULL;
2444 V9fsPDU *pdu = opaque;
2445 V9fsState *s = pdu->s;
2446
2447 err = pdu_unmarshal(pdu, offset, "w", &tag);
2448 if (err < 0) {
2449 pdu_complete(pdu, err);
2450 return;
2451 }
2452 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2453
2454 if (pdu->tag == tag) {
2455 warn_report("the guest sent a self-referencing 9P flush request");
2456 } else {
2457 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2458 if (cancel_pdu->tag == tag) {
2459 break;
2460 }
2461 }
2462 }
2463 if (cancel_pdu) {
2464 cancel_pdu->cancelled = 1;
2465 /*
2466 * Wait for pdu to complete.
2467 */
2468 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2469 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2470 cancel_pdu->cancelled = 0;
2471 pdu_free(cancel_pdu);
2472 }
2473 }
2474 pdu_complete(pdu, 7);
2475 }
2476
2477 static void coroutine_fn v9fs_link(void *opaque)
2478 {
2479 V9fsPDU *pdu = opaque;
2480 int32_t dfid, oldfid;
2481 V9fsFidState *dfidp, *oldfidp;
2482 V9fsString name;
2483 size_t offset = 7;
2484 int err = 0;
2485
2486 v9fs_string_init(&name);
2487 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2488 if (err < 0) {
2489 goto out_nofid;
2490 }
2491 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2492
2493 if (name_is_illegal(name.data)) {
2494 err = -ENOENT;
2495 goto out_nofid;
2496 }
2497
2498 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2499 err = -EEXIST;
2500 goto out_nofid;
2501 }
2502
2503 dfidp = get_fid(pdu, dfid);
2504 if (dfidp == NULL) {
2505 err = -ENOENT;
2506 goto out_nofid;
2507 }
2508
2509 oldfidp = get_fid(pdu, oldfid);
2510 if (oldfidp == NULL) {
2511 err = -ENOENT;
2512 goto out;
2513 }
2514 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2515 if (!err) {
2516 err = offset;
2517 }
2518 put_fid(pdu, oldfidp);
2519 out:
2520 put_fid(pdu, dfidp);
2521 out_nofid:
2522 v9fs_string_free(&name);
2523 pdu_complete(pdu, err);
2524 }
2525
2526 /* Only works with path name based fid */
2527 static void coroutine_fn v9fs_remove(void *opaque)
2528 {
2529 int32_t fid;
2530 int err = 0;
2531 size_t offset = 7;
2532 V9fsFidState *fidp;
2533 V9fsPDU *pdu = opaque;
2534
2535 err = pdu_unmarshal(pdu, offset, "d", &fid);
2536 if (err < 0) {
2537 goto out_nofid;
2538 }
2539 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2540
2541 fidp = get_fid(pdu, fid);
2542 if (fidp == NULL) {
2543 err = -EINVAL;
2544 goto out_nofid;
2545 }
2546 /* if fs driver is not path based, return EOPNOTSUPP */
2547 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2548 err = -EOPNOTSUPP;
2549 goto out_err;
2550 }
2551 /*
2552 * IF the file is unlinked, we cannot reopen
2553 * the file later. So don't reclaim fd
2554 */
2555 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2556 if (err < 0) {
2557 goto out_err;
2558 }
2559 err = v9fs_co_remove(pdu, &fidp->path);
2560 if (!err) {
2561 err = offset;
2562 }
2563 out_err:
2564 /* For TREMOVE we need to clunk the fid even on failed remove */
2565 clunk_fid(pdu->s, fidp->fid);
2566 put_fid(pdu, fidp);
2567 out_nofid:
2568 pdu_complete(pdu, err);
2569 }
2570
2571 static void coroutine_fn v9fs_unlinkat(void *opaque)
2572 {
2573 int err = 0;
2574 V9fsString name;
2575 int32_t dfid, flags, rflags = 0;
2576 size_t offset = 7;
2577 V9fsPath path;
2578 V9fsFidState *dfidp;
2579 V9fsPDU *pdu = opaque;
2580
2581 v9fs_string_init(&name);
2582 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2583 if (err < 0) {
2584 goto out_nofid;
2585 }
2586
2587 if (name_is_illegal(name.data)) {
2588 err = -ENOENT;
2589 goto out_nofid;
2590 }
2591
2592 if (!strcmp(".", name.data)) {
2593 err = -EINVAL;
2594 goto out_nofid;
2595 }
2596
2597 if (!strcmp("..", name.data)) {
2598 err = -ENOTEMPTY;
2599 goto out_nofid;
2600 }
2601
2602 if (flags & ~P9_DOTL_AT_REMOVEDIR) {
2603 err = -EINVAL;
2604 goto out_nofid;
2605 }
2606
2607 if (flags & P9_DOTL_AT_REMOVEDIR) {
2608 rflags |= AT_REMOVEDIR;
2609 }
2610
2611 dfidp = get_fid(pdu, dfid);
2612 if (dfidp == NULL) {
2613 err = -EINVAL;
2614 goto out_nofid;
2615 }
2616 /*
2617 * IF the file is unlinked, we cannot reopen
2618 * the file later. So don't reclaim fd
2619 */
2620 v9fs_path_init(&path);
2621 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2622 if (err < 0) {
2623 goto out_err;
2624 }
2625 err = v9fs_mark_fids_unreclaim(pdu, &path);
2626 if (err < 0) {
2627 goto out_err;
2628 }
2629 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags);
2630 if (!err) {
2631 err = offset;
2632 }
2633 out_err:
2634 put_fid(pdu, dfidp);
2635 v9fs_path_free(&path);
2636 out_nofid:
2637 pdu_complete(pdu, err);
2638 v9fs_string_free(&name);
2639 }
2640
2641
2642 /* Only works with path name based fid */
2643 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2644 int32_t newdirfid,
2645 V9fsString *name)
2646 {
2647 int err = 0;
2648 V9fsPath new_path;
2649 V9fsFidState *tfidp;
2650 V9fsState *s = pdu->s;
2651 V9fsFidState *dirfidp = NULL;
2652
2653 v9fs_path_init(&new_path);
2654 if (newdirfid != -1) {
2655 dirfidp = get_fid(pdu, newdirfid);
2656 if (dirfidp == NULL) {
2657 err = -ENOENT;
2658 goto out_nofid;
2659 }
2660 if (fidp->fid_type != P9_FID_NONE) {
2661 err = -EINVAL;
2662 goto out;
2663 }
2664 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2665 if (err < 0) {
2666 goto out;
2667 }
2668 } else {
2669 char *dir_name = g_path_get_dirname(fidp->path.data);
2670 V9fsPath dir_path;
2671
2672 v9fs_path_init(&dir_path);
2673 v9fs_path_sprintf(&dir_path, "%s", dir_name);
2674 g_free(dir_name);
2675
2676 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
2677 v9fs_path_free(&dir_path);
2678 if (err < 0) {
2679 goto out;
2680 }
2681 }
2682 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2683 if (err < 0) {
2684 goto out;
2685 }
2686 /*
2687 * Fixup fid's pointing to the old name to
2688 * start pointing to the new name
2689 */
2690 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2691 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2692 /* replace the name */
2693 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2694 }
2695 }
2696 out:
2697 if (dirfidp) {
2698 put_fid(pdu, dirfidp);
2699 }
2700 v9fs_path_free(&new_path);
2701 out_nofid:
2702 return err;
2703 }
2704
2705 /* Only works with path name based fid */
2706 static void coroutine_fn v9fs_rename(void *opaque)
2707 {
2708 int32_t fid;
2709 ssize_t err = 0;
2710 size_t offset = 7;
2711 V9fsString name;
2712 int32_t newdirfid;
2713 V9fsFidState *fidp;
2714 V9fsPDU *pdu = opaque;
2715 V9fsState *s = pdu->s;
2716
2717 v9fs_string_init(&name);
2718 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2719 if (err < 0) {
2720 goto out_nofid;
2721 }
2722
2723 if (name_is_illegal(name.data)) {
2724 err = -ENOENT;
2725 goto out_nofid;
2726 }
2727
2728 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2729 err = -EISDIR;
2730 goto out_nofid;
2731 }
2732
2733 fidp = get_fid(pdu, fid);
2734 if (fidp == NULL) {
2735 err = -ENOENT;
2736 goto out_nofid;
2737 }
2738 if (fidp->fid_type != P9_FID_NONE) {
2739 err = -EINVAL;
2740 goto out;
2741 }
2742 /* if fs driver is not path based, return EOPNOTSUPP */
2743 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2744 err = -EOPNOTSUPP;
2745 goto out;
2746 }
2747 v9fs_path_write_lock(s);
2748 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2749 v9fs_path_unlock(s);
2750 if (!err) {
2751 err = offset;
2752 }
2753 out:
2754 put_fid(pdu, fidp);
2755 out_nofid:
2756 pdu_complete(pdu, err);
2757 v9fs_string_free(&name);
2758 }
2759
2760 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2761 V9fsString *old_name,
2762 V9fsPath *newdir,
2763 V9fsString *new_name)
2764 {
2765 V9fsFidState *tfidp;
2766 V9fsPath oldpath, newpath;
2767 V9fsState *s = pdu->s;
2768 int err;
2769
2770 v9fs_path_init(&oldpath);
2771 v9fs_path_init(&newpath);
2772 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2773 if (err < 0) {
2774 goto out;
2775 }
2776 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2777 if (err < 0) {
2778 goto out;
2779 }
2780
2781 /*
2782 * Fixup fid's pointing to the old name to
2783 * start pointing to the new name
2784 */
2785 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2786 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2787 /* replace the name */
2788 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2789 }
2790 }
2791 out:
2792 v9fs_path_free(&oldpath);
2793 v9fs_path_free(&newpath);
2794 return err;
2795 }
2796
2797 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2798 V9fsString *old_name,
2799 int32_t newdirfid,
2800 V9fsString *new_name)
2801 {
2802 int err = 0;
2803 V9fsState *s = pdu->s;
2804 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2805
2806 olddirfidp = get_fid(pdu, olddirfid);
2807 if (olddirfidp == NULL) {
2808 err = -ENOENT;
2809 goto out;
2810 }
2811 if (newdirfid != -1) {
2812 newdirfidp = get_fid(pdu, newdirfid);
2813 if (newdirfidp == NULL) {
2814 err = -ENOENT;
2815 goto out;
2816 }
2817 } else {
2818 newdirfidp = get_fid(pdu, olddirfid);
2819 }
2820
2821 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2822 &newdirfidp->path, new_name);
2823 if (err < 0) {
2824 goto out;
2825 }
2826 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2827 /* Only for path based fid we need to do the below fixup */
2828 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2829 &newdirfidp->path, new_name);
2830 }
2831 out:
2832 if (olddirfidp) {
2833 put_fid(pdu, olddirfidp);
2834 }
2835 if (newdirfidp) {
2836 put_fid(pdu, newdirfidp);
2837 }
2838 return err;
2839 }
2840
2841 static void coroutine_fn v9fs_renameat(void *opaque)
2842 {
2843 ssize_t err = 0;
2844 size_t offset = 7;
2845 V9fsPDU *pdu = opaque;
2846 V9fsState *s = pdu->s;
2847 int32_t olddirfid, newdirfid;
2848 V9fsString old_name, new_name;
2849
2850 v9fs_string_init(&old_name);
2851 v9fs_string_init(&new_name);
2852 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2853 &old_name, &newdirfid, &new_name);
2854 if (err < 0) {
2855 goto out_err;
2856 }
2857
2858 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2859 err = -ENOENT;
2860 goto out_err;
2861 }
2862
2863 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2864 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2865 err = -EISDIR;
2866 goto out_err;
2867 }
2868
2869 v9fs_path_write_lock(s);
2870 err = v9fs_complete_renameat(pdu, olddirfid,
2871 &old_name, newdirfid, &new_name);
2872 v9fs_path_unlock(s);
2873 if (!err) {
2874 err = offset;
2875 }
2876
2877 out_err:
2878 pdu_complete(pdu, err);
2879 v9fs_string_free(&old_name);
2880 v9fs_string_free(&new_name);
2881 }
2882
2883 static void coroutine_fn v9fs_wstat(void *opaque)
2884 {
2885 int32_t fid;
2886 int err = 0;
2887 int16_t unused;
2888 V9fsStat v9stat;
2889 size_t offset = 7;
2890 struct stat stbuf;
2891 V9fsFidState *fidp;
2892 V9fsPDU *pdu = opaque;
2893 V9fsState *s = pdu->s;
2894
2895 v9fs_stat_init(&v9stat);
2896 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2897 if (err < 0) {
2898 goto out_nofid;
2899 }
2900 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2901 v9stat.mode, v9stat.atime, v9stat.mtime);
2902
2903 fidp = get_fid(pdu, fid);
2904 if (fidp == NULL) {
2905 err = -EINVAL;
2906 goto out_nofid;
2907 }
2908 /* do we need to sync the file? */
2909 if (donttouch_stat(&v9stat)) {
2910 err = v9fs_co_fsync(pdu, fidp, 0);
2911 goto out;
2912 }
2913 if (v9stat.mode != -1) {
2914 uint32_t v9_mode;
2915 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2916 if (err < 0) {
2917 goto out;
2918 }
2919 v9_mode = stat_to_v9mode(&stbuf);
2920 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2921 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2922 /* Attempting to change the type */
2923 err = -EIO;
2924 goto out;
2925 }
2926 err = v9fs_co_chmod(pdu, &fidp->path,
2927 v9mode_to_mode(v9stat.mode,
2928 &v9stat.extension));
2929 if (err < 0) {
2930 goto out;
2931 }
2932 }
2933 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2934 struct timespec times[2];
2935 if (v9stat.atime != -1) {
2936 times[0].tv_sec = v9stat.atime;
2937 times[0].tv_nsec = 0;
2938 } else {
2939 times[0].tv_nsec = UTIME_OMIT;
2940 }
2941 if (v9stat.mtime != -1) {
2942 times[1].tv_sec = v9stat.mtime;
2943 times[1].tv_nsec = 0;
2944 } else {
2945 times[1].tv_nsec = UTIME_OMIT;
2946 }
2947 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2948 if (err < 0) {
2949 goto out;
2950 }
2951 }
2952 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2953 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2954 if (err < 0) {
2955 goto out;
2956 }
2957 }
2958 if (v9stat.name.size != 0) {
2959 v9fs_path_write_lock(s);
2960 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2961 v9fs_path_unlock(s);
2962 if (err < 0) {
2963 goto out;
2964 }
2965 }
2966 if (v9stat.length != -1) {
2967 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2968 if (err < 0) {
2969 goto out;
2970 }
2971 }
2972 err = offset;
2973 out:
2974 put_fid(pdu, fidp);
2975 out_nofid:
2976 v9fs_stat_free(&v9stat);
2977 pdu_complete(pdu, err);
2978 }
2979
2980 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2981 {
2982 uint32_t f_type;
2983 uint32_t f_bsize;
2984 uint64_t f_blocks;
2985 uint64_t f_bfree;
2986 uint64_t f_bavail;
2987 uint64_t f_files;
2988 uint64_t f_ffree;
2989 uint64_t fsid_val;
2990 uint32_t f_namelen;
2991 size_t offset = 7;
2992 int32_t bsize_factor;
2993
2994 /*
2995 * compute bsize factor based on host file system block size
2996 * and client msize
2997 */
2998 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2999 if (!bsize_factor) {
3000 bsize_factor = 1;
3001 }
3002 f_type = stbuf->f_type;
3003 f_bsize = stbuf->f_bsize;
3004 f_bsize *= bsize_factor;
3005 /*
3006 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
3007 * adjust(divide) the number of blocks, free blocks and available
3008 * blocks by bsize factor
3009 */
3010 f_blocks = stbuf->f_blocks/bsize_factor;
3011 f_bfree = stbuf->f_bfree/bsize_factor;
3012 f_bavail = stbuf->f_bavail/bsize_factor;
3013 f_files = stbuf->f_files;
3014 f_ffree = stbuf->f_ffree;
3015 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
3016 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
3017 f_namelen = stbuf->f_namelen;
3018
3019 return pdu_marshal(pdu, offset, "ddqqqqqqd",
3020 f_type, f_bsize, f_blocks, f_bfree,
3021 f_bavail, f_files, f_ffree,
3022 fsid_val, f_namelen);
3023 }
3024
3025 static void coroutine_fn v9fs_statfs(void *opaque)
3026 {
3027 int32_t fid;
3028 ssize_t retval = 0;
3029 size_t offset = 7;
3030 V9fsFidState *fidp;
3031 struct statfs stbuf;
3032 V9fsPDU *pdu = opaque;
3033 V9fsState *s = pdu->s;
3034
3035 retval = pdu_unmarshal(pdu, offset, "d", &fid);
3036 if (retval < 0) {
3037 goto out_nofid;
3038 }
3039 fidp = get_fid(pdu, fid);
3040 if (fidp == NULL) {
3041 retval = -ENOENT;
3042 goto out_nofid;
3043 }
3044 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
3045 if (retval < 0) {
3046 goto out;
3047 }
3048 retval = v9fs_fill_statfs(s, pdu, &stbuf);
3049 if (retval < 0) {
3050 goto out;
3051 }
3052 retval += offset;
3053 out:
3054 put_fid(pdu, fidp);
3055 out_nofid:
3056 pdu_complete(pdu, retval);
3057 }
3058
3059 static void coroutine_fn v9fs_mknod(void *opaque)
3060 {
3061
3062 int mode;
3063 gid_t gid;
3064 int32_t fid;
3065 V9fsQID qid;
3066 int err = 0;
3067 int major, minor;
3068 size_t offset = 7;
3069 V9fsString name;
3070 struct stat stbuf;
3071 V9fsFidState *fidp;
3072 V9fsPDU *pdu = opaque;
3073
3074 v9fs_string_init(&name);
3075 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3076 &major, &minor, &gid);
3077 if (err < 0) {
3078 goto out_nofid;
3079 }
3080 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3081
3082 if (name_is_illegal(name.data)) {
3083 err = -ENOENT;
3084 goto out_nofid;
3085 }
3086
3087 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3088 err = -EEXIST;
3089 goto out_nofid;
3090 }
3091
3092 fidp = get_fid(pdu, fid);
3093 if (fidp == NULL) {
3094 err = -ENOENT;
3095 goto out_nofid;
3096 }
3097 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3098 makedev(major, minor), mode, &stbuf);
3099 if (err < 0) {
3100 goto out;
3101 }
3102 err = stat_to_qid(pdu, &stbuf, &qid);
3103 if (err < 0) {
3104 goto out;
3105 }
3106 err = pdu_marshal(pdu, offset, "Q", &qid);
3107 if (err < 0) {
3108 goto out;
3109 }
3110 err += offset;
3111 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3112 qid.type, qid.version, qid.path);
3113 out:
3114 put_fid(pdu, fidp);
3115 out_nofid:
3116 pdu_complete(pdu, err);
3117 v9fs_string_free(&name);
3118 }
3119
3120 /*
3121 * Implement posix byte range locking code
3122 * Server side handling of locking code is very simple, because 9p server in
3123 * QEMU can handle only one client. And most of the lock handling
3124 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3125 * do any thing in * qemu 9p server side lock code path.
3126 * So when a TLOCK request comes, always return success
3127 */
3128 static void coroutine_fn v9fs_lock(void *opaque)
3129 {
3130 V9fsFlock flock;
3131 size_t offset = 7;
3132 struct stat stbuf;
3133 V9fsFidState *fidp;
3134 int32_t fid, err = 0;
3135 V9fsPDU *pdu = opaque;
3136
3137 v9fs_string_init(&flock.client_id);
3138 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3139 &flock.flags, &flock.start, &flock.length,
3140 &flock.proc_id, &flock.client_id);
3141 if (err < 0) {
3142 goto out_nofid;
3143 }
3144 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3145 flock.type, flock.start, flock.length);
3146
3147
3148 /* We support only block flag now (that too ignored currently) */
3149 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3150 err = -EINVAL;
3151 goto out_nofid;
3152 }
3153 fidp = get_fid(pdu, fid);
3154 if (fidp == NULL) {
3155 err = -ENOENT;
3156 goto out_nofid;
3157 }
3158 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3159 if (err < 0) {
3160 goto out;
3161 }
3162 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3163 if (err < 0) {
3164 goto out;
3165 }
3166 err += offset;
3167 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3168 out:
3169 put_fid(pdu, fidp);
3170 out_nofid:
3171 pdu_complete(pdu, err);
3172 v9fs_string_free(&flock.client_id);
3173 }
3174
3175 /*
3176 * When a TGETLOCK request comes, always return success because all lock
3177 * handling is done by client's VFS layer.
3178 */
3179 static void coroutine_fn v9fs_getlock(void *opaque)
3180 {
3181 size_t offset = 7;
3182 struct stat stbuf;
3183 V9fsFidState *fidp;
3184 V9fsGetlock glock;
3185 int32_t fid, err = 0;
3186 V9fsPDU *pdu = opaque;
3187
3188 v9fs_string_init(&glock.client_id);
3189 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3190 &glock.start, &glock.length, &glock.proc_id,
3191 &glock.client_id);
3192 if (err < 0) {
3193 goto out_nofid;
3194 }
3195 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3196 glock.type, glock.start, glock.length);
3197
3198 fidp = get_fid(pdu, fid);
3199 if (fidp == NULL) {
3200 err = -ENOENT;
3201 goto out_nofid;
3202 }
3203 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3204 if (err < 0) {
3205 goto out;
3206 }
3207 glock.type = P9_LOCK_TYPE_UNLCK;
3208 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3209 glock.start, glock.length, glock.proc_id,
3210 &glock.client_id);
3211 if (err < 0) {
3212 goto out;
3213 }
3214 err += offset;
3215 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3216 glock.length, glock.proc_id);
3217 out:
3218 put_fid(pdu, fidp);
3219 out_nofid:
3220 pdu_complete(pdu, err);
3221 v9fs_string_free(&glock.client_id);
3222 }
3223
3224 static void coroutine_fn v9fs_mkdir(void *opaque)
3225 {
3226 V9fsPDU *pdu = opaque;
3227 size_t offset = 7;
3228 int32_t fid;
3229 struct stat stbuf;
3230 V9fsQID qid;
3231 V9fsString name;
3232 V9fsFidState *fidp;
3233 gid_t gid;
3234 int mode;
3235 int err = 0;
3236
3237 v9fs_string_init(&name);
3238 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3239 if (err < 0) {
3240 goto out_nofid;
3241 }
3242 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3243
3244 if (name_is_illegal(name.data)) {
3245 err = -ENOENT;
3246 goto out_nofid;
3247 }
3248
3249 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3250 err = -EEXIST;
3251 goto out_nofid;
3252 }
3253
3254 fidp = get_fid(pdu, fid);
3255 if (fidp == NULL) {
3256 err = -ENOENT;
3257 goto out_nofid;
3258 }
3259 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3260 if (err < 0) {
3261 goto out;
3262 }
3263 err = stat_to_qid(pdu, &stbuf, &qid);
3264 if (err < 0) {
3265 goto out;
3266 }
3267 err = pdu_marshal(pdu, offset, "Q", &qid);
3268 if (err < 0) {
3269 goto out;
3270 }
3271 err += offset;
3272 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3273 qid.type, qid.version, qid.path, err);
3274 out:
3275 put_fid(pdu, fidp);
3276 out_nofid:
3277 pdu_complete(pdu, err);
3278 v9fs_string_free(&name);
3279 }
3280
3281 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3282 {
3283 int64_t size;
3284 V9fsString name;
3285 ssize_t err = 0;
3286 size_t offset = 7;
3287 int32_t fid, newfid;
3288 V9fsFidState *file_fidp;
3289 V9fsFidState *xattr_fidp = NULL;
3290 V9fsPDU *pdu = opaque;
3291 V9fsState *s = pdu->s;
3292
3293 v9fs_string_init(&name);
3294 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3295 if (err < 0) {
3296 goto out_nofid;
3297 }
3298 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3299
3300 file_fidp = get_fid(pdu, fid);
3301 if (file_fidp == NULL) {
3302 err = -ENOENT;
3303 goto out_nofid;
3304 }
3305 xattr_fidp = alloc_fid(s, newfid);
3306 if (xattr_fidp == NULL) {
3307 err = -EINVAL;
3308 goto out;
3309 }
3310 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3311 if (!v9fs_string_size(&name)) {
3312 /*
3313 * listxattr request. Get the size first
3314 */
3315 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3316 if (size < 0) {
3317 err = size;
3318 clunk_fid(s, xattr_fidp->fid);
3319 goto out;
3320 }
3321 /*
3322 * Read the xattr value
3323 */
3324 xattr_fidp->fs.xattr.len = size;
3325 xattr_fidp->fid_type = P9_FID_XATTR;
3326 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3327 xattr_fidp->fs.xattr.value = g_malloc0(size);
3328 if (size) {
3329 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3330 xattr_fidp->fs.xattr.value,
3331 xattr_fidp->fs.xattr.len);
3332 if (err < 0) {
3333 clunk_fid(s, xattr_fidp->fid);
3334 goto out;
3335 }
3336 }
3337 err = pdu_marshal(pdu, offset, "q", size);
3338 if (err < 0) {
3339 goto out;
3340 }
3341 err += offset;
3342 } else {
3343 /*
3344 * specific xattr fid. We check for xattr
3345 * presence also collect the xattr size
3346 */
3347 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3348 &name, NULL, 0);
3349 if (size < 0) {
3350 err = size;
3351 clunk_fid(s, xattr_fidp->fid);
3352 goto out;
3353 }
3354 /*
3355 * Read the xattr value
3356 */
3357 xattr_fidp->fs.xattr.len = size;
3358 xattr_fidp->fid_type = P9_FID_XATTR;
3359 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3360 xattr_fidp->fs.xattr.value = g_malloc0(size);
3361 if (size) {
3362 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3363 &name, xattr_fidp->fs.xattr.value,
3364 xattr_fidp->fs.xattr.len);
3365 if (err < 0) {
3366 clunk_fid(s, xattr_fidp->fid);
3367 goto out;
3368 }
3369 }
3370 err = pdu_marshal(pdu, offset, "q", size);
3371 if (err < 0) {
3372 goto out;
3373 }
3374 err += offset;
3375 }
3376 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3377 out:
3378 put_fid(pdu, file_fidp);
3379 if (xattr_fidp) {
3380 put_fid(pdu, xattr_fidp);
3381 }
3382 out_nofid:
3383 pdu_complete(pdu, err);
3384 v9fs_string_free(&name);
3385 }
3386
3387 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3388 {
3389 int flags, rflags = 0;
3390 int32_t fid;
3391 uint64_t size;
3392 ssize_t err = 0;
3393 V9fsString name;
3394 size_t offset = 7;
3395 V9fsFidState *file_fidp;
3396 V9fsFidState *xattr_fidp;
3397 V9fsPDU *pdu = opaque;
3398
3399 v9fs_string_init(&name);
3400 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3401 if (err < 0) {
3402 goto out_nofid;
3403 }
3404 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3405
3406 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) {
3407 err = -EINVAL;
3408 goto out_nofid;
3409 }
3410
3411 if (flags & P9_XATTR_CREATE) {
3412 rflags |= XATTR_CREATE;
3413 }
3414
3415 if (flags & P9_XATTR_REPLACE) {
3416 rflags |= XATTR_REPLACE;
3417 }
3418
3419 if (size > XATTR_SIZE_MAX) {
3420 err = -E2BIG;
3421 goto out_nofid;
3422 }
3423
3424 file_fidp = get_fid(pdu, fid);
3425 if (file_fidp == NULL) {
3426 err = -EINVAL;
3427 goto out_nofid;
3428 }
3429 if (file_fidp->fid_type != P9_FID_NONE) {
3430 err = -EINVAL;
3431 goto out_put_fid;
3432 }
3433
3434 /* Make the file fid point to xattr */
3435 xattr_fidp = file_fidp;
3436 xattr_fidp->fid_type = P9_FID_XATTR;
3437 xattr_fidp->fs.xattr.copied_len = 0;
3438 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3439 xattr_fidp->fs.xattr.len = size;
3440 xattr_fidp->fs.xattr.flags = rflags;
3441 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3442 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3443 xattr_fidp->fs.xattr.value = g_malloc0(size);
3444 err = offset;
3445 out_put_fid:
3446 put_fid(pdu, file_fidp);
3447 out_nofid:
3448 pdu_complete(pdu, err);
3449 v9fs_string_free(&name);
3450 }
3451
3452 static void coroutine_fn v9fs_readlink(void *opaque)
3453 {
3454 V9fsPDU *pdu = opaque;
3455 size_t offset = 7;
3456 V9fsString target;
3457 int32_t fid;
3458 int err = 0;
3459 V9fsFidState *fidp;
3460
3461 err = pdu_unmarshal(pdu, offset, "d", &fid);
3462 if (err < 0) {
3463 goto out_nofid;
3464 }
3465 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3466 fidp = get_fid(pdu, fid);
3467 if (fidp == NULL) {
3468 err = -ENOENT;
3469 goto out_nofid;
3470 }
3471
3472 v9fs_string_init(&target);
3473 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3474 if (err < 0) {
3475 goto out;
3476 }
3477 err = pdu_marshal(pdu, offset, "s", &target);
3478 if (err < 0) {
3479 v9fs_string_free(&target);
3480 goto out;
3481 }
3482 err += offset;
3483 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3484 v9fs_string_free(&target);
3485 out:
3486 put_fid(pdu, fidp);
3487 out_nofid:
3488 pdu_complete(pdu, err);
3489 }
3490
3491 static CoroutineEntry *pdu_co_handlers[] = {
3492 [P9_TREADDIR] = v9fs_readdir,
3493 [P9_TSTATFS] = v9fs_statfs,
3494 [P9_TGETATTR] = v9fs_getattr,
3495 [P9_TSETATTR] = v9fs_setattr,
3496 [P9_TXATTRWALK] = v9fs_xattrwalk,
3497 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3498 [P9_TMKNOD] = v9fs_mknod,
3499 [P9_TRENAME] = v9fs_rename,
3500 [P9_TLOCK] = v9fs_lock,
3501 [P9_TGETLOCK] = v9fs_getlock,
3502 [P9_TRENAMEAT] = v9fs_renameat,
3503 [P9_TREADLINK] = v9fs_readlink,
3504 [P9_TUNLINKAT] = v9fs_unlinkat,
3505 [P9_TMKDIR] = v9fs_mkdir,
3506 [P9_TVERSION] = v9fs_version,
3507 [P9_TLOPEN] = v9fs_open,
3508 [P9_TATTACH] = v9fs_attach,
3509 [P9_TSTAT] = v9fs_stat,
3510 [P9_TWALK] = v9fs_walk,
3511 [P9_TCLUNK] = v9fs_clunk,
3512 [P9_TFSYNC] = v9fs_fsync,
3513 [P9_TOPEN] = v9fs_open,
3514 [P9_TREAD] = v9fs_read,
3515 #if 0
3516 [P9_TAUTH] = v9fs_auth,
3517 #endif
3518 [P9_TFLUSH] = v9fs_flush,
3519 [P9_TLINK] = v9fs_link,
3520 [P9_TSYMLINK] = v9fs_symlink,
3521 [P9_TCREATE] = v9fs_create,
3522 [P9_TLCREATE] = v9fs_lcreate,
3523 [P9_TWRITE] = v9fs_write,
3524 [P9_TWSTAT] = v9fs_wstat,
3525 [P9_TREMOVE] = v9fs_remove,
3526 };
3527
3528 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3529 {
3530 V9fsPDU *pdu = opaque;
3531 pdu_complete(pdu, -EOPNOTSUPP);
3532 }
3533
3534 static void coroutine_fn v9fs_fs_ro(void *opaque)
3535 {
3536 V9fsPDU *pdu = opaque;
3537 pdu_complete(pdu, -EROFS);
3538 }
3539
3540 static inline bool is_read_only_op(V9fsPDU *pdu)
3541 {
3542 switch (pdu->id) {
3543 case P9_TREADDIR:
3544 case P9_TSTATFS:
3545 case P9_TGETATTR:
3546 case P9_TXATTRWALK:
3547 case P9_TLOCK:
3548 case P9_TGETLOCK:
3549 case P9_TREADLINK:
3550 case P9_TVERSION:
3551 case P9_TLOPEN:
3552 case P9_TATTACH:
3553 case P9_TSTAT:
3554 case P9_TWALK:
3555 case P9_TCLUNK:
3556 case P9_TFSYNC:
3557 case P9_TOPEN:
3558 case P9_TREAD:
3559 case P9_TAUTH:
3560 case P9_TFLUSH:
3561 return 1;
3562 default:
3563 return 0;
3564 }
3565 }
3566
3567 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
3568 {
3569 Coroutine *co;
3570 CoroutineEntry *handler;
3571 V9fsState *s = pdu->s;
3572
3573 pdu->size = le32_to_cpu(hdr->size_le);
3574 pdu->id = hdr->id;
3575 pdu->tag = le16_to_cpu(hdr->tag_le);
3576
3577 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3578 (pdu_co_handlers[pdu->id] == NULL)) {
3579 handler = v9fs_op_not_supp;
3580 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3581 handler = v9fs_fs_ro;
3582 } else {
3583 handler = pdu_co_handlers[pdu->id];
3584 }
3585
3586 qemu_co_queue_init(&pdu->complete);
3587 co = qemu_coroutine_create(handler, pdu);
3588 qemu_coroutine_enter(co);
3589 }
3590
3591 /* Returns 0 on success, 1 on failure. */
3592 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
3593 Error **errp)
3594 {
3595 int i, len;
3596 struct stat stat;
3597 FsDriverEntry *fse;
3598 V9fsPath path;
3599 int rc = 1;
3600
3601 assert(!s->transport);
3602 s->transport = t;
3603
3604 /* initialize pdu allocator */
3605 QLIST_INIT(&s->free_list);
3606 QLIST_INIT(&s->active_list);
3607 for (i = 0; i < MAX_REQ; i++) {
3608 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3609 s->pdus[i].s = s;
3610 s->pdus[i].idx = i;
3611 }
3612
3613 v9fs_path_init(&path);
3614
3615 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3616
3617 if (!fse) {
3618 /* We don't have a fsdev identified by fsdev_id */
3619 error_setg(errp, "9pfs device couldn't find fsdev with the "
3620 "id = %s",
3621 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3622 goto out;
3623 }
3624
3625 if (!s->fsconf.tag) {
3626 /* we haven't specified a mount_tag */
3627 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3628 s->fsconf.fsdev_id);
3629 goto out;
3630 }
3631
3632 s->ctx.export_flags = fse->export_flags;
3633 s->ctx.fs_root = g_strdup(fse->path);
3634 s->ctx.exops.get_st_gen = NULL;
3635 len = strlen(s->fsconf.tag);
3636 if (len > MAX_TAG_LEN - 1) {
3637 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3638 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3639 goto out;
3640 }
3641
3642 s->tag = g_strdup(s->fsconf.tag);
3643 s->ctx.uid = -1;
3644
3645 s->ops = fse->ops;
3646
3647 s->ctx.fmode = fse->fmode;
3648 s->ctx.dmode = fse->dmode;
3649
3650 s->fid_list = NULL;
3651 qemu_co_rwlock_init(&s->rename_lock);
3652
3653 if (s->ops->init(&s->ctx, errp) < 0) {
3654 error_prepend(errp, "cannot initialize fsdev '%s': ",
3655 s->fsconf.fsdev_id);
3656 goto out;
3657 }
3658
3659 /*
3660 * Check details of export path, We need to use fs driver
3661 * call back to do that. Since we are in the init path, we don't
3662 * use co-routines here.
3663 */
3664 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3665 error_setg(errp,
3666 "error in converting name to path %s", strerror(errno));
3667 goto out;
3668 }
3669 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3670 error_setg(errp, "share path %s does not exist", fse->path);
3671 goto out;
3672 } else if (!S_ISDIR(stat.st_mode)) {
3673 error_setg(errp, "share path %s is not a directory", fse->path);
3674 goto out;
3675 }
3676
3677 s->dev_id = stat.st_dev;
3678
3679 s->ctx.fst = &fse->fst;
3680 fsdev_throttle_init(s->ctx.fst);
3681
3682 rc = 0;
3683 out:
3684 if (rc) {
3685 v9fs_device_unrealize_common(s, NULL);
3686 }
3687 v9fs_path_free(&path);
3688 return rc;
3689 }
3690
3691 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3692 {
3693 if (s->ops && s->ops->cleanup) {
3694 s->ops->cleanup(&s->ctx);
3695 }
3696 if (s->ctx.fst) {
3697 fsdev_throttle_cleanup(s->ctx.fst);
3698 }
3699 g_free(s->tag);
3700 g_free(s->ctx.fs_root);
3701 }
3702
3703 typedef struct VirtfsCoResetData {
3704 V9fsPDU pdu;
3705 bool done;
3706 } VirtfsCoResetData;
3707
3708 static void coroutine_fn virtfs_co_reset(void *opaque)
3709 {
3710 VirtfsCoResetData *data = opaque;
3711
3712 virtfs_reset(&data->pdu);
3713 data->done = true;
3714 }
3715
3716 void v9fs_reset(V9fsState *s)
3717 {
3718 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3719 Coroutine *co;
3720
3721 while (!QLIST_EMPTY(&s->active_list)) {
3722 aio_poll(qemu_get_aio_context(), true);
3723 }
3724
3725 co = qemu_coroutine_create(virtfs_co_reset, &data);
3726 qemu_coroutine_enter(co);
3727
3728 while (!data.done) {
3729 aio_poll(qemu_get_aio_context(), true);
3730 }
3731 }
3732
3733 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3734 {
3735 struct rlimit rlim;
3736 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3737 error_report("Failed to get the resource limit");
3738 exit(1);
3739 }
3740 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3741 open_fd_rc = rlim.rlim_cur/2;
3742 }