pc-dimm: pass in the machine and to the MemoryHotplugState
[qemu.git] / hw / 9pfs / 9p.c
blobd74302deeba58ea6f5127a9fff8921b7c58cfb70
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 *dst, const V9fsPath *src)
195 v9fs_path_free(dst);
196 dst->size = src->size;
197 dst->data = g_memdup(src->data, src->size);
200 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
201 const char *name, V9fsPath *path)
203 int err;
204 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
205 if (err < 0) {
206 err = -errno;
208 return err;
212 * Return TRUE if s1 is an ancestor of s2.
214 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
215 * As a special case, We treat s1 as ancestor of s2 if they are same!
217 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
219 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
220 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
221 return 1;
224 return 0;
227 static size_t v9fs_string_size(V9fsString *str)
229 return str->size;
233 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
234 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
236 int err = 1;
237 if (f->fid_type == P9_FID_FILE) {
238 if (f->fs.fd == -1) {
239 do {
240 err = v9fs_co_open(pdu, f, f->open_flags);
241 } while (err == -EINTR && !pdu->cancelled);
243 } else if (f->fid_type == P9_FID_DIR) {
244 if (f->fs.dir.stream == NULL) {
245 do {
246 err = v9fs_co_opendir(pdu, f);
247 } while (err == -EINTR && !pdu->cancelled);
250 return err;
253 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid)
255 int err;
256 V9fsFidState *f;
257 V9fsState *s = pdu->s;
259 for (f = s->fid_list; f; f = f->next) {
260 BUG_ON(f->clunked);
261 if (f->fid == fid) {
263 * Update the fid ref upfront so that
264 * we don't get reclaimed when we yield
265 * in open later.
267 f->ref++;
269 * check whether we need to reopen the
270 * file. We might have closed the fd
271 * while trying to free up some file
272 * descriptors.
274 err = v9fs_reopen_fid(pdu, f);
275 if (err < 0) {
276 f->ref--;
277 return NULL;
280 * Mark the fid as referenced so that the LRU
281 * reclaim won't close the file descriptor
283 f->flags |= FID_REFERENCED;
284 return f;
287 return NULL;
290 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
292 V9fsFidState *f;
294 for (f = s->fid_list; f; f = f->next) {
295 /* If fid is already there return NULL */
296 BUG_ON(f->clunked);
297 if (f->fid == fid) {
298 return NULL;
301 f = g_malloc0(sizeof(V9fsFidState));
302 f->fid = fid;
303 f->fid_type = P9_FID_NONE;
304 f->ref = 1;
306 * Mark the fid as referenced so that the LRU
307 * reclaim won't close the file descriptor
309 f->flags |= FID_REFERENCED;
310 f->next = s->fid_list;
311 s->fid_list = f;
313 v9fs_readdir_init(&f->fs.dir);
314 v9fs_readdir_init(&f->fs_reclaim.dir);
316 return f;
319 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
321 int retval = 0;
323 if (fidp->fs.xattr.xattrwalk_fid) {
324 /* getxattr/listxattr fid */
325 goto free_value;
328 * if this is fid for setxattr. clunk should
329 * result in setxattr localcall
331 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
332 /* clunk after partial write */
333 retval = -EINVAL;
334 goto free_out;
336 if (fidp->fs.xattr.len) {
337 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
338 fidp->fs.xattr.value,
339 fidp->fs.xattr.len,
340 fidp->fs.xattr.flags);
341 } else {
342 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
344 free_out:
345 v9fs_string_free(&fidp->fs.xattr.name);
346 free_value:
347 g_free(fidp->fs.xattr.value);
348 return retval;
351 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
353 int retval = 0;
355 if (fidp->fid_type == P9_FID_FILE) {
356 /* If we reclaimed the fd no need to close */
357 if (fidp->fs.fd != -1) {
358 retval = v9fs_co_close(pdu, &fidp->fs);
360 } else if (fidp->fid_type == P9_FID_DIR) {
361 if (fidp->fs.dir.stream != NULL) {
362 retval = v9fs_co_closedir(pdu, &fidp->fs);
364 } else if (fidp->fid_type == P9_FID_XATTR) {
365 retval = v9fs_xattr_fid_clunk(pdu, fidp);
367 v9fs_path_free(&fidp->path);
368 g_free(fidp);
369 return retval;
372 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
374 BUG_ON(!fidp->ref);
375 fidp->ref--;
377 * Don't free the fid if it is in reclaim list
379 if (!fidp->ref && fidp->clunked) {
380 if (fidp->fid == pdu->s->root_fid) {
382 * if the clunked fid is root fid then we
383 * have unmounted the fs on the client side.
384 * delete the migration blocker. Ideally, this
385 * should be hooked to transport close notification
387 if (pdu->s->migration_blocker) {
388 migrate_del_blocker(pdu->s->migration_blocker);
389 error_free(pdu->s->migration_blocker);
390 pdu->s->migration_blocker = NULL;
393 return free_fid(pdu, fidp);
395 return 0;
398 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
400 V9fsFidState **fidpp, *fidp;
402 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
403 if ((*fidpp)->fid == fid) {
404 break;
407 if (*fidpp == NULL) {
408 return NULL;
410 fidp = *fidpp;
411 *fidpp = fidp->next;
412 fidp->clunked = 1;
413 return fidp;
416 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu)
418 int reclaim_count = 0;
419 V9fsState *s = pdu->s;
420 V9fsFidState *f, *reclaim_list = NULL;
422 for (f = s->fid_list; f; f = f->next) {
424 * Unlink fids cannot be reclaimed. Check
425 * for them and skip them. Also skip fids
426 * currently being operated on.
428 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
429 continue;
432 * if it is a recently referenced fid
433 * we leave the fid untouched and clear the
434 * reference bit. We come back to it later
435 * in the next iteration. (a simple LRU without
436 * moving list elements around)
438 if (f->flags & FID_REFERENCED) {
439 f->flags &= ~FID_REFERENCED;
440 continue;
443 * Add fids to reclaim list.
445 if (f->fid_type == P9_FID_FILE) {
446 if (f->fs.fd != -1) {
448 * Up the reference count so that
449 * a clunk request won't free this fid
451 f->ref++;
452 f->rclm_lst = reclaim_list;
453 reclaim_list = f;
454 f->fs_reclaim.fd = f->fs.fd;
455 f->fs.fd = -1;
456 reclaim_count++;
458 } else if (f->fid_type == P9_FID_DIR) {
459 if (f->fs.dir.stream != NULL) {
461 * Up the reference count so that
462 * a clunk request won't free this fid
464 f->ref++;
465 f->rclm_lst = reclaim_list;
466 reclaim_list = f;
467 f->fs_reclaim.dir.stream = f->fs.dir.stream;
468 f->fs.dir.stream = NULL;
469 reclaim_count++;
472 if (reclaim_count >= open_fd_rc) {
473 break;
477 * Now close the fid in reclaim list. Free them if they
478 * are already clunked.
480 while (reclaim_list) {
481 f = reclaim_list;
482 reclaim_list = f->rclm_lst;
483 if (f->fid_type == P9_FID_FILE) {
484 v9fs_co_close(pdu, &f->fs_reclaim);
485 } else if (f->fid_type == P9_FID_DIR) {
486 v9fs_co_closedir(pdu, &f->fs_reclaim);
488 f->rclm_lst = NULL;
490 * Now drop the fid reference, free it
491 * if clunked.
493 put_fid(pdu, f);
497 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
499 int err;
500 V9fsState *s = pdu->s;
501 V9fsFidState *fidp, head_fid;
503 head_fid.next = s->fid_list;
504 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
505 if (fidp->path.size != path->size) {
506 continue;
508 if (!memcmp(fidp->path.data, path->data, path->size)) {
509 /* Mark the fid non reclaimable. */
510 fidp->flags |= FID_NON_RECLAIMABLE;
512 /* reopen the file/dir if already closed */
513 err = v9fs_reopen_fid(pdu, fidp);
514 if (err < 0) {
515 return err;
518 * Go back to head of fid list because
519 * the list could have got updated when
520 * switched to the worker thread
522 if (err == 0) {
523 fidp = &head_fid;
527 return 0;
530 static void coroutine_fn virtfs_reset(V9fsPDU *pdu)
532 V9fsState *s = pdu->s;
533 V9fsFidState *fidp;
535 /* Free all fids */
536 while (s->fid_list) {
537 /* Get fid */
538 fidp = s->fid_list;
539 fidp->ref++;
541 /* Clunk fid */
542 s->fid_list = fidp->next;
543 fidp->clunked = 1;
545 put_fid(pdu, fidp);
549 #define P9_QID_TYPE_DIR 0x80
550 #define P9_QID_TYPE_SYMLINK 0x02
552 #define P9_STAT_MODE_DIR 0x80000000
553 #define P9_STAT_MODE_APPEND 0x40000000
554 #define P9_STAT_MODE_EXCL 0x20000000
555 #define P9_STAT_MODE_MOUNT 0x10000000
556 #define P9_STAT_MODE_AUTH 0x08000000
557 #define P9_STAT_MODE_TMP 0x04000000
558 #define P9_STAT_MODE_SYMLINK 0x02000000
559 #define P9_STAT_MODE_LINK 0x01000000
560 #define P9_STAT_MODE_DEVICE 0x00800000
561 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
562 #define P9_STAT_MODE_SOCKET 0x00100000
563 #define P9_STAT_MODE_SETUID 0x00080000
564 #define P9_STAT_MODE_SETGID 0x00040000
565 #define P9_STAT_MODE_SETVTX 0x00010000
567 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
568 P9_STAT_MODE_SYMLINK | \
569 P9_STAT_MODE_LINK | \
570 P9_STAT_MODE_DEVICE | \
571 P9_STAT_MODE_NAMED_PIPE | \
572 P9_STAT_MODE_SOCKET)
574 /* This is the algorithm from ufs in spfs */
575 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
577 size_t size;
579 memset(&qidp->path, 0, sizeof(qidp->path));
580 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
581 memcpy(&qidp->path, &stbuf->st_ino, size);
582 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
583 qidp->type = 0;
584 if (S_ISDIR(stbuf->st_mode)) {
585 qidp->type |= P9_QID_TYPE_DIR;
587 if (S_ISLNK(stbuf->st_mode)) {
588 qidp->type |= P9_QID_TYPE_SYMLINK;
592 static int coroutine_fn fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp,
593 V9fsQID *qidp)
595 struct stat stbuf;
596 int err;
598 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
599 if (err < 0) {
600 return err;
602 stat_to_qid(&stbuf, qidp);
603 return 0;
606 V9fsPDU *pdu_alloc(V9fsState *s)
608 V9fsPDU *pdu = NULL;
610 if (!QLIST_EMPTY(&s->free_list)) {
611 pdu = QLIST_FIRST(&s->free_list);
612 QLIST_REMOVE(pdu, next);
613 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
615 return pdu;
618 void pdu_free(V9fsPDU *pdu)
620 V9fsState *s = pdu->s;
622 g_assert(!pdu->cancelled);
623 QLIST_REMOVE(pdu, next);
624 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
627 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len)
629 int8_t id = pdu->id + 1; /* Response */
630 V9fsState *s = pdu->s;
631 int ret;
634 * The 9p spec requires that successfully cancelled pdus receive no reply.
635 * Sending a reply would confuse clients because they would
636 * assume that any EINTR is the actual result of the operation,
637 * rather than a consequence of the cancellation. However, if
638 * the operation completed (succesfully or with an error other
639 * than caused be cancellation), we do send out that reply, both
640 * for efficiency and to avoid confusing the rest of the state machine
641 * that assumes passing a non-error here will mean a successful
642 * transmission of the reply.
644 bool discard = pdu->cancelled && len == -EINTR;
645 if (discard) {
646 trace_v9fs_rcancel(pdu->tag, pdu->id);
647 pdu->size = 0;
648 goto out_notify;
651 if (len < 0) {
652 int err = -len;
653 len = 7;
655 if (s->proto_version != V9FS_PROTO_2000L) {
656 V9fsString str;
658 str.data = strerror(err);
659 str.size = strlen(str.data);
661 ret = pdu_marshal(pdu, len, "s", &str);
662 if (ret < 0) {
663 goto out_notify;
665 len += ret;
666 id = P9_RERROR;
669 ret = pdu_marshal(pdu, len, "d", err);
670 if (ret < 0) {
671 goto out_notify;
673 len += ret;
675 if (s->proto_version == V9FS_PROTO_2000L) {
676 id = P9_RLERROR;
678 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
681 /* fill out the header */
682 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) {
683 goto out_notify;
686 /* keep these in sync */
687 pdu->size = len;
688 pdu->id = id;
690 out_notify:
691 pdu->s->transport->push_and_notify(pdu);
693 /* Now wakeup anybody waiting in flush for this request */
694 if (!qemu_co_queue_next(&pdu->complete)) {
695 pdu_free(pdu);
699 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
701 mode_t ret;
703 ret = mode & 0777;
704 if (mode & P9_STAT_MODE_DIR) {
705 ret |= S_IFDIR;
708 if (mode & P9_STAT_MODE_SYMLINK) {
709 ret |= S_IFLNK;
711 if (mode & P9_STAT_MODE_SOCKET) {
712 ret |= S_IFSOCK;
714 if (mode & P9_STAT_MODE_NAMED_PIPE) {
715 ret |= S_IFIFO;
717 if (mode & P9_STAT_MODE_DEVICE) {
718 if (extension->size && extension->data[0] == 'c') {
719 ret |= S_IFCHR;
720 } else {
721 ret |= S_IFBLK;
725 if (!(ret&~0777)) {
726 ret |= S_IFREG;
729 if (mode & P9_STAT_MODE_SETUID) {
730 ret |= S_ISUID;
732 if (mode & P9_STAT_MODE_SETGID) {
733 ret |= S_ISGID;
735 if (mode & P9_STAT_MODE_SETVTX) {
736 ret |= S_ISVTX;
739 return ret;
742 static int donttouch_stat(V9fsStat *stat)
744 if (stat->type == -1 &&
745 stat->dev == -1 &&
746 stat->qid.type == -1 &&
747 stat->qid.version == -1 &&
748 stat->qid.path == -1 &&
749 stat->mode == -1 &&
750 stat->atime == -1 &&
751 stat->mtime == -1 &&
752 stat->length == -1 &&
753 !stat->name.size &&
754 !stat->uid.size &&
755 !stat->gid.size &&
756 !stat->muid.size &&
757 stat->n_uid == -1 &&
758 stat->n_gid == -1 &&
759 stat->n_muid == -1) {
760 return 1;
763 return 0;
766 static void v9fs_stat_init(V9fsStat *stat)
768 v9fs_string_init(&stat->name);
769 v9fs_string_init(&stat->uid);
770 v9fs_string_init(&stat->gid);
771 v9fs_string_init(&stat->muid);
772 v9fs_string_init(&stat->extension);
775 static void v9fs_stat_free(V9fsStat *stat)
777 v9fs_string_free(&stat->name);
778 v9fs_string_free(&stat->uid);
779 v9fs_string_free(&stat->gid);
780 v9fs_string_free(&stat->muid);
781 v9fs_string_free(&stat->extension);
784 static uint32_t stat_to_v9mode(const struct stat *stbuf)
786 uint32_t mode;
788 mode = stbuf->st_mode & 0777;
789 if (S_ISDIR(stbuf->st_mode)) {
790 mode |= P9_STAT_MODE_DIR;
793 if (S_ISLNK(stbuf->st_mode)) {
794 mode |= P9_STAT_MODE_SYMLINK;
797 if (S_ISSOCK(stbuf->st_mode)) {
798 mode |= P9_STAT_MODE_SOCKET;
801 if (S_ISFIFO(stbuf->st_mode)) {
802 mode |= P9_STAT_MODE_NAMED_PIPE;
805 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
806 mode |= P9_STAT_MODE_DEVICE;
809 if (stbuf->st_mode & S_ISUID) {
810 mode |= P9_STAT_MODE_SETUID;
813 if (stbuf->st_mode & S_ISGID) {
814 mode |= P9_STAT_MODE_SETGID;
817 if (stbuf->st_mode & S_ISVTX) {
818 mode |= P9_STAT_MODE_SETVTX;
821 return mode;
824 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path,
825 const char *basename,
826 const struct stat *stbuf,
827 V9fsStat *v9stat)
829 int err;
831 memset(v9stat, 0, sizeof(*v9stat));
833 stat_to_qid(stbuf, &v9stat->qid);
834 v9stat->mode = stat_to_v9mode(stbuf);
835 v9stat->atime = stbuf->st_atime;
836 v9stat->mtime = stbuf->st_mtime;
837 v9stat->length = stbuf->st_size;
839 v9fs_string_free(&v9stat->uid);
840 v9fs_string_free(&v9stat->gid);
841 v9fs_string_free(&v9stat->muid);
843 v9stat->n_uid = stbuf->st_uid;
844 v9stat->n_gid = stbuf->st_gid;
845 v9stat->n_muid = 0;
847 v9fs_string_free(&v9stat->extension);
849 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
850 err = v9fs_co_readlink(pdu, path, &v9stat->extension);
851 if (err < 0) {
852 return err;
854 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
855 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
856 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
857 major(stbuf->st_rdev), minor(stbuf->st_rdev));
858 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
859 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
860 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
863 v9fs_string_sprintf(&v9stat->name, "%s", basename);
865 v9stat->size = 61 +
866 v9fs_string_size(&v9stat->name) +
867 v9fs_string_size(&v9stat->uid) +
868 v9fs_string_size(&v9stat->gid) +
869 v9fs_string_size(&v9stat->muid) +
870 v9fs_string_size(&v9stat->extension);
871 return 0;
874 #define P9_STATS_MODE 0x00000001ULL
875 #define P9_STATS_NLINK 0x00000002ULL
876 #define P9_STATS_UID 0x00000004ULL
877 #define P9_STATS_GID 0x00000008ULL
878 #define P9_STATS_RDEV 0x00000010ULL
879 #define P9_STATS_ATIME 0x00000020ULL
880 #define P9_STATS_MTIME 0x00000040ULL
881 #define P9_STATS_CTIME 0x00000080ULL
882 #define P9_STATS_INO 0x00000100ULL
883 #define P9_STATS_SIZE 0x00000200ULL
884 #define P9_STATS_BLOCKS 0x00000400ULL
886 #define P9_STATS_BTIME 0x00000800ULL
887 #define P9_STATS_GEN 0x00001000ULL
888 #define P9_STATS_DATA_VERSION 0x00002000ULL
890 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
891 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
894 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
895 V9fsStatDotl *v9lstat)
897 memset(v9lstat, 0, sizeof(*v9lstat));
899 v9lstat->st_mode = stbuf->st_mode;
900 v9lstat->st_nlink = stbuf->st_nlink;
901 v9lstat->st_uid = stbuf->st_uid;
902 v9lstat->st_gid = stbuf->st_gid;
903 v9lstat->st_rdev = stbuf->st_rdev;
904 v9lstat->st_size = stbuf->st_size;
905 v9lstat->st_blksize = stbuf->st_blksize;
906 v9lstat->st_blocks = stbuf->st_blocks;
907 v9lstat->st_atime_sec = stbuf->st_atime;
908 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
909 v9lstat->st_mtime_sec = stbuf->st_mtime;
910 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
911 v9lstat->st_ctime_sec = stbuf->st_ctime;
912 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
913 /* Currently we only support BASIC fields in stat */
914 v9lstat->st_result_mask = P9_STATS_BASIC;
916 stat_to_qid(stbuf, &v9lstat->qid);
919 static void print_sg(struct iovec *sg, int cnt)
921 int i;
923 printf("sg[%d]: {", cnt);
924 for (i = 0; i < cnt; i++) {
925 if (i) {
926 printf(", ");
928 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
930 printf("}\n");
933 /* Will call this only for path name based fid */
934 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
936 V9fsPath str;
937 v9fs_path_init(&str);
938 v9fs_path_copy(&str, dst);
939 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len);
940 v9fs_path_free(&str);
943 static inline bool is_ro_export(FsContext *ctx)
945 return ctx->export_flags & V9FS_RDONLY;
948 static void coroutine_fn v9fs_version(void *opaque)
950 ssize_t err;
951 V9fsPDU *pdu = opaque;
952 V9fsState *s = pdu->s;
953 V9fsString version;
954 size_t offset = 7;
956 v9fs_string_init(&version);
957 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
958 if (err < 0) {
959 goto out;
961 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
963 virtfs_reset(pdu);
965 if (!strcmp(version.data, "9P2000.u")) {
966 s->proto_version = V9FS_PROTO_2000U;
967 } else if (!strcmp(version.data, "9P2000.L")) {
968 s->proto_version = V9FS_PROTO_2000L;
969 } else {
970 v9fs_string_sprintf(&version, "unknown");
973 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
974 if (err < 0) {
975 goto out;
977 err += offset;
978 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
979 out:
980 pdu_complete(pdu, err);
981 v9fs_string_free(&version);
984 static void coroutine_fn v9fs_attach(void *opaque)
986 V9fsPDU *pdu = opaque;
987 V9fsState *s = pdu->s;
988 int32_t fid, afid, n_uname;
989 V9fsString uname, aname;
990 V9fsFidState *fidp;
991 size_t offset = 7;
992 V9fsQID qid;
993 ssize_t err;
994 Error *local_err = NULL;
996 v9fs_string_init(&uname);
997 v9fs_string_init(&aname);
998 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
999 &afid, &uname, &aname, &n_uname);
1000 if (err < 0) {
1001 goto out_nofid;
1003 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
1005 fidp = alloc_fid(s, fid);
1006 if (fidp == NULL) {
1007 err = -EINVAL;
1008 goto out_nofid;
1010 fidp->uid = n_uname;
1011 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
1012 if (err < 0) {
1013 err = -EINVAL;
1014 clunk_fid(s, fid);
1015 goto out;
1017 err = fid_to_qid(pdu, fidp, &qid);
1018 if (err < 0) {
1019 err = -EINVAL;
1020 clunk_fid(s, fid);
1021 goto out;
1025 * disable migration if we haven't done already.
1026 * attach could get called multiple times for the same export.
1028 if (!s->migration_blocker) {
1029 error_setg(&s->migration_blocker,
1030 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1031 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1032 err = migrate_add_blocker(s->migration_blocker, &local_err);
1033 if (local_err) {
1034 error_free(local_err);
1035 error_free(s->migration_blocker);
1036 s->migration_blocker = NULL;
1037 clunk_fid(s, fid);
1038 goto out;
1040 s->root_fid = fid;
1043 err = pdu_marshal(pdu, offset, "Q", &qid);
1044 if (err < 0) {
1045 clunk_fid(s, fid);
1046 goto out;
1048 err += offset;
1050 memcpy(&s->root_qid, &qid, sizeof(qid));
1051 trace_v9fs_attach_return(pdu->tag, pdu->id,
1052 qid.type, qid.version, qid.path);
1053 out:
1054 put_fid(pdu, fidp);
1055 out_nofid:
1056 pdu_complete(pdu, err);
1057 v9fs_string_free(&uname);
1058 v9fs_string_free(&aname);
1061 static void coroutine_fn v9fs_stat(void *opaque)
1063 int32_t fid;
1064 V9fsStat v9stat;
1065 ssize_t err = 0;
1066 size_t offset = 7;
1067 struct stat stbuf;
1068 V9fsFidState *fidp;
1069 V9fsPDU *pdu = opaque;
1070 char *basename;
1072 err = pdu_unmarshal(pdu, offset, "d", &fid);
1073 if (err < 0) {
1074 goto out_nofid;
1076 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1078 fidp = get_fid(pdu, fid);
1079 if (fidp == NULL) {
1080 err = -ENOENT;
1081 goto out_nofid;
1083 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1084 if (err < 0) {
1085 goto out;
1087 basename = g_path_get_basename(fidp->path.data);
1088 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat);
1089 g_free(basename);
1090 if (err < 0) {
1091 goto out;
1093 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1094 if (err < 0) {
1095 v9fs_stat_free(&v9stat);
1096 goto out;
1098 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1099 v9stat.atime, v9stat.mtime, v9stat.length);
1100 err += offset;
1101 v9fs_stat_free(&v9stat);
1102 out:
1103 put_fid(pdu, fidp);
1104 out_nofid:
1105 pdu_complete(pdu, err);
1108 static void coroutine_fn v9fs_getattr(void *opaque)
1110 int32_t fid;
1111 size_t offset = 7;
1112 ssize_t retval = 0;
1113 struct stat stbuf;
1114 V9fsFidState *fidp;
1115 uint64_t request_mask;
1116 V9fsStatDotl v9stat_dotl;
1117 V9fsPDU *pdu = opaque;
1118 V9fsState *s = pdu->s;
1120 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1121 if (retval < 0) {
1122 goto out_nofid;
1124 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1126 fidp = get_fid(pdu, fid);
1127 if (fidp == NULL) {
1128 retval = -ENOENT;
1129 goto out_nofid;
1132 * Currently we only support BASIC fields in stat, so there is no
1133 * need to look at request_mask.
1135 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1136 if (retval < 0) {
1137 goto out;
1139 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1141 /* fill st_gen if requested and supported by underlying fs */
1142 if (request_mask & P9_STATS_GEN) {
1143 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1144 switch (retval) {
1145 case 0:
1146 /* we have valid st_gen: update result mask */
1147 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1148 break;
1149 case -EINTR:
1150 /* request cancelled, e.g. by Tflush */
1151 goto out;
1152 default:
1153 /* failed to get st_gen: not fatal, ignore */
1154 break;
1157 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1158 if (retval < 0) {
1159 goto out;
1161 retval += offset;
1162 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1163 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1164 v9stat_dotl.st_gid);
1165 out:
1166 put_fid(pdu, fidp);
1167 out_nofid:
1168 pdu_complete(pdu, retval);
1171 /* Attribute flags */
1172 #define P9_ATTR_MODE (1 << 0)
1173 #define P9_ATTR_UID (1 << 1)
1174 #define P9_ATTR_GID (1 << 2)
1175 #define P9_ATTR_SIZE (1 << 3)
1176 #define P9_ATTR_ATIME (1 << 4)
1177 #define P9_ATTR_MTIME (1 << 5)
1178 #define P9_ATTR_CTIME (1 << 6)
1179 #define P9_ATTR_ATIME_SET (1 << 7)
1180 #define P9_ATTR_MTIME_SET (1 << 8)
1182 #define P9_ATTR_MASK 127
1184 static void coroutine_fn v9fs_setattr(void *opaque)
1186 int err = 0;
1187 int32_t fid;
1188 V9fsFidState *fidp;
1189 size_t offset = 7;
1190 V9fsIattr v9iattr;
1191 V9fsPDU *pdu = opaque;
1193 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1194 if (err < 0) {
1195 goto out_nofid;
1198 trace_v9fs_setattr(pdu->tag, pdu->id, fid,
1199 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid,
1200 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec);
1202 fidp = get_fid(pdu, fid);
1203 if (fidp == NULL) {
1204 err = -EINVAL;
1205 goto out_nofid;
1207 if (v9iattr.valid & P9_ATTR_MODE) {
1208 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1209 if (err < 0) {
1210 goto out;
1213 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1214 struct timespec times[2];
1215 if (v9iattr.valid & P9_ATTR_ATIME) {
1216 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1217 times[0].tv_sec = v9iattr.atime_sec;
1218 times[0].tv_nsec = v9iattr.atime_nsec;
1219 } else {
1220 times[0].tv_nsec = UTIME_NOW;
1222 } else {
1223 times[0].tv_nsec = UTIME_OMIT;
1225 if (v9iattr.valid & P9_ATTR_MTIME) {
1226 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1227 times[1].tv_sec = v9iattr.mtime_sec;
1228 times[1].tv_nsec = v9iattr.mtime_nsec;
1229 } else {
1230 times[1].tv_nsec = UTIME_NOW;
1232 } else {
1233 times[1].tv_nsec = UTIME_OMIT;
1235 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1236 if (err < 0) {
1237 goto out;
1241 * If the only valid entry in iattr is ctime we can call
1242 * chown(-1,-1) to update the ctime of the file
1244 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1245 ((v9iattr.valid & P9_ATTR_CTIME)
1246 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1247 if (!(v9iattr.valid & P9_ATTR_UID)) {
1248 v9iattr.uid = -1;
1250 if (!(v9iattr.valid & P9_ATTR_GID)) {
1251 v9iattr.gid = -1;
1253 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1254 v9iattr.gid);
1255 if (err < 0) {
1256 goto out;
1259 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1260 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1261 if (err < 0) {
1262 goto out;
1265 err = offset;
1266 trace_v9fs_setattr_return(pdu->tag, pdu->id);
1267 out:
1268 put_fid(pdu, fidp);
1269 out_nofid:
1270 pdu_complete(pdu, err);
1273 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1275 int i;
1276 ssize_t err;
1277 size_t offset = 7;
1279 err = pdu_marshal(pdu, offset, "w", nwnames);
1280 if (err < 0) {
1281 return err;
1283 offset += err;
1284 for (i = 0; i < nwnames; i++) {
1285 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1286 if (err < 0) {
1287 return err;
1289 offset += err;
1291 return offset;
1294 static bool name_is_illegal(const char *name)
1296 return !*name || strchr(name, '/') != NULL;
1299 static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2)
1301 return
1302 qid1->type != qid2->type ||
1303 qid1->version != qid2->version ||
1304 qid1->path != qid2->path;
1307 static void coroutine_fn v9fs_walk(void *opaque)
1309 int name_idx;
1310 V9fsQID *qids = NULL;
1311 int i, err = 0;
1312 V9fsPath dpath, path;
1313 uint16_t nwnames;
1314 struct stat stbuf;
1315 size_t offset = 7;
1316 int32_t fid, newfid;
1317 V9fsString *wnames = NULL;
1318 V9fsFidState *fidp;
1319 V9fsFidState *newfidp = NULL;
1320 V9fsPDU *pdu = opaque;
1321 V9fsState *s = pdu->s;
1322 V9fsQID qid;
1324 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1325 if (err < 0) {
1326 pdu_complete(pdu, err);
1327 return ;
1329 offset += err;
1331 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1333 if (nwnames && nwnames <= P9_MAXWELEM) {
1334 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1335 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1336 for (i = 0; i < nwnames; i++) {
1337 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1338 if (err < 0) {
1339 goto out_nofid;
1341 if (name_is_illegal(wnames[i].data)) {
1342 err = -ENOENT;
1343 goto out_nofid;
1345 offset += err;
1347 } else if (nwnames > P9_MAXWELEM) {
1348 err = -EINVAL;
1349 goto out_nofid;
1351 fidp = get_fid(pdu, fid);
1352 if (fidp == NULL) {
1353 err = -ENOENT;
1354 goto out_nofid;
1357 v9fs_path_init(&dpath);
1358 v9fs_path_init(&path);
1360 err = fid_to_qid(pdu, fidp, &qid);
1361 if (err < 0) {
1362 goto out;
1366 * Both dpath and path initially poin to fidp.
1367 * Needed to handle request with nwnames == 0
1369 v9fs_path_copy(&dpath, &fidp->path);
1370 v9fs_path_copy(&path, &fidp->path);
1371 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1372 if (not_same_qid(&pdu->s->root_qid, &qid) ||
1373 strcmp("..", wnames[name_idx].data)) {
1374 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data,
1375 &path);
1376 if (err < 0) {
1377 goto out;
1380 err = v9fs_co_lstat(pdu, &path, &stbuf);
1381 if (err < 0) {
1382 goto out;
1384 stat_to_qid(&stbuf, &qid);
1385 v9fs_path_copy(&dpath, &path);
1387 memcpy(&qids[name_idx], &qid, sizeof(qid));
1389 if (fid == newfid) {
1390 if (fidp->fid_type != P9_FID_NONE) {
1391 err = -EINVAL;
1392 goto out;
1394 v9fs_path_copy(&fidp->path, &path);
1395 } else {
1396 newfidp = alloc_fid(s, newfid);
1397 if (newfidp == NULL) {
1398 err = -EINVAL;
1399 goto out;
1401 newfidp->uid = fidp->uid;
1402 v9fs_path_copy(&newfidp->path, &path);
1404 err = v9fs_walk_marshal(pdu, nwnames, qids);
1405 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1406 out:
1407 put_fid(pdu, fidp);
1408 if (newfidp) {
1409 put_fid(pdu, newfidp);
1411 v9fs_path_free(&dpath);
1412 v9fs_path_free(&path);
1413 out_nofid:
1414 pdu_complete(pdu, err);
1415 if (nwnames && nwnames <= P9_MAXWELEM) {
1416 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1417 v9fs_string_free(&wnames[name_idx]);
1419 g_free(wnames);
1420 g_free(qids);
1424 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path)
1426 struct statfs stbuf;
1427 int32_t iounit = 0;
1428 V9fsState *s = pdu->s;
1431 * iounit should be multiples of f_bsize (host filesystem block size
1432 * and as well as less than (client msize - P9_IOHDRSZ))
1434 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1435 iounit = stbuf.f_bsize;
1436 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1438 if (!iounit) {
1439 iounit = s->msize - P9_IOHDRSZ;
1441 return iounit;
1444 static void coroutine_fn v9fs_open(void *opaque)
1446 int flags;
1447 int32_t fid;
1448 int32_t mode;
1449 V9fsQID qid;
1450 int iounit = 0;
1451 ssize_t err = 0;
1452 size_t offset = 7;
1453 struct stat stbuf;
1454 V9fsFidState *fidp;
1455 V9fsPDU *pdu = opaque;
1456 V9fsState *s = pdu->s;
1458 if (s->proto_version == V9FS_PROTO_2000L) {
1459 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1460 } else {
1461 uint8_t modebyte;
1462 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1463 mode = modebyte;
1465 if (err < 0) {
1466 goto out_nofid;
1468 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1470 fidp = get_fid(pdu, fid);
1471 if (fidp == NULL) {
1472 err = -ENOENT;
1473 goto out_nofid;
1475 if (fidp->fid_type != P9_FID_NONE) {
1476 err = -EINVAL;
1477 goto out;
1480 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1481 if (err < 0) {
1482 goto out;
1484 stat_to_qid(&stbuf, &qid);
1485 if (S_ISDIR(stbuf.st_mode)) {
1486 err = v9fs_co_opendir(pdu, fidp);
1487 if (err < 0) {
1488 goto out;
1490 fidp->fid_type = P9_FID_DIR;
1491 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1492 if (err < 0) {
1493 goto out;
1495 err += offset;
1496 } else {
1497 if (s->proto_version == V9FS_PROTO_2000L) {
1498 flags = get_dotl_openflags(s, mode);
1499 } else {
1500 flags = omode_to_uflags(mode);
1502 if (is_ro_export(&s->ctx)) {
1503 if (mode & O_WRONLY || mode & O_RDWR ||
1504 mode & O_APPEND || mode & O_TRUNC) {
1505 err = -EROFS;
1506 goto out;
1509 err = v9fs_co_open(pdu, fidp, flags);
1510 if (err < 0) {
1511 goto out;
1513 fidp->fid_type = P9_FID_FILE;
1514 fidp->open_flags = flags;
1515 if (flags & O_EXCL) {
1517 * We let the host file system do O_EXCL check
1518 * We should not reclaim such fd
1520 fidp->flags |= FID_NON_RECLAIMABLE;
1522 iounit = get_iounit(pdu, &fidp->path);
1523 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1524 if (err < 0) {
1525 goto out;
1527 err += offset;
1529 trace_v9fs_open_return(pdu->tag, pdu->id,
1530 qid.type, qid.version, qid.path, iounit);
1531 out:
1532 put_fid(pdu, fidp);
1533 out_nofid:
1534 pdu_complete(pdu, err);
1537 static void coroutine_fn v9fs_lcreate(void *opaque)
1539 int32_t dfid, flags, mode;
1540 gid_t gid;
1541 ssize_t err = 0;
1542 ssize_t offset = 7;
1543 V9fsString name;
1544 V9fsFidState *fidp;
1545 struct stat stbuf;
1546 V9fsQID qid;
1547 int32_t iounit;
1548 V9fsPDU *pdu = opaque;
1550 v9fs_string_init(&name);
1551 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1552 &name, &flags, &mode, &gid);
1553 if (err < 0) {
1554 goto out_nofid;
1556 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1558 if (name_is_illegal(name.data)) {
1559 err = -ENOENT;
1560 goto out_nofid;
1563 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
1564 err = -EEXIST;
1565 goto out_nofid;
1568 fidp = get_fid(pdu, dfid);
1569 if (fidp == NULL) {
1570 err = -ENOENT;
1571 goto out_nofid;
1573 if (fidp->fid_type != P9_FID_NONE) {
1574 err = -EINVAL;
1575 goto out;
1578 flags = get_dotl_openflags(pdu->s, flags);
1579 err = v9fs_co_open2(pdu, fidp, &name, gid,
1580 flags | O_CREAT, mode, &stbuf);
1581 if (err < 0) {
1582 goto out;
1584 fidp->fid_type = P9_FID_FILE;
1585 fidp->open_flags = flags;
1586 if (flags & O_EXCL) {
1588 * We let the host file system do O_EXCL check
1589 * We should not reclaim such fd
1591 fidp->flags |= FID_NON_RECLAIMABLE;
1593 iounit = get_iounit(pdu, &fidp->path);
1594 stat_to_qid(&stbuf, &qid);
1595 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1596 if (err < 0) {
1597 goto out;
1599 err += offset;
1600 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1601 qid.type, qid.version, qid.path, iounit);
1602 out:
1603 put_fid(pdu, fidp);
1604 out_nofid:
1605 pdu_complete(pdu, err);
1606 v9fs_string_free(&name);
1609 static void coroutine_fn v9fs_fsync(void *opaque)
1611 int err;
1612 int32_t fid;
1613 int datasync;
1614 size_t offset = 7;
1615 V9fsFidState *fidp;
1616 V9fsPDU *pdu = opaque;
1618 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1619 if (err < 0) {
1620 goto out_nofid;
1622 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1624 fidp = get_fid(pdu, fid);
1625 if (fidp == NULL) {
1626 err = -ENOENT;
1627 goto out_nofid;
1629 err = v9fs_co_fsync(pdu, fidp, datasync);
1630 if (!err) {
1631 err = offset;
1633 put_fid(pdu, fidp);
1634 out_nofid:
1635 pdu_complete(pdu, err);
1638 static void coroutine_fn v9fs_clunk(void *opaque)
1640 int err;
1641 int32_t fid;
1642 size_t offset = 7;
1643 V9fsFidState *fidp;
1644 V9fsPDU *pdu = opaque;
1645 V9fsState *s = pdu->s;
1647 err = pdu_unmarshal(pdu, offset, "d", &fid);
1648 if (err < 0) {
1649 goto out_nofid;
1651 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1653 fidp = clunk_fid(s, fid);
1654 if (fidp == NULL) {
1655 err = -ENOENT;
1656 goto out_nofid;
1659 * Bump the ref so that put_fid will
1660 * free the fid.
1662 fidp->ref++;
1663 err = put_fid(pdu, fidp);
1664 if (!err) {
1665 err = offset;
1667 out_nofid:
1668 pdu_complete(pdu, err);
1672 * Create a QEMUIOVector for a sub-region of PDU iovecs
1674 * @qiov: uninitialized QEMUIOVector
1675 * @skip: number of bytes to skip from beginning of PDU
1676 * @size: number of bytes to include
1677 * @is_write: true - write, false - read
1679 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1680 * with qemu_iovec_destroy().
1682 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1683 size_t skip, size_t size,
1684 bool is_write)
1686 QEMUIOVector elem;
1687 struct iovec *iov;
1688 unsigned int niov;
1690 if (is_write) {
1691 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip);
1692 } else {
1693 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip);
1696 qemu_iovec_init_external(&elem, iov, niov);
1697 qemu_iovec_init(qiov, niov);
1698 qemu_iovec_concat(qiov, &elem, skip, size);
1701 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1702 uint64_t off, uint32_t max_count)
1704 ssize_t err;
1705 size_t offset = 7;
1706 uint64_t read_count;
1707 QEMUIOVector qiov_full;
1709 if (fidp->fs.xattr.len < off) {
1710 read_count = 0;
1711 } else {
1712 read_count = fidp->fs.xattr.len - off;
1714 if (read_count > max_count) {
1715 read_count = max_count;
1717 err = pdu_marshal(pdu, offset, "d", read_count);
1718 if (err < 0) {
1719 return err;
1721 offset += err;
1723 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false);
1724 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0,
1725 ((char *)fidp->fs.xattr.value) + off,
1726 read_count);
1727 qemu_iovec_destroy(&qiov_full);
1728 if (err < 0) {
1729 return err;
1731 offset += err;
1732 return offset;
1735 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1736 V9fsFidState *fidp,
1737 uint32_t max_count)
1739 V9fsPath path;
1740 V9fsStat v9stat;
1741 int len, err = 0;
1742 int32_t count = 0;
1743 struct stat stbuf;
1744 off_t saved_dir_pos;
1745 struct dirent *dent;
1747 /* save the directory position */
1748 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1749 if (saved_dir_pos < 0) {
1750 return saved_dir_pos;
1753 while (1) {
1754 v9fs_path_init(&path);
1756 v9fs_readdir_lock(&fidp->fs.dir);
1758 err = v9fs_co_readdir(pdu, fidp, &dent);
1759 if (err || !dent) {
1760 break;
1762 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1763 if (err < 0) {
1764 break;
1766 err = v9fs_co_lstat(pdu, &path, &stbuf);
1767 if (err < 0) {
1768 break;
1770 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat);
1771 if (err < 0) {
1772 break;
1774 if ((count + v9stat.size + 2) > max_count) {
1775 v9fs_readdir_unlock(&fidp->fs.dir);
1777 /* Ran out of buffer. Set dir back to old position and return */
1778 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1779 v9fs_stat_free(&v9stat);
1780 v9fs_path_free(&path);
1781 return count;
1784 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1785 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1787 v9fs_readdir_unlock(&fidp->fs.dir);
1789 if (len < 0) {
1790 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1791 v9fs_stat_free(&v9stat);
1792 v9fs_path_free(&path);
1793 return len;
1795 count += len;
1796 v9fs_stat_free(&v9stat);
1797 v9fs_path_free(&path);
1798 saved_dir_pos = dent->d_off;
1801 v9fs_readdir_unlock(&fidp->fs.dir);
1803 v9fs_path_free(&path);
1804 if (err < 0) {
1805 return err;
1807 return count;
1810 static void coroutine_fn v9fs_read(void *opaque)
1812 int32_t fid;
1813 uint64_t off;
1814 ssize_t err = 0;
1815 int32_t count = 0;
1816 size_t offset = 7;
1817 uint32_t max_count;
1818 V9fsFidState *fidp;
1819 V9fsPDU *pdu = opaque;
1820 V9fsState *s = pdu->s;
1822 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1823 if (err < 0) {
1824 goto out_nofid;
1826 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1828 fidp = get_fid(pdu, fid);
1829 if (fidp == NULL) {
1830 err = -EINVAL;
1831 goto out_nofid;
1833 if (fidp->fid_type == P9_FID_DIR) {
1835 if (off == 0) {
1836 v9fs_co_rewinddir(pdu, fidp);
1838 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1839 if (count < 0) {
1840 err = count;
1841 goto out;
1843 err = pdu_marshal(pdu, offset, "d", count);
1844 if (err < 0) {
1845 goto out;
1847 err += offset + count;
1848 } else if (fidp->fid_type == P9_FID_FILE) {
1849 QEMUIOVector qiov_full;
1850 QEMUIOVector qiov;
1851 int32_t len;
1853 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1854 qemu_iovec_init(&qiov, qiov_full.niov);
1855 do {
1856 qemu_iovec_reset(&qiov);
1857 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1858 if (0) {
1859 print_sg(qiov.iov, qiov.niov);
1861 /* Loop in case of EINTR */
1862 do {
1863 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1864 if (len >= 0) {
1865 off += len;
1866 count += len;
1868 } while (len == -EINTR && !pdu->cancelled);
1869 if (len < 0) {
1870 /* IO error return the error */
1871 err = len;
1872 goto out_free_iovec;
1874 } while (count < max_count && len > 0);
1875 err = pdu_marshal(pdu, offset, "d", count);
1876 if (err < 0) {
1877 goto out_free_iovec;
1879 err += offset + count;
1880 out_free_iovec:
1881 qemu_iovec_destroy(&qiov);
1882 qemu_iovec_destroy(&qiov_full);
1883 } else if (fidp->fid_type == P9_FID_XATTR) {
1884 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1885 } else {
1886 err = -EINVAL;
1888 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1889 out:
1890 put_fid(pdu, fidp);
1891 out_nofid:
1892 pdu_complete(pdu, err);
1895 static size_t v9fs_readdir_data_size(V9fsString *name)
1898 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1899 * size of type (1) + size of name.size (2) + strlen(name.data)
1901 return 24 + v9fs_string_size(name);
1904 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
1905 int32_t max_count)
1907 size_t size;
1908 V9fsQID qid;
1909 V9fsString name;
1910 int len, err = 0;
1911 int32_t count = 0;
1912 off_t saved_dir_pos;
1913 struct dirent *dent;
1915 /* save the directory position */
1916 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1917 if (saved_dir_pos < 0) {
1918 return saved_dir_pos;
1921 while (1) {
1922 v9fs_readdir_lock(&fidp->fs.dir);
1924 err = v9fs_co_readdir(pdu, fidp, &dent);
1925 if (err || !dent) {
1926 break;
1928 v9fs_string_init(&name);
1929 v9fs_string_sprintf(&name, "%s", dent->d_name);
1930 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1931 v9fs_readdir_unlock(&fidp->fs.dir);
1933 /* Ran out of buffer. Set dir back to old position and return */
1934 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1935 v9fs_string_free(&name);
1936 return count;
1939 * Fill up just the path field of qid because the client uses
1940 * only that. To fill the entire qid structure we will have
1941 * to stat each dirent found, which is expensive
1943 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1944 memcpy(&qid.path, &dent->d_ino, size);
1945 /* Fill the other fields with dummy values */
1946 qid.type = 0;
1947 qid.version = 0;
1949 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1950 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1951 &qid, dent->d_off,
1952 dent->d_type, &name);
1954 v9fs_readdir_unlock(&fidp->fs.dir);
1956 if (len < 0) {
1957 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1958 v9fs_string_free(&name);
1959 return len;
1961 count += len;
1962 v9fs_string_free(&name);
1963 saved_dir_pos = dent->d_off;
1966 v9fs_readdir_unlock(&fidp->fs.dir);
1968 if (err < 0) {
1969 return err;
1971 return count;
1974 static void coroutine_fn v9fs_readdir(void *opaque)
1976 int32_t fid;
1977 V9fsFidState *fidp;
1978 ssize_t retval = 0;
1979 size_t offset = 7;
1980 uint64_t initial_offset;
1981 int32_t count;
1982 uint32_t max_count;
1983 V9fsPDU *pdu = opaque;
1985 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1986 &initial_offset, &max_count);
1987 if (retval < 0) {
1988 goto out_nofid;
1990 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1992 fidp = get_fid(pdu, fid);
1993 if (fidp == NULL) {
1994 retval = -EINVAL;
1995 goto out_nofid;
1997 if (!fidp->fs.dir.stream) {
1998 retval = -EINVAL;
1999 goto out;
2001 if (initial_offset == 0) {
2002 v9fs_co_rewinddir(pdu, fidp);
2003 } else {
2004 v9fs_co_seekdir(pdu, fidp, initial_offset);
2006 count = v9fs_do_readdir(pdu, fidp, max_count);
2007 if (count < 0) {
2008 retval = count;
2009 goto out;
2011 retval = pdu_marshal(pdu, offset, "d", count);
2012 if (retval < 0) {
2013 goto out;
2015 retval += count + offset;
2016 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
2017 out:
2018 put_fid(pdu, fidp);
2019 out_nofid:
2020 pdu_complete(pdu, retval);
2023 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
2024 uint64_t off, uint32_t count,
2025 struct iovec *sg, int cnt)
2027 int i, to_copy;
2028 ssize_t err = 0;
2029 uint64_t write_count;
2030 size_t offset = 7;
2033 if (fidp->fs.xattr.len < off) {
2034 err = -ENOSPC;
2035 goto out;
2037 write_count = fidp->fs.xattr.len - off;
2038 if (write_count > count) {
2039 write_count = count;
2041 err = pdu_marshal(pdu, offset, "d", write_count);
2042 if (err < 0) {
2043 return err;
2045 err += offset;
2046 fidp->fs.xattr.copied_len += write_count;
2048 * Now copy the content from sg list
2050 for (i = 0; i < cnt; i++) {
2051 if (write_count > sg[i].iov_len) {
2052 to_copy = sg[i].iov_len;
2053 } else {
2054 to_copy = write_count;
2056 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
2057 /* updating vs->off since we are not using below */
2058 off += to_copy;
2059 write_count -= to_copy;
2061 out:
2062 return err;
2065 static void coroutine_fn v9fs_write(void *opaque)
2067 ssize_t err;
2068 int32_t fid;
2069 uint64_t off;
2070 uint32_t count;
2071 int32_t len = 0;
2072 int32_t total = 0;
2073 size_t offset = 7;
2074 V9fsFidState *fidp;
2075 V9fsPDU *pdu = opaque;
2076 V9fsState *s = pdu->s;
2077 QEMUIOVector qiov_full;
2078 QEMUIOVector qiov;
2080 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
2081 if (err < 0) {
2082 pdu_complete(pdu, err);
2083 return;
2085 offset += err;
2086 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
2087 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
2089 fidp = get_fid(pdu, fid);
2090 if (fidp == NULL) {
2091 err = -EINVAL;
2092 goto out_nofid;
2094 if (fidp->fid_type == P9_FID_FILE) {
2095 if (fidp->fs.fd == -1) {
2096 err = -EINVAL;
2097 goto out;
2099 } else if (fidp->fid_type == P9_FID_XATTR) {
2101 * setxattr operation
2103 err = v9fs_xattr_write(s, pdu, fidp, off, count,
2104 qiov_full.iov, qiov_full.niov);
2105 goto out;
2106 } else {
2107 err = -EINVAL;
2108 goto out;
2110 qemu_iovec_init(&qiov, qiov_full.niov);
2111 do {
2112 qemu_iovec_reset(&qiov);
2113 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2114 if (0) {
2115 print_sg(qiov.iov, qiov.niov);
2117 /* Loop in case of EINTR */
2118 do {
2119 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2120 if (len >= 0) {
2121 off += len;
2122 total += len;
2124 } while (len == -EINTR && !pdu->cancelled);
2125 if (len < 0) {
2126 /* IO error return the error */
2127 err = len;
2128 goto out_qiov;
2130 } while (total < count && len > 0);
2132 offset = 7;
2133 err = pdu_marshal(pdu, offset, "d", total);
2134 if (err < 0) {
2135 goto out_qiov;
2137 err += offset;
2138 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2139 out_qiov:
2140 qemu_iovec_destroy(&qiov);
2141 out:
2142 put_fid(pdu, fidp);
2143 out_nofid:
2144 qemu_iovec_destroy(&qiov_full);
2145 pdu_complete(pdu, err);
2148 static void coroutine_fn v9fs_create(void *opaque)
2150 int32_t fid;
2151 int err = 0;
2152 size_t offset = 7;
2153 V9fsFidState *fidp;
2154 V9fsQID qid;
2155 int32_t perm;
2156 int8_t mode;
2157 V9fsPath path;
2158 struct stat stbuf;
2159 V9fsString name;
2160 V9fsString extension;
2161 int iounit;
2162 V9fsPDU *pdu = opaque;
2164 v9fs_path_init(&path);
2165 v9fs_string_init(&name);
2166 v9fs_string_init(&extension);
2167 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2168 &perm, &mode, &extension);
2169 if (err < 0) {
2170 goto out_nofid;
2172 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2174 if (name_is_illegal(name.data)) {
2175 err = -ENOENT;
2176 goto out_nofid;
2179 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2180 err = -EEXIST;
2181 goto out_nofid;
2184 fidp = get_fid(pdu, fid);
2185 if (fidp == NULL) {
2186 err = -EINVAL;
2187 goto out_nofid;
2189 if (fidp->fid_type != P9_FID_NONE) {
2190 err = -EINVAL;
2191 goto out;
2193 if (perm & P9_STAT_MODE_DIR) {
2194 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2195 fidp->uid, -1, &stbuf);
2196 if (err < 0) {
2197 goto out;
2199 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2200 if (err < 0) {
2201 goto out;
2203 v9fs_path_copy(&fidp->path, &path);
2204 err = v9fs_co_opendir(pdu, fidp);
2205 if (err < 0) {
2206 goto out;
2208 fidp->fid_type = P9_FID_DIR;
2209 } else if (perm & P9_STAT_MODE_SYMLINK) {
2210 err = v9fs_co_symlink(pdu, fidp, &name,
2211 extension.data, -1 , &stbuf);
2212 if (err < 0) {
2213 goto out;
2215 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2216 if (err < 0) {
2217 goto out;
2219 v9fs_path_copy(&fidp->path, &path);
2220 } else if (perm & P9_STAT_MODE_LINK) {
2221 int32_t ofid = atoi(extension.data);
2222 V9fsFidState *ofidp = get_fid(pdu, ofid);
2223 if (ofidp == NULL) {
2224 err = -EINVAL;
2225 goto out;
2227 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2228 put_fid(pdu, ofidp);
2229 if (err < 0) {
2230 goto out;
2232 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2233 if (err < 0) {
2234 fidp->fid_type = P9_FID_NONE;
2235 goto out;
2237 v9fs_path_copy(&fidp->path, &path);
2238 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2239 if (err < 0) {
2240 fidp->fid_type = P9_FID_NONE;
2241 goto out;
2243 } else if (perm & P9_STAT_MODE_DEVICE) {
2244 char ctype;
2245 uint32_t major, minor;
2246 mode_t nmode = 0;
2248 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2249 err = -errno;
2250 goto out;
2253 switch (ctype) {
2254 case 'c':
2255 nmode = S_IFCHR;
2256 break;
2257 case 'b':
2258 nmode = S_IFBLK;
2259 break;
2260 default:
2261 err = -EIO;
2262 goto out;
2265 nmode |= perm & 0777;
2266 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2267 makedev(major, minor), nmode, &stbuf);
2268 if (err < 0) {
2269 goto out;
2271 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2272 if (err < 0) {
2273 goto out;
2275 v9fs_path_copy(&fidp->path, &path);
2276 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2277 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2278 0, S_IFIFO | (perm & 0777), &stbuf);
2279 if (err < 0) {
2280 goto out;
2282 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2283 if (err < 0) {
2284 goto out;
2286 v9fs_path_copy(&fidp->path, &path);
2287 } else if (perm & P9_STAT_MODE_SOCKET) {
2288 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2289 0, S_IFSOCK | (perm & 0777), &stbuf);
2290 if (err < 0) {
2291 goto out;
2293 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2294 if (err < 0) {
2295 goto out;
2297 v9fs_path_copy(&fidp->path, &path);
2298 } else {
2299 err = v9fs_co_open2(pdu, fidp, &name, -1,
2300 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2301 if (err < 0) {
2302 goto out;
2304 fidp->fid_type = P9_FID_FILE;
2305 fidp->open_flags = omode_to_uflags(mode);
2306 if (fidp->open_flags & O_EXCL) {
2308 * We let the host file system do O_EXCL check
2309 * We should not reclaim such fd
2311 fidp->flags |= FID_NON_RECLAIMABLE;
2314 iounit = get_iounit(pdu, &fidp->path);
2315 stat_to_qid(&stbuf, &qid);
2316 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2317 if (err < 0) {
2318 goto out;
2320 err += offset;
2321 trace_v9fs_create_return(pdu->tag, pdu->id,
2322 qid.type, qid.version, qid.path, iounit);
2323 out:
2324 put_fid(pdu, fidp);
2325 out_nofid:
2326 pdu_complete(pdu, err);
2327 v9fs_string_free(&name);
2328 v9fs_string_free(&extension);
2329 v9fs_path_free(&path);
2332 static void coroutine_fn v9fs_symlink(void *opaque)
2334 V9fsPDU *pdu = opaque;
2335 V9fsString name;
2336 V9fsString symname;
2337 V9fsFidState *dfidp;
2338 V9fsQID qid;
2339 struct stat stbuf;
2340 int32_t dfid;
2341 int err = 0;
2342 gid_t gid;
2343 size_t offset = 7;
2345 v9fs_string_init(&name);
2346 v9fs_string_init(&symname);
2347 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2348 if (err < 0) {
2349 goto out_nofid;
2351 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2353 if (name_is_illegal(name.data)) {
2354 err = -ENOENT;
2355 goto out_nofid;
2358 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2359 err = -EEXIST;
2360 goto out_nofid;
2363 dfidp = get_fid(pdu, dfid);
2364 if (dfidp == NULL) {
2365 err = -EINVAL;
2366 goto out_nofid;
2368 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2369 if (err < 0) {
2370 goto out;
2372 stat_to_qid(&stbuf, &qid);
2373 err = pdu_marshal(pdu, offset, "Q", &qid);
2374 if (err < 0) {
2375 goto out;
2377 err += offset;
2378 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2379 qid.type, qid.version, qid.path);
2380 out:
2381 put_fid(pdu, dfidp);
2382 out_nofid:
2383 pdu_complete(pdu, err);
2384 v9fs_string_free(&name);
2385 v9fs_string_free(&symname);
2388 static void coroutine_fn v9fs_flush(void *opaque)
2390 ssize_t err;
2391 int16_t tag;
2392 size_t offset = 7;
2393 V9fsPDU *cancel_pdu = NULL;
2394 V9fsPDU *pdu = opaque;
2395 V9fsState *s = pdu->s;
2397 err = pdu_unmarshal(pdu, offset, "w", &tag);
2398 if (err < 0) {
2399 pdu_complete(pdu, err);
2400 return;
2402 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2404 if (pdu->tag == tag) {
2405 warn_report("the guest sent a self-referencing 9P flush request");
2406 } else {
2407 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2408 if (cancel_pdu->tag == tag) {
2409 break;
2413 if (cancel_pdu) {
2414 cancel_pdu->cancelled = 1;
2416 * Wait for pdu to complete.
2418 qemu_co_queue_wait(&cancel_pdu->complete, NULL);
2419 if (!qemu_co_queue_next(&cancel_pdu->complete)) {
2420 cancel_pdu->cancelled = 0;
2421 pdu_free(cancel_pdu);
2424 pdu_complete(pdu, 7);
2427 static void coroutine_fn v9fs_link(void *opaque)
2429 V9fsPDU *pdu = opaque;
2430 int32_t dfid, oldfid;
2431 V9fsFidState *dfidp, *oldfidp;
2432 V9fsString name;
2433 size_t offset = 7;
2434 int err = 0;
2436 v9fs_string_init(&name);
2437 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2438 if (err < 0) {
2439 goto out_nofid;
2441 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2443 if (name_is_illegal(name.data)) {
2444 err = -ENOENT;
2445 goto out_nofid;
2448 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2449 err = -EEXIST;
2450 goto out_nofid;
2453 dfidp = get_fid(pdu, dfid);
2454 if (dfidp == NULL) {
2455 err = -ENOENT;
2456 goto out_nofid;
2459 oldfidp = get_fid(pdu, oldfid);
2460 if (oldfidp == NULL) {
2461 err = -ENOENT;
2462 goto out;
2464 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2465 if (!err) {
2466 err = offset;
2468 put_fid(pdu, oldfidp);
2469 out:
2470 put_fid(pdu, dfidp);
2471 out_nofid:
2472 v9fs_string_free(&name);
2473 pdu_complete(pdu, err);
2476 /* Only works with path name based fid */
2477 static void coroutine_fn v9fs_remove(void *opaque)
2479 int32_t fid;
2480 int err = 0;
2481 size_t offset = 7;
2482 V9fsFidState *fidp;
2483 V9fsPDU *pdu = opaque;
2485 err = pdu_unmarshal(pdu, offset, "d", &fid);
2486 if (err < 0) {
2487 goto out_nofid;
2489 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2491 fidp = get_fid(pdu, fid);
2492 if (fidp == NULL) {
2493 err = -EINVAL;
2494 goto out_nofid;
2496 /* if fs driver is not path based, return EOPNOTSUPP */
2497 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2498 err = -EOPNOTSUPP;
2499 goto out_err;
2502 * IF the file is unlinked, we cannot reopen
2503 * the file later. So don't reclaim fd
2505 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2506 if (err < 0) {
2507 goto out_err;
2509 err = v9fs_co_remove(pdu, &fidp->path);
2510 if (!err) {
2511 err = offset;
2513 out_err:
2514 /* For TREMOVE we need to clunk the fid even on failed remove */
2515 clunk_fid(pdu->s, fidp->fid);
2516 put_fid(pdu, fidp);
2517 out_nofid:
2518 pdu_complete(pdu, err);
2521 static void coroutine_fn v9fs_unlinkat(void *opaque)
2523 int err = 0;
2524 V9fsString name;
2525 int32_t dfid, flags;
2526 size_t offset = 7;
2527 V9fsPath path;
2528 V9fsFidState *dfidp;
2529 V9fsPDU *pdu = opaque;
2531 v9fs_string_init(&name);
2532 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2533 if (err < 0) {
2534 goto out_nofid;
2537 if (name_is_illegal(name.data)) {
2538 err = -ENOENT;
2539 goto out_nofid;
2542 if (!strcmp(".", name.data)) {
2543 err = -EINVAL;
2544 goto out_nofid;
2547 if (!strcmp("..", name.data)) {
2548 err = -ENOTEMPTY;
2549 goto out_nofid;
2552 dfidp = get_fid(pdu, dfid);
2553 if (dfidp == NULL) {
2554 err = -EINVAL;
2555 goto out_nofid;
2558 * IF the file is unlinked, we cannot reopen
2559 * the file later. So don't reclaim fd
2561 v9fs_path_init(&path);
2562 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2563 if (err < 0) {
2564 goto out_err;
2566 err = v9fs_mark_fids_unreclaim(pdu, &path);
2567 if (err < 0) {
2568 goto out_err;
2570 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2571 if (!err) {
2572 err = offset;
2574 out_err:
2575 put_fid(pdu, dfidp);
2576 v9fs_path_free(&path);
2577 out_nofid:
2578 pdu_complete(pdu, err);
2579 v9fs_string_free(&name);
2583 /* Only works with path name based fid */
2584 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2585 int32_t newdirfid,
2586 V9fsString *name)
2588 int err = 0;
2589 V9fsPath new_path;
2590 V9fsFidState *tfidp;
2591 V9fsState *s = pdu->s;
2592 V9fsFidState *dirfidp = NULL;
2594 v9fs_path_init(&new_path);
2595 if (newdirfid != -1) {
2596 dirfidp = get_fid(pdu, newdirfid);
2597 if (dirfidp == NULL) {
2598 err = -ENOENT;
2599 goto out_nofid;
2601 if (fidp->fid_type != P9_FID_NONE) {
2602 err = -EINVAL;
2603 goto out;
2605 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2606 if (err < 0) {
2607 goto out;
2609 } else {
2610 char *dir_name = g_path_get_dirname(fidp->path.data);
2611 V9fsPath dir_path;
2613 v9fs_path_init(&dir_path);
2614 v9fs_path_sprintf(&dir_path, "%s", dir_name);
2615 g_free(dir_name);
2617 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path);
2618 v9fs_path_free(&dir_path);
2619 if (err < 0) {
2620 goto out;
2623 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2624 if (err < 0) {
2625 goto out;
2628 * Fixup fid's pointing to the old name to
2629 * start pointing to the new name
2631 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2632 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2633 /* replace the name */
2634 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2637 out:
2638 if (dirfidp) {
2639 put_fid(pdu, dirfidp);
2641 v9fs_path_free(&new_path);
2642 out_nofid:
2643 return err;
2646 /* Only works with path name based fid */
2647 static void coroutine_fn v9fs_rename(void *opaque)
2649 int32_t fid;
2650 ssize_t err = 0;
2651 size_t offset = 7;
2652 V9fsString name;
2653 int32_t newdirfid;
2654 V9fsFidState *fidp;
2655 V9fsPDU *pdu = opaque;
2656 V9fsState *s = pdu->s;
2658 v9fs_string_init(&name);
2659 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2660 if (err < 0) {
2661 goto out_nofid;
2664 if (name_is_illegal(name.data)) {
2665 err = -ENOENT;
2666 goto out_nofid;
2669 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
2670 err = -EISDIR;
2671 goto out_nofid;
2674 fidp = get_fid(pdu, fid);
2675 if (fidp == NULL) {
2676 err = -ENOENT;
2677 goto out_nofid;
2679 if (fidp->fid_type != P9_FID_NONE) {
2680 err = -EINVAL;
2681 goto out;
2683 /* if fs driver is not path based, return EOPNOTSUPP */
2684 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2685 err = -EOPNOTSUPP;
2686 goto out;
2688 v9fs_path_write_lock(s);
2689 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2690 v9fs_path_unlock(s);
2691 if (!err) {
2692 err = offset;
2694 out:
2695 put_fid(pdu, fidp);
2696 out_nofid:
2697 pdu_complete(pdu, err);
2698 v9fs_string_free(&name);
2701 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2702 V9fsString *old_name,
2703 V9fsPath *newdir,
2704 V9fsString *new_name)
2706 V9fsFidState *tfidp;
2707 V9fsPath oldpath, newpath;
2708 V9fsState *s = pdu->s;
2709 int err;
2711 v9fs_path_init(&oldpath);
2712 v9fs_path_init(&newpath);
2713 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2714 if (err < 0) {
2715 goto out;
2717 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2718 if (err < 0) {
2719 goto out;
2723 * Fixup fid's pointing to the old name to
2724 * start pointing to the new name
2726 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2727 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2728 /* replace the name */
2729 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2732 out:
2733 v9fs_path_free(&oldpath);
2734 v9fs_path_free(&newpath);
2735 return err;
2738 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2739 V9fsString *old_name,
2740 int32_t newdirfid,
2741 V9fsString *new_name)
2743 int err = 0;
2744 V9fsState *s = pdu->s;
2745 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2747 olddirfidp = get_fid(pdu, olddirfid);
2748 if (olddirfidp == NULL) {
2749 err = -ENOENT;
2750 goto out;
2752 if (newdirfid != -1) {
2753 newdirfidp = get_fid(pdu, newdirfid);
2754 if (newdirfidp == NULL) {
2755 err = -ENOENT;
2756 goto out;
2758 } else {
2759 newdirfidp = get_fid(pdu, olddirfid);
2762 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2763 &newdirfidp->path, new_name);
2764 if (err < 0) {
2765 goto out;
2767 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2768 /* Only for path based fid we need to do the below fixup */
2769 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2770 &newdirfidp->path, new_name);
2772 out:
2773 if (olddirfidp) {
2774 put_fid(pdu, olddirfidp);
2776 if (newdirfidp) {
2777 put_fid(pdu, newdirfidp);
2779 return err;
2782 static void coroutine_fn v9fs_renameat(void *opaque)
2784 ssize_t err = 0;
2785 size_t offset = 7;
2786 V9fsPDU *pdu = opaque;
2787 V9fsState *s = pdu->s;
2788 int32_t olddirfid, newdirfid;
2789 V9fsString old_name, new_name;
2791 v9fs_string_init(&old_name);
2792 v9fs_string_init(&new_name);
2793 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2794 &old_name, &newdirfid, &new_name);
2795 if (err < 0) {
2796 goto out_err;
2799 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) {
2800 err = -ENOENT;
2801 goto out_err;
2804 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) ||
2805 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) {
2806 err = -EISDIR;
2807 goto out_err;
2810 v9fs_path_write_lock(s);
2811 err = v9fs_complete_renameat(pdu, olddirfid,
2812 &old_name, newdirfid, &new_name);
2813 v9fs_path_unlock(s);
2814 if (!err) {
2815 err = offset;
2818 out_err:
2819 pdu_complete(pdu, err);
2820 v9fs_string_free(&old_name);
2821 v9fs_string_free(&new_name);
2824 static void coroutine_fn v9fs_wstat(void *opaque)
2826 int32_t fid;
2827 int err = 0;
2828 int16_t unused;
2829 V9fsStat v9stat;
2830 size_t offset = 7;
2831 struct stat stbuf;
2832 V9fsFidState *fidp;
2833 V9fsPDU *pdu = opaque;
2835 v9fs_stat_init(&v9stat);
2836 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2837 if (err < 0) {
2838 goto out_nofid;
2840 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2841 v9stat.mode, v9stat.atime, v9stat.mtime);
2843 fidp = get_fid(pdu, fid);
2844 if (fidp == NULL) {
2845 err = -EINVAL;
2846 goto out_nofid;
2848 /* do we need to sync the file? */
2849 if (donttouch_stat(&v9stat)) {
2850 err = v9fs_co_fsync(pdu, fidp, 0);
2851 goto out;
2853 if (v9stat.mode != -1) {
2854 uint32_t v9_mode;
2855 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2856 if (err < 0) {
2857 goto out;
2859 v9_mode = stat_to_v9mode(&stbuf);
2860 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2861 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2862 /* Attempting to change the type */
2863 err = -EIO;
2864 goto out;
2866 err = v9fs_co_chmod(pdu, &fidp->path,
2867 v9mode_to_mode(v9stat.mode,
2868 &v9stat.extension));
2869 if (err < 0) {
2870 goto out;
2873 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2874 struct timespec times[2];
2875 if (v9stat.atime != -1) {
2876 times[0].tv_sec = v9stat.atime;
2877 times[0].tv_nsec = 0;
2878 } else {
2879 times[0].tv_nsec = UTIME_OMIT;
2881 if (v9stat.mtime != -1) {
2882 times[1].tv_sec = v9stat.mtime;
2883 times[1].tv_nsec = 0;
2884 } else {
2885 times[1].tv_nsec = UTIME_OMIT;
2887 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2888 if (err < 0) {
2889 goto out;
2892 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2893 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2894 if (err < 0) {
2895 goto out;
2898 if (v9stat.name.size != 0) {
2899 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2900 if (err < 0) {
2901 goto out;
2904 if (v9stat.length != -1) {
2905 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2906 if (err < 0) {
2907 goto out;
2910 err = offset;
2911 out:
2912 put_fid(pdu, fidp);
2913 out_nofid:
2914 v9fs_stat_free(&v9stat);
2915 pdu_complete(pdu, err);
2918 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2920 uint32_t f_type;
2921 uint32_t f_bsize;
2922 uint64_t f_blocks;
2923 uint64_t f_bfree;
2924 uint64_t f_bavail;
2925 uint64_t f_files;
2926 uint64_t f_ffree;
2927 uint64_t fsid_val;
2928 uint32_t f_namelen;
2929 size_t offset = 7;
2930 int32_t bsize_factor;
2933 * compute bsize factor based on host file system block size
2934 * and client msize
2936 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2937 if (!bsize_factor) {
2938 bsize_factor = 1;
2940 f_type = stbuf->f_type;
2941 f_bsize = stbuf->f_bsize;
2942 f_bsize *= bsize_factor;
2944 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2945 * adjust(divide) the number of blocks, free blocks and available
2946 * blocks by bsize factor
2948 f_blocks = stbuf->f_blocks/bsize_factor;
2949 f_bfree = stbuf->f_bfree/bsize_factor;
2950 f_bavail = stbuf->f_bavail/bsize_factor;
2951 f_files = stbuf->f_files;
2952 f_ffree = stbuf->f_ffree;
2953 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2954 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2955 f_namelen = stbuf->f_namelen;
2957 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2958 f_type, f_bsize, f_blocks, f_bfree,
2959 f_bavail, f_files, f_ffree,
2960 fsid_val, f_namelen);
2963 static void coroutine_fn v9fs_statfs(void *opaque)
2965 int32_t fid;
2966 ssize_t retval = 0;
2967 size_t offset = 7;
2968 V9fsFidState *fidp;
2969 struct statfs stbuf;
2970 V9fsPDU *pdu = opaque;
2971 V9fsState *s = pdu->s;
2973 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2974 if (retval < 0) {
2975 goto out_nofid;
2977 fidp = get_fid(pdu, fid);
2978 if (fidp == NULL) {
2979 retval = -ENOENT;
2980 goto out_nofid;
2982 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2983 if (retval < 0) {
2984 goto out;
2986 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2987 if (retval < 0) {
2988 goto out;
2990 retval += offset;
2991 out:
2992 put_fid(pdu, fidp);
2993 out_nofid:
2994 pdu_complete(pdu, retval);
2997 static void coroutine_fn v9fs_mknod(void *opaque)
3000 int mode;
3001 gid_t gid;
3002 int32_t fid;
3003 V9fsQID qid;
3004 int err = 0;
3005 int major, minor;
3006 size_t offset = 7;
3007 V9fsString name;
3008 struct stat stbuf;
3009 V9fsFidState *fidp;
3010 V9fsPDU *pdu = opaque;
3012 v9fs_string_init(&name);
3013 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
3014 &major, &minor, &gid);
3015 if (err < 0) {
3016 goto out_nofid;
3018 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
3020 if (name_is_illegal(name.data)) {
3021 err = -ENOENT;
3022 goto out_nofid;
3025 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3026 err = -EEXIST;
3027 goto out_nofid;
3030 fidp = get_fid(pdu, fid);
3031 if (fidp == NULL) {
3032 err = -ENOENT;
3033 goto out_nofid;
3035 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
3036 makedev(major, minor), mode, &stbuf);
3037 if (err < 0) {
3038 goto out;
3040 stat_to_qid(&stbuf, &qid);
3041 err = pdu_marshal(pdu, offset, "Q", &qid);
3042 if (err < 0) {
3043 goto out;
3045 err += offset;
3046 trace_v9fs_mknod_return(pdu->tag, pdu->id,
3047 qid.type, qid.version, qid.path);
3048 out:
3049 put_fid(pdu, fidp);
3050 out_nofid:
3051 pdu_complete(pdu, err);
3052 v9fs_string_free(&name);
3056 * Implement posix byte range locking code
3057 * Server side handling of locking code is very simple, because 9p server in
3058 * QEMU can handle only one client. And most of the lock handling
3059 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3060 * do any thing in * qemu 9p server side lock code path.
3061 * So when a TLOCK request comes, always return success
3063 static void coroutine_fn v9fs_lock(void *opaque)
3065 V9fsFlock flock;
3066 size_t offset = 7;
3067 struct stat stbuf;
3068 V9fsFidState *fidp;
3069 int32_t fid, err = 0;
3070 V9fsPDU *pdu = opaque;
3072 v9fs_string_init(&flock.client_id);
3073 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
3074 &flock.flags, &flock.start, &flock.length,
3075 &flock.proc_id, &flock.client_id);
3076 if (err < 0) {
3077 goto out_nofid;
3079 trace_v9fs_lock(pdu->tag, pdu->id, fid,
3080 flock.type, flock.start, flock.length);
3083 /* We support only block flag now (that too ignored currently) */
3084 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
3085 err = -EINVAL;
3086 goto out_nofid;
3088 fidp = get_fid(pdu, fid);
3089 if (fidp == NULL) {
3090 err = -ENOENT;
3091 goto out_nofid;
3093 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3094 if (err < 0) {
3095 goto out;
3097 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS);
3098 if (err < 0) {
3099 goto out;
3101 err += offset;
3102 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS);
3103 out:
3104 put_fid(pdu, fidp);
3105 out_nofid:
3106 pdu_complete(pdu, err);
3107 v9fs_string_free(&flock.client_id);
3111 * When a TGETLOCK request comes, always return success because all lock
3112 * handling is done by client's VFS layer.
3114 static void coroutine_fn v9fs_getlock(void *opaque)
3116 size_t offset = 7;
3117 struct stat stbuf;
3118 V9fsFidState *fidp;
3119 V9fsGetlock glock;
3120 int32_t fid, err = 0;
3121 V9fsPDU *pdu = opaque;
3123 v9fs_string_init(&glock.client_id);
3124 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
3125 &glock.start, &glock.length, &glock.proc_id,
3126 &glock.client_id);
3127 if (err < 0) {
3128 goto out_nofid;
3130 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
3131 glock.type, glock.start, glock.length);
3133 fidp = get_fid(pdu, fid);
3134 if (fidp == NULL) {
3135 err = -ENOENT;
3136 goto out_nofid;
3138 err = v9fs_co_fstat(pdu, fidp, &stbuf);
3139 if (err < 0) {
3140 goto out;
3142 glock.type = P9_LOCK_TYPE_UNLCK;
3143 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
3144 glock.start, glock.length, glock.proc_id,
3145 &glock.client_id);
3146 if (err < 0) {
3147 goto out;
3149 err += offset;
3150 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
3151 glock.length, glock.proc_id);
3152 out:
3153 put_fid(pdu, fidp);
3154 out_nofid:
3155 pdu_complete(pdu, err);
3156 v9fs_string_free(&glock.client_id);
3159 static void coroutine_fn v9fs_mkdir(void *opaque)
3161 V9fsPDU *pdu = opaque;
3162 size_t offset = 7;
3163 int32_t fid;
3164 struct stat stbuf;
3165 V9fsQID qid;
3166 V9fsString name;
3167 V9fsFidState *fidp;
3168 gid_t gid;
3169 int mode;
3170 int err = 0;
3172 v9fs_string_init(&name);
3173 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
3174 if (err < 0) {
3175 goto out_nofid;
3177 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
3179 if (name_is_illegal(name.data)) {
3180 err = -ENOENT;
3181 goto out_nofid;
3184 if (!strcmp(".", name.data) || !strcmp("..", name.data)) {
3185 err = -EEXIST;
3186 goto out_nofid;
3189 fidp = get_fid(pdu, fid);
3190 if (fidp == NULL) {
3191 err = -ENOENT;
3192 goto out_nofid;
3194 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
3195 if (err < 0) {
3196 goto out;
3198 stat_to_qid(&stbuf, &qid);
3199 err = pdu_marshal(pdu, offset, "Q", &qid);
3200 if (err < 0) {
3201 goto out;
3203 err += offset;
3204 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
3205 qid.type, qid.version, qid.path, err);
3206 out:
3207 put_fid(pdu, fidp);
3208 out_nofid:
3209 pdu_complete(pdu, err);
3210 v9fs_string_free(&name);
3213 static void coroutine_fn v9fs_xattrwalk(void *opaque)
3215 int64_t size;
3216 V9fsString name;
3217 ssize_t err = 0;
3218 size_t offset = 7;
3219 int32_t fid, newfid;
3220 V9fsFidState *file_fidp;
3221 V9fsFidState *xattr_fidp = NULL;
3222 V9fsPDU *pdu = opaque;
3223 V9fsState *s = pdu->s;
3225 v9fs_string_init(&name);
3226 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3227 if (err < 0) {
3228 goto out_nofid;
3230 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3232 file_fidp = get_fid(pdu, fid);
3233 if (file_fidp == NULL) {
3234 err = -ENOENT;
3235 goto out_nofid;
3237 xattr_fidp = alloc_fid(s, newfid);
3238 if (xattr_fidp == NULL) {
3239 err = -EINVAL;
3240 goto out;
3242 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3243 if (!v9fs_string_size(&name)) {
3245 * listxattr request. Get the size first
3247 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3248 if (size < 0) {
3249 err = size;
3250 clunk_fid(s, xattr_fidp->fid);
3251 goto out;
3254 * Read the xattr value
3256 xattr_fidp->fs.xattr.len = size;
3257 xattr_fidp->fid_type = P9_FID_XATTR;
3258 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3259 if (size) {
3260 xattr_fidp->fs.xattr.value = g_malloc0(size);
3261 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3262 xattr_fidp->fs.xattr.value,
3263 xattr_fidp->fs.xattr.len);
3264 if (err < 0) {
3265 clunk_fid(s, xattr_fidp->fid);
3266 goto out;
3269 err = pdu_marshal(pdu, offset, "q", size);
3270 if (err < 0) {
3271 goto out;
3273 err += offset;
3274 } else {
3276 * specific xattr fid. We check for xattr
3277 * presence also collect the xattr size
3279 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3280 &name, NULL, 0);
3281 if (size < 0) {
3282 err = size;
3283 clunk_fid(s, xattr_fidp->fid);
3284 goto out;
3287 * Read the xattr value
3289 xattr_fidp->fs.xattr.len = size;
3290 xattr_fidp->fid_type = P9_FID_XATTR;
3291 xattr_fidp->fs.xattr.xattrwalk_fid = true;
3292 if (size) {
3293 xattr_fidp->fs.xattr.value = g_malloc0(size);
3294 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3295 &name, xattr_fidp->fs.xattr.value,
3296 xattr_fidp->fs.xattr.len);
3297 if (err < 0) {
3298 clunk_fid(s, xattr_fidp->fid);
3299 goto out;
3302 err = pdu_marshal(pdu, offset, "q", size);
3303 if (err < 0) {
3304 goto out;
3306 err += offset;
3308 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3309 out:
3310 put_fid(pdu, file_fidp);
3311 if (xattr_fidp) {
3312 put_fid(pdu, xattr_fidp);
3314 out_nofid:
3315 pdu_complete(pdu, err);
3316 v9fs_string_free(&name);
3319 static void coroutine_fn v9fs_xattrcreate(void *opaque)
3321 int flags;
3322 int32_t fid;
3323 uint64_t size;
3324 ssize_t err = 0;
3325 V9fsString name;
3326 size_t offset = 7;
3327 V9fsFidState *file_fidp;
3328 V9fsFidState *xattr_fidp;
3329 V9fsPDU *pdu = opaque;
3331 v9fs_string_init(&name);
3332 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3333 if (err < 0) {
3334 goto out_nofid;
3336 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3338 if (size > XATTR_SIZE_MAX) {
3339 err = -E2BIG;
3340 goto out_nofid;
3343 file_fidp = get_fid(pdu, fid);
3344 if (file_fidp == NULL) {
3345 err = -EINVAL;
3346 goto out_nofid;
3348 if (file_fidp->fid_type != P9_FID_NONE) {
3349 err = -EINVAL;
3350 goto out_put_fid;
3353 /* Make the file fid point to xattr */
3354 xattr_fidp = file_fidp;
3355 xattr_fidp->fid_type = P9_FID_XATTR;
3356 xattr_fidp->fs.xattr.copied_len = 0;
3357 xattr_fidp->fs.xattr.xattrwalk_fid = false;
3358 xattr_fidp->fs.xattr.len = size;
3359 xattr_fidp->fs.xattr.flags = flags;
3360 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3361 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3362 xattr_fidp->fs.xattr.value = g_malloc0(size);
3363 err = offset;
3364 out_put_fid:
3365 put_fid(pdu, file_fidp);
3366 out_nofid:
3367 pdu_complete(pdu, err);
3368 v9fs_string_free(&name);
3371 static void coroutine_fn v9fs_readlink(void *opaque)
3373 V9fsPDU *pdu = opaque;
3374 size_t offset = 7;
3375 V9fsString target;
3376 int32_t fid;
3377 int err = 0;
3378 V9fsFidState *fidp;
3380 err = pdu_unmarshal(pdu, offset, "d", &fid);
3381 if (err < 0) {
3382 goto out_nofid;
3384 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3385 fidp = get_fid(pdu, fid);
3386 if (fidp == NULL) {
3387 err = -ENOENT;
3388 goto out_nofid;
3391 v9fs_string_init(&target);
3392 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3393 if (err < 0) {
3394 goto out;
3396 err = pdu_marshal(pdu, offset, "s", &target);
3397 if (err < 0) {
3398 v9fs_string_free(&target);
3399 goto out;
3401 err += offset;
3402 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3403 v9fs_string_free(&target);
3404 out:
3405 put_fid(pdu, fidp);
3406 out_nofid:
3407 pdu_complete(pdu, err);
3410 static CoroutineEntry *pdu_co_handlers[] = {
3411 [P9_TREADDIR] = v9fs_readdir,
3412 [P9_TSTATFS] = v9fs_statfs,
3413 [P9_TGETATTR] = v9fs_getattr,
3414 [P9_TSETATTR] = v9fs_setattr,
3415 [P9_TXATTRWALK] = v9fs_xattrwalk,
3416 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3417 [P9_TMKNOD] = v9fs_mknod,
3418 [P9_TRENAME] = v9fs_rename,
3419 [P9_TLOCK] = v9fs_lock,
3420 [P9_TGETLOCK] = v9fs_getlock,
3421 [P9_TRENAMEAT] = v9fs_renameat,
3422 [P9_TREADLINK] = v9fs_readlink,
3423 [P9_TUNLINKAT] = v9fs_unlinkat,
3424 [P9_TMKDIR] = v9fs_mkdir,
3425 [P9_TVERSION] = v9fs_version,
3426 [P9_TLOPEN] = v9fs_open,
3427 [P9_TATTACH] = v9fs_attach,
3428 [P9_TSTAT] = v9fs_stat,
3429 [P9_TWALK] = v9fs_walk,
3430 [P9_TCLUNK] = v9fs_clunk,
3431 [P9_TFSYNC] = v9fs_fsync,
3432 [P9_TOPEN] = v9fs_open,
3433 [P9_TREAD] = v9fs_read,
3434 #if 0
3435 [P9_TAUTH] = v9fs_auth,
3436 #endif
3437 [P9_TFLUSH] = v9fs_flush,
3438 [P9_TLINK] = v9fs_link,
3439 [P9_TSYMLINK] = v9fs_symlink,
3440 [P9_TCREATE] = v9fs_create,
3441 [P9_TLCREATE] = v9fs_lcreate,
3442 [P9_TWRITE] = v9fs_write,
3443 [P9_TWSTAT] = v9fs_wstat,
3444 [P9_TREMOVE] = v9fs_remove,
3447 static void coroutine_fn v9fs_op_not_supp(void *opaque)
3449 V9fsPDU *pdu = opaque;
3450 pdu_complete(pdu, -EOPNOTSUPP);
3453 static void coroutine_fn v9fs_fs_ro(void *opaque)
3455 V9fsPDU *pdu = opaque;
3456 pdu_complete(pdu, -EROFS);
3459 static inline bool is_read_only_op(V9fsPDU *pdu)
3461 switch (pdu->id) {
3462 case P9_TREADDIR:
3463 case P9_TSTATFS:
3464 case P9_TGETATTR:
3465 case P9_TXATTRWALK:
3466 case P9_TLOCK:
3467 case P9_TGETLOCK:
3468 case P9_TREADLINK:
3469 case P9_TVERSION:
3470 case P9_TLOPEN:
3471 case P9_TATTACH:
3472 case P9_TSTAT:
3473 case P9_TWALK:
3474 case P9_TCLUNK:
3475 case P9_TFSYNC:
3476 case P9_TOPEN:
3477 case P9_TREAD:
3478 case P9_TAUTH:
3479 case P9_TFLUSH:
3480 return 1;
3481 default:
3482 return 0;
3486 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr)
3488 Coroutine *co;
3489 CoroutineEntry *handler;
3490 V9fsState *s = pdu->s;
3492 pdu->size = le32_to_cpu(hdr->size_le);
3493 pdu->id = hdr->id;
3494 pdu->tag = le16_to_cpu(hdr->tag_le);
3496 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3497 (pdu_co_handlers[pdu->id] == NULL)) {
3498 handler = v9fs_op_not_supp;
3499 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3500 handler = v9fs_fs_ro;
3501 } else {
3502 handler = pdu_co_handlers[pdu->id];
3505 qemu_co_queue_init(&pdu->complete);
3506 co = qemu_coroutine_create(handler, pdu);
3507 qemu_coroutine_enter(co);
3510 /* Returns 0 on success, 1 on failure. */
3511 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t,
3512 Error **errp)
3514 int i, len;
3515 struct stat stat;
3516 FsDriverEntry *fse;
3517 V9fsPath path;
3518 int rc = 1;
3520 assert(!s->transport);
3521 s->transport = t;
3523 /* initialize pdu allocator */
3524 QLIST_INIT(&s->free_list);
3525 QLIST_INIT(&s->active_list);
3526 for (i = 0; i < MAX_REQ; i++) {
3527 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next);
3528 s->pdus[i].s = s;
3529 s->pdus[i].idx = i;
3532 v9fs_path_init(&path);
3534 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3536 if (!fse) {
3537 /* We don't have a fsdev identified by fsdev_id */
3538 error_setg(errp, "9pfs device couldn't find fsdev with the "
3539 "id = %s",
3540 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3541 goto out;
3544 if (!s->fsconf.tag) {
3545 /* we haven't specified a mount_tag */
3546 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3547 s->fsconf.fsdev_id);
3548 goto out;
3551 s->ctx.export_flags = fse->export_flags;
3552 s->ctx.fs_root = g_strdup(fse->path);
3553 s->ctx.exops.get_st_gen = NULL;
3554 len = strlen(s->fsconf.tag);
3555 if (len > MAX_TAG_LEN - 1) {
3556 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3557 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3558 goto out;
3561 s->tag = g_strdup(s->fsconf.tag);
3562 s->ctx.uid = -1;
3564 s->ops = fse->ops;
3566 s->ctx.fmode = fse->fmode;
3567 s->ctx.dmode = fse->dmode;
3569 s->fid_list = NULL;
3570 qemu_co_rwlock_init(&s->rename_lock);
3572 if (s->ops->init(&s->ctx, errp) < 0) {
3573 error_prepend(errp, "cannot initialize fsdev '%s': ",
3574 s->fsconf.fsdev_id);
3575 goto out;
3579 * Check details of export path, We need to use fs driver
3580 * call back to do that. Since we are in the init path, we don't
3581 * use co-routines here.
3583 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3584 error_setg(errp,
3585 "error in converting name to path %s", strerror(errno));
3586 goto out;
3588 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3589 error_setg(errp, "share path %s does not exist", fse->path);
3590 goto out;
3591 } else if (!S_ISDIR(stat.st_mode)) {
3592 error_setg(errp, "share path %s is not a directory", fse->path);
3593 goto out;
3596 s->ctx.fst = &fse->fst;
3597 fsdev_throttle_init(s->ctx.fst);
3599 v9fs_path_free(&path);
3601 rc = 0;
3602 out:
3603 if (rc) {
3604 if (s->ops && s->ops->cleanup && s->ctx.private) {
3605 s->ops->cleanup(&s->ctx);
3607 g_free(s->tag);
3608 g_free(s->ctx.fs_root);
3609 v9fs_path_free(&path);
3611 return rc;
3614 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3616 if (s->ops->cleanup) {
3617 s->ops->cleanup(&s->ctx);
3619 fsdev_throttle_cleanup(s->ctx.fst);
3620 g_free(s->tag);
3621 g_free(s->ctx.fs_root);
3624 typedef struct VirtfsCoResetData {
3625 V9fsPDU pdu;
3626 bool done;
3627 } VirtfsCoResetData;
3629 static void coroutine_fn virtfs_co_reset(void *opaque)
3631 VirtfsCoResetData *data = opaque;
3633 virtfs_reset(&data->pdu);
3634 data->done = true;
3637 void v9fs_reset(V9fsState *s)
3639 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false };
3640 Coroutine *co;
3642 while (!QLIST_EMPTY(&s->active_list)) {
3643 aio_poll(qemu_get_aio_context(), true);
3646 co = qemu_coroutine_create(virtfs_co_reset, &data);
3647 qemu_coroutine_enter(co);
3649 while (!data.done) {
3650 aio_poll(qemu_get_aio_context(), true);
3654 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3656 struct rlimit rlim;
3657 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3658 error_report("Failed to get the resource limit");
3659 exit(1);
3661 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3662 open_fd_rc = rlim.rlim_cur/2;