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/blocker.h"
30 static int open_fd_rc
;
44 static ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
50 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
56 static ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
62 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
68 static int omode_to_uflags(int8_t mode
)
102 typedef struct DotlOpenflagMap
{
107 static int dotl_to_open_flags(int flags
)
111 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
112 * and P9_DOTL_NOACCESS
114 int oflags
= flags
& O_ACCMODE
;
116 DotlOpenflagMap dotl_oflag_map
[] = {
117 { P9_DOTL_CREATE
, O_CREAT
},
118 { P9_DOTL_EXCL
, O_EXCL
},
119 { P9_DOTL_NOCTTY
, O_NOCTTY
},
120 { P9_DOTL_TRUNC
, O_TRUNC
},
121 { P9_DOTL_APPEND
, O_APPEND
},
122 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
123 { P9_DOTL_DSYNC
, O_DSYNC
},
124 { P9_DOTL_FASYNC
, FASYNC
},
125 { P9_DOTL_DIRECT
, O_DIRECT
},
126 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
127 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
128 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
129 { P9_DOTL_NOATIME
, O_NOATIME
},
130 { P9_DOTL_SYNC
, O_SYNC
},
133 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
134 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
135 oflags
|= dotl_oflag_map
[i
].open_flag
;
142 void cred_init(FsCred
*credp
)
150 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
154 * Filter the client open flags
156 flags
= dotl_to_open_flags(oflags
);
157 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
159 * Ignore direct disk access hint until the server supports it.
165 void v9fs_path_init(V9fsPath
*path
)
171 void v9fs_path_free(V9fsPath
*path
)
179 void GCC_FMT_ATTR(2, 3)
180 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
184 v9fs_path_free(path
);
187 /* Bump the size for including terminating NULL */
188 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
192 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
195 lhs
->data
= g_malloc(rhs
->size
);
196 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
197 lhs
->size
= rhs
->size
;
200 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
201 const char *name
, V9fsPath
*path
)
204 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
212 * Return TRUE if s1 is an ancestor of s2.
214 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
215 * As a special case, We treat s1 as ancestor of s2 if they are same!
217 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
219 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
220 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
227 static size_t v9fs_string_size(V9fsString
*str
)
233 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
234 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
237 if (f
->fid_type
== P9_FID_FILE
) {
238 if (f
->fs
.fd
== -1) {
240 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
241 } while (err
== -EINTR
&& !pdu
->cancelled
);
243 } else if (f
->fid_type
== P9_FID_DIR
) {
244 if (f
->fs
.dir
.stream
== NULL
) {
246 err
= v9fs_co_opendir(pdu
, f
);
247 } while (err
== -EINTR
&& !pdu
->cancelled
);
253 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
257 V9fsState
*s
= pdu
->s
;
259 for (f
= s
->fid_list
; f
; f
= f
->next
) {
263 * Update the fid ref upfront so that
264 * we don't get reclaimed when we yield
269 * check whether we need to reopen the
270 * file. We might have closed the fd
271 * while trying to free up some file
274 err
= v9fs_reopen_fid(pdu
, f
);
280 * Mark the fid as referenced so that the LRU
281 * reclaim won't close the file descriptor
283 f
->flags
|= FID_REFERENCED
;
290 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
294 for (f
= s
->fid_list
; f
; f
= f
->next
) {
295 /* If fid is already there return NULL */
301 f
= g_malloc0(sizeof(V9fsFidState
));
303 f
->fid_type
= P9_FID_NONE
;
306 * Mark the fid as referenced so that the LRU
307 * reclaim won't close the file descriptor
309 f
->flags
|= FID_REFERENCED
;
310 f
->next
= s
->fid_list
;
313 v9fs_readdir_init(&f
->fs
.dir
);
314 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
319 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
323 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
324 /* getxattr/listxattr fid */
328 * if this is fid for setxattr. clunk should
329 * result in setxattr localcall
331 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
332 /* clunk after partial write */
336 if (fidp
->fs
.xattr
.len
) {
337 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
338 fidp
->fs
.xattr
.value
,
340 fidp
->fs
.xattr
.flags
);
342 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
345 v9fs_string_free(&fidp
->fs
.xattr
.name
);
347 g_free(fidp
->fs
.xattr
.value
);
351 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
355 if (fidp
->fid_type
== P9_FID_FILE
) {
356 /* If we reclaimed the fd no need to close */
357 if (fidp
->fs
.fd
!= -1) {
358 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
360 } else if (fidp
->fid_type
== P9_FID_DIR
) {
361 if (fidp
->fs
.dir
.stream
!= NULL
) {
362 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
364 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
365 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
367 v9fs_path_free(&fidp
->path
);
372 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
377 * Don't free the fid if it is in reclaim list
379 if (!fidp
->ref
&& fidp
->clunked
) {
380 if (fidp
->fid
== pdu
->s
->root_fid
) {
382 * if the clunked fid is root fid then we
383 * have unmounted the fs on the client side.
384 * delete the migration blocker. Ideally, this
385 * should be hooked to transport close notification
387 if (pdu
->s
->migration_blocker
) {
388 migrate_del_blocker(pdu
->s
->migration_blocker
);
389 error_free(pdu
->s
->migration_blocker
);
390 pdu
->s
->migration_blocker
= NULL
;
393 return free_fid(pdu
, fidp
);
398 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
400 V9fsFidState
**fidpp
, *fidp
;
402 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
403 if ((*fidpp
)->fid
== fid
) {
407 if (*fidpp
== NULL
) {
416 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
418 int reclaim_count
= 0;
419 V9fsState
*s
= pdu
->s
;
420 V9fsFidState
*f
, *reclaim_list
= NULL
;
422 for (f
= s
->fid_list
; f
; f
= f
->next
) {
424 * Unlink fids cannot be reclaimed. Check
425 * for them and skip them. Also skip fids
426 * currently being operated on.
428 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
432 * if it is a recently referenced fid
433 * we leave the fid untouched and clear the
434 * reference bit. We come back to it later
435 * in the next iteration. (a simple LRU without
436 * moving list elements around)
438 if (f
->flags
& FID_REFERENCED
) {
439 f
->flags
&= ~FID_REFERENCED
;
443 * Add fids to reclaim list.
445 if (f
->fid_type
== P9_FID_FILE
) {
446 if (f
->fs
.fd
!= -1) {
448 * Up the reference count so that
449 * a clunk request won't free this fid
452 f
->rclm_lst
= reclaim_list
;
454 f
->fs_reclaim
.fd
= f
->fs
.fd
;
458 } else if (f
->fid_type
== P9_FID_DIR
) {
459 if (f
->fs
.dir
.stream
!= NULL
) {
461 * Up the reference count so that
462 * a clunk request won't free this fid
465 f
->rclm_lst
= reclaim_list
;
467 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
468 f
->fs
.dir
.stream
= NULL
;
472 if (reclaim_count
>= open_fd_rc
) {
477 * Now close the fid in reclaim list. Free them if they
478 * are already clunked.
480 while (reclaim_list
) {
482 reclaim_list
= f
->rclm_lst
;
483 if (f
->fid_type
== P9_FID_FILE
) {
484 v9fs_co_close(pdu
, &f
->fs_reclaim
);
485 } else if (f
->fid_type
== P9_FID_DIR
) {
486 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
490 * Now drop the fid reference, free it
497 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
500 V9fsState
*s
= pdu
->s
;
501 V9fsFidState
*fidp
, head_fid
;
503 head_fid
.next
= s
->fid_list
;
504 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
505 if (fidp
->path
.size
!= path
->size
) {
508 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
509 /* Mark the fid non reclaimable. */
510 fidp
->flags
|= FID_NON_RECLAIMABLE
;
512 /* reopen the file/dir if already closed */
513 err
= v9fs_reopen_fid(pdu
, fidp
);
518 * Go back to head of fid list because
519 * the list could have got updated when
520 * switched to the worker thread
530 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
532 V9fsState
*s
= pdu
->s
;
536 while (s
->fid_list
) {
542 s
->fid_list
= fidp
->next
;
549 #define P9_QID_TYPE_DIR 0x80
550 #define P9_QID_TYPE_SYMLINK 0x02
552 #define P9_STAT_MODE_DIR 0x80000000
553 #define P9_STAT_MODE_APPEND 0x40000000
554 #define P9_STAT_MODE_EXCL 0x20000000
555 #define P9_STAT_MODE_MOUNT 0x10000000
556 #define P9_STAT_MODE_AUTH 0x08000000
557 #define P9_STAT_MODE_TMP 0x04000000
558 #define P9_STAT_MODE_SYMLINK 0x02000000
559 #define P9_STAT_MODE_LINK 0x01000000
560 #define P9_STAT_MODE_DEVICE 0x00800000
561 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
562 #define P9_STAT_MODE_SOCKET 0x00100000
563 #define P9_STAT_MODE_SETUID 0x00080000
564 #define P9_STAT_MODE_SETGID 0x00040000
565 #define P9_STAT_MODE_SETVTX 0x00010000
567 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
568 P9_STAT_MODE_SYMLINK | \
569 P9_STAT_MODE_LINK | \
570 P9_STAT_MODE_DEVICE | \
571 P9_STAT_MODE_NAMED_PIPE | \
574 /* This is the algorithm from ufs in spfs */
575 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
579 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
580 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
581 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
582 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
584 if (S_ISDIR(stbuf
->st_mode
)) {
585 qidp
->type
|= P9_QID_TYPE_DIR
;
587 if (S_ISLNK(stbuf
->st_mode
)) {
588 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
592 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
598 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
602 stat_to_qid(&stbuf
, qidp
);
606 V9fsPDU
*pdu_alloc(V9fsState
*s
)
610 if (!QLIST_EMPTY(&s
->free_list
)) {
611 pdu
= QLIST_FIRST(&s
->free_list
);
612 QLIST_REMOVE(pdu
, next
);
613 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
618 void pdu_free(V9fsPDU
*pdu
)
620 V9fsState
*s
= pdu
->s
;
622 g_assert(!pdu
->cancelled
);
623 QLIST_REMOVE(pdu
, next
);
624 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
627 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
629 int8_t id
= pdu
->id
+ 1; /* Response */
630 V9fsState
*s
= pdu
->s
;
637 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
640 str
.data
= strerror(err
);
641 str
.size
= strlen(str
.data
);
643 ret
= pdu_marshal(pdu
, len
, "s", &str
);
651 ret
= pdu_marshal(pdu
, len
, "d", err
);
657 if (s
->proto_version
== V9FS_PROTO_2000L
) {
660 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
663 /* fill out the header */
664 if (pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
) < 0) {
668 /* keep these in sync */
673 pdu
->s
->transport
->push_and_notify(pdu
);
675 /* Now wakeup anybody waiting in flush for this request */
676 if (!qemu_co_queue_next(&pdu
->complete
)) {
681 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
686 if (mode
& P9_STAT_MODE_DIR
) {
690 if (mode
& P9_STAT_MODE_SYMLINK
) {
693 if (mode
& P9_STAT_MODE_SOCKET
) {
696 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
699 if (mode
& P9_STAT_MODE_DEVICE
) {
700 if (extension
->size
&& extension
->data
[0] == 'c') {
711 if (mode
& P9_STAT_MODE_SETUID
) {
714 if (mode
& P9_STAT_MODE_SETGID
) {
717 if (mode
& P9_STAT_MODE_SETVTX
) {
724 static int donttouch_stat(V9fsStat
*stat
)
726 if (stat
->type
== -1 &&
728 stat
->qid
.type
== -1 &&
729 stat
->qid
.version
== -1 &&
730 stat
->qid
.path
== -1 &&
734 stat
->length
== -1 &&
741 stat
->n_muid
== -1) {
748 static void v9fs_stat_init(V9fsStat
*stat
)
750 v9fs_string_init(&stat
->name
);
751 v9fs_string_init(&stat
->uid
);
752 v9fs_string_init(&stat
->gid
);
753 v9fs_string_init(&stat
->muid
);
754 v9fs_string_init(&stat
->extension
);
757 static void v9fs_stat_free(V9fsStat
*stat
)
759 v9fs_string_free(&stat
->name
);
760 v9fs_string_free(&stat
->uid
);
761 v9fs_string_free(&stat
->gid
);
762 v9fs_string_free(&stat
->muid
);
763 v9fs_string_free(&stat
->extension
);
766 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
770 mode
= stbuf
->st_mode
& 0777;
771 if (S_ISDIR(stbuf
->st_mode
)) {
772 mode
|= P9_STAT_MODE_DIR
;
775 if (S_ISLNK(stbuf
->st_mode
)) {
776 mode
|= P9_STAT_MODE_SYMLINK
;
779 if (S_ISSOCK(stbuf
->st_mode
)) {
780 mode
|= P9_STAT_MODE_SOCKET
;
783 if (S_ISFIFO(stbuf
->st_mode
)) {
784 mode
|= P9_STAT_MODE_NAMED_PIPE
;
787 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
788 mode
|= P9_STAT_MODE_DEVICE
;
791 if (stbuf
->st_mode
& S_ISUID
) {
792 mode
|= P9_STAT_MODE_SETUID
;
795 if (stbuf
->st_mode
& S_ISGID
) {
796 mode
|= P9_STAT_MODE_SETGID
;
799 if (stbuf
->st_mode
& S_ISVTX
) {
800 mode
|= P9_STAT_MODE_SETVTX
;
806 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*path
,
807 const char *basename
,
808 const struct stat
*stbuf
,
813 memset(v9stat
, 0, sizeof(*v9stat
));
815 stat_to_qid(stbuf
, &v9stat
->qid
);
816 v9stat
->mode
= stat_to_v9mode(stbuf
);
817 v9stat
->atime
= stbuf
->st_atime
;
818 v9stat
->mtime
= stbuf
->st_mtime
;
819 v9stat
->length
= stbuf
->st_size
;
821 v9fs_string_free(&v9stat
->uid
);
822 v9fs_string_free(&v9stat
->gid
);
823 v9fs_string_free(&v9stat
->muid
);
825 v9stat
->n_uid
= stbuf
->st_uid
;
826 v9stat
->n_gid
= stbuf
->st_gid
;
829 v9fs_string_free(&v9stat
->extension
);
831 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
832 err
= v9fs_co_readlink(pdu
, path
, &v9stat
->extension
);
836 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
837 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
838 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
839 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
840 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
841 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
842 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
845 v9fs_string_sprintf(&v9stat
->name
, "%s", basename
);
848 v9fs_string_size(&v9stat
->name
) +
849 v9fs_string_size(&v9stat
->uid
) +
850 v9fs_string_size(&v9stat
->gid
) +
851 v9fs_string_size(&v9stat
->muid
) +
852 v9fs_string_size(&v9stat
->extension
);
856 #define P9_STATS_MODE 0x00000001ULL
857 #define P9_STATS_NLINK 0x00000002ULL
858 #define P9_STATS_UID 0x00000004ULL
859 #define P9_STATS_GID 0x00000008ULL
860 #define P9_STATS_RDEV 0x00000010ULL
861 #define P9_STATS_ATIME 0x00000020ULL
862 #define P9_STATS_MTIME 0x00000040ULL
863 #define P9_STATS_CTIME 0x00000080ULL
864 #define P9_STATS_INO 0x00000100ULL
865 #define P9_STATS_SIZE 0x00000200ULL
866 #define P9_STATS_BLOCKS 0x00000400ULL
868 #define P9_STATS_BTIME 0x00000800ULL
869 #define P9_STATS_GEN 0x00001000ULL
870 #define P9_STATS_DATA_VERSION 0x00002000ULL
872 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
873 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
876 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
877 V9fsStatDotl
*v9lstat
)
879 memset(v9lstat
, 0, sizeof(*v9lstat
));
881 v9lstat
->st_mode
= stbuf
->st_mode
;
882 v9lstat
->st_nlink
= stbuf
->st_nlink
;
883 v9lstat
->st_uid
= stbuf
->st_uid
;
884 v9lstat
->st_gid
= stbuf
->st_gid
;
885 v9lstat
->st_rdev
= stbuf
->st_rdev
;
886 v9lstat
->st_size
= stbuf
->st_size
;
887 v9lstat
->st_blksize
= stbuf
->st_blksize
;
888 v9lstat
->st_blocks
= stbuf
->st_blocks
;
889 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
890 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
891 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
892 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
893 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
894 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
895 /* Currently we only support BASIC fields in stat */
896 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
898 stat_to_qid(stbuf
, &v9lstat
->qid
);
901 static void print_sg(struct iovec
*sg
, int cnt
)
905 printf("sg[%d]: {", cnt
);
906 for (i
= 0; i
< cnt
; i
++) {
910 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
915 /* Will call this only for path name based fid */
916 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
919 v9fs_path_init(&str
);
920 v9fs_path_copy(&str
, dst
);
921 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
922 v9fs_path_free(&str
);
925 static inline bool is_ro_export(FsContext
*ctx
)
927 return ctx
->export_flags
& V9FS_RDONLY
;
930 static void coroutine_fn
v9fs_version(void *opaque
)
933 V9fsPDU
*pdu
= opaque
;
934 V9fsState
*s
= pdu
->s
;
938 v9fs_string_init(&version
);
939 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
943 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
947 if (!strcmp(version
.data
, "9P2000.u")) {
948 s
->proto_version
= V9FS_PROTO_2000U
;
949 } else if (!strcmp(version
.data
, "9P2000.L")) {
950 s
->proto_version
= V9FS_PROTO_2000L
;
952 v9fs_string_sprintf(&version
, "unknown");
955 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
960 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
962 pdu_complete(pdu
, err
);
963 v9fs_string_free(&version
);
966 static void coroutine_fn
v9fs_attach(void *opaque
)
968 V9fsPDU
*pdu
= opaque
;
969 V9fsState
*s
= pdu
->s
;
970 int32_t fid
, afid
, n_uname
;
971 V9fsString uname
, aname
;
976 Error
*local_err
= NULL
;
978 v9fs_string_init(&uname
);
979 v9fs_string_init(&aname
);
980 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
981 &afid
, &uname
, &aname
, &n_uname
);
985 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
987 fidp
= alloc_fid(s
, fid
);
993 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
999 err
= fid_to_qid(pdu
, fidp
, &qid
);
1007 * disable migration if we haven't done already.
1008 * attach could get called multiple times for the same export.
1010 if (!s
->migration_blocker
) {
1011 error_setg(&s
->migration_blocker
,
1012 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1013 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1014 err
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1016 error_free(local_err
);
1017 error_free(s
->migration_blocker
);
1018 s
->migration_blocker
= NULL
;
1025 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1032 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1033 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1034 qid
.type
, qid
.version
, qid
.path
);
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
;
1054 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1058 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1060 fidp
= get_fid(pdu
, fid
);
1065 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1069 basename
= g_path_get_basename(fidp
->path
.data
);
1070 err
= stat_to_v9stat(pdu
, &fidp
->path
, basename
, &stbuf
, &v9stat
);
1075 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1077 v9fs_stat_free(&v9stat
);
1080 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1081 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1083 v9fs_stat_free(&v9stat
);
1087 pdu_complete(pdu
, err
);
1090 static void coroutine_fn
v9fs_getattr(void *opaque
)
1097 uint64_t request_mask
;
1098 V9fsStatDotl v9stat_dotl
;
1099 V9fsPDU
*pdu
= opaque
;
1100 V9fsState
*s
= pdu
->s
;
1102 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1106 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1108 fidp
= get_fid(pdu
, fid
);
1114 * Currently we only support BASIC fields in stat, so there is no
1115 * need to look at request_mask.
1117 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1121 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1123 /* fill st_gen if requested and supported by underlying fs */
1124 if (request_mask
& P9_STATS_GEN
) {
1125 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1128 /* we have valid st_gen: update result mask */
1129 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1132 /* request cancelled, e.g. by Tflush */
1135 /* failed to get st_gen: not fatal, ignore */
1139 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1144 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1145 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1146 v9stat_dotl
.st_gid
);
1150 pdu_complete(pdu
, retval
);
1153 /* Attribute flags */
1154 #define P9_ATTR_MODE (1 << 0)
1155 #define P9_ATTR_UID (1 << 1)
1156 #define P9_ATTR_GID (1 << 2)
1157 #define P9_ATTR_SIZE (1 << 3)
1158 #define P9_ATTR_ATIME (1 << 4)
1159 #define P9_ATTR_MTIME (1 << 5)
1160 #define P9_ATTR_CTIME (1 << 6)
1161 #define P9_ATTR_ATIME_SET (1 << 7)
1162 #define P9_ATTR_MTIME_SET (1 << 8)
1164 #define P9_ATTR_MASK 127
1166 static void coroutine_fn
v9fs_setattr(void *opaque
)
1173 V9fsPDU
*pdu
= opaque
;
1175 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1180 fidp
= get_fid(pdu
, fid
);
1185 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1186 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1191 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1192 struct timespec times
[2];
1193 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1194 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1195 times
[0].tv_sec
= v9iattr
.atime_sec
;
1196 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1198 times
[0].tv_nsec
= UTIME_NOW
;
1201 times
[0].tv_nsec
= UTIME_OMIT
;
1203 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1204 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1205 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1206 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1208 times
[1].tv_nsec
= UTIME_NOW
;
1211 times
[1].tv_nsec
= UTIME_OMIT
;
1213 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1219 * If the only valid entry in iattr is ctime we can call
1220 * chown(-1,-1) to update the ctime of the file
1222 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1223 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1224 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1225 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1228 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1231 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1237 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1238 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1247 pdu_complete(pdu
, err
);
1250 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1256 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1261 for (i
= 0; i
< nwnames
; i
++) {
1262 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1271 static bool name_is_illegal(const char *name
)
1273 return !*name
|| strchr(name
, '/') != NULL
;
1276 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1279 qid1
->type
!= qid2
->type
||
1280 qid1
->version
!= qid2
->version
||
1281 qid1
->path
!= qid2
->path
;
1284 static void coroutine_fn
v9fs_walk(void *opaque
)
1287 V9fsQID
*qids
= NULL
;
1289 V9fsPath dpath
, path
;
1293 int32_t fid
, newfid
;
1294 V9fsString
*wnames
= NULL
;
1296 V9fsFidState
*newfidp
= NULL
;
1297 V9fsPDU
*pdu
= opaque
;
1298 V9fsState
*s
= pdu
->s
;
1301 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1303 pdu_complete(pdu
, err
);
1308 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1310 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1311 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1312 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1313 for (i
= 0; i
< nwnames
; i
++) {
1314 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1318 if (name_is_illegal(wnames
[i
].data
)) {
1324 } else if (nwnames
> P9_MAXWELEM
) {
1328 fidp
= get_fid(pdu
, fid
);
1334 v9fs_path_init(&dpath
);
1335 v9fs_path_init(&path
);
1337 err
= fid_to_qid(pdu
, fidp
, &qid
);
1343 * Both dpath and path initially poin to fidp.
1344 * Needed to handle request with nwnames == 0
1346 v9fs_path_copy(&dpath
, &fidp
->path
);
1347 v9fs_path_copy(&path
, &fidp
->path
);
1348 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1349 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1350 strcmp("..", wnames
[name_idx
].data
)) {
1351 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1357 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1361 stat_to_qid(&stbuf
, &qid
);
1362 v9fs_path_copy(&dpath
, &path
);
1364 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1366 if (fid
== newfid
) {
1367 if (fidp
->fid_type
!= P9_FID_NONE
) {
1371 v9fs_path_copy(&fidp
->path
, &path
);
1373 newfidp
= alloc_fid(s
, newfid
);
1374 if (newfidp
== NULL
) {
1378 newfidp
->uid
= fidp
->uid
;
1379 v9fs_path_copy(&newfidp
->path
, &path
);
1381 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1382 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1386 put_fid(pdu
, newfidp
);
1388 v9fs_path_free(&dpath
);
1389 v9fs_path_free(&path
);
1391 pdu_complete(pdu
, err
);
1392 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1393 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1394 v9fs_string_free(&wnames
[name_idx
]);
1401 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1403 struct statfs stbuf
;
1405 V9fsState
*s
= pdu
->s
;
1408 * iounit should be multiples of f_bsize (host filesystem block size
1409 * and as well as less than (client msize - P9_IOHDRSZ))
1411 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1412 iounit
= stbuf
.f_bsize
;
1413 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1416 iounit
= s
->msize
- P9_IOHDRSZ
;
1421 static void coroutine_fn
v9fs_open(void *opaque
)
1432 V9fsPDU
*pdu
= opaque
;
1433 V9fsState
*s
= pdu
->s
;
1435 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1436 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1439 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1445 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1447 fidp
= get_fid(pdu
, fid
);
1452 if (fidp
->fid_type
!= P9_FID_NONE
) {
1457 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1461 stat_to_qid(&stbuf
, &qid
);
1462 if (S_ISDIR(stbuf
.st_mode
)) {
1463 err
= v9fs_co_opendir(pdu
, fidp
);
1467 fidp
->fid_type
= P9_FID_DIR
;
1468 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1474 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1475 flags
= get_dotl_openflags(s
, mode
);
1477 flags
= omode_to_uflags(mode
);
1479 if (is_ro_export(&s
->ctx
)) {
1480 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1481 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1486 err
= v9fs_co_open(pdu
, fidp
, flags
);
1490 fidp
->fid_type
= P9_FID_FILE
;
1491 fidp
->open_flags
= flags
;
1492 if (flags
& O_EXCL
) {
1494 * We let the host file system do O_EXCL check
1495 * We should not reclaim such fd
1497 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1499 iounit
= get_iounit(pdu
, &fidp
->path
);
1500 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1506 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1507 qid
.type
, qid
.version
, qid
.path
, iounit
);
1511 pdu_complete(pdu
, err
);
1514 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1516 int32_t dfid
, flags
, mode
;
1525 V9fsPDU
*pdu
= opaque
;
1527 v9fs_string_init(&name
);
1528 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1529 &name
, &flags
, &mode
, &gid
);
1533 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1535 if (name_is_illegal(name
.data
)) {
1540 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1545 fidp
= get_fid(pdu
, dfid
);
1550 if (fidp
->fid_type
!= P9_FID_NONE
) {
1555 flags
= get_dotl_openflags(pdu
->s
, flags
);
1556 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1557 flags
| O_CREAT
, mode
, &stbuf
);
1561 fidp
->fid_type
= P9_FID_FILE
;
1562 fidp
->open_flags
= flags
;
1563 if (flags
& O_EXCL
) {
1565 * We let the host file system do O_EXCL check
1566 * We should not reclaim such fd
1568 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1570 iounit
= get_iounit(pdu
, &fidp
->path
);
1571 stat_to_qid(&stbuf
, &qid
);
1572 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1577 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1578 qid
.type
, qid
.version
, qid
.path
, iounit
);
1582 pdu_complete(pdu
, err
);
1583 v9fs_string_free(&name
);
1586 static void coroutine_fn
v9fs_fsync(void *opaque
)
1593 V9fsPDU
*pdu
= opaque
;
1595 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1599 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1601 fidp
= get_fid(pdu
, fid
);
1606 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1612 pdu_complete(pdu
, err
);
1615 static void coroutine_fn
v9fs_clunk(void *opaque
)
1621 V9fsPDU
*pdu
= opaque
;
1622 V9fsState
*s
= pdu
->s
;
1624 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1628 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1630 fidp
= clunk_fid(s
, fid
);
1636 * Bump the ref so that put_fid will
1640 err
= put_fid(pdu
, fidp
);
1645 pdu_complete(pdu
, err
);
1649 * Create a QEMUIOVector for a sub-region of PDU iovecs
1651 * @qiov: uninitialized QEMUIOVector
1652 * @skip: number of bytes to skip from beginning of PDU
1653 * @size: number of bytes to include
1654 * @is_write: true - write, false - read
1656 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1657 * with qemu_iovec_destroy().
1659 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1660 size_t skip
, size_t size
,
1668 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1670 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1673 qemu_iovec_init_external(&elem
, iov
, niov
);
1674 qemu_iovec_init(qiov
, niov
);
1675 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1678 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1679 uint64_t off
, uint32_t max_count
)
1683 uint64_t read_count
;
1684 QEMUIOVector qiov_full
;
1686 if (fidp
->fs
.xattr
.len
< off
) {
1689 read_count
= fidp
->fs
.xattr
.len
- off
;
1691 if (read_count
> max_count
) {
1692 read_count
= max_count
;
1694 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1700 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
1701 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
1702 ((char *)fidp
->fs
.xattr
.value
) + off
,
1704 qemu_iovec_destroy(&qiov_full
);
1712 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1721 off_t saved_dir_pos
;
1722 struct dirent
*dent
;
1724 /* save the directory position */
1725 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1726 if (saved_dir_pos
< 0) {
1727 return saved_dir_pos
;
1731 v9fs_path_init(&path
);
1733 v9fs_readdir_lock(&fidp
->fs
.dir
);
1735 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1739 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1743 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1747 err
= stat_to_v9stat(pdu
, &path
, dent
->d_name
, &stbuf
, &v9stat
);
1751 if ((count
+ v9stat
.size
+ 2) > max_count
) {
1752 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1754 /* Ran out of buffer. Set dir back to old position and return */
1755 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1756 v9fs_stat_free(&v9stat
);
1757 v9fs_path_free(&path
);
1761 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1762 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1764 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1767 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1768 v9fs_stat_free(&v9stat
);
1769 v9fs_path_free(&path
);
1773 v9fs_stat_free(&v9stat
);
1774 v9fs_path_free(&path
);
1775 saved_dir_pos
= dent
->d_off
;
1778 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1780 v9fs_path_free(&path
);
1787 static void coroutine_fn
v9fs_read(void *opaque
)
1796 V9fsPDU
*pdu
= opaque
;
1797 V9fsState
*s
= pdu
->s
;
1799 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1803 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1805 fidp
= get_fid(pdu
, fid
);
1810 if (fidp
->fid_type
== P9_FID_DIR
) {
1813 v9fs_co_rewinddir(pdu
, fidp
);
1815 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1820 err
= pdu_marshal(pdu
, offset
, "d", count
);
1824 err
+= offset
+ count
;
1825 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1826 QEMUIOVector qiov_full
;
1830 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1831 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1833 qemu_iovec_reset(&qiov
);
1834 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1836 print_sg(qiov
.iov
, qiov
.niov
);
1838 /* Loop in case of EINTR */
1840 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1845 } while (len
== -EINTR
&& !pdu
->cancelled
);
1847 /* IO error return the error */
1849 goto out_free_iovec
;
1851 } while (count
< max_count
&& len
> 0);
1852 err
= pdu_marshal(pdu
, offset
, "d", count
);
1854 goto out_free_iovec
;
1856 err
+= offset
+ count
;
1858 qemu_iovec_destroy(&qiov
);
1859 qemu_iovec_destroy(&qiov_full
);
1860 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1861 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1865 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1869 pdu_complete(pdu
, err
);
1872 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1875 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1876 * size of type (1) + size of name.size (2) + strlen(name.data)
1878 return 24 + v9fs_string_size(name
);
1881 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1889 off_t saved_dir_pos
;
1890 struct dirent
*dent
;
1892 /* save the directory position */
1893 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1894 if (saved_dir_pos
< 0) {
1895 return saved_dir_pos
;
1899 v9fs_readdir_lock(&fidp
->fs
.dir
);
1901 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1905 v9fs_string_init(&name
);
1906 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1907 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1908 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1910 /* Ran out of buffer. Set dir back to old position and return */
1911 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1912 v9fs_string_free(&name
);
1916 * Fill up just the path field of qid because the client uses
1917 * only that. To fill the entire qid structure we will have
1918 * to stat each dirent found, which is expensive
1920 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1921 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1922 /* Fill the other fields with dummy values */
1926 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1927 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1929 dent
->d_type
, &name
);
1931 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1934 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1935 v9fs_string_free(&name
);
1939 v9fs_string_free(&name
);
1940 saved_dir_pos
= dent
->d_off
;
1943 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1951 static void coroutine_fn
v9fs_readdir(void *opaque
)
1957 uint64_t initial_offset
;
1960 V9fsPDU
*pdu
= opaque
;
1962 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1963 &initial_offset
, &max_count
);
1967 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1969 fidp
= get_fid(pdu
, fid
);
1974 if (!fidp
->fs
.dir
.stream
) {
1978 if (initial_offset
== 0) {
1979 v9fs_co_rewinddir(pdu
, fidp
);
1981 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1983 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1988 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1992 retval
+= count
+ offset
;
1993 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1997 pdu_complete(pdu
, retval
);
2000 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2001 uint64_t off
, uint32_t count
,
2002 struct iovec
*sg
, int cnt
)
2006 uint64_t write_count
;
2010 if (fidp
->fs
.xattr
.len
< off
) {
2014 write_count
= fidp
->fs
.xattr
.len
- off
;
2015 if (write_count
> count
) {
2016 write_count
= count
;
2018 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2023 fidp
->fs
.xattr
.copied_len
+= write_count
;
2025 * Now copy the content from sg list
2027 for (i
= 0; i
< cnt
; i
++) {
2028 if (write_count
> sg
[i
].iov_len
) {
2029 to_copy
= sg
[i
].iov_len
;
2031 to_copy
= write_count
;
2033 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2034 /* updating vs->off since we are not using below */
2036 write_count
-= to_copy
;
2042 static void coroutine_fn
v9fs_write(void *opaque
)
2052 V9fsPDU
*pdu
= opaque
;
2053 V9fsState
*s
= pdu
->s
;
2054 QEMUIOVector qiov_full
;
2057 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2059 pdu_complete(pdu
, err
);
2063 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2064 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2066 fidp
= get_fid(pdu
, fid
);
2071 if (fidp
->fid_type
== P9_FID_FILE
) {
2072 if (fidp
->fs
.fd
== -1) {
2076 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2078 * setxattr operation
2080 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2081 qiov_full
.iov
, qiov_full
.niov
);
2087 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2089 qemu_iovec_reset(&qiov
);
2090 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2092 print_sg(qiov
.iov
, qiov
.niov
);
2094 /* Loop in case of EINTR */
2096 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2101 } while (len
== -EINTR
&& !pdu
->cancelled
);
2103 /* IO error return the error */
2107 } while (total
< count
&& len
> 0);
2110 err
= pdu_marshal(pdu
, offset
, "d", total
);
2115 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2117 qemu_iovec_destroy(&qiov
);
2121 qemu_iovec_destroy(&qiov_full
);
2122 pdu_complete(pdu
, err
);
2125 static void coroutine_fn
v9fs_create(void *opaque
)
2137 V9fsString extension
;
2139 V9fsPDU
*pdu
= opaque
;
2141 v9fs_path_init(&path
);
2142 v9fs_string_init(&name
);
2143 v9fs_string_init(&extension
);
2144 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2145 &perm
, &mode
, &extension
);
2149 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2151 if (name_is_illegal(name
.data
)) {
2156 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2161 fidp
= get_fid(pdu
, fid
);
2166 if (fidp
->fid_type
!= P9_FID_NONE
) {
2170 if (perm
& P9_STAT_MODE_DIR
) {
2171 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2172 fidp
->uid
, -1, &stbuf
);
2176 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2180 v9fs_path_copy(&fidp
->path
, &path
);
2181 err
= v9fs_co_opendir(pdu
, fidp
);
2185 fidp
->fid_type
= P9_FID_DIR
;
2186 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2187 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2188 extension
.data
, -1 , &stbuf
);
2192 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2196 v9fs_path_copy(&fidp
->path
, &path
);
2197 } else if (perm
& P9_STAT_MODE_LINK
) {
2198 int32_t ofid
= atoi(extension
.data
);
2199 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2200 if (ofidp
== NULL
) {
2204 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2205 put_fid(pdu
, ofidp
);
2209 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2211 fidp
->fid_type
= P9_FID_NONE
;
2214 v9fs_path_copy(&fidp
->path
, &path
);
2215 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2217 fidp
->fid_type
= P9_FID_NONE
;
2220 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2222 uint32_t major
, minor
;
2225 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2242 nmode
|= perm
& 0777;
2243 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2244 makedev(major
, minor
), nmode
, &stbuf
);
2248 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2252 v9fs_path_copy(&fidp
->path
, &path
);
2253 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2254 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2255 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2259 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2263 v9fs_path_copy(&fidp
->path
, &path
);
2264 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2265 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2266 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2270 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2274 v9fs_path_copy(&fidp
->path
, &path
);
2276 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2277 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2281 fidp
->fid_type
= P9_FID_FILE
;
2282 fidp
->open_flags
= omode_to_uflags(mode
);
2283 if (fidp
->open_flags
& O_EXCL
) {
2285 * We let the host file system do O_EXCL check
2286 * We should not reclaim such fd
2288 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2291 iounit
= get_iounit(pdu
, &fidp
->path
);
2292 stat_to_qid(&stbuf
, &qid
);
2293 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2298 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2299 qid
.type
, qid
.version
, qid
.path
, iounit
);
2303 pdu_complete(pdu
, err
);
2304 v9fs_string_free(&name
);
2305 v9fs_string_free(&extension
);
2306 v9fs_path_free(&path
);
2309 static void coroutine_fn
v9fs_symlink(void *opaque
)
2311 V9fsPDU
*pdu
= opaque
;
2314 V9fsFidState
*dfidp
;
2322 v9fs_string_init(&name
);
2323 v9fs_string_init(&symname
);
2324 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2328 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2330 if (name_is_illegal(name
.data
)) {
2335 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2340 dfidp
= get_fid(pdu
, dfid
);
2341 if (dfidp
== NULL
) {
2345 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2349 stat_to_qid(&stbuf
, &qid
);
2350 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2355 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2356 qid
.type
, qid
.version
, qid
.path
);
2358 put_fid(pdu
, dfidp
);
2360 pdu_complete(pdu
, err
);
2361 v9fs_string_free(&name
);
2362 v9fs_string_free(&symname
);
2365 static void coroutine_fn
v9fs_flush(void *opaque
)
2370 V9fsPDU
*cancel_pdu
= NULL
;
2371 V9fsPDU
*pdu
= opaque
;
2372 V9fsState
*s
= pdu
->s
;
2374 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2376 pdu_complete(pdu
, err
);
2379 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2381 if (pdu
->tag
== tag
) {
2382 warn_report("the guest sent a self-referencing 9P flush request");
2384 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2385 if (cancel_pdu
->tag
== tag
) {
2391 cancel_pdu
->cancelled
= 1;
2393 * Wait for pdu to complete.
2395 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2396 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2397 cancel_pdu
->cancelled
= 0;
2398 pdu_free(cancel_pdu
);
2401 pdu_complete(pdu
, 7);
2404 static void coroutine_fn
v9fs_link(void *opaque
)
2406 V9fsPDU
*pdu
= opaque
;
2407 int32_t dfid
, oldfid
;
2408 V9fsFidState
*dfidp
, *oldfidp
;
2413 v9fs_string_init(&name
);
2414 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2418 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2420 if (name_is_illegal(name
.data
)) {
2425 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2430 dfidp
= get_fid(pdu
, dfid
);
2431 if (dfidp
== NULL
) {
2436 oldfidp
= get_fid(pdu
, oldfid
);
2437 if (oldfidp
== NULL
) {
2441 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2445 put_fid(pdu
, oldfidp
);
2447 put_fid(pdu
, dfidp
);
2449 v9fs_string_free(&name
);
2450 pdu_complete(pdu
, err
);
2453 /* Only works with path name based fid */
2454 static void coroutine_fn
v9fs_remove(void *opaque
)
2460 V9fsPDU
*pdu
= opaque
;
2462 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2466 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2468 fidp
= get_fid(pdu
, fid
);
2473 /* if fs driver is not path based, return EOPNOTSUPP */
2474 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2479 * IF the file is unlinked, we cannot reopen
2480 * the file later. So don't reclaim fd
2482 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2486 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2491 /* For TREMOVE we need to clunk the fid even on failed remove */
2492 clunk_fid(pdu
->s
, fidp
->fid
);
2495 pdu_complete(pdu
, err
);
2498 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2502 int32_t dfid
, flags
;
2505 V9fsFidState
*dfidp
;
2506 V9fsPDU
*pdu
= opaque
;
2508 v9fs_string_init(&name
);
2509 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2514 if (name_is_illegal(name
.data
)) {
2519 if (!strcmp(".", name
.data
)) {
2524 if (!strcmp("..", name
.data
)) {
2529 dfidp
= get_fid(pdu
, dfid
);
2530 if (dfidp
== NULL
) {
2535 * IF the file is unlinked, we cannot reopen
2536 * the file later. So don't reclaim fd
2538 v9fs_path_init(&path
);
2539 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2543 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2547 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2552 put_fid(pdu
, dfidp
);
2553 v9fs_path_free(&path
);
2555 pdu_complete(pdu
, err
);
2556 v9fs_string_free(&name
);
2560 /* Only works with path name based fid */
2561 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2567 V9fsFidState
*tfidp
;
2568 V9fsState
*s
= pdu
->s
;
2569 V9fsFidState
*dirfidp
= NULL
;
2571 v9fs_path_init(&new_path
);
2572 if (newdirfid
!= -1) {
2573 dirfidp
= get_fid(pdu
, newdirfid
);
2574 if (dirfidp
== NULL
) {
2578 if (fidp
->fid_type
!= P9_FID_NONE
) {
2582 err
= v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2587 char *dir_name
= g_path_get_dirname(fidp
->path
.data
);
2590 v9fs_path_init(&dir_path
);
2591 v9fs_path_sprintf(&dir_path
, "%s", dir_name
);
2594 err
= v9fs_co_name_to_path(pdu
, &dir_path
, name
->data
, &new_path
);
2595 v9fs_path_free(&dir_path
);
2600 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2605 * Fixup fid's pointing to the old name to
2606 * start pointing to the new name
2608 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2609 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2610 /* replace the name */
2611 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2616 put_fid(pdu
, dirfidp
);
2618 v9fs_path_free(&new_path
);
2623 /* Only works with path name based fid */
2624 static void coroutine_fn
v9fs_rename(void *opaque
)
2632 V9fsPDU
*pdu
= opaque
;
2633 V9fsState
*s
= pdu
->s
;
2635 v9fs_string_init(&name
);
2636 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2641 if (name_is_illegal(name
.data
)) {
2646 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2651 fidp
= get_fid(pdu
, fid
);
2656 if (fidp
->fid_type
!= P9_FID_NONE
) {
2660 /* if fs driver is not path based, return EOPNOTSUPP */
2661 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2665 v9fs_path_write_lock(s
);
2666 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2667 v9fs_path_unlock(s
);
2674 pdu_complete(pdu
, err
);
2675 v9fs_string_free(&name
);
2678 static int coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2679 V9fsString
*old_name
,
2681 V9fsString
*new_name
)
2683 V9fsFidState
*tfidp
;
2684 V9fsPath oldpath
, newpath
;
2685 V9fsState
*s
= pdu
->s
;
2688 v9fs_path_init(&oldpath
);
2689 v9fs_path_init(&newpath
);
2690 err
= v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2694 err
= v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2700 * Fixup fid's pointing to the old name to
2701 * start pointing to the new name
2703 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2704 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2705 /* replace the name */
2706 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2710 v9fs_path_free(&oldpath
);
2711 v9fs_path_free(&newpath
);
2715 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2716 V9fsString
*old_name
,
2718 V9fsString
*new_name
)
2721 V9fsState
*s
= pdu
->s
;
2722 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2724 olddirfidp
= get_fid(pdu
, olddirfid
);
2725 if (olddirfidp
== NULL
) {
2729 if (newdirfid
!= -1) {
2730 newdirfidp
= get_fid(pdu
, newdirfid
);
2731 if (newdirfidp
== NULL
) {
2736 newdirfidp
= get_fid(pdu
, olddirfid
);
2739 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2740 &newdirfidp
->path
, new_name
);
2744 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2745 /* Only for path based fid we need to do the below fixup */
2746 err
= v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2747 &newdirfidp
->path
, new_name
);
2751 put_fid(pdu
, olddirfidp
);
2754 put_fid(pdu
, newdirfidp
);
2759 static void coroutine_fn
v9fs_renameat(void *opaque
)
2763 V9fsPDU
*pdu
= opaque
;
2764 V9fsState
*s
= pdu
->s
;
2765 int32_t olddirfid
, newdirfid
;
2766 V9fsString old_name
, new_name
;
2768 v9fs_string_init(&old_name
);
2769 v9fs_string_init(&new_name
);
2770 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2771 &old_name
, &newdirfid
, &new_name
);
2776 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2781 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
2782 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
2787 v9fs_path_write_lock(s
);
2788 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2789 &old_name
, newdirfid
, &new_name
);
2790 v9fs_path_unlock(s
);
2796 pdu_complete(pdu
, err
);
2797 v9fs_string_free(&old_name
);
2798 v9fs_string_free(&new_name
);
2801 static void coroutine_fn
v9fs_wstat(void *opaque
)
2810 V9fsPDU
*pdu
= opaque
;
2812 v9fs_stat_init(&v9stat
);
2813 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2817 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2818 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2820 fidp
= get_fid(pdu
, fid
);
2825 /* do we need to sync the file? */
2826 if (donttouch_stat(&v9stat
)) {
2827 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2830 if (v9stat
.mode
!= -1) {
2832 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2836 v9_mode
= stat_to_v9mode(&stbuf
);
2837 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2838 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2839 /* Attempting to change the type */
2843 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2844 v9mode_to_mode(v9stat
.mode
,
2845 &v9stat
.extension
));
2850 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2851 struct timespec times
[2];
2852 if (v9stat
.atime
!= -1) {
2853 times
[0].tv_sec
= v9stat
.atime
;
2854 times
[0].tv_nsec
= 0;
2856 times
[0].tv_nsec
= UTIME_OMIT
;
2858 if (v9stat
.mtime
!= -1) {
2859 times
[1].tv_sec
= v9stat
.mtime
;
2860 times
[1].tv_nsec
= 0;
2862 times
[1].tv_nsec
= UTIME_OMIT
;
2864 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2869 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2870 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2875 if (v9stat
.name
.size
!= 0) {
2876 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2881 if (v9stat
.length
!= -1) {
2882 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2891 v9fs_stat_free(&v9stat
);
2892 pdu_complete(pdu
, err
);
2895 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2907 int32_t bsize_factor
;
2910 * compute bsize factor based on host file system block size
2913 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2914 if (!bsize_factor
) {
2917 f_type
= stbuf
->f_type
;
2918 f_bsize
= stbuf
->f_bsize
;
2919 f_bsize
*= bsize_factor
;
2921 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2922 * adjust(divide) the number of blocks, free blocks and available
2923 * blocks by bsize factor
2925 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2926 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2927 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2928 f_files
= stbuf
->f_files
;
2929 f_ffree
= stbuf
->f_ffree
;
2930 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2931 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2932 f_namelen
= stbuf
->f_namelen
;
2934 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2935 f_type
, f_bsize
, f_blocks
, f_bfree
,
2936 f_bavail
, f_files
, f_ffree
,
2937 fsid_val
, f_namelen
);
2940 static void coroutine_fn
v9fs_statfs(void *opaque
)
2946 struct statfs stbuf
;
2947 V9fsPDU
*pdu
= opaque
;
2948 V9fsState
*s
= pdu
->s
;
2950 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2954 fidp
= get_fid(pdu
, fid
);
2959 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2963 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2971 pdu_complete(pdu
, retval
);
2974 static void coroutine_fn
v9fs_mknod(void *opaque
)
2987 V9fsPDU
*pdu
= opaque
;
2989 v9fs_string_init(&name
);
2990 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2991 &major
, &minor
, &gid
);
2995 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2997 if (name_is_illegal(name
.data
)) {
3002 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3007 fidp
= get_fid(pdu
, fid
);
3012 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
3013 makedev(major
, minor
), mode
, &stbuf
);
3017 stat_to_qid(&stbuf
, &qid
);
3018 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3023 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3024 qid
.type
, qid
.version
, qid
.path
);
3028 pdu_complete(pdu
, err
);
3029 v9fs_string_free(&name
);
3033 * Implement posix byte range locking code
3034 * Server side handling of locking code is very simple, because 9p server in
3035 * QEMU can handle only one client. And most of the lock handling
3036 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3037 * do any thing in * qemu 9p server side lock code path.
3038 * So when a TLOCK request comes, always return success
3040 static void coroutine_fn
v9fs_lock(void *opaque
)
3046 int32_t fid
, err
= 0;
3047 V9fsPDU
*pdu
= opaque
;
3049 v9fs_string_init(&flock
.client_id
);
3050 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3051 &flock
.flags
, &flock
.start
, &flock
.length
,
3052 &flock
.proc_id
, &flock
.client_id
);
3056 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3057 flock
.type
, flock
.start
, flock
.length
);
3060 /* We support only block flag now (that too ignored currently) */
3061 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3065 fidp
= get_fid(pdu
, fid
);
3070 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3074 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3079 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3083 pdu_complete(pdu
, err
);
3084 v9fs_string_free(&flock
.client_id
);
3088 * When a TGETLOCK request comes, always return success because all lock
3089 * handling is done by client's VFS layer.
3091 static void coroutine_fn
v9fs_getlock(void *opaque
)
3097 int32_t fid
, err
= 0;
3098 V9fsPDU
*pdu
= opaque
;
3100 v9fs_string_init(&glock
.client_id
);
3101 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3102 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3107 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3108 glock
.type
, glock
.start
, glock
.length
);
3110 fidp
= get_fid(pdu
, fid
);
3115 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3119 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3120 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3121 glock
.start
, glock
.length
, glock
.proc_id
,
3127 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3128 glock
.length
, glock
.proc_id
);
3132 pdu_complete(pdu
, err
);
3133 v9fs_string_free(&glock
.client_id
);
3136 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3138 V9fsPDU
*pdu
= opaque
;
3149 v9fs_string_init(&name
);
3150 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3154 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3156 if (name_is_illegal(name
.data
)) {
3161 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3166 fidp
= get_fid(pdu
, fid
);
3171 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3175 stat_to_qid(&stbuf
, &qid
);
3176 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3181 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3182 qid
.type
, qid
.version
, qid
.path
, err
);
3186 pdu_complete(pdu
, err
);
3187 v9fs_string_free(&name
);
3190 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3196 int32_t fid
, newfid
;
3197 V9fsFidState
*file_fidp
;
3198 V9fsFidState
*xattr_fidp
= NULL
;
3199 V9fsPDU
*pdu
= opaque
;
3200 V9fsState
*s
= pdu
->s
;
3202 v9fs_string_init(&name
);
3203 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3207 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3209 file_fidp
= get_fid(pdu
, fid
);
3210 if (file_fidp
== NULL
) {
3214 xattr_fidp
= alloc_fid(s
, newfid
);
3215 if (xattr_fidp
== NULL
) {
3219 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3220 if (!v9fs_string_size(&name
)) {
3222 * listxattr request. Get the size first
3224 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3227 clunk_fid(s
, xattr_fidp
->fid
);
3231 * Read the xattr value
3233 xattr_fidp
->fs
.xattr
.len
= size
;
3234 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3235 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3237 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3238 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3239 xattr_fidp
->fs
.xattr
.value
,
3240 xattr_fidp
->fs
.xattr
.len
);
3242 clunk_fid(s
, xattr_fidp
->fid
);
3246 err
= pdu_marshal(pdu
, offset
, "q", size
);
3253 * specific xattr fid. We check for xattr
3254 * presence also collect the xattr size
3256 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3260 clunk_fid(s
, xattr_fidp
->fid
);
3264 * Read the xattr value
3266 xattr_fidp
->fs
.xattr
.len
= size
;
3267 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3268 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3270 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3271 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3272 &name
, xattr_fidp
->fs
.xattr
.value
,
3273 xattr_fidp
->fs
.xattr
.len
);
3275 clunk_fid(s
, xattr_fidp
->fid
);
3279 err
= pdu_marshal(pdu
, offset
, "q", size
);
3285 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3287 put_fid(pdu
, file_fidp
);
3289 put_fid(pdu
, xattr_fidp
);
3292 pdu_complete(pdu
, err
);
3293 v9fs_string_free(&name
);
3296 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3304 V9fsFidState
*file_fidp
;
3305 V9fsFidState
*xattr_fidp
;
3306 V9fsPDU
*pdu
= opaque
;
3308 v9fs_string_init(&name
);
3309 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3313 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3315 if (size
> XATTR_SIZE_MAX
) {
3320 file_fidp
= get_fid(pdu
, fid
);
3321 if (file_fidp
== NULL
) {
3325 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3330 /* Make the file fid point to xattr */
3331 xattr_fidp
= file_fidp
;
3332 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3333 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3334 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3335 xattr_fidp
->fs
.xattr
.len
= size
;
3336 xattr_fidp
->fs
.xattr
.flags
= flags
;
3337 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3338 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3339 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3342 put_fid(pdu
, file_fidp
);
3344 pdu_complete(pdu
, err
);
3345 v9fs_string_free(&name
);
3348 static void coroutine_fn
v9fs_readlink(void *opaque
)
3350 V9fsPDU
*pdu
= opaque
;
3357 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3361 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3362 fidp
= get_fid(pdu
, fid
);
3368 v9fs_string_init(&target
);
3369 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3373 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3375 v9fs_string_free(&target
);
3379 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3380 v9fs_string_free(&target
);
3384 pdu_complete(pdu
, err
);
3387 static CoroutineEntry
*pdu_co_handlers
[] = {
3388 [P9_TREADDIR
] = v9fs_readdir
,
3389 [P9_TSTATFS
] = v9fs_statfs
,
3390 [P9_TGETATTR
] = v9fs_getattr
,
3391 [P9_TSETATTR
] = v9fs_setattr
,
3392 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3393 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3394 [P9_TMKNOD
] = v9fs_mknod
,
3395 [P9_TRENAME
] = v9fs_rename
,
3396 [P9_TLOCK
] = v9fs_lock
,
3397 [P9_TGETLOCK
] = v9fs_getlock
,
3398 [P9_TRENAMEAT
] = v9fs_renameat
,
3399 [P9_TREADLINK
] = v9fs_readlink
,
3400 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3401 [P9_TMKDIR
] = v9fs_mkdir
,
3402 [P9_TVERSION
] = v9fs_version
,
3403 [P9_TLOPEN
] = v9fs_open
,
3404 [P9_TATTACH
] = v9fs_attach
,
3405 [P9_TSTAT
] = v9fs_stat
,
3406 [P9_TWALK
] = v9fs_walk
,
3407 [P9_TCLUNK
] = v9fs_clunk
,
3408 [P9_TFSYNC
] = v9fs_fsync
,
3409 [P9_TOPEN
] = v9fs_open
,
3410 [P9_TREAD
] = v9fs_read
,
3412 [P9_TAUTH
] = v9fs_auth
,
3414 [P9_TFLUSH
] = v9fs_flush
,
3415 [P9_TLINK
] = v9fs_link
,
3416 [P9_TSYMLINK
] = v9fs_symlink
,
3417 [P9_TCREATE
] = v9fs_create
,
3418 [P9_TLCREATE
] = v9fs_lcreate
,
3419 [P9_TWRITE
] = v9fs_write
,
3420 [P9_TWSTAT
] = v9fs_wstat
,
3421 [P9_TREMOVE
] = v9fs_remove
,
3424 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3426 V9fsPDU
*pdu
= opaque
;
3427 pdu_complete(pdu
, -EOPNOTSUPP
);
3430 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3432 V9fsPDU
*pdu
= opaque
;
3433 pdu_complete(pdu
, -EROFS
);
3436 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3463 void pdu_submit(V9fsPDU
*pdu
, P9MsgHeader
*hdr
)
3466 CoroutineEntry
*handler
;
3467 V9fsState
*s
= pdu
->s
;
3469 pdu
->size
= le32_to_cpu(hdr
->size_le
);
3471 pdu
->tag
= le16_to_cpu(hdr
->tag_le
);
3473 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3474 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3475 handler
= v9fs_op_not_supp
;
3476 } else if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3477 handler
= v9fs_fs_ro
;
3479 handler
= pdu_co_handlers
[pdu
->id
];
3482 qemu_co_queue_init(&pdu
->complete
);
3483 co
= qemu_coroutine_create(handler
, pdu
);
3484 qemu_coroutine_enter(co
);
3487 /* Returns 0 on success, 1 on failure. */
3488 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3496 /* initialize pdu allocator */
3497 QLIST_INIT(&s
->free_list
);
3498 QLIST_INIT(&s
->active_list
);
3499 for (i
= 0; i
< MAX_REQ
; i
++) {
3500 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
3505 v9fs_path_init(&path
);
3507 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3510 /* We don't have a fsdev identified by fsdev_id */
3511 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3513 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3517 if (!s
->fsconf
.tag
) {
3518 /* we haven't specified a mount_tag */
3519 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3520 s
->fsconf
.fsdev_id
);
3524 s
->ctx
.export_flags
= fse
->export_flags
;
3525 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3526 s
->ctx
.exops
.get_st_gen
= NULL
;
3527 len
= strlen(s
->fsconf
.tag
);
3528 if (len
> MAX_TAG_LEN
- 1) {
3529 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3530 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3534 s
->tag
= g_strdup(s
->fsconf
.tag
);
3539 s
->ctx
.fmode
= fse
->fmode
;
3540 s
->ctx
.dmode
= fse
->dmode
;
3543 qemu_co_rwlock_init(&s
->rename_lock
);
3545 if (s
->ops
->init(&s
->ctx
, errp
) < 0) {
3546 error_prepend(errp
, "cannot initialize fsdev '%s': ",
3547 s
->fsconf
.fsdev_id
);
3552 * Check details of export path, We need to use fs driver
3553 * call back to do that. Since we are in the init path, we don't
3554 * use co-routines here.
3556 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3558 "error in converting name to path %s", strerror(errno
));
3561 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3562 error_setg(errp
, "share path %s does not exist", fse
->path
);
3564 } else if (!S_ISDIR(stat
.st_mode
)) {
3565 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3569 s
->ctx
.fst
= &fse
->fst
;
3570 fsdev_throttle_init(s
->ctx
.fst
);
3572 v9fs_path_free(&path
);
3577 if (s
->ops
&& s
->ops
->cleanup
&& s
->ctx
.private) {
3578 s
->ops
->cleanup(&s
->ctx
);
3581 g_free(s
->ctx
.fs_root
);
3582 v9fs_path_free(&path
);
3587 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3589 if (s
->ops
->cleanup
) {
3590 s
->ops
->cleanup(&s
->ctx
);
3592 fsdev_throttle_cleanup(s
->ctx
.fst
);
3594 g_free(s
->ctx
.fs_root
);
3597 typedef struct VirtfsCoResetData
{
3600 } VirtfsCoResetData
;
3602 static void coroutine_fn
virtfs_co_reset(void *opaque
)
3604 VirtfsCoResetData
*data
= opaque
;
3606 virtfs_reset(&data
->pdu
);
3610 void v9fs_reset(V9fsState
*s
)
3612 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
3615 while (!QLIST_EMPTY(&s
->active_list
)) {
3616 aio_poll(qemu_get_aio_context(), true);
3619 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
3620 qemu_coroutine_enter(co
);
3622 while (!data
.done
) {
3623 aio_poll(qemu_get_aio_context(), true);
3627 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3630 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3631 error_report("Failed to get the resource limit");
3634 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3635 open_fd_rc
= rlim
.rlim_cur
/2;