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/sockets.h"
17 #include "virtio-9p.h"
18 #include "fsdev/qemu-fsdev.h"
19 #include "virtio-9p-xattr.h"
20 #include "virtio-9p-coth.h"
22 #include "migration/migration.h"
26 static int open_fd_rc
;
40 static int omode_to_uflags(int8_t mode
)
74 struct dotl_openflag_map
{
79 static int dotl_to_open_flags(int flags
)
83 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
84 * and P9_DOTL_NOACCESS
86 int oflags
= flags
& O_ACCMODE
;
88 struct dotl_openflag_map dotl_oflag_map
[] = {
89 { P9_DOTL_CREATE
, O_CREAT
},
90 { P9_DOTL_EXCL
, O_EXCL
},
91 { P9_DOTL_NOCTTY
, O_NOCTTY
},
92 { P9_DOTL_TRUNC
, O_TRUNC
},
93 { P9_DOTL_APPEND
, O_APPEND
},
94 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
95 { P9_DOTL_DSYNC
, O_DSYNC
},
96 { P9_DOTL_FASYNC
, FASYNC
},
97 { P9_DOTL_DIRECT
, O_DIRECT
},
98 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
99 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
100 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
101 { P9_DOTL_NOATIME
, O_NOATIME
},
102 { P9_DOTL_SYNC
, O_SYNC
},
105 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
106 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
107 oflags
|= dotl_oflag_map
[i
].open_flag
;
114 void cred_init(FsCred
*credp
)
122 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
126 * Filter the client open flags
128 flags
= dotl_to_open_flags(oflags
);
129 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
131 * Ignore direct disk access hint until the server supports it.
137 void v9fs_path_init(V9fsPath
*path
)
143 void v9fs_path_free(V9fsPath
*path
)
150 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
153 lhs
->data
= g_malloc(rhs
->size
);
154 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
155 lhs
->size
= rhs
->size
;
158 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
159 const char *name
, V9fsPath
*path
)
162 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
170 * Return TRUE if s1 is an ancestor of s2.
172 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
173 * As a special case, We treat s1 as ancestor of s2 if they are same!
175 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
177 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
178 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
185 static size_t v9fs_string_size(V9fsString
*str
)
191 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
192 static int v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
195 if (f
->fid_type
== P9_FID_FILE
) {
196 if (f
->fs
.fd
== -1) {
198 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
199 } while (err
== -EINTR
&& !pdu
->cancelled
);
201 } else if (f
->fid_type
== P9_FID_DIR
) {
202 if (f
->fs
.dir
== NULL
) {
204 err
= v9fs_co_opendir(pdu
, f
);
205 } while (err
== -EINTR
&& !pdu
->cancelled
);
211 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
215 V9fsState
*s
= pdu
->s
;
217 for (f
= s
->fid_list
; f
; f
= f
->next
) {
221 * Update the fid ref upfront so that
222 * we don't get reclaimed when we yield
227 * check whether we need to reopen the
228 * file. We might have closed the fd
229 * while trying to free up some file
232 err
= v9fs_reopen_fid(pdu
, f
);
238 * Mark the fid as referenced so that the LRU
239 * reclaim won't close the file descriptor
241 f
->flags
|= FID_REFERENCED
;
248 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
252 for (f
= s
->fid_list
; f
; f
= f
->next
) {
253 /* If fid is already there return NULL */
259 f
= g_malloc0(sizeof(V9fsFidState
));
261 f
->fid_type
= P9_FID_NONE
;
264 * Mark the fid as referenced so that the LRU
265 * reclaim won't close the file descriptor
267 f
->flags
|= FID_REFERENCED
;
268 f
->next
= s
->fid_list
;
274 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
278 if (fidp
->fs
.xattr
.copied_len
== -1) {
279 /* getxattr/listxattr fid */
283 * if this is fid for setxattr. clunk should
284 * result in setxattr localcall
286 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
287 /* clunk after partial write */
291 if (fidp
->fs
.xattr
.len
) {
292 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
293 fidp
->fs
.xattr
.value
,
295 fidp
->fs
.xattr
.flags
);
297 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
300 v9fs_string_free(&fidp
->fs
.xattr
.name
);
302 if (fidp
->fs
.xattr
.value
) {
303 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_set(&s
->migration_blocker
, QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION
,
991 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
992 migrate_add_blocker(s
->migration_blocker
);
997 complete_pdu(s
, pdu
, err
);
998 v9fs_string_free(&uname
);
999 v9fs_string_free(&aname
);
1002 static void v9fs_stat(void *opaque
)
1010 V9fsPDU
*pdu
= opaque
;
1011 V9fsState
*s
= pdu
->s
;
1013 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1017 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1019 fidp
= get_fid(pdu
, fid
);
1024 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1028 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1032 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1034 v9fs_stat_free(&v9stat
);
1037 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1038 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1040 v9fs_stat_free(&v9stat
);
1044 complete_pdu(s
, pdu
, err
);
1047 static void v9fs_getattr(void *opaque
)
1054 uint64_t request_mask
;
1055 V9fsStatDotl v9stat_dotl
;
1056 V9fsPDU
*pdu
= opaque
;
1057 V9fsState
*s
= pdu
->s
;
1059 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1063 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1065 fidp
= get_fid(pdu
, fid
);
1071 * Currently we only support BASIC fields in stat, so there is no
1072 * need to look at request_mask.
1074 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1078 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1080 /* fill st_gen if requested and supported by underlying fs */
1081 if (request_mask
& P9_STATS_GEN
) {
1082 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1085 /* we have valid st_gen: update result mask */
1086 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1089 /* request cancelled, e.g. by Tflush */
1092 /* failed to get st_gen: not fatal, ignore */
1096 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1101 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1102 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1103 v9stat_dotl
.st_gid
);
1107 complete_pdu(s
, pdu
, retval
);
1110 /* Attribute flags */
1111 #define P9_ATTR_MODE (1 << 0)
1112 #define P9_ATTR_UID (1 << 1)
1113 #define P9_ATTR_GID (1 << 2)
1114 #define P9_ATTR_SIZE (1 << 3)
1115 #define P9_ATTR_ATIME (1 << 4)
1116 #define P9_ATTR_MTIME (1 << 5)
1117 #define P9_ATTR_CTIME (1 << 6)
1118 #define P9_ATTR_ATIME_SET (1 << 7)
1119 #define P9_ATTR_MTIME_SET (1 << 8)
1121 #define P9_ATTR_MASK 127
1123 static void v9fs_setattr(void *opaque
)
1130 V9fsPDU
*pdu
= opaque
;
1131 V9fsState
*s
= pdu
->s
;
1133 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1138 fidp
= get_fid(pdu
, fid
);
1143 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1144 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1149 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1150 struct timespec times
[2];
1151 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1152 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1153 times
[0].tv_sec
= v9iattr
.atime_sec
;
1154 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1156 times
[0].tv_nsec
= UTIME_NOW
;
1159 times
[0].tv_nsec
= UTIME_OMIT
;
1161 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1162 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1163 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1164 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1166 times
[1].tv_nsec
= UTIME_NOW
;
1169 times
[1].tv_nsec
= UTIME_OMIT
;
1171 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1177 * If the only valid entry in iattr is ctime we can call
1178 * chown(-1,-1) to update the ctime of the file
1180 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1181 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1182 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1183 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1186 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1189 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1195 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1196 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1205 complete_pdu(s
, pdu
, err
);
1208 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1214 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1219 for (i
= 0; i
< nwnames
; i
++) {
1220 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1229 static void v9fs_walk(void *opaque
)
1232 V9fsQID
*qids
= NULL
;
1234 V9fsPath dpath
, path
;
1238 int32_t fid
, newfid
;
1239 V9fsString
*wnames
= NULL
;
1241 V9fsFidState
*newfidp
= NULL
;
1242 V9fsPDU
*pdu
= opaque
;
1243 V9fsState
*s
= pdu
->s
;
1245 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1247 complete_pdu(s
, pdu
, err
);
1252 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1254 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1255 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1256 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1257 for (i
= 0; i
< nwnames
; i
++) {
1258 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1264 } else if (nwnames
> P9_MAXWELEM
) {
1268 fidp
= get_fid(pdu
, fid
);
1273 v9fs_path_init(&dpath
);
1274 v9fs_path_init(&path
);
1276 * Both dpath and path initially poin to fidp.
1277 * Needed to handle request with nwnames == 0
1279 v9fs_path_copy(&dpath
, &fidp
->path
);
1280 v9fs_path_copy(&path
, &fidp
->path
);
1281 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1282 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1286 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1290 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1291 v9fs_path_copy(&dpath
, &path
);
1293 if (fid
== newfid
) {
1294 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1295 v9fs_path_copy(&fidp
->path
, &path
);
1297 newfidp
= alloc_fid(s
, newfid
);
1298 if (newfidp
== NULL
) {
1302 newfidp
->uid
= fidp
->uid
;
1303 v9fs_path_copy(&newfidp
->path
, &path
);
1305 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1306 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1310 put_fid(pdu
, newfidp
);
1312 v9fs_path_free(&dpath
);
1313 v9fs_path_free(&path
);
1315 complete_pdu(s
, pdu
, err
);
1316 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1317 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1318 v9fs_string_free(&wnames
[name_idx
]);
1325 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1327 struct statfs stbuf
;
1329 V9fsState
*s
= pdu
->s
;
1332 * iounit should be multiples of f_bsize (host filesystem block size
1333 * and as well as less than (client msize - P9_IOHDRSZ))
1335 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1336 iounit
= stbuf
.f_bsize
;
1337 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1340 iounit
= s
->msize
- P9_IOHDRSZ
;
1345 static void v9fs_open(void *opaque
)
1356 V9fsPDU
*pdu
= opaque
;
1357 V9fsState
*s
= pdu
->s
;
1359 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1360 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1363 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1369 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1371 fidp
= get_fid(pdu
, fid
);
1376 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1378 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1382 stat_to_qid(&stbuf
, &qid
);
1383 if (S_ISDIR(stbuf
.st_mode
)) {
1384 err
= v9fs_co_opendir(pdu
, fidp
);
1388 fidp
->fid_type
= P9_FID_DIR
;
1389 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1395 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1396 flags
= get_dotl_openflags(s
, mode
);
1398 flags
= omode_to_uflags(mode
);
1400 if (is_ro_export(&s
->ctx
)) {
1401 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1402 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1407 err
= v9fs_co_open(pdu
, fidp
, flags
);
1411 fidp
->fid_type
= P9_FID_FILE
;
1412 fidp
->open_flags
= flags
;
1413 if (flags
& O_EXCL
) {
1415 * We let the host file system do O_EXCL check
1416 * We should not reclaim such fd
1418 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1420 iounit
= get_iounit(pdu
, &fidp
->path
);
1421 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1427 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1428 qid
.type
, qid
.version
, qid
.path
, iounit
);
1432 complete_pdu(s
, pdu
, err
);
1435 static void v9fs_lcreate(void *opaque
)
1437 int32_t dfid
, flags
, mode
;
1446 V9fsPDU
*pdu
= opaque
;
1448 v9fs_string_init(&name
);
1449 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1450 &name
, &flags
, &mode
, &gid
);
1454 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1456 fidp
= get_fid(pdu
, dfid
);
1462 flags
= get_dotl_openflags(pdu
->s
, flags
);
1463 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1464 flags
| O_CREAT
, mode
, &stbuf
);
1468 fidp
->fid_type
= P9_FID_FILE
;
1469 fidp
->open_flags
= flags
;
1470 if (flags
& O_EXCL
) {
1472 * We let the host file system do O_EXCL check
1473 * We should not reclaim such fd
1475 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1477 iounit
= get_iounit(pdu
, &fidp
->path
);
1478 stat_to_qid(&stbuf
, &qid
);
1479 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1484 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1485 qid
.type
, qid
.version
, qid
.path
, iounit
);
1489 complete_pdu(pdu
->s
, pdu
, err
);
1490 v9fs_string_free(&name
);
1493 static void v9fs_fsync(void *opaque
)
1500 V9fsPDU
*pdu
= opaque
;
1501 V9fsState
*s
= pdu
->s
;
1503 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1507 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1509 fidp
= get_fid(pdu
, fid
);
1514 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1520 complete_pdu(s
, pdu
, err
);
1523 static void v9fs_clunk(void *opaque
)
1529 V9fsPDU
*pdu
= opaque
;
1530 V9fsState
*s
= pdu
->s
;
1532 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1536 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1538 fidp
= clunk_fid(s
, fid
);
1544 * Bump the ref so that put_fid will
1548 err
= put_fid(pdu
, fidp
);
1553 complete_pdu(s
, pdu
, err
);
1556 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1557 uint64_t off
, uint32_t max_count
)
1564 xattr_len
= fidp
->fs
.xattr
.len
;
1565 read_count
= xattr_len
- off
;
1566 if (read_count
> max_count
) {
1567 read_count
= max_count
;
1568 } else if (read_count
< 0) {
1570 * read beyond XATTR value
1574 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1579 err
= v9fs_pack(pdu
->elem
.in_sg
, pdu
->elem
.in_num
, offset
,
1580 ((char *)fidp
->fs
.xattr
.value
) + off
,
1589 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1590 V9fsFidState
*fidp
, uint32_t max_count
)
1597 off_t saved_dir_pos
;
1598 struct dirent
*dent
, *result
;
1600 /* save the directory position */
1601 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1602 if (saved_dir_pos
< 0) {
1603 return saved_dir_pos
;
1606 dent
= g_malloc(sizeof(struct dirent
));
1609 v9fs_path_init(&path
);
1610 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1611 if (err
|| !result
) {
1614 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1618 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1622 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1626 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1627 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1628 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1629 /* Ran out of buffer. Set dir back to old position and return */
1630 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1631 v9fs_stat_free(&v9stat
);
1632 v9fs_path_free(&path
);
1637 v9fs_stat_free(&v9stat
);
1638 v9fs_path_free(&path
);
1639 saved_dir_pos
= dent
->d_off
;
1643 v9fs_path_free(&path
);
1651 * Create a QEMUIOVector for a sub-region of PDU iovecs
1653 * @qiov: uninitialized QEMUIOVector
1654 * @skip: number of bytes to skip from beginning of PDU
1655 * @size: number of bytes to include
1656 * @is_write: true - write, false - read
1658 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1659 * with qemu_iovec_destroy().
1661 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1662 size_t skip
, size_t size
,
1670 iov
= pdu
->elem
.out_sg
;
1671 niov
= pdu
->elem
.out_num
;
1673 iov
= pdu
->elem
.in_sg
;
1674 niov
= pdu
->elem
.in_num
;
1677 qemu_iovec_init_external(&elem
, iov
, niov
);
1678 qemu_iovec_init(qiov
, niov
);
1679 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1682 static void v9fs_read(void *opaque
)
1691 V9fsPDU
*pdu
= opaque
;
1692 V9fsState
*s
= pdu
->s
;
1694 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1698 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1700 fidp
= get_fid(pdu
, fid
);
1705 if (fidp
->fid_type
== P9_FID_DIR
) {
1708 v9fs_co_rewinddir(pdu
, fidp
);
1710 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1715 err
= pdu_marshal(pdu
, offset
, "d", count
);
1719 err
+= offset
+ count
;
1720 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1721 QEMUIOVector qiov_full
;
1725 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1726 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1728 qemu_iovec_reset(&qiov
);
1729 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1731 print_sg(qiov
.iov
, qiov
.niov
);
1733 /* Loop in case of EINTR */
1735 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1740 } while (len
== -EINTR
&& !pdu
->cancelled
);
1742 /* IO error return the error */
1746 } while (count
< max_count
&& len
> 0);
1747 err
= pdu_marshal(pdu
, offset
, "d", count
);
1751 err
+= offset
+ count
;
1752 qemu_iovec_destroy(&qiov
);
1753 qemu_iovec_destroy(&qiov_full
);
1754 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1755 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1759 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1763 complete_pdu(s
, pdu
, err
);
1766 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1769 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1770 * size of type (1) + size of name.size (2) + strlen(name.data)
1772 return 24 + v9fs_string_size(name
);
1775 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1776 V9fsFidState
*fidp
, int32_t max_count
)
1783 off_t saved_dir_pos
;
1784 struct dirent
*dent
, *result
;
1786 /* save the directory position */
1787 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1788 if (saved_dir_pos
< 0) {
1789 return saved_dir_pos
;
1792 dent
= g_malloc(sizeof(struct dirent
));
1795 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1796 if (err
|| !result
) {
1799 v9fs_string_init(&name
);
1800 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1801 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1802 /* Ran out of buffer. Set dir back to old position and return */
1803 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1804 v9fs_string_free(&name
);
1809 * Fill up just the path field of qid because the client uses
1810 * only that. To fill the entire qid structure we will have
1811 * to stat each dirent found, which is expensive
1813 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1814 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1815 /* Fill the other fields with dummy values */
1819 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1820 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1822 dent
->d_type
, &name
);
1824 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1825 v9fs_string_free(&name
);
1830 v9fs_string_free(&name
);
1831 saved_dir_pos
= dent
->d_off
;
1840 static void v9fs_readdir(void *opaque
)
1846 uint64_t initial_offset
;
1849 V9fsPDU
*pdu
= opaque
;
1850 V9fsState
*s
= pdu
->s
;
1852 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1853 &initial_offset
, &max_count
);
1857 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1859 fidp
= get_fid(pdu
, fid
);
1864 if (!fidp
->fs
.dir
) {
1868 if (initial_offset
== 0) {
1869 v9fs_co_rewinddir(pdu
, fidp
);
1871 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1873 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1878 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1882 retval
+= count
+ offset
;
1883 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1887 complete_pdu(s
, pdu
, retval
);
1890 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1891 uint64_t off
, uint32_t count
,
1892 struct iovec
*sg
, int cnt
)
1901 xattr_len
= fidp
->fs
.xattr
.len
;
1902 write_count
= xattr_len
- off
;
1903 if (write_count
> count
) {
1904 write_count
= count
;
1905 } else if (write_count
< 0) {
1907 * write beyond XATTR value len specified in
1913 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
1918 fidp
->fs
.xattr
.copied_len
+= write_count
;
1920 * Now copy the content from sg list
1922 for (i
= 0; i
< cnt
; i
++) {
1923 if (write_count
> sg
[i
].iov_len
) {
1924 to_copy
= sg
[i
].iov_len
;
1926 to_copy
= write_count
;
1928 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1929 /* updating vs->off since we are not using below */
1931 write_count
-= to_copy
;
1937 static void v9fs_write(void *opaque
)
1947 V9fsPDU
*pdu
= opaque
;
1948 V9fsState
*s
= pdu
->s
;
1949 QEMUIOVector qiov_full
;
1952 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
1954 return complete_pdu(s
, pdu
, err
);
1957 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
1958 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
1960 fidp
= get_fid(pdu
, fid
);
1965 if (fidp
->fid_type
== P9_FID_FILE
) {
1966 if (fidp
->fs
.fd
== -1) {
1970 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1972 * setxattr operation
1974 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
1975 qiov_full
.iov
, qiov_full
.niov
);
1981 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1983 qemu_iovec_reset(&qiov
);
1984 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
1986 print_sg(qiov
.iov
, qiov
.niov
);
1988 /* Loop in case of EINTR */
1990 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1995 } while (len
== -EINTR
&& !pdu
->cancelled
);
1997 /* IO error return the error */
2001 } while (total
< count
&& len
> 0);
2004 err
= pdu_marshal(pdu
, offset
, "d", total
);
2009 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2011 qemu_iovec_destroy(&qiov
);
2015 qemu_iovec_destroy(&qiov_full
);
2016 complete_pdu(s
, pdu
, err
);
2019 static void v9fs_create(void *opaque
)
2031 V9fsString extension
;
2033 V9fsPDU
*pdu
= opaque
;
2035 v9fs_path_init(&path
);
2036 v9fs_string_init(&name
);
2037 v9fs_string_init(&extension
);
2038 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2039 &perm
, &mode
, &extension
);
2043 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2045 fidp
= get_fid(pdu
, fid
);
2050 if (perm
& P9_STAT_MODE_DIR
) {
2051 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2052 fidp
->uid
, -1, &stbuf
);
2056 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2060 v9fs_path_copy(&fidp
->path
, &path
);
2061 err
= v9fs_co_opendir(pdu
, fidp
);
2065 fidp
->fid_type
= P9_FID_DIR
;
2066 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2067 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2068 extension
.data
, -1 , &stbuf
);
2072 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2076 v9fs_path_copy(&fidp
->path
, &path
);
2077 } else if (perm
& P9_STAT_MODE_LINK
) {
2078 int32_t ofid
= atoi(extension
.data
);
2079 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2080 if (ofidp
== NULL
) {
2084 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2085 put_fid(pdu
, ofidp
);
2089 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2091 fidp
->fid_type
= P9_FID_NONE
;
2094 v9fs_path_copy(&fidp
->path
, &path
);
2095 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2097 fidp
->fid_type
= P9_FID_NONE
;
2100 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2102 uint32_t major
, minor
;
2105 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2122 nmode
|= perm
& 0777;
2123 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2124 makedev(major
, minor
), nmode
, &stbuf
);
2128 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2132 v9fs_path_copy(&fidp
->path
, &path
);
2133 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2134 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2135 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2139 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2143 v9fs_path_copy(&fidp
->path
, &path
);
2144 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2145 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2146 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2150 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2154 v9fs_path_copy(&fidp
->path
, &path
);
2156 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2157 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2161 fidp
->fid_type
= P9_FID_FILE
;
2162 fidp
->open_flags
= omode_to_uflags(mode
);
2163 if (fidp
->open_flags
& O_EXCL
) {
2165 * We let the host file system do O_EXCL check
2166 * We should not reclaim such fd
2168 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2171 iounit
= get_iounit(pdu
, &fidp
->path
);
2172 stat_to_qid(&stbuf
, &qid
);
2173 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2178 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2179 qid
.type
, qid
.version
, qid
.path
, iounit
);
2183 complete_pdu(pdu
->s
, pdu
, err
);
2184 v9fs_string_free(&name
);
2185 v9fs_string_free(&extension
);
2186 v9fs_path_free(&path
);
2189 static void v9fs_symlink(void *opaque
)
2191 V9fsPDU
*pdu
= opaque
;
2194 V9fsFidState
*dfidp
;
2202 v9fs_string_init(&name
);
2203 v9fs_string_init(&symname
);
2204 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2208 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2210 dfidp
= get_fid(pdu
, dfid
);
2211 if (dfidp
== NULL
) {
2215 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2219 stat_to_qid(&stbuf
, &qid
);
2220 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2225 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2226 qid
.type
, qid
.version
, qid
.path
);
2228 put_fid(pdu
, dfidp
);
2230 complete_pdu(pdu
->s
, pdu
, err
);
2231 v9fs_string_free(&name
);
2232 v9fs_string_free(&symname
);
2235 static void v9fs_flush(void *opaque
)
2240 V9fsPDU
*cancel_pdu
;
2241 V9fsPDU
*pdu
= opaque
;
2242 V9fsState
*s
= pdu
->s
;
2244 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2246 complete_pdu(s
, pdu
, err
);
2249 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2251 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2252 if (cancel_pdu
->tag
== tag
) {
2257 cancel_pdu
->cancelled
= 1;
2259 * Wait for pdu to complete.
2261 qemu_co_queue_wait(&cancel_pdu
->complete
);
2262 cancel_pdu
->cancelled
= 0;
2263 free_pdu(pdu
->s
, cancel_pdu
);
2265 complete_pdu(s
, pdu
, 7);
2268 static void v9fs_link(void *opaque
)
2270 V9fsPDU
*pdu
= opaque
;
2271 V9fsState
*s
= pdu
->s
;
2272 int32_t dfid
, oldfid
;
2273 V9fsFidState
*dfidp
, *oldfidp
;
2278 v9fs_string_init(&name
);
2279 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2283 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2285 dfidp
= get_fid(pdu
, dfid
);
2286 if (dfidp
== NULL
) {
2291 oldfidp
= get_fid(pdu
, oldfid
);
2292 if (oldfidp
== NULL
) {
2296 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2301 put_fid(pdu
, dfidp
);
2303 v9fs_string_free(&name
);
2304 complete_pdu(s
, pdu
, err
);
2307 /* Only works with path name based fid */
2308 static void v9fs_remove(void *opaque
)
2314 V9fsPDU
*pdu
= opaque
;
2316 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2320 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2322 fidp
= get_fid(pdu
, fid
);
2327 /* if fs driver is not path based, return EOPNOTSUPP */
2328 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2333 * IF the file is unlinked, we cannot reopen
2334 * the file later. So don't reclaim fd
2336 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2340 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2345 /* For TREMOVE we need to clunk the fid even on failed remove */
2346 clunk_fid(pdu
->s
, fidp
->fid
);
2349 complete_pdu(pdu
->s
, pdu
, err
);
2352 static void v9fs_unlinkat(void *opaque
)
2356 int32_t dfid
, flags
;
2359 V9fsFidState
*dfidp
;
2360 V9fsPDU
*pdu
= opaque
;
2362 v9fs_string_init(&name
);
2363 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2367 dfidp
= get_fid(pdu
, dfid
);
2368 if (dfidp
== NULL
) {
2373 * IF the file is unlinked, we cannot reopen
2374 * the file later. So don't reclaim fd
2376 v9fs_path_init(&path
);
2377 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2381 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2385 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2390 put_fid(pdu
, dfidp
);
2391 v9fs_path_free(&path
);
2393 complete_pdu(pdu
->s
, pdu
, err
);
2394 v9fs_string_free(&name
);
2398 /* Only works with path name based fid */
2399 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2400 int32_t newdirfid
, V9fsString
*name
)
2405 V9fsFidState
*tfidp
;
2406 V9fsState
*s
= pdu
->s
;
2407 V9fsFidState
*dirfidp
= NULL
;
2408 char *old_name
, *new_name
;
2410 v9fs_path_init(&new_path
);
2411 if (newdirfid
!= -1) {
2412 dirfidp
= get_fid(pdu
, newdirfid
);
2413 if (dirfidp
== NULL
) {
2417 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2418 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2420 old_name
= fidp
->path
.data
;
2421 end
= strrchr(old_name
, '/');
2427 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2428 strncat(new_name
, old_name
, end
- old_name
);
2429 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2430 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2433 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2438 * Fixup fid's pointing to the old name to
2439 * start pointing to the new name
2441 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2442 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2443 /* replace the name */
2444 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2449 put_fid(pdu
, dirfidp
);
2451 v9fs_path_free(&new_path
);
2456 /* Only works with path name based fid */
2457 static void v9fs_rename(void *opaque
)
2465 V9fsPDU
*pdu
= opaque
;
2466 V9fsState
*s
= pdu
->s
;
2468 v9fs_string_init(&name
);
2469 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2473 fidp
= get_fid(pdu
, fid
);
2478 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2479 /* if fs driver is not path based, return EOPNOTSUPP */
2480 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2484 v9fs_path_write_lock(s
);
2485 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2486 v9fs_path_unlock(s
);
2493 complete_pdu(s
, pdu
, err
);
2494 v9fs_string_free(&name
);
2497 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2498 V9fsString
*old_name
, V9fsPath
*newdir
,
2499 V9fsString
*new_name
)
2501 V9fsFidState
*tfidp
;
2502 V9fsPath oldpath
, newpath
;
2503 V9fsState
*s
= pdu
->s
;
2506 v9fs_path_init(&oldpath
);
2507 v9fs_path_init(&newpath
);
2508 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2509 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2512 * Fixup fid's pointing to the old name to
2513 * start pointing to the new name
2515 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2516 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2517 /* replace the name */
2518 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2521 v9fs_path_free(&oldpath
);
2522 v9fs_path_free(&newpath
);
2525 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2526 V9fsString
*old_name
, int32_t newdirfid
,
2527 V9fsString
*new_name
)
2530 V9fsState
*s
= pdu
->s
;
2531 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2533 olddirfidp
= get_fid(pdu
, olddirfid
);
2534 if (olddirfidp
== NULL
) {
2538 if (newdirfid
!= -1) {
2539 newdirfidp
= get_fid(pdu
, newdirfid
);
2540 if (newdirfidp
== NULL
) {
2545 newdirfidp
= get_fid(pdu
, olddirfid
);
2548 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2549 &newdirfidp
->path
, new_name
);
2553 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2554 /* Only for path based fid we need to do the below fixup */
2555 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2556 &newdirfidp
->path
, new_name
);
2560 put_fid(pdu
, olddirfidp
);
2563 put_fid(pdu
, newdirfidp
);
2568 static void v9fs_renameat(void *opaque
)
2572 V9fsPDU
*pdu
= opaque
;
2573 V9fsState
*s
= pdu
->s
;
2574 int32_t olddirfid
, newdirfid
;
2575 V9fsString old_name
, new_name
;
2577 v9fs_string_init(&old_name
);
2578 v9fs_string_init(&new_name
);
2579 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2580 &old_name
, &newdirfid
, &new_name
);
2585 v9fs_path_write_lock(s
);
2586 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2587 &old_name
, newdirfid
, &new_name
);
2588 v9fs_path_unlock(s
);
2594 complete_pdu(s
, pdu
, err
);
2595 v9fs_string_free(&old_name
);
2596 v9fs_string_free(&new_name
);
2599 static void v9fs_wstat(void *opaque
)
2608 V9fsPDU
*pdu
= opaque
;
2609 V9fsState
*s
= pdu
->s
;
2611 v9fs_stat_init(&v9stat
);
2612 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2616 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2617 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2619 fidp
= get_fid(pdu
, fid
);
2624 /* do we need to sync the file? */
2625 if (donttouch_stat(&v9stat
)) {
2626 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2629 if (v9stat
.mode
!= -1) {
2631 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2635 v9_mode
= stat_to_v9mode(&stbuf
);
2636 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2637 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2638 /* Attempting to change the type */
2642 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2643 v9mode_to_mode(v9stat
.mode
,
2644 &v9stat
.extension
));
2649 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2650 struct timespec times
[2];
2651 if (v9stat
.atime
!= -1) {
2652 times
[0].tv_sec
= v9stat
.atime
;
2653 times
[0].tv_nsec
= 0;
2655 times
[0].tv_nsec
= UTIME_OMIT
;
2657 if (v9stat
.mtime
!= -1) {
2658 times
[1].tv_sec
= v9stat
.mtime
;
2659 times
[1].tv_nsec
= 0;
2661 times
[1].tv_nsec
= UTIME_OMIT
;
2663 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2668 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2669 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2674 if (v9stat
.name
.size
!= 0) {
2675 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2680 if (v9stat
.length
!= -1) {
2681 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2690 v9fs_stat_free(&v9stat
);
2691 complete_pdu(s
, pdu
, err
);
2694 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2706 int32_t bsize_factor
;
2709 * compute bsize factor based on host file system block size
2712 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2713 if (!bsize_factor
) {
2716 f_type
= stbuf
->f_type
;
2717 f_bsize
= stbuf
->f_bsize
;
2718 f_bsize
*= bsize_factor
;
2720 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2721 * adjust(divide) the number of blocks, free blocks and available
2722 * blocks by bsize factor
2724 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2725 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2726 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2727 f_files
= stbuf
->f_files
;
2728 f_ffree
= stbuf
->f_ffree
;
2729 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2730 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2731 f_namelen
= stbuf
->f_namelen
;
2733 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2734 f_type
, f_bsize
, f_blocks
, f_bfree
,
2735 f_bavail
, f_files
, f_ffree
,
2736 fsid_val
, f_namelen
);
2739 static void v9fs_statfs(void *opaque
)
2745 struct statfs stbuf
;
2746 V9fsPDU
*pdu
= opaque
;
2747 V9fsState
*s
= pdu
->s
;
2749 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2753 fidp
= get_fid(pdu
, fid
);
2758 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2762 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2770 complete_pdu(s
, pdu
, retval
);
2773 static void v9fs_mknod(void *opaque
)
2786 V9fsPDU
*pdu
= opaque
;
2787 V9fsState
*s
= pdu
->s
;
2789 v9fs_string_init(&name
);
2790 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2791 &major
, &minor
, &gid
);
2795 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2797 fidp
= get_fid(pdu
, fid
);
2802 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2803 makedev(major
, minor
), mode
, &stbuf
);
2807 stat_to_qid(&stbuf
, &qid
);
2808 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2813 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2814 qid
.type
, qid
.version
, qid
.path
);
2818 complete_pdu(s
, pdu
, err
);
2819 v9fs_string_free(&name
);
2823 * Implement posix byte range locking code
2824 * Server side handling of locking code is very simple, because 9p server in
2825 * QEMU can handle only one client. And most of the lock handling
2826 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2827 * do any thing in * qemu 9p server side lock code path.
2828 * So when a TLOCK request comes, always return success
2830 static void v9fs_lock(void *opaque
)
2837 int32_t fid
, err
= 0;
2838 V9fsPDU
*pdu
= opaque
;
2839 V9fsState
*s
= pdu
->s
;
2841 status
= P9_LOCK_ERROR
;
2842 v9fs_string_init(&flock
.client_id
);
2843 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
2844 &flock
.flags
, &flock
.start
, &flock
.length
,
2845 &flock
.proc_id
, &flock
.client_id
);
2849 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2850 flock
.type
, flock
.start
, flock
.length
);
2853 /* We support only block flag now (that too ignored currently) */
2854 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2858 fidp
= get_fid(pdu
, fid
);
2863 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2867 status
= P9_LOCK_SUCCESS
;
2871 err
= pdu_marshal(pdu
, offset
, "b", status
);
2875 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2876 complete_pdu(s
, pdu
, err
);
2877 v9fs_string_free(&flock
.client_id
);
2881 * When a TGETLOCK request comes, always return success because all lock
2882 * handling is done by client's VFS layer.
2884 static void v9fs_getlock(void *opaque
)
2890 int32_t fid
, err
= 0;
2891 V9fsPDU
*pdu
= opaque
;
2892 V9fsState
*s
= pdu
->s
;
2894 v9fs_string_init(&glock
.client_id
);
2895 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
2896 &glock
.start
, &glock
.length
, &glock
.proc_id
,
2901 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2902 glock
.type
, glock
.start
, glock
.length
);
2904 fidp
= get_fid(pdu
, fid
);
2909 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2913 glock
.type
= P9_LOCK_TYPE_UNLCK
;
2914 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
2915 glock
.start
, glock
.length
, glock
.proc_id
,
2921 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
2922 glock
.length
, glock
.proc_id
);
2926 complete_pdu(s
, pdu
, err
);
2927 v9fs_string_free(&glock
.client_id
);
2930 static void v9fs_mkdir(void *opaque
)
2932 V9fsPDU
*pdu
= opaque
;
2943 v9fs_string_init(&name
);
2944 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2948 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
2950 fidp
= get_fid(pdu
, fid
);
2955 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2959 stat_to_qid(&stbuf
, &qid
);
2960 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2965 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
2966 qid
.type
, qid
.version
, qid
.path
, err
);
2970 complete_pdu(pdu
->s
, pdu
, err
);
2971 v9fs_string_free(&name
);
2974 static void v9fs_xattrwalk(void *opaque
)
2980 int32_t fid
, newfid
;
2981 V9fsFidState
*file_fidp
;
2982 V9fsFidState
*xattr_fidp
= NULL
;
2983 V9fsPDU
*pdu
= opaque
;
2984 V9fsState
*s
= pdu
->s
;
2986 v9fs_string_init(&name
);
2987 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
2991 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
2993 file_fidp
= get_fid(pdu
, fid
);
2994 if (file_fidp
== NULL
) {
2998 xattr_fidp
= alloc_fid(s
, newfid
);
2999 if (xattr_fidp
== NULL
) {
3003 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3004 if (name
.data
== NULL
) {
3006 * listxattr request. Get the size first
3008 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3011 clunk_fid(s
, xattr_fidp
->fid
);
3015 * Read the xattr value
3017 xattr_fidp
->fs
.xattr
.len
= size
;
3018 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3019 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3021 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3022 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3023 xattr_fidp
->fs
.xattr
.value
,
3024 xattr_fidp
->fs
.xattr
.len
);
3026 clunk_fid(s
, xattr_fidp
->fid
);
3030 err
= pdu_marshal(pdu
, offset
, "q", size
);
3037 * specific xattr fid. We check for xattr
3038 * presence also collect the xattr size
3040 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3044 clunk_fid(s
, xattr_fidp
->fid
);
3048 * Read the xattr value
3050 xattr_fidp
->fs
.xattr
.len
= size
;
3051 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3052 xattr_fidp
->fs
.xattr
.copied_len
= -1;
3054 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3055 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3056 &name
, xattr_fidp
->fs
.xattr
.value
,
3057 xattr_fidp
->fs
.xattr
.len
);
3059 clunk_fid(s
, xattr_fidp
->fid
);
3063 err
= pdu_marshal(pdu
, offset
, "q", size
);
3069 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3071 put_fid(pdu
, file_fidp
);
3073 put_fid(pdu
, xattr_fidp
);
3076 complete_pdu(s
, pdu
, err
);
3077 v9fs_string_free(&name
);
3080 static void v9fs_xattrcreate(void *opaque
)
3088 V9fsFidState
*file_fidp
;
3089 V9fsFidState
*xattr_fidp
;
3090 V9fsPDU
*pdu
= opaque
;
3091 V9fsState
*s
= pdu
->s
;
3093 v9fs_string_init(&name
);
3094 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3098 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3100 file_fidp
= get_fid(pdu
, fid
);
3101 if (file_fidp
== NULL
) {
3105 /* Make the file fid point to xattr */
3106 xattr_fidp
= file_fidp
;
3107 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3108 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3109 xattr_fidp
->fs
.xattr
.len
= size
;
3110 xattr_fidp
->fs
.xattr
.flags
= flags
;
3111 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3112 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3113 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3115 put_fid(pdu
, file_fidp
);
3117 complete_pdu(s
, pdu
, err
);
3118 v9fs_string_free(&name
);
3121 static void v9fs_readlink(void *opaque
)
3123 V9fsPDU
*pdu
= opaque
;
3130 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3134 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3135 fidp
= get_fid(pdu
, fid
);
3141 v9fs_string_init(&target
);
3142 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3146 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3148 v9fs_string_free(&target
);
3152 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3153 v9fs_string_free(&target
);
3157 complete_pdu(pdu
->s
, pdu
, err
);
3160 static CoroutineEntry
*pdu_co_handlers
[] = {
3161 [P9_TREADDIR
] = v9fs_readdir
,
3162 [P9_TSTATFS
] = v9fs_statfs
,
3163 [P9_TGETATTR
] = v9fs_getattr
,
3164 [P9_TSETATTR
] = v9fs_setattr
,
3165 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3166 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3167 [P9_TMKNOD
] = v9fs_mknod
,
3168 [P9_TRENAME
] = v9fs_rename
,
3169 [P9_TLOCK
] = v9fs_lock
,
3170 [P9_TGETLOCK
] = v9fs_getlock
,
3171 [P9_TRENAMEAT
] = v9fs_renameat
,
3172 [P9_TREADLINK
] = v9fs_readlink
,
3173 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3174 [P9_TMKDIR
] = v9fs_mkdir
,
3175 [P9_TVERSION
] = v9fs_version
,
3176 [P9_TLOPEN
] = v9fs_open
,
3177 [P9_TATTACH
] = v9fs_attach
,
3178 [P9_TSTAT
] = v9fs_stat
,
3179 [P9_TWALK
] = v9fs_walk
,
3180 [P9_TCLUNK
] = v9fs_clunk
,
3181 [P9_TFSYNC
] = v9fs_fsync
,
3182 [P9_TOPEN
] = v9fs_open
,
3183 [P9_TREAD
] = v9fs_read
,
3185 [P9_TAUTH
] = v9fs_auth
,
3187 [P9_TFLUSH
] = v9fs_flush
,
3188 [P9_TLINK
] = v9fs_link
,
3189 [P9_TSYMLINK
] = v9fs_symlink
,
3190 [P9_TCREATE
] = v9fs_create
,
3191 [P9_TLCREATE
] = v9fs_lcreate
,
3192 [P9_TWRITE
] = v9fs_write
,
3193 [P9_TWSTAT
] = v9fs_wstat
,
3194 [P9_TREMOVE
] = v9fs_remove
,
3197 static void v9fs_op_not_supp(void *opaque
)
3199 V9fsPDU
*pdu
= opaque
;
3200 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
3203 static void v9fs_fs_ro(void *opaque
)
3205 V9fsPDU
*pdu
= opaque
;
3206 complete_pdu(pdu
->s
, pdu
, -EROFS
);
3209 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3236 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3239 CoroutineEntry
*handler
;
3241 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3242 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3243 handler
= v9fs_op_not_supp
;
3245 handler
= pdu_co_handlers
[pdu
->id
];
3248 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3249 handler
= v9fs_fs_ro
;
3251 co
= qemu_coroutine_create(handler
);
3252 qemu_coroutine_enter(co
, pdu
);
3255 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3257 V9fsState
*s
= (V9fsState
*)vdev
;
3261 while ((pdu
= alloc_pdu(s
)) &&
3262 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3265 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3266 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3268 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3270 pdu
->size
= le32_to_cpu(*(uint32_t *)ptr
);
3272 pdu
->tag
= le16_to_cpu(*(uint16_t *)(ptr
+ 5));
3273 qemu_co_queue_init(&pdu
->complete
);
3279 static void __attribute__((__constructor__
)) virtio_9p_set_fd_limit(void)
3282 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3283 fprintf(stderr
, "Failed to get the resource limit\n");
3286 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3287 open_fd_rc
= rlim
.rlim_cur
/2;