Merge tag 'pull-error-2022-10-28' of https://repo.or.cz/qemu/armbru into staging
[qemu/ar7.git] / hw / 9pfs / cofile.c
blob20f93a90e75fc7cde8d2e4c64fca326777798fb3
1 /*
2 * 9p backend
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 * Not so fast! You might want to read the 9p developer docs first:
16 * https://wiki.qemu.org/Documentation/9p
19 #include "qemu/osdep.h"
20 #include "fsdev/qemu-fsdev.h"
21 #include "qemu/thread.h"
22 #include "qemu/coroutine.h"
23 #include "qemu/main-loop.h"
24 #include "coth.h"
26 int coroutine_fn v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t st_mode,
27 V9fsStatDotl *v9stat)
29 int err = 0;
30 V9fsState *s = pdu->s;
32 if (v9fs_request_cancelled(pdu)) {
33 return -EINTR;
35 if (s->ctx.exops.get_st_gen) {
36 v9fs_path_read_lock(s);
37 v9fs_co_run_in_worker(
39 err = s->ctx.exops.get_st_gen(&s->ctx, path, st_mode,
40 &v9stat->st_gen);
41 if (err < 0) {
42 err = -errno;
44 });
45 v9fs_path_unlock(s);
47 return err;
50 int coroutine_fn v9fs_co_lstat(V9fsPDU *pdu, V9fsPath *path, struct stat *stbuf)
52 int err;
53 V9fsState *s = pdu->s;
55 if (v9fs_request_cancelled(pdu)) {
56 return -EINTR;
58 v9fs_path_read_lock(s);
59 v9fs_co_run_in_worker(
61 err = s->ops->lstat(&s->ctx, path, stbuf);
62 if (err < 0) {
63 err = -errno;
65 });
66 v9fs_path_unlock(s);
67 return err;
70 int coroutine_fn v9fs_co_fstat(V9fsPDU *pdu, V9fsFidState *fidp,
71 struct stat *stbuf)
73 int err;
74 V9fsState *s = pdu->s;
76 if (v9fs_request_cancelled(pdu)) {
77 return -EINTR;
79 v9fs_co_run_in_worker(
81 err = s->ops->fstat(&s->ctx, fidp->fid_type, &fidp->fs, stbuf);
82 if (err < 0) {
83 err = -errno;
85 });
87 * Some FS driver (local:mapped-file) can't support fetching attributes
88 * using file descriptor. Use Path name in that case.
90 if (err == -EOPNOTSUPP) {
91 err = v9fs_co_lstat(pdu, &fidp->path, stbuf);
92 if (err == -ENOENT) {
94 * fstat on an unlinked file. Work with partial results
95 * returned from s->ops->fstat
97 err = 0;
100 return err;
103 int coroutine_fn v9fs_co_open(V9fsPDU *pdu, V9fsFidState *fidp, int flags)
105 int err;
106 V9fsState *s = pdu->s;
108 if (v9fs_request_cancelled(pdu)) {
109 return -EINTR;
111 v9fs_path_read_lock(s);
112 v9fs_co_run_in_worker(
114 err = s->ops->open(&s->ctx, &fidp->path, flags, &fidp->fs);
115 if (err == -1) {
116 err = -errno;
117 } else {
118 err = 0;
121 v9fs_path_unlock(s);
122 if (!err) {
123 total_open_fd++;
124 if (total_open_fd > open_fd_hw) {
125 v9fs_reclaim_fd(pdu);
128 return err;
131 int coroutine_fn v9fs_co_open2(V9fsPDU *pdu, V9fsFidState *fidp,
132 V9fsString *name, gid_t gid, int flags, int mode,
133 struct stat *stbuf)
135 int err;
136 FsCred cred;
137 V9fsPath path;
138 V9fsState *s = pdu->s;
140 if (v9fs_request_cancelled(pdu)) {
141 return -EINTR;
143 cred_init(&cred);
144 cred.fc_mode = mode & 07777;
145 cred.fc_uid = fidp->uid;
146 cred.fc_gid = gid;
148 * Hold the directory fid lock so that directory path name
149 * don't change. Take the write lock to be sure this fid
150 * cannot be used by another operation.
152 v9fs_path_write_lock(s);
153 v9fs_co_run_in_worker(
155 err = s->ops->open2(&s->ctx, &fidp->path,
156 name->data, flags, &cred, &fidp->fs);
157 if (err < 0) {
158 err = -errno;
159 } else {
160 v9fs_path_init(&path);
161 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
162 if (!err) {
163 err = s->ops->lstat(&s->ctx, &path, stbuf);
164 if (err < 0) {
165 err = -errno;
166 s->ops->close(&s->ctx, &fidp->fs);
167 } else {
168 v9fs_path_copy(&fidp->path, &path);
170 } else {
171 s->ops->close(&s->ctx, &fidp->fs);
173 v9fs_path_free(&path);
176 v9fs_path_unlock(s);
177 if (!err) {
178 total_open_fd++;
179 if (total_open_fd > open_fd_hw) {
180 v9fs_reclaim_fd(pdu);
183 return err;
186 int coroutine_fn v9fs_co_close(V9fsPDU *pdu, V9fsFidOpenState *fs)
188 int err;
189 V9fsState *s = pdu->s;
191 if (v9fs_request_cancelled(pdu)) {
192 return -EINTR;
194 v9fs_co_run_in_worker(
196 err = s->ops->close(&s->ctx, fs);
197 if (err < 0) {
198 err = -errno;
201 if (!err) {
202 total_open_fd--;
204 return err;
207 int coroutine_fn v9fs_co_fsync(V9fsPDU *pdu, V9fsFidState *fidp, int datasync)
209 int err;
210 V9fsState *s = pdu->s;
212 if (v9fs_request_cancelled(pdu)) {
213 return -EINTR;
215 v9fs_co_run_in_worker(
217 err = s->ops->fsync(&s->ctx, fidp->fid_type, &fidp->fs, datasync);
218 if (err < 0) {
219 err = -errno;
222 return err;
225 int coroutine_fn v9fs_co_link(V9fsPDU *pdu, V9fsFidState *oldfid,
226 V9fsFidState *newdirfid, V9fsString *name)
228 int err;
229 V9fsState *s = pdu->s;
231 if (v9fs_request_cancelled(pdu)) {
232 return -EINTR;
234 v9fs_path_read_lock(s);
235 v9fs_co_run_in_worker(
237 err = s->ops->link(&s->ctx, &oldfid->path,
238 &newdirfid->path, name->data);
239 if (err < 0) {
240 err = -errno;
243 v9fs_path_unlock(s);
244 return err;
247 int coroutine_fn v9fs_co_pwritev(V9fsPDU *pdu, V9fsFidState *fidp,
248 struct iovec *iov, int iovcnt, int64_t offset)
250 int err;
251 V9fsState *s = pdu->s;
253 if (v9fs_request_cancelled(pdu)) {
254 return -EINTR;
256 fsdev_co_throttle_request(s->ctx.fst, true, iov, iovcnt);
257 v9fs_co_run_in_worker(
259 err = s->ops->pwritev(&s->ctx, &fidp->fs, iov, iovcnt, offset);
260 if (err < 0) {
261 err = -errno;
264 return err;
267 int coroutine_fn v9fs_co_preadv(V9fsPDU *pdu, V9fsFidState *fidp,
268 struct iovec *iov, int iovcnt, int64_t offset)
270 int err;
271 V9fsState *s = pdu->s;
273 if (v9fs_request_cancelled(pdu)) {
274 return -EINTR;
276 fsdev_co_throttle_request(s->ctx.fst, false, iov, iovcnt);
277 v9fs_co_run_in_worker(
279 err = s->ops->preadv(&s->ctx, &fidp->fs, iov, iovcnt, offset);
280 if (err < 0) {
281 err = -errno;
284 return err;