include/standard-headers: add pvrdma related headers
[qemu.git] / hw / 9pfs / 9p.c
blob85a1ed8171a4455b78ba55196da9c21ae31b7540
1 /*
2 * Virtio 9p backend
4 * Copyright IBM, Corp. 2010
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include <glib/gprintf.h>
16 #include "hw/virtio/virtio.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
19 #include "qemu/iov.h"
20 #include "qemu/sockets.h"
21 #include "virtio-9p.h"
22 #include "fsdev/qemu-fsdev.h"
23 #include "9p-xattr.h"
24 #include "coth.h"
25 #include "trace.h"
26 #include "migration/blocker.h"
27 #include "sysemu/qtest.h"
29 int open_fd_hw;
30 int total_open_fd;
31 static int open_fd_rc;
33 enum {
34 Oread = 0x00,
35 Owrite = 0x01,
36 Ordwr = 0x02,
37 Oexec = 0x03,
38 Oexcl = 0x04,
39 Otrunc = 0x10,
40 Orexec = 0x20,
41 Orclose = 0x40,
42 Oappend = 0x80,
45 static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
47 ssize_t ret;
48 va_list ap;
50 va_start(ap, fmt);
51 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap);
52 va_end(ap);
54 return ret;
57 static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
59 ssize_t ret;
60 va_list ap;
62 va_start(ap, fmt);
63 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap);
64 va_end(ap);
66 return ret;
69 static int omode_to_uflags(int8_t mode)
71 int ret = 0;
73 switch (mode & 3) {
74 case Oread:
75 ret = O_RDONLY;
76 break;
77 case Ordwr:
78 ret = O_RDWR;
79 break;
80 case Owrite:
81 ret = O_WRONLY;
82 break;
83 case Oexec:
84 ret = O_RDONLY;
85 break;
88 if (mode & Otrunc) {
89 ret |= O_TRUNC;
92 if (mode & Oappend) {
93 ret |= O_APPEND;
96 if (mode & Oexcl) {
97 ret |= O_EXCL;
100 return ret;
103 typedef struct DotlOpenflagMap {
104 int dotl_flag;
105 int open_flag;
106 } DotlOpenflagMap;
108 static int dotl_to_open_flags(int flags)
110 int i;
112 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
113 * and P9_DOTL_NOACCESS
115 int oflags = flags & O_ACCMODE;
117 DotlOpenflagMap dotl_oflag_map[] = {
118 { P9_DOTL_CREATE, O_CREAT },
119 { P9_DOTL_EXCL, O_EXCL },
120 { P9_DOTL_NOCTTY , O_NOCTTY },
121 { P9_DOTL_TRUNC, O_TRUNC },
122 { P9_DOTL_APPEND, O_APPEND },
123 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
124 { P9_DOTL_DSYNC, O_DSYNC },
125 { P9_DOTL_FASYNC, FASYNC },
126 { P9_DOTL_DIRECT, O_DIRECT },
127 { P9_DOTL_LARGEFILE, O_LARGEFILE },
128 { P9_DOTL_DIRECTORY, O_DIRECTORY },
129 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
130 { P9_DOTL_NOATIME, O_NOATIME },
131 { P9_DOTL_SYNC, O_SYNC },
134 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
135 if (flags & dotl_oflag_map[i].dotl_flag) {
136 oflags |= dotl_oflag_map[i].open_flag;
140 return oflags;
143 void cred_init(FsCred *credp)
145 credp->fc_uid = -1;
146 credp->fc_gid = -1;
147 credp->fc_mode = -1;
148 credp->fc_rdev = -1;
151 static int get_dotl_openflags(V9fsState *s, int oflags)
153 int flags;
155 * Filter the client open flags
157 flags = dotl_to_open_flags(oflags);
158 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
160 * Ignore direct disk access hint until the server supports it.
162 flags &= ~O_DIRECT;
163 return flags;
166 void v9fs_path_init(V9fsPath *path)
168 path->data = NULL;
169 path->size = 0;
172 void v9fs_path_free(V9fsPath *path)
174 g_free(path->data);
175 path->data = NULL;
176 path->size = 0;
180 void GCC_FMT_ATTR(2, 3)
181 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...)
183 va_list ap;
185 v9fs_path_free(path);
187 va_start(ap, fmt);
188 /* Bump the size for including terminating NULL */
189 path->size = g_vasprintf(&path->data, fmt, ap) + 1;
190 va_end(ap);
193 void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
195 v9fs_path_free(lhs);
196 lhs->data = g_malloc(rhs->size);
197 memcpy(lhs->data, rhs->data, rhs->size);
198 lhs->size = rhs->size;
201 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
202 const char *name, V9fsPath *path)
204 int err;
205 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
206 if (err < 0) {
207 err = -errno;
209 return err;
213 * Return TRUE if s1 is an ancestor of s2.
215 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
216 * As a special case, We treat s1 as ancestor of s2 if they are same!
218 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
220 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
221 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
222 return 1;
225 return 0;
228 static size_t v9fs_string_size(V9fsString *str)
230 return str->size;
234 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
235 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
237 int err = 1;
238 if (f->fid_type == P9_FID_FILE) {
239 if (f->fs.fd == -1) {
240 do {
241 err = v9fs_co_open(pdu, f, f->open_flags);
242 } while (err == -EINTR && !pdu->cancelled);
244 } else if (f->fid_type == P9_FID_DIR) {
245 if (f->fs.dir.stream == NULL) {
246 do {
247 err = v9fs_co_opendir(pdu, f);
248 } while (err == -EINTR && !pdu->cancelled);
251 return err;
254 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
256 int err;
257 V9fsFidState *f;
258 V9fsState *s = pdu->s;
260 for (f = s->fid_list; f; f = f->next) {
261 BUG_ON(f->clunked);
262 if (f->fid == fid) {
264 * Update the fid ref upfront so that
265 * we don't get reclaimed when we yield
266 * in open later.
268 f->ref++;
270 * check whether we need to reopen the
271 * file. We might have closed the fd
272 * while trying to free up some file
273 * descriptors.
275 err = v9fs_reopen_fid(pdu, f);
276 if (err < 0) {
277 f->ref--;
278 return NULL;
281 * Mark the fid as referenced so that the LRU
282 * reclaim won't close the file descriptor
284 f->flags |= FID_REFERENCED;
285 return f;
288 return NULL;
291 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
293 V9fsFidState *f;
295 for (f = s->fid_list; f; f = f->next) {
296 /* If fid is already there return NULL */
297 BUG_ON(f->clunked);
298 if (f->fid == fid) {
299 return NULL;
302 f = g_malloc0(sizeof(V9fsFidState));
303 f->fid = fid;
304 f->fid_type = P9_FID_NONE;
305 f->ref = 1;
307 * Mark the fid as referenced so that the LRU
308 * reclaim won't close the file descriptor
310 f->flags |= FID_REFERENCED;
311 f->next = s->fid_list;
312 s->fid_list = f;
314 v9fs_readdir_init(&f->fs.dir);
315 v9fs_readdir_init(&f->fs_reclaim.dir);
317 return f;
320 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
322 int retval = 0;
324 if (fidp->fs.xattr.xattrwalk_fid) {
325 /* getxattr/listxattr fid */
326 goto free_value;
329 * if this is fid for setxattr. clunk should
330 * result in setxattr localcall
332 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
333 /* clunk after partial write */
334 retval = -EINVAL;
335 goto free_out;
337 if (fidp->fs.xattr.len) {
338 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
339 fidp->fs.xattr.value,
340 fidp->fs.xattr.len,
341 fidp->fs.xattr.flags);
342 } else {
343 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
345 free_out:
346 v9fs_string_free(&fidp->fs.xattr.name);
347 free_value:
348 g_free(fidp->fs.xattr.value);
349 return retval;
352 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
354 int retval = 0;
356 if (fidp->fid_type == P9_FID_FILE) {
357 /* If we reclaimed the fd no need to close */
358 if (fidp->fs.fd != -1) {
359 retval = v9fs_co_close(pdu, &fidp->fs);
361 } else if (fidp->fid_type == P9_FID_DIR) {
362 if (fidp->fs.dir.stream != NULL) {
363 retval = v9fs_co_closedir(pdu, &fidp->fs);
365 } else if (fidp->fid_type == P9_FID_XATTR) {
366 retval = v9fs_xattr_fid_clunk(pdu, fidp);
368 v9fs_path_free(&fidp->path);
369 g_free(fidp);
370 return retval;
373 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
375 BUG_ON(!fidp->ref);
376 fidp->ref--;
378 * Don't free the fid if it is in reclaim list
380 if (!fidp->ref && fidp->clunked) {
381 if (fidp->fid == pdu->s->root_fid) {
383 * if the clunked fid is root fid then we
384 * have unmounted the fs on the client side.
385 * delete the migration blocker. Ideally, this
386 * should be hooked to transport close notification
388 if (pdu->s->migration_blocker) {
389 migrate_del_blocker(pdu->s->migration_blocker);
390 error_free(pdu->s->migration_blocker);
391 pdu->s->migration_blocker = NULL;
394 return free_fid(pdu, fidp);
396 return 0;
399 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
401 V9fsFidState **fidpp, *fidp;
403 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
404 if ((*fidpp)->fid == fid) {
405 break;
408 if (*fidpp == NULL) {
409 return NULL;
411 fidp = *fidpp;
412 *fidpp = fidp->next;
413 fidp->clunked = 1;
414 return fidp;
417 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
419 int reclaim_count = 0;
420 V9fsState *s = pdu->s;
421 V9fsFidState *f, *reclaim_list = NULL;
423 for (f = s->fid_list; f; f = f->next) {
425 * Unlink fids cannot be reclaimed. Check
426 * for them and skip them. Also skip fids
427 * currently being operated on.
429 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
430 continue;
433 * if it is a recently referenced fid
434 * we leave the fid untouched and clear the
435 * reference bit. We come back to it later
436 * in the next iteration. (a simple LRU without
437 * moving list elements around)
439 if (f->flags & FID_REFERENCED) {
440 f->flags &= ~FID_REFERENCED;
441 continue;
444 * Add fids to reclaim list.
446 if (f->fid_type == P9_FID_FILE) {
447 if (f->fs.fd != -1) {
449 * Up the reference count so that
450 * a clunk request won't free this fid
452 f->ref++;
453 f->rclm_lst = reclaim_list;
454 reclaim_list = f;
455 f->fs_reclaim.fd = f->fs.fd;
456 f->fs.fd = -1;
457 reclaim_count++;
459 } else if (f->fid_type == P9_FID_DIR) {
460 if (f->fs.dir.stream != NULL) {
462 * Up the reference count so that
463 * a clunk request won't free this fid
465 f->ref++;
466 f->rclm_lst = reclaim_list;
467 reclaim_list = f;
468 f->fs_reclaim.dir.stream = f->fs.dir.stream;
469 f->fs.dir.stream = NULL;
470 reclaim_count++;
473 if (reclaim_count >= open_fd_rc) {
474 break;
478 * Now close the fid in reclaim list. Free them if they
479 * are already clunked.
481 while (reclaim_list) {
482 f = reclaim_list;
483 reclaim_list = f->rclm_lst;
484 if (f->fid_type == P9_FID_FILE) {
485 v9fs_co_close(pdu, &f->fs_reclaim);
486 } else if (f->fid_type == P9_FID_DIR) {
487 v9fs_co_closedir(pdu, &f->fs_reclaim);
489 f->rclm_lst = NULL;
491 * Now drop the fid reference, free it
492 * if clunked.
494 put_fid(pdu, f);
498 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
500 int err;
501 V9fsState *s = pdu->s;
502 V9fsFidState *fidp, head_fid;
504 head_fid.next = s->fid_list;
505 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
506 if (fidp->path.size != path->size) {
507 continue;
509 if (!memcmp(fidp->path.data, path->data, path->size)) {
510 /* Mark the fid non reclaimable. */
511 fidp->flags |= FID_NON_RECLAIMABLE;
513 /* reopen the file/dir if already closed */
514 err = v9fs_reopen_fid(pdu, fidp);
515 if (err < 0) {
516 return err;
519 * Go back to head of fid list because
520 * the list could have got updated when
521 * switched to the worker thread
523 if (err == 0) {
524 fidp = &head_fid;
528 return 0;
531 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
533 V9fsState *s = pdu->s;
534 V9fsFidState *fidp;
536 /* Free all fids */
537 while (s->fid_list) {
538 /* Get fid */
539 fidp = s->fid_list;
540 fidp->ref++;
542 /* Clunk fid */
543 s->fid_list = fidp->next;
544 fidp->clunked = 1;
546 put_fid(pdu, fidp);
550 #define P9_QID_TYPE_DIR 0x80
551 #define P9_QID_TYPE_SYMLINK 0x02
553 #define P9_STAT_MODE_DIR 0x80000000
554 #define P9_STAT_MODE_APPEND 0x40000000
555 #define P9_STAT_MODE_EXCL 0x20000000
556 #define P9_STAT_MODE_MOUNT 0x10000000
557 #define P9_STAT_MODE_AUTH 0x08000000
558 #define P9_STAT_MODE_TMP 0x04000000
559 #define P9_STAT_MODE_SYMLINK 0x02000000
560 #define P9_STAT_MODE_LINK 0x01000000
561 #define P9_STAT_MODE_DEVICE 0x00800000
562 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
563 #define P9_STAT_MODE_SOCKET 0x00100000
564 #define P9_STAT_MODE_SETUID 0x00080000
565 #define P9_STAT_MODE_SETGID 0x00040000
566 #define P9_STAT_MODE_SETVTX 0x00010000
568 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
569 P9_STAT_MODE_SYMLINK | \
570 P9_STAT_MODE_LINK | \
571 P9_STAT_MODE_DEVICE | \
572 P9_STAT_MODE_NAMED_PIPE | \
573 P9_STAT_MODE_SOCKET)
575 /* This is the algorithm from ufs in spfs */
576 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
578 size_t size;
580 memset(&qidp->path, 0, sizeof(qidp->path));
581 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
582 memcpy(&qidp->path, &stbuf->st_ino, size);
583 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
584 qidp->type = 0;
585 if (S_ISDIR(stbuf->st_mode)) {
586 qidp->type |= P9_QID_TYPE_DIR;
588 if (S_ISLNK(stbuf->st_mode)) {
589 qidp->type |= P9_QID_TYPE_SYMLINK;
593 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
594 V9fsQID *qidp)
596 struct stat stbuf;
597 int err;
599 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
600 if (err < 0) {
601 return err;
603 stat_to_qid(&stbuf, qidp);
604 return 0;
607 V9fsPDU *pdu_alloc(V9fsState *s)
609 V9fsPDU *pdu = NULL;
611 if (!QLIST_EMPTY(&s->free_list)) {
612 pdu = QLIST_FIRST(&s->free_list);
613 QLIST_REMOVE(pdu, next);
614 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
616 return pdu;
619 void pdu_free(V9fsPDU *pdu)
621 V9fsState *s = pdu->s;
623 g_assert(!pdu->cancelled);
624 QLIST_REMOVE(pdu, next);
625 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
628 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
630 int8_t id = pdu->id + 1; /* Response */
631 V9fsState *s = pdu->s;
632 int ret;
635 * The 9p spec requires that successfully cancelled pdus receive no reply.
636 * Sending a reply would confuse clients because they would
637 * assume that any EINTR is the actual result of the operation,
638 * rather than a consequence of the cancellation. However, if
639 * the operation completed (succesfully or with an error other
640 * than caused be cancellation), we do send out that reply, both
641 * for efficiency and to avoid confusing the rest of the state machine
642 * that assumes passing a non-error here will mean a successful
643 * transmission of the reply.
645 bool discard = pdu->cancelled && len == -EINTR;
646 if (discard) {
647 trace_v9fs_rcancel(pdu->tag, pdu->id);
648 pdu->size = 0;
649 goto out_notify;
652 if (len < 0) {
653 int err = -len;
654 len = 7;
656 if (s->proto_version != V9FS_PROTO_2000L) {
657 V9fsString str;
659 str.data = strerror(err);
660 str.size = strlen(str.data);
662 ret = pdu_marshal(pdu, len, "s", &str);
663 if (ret < 0) {
664 goto out_notify;
666 len += ret;
667 id = P9_RERROR;
670 ret = pdu_marshal(pdu, len, "d", err);
671 if (ret < 0) {
672 goto out_notify;
674 len += ret;
676 if (s->proto_version == V9FS_PROTO_2000L) {
677 id = P9_RLERROR;
679 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
682 /* fill out the header */
683 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
684 goto out_notify;
687 /* keep these in sync */
688 pdu->size = len;
689 pdu->id = id;
691 out_notify:
692 pdu->s->transport->push_and_notify(pdu);
694 /* Now wakeup anybody waiting in flush for this request */
695 if (!qemu_co_queue_next(&pdu->complete)) {
696 pdu_free(pdu);
700 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
702 mode_t ret;
704 ret = mode & 0777;
705 if (mode & P9_STAT_MODE_DIR) {
706 ret |= S_IFDIR;
709 if (mode & P9_STAT_MODE_SYMLINK) {
710 ret |= S_IFLNK;
712 if (mode & P9_STAT_MODE_SOCKET) {
713 ret |= S_IFSOCK;
715 if (mode & P9_STAT_MODE_NAMED_PIPE) {
716 ret |= S_IFIFO;
718 if (mode & P9_STAT_MODE_DEVICE) {
719 if (extension->size && extension->data[0] == 'c') {
720 ret |= S_IFCHR;
721 } else {
722 ret |= S_IFBLK;
726 if (!(ret&~0777)) {
727 ret |= S_IFREG;
730 if (mode & P9_STAT_MODE_SETUID) {
731 ret |= S_ISUID;
733 if (mode & P9_STAT_MODE_SETGID) {
734 ret |= S_ISGID;
736 if (mode & P9_STAT_MODE_SETVTX) {
737 ret |= S_ISVTX;
740 return ret;
743 static int donttouch_stat(V9fsStat *stat)
745 if (stat->type == -1 &&
746 stat->dev == -1 &&
747 stat->qid.type == -1 &&
748 stat->qid.version == -1 &&
749 stat->qid.path == -1 &&
750 stat->mode == -1 &&
751 stat->atime == -1 &&
752 stat->mtime == -1 &&
753 stat->length == -1 &&
754 !stat->name.size &&
755 !stat->uid.size &&
756 !stat->gid.size &&
757 !stat->muid.size &&
758 stat->n_uid == -1 &&
759 stat->n_gid == -1 &&
760 stat->n_muid == -1) {
761 return 1;
764 return 0;
767 static void v9fs_stat_init(V9fsStat *stat)
769 v9fs_string_init(&stat->name);
770 v9fs_string_init(&stat->uid);
771 v9fs_string_init(&stat->gid);
772 v9fs_string_init(&stat->muid);
773 v9fs_string_init(&stat->extension);
776 static void v9fs_stat_free(V9fsStat *stat)
778 v9fs_string_free(&stat->name);
779 v9fs_string_free(&stat->uid);
780 v9fs_string_free(&stat->gid);
781 v9fs_string_free(&stat->muid);
782 v9fs_string_free(&stat->extension);
785 static uint32_t stat_to_v9mode(const struct stat *stbuf)
787 uint32_t mode;
789 mode = stbuf->st_mode & 0777;
790 if (S_ISDIR(stbuf->st_mode)) {
791 mode |= P9_STAT_MODE_DIR;
794 if (S_ISLNK(stbuf->st_mode)) {
795 mode |= P9_STAT_MODE_SYMLINK;
798 if (S_ISSOCK(stbuf->st_mode)) {
799 mode |= P9_STAT_MODE_SOCKET;
802 if (S_ISFIFO(stbuf->st_mode)) {
803 mode |= P9_STAT_MODE_NAMED_PIPE;
806 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
807 mode |= P9_STAT_MODE_DEVICE;
810 if (stbuf->st_mode & S_ISUID) {
811 mode |= P9_STAT_MODE_SETUID;
814 if (stbuf->st_mode & S_ISGID) {
815 mode |= P9_STAT_MODE_SETGID;
818 if (stbuf->st_mode & S_ISVTX) {
819 mode |= P9_STAT_MODE_SETVTX;
822 return mode;
825 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
826 const char *basename,
827 const struct stat *stbuf,
828 V9fsStat *v9stat)
830 int err;
832 memset(v9stat, 0, sizeof(*v9stat));
834 stat_to_qid(stbuf, &v9stat->qid);
835 v9stat->mode = stat_to_v9mode(stbuf);
836 v9stat->atime = stbuf->st_atime;
837 v9stat->mtime = stbuf->st_mtime;
838 v9stat->length = stbuf->st_size;
840 v9fs_string_free(&v9stat->uid);
841 v9fs_string_free(&v9stat->gid);
842 v9fs_string_free(&v9stat->muid);
844 v9stat->n_uid = stbuf->st_uid;
845 v9stat->n_gid = stbuf->st_gid;
846 v9stat->n_muid = 0;
848 v9fs_string_free(&v9stat->extension);
850 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
851 err = v9fs_co_readlink(pdu, path, &v9stat->extension);
852 if (err < 0) {
853 return err;
855 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
856 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
857 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
858 major(stbuf->st_rdev), minor(stbuf->st_rdev));
859 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
860 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
861 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
864 v9fs_string_sprintf(&v9stat->name, "%s", basename);
866 v9stat->size = 61 +
867 v9fs_string_size(&v9stat->name) +
868 v9fs_string_size(&v9stat->uid) +
869 v9fs_string_size(&v9stat->gid) +
870 v9fs_string_size(&v9stat->muid) +
871 v9fs_string_size(&v9stat->extension);
872 return 0;
875 #define P9_STATS_MODE 0x00000001ULL
876 #define P9_STATS_NLINK 0x00000002ULL
877 #define P9_STATS_UID 0x00000004ULL
878 #define P9_STATS_GID 0x00000008ULL
879 #define P9_STATS_RDEV 0x00000010ULL
880 #define P9_STATS_ATIME 0x00000020ULL
881 #define P9_STATS_MTIME 0x00000040ULL
882 #define P9_STATS_CTIME 0x00000080ULL
883 #define P9_STATS_INO 0x00000100ULL
884 #define P9_STATS_SIZE 0x00000200ULL
885 #define P9_STATS_BLOCKS 0x00000400ULL
887 #define P9_STATS_BTIME 0x00000800ULL
888 #define P9_STATS_GEN 0x00001000ULL
889 #define P9_STATS_DATA_VERSION 0x00002000ULL
891 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
892 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
895 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
896 V9fsStatDotl *v9lstat)
898 memset(v9lstat, 0, sizeof(*v9lstat));
900 v9lstat->st_mode = stbuf->st_mode;
901 v9lstat->st_nlink = stbuf->st_nlink;
902 v9lstat->st_uid = stbuf->st_uid;
903 v9lstat->st_gid = stbuf->st_gid;
904 v9lstat->st_rdev = stbuf->st_rdev;
905 v9lstat->st_size = stbuf->st_size;
906 v9lstat->st_blksize = stbuf->st_blksize;
907 v9lstat->st_blocks = stbuf->st_blocks;
908 v9lstat->st_atime_sec = stbuf->st_atime;
909 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
910 v9lstat->st_mtime_sec = stbuf->st_mtime;
911 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
912 v9lstat->st_ctime_sec = stbuf->st_ctime;
913 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
914 /* Currently we only support BASIC fields in stat */
915 v9lstat->st_result_mask = P9_STATS_BASIC;
917 stat_to_qid(stbuf, &v9lstat->qid);
920 static void print_sg(struct iovec *sg, int cnt)
922 int i;
924 printf("sg[%d]: {", cnt);
925 for (i = 0; i < cnt; i++) {
926 if (i) {
927 printf(", ");
929 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
931 printf("}\n");
934 /* Will call this only for path name based fid */
935 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
937 V9fsPath str;
938 v9fs_path_init(&str);
939 v9fs_path_copy(&str, dst);
940 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
941 v9fs_path_free(&str);
944 static inline bool is_ro_export(FsContext *ctx)
946 return ctx->export_flags & V9FS_RDONLY;
949 static void coroutine_fn v9fs_version(void *opaque)
951 ssize_t err;
952 V9fsPDU *pdu = opaque;
953 V9fsState *s = pdu->s;
954 V9fsString version;
955 size_t offset = 7;
957 v9fs_string_init(&version);
958 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
959 if (err < 0) {
960 goto out;
962 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
964 virtfs_reset(pdu);
966 if (!strcmp(version.data, "9P2000.u")) {
967 s->proto_version = V9FS_PROTO_2000U;
968 } else if (!strcmp(version.data, "9P2000.L")) {
969 s->proto_version = V9FS_PROTO_2000L;
970 } else {
971 v9fs_string_sprintf(&version, "unknown");
974 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
975 if (err < 0) {
976 goto out;
978 err += offset;
979 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
980 out:
981 pdu_complete(pdu, err);
982 v9fs_string_free(&version);
985 static void coroutine_fn v9fs_attach(void *opaque)
987 V9fsPDU *pdu = opaque;
988 V9fsState *s = pdu->s;
989 int32_t fid, afid, n_uname;
990 V9fsString uname, aname;
991 V9fsFidState *fidp;
992 size_t offset = 7;
993 V9fsQID qid;
994 ssize_t err;
995 Error *local_err = NULL;
997 v9fs_string_init(&uname);
998 v9fs_string_init(&aname);
999 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
1000 &afid, &uname, &aname, &n_uname);
1001 if (err < 0) {
1002 goto out_nofid;
1004 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1006 fidp = alloc_fid(s, fid);
1007 if (fidp == NULL) {
1008 err = -EINVAL;
1009 goto out_nofid;
1011 fidp->uid = n_uname;
1012 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1013 if (err < 0) {
1014 err = -EINVAL;
1015 clunk_fid(s, fid);
1016 goto out;
1018 err = fid_to_qid(pdu, fidp, &qid);
1019 if (err < 0) {
1020 err = -EINVAL;
1021 clunk_fid(s, fid);
1022 goto out;
1026 * disable migration if we haven't done already.
1027 * attach could get called multiple times for the same export.
1029 if (!s->migration_blocker) {
1030 error_setg(&s->migration_blocker,
1031 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1032 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1033 err = migrate_add_blocker(s->migration_blocker, &local_err);
1034 if (local_err) {
1035 error_free(local_err);
1036 error_free(s->migration_blocker);
1037 s->migration_blocker = NULL;
1038 clunk_fid(s, fid);
1039 goto out;
1041 s->root_fid = fid;
1044 err = pdu_marshal(pdu, offset, "Q", &qid);
1045 if (err < 0) {
1046 clunk_fid(s, fid);
1047 goto out;
1049 err += offset;
1051 memcpy(&s->root_qid, &qid, sizeof(qid));
1052 trace_v9fs_attach_return(pdu->tag, pdu->id,
1053 qid.type, qid.version, qid.path);
1054 out:
1055 put_fid(pdu, fidp);
1056 out_nofid:
1057 pdu_complete(pdu, err);
1058 v9fs_string_free(&uname);
1059 v9fs_string_free(&aname);
1062 static void coroutine_fn v9fs_stat(void *opaque)
1064 int32_t fid;
1065 V9fsStat v9stat;
1066 ssize_t err = 0;
1067 size_t offset = 7;
1068 struct stat stbuf;
1069 V9fsFidState *fidp;
1070 V9fsPDU *pdu = opaque;
1071 char *basename;
1073 err = pdu_unmarshal(pdu, offset, "d", &fid);
1074 if (err < 0) {
1075 goto out_nofid;
1077 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1079 fidp = get_fid(pdu, fid);
1080 if (fidp == NULL) {
1081 err = -ENOENT;
1082 goto out_nofid;
1084 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1085 if (err < 0) {
1086 goto out;
1088 basename = g_path_get_basename(fidp->path.data);
1089 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1090 g_free(basename);
1091 if (err < 0) {
1092 goto out;
1094 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1095 if (err < 0) {
1096 v9fs_stat_free(&v9stat);
1097 goto out;
1099 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1100 v9stat.atime, v9stat.mtime, v9stat.length);
1101 err += offset;
1102 v9fs_stat_free(&v9stat);
1103 out:
1104 put_fid(pdu, fidp);
1105 out_nofid:
1106 pdu_complete(pdu, err);
1109 static void coroutine_fn v9fs_getattr(void *opaque)
1111 int32_t fid;
1112 size_t offset = 7;
1113 ssize_t retval = 0;
1114 struct stat stbuf;
1115 V9fsFidState *fidp;
1116 uint64_t request_mask;
1117 V9fsStatDotl v9stat_dotl;
1118 V9fsPDU *pdu = opaque;
1119 V9fsState *s = pdu->s;
1121 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1122 if (retval < 0) {
1123 goto out_nofid;
1125 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1127 fidp = get_fid(pdu, fid);
1128 if (fidp == NULL) {
1129 retval = -ENOENT;
1130 goto out_nofid;
1133 * Currently we only support BASIC fields in stat, so there is no
1134 * need to look at request_mask.
1136 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1137 if (retval < 0) {
1138 goto out;
1140 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1142 /* fill st_gen if requested and supported by underlying fs */
1143 if (request_mask & P9_STATS_GEN) {
1144 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1145 switch (retval) {
1146 case 0:
1147 /* we have valid st_gen: update result mask */
1148 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1149 break;
1150 case -EINTR:
1151 /* request cancelled, e.g. by Tflush */
1152 goto out;
1153 default:
1154 /* failed to get st_gen: not fatal, ignore */
1155 break;
1158 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1159 if (retval < 0) {
1160 goto out;
1162 retval += offset;
1163 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1164 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1165 v9stat_dotl.st_gid);
1166 out:
1167 put_fid(pdu, fidp);
1168 out_nofid:
1169 pdu_complete(pdu, retval);
1172 /* Attribute flags */
1173 #define P9_ATTR_MODE (1 << 0)
1174 #define P9_ATTR_UID (1 << 1)
1175 #define P9_ATTR_GID (1 << 2)
1176 #define P9_ATTR_SIZE (1 << 3)
1177 #define P9_ATTR_ATIME (1 << 4)
1178 #define P9_ATTR_MTIME (1 << 5)
1179 #define P9_ATTR_CTIME (1 << 6)
1180 #define P9_ATTR_ATIME_SET (1 << 7)
1181 #define P9_ATTR_MTIME_SET (1 << 8)
1183 #define P9_ATTR_MASK 127
1185 static void coroutine_fn v9fs_setattr(void *opaque)
1187 int err = 0;
1188 int32_t fid;
1189 V9fsFidState *fidp;
1190 size_t offset = 7;
1191 V9fsIattr v9iattr;
1192 V9fsPDU *pdu = opaque;
1194 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1195 if (err < 0) {
1196 goto out_nofid;
1199 fidp = get_fid(pdu, fid);
1200 if (fidp == NULL) {
1201 err = -EINVAL;
1202 goto out_nofid;
1204 if (v9iattr.valid & P9_ATTR_MODE) {
1205 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1206 if (err < 0) {
1207 goto out;
1210 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1211 struct timespec times[2];
1212 if (v9iattr.valid & P9_ATTR_ATIME) {
1213 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1214 times[0].tv_sec = v9iattr.atime_sec;
1215 times[0].tv_nsec = v9iattr.atime_nsec;
1216 } else {
1217 times[0].tv_nsec = UTIME_NOW;
1219 } else {
1220 times[0].tv_nsec = UTIME_OMIT;
1222 if (v9iattr.valid & P9_ATTR_MTIME) {
1223 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1224 times[1].tv_sec = v9iattr.mtime_sec;
1225 times[1].tv_nsec = v9iattr.mtime_nsec;
1226 } else {
1227 times[1].tv_nsec = UTIME_NOW;
1229 } else {
1230 times[1].tv_nsec = UTIME_OMIT;
1232 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1233 if (err < 0) {
1234 goto out;
1238 * If the only valid entry in iattr is ctime we can call
1239 * chown(-1,-1) to update the ctime of the file
1241 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1242 ((v9iattr.valid & P9_ATTR_CTIME)
1243 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1244 if (!(v9iattr.valid & P9_ATTR_UID)) {
1245 v9iattr.uid = -1;
1247 if (!(v9iattr.valid & P9_ATTR_GID)) {
1248 v9iattr.gid = -1;
1250 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1251 v9iattr.gid);
1252 if (err < 0) {
1253 goto out;
1256 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1257 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1258 if (err < 0) {
1259 goto out;
1262 err = offset;
1263 out:
1264 put_fid(pdu, fidp);
1265 out_nofid:
1266 pdu_complete(pdu, err);
1269 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1271 int i;
1272 ssize_t err;
1273 size_t offset = 7;
1275 err = pdu_marshal(pdu, offset, "w", nwnames);
1276 if (err < 0) {
1277 return err;
1279 offset += err;
1280 for (i = 0; i < nwnames; i++) {
1281 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1282 if (err < 0) {
1283 return err;
1285 offset += err;
1287 return offset;
1290 static bool name_is_illegal(const char *name)
1292 return !*name || strchr(name, '/') != NULL;
1295 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1297 return
1298 qid1->type != qid2->type ||
1299 qid1->version != qid2->version ||
1300 qid1->path != qid2->path;
1303 static void coroutine_fn v9fs_walk(void *opaque)
1305 int name_idx;
1306 V9fsQID *qids = NULL;
1307 int i, err = 0;
1308 V9fsPath dpath, path;
1309 uint16_t nwnames;
1310 struct stat stbuf;
1311 size_t offset = 7;
1312 int32_t fid, newfid;
1313 V9fsString *wnames = NULL;
1314 V9fsFidState *fidp;
1315 V9fsFidState *newfidp = NULL;
1316 V9fsPDU *pdu = opaque;
1317 V9fsState *s = pdu->s;
1318 V9fsQID qid;
1320 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1321 if (err < 0) {
1322 pdu_complete(pdu, err);
1323 return ;
1325 offset += err;
1327 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1329 if (nwnames && nwnames <= P9_MAXWELEM) {
1330 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1331 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1332 for (i = 0; i < nwnames; i++) {
1333 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1334 if (err < 0) {
1335 goto out_nofid;
1337 if (name_is_illegal(wnames[i].data)) {
1338 err = -ENOENT;
1339 goto out_nofid;
1341 offset += err;
1343 } else if (nwnames > P9_MAXWELEM) {
1344 err = -EINVAL;
1345 goto out_nofid;
1347 fidp = get_fid(pdu, fid);
1348 if (fidp == NULL) {
1349 err = -ENOENT;
1350 goto out_nofid;
1353 v9fs_path_init(&dpath);
1354 v9fs_path_init(&path);
1356 err = fid_to_qid(pdu, fidp, &qid);
1357 if (err < 0) {
1358 goto out;
1362 * Both dpath and path initially poin to fidp.
1363 * Needed to handle request with nwnames == 0
1365 v9fs_path_copy(&dpath, &fidp->path);
1366 v9fs_path_copy(&path, &fidp->path);
1367 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1368 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1369 strcmp("..", wnames[name_idx].data)) {
1370 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1371 &path);
1372 if (err < 0) {
1373 goto out;
1376 err = v9fs_co_lstat(pdu, &path, &stbuf);
1377 if (err < 0) {
1378 goto out;
1380 stat_to_qid(&stbuf, &qid);
1381 v9fs_path_copy(&dpath, &path);
1383 memcpy(&qids[name_idx], &qid, sizeof(qid));
1385 if (fid == newfid) {
1386 if (fidp->fid_type != P9_FID_NONE) {
1387 err = -EINVAL;
1388 goto out;
1390 v9fs_path_copy(&fidp->path, &path);
1391 } else {
1392 newfidp = alloc_fid(s, newfid);
1393 if (newfidp == NULL) {
1394 err = -EINVAL;
1395 goto out;
1397 newfidp->uid = fidp->uid;
1398 v9fs_path_copy(&newfidp->path, &path);
1400 err = v9fs_walk_marshal(pdu, nwnames, qids);
1401 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1402 out:
1403 put_fid(pdu, fidp);
1404 if (newfidp) {
1405 put_fid(pdu, newfidp);
1407 v9fs_path_free(&dpath);
1408 v9fs_path_free(&path);
1409 out_nofid:
1410 pdu_complete(pdu, err);
1411 if (nwnames && nwnames <= P9_MAXWELEM) {
1412 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1413 v9fs_string_free(&wnames[name_idx]);
1415 g_free(wnames);
1416 g_free(qids);
1420 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1422 struct statfs stbuf;
1423 int32_t iounit = 0;
1424 V9fsState *s = pdu->s;
1427 * iounit should be multiples of f_bsize (host filesystem block size
1428 * and as well as less than (client msize - P9_IOHDRSZ))
1430 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1431 iounit = stbuf.f_bsize;
1432 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1434 if (!iounit) {
1435 iounit = s->msize - P9_IOHDRSZ;
1437 return iounit;
1440 static void coroutine_fn v9fs_open(void *opaque)
1442 int flags;
1443 int32_t fid;
1444 int32_t mode;
1445 V9fsQID qid;
1446 int iounit = 0;
1447 ssize_t err = 0;
1448 size_t offset = 7;
1449 struct stat stbuf;
1450 V9fsFidState *fidp;
1451 V9fsPDU *pdu = opaque;
1452 V9fsState *s = pdu->s;
1454 if (s->proto_version == V9FS_PROTO_2000L) {
1455 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1456 } else {
1457 uint8_t modebyte;
1458 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1459 mode = modebyte;
1461 if (err < 0) {
1462 goto out_nofid;
1464 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1466 fidp = get_fid(pdu, fid);
1467 if (fidp == NULL) {
1468 err = -ENOENT;
1469 goto out_nofid;
1471 if (fidp->fid_type != P9_FID_NONE) {
1472 err = -EINVAL;
1473 goto out;
1476 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1477 if (err < 0) {
1478 goto out;
1480 stat_to_qid(&stbuf, &qid);
1481 if (S_ISDIR(stbuf.st_mode)) {
1482 err = v9fs_co_opendir(pdu, fidp);
1483 if (err < 0) {
1484 goto out;
1486 fidp->fid_type = P9_FID_DIR;
1487 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1488 if (err < 0) {
1489 goto out;
1491 err += offset;
1492 } else {
1493 if (s->proto_version == V9FS_PROTO_2000L) {
1494 flags = get_dotl_openflags(s, mode);
1495 } else {
1496 flags = omode_to_uflags(mode);
1498 if (is_ro_export(&s->ctx)) {
1499 if (mode & O_WRONLY || mode & O_RDWR ||
1500 mode & O_APPEND || mode & O_TRUNC) {
1501 err = -EROFS;
1502 goto out;
1505 err = v9fs_co_open(pdu, fidp, flags);
1506 if (err < 0) {
1507 goto out;
1509 fidp->fid_type = P9_FID_FILE;
1510 fidp->open_flags = flags;
1511 if (flags & O_EXCL) {
1513 * We let the host file system do O_EXCL check
1514 * We should not reclaim such fd
1516 fidp->flags |= FID_NON_RECLAIMABLE;
1518 iounit = get_iounit(pdu, &fidp->path);
1519 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1520 if (err < 0) {
1521 goto out;
1523 err += offset;
1525 trace_v9fs_open_return(pdu->tag, pdu->id,
1526 qid.type, qid.version, qid.path, iounit);
1527 out:
1528 put_fid(pdu, fidp);
1529 out_nofid:
1530 pdu_complete(pdu, err);
1533 static void coroutine_fn v9fs_lcreate(void *opaque)
1535 int32_t dfid, flags, mode;
1536 gid_t gid;
1537 ssize_t err = 0;
1538 ssize_t offset = 7;
1539 V9fsString name;
1540 V9fsFidState *fidp;
1541 struct stat stbuf;
1542 V9fsQID qid;
1543 int32_t iounit;
1544 V9fsPDU *pdu = opaque;
1546 v9fs_string_init(&name);
1547 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1548 &name, &flags, &mode, &gid);
1549 if (err < 0) {
1550 goto out_nofid;
1552 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1554 if (name_is_illegal(name.data)) {
1555 err = -ENOENT;
1556 goto out_nofid;
1559 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1560 err = -EEXIST;
1561 goto out_nofid;
1564 fidp = get_fid(pdu, dfid);
1565 if (fidp == NULL) {
1566 err = -ENOENT;
1567 goto out_nofid;
1569 if (fidp->fid_type != P9_FID_NONE) {
1570 err = -EINVAL;
1571 goto out;
1574 flags = get_dotl_openflags(pdu->s, flags);
1575 err = v9fs_co_open2(pdu, fidp, &name, gid,
1576 flags | O_CREAT, mode, &stbuf);
1577 if (err < 0) {
1578 goto out;
1580 fidp->fid_type = P9_FID_FILE;
1581 fidp->open_flags = flags;
1582 if (flags & O_EXCL) {
1584 * We let the host file system do O_EXCL check
1585 * We should not reclaim such fd
1587 fidp->flags |= FID_NON_RECLAIMABLE;
1589 iounit = get_iounit(pdu, &fidp->path);
1590 stat_to_qid(&stbuf, &qid);
1591 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1592 if (err < 0) {
1593 goto out;
1595 err += offset;
1596 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1597 qid.type, qid.version, qid.path, iounit);
1598 out:
1599 put_fid(pdu, fidp);
1600 out_nofid:
1601 pdu_complete(pdu, err);
1602 v9fs_string_free(&name);
1605 static void coroutine_fn v9fs_fsync(void *opaque)
1607 int err;
1608 int32_t fid;
1609 int datasync;
1610 size_t offset = 7;
1611 V9fsFidState *fidp;
1612 V9fsPDU *pdu = opaque;
1614 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1615 if (err < 0) {
1616 goto out_nofid;
1618 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1620 fidp = get_fid(pdu, fid);
1621 if (fidp == NULL) {
1622 err = -ENOENT;
1623 goto out_nofid;
1625 err = v9fs_co_fsync(pdu, fidp, datasync);
1626 if (!err) {
1627 err = offset;
1629 put_fid(pdu, fidp);
1630 out_nofid:
1631 pdu_complete(pdu, err);
1634 static void coroutine_fn v9fs_clunk(void *opaque)
1636 int err;
1637 int32_t fid;
1638 size_t offset = 7;
1639 V9fsFidState *fidp;
1640 V9fsPDU *pdu = opaque;
1641 V9fsState *s = pdu->s;
1643 err = pdu_unmarshal(pdu, offset, "d", &fid);
1644 if (err < 0) {
1645 goto out_nofid;
1647 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1649 fidp = clunk_fid(s, fid);
1650 if (fidp == NULL) {
1651 err = -ENOENT;
1652 goto out_nofid;
1655 * Bump the ref so that put_fid will
1656 * free the fid.
1658 fidp->ref++;
1659 err = put_fid(pdu, fidp);
1660 if (!err) {
1661 err = offset;
1663 out_nofid:
1664 pdu_complete(pdu, err);
1668 * Create a QEMUIOVector for a sub-region of PDU iovecs
1670 * @qiov: uninitialized QEMUIOVector
1671 * @skip: number of bytes to skip from beginning of PDU
1672 * @size: number of bytes to include
1673 * @is_write: true - write, false - read
1675 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1676 * with qemu_iovec_destroy().
1678 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1679 size_t skip, size_t size,
1680 bool is_write)
1682 QEMUIOVector elem;
1683 struct iovec *iov;
1684 unsigned int niov;
1686 if (is_write) {
1687 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
1688 } else {
1689 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
1692 qemu_iovec_init_external(&elem, iov, niov);
1693 qemu_iovec_init(qiov, niov);
1694 qemu_iovec_concat(qiov, &elem, skip, size);
1697 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1698 uint64_t off, uint32_t max_count)
1700 ssize_t err;
1701 size_t offset = 7;
1702 uint64_t read_count;
1703 QEMUIOVector qiov_full;
1705 if (fidp->fs.xattr.len < off) {
1706 read_count = 0;
1707 } else {
1708 read_count = fidp->fs.xattr.len - off;
1710 if (read_count > max_count) {
1711 read_count = max_count;
1713 err = pdu_marshal(pdu, offset, "d", read_count);
1714 if (err < 0) {
1715 return err;
1717 offset += err;
1719 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1720 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
1721 ((char *)fidp->fs.xattr.value) + off,
1722 read_count);
1723 qemu_iovec_destroy(&qiov_full);
1724 if (err < 0) {
1725 return err;
1727 offset += err;
1728 return offset;
1731 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1732 V9fsFidState *fidp,
1733 uint32_t max_count)
1735 V9fsPath path;
1736 V9fsStat v9stat;
1737 int len, err = 0;
1738 int32_t count = 0;
1739 struct stat stbuf;
1740 off_t saved_dir_pos;
1741 struct dirent *dent;
1743 /* save the directory position */
1744 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1745 if (saved_dir_pos < 0) {
1746 return saved_dir_pos;
1749 while (1) {
1750 v9fs_path_init(&path);
1752 v9fs_readdir_lock(&fidp->fs.dir);
1754 err = v9fs_co_readdir(pdu, fidp, &dent);
1755 if (err || !dent) {
1756 break;
1758 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1759 if (err < 0) {
1760 break;
1762 err = v9fs_co_lstat(pdu, &path, &stbuf);
1763 if (err < 0) {
1764 break;
1766 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
1767 if (err < 0) {
1768 break;
1770 if ((count + v9stat.size + 2) > max_count) {
1771 v9fs_readdir_unlock(&fidp->fs.dir);
1773 /* Ran out of buffer. Set dir back to old position and return */
1774 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1775 v9fs_stat_free(&v9stat);
1776 v9fs_path_free(&path);
1777 return count;
1780 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1781 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1783 v9fs_readdir_unlock(&fidp->fs.dir);
1785 if (len < 0) {
1786 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1787 v9fs_stat_free(&v9stat);
1788 v9fs_path_free(&path);
1789 return len;
1791 count += len;
1792 v9fs_stat_free(&v9stat);
1793 v9fs_path_free(&path);
1794 saved_dir_pos = dent->d_off;
1797 v9fs_readdir_unlock(&fidp->fs.dir);
1799 v9fs_path_free(&path);
1800 if (err < 0) {
1801 return err;
1803 return count;
1806 static void coroutine_fn v9fs_read(void *opaque)
1808 int32_t fid;
1809 uint64_t off;
1810 ssize_t err = 0;
1811 int32_t count = 0;
1812 size_t offset = 7;
1813 uint32_t max_count;
1814 V9fsFidState *fidp;
1815 V9fsPDU *pdu = opaque;
1816 V9fsState *s = pdu->s;
1818 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1819 if (err < 0) {
1820 goto out_nofid;
1822 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1824 fidp = get_fid(pdu, fid);
1825 if (fidp == NULL) {
1826 err = -EINVAL;
1827 goto out_nofid;
1829 if (fidp->fid_type == P9_FID_DIR) {
1831 if (off == 0) {
1832 v9fs_co_rewinddir(pdu, fidp);
1834 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1835 if (count < 0) {
1836 err = count;
1837 goto out;
1839 err = pdu_marshal(pdu, offset, "d", count);
1840 if (err < 0) {
1841 goto out;
1843 err += offset + count;
1844 } else if (fidp->fid_type == P9_FID_FILE) {
1845 QEMUIOVector qiov_full;
1846 QEMUIOVector qiov;
1847 int32_t len;
1849 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1850 qemu_iovec_init(&qiov, qiov_full.niov);
1851 do {
1852 qemu_iovec_reset(&qiov);
1853 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1854 if (0) {
1855 print_sg(qiov.iov, qiov.niov);
1857 /* Loop in case of EINTR */
1858 do {
1859 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1860 if (len >= 0) {
1861 off += len;
1862 count += len;
1864 } while (len == -EINTR && !pdu->cancelled);
1865 if (len < 0) {
1866 /* IO error return the error */
1867 err = len;
1868 goto out_free_iovec;
1870 } while (count < max_count && len > 0);
1871 err = pdu_marshal(pdu, offset, "d", count);
1872 if (err < 0) {
1873 goto out_free_iovec;
1875 err += offset + count;
1876 out_free_iovec:
1877 qemu_iovec_destroy(&qiov);
1878 qemu_iovec_destroy(&qiov_full);
1879 } else if (fidp->fid_type == P9_FID_XATTR) {
1880 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1881 } else {
1882 err = -EINVAL;
1884 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1885 out:
1886 put_fid(pdu, fidp);
1887 out_nofid:
1888 pdu_complete(pdu, err);
1891 static size_t v9fs_readdir_data_size(V9fsString *name)
1894 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1895 * size of type (1) + size of name.size (2) + strlen(name.data)
1897 return 24 + v9fs_string_size(name);
1900 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1901 int32_t max_count)
1903 size_t size;
1904 V9fsQID qid;
1905 V9fsString name;
1906 int len, err = 0;
1907 int32_t count = 0;
1908 off_t saved_dir_pos;
1909 struct dirent *dent;
1911 /* save the directory position */
1912 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1913 if (saved_dir_pos < 0) {
1914 return saved_dir_pos;
1917 while (1) {
1918 v9fs_readdir_lock(&fidp->fs.dir);
1920 err = v9fs_co_readdir(pdu, fidp, &dent);
1921 if (err || !dent) {
1922 break;
1924 v9fs_string_init(&name);
1925 v9fs_string_sprintf(&name, "%s", dent->d_name);
1926 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1927 v9fs_readdir_unlock(&fidp->fs.dir);
1929 /* Ran out of buffer. Set dir back to old position and return */
1930 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1931 v9fs_string_free(&name);
1932 return count;
1935 * Fill up just the path field of qid because the client uses
1936 * only that. To fill the entire qid structure we will have
1937 * to stat each dirent found, which is expensive
1939 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1940 memcpy(&qid.path, &dent->d_ino, size);
1941 /* Fill the other fields with dummy values */
1942 qid.type = 0;
1943 qid.version = 0;
1945 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1946 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1947 &qid, dent->d_off,
1948 dent->d_type, &name);
1950 v9fs_readdir_unlock(&fidp->fs.dir);
1952 if (len < 0) {
1953 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1954 v9fs_string_free(&name);
1955 return len;
1957 count += len;
1958 v9fs_string_free(&name);
1959 saved_dir_pos = dent->d_off;
1962 v9fs_readdir_unlock(&fidp->fs.dir);
1964 if (err < 0) {
1965 return err;
1967 return count;
1970 static void coroutine_fn v9fs_readdir(void *opaque)
1972 int32_t fid;
1973 V9fsFidState *fidp;
1974 ssize_t retval = 0;
1975 size_t offset = 7;
1976 uint64_t initial_offset;
1977 int32_t count;
1978 uint32_t max_count;
1979 V9fsPDU *pdu = opaque;
1981 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1982 &initial_offset, &max_count);
1983 if (retval < 0) {
1984 goto out_nofid;
1986 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1988 fidp = get_fid(pdu, fid);
1989 if (fidp == NULL) {
1990 retval = -EINVAL;
1991 goto out_nofid;
1993 if (!fidp->fs.dir.stream) {
1994 retval = -EINVAL;
1995 goto out;
1997 if (initial_offset == 0) {
1998 v9fs_co_rewinddir(pdu, fidp);
1999 } else {
2000 v9fs_co_seekdir(pdu, fidp, initial_offset);
2002 count = v9fs_do_readdir(pdu, fidp, max_count);
2003 if (count < 0) {
2004 retval = count;
2005 goto out;
2007 retval = pdu_marshal(pdu, offset, "d", count);
2008 if (retval < 0) {
2009 goto out;
2011 retval += count + offset;
2012 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2013 out:
2014 put_fid(pdu, fidp);
2015 out_nofid:
2016 pdu_complete(pdu, retval);
2019 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2020 uint64_t off, uint32_t count,
2021 struct iovec *sg, int cnt)
2023 int i, to_copy;
2024 ssize_t err = 0;
2025 uint64_t write_count;
2026 size_t offset = 7;
2029 if (fidp->fs.xattr.len < off) {
2030 err = -ENOSPC;
2031 goto out;
2033 write_count = fidp->fs.xattr.len - off;
2034 if (write_count > count) {
2035 write_count = count;
2037 err = pdu_marshal(pdu, offset, "d", write_count);
2038 if (err < 0) {
2039 return err;
2041 err += offset;
2042 fidp->fs.xattr.copied_len += write_count;
2044 * Now copy the content from sg list
2046 for (i = 0; i < cnt; i++) {
2047 if (write_count > sg[i].iov_len) {
2048 to_copy = sg[i].iov_len;
2049 } else {
2050 to_copy = write_count;
2052 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2053 /* updating vs->off since we are not using below */
2054 off += to_copy;
2055 write_count -= to_copy;
2057 out:
2058 return err;
2061 static void coroutine_fn v9fs_write(void *opaque)
2063 ssize_t err;
2064 int32_t fid;
2065 uint64_t off;
2066 uint32_t count;
2067 int32_t len = 0;
2068 int32_t total = 0;
2069 size_t offset = 7;
2070 V9fsFidState *fidp;
2071 V9fsPDU *pdu = opaque;
2072 V9fsState *s = pdu->s;
2073 QEMUIOVector qiov_full;
2074 QEMUIOVector qiov;
2076 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2077 if (err < 0) {
2078 pdu_complete(pdu, err);
2079 return;
2081 offset += err;
2082 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2083 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2085 fidp = get_fid(pdu, fid);
2086 if (fidp == NULL) {
2087 err = -EINVAL;
2088 goto out_nofid;
2090 if (fidp->fid_type == P9_FID_FILE) {
2091 if (fidp->fs.fd == -1) {
2092 err = -EINVAL;
2093 goto out;
2095 } else if (fidp->fid_type == P9_FID_XATTR) {
2097 * setxattr operation
2099 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2100 qiov_full.iov, qiov_full.niov);
2101 goto out;
2102 } else {
2103 err = -EINVAL;
2104 goto out;
2106 qemu_iovec_init(&qiov, qiov_full.niov);
2107 do {
2108 qemu_iovec_reset(&qiov);
2109 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2110 if (0) {
2111 print_sg(qiov.iov, qiov.niov);
2113 /* Loop in case of EINTR */
2114 do {
2115 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2116 if (len >= 0) {
2117 off += len;
2118 total += len;
2120 } while (len == -EINTR && !pdu->cancelled);
2121 if (len < 0) {
2122 /* IO error return the error */
2123 err = len;
2124 goto out_qiov;
2126 } while (total < count && len > 0);
2128 offset = 7;
2129 err = pdu_marshal(pdu, offset, "d", total);
2130 if (err < 0) {
2131 goto out_qiov;
2133 err += offset;
2134 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2135 out_qiov:
2136 qemu_iovec_destroy(&qiov);
2137 out:
2138 put_fid(pdu, fidp);
2139 out_nofid:
2140 qemu_iovec_destroy(&qiov_full);
2141 pdu_complete(pdu, err);
2144 static void coroutine_fn v9fs_create(void *opaque)
2146 int32_t fid;
2147 int err = 0;
2148 size_t offset = 7;
2149 V9fsFidState *fidp;
2150 V9fsQID qid;
2151 int32_t perm;
2152 int8_t mode;
2153 V9fsPath path;
2154 struct stat stbuf;
2155 V9fsString name;
2156 V9fsString extension;
2157 int iounit;
2158 V9fsPDU *pdu = opaque;
2160 v9fs_path_init(&path);
2161 v9fs_string_init(&name);
2162 v9fs_string_init(&extension);
2163 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2164 &perm, &mode, &extension);
2165 if (err < 0) {
2166 goto out_nofid;
2168 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2170 if (name_is_illegal(name.data)) {
2171 err = -ENOENT;
2172 goto out_nofid;
2175 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2176 err = -EEXIST;
2177 goto out_nofid;
2180 fidp = get_fid(pdu, fid);
2181 if (fidp == NULL) {
2182 err = -EINVAL;
2183 goto out_nofid;
2185 if (fidp->fid_type != P9_FID_NONE) {
2186 err = -EINVAL;
2187 goto out;
2189 if (perm & P9_STAT_MODE_DIR) {
2190 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2191 fidp->uid, -1, &stbuf);
2192 if (err < 0) {
2193 goto out;
2195 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2196 if (err < 0) {
2197 goto out;
2199 v9fs_path_copy(&fidp->path, &path);
2200 err = v9fs_co_opendir(pdu, fidp);
2201 if (err < 0) {
2202 goto out;
2204 fidp->fid_type = P9_FID_DIR;
2205 } else if (perm & P9_STAT_MODE_SYMLINK) {
2206 err = v9fs_co_symlink(pdu, fidp, &name,
2207 extension.data, -1 , &stbuf);
2208 if (err < 0) {
2209 goto out;
2211 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2212 if (err < 0) {
2213 goto out;
2215 v9fs_path_copy(&fidp->path, &path);
2216 } else if (perm & P9_STAT_MODE_LINK) {
2217 int32_t ofid = atoi(extension.data);
2218 V9fsFidState *ofidp = get_fid(pdu, ofid);
2219 if (ofidp == NULL) {
2220 err = -EINVAL;
2221 goto out;
2223 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2224 put_fid(pdu, ofidp);
2225 if (err < 0) {
2226 goto out;
2228 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2229 if (err < 0) {
2230 fidp->fid_type = P9_FID_NONE;
2231 goto out;
2233 v9fs_path_copy(&fidp->path, &path);
2234 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2235 if (err < 0) {
2236 fidp->fid_type = P9_FID_NONE;
2237 goto out;
2239 } else if (perm & P9_STAT_MODE_DEVICE) {
2240 char ctype;
2241 uint32_t major, minor;
2242 mode_t nmode = 0;
2244 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2245 err = -errno;
2246 goto out;
2249 switch (ctype) {
2250 case 'c':
2251 nmode = S_IFCHR;
2252 break;
2253 case 'b':
2254 nmode = S_IFBLK;
2255 break;
2256 default:
2257 err = -EIO;
2258 goto out;
2261 nmode |= perm & 0777;
2262 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2263 makedev(major, minor), nmode, &stbuf);
2264 if (err < 0) {
2265 goto out;
2267 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2268 if (err < 0) {
2269 goto out;
2271 v9fs_path_copy(&fidp->path, &path);
2272 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2273 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2274 0, S_IFIFO | (perm & 0777), &stbuf);
2275 if (err < 0) {
2276 goto out;
2278 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2279 if (err < 0) {
2280 goto out;
2282 v9fs_path_copy(&fidp->path, &path);
2283 } else if (perm & P9_STAT_MODE_SOCKET) {
2284 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2285 0, S_IFSOCK | (perm & 0777), &stbuf);
2286 if (err < 0) {
2287 goto out;
2289 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2290 if (err < 0) {
2291 goto out;
2293 v9fs_path_copy(&fidp->path, &path);
2294 } else {
2295 err = v9fs_co_open2(pdu, fidp, &name, -1,
2296 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2297 if (err < 0) {
2298 goto out;
2300 fidp->fid_type = P9_FID_FILE;
2301 fidp->open_flags = omode_to_uflags(mode);
2302 if (fidp->open_flags & O_EXCL) {
2304 * We let the host file system do O_EXCL check
2305 * We should not reclaim such fd
2307 fidp->flags |= FID_NON_RECLAIMABLE;
2310 iounit = get_iounit(pdu, &fidp->path);
2311 stat_to_qid(&stbuf, &qid);
2312 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2313 if (err < 0) {
2314 goto out;
2316 err += offset;
2317 trace_v9fs_create_return(pdu->tag, pdu->id,
2318 qid.type, qid.version, qid.path, iounit);
2319 out:
2320 put_fid(pdu, fidp);
2321 out_nofid:
2322 pdu_complete(pdu, err);
2323 v9fs_string_free(&name);
2324 v9fs_string_free(&extension);
2325 v9fs_path_free(&path);
2328 static void coroutine_fn v9fs_symlink(void *opaque)
2330 V9fsPDU *pdu = opaque;
2331 V9fsString name;
2332 V9fsString symname;
2333 V9fsFidState *dfidp;
2334 V9fsQID qid;
2335 struct stat stbuf;
2336 int32_t dfid;
2337 int err = 0;
2338 gid_t gid;
2339 size_t offset = 7;
2341 v9fs_string_init(&name);
2342 v9fs_string_init(&symname);
2343 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2344 if (err < 0) {
2345 goto out_nofid;
2347 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2349 if (name_is_illegal(name.data)) {
2350 err = -ENOENT;
2351 goto out_nofid;
2354 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2355 err = -EEXIST;
2356 goto out_nofid;
2359 dfidp = get_fid(pdu, dfid);
2360 if (dfidp == NULL) {
2361 err = -EINVAL;
2362 goto out_nofid;
2364 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2365 if (err < 0) {
2366 goto out;
2368 stat_to_qid(&stbuf, &qid);
2369 err = pdu_marshal(pdu, offset, "Q", &qid);
2370 if (err < 0) {
2371 goto out;
2373 err += offset;
2374 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2375 qid.type, qid.version, qid.path);
2376 out:
2377 put_fid(pdu, dfidp);
2378 out_nofid:
2379 pdu_complete(pdu, err);
2380 v9fs_string_free(&name);
2381 v9fs_string_free(&symname);
2384 static void coroutine_fn v9fs_flush(void *opaque)
2386 ssize_t err;
2387 int16_t tag;
2388 size_t offset = 7;
2389 V9fsPDU *cancel_pdu = NULL;
2390 V9fsPDU *pdu = opaque;
2391 V9fsState *s = pdu->s;
2393 err = pdu_unmarshal(pdu, offset, "w", &tag);
2394 if (err < 0) {
2395 pdu_complete(pdu, err);
2396 return;
2398 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2400 if (pdu->tag == tag) {
2401 warn_report("the guest sent a self-referencing 9P flush request");
2402 } else {
2403 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2404 if (cancel_pdu->tag == tag) {
2405 break;
2409 if (cancel_pdu) {
2410 cancel_pdu->cancelled = 1;
2412 * Wait for pdu to complete.
2414 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2415 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2416 cancel_pdu->cancelled = 0;
2417 pdu_free(cancel_pdu);
2420 pdu_complete(pdu, 7);
2423 static void coroutine_fn v9fs_link(void *opaque)
2425 V9fsPDU *pdu = opaque;
2426 int32_t dfid, oldfid;
2427 V9fsFidState *dfidp, *oldfidp;
2428 V9fsString name;
2429 size_t offset = 7;
2430 int err = 0;
2432 v9fs_string_init(&name);
2433 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2434 if (err < 0) {
2435 goto out_nofid;
2437 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2439 if (name_is_illegal(name.data)) {
2440 err = -ENOENT;
2441 goto out_nofid;
2444 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2445 err = -EEXIST;
2446 goto out_nofid;
2449 dfidp = get_fid(pdu, dfid);
2450 if (dfidp == NULL) {
2451 err = -ENOENT;
2452 goto out_nofid;
2455 oldfidp = get_fid(pdu, oldfid);
2456 if (oldfidp == NULL) {
2457 err = -ENOENT;
2458 goto out;
2460 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2461 if (!err) {
2462 err = offset;
2464 put_fid(pdu, oldfidp);
2465 out:
2466 put_fid(pdu, dfidp);
2467 out_nofid:
2468 v9fs_string_free(&name);
2469 pdu_complete(pdu, err);
2472 /* Only works with path name based fid */
2473 static void coroutine_fn v9fs_remove(void *opaque)
2475 int32_t fid;
2476 int err = 0;
2477 size_t offset = 7;
2478 V9fsFidState *fidp;
2479 V9fsPDU *pdu = opaque;
2481 err = pdu_unmarshal(pdu, offset, "d", &fid);
2482 if (err < 0) {
2483 goto out_nofid;
2485 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2487 fidp = get_fid(pdu, fid);
2488 if (fidp == NULL) {
2489 err = -EINVAL;
2490 goto out_nofid;
2492 /* if fs driver is not path based, return EOPNOTSUPP */
2493 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2494 err = -EOPNOTSUPP;
2495 goto out_err;
2498 * IF the file is unlinked, we cannot reopen
2499 * the file later. So don't reclaim fd
2501 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2502 if (err < 0) {
2503 goto out_err;
2505 err = v9fs_co_remove(pdu, &fidp->path);
2506 if (!err) {
2507 err = offset;
2509 out_err:
2510 /* For TREMOVE we need to clunk the fid even on failed remove */
2511 clunk_fid(pdu->s, fidp->fid);
2512 put_fid(pdu, fidp);
2513 out_nofid:
2514 pdu_complete(pdu, err);
2517 static void coroutine_fn v9fs_unlinkat(void *opaque)
2519 int err = 0;
2520 V9fsString name;
2521 int32_t dfid, flags;
2522 size_t offset = 7;
2523 V9fsPath path;
2524 V9fsFidState *dfidp;
2525 V9fsPDU *pdu = opaque;
2527 v9fs_string_init(&name);
2528 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2529 if (err < 0) {
2530 goto out_nofid;
2533 if (name_is_illegal(name.data)) {
2534 err = -ENOENT;
2535 goto out_nofid;
2538 if (!strcmp(".", name.data)) {
2539 err = -EINVAL;
2540 goto out_nofid;
2543 if (!strcmp("..", name.data)) {
2544 err = -ENOTEMPTY;
2545 goto out_nofid;
2548 dfidp = get_fid(pdu, dfid);
2549 if (dfidp == NULL) {
2550 err = -EINVAL;
2551 goto out_nofid;
2554 * IF the file is unlinked, we cannot reopen
2555 * the file later. So don't reclaim fd
2557 v9fs_path_init(&path);
2558 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2559 if (err < 0) {
2560 goto out_err;
2562 err = v9fs_mark_fids_unreclaim(pdu, &path);
2563 if (err < 0) {
2564 goto out_err;
2566 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2567 if (!err) {
2568 err = offset;
2570 out_err:
2571 put_fid(pdu, dfidp);
2572 v9fs_path_free(&path);
2573 out_nofid:
2574 pdu_complete(pdu, err);
2575 v9fs_string_free(&name);
2579 /* Only works with path name based fid */
2580 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2581 int32_t newdirfid,
2582 V9fsString *name)
2584 int err = 0;
2585 V9fsPath new_path;
2586 V9fsFidState *tfidp;
2587 V9fsState *s = pdu->s;
2588 V9fsFidState *dirfidp = NULL;
2590 v9fs_path_init(&new_path);
2591 if (newdirfid != -1) {
2592 dirfidp = get_fid(pdu, newdirfid);
2593 if (dirfidp == NULL) {
2594 err = -ENOENT;
2595 goto out_nofid;
2597 if (fidp->fid_type != P9_FID_NONE) {
2598 err = -EINVAL;
2599 goto out;
2601 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2602 if (err < 0) {
2603 goto out;
2605 } else {
2606 char *dir_name = g_path_get_dirname(fidp->path.data);
2607 V9fsPath dir_path;
2609 v9fs_path_init(&dir_path);
2610 v9fs_path_sprintf(&dir_path, "%s", dir_name);
2611 g_free(dir_name);
2613 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
2614 v9fs_path_free(&dir_path);
2615 if (err < 0) {
2616 goto out;
2619 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2620 if (err < 0) {
2621 goto out;
2624 * Fixup fid's pointing to the old name to
2625 * start pointing to the new name
2627 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2628 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2629 /* replace the name */
2630 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2633 out:
2634 if (dirfidp) {
2635 put_fid(pdu, dirfidp);
2637 v9fs_path_free(&new_path);
2638 out_nofid:
2639 return err;
2642 /* Only works with path name based fid */
2643 static void coroutine_fn v9fs_rename(void *opaque)
2645 int32_t fid;
2646 ssize_t err = 0;
2647 size_t offset = 7;
2648 V9fsString name;
2649 int32_t newdirfid;
2650 V9fsFidState *fidp;
2651 V9fsPDU *pdu = opaque;
2652 V9fsState *s = pdu->s;
2654 v9fs_string_init(&name);
2655 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2656 if (err < 0) {
2657 goto out_nofid;
2660 if (name_is_illegal(name.data)) {
2661 err = -ENOENT;
2662 goto out_nofid;
2665 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2666 err = -EISDIR;
2667 goto out_nofid;
2670 fidp = get_fid(pdu, fid);
2671 if (fidp == NULL) {
2672 err = -ENOENT;
2673 goto out_nofid;
2675 if (fidp->fid_type != P9_FID_NONE) {
2676 err = -EINVAL;
2677 goto out;
2679 /* if fs driver is not path based, return EOPNOTSUPP */
2680 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2681 err = -EOPNOTSUPP;
2682 goto out;
2684 v9fs_path_write_lock(s);
2685 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2686 v9fs_path_unlock(s);
2687 if (!err) {
2688 err = offset;
2690 out:
2691 put_fid(pdu, fidp);
2692 out_nofid:
2693 pdu_complete(pdu, err);
2694 v9fs_string_free(&name);
2697 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2698 V9fsString *old_name,
2699 V9fsPath *newdir,
2700 V9fsString *new_name)
2702 V9fsFidState *tfidp;
2703 V9fsPath oldpath, newpath;
2704 V9fsState *s = pdu->s;
2705 int err;
2707 v9fs_path_init(&oldpath);
2708 v9fs_path_init(&newpath);
2709 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2710 if (err < 0) {
2711 goto out;
2713 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2714 if (err < 0) {
2715 goto out;
2719 * Fixup fid's pointing to the old name to
2720 * start pointing to the new name
2722 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2723 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2724 /* replace the name */
2725 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2728 out:
2729 v9fs_path_free(&oldpath);
2730 v9fs_path_free(&newpath);
2731 return err;
2734 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2735 V9fsString *old_name,
2736 int32_t newdirfid,
2737 V9fsString *new_name)
2739 int err = 0;
2740 V9fsState *s = pdu->s;
2741 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2743 olddirfidp = get_fid(pdu, olddirfid);
2744 if (olddirfidp == NULL) {
2745 err = -ENOENT;
2746 goto out;
2748 if (newdirfid != -1) {
2749 newdirfidp = get_fid(pdu, newdirfid);
2750 if (newdirfidp == NULL) {
2751 err = -ENOENT;
2752 goto out;
2754 } else {
2755 newdirfidp = get_fid(pdu, olddirfid);
2758 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2759 &newdirfidp->path, new_name);
2760 if (err < 0) {
2761 goto out;
2763 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2764 /* Only for path based fid we need to do the below fixup */
2765 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2766 &newdirfidp->path, new_name);
2768 out:
2769 if (olddirfidp) {
2770 put_fid(pdu, olddirfidp);
2772 if (newdirfidp) {
2773 put_fid(pdu, newdirfidp);
2775 return err;
2778 static void coroutine_fn v9fs_renameat(void *opaque)
2780 ssize_t err = 0;
2781 size_t offset = 7;
2782 V9fsPDU *pdu = opaque;
2783 V9fsState *s = pdu->s;
2784 int32_t olddirfid, newdirfid;
2785 V9fsString old_name, new_name;
2787 v9fs_string_init(&old_name);
2788 v9fs_string_init(&new_name);
2789 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2790 &old_name, &newdirfid, &new_name);
2791 if (err < 0) {
2792 goto out_err;
2795 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2796 err = -ENOENT;
2797 goto out_err;
2800 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2801 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2802 err = -EISDIR;
2803 goto out_err;
2806 v9fs_path_write_lock(s);
2807 err = v9fs_complete_renameat(pdu, olddirfid,
2808 &old_name, newdirfid, &new_name);
2809 v9fs_path_unlock(s);
2810 if (!err) {
2811 err = offset;
2814 out_err:
2815 pdu_complete(pdu, err);
2816 v9fs_string_free(&old_name);
2817 v9fs_string_free(&new_name);
2820 static void coroutine_fn v9fs_wstat(void *opaque)
2822 int32_t fid;
2823 int err = 0;
2824 int16_t unused;
2825 V9fsStat v9stat;
2826 size_t offset = 7;
2827 struct stat stbuf;
2828 V9fsFidState *fidp;
2829 V9fsPDU *pdu = opaque;
2831 v9fs_stat_init(&v9stat);
2832 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2833 if (err < 0) {
2834 goto out_nofid;
2836 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2837 v9stat.mode, v9stat.atime, v9stat.mtime);
2839 fidp = get_fid(pdu, fid);
2840 if (fidp == NULL) {
2841 err = -EINVAL;
2842 goto out_nofid;
2844 /* do we need to sync the file? */
2845 if (donttouch_stat(&v9stat)) {
2846 err = v9fs_co_fsync(pdu, fidp, 0);
2847 goto out;
2849 if (v9stat.mode != -1) {
2850 uint32_t v9_mode;
2851 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2852 if (err < 0) {
2853 goto out;
2855 v9_mode = stat_to_v9mode(&stbuf);
2856 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2857 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2858 /* Attempting to change the type */
2859 err = -EIO;
2860 goto out;
2862 err = v9fs_co_chmod(pdu, &fidp->path,
2863 v9mode_to_mode(v9stat.mode,
2864 &v9stat.extension));
2865 if (err < 0) {
2866 goto out;
2869 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2870 struct timespec times[2];
2871 if (v9stat.atime != -1) {
2872 times[0].tv_sec = v9stat.atime;
2873 times[0].tv_nsec = 0;
2874 } else {
2875 times[0].tv_nsec = UTIME_OMIT;
2877 if (v9stat.mtime != -1) {
2878 times[1].tv_sec = v9stat.mtime;
2879 times[1].tv_nsec = 0;
2880 } else {
2881 times[1].tv_nsec = UTIME_OMIT;
2883 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2884 if (err < 0) {
2885 goto out;
2888 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2889 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2890 if (err < 0) {
2891 goto out;
2894 if (v9stat.name.size != 0) {
2895 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2896 if (err < 0) {
2897 goto out;
2900 if (v9stat.length != -1) {
2901 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2902 if (err < 0) {
2903 goto out;
2906 err = offset;
2907 out:
2908 put_fid(pdu, fidp);
2909 out_nofid:
2910 v9fs_stat_free(&v9stat);
2911 pdu_complete(pdu, err);
2914 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2916 uint32_t f_type;
2917 uint32_t f_bsize;
2918 uint64_t f_blocks;
2919 uint64_t f_bfree;
2920 uint64_t f_bavail;
2921 uint64_t f_files;
2922 uint64_t f_ffree;
2923 uint64_t fsid_val;
2924 uint32_t f_namelen;
2925 size_t offset = 7;
2926 int32_t bsize_factor;
2929 * compute bsize factor based on host file system block size
2930 * and client msize
2932 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2933 if (!bsize_factor) {
2934 bsize_factor = 1;
2936 f_type = stbuf->f_type;
2937 f_bsize = stbuf->f_bsize;
2938 f_bsize *= bsize_factor;
2940 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2941 * adjust(divide) the number of blocks, free blocks and available
2942 * blocks by bsize factor
2944 f_blocks = stbuf->f_blocks/bsize_factor;
2945 f_bfree = stbuf->f_bfree/bsize_factor;
2946 f_bavail = stbuf->f_bavail/bsize_factor;
2947 f_files = stbuf->f_files;
2948 f_ffree = stbuf->f_ffree;
2949 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2950 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2951 f_namelen = stbuf->f_namelen;
2953 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2954 f_type, f_bsize, f_blocks, f_bfree,
2955 f_bavail, f_files, f_ffree,
2956 fsid_val, f_namelen);
2959 static void coroutine_fn v9fs_statfs(void *opaque)
2961 int32_t fid;
2962 ssize_t retval = 0;
2963 size_t offset = 7;
2964 V9fsFidState *fidp;
2965 struct statfs stbuf;
2966 V9fsPDU *pdu = opaque;
2967 V9fsState *s = pdu->s;
2969 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2970 if (retval < 0) {
2971 goto out_nofid;
2973 fidp = get_fid(pdu, fid);
2974 if (fidp == NULL) {
2975 retval = -ENOENT;
2976 goto out_nofid;
2978 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2979 if (retval < 0) {
2980 goto out;
2982 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2983 if (retval < 0) {
2984 goto out;
2986 retval += offset;
2987 out:
2988 put_fid(pdu, fidp);
2989 out_nofid:
2990 pdu_complete(pdu, retval);
2993 static void coroutine_fn v9fs_mknod(void *opaque)
2996 int mode;
2997 gid_t gid;
2998 int32_t fid;
2999 V9fsQID qid;
3000 int err = 0;
3001 int major, minor;
3002 size_t offset = 7;
3003 V9fsString name;
3004 struct stat stbuf;
3005 V9fsFidState *fidp;
3006 V9fsPDU *pdu = opaque;
3008 v9fs_string_init(&name);
3009 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3010 &major, &minor, &gid);
3011 if (err < 0) {
3012 goto out_nofid;
3014 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3016 if (name_is_illegal(name.data)) {
3017 err = -ENOENT;
3018 goto out_nofid;
3021 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3022 err = -EEXIST;
3023 goto out_nofid;
3026 fidp = get_fid(pdu, fid);
3027 if (fidp == NULL) {
3028 err = -ENOENT;
3029 goto out_nofid;
3031 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3032 makedev(major, minor), mode, &stbuf);
3033 if (err < 0) {
3034 goto out;
3036 stat_to_qid(&stbuf, &qid);
3037 err = pdu_marshal(pdu, offset, "Q", &qid);
3038 if (err < 0) {
3039 goto out;
3041 err += offset;
3042 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3043 qid.type, qid.version, qid.path);
3044 out:
3045 put_fid(pdu, fidp);
3046 out_nofid:
3047 pdu_complete(pdu, err);
3048 v9fs_string_free(&name);
3052 * Implement posix byte range locking code
3053 * Server side handling of locking code is very simple, because 9p server in
3054 * QEMU can handle only one client. And most of the lock handling
3055 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3056 * do any thing in * qemu 9p server side lock code path.
3057 * So when a TLOCK request comes, always return success
3059 static void coroutine_fn v9fs_lock(void *opaque)
3061 V9fsFlock flock;
3062 size_t offset = 7;
3063 struct stat stbuf;
3064 V9fsFidState *fidp;
3065 int32_t fid, err = 0;
3066 V9fsPDU *pdu = opaque;
3068 v9fs_string_init(&flock.client_id);
3069 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3070 &flock.flags, &flock.start, &flock.length,
3071 &flock.proc_id, &flock.client_id);
3072 if (err < 0) {
3073 goto out_nofid;
3075 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3076 flock.type, flock.start, flock.length);
3079 /* We support only block flag now (that too ignored currently) */
3080 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3081 err = -EINVAL;
3082 goto out_nofid;
3084 fidp = get_fid(pdu, fid);
3085 if (fidp == NULL) {
3086 err = -ENOENT;
3087 goto out_nofid;
3089 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3090 if (err < 0) {
3091 goto out;
3093 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3094 if (err < 0) {
3095 goto out;
3097 err += offset;
3098 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3099 out:
3100 put_fid(pdu, fidp);
3101 out_nofid:
3102 pdu_complete(pdu, err);
3103 v9fs_string_free(&flock.client_id);
3107 * When a TGETLOCK request comes, always return success because all lock
3108 * handling is done by client's VFS layer.
3110 static void coroutine_fn v9fs_getlock(void *opaque)
3112 size_t offset = 7;
3113 struct stat stbuf;
3114 V9fsFidState *fidp;
3115 V9fsGetlock glock;
3116 int32_t fid, err = 0;
3117 V9fsPDU *pdu = opaque;
3119 v9fs_string_init(&glock.client_id);
3120 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3121 &glock.start, &glock.length, &glock.proc_id,
3122 &glock.client_id);
3123 if (err < 0) {
3124 goto out_nofid;
3126 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3127 glock.type, glock.start, glock.length);
3129 fidp = get_fid(pdu, fid);
3130 if (fidp == NULL) {
3131 err = -ENOENT;
3132 goto out_nofid;
3134 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3135 if (err < 0) {
3136 goto out;
3138 glock.type = P9_LOCK_TYPE_UNLCK;
3139 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3140 glock.start, glock.length, glock.proc_id,
3141 &glock.client_id);
3142 if (err < 0) {
3143 goto out;
3145 err += offset;
3146 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3147 glock.length, glock.proc_id);
3148 out:
3149 put_fid(pdu, fidp);
3150 out_nofid:
3151 pdu_complete(pdu, err);
3152 v9fs_string_free(&glock.client_id);
3155 static void coroutine_fn v9fs_mkdir(void *opaque)
3157 V9fsPDU *pdu = opaque;
3158 size_t offset = 7;
3159 int32_t fid;
3160 struct stat stbuf;
3161 V9fsQID qid;
3162 V9fsString name;
3163 V9fsFidState *fidp;
3164 gid_t gid;
3165 int mode;
3166 int err = 0;
3168 v9fs_string_init(&name);
3169 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3170 if (err < 0) {
3171 goto out_nofid;
3173 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3175 if (name_is_illegal(name.data)) {
3176 err = -ENOENT;
3177 goto out_nofid;
3180 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3181 err = -EEXIST;
3182 goto out_nofid;
3185 fidp = get_fid(pdu, fid);
3186 if (fidp == NULL) {
3187 err = -ENOENT;
3188 goto out_nofid;
3190 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3191 if (err < 0) {
3192 goto out;
3194 stat_to_qid(&stbuf, &qid);
3195 err = pdu_marshal(pdu, offset, "Q", &qid);
3196 if (err < 0) {
3197 goto out;
3199 err += offset;
3200 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3201 qid.type, qid.version, qid.path, err);
3202 out:
3203 put_fid(pdu, fidp);
3204 out_nofid:
3205 pdu_complete(pdu, err);
3206 v9fs_string_free(&name);
3209 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3211 int64_t size;
3212 V9fsString name;
3213 ssize_t err = 0;
3214 size_t offset = 7;
3215 int32_t fid, newfid;
3216 V9fsFidState *file_fidp;
3217 V9fsFidState *xattr_fidp = NULL;
3218 V9fsPDU *pdu = opaque;
3219 V9fsState *s = pdu->s;
3221 v9fs_string_init(&name);
3222 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3223 if (err < 0) {
3224 goto out_nofid;
3226 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3228 file_fidp = get_fid(pdu, fid);
3229 if (file_fidp == NULL) {
3230 err = -ENOENT;
3231 goto out_nofid;
3233 xattr_fidp = alloc_fid(s, newfid);
3234 if (xattr_fidp == NULL) {
3235 err = -EINVAL;
3236 goto out;
3238 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3239 if (!v9fs_string_size(&name)) {
3241 * listxattr request. Get the size first
3243 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3244 if (size < 0) {
3245 err = size;
3246 clunk_fid(s, xattr_fidp->fid);
3247 goto out;
3250 * Read the xattr value
3252 xattr_fidp->fs.xattr.len = size;
3253 xattr_fidp->fid_type = P9_FID_XATTR;
3254 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3255 if (size) {
3256 xattr_fidp->fs.xattr.value = g_malloc0(size);
3257 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3258 xattr_fidp->fs.xattr.value,
3259 xattr_fidp->fs.xattr.len);
3260 if (err < 0) {
3261 clunk_fid(s, xattr_fidp->fid);
3262 goto out;
3265 err = pdu_marshal(pdu, offset, "q", size);
3266 if (err < 0) {
3267 goto out;
3269 err += offset;
3270 } else {
3272 * specific xattr fid. We check for xattr
3273 * presence also collect the xattr size
3275 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3276 &name, NULL, 0);
3277 if (size < 0) {
3278 err = size;
3279 clunk_fid(s, xattr_fidp->fid);
3280 goto out;
3283 * Read the xattr value
3285 xattr_fidp->fs.xattr.len = size;
3286 xattr_fidp->fid_type = P9_FID_XATTR;
3287 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3288 if (size) {
3289 xattr_fidp->fs.xattr.value = g_malloc0(size);
3290 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3291 &name, xattr_fidp->fs.xattr.value,
3292 xattr_fidp->fs.xattr.len);
3293 if (err < 0) {
3294 clunk_fid(s, xattr_fidp->fid);
3295 goto out;
3298 err = pdu_marshal(pdu, offset, "q", size);
3299 if (err < 0) {
3300 goto out;
3302 err += offset;
3304 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3305 out:
3306 put_fid(pdu, file_fidp);
3307 if (xattr_fidp) {
3308 put_fid(pdu, xattr_fidp);
3310 out_nofid:
3311 pdu_complete(pdu, err);
3312 v9fs_string_free(&name);
3315 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3317 int flags;
3318 int32_t fid;
3319 uint64_t size;
3320 ssize_t err = 0;
3321 V9fsString name;
3322 size_t offset = 7;
3323 V9fsFidState *file_fidp;
3324 V9fsFidState *xattr_fidp;
3325 V9fsPDU *pdu = opaque;
3327 v9fs_string_init(&name);
3328 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3329 if (err < 0) {
3330 goto out_nofid;
3332 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3334 if (size > XATTR_SIZE_MAX) {
3335 err = -E2BIG;
3336 goto out_nofid;
3339 file_fidp = get_fid(pdu, fid);
3340 if (file_fidp == NULL) {
3341 err = -EINVAL;
3342 goto out_nofid;
3344 if (file_fidp->fid_type != P9_FID_NONE) {
3345 err = -EINVAL;
3346 goto out_put_fid;
3349 /* Make the file fid point to xattr */
3350 xattr_fidp = file_fidp;
3351 xattr_fidp->fid_type = P9_FID_XATTR;
3352 xattr_fidp->fs.xattr.copied_len = 0;
3353 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3354 xattr_fidp->fs.xattr.len = size;
3355 xattr_fidp->fs.xattr.flags = flags;
3356 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3357 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3358 xattr_fidp->fs.xattr.value = g_malloc0(size);
3359 err = offset;
3360 out_put_fid:
3361 put_fid(pdu, file_fidp);
3362 out_nofid:
3363 pdu_complete(pdu, err);
3364 v9fs_string_free(&name);
3367 static void coroutine_fn v9fs_readlink(void *opaque)
3369 V9fsPDU *pdu = opaque;
3370 size_t offset = 7;
3371 V9fsString target;
3372 int32_t fid;
3373 int err = 0;
3374 V9fsFidState *fidp;
3376 err = pdu_unmarshal(pdu, offset, "d", &fid);
3377 if (err < 0) {
3378 goto out_nofid;
3380 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3381 fidp = get_fid(pdu, fid);
3382 if (fidp == NULL) {
3383 err = -ENOENT;
3384 goto out_nofid;
3387 v9fs_string_init(&target);
3388 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3389 if (err < 0) {
3390 goto out;
3392 err = pdu_marshal(pdu, offset, "s", &target);
3393 if (err < 0) {
3394 v9fs_string_free(&target);
3395 goto out;
3397 err += offset;
3398 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3399 v9fs_string_free(&target);
3400 out:
3401 put_fid(pdu, fidp);
3402 out_nofid:
3403 pdu_complete(pdu, err);
3406 static CoroutineEntry *pdu_co_handlers[] = {
3407 [P9_TREADDIR] = v9fs_readdir,
3408 [P9_TSTATFS] = v9fs_statfs,
3409 [P9_TGETATTR] = v9fs_getattr,
3410 [P9_TSETATTR] = v9fs_setattr,
3411 [P9_TXATTRWALK] = v9fs_xattrwalk,
3412 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3413 [P9_TMKNOD] = v9fs_mknod,
3414 [P9_TRENAME] = v9fs_rename,
3415 [P9_TLOCK] = v9fs_lock,
3416 [P9_TGETLOCK] = v9fs_getlock,
3417 [P9_TRENAMEAT] = v9fs_renameat,
3418 [P9_TREADLINK] = v9fs_readlink,
3419 [P9_TUNLINKAT] = v9fs_unlinkat,
3420 [P9_TMKDIR] = v9fs_mkdir,
3421 [P9_TVERSION] = v9fs_version,
3422 [P9_TLOPEN] = v9fs_open,
3423 [P9_TATTACH] = v9fs_attach,
3424 [P9_TSTAT] = v9fs_stat,
3425 [P9_TWALK] = v9fs_walk,
3426 [P9_TCLUNK] = v9fs_clunk,
3427 [P9_TFSYNC] = v9fs_fsync,
3428 [P9_TOPEN] = v9fs_open,
3429 [P9_TREAD] = v9fs_read,
3430 #if 0
3431 [P9_TAUTH] = v9fs_auth,
3432 #endif
3433 [P9_TFLUSH] = v9fs_flush,
3434 [P9_TLINK] = v9fs_link,
3435 [P9_TSYMLINK] = v9fs_symlink,
3436 [P9_TCREATE] = v9fs_create,
3437 [P9_TLCREATE] = v9fs_lcreate,
3438 [P9_TWRITE] = v9fs_write,
3439 [P9_TWSTAT] = v9fs_wstat,
3440 [P9_TREMOVE] = v9fs_remove,
3443 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3445 V9fsPDU *pdu = opaque;
3446 pdu_complete(pdu, -EOPNOTSUPP);
3449 static void coroutine_fn v9fs_fs_ro(void *opaque)
3451 V9fsPDU *pdu = opaque;
3452 pdu_complete(pdu, -EROFS);
3455 static inline bool is_read_only_op(V9fsPDU *pdu)
3457 switch (pdu->id) {
3458 case P9_TREADDIR:
3459 case P9_TSTATFS:
3460 case P9_TGETATTR:
3461 case P9_TXATTRWALK:
3462 case P9_TLOCK:
3463 case P9_TGETLOCK:
3464 case P9_TREADLINK:
3465 case P9_TVERSION:
3466 case P9_TLOPEN:
3467 case P9_TATTACH:
3468 case P9_TSTAT:
3469 case P9_TWALK:
3470 case P9_TCLUNK:
3471 case P9_TFSYNC:
3472 case P9_TOPEN:
3473 case P9_TREAD:
3474 case P9_TAUTH:
3475 case P9_TFLUSH:
3476 return 1;
3477 default:
3478 return 0;
3482 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
3484 Coroutine *co;
3485 CoroutineEntry *handler;
3486 V9fsState *s = pdu->s;
3488 pdu->size = le32_to_cpu(hdr->size_le);
3489 pdu->id = hdr->id;
3490 pdu->tag = le16_to_cpu(hdr->tag_le);
3492 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3493 (pdu_co_handlers[pdu->id] == NULL)) {
3494 handler = v9fs_op_not_supp;
3495 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3496 handler = v9fs_fs_ro;
3497 } else {
3498 handler = pdu_co_handlers[pdu->id];
3501 qemu_co_queue_init(&pdu->complete);
3502 co = qemu_coroutine_create(handler, pdu);
3503 qemu_coroutine_enter(co);
3506 /* Returns 0 on success, 1 on failure. */
3507 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
3508 Error **errp)
3510 int i, len;
3511 struct stat stat;
3512 FsDriverEntry *fse;
3513 V9fsPath path;
3514 int rc = 1;
3516 assert(!s->transport);
3517 s->transport = t;
3519 /* initialize pdu allocator */
3520 QLIST_INIT(&s->free_list);
3521 QLIST_INIT(&s->active_list);
3522 for (i = 0; i < MAX_REQ; i++) {
3523 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3524 s->pdus[i].s = s;
3525 s->pdus[i].idx = i;
3528 v9fs_path_init(&path);
3530 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3532 if (!fse) {
3533 /* We don't have a fsdev identified by fsdev_id */
3534 error_setg(errp, "9pfs device couldn't find fsdev with the "
3535 "id = %s",
3536 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3537 goto out;
3540 if (!s->fsconf.tag) {
3541 /* we haven't specified a mount_tag */
3542 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3543 s->fsconf.fsdev_id);
3544 goto out;
3547 s->ctx.export_flags = fse->export_flags;
3548 s->ctx.fs_root = g_strdup(fse->path);
3549 s->ctx.exops.get_st_gen = NULL;
3550 len = strlen(s->fsconf.tag);
3551 if (len > MAX_TAG_LEN - 1) {
3552 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3553 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3554 goto out;
3557 s->tag = g_strdup(s->fsconf.tag);
3558 s->ctx.uid = -1;
3560 s->ops = fse->ops;
3562 s->ctx.fmode = fse->fmode;
3563 s->ctx.dmode = fse->dmode;
3565 s->fid_list = NULL;
3566 qemu_co_rwlock_init(&s->rename_lock);
3568 if (s->ops->init(&s->ctx, errp) < 0) {
3569 error_prepend(errp, "cannot initialize fsdev '%s': ",
3570 s->fsconf.fsdev_id);
3571 goto out;
3575 * Check details of export path, We need to use fs driver
3576 * call back to do that. Since we are in the init path, we don't
3577 * use co-routines here.
3579 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3580 error_setg(errp,
3581 "error in converting name to path %s", strerror(errno));
3582 goto out;
3584 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3585 error_setg(errp, "share path %s does not exist", fse->path);
3586 goto out;
3587 } else if (!S_ISDIR(stat.st_mode)) {
3588 error_setg(errp, "share path %s is not a directory", fse->path);
3589 goto out;
3592 s->ctx.fst = &fse->fst;
3593 fsdev_throttle_init(s->ctx.fst);
3595 v9fs_path_free(&path);
3597 rc = 0;
3598 out:
3599 if (rc) {
3600 if (s->ops && s->ops->cleanup && s->ctx.private) {
3601 s->ops->cleanup(&s->ctx);
3603 g_free(s->tag);
3604 g_free(s->ctx.fs_root);
3605 v9fs_path_free(&path);
3607 return rc;
3610 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3612 if (s->ops->cleanup) {
3613 s->ops->cleanup(&s->ctx);
3615 fsdev_throttle_cleanup(s->ctx.fst);
3616 g_free(s->tag);
3617 g_free(s->ctx.fs_root);
3620 typedef struct VirtfsCoResetData {
3621 V9fsPDU pdu;
3622 bool done;
3623 } VirtfsCoResetData;
3625 static void coroutine_fn virtfs_co_reset(void *opaque)
3627 VirtfsCoResetData *data = opaque;
3629 virtfs_reset(&data->pdu);
3630 data->done = true;
3633 void v9fs_reset(V9fsState *s)
3635 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3636 Coroutine *co;
3638 while (!QLIST_EMPTY(&s->active_list)) {
3639 aio_poll(qemu_get_aio_context(), true);
3642 co = qemu_coroutine_create(virtfs_co_reset, &data);
3643 qemu_coroutine_enter(co);
3645 while (!data.done) {
3646 aio_poll(qemu_get_aio_context(), true);
3650 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3652 struct rlimit rlim;
3653 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3654 error_report("Failed to get the resource limit");
3655 exit(1);
3657 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3658 open_fd_rc = rlim.rlim_cur/2;