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 "hw/virtio/virtio.h"
15 #include "hw/i386/pc.h"
16 #include "qemu/sockets.h"
17 #include "virtio-9p.h"
18 #include "fsdev/qemu-fsdev.h"
19 #include "virtio-9p-xattr.h"
20 #include "virtio-9p-coth.h"
22 #include "migration/migration.h"
26 static int open_fd_rc
;
40 static int omode_to_uflags(int8_t mode
)
74 struct dotl_openflag_map
{
79 static int dotl_to_open_flags(int flags
)
83 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
84 * and P9_DOTL_NOACCESS
86 int oflags
= flags
& O_ACCMODE
;
88 struct dotl_openflag_map dotl_oflag_map
[] = {
89 { P9_DOTL_CREATE
, O_CREAT
},
90 { P9_DOTL_EXCL
, O_EXCL
},
91 { P9_DOTL_NOCTTY
, O_NOCTTY
},
92 { P9_DOTL_TRUNC
, O_TRUNC
},
93 { P9_DOTL_APPEND
, O_APPEND
},
94 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
95 { P9_DOTL_DSYNC
, O_DSYNC
},
96 { P9_DOTL_FASYNC
, FASYNC
},
97 { P9_DOTL_DIRECT
, O_DIRECT
},
98 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
99 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
100 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
101 { P9_DOTL_NOATIME
, O_NOATIME
},
102 { P9_DOTL_SYNC
, O_SYNC
},
105 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
106 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
107 oflags
|= dotl_oflag_map
[i
].open_flag
;
114 void cred_init(FsCred
*credp
)
122 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
126 * Filter the client open flags
128 flags
= dotl_to_open_flags(oflags
);
129 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
131 * Ignore direct disk access hint until the server supports it.
137 void v9fs_path_init(V9fsPath
*path
)
143 void v9fs_path_free(V9fsPath
*path
)
150 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
153 lhs
->data
= g_malloc(rhs
->size
);
154 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
155 lhs
->size
= rhs
->size
;
158 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
159 const char *name
, V9fsPath
*path
)
162 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
170 * Return TRUE if s1 is an ancestor of s2.
172 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
173 * As a special case, We treat s1 as ancestor of s2 if they are same!
175 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
177 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
178 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
185 static size_t v9fs_string_size(V9fsString
*str
)
191 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
192 static int v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
195 if (f
->fid_type
== P9_FID_FILE
) {
196 if (f
->fs
.fd
== -1) {
198 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
199 } while (err
== -EINTR
&& !pdu
->cancelled
);
201 } else if (f
->fid_type
== P9_FID_DIR
) {
202 if (f
->fs
.dir
== NULL
) {
204 err
= v9fs_co_opendir(pdu
, f
);
205 } while (err
== -EINTR
&& !pdu
->cancelled
);
211 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
215 V9fsState
*s
= pdu
->s
;
217 for (f
= s
->fid_list
; f
; f
= f
->next
) {
221 * Update the fid ref upfront so that
222 * we don't get reclaimed when we yield
227 * check whether we need to reopen the
228 * file. We might have closed the fd
229 * while trying to free up some file
232 err
= v9fs_reopen_fid(pdu
, f
);
238 * Mark the fid as referenced so that the LRU
239 * reclaim won't close the file descriptor
241 f
->flags
|= FID_REFERENCED
;
248 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
252 for (f
= s
->fid_list
; f
; f
= f
->next
) {
253 /* If fid is already there return NULL */
259 f
= g_malloc0(sizeof(V9fsFidState
));
261 f
->fid_type
= P9_FID_NONE
;
264 * Mark the fid as referenced so that the LRU
265 * reclaim won't close the file descriptor
267 f
->flags
|= FID_REFERENCED
;
268 f
->next
= s
->fid_list
;
274 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
278 if (fidp
->fs
.xattr
.copied_len
== -1) {
279 /* getxattr/listxattr fid */
283 * if this is fid for setxattr. clunk should
284 * result in setxattr localcall
286 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
287 /* clunk after partial write */
291 if (fidp
->fs
.xattr
.len
) {
292 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
293 fidp
->fs
.xattr
.value
,
295 fidp
->fs
.xattr
.flags
);
297 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
300 v9fs_string_free(&fidp
->fs
.xattr
.name
);
302 g_free(fidp
->fs
.xattr
.value
);
306 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
310 if (fidp
->fid_type
== P9_FID_FILE
) {
311 /* If we reclaimed the fd no need to close */
312 if (fidp
->fs
.fd
!= -1) {
313 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
315 } else if (fidp
->fid_type
== P9_FID_DIR
) {
316 if (fidp
->fs
.dir
!= NULL
) {
317 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
319 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
320 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
322 v9fs_path_free(&fidp
->path
);
327 static int put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
332 * Don't free the fid if it is in reclaim list
334 if (!fidp
->ref
&& fidp
->clunked
) {
335 if (fidp
->fid
== pdu
->s
->root_fid
) {
337 * if the clunked fid is root fid then we
338 * have unmounted the fs on the client side.
339 * delete the migration blocker. Ideally, this
340 * should be hooked to transport close notification
342 if (pdu
->s
->migration_blocker
) {
343 migrate_del_blocker(pdu
->s
->migration_blocker
);
344 error_free(pdu
->s
->migration_blocker
);
345 pdu
->s
->migration_blocker
= NULL
;
348 return free_fid(pdu
, fidp
);
353 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
355 V9fsFidState
**fidpp
, *fidp
;
357 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
358 if ((*fidpp
)->fid
== fid
) {
362 if (*fidpp
== NULL
) {
371 void v9fs_reclaim_fd(V9fsPDU
*pdu
)
373 int reclaim_count
= 0;
374 V9fsState
*s
= pdu
->s
;
375 V9fsFidState
*f
, *reclaim_list
= NULL
;
377 for (f
= s
->fid_list
; f
; f
= f
->next
) {
379 * Unlink fids cannot be reclaimed. Check
380 * for them and skip them. Also skip fids
381 * currently being operated on.
383 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
387 * if it is a recently referenced fid
388 * we leave the fid untouched and clear the
389 * reference bit. We come back to it later
390 * in the next iteration. (a simple LRU without
391 * moving list elements around)
393 if (f
->flags
& FID_REFERENCED
) {
394 f
->flags
&= ~FID_REFERENCED
;
398 * Add fids to reclaim list.
400 if (f
->fid_type
== P9_FID_FILE
) {
401 if (f
->fs
.fd
!= -1) {
403 * Up the reference count so that
404 * a clunk request won't free this fid
407 f
->rclm_lst
= reclaim_list
;
409 f
->fs_reclaim
.fd
= f
->fs
.fd
;
413 } else if (f
->fid_type
== P9_FID_DIR
) {
414 if (f
->fs
.dir
!= NULL
) {
416 * Up the reference count so that
417 * a clunk request won't free this fid
420 f
->rclm_lst
= reclaim_list
;
422 f
->fs_reclaim
.dir
= f
->fs
.dir
;
427 if (reclaim_count
>= open_fd_rc
) {
432 * Now close the fid in reclaim list. Free them if they
433 * are already clunked.
435 while (reclaim_list
) {
437 reclaim_list
= f
->rclm_lst
;
438 if (f
->fid_type
== P9_FID_FILE
) {
439 v9fs_co_close(pdu
, &f
->fs_reclaim
);
440 } else if (f
->fid_type
== P9_FID_DIR
) {
441 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
445 * Now drop the fid reference, free it
452 static int v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
455 V9fsState
*s
= pdu
->s
;
456 V9fsFidState
*fidp
, head_fid
;
458 head_fid
.next
= s
->fid_list
;
459 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
460 if (fidp
->path
.size
!= path
->size
) {
463 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
464 /* Mark the fid non reclaimable. */
465 fidp
->flags
|= FID_NON_RECLAIMABLE
;
467 /* reopen the file/dir if already closed */
468 err
= v9fs_reopen_fid(pdu
, fidp
);
473 * Go back to head of fid list because
474 * the list could have got updated when
475 * switched to the worker thread
485 static void virtfs_reset(V9fsPDU
*pdu
)
487 V9fsState
*s
= pdu
->s
;
488 V9fsFidState
*fidp
= NULL
;
491 while (s
->fid_list
) {
493 s
->fid_list
= fidp
->next
;
502 /* One or more unclunked fids found... */
503 error_report("9pfs:%s: One or more uncluncked fids "
504 "found during reset", __func__
);
508 #define P9_QID_TYPE_DIR 0x80
509 #define P9_QID_TYPE_SYMLINK 0x02
511 #define P9_STAT_MODE_DIR 0x80000000
512 #define P9_STAT_MODE_APPEND 0x40000000
513 #define P9_STAT_MODE_EXCL 0x20000000
514 #define P9_STAT_MODE_MOUNT 0x10000000
515 #define P9_STAT_MODE_AUTH 0x08000000
516 #define P9_STAT_MODE_TMP 0x04000000
517 #define P9_STAT_MODE_SYMLINK 0x02000000
518 #define P9_STAT_MODE_LINK 0x01000000
519 #define P9_STAT_MODE_DEVICE 0x00800000
520 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
521 #define P9_STAT_MODE_SOCKET 0x00100000
522 #define P9_STAT_MODE_SETUID 0x00080000
523 #define P9_STAT_MODE_SETGID 0x00040000
524 #define P9_STAT_MODE_SETVTX 0x00010000
526 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
527 P9_STAT_MODE_SYMLINK | \
528 P9_STAT_MODE_LINK | \
529 P9_STAT_MODE_DEVICE | \
530 P9_STAT_MODE_NAMED_PIPE | \
533 /* This is the algorithm from ufs in spfs */
534 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
538 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
539 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
540 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
541 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
543 if (S_ISDIR(stbuf
->st_mode
)) {
544 qidp
->type
|= P9_QID_TYPE_DIR
;
546 if (S_ISLNK(stbuf
->st_mode
)) {
547 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
551 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
556 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
560 stat_to_qid(&stbuf
, qidp
);
564 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
568 if (!QLIST_EMPTY(&s
->free_list
)) {
569 pdu
= QLIST_FIRST(&s
->free_list
);
570 QLIST_REMOVE(pdu
, next
);
571 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
576 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
580 * Cancelled pdu are added back to the freelist
583 if (!pdu
->cancelled
) {
584 QLIST_REMOVE(pdu
, next
);
585 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
591 * We don't do error checking for pdu_marshal/unmarshal here
592 * because we always expect to have enough space to encode
595 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
597 int8_t id
= pdu
->id
+ 1; /* Response */
603 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
606 str
.data
= strerror(err
);
607 str
.size
= strlen(str
.data
);
609 len
+= pdu_marshal(pdu
, len
, "s", &str
);
613 len
+= pdu_marshal(pdu
, len
, "d", err
);
615 if (s
->proto_version
== V9FS_PROTO_2000L
) {
618 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
621 /* fill out the header */
622 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
624 /* keep these in sync */
628 /* push onto queue and notify */
629 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
631 /* FIXME: we should batch these completions */
632 virtio_notify(VIRTIO_DEVICE(s
), s
->vq
);
634 /* Now wakeup anybody waiting in flush for this request */
635 qemu_co_queue_next(&pdu
->complete
);
640 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
645 if (mode
& P9_STAT_MODE_DIR
) {
649 if (mode
& P9_STAT_MODE_SYMLINK
) {
652 if (mode
& P9_STAT_MODE_SOCKET
) {
655 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
658 if (mode
& P9_STAT_MODE_DEVICE
) {
659 if (extension
->size
&& extension
->data
[0] == 'c') {
670 if (mode
& P9_STAT_MODE_SETUID
) {
673 if (mode
& P9_STAT_MODE_SETGID
) {
676 if (mode
& P9_STAT_MODE_SETVTX
) {
683 static int donttouch_stat(V9fsStat
*stat
)
685 if (stat
->type
== -1 &&
687 stat
->qid
.type
== -1 &&
688 stat
->qid
.version
== -1 &&
689 stat
->qid
.path
== -1 &&
693 stat
->length
== -1 &&
700 stat
->n_muid
== -1) {
707 static void v9fs_stat_init(V9fsStat
*stat
)
709 v9fs_string_init(&stat
->name
);
710 v9fs_string_init(&stat
->uid
);
711 v9fs_string_init(&stat
->gid
);
712 v9fs_string_init(&stat
->muid
);
713 v9fs_string_init(&stat
->extension
);
716 static void v9fs_stat_free(V9fsStat
*stat
)
718 v9fs_string_free(&stat
->name
);
719 v9fs_string_free(&stat
->uid
);
720 v9fs_string_free(&stat
->gid
);
721 v9fs_string_free(&stat
->muid
);
722 v9fs_string_free(&stat
->extension
);
725 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
729 mode
= stbuf
->st_mode
& 0777;
730 if (S_ISDIR(stbuf
->st_mode
)) {
731 mode
|= P9_STAT_MODE_DIR
;
734 if (S_ISLNK(stbuf
->st_mode
)) {
735 mode
|= P9_STAT_MODE_SYMLINK
;
738 if (S_ISSOCK(stbuf
->st_mode
)) {
739 mode
|= P9_STAT_MODE_SOCKET
;
742 if (S_ISFIFO(stbuf
->st_mode
)) {
743 mode
|= P9_STAT_MODE_NAMED_PIPE
;
746 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
747 mode
|= P9_STAT_MODE_DEVICE
;
750 if (stbuf
->st_mode
& S_ISUID
) {
751 mode
|= P9_STAT_MODE_SETUID
;
754 if (stbuf
->st_mode
& S_ISGID
) {
755 mode
|= P9_STAT_MODE_SETGID
;
758 if (stbuf
->st_mode
& S_ISVTX
) {
759 mode
|= P9_STAT_MODE_SETVTX
;
765 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
766 const struct stat
*stbuf
,
772 memset(v9stat
, 0, sizeof(*v9stat
));
774 stat_to_qid(stbuf
, &v9stat
->qid
);
775 v9stat
->mode
= stat_to_v9mode(stbuf
);
776 v9stat
->atime
= stbuf
->st_atime
;
777 v9stat
->mtime
= stbuf
->st_mtime
;
778 v9stat
->length
= stbuf
->st_size
;
780 v9fs_string_null(&v9stat
->uid
);
781 v9fs_string_null(&v9stat
->gid
);
782 v9fs_string_null(&v9stat
->muid
);
784 v9stat
->n_uid
= stbuf
->st_uid
;
785 v9stat
->n_gid
= stbuf
->st_gid
;
788 v9fs_string_null(&v9stat
->extension
);
790 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
791 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
795 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
796 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
797 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
798 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
799 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
800 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
801 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
804 str
= strrchr(name
->data
, '/');
811 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
814 v9fs_string_size(&v9stat
->name
) +
815 v9fs_string_size(&v9stat
->uid
) +
816 v9fs_string_size(&v9stat
->gid
) +
817 v9fs_string_size(&v9stat
->muid
) +
818 v9fs_string_size(&v9stat
->extension
);
822 #define P9_STATS_MODE 0x00000001ULL
823 #define P9_STATS_NLINK 0x00000002ULL
824 #define P9_STATS_UID 0x00000004ULL
825 #define P9_STATS_GID 0x00000008ULL
826 #define P9_STATS_RDEV 0x00000010ULL
827 #define P9_STATS_ATIME 0x00000020ULL
828 #define P9_STATS_MTIME 0x00000040ULL
829 #define P9_STATS_CTIME 0x00000080ULL
830 #define P9_STATS_INO 0x00000100ULL
831 #define P9_STATS_SIZE 0x00000200ULL
832 #define P9_STATS_BLOCKS 0x00000400ULL
834 #define P9_STATS_BTIME 0x00000800ULL
835 #define P9_STATS_GEN 0x00001000ULL
836 #define P9_STATS_DATA_VERSION 0x00002000ULL
838 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
839 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
842 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
843 V9fsStatDotl
*v9lstat
)
845 memset(v9lstat
, 0, sizeof(*v9lstat
));
847 v9lstat
->st_mode
= stbuf
->st_mode
;
848 v9lstat
->st_nlink
= stbuf
->st_nlink
;
849 v9lstat
->st_uid
= stbuf
->st_uid
;
850 v9lstat
->st_gid
= stbuf
->st_gid
;
851 v9lstat
->st_rdev
= stbuf
->st_rdev
;
852 v9lstat
->st_size
= stbuf
->st_size
;
853 v9lstat
->st_blksize
= stbuf
->st_blksize
;
854 v9lstat
->st_blocks
= stbuf
->st_blocks
;
855 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
856 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
857 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
858 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
859 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
860 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
861 /* Currently we only support BASIC fields in stat */
862 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
864 stat_to_qid(stbuf
, &v9lstat
->qid
);
867 static void print_sg(struct iovec
*sg
, int cnt
)
871 printf("sg[%d]: {", cnt
);
872 for (i
= 0; i
< cnt
; i
++) {
876 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
881 /* Will call this only for path name based fid */
882 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
885 v9fs_path_init(&str
);
886 v9fs_path_copy(&str
, dst
);
887 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
888 v9fs_path_free(&str
);
889 /* +1 to include terminating NULL */
893 static inline bool is_ro_export(FsContext
*ctx
)
895 return ctx
->export_flags
& V9FS_RDONLY
;
898 static void v9fs_version(void *opaque
)
901 V9fsPDU
*pdu
= opaque
;
902 V9fsState
*s
= pdu
->s
;
906 v9fs_string_init(&version
);
907 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
912 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
916 if (!strcmp(version
.data
, "9P2000.u")) {
917 s
->proto_version
= V9FS_PROTO_2000U
;
918 } else if (!strcmp(version
.data
, "9P2000.L")) {
919 s
->proto_version
= V9FS_PROTO_2000L
;
921 v9fs_string_sprintf(&version
, "unknown");
924 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
930 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
932 complete_pdu(s
, pdu
, offset
);
933 v9fs_string_free(&version
);
936 static void v9fs_attach(void *opaque
)
938 V9fsPDU
*pdu
= opaque
;
939 V9fsState
*s
= pdu
->s
;
940 int32_t fid
, afid
, n_uname
;
941 V9fsString uname
, aname
;
947 v9fs_string_init(&uname
);
948 v9fs_string_init(&aname
);
949 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
950 &afid
, &uname
, &aname
, &n_uname
);
954 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
956 fidp
= alloc_fid(s
, fid
);
962 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
968 err
= fid_to_qid(pdu
, fidp
, &qid
);
974 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
980 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
981 qid
.type
, qid
.version
, qid
.path
);
983 * disable migration if we haven't done already.
984 * attach could get called multiple times for the same export.
986 if (!s
->migration_blocker
) {
988 error_setg(&s
->migration_blocker
,
989 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
990 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
991 migrate_add_blocker(s
->migration_blocker
);
996 complete_pdu(s
, pdu
, err
);
997 v9fs_string_free(&uname
);
998 v9fs_string_free(&aname
);
1001 static void v9fs_stat(void *opaque
)
1009 V9fsPDU
*pdu
= opaque
;
1010 V9fsState
*s
= pdu
->s
;
1012 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1016 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1018 fidp
= get_fid(pdu
, fid
);
1023 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1027 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1031 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1033 v9fs_stat_free(&v9stat
);
1036 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1037 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1039 v9fs_stat_free(&v9stat
);
1043 complete_pdu(s
, pdu
, err
);
1046 static void v9fs_getattr(void *opaque
)
1053 uint64_t request_mask
;
1054 V9fsStatDotl v9stat_dotl
;
1055 V9fsPDU
*pdu
= opaque
;
1056 V9fsState
*s
= pdu
->s
;
1058 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1062 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1064 fidp
= get_fid(pdu
, fid
);
1070 * Currently we only support BASIC fields in stat, so there is no
1071 * need to look at request_mask.
1073 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1077 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1079 /* fill st_gen if requested and supported by underlying fs */
1080 if (request_mask
& P9_STATS_GEN
) {
1081 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1084 /* we have valid st_gen: update result mask */
1085 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1088 /* request cancelled, e.g. by Tflush */
1091 /* failed to get st_gen: not fatal, ignore */
1095 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1100 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1101 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1102 v9stat_dotl
.st_gid
);
1106 complete_pdu(s
, pdu
, retval
);
1109 /* Attribute flags */
1110 #define P9_ATTR_MODE (1 << 0)
1111 #define P9_ATTR_UID (1 << 1)
1112 #define P9_ATTR_GID (1 << 2)
1113 #define P9_ATTR_SIZE (1 << 3)
1114 #define P9_ATTR_ATIME (1 << 4)
1115 #define P9_ATTR_MTIME (1 << 5)
1116 #define P9_ATTR_CTIME (1 << 6)
1117 #define P9_ATTR_ATIME_SET (1 << 7)
1118 #define P9_ATTR_MTIME_SET (1 << 8)
1120 #define P9_ATTR_MASK 127
1122 static void v9fs_setattr(void *opaque
)
1129 V9fsPDU
*pdu
= opaque
;
1130 V9fsState
*s
= pdu
->s
;
1132 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1137 fidp
= get_fid(pdu
, fid
);
1142 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1143 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1148 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1149 struct timespec times
[2];
1150 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1151 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1152 times
[0].tv_sec
= v9iattr
.atime_sec
;
1153 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1155 times
[0].tv_nsec
= UTIME_NOW
;
1158 times
[0].tv_nsec
= UTIME_OMIT
;
1160 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1161 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1162 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1163 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1165 times
[1].tv_nsec
= UTIME_NOW
;
1168 times
[1].tv_nsec
= UTIME_OMIT
;
1170 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1176 * If the only valid entry in iattr is ctime we can call
1177 * chown(-1,-1) to update the ctime of the file
1179 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1180 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1181 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1182 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1185 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1188 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1194 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1195 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1204 complete_pdu(s
, pdu
, err
);
1207 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1213 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1218 for (i
= 0; i
< nwnames
; i
++) {
1219 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1228 static void v9fs_walk(void *opaque
)
1231 V9fsQID
*qids
= NULL
;
1233 V9fsPath dpath
, path
;
1237 int32_t fid
, newfid
;
1238 V9fsString
*wnames
= NULL
;
1240 V9fsFidState
*newfidp
= NULL
;
1241 V9fsPDU
*pdu
= opaque
;
1242 V9fsState
*s
= pdu
->s
;
1244 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1246 complete_pdu(s
, pdu
, err
);
1251 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1253 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1254 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1255 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1256 for (i
= 0; i
< nwnames
; i
++) {
1257 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1263 } else if (nwnames
> P9_MAXWELEM
) {
1267 fidp
= get_fid(pdu
, fid
);
1272 v9fs_path_init(&dpath
);
1273 v9fs_path_init(&path
);
1275 * Both dpath and path initially poin to fidp.
1276 * Needed to handle request with nwnames == 0
1278 v9fs_path_copy(&dpath
, &fidp
->path
);
1279 v9fs_path_copy(&path
, &fidp
->path
);
1280 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1281 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1285 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1289 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1290 v9fs_path_copy(&dpath
, &path
);
1292 if (fid
== newfid
) {
1293 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1294 v9fs_path_copy(&fidp
->path
, &path
);
1296 newfidp
= alloc_fid(s
, newfid
);
1297 if (newfidp
== NULL
) {
1301 newfidp
->uid
= fidp
->uid
;
1302 v9fs_path_copy(&newfidp
->path
, &path
);
1304 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1305 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1309 put_fid(pdu
, newfidp
);
1311 v9fs_path_free(&dpath
);
1312 v9fs_path_free(&path
);
1314 complete_pdu(s
, pdu
, err
);
1315 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1316 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1317 v9fs_string_free(&wnames
[name_idx
]);
1324 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1326 struct statfs stbuf
;
1328 V9fsState
*s
= pdu
->s
;
1331 * iounit should be multiples of f_bsize (host filesystem block size
1332 * and as well as less than (client msize - P9_IOHDRSZ))
1334 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1335 iounit
= stbuf
.f_bsize
;
1336 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1339 iounit
= s
->msize
- P9_IOHDRSZ
;
1344 static void v9fs_open(void *opaque
)
1355 V9fsPDU
*pdu
= opaque
;
1356 V9fsState
*s
= pdu
->s
;
1358 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1359 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1362 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1368 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1370 fidp
= get_fid(pdu
, fid
);
1375 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1377 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1381 stat_to_qid(&stbuf
, &qid
);
1382 if (S_ISDIR(stbuf
.st_mode
)) {
1383 err
= v9fs_co_opendir(pdu
, fidp
);
1387 fidp
->fid_type
= P9_FID_DIR
;
1388 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1394 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1395 flags
= get_dotl_openflags(s
, mode
);
1397 flags
= omode_to_uflags(mode
);
1399 if (is_ro_export(&s
->ctx
)) {
1400 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1401 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1406 err
= v9fs_co_open(pdu
, fidp
, flags
);
1410 fidp
->fid_type
= P9_FID_FILE
;
1411 fidp
->open_flags
= flags
;
1412 if (flags
& O_EXCL
) {
1414 * We let the host file system do O_EXCL check
1415 * We should not reclaim such fd
1417 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1419 iounit
= get_iounit(pdu
, &fidp
->path
);
1420 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1426 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1427 qid
.type
, qid
.version
, qid
.path
, iounit
);
1431 complete_pdu(s
, pdu
, err
);
1434 static void v9fs_lcreate(void *opaque
)
1436 int32_t dfid
, flags
, mode
;
1445 V9fsPDU
*pdu
= opaque
;
1447 v9fs_string_init(&name
);
1448 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1449 &name
, &flags
, &mode
, &gid
);
1453 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1455 fidp
= get_fid(pdu
, dfid
);
1461 flags
= get_dotl_openflags(pdu
->s
, flags
);
1462 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1463 flags
| O_CREAT
, mode
, &stbuf
);
1467 fidp
->fid_type
= P9_FID_FILE
;
1468 fidp
->open_flags
= flags
;
1469 if (flags
& O_EXCL
) {
1471 * We let the host file system do O_EXCL check
1472 * We should not reclaim such fd
1474 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1476 iounit
= get_iounit(pdu
, &fidp
->path
);
1477 stat_to_qid(&stbuf
, &qid
);
1478 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1483 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1484 qid
.type
, qid
.version
, qid
.path
, iounit
);
1488 complete_pdu(pdu
->s
, pdu
, err
);
1489 v9fs_string_free(&name
);
1492 static void v9fs_fsync(void *opaque
)
1499 V9fsPDU
*pdu
= opaque
;
1500 V9fsState
*s
= pdu
->s
;
1502 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1506 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1508 fidp
= get_fid(pdu
, fid
);
1513 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1519 complete_pdu(s
, pdu
, err
);
1522 static void v9fs_clunk(void *opaque
)
1528 V9fsPDU
*pdu
= opaque
;
1529 V9fsState
*s
= pdu
->s
;
1531 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1535 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1537 fidp
= clunk_fid(s
, fid
);
1543 * Bump the ref so that put_fid will
1547 err
= put_fid(pdu
, fidp
);
1552 complete_pdu(s
, pdu
, err
);
1555 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1556 uint64_t off
, uint32_t max_count
)
1563 xattr_len
= fidp
->fs
.xattr
.len
;
1564 read_count
= xattr_len
- off
;
1565 if (read_count
> max_count
) {
1566 read_count
= max_count
;
1567 } else if (read_count
< 0) {
1569 * read beyond XATTR value
1573 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1578 err
= v9fs_pack(pdu
->elem
.in_sg
, pdu
->elem
.in_num
, offset
,
1579 ((char *)fidp
->fs
.xattr
.value
) + off
,
1588 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1589 V9fsFidState
*fidp
, uint32_t max_count
)
1596 off_t saved_dir_pos
;
1597 struct dirent
*dent
, *result
;
1599 /* save the directory position */
1600 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1601 if (saved_dir_pos
< 0) {
1602 return saved_dir_pos
;
1605 dent
= g_malloc(sizeof(struct dirent
));
1608 v9fs_path_init(&path
);
1609 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1610 if (err
|| !result
) {
1613 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1617 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1621 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1625 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1626 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1627 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1628 /* Ran out of buffer. Set dir back to old position and return */
1629 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1630 v9fs_stat_free(&v9stat
);
1631 v9fs_path_free(&path
);
1636 v9fs_stat_free(&v9stat
);
1637 v9fs_path_free(&path
);
1638 saved_dir_pos
= dent
->d_off
;
1642 v9fs_path_free(&path
);
1650 * Create a QEMUIOVector for a sub-region of PDU iovecs
1652 * @qiov: uninitialized QEMUIOVector
1653 * @skip: number of bytes to skip from beginning of PDU
1654 * @size: number of bytes to include
1655 * @is_write: true - write, false - read
1657 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1658 * with qemu_iovec_destroy().
1660 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1661 size_t skip
, size_t size
,
1669 iov
= pdu
->elem
.out_sg
;
1670 niov
= pdu
->elem
.out_num
;
1672 iov
= pdu
->elem
.in_sg
;
1673 niov
= pdu
->elem
.in_num
;
1676 qemu_iovec_init_external(&elem
, iov
, niov
);
1677 qemu_iovec_init(qiov
, niov
);
1678 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1681 static void v9fs_read(void *opaque
)
1690 V9fsPDU
*pdu
= opaque
;
1691 V9fsState
*s
= pdu
->s
;
1693 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1697 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1699 fidp
= get_fid(pdu
, fid
);
1704 if (fidp
->fid_type
== P9_FID_DIR
) {
1707 v9fs_co_rewinddir(pdu
, fidp
);
1709 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1714 err
= pdu_marshal(pdu
, offset
, "d", count
);
1718 err
+= offset
+ count
;
1719 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1720 QEMUIOVector qiov_full
;
1724 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1725 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1727 qemu_iovec_reset(&qiov
);
1728 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1730 print_sg(qiov
.iov
, qiov
.niov
);
1732 /* Loop in case of EINTR */
1734 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1739 } while (len
== -EINTR
&& !pdu
->cancelled
);
1741 /* IO error return the error */
1745 } while (count
< max_count
&& len
> 0);
1746 err
= pdu_marshal(pdu
, offset
, "d", count
);
1750 err
+= offset
+ count
;
1751 qemu_iovec_destroy(&qiov
);
1752 qemu_iovec_destroy(&qiov_full
);
1753 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1754 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1758 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1762 complete_pdu(s
, pdu
, err
);
1765 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1768 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1769 * size of type (1) + size of name.size (2) + strlen(name.data)
1771 return 24 + v9fs_string_size(name
);
1774 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1775 V9fsFidState
*fidp
, int32_t max_count
)
1782 off_t saved_dir_pos
;
1783 struct dirent
*dent
, *result
;
1785 /* save the directory position */
1786 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1787 if (saved_dir_pos
< 0) {
1788 return saved_dir_pos
;
1791 dent
= g_malloc(sizeof(struct dirent
));
1794 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1795 if (err
|| !result
) {
1798 v9fs_string_init(&name
);
1799 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1800 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1801 /* Ran out of buffer. Set dir back to old position and return */
1802 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1803 v9fs_string_free(&name
);
1808 * Fill up just the path field of qid because the client uses
1809 * only that. To fill the entire qid structure we will have
1810 * to stat each dirent found, which is expensive
1812 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1813 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1814 /* Fill the other fields with dummy values */
1818 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1819 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1821 dent
->d_type
, &name
);
1823 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1824 v9fs_string_free(&name
);
1829 v9fs_string_free(&name
);
1830 saved_dir_pos
= dent
->d_off
;
1839 static void v9fs_readdir(void *opaque
)
1845 uint64_t initial_offset
;
1848 V9fsPDU
*pdu
= opaque
;
1849 V9fsState
*s
= pdu
->s
;
1851 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1852 &initial_offset
, &max_count
);
1856 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1858 fidp
= get_fid(pdu
, fid
);
1863 if (!fidp
->fs
.dir
) {
1867 if (initial_offset
== 0) {
1868 v9fs_co_rewinddir(pdu
, fidp
);
1870 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1872 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1877 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1881 retval
+= count
+ offset
;
1882 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1886 complete_pdu(s
, pdu
, retval
);
1889 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1890 uint64_t off
, uint32_t count
,
1891 struct iovec
*sg
, int cnt
)
1900 xattr_len
= fidp
->fs
.xattr
.len
;
1901 write_count
= xattr_len
- off
;
1902 if (write_count
> count
) {
1903 write_count
= count
;
1904 } else if (write_count
< 0) {
1906 * write beyond XATTR value len specified in
1912 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1917 fidp
->fs
.xattr
.copied_len
+= write_count
;
1919 * Now copy the content from sg list
1921 for (i
= 0; i
< cnt
; i
++) {
1922 if (write_count
> sg
[i
].iov_len
) {
1923 to_copy
= sg
[i
].iov_len
;
1925 to_copy
= write_count
;
1927 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1928 /* updating vs->off since we are not using below */
1930 write_count
-= to_copy
;
1936 static void v9fs_write(void *opaque
)
1946 V9fsPDU
*pdu
= opaque
;
1947 V9fsState
*s
= pdu
->s
;
1948 QEMUIOVector qiov_full
;
1951 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
1953 return complete_pdu(s
, pdu
, err
);
1956 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
1957 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
1959 fidp
= get_fid(pdu
, fid
);
1964 if (fidp
->fid_type
== P9_FID_FILE
) {
1965 if (fidp
->fs
.fd
== -1) {
1969 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1971 * setxattr operation
1973 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
1974 qiov_full
.iov
, qiov_full
.niov
);
1980 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1982 qemu_iovec_reset(&qiov
);
1983 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
1985 print_sg(qiov
.iov
, qiov
.niov
);
1987 /* Loop in case of EINTR */
1989 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1994 } while (len
== -EINTR
&& !pdu
->cancelled
);
1996 /* IO error return the error */
2000 } while (total
< count
&& len
> 0);
2003 err
= pdu_marshal(pdu
, offset
, "d", total
);
2008 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2010 qemu_iovec_destroy(&qiov
);
2014 qemu_iovec_destroy(&qiov_full
);
2015 complete_pdu(s
, pdu
, err
);
2018 static void v9fs_create(void *opaque
)
2030 V9fsString extension
;
2032 V9fsPDU
*pdu
= opaque
;
2034 v9fs_path_init(&path
);
2035 v9fs_string_init(&name
);
2036 v9fs_string_init(&extension
);
2037 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2038 &perm
, &mode
, &extension
);
2042 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2044 fidp
= get_fid(pdu
, fid
);
2049 if (perm
& P9_STAT_MODE_DIR
) {
2050 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2051 fidp
->uid
, -1, &stbuf
);
2055 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2059 v9fs_path_copy(&fidp
->path
, &path
);
2060 err
= v9fs_co_opendir(pdu
, fidp
);
2064 fidp
->fid_type
= P9_FID_DIR
;
2065 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2066 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2067 extension
.data
, -1 , &stbuf
);
2071 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2075 v9fs_path_copy(&fidp
->path
, &path
);
2076 } else if (perm
& P9_STAT_MODE_LINK
) {
2077 int32_t ofid
= atoi(extension
.data
);
2078 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2079 if (ofidp
== NULL
) {
2083 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2084 put_fid(pdu
, ofidp
);
2088 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2090 fidp
->fid_type
= P9_FID_NONE
;
2093 v9fs_path_copy(&fidp
->path
, &path
);
2094 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2096 fidp
->fid_type
= P9_FID_NONE
;
2099 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2101 uint32_t major
, minor
;
2104 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2121 nmode
|= perm
& 0777;
2122 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2123 makedev(major
, minor
), nmode
, &stbuf
);
2127 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2131 v9fs_path_copy(&fidp
->path
, &path
);
2132 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2133 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2134 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2138 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2142 v9fs_path_copy(&fidp
->path
, &path
);
2143 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2144 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2145 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2149 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2153 v9fs_path_copy(&fidp
->path
, &path
);
2155 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2156 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2160 fidp
->fid_type
= P9_FID_FILE
;
2161 fidp
->open_flags
= omode_to_uflags(mode
);
2162 if (fidp
->open_flags
& O_EXCL
) {
2164 * We let the host file system do O_EXCL check
2165 * We should not reclaim such fd
2167 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2170 iounit
= get_iounit(pdu
, &fidp
->path
);
2171 stat_to_qid(&stbuf
, &qid
);
2172 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2177 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2178 qid
.type
, qid
.version
, qid
.path
, iounit
);
2182 complete_pdu(pdu
->s
, pdu
, err
);
2183 v9fs_string_free(&name
);
2184 v9fs_string_free(&extension
);
2185 v9fs_path_free(&path
);
2188 static void v9fs_symlink(void *opaque
)
2190 V9fsPDU
*pdu
= opaque
;
2193 V9fsFidState
*dfidp
;
2201 v9fs_string_init(&name
);
2202 v9fs_string_init(&symname
);
2203 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2207 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2209 dfidp
= get_fid(pdu
, dfid
);
2210 if (dfidp
== NULL
) {
2214 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2218 stat_to_qid(&stbuf
, &qid
);
2219 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2224 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2225 qid
.type
, qid
.version
, qid
.path
);
2227 put_fid(pdu
, dfidp
);
2229 complete_pdu(pdu
->s
, pdu
, err
);
2230 v9fs_string_free(&name
);
2231 v9fs_string_free(&symname
);
2234 static void v9fs_flush(void *opaque
)
2239 V9fsPDU
*cancel_pdu
;
2240 V9fsPDU
*pdu
= opaque
;
2241 V9fsState
*s
= pdu
->s
;
2243 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2245 complete_pdu(s
, pdu
, err
);
2248 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2250 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2251 if (cancel_pdu
->tag
== tag
) {
2256 cancel_pdu
->cancelled
= 1;
2258 * Wait for pdu to complete.
2260 qemu_co_queue_wait(&cancel_pdu
->complete
);
2261 cancel_pdu
->cancelled
= 0;
2262 free_pdu(pdu
->s
, cancel_pdu
);
2264 complete_pdu(s
, pdu
, 7);
2267 static void v9fs_link(void *opaque
)
2269 V9fsPDU
*pdu
= opaque
;
2270 V9fsState
*s
= pdu
->s
;
2271 int32_t dfid
, oldfid
;
2272 V9fsFidState
*dfidp
, *oldfidp
;
2277 v9fs_string_init(&name
);
2278 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2282 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2284 dfidp
= get_fid(pdu
, dfid
);
2285 if (dfidp
== NULL
) {
2290 oldfidp
= get_fid(pdu
, oldfid
);
2291 if (oldfidp
== NULL
) {
2295 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2300 put_fid(pdu
, dfidp
);
2302 v9fs_string_free(&name
);
2303 complete_pdu(s
, pdu
, err
);
2306 /* Only works with path name based fid */
2307 static void v9fs_remove(void *opaque
)
2313 V9fsPDU
*pdu
= opaque
;
2315 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2319 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2321 fidp
= get_fid(pdu
, fid
);
2326 /* if fs driver is not path based, return EOPNOTSUPP */
2327 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2332 * IF the file is unlinked, we cannot reopen
2333 * the file later. So don't reclaim fd
2335 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2339 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2344 /* For TREMOVE we need to clunk the fid even on failed remove */
2345 clunk_fid(pdu
->s
, fidp
->fid
);
2348 complete_pdu(pdu
->s
, pdu
, err
);
2351 static void v9fs_unlinkat(void *opaque
)
2355 int32_t dfid
, flags
;
2358 V9fsFidState
*dfidp
;
2359 V9fsPDU
*pdu
= opaque
;
2361 v9fs_string_init(&name
);
2362 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2366 dfidp
= get_fid(pdu
, dfid
);
2367 if (dfidp
== NULL
) {
2372 * IF the file is unlinked, we cannot reopen
2373 * the file later. So don't reclaim fd
2375 v9fs_path_init(&path
);
2376 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2380 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2384 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2389 put_fid(pdu
, dfidp
);
2390 v9fs_path_free(&path
);
2392 complete_pdu(pdu
->s
, pdu
, err
);
2393 v9fs_string_free(&name
);
2397 /* Only works with path name based fid */
2398 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2399 int32_t newdirfid
, V9fsString
*name
)
2404 V9fsFidState
*tfidp
;
2405 V9fsState
*s
= pdu
->s
;
2406 V9fsFidState
*dirfidp
= NULL
;
2407 char *old_name
, *new_name
;
2409 v9fs_path_init(&new_path
);
2410 if (newdirfid
!= -1) {
2411 dirfidp
= get_fid(pdu
, newdirfid
);
2412 if (dirfidp
== NULL
) {
2416 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2417 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2419 old_name
= fidp
->path
.data
;
2420 end
= strrchr(old_name
, '/');
2426 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2427 strncat(new_name
, old_name
, end
- old_name
);
2428 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2429 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2432 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2437 * Fixup fid's pointing to the old name to
2438 * start pointing to the new name
2440 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2441 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2442 /* replace the name */
2443 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2448 put_fid(pdu
, dirfidp
);
2450 v9fs_path_free(&new_path
);
2455 /* Only works with path name based fid */
2456 static void v9fs_rename(void *opaque
)
2464 V9fsPDU
*pdu
= opaque
;
2465 V9fsState
*s
= pdu
->s
;
2467 v9fs_string_init(&name
);
2468 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2472 fidp
= get_fid(pdu
, fid
);
2477 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2478 /* if fs driver is not path based, return EOPNOTSUPP */
2479 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2483 v9fs_path_write_lock(s
);
2484 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2485 v9fs_path_unlock(s
);
2492 complete_pdu(s
, pdu
, err
);
2493 v9fs_string_free(&name
);
2496 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2497 V9fsString
*old_name
, V9fsPath
*newdir
,
2498 V9fsString
*new_name
)
2500 V9fsFidState
*tfidp
;
2501 V9fsPath oldpath
, newpath
;
2502 V9fsState
*s
= pdu
->s
;
2505 v9fs_path_init(&oldpath
);
2506 v9fs_path_init(&newpath
);
2507 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2508 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2511 * Fixup fid's pointing to the old name to
2512 * start pointing to the new name
2514 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2515 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2516 /* replace the name */
2517 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2520 v9fs_path_free(&oldpath
);
2521 v9fs_path_free(&newpath
);
2524 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2525 V9fsString
*old_name
, int32_t newdirfid
,
2526 V9fsString
*new_name
)
2529 V9fsState
*s
= pdu
->s
;
2530 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2532 olddirfidp
= get_fid(pdu
, olddirfid
);
2533 if (olddirfidp
== NULL
) {
2537 if (newdirfid
!= -1) {
2538 newdirfidp
= get_fid(pdu
, newdirfid
);
2539 if (newdirfidp
== NULL
) {
2544 newdirfidp
= get_fid(pdu
, olddirfid
);
2547 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2548 &newdirfidp
->path
, new_name
);
2552 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2553 /* Only for path based fid we need to do the below fixup */
2554 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2555 &newdirfidp
->path
, new_name
);
2559 put_fid(pdu
, olddirfidp
);
2562 put_fid(pdu
, newdirfidp
);
2567 static void v9fs_renameat(void *opaque
)
2571 V9fsPDU
*pdu
= opaque
;
2572 V9fsState
*s
= pdu
->s
;
2573 int32_t olddirfid
, newdirfid
;
2574 V9fsString old_name
, new_name
;
2576 v9fs_string_init(&old_name
);
2577 v9fs_string_init(&new_name
);
2578 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2579 &old_name
, &newdirfid
, &new_name
);
2584 v9fs_path_write_lock(s
);
2585 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2586 &old_name
, newdirfid
, &new_name
);
2587 v9fs_path_unlock(s
);
2593 complete_pdu(s
, pdu
, err
);
2594 v9fs_string_free(&old_name
);
2595 v9fs_string_free(&new_name
);
2598 static void v9fs_wstat(void *opaque
)
2607 V9fsPDU
*pdu
= opaque
;
2608 V9fsState
*s
= pdu
->s
;
2610 v9fs_stat_init(&v9stat
);
2611 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2615 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2616 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2618 fidp
= get_fid(pdu
, fid
);
2623 /* do we need to sync the file? */
2624 if (donttouch_stat(&v9stat
)) {
2625 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2628 if (v9stat
.mode
!= -1) {
2630 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2634 v9_mode
= stat_to_v9mode(&stbuf
);
2635 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2636 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2637 /* Attempting to change the type */
2641 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2642 v9mode_to_mode(v9stat
.mode
,
2643 &v9stat
.extension
));
2648 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2649 struct timespec times
[2];
2650 if (v9stat
.atime
!= -1) {
2651 times
[0].tv_sec
= v9stat
.atime
;
2652 times
[0].tv_nsec
= 0;
2654 times
[0].tv_nsec
= UTIME_OMIT
;
2656 if (v9stat
.mtime
!= -1) {
2657 times
[1].tv_sec
= v9stat
.mtime
;
2658 times
[1].tv_nsec
= 0;
2660 times
[1].tv_nsec
= UTIME_OMIT
;
2662 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2667 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2668 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2673 if (v9stat
.name
.size
!= 0) {
2674 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2679 if (v9stat
.length
!= -1) {
2680 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2689 v9fs_stat_free(&v9stat
);
2690 complete_pdu(s
, pdu
, err
);
2693 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2705 int32_t bsize_factor
;
2708 * compute bsize factor based on host file system block size
2711 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2712 if (!bsize_factor
) {
2715 f_type
= stbuf
->f_type
;
2716 f_bsize
= stbuf
->f_bsize
;
2717 f_bsize
*= bsize_factor
;
2719 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2720 * adjust(divide) the number of blocks, free blocks and available
2721 * blocks by bsize factor
2723 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2724 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2725 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2726 f_files
= stbuf
->f_files
;
2727 f_ffree
= stbuf
->f_ffree
;
2728 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2729 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2730 f_namelen
= stbuf
->f_namelen
;
2732 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2733 f_type
, f_bsize
, f_blocks
, f_bfree
,
2734 f_bavail
, f_files
, f_ffree
,
2735 fsid_val
, f_namelen
);
2738 static void v9fs_statfs(void *opaque
)
2744 struct statfs stbuf
;
2745 V9fsPDU
*pdu
= opaque
;
2746 V9fsState
*s
= pdu
->s
;
2748 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2752 fidp
= get_fid(pdu
, fid
);
2757 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2761 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2769 complete_pdu(s
, pdu
, retval
);
2772 static void v9fs_mknod(void *opaque
)
2785 V9fsPDU
*pdu
= opaque
;
2786 V9fsState
*s
= pdu
->s
;
2788 v9fs_string_init(&name
);
2789 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2790 &major
, &minor
, &gid
);
2794 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2796 fidp
= get_fid(pdu
, fid
);
2801 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2802 makedev(major
, minor
), mode
, &stbuf
);
2806 stat_to_qid(&stbuf
, &qid
);
2807 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2812 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2813 qid
.type
, qid
.version
, qid
.path
);
2817 complete_pdu(s
, pdu
, err
);
2818 v9fs_string_free(&name
);
2822 * Implement posix byte range locking code
2823 * Server side handling of locking code is very simple, because 9p server in
2824 * QEMU can handle only one client. And most of the lock handling
2825 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2826 * do any thing in * qemu 9p server side lock code path.
2827 * So when a TLOCK request comes, always return success
2829 static void v9fs_lock(void *opaque
)
2836 int32_t fid
, err
= 0;
2837 V9fsPDU
*pdu
= opaque
;
2838 V9fsState
*s
= pdu
->s
;
2840 status
= P9_LOCK_ERROR
;
2841 v9fs_string_init(&flock
.client_id
);
2842 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2843 &flock
.flags
, &flock
.start
, &flock
.length
,
2844 &flock
.proc_id
, &flock
.client_id
);
2848 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2849 flock
.type
, flock
.start
, flock
.length
);
2852 /* We support only block flag now (that too ignored currently) */
2853 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2857 fidp
= get_fid(pdu
, fid
);
2862 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2866 status
= P9_LOCK_SUCCESS
;
2870 err
= pdu_marshal(pdu
, offset
, "b", status
);
2874 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2875 complete_pdu(s
, pdu
, err
);
2876 v9fs_string_free(&flock
.client_id
);
2880 * When a TGETLOCK request comes, always return success because all lock
2881 * handling is done by client's VFS layer.
2883 static void v9fs_getlock(void *opaque
)
2889 int32_t fid
, err
= 0;
2890 V9fsPDU
*pdu
= opaque
;
2891 V9fsState
*s
= pdu
->s
;
2893 v9fs_string_init(&glock
.client_id
);
2894 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
2895 &glock
.start
, &glock
.length
, &glock
.proc_id
,
2900 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2901 glock
.type
, glock
.start
, glock
.length
);
2903 fidp
= get_fid(pdu
, fid
);
2908 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2912 glock
.type
= P9_LOCK_TYPE_UNLCK
;
2913 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
2914 glock
.start
, glock
.length
, glock
.proc_id
,
2920 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
2921 glock
.length
, glock
.proc_id
);
2925 complete_pdu(s
, pdu
, err
);
2926 v9fs_string_free(&glock
.client_id
);
2929 static void v9fs_mkdir(void *opaque
)
2931 V9fsPDU
*pdu
= opaque
;
2942 v9fs_string_init(&name
);
2943 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2947 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
2949 fidp
= get_fid(pdu
, fid
);
2954 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2958 stat_to_qid(&stbuf
, &qid
);
2959 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2964 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
2965 qid
.type
, qid
.version
, qid
.path
, err
);
2969 complete_pdu(pdu
->s
, pdu
, err
);
2970 v9fs_string_free(&name
);
2973 static void v9fs_xattrwalk(void *opaque
)
2979 int32_t fid
, newfid
;
2980 V9fsFidState
*file_fidp
;
2981 V9fsFidState
*xattr_fidp
= NULL
;
2982 V9fsPDU
*pdu
= opaque
;
2983 V9fsState
*s
= pdu
->s
;
2985 v9fs_string_init(&name
);
2986 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
2990 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
2992 file_fidp
= get_fid(pdu
, fid
);
2993 if (file_fidp
== NULL
) {
2997 xattr_fidp
= alloc_fid(s
, newfid
);
2998 if (xattr_fidp
== NULL
) {
3002 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3003 if (name
.data
== NULL
) {
3005 * listxattr request. Get the size first
3007 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3010 clunk_fid(s
, xattr_fidp
->fid
);
3014 * Read the xattr value
3016 xattr_fidp
->fs
.xattr
.len
= size
;
3017 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3018 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3020 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3021 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3022 xattr_fidp
->fs
.xattr
.value
,
3023 xattr_fidp
->fs
.xattr
.len
);
3025 clunk_fid(s
, xattr_fidp
->fid
);
3029 err
= pdu_marshal(pdu
, offset
, "q", size
);
3036 * specific xattr fid. We check for xattr
3037 * presence also collect the xattr size
3039 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3043 clunk_fid(s
, xattr_fidp
->fid
);
3047 * Read the xattr value
3049 xattr_fidp
->fs
.xattr
.len
= size
;
3050 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3051 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3053 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3054 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3055 &name
, xattr_fidp
->fs
.xattr
.value
,
3056 xattr_fidp
->fs
.xattr
.len
);
3058 clunk_fid(s
, xattr_fidp
->fid
);
3062 err
= pdu_marshal(pdu
, offset
, "q", size
);
3068 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3070 put_fid(pdu
, file_fidp
);
3072 put_fid(pdu
, xattr_fidp
);
3075 complete_pdu(s
, pdu
, err
);
3076 v9fs_string_free(&name
);
3079 static void v9fs_xattrcreate(void *opaque
)
3087 V9fsFidState
*file_fidp
;
3088 V9fsFidState
*xattr_fidp
;
3089 V9fsPDU
*pdu
= opaque
;
3090 V9fsState
*s
= pdu
->s
;
3092 v9fs_string_init(&name
);
3093 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3097 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3099 file_fidp
= get_fid(pdu
, fid
);
3100 if (file_fidp
== NULL
) {
3104 /* Make the file fid point to xattr */
3105 xattr_fidp
= file_fidp
;
3106 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3107 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3108 xattr_fidp
->fs
.xattr
.len
= size
;
3109 xattr_fidp
->fs
.xattr
.flags
= flags
;
3110 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3111 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3112 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3114 put_fid(pdu
, file_fidp
);
3116 complete_pdu(s
, pdu
, err
);
3117 v9fs_string_free(&name
);
3120 static void v9fs_readlink(void *opaque
)
3122 V9fsPDU
*pdu
= opaque
;
3129 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3133 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3134 fidp
= get_fid(pdu
, fid
);
3140 v9fs_string_init(&target
);
3141 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3145 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3147 v9fs_string_free(&target
);
3151 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3152 v9fs_string_free(&target
);
3156 complete_pdu(pdu
->s
, pdu
, err
);
3159 static CoroutineEntry
*pdu_co_handlers
[] = {
3160 [P9_TREADDIR
] = v9fs_readdir
,
3161 [P9_TSTATFS
] = v9fs_statfs
,
3162 [P9_TGETATTR
] = v9fs_getattr
,
3163 [P9_TSETATTR
] = v9fs_setattr
,
3164 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3165 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3166 [P9_TMKNOD
] = v9fs_mknod
,
3167 [P9_TRENAME
] = v9fs_rename
,
3168 [P9_TLOCK
] = v9fs_lock
,
3169 [P9_TGETLOCK
] = v9fs_getlock
,
3170 [P9_TRENAMEAT
] = v9fs_renameat
,
3171 [P9_TREADLINK
] = v9fs_readlink
,
3172 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3173 [P9_TMKDIR
] = v9fs_mkdir
,
3174 [P9_TVERSION
] = v9fs_version
,
3175 [P9_TLOPEN
] = v9fs_open
,
3176 [P9_TATTACH
] = v9fs_attach
,
3177 [P9_TSTAT
] = v9fs_stat
,
3178 [P9_TWALK
] = v9fs_walk
,
3179 [P9_TCLUNK
] = v9fs_clunk
,
3180 [P9_TFSYNC
] = v9fs_fsync
,
3181 [P9_TOPEN
] = v9fs_open
,
3182 [P9_TREAD
] = v9fs_read
,
3184 [P9_TAUTH
] = v9fs_auth
,
3186 [P9_TFLUSH
] = v9fs_flush
,
3187 [P9_TLINK
] = v9fs_link
,
3188 [P9_TSYMLINK
] = v9fs_symlink
,
3189 [P9_TCREATE
] = v9fs_create
,
3190 [P9_TLCREATE
] = v9fs_lcreate
,
3191 [P9_TWRITE
] = v9fs_write
,
3192 [P9_TWSTAT
] = v9fs_wstat
,
3193 [P9_TREMOVE
] = v9fs_remove
,
3196 static void v9fs_op_not_supp(void *opaque
)
3198 V9fsPDU
*pdu
= opaque
;
3199 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
3202 static void v9fs_fs_ro(void *opaque
)
3204 V9fsPDU
*pdu
= opaque
;
3205 complete_pdu(pdu
->s
, pdu
, -EROFS
);
3208 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3235 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3238 CoroutineEntry
*handler
;
3240 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3241 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3242 handler
= v9fs_op_not_supp
;
3244 handler
= pdu_co_handlers
[pdu
->id
];
3247 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3248 handler
= v9fs_fs_ro
;
3250 co
= qemu_coroutine_create(handler
);
3251 qemu_coroutine_enter(co
, pdu
);
3254 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3256 V9fsState
*s
= (V9fsState
*)vdev
;
3260 while ((pdu
= alloc_pdu(s
)) &&
3261 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3264 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3265 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3267 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3269 pdu
->size
= le32_to_cpu(*(uint32_t *)ptr
);
3271 pdu
->tag
= le16_to_cpu(*(uint16_t *)(ptr
+ 5));
3272 qemu_co_queue_init(&pdu
->complete
);
3278 static void __attribute__((__constructor__
)) virtio_9p_set_fd_limit(void)
3281 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3282 fprintf(stderr
, "Failed to get the resource limit\n");
3285 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3286 open_fd_rc
= rlim
.rlim_cur
/2;