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 bool name_is_illegal(const char *name
)
1261 return !*name
|| strchr(name
, '/') != NULL
;
1264 static void v9fs_walk(void *opaque
)
1267 V9fsQID
*qids
= NULL
;
1269 V9fsPath dpath
, path
;
1273 int32_t fid
, newfid
;
1274 V9fsString
*wnames
= NULL
;
1276 V9fsFidState
*newfidp
= NULL
;
1277 V9fsPDU
*pdu
= opaque
;
1278 V9fsState
*s
= pdu
->s
;
1280 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1282 pdu_complete(pdu
, err
);
1287 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1289 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1290 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1291 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1292 for (i
= 0; i
< nwnames
; i
++) {
1293 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1297 if (name_is_illegal(wnames
[i
].data
)) {
1303 } else if (nwnames
> P9_MAXWELEM
) {
1307 fidp
= get_fid(pdu
, fid
);
1312 v9fs_path_init(&dpath
);
1313 v9fs_path_init(&path
);
1315 * Both dpath and path initially poin to fidp.
1316 * Needed to handle request with nwnames == 0
1318 v9fs_path_copy(&dpath
, &fidp
->path
);
1319 v9fs_path_copy(&path
, &fidp
->path
);
1320 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1321 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1325 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1329 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1330 v9fs_path_copy(&dpath
, &path
);
1332 if (fid
== newfid
) {
1333 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1334 v9fs_path_copy(&fidp
->path
, &path
);
1336 newfidp
= alloc_fid(s
, newfid
);
1337 if (newfidp
== NULL
) {
1341 newfidp
->uid
= fidp
->uid
;
1342 v9fs_path_copy(&newfidp
->path
, &path
);
1344 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1345 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1349 put_fid(pdu
, newfidp
);
1351 v9fs_path_free(&dpath
);
1352 v9fs_path_free(&path
);
1354 pdu_complete(pdu
, err
);
1355 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1356 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1357 v9fs_string_free(&wnames
[name_idx
]);
1364 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1366 struct statfs stbuf
;
1368 V9fsState
*s
= pdu
->s
;
1371 * iounit should be multiples of f_bsize (host filesystem block size
1372 * and as well as less than (client msize - P9_IOHDRSZ))
1374 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1375 iounit
= stbuf
.f_bsize
;
1376 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1379 iounit
= s
->msize
- P9_IOHDRSZ
;
1384 static void v9fs_open(void *opaque
)
1395 V9fsPDU
*pdu
= opaque
;
1396 V9fsState
*s
= pdu
->s
;
1398 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1399 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1402 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1408 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1410 fidp
= get_fid(pdu
, fid
);
1415 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1417 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1421 stat_to_qid(&stbuf
, &qid
);
1422 if (S_ISDIR(stbuf
.st_mode
)) {
1423 err
= v9fs_co_opendir(pdu
, fidp
);
1427 fidp
->fid_type
= P9_FID_DIR
;
1428 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1434 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1435 flags
= get_dotl_openflags(s
, mode
);
1437 flags
= omode_to_uflags(mode
);
1439 if (is_ro_export(&s
->ctx
)) {
1440 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1441 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1446 err
= v9fs_co_open(pdu
, fidp
, flags
);
1450 fidp
->fid_type
= P9_FID_FILE
;
1451 fidp
->open_flags
= flags
;
1452 if (flags
& O_EXCL
) {
1454 * We let the host file system do O_EXCL check
1455 * We should not reclaim such fd
1457 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1459 iounit
= get_iounit(pdu
, &fidp
->path
);
1460 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1466 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1467 qid
.type
, qid
.version
, qid
.path
, iounit
);
1471 pdu_complete(pdu
, err
);
1474 static void v9fs_lcreate(void *opaque
)
1476 int32_t dfid
, flags
, mode
;
1485 V9fsPDU
*pdu
= opaque
;
1487 v9fs_string_init(&name
);
1488 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1489 &name
, &flags
, &mode
, &gid
);
1493 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1495 if (name_is_illegal(name
.data
)) {
1500 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1505 fidp
= get_fid(pdu
, dfid
);
1511 flags
= get_dotl_openflags(pdu
->s
, flags
);
1512 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1513 flags
| O_CREAT
, mode
, &stbuf
);
1517 fidp
->fid_type
= P9_FID_FILE
;
1518 fidp
->open_flags
= flags
;
1519 if (flags
& O_EXCL
) {
1521 * We let the host file system do O_EXCL check
1522 * We should not reclaim such fd
1524 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1526 iounit
= get_iounit(pdu
, &fidp
->path
);
1527 stat_to_qid(&stbuf
, &qid
);
1528 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1533 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1534 qid
.type
, qid
.version
, qid
.path
, iounit
);
1538 pdu_complete(pdu
, err
);
1539 v9fs_string_free(&name
);
1542 static void v9fs_fsync(void *opaque
)
1549 V9fsPDU
*pdu
= opaque
;
1551 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1555 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1557 fidp
= get_fid(pdu
, fid
);
1562 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1568 pdu_complete(pdu
, err
);
1571 static void v9fs_clunk(void *opaque
)
1577 V9fsPDU
*pdu
= opaque
;
1578 V9fsState
*s
= pdu
->s
;
1580 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1584 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1586 fidp
= clunk_fid(s
, fid
);
1592 * Bump the ref so that put_fid will
1596 err
= put_fid(pdu
, fidp
);
1601 pdu_complete(pdu
, err
);
1604 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1605 uint64_t off
, uint32_t max_count
)
1611 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
1612 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
1614 xattr_len
= fidp
->fs
.xattr
.len
;
1615 read_count
= xattr_len
- off
;
1616 if (read_count
> max_count
) {
1617 read_count
= max_count
;
1618 } else if (read_count
< 0) {
1620 * read beyond XATTR value
1624 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1630 err
= v9fs_pack(elem
->in_sg
, elem
->in_num
, offset
,
1631 ((char *)fidp
->fs
.xattr
.value
) + off
,
1640 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1641 V9fsFidState
*fidp
, uint32_t max_count
)
1648 off_t saved_dir_pos
;
1649 struct dirent
*dent
;
1651 /* save the directory position */
1652 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1653 if (saved_dir_pos
< 0) {
1654 return saved_dir_pos
;
1658 v9fs_path_init(&path
);
1660 v9fs_readdir_lock(&fidp
->fs
.dir
);
1662 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1666 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1670 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1674 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1678 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1679 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1681 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1683 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1684 /* Ran out of buffer. Set dir back to old position and return */
1685 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1686 v9fs_stat_free(&v9stat
);
1687 v9fs_path_free(&path
);
1691 v9fs_stat_free(&v9stat
);
1692 v9fs_path_free(&path
);
1693 saved_dir_pos
= dent
->d_off
;
1696 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1698 v9fs_path_free(&path
);
1706 * Create a QEMUIOVector for a sub-region of PDU iovecs
1708 * @qiov: uninitialized QEMUIOVector
1709 * @skip: number of bytes to skip from beginning of PDU
1710 * @size: number of bytes to include
1711 * @is_write: true - write, false - read
1713 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1714 * with qemu_iovec_destroy().
1716 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1717 size_t skip
, size_t size
,
1724 virtio_init_iov_from_pdu(pdu
, &iov
, &niov
, is_write
);
1726 qemu_iovec_init_external(&elem
, iov
, niov
);
1727 qemu_iovec_init(qiov
, niov
);
1728 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1731 static void v9fs_read(void *opaque
)
1740 V9fsPDU
*pdu
= opaque
;
1741 V9fsState
*s
= pdu
->s
;
1743 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1747 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1749 fidp
= get_fid(pdu
, fid
);
1754 if (fidp
->fid_type
== P9_FID_DIR
) {
1757 v9fs_co_rewinddir(pdu
, fidp
);
1759 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1764 err
= pdu_marshal(pdu
, offset
, "d", count
);
1768 err
+= offset
+ count
;
1769 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1770 QEMUIOVector qiov_full
;
1774 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1775 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1777 qemu_iovec_reset(&qiov
);
1778 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1780 print_sg(qiov
.iov
, qiov
.niov
);
1782 /* Loop in case of EINTR */
1784 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1789 } while (len
== -EINTR
&& !pdu
->cancelled
);
1791 /* IO error return the error */
1795 } while (count
< max_count
&& len
> 0);
1796 err
= pdu_marshal(pdu
, offset
, "d", count
);
1800 err
+= offset
+ count
;
1801 qemu_iovec_destroy(&qiov
);
1802 qemu_iovec_destroy(&qiov_full
);
1803 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1804 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1808 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1812 pdu_complete(pdu
, err
);
1815 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1818 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1819 * size of type (1) + size of name.size (2) + strlen(name.data)
1821 return 24 + v9fs_string_size(name
);
1824 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1825 V9fsFidState
*fidp
, int32_t max_count
)
1832 off_t saved_dir_pos
;
1833 struct dirent
*dent
;
1835 /* save the directory position */
1836 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1837 if (saved_dir_pos
< 0) {
1838 return saved_dir_pos
;
1842 v9fs_readdir_lock(&fidp
->fs
.dir
);
1844 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1848 v9fs_string_init(&name
);
1849 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1850 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1851 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1853 /* Ran out of buffer. Set dir back to old position and return */
1854 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1855 v9fs_string_free(&name
);
1859 * Fill up just the path field of qid because the client uses
1860 * only that. To fill the entire qid structure we will have
1861 * to stat each dirent found, which is expensive
1863 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1864 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1865 /* Fill the other fields with dummy values */
1869 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1870 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1872 dent
->d_type
, &name
);
1874 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1877 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1878 v9fs_string_free(&name
);
1882 v9fs_string_free(&name
);
1883 saved_dir_pos
= dent
->d_off
;
1886 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1894 static void v9fs_readdir(void *opaque
)
1900 uint64_t initial_offset
;
1903 V9fsPDU
*pdu
= opaque
;
1905 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1906 &initial_offset
, &max_count
);
1910 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1912 fidp
= get_fid(pdu
, fid
);
1917 if (!fidp
->fs
.dir
.stream
) {
1921 if (initial_offset
== 0) {
1922 v9fs_co_rewinddir(pdu
, fidp
);
1924 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1926 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1931 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1935 retval
+= count
+ offset
;
1936 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1940 pdu_complete(pdu
, retval
);
1943 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1944 uint64_t off
, uint32_t count
,
1945 struct iovec
*sg
, int cnt
)
1954 xattr_len
= fidp
->fs
.xattr
.len
;
1955 write_count
= xattr_len
- off
;
1956 if (write_count
> count
) {
1957 write_count
= count
;
1958 } else if (write_count
< 0) {
1960 * write beyond XATTR value len specified in
1966 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1971 fidp
->fs
.xattr
.copied_len
+= write_count
;
1973 * Now copy the content from sg list
1975 for (i
= 0; i
< cnt
; i
++) {
1976 if (write_count
> sg
[i
].iov_len
) {
1977 to_copy
= sg
[i
].iov_len
;
1979 to_copy
= write_count
;
1981 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1982 /* updating vs->off since we are not using below */
1984 write_count
-= to_copy
;
1990 static void v9fs_write(void *opaque
)
2000 V9fsPDU
*pdu
= opaque
;
2001 V9fsState
*s
= pdu
->s
;
2002 QEMUIOVector qiov_full
;
2005 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2007 pdu_complete(pdu
, err
);
2011 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2012 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2014 fidp
= get_fid(pdu
, fid
);
2019 if (fidp
->fid_type
== P9_FID_FILE
) {
2020 if (fidp
->fs
.fd
== -1) {
2024 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2026 * setxattr operation
2028 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2029 qiov_full
.iov
, qiov_full
.niov
);
2035 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2037 qemu_iovec_reset(&qiov
);
2038 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2040 print_sg(qiov
.iov
, qiov
.niov
);
2042 /* Loop in case of EINTR */
2044 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2049 } while (len
== -EINTR
&& !pdu
->cancelled
);
2051 /* IO error return the error */
2055 } while (total
< count
&& len
> 0);
2058 err
= pdu_marshal(pdu
, offset
, "d", total
);
2063 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2065 qemu_iovec_destroy(&qiov
);
2069 qemu_iovec_destroy(&qiov_full
);
2070 pdu_complete(pdu
, err
);
2073 static void v9fs_create(void *opaque
)
2085 V9fsString extension
;
2087 V9fsPDU
*pdu
= opaque
;
2089 v9fs_path_init(&path
);
2090 v9fs_string_init(&name
);
2091 v9fs_string_init(&extension
);
2092 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2093 &perm
, &mode
, &extension
);
2097 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2099 if (name_is_illegal(name
.data
)) {
2104 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2109 fidp
= get_fid(pdu
, fid
);
2114 if (perm
& P9_STAT_MODE_DIR
) {
2115 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2116 fidp
->uid
, -1, &stbuf
);
2120 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2124 v9fs_path_copy(&fidp
->path
, &path
);
2125 err
= v9fs_co_opendir(pdu
, fidp
);
2129 fidp
->fid_type
= P9_FID_DIR
;
2130 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2131 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2132 extension
.data
, -1 , &stbuf
);
2136 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2140 v9fs_path_copy(&fidp
->path
, &path
);
2141 } else if (perm
& P9_STAT_MODE_LINK
) {
2142 int32_t ofid
= atoi(extension
.data
);
2143 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2144 if (ofidp
== NULL
) {
2148 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2149 put_fid(pdu
, ofidp
);
2153 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2155 fidp
->fid_type
= P9_FID_NONE
;
2158 v9fs_path_copy(&fidp
->path
, &path
);
2159 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2161 fidp
->fid_type
= P9_FID_NONE
;
2164 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2166 uint32_t major
, minor
;
2169 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2186 nmode
|= perm
& 0777;
2187 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2188 makedev(major
, minor
), nmode
, &stbuf
);
2192 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2196 v9fs_path_copy(&fidp
->path
, &path
);
2197 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2198 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2199 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2203 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2207 v9fs_path_copy(&fidp
->path
, &path
);
2208 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2209 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2210 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2214 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2218 v9fs_path_copy(&fidp
->path
, &path
);
2220 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2221 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2225 fidp
->fid_type
= P9_FID_FILE
;
2226 fidp
->open_flags
= omode_to_uflags(mode
);
2227 if (fidp
->open_flags
& O_EXCL
) {
2229 * We let the host file system do O_EXCL check
2230 * We should not reclaim such fd
2232 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2235 iounit
= get_iounit(pdu
, &fidp
->path
);
2236 stat_to_qid(&stbuf
, &qid
);
2237 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2242 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2243 qid
.type
, qid
.version
, qid
.path
, iounit
);
2247 pdu_complete(pdu
, err
);
2248 v9fs_string_free(&name
);
2249 v9fs_string_free(&extension
);
2250 v9fs_path_free(&path
);
2253 static void v9fs_symlink(void *opaque
)
2255 V9fsPDU
*pdu
= opaque
;
2258 V9fsFidState
*dfidp
;
2266 v9fs_string_init(&name
);
2267 v9fs_string_init(&symname
);
2268 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2272 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2274 if (name_is_illegal(name
.data
)) {
2279 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2284 dfidp
= get_fid(pdu
, dfid
);
2285 if (dfidp
== NULL
) {
2289 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2293 stat_to_qid(&stbuf
, &qid
);
2294 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2299 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2300 qid
.type
, qid
.version
, qid
.path
);
2302 put_fid(pdu
, dfidp
);
2304 pdu_complete(pdu
, err
);
2305 v9fs_string_free(&name
);
2306 v9fs_string_free(&symname
);
2309 static void v9fs_flush(void *opaque
)
2314 V9fsPDU
*cancel_pdu
;
2315 V9fsPDU
*pdu
= opaque
;
2316 V9fsState
*s
= pdu
->s
;
2318 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2320 pdu_complete(pdu
, err
);
2323 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2325 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2326 if (cancel_pdu
->tag
== tag
) {
2331 cancel_pdu
->cancelled
= 1;
2333 * Wait for pdu to complete.
2335 qemu_co_queue_wait(&cancel_pdu
->complete
);
2336 cancel_pdu
->cancelled
= 0;
2337 pdu_free(cancel_pdu
);
2339 pdu_complete(pdu
, 7);
2342 static void v9fs_link(void *opaque
)
2344 V9fsPDU
*pdu
= opaque
;
2345 int32_t dfid
, oldfid
;
2346 V9fsFidState
*dfidp
, *oldfidp
;
2351 v9fs_string_init(&name
);
2352 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2356 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2358 if (name_is_illegal(name
.data
)) {
2363 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2368 dfidp
= get_fid(pdu
, dfid
);
2369 if (dfidp
== NULL
) {
2374 oldfidp
= get_fid(pdu
, oldfid
);
2375 if (oldfidp
== NULL
) {
2379 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2384 put_fid(pdu
, dfidp
);
2386 v9fs_string_free(&name
);
2387 pdu_complete(pdu
, err
);
2390 /* Only works with path name based fid */
2391 static void v9fs_remove(void *opaque
)
2397 V9fsPDU
*pdu
= opaque
;
2399 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2403 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2405 fidp
= get_fid(pdu
, fid
);
2410 /* if fs driver is not path based, return EOPNOTSUPP */
2411 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2416 * IF the file is unlinked, we cannot reopen
2417 * the file later. So don't reclaim fd
2419 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2423 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2428 /* For TREMOVE we need to clunk the fid even on failed remove */
2429 clunk_fid(pdu
->s
, fidp
->fid
);
2432 pdu_complete(pdu
, err
);
2435 static void v9fs_unlinkat(void *opaque
)
2439 int32_t dfid
, flags
;
2442 V9fsFidState
*dfidp
;
2443 V9fsPDU
*pdu
= opaque
;
2445 v9fs_string_init(&name
);
2446 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2451 if (name_is_illegal(name
.data
)) {
2456 if (!strcmp(".", name
.data
)) {
2461 if (!strcmp("..", name
.data
)) {
2466 dfidp
= get_fid(pdu
, dfid
);
2467 if (dfidp
== NULL
) {
2472 * IF the file is unlinked, we cannot reopen
2473 * the file later. So don't reclaim fd
2475 v9fs_path_init(&path
);
2476 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2480 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2484 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2489 put_fid(pdu
, dfidp
);
2490 v9fs_path_free(&path
);
2492 pdu_complete(pdu
, err
);
2493 v9fs_string_free(&name
);
2497 /* Only works with path name based fid */
2498 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2499 int32_t newdirfid
, V9fsString
*name
)
2504 V9fsFidState
*tfidp
;
2505 V9fsState
*s
= pdu
->s
;
2506 V9fsFidState
*dirfidp
= NULL
;
2507 char *old_name
, *new_name
;
2509 v9fs_path_init(&new_path
);
2510 if (newdirfid
!= -1) {
2511 dirfidp
= get_fid(pdu
, newdirfid
);
2512 if (dirfidp
== NULL
) {
2516 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2517 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2519 old_name
= fidp
->path
.data
;
2520 end
= strrchr(old_name
, '/');
2526 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2527 strncat(new_name
, old_name
, end
- old_name
);
2528 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2529 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2532 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2537 * Fixup fid's pointing to the old name to
2538 * start pointing to the new name
2540 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2541 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2542 /* replace the name */
2543 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2548 put_fid(pdu
, dirfidp
);
2550 v9fs_path_free(&new_path
);
2555 /* Only works with path name based fid */
2556 static void v9fs_rename(void *opaque
)
2564 V9fsPDU
*pdu
= opaque
;
2565 V9fsState
*s
= pdu
->s
;
2567 v9fs_string_init(&name
);
2568 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2573 if (name_is_illegal(name
.data
)) {
2578 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2583 fidp
= get_fid(pdu
, fid
);
2588 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2589 /* if fs driver is not path based, return EOPNOTSUPP */
2590 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2594 v9fs_path_write_lock(s
);
2595 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2596 v9fs_path_unlock(s
);
2603 pdu_complete(pdu
, err
);
2604 v9fs_string_free(&name
);
2607 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2608 V9fsString
*old_name
, V9fsPath
*newdir
,
2609 V9fsString
*new_name
)
2611 V9fsFidState
*tfidp
;
2612 V9fsPath oldpath
, newpath
;
2613 V9fsState
*s
= pdu
->s
;
2616 v9fs_path_init(&oldpath
);
2617 v9fs_path_init(&newpath
);
2618 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2619 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2622 * Fixup fid's pointing to the old name to
2623 * start pointing to the new name
2625 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2626 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2627 /* replace the name */
2628 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2631 v9fs_path_free(&oldpath
);
2632 v9fs_path_free(&newpath
);
2635 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2636 V9fsString
*old_name
, int32_t newdirfid
,
2637 V9fsString
*new_name
)
2640 V9fsState
*s
= pdu
->s
;
2641 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2643 olddirfidp
= get_fid(pdu
, olddirfid
);
2644 if (olddirfidp
== NULL
) {
2648 if (newdirfid
!= -1) {
2649 newdirfidp
= get_fid(pdu
, newdirfid
);
2650 if (newdirfidp
== NULL
) {
2655 newdirfidp
= get_fid(pdu
, olddirfid
);
2658 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2659 &newdirfidp
->path
, new_name
);
2663 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2664 /* Only for path based fid we need to do the below fixup */
2665 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2666 &newdirfidp
->path
, new_name
);
2670 put_fid(pdu
, olddirfidp
);
2673 put_fid(pdu
, newdirfidp
);
2678 static void v9fs_renameat(void *opaque
)
2682 V9fsPDU
*pdu
= opaque
;
2683 V9fsState
*s
= pdu
->s
;
2684 int32_t olddirfid
, newdirfid
;
2685 V9fsString old_name
, new_name
;
2687 v9fs_string_init(&old_name
);
2688 v9fs_string_init(&new_name
);
2689 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2690 &old_name
, &newdirfid
, &new_name
);
2695 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2700 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
2701 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
2706 v9fs_path_write_lock(s
);
2707 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2708 &old_name
, newdirfid
, &new_name
);
2709 v9fs_path_unlock(s
);
2715 pdu_complete(pdu
, err
);
2716 v9fs_string_free(&old_name
);
2717 v9fs_string_free(&new_name
);
2720 static void v9fs_wstat(void *opaque
)
2729 V9fsPDU
*pdu
= opaque
;
2731 v9fs_stat_init(&v9stat
);
2732 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2736 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2737 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2739 fidp
= get_fid(pdu
, fid
);
2744 /* do we need to sync the file? */
2745 if (donttouch_stat(&v9stat
)) {
2746 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2749 if (v9stat
.mode
!= -1) {
2751 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2755 v9_mode
= stat_to_v9mode(&stbuf
);
2756 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2757 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2758 /* Attempting to change the type */
2762 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2763 v9mode_to_mode(v9stat
.mode
,
2764 &v9stat
.extension
));
2769 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2770 struct timespec times
[2];
2771 if (v9stat
.atime
!= -1) {
2772 times
[0].tv_sec
= v9stat
.atime
;
2773 times
[0].tv_nsec
= 0;
2775 times
[0].tv_nsec
= UTIME_OMIT
;
2777 if (v9stat
.mtime
!= -1) {
2778 times
[1].tv_sec
= v9stat
.mtime
;
2779 times
[1].tv_nsec
= 0;
2781 times
[1].tv_nsec
= UTIME_OMIT
;
2783 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2788 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2789 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2794 if (v9stat
.name
.size
!= 0) {
2795 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2800 if (v9stat
.length
!= -1) {
2801 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2810 v9fs_stat_free(&v9stat
);
2811 pdu_complete(pdu
, err
);
2814 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2826 int32_t bsize_factor
;
2829 * compute bsize factor based on host file system block size
2832 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2833 if (!bsize_factor
) {
2836 f_type
= stbuf
->f_type
;
2837 f_bsize
= stbuf
->f_bsize
;
2838 f_bsize
*= bsize_factor
;
2840 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2841 * adjust(divide) the number of blocks, free blocks and available
2842 * blocks by bsize factor
2844 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2845 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2846 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2847 f_files
= stbuf
->f_files
;
2848 f_ffree
= stbuf
->f_ffree
;
2849 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2850 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2851 f_namelen
= stbuf
->f_namelen
;
2853 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2854 f_type
, f_bsize
, f_blocks
, f_bfree
,
2855 f_bavail
, f_files
, f_ffree
,
2856 fsid_val
, f_namelen
);
2859 static void v9fs_statfs(void *opaque
)
2865 struct statfs stbuf
;
2866 V9fsPDU
*pdu
= opaque
;
2867 V9fsState
*s
= pdu
->s
;
2869 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2873 fidp
= get_fid(pdu
, fid
);
2878 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2882 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2890 pdu_complete(pdu
, retval
);
2893 static void v9fs_mknod(void *opaque
)
2906 V9fsPDU
*pdu
= opaque
;
2908 v9fs_string_init(&name
);
2909 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2910 &major
, &minor
, &gid
);
2914 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2916 if (name_is_illegal(name
.data
)) {
2921 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2926 fidp
= get_fid(pdu
, fid
);
2931 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2932 makedev(major
, minor
), mode
, &stbuf
);
2936 stat_to_qid(&stbuf
, &qid
);
2937 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2942 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2943 qid
.type
, qid
.version
, qid
.path
);
2947 pdu_complete(pdu
, err
);
2948 v9fs_string_free(&name
);
2952 * Implement posix byte range locking code
2953 * Server side handling of locking code is very simple, because 9p server in
2954 * QEMU can handle only one client. And most of the lock handling
2955 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2956 * do any thing in * qemu 9p server side lock code path.
2957 * So when a TLOCK request comes, always return success
2959 static void v9fs_lock(void *opaque
)
2966 int32_t fid
, err
= 0;
2967 V9fsPDU
*pdu
= opaque
;
2969 status
= P9_LOCK_ERROR
;
2970 v9fs_string_init(&flock
.client_id
);
2971 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2972 &flock
.flags
, &flock
.start
, &flock
.length
,
2973 &flock
.proc_id
, &flock
.client_id
);
2977 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2978 flock
.type
, flock
.start
, flock
.length
);
2981 /* We support only block flag now (that too ignored currently) */
2982 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2986 fidp
= get_fid(pdu
, fid
);
2991 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2995 status
= P9_LOCK_SUCCESS
;
2999 err
= pdu_marshal(pdu
, offset
, "b", status
);
3003 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
3004 pdu_complete(pdu
, err
);
3005 v9fs_string_free(&flock
.client_id
);
3009 * When a TGETLOCK request comes, always return success because all lock
3010 * handling is done by client's VFS layer.
3012 static void v9fs_getlock(void *opaque
)
3018 int32_t fid
, err
= 0;
3019 V9fsPDU
*pdu
= opaque
;
3021 v9fs_string_init(&glock
.client_id
);
3022 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3023 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3028 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3029 glock
.type
, glock
.start
, glock
.length
);
3031 fidp
= get_fid(pdu
, fid
);
3036 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3040 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3041 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3042 glock
.start
, glock
.length
, glock
.proc_id
,
3048 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3049 glock
.length
, glock
.proc_id
);
3053 pdu_complete(pdu
, err
);
3054 v9fs_string_free(&glock
.client_id
);
3057 static void v9fs_mkdir(void *opaque
)
3059 V9fsPDU
*pdu
= opaque
;
3070 v9fs_string_init(&name
);
3071 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3075 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3077 if (name_is_illegal(name
.data
)) {
3082 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3087 fidp
= get_fid(pdu
, fid
);
3092 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3096 stat_to_qid(&stbuf
, &qid
);
3097 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3102 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3103 qid
.type
, qid
.version
, qid
.path
, err
);
3107 pdu_complete(pdu
, err
);
3108 v9fs_string_free(&name
);
3111 static void v9fs_xattrwalk(void *opaque
)
3117 int32_t fid
, newfid
;
3118 V9fsFidState
*file_fidp
;
3119 V9fsFidState
*xattr_fidp
= NULL
;
3120 V9fsPDU
*pdu
= opaque
;
3121 V9fsState
*s
= pdu
->s
;
3123 v9fs_string_init(&name
);
3124 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3128 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3130 file_fidp
= get_fid(pdu
, fid
);
3131 if (file_fidp
== NULL
) {
3135 xattr_fidp
= alloc_fid(s
, newfid
);
3136 if (xattr_fidp
== NULL
) {
3140 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3141 if (name
.data
== NULL
) {
3143 * listxattr request. Get the size first
3145 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3148 clunk_fid(s
, xattr_fidp
->fid
);
3152 * Read the xattr value
3154 xattr_fidp
->fs
.xattr
.len
= size
;
3155 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3156 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3158 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3159 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3160 xattr_fidp
->fs
.xattr
.value
,
3161 xattr_fidp
->fs
.xattr
.len
);
3163 clunk_fid(s
, xattr_fidp
->fid
);
3167 err
= pdu_marshal(pdu
, offset
, "q", size
);
3174 * specific xattr fid. We check for xattr
3175 * presence also collect the xattr size
3177 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3181 clunk_fid(s
, xattr_fidp
->fid
);
3185 * Read the xattr value
3187 xattr_fidp
->fs
.xattr
.len
= size
;
3188 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3189 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3191 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3192 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3193 &name
, xattr_fidp
->fs
.xattr
.value
,
3194 xattr_fidp
->fs
.xattr
.len
);
3196 clunk_fid(s
, xattr_fidp
->fid
);
3200 err
= pdu_marshal(pdu
, offset
, "q", size
);
3206 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3208 put_fid(pdu
, file_fidp
);
3210 put_fid(pdu
, xattr_fidp
);
3213 pdu_complete(pdu
, err
);
3214 v9fs_string_free(&name
);
3217 static void v9fs_xattrcreate(void *opaque
)
3225 V9fsFidState
*file_fidp
;
3226 V9fsFidState
*xattr_fidp
;
3227 V9fsPDU
*pdu
= opaque
;
3229 v9fs_string_init(&name
);
3230 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3234 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3236 file_fidp
= get_fid(pdu
, fid
);
3237 if (file_fidp
== NULL
) {
3241 /* Make the file fid point to xattr */
3242 xattr_fidp
= file_fidp
;
3243 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3244 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3245 xattr_fidp
->fs
.xattr
.len
= size
;
3246 xattr_fidp
->fs
.xattr
.flags
= flags
;
3247 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3248 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3249 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3251 put_fid(pdu
, file_fidp
);
3253 pdu_complete(pdu
, err
);
3254 v9fs_string_free(&name
);
3257 static void v9fs_readlink(void *opaque
)
3259 V9fsPDU
*pdu
= opaque
;
3266 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3270 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3271 fidp
= get_fid(pdu
, fid
);
3277 v9fs_string_init(&target
);
3278 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3282 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3284 v9fs_string_free(&target
);
3288 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3289 v9fs_string_free(&target
);
3293 pdu_complete(pdu
, err
);
3296 static CoroutineEntry
*pdu_co_handlers
[] = {
3297 [P9_TREADDIR
] = v9fs_readdir
,
3298 [P9_TSTATFS
] = v9fs_statfs
,
3299 [P9_TGETATTR
] = v9fs_getattr
,
3300 [P9_TSETATTR
] = v9fs_setattr
,
3301 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3302 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3303 [P9_TMKNOD
] = v9fs_mknod
,
3304 [P9_TRENAME
] = v9fs_rename
,
3305 [P9_TLOCK
] = v9fs_lock
,
3306 [P9_TGETLOCK
] = v9fs_getlock
,
3307 [P9_TRENAMEAT
] = v9fs_renameat
,
3308 [P9_TREADLINK
] = v9fs_readlink
,
3309 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3310 [P9_TMKDIR
] = v9fs_mkdir
,
3311 [P9_TVERSION
] = v9fs_version
,
3312 [P9_TLOPEN
] = v9fs_open
,
3313 [P9_TATTACH
] = v9fs_attach
,
3314 [P9_TSTAT
] = v9fs_stat
,
3315 [P9_TWALK
] = v9fs_walk
,
3316 [P9_TCLUNK
] = v9fs_clunk
,
3317 [P9_TFSYNC
] = v9fs_fsync
,
3318 [P9_TOPEN
] = v9fs_open
,
3319 [P9_TREAD
] = v9fs_read
,
3321 [P9_TAUTH
] = v9fs_auth
,
3323 [P9_TFLUSH
] = v9fs_flush
,
3324 [P9_TLINK
] = v9fs_link
,
3325 [P9_TSYMLINK
] = v9fs_symlink
,
3326 [P9_TCREATE
] = v9fs_create
,
3327 [P9_TLCREATE
] = v9fs_lcreate
,
3328 [P9_TWRITE
] = v9fs_write
,
3329 [P9_TWSTAT
] = v9fs_wstat
,
3330 [P9_TREMOVE
] = v9fs_remove
,
3333 static void v9fs_op_not_supp(void *opaque
)
3335 V9fsPDU
*pdu
= opaque
;
3336 pdu_complete(pdu
, -EOPNOTSUPP
);
3339 static void v9fs_fs_ro(void *opaque
)
3341 V9fsPDU
*pdu
= opaque
;
3342 pdu_complete(pdu
, -EROFS
);
3345 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3372 void pdu_submit(V9fsPDU
*pdu
)
3375 CoroutineEntry
*handler
;
3376 V9fsState
*s
= pdu
->s
;
3378 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3379 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3380 handler
= v9fs_op_not_supp
;
3382 handler
= pdu_co_handlers
[pdu
->id
];
3385 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3386 handler
= v9fs_fs_ro
;
3388 co
= qemu_coroutine_create(handler
, pdu
);
3389 qemu_coroutine_enter(co
);
3392 /* Returns 0 on success, 1 on failure. */
3393 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3395 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
3402 /* initialize pdu allocator */
3403 QLIST_INIT(&s
->free_list
);
3404 QLIST_INIT(&s
->active_list
);
3405 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3406 QLIST_INSERT_HEAD(&s
->free_list
, &v
->pdus
[i
], next
);
3411 v9fs_path_init(&path
);
3413 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3416 /* We don't have a fsdev identified by fsdev_id */
3417 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3419 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3423 if (!s
->fsconf
.tag
) {
3424 /* we haven't specified a mount_tag */
3425 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3426 s
->fsconf
.fsdev_id
);
3430 s
->ctx
.export_flags
= fse
->export_flags
;
3431 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3432 s
->ctx
.exops
.get_st_gen
= NULL
;
3433 len
= strlen(s
->fsconf
.tag
);
3434 if (len
> MAX_TAG_LEN
- 1) {
3435 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3436 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3440 s
->tag
= g_strdup(s
->fsconf
.tag
);
3446 qemu_co_rwlock_init(&s
->rename_lock
);
3448 if (s
->ops
->init(&s
->ctx
) < 0) {
3449 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3450 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3455 * Check details of export path, We need to use fs driver
3456 * call back to do that. Since we are in the init path, we don't
3457 * use co-routines here.
3459 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3461 "error in converting name to path %s", strerror(errno
));
3464 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3465 error_setg(errp
, "share path %s does not exist", fse
->path
);
3467 } else if (!S_ISDIR(stat
.st_mode
)) {
3468 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3471 v9fs_path_free(&path
);
3476 g_free(s
->ctx
.fs_root
);
3478 v9fs_path_free(&path
);
3483 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3485 g_free(s
->ctx
.fs_root
);
3489 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3492 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3493 error_report("Failed to get the resource limit");
3496 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3497 open_fd_rc
= rlim
.rlim_cur
/2;