xref: /openbmc/qemu/hw/9pfs/cofile.c (revision 7a462745)
1172198d4SAneesh Kumar K.V 
2172198d4SAneesh Kumar K.V /*
3172198d4SAneesh Kumar K.V  * Virtio 9p backend
4172198d4SAneesh Kumar K.V  *
5172198d4SAneesh Kumar K.V  * Copyright IBM, Corp. 2011
6172198d4SAneesh Kumar K.V  *
7172198d4SAneesh Kumar K.V  * Authors:
8172198d4SAneesh Kumar K.V  *  Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9172198d4SAneesh Kumar K.V  *
10172198d4SAneesh Kumar K.V  * This work is licensed under the terms of the GNU GPL, version 2.  See
11172198d4SAneesh Kumar K.V  * the COPYING file in the top-level directory.
12172198d4SAneesh Kumar K.V  *
13172198d4SAneesh Kumar K.V  */
14172198d4SAneesh Kumar K.V 
15172198d4SAneesh Kumar K.V #include "fsdev/qemu-fsdev.h"
16172198d4SAneesh Kumar K.V #include "qemu-thread.h"
17172198d4SAneesh Kumar K.V #include "qemu-coroutine.h"
18172198d4SAneesh Kumar K.V #include "virtio-9p-coth.h"
19172198d4SAneesh Kumar K.V 
20172198d4SAneesh Kumar K.V int v9fs_co_lstat(V9fsState *s, V9fsString *path, struct stat *stbuf)
21172198d4SAneesh Kumar K.V {
22172198d4SAneesh Kumar K.V     int err;
23172198d4SAneesh Kumar K.V 
24172198d4SAneesh Kumar K.V     v9fs_co_run_in_worker(
25172198d4SAneesh Kumar K.V         {
26172198d4SAneesh Kumar K.V             err = s->ops->lstat(&s->ctx, path->data, stbuf);
27172198d4SAneesh Kumar K.V             if (err < 0) {
28172198d4SAneesh Kumar K.V                 err = -errno;
29172198d4SAneesh Kumar K.V             }
30172198d4SAneesh Kumar K.V         });
31172198d4SAneesh Kumar K.V     return err;
32172198d4SAneesh Kumar K.V }
3303feb1e1SAneesh Kumar K.V 
3403feb1e1SAneesh Kumar K.V int v9fs_co_fstat(V9fsState *s, int fd, struct stat *stbuf)
3503feb1e1SAneesh Kumar K.V {
3603feb1e1SAneesh Kumar K.V     int err;
3703feb1e1SAneesh Kumar K.V 
3803feb1e1SAneesh Kumar K.V     v9fs_co_run_in_worker(
3903feb1e1SAneesh Kumar K.V         {
4003feb1e1SAneesh Kumar K.V             err = s->ops->fstat(&s->ctx, fd, stbuf);
4103feb1e1SAneesh Kumar K.V             if (err < 0) {
4203feb1e1SAneesh Kumar K.V                 err = -errno;
4303feb1e1SAneesh Kumar K.V             }
4403feb1e1SAneesh Kumar K.V         });
4503feb1e1SAneesh Kumar K.V     return err;
4603feb1e1SAneesh Kumar K.V }
47f6b7f0abSAneesh Kumar K.V 
48f6b7f0abSAneesh Kumar K.V int v9fs_co_open(V9fsState *s, V9fsFidState *fidp, int flags)
49f6b7f0abSAneesh Kumar K.V {
50f6b7f0abSAneesh Kumar K.V     int err;
51f6b7f0abSAneesh Kumar K.V 
52f6b7f0abSAneesh Kumar K.V     v9fs_co_run_in_worker(
53f6b7f0abSAneesh Kumar K.V         {
54f6b7f0abSAneesh Kumar K.V             fidp->fs.fd = s->ops->open(&s->ctx, fidp->path.data, flags);
55f6b7f0abSAneesh Kumar K.V             if (fidp->fs.fd == -1) {
56f6b7f0abSAneesh Kumar K.V                 err = -errno;
57f6b7f0abSAneesh Kumar K.V             } else {
58f6b7f0abSAneesh Kumar K.V                 err = 0;
59f6b7f0abSAneesh Kumar K.V             }
60f6b7f0abSAneesh Kumar K.V         });
61*7a462745SAneesh Kumar K.V     if (!err) {
62*7a462745SAneesh Kumar K.V         total_open_fd++;
63*7a462745SAneesh Kumar K.V         if (total_open_fd > open_fd_hw) {
64*7a462745SAneesh Kumar K.V             v9fs_reclaim_fd(s);
65*7a462745SAneesh Kumar K.V         }
66*7a462745SAneesh Kumar K.V     }
67f6b7f0abSAneesh Kumar K.V     return err;
68f6b7f0abSAneesh Kumar K.V }
69e4de4232SVenkateswararao Jujjuri 
70e4de4232SVenkateswararao Jujjuri int v9fs_co_open2(V9fsState *s, V9fsFidState *fidp, char *fullname, gid_t gid,
71e4de4232SVenkateswararao Jujjuri                   int flags, int mode)
72e4de4232SVenkateswararao Jujjuri {
73e4de4232SVenkateswararao Jujjuri     int err;
74e4de4232SVenkateswararao Jujjuri     FsCred cred;
75e4de4232SVenkateswararao Jujjuri 
76e4de4232SVenkateswararao Jujjuri     cred_init(&cred);
77e4de4232SVenkateswararao Jujjuri     cred.fc_mode = mode & 07777;
78e4de4232SVenkateswararao Jujjuri     cred.fc_uid = fidp->uid;
79e4de4232SVenkateswararao Jujjuri     cred.fc_gid = gid;
80e4de4232SVenkateswararao Jujjuri     v9fs_co_run_in_worker(
81e4de4232SVenkateswararao Jujjuri         {
82e4de4232SVenkateswararao Jujjuri             fidp->fs.fd = s->ops->open2(&s->ctx, fullname, flags, &cred);
83e4de4232SVenkateswararao Jujjuri             err = 0;
84e4de4232SVenkateswararao Jujjuri             if (fidp->fs.fd == -1) {
85e4de4232SVenkateswararao Jujjuri                 err = -errno;
86e4de4232SVenkateswararao Jujjuri             }
87e4de4232SVenkateswararao Jujjuri         });
88*7a462745SAneesh Kumar K.V     if (!err) {
89*7a462745SAneesh Kumar K.V         total_open_fd++;
90*7a462745SAneesh Kumar K.V         if (total_open_fd > open_fd_hw) {
91*7a462745SAneesh Kumar K.V             v9fs_reclaim_fd(s);
92*7a462745SAneesh Kumar K.V         }
93*7a462745SAneesh Kumar K.V     }
94e4de4232SVenkateswararao Jujjuri     return err;
95e4de4232SVenkateswararao Jujjuri }
96bed4352cSAneesh Kumar K.V 
97*7a462745SAneesh Kumar K.V int v9fs_co_close(V9fsState *s, int fd)
98bed4352cSAneesh Kumar K.V {
99bed4352cSAneesh Kumar K.V     int err;
100bed4352cSAneesh Kumar K.V 
101bed4352cSAneesh Kumar K.V     v9fs_co_run_in_worker(
102bed4352cSAneesh Kumar K.V         {
103bed4352cSAneesh Kumar K.V             err = s->ops->close(&s->ctx, fd);
104bed4352cSAneesh Kumar K.V             if (err < 0) {
105bed4352cSAneesh Kumar K.V                 err = -errno;
106bed4352cSAneesh Kumar K.V             }
107bed4352cSAneesh Kumar K.V         });
108*7a462745SAneesh Kumar K.V     if (!err) {
109*7a462745SAneesh Kumar K.V         total_open_fd--;
110*7a462745SAneesh Kumar K.V     }
111bed4352cSAneesh Kumar K.V     return err;
112bed4352cSAneesh Kumar K.V }
1134743d1f5SAneesh Kumar K.V 
1144743d1f5SAneesh Kumar K.V int v9fs_co_fsync(V9fsState *s, V9fsFidState *fidp, int datasync)
1154743d1f5SAneesh Kumar K.V {
1164743d1f5SAneesh Kumar K.V     int fd;
1174743d1f5SAneesh Kumar K.V     int err;
1184743d1f5SAneesh Kumar K.V 
1194743d1f5SAneesh Kumar K.V     fd = fidp->fs.fd;
1204743d1f5SAneesh Kumar K.V     v9fs_co_run_in_worker(
1214743d1f5SAneesh Kumar K.V         {
1224743d1f5SAneesh Kumar K.V             err = s->ops->fsync(&s->ctx, fd, datasync);
1234743d1f5SAneesh Kumar K.V             if (err < 0) {
1244743d1f5SAneesh Kumar K.V                 err = -errno;
1254743d1f5SAneesh Kumar K.V             }
1264743d1f5SAneesh Kumar K.V         });
1274743d1f5SAneesh Kumar K.V     return err;
1284743d1f5SAneesh Kumar K.V }
129c6c069b0SVenkateswararao Jujjuri 
130c6c069b0SVenkateswararao Jujjuri int v9fs_co_link(V9fsState *s, V9fsString *oldpath, V9fsString *newpath)
131c6c069b0SVenkateswararao Jujjuri {
132c6c069b0SVenkateswararao Jujjuri     int err;
133c6c069b0SVenkateswararao Jujjuri 
134c6c069b0SVenkateswararao Jujjuri     v9fs_co_run_in_worker(
135c6c069b0SVenkateswararao Jujjuri         {
136c6c069b0SVenkateswararao Jujjuri             err = s->ops->link(&s->ctx, oldpath->data, newpath->data);
137c6c069b0SVenkateswararao Jujjuri             if (err < 0) {
138c6c069b0SVenkateswararao Jujjuri                 err = -errno;
139c6c069b0SVenkateswararao Jujjuri             }
140c6c069b0SVenkateswararao Jujjuri         });
141c6c069b0SVenkateswararao Jujjuri     return err;
142c6c069b0SVenkateswararao Jujjuri }
143f6b3c976SAneesh Kumar K.V 
144f6b3c976SAneesh Kumar K.V int v9fs_co_pwritev(V9fsState *s, V9fsFidState *fidp,
145f6b3c976SAneesh Kumar K.V                     struct iovec *iov, int iovcnt, int64_t offset)
146f6b3c976SAneesh Kumar K.V {
147f6b3c976SAneesh Kumar K.V     int fd;
148f6b3c976SAneesh Kumar K.V     int err;
149f6b3c976SAneesh Kumar K.V 
150f6b3c976SAneesh Kumar K.V     fd = fidp->fs.fd;
151f6b3c976SAneesh Kumar K.V     v9fs_co_run_in_worker(
152f6b3c976SAneesh Kumar K.V         {
153f6b3c976SAneesh Kumar K.V             err = s->ops->pwritev(&s->ctx, fd, iov, iovcnt, offset);
154f6b3c976SAneesh Kumar K.V             if (err < 0) {
155f6b3c976SAneesh Kumar K.V                 err = -errno;
156f6b3c976SAneesh Kumar K.V             }
157f6b3c976SAneesh Kumar K.V         });
158f6b3c976SAneesh Kumar K.V     return err;
159f6b3c976SAneesh Kumar K.V }
1607eafdcc9SAneesh Kumar K.V 
1617eafdcc9SAneesh Kumar K.V int v9fs_co_preadv(V9fsState *s, V9fsFidState *fidp,
1627eafdcc9SAneesh Kumar K.V                    struct iovec *iov, int iovcnt, int64_t offset)
1637eafdcc9SAneesh Kumar K.V {
1647eafdcc9SAneesh Kumar K.V     int fd;
1657eafdcc9SAneesh Kumar K.V     int err;
1667eafdcc9SAneesh Kumar K.V 
1677eafdcc9SAneesh Kumar K.V     fd = fidp->fs.fd;
1687eafdcc9SAneesh Kumar K.V     v9fs_co_run_in_worker(
1697eafdcc9SAneesh Kumar K.V         {
1707eafdcc9SAneesh Kumar K.V             err = s->ops->preadv(&s->ctx, fd, iov, iovcnt, offset);
1717eafdcc9SAneesh Kumar K.V             if (err < 0) {
1727eafdcc9SAneesh Kumar K.V                 err = -errno;
1737eafdcc9SAneesh Kumar K.V             }
1747eafdcc9SAneesh Kumar K.V         });
1757eafdcc9SAneesh Kumar K.V     return err;
1767eafdcc9SAneesh Kumar K.V }
177