Merge remote-tracking branch 'remotes/amarkovic/tags/mips-queue-oct-2018-part-2-v2...
[qemu.git] / hw / 9pfs / codir.c
blob7cd6fce1ad3748db5b1a32585d163c07a92290d4
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.
14 #include "qemu/osdep.h"
15 #include "fsdev/qemu-fsdev.h"
16 #include "qemu/thread.h"
17 #include "qemu/coroutine.h"
18 #include "coth.h"
20 int coroutine_fn v9fs_co_readdir(V9fsPDU *pdu, V9fsFidState *fidp,
21 struct dirent **dent)
23 int err;
24 V9fsState *s = pdu->s;
26 if (v9fs_request_cancelled(pdu)) {
27 return -EINTR;
29 v9fs_co_run_in_worker(
31 struct dirent *entry;
33 errno = 0;
34 entry = s->ops->readdir(&s->ctx, &fidp->fs);
35 if (!entry && errno) {
36 err = -errno;
37 } else {
38 *dent = entry;
39 err = 0;
41 });
42 return err;
45 off_t v9fs_co_telldir(V9fsPDU *pdu, V9fsFidState *fidp)
47 off_t err;
48 V9fsState *s = pdu->s;
50 if (v9fs_request_cancelled(pdu)) {
51 return -EINTR;
53 v9fs_co_run_in_worker(
55 err = s->ops->telldir(&s->ctx, &fidp->fs);
56 if (err < 0) {
57 err = -errno;
59 });
60 return err;
63 void coroutine_fn v9fs_co_seekdir(V9fsPDU *pdu, V9fsFidState *fidp,
64 off_t offset)
66 V9fsState *s = pdu->s;
67 if (v9fs_request_cancelled(pdu)) {
68 return;
70 v9fs_co_run_in_worker(
72 s->ops->seekdir(&s->ctx, &fidp->fs, offset);
73 });
76 void coroutine_fn v9fs_co_rewinddir(V9fsPDU *pdu, V9fsFidState *fidp)
78 V9fsState *s = pdu->s;
79 if (v9fs_request_cancelled(pdu)) {
80 return;
82 v9fs_co_run_in_worker(
84 s->ops->rewinddir(&s->ctx, &fidp->fs);
85 });
88 int coroutine_fn v9fs_co_mkdir(V9fsPDU *pdu, V9fsFidState *fidp,
89 V9fsString *name, mode_t mode, uid_t uid,
90 gid_t gid, struct stat *stbuf)
92 int err;
93 FsCred cred;
94 V9fsPath path;
95 V9fsState *s = pdu->s;
97 if (v9fs_request_cancelled(pdu)) {
98 return -EINTR;
100 cred_init(&cred);
101 cred.fc_mode = mode;
102 cred.fc_uid = uid;
103 cred.fc_gid = gid;
104 v9fs_path_read_lock(s);
105 v9fs_co_run_in_worker(
107 err = s->ops->mkdir(&s->ctx, &fidp->path, name->data, &cred);
108 if (err < 0) {
109 err = -errno;
110 } else {
111 v9fs_path_init(&path);
112 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
113 if (!err) {
114 err = s->ops->lstat(&s->ctx, &path, stbuf);
115 if (err < 0) {
116 err = -errno;
119 v9fs_path_free(&path);
122 v9fs_path_unlock(s);
123 return err;
126 int coroutine_fn v9fs_co_opendir(V9fsPDU *pdu, V9fsFidState *fidp)
128 int err;
129 V9fsState *s = pdu->s;
131 if (v9fs_request_cancelled(pdu)) {
132 return -EINTR;
134 v9fs_path_read_lock(s);
135 v9fs_co_run_in_worker(
137 err = s->ops->opendir(&s->ctx, &fidp->path, &fidp->fs);
138 if (err < 0) {
139 err = -errno;
140 } else {
141 err = 0;
144 v9fs_path_unlock(s);
145 if (!err) {
146 total_open_fd++;
147 if (total_open_fd > open_fd_hw) {
148 v9fs_reclaim_fd(pdu);
151 return err;
154 int coroutine_fn v9fs_co_closedir(V9fsPDU *pdu, V9fsFidOpenState *fs)
156 int err;
157 V9fsState *s = pdu->s;
159 if (v9fs_request_cancelled(pdu)) {
160 return -EINTR;
162 v9fs_co_run_in_worker(
164 err = s->ops->closedir(&s->ctx, fs);
165 if (err < 0) {
166 err = -errno;
169 if (!err) {
170 total_open_fd--;
172 return err;