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/error-report.h"
18 #include "qemu/sockets.h"
19 #include "virtio-9p.h"
20 #include "fsdev/qemu-fsdev.h"
24 #include "migration/migration.h"
28 static int open_fd_rc
;
42 ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
48 ret
= virtio_pdu_vmarshal(pdu
, offset
, fmt
, ap
);
54 ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
60 ret
= virtio_pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
66 static void pdu_push_and_notify(V9fsPDU
*pdu
)
68 virtio_9p_push_and_notify(pdu
);
71 static int omode_to_uflags(int8_t mode
)
105 struct dotl_openflag_map
{
110 static int dotl_to_open_flags(int flags
)
114 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
115 * and P9_DOTL_NOACCESS
117 int oflags
= flags
& O_ACCMODE
;
119 struct dotl_openflag_map dotl_oflag_map
[] = {
120 { P9_DOTL_CREATE
, O_CREAT
},
121 { P9_DOTL_EXCL
, O_EXCL
},
122 { P9_DOTL_NOCTTY
, O_NOCTTY
},
123 { P9_DOTL_TRUNC
, O_TRUNC
},
124 { P9_DOTL_APPEND
, O_APPEND
},
125 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
126 { P9_DOTL_DSYNC
, O_DSYNC
},
127 { P9_DOTL_FASYNC
, FASYNC
},
128 { P9_DOTL_DIRECT
, O_DIRECT
},
129 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
130 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
131 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
132 { P9_DOTL_NOATIME
, O_NOATIME
},
133 { P9_DOTL_SYNC
, O_SYNC
},
136 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
137 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
138 oflags
|= dotl_oflag_map
[i
].open_flag
;
145 void cred_init(FsCred
*credp
)
153 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
157 * Filter the client open flags
159 flags
= dotl_to_open_flags(oflags
);
160 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
162 * Ignore direct disk access hint until the server supports it.
168 void v9fs_path_init(V9fsPath
*path
)
174 void v9fs_path_free(V9fsPath
*path
)
181 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
184 lhs
->data
= g_malloc(rhs
->size
);
185 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
186 lhs
->size
= rhs
->size
;
189 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
190 const char *name
, V9fsPath
*path
)
193 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
201 * Return TRUE if s1 is an ancestor of s2.
203 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
204 * As a special case, We treat s1 as ancestor of s2 if they are same!
206 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
208 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
209 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
216 static size_t v9fs_string_size(V9fsString
*str
)
222 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
223 static int v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
226 if (f
->fid_type
== P9_FID_FILE
) {
227 if (f
->fs
.fd
== -1) {
229 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
230 } while (err
== -EINTR
&& !pdu
->cancelled
);
232 } else if (f
->fid_type
== P9_FID_DIR
) {
233 if (f
->fs
.dir
== NULL
) {
235 err
= v9fs_co_opendir(pdu
, f
);
236 } while (err
== -EINTR
&& !pdu
->cancelled
);
242 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
246 V9fsState
*s
= pdu
->s
;
248 for (f
= s
->fid_list
; f
; f
= f
->next
) {
252 * Update the fid ref upfront so that
253 * we don't get reclaimed when we yield
258 * check whether we need to reopen the
259 * file. We might have closed the fd
260 * while trying to free up some file
263 err
= v9fs_reopen_fid(pdu
, f
);
269 * Mark the fid as referenced so that the LRU
270 * reclaim won't close the file descriptor
272 f
->flags
|= FID_REFERENCED
;
279 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
283 for (f
= s
->fid_list
; f
; f
= f
->next
) {
284 /* If fid is already there return NULL */
290 f
= g_malloc0(sizeof(V9fsFidState
));
292 f
->fid_type
= P9_FID_NONE
;
295 * Mark the fid as referenced so that the LRU
296 * reclaim won't close the file descriptor
298 f
->flags
|= FID_REFERENCED
;
299 f
->next
= s
->fid_list
;
305 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
309 if (fidp
->fs
.xattr
.copied_len
== -1) {
310 /* getxattr/listxattr fid */
314 * if this is fid for setxattr. clunk should
315 * result in setxattr localcall
317 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
318 /* clunk after partial write */
322 if (fidp
->fs
.xattr
.len
) {
323 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
324 fidp
->fs
.xattr
.value
,
326 fidp
->fs
.xattr
.flags
);
328 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
331 v9fs_string_free(&fidp
->fs
.xattr
.name
);
333 g_free(fidp
->fs
.xattr
.value
);
337 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
341 if (fidp
->fid_type
== P9_FID_FILE
) {
342 /* If we reclaimed the fd no need to close */
343 if (fidp
->fs
.fd
!= -1) {
344 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
346 } else if (fidp
->fid_type
== P9_FID_DIR
) {
347 if (fidp
->fs
.dir
!= NULL
) {
348 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
350 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
351 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
353 v9fs_path_free(&fidp
->path
);
358 static int put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
363 * Don't free the fid if it is in reclaim list
365 if (!fidp
->ref
&& fidp
->clunked
) {
366 if (fidp
->fid
== pdu
->s
->root_fid
) {
368 * if the clunked fid is root fid then we
369 * have unmounted the fs on the client side.
370 * delete the migration blocker. Ideally, this
371 * should be hooked to transport close notification
373 if (pdu
->s
->migration_blocker
) {
374 migrate_del_blocker(pdu
->s
->migration_blocker
);
375 error_free(pdu
->s
->migration_blocker
);
376 pdu
->s
->migration_blocker
= NULL
;
379 return free_fid(pdu
, fidp
);
384 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
386 V9fsFidState
**fidpp
, *fidp
;
388 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
389 if ((*fidpp
)->fid
== fid
) {
393 if (*fidpp
== NULL
) {
402 void v9fs_reclaim_fd(V9fsPDU
*pdu
)
404 int reclaim_count
= 0;
405 V9fsState
*s
= pdu
->s
;
406 V9fsFidState
*f
, *reclaim_list
= NULL
;
408 for (f
= s
->fid_list
; f
; f
= f
->next
) {
410 * Unlink fids cannot be reclaimed. Check
411 * for them and skip them. Also skip fids
412 * currently being operated on.
414 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
418 * if it is a recently referenced fid
419 * we leave the fid untouched and clear the
420 * reference bit. We come back to it later
421 * in the next iteration. (a simple LRU without
422 * moving list elements around)
424 if (f
->flags
& FID_REFERENCED
) {
425 f
->flags
&= ~FID_REFERENCED
;
429 * Add fids to reclaim list.
431 if (f
->fid_type
== P9_FID_FILE
) {
432 if (f
->fs
.fd
!= -1) {
434 * Up the reference count so that
435 * a clunk request won't free this fid
438 f
->rclm_lst
= reclaim_list
;
440 f
->fs_reclaim
.fd
= f
->fs
.fd
;
444 } else if (f
->fid_type
== P9_FID_DIR
) {
445 if (f
->fs
.dir
!= NULL
) {
447 * Up the reference count so that
448 * a clunk request won't free this fid
451 f
->rclm_lst
= reclaim_list
;
453 f
->fs_reclaim
.dir
= f
->fs
.dir
;
458 if (reclaim_count
>= open_fd_rc
) {
463 * Now close the fid in reclaim list. Free them if they
464 * are already clunked.
466 while (reclaim_list
) {
468 reclaim_list
= f
->rclm_lst
;
469 if (f
->fid_type
== P9_FID_FILE
) {
470 v9fs_co_close(pdu
, &f
->fs_reclaim
);
471 } else if (f
->fid_type
== P9_FID_DIR
) {
472 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
476 * Now drop the fid reference, free it
483 static int v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
486 V9fsState
*s
= pdu
->s
;
487 V9fsFidState
*fidp
, head_fid
;
489 head_fid
.next
= s
->fid_list
;
490 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
491 if (fidp
->path
.size
!= path
->size
) {
494 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
495 /* Mark the fid non reclaimable. */
496 fidp
->flags
|= FID_NON_RECLAIMABLE
;
498 /* reopen the file/dir if already closed */
499 err
= v9fs_reopen_fid(pdu
, fidp
);
504 * Go back to head of fid list because
505 * the list could have got updated when
506 * switched to the worker thread
516 static void virtfs_reset(V9fsPDU
*pdu
)
518 V9fsState
*s
= pdu
->s
;
519 V9fsFidState
*fidp
= NULL
;
522 while (s
->fid_list
) {
524 s
->fid_list
= fidp
->next
;
533 /* One or more unclunked fids found... */
534 error_report("9pfs:%s: One or more uncluncked fids "
535 "found during reset", __func__
);
539 #define P9_QID_TYPE_DIR 0x80
540 #define P9_QID_TYPE_SYMLINK 0x02
542 #define P9_STAT_MODE_DIR 0x80000000
543 #define P9_STAT_MODE_APPEND 0x40000000
544 #define P9_STAT_MODE_EXCL 0x20000000
545 #define P9_STAT_MODE_MOUNT 0x10000000
546 #define P9_STAT_MODE_AUTH 0x08000000
547 #define P9_STAT_MODE_TMP 0x04000000
548 #define P9_STAT_MODE_SYMLINK 0x02000000
549 #define P9_STAT_MODE_LINK 0x01000000
550 #define P9_STAT_MODE_DEVICE 0x00800000
551 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
552 #define P9_STAT_MODE_SOCKET 0x00100000
553 #define P9_STAT_MODE_SETUID 0x00080000
554 #define P9_STAT_MODE_SETGID 0x00040000
555 #define P9_STAT_MODE_SETVTX 0x00010000
557 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
558 P9_STAT_MODE_SYMLINK | \
559 P9_STAT_MODE_LINK | \
560 P9_STAT_MODE_DEVICE | \
561 P9_STAT_MODE_NAMED_PIPE | \
564 /* This is the algorithm from ufs in spfs */
565 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
569 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
570 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
571 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
572 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
574 if (S_ISDIR(stbuf
->st_mode
)) {
575 qidp
->type
|= P9_QID_TYPE_DIR
;
577 if (S_ISLNK(stbuf
->st_mode
)) {
578 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
582 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
587 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
591 stat_to_qid(&stbuf
, qidp
);
595 V9fsPDU
*pdu_alloc(V9fsState
*s
)
599 if (!QLIST_EMPTY(&s
->free_list
)) {
600 pdu
= QLIST_FIRST(&s
->free_list
);
601 QLIST_REMOVE(pdu
, next
);
602 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
607 void pdu_free(V9fsPDU
*pdu
)
610 V9fsState
*s
= pdu
->s
;
612 * Cancelled pdu are added back to the freelist
615 if (!pdu
->cancelled
) {
616 QLIST_REMOVE(pdu
, next
);
617 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
623 * We don't do error checking for pdu_marshal/unmarshal here
624 * because we always expect to have enough space to encode
627 static void pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
629 int8_t id
= pdu
->id
+ 1; /* Response */
630 V9fsState
*s
= pdu
->s
;
636 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
639 str
.data
= strerror(err
);
640 str
.size
= strlen(str
.data
);
642 len
+= pdu_marshal(pdu
, len
, "s", &str
);
646 len
+= pdu_marshal(pdu
, len
, "d", err
);
648 if (s
->proto_version
== V9FS_PROTO_2000L
) {
651 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
654 /* fill out the header */
655 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
657 /* keep these in sync */
661 pdu_push_and_notify(pdu
);
663 /* Now wakeup anybody waiting in flush for this request */
664 qemu_co_queue_next(&pdu
->complete
);
669 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
674 if (mode
& P9_STAT_MODE_DIR
) {
678 if (mode
& P9_STAT_MODE_SYMLINK
) {
681 if (mode
& P9_STAT_MODE_SOCKET
) {
684 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
687 if (mode
& P9_STAT_MODE_DEVICE
) {
688 if (extension
->size
&& extension
->data
[0] == 'c') {
699 if (mode
& P9_STAT_MODE_SETUID
) {
702 if (mode
& P9_STAT_MODE_SETGID
) {
705 if (mode
& P9_STAT_MODE_SETVTX
) {
712 static int donttouch_stat(V9fsStat
*stat
)
714 if (stat
->type
== -1 &&
716 stat
->qid
.type
== -1 &&
717 stat
->qid
.version
== -1 &&
718 stat
->qid
.path
== -1 &&
722 stat
->length
== -1 &&
729 stat
->n_muid
== -1) {
736 static void v9fs_stat_init(V9fsStat
*stat
)
738 v9fs_string_init(&stat
->name
);
739 v9fs_string_init(&stat
->uid
);
740 v9fs_string_init(&stat
->gid
);
741 v9fs_string_init(&stat
->muid
);
742 v9fs_string_init(&stat
->extension
);
745 static void v9fs_stat_free(V9fsStat
*stat
)
747 v9fs_string_free(&stat
->name
);
748 v9fs_string_free(&stat
->uid
);
749 v9fs_string_free(&stat
->gid
);
750 v9fs_string_free(&stat
->muid
);
751 v9fs_string_free(&stat
->extension
);
754 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
758 mode
= stbuf
->st_mode
& 0777;
759 if (S_ISDIR(stbuf
->st_mode
)) {
760 mode
|= P9_STAT_MODE_DIR
;
763 if (S_ISLNK(stbuf
->st_mode
)) {
764 mode
|= P9_STAT_MODE_SYMLINK
;
767 if (S_ISSOCK(stbuf
->st_mode
)) {
768 mode
|= P9_STAT_MODE_SOCKET
;
771 if (S_ISFIFO(stbuf
->st_mode
)) {
772 mode
|= P9_STAT_MODE_NAMED_PIPE
;
775 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
776 mode
|= P9_STAT_MODE_DEVICE
;
779 if (stbuf
->st_mode
& S_ISUID
) {
780 mode
|= P9_STAT_MODE_SETUID
;
783 if (stbuf
->st_mode
& S_ISGID
) {
784 mode
|= P9_STAT_MODE_SETGID
;
787 if (stbuf
->st_mode
& S_ISVTX
) {
788 mode
|= P9_STAT_MODE_SETVTX
;
794 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
795 const struct stat
*stbuf
,
801 memset(v9stat
, 0, sizeof(*v9stat
));
803 stat_to_qid(stbuf
, &v9stat
->qid
);
804 v9stat
->mode
= stat_to_v9mode(stbuf
);
805 v9stat
->atime
= stbuf
->st_atime
;
806 v9stat
->mtime
= stbuf
->st_mtime
;
807 v9stat
->length
= stbuf
->st_size
;
809 v9fs_string_null(&v9stat
->uid
);
810 v9fs_string_null(&v9stat
->gid
);
811 v9fs_string_null(&v9stat
->muid
);
813 v9stat
->n_uid
= stbuf
->st_uid
;
814 v9stat
->n_gid
= stbuf
->st_gid
;
817 v9fs_string_null(&v9stat
->extension
);
819 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
820 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
824 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
825 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
826 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
827 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
828 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
829 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
830 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
833 str
= strrchr(name
->data
, '/');
840 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
843 v9fs_string_size(&v9stat
->name
) +
844 v9fs_string_size(&v9stat
->uid
) +
845 v9fs_string_size(&v9stat
->gid
) +
846 v9fs_string_size(&v9stat
->muid
) +
847 v9fs_string_size(&v9stat
->extension
);
851 #define P9_STATS_MODE 0x00000001ULL
852 #define P9_STATS_NLINK 0x00000002ULL
853 #define P9_STATS_UID 0x00000004ULL
854 #define P9_STATS_GID 0x00000008ULL
855 #define P9_STATS_RDEV 0x00000010ULL
856 #define P9_STATS_ATIME 0x00000020ULL
857 #define P9_STATS_MTIME 0x00000040ULL
858 #define P9_STATS_CTIME 0x00000080ULL
859 #define P9_STATS_INO 0x00000100ULL
860 #define P9_STATS_SIZE 0x00000200ULL
861 #define P9_STATS_BLOCKS 0x00000400ULL
863 #define P9_STATS_BTIME 0x00000800ULL
864 #define P9_STATS_GEN 0x00001000ULL
865 #define P9_STATS_DATA_VERSION 0x00002000ULL
867 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
868 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
871 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
872 V9fsStatDotl
*v9lstat
)
874 memset(v9lstat
, 0, sizeof(*v9lstat
));
876 v9lstat
->st_mode
= stbuf
->st_mode
;
877 v9lstat
->st_nlink
= stbuf
->st_nlink
;
878 v9lstat
->st_uid
= stbuf
->st_uid
;
879 v9lstat
->st_gid
= stbuf
->st_gid
;
880 v9lstat
->st_rdev
= stbuf
->st_rdev
;
881 v9lstat
->st_size
= stbuf
->st_size
;
882 v9lstat
->st_blksize
= stbuf
->st_blksize
;
883 v9lstat
->st_blocks
= stbuf
->st_blocks
;
884 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
885 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
886 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
887 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
888 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
889 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
890 /* Currently we only support BASIC fields in stat */
891 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
893 stat_to_qid(stbuf
, &v9lstat
->qid
);
896 static void print_sg(struct iovec
*sg
, int cnt
)
900 printf("sg[%d]: {", cnt
);
901 for (i
= 0; i
< cnt
; i
++) {
905 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
910 /* Will call this only for path name based fid */
911 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
914 v9fs_path_init(&str
);
915 v9fs_path_copy(&str
, dst
);
916 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
917 v9fs_path_free(&str
);
918 /* +1 to include terminating NULL */
922 static inline bool is_ro_export(FsContext
*ctx
)
924 return ctx
->export_flags
& V9FS_RDONLY
;
927 static void v9fs_version(void *opaque
)
930 V9fsPDU
*pdu
= opaque
;
931 V9fsState
*s
= pdu
->s
;
935 v9fs_string_init(&version
);
936 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
941 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
945 if (!strcmp(version
.data
, "9P2000.u")) {
946 s
->proto_version
= V9FS_PROTO_2000U
;
947 } else if (!strcmp(version
.data
, "9P2000.L")) {
948 s
->proto_version
= V9FS_PROTO_2000L
;
950 v9fs_string_sprintf(&version
, "unknown");
953 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
959 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
961 pdu_complete(pdu
, offset
);
962 v9fs_string_free(&version
);
965 static void v9fs_attach(void *opaque
)
967 V9fsPDU
*pdu
= opaque
;
968 V9fsState
*s
= pdu
->s
;
969 int32_t fid
, afid
, n_uname
;
970 V9fsString uname
, aname
;
976 v9fs_string_init(&uname
);
977 v9fs_string_init(&aname
);
978 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
979 &afid
, &uname
, &aname
, &n_uname
);
983 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
985 fidp
= alloc_fid(s
, fid
);
991 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
997 err
= fid_to_qid(pdu
, fidp
, &qid
);
1003 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1009 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1010 qid
.type
, qid
.version
, qid
.path
);
1012 * disable migration if we haven't done already.
1013 * attach could get called multiple times for the same export.
1015 if (!s
->migration_blocker
) {
1017 error_setg(&s
->migration_blocker
,
1018 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1019 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1020 migrate_add_blocker(s
->migration_blocker
);
1025 pdu_complete(pdu
, err
);
1026 v9fs_string_free(&uname
);
1027 v9fs_string_free(&aname
);
1030 static void v9fs_stat(void *opaque
)
1038 V9fsPDU
*pdu
= opaque
;
1040 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1044 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1046 fidp
= get_fid(pdu
, fid
);
1051 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1055 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1059 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1061 v9fs_stat_free(&v9stat
);
1064 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1065 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1067 v9fs_stat_free(&v9stat
);
1071 pdu_complete(pdu
, err
);
1074 static void v9fs_getattr(void *opaque
)
1081 uint64_t request_mask
;
1082 V9fsStatDotl v9stat_dotl
;
1083 V9fsPDU
*pdu
= opaque
;
1084 V9fsState
*s
= pdu
->s
;
1086 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1090 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1092 fidp
= get_fid(pdu
, fid
);
1098 * Currently we only support BASIC fields in stat, so there is no
1099 * need to look at request_mask.
1101 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1105 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1107 /* fill st_gen if requested and supported by underlying fs */
1108 if (request_mask
& P9_STATS_GEN
) {
1109 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1112 /* we have valid st_gen: update result mask */
1113 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1116 /* request cancelled, e.g. by Tflush */
1119 /* failed to get st_gen: not fatal, ignore */
1123 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1128 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1129 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1130 v9stat_dotl
.st_gid
);
1134 pdu_complete(pdu
, retval
);
1137 /* Attribute flags */
1138 #define P9_ATTR_MODE (1 << 0)
1139 #define P9_ATTR_UID (1 << 1)
1140 #define P9_ATTR_GID (1 << 2)
1141 #define P9_ATTR_SIZE (1 << 3)
1142 #define P9_ATTR_ATIME (1 << 4)
1143 #define P9_ATTR_MTIME (1 << 5)
1144 #define P9_ATTR_CTIME (1 << 6)
1145 #define P9_ATTR_ATIME_SET (1 << 7)
1146 #define P9_ATTR_MTIME_SET (1 << 8)
1148 #define P9_ATTR_MASK 127
1150 static void v9fs_setattr(void *opaque
)
1157 V9fsPDU
*pdu
= opaque
;
1159 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1164 fidp
= get_fid(pdu
, fid
);
1169 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1170 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1175 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1176 struct timespec times
[2];
1177 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1178 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1179 times
[0].tv_sec
= v9iattr
.atime_sec
;
1180 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1182 times
[0].tv_nsec
= UTIME_NOW
;
1185 times
[0].tv_nsec
= UTIME_OMIT
;
1187 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1188 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1189 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1190 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1192 times
[1].tv_nsec
= UTIME_NOW
;
1195 times
[1].tv_nsec
= UTIME_OMIT
;
1197 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1203 * If the only valid entry in iattr is ctime we can call
1204 * chown(-1,-1) to update the ctime of the file
1206 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1207 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1208 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1209 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1212 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1215 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1221 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1222 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1231 pdu_complete(pdu
, err
);
1234 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1240 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1245 for (i
= 0; i
< nwnames
; i
++) {
1246 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1255 static void v9fs_walk(void *opaque
)
1258 V9fsQID
*qids
= NULL
;
1260 V9fsPath dpath
, path
;
1264 int32_t fid
, newfid
;
1265 V9fsString
*wnames
= NULL
;
1267 V9fsFidState
*newfidp
= NULL
;
1268 V9fsPDU
*pdu
= opaque
;
1269 V9fsState
*s
= pdu
->s
;
1271 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1273 pdu_complete(pdu
, err
);
1278 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1280 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1281 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1282 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1283 for (i
= 0; i
< nwnames
; i
++) {
1284 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1290 } else if (nwnames
> P9_MAXWELEM
) {
1294 fidp
= get_fid(pdu
, fid
);
1299 v9fs_path_init(&dpath
);
1300 v9fs_path_init(&path
);
1302 * Both dpath and path initially poin to fidp.
1303 * Needed to handle request with nwnames == 0
1305 v9fs_path_copy(&dpath
, &fidp
->path
);
1306 v9fs_path_copy(&path
, &fidp
->path
);
1307 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1308 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1312 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1316 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1317 v9fs_path_copy(&dpath
, &path
);
1319 if (fid
== newfid
) {
1320 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1321 v9fs_path_copy(&fidp
->path
, &path
);
1323 newfidp
= alloc_fid(s
, newfid
);
1324 if (newfidp
== NULL
) {
1328 newfidp
->uid
= fidp
->uid
;
1329 v9fs_path_copy(&newfidp
->path
, &path
);
1331 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1332 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1336 put_fid(pdu
, newfidp
);
1338 v9fs_path_free(&dpath
);
1339 v9fs_path_free(&path
);
1341 pdu_complete(pdu
, err
);
1342 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1343 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1344 v9fs_string_free(&wnames
[name_idx
]);
1351 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1353 struct statfs stbuf
;
1355 V9fsState
*s
= pdu
->s
;
1358 * iounit should be multiples of f_bsize (host filesystem block size
1359 * and as well as less than (client msize - P9_IOHDRSZ))
1361 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1362 iounit
= stbuf
.f_bsize
;
1363 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1366 iounit
= s
->msize
- P9_IOHDRSZ
;
1371 static void v9fs_open(void *opaque
)
1382 V9fsPDU
*pdu
= opaque
;
1383 V9fsState
*s
= pdu
->s
;
1385 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1386 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1389 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1395 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1397 fidp
= get_fid(pdu
, fid
);
1402 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1404 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1408 stat_to_qid(&stbuf
, &qid
);
1409 if (S_ISDIR(stbuf
.st_mode
)) {
1410 err
= v9fs_co_opendir(pdu
, fidp
);
1414 fidp
->fid_type
= P9_FID_DIR
;
1415 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1421 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1422 flags
= get_dotl_openflags(s
, mode
);
1424 flags
= omode_to_uflags(mode
);
1426 if (is_ro_export(&s
->ctx
)) {
1427 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1428 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1433 err
= v9fs_co_open(pdu
, fidp
, flags
);
1437 fidp
->fid_type
= P9_FID_FILE
;
1438 fidp
->open_flags
= flags
;
1439 if (flags
& O_EXCL
) {
1441 * We let the host file system do O_EXCL check
1442 * We should not reclaim such fd
1444 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1446 iounit
= get_iounit(pdu
, &fidp
->path
);
1447 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1453 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1454 qid
.type
, qid
.version
, qid
.path
, iounit
);
1458 pdu_complete(pdu
, err
);
1461 static void v9fs_lcreate(void *opaque
)
1463 int32_t dfid
, flags
, mode
;
1472 V9fsPDU
*pdu
= opaque
;
1474 v9fs_string_init(&name
);
1475 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1476 &name
, &flags
, &mode
, &gid
);
1480 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1482 fidp
= get_fid(pdu
, dfid
);
1488 flags
= get_dotl_openflags(pdu
->s
, flags
);
1489 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1490 flags
| O_CREAT
, mode
, &stbuf
);
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 stat_to_qid(&stbuf
, &qid
);
1505 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1510 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1511 qid
.type
, qid
.version
, qid
.path
, iounit
);
1515 pdu_complete(pdu
, err
);
1516 v9fs_string_free(&name
);
1519 static void v9fs_fsync(void *opaque
)
1526 V9fsPDU
*pdu
= opaque
;
1528 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1532 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1534 fidp
= get_fid(pdu
, fid
);
1539 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1545 pdu_complete(pdu
, err
);
1548 static void v9fs_clunk(void *opaque
)
1554 V9fsPDU
*pdu
= opaque
;
1555 V9fsState
*s
= pdu
->s
;
1557 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1561 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1563 fidp
= clunk_fid(s
, fid
);
1569 * Bump the ref so that put_fid will
1573 err
= put_fid(pdu
, fidp
);
1578 pdu_complete(pdu
, err
);
1581 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1582 uint64_t off
, uint32_t max_count
)
1588 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
1589 VirtQueueElement
*elem
= &v
->elems
[pdu
->idx
];
1591 xattr_len
= fidp
->fs
.xattr
.len
;
1592 read_count
= xattr_len
- off
;
1593 if (read_count
> max_count
) {
1594 read_count
= max_count
;
1595 } else if (read_count
< 0) {
1597 * read beyond XATTR value
1601 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1607 err
= v9fs_pack(elem
->in_sg
, elem
->in_num
, offset
,
1608 ((char *)fidp
->fs
.xattr
.value
) + off
,
1617 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1618 V9fsFidState
*fidp
, uint32_t max_count
)
1625 off_t saved_dir_pos
;
1626 struct dirent
*dent
, *result
;
1628 /* save the directory position */
1629 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1630 if (saved_dir_pos
< 0) {
1631 return saved_dir_pos
;
1634 dent
= g_malloc(sizeof(struct dirent
));
1637 v9fs_path_init(&path
);
1638 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1639 if (err
|| !result
) {
1642 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1646 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1650 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1654 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1655 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1656 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1657 /* Ran out of buffer. Set dir back to old position and return */
1658 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1659 v9fs_stat_free(&v9stat
);
1660 v9fs_path_free(&path
);
1665 v9fs_stat_free(&v9stat
);
1666 v9fs_path_free(&path
);
1667 saved_dir_pos
= dent
->d_off
;
1671 v9fs_path_free(&path
);
1679 * Create a QEMUIOVector for a sub-region of PDU iovecs
1681 * @qiov: uninitialized QEMUIOVector
1682 * @skip: number of bytes to skip from beginning of PDU
1683 * @size: number of bytes to include
1684 * @is_write: true - write, false - read
1686 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1687 * with qemu_iovec_destroy().
1689 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1690 size_t skip
, size_t size
,
1697 virtio_init_iov_from_pdu(pdu
, &iov
, &niov
, is_write
);
1699 qemu_iovec_init_external(&elem
, iov
, niov
);
1700 qemu_iovec_init(qiov
, niov
);
1701 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1704 static void v9fs_read(void *opaque
)
1713 V9fsPDU
*pdu
= opaque
;
1714 V9fsState
*s
= pdu
->s
;
1716 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1720 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1722 fidp
= get_fid(pdu
, fid
);
1727 if (fidp
->fid_type
== P9_FID_DIR
) {
1730 v9fs_co_rewinddir(pdu
, fidp
);
1732 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1737 err
= pdu_marshal(pdu
, offset
, "d", count
);
1741 err
+= offset
+ count
;
1742 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1743 QEMUIOVector qiov_full
;
1747 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1748 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1750 qemu_iovec_reset(&qiov
);
1751 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1753 print_sg(qiov
.iov
, qiov
.niov
);
1755 /* Loop in case of EINTR */
1757 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1762 } while (len
== -EINTR
&& !pdu
->cancelled
);
1764 /* IO error return the error */
1768 } while (count
< max_count
&& len
> 0);
1769 err
= pdu_marshal(pdu
, offset
, "d", count
);
1773 err
+= offset
+ count
;
1774 qemu_iovec_destroy(&qiov
);
1775 qemu_iovec_destroy(&qiov_full
);
1776 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1777 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1781 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1785 pdu_complete(pdu
, err
);
1788 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1791 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1792 * size of type (1) + size of name.size (2) + strlen(name.data)
1794 return 24 + v9fs_string_size(name
);
1797 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1798 V9fsFidState
*fidp
, int32_t max_count
)
1805 off_t saved_dir_pos
;
1806 struct dirent
*dent
, *result
;
1808 /* save the directory position */
1809 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1810 if (saved_dir_pos
< 0) {
1811 return saved_dir_pos
;
1814 dent
= g_malloc(sizeof(struct dirent
));
1817 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1818 if (err
|| !result
) {
1821 v9fs_string_init(&name
);
1822 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1823 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1824 /* Ran out of buffer. Set dir back to old position and return */
1825 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1826 v9fs_string_free(&name
);
1831 * Fill up just the path field of qid because the client uses
1832 * only that. To fill the entire qid structure we will have
1833 * to stat each dirent found, which is expensive
1835 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1836 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1837 /* Fill the other fields with dummy values */
1841 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1842 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1844 dent
->d_type
, &name
);
1846 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1847 v9fs_string_free(&name
);
1852 v9fs_string_free(&name
);
1853 saved_dir_pos
= dent
->d_off
;
1862 static void v9fs_readdir(void *opaque
)
1868 uint64_t initial_offset
;
1871 V9fsPDU
*pdu
= opaque
;
1873 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1874 &initial_offset
, &max_count
);
1878 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1880 fidp
= get_fid(pdu
, fid
);
1885 if (!fidp
->fs
.dir
) {
1889 if (initial_offset
== 0) {
1890 v9fs_co_rewinddir(pdu
, fidp
);
1892 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1894 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1899 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1903 retval
+= count
+ offset
;
1904 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1908 pdu_complete(pdu
, retval
);
1911 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1912 uint64_t off
, uint32_t count
,
1913 struct iovec
*sg
, int cnt
)
1922 xattr_len
= fidp
->fs
.xattr
.len
;
1923 write_count
= xattr_len
- off
;
1924 if (write_count
> count
) {
1925 write_count
= count
;
1926 } else if (write_count
< 0) {
1928 * write beyond XATTR value len specified in
1934 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1939 fidp
->fs
.xattr
.copied_len
+= write_count
;
1941 * Now copy the content from sg list
1943 for (i
= 0; i
< cnt
; i
++) {
1944 if (write_count
> sg
[i
].iov_len
) {
1945 to_copy
= sg
[i
].iov_len
;
1947 to_copy
= write_count
;
1949 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1950 /* updating vs->off since we are not using below */
1952 write_count
-= to_copy
;
1958 static void v9fs_write(void *opaque
)
1968 V9fsPDU
*pdu
= opaque
;
1969 V9fsState
*s
= pdu
->s
;
1970 QEMUIOVector qiov_full
;
1973 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
1975 pdu_complete(pdu
, err
);
1979 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
1980 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
1982 fidp
= get_fid(pdu
, fid
);
1987 if (fidp
->fid_type
== P9_FID_FILE
) {
1988 if (fidp
->fs
.fd
== -1) {
1992 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1994 * setxattr operation
1996 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
1997 qiov_full
.iov
, qiov_full
.niov
);
2003 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2005 qemu_iovec_reset(&qiov
);
2006 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2008 print_sg(qiov
.iov
, qiov
.niov
);
2010 /* Loop in case of EINTR */
2012 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2017 } while (len
== -EINTR
&& !pdu
->cancelled
);
2019 /* IO error return the error */
2023 } while (total
< count
&& len
> 0);
2026 err
= pdu_marshal(pdu
, offset
, "d", total
);
2031 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2033 qemu_iovec_destroy(&qiov
);
2037 qemu_iovec_destroy(&qiov_full
);
2038 pdu_complete(pdu
, err
);
2041 static void v9fs_create(void *opaque
)
2053 V9fsString extension
;
2055 V9fsPDU
*pdu
= opaque
;
2057 v9fs_path_init(&path
);
2058 v9fs_string_init(&name
);
2059 v9fs_string_init(&extension
);
2060 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2061 &perm
, &mode
, &extension
);
2065 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2067 fidp
= get_fid(pdu
, fid
);
2072 if (perm
& P9_STAT_MODE_DIR
) {
2073 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2074 fidp
->uid
, -1, &stbuf
);
2078 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2082 v9fs_path_copy(&fidp
->path
, &path
);
2083 err
= v9fs_co_opendir(pdu
, fidp
);
2087 fidp
->fid_type
= P9_FID_DIR
;
2088 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2089 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2090 extension
.data
, -1 , &stbuf
);
2094 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2098 v9fs_path_copy(&fidp
->path
, &path
);
2099 } else if (perm
& P9_STAT_MODE_LINK
) {
2100 int32_t ofid
= atoi(extension
.data
);
2101 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2102 if (ofidp
== NULL
) {
2106 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2107 put_fid(pdu
, ofidp
);
2111 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2113 fidp
->fid_type
= P9_FID_NONE
;
2116 v9fs_path_copy(&fidp
->path
, &path
);
2117 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2119 fidp
->fid_type
= P9_FID_NONE
;
2122 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2124 uint32_t major
, minor
;
2127 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2144 nmode
|= perm
& 0777;
2145 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2146 makedev(major
, minor
), nmode
, &stbuf
);
2150 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2154 v9fs_path_copy(&fidp
->path
, &path
);
2155 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2156 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2157 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2161 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2165 v9fs_path_copy(&fidp
->path
, &path
);
2166 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2167 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2168 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2172 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2176 v9fs_path_copy(&fidp
->path
, &path
);
2178 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2179 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2183 fidp
->fid_type
= P9_FID_FILE
;
2184 fidp
->open_flags
= omode_to_uflags(mode
);
2185 if (fidp
->open_flags
& O_EXCL
) {
2187 * We let the host file system do O_EXCL check
2188 * We should not reclaim such fd
2190 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2193 iounit
= get_iounit(pdu
, &fidp
->path
);
2194 stat_to_qid(&stbuf
, &qid
);
2195 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2200 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2201 qid
.type
, qid
.version
, qid
.path
, iounit
);
2205 pdu_complete(pdu
, err
);
2206 v9fs_string_free(&name
);
2207 v9fs_string_free(&extension
);
2208 v9fs_path_free(&path
);
2211 static void v9fs_symlink(void *opaque
)
2213 V9fsPDU
*pdu
= opaque
;
2216 V9fsFidState
*dfidp
;
2224 v9fs_string_init(&name
);
2225 v9fs_string_init(&symname
);
2226 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2230 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2232 dfidp
= get_fid(pdu
, dfid
);
2233 if (dfidp
== NULL
) {
2237 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2241 stat_to_qid(&stbuf
, &qid
);
2242 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2247 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2248 qid
.type
, qid
.version
, qid
.path
);
2250 put_fid(pdu
, dfidp
);
2252 pdu_complete(pdu
, err
);
2253 v9fs_string_free(&name
);
2254 v9fs_string_free(&symname
);
2257 static void v9fs_flush(void *opaque
)
2262 V9fsPDU
*cancel_pdu
;
2263 V9fsPDU
*pdu
= opaque
;
2264 V9fsState
*s
= pdu
->s
;
2266 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2268 pdu_complete(pdu
, err
);
2271 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2273 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2274 if (cancel_pdu
->tag
== tag
) {
2279 cancel_pdu
->cancelled
= 1;
2281 * Wait for pdu to complete.
2283 qemu_co_queue_wait(&cancel_pdu
->complete
);
2284 cancel_pdu
->cancelled
= 0;
2285 pdu_free(cancel_pdu
);
2287 pdu_complete(pdu
, 7);
2290 static void v9fs_link(void *opaque
)
2292 V9fsPDU
*pdu
= opaque
;
2293 int32_t dfid
, oldfid
;
2294 V9fsFidState
*dfidp
, *oldfidp
;
2299 v9fs_string_init(&name
);
2300 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2304 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2306 dfidp
= get_fid(pdu
, dfid
);
2307 if (dfidp
== NULL
) {
2312 oldfidp
= get_fid(pdu
, oldfid
);
2313 if (oldfidp
== NULL
) {
2317 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2322 put_fid(pdu
, dfidp
);
2324 v9fs_string_free(&name
);
2325 pdu_complete(pdu
, err
);
2328 /* Only works with path name based fid */
2329 static void v9fs_remove(void *opaque
)
2335 V9fsPDU
*pdu
= opaque
;
2337 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2341 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2343 fidp
= get_fid(pdu
, fid
);
2348 /* if fs driver is not path based, return EOPNOTSUPP */
2349 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2354 * IF the file is unlinked, we cannot reopen
2355 * the file later. So don't reclaim fd
2357 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2361 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2366 /* For TREMOVE we need to clunk the fid even on failed remove */
2367 clunk_fid(pdu
->s
, fidp
->fid
);
2370 pdu_complete(pdu
, err
);
2373 static void v9fs_unlinkat(void *opaque
)
2377 int32_t dfid
, flags
;
2380 V9fsFidState
*dfidp
;
2381 V9fsPDU
*pdu
= opaque
;
2383 v9fs_string_init(&name
);
2384 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2388 dfidp
= get_fid(pdu
, dfid
);
2389 if (dfidp
== NULL
) {
2394 * IF the file is unlinked, we cannot reopen
2395 * the file later. So don't reclaim fd
2397 v9fs_path_init(&path
);
2398 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2402 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2406 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2411 put_fid(pdu
, dfidp
);
2412 v9fs_path_free(&path
);
2414 pdu_complete(pdu
, err
);
2415 v9fs_string_free(&name
);
2419 /* Only works with path name based fid */
2420 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2421 int32_t newdirfid
, V9fsString
*name
)
2426 V9fsFidState
*tfidp
;
2427 V9fsState
*s
= pdu
->s
;
2428 V9fsFidState
*dirfidp
= NULL
;
2429 char *old_name
, *new_name
;
2431 v9fs_path_init(&new_path
);
2432 if (newdirfid
!= -1) {
2433 dirfidp
= get_fid(pdu
, newdirfid
);
2434 if (dirfidp
== NULL
) {
2438 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2439 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2441 old_name
= fidp
->path
.data
;
2442 end
= strrchr(old_name
, '/');
2448 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2449 strncat(new_name
, old_name
, end
- old_name
);
2450 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2451 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2454 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2459 * Fixup fid's pointing to the old name to
2460 * start pointing to the new name
2462 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2463 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2464 /* replace the name */
2465 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2470 put_fid(pdu
, dirfidp
);
2472 v9fs_path_free(&new_path
);
2477 /* Only works with path name based fid */
2478 static void v9fs_rename(void *opaque
)
2486 V9fsPDU
*pdu
= opaque
;
2487 V9fsState
*s
= pdu
->s
;
2489 v9fs_string_init(&name
);
2490 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2494 fidp
= get_fid(pdu
, fid
);
2499 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2500 /* if fs driver is not path based, return EOPNOTSUPP */
2501 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2505 v9fs_path_write_lock(s
);
2506 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2507 v9fs_path_unlock(s
);
2514 pdu_complete(pdu
, err
);
2515 v9fs_string_free(&name
);
2518 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2519 V9fsString
*old_name
, V9fsPath
*newdir
,
2520 V9fsString
*new_name
)
2522 V9fsFidState
*tfidp
;
2523 V9fsPath oldpath
, newpath
;
2524 V9fsState
*s
= pdu
->s
;
2527 v9fs_path_init(&oldpath
);
2528 v9fs_path_init(&newpath
);
2529 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2530 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2533 * Fixup fid's pointing to the old name to
2534 * start pointing to the new name
2536 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2537 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2538 /* replace the name */
2539 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2542 v9fs_path_free(&oldpath
);
2543 v9fs_path_free(&newpath
);
2546 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2547 V9fsString
*old_name
, int32_t newdirfid
,
2548 V9fsString
*new_name
)
2551 V9fsState
*s
= pdu
->s
;
2552 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2554 olddirfidp
= get_fid(pdu
, olddirfid
);
2555 if (olddirfidp
== NULL
) {
2559 if (newdirfid
!= -1) {
2560 newdirfidp
= get_fid(pdu
, newdirfid
);
2561 if (newdirfidp
== NULL
) {
2566 newdirfidp
= get_fid(pdu
, olddirfid
);
2569 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2570 &newdirfidp
->path
, new_name
);
2574 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2575 /* Only for path based fid we need to do the below fixup */
2576 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2577 &newdirfidp
->path
, new_name
);
2581 put_fid(pdu
, olddirfidp
);
2584 put_fid(pdu
, newdirfidp
);
2589 static void v9fs_renameat(void *opaque
)
2593 V9fsPDU
*pdu
= opaque
;
2594 V9fsState
*s
= pdu
->s
;
2595 int32_t olddirfid
, newdirfid
;
2596 V9fsString old_name
, new_name
;
2598 v9fs_string_init(&old_name
);
2599 v9fs_string_init(&new_name
);
2600 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2601 &old_name
, &newdirfid
, &new_name
);
2606 v9fs_path_write_lock(s
);
2607 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2608 &old_name
, newdirfid
, &new_name
);
2609 v9fs_path_unlock(s
);
2615 pdu_complete(pdu
, err
);
2616 v9fs_string_free(&old_name
);
2617 v9fs_string_free(&new_name
);
2620 static void v9fs_wstat(void *opaque
)
2629 V9fsPDU
*pdu
= opaque
;
2631 v9fs_stat_init(&v9stat
);
2632 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2636 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2637 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2639 fidp
= get_fid(pdu
, fid
);
2644 /* do we need to sync the file? */
2645 if (donttouch_stat(&v9stat
)) {
2646 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2649 if (v9stat
.mode
!= -1) {
2651 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2655 v9_mode
= stat_to_v9mode(&stbuf
);
2656 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2657 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2658 /* Attempting to change the type */
2662 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2663 v9mode_to_mode(v9stat
.mode
,
2664 &v9stat
.extension
));
2669 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2670 struct timespec times
[2];
2671 if (v9stat
.atime
!= -1) {
2672 times
[0].tv_sec
= v9stat
.atime
;
2673 times
[0].tv_nsec
= 0;
2675 times
[0].tv_nsec
= UTIME_OMIT
;
2677 if (v9stat
.mtime
!= -1) {
2678 times
[1].tv_sec
= v9stat
.mtime
;
2679 times
[1].tv_nsec
= 0;
2681 times
[1].tv_nsec
= UTIME_OMIT
;
2683 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2688 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2689 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2694 if (v9stat
.name
.size
!= 0) {
2695 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2700 if (v9stat
.length
!= -1) {
2701 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2710 v9fs_stat_free(&v9stat
);
2711 pdu_complete(pdu
, err
);
2714 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2726 int32_t bsize_factor
;
2729 * compute bsize factor based on host file system block size
2732 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2733 if (!bsize_factor
) {
2736 f_type
= stbuf
->f_type
;
2737 f_bsize
= stbuf
->f_bsize
;
2738 f_bsize
*= bsize_factor
;
2740 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2741 * adjust(divide) the number of blocks, free blocks and available
2742 * blocks by bsize factor
2744 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2745 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2746 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2747 f_files
= stbuf
->f_files
;
2748 f_ffree
= stbuf
->f_ffree
;
2749 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2750 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2751 f_namelen
= stbuf
->f_namelen
;
2753 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2754 f_type
, f_bsize
, f_blocks
, f_bfree
,
2755 f_bavail
, f_files
, f_ffree
,
2756 fsid_val
, f_namelen
);
2759 static void v9fs_statfs(void *opaque
)
2765 struct statfs stbuf
;
2766 V9fsPDU
*pdu
= opaque
;
2767 V9fsState
*s
= pdu
->s
;
2769 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2773 fidp
= get_fid(pdu
, fid
);
2778 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2782 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2790 pdu_complete(pdu
, retval
);
2793 static void v9fs_mknod(void *opaque
)
2806 V9fsPDU
*pdu
= opaque
;
2808 v9fs_string_init(&name
);
2809 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2810 &major
, &minor
, &gid
);
2814 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2816 fidp
= get_fid(pdu
, fid
);
2821 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2822 makedev(major
, minor
), mode
, &stbuf
);
2826 stat_to_qid(&stbuf
, &qid
);
2827 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2832 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2833 qid
.type
, qid
.version
, qid
.path
);
2837 pdu_complete(pdu
, err
);
2838 v9fs_string_free(&name
);
2842 * Implement posix byte range locking code
2843 * Server side handling of locking code is very simple, because 9p server in
2844 * QEMU can handle only one client. And most of the lock handling
2845 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2846 * do any thing in * qemu 9p server side lock code path.
2847 * So when a TLOCK request comes, always return success
2849 static void v9fs_lock(void *opaque
)
2856 int32_t fid
, err
= 0;
2857 V9fsPDU
*pdu
= opaque
;
2859 status
= P9_LOCK_ERROR
;
2860 v9fs_string_init(&flock
.client_id
);
2861 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2862 &flock
.flags
, &flock
.start
, &flock
.length
,
2863 &flock
.proc_id
, &flock
.client_id
);
2867 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2868 flock
.type
, flock
.start
, flock
.length
);
2871 /* We support only block flag now (that too ignored currently) */
2872 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2876 fidp
= get_fid(pdu
, fid
);
2881 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2885 status
= P9_LOCK_SUCCESS
;
2889 err
= pdu_marshal(pdu
, offset
, "b", status
);
2893 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2894 pdu_complete(pdu
, err
);
2895 v9fs_string_free(&flock
.client_id
);
2899 * When a TGETLOCK request comes, always return success because all lock
2900 * handling is done by client's VFS layer.
2902 static void v9fs_getlock(void *opaque
)
2908 int32_t fid
, err
= 0;
2909 V9fsPDU
*pdu
= opaque
;
2911 v9fs_string_init(&glock
.client_id
);
2912 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
2913 &glock
.start
, &glock
.length
, &glock
.proc_id
,
2918 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2919 glock
.type
, glock
.start
, glock
.length
);
2921 fidp
= get_fid(pdu
, fid
);
2926 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2930 glock
.type
= P9_LOCK_TYPE_UNLCK
;
2931 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
2932 glock
.start
, glock
.length
, glock
.proc_id
,
2938 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
2939 glock
.length
, glock
.proc_id
);
2943 pdu_complete(pdu
, err
);
2944 v9fs_string_free(&glock
.client_id
);
2947 static void v9fs_mkdir(void *opaque
)
2949 V9fsPDU
*pdu
= opaque
;
2960 v9fs_string_init(&name
);
2961 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2965 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
2967 fidp
= get_fid(pdu
, fid
);
2972 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2976 stat_to_qid(&stbuf
, &qid
);
2977 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2982 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
2983 qid
.type
, qid
.version
, qid
.path
, err
);
2987 pdu_complete(pdu
, err
);
2988 v9fs_string_free(&name
);
2991 static void v9fs_xattrwalk(void *opaque
)
2997 int32_t fid
, newfid
;
2998 V9fsFidState
*file_fidp
;
2999 V9fsFidState
*xattr_fidp
= NULL
;
3000 V9fsPDU
*pdu
= opaque
;
3001 V9fsState
*s
= pdu
->s
;
3003 v9fs_string_init(&name
);
3004 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3008 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3010 file_fidp
= get_fid(pdu
, fid
);
3011 if (file_fidp
== NULL
) {
3015 xattr_fidp
= alloc_fid(s
, newfid
);
3016 if (xattr_fidp
== NULL
) {
3020 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3021 if (name
.data
== NULL
) {
3023 * listxattr request. Get the size first
3025 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3028 clunk_fid(s
, xattr_fidp
->fid
);
3032 * Read the xattr value
3034 xattr_fidp
->fs
.xattr
.len
= size
;
3035 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3036 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3038 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3039 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3040 xattr_fidp
->fs
.xattr
.value
,
3041 xattr_fidp
->fs
.xattr
.len
);
3043 clunk_fid(s
, xattr_fidp
->fid
);
3047 err
= pdu_marshal(pdu
, offset
, "q", size
);
3054 * specific xattr fid. We check for xattr
3055 * presence also collect the xattr size
3057 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3061 clunk_fid(s
, xattr_fidp
->fid
);
3065 * Read the xattr value
3067 xattr_fidp
->fs
.xattr
.len
= size
;
3068 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3069 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3071 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3072 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3073 &name
, xattr_fidp
->fs
.xattr
.value
,
3074 xattr_fidp
->fs
.xattr
.len
);
3076 clunk_fid(s
, xattr_fidp
->fid
);
3080 err
= pdu_marshal(pdu
, offset
, "q", size
);
3086 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3088 put_fid(pdu
, file_fidp
);
3090 put_fid(pdu
, xattr_fidp
);
3093 pdu_complete(pdu
, err
);
3094 v9fs_string_free(&name
);
3097 static void v9fs_xattrcreate(void *opaque
)
3105 V9fsFidState
*file_fidp
;
3106 V9fsFidState
*xattr_fidp
;
3107 V9fsPDU
*pdu
= opaque
;
3109 v9fs_string_init(&name
);
3110 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3114 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3116 file_fidp
= get_fid(pdu
, fid
);
3117 if (file_fidp
== NULL
) {
3121 /* Make the file fid point to xattr */
3122 xattr_fidp
= file_fidp
;
3123 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3124 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3125 xattr_fidp
->fs
.xattr
.len
= size
;
3126 xattr_fidp
->fs
.xattr
.flags
= flags
;
3127 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3128 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3129 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3131 put_fid(pdu
, file_fidp
);
3133 pdu_complete(pdu
, err
);
3134 v9fs_string_free(&name
);
3137 static void v9fs_readlink(void *opaque
)
3139 V9fsPDU
*pdu
= opaque
;
3146 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3150 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3151 fidp
= get_fid(pdu
, fid
);
3157 v9fs_string_init(&target
);
3158 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3162 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3164 v9fs_string_free(&target
);
3168 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3169 v9fs_string_free(&target
);
3173 pdu_complete(pdu
, err
);
3176 static CoroutineEntry
*pdu_co_handlers
[] = {
3177 [P9_TREADDIR
] = v9fs_readdir
,
3178 [P9_TSTATFS
] = v9fs_statfs
,
3179 [P9_TGETATTR
] = v9fs_getattr
,
3180 [P9_TSETATTR
] = v9fs_setattr
,
3181 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3182 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3183 [P9_TMKNOD
] = v9fs_mknod
,
3184 [P9_TRENAME
] = v9fs_rename
,
3185 [P9_TLOCK
] = v9fs_lock
,
3186 [P9_TGETLOCK
] = v9fs_getlock
,
3187 [P9_TRENAMEAT
] = v9fs_renameat
,
3188 [P9_TREADLINK
] = v9fs_readlink
,
3189 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3190 [P9_TMKDIR
] = v9fs_mkdir
,
3191 [P9_TVERSION
] = v9fs_version
,
3192 [P9_TLOPEN
] = v9fs_open
,
3193 [P9_TATTACH
] = v9fs_attach
,
3194 [P9_TSTAT
] = v9fs_stat
,
3195 [P9_TWALK
] = v9fs_walk
,
3196 [P9_TCLUNK
] = v9fs_clunk
,
3197 [P9_TFSYNC
] = v9fs_fsync
,
3198 [P9_TOPEN
] = v9fs_open
,
3199 [P9_TREAD
] = v9fs_read
,
3201 [P9_TAUTH
] = v9fs_auth
,
3203 [P9_TFLUSH
] = v9fs_flush
,
3204 [P9_TLINK
] = v9fs_link
,
3205 [P9_TSYMLINK
] = v9fs_symlink
,
3206 [P9_TCREATE
] = v9fs_create
,
3207 [P9_TLCREATE
] = v9fs_lcreate
,
3208 [P9_TWRITE
] = v9fs_write
,
3209 [P9_TWSTAT
] = v9fs_wstat
,
3210 [P9_TREMOVE
] = v9fs_remove
,
3213 static void v9fs_op_not_supp(void *opaque
)
3215 V9fsPDU
*pdu
= opaque
;
3216 pdu_complete(pdu
, -EOPNOTSUPP
);
3219 static void v9fs_fs_ro(void *opaque
)
3221 V9fsPDU
*pdu
= opaque
;
3222 pdu_complete(pdu
, -EROFS
);
3225 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3252 void pdu_submit(V9fsPDU
*pdu
)
3255 CoroutineEntry
*handler
;
3256 V9fsState
*s
= pdu
->s
;
3258 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3259 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3260 handler
= v9fs_op_not_supp
;
3262 handler
= pdu_co_handlers
[pdu
->id
];
3265 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3266 handler
= v9fs_fs_ro
;
3268 co
= qemu_coroutine_create(handler
);
3269 qemu_coroutine_enter(co
, pdu
);
3272 /* Returns 0 on success, 1 on failure. */
3273 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3275 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
3282 /* initialize pdu allocator */
3283 QLIST_INIT(&s
->free_list
);
3284 QLIST_INIT(&s
->active_list
);
3285 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3286 QLIST_INSERT_HEAD(&s
->free_list
, &v
->pdus
[i
], next
);
3291 v9fs_path_init(&path
);
3293 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3296 /* We don't have a fsdev identified by fsdev_id */
3297 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3299 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3303 if (!s
->fsconf
.tag
) {
3304 /* we haven't specified a mount_tag */
3305 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3306 s
->fsconf
.fsdev_id
);
3310 s
->ctx
.export_flags
= fse
->export_flags
;
3311 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3312 s
->ctx
.exops
.get_st_gen
= NULL
;
3313 len
= strlen(s
->fsconf
.tag
);
3314 if (len
> MAX_TAG_LEN
- 1) {
3315 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3316 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3320 s
->tag
= g_strdup(s
->fsconf
.tag
);
3326 qemu_co_rwlock_init(&s
->rename_lock
);
3328 if (s
->ops
->init(&s
->ctx
) < 0) {
3329 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3330 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3335 * Check details of export path, We need to use fs driver
3336 * call back to do that. Since we are in the init path, we don't
3337 * use co-routines here.
3339 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3341 "error in converting name to path %s", strerror(errno
));
3344 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3345 error_setg(errp
, "share path %s does not exist", fse
->path
);
3347 } else if (!S_ISDIR(stat
.st_mode
)) {
3348 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3351 v9fs_path_free(&path
);
3356 g_free(s
->ctx
.fs_root
);
3358 v9fs_path_free(&path
);
3363 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3365 g_free(s
->ctx
.fs_root
);
3369 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3372 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3373 fprintf(stderr
, "Failed to get the resource limit\n");
3376 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3377 open_fd_rc
= rlim
.rlim_cur
/2;