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 <glib/gprintf.h>
16 #include "hw/virtio/virtio.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
)
184 void GCC_FMT_ATTR(2, 3)
185 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
189 v9fs_path_free(path
);
192 /* Bump the size for including terminating NULL */
193 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
197 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
200 lhs
->data
= g_malloc(rhs
->size
);
201 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
202 lhs
->size
= rhs
->size
;
205 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
206 const char *name
, V9fsPath
*path
)
209 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
217 * Return TRUE if s1 is an ancestor of s2.
219 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
220 * As a special case, We treat s1 as ancestor of s2 if they are same!
222 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
224 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
225 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
232 static size_t v9fs_string_size(V9fsString
*str
)
238 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
239 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
242 if (f
->fid_type
== P9_FID_FILE
) {
243 if (f
->fs
.fd
== -1) {
245 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
246 } while (err
== -EINTR
&& !pdu
->cancelled
);
248 } else if (f
->fid_type
== P9_FID_DIR
) {
249 if (f
->fs
.dir
.stream
== NULL
) {
251 err
= v9fs_co_opendir(pdu
, f
);
252 } while (err
== -EINTR
&& !pdu
->cancelled
);
258 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
262 V9fsState
*s
= pdu
->s
;
264 for (f
= s
->fid_list
; f
; f
= f
->next
) {
268 * Update the fid ref upfront so that
269 * we don't get reclaimed when we yield
274 * check whether we need to reopen the
275 * file. We might have closed the fd
276 * while trying to free up some file
279 err
= v9fs_reopen_fid(pdu
, f
);
285 * Mark the fid as referenced so that the LRU
286 * reclaim won't close the file descriptor
288 f
->flags
|= FID_REFERENCED
;
295 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
299 for (f
= s
->fid_list
; f
; f
= f
->next
) {
300 /* If fid is already there return NULL */
306 f
= g_malloc0(sizeof(V9fsFidState
));
308 f
->fid_type
= P9_FID_NONE
;
311 * Mark the fid as referenced so that the LRU
312 * reclaim won't close the file descriptor
314 f
->flags
|= FID_REFERENCED
;
315 f
->next
= s
->fid_list
;
318 v9fs_readdir_init(&f
->fs
.dir
);
319 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
324 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
328 if (fidp
->fs
.xattr
.copied_len
== -1) {
329 /* getxattr/listxattr fid */
333 * if this is fid for setxattr. clunk should
334 * result in setxattr localcall
336 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
337 /* clunk after partial write */
341 if (fidp
->fs
.xattr
.len
) {
342 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
343 fidp
->fs
.xattr
.value
,
345 fidp
->fs
.xattr
.flags
);
347 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
350 v9fs_string_free(&fidp
->fs
.xattr
.name
);
352 g_free(fidp
->fs
.xattr
.value
);
356 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
360 if (fidp
->fid_type
== P9_FID_FILE
) {
361 /* If we reclaimed the fd no need to close */
362 if (fidp
->fs
.fd
!= -1) {
363 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
365 } else if (fidp
->fid_type
== P9_FID_DIR
) {
366 if (fidp
->fs
.dir
.stream
!= NULL
) {
367 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
369 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
370 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
372 v9fs_path_free(&fidp
->path
);
377 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
382 * Don't free the fid if it is in reclaim list
384 if (!fidp
->ref
&& fidp
->clunked
) {
385 if (fidp
->fid
== pdu
->s
->root_fid
) {
387 * if the clunked fid is root fid then we
388 * have unmounted the fs on the client side.
389 * delete the migration blocker. Ideally, this
390 * should be hooked to transport close notification
392 if (pdu
->s
->migration_blocker
) {
393 migrate_del_blocker(pdu
->s
->migration_blocker
);
394 error_free(pdu
->s
->migration_blocker
);
395 pdu
->s
->migration_blocker
= NULL
;
398 return free_fid(pdu
, fidp
);
403 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
405 V9fsFidState
**fidpp
, *fidp
;
407 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
408 if ((*fidpp
)->fid
== fid
) {
412 if (*fidpp
== NULL
) {
421 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
423 int reclaim_count
= 0;
424 V9fsState
*s
= pdu
->s
;
425 V9fsFidState
*f
, *reclaim_list
= NULL
;
427 for (f
= s
->fid_list
; f
; f
= f
->next
) {
429 * Unlink fids cannot be reclaimed. Check
430 * for them and skip them. Also skip fids
431 * currently being operated on.
433 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
437 * if it is a recently referenced fid
438 * we leave the fid untouched and clear the
439 * reference bit. We come back to it later
440 * in the next iteration. (a simple LRU without
441 * moving list elements around)
443 if (f
->flags
& FID_REFERENCED
) {
444 f
->flags
&= ~FID_REFERENCED
;
448 * Add fids to reclaim list.
450 if (f
->fid_type
== P9_FID_FILE
) {
451 if (f
->fs
.fd
!= -1) {
453 * Up the reference count so that
454 * a clunk request won't free this fid
457 f
->rclm_lst
= reclaim_list
;
459 f
->fs_reclaim
.fd
= f
->fs
.fd
;
463 } else if (f
->fid_type
== P9_FID_DIR
) {
464 if (f
->fs
.dir
.stream
!= NULL
) {
466 * Up the reference count so that
467 * a clunk request won't free this fid
470 f
->rclm_lst
= reclaim_list
;
472 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
473 f
->fs
.dir
.stream
= NULL
;
477 if (reclaim_count
>= open_fd_rc
) {
482 * Now close the fid in reclaim list. Free them if they
483 * are already clunked.
485 while (reclaim_list
) {
487 reclaim_list
= f
->rclm_lst
;
488 if (f
->fid_type
== P9_FID_FILE
) {
489 v9fs_co_close(pdu
, &f
->fs_reclaim
);
490 } else if (f
->fid_type
== P9_FID_DIR
) {
491 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
495 * Now drop the fid reference, free it
502 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
505 V9fsState
*s
= pdu
->s
;
506 V9fsFidState
*fidp
, head_fid
;
508 head_fid
.next
= s
->fid_list
;
509 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
510 if (fidp
->path
.size
!= path
->size
) {
513 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
514 /* Mark the fid non reclaimable. */
515 fidp
->flags
|= FID_NON_RECLAIMABLE
;
517 /* reopen the file/dir if already closed */
518 err
= v9fs_reopen_fid(pdu
, fidp
);
523 * Go back to head of fid list because
524 * the list could have got updated when
525 * switched to the worker thread
535 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
537 V9fsState
*s
= pdu
->s
;
538 V9fsFidState
*fidp
= NULL
;
541 while (s
->fid_list
) {
543 s
->fid_list
= fidp
->next
;
552 /* One or more unclunked fids found... */
553 error_report("9pfs:%s: One or more uncluncked fids "
554 "found during reset", __func__
);
558 #define P9_QID_TYPE_DIR 0x80
559 #define P9_QID_TYPE_SYMLINK 0x02
561 #define P9_STAT_MODE_DIR 0x80000000
562 #define P9_STAT_MODE_APPEND 0x40000000
563 #define P9_STAT_MODE_EXCL 0x20000000
564 #define P9_STAT_MODE_MOUNT 0x10000000
565 #define P9_STAT_MODE_AUTH 0x08000000
566 #define P9_STAT_MODE_TMP 0x04000000
567 #define P9_STAT_MODE_SYMLINK 0x02000000
568 #define P9_STAT_MODE_LINK 0x01000000
569 #define P9_STAT_MODE_DEVICE 0x00800000
570 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
571 #define P9_STAT_MODE_SOCKET 0x00100000
572 #define P9_STAT_MODE_SETUID 0x00080000
573 #define P9_STAT_MODE_SETGID 0x00040000
574 #define P9_STAT_MODE_SETVTX 0x00010000
576 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
577 P9_STAT_MODE_SYMLINK | \
578 P9_STAT_MODE_LINK | \
579 P9_STAT_MODE_DEVICE | \
580 P9_STAT_MODE_NAMED_PIPE | \
583 /* This is the algorithm from ufs in spfs */
584 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
588 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
589 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
590 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
591 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
593 if (S_ISDIR(stbuf
->st_mode
)) {
594 qidp
->type
|= P9_QID_TYPE_DIR
;
596 if (S_ISLNK(stbuf
->st_mode
)) {
597 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
601 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
607 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
611 stat_to_qid(&stbuf
, qidp
);
615 V9fsPDU
*pdu_alloc(V9fsState
*s
)
619 if (!QLIST_EMPTY(&s
->free_list
)) {
620 pdu
= QLIST_FIRST(&s
->free_list
);
621 QLIST_REMOVE(pdu
, next
);
622 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
627 void pdu_free(V9fsPDU
*pdu
)
629 V9fsState
*s
= pdu
->s
;
631 g_assert(!pdu
->cancelled
);
632 QLIST_REMOVE(pdu
, next
);
633 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
637 * We don't do error checking for pdu_marshal/unmarshal here
638 * because we always expect to have enough space to encode
641 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
643 int8_t id
= pdu
->id
+ 1; /* Response */
644 V9fsState
*s
= pdu
->s
;
650 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
653 str
.data
= strerror(err
);
654 str
.size
= strlen(str
.data
);
656 len
+= pdu_marshal(pdu
, len
, "s", &str
);
660 len
+= pdu_marshal(pdu
, len
, "d", err
);
662 if (s
->proto_version
== V9FS_PROTO_2000L
) {
665 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
668 /* fill out the header */
669 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
671 /* keep these in sync */
675 pdu_push_and_notify(pdu
);
677 /* Now wakeup anybody waiting in flush for this request */
678 if (!qemu_co_queue_next(&pdu
->complete
)) {
683 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
688 if (mode
& P9_STAT_MODE_DIR
) {
692 if (mode
& P9_STAT_MODE_SYMLINK
) {
695 if (mode
& P9_STAT_MODE_SOCKET
) {
698 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
701 if (mode
& P9_STAT_MODE_DEVICE
) {
702 if (extension
->size
&& extension
->data
[0] == 'c') {
713 if (mode
& P9_STAT_MODE_SETUID
) {
716 if (mode
& P9_STAT_MODE_SETGID
) {
719 if (mode
& P9_STAT_MODE_SETVTX
) {
726 static int donttouch_stat(V9fsStat
*stat
)
728 if (stat
->type
== -1 &&
730 stat
->qid
.type
== -1 &&
731 stat
->qid
.version
== -1 &&
732 stat
->qid
.path
== -1 &&
736 stat
->length
== -1 &&
743 stat
->n_muid
== -1) {
750 static void v9fs_stat_init(V9fsStat
*stat
)
752 v9fs_string_init(&stat
->name
);
753 v9fs_string_init(&stat
->uid
);
754 v9fs_string_init(&stat
->gid
);
755 v9fs_string_init(&stat
->muid
);
756 v9fs_string_init(&stat
->extension
);
759 static void v9fs_stat_free(V9fsStat
*stat
)
761 v9fs_string_free(&stat
->name
);
762 v9fs_string_free(&stat
->uid
);
763 v9fs_string_free(&stat
->gid
);
764 v9fs_string_free(&stat
->muid
);
765 v9fs_string_free(&stat
->extension
);
768 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
772 mode
= stbuf
->st_mode
& 0777;
773 if (S_ISDIR(stbuf
->st_mode
)) {
774 mode
|= P9_STAT_MODE_DIR
;
777 if (S_ISLNK(stbuf
->st_mode
)) {
778 mode
|= P9_STAT_MODE_SYMLINK
;
781 if (S_ISSOCK(stbuf
->st_mode
)) {
782 mode
|= P9_STAT_MODE_SOCKET
;
785 if (S_ISFIFO(stbuf
->st_mode
)) {
786 mode
|= P9_STAT_MODE_NAMED_PIPE
;
789 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
790 mode
|= P9_STAT_MODE_DEVICE
;
793 if (stbuf
->st_mode
& S_ISUID
) {
794 mode
|= P9_STAT_MODE_SETUID
;
797 if (stbuf
->st_mode
& S_ISGID
) {
798 mode
|= P9_STAT_MODE_SETGID
;
801 if (stbuf
->st_mode
& S_ISVTX
) {
802 mode
|= P9_STAT_MODE_SETVTX
;
808 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
809 const struct stat
*stbuf
,
815 memset(v9stat
, 0, sizeof(*v9stat
));
817 stat_to_qid(stbuf
, &v9stat
->qid
);
818 v9stat
->mode
= stat_to_v9mode(stbuf
);
819 v9stat
->atime
= stbuf
->st_atime
;
820 v9stat
->mtime
= stbuf
->st_mtime
;
821 v9stat
->length
= stbuf
->st_size
;
823 v9fs_string_free(&v9stat
->uid
);
824 v9fs_string_free(&v9stat
->gid
);
825 v9fs_string_free(&v9stat
->muid
);
827 v9stat
->n_uid
= stbuf
->st_uid
;
828 v9stat
->n_gid
= stbuf
->st_gid
;
831 v9fs_string_free(&v9stat
->extension
);
833 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
834 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
838 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
839 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
840 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
841 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
842 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
843 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
844 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
847 str
= strrchr(name
->data
, '/');
854 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
857 v9fs_string_size(&v9stat
->name
) +
858 v9fs_string_size(&v9stat
->uid
) +
859 v9fs_string_size(&v9stat
->gid
) +
860 v9fs_string_size(&v9stat
->muid
) +
861 v9fs_string_size(&v9stat
->extension
);
865 #define P9_STATS_MODE 0x00000001ULL
866 #define P9_STATS_NLINK 0x00000002ULL
867 #define P9_STATS_UID 0x00000004ULL
868 #define P9_STATS_GID 0x00000008ULL
869 #define P9_STATS_RDEV 0x00000010ULL
870 #define P9_STATS_ATIME 0x00000020ULL
871 #define P9_STATS_MTIME 0x00000040ULL
872 #define P9_STATS_CTIME 0x00000080ULL
873 #define P9_STATS_INO 0x00000100ULL
874 #define P9_STATS_SIZE 0x00000200ULL
875 #define P9_STATS_BLOCKS 0x00000400ULL
877 #define P9_STATS_BTIME 0x00000800ULL
878 #define P9_STATS_GEN 0x00001000ULL
879 #define P9_STATS_DATA_VERSION 0x00002000ULL
881 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
882 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
885 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
886 V9fsStatDotl
*v9lstat
)
888 memset(v9lstat
, 0, sizeof(*v9lstat
));
890 v9lstat
->st_mode
= stbuf
->st_mode
;
891 v9lstat
->st_nlink
= stbuf
->st_nlink
;
892 v9lstat
->st_uid
= stbuf
->st_uid
;
893 v9lstat
->st_gid
= stbuf
->st_gid
;
894 v9lstat
->st_rdev
= stbuf
->st_rdev
;
895 v9lstat
->st_size
= stbuf
->st_size
;
896 v9lstat
->st_blksize
= stbuf
->st_blksize
;
897 v9lstat
->st_blocks
= stbuf
->st_blocks
;
898 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
899 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
900 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
901 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
902 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
903 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
904 /* Currently we only support BASIC fields in stat */
905 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
907 stat_to_qid(stbuf
, &v9lstat
->qid
);
910 static void print_sg(struct iovec
*sg
, int cnt
)
914 printf("sg[%d]: {", cnt
);
915 for (i
= 0; i
< cnt
; i
++) {
919 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
924 /* Will call this only for path name based fid */
925 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
928 v9fs_path_init(&str
);
929 v9fs_path_copy(&str
, dst
);
930 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
931 v9fs_path_free(&str
);
934 static inline bool is_ro_export(FsContext
*ctx
)
936 return ctx
->export_flags
& V9FS_RDONLY
;
939 static void coroutine_fn
v9fs_version(void *opaque
)
942 V9fsPDU
*pdu
= opaque
;
943 V9fsState
*s
= pdu
->s
;
947 v9fs_string_init(&version
);
948 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
953 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
957 if (!strcmp(version
.data
, "9P2000.u")) {
958 s
->proto_version
= V9FS_PROTO_2000U
;
959 } else if (!strcmp(version
.data
, "9P2000.L")) {
960 s
->proto_version
= V9FS_PROTO_2000L
;
962 v9fs_string_sprintf(&version
, "unknown");
965 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
971 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
973 pdu_complete(pdu
, offset
);
974 v9fs_string_free(&version
);
977 static void coroutine_fn
v9fs_attach(void *opaque
)
979 V9fsPDU
*pdu
= opaque
;
980 V9fsState
*s
= pdu
->s
;
981 int32_t fid
, afid
, n_uname
;
982 V9fsString uname
, aname
;
988 v9fs_string_init(&uname
);
989 v9fs_string_init(&aname
);
990 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
991 &afid
, &uname
, &aname
, &n_uname
);
995 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
997 fidp
= alloc_fid(s
, fid
);
1002 fidp
->uid
= n_uname
;
1003 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1009 err
= fid_to_qid(pdu
, fidp
, &qid
);
1015 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1021 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1022 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1023 qid
.type
, qid
.version
, qid
.path
);
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
) {
1030 error_setg(&s
->migration_blocker
,
1031 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1032 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1033 migrate_add_blocker(s
->migration_blocker
);
1038 pdu_complete(pdu
, err
);
1039 v9fs_string_free(&uname
);
1040 v9fs_string_free(&aname
);
1043 static void coroutine_fn
v9fs_stat(void *opaque
)
1051 V9fsPDU
*pdu
= opaque
;
1053 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1057 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1059 fidp
= get_fid(pdu
, fid
);
1064 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1068 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1072 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1074 v9fs_stat_free(&v9stat
);
1077 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1078 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1080 v9fs_stat_free(&v9stat
);
1084 pdu_complete(pdu
, err
);
1087 static void coroutine_fn
v9fs_getattr(void *opaque
)
1094 uint64_t request_mask
;
1095 V9fsStatDotl v9stat_dotl
;
1096 V9fsPDU
*pdu
= opaque
;
1097 V9fsState
*s
= pdu
->s
;
1099 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1103 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1105 fidp
= get_fid(pdu
, fid
);
1111 * Currently we only support BASIC fields in stat, so there is no
1112 * need to look at request_mask.
1114 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1118 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1120 /* fill st_gen if requested and supported by underlying fs */
1121 if (request_mask
& P9_STATS_GEN
) {
1122 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1125 /* we have valid st_gen: update result mask */
1126 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1129 /* request cancelled, e.g. by Tflush */
1132 /* failed to get st_gen: not fatal, ignore */
1136 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1141 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1142 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1143 v9stat_dotl
.st_gid
);
1147 pdu_complete(pdu
, retval
);
1150 /* Attribute flags */
1151 #define P9_ATTR_MODE (1 << 0)
1152 #define P9_ATTR_UID (1 << 1)
1153 #define P9_ATTR_GID (1 << 2)
1154 #define P9_ATTR_SIZE (1 << 3)
1155 #define P9_ATTR_ATIME (1 << 4)
1156 #define P9_ATTR_MTIME (1 << 5)
1157 #define P9_ATTR_CTIME (1 << 6)
1158 #define P9_ATTR_ATIME_SET (1 << 7)
1159 #define P9_ATTR_MTIME_SET (1 << 8)
1161 #define P9_ATTR_MASK 127
1163 static void coroutine_fn
v9fs_setattr(void *opaque
)
1170 V9fsPDU
*pdu
= opaque
;
1172 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1177 fidp
= get_fid(pdu
, fid
);
1182 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1183 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1188 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1189 struct timespec times
[2];
1190 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1191 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1192 times
[0].tv_sec
= v9iattr
.atime_sec
;
1193 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1195 times
[0].tv_nsec
= UTIME_NOW
;
1198 times
[0].tv_nsec
= UTIME_OMIT
;
1200 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1201 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1202 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1203 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1205 times
[1].tv_nsec
= UTIME_NOW
;
1208 times
[1].tv_nsec
= UTIME_OMIT
;
1210 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1216 * If the only valid entry in iattr is ctime we can call
1217 * chown(-1,-1) to update the ctime of the file
1219 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1220 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1221 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1222 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1225 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1228 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1234 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1235 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1244 pdu_complete(pdu
, err
);
1247 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1253 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1258 for (i
= 0; i
< nwnames
; i
++) {
1259 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1268 static bool name_is_illegal(const char *name
)
1270 return !*name
|| strchr(name
, '/') != NULL
;
1273 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1276 qid1
->type
!= qid2
->type
||
1277 qid1
->version
!= qid2
->version
||
1278 qid1
->path
!= qid2
->path
;
1281 static void coroutine_fn
v9fs_walk(void *opaque
)
1284 V9fsQID
*qids
= NULL
;
1286 V9fsPath dpath
, path
;
1290 int32_t fid
, newfid
;
1291 V9fsString
*wnames
= NULL
;
1293 V9fsFidState
*newfidp
= NULL
;
1294 V9fsPDU
*pdu
= opaque
;
1295 V9fsState
*s
= pdu
->s
;
1298 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1300 pdu_complete(pdu
, err
);
1305 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1307 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1308 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1309 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1310 for (i
= 0; i
< nwnames
; i
++) {
1311 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1315 if (name_is_illegal(wnames
[i
].data
)) {
1321 } else if (nwnames
> P9_MAXWELEM
) {
1325 fidp
= get_fid(pdu
, fid
);
1331 v9fs_path_init(&dpath
);
1332 v9fs_path_init(&path
);
1334 err
= fid_to_qid(pdu
, fidp
, &qid
);
1340 * Both dpath and path initially poin to fidp.
1341 * Needed to handle request with nwnames == 0
1343 v9fs_path_copy(&dpath
, &fidp
->path
);
1344 v9fs_path_copy(&path
, &fidp
->path
);
1345 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1346 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1347 strcmp("..", wnames
[name_idx
].data
)) {
1348 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1354 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1358 stat_to_qid(&stbuf
, &qid
);
1359 v9fs_path_copy(&dpath
, &path
);
1361 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1363 if (fid
== newfid
) {
1364 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1365 v9fs_path_copy(&fidp
->path
, &path
);
1367 newfidp
= alloc_fid(s
, newfid
);
1368 if (newfidp
== NULL
) {
1372 newfidp
->uid
= fidp
->uid
;
1373 v9fs_path_copy(&newfidp
->path
, &path
);
1375 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1376 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1380 put_fid(pdu
, newfidp
);
1382 v9fs_path_free(&dpath
);
1383 v9fs_path_free(&path
);
1385 pdu_complete(pdu
, err
);
1386 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1387 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1388 v9fs_string_free(&wnames
[name_idx
]);
1395 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1397 struct statfs stbuf
;
1399 V9fsState
*s
= pdu
->s
;
1402 * iounit should be multiples of f_bsize (host filesystem block size
1403 * and as well as less than (client msize - P9_IOHDRSZ))
1405 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1406 iounit
= stbuf
.f_bsize
;
1407 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1410 iounit
= s
->msize
- P9_IOHDRSZ
;
1415 static void coroutine_fn
v9fs_open(void *opaque
)
1426 V9fsPDU
*pdu
= opaque
;
1427 V9fsState
*s
= pdu
->s
;
1429 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1430 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1433 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1439 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1441 fidp
= get_fid(pdu
, fid
);
1446 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1448 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1452 stat_to_qid(&stbuf
, &qid
);
1453 if (S_ISDIR(stbuf
.st_mode
)) {
1454 err
= v9fs_co_opendir(pdu
, fidp
);
1458 fidp
->fid_type
= P9_FID_DIR
;
1459 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1465 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1466 flags
= get_dotl_openflags(s
, mode
);
1468 flags
= omode_to_uflags(mode
);
1470 if (is_ro_export(&s
->ctx
)) {
1471 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1472 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1477 err
= v9fs_co_open(pdu
, fidp
, flags
);
1481 fidp
->fid_type
= P9_FID_FILE
;
1482 fidp
->open_flags
= flags
;
1483 if (flags
& O_EXCL
) {
1485 * We let the host file system do O_EXCL check
1486 * We should not reclaim such fd
1488 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1490 iounit
= get_iounit(pdu
, &fidp
->path
);
1491 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1497 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1498 qid
.type
, qid
.version
, qid
.path
, iounit
);
1502 pdu_complete(pdu
, err
);
1505 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1507 int32_t dfid
, flags
, mode
;
1516 V9fsPDU
*pdu
= opaque
;
1518 v9fs_string_init(&name
);
1519 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1520 &name
, &flags
, &mode
, &gid
);
1524 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1526 if (name_is_illegal(name
.data
)) {
1531 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1536 fidp
= get_fid(pdu
, dfid
);
1542 flags
= get_dotl_openflags(pdu
->s
, flags
);
1543 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1544 flags
| O_CREAT
, mode
, &stbuf
);
1548 fidp
->fid_type
= P9_FID_FILE
;
1549 fidp
->open_flags
= flags
;
1550 if (flags
& O_EXCL
) {
1552 * We let the host file system do O_EXCL check
1553 * We should not reclaim such fd
1555 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1557 iounit
= get_iounit(pdu
, &fidp
->path
);
1558 stat_to_qid(&stbuf
, &qid
);
1559 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1564 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1565 qid
.type
, qid
.version
, qid
.path
, iounit
);
1569 pdu_complete(pdu
, err
);
1570 v9fs_string_free(&name
);
1573 static void v9fs_fsync(void *opaque
)
1580 V9fsPDU
*pdu
= opaque
;
1582 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1586 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1588 fidp
= get_fid(pdu
, fid
);
1593 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1599 pdu_complete(pdu
, err
);
1602 static void coroutine_fn
v9fs_clunk(void *opaque
)
1608 V9fsPDU
*pdu
= opaque
;
1609 V9fsState
*s
= pdu
->s
;
1611 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1615 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1617 fidp
= clunk_fid(s
, fid
);
1623 * Bump the ref so that put_fid will
1627 err
= put_fid(pdu
, fidp
);
1632 pdu_complete(pdu
, err
);
1635 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1636 uint64_t off
, uint32_t max_count
)
1642 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
1643 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
1645 xattr_len
= fidp
->fs
.xattr
.len
;
1646 read_count
= xattr_len
- off
;
1647 if (read_count
> max_count
) {
1648 read_count
= max_count
;
1649 } else if (read_count
< 0) {
1651 * read beyond XATTR value
1655 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1661 err
= v9fs_pack(elem
->in_sg
, elem
->in_num
, offset
,
1662 ((char *)fidp
->fs
.xattr
.value
) + off
,
1671 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1680 off_t saved_dir_pos
;
1681 struct dirent
*dent
;
1683 /* save the directory position */
1684 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1685 if (saved_dir_pos
< 0) {
1686 return saved_dir_pos
;
1690 v9fs_path_init(&path
);
1692 v9fs_readdir_lock(&fidp
->fs
.dir
);
1694 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1698 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1702 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1706 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1710 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1711 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1713 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1715 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1716 /* Ran out of buffer. Set dir back to old position and return */
1717 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1718 v9fs_stat_free(&v9stat
);
1719 v9fs_path_free(&path
);
1723 v9fs_stat_free(&v9stat
);
1724 v9fs_path_free(&path
);
1725 saved_dir_pos
= dent
->d_off
;
1728 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1730 v9fs_path_free(&path
);
1738 * Create a QEMUIOVector for a sub-region of PDU iovecs
1740 * @qiov: uninitialized QEMUIOVector
1741 * @skip: number of bytes to skip from beginning of PDU
1742 * @size: number of bytes to include
1743 * @is_write: true - write, false - read
1745 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1746 * with qemu_iovec_destroy().
1748 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1749 size_t skip
, size_t size
,
1756 virtio_init_iov_from_pdu(pdu
, &iov
, &niov
, is_write
);
1758 qemu_iovec_init_external(&elem
, iov
, niov
);
1759 qemu_iovec_init(qiov
, niov
);
1760 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1763 static void coroutine_fn
v9fs_read(void *opaque
)
1772 V9fsPDU
*pdu
= opaque
;
1773 V9fsState
*s
= pdu
->s
;
1775 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1779 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1781 fidp
= get_fid(pdu
, fid
);
1786 if (fidp
->fid_type
== P9_FID_DIR
) {
1789 v9fs_co_rewinddir(pdu
, fidp
);
1791 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1796 err
= pdu_marshal(pdu
, offset
, "d", count
);
1800 err
+= offset
+ count
;
1801 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1802 QEMUIOVector qiov_full
;
1806 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1807 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1809 qemu_iovec_reset(&qiov
);
1810 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1812 print_sg(qiov
.iov
, qiov
.niov
);
1814 /* Loop in case of EINTR */
1816 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1821 } while (len
== -EINTR
&& !pdu
->cancelled
);
1823 /* IO error return the error */
1825 goto out_free_iovec
;
1827 } while (count
< max_count
&& len
> 0);
1828 err
= pdu_marshal(pdu
, offset
, "d", count
);
1830 goto out_free_iovec
;
1832 err
+= offset
+ count
;
1834 qemu_iovec_destroy(&qiov
);
1835 qemu_iovec_destroy(&qiov_full
);
1836 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1837 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1841 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1845 pdu_complete(pdu
, err
);
1848 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1851 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1852 * size of type (1) + size of name.size (2) + strlen(name.data)
1854 return 24 + v9fs_string_size(name
);
1857 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1865 off_t saved_dir_pos
;
1866 struct dirent
*dent
;
1868 /* save the directory position */
1869 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1870 if (saved_dir_pos
< 0) {
1871 return saved_dir_pos
;
1875 v9fs_readdir_lock(&fidp
->fs
.dir
);
1877 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1881 v9fs_string_init(&name
);
1882 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1883 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1884 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1886 /* Ran out of buffer. Set dir back to old position and return */
1887 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1888 v9fs_string_free(&name
);
1892 * Fill up just the path field of qid because the client uses
1893 * only that. To fill the entire qid structure we will have
1894 * to stat each dirent found, which is expensive
1896 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1897 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1898 /* Fill the other fields with dummy values */
1902 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1903 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1905 dent
->d_type
, &name
);
1907 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1910 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1911 v9fs_string_free(&name
);
1915 v9fs_string_free(&name
);
1916 saved_dir_pos
= dent
->d_off
;
1919 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1927 static void coroutine_fn
v9fs_readdir(void *opaque
)
1933 uint64_t initial_offset
;
1936 V9fsPDU
*pdu
= opaque
;
1938 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1939 &initial_offset
, &max_count
);
1943 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1945 fidp
= get_fid(pdu
, fid
);
1950 if (!fidp
->fs
.dir
.stream
) {
1954 if (initial_offset
== 0) {
1955 v9fs_co_rewinddir(pdu
, fidp
);
1957 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1959 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1964 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1968 retval
+= count
+ offset
;
1969 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1973 pdu_complete(pdu
, retval
);
1976 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1977 uint64_t off
, uint32_t count
,
1978 struct iovec
*sg
, int cnt
)
1987 xattr_len
= fidp
->fs
.xattr
.len
;
1988 write_count
= xattr_len
- off
;
1989 if (write_count
> count
) {
1990 write_count
= count
;
1991 } else if (write_count
< 0) {
1993 * write beyond XATTR value len specified in
1999 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2004 fidp
->fs
.xattr
.copied_len
+= write_count
;
2006 * Now copy the content from sg list
2008 for (i
= 0; i
< cnt
; i
++) {
2009 if (write_count
> sg
[i
].iov_len
) {
2010 to_copy
= sg
[i
].iov_len
;
2012 to_copy
= write_count
;
2014 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2015 /* updating vs->off since we are not using below */
2017 write_count
-= to_copy
;
2023 static void coroutine_fn
v9fs_write(void *opaque
)
2033 V9fsPDU
*pdu
= opaque
;
2034 V9fsState
*s
= pdu
->s
;
2035 QEMUIOVector qiov_full
;
2038 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2040 pdu_complete(pdu
, err
);
2044 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2045 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2047 fidp
= get_fid(pdu
, fid
);
2052 if (fidp
->fid_type
== P9_FID_FILE
) {
2053 if (fidp
->fs
.fd
== -1) {
2057 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2059 * setxattr operation
2061 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2062 qiov_full
.iov
, qiov_full
.niov
);
2068 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2070 qemu_iovec_reset(&qiov
);
2071 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2073 print_sg(qiov
.iov
, qiov
.niov
);
2075 /* Loop in case of EINTR */
2077 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2082 } while (len
== -EINTR
&& !pdu
->cancelled
);
2084 /* IO error return the error */
2088 } while (total
< count
&& len
> 0);
2091 err
= pdu_marshal(pdu
, offset
, "d", total
);
2096 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2098 qemu_iovec_destroy(&qiov
);
2102 qemu_iovec_destroy(&qiov_full
);
2103 pdu_complete(pdu
, err
);
2106 static void coroutine_fn
v9fs_create(void *opaque
)
2118 V9fsString extension
;
2120 V9fsPDU
*pdu
= opaque
;
2122 v9fs_path_init(&path
);
2123 v9fs_string_init(&name
);
2124 v9fs_string_init(&extension
);
2125 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2126 &perm
, &mode
, &extension
);
2130 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2132 if (name_is_illegal(name
.data
)) {
2137 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2142 fidp
= get_fid(pdu
, fid
);
2147 if (perm
& P9_STAT_MODE_DIR
) {
2148 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2149 fidp
->uid
, -1, &stbuf
);
2153 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2157 v9fs_path_copy(&fidp
->path
, &path
);
2158 err
= v9fs_co_opendir(pdu
, fidp
);
2162 fidp
->fid_type
= P9_FID_DIR
;
2163 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2164 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2165 extension
.data
, -1 , &stbuf
);
2169 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2173 v9fs_path_copy(&fidp
->path
, &path
);
2174 } else if (perm
& P9_STAT_MODE_LINK
) {
2175 int32_t ofid
= atoi(extension
.data
);
2176 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2177 if (ofidp
== NULL
) {
2181 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2182 put_fid(pdu
, ofidp
);
2186 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2188 fidp
->fid_type
= P9_FID_NONE
;
2191 v9fs_path_copy(&fidp
->path
, &path
);
2192 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2194 fidp
->fid_type
= P9_FID_NONE
;
2197 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2199 uint32_t major
, minor
;
2202 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2219 nmode
|= perm
& 0777;
2220 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2221 makedev(major
, minor
), nmode
, &stbuf
);
2225 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2229 v9fs_path_copy(&fidp
->path
, &path
);
2230 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2231 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2232 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2236 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2240 v9fs_path_copy(&fidp
->path
, &path
);
2241 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2242 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2243 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2247 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2251 v9fs_path_copy(&fidp
->path
, &path
);
2253 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2254 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2258 fidp
->fid_type
= P9_FID_FILE
;
2259 fidp
->open_flags
= omode_to_uflags(mode
);
2260 if (fidp
->open_flags
& O_EXCL
) {
2262 * We let the host file system do O_EXCL check
2263 * We should not reclaim such fd
2265 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2268 iounit
= get_iounit(pdu
, &fidp
->path
);
2269 stat_to_qid(&stbuf
, &qid
);
2270 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2275 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2276 qid
.type
, qid
.version
, qid
.path
, iounit
);
2280 pdu_complete(pdu
, err
);
2281 v9fs_string_free(&name
);
2282 v9fs_string_free(&extension
);
2283 v9fs_path_free(&path
);
2286 static void coroutine_fn
v9fs_symlink(void *opaque
)
2288 V9fsPDU
*pdu
= opaque
;
2291 V9fsFidState
*dfidp
;
2299 v9fs_string_init(&name
);
2300 v9fs_string_init(&symname
);
2301 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2305 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2307 if (name_is_illegal(name
.data
)) {
2312 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2317 dfidp
= get_fid(pdu
, dfid
);
2318 if (dfidp
== NULL
) {
2322 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2326 stat_to_qid(&stbuf
, &qid
);
2327 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2332 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2333 qid
.type
, qid
.version
, qid
.path
);
2335 put_fid(pdu
, dfidp
);
2337 pdu_complete(pdu
, err
);
2338 v9fs_string_free(&name
);
2339 v9fs_string_free(&symname
);
2342 static void v9fs_flush(void *opaque
)
2347 V9fsPDU
*cancel_pdu
;
2348 V9fsPDU
*pdu
= opaque
;
2349 V9fsState
*s
= pdu
->s
;
2351 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2353 pdu_complete(pdu
, err
);
2356 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2358 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2359 if (cancel_pdu
->tag
== tag
) {
2364 cancel_pdu
->cancelled
= 1;
2366 * Wait for pdu to complete.
2368 qemu_co_queue_wait(&cancel_pdu
->complete
);
2369 cancel_pdu
->cancelled
= 0;
2370 pdu_free(cancel_pdu
);
2372 pdu_complete(pdu
, 7);
2375 static void coroutine_fn
v9fs_link(void *opaque
)
2377 V9fsPDU
*pdu
= opaque
;
2378 int32_t dfid
, oldfid
;
2379 V9fsFidState
*dfidp
, *oldfidp
;
2384 v9fs_string_init(&name
);
2385 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2389 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2391 if (name_is_illegal(name
.data
)) {
2396 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2401 dfidp
= get_fid(pdu
, dfid
);
2402 if (dfidp
== NULL
) {
2407 oldfidp
= get_fid(pdu
, oldfid
);
2408 if (oldfidp
== NULL
) {
2412 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2417 put_fid(pdu
, dfidp
);
2419 v9fs_string_free(&name
);
2420 pdu_complete(pdu
, err
);
2423 /* Only works with path name based fid */
2424 static void coroutine_fn
v9fs_remove(void *opaque
)
2430 V9fsPDU
*pdu
= opaque
;
2432 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2436 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2438 fidp
= get_fid(pdu
, fid
);
2443 /* if fs driver is not path based, return EOPNOTSUPP */
2444 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2449 * IF the file is unlinked, we cannot reopen
2450 * the file later. So don't reclaim fd
2452 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2456 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2461 /* For TREMOVE we need to clunk the fid even on failed remove */
2462 clunk_fid(pdu
->s
, fidp
->fid
);
2465 pdu_complete(pdu
, err
);
2468 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2472 int32_t dfid
, flags
;
2475 V9fsFidState
*dfidp
;
2476 V9fsPDU
*pdu
= opaque
;
2478 v9fs_string_init(&name
);
2479 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2484 if (name_is_illegal(name
.data
)) {
2489 if (!strcmp(".", name
.data
)) {
2494 if (!strcmp("..", name
.data
)) {
2499 dfidp
= get_fid(pdu
, dfid
);
2500 if (dfidp
== NULL
) {
2505 * IF the file is unlinked, we cannot reopen
2506 * the file later. So don't reclaim fd
2508 v9fs_path_init(&path
);
2509 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2513 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2517 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2522 put_fid(pdu
, dfidp
);
2523 v9fs_path_free(&path
);
2525 pdu_complete(pdu
, err
);
2526 v9fs_string_free(&name
);
2530 /* Only works with path name based fid */
2531 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2538 V9fsFidState
*tfidp
;
2539 V9fsState
*s
= pdu
->s
;
2540 V9fsFidState
*dirfidp
= NULL
;
2541 char *old_name
, *new_name
;
2543 v9fs_path_init(&new_path
);
2544 if (newdirfid
!= -1) {
2545 dirfidp
= get_fid(pdu
, newdirfid
);
2546 if (dirfidp
== NULL
) {
2550 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2551 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2553 old_name
= fidp
->path
.data
;
2554 end
= strrchr(old_name
, '/');
2560 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2561 strncat(new_name
, old_name
, end
- old_name
);
2562 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2563 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2566 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2571 * Fixup fid's pointing to the old name to
2572 * start pointing to the new name
2574 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2575 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2576 /* replace the name */
2577 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2582 put_fid(pdu
, dirfidp
);
2584 v9fs_path_free(&new_path
);
2589 /* Only works with path name based fid */
2590 static void coroutine_fn
v9fs_rename(void *opaque
)
2598 V9fsPDU
*pdu
= opaque
;
2599 V9fsState
*s
= pdu
->s
;
2601 v9fs_string_init(&name
);
2602 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2607 if (name_is_illegal(name
.data
)) {
2612 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2617 fidp
= get_fid(pdu
, fid
);
2622 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2623 /* if fs driver is not path based, return EOPNOTSUPP */
2624 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2628 v9fs_path_write_lock(s
);
2629 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2630 v9fs_path_unlock(s
);
2637 pdu_complete(pdu
, err
);
2638 v9fs_string_free(&name
);
2641 static void coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2642 V9fsString
*old_name
,
2644 V9fsString
*new_name
)
2646 V9fsFidState
*tfidp
;
2647 V9fsPath oldpath
, newpath
;
2648 V9fsState
*s
= pdu
->s
;
2651 v9fs_path_init(&oldpath
);
2652 v9fs_path_init(&newpath
);
2653 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2654 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2657 * Fixup fid's pointing to the old name to
2658 * start pointing to the new name
2660 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2661 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2662 /* replace the name */
2663 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2666 v9fs_path_free(&oldpath
);
2667 v9fs_path_free(&newpath
);
2670 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2671 V9fsString
*old_name
,
2673 V9fsString
*new_name
)
2676 V9fsState
*s
= pdu
->s
;
2677 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2679 olddirfidp
= get_fid(pdu
, olddirfid
);
2680 if (olddirfidp
== NULL
) {
2684 if (newdirfid
!= -1) {
2685 newdirfidp
= get_fid(pdu
, newdirfid
);
2686 if (newdirfidp
== NULL
) {
2691 newdirfidp
= get_fid(pdu
, olddirfid
);
2694 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2695 &newdirfidp
->path
, new_name
);
2699 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2700 /* Only for path based fid we need to do the below fixup */
2701 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2702 &newdirfidp
->path
, new_name
);
2706 put_fid(pdu
, olddirfidp
);
2709 put_fid(pdu
, newdirfidp
);
2714 static void coroutine_fn
v9fs_renameat(void *opaque
)
2718 V9fsPDU
*pdu
= opaque
;
2719 V9fsState
*s
= pdu
->s
;
2720 int32_t olddirfid
, newdirfid
;
2721 V9fsString old_name
, new_name
;
2723 v9fs_string_init(&old_name
);
2724 v9fs_string_init(&new_name
);
2725 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2726 &old_name
, &newdirfid
, &new_name
);
2731 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2736 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
2737 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
2742 v9fs_path_write_lock(s
);
2743 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2744 &old_name
, newdirfid
, &new_name
);
2745 v9fs_path_unlock(s
);
2751 pdu_complete(pdu
, err
);
2752 v9fs_string_free(&old_name
);
2753 v9fs_string_free(&new_name
);
2756 static void coroutine_fn
v9fs_wstat(void *opaque
)
2765 V9fsPDU
*pdu
= opaque
;
2767 v9fs_stat_init(&v9stat
);
2768 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2772 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2773 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2775 fidp
= get_fid(pdu
, fid
);
2780 /* do we need to sync the file? */
2781 if (donttouch_stat(&v9stat
)) {
2782 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2785 if (v9stat
.mode
!= -1) {
2787 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2791 v9_mode
= stat_to_v9mode(&stbuf
);
2792 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2793 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2794 /* Attempting to change the type */
2798 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2799 v9mode_to_mode(v9stat
.mode
,
2800 &v9stat
.extension
));
2805 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2806 struct timespec times
[2];
2807 if (v9stat
.atime
!= -1) {
2808 times
[0].tv_sec
= v9stat
.atime
;
2809 times
[0].tv_nsec
= 0;
2811 times
[0].tv_nsec
= UTIME_OMIT
;
2813 if (v9stat
.mtime
!= -1) {
2814 times
[1].tv_sec
= v9stat
.mtime
;
2815 times
[1].tv_nsec
= 0;
2817 times
[1].tv_nsec
= UTIME_OMIT
;
2819 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2824 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2825 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2830 if (v9stat
.name
.size
!= 0) {
2831 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2836 if (v9stat
.length
!= -1) {
2837 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2846 v9fs_stat_free(&v9stat
);
2847 pdu_complete(pdu
, err
);
2850 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2862 int32_t bsize_factor
;
2865 * compute bsize factor based on host file system block size
2868 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2869 if (!bsize_factor
) {
2872 f_type
= stbuf
->f_type
;
2873 f_bsize
= stbuf
->f_bsize
;
2874 f_bsize
*= bsize_factor
;
2876 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2877 * adjust(divide) the number of blocks, free blocks and available
2878 * blocks by bsize factor
2880 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2881 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2882 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2883 f_files
= stbuf
->f_files
;
2884 f_ffree
= stbuf
->f_ffree
;
2885 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2886 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2887 f_namelen
= stbuf
->f_namelen
;
2889 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2890 f_type
, f_bsize
, f_blocks
, f_bfree
,
2891 f_bavail
, f_files
, f_ffree
,
2892 fsid_val
, f_namelen
);
2895 static void coroutine_fn
v9fs_statfs(void *opaque
)
2901 struct statfs stbuf
;
2902 V9fsPDU
*pdu
= opaque
;
2903 V9fsState
*s
= pdu
->s
;
2905 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2909 fidp
= get_fid(pdu
, fid
);
2914 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2918 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2926 pdu_complete(pdu
, retval
);
2929 static void coroutine_fn
v9fs_mknod(void *opaque
)
2942 V9fsPDU
*pdu
= opaque
;
2944 v9fs_string_init(&name
);
2945 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2946 &major
, &minor
, &gid
);
2950 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2952 if (name_is_illegal(name
.data
)) {
2957 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2962 fidp
= get_fid(pdu
, fid
);
2967 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2968 makedev(major
, minor
), mode
, &stbuf
);
2972 stat_to_qid(&stbuf
, &qid
);
2973 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2978 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2979 qid
.type
, qid
.version
, qid
.path
);
2983 pdu_complete(pdu
, err
);
2984 v9fs_string_free(&name
);
2988 * Implement posix byte range locking code
2989 * Server side handling of locking code is very simple, because 9p server in
2990 * QEMU can handle only one client. And most of the lock handling
2991 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2992 * do any thing in * qemu 9p server side lock code path.
2993 * So when a TLOCK request comes, always return success
2995 static void coroutine_fn
v9fs_lock(void *opaque
)
3002 int32_t fid
, err
= 0;
3003 V9fsPDU
*pdu
= opaque
;
3005 status
= P9_LOCK_ERROR
;
3006 v9fs_string_init(&flock
.client_id
);
3007 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3008 &flock
.flags
, &flock
.start
, &flock
.length
,
3009 &flock
.proc_id
, &flock
.client_id
);
3013 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3014 flock
.type
, flock
.start
, flock
.length
);
3017 /* We support only block flag now (that too ignored currently) */
3018 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3022 fidp
= get_fid(pdu
, fid
);
3027 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3031 status
= P9_LOCK_SUCCESS
;
3035 err
= pdu_marshal(pdu
, offset
, "b", status
);
3039 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
3040 pdu_complete(pdu
, err
);
3041 v9fs_string_free(&flock
.client_id
);
3045 * When a TGETLOCK request comes, always return success because all lock
3046 * handling is done by client's VFS layer.
3048 static void coroutine_fn
v9fs_getlock(void *opaque
)
3054 int32_t fid
, err
= 0;
3055 V9fsPDU
*pdu
= opaque
;
3057 v9fs_string_init(&glock
.client_id
);
3058 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3059 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3064 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3065 glock
.type
, glock
.start
, glock
.length
);
3067 fidp
= get_fid(pdu
, fid
);
3072 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3076 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3077 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3078 glock
.start
, glock
.length
, glock
.proc_id
,
3084 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3085 glock
.length
, glock
.proc_id
);
3089 pdu_complete(pdu
, err
);
3090 v9fs_string_free(&glock
.client_id
);
3093 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3095 V9fsPDU
*pdu
= opaque
;
3106 v9fs_string_init(&name
);
3107 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3111 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3113 if (name_is_illegal(name
.data
)) {
3118 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3123 fidp
= get_fid(pdu
, fid
);
3128 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3132 stat_to_qid(&stbuf
, &qid
);
3133 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3138 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3139 qid
.type
, qid
.version
, qid
.path
, err
);
3143 pdu_complete(pdu
, err
);
3144 v9fs_string_free(&name
);
3147 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3153 int32_t fid
, newfid
;
3154 V9fsFidState
*file_fidp
;
3155 V9fsFidState
*xattr_fidp
= NULL
;
3156 V9fsPDU
*pdu
= opaque
;
3157 V9fsState
*s
= pdu
->s
;
3159 v9fs_string_init(&name
);
3160 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3164 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3166 file_fidp
= get_fid(pdu
, fid
);
3167 if (file_fidp
== NULL
) {
3171 xattr_fidp
= alloc_fid(s
, newfid
);
3172 if (xattr_fidp
== NULL
) {
3176 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3177 if (!v9fs_string_size(&name
)) {
3179 * listxattr request. Get the size first
3181 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3184 clunk_fid(s
, xattr_fidp
->fid
);
3188 * Read the xattr value
3190 xattr_fidp
->fs
.xattr
.len
= size
;
3191 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3192 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3194 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3195 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3196 xattr_fidp
->fs
.xattr
.value
,
3197 xattr_fidp
->fs
.xattr
.len
);
3199 clunk_fid(s
, xattr_fidp
->fid
);
3203 err
= pdu_marshal(pdu
, offset
, "q", size
);
3210 * specific xattr fid. We check for xattr
3211 * presence also collect the xattr size
3213 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3217 clunk_fid(s
, xattr_fidp
->fid
);
3221 * Read the xattr value
3223 xattr_fidp
->fs
.xattr
.len
= size
;
3224 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3225 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3227 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3228 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3229 &name
, xattr_fidp
->fs
.xattr
.value
,
3230 xattr_fidp
->fs
.xattr
.len
);
3232 clunk_fid(s
, xattr_fidp
->fid
);
3236 err
= pdu_marshal(pdu
, offset
, "q", size
);
3242 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3244 put_fid(pdu
, file_fidp
);
3246 put_fid(pdu
, xattr_fidp
);
3249 pdu_complete(pdu
, err
);
3250 v9fs_string_free(&name
);
3253 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3261 V9fsFidState
*file_fidp
;
3262 V9fsFidState
*xattr_fidp
;
3263 V9fsPDU
*pdu
= opaque
;
3265 v9fs_string_init(&name
);
3266 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3270 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3272 file_fidp
= get_fid(pdu
, fid
);
3273 if (file_fidp
== NULL
) {
3277 /* Make the file fid point to xattr */
3278 xattr_fidp
= file_fidp
;
3279 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3280 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3281 xattr_fidp
->fs
.xattr
.len
= size
;
3282 xattr_fidp
->fs
.xattr
.flags
= flags
;
3283 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3284 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3285 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3287 put_fid(pdu
, file_fidp
);
3289 pdu_complete(pdu
, err
);
3290 v9fs_string_free(&name
);
3293 static void coroutine_fn
v9fs_readlink(void *opaque
)
3295 V9fsPDU
*pdu
= opaque
;
3302 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3306 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3307 fidp
= get_fid(pdu
, fid
);
3313 v9fs_string_init(&target
);
3314 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3318 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3320 v9fs_string_free(&target
);
3324 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3325 v9fs_string_free(&target
);
3329 pdu_complete(pdu
, err
);
3332 static CoroutineEntry
*pdu_co_handlers
[] = {
3333 [P9_TREADDIR
] = v9fs_readdir
,
3334 [P9_TSTATFS
] = v9fs_statfs
,
3335 [P9_TGETATTR
] = v9fs_getattr
,
3336 [P9_TSETATTR
] = v9fs_setattr
,
3337 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3338 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3339 [P9_TMKNOD
] = v9fs_mknod
,
3340 [P9_TRENAME
] = v9fs_rename
,
3341 [P9_TLOCK
] = v9fs_lock
,
3342 [P9_TGETLOCK
] = v9fs_getlock
,
3343 [P9_TRENAMEAT
] = v9fs_renameat
,
3344 [P9_TREADLINK
] = v9fs_readlink
,
3345 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3346 [P9_TMKDIR
] = v9fs_mkdir
,
3347 [P9_TVERSION
] = v9fs_version
,
3348 [P9_TLOPEN
] = v9fs_open
,
3349 [P9_TATTACH
] = v9fs_attach
,
3350 [P9_TSTAT
] = v9fs_stat
,
3351 [P9_TWALK
] = v9fs_walk
,
3352 [P9_TCLUNK
] = v9fs_clunk
,
3353 [P9_TFSYNC
] = v9fs_fsync
,
3354 [P9_TOPEN
] = v9fs_open
,
3355 [P9_TREAD
] = v9fs_read
,
3357 [P9_TAUTH
] = v9fs_auth
,
3359 [P9_TFLUSH
] = v9fs_flush
,
3360 [P9_TLINK
] = v9fs_link
,
3361 [P9_TSYMLINK
] = v9fs_symlink
,
3362 [P9_TCREATE
] = v9fs_create
,
3363 [P9_TLCREATE
] = v9fs_lcreate
,
3364 [P9_TWRITE
] = v9fs_write
,
3365 [P9_TWSTAT
] = v9fs_wstat
,
3366 [P9_TREMOVE
] = v9fs_remove
,
3369 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3371 V9fsPDU
*pdu
= opaque
;
3372 pdu_complete(pdu
, -EOPNOTSUPP
);
3375 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3377 V9fsPDU
*pdu
= opaque
;
3378 pdu_complete(pdu
, -EROFS
);
3381 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3408 void pdu_submit(V9fsPDU
*pdu
)
3411 CoroutineEntry
*handler
;
3412 V9fsState
*s
= pdu
->s
;
3414 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3415 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3416 handler
= v9fs_op_not_supp
;
3418 handler
= pdu_co_handlers
[pdu
->id
];
3421 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3422 handler
= v9fs_fs_ro
;
3424 co
= qemu_coroutine_create(handler
, pdu
);
3425 qemu_coroutine_enter(co
);
3428 /* Returns 0 on success, 1 on failure. */
3429 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3431 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
3438 /* initialize pdu allocator */
3439 QLIST_INIT(&s
->free_list
);
3440 QLIST_INIT(&s
->active_list
);
3441 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3442 QLIST_INSERT_HEAD(&s
->free_list
, &v
->pdus
[i
], next
);
3447 v9fs_path_init(&path
);
3449 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3452 /* We don't have a fsdev identified by fsdev_id */
3453 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3455 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3459 if (!s
->fsconf
.tag
) {
3460 /* we haven't specified a mount_tag */
3461 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3462 s
->fsconf
.fsdev_id
);
3466 s
->ctx
.export_flags
= fse
->export_flags
;
3467 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3468 s
->ctx
.exops
.get_st_gen
= NULL
;
3469 len
= strlen(s
->fsconf
.tag
);
3470 if (len
> MAX_TAG_LEN
- 1) {
3471 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3472 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3476 s
->tag
= g_strdup(s
->fsconf
.tag
);
3482 qemu_co_rwlock_init(&s
->rename_lock
);
3484 if (s
->ops
->init(&s
->ctx
) < 0) {
3485 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3486 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3491 * Check details of export path, We need to use fs driver
3492 * call back to do that. Since we are in the init path, we don't
3493 * use co-routines here.
3495 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3497 "error in converting name to path %s", strerror(errno
));
3500 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3501 error_setg(errp
, "share path %s does not exist", fse
->path
);
3503 } else if (!S_ISDIR(stat
.st_mode
)) {
3504 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3507 v9fs_path_free(&path
);
3512 g_free(s
->ctx
.fs_root
);
3514 v9fs_path_free(&path
);
3519 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3521 g_free(s
->ctx
.fs_root
);
3525 typedef struct VirtfsCoResetData
{
3528 } VirtfsCoResetData
;
3530 static void coroutine_fn
virtfs_co_reset(void *opaque
)
3532 VirtfsCoResetData
*data
= opaque
;
3534 virtfs_reset(&data
->pdu
);
3538 void v9fs_reset(V9fsState
*s
)
3540 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
3543 while (!QLIST_EMPTY(&s
->active_list
)) {
3544 aio_poll(qemu_get_aio_context(), true);
3547 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
3548 qemu_coroutine_enter(co
);
3550 while (!data
.done
) {
3551 aio_poll(qemu_get_aio_context(), true);
3555 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3558 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3559 error_report("Failed to get the resource limit");
3562 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3563 open_fd_rc
= rlim
.rlim_cur
/2;