nbd/server: rename clientflags var in nbd_negotiate_options
[qemu/ar7.git] / hw / 9pfs / cofs.c
blobc62103221d2a90497a4e219dd10496caf0a30d36
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 static ssize_t __readlink(V9fsState *s, V9fsPath *path, V9fsString *buf)
22 ssize_t len, maxlen = PATH_MAX;
24 buf->data = g_malloc(PATH_MAX);
25 for(;;) {
26 len = s->ops->readlink(&s->ctx, path, buf->data, maxlen);
27 if (len < 0) {
28 g_free(buf->data);
29 buf->data = NULL;
30 buf->size = 0;
31 break;
32 } else if (len == maxlen) {
34 * We dodn't have space to put the NULL or we have more
35 * to read. Increase the size and try again
37 maxlen *= 2;
38 g_free(buf->data);
39 buf->data = g_malloc(maxlen);
40 continue;
43 * Null terminate the readlink output
45 buf->data[len] = '\0';
46 buf->size = len;
47 break;
49 return len;
52 int coroutine_fn v9fs_co_readlink(V9fsPDU *pdu, V9fsPath *path, V9fsString *buf)
54 int err;
55 V9fsState *s = pdu->s;
57 if (v9fs_request_cancelled(pdu)) {
58 return -EINTR;
60 v9fs_path_read_lock(s);
61 v9fs_co_run_in_worker(
63 err = __readlink(s, path, buf);
64 if (err < 0) {
65 err = -errno;
67 });
68 v9fs_path_unlock(s);
69 return err;
72 int coroutine_fn v9fs_co_statfs(V9fsPDU *pdu, V9fsPath *path,
73 struct statfs *stbuf)
75 int err;
76 V9fsState *s = pdu->s;
78 if (v9fs_request_cancelled(pdu)) {
79 return -EINTR;
81 v9fs_path_read_lock(s);
82 v9fs_co_run_in_worker(
84 err = s->ops->statfs(&s->ctx, path, stbuf);
85 if (err < 0) {
86 err = -errno;
88 });
89 v9fs_path_unlock(s);
90 return err;
93 int coroutine_fn v9fs_co_chmod(V9fsPDU *pdu, V9fsPath *path, mode_t mode)
95 int err;
96 FsCred cred;
97 V9fsState *s = pdu->s;
99 if (v9fs_request_cancelled(pdu)) {
100 return -EINTR;
102 cred_init(&cred);
103 cred.fc_mode = mode;
104 v9fs_path_read_lock(s);
105 v9fs_co_run_in_worker(
107 err = s->ops->chmod(&s->ctx, path, &cred);
108 if (err < 0) {
109 err = -errno;
112 v9fs_path_unlock(s);
113 return err;
116 int coroutine_fn v9fs_co_utimensat(V9fsPDU *pdu, V9fsPath *path,
117 struct timespec times[2])
119 int err;
120 V9fsState *s = pdu->s;
122 if (v9fs_request_cancelled(pdu)) {
123 return -EINTR;
125 v9fs_path_read_lock(s);
126 v9fs_co_run_in_worker(
128 err = s->ops->utimensat(&s->ctx, path, times);
129 if (err < 0) {
130 err = -errno;
133 v9fs_path_unlock(s);
134 return err;
137 int coroutine_fn v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid,
138 gid_t gid)
140 int err;
141 FsCred cred;
142 V9fsState *s = pdu->s;
144 if (v9fs_request_cancelled(pdu)) {
145 return -EINTR;
147 cred_init(&cred);
148 cred.fc_uid = uid;
149 cred.fc_gid = gid;
150 v9fs_path_read_lock(s);
151 v9fs_co_run_in_worker(
153 err = s->ops->chown(&s->ctx, path, &cred);
154 if (err < 0) {
155 err = -errno;
158 v9fs_path_unlock(s);
159 return err;
162 int coroutine_fn v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
164 int err;
165 V9fsState *s = pdu->s;
167 if (v9fs_request_cancelled(pdu)) {
168 return -EINTR;
170 v9fs_path_read_lock(s);
171 v9fs_co_run_in_worker(
173 err = s->ops->truncate(&s->ctx, path, size);
174 if (err < 0) {
175 err = -errno;
178 v9fs_path_unlock(s);
179 return err;
182 int coroutine_fn v9fs_co_mknod(V9fsPDU *pdu, V9fsFidState *fidp,
183 V9fsString *name, uid_t uid, gid_t gid,
184 dev_t dev, mode_t mode, struct stat *stbuf)
186 int err;
187 V9fsPath path;
188 FsCred cred;
189 V9fsState *s = pdu->s;
191 if (v9fs_request_cancelled(pdu)) {
192 return -EINTR;
194 cred_init(&cred);
195 cred.fc_uid = uid;
196 cred.fc_gid = gid;
197 cred.fc_mode = mode;
198 cred.fc_rdev = dev;
199 v9fs_path_read_lock(s);
200 v9fs_co_run_in_worker(
202 err = s->ops->mknod(&s->ctx, &fidp->path, name->data, &cred);
203 if (err < 0) {
204 err = -errno;
205 } else {
206 v9fs_path_init(&path);
207 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
208 if (!err) {
209 err = s->ops->lstat(&s->ctx, &path, stbuf);
210 if (err < 0) {
211 err = -errno;
214 v9fs_path_free(&path);
217 v9fs_path_unlock(s);
218 return err;
221 /* Only works with path name based fid */
222 int coroutine_fn v9fs_co_remove(V9fsPDU *pdu, V9fsPath *path)
224 int err;
225 V9fsState *s = pdu->s;
227 if (v9fs_request_cancelled(pdu)) {
228 return -EINTR;
230 v9fs_path_read_lock(s);
231 v9fs_co_run_in_worker(
233 err = s->ops->remove(&s->ctx, path->data);
234 if (err < 0) {
235 err = -errno;
238 v9fs_path_unlock(s);
239 return err;
242 int coroutine_fn v9fs_co_unlinkat(V9fsPDU *pdu, V9fsPath *path,
243 V9fsString *name, int flags)
245 int err;
246 V9fsState *s = pdu->s;
248 if (v9fs_request_cancelled(pdu)) {
249 return -EINTR;
251 v9fs_path_read_lock(s);
252 v9fs_co_run_in_worker(
254 err = s->ops->unlinkat(&s->ctx, path, name->data, flags);
255 if (err < 0) {
256 err = -errno;
259 v9fs_path_unlock(s);
260 return err;
263 /* Only work with path name based fid */
264 int coroutine_fn v9fs_co_rename(V9fsPDU *pdu, V9fsPath *oldpath,
265 V9fsPath *newpath)
267 int err;
268 V9fsState *s = pdu->s;
270 if (v9fs_request_cancelled(pdu)) {
271 return -EINTR;
273 v9fs_co_run_in_worker(
275 err = s->ops->rename(&s->ctx, oldpath->data, newpath->data);
276 if (err < 0) {
277 err = -errno;
280 return err;
283 int coroutine_fn v9fs_co_renameat(V9fsPDU *pdu, V9fsPath *olddirpath,
284 V9fsString *oldname, V9fsPath *newdirpath,
285 V9fsString *newname)
287 int err;
288 V9fsState *s = pdu->s;
290 if (v9fs_request_cancelled(pdu)) {
291 return -EINTR;
293 v9fs_co_run_in_worker(
295 err = s->ops->renameat(&s->ctx, olddirpath, oldname->data,
296 newdirpath, newname->data);
297 if (err < 0) {
298 err = -errno;
301 return err;
304 int coroutine_fn v9fs_co_symlink(V9fsPDU *pdu, V9fsFidState *dfidp,
305 V9fsString *name, const char *oldpath,
306 gid_t gid, struct stat *stbuf)
308 int err;
309 FsCred cred;
310 V9fsPath path;
311 V9fsState *s = pdu->s;
313 if (v9fs_request_cancelled(pdu)) {
314 return -EINTR;
316 cred_init(&cred);
317 cred.fc_uid = dfidp->uid;
318 cred.fc_gid = gid;
319 cred.fc_mode = 0777;
320 v9fs_path_read_lock(s);
321 v9fs_co_run_in_worker(
323 err = s->ops->symlink(&s->ctx, oldpath, &dfidp->path,
324 name->data, &cred);
325 if (err < 0) {
326 err = -errno;
327 } else {
328 v9fs_path_init(&path);
329 err = v9fs_name_to_path(s, &dfidp->path, name->data, &path);
330 if (!err) {
331 err = s->ops->lstat(&s->ctx, &path, stbuf);
332 if (err < 0) {
333 err = -errno;
336 v9fs_path_free(&path);
339 v9fs_path_unlock(s);
340 return err;
344 * For path name based fid we don't block. So we can
345 * directly call the fs driver ops.
347 int coroutine_fn v9fs_co_name_to_path(V9fsPDU *pdu, V9fsPath *dirpath,
348 const char *name, V9fsPath *path)
350 int err;
351 V9fsState *s = pdu->s;
353 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
354 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
355 if (err < 0) {
356 err = -errno;
358 } else {
359 if (v9fs_request_cancelled(pdu)) {
360 return -EINTR;
362 v9fs_co_run_in_worker(
364 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
365 if (err < 0) {
366 err = -errno;
370 return err;