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 * Cancelled pdu are added back to the freelist
634 if (!pdu
->cancelled
) {
635 QLIST_REMOVE(pdu
, next
);
636 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
641 * We don't do error checking for pdu_marshal/unmarshal here
642 * because we always expect to have enough space to encode
645 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
647 int8_t id
= pdu
->id
+ 1; /* Response */
648 V9fsState
*s
= pdu
->s
;
654 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
657 str
.data
= strerror(err
);
658 str
.size
= strlen(str
.data
);
660 len
+= pdu_marshal(pdu
, len
, "s", &str
);
664 len
+= pdu_marshal(pdu
, len
, "d", err
);
666 if (s
->proto_version
== V9FS_PROTO_2000L
) {
669 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
672 /* fill out the header */
673 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
675 /* keep these in sync */
679 pdu_push_and_notify(pdu
);
681 /* Now wakeup anybody waiting in flush for this request */
682 qemu_co_queue_next(&pdu
->complete
);
687 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
692 if (mode
& P9_STAT_MODE_DIR
) {
696 if (mode
& P9_STAT_MODE_SYMLINK
) {
699 if (mode
& P9_STAT_MODE_SOCKET
) {
702 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
705 if (mode
& P9_STAT_MODE_DEVICE
) {
706 if (extension
->size
&& extension
->data
[0] == 'c') {
717 if (mode
& P9_STAT_MODE_SETUID
) {
720 if (mode
& P9_STAT_MODE_SETGID
) {
723 if (mode
& P9_STAT_MODE_SETVTX
) {
730 static int donttouch_stat(V9fsStat
*stat
)
732 if (stat
->type
== -1 &&
734 stat
->qid
.type
== -1 &&
735 stat
->qid
.version
== -1 &&
736 stat
->qid
.path
== -1 &&
740 stat
->length
== -1 &&
747 stat
->n_muid
== -1) {
754 static void v9fs_stat_init(V9fsStat
*stat
)
756 v9fs_string_init(&stat
->name
);
757 v9fs_string_init(&stat
->uid
);
758 v9fs_string_init(&stat
->gid
);
759 v9fs_string_init(&stat
->muid
);
760 v9fs_string_init(&stat
->extension
);
763 static void v9fs_stat_free(V9fsStat
*stat
)
765 v9fs_string_free(&stat
->name
);
766 v9fs_string_free(&stat
->uid
);
767 v9fs_string_free(&stat
->gid
);
768 v9fs_string_free(&stat
->muid
);
769 v9fs_string_free(&stat
->extension
);
772 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
776 mode
= stbuf
->st_mode
& 0777;
777 if (S_ISDIR(stbuf
->st_mode
)) {
778 mode
|= P9_STAT_MODE_DIR
;
781 if (S_ISLNK(stbuf
->st_mode
)) {
782 mode
|= P9_STAT_MODE_SYMLINK
;
785 if (S_ISSOCK(stbuf
->st_mode
)) {
786 mode
|= P9_STAT_MODE_SOCKET
;
789 if (S_ISFIFO(stbuf
->st_mode
)) {
790 mode
|= P9_STAT_MODE_NAMED_PIPE
;
793 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
794 mode
|= P9_STAT_MODE_DEVICE
;
797 if (stbuf
->st_mode
& S_ISUID
) {
798 mode
|= P9_STAT_MODE_SETUID
;
801 if (stbuf
->st_mode
& S_ISGID
) {
802 mode
|= P9_STAT_MODE_SETGID
;
805 if (stbuf
->st_mode
& S_ISVTX
) {
806 mode
|= P9_STAT_MODE_SETVTX
;
812 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
813 const struct stat
*stbuf
,
819 memset(v9stat
, 0, sizeof(*v9stat
));
821 stat_to_qid(stbuf
, &v9stat
->qid
);
822 v9stat
->mode
= stat_to_v9mode(stbuf
);
823 v9stat
->atime
= stbuf
->st_atime
;
824 v9stat
->mtime
= stbuf
->st_mtime
;
825 v9stat
->length
= stbuf
->st_size
;
827 v9fs_string_free(&v9stat
->uid
);
828 v9fs_string_free(&v9stat
->gid
);
829 v9fs_string_free(&v9stat
->muid
);
831 v9stat
->n_uid
= stbuf
->st_uid
;
832 v9stat
->n_gid
= stbuf
->st_gid
;
835 v9fs_string_free(&v9stat
->extension
);
837 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
838 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
842 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
843 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
844 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
845 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
846 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
847 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
848 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
851 str
= strrchr(name
->data
, '/');
858 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
861 v9fs_string_size(&v9stat
->name
) +
862 v9fs_string_size(&v9stat
->uid
) +
863 v9fs_string_size(&v9stat
->gid
) +
864 v9fs_string_size(&v9stat
->muid
) +
865 v9fs_string_size(&v9stat
->extension
);
869 #define P9_STATS_MODE 0x00000001ULL
870 #define P9_STATS_NLINK 0x00000002ULL
871 #define P9_STATS_UID 0x00000004ULL
872 #define P9_STATS_GID 0x00000008ULL
873 #define P9_STATS_RDEV 0x00000010ULL
874 #define P9_STATS_ATIME 0x00000020ULL
875 #define P9_STATS_MTIME 0x00000040ULL
876 #define P9_STATS_CTIME 0x00000080ULL
877 #define P9_STATS_INO 0x00000100ULL
878 #define P9_STATS_SIZE 0x00000200ULL
879 #define P9_STATS_BLOCKS 0x00000400ULL
881 #define P9_STATS_BTIME 0x00000800ULL
882 #define P9_STATS_GEN 0x00001000ULL
883 #define P9_STATS_DATA_VERSION 0x00002000ULL
885 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
886 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
889 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
890 V9fsStatDotl
*v9lstat
)
892 memset(v9lstat
, 0, sizeof(*v9lstat
));
894 v9lstat
->st_mode
= stbuf
->st_mode
;
895 v9lstat
->st_nlink
= stbuf
->st_nlink
;
896 v9lstat
->st_uid
= stbuf
->st_uid
;
897 v9lstat
->st_gid
= stbuf
->st_gid
;
898 v9lstat
->st_rdev
= stbuf
->st_rdev
;
899 v9lstat
->st_size
= stbuf
->st_size
;
900 v9lstat
->st_blksize
= stbuf
->st_blksize
;
901 v9lstat
->st_blocks
= stbuf
->st_blocks
;
902 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
903 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
904 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
905 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
906 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
907 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
908 /* Currently we only support BASIC fields in stat */
909 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
911 stat_to_qid(stbuf
, &v9lstat
->qid
);
914 static void print_sg(struct iovec
*sg
, int cnt
)
918 printf("sg[%d]: {", cnt
);
919 for (i
= 0; i
< cnt
; i
++) {
923 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
928 /* Will call this only for path name based fid */
929 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
932 v9fs_path_init(&str
);
933 v9fs_path_copy(&str
, dst
);
934 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
935 v9fs_path_free(&str
);
938 static inline bool is_ro_export(FsContext
*ctx
)
940 return ctx
->export_flags
& V9FS_RDONLY
;
943 static void coroutine_fn
v9fs_version(void *opaque
)
946 V9fsPDU
*pdu
= opaque
;
947 V9fsState
*s
= pdu
->s
;
951 v9fs_string_init(&version
);
952 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
957 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
961 if (!strcmp(version
.data
, "9P2000.u")) {
962 s
->proto_version
= V9FS_PROTO_2000U
;
963 } else if (!strcmp(version
.data
, "9P2000.L")) {
964 s
->proto_version
= V9FS_PROTO_2000L
;
966 v9fs_string_sprintf(&version
, "unknown");
969 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
975 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
977 pdu_complete(pdu
, offset
);
978 v9fs_string_free(&version
);
981 static void coroutine_fn
v9fs_attach(void *opaque
)
983 V9fsPDU
*pdu
= opaque
;
984 V9fsState
*s
= pdu
->s
;
985 int32_t fid
, afid
, n_uname
;
986 V9fsString uname
, aname
;
992 v9fs_string_init(&uname
);
993 v9fs_string_init(&aname
);
994 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
995 &afid
, &uname
, &aname
, &n_uname
);
999 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
1001 fidp
= alloc_fid(s
, fid
);
1006 fidp
->uid
= n_uname
;
1007 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1013 err
= fid_to_qid(pdu
, fidp
, &qid
);
1019 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1025 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1026 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1027 qid
.type
, qid
.version
, qid
.path
);
1029 * disable migration if we haven't done already.
1030 * attach could get called multiple times for the same export.
1032 if (!s
->migration_blocker
) {
1034 error_setg(&s
->migration_blocker
,
1035 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1036 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1037 migrate_add_blocker(s
->migration_blocker
);
1042 pdu_complete(pdu
, err
);
1043 v9fs_string_free(&uname
);
1044 v9fs_string_free(&aname
);
1047 static void coroutine_fn
v9fs_stat(void *opaque
)
1055 V9fsPDU
*pdu
= opaque
;
1057 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1061 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1063 fidp
= get_fid(pdu
, fid
);
1068 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1072 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1076 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1078 v9fs_stat_free(&v9stat
);
1081 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1082 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1084 v9fs_stat_free(&v9stat
);
1088 pdu_complete(pdu
, err
);
1091 static void coroutine_fn
v9fs_getattr(void *opaque
)
1098 uint64_t request_mask
;
1099 V9fsStatDotl v9stat_dotl
;
1100 V9fsPDU
*pdu
= opaque
;
1101 V9fsState
*s
= pdu
->s
;
1103 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1107 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1109 fidp
= get_fid(pdu
, fid
);
1115 * Currently we only support BASIC fields in stat, so there is no
1116 * need to look at request_mask.
1118 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1122 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1124 /* fill st_gen if requested and supported by underlying fs */
1125 if (request_mask
& P9_STATS_GEN
) {
1126 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1129 /* we have valid st_gen: update result mask */
1130 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1133 /* request cancelled, e.g. by Tflush */
1136 /* failed to get st_gen: not fatal, ignore */
1140 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1145 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1146 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1147 v9stat_dotl
.st_gid
);
1151 pdu_complete(pdu
, retval
);
1154 /* Attribute flags */
1155 #define P9_ATTR_MODE (1 << 0)
1156 #define P9_ATTR_UID (1 << 1)
1157 #define P9_ATTR_GID (1 << 2)
1158 #define P9_ATTR_SIZE (1 << 3)
1159 #define P9_ATTR_ATIME (1 << 4)
1160 #define P9_ATTR_MTIME (1 << 5)
1161 #define P9_ATTR_CTIME (1 << 6)
1162 #define P9_ATTR_ATIME_SET (1 << 7)
1163 #define P9_ATTR_MTIME_SET (1 << 8)
1165 #define P9_ATTR_MASK 127
1167 static void coroutine_fn
v9fs_setattr(void *opaque
)
1174 V9fsPDU
*pdu
= opaque
;
1176 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1181 fidp
= get_fid(pdu
, fid
);
1186 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1187 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1192 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1193 struct timespec times
[2];
1194 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1195 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1196 times
[0].tv_sec
= v9iattr
.atime_sec
;
1197 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1199 times
[0].tv_nsec
= UTIME_NOW
;
1202 times
[0].tv_nsec
= UTIME_OMIT
;
1204 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1205 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1206 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1207 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1209 times
[1].tv_nsec
= UTIME_NOW
;
1212 times
[1].tv_nsec
= UTIME_OMIT
;
1214 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1220 * If the only valid entry in iattr is ctime we can call
1221 * chown(-1,-1) to update the ctime of the file
1223 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1224 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1225 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1226 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1229 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1232 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1238 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1239 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1248 pdu_complete(pdu
, err
);
1251 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1257 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1262 for (i
= 0; i
< nwnames
; i
++) {
1263 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1272 static bool name_is_illegal(const char *name
)
1274 return !*name
|| strchr(name
, '/') != NULL
;
1277 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1280 qid1
->type
!= qid2
->type
||
1281 qid1
->version
!= qid2
->version
||
1282 qid1
->path
!= qid2
->path
;
1285 static void coroutine_fn
v9fs_walk(void *opaque
)
1288 V9fsQID
*qids
= NULL
;
1290 V9fsPath dpath
, path
;
1294 int32_t fid
, newfid
;
1295 V9fsString
*wnames
= NULL
;
1297 V9fsFidState
*newfidp
= NULL
;
1298 V9fsPDU
*pdu
= opaque
;
1299 V9fsState
*s
= pdu
->s
;
1302 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1304 pdu_complete(pdu
, err
);
1309 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1311 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1312 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1313 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1314 for (i
= 0; i
< nwnames
; i
++) {
1315 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1319 if (name_is_illegal(wnames
[i
].data
)) {
1325 } else if (nwnames
> P9_MAXWELEM
) {
1329 fidp
= get_fid(pdu
, fid
);
1335 v9fs_path_init(&dpath
);
1336 v9fs_path_init(&path
);
1338 err
= fid_to_qid(pdu
, fidp
, &qid
);
1344 * Both dpath and path initially poin to fidp.
1345 * Needed to handle request with nwnames == 0
1347 v9fs_path_copy(&dpath
, &fidp
->path
);
1348 v9fs_path_copy(&path
, &fidp
->path
);
1349 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1350 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1351 strcmp("..", wnames
[name_idx
].data
)) {
1352 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1358 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1362 stat_to_qid(&stbuf
, &qid
);
1363 v9fs_path_copy(&dpath
, &path
);
1365 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1367 if (fid
== newfid
) {
1368 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1369 v9fs_path_copy(&fidp
->path
, &path
);
1371 newfidp
= alloc_fid(s
, newfid
);
1372 if (newfidp
== NULL
) {
1376 newfidp
->uid
= fidp
->uid
;
1377 v9fs_path_copy(&newfidp
->path
, &path
);
1379 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1380 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1384 put_fid(pdu
, newfidp
);
1386 v9fs_path_free(&dpath
);
1387 v9fs_path_free(&path
);
1389 pdu_complete(pdu
, err
);
1390 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1391 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1392 v9fs_string_free(&wnames
[name_idx
]);
1399 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1401 struct statfs stbuf
;
1403 V9fsState
*s
= pdu
->s
;
1406 * iounit should be multiples of f_bsize (host filesystem block size
1407 * and as well as less than (client msize - P9_IOHDRSZ))
1409 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1410 iounit
= stbuf
.f_bsize
;
1411 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1414 iounit
= s
->msize
- P9_IOHDRSZ
;
1419 static void coroutine_fn
v9fs_open(void *opaque
)
1430 V9fsPDU
*pdu
= opaque
;
1431 V9fsState
*s
= pdu
->s
;
1433 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1434 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1437 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1443 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1445 fidp
= get_fid(pdu
, fid
);
1450 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1452 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1456 stat_to_qid(&stbuf
, &qid
);
1457 if (S_ISDIR(stbuf
.st_mode
)) {
1458 err
= v9fs_co_opendir(pdu
, fidp
);
1462 fidp
->fid_type
= P9_FID_DIR
;
1463 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1469 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1470 flags
= get_dotl_openflags(s
, mode
);
1472 flags
= omode_to_uflags(mode
);
1474 if (is_ro_export(&s
->ctx
)) {
1475 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1476 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1481 err
= v9fs_co_open(pdu
, fidp
, flags
);
1485 fidp
->fid_type
= P9_FID_FILE
;
1486 fidp
->open_flags
= flags
;
1487 if (flags
& O_EXCL
) {
1489 * We let the host file system do O_EXCL check
1490 * We should not reclaim such fd
1492 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1494 iounit
= get_iounit(pdu
, &fidp
->path
);
1495 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1501 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1502 qid
.type
, qid
.version
, qid
.path
, iounit
);
1506 pdu_complete(pdu
, err
);
1509 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1511 int32_t dfid
, flags
, mode
;
1520 V9fsPDU
*pdu
= opaque
;
1522 v9fs_string_init(&name
);
1523 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1524 &name
, &flags
, &mode
, &gid
);
1528 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1530 if (name_is_illegal(name
.data
)) {
1535 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1540 fidp
= get_fid(pdu
, dfid
);
1546 flags
= get_dotl_openflags(pdu
->s
, flags
);
1547 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1548 flags
| O_CREAT
, mode
, &stbuf
);
1552 fidp
->fid_type
= P9_FID_FILE
;
1553 fidp
->open_flags
= flags
;
1554 if (flags
& O_EXCL
) {
1556 * We let the host file system do O_EXCL check
1557 * We should not reclaim such fd
1559 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1561 iounit
= get_iounit(pdu
, &fidp
->path
);
1562 stat_to_qid(&stbuf
, &qid
);
1563 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1568 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1569 qid
.type
, qid
.version
, qid
.path
, iounit
);
1573 pdu_complete(pdu
, err
);
1574 v9fs_string_free(&name
);
1577 static void v9fs_fsync(void *opaque
)
1584 V9fsPDU
*pdu
= opaque
;
1586 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1590 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1592 fidp
= get_fid(pdu
, fid
);
1597 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1603 pdu_complete(pdu
, err
);
1606 static void coroutine_fn
v9fs_clunk(void *opaque
)
1612 V9fsPDU
*pdu
= opaque
;
1613 V9fsState
*s
= pdu
->s
;
1615 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1619 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1621 fidp
= clunk_fid(s
, fid
);
1627 * Bump the ref so that put_fid will
1631 err
= put_fid(pdu
, fidp
);
1636 pdu_complete(pdu
, err
);
1639 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1640 uint64_t off
, uint32_t max_count
)
1646 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
1647 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
1649 xattr_len
= fidp
->fs
.xattr
.len
;
1650 read_count
= xattr_len
- off
;
1651 if (read_count
> max_count
) {
1652 read_count
= max_count
;
1653 } else if (read_count
< 0) {
1655 * read beyond XATTR value
1659 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1665 err
= v9fs_pack(elem
->in_sg
, elem
->in_num
, offset
,
1666 ((char *)fidp
->fs
.xattr
.value
) + off
,
1675 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1684 off_t saved_dir_pos
;
1685 struct dirent
*dent
;
1687 /* save the directory position */
1688 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1689 if (saved_dir_pos
< 0) {
1690 return saved_dir_pos
;
1694 v9fs_path_init(&path
);
1696 v9fs_readdir_lock(&fidp
->fs
.dir
);
1698 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1702 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1706 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1710 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1714 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1715 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1717 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1719 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1720 /* Ran out of buffer. Set dir back to old position and return */
1721 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1722 v9fs_stat_free(&v9stat
);
1723 v9fs_path_free(&path
);
1727 v9fs_stat_free(&v9stat
);
1728 v9fs_path_free(&path
);
1729 saved_dir_pos
= dent
->d_off
;
1732 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1734 v9fs_path_free(&path
);
1742 * Create a QEMUIOVector for a sub-region of PDU iovecs
1744 * @qiov: uninitialized QEMUIOVector
1745 * @skip: number of bytes to skip from beginning of PDU
1746 * @size: number of bytes to include
1747 * @is_write: true - write, false - read
1749 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1750 * with qemu_iovec_destroy().
1752 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1753 size_t skip
, size_t size
,
1760 virtio_init_iov_from_pdu(pdu
, &iov
, &niov
, is_write
);
1762 qemu_iovec_init_external(&elem
, iov
, niov
);
1763 qemu_iovec_init(qiov
, niov
);
1764 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1767 static void coroutine_fn
v9fs_read(void *opaque
)
1776 V9fsPDU
*pdu
= opaque
;
1777 V9fsState
*s
= pdu
->s
;
1779 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1783 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1785 fidp
= get_fid(pdu
, fid
);
1790 if (fidp
->fid_type
== P9_FID_DIR
) {
1793 v9fs_co_rewinddir(pdu
, fidp
);
1795 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1800 err
= pdu_marshal(pdu
, offset
, "d", count
);
1804 err
+= offset
+ count
;
1805 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1806 QEMUIOVector qiov_full
;
1810 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1811 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1813 qemu_iovec_reset(&qiov
);
1814 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1816 print_sg(qiov
.iov
, qiov
.niov
);
1818 /* Loop in case of EINTR */
1820 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1825 } while (len
== -EINTR
&& !pdu
->cancelled
);
1827 /* IO error return the error */
1829 goto out_free_iovec
;
1831 } while (count
< max_count
&& len
> 0);
1832 err
= pdu_marshal(pdu
, offset
, "d", count
);
1834 goto out_free_iovec
;
1836 err
+= offset
+ count
;
1838 qemu_iovec_destroy(&qiov
);
1839 qemu_iovec_destroy(&qiov_full
);
1840 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1841 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1845 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1849 pdu_complete(pdu
, err
);
1852 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1855 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1856 * size of type (1) + size of name.size (2) + strlen(name.data)
1858 return 24 + v9fs_string_size(name
);
1861 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1869 off_t saved_dir_pos
;
1870 struct dirent
*dent
;
1872 /* save the directory position */
1873 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1874 if (saved_dir_pos
< 0) {
1875 return saved_dir_pos
;
1879 v9fs_readdir_lock(&fidp
->fs
.dir
);
1881 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1885 v9fs_string_init(&name
);
1886 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1887 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1888 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1890 /* Ran out of buffer. Set dir back to old position and return */
1891 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1892 v9fs_string_free(&name
);
1896 * Fill up just the path field of qid because the client uses
1897 * only that. To fill the entire qid structure we will have
1898 * to stat each dirent found, which is expensive
1900 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1901 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1902 /* Fill the other fields with dummy values */
1906 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1907 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1909 dent
->d_type
, &name
);
1911 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1914 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1915 v9fs_string_free(&name
);
1919 v9fs_string_free(&name
);
1920 saved_dir_pos
= dent
->d_off
;
1923 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1931 static void coroutine_fn
v9fs_readdir(void *opaque
)
1937 uint64_t initial_offset
;
1940 V9fsPDU
*pdu
= opaque
;
1942 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1943 &initial_offset
, &max_count
);
1947 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1949 fidp
= get_fid(pdu
, fid
);
1954 if (!fidp
->fs
.dir
.stream
) {
1958 if (initial_offset
== 0) {
1959 v9fs_co_rewinddir(pdu
, fidp
);
1961 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1963 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1968 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1972 retval
+= count
+ offset
;
1973 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1977 pdu_complete(pdu
, retval
);
1980 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1981 uint64_t off
, uint32_t count
,
1982 struct iovec
*sg
, int cnt
)
1991 xattr_len
= fidp
->fs
.xattr
.len
;
1992 write_count
= xattr_len
- off
;
1993 if (write_count
> count
) {
1994 write_count
= count
;
1995 } else if (write_count
< 0) {
1997 * write beyond XATTR value len specified in
2003 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2008 fidp
->fs
.xattr
.copied_len
+= write_count
;
2010 * Now copy the content from sg list
2012 for (i
= 0; i
< cnt
; i
++) {
2013 if (write_count
> sg
[i
].iov_len
) {
2014 to_copy
= sg
[i
].iov_len
;
2016 to_copy
= write_count
;
2018 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2019 /* updating vs->off since we are not using below */
2021 write_count
-= to_copy
;
2027 static void coroutine_fn
v9fs_write(void *opaque
)
2037 V9fsPDU
*pdu
= opaque
;
2038 V9fsState
*s
= pdu
->s
;
2039 QEMUIOVector qiov_full
;
2042 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2044 pdu_complete(pdu
, err
);
2048 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2049 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2051 fidp
= get_fid(pdu
, fid
);
2056 if (fidp
->fid_type
== P9_FID_FILE
) {
2057 if (fidp
->fs
.fd
== -1) {
2061 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2063 * setxattr operation
2065 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2066 qiov_full
.iov
, qiov_full
.niov
);
2072 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2074 qemu_iovec_reset(&qiov
);
2075 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2077 print_sg(qiov
.iov
, qiov
.niov
);
2079 /* Loop in case of EINTR */
2081 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2086 } while (len
== -EINTR
&& !pdu
->cancelled
);
2088 /* IO error return the error */
2092 } while (total
< count
&& len
> 0);
2095 err
= pdu_marshal(pdu
, offset
, "d", total
);
2100 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2102 qemu_iovec_destroy(&qiov
);
2106 qemu_iovec_destroy(&qiov_full
);
2107 pdu_complete(pdu
, err
);
2110 static void coroutine_fn
v9fs_create(void *opaque
)
2122 V9fsString extension
;
2124 V9fsPDU
*pdu
= opaque
;
2126 v9fs_path_init(&path
);
2127 v9fs_string_init(&name
);
2128 v9fs_string_init(&extension
);
2129 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2130 &perm
, &mode
, &extension
);
2134 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2136 if (name_is_illegal(name
.data
)) {
2141 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2146 fidp
= get_fid(pdu
, fid
);
2151 if (perm
& P9_STAT_MODE_DIR
) {
2152 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2153 fidp
->uid
, -1, &stbuf
);
2157 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2161 v9fs_path_copy(&fidp
->path
, &path
);
2162 err
= v9fs_co_opendir(pdu
, fidp
);
2166 fidp
->fid_type
= P9_FID_DIR
;
2167 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2168 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2169 extension
.data
, -1 , &stbuf
);
2173 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2177 v9fs_path_copy(&fidp
->path
, &path
);
2178 } else if (perm
& P9_STAT_MODE_LINK
) {
2179 int32_t ofid
= atoi(extension
.data
);
2180 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2181 if (ofidp
== NULL
) {
2185 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2186 put_fid(pdu
, ofidp
);
2190 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2192 fidp
->fid_type
= P9_FID_NONE
;
2195 v9fs_path_copy(&fidp
->path
, &path
);
2196 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2198 fidp
->fid_type
= P9_FID_NONE
;
2201 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2203 uint32_t major
, minor
;
2206 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2223 nmode
|= perm
& 0777;
2224 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2225 makedev(major
, minor
), nmode
, &stbuf
);
2229 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2233 v9fs_path_copy(&fidp
->path
, &path
);
2234 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2235 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2236 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2240 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2244 v9fs_path_copy(&fidp
->path
, &path
);
2245 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2246 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2247 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2251 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2255 v9fs_path_copy(&fidp
->path
, &path
);
2257 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2258 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2262 fidp
->fid_type
= P9_FID_FILE
;
2263 fidp
->open_flags
= omode_to_uflags(mode
);
2264 if (fidp
->open_flags
& O_EXCL
) {
2266 * We let the host file system do O_EXCL check
2267 * We should not reclaim such fd
2269 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2272 iounit
= get_iounit(pdu
, &fidp
->path
);
2273 stat_to_qid(&stbuf
, &qid
);
2274 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2279 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2280 qid
.type
, qid
.version
, qid
.path
, iounit
);
2284 pdu_complete(pdu
, err
);
2285 v9fs_string_free(&name
);
2286 v9fs_string_free(&extension
);
2287 v9fs_path_free(&path
);
2290 static void coroutine_fn
v9fs_symlink(void *opaque
)
2292 V9fsPDU
*pdu
= opaque
;
2295 V9fsFidState
*dfidp
;
2303 v9fs_string_init(&name
);
2304 v9fs_string_init(&symname
);
2305 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2309 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2311 if (name_is_illegal(name
.data
)) {
2316 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2321 dfidp
= get_fid(pdu
, dfid
);
2322 if (dfidp
== NULL
) {
2326 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2330 stat_to_qid(&stbuf
, &qid
);
2331 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2336 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2337 qid
.type
, qid
.version
, qid
.path
);
2339 put_fid(pdu
, dfidp
);
2341 pdu_complete(pdu
, err
);
2342 v9fs_string_free(&name
);
2343 v9fs_string_free(&symname
);
2346 static void v9fs_flush(void *opaque
)
2351 V9fsPDU
*cancel_pdu
;
2352 V9fsPDU
*pdu
= opaque
;
2353 V9fsState
*s
= pdu
->s
;
2355 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2357 pdu_complete(pdu
, err
);
2360 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2362 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2363 if (cancel_pdu
->tag
== tag
) {
2368 cancel_pdu
->cancelled
= 1;
2370 * Wait for pdu to complete.
2372 qemu_co_queue_wait(&cancel_pdu
->complete
);
2373 cancel_pdu
->cancelled
= 0;
2374 pdu_free(cancel_pdu
);
2376 pdu_complete(pdu
, 7);
2379 static void coroutine_fn
v9fs_link(void *opaque
)
2381 V9fsPDU
*pdu
= opaque
;
2382 int32_t dfid
, oldfid
;
2383 V9fsFidState
*dfidp
, *oldfidp
;
2388 v9fs_string_init(&name
);
2389 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2393 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2395 if (name_is_illegal(name
.data
)) {
2400 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2405 dfidp
= get_fid(pdu
, dfid
);
2406 if (dfidp
== NULL
) {
2411 oldfidp
= get_fid(pdu
, oldfid
);
2412 if (oldfidp
== NULL
) {
2416 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2421 put_fid(pdu
, dfidp
);
2423 v9fs_string_free(&name
);
2424 pdu_complete(pdu
, err
);
2427 /* Only works with path name based fid */
2428 static void coroutine_fn
v9fs_remove(void *opaque
)
2434 V9fsPDU
*pdu
= opaque
;
2436 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2440 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2442 fidp
= get_fid(pdu
, fid
);
2447 /* if fs driver is not path based, return EOPNOTSUPP */
2448 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2453 * IF the file is unlinked, we cannot reopen
2454 * the file later. So don't reclaim fd
2456 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2460 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2465 /* For TREMOVE we need to clunk the fid even on failed remove */
2466 clunk_fid(pdu
->s
, fidp
->fid
);
2469 pdu_complete(pdu
, err
);
2472 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2476 int32_t dfid
, flags
;
2479 V9fsFidState
*dfidp
;
2480 V9fsPDU
*pdu
= opaque
;
2482 v9fs_string_init(&name
);
2483 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2488 if (name_is_illegal(name
.data
)) {
2493 if (!strcmp(".", name
.data
)) {
2498 if (!strcmp("..", name
.data
)) {
2503 dfidp
= get_fid(pdu
, dfid
);
2504 if (dfidp
== NULL
) {
2509 * IF the file is unlinked, we cannot reopen
2510 * the file later. So don't reclaim fd
2512 v9fs_path_init(&path
);
2513 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2517 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2521 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2526 put_fid(pdu
, dfidp
);
2527 v9fs_path_free(&path
);
2529 pdu_complete(pdu
, err
);
2530 v9fs_string_free(&name
);
2534 /* Only works with path name based fid */
2535 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2542 V9fsFidState
*tfidp
;
2543 V9fsState
*s
= pdu
->s
;
2544 V9fsFidState
*dirfidp
= NULL
;
2545 char *old_name
, *new_name
;
2547 v9fs_path_init(&new_path
);
2548 if (newdirfid
!= -1) {
2549 dirfidp
= get_fid(pdu
, newdirfid
);
2550 if (dirfidp
== NULL
) {
2554 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2555 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2557 old_name
= fidp
->path
.data
;
2558 end
= strrchr(old_name
, '/');
2564 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2565 strncat(new_name
, old_name
, end
- old_name
);
2566 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2567 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2570 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2575 * Fixup fid's pointing to the old name to
2576 * start pointing to the new name
2578 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2579 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2580 /* replace the name */
2581 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2586 put_fid(pdu
, dirfidp
);
2588 v9fs_path_free(&new_path
);
2593 /* Only works with path name based fid */
2594 static void coroutine_fn
v9fs_rename(void *opaque
)
2602 V9fsPDU
*pdu
= opaque
;
2603 V9fsState
*s
= pdu
->s
;
2605 v9fs_string_init(&name
);
2606 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2611 if (name_is_illegal(name
.data
)) {
2616 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2621 fidp
= get_fid(pdu
, fid
);
2626 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2627 /* if fs driver is not path based, return EOPNOTSUPP */
2628 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2632 v9fs_path_write_lock(s
);
2633 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2634 v9fs_path_unlock(s
);
2641 pdu_complete(pdu
, err
);
2642 v9fs_string_free(&name
);
2645 static void coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2646 V9fsString
*old_name
,
2648 V9fsString
*new_name
)
2650 V9fsFidState
*tfidp
;
2651 V9fsPath oldpath
, newpath
;
2652 V9fsState
*s
= pdu
->s
;
2655 v9fs_path_init(&oldpath
);
2656 v9fs_path_init(&newpath
);
2657 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2658 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2661 * Fixup fid's pointing to the old name to
2662 * start pointing to the new name
2664 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2665 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2666 /* replace the name */
2667 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2670 v9fs_path_free(&oldpath
);
2671 v9fs_path_free(&newpath
);
2674 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2675 V9fsString
*old_name
,
2677 V9fsString
*new_name
)
2680 V9fsState
*s
= pdu
->s
;
2681 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2683 olddirfidp
= get_fid(pdu
, olddirfid
);
2684 if (olddirfidp
== NULL
) {
2688 if (newdirfid
!= -1) {
2689 newdirfidp
= get_fid(pdu
, newdirfid
);
2690 if (newdirfidp
== NULL
) {
2695 newdirfidp
= get_fid(pdu
, olddirfid
);
2698 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2699 &newdirfidp
->path
, new_name
);
2703 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2704 /* Only for path based fid we need to do the below fixup */
2705 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2706 &newdirfidp
->path
, new_name
);
2710 put_fid(pdu
, olddirfidp
);
2713 put_fid(pdu
, newdirfidp
);
2718 static void coroutine_fn
v9fs_renameat(void *opaque
)
2722 V9fsPDU
*pdu
= opaque
;
2723 V9fsState
*s
= pdu
->s
;
2724 int32_t olddirfid
, newdirfid
;
2725 V9fsString old_name
, new_name
;
2727 v9fs_string_init(&old_name
);
2728 v9fs_string_init(&new_name
);
2729 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2730 &old_name
, &newdirfid
, &new_name
);
2735 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2740 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
2741 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
2746 v9fs_path_write_lock(s
);
2747 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2748 &old_name
, newdirfid
, &new_name
);
2749 v9fs_path_unlock(s
);
2755 pdu_complete(pdu
, err
);
2756 v9fs_string_free(&old_name
);
2757 v9fs_string_free(&new_name
);
2760 static void coroutine_fn
v9fs_wstat(void *opaque
)
2769 V9fsPDU
*pdu
= opaque
;
2771 v9fs_stat_init(&v9stat
);
2772 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2776 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2777 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2779 fidp
= get_fid(pdu
, fid
);
2784 /* do we need to sync the file? */
2785 if (donttouch_stat(&v9stat
)) {
2786 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2789 if (v9stat
.mode
!= -1) {
2791 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2795 v9_mode
= stat_to_v9mode(&stbuf
);
2796 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2797 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2798 /* Attempting to change the type */
2802 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2803 v9mode_to_mode(v9stat
.mode
,
2804 &v9stat
.extension
));
2809 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2810 struct timespec times
[2];
2811 if (v9stat
.atime
!= -1) {
2812 times
[0].tv_sec
= v9stat
.atime
;
2813 times
[0].tv_nsec
= 0;
2815 times
[0].tv_nsec
= UTIME_OMIT
;
2817 if (v9stat
.mtime
!= -1) {
2818 times
[1].tv_sec
= v9stat
.mtime
;
2819 times
[1].tv_nsec
= 0;
2821 times
[1].tv_nsec
= UTIME_OMIT
;
2823 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2828 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2829 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2834 if (v9stat
.name
.size
!= 0) {
2835 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2840 if (v9stat
.length
!= -1) {
2841 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2850 v9fs_stat_free(&v9stat
);
2851 pdu_complete(pdu
, err
);
2854 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2866 int32_t bsize_factor
;
2869 * compute bsize factor based on host file system block size
2872 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2873 if (!bsize_factor
) {
2876 f_type
= stbuf
->f_type
;
2877 f_bsize
= stbuf
->f_bsize
;
2878 f_bsize
*= bsize_factor
;
2880 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2881 * adjust(divide) the number of blocks, free blocks and available
2882 * blocks by bsize factor
2884 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2885 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2886 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2887 f_files
= stbuf
->f_files
;
2888 f_ffree
= stbuf
->f_ffree
;
2889 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2890 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2891 f_namelen
= stbuf
->f_namelen
;
2893 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2894 f_type
, f_bsize
, f_blocks
, f_bfree
,
2895 f_bavail
, f_files
, f_ffree
,
2896 fsid_val
, f_namelen
);
2899 static void coroutine_fn
v9fs_statfs(void *opaque
)
2905 struct statfs stbuf
;
2906 V9fsPDU
*pdu
= opaque
;
2907 V9fsState
*s
= pdu
->s
;
2909 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2913 fidp
= get_fid(pdu
, fid
);
2918 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2922 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2930 pdu_complete(pdu
, retval
);
2933 static void coroutine_fn
v9fs_mknod(void *opaque
)
2946 V9fsPDU
*pdu
= opaque
;
2948 v9fs_string_init(&name
);
2949 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2950 &major
, &minor
, &gid
);
2954 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2956 if (name_is_illegal(name
.data
)) {
2961 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2966 fidp
= get_fid(pdu
, fid
);
2971 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2972 makedev(major
, minor
), mode
, &stbuf
);
2976 stat_to_qid(&stbuf
, &qid
);
2977 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2982 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2983 qid
.type
, qid
.version
, qid
.path
);
2987 pdu_complete(pdu
, err
);
2988 v9fs_string_free(&name
);
2992 * Implement posix byte range locking code
2993 * Server side handling of locking code is very simple, because 9p server in
2994 * QEMU can handle only one client. And most of the lock handling
2995 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2996 * do any thing in * qemu 9p server side lock code path.
2997 * So when a TLOCK request comes, always return success
2999 static void coroutine_fn
v9fs_lock(void *opaque
)
3006 int32_t fid
, err
= 0;
3007 V9fsPDU
*pdu
= opaque
;
3009 status
= P9_LOCK_ERROR
;
3010 v9fs_string_init(&flock
.client_id
);
3011 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3012 &flock
.flags
, &flock
.start
, &flock
.length
,
3013 &flock
.proc_id
, &flock
.client_id
);
3017 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3018 flock
.type
, flock
.start
, flock
.length
);
3021 /* We support only block flag now (that too ignored currently) */
3022 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3026 fidp
= get_fid(pdu
, fid
);
3031 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3035 status
= P9_LOCK_SUCCESS
;
3039 err
= pdu_marshal(pdu
, offset
, "b", status
);
3043 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
3044 pdu_complete(pdu
, err
);
3045 v9fs_string_free(&flock
.client_id
);
3049 * When a TGETLOCK request comes, always return success because all lock
3050 * handling is done by client's VFS layer.
3052 static void coroutine_fn
v9fs_getlock(void *opaque
)
3058 int32_t fid
, err
= 0;
3059 V9fsPDU
*pdu
= opaque
;
3061 v9fs_string_init(&glock
.client_id
);
3062 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3063 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3068 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3069 glock
.type
, glock
.start
, glock
.length
);
3071 fidp
= get_fid(pdu
, fid
);
3076 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3080 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3081 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3082 glock
.start
, glock
.length
, glock
.proc_id
,
3088 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3089 glock
.length
, glock
.proc_id
);
3093 pdu_complete(pdu
, err
);
3094 v9fs_string_free(&glock
.client_id
);
3097 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3099 V9fsPDU
*pdu
= opaque
;
3110 v9fs_string_init(&name
);
3111 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3115 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3117 if (name_is_illegal(name
.data
)) {
3122 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3127 fidp
= get_fid(pdu
, fid
);
3132 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3136 stat_to_qid(&stbuf
, &qid
);
3137 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3142 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3143 qid
.type
, qid
.version
, qid
.path
, err
);
3147 pdu_complete(pdu
, err
);
3148 v9fs_string_free(&name
);
3151 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3157 int32_t fid
, newfid
;
3158 V9fsFidState
*file_fidp
;
3159 V9fsFidState
*xattr_fidp
= NULL
;
3160 V9fsPDU
*pdu
= opaque
;
3161 V9fsState
*s
= pdu
->s
;
3163 v9fs_string_init(&name
);
3164 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3168 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3170 file_fidp
= get_fid(pdu
, fid
);
3171 if (file_fidp
== NULL
) {
3175 xattr_fidp
= alloc_fid(s
, newfid
);
3176 if (xattr_fidp
== NULL
) {
3180 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3181 if (!v9fs_string_size(&name
)) {
3183 * listxattr request. Get the size first
3185 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3188 clunk_fid(s
, xattr_fidp
->fid
);
3192 * Read the xattr value
3194 xattr_fidp
->fs
.xattr
.len
= size
;
3195 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3196 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3198 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3199 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3200 xattr_fidp
->fs
.xattr
.value
,
3201 xattr_fidp
->fs
.xattr
.len
);
3203 clunk_fid(s
, xattr_fidp
->fid
);
3207 err
= pdu_marshal(pdu
, offset
, "q", size
);
3214 * specific xattr fid. We check for xattr
3215 * presence also collect the xattr size
3217 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3221 clunk_fid(s
, xattr_fidp
->fid
);
3225 * Read the xattr value
3227 xattr_fidp
->fs
.xattr
.len
= size
;
3228 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3229 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3231 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3232 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3233 &name
, xattr_fidp
->fs
.xattr
.value
,
3234 xattr_fidp
->fs
.xattr
.len
);
3236 clunk_fid(s
, xattr_fidp
->fid
);
3240 err
= pdu_marshal(pdu
, offset
, "q", size
);
3246 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3248 put_fid(pdu
, file_fidp
);
3250 put_fid(pdu
, xattr_fidp
);
3253 pdu_complete(pdu
, err
);
3254 v9fs_string_free(&name
);
3257 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3265 V9fsFidState
*file_fidp
;
3266 V9fsFidState
*xattr_fidp
;
3267 V9fsPDU
*pdu
= opaque
;
3269 v9fs_string_init(&name
);
3270 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3274 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3276 file_fidp
= get_fid(pdu
, fid
);
3277 if (file_fidp
== NULL
) {
3281 /* Make the file fid point to xattr */
3282 xattr_fidp
= file_fidp
;
3283 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3284 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3285 xattr_fidp
->fs
.xattr
.len
= size
;
3286 xattr_fidp
->fs
.xattr
.flags
= flags
;
3287 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3288 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3289 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3291 put_fid(pdu
, file_fidp
);
3293 pdu_complete(pdu
, err
);
3294 v9fs_string_free(&name
);
3297 static void coroutine_fn
v9fs_readlink(void *opaque
)
3299 V9fsPDU
*pdu
= opaque
;
3306 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3310 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3311 fidp
= get_fid(pdu
, fid
);
3317 v9fs_string_init(&target
);
3318 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3322 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3324 v9fs_string_free(&target
);
3328 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3329 v9fs_string_free(&target
);
3333 pdu_complete(pdu
, err
);
3336 static CoroutineEntry
*pdu_co_handlers
[] = {
3337 [P9_TREADDIR
] = v9fs_readdir
,
3338 [P9_TSTATFS
] = v9fs_statfs
,
3339 [P9_TGETATTR
] = v9fs_getattr
,
3340 [P9_TSETATTR
] = v9fs_setattr
,
3341 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3342 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3343 [P9_TMKNOD
] = v9fs_mknod
,
3344 [P9_TRENAME
] = v9fs_rename
,
3345 [P9_TLOCK
] = v9fs_lock
,
3346 [P9_TGETLOCK
] = v9fs_getlock
,
3347 [P9_TRENAMEAT
] = v9fs_renameat
,
3348 [P9_TREADLINK
] = v9fs_readlink
,
3349 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3350 [P9_TMKDIR
] = v9fs_mkdir
,
3351 [P9_TVERSION
] = v9fs_version
,
3352 [P9_TLOPEN
] = v9fs_open
,
3353 [P9_TATTACH
] = v9fs_attach
,
3354 [P9_TSTAT
] = v9fs_stat
,
3355 [P9_TWALK
] = v9fs_walk
,
3356 [P9_TCLUNK
] = v9fs_clunk
,
3357 [P9_TFSYNC
] = v9fs_fsync
,
3358 [P9_TOPEN
] = v9fs_open
,
3359 [P9_TREAD
] = v9fs_read
,
3361 [P9_TAUTH
] = v9fs_auth
,
3363 [P9_TFLUSH
] = v9fs_flush
,
3364 [P9_TLINK
] = v9fs_link
,
3365 [P9_TSYMLINK
] = v9fs_symlink
,
3366 [P9_TCREATE
] = v9fs_create
,
3367 [P9_TLCREATE
] = v9fs_lcreate
,
3368 [P9_TWRITE
] = v9fs_write
,
3369 [P9_TWSTAT
] = v9fs_wstat
,
3370 [P9_TREMOVE
] = v9fs_remove
,
3373 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3375 V9fsPDU
*pdu
= opaque
;
3376 pdu_complete(pdu
, -EOPNOTSUPP
);
3379 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3381 V9fsPDU
*pdu
= opaque
;
3382 pdu_complete(pdu
, -EROFS
);
3385 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3412 void pdu_submit(V9fsPDU
*pdu
)
3415 CoroutineEntry
*handler
;
3416 V9fsState
*s
= pdu
->s
;
3418 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3419 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3420 handler
= v9fs_op_not_supp
;
3422 handler
= pdu_co_handlers
[pdu
->id
];
3425 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3426 handler
= v9fs_fs_ro
;
3428 co
= qemu_coroutine_create(handler
, pdu
);
3429 qemu_coroutine_enter(co
);
3432 /* Returns 0 on success, 1 on failure. */
3433 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3435 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
3442 /* initialize pdu allocator */
3443 QLIST_INIT(&s
->free_list
);
3444 QLIST_INIT(&s
->active_list
);
3445 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3446 QLIST_INSERT_HEAD(&s
->free_list
, &v
->pdus
[i
], next
);
3451 v9fs_path_init(&path
);
3453 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3456 /* We don't have a fsdev identified by fsdev_id */
3457 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3459 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3463 if (!s
->fsconf
.tag
) {
3464 /* we haven't specified a mount_tag */
3465 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3466 s
->fsconf
.fsdev_id
);
3470 s
->ctx
.export_flags
= fse
->export_flags
;
3471 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3472 s
->ctx
.exops
.get_st_gen
= NULL
;
3473 len
= strlen(s
->fsconf
.tag
);
3474 if (len
> MAX_TAG_LEN
- 1) {
3475 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3476 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3480 s
->tag
= g_strdup(s
->fsconf
.tag
);
3486 qemu_co_rwlock_init(&s
->rename_lock
);
3488 if (s
->ops
->init(&s
->ctx
) < 0) {
3489 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3490 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3495 * Check details of export path, We need to use fs driver
3496 * call back to do that. Since we are in the init path, we don't
3497 * use co-routines here.
3499 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3501 "error in converting name to path %s", strerror(errno
));
3504 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3505 error_setg(errp
, "share path %s does not exist", fse
->path
);
3507 } else if (!S_ISDIR(stat
.st_mode
)) {
3508 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3511 v9fs_path_free(&path
);
3516 g_free(s
->ctx
.fs_root
);
3518 v9fs_path_free(&path
);
3523 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3525 g_free(s
->ctx
.fs_root
);
3529 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3532 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3533 error_report("Failed to get the resource limit");
3536 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3537 open_fd_rc
= rlim
.rlim_cur
/2;