4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "hw/virtio/virtio.h"
16 #include "qapi/error.h"
17 #include "qemu/error-report.h"
19 #include "qemu/sockets.h"
20 #include "virtio-9p.h"
21 #include "fsdev/qemu-fsdev.h"
25 #include "migration/migration.h"
29 static int open_fd_rc
;
43 ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
49 ret
= virtio_pdu_vmarshal(pdu
, offset
, fmt
, ap
);
55 ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
61 ret
= virtio_pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
67 static void pdu_push_and_notify(V9fsPDU
*pdu
)
69 virtio_9p_push_and_notify(pdu
);
72 static int omode_to_uflags(int8_t mode
)
106 struct dotl_openflag_map
{
111 static int dotl_to_open_flags(int flags
)
115 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
116 * and P9_DOTL_NOACCESS
118 int oflags
= flags
& O_ACCMODE
;
120 struct dotl_openflag_map dotl_oflag_map
[] = {
121 { P9_DOTL_CREATE
, O_CREAT
},
122 { P9_DOTL_EXCL
, O_EXCL
},
123 { P9_DOTL_NOCTTY
, O_NOCTTY
},
124 { P9_DOTL_TRUNC
, O_TRUNC
},
125 { P9_DOTL_APPEND
, O_APPEND
},
126 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
127 { P9_DOTL_DSYNC
, O_DSYNC
},
128 { P9_DOTL_FASYNC
, FASYNC
},
129 { P9_DOTL_DIRECT
, O_DIRECT
},
130 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
131 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
132 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
133 { P9_DOTL_NOATIME
, O_NOATIME
},
134 { P9_DOTL_SYNC
, O_SYNC
},
137 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
138 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
139 oflags
|= dotl_oflag_map
[i
].open_flag
;
146 void cred_init(FsCred
*credp
)
154 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
158 * Filter the client open flags
160 flags
= dotl_to_open_flags(oflags
);
161 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
163 * Ignore direct disk access hint until the server supports it.
169 void v9fs_path_init(V9fsPath
*path
)
175 void v9fs_path_free(V9fsPath
*path
)
182 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
185 lhs
->data
= g_malloc(rhs
->size
);
186 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
187 lhs
->size
= rhs
->size
;
190 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
191 const char *name
, V9fsPath
*path
)
194 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
202 * Return TRUE if s1 is an ancestor of s2.
204 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
205 * As a special case, We treat s1 as ancestor of s2 if they are same!
207 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
209 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
210 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
217 static size_t v9fs_string_size(V9fsString
*str
)
223 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
224 static int v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
227 if (f
->fid_type
== P9_FID_FILE
) {
228 if (f
->fs
.fd
== -1) {
230 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
231 } while (err
== -EINTR
&& !pdu
->cancelled
);
233 } else if (f
->fid_type
== P9_FID_DIR
) {
234 if (f
->fs
.dir
.stream
== NULL
) {
236 err
= v9fs_co_opendir(pdu
, f
);
237 } while (err
== -EINTR
&& !pdu
->cancelled
);
243 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
247 V9fsState
*s
= pdu
->s
;
249 for (f
= s
->fid_list
; f
; f
= f
->next
) {
253 * Update the fid ref upfront so that
254 * we don't get reclaimed when we yield
259 * check whether we need to reopen the
260 * file. We might have closed the fd
261 * while trying to free up some file
264 err
= v9fs_reopen_fid(pdu
, f
);
270 * Mark the fid as referenced so that the LRU
271 * reclaim won't close the file descriptor
273 f
->flags
|= FID_REFERENCED
;
280 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
284 for (f
= s
->fid_list
; f
; f
= f
->next
) {
285 /* If fid is already there return NULL */
291 f
= g_malloc0(sizeof(V9fsFidState
));
293 f
->fid_type
= P9_FID_NONE
;
296 * Mark the fid as referenced so that the LRU
297 * reclaim won't close the file descriptor
299 f
->flags
|= FID_REFERENCED
;
300 f
->next
= s
->fid_list
;
303 v9fs_readdir_init(&f
->fs
.dir
);
304 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
309 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
313 if (fidp
->fs
.xattr
.copied_len
== -1) {
314 /* getxattr/listxattr fid */
318 * if this is fid for setxattr. clunk should
319 * result in setxattr localcall
321 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
322 /* clunk after partial write */
326 if (fidp
->fs
.xattr
.len
) {
327 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
328 fidp
->fs
.xattr
.value
,
330 fidp
->fs
.xattr
.flags
);
332 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
335 v9fs_string_free(&fidp
->fs
.xattr
.name
);
337 g_free(fidp
->fs
.xattr
.value
);
341 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
345 if (fidp
->fid_type
== P9_FID_FILE
) {
346 /* If we reclaimed the fd no need to close */
347 if (fidp
->fs
.fd
!= -1) {
348 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
350 } else if (fidp
->fid_type
== P9_FID_DIR
) {
351 if (fidp
->fs
.dir
.stream
!= NULL
) {
352 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
354 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
355 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
357 v9fs_path_free(&fidp
->path
);
362 static int put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
367 * Don't free the fid if it is in reclaim list
369 if (!fidp
->ref
&& fidp
->clunked
) {
370 if (fidp
->fid
== pdu
->s
->root_fid
) {
372 * if the clunked fid is root fid then we
373 * have unmounted the fs on the client side.
374 * delete the migration blocker. Ideally, this
375 * should be hooked to transport close notification
377 if (pdu
->s
->migration_blocker
) {
378 migrate_del_blocker(pdu
->s
->migration_blocker
);
379 error_free(pdu
->s
->migration_blocker
);
380 pdu
->s
->migration_blocker
= NULL
;
383 return free_fid(pdu
, fidp
);
388 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
390 V9fsFidState
**fidpp
, *fidp
;
392 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
393 if ((*fidpp
)->fid
== fid
) {
397 if (*fidpp
== NULL
) {
406 void v9fs_reclaim_fd(V9fsPDU
*pdu
)
408 int reclaim_count
= 0;
409 V9fsState
*s
= pdu
->s
;
410 V9fsFidState
*f
, *reclaim_list
= NULL
;
412 for (f
= s
->fid_list
; f
; f
= f
->next
) {
414 * Unlink fids cannot be reclaimed. Check
415 * for them and skip them. Also skip fids
416 * currently being operated on.
418 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
422 * if it is a recently referenced fid
423 * we leave the fid untouched and clear the
424 * reference bit. We come back to it later
425 * in the next iteration. (a simple LRU without
426 * moving list elements around)
428 if (f
->flags
& FID_REFERENCED
) {
429 f
->flags
&= ~FID_REFERENCED
;
433 * Add fids to reclaim list.
435 if (f
->fid_type
== P9_FID_FILE
) {
436 if (f
->fs
.fd
!= -1) {
438 * Up the reference count so that
439 * a clunk request won't free this fid
442 f
->rclm_lst
= reclaim_list
;
444 f
->fs_reclaim
.fd
= f
->fs
.fd
;
448 } else if (f
->fid_type
== P9_FID_DIR
) {
449 if (f
->fs
.dir
.stream
!= NULL
) {
451 * Up the reference count so that
452 * a clunk request won't free this fid
455 f
->rclm_lst
= reclaim_list
;
457 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
458 f
->fs
.dir
.stream
= NULL
;
462 if (reclaim_count
>= open_fd_rc
) {
467 * Now close the fid in reclaim list. Free them if they
468 * are already clunked.
470 while (reclaim_list
) {
472 reclaim_list
= f
->rclm_lst
;
473 if (f
->fid_type
== P9_FID_FILE
) {
474 v9fs_co_close(pdu
, &f
->fs_reclaim
);
475 } else if (f
->fid_type
== P9_FID_DIR
) {
476 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
480 * Now drop the fid reference, free it
487 static int v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
490 V9fsState
*s
= pdu
->s
;
491 V9fsFidState
*fidp
, head_fid
;
493 head_fid
.next
= s
->fid_list
;
494 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
495 if (fidp
->path
.size
!= path
->size
) {
498 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
499 /* Mark the fid non reclaimable. */
500 fidp
->flags
|= FID_NON_RECLAIMABLE
;
502 /* reopen the file/dir if already closed */
503 err
= v9fs_reopen_fid(pdu
, fidp
);
508 * Go back to head of fid list because
509 * the list could have got updated when
510 * switched to the worker thread
520 static void virtfs_reset(V9fsPDU
*pdu
)
522 V9fsState
*s
= pdu
->s
;
523 V9fsFidState
*fidp
= NULL
;
526 while (s
->fid_list
) {
528 s
->fid_list
= fidp
->next
;
537 /* One or more unclunked fids found... */
538 error_report("9pfs:%s: One or more uncluncked fids "
539 "found during reset", __func__
);
543 #define P9_QID_TYPE_DIR 0x80
544 #define P9_QID_TYPE_SYMLINK 0x02
546 #define P9_STAT_MODE_DIR 0x80000000
547 #define P9_STAT_MODE_APPEND 0x40000000
548 #define P9_STAT_MODE_EXCL 0x20000000
549 #define P9_STAT_MODE_MOUNT 0x10000000
550 #define P9_STAT_MODE_AUTH 0x08000000
551 #define P9_STAT_MODE_TMP 0x04000000
552 #define P9_STAT_MODE_SYMLINK 0x02000000
553 #define P9_STAT_MODE_LINK 0x01000000
554 #define P9_STAT_MODE_DEVICE 0x00800000
555 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
556 #define P9_STAT_MODE_SOCKET 0x00100000
557 #define P9_STAT_MODE_SETUID 0x00080000
558 #define P9_STAT_MODE_SETGID 0x00040000
559 #define P9_STAT_MODE_SETVTX 0x00010000
561 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
562 P9_STAT_MODE_SYMLINK | \
563 P9_STAT_MODE_LINK | \
564 P9_STAT_MODE_DEVICE | \
565 P9_STAT_MODE_NAMED_PIPE | \
568 /* This is the algorithm from ufs in spfs */
569 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
573 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
574 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
575 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
576 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
578 if (S_ISDIR(stbuf
->st_mode
)) {
579 qidp
->type
|= P9_QID_TYPE_DIR
;
581 if (S_ISLNK(stbuf
->st_mode
)) {
582 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
586 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
591 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
595 stat_to_qid(&stbuf
, qidp
);
599 V9fsPDU
*pdu_alloc(V9fsState
*s
)
603 if (!QLIST_EMPTY(&s
->free_list
)) {
604 pdu
= QLIST_FIRST(&s
->free_list
);
605 QLIST_REMOVE(pdu
, next
);
606 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
611 void pdu_free(V9fsPDU
*pdu
)
614 V9fsState
*s
= pdu
->s
;
616 * Cancelled pdu are added back to the freelist
619 if (!pdu
->cancelled
) {
620 QLIST_REMOVE(pdu
, next
);
621 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
627 * We don't do error checking for pdu_marshal/unmarshal here
628 * because we always expect to have enough space to encode
631 static void pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
633 int8_t id
= pdu
->id
+ 1; /* Response */
634 V9fsState
*s
= pdu
->s
;
640 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
643 str
.data
= strerror(err
);
644 str
.size
= strlen(str
.data
);
646 len
+= pdu_marshal(pdu
, len
, "s", &str
);
650 len
+= pdu_marshal(pdu
, len
, "d", err
);
652 if (s
->proto_version
== V9FS_PROTO_2000L
) {
655 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
658 /* fill out the header */
659 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
661 /* keep these in sync */
665 pdu_push_and_notify(pdu
);
667 /* Now wakeup anybody waiting in flush for this request */
668 qemu_co_queue_next(&pdu
->complete
);
673 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
678 if (mode
& P9_STAT_MODE_DIR
) {
682 if (mode
& P9_STAT_MODE_SYMLINK
) {
685 if (mode
& P9_STAT_MODE_SOCKET
) {
688 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
691 if (mode
& P9_STAT_MODE_DEVICE
) {
692 if (extension
->size
&& extension
->data
[0] == 'c') {
703 if (mode
& P9_STAT_MODE_SETUID
) {
706 if (mode
& P9_STAT_MODE_SETGID
) {
709 if (mode
& P9_STAT_MODE_SETVTX
) {
716 static int donttouch_stat(V9fsStat
*stat
)
718 if (stat
->type
== -1 &&
720 stat
->qid
.type
== -1 &&
721 stat
->qid
.version
== -1 &&
722 stat
->qid
.path
== -1 &&
726 stat
->length
== -1 &&
733 stat
->n_muid
== -1) {
740 static void v9fs_stat_init(V9fsStat
*stat
)
742 v9fs_string_init(&stat
->name
);
743 v9fs_string_init(&stat
->uid
);
744 v9fs_string_init(&stat
->gid
);
745 v9fs_string_init(&stat
->muid
);
746 v9fs_string_init(&stat
->extension
);
749 static void v9fs_stat_free(V9fsStat
*stat
)
751 v9fs_string_free(&stat
->name
);
752 v9fs_string_free(&stat
->uid
);
753 v9fs_string_free(&stat
->gid
);
754 v9fs_string_free(&stat
->muid
);
755 v9fs_string_free(&stat
->extension
);
758 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
762 mode
= stbuf
->st_mode
& 0777;
763 if (S_ISDIR(stbuf
->st_mode
)) {
764 mode
|= P9_STAT_MODE_DIR
;
767 if (S_ISLNK(stbuf
->st_mode
)) {
768 mode
|= P9_STAT_MODE_SYMLINK
;
771 if (S_ISSOCK(stbuf
->st_mode
)) {
772 mode
|= P9_STAT_MODE_SOCKET
;
775 if (S_ISFIFO(stbuf
->st_mode
)) {
776 mode
|= P9_STAT_MODE_NAMED_PIPE
;
779 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
780 mode
|= P9_STAT_MODE_DEVICE
;
783 if (stbuf
->st_mode
& S_ISUID
) {
784 mode
|= P9_STAT_MODE_SETUID
;
787 if (stbuf
->st_mode
& S_ISGID
) {
788 mode
|= P9_STAT_MODE_SETGID
;
791 if (stbuf
->st_mode
& S_ISVTX
) {
792 mode
|= P9_STAT_MODE_SETVTX
;
798 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
799 const struct stat
*stbuf
,
805 memset(v9stat
, 0, sizeof(*v9stat
));
807 stat_to_qid(stbuf
, &v9stat
->qid
);
808 v9stat
->mode
= stat_to_v9mode(stbuf
);
809 v9stat
->atime
= stbuf
->st_atime
;
810 v9stat
->mtime
= stbuf
->st_mtime
;
811 v9stat
->length
= stbuf
->st_size
;
813 v9fs_string_null(&v9stat
->uid
);
814 v9fs_string_null(&v9stat
->gid
);
815 v9fs_string_null(&v9stat
->muid
);
817 v9stat
->n_uid
= stbuf
->st_uid
;
818 v9stat
->n_gid
= stbuf
->st_gid
;
821 v9fs_string_null(&v9stat
->extension
);
823 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
824 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
828 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
829 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
830 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
831 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
832 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
833 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
834 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
837 str
= strrchr(name
->data
, '/');
844 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
847 v9fs_string_size(&v9stat
->name
) +
848 v9fs_string_size(&v9stat
->uid
) +
849 v9fs_string_size(&v9stat
->gid
) +
850 v9fs_string_size(&v9stat
->muid
) +
851 v9fs_string_size(&v9stat
->extension
);
855 #define P9_STATS_MODE 0x00000001ULL
856 #define P9_STATS_NLINK 0x00000002ULL
857 #define P9_STATS_UID 0x00000004ULL
858 #define P9_STATS_GID 0x00000008ULL
859 #define P9_STATS_RDEV 0x00000010ULL
860 #define P9_STATS_ATIME 0x00000020ULL
861 #define P9_STATS_MTIME 0x00000040ULL
862 #define P9_STATS_CTIME 0x00000080ULL
863 #define P9_STATS_INO 0x00000100ULL
864 #define P9_STATS_SIZE 0x00000200ULL
865 #define P9_STATS_BLOCKS 0x00000400ULL
867 #define P9_STATS_BTIME 0x00000800ULL
868 #define P9_STATS_GEN 0x00001000ULL
869 #define P9_STATS_DATA_VERSION 0x00002000ULL
871 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
872 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
875 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
876 V9fsStatDotl
*v9lstat
)
878 memset(v9lstat
, 0, sizeof(*v9lstat
));
880 v9lstat
->st_mode
= stbuf
->st_mode
;
881 v9lstat
->st_nlink
= stbuf
->st_nlink
;
882 v9lstat
->st_uid
= stbuf
->st_uid
;
883 v9lstat
->st_gid
= stbuf
->st_gid
;
884 v9lstat
->st_rdev
= stbuf
->st_rdev
;
885 v9lstat
->st_size
= stbuf
->st_size
;
886 v9lstat
->st_blksize
= stbuf
->st_blksize
;
887 v9lstat
->st_blocks
= stbuf
->st_blocks
;
888 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
889 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
890 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
891 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
892 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
893 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
894 /* Currently we only support BASIC fields in stat */
895 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
897 stat_to_qid(stbuf
, &v9lstat
->qid
);
900 static void print_sg(struct iovec
*sg
, int cnt
)
904 printf("sg[%d]: {", cnt
);
905 for (i
= 0; i
< cnt
; i
++) {
909 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
914 /* Will call this only for path name based fid */
915 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
918 v9fs_path_init(&str
);
919 v9fs_path_copy(&str
, dst
);
920 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
921 v9fs_path_free(&str
);
922 /* +1 to include terminating NULL */
926 static inline bool is_ro_export(FsContext
*ctx
)
928 return ctx
->export_flags
& V9FS_RDONLY
;
931 static void v9fs_version(void *opaque
)
934 V9fsPDU
*pdu
= opaque
;
935 V9fsState
*s
= pdu
->s
;
939 v9fs_string_init(&version
);
940 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
945 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
949 if (!strcmp(version
.data
, "9P2000.u")) {
950 s
->proto_version
= V9FS_PROTO_2000U
;
951 } else if (!strcmp(version
.data
, "9P2000.L")) {
952 s
->proto_version
= V9FS_PROTO_2000L
;
954 v9fs_string_sprintf(&version
, "unknown");
957 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
963 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
965 pdu_complete(pdu
, offset
);
966 v9fs_string_free(&version
);
969 static void v9fs_attach(void *opaque
)
971 V9fsPDU
*pdu
= opaque
;
972 V9fsState
*s
= pdu
->s
;
973 int32_t fid
, afid
, n_uname
;
974 V9fsString uname
, aname
;
980 v9fs_string_init(&uname
);
981 v9fs_string_init(&aname
);
982 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
983 &afid
, &uname
, &aname
, &n_uname
);
987 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
989 fidp
= alloc_fid(s
, fid
);
995 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1001 err
= fid_to_qid(pdu
, fidp
, &qid
);
1007 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1013 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1014 qid
.type
, qid
.version
, qid
.path
);
1016 * disable migration if we haven't done already.
1017 * attach could get called multiple times for the same export.
1019 if (!s
->migration_blocker
) {
1021 error_setg(&s
->migration_blocker
,
1022 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1023 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1024 migrate_add_blocker(s
->migration_blocker
);
1029 pdu_complete(pdu
, err
);
1030 v9fs_string_free(&uname
);
1031 v9fs_string_free(&aname
);
1034 static void v9fs_stat(void *opaque
)
1042 V9fsPDU
*pdu
= opaque
;
1044 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1048 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1050 fidp
= get_fid(pdu
, fid
);
1055 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1059 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1063 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1065 v9fs_stat_free(&v9stat
);
1068 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1069 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1071 v9fs_stat_free(&v9stat
);
1075 pdu_complete(pdu
, err
);
1078 static void v9fs_getattr(void *opaque
)
1085 uint64_t request_mask
;
1086 V9fsStatDotl v9stat_dotl
;
1087 V9fsPDU
*pdu
= opaque
;
1088 V9fsState
*s
= pdu
->s
;
1090 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1094 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1096 fidp
= get_fid(pdu
, fid
);
1102 * Currently we only support BASIC fields in stat, so there is no
1103 * need to look at request_mask.
1105 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1109 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1111 /* fill st_gen if requested and supported by underlying fs */
1112 if (request_mask
& P9_STATS_GEN
) {
1113 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1116 /* we have valid st_gen: update result mask */
1117 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1120 /* request cancelled, e.g. by Tflush */
1123 /* failed to get st_gen: not fatal, ignore */
1127 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1132 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1133 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1134 v9stat_dotl
.st_gid
);
1138 pdu_complete(pdu
, retval
);
1141 /* Attribute flags */
1142 #define P9_ATTR_MODE (1 << 0)
1143 #define P9_ATTR_UID (1 << 1)
1144 #define P9_ATTR_GID (1 << 2)
1145 #define P9_ATTR_SIZE (1 << 3)
1146 #define P9_ATTR_ATIME (1 << 4)
1147 #define P9_ATTR_MTIME (1 << 5)
1148 #define P9_ATTR_CTIME (1 << 6)
1149 #define P9_ATTR_ATIME_SET (1 << 7)
1150 #define P9_ATTR_MTIME_SET (1 << 8)
1152 #define P9_ATTR_MASK 127
1154 static void v9fs_setattr(void *opaque
)
1161 V9fsPDU
*pdu
= opaque
;
1163 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1168 fidp
= get_fid(pdu
, fid
);
1173 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1174 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1179 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1180 struct timespec times
[2];
1181 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1182 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1183 times
[0].tv_sec
= v9iattr
.atime_sec
;
1184 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1186 times
[0].tv_nsec
= UTIME_NOW
;
1189 times
[0].tv_nsec
= UTIME_OMIT
;
1191 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1192 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1193 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1194 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1196 times
[1].tv_nsec
= UTIME_NOW
;
1199 times
[1].tv_nsec
= UTIME_OMIT
;
1201 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1207 * If the only valid entry in iattr is ctime we can call
1208 * chown(-1,-1) to update the ctime of the file
1210 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1211 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1212 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1213 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1216 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1219 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1225 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1226 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1235 pdu_complete(pdu
, err
);
1238 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1244 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1249 for (i
= 0; i
< nwnames
; i
++) {
1250 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1259 static void v9fs_walk(void *opaque
)
1262 V9fsQID
*qids
= NULL
;
1264 V9fsPath dpath
, path
;
1268 int32_t fid
, newfid
;
1269 V9fsString
*wnames
= NULL
;
1271 V9fsFidState
*newfidp
= NULL
;
1272 V9fsPDU
*pdu
= opaque
;
1273 V9fsState
*s
= pdu
->s
;
1275 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1277 pdu_complete(pdu
, err
);
1282 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1284 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1285 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1286 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1287 for (i
= 0; i
< nwnames
; i
++) {
1288 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1294 } else if (nwnames
> P9_MAXWELEM
) {
1298 fidp
= get_fid(pdu
, fid
);
1303 v9fs_path_init(&dpath
);
1304 v9fs_path_init(&path
);
1306 * Both dpath and path initially poin to fidp.
1307 * Needed to handle request with nwnames == 0
1309 v9fs_path_copy(&dpath
, &fidp
->path
);
1310 v9fs_path_copy(&path
, &fidp
->path
);
1311 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1312 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1316 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1320 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1321 v9fs_path_copy(&dpath
, &path
);
1323 if (fid
== newfid
) {
1324 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1325 v9fs_path_copy(&fidp
->path
, &path
);
1327 newfidp
= alloc_fid(s
, newfid
);
1328 if (newfidp
== NULL
) {
1332 newfidp
->uid
= fidp
->uid
;
1333 v9fs_path_copy(&newfidp
->path
, &path
);
1335 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1336 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1340 put_fid(pdu
, newfidp
);
1342 v9fs_path_free(&dpath
);
1343 v9fs_path_free(&path
);
1345 pdu_complete(pdu
, err
);
1346 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1347 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1348 v9fs_string_free(&wnames
[name_idx
]);
1355 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1357 struct statfs stbuf
;
1359 V9fsState
*s
= pdu
->s
;
1362 * iounit should be multiples of f_bsize (host filesystem block size
1363 * and as well as less than (client msize - P9_IOHDRSZ))
1365 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1366 iounit
= stbuf
.f_bsize
;
1367 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1370 iounit
= s
->msize
- P9_IOHDRSZ
;
1375 static void v9fs_open(void *opaque
)
1386 V9fsPDU
*pdu
= opaque
;
1387 V9fsState
*s
= pdu
->s
;
1389 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1390 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1393 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1399 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1401 fidp
= get_fid(pdu
, fid
);
1406 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1408 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1412 stat_to_qid(&stbuf
, &qid
);
1413 if (S_ISDIR(stbuf
.st_mode
)) {
1414 err
= v9fs_co_opendir(pdu
, fidp
);
1418 fidp
->fid_type
= P9_FID_DIR
;
1419 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1425 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1426 flags
= get_dotl_openflags(s
, mode
);
1428 flags
= omode_to_uflags(mode
);
1430 if (is_ro_export(&s
->ctx
)) {
1431 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1432 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1437 err
= v9fs_co_open(pdu
, fidp
, flags
);
1441 fidp
->fid_type
= P9_FID_FILE
;
1442 fidp
->open_flags
= flags
;
1443 if (flags
& O_EXCL
) {
1445 * We let the host file system do O_EXCL check
1446 * We should not reclaim such fd
1448 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1450 iounit
= get_iounit(pdu
, &fidp
->path
);
1451 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1457 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1458 qid
.type
, qid
.version
, qid
.path
, iounit
);
1462 pdu_complete(pdu
, err
);
1465 static void v9fs_lcreate(void *opaque
)
1467 int32_t dfid
, flags
, mode
;
1476 V9fsPDU
*pdu
= opaque
;
1478 v9fs_string_init(&name
);
1479 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1480 &name
, &flags
, &mode
, &gid
);
1484 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1486 fidp
= get_fid(pdu
, dfid
);
1492 flags
= get_dotl_openflags(pdu
->s
, flags
);
1493 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1494 flags
| O_CREAT
, mode
, &stbuf
);
1498 fidp
->fid_type
= P9_FID_FILE
;
1499 fidp
->open_flags
= flags
;
1500 if (flags
& O_EXCL
) {
1502 * We let the host file system do O_EXCL check
1503 * We should not reclaim such fd
1505 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1507 iounit
= get_iounit(pdu
, &fidp
->path
);
1508 stat_to_qid(&stbuf
, &qid
);
1509 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1514 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1515 qid
.type
, qid
.version
, qid
.path
, iounit
);
1519 pdu_complete(pdu
, err
);
1520 v9fs_string_free(&name
);
1523 static void v9fs_fsync(void *opaque
)
1530 V9fsPDU
*pdu
= opaque
;
1532 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1536 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1538 fidp
= get_fid(pdu
, fid
);
1543 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1549 pdu_complete(pdu
, err
);
1552 static void v9fs_clunk(void *opaque
)
1558 V9fsPDU
*pdu
= opaque
;
1559 V9fsState
*s
= pdu
->s
;
1561 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1565 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1567 fidp
= clunk_fid(s
, fid
);
1573 * Bump the ref so that put_fid will
1577 err
= put_fid(pdu
, fidp
);
1582 pdu_complete(pdu
, err
);
1585 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1586 uint64_t off
, uint32_t max_count
)
1592 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
1593 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
1595 xattr_len
= fidp
->fs
.xattr
.len
;
1596 read_count
= xattr_len
- off
;
1597 if (read_count
> max_count
) {
1598 read_count
= max_count
;
1599 } else if (read_count
< 0) {
1601 * read beyond XATTR value
1605 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1611 err
= v9fs_pack(elem
->in_sg
, elem
->in_num
, offset
,
1612 ((char *)fidp
->fs
.xattr
.value
) + off
,
1621 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1622 V9fsFidState
*fidp
, uint32_t max_count
)
1629 off_t saved_dir_pos
;
1630 struct dirent
*dent
;
1632 /* save the directory position */
1633 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1634 if (saved_dir_pos
< 0) {
1635 return saved_dir_pos
;
1639 v9fs_path_init(&path
);
1641 v9fs_readdir_lock(&fidp
->fs
.dir
);
1643 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1647 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1651 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1655 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1659 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1660 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1662 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1664 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1665 /* Ran out of buffer. Set dir back to old position and return */
1666 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1667 v9fs_stat_free(&v9stat
);
1668 v9fs_path_free(&path
);
1672 v9fs_stat_free(&v9stat
);
1673 v9fs_path_free(&path
);
1674 saved_dir_pos
= dent
->d_off
;
1677 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1679 v9fs_path_free(&path
);
1687 * Create a QEMUIOVector for a sub-region of PDU iovecs
1689 * @qiov: uninitialized QEMUIOVector
1690 * @skip: number of bytes to skip from beginning of PDU
1691 * @size: number of bytes to include
1692 * @is_write: true - write, false - read
1694 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1695 * with qemu_iovec_destroy().
1697 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1698 size_t skip
, size_t size
,
1705 virtio_init_iov_from_pdu(pdu
, &iov
, &niov
, is_write
);
1707 qemu_iovec_init_external(&elem
, iov
, niov
);
1708 qemu_iovec_init(qiov
, niov
);
1709 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1712 static void v9fs_read(void *opaque
)
1721 V9fsPDU
*pdu
= opaque
;
1722 V9fsState
*s
= pdu
->s
;
1724 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1728 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1730 fidp
= get_fid(pdu
, fid
);
1735 if (fidp
->fid_type
== P9_FID_DIR
) {
1738 v9fs_co_rewinddir(pdu
, fidp
);
1740 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1745 err
= pdu_marshal(pdu
, offset
, "d", count
);
1749 err
+= offset
+ count
;
1750 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1751 QEMUIOVector qiov_full
;
1755 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1756 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1758 qemu_iovec_reset(&qiov
);
1759 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1761 print_sg(qiov
.iov
, qiov
.niov
);
1763 /* Loop in case of EINTR */
1765 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1770 } while (len
== -EINTR
&& !pdu
->cancelled
);
1772 /* IO error return the error */
1776 } while (count
< max_count
&& len
> 0);
1777 err
= pdu_marshal(pdu
, offset
, "d", count
);
1781 err
+= offset
+ count
;
1782 qemu_iovec_destroy(&qiov
);
1783 qemu_iovec_destroy(&qiov_full
);
1784 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1785 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1789 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1793 pdu_complete(pdu
, err
);
1796 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1799 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1800 * size of type (1) + size of name.size (2) + strlen(name.data)
1802 return 24 + v9fs_string_size(name
);
1805 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1806 V9fsFidState
*fidp
, int32_t max_count
)
1813 off_t saved_dir_pos
;
1814 struct dirent
*dent
;
1816 /* save the directory position */
1817 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1818 if (saved_dir_pos
< 0) {
1819 return saved_dir_pos
;
1823 v9fs_readdir_lock(&fidp
->fs
.dir
);
1825 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1829 v9fs_string_init(&name
);
1830 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1831 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1832 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1834 /* Ran out of buffer. Set dir back to old position and return */
1835 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1836 v9fs_string_free(&name
);
1840 * Fill up just the path field of qid because the client uses
1841 * only that. To fill the entire qid structure we will have
1842 * to stat each dirent found, which is expensive
1844 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1845 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1846 /* Fill the other fields with dummy values */
1850 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1851 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1853 dent
->d_type
, &name
);
1855 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1858 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1859 v9fs_string_free(&name
);
1863 v9fs_string_free(&name
);
1864 saved_dir_pos
= dent
->d_off
;
1867 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1875 static void v9fs_readdir(void *opaque
)
1881 uint64_t initial_offset
;
1884 V9fsPDU
*pdu
= opaque
;
1886 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1887 &initial_offset
, &max_count
);
1891 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1893 fidp
= get_fid(pdu
, fid
);
1898 if (!fidp
->fs
.dir
.stream
) {
1902 if (initial_offset
== 0) {
1903 v9fs_co_rewinddir(pdu
, fidp
);
1905 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1907 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1912 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1916 retval
+= count
+ offset
;
1917 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1921 pdu_complete(pdu
, retval
);
1924 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1925 uint64_t off
, uint32_t count
,
1926 struct iovec
*sg
, int cnt
)
1935 xattr_len
= fidp
->fs
.xattr
.len
;
1936 write_count
= xattr_len
- off
;
1937 if (write_count
> count
) {
1938 write_count
= count
;
1939 } else if (write_count
< 0) {
1941 * write beyond XATTR value len specified in
1947 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1952 fidp
->fs
.xattr
.copied_len
+= write_count
;
1954 * Now copy the content from sg list
1956 for (i
= 0; i
< cnt
; i
++) {
1957 if (write_count
> sg
[i
].iov_len
) {
1958 to_copy
= sg
[i
].iov_len
;
1960 to_copy
= write_count
;
1962 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1963 /* updating vs->off since we are not using below */
1965 write_count
-= to_copy
;
1971 static void v9fs_write(void *opaque
)
1981 V9fsPDU
*pdu
= opaque
;
1982 V9fsState
*s
= pdu
->s
;
1983 QEMUIOVector qiov_full
;
1986 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
1988 pdu_complete(pdu
, err
);
1992 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
1993 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
1995 fidp
= get_fid(pdu
, fid
);
2000 if (fidp
->fid_type
== P9_FID_FILE
) {
2001 if (fidp
->fs
.fd
== -1) {
2005 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2007 * setxattr operation
2009 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2010 qiov_full
.iov
, qiov_full
.niov
);
2016 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2018 qemu_iovec_reset(&qiov
);
2019 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2021 print_sg(qiov
.iov
, qiov
.niov
);
2023 /* Loop in case of EINTR */
2025 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2030 } while (len
== -EINTR
&& !pdu
->cancelled
);
2032 /* IO error return the error */
2036 } while (total
< count
&& len
> 0);
2039 err
= pdu_marshal(pdu
, offset
, "d", total
);
2044 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2046 qemu_iovec_destroy(&qiov
);
2050 qemu_iovec_destroy(&qiov_full
);
2051 pdu_complete(pdu
, err
);
2054 static void v9fs_create(void *opaque
)
2066 V9fsString extension
;
2068 V9fsPDU
*pdu
= opaque
;
2070 v9fs_path_init(&path
);
2071 v9fs_string_init(&name
);
2072 v9fs_string_init(&extension
);
2073 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2074 &perm
, &mode
, &extension
);
2078 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2080 fidp
= get_fid(pdu
, fid
);
2085 if (perm
& P9_STAT_MODE_DIR
) {
2086 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2087 fidp
->uid
, -1, &stbuf
);
2091 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2095 v9fs_path_copy(&fidp
->path
, &path
);
2096 err
= v9fs_co_opendir(pdu
, fidp
);
2100 fidp
->fid_type
= P9_FID_DIR
;
2101 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2102 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2103 extension
.data
, -1 , &stbuf
);
2107 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2111 v9fs_path_copy(&fidp
->path
, &path
);
2112 } else if (perm
& P9_STAT_MODE_LINK
) {
2113 int32_t ofid
= atoi(extension
.data
);
2114 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2115 if (ofidp
== NULL
) {
2119 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2120 put_fid(pdu
, ofidp
);
2124 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2126 fidp
->fid_type
= P9_FID_NONE
;
2129 v9fs_path_copy(&fidp
->path
, &path
);
2130 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2132 fidp
->fid_type
= P9_FID_NONE
;
2135 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2137 uint32_t major
, minor
;
2140 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2157 nmode
|= perm
& 0777;
2158 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2159 makedev(major
, minor
), nmode
, &stbuf
);
2163 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2167 v9fs_path_copy(&fidp
->path
, &path
);
2168 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2169 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2170 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2174 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2178 v9fs_path_copy(&fidp
->path
, &path
);
2179 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2180 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2181 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2185 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2189 v9fs_path_copy(&fidp
->path
, &path
);
2191 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2192 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2196 fidp
->fid_type
= P9_FID_FILE
;
2197 fidp
->open_flags
= omode_to_uflags(mode
);
2198 if (fidp
->open_flags
& O_EXCL
) {
2200 * We let the host file system do O_EXCL check
2201 * We should not reclaim such fd
2203 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2206 iounit
= get_iounit(pdu
, &fidp
->path
);
2207 stat_to_qid(&stbuf
, &qid
);
2208 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2213 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2214 qid
.type
, qid
.version
, qid
.path
, iounit
);
2218 pdu_complete(pdu
, err
);
2219 v9fs_string_free(&name
);
2220 v9fs_string_free(&extension
);
2221 v9fs_path_free(&path
);
2224 static void v9fs_symlink(void *opaque
)
2226 V9fsPDU
*pdu
= opaque
;
2229 V9fsFidState
*dfidp
;
2237 v9fs_string_init(&name
);
2238 v9fs_string_init(&symname
);
2239 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2243 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2245 dfidp
= get_fid(pdu
, dfid
);
2246 if (dfidp
== NULL
) {
2250 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2254 stat_to_qid(&stbuf
, &qid
);
2255 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2260 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2261 qid
.type
, qid
.version
, qid
.path
);
2263 put_fid(pdu
, dfidp
);
2265 pdu_complete(pdu
, err
);
2266 v9fs_string_free(&name
);
2267 v9fs_string_free(&symname
);
2270 static void v9fs_flush(void *opaque
)
2275 V9fsPDU
*cancel_pdu
;
2276 V9fsPDU
*pdu
= opaque
;
2277 V9fsState
*s
= pdu
->s
;
2279 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2281 pdu_complete(pdu
, err
);
2284 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2286 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2287 if (cancel_pdu
->tag
== tag
) {
2292 cancel_pdu
->cancelled
= 1;
2294 * Wait for pdu to complete.
2296 qemu_co_queue_wait(&cancel_pdu
->complete
);
2297 cancel_pdu
->cancelled
= 0;
2298 pdu_free(cancel_pdu
);
2300 pdu_complete(pdu
, 7);
2303 static void v9fs_link(void *opaque
)
2305 V9fsPDU
*pdu
= opaque
;
2306 int32_t dfid
, oldfid
;
2307 V9fsFidState
*dfidp
, *oldfidp
;
2312 v9fs_string_init(&name
);
2313 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2317 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2319 dfidp
= get_fid(pdu
, dfid
);
2320 if (dfidp
== NULL
) {
2325 oldfidp
= get_fid(pdu
, oldfid
);
2326 if (oldfidp
== NULL
) {
2330 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2335 put_fid(pdu
, dfidp
);
2337 v9fs_string_free(&name
);
2338 pdu_complete(pdu
, err
);
2341 /* Only works with path name based fid */
2342 static void v9fs_remove(void *opaque
)
2348 V9fsPDU
*pdu
= opaque
;
2350 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2354 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2356 fidp
= get_fid(pdu
, fid
);
2361 /* if fs driver is not path based, return EOPNOTSUPP */
2362 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2367 * IF the file is unlinked, we cannot reopen
2368 * the file later. So don't reclaim fd
2370 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2374 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2379 /* For TREMOVE we need to clunk the fid even on failed remove */
2380 clunk_fid(pdu
->s
, fidp
->fid
);
2383 pdu_complete(pdu
, err
);
2386 static void v9fs_unlinkat(void *opaque
)
2390 int32_t dfid
, flags
;
2393 V9fsFidState
*dfidp
;
2394 V9fsPDU
*pdu
= opaque
;
2396 v9fs_string_init(&name
);
2397 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2401 dfidp
= get_fid(pdu
, dfid
);
2402 if (dfidp
== NULL
) {
2407 * IF the file is unlinked, we cannot reopen
2408 * the file later. So don't reclaim fd
2410 v9fs_path_init(&path
);
2411 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2415 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2419 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2424 put_fid(pdu
, dfidp
);
2425 v9fs_path_free(&path
);
2427 pdu_complete(pdu
, err
);
2428 v9fs_string_free(&name
);
2432 /* Only works with path name based fid */
2433 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2434 int32_t newdirfid
, V9fsString
*name
)
2439 V9fsFidState
*tfidp
;
2440 V9fsState
*s
= pdu
->s
;
2441 V9fsFidState
*dirfidp
= NULL
;
2442 char *old_name
, *new_name
;
2444 v9fs_path_init(&new_path
);
2445 if (newdirfid
!= -1) {
2446 dirfidp
= get_fid(pdu
, newdirfid
);
2447 if (dirfidp
== NULL
) {
2451 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2452 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2454 old_name
= fidp
->path
.data
;
2455 end
= strrchr(old_name
, '/');
2461 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2462 strncat(new_name
, old_name
, end
- old_name
);
2463 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2464 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2467 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2472 * Fixup fid's pointing to the old name to
2473 * start pointing to the new name
2475 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2476 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2477 /* replace the name */
2478 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2483 put_fid(pdu
, dirfidp
);
2485 v9fs_path_free(&new_path
);
2490 /* Only works with path name based fid */
2491 static void v9fs_rename(void *opaque
)
2499 V9fsPDU
*pdu
= opaque
;
2500 V9fsState
*s
= pdu
->s
;
2502 v9fs_string_init(&name
);
2503 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2507 fidp
= get_fid(pdu
, fid
);
2512 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2513 /* if fs driver is not path based, return EOPNOTSUPP */
2514 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2518 v9fs_path_write_lock(s
);
2519 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2520 v9fs_path_unlock(s
);
2527 pdu_complete(pdu
, err
);
2528 v9fs_string_free(&name
);
2531 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2532 V9fsString
*old_name
, V9fsPath
*newdir
,
2533 V9fsString
*new_name
)
2535 V9fsFidState
*tfidp
;
2536 V9fsPath oldpath
, newpath
;
2537 V9fsState
*s
= pdu
->s
;
2540 v9fs_path_init(&oldpath
);
2541 v9fs_path_init(&newpath
);
2542 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2543 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2546 * Fixup fid's pointing to the old name to
2547 * start pointing to the new name
2549 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2550 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2551 /* replace the name */
2552 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2555 v9fs_path_free(&oldpath
);
2556 v9fs_path_free(&newpath
);
2559 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2560 V9fsString
*old_name
, int32_t newdirfid
,
2561 V9fsString
*new_name
)
2564 V9fsState
*s
= pdu
->s
;
2565 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2567 olddirfidp
= get_fid(pdu
, olddirfid
);
2568 if (olddirfidp
== NULL
) {
2572 if (newdirfid
!= -1) {
2573 newdirfidp
= get_fid(pdu
, newdirfid
);
2574 if (newdirfidp
== NULL
) {
2579 newdirfidp
= get_fid(pdu
, olddirfid
);
2582 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2583 &newdirfidp
->path
, new_name
);
2587 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2588 /* Only for path based fid we need to do the below fixup */
2589 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2590 &newdirfidp
->path
, new_name
);
2594 put_fid(pdu
, olddirfidp
);
2597 put_fid(pdu
, newdirfidp
);
2602 static void v9fs_renameat(void *opaque
)
2606 V9fsPDU
*pdu
= opaque
;
2607 V9fsState
*s
= pdu
->s
;
2608 int32_t olddirfid
, newdirfid
;
2609 V9fsString old_name
, new_name
;
2611 v9fs_string_init(&old_name
);
2612 v9fs_string_init(&new_name
);
2613 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2614 &old_name
, &newdirfid
, &new_name
);
2619 v9fs_path_write_lock(s
);
2620 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2621 &old_name
, newdirfid
, &new_name
);
2622 v9fs_path_unlock(s
);
2628 pdu_complete(pdu
, err
);
2629 v9fs_string_free(&old_name
);
2630 v9fs_string_free(&new_name
);
2633 static void v9fs_wstat(void *opaque
)
2642 V9fsPDU
*pdu
= opaque
;
2644 v9fs_stat_init(&v9stat
);
2645 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2649 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2650 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2652 fidp
= get_fid(pdu
, fid
);
2657 /* do we need to sync the file? */
2658 if (donttouch_stat(&v9stat
)) {
2659 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2662 if (v9stat
.mode
!= -1) {
2664 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2668 v9_mode
= stat_to_v9mode(&stbuf
);
2669 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2670 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2671 /* Attempting to change the type */
2675 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2676 v9mode_to_mode(v9stat
.mode
,
2677 &v9stat
.extension
));
2682 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2683 struct timespec times
[2];
2684 if (v9stat
.atime
!= -1) {
2685 times
[0].tv_sec
= v9stat
.atime
;
2686 times
[0].tv_nsec
= 0;
2688 times
[0].tv_nsec
= UTIME_OMIT
;
2690 if (v9stat
.mtime
!= -1) {
2691 times
[1].tv_sec
= v9stat
.mtime
;
2692 times
[1].tv_nsec
= 0;
2694 times
[1].tv_nsec
= UTIME_OMIT
;
2696 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2701 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2702 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2707 if (v9stat
.name
.size
!= 0) {
2708 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2713 if (v9stat
.length
!= -1) {
2714 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2723 v9fs_stat_free(&v9stat
);
2724 pdu_complete(pdu
, err
);
2727 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2739 int32_t bsize_factor
;
2742 * compute bsize factor based on host file system block size
2745 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2746 if (!bsize_factor
) {
2749 f_type
= stbuf
->f_type
;
2750 f_bsize
= stbuf
->f_bsize
;
2751 f_bsize
*= bsize_factor
;
2753 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2754 * adjust(divide) the number of blocks, free blocks and available
2755 * blocks by bsize factor
2757 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2758 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2759 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2760 f_files
= stbuf
->f_files
;
2761 f_ffree
= stbuf
->f_ffree
;
2762 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2763 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2764 f_namelen
= stbuf
->f_namelen
;
2766 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2767 f_type
, f_bsize
, f_blocks
, f_bfree
,
2768 f_bavail
, f_files
, f_ffree
,
2769 fsid_val
, f_namelen
);
2772 static void v9fs_statfs(void *opaque
)
2778 struct statfs stbuf
;
2779 V9fsPDU
*pdu
= opaque
;
2780 V9fsState
*s
= pdu
->s
;
2782 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2786 fidp
= get_fid(pdu
, fid
);
2791 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2795 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2803 pdu_complete(pdu
, retval
);
2806 static void v9fs_mknod(void *opaque
)
2819 V9fsPDU
*pdu
= opaque
;
2821 v9fs_string_init(&name
);
2822 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2823 &major
, &minor
, &gid
);
2827 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2829 fidp
= get_fid(pdu
, fid
);
2834 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2835 makedev(major
, minor
), mode
, &stbuf
);
2839 stat_to_qid(&stbuf
, &qid
);
2840 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2845 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2846 qid
.type
, qid
.version
, qid
.path
);
2850 pdu_complete(pdu
, err
);
2851 v9fs_string_free(&name
);
2855 * Implement posix byte range locking code
2856 * Server side handling of locking code is very simple, because 9p server in
2857 * QEMU can handle only one client. And most of the lock handling
2858 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2859 * do any thing in * qemu 9p server side lock code path.
2860 * So when a TLOCK request comes, always return success
2862 static void v9fs_lock(void *opaque
)
2869 int32_t fid
, err
= 0;
2870 V9fsPDU
*pdu
= opaque
;
2872 status
= P9_LOCK_ERROR
;
2873 v9fs_string_init(&flock
.client_id
);
2874 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2875 &flock
.flags
, &flock
.start
, &flock
.length
,
2876 &flock
.proc_id
, &flock
.client_id
);
2880 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2881 flock
.type
, flock
.start
, flock
.length
);
2884 /* We support only block flag now (that too ignored currently) */
2885 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2889 fidp
= get_fid(pdu
, fid
);
2894 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2898 status
= P9_LOCK_SUCCESS
;
2902 err
= pdu_marshal(pdu
, offset
, "b", status
);
2906 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2907 pdu_complete(pdu
, err
);
2908 v9fs_string_free(&flock
.client_id
);
2912 * When a TGETLOCK request comes, always return success because all lock
2913 * handling is done by client's VFS layer.
2915 static void v9fs_getlock(void *opaque
)
2921 int32_t fid
, err
= 0;
2922 V9fsPDU
*pdu
= opaque
;
2924 v9fs_string_init(&glock
.client_id
);
2925 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
2926 &glock
.start
, &glock
.length
, &glock
.proc_id
,
2931 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2932 glock
.type
, glock
.start
, glock
.length
);
2934 fidp
= get_fid(pdu
, fid
);
2939 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2943 glock
.type
= P9_LOCK_TYPE_UNLCK
;
2944 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
2945 glock
.start
, glock
.length
, glock
.proc_id
,
2951 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
2952 glock
.length
, glock
.proc_id
);
2956 pdu_complete(pdu
, err
);
2957 v9fs_string_free(&glock
.client_id
);
2960 static void v9fs_mkdir(void *opaque
)
2962 V9fsPDU
*pdu
= opaque
;
2973 v9fs_string_init(&name
);
2974 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2978 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
2980 fidp
= get_fid(pdu
, fid
);
2985 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2989 stat_to_qid(&stbuf
, &qid
);
2990 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2995 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
2996 qid
.type
, qid
.version
, qid
.path
, err
);
3000 pdu_complete(pdu
, err
);
3001 v9fs_string_free(&name
);
3004 static void v9fs_xattrwalk(void *opaque
)
3010 int32_t fid
, newfid
;
3011 V9fsFidState
*file_fidp
;
3012 V9fsFidState
*xattr_fidp
= NULL
;
3013 V9fsPDU
*pdu
= opaque
;
3014 V9fsState
*s
= pdu
->s
;
3016 v9fs_string_init(&name
);
3017 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3021 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3023 file_fidp
= get_fid(pdu
, fid
);
3024 if (file_fidp
== NULL
) {
3028 xattr_fidp
= alloc_fid(s
, newfid
);
3029 if (xattr_fidp
== NULL
) {
3033 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3034 if (name
.data
== NULL
) {
3036 * listxattr request. Get the size first
3038 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3041 clunk_fid(s
, xattr_fidp
->fid
);
3045 * Read the xattr value
3047 xattr_fidp
->fs
.xattr
.len
= size
;
3048 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3049 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3051 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3052 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3053 xattr_fidp
->fs
.xattr
.value
,
3054 xattr_fidp
->fs
.xattr
.len
);
3056 clunk_fid(s
, xattr_fidp
->fid
);
3060 err
= pdu_marshal(pdu
, offset
, "q", size
);
3067 * specific xattr fid. We check for xattr
3068 * presence also collect the xattr size
3070 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3074 clunk_fid(s
, xattr_fidp
->fid
);
3078 * Read the xattr value
3080 xattr_fidp
->fs
.xattr
.len
= size
;
3081 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3082 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3084 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3085 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3086 &name
, xattr_fidp
->fs
.xattr
.value
,
3087 xattr_fidp
->fs
.xattr
.len
);
3089 clunk_fid(s
, xattr_fidp
->fid
);
3093 err
= pdu_marshal(pdu
, offset
, "q", size
);
3099 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3101 put_fid(pdu
, file_fidp
);
3103 put_fid(pdu
, xattr_fidp
);
3106 pdu_complete(pdu
, err
);
3107 v9fs_string_free(&name
);
3110 static void v9fs_xattrcreate(void *opaque
)
3118 V9fsFidState
*file_fidp
;
3119 V9fsFidState
*xattr_fidp
;
3120 V9fsPDU
*pdu
= opaque
;
3122 v9fs_string_init(&name
);
3123 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3127 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3129 file_fidp
= get_fid(pdu
, fid
);
3130 if (file_fidp
== NULL
) {
3134 /* Make the file fid point to xattr */
3135 xattr_fidp
= file_fidp
;
3136 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3137 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3138 xattr_fidp
->fs
.xattr
.len
= size
;
3139 xattr_fidp
->fs
.xattr
.flags
= flags
;
3140 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3141 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3142 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3144 put_fid(pdu
, file_fidp
);
3146 pdu_complete(pdu
, err
);
3147 v9fs_string_free(&name
);
3150 static void v9fs_readlink(void *opaque
)
3152 V9fsPDU
*pdu
= opaque
;
3159 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3163 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3164 fidp
= get_fid(pdu
, fid
);
3170 v9fs_string_init(&target
);
3171 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3175 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3177 v9fs_string_free(&target
);
3181 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3182 v9fs_string_free(&target
);
3186 pdu_complete(pdu
, err
);
3189 static CoroutineEntry
*pdu_co_handlers
[] = {
3190 [P9_TREADDIR
] = v9fs_readdir
,
3191 [P9_TSTATFS
] = v9fs_statfs
,
3192 [P9_TGETATTR
] = v9fs_getattr
,
3193 [P9_TSETATTR
] = v9fs_setattr
,
3194 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3195 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3196 [P9_TMKNOD
] = v9fs_mknod
,
3197 [P9_TRENAME
] = v9fs_rename
,
3198 [P9_TLOCK
] = v9fs_lock
,
3199 [P9_TGETLOCK
] = v9fs_getlock
,
3200 [P9_TRENAMEAT
] = v9fs_renameat
,
3201 [P9_TREADLINK
] = v9fs_readlink
,
3202 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3203 [P9_TMKDIR
] = v9fs_mkdir
,
3204 [P9_TVERSION
] = v9fs_version
,
3205 [P9_TLOPEN
] = v9fs_open
,
3206 [P9_TATTACH
] = v9fs_attach
,
3207 [P9_TSTAT
] = v9fs_stat
,
3208 [P9_TWALK
] = v9fs_walk
,
3209 [P9_TCLUNK
] = v9fs_clunk
,
3210 [P9_TFSYNC
] = v9fs_fsync
,
3211 [P9_TOPEN
] = v9fs_open
,
3212 [P9_TREAD
] = v9fs_read
,
3214 [P9_TAUTH
] = v9fs_auth
,
3216 [P9_TFLUSH
] = v9fs_flush
,
3217 [P9_TLINK
] = v9fs_link
,
3218 [P9_TSYMLINK
] = v9fs_symlink
,
3219 [P9_TCREATE
] = v9fs_create
,
3220 [P9_TLCREATE
] = v9fs_lcreate
,
3221 [P9_TWRITE
] = v9fs_write
,
3222 [P9_TWSTAT
] = v9fs_wstat
,
3223 [P9_TREMOVE
] = v9fs_remove
,
3226 static void v9fs_op_not_supp(void *opaque
)
3228 V9fsPDU
*pdu
= opaque
;
3229 pdu_complete(pdu
, -EOPNOTSUPP
);
3232 static void v9fs_fs_ro(void *opaque
)
3234 V9fsPDU
*pdu
= opaque
;
3235 pdu_complete(pdu
, -EROFS
);
3238 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3265 void pdu_submit(V9fsPDU
*pdu
)
3268 CoroutineEntry
*handler
;
3269 V9fsState
*s
= pdu
->s
;
3271 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3272 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3273 handler
= v9fs_op_not_supp
;
3275 handler
= pdu_co_handlers
[pdu
->id
];
3278 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3279 handler
= v9fs_fs_ro
;
3281 co
= qemu_coroutine_create(handler
, pdu
);
3282 qemu_coroutine_enter(co
);
3285 /* Returns 0 on success, 1 on failure. */
3286 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3288 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
3295 /* initialize pdu allocator */
3296 QLIST_INIT(&s
->free_list
);
3297 QLIST_INIT(&s
->active_list
);
3298 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3299 QLIST_INSERT_HEAD(&s
->free_list
, &v
->pdus
[i
], next
);
3304 v9fs_path_init(&path
);
3306 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3309 /* We don't have a fsdev identified by fsdev_id */
3310 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3312 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3316 if (!s
->fsconf
.tag
) {
3317 /* we haven't specified a mount_tag */
3318 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3319 s
->fsconf
.fsdev_id
);
3323 s
->ctx
.export_flags
= fse
->export_flags
;
3324 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3325 s
->ctx
.exops
.get_st_gen
= NULL
;
3326 len
= strlen(s
->fsconf
.tag
);
3327 if (len
> MAX_TAG_LEN
- 1) {
3328 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3329 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3333 s
->tag
= g_strdup(s
->fsconf
.tag
);
3339 qemu_co_rwlock_init(&s
->rename_lock
);
3341 if (s
->ops
->init(&s
->ctx
) < 0) {
3342 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3343 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3348 * Check details of export path, We need to use fs driver
3349 * call back to do that. Since we are in the init path, we don't
3350 * use co-routines here.
3352 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3354 "error in converting name to path %s", strerror(errno
));
3357 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3358 error_setg(errp
, "share path %s does not exist", fse
->path
);
3360 } else if (!S_ISDIR(stat
.st_mode
)) {
3361 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3364 v9fs_path_free(&path
);
3369 g_free(s
->ctx
.fs_root
);
3371 v9fs_path_free(&path
);
3376 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3378 g_free(s
->ctx
.fs_root
);
3382 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3385 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3386 error_report("Failed to get the resource limit");
3389 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3390 open_fd_rc
= rlim
.rlim_cur
/2;