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