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