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  */
v9fs_receivefd(int sockfd,int * status)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 
socket_read(int sockfd,void * buff,size_t size)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 */
prstatfs_to_statfs(struct statfs * stfs,ProxyStatFS * prstfs)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 */
prstat_to_stat(struct stat * stbuf,ProxyStat * prstat)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  */
v9fs_receive_response(V9fsProxy * proxy,int type,int * status,void * response)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  */
v9fs_receive_status(V9fsProxy * proxy,struct iovec * reply,int * status)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  */
v9fs_request(V9fsProxy * proxy,int type,void * response,...)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     proxy_marshal(iovec, 0, "dd", header.type, header.size);
541     header.size += PROXY_HDR_SZ;
542 
543     retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
544     if (retval != header.size) {
545         goto close_error;
546     }
547 
548     switch (type) {
549     case T_OPEN:
550     case T_CREATE:
551         /*
552          * A file descriptor is returned as response for
553          * T_OPEN,T_CREATE on success
554          */
555         if (v9fs_receivefd(proxy->sockfd, &retval) < 0) {
556             goto close_error;
557         }
558         break;
559     case T_MKNOD:
560     case T_MKDIR:
561     case T_SYMLINK:
562     case T_LINK:
563     case T_CHMOD:
564     case T_CHOWN:
565     case T_RENAME:
566     case T_TRUNCATE:
567     case T_UTIME:
568     case T_REMOVE:
569     case T_LSETXATTR:
570     case T_LREMOVEXATTR:
571         if (v9fs_receive_status(proxy, reply, &retval) < 0) {
572             goto close_error;
573         }
574         break;
575     case T_LSTAT:
576     case T_READLINK:
577     case T_STATFS:
578     case T_GETVERSION:
579         if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
580             goto close_error;
581         }
582         break;
583     case T_LGETXATTR:
584     case T_LLISTXATTR:
585         if (!size) {
586             if (v9fs_receive_status(proxy, reply, &retval) < 0) {
587                 goto close_error;
588             }
589         } else {
590             if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
591                 goto close_error;
592             }
593         }
594         break;
595     }
596 
597 err_out:
598     qemu_mutex_unlock(&proxy->mutex);
599     return retval;
600 
601 close_error:
602     close(proxy->sockfd);
603     proxy->sockfd = -1;
604     qemu_mutex_unlock(&proxy->mutex);
605     return -EIO;
606 }
607 
proxy_lstat(FsContext * fs_ctx,V9fsPath * fs_path,struct stat * stbuf)608 static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
609 {
610     int retval;
611     retval = v9fs_request(fs_ctx->private, T_LSTAT, stbuf, fs_path);
612     if (retval < 0) {
613         errno = -retval;
614         return -1;
615     }
616     return retval;
617 }
618 
proxy_readlink(FsContext * fs_ctx,V9fsPath * fs_path,char * buf,size_t bufsz)619 static ssize_t proxy_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
620                               char *buf, size_t bufsz)
621 {
622     int retval;
623     retval = v9fs_request(fs_ctx->private, T_READLINK, buf, fs_path, bufsz);
624     if (retval < 0) {
625         errno = -retval;
626         return -1;
627     }
628     return strlen(buf);
629 }
630 
proxy_close(FsContext * ctx,V9fsFidOpenState * fs)631 static int proxy_close(FsContext *ctx, V9fsFidOpenState *fs)
632 {
633     return close(fs->fd);
634 }
635 
proxy_closedir(FsContext * ctx,V9fsFidOpenState * fs)636 static int proxy_closedir(FsContext *ctx, V9fsFidOpenState *fs)
637 {
638     return closedir(fs->dir.stream);
639 }
640 
proxy_open(FsContext * ctx,V9fsPath * fs_path,int flags,V9fsFidOpenState * fs)641 static int proxy_open(FsContext *ctx, V9fsPath *fs_path,
642                       int flags, V9fsFidOpenState *fs)
643 {
644     fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, fs_path, flags);
645     if (fs->fd < 0) {
646         errno = -fs->fd;
647         fs->fd = -1;
648     }
649     return fs->fd;
650 }
651 
proxy_opendir(FsContext * ctx,V9fsPath * fs_path,V9fsFidOpenState * fs)652 static int proxy_opendir(FsContext *ctx,
653                          V9fsPath *fs_path, V9fsFidOpenState *fs)
654 {
655     int serrno, fd;
656 
657     fs->dir.stream = NULL;
658     fd = v9fs_request(ctx->private, T_OPEN, NULL, fs_path, O_DIRECTORY);
659     if (fd < 0) {
660         errno = -fd;
661         return -1;
662     }
663     fs->dir.stream = fdopendir(fd);
664     if (!fs->dir.stream) {
665         serrno = errno;
666         close(fd);
667         errno = serrno;
668         return -1;
669     }
670     return 0;
671 }
672 
proxy_rewinddir(FsContext * ctx,V9fsFidOpenState * fs)673 static void proxy_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
674 {
675     rewinddir(fs->dir.stream);
676 }
677 
proxy_telldir(FsContext * ctx,V9fsFidOpenState * fs)678 static off_t proxy_telldir(FsContext *ctx, V9fsFidOpenState *fs)
679 {
680     return telldir(fs->dir.stream);
681 }
682 
proxy_readdir(FsContext * ctx,V9fsFidOpenState * fs)683 static struct dirent *proxy_readdir(FsContext *ctx, V9fsFidOpenState *fs)
684 {
685     return readdir(fs->dir.stream);
686 }
687 
proxy_seekdir(FsContext * ctx,V9fsFidOpenState * fs,off_t off)688 static void proxy_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
689 {
690     seekdir(fs->dir.stream, off);
691 }
692 
proxy_preadv(FsContext * ctx,V9fsFidOpenState * fs,const struct iovec * iov,int iovcnt,off_t offset)693 static ssize_t proxy_preadv(FsContext *ctx, V9fsFidOpenState *fs,
694                             const struct iovec *iov,
695                             int iovcnt, off_t offset)
696 {
697     ssize_t ret;
698 #ifdef CONFIG_PREADV
699     ret = preadv(fs->fd, iov, iovcnt, offset);
700 #else
701     ret = lseek(fs->fd, offset, SEEK_SET);
702     if (ret >= 0) {
703         ret = readv(fs->fd, iov, iovcnt);
704     }
705 #endif
706     return ret;
707 }
708 
proxy_pwritev(FsContext * ctx,V9fsFidOpenState * fs,const struct iovec * iov,int iovcnt,off_t offset)709 static ssize_t proxy_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
710                              const struct iovec *iov,
711                              int iovcnt, off_t offset)
712 {
713     ssize_t ret;
714 
715 #ifdef CONFIG_PREADV
716     ret = pwritev(fs->fd, iov, iovcnt, offset);
717 #else
718     ret = lseek(fs->fd, offset, SEEK_SET);
719     if (ret >= 0) {
720         ret = writev(fs->fd, iov, iovcnt);
721     }
722 #endif
723 #ifdef CONFIG_SYNC_FILE_RANGE
724     if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
725         /*
726          * Initiate a writeback. This is not a data integrity sync.
727          * We want to ensure that we don't leave dirty pages in the cache
728          * after write when writeout=immediate is sepcified.
729          */
730         sync_file_range(fs->fd, offset, ret,
731                         SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
732     }
733 #endif
734     return ret;
735 }
736 
proxy_chmod(FsContext * fs_ctx,V9fsPath * fs_path,FsCred * credp)737 static int proxy_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
738 {
739     int retval;
740     retval = v9fs_request(fs_ctx->private, T_CHMOD, NULL, fs_path,
741                           credp->fc_mode);
742     if (retval < 0) {
743         errno = -retval;
744     }
745     return retval;
746 }
747 
proxy_mknod(FsContext * fs_ctx,V9fsPath * dir_path,const char * name,FsCred * credp)748 static int proxy_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
749                        const char *name, FsCred *credp)
750 {
751     int retval;
752     V9fsString fullname;
753 
754     v9fs_string_init(&fullname);
755     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
756 
757     retval = v9fs_request(fs_ctx->private, T_MKNOD, NULL, &fullname,
758                           credp->fc_mode, credp->fc_rdev,
759                           credp->fc_uid, credp->fc_gid);
760     v9fs_string_free(&fullname);
761     if (retval < 0) {
762         errno = -retval;
763         retval = -1;
764     }
765     return retval;
766 }
767 
proxy_mkdir(FsContext * fs_ctx,V9fsPath * dir_path,const char * name,FsCred * credp)768 static int proxy_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
769                        const char *name, FsCred *credp)
770 {
771     int retval;
772     V9fsString fullname;
773 
774     v9fs_string_init(&fullname);
775     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
776 
777     retval = v9fs_request(fs_ctx->private, T_MKDIR, NULL, &fullname,
778                           credp->fc_mode, credp->fc_uid, credp->fc_gid);
779     v9fs_string_free(&fullname);
780     if (retval < 0) {
781         errno = -retval;
782         retval = -1;
783     }
784     return retval;
785 }
786 
proxy_fstat(FsContext * fs_ctx,int fid_type,V9fsFidOpenState * fs,struct stat * stbuf)787 static int proxy_fstat(FsContext *fs_ctx, int fid_type,
788                        V9fsFidOpenState *fs, struct stat *stbuf)
789 {
790     int fd;
791 
792     if (fid_type == P9_FID_DIR) {
793         fd = dirfd(fs->dir.stream);
794     } else {
795         fd = fs->fd;
796     }
797     return fstat(fd, stbuf);
798 }
799 
proxy_open2(FsContext * fs_ctx,V9fsPath * dir_path,const char * name,int flags,FsCred * credp,V9fsFidOpenState * fs)800 static int proxy_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
801                        int flags, FsCred *credp, V9fsFidOpenState *fs)
802 {
803     V9fsString fullname;
804 
805     v9fs_string_init(&fullname);
806     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
807 
808     fs->fd = v9fs_request(fs_ctx->private, T_CREATE, NULL, &fullname, flags,
809                           credp->fc_mode, credp->fc_uid, credp->fc_gid);
810     v9fs_string_free(&fullname);
811     if (fs->fd < 0) {
812         errno = -fs->fd;
813         fs->fd = -1;
814     }
815     return fs->fd;
816 }
817 
proxy_symlink(FsContext * fs_ctx,const char * oldpath,V9fsPath * dir_path,const char * name,FsCred * credp)818 static int proxy_symlink(FsContext *fs_ctx, const char *oldpath,
819                          V9fsPath *dir_path, const char *name, FsCred *credp)
820 {
821     int retval;
822     V9fsString fullname, target;
823 
824     v9fs_string_init(&fullname);
825     v9fs_string_init(&target);
826 
827     v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name);
828     v9fs_string_sprintf(&target, "%s", oldpath);
829 
830     retval = v9fs_request(fs_ctx->private, T_SYMLINK, NULL, &target, &fullname,
831                           credp->fc_uid, credp->fc_gid);
832     v9fs_string_free(&fullname);
833     v9fs_string_free(&target);
834     if (retval < 0) {
835         errno = -retval;
836         retval = -1;
837     }
838     return retval;
839 }
840 
proxy_link(FsContext * ctx,V9fsPath * oldpath,V9fsPath * dirpath,const char * name)841 static int proxy_link(FsContext *ctx, V9fsPath *oldpath,
842                       V9fsPath *dirpath, const char *name)
843 {
844     int retval;
845     V9fsString newpath;
846 
847     v9fs_string_init(&newpath);
848     v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name);
849 
850     retval = v9fs_request(ctx->private, T_LINK, NULL, oldpath, &newpath);
851     v9fs_string_free(&newpath);
852     if (retval < 0) {
853         errno = -retval;
854         retval = -1;
855     }
856     return retval;
857 }
858 
proxy_truncate(FsContext * ctx,V9fsPath * fs_path,off_t size)859 static int proxy_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
860 {
861     int retval;
862 
863     retval = v9fs_request(ctx->private, T_TRUNCATE, NULL, fs_path, size);
864     if (retval < 0) {
865         errno = -retval;
866         return -1;
867     }
868     return 0;
869 }
870 
proxy_rename(FsContext * ctx,const char * oldpath,const char * newpath)871 static int proxy_rename(FsContext *ctx, const char *oldpath,
872                         const char *newpath)
873 {
874     int retval;
875     V9fsString oldname, newname;
876 
877     v9fs_string_init(&oldname);
878     v9fs_string_init(&newname);
879 
880     v9fs_string_sprintf(&oldname, "%s", oldpath);
881     v9fs_string_sprintf(&newname, "%s", newpath);
882     retval = v9fs_request(ctx->private, T_RENAME, NULL, &oldname, &newname);
883     v9fs_string_free(&oldname);
884     v9fs_string_free(&newname);
885     if (retval < 0) {
886         errno = -retval;
887     }
888     return retval;
889 }
890 
proxy_chown(FsContext * fs_ctx,V9fsPath * fs_path,FsCred * credp)891 static int proxy_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
892 {
893     int retval;
894     retval = v9fs_request(fs_ctx->private, T_CHOWN, NULL, fs_path,
895                           credp->fc_uid, credp->fc_gid);
896     if (retval < 0) {
897         errno = -retval;
898     }
899     return retval;
900 }
901 
proxy_utimensat(FsContext * s,V9fsPath * fs_path,const struct timespec * buf)902 static int proxy_utimensat(FsContext *s, V9fsPath *fs_path,
903                            const struct timespec *buf)
904 {
905     int retval;
906     retval = v9fs_request(s->private, T_UTIME, NULL, fs_path,
907                           buf[0].tv_sec, buf[0].tv_nsec,
908                           buf[1].tv_sec, buf[1].tv_nsec);
909     if (retval < 0) {
910         errno = -retval;
911     }
912     return retval;
913 }
914 
proxy_remove(FsContext * ctx,const char * path)915 static int proxy_remove(FsContext *ctx, const char *path)
916 {
917     int retval;
918     V9fsString name;
919     v9fs_string_init(&name);
920     v9fs_string_sprintf(&name, "%s", path);
921     retval = v9fs_request(ctx->private, T_REMOVE, NULL, &name);
922     v9fs_string_free(&name);
923     if (retval < 0) {
924         errno = -retval;
925     }
926     return retval;
927 }
928 
proxy_fsync(FsContext * ctx,int fid_type,V9fsFidOpenState * fs,int datasync)929 static int proxy_fsync(FsContext *ctx, int fid_type,
930                        V9fsFidOpenState *fs, int datasync)
931 {
932     int fd;
933 
934     if (fid_type == P9_FID_DIR) {
935         fd = dirfd(fs->dir.stream);
936     } else {
937         fd = fs->fd;
938     }
939 
940     if (datasync) {
941         return qemu_fdatasync(fd);
942     } else {
943         return fsync(fd);
944     }
945 }
946 
proxy_statfs(FsContext * s,V9fsPath * fs_path,struct statfs * stbuf)947 static int proxy_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
948 {
949     int retval;
950     retval = v9fs_request(s->private, T_STATFS, stbuf, fs_path);
951     if (retval < 0) {
952         errno = -retval;
953         return -1;
954     }
955     return retval;
956 }
957 
proxy_lgetxattr(FsContext * ctx,V9fsPath * fs_path,const char * name,void * value,size_t size)958 static ssize_t proxy_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
959                                const char *name, void *value, size_t size)
960 {
961     int retval;
962     V9fsString xname;
963 
964     v9fs_string_init(&xname);
965     v9fs_string_sprintf(&xname, "%s", name);
966     retval = v9fs_request(ctx->private, T_LGETXATTR, value, size, fs_path,
967                           &xname);
968     v9fs_string_free(&xname);
969     if (retval < 0) {
970         errno = -retval;
971     }
972     return retval;
973 }
974 
proxy_llistxattr(FsContext * ctx,V9fsPath * fs_path,void * value,size_t size)975 static ssize_t proxy_llistxattr(FsContext *ctx, V9fsPath *fs_path,
976                                 void *value, size_t size)
977 {
978     int retval;
979     retval = v9fs_request(ctx->private, T_LLISTXATTR, value, size, fs_path);
980     if (retval < 0) {
981         errno = -retval;
982     }
983     return retval;
984 }
985 
proxy_lsetxattr(FsContext * ctx,V9fsPath * fs_path,const char * name,void * value,size_t size,int flags)986 static int proxy_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
987                            void *value, size_t size, int flags)
988 {
989     int retval;
990     V9fsString xname, xvalue;
991 
992     v9fs_string_init(&xname);
993     v9fs_string_sprintf(&xname, "%s", name);
994 
995     v9fs_string_init(&xvalue);
996     xvalue.size = size;
997     xvalue.data = g_malloc(size);
998     memcpy(xvalue.data, value, size);
999 
1000     retval = v9fs_request(ctx->private, T_LSETXATTR, value, fs_path, &xname,
1001                           &xvalue, size, flags);
1002     v9fs_string_free(&xname);
1003     v9fs_string_free(&xvalue);
1004     if (retval < 0) {
1005         errno = -retval;
1006     }
1007     return retval;
1008 }
1009 
proxy_lremovexattr(FsContext * ctx,V9fsPath * fs_path,const char * name)1010 static int proxy_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
1011                               const char *name)
1012 {
1013     int retval;
1014     V9fsString xname;
1015 
1016     v9fs_string_init(&xname);
1017     v9fs_string_sprintf(&xname, "%s", name);
1018     retval = v9fs_request(ctx->private, T_LREMOVEXATTR, NULL, fs_path, &xname);
1019     v9fs_string_free(&xname);
1020     if (retval < 0) {
1021         errno = -retval;
1022     }
1023     return retval;
1024 }
1025 
proxy_name_to_path(FsContext * ctx,V9fsPath * dir_path,const char * name,V9fsPath * target)1026 static int proxy_name_to_path(FsContext *ctx, V9fsPath *dir_path,
1027                               const char *name, V9fsPath *target)
1028 {
1029     if (dir_path) {
1030         v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
1031     } else {
1032         v9fs_path_sprintf(target, "%s", name);
1033     }
1034     return 0;
1035 }
1036 
proxy_renameat(FsContext * ctx,V9fsPath * olddir,const char * old_name,V9fsPath * newdir,const char * new_name)1037 static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
1038                           const char *old_name, V9fsPath *newdir,
1039                           const char *new_name)
1040 {
1041     int ret;
1042     V9fsString old_full_name, new_full_name;
1043 
1044     v9fs_string_init(&old_full_name);
1045     v9fs_string_init(&new_full_name);
1046 
1047     v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
1048     v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
1049 
1050     ret = proxy_rename(ctx, old_full_name.data, new_full_name.data);
1051     v9fs_string_free(&old_full_name);
1052     v9fs_string_free(&new_full_name);
1053     return ret;
1054 }
1055 
proxy_unlinkat(FsContext * ctx,V9fsPath * dir,const char * name,int flags)1056 static int proxy_unlinkat(FsContext *ctx, V9fsPath *dir,
1057                           const char *name, int flags)
1058 {
1059     int ret;
1060     V9fsString fullname;
1061     v9fs_string_init(&fullname);
1062 
1063     v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
1064     ret = proxy_remove(ctx, fullname.data);
1065     v9fs_string_free(&fullname);
1066 
1067     return ret;
1068 }
1069 
proxy_ioc_getversion(FsContext * fs_ctx,V9fsPath * path,mode_t st_mode,uint64_t * st_gen)1070 static int proxy_ioc_getversion(FsContext *fs_ctx, V9fsPath *path,
1071                                 mode_t st_mode, uint64_t *st_gen)
1072 {
1073     int err;
1074 
1075     /* Do not try to open special files like device nodes, fifos etc
1076      * we can get fd for regular files and directories only
1077      */
1078     if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
1079         errno = ENOTTY;
1080         return -1;
1081     }
1082     err = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, path);
1083     if (err < 0) {
1084         errno = -err;
1085         err = -1;
1086     }
1087     return err;
1088 }
1089 
connect_namedsocket(const char * path,Error ** errp)1090 static int connect_namedsocket(const char *path, Error **errp)
1091 {
1092     int sockfd;
1093     struct sockaddr_un helper;
1094 
1095     if (strlen(path) >= sizeof(helper.sun_path)) {
1096         error_setg(errp, "socket name too long");
1097         return -1;
1098     }
1099     sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
1100     if (sockfd < 0) {
1101         error_setg_errno(errp, errno, "failed to create client socket");
1102         return -1;
1103     }
1104     strcpy(helper.sun_path, path);
1105     helper.sun_family = AF_UNIX;
1106     if (connect(sockfd, (struct sockaddr *)&helper, sizeof(helper)) < 0) {
1107         error_setg_errno(errp, errno, "failed to connect to '%s'", path);
1108         close(sockfd);
1109         return -1;
1110     }
1111 
1112     /* remove the socket for security reasons */
1113     unlink(path);
1114     return sockfd;
1115 }
1116 
error_append_socket_sockfd_hint(Error ** errp)1117 static void error_append_socket_sockfd_hint(Error **errp)
1118 {
1119     error_append_hint(errp, "Either specify socket=/some/path where /some/path"
1120                       " points to a listening AF_UNIX socket or sock_fd=fd"
1121                       " where fd is a file descriptor to a connected AF_UNIX"
1122                       " socket\n");
1123 }
1124 
proxy_parse_opts(QemuOpts * opts,FsDriverEntry * fs,Error ** errp)1125 static int proxy_parse_opts(QemuOpts *opts, FsDriverEntry *fs, Error **errp)
1126 {
1127     const char *socket = qemu_opt_get(opts, "socket");
1128     const char *sock_fd = qemu_opt_get(opts, "sock_fd");
1129 
1130     if (!socket && !sock_fd) {
1131         error_setg(errp, "both socket and sock_fd properties are missing");
1132         error_append_socket_sockfd_hint(errp);
1133         return -1;
1134     }
1135     if (socket && sock_fd) {
1136         error_setg(errp, "both socket and sock_fd properties are set");
1137         error_append_socket_sockfd_hint(errp);
1138         return -1;
1139     }
1140     if (socket) {
1141         fs->path = g_strdup(socket);
1142         fs->export_flags |= V9FS_PROXY_SOCK_NAME;
1143     } else {
1144         fs->path = g_strdup(sock_fd);
1145         fs->export_flags |= V9FS_PROXY_SOCK_FD;
1146     }
1147     return 0;
1148 }
1149 
proxy_init(FsContext * ctx,Error ** errp)1150 static int proxy_init(FsContext *ctx, Error **errp)
1151 {
1152     V9fsProxy *proxy = g_malloc(sizeof(V9fsProxy));
1153     int sock_id;
1154 
1155     if (ctx->export_flags & V9FS_PROXY_SOCK_NAME) {
1156         sock_id = connect_namedsocket(ctx->fs_root, errp);
1157     } else {
1158         sock_id = atoi(ctx->fs_root);
1159         if (sock_id < 0) {
1160             error_setg(errp, "socket descriptor not initialized");
1161         }
1162     }
1163     if (sock_id < 0) {
1164         g_free(proxy);
1165         return -1;
1166     }
1167     g_free(ctx->fs_root);
1168     ctx->fs_root = NULL;
1169 
1170     proxy->in_iovec.iov_base  = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
1171     proxy->in_iovec.iov_len   = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
1172     proxy->out_iovec.iov_base = g_malloc(PROXY_MAX_IO_SZ + PROXY_HDR_SZ);
1173     proxy->out_iovec.iov_len  = PROXY_MAX_IO_SZ + PROXY_HDR_SZ;
1174 
1175     ctx->private = proxy;
1176     proxy->sockfd = sock_id;
1177     qemu_mutex_init(&proxy->mutex);
1178 
1179     ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
1180     ctx->exops.get_st_gen = proxy_ioc_getversion;
1181     return 0;
1182 }
1183 
proxy_cleanup(FsContext * ctx)1184 static void proxy_cleanup(FsContext *ctx)
1185 {
1186     V9fsProxy *proxy = ctx->private;
1187 
1188     if (!proxy) {
1189         return;
1190     }
1191 
1192     g_free(proxy->out_iovec.iov_base);
1193     g_free(proxy->in_iovec.iov_base);
1194     if (ctx->export_flags & V9FS_PROXY_SOCK_NAME) {
1195         close(proxy->sockfd);
1196     }
1197     g_free(proxy);
1198 }
1199 
1200 FileOperations proxy_ops = {
1201     .parse_opts   = proxy_parse_opts,
1202     .init         = proxy_init,
1203     .cleanup      = proxy_cleanup,
1204     .lstat        = proxy_lstat,
1205     .readlink     = proxy_readlink,
1206     .close        = proxy_close,
1207     .closedir     = proxy_closedir,
1208     .open         = proxy_open,
1209     .opendir      = proxy_opendir,
1210     .rewinddir    = proxy_rewinddir,
1211     .telldir      = proxy_telldir,
1212     .readdir      = proxy_readdir,
1213     .seekdir      = proxy_seekdir,
1214     .preadv       = proxy_preadv,
1215     .pwritev      = proxy_pwritev,
1216     .chmod        = proxy_chmod,
1217     .mknod        = proxy_mknod,
1218     .mkdir        = proxy_mkdir,
1219     .fstat        = proxy_fstat,
1220     .open2        = proxy_open2,
1221     .symlink      = proxy_symlink,
1222     .link         = proxy_link,
1223     .truncate     = proxy_truncate,
1224     .rename       = proxy_rename,
1225     .chown        = proxy_chown,
1226     .utimensat    = proxy_utimensat,
1227     .remove       = proxy_remove,
1228     .fsync        = proxy_fsync,
1229     .statfs       = proxy_statfs,
1230     .lgetxattr    = proxy_lgetxattr,
1231     .llistxattr   = proxy_llistxattr,
1232     .lsetxattr    = proxy_lsetxattr,
1233     .lremovexattr = proxy_lremovexattr,
1234     .name_to_path = proxy_name_to_path,
1235     .renameat     = proxy_renameat,
1236     .unlinkat     = proxy_unlinkat,
1237 };
1238