xref: /openbmc/qemu/hw/9pfs/9p-proxy.c (revision e9e51b71)
1 /*
2  * 9p Proxy callback
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  * M. Mohan Kumar <mohan@in.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include <sys/socket.h>
15 #include <sys/un.h>
16 #include "qemu-common.h"
17 #include "9p.h"
18 #include "qapi/error.h"
19 #include "qemu/cutils.h"
20 #include "qemu/error-report.h"
21 #include "qemu/option.h"
22 #include "fsdev/qemu-fsdev.h"
23 #include "9p-proxy.h"
24 
25 typedef struct V9fsProxy {
26     int sockfd;
27     QemuMutex mutex;
28     struct iovec in_iovec;
29     struct iovec out_iovec;
30 } V9fsProxy;
31 
32 /*
33  * Return received file descriptor on success in *status.
34  * errno is also returned on *status (which will be < 0)
35  * return < 0 on transport error.
36  */
37 static int v9fs_receivefd(int sockfd, int *status)
38 {
39     struct iovec iov;
40     struct msghdr msg;
41     struct cmsghdr *cmsg;
42     int retval, data, fd;
43     union MsgControl msg_control;
44 
45     iov.iov_base = &data;
46     iov.iov_len = sizeof(data);
47 
48     memset(&msg, 0, sizeof(msg));
49     msg.msg_iov = &iov;
50     msg.msg_iovlen = 1;
51     msg.msg_control = &msg_control;
52     msg.msg_controllen = sizeof(msg_control);
53 
54     do {
55         retval = recvmsg(sockfd, &msg, 0);
56     } while (retval < 0 && errno == EINTR);
57     if (retval <= 0) {
58         return retval;
59     }
60     /*
61      * data is set to V9FS_FD_VALID, if ancillary data is sent.  If this
62      * request doesn't need ancillary data (fd) or an error occurred,
63      * data is set to negative errno value.
64      */
65     if (data != V9FS_FD_VALID) {
66         *status = data;
67         return 0;
68     }
69     /*
70      * File descriptor (fd) is sent in the ancillary data. Check if we
71      * indeed received it. One of the reasons to fail to receive it is if
72      * we exceeded the maximum number of file descriptors!
73      */
74     for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
75         if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)) ||
76             cmsg->cmsg_level != SOL_SOCKET ||
77             cmsg->cmsg_type != SCM_RIGHTS) {
78             continue;
79         }
80         fd = *((int *)CMSG_DATA(cmsg));
81         *status = fd;
82         return 0;
83     }
84     *status = -ENFILE;  /* Ancillary data sent but not received */
85     return 0;
86 }
87 
88 static ssize_t socket_read(int sockfd, void *buff, size_t size)
89 {
90     ssize_t retval, total = 0;
91 
92     while (size) {
93         retval = read(sockfd, buff, size);
94         if (retval == 0) {
95             return -EIO;
96         }
97         if (retval < 0) {
98             if (errno == EINTR) {
99                 continue;
100             }
101             return -errno;
102         }
103         size -= retval;
104         buff += retval;
105         total += retval;
106     }
107     return total;
108 }
109 
110 /* Converts proxy_statfs to VFS statfs structure */
111 static void prstatfs_to_statfs(struct statfs *stfs, ProxyStatFS *prstfs)
112 {
113     memset(stfs, 0, sizeof(*stfs));
114     stfs->f_type = prstfs->f_type;
115     stfs->f_bsize = prstfs->f_bsize;
116     stfs->f_blocks = prstfs->f_blocks;
117     stfs->f_bfree = prstfs->f_bfree;
118     stfs->f_bavail = prstfs->f_bavail;
119     stfs->f_files = prstfs->f_files;
120     stfs->f_ffree = prstfs->f_ffree;
121     stfs->f_fsid.__val[0] = prstfs->f_fsid[0] & 0xFFFFFFFFU;
122     stfs->f_fsid.__val[1] = prstfs->f_fsid[1] >> 32 & 0xFFFFFFFFU;
123     stfs->f_namelen = prstfs->f_namelen;
124     stfs->f_frsize = prstfs->f_frsize;
125 }
126 
127 /* Converts proxy_stat structure to VFS stat structure */
128 static void prstat_to_stat(struct stat *stbuf, ProxyStat *prstat)
129 {
130    memset(stbuf, 0, sizeof(*stbuf));
131    stbuf->st_dev = prstat->st_dev;
132    stbuf->st_ino = prstat->st_ino;
133    stbuf->st_nlink = prstat->st_nlink;
134    stbuf->st_mode = prstat->st_mode;
135    stbuf->st_uid = prstat->st_uid;
136    stbuf->st_gid = prstat->st_gid;
137    stbuf->st_rdev = prstat->st_rdev;
138    stbuf->st_size = prstat->st_size;
139    stbuf->st_blksize = prstat->st_blksize;
140    stbuf->st_blocks = prstat->st_blocks;
141    stbuf->st_atim.tv_sec = prstat->st_atim_sec;
142    stbuf->st_atim.tv_nsec = prstat->st_atim_nsec;
143    stbuf->st_mtime = prstat->st_mtim_sec;
144    stbuf->st_mtim.tv_nsec = prstat->st_mtim_nsec;
145    stbuf->st_ctime = prstat->st_ctim_sec;
146    stbuf->st_ctim.tv_nsec = prstat->st_ctim_nsec;
147 }
148 
149 /*
150  * Response contains two parts
151  * {header, data}
152  * header.type == T_ERROR, data -> -errno
153  * header.type == T_SUCCESS, data -> response
154  * size of errno/response is given by header.size
155  * returns < 0, on transport error. response is
156  * valid only if status >= 0.
157  */
158 static int v9fs_receive_response(V9fsProxy *proxy, int type,
159                                  int *status, void *response)
160 {
161     int retval;
162     ProxyHeader header;
163     struct iovec *reply = &proxy->in_iovec;
164 
165     *status = 0;
166     reply->iov_len = 0;
167     retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
168     if (retval < 0) {
169         return retval;
170     }
171     reply->iov_len = PROXY_HDR_SZ;
172     retval = proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
173     assert(retval == 4 * 2);
174     /*
175      * if response size > PROXY_MAX_IO_SZ, read the response but ignore it and
176      * return -ENOBUFS
177      */
178     if (header.size > PROXY_MAX_IO_SZ) {
179         int count;
180         while (header.size > 0) {
181             count = MIN(PROXY_MAX_IO_SZ, header.size);
182             count = socket_read(proxy->sockfd, reply->iov_base, count);
183             if (count < 0) {
184                 return count;
185             }
186             header.size -= count;
187         }
188         *status = -ENOBUFS;
189         return 0;
190     }
191 
192     retval = socket_read(proxy->sockfd,
193                          reply->iov_base + PROXY_HDR_SZ, header.size);
194     if (retval < 0) {
195         return retval;
196     }
197     reply->iov_len += header.size;
198     /* there was an error during processing request */
199     if (header.type == T_ERROR) {
200         int ret;
201         ret = proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
202         assert(ret == 4);
203         return 0;
204     }
205 
206     switch (type) {
207     case T_LSTAT: {
208         ProxyStat prstat;
209         retval = proxy_unmarshal(reply, PROXY_HDR_SZ,
210                                  "qqqdddqqqqqqqqqq", &prstat.st_dev,
211                                  &prstat.st_ino, &prstat.st_nlink,
212                                  &prstat.st_mode, &prstat.st_uid,
213                                  &prstat.st_gid, &prstat.st_rdev,
214                                  &prstat.st_size, &prstat.st_blksize,
215                                  &prstat.st_blocks,
216                                  &prstat.st_atim_sec, &prstat.st_atim_nsec,
217                                  &prstat.st_mtim_sec, &prstat.st_mtim_nsec,
218                                  &prstat.st_ctim_sec, &prstat.st_ctim_nsec);
219         assert(retval == 8 * 3 + 4 * 3 + 8 * 10);
220         prstat_to_stat(response, &prstat);
221         break;
222     }
223     case T_STATFS: {
224         ProxyStatFS prstfs;
225         retval = proxy_unmarshal(reply, PROXY_HDR_SZ,
226                                  "qqqqqqqqqqq", &prstfs.f_type,
227                                  &prstfs.f_bsize, &prstfs.f_blocks,
228                                  &prstfs.f_bfree, &prstfs.f_bavail,
229                                  &prstfs.f_files, &prstfs.f_ffree,
230                                  &prstfs.f_fsid[0], &prstfs.f_fsid[1],
231                                  &prstfs.f_namelen, &prstfs.f_frsize);
232         assert(retval == 8 * 11);
233         prstatfs_to_statfs(response, &prstfs);
234         break;
235     }
236     case T_READLINK: {
237         V9fsString target;
238         v9fs_string_init(&target);
239         retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "s", &target);
240         strcpy(response, target.data);
241         v9fs_string_free(&target);
242         break;
243     }
244     case T_LGETXATTR:
245     case T_LLISTXATTR: {
246         V9fsString xattr;
247         v9fs_string_init(&xattr);
248         retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "s", &xattr);
249         memcpy(response, xattr.data, xattr.size);
250         v9fs_string_free(&xattr);
251         break;
252     }
253     case T_GETVERSION:
254         retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "q", response);
255         assert(retval == 8);
256         break;
257     default:
258         return -1;
259     }
260     if (retval < 0) {
261         *status  = retval;
262     }
263     return 0;
264 }
265 
266 /*
267  * return < 0 on transport error.
268  * *status is valid only if return >= 0
269  */
270 static int v9fs_receive_status(V9fsProxy *proxy,
271                                struct iovec *reply, int *status)
272 {
273     int retval;
274     ProxyHeader header;
275 
276     *status = 0;
277     reply->iov_len = 0;
278     retval = socket_read(proxy->sockfd, reply->iov_base, PROXY_HDR_SZ);
279     if (retval < 0) {
280         return retval;
281     }
282     reply->iov_len = PROXY_HDR_SZ;
283     retval = proxy_unmarshal(reply, 0, "dd", &header.type, &header.size);
284     assert(retval == 4 * 2);
285     retval = socket_read(proxy->sockfd,
286                          reply->iov_base + PROXY_HDR_SZ, header.size);
287     if (retval < 0) {
288         return retval;
289     }
290     reply->iov_len += header.size;
291     retval = proxy_unmarshal(reply, PROXY_HDR_SZ, "d", status);
292     assert(retval == 4);
293     return 0;
294 }
295 
296 /*
297  * Proxy->header and proxy->request written to socket by QEMU process.
298  * This request read by proxy helper process
299  * returns 0 on success and -errno on error
300  */
301 static int v9fs_request(V9fsProxy *proxy, int type, void *response, ...)
302 {
303     dev_t rdev;
304     va_list ap;
305     int size = 0;
306     int retval = 0;
307     uint64_t offset;
308     ProxyHeader header = { 0, 0};
309     struct timespec spec[2];
310     int flags, mode, uid, gid;
311     V9fsString *name, *value;
312     V9fsString *path, *oldpath;
313     struct iovec *iovec = NULL, *reply = NULL;
314 
315     qemu_mutex_lock(&proxy->mutex);
316 
317     if (proxy->sockfd == -1) {
318         retval = -EIO;
319         goto err_out;
320     }
321     iovec = &proxy->out_iovec;
322     reply = &proxy->in_iovec;
323     va_start(ap, response);
324     switch (type) {
325     case T_OPEN:
326         path = va_arg(ap, V9fsString *);
327         flags = va_arg(ap, int);
328         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, flags);
329         if (retval > 0) {
330             header.size = retval;
331             header.type = T_OPEN;
332         }
333         break;
334     case T_CREATE:
335         path = va_arg(ap, V9fsString *);
336         flags = va_arg(ap, int);
337         mode = va_arg(ap, int);
338         uid = va_arg(ap, int);
339         gid = va_arg(ap, int);
340         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdddd", path,
341                                     flags, mode, uid, gid);
342         if (retval > 0) {
343             header.size = retval;
344             header.type = T_CREATE;
345         }
346         break;
347     case T_MKNOD:
348         path = va_arg(ap, V9fsString *);
349         mode = va_arg(ap, int);
350         rdev = va_arg(ap, long int);
351         uid = va_arg(ap, int);
352         gid = va_arg(ap, int);
353         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsdq",
354                                     uid, gid, path, mode, rdev);
355         if (retval > 0) {
356             header.size = retval;
357             header.type = T_MKNOD;
358         }
359         break;
360     case T_MKDIR:
361         path = va_arg(ap, V9fsString *);
362         mode = va_arg(ap, int);
363         uid = va_arg(ap, int);
364         gid = va_arg(ap, int);
365         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddsd",
366                                     uid, gid, path, mode);
367         if (retval > 0) {
368             header.size = retval;
369             header.type = T_MKDIR;
370         }
371         break;
372     case T_SYMLINK:
373         oldpath = va_arg(ap, V9fsString *);
374         path = va_arg(ap, V9fsString *);
375         uid = va_arg(ap, int);
376         gid = va_arg(ap, int);
377         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ddss",
378                                     uid, gid, oldpath, path);
379         if (retval > 0) {
380             header.size = retval;
381             header.type = T_SYMLINK;
382         }
383         break;
384     case T_LINK:
385         oldpath = va_arg(ap, V9fsString *);
386         path = va_arg(ap, V9fsString *);
387         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss",
388                                     oldpath, path);
389         if (retval > 0) {
390             header.size = retval;
391             header.type = T_LINK;
392         }
393         break;
394     case T_LSTAT:
395         path = va_arg(ap, V9fsString *);
396         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
397         if (retval > 0) {
398             header.size = retval;
399             header.type = T_LSTAT;
400         }
401         break;
402     case T_READLINK:
403         path = va_arg(ap, V9fsString *);
404         size = va_arg(ap, int);
405         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, size);
406         if (retval > 0) {
407             header.size = retval;
408             header.type = T_READLINK;
409         }
410         break;
411     case T_STATFS:
412         path = va_arg(ap, V9fsString *);
413         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
414         if (retval > 0) {
415             header.size = retval;
416             header.type = T_STATFS;
417         }
418         break;
419     case T_CHMOD:
420         path = va_arg(ap, V9fsString *);
421         mode = va_arg(ap, int);
422         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sd", path, mode);
423         if (retval > 0) {
424             header.size = retval;
425             header.type = T_CHMOD;
426         }
427         break;
428     case T_CHOWN:
429         path = va_arg(ap, V9fsString *);
430         uid = va_arg(ap, int);
431         gid = va_arg(ap, int);
432         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sdd", path, uid, gid);
433         if (retval > 0) {
434             header.size = retval;
435             header.type = T_CHOWN;
436         }
437         break;
438     case T_TRUNCATE:
439         path = va_arg(ap, V9fsString *);
440         offset = va_arg(ap, uint64_t);
441         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sq", path, offset);
442         if (retval > 0) {
443             header.size = retval;
444             header.type = T_TRUNCATE;
445         }
446         break;
447     case T_UTIME:
448         path = va_arg(ap, V9fsString *);
449         spec[0].tv_sec = va_arg(ap, long);
450         spec[0].tv_nsec = va_arg(ap, long);
451         spec[1].tv_sec = va_arg(ap, long);
452         spec[1].tv_nsec = va_arg(ap, long);
453         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sqqqq", path,
454                                     spec[0].tv_sec, spec[1].tv_nsec,
455                                     spec[1].tv_sec, spec[1].tv_nsec);
456         if (retval > 0) {
457             header.size = retval;
458             header.type = T_UTIME;
459         }
460         break;
461     case T_RENAME:
462         oldpath = va_arg(ap, V9fsString *);
463         path = va_arg(ap, V9fsString *);
464         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", oldpath, path);
465         if (retval > 0) {
466             header.size = retval;
467             header.type = T_RENAME;
468         }
469         break;
470     case T_REMOVE:
471         path = va_arg(ap, V9fsString *);
472         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
473         if (retval > 0) {
474             header.size = retval;
475             header.type = T_REMOVE;
476         }
477         break;
478     case T_LGETXATTR:
479         size = va_arg(ap, int);
480         path = va_arg(ap, V9fsString *);
481         name = va_arg(ap, V9fsString *);
482         retval = proxy_marshal(iovec, PROXY_HDR_SZ,
483                                     "dss", size, path, name);
484         if (retval > 0) {
485             header.size = retval;
486             header.type = T_LGETXATTR;
487         }
488         break;
489     case T_LLISTXATTR:
490         size = va_arg(ap, int);
491         path = va_arg(ap, V9fsString *);
492         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ds", size, path);
493         if (retval > 0) {
494             header.size = retval;
495             header.type = T_LLISTXATTR;
496         }
497         break;
498     case T_LSETXATTR:
499         path = va_arg(ap, V9fsString *);
500         name = va_arg(ap, V9fsString *);
501         value = va_arg(ap, V9fsString *);
502         size = va_arg(ap, int);
503         flags = va_arg(ap, int);
504         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "sssdd",
505                                     path, name, value, size, flags);
506         if (retval > 0) {
507             header.size = retval;
508             header.type = T_LSETXATTR;
509         }
510         break;
511     case T_LREMOVEXATTR:
512         path = va_arg(ap, V9fsString *);
513         name = va_arg(ap, V9fsString *);
514         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "ss", path, name);
515         if (retval > 0) {
516             header.size = retval;
517             header.type = T_LREMOVEXATTR;
518         }
519         break;
520     case T_GETVERSION:
521         path = va_arg(ap, V9fsString *);
522         retval = proxy_marshal(iovec, PROXY_HDR_SZ, "s", path);
523         if (retval > 0) {
524             header.size = retval;
525             header.type = T_GETVERSION;
526         }
527         break;
528     default:
529         error_report("Invalid type %d", type);
530         retval = -EINVAL;
531         break;
532     }
533     va_end(ap);
534 
535     if (retval < 0) {
536         goto err_out;
537     }
538 
539     /* marshal the header details */
540     retval = proxy_marshal(iovec, 0, "dd", header.type, header.size);
541     assert(retval == 4 * 2);
542     header.size += PROXY_HDR_SZ;
543 
544     retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
545     if (retval != header.size) {
546         goto close_error;
547     }
548 
549     switch (type) {
550     case T_OPEN:
551     case T_CREATE:
552         /*
553          * A file descriptor is returned as response for
554          * T_OPEN,T_CREATE on success
555          */
556         if (v9fs_receivefd(proxy->sockfd, &retval) < 0) {
557             goto close_error;
558         }
559         break;
560     case T_MKNOD:
561     case T_MKDIR:
562     case T_SYMLINK:
563     case T_LINK:
564     case T_CHMOD:
565     case T_CHOWN:
566     case T_RENAME:
567     case T_TRUNCATE:
568     case T_UTIME:
569     case T_REMOVE:
570     case T_LSETXATTR:
571     case T_LREMOVEXATTR:
572         if (v9fs_receive_status(proxy, reply, &retval) < 0) {
573             goto close_error;
574         }
575         break;
576     case T_LSTAT:
577     case T_READLINK:
578     case T_STATFS:
579     case T_GETVERSION:
580         if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
581             goto close_error;
582         }
583         break;
584     case T_LGETXATTR:
585     case T_LLISTXATTR:
586         if (!size) {
587             if (v9fs_receive_status(proxy, reply, &retval) < 0) {
588                 goto close_error;
589             }
590         } else {
591             if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
592                 goto close_error;
593             }
594         }
595         break;
596     }
597 
598 err_out:
599     qemu_mutex_unlock(&proxy->mutex);
600     return retval;
601 
602 close_error:
603     close(proxy->sockfd);
604     proxy->sockfd = -1;
605     qemu_mutex_unlock(&proxy->mutex);
606     return -EIO;
607 }
608 
609 static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
610 {
611     int retval;
612     retval = v9fs_request(fs_ctx->private, T_LSTAT, stbuf, fs_path);
613     if (retval < 0) {
614         errno = -retval;
615         return -1;
616     }
617     return retval;
618 }
619 
620 static ssize_t proxy_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
621                               char *buf, size_t bufsz)
622 {
623     int retval;
624     retval = v9fs_request(fs_ctx->private, T_READLINK, buf, fs_path, bufsz);
625     if (retval < 0) {
626         errno = -retval;
627         return -1;
628     }
629     return strlen(buf);
630 }
631 
632 static int proxy_close(FsContext *ctx, V9fsFidOpenState *fs)
633 {
634     return close(fs->fd);
635 }
636 
637 static int proxy_closedir(FsContext *ctx, V9fsFidOpenState *fs)
638 {
639     return closedir(fs->dir.stream);
640 }
641 
642 static int proxy_open(FsContext *ctx, V9fsPath *fs_path,
643                       int flags, V9fsFidOpenState *fs)
644 {
645     fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, fs_path, flags);
646     if (fs->fd < 0) {
647         errno = -fs->fd;
648         fs->fd = -1;
649     }
650     return fs->fd;
651 }
652 
653 static int proxy_opendir(FsContext *ctx,
654                          V9fsPath *fs_path, V9fsFidOpenState *fs)
655 {
656     int serrno, fd;
657 
658     fs->dir.stream = NULL;
659     fd = v9fs_request(ctx->private, T_OPEN, NULL, fs_path, O_DIRECTORY);
660     if (fd < 0) {
661         errno = -fd;
662         return -1;
663     }
664     fs->dir.stream = fdopendir(fd);
665     if (!fs->dir.stream) {
666         serrno = errno;
667         close(fd);
668         errno = serrno;
669         return -1;
670     }
671     return 0;
672 }
673 
674 static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
675 {
676     rewinddir(fs->dir.stream);
677 }
678 
679 static off_t proxy_telldir(FsContext *ctx, V9fsFidOpenState *fs)
680 {
681     return telldir(fs->dir.stream);
682 }
683 
684 static struct dirent *proxy_readdir(FsContext *ctx, V9fsFidOpenState *fs)
685 {
686     return readdir(fs->dir.stream);
687 }
688 
689 static void proxy_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
690 {
691     seekdir(fs->dir.stream, off);
692 }
693 
694 static ssize_t proxy_preadv(FsContext *ctx, V9fsFidOpenState *fs,
695                             const struct iovec *iov,
696                             int iovcnt, off_t offset)
697 {
698     ssize_t ret;
699 #ifdef CONFIG_PREADV
700     ret = preadv(fs->fd, iov, iovcnt, offset);
701 #else
702     ret = lseek(fs->fd, offset, SEEK_SET);
703     if (ret >= 0) {
704         ret = readv(fs->fd, iov, iovcnt);
705     }
706 #endif
707     return ret;
708 }
709 
710 static ssize_t proxy_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
711                              const struct iovec *iov,
712                              int iovcnt, off_t offset)
713 {
714     ssize_t ret;
715 
716 #ifdef CONFIG_PREADV
717     ret = pwritev(fs->fd, iov, iovcnt, offset);
718 #else
719     ret = lseek(fs->fd, offset, SEEK_SET);
720     if (ret >= 0) {
721         ret = writev(fs->fd, iov, iovcnt);
722     }
723 #endif
724 #ifdef CONFIG_SYNC_FILE_RANGE
725     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
726         /*
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.
730          */
731         sync_file_range(fs->fd, offset, ret,
732                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
733     }
734 #endif
735     return ret;
736 }
737 
738 static int proxy_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
739 {
740     int retval;
741     retval = v9fs_request(fs_ctx->private, T_CHMOD, NULL, fs_path,
742                           credp->fc_mode);
743     if (retval < 0) {
744         errno = -retval;
745     }
746     return retval;
747 }
748 
749 static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
750                        const char *name, FsCred *credp)
751 {
752     int retval;
753     V9fsString fullname;
754 
755     v9fs_string_init(&fullname);
756     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
757 
758     retval = v9fs_request(fs_ctx->private, T_MKNOD, NULL, &fullname,
759                           credp->fc_mode, credp->fc_rdev,
760                           credp->fc_uid, credp->fc_gid);
761     v9fs_string_free(&fullname);
762     if (retval < 0) {
763         errno = -retval;
764         retval = -1;
765     }
766     return retval;
767 }
768 
769 static int proxy_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
770                        const char *name, FsCred *credp)
771 {
772     int retval;
773     V9fsString fullname;
774 
775     v9fs_string_init(&fullname);
776     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
777 
778     retval = v9fs_request(fs_ctx->private, T_MKDIR, NULL, &fullname,
779                           credp->fc_mode, credp->fc_uid, credp->fc_gid);
780     v9fs_string_free(&fullname);
781     if (retval < 0) {
782         errno = -retval;
783         retval = -1;
784     }
785     return retval;
786 }
787 
788 static int proxy_fstat(FsContext *fs_ctx, int fid_type,
789                        V9fsFidOpenState *fs, struct stat *stbuf)
790 {
791     int fd;
792 
793     if (fid_type == P9_FID_DIR) {
794         fd = dirfd(fs->dir.stream);
795     } else {
796         fd = fs->fd;
797     }
798     return fstat(fd, stbuf);
799 }
800 
801 static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
802                        int flags, FsCred *credp, V9fsFidOpenState *fs)
803 {
804     V9fsString fullname;
805 
806     v9fs_string_init(&fullname);
807     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
808 
809     fs->fd = v9fs_request(fs_ctx->private, T_CREATE, NULL, &fullname, flags,
810                           credp->fc_mode, credp->fc_uid, credp->fc_gid);
811     v9fs_string_free(&fullname);
812     if (fs->fd < 0) {
813         errno = -fs->fd;
814         fs->fd = -1;
815     }
816     return fs->fd;
817 }
818 
819 static int proxy_symlink(FsContext *fs_ctx, const char *oldpath,
820                          V9fsPath *dir_path, const char *name, FsCred *credp)
821 {
822     int retval;
823     V9fsString fullname, target;
824 
825     v9fs_string_init(&fullname);
826     v9fs_string_init(&target);
827 
828     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
829     v9fs_string_sprintf(&target, "%s", oldpath);
830 
831     retval = v9fs_request(fs_ctx->private, T_SYMLINK, NULL, &target, &fullname,
832                           credp->fc_uid, credp->fc_gid);
833     v9fs_string_free(&fullname);
834     v9fs_string_free(&target);
835     if (retval < 0) {
836         errno = -retval;
837         retval = -1;
838     }
839     return retval;
840 }
841 
842 static int proxy_link(FsContext *ctx, V9fsPath *oldpath,
843                       V9fsPath *dirpath, const char *name)
844 {
845     int retval;
846     V9fsString newpath;
847 
848     v9fs_string_init(&newpath);
849     v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
850 
851     retval = v9fs_request(ctx->private, T_LINK, NULL, oldpath, &newpath);
852     v9fs_string_free(&newpath);
853     if (retval < 0) {
854         errno = -retval;
855         retval = -1;
856     }
857     return retval;
858 }
859 
860 static int proxy_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
861 {
862     int retval;
863 
864     retval = v9fs_request(ctx->private, T_TRUNCATE, NULL, fs_path, size);
865     if (retval < 0) {
866         errno = -retval;
867         return -1;
868     }
869     return 0;
870 }
871 
872 static int proxy_rename(FsContext *ctx, const char *oldpath,
873                         const char *newpath)
874 {
875     int retval;
876     V9fsString oldname, newname;
877 
878     v9fs_string_init(&oldname);
879     v9fs_string_init(&newname);
880 
881     v9fs_string_sprintf(&oldname, "%s", oldpath);
882     v9fs_string_sprintf(&newname, "%s", newpath);
883     retval = v9fs_request(ctx->private, T_RENAME, NULL, &oldname, &newname);
884     v9fs_string_free(&oldname);
885     v9fs_string_free(&newname);
886     if (retval < 0) {
887         errno = -retval;
888     }
889     return retval;
890 }
891 
892 static int proxy_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
893 {
894     int retval;
895     retval = v9fs_request(fs_ctx->private, T_CHOWN, NULL, fs_path,
896                           credp->fc_uid, credp->fc_gid);
897     if (retval < 0) {
898         errno = -retval;
899     }
900     return retval;
901 }
902 
903 static int proxy_utimensat(FsContext *s, V9fsPath *fs_path,
904                            const struct timespec *buf)
905 {
906     int retval;
907     retval = v9fs_request(s->private, T_UTIME, NULL, fs_path,
908                           buf[0].tv_sec, buf[0].tv_nsec,
909                           buf[1].tv_sec, buf[1].tv_nsec);
910     if (retval < 0) {
911         errno = -retval;
912     }
913     return retval;
914 }
915 
916 static int proxy_remove(FsContext *ctx, const char *path)
917 {
918     int retval;
919     V9fsString name;
920     v9fs_string_init(&name);
921     v9fs_string_sprintf(&name, "%s", path);
922     retval = v9fs_request(ctx->private, T_REMOVE, NULL, &name);
923     v9fs_string_free(&name);
924     if (retval < 0) {
925         errno = -retval;
926     }
927     return retval;
928 }
929 
930 static int proxy_fsync(FsContext *ctx, int fid_type,
931                        V9fsFidOpenState *fs, int datasync)
932 {
933     int fd;
934 
935     if (fid_type == P9_FID_DIR) {
936         fd = dirfd(fs->dir.stream);
937     } else {
938         fd = fs->fd;
939     }
940 
941     if (datasync) {
942         return qemu_fdatasync(fd);
943     } else {
944         return fsync(fd);
945     }
946 }
947 
948 static int proxy_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
949 {
950     int retval;
951     retval = v9fs_request(s->private, T_STATFS, stbuf, fs_path);
952     if (retval < 0) {
953         errno = -retval;
954         return -1;
955     }
956     return retval;
957 }
958 
959 static ssize_t proxy_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
960                                const char *name, void *value, size_t size)
961 {
962     int retval;
963     V9fsString xname;
964 
965     v9fs_string_init(&xname);
966     v9fs_string_sprintf(&xname, "%s", name);
967     retval = v9fs_request(ctx->private, T_LGETXATTR, value, size, fs_path,
968                           &xname);
969     v9fs_string_free(&xname);
970     if (retval < 0) {
971         errno = -retval;
972     }
973     return retval;
974 }
975 
976 static ssize_t proxy_llistxattr(FsContext *ctx, V9fsPath *fs_path,
977                                 void *value, size_t size)
978 {
979     int retval;
980     retval = v9fs_request(ctx->private, T_LLISTXATTR, value, size, fs_path);
981     if (retval < 0) {
982         errno = -retval;
983     }
984     return retval;
985 }
986 
987 static int proxy_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
988                            void *value, size_t size, int flags)
989 {
990     int retval;
991     V9fsString xname, xvalue;
992 
993     v9fs_string_init(&xname);
994     v9fs_string_sprintf(&xname, "%s", name);
995 
996     v9fs_string_init(&xvalue);
997     xvalue.size = size;
998     xvalue.data = g_malloc(size);
999     memcpy(xvalue.data, value, size);
1000 
1001     retval = v9fs_request(ctx->private, T_LSETXATTR, value, fs_path, &xname,
1002                           &xvalue, size, flags);
1003     v9fs_string_free(&xname);
1004     v9fs_string_free(&xvalue);
1005     if (retval < 0) {
1006         errno = -retval;
1007     }
1008     return retval;
1009 }
1010 
1011 static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1012                               const char *name)
1013 {
1014     int retval;
1015     V9fsString xname;
1016 
1017     v9fs_string_init(&xname);
1018     v9fs_string_sprintf(&xname, "%s", name);
1019     retval = v9fs_request(ctx->private, T_LREMOVEXATTR, NULL, fs_path, &xname);
1020     v9fs_string_free(&xname);
1021     if (retval < 0) {
1022         errno = -retval;
1023     }
1024     return retval;
1025 }
1026 
1027 static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1028                               const char *name, V9fsPath *target)
1029 {
1030     if (dir_path) {
1031         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1032     } else {
1033         v9fs_path_sprintf(target, "%s", name);
1034     }
1035     return 0;
1036 }
1037 
1038 static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
1039                           const char *old_name, V9fsPath *newdir,
1040                           const char *new_name)
1041 {
1042     int ret;
1043     V9fsString old_full_name, new_full_name;
1044 
1045     v9fs_string_init(&old_full_name);
1046     v9fs_string_init(&new_full_name);
1047 
1048     v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1049     v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1050 
1051     ret = proxy_rename(ctx, old_full_name.data, new_full_name.data);
1052     v9fs_string_free(&old_full_name);
1053     v9fs_string_free(&new_full_name);
1054     return ret;
1055 }
1056 
1057 static int proxy_unlinkat(FsContext *ctx, V9fsPath *dir,
1058                           const char *name, int flags)
1059 {
1060     int ret;
1061     V9fsString fullname;
1062     v9fs_string_init(&fullname);
1063 
1064     v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1065     ret = proxy_remove(ctx, fullname.data);
1066     v9fs_string_free(&fullname);
1067 
1068     return ret;
1069 }
1070 
1071 static int proxy_ioc_getversion(FsContext *fs_ctx, V9fsPath *path,
1072                                 mode_t st_mode, uint64_t *st_gen)
1073 {
1074     int err;
1075 
1076     /* Do not try to open special files like device nodes, fifos etc
1077      * we can get fd for regular files and directories only
1078      */
1079     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1080         errno = ENOTTY;
1081         return -1;
1082     }
1083     err = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, path);
1084     if (err < 0) {
1085         errno = -err;
1086         err = -1;
1087     }
1088     return err;
1089 }
1090 
1091 static int connect_namedsocket(const char *path, Error **errp)
1092 {
1093     int sockfd;
1094     struct sockaddr_un helper;
1095 
1096     if (strlen(path) >= sizeof(helper.sun_path)) {
1097         error_setg(errp, "socket name too long");
1098         return -1;
1099     }
1100     sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1101     if (sockfd < 0) {
1102         error_setg_errno(errp, errno, "failed to create client socket");
1103         return -1;
1104     }
1105     strcpy(helper.sun_path, path);
1106     helper.sun_family = AF_UNIX;
1107     if (connect(sockfd, (struct sockaddr *)&helper, sizeof(helper)) < 0) {
1108         error_setg_errno(errp, errno, "failed to connect to '%s'", path);
1109         close(sockfd);
1110         return -1;
1111     }
1112 
1113     /* remove the socket for security reasons */
1114     unlink(path);
1115     return sockfd;
1116 }
1117 
1118 static void error_append_socket_sockfd_hint(Error *const *errp)
1119 {
1120     error_append_hint(errp, "Either specify socket=/some/path where /some/path"
1121                       " points to a listening AF_UNIX socket or sock_fd=fd"
1122                       " where fd is a file descriptor to a connected AF_UNIX"
1123                       " socket\n");
1124 }
1125 
1126 static int proxy_parse_opts(QemuOpts *opts, FsDriverEntry *fs, Error **errp)
1127 {
1128     const char *socket = qemu_opt_get(opts, "socket");
1129     const char *sock_fd = qemu_opt_get(opts, "sock_fd");
1130 
1131     if (!socket && !sock_fd) {
1132         error_setg(errp, "both socket and sock_fd properties are missing");
1133         error_append_socket_sockfd_hint(errp);
1134         return -1;
1135     }
1136     if (socket && sock_fd) {
1137         error_setg(errp, "both socket and sock_fd properties are set");
1138         error_append_socket_sockfd_hint(errp);
1139         return -1;
1140     }
1141     if (socket) {
1142         fs->path = g_strdup(socket);
1143         fs->export_flags |= V9FS_PROXY_SOCK_NAME;
1144     } else {
1145         fs->path = g_strdup(sock_fd);
1146         fs->export_flags |= V9FS_PROXY_SOCK_FD;
1147     }
1148     return 0;
1149 }
1150 
1151 static int proxy_init(FsContext *ctx, Error **errp)
1152 {
1153     V9fsProxy *proxy = g_malloc(sizeof(V9fsProxy));
1154     int sock_id;
1155 
1156     if (ctx->export_flags & V9FS_PROXY_SOCK_NAME) {
1157         sock_id = connect_namedsocket(ctx->fs_root, errp);
1158     } else {
1159         sock_id = atoi(ctx->fs_root);
1160         if (sock_id < 0) {
1161             error_setg(errp, "socket descriptor not initialized");
1162         }
1163     }
1164     if (sock_id < 0) {
1165         g_free(proxy);
1166         return -1;
1167     }
1168     g_free(ctx->fs_root);
1169     ctx->fs_root = NULL;
1170 
1171     proxy->in_iovec.iov_base  = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
1172     proxy->in_iovec.iov_len   = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
1173     proxy->out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
1174     proxy->out_iovec.iov_len  = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
1175 
1176     ctx->private = proxy;
1177     proxy->sockfd = sock_id;
1178     qemu_mutex_init(&proxy->mutex);
1179 
1180     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1181     ctx->exops.get_st_gen = proxy_ioc_getversion;
1182     return 0;
1183 }
1184 
1185 static void proxy_cleanup(FsContext *ctx)
1186 {
1187     V9fsProxy *proxy = ctx->private;
1188 
1189     if (!proxy) {
1190         return;
1191     }
1192 
1193     g_free(proxy->out_iovec.iov_base);
1194     g_free(proxy->in_iovec.iov_base);
1195     if (ctx->export_flags & V9FS_PROXY_SOCK_NAME) {
1196         close(proxy->sockfd);
1197     }
1198     g_free(proxy);
1199 }
1200 
1201 FileOperations proxy_ops = {
1202     .parse_opts   = proxy_parse_opts,
1203     .init         = proxy_init,
1204     .cleanup      = proxy_cleanup,
1205     .lstat        = proxy_lstat,
1206     .readlink     = proxy_readlink,
1207     .close        = proxy_close,
1208     .closedir     = proxy_closedir,
1209     .open         = proxy_open,
1210     .opendir      = proxy_opendir,
1211     .rewinddir    = proxy_rewinddir,
1212     .telldir      = proxy_telldir,
1213     .readdir      = proxy_readdir,
1214     .seekdir      = proxy_seekdir,
1215     .preadv       = proxy_preadv,
1216     .pwritev      = proxy_pwritev,
1217     .chmod        = proxy_chmod,
1218     .mknod        = proxy_mknod,
1219     .mkdir        = proxy_mkdir,
1220     .fstat        = proxy_fstat,
1221     .open2        = proxy_open2,
1222     .symlink      = proxy_symlink,
1223     .link         = proxy_link,
1224     .truncate     = proxy_truncate,
1225     .rename       = proxy_rename,
1226     .chown        = proxy_chown,
1227     .utimensat    = proxy_utimensat,
1228     .remove       = proxy_remove,
1229     .fsync        = proxy_fsync,
1230     .statfs       = proxy_statfs,
1231     .lgetxattr    = proxy_lgetxattr,
1232     .llistxattr   = proxy_llistxattr,
1233     .lsetxattr    = proxy_lsetxattr,
1234     .lremovexattr = proxy_lremovexattr,
1235     .name_to_path = proxy_name_to_path,
1236     .renameat     = proxy_renameat,
1237     .unlinkat     = proxy_unlinkat,
1238 };
1239