]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/cofile.c
block: move include files to include/block/
[mirror_qemu.git] / hw / 9pfs / cofile.c
CommitLineData
172198d4
AK
1
2/*
3 * Virtio 9p backend
4 *
5 * Copyright IBM, Corp. 2011
6 *
7 * Authors:
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
12 *
13 */
14
15#include "fsdev/qemu-fsdev.h"
16#include "qemu-thread.h"
737e150e 17#include "block/coroutine.h"
172198d4
AK
18#include "virtio-9p-coth.h"
19
e06a765e
HPB
20int v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t st_mode,
21 V9fsStatDotl *v9stat)
22{
23 int err = 0;
24 V9fsState *s = pdu->s;
25
26 if (v9fs_request_cancelled(pdu)) {
27 return -EINTR;
28 }
29 if (s->ctx.exops.get_st_gen) {
30 v9fs_path_read_lock(s);
31 v9fs_co_run_in_worker(
32 {
33 err = s->ctx.exops.get_st_gen(&s->ctx, path, st_mode,
34 &v9stat->st_gen);
35 if (err < 0) {
36 err = -errno;
37 }
38 });
39 v9fs_path_unlock(s);
40 }
41 return err;
42}
43
bccacf6c 44int v9fs_co_lstat(V9fsPDU *pdu, V9fsPath *path, struct stat *stbuf)
172198d4
AK
45{
46 int err;
bccacf6c 47 V9fsState *s = pdu->s;
172198d4 48
bccacf6c
AK
49 if (v9fs_request_cancelled(pdu)) {
50 return -EINTR;
51 }
532decb7 52 v9fs_path_read_lock(s);
172198d4
AK
53 v9fs_co_run_in_worker(
54 {
2289be19 55 err = s->ops->lstat(&s->ctx, path, stbuf);
172198d4
AK
56 if (err < 0) {
57 err = -errno;
58 }
59 });
532decb7 60 v9fs_path_unlock(s);
172198d4
AK
61 return err;
62}
03feb1e1 63
cc720ddb 64int v9fs_co_fstat(V9fsPDU *pdu, V9fsFidState *fidp, struct stat *stbuf)
03feb1e1
AK
65{
66 int err;
bccacf6c 67 V9fsState *s = pdu->s;
03feb1e1 68
bccacf6c
AK
69 if (v9fs_request_cancelled(pdu)) {
70 return -EINTR;
71 }
03feb1e1
AK
72 v9fs_co_run_in_worker(
73 {
8b888272 74 err = s->ops->fstat(&s->ctx, fidp->fid_type, &fidp->fs, stbuf);
03feb1e1
AK
75 if (err < 0) {
76 err = -errno;
77 }
78 });
2c30dd74
AK
79 /*
80 * Some FS driver (local:mapped-file) can't support fetching attributes
81 * using file descriptor. Use Path name in that case.
82 */
83 if (err == -EOPNOTSUPP) {
84 err = v9fs_co_lstat(pdu, &fidp->path, stbuf);
85 if (err == -ENOENT) {
86 /*
87 * fstat on an unlinked file. Work with partial results
88 * returned from s->ops->fstat
89 */
90 err = 0;
91 }
92 }
03feb1e1
AK
93 return err;
94}
f6b7f0ab 95
bccacf6c 96int v9fs_co_open(V9fsPDU *pdu, V9fsFidState *fidp, int flags)
f6b7f0ab
AK
97{
98 int err;
bccacf6c 99 V9fsState *s = pdu->s;
f6b7f0ab 100
bccacf6c
AK
101 if (v9fs_request_cancelled(pdu)) {
102 return -EINTR;
103 }
532decb7 104 v9fs_path_read_lock(s);
f6b7f0ab
AK
105 v9fs_co_run_in_worker(
106 {
cc720ddb
AK
107 err = s->ops->open(&s->ctx, &fidp->path, flags, &fidp->fs);
108 if (err == -1) {
f6b7f0ab
AK
109 err = -errno;
110 } else {
111 err = 0;
112 }
113 });
532decb7 114 v9fs_path_unlock(s);
7a462745
AK
115 if (!err) {
116 total_open_fd++;
117 if (total_open_fd > open_fd_hw) {
bccacf6c 118 v9fs_reclaim_fd(pdu);
7a462745
AK
119 }
120 }
f6b7f0ab
AK
121 return err;
122}
e4de4232 123
bccacf6c 124int v9fs_co_open2(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name, gid_t gid,
02cb7f3a 125 int flags, int mode, struct stat *stbuf)
e4de4232
VJ
126{
127 int err;
128 FsCred cred;
2289be19 129 V9fsPath path;
bccacf6c 130 V9fsState *s = pdu->s;
2289be19 131
bccacf6c
AK
132 if (v9fs_request_cancelled(pdu)) {
133 return -EINTR;
134 }
e4de4232
VJ
135 cred_init(&cred);
136 cred.fc_mode = mode & 07777;
137 cred.fc_uid = fidp->uid;
138 cred.fc_gid = gid;
02cb7f3a
AK
139 /*
140 * Hold the directory fid lock so that directory path name
141 * don't change. Read lock is fine because this fid cannot
142 * be used by any other operation.
143 */
532decb7 144 v9fs_path_read_lock(s);
e4de4232
VJ
145 v9fs_co_run_in_worker(
146 {
cc720ddb
AK
147 err = s->ops->open2(&s->ctx, &fidp->path,
148 name->data, flags, &cred, &fidp->fs);
149 if (err < 0) {
e4de4232 150 err = -errno;
02cb7f3a 151 } else {
2289be19
AK
152 v9fs_path_init(&path);
153 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
154 if (!err) {
155 err = s->ops->lstat(&s->ctx, &path, stbuf);
156 if (err < 0) {
157 err = -errno;
cc720ddb 158 s->ops->close(&s->ctx, &fidp->fs);
2289be19
AK
159 } else {
160 v9fs_path_copy(&fidp->path, &path);
161 }
02cb7f3a 162 } else {
cc720ddb 163 s->ops->close(&s->ctx, &fidp->fs);
02cb7f3a 164 }
2289be19 165 v9fs_path_free(&path);
e4de4232
VJ
166 }
167 });
532decb7 168 v9fs_path_unlock(s);
7a462745
AK
169 if (!err) {
170 total_open_fd++;
171 if (total_open_fd > open_fd_hw) {
bccacf6c 172 v9fs_reclaim_fd(pdu);
7a462745
AK
173 }
174 }
e4de4232
VJ
175 return err;
176}
bed4352c 177
cc720ddb 178int v9fs_co_close(V9fsPDU *pdu, V9fsFidOpenState *fs)
bed4352c 179{
bed4352c 180 int err;
bccacf6c 181 V9fsState *s = pdu->s;
bed4352c 182
bccacf6c
AK
183 if (v9fs_request_cancelled(pdu)) {
184 return -EINTR;
185 }
bed4352c
AK
186 v9fs_co_run_in_worker(
187 {
cc720ddb 188 err = s->ops->close(&s->ctx, fs);
bed4352c
AK
189 if (err < 0) {
190 err = -errno;
191 }
192 });
7a462745
AK
193 if (!err) {
194 total_open_fd--;
195 }
bed4352c
AK
196 return err;
197}
4743d1f5 198
bccacf6c 199int v9fs_co_fsync(V9fsPDU *pdu, V9fsFidState *fidp, int datasync)
4743d1f5 200{
cc720ddb 201 int err;
bccacf6c 202 V9fsState *s = pdu->s;
4743d1f5 203
bccacf6c
AK
204 if (v9fs_request_cancelled(pdu)) {
205 return -EINTR;
206 }
4743d1f5
AK
207 v9fs_co_run_in_worker(
208 {
8b888272 209 err = s->ops->fsync(&s->ctx, fidp->fid_type, &fidp->fs, datasync);
4743d1f5
AK
210 if (err < 0) {
211 err = -errno;
212 }
213 });
214 return err;
215}
c6c069b0 216
bccacf6c 217int v9fs_co_link(V9fsPDU *pdu, V9fsFidState *oldfid,
2289be19 218 V9fsFidState *newdirfid, V9fsString *name)
c6c069b0
VJ
219{
220 int err;
bccacf6c 221 V9fsState *s = pdu->s;
c6c069b0 222
bccacf6c
AK
223 if (v9fs_request_cancelled(pdu)) {
224 return -EINTR;
225 }
532decb7 226 v9fs_path_read_lock(s);
c6c069b0
VJ
227 v9fs_co_run_in_worker(
228 {
2289be19
AK
229 err = s->ops->link(&s->ctx, &oldfid->path,
230 &newdirfid->path, name->data);
c6c069b0
VJ
231 if (err < 0) {
232 err = -errno;
233 }
234 });
532decb7 235 v9fs_path_unlock(s);
c6c069b0
VJ
236 return err;
237}
f6b3c976 238
bccacf6c 239int v9fs_co_pwritev(V9fsPDU *pdu, V9fsFidState *fidp,
f6b3c976
AK
240 struct iovec *iov, int iovcnt, int64_t offset)
241{
cc720ddb 242 int err;
bccacf6c 243 V9fsState *s = pdu->s;
f6b3c976 244
bccacf6c
AK
245 if (v9fs_request_cancelled(pdu)) {
246 return -EINTR;
247 }
f6b3c976
AK
248 v9fs_co_run_in_worker(
249 {
cc720ddb 250 err = s->ops->pwritev(&s->ctx, &fidp->fs, iov, iovcnt, offset);
f6b3c976
AK
251 if (err < 0) {
252 err = -errno;
253 }
254 });
255 return err;
256}
7eafdcc9 257
bccacf6c 258int v9fs_co_preadv(V9fsPDU *pdu, V9fsFidState *fidp,
7eafdcc9
AK
259 struct iovec *iov, int iovcnt, int64_t offset)
260{
cc720ddb 261 int err;
bccacf6c 262 V9fsState *s = pdu->s;
7eafdcc9 263
bccacf6c
AK
264 if (v9fs_request_cancelled(pdu)) {
265 return -EINTR;
266 }
7eafdcc9
AK
267 v9fs_co_run_in_worker(
268 {
cc720ddb 269 err = s->ops->preadv(&s->ctx, &fidp->fs, iov, iovcnt, offset);
7eafdcc9
AK
270 if (err < 0) {
271 err = -errno;
272 }
273 });
274 return err;
275}