4 * Copyright IBM, Corp. 2010
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 "hw/virtio/virtio.h"
16 #include "hw/i386/pc.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
20 #include "qemu/sockets.h"
21 #include "virtio-9p.h"
22 #include "fsdev/qemu-fsdev.h"
26 #include "migration/migration.h"
30 static int open_fd_rc;
44 ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
50 ret = virtio_pdu_vmarshal(pdu, offset, fmt, ap);
56 ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...)
62 ret = virtio_pdu_vunmarshal(pdu, offset, fmt, ap);
68 static void pdu_push_and_notify(V9fsPDU *pdu)
70 virtio_9p_push_and_notify(pdu);
73 static int omode_to_uflags(int8_t mode)
107 struct dotl_openflag_map {
112 static int dotl_to_open_flags(int flags)
116 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
117 * and P9_DOTL_NOACCESS
119 int oflags = flags & O_ACCMODE;
121 struct dotl_openflag_map dotl_oflag_map[] = {
122 { P9_DOTL_CREATE, O_CREAT },
123 { P9_DOTL_EXCL, O_EXCL },
124 { P9_DOTL_NOCTTY , O_NOCTTY },
125 { P9_DOTL_TRUNC, O_TRUNC },
126 { P9_DOTL_APPEND, O_APPEND },
127 { P9_DOTL_NONBLOCK, O_NONBLOCK } ,
128 { P9_DOTL_DSYNC, O_DSYNC },
129 { P9_DOTL_FASYNC, FASYNC },
130 { P9_DOTL_DIRECT, O_DIRECT },
131 { P9_DOTL_LARGEFILE, O_LARGEFILE },
132 { P9_DOTL_DIRECTORY, O_DIRECTORY },
133 { P9_DOTL_NOFOLLOW, O_NOFOLLOW },
134 { P9_DOTL_NOATIME, O_NOATIME },
135 { P9_DOTL_SYNC, O_SYNC },
138 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) {
139 if (flags & dotl_oflag_map[i].dotl_flag) {
140 oflags |= dotl_oflag_map[i].open_flag;
147 void cred_init(FsCred *credp)
155 static int get_dotl_openflags(V9fsState *s, int oflags)
159 * Filter the client open flags
161 flags = dotl_to_open_flags(oflags);
162 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT);
164 * Ignore direct disk access hint until the server supports it.
170 void v9fs_path_init(V9fsPath *path)
176 void v9fs_path_free(V9fsPath *path)
183 void v9fs_path_copy(V9fsPath *lhs, V9fsPath *rhs)
186 lhs->data = g_malloc(rhs->size);
187 memcpy(lhs->data, rhs->data, rhs->size);
188 lhs->size = rhs->size;
191 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath,
192 const char *name, V9fsPath *path)
195 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
203 * Return TRUE if s1 is an ancestor of s2.
205 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
206 * As a special case, We treat s1 as ancestor of s2 if they are same!
208 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2)
210 if (!strncmp(s1->data, s2->data, s1->size - 1)) {
211 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') {
218 static size_t v9fs_string_size(V9fsString *str)
224 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
225 static int v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f)
228 if (f->fid_type == P9_FID_FILE) {
229 if (f->fs.fd == -1) {
231 err = v9fs_co_open(pdu, f, f->open_flags);
232 } while (err == -EINTR && !pdu->cancelled);
234 } else if (f->fid_type == P9_FID_DIR) {
235 if (f->fs.dir == NULL) {
237 err = v9fs_co_opendir(pdu, f);
238 } while (err == -EINTR && !pdu->cancelled);
244 static V9fsFidState *get_fid(V9fsPDU *pdu, int32_t fid)
248 V9fsState *s = pdu->s;
250 for (f = s->fid_list; f; f = f->next) {
254 * Update the fid ref upfront so that
255 * we don't get reclaimed when we yield
260 * check whether we need to reopen the
261 * file. We might have closed the fd
262 * while trying to free up some file
265 err = v9fs_reopen_fid(pdu, f);
271 * Mark the fid as referenced so that the LRU
272 * reclaim won't close the file descriptor
274 f->flags |= FID_REFERENCED;
281 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid)
285 for (f = s->fid_list; f; f = f->next) {
286 /* If fid is already there return NULL */
292 f = g_malloc0(sizeof(V9fsFidState));
294 f->fid_type = P9_FID_NONE;
297 * Mark the fid as referenced so that the LRU
298 * reclaim won't close the file descriptor
300 f->flags |= FID_REFERENCED;
301 f->next = s->fid_list;
307 static int v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp)
311 if (fidp->fs.xattr.copied_len == -1) {
312 /* getxattr/listxattr fid */
316 * if this is fid for setxattr. clunk should
317 * result in setxattr localcall
319 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) {
320 /* clunk after partial write */
324 if (fidp->fs.xattr.len) {
325 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name,
326 fidp->fs.xattr.value,
328 fidp->fs.xattr.flags);
330 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name);
333 v9fs_string_free(&fidp->fs.xattr.name);
335 g_free(fidp->fs.xattr.value);
339 static int free_fid(V9fsPDU *pdu, V9fsFidState *fidp)
343 if (fidp->fid_type == P9_FID_FILE) {
344 /* If we reclaimed the fd no need to close */
345 if (fidp->fs.fd != -1) {
346 retval = v9fs_co_close(pdu, &fidp->fs);
348 } else if (fidp->fid_type == P9_FID_DIR) {
349 if (fidp->fs.dir != NULL) {
350 retval = v9fs_co_closedir(pdu, &fidp->fs);
352 } else if (fidp->fid_type == P9_FID_XATTR) {
353 retval = v9fs_xattr_fid_clunk(pdu, fidp);
355 v9fs_path_free(&fidp->path);
360 static int put_fid(V9fsPDU *pdu, V9fsFidState *fidp)
365 * Don't free the fid if it is in reclaim list
367 if (!fidp->ref && fidp->clunked) {
368 if (fidp->fid == pdu->s->root_fid) {
370 * if the clunked fid is root fid then we
371 * have unmounted the fs on the client side.
372 * delete the migration blocker. Ideally, this
373 * should be hooked to transport close notification
375 if (pdu->s->migration_blocker) {
376 migrate_del_blocker(pdu->s->migration_blocker);
377 error_free(pdu->s->migration_blocker);
378 pdu->s->migration_blocker = NULL;
381 return free_fid(pdu, fidp);
386 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid)
388 V9fsFidState **fidpp, *fidp;
390 for (fidpp = &s->fid_list; *fidpp; fidpp = &(*fidpp)->next) {
391 if ((*fidpp)->fid == fid) {
395 if (*fidpp == NULL) {
404 void v9fs_reclaim_fd(V9fsPDU *pdu)
406 int reclaim_count = 0;
407 V9fsState *s = pdu->s;
408 V9fsFidState *f, *reclaim_list = NULL;
410 for (f = s->fid_list; f; f = f->next) {
412 * Unlink fids cannot be reclaimed. Check
413 * for them and skip them. Also skip fids
414 * currently being operated on.
416 if (f->ref || f->flags & FID_NON_RECLAIMABLE) {
420 * if it is a recently referenced fid
421 * we leave the fid untouched and clear the
422 * reference bit. We come back to it later
423 * in the next iteration. (a simple LRU without
424 * moving list elements around)
426 if (f->flags & FID_REFERENCED) {
427 f->flags &= ~FID_REFERENCED;
431 * Add fids to reclaim list.
433 if (f->fid_type == P9_FID_FILE) {
434 if (f->fs.fd != -1) {
436 * Up the reference count so that
437 * a clunk request won't free this fid
440 f->rclm_lst = reclaim_list;
442 f->fs_reclaim.fd = f->fs.fd;
446 } else if (f->fid_type == P9_FID_DIR) {
447 if (f->fs.dir != NULL) {
449 * Up the reference count so that
450 * a clunk request won't free this fid
453 f->rclm_lst = reclaim_list;
455 f->fs_reclaim.dir = f->fs.dir;
460 if (reclaim_count >= open_fd_rc) {
465 * Now close the fid in reclaim list. Free them if they
466 * are already clunked.
468 while (reclaim_list) {
470 reclaim_list = f->rclm_lst;
471 if (f->fid_type == P9_FID_FILE) {
472 v9fs_co_close(pdu, &f->fs_reclaim);
473 } else if (f->fid_type == P9_FID_DIR) {
474 v9fs_co_closedir(pdu, &f->fs_reclaim);
478 * Now drop the fid reference, free it
485 static int v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path)
488 V9fsState *s = pdu->s;
489 V9fsFidState *fidp, head_fid;
491 head_fid.next = s->fid_list;
492 for (fidp = s->fid_list; fidp; fidp = fidp->next) {
493 if (fidp->path.size != path->size) {
496 if (!memcmp(fidp->path.data, path->data, path->size)) {
497 /* Mark the fid non reclaimable. */
498 fidp->flags |= FID_NON_RECLAIMABLE;
500 /* reopen the file/dir if already closed */
501 err = v9fs_reopen_fid(pdu, fidp);
506 * Go back to head of fid list because
507 * the list could have got updated when
508 * switched to the worker thread
518 static void virtfs_reset(V9fsPDU *pdu)
520 V9fsState *s = pdu->s;
521 V9fsFidState *fidp = NULL;
524 while (s->fid_list) {
526 s->fid_list = fidp->next;
535 /* One or more unclunked fids found... */
536 error_report("9pfs:%s: One or more uncluncked fids "
537 "found during reset", __func__);
541 #define P9_QID_TYPE_DIR 0x80
542 #define P9_QID_TYPE_SYMLINK 0x02
544 #define P9_STAT_MODE_DIR 0x80000000
545 #define P9_STAT_MODE_APPEND 0x40000000
546 #define P9_STAT_MODE_EXCL 0x20000000
547 #define P9_STAT_MODE_MOUNT 0x10000000
548 #define P9_STAT_MODE_AUTH 0x08000000
549 #define P9_STAT_MODE_TMP 0x04000000
550 #define P9_STAT_MODE_SYMLINK 0x02000000
551 #define P9_STAT_MODE_LINK 0x01000000
552 #define P9_STAT_MODE_DEVICE 0x00800000
553 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
554 #define P9_STAT_MODE_SOCKET 0x00100000
555 #define P9_STAT_MODE_SETUID 0x00080000
556 #define P9_STAT_MODE_SETGID 0x00040000
557 #define P9_STAT_MODE_SETVTX 0x00010000
559 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
560 P9_STAT_MODE_SYMLINK | \
561 P9_STAT_MODE_LINK | \
562 P9_STAT_MODE_DEVICE | \
563 P9_STAT_MODE_NAMED_PIPE | \
566 /* This is the algorithm from ufs in spfs */
567 static void stat_to_qid(const struct stat *stbuf, V9fsQID *qidp)
571 memset(&qidp->path, 0, sizeof(qidp->path));
572 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path));
573 memcpy(&qidp->path, &stbuf->st_ino, size);
574 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8);
576 if (S_ISDIR(stbuf->st_mode)) {
577 qidp->type |= P9_QID_TYPE_DIR;
579 if (S_ISLNK(stbuf->st_mode)) {
580 qidp->type |= P9_QID_TYPE_SYMLINK;
584 static int fid_to_qid(V9fsPDU *pdu, V9fsFidState *fidp, V9fsQID *qidp)
589 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
593 stat_to_qid(&stbuf, qidp);
597 V9fsPDU *pdu_alloc(V9fsState *s)
601 if (!QLIST_EMPTY(&s->free_list)) {
602 pdu = QLIST_FIRST(&s->free_list);
603 QLIST_REMOVE(pdu, next);
604 QLIST_INSERT_HEAD(&s->active_list, pdu, next);
609 void pdu_free(V9fsPDU *pdu)
612 V9fsState *s = pdu->s;
614 * Cancelled pdu are added back to the freelist
617 if (!pdu->cancelled) {
618 QLIST_REMOVE(pdu, next);
619 QLIST_INSERT_HEAD(&s->free_list, pdu, next);
625 * We don't do error checking for pdu_marshal/unmarshal here
626 * because we always expect to have enough space to encode
629 static void pdu_complete(V9fsPDU *pdu, ssize_t len)
631 int8_t id = pdu->id + 1; /* Response */
632 V9fsState *s = pdu->s;
638 if (s->proto_version != V9FS_PROTO_2000L) {
641 str.data = strerror(err);
642 str.size = strlen(str.data);
644 len += pdu_marshal(pdu, len, "s", &str);
648 len += pdu_marshal(pdu, len, "d", err);
650 if (s->proto_version == V9FS_PROTO_2000L) {
653 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */
656 /* fill out the header */
657 pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag);
659 /* keep these in sync */
663 pdu_push_and_notify(pdu);
665 /* Now wakeup anybody waiting in flush for this request */
666 qemu_co_queue_next(&pdu->complete);
671 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension)
676 if (mode & P9_STAT_MODE_DIR) {
680 if (mode & P9_STAT_MODE_SYMLINK) {
683 if (mode & P9_STAT_MODE_SOCKET) {
686 if (mode & P9_STAT_MODE_NAMED_PIPE) {
689 if (mode & P9_STAT_MODE_DEVICE) {
690 if (extension->size && extension->data[0] == 'c') {
701 if (mode & P9_STAT_MODE_SETUID) {
704 if (mode & P9_STAT_MODE_SETGID) {
707 if (mode & P9_STAT_MODE_SETVTX) {
714 static int donttouch_stat(V9fsStat *stat)
716 if (stat->type == -1 &&
718 stat->qid.type == -1 &&
719 stat->qid.version == -1 &&
720 stat->qid.path == -1 &&
724 stat->length == -1 &&
731 stat->n_muid == -1) {
738 static void v9fs_stat_init(V9fsStat *stat)
740 v9fs_string_init(&stat->name);
741 v9fs_string_init(&stat->uid);
742 v9fs_string_init(&stat->gid);
743 v9fs_string_init(&stat->muid);
744 v9fs_string_init(&stat->extension);
747 static void v9fs_stat_free(V9fsStat *stat)
749 v9fs_string_free(&stat->name);
750 v9fs_string_free(&stat->uid);
751 v9fs_string_free(&stat->gid);
752 v9fs_string_free(&stat->muid);
753 v9fs_string_free(&stat->extension);
756 static uint32_t stat_to_v9mode(const struct stat *stbuf)
760 mode = stbuf->st_mode & 0777;
761 if (S_ISDIR(stbuf->st_mode)) {
762 mode |= P9_STAT_MODE_DIR;
765 if (S_ISLNK(stbuf->st_mode)) {
766 mode |= P9_STAT_MODE_SYMLINK;
769 if (S_ISSOCK(stbuf->st_mode)) {
770 mode |= P9_STAT_MODE_SOCKET;
773 if (S_ISFIFO(stbuf->st_mode)) {
774 mode |= P9_STAT_MODE_NAMED_PIPE;
777 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) {
778 mode |= P9_STAT_MODE_DEVICE;
781 if (stbuf->st_mode & S_ISUID) {
782 mode |= P9_STAT_MODE_SETUID;
785 if (stbuf->st_mode & S_ISGID) {
786 mode |= P9_STAT_MODE_SETGID;
789 if (stbuf->st_mode & S_ISVTX) {
790 mode |= P9_STAT_MODE_SETVTX;
796 static int stat_to_v9stat(V9fsPDU *pdu, V9fsPath *name,
797 const struct stat *stbuf,
803 memset(v9stat, 0, sizeof(*v9stat));
805 stat_to_qid(stbuf, &v9stat->qid);
806 v9stat->mode = stat_to_v9mode(stbuf);
807 v9stat->atime = stbuf->st_atime;
808 v9stat->mtime = stbuf->st_mtime;
809 v9stat->length = stbuf->st_size;
811 v9fs_string_null(&v9stat->uid);
812 v9fs_string_null(&v9stat->gid);
813 v9fs_string_null(&v9stat->muid);
815 v9stat->n_uid = stbuf->st_uid;
816 v9stat->n_gid = stbuf->st_gid;
819 v9fs_string_null(&v9stat->extension);
821 if (v9stat->mode & P9_STAT_MODE_SYMLINK) {
822 err = v9fs_co_readlink(pdu, name, &v9stat->extension);
826 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) {
827 v9fs_string_sprintf(&v9stat->extension, "%c %u %u",
828 S_ISCHR(stbuf->st_mode) ? 'c' : 'b',
829 major(stbuf->st_rdev), minor(stbuf->st_rdev));
830 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) {
831 v9fs_string_sprintf(&v9stat->extension, "%s %lu",
832 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink);
835 str = strrchr(name->data, '/');
842 v9fs_string_sprintf(&v9stat->name, "%s", str);
845 v9fs_string_size(&v9stat->name) +
846 v9fs_string_size(&v9stat->uid) +
847 v9fs_string_size(&v9stat->gid) +
848 v9fs_string_size(&v9stat->muid) +
849 v9fs_string_size(&v9stat->extension);
853 #define P9_STATS_MODE 0x00000001ULL
854 #define P9_STATS_NLINK 0x00000002ULL
855 #define P9_STATS_UID 0x00000004ULL
856 #define P9_STATS_GID 0x00000008ULL
857 #define P9_STATS_RDEV 0x00000010ULL
858 #define P9_STATS_ATIME 0x00000020ULL
859 #define P9_STATS_MTIME 0x00000040ULL
860 #define P9_STATS_CTIME 0x00000080ULL
861 #define P9_STATS_INO 0x00000100ULL
862 #define P9_STATS_SIZE 0x00000200ULL
863 #define P9_STATS_BLOCKS 0x00000400ULL
865 #define P9_STATS_BTIME 0x00000800ULL
866 #define P9_STATS_GEN 0x00001000ULL
867 #define P9_STATS_DATA_VERSION 0x00002000ULL
869 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
870 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
873 static void stat_to_v9stat_dotl(V9fsState *s, const struct stat *stbuf,
874 V9fsStatDotl *v9lstat)
876 memset(v9lstat, 0, sizeof(*v9lstat));
878 v9lstat->st_mode = stbuf->st_mode;
879 v9lstat->st_nlink = stbuf->st_nlink;
880 v9lstat->st_uid = stbuf->st_uid;
881 v9lstat->st_gid = stbuf->st_gid;
882 v9lstat->st_rdev = stbuf->st_rdev;
883 v9lstat->st_size = stbuf->st_size;
884 v9lstat->st_blksize = stbuf->st_blksize;
885 v9lstat->st_blocks = stbuf->st_blocks;
886 v9lstat->st_atime_sec = stbuf->st_atime;
887 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec;
888 v9lstat->st_mtime_sec = stbuf->st_mtime;
889 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec;
890 v9lstat->st_ctime_sec = stbuf->st_ctime;
891 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec;
892 /* Currently we only support BASIC fields in stat */
893 v9lstat->st_result_mask = P9_STATS_BASIC;
895 stat_to_qid(stbuf, &v9lstat->qid);
898 static void print_sg(struct iovec *sg, int cnt)
902 printf("sg[%d]: {", cnt);
903 for (i = 0; i < cnt; i++) {
907 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len);
912 /* Will call this only for path name based fid */
913 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len)
916 v9fs_path_init(&str);
917 v9fs_path_copy(&str, dst);
918 v9fs_string_sprintf((V9fsString *)dst, "%s%s", src->data, str.data+len);
919 v9fs_path_free(&str);
920 /* +1 to include terminating NULL */
924 static inline bool is_ro_export(FsContext *ctx)
926 return ctx->export_flags & V9FS_RDONLY;
929 static void v9fs_version(void *opaque)
932 V9fsPDU *pdu = opaque;
933 V9fsState *s = pdu->s;
937 v9fs_string_init(&version);
938 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version);
943 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data);
947 if (!strcmp(version.data, "9P2000.u")) {
948 s->proto_version = V9FS_PROTO_2000U;
949 } else if (!strcmp(version.data, "9P2000.L")) {
950 s->proto_version = V9FS_PROTO_2000L;
952 v9fs_string_sprintf(&version, "unknown");
955 err = pdu_marshal(pdu, offset, "ds", s->msize, &version);
961 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data);
963 pdu_complete(pdu, offset);
964 v9fs_string_free(&version);
967 static void v9fs_attach(void *opaque)
969 V9fsPDU *pdu = opaque;
970 V9fsState *s = pdu->s;
971 int32_t fid, afid, n_uname;
972 V9fsString uname, aname;
978 v9fs_string_init(&uname);
979 v9fs_string_init(&aname);
980 err = pdu_unmarshal(pdu, offset, "ddssd", &fid,
981 &afid, &uname, &aname, &n_uname);
985 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data);
987 fidp = alloc_fid(s, fid);
993 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path);
999 err = fid_to_qid(pdu, fidp, &qid);
1005 err = pdu_marshal(pdu, offset, "Q", &qid);
1011 trace_v9fs_attach_return(pdu->tag, pdu->id,
1012 qid.type, qid.version, qid.path);
1014 * disable migration if we haven't done already.
1015 * attach could get called multiple times for the same export.
1017 if (!s->migration_blocker) {
1019 error_setg(&s->migration_blocker,
1020 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1021 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag);
1022 migrate_add_blocker(s->migration_blocker);
1027 pdu_complete(pdu, err);
1028 v9fs_string_free(&uname);
1029 v9fs_string_free(&aname);
1032 static void v9fs_stat(void *opaque)
1040 V9fsPDU *pdu = opaque;
1042 err = pdu_unmarshal(pdu, offset, "d", &fid);
1046 trace_v9fs_stat(pdu->tag, pdu->id, fid);
1048 fidp = get_fid(pdu, fid);
1053 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1057 err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat);
1061 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat);
1063 v9fs_stat_free(&v9stat);
1066 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode,
1067 v9stat.atime, v9stat.mtime, v9stat.length);
1069 v9fs_stat_free(&v9stat);
1073 pdu_complete(pdu, err);
1076 static void v9fs_getattr(void *opaque)
1083 uint64_t request_mask;
1084 V9fsStatDotl v9stat_dotl;
1085 V9fsPDU *pdu = opaque;
1086 V9fsState *s = pdu->s;
1088 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask);
1092 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask);
1094 fidp = get_fid(pdu, fid);
1100 * Currently we only support BASIC fields in stat, so there is no
1101 * need to look at request_mask.
1103 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1107 stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl);
1109 /* fill st_gen if requested and supported by underlying fs */
1110 if (request_mask & P9_STATS_GEN) {
1111 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl);
1114 /* we have valid st_gen: update result mask */
1115 v9stat_dotl.st_result_mask |= P9_STATS_GEN;
1118 /* request cancelled, e.g. by Tflush */
1121 /* failed to get st_gen: not fatal, ignore */
1125 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl);
1130 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask,
1131 v9stat_dotl.st_mode, v9stat_dotl.st_uid,
1132 v9stat_dotl.st_gid);
1136 pdu_complete(pdu, retval);
1139 /* Attribute flags */
1140 #define P9_ATTR_MODE (1 << 0)
1141 #define P9_ATTR_UID (1 << 1)
1142 #define P9_ATTR_GID (1 << 2)
1143 #define P9_ATTR_SIZE (1 << 3)
1144 #define P9_ATTR_ATIME (1 << 4)
1145 #define P9_ATTR_MTIME (1 << 5)
1146 #define P9_ATTR_CTIME (1 << 6)
1147 #define P9_ATTR_ATIME_SET (1 << 7)
1148 #define P9_ATTR_MTIME_SET (1 << 8)
1150 #define P9_ATTR_MASK 127
1152 static void v9fs_setattr(void *opaque)
1159 V9fsPDU *pdu = opaque;
1161 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr);
1166 fidp = get_fid(pdu, fid);
1171 if (v9iattr.valid & P9_ATTR_MODE) {
1172 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode);
1177 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) {
1178 struct timespec times[2];
1179 if (v9iattr.valid & P9_ATTR_ATIME) {
1180 if (v9iattr.valid & P9_ATTR_ATIME_SET) {
1181 times[0].tv_sec = v9iattr.atime_sec;
1182 times[0].tv_nsec = v9iattr.atime_nsec;
1184 times[0].tv_nsec = UTIME_NOW;
1187 times[0].tv_nsec = UTIME_OMIT;
1189 if (v9iattr.valid & P9_ATTR_MTIME) {
1190 if (v9iattr.valid & P9_ATTR_MTIME_SET) {
1191 times[1].tv_sec = v9iattr.mtime_sec;
1192 times[1].tv_nsec = v9iattr.mtime_nsec;
1194 times[1].tv_nsec = UTIME_NOW;
1197 times[1].tv_nsec = UTIME_OMIT;
1199 err = v9fs_co_utimensat(pdu, &fidp->path, times);
1205 * If the only valid entry in iattr is ctime we can call
1206 * chown(-1,-1) to update the ctime of the file
1208 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) ||
1209 ((v9iattr.valid & P9_ATTR_CTIME)
1210 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) {
1211 if (!(v9iattr.valid & P9_ATTR_UID)) {
1214 if (!(v9iattr.valid & P9_ATTR_GID)) {
1217 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid,
1223 if (v9iattr.valid & (P9_ATTR_SIZE)) {
1224 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size);
1233 pdu_complete(pdu, err);
1236 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids)
1242 err = pdu_marshal(pdu, offset, "w", nwnames);
1247 for (i = 0; i < nwnames; i++) {
1248 err = pdu_marshal(pdu, offset, "Q", &qids[i]);
1257 static void v9fs_walk(void *opaque)
1260 V9fsQID *qids = NULL;
1262 V9fsPath dpath, path;
1266 int32_t fid, newfid;
1267 V9fsString *wnames = NULL;
1269 V9fsFidState *newfidp = NULL;
1270 V9fsPDU *pdu = opaque;
1271 V9fsState *s = pdu->s;
1273 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames);
1275 pdu_complete(pdu, err);
1280 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames);
1282 if (nwnames && nwnames <= P9_MAXWELEM) {
1283 wnames = g_malloc0(sizeof(wnames[0]) * nwnames);
1284 qids = g_malloc0(sizeof(qids[0]) * nwnames);
1285 for (i = 0; i < nwnames; i++) {
1286 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]);
1292 } else if (nwnames > P9_MAXWELEM) {
1296 fidp = get_fid(pdu, fid);
1301 v9fs_path_init(&dpath);
1302 v9fs_path_init(&path);
1304 * Both dpath and path initially poin to fidp.
1305 * Needed to handle request with nwnames == 0
1307 v9fs_path_copy(&dpath, &fidp->path);
1308 v9fs_path_copy(&path, &fidp->path);
1309 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1310 err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, &path);
1314 err = v9fs_co_lstat(pdu, &path, &stbuf);
1318 stat_to_qid(&stbuf, &qids[name_idx]);
1319 v9fs_path_copy(&dpath, &path);
1321 if (fid == newfid) {
1322 BUG_ON(fidp->fid_type != P9_FID_NONE);
1323 v9fs_path_copy(&fidp->path, &path);
1325 newfidp = alloc_fid(s, newfid);
1326 if (newfidp == NULL) {
1330 newfidp->uid = fidp->uid;
1331 v9fs_path_copy(&newfidp->path, &path);
1333 err = v9fs_walk_marshal(pdu, nwnames, qids);
1334 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids);
1338 put_fid(pdu, newfidp);
1340 v9fs_path_free(&dpath);
1341 v9fs_path_free(&path);
1343 pdu_complete(pdu, err);
1344 if (nwnames && nwnames <= P9_MAXWELEM) {
1345 for (name_idx = 0; name_idx < nwnames; name_idx++) {
1346 v9fs_string_free(&wnames[name_idx]);
1353 static int32_t get_iounit(V9fsPDU *pdu, V9fsPath *path)
1355 struct statfs stbuf;
1357 V9fsState *s = pdu->s;
1360 * iounit should be multiples of f_bsize (host filesystem block size
1361 * and as well as less than (client msize - P9_IOHDRSZ))
1363 if (!v9fs_co_statfs(pdu, path, &stbuf)) {
1364 iounit = stbuf.f_bsize;
1365 iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize;
1368 iounit = s->msize - P9_IOHDRSZ;
1373 static void v9fs_open(void *opaque)
1384 V9fsPDU *pdu = opaque;
1385 V9fsState *s = pdu->s;
1387 if (s->proto_version == V9FS_PROTO_2000L) {
1388 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode);
1391 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte);
1397 trace_v9fs_open(pdu->tag, pdu->id, fid, mode);
1399 fidp = get_fid(pdu, fid);
1404 BUG_ON(fidp->fid_type != P9_FID_NONE);
1406 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
1410 stat_to_qid(&stbuf, &qid);
1411 if (S_ISDIR(stbuf.st_mode)) {
1412 err = v9fs_co_opendir(pdu, fidp);
1416 fidp->fid_type = P9_FID_DIR;
1417 err = pdu_marshal(pdu, offset, "Qd", &qid, 0);
1423 if (s->proto_version == V9FS_PROTO_2000L) {
1424 flags = get_dotl_openflags(s, mode);
1426 flags = omode_to_uflags(mode);
1428 if (is_ro_export(&s->ctx)) {
1429 if (mode & O_WRONLY || mode & O_RDWR ||
1430 mode & O_APPEND || mode & O_TRUNC) {
1435 err = v9fs_co_open(pdu, fidp, flags);
1439 fidp->fid_type = P9_FID_FILE;
1440 fidp->open_flags = flags;
1441 if (flags & O_EXCL) {
1443 * We let the host file system do O_EXCL check
1444 * We should not reclaim such fd
1446 fidp->flags |= FID_NON_RECLAIMABLE;
1448 iounit = get_iounit(pdu, &fidp->path);
1449 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1455 trace_v9fs_open_return(pdu->tag, pdu->id,
1456 qid.type, qid.version, qid.path, iounit);
1460 pdu_complete(pdu, err);
1463 static void v9fs_lcreate(void *opaque)
1465 int32_t dfid, flags, mode;
1474 V9fsPDU *pdu = opaque;
1476 v9fs_string_init(&name);
1477 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid,
1478 &name, &flags, &mode, &gid);
1482 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid);
1484 fidp = get_fid(pdu, dfid);
1490 flags = get_dotl_openflags(pdu->s, flags);
1491 err = v9fs_co_open2(pdu, fidp, &name, gid,
1492 flags | O_CREAT, mode, &stbuf);
1496 fidp->fid_type = P9_FID_FILE;
1497 fidp->open_flags = flags;
1498 if (flags & O_EXCL) {
1500 * We let the host file system do O_EXCL check
1501 * We should not reclaim such fd
1503 fidp->flags |= FID_NON_RECLAIMABLE;
1505 iounit = get_iounit(pdu, &fidp->path);
1506 stat_to_qid(&stbuf, &qid);
1507 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
1512 trace_v9fs_lcreate_return(pdu->tag, pdu->id,
1513 qid.type, qid.version, qid.path, iounit);
1517 pdu_complete(pdu, err);
1518 v9fs_string_free(&name);
1521 static void v9fs_fsync(void *opaque)
1528 V9fsPDU *pdu = opaque;
1530 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync);
1534 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync);
1536 fidp = get_fid(pdu, fid);
1541 err = v9fs_co_fsync(pdu, fidp, datasync);
1547 pdu_complete(pdu, err);
1550 static void v9fs_clunk(void *opaque)
1556 V9fsPDU *pdu = opaque;
1557 V9fsState *s = pdu->s;
1559 err = pdu_unmarshal(pdu, offset, "d", &fid);
1563 trace_v9fs_clunk(pdu->tag, pdu->id, fid);
1565 fidp = clunk_fid(s, fid);
1571 * Bump the ref so that put_fid will
1575 err = put_fid(pdu, fidp);
1580 pdu_complete(pdu, err);
1583 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1584 uint64_t off, uint32_t max_count)
1590 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
1591 VirtQueueElement *elem = v->elems[pdu->idx];
1593 xattr_len = fidp->fs.xattr.len;
1594 read_count = xattr_len - off;
1595 if (read_count > max_count) {
1596 read_count = max_count;
1597 } else if (read_count < 0) {
1599 * read beyond XATTR value
1603 err = pdu_marshal(pdu, offset, "d", read_count);
1609 err = v9fs_pack(elem->in_sg, elem->in_num, offset,
1610 ((char *)fidp->fs.xattr.value) + off,
1619 static int v9fs_do_readdir_with_stat(V9fsPDU *pdu,
1620 V9fsFidState *fidp, uint32_t max_count)
1627 off_t saved_dir_pos;
1628 struct dirent *dent, *result;
1630 /* save the directory position */
1631 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1632 if (saved_dir_pos < 0) {
1633 return saved_dir_pos;
1636 dent = g_malloc(sizeof(struct dirent));
1639 v9fs_path_init(&path);
1640 err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
1641 if (err || !result) {
1644 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path);
1648 err = v9fs_co_lstat(pdu, &path, &stbuf);
1652 err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat);
1656 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1657 len = pdu_marshal(pdu, 11 + count, "S", &v9stat);
1658 if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) {
1659 /* Ran out of buffer. Set dir back to old position and return */
1660 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1661 v9fs_stat_free(&v9stat);
1662 v9fs_path_free(&path);
1667 v9fs_stat_free(&v9stat);
1668 v9fs_path_free(&path);
1669 saved_dir_pos = dent->d_off;
1673 v9fs_path_free(&path);
1681 * Create a QEMUIOVector for a sub-region of PDU iovecs
1683 * @qiov: uninitialized QEMUIOVector
1684 * @skip: number of bytes to skip from beginning of PDU
1685 * @size: number of bytes to include
1686 * @is_write: true - write, false - read
1688 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1689 * with qemu_iovec_destroy().
1691 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu,
1692 size_t skip, size_t size,
1699 virtio_init_iov_from_pdu(pdu, &iov, &niov, is_write);
1701 qemu_iovec_init_external(&elem, iov, niov);
1702 qemu_iovec_init(qiov, niov);
1703 qemu_iovec_concat(qiov, &elem, skip, size);
1706 static void v9fs_read(void *opaque)
1715 V9fsPDU *pdu = opaque;
1716 V9fsState *s = pdu->s;
1718 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count);
1722 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count);
1724 fidp = get_fid(pdu, fid);
1729 if (fidp->fid_type == P9_FID_DIR) {
1732 v9fs_co_rewinddir(pdu, fidp);
1734 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count);
1739 err = pdu_marshal(pdu, offset, "d", count);
1743 err += offset + count;
1744 } else if (fidp->fid_type == P9_FID_FILE) {
1745 QEMUIOVector qiov_full;
1749 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false);
1750 qemu_iovec_init(&qiov, qiov_full.niov);
1752 qemu_iovec_reset(&qiov);
1753 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count);
1755 print_sg(qiov.iov, qiov.niov);
1757 /* Loop in case of EINTR */
1759 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off);
1764 } while (len == -EINTR && !pdu->cancelled);
1766 /* IO error return the error */
1770 } while (count < max_count && len > 0);
1771 err = pdu_marshal(pdu, offset, "d", count);
1775 err += offset + count;
1776 qemu_iovec_destroy(&qiov);
1777 qemu_iovec_destroy(&qiov_full);
1778 } else if (fidp->fid_type == P9_FID_XATTR) {
1779 err = v9fs_xattr_read(s, pdu, fidp, off, max_count);
1783 trace_v9fs_read_return(pdu->tag, pdu->id, count, err);
1787 pdu_complete(pdu, err);
1790 static size_t v9fs_readdir_data_size(V9fsString *name)
1793 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1794 * size of type (1) + size of name.size (2) + strlen(name.data)
1796 return 24 + v9fs_string_size(name);
1799 static int v9fs_do_readdir(V9fsPDU *pdu,
1800 V9fsFidState *fidp, int32_t max_count)
1807 off_t saved_dir_pos;
1808 struct dirent *dent, *result;
1810 /* save the directory position */
1811 saved_dir_pos = v9fs_co_telldir(pdu, fidp);
1812 if (saved_dir_pos < 0) {
1813 return saved_dir_pos;
1816 dent = g_malloc(sizeof(struct dirent));
1819 err = v9fs_co_readdir_r(pdu, fidp, dent, &result);
1820 if (err || !result) {
1823 v9fs_string_init(&name);
1824 v9fs_string_sprintf(&name, "%s", dent->d_name);
1825 if ((count + v9fs_readdir_data_size(&name)) > max_count) {
1826 /* Ran out of buffer. Set dir back to old position and return */
1827 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1828 v9fs_string_free(&name);
1833 * Fill up just the path field of qid because the client uses
1834 * only that. To fill the entire qid structure we will have
1835 * to stat each dirent found, which is expensive
1837 size = MIN(sizeof(dent->d_ino), sizeof(qid.path));
1838 memcpy(&qid.path, &dent->d_ino, size);
1839 /* Fill the other fields with dummy values */
1843 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1844 len = pdu_marshal(pdu, 11 + count, "Qqbs",
1846 dent->d_type, &name);
1848 v9fs_co_seekdir(pdu, fidp, saved_dir_pos);
1849 v9fs_string_free(&name);
1854 v9fs_string_free(&name);
1855 saved_dir_pos = dent->d_off;
1864 static void v9fs_readdir(void *opaque)
1870 uint64_t initial_offset;
1873 V9fsPDU *pdu = opaque;
1875 retval = pdu_unmarshal(pdu, offset, "dqd", &fid,
1876 &initial_offset, &max_count);
1880 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count);
1882 fidp = get_fid(pdu, fid);
1887 if (!fidp->fs.dir) {
1891 if (initial_offset == 0) {
1892 v9fs_co_rewinddir(pdu, fidp);
1894 v9fs_co_seekdir(pdu, fidp, initial_offset);
1896 count = v9fs_do_readdir(pdu, fidp, max_count);
1901 retval = pdu_marshal(pdu, offset, "d", count);
1905 retval += count + offset;
1906 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval);
1910 pdu_complete(pdu, retval);
1913 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp,
1914 uint64_t off, uint32_t count,
1915 struct iovec *sg, int cnt)
1924 xattr_len = fidp->fs.xattr.len;
1925 write_count = xattr_len - off;
1926 if (write_count > count) {
1927 write_count = count;
1928 } else if (write_count < 0) {
1930 * write beyond XATTR value len specified in
1936 err = pdu_marshal(pdu, offset, "d", write_count);
1941 fidp->fs.xattr.copied_len += write_count;
1943 * Now copy the content from sg list
1945 for (i = 0; i < cnt; i++) {
1946 if (write_count > sg[i].iov_len) {
1947 to_copy = sg[i].iov_len;
1949 to_copy = write_count;
1951 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy);
1952 /* updating vs->off since we are not using below */
1954 write_count -= to_copy;
1960 static void v9fs_write(void *opaque)
1970 V9fsPDU *pdu = opaque;
1971 V9fsState *s = pdu->s;
1972 QEMUIOVector qiov_full;
1975 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count);
1977 pdu_complete(pdu, err);
1981 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true);
1982 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov);
1984 fidp = get_fid(pdu, fid);
1989 if (fidp->fid_type == P9_FID_FILE) {
1990 if (fidp->fs.fd == -1) {
1994 } else if (fidp->fid_type == P9_FID_XATTR) {
1996 * setxattr operation
1998 err = v9fs_xattr_write(s, pdu, fidp, off, count,
1999 qiov_full.iov, qiov_full.niov);
2005 qemu_iovec_init(&qiov, qiov_full.niov);
2007 qemu_iovec_reset(&qiov);
2008 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total);
2010 print_sg(qiov.iov, qiov.niov);
2012 /* Loop in case of EINTR */
2014 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off);
2019 } while (len == -EINTR && !pdu->cancelled);
2021 /* IO error return the error */
2025 } while (total < count && len > 0);
2028 err = pdu_marshal(pdu, offset, "d", total);
2033 trace_v9fs_write_return(pdu->tag, pdu->id, total, err);
2035 qemu_iovec_destroy(&qiov);
2039 qemu_iovec_destroy(&qiov_full);
2040 pdu_complete(pdu, err);
2043 static void v9fs_create(void *opaque)
2055 V9fsString extension;
2057 V9fsPDU *pdu = opaque;
2059 v9fs_path_init(&path);
2060 v9fs_string_init(&name);
2061 v9fs_string_init(&extension);
2062 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name,
2063 &perm, &mode, &extension);
2067 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode);
2069 fidp = get_fid(pdu, fid);
2074 if (perm & P9_STAT_MODE_DIR) {
2075 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777,
2076 fidp->uid, -1, &stbuf);
2080 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2084 v9fs_path_copy(&fidp->path, &path);
2085 err = v9fs_co_opendir(pdu, fidp);
2089 fidp->fid_type = P9_FID_DIR;
2090 } else if (perm & P9_STAT_MODE_SYMLINK) {
2091 err = v9fs_co_symlink(pdu, fidp, &name,
2092 extension.data, -1 , &stbuf);
2096 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2100 v9fs_path_copy(&fidp->path, &path);
2101 } else if (perm & P9_STAT_MODE_LINK) {
2102 int32_t ofid = atoi(extension.data);
2103 V9fsFidState *ofidp = get_fid(pdu, ofid);
2104 if (ofidp == NULL) {
2108 err = v9fs_co_link(pdu, ofidp, fidp, &name);
2109 put_fid(pdu, ofidp);
2113 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2115 fidp->fid_type = P9_FID_NONE;
2118 v9fs_path_copy(&fidp->path, &path);
2119 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2121 fidp->fid_type = P9_FID_NONE;
2124 } else if (perm & P9_STAT_MODE_DEVICE) {
2126 uint32_t major, minor;
2129 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) {
2146 nmode |= perm & 0777;
2147 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2148 makedev(major, minor), nmode, &stbuf);
2152 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2156 v9fs_path_copy(&fidp->path, &path);
2157 } else if (perm & P9_STAT_MODE_NAMED_PIPE) {
2158 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2159 0, S_IFIFO | (perm & 0777), &stbuf);
2163 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2167 v9fs_path_copy(&fidp->path, &path);
2168 } else if (perm & P9_STAT_MODE_SOCKET) {
2169 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1,
2170 0, S_IFSOCK | (perm & 0777), &stbuf);
2174 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path);
2178 v9fs_path_copy(&fidp->path, &path);
2180 err = v9fs_co_open2(pdu, fidp, &name, -1,
2181 omode_to_uflags(mode)|O_CREAT, perm, &stbuf);
2185 fidp->fid_type = P9_FID_FILE;
2186 fidp->open_flags = omode_to_uflags(mode);
2187 if (fidp->open_flags & O_EXCL) {
2189 * We let the host file system do O_EXCL check
2190 * We should not reclaim such fd
2192 fidp->flags |= FID_NON_RECLAIMABLE;
2195 iounit = get_iounit(pdu, &fidp->path);
2196 stat_to_qid(&stbuf, &qid);
2197 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit);
2202 trace_v9fs_create_return(pdu->tag, pdu->id,
2203 qid.type, qid.version, qid.path, iounit);
2207 pdu_complete(pdu, err);
2208 v9fs_string_free(&name);
2209 v9fs_string_free(&extension);
2210 v9fs_path_free(&path);
2213 static void v9fs_symlink(void *opaque)
2215 V9fsPDU *pdu = opaque;
2218 V9fsFidState *dfidp;
2226 v9fs_string_init(&name);
2227 v9fs_string_init(&symname);
2228 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid);
2232 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid);
2234 dfidp = get_fid(pdu, dfid);
2235 if (dfidp == NULL) {
2239 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf);
2243 stat_to_qid(&stbuf, &qid);
2244 err = pdu_marshal(pdu, offset, "Q", &qid);
2249 trace_v9fs_symlink_return(pdu->tag, pdu->id,
2250 qid.type, qid.version, qid.path);
2252 put_fid(pdu, dfidp);
2254 pdu_complete(pdu, err);
2255 v9fs_string_free(&name);
2256 v9fs_string_free(&symname);
2259 static void v9fs_flush(void *opaque)
2264 V9fsPDU *cancel_pdu;
2265 V9fsPDU *pdu = opaque;
2266 V9fsState *s = pdu->s;
2268 err = pdu_unmarshal(pdu, offset, "w", &tag);
2270 pdu_complete(pdu, err);
2273 trace_v9fs_flush(pdu->tag, pdu->id, tag);
2275 QLIST_FOREACH(cancel_pdu, &s->active_list, next) {
2276 if (cancel_pdu->tag == tag) {
2281 cancel_pdu->cancelled = 1;
2283 * Wait for pdu to complete.
2285 qemu_co_queue_wait(&cancel_pdu->complete);
2286 cancel_pdu->cancelled = 0;
2287 pdu_free(cancel_pdu);
2289 pdu_complete(pdu, 7);
2292 static void v9fs_link(void *opaque)
2294 V9fsPDU *pdu = opaque;
2295 int32_t dfid, oldfid;
2296 V9fsFidState *dfidp, *oldfidp;
2301 v9fs_string_init(&name);
2302 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name);
2306 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data);
2308 dfidp = get_fid(pdu, dfid);
2309 if (dfidp == NULL) {
2314 oldfidp = get_fid(pdu, oldfid);
2315 if (oldfidp == NULL) {
2319 err = v9fs_co_link(pdu, oldfidp, dfidp, &name);
2324 put_fid(pdu, dfidp);
2326 v9fs_string_free(&name);
2327 pdu_complete(pdu, err);
2330 /* Only works with path name based fid */
2331 static void v9fs_remove(void *opaque)
2337 V9fsPDU *pdu = opaque;
2339 err = pdu_unmarshal(pdu, offset, "d", &fid);
2343 trace_v9fs_remove(pdu->tag, pdu->id, fid);
2345 fidp = get_fid(pdu, fid);
2350 /* if fs driver is not path based, return EOPNOTSUPP */
2351 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2356 * IF the file is unlinked, we cannot reopen
2357 * the file later. So don't reclaim fd
2359 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path);
2363 err = v9fs_co_remove(pdu, &fidp->path);
2368 /* For TREMOVE we need to clunk the fid even on failed remove */
2369 clunk_fid(pdu->s, fidp->fid);
2372 pdu_complete(pdu, err);
2375 static void v9fs_unlinkat(void *opaque)
2379 int32_t dfid, flags;
2382 V9fsFidState *dfidp;
2383 V9fsPDU *pdu = opaque;
2385 v9fs_string_init(&name);
2386 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags);
2390 dfidp = get_fid(pdu, dfid);
2391 if (dfidp == NULL) {
2396 * IF the file is unlinked, we cannot reopen
2397 * the file later. So don't reclaim fd
2399 v9fs_path_init(&path);
2400 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path);
2404 err = v9fs_mark_fids_unreclaim(pdu, &path);
2408 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags);
2413 put_fid(pdu, dfidp);
2414 v9fs_path_free(&path);
2416 pdu_complete(pdu, err);
2417 v9fs_string_free(&name);
2421 /* Only works with path name based fid */
2422 static int v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp,
2423 int32_t newdirfid, V9fsString *name)
2428 V9fsFidState *tfidp;
2429 V9fsState *s = pdu->s;
2430 V9fsFidState *dirfidp = NULL;
2431 char *old_name, *new_name;
2433 v9fs_path_init(&new_path);
2434 if (newdirfid != -1) {
2435 dirfidp = get_fid(pdu, newdirfid);
2436 if (dirfidp == NULL) {
2440 BUG_ON(dirfidp->fid_type != P9_FID_NONE);
2441 v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path);
2443 old_name = fidp->path.data;
2444 end = strrchr(old_name, '/');
2450 new_name = g_malloc0(end - old_name + name->size + 1);
2451 strncat(new_name, old_name, end - old_name);
2452 strncat(new_name + (end - old_name), name->data, name->size);
2453 v9fs_co_name_to_path(pdu, NULL, new_name, &new_path);
2456 err = v9fs_co_rename(pdu, &fidp->path, &new_path);
2461 * Fixup fid's pointing to the old name to
2462 * start pointing to the new name
2464 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2465 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) {
2466 /* replace the name */
2467 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data));
2472 put_fid(pdu, dirfidp);
2474 v9fs_path_free(&new_path);
2479 /* Only works with path name based fid */
2480 static void v9fs_rename(void *opaque)
2488 V9fsPDU *pdu = opaque;
2489 V9fsState *s = pdu->s;
2491 v9fs_string_init(&name);
2492 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name);
2496 fidp = get_fid(pdu, fid);
2501 BUG_ON(fidp->fid_type != P9_FID_NONE);
2502 /* if fs driver is not path based, return EOPNOTSUPP */
2503 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) {
2507 v9fs_path_write_lock(s);
2508 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name);
2509 v9fs_path_unlock(s);
2516 pdu_complete(pdu, err);
2517 v9fs_string_free(&name);
2520 static void v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir,
2521 V9fsString *old_name, V9fsPath *newdir,
2522 V9fsString *new_name)
2524 V9fsFidState *tfidp;
2525 V9fsPath oldpath, newpath;
2526 V9fsState *s = pdu->s;
2529 v9fs_path_init(&oldpath);
2530 v9fs_path_init(&newpath);
2531 v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath);
2532 v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath);
2535 * Fixup fid's pointing to the old name to
2536 * start pointing to the new name
2538 for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) {
2539 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) {
2540 /* replace the name */
2541 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data));
2544 v9fs_path_free(&oldpath);
2545 v9fs_path_free(&newpath);
2548 static int v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid,
2549 V9fsString *old_name, int32_t newdirfid,
2550 V9fsString *new_name)
2553 V9fsState *s = pdu->s;
2554 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL;
2556 olddirfidp = get_fid(pdu, olddirfid);
2557 if (olddirfidp == NULL) {
2561 if (newdirfid != -1) {
2562 newdirfidp = get_fid(pdu, newdirfid);
2563 if (newdirfidp == NULL) {
2568 newdirfidp = get_fid(pdu, olddirfid);
2571 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name,
2572 &newdirfidp->path, new_name);
2576 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
2577 /* Only for path based fid we need to do the below fixup */
2578 v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name,
2579 &newdirfidp->path, new_name);
2583 put_fid(pdu, olddirfidp);
2586 put_fid(pdu, newdirfidp);
2591 static void v9fs_renameat(void *opaque)
2595 V9fsPDU *pdu = opaque;
2596 V9fsState *s = pdu->s;
2597 int32_t olddirfid, newdirfid;
2598 V9fsString old_name, new_name;
2600 v9fs_string_init(&old_name);
2601 v9fs_string_init(&new_name);
2602 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid,
2603 &old_name, &newdirfid, &new_name);
2608 v9fs_path_write_lock(s);
2609 err = v9fs_complete_renameat(pdu, olddirfid,
2610 &old_name, newdirfid, &new_name);
2611 v9fs_path_unlock(s);
2617 pdu_complete(pdu, err);
2618 v9fs_string_free(&old_name);
2619 v9fs_string_free(&new_name);
2622 static void v9fs_wstat(void *opaque)
2631 V9fsPDU *pdu = opaque;
2633 v9fs_stat_init(&v9stat);
2634 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat);
2638 trace_v9fs_wstat(pdu->tag, pdu->id, fid,
2639 v9stat.mode, v9stat.atime, v9stat.mtime);
2641 fidp = get_fid(pdu, fid);
2646 /* do we need to sync the file? */
2647 if (donttouch_stat(&v9stat)) {
2648 err = v9fs_co_fsync(pdu, fidp, 0);
2651 if (v9stat.mode != -1) {
2653 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
2657 v9_mode = stat_to_v9mode(&stbuf);
2658 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) !=
2659 (v9_mode & P9_STAT_MODE_TYPE_BITS)) {
2660 /* Attempting to change the type */
2664 err = v9fs_co_chmod(pdu, &fidp->path,
2665 v9mode_to_mode(v9stat.mode,
2666 &v9stat.extension));
2671 if (v9stat.mtime != -1 || v9stat.atime != -1) {
2672 struct timespec times[2];
2673 if (v9stat.atime != -1) {
2674 times[0].tv_sec = v9stat.atime;
2675 times[0].tv_nsec = 0;
2677 times[0].tv_nsec = UTIME_OMIT;
2679 if (v9stat.mtime != -1) {
2680 times[1].tv_sec = v9stat.mtime;
2681 times[1].tv_nsec = 0;
2683 times[1].tv_nsec = UTIME_OMIT;
2685 err = v9fs_co_utimensat(pdu, &fidp->path, times);
2690 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) {
2691 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid);
2696 if (v9stat.name.size != 0) {
2697 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name);
2702 if (v9stat.length != -1) {
2703 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length);
2712 v9fs_stat_free(&v9stat);
2713 pdu_complete(pdu, err);
2716 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf)
2728 int32_t bsize_factor;
2731 * compute bsize factor based on host file system block size
2734 bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize;
2735 if (!bsize_factor) {
2738 f_type = stbuf->f_type;
2739 f_bsize = stbuf->f_bsize;
2740 f_bsize *= bsize_factor;
2742 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2743 * adjust(divide) the number of blocks, free blocks and available
2744 * blocks by bsize factor
2746 f_blocks = stbuf->f_blocks/bsize_factor;
2747 f_bfree = stbuf->f_bfree/bsize_factor;
2748 f_bavail = stbuf->f_bavail/bsize_factor;
2749 f_files = stbuf->f_files;
2750 f_ffree = stbuf->f_ffree;
2751 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] |
2752 (unsigned long long)stbuf->f_fsid.__val[1] << 32;
2753 f_namelen = stbuf->f_namelen;
2755 return pdu_marshal(pdu, offset, "ddqqqqqqd",
2756 f_type, f_bsize, f_blocks, f_bfree,
2757 f_bavail, f_files, f_ffree,
2758 fsid_val, f_namelen);
2761 static void v9fs_statfs(void *opaque)
2767 struct statfs stbuf;
2768 V9fsPDU *pdu = opaque;
2769 V9fsState *s = pdu->s;
2771 retval = pdu_unmarshal(pdu, offset, "d", &fid);
2775 fidp = get_fid(pdu, fid);
2780 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf);
2784 retval = v9fs_fill_statfs(s, pdu, &stbuf);
2792 pdu_complete(pdu, retval);
2795 static void v9fs_mknod(void *opaque)
2808 V9fsPDU *pdu = opaque;
2810 v9fs_string_init(&name);
2811 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode,
2812 &major, &minor, &gid);
2816 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor);
2818 fidp = get_fid(pdu, fid);
2823 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid,
2824 makedev(major, minor), mode, &stbuf);
2828 stat_to_qid(&stbuf, &qid);
2829 err = pdu_marshal(pdu, offset, "Q", &qid);
2834 trace_v9fs_mknod_return(pdu->tag, pdu->id,
2835 qid.type, qid.version, qid.path);
2839 pdu_complete(pdu, err);
2840 v9fs_string_free(&name);
2844 * Implement posix byte range locking code
2845 * Server side handling of locking code is very simple, because 9p server in
2846 * QEMU can handle only one client. And most of the lock handling
2847 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2848 * do any thing in * qemu 9p server side lock code path.
2849 * So when a TLOCK request comes, always return success
2851 static void v9fs_lock(void *opaque)
2858 int32_t fid, err = 0;
2859 V9fsPDU *pdu = opaque;
2861 status = P9_LOCK_ERROR;
2862 v9fs_string_init(&flock.client_id);
2863 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type,
2864 &flock.flags, &flock.start, &flock.length,
2865 &flock.proc_id, &flock.client_id);
2869 trace_v9fs_lock(pdu->tag, pdu->id, fid,
2870 flock.type, flock.start, flock.length);
2873 /* We support only block flag now (that too ignored currently) */
2874 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) {
2878 fidp = get_fid(pdu, fid);
2883 err = v9fs_co_fstat(pdu, fidp, &stbuf);
2887 status = P9_LOCK_SUCCESS;
2891 err = pdu_marshal(pdu, offset, "b", status);
2895 trace_v9fs_lock_return(pdu->tag, pdu->id, status);
2896 pdu_complete(pdu, err);
2897 v9fs_string_free(&flock.client_id);
2901 * When a TGETLOCK request comes, always return success because all lock
2902 * handling is done by client's VFS layer.
2904 static void v9fs_getlock(void *opaque)
2910 int32_t fid, err = 0;
2911 V9fsPDU *pdu = opaque;
2913 v9fs_string_init(&glock.client_id);
2914 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type,
2915 &glock.start, &glock.length, &glock.proc_id,
2920 trace_v9fs_getlock(pdu->tag, pdu->id, fid,
2921 glock.type, glock.start, glock.length);
2923 fidp = get_fid(pdu, fid);
2928 err = v9fs_co_fstat(pdu, fidp, &stbuf);
2932 glock.type = P9_LOCK_TYPE_UNLCK;
2933 err = pdu_marshal(pdu, offset, "bqqds", glock.type,
2934 glock.start, glock.length, glock.proc_id,
2940 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start,
2941 glock.length, glock.proc_id);
2945 pdu_complete(pdu, err);
2946 v9fs_string_free(&glock.client_id);
2949 static void v9fs_mkdir(void *opaque)
2951 V9fsPDU *pdu = opaque;
2962 v9fs_string_init(&name);
2963 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid);
2967 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid);
2969 fidp = get_fid(pdu, fid);
2974 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf);
2978 stat_to_qid(&stbuf, &qid);
2979 err = pdu_marshal(pdu, offset, "Q", &qid);
2984 trace_v9fs_mkdir_return(pdu->tag, pdu->id,
2985 qid.type, qid.version, qid.path, err);
2989 pdu_complete(pdu, err);
2990 v9fs_string_free(&name);
2993 static void v9fs_xattrwalk(void *opaque)
2999 int32_t fid, newfid;
3000 V9fsFidState *file_fidp;
3001 V9fsFidState *xattr_fidp = NULL;
3002 V9fsPDU *pdu = opaque;
3003 V9fsState *s = pdu->s;
3005 v9fs_string_init(&name);
3006 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name);
3010 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data);
3012 file_fidp = get_fid(pdu, fid);
3013 if (file_fidp == NULL) {
3017 xattr_fidp = alloc_fid(s, newfid);
3018 if (xattr_fidp == NULL) {
3022 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path);
3023 if (name.data == NULL) {
3025 * listxattr request. Get the size first
3027 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0);
3030 clunk_fid(s, xattr_fidp->fid);
3034 * Read the xattr value
3036 xattr_fidp->fs.xattr.len = size;
3037 xattr_fidp->fid_type = P9_FID_XATTR;
3038 xattr_fidp->fs.xattr.copied_len = -1;
3040 xattr_fidp->fs.xattr.value = g_malloc(size);
3041 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path,
3042 xattr_fidp->fs.xattr.value,
3043 xattr_fidp->fs.xattr.len);
3045 clunk_fid(s, xattr_fidp->fid);
3049 err = pdu_marshal(pdu, offset, "q", size);
3056 * specific xattr fid. We check for xattr
3057 * presence also collect the xattr size
3059 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3063 clunk_fid(s, xattr_fidp->fid);
3067 * Read the xattr value
3069 xattr_fidp->fs.xattr.len = size;
3070 xattr_fidp->fid_type = P9_FID_XATTR;
3071 xattr_fidp->fs.xattr.copied_len = -1;
3073 xattr_fidp->fs.xattr.value = g_malloc(size);
3074 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path,
3075 &name, xattr_fidp->fs.xattr.value,
3076 xattr_fidp->fs.xattr.len);
3078 clunk_fid(s, xattr_fidp->fid);
3082 err = pdu_marshal(pdu, offset, "q", size);
3088 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size);
3090 put_fid(pdu, file_fidp);
3092 put_fid(pdu, xattr_fidp);
3095 pdu_complete(pdu, err);
3096 v9fs_string_free(&name);
3099 static void v9fs_xattrcreate(void *opaque)
3107 V9fsFidState *file_fidp;
3108 V9fsFidState *xattr_fidp;
3109 V9fsPDU *pdu = opaque;
3111 v9fs_string_init(&name);
3112 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags);
3116 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags);
3118 file_fidp = get_fid(pdu, fid);
3119 if (file_fidp == NULL) {
3123 /* Make the file fid point to xattr */
3124 xattr_fidp = file_fidp;
3125 xattr_fidp->fid_type = P9_FID_XATTR;
3126 xattr_fidp->fs.xattr.copied_len = 0;
3127 xattr_fidp->fs.xattr.len = size;
3128 xattr_fidp->fs.xattr.flags = flags;
3129 v9fs_string_init(&xattr_fidp->fs.xattr.name);
3130 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name);
3131 xattr_fidp->fs.xattr.value = g_malloc(size);
3133 put_fid(pdu, file_fidp);
3135 pdu_complete(pdu, err);
3136 v9fs_string_free(&name);
3139 static void v9fs_readlink(void *opaque)
3141 V9fsPDU *pdu = opaque;
3148 err = pdu_unmarshal(pdu, offset, "d", &fid);
3152 trace_v9fs_readlink(pdu->tag, pdu->id, fid);
3153 fidp = get_fid(pdu, fid);
3159 v9fs_string_init(&target);
3160 err = v9fs_co_readlink(pdu, &fidp->path, &target);
3164 err = pdu_marshal(pdu, offset, "s", &target);
3166 v9fs_string_free(&target);
3170 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data);
3171 v9fs_string_free(&target);
3175 pdu_complete(pdu, err);
3178 static CoroutineEntry *pdu_co_handlers[] = {
3179 [P9_TREADDIR] = v9fs_readdir,
3180 [P9_TSTATFS] = v9fs_statfs,
3181 [P9_TGETATTR] = v9fs_getattr,
3182 [P9_TSETATTR] = v9fs_setattr,
3183 [P9_TXATTRWALK] = v9fs_xattrwalk,
3184 [P9_TXATTRCREATE] = v9fs_xattrcreate,
3185 [P9_TMKNOD] = v9fs_mknod,
3186 [P9_TRENAME] = v9fs_rename,
3187 [P9_TLOCK] = v9fs_lock,
3188 [P9_TGETLOCK] = v9fs_getlock,
3189 [P9_TRENAMEAT] = v9fs_renameat,
3190 [P9_TREADLINK] = v9fs_readlink,
3191 [P9_TUNLINKAT] = v9fs_unlinkat,
3192 [P9_TMKDIR] = v9fs_mkdir,
3193 [P9_TVERSION] = v9fs_version,
3194 [P9_TLOPEN] = v9fs_open,
3195 [P9_TATTACH] = v9fs_attach,
3196 [P9_TSTAT] = v9fs_stat,
3197 [P9_TWALK] = v9fs_walk,
3198 [P9_TCLUNK] = v9fs_clunk,
3199 [P9_TFSYNC] = v9fs_fsync,
3200 [P9_TOPEN] = v9fs_open,
3201 [P9_TREAD] = v9fs_read,
3203 [P9_TAUTH] = v9fs_auth,
3205 [P9_TFLUSH] = v9fs_flush,
3206 [P9_TLINK] = v9fs_link,
3207 [P9_TSYMLINK] = v9fs_symlink,
3208 [P9_TCREATE] = v9fs_create,
3209 [P9_TLCREATE] = v9fs_lcreate,
3210 [P9_TWRITE] = v9fs_write,
3211 [P9_TWSTAT] = v9fs_wstat,
3212 [P9_TREMOVE] = v9fs_remove,
3215 static void v9fs_op_not_supp(void *opaque)
3217 V9fsPDU *pdu = opaque;
3218 pdu_complete(pdu, -EOPNOTSUPP);
3221 static void v9fs_fs_ro(void *opaque)
3223 V9fsPDU *pdu = opaque;
3224 pdu_complete(pdu, -EROFS);
3227 static inline bool is_read_only_op(V9fsPDU *pdu)
3254 void pdu_submit(V9fsPDU *pdu)
3257 CoroutineEntry *handler;
3258 V9fsState *s = pdu->s;
3260 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) ||
3261 (pdu_co_handlers[pdu->id] == NULL)) {
3262 handler = v9fs_op_not_supp;
3264 handler = pdu_co_handlers[pdu->id];
3267 if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) {
3268 handler = v9fs_fs_ro;
3270 co = qemu_coroutine_create(handler);
3271 qemu_coroutine_enter(co, pdu);
3274 /* Returns 0 on success, 1 on failure. */
3275 int v9fs_device_realize_common(V9fsState *s, Error **errp)
3277 V9fsVirtioState *v = container_of(s, V9fsVirtioState, state);
3284 /* initialize pdu allocator */
3285 QLIST_INIT(&s->free_list);
3286 QLIST_INIT(&s->active_list);
3287 for (i = 0; i < (MAX_REQ - 1); i++) {
3288 QLIST_INSERT_HEAD(&s->free_list, &v->pdus[i], next);
3293 v9fs_path_init(&path);
3295 fse = get_fsdev_fsentry(s->fsconf.fsdev_id);
3298 /* We don't have a fsdev identified by fsdev_id */
3299 error_setg(errp, "9pfs device couldn't find fsdev with the "
3301 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL");
3305 if (!s->fsconf.tag) {
3306 /* we haven't specified a mount_tag */
3307 error_setg(errp, "fsdev with id %s needs mount_tag arguments",
3308 s->fsconf.fsdev_id);
3312 s->ctx.export_flags = fse->export_flags;
3313 s->ctx.fs_root = g_strdup(fse->path);
3314 s->ctx.exops.get_st_gen = NULL;
3315 len = strlen(s->fsconf.tag);
3316 if (len > MAX_TAG_LEN - 1) {
3317 error_setg(errp, "mount tag '%s' (%d bytes) is longer than "
3318 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1);
3322 s->tag = g_strdup(s->fsconf.tag);
3328 qemu_co_rwlock_init(&s->rename_lock);
3330 if (s->ops->init(&s->ctx) < 0) {
3331 error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s"
3332 " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root);
3337 * Check details of export path, We need to use fs driver
3338 * call back to do that. Since we are in the init path, we don't
3339 * use co-routines here.
3341 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) {
3343 "error in converting name to path %s", strerror(errno));
3346 if (s->ops->lstat(&s->ctx, &path, &stat)) {
3347 error_setg(errp, "share path %s does not exist", fse->path);
3349 } else if (!S_ISDIR(stat.st_mode)) {
3350 error_setg(errp, "share path %s is not a directory", fse->path);
3353 v9fs_path_free(&path);
3358 g_free(s->ctx.fs_root);
3360 v9fs_path_free(&path);
3365 void v9fs_device_unrealize_common(V9fsState *s, Error **errp)
3367 g_free(s->ctx.fs_root);
3371 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void)
3374 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) {
3375 error_report("Failed to get the resource limit");
3378 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3);
3379 open_fd_rc = rlim.rlim_cur/2;