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.
14 * Not so fast! You might want to read the 9p developer docs first:
15 * https://wiki.qemu.org/Documentation/9p
18 #include "qemu/osdep.h"
19 #include <sys/socket.h>
21 #include "qemu-common.h"
23 #include "qapi/error.h"
24 #include "qemu/cutils.h"
25 #include "qemu/error-report.h"
26 #include "qemu/option.h"
27 #include "fsdev/qemu-fsdev.h"
30 typedef struct V9fsProxy
{
33 struct iovec in_iovec
;
34 struct iovec out_iovec
;
38 * Return received file descriptor on success in *status.
39 * errno is also returned on *status (which will be < 0)
40 * return < 0 on transport error.
42 static int v9fs_receivefd(int sockfd
, int *status
)
48 union MsgControl msg_control
;
51 iov
.iov_len
= sizeof(data
);
53 memset(&msg
, 0, sizeof(msg
));
56 msg
.msg_control
= &msg_control
;
57 msg
.msg_controllen
= sizeof(msg_control
);
60 retval
= recvmsg(sockfd
, &msg
, 0);
61 } while (retval
< 0 && errno
== EINTR
);
66 * data is set to V9FS_FD_VALID, if ancillary data is sent. If this
67 * request doesn't need ancillary data (fd) or an error occurred,
68 * data is set to negative errno value.
70 if (data
!= V9FS_FD_VALID
) {
75 * File descriptor (fd) is sent in the ancillary data. Check if we
76 * indeed received it. One of the reasons to fail to receive it is if
77 * we exceeded the maximum number of file descriptors!
79 for (cmsg
= CMSG_FIRSTHDR(&msg
); cmsg
; cmsg
= CMSG_NXTHDR(&msg
, cmsg
)) {
80 if (cmsg
->cmsg_len
!= CMSG_LEN(sizeof(int)) ||
81 cmsg
->cmsg_level
!= SOL_SOCKET
||
82 cmsg
->cmsg_type
!= SCM_RIGHTS
) {
85 fd
= *((int *)CMSG_DATA(cmsg
));
89 *status
= -ENFILE
; /* Ancillary data sent but not received */
93 static ssize_t
socket_read(int sockfd
, void *buff
, size_t size
)
95 ssize_t retval
, total
= 0;
98 retval
= read(sockfd
, buff
, size
);
103 if (errno
== EINTR
) {
115 /* Converts proxy_statfs to VFS statfs structure */
116 static void prstatfs_to_statfs(struct statfs
*stfs
, ProxyStatFS
*prstfs
)
118 memset(stfs
, 0, sizeof(*stfs
));
119 stfs
->f_type
= prstfs
->f_type
;
120 stfs
->f_bsize
= prstfs
->f_bsize
;
121 stfs
->f_blocks
= prstfs
->f_blocks
;
122 stfs
->f_bfree
= prstfs
->f_bfree
;
123 stfs
->f_bavail
= prstfs
->f_bavail
;
124 stfs
->f_files
= prstfs
->f_files
;
125 stfs
->f_ffree
= prstfs
->f_ffree
;
126 stfs
->f_fsid
.__val
[0] = prstfs
->f_fsid
[0] & 0xFFFFFFFFU
;
127 stfs
->f_fsid
.__val
[1] = prstfs
->f_fsid
[1] >> 32 & 0xFFFFFFFFU
;
128 stfs
->f_namelen
= prstfs
->f_namelen
;
129 stfs
->f_frsize
= prstfs
->f_frsize
;
132 /* Converts proxy_stat structure to VFS stat structure */
133 static void prstat_to_stat(struct stat
*stbuf
, ProxyStat
*prstat
)
135 memset(stbuf
, 0, sizeof(*stbuf
));
136 stbuf
->st_dev
= prstat
->st_dev
;
137 stbuf
->st_ino
= prstat
->st_ino
;
138 stbuf
->st_nlink
= prstat
->st_nlink
;
139 stbuf
->st_mode
= prstat
->st_mode
;
140 stbuf
->st_uid
= prstat
->st_uid
;
141 stbuf
->st_gid
= prstat
->st_gid
;
142 stbuf
->st_rdev
= prstat
->st_rdev
;
143 stbuf
->st_size
= prstat
->st_size
;
144 stbuf
->st_blksize
= prstat
->st_blksize
;
145 stbuf
->st_blocks
= prstat
->st_blocks
;
146 stbuf
->st_atim
.tv_sec
= prstat
->st_atim_sec
;
147 stbuf
->st_atim
.tv_nsec
= prstat
->st_atim_nsec
;
148 stbuf
->st_mtime
= prstat
->st_mtim_sec
;
149 stbuf
->st_mtim
.tv_nsec
= prstat
->st_mtim_nsec
;
150 stbuf
->st_ctime
= prstat
->st_ctim_sec
;
151 stbuf
->st_ctim
.tv_nsec
= prstat
->st_ctim_nsec
;
155 * Response contains two parts
157 * header.type == T_ERROR, data -> -errno
158 * header.type == T_SUCCESS, data -> response
159 * size of errno/response is given by header.size
160 * returns < 0, on transport error. response is
161 * valid only if status >= 0.
163 static int v9fs_receive_response(V9fsProxy
*proxy
, int type
,
164 int *status
, void *response
)
168 struct iovec
*reply
= &proxy
->in_iovec
;
172 retval
= socket_read(proxy
->sockfd
, reply
->iov_base
, PROXY_HDR_SZ
);
176 reply
->iov_len
= PROXY_HDR_SZ
;
177 retval
= proxy_unmarshal(reply
, 0, "dd", &header
.type
, &header
.size
);
178 assert(retval
== 4 * 2);
180 * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
183 if (header
.size
> PROXY_MAX_IO_SZ
) {
185 while (header
.size
> 0) {
186 count
= MIN(PROXY_MAX_IO_SZ
, header
.size
);
187 count
= socket_read(proxy
->sockfd
, reply
->iov_base
, count
);
191 header
.size
-= count
;
197 retval
= socket_read(proxy
->sockfd
,
198 reply
->iov_base
+ PROXY_HDR_SZ
, header
.size
);
202 reply
->iov_len
+= header
.size
;
203 /* there was an error during processing request */
204 if (header
.type
== T_ERROR
) {
206 ret
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "d", status
);
214 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
,
215 "qqqdddqqqqqqqqqq", &prstat
.st_dev
,
216 &prstat
.st_ino
, &prstat
.st_nlink
,
217 &prstat
.st_mode
, &prstat
.st_uid
,
218 &prstat
.st_gid
, &prstat
.st_rdev
,
219 &prstat
.st_size
, &prstat
.st_blksize
,
221 &prstat
.st_atim_sec
, &prstat
.st_atim_nsec
,
222 &prstat
.st_mtim_sec
, &prstat
.st_mtim_nsec
,
223 &prstat
.st_ctim_sec
, &prstat
.st_ctim_nsec
);
224 assert(retval
== 8 * 3 + 4 * 3 + 8 * 10);
225 prstat_to_stat(response
, &prstat
);
230 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
,
231 "qqqqqqqqqqq", &prstfs
.f_type
,
232 &prstfs
.f_bsize
, &prstfs
.f_blocks
,
233 &prstfs
.f_bfree
, &prstfs
.f_bavail
,
234 &prstfs
.f_files
, &prstfs
.f_ffree
,
235 &prstfs
.f_fsid
[0], &prstfs
.f_fsid
[1],
236 &prstfs
.f_namelen
, &prstfs
.f_frsize
);
237 assert(retval
== 8 * 11);
238 prstatfs_to_statfs(response
, &prstfs
);
243 v9fs_string_init(&target
);
244 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "s", &target
);
245 strcpy(response
, target
.data
);
246 v9fs_string_free(&target
);
252 v9fs_string_init(&xattr
);
253 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "s", &xattr
);
254 memcpy(response
, xattr
.data
, xattr
.size
);
255 v9fs_string_free(&xattr
);
259 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "q", response
);
272 * return < 0 on transport error.
273 * *status is valid only if return >= 0
275 static int v9fs_receive_status(V9fsProxy
*proxy
,
276 struct iovec
*reply
, int *status
)
283 retval
= socket_read(proxy
->sockfd
, reply
->iov_base
, PROXY_HDR_SZ
);
287 reply
->iov_len
= PROXY_HDR_SZ
;
288 retval
= proxy_unmarshal(reply
, 0, "dd", &header
.type
, &header
.size
);
289 assert(retval
== 4 * 2);
290 retval
= socket_read(proxy
->sockfd
,
291 reply
->iov_base
+ PROXY_HDR_SZ
, header
.size
);
295 reply
->iov_len
+= header
.size
;
296 retval
= proxy_unmarshal(reply
, PROXY_HDR_SZ
, "d", status
);
302 * Proxy->header and proxy->request written to socket by QEMU process.
303 * This request read by proxy helper process
304 * returns 0 on success and -errno on error
306 static int v9fs_request(V9fsProxy
*proxy
, int type
, void *response
, ...)
313 ProxyHeader header
= { 0, 0};
314 struct timespec spec
[2];
315 int flags
, mode
, uid
, gid
;
316 V9fsString
*name
, *value
;
317 V9fsString
*path
, *oldpath
;
318 struct iovec
*iovec
= NULL
, *reply
= NULL
;
320 qemu_mutex_lock(&proxy
->mutex
);
322 if (proxy
->sockfd
== -1) {
326 iovec
= &proxy
->out_iovec
;
327 reply
= &proxy
->in_iovec
;
328 va_start(ap
, response
);
331 path
= va_arg(ap
, V9fsString
*);
332 flags
= va_arg(ap
, int);
333 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, flags
);
335 header
.size
= retval
;
336 header
.type
= T_OPEN
;
340 path
= va_arg(ap
, V9fsString
*);
341 flags
= va_arg(ap
, int);
342 mode
= va_arg(ap
, int);
343 uid
= va_arg(ap
, int);
344 gid
= va_arg(ap
, int);
345 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdddd", path
,
346 flags
, mode
, uid
, gid
);
348 header
.size
= retval
;
349 header
.type
= T_CREATE
;
353 path
= va_arg(ap
, V9fsString
*);
354 mode
= va_arg(ap
, int);
355 rdev
= va_arg(ap
, long int);
356 uid
= va_arg(ap
, int);
357 gid
= va_arg(ap
, int);
358 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsdq",
359 uid
, gid
, path
, mode
, rdev
);
361 header
.size
= retval
;
362 header
.type
= T_MKNOD
;
366 path
= va_arg(ap
, V9fsString
*);
367 mode
= va_arg(ap
, int);
368 uid
= va_arg(ap
, int);
369 gid
= va_arg(ap
, int);
370 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddsd",
371 uid
, gid
, path
, mode
);
373 header
.size
= retval
;
374 header
.type
= T_MKDIR
;
378 oldpath
= va_arg(ap
, V9fsString
*);
379 path
= va_arg(ap
, V9fsString
*);
380 uid
= va_arg(ap
, int);
381 gid
= va_arg(ap
, int);
382 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ddss",
383 uid
, gid
, oldpath
, path
);
385 header
.size
= retval
;
386 header
.type
= T_SYMLINK
;
390 oldpath
= va_arg(ap
, V9fsString
*);
391 path
= va_arg(ap
, V9fsString
*);
392 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss",
395 header
.size
= retval
;
396 header
.type
= T_LINK
;
400 path
= va_arg(ap
, V9fsString
*);
401 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
403 header
.size
= retval
;
404 header
.type
= T_LSTAT
;
408 path
= va_arg(ap
, V9fsString
*);
409 size
= va_arg(ap
, int);
410 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, size
);
412 header
.size
= retval
;
413 header
.type
= T_READLINK
;
417 path
= va_arg(ap
, V9fsString
*);
418 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
420 header
.size
= retval
;
421 header
.type
= T_STATFS
;
425 path
= va_arg(ap
, V9fsString
*);
426 mode
= va_arg(ap
, int);
427 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sd", path
, mode
);
429 header
.size
= retval
;
430 header
.type
= T_CHMOD
;
434 path
= va_arg(ap
, V9fsString
*);
435 uid
= va_arg(ap
, int);
436 gid
= va_arg(ap
, int);
437 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sdd", path
, uid
, gid
);
439 header
.size
= retval
;
440 header
.type
= T_CHOWN
;
444 path
= va_arg(ap
, V9fsString
*);
445 offset
= va_arg(ap
, uint64_t);
446 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sq", path
, offset
);
448 header
.size
= retval
;
449 header
.type
= T_TRUNCATE
;
453 path
= va_arg(ap
, V9fsString
*);
454 spec
[0].tv_sec
= va_arg(ap
, long);
455 spec
[0].tv_nsec
= va_arg(ap
, long);
456 spec
[1].tv_sec
= va_arg(ap
, long);
457 spec
[1].tv_nsec
= va_arg(ap
, long);
458 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sqqqq", path
,
459 spec
[0].tv_sec
, spec
[1].tv_nsec
,
460 spec
[1].tv_sec
, spec
[1].tv_nsec
);
462 header
.size
= retval
;
463 header
.type
= T_UTIME
;
467 oldpath
= va_arg(ap
, V9fsString
*);
468 path
= va_arg(ap
, V9fsString
*);
469 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", oldpath
, path
);
471 header
.size
= retval
;
472 header
.type
= T_RENAME
;
476 path
= va_arg(ap
, V9fsString
*);
477 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
479 header
.size
= retval
;
480 header
.type
= T_REMOVE
;
484 size
= va_arg(ap
, int);
485 path
= va_arg(ap
, V9fsString
*);
486 name
= va_arg(ap
, V9fsString
*);
487 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
,
488 "dss", size
, path
, name
);
490 header
.size
= retval
;
491 header
.type
= T_LGETXATTR
;
495 size
= va_arg(ap
, int);
496 path
= va_arg(ap
, V9fsString
*);
497 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ds", size
, path
);
499 header
.size
= retval
;
500 header
.type
= T_LLISTXATTR
;
504 path
= va_arg(ap
, V9fsString
*);
505 name
= va_arg(ap
, V9fsString
*);
506 value
= va_arg(ap
, V9fsString
*);
507 size
= va_arg(ap
, int);
508 flags
= va_arg(ap
, int);
509 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "sssdd",
510 path
, name
, value
, size
, flags
);
512 header
.size
= retval
;
513 header
.type
= T_LSETXATTR
;
517 path
= va_arg(ap
, V9fsString
*);
518 name
= va_arg(ap
, V9fsString
*);
519 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "ss", path
, name
);
521 header
.size
= retval
;
522 header
.type
= T_LREMOVEXATTR
;
526 path
= va_arg(ap
, V9fsString
*);
527 retval
= proxy_marshal(iovec
, PROXY_HDR_SZ
, "s", path
);
529 header
.size
= retval
;
530 header
.type
= T_GETVERSION
;
534 error_report("Invalid type %d", type
);
544 /* marshal the header details */
545 retval
= proxy_marshal(iovec
, 0, "dd", header
.type
, header
.size
);
546 assert(retval
== 4 * 2);
547 header
.size
+= PROXY_HDR_SZ
;
549 retval
= qemu_write_full(proxy
->sockfd
, iovec
->iov_base
, header
.size
);
550 if (retval
!= header
.size
) {
558 * A file descriptor is returned as response for
559 * T_OPEN,T_CREATE on success
561 if (v9fs_receivefd(proxy
->sockfd
, &retval
) < 0) {
577 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
585 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
592 if (v9fs_receive_status(proxy
, reply
, &retval
) < 0) {
596 if (v9fs_receive_response(proxy
, type
, &retval
, response
) < 0) {
604 qemu_mutex_unlock(&proxy
->mutex
);
608 close(proxy
->sockfd
);
610 qemu_mutex_unlock(&proxy
->mutex
);
614 static int proxy_lstat(FsContext
*fs_ctx
, V9fsPath
*fs_path
, struct stat
*stbuf
)
617 retval
= v9fs_request(fs_ctx
->private, T_LSTAT
, stbuf
, fs_path
);
625 static ssize_t
proxy_readlink(FsContext
*fs_ctx
, V9fsPath
*fs_path
,
626 char *buf
, size_t bufsz
)
629 retval
= v9fs_request(fs_ctx
->private, T_READLINK
, buf
, fs_path
, bufsz
);
637 static int proxy_close(FsContext
*ctx
, V9fsFidOpenState
*fs
)
639 return close(fs
->fd
);
642 static int proxy_closedir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
644 return closedir(fs
->dir
.stream
);
647 static int proxy_open(FsContext
*ctx
, V9fsPath
*fs_path
,
648 int flags
, V9fsFidOpenState
*fs
)
650 fs
->fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, fs_path
, flags
);
658 static int proxy_opendir(FsContext
*ctx
,
659 V9fsPath
*fs_path
, V9fsFidOpenState
*fs
)
663 fs
->dir
.stream
= NULL
;
664 fd
= v9fs_request(ctx
->private, T_OPEN
, NULL
, fs_path
, O_DIRECTORY
);
669 fs
->dir
.stream
= fdopendir(fd
);
670 if (!fs
->dir
.stream
) {
679 static void proxy_rewinddir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
681 rewinddir(fs
->dir
.stream
);
684 static off_t
proxy_telldir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
686 return telldir(fs
->dir
.stream
);
689 static struct dirent
*proxy_readdir(FsContext
*ctx
, V9fsFidOpenState
*fs
)
691 return readdir(fs
->dir
.stream
);
694 static void proxy_seekdir(FsContext
*ctx
, V9fsFidOpenState
*fs
, off_t off
)
696 seekdir(fs
->dir
.stream
, off
);
699 static ssize_t
proxy_preadv(FsContext
*ctx
, V9fsFidOpenState
*fs
,
700 const struct iovec
*iov
,
701 int iovcnt
, off_t offset
)
705 ret
= preadv(fs
->fd
, iov
, iovcnt
, offset
);
707 ret
= lseek(fs
->fd
, offset
, SEEK_SET
);
709 ret
= readv(fs
->fd
, iov
, iovcnt
);
715 static ssize_t
proxy_pwritev(FsContext
*ctx
, V9fsFidOpenState
*fs
,
716 const struct iovec
*iov
,
717 int iovcnt
, off_t offset
)
722 ret
= pwritev(fs
->fd
, iov
, iovcnt
, offset
);
724 ret
= lseek(fs
->fd
, offset
, SEEK_SET
);
726 ret
= writev(fs
->fd
, iov
, iovcnt
);
729 #ifdef CONFIG_SYNC_FILE_RANGE
730 if (ret
> 0 && ctx
->export_flags
& V9FS_IMMEDIATE_WRITEOUT
) {
732 * Initiate a writeback. This is not a data integrity sync.
733 * We want to ensure that we don't leave dirty pages in the cache
734 * after write when writeout=immediate is sepcified.
736 sync_file_range(fs
->fd
, offset
, ret
,
737 SYNC_FILE_RANGE_WAIT_BEFORE
| SYNC_FILE_RANGE_WRITE
);
743 static int proxy_chmod(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
746 retval
= v9fs_request(fs_ctx
->private, T_CHMOD
, NULL
, fs_path
,
754 static int proxy_mknod(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
755 const char *name
, FsCred
*credp
)
760 v9fs_string_init(&fullname
);
761 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
763 retval
= v9fs_request(fs_ctx
->private, T_MKNOD
, NULL
, &fullname
,
764 credp
->fc_mode
, credp
->fc_rdev
,
765 credp
->fc_uid
, credp
->fc_gid
);
766 v9fs_string_free(&fullname
);
774 static int proxy_mkdir(FsContext
*fs_ctx
, V9fsPath
*dir_path
,
775 const char *name
, FsCred
*credp
)
780 v9fs_string_init(&fullname
);
781 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
783 retval
= v9fs_request(fs_ctx
->private, T_MKDIR
, NULL
, &fullname
,
784 credp
->fc_mode
, credp
->fc_uid
, credp
->fc_gid
);
785 v9fs_string_free(&fullname
);
793 static int proxy_fstat(FsContext
*fs_ctx
, int fid_type
,
794 V9fsFidOpenState
*fs
, struct stat
*stbuf
)
798 if (fid_type
== P9_FID_DIR
) {
799 fd
= dirfd(fs
->dir
.stream
);
803 return fstat(fd
, stbuf
);
806 static int proxy_open2(FsContext
*fs_ctx
, V9fsPath
*dir_path
, const char *name
,
807 int flags
, FsCred
*credp
, V9fsFidOpenState
*fs
)
811 v9fs_string_init(&fullname
);
812 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
814 fs
->fd
= v9fs_request(fs_ctx
->private, T_CREATE
, NULL
, &fullname
, flags
,
815 credp
->fc_mode
, credp
->fc_uid
, credp
->fc_gid
);
816 v9fs_string_free(&fullname
);
824 static int proxy_symlink(FsContext
*fs_ctx
, const char *oldpath
,
825 V9fsPath
*dir_path
, const char *name
, FsCred
*credp
)
828 V9fsString fullname
, target
;
830 v9fs_string_init(&fullname
);
831 v9fs_string_init(&target
);
833 v9fs_string_sprintf(&fullname
, "%s/%s", dir_path
->data
, name
);
834 v9fs_string_sprintf(&target
, "%s", oldpath
);
836 retval
= v9fs_request(fs_ctx
->private, T_SYMLINK
, NULL
, &target
, &fullname
,
837 credp
->fc_uid
, credp
->fc_gid
);
838 v9fs_string_free(&fullname
);
839 v9fs_string_free(&target
);
847 static int proxy_link(FsContext
*ctx
, V9fsPath
*oldpath
,
848 V9fsPath
*dirpath
, const char *name
)
853 v9fs_string_init(&newpath
);
854 v9fs_string_sprintf(&newpath
, "%s/%s", dirpath
->data
, name
);
856 retval
= v9fs_request(ctx
->private, T_LINK
, NULL
, oldpath
, &newpath
);
857 v9fs_string_free(&newpath
);
865 static int proxy_truncate(FsContext
*ctx
, V9fsPath
*fs_path
, off_t size
)
869 retval
= v9fs_request(ctx
->private, T_TRUNCATE
, NULL
, fs_path
, size
);
877 static int proxy_rename(FsContext
*ctx
, const char *oldpath
,
881 V9fsString oldname
, newname
;
883 v9fs_string_init(&oldname
);
884 v9fs_string_init(&newname
);
886 v9fs_string_sprintf(&oldname
, "%s", oldpath
);
887 v9fs_string_sprintf(&newname
, "%s", newpath
);
888 retval
= v9fs_request(ctx
->private, T_RENAME
, NULL
, &oldname
, &newname
);
889 v9fs_string_free(&oldname
);
890 v9fs_string_free(&newname
);
897 static int proxy_chown(FsContext
*fs_ctx
, V9fsPath
*fs_path
, FsCred
*credp
)
900 retval
= v9fs_request(fs_ctx
->private, T_CHOWN
, NULL
, fs_path
,
901 credp
->fc_uid
, credp
->fc_gid
);
908 static int proxy_utimensat(FsContext
*s
, V9fsPath
*fs_path
,
909 const struct timespec
*buf
)
912 retval
= v9fs_request(s
->private, T_UTIME
, NULL
, fs_path
,
913 buf
[0].tv_sec
, buf
[0].tv_nsec
,
914 buf
[1].tv_sec
, buf
[1].tv_nsec
);
921 static int proxy_remove(FsContext
*ctx
, const char *path
)
925 v9fs_string_init(&name
);
926 v9fs_string_sprintf(&name
, "%s", path
);
927 retval
= v9fs_request(ctx
->private, T_REMOVE
, NULL
, &name
);
928 v9fs_string_free(&name
);
935 static int proxy_fsync(FsContext
*ctx
, int fid_type
,
936 V9fsFidOpenState
*fs
, int datasync
)
940 if (fid_type
== P9_FID_DIR
) {
941 fd
= dirfd(fs
->dir
.stream
);
947 return qemu_fdatasync(fd
);
953 static int proxy_statfs(FsContext
*s
, V9fsPath
*fs_path
, struct statfs
*stbuf
)
956 retval
= v9fs_request(s
->private, T_STATFS
, stbuf
, fs_path
);
964 static ssize_t
proxy_lgetxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
965 const char *name
, void *value
, size_t size
)
970 v9fs_string_init(&xname
);
971 v9fs_string_sprintf(&xname
, "%s", name
);
972 retval
= v9fs_request(ctx
->private, T_LGETXATTR
, value
, size
, fs_path
,
974 v9fs_string_free(&xname
);
981 static ssize_t
proxy_llistxattr(FsContext
*ctx
, V9fsPath
*fs_path
,
982 void *value
, size_t size
)
985 retval
= v9fs_request(ctx
->private, T_LLISTXATTR
, value
, size
, fs_path
);
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
, fs_path
, &xname
,
1007 &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
, fs_path
, &xname
);
1025 v9fs_string_free(&xname
);
1032 static int proxy_name_to_path(FsContext
*ctx
, V9fsPath
*dir_path
,
1033 const char *name
, V9fsPath
*target
)
1036 v9fs_path_sprintf(target
, "%s/%s", dir_path
->data
, name
);
1038 v9fs_path_sprintf(target
, "%s", name
);
1043 static int proxy_renameat(FsContext
*ctx
, V9fsPath
*olddir
,
1044 const char *old_name
, V9fsPath
*newdir
,
1045 const char *new_name
)
1048 V9fsString old_full_name
, new_full_name
;
1050 v9fs_string_init(&old_full_name
);
1051 v9fs_string_init(&new_full_name
);
1053 v9fs_string_sprintf(&old_full_name
, "%s/%s", olddir
->data
, old_name
);
1054 v9fs_string_sprintf(&new_full_name
, "%s/%s", newdir
->data
, new_name
);
1056 ret
= proxy_rename(ctx
, old_full_name
.data
, new_full_name
.data
);
1057 v9fs_string_free(&old_full_name
);
1058 v9fs_string_free(&new_full_name
);
1062 static int proxy_unlinkat(FsContext
*ctx
, V9fsPath
*dir
,
1063 const char *name
, int flags
)
1066 V9fsString fullname
;
1067 v9fs_string_init(&fullname
);
1069 v9fs_string_sprintf(&fullname
, "%s/%s", dir
->data
, name
);
1070 ret
= proxy_remove(ctx
, fullname
.data
);
1071 v9fs_string_free(&fullname
);
1076 static int proxy_ioc_getversion(FsContext
*fs_ctx
, V9fsPath
*path
,
1077 mode_t st_mode
, uint64_t *st_gen
)
1081 /* Do not try to open special files like device nodes, fifos etc
1082 * we can get fd for regular files and directories only
1084 if (!S_ISREG(st_mode
) && !S_ISDIR(st_mode
)) {
1088 err
= v9fs_request(fs_ctx
->private, T_GETVERSION
, st_gen
, path
);
1096 static int connect_namedsocket(const char *path
, Error
**errp
)
1099 struct sockaddr_un helper
;
1101 if (strlen(path
) >= sizeof(helper
.sun_path
)) {
1102 error_setg(errp
, "socket name too long");
1105 sockfd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
1107 error_setg_errno(errp
, errno
, "failed to create client socket");
1110 strcpy(helper
.sun_path
, path
);
1111 helper
.sun_family
= AF_UNIX
;
1112 if (connect(sockfd
, (struct sockaddr
*)&helper
, sizeof(helper
)) < 0) {
1113 error_setg_errno(errp
, errno
, "failed to connect to '%s'", path
);
1118 /* remove the socket for security reasons */
1123 static void error_append_socket_sockfd_hint(Error
*const *errp
)
1125 error_append_hint(errp
, "Either specify socket=/some/path where /some/path"
1126 " points to a listening AF_UNIX socket or sock_fd=fd"
1127 " where fd is a file descriptor to a connected AF_UNIX"
1131 static int proxy_parse_opts(QemuOpts
*opts
, FsDriverEntry
*fs
, Error
**errp
)
1133 const char *socket
= qemu_opt_get(opts
, "socket");
1134 const char *sock_fd
= qemu_opt_get(opts
, "sock_fd");
1136 if (!socket
&& !sock_fd
) {
1137 error_setg(errp
, "both socket and sock_fd properties are missing");
1138 error_append_socket_sockfd_hint(errp
);
1141 if (socket
&& sock_fd
) {
1142 error_setg(errp
, "both socket and sock_fd properties are set");
1143 error_append_socket_sockfd_hint(errp
);
1147 fs
->path
= g_strdup(socket
);
1148 fs
->export_flags
|= V9FS_PROXY_SOCK_NAME
;
1150 fs
->path
= g_strdup(sock_fd
);
1151 fs
->export_flags
|= V9FS_PROXY_SOCK_FD
;
1156 static int proxy_init(FsContext
*ctx
, Error
**errp
)
1158 V9fsProxy
*proxy
= g_malloc(sizeof(V9fsProxy
));
1161 if (ctx
->export_flags
& V9FS_PROXY_SOCK_NAME
) {
1162 sock_id
= connect_namedsocket(ctx
->fs_root
, errp
);
1164 sock_id
= atoi(ctx
->fs_root
);
1166 error_setg(errp
, "socket descriptor not initialized");
1173 g_free(ctx
->fs_root
);
1174 ctx
->fs_root
= NULL
;
1176 proxy
->in_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1177 proxy
->in_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1178 proxy
->out_iovec
.iov_base
= g_malloc(PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
);
1179 proxy
->out_iovec
.iov_len
= PROXY_MAX_IO_SZ
+ PROXY_HDR_SZ
;
1181 ctx
->private = proxy
;
1182 proxy
->sockfd
= sock_id
;
1183 qemu_mutex_init(&proxy
->mutex
);
1185 ctx
->export_flags
|= V9FS_PATHNAME_FSCONTEXT
;
1186 ctx
->exops
.get_st_gen
= proxy_ioc_getversion
;
1190 static void proxy_cleanup(FsContext
*ctx
)
1192 V9fsProxy
*proxy
= ctx
->private;
1198 g_free(proxy
->out_iovec
.iov_base
);
1199 g_free(proxy
->in_iovec
.iov_base
);
1200 if (ctx
->export_flags
& V9FS_PROXY_SOCK_NAME
) {
1201 close(proxy
->sockfd
);
1206 FileOperations proxy_ops
= {
1207 .parse_opts
= proxy_parse_opts
,
1209 .cleanup
= proxy_cleanup
,
1210 .lstat
= proxy_lstat
,
1211 .readlink
= proxy_readlink
,
1212 .close
= proxy_close
,
1213 .closedir
= proxy_closedir
,
1215 .opendir
= proxy_opendir
,
1216 .rewinddir
= proxy_rewinddir
,
1217 .telldir
= proxy_telldir
,
1218 .readdir
= proxy_readdir
,
1219 .seekdir
= proxy_seekdir
,
1220 .preadv
= proxy_preadv
,
1221 .pwritev
= proxy_pwritev
,
1222 .chmod
= proxy_chmod
,
1223 .mknod
= proxy_mknod
,
1224 .mkdir
= proxy_mkdir
,
1225 .fstat
= proxy_fstat
,
1226 .open2
= proxy_open2
,
1227 .symlink
= proxy_symlink
,
1229 .truncate
= proxy_truncate
,
1230 .rename
= proxy_rename
,
1231 .chown
= proxy_chown
,
1232 .utimensat
= proxy_utimensat
,
1233 .remove
= proxy_remove
,
1234 .fsync
= proxy_fsync
,
1235 .statfs
= proxy_statfs
,
1236 .lgetxattr
= proxy_lgetxattr
,
1237 .llistxattr
= proxy_llistxattr
,
1238 .lsetxattr
= proxy_lsetxattr
,
1239 .lremovexattr
= proxy_lremovexattr
,
1240 .name_to_path
= proxy_name_to_path
,
1241 .renameat
= proxy_renameat
,
1242 .unlinkat
= proxy_unlinkat
,