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/virtio.h"
15 #include "hw/i386/pc.h"
16 #include "qemu/error-report.h"
18 #include "qemu/sockets.h"
19 #include "virtio-9p.h"
20 #include "fsdev/qemu-fsdev.h"
21 #include "virtio-9p-xattr.h"
22 #include "virtio-9p-coth.h"
24 #include "migration/migration.h"
28 static int open_fd_rc
;
42 static int omode_to_uflags(int8_t mode
)
76 struct dotl_openflag_map
{
81 static int dotl_to_open_flags(int flags
)
85 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
86 * and P9_DOTL_NOACCESS
88 int oflags
= flags
& O_ACCMODE
;
90 struct dotl_openflag_map dotl_oflag_map
[] = {
91 { P9_DOTL_CREATE
, O_CREAT
},
92 { P9_DOTL_EXCL
, O_EXCL
},
93 { P9_DOTL_NOCTTY
, O_NOCTTY
},
94 { P9_DOTL_TRUNC
, O_TRUNC
},
95 { P9_DOTL_APPEND
, O_APPEND
},
96 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
97 { P9_DOTL_DSYNC
, O_DSYNC
},
98 { P9_DOTL_FASYNC
, FASYNC
},
99 { P9_DOTL_DIRECT
, O_DIRECT
},
100 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
101 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
102 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
103 { P9_DOTL_NOATIME
, O_NOATIME
},
104 { P9_DOTL_SYNC
, O_SYNC
},
107 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
108 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
109 oflags
|= dotl_oflag_map
[i
].open_flag
;
116 void cred_init(FsCred
*credp
)
124 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
128 * Filter the client open flags
130 flags
= dotl_to_open_flags(oflags
);
131 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
133 * Ignore direct disk access hint until the server supports it.
139 void v9fs_path_init(V9fsPath
*path
)
145 void v9fs_path_free(V9fsPath
*path
)
152 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
155 lhs
->data
= g_malloc(rhs
->size
);
156 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
157 lhs
->size
= rhs
->size
;
160 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
161 const char *name
, V9fsPath
*path
)
164 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
172 * Return TRUE if s1 is an ancestor of s2.
174 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
175 * As a special case, We treat s1 as ancestor of s2 if they are same!
177 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
179 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
180 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
187 static size_t v9fs_string_size(V9fsString
*str
)
193 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
194 static int v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
197 if (f
->fid_type
== P9_FID_FILE
) {
198 if (f
->fs
.fd
== -1) {
200 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
201 } while (err
== -EINTR
&& !pdu
->cancelled
);
203 } else if (f
->fid_type
== P9_FID_DIR
) {
204 if (f
->fs
.dir
== NULL
) {
206 err
= v9fs_co_opendir(pdu
, f
);
207 } while (err
== -EINTR
&& !pdu
->cancelled
);
213 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
217 V9fsState
*s
= pdu
->s
;
219 for (f
= s
->fid_list
; f
; f
= f
->next
) {
223 * Update the fid ref upfront so that
224 * we don't get reclaimed when we yield
229 * check whether we need to reopen the
230 * file. We might have closed the fd
231 * while trying to free up some file
234 err
= v9fs_reopen_fid(pdu
, f
);
240 * Mark the fid as referenced so that the LRU
241 * reclaim won't close the file descriptor
243 f
->flags
|= FID_REFERENCED
;
250 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
254 for (f
= s
->fid_list
; f
; f
= f
->next
) {
255 /* If fid is already there return NULL */
261 f
= g_malloc0(sizeof(V9fsFidState
));
263 f
->fid_type
= P9_FID_NONE
;
266 * Mark the fid as referenced so that the LRU
267 * reclaim won't close the file descriptor
269 f
->flags
|= FID_REFERENCED
;
270 f
->next
= s
->fid_list
;
276 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
280 if (fidp
->fs
.xattr
.copied_len
== -1) {
281 /* getxattr/listxattr fid */
285 * if this is fid for setxattr. clunk should
286 * result in setxattr localcall
288 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
289 /* clunk after partial write */
293 if (fidp
->fs
.xattr
.len
) {
294 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
295 fidp
->fs
.xattr
.value
,
297 fidp
->fs
.xattr
.flags
);
299 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
302 v9fs_string_free(&fidp
->fs
.xattr
.name
);
304 g_free(fidp
->fs
.xattr
.value
);
308 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
312 if (fidp
->fid_type
== P9_FID_FILE
) {
313 /* If we reclaimed the fd no need to close */
314 if (fidp
->fs
.fd
!= -1) {
315 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
317 } else if (fidp
->fid_type
== P9_FID_DIR
) {
318 if (fidp
->fs
.dir
!= NULL
) {
319 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
321 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
322 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
324 v9fs_path_free(&fidp
->path
);
329 static int put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
334 * Don't free the fid if it is in reclaim list
336 if (!fidp
->ref
&& fidp
->clunked
) {
337 if (fidp
->fid
== pdu
->s
->root_fid
) {
339 * if the clunked fid is root fid then we
340 * have unmounted the fs on the client side.
341 * delete the migration blocker. Ideally, this
342 * should be hooked to transport close notification
344 if (pdu
->s
->migration_blocker
) {
345 migrate_del_blocker(pdu
->s
->migration_blocker
);
346 error_free(pdu
->s
->migration_blocker
);
347 pdu
->s
->migration_blocker
= NULL
;
350 return free_fid(pdu
, fidp
);
355 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
357 V9fsFidState
**fidpp
, *fidp
;
359 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
360 if ((*fidpp
)->fid
== fid
) {
364 if (*fidpp
== NULL
) {
373 void v9fs_reclaim_fd(V9fsPDU
*pdu
)
375 int reclaim_count
= 0;
376 V9fsState
*s
= pdu
->s
;
377 V9fsFidState
*f
, *reclaim_list
= NULL
;
379 for (f
= s
->fid_list
; f
; f
= f
->next
) {
381 * Unlink fids cannot be reclaimed. Check
382 * for them and skip them. Also skip fids
383 * currently being operated on.
385 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
389 * if it is a recently referenced fid
390 * we leave the fid untouched and clear the
391 * reference bit. We come back to it later
392 * in the next iteration. (a simple LRU without
393 * moving list elements around)
395 if (f
->flags
& FID_REFERENCED
) {
396 f
->flags
&= ~FID_REFERENCED
;
400 * Add fids to reclaim list.
402 if (f
->fid_type
== P9_FID_FILE
) {
403 if (f
->fs
.fd
!= -1) {
405 * Up the reference count so that
406 * a clunk request won't free this fid
409 f
->rclm_lst
= reclaim_list
;
411 f
->fs_reclaim
.fd
= f
->fs
.fd
;
415 } else if (f
->fid_type
== P9_FID_DIR
) {
416 if (f
->fs
.dir
!= NULL
) {
418 * Up the reference count so that
419 * a clunk request won't free this fid
422 f
->rclm_lst
= reclaim_list
;
424 f
->fs_reclaim
.dir
= f
->fs
.dir
;
429 if (reclaim_count
>= open_fd_rc
) {
434 * Now close the fid in reclaim list. Free them if they
435 * are already clunked.
437 while (reclaim_list
) {
439 reclaim_list
= f
->rclm_lst
;
440 if (f
->fid_type
== P9_FID_FILE
) {
441 v9fs_co_close(pdu
, &f
->fs_reclaim
);
442 } else if (f
->fid_type
== P9_FID_DIR
) {
443 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
447 * Now drop the fid reference, free it
454 static int v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
457 V9fsState
*s
= pdu
->s
;
458 V9fsFidState
*fidp
, head_fid
;
460 head_fid
.next
= s
->fid_list
;
461 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
462 if (fidp
->path
.size
!= path
->size
) {
465 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
466 /* Mark the fid non reclaimable. */
467 fidp
->flags
|= FID_NON_RECLAIMABLE
;
469 /* reopen the file/dir if already closed */
470 err
= v9fs_reopen_fid(pdu
, fidp
);
475 * Go back to head of fid list because
476 * the list could have got updated when
477 * switched to the worker thread
487 static void virtfs_reset(V9fsPDU
*pdu
)
489 V9fsState
*s
= pdu
->s
;
490 V9fsFidState
*fidp
= NULL
;
493 while (s
->fid_list
) {
495 s
->fid_list
= fidp
->next
;
504 /* One or more unclunked fids found... */
505 error_report("9pfs:%s: One or more uncluncked fids "
506 "found during reset", __func__
);
510 #define P9_QID_TYPE_DIR 0x80
511 #define P9_QID_TYPE_SYMLINK 0x02
513 #define P9_STAT_MODE_DIR 0x80000000
514 #define P9_STAT_MODE_APPEND 0x40000000
515 #define P9_STAT_MODE_EXCL 0x20000000
516 #define P9_STAT_MODE_MOUNT 0x10000000
517 #define P9_STAT_MODE_AUTH 0x08000000
518 #define P9_STAT_MODE_TMP 0x04000000
519 #define P9_STAT_MODE_SYMLINK 0x02000000
520 #define P9_STAT_MODE_LINK 0x01000000
521 #define P9_STAT_MODE_DEVICE 0x00800000
522 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
523 #define P9_STAT_MODE_SOCKET 0x00100000
524 #define P9_STAT_MODE_SETUID 0x00080000
525 #define P9_STAT_MODE_SETGID 0x00040000
526 #define P9_STAT_MODE_SETVTX 0x00010000
528 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
529 P9_STAT_MODE_SYMLINK | \
530 P9_STAT_MODE_LINK | \
531 P9_STAT_MODE_DEVICE | \
532 P9_STAT_MODE_NAMED_PIPE | \
535 /* This is the algorithm from ufs in spfs */
536 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
540 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
541 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
542 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
543 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
545 if (S_ISDIR(stbuf
->st_mode
)) {
546 qidp
->type
|= P9_QID_TYPE_DIR
;
548 if (S_ISLNK(stbuf
->st_mode
)) {
549 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
553 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
558 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
562 stat_to_qid(&stbuf
, qidp
);
566 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
570 if (!QLIST_EMPTY(&s
->free_list
)) {
571 pdu
= QLIST_FIRST(&s
->free_list
);
572 QLIST_REMOVE(pdu
, next
);
573 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
578 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
582 * Cancelled pdu are added back to the freelist
585 if (!pdu
->cancelled
) {
586 QLIST_REMOVE(pdu
, next
);
587 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
593 * We don't do error checking for pdu_marshal/unmarshal here
594 * because we always expect to have enough space to encode
597 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
599 int8_t id
= pdu
->id
+ 1; /* Response */
605 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
608 str
.data
= strerror(err
);
609 str
.size
= strlen(str
.data
);
611 len
+= pdu_marshal(pdu
, len
, "s", &str
);
615 len
+= pdu_marshal(pdu
, len
, "d", err
);
617 if (s
->proto_version
== V9FS_PROTO_2000L
) {
620 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
623 /* fill out the header */
624 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
626 /* keep these in sync */
630 /* push onto queue and notify */
631 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
633 /* FIXME: we should batch these completions */
634 virtio_notify(VIRTIO_DEVICE(s
), s
->vq
);
636 /* Now wakeup anybody waiting in flush for this request */
637 qemu_co_queue_next(&pdu
->complete
);
642 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
647 if (mode
& P9_STAT_MODE_DIR
) {
651 if (mode
& P9_STAT_MODE_SYMLINK
) {
654 if (mode
& P9_STAT_MODE_SOCKET
) {
657 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
660 if (mode
& P9_STAT_MODE_DEVICE
) {
661 if (extension
->size
&& extension
->data
[0] == 'c') {
672 if (mode
& P9_STAT_MODE_SETUID
) {
675 if (mode
& P9_STAT_MODE_SETGID
) {
678 if (mode
& P9_STAT_MODE_SETVTX
) {
685 static int donttouch_stat(V9fsStat
*stat
)
687 if (stat
->type
== -1 &&
689 stat
->qid
.type
== -1 &&
690 stat
->qid
.version
== -1 &&
691 stat
->qid
.path
== -1 &&
695 stat
->length
== -1 &&
702 stat
->n_muid
== -1) {
709 static void v9fs_stat_init(V9fsStat
*stat
)
711 v9fs_string_init(&stat
->name
);
712 v9fs_string_init(&stat
->uid
);
713 v9fs_string_init(&stat
->gid
);
714 v9fs_string_init(&stat
->muid
);
715 v9fs_string_init(&stat
->extension
);
718 static void v9fs_stat_free(V9fsStat
*stat
)
720 v9fs_string_free(&stat
->name
);
721 v9fs_string_free(&stat
->uid
);
722 v9fs_string_free(&stat
->gid
);
723 v9fs_string_free(&stat
->muid
);
724 v9fs_string_free(&stat
->extension
);
727 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
731 mode
= stbuf
->st_mode
& 0777;
732 if (S_ISDIR(stbuf
->st_mode
)) {
733 mode
|= P9_STAT_MODE_DIR
;
736 if (S_ISLNK(stbuf
->st_mode
)) {
737 mode
|= P9_STAT_MODE_SYMLINK
;
740 if (S_ISSOCK(stbuf
->st_mode
)) {
741 mode
|= P9_STAT_MODE_SOCKET
;
744 if (S_ISFIFO(stbuf
->st_mode
)) {
745 mode
|= P9_STAT_MODE_NAMED_PIPE
;
748 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
749 mode
|= P9_STAT_MODE_DEVICE
;
752 if (stbuf
->st_mode
& S_ISUID
) {
753 mode
|= P9_STAT_MODE_SETUID
;
756 if (stbuf
->st_mode
& S_ISGID
) {
757 mode
|= P9_STAT_MODE_SETGID
;
760 if (stbuf
->st_mode
& S_ISVTX
) {
761 mode
|= P9_STAT_MODE_SETVTX
;
767 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
768 const struct stat
*stbuf
,
774 memset(v9stat
, 0, sizeof(*v9stat
));
776 stat_to_qid(stbuf
, &v9stat
->qid
);
777 v9stat
->mode
= stat_to_v9mode(stbuf
);
778 v9stat
->atime
= stbuf
->st_atime
;
779 v9stat
->mtime
= stbuf
->st_mtime
;
780 v9stat
->length
= stbuf
->st_size
;
782 v9fs_string_null(&v9stat
->uid
);
783 v9fs_string_null(&v9stat
->gid
);
784 v9fs_string_null(&v9stat
->muid
);
786 v9stat
->n_uid
= stbuf
->st_uid
;
787 v9stat
->n_gid
= stbuf
->st_gid
;
790 v9fs_string_null(&v9stat
->extension
);
792 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
793 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
797 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
798 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
799 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
800 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
801 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
802 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
803 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
806 str
= strrchr(name
->data
, '/');
813 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
816 v9fs_string_size(&v9stat
->name
) +
817 v9fs_string_size(&v9stat
->uid
) +
818 v9fs_string_size(&v9stat
->gid
) +
819 v9fs_string_size(&v9stat
->muid
) +
820 v9fs_string_size(&v9stat
->extension
);
824 #define P9_STATS_MODE 0x00000001ULL
825 #define P9_STATS_NLINK 0x00000002ULL
826 #define P9_STATS_UID 0x00000004ULL
827 #define P9_STATS_GID 0x00000008ULL
828 #define P9_STATS_RDEV 0x00000010ULL
829 #define P9_STATS_ATIME 0x00000020ULL
830 #define P9_STATS_MTIME 0x00000040ULL
831 #define P9_STATS_CTIME 0x00000080ULL
832 #define P9_STATS_INO 0x00000100ULL
833 #define P9_STATS_SIZE 0x00000200ULL
834 #define P9_STATS_BLOCKS 0x00000400ULL
836 #define P9_STATS_BTIME 0x00000800ULL
837 #define P9_STATS_GEN 0x00001000ULL
838 #define P9_STATS_DATA_VERSION 0x00002000ULL
840 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
841 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
844 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
845 V9fsStatDotl
*v9lstat
)
847 memset(v9lstat
, 0, sizeof(*v9lstat
));
849 v9lstat
->st_mode
= stbuf
->st_mode
;
850 v9lstat
->st_nlink
= stbuf
->st_nlink
;
851 v9lstat
->st_uid
= stbuf
->st_uid
;
852 v9lstat
->st_gid
= stbuf
->st_gid
;
853 v9lstat
->st_rdev
= stbuf
->st_rdev
;
854 v9lstat
->st_size
= stbuf
->st_size
;
855 v9lstat
->st_blksize
= stbuf
->st_blksize
;
856 v9lstat
->st_blocks
= stbuf
->st_blocks
;
857 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
858 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
859 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
860 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
861 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
862 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
863 /* Currently we only support BASIC fields in stat */
864 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
866 stat_to_qid(stbuf
, &v9lstat
->qid
);
869 static void print_sg(struct iovec
*sg
, int cnt
)
873 printf("sg[%d]: {", cnt
);
874 for (i
= 0; i
< cnt
; i
++) {
878 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
883 /* Will call this only for path name based fid */
884 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
887 v9fs_path_init(&str
);
888 v9fs_path_copy(&str
, dst
);
889 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
890 v9fs_path_free(&str
);
891 /* +1 to include terminating NULL */
895 static inline bool is_ro_export(FsContext
*ctx
)
897 return ctx
->export_flags
& V9FS_RDONLY
;
900 static void v9fs_version(void *opaque
)
903 V9fsPDU
*pdu
= opaque
;
904 V9fsState
*s
= pdu
->s
;
908 v9fs_string_init(&version
);
909 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
914 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
918 if (!strcmp(version
.data
, "9P2000.u")) {
919 s
->proto_version
= V9FS_PROTO_2000U
;
920 } else if (!strcmp(version
.data
, "9P2000.L")) {
921 s
->proto_version
= V9FS_PROTO_2000L
;
923 v9fs_string_sprintf(&version
, "unknown");
926 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
932 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
934 complete_pdu(s
, pdu
, offset
);
935 v9fs_string_free(&version
);
938 static void v9fs_attach(void *opaque
)
940 V9fsPDU
*pdu
= opaque
;
941 V9fsState
*s
= pdu
->s
;
942 int32_t fid
, afid
, n_uname
;
943 V9fsString uname
, aname
;
949 v9fs_string_init(&uname
);
950 v9fs_string_init(&aname
);
951 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
952 &afid
, &uname
, &aname
, &n_uname
);
956 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
958 fidp
= alloc_fid(s
, fid
);
964 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
970 err
= fid_to_qid(pdu
, fidp
, &qid
);
976 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
982 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
983 qid
.type
, qid
.version
, qid
.path
);
985 * disable migration if we haven't done already.
986 * attach could get called multiple times for the same export.
988 if (!s
->migration_blocker
) {
990 error_setg(&s
->migration_blocker
,
991 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
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
);
1086 /* we have valid st_gen: update result mask */
1087 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1090 /* request cancelled, e.g. by Tflush */
1093 /* failed to get st_gen: not fatal, ignore */
1097 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1102 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1103 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1104 v9stat_dotl
.st_gid
);
1108 complete_pdu(s
, pdu
, retval
);
1111 /* Attribute flags */
1112 #define P9_ATTR_MODE (1 << 0)
1113 #define P9_ATTR_UID (1 << 1)
1114 #define P9_ATTR_GID (1 << 2)
1115 #define P9_ATTR_SIZE (1 << 3)
1116 #define P9_ATTR_ATIME (1 << 4)
1117 #define P9_ATTR_MTIME (1 << 5)
1118 #define P9_ATTR_CTIME (1 << 6)
1119 #define P9_ATTR_ATIME_SET (1 << 7)
1120 #define P9_ATTR_MTIME_SET (1 << 8)
1122 #define P9_ATTR_MASK 127
1124 static void v9fs_setattr(void *opaque
)
1131 V9fsPDU
*pdu
= opaque
;
1132 V9fsState
*s
= pdu
->s
;
1134 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1139 fidp
= get_fid(pdu
, fid
);
1144 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1145 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1150 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1151 struct timespec times
[2];
1152 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1153 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1154 times
[0].tv_sec
= v9iattr
.atime_sec
;
1155 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1157 times
[0].tv_nsec
= UTIME_NOW
;
1160 times
[0].tv_nsec
= UTIME_OMIT
;
1162 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1163 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1164 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1165 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1167 times
[1].tv_nsec
= UTIME_NOW
;
1170 times
[1].tv_nsec
= UTIME_OMIT
;
1172 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1178 * If the only valid entry in iattr is ctime we can call
1179 * chown(-1,-1) to update the ctime of the file
1181 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1182 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1183 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1184 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1187 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1190 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1196 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1197 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1206 complete_pdu(s
, pdu
, err
);
1209 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1215 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1220 for (i
= 0; i
< nwnames
; i
++) {
1221 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1230 static void v9fs_walk(void *opaque
)
1233 V9fsQID
*qids
= NULL
;
1235 V9fsPath dpath
, path
;
1239 int32_t fid
, newfid
;
1240 V9fsString
*wnames
= NULL
;
1242 V9fsFidState
*newfidp
= NULL
;
1243 V9fsPDU
*pdu
= opaque
;
1244 V9fsState
*s
= pdu
->s
;
1246 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1248 complete_pdu(s
, pdu
, err
);
1253 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1255 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1256 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1257 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1258 for (i
= 0; i
< nwnames
; i
++) {
1259 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1265 } else if (nwnames
> P9_MAXWELEM
) {
1269 fidp
= get_fid(pdu
, fid
);
1274 v9fs_path_init(&dpath
);
1275 v9fs_path_init(&path
);
1277 * Both dpath and path initially poin to fidp.
1278 * Needed to handle request with nwnames == 0
1280 v9fs_path_copy(&dpath
, &fidp
->path
);
1281 v9fs_path_copy(&path
, &fidp
->path
);
1282 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1283 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1287 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1291 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1292 v9fs_path_copy(&dpath
, &path
);
1294 if (fid
== newfid
) {
1295 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1296 v9fs_path_copy(&fidp
->path
, &path
);
1298 newfidp
= alloc_fid(s
, newfid
);
1299 if (newfidp
== NULL
) {
1303 newfidp
->uid
= fidp
->uid
;
1304 v9fs_path_copy(&newfidp
->path
, &path
);
1306 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1307 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1311 put_fid(pdu
, newfidp
);
1313 v9fs_path_free(&dpath
);
1314 v9fs_path_free(&path
);
1316 complete_pdu(s
, pdu
, err
);
1317 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1318 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1319 v9fs_string_free(&wnames
[name_idx
]);
1326 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1328 struct statfs stbuf
;
1330 V9fsState
*s
= pdu
->s
;
1333 * iounit should be multiples of f_bsize (host filesystem block size
1334 * and as well as less than (client msize - P9_IOHDRSZ))
1336 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1337 iounit
= stbuf
.f_bsize
;
1338 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1341 iounit
= s
->msize
- P9_IOHDRSZ
;
1346 static void v9fs_open(void *opaque
)
1357 V9fsPDU
*pdu
= opaque
;
1358 V9fsState
*s
= pdu
->s
;
1360 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1361 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1364 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1370 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1372 fidp
= get_fid(pdu
, fid
);
1377 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1379 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1383 stat_to_qid(&stbuf
, &qid
);
1384 if (S_ISDIR(stbuf
.st_mode
)) {
1385 err
= v9fs_co_opendir(pdu
, fidp
);
1389 fidp
->fid_type
= P9_FID_DIR
;
1390 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1396 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1397 flags
= get_dotl_openflags(s
, mode
);
1399 flags
= omode_to_uflags(mode
);
1401 if (is_ro_export(&s
->ctx
)) {
1402 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1403 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1408 err
= v9fs_co_open(pdu
, fidp
, flags
);
1412 fidp
->fid_type
= P9_FID_FILE
;
1413 fidp
->open_flags
= flags
;
1414 if (flags
& O_EXCL
) {
1416 * We let the host file system do O_EXCL check
1417 * We should not reclaim such fd
1419 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1421 iounit
= get_iounit(pdu
, &fidp
->path
);
1422 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1428 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1429 qid
.type
, qid
.version
, qid
.path
, iounit
);
1433 complete_pdu(s
, pdu
, err
);
1436 static void v9fs_lcreate(void *opaque
)
1438 int32_t dfid
, flags
, mode
;
1447 V9fsPDU
*pdu
= opaque
;
1449 v9fs_string_init(&name
);
1450 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1451 &name
, &flags
, &mode
, &gid
);
1455 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1457 fidp
= get_fid(pdu
, dfid
);
1463 flags
= get_dotl_openflags(pdu
->s
, flags
);
1464 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1465 flags
| O_CREAT
, mode
, &stbuf
);
1469 fidp
->fid_type
= P9_FID_FILE
;
1470 fidp
->open_flags
= flags
;
1471 if (flags
& O_EXCL
) {
1473 * We let the host file system do O_EXCL check
1474 * We should not reclaim such fd
1476 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1478 iounit
= get_iounit(pdu
, &fidp
->path
);
1479 stat_to_qid(&stbuf
, &qid
);
1480 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1485 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1486 qid
.type
, qid
.version
, qid
.path
, iounit
);
1490 complete_pdu(pdu
->s
, pdu
, err
);
1491 v9fs_string_free(&name
);
1494 static void v9fs_fsync(void *opaque
)
1501 V9fsPDU
*pdu
= opaque
;
1502 V9fsState
*s
= pdu
->s
;
1504 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1508 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1510 fidp
= get_fid(pdu
, fid
);
1515 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1521 complete_pdu(s
, pdu
, err
);
1524 static void v9fs_clunk(void *opaque
)
1530 V9fsPDU
*pdu
= opaque
;
1531 V9fsState
*s
= pdu
->s
;
1533 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1537 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1539 fidp
= clunk_fid(s
, fid
);
1545 * Bump the ref so that put_fid will
1549 err
= put_fid(pdu
, fidp
);
1554 complete_pdu(s
, pdu
, err
);
1557 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1558 uint64_t off
, uint32_t max_count
)
1565 xattr_len
= fidp
->fs
.xattr
.len
;
1566 read_count
= xattr_len
- off
;
1567 if (read_count
> max_count
) {
1568 read_count
= max_count
;
1569 } else if (read_count
< 0) {
1571 * read beyond XATTR value
1575 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1580 err
= v9fs_pack(pdu
->elem
.in_sg
, pdu
->elem
.in_num
, offset
,
1581 ((char *)fidp
->fs
.xattr
.value
) + off
,
1590 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1591 V9fsFidState
*fidp
, uint32_t max_count
)
1598 off_t saved_dir_pos
;
1599 struct dirent
*dent
, *result
;
1601 /* save the directory position */
1602 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1603 if (saved_dir_pos
< 0) {
1604 return saved_dir_pos
;
1607 dent
= g_malloc(sizeof(struct dirent
));
1610 v9fs_path_init(&path
);
1611 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1612 if (err
|| !result
) {
1615 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1619 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1623 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1627 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1628 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1629 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1630 /* Ran out of buffer. Set dir back to old position and return */
1631 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1632 v9fs_stat_free(&v9stat
);
1633 v9fs_path_free(&path
);
1638 v9fs_stat_free(&v9stat
);
1639 v9fs_path_free(&path
);
1640 saved_dir_pos
= dent
->d_off
;
1644 v9fs_path_free(&path
);
1652 * Create a QEMUIOVector for a sub-region of PDU iovecs
1654 * @qiov: uninitialized QEMUIOVector
1655 * @skip: number of bytes to skip from beginning of PDU
1656 * @size: number of bytes to include
1657 * @is_write: true - write, false - read
1659 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1660 * with qemu_iovec_destroy().
1662 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1663 size_t skip
, size_t size
,
1671 iov
= pdu
->elem
.out_sg
;
1672 niov
= pdu
->elem
.out_num
;
1674 iov
= pdu
->elem
.in_sg
;
1675 niov
= pdu
->elem
.in_num
;
1678 qemu_iovec_init_external(&elem
, iov
, niov
);
1679 qemu_iovec_init(qiov
, niov
);
1680 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1683 static void v9fs_read(void *opaque
)
1692 V9fsPDU
*pdu
= opaque
;
1693 V9fsState
*s
= pdu
->s
;
1695 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1699 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1701 fidp
= get_fid(pdu
, fid
);
1706 if (fidp
->fid_type
== P9_FID_DIR
) {
1709 v9fs_co_rewinddir(pdu
, fidp
);
1711 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1716 err
= pdu_marshal(pdu
, offset
, "d", count
);
1720 err
+= offset
+ count
;
1721 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1722 QEMUIOVector qiov_full
;
1726 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1727 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1729 qemu_iovec_reset(&qiov
);
1730 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1732 print_sg(qiov
.iov
, qiov
.niov
);
1734 /* Loop in case of EINTR */
1736 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1741 } while (len
== -EINTR
&& !pdu
->cancelled
);
1743 /* IO error return the error */
1747 } while (count
< max_count
&& len
> 0);
1748 err
= pdu_marshal(pdu
, offset
, "d", count
);
1752 err
+= offset
+ count
;
1753 qemu_iovec_destroy(&qiov
);
1754 qemu_iovec_destroy(&qiov_full
);
1755 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1756 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1760 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1764 complete_pdu(s
, pdu
, err
);
1767 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1770 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1771 * size of type (1) + size of name.size (2) + strlen(name.data)
1773 return 24 + v9fs_string_size(name
);
1776 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1777 V9fsFidState
*fidp
, int32_t max_count
)
1784 off_t saved_dir_pos
;
1785 struct dirent
*dent
, *result
;
1787 /* save the directory position */
1788 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1789 if (saved_dir_pos
< 0) {
1790 return saved_dir_pos
;
1793 dent
= g_malloc(sizeof(struct dirent
));
1796 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1797 if (err
|| !result
) {
1800 v9fs_string_init(&name
);
1801 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1802 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1803 /* Ran out of buffer. Set dir back to old position and return */
1804 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1805 v9fs_string_free(&name
);
1810 * Fill up just the path field of qid because the client uses
1811 * only that. To fill the entire qid structure we will have
1812 * to stat each dirent found, which is expensive
1814 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1815 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1816 /* Fill the other fields with dummy values */
1820 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1821 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1823 dent
->d_type
, &name
);
1825 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1826 v9fs_string_free(&name
);
1831 v9fs_string_free(&name
);
1832 saved_dir_pos
= dent
->d_off
;
1841 static void v9fs_readdir(void *opaque
)
1847 uint64_t initial_offset
;
1850 V9fsPDU
*pdu
= opaque
;
1851 V9fsState
*s
= pdu
->s
;
1853 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1854 &initial_offset
, &max_count
);
1858 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1860 fidp
= get_fid(pdu
, fid
);
1865 if (!fidp
->fs
.dir
) {
1869 if (initial_offset
== 0) {
1870 v9fs_co_rewinddir(pdu
, fidp
);
1872 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1874 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1879 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1883 retval
+= count
+ offset
;
1884 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1888 complete_pdu(s
, pdu
, retval
);
1891 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1892 uint64_t off
, uint32_t count
,
1893 struct iovec
*sg
, int cnt
)
1902 xattr_len
= fidp
->fs
.xattr
.len
;
1903 write_count
= xattr_len
- off
;
1904 if (write_count
> count
) {
1905 write_count
= count
;
1906 } else if (write_count
< 0) {
1908 * write beyond XATTR value len specified in
1914 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1919 fidp
->fs
.xattr
.copied_len
+= write_count
;
1921 * Now copy the content from sg list
1923 for (i
= 0; i
< cnt
; i
++) {
1924 if (write_count
> sg
[i
].iov_len
) {
1925 to_copy
= sg
[i
].iov_len
;
1927 to_copy
= write_count
;
1929 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1930 /* updating vs->off since we are not using below */
1932 write_count
-= to_copy
;
1938 static void v9fs_write(void *opaque
)
1948 V9fsPDU
*pdu
= opaque
;
1949 V9fsState
*s
= pdu
->s
;
1950 QEMUIOVector qiov_full
;
1953 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
1955 complete_pdu(s
, pdu
, err
);
1959 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
1960 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
1962 fidp
= get_fid(pdu
, fid
);
1967 if (fidp
->fid_type
== P9_FID_FILE
) {
1968 if (fidp
->fs
.fd
== -1) {
1972 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1974 * setxattr operation
1976 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
1977 qiov_full
.iov
, qiov_full
.niov
);
1983 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1985 qemu_iovec_reset(&qiov
);
1986 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
1988 print_sg(qiov
.iov
, qiov
.niov
);
1990 /* Loop in case of EINTR */
1992 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1997 } while (len
== -EINTR
&& !pdu
->cancelled
);
1999 /* IO error return the error */
2003 } while (total
< count
&& len
> 0);
2006 err
= pdu_marshal(pdu
, offset
, "d", total
);
2011 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2013 qemu_iovec_destroy(&qiov
);
2017 qemu_iovec_destroy(&qiov_full
);
2018 complete_pdu(s
, pdu
, err
);
2021 static void v9fs_create(void *opaque
)
2033 V9fsString extension
;
2035 V9fsPDU
*pdu
= opaque
;
2037 v9fs_path_init(&path
);
2038 v9fs_string_init(&name
);
2039 v9fs_string_init(&extension
);
2040 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2041 &perm
, &mode
, &extension
);
2045 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2047 fidp
= get_fid(pdu
, fid
);
2052 if (perm
& P9_STAT_MODE_DIR
) {
2053 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2054 fidp
->uid
, -1, &stbuf
);
2058 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2062 v9fs_path_copy(&fidp
->path
, &path
);
2063 err
= v9fs_co_opendir(pdu
, fidp
);
2067 fidp
->fid_type
= P9_FID_DIR
;
2068 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2069 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2070 extension
.data
, -1 , &stbuf
);
2074 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2078 v9fs_path_copy(&fidp
->path
, &path
);
2079 } else if (perm
& P9_STAT_MODE_LINK
) {
2080 int32_t ofid
= atoi(extension
.data
);
2081 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2082 if (ofidp
== NULL
) {
2086 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2087 put_fid(pdu
, ofidp
);
2091 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2093 fidp
->fid_type
= P9_FID_NONE
;
2096 v9fs_path_copy(&fidp
->path
, &path
);
2097 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2099 fidp
->fid_type
= P9_FID_NONE
;
2102 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2104 uint32_t major
, minor
;
2107 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2124 nmode
|= perm
& 0777;
2125 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2126 makedev(major
, minor
), nmode
, &stbuf
);
2130 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2134 v9fs_path_copy(&fidp
->path
, &path
);
2135 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2136 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2137 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2141 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2145 v9fs_path_copy(&fidp
->path
, &path
);
2146 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2147 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2148 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2152 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2156 v9fs_path_copy(&fidp
->path
, &path
);
2158 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2159 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2163 fidp
->fid_type
= P9_FID_FILE
;
2164 fidp
->open_flags
= omode_to_uflags(mode
);
2165 if (fidp
->open_flags
& O_EXCL
) {
2167 * We let the host file system do O_EXCL check
2168 * We should not reclaim such fd
2170 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2173 iounit
= get_iounit(pdu
, &fidp
->path
);
2174 stat_to_qid(&stbuf
, &qid
);
2175 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2180 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2181 qid
.type
, qid
.version
, qid
.path
, iounit
);
2185 complete_pdu(pdu
->s
, pdu
, err
);
2186 v9fs_string_free(&name
);
2187 v9fs_string_free(&extension
);
2188 v9fs_path_free(&path
);
2191 static void v9fs_symlink(void *opaque
)
2193 V9fsPDU
*pdu
= opaque
;
2196 V9fsFidState
*dfidp
;
2204 v9fs_string_init(&name
);
2205 v9fs_string_init(&symname
);
2206 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2210 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2212 dfidp
= get_fid(pdu
, dfid
);
2213 if (dfidp
== NULL
) {
2217 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2221 stat_to_qid(&stbuf
, &qid
);
2222 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2227 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2228 qid
.type
, qid
.version
, qid
.path
);
2230 put_fid(pdu
, dfidp
);
2232 complete_pdu(pdu
->s
, pdu
, err
);
2233 v9fs_string_free(&name
);
2234 v9fs_string_free(&symname
);
2237 static void v9fs_flush(void *opaque
)
2242 V9fsPDU
*cancel_pdu
;
2243 V9fsPDU
*pdu
= opaque
;
2244 V9fsState
*s
= pdu
->s
;
2246 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2248 complete_pdu(s
, pdu
, err
);
2251 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2253 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2254 if (cancel_pdu
->tag
== tag
) {
2259 cancel_pdu
->cancelled
= 1;
2261 * Wait for pdu to complete.
2263 qemu_co_queue_wait(&cancel_pdu
->complete
);
2264 cancel_pdu
->cancelled
= 0;
2265 free_pdu(pdu
->s
, cancel_pdu
);
2267 complete_pdu(s
, pdu
, 7);
2270 static void v9fs_link(void *opaque
)
2272 V9fsPDU
*pdu
= opaque
;
2273 V9fsState
*s
= pdu
->s
;
2274 int32_t dfid
, oldfid
;
2275 V9fsFidState
*dfidp
, *oldfidp
;
2280 v9fs_string_init(&name
);
2281 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2285 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2287 dfidp
= get_fid(pdu
, dfid
);
2288 if (dfidp
== NULL
) {
2293 oldfidp
= get_fid(pdu
, oldfid
);
2294 if (oldfidp
== NULL
) {
2298 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2303 put_fid(pdu
, dfidp
);
2305 v9fs_string_free(&name
);
2306 complete_pdu(s
, pdu
, err
);
2309 /* Only works with path name based fid */
2310 static void v9fs_remove(void *opaque
)
2316 V9fsPDU
*pdu
= opaque
;
2318 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2322 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2324 fidp
= get_fid(pdu
, fid
);
2329 /* if fs driver is not path based, return EOPNOTSUPP */
2330 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2335 * IF the file is unlinked, we cannot reopen
2336 * the file later. So don't reclaim fd
2338 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2342 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2347 /* For TREMOVE we need to clunk the fid even on failed remove */
2348 clunk_fid(pdu
->s
, fidp
->fid
);
2351 complete_pdu(pdu
->s
, pdu
, err
);
2354 static void v9fs_unlinkat(void *opaque
)
2358 int32_t dfid
, flags
;
2361 V9fsFidState
*dfidp
;
2362 V9fsPDU
*pdu
= opaque
;
2364 v9fs_string_init(&name
);
2365 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2369 dfidp
= get_fid(pdu
, dfid
);
2370 if (dfidp
== NULL
) {
2375 * IF the file is unlinked, we cannot reopen
2376 * the file later. So don't reclaim fd
2378 v9fs_path_init(&path
);
2379 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2383 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2387 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2392 put_fid(pdu
, dfidp
);
2393 v9fs_path_free(&path
);
2395 complete_pdu(pdu
->s
, pdu
, err
);
2396 v9fs_string_free(&name
);
2400 /* Only works with path name based fid */
2401 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2402 int32_t newdirfid
, V9fsString
*name
)
2407 V9fsFidState
*tfidp
;
2408 V9fsState
*s
= pdu
->s
;
2409 V9fsFidState
*dirfidp
= NULL
;
2410 char *old_name
, *new_name
;
2412 v9fs_path_init(&new_path
);
2413 if (newdirfid
!= -1) {
2414 dirfidp
= get_fid(pdu
, newdirfid
);
2415 if (dirfidp
== NULL
) {
2419 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2420 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2422 old_name
= fidp
->path
.data
;
2423 end
= strrchr(old_name
, '/');
2429 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2430 strncat(new_name
, old_name
, end
- old_name
);
2431 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2432 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2435 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2440 * Fixup fid's pointing to the old name to
2441 * start pointing to the new name
2443 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2444 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2445 /* replace the name */
2446 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2451 put_fid(pdu
, dirfidp
);
2453 v9fs_path_free(&new_path
);
2458 /* Only works with path name based fid */
2459 static void v9fs_rename(void *opaque
)
2467 V9fsPDU
*pdu
= opaque
;
2468 V9fsState
*s
= pdu
->s
;
2470 v9fs_string_init(&name
);
2471 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2475 fidp
= get_fid(pdu
, fid
);
2480 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2481 /* if fs driver is not path based, return EOPNOTSUPP */
2482 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2486 v9fs_path_write_lock(s
);
2487 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2488 v9fs_path_unlock(s
);
2495 complete_pdu(s
, pdu
, err
);
2496 v9fs_string_free(&name
);
2499 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2500 V9fsString
*old_name
, V9fsPath
*newdir
,
2501 V9fsString
*new_name
)
2503 V9fsFidState
*tfidp
;
2504 V9fsPath oldpath
, newpath
;
2505 V9fsState
*s
= pdu
->s
;
2508 v9fs_path_init(&oldpath
);
2509 v9fs_path_init(&newpath
);
2510 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2511 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2514 * Fixup fid's pointing to the old name to
2515 * start pointing to the new name
2517 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2518 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2519 /* replace the name */
2520 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2523 v9fs_path_free(&oldpath
);
2524 v9fs_path_free(&newpath
);
2527 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2528 V9fsString
*old_name
, int32_t newdirfid
,
2529 V9fsString
*new_name
)
2532 V9fsState
*s
= pdu
->s
;
2533 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2535 olddirfidp
= get_fid(pdu
, olddirfid
);
2536 if (olddirfidp
== NULL
) {
2540 if (newdirfid
!= -1) {
2541 newdirfidp
= get_fid(pdu
, newdirfid
);
2542 if (newdirfidp
== NULL
) {
2547 newdirfidp
= get_fid(pdu
, olddirfid
);
2550 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2551 &newdirfidp
->path
, new_name
);
2555 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2556 /* Only for path based fid we need to do the below fixup */
2557 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2558 &newdirfidp
->path
, new_name
);
2562 put_fid(pdu
, olddirfidp
);
2565 put_fid(pdu
, newdirfidp
);
2570 static void v9fs_renameat(void *opaque
)
2574 V9fsPDU
*pdu
= opaque
;
2575 V9fsState
*s
= pdu
->s
;
2576 int32_t olddirfid
, newdirfid
;
2577 V9fsString old_name
, new_name
;
2579 v9fs_string_init(&old_name
);
2580 v9fs_string_init(&new_name
);
2581 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2582 &old_name
, &newdirfid
, &new_name
);
2587 v9fs_path_write_lock(s
);
2588 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2589 &old_name
, newdirfid
, &new_name
);
2590 v9fs_path_unlock(s
);
2596 complete_pdu(s
, pdu
, err
);
2597 v9fs_string_free(&old_name
);
2598 v9fs_string_free(&new_name
);
2601 static void v9fs_wstat(void *opaque
)
2610 V9fsPDU
*pdu
= opaque
;
2611 V9fsState
*s
= pdu
->s
;
2613 v9fs_stat_init(&v9stat
);
2614 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2618 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2619 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2621 fidp
= get_fid(pdu
, fid
);
2626 /* do we need to sync the file? */
2627 if (donttouch_stat(&v9stat
)) {
2628 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2631 if (v9stat
.mode
!= -1) {
2633 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2637 v9_mode
= stat_to_v9mode(&stbuf
);
2638 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2639 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2640 /* Attempting to change the type */
2644 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2645 v9mode_to_mode(v9stat
.mode
,
2646 &v9stat
.extension
));
2651 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2652 struct timespec times
[2];
2653 if (v9stat
.atime
!= -1) {
2654 times
[0].tv_sec
= v9stat
.atime
;
2655 times
[0].tv_nsec
= 0;
2657 times
[0].tv_nsec
= UTIME_OMIT
;
2659 if (v9stat
.mtime
!= -1) {
2660 times
[1].tv_sec
= v9stat
.mtime
;
2661 times
[1].tv_nsec
= 0;
2663 times
[1].tv_nsec
= UTIME_OMIT
;
2665 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2670 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2671 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2676 if (v9stat
.name
.size
!= 0) {
2677 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2682 if (v9stat
.length
!= -1) {
2683 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2692 v9fs_stat_free(&v9stat
);
2693 complete_pdu(s
, pdu
, err
);
2696 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2708 int32_t bsize_factor
;
2711 * compute bsize factor based on host file system block size
2714 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2715 if (!bsize_factor
) {
2718 f_type
= stbuf
->f_type
;
2719 f_bsize
= stbuf
->f_bsize
;
2720 f_bsize
*= bsize_factor
;
2722 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2723 * adjust(divide) the number of blocks, free blocks and available
2724 * blocks by bsize factor
2726 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2727 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2728 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2729 f_files
= stbuf
->f_files
;
2730 f_ffree
= stbuf
->f_ffree
;
2731 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2732 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2733 f_namelen
= stbuf
->f_namelen
;
2735 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2736 f_type
, f_bsize
, f_blocks
, f_bfree
,
2737 f_bavail
, f_files
, f_ffree
,
2738 fsid_val
, f_namelen
);
2741 static void v9fs_statfs(void *opaque
)
2747 struct statfs stbuf
;
2748 V9fsPDU
*pdu
= opaque
;
2749 V9fsState
*s
= pdu
->s
;
2751 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2755 fidp
= get_fid(pdu
, fid
);
2760 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2764 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2772 complete_pdu(s
, pdu
, retval
);
2775 static void v9fs_mknod(void *opaque
)
2788 V9fsPDU
*pdu
= opaque
;
2789 V9fsState
*s
= pdu
->s
;
2791 v9fs_string_init(&name
);
2792 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2793 &major
, &minor
, &gid
);
2797 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2799 fidp
= get_fid(pdu
, fid
);
2804 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2805 makedev(major
, minor
), mode
, &stbuf
);
2809 stat_to_qid(&stbuf
, &qid
);
2810 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2815 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2816 qid
.type
, qid
.version
, qid
.path
);
2820 complete_pdu(s
, pdu
, err
);
2821 v9fs_string_free(&name
);
2825 * Implement posix byte range locking code
2826 * Server side handling of locking code is very simple, because 9p server in
2827 * QEMU can handle only one client. And most of the lock handling
2828 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2829 * do any thing in * qemu 9p server side lock code path.
2830 * So when a TLOCK request comes, always return success
2832 static void v9fs_lock(void *opaque
)
2839 int32_t fid
, err
= 0;
2840 V9fsPDU
*pdu
= opaque
;
2841 V9fsState
*s
= pdu
->s
;
2843 status
= P9_LOCK_ERROR
;
2844 v9fs_string_init(&flock
.client_id
);
2845 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2846 &flock
.flags
, &flock
.start
, &flock
.length
,
2847 &flock
.proc_id
, &flock
.client_id
);
2851 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2852 flock
.type
, flock
.start
, flock
.length
);
2855 /* We support only block flag now (that too ignored currently) */
2856 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2860 fidp
= get_fid(pdu
, fid
);
2865 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2869 status
= P9_LOCK_SUCCESS
;
2873 err
= pdu_marshal(pdu
, offset
, "b", status
);
2877 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2878 complete_pdu(s
, pdu
, err
);
2879 v9fs_string_free(&flock
.client_id
);
2883 * When a TGETLOCK request comes, always return success because all lock
2884 * handling is done by client's VFS layer.
2886 static void v9fs_getlock(void *opaque
)
2892 int32_t fid
, err
= 0;
2893 V9fsPDU
*pdu
= opaque
;
2894 V9fsState
*s
= pdu
->s
;
2896 v9fs_string_init(&glock
.client_id
);
2897 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
2898 &glock
.start
, &glock
.length
, &glock
.proc_id
,
2903 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2904 glock
.type
, glock
.start
, glock
.length
);
2906 fidp
= get_fid(pdu
, fid
);
2911 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2915 glock
.type
= P9_LOCK_TYPE_UNLCK
;
2916 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
2917 glock
.start
, glock
.length
, glock
.proc_id
,
2923 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
2924 glock
.length
, glock
.proc_id
);
2928 complete_pdu(s
, pdu
, err
);
2929 v9fs_string_free(&glock
.client_id
);
2932 static void v9fs_mkdir(void *opaque
)
2934 V9fsPDU
*pdu
= opaque
;
2945 v9fs_string_init(&name
);
2946 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2950 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
2952 fidp
= get_fid(pdu
, fid
);
2957 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2961 stat_to_qid(&stbuf
, &qid
);
2962 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2967 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
2968 qid
.type
, qid
.version
, qid
.path
, err
);
2972 complete_pdu(pdu
->s
, pdu
, err
);
2973 v9fs_string_free(&name
);
2976 static void v9fs_xattrwalk(void *opaque
)
2982 int32_t fid
, newfid
;
2983 V9fsFidState
*file_fidp
;
2984 V9fsFidState
*xattr_fidp
= NULL
;
2985 V9fsPDU
*pdu
= opaque
;
2986 V9fsState
*s
= pdu
->s
;
2988 v9fs_string_init(&name
);
2989 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
2993 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
2995 file_fidp
= get_fid(pdu
, fid
);
2996 if (file_fidp
== NULL
) {
3000 xattr_fidp
= alloc_fid(s
, newfid
);
3001 if (xattr_fidp
== NULL
) {
3005 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3006 if (name
.data
== NULL
) {
3008 * listxattr request. Get the size first
3010 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3013 clunk_fid(s
, xattr_fidp
->fid
);
3017 * Read the xattr value
3019 xattr_fidp
->fs
.xattr
.len
= size
;
3020 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3021 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3023 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3024 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3025 xattr_fidp
->fs
.xattr
.value
,
3026 xattr_fidp
->fs
.xattr
.len
);
3028 clunk_fid(s
, xattr_fidp
->fid
);
3032 err
= pdu_marshal(pdu
, offset
, "q", size
);
3039 * specific xattr fid. We check for xattr
3040 * presence also collect the xattr size
3042 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3046 clunk_fid(s
, xattr_fidp
->fid
);
3050 * Read the xattr value
3052 xattr_fidp
->fs
.xattr
.len
= size
;
3053 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3054 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3056 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3057 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3058 &name
, xattr_fidp
->fs
.xattr
.value
,
3059 xattr_fidp
->fs
.xattr
.len
);
3061 clunk_fid(s
, xattr_fidp
->fid
);
3065 err
= pdu_marshal(pdu
, offset
, "q", size
);
3071 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3073 put_fid(pdu
, file_fidp
);
3075 put_fid(pdu
, xattr_fidp
);
3078 complete_pdu(s
, pdu
, err
);
3079 v9fs_string_free(&name
);
3082 static void v9fs_xattrcreate(void *opaque
)
3090 V9fsFidState
*file_fidp
;
3091 V9fsFidState
*xattr_fidp
;
3092 V9fsPDU
*pdu
= opaque
;
3093 V9fsState
*s
= pdu
->s
;
3095 v9fs_string_init(&name
);
3096 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3100 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3102 file_fidp
= get_fid(pdu
, fid
);
3103 if (file_fidp
== NULL
) {
3107 /* Make the file fid point to xattr */
3108 xattr_fidp
= file_fidp
;
3109 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3110 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3111 xattr_fidp
->fs
.xattr
.len
= size
;
3112 xattr_fidp
->fs
.xattr
.flags
= flags
;
3113 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3114 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3115 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3117 put_fid(pdu
, file_fidp
);
3119 complete_pdu(s
, pdu
, err
);
3120 v9fs_string_free(&name
);
3123 static void v9fs_readlink(void *opaque
)
3125 V9fsPDU
*pdu
= opaque
;
3132 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3136 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3137 fidp
= get_fid(pdu
, fid
);
3143 v9fs_string_init(&target
);
3144 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3148 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3150 v9fs_string_free(&target
);
3154 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3155 v9fs_string_free(&target
);
3159 complete_pdu(pdu
->s
, pdu
, err
);
3162 static CoroutineEntry
*pdu_co_handlers
[] = {
3163 [P9_TREADDIR
] = v9fs_readdir
,
3164 [P9_TSTATFS
] = v9fs_statfs
,
3165 [P9_TGETATTR
] = v9fs_getattr
,
3166 [P9_TSETATTR
] = v9fs_setattr
,
3167 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3168 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3169 [P9_TMKNOD
] = v9fs_mknod
,
3170 [P9_TRENAME
] = v9fs_rename
,
3171 [P9_TLOCK
] = v9fs_lock
,
3172 [P9_TGETLOCK
] = v9fs_getlock
,
3173 [P9_TRENAMEAT
] = v9fs_renameat
,
3174 [P9_TREADLINK
] = v9fs_readlink
,
3175 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3176 [P9_TMKDIR
] = v9fs_mkdir
,
3177 [P9_TVERSION
] = v9fs_version
,
3178 [P9_TLOPEN
] = v9fs_open
,
3179 [P9_TATTACH
] = v9fs_attach
,
3180 [P9_TSTAT
] = v9fs_stat
,
3181 [P9_TWALK
] = v9fs_walk
,
3182 [P9_TCLUNK
] = v9fs_clunk
,
3183 [P9_TFSYNC
] = v9fs_fsync
,
3184 [P9_TOPEN
] = v9fs_open
,
3185 [P9_TREAD
] = v9fs_read
,
3187 [P9_TAUTH
] = v9fs_auth
,
3189 [P9_TFLUSH
] = v9fs_flush
,
3190 [P9_TLINK
] = v9fs_link
,
3191 [P9_TSYMLINK
] = v9fs_symlink
,
3192 [P9_TCREATE
] = v9fs_create
,
3193 [P9_TLCREATE
] = v9fs_lcreate
,
3194 [P9_TWRITE
] = v9fs_write
,
3195 [P9_TWSTAT
] = v9fs_wstat
,
3196 [P9_TREMOVE
] = v9fs_remove
,
3199 static void v9fs_op_not_supp(void *opaque
)
3201 V9fsPDU
*pdu
= opaque
;
3202 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
3205 static void v9fs_fs_ro(void *opaque
)
3207 V9fsPDU
*pdu
= opaque
;
3208 complete_pdu(pdu
->s
, pdu
, -EROFS
);
3211 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3238 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3241 CoroutineEntry
*handler
;
3243 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3244 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3245 handler
= v9fs_op_not_supp
;
3247 handler
= pdu_co_handlers
[pdu
->id
];
3250 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3251 handler
= v9fs_fs_ro
;
3253 co
= qemu_coroutine_create(handler
);
3254 qemu_coroutine_enter(co
, pdu
);
3257 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3259 V9fsState
*s
= (V9fsState
*)vdev
;
3263 while ((pdu
= alloc_pdu(s
)) &&
3264 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3273 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3274 QEMU_BUILD_BUG_ON(sizeof out
!= 7);
3276 len
= iov_to_buf(pdu
->elem
.out_sg
, pdu
->elem
.out_num
, 0,
3278 BUG_ON(len
!= sizeof out
);
3280 pdu
->size
= le32_to_cpu(out
.size_le
);
3283 pdu
->tag
= le16_to_cpu(out
.tag_le
);
3285 qemu_co_queue_init(&pdu
->complete
);
3291 static void __attribute__((__constructor__
)) virtio_9p_set_fd_limit(void)
3294 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3295 fprintf(stderr
, "Failed to get the resource limit\n");
3298 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3299 open_fd_rc
= rlim
.rlim_cur
/2;