5 * Copyright IBM, Corp. 2011
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
15 #include "fsdev/qemu-fsdev.h"
16 #include "qemu/thread.h"
17 #include "block/coroutine.h"
18 #include "virtio-9p-coth.h"
28 int v9fs_co_st_gen(V9fsPDU *pdu, V9fsPath *path, mode_t st_mode,
32 V9fsState *s = pdu->s;
34 if (v9fs_request_cancelled(pdu)) {
37 if (s->ctx.exops.get_st_gen) {
38 v9fs_path_read_lock(s);
39 v9fs_co_run_in_worker(
41 err = s->ctx.exops.get_st_gen(&s->ctx, path, st_mode,
52 int v9fs_co_lstat(V9fsPDU *pdu, V9fsPath *path, struct stat *stbuf)
55 V9fsState *s = pdu->s;
57 if (v9fs_request_cancelled(pdu)) {
60 v9fs_path_read_lock(s);
61 v9fs_co_run_in_worker(
63 err = s->ops->lstat(&s->ctx, path, stbuf);
72 int v9fs_co_fstat(V9fsPDU *pdu, V9fsFidState *fidp, struct stat *stbuf)
75 V9fsState *s = pdu->s;
77 if (v9fs_request_cancelled(pdu)) {
80 v9fs_co_run_in_worker(
82 err = s->ops->fstat(&s->ctx, fidp->fid_type, &fidp->fs, stbuf);
88 * Some FS driver (local:mapped-file) can't support fetching attributes
89 * using file descriptor. Use Path name in that case.
91 if (err == -EOPNOTSUPP) {
92 err = v9fs_co_lstat(pdu, &fidp->path, stbuf);
95 * fstat on an unlinked file. Work with partial results
96 * returned from s->ops->fstat
104 int v9fs_co_open(V9fsPDU *pdu, V9fsFidState *fidp, int flags)
107 V9fsState *s = pdu->s;
109 if (v9fs_request_cancelled(pdu)) {
112 v9fs_path_read_lock(s);
113 v9fs_co_run_in_worker(
115 err = s->ops->open(&s->ctx, &fidp->path, flags, &fidp->fs);
125 if (total_open_fd > open_fd_hw) {
126 v9fs_reclaim_fd(pdu);
132 int v9fs_co_open2(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name, gid_t gid,
133 int flags, int mode, struct stat *stbuf)
138 V9fsState *s = pdu->s;
140 if (v9fs_request_cancelled(pdu)) {
144 cred.fc_mode = mode & 07777;
145 cred.fc_uid = fidp->uid;
148 * Hold the directory fid lock so that directory path name
149 * don't change. Read lock is fine because this fid cannot
150 * be used by any other operation.
152 v9fs_path_read_lock(s);
153 v9fs_co_run_in_worker(
155 err = s->ops->open2(&s->ctx, &fidp->path,
156 name->data, flags, &cred, &fidp->fs);
160 v9fs_path_init(&path);
161 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
163 err = s->ops->lstat(&s->ctx, &path, stbuf);
166 s->ops->close(&s->ctx, &fidp->fs);
168 v9fs_path_copy(&fidp->path, &path);
171 s->ops->close(&s->ctx, &fidp->fs);
173 v9fs_path_free(&path);
179 if (total_open_fd > open_fd_hw) {
180 v9fs_reclaim_fd(pdu);
186 int v9fs_co_close(V9fsPDU *pdu, V9fsFidOpenState *fs)
189 V9fsState *s = pdu->s;
191 if (v9fs_request_cancelled(pdu)) {
194 v9fs_co_run_in_worker(
196 err = s->ops->close(&s->ctx, fs);
207 int v9fs_co_fsync(V9fsPDU *pdu, V9fsFidState *fidp, int datasync)
210 V9fsState *s = pdu->s;
212 if (v9fs_request_cancelled(pdu)) {
215 v9fs_co_run_in_worker(
217 err = s->ops->fsync(&s->ctx, fidp->fid_type, &fidp->fs, datasync);
225 int v9fs_co_link(V9fsPDU *pdu, V9fsFidState *oldfid,
226 V9fsFidState *newdirfid, V9fsString *name)
229 V9fsState *s = pdu->s;
231 if (v9fs_request_cancelled(pdu)) {
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);
247 int v9fs_co_pwritev(V9fsPDU *pdu, V9fsFidState *fidp,
248 struct iovec *iov, int iovcnt, int64_t offset)
251 V9fsState *s = pdu->s;
253 if (v9fs_request_cancelled(pdu)) {
256 v9fs_co_run_in_worker(
258 err = s->ops->pwritev(&s->ctx, &fidp->fs, iov, iovcnt, offset);
266 int v9fs_co_preadv(V9fsPDU *pdu, V9fsFidState *fidp,
267 struct iovec *iov, int iovcnt, int64_t offset)
270 V9fsState *s = pdu->s;
272 if (v9fs_request_cancelled(pdu)) {
275 v9fs_co_run_in_worker(
277 err = s->ops->preadv(&s->ctx, &fidp->fs, iov, iovcnt, offset);
288 #define fsync _commit