2 * Virtio 9p Proxy callback
4 * Copyright IBM, Corp. 2011
7 * M. Mohan Kumar <mohan@in.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.
12 #include <sys/socket.h>
14 #include "hw/virtio/virtio.h"
15 #include "virtio-9p.h"
16 #include "qemu/error-report.h"
17 #include "fsdev/qemu-fsdev.h"
18 #include "virtio-9p-proxy.h"
20 typedef struct V9fsProxy
{
23 struct iovec in_iovec
;
24 struct iovec out_iovec
;
28 * Return received file descriptor on success in *status.
29 * errno is also returned on *status (which will be < 0)
30 * return < 0 on transport error.
32 static int v9fs_receivefd(int sockfd
, int *status
)
38 union MsgControl msg_control
;
41 iov
.iov_len
= sizeof(data
);
43 memset(&msg
, 0, sizeof(msg
));
46 msg
.msg_control
= &msg_control
;
47 msg
.msg_controllen
= sizeof(msg_control
);
50 retval
= recvmsg(sockfd
, &msg
, 0);
51 } while (retval
< 0 && errno
== EINTR
);
56 * data is set to V9FS_FD_VALID, if ancillary data is sent. If this
57 * request doesn't need ancillary data (fd) or an error occurred,
58 * data is set to negative errno value.
60 if (data
!= V9FS_FD_VALID
) {
65 * File descriptor (fd) is sent in the ancillary data. Check if we
66 * indeed received it. One of the reasons to fail to receive it is if
67 * we exceeded the maximum number of file descriptors!
69 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
70 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
71 cmsg
->cmsg_level
!= SOL_SOCKET
||
72 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
75 fd
= *((int *)CMSG_DATA(cmsg
));
79 *status
= -ENFILE
; /* Ancillary data sent but not received */
83 static ssize_t
socket_read(int sockfd
, void *buff
, size_t size
)
85 ssize_t retval
, total
= 0;
88 retval
= read(sockfd
, buff
, size
);
105 /* Converts proxy_statfs to VFS statfs structure */
106 static void prstatfs_to_statfs(struct statfs
*stfs
, ProxyStatFS
*prstfs
)
108 memset(stfs
, 0, sizeof(*stfs
));
109 stfs
->f_type
= prstfs
->f_type
;
110 stfs
->f_bsize
= prstfs
->f_bsize
;
111 stfs
->f_blocks
= prstfs
->f_blocks
;
112 stfs
->f_bfree
= prstfs
->f_bfree
;
113 stfs
->f_bavail
= prstfs
->f_bavail
;
114 stfs
->f_files
= prstfs
->f_files
;
115 stfs
->f_ffree
= prstfs
->f_ffree
;
116 stfs
->f_fsid
.__val
[0] = prstfs
->f_fsid
[0] & 0xFFFFFFFFU
;
117 stfs
->f_fsid
.__val
[1] = prstfs
->f_fsid
[1] >> 32 & 0xFFFFFFFFU
;
118 stfs
->f_namelen
= prstfs
->f_namelen
;
119 stfs
->f_frsize
= prstfs
->f_frsize
;
122 /* Converts proxy_stat structure to VFS stat structure */
123 static void prstat_to_stat(struct stat
*stbuf
, ProxyStat
*prstat
)
125 memset(stbuf
, 0, sizeof(*stbuf
));
126 stbuf
->st_dev
= prstat
->st_dev
;
127 stbuf
->st_ino
= prstat
->st_ino
;
128 stbuf
->st_nlink
= prstat
->st_nlink
;
129 stbuf
->st_mode
= prstat
->st_mode
;
130 stbuf
->st_uid
= prstat
->st_uid
;
131 stbuf
->st_gid
= prstat
->st_gid
;
132 stbuf
->st_rdev
= prstat
->st_rdev
;
133 stbuf
->st_size
= prstat
->st_size
;
134 stbuf
->st_blksize
= prstat
->st_blksize
;
135 stbuf
->st_blocks
= prstat
->st_blocks
;
136 stbuf
->st_atim
.tv_sec
= prstat
->st_atim_sec
;
137 stbuf
->st_atim
.tv_nsec
= prstat
->st_atim_nsec
;
138 stbuf
->st_mtime
= prstat
->st_mtim_sec
;
139 stbuf
->st_mtim
.tv_nsec
= prstat
->st_mtim_nsec
;
140 stbuf
->st_ctime
= prstat
->st_ctim_sec
;
141 stbuf
->st_ctim
.tv_nsec
= prstat
->st_ctim_nsec
;
145 * Response contains two parts
147 * header.type == T_ERROR, data -> -errno
148 * header.type == T_SUCCESS, data -> response
149 * size of errno/response is given by header.size
150 * returns < 0, on transport error. response is
151 * valid only if status >= 0.
153 static int v9fs_receive_response(V9fsProxy
*proxy
, int type
,
154 int *status
, void *response
)
158 struct iovec
*reply
= &proxy
->in_iovec
;
162 retval
= socket_read(proxy
->sockfd
, reply
->iov_base
, PROXY_HDR_SZ
);
166 reply
->iov_len
= PROXY_HDR_SZ
;
167 proxy_unmarshal(reply
, 0, "dd", &header
.type
, &header
.size
);
169 * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
172 if (header
.size
> PROXY_MAX_IO_SZ
) {
174 while (header
.size
> 0) {
175 count
= MIN(PROXY_MAX_IO_SZ
, header
.size
);
176 count
= socket_read(proxy
->sockfd
, reply
->iov_base
, count
);
180 header
.size
-= count
;
186 retval
= socket_read(proxy
->sockfd
,
187 reply
->iov_base
+ PROXY_HDR_SZ
, header
.size
);
191 reply
->iov_len
+= header
.size
;
192 /* there was an error during processing request */
193 if (header
.type
== T_ERROR
) {
195 ret
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "d", status
);
205 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
,
206 "qqqdddqqqqqqqqqq", &prstat
.st_dev
,
207 &prstat
.st_ino
, &prstat
.st_nlink
,
208 &prstat
.st_mode
, &prstat
.st_uid
,
209 &prstat
.st_gid
, &prstat
.st_rdev
,
210 &prstat
.st_size
, &prstat
.st_blksize
,
212 &prstat
.st_atim_sec
, &prstat
.st_atim_nsec
,
213 &prstat
.st_mtim_sec
, &prstat
.st_mtim_nsec
,
214 &prstat
.st_ctim_sec
, &prstat
.st_ctim_nsec
);
215 prstat_to_stat(response
, &prstat
);
220 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
,
221 "qqqqqqqqqqq", &prstfs
.f_type
,
222 &prstfs
.f_bsize
, &prstfs
.f_blocks
,
223 &prstfs
.f_bfree
, &prstfs
.f_bavail
,
224 &prstfs
.f_files
, &prstfs
.f_ffree
,
225 &prstfs
.f_fsid
[0], &prstfs
.f_fsid
[1],
226 &prstfs
.f_namelen
, &prstfs
.f_frsize
);
227 prstatfs_to_statfs(response
, &prstfs
);
232 v9fs_string_init(&target
);
233 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "s", &target
);
234 strcpy(response
, target
.data
);
235 v9fs_string_free(&target
);
241 v9fs_string_init(&xattr
);
242 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "s", &xattr
);
243 memcpy(response
, xattr
.data
, xattr
.size
);
244 v9fs_string_free(&xattr
);
248 proxy_unmarshal(reply
, PROXY_HDR_SZ
, "q", response
);
260 * return < 0 on transport error.
261 * *status is valid only if return >= 0
263 static int v9fs_receive_status(V9fsProxy
*proxy
,
264 struct iovec
*reply
, int *status
)
271 retval
= socket_read(proxy
->sockfd
, reply
->iov_base
, PROXY_HDR_SZ
);
275 reply
->iov_len
= PROXY_HDR_SZ
;
276 proxy_unmarshal(reply
, 0, "dd", &header
.type
, &header
.size
);
277 if (header
.size
!= sizeof(int)) {
281 retval
= socket_read(proxy
->sockfd
,
282 reply
->iov_base
+ PROXY_HDR_SZ
, header
.size
);
286 reply
->iov_len
+= header
.size
;
287 proxy_unmarshal(reply
, PROXY_HDR_SZ
, "d", status
);
292 * Proxy->header and proxy->request written to socket by QEMU process.
293 * This request read by proxy helper process
294 * returns 0 on success and -errno on error
296 static int v9fs_request(V9fsProxy
*proxy
, int type
,
297 void *response
, const char *fmt
, ...)
304 ProxyHeader header
= { 0, 0};
305 struct timespec spec
[2];
306 int flags
, mode
, uid
, gid
;
307 V9fsString
*name
, *value
;
308 V9fsString
*path
, *oldpath
;
309 struct iovec
*iovec
= NULL
, *reply
= NULL
;
311 qemu_mutex_lock(&proxy
->mutex
);
313 if (proxy
->sockfd
== -1) {
317 iovec
= &proxy
->out_iovec
;
318 reply
= &proxy
->in_iovec
;
322 path
= va_arg(ap
, V9fsString
*);
323 flags
= va_arg(ap
, int);
324 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, flags
);
326 header
.size
= retval
;
327 header
.type
= T_OPEN
;
331 path
= va_arg(ap
, V9fsString
*);
332 flags
= va_arg(ap
, int);
333 mode
= va_arg(ap
, int);
334 uid
= va_arg(ap
, int);
335 gid
= va_arg(ap
, int);
336 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdddd", path
,
337 flags
, mode
, uid
, gid
);
339 header
.size
= retval
;
340 header
.type
= T_CREATE
;
344 path
= va_arg(ap
, V9fsString
*);
345 mode
= va_arg(ap
, int);
346 rdev
= va_arg(ap
, long int);
347 uid
= va_arg(ap
, int);
348 gid
= va_arg(ap
, int);
349 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsdq",
350 uid
, gid
, path
, mode
, rdev
);
352 header
.size
= retval
;
353 header
.type
= T_MKNOD
;
357 path
= va_arg(ap
, V9fsString
*);
358 mode
= va_arg(ap
, int);
359 uid
= va_arg(ap
, int);
360 gid
= va_arg(ap
, int);
361 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsd",
362 uid
, gid
, path
, mode
);
364 header
.size
= retval
;
365 header
.type
= T_MKDIR
;
369 oldpath
= va_arg(ap
, V9fsString
*);
370 path
= va_arg(ap
, V9fsString
*);
371 uid
= va_arg(ap
, int);
372 gid
= va_arg(ap
, int);
373 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddss",
374 uid
, gid
, oldpath
, path
);
376 header
.size
= retval
;
377 header
.type
= T_SYMLINK
;
381 oldpath
= va_arg(ap
, V9fsString
*);
382 path
= va_arg(ap
, V9fsString
*);
383 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss",
386 header
.size
= retval
;
387 header
.type
= T_LINK
;
391 path
= va_arg(ap
, V9fsString
*);
392 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
394 header
.size
= retval
;
395 header
.type
= T_LSTAT
;
399 path
= va_arg(ap
, V9fsString
*);
400 size
= va_arg(ap
, int);
401 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, size
);
403 header
.size
= retval
;
404 header
.type
= T_READLINK
;
408 path
= va_arg(ap
, V9fsString
*);
409 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
411 header
.size
= retval
;
412 header
.type
= T_STATFS
;
416 path
= va_arg(ap
, V9fsString
*);
417 mode
= va_arg(ap
, int);
418 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, mode
);
420 header
.size
= retval
;
421 header
.type
= T_CHMOD
;
425 path
= va_arg(ap
, V9fsString
*);
426 uid
= va_arg(ap
, int);
427 gid
= va_arg(ap
, int);
428 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdd", path
, uid
, gid
);
430 header
.size
= retval
;
431 header
.type
= T_CHOWN
;
435 path
= va_arg(ap
, V9fsString
*);
436 offset
= va_arg(ap
, uint64_t);
437 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sq", path
, offset
);
439 header
.size
= retval
;
440 header
.type
= T_TRUNCATE
;
444 path
= va_arg(ap
, V9fsString
*);
445 spec
[0].tv_sec
= va_arg(ap
, long);
446 spec
[0].tv_nsec
= va_arg(ap
, long);
447 spec
[1].tv_sec
= va_arg(ap
, long);
448 spec
[1].tv_nsec
= va_arg(ap
, long);
449 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sqqqq", path
,
450 spec
[0].tv_sec
, spec
[1].tv_nsec
,
451 spec
[1].tv_sec
, spec
[1].tv_nsec
);
453 header
.size
= retval
;
454 header
.type
= T_UTIME
;
458 oldpath
= va_arg(ap
, V9fsString
*);
459 path
= va_arg(ap
, V9fsString
*);
460 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", oldpath
, path
);
462 header
.size
= retval
;
463 header
.type
= T_RENAME
;
467 path
= va_arg(ap
, V9fsString
*);
468 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
470 header
.size
= retval
;
471 header
.type
= T_REMOVE
;
475 size
= va_arg(ap
, int);
476 path
= va_arg(ap
, V9fsString
*);
477 name
= va_arg(ap
, V9fsString
*);
478 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
,
479 "dss", size
, path
, name
);
481 header
.size
= retval
;
482 header
.type
= T_LGETXATTR
;
486 size
= va_arg(ap
, int);
487 path
= va_arg(ap
, V9fsString
*);
488 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ds", size
, path
);
490 header
.size
= retval
;
491 header
.type
= T_LLISTXATTR
;
495 path
= va_arg(ap
, V9fsString
*);
496 name
= va_arg(ap
, V9fsString
*);
497 value
= va_arg(ap
, V9fsString
*);
498 size
= va_arg(ap
, int);
499 flags
= va_arg(ap
, int);
500 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sssdd",
501 path
, name
, value
, size
, flags
);
503 header
.size
= retval
;
504 header
.type
= T_LSETXATTR
;
508 path
= va_arg(ap
, V9fsString
*);
509 name
= va_arg(ap
, V9fsString
*);
510 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", path
, name
);
512 header
.size
= retval
;
513 header
.type
= T_LREMOVEXATTR
;
517 path
= va_arg(ap
, V9fsString
*);
518 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
520 header
.size
= retval
;
521 header
.type
= T_GETVERSION
;
525 error_report("Invalid type %d", type
);
535 /* marshal the header details */
536 proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
537 header
.size
+= PROXY_HDR_SZ
;
539 retval
= qemu_write_full(proxy
->sockfd
, iovec
->iov_base
, header
.size
);
540 if (retval
!= header
.size
) {
548 * A file descriptor is returned as response for
549 * T_OPEN,T_CREATE on success
551 if (v9fs_receivefd(proxy
->sockfd
, &retval
) < 0) {
567 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
575 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
582 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
586 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
594 qemu_mutex_unlock(&proxy
->mutex
);
598 close(proxy
->sockfd
);
600 qemu_mutex_unlock(&proxy
->mutex
);
604 static int proxy_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
607 retval
= v9fs_request(fs_ctx
->private, T_LSTAT
, stbuf
, "s", fs_path
);
615 static ssize_t
proxy_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
616 char *buf
, size_t bufsz
)
619 retval
= v9fs_request(fs_ctx
->private, T_READLINK
, buf
, "sd",
628 static int proxy_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
630 return close(fs
->fd
);
633 static int proxy_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
635 return closedir(fs
->dir
);
638 static int proxy_open(FsContext
*ctx
, V9fsPath
*fs_path
,
639 int flags
, V9fsFidOpenState
*fs
)
641 fs
->fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, "sd", fs_path
, flags
);
649 static int proxy_opendir(FsContext
*ctx
,
650 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
655 fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, "sd", fs_path
, O_DIRECTORY
);
660 fs
->dir
= fdopendir(fd
);
670 static void proxy_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
672 return rewinddir(fs
->dir
);
675 static off_t
proxy_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
677 return telldir(fs
->dir
);
680 static int proxy_readdir_r(FsContext
*ctx
, V9fsFidOpenState
*fs
,
681 struct dirent
*entry
,
682 struct dirent
**result
)
684 return readdir_r(fs
->dir
, entry
, result
);
687 static void proxy_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
689 return seekdir(fs
->dir
, off
);
692 static ssize_t
proxy_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
693 const struct iovec
*iov
,
694 int iovcnt
, off_t offset
)
697 return preadv(fs
->fd
, iov
, iovcnt
, offset
);
699 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
703 return readv(fs
->fd
, iov
, iovcnt
);
708 static ssize_t
proxy_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
709 const struct iovec
*iov
,
710 int iovcnt
, off_t offset
)
715 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
717 int err
= lseek(fs
->fd
, offset
, SEEK_SET
);
721 ret
= writev(fs
->fd
, iov
, iovcnt
);
724 #ifdef CONFIG_SYNC_FILE_RANGE
725 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
727 * Initiate a writeback. This is not a data integrity sync.
728 * We want to ensure that we don't leave dirty pages in the cache
729 * after write when writeout=immediate is sepcified.
731 sync_file_range(fs
->fd
, offset
, ret
,
732 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
738 static int proxy_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
741 retval
= v9fs_request(fs_ctx
->private, T_CHMOD
, NULL
, "sd",
742 fs_path
, credp
->fc_mode
);
749 static int proxy_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
750 const char *name
, FsCred
*credp
)
755 v9fs_string_init(&fullname
);
756 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
758 retval
= v9fs_request(fs_ctx
->private, T_MKNOD
, NULL
, "sdqdd",
759 &fullname
, credp
->fc_mode
, credp
->fc_rdev
,
760 credp
->fc_uid
, credp
->fc_gid
);
761 v9fs_string_free(&fullname
);
769 static int proxy_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
770 const char *name
, FsCred
*credp
)
775 v9fs_string_init(&fullname
);
776 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
778 retval
= v9fs_request(fs_ctx
->private, T_MKDIR
, NULL
, "sddd", &fullname
,
779 credp
->fc_mode
, credp
->fc_uid
, credp
->fc_gid
);
780 v9fs_string_free(&fullname
);
785 v9fs_string_free(&fullname
);
789 static int proxy_fstat(FsContext
*fs_ctx
, int fid_type
,
790 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
794 if (fid_type
== P9_FID_DIR
) {
799 return fstat(fd
, stbuf
);
802 static int proxy_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
803 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
807 v9fs_string_init(&fullname
);
808 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
810 fs
->fd
= v9fs_request(fs_ctx
->private, T_CREATE
, NULL
, "sdddd",
811 &fullname
, flags
, credp
->fc_mode
,
812 credp
->fc_uid
, credp
->fc_gid
);
813 v9fs_string_free(&fullname
);
821 static int proxy_symlink(FsContext
*fs_ctx
, const char *oldpath
,
822 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
825 V9fsString fullname
, target
;
827 v9fs_string_init(&fullname
);
828 v9fs_string_init(&target
);
830 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
831 v9fs_string_sprintf(&target
, "%s", oldpath
);
833 retval
= v9fs_request(fs_ctx
->private, T_SYMLINK
, NULL
, "ssdd",
834 &target
, &fullname
, credp
->fc_uid
, credp
->fc_gid
);
835 v9fs_string_free(&fullname
);
836 v9fs_string_free(&target
);
844 static int proxy_link(FsContext
*ctx
, V9fsPath
*oldpath
,
845 V9fsPath
*dirpath
, const char *name
)
850 v9fs_string_init(&newpath
);
851 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
853 retval
= v9fs_request(ctx
->private, T_LINK
, NULL
, "ss", oldpath
, &newpath
);
854 v9fs_string_free(&newpath
);
862 static int proxy_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
866 retval
= v9fs_request(ctx
->private, T_TRUNCATE
, NULL
, "sq", fs_path
, size
);
874 static int proxy_rename(FsContext
*ctx
, const char *oldpath
,
878 V9fsString oldname
, newname
;
880 v9fs_string_init(&oldname
);
881 v9fs_string_init(&newname
);
883 v9fs_string_sprintf(&oldname
, "%s", oldpath
);
884 v9fs_string_sprintf(&newname
, "%s", newpath
);
885 retval
= v9fs_request(ctx
->private, T_RENAME
, NULL
, "ss",
887 v9fs_string_free(&oldname
);
888 v9fs_string_free(&newname
);
895 static int proxy_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
898 retval
= v9fs_request(fs_ctx
->private, T_CHOWN
, NULL
, "sdd",
899 fs_path
, credp
->fc_uid
, credp
->fc_gid
);
906 static int proxy_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
907 const struct timespec
*buf
)
910 retval
= v9fs_request(s
->private, T_UTIME
, NULL
, "sqqqq",
912 buf
[0].tv_sec
, buf
[0].tv_nsec
,
913 buf
[1].tv_sec
, buf
[1].tv_nsec
);
920 static int proxy_remove(FsContext
*ctx
, const char *path
)
924 v9fs_string_init(&name
);
925 v9fs_string_sprintf(&name
, "%s", path
);
926 retval
= v9fs_request(ctx
->private, T_REMOVE
, NULL
, "s", &name
);
927 v9fs_string_free(&name
);
934 static int proxy_fsync(FsContext
*ctx
, int fid_type
,
935 V9fsFidOpenState
*fs
, int datasync
)
939 if (fid_type
== P9_FID_DIR
) {
946 return qemu_fdatasync(fd
);
952 static int proxy_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
955 retval
= v9fs_request(s
->private, T_STATFS
, stbuf
, "s", fs_path
);
963 static ssize_t
proxy_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
964 const char *name
, void *value
, size_t size
)
969 v9fs_string_init(&xname
);
970 v9fs_string_sprintf(&xname
, "%s", name
);
971 retval
= v9fs_request(ctx
->private, T_LGETXATTR
, value
, "dss", size
,
973 v9fs_string_free(&xname
);
980 static ssize_t
proxy_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
981 void *value
, size_t size
)
984 retval
= v9fs_request(ctx
->private, T_LLISTXATTR
, value
, "ds", size
,
992 static int proxy_lsetxattr(FsContext
*ctx
, V9fsPath
*fs_path
, const char *name
,
993 void *value
, size_t size
, int flags
)
996 V9fsString xname
, xvalue
;
998 v9fs_string_init(&xname
);
999 v9fs_string_sprintf(&xname
, "%s", name
);
1001 v9fs_string_init(&xvalue
);
1003 xvalue
.data
= g_malloc(size
);
1004 memcpy(xvalue
.data
, value
, size
);
1006 retval
= v9fs_request(ctx
->private, T_LSETXATTR
, value
, "sssdd",
1007 fs_path
, &xname
, &xvalue
, size
, flags
);
1008 v9fs_string_free(&xname
);
1009 v9fs_string_free(&xvalue
);
1016 static int proxy_lremovexattr(FsContext
*ctx
, V9fsPath
*fs_path
,
1022 v9fs_string_init(&xname
);
1023 v9fs_string_sprintf(&xname
, "%s", name
);
1024 retval
= v9fs_request(ctx
->private, T_LREMOVEXATTR
, NULL
, "ss",
1026 v9fs_string_free(&xname
);
1033 static int proxy_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1034 const char *name
, V9fsPath
*target
)
1037 v9fs_string_sprintf((V9fsString
*)target
, "%s/%s",
1038 dir_path
->data
, name
);
1040 v9fs_string_sprintf((V9fsString
*)target
, "%s", name
);
1042 /* Bump the size for including terminating NULL */
1047 static int proxy_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1048 const char *old_name
, V9fsPath
*newdir
,
1049 const char *new_name
)
1052 V9fsString old_full_name
, new_full_name
;
1054 v9fs_string_init(&old_full_name
);
1055 v9fs_string_init(&new_full_name
);
1057 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1058 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1060 ret
= proxy_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1061 v9fs_string_free(&old_full_name
);
1062 v9fs_string_free(&new_full_name
);
1066 static int proxy_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1067 const char *name
, int flags
)
1070 V9fsString fullname
;
1071 v9fs_string_init(&fullname
);
1073 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1074 ret
= proxy_remove(ctx
, fullname
.data
);
1075 v9fs_string_free(&fullname
);
1080 static int proxy_ioc_getversion(FsContext
*fs_ctx
, V9fsPath
*path
,
1081 mode_t st_mode
, uint64_t *st_gen
)
1085 /* Do not try to open special files like device nodes, fifos etc
1086 * we can get fd for regular files and directories only
1088 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1091 err
= v9fs_request(fs_ctx
->private, T_GETVERSION
, st_gen
, "s", path
);
1099 static int connect_namedsocket(const char *path
)
1102 struct sockaddr_un helper
;
1104 sockfd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1106 fprintf(stderr
, "socket %s\n", strerror(errno
));
1109 strcpy(helper
.sun_path
, path
);
1110 helper
.sun_family
= AF_UNIX
;
1111 size
= strlen(helper
.sun_path
) + sizeof(helper
.sun_family
);
1112 if (connect(sockfd
, (struct sockaddr
*)&helper
, size
) < 0) {
1113 fprintf(stderr
, "socket error\n");
1117 /* remove the socket for security reasons */
1122 static int proxy_parse_opts(QemuOpts
*opts
, struct FsDriverEntry
*fs
)
1124 const char *socket
= qemu_opt_get(opts
, "socket");
1125 const char *sock_fd
= qemu_opt_get(opts
, "sock_fd");
1127 if (!socket
&& !sock_fd
) {
1128 fprintf(stderr
, "socket and sock_fd none of the option specified\n");
1131 if (socket
&& sock_fd
) {
1132 fprintf(stderr
, "Both socket and sock_fd options specified\n");
1136 fs
->path
= g_strdup(socket
);
1137 fs
->export_flags
= V9FS_PROXY_SOCK_NAME
;
1139 fs
->path
= g_strdup(sock_fd
);
1140 fs
->export_flags
= V9FS_PROXY_SOCK_FD
;
1145 static int proxy_init(FsContext
*ctx
)
1147 V9fsProxy
*proxy
= g_malloc(sizeof(V9fsProxy
));
1150 if (ctx
->export_flags
& V9FS_PROXY_SOCK_NAME
) {
1151 sock_id
= connect_namedsocket(ctx
->fs_root
);
1153 sock_id
= atoi(ctx
->fs_root
);
1155 fprintf(stderr
, "socket descriptor not initialized\n");
1159 g_free(ctx
->fs_root
);
1161 proxy
->in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1162 proxy
->in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1163 proxy
->out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1164 proxy
->out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1166 ctx
->private = proxy
;
1167 proxy
->sockfd
= sock_id
;
1168 qemu_mutex_init(&proxy
->mutex
);
1170 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1171 ctx
->exops
.get_st_gen
= proxy_ioc_getversion
;
1175 FileOperations proxy_ops
= {
1176 .parse_opts
= proxy_parse_opts
,
1178 .lstat
= proxy_lstat
,
1179 .readlink
= proxy_readlink
,
1180 .close
= proxy_close
,
1181 .closedir
= proxy_closedir
,
1183 .opendir
= proxy_opendir
,
1184 .rewinddir
= proxy_rewinddir
,
1185 .telldir
= proxy_telldir
,
1186 .readdir_r
= proxy_readdir_r
,
1187 .seekdir
= proxy_seekdir
,
1188 .preadv
= proxy_preadv
,
1189 .pwritev
= proxy_pwritev
,
1190 .chmod
= proxy_chmod
,
1191 .mknod
= proxy_mknod
,
1192 .mkdir
= proxy_mkdir
,
1193 .fstat
= proxy_fstat
,
1194 .open2
= proxy_open2
,
1195 .symlink
= proxy_symlink
,
1197 .truncate
= proxy_truncate
,
1198 .rename
= proxy_rename
,
1199 .chown
= proxy_chown
,
1200 .utimensat
= proxy_utimensat
,
1201 .remove
= proxy_remove
,
1202 .fsync
= proxy_fsync
,
1203 .statfs
= proxy_statfs
,
1204 .lgetxattr
= proxy_lgetxattr
,
1205 .llistxattr
= proxy_llistxattr
,
1206 .lsetxattr
= proxy_lsetxattr
,
1207 .lremovexattr
= proxy_lremovexattr
,
1208 .name_to_path
= proxy_name_to_path
,
1209 .renameat
= proxy_renameat
,
1210 .unlinkat
= proxy_unlinkat
,