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