4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "hw/virtio.h"
16 #include "qemu_socket.h"
17 #include "hw/virtio-pci.h"
18 #include "virtio-9p.h"
19 #include "fsdev/qemu-fsdev.h"
20 #include "virtio-9p-xattr.h"
21 #include "virtio-9p-coth.h"
23 #include "migration.h"
27 static int open_fd_rc
;
41 static int omode_to_uflags(int8_t mode
)
75 struct dotl_openflag_map
{
80 static int dotl_to_open_flags(int flags
)
84 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY
85 * and P9_DOTL_NOACCESS
87 int oflags
= flags
& O_ACCMODE
;
89 struct dotl_openflag_map dotl_oflag_map
[] = {
90 { P9_DOTL_CREATE
, O_CREAT
},
91 { P9_DOTL_EXCL
, O_EXCL
},
92 { P9_DOTL_NOCTTY
, O_NOCTTY
},
93 { P9_DOTL_TRUNC
, O_TRUNC
},
94 { P9_DOTL_APPEND
, O_APPEND
},
95 { P9_DOTL_NONBLOCK
, O_NONBLOCK
} ,
96 { P9_DOTL_DSYNC
, O_DSYNC
},
97 { P9_DOTL_FASYNC
, FASYNC
},
98 { P9_DOTL_DIRECT
, O_DIRECT
},
99 { P9_DOTL_LARGEFILE
, O_LARGEFILE
},
100 { P9_DOTL_DIRECTORY
, O_DIRECTORY
},
101 { P9_DOTL_NOFOLLOW
, O_NOFOLLOW
},
102 { P9_DOTL_NOATIME
, O_NOATIME
},
103 { P9_DOTL_SYNC
, O_SYNC
},
106 for (i
= 0; i
< ARRAY_SIZE(dotl_oflag_map
); i
++) {
107 if (flags
& dotl_oflag_map
[i
].dotl_flag
) {
108 oflags
|= dotl_oflag_map
[i
].open_flag
;
115 void cred_init(FsCred
*credp
)
123 static int get_dotl_openflags(V9fsState
*s
, int oflags
)
127 * Filter the client open flags
129 flags
= dotl_to_open_flags(oflags
);
130 flags
&= ~(O_NOCTTY
| O_ASYNC
| O_CREAT
);
132 * Ignore direct disk access hint until the server supports it.
138 void v9fs_path_init(V9fsPath
*path
)
144 void v9fs_path_free(V9fsPath
*path
)
151 void v9fs_path_copy(V9fsPath
*lhs
, V9fsPath
*rhs
)
154 lhs
->data
= g_malloc(rhs
->size
);
155 memcpy(lhs
->data
, rhs
->data
, rhs
->size
);
156 lhs
->size
= rhs
->size
;
159 int v9fs_name_to_path(V9fsState
*s
, V9fsPath
*dirpath
,
160 const char *name
, V9fsPath
*path
)
163 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
171 * Return TRUE if s1 is an ancestor of s2.
173 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d".
174 * As a special case, We treat s1 as ancestor of s2 if they are same!
176 static int v9fs_path_is_ancestor(V9fsPath
*s1
, V9fsPath
*s2
)
178 if (!strncmp(s1
->data
, s2
->data
, s1
->size
- 1)) {
179 if (s2
->data
[s1
->size
- 1] == '\0' || s2
->data
[s1
->size
- 1] == '/') {
186 static size_t v9fs_string_size(V9fsString
*str
)
192 * returns 0 if fid got re-opened, 1 if not, < 0 on error */
193 static int v9fs_reopen_fid(V9fsPDU
*pdu
, V9fsFidState
*f
)
196 if (f
->fid_type
== P9_FID_FILE
) {
197 if (f
->fs
.fd
== -1) {
199 err
= v9fs_co_open(pdu
, f
, f
->open_flags
);
200 } while (err
== -EINTR
&& !pdu
->cancelled
);
202 } else if (f
->fid_type
== P9_FID_DIR
) {
203 if (f
->fs
.dir
== NULL
) {
205 err
= v9fs_co_opendir(pdu
, f
);
206 } while (err
== -EINTR
&& !pdu
->cancelled
);
212 static V9fsFidState
*get_fid(V9fsPDU
*pdu
, int32_t fid
)
216 V9fsState
*s
= pdu
->s
;
218 for (f
= s
->fid_list
; f
; f
= f
->next
) {
222 * Update the fid ref upfront so that
223 * we don't get reclaimed when we yield
228 * check whether we need to reopen the
229 * file. We might have closed the fd
230 * while trying to free up some file
233 err
= v9fs_reopen_fid(pdu
, f
);
239 * Mark the fid as referenced so that the LRU
240 * reclaim won't close the file descriptor
242 f
->flags
|= FID_REFERENCED
;
249 static V9fsFidState
*alloc_fid(V9fsState
*s
, int32_t fid
)
253 for (f
= s
->fid_list
; f
; f
= f
->next
) {
254 /* If fid is already there return NULL */
260 f
= g_malloc0(sizeof(V9fsFidState
));
262 f
->fid_type
= P9_FID_NONE
;
265 * Mark the fid as referenced so that the LRU
266 * reclaim won't close the file descriptor
268 f
->flags
|= FID_REFERENCED
;
269 f
->next
= s
->fid_list
;
275 static int v9fs_xattr_fid_clunk(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
279 if (fidp
->fs
.xattr
.copied_len
== -1) {
280 /* getxattr/listxattr fid */
284 * if this is fid for setxattr. clunk should
285 * result in setxattr localcall
287 if (fidp
->fs
.xattr
.len
!= fidp
->fs
.xattr
.copied_len
) {
288 /* clunk after partial write */
292 if (fidp
->fs
.xattr
.len
) {
293 retval
= v9fs_co_lsetxattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
,
294 fidp
->fs
.xattr
.value
,
296 fidp
->fs
.xattr
.flags
);
298 retval
= v9fs_co_lremovexattr(pdu
, &fidp
->path
, &fidp
->fs
.xattr
.name
);
301 v9fs_string_free(&fidp
->fs
.xattr
.name
);
303 if (fidp
->fs
.xattr
.value
) {
304 g_free(fidp
->fs
.xattr
.value
);
309 static int free_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
313 if (fidp
->fid_type
== P9_FID_FILE
) {
314 /* If we reclaimed the fd no need to close */
315 if (fidp
->fs
.fd
!= -1) {
316 retval
= v9fs_co_close(pdu
, &fidp
->fs
);
318 } else if (fidp
->fid_type
== P9_FID_DIR
) {
319 if (fidp
->fs
.dir
!= NULL
) {
320 retval
= v9fs_co_closedir(pdu
, &fidp
->fs
);
322 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
323 retval
= v9fs_xattr_fid_clunk(pdu
, fidp
);
325 v9fs_path_free(&fidp
->path
);
330 static void put_fid(V9fsPDU
*pdu
, V9fsFidState
*fidp
)
335 * Don't free the fid if it is in reclaim list
337 if (!fidp
->ref
&& fidp
->clunked
) {
338 if (fidp
->fid
== pdu
->s
->root_fid
) {
340 * if the clunked fid is root fid then we
341 * have unmounted the fs on the client side.
342 * delete the migration blocker. Ideally, this
343 * should be hooked to transport close notification
345 if (pdu
->s
->migration_blocker
) {
346 migrate_del_blocker(pdu
->s
->migration_blocker
);
347 error_free(pdu
->s
->migration_blocker
);
348 pdu
->s
->migration_blocker
= NULL
;
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__
);
511 #define P9_QID_TYPE_DIR 0x80
512 #define P9_QID_TYPE_SYMLINK 0x02
514 #define P9_STAT_MODE_DIR 0x80000000
515 #define P9_STAT_MODE_APPEND 0x40000000
516 #define P9_STAT_MODE_EXCL 0x20000000
517 #define P9_STAT_MODE_MOUNT 0x10000000
518 #define P9_STAT_MODE_AUTH 0x08000000
519 #define P9_STAT_MODE_TMP 0x04000000
520 #define P9_STAT_MODE_SYMLINK 0x02000000
521 #define P9_STAT_MODE_LINK 0x01000000
522 #define P9_STAT_MODE_DEVICE 0x00800000
523 #define P9_STAT_MODE_NAMED_PIPE 0x00200000
524 #define P9_STAT_MODE_SOCKET 0x00100000
525 #define P9_STAT_MODE_SETUID 0x00080000
526 #define P9_STAT_MODE_SETGID 0x00040000
527 #define P9_STAT_MODE_SETVTX 0x00010000
529 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \
530 P9_STAT_MODE_SYMLINK | \
531 P9_STAT_MODE_LINK | \
532 P9_STAT_MODE_DEVICE | \
533 P9_STAT_MODE_NAMED_PIPE | \
536 /* This is the algorithm from ufs in spfs */
537 static void stat_to_qid(const struct stat
*stbuf
, V9fsQID
*qidp
)
541 memset(&qidp
->path
, 0, sizeof(qidp
->path
));
542 size
= MIN(sizeof(stbuf
->st_ino
), sizeof(qidp
->path
));
543 memcpy(&qidp
->path
, &stbuf
->st_ino
, size
);
544 qidp
->version
= stbuf
->st_mtime
^ (stbuf
->st_size
<< 8);
546 if (S_ISDIR(stbuf
->st_mode
)) {
547 qidp
->type
|= P9_QID_TYPE_DIR
;
549 if (S_ISLNK(stbuf
->st_mode
)) {
550 qidp
->type
|= P9_QID_TYPE_SYMLINK
;
554 static int fid_to_qid(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsQID
*qidp
)
559 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
563 stat_to_qid(&stbuf
, qidp
);
567 static V9fsPDU
*alloc_pdu(V9fsState
*s
)
571 if (!QLIST_EMPTY(&s
->free_list
)) {
572 pdu
= QLIST_FIRST(&s
->free_list
);
573 QLIST_REMOVE(pdu
, next
);
574 QLIST_INSERT_HEAD(&s
->active_list
, pdu
, next
);
579 static void free_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
583 * Cancelled pdu are added back to the freelist
586 if (!pdu
->cancelled
) {
587 QLIST_REMOVE(pdu
, next
);
588 QLIST_INSERT_HEAD(&s
->free_list
, pdu
, next
);
593 static void complete_pdu(V9fsState
*s
, V9fsPDU
*pdu
, ssize_t len
)
595 int8_t id
= pdu
->id
+ 1; /* Response */
601 if (s
->proto_version
!= V9FS_PROTO_2000L
) {
604 str
.data
= strerror(err
);
605 str
.size
= strlen(str
.data
);
607 len
+= pdu_marshal(pdu
, len
, "s", &str
);
611 len
+= pdu_marshal(pdu
, len
, "d", err
);
613 if (s
->proto_version
== V9FS_PROTO_2000L
) {
616 trace_v9fs_rerror(pdu
->tag
, pdu
->id
, err
); /* Trace ERROR */
619 /* fill out the header */
620 pdu_marshal(pdu
, 0, "dbw", (int32_t)len
, id
, pdu
->tag
);
622 /* keep these in sync */
626 /* push onto queue and notify */
627 virtqueue_push(s
->vq
, &pdu
->elem
, len
);
629 /* FIXME: we should batch these completions */
630 virtio_notify(&s
->vdev
, s
->vq
);
632 /* Now wakeup anybody waiting in flush for this request */
633 qemu_co_queue_next(&pdu
->complete
);
638 static mode_t
v9mode_to_mode(uint32_t mode
, V9fsString
*extension
)
643 if (mode
& P9_STAT_MODE_DIR
) {
647 if (mode
& P9_STAT_MODE_SYMLINK
) {
650 if (mode
& P9_STAT_MODE_SOCKET
) {
653 if (mode
& P9_STAT_MODE_NAMED_PIPE
) {
656 if (mode
& P9_STAT_MODE_DEVICE
) {
657 if (extension
&& extension
->data
[0] == 'c') {
668 if (mode
& P9_STAT_MODE_SETUID
) {
671 if (mode
& P9_STAT_MODE_SETGID
) {
674 if (mode
& P9_STAT_MODE_SETVTX
) {
681 static int donttouch_stat(V9fsStat
*stat
)
683 if (stat
->type
== -1 &&
685 stat
->qid
.type
== -1 &&
686 stat
->qid
.version
== -1 &&
687 stat
->qid
.path
== -1 &&
691 stat
->length
== -1 &&
698 stat
->n_muid
== -1) {
705 static void v9fs_stat_free(V9fsStat
*stat
)
707 v9fs_string_free(&stat
->name
);
708 v9fs_string_free(&stat
->uid
);
709 v9fs_string_free(&stat
->gid
);
710 v9fs_string_free(&stat
->muid
);
711 v9fs_string_free(&stat
->extension
);
714 static uint32_t stat_to_v9mode(const struct stat
*stbuf
)
718 mode
= stbuf
->st_mode
& 0777;
719 if (S_ISDIR(stbuf
->st_mode
)) {
720 mode
|= P9_STAT_MODE_DIR
;
723 if (S_ISLNK(stbuf
->st_mode
)) {
724 mode
|= P9_STAT_MODE_SYMLINK
;
727 if (S_ISSOCK(stbuf
->st_mode
)) {
728 mode
|= P9_STAT_MODE_SOCKET
;
731 if (S_ISFIFO(stbuf
->st_mode
)) {
732 mode
|= P9_STAT_MODE_NAMED_PIPE
;
735 if (S_ISBLK(stbuf
->st_mode
) || S_ISCHR(stbuf
->st_mode
)) {
736 mode
|= P9_STAT_MODE_DEVICE
;
739 if (stbuf
->st_mode
& S_ISUID
) {
740 mode
|= P9_STAT_MODE_SETUID
;
743 if (stbuf
->st_mode
& S_ISGID
) {
744 mode
|= P9_STAT_MODE_SETGID
;
747 if (stbuf
->st_mode
& S_ISVTX
) {
748 mode
|= P9_STAT_MODE_SETVTX
;
754 static int stat_to_v9stat(V9fsPDU
*pdu
, V9fsPath
*name
,
755 const struct stat
*stbuf
,
761 memset(v9stat
, 0, sizeof(*v9stat
));
763 stat_to_qid(stbuf
, &v9stat
->qid
);
764 v9stat
->mode
= stat_to_v9mode(stbuf
);
765 v9stat
->atime
= stbuf
->st_atime
;
766 v9stat
->mtime
= stbuf
->st_mtime
;
767 v9stat
->length
= stbuf
->st_size
;
769 v9fs_string_null(&v9stat
->uid
);
770 v9fs_string_null(&v9stat
->gid
);
771 v9fs_string_null(&v9stat
->muid
);
773 v9stat
->n_uid
= stbuf
->st_uid
;
774 v9stat
->n_gid
= stbuf
->st_gid
;
777 v9fs_string_null(&v9stat
->extension
);
779 if (v9stat
->mode
& P9_STAT_MODE_SYMLINK
) {
780 err
= v9fs_co_readlink(pdu
, name
, &v9stat
->extension
);
784 } else if (v9stat
->mode
& P9_STAT_MODE_DEVICE
) {
785 v9fs_string_sprintf(&v9stat
->extension
, "%c %u %u",
786 S_ISCHR(stbuf
->st_mode
) ? 'c' : 'b',
787 major(stbuf
->st_rdev
), minor(stbuf
->st_rdev
));
788 } else if (S_ISDIR(stbuf
->st_mode
) || S_ISREG(stbuf
->st_mode
)) {
789 v9fs_string_sprintf(&v9stat
->extension
, "%s %lu",
790 "HARDLINKCOUNT", (unsigned long)stbuf
->st_nlink
);
793 str
= strrchr(name
->data
, '/');
800 v9fs_string_sprintf(&v9stat
->name
, "%s", str
);
803 v9fs_string_size(&v9stat
->name
) +
804 v9fs_string_size(&v9stat
->uid
) +
805 v9fs_string_size(&v9stat
->gid
) +
806 v9fs_string_size(&v9stat
->muid
) +
807 v9fs_string_size(&v9stat
->extension
);
811 #define P9_STATS_MODE 0x00000001ULL
812 #define P9_STATS_NLINK 0x00000002ULL
813 #define P9_STATS_UID 0x00000004ULL
814 #define P9_STATS_GID 0x00000008ULL
815 #define P9_STATS_RDEV 0x00000010ULL
816 #define P9_STATS_ATIME 0x00000020ULL
817 #define P9_STATS_MTIME 0x00000040ULL
818 #define P9_STATS_CTIME 0x00000080ULL
819 #define P9_STATS_INO 0x00000100ULL
820 #define P9_STATS_SIZE 0x00000200ULL
821 #define P9_STATS_BLOCKS 0x00000400ULL
823 #define P9_STATS_BTIME 0x00000800ULL
824 #define P9_STATS_GEN 0x00001000ULL
825 #define P9_STATS_DATA_VERSION 0x00002000ULL
827 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */
828 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */
831 static void stat_to_v9stat_dotl(V9fsState
*s
, const struct stat
*stbuf
,
832 V9fsStatDotl
*v9lstat
)
834 memset(v9lstat
, 0, sizeof(*v9lstat
));
836 v9lstat
->st_mode
= stbuf
->st_mode
;
837 v9lstat
->st_nlink
= stbuf
->st_nlink
;
838 v9lstat
->st_uid
= stbuf
->st_uid
;
839 v9lstat
->st_gid
= stbuf
->st_gid
;
840 v9lstat
->st_rdev
= stbuf
->st_rdev
;
841 v9lstat
->st_size
= stbuf
->st_size
;
842 v9lstat
->st_blksize
= stbuf
->st_blksize
;
843 v9lstat
->st_blocks
= stbuf
->st_blocks
;
844 v9lstat
->st_atime_sec
= stbuf
->st_atime
;
845 v9lstat
->st_atime_nsec
= stbuf
->st_atim
.tv_nsec
;
846 v9lstat
->st_mtime_sec
= stbuf
->st_mtime
;
847 v9lstat
->st_mtime_nsec
= stbuf
->st_mtim
.tv_nsec
;
848 v9lstat
->st_ctime_sec
= stbuf
->st_ctime
;
849 v9lstat
->st_ctime_nsec
= stbuf
->st_ctim
.tv_nsec
;
850 /* Currently we only support BASIC fields in stat */
851 v9lstat
->st_result_mask
= P9_STATS_BASIC
;
853 stat_to_qid(stbuf
, &v9lstat
->qid
);
856 static void print_sg(struct iovec
*sg
, int cnt
)
860 printf("sg[%d]: {", cnt
);
861 for (i
= 0; i
< cnt
; i
++) {
865 printf("(%p, %zd)", sg
[i
].iov_base
, sg
[i
].iov_len
);
870 /* Will call this only for path name based fid */
871 static void v9fs_fix_path(V9fsPath
*dst
, V9fsPath
*src
, int len
)
874 v9fs_path_init(&str
);
875 v9fs_path_copy(&str
, dst
);
876 v9fs_string_sprintf((V9fsString
*)dst
, "%s%s", src
->data
, str
.data
+len
);
877 v9fs_path_free(&str
);
878 /* +1 to include terminating NULL */
882 static inline bool is_ro_export(FsContext
*ctx
)
884 return ctx
->export_flags
& V9FS_RDONLY
;
887 static void v9fs_version(void *opaque
)
889 V9fsPDU
*pdu
= opaque
;
890 V9fsState
*s
= pdu
->s
;
894 pdu_unmarshal(pdu
, offset
, "ds", &s
->msize
, &version
);
895 trace_v9fs_version(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
899 if (!strcmp(version
.data
, "9P2000.u")) {
900 s
->proto_version
= V9FS_PROTO_2000U
;
901 } else if (!strcmp(version
.data
, "9P2000.L")) {
902 s
->proto_version
= V9FS_PROTO_2000L
;
904 v9fs_string_sprintf(&version
, "unknown");
907 offset
+= pdu_marshal(pdu
, offset
, "ds", s
->msize
, &version
);
908 trace_v9fs_version_return(pdu
->tag
, pdu
->id
, s
->msize
, version
.data
);
910 complete_pdu(s
, pdu
, offset
);
912 v9fs_string_free(&version
);
916 static void v9fs_attach(void *opaque
)
918 V9fsPDU
*pdu
= opaque
;
919 V9fsState
*s
= pdu
->s
;
920 int32_t fid
, afid
, n_uname
;
921 V9fsString uname
, aname
;
927 pdu_unmarshal(pdu
, offset
, "ddssd", &fid
, &afid
, &uname
, &aname
, &n_uname
);
928 trace_v9fs_attach(pdu
->tag
, pdu
->id
, fid
, afid
, uname
.data
, aname
.data
);
930 fidp
= alloc_fid(s
, fid
);
936 err
= v9fs_co_name_to_path(pdu
, NULL
, "/", &fidp
->path
);
942 err
= fid_to_qid(pdu
, fidp
, &qid
);
948 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
950 trace_v9fs_attach_return(pdu
->tag
, pdu
->id
,
951 qid
.type
, qid
.version
, qid
.path
);
953 /* disable migration */
954 error_set(&s
->migration_blocker
, QERR_VIRTFS_FEATURE_BLOCKS_MIGRATION
,
955 s
->ctx
.fs_root
, s
->tag
);
956 migrate_add_blocker(s
->migration_blocker
);
960 complete_pdu(s
, pdu
, err
);
961 v9fs_string_free(&uname
);
962 v9fs_string_free(&aname
);
965 static void v9fs_stat(void *opaque
)
973 V9fsPDU
*pdu
= opaque
;
974 V9fsState
*s
= pdu
->s
;
976 pdu_unmarshal(pdu
, offset
, "d", &fid
);
977 trace_v9fs_stat(pdu
->tag
, pdu
->id
, fid
);
979 fidp
= get_fid(pdu
, fid
);
984 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
988 err
= stat_to_v9stat(pdu
, &fidp
->path
, &stbuf
, &v9stat
);
992 offset
+= pdu_marshal(pdu
, offset
, "wS", 0, &v9stat
);
994 trace_v9fs_stat_return(pdu
->tag
, pdu
->id
, v9stat
.mode
,
995 v9stat
.atime
, v9stat
.mtime
, v9stat
.length
);
996 v9fs_stat_free(&v9stat
);
1000 complete_pdu(s
, pdu
, err
);
1003 static void v9fs_getattr(void *opaque
)
1010 uint64_t request_mask
;
1011 V9fsStatDotl v9stat_dotl
;
1012 V9fsPDU
*pdu
= opaque
;
1013 V9fsState
*s
= pdu
->s
;
1015 pdu_unmarshal(pdu
, offset
, "dq", &fid
, &request_mask
);
1016 trace_v9fs_getattr(pdu
->tag
, pdu
->id
, fid
, request_mask
);
1018 fidp
= get_fid(pdu
, fid
);
1024 * Currently we only support BASIC fields in stat, so there is no
1025 * need to look at request_mask.
1027 retval
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1031 stat_to_v9stat_dotl(s
, &stbuf
, &v9stat_dotl
);
1033 /* fill st_gen if requested and supported by underlying fs */
1034 if (request_mask
& P9_STATS_GEN
) {
1035 retval
= v9fs_co_st_gen(pdu
, &fidp
->path
, stbuf
.st_mode
, &v9stat_dotl
);
1039 v9stat_dotl
.st_result_mask
|= P9_STATS_GEN
;
1042 retval
+= pdu_marshal(pdu
, offset
, "A", &v9stat_dotl
);
1043 trace_v9fs_getattr_return(pdu
->tag
, pdu
->id
, v9stat_dotl
.st_result_mask
,
1044 v9stat_dotl
.st_mode
, v9stat_dotl
.st_uid
,
1045 v9stat_dotl
.st_gid
);
1049 complete_pdu(s
, pdu
, retval
);
1052 /* Attribute flags */
1053 #define P9_ATTR_MODE (1 << 0)
1054 #define P9_ATTR_UID (1 << 1)
1055 #define P9_ATTR_GID (1 << 2)
1056 #define P9_ATTR_SIZE (1 << 3)
1057 #define P9_ATTR_ATIME (1 << 4)
1058 #define P9_ATTR_MTIME (1 << 5)
1059 #define P9_ATTR_CTIME (1 << 6)
1060 #define P9_ATTR_ATIME_SET (1 << 7)
1061 #define P9_ATTR_MTIME_SET (1 << 8)
1063 #define P9_ATTR_MASK 127
1065 static void v9fs_setattr(void *opaque
)
1072 V9fsPDU
*pdu
= opaque
;
1073 V9fsState
*s
= pdu
->s
;
1075 pdu_unmarshal(pdu
, offset
, "dI", &fid
, &v9iattr
);
1077 fidp
= get_fid(pdu
, fid
);
1082 if (v9iattr
.valid
& P9_ATTR_MODE
) {
1083 err
= v9fs_co_chmod(pdu
, &fidp
->path
, v9iattr
.mode
);
1088 if (v9iattr
.valid
& (P9_ATTR_ATIME
| P9_ATTR_MTIME
)) {
1089 struct timespec times
[2];
1090 if (v9iattr
.valid
& P9_ATTR_ATIME
) {
1091 if (v9iattr
.valid
& P9_ATTR_ATIME_SET
) {
1092 times
[0].tv_sec
= v9iattr
.atime_sec
;
1093 times
[0].tv_nsec
= v9iattr
.atime_nsec
;
1095 times
[0].tv_nsec
= UTIME_NOW
;
1098 times
[0].tv_nsec
= UTIME_OMIT
;
1100 if (v9iattr
.valid
& P9_ATTR_MTIME
) {
1101 if (v9iattr
.valid
& P9_ATTR_MTIME_SET
) {
1102 times
[1].tv_sec
= v9iattr
.mtime_sec
;
1103 times
[1].tv_nsec
= v9iattr
.mtime_nsec
;
1105 times
[1].tv_nsec
= UTIME_NOW
;
1108 times
[1].tv_nsec
= UTIME_OMIT
;
1110 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
1116 * If the only valid entry in iattr is ctime we can call
1117 * chown(-1,-1) to update the ctime of the file
1119 if ((v9iattr
.valid
& (P9_ATTR_UID
| P9_ATTR_GID
)) ||
1120 ((v9iattr
.valid
& P9_ATTR_CTIME
)
1121 && !((v9iattr
.valid
& P9_ATTR_MASK
) & ~P9_ATTR_CTIME
))) {
1122 if (!(v9iattr
.valid
& P9_ATTR_UID
)) {
1125 if (!(v9iattr
.valid
& P9_ATTR_GID
)) {
1128 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9iattr
.uid
,
1134 if (v9iattr
.valid
& (P9_ATTR_SIZE
)) {
1135 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9iattr
.size
);
1144 complete_pdu(s
, pdu
, err
);
1147 static int v9fs_walk_marshal(V9fsPDU
*pdu
, uint16_t nwnames
, V9fsQID
*qids
)
1151 offset
+= pdu_marshal(pdu
, offset
, "w", nwnames
);
1152 for (i
= 0; i
< nwnames
; i
++) {
1153 offset
+= pdu_marshal(pdu
, offset
, "Q", &qids
[i
]);
1158 static void v9fs_walk(void *opaque
)
1161 V9fsQID
*qids
= NULL
;
1163 V9fsPath dpath
, path
;
1167 int32_t fid
, newfid
;
1168 V9fsString
*wnames
= NULL
;
1170 V9fsFidState
*newfidp
= NULL
;
1171 V9fsPDU
*pdu
= opaque
;
1172 V9fsState
*s
= pdu
->s
;
1174 offset
+= pdu_unmarshal(pdu
, offset
, "ddw", &fid
,
1177 trace_v9fs_walk(pdu
->tag
, pdu
->id
, fid
, newfid
, nwnames
);
1179 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1180 wnames
= g_malloc0(sizeof(wnames
[0]) * nwnames
);
1181 qids
= g_malloc0(sizeof(qids
[0]) * nwnames
);
1182 for (i
= 0; i
< nwnames
; i
++) {
1183 offset
+= pdu_unmarshal(pdu
, offset
, "s", &wnames
[i
]);
1185 } else if (nwnames
> P9_MAXWELEM
) {
1189 fidp
= get_fid(pdu
, fid
);
1194 v9fs_path_init(&dpath
);
1195 v9fs_path_init(&path
);
1197 * Both dpath and path initially poin to fidp.
1198 * Needed to handle request with nwnames == 0
1200 v9fs_path_copy(&dpath
, &fidp
->path
);
1201 v9fs_path_copy(&path
, &fidp
->path
);
1202 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1203 err
= v9fs_co_name_to_path(pdu
, &dpath
, wnames
[name_idx
].data
, &path
);
1207 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1211 stat_to_qid(&stbuf
, &qids
[name_idx
]);
1212 v9fs_path_copy(&dpath
, &path
);
1214 if (fid
== newfid
) {
1215 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1216 v9fs_path_copy(&fidp
->path
, &path
);
1218 newfidp
= alloc_fid(s
, newfid
);
1219 if (newfidp
== NULL
) {
1223 newfidp
->uid
= fidp
->uid
;
1224 v9fs_path_copy(&newfidp
->path
, &path
);
1226 err
= v9fs_walk_marshal(pdu
, nwnames
, qids
);
1227 trace_v9fs_walk_return(pdu
->tag
, pdu
->id
, nwnames
, qids
);
1231 put_fid(pdu
, newfidp
);
1233 v9fs_path_free(&dpath
);
1234 v9fs_path_free(&path
);
1236 complete_pdu(s
, pdu
, err
);
1237 if (nwnames
&& nwnames
<= P9_MAXWELEM
) {
1238 for (name_idx
= 0; name_idx
< nwnames
; name_idx
++) {
1239 v9fs_string_free(&wnames
[name_idx
]);
1247 static int32_t get_iounit(V9fsPDU
*pdu
, V9fsPath
*path
)
1249 struct statfs stbuf
;
1251 V9fsState
*s
= pdu
->s
;
1254 * iounit should be multiples of f_bsize (host filesystem block size
1255 * and as well as less than (client msize - P9_IOHDRSZ))
1257 if (!v9fs_co_statfs(pdu
, path
, &stbuf
)) {
1258 iounit
= stbuf
.f_bsize
;
1259 iounit
*= (s
->msize
- P9_IOHDRSZ
)/stbuf
.f_bsize
;
1262 iounit
= s
->msize
- P9_IOHDRSZ
;
1267 static void v9fs_open(void *opaque
)
1278 V9fsPDU
*pdu
= opaque
;
1279 V9fsState
*s
= pdu
->s
;
1281 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1282 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &mode
);
1284 pdu_unmarshal(pdu
, offset
, "db", &fid
, &mode
);
1286 trace_v9fs_open(pdu
->tag
, pdu
->id
, fid
, mode
);
1288 fidp
= get_fid(pdu
, fid
);
1293 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
1295 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1299 stat_to_qid(&stbuf
, &qid
);
1300 if (S_ISDIR(stbuf
.st_mode
)) {
1301 err
= v9fs_co_opendir(pdu
, fidp
);
1305 fidp
->fid_type
= P9_FID_DIR
;
1306 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, 0);
1309 if (s
->proto_version
== V9FS_PROTO_2000L
) {
1310 flags
= get_dotl_openflags(s
, mode
);
1312 flags
= omode_to_uflags(mode
);
1314 if (is_ro_export(&s
->ctx
)) {
1315 if (mode
& O_WRONLY
|| mode
& O_RDWR
||
1316 mode
& O_APPEND
|| mode
& O_TRUNC
) {
1322 err
= v9fs_co_open(pdu
, fidp
, flags
);
1326 fidp
->fid_type
= P9_FID_FILE
;
1327 fidp
->open_flags
= flags
;
1328 if (flags
& O_EXCL
) {
1330 * We let the host file system do O_EXCL check
1331 * We should not reclaim such fd
1333 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1335 iounit
= get_iounit(pdu
, &fidp
->path
);
1336 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1339 trace_v9fs_open_return(pdu
->tag
, pdu
->id
,
1340 qid
.type
, qid
.version
, qid
.path
, iounit
);
1344 complete_pdu(s
, pdu
, err
);
1347 static void v9fs_lcreate(void *opaque
)
1349 int32_t dfid
, flags
, mode
;
1358 V9fsPDU
*pdu
= opaque
;
1360 pdu_unmarshal(pdu
, offset
, "dsddd", &dfid
, &name
, &flags
,
1362 trace_v9fs_lcreate(pdu
->tag
, pdu
->id
, dfid
, flags
, mode
, gid
);
1364 fidp
= get_fid(pdu
, dfid
);
1370 flags
= get_dotl_openflags(pdu
->s
, flags
);
1371 err
= v9fs_co_open2(pdu
, fidp
, &name
, gid
,
1372 flags
| O_CREAT
, mode
, &stbuf
);
1376 fidp
->fid_type
= P9_FID_FILE
;
1377 fidp
->open_flags
= flags
;
1378 if (flags
& O_EXCL
) {
1380 * We let the host file system do O_EXCL check
1381 * We should not reclaim such fd
1383 fidp
->flags
|= FID_NON_RECLAIMABLE
;
1385 iounit
= get_iounit(pdu
, &fidp
->path
);
1386 stat_to_qid(&stbuf
, &qid
);
1387 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
1389 trace_v9fs_lcreate_return(pdu
->tag
, pdu
->id
,
1390 qid
.type
, qid
.version
, qid
.path
, iounit
);
1394 complete_pdu(pdu
->s
, pdu
, err
);
1395 v9fs_string_free(&name
);
1398 static void v9fs_fsync(void *opaque
)
1405 V9fsPDU
*pdu
= opaque
;
1406 V9fsState
*s
= pdu
->s
;
1408 pdu_unmarshal(pdu
, offset
, "dd", &fid
, &datasync
);
1409 trace_v9fs_fsync(pdu
->tag
, pdu
->id
, fid
, datasync
);
1411 fidp
= get_fid(pdu
, fid
);
1416 err
= v9fs_co_fsync(pdu
, fidp
, datasync
);
1422 complete_pdu(s
, pdu
, err
);
1425 static void v9fs_clunk(void *opaque
)
1431 V9fsPDU
*pdu
= opaque
;
1432 V9fsState
*s
= pdu
->s
;
1434 pdu_unmarshal(pdu
, offset
, "d", &fid
);
1435 trace_v9fs_clunk(pdu
->tag
, pdu
->id
, fid
);
1437 fidp
= clunk_fid(s
, fid
);
1443 * Bump the ref so that put_fid will
1451 complete_pdu(s
, pdu
, err
);
1454 static int v9fs_xattr_read(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1455 uint64_t off
, uint32_t max_count
)
1461 xattr_len
= fidp
->fs
.xattr
.len
;
1462 read_count
= xattr_len
- off
;
1463 if (read_count
> max_count
) {
1464 read_count
= max_count
;
1465 } else if (read_count
< 0) {
1467 * read beyond XATTR value
1471 offset
+= pdu_marshal(pdu
, offset
, "d", read_count
);
1472 offset
+= v9fs_pack(pdu
->elem
.in_sg
, pdu
->elem
.in_num
, offset
,
1473 ((char *)fidp
->fs
.xattr
.value
) + off
,
1479 static int v9fs_do_readdir_with_stat(V9fsPDU
*pdu
,
1480 V9fsFidState
*fidp
, uint32_t max_count
)
1487 off_t saved_dir_pos
;
1488 struct dirent
*dent
, *result
;
1490 /* save the directory position */
1491 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1492 if (saved_dir_pos
< 0) {
1493 return saved_dir_pos
;
1496 dent
= g_malloc(sizeof(struct dirent
));
1499 v9fs_path_init(&path
);
1500 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1501 if (err
|| !result
) {
1504 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, dent
->d_name
, &path
);
1508 err
= v9fs_co_lstat(pdu
, &path
, &stbuf
);
1512 err
= stat_to_v9stat(pdu
, &path
, &stbuf
, &v9stat
);
1516 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1517 len
= pdu_marshal(pdu
, 11 + count
, "S", &v9stat
);
1518 if ((len
!= (v9stat
.size
+ 2)) || ((count
+ len
) > max_count
)) {
1519 /* Ran out of buffer. Set dir back to old position and return */
1520 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1521 v9fs_stat_free(&v9stat
);
1522 v9fs_path_free(&path
);
1527 v9fs_stat_free(&v9stat
);
1528 v9fs_path_free(&path
);
1529 saved_dir_pos
= dent
->d_off
;
1533 v9fs_path_free(&path
);
1541 * Create a QEMUIOVector for a sub-region of PDU iovecs
1543 * @qiov: uninitialized QEMUIOVector
1544 * @skip: number of bytes to skip from beginning of PDU
1545 * @size: number of bytes to include
1546 * @is_write: true - write, false - read
1548 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up
1549 * with qemu_iovec_destroy().
1551 static void v9fs_init_qiov_from_pdu(QEMUIOVector
*qiov
, V9fsPDU
*pdu
,
1552 uint64_t skip
, size_t size
,
1560 iov
= pdu
->elem
.out_sg
;
1561 niov
= pdu
->elem
.out_num
;
1563 iov
= pdu
->elem
.in_sg
;
1564 niov
= pdu
->elem
.in_num
;
1567 qemu_iovec_init_external(&elem
, iov
, niov
);
1568 qemu_iovec_init(qiov
, niov
);
1569 qemu_iovec_copy(qiov
, &elem
, skip
, size
);
1572 static void v9fs_read(void *opaque
)
1581 V9fsPDU
*pdu
= opaque
;
1582 V9fsState
*s
= pdu
->s
;
1584 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &max_count
);
1585 trace_v9fs_read(pdu
->tag
, pdu
->id
, fid
, off
, max_count
);
1587 fidp
= get_fid(pdu
, fid
);
1592 if (fidp
->fid_type
== P9_FID_DIR
) {
1595 v9fs_co_rewinddir(pdu
, fidp
);
1597 count
= v9fs_do_readdir_with_stat(pdu
, fidp
, max_count
);
1603 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1605 } else if (fidp
->fid_type
== P9_FID_FILE
) {
1606 QEMUIOVector qiov_full
;
1610 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
+ 4, max_count
, false);
1611 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1613 qemu_iovec_reset(&qiov
);
1614 qemu_iovec_copy(&qiov
, &qiov_full
, count
, qiov_full
.size
- count
);
1616 print_sg(qiov
.iov
, qiov
.niov
);
1618 /* Loop in case of EINTR */
1620 len
= v9fs_co_preadv(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1625 } while (len
== -EINTR
&& !pdu
->cancelled
);
1627 /* IO error return the error */
1631 } while (count
< max_count
&& len
> 0);
1633 err
+= pdu_marshal(pdu
, offset
, "d", count
);
1635 qemu_iovec_destroy(&qiov
);
1636 qemu_iovec_destroy(&qiov_full
);
1637 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1638 err
= v9fs_xattr_read(s
, pdu
, fidp
, off
, max_count
);
1642 trace_v9fs_read_return(pdu
->tag
, pdu
->id
, count
, err
);
1646 complete_pdu(s
, pdu
, err
);
1649 static size_t v9fs_readdir_data_size(V9fsString
*name
)
1652 * Size of each dirent on the wire: size of qid (13) + size of offset (8)
1653 * size of type (1) + size of name.size (2) + strlen(name.data)
1655 return 24 + v9fs_string_size(name
);
1658 static int v9fs_do_readdir(V9fsPDU
*pdu
,
1659 V9fsFidState
*fidp
, int32_t max_count
)
1666 off_t saved_dir_pos
;
1667 struct dirent
*dent
, *result
;
1669 /* save the directory position */
1670 saved_dir_pos
= v9fs_co_telldir(pdu
, fidp
);
1671 if (saved_dir_pos
< 0) {
1672 return saved_dir_pos
;
1675 dent
= g_malloc(sizeof(struct dirent
));
1678 err
= v9fs_co_readdir_r(pdu
, fidp
, dent
, &result
);
1679 if (err
|| !result
) {
1682 v9fs_string_init(&name
);
1683 v9fs_string_sprintf(&name
, "%s", dent
->d_name
);
1684 if ((count
+ v9fs_readdir_data_size(&name
)) > max_count
) {
1685 /* Ran out of buffer. Set dir back to old position and return */
1686 v9fs_co_seekdir(pdu
, fidp
, saved_dir_pos
);
1687 v9fs_string_free(&name
);
1692 * Fill up just the path field of qid because the client uses
1693 * only that. To fill the entire qid structure we will have
1694 * to stat each dirent found, which is expensive
1696 size
= MIN(sizeof(dent
->d_ino
), sizeof(qid
.path
));
1697 memcpy(&qid
.path
, &dent
->d_ino
, size
);
1698 /* Fill the other fields with dummy values */
1702 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */
1703 len
= pdu_marshal(pdu
, 11 + count
, "Qqbs",
1705 dent
->d_type
, &name
);
1707 v9fs_string_free(&name
);
1708 saved_dir_pos
= dent
->d_off
;
1717 static void v9fs_readdir(void *opaque
)
1723 uint64_t initial_offset
;
1726 V9fsPDU
*pdu
= opaque
;
1727 V9fsState
*s
= pdu
->s
;
1729 pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &initial_offset
, &max_count
);
1731 trace_v9fs_readdir(pdu
->tag
, pdu
->id
, fid
, initial_offset
, max_count
);
1733 fidp
= get_fid(pdu
, fid
);
1738 if (!fidp
->fs
.dir
) {
1742 if (initial_offset
== 0) {
1743 v9fs_co_rewinddir(pdu
, fidp
);
1745 v9fs_co_seekdir(pdu
, fidp
, initial_offset
);
1747 count
= v9fs_do_readdir(pdu
, fidp
, max_count
);
1753 retval
+= pdu_marshal(pdu
, offset
, "d", count
);
1755 trace_v9fs_readdir_return(pdu
->tag
, pdu
->id
, count
, retval
);
1759 complete_pdu(s
, pdu
, retval
);
1762 static int v9fs_xattr_write(V9fsState
*s
, V9fsPDU
*pdu
, V9fsFidState
*fidp
,
1763 uint64_t off
, uint32_t count
,
1764 struct iovec
*sg
, int cnt
)
1773 xattr_len
= fidp
->fs
.xattr
.len
;
1774 write_count
= xattr_len
- off
;
1775 if (write_count
> count
) {
1776 write_count
= count
;
1777 } else if (write_count
< 0) {
1779 * write beyond XATTR value len specified in
1785 offset
+= pdu_marshal(pdu
, offset
, "d", write_count
);
1787 fidp
->fs
.xattr
.copied_len
+= write_count
;
1789 * Now copy the content from sg list
1791 for (i
= 0; i
< cnt
; i
++) {
1792 if (write_count
> sg
[i
].iov_len
) {
1793 to_copy
= sg
[i
].iov_len
;
1795 to_copy
= write_count
;
1797 memcpy((char *)fidp
->fs
.xattr
.value
+ off
, sg
[i
].iov_base
, to_copy
);
1798 /* updating vs->off since we are not using below */
1800 write_count
-= to_copy
;
1806 static void v9fs_write(void *opaque
)
1816 V9fsPDU
*pdu
= opaque
;
1817 V9fsState
*s
= pdu
->s
;
1818 QEMUIOVector qiov_full
;
1821 offset
+= pdu_unmarshal(pdu
, offset
, "dqd", &fid
, &off
, &count
);
1822 v9fs_init_qiov_from_pdu(&qiov_full
, pdu
, offset
, count
, true);
1823 trace_v9fs_write(pdu
->tag
, pdu
->id
, fid
, off
, count
, qiov_full
.niov
);
1825 fidp
= get_fid(pdu
, fid
);
1830 if (fidp
->fid_type
== P9_FID_FILE
) {
1831 if (fidp
->fs
.fd
== -1) {
1835 } else if (fidp
->fid_type
== P9_FID_XATTR
) {
1837 * setxattr operation
1839 err
= v9fs_xattr_write(s
, pdu
, fidp
, off
, count
,
1840 qiov_full
.iov
, qiov_full
.niov
);
1846 qemu_iovec_init(&qiov
, qiov_full
.niov
);
1848 qemu_iovec_reset(&qiov
);
1849 qemu_iovec_copy(&qiov
, &qiov_full
, total
, qiov_full
.size
- total
);
1851 print_sg(qiov
.iov
, qiov
.niov
);
1853 /* Loop in case of EINTR */
1855 len
= v9fs_co_pwritev(pdu
, fidp
, qiov
.iov
, qiov
.niov
, off
);
1860 } while (len
== -EINTR
&& !pdu
->cancelled
);
1862 /* IO error return the error */
1866 } while (total
< count
&& len
> 0);
1869 offset
+= pdu_marshal(pdu
, offset
, "d", total
);
1871 trace_v9fs_write_return(pdu
->tag
, pdu
->id
, total
, err
);
1873 qemu_iovec_destroy(&qiov
);
1877 qemu_iovec_destroy(&qiov_full
);
1878 complete_pdu(s
, pdu
, err
);
1881 static void v9fs_create(void *opaque
)
1893 V9fsString extension
;
1895 V9fsPDU
*pdu
= opaque
;
1897 v9fs_path_init(&path
);
1899 pdu_unmarshal(pdu
, offset
, "dsdbs", &fid
, &name
,
1900 &perm
, &mode
, &extension
);
1902 trace_v9fs_create(pdu
->tag
, pdu
->id
, fid
, name
.data
, perm
, mode
);
1904 fidp
= get_fid(pdu
, fid
);
1909 if (perm
& P9_STAT_MODE_DIR
) {
1910 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, perm
& 0777,
1911 fidp
->uid
, -1, &stbuf
);
1915 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
1919 v9fs_path_copy(&fidp
->path
, &path
);
1920 err
= v9fs_co_opendir(pdu
, fidp
);
1924 fidp
->fid_type
= P9_FID_DIR
;
1925 } else if (perm
& P9_STAT_MODE_SYMLINK
) {
1926 err
= v9fs_co_symlink(pdu
, fidp
, &name
,
1927 extension
.data
, -1 , &stbuf
);
1931 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
1935 v9fs_path_copy(&fidp
->path
, &path
);
1936 } else if (perm
& P9_STAT_MODE_LINK
) {
1937 int32_t ofid
= atoi(extension
.data
);
1938 V9fsFidState
*ofidp
= get_fid(pdu
, ofid
);
1939 if (ofidp
== NULL
) {
1943 err
= v9fs_co_link(pdu
, ofidp
, fidp
, &name
);
1944 put_fid(pdu
, ofidp
);
1948 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
1950 fidp
->fid_type
= P9_FID_NONE
;
1953 v9fs_path_copy(&fidp
->path
, &path
);
1954 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
1956 fidp
->fid_type
= P9_FID_NONE
;
1959 } else if (perm
& P9_STAT_MODE_DEVICE
) {
1961 uint32_t major
, minor
;
1964 if (sscanf(extension
.data
, "%c %u %u", &ctype
, &major
, &minor
) != 3) {
1981 nmode
|= perm
& 0777;
1982 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
1983 makedev(major
, minor
), nmode
, &stbuf
);
1987 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
1991 v9fs_path_copy(&fidp
->path
, &path
);
1992 } else if (perm
& P9_STAT_MODE_NAMED_PIPE
) {
1993 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
1994 0, S_IFIFO
| (perm
& 0777), &stbuf
);
1998 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2002 v9fs_path_copy(&fidp
->path
, &path
);
2003 } else if (perm
& P9_STAT_MODE_SOCKET
) {
2004 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, -1,
2005 0, S_IFSOCK
| (perm
& 0777), &stbuf
);
2009 err
= v9fs_co_name_to_path(pdu
, &fidp
->path
, name
.data
, &path
);
2013 v9fs_path_copy(&fidp
->path
, &path
);
2015 err
= v9fs_co_open2(pdu
, fidp
, &name
, -1,
2016 omode_to_uflags(mode
)|O_CREAT
, perm
, &stbuf
);
2020 fidp
->fid_type
= P9_FID_FILE
;
2021 fidp
->open_flags
= omode_to_uflags(mode
);
2022 if (fidp
->open_flags
& O_EXCL
) {
2024 * We let the host file system do O_EXCL check
2025 * We should not reclaim such fd
2027 fidp
->flags
|= FID_NON_RECLAIMABLE
;
2030 iounit
= get_iounit(pdu
, &fidp
->path
);
2031 stat_to_qid(&stbuf
, &qid
);
2032 offset
+= pdu_marshal(pdu
, offset
, "Qd", &qid
, iounit
);
2034 trace_v9fs_create_return(pdu
->tag
, pdu
->id
,
2035 qid
.type
, qid
.version
, qid
.path
, iounit
);
2039 complete_pdu(pdu
->s
, pdu
, err
);
2040 v9fs_string_free(&name
);
2041 v9fs_string_free(&extension
);
2042 v9fs_path_free(&path
);
2045 static void v9fs_symlink(void *opaque
)
2047 V9fsPDU
*pdu
= opaque
;
2050 V9fsFidState
*dfidp
;
2058 pdu_unmarshal(pdu
, offset
, "dssd", &dfid
, &name
, &symname
, &gid
);
2059 trace_v9fs_symlink(pdu
->tag
, pdu
->id
, dfid
, name
.data
, symname
.data
, gid
);
2061 dfidp
= get_fid(pdu
, dfid
);
2062 if (dfidp
== NULL
) {
2066 err
= v9fs_co_symlink(pdu
, dfidp
, &name
, symname
.data
, gid
, &stbuf
);
2070 stat_to_qid(&stbuf
, &qid
);
2071 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2073 trace_v9fs_symlink_return(pdu
->tag
, pdu
->id
,
2074 qid
.type
, qid
.version
, qid
.path
);
2076 put_fid(pdu
, dfidp
);
2078 complete_pdu(pdu
->s
, pdu
, err
);
2079 v9fs_string_free(&name
);
2080 v9fs_string_free(&symname
);
2083 static void v9fs_flush(void *opaque
)
2087 V9fsPDU
*cancel_pdu
;
2088 V9fsPDU
*pdu
= opaque
;
2089 V9fsState
*s
= pdu
->s
;
2091 pdu_unmarshal(pdu
, offset
, "w", &tag
);
2092 trace_v9fs_flush(pdu
->tag
, pdu
->id
, tag
);
2094 QLIST_FOREACH(cancel_pdu
, &s
->active_list
, next
) {
2095 if (cancel_pdu
->tag
== tag
) {
2100 cancel_pdu
->cancelled
= 1;
2102 * Wait for pdu to complete.
2104 qemu_co_queue_wait(&cancel_pdu
->complete
);
2105 cancel_pdu
->cancelled
= 0;
2106 free_pdu(pdu
->s
, cancel_pdu
);
2108 complete_pdu(s
, pdu
, 7);
2112 static void v9fs_link(void *opaque
)
2114 V9fsPDU
*pdu
= opaque
;
2115 V9fsState
*s
= pdu
->s
;
2116 int32_t dfid
, oldfid
;
2117 V9fsFidState
*dfidp
, *oldfidp
;
2122 pdu_unmarshal(pdu
, offset
, "dds", &dfid
, &oldfid
, &name
);
2123 trace_v9fs_link(pdu
->tag
, pdu
->id
, dfid
, oldfid
, name
.data
);
2125 dfidp
= get_fid(pdu
, dfid
);
2126 if (dfidp
== NULL
) {
2131 oldfidp
= get_fid(pdu
, oldfid
);
2132 if (oldfidp
== NULL
) {
2136 err
= v9fs_co_link(pdu
, oldfidp
, dfidp
, &name
);
2141 put_fid(pdu
, dfidp
);
2143 v9fs_string_free(&name
);
2144 complete_pdu(s
, pdu
, err
);
2147 /* Only works with path name based fid */
2148 static void v9fs_remove(void *opaque
)
2154 V9fsPDU
*pdu
= opaque
;
2156 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2157 trace_v9fs_remove(pdu
->tag
, pdu
->id
, fid
);
2159 fidp
= get_fid(pdu
, fid
);
2164 /* if fs driver is not path based, return EOPNOTSUPP */
2165 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2170 * IF the file is unlinked, we cannot reopen
2171 * the file later. So don't reclaim fd
2173 err
= v9fs_mark_fids_unreclaim(pdu
, &fidp
->path
);
2177 err
= v9fs_co_remove(pdu
, &fidp
->path
);
2182 /* For TREMOVE we need to clunk the fid even on failed remove */
2183 clunk_fid(pdu
->s
, fidp
->fid
);
2186 complete_pdu(pdu
->s
, pdu
, err
);
2189 static void v9fs_unlinkat(void *opaque
)
2193 int32_t dfid
, flags
;
2196 V9fsFidState
*dfidp
;
2197 V9fsPDU
*pdu
= opaque
;
2199 pdu_unmarshal(pdu
, offset
, "dsd", &dfid
, &name
, &flags
);
2201 dfidp
= get_fid(pdu
, dfid
);
2202 if (dfidp
== NULL
) {
2207 * IF the file is unlinked, we cannot reopen
2208 * the file later. So don't reclaim fd
2210 v9fs_path_init(&path
);
2211 err
= v9fs_co_name_to_path(pdu
, &dfidp
->path
, name
.data
, &path
);
2215 err
= v9fs_mark_fids_unreclaim(pdu
, &path
);
2219 err
= v9fs_co_unlinkat(pdu
, &dfidp
->path
, &name
, flags
);
2224 put_fid(pdu
, dfidp
);
2225 v9fs_path_free(&path
);
2227 complete_pdu(pdu
->s
, pdu
, err
);
2228 v9fs_string_free(&name
);
2232 /* Only works with path name based fid */
2233 static int v9fs_complete_rename(V9fsPDU
*pdu
, V9fsFidState
*fidp
,
2234 int32_t newdirfid
, V9fsString
*name
)
2239 V9fsFidState
*tfidp
;
2240 V9fsState
*s
= pdu
->s
;
2241 V9fsFidState
*dirfidp
= NULL
;
2242 char *old_name
, *new_name
;
2244 v9fs_path_init(&new_path
);
2245 if (newdirfid
!= -1) {
2246 dirfidp
= get_fid(pdu
, newdirfid
);
2247 if (dirfidp
== NULL
) {
2251 BUG_ON(dirfidp
->fid_type
!= P9_FID_NONE
);
2252 v9fs_co_name_to_path(pdu
, &dirfidp
->path
, name
->data
, &new_path
);
2254 old_name
= fidp
->path
.data
;
2255 end
= strrchr(old_name
, '/');
2261 new_name
= g_malloc0(end
- old_name
+ name
->size
+ 1);
2262 strncat(new_name
, old_name
, end
- old_name
);
2263 strncat(new_name
+ (end
- old_name
), name
->data
, name
->size
);
2264 v9fs_co_name_to_path(pdu
, NULL
, new_name
, &new_path
);
2267 err
= v9fs_co_rename(pdu
, &fidp
->path
, &new_path
);
2272 * Fixup fid's pointing to the old name to
2273 * start pointing to the new name
2275 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2276 if (v9fs_path_is_ancestor(&fidp
->path
, &tfidp
->path
)) {
2277 /* replace the name */
2278 v9fs_fix_path(&tfidp
->path
, &new_path
, strlen(fidp
->path
.data
));
2283 put_fid(pdu
, dirfidp
);
2285 v9fs_path_free(&new_path
);
2290 /* Only works with path name based fid */
2291 static void v9fs_rename(void *opaque
)
2299 V9fsPDU
*pdu
= opaque
;
2300 V9fsState
*s
= pdu
->s
;
2302 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newdirfid
, &name
);
2304 fidp
= get_fid(pdu
, fid
);
2309 BUG_ON(fidp
->fid_type
!= P9_FID_NONE
);
2310 /* if fs driver is not path based, return EOPNOTSUPP */
2311 if (!(pdu
->s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
)) {
2315 v9fs_path_write_lock(s
);
2316 err
= v9fs_complete_rename(pdu
, fidp
, newdirfid
, &name
);
2317 v9fs_path_unlock(s
);
2324 complete_pdu(s
, pdu
, err
);
2325 v9fs_string_free(&name
);
2328 static void v9fs_fix_fid_paths(V9fsPDU
*pdu
, V9fsPath
*olddir
,
2329 V9fsString
*old_name
, V9fsPath
*newdir
,
2330 V9fsString
*new_name
)
2332 V9fsFidState
*tfidp
;
2333 V9fsPath oldpath
, newpath
;
2334 V9fsState
*s
= pdu
->s
;
2337 v9fs_path_init(&oldpath
);
2338 v9fs_path_init(&newpath
);
2339 v9fs_co_name_to_path(pdu
, olddir
, old_name
->data
, &oldpath
);
2340 v9fs_co_name_to_path(pdu
, newdir
, new_name
->data
, &newpath
);
2343 * Fixup fid's pointing to the old name to
2344 * start pointing to the new name
2346 for (tfidp
= s
->fid_list
; tfidp
; tfidp
= tfidp
->next
) {
2347 if (v9fs_path_is_ancestor(&oldpath
, &tfidp
->path
)) {
2348 /* replace the name */
2349 v9fs_fix_path(&tfidp
->path
, &newpath
, strlen(oldpath
.data
));
2352 v9fs_path_free(&oldpath
);
2353 v9fs_path_free(&newpath
);
2356 static int v9fs_complete_renameat(V9fsPDU
*pdu
, int32_t olddirfid
,
2357 V9fsString
*old_name
, int32_t newdirfid
,
2358 V9fsString
*new_name
)
2361 V9fsState
*s
= pdu
->s
;
2362 V9fsFidState
*newdirfidp
= NULL
, *olddirfidp
= NULL
;
2364 olddirfidp
= get_fid(pdu
, olddirfid
);
2365 if (olddirfidp
== NULL
) {
2369 if (newdirfid
!= -1) {
2370 newdirfidp
= get_fid(pdu
, newdirfid
);
2371 if (newdirfidp
== NULL
) {
2376 newdirfidp
= get_fid(pdu
, olddirfid
);
2379 err
= v9fs_co_renameat(pdu
, &olddirfidp
->path
, old_name
,
2380 &newdirfidp
->path
, new_name
);
2384 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
2385 /* Only for path based fid we need to do the below fixup */
2386 v9fs_fix_fid_paths(pdu
, &olddirfidp
->path
, old_name
,
2387 &newdirfidp
->path
, new_name
);
2391 put_fid(pdu
, olddirfidp
);
2394 put_fid(pdu
, newdirfidp
);
2399 static void v9fs_renameat(void *opaque
)
2403 V9fsPDU
*pdu
= opaque
;
2404 V9fsState
*s
= pdu
->s
;
2405 int32_t olddirfid
, newdirfid
;
2406 V9fsString old_name
, new_name
;
2408 pdu_unmarshal(pdu
, offset
, "dsds", &olddirfid
,
2409 &old_name
, &newdirfid
, &new_name
);
2411 v9fs_path_write_lock(s
);
2412 err
= v9fs_complete_renameat(pdu
, olddirfid
,
2413 &old_name
, newdirfid
, &new_name
);
2414 v9fs_path_unlock(s
);
2418 complete_pdu(s
, pdu
, err
);
2419 v9fs_string_free(&old_name
);
2420 v9fs_string_free(&new_name
);
2423 static void v9fs_wstat(void *opaque
)
2432 V9fsPDU
*pdu
= opaque
;
2433 V9fsState
*s
= pdu
->s
;
2435 pdu_unmarshal(pdu
, offset
, "dwS", &fid
, &unused
, &v9stat
);
2436 trace_v9fs_wstat(pdu
->tag
, pdu
->id
, fid
,
2437 v9stat
.mode
, v9stat
.atime
, v9stat
.mtime
);
2439 fidp
= get_fid(pdu
, fid
);
2444 /* do we need to sync the file? */
2445 if (donttouch_stat(&v9stat
)) {
2446 err
= v9fs_co_fsync(pdu
, fidp
, 0);
2449 if (v9stat
.mode
!= -1) {
2451 err
= v9fs_co_lstat(pdu
, &fidp
->path
, &stbuf
);
2455 v9_mode
= stat_to_v9mode(&stbuf
);
2456 if ((v9stat
.mode
& P9_STAT_MODE_TYPE_BITS
) !=
2457 (v9_mode
& P9_STAT_MODE_TYPE_BITS
)) {
2458 /* Attempting to change the type */
2462 err
= v9fs_co_chmod(pdu
, &fidp
->path
,
2463 v9mode_to_mode(v9stat
.mode
,
2464 &v9stat
.extension
));
2469 if (v9stat
.mtime
!= -1 || v9stat
.atime
!= -1) {
2470 struct timespec times
[2];
2471 if (v9stat
.atime
!= -1) {
2472 times
[0].tv_sec
= v9stat
.atime
;
2473 times
[0].tv_nsec
= 0;
2475 times
[0].tv_nsec
= UTIME_OMIT
;
2477 if (v9stat
.mtime
!= -1) {
2478 times
[1].tv_sec
= v9stat
.mtime
;
2479 times
[1].tv_nsec
= 0;
2481 times
[1].tv_nsec
= UTIME_OMIT
;
2483 err
= v9fs_co_utimensat(pdu
, &fidp
->path
, times
);
2488 if (v9stat
.n_gid
!= -1 || v9stat
.n_uid
!= -1) {
2489 err
= v9fs_co_chown(pdu
, &fidp
->path
, v9stat
.n_uid
, v9stat
.n_gid
);
2494 if (v9stat
.name
.size
!= 0) {
2495 err
= v9fs_complete_rename(pdu
, fidp
, -1, &v9stat
.name
);
2500 if (v9stat
.length
!= -1) {
2501 err
= v9fs_co_truncate(pdu
, &fidp
->path
, v9stat
.length
);
2510 v9fs_stat_free(&v9stat
);
2511 complete_pdu(s
, pdu
, err
);
2514 static int v9fs_fill_statfs(V9fsState
*s
, V9fsPDU
*pdu
, struct statfs
*stbuf
)
2526 int32_t bsize_factor
;
2529 * compute bsize factor based on host file system block size
2532 bsize_factor
= (s
->msize
- P9_IOHDRSZ
)/stbuf
->f_bsize
;
2533 if (!bsize_factor
) {
2536 f_type
= stbuf
->f_type
;
2537 f_bsize
= stbuf
->f_bsize
;
2538 f_bsize
*= bsize_factor
;
2540 * f_bsize is adjusted(multiplied) by bsize factor, so we need to
2541 * adjust(divide) the number of blocks, free blocks and available
2542 * blocks by bsize factor
2544 f_blocks
= stbuf
->f_blocks
/bsize_factor
;
2545 f_bfree
= stbuf
->f_bfree
/bsize_factor
;
2546 f_bavail
= stbuf
->f_bavail
/bsize_factor
;
2547 f_files
= stbuf
->f_files
;
2548 f_ffree
= stbuf
->f_ffree
;
2549 fsid_val
= (unsigned int) stbuf
->f_fsid
.__val
[0] |
2550 (unsigned long long)stbuf
->f_fsid
.__val
[1] << 32;
2551 f_namelen
= stbuf
->f_namelen
;
2553 return pdu_marshal(pdu
, offset
, "ddqqqqqqd",
2554 f_type
, f_bsize
, f_blocks
, f_bfree
,
2555 f_bavail
, f_files
, f_ffree
,
2556 fsid_val
, f_namelen
);
2559 static void v9fs_statfs(void *opaque
)
2565 struct statfs stbuf
;
2566 V9fsPDU
*pdu
= opaque
;
2567 V9fsState
*s
= pdu
->s
;
2569 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2570 fidp
= get_fid(pdu
, fid
);
2575 retval
= v9fs_co_statfs(pdu
, &fidp
->path
, &stbuf
);
2580 retval
+= v9fs_fill_statfs(s
, pdu
, &stbuf
);
2584 complete_pdu(s
, pdu
, retval
);
2588 static void v9fs_mknod(void *opaque
)
2601 V9fsPDU
*pdu
= opaque
;
2602 V9fsState
*s
= pdu
->s
;
2604 pdu_unmarshal(pdu
, offset
, "dsdddd", &fid
, &name
, &mode
,
2605 &major
, &minor
, &gid
);
2606 trace_v9fs_mknod(pdu
->tag
, pdu
->id
, fid
, mode
, major
, minor
);
2608 fidp
= get_fid(pdu
, fid
);
2613 err
= v9fs_co_mknod(pdu
, fidp
, &name
, fidp
->uid
, gid
,
2614 makedev(major
, minor
), mode
, &stbuf
);
2618 stat_to_qid(&stbuf
, &qid
);
2620 err
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2621 trace_v9fs_mknod_return(pdu
->tag
, pdu
->id
,
2622 qid
.type
, qid
.version
, qid
.path
);
2626 complete_pdu(s
, pdu
, err
);
2627 v9fs_string_free(&name
);
2631 * Implement posix byte range locking code
2632 * Server side handling of locking code is very simple, because 9p server in
2633 * QEMU can handle only one client. And most of the lock handling
2634 * (like conflict, merging) etc is done by the VFS layer itself, so no need to
2635 * do any thing in * qemu 9p server side lock code path.
2636 * So when a TLOCK request comes, always return success
2638 static void v9fs_lock(void *opaque
)
2645 int32_t fid
, err
= 0;
2646 V9fsPDU
*pdu
= opaque
;
2647 V9fsState
*s
= pdu
->s
;
2649 flock
= g_malloc(sizeof(*flock
));
2650 pdu_unmarshal(pdu
, offset
, "dbdqqds", &fid
, &flock
->type
,
2651 &flock
->flags
, &flock
->start
, &flock
->length
,
2652 &flock
->proc_id
, &flock
->client_id
);
2654 trace_v9fs_lock(pdu
->tag
, pdu
->id
, fid
,
2655 flock
->type
, flock
->start
, flock
->length
);
2657 status
= P9_LOCK_ERROR
;
2659 /* We support only block flag now (that too ignored currently) */
2660 if (flock
->flags
& ~P9_LOCK_FLAGS_BLOCK
) {
2664 fidp
= get_fid(pdu
, fid
);
2669 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2673 status
= P9_LOCK_SUCCESS
;
2678 err
+= pdu_marshal(pdu
, offset
, "b", status
);
2679 trace_v9fs_lock_return(pdu
->tag
, pdu
->id
, status
);
2680 complete_pdu(s
, pdu
, err
);
2681 v9fs_string_free(&flock
->client_id
);
2686 * When a TGETLOCK request comes, always return success because all lock
2687 * handling is done by client's VFS layer.
2689 static void v9fs_getlock(void *opaque
)
2695 int32_t fid
, err
= 0;
2696 V9fsPDU
*pdu
= opaque
;
2697 V9fsState
*s
= pdu
->s
;
2699 glock
= g_malloc(sizeof(*glock
));
2700 pdu_unmarshal(pdu
, offset
, "dbqqds", &fid
, &glock
->type
,
2701 &glock
->start
, &glock
->length
, &glock
->proc_id
,
2704 trace_v9fs_getlock(pdu
->tag
, pdu
->id
, fid
,
2705 glock
->type
, glock
->start
, glock
->length
);
2707 fidp
= get_fid(pdu
, fid
);
2712 err
= v9fs_co_fstat(pdu
, fidp
, &stbuf
);
2716 glock
->type
= P9_LOCK_TYPE_UNLCK
;
2717 offset
+= pdu_marshal(pdu
, offset
, "bqqds", glock
->type
,
2718 glock
->start
, glock
->length
, glock
->proc_id
,
2721 trace_v9fs_getlock_return(pdu
->tag
, pdu
->id
, glock
->type
, glock
->start
,
2722 glock
->length
, glock
->proc_id
);
2726 complete_pdu(s
, pdu
, err
);
2727 v9fs_string_free(&glock
->client_id
);
2731 static void v9fs_mkdir(void *opaque
)
2733 V9fsPDU
*pdu
= opaque
;
2744 pdu_unmarshal(pdu
, offset
, "dsdd", &fid
, &name
, &mode
, &gid
);
2746 trace_v9fs_mkdir(pdu
->tag
, pdu
->id
, fid
, name
.data
, mode
, gid
);
2748 fidp
= get_fid(pdu
, fid
);
2753 err
= v9fs_co_mkdir(pdu
, fidp
, &name
, mode
, fidp
->uid
, gid
, &stbuf
);
2757 stat_to_qid(&stbuf
, &qid
);
2758 offset
+= pdu_marshal(pdu
, offset
, "Q", &qid
);
2760 trace_v9fs_mkdir_return(pdu
->tag
, pdu
->id
,
2761 qid
.type
, qid
.version
, qid
.path
, err
);
2765 complete_pdu(pdu
->s
, pdu
, err
);
2766 v9fs_string_free(&name
);
2769 static void v9fs_xattrwalk(void *opaque
)
2775 int32_t fid
, newfid
;
2776 V9fsFidState
*file_fidp
;
2777 V9fsFidState
*xattr_fidp
= NULL
;
2778 V9fsPDU
*pdu
= opaque
;
2779 V9fsState
*s
= pdu
->s
;
2781 pdu_unmarshal(pdu
, offset
, "dds", &fid
, &newfid
, &name
);
2782 trace_v9fs_xattrwalk(pdu
->tag
, pdu
->id
, fid
, newfid
, name
.data
);
2784 file_fidp
= get_fid(pdu
, fid
);
2785 if (file_fidp
== NULL
) {
2789 xattr_fidp
= alloc_fid(s
, newfid
);
2790 if (xattr_fidp
== NULL
) {
2794 v9fs_path_copy(&xattr_fidp
->path
, &file_fidp
->path
);
2795 if (name
.data
[0] == 0) {
2797 * listxattr request. Get the size first
2799 size
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
, NULL
, 0);
2802 clunk_fid(s
, xattr_fidp
->fid
);
2806 * Read the xattr value
2808 xattr_fidp
->fs
.xattr
.len
= size
;
2809 xattr_fidp
->fid_type
= P9_FID_XATTR
;
2810 xattr_fidp
->fs
.xattr
.copied_len
= -1;
2812 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
2813 err
= v9fs_co_llistxattr(pdu
, &xattr_fidp
->path
,
2814 xattr_fidp
->fs
.xattr
.value
,
2815 xattr_fidp
->fs
.xattr
.len
);
2817 clunk_fid(s
, xattr_fidp
->fid
);
2821 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
2825 * specific xattr fid. We check for xattr
2826 * presence also collect the xattr size
2828 size
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
2832 clunk_fid(s
, xattr_fidp
->fid
);
2836 * Read the xattr value
2838 xattr_fidp
->fs
.xattr
.len
= size
;
2839 xattr_fidp
->fid_type
= P9_FID_XATTR
;
2840 xattr_fidp
->fs
.xattr
.copied_len
= -1;
2842 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
2843 err
= v9fs_co_lgetxattr(pdu
, &xattr_fidp
->path
,
2844 &name
, xattr_fidp
->fs
.xattr
.value
,
2845 xattr_fidp
->fs
.xattr
.len
);
2847 clunk_fid(s
, xattr_fidp
->fid
);
2851 offset
+= pdu_marshal(pdu
, offset
, "q", size
);
2854 trace_v9fs_xattrwalk_return(pdu
->tag
, pdu
->id
, size
);
2856 put_fid(pdu
, file_fidp
);
2858 put_fid(pdu
, xattr_fidp
);
2861 complete_pdu(s
, pdu
, err
);
2862 v9fs_string_free(&name
);
2865 static void v9fs_xattrcreate(void *opaque
)
2873 V9fsFidState
*file_fidp
;
2874 V9fsFidState
*xattr_fidp
;
2875 V9fsPDU
*pdu
= opaque
;
2876 V9fsState
*s
= pdu
->s
;
2878 pdu_unmarshal(pdu
, offset
, "dsqd",
2879 &fid
, &name
, &size
, &flags
);
2880 trace_v9fs_xattrcreate(pdu
->tag
, pdu
->id
, fid
, name
.data
, size
, flags
);
2882 file_fidp
= get_fid(pdu
, fid
);
2883 if (file_fidp
== NULL
) {
2887 /* Make the file fid point to xattr */
2888 xattr_fidp
= file_fidp
;
2889 xattr_fidp
->fid_type
= P9_FID_XATTR
;
2890 xattr_fidp
->fs
.xattr
.copied_len
= 0;
2891 xattr_fidp
->fs
.xattr
.len
= size
;
2892 xattr_fidp
->fs
.xattr
.flags
= flags
;
2893 v9fs_string_init(&xattr_fidp
->fs
.xattr
.name
);
2894 v9fs_string_copy(&xattr_fidp
->fs
.xattr
.name
, &name
);
2896 xattr_fidp
->fs
.xattr
.value
= g_malloc(size
);
2898 xattr_fidp
->fs
.xattr
.value
= NULL
;
2901 put_fid(pdu
, file_fidp
);
2903 complete_pdu(s
, pdu
, err
);
2904 v9fs_string_free(&name
);
2907 static void v9fs_readlink(void *opaque
)
2909 V9fsPDU
*pdu
= opaque
;
2916 pdu_unmarshal(pdu
, offset
, "d", &fid
);
2917 trace_v9fs_readlink(pdu
->tag
, pdu
->id
, fid
);
2918 fidp
= get_fid(pdu
, fid
);
2924 v9fs_string_init(&target
);
2925 err
= v9fs_co_readlink(pdu
, &fidp
->path
, &target
);
2929 offset
+= pdu_marshal(pdu
, offset
, "s", &target
);
2931 trace_v9fs_readlink_return(pdu
->tag
, pdu
->id
, target
.data
);
2932 v9fs_string_free(&target
);
2936 complete_pdu(pdu
->s
, pdu
, err
);
2939 static CoroutineEntry
*pdu_co_handlers
[] = {
2940 [P9_TREADDIR
] = v9fs_readdir
,
2941 [P9_TSTATFS
] = v9fs_statfs
,
2942 [P9_TGETATTR
] = v9fs_getattr
,
2943 [P9_TSETATTR
] = v9fs_setattr
,
2944 [P9_TXATTRWALK
] = v9fs_xattrwalk
,
2945 [P9_TXATTRCREATE
] = v9fs_xattrcreate
,
2946 [P9_TMKNOD
] = v9fs_mknod
,
2947 [P9_TRENAME
] = v9fs_rename
,
2948 [P9_TLOCK
] = v9fs_lock
,
2949 [P9_TGETLOCK
] = v9fs_getlock
,
2950 [P9_TRENAMEAT
] = v9fs_renameat
,
2951 [P9_TREADLINK
] = v9fs_readlink
,
2952 [P9_TUNLINKAT
] = v9fs_unlinkat
,
2953 [P9_TMKDIR
] = v9fs_mkdir
,
2954 [P9_TVERSION
] = v9fs_version
,
2955 [P9_TLOPEN
] = v9fs_open
,
2956 [P9_TATTACH
] = v9fs_attach
,
2957 [P9_TSTAT
] = v9fs_stat
,
2958 [P9_TWALK
] = v9fs_walk
,
2959 [P9_TCLUNK
] = v9fs_clunk
,
2960 [P9_TFSYNC
] = v9fs_fsync
,
2961 [P9_TOPEN
] = v9fs_open
,
2962 [P9_TREAD
] = v9fs_read
,
2964 [P9_TAUTH
] = v9fs_auth
,
2966 [P9_TFLUSH
] = v9fs_flush
,
2967 [P9_TLINK
] = v9fs_link
,
2968 [P9_TSYMLINK
] = v9fs_symlink
,
2969 [P9_TCREATE
] = v9fs_create
,
2970 [P9_TLCREATE
] = v9fs_lcreate
,
2971 [P9_TWRITE
] = v9fs_write
,
2972 [P9_TWSTAT
] = v9fs_wstat
,
2973 [P9_TREMOVE
] = v9fs_remove
,
2976 static void v9fs_op_not_supp(void *opaque
)
2978 V9fsPDU
*pdu
= opaque
;
2979 complete_pdu(pdu
->s
, pdu
, -EOPNOTSUPP
);
2982 static void v9fs_fs_ro(void *opaque
)
2984 V9fsPDU
*pdu
= opaque
;
2985 complete_pdu(pdu
->s
, pdu
, -EROFS
);
2988 static inline bool is_read_only_op(V9fsPDU
*pdu
)
3015 static void submit_pdu(V9fsState
*s
, V9fsPDU
*pdu
)
3018 CoroutineEntry
*handler
;
3020 if (pdu
->id
>= ARRAY_SIZE(pdu_co_handlers
) ||
3021 (pdu_co_handlers
[pdu
->id
] == NULL
)) {
3022 handler
= v9fs_op_not_supp
;
3024 handler
= pdu_co_handlers
[pdu
->id
];
3027 if (is_ro_export(&s
->ctx
) && !is_read_only_op(pdu
)) {
3028 handler
= v9fs_fs_ro
;
3030 co
= qemu_coroutine_create(handler
);
3031 qemu_coroutine_enter(co
, pdu
);
3034 void handle_9p_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
3036 V9fsState
*s
= (V9fsState
*)vdev
;
3040 while ((pdu
= alloc_pdu(s
)) &&
3041 (len
= virtqueue_pop(vq
, &pdu
->elem
)) != 0) {
3044 BUG_ON(pdu
->elem
.out_num
== 0 || pdu
->elem
.in_num
== 0);
3045 BUG_ON(pdu
->elem
.out_sg
[0].iov_len
< 7);
3047 ptr
= pdu
->elem
.out_sg
[0].iov_base
;
3049 memcpy(&pdu
->size
, ptr
, 4);
3051 memcpy(&pdu
->tag
, ptr
+ 5, 2);
3052 qemu_co_queue_init(&pdu
->complete
);
3058 void virtio_9p_set_fd_limit(void)
3061 if (getrlimit(RLIMIT_NOFILE
, &rlim
) < 0) {
3062 fprintf(stderr
, "Failed to get the resource limit\n");
3065 open_fd_hw
= rlim
.rlim_cur
- MIN(400, rlim
.rlim_cur
/3);
3066 open_fd_rc
= rlim
.rlim_cur
/2;