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