4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include <glib/gprintf.h>
16 #include "hw/virtio/virtio.h"
17 #include "qapi/error.h"
18 #include "qemu/error-report.h"
20 #include "qemu/sockets.h"
21 #include "virtio-9p.h"
22 #include "fsdev/qemu-fsdev.h"
26 #include "migration/migration.h"
30 static int open_fd_rc
;
44 ssize_t
pdu_marshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
50 ret
= pdu
->s
->transport
->pdu_vmarshal(pdu
, offset
, fmt
, ap
);
56 ssize_t
pdu_unmarshal(V9fsPDU
*pdu
, size_t offset
, const char *fmt
, ...)
62 ret
= pdu
->s
->transport
->pdu_vunmarshal(pdu
, offset
, fmt
, ap
);
68 static void pdu_push_and_notify(V9fsPDU
*pdu
)
70 pdu
->s
->transport
->push_and_notify(pdu
);
73 static int omode_to_uflags(int8_t mode
)
107 struct dotl_openflag_map
{
112 static int dotl_to_open_flags(int flags
)
116 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
117 * and P9_DOTL_NOACCESS
119 int oflags
= flags
& O_ACCMODE
;
121 struct dotl_openflag_map dotl_oflag_map
[] = {
122 { P9_DOTL_CREATE
, O_CREAT
},
123 { P9_DOTL_EXCL
, O_EXCL
},
124 { P9_DOTL_NOCTTY
, O_NOCTTY
},
125 { P9_DOTL_TRUNC
, O_TRUNC
},
126 { P9_DOTL_APPEND
, O_APPEND
},
127 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
128 { P9_DOTL_DSYNC
, O_DSYNC
},
129 { P9_DOTL_FASYNC
, FASYNC
},
130 { P9_DOTL_DIRECT
, O_DIRECT
},
131 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
132 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
133 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
134 { P9_DOTL_NOATIME
, O_NOATIME
},
135 { P9_DOTL_SYNC
, O_SYNC
},
138 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
139 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
140 oflags
|= dotl_oflag_map
[i
].open_flag
;
147 void cred_init(FsCred
*credp
)
155 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
159 * Filter the client open flags
161 flags
= dotl_to_open_flags(oflags
);
162 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
164 * Ignore direct disk access hint until the server supports it.
170 void v9fs_path_init(V9fsPath
*path
)
176 void v9fs_path_free(V9fsPath
*path
)
184 void GCC_FMT_ATTR(2, 3)
185 v9fs_path_sprintf(V9fsPath
*path
, const char *fmt
, ...)
189 v9fs_path_free(path
);
192 /* Bump the size for including terminating NULL */
193 path
->size
= g_vasprintf(&path
->data
, fmt
, ap
) + 1;
197 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
200 lhs
->data
= g_malloc(rhs
->size
);
201 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
202 lhs
->size
= rhs
->size
;
205 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
206 const char *name
, V9fsPath
*path
)
209 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
217 * Return TRUE if s1 is an ancestor of s2.
219 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
220 * As a special case, We treat s1 as ancestor of s2 if they are same!
222 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
224 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
225 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
232 static size_t v9fs_string_size(V9fsString
*str
)
238 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
239 static int coroutine_fn
v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
242 if (f
->fid_type
== P9_FID_FILE
) {
243 if (f
->fs
.fd
== -1) {
245 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
246 } while (err
== -EINTR
&& !pdu
->cancelled
);
248 } else if (f
->fid_type
== P9_FID_DIR
) {
249 if (f
->fs
.dir
.stream
== NULL
) {
251 err
= v9fs_co_opendir(pdu
, f
);
252 } while (err
== -EINTR
&& !pdu
->cancelled
);
258 static V9fsFidState
*coroutine_fn
get_fid(V9fsPDU
*pdu
, int32_t fid
)
262 V9fsState
*s
= pdu
->s
;
264 for (f
= s
->fid_list
; f
; f
= f
->next
) {
268 * Update the fid ref upfront so that
269 * we don't get reclaimed when we yield
274 * check whether we need to reopen the
275 * file. We might have closed the fd
276 * while trying to free up some file
279 err
= v9fs_reopen_fid(pdu
, f
);
285 * Mark the fid as referenced so that the LRU
286 * reclaim won't close the file descriptor
288 f
->flags
|= FID_REFERENCED
;
295 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
299 for (f
= s
->fid_list
; f
; f
= f
->next
) {
300 /* If fid is already there return NULL */
306 f
= g_malloc0(sizeof(V9fsFidState
));
308 f
->fid_type
= P9_FID_NONE
;
311 * Mark the fid as referenced so that the LRU
312 * reclaim won't close the file descriptor
314 f
->flags
|= FID_REFERENCED
;
315 f
->next
= s
->fid_list
;
318 v9fs_readdir_init(&f
->fs
.dir
);
319 v9fs_readdir_init(&f
->fs_reclaim
.dir
);
324 static int coroutine_fn
v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
328 if (fidp
->fs
.xattr
.xattrwalk_fid
) {
329 /* getxattr/listxattr fid */
333 * if this is fid for setxattr. clunk should
334 * result in setxattr localcall
336 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
337 /* clunk after partial write */
341 if (fidp
->fs
.xattr
.len
) {
342 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
343 fidp
->fs
.xattr
.value
,
345 fidp
->fs
.xattr
.flags
);
347 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
350 v9fs_string_free(&fidp
->fs
.xattr
.name
);
352 g_free(fidp
->fs
.xattr
.value
);
356 static int coroutine_fn
free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
360 if (fidp
->fid_type
== P9_FID_FILE
) {
361 /* If we reclaimed the fd no need to close */
362 if (fidp
->fs
.fd
!= -1) {
363 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
365 } else if (fidp
->fid_type
== P9_FID_DIR
) {
366 if (fidp
->fs
.dir
.stream
!= NULL
) {
367 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
369 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
370 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
372 v9fs_path_free(&fidp
->path
);
377 static int coroutine_fn
put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
382 * Don't free the fid if it is in reclaim list
384 if (!fidp
->ref
&& fidp
->clunked
) {
385 if (fidp
->fid
== pdu
->s
->root_fid
) {
387 * if the clunked fid is root fid then we
388 * have unmounted the fs on the client side.
389 * delete the migration blocker. Ideally, this
390 * should be hooked to transport close notification
392 if (pdu
->s
->migration_blocker
) {
393 migrate_del_blocker(pdu
->s
->migration_blocker
);
394 error_free(pdu
->s
->migration_blocker
);
395 pdu
->s
->migration_blocker
= NULL
;
398 return free_fid(pdu
, fidp
);
403 static V9fsFidState
*clunk_fid(V9fsState
*s
, int32_t fid
)
405 V9fsFidState
**fidpp
, *fidp
;
407 for (fidpp
= &s
->fid_list
; *fidpp
; fidpp
= &(*fidpp
)->next
) {
408 if ((*fidpp
)->fid
== fid
) {
412 if (*fidpp
== NULL
) {
421 void coroutine_fn
v9fs_reclaim_fd(V9fsPDU
*pdu
)
423 int reclaim_count
= 0;
424 V9fsState
*s
= pdu
->s
;
425 V9fsFidState
*f
, *reclaim_list
= NULL
;
427 for (f
= s
->fid_list
; f
; f
= f
->next
) {
429 * Unlink fids cannot be reclaimed. Check
430 * for them and skip them. Also skip fids
431 * currently being operated on.
433 if (f
->ref
|| f
->flags
& FID_NON_RECLAIMABLE
) {
437 * if it is a recently referenced fid
438 * we leave the fid untouched and clear the
439 * reference bit. We come back to it later
440 * in the next iteration. (a simple LRU without
441 * moving list elements around)
443 if (f
->flags
& FID_REFERENCED
) {
444 f
->flags
&= ~FID_REFERENCED
;
448 * Add fids to reclaim list.
450 if (f
->fid_type
== P9_FID_FILE
) {
451 if (f
->fs
.fd
!= -1) {
453 * Up the reference count so that
454 * a clunk request won't free this fid
457 f
->rclm_lst
= reclaim_list
;
459 f
->fs_reclaim
.fd
= f
->fs
.fd
;
463 } else if (f
->fid_type
== P9_FID_DIR
) {
464 if (f
->fs
.dir
.stream
!= NULL
) {
466 * Up the reference count so that
467 * a clunk request won't free this fid
470 f
->rclm_lst
= reclaim_list
;
472 f
->fs_reclaim
.dir
.stream
= f
->fs
.dir
.stream
;
473 f
->fs
.dir
.stream
= NULL
;
477 if (reclaim_count
>= open_fd_rc
) {
482 * Now close the fid in reclaim list. Free them if they
483 * are already clunked.
485 while (reclaim_list
) {
487 reclaim_list
= f
->rclm_lst
;
488 if (f
->fid_type
== P9_FID_FILE
) {
489 v9fs_co_close(pdu
, &f
->fs_reclaim
);
490 } else if (f
->fid_type
== P9_FID_DIR
) {
491 v9fs_co_closedir(pdu
, &f
->fs_reclaim
);
495 * Now drop the fid reference, free it
502 static int coroutine_fn
v9fs_mark_fids_unreclaim(V9fsPDU
*pdu
, V9fsPath
*path
)
505 V9fsState
*s
= pdu
->s
;
506 V9fsFidState
*fidp
, head_fid
;
508 head_fid
.next
= s
->fid_list
;
509 for (fidp
= s
->fid_list
; fidp
; fidp
= fidp
->next
) {
510 if (fidp
->path
.size
!= path
->size
) {
513 if (!memcmp(fidp
->path
.data
, path
->data
, path
->size
)) {
514 /* Mark the fid non reclaimable. */
515 fidp
->flags
|= FID_NON_RECLAIMABLE
;
517 /* reopen the file/dir if already closed */
518 err
= v9fs_reopen_fid(pdu
, fidp
);
523 * Go back to head of fid list because
524 * the list could have got updated when
525 * switched to the worker thread
535 static void coroutine_fn
virtfs_reset(V9fsPDU
*pdu
)
537 V9fsState
*s
= pdu
->s
;
541 while (s
->fid_list
) {
543 s
->fid_list
= fidp
->next
;
553 #define P9_QID_TYPE_DIR 0x80
554 #define P9_QID_TYPE_SYMLINK 0x02
556 #define P9_STAT_MODE_DIR 0x80000000
557 #define P9_STAT_MODE_APPEND 0x40000000
558 #define P9_STAT_MODE_EXCL 0x20000000
559 #define P9_STAT_MODE_MOUNT 0x10000000
560 #define P9_STAT_MODE_AUTH 0x08000000
561 #define P9_STAT_MODE_TMP 0x04000000
562 #define P9_STAT_MODE_SYMLINK 0x02000000
563 #define P9_STAT_MODE_LINK 0x01000000
564 #define P9_STAT_MODE_DEVICE 0x00800000
565 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
566 #define P9_STAT_MODE_SOCKET 0x00100000
567 #define P9_STAT_MODE_SETUID 0x00080000
568 #define P9_STAT_MODE_SETGID 0x00040000
569 #define P9_STAT_MODE_SETVTX 0x00010000
571 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
572 P9_STAT_MODE_SYMLINK | \
573 P9_STAT_MODE_LINK | \
574 P9_STAT_MODE_DEVICE | \
575 P9_STAT_MODE_NAMED_PIPE | \
578 /* This is the algorithm from ufs in spfs */
579 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
583 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
584 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
585 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
586 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
588 if (S_ISDIR(stbuf
->st_mode
)) {
589 qidp
->type
|= P9_QID_TYPE_DIR
;
591 if (S_ISLNK(stbuf
->st_mode
)) {
592 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
596 static int coroutine_fn
fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
602 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
606 stat_to_qid(&stbuf
, qidp
);
610 V9fsPDU
*pdu_alloc(V9fsState
*s
)
614 if (!QLIST_EMPTY(&s
->free_list
)) {
615 pdu
= QLIST_FIRST(&s
->free_list
);
616 QLIST_REMOVE(pdu
, next
);
617 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
622 void pdu_free(V9fsPDU
*pdu
)
624 V9fsState
*s
= pdu
->s
;
626 g_assert(!pdu
->cancelled
);
627 QLIST_REMOVE(pdu
, next
);
628 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
632 * We don't do error checking for pdu_marshal/unmarshal here
633 * because we always expect to have enough space to encode
636 static void coroutine_fn
pdu_complete(V9fsPDU
*pdu
, ssize_t len
)
638 int8_t id
= pdu
->id
+ 1; /* Response */
639 V9fsState
*s
= pdu
->s
;
645 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
648 str
.data
= strerror(err
);
649 str
.size
= strlen(str
.data
);
651 len
+= pdu_marshal(pdu
, len
, "s", &str
);
655 len
+= pdu_marshal(pdu
, len
, "d", err
);
657 if (s
->proto_version
== V9FS_PROTO_2000L
) {
660 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
663 /* fill out the header */
664 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
666 /* keep these in sync */
670 pdu_push_and_notify(pdu
);
672 /* Now wakeup anybody waiting in flush for this request */
673 if (!qemu_co_queue_next(&pdu
->complete
)) {
678 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
683 if (mode
& P9_STAT_MODE_DIR
) {
687 if (mode
& P9_STAT_MODE_SYMLINK
) {
690 if (mode
& P9_STAT_MODE_SOCKET
) {
693 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
696 if (mode
& P9_STAT_MODE_DEVICE
) {
697 if (extension
->size
&& extension
->data
[0] == 'c') {
708 if (mode
& P9_STAT_MODE_SETUID
) {
711 if (mode
& P9_STAT_MODE_SETGID
) {
714 if (mode
& P9_STAT_MODE_SETVTX
) {
721 static int donttouch_stat(V9fsStat
*stat
)
723 if (stat
->type
== -1 &&
725 stat
->qid
.type
== -1 &&
726 stat
->qid
.version
== -1 &&
727 stat
->qid
.path
== -1 &&
731 stat
->length
== -1 &&
738 stat
->n_muid
== -1) {
745 static void v9fs_stat_init(V9fsStat
*stat
)
747 v9fs_string_init(&stat
->name
);
748 v9fs_string_init(&stat
->uid
);
749 v9fs_string_init(&stat
->gid
);
750 v9fs_string_init(&stat
->muid
);
751 v9fs_string_init(&stat
->extension
);
754 static void v9fs_stat_free(V9fsStat
*stat
)
756 v9fs_string_free(&stat
->name
);
757 v9fs_string_free(&stat
->uid
);
758 v9fs_string_free(&stat
->gid
);
759 v9fs_string_free(&stat
->muid
);
760 v9fs_string_free(&stat
->extension
);
763 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
767 mode
= stbuf
->st_mode
& 0777;
768 if (S_ISDIR(stbuf
->st_mode
)) {
769 mode
|= P9_STAT_MODE_DIR
;
772 if (S_ISLNK(stbuf
->st_mode
)) {
773 mode
|= P9_STAT_MODE_SYMLINK
;
776 if (S_ISSOCK(stbuf
->st_mode
)) {
777 mode
|= P9_STAT_MODE_SOCKET
;
780 if (S_ISFIFO(stbuf
->st_mode
)) {
781 mode
|= P9_STAT_MODE_NAMED_PIPE
;
784 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
785 mode
|= P9_STAT_MODE_DEVICE
;
788 if (stbuf
->st_mode
& S_ISUID
) {
789 mode
|= P9_STAT_MODE_SETUID
;
792 if (stbuf
->st_mode
& S_ISGID
) {
793 mode
|= P9_STAT_MODE_SETGID
;
796 if (stbuf
->st_mode
& S_ISVTX
) {
797 mode
|= P9_STAT_MODE_SETVTX
;
803 static int coroutine_fn
stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
804 const struct stat
*stbuf
,
810 memset(v9stat
, 0, sizeof(*v9stat
));
812 stat_to_qid(stbuf
, &v9stat
->qid
);
813 v9stat
->mode
= stat_to_v9mode(stbuf
);
814 v9stat
->atime
= stbuf
->st_atime
;
815 v9stat
->mtime
= stbuf
->st_mtime
;
816 v9stat
->length
= stbuf
->st_size
;
818 v9fs_string_free(&v9stat
->uid
);
819 v9fs_string_free(&v9stat
->gid
);
820 v9fs_string_free(&v9stat
->muid
);
822 v9stat
->n_uid
= stbuf
->st_uid
;
823 v9stat
->n_gid
= stbuf
->st_gid
;
826 v9fs_string_free(&v9stat
->extension
);
828 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
829 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
833 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
834 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
835 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
836 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
837 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
838 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
839 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
842 str
= strrchr(name
->data
, '/');
849 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
852 v9fs_string_size(&v9stat
->name
) +
853 v9fs_string_size(&v9stat
->uid
) +
854 v9fs_string_size(&v9stat
->gid
) +
855 v9fs_string_size(&v9stat
->muid
) +
856 v9fs_string_size(&v9stat
->extension
);
860 #define P9_STATS_MODE 0x00000001ULL
861 #define P9_STATS_NLINK 0x00000002ULL
862 #define P9_STATS_UID 0x00000004ULL
863 #define P9_STATS_GID 0x00000008ULL
864 #define P9_STATS_RDEV 0x00000010ULL
865 #define P9_STATS_ATIME 0x00000020ULL
866 #define P9_STATS_MTIME 0x00000040ULL
867 #define P9_STATS_CTIME 0x00000080ULL
868 #define P9_STATS_INO 0x00000100ULL
869 #define P9_STATS_SIZE 0x00000200ULL
870 #define P9_STATS_BLOCKS 0x00000400ULL
872 #define P9_STATS_BTIME 0x00000800ULL
873 #define P9_STATS_GEN 0x00001000ULL
874 #define P9_STATS_DATA_VERSION 0x00002000ULL
876 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
877 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
880 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
881 V9fsStatDotl
*v9lstat
)
883 memset(v9lstat
, 0, sizeof(*v9lstat
));
885 v9lstat
->st_mode
= stbuf
->st_mode
;
886 v9lstat
->st_nlink
= stbuf
->st_nlink
;
887 v9lstat
->st_uid
= stbuf
->st_uid
;
888 v9lstat
->st_gid
= stbuf
->st_gid
;
889 v9lstat
->st_rdev
= stbuf
->st_rdev
;
890 v9lstat
->st_size
= stbuf
->st_size
;
891 v9lstat
->st_blksize
= stbuf
->st_blksize
;
892 v9lstat
->st_blocks
= stbuf
->st_blocks
;
893 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
894 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
895 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
896 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
897 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
898 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
899 /* Currently we only support BASIC fields in stat */
900 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
902 stat_to_qid(stbuf
, &v9lstat
->qid
);
905 static void print_sg(struct iovec
*sg
, int cnt
)
909 printf("sg[%d]: {", cnt
);
910 for (i
= 0; i
< cnt
; i
++) {
914 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
919 /* Will call this only for path name based fid */
920 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
923 v9fs_path_init(&str
);
924 v9fs_path_copy(&str
, dst
);
925 v9fs_path_sprintf(dst
, "%s%s", src
->data
, str
.data
+ len
);
926 v9fs_path_free(&str
);
929 static inline bool is_ro_export(FsContext
*ctx
)
931 return ctx
->export_flags
& V9FS_RDONLY
;
934 static void coroutine_fn
v9fs_version(void *opaque
)
937 V9fsPDU
*pdu
= opaque
;
938 V9fsState
*s
= pdu
->s
;
942 v9fs_string_init(&version
);
943 err
= pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
948 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
952 if (!strcmp(version
.data
, "9P2000.u")) {
953 s
->proto_version
= V9FS_PROTO_2000U
;
954 } else if (!strcmp(version
.data
, "9P2000.L")) {
955 s
->proto_version
= V9FS_PROTO_2000L
;
957 v9fs_string_sprintf(&version
, "unknown");
960 err
= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
966 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
968 pdu_complete(pdu
, offset
);
969 v9fs_string_free(&version
);
972 static void coroutine_fn
v9fs_attach(void *opaque
)
974 V9fsPDU
*pdu
= opaque
;
975 V9fsState
*s
= pdu
->s
;
976 int32_t fid
, afid
, n_uname
;
977 V9fsString uname
, aname
;
982 Error
*local_err
= NULL
;
984 v9fs_string_init(&uname
);
985 v9fs_string_init(&aname
);
986 err
= pdu_unmarshal(pdu
, offset
, "ddssd", &fid
,
987 &afid
, &uname
, &aname
, &n_uname
);
991 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
993 fidp
= alloc_fid(s
, fid
);
999 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
1005 err
= fid_to_qid(pdu
, fidp
, &qid
);
1013 * disable migration if we haven't done already.
1014 * attach could get called multiple times for the same export.
1016 if (!s
->migration_blocker
) {
1017 error_setg(&s
->migration_blocker
,
1018 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'",
1019 s
->ctx
.fs_root
? s
->ctx
.fs_root
: "NULL", s
->tag
);
1020 err
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
1022 error_free(local_err
);
1023 error_free(s
->migration_blocker
);
1024 s
->migration_blocker
= NULL
;
1031 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
1038 memcpy(&s
->root_qid
, &qid
, sizeof(qid
));
1039 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
1040 qid
.type
, qid
.version
, qid
.path
);
1044 pdu_complete(pdu
, err
);
1045 v9fs_string_free(&uname
);
1046 v9fs_string_free(&aname
);
1049 static void coroutine_fn
v9fs_stat(void *opaque
)
1057 V9fsPDU
*pdu
= opaque
;
1059 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1063 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
1065 fidp
= get_fid(pdu
, fid
);
1070 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1074 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
1078 err
= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
1080 v9fs_stat_free(&v9stat
);
1083 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
1084 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
1086 v9fs_stat_free(&v9stat
);
1090 pdu_complete(pdu
, err
);
1093 static void coroutine_fn
v9fs_getattr(void *opaque
)
1100 uint64_t request_mask
;
1101 V9fsStatDotl v9stat_dotl
;
1102 V9fsPDU
*pdu
= opaque
;
1103 V9fsState
*s
= pdu
->s
;
1105 retval
= pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1109 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1111 fidp
= get_fid(pdu
, fid
);
1117 * Currently we only support BASIC fields in stat, so there is no
1118 * need to look at request_mask.
1120 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1124 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1126 /* fill st_gen if requested and supported by underlying fs */
1127 if (request_mask
& P9_STATS_GEN
) {
1128 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1131 /* we have valid st_gen: update result mask */
1132 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1135 /* request cancelled, e.g. by Tflush */
1138 /* failed to get st_gen: not fatal, ignore */
1142 retval
= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1147 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1148 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1149 v9stat_dotl
.st_gid
);
1153 pdu_complete(pdu
, retval
);
1156 /* Attribute flags */
1157 #define P9_ATTR_MODE (1 << 0)
1158 #define P9_ATTR_UID (1 << 1)
1159 #define P9_ATTR_GID (1 << 2)
1160 #define P9_ATTR_SIZE (1 << 3)
1161 #define P9_ATTR_ATIME (1 << 4)
1162 #define P9_ATTR_MTIME (1 << 5)
1163 #define P9_ATTR_CTIME (1 << 6)
1164 #define P9_ATTR_ATIME_SET (1 << 7)
1165 #define P9_ATTR_MTIME_SET (1 << 8)
1167 #define P9_ATTR_MASK 127
1169 static void coroutine_fn
v9fs_setattr(void *opaque
)
1176 V9fsPDU
*pdu
= opaque
;
1178 err
= pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1183 fidp
= get_fid(pdu
, fid
);
1188 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1189 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1194 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1195 struct timespec times
[2];
1196 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1197 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1198 times
[0].tv_sec
= v9iattr
.atime_sec
;
1199 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1201 times
[0].tv_nsec
= UTIME_NOW
;
1204 times
[0].tv_nsec
= UTIME_OMIT
;
1206 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1207 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1208 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1209 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1211 times
[1].tv_nsec
= UTIME_NOW
;
1214 times
[1].tv_nsec
= UTIME_OMIT
;
1216 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1222 * If the only valid entry in iattr is ctime we can call
1223 * chown(-1,-1) to update the ctime of the file
1225 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1226 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1227 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1228 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1231 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1234 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1240 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1241 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1250 pdu_complete(pdu
, err
);
1253 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1259 err
= pdu_marshal(pdu
, offset
, "w", nwnames
);
1264 for (i
= 0; i
< nwnames
; i
++) {
1265 err
= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1274 static bool name_is_illegal(const char *name
)
1276 return !*name
|| strchr(name
, '/') != NULL
;
1279 static bool not_same_qid(const V9fsQID
*qid1
, const V9fsQID
*qid2
)
1282 qid1
->type
!= qid2
->type
||
1283 qid1
->version
!= qid2
->version
||
1284 qid1
->path
!= qid2
->path
;
1287 static void coroutine_fn
v9fs_walk(void *opaque
)
1290 V9fsQID
*qids
= NULL
;
1292 V9fsPath dpath
, path
;
1296 int32_t fid
, newfid
;
1297 V9fsString
*wnames
= NULL
;
1299 V9fsFidState
*newfidp
= NULL
;
1300 V9fsPDU
*pdu
= opaque
;
1301 V9fsState
*s
= pdu
->s
;
1304 err
= pdu_unmarshal(pdu
, offset
, "ddw", &fid
, &newfid
, &nwnames
);
1306 pdu_complete(pdu
, err
);
1311 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1313 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1314 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1315 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1316 for (i
= 0; i
< nwnames
; i
++) {
1317 err
= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1321 if (name_is_illegal(wnames
[i
].data
)) {
1327 } else if (nwnames
> P9_MAXWELEM
) {
1331 fidp
= get_fid(pdu
, fid
);
1337 v9fs_path_init(&dpath
);
1338 v9fs_path_init(&path
);
1340 err
= fid_to_qid(pdu
, fidp
, &qid
);
1346 * Both dpath and path initially poin to fidp.
1347 * Needed to handle request with nwnames == 0
1349 v9fs_path_copy(&dpath
, &fidp
->path
);
1350 v9fs_path_copy(&path
, &fidp
->path
);
1351 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1352 if (not_same_qid(&pdu
->s
->root_qid
, &qid
) ||
1353 strcmp("..", wnames
[name_idx
].data
)) {
1354 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
,
1360 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1364 stat_to_qid(&stbuf
, &qid
);
1365 v9fs_path_copy(&dpath
, &path
);
1367 memcpy(&qids
[name_idx
], &qid
, sizeof(qid
));
1369 if (fid
== newfid
) {
1370 if (fidp
->fid_type
!= P9_FID_NONE
) {
1374 v9fs_path_copy(&fidp
->path
, &path
);
1376 newfidp
= alloc_fid(s
, newfid
);
1377 if (newfidp
== NULL
) {
1381 newfidp
->uid
= fidp
->uid
;
1382 v9fs_path_copy(&newfidp
->path
, &path
);
1384 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1385 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1389 put_fid(pdu
, newfidp
);
1391 v9fs_path_free(&dpath
);
1392 v9fs_path_free(&path
);
1394 pdu_complete(pdu
, err
);
1395 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1396 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1397 v9fs_string_free(&wnames
[name_idx
]);
1404 static int32_t coroutine_fn
get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1406 struct statfs stbuf
;
1408 V9fsState
*s
= pdu
->s
;
1411 * iounit should be multiples of f_bsize (host filesystem block size
1412 * and as well as less than (client msize - P9_IOHDRSZ))
1414 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1415 iounit
= stbuf
.f_bsize
;
1416 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1419 iounit
= s
->msize
- P9_IOHDRSZ
;
1424 static void coroutine_fn
v9fs_open(void *opaque
)
1435 V9fsPDU
*pdu
= opaque
;
1436 V9fsState
*s
= pdu
->s
;
1438 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1439 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1442 err
= pdu_unmarshal(pdu
, offset
, "db", &fid
, &modebyte
);
1448 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1450 fidp
= get_fid(pdu
, fid
);
1455 if (fidp
->fid_type
!= P9_FID_NONE
) {
1460 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1464 stat_to_qid(&stbuf
, &qid
);
1465 if (S_ISDIR(stbuf
.st_mode
)) {
1466 err
= v9fs_co_opendir(pdu
, fidp
);
1470 fidp
->fid_type
= P9_FID_DIR
;
1471 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1477 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1478 flags
= get_dotl_openflags(s
, mode
);
1480 flags
= omode_to_uflags(mode
);
1482 if (is_ro_export(&s
->ctx
)) {
1483 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1484 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1489 err
= v9fs_co_open(pdu
, fidp
, flags
);
1493 fidp
->fid_type
= P9_FID_FILE
;
1494 fidp
->open_flags
= flags
;
1495 if (flags
& O_EXCL
) {
1497 * We let the host file system do O_EXCL check
1498 * We should not reclaim such fd
1500 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1502 iounit
= get_iounit(pdu
, &fidp
->path
);
1503 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1509 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1510 qid
.type
, qid
.version
, qid
.path
, iounit
);
1514 pdu_complete(pdu
, err
);
1517 static void coroutine_fn
v9fs_lcreate(void *opaque
)
1519 int32_t dfid
, flags
, mode
;
1528 V9fsPDU
*pdu
= opaque
;
1530 v9fs_string_init(&name
);
1531 err
= pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
,
1532 &name
, &flags
, &mode
, &gid
);
1536 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1538 if (name_is_illegal(name
.data
)) {
1543 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
1548 fidp
= get_fid(pdu
, dfid
);
1554 flags
= get_dotl_openflags(pdu
->s
, flags
);
1555 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1556 flags
| O_CREAT
, mode
, &stbuf
);
1560 fidp
->fid_type
= P9_FID_FILE
;
1561 fidp
->open_flags
= flags
;
1562 if (flags
& O_EXCL
) {
1564 * We let the host file system do O_EXCL check
1565 * We should not reclaim such fd
1567 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1569 iounit
= get_iounit(pdu
, &fidp
->path
);
1570 stat_to_qid(&stbuf
, &qid
);
1571 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1576 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1577 qid
.type
, qid
.version
, qid
.path
, iounit
);
1581 pdu_complete(pdu
, err
);
1582 v9fs_string_free(&name
);
1585 static void v9fs_fsync(void *opaque
)
1592 V9fsPDU
*pdu
= opaque
;
1594 err
= pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1598 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1600 fidp
= get_fid(pdu
, fid
);
1605 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1611 pdu_complete(pdu
, err
);
1614 static void coroutine_fn
v9fs_clunk(void *opaque
)
1620 V9fsPDU
*pdu
= opaque
;
1621 V9fsState
*s
= pdu
->s
;
1623 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
1627 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1629 fidp
= clunk_fid(s
, fid
);
1635 * Bump the ref so that put_fid will
1639 err
= put_fid(pdu
, fidp
);
1644 pdu_complete(pdu
, err
);
1648 * Create a QEMUIOVector for a sub-region of PDU iovecs
1650 * @qiov: uninitialized QEMUIOVector
1651 * @skip: number of bytes to skip from beginning of PDU
1652 * @size: number of bytes to include
1653 * @is_write: true - write, false - read
1655 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1656 * with qemu_iovec_destroy().
1658 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1659 size_t skip
, size_t size
,
1667 pdu
->s
->transport
->init_out_iov_from_pdu(pdu
, &iov
, &niov
);
1669 pdu
->s
->transport
->init_in_iov_from_pdu(pdu
, &iov
, &niov
, size
);
1672 qemu_iovec_init_external(&elem
, iov
, niov
);
1673 qemu_iovec_init(qiov
, niov
);
1674 qemu_iovec_concat(qiov
, &elem
, skip
, size
);
1677 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1678 uint64_t off
, uint32_t max_count
)
1682 uint64_t read_count
;
1683 QEMUIOVector qiov_full
;
1685 if (fidp
->fs
.xattr
.len
< off
) {
1688 read_count
= fidp
->fs
.xattr
.len
- off
;
1690 if (read_count
> max_count
) {
1691 read_count
= max_count
;
1693 err
= pdu_marshal(pdu
, offset
, "d", read_count
);
1699 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, 0, read_count
, false);
1700 err
= v9fs_pack(qiov_full
.iov
, qiov_full
.niov
, offset
,
1701 ((char *)fidp
->fs
.xattr
.value
) + off
,
1703 qemu_iovec_destroy(&qiov_full
);
1711 static int coroutine_fn
v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1720 off_t saved_dir_pos
;
1721 struct dirent
*dent
;
1723 /* save the directory position */
1724 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1725 if (saved_dir_pos
< 0) {
1726 return saved_dir_pos
;
1730 v9fs_path_init(&path
);
1732 v9fs_readdir_lock(&fidp
->fs
.dir
);
1734 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1738 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1742 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1746 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1750 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1751 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1753 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1755 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1756 /* Ran out of buffer. Set dir back to old position and return */
1757 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1758 v9fs_stat_free(&v9stat
);
1759 v9fs_path_free(&path
);
1763 v9fs_stat_free(&v9stat
);
1764 v9fs_path_free(&path
);
1765 saved_dir_pos
= dent
->d_off
;
1768 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1770 v9fs_path_free(&path
);
1777 static void coroutine_fn
v9fs_read(void *opaque
)
1786 V9fsPDU
*pdu
= opaque
;
1787 V9fsState
*s
= pdu
->s
;
1789 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1793 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1795 fidp
= get_fid(pdu
, fid
);
1800 if (fidp
->fid_type
== P9_FID_DIR
) {
1803 v9fs_co_rewinddir(pdu
, fidp
);
1805 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1810 err
= pdu_marshal(pdu
, offset
, "d", count
);
1814 err
+= offset
+ count
;
1815 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1816 QEMUIOVector qiov_full
;
1820 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1821 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1823 qemu_iovec_reset(&qiov
);
1824 qemu_iovec_concat(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1826 print_sg(qiov
.iov
, qiov
.niov
);
1828 /* Loop in case of EINTR */
1830 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1835 } while (len
== -EINTR
&& !pdu
->cancelled
);
1837 /* IO error return the error */
1839 goto out_free_iovec
;
1841 } while (count
< max_count
&& len
> 0);
1842 err
= pdu_marshal(pdu
, offset
, "d", count
);
1844 goto out_free_iovec
;
1846 err
+= offset
+ count
;
1848 qemu_iovec_destroy(&qiov
);
1849 qemu_iovec_destroy(&qiov_full
);
1850 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1851 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1855 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1859 pdu_complete(pdu
, err
);
1862 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1865 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1866 * size of type (1) + size of name.size (2) + strlen(name.data)
1868 return 24 + v9fs_string_size(name
);
1871 static int coroutine_fn
v9fs_do_readdir(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1879 off_t saved_dir_pos
;
1880 struct dirent
*dent
;
1882 /* save the directory position */
1883 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1884 if (saved_dir_pos
< 0) {
1885 return saved_dir_pos
;
1889 v9fs_readdir_lock(&fidp
->fs
.dir
);
1891 err
= v9fs_co_readdir(pdu
, fidp
, &dent
);
1895 v9fs_string_init(&name
);
1896 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1897 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1898 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1900 /* Ran out of buffer. Set dir back to old position and return */
1901 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1902 v9fs_string_free(&name
);
1906 * Fill up just the path field of qid because the client uses
1907 * only that. To fill the entire qid structure we will have
1908 * to stat each dirent found, which is expensive
1910 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1911 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1912 /* Fill the other fields with dummy values */
1916 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1917 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1919 dent
->d_type
, &name
);
1921 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1924 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1925 v9fs_string_free(&name
);
1929 v9fs_string_free(&name
);
1930 saved_dir_pos
= dent
->d_off
;
1933 v9fs_readdir_unlock(&fidp
->fs
.dir
);
1941 static void coroutine_fn
v9fs_readdir(void *opaque
)
1947 uint64_t initial_offset
;
1950 V9fsPDU
*pdu
= opaque
;
1952 retval
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
,
1953 &initial_offset
, &max_count
);
1957 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1959 fidp
= get_fid(pdu
, fid
);
1964 if (!fidp
->fs
.dir
.stream
) {
1968 if (initial_offset
== 0) {
1969 v9fs_co_rewinddir(pdu
, fidp
);
1971 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1973 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1978 retval
= pdu_marshal(pdu
, offset
, "d", count
);
1982 retval
+= count
+ offset
;
1983 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1987 pdu_complete(pdu
, retval
);
1990 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1991 uint64_t off
, uint32_t count
,
1992 struct iovec
*sg
, int cnt
)
1996 uint64_t write_count
;
2000 if (fidp
->fs
.xattr
.len
< off
) {
2004 write_count
= fidp
->fs
.xattr
.len
- off
;
2005 if (write_count
> count
) {
2006 write_count
= count
;
2008 err
= pdu_marshal(pdu
, offset
, "d", write_count
);
2013 fidp
->fs
.xattr
.copied_len
+= write_count
;
2015 * Now copy the content from sg list
2017 for (i
= 0; i
< cnt
; i
++) {
2018 if (write_count
> sg
[i
].iov_len
) {
2019 to_copy
= sg
[i
].iov_len
;
2021 to_copy
= write_count
;
2023 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
2024 /* updating vs->off since we are not using below */
2026 write_count
-= to_copy
;
2032 static void coroutine_fn
v9fs_write(void *opaque
)
2042 V9fsPDU
*pdu
= opaque
;
2043 V9fsState
*s
= pdu
->s
;
2044 QEMUIOVector qiov_full
;
2047 err
= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
2049 pdu_complete(pdu
, err
);
2053 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
2054 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
2056 fidp
= get_fid(pdu
, fid
);
2061 if (fidp
->fid_type
== P9_FID_FILE
) {
2062 if (fidp
->fs
.fd
== -1) {
2066 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
2068 * setxattr operation
2070 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
2071 qiov_full
.iov
, qiov_full
.niov
);
2077 qemu_iovec_init(&qiov
, qiov_full
.niov
);
2079 qemu_iovec_reset(&qiov
);
2080 qemu_iovec_concat(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
2082 print_sg(qiov
.iov
, qiov
.niov
);
2084 /* Loop in case of EINTR */
2086 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
2091 } while (len
== -EINTR
&& !pdu
->cancelled
);
2093 /* IO error return the error */
2097 } while (total
< count
&& len
> 0);
2100 err
= pdu_marshal(pdu
, offset
, "d", total
);
2105 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
2107 qemu_iovec_destroy(&qiov
);
2111 qemu_iovec_destroy(&qiov_full
);
2112 pdu_complete(pdu
, err
);
2115 static void coroutine_fn
v9fs_create(void *opaque
)
2127 V9fsString extension
;
2129 V9fsPDU
*pdu
= opaque
;
2131 v9fs_path_init(&path
);
2132 v9fs_string_init(&name
);
2133 v9fs_string_init(&extension
);
2134 err
= pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
2135 &perm
, &mode
, &extension
);
2139 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
2141 if (name_is_illegal(name
.data
)) {
2146 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2151 fidp
= get_fid(pdu
, fid
);
2156 if (perm
& P9_STAT_MODE_DIR
) {
2157 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
2158 fidp
->uid
, -1, &stbuf
);
2162 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2166 v9fs_path_copy(&fidp
->path
, &path
);
2167 err
= v9fs_co_opendir(pdu
, fidp
);
2171 fidp
->fid_type
= P9_FID_DIR
;
2172 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
2173 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
2174 extension
.data
, -1 , &stbuf
);
2178 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2182 v9fs_path_copy(&fidp
->path
, &path
);
2183 } else if (perm
& P9_STAT_MODE_LINK
) {
2184 int32_t ofid
= atoi(extension
.data
);
2185 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
2186 if (ofidp
== NULL
) {
2190 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
2191 put_fid(pdu
, ofidp
);
2195 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2197 fidp
->fid_type
= P9_FID_NONE
;
2200 v9fs_path_copy(&fidp
->path
, &path
);
2201 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2203 fidp
->fid_type
= P9_FID_NONE
;
2206 } else if (perm
& P9_STAT_MODE_DEVICE
) {
2208 uint32_t major
, minor
;
2211 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
2228 nmode
|= perm
& 0777;
2229 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2230 makedev(major
, minor
), nmode
, &stbuf
);
2234 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2238 v9fs_path_copy(&fidp
->path
, &path
);
2239 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
2240 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2241 0, S_IFIFO
| (perm
& 0777), &stbuf
);
2245 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2249 v9fs_path_copy(&fidp
->path
, &path
);
2250 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2251 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2252 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2256 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2260 v9fs_path_copy(&fidp
->path
, &path
);
2262 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2263 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2267 fidp
->fid_type
= P9_FID_FILE
;
2268 fidp
->open_flags
= omode_to_uflags(mode
);
2269 if (fidp
->open_flags
& O_EXCL
) {
2271 * We let the host file system do O_EXCL check
2272 * We should not reclaim such fd
2274 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2277 iounit
= get_iounit(pdu
, &fidp
->path
);
2278 stat_to_qid(&stbuf
, &qid
);
2279 err
= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2284 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2285 qid
.type
, qid
.version
, qid
.path
, iounit
);
2289 pdu_complete(pdu
, err
);
2290 v9fs_string_free(&name
);
2291 v9fs_string_free(&extension
);
2292 v9fs_path_free(&path
);
2295 static void coroutine_fn
v9fs_symlink(void *opaque
)
2297 V9fsPDU
*pdu
= opaque
;
2300 V9fsFidState
*dfidp
;
2308 v9fs_string_init(&name
);
2309 v9fs_string_init(&symname
);
2310 err
= pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2314 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2316 if (name_is_illegal(name
.data
)) {
2321 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2326 dfidp
= get_fid(pdu
, dfid
);
2327 if (dfidp
== NULL
) {
2331 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2335 stat_to_qid(&stbuf
, &qid
);
2336 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2341 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2342 qid
.type
, qid
.version
, qid
.path
);
2344 put_fid(pdu
, dfidp
);
2346 pdu_complete(pdu
, err
);
2347 v9fs_string_free(&name
);
2348 v9fs_string_free(&symname
);
2351 static void v9fs_flush(void *opaque
)
2356 V9fsPDU
*cancel_pdu
;
2357 V9fsPDU
*pdu
= opaque
;
2358 V9fsState
*s
= pdu
->s
;
2360 err
= pdu_unmarshal(pdu
, offset
, "w", &tag
);
2362 pdu_complete(pdu
, err
);
2365 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2367 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2368 if (cancel_pdu
->tag
== tag
) {
2373 cancel_pdu
->cancelled
= 1;
2375 * Wait for pdu to complete.
2377 qemu_co_queue_wait(&cancel_pdu
->complete
);
2378 cancel_pdu
->cancelled
= 0;
2379 pdu_free(cancel_pdu
);
2381 pdu_complete(pdu
, 7);
2384 static void coroutine_fn
v9fs_link(void *opaque
)
2386 V9fsPDU
*pdu
= opaque
;
2387 int32_t dfid
, oldfid
;
2388 V9fsFidState
*dfidp
, *oldfidp
;
2393 v9fs_string_init(&name
);
2394 err
= pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2398 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2400 if (name_is_illegal(name
.data
)) {
2405 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2410 dfidp
= get_fid(pdu
, dfid
);
2411 if (dfidp
== NULL
) {
2416 oldfidp
= get_fid(pdu
, oldfid
);
2417 if (oldfidp
== NULL
) {
2421 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2425 put_fid(pdu
, oldfidp
);
2427 put_fid(pdu
, dfidp
);
2429 v9fs_string_free(&name
);
2430 pdu_complete(pdu
, err
);
2433 /* Only works with path name based fid */
2434 static void coroutine_fn
v9fs_remove(void *opaque
)
2440 V9fsPDU
*pdu
= opaque
;
2442 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2446 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2448 fidp
= get_fid(pdu
, fid
);
2453 /* if fs driver is not path based, return EOPNOTSUPP */
2454 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2459 * IF the file is unlinked, we cannot reopen
2460 * the file later. So don't reclaim fd
2462 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2466 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2471 /* For TREMOVE we need to clunk the fid even on failed remove */
2472 clunk_fid(pdu
->s
, fidp
->fid
);
2475 pdu_complete(pdu
, err
);
2478 static void coroutine_fn
v9fs_unlinkat(void *opaque
)
2482 int32_t dfid
, flags
;
2485 V9fsFidState
*dfidp
;
2486 V9fsPDU
*pdu
= opaque
;
2488 v9fs_string_init(&name
);
2489 err
= pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2494 if (name_is_illegal(name
.data
)) {
2499 if (!strcmp(".", name
.data
)) {
2504 if (!strcmp("..", name
.data
)) {
2509 dfidp
= get_fid(pdu
, dfid
);
2510 if (dfidp
== NULL
) {
2515 * IF the file is unlinked, we cannot reopen
2516 * the file later. So don't reclaim fd
2518 v9fs_path_init(&path
);
2519 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2523 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2527 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2532 put_fid(pdu
, dfidp
);
2533 v9fs_path_free(&path
);
2535 pdu_complete(pdu
, err
);
2536 v9fs_string_free(&name
);
2540 /* Only works with path name based fid */
2541 static int coroutine_fn
v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2548 V9fsFidState
*tfidp
;
2549 V9fsState
*s
= pdu
->s
;
2550 V9fsFidState
*dirfidp
= NULL
;
2551 char *old_name
, *new_name
;
2553 v9fs_path_init(&new_path
);
2554 if (newdirfid
!= -1) {
2555 dirfidp
= get_fid(pdu
, newdirfid
);
2556 if (dirfidp
== NULL
) {
2560 if (fidp
->fid_type
!= P9_FID_NONE
) {
2564 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2566 old_name
= fidp
->path
.data
;
2567 end
= strrchr(old_name
, '/');
2573 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2574 strncat(new_name
, old_name
, end
- old_name
);
2575 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2576 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2579 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2584 * Fixup fid's pointing to the old name to
2585 * start pointing to the new name
2587 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2588 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2589 /* replace the name */
2590 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2595 put_fid(pdu
, dirfidp
);
2597 v9fs_path_free(&new_path
);
2602 /* Only works with path name based fid */
2603 static void coroutine_fn
v9fs_rename(void *opaque
)
2611 V9fsPDU
*pdu
= opaque
;
2612 V9fsState
*s
= pdu
->s
;
2614 v9fs_string_init(&name
);
2615 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2620 if (name_is_illegal(name
.data
)) {
2625 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2630 fidp
= get_fid(pdu
, fid
);
2635 if (fidp
->fid_type
!= P9_FID_NONE
) {
2639 /* if fs driver is not path based, return EOPNOTSUPP */
2640 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2644 v9fs_path_write_lock(s
);
2645 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2646 v9fs_path_unlock(s
);
2653 pdu_complete(pdu
, err
);
2654 v9fs_string_free(&name
);
2657 static void coroutine_fn
v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2658 V9fsString
*old_name
,
2660 V9fsString
*new_name
)
2662 V9fsFidState
*tfidp
;
2663 V9fsPath oldpath
, newpath
;
2664 V9fsState
*s
= pdu
->s
;
2667 v9fs_path_init(&oldpath
);
2668 v9fs_path_init(&newpath
);
2669 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2670 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2673 * Fixup fid's pointing to the old name to
2674 * start pointing to the new name
2676 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2677 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2678 /* replace the name */
2679 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2682 v9fs_path_free(&oldpath
);
2683 v9fs_path_free(&newpath
);
2686 static int coroutine_fn
v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2687 V9fsString
*old_name
,
2689 V9fsString
*new_name
)
2692 V9fsState
*s
= pdu
->s
;
2693 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2695 olddirfidp
= get_fid(pdu
, olddirfid
);
2696 if (olddirfidp
== NULL
) {
2700 if (newdirfid
!= -1) {
2701 newdirfidp
= get_fid(pdu
, newdirfid
);
2702 if (newdirfidp
== NULL
) {
2707 newdirfidp
= get_fid(pdu
, olddirfid
);
2710 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2711 &newdirfidp
->path
, new_name
);
2715 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2716 /* Only for path based fid we need to do the below fixup */
2717 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2718 &newdirfidp
->path
, new_name
);
2722 put_fid(pdu
, olddirfidp
);
2725 put_fid(pdu
, newdirfidp
);
2730 static void coroutine_fn
v9fs_renameat(void *opaque
)
2734 V9fsPDU
*pdu
= opaque
;
2735 V9fsState
*s
= pdu
->s
;
2736 int32_t olddirfid
, newdirfid
;
2737 V9fsString old_name
, new_name
;
2739 v9fs_string_init(&old_name
);
2740 v9fs_string_init(&new_name
);
2741 err
= pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2742 &old_name
, &newdirfid
, &new_name
);
2747 if (name_is_illegal(old_name
.data
) || name_is_illegal(new_name
.data
)) {
2752 if (!strcmp(".", old_name
.data
) || !strcmp("..", old_name
.data
) ||
2753 !strcmp(".", new_name
.data
) || !strcmp("..", new_name
.data
)) {
2758 v9fs_path_write_lock(s
);
2759 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2760 &old_name
, newdirfid
, &new_name
);
2761 v9fs_path_unlock(s
);
2767 pdu_complete(pdu
, err
);
2768 v9fs_string_free(&old_name
);
2769 v9fs_string_free(&new_name
);
2772 static void coroutine_fn
v9fs_wstat(void *opaque
)
2781 V9fsPDU
*pdu
= opaque
;
2783 v9fs_stat_init(&v9stat
);
2784 err
= pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2788 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2789 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2791 fidp
= get_fid(pdu
, fid
);
2796 /* do we need to sync the file? */
2797 if (donttouch_stat(&v9stat
)) {
2798 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2801 if (v9stat
.mode
!= -1) {
2803 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2807 v9_mode
= stat_to_v9mode(&stbuf
);
2808 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2809 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2810 /* Attempting to change the type */
2814 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2815 v9mode_to_mode(v9stat
.mode
,
2816 &v9stat
.extension
));
2821 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2822 struct timespec times
[2];
2823 if (v9stat
.atime
!= -1) {
2824 times
[0].tv_sec
= v9stat
.atime
;
2825 times
[0].tv_nsec
= 0;
2827 times
[0].tv_nsec
= UTIME_OMIT
;
2829 if (v9stat
.mtime
!= -1) {
2830 times
[1].tv_sec
= v9stat
.mtime
;
2831 times
[1].tv_nsec
= 0;
2833 times
[1].tv_nsec
= UTIME_OMIT
;
2835 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2840 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2841 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2846 if (v9stat
.name
.size
!= 0) {
2847 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2852 if (v9stat
.length
!= -1) {
2853 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2862 v9fs_stat_free(&v9stat
);
2863 pdu_complete(pdu
, err
);
2866 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2878 int32_t bsize_factor
;
2881 * compute bsize factor based on host file system block size
2884 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2885 if (!bsize_factor
) {
2888 f_type
= stbuf
->f_type
;
2889 f_bsize
= stbuf
->f_bsize
;
2890 f_bsize
*= bsize_factor
;
2892 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2893 * adjust(divide) the number of blocks, free blocks and available
2894 * blocks by bsize factor
2896 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2897 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2898 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2899 f_files
= stbuf
->f_files
;
2900 f_ffree
= stbuf
->f_ffree
;
2901 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2902 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2903 f_namelen
= stbuf
->f_namelen
;
2905 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2906 f_type
, f_bsize
, f_blocks
, f_bfree
,
2907 f_bavail
, f_files
, f_ffree
,
2908 fsid_val
, f_namelen
);
2911 static void coroutine_fn
v9fs_statfs(void *opaque
)
2917 struct statfs stbuf
;
2918 V9fsPDU
*pdu
= opaque
;
2919 V9fsState
*s
= pdu
->s
;
2921 retval
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
2925 fidp
= get_fid(pdu
, fid
);
2930 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2934 retval
= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2942 pdu_complete(pdu
, retval
);
2945 static void coroutine_fn
v9fs_mknod(void *opaque
)
2958 V9fsPDU
*pdu
= opaque
;
2960 v9fs_string_init(&name
);
2961 err
= pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2962 &major
, &minor
, &gid
);
2966 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2968 if (name_is_illegal(name
.data
)) {
2973 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
2978 fidp
= get_fid(pdu
, fid
);
2983 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2984 makedev(major
, minor
), mode
, &stbuf
);
2988 stat_to_qid(&stbuf
, &qid
);
2989 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
2994 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2995 qid
.type
, qid
.version
, qid
.path
);
2999 pdu_complete(pdu
, err
);
3000 v9fs_string_free(&name
);
3004 * Implement posix byte range locking code
3005 * Server side handling of locking code is very simple, because 9p server in
3006 * QEMU can handle only one client. And most of the lock handling
3007 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
3008 * do any thing in * qemu 9p server side lock code path.
3009 * So when a TLOCK request comes, always return success
3011 static void coroutine_fn
v9fs_lock(void *opaque
)
3018 int32_t fid
, err
= 0;
3019 V9fsPDU
*pdu
= opaque
;
3021 status
= P9_LOCK_ERROR
;
3022 v9fs_string_init(&flock
.client_id
);
3023 err
= pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
.type
,
3024 &flock
.flags
, &flock
.start
, &flock
.length
,
3025 &flock
.proc_id
, &flock
.client_id
);
3029 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
3030 flock
.type
, flock
.start
, flock
.length
);
3033 /* We support only block flag now (that too ignored currently) */
3034 if (flock
.flags
& ~P9_LOCK_FLAGS_BLOCK
) {
3038 fidp
= get_fid(pdu
, fid
);
3043 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3047 status
= P9_LOCK_SUCCESS
;
3051 err
= pdu_marshal(pdu
, offset
, "b", status
);
3055 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
3056 pdu_complete(pdu
, err
);
3057 v9fs_string_free(&flock
.client_id
);
3061 * When a TGETLOCK request comes, always return success because all lock
3062 * handling is done by client's VFS layer.
3064 static void coroutine_fn
v9fs_getlock(void *opaque
)
3070 int32_t fid
, err
= 0;
3071 V9fsPDU
*pdu
= opaque
;
3073 v9fs_string_init(&glock
.client_id
);
3074 err
= pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
.type
,
3075 &glock
.start
, &glock
.length
, &glock
.proc_id
,
3080 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
3081 glock
.type
, glock
.start
, glock
.length
);
3083 fidp
= get_fid(pdu
, fid
);
3088 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
3092 glock
.type
= P9_LOCK_TYPE_UNLCK
;
3093 err
= pdu_marshal(pdu
, offset
, "bqqds", glock
.type
,
3094 glock
.start
, glock
.length
, glock
.proc_id
,
3100 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
.type
, glock
.start
,
3101 glock
.length
, glock
.proc_id
);
3105 pdu_complete(pdu
, err
);
3106 v9fs_string_free(&glock
.client_id
);
3109 static void coroutine_fn
v9fs_mkdir(void *opaque
)
3111 V9fsPDU
*pdu
= opaque
;
3122 v9fs_string_init(&name
);
3123 err
= pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
3127 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
3129 if (name_is_illegal(name
.data
)) {
3134 if (!strcmp(".", name
.data
) || !strcmp("..", name
.data
)) {
3139 fidp
= get_fid(pdu
, fid
);
3144 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
3148 stat_to_qid(&stbuf
, &qid
);
3149 err
= pdu_marshal(pdu
, offset
, "Q", &qid
);
3154 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
3155 qid
.type
, qid
.version
, qid
.path
, err
);
3159 pdu_complete(pdu
, err
);
3160 v9fs_string_free(&name
);
3163 static void coroutine_fn
v9fs_xattrwalk(void *opaque
)
3169 int32_t fid
, newfid
;
3170 V9fsFidState
*file_fidp
;
3171 V9fsFidState
*xattr_fidp
= NULL
;
3172 V9fsPDU
*pdu
= opaque
;
3173 V9fsState
*s
= pdu
->s
;
3175 v9fs_string_init(&name
);
3176 err
= pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
3180 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
3182 file_fidp
= get_fid(pdu
, fid
);
3183 if (file_fidp
== NULL
) {
3187 xattr_fidp
= alloc_fid(s
, newfid
);
3188 if (xattr_fidp
== NULL
) {
3192 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
3193 if (!v9fs_string_size(&name
)) {
3195 * listxattr request. Get the size first
3197 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
3200 clunk_fid(s
, xattr_fidp
->fid
);
3204 * Read the xattr value
3206 xattr_fidp
->fs
.xattr
.len
= size
;
3207 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3208 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3210 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3211 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
3212 xattr_fidp
->fs
.xattr
.value
,
3213 xattr_fidp
->fs
.xattr
.len
);
3215 clunk_fid(s
, xattr_fidp
->fid
);
3219 err
= pdu_marshal(pdu
, offset
, "q", size
);
3226 * specific xattr fid. We check for xattr
3227 * presence also collect the xattr size
3229 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3233 clunk_fid(s
, xattr_fidp
->fid
);
3237 * Read the xattr value
3239 xattr_fidp
->fs
.xattr
.len
= size
;
3240 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3241 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= true;
3243 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
3244 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
3245 &name
, xattr_fidp
->fs
.xattr
.value
,
3246 xattr_fidp
->fs
.xattr
.len
);
3248 clunk_fid(s
, xattr_fidp
->fid
);
3252 err
= pdu_marshal(pdu
, offset
, "q", size
);
3258 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
3260 put_fid(pdu
, file_fidp
);
3262 put_fid(pdu
, xattr_fidp
);
3265 pdu_complete(pdu
, err
);
3266 v9fs_string_free(&name
);
3269 static void coroutine_fn
v9fs_xattrcreate(void *opaque
)
3277 V9fsFidState
*file_fidp
;
3278 V9fsFidState
*xattr_fidp
;
3279 V9fsPDU
*pdu
= opaque
;
3281 v9fs_string_init(&name
);
3282 err
= pdu_unmarshal(pdu
, offset
, "dsqd", &fid
, &name
, &size
, &flags
);
3286 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
3288 if (size
> XATTR_SIZE_MAX
) {
3293 file_fidp
= get_fid(pdu
, fid
);
3294 if (file_fidp
== NULL
) {
3298 if (file_fidp
->fid_type
!= P9_FID_NONE
) {
3303 /* Make the file fid point to xattr */
3304 xattr_fidp
= file_fidp
;
3305 xattr_fidp
->fid_type
= P9_FID_XATTR
;
3306 xattr_fidp
->fs
.xattr
.copied_len
= 0;
3307 xattr_fidp
->fs
.xattr
.xattrwalk_fid
= false;
3308 xattr_fidp
->fs
.xattr
.len
= size
;
3309 xattr_fidp
->fs
.xattr
.flags
= flags
;
3310 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
3311 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
3312 xattr_fidp
->fs
.xattr
.value
= g_malloc0(size
);
3315 put_fid(pdu
, file_fidp
);
3317 pdu_complete(pdu
, err
);
3318 v9fs_string_free(&name
);
3321 static void coroutine_fn
v9fs_readlink(void *opaque
)
3323 V9fsPDU
*pdu
= opaque
;
3330 err
= pdu_unmarshal(pdu
, offset
, "d", &fid
);
3334 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
3335 fidp
= get_fid(pdu
, fid
);
3341 v9fs_string_init(&target
);
3342 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
3346 err
= pdu_marshal(pdu
, offset
, "s", &target
);
3348 v9fs_string_free(&target
);
3352 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
3353 v9fs_string_free(&target
);
3357 pdu_complete(pdu
, err
);
3360 static CoroutineEntry
*pdu_co_handlers
[] = {
3361 [P9_TREADDIR
] = v9fs_readdir
,
3362 [P9_TSTATFS
] = v9fs_statfs
,
3363 [P9_TGETATTR
] = v9fs_getattr
,
3364 [P9_TSETATTR
] = v9fs_setattr
,
3365 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
3366 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
3367 [P9_TMKNOD
] = v9fs_mknod
,
3368 [P9_TRENAME
] = v9fs_rename
,
3369 [P9_TLOCK
] = v9fs_lock
,
3370 [P9_TGETLOCK
] = v9fs_getlock
,
3371 [P9_TRENAMEAT
] = v9fs_renameat
,
3372 [P9_TREADLINK
] = v9fs_readlink
,
3373 [P9_TUNLINKAT
] = v9fs_unlinkat
,
3374 [P9_TMKDIR
] = v9fs_mkdir
,
3375 [P9_TVERSION
] = v9fs_version
,
3376 [P9_TLOPEN
] = v9fs_open
,
3377 [P9_TATTACH
] = v9fs_attach
,
3378 [P9_TSTAT
] = v9fs_stat
,
3379 [P9_TWALK
] = v9fs_walk
,
3380 [P9_TCLUNK
] = v9fs_clunk
,
3381 [P9_TFSYNC
] = v9fs_fsync
,
3382 [P9_TOPEN
] = v9fs_open
,
3383 [P9_TREAD
] = v9fs_read
,
3385 [P9_TAUTH
] = v9fs_auth
,
3387 [P9_TFLUSH
] = v9fs_flush
,
3388 [P9_TLINK
] = v9fs_link
,
3389 [P9_TSYMLINK
] = v9fs_symlink
,
3390 [P9_TCREATE
] = v9fs_create
,
3391 [P9_TLCREATE
] = v9fs_lcreate
,
3392 [P9_TWRITE
] = v9fs_write
,
3393 [P9_TWSTAT
] = v9fs_wstat
,
3394 [P9_TREMOVE
] = v9fs_remove
,
3397 static void coroutine_fn
v9fs_op_not_supp(void *opaque
)
3399 V9fsPDU
*pdu
= opaque
;
3400 pdu_complete(pdu
, -EOPNOTSUPP
);
3403 static void coroutine_fn
v9fs_fs_ro(void *opaque
)
3405 V9fsPDU
*pdu
= opaque
;
3406 pdu_complete(pdu
, -EROFS
);
3409 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3436 void pdu_submit(V9fsPDU
*pdu
)
3439 CoroutineEntry
*handler
;
3440 V9fsState
*s
= pdu
->s
;
3442 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3443 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3444 handler
= v9fs_op_not_supp
;
3446 handler
= pdu_co_handlers
[pdu
->id
];
3449 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3450 handler
= v9fs_fs_ro
;
3452 co
= qemu_coroutine_create(handler
, pdu
);
3453 qemu_coroutine_enter(co
);
3456 /* Returns 0 on success, 1 on failure. */
3457 int v9fs_device_realize_common(V9fsState
*s
, Error
**errp
)
3465 /* initialize pdu allocator */
3466 QLIST_INIT(&s
->free_list
);
3467 QLIST_INIT(&s
->active_list
);
3468 for (i
= 0; i
< (MAX_REQ
- 1); i
++) {
3469 QLIST_INSERT_HEAD(&s
->free_list
, &s
->pdus
[i
], next
);
3474 v9fs_path_init(&path
);
3476 fse
= get_fsdev_fsentry(s
->fsconf
.fsdev_id
);
3479 /* We don't have a fsdev identified by fsdev_id */
3480 error_setg(errp
, "9pfs device couldn't find fsdev with the "
3482 s
->fsconf
.fsdev_id
? s
->fsconf
.fsdev_id
: "NULL");
3486 if (!s
->fsconf
.tag
) {
3487 /* we haven't specified a mount_tag */
3488 error_setg(errp
, "fsdev with id %s needs mount_tag arguments",
3489 s
->fsconf
.fsdev_id
);
3493 s
->ctx
.export_flags
= fse
->export_flags
;
3494 s
->ctx
.fs_root
= g_strdup(fse
->path
);
3495 s
->ctx
.exops
.get_st_gen
= NULL
;
3496 len
= strlen(s
->fsconf
.tag
);
3497 if (len
> MAX_TAG_LEN
- 1) {
3498 error_setg(errp
, "mount tag '%s' (%d bytes) is longer than "
3499 "maximum (%d bytes)", s
->fsconf
.tag
, len
, MAX_TAG_LEN
- 1);
3503 s
->tag
= g_strdup(s
->fsconf
.tag
);
3509 qemu_co_rwlock_init(&s
->rename_lock
);
3511 if (s
->ops
->init(&s
->ctx
) < 0) {
3512 error_setg(errp
, "9pfs Failed to initialize fs-driver with id:%s"
3513 " and export path:%s", s
->fsconf
.fsdev_id
, s
->ctx
.fs_root
);
3518 * Check details of export path, We need to use fs driver
3519 * call back to do that. Since we are in the init path, we don't
3520 * use co-routines here.
3522 if (s
->ops
->name_to_path(&s
->ctx
, NULL
, "/", &path
) < 0) {
3524 "error in converting name to path %s", strerror(errno
));
3527 if (s
->ops
->lstat(&s
->ctx
, &path
, &stat
)) {
3528 error_setg(errp
, "share path %s does not exist", fse
->path
);
3530 } else if (!S_ISDIR(stat
.st_mode
)) {
3531 error_setg(errp
, "share path %s is not a directory", fse
->path
);
3534 v9fs_path_free(&path
);
3539 if (s
->ops
&& s
->ops
->cleanup
&& s
->ctx
.private) {
3540 s
->ops
->cleanup(&s
->ctx
);
3543 g_free(s
->ctx
.fs_root
);
3544 v9fs_path_free(&path
);
3549 void v9fs_device_unrealize_common(V9fsState
*s
, Error
**errp
)
3551 if (s
->ops
->cleanup
) {
3552 s
->ops
->cleanup(&s
->ctx
);
3555 g_free(s
->ctx
.fs_root
);
3558 typedef struct VirtfsCoResetData
{
3561 } VirtfsCoResetData
;
3563 static void coroutine_fn
virtfs_co_reset(void *opaque
)
3565 VirtfsCoResetData
*data
= opaque
;
3567 virtfs_reset(&data
->pdu
);
3571 void v9fs_reset(V9fsState
*s
)
3573 VirtfsCoResetData data
= { .pdu
= { .s
= s
}, .done
= false };
3576 while (!QLIST_EMPTY(&s
->active_list
)) {
3577 aio_poll(qemu_get_aio_context(), true);
3580 co
= qemu_coroutine_create(virtfs_co_reset
, &data
);
3581 qemu_coroutine_enter(co
);
3583 while (!data
.done
) {
3584 aio_poll(qemu_get_aio_context(), true);
3588 static void __attribute__((__constructor__
)) v9fs_set_fd_limit(void)
3591 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3592 error_report("Failed to get the resource limit");
3595 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3596 open_fd_rc
= rlim
.rlim_cur
/2;