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
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
56 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 void pdu_push_and_notify(V9fsPDU
*pdu
)
70 pdu
->s
->transport
->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
.xattrwalk_fid
) {
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
;
541 while (s
->fid_list
) {
547 s
->fid_list
= fidp
->next
;
554 #define P9_QID_TYPE_DIR 0x80
555 #define P9_QID_TYPE_SYMLINK 0x02
557 #define P9_STAT_MODE_DIR 0x80000000
558 #define P9_STAT_MODE_APPEND 0x40000000
559 #define P9_STAT_MODE_EXCL 0x20000000
560 #define P9_STAT_MODE_MOUNT 0x10000000
561 #define P9_STAT_MODE_AUTH 0x08000000
562 #define P9_STAT_MODE_TMP 0x04000000
563 #define P9_STAT_MODE_SYMLINK 0x02000000
564 #define P9_STAT_MODE_LINK 0x01000000
565 #define P9_STAT_MODE_DEVICE 0x00800000
566 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
567 #define P9_STAT_MODE_SOCKET 0x00100000
568 #define P9_STAT_MODE_SETUID 0x00080000
569 #define P9_STAT_MODE_SETGID 0x00040000
570 #define P9_STAT_MODE_SETVTX 0x00010000
572 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
573 P9_STAT_MODE_SYMLINK | \
574 P9_STAT_MODE_LINK | \
575 P9_STAT_MODE_DEVICE | \
576 P9_STAT_MODE_NAMED_PIPE | \
579 /* This is the algorithm from ufs in spfs */
580 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
584 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
585 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
586 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
587 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
589 if (S_ISDIR(stbuf
->st_mode
)) {
590 qidp
->type
|= P9_QID_TYPE_DIR
;
592 if (S_ISLNK(stbuf
->st_mode
)) {
593 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
597 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
603 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
607 stat_to_qid(&stbuf
, qidp
);
611 V9fsPDU
*pdu_alloc(V9fsState
*s
)
615 if (!QLIST_EMPTY(&s
->free_list
)) {
616 pdu
= QLIST_FIRST(&s
->free_list
);
617 QLIST_REMOVE(pdu
, next
);
618 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
623 void pdu_free(V9fsPDU
*pdu
)
625 V9fsState
*s
= pdu
->s
;
627 g_assert(!pdu
->cancelled
);
628 QLIST_REMOVE(pdu
, next
);
629 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
633 * We don't do error checking for pdu_marshal/unmarshal here
634 * because we always expect to have enough space to encode
637 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
639 int8_t id
= pdu
->id
+ 1; /* Response */
640 V9fsState
*s
= pdu
->s
;
646 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
649 str
.data
= strerror(err
);
650 str
.size
= strlen(str
.data
);
652 len
+= pdu_marshal(pdu
, len
, "s", &str
);
656 len
+= pdu_marshal(pdu
, len
, "d", err
);
658 if (s
->proto_version
== V9FS_PROTO_2000L
) {
661 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
664 /* fill out the header */
665 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
667 /* keep these in sync */
671 pdu_push_and_notify(pdu
);
673 /* Now wakeup anybody waiting in flush for this request */
674 if (!qemu_co_queue_next(&pdu
->complete
)) {
679 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
684 if (mode
& P9_STAT_MODE_DIR
) {
688 if (mode
& P9_STAT_MODE_SYMLINK
) {
691 if (mode
& P9_STAT_MODE_SOCKET
) {
694 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
697 if (mode
& P9_STAT_MODE_DEVICE
) {
698 if (extension
->size
&& extension
->data
[0] == 'c') {
709 if (mode
& P9_STAT_MODE_SETUID
) {
712 if (mode
& P9_STAT_MODE_SETGID
) {
715 if (mode
& P9_STAT_MODE_SETVTX
) {
722 static int donttouch_stat(V9fsStat
*stat
)
724 if (stat
->type
== -1 &&
726 stat
->qid
.type
== -1 &&
727 stat
->qid
.version
== -1 &&
728 stat
->qid
.path
== -1 &&
732 stat
->length
== -1 &&
739 stat
->n_muid
== -1) {
746 static void v9fs_stat_init(V9fsStat
*stat
)
748 v9fs_string_init(&stat
->name
);
749 v9fs_string_init(&stat
->uid
);
750 v9fs_string_init(&stat
->gid
);
751 v9fs_string_init(&stat
->muid
);
752 v9fs_string_init(&stat
->extension
);
755 static void v9fs_stat_free(V9fsStat
*stat
)
757 v9fs_string_free(&stat
->name
);
758 v9fs_string_free(&stat
->uid
);
759 v9fs_string_free(&stat
->gid
);
760 v9fs_string_free(&stat
->muid
);
761 v9fs_string_free(&stat
->extension
);
764 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
768 mode
= stbuf
->st_mode
& 0777;
769 if (S_ISDIR(stbuf
->st_mode
)) {
770 mode
|= P9_STAT_MODE_DIR
;
773 if (S_ISLNK(stbuf
->st_mode
)) {
774 mode
|= P9_STAT_MODE_SYMLINK
;
777 if (S_ISSOCK(stbuf
->st_mode
)) {
778 mode
|= P9_STAT_MODE_SOCKET
;
781 if (S_ISFIFO(stbuf
->st_mode
)) {
782 mode
|= P9_STAT_MODE_NAMED_PIPE
;
785 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
786 mode
|= P9_STAT_MODE_DEVICE
;
789 if (stbuf
->st_mode
& S_ISUID
) {
790 mode
|= P9_STAT_MODE_SETUID
;
793 if (stbuf
->st_mode
& S_ISGID
) {
794 mode
|= P9_STAT_MODE_SETGID
;
797 if (stbuf
->st_mode
& S_ISVTX
) {
798 mode
|= P9_STAT_MODE_SETVTX
;
804 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
805 const struct stat
*stbuf
,
811 memset(v9stat
, 0, sizeof(*v9stat
));
813 stat_to_qid(stbuf
, &v9stat
->qid
);
814 v9stat
->mode
= stat_to_v9mode(stbuf
);
815 v9stat
->atime
= stbuf
->st_atime
;
816 v9stat
->mtime
= stbuf
->st_mtime
;
817 v9stat
->length
= stbuf
->st_size
;
819 v9fs_string_free(&v9stat
->uid
);
820 v9fs_string_free(&v9stat
->gid
);
821 v9fs_string_free(&v9stat
->muid
);
823 v9stat
->n_uid
= stbuf
->st_uid
;
824 v9stat
->n_gid
= stbuf
->st_gid
;
827 v9fs_string_free(&v9stat
->extension
);
829 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
830 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
834 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
835 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
836 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
837 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
838 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
839 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
840 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
843 str
= strrchr(name
->data
, '/');
850 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
853 v9fs_string_size(&v9stat
->name
) +
854 v9fs_string_size(&v9stat
->uid
) +
855 v9fs_string_size(&v9stat
->gid
) +
856 v9fs_string_size(&v9stat
->muid
) +
857 v9fs_string_size(&v9stat
->extension
);
861 #define P9_STATS_MODE 0x00000001ULL
862 #define P9_STATS_NLINK 0x00000002ULL
863 #define P9_STATS_UID 0x00000004ULL
864 #define P9_STATS_GID 0x00000008ULL
865 #define P9_STATS_RDEV 0x00000010ULL
866 #define P9_STATS_ATIME 0x00000020ULL
867 #define P9_STATS_MTIME 0x00000040ULL
868 #define P9_STATS_CTIME 0x00000080ULL
869 #define P9_STATS_INO 0x00000100ULL
870 #define P9_STATS_SIZE 0x00000200ULL
871 #define P9_STATS_BLOCKS 0x00000400ULL
873 #define P9_STATS_BTIME 0x00000800ULL
874 #define P9_STATS_GEN 0x00001000ULL
875 #define P9_STATS_DATA_VERSION 0x00002000ULL
877 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
878 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
881 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
882 V9fsStatDotl
*v9lstat
)
884 memset(v9lstat
, 0, sizeof(*v9lstat
));
886 v9lstat
->st_mode
= stbuf
->st_mode
;
887 v9lstat
->st_nlink
= stbuf
->st_nlink
;
888 v9lstat
->st_uid
= stbuf
->st_uid
;
889 v9lstat
->st_gid
= stbuf
->st_gid
;
890 v9lstat
->st_rdev
= stbuf
->st_rdev
;
891 v9lstat
->st_size
= stbuf
->st_size
;
892 v9lstat
->st_blksize
= stbuf
->st_blksize
;
893 v9lstat
->st_blocks
= stbuf
->st_blocks
;
894 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
895 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
896 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
897 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
898 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
899 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
900 /* Currently we only support BASIC fields in stat */
901 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
903 stat_to_qid(stbuf
, &v9lstat
->qid
);
906 static void print_sg(struct iovec
*sg
, int cnt
)
910 printf("sg[%d]: {", cnt
);
911 for (i
= 0; i
< cnt
; i
++) {
915 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
920 /* Will call this only for path name based fid */
921 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
924 v9fs_path_init(&str
);
925 v9fs_path_copy(&str
, dst
);
926 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
927 v9fs_path_free(&str
);
930 static inline bool is_ro_export(FsContext
*ctx
)
932 return ctx
->export_flags
& V9FS_RDONLY
;
935 static void coroutine_fn
v9fs_version(void *opaque
)
938 V9fsPDU
*pdu
= opaque
;
939 V9fsState
*s
= pdu
->s
;
943 v9fs_string_init(&version
);
944 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
949 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
953 if (!strcmp(version
.data
, "9P2000.u")) {
954 s
->proto_version
= V9FS_PROTO_2000U
;
955 } else if (!strcmp(version
.data
, "9P2000.L")) {
956 s
->proto_version
= V9FS_PROTO_2000L
;
958 v9fs_string_sprintf(&version
, "unknown");
961 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
967 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
969 pdu_complete(pdu
, offset
);
970 v9fs_string_free(&version
);
973 static void coroutine_fn
v9fs_attach(void *opaque
)
975 V9fsPDU
*pdu
= opaque
;
976 V9fsState
*s
= pdu
->s
;
977 int32_t fid
, afid
, n_uname
;
978 V9fsString uname
, aname
;
983 Error
*local_err
= NULL
;
985 v9fs_string_init(&uname
);
986 v9fs_string_init(&aname
);
987 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
988 &afid
, &uname
, &aname
, &n_uname
);
992 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
994 fidp
= alloc_fid(s
, fid
);
1000 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1006 err
= fid_to_qid(pdu
, fidp
, &qid
);
1014 * disable migration if we haven't done already.
1015 * attach could get called multiple times for the same export.
1017 if (!s
->migration_blocker
) {
1018 error_setg(&s
->migration_blocker
,
1019 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1020 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1021 err
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1023 error_free(local_err
);
1024 error_free(s
->migration_blocker
);
1025 s
->migration_blocker
= NULL
;
1032 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1039 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1040 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1041 qid
.type
, qid
.version
, qid
.path
);
1045 pdu_complete(pdu
, err
);
1046 v9fs_string_free(&uname
);
1047 v9fs_string_free(&aname
);
1050 static void coroutine_fn
v9fs_stat(void *opaque
)
1058 V9fsPDU
*pdu
= opaque
;
1060 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1064 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1066 fidp
= get_fid(pdu
, fid
);
1071 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1075 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1079 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1081 v9fs_stat_free(&v9stat
);
1084 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1085 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1087 v9fs_stat_free(&v9stat
);
1091 pdu_complete(pdu
, err
);
1094 static void coroutine_fn
v9fs_getattr(void *opaque
)
1101 uint64_t request_mask
;
1102 V9fsStatDotl v9stat_dotl
;
1103 V9fsPDU
*pdu
= opaque
;
1104 V9fsState
*s
= pdu
->s
;
1106 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1110 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1112 fidp
= get_fid(pdu
, fid
);
1118 * Currently we only support BASIC fields in stat, so there is no
1119 * need to look at request_mask.
1121 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1125 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1127 /* fill st_gen if requested and supported by underlying fs */
1128 if (request_mask
& P9_STATS_GEN
) {
1129 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1132 /* we have valid st_gen: update result mask */
1133 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1136 /* request cancelled, e.g. by Tflush */
1139 /* failed to get st_gen: not fatal, ignore */
1143 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1148 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1149 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1150 v9stat_dotl
.st_gid
);
1154 pdu_complete(pdu
, retval
);
1157 /* Attribute flags */
1158 #define P9_ATTR_MODE (1 << 0)
1159 #define P9_ATTR_UID (1 << 1)
1160 #define P9_ATTR_GID (1 << 2)
1161 #define P9_ATTR_SIZE (1 << 3)
1162 #define P9_ATTR_ATIME (1 << 4)
1163 #define P9_ATTR_MTIME (1 << 5)
1164 #define P9_ATTR_CTIME (1 << 6)
1165 #define P9_ATTR_ATIME_SET (1 << 7)
1166 #define P9_ATTR_MTIME_SET (1 << 8)
1168 #define P9_ATTR_MASK 127
1170 static void coroutine_fn
v9fs_setattr(void *opaque
)
1177 V9fsPDU
*pdu
= opaque
;
1179 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1184 fidp
= get_fid(pdu
, fid
);
1189 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1190 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1195 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1196 struct timespec times
[2];
1197 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1198 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1199 times
[0].tv_sec
= v9iattr
.atime_sec
;
1200 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1202 times
[0].tv_nsec
= UTIME_NOW
;
1205 times
[0].tv_nsec
= UTIME_OMIT
;
1207 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1208 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1209 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1210 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1212 times
[1].tv_nsec
= UTIME_NOW
;
1215 times
[1].tv_nsec
= UTIME_OMIT
;
1217 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1223 * If the only valid entry in iattr is ctime we can call
1224 * chown(-1,-1) to update the ctime of the file
1226 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1227 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1228 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1229 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1232 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1235 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1241 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1242 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1251 pdu_complete(pdu
, err
);
1254 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1260 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1265 for (i
= 0; i
< nwnames
; i
++) {
1266 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1275 static bool name_is_illegal(const char *name
)
1277 return !*name
|| strchr(name
, '/') != NULL
;
1280 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1283 qid1
->type
!= qid2
->type
||
1284 qid1
->version
!= qid2
->version
||
1285 qid1
->path
!= qid2
->path
;
1288 static void coroutine_fn
v9fs_walk(void *opaque
)
1291 V9fsQID
*qids
= NULL
;
1293 V9fsPath dpath
, path
;
1297 int32_t fid
, newfid
;
1298 V9fsString
*wnames
= NULL
;
1300 V9fsFidState
*newfidp
= NULL
;
1301 V9fsPDU
*pdu
= opaque
;
1302 V9fsState
*s
= pdu
->s
;
1305 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1307 pdu_complete(pdu
, err
);
1312 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1314 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1315 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1316 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1317 for (i
= 0; i
< nwnames
; i
++) {
1318 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1322 if (name_is_illegal(wnames
[i
].data
)) {
1328 } else if (nwnames
> P9_MAXWELEM
) {
1332 fidp
= get_fid(pdu
, fid
);
1338 v9fs_path_init(&dpath
);
1339 v9fs_path_init(&path
);
1341 err
= fid_to_qid(pdu
, fidp
, &qid
);
1347 * Both dpath and path initially poin to fidp.
1348 * Needed to handle request with nwnames == 0
1350 v9fs_path_copy(&dpath
, &fidp
->path
);
1351 v9fs_path_copy(&path
, &fidp
->path
);
1352 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1353 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1354 strcmp("..", wnames
[name_idx
].data
)) {
1355 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1361 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1365 stat_to_qid(&stbuf
, &qid
);
1366 v9fs_path_copy(&dpath
, &path
);
1368 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1370 if (fid
== newfid
) {
1371 if (fidp
->fid_type
!= P9_FID_NONE
) {
1375 v9fs_path_copy(&fidp
->path
, &path
);
1377 newfidp
= alloc_fid(s
, newfid
);
1378 if (newfidp
== NULL
) {
1382 newfidp
->uid
= fidp
->uid
;
1383 v9fs_path_copy(&newfidp
->path
, &path
);
1385 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1386 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1390 put_fid(pdu
, newfidp
);
1392 v9fs_path_free(&dpath
);
1393 v9fs_path_free(&path
);
1395 pdu_complete(pdu
, err
);
1396 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1397 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1398 v9fs_string_free(&wnames
[name_idx
]);
1405 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1407 struct statfs stbuf
;
1409 V9fsState
*s
= pdu
->s
;
1412 * iounit should be multiples of f_bsize (host filesystem block size
1413 * and as well as less than (client msize - P9_IOHDRSZ))
1415 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1416 iounit
= stbuf
.f_bsize
;
1417 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1420 iounit
= s
->msize
- P9_IOHDRSZ
;
1425 static void coroutine_fn
v9fs_open(void *opaque
)
1436 V9fsPDU
*pdu
= opaque
;
1437 V9fsState
*s
= pdu
->s
;
1439 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1440 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1443 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1449 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1451 fidp
= get_fid(pdu
, fid
);
1456 if (fidp
->fid_type
!= P9_FID_NONE
) {
1461 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1465 stat_to_qid(&stbuf
, &qid
);
1466 if (S_ISDIR(stbuf
.st_mode
)) {
1467 err
= v9fs_co_opendir(pdu
, fidp
);
1471 fidp
->fid_type
= P9_FID_DIR
;
1472 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1478 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1479 flags
= get_dotl_openflags(s
, mode
);
1481 flags
= omode_to_uflags(mode
);
1483 if (is_ro_export(&s
->ctx
)) {
1484 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1485 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1490 err
= v9fs_co_open(pdu
, fidp
, flags
);
1494 fidp
->fid_type
= P9_FID_FILE
;
1495 fidp
->open_flags
= flags
;
1496 if (flags
& O_EXCL
) {
1498 * We let the host file system do O_EXCL check
1499 * We should not reclaim such fd
1501 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1503 iounit
= get_iounit(pdu
, &fidp
->path
);
1504 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1510 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1511 qid
.type
, qid
.version
, qid
.path
, iounit
);
1515 pdu_complete(pdu
, err
);
1518 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1520 int32_t dfid
, flags
, mode
;
1529 V9fsPDU
*pdu
= opaque
;
1531 v9fs_string_init(&name
);
1532 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1533 &name
, &flags
, &mode
, &gid
);
1537 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1539 if (name_is_illegal(name
.data
)) {
1544 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1549 fidp
= get_fid(pdu
, dfid
);
1554 if (fidp
->fid_type
!= P9_FID_NONE
) {
1559 flags
= get_dotl_openflags(pdu
->s
, flags
);
1560 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1561 flags
| O_CREAT
, mode
, &stbuf
);
1565 fidp
->fid_type
= P9_FID_FILE
;
1566 fidp
->open_flags
= flags
;
1567 if (flags
& O_EXCL
) {
1569 * We let the host file system do O_EXCL check
1570 * We should not reclaim such fd
1572 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1574 iounit
= get_iounit(pdu
, &fidp
->path
);
1575 stat_to_qid(&stbuf
, &qid
);
1576 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1581 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1582 qid
.type
, qid
.version
, qid
.path
, iounit
);
1586 pdu_complete(pdu
, err
);
1587 v9fs_string_free(&name
);
1590 static void coroutine_fn
v9fs_fsync(void *opaque
)
1597 V9fsPDU
*pdu
= opaque
;
1599 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1603 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1605 fidp
= get_fid(pdu
, fid
);
1610 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1616 pdu_complete(pdu
, err
);
1619 static void coroutine_fn
v9fs_clunk(void *opaque
)
1625 V9fsPDU
*pdu
= opaque
;
1626 V9fsState
*s
= pdu
->s
;
1628 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1632 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1634 fidp
= clunk_fid(s
, fid
);
1640 * Bump the ref so that put_fid will
1644 err
= put_fid(pdu
, fidp
);
1649 pdu_complete(pdu
, err
);
1653 * Create a QEMUIOVector for a sub-region of PDU iovecs
1655 * @qiov: uninitialized QEMUIOVector
1656 * @skip: number of bytes to skip from beginning of PDU
1657 * @size: number of bytes to include
1658 * @is_write: true - write, false - read
1660 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1661 * with qemu_iovec_destroy().
1663 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1664 size_t skip
, size_t size
,
1672 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
);
1674 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
+ skip
);
1677 qemu_iovec_init_external(&elem
, iov
, niov
);
1678 qemu_iovec_init(qiov
, niov
);
1679 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1682 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1683 uint64_t off
, uint32_t max_count
)
1687 uint64_t read_count
;
1688 QEMUIOVector qiov_full
;
1690 if (fidp
->fs
.xattr
.len
< off
) {
1693 read_count
= fidp
->fs
.xattr
.len
- off
;
1695 if (read_count
> max_count
) {
1696 read_count
= max_count
;
1698 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1704 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, read_count
, false);
1705 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, 0,
1706 ((char *)fidp
->fs
.xattr
.value
) + off
,
1708 qemu_iovec_destroy(&qiov_full
);
1716 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1725 off_t saved_dir_pos
;
1726 struct dirent
*dent
;
1728 /* save the directory position */
1729 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1730 if (saved_dir_pos
< 0) {
1731 return saved_dir_pos
;
1735 v9fs_path_init(&path
);
1737 v9fs_readdir_lock(&fidp
->fs
.dir
);
1739 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1743 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1747 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1751 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1755 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1756 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1758 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1760 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1761 /* Ran out of buffer. Set dir back to old position and return */
1762 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1763 v9fs_stat_free(&v9stat
);
1764 v9fs_path_free(&path
);
1768 v9fs_stat_free(&v9stat
);
1769 v9fs_path_free(&path
);
1770 saved_dir_pos
= dent
->d_off
;
1773 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1775 v9fs_path_free(&path
);
1782 static void coroutine_fn
v9fs_read(void *opaque
)
1791 V9fsPDU
*pdu
= opaque
;
1792 V9fsState
*s
= pdu
->s
;
1794 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1798 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1800 fidp
= get_fid(pdu
, fid
);
1805 if (fidp
->fid_type
== P9_FID_DIR
) {
1808 v9fs_co_rewinddir(pdu
, fidp
);
1810 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1815 err
= pdu_marshal(pdu
, offset
, "d", count
);
1819 err
+= offset
+ count
;
1820 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1821 QEMUIOVector qiov_full
;
1825 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1826 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1828 qemu_iovec_reset(&qiov
);
1829 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1831 print_sg(qiov
.iov
, qiov
.niov
);
1833 /* Loop in case of EINTR */
1835 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1840 } while (len
== -EINTR
&& !pdu
->cancelled
);
1842 /* IO error return the error */
1844 goto out_free_iovec
;
1846 } while (count
< max_count
&& len
> 0);
1847 err
= pdu_marshal(pdu
, offset
, "d", count
);
1849 goto out_free_iovec
;
1851 err
+= offset
+ count
;
1853 qemu_iovec_destroy(&qiov
);
1854 qemu_iovec_destroy(&qiov_full
);
1855 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1856 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1860 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1864 pdu_complete(pdu
, err
);
1867 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1870 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1871 * size of type (1) + size of name.size (2) + strlen(name.data)
1873 return 24 + v9fs_string_size(name
);
1876 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1884 off_t saved_dir_pos
;
1885 struct dirent
*dent
;
1887 /* save the directory position */
1888 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1889 if (saved_dir_pos
< 0) {
1890 return saved_dir_pos
;
1894 v9fs_readdir_lock(&fidp
->fs
.dir
);
1896 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1900 v9fs_string_init(&name
);
1901 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1902 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1903 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1905 /* Ran out of buffer. Set dir back to old position and return */
1906 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1907 v9fs_string_free(&name
);
1911 * Fill up just the path field of qid because the client uses
1912 * only that. To fill the entire qid structure we will have
1913 * to stat each dirent found, which is expensive
1915 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1916 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1917 /* Fill the other fields with dummy values */
1921 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1922 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1924 dent
->d_type
, &name
);
1926 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1929 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1930 v9fs_string_free(&name
);
1934 v9fs_string_free(&name
);
1935 saved_dir_pos
= dent
->d_off
;
1938 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1946 static void coroutine_fn
v9fs_readdir(void *opaque
)
1952 uint64_t initial_offset
;
1955 V9fsPDU
*pdu
= opaque
;
1957 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1958 &initial_offset
, &max_count
);
1962 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1964 fidp
= get_fid(pdu
, fid
);
1969 if (!fidp
->fs
.dir
.stream
) {
1973 if (initial_offset
== 0) {
1974 v9fs_co_rewinddir(pdu
, fidp
);
1976 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1978 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1983 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1987 retval
+= count
+ offset
;
1988 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1992 pdu_complete(pdu
, retval
);
1995 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1996 uint64_t off
, uint32_t count
,
1997 struct iovec
*sg
, int cnt
)
2001 uint64_t write_count
;
2005 if (fidp
->fs
.xattr
.len
< off
) {
2009 write_count
= fidp
->fs
.xattr
.len
- off
;
2010 if (write_count
> count
) {
2011 write_count
= count
;
2013 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2018 fidp
->fs
.xattr
.copied_len
+= write_count
;
2020 * Now copy the content from sg list
2022 for (i
= 0; i
< cnt
; i
++) {
2023 if (write_count
> sg
[i
].iov_len
) {
2024 to_copy
= sg
[i
].iov_len
;
2026 to_copy
= write_count
;
2028 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2029 /* updating vs->off since we are not using below */
2031 write_count
-= to_copy
;
2037 static void coroutine_fn
v9fs_write(void *opaque
)
2047 V9fsPDU
*pdu
= opaque
;
2048 V9fsState
*s
= pdu
->s
;
2049 QEMUIOVector qiov_full
;
2052 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2054 pdu_complete(pdu
, err
);
2058 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2059 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2061 fidp
= get_fid(pdu
, fid
);
2066 if (fidp
->fid_type
== P9_FID_FILE
) {
2067 if (fidp
->fs
.fd
== -1) {
2071 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2073 * setxattr operation
2075 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2076 qiov_full
.iov
, qiov_full
.niov
);
2082 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2084 qemu_iovec_reset(&qiov
);
2085 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2087 print_sg(qiov
.iov
, qiov
.niov
);
2089 /* Loop in case of EINTR */
2091 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2096 } while (len
== -EINTR
&& !pdu
->cancelled
);
2098 /* IO error return the error */
2102 } while (total
< count
&& len
> 0);
2105 err
= pdu_marshal(pdu
, offset
, "d", total
);
2110 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2112 qemu_iovec_destroy(&qiov
);
2116 qemu_iovec_destroy(&qiov_full
);
2117 pdu_complete(pdu
, err
);
2120 static void coroutine_fn
v9fs_create(void *opaque
)
2132 V9fsString extension
;
2134 V9fsPDU
*pdu
= opaque
;
2136 v9fs_path_init(&path
);
2137 v9fs_string_init(&name
);
2138 v9fs_string_init(&extension
);
2139 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2140 &perm
, &mode
, &extension
);
2144 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2146 if (name_is_illegal(name
.data
)) {
2151 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2156 fidp
= get_fid(pdu
, fid
);
2161 if (fidp
->fid_type
!= P9_FID_NONE
) {
2165 if (perm
& P9_STAT_MODE_DIR
) {
2166 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2167 fidp
->uid
, -1, &stbuf
);
2171 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2175 v9fs_path_copy(&fidp
->path
, &path
);
2176 err
= v9fs_co_opendir(pdu
, fidp
);
2180 fidp
->fid_type
= P9_FID_DIR
;
2181 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2182 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2183 extension
.data
, -1 , &stbuf
);
2187 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2191 v9fs_path_copy(&fidp
->path
, &path
);
2192 } else if (perm
& P9_STAT_MODE_LINK
) {
2193 int32_t ofid
= atoi(extension
.data
);
2194 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2195 if (ofidp
== NULL
) {
2199 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2200 put_fid(pdu
, ofidp
);
2204 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2206 fidp
->fid_type
= P9_FID_NONE
;
2209 v9fs_path_copy(&fidp
->path
, &path
);
2210 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2212 fidp
->fid_type
= P9_FID_NONE
;
2215 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2217 uint32_t major
, minor
;
2220 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2237 nmode
|= perm
& 0777;
2238 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2239 makedev(major
, minor
), nmode
, &stbuf
);
2243 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2247 v9fs_path_copy(&fidp
->path
, &path
);
2248 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2249 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2250 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2254 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2258 v9fs_path_copy(&fidp
->path
, &path
);
2259 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2260 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2261 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2265 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2269 v9fs_path_copy(&fidp
->path
, &path
);
2271 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2272 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2276 fidp
->fid_type
= P9_FID_FILE
;
2277 fidp
->open_flags
= omode_to_uflags(mode
);
2278 if (fidp
->open_flags
& O_EXCL
) {
2280 * We let the host file system do O_EXCL check
2281 * We should not reclaim such fd
2283 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2286 iounit
= get_iounit(pdu
, &fidp
->path
);
2287 stat_to_qid(&stbuf
, &qid
);
2288 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2293 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2294 qid
.type
, qid
.version
, qid
.path
, iounit
);
2298 pdu_complete(pdu
, err
);
2299 v9fs_string_free(&name
);
2300 v9fs_string_free(&extension
);
2301 v9fs_path_free(&path
);
2304 static void coroutine_fn
v9fs_symlink(void *opaque
)
2306 V9fsPDU
*pdu
= opaque
;
2309 V9fsFidState
*dfidp
;
2317 v9fs_string_init(&name
);
2318 v9fs_string_init(&symname
);
2319 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2323 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2325 if (name_is_illegal(name
.data
)) {
2330 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2335 dfidp
= get_fid(pdu
, dfid
);
2336 if (dfidp
== NULL
) {
2340 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2344 stat_to_qid(&stbuf
, &qid
);
2345 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2350 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2351 qid
.type
, qid
.version
, qid
.path
);
2353 put_fid(pdu
, dfidp
);
2355 pdu_complete(pdu
, err
);
2356 v9fs_string_free(&name
);
2357 v9fs_string_free(&symname
);
2360 static void coroutine_fn
v9fs_flush(void *opaque
)
2365 V9fsPDU
*cancel_pdu
= NULL
;
2366 V9fsPDU
*pdu
= opaque
;
2367 V9fsState
*s
= pdu
->s
;
2369 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2371 pdu_complete(pdu
, err
);
2374 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2376 if (pdu
->tag
== tag
) {
2377 error_report("Warning: the guest sent a self-referencing 9P flush request");
2379 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2380 if (cancel_pdu
->tag
== tag
) {
2386 cancel_pdu
->cancelled
= 1;
2388 * Wait for pdu to complete.
2390 qemu_co_queue_wait(&cancel_pdu
->complete
, NULL
);
2391 if (!qemu_co_queue_next(&cancel_pdu
->complete
)) {
2392 cancel_pdu
->cancelled
= 0;
2393 pdu_free(cancel_pdu
);
2396 pdu_complete(pdu
, 7);
2399 static void coroutine_fn
v9fs_link(void *opaque
)
2401 V9fsPDU
*pdu
= opaque
;
2402 int32_t dfid
, oldfid
;
2403 V9fsFidState
*dfidp
, *oldfidp
;
2408 v9fs_string_init(&name
);
2409 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2413 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2415 if (name_is_illegal(name
.data
)) {
2420 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2425 dfidp
= get_fid(pdu
, dfid
);
2426 if (dfidp
== NULL
) {
2431 oldfidp
= get_fid(pdu
, oldfid
);
2432 if (oldfidp
== NULL
) {
2436 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2440 put_fid(pdu
, oldfidp
);
2442 put_fid(pdu
, dfidp
);
2444 v9fs_string_free(&name
);
2445 pdu_complete(pdu
, err
);
2448 /* Only works with path name based fid */
2449 static void coroutine_fn
v9fs_remove(void *opaque
)
2455 V9fsPDU
*pdu
= opaque
;
2457 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2461 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2463 fidp
= get_fid(pdu
, fid
);
2468 /* if fs driver is not path based, return EOPNOTSUPP */
2469 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2474 * IF the file is unlinked, we cannot reopen
2475 * the file later. So don't reclaim fd
2477 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2481 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2486 /* For TREMOVE we need to clunk the fid even on failed remove */
2487 clunk_fid(pdu
->s
, fidp
->fid
);
2490 pdu_complete(pdu
, err
);
2493 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2497 int32_t dfid
, flags
;
2500 V9fsFidState
*dfidp
;
2501 V9fsPDU
*pdu
= opaque
;
2503 v9fs_string_init(&name
);
2504 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2509 if (name_is_illegal(name
.data
)) {
2514 if (!strcmp(".", name
.data
)) {
2519 if (!strcmp("..", name
.data
)) {
2524 dfidp
= get_fid(pdu
, dfid
);
2525 if (dfidp
== NULL
) {
2530 * IF the file is unlinked, we cannot reopen
2531 * the file later. So don't reclaim fd
2533 v9fs_path_init(&path
);
2534 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2538 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2542 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2547 put_fid(pdu
, dfidp
);
2548 v9fs_path_free(&path
);
2550 pdu_complete(pdu
, err
);
2551 v9fs_string_free(&name
);
2555 /* Only works with path name based fid */
2556 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2563 V9fsFidState
*tfidp
;
2564 V9fsState
*s
= pdu
->s
;
2565 V9fsFidState
*dirfidp
= NULL
;
2566 char *old_name
, *new_name
;
2568 v9fs_path_init(&new_path
);
2569 if (newdirfid
!= -1) {
2570 dirfidp
= get_fid(pdu
, newdirfid
);
2571 if (dirfidp
== NULL
) {
2575 if (fidp
->fid_type
!= P9_FID_NONE
) {
2579 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2581 old_name
= fidp
->path
.data
;
2582 end
= strrchr(old_name
, '/');
2588 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2589 strncat(new_name
, old_name
, end
- old_name
);
2590 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2591 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2594 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2599 * Fixup fid's pointing to the old name to
2600 * start pointing to the new name
2602 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2603 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2604 /* replace the name */
2605 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2610 put_fid(pdu
, dirfidp
);
2612 v9fs_path_free(&new_path
);
2617 /* Only works with path name based fid */
2618 static void coroutine_fn
v9fs_rename(void *opaque
)
2626 V9fsPDU
*pdu
= opaque
;
2627 V9fsState
*s
= pdu
->s
;
2629 v9fs_string_init(&name
);
2630 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2635 if (name_is_illegal(name
.data
)) {
2640 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2645 fidp
= get_fid(pdu
, fid
);
2650 if (fidp
->fid_type
!= P9_FID_NONE
) {
2654 /* if fs driver is not path based, return EOPNOTSUPP */
2655 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2659 v9fs_path_write_lock(s
);
2660 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2661 v9fs_path_unlock(s
);
2668 pdu_complete(pdu
, err
);
2669 v9fs_string_free(&name
);
2672 static void coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2673 V9fsString
*old_name
,
2675 V9fsString
*new_name
)
2677 V9fsFidState
*tfidp
;
2678 V9fsPath oldpath
, newpath
;
2679 V9fsState
*s
= pdu
->s
;
2682 v9fs_path_init(&oldpath
);
2683 v9fs_path_init(&newpath
);
2684 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2685 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2688 * Fixup fid's pointing to the old name to
2689 * start pointing to the new name
2691 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2692 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2693 /* replace the name */
2694 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2697 v9fs_path_free(&oldpath
);
2698 v9fs_path_free(&newpath
);
2701 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2702 V9fsString
*old_name
,
2704 V9fsString
*new_name
)
2707 V9fsState
*s
= pdu
->s
;
2708 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2710 olddirfidp
= get_fid(pdu
, olddirfid
);
2711 if (olddirfidp
== NULL
) {
2715 if (newdirfid
!= -1) {
2716 newdirfidp
= get_fid(pdu
, newdirfid
);
2717 if (newdirfidp
== NULL
) {
2722 newdirfidp
= get_fid(pdu
, olddirfid
);
2725 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2726 &newdirfidp
->path
, new_name
);
2730 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2731 /* Only for path based fid we need to do the below fixup */
2732 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2733 &newdirfidp
->path
, new_name
);
2737 put_fid(pdu
, olddirfidp
);
2740 put_fid(pdu
, newdirfidp
);
2745 static void coroutine_fn
v9fs_renameat(void *opaque
)
2749 V9fsPDU
*pdu
= opaque
;
2750 V9fsState
*s
= pdu
->s
;
2751 int32_t olddirfid
, newdirfid
;
2752 V9fsString old_name
, new_name
;
2754 v9fs_string_init(&old_name
);
2755 v9fs_string_init(&new_name
);
2756 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2757 &old_name
, &newdirfid
, &new_name
);
2762 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2767 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
2768 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
2773 v9fs_path_write_lock(s
);
2774 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2775 &old_name
, newdirfid
, &new_name
);
2776 v9fs_path_unlock(s
);
2782 pdu_complete(pdu
, err
);
2783 v9fs_string_free(&old_name
);
2784 v9fs_string_free(&new_name
);
2787 static void coroutine_fn
v9fs_wstat(void *opaque
)
2796 V9fsPDU
*pdu
= opaque
;
2798 v9fs_stat_init(&v9stat
);
2799 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2803 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2804 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2806 fidp
= get_fid(pdu
, fid
);
2811 /* do we need to sync the file? */
2812 if (donttouch_stat(&v9stat
)) {
2813 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2816 if (v9stat
.mode
!= -1) {
2818 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2822 v9_mode
= stat_to_v9mode(&stbuf
);
2823 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2824 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2825 /* Attempting to change the type */
2829 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2830 v9mode_to_mode(v9stat
.mode
,
2831 &v9stat
.extension
));
2836 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2837 struct timespec times
[2];
2838 if (v9stat
.atime
!= -1) {
2839 times
[0].tv_sec
= v9stat
.atime
;
2840 times
[0].tv_nsec
= 0;
2842 times
[0].tv_nsec
= UTIME_OMIT
;
2844 if (v9stat
.mtime
!= -1) {
2845 times
[1].tv_sec
= v9stat
.mtime
;
2846 times
[1].tv_nsec
= 0;
2848 times
[1].tv_nsec
= UTIME_OMIT
;
2850 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2855 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2856 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2861 if (v9stat
.name
.size
!= 0) {
2862 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2867 if (v9stat
.length
!= -1) {
2868 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2877 v9fs_stat_free(&v9stat
);
2878 pdu_complete(pdu
, err
);
2881 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2893 int32_t bsize_factor
;
2896 * compute bsize factor based on host file system block size
2899 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2900 if (!bsize_factor
) {
2903 f_type
= stbuf
->f_type
;
2904 f_bsize
= stbuf
->f_bsize
;
2905 f_bsize
*= bsize_factor
;
2907 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2908 * adjust(divide) the number of blocks, free blocks and available
2909 * blocks by bsize factor
2911 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2912 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2913 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2914 f_files
= stbuf
->f_files
;
2915 f_ffree
= stbuf
->f_ffree
;
2916 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2917 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2918 f_namelen
= stbuf
->f_namelen
;
2920 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2921 f_type
, f_bsize
, f_blocks
, f_bfree
,
2922 f_bavail
, f_files
, f_ffree
,
2923 fsid_val
, f_namelen
);
2926 static void coroutine_fn
v9fs_statfs(void *opaque
)
2932 struct statfs stbuf
;
2933 V9fsPDU
*pdu
= opaque
;
2934 V9fsState
*s
= pdu
->s
;
2936 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2940 fidp
= get_fid(pdu
, fid
);
2945 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2949 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2957 pdu_complete(pdu
, retval
);
2960 static void coroutine_fn
v9fs_mknod(void *opaque
)
2973 V9fsPDU
*pdu
= opaque
;
2975 v9fs_string_init(&name
);
2976 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2977 &major
, &minor
, &gid
);
2981 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2983 if (name_is_illegal(name
.data
)) {
2988 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2993 fidp
= get_fid(pdu
, fid
);
2998 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2999 makedev(major
, minor
), mode
, &stbuf
);
3003 stat_to_qid(&stbuf
, &qid
);
3004 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3009 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
3010 qid
.type
, qid
.version
, qid
.path
);
3014 pdu_complete(pdu
, err
);
3015 v9fs_string_free(&name
);
3019 * Implement posix byte range locking code
3020 * Server side handling of locking code is very simple, because 9p server in
3021 * QEMU can handle only one client. And most of the lock handling
3022 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3023 * do any thing in * qemu 9p server side lock code path.
3024 * So when a TLOCK request comes, always return success
3026 static void coroutine_fn
v9fs_lock(void *opaque
)
3032 int32_t fid
, err
= 0;
3033 V9fsPDU
*pdu
= opaque
;
3035 v9fs_string_init(&flock
.client_id
);
3036 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3037 &flock
.flags
, &flock
.start
, &flock
.length
,
3038 &flock
.proc_id
, &flock
.client_id
);
3042 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3043 flock
.type
, flock
.start
, flock
.length
);
3046 /* We support only block flag now (that too ignored currently) */
3047 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3051 fidp
= get_fid(pdu
, fid
);
3056 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3060 err
= pdu_marshal(pdu
, offset
, "b", P9_LOCK_SUCCESS
);
3065 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, P9_LOCK_SUCCESS
);
3069 pdu_complete(pdu
, err
);
3070 v9fs_string_free(&flock
.client_id
);
3074 * When a TGETLOCK request comes, always return success because all lock
3075 * handling is done by client's VFS layer.
3077 static void coroutine_fn
v9fs_getlock(void *opaque
)
3083 int32_t fid
, err
= 0;
3084 V9fsPDU
*pdu
= opaque
;
3086 v9fs_string_init(&glock
.client_id
);
3087 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3088 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3093 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3094 glock
.type
, glock
.start
, glock
.length
);
3096 fidp
= get_fid(pdu
, fid
);
3101 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3105 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3106 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3107 glock
.start
, glock
.length
, glock
.proc_id
,
3113 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3114 glock
.length
, glock
.proc_id
);
3118 pdu_complete(pdu
, err
);
3119 v9fs_string_free(&glock
.client_id
);
3122 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3124 V9fsPDU
*pdu
= opaque
;
3135 v9fs_string_init(&name
);
3136 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3140 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3142 if (name_is_illegal(name
.data
)) {
3147 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3152 fidp
= get_fid(pdu
, fid
);
3157 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3161 stat_to_qid(&stbuf
, &qid
);
3162 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3167 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3168 qid
.type
, qid
.version
, qid
.path
, err
);
3172 pdu_complete(pdu
, err
);
3173 v9fs_string_free(&name
);
3176 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3182 int32_t fid
, newfid
;
3183 V9fsFidState
*file_fidp
;
3184 V9fsFidState
*xattr_fidp
= NULL
;
3185 V9fsPDU
*pdu
= opaque
;
3186 V9fsState
*s
= pdu
->s
;
3188 v9fs_string_init(&name
);
3189 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3193 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3195 file_fidp
= get_fid(pdu
, fid
);
3196 if (file_fidp
== NULL
) {
3200 xattr_fidp
= alloc_fid(s
, newfid
);
3201 if (xattr_fidp
== NULL
) {
3205 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3206 if (!v9fs_string_size(&name
)) {
3208 * listxattr request. Get the size first
3210 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3213 clunk_fid(s
, xattr_fidp
->fid
);
3217 * Read the xattr value
3219 xattr_fidp
->fs
.xattr
.len
= size
;
3220 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3221 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3223 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3224 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3225 xattr_fidp
->fs
.xattr
.value
,
3226 xattr_fidp
->fs
.xattr
.len
);
3228 clunk_fid(s
, xattr_fidp
->fid
);
3232 err
= pdu_marshal(pdu
, offset
, "q", size
);
3239 * specific xattr fid. We check for xattr
3240 * presence also collect the xattr size
3242 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3246 clunk_fid(s
, xattr_fidp
->fid
);
3250 * Read the xattr value
3252 xattr_fidp
->fs
.xattr
.len
= size
;
3253 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3254 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3256 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3257 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3258 &name
, xattr_fidp
->fs
.xattr
.value
,
3259 xattr_fidp
->fs
.xattr
.len
);
3261 clunk_fid(s
, xattr_fidp
->fid
);
3265 err
= pdu_marshal(pdu
, offset
, "q", size
);
3271 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3273 put_fid(pdu
, file_fidp
);
3275 put_fid(pdu
, xattr_fidp
);
3278 pdu_complete(pdu
, err
);
3279 v9fs_string_free(&name
);
3282 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3290 V9fsFidState
*file_fidp
;
3291 V9fsFidState
*xattr_fidp
;
3292 V9fsPDU
*pdu
= opaque
;
3294 v9fs_string_init(&name
);
3295 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3299 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3301 if (size
> XATTR_SIZE_MAX
) {
3306 file_fidp
= get_fid(pdu
, fid
);
3307 if (file_fidp
== NULL
) {
3311 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3316 /* Make the file fid point to xattr */
3317 xattr_fidp
= file_fidp
;
3318 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3319 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3320 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3321 xattr_fidp
->fs
.xattr
.len
= size
;
3322 xattr_fidp
->fs
.xattr
.flags
= flags
;
3323 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3324 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3325 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3328 put_fid(pdu
, file_fidp
);
3330 pdu_complete(pdu
, err
);
3331 v9fs_string_free(&name
);
3334 static void coroutine_fn
v9fs_readlink(void *opaque
)
3336 V9fsPDU
*pdu
= opaque
;
3343 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3347 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3348 fidp
= get_fid(pdu
, fid
);
3354 v9fs_string_init(&target
);
3355 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3359 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3361 v9fs_string_free(&target
);
3365 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3366 v9fs_string_free(&target
);
3370 pdu_complete(pdu
, err
);
3373 static CoroutineEntry
*pdu_co_handlers
[] = {
3374 [P9_TREADDIR
] = v9fs_readdir
,
3375 [P9_TSTATFS
] = v9fs_statfs
,
3376 [P9_TGETATTR
] = v9fs_getattr
,
3377 [P9_TSETATTR
] = v9fs_setattr
,
3378 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3379 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3380 [P9_TMKNOD
] = v9fs_mknod
,
3381 [P9_TRENAME
] = v9fs_rename
,
3382 [P9_TLOCK
] = v9fs_lock
,
3383 [P9_TGETLOCK
] = v9fs_getlock
,
3384 [P9_TRENAMEAT
] = v9fs_renameat
,
3385 [P9_TREADLINK
] = v9fs_readlink
,
3386 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3387 [P9_TMKDIR
] = v9fs_mkdir
,
3388 [P9_TVERSION
] = v9fs_version
,
3389 [P9_TLOPEN
] = v9fs_open
,
3390 [P9_TATTACH
] = v9fs_attach
,
3391 [P9_TSTAT
] = v9fs_stat
,
3392 [P9_TWALK
] = v9fs_walk
,
3393 [P9_TCLUNK
] = v9fs_clunk
,
3394 [P9_TFSYNC
] = v9fs_fsync
,
3395 [P9_TOPEN
] = v9fs_open
,
3396 [P9_TREAD
] = v9fs_read
,
3398 [P9_TAUTH
] = v9fs_auth
,
3400 [P9_TFLUSH
] = v9fs_flush
,
3401 [P9_TLINK
] = v9fs_link
,
3402 [P9_TSYMLINK
] = v9fs_symlink
,
3403 [P9_TCREATE
] = v9fs_create
,
3404 [P9_TLCREATE
] = v9fs_lcreate
,
3405 [P9_TWRITE
] = v9fs_write
,
3406 [P9_TWSTAT
] = v9fs_wstat
,
3407 [P9_TREMOVE
] = v9fs_remove
,
3410 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3412 V9fsPDU
*pdu
= opaque
;
3413 pdu_complete(pdu
, -EOPNOTSUPP
);
3416 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3418 V9fsPDU
*pdu
= opaque
;
3419 pdu_complete(pdu
, -EROFS
);
3422 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3449 void pdu_submit(V9fsPDU
*pdu
)
3452 CoroutineEntry
*handler
;
3453 V9fsState
*s
= pdu
->s
;
3455 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3456 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3457 handler
= v9fs_op_not_supp
;
3459 handler
= pdu_co_handlers
[pdu
->id
];
3462 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3463 handler
= v9fs_fs_ro
;
3465 co
= qemu_coroutine_create(handler
, pdu
);
3466 qemu_coroutine_enter(co
);
3469 /* Returns 0 on success, 1 on failure. */
3470 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3478 /* initialize pdu allocator */
3479 QLIST_INIT(&s
->free_list
);
3480 QLIST_INIT(&s
->active_list
);
3481 for (i
= 0; i
< MAX_REQ
; i
++) {
3482 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
3487 v9fs_path_init(&path
);
3489 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3492 /* We don't have a fsdev identified by fsdev_id */
3493 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3495 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3499 if (!s
->fsconf
.tag
) {
3500 /* we haven't specified a mount_tag */
3501 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3502 s
->fsconf
.fsdev_id
);
3506 s
->ctx
.export_flags
= fse
->export_flags
;
3507 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3508 s
->ctx
.exops
.get_st_gen
= NULL
;
3509 len
= strlen(s
->fsconf
.tag
);
3510 if (len
> MAX_TAG_LEN
- 1) {
3511 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3512 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3516 s
->tag
= g_strdup(s
->fsconf
.tag
);
3522 qemu_co_rwlock_init(&s
->rename_lock
);
3524 if (s
->ops
->init(&s
->ctx
) < 0) {
3525 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3526 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3531 * Check details of export path, We need to use fs driver
3532 * call back to do that. Since we are in the init path, we don't
3533 * use co-routines here.
3535 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3537 "error in converting name to path %s", strerror(errno
));
3540 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3541 error_setg(errp
, "share path %s does not exist", fse
->path
);
3543 } else if (!S_ISDIR(stat
.st_mode
)) {
3544 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3548 s
->ctx
.fst
= &fse
->fst
;
3549 fsdev_throttle_init(s
->ctx
.fst
);
3551 v9fs_path_free(&path
);
3556 if (s
->ops
&& s
->ops
->cleanup
&& s
->ctx
.private) {
3557 s
->ops
->cleanup(&s
->ctx
);
3560 g_free(s
->ctx
.fs_root
);
3561 v9fs_path_free(&path
);
3566 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3568 if (s
->ops
->cleanup
) {
3569 s
->ops
->cleanup(&s
->ctx
);
3571 fsdev_throttle_cleanup(s
->ctx
.fst
);
3573 g_free(s
->ctx
.fs_root
);
3576 typedef struct VirtfsCoResetData
{
3579 } VirtfsCoResetData
;
3581 static void coroutine_fn
virtfs_co_reset(void *opaque
)
3583 VirtfsCoResetData
*data
= opaque
;
3585 virtfs_reset(&data
->pdu
);
3589 void v9fs_reset(V9fsState
*s
)
3591 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
3594 while (!QLIST_EMPTY(&s
->active_list
)) {
3595 aio_poll(qemu_get_aio_context(), true);
3598 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
3599 qemu_coroutine_enter(co
);
3601 while (!data
.done
) {
3602 aio_poll(qemu_get_aio_context(), true);
3606 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3609 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3610 error_report("Failed to get the resource limit");
3613 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3614 open_fd_rc
= rlim
.rlim_cur
/2;