nbd/server: Add FLAG_PAYLOAD support to CMD_BLOCK_STATUS
[qemu/ericb.git] / hw / 9pfs / cofs.c
blob67e3ae5c5ccd14237073ed44e7170f1401c3fdcd
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/main-loop.h"
23 #include "coth.h"
25 static ssize_t __readlink(V9fsState *s, V9fsPath *path, V9fsString *buf)
27 ssize_t len, maxlen = PATH_MAX;
29 buf->data = g_malloc(PATH_MAX);
30 for (;;) {
31 len = s->ops->readlink(&s->ctx, path, buf->data, maxlen);
32 if (len < 0) {
33 g_free(buf->data);
34 buf->data = NULL;
35 buf->size = 0;
36 break;
37 } else if (len == maxlen) {
39 * We dodn't have space to put the NULL or we have more
40 * to read. Increase the size and try again
42 maxlen *= 2;
43 g_free(buf->data);
44 buf->data = g_malloc(maxlen);
45 continue;
48 * Null terminate the readlink output
50 buf->data[len] = '\0';
51 buf->size = len;
52 break;
54 return len;
57 int coroutine_fn v9fs_co_readlink(V9fsPDU *pdu, V9fsPath *path, V9fsString *buf)
59 int err;
60 V9fsState *s = pdu->s;
62 if (v9fs_request_cancelled(pdu)) {
63 return -EINTR;
65 v9fs_path_read_lock(s);
66 v9fs_co_run_in_worker(
68 err = __readlink(s, path, buf);
69 if (err < 0) {
70 err = -errno;
72 });
73 v9fs_path_unlock(s);
74 return err;
77 int coroutine_fn v9fs_co_statfs(V9fsPDU *pdu, V9fsPath *path,
78 struct statfs *stbuf)
80 int err;
81 V9fsState *s = pdu->s;
83 if (v9fs_request_cancelled(pdu)) {
84 return -EINTR;
86 v9fs_path_read_lock(s);
87 v9fs_co_run_in_worker(
89 err = s->ops->statfs(&s->ctx, path, stbuf);
90 if (err < 0) {
91 err = -errno;
93 });
94 v9fs_path_unlock(s);
95 return err;
98 int coroutine_fn v9fs_co_chmod(V9fsPDU *pdu, V9fsPath *path, mode_t mode)
100 int err;
101 FsCred cred;
102 V9fsState *s = pdu->s;
104 if (v9fs_request_cancelled(pdu)) {
105 return -EINTR;
107 cred_init(&cred);
108 cred.fc_mode = mode;
109 v9fs_path_read_lock(s);
110 v9fs_co_run_in_worker(
112 err = s->ops->chmod(&s->ctx, path, &cred);
113 if (err < 0) {
114 err = -errno;
117 v9fs_path_unlock(s);
118 return err;
121 int coroutine_fn v9fs_co_utimensat(V9fsPDU *pdu, V9fsPath *path,
122 struct timespec times[2])
124 int err;
125 V9fsState *s = pdu->s;
127 if (v9fs_request_cancelled(pdu)) {
128 return -EINTR;
130 v9fs_path_read_lock(s);
131 v9fs_co_run_in_worker(
133 err = s->ops->utimensat(&s->ctx, path, times);
134 if (err < 0) {
135 err = -errno;
138 v9fs_path_unlock(s);
139 return err;
142 int coroutine_fn v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid,
143 gid_t gid)
145 int err;
146 FsCred cred;
147 V9fsState *s = pdu->s;
149 if (v9fs_request_cancelled(pdu)) {
150 return -EINTR;
152 cred_init(&cred);
153 cred.fc_uid = uid;
154 cred.fc_gid = gid;
155 v9fs_path_read_lock(s);
156 v9fs_co_run_in_worker(
158 err = s->ops->chown(&s->ctx, path, &cred);
159 if (err < 0) {
160 err = -errno;
163 v9fs_path_unlock(s);
164 return err;
167 int coroutine_fn v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
169 int err;
170 V9fsState *s = pdu->s;
172 if (v9fs_request_cancelled(pdu)) {
173 return -EINTR;
175 v9fs_path_read_lock(s);
176 v9fs_co_run_in_worker(
178 err = s->ops->truncate(&s->ctx, path, size);
179 if (err < 0) {
180 err = -errno;
183 v9fs_path_unlock(s);
184 return err;
187 int coroutine_fn v9fs_co_mknod(V9fsPDU *pdu, V9fsFidState *fidp,
188 V9fsString *name, uid_t uid, gid_t gid,
189 dev_t dev, mode_t mode, struct stat *stbuf)
191 int err;
192 V9fsPath path;
193 FsCred cred;
194 V9fsState *s = pdu->s;
196 if (v9fs_request_cancelled(pdu)) {
197 return -EINTR;
199 cred_init(&cred);
200 cred.fc_uid = uid;
201 cred.fc_gid = gid;
202 cred.fc_mode = mode;
203 cred.fc_rdev = dev;
204 v9fs_path_read_lock(s);
205 v9fs_co_run_in_worker(
207 err = s->ops->mknod(&s->ctx, &fidp->path, name->data, &cred);
208 if (err < 0) {
209 err = -errno;
210 } else {
211 v9fs_path_init(&path);
212 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
213 if (!err) {
214 err = s->ops->lstat(&s->ctx, &path, stbuf);
215 if (err < 0) {
216 err = -errno;
219 v9fs_path_free(&path);
222 v9fs_path_unlock(s);
223 return err;
226 /* Only works with path name based fid */
227 int coroutine_fn v9fs_co_remove(V9fsPDU *pdu, V9fsPath *path)
229 int err;
230 V9fsState *s = pdu->s;
232 if (v9fs_request_cancelled(pdu)) {
233 return -EINTR;
235 v9fs_path_read_lock(s);
236 v9fs_co_run_in_worker(
238 err = s->ops->remove(&s->ctx, path->data);
239 if (err < 0) {
240 err = -errno;
243 v9fs_path_unlock(s);
244 return err;
247 int coroutine_fn v9fs_co_unlinkat(V9fsPDU *pdu, V9fsPath *path,
248 V9fsString *name, int flags)
250 int err;
251 V9fsState *s = pdu->s;
253 if (v9fs_request_cancelled(pdu)) {
254 return -EINTR;
256 v9fs_path_read_lock(s);
257 v9fs_co_run_in_worker(
259 err = s->ops->unlinkat(&s->ctx, path, name->data, flags);
260 if (err < 0) {
261 err = -errno;
264 v9fs_path_unlock(s);
265 return err;
268 /* Only work with path name based fid */
269 int coroutine_fn v9fs_co_rename(V9fsPDU *pdu, V9fsPath *oldpath,
270 V9fsPath *newpath)
272 int err;
273 V9fsState *s = pdu->s;
275 if (v9fs_request_cancelled(pdu)) {
276 return -EINTR;
278 v9fs_co_run_in_worker(
280 err = s->ops->rename(&s->ctx, oldpath->data, newpath->data);
281 if (err < 0) {
282 err = -errno;
285 return err;
288 int coroutine_fn v9fs_co_renameat(V9fsPDU *pdu, V9fsPath *olddirpath,
289 V9fsString *oldname, V9fsPath *newdirpath,
290 V9fsString *newname)
292 int err;
293 V9fsState *s = pdu->s;
295 if (v9fs_request_cancelled(pdu)) {
296 return -EINTR;
298 v9fs_co_run_in_worker(
300 err = s->ops->renameat(&s->ctx, olddirpath, oldname->data,
301 newdirpath, newname->data);
302 if (err < 0) {
303 err = -errno;
306 return err;
309 int coroutine_fn v9fs_co_symlink(V9fsPDU *pdu, V9fsFidState *dfidp,
310 V9fsString *name, const char *oldpath,
311 gid_t gid, struct stat *stbuf)
313 int err;
314 FsCred cred;
315 V9fsPath path;
316 V9fsState *s = pdu->s;
318 if (v9fs_request_cancelled(pdu)) {
319 return -EINTR;
321 cred_init(&cred);
322 cred.fc_uid = dfidp->uid;
323 cred.fc_gid = gid;
324 cred.fc_mode = 0777;
325 v9fs_path_read_lock(s);
326 v9fs_co_run_in_worker(
328 err = s->ops->symlink(&s->ctx, oldpath, &dfidp->path,
329 name->data, &cred);
330 if (err < 0) {
331 err = -errno;
332 } else {
333 v9fs_path_init(&path);
334 err = v9fs_name_to_path(s, &dfidp->path, name->data, &path);
335 if (!err) {
336 err = s->ops->lstat(&s->ctx, &path, stbuf);
337 if (err < 0) {
338 err = -errno;
341 v9fs_path_free(&path);
344 v9fs_path_unlock(s);
345 return err;
349 * For path name based fid we don't block. So we can
350 * directly call the fs driver ops.
352 int coroutine_fn v9fs_co_name_to_path(V9fsPDU *pdu, V9fsPath *dirpath,
353 const char *name, V9fsPath *path)
355 int err;
356 V9fsState *s = pdu->s;
358 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
359 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
360 if (err < 0) {
361 err = -errno;
363 } else {
364 if (v9fs_request_cancelled(pdu)) {
365 return -EINTR;
367 v9fs_co_run_in_worker(
369 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
370 if (err < 0) {
371 err = -errno;
375 return err;