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