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 fidp
= get_fid(pdu
, dfid
);
1506 flags
= get_dotl_openflags(pdu
->s
, flags
);
1507 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1508 flags
| O_CREAT
, mode
, &stbuf
);
1512 fidp
->fid_type
= P9_FID_FILE
;
1513 fidp
->open_flags
= flags
;
1514 if (flags
& O_EXCL
) {
1516 * We let the host file system do O_EXCL check
1517 * We should not reclaim such fd
1519 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1521 iounit
= get_iounit(pdu
, &fidp
->path
);
1522 stat_to_qid(&stbuf
, &qid
);
1523 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1528 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1529 qid
.type
, qid
.version
, qid
.path
, iounit
);
1533 pdu_complete(pdu
, err
);
1534 v9fs_string_free(&name
);
1537 static void v9fs_fsync(void *opaque
)
1544 V9fsPDU
*pdu
= opaque
;
1546 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1550 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1552 fidp
= get_fid(pdu
, fid
);
1557 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1563 pdu_complete(pdu
, err
);
1566 static void v9fs_clunk(void *opaque
)
1572 V9fsPDU
*pdu
= opaque
;
1573 V9fsState
*s
= pdu
->s
;
1575 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1579 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1581 fidp
= clunk_fid(s
, fid
);
1587 * Bump the ref so that put_fid will
1591 err
= put_fid(pdu
, fidp
);
1596 pdu_complete(pdu
, err
);
1599 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1600 uint64_t off
, uint32_t max_count
)
1606 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
1607 VirtQueueElement
*elem
= v
->elems
[pdu
->idx
];
1609 xattr_len
= fidp
->fs
.xattr
.len
;
1610 read_count
= xattr_len
- off
;
1611 if (read_count
> max_count
) {
1612 read_count
= max_count
;
1613 } else if (read_count
< 0) {
1615 * read beyond XATTR value
1619 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1625 err
= v9fs_pack(elem
->in_sg
, elem
->in_num
, offset
,
1626 ((char *)fidp
->fs
.xattr
.value
) + off
,
1635 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1636 V9fsFidState
*fidp
, uint32_t max_count
)
1643 off_t saved_dir_pos
;
1644 struct dirent
*dent
;
1646 /* save the directory position */
1647 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1648 if (saved_dir_pos
< 0) {
1649 return saved_dir_pos
;
1653 v9fs_path_init(&path
);
1655 v9fs_readdir_lock(&fidp
->fs
.dir
);
1657 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1661 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1665 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1669 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1673 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1674 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1676 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1678 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1679 /* Ran out of buffer. Set dir back to old position and return */
1680 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1681 v9fs_stat_free(&v9stat
);
1682 v9fs_path_free(&path
);
1686 v9fs_stat_free(&v9stat
);
1687 v9fs_path_free(&path
);
1688 saved_dir_pos
= dent
->d_off
;
1691 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1693 v9fs_path_free(&path
);
1701 * Create a QEMUIOVector for a sub-region of PDU iovecs
1703 * @qiov: uninitialized QEMUIOVector
1704 * @skip: number of bytes to skip from beginning of PDU
1705 * @size: number of bytes to include
1706 * @is_write: true - write, false - read
1708 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1709 * with qemu_iovec_destroy().
1711 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1712 size_t skip
, size_t size
,
1719 virtio_init_iov_from_pdu(pdu
, &iov
, &niov
, is_write
);
1721 qemu_iovec_init_external(&elem
, iov
, niov
);
1722 qemu_iovec_init(qiov
, niov
);
1723 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1726 static void v9fs_read(void *opaque
)
1735 V9fsPDU
*pdu
= opaque
;
1736 V9fsState
*s
= pdu
->s
;
1738 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1742 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1744 fidp
= get_fid(pdu
, fid
);
1749 if (fidp
->fid_type
== P9_FID_DIR
) {
1752 v9fs_co_rewinddir(pdu
, fidp
);
1754 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1759 err
= pdu_marshal(pdu
, offset
, "d", count
);
1763 err
+= offset
+ count
;
1764 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1765 QEMUIOVector qiov_full
;
1769 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1770 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1772 qemu_iovec_reset(&qiov
);
1773 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1775 print_sg(qiov
.iov
, qiov
.niov
);
1777 /* Loop in case of EINTR */
1779 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1784 } while (len
== -EINTR
&& !pdu
->cancelled
);
1786 /* IO error return the error */
1790 } while (count
< max_count
&& len
> 0);
1791 err
= pdu_marshal(pdu
, offset
, "d", count
);
1795 err
+= offset
+ count
;
1796 qemu_iovec_destroy(&qiov
);
1797 qemu_iovec_destroy(&qiov_full
);
1798 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1799 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1803 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1807 pdu_complete(pdu
, err
);
1810 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1813 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1814 * size of type (1) + size of name.size (2) + strlen(name.data)
1816 return 24 + v9fs_string_size(name
);
1819 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1820 V9fsFidState
*fidp
, int32_t max_count
)
1827 off_t saved_dir_pos
;
1828 struct dirent
*dent
;
1830 /* save the directory position */
1831 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1832 if (saved_dir_pos
< 0) {
1833 return saved_dir_pos
;
1837 v9fs_readdir_lock(&fidp
->fs
.dir
);
1839 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1843 v9fs_string_init(&name
);
1844 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1845 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1846 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1848 /* Ran out of buffer. Set dir back to old position and return */
1849 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1850 v9fs_string_free(&name
);
1854 * Fill up just the path field of qid because the client uses
1855 * only that. To fill the entire qid structure we will have
1856 * to stat each dirent found, which is expensive
1858 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1859 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1860 /* Fill the other fields with dummy values */
1864 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1865 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1867 dent
->d_type
, &name
);
1869 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1872 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1873 v9fs_string_free(&name
);
1877 v9fs_string_free(&name
);
1878 saved_dir_pos
= dent
->d_off
;
1881 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1889 static void v9fs_readdir(void *opaque
)
1895 uint64_t initial_offset
;
1898 V9fsPDU
*pdu
= opaque
;
1900 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1901 &initial_offset
, &max_count
);
1905 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1907 fidp
= get_fid(pdu
, fid
);
1912 if (!fidp
->fs
.dir
.stream
) {
1916 if (initial_offset
== 0) {
1917 v9fs_co_rewinddir(pdu
, fidp
);
1919 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1921 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1926 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1930 retval
+= count
+ offset
;
1931 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1935 pdu_complete(pdu
, retval
);
1938 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1939 uint64_t off
, uint32_t count
,
1940 struct iovec
*sg
, int cnt
)
1949 xattr_len
= fidp
->fs
.xattr
.len
;
1950 write_count
= xattr_len
- off
;
1951 if (write_count
> count
) {
1952 write_count
= count
;
1953 } else if (write_count
< 0) {
1955 * write beyond XATTR value len specified in
1961 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1966 fidp
->fs
.xattr
.copied_len
+= write_count
;
1968 * Now copy the content from sg list
1970 for (i
= 0; i
< cnt
; i
++) {
1971 if (write_count
> sg
[i
].iov_len
) {
1972 to_copy
= sg
[i
].iov_len
;
1974 to_copy
= write_count
;
1976 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1977 /* updating vs->off since we are not using below */
1979 write_count
-= to_copy
;
1985 static void v9fs_write(void *opaque
)
1995 V9fsPDU
*pdu
= opaque
;
1996 V9fsState
*s
= pdu
->s
;
1997 QEMUIOVector qiov_full
;
2000 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2002 pdu_complete(pdu
, err
);
2006 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2007 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2009 fidp
= get_fid(pdu
, fid
);
2014 if (fidp
->fid_type
== P9_FID_FILE
) {
2015 if (fidp
->fs
.fd
== -1) {
2019 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2021 * setxattr operation
2023 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2024 qiov_full
.iov
, qiov_full
.niov
);
2030 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2032 qemu_iovec_reset(&qiov
);
2033 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2035 print_sg(qiov
.iov
, qiov
.niov
);
2037 /* Loop in case of EINTR */
2039 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2044 } while (len
== -EINTR
&& !pdu
->cancelled
);
2046 /* IO error return the error */
2050 } while (total
< count
&& len
> 0);
2053 err
= pdu_marshal(pdu
, offset
, "d", total
);
2058 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2060 qemu_iovec_destroy(&qiov
);
2064 qemu_iovec_destroy(&qiov_full
);
2065 pdu_complete(pdu
, err
);
2068 static void v9fs_create(void *opaque
)
2080 V9fsString extension
;
2082 V9fsPDU
*pdu
= opaque
;
2084 v9fs_path_init(&path
);
2085 v9fs_string_init(&name
);
2086 v9fs_string_init(&extension
);
2087 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2088 &perm
, &mode
, &extension
);
2092 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2094 if (name_is_illegal(name
.data
)) {
2099 fidp
= get_fid(pdu
, fid
);
2104 if (perm
& P9_STAT_MODE_DIR
) {
2105 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2106 fidp
->uid
, -1, &stbuf
);
2110 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2114 v9fs_path_copy(&fidp
->path
, &path
);
2115 err
= v9fs_co_opendir(pdu
, fidp
);
2119 fidp
->fid_type
= P9_FID_DIR
;
2120 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2121 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2122 extension
.data
, -1 , &stbuf
);
2126 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2130 v9fs_path_copy(&fidp
->path
, &path
);
2131 } else if (perm
& P9_STAT_MODE_LINK
) {
2132 int32_t ofid
= atoi(extension
.data
);
2133 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2134 if (ofidp
== NULL
) {
2138 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2139 put_fid(pdu
, ofidp
);
2143 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2145 fidp
->fid_type
= P9_FID_NONE
;
2148 v9fs_path_copy(&fidp
->path
, &path
);
2149 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2151 fidp
->fid_type
= P9_FID_NONE
;
2154 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2156 uint32_t major
, minor
;
2159 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2176 nmode
|= perm
& 0777;
2177 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2178 makedev(major
, minor
), nmode
, &stbuf
);
2182 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2186 v9fs_path_copy(&fidp
->path
, &path
);
2187 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2188 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2189 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2193 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2197 v9fs_path_copy(&fidp
->path
, &path
);
2198 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2199 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2200 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2204 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2208 v9fs_path_copy(&fidp
->path
, &path
);
2210 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2211 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2215 fidp
->fid_type
= P9_FID_FILE
;
2216 fidp
->open_flags
= omode_to_uflags(mode
);
2217 if (fidp
->open_flags
& O_EXCL
) {
2219 * We let the host file system do O_EXCL check
2220 * We should not reclaim such fd
2222 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2225 iounit
= get_iounit(pdu
, &fidp
->path
);
2226 stat_to_qid(&stbuf
, &qid
);
2227 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2232 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2233 qid
.type
, qid
.version
, qid
.path
, iounit
);
2237 pdu_complete(pdu
, err
);
2238 v9fs_string_free(&name
);
2239 v9fs_string_free(&extension
);
2240 v9fs_path_free(&path
);
2243 static void v9fs_symlink(void *opaque
)
2245 V9fsPDU
*pdu
= opaque
;
2248 V9fsFidState
*dfidp
;
2256 v9fs_string_init(&name
);
2257 v9fs_string_init(&symname
);
2258 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2262 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2264 if (name_is_illegal(name
.data
)) {
2269 dfidp
= get_fid(pdu
, dfid
);
2270 if (dfidp
== NULL
) {
2274 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2278 stat_to_qid(&stbuf
, &qid
);
2279 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2284 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2285 qid
.type
, qid
.version
, qid
.path
);
2287 put_fid(pdu
, dfidp
);
2289 pdu_complete(pdu
, err
);
2290 v9fs_string_free(&name
);
2291 v9fs_string_free(&symname
);
2294 static void v9fs_flush(void *opaque
)
2299 V9fsPDU
*cancel_pdu
;
2300 V9fsPDU
*pdu
= opaque
;
2301 V9fsState
*s
= pdu
->s
;
2303 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2305 pdu_complete(pdu
, err
);
2308 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2310 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2311 if (cancel_pdu
->tag
== tag
) {
2316 cancel_pdu
->cancelled
= 1;
2318 * Wait for pdu to complete.
2320 qemu_co_queue_wait(&cancel_pdu
->complete
);
2321 cancel_pdu
->cancelled
= 0;
2322 pdu_free(cancel_pdu
);
2324 pdu_complete(pdu
, 7);
2327 static void v9fs_link(void *opaque
)
2329 V9fsPDU
*pdu
= opaque
;
2330 int32_t dfid
, oldfid
;
2331 V9fsFidState
*dfidp
, *oldfidp
;
2336 v9fs_string_init(&name
);
2337 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2341 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2343 if (name_is_illegal(name
.data
)) {
2348 dfidp
= get_fid(pdu
, dfid
);
2349 if (dfidp
== NULL
) {
2354 oldfidp
= get_fid(pdu
, oldfid
);
2355 if (oldfidp
== NULL
) {
2359 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2364 put_fid(pdu
, dfidp
);
2366 v9fs_string_free(&name
);
2367 pdu_complete(pdu
, err
);
2370 /* Only works with path name based fid */
2371 static void v9fs_remove(void *opaque
)
2377 V9fsPDU
*pdu
= opaque
;
2379 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2383 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2385 fidp
= get_fid(pdu
, fid
);
2390 /* if fs driver is not path based, return EOPNOTSUPP */
2391 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2396 * IF the file is unlinked, we cannot reopen
2397 * the file later. So don't reclaim fd
2399 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2403 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2408 /* For TREMOVE we need to clunk the fid even on failed remove */
2409 clunk_fid(pdu
->s
, fidp
->fid
);
2412 pdu_complete(pdu
, err
);
2415 static void v9fs_unlinkat(void *opaque
)
2419 int32_t dfid
, flags
;
2422 V9fsFidState
*dfidp
;
2423 V9fsPDU
*pdu
= opaque
;
2425 v9fs_string_init(&name
);
2426 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2431 if (name_is_illegal(name
.data
)) {
2436 dfidp
= get_fid(pdu
, dfid
);
2437 if (dfidp
== NULL
) {
2442 * IF the file is unlinked, we cannot reopen
2443 * the file later. So don't reclaim fd
2445 v9fs_path_init(&path
);
2446 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2450 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2454 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2459 put_fid(pdu
, dfidp
);
2460 v9fs_path_free(&path
);
2462 pdu_complete(pdu
, err
);
2463 v9fs_string_free(&name
);
2467 /* Only works with path name based fid */
2468 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2469 int32_t newdirfid
, V9fsString
*name
)
2474 V9fsFidState
*tfidp
;
2475 V9fsState
*s
= pdu
->s
;
2476 V9fsFidState
*dirfidp
= NULL
;
2477 char *old_name
, *new_name
;
2479 v9fs_path_init(&new_path
);
2480 if (newdirfid
!= -1) {
2481 dirfidp
= get_fid(pdu
, newdirfid
);
2482 if (dirfidp
== NULL
) {
2486 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2487 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2489 old_name
= fidp
->path
.data
;
2490 end
= strrchr(old_name
, '/');
2496 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2497 strncat(new_name
, old_name
, end
- old_name
);
2498 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2499 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2502 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2507 * Fixup fid's pointing to the old name to
2508 * start pointing to the new name
2510 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2511 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2512 /* replace the name */
2513 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2518 put_fid(pdu
, dirfidp
);
2520 v9fs_path_free(&new_path
);
2525 /* Only works with path name based fid */
2526 static void v9fs_rename(void *opaque
)
2534 V9fsPDU
*pdu
= opaque
;
2535 V9fsState
*s
= pdu
->s
;
2537 v9fs_string_init(&name
);
2538 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2543 if (name_is_illegal(name
.data
)) {
2548 fidp
= get_fid(pdu
, fid
);
2553 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2554 /* if fs driver is not path based, return EOPNOTSUPP */
2555 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2559 v9fs_path_write_lock(s
);
2560 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2561 v9fs_path_unlock(s
);
2568 pdu_complete(pdu
, err
);
2569 v9fs_string_free(&name
);
2572 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2573 V9fsString
*old_name
, V9fsPath
*newdir
,
2574 V9fsString
*new_name
)
2576 V9fsFidState
*tfidp
;
2577 V9fsPath oldpath
, newpath
;
2578 V9fsState
*s
= pdu
->s
;
2581 v9fs_path_init(&oldpath
);
2582 v9fs_path_init(&newpath
);
2583 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2584 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2587 * Fixup fid's pointing to the old name to
2588 * start pointing to the new name
2590 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2591 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2592 /* replace the name */
2593 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2596 v9fs_path_free(&oldpath
);
2597 v9fs_path_free(&newpath
);
2600 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2601 V9fsString
*old_name
, int32_t newdirfid
,
2602 V9fsString
*new_name
)
2605 V9fsState
*s
= pdu
->s
;
2606 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2608 olddirfidp
= get_fid(pdu
, olddirfid
);
2609 if (olddirfidp
== NULL
) {
2613 if (newdirfid
!= -1) {
2614 newdirfidp
= get_fid(pdu
, newdirfid
);
2615 if (newdirfidp
== NULL
) {
2620 newdirfidp
= get_fid(pdu
, olddirfid
);
2623 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2624 &newdirfidp
->path
, new_name
);
2628 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2629 /* Only for path based fid we need to do the below fixup */
2630 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2631 &newdirfidp
->path
, new_name
);
2635 put_fid(pdu
, olddirfidp
);
2638 put_fid(pdu
, newdirfidp
);
2643 static void v9fs_renameat(void *opaque
)
2647 V9fsPDU
*pdu
= opaque
;
2648 V9fsState
*s
= pdu
->s
;
2649 int32_t olddirfid
, newdirfid
;
2650 V9fsString old_name
, new_name
;
2652 v9fs_string_init(&old_name
);
2653 v9fs_string_init(&new_name
);
2654 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2655 &old_name
, &newdirfid
, &new_name
);
2660 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2665 v9fs_path_write_lock(s
);
2666 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2667 &old_name
, newdirfid
, &new_name
);
2668 v9fs_path_unlock(s
);
2674 pdu_complete(pdu
, err
);
2675 v9fs_string_free(&old_name
);
2676 v9fs_string_free(&new_name
);
2679 static void v9fs_wstat(void *opaque
)
2688 V9fsPDU
*pdu
= opaque
;
2690 v9fs_stat_init(&v9stat
);
2691 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2695 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2696 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2698 fidp
= get_fid(pdu
, fid
);
2703 /* do we need to sync the file? */
2704 if (donttouch_stat(&v9stat
)) {
2705 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2708 if (v9stat
.mode
!= -1) {
2710 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2714 v9_mode
= stat_to_v9mode(&stbuf
);
2715 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2716 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2717 /* Attempting to change the type */
2721 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2722 v9mode_to_mode(v9stat
.mode
,
2723 &v9stat
.extension
));
2728 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2729 struct timespec times
[2];
2730 if (v9stat
.atime
!= -1) {
2731 times
[0].tv_sec
= v9stat
.atime
;
2732 times
[0].tv_nsec
= 0;
2734 times
[0].tv_nsec
= UTIME_OMIT
;
2736 if (v9stat
.mtime
!= -1) {
2737 times
[1].tv_sec
= v9stat
.mtime
;
2738 times
[1].tv_nsec
= 0;
2740 times
[1].tv_nsec
= UTIME_OMIT
;
2742 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2747 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2748 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2753 if (v9stat
.name
.size
!= 0) {
2754 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2759 if (v9stat
.length
!= -1) {
2760 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2769 v9fs_stat_free(&v9stat
);
2770 pdu_complete(pdu
, err
);
2773 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2785 int32_t bsize_factor
;
2788 * compute bsize factor based on host file system block size
2791 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2792 if (!bsize_factor
) {
2795 f_type
= stbuf
->f_type
;
2796 f_bsize
= stbuf
->f_bsize
;
2797 f_bsize
*= bsize_factor
;
2799 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2800 * adjust(divide) the number of blocks, free blocks and available
2801 * blocks by bsize factor
2803 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2804 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2805 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2806 f_files
= stbuf
->f_files
;
2807 f_ffree
= stbuf
->f_ffree
;
2808 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2809 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2810 f_namelen
= stbuf
->f_namelen
;
2812 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2813 f_type
, f_bsize
, f_blocks
, f_bfree
,
2814 f_bavail
, f_files
, f_ffree
,
2815 fsid_val
, f_namelen
);
2818 static void v9fs_statfs(void *opaque
)
2824 struct statfs stbuf
;
2825 V9fsPDU
*pdu
= opaque
;
2826 V9fsState
*s
= pdu
->s
;
2828 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2832 fidp
= get_fid(pdu
, fid
);
2837 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2841 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2849 pdu_complete(pdu
, retval
);
2852 static void v9fs_mknod(void *opaque
)
2865 V9fsPDU
*pdu
= opaque
;
2867 v9fs_string_init(&name
);
2868 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2869 &major
, &minor
, &gid
);
2873 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2875 if (name_is_illegal(name
.data
)) {
2880 fidp
= get_fid(pdu
, fid
);
2885 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2886 makedev(major
, minor
), mode
, &stbuf
);
2890 stat_to_qid(&stbuf
, &qid
);
2891 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2896 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2897 qid
.type
, qid
.version
, qid
.path
);
2901 pdu_complete(pdu
, err
);
2902 v9fs_string_free(&name
);
2906 * Implement posix byte range locking code
2907 * Server side handling of locking code is very simple, because 9p server in
2908 * QEMU can handle only one client. And most of the lock handling
2909 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2910 * do any thing in * qemu 9p server side lock code path.
2911 * So when a TLOCK request comes, always return success
2913 static void v9fs_lock(void *opaque
)
2920 int32_t fid
, err
= 0;
2921 V9fsPDU
*pdu
= opaque
;
2923 status
= P9_LOCK_ERROR
;
2924 v9fs_string_init(&flock
.client_id
);
2925 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2926 &flock
.flags
, &flock
.start
, &flock
.length
,
2927 &flock
.proc_id
, &flock
.client_id
);
2931 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2932 flock
.type
, flock
.start
, flock
.length
);
2935 /* We support only block flag now (that too ignored currently) */
2936 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2940 fidp
= get_fid(pdu
, fid
);
2945 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2949 status
= P9_LOCK_SUCCESS
;
2953 err
= pdu_marshal(pdu
, offset
, "b", status
);
2957 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2958 pdu_complete(pdu
, err
);
2959 v9fs_string_free(&flock
.client_id
);
2963 * When a TGETLOCK request comes, always return success because all lock
2964 * handling is done by client's VFS layer.
2966 static void v9fs_getlock(void *opaque
)
2972 int32_t fid
, err
= 0;
2973 V9fsPDU
*pdu
= opaque
;
2975 v9fs_string_init(&glock
.client_id
);
2976 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
2977 &glock
.start
, &glock
.length
, &glock
.proc_id
,
2982 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2983 glock
.type
, glock
.start
, glock
.length
);
2985 fidp
= get_fid(pdu
, fid
);
2990 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2994 glock
.type
= P9_LOCK_TYPE_UNLCK
;
2995 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
2996 glock
.start
, glock
.length
, glock
.proc_id
,
3002 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3003 glock
.length
, glock
.proc_id
);
3007 pdu_complete(pdu
, err
);
3008 v9fs_string_free(&glock
.client_id
);
3011 static void v9fs_mkdir(void *opaque
)
3013 V9fsPDU
*pdu
= opaque
;
3024 v9fs_string_init(&name
);
3025 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3029 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3031 if (name_is_illegal(name
.data
)) {
3036 fidp
= get_fid(pdu
, fid
);
3041 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3045 stat_to_qid(&stbuf
, &qid
);
3046 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3051 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3052 qid
.type
, qid
.version
, qid
.path
, err
);
3056 pdu_complete(pdu
, err
);
3057 v9fs_string_free(&name
);
3060 static void v9fs_xattrwalk(void *opaque
)
3066 int32_t fid
, newfid
;
3067 V9fsFidState
*file_fidp
;
3068 V9fsFidState
*xattr_fidp
= NULL
;
3069 V9fsPDU
*pdu
= opaque
;
3070 V9fsState
*s
= pdu
->s
;
3072 v9fs_string_init(&name
);
3073 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3077 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3079 file_fidp
= get_fid(pdu
, fid
);
3080 if (file_fidp
== NULL
) {
3084 xattr_fidp
= alloc_fid(s
, newfid
);
3085 if (xattr_fidp
== NULL
) {
3089 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3090 if (name
.data
== NULL
) {
3092 * listxattr request. Get the size first
3094 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3097 clunk_fid(s
, xattr_fidp
->fid
);
3101 * Read the xattr value
3103 xattr_fidp
->fs
.xattr
.len
= size
;
3104 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3105 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3107 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3108 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3109 xattr_fidp
->fs
.xattr
.value
,
3110 xattr_fidp
->fs
.xattr
.len
);
3112 clunk_fid(s
, xattr_fidp
->fid
);
3116 err
= pdu_marshal(pdu
, offset
, "q", size
);
3123 * specific xattr fid. We check for xattr
3124 * presence also collect the xattr size
3126 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3130 clunk_fid(s
, xattr_fidp
->fid
);
3134 * Read the xattr value
3136 xattr_fidp
->fs
.xattr
.len
= size
;
3137 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3138 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3140 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3141 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3142 &name
, xattr_fidp
->fs
.xattr
.value
,
3143 xattr_fidp
->fs
.xattr
.len
);
3145 clunk_fid(s
, xattr_fidp
->fid
);
3149 err
= pdu_marshal(pdu
, offset
, "q", size
);
3155 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3157 put_fid(pdu
, file_fidp
);
3159 put_fid(pdu
, xattr_fidp
);
3162 pdu_complete(pdu
, err
);
3163 v9fs_string_free(&name
);
3166 static void v9fs_xattrcreate(void *opaque
)
3174 V9fsFidState
*file_fidp
;
3175 V9fsFidState
*xattr_fidp
;
3176 V9fsPDU
*pdu
= opaque
;
3178 v9fs_string_init(&name
);
3179 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3183 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3185 file_fidp
= get_fid(pdu
, fid
);
3186 if (file_fidp
== NULL
) {
3190 /* Make the file fid point to xattr */
3191 xattr_fidp
= file_fidp
;
3192 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3193 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3194 xattr_fidp
->fs
.xattr
.len
= size
;
3195 xattr_fidp
->fs
.xattr
.flags
= flags
;
3196 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3197 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3198 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3200 put_fid(pdu
, file_fidp
);
3202 pdu_complete(pdu
, err
);
3203 v9fs_string_free(&name
);
3206 static void v9fs_readlink(void *opaque
)
3208 V9fsPDU
*pdu
= opaque
;
3215 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3219 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3220 fidp
= get_fid(pdu
, fid
);
3226 v9fs_string_init(&target
);
3227 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3231 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3233 v9fs_string_free(&target
);
3237 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3238 v9fs_string_free(&target
);
3242 pdu_complete(pdu
, err
);
3245 static CoroutineEntry
*pdu_co_handlers
[] = {
3246 [P9_TREADDIR
] = v9fs_readdir
,
3247 [P9_TSTATFS
] = v9fs_statfs
,
3248 [P9_TGETATTR
] = v9fs_getattr
,
3249 [P9_TSETATTR
] = v9fs_setattr
,
3250 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3251 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3252 [P9_TMKNOD
] = v9fs_mknod
,
3253 [P9_TRENAME
] = v9fs_rename
,
3254 [P9_TLOCK
] = v9fs_lock
,
3255 [P9_TGETLOCK
] = v9fs_getlock
,
3256 [P9_TRENAMEAT
] = v9fs_renameat
,
3257 [P9_TREADLINK
] = v9fs_readlink
,
3258 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3259 [P9_TMKDIR
] = v9fs_mkdir
,
3260 [P9_TVERSION
] = v9fs_version
,
3261 [P9_TLOPEN
] = v9fs_open
,
3262 [P9_TATTACH
] = v9fs_attach
,
3263 [P9_TSTAT
] = v9fs_stat
,
3264 [P9_TWALK
] = v9fs_walk
,
3265 [P9_TCLUNK
] = v9fs_clunk
,
3266 [P9_TFSYNC
] = v9fs_fsync
,
3267 [P9_TOPEN
] = v9fs_open
,
3268 [P9_TREAD
] = v9fs_read
,
3270 [P9_TAUTH
] = v9fs_auth
,
3272 [P9_TFLUSH
] = v9fs_flush
,
3273 [P9_TLINK
] = v9fs_link
,
3274 [P9_TSYMLINK
] = v9fs_symlink
,
3275 [P9_TCREATE
] = v9fs_create
,
3276 [P9_TLCREATE
] = v9fs_lcreate
,
3277 [P9_TWRITE
] = v9fs_write
,
3278 [P9_TWSTAT
] = v9fs_wstat
,
3279 [P9_TREMOVE
] = v9fs_remove
,
3282 static void v9fs_op_not_supp(void *opaque
)
3284 V9fsPDU
*pdu
= opaque
;
3285 pdu_complete(pdu
, -EOPNOTSUPP
);
3288 static void v9fs_fs_ro(void *opaque
)
3290 V9fsPDU
*pdu
= opaque
;
3291 pdu_complete(pdu
, -EROFS
);
3294 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3321 void pdu_submit(V9fsPDU
*pdu
)
3324 CoroutineEntry
*handler
;
3325 V9fsState
*s
= pdu
->s
;
3327 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3328 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3329 handler
= v9fs_op_not_supp
;
3331 handler
= pdu_co_handlers
[pdu
->id
];
3334 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3335 handler
= v9fs_fs_ro
;
3337 co
= qemu_coroutine_create(handler
, pdu
);
3338 qemu_coroutine_enter(co
);
3341 /* Returns 0 on success, 1 on failure. */
3342 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3344 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
3351 /* initialize pdu allocator */
3352 QLIST_INIT(&s
->free_list
);
3353 QLIST_INIT(&s
->active_list
);
3354 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3355 QLIST_INSERT_HEAD(&s
->free_list
, &v
->pdus
[i
], next
);
3360 v9fs_path_init(&path
);
3362 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3365 /* We don't have a fsdev identified by fsdev_id */
3366 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3368 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3372 if (!s
->fsconf
.tag
) {
3373 /* we haven't specified a mount_tag */
3374 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3375 s
->fsconf
.fsdev_id
);
3379 s
->ctx
.export_flags
= fse
->export_flags
;
3380 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3381 s
->ctx
.exops
.get_st_gen
= NULL
;
3382 len
= strlen(s
->fsconf
.tag
);
3383 if (len
> MAX_TAG_LEN
- 1) {
3384 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3385 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3389 s
->tag
= g_strdup(s
->fsconf
.tag
);
3395 qemu_co_rwlock_init(&s
->rename_lock
);
3397 if (s
->ops
->init(&s
->ctx
) < 0) {
3398 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3399 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3404 * Check details of export path, We need to use fs driver
3405 * call back to do that. Since we are in the init path, we don't
3406 * use co-routines here.
3408 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3410 "error in converting name to path %s", strerror(errno
));
3413 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3414 error_setg(errp
, "share path %s does not exist", fse
->path
);
3416 } else if (!S_ISDIR(stat
.st_mode
)) {
3417 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3420 v9fs_path_free(&path
);
3425 g_free(s
->ctx
.fs_root
);
3427 v9fs_path_free(&path
);
3432 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3434 g_free(s
->ctx
.fs_root
);
3438 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3441 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3442 error_report("Failed to get the resource limit");
3445 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3446 open_fd_rc
= rlim
.rlim_cur
/2;