4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "hw/virtio.h"
16 #include "qemu/sockets.h"
17 #include "hw/virtio-pci.h"
18 #include "virtio-9p.h"
19 #include "fsdev/qemu-fsdev.h"
20 #include "virtio-9p-xattr.h"
21 #include "virtio-9p-coth.h"
23 #include "migration/migration.h"
27 static int open_fd_rc
;
41 static int omode_to_uflags(int8_t mode
)
75 struct dotl_openflag_map
{
80 static int dotl_to_open_flags(int flags
)
84 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
85 * and P9_DOTL_NOACCESS
87 int oflags
= flags
& O_ACCMODE
;
89 struct dotl_openflag_map dotl_oflag_map
[] = {
90 { P9_DOTL_CREATE
, O_CREAT
},
91 { P9_DOTL_EXCL
, O_EXCL
},
92 { P9_DOTL_NOCTTY
, O_NOCTTY
},
93 { P9_DOTL_TRUNC
, O_TRUNC
},
94 { P9_DOTL_APPEND
, O_APPEND
},
95 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
96 { P9_DOTL_DSYNC
, O_DSYNC
},
97 { P9_DOTL_FASYNC
, FASYNC
},
98 { P9_DOTL_DIRECT
, O_DIRECT
},
99 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
100 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
101 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
102 { P9_DOTL_NOATIME
, O_NOATIME
},
103 { P9_DOTL_SYNC
, O_SYNC
},
106 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
107 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
108 oflags
|= dotl_oflag_map
[i
].open_flag
;
115 void cred_init(FsCred
*credp
)
123 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
127 * Filter the client open flags
129 flags
= dotl_to_open_flags(oflags
);
130 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
132 * Ignore direct disk access hint until the server supports it.
138 void v9fs_path_init(V9fsPath
*path
)
144 void v9fs_path_free(V9fsPath
*path
)
151 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
154 lhs
->data
= g_malloc(rhs
->size
);
155 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
156 lhs
->size
= rhs
->size
;
159 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
160 const char *name
, V9fsPath
*path
)
163 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
171 * Return TRUE if s1 is an ancestor of s2.
173 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
174 * As a special case, We treat s1 as ancestor of s2 if they are same!
176 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
178 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
179 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
186 static size_t v9fs_string_size(V9fsString
*str
)
192 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
193 static int v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
196 if (f
->fid_type
== P9_FID_FILE
) {
197 if (f
->fs
.fd
== -1) {
199 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
200 } while (err
== -EINTR
&& !pdu
->cancelled
);
202 } else if (f
->fid_type
== P9_FID_DIR
) {
203 if (f
->fs
.dir
== NULL
) {
205 err
= v9fs_co_opendir(pdu
, f
);
206 } while (err
== -EINTR
&& !pdu
->cancelled
);
212 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
216 V9fsState
*s
= pdu
->s
;
218 for (f
= s
->fid_list
; f
; f
= f
->next
) {
222 * Update the fid ref upfront so that
223 * we don't get reclaimed when we yield
228 * check whether we need to reopen the
229 * file. We might have closed the fd
230 * while trying to free up some file
233 err
= v9fs_reopen_fid(pdu
, f
);
239 * Mark the fid as referenced so that the LRU
240 * reclaim won't close the file descriptor
242 f
->flags
|= FID_REFERENCED
;
249 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
253 for (f
= s
->fid_list
; f
; f
= f
->next
) {
254 /* If fid is already there return NULL */
260 f
= g_malloc0(sizeof(V9fsFidState
));
262 f
->fid_type
= P9_FID_NONE
;
265 * Mark the fid as referenced so that the LRU
266 * reclaim won't close the file descriptor
268 f
->flags
|= FID_REFERENCED
;
269 f
->next
= s
->fid_list
;
275 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
279 if (fidp
->fs
.xattr
.copied_len
== -1) {
280 /* getxattr/listxattr fid */
284 * if this is fid for setxattr. clunk should
285 * result in setxattr localcall
287 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
288 /* clunk after partial write */
292 if (fidp
->fs
.xattr
.len
) {
293 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
294 fidp
->fs
.xattr
.value
,
296 fidp
->fs
.xattr
.flags
);
298 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
301 v9fs_string_free(&fidp
->fs
.xattr
.name
);
303 if (fidp
->fs
.xattr
.value
) {
304 g_free(fidp
->fs
.xattr
.value
);
309 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
313 if (fidp
->fid_type
== P9_FID_FILE
) {
314 /* If we reclaimed the fd no need to close */
315 if (fidp
->fs
.fd
!= -1) {
316 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
318 } else if (fidp
->fid_type
== P9_FID_DIR
) {
319 if (fidp
->fs
.dir
!= NULL
) {
320 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
322 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
323 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
325 v9fs_path_free(&fidp
->path
);
330 static int put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
335 * Don't free the fid if it is in reclaim list
337 if (!fidp
->ref
&& fidp
->clunked
) {
338 if (fidp
->fid
== pdu
->s
->root_fid
) {
340 * if the clunked fid is root fid then we
341 * have unmounted the fs on the client side.
342 * delete the migration blocker. Ideally, this
343 * should be hooked to transport close notification
345 if (pdu
->s
->migration_blocker
) {
346 migrate_del_blocker(pdu
->s
->migration_blocker
);
347 error_free(pdu
->s
->migration_blocker
);
348 pdu
->s
->migration_blocker
= NULL
;
351 return free_fid(pdu
, fidp
);
356 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
358 V9fsFidState
**fidpp
, *fidp
;
360 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
361 if ((*fidpp
)->fid
== fid
) {
365 if (*fidpp
== NULL
) {
374 void v9fs_reclaim_fd(V9fsPDU
*pdu
)
376 int reclaim_count
= 0;
377 V9fsState
*s
= pdu
->s
;
378 V9fsFidState
*f
, *reclaim_list
= NULL
;
380 for (f
= s
->fid_list
; f
; f
= f
->next
) {
382 * Unlink fids cannot be reclaimed. Check
383 * for them and skip them. Also skip fids
384 * currently being operated on.
386 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
390 * if it is a recently referenced fid
391 * we leave the fid untouched and clear the
392 * reference bit. We come back to it later
393 * in the next iteration. (a simple LRU without
394 * moving list elements around)
396 if (f
->flags
& FID_REFERENCED
) {
397 f
->flags
&= ~FID_REFERENCED
;
401 * Add fids to reclaim list.
403 if (f
->fid_type
== P9_FID_FILE
) {
404 if (f
->fs
.fd
!= -1) {
406 * Up the reference count so that
407 * a clunk request won't free this fid
410 f
->rclm_lst
= reclaim_list
;
412 f
->fs_reclaim
.fd
= f
->fs
.fd
;
416 } else if (f
->fid_type
== P9_FID_DIR
) {
417 if (f
->fs
.dir
!= NULL
) {
419 * Up the reference count so that
420 * a clunk request won't free this fid
423 f
->rclm_lst
= reclaim_list
;
425 f
->fs_reclaim
.dir
= f
->fs
.dir
;
430 if (reclaim_count
>= open_fd_rc
) {
435 * Now close the fid in reclaim list. Free them if they
436 * are already clunked.
438 while (reclaim_list
) {
440 reclaim_list
= f
->rclm_lst
;
441 if (f
->fid_type
== P9_FID_FILE
) {
442 v9fs_co_close(pdu
, &f
->fs_reclaim
);
443 } else if (f
->fid_type
== P9_FID_DIR
) {
444 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
448 * Now drop the fid reference, free it
455 static int v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
458 V9fsState
*s
= pdu
->s
;
459 V9fsFidState
*fidp
, head_fid
;
461 head_fid
.next
= s
->fid_list
;
462 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
463 if (fidp
->path
.size
!= path
->size
) {
466 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
467 /* Mark the fid non reclaimable. */
468 fidp
->flags
|= FID_NON_RECLAIMABLE
;
470 /* reopen the file/dir if already closed */
471 err
= v9fs_reopen_fid(pdu
, fidp
);
476 * Go back to head of fid list because
477 * the list could have got updated when
478 * switched to the worker thread
488 static void virtfs_reset(V9fsPDU
*pdu
)
490 V9fsState
*s
= pdu
->s
;
491 V9fsFidState
*fidp
= NULL
;
494 while (s
->fid_list
) {
496 s
->fid_list
= fidp
->next
;
505 /* One or more unclunked fids found... */
506 error_report("9pfs:%s: One or more uncluncked fids "
507 "found during reset", __func__
);
511 #define P9_QID_TYPE_DIR 0x80
512 #define P9_QID_TYPE_SYMLINK 0x02
514 #define P9_STAT_MODE_DIR 0x80000000
515 #define P9_STAT_MODE_APPEND 0x40000000
516 #define P9_STAT_MODE_EXCL 0x20000000
517 #define P9_STAT_MODE_MOUNT 0x10000000
518 #define P9_STAT_MODE_AUTH 0x08000000
519 #define P9_STAT_MODE_TMP 0x04000000
520 #define P9_STAT_MODE_SYMLINK 0x02000000
521 #define P9_STAT_MODE_LINK 0x01000000
522 #define P9_STAT_MODE_DEVICE 0x00800000
523 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
524 #define P9_STAT_MODE_SOCKET 0x00100000
525 #define P9_STAT_MODE_SETUID 0x00080000
526 #define P9_STAT_MODE_SETGID 0x00040000
527 #define P9_STAT_MODE_SETVTX 0x00010000
529 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
530 P9_STAT_MODE_SYMLINK | \
531 P9_STAT_MODE_LINK | \
532 P9_STAT_MODE_DEVICE | \
533 P9_STAT_MODE_NAMED_PIPE | \
536 /* This is the algorithm from ufs in spfs */
537 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
541 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
542 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
543 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
544 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
546 if (S_ISDIR(stbuf
->st_mode
)) {
547 qidp
->type
|= P9_QID_TYPE_DIR
;
549 if (S_ISLNK(stbuf
->st_mode
)) {
550 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
554 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
559 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
563 stat_to_qid(&stbuf
, qidp
);
567 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
571 if (!QLIST_EMPTY(&s
->free_list
)) {
572 pdu
= QLIST_FIRST(&s
->free_list
);
573 QLIST_REMOVE(pdu
, next
);
574 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
579 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
583 * Cancelled pdu are added back to the freelist
586 if (!pdu
->cancelled
) {
587 QLIST_REMOVE(pdu
, next
);
588 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
594 * We don't do error checking for pdu_marshal/unmarshal here
595 * because we always expect to have enough space to encode
598 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
600 int8_t id
= pdu
->id
+ 1; /* Response */
606 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
609 str
.data
= strerror(err
);
610 str
.size
= strlen(str
.data
);
612 len
+= pdu_marshal(pdu
, len
, "s", &str
);
616 len
+= pdu_marshal(pdu
, len
, "d", err
);
618 if (s
->proto_version
== V9FS_PROTO_2000L
) {
621 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
624 /* fill out the header */
625 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
627 /* keep these in sync */
631 /* push onto queue and notify */
632 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
634 /* FIXME: we should batch these completions */
635 virtio_notify(&s
->vdev
, s
->vq
);
637 /* Now wakeup anybody waiting in flush for this request */
638 qemu_co_queue_next(&pdu
->complete
);
643 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
648 if (mode
& P9_STAT_MODE_DIR
) {
652 if (mode
& P9_STAT_MODE_SYMLINK
) {
655 if (mode
& P9_STAT_MODE_SOCKET
) {
658 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
661 if (mode
& P9_STAT_MODE_DEVICE
) {
662 if (extension
&& extension
->data
[0] == 'c') {
673 if (mode
& P9_STAT_MODE_SETUID
) {
676 if (mode
& P9_STAT_MODE_SETGID
) {
679 if (mode
& P9_STAT_MODE_SETVTX
) {
686 static int donttouch_stat(V9fsStat
*stat
)
688 if (stat
->type
== -1 &&
690 stat
->qid
.type
== -1 &&
691 stat
->qid
.version
== -1 &&
692 stat
->qid
.path
== -1 &&
696 stat
->length
== -1 &&
703 stat
->n_muid
== -1) {
710 static void v9fs_stat_init(V9fsStat
*stat
)
712 v9fs_string_init(&stat
->name
);
713 v9fs_string_init(&stat
->uid
);
714 v9fs_string_init(&stat
->gid
);
715 v9fs_string_init(&stat
->muid
);
716 v9fs_string_init(&stat
->extension
);
719 static void v9fs_stat_free(V9fsStat
*stat
)
721 v9fs_string_free(&stat
->name
);
722 v9fs_string_free(&stat
->uid
);
723 v9fs_string_free(&stat
->gid
);
724 v9fs_string_free(&stat
->muid
);
725 v9fs_string_free(&stat
->extension
);
728 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
732 mode
= stbuf
->st_mode
& 0777;
733 if (S_ISDIR(stbuf
->st_mode
)) {
734 mode
|= P9_STAT_MODE_DIR
;
737 if (S_ISLNK(stbuf
->st_mode
)) {
738 mode
|= P9_STAT_MODE_SYMLINK
;
741 if (S_ISSOCK(stbuf
->st_mode
)) {
742 mode
|= P9_STAT_MODE_SOCKET
;
745 if (S_ISFIFO(stbuf
->st_mode
)) {
746 mode
|= P9_STAT_MODE_NAMED_PIPE
;
749 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
750 mode
|= P9_STAT_MODE_DEVICE
;
753 if (stbuf
->st_mode
& S_ISUID
) {
754 mode
|= P9_STAT_MODE_SETUID
;
757 if (stbuf
->st_mode
& S_ISGID
) {
758 mode
|= P9_STAT_MODE_SETGID
;
761 if (stbuf
->st_mode
& S_ISVTX
) {
762 mode
|= P9_STAT_MODE_SETVTX
;
768 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
769 const struct stat
*stbuf
,
775 memset(v9stat
, 0, sizeof(*v9stat
));
777 stat_to_qid(stbuf
, &v9stat
->qid
);
778 v9stat
->mode
= stat_to_v9mode(stbuf
);
779 v9stat
->atime
= stbuf
->st_atime
;
780 v9stat
->mtime
= stbuf
->st_mtime
;
781 v9stat
->length
= stbuf
->st_size
;
783 v9fs_string_null(&v9stat
->uid
);
784 v9fs_string_null(&v9stat
->gid
);
785 v9fs_string_null(&v9stat
->muid
);
787 v9stat
->n_uid
= stbuf
->st_uid
;
788 v9stat
->n_gid
= stbuf
->st_gid
;
791 v9fs_string_null(&v9stat
->extension
);
793 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
794 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
798 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
799 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
800 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
801 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
802 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
803 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
804 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
807 str
= strrchr(name
->data
, '/');
814 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
817 v9fs_string_size(&v9stat
->name
) +
818 v9fs_string_size(&v9stat
->uid
) +
819 v9fs_string_size(&v9stat
->gid
) +
820 v9fs_string_size(&v9stat
->muid
) +
821 v9fs_string_size(&v9stat
->extension
);
825 #define P9_STATS_MODE 0x00000001ULL
826 #define P9_STATS_NLINK 0x00000002ULL
827 #define P9_STATS_UID 0x00000004ULL
828 #define P9_STATS_GID 0x00000008ULL
829 #define P9_STATS_RDEV 0x00000010ULL
830 #define P9_STATS_ATIME 0x00000020ULL
831 #define P9_STATS_MTIME 0x00000040ULL
832 #define P9_STATS_CTIME 0x00000080ULL
833 #define P9_STATS_INO 0x00000100ULL
834 #define P9_STATS_SIZE 0x00000200ULL
835 #define P9_STATS_BLOCKS 0x00000400ULL
837 #define P9_STATS_BTIME 0x00000800ULL
838 #define P9_STATS_GEN 0x00001000ULL
839 #define P9_STATS_DATA_VERSION 0x00002000ULL
841 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
842 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
845 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
846 V9fsStatDotl
*v9lstat
)
848 memset(v9lstat
, 0, sizeof(*v9lstat
));
850 v9lstat
->st_mode
= stbuf
->st_mode
;
851 v9lstat
->st_nlink
= stbuf
->st_nlink
;
852 v9lstat
->st_uid
= stbuf
->st_uid
;
853 v9lstat
->st_gid
= stbuf
->st_gid
;
854 v9lstat
->st_rdev
= stbuf
->st_rdev
;
855 v9lstat
->st_size
= stbuf
->st_size
;
856 v9lstat
->st_blksize
= stbuf
->st_blksize
;
857 v9lstat
->st_blocks
= stbuf
->st_blocks
;
858 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
859 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
860 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
861 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
862 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
863 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
864 /* Currently we only support BASIC fields in stat */
865 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
867 stat_to_qid(stbuf
, &v9lstat
->qid
);
870 static void print_sg(struct iovec
*sg
, int cnt
)
874 printf("sg[%d]: {", cnt
);
875 for (i
= 0; i
< cnt
; i
++) {
879 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
884 /* Will call this only for path name based fid */
885 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
888 v9fs_path_init(&str
);
889 v9fs_path_copy(&str
, dst
);
890 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
891 v9fs_path_free(&str
);
892 /* +1 to include terminating NULL */
896 static inline bool is_ro_export(FsContext
*ctx
)
898 return ctx
->export_flags
& V9FS_RDONLY
;
901 static void v9fs_version(void *opaque
)
904 V9fsPDU
*pdu
= opaque
;
905 V9fsState
*s
= pdu
->s
;
909 v9fs_string_init(&version
);
910 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
915 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
919 if (!strcmp(version
.data
, "9P2000.u")) {
920 s
->proto_version
= V9FS_PROTO_2000U
;
921 } else if (!strcmp(version
.data
, "9P2000.L")) {
922 s
->proto_version
= V9FS_PROTO_2000L
;
924 v9fs_string_sprintf(&version
, "unknown");
927 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
933 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
935 complete_pdu(s
, pdu
, offset
);
936 v9fs_string_free(&version
);
939 static void v9fs_attach(void *opaque
)
941 V9fsPDU
*pdu
= opaque
;
942 V9fsState
*s
= pdu
->s
;
943 int32_t fid
, afid
, n_uname
;
944 V9fsString uname
, aname
;
950 v9fs_string_init(&uname
);
951 v9fs_string_init(&aname
);
952 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
953 &afid
, &uname
, &aname
, &n_uname
);
957 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
959 fidp
= alloc_fid(s
, fid
);
965 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
971 err
= fid_to_qid(pdu
, fidp
, &qid
);
977 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
983 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
984 qid
.type
, qid
.version
, qid
.path
);
986 * disable migration if we haven't done already.
987 * attach could get called multiple times for the same export.
989 if (!s
->migration_blocker
) {
991 error_set(&s
->migration_blocker
, QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION
,
992 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
993 migrate_add_blocker(s
->migration_blocker
);
998 complete_pdu(s
, pdu
, err
);
999 v9fs_string_free(&uname
);
1000 v9fs_string_free(&aname
);
1003 static void v9fs_stat(void *opaque
)
1011 V9fsPDU
*pdu
= opaque
;
1012 V9fsState
*s
= pdu
->s
;
1014 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1018 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1020 fidp
= get_fid(pdu
, fid
);
1025 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1029 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1033 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1035 v9fs_stat_free(&v9stat
);
1038 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1039 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1041 v9fs_stat_free(&v9stat
);
1045 complete_pdu(s
, pdu
, err
);
1048 static void v9fs_getattr(void *opaque
)
1055 uint64_t request_mask
;
1056 V9fsStatDotl v9stat_dotl
;
1057 V9fsPDU
*pdu
= opaque
;
1058 V9fsState
*s
= pdu
->s
;
1060 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1064 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1066 fidp
= get_fid(pdu
, fid
);
1072 * Currently we only support BASIC fields in stat, so there is no
1073 * need to look at request_mask.
1075 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1079 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1081 /* fill st_gen if requested and supported by underlying fs */
1082 if (request_mask
& P9_STATS_GEN
) {
1083 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1087 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1089 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1094 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1095 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1096 v9stat_dotl
.st_gid
);
1100 complete_pdu(s
, pdu
, retval
);
1103 /* Attribute flags */
1104 #define P9_ATTR_MODE (1 << 0)
1105 #define P9_ATTR_UID (1 << 1)
1106 #define P9_ATTR_GID (1 << 2)
1107 #define P9_ATTR_SIZE (1 << 3)
1108 #define P9_ATTR_ATIME (1 << 4)
1109 #define P9_ATTR_MTIME (1 << 5)
1110 #define P9_ATTR_CTIME (1 << 6)
1111 #define P9_ATTR_ATIME_SET (1 << 7)
1112 #define P9_ATTR_MTIME_SET (1 << 8)
1114 #define P9_ATTR_MASK 127
1116 static void v9fs_setattr(void *opaque
)
1123 V9fsPDU
*pdu
= opaque
;
1124 V9fsState
*s
= pdu
->s
;
1126 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1131 fidp
= get_fid(pdu
, fid
);
1136 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1137 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1142 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1143 struct timespec times
[2];
1144 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1145 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1146 times
[0].tv_sec
= v9iattr
.atime_sec
;
1147 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1149 times
[0].tv_nsec
= UTIME_NOW
;
1152 times
[0].tv_nsec
= UTIME_OMIT
;
1154 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1155 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1156 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1157 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1159 times
[1].tv_nsec
= UTIME_NOW
;
1162 times
[1].tv_nsec
= UTIME_OMIT
;
1164 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1170 * If the only valid entry in iattr is ctime we can call
1171 * chown(-1,-1) to update the ctime of the file
1173 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1174 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1175 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1176 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1179 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1182 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1188 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1189 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1198 complete_pdu(s
, pdu
, err
);
1201 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1207 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1212 for (i
= 0; i
< nwnames
; i
++) {
1213 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1222 static void v9fs_walk(void *opaque
)
1225 V9fsQID
*qids
= NULL
;
1227 V9fsPath dpath
, path
;
1231 int32_t fid
, newfid
;
1232 V9fsString
*wnames
= NULL
;
1234 V9fsFidState
*newfidp
= NULL
;
1235 V9fsPDU
*pdu
= opaque
;
1236 V9fsState
*s
= pdu
->s
;
1238 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1240 complete_pdu(s
, pdu
, err
);
1245 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1247 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1248 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1249 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1250 for (i
= 0; i
< nwnames
; i
++) {
1251 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1257 } else if (nwnames
> P9_MAXWELEM
) {
1261 fidp
= get_fid(pdu
, fid
);
1266 v9fs_path_init(&dpath
);
1267 v9fs_path_init(&path
);
1269 * Both dpath and path initially poin to fidp.
1270 * Needed to handle request with nwnames == 0
1272 v9fs_path_copy(&dpath
, &fidp
->path
);
1273 v9fs_path_copy(&path
, &fidp
->path
);
1274 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1275 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1279 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1283 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1284 v9fs_path_copy(&dpath
, &path
);
1286 if (fid
== newfid
) {
1287 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1288 v9fs_path_copy(&fidp
->path
, &path
);
1290 newfidp
= alloc_fid(s
, newfid
);
1291 if (newfidp
== NULL
) {
1295 newfidp
->uid
= fidp
->uid
;
1296 v9fs_path_copy(&newfidp
->path
, &path
);
1298 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1299 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1303 put_fid(pdu
, newfidp
);
1305 v9fs_path_free(&dpath
);
1306 v9fs_path_free(&path
);
1308 complete_pdu(s
, pdu
, err
);
1309 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1310 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1311 v9fs_string_free(&wnames
[name_idx
]);
1318 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1320 struct statfs stbuf
;
1322 V9fsState
*s
= pdu
->s
;
1325 * iounit should be multiples of f_bsize (host filesystem block size
1326 * and as well as less than (client msize - P9_IOHDRSZ))
1328 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1329 iounit
= stbuf
.f_bsize
;
1330 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1333 iounit
= s
->msize
- P9_IOHDRSZ
;
1338 static void v9fs_open(void *opaque
)
1349 V9fsPDU
*pdu
= opaque
;
1350 V9fsState
*s
= pdu
->s
;
1352 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1353 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1356 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1362 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1364 fidp
= get_fid(pdu
, fid
);
1369 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1371 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1375 stat_to_qid(&stbuf
, &qid
);
1376 if (S_ISDIR(stbuf
.st_mode
)) {
1377 err
= v9fs_co_opendir(pdu
, fidp
);
1381 fidp
->fid_type
= P9_FID_DIR
;
1382 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1388 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1389 flags
= get_dotl_openflags(s
, mode
);
1391 flags
= omode_to_uflags(mode
);
1393 if (is_ro_export(&s
->ctx
)) {
1394 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1395 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1400 err
= v9fs_co_open(pdu
, fidp
, flags
);
1404 fidp
->fid_type
= P9_FID_FILE
;
1405 fidp
->open_flags
= flags
;
1406 if (flags
& O_EXCL
) {
1408 * We let the host file system do O_EXCL check
1409 * We should not reclaim such fd
1411 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1413 iounit
= get_iounit(pdu
, &fidp
->path
);
1414 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1420 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1421 qid
.type
, qid
.version
, qid
.path
, iounit
);
1425 complete_pdu(s
, pdu
, err
);
1428 static void v9fs_lcreate(void *opaque
)
1430 int32_t dfid
, flags
, mode
;
1439 V9fsPDU
*pdu
= opaque
;
1441 v9fs_string_init(&name
);
1442 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1443 &name
, &flags
, &mode
, &gid
);
1447 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1449 fidp
= get_fid(pdu
, dfid
);
1455 flags
= get_dotl_openflags(pdu
->s
, flags
);
1456 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1457 flags
| O_CREAT
, mode
, &stbuf
);
1461 fidp
->fid_type
= P9_FID_FILE
;
1462 fidp
->open_flags
= flags
;
1463 if (flags
& O_EXCL
) {
1465 * We let the host file system do O_EXCL check
1466 * We should not reclaim such fd
1468 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1470 iounit
= get_iounit(pdu
, &fidp
->path
);
1471 stat_to_qid(&stbuf
, &qid
);
1472 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1477 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1478 qid
.type
, qid
.version
, qid
.path
, iounit
);
1482 complete_pdu(pdu
->s
, pdu
, err
);
1483 v9fs_string_free(&name
);
1486 static void v9fs_fsync(void *opaque
)
1493 V9fsPDU
*pdu
= opaque
;
1494 V9fsState
*s
= pdu
->s
;
1496 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1500 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1502 fidp
= get_fid(pdu
, fid
);
1507 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1513 complete_pdu(s
, pdu
, err
);
1516 static void v9fs_clunk(void *opaque
)
1522 V9fsPDU
*pdu
= opaque
;
1523 V9fsState
*s
= pdu
->s
;
1525 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1529 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1531 fidp
= clunk_fid(s
, fid
);
1537 * Bump the ref so that put_fid will
1541 err
= put_fid(pdu
, fidp
);
1546 complete_pdu(s
, pdu
, err
);
1549 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1550 uint64_t off
, uint32_t max_count
)
1557 xattr_len
= fidp
->fs
.xattr
.len
;
1558 read_count
= xattr_len
- off
;
1559 if (read_count
> max_count
) {
1560 read_count
= max_count
;
1561 } else if (read_count
< 0) {
1563 * read beyond XATTR value
1567 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1572 err
= v9fs_pack(pdu
->elem
.in_sg
, pdu
->elem
.in_num
, offset
,
1573 ((char *)fidp
->fs
.xattr
.value
) + off
,
1582 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1583 V9fsFidState
*fidp
, uint32_t max_count
)
1590 off_t saved_dir_pos
;
1591 struct dirent
*dent
, *result
;
1593 /* save the directory position */
1594 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1595 if (saved_dir_pos
< 0) {
1596 return saved_dir_pos
;
1599 dent
= g_malloc(sizeof(struct dirent
));
1602 v9fs_path_init(&path
);
1603 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1604 if (err
|| !result
) {
1607 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1611 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1615 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1619 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1620 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1621 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1622 /* Ran out of buffer. Set dir back to old position and return */
1623 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1624 v9fs_stat_free(&v9stat
);
1625 v9fs_path_free(&path
);
1630 v9fs_stat_free(&v9stat
);
1631 v9fs_path_free(&path
);
1632 saved_dir_pos
= dent
->d_off
;
1636 v9fs_path_free(&path
);
1644 * Create a QEMUIOVector for a sub-region of PDU iovecs
1646 * @qiov: uninitialized QEMUIOVector
1647 * @skip: number of bytes to skip from beginning of PDU
1648 * @size: number of bytes to include
1649 * @is_write: true - write, false - read
1651 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1652 * with qemu_iovec_destroy().
1654 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1655 size_t skip
, size_t size
,
1663 iov
= pdu
->elem
.out_sg
;
1664 niov
= pdu
->elem
.out_num
;
1666 iov
= pdu
->elem
.in_sg
;
1667 niov
= pdu
->elem
.in_num
;
1670 qemu_iovec_init_external(&elem
, iov
, niov
);
1671 qemu_iovec_init(qiov
, niov
);
1672 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1675 static void v9fs_read(void *opaque
)
1684 V9fsPDU
*pdu
= opaque
;
1685 V9fsState
*s
= pdu
->s
;
1687 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1691 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1693 fidp
= get_fid(pdu
, fid
);
1698 if (fidp
->fid_type
== P9_FID_DIR
) {
1701 v9fs_co_rewinddir(pdu
, fidp
);
1703 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1708 err
= pdu_marshal(pdu
, offset
, "d", count
);
1712 err
+= offset
+ count
;
1713 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1714 QEMUIOVector qiov_full
;
1718 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1719 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1721 qemu_iovec_reset(&qiov
);
1722 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1724 print_sg(qiov
.iov
, qiov
.niov
);
1726 /* Loop in case of EINTR */
1728 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1733 } while (len
== -EINTR
&& !pdu
->cancelled
);
1735 /* IO error return the error */
1739 } while (count
< max_count
&& len
> 0);
1740 err
= pdu_marshal(pdu
, offset
, "d", count
);
1744 err
+= offset
+ count
;
1745 qemu_iovec_destroy(&qiov
);
1746 qemu_iovec_destroy(&qiov_full
);
1747 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1748 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1752 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1756 complete_pdu(s
, pdu
, err
);
1759 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1762 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1763 * size of type (1) + size of name.size (2) + strlen(name.data)
1765 return 24 + v9fs_string_size(name
);
1768 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1769 V9fsFidState
*fidp
, int32_t max_count
)
1776 off_t saved_dir_pos
;
1777 struct dirent
*dent
, *result
;
1779 /* save the directory position */
1780 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1781 if (saved_dir_pos
< 0) {
1782 return saved_dir_pos
;
1785 dent
= g_malloc(sizeof(struct dirent
));
1788 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1789 if (err
|| !result
) {
1792 v9fs_string_init(&name
);
1793 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1794 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1795 /* Ran out of buffer. Set dir back to old position and return */
1796 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1797 v9fs_string_free(&name
);
1802 * Fill up just the path field of qid because the client uses
1803 * only that. To fill the entire qid structure we will have
1804 * to stat each dirent found, which is expensive
1806 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1807 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1808 /* Fill the other fields with dummy values */
1812 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1813 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1815 dent
->d_type
, &name
);
1817 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1818 v9fs_string_free(&name
);
1823 v9fs_string_free(&name
);
1824 saved_dir_pos
= dent
->d_off
;
1833 static void v9fs_readdir(void *opaque
)
1839 uint64_t initial_offset
;
1842 V9fsPDU
*pdu
= opaque
;
1843 V9fsState
*s
= pdu
->s
;
1845 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1846 &initial_offset
, &max_count
);
1850 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1852 fidp
= get_fid(pdu
, fid
);
1857 if (!fidp
->fs
.dir
) {
1861 if (initial_offset
== 0) {
1862 v9fs_co_rewinddir(pdu
, fidp
);
1864 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1866 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1871 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1875 retval
+= count
+ offset
;
1876 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1880 complete_pdu(s
, pdu
, retval
);
1883 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1884 uint64_t off
, uint32_t count
,
1885 struct iovec
*sg
, int cnt
)
1894 xattr_len
= fidp
->fs
.xattr
.len
;
1895 write_count
= xattr_len
- off
;
1896 if (write_count
> count
) {
1897 write_count
= count
;
1898 } else if (write_count
< 0) {
1900 * write beyond XATTR value len specified in
1906 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1911 fidp
->fs
.xattr
.copied_len
+= write_count
;
1913 * Now copy the content from sg list
1915 for (i
= 0; i
< cnt
; i
++) {
1916 if (write_count
> sg
[i
].iov_len
) {
1917 to_copy
= sg
[i
].iov_len
;
1919 to_copy
= write_count
;
1921 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1922 /* updating vs->off since we are not using below */
1924 write_count
-= to_copy
;
1930 static void v9fs_write(void *opaque
)
1940 V9fsPDU
*pdu
= opaque
;
1941 V9fsState
*s
= pdu
->s
;
1942 QEMUIOVector qiov_full
;
1945 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
1947 return complete_pdu(s
, pdu
, err
);
1950 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
1951 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
1953 fidp
= get_fid(pdu
, fid
);
1958 if (fidp
->fid_type
== P9_FID_FILE
) {
1959 if (fidp
->fs
.fd
== -1) {
1963 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1965 * setxattr operation
1967 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
1968 qiov_full
.iov
, qiov_full
.niov
);
1974 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1976 qemu_iovec_reset(&qiov
);
1977 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
1979 print_sg(qiov
.iov
, qiov
.niov
);
1981 /* Loop in case of EINTR */
1983 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1988 } while (len
== -EINTR
&& !pdu
->cancelled
);
1990 /* IO error return the error */
1994 } while (total
< count
&& len
> 0);
1997 err
= pdu_marshal(pdu
, offset
, "d", total
);
2002 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2004 qemu_iovec_destroy(&qiov
);
2008 qemu_iovec_destroy(&qiov_full
);
2009 complete_pdu(s
, pdu
, err
);
2012 static void v9fs_create(void *opaque
)
2024 V9fsString extension
;
2026 V9fsPDU
*pdu
= opaque
;
2028 v9fs_path_init(&path
);
2029 v9fs_string_init(&name
);
2030 v9fs_string_init(&extension
);
2031 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2032 &perm
, &mode
, &extension
);
2036 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2038 fidp
= get_fid(pdu
, fid
);
2043 if (perm
& P9_STAT_MODE_DIR
) {
2044 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2045 fidp
->uid
, -1, &stbuf
);
2049 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2053 v9fs_path_copy(&fidp
->path
, &path
);
2054 err
= v9fs_co_opendir(pdu
, fidp
);
2058 fidp
->fid_type
= P9_FID_DIR
;
2059 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2060 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2061 extension
.data
, -1 , &stbuf
);
2065 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2069 v9fs_path_copy(&fidp
->path
, &path
);
2070 } else if (perm
& P9_STAT_MODE_LINK
) {
2071 int32_t ofid
= atoi(extension
.data
);
2072 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2073 if (ofidp
== NULL
) {
2077 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2078 put_fid(pdu
, ofidp
);
2082 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2084 fidp
->fid_type
= P9_FID_NONE
;
2087 v9fs_path_copy(&fidp
->path
, &path
);
2088 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2090 fidp
->fid_type
= P9_FID_NONE
;
2093 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2095 uint32_t major
, minor
;
2098 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2115 nmode
|= perm
& 0777;
2116 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2117 makedev(major
, minor
), nmode
, &stbuf
);
2121 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2125 v9fs_path_copy(&fidp
->path
, &path
);
2126 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2127 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2128 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2132 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2136 v9fs_path_copy(&fidp
->path
, &path
);
2137 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2138 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2139 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2143 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2147 v9fs_path_copy(&fidp
->path
, &path
);
2149 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2150 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2154 fidp
->fid_type
= P9_FID_FILE
;
2155 fidp
->open_flags
= omode_to_uflags(mode
);
2156 if (fidp
->open_flags
& O_EXCL
) {
2158 * We let the host file system do O_EXCL check
2159 * We should not reclaim such fd
2161 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2164 iounit
= get_iounit(pdu
, &fidp
->path
);
2165 stat_to_qid(&stbuf
, &qid
);
2166 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2171 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2172 qid
.type
, qid
.version
, qid
.path
, iounit
);
2176 complete_pdu(pdu
->s
, pdu
, err
);
2177 v9fs_string_free(&name
);
2178 v9fs_string_free(&extension
);
2179 v9fs_path_free(&path
);
2182 static void v9fs_symlink(void *opaque
)
2184 V9fsPDU
*pdu
= opaque
;
2187 V9fsFidState
*dfidp
;
2195 v9fs_string_init(&name
);
2196 v9fs_string_init(&symname
);
2197 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2201 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2203 dfidp
= get_fid(pdu
, dfid
);
2204 if (dfidp
== NULL
) {
2208 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2212 stat_to_qid(&stbuf
, &qid
);
2213 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2218 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2219 qid
.type
, qid
.version
, qid
.path
);
2221 put_fid(pdu
, dfidp
);
2223 complete_pdu(pdu
->s
, pdu
, err
);
2224 v9fs_string_free(&name
);
2225 v9fs_string_free(&symname
);
2228 static void v9fs_flush(void *opaque
)
2233 V9fsPDU
*cancel_pdu
;
2234 V9fsPDU
*pdu
= opaque
;
2235 V9fsState
*s
= pdu
->s
;
2237 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2239 complete_pdu(s
, pdu
, err
);
2242 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2244 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2245 if (cancel_pdu
->tag
== tag
) {
2250 cancel_pdu
->cancelled
= 1;
2252 * Wait for pdu to complete.
2254 qemu_co_queue_wait(&cancel_pdu
->complete
);
2255 cancel_pdu
->cancelled
= 0;
2256 free_pdu(pdu
->s
, cancel_pdu
);
2258 complete_pdu(s
, pdu
, 7);
2261 static void v9fs_link(void *opaque
)
2263 V9fsPDU
*pdu
= opaque
;
2264 V9fsState
*s
= pdu
->s
;
2265 int32_t dfid
, oldfid
;
2266 V9fsFidState
*dfidp
, *oldfidp
;
2271 v9fs_string_init(&name
);
2272 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2276 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2278 dfidp
= get_fid(pdu
, dfid
);
2279 if (dfidp
== NULL
) {
2284 oldfidp
= get_fid(pdu
, oldfid
);
2285 if (oldfidp
== NULL
) {
2289 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2294 put_fid(pdu
, dfidp
);
2296 v9fs_string_free(&name
);
2297 complete_pdu(s
, pdu
, err
);
2300 /* Only works with path name based fid */
2301 static void v9fs_remove(void *opaque
)
2307 V9fsPDU
*pdu
= opaque
;
2309 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2313 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2315 fidp
= get_fid(pdu
, fid
);
2320 /* if fs driver is not path based, return EOPNOTSUPP */
2321 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2326 * IF the file is unlinked, we cannot reopen
2327 * the file later. So don't reclaim fd
2329 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2333 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2338 /* For TREMOVE we need to clunk the fid even on failed remove */
2339 clunk_fid(pdu
->s
, fidp
->fid
);
2342 complete_pdu(pdu
->s
, pdu
, err
);
2345 static void v9fs_unlinkat(void *opaque
)
2349 int32_t dfid
, flags
;
2352 V9fsFidState
*dfidp
;
2353 V9fsPDU
*pdu
= opaque
;
2355 v9fs_string_init(&name
);
2356 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2360 dfidp
= get_fid(pdu
, dfid
);
2361 if (dfidp
== NULL
) {
2366 * IF the file is unlinked, we cannot reopen
2367 * the file later. So don't reclaim fd
2369 v9fs_path_init(&path
);
2370 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2374 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2378 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2383 put_fid(pdu
, dfidp
);
2384 v9fs_path_free(&path
);
2386 complete_pdu(pdu
->s
, pdu
, err
);
2387 v9fs_string_free(&name
);
2391 /* Only works with path name based fid */
2392 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2393 int32_t newdirfid
, V9fsString
*name
)
2398 V9fsFidState
*tfidp
;
2399 V9fsState
*s
= pdu
->s
;
2400 V9fsFidState
*dirfidp
= NULL
;
2401 char *old_name
, *new_name
;
2403 v9fs_path_init(&new_path
);
2404 if (newdirfid
!= -1) {
2405 dirfidp
= get_fid(pdu
, newdirfid
);
2406 if (dirfidp
== NULL
) {
2410 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2411 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2413 old_name
= fidp
->path
.data
;
2414 end
= strrchr(old_name
, '/');
2420 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2421 strncat(new_name
, old_name
, end
- old_name
);
2422 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2423 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2426 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2431 * Fixup fid's pointing to the old name to
2432 * start pointing to the new name
2434 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2435 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2436 /* replace the name */
2437 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2442 put_fid(pdu
, dirfidp
);
2444 v9fs_path_free(&new_path
);
2449 /* Only works with path name based fid */
2450 static void v9fs_rename(void *opaque
)
2458 V9fsPDU
*pdu
= opaque
;
2459 V9fsState
*s
= pdu
->s
;
2461 v9fs_string_init(&name
);
2462 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2466 fidp
= get_fid(pdu
, fid
);
2471 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2472 /* if fs driver is not path based, return EOPNOTSUPP */
2473 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2477 v9fs_path_write_lock(s
);
2478 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2479 v9fs_path_unlock(s
);
2486 complete_pdu(s
, pdu
, err
);
2487 v9fs_string_free(&name
);
2490 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2491 V9fsString
*old_name
, V9fsPath
*newdir
,
2492 V9fsString
*new_name
)
2494 V9fsFidState
*tfidp
;
2495 V9fsPath oldpath
, newpath
;
2496 V9fsState
*s
= pdu
->s
;
2499 v9fs_path_init(&oldpath
);
2500 v9fs_path_init(&newpath
);
2501 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2502 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2505 * Fixup fid's pointing to the old name to
2506 * start pointing to the new name
2508 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2509 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2510 /* replace the name */
2511 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2514 v9fs_path_free(&oldpath
);
2515 v9fs_path_free(&newpath
);
2518 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2519 V9fsString
*old_name
, int32_t newdirfid
,
2520 V9fsString
*new_name
)
2523 V9fsState
*s
= pdu
->s
;
2524 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2526 olddirfidp
= get_fid(pdu
, olddirfid
);
2527 if (olddirfidp
== NULL
) {
2531 if (newdirfid
!= -1) {
2532 newdirfidp
= get_fid(pdu
, newdirfid
);
2533 if (newdirfidp
== NULL
) {
2538 newdirfidp
= get_fid(pdu
, olddirfid
);
2541 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2542 &newdirfidp
->path
, new_name
);
2546 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2547 /* Only for path based fid we need to do the below fixup */
2548 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2549 &newdirfidp
->path
, new_name
);
2553 put_fid(pdu
, olddirfidp
);
2556 put_fid(pdu
, newdirfidp
);
2561 static void v9fs_renameat(void *opaque
)
2565 V9fsPDU
*pdu
= opaque
;
2566 V9fsState
*s
= pdu
->s
;
2567 int32_t olddirfid
, newdirfid
;
2568 V9fsString old_name
, new_name
;
2570 v9fs_string_init(&old_name
);
2571 v9fs_string_init(&new_name
);
2572 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2573 &old_name
, &newdirfid
, &new_name
);
2578 v9fs_path_write_lock(s
);
2579 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2580 &old_name
, newdirfid
, &new_name
);
2581 v9fs_path_unlock(s
);
2587 complete_pdu(s
, pdu
, err
);
2588 v9fs_string_free(&old_name
);
2589 v9fs_string_free(&new_name
);
2592 static void v9fs_wstat(void *opaque
)
2601 V9fsPDU
*pdu
= opaque
;
2602 V9fsState
*s
= pdu
->s
;
2604 v9fs_stat_init(&v9stat
);
2605 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2609 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2610 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2612 fidp
= get_fid(pdu
, fid
);
2617 /* do we need to sync the file? */
2618 if (donttouch_stat(&v9stat
)) {
2619 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2622 if (v9stat
.mode
!= -1) {
2624 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2628 v9_mode
= stat_to_v9mode(&stbuf
);
2629 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2630 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2631 /* Attempting to change the type */
2635 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2636 v9mode_to_mode(v9stat
.mode
,
2637 &v9stat
.extension
));
2642 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2643 struct timespec times
[2];
2644 if (v9stat
.atime
!= -1) {
2645 times
[0].tv_sec
= v9stat
.atime
;
2646 times
[0].tv_nsec
= 0;
2648 times
[0].tv_nsec
= UTIME_OMIT
;
2650 if (v9stat
.mtime
!= -1) {
2651 times
[1].tv_sec
= v9stat
.mtime
;
2652 times
[1].tv_nsec
= 0;
2654 times
[1].tv_nsec
= UTIME_OMIT
;
2656 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2661 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2662 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2667 if (v9stat
.name
.size
!= 0) {
2668 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2673 if (v9stat
.length
!= -1) {
2674 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2683 v9fs_stat_free(&v9stat
);
2684 complete_pdu(s
, pdu
, err
);
2687 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2699 int32_t bsize_factor
;
2702 * compute bsize factor based on host file system block size
2705 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2706 if (!bsize_factor
) {
2709 f_type
= stbuf
->f_type
;
2710 f_bsize
= stbuf
->f_bsize
;
2711 f_bsize
*= bsize_factor
;
2713 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2714 * adjust(divide) the number of blocks, free blocks and available
2715 * blocks by bsize factor
2717 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2718 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2719 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2720 f_files
= stbuf
->f_files
;
2721 f_ffree
= stbuf
->f_ffree
;
2722 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2723 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2724 f_namelen
= stbuf
->f_namelen
;
2726 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2727 f_type
, f_bsize
, f_blocks
, f_bfree
,
2728 f_bavail
, f_files
, f_ffree
,
2729 fsid_val
, f_namelen
);
2732 static void v9fs_statfs(void *opaque
)
2738 struct statfs stbuf
;
2739 V9fsPDU
*pdu
= opaque
;
2740 V9fsState
*s
= pdu
->s
;
2742 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2746 fidp
= get_fid(pdu
, fid
);
2751 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2755 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2763 complete_pdu(s
, pdu
, retval
);
2766 static void v9fs_mknod(void *opaque
)
2779 V9fsPDU
*pdu
= opaque
;
2780 V9fsState
*s
= pdu
->s
;
2782 v9fs_string_init(&name
);
2783 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2784 &major
, &minor
, &gid
);
2788 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2790 fidp
= get_fid(pdu
, fid
);
2795 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2796 makedev(major
, minor
), mode
, &stbuf
);
2800 stat_to_qid(&stbuf
, &qid
);
2801 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2806 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2807 qid
.type
, qid
.version
, qid
.path
);
2811 complete_pdu(s
, pdu
, err
);
2812 v9fs_string_free(&name
);
2816 * Implement posix byte range locking code
2817 * Server side handling of locking code is very simple, because 9p server in
2818 * QEMU can handle only one client. And most of the lock handling
2819 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2820 * do any thing in * qemu 9p server side lock code path.
2821 * So when a TLOCK request comes, always return success
2823 static void v9fs_lock(void *opaque
)
2830 int32_t fid
, err
= 0;
2831 V9fsPDU
*pdu
= opaque
;
2832 V9fsState
*s
= pdu
->s
;
2834 status
= P9_LOCK_ERROR
;
2835 v9fs_string_init(&flock
.client_id
);
2836 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2837 &flock
.flags
, &flock
.start
, &flock
.length
,
2838 &flock
.proc_id
, &flock
.client_id
);
2842 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2843 flock
.type
, flock
.start
, flock
.length
);
2846 /* We support only block flag now (that too ignored currently) */
2847 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2851 fidp
= get_fid(pdu
, fid
);
2856 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2860 status
= P9_LOCK_SUCCESS
;
2864 err
= pdu_marshal(pdu
, offset
, "b", status
);
2868 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2869 complete_pdu(s
, pdu
, err
);
2870 v9fs_string_free(&flock
.client_id
);
2874 * When a TGETLOCK request comes, always return success because all lock
2875 * handling is done by client's VFS layer.
2877 static void v9fs_getlock(void *opaque
)
2883 int32_t fid
, err
= 0;
2884 V9fsPDU
*pdu
= opaque
;
2885 V9fsState
*s
= pdu
->s
;
2887 v9fs_string_init(&glock
.client_id
);
2888 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
2889 &glock
.start
, &glock
.length
, &glock
.proc_id
,
2894 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2895 glock
.type
, glock
.start
, glock
.length
);
2897 fidp
= get_fid(pdu
, fid
);
2902 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2906 glock
.type
= P9_LOCK_TYPE_UNLCK
;
2907 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
2908 glock
.start
, glock
.length
, glock
.proc_id
,
2914 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
2915 glock
.length
, glock
.proc_id
);
2919 complete_pdu(s
, pdu
, err
);
2920 v9fs_string_free(&glock
.client_id
);
2923 static void v9fs_mkdir(void *opaque
)
2925 V9fsPDU
*pdu
= opaque
;
2936 v9fs_string_init(&name
);
2937 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2941 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
2943 fidp
= get_fid(pdu
, fid
);
2948 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2952 stat_to_qid(&stbuf
, &qid
);
2953 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2958 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
2959 qid
.type
, qid
.version
, qid
.path
, err
);
2963 complete_pdu(pdu
->s
, pdu
, err
);
2964 v9fs_string_free(&name
);
2967 static void v9fs_xattrwalk(void *opaque
)
2973 int32_t fid
, newfid
;
2974 V9fsFidState
*file_fidp
;
2975 V9fsFidState
*xattr_fidp
= NULL
;
2976 V9fsPDU
*pdu
= opaque
;
2977 V9fsState
*s
= pdu
->s
;
2979 v9fs_string_init(&name
);
2980 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
2984 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
2986 file_fidp
= get_fid(pdu
, fid
);
2987 if (file_fidp
== NULL
) {
2991 xattr_fidp
= alloc_fid(s
, newfid
);
2992 if (xattr_fidp
== NULL
) {
2996 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
2997 if (name
.data
== NULL
) {
2999 * listxattr request. Get the size first
3001 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3004 clunk_fid(s
, xattr_fidp
->fid
);
3008 * Read the xattr value
3010 xattr_fidp
->fs
.xattr
.len
= size
;
3011 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3012 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3014 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3015 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3016 xattr_fidp
->fs
.xattr
.value
,
3017 xattr_fidp
->fs
.xattr
.len
);
3019 clunk_fid(s
, xattr_fidp
->fid
);
3023 err
= pdu_marshal(pdu
, offset
, "q", size
);
3030 * specific xattr fid. We check for xattr
3031 * presence also collect the xattr size
3033 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3037 clunk_fid(s
, xattr_fidp
->fid
);
3041 * Read the xattr value
3043 xattr_fidp
->fs
.xattr
.len
= size
;
3044 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3045 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3047 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3048 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3049 &name
, xattr_fidp
->fs
.xattr
.value
,
3050 xattr_fidp
->fs
.xattr
.len
);
3052 clunk_fid(s
, xattr_fidp
->fid
);
3056 err
= pdu_marshal(pdu
, offset
, "q", size
);
3062 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3064 put_fid(pdu
, file_fidp
);
3066 put_fid(pdu
, xattr_fidp
);
3069 complete_pdu(s
, pdu
, err
);
3070 v9fs_string_free(&name
);
3073 static void v9fs_xattrcreate(void *opaque
)
3081 V9fsFidState
*file_fidp
;
3082 V9fsFidState
*xattr_fidp
;
3083 V9fsPDU
*pdu
= opaque
;
3084 V9fsState
*s
= pdu
->s
;
3086 v9fs_string_init(&name
);
3087 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3091 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3093 file_fidp
= get_fid(pdu
, fid
);
3094 if (file_fidp
== NULL
) {
3098 /* Make the file fid point to xattr */
3099 xattr_fidp
= file_fidp
;
3100 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3101 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3102 xattr_fidp
->fs
.xattr
.len
= size
;
3103 xattr_fidp
->fs
.xattr
.flags
= flags
;
3104 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3105 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3106 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3108 put_fid(pdu
, file_fidp
);
3110 complete_pdu(s
, pdu
, err
);
3111 v9fs_string_free(&name
);
3114 static void v9fs_readlink(void *opaque
)
3116 V9fsPDU
*pdu
= opaque
;
3123 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3127 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3128 fidp
= get_fid(pdu
, fid
);
3134 v9fs_string_init(&target
);
3135 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3139 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3141 v9fs_string_free(&target
);
3145 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3146 v9fs_string_free(&target
);
3150 complete_pdu(pdu
->s
, pdu
, err
);
3153 static CoroutineEntry
*pdu_co_handlers
[] = {
3154 [P9_TREADDIR
] = v9fs_readdir
,
3155 [P9_TSTATFS
] = v9fs_statfs
,
3156 [P9_TGETATTR
] = v9fs_getattr
,
3157 [P9_TSETATTR
] = v9fs_setattr
,
3158 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3159 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3160 [P9_TMKNOD
] = v9fs_mknod
,
3161 [P9_TRENAME
] = v9fs_rename
,
3162 [P9_TLOCK
] = v9fs_lock
,
3163 [P9_TGETLOCK
] = v9fs_getlock
,
3164 [P9_TRENAMEAT
] = v9fs_renameat
,
3165 [P9_TREADLINK
] = v9fs_readlink
,
3166 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3167 [P9_TMKDIR
] = v9fs_mkdir
,
3168 [P9_TVERSION
] = v9fs_version
,
3169 [P9_TLOPEN
] = v9fs_open
,
3170 [P9_TATTACH
] = v9fs_attach
,
3171 [P9_TSTAT
] = v9fs_stat
,
3172 [P9_TWALK
] = v9fs_walk
,
3173 [P9_TCLUNK
] = v9fs_clunk
,
3174 [P9_TFSYNC
] = v9fs_fsync
,
3175 [P9_TOPEN
] = v9fs_open
,
3176 [P9_TREAD
] = v9fs_read
,
3178 [P9_TAUTH
] = v9fs_auth
,
3180 [P9_TFLUSH
] = v9fs_flush
,
3181 [P9_TLINK
] = v9fs_link
,
3182 [P9_TSYMLINK
] = v9fs_symlink
,
3183 [P9_TCREATE
] = v9fs_create
,
3184 [P9_TLCREATE
] = v9fs_lcreate
,
3185 [P9_TWRITE
] = v9fs_write
,
3186 [P9_TWSTAT
] = v9fs_wstat
,
3187 [P9_TREMOVE
] = v9fs_remove
,
3190 static void v9fs_op_not_supp(void *opaque
)
3192 V9fsPDU
*pdu
= opaque
;
3193 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
3196 static void v9fs_fs_ro(void *opaque
)
3198 V9fsPDU
*pdu
= opaque
;
3199 complete_pdu(pdu
->s
, pdu
, -EROFS
);
3202 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3229 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3232 CoroutineEntry
*handler
;
3234 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3235 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3236 handler
= v9fs_op_not_supp
;
3238 handler
= pdu_co_handlers
[pdu
->id
];
3241 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3242 handler
= v9fs_fs_ro
;
3244 co
= qemu_coroutine_create(handler
);
3245 qemu_coroutine_enter(co
, pdu
);
3248 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3250 V9fsState
*s
= (V9fsState
*)vdev
;
3254 while ((pdu
= alloc_pdu(s
)) &&
3255 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3258 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3259 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3261 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3263 pdu
->size
= le32_to_cpu(*(uint32_t *)ptr
);
3265 pdu
->tag
= le16_to_cpu(*(uint16_t *)(ptr
+ 5));
3266 qemu_co_queue_init(&pdu
->complete
);
3272 void virtio_9p_set_fd_limit(void)
3275 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3276 fprintf(stderr
, "Failed to get the resource limit\n");
3279 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3280 open_fd_rc
= rlim
.rlim_cur
/2;