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 "hw/i386/pc.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
== 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
;
306 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
310 if (fidp
->fs
.xattr
.copied_len
== -1) {
311 /* getxattr/listxattr fid */
315 * if this is fid for setxattr. clunk should
316 * result in setxattr localcall
318 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
319 /* clunk after partial write */
323 if (fidp
->fs
.xattr
.len
) {
324 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
325 fidp
->fs
.xattr
.value
,
327 fidp
->fs
.xattr
.flags
);
329 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
332 v9fs_string_free(&fidp
->fs
.xattr
.name
);
334 g_free(fidp
->fs
.xattr
.value
);
338 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
342 if (fidp
->fid_type
== P9_FID_FILE
) {
343 /* If we reclaimed the fd no need to close */
344 if (fidp
->fs
.fd
!= -1) {
345 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
347 } else if (fidp
->fid_type
== P9_FID_DIR
) {
348 if (fidp
->fs
.dir
!= NULL
) {
349 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
351 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
352 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
354 v9fs_path_free(&fidp
->path
);
359 static int put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
364 * Don't free the fid if it is in reclaim list
366 if (!fidp
->ref
&& fidp
->clunked
) {
367 if (fidp
->fid
== pdu
->s
->root_fid
) {
369 * if the clunked fid is root fid then we
370 * have unmounted the fs on the client side.
371 * delete the migration blocker. Ideally, this
372 * should be hooked to transport close notification
374 if (pdu
->s
->migration_blocker
) {
375 migrate_del_blocker(pdu
->s
->migration_blocker
);
376 error_free(pdu
->s
->migration_blocker
);
377 pdu
->s
->migration_blocker
= NULL
;
380 return free_fid(pdu
, fidp
);
385 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
387 V9fsFidState
**fidpp
, *fidp
;
389 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
390 if ((*fidpp
)->fid
== fid
) {
394 if (*fidpp
== NULL
) {
403 void v9fs_reclaim_fd(V9fsPDU
*pdu
)
405 int reclaim_count
= 0;
406 V9fsState
*s
= pdu
->s
;
407 V9fsFidState
*f
, *reclaim_list
= NULL
;
409 for (f
= s
->fid_list
; f
; f
= f
->next
) {
411 * Unlink fids cannot be reclaimed. Check
412 * for them and skip them. Also skip fids
413 * currently being operated on.
415 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
419 * if it is a recently referenced fid
420 * we leave the fid untouched and clear the
421 * reference bit. We come back to it later
422 * in the next iteration. (a simple LRU without
423 * moving list elements around)
425 if (f
->flags
& FID_REFERENCED
) {
426 f
->flags
&= ~FID_REFERENCED
;
430 * Add fids to reclaim list.
432 if (f
->fid_type
== P9_FID_FILE
) {
433 if (f
->fs
.fd
!= -1) {
435 * Up the reference count so that
436 * a clunk request won't free this fid
439 f
->rclm_lst
= reclaim_list
;
441 f
->fs_reclaim
.fd
= f
->fs
.fd
;
445 } else if (f
->fid_type
== P9_FID_DIR
) {
446 if (f
->fs
.dir
!= NULL
) {
448 * Up the reference count so that
449 * a clunk request won't free this fid
452 f
->rclm_lst
= reclaim_list
;
454 f
->fs_reclaim
.dir
= f
->fs
.dir
;
459 if (reclaim_count
>= open_fd_rc
) {
464 * Now close the fid in reclaim list. Free them if they
465 * are already clunked.
467 while (reclaim_list
) {
469 reclaim_list
= f
->rclm_lst
;
470 if (f
->fid_type
== P9_FID_FILE
) {
471 v9fs_co_close(pdu
, &f
->fs_reclaim
);
472 } else if (f
->fid_type
== P9_FID_DIR
) {
473 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
477 * Now drop the fid reference, free it
484 static int v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
487 V9fsState
*s
= pdu
->s
;
488 V9fsFidState
*fidp
, head_fid
;
490 head_fid
.next
= s
->fid_list
;
491 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
492 if (fidp
->path
.size
!= path
->size
) {
495 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
496 /* Mark the fid non reclaimable. */
497 fidp
->flags
|= FID_NON_RECLAIMABLE
;
499 /* reopen the file/dir if already closed */
500 err
= v9fs_reopen_fid(pdu
, fidp
);
505 * Go back to head of fid list because
506 * the list could have got updated when
507 * switched to the worker thread
517 static void virtfs_reset(V9fsPDU
*pdu
)
519 V9fsState
*s
= pdu
->s
;
520 V9fsFidState
*fidp
= NULL
;
523 while (s
->fid_list
) {
525 s
->fid_list
= fidp
->next
;
534 /* One or more unclunked fids found... */
535 error_report("9pfs:%s: One or more uncluncked fids "
536 "found during reset", __func__
);
540 #define P9_QID_TYPE_DIR 0x80
541 #define P9_QID_TYPE_SYMLINK 0x02
543 #define P9_STAT_MODE_DIR 0x80000000
544 #define P9_STAT_MODE_APPEND 0x40000000
545 #define P9_STAT_MODE_EXCL 0x20000000
546 #define P9_STAT_MODE_MOUNT 0x10000000
547 #define P9_STAT_MODE_AUTH 0x08000000
548 #define P9_STAT_MODE_TMP 0x04000000
549 #define P9_STAT_MODE_SYMLINK 0x02000000
550 #define P9_STAT_MODE_LINK 0x01000000
551 #define P9_STAT_MODE_DEVICE 0x00800000
552 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
553 #define P9_STAT_MODE_SOCKET 0x00100000
554 #define P9_STAT_MODE_SETUID 0x00080000
555 #define P9_STAT_MODE_SETGID 0x00040000
556 #define P9_STAT_MODE_SETVTX 0x00010000
558 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
559 P9_STAT_MODE_SYMLINK | \
560 P9_STAT_MODE_LINK | \
561 P9_STAT_MODE_DEVICE | \
562 P9_STAT_MODE_NAMED_PIPE | \
565 /* This is the algorithm from ufs in spfs */
566 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
570 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
571 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
572 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
573 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
575 if (S_ISDIR(stbuf
->st_mode
)) {
576 qidp
->type
|= P9_QID_TYPE_DIR
;
578 if (S_ISLNK(stbuf
->st_mode
)) {
579 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
583 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
588 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
592 stat_to_qid(&stbuf
, qidp
);
596 V9fsPDU
*pdu_alloc(V9fsState
*s
)
600 if (!QLIST_EMPTY(&s
->free_list
)) {
601 pdu
= QLIST_FIRST(&s
->free_list
);
602 QLIST_REMOVE(pdu
, next
);
603 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
608 void pdu_free(V9fsPDU
*pdu
)
611 V9fsState
*s
= pdu
->s
;
613 * Cancelled pdu are added back to the freelist
616 if (!pdu
->cancelled
) {
617 QLIST_REMOVE(pdu
, next
);
618 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
624 * We don't do error checking for pdu_marshal/unmarshal here
625 * because we always expect to have enough space to encode
628 static void pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
630 int8_t id
= pdu
->id
+ 1; /* Response */
631 V9fsState
*s
= pdu
->s
;
637 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
640 str
.data
= strerror(err
);
641 str
.size
= strlen(str
.data
);
643 len
+= pdu_marshal(pdu
, len
, "s", &str
);
647 len
+= pdu_marshal(pdu
, len
, "d", err
);
649 if (s
->proto_version
== V9FS_PROTO_2000L
) {
652 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
655 /* fill out the header */
656 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
658 /* keep these in sync */
662 pdu_push_and_notify(pdu
);
664 /* Now wakeup anybody waiting in flush for this request */
665 qemu_co_queue_next(&pdu
->complete
);
670 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
675 if (mode
& P9_STAT_MODE_DIR
) {
679 if (mode
& P9_STAT_MODE_SYMLINK
) {
682 if (mode
& P9_STAT_MODE_SOCKET
) {
685 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
688 if (mode
& P9_STAT_MODE_DEVICE
) {
689 if (extension
->size
&& extension
->data
[0] == 'c') {
700 if (mode
& P9_STAT_MODE_SETUID
) {
703 if (mode
& P9_STAT_MODE_SETGID
) {
706 if (mode
& P9_STAT_MODE_SETVTX
) {
713 static int donttouch_stat(V9fsStat
*stat
)
715 if (stat
->type
== -1 &&
717 stat
->qid
.type
== -1 &&
718 stat
->qid
.version
== -1 &&
719 stat
->qid
.path
== -1 &&
723 stat
->length
== -1 &&
730 stat
->n_muid
== -1) {
737 static void v9fs_stat_init(V9fsStat
*stat
)
739 v9fs_string_init(&stat
->name
);
740 v9fs_string_init(&stat
->uid
);
741 v9fs_string_init(&stat
->gid
);
742 v9fs_string_init(&stat
->muid
);
743 v9fs_string_init(&stat
->extension
);
746 static void v9fs_stat_free(V9fsStat
*stat
)
748 v9fs_string_free(&stat
->name
);
749 v9fs_string_free(&stat
->uid
);
750 v9fs_string_free(&stat
->gid
);
751 v9fs_string_free(&stat
->muid
);
752 v9fs_string_free(&stat
->extension
);
755 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
759 mode
= stbuf
->st_mode
& 0777;
760 if (S_ISDIR(stbuf
->st_mode
)) {
761 mode
|= P9_STAT_MODE_DIR
;
764 if (S_ISLNK(stbuf
->st_mode
)) {
765 mode
|= P9_STAT_MODE_SYMLINK
;
768 if (S_ISSOCK(stbuf
->st_mode
)) {
769 mode
|= P9_STAT_MODE_SOCKET
;
772 if (S_ISFIFO(stbuf
->st_mode
)) {
773 mode
|= P9_STAT_MODE_NAMED_PIPE
;
776 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
777 mode
|= P9_STAT_MODE_DEVICE
;
780 if (stbuf
->st_mode
& S_ISUID
) {
781 mode
|= P9_STAT_MODE_SETUID
;
784 if (stbuf
->st_mode
& S_ISGID
) {
785 mode
|= P9_STAT_MODE_SETGID
;
788 if (stbuf
->st_mode
& S_ISVTX
) {
789 mode
|= P9_STAT_MODE_SETVTX
;
795 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
796 const struct stat
*stbuf
,
802 memset(v9stat
, 0, sizeof(*v9stat
));
804 stat_to_qid(stbuf
, &v9stat
->qid
);
805 v9stat
->mode
= stat_to_v9mode(stbuf
);
806 v9stat
->atime
= stbuf
->st_atime
;
807 v9stat
->mtime
= stbuf
->st_mtime
;
808 v9stat
->length
= stbuf
->st_size
;
810 v9fs_string_null(&v9stat
->uid
);
811 v9fs_string_null(&v9stat
->gid
);
812 v9fs_string_null(&v9stat
->muid
);
814 v9stat
->n_uid
= stbuf
->st_uid
;
815 v9stat
->n_gid
= stbuf
->st_gid
;
818 v9fs_string_null(&v9stat
->extension
);
820 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
821 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
825 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
826 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
827 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
828 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
829 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
830 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
831 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
834 str
= strrchr(name
->data
, '/');
841 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
844 v9fs_string_size(&v9stat
->name
) +
845 v9fs_string_size(&v9stat
->uid
) +
846 v9fs_string_size(&v9stat
->gid
) +
847 v9fs_string_size(&v9stat
->muid
) +
848 v9fs_string_size(&v9stat
->extension
);
852 #define P9_STATS_MODE 0x00000001ULL
853 #define P9_STATS_NLINK 0x00000002ULL
854 #define P9_STATS_UID 0x00000004ULL
855 #define P9_STATS_GID 0x00000008ULL
856 #define P9_STATS_RDEV 0x00000010ULL
857 #define P9_STATS_ATIME 0x00000020ULL
858 #define P9_STATS_MTIME 0x00000040ULL
859 #define P9_STATS_CTIME 0x00000080ULL
860 #define P9_STATS_INO 0x00000100ULL
861 #define P9_STATS_SIZE 0x00000200ULL
862 #define P9_STATS_BLOCKS 0x00000400ULL
864 #define P9_STATS_BTIME 0x00000800ULL
865 #define P9_STATS_GEN 0x00001000ULL
866 #define P9_STATS_DATA_VERSION 0x00002000ULL
868 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
869 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
872 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
873 V9fsStatDotl
*v9lstat
)
875 memset(v9lstat
, 0, sizeof(*v9lstat
));
877 v9lstat
->st_mode
= stbuf
->st_mode
;
878 v9lstat
->st_nlink
= stbuf
->st_nlink
;
879 v9lstat
->st_uid
= stbuf
->st_uid
;
880 v9lstat
->st_gid
= stbuf
->st_gid
;
881 v9lstat
->st_rdev
= stbuf
->st_rdev
;
882 v9lstat
->st_size
= stbuf
->st_size
;
883 v9lstat
->st_blksize
= stbuf
->st_blksize
;
884 v9lstat
->st_blocks
= stbuf
->st_blocks
;
885 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
886 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
887 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
888 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
889 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
890 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
891 /* Currently we only support BASIC fields in stat */
892 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
894 stat_to_qid(stbuf
, &v9lstat
->qid
);
897 static void print_sg(struct iovec
*sg
, int cnt
)
901 printf("sg[%d]: {", cnt
);
902 for (i
= 0; i
< cnt
; i
++) {
906 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
911 /* Will call this only for path name based fid */
912 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
915 v9fs_path_init(&str
);
916 v9fs_path_copy(&str
, dst
);
917 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
918 v9fs_path_free(&str
);
919 /* +1 to include terminating NULL */
923 static inline bool is_ro_export(FsContext
*ctx
)
925 return ctx
->export_flags
& V9FS_RDONLY
;
928 static void v9fs_version(void *opaque
)
931 V9fsPDU
*pdu
= opaque
;
932 V9fsState
*s
= pdu
->s
;
936 v9fs_string_init(&version
);
937 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
942 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
946 if (!strcmp(version
.data
, "9P2000.u")) {
947 s
->proto_version
= V9FS_PROTO_2000U
;
948 } else if (!strcmp(version
.data
, "9P2000.L")) {
949 s
->proto_version
= V9FS_PROTO_2000L
;
951 v9fs_string_sprintf(&version
, "unknown");
954 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
960 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
962 pdu_complete(pdu
, offset
);
963 v9fs_string_free(&version
);
966 static void v9fs_attach(void *opaque
)
968 V9fsPDU
*pdu
= opaque
;
969 V9fsState
*s
= pdu
->s
;
970 int32_t fid
, afid
, n_uname
;
971 V9fsString uname
, aname
;
977 v9fs_string_init(&uname
);
978 v9fs_string_init(&aname
);
979 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
980 &afid
, &uname
, &aname
, &n_uname
);
984 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
986 fidp
= alloc_fid(s
, fid
);
992 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
998 err
= fid_to_qid(pdu
, fidp
, &qid
);
1004 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1010 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1011 qid
.type
, qid
.version
, qid
.path
);
1013 * disable migration if we haven't done already.
1014 * attach could get called multiple times for the same export.
1016 if (!s
->migration_blocker
) {
1018 error_setg(&s
->migration_blocker
,
1019 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1020 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1021 migrate_add_blocker(s
->migration_blocker
);
1026 pdu_complete(pdu
, err
);
1027 v9fs_string_free(&uname
);
1028 v9fs_string_free(&aname
);
1031 static void v9fs_stat(void *opaque
)
1039 V9fsPDU
*pdu
= opaque
;
1041 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1045 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1047 fidp
= get_fid(pdu
, fid
);
1052 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1056 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1060 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1062 v9fs_stat_free(&v9stat
);
1065 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1066 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1068 v9fs_stat_free(&v9stat
);
1072 pdu_complete(pdu
, err
);
1075 static void v9fs_getattr(void *opaque
)
1082 uint64_t request_mask
;
1083 V9fsStatDotl v9stat_dotl
;
1084 V9fsPDU
*pdu
= opaque
;
1085 V9fsState
*s
= pdu
->s
;
1087 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1091 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1093 fidp
= get_fid(pdu
, fid
);
1099 * Currently we only support BASIC fields in stat, so there is no
1100 * need to look at request_mask.
1102 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1106 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1108 /* fill st_gen if requested and supported by underlying fs */
1109 if (request_mask
& P9_STATS_GEN
) {
1110 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1113 /* we have valid st_gen: update result mask */
1114 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1117 /* request cancelled, e.g. by Tflush */
1120 /* failed to get st_gen: not fatal, ignore */
1124 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1129 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1130 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1131 v9stat_dotl
.st_gid
);
1135 pdu_complete(pdu
, retval
);
1138 /* Attribute flags */
1139 #define P9_ATTR_MODE (1 << 0)
1140 #define P9_ATTR_UID (1 << 1)
1141 #define P9_ATTR_GID (1 << 2)
1142 #define P9_ATTR_SIZE (1 << 3)
1143 #define P9_ATTR_ATIME (1 << 4)
1144 #define P9_ATTR_MTIME (1 << 5)
1145 #define P9_ATTR_CTIME (1 << 6)
1146 #define P9_ATTR_ATIME_SET (1 << 7)
1147 #define P9_ATTR_MTIME_SET (1 << 8)
1149 #define P9_ATTR_MASK 127
1151 static void v9fs_setattr(void *opaque
)
1158 V9fsPDU
*pdu
= opaque
;
1160 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1165 fidp
= get_fid(pdu
, fid
);
1170 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1171 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1176 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1177 struct timespec times
[2];
1178 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1179 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1180 times
[0].tv_sec
= v9iattr
.atime_sec
;
1181 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1183 times
[0].tv_nsec
= UTIME_NOW
;
1186 times
[0].tv_nsec
= UTIME_OMIT
;
1188 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1189 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1190 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1191 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1193 times
[1].tv_nsec
= UTIME_NOW
;
1196 times
[1].tv_nsec
= UTIME_OMIT
;
1198 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1204 * If the only valid entry in iattr is ctime we can call
1205 * chown(-1,-1) to update the ctime of the file
1207 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1208 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1209 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1210 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1213 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1216 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1222 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1223 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1232 pdu_complete(pdu
, err
);
1235 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1241 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1246 for (i
= 0; i
< nwnames
; i
++) {
1247 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1256 static void v9fs_walk(void *opaque
)
1259 V9fsQID
*qids
= NULL
;
1261 V9fsPath dpath
, path
;
1265 int32_t fid
, newfid
;
1266 V9fsString
*wnames
= NULL
;
1268 V9fsFidState
*newfidp
= NULL
;
1269 V9fsPDU
*pdu
= opaque
;
1270 V9fsState
*s
= pdu
->s
;
1272 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1274 pdu_complete(pdu
, err
);
1279 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1281 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1282 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1283 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1284 for (i
= 0; i
< nwnames
; i
++) {
1285 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1291 } else if (nwnames
> P9_MAXWELEM
) {
1295 fidp
= get_fid(pdu
, fid
);
1300 v9fs_path_init(&dpath
);
1301 v9fs_path_init(&path
);
1303 * Both dpath and path initially poin to fidp.
1304 * Needed to handle request with nwnames == 0
1306 v9fs_path_copy(&dpath
, &fidp
->path
);
1307 v9fs_path_copy(&path
, &fidp
->path
);
1308 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1309 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1313 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1317 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1318 v9fs_path_copy(&dpath
, &path
);
1320 if (fid
== newfid
) {
1321 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1322 v9fs_path_copy(&fidp
->path
, &path
);
1324 newfidp
= alloc_fid(s
, newfid
);
1325 if (newfidp
== NULL
) {
1329 newfidp
->uid
= fidp
->uid
;
1330 v9fs_path_copy(&newfidp
->path
, &path
);
1332 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1333 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1337 put_fid(pdu
, newfidp
);
1339 v9fs_path_free(&dpath
);
1340 v9fs_path_free(&path
);
1342 pdu_complete(pdu
, err
);
1343 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1344 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1345 v9fs_string_free(&wnames
[name_idx
]);
1352 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1354 struct statfs stbuf
;
1356 V9fsState
*s
= pdu
->s
;
1359 * iounit should be multiples of f_bsize (host filesystem block size
1360 * and as well as less than (client msize - P9_IOHDRSZ))
1362 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1363 iounit
= stbuf
.f_bsize
;
1364 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1367 iounit
= s
->msize
- P9_IOHDRSZ
;
1372 static void v9fs_open(void *opaque
)
1383 V9fsPDU
*pdu
= opaque
;
1384 V9fsState
*s
= pdu
->s
;
1386 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1387 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1390 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1396 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1398 fidp
= get_fid(pdu
, fid
);
1403 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1405 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1409 stat_to_qid(&stbuf
, &qid
);
1410 if (S_ISDIR(stbuf
.st_mode
)) {
1411 err
= v9fs_co_opendir(pdu
, fidp
);
1415 fidp
->fid_type
= P9_FID_DIR
;
1416 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1422 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1423 flags
= get_dotl_openflags(s
, mode
);
1425 flags
= omode_to_uflags(mode
);
1427 if (is_ro_export(&s
->ctx
)) {
1428 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1429 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1434 err
= v9fs_co_open(pdu
, fidp
, flags
);
1438 fidp
->fid_type
= P9_FID_FILE
;
1439 fidp
->open_flags
= flags
;
1440 if (flags
& O_EXCL
) {
1442 * We let the host file system do O_EXCL check
1443 * We should not reclaim such fd
1445 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1447 iounit
= get_iounit(pdu
, &fidp
->path
);
1448 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1454 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1455 qid
.type
, qid
.version
, qid
.path
, iounit
);
1459 pdu_complete(pdu
, err
);
1462 static void v9fs_lcreate(void *opaque
)
1464 int32_t dfid
, flags
, mode
;
1473 V9fsPDU
*pdu
= opaque
;
1475 v9fs_string_init(&name
);
1476 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1477 &name
, &flags
, &mode
, &gid
);
1481 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1483 fidp
= get_fid(pdu
, dfid
);
1489 flags
= get_dotl_openflags(pdu
->s
, flags
);
1490 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1491 flags
| O_CREAT
, mode
, &stbuf
);
1495 fidp
->fid_type
= P9_FID_FILE
;
1496 fidp
->open_flags
= flags
;
1497 if (flags
& O_EXCL
) {
1499 * We let the host file system do O_EXCL check
1500 * We should not reclaim such fd
1502 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1504 iounit
= get_iounit(pdu
, &fidp
->path
);
1505 stat_to_qid(&stbuf
, &qid
);
1506 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1511 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1512 qid
.type
, qid
.version
, qid
.path
, iounit
);
1516 pdu_complete(pdu
, err
);
1517 v9fs_string_free(&name
);
1520 static void v9fs_fsync(void *opaque
)
1527 V9fsPDU
*pdu
= opaque
;
1529 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1533 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1535 fidp
= get_fid(pdu
, fid
);
1540 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1546 pdu_complete(pdu
, err
);
1549 static void v9fs_clunk(void *opaque
)
1555 V9fsPDU
*pdu
= opaque
;
1556 V9fsState
*s
= pdu
->s
;
1558 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1562 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1564 fidp
= clunk_fid(s
, fid
);
1570 * Bump the ref so that put_fid will
1574 err
= put_fid(pdu
, fidp
);
1579 pdu_complete(pdu
, err
);
1582 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1583 uint64_t off
, uint32_t max_count
)
1589 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
1590 VirtQueueElement
*elem
= &v
->elems
[pdu
->idx
];
1592 xattr_len
= fidp
->fs
.xattr
.len
;
1593 read_count
= xattr_len
- off
;
1594 if (read_count
> max_count
) {
1595 read_count
= max_count
;
1596 } else if (read_count
< 0) {
1598 * read beyond XATTR value
1602 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1608 err
= v9fs_pack(elem
->in_sg
, elem
->in_num
, offset
,
1609 ((char *)fidp
->fs
.xattr
.value
) + off
,
1618 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1619 V9fsFidState
*fidp
, uint32_t max_count
)
1626 off_t saved_dir_pos
;
1627 struct dirent
*dent
, *result
;
1629 /* save the directory position */
1630 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1631 if (saved_dir_pos
< 0) {
1632 return saved_dir_pos
;
1635 dent
= g_malloc(sizeof(struct dirent
));
1638 v9fs_path_init(&path
);
1639 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1640 if (err
|| !result
) {
1643 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1647 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1651 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1655 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1656 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1657 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1658 /* Ran out of buffer. Set dir back to old position and return */
1659 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1660 v9fs_stat_free(&v9stat
);
1661 v9fs_path_free(&path
);
1666 v9fs_stat_free(&v9stat
);
1667 v9fs_path_free(&path
);
1668 saved_dir_pos
= dent
->d_off
;
1672 v9fs_path_free(&path
);
1680 * Create a QEMUIOVector for a sub-region of PDU iovecs
1682 * @qiov: uninitialized QEMUIOVector
1683 * @skip: number of bytes to skip from beginning of PDU
1684 * @size: number of bytes to include
1685 * @is_write: true - write, false - read
1687 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1688 * with qemu_iovec_destroy().
1690 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1691 size_t skip
, size_t size
,
1698 virtio_init_iov_from_pdu(pdu
, &iov
, &niov
, is_write
);
1700 qemu_iovec_init_external(&elem
, iov
, niov
);
1701 qemu_iovec_init(qiov
, niov
);
1702 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1705 static void v9fs_read(void *opaque
)
1714 V9fsPDU
*pdu
= opaque
;
1715 V9fsState
*s
= pdu
->s
;
1717 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1721 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1723 fidp
= get_fid(pdu
, fid
);
1728 if (fidp
->fid_type
== P9_FID_DIR
) {
1731 v9fs_co_rewinddir(pdu
, fidp
);
1733 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1738 err
= pdu_marshal(pdu
, offset
, "d", count
);
1742 err
+= offset
+ count
;
1743 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1744 QEMUIOVector qiov_full
;
1748 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1749 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1751 qemu_iovec_reset(&qiov
);
1752 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1754 print_sg(qiov
.iov
, qiov
.niov
);
1756 /* Loop in case of EINTR */
1758 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1763 } while (len
== -EINTR
&& !pdu
->cancelled
);
1765 /* IO error return the error */
1769 } while (count
< max_count
&& len
> 0);
1770 err
= pdu_marshal(pdu
, offset
, "d", count
);
1774 err
+= offset
+ count
;
1775 qemu_iovec_destroy(&qiov
);
1776 qemu_iovec_destroy(&qiov_full
);
1777 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1778 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1782 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1786 pdu_complete(pdu
, err
);
1789 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1792 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1793 * size of type (1) + size of name.size (2) + strlen(name.data)
1795 return 24 + v9fs_string_size(name
);
1798 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1799 V9fsFidState
*fidp
, int32_t max_count
)
1806 off_t saved_dir_pos
;
1807 struct dirent
*dent
, *result
;
1809 /* save the directory position */
1810 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1811 if (saved_dir_pos
< 0) {
1812 return saved_dir_pos
;
1815 dent
= g_malloc(sizeof(struct dirent
));
1818 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1819 if (err
|| !result
) {
1822 v9fs_string_init(&name
);
1823 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1824 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1825 /* Ran out of buffer. Set dir back to old position and return */
1826 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1827 v9fs_string_free(&name
);
1832 * Fill up just the path field of qid because the client uses
1833 * only that. To fill the entire qid structure we will have
1834 * to stat each dirent found, which is expensive
1836 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1837 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1838 /* Fill the other fields with dummy values */
1842 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1843 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1845 dent
->d_type
, &name
);
1847 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1848 v9fs_string_free(&name
);
1853 v9fs_string_free(&name
);
1854 saved_dir_pos
= dent
->d_off
;
1863 static void v9fs_readdir(void *opaque
)
1869 uint64_t initial_offset
;
1872 V9fsPDU
*pdu
= opaque
;
1874 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1875 &initial_offset
, &max_count
);
1879 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1881 fidp
= get_fid(pdu
, fid
);
1886 if (!fidp
->fs
.dir
) {
1890 if (initial_offset
== 0) {
1891 v9fs_co_rewinddir(pdu
, fidp
);
1893 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1895 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1900 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1904 retval
+= count
+ offset
;
1905 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1909 pdu_complete(pdu
, retval
);
1912 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1913 uint64_t off
, uint32_t count
,
1914 struct iovec
*sg
, int cnt
)
1923 xattr_len
= fidp
->fs
.xattr
.len
;
1924 write_count
= xattr_len
- off
;
1925 if (write_count
> count
) {
1926 write_count
= count
;
1927 } else if (write_count
< 0) {
1929 * write beyond XATTR value len specified in
1935 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1940 fidp
->fs
.xattr
.copied_len
+= write_count
;
1942 * Now copy the content from sg list
1944 for (i
= 0; i
< cnt
; i
++) {
1945 if (write_count
> sg
[i
].iov_len
) {
1946 to_copy
= sg
[i
].iov_len
;
1948 to_copy
= write_count
;
1950 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1951 /* updating vs->off since we are not using below */
1953 write_count
-= to_copy
;
1959 static void v9fs_write(void *opaque
)
1969 V9fsPDU
*pdu
= opaque
;
1970 V9fsState
*s
= pdu
->s
;
1971 QEMUIOVector qiov_full
;
1974 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
1976 pdu_complete(pdu
, err
);
1980 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
1981 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
1983 fidp
= get_fid(pdu
, fid
);
1988 if (fidp
->fid_type
== P9_FID_FILE
) {
1989 if (fidp
->fs
.fd
== -1) {
1993 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1995 * setxattr operation
1997 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
1998 qiov_full
.iov
, qiov_full
.niov
);
2004 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2006 qemu_iovec_reset(&qiov
);
2007 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2009 print_sg(qiov
.iov
, qiov
.niov
);
2011 /* Loop in case of EINTR */
2013 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2018 } while (len
== -EINTR
&& !pdu
->cancelled
);
2020 /* IO error return the error */
2024 } while (total
< count
&& len
> 0);
2027 err
= pdu_marshal(pdu
, offset
, "d", total
);
2032 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2034 qemu_iovec_destroy(&qiov
);
2038 qemu_iovec_destroy(&qiov_full
);
2039 pdu_complete(pdu
, err
);
2042 static void v9fs_create(void *opaque
)
2054 V9fsString extension
;
2056 V9fsPDU
*pdu
= opaque
;
2058 v9fs_path_init(&path
);
2059 v9fs_string_init(&name
);
2060 v9fs_string_init(&extension
);
2061 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2062 &perm
, &mode
, &extension
);
2066 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2068 fidp
= get_fid(pdu
, fid
);
2073 if (perm
& P9_STAT_MODE_DIR
) {
2074 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2075 fidp
->uid
, -1, &stbuf
);
2079 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2083 v9fs_path_copy(&fidp
->path
, &path
);
2084 err
= v9fs_co_opendir(pdu
, fidp
);
2088 fidp
->fid_type
= P9_FID_DIR
;
2089 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2090 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2091 extension
.data
, -1 , &stbuf
);
2095 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2099 v9fs_path_copy(&fidp
->path
, &path
);
2100 } else if (perm
& P9_STAT_MODE_LINK
) {
2101 int32_t ofid
= atoi(extension
.data
);
2102 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2103 if (ofidp
== NULL
) {
2107 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2108 put_fid(pdu
, ofidp
);
2112 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2114 fidp
->fid_type
= P9_FID_NONE
;
2117 v9fs_path_copy(&fidp
->path
, &path
);
2118 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2120 fidp
->fid_type
= P9_FID_NONE
;
2123 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2125 uint32_t major
, minor
;
2128 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2145 nmode
|= perm
& 0777;
2146 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2147 makedev(major
, minor
), nmode
, &stbuf
);
2151 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2155 v9fs_path_copy(&fidp
->path
, &path
);
2156 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2157 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2158 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2162 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2166 v9fs_path_copy(&fidp
->path
, &path
);
2167 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2168 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2169 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2173 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2177 v9fs_path_copy(&fidp
->path
, &path
);
2179 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2180 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2184 fidp
->fid_type
= P9_FID_FILE
;
2185 fidp
->open_flags
= omode_to_uflags(mode
);
2186 if (fidp
->open_flags
& O_EXCL
) {
2188 * We let the host file system do O_EXCL check
2189 * We should not reclaim such fd
2191 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2194 iounit
= get_iounit(pdu
, &fidp
->path
);
2195 stat_to_qid(&stbuf
, &qid
);
2196 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2201 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2202 qid
.type
, qid
.version
, qid
.path
, iounit
);
2206 pdu_complete(pdu
, err
);
2207 v9fs_string_free(&name
);
2208 v9fs_string_free(&extension
);
2209 v9fs_path_free(&path
);
2212 static void v9fs_symlink(void *opaque
)
2214 V9fsPDU
*pdu
= opaque
;
2217 V9fsFidState
*dfidp
;
2225 v9fs_string_init(&name
);
2226 v9fs_string_init(&symname
);
2227 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2231 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2233 dfidp
= get_fid(pdu
, dfid
);
2234 if (dfidp
== NULL
) {
2238 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2242 stat_to_qid(&stbuf
, &qid
);
2243 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2248 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2249 qid
.type
, qid
.version
, qid
.path
);
2251 put_fid(pdu
, dfidp
);
2253 pdu_complete(pdu
, err
);
2254 v9fs_string_free(&name
);
2255 v9fs_string_free(&symname
);
2258 static void v9fs_flush(void *opaque
)
2263 V9fsPDU
*cancel_pdu
;
2264 V9fsPDU
*pdu
= opaque
;
2265 V9fsState
*s
= pdu
->s
;
2267 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2269 pdu_complete(pdu
, err
);
2272 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2274 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2275 if (cancel_pdu
->tag
== tag
) {
2280 cancel_pdu
->cancelled
= 1;
2282 * Wait for pdu to complete.
2284 qemu_co_queue_wait(&cancel_pdu
->complete
);
2285 cancel_pdu
->cancelled
= 0;
2286 pdu_free(cancel_pdu
);
2288 pdu_complete(pdu
, 7);
2291 static void v9fs_link(void *opaque
)
2293 V9fsPDU
*pdu
= opaque
;
2294 int32_t dfid
, oldfid
;
2295 V9fsFidState
*dfidp
, *oldfidp
;
2300 v9fs_string_init(&name
);
2301 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2305 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2307 dfidp
= get_fid(pdu
, dfid
);
2308 if (dfidp
== NULL
) {
2313 oldfidp
= get_fid(pdu
, oldfid
);
2314 if (oldfidp
== NULL
) {
2318 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2323 put_fid(pdu
, dfidp
);
2325 v9fs_string_free(&name
);
2326 pdu_complete(pdu
, err
);
2329 /* Only works with path name based fid */
2330 static void v9fs_remove(void *opaque
)
2336 V9fsPDU
*pdu
= opaque
;
2338 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2342 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2344 fidp
= get_fid(pdu
, fid
);
2349 /* if fs driver is not path based, return EOPNOTSUPP */
2350 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2355 * IF the file is unlinked, we cannot reopen
2356 * the file later. So don't reclaim fd
2358 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2362 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2367 /* For TREMOVE we need to clunk the fid even on failed remove */
2368 clunk_fid(pdu
->s
, fidp
->fid
);
2371 pdu_complete(pdu
, err
);
2374 static void v9fs_unlinkat(void *opaque
)
2378 int32_t dfid
, flags
;
2381 V9fsFidState
*dfidp
;
2382 V9fsPDU
*pdu
= opaque
;
2384 v9fs_string_init(&name
);
2385 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2389 dfidp
= get_fid(pdu
, dfid
);
2390 if (dfidp
== NULL
) {
2395 * IF the file is unlinked, we cannot reopen
2396 * the file later. So don't reclaim fd
2398 v9fs_path_init(&path
);
2399 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2403 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2407 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2412 put_fid(pdu
, dfidp
);
2413 v9fs_path_free(&path
);
2415 pdu_complete(pdu
, err
);
2416 v9fs_string_free(&name
);
2420 /* Only works with path name based fid */
2421 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2422 int32_t newdirfid
, V9fsString
*name
)
2427 V9fsFidState
*tfidp
;
2428 V9fsState
*s
= pdu
->s
;
2429 V9fsFidState
*dirfidp
= NULL
;
2430 char *old_name
, *new_name
;
2432 v9fs_path_init(&new_path
);
2433 if (newdirfid
!= -1) {
2434 dirfidp
= get_fid(pdu
, newdirfid
);
2435 if (dirfidp
== NULL
) {
2439 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2440 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2442 old_name
= fidp
->path
.data
;
2443 end
= strrchr(old_name
, '/');
2449 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2450 strncat(new_name
, old_name
, end
- old_name
);
2451 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2452 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2455 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2460 * Fixup fid's pointing to the old name to
2461 * start pointing to the new name
2463 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2464 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2465 /* replace the name */
2466 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2471 put_fid(pdu
, dirfidp
);
2473 v9fs_path_free(&new_path
);
2478 /* Only works with path name based fid */
2479 static void v9fs_rename(void *opaque
)
2487 V9fsPDU
*pdu
= opaque
;
2488 V9fsState
*s
= pdu
->s
;
2490 v9fs_string_init(&name
);
2491 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2495 fidp
= get_fid(pdu
, fid
);
2500 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2501 /* if fs driver is not path based, return EOPNOTSUPP */
2502 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2506 v9fs_path_write_lock(s
);
2507 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2508 v9fs_path_unlock(s
);
2515 pdu_complete(pdu
, err
);
2516 v9fs_string_free(&name
);
2519 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2520 V9fsString
*old_name
, V9fsPath
*newdir
,
2521 V9fsString
*new_name
)
2523 V9fsFidState
*tfidp
;
2524 V9fsPath oldpath
, newpath
;
2525 V9fsState
*s
= pdu
->s
;
2528 v9fs_path_init(&oldpath
);
2529 v9fs_path_init(&newpath
);
2530 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2531 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2534 * Fixup fid's pointing to the old name to
2535 * start pointing to the new name
2537 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2538 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2539 /* replace the name */
2540 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2543 v9fs_path_free(&oldpath
);
2544 v9fs_path_free(&newpath
);
2547 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2548 V9fsString
*old_name
, int32_t newdirfid
,
2549 V9fsString
*new_name
)
2552 V9fsState
*s
= pdu
->s
;
2553 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2555 olddirfidp
= get_fid(pdu
, olddirfid
);
2556 if (olddirfidp
== NULL
) {
2560 if (newdirfid
!= -1) {
2561 newdirfidp
= get_fid(pdu
, newdirfid
);
2562 if (newdirfidp
== NULL
) {
2567 newdirfidp
= get_fid(pdu
, olddirfid
);
2570 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2571 &newdirfidp
->path
, new_name
);
2575 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2576 /* Only for path based fid we need to do the below fixup */
2577 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2578 &newdirfidp
->path
, new_name
);
2582 put_fid(pdu
, olddirfidp
);
2585 put_fid(pdu
, newdirfidp
);
2590 static void v9fs_renameat(void *opaque
)
2594 V9fsPDU
*pdu
= opaque
;
2595 V9fsState
*s
= pdu
->s
;
2596 int32_t olddirfid
, newdirfid
;
2597 V9fsString old_name
, new_name
;
2599 v9fs_string_init(&old_name
);
2600 v9fs_string_init(&new_name
);
2601 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2602 &old_name
, &newdirfid
, &new_name
);
2607 v9fs_path_write_lock(s
);
2608 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2609 &old_name
, newdirfid
, &new_name
);
2610 v9fs_path_unlock(s
);
2616 pdu_complete(pdu
, err
);
2617 v9fs_string_free(&old_name
);
2618 v9fs_string_free(&new_name
);
2621 static void v9fs_wstat(void *opaque
)
2630 V9fsPDU
*pdu
= opaque
;
2632 v9fs_stat_init(&v9stat
);
2633 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2637 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2638 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2640 fidp
= get_fid(pdu
, fid
);
2645 /* do we need to sync the file? */
2646 if (donttouch_stat(&v9stat
)) {
2647 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2650 if (v9stat
.mode
!= -1) {
2652 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2656 v9_mode
= stat_to_v9mode(&stbuf
);
2657 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2658 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2659 /* Attempting to change the type */
2663 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2664 v9mode_to_mode(v9stat
.mode
,
2665 &v9stat
.extension
));
2670 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2671 struct timespec times
[2];
2672 if (v9stat
.atime
!= -1) {
2673 times
[0].tv_sec
= v9stat
.atime
;
2674 times
[0].tv_nsec
= 0;
2676 times
[0].tv_nsec
= UTIME_OMIT
;
2678 if (v9stat
.mtime
!= -1) {
2679 times
[1].tv_sec
= v9stat
.mtime
;
2680 times
[1].tv_nsec
= 0;
2682 times
[1].tv_nsec
= UTIME_OMIT
;
2684 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2689 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2690 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2695 if (v9stat
.name
.size
!= 0) {
2696 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2701 if (v9stat
.length
!= -1) {
2702 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2711 v9fs_stat_free(&v9stat
);
2712 pdu_complete(pdu
, err
);
2715 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2727 int32_t bsize_factor
;
2730 * compute bsize factor based on host file system block size
2733 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2734 if (!bsize_factor
) {
2737 f_type
= stbuf
->f_type
;
2738 f_bsize
= stbuf
->f_bsize
;
2739 f_bsize
*= bsize_factor
;
2741 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2742 * adjust(divide) the number of blocks, free blocks and available
2743 * blocks by bsize factor
2745 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2746 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2747 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2748 f_files
= stbuf
->f_files
;
2749 f_ffree
= stbuf
->f_ffree
;
2750 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2751 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2752 f_namelen
= stbuf
->f_namelen
;
2754 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2755 f_type
, f_bsize
, f_blocks
, f_bfree
,
2756 f_bavail
, f_files
, f_ffree
,
2757 fsid_val
, f_namelen
);
2760 static void v9fs_statfs(void *opaque
)
2766 struct statfs stbuf
;
2767 V9fsPDU
*pdu
= opaque
;
2768 V9fsState
*s
= pdu
->s
;
2770 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2774 fidp
= get_fid(pdu
, fid
);
2779 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2783 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2791 pdu_complete(pdu
, retval
);
2794 static void v9fs_mknod(void *opaque
)
2807 V9fsPDU
*pdu
= opaque
;
2809 v9fs_string_init(&name
);
2810 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2811 &major
, &minor
, &gid
);
2815 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2817 fidp
= get_fid(pdu
, fid
);
2822 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2823 makedev(major
, minor
), mode
, &stbuf
);
2827 stat_to_qid(&stbuf
, &qid
);
2828 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2833 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2834 qid
.type
, qid
.version
, qid
.path
);
2838 pdu_complete(pdu
, err
);
2839 v9fs_string_free(&name
);
2843 * Implement posix byte range locking code
2844 * Server side handling of locking code is very simple, because 9p server in
2845 * QEMU can handle only one client. And most of the lock handling
2846 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2847 * do any thing in * qemu 9p server side lock code path.
2848 * So when a TLOCK request comes, always return success
2850 static void v9fs_lock(void *opaque
)
2857 int32_t fid
, err
= 0;
2858 V9fsPDU
*pdu
= opaque
;
2860 status
= P9_LOCK_ERROR
;
2861 v9fs_string_init(&flock
.client_id
);
2862 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2863 &flock
.flags
, &flock
.start
, &flock
.length
,
2864 &flock
.proc_id
, &flock
.client_id
);
2868 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2869 flock
.type
, flock
.start
, flock
.length
);
2872 /* We support only block flag now (that too ignored currently) */
2873 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2877 fidp
= get_fid(pdu
, fid
);
2882 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2886 status
= P9_LOCK_SUCCESS
;
2890 err
= pdu_marshal(pdu
, offset
, "b", status
);
2894 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2895 pdu_complete(pdu
, err
);
2896 v9fs_string_free(&flock
.client_id
);
2900 * When a TGETLOCK request comes, always return success because all lock
2901 * handling is done by client's VFS layer.
2903 static void v9fs_getlock(void *opaque
)
2909 int32_t fid
, err
= 0;
2910 V9fsPDU
*pdu
= opaque
;
2912 v9fs_string_init(&glock
.client_id
);
2913 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
2914 &glock
.start
, &glock
.length
, &glock
.proc_id
,
2919 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2920 glock
.type
, glock
.start
, glock
.length
);
2922 fidp
= get_fid(pdu
, fid
);
2927 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2931 glock
.type
= P9_LOCK_TYPE_UNLCK
;
2932 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
2933 glock
.start
, glock
.length
, glock
.proc_id
,
2939 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
2940 glock
.length
, glock
.proc_id
);
2944 pdu_complete(pdu
, err
);
2945 v9fs_string_free(&glock
.client_id
);
2948 static void v9fs_mkdir(void *opaque
)
2950 V9fsPDU
*pdu
= opaque
;
2961 v9fs_string_init(&name
);
2962 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2966 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
2968 fidp
= get_fid(pdu
, fid
);
2973 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2977 stat_to_qid(&stbuf
, &qid
);
2978 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2983 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
2984 qid
.type
, qid
.version
, qid
.path
, err
);
2988 pdu_complete(pdu
, err
);
2989 v9fs_string_free(&name
);
2992 static void v9fs_xattrwalk(void *opaque
)
2998 int32_t fid
, newfid
;
2999 V9fsFidState
*file_fidp
;
3000 V9fsFidState
*xattr_fidp
= NULL
;
3001 V9fsPDU
*pdu
= opaque
;
3002 V9fsState
*s
= pdu
->s
;
3004 v9fs_string_init(&name
);
3005 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3009 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3011 file_fidp
= get_fid(pdu
, fid
);
3012 if (file_fidp
== NULL
) {
3016 xattr_fidp
= alloc_fid(s
, newfid
);
3017 if (xattr_fidp
== NULL
) {
3021 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3022 if (name
.data
== NULL
) {
3024 * listxattr request. Get the size first
3026 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3029 clunk_fid(s
, xattr_fidp
->fid
);
3033 * Read the xattr value
3035 xattr_fidp
->fs
.xattr
.len
= size
;
3036 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3037 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3039 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3040 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3041 xattr_fidp
->fs
.xattr
.value
,
3042 xattr_fidp
->fs
.xattr
.len
);
3044 clunk_fid(s
, xattr_fidp
->fid
);
3048 err
= pdu_marshal(pdu
, offset
, "q", size
);
3055 * specific xattr fid. We check for xattr
3056 * presence also collect the xattr size
3058 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3062 clunk_fid(s
, xattr_fidp
->fid
);
3066 * Read the xattr value
3068 xattr_fidp
->fs
.xattr
.len
= size
;
3069 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3070 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3072 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3073 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3074 &name
, xattr_fidp
->fs
.xattr
.value
,
3075 xattr_fidp
->fs
.xattr
.len
);
3077 clunk_fid(s
, xattr_fidp
->fid
);
3081 err
= pdu_marshal(pdu
, offset
, "q", size
);
3087 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3089 put_fid(pdu
, file_fidp
);
3091 put_fid(pdu
, xattr_fidp
);
3094 pdu_complete(pdu
, err
);
3095 v9fs_string_free(&name
);
3098 static void v9fs_xattrcreate(void *opaque
)
3106 V9fsFidState
*file_fidp
;
3107 V9fsFidState
*xattr_fidp
;
3108 V9fsPDU
*pdu
= opaque
;
3110 v9fs_string_init(&name
);
3111 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3115 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3117 file_fidp
= get_fid(pdu
, fid
);
3118 if (file_fidp
== NULL
) {
3122 /* Make the file fid point to xattr */
3123 xattr_fidp
= file_fidp
;
3124 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3125 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3126 xattr_fidp
->fs
.xattr
.len
= size
;
3127 xattr_fidp
->fs
.xattr
.flags
= flags
;
3128 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3129 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3130 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3132 put_fid(pdu
, file_fidp
);
3134 pdu_complete(pdu
, err
);
3135 v9fs_string_free(&name
);
3138 static void v9fs_readlink(void *opaque
)
3140 V9fsPDU
*pdu
= opaque
;
3147 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3151 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3152 fidp
= get_fid(pdu
, fid
);
3158 v9fs_string_init(&target
);
3159 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3163 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3165 v9fs_string_free(&target
);
3169 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3170 v9fs_string_free(&target
);
3174 pdu_complete(pdu
, err
);
3177 static CoroutineEntry
*pdu_co_handlers
[] = {
3178 [P9_TREADDIR
] = v9fs_readdir
,
3179 [P9_TSTATFS
] = v9fs_statfs
,
3180 [P9_TGETATTR
] = v9fs_getattr
,
3181 [P9_TSETATTR
] = v9fs_setattr
,
3182 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3183 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3184 [P9_TMKNOD
] = v9fs_mknod
,
3185 [P9_TRENAME
] = v9fs_rename
,
3186 [P9_TLOCK
] = v9fs_lock
,
3187 [P9_TGETLOCK
] = v9fs_getlock
,
3188 [P9_TRENAMEAT
] = v9fs_renameat
,
3189 [P9_TREADLINK
] = v9fs_readlink
,
3190 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3191 [P9_TMKDIR
] = v9fs_mkdir
,
3192 [P9_TVERSION
] = v9fs_version
,
3193 [P9_TLOPEN
] = v9fs_open
,
3194 [P9_TATTACH
] = v9fs_attach
,
3195 [P9_TSTAT
] = v9fs_stat
,
3196 [P9_TWALK
] = v9fs_walk
,
3197 [P9_TCLUNK
] = v9fs_clunk
,
3198 [P9_TFSYNC
] = v9fs_fsync
,
3199 [P9_TOPEN
] = v9fs_open
,
3200 [P9_TREAD
] = v9fs_read
,
3202 [P9_TAUTH
] = v9fs_auth
,
3204 [P9_TFLUSH
] = v9fs_flush
,
3205 [P9_TLINK
] = v9fs_link
,
3206 [P9_TSYMLINK
] = v9fs_symlink
,
3207 [P9_TCREATE
] = v9fs_create
,
3208 [P9_TLCREATE
] = v9fs_lcreate
,
3209 [P9_TWRITE
] = v9fs_write
,
3210 [P9_TWSTAT
] = v9fs_wstat
,
3211 [P9_TREMOVE
] = v9fs_remove
,
3214 static void v9fs_op_not_supp(void *opaque
)
3216 V9fsPDU
*pdu
= opaque
;
3217 pdu_complete(pdu
, -EOPNOTSUPP
);
3220 static void v9fs_fs_ro(void *opaque
)
3222 V9fsPDU
*pdu
= opaque
;
3223 pdu_complete(pdu
, -EROFS
);
3226 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3253 void pdu_submit(V9fsPDU
*pdu
)
3256 CoroutineEntry
*handler
;
3257 V9fsState
*s
= pdu
->s
;
3259 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3260 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3261 handler
= v9fs_op_not_supp
;
3263 handler
= pdu_co_handlers
[pdu
->id
];
3266 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3267 handler
= v9fs_fs_ro
;
3269 co
= qemu_coroutine_create(handler
);
3270 qemu_coroutine_enter(co
, pdu
);
3273 /* Returns 0 on success, 1 on failure. */
3274 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3276 V9fsVirtioState
*v
= container_of(s
, V9fsVirtioState
, state
);
3283 /* initialize pdu allocator */
3284 QLIST_INIT(&s
->free_list
);
3285 QLIST_INIT(&s
->active_list
);
3286 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3287 QLIST_INSERT_HEAD(&s
->free_list
, &v
->pdus
[i
], next
);
3292 v9fs_path_init(&path
);
3294 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3297 /* We don't have a fsdev identified by fsdev_id */
3298 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3300 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3304 if (!s
->fsconf
.tag
) {
3305 /* we haven't specified a mount_tag */
3306 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3307 s
->fsconf
.fsdev_id
);
3311 s
->ctx
.export_flags
= fse
->export_flags
;
3312 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3313 s
->ctx
.exops
.get_st_gen
= NULL
;
3314 len
= strlen(s
->fsconf
.tag
);
3315 if (len
> MAX_TAG_LEN
- 1) {
3316 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3317 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3321 s
->tag
= g_strdup(s
->fsconf
.tag
);
3327 qemu_co_rwlock_init(&s
->rename_lock
);
3329 if (s
->ops
->init(&s
->ctx
) < 0) {
3330 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3331 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3336 * Check details of export path, We need to use fs driver
3337 * call back to do that. Since we are in the init path, we don't
3338 * use co-routines here.
3340 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3342 "error in converting name to path %s", strerror(errno
));
3345 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3346 error_setg(errp
, "share path %s does not exist", fse
->path
);
3348 } else if (!S_ISDIR(stat
.st_mode
)) {
3349 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3352 v9fs_path_free(&path
);
3357 g_free(s
->ctx
.fs_root
);
3359 v9fs_path_free(&path
);
3364 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3366 g_free(s
->ctx
.fs_root
);
3370 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3373 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3374 error_report("Failed to get the resource limit");
3377 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3378 open_fd_rc
= rlim
.rlim_cur
/2;