qga: Delete redundant spaces
[qemu/ar7.git] / hw / 9pfs / cofs.c
blob0b321b456e3eb21a7b620d74233173230cb15ec3
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 "qemu/main-loop.h"
19 #include "coth.h"
21 static ssize_t __readlink(V9fsState *s, V9fsPath *path, V9fsString *buf)
23 ssize_t len, maxlen = PATH_MAX;
25 buf->data = g_malloc(PATH_MAX);
26 for (;;) {
27 len = s->ops->readlink(&s->ctx, path, buf->data, maxlen);
28 if (len < 0) {
29 g_free(buf->data);
30 buf->data = NULL;
31 buf->size = 0;
32 break;
33 } else if (len == maxlen) {
35 * We dodn't have space to put the NULL or we have more
36 * to read. Increase the size and try again
38 maxlen *= 2;
39 g_free(buf->data);
40 buf->data = g_malloc(maxlen);
41 continue;
44 * Null terminate the readlink output
46 buf->data[len] = '\0';
47 buf->size = len;
48 break;
50 return len;
53 int coroutine_fn v9fs_co_readlink(V9fsPDU *pdu, V9fsPath *path, V9fsString *buf)
55 int err;
56 V9fsState *s = pdu->s;
58 if (v9fs_request_cancelled(pdu)) {
59 return -EINTR;
61 v9fs_path_read_lock(s);
62 v9fs_co_run_in_worker(
64 err = __readlink(s, path, buf);
65 if (err < 0) {
66 err = -errno;
68 });
69 v9fs_path_unlock(s);
70 return err;
73 int coroutine_fn v9fs_co_statfs(V9fsPDU *pdu, V9fsPath *path,
74 struct statfs *stbuf)
76 int err;
77 V9fsState *s = pdu->s;
79 if (v9fs_request_cancelled(pdu)) {
80 return -EINTR;
82 v9fs_path_read_lock(s);
83 v9fs_co_run_in_worker(
85 err = s->ops->statfs(&s->ctx, path, stbuf);
86 if (err < 0) {
87 err = -errno;
89 });
90 v9fs_path_unlock(s);
91 return err;
94 int coroutine_fn v9fs_co_chmod(V9fsPDU *pdu, V9fsPath *path, mode_t mode)
96 int err;
97 FsCred cred;
98 V9fsState *s = pdu->s;
100 if (v9fs_request_cancelled(pdu)) {
101 return -EINTR;
103 cred_init(&cred);
104 cred.fc_mode = mode;
105 v9fs_path_read_lock(s);
106 v9fs_co_run_in_worker(
108 err = s->ops->chmod(&s->ctx, path, &cred);
109 if (err < 0) {
110 err = -errno;
113 v9fs_path_unlock(s);
114 return err;
117 int coroutine_fn v9fs_co_utimensat(V9fsPDU *pdu, V9fsPath *path,
118 struct timespec times[2])
120 int err;
121 V9fsState *s = pdu->s;
123 if (v9fs_request_cancelled(pdu)) {
124 return -EINTR;
126 v9fs_path_read_lock(s);
127 v9fs_co_run_in_worker(
129 err = s->ops->utimensat(&s->ctx, path, times);
130 if (err < 0) {
131 err = -errno;
134 v9fs_path_unlock(s);
135 return err;
138 int coroutine_fn v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid,
139 gid_t gid)
141 int err;
142 FsCred cred;
143 V9fsState *s = pdu->s;
145 if (v9fs_request_cancelled(pdu)) {
146 return -EINTR;
148 cred_init(&cred);
149 cred.fc_uid = uid;
150 cred.fc_gid = gid;
151 v9fs_path_read_lock(s);
152 v9fs_co_run_in_worker(
154 err = s->ops->chown(&s->ctx, path, &cred);
155 if (err < 0) {
156 err = -errno;
159 v9fs_path_unlock(s);
160 return err;
163 int coroutine_fn v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
165 int err;
166 V9fsState *s = pdu->s;
168 if (v9fs_request_cancelled(pdu)) {
169 return -EINTR;
171 v9fs_path_read_lock(s);
172 v9fs_co_run_in_worker(
174 err = s->ops->truncate(&s->ctx, path, size);
175 if (err < 0) {
176 err = -errno;
179 v9fs_path_unlock(s);
180 return err;
183 int coroutine_fn v9fs_co_mknod(V9fsPDU *pdu, V9fsFidState *fidp,
184 V9fsString *name, uid_t uid, gid_t gid,
185 dev_t dev, mode_t mode, struct stat *stbuf)
187 int err;
188 V9fsPath path;
189 FsCred cred;
190 V9fsState *s = pdu->s;
192 if (v9fs_request_cancelled(pdu)) {
193 return -EINTR;
195 cred_init(&cred);
196 cred.fc_uid = uid;
197 cred.fc_gid = gid;
198 cred.fc_mode = mode;
199 cred.fc_rdev = dev;
200 v9fs_path_read_lock(s);
201 v9fs_co_run_in_worker(
203 err = s->ops->mknod(&s->ctx, &fidp->path, name->data, &cred);
204 if (err < 0) {
205 err = -errno;
206 } else {
207 v9fs_path_init(&path);
208 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
209 if (!err) {
210 err = s->ops->lstat(&s->ctx, &path, stbuf);
211 if (err < 0) {
212 err = -errno;
215 v9fs_path_free(&path);
218 v9fs_path_unlock(s);
219 return err;
222 /* Only works with path name based fid */
223 int coroutine_fn v9fs_co_remove(V9fsPDU *pdu, V9fsPath *path)
225 int err;
226 V9fsState *s = pdu->s;
228 if (v9fs_request_cancelled(pdu)) {
229 return -EINTR;
231 v9fs_path_read_lock(s);
232 v9fs_co_run_in_worker(
234 err = s->ops->remove(&s->ctx, path->data);
235 if (err < 0) {
236 err = -errno;
239 v9fs_path_unlock(s);
240 return err;
243 int coroutine_fn v9fs_co_unlinkat(V9fsPDU *pdu, V9fsPath *path,
244 V9fsString *name, int flags)
246 int err;
247 V9fsState *s = pdu->s;
249 if (v9fs_request_cancelled(pdu)) {
250 return -EINTR;
252 v9fs_path_read_lock(s);
253 v9fs_co_run_in_worker(
255 err = s->ops->unlinkat(&s->ctx, path, name->data, flags);
256 if (err < 0) {
257 err = -errno;
260 v9fs_path_unlock(s);
261 return err;
264 /* Only work with path name based fid */
265 int coroutine_fn v9fs_co_rename(V9fsPDU *pdu, V9fsPath *oldpath,
266 V9fsPath *newpath)
268 int err;
269 V9fsState *s = pdu->s;
271 if (v9fs_request_cancelled(pdu)) {
272 return -EINTR;
274 v9fs_co_run_in_worker(
276 err = s->ops->rename(&s->ctx, oldpath->data, newpath->data);
277 if (err < 0) {
278 err = -errno;
281 return err;
284 int coroutine_fn v9fs_co_renameat(V9fsPDU *pdu, V9fsPath *olddirpath,
285 V9fsString *oldname, V9fsPath *newdirpath,
286 V9fsString *newname)
288 int err;
289 V9fsState *s = pdu->s;
291 if (v9fs_request_cancelled(pdu)) {
292 return -EINTR;
294 v9fs_co_run_in_worker(
296 err = s->ops->renameat(&s->ctx, olddirpath, oldname->data,
297 newdirpath, newname->data);
298 if (err < 0) {
299 err = -errno;
302 return err;
305 int coroutine_fn v9fs_co_symlink(V9fsPDU *pdu, V9fsFidState *dfidp,
306 V9fsString *name, const char *oldpath,
307 gid_t gid, struct stat *stbuf)
309 int err;
310 FsCred cred;
311 V9fsPath path;
312 V9fsState *s = pdu->s;
314 if (v9fs_request_cancelled(pdu)) {
315 return -EINTR;
317 cred_init(&cred);
318 cred.fc_uid = dfidp->uid;
319 cred.fc_gid = gid;
320 cred.fc_mode = 0777;
321 v9fs_path_read_lock(s);
322 v9fs_co_run_in_worker(
324 err = s->ops->symlink(&s->ctx, oldpath, &dfidp->path,
325 name->data, &cred);
326 if (err < 0) {
327 err = -errno;
328 } else {
329 v9fs_path_init(&path);
330 err = v9fs_name_to_path(s, &dfidp->path, name->data, &path);
331 if (!err) {
332 err = s->ops->lstat(&s->ctx, &path, stbuf);
333 if (err < 0) {
334 err = -errno;
337 v9fs_path_free(&path);
340 v9fs_path_unlock(s);
341 return err;
345 * For path name based fid we don't block. So we can
346 * directly call the fs driver ops.
348 int coroutine_fn v9fs_co_name_to_path(V9fsPDU *pdu, V9fsPath *dirpath,
349 const char *name, V9fsPath *path)
351 int err;
352 V9fsState *s = pdu->s;
354 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
355 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
356 if (err < 0) {
357 err = -errno;
359 } else {
360 if (v9fs_request_cancelled(pdu)) {
361 return -EINTR;
363 v9fs_co_run_in_worker(
365 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
366 if (err < 0) {
367 err = -errno;
371 return err;