]> git.proxmox.com Git - mirror_qemu.git/blame - hw/9pfs/cofs.c
9pfs: Clean up includes
[mirror_qemu.git] / hw / 9pfs / cofs.c
CommitLineData
86e42d74
VJ
1
2/*
3 * Virtio 9p backend
4 *
5 * Copyright IBM, Corp. 2011
6 *
7 * Authors:
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
12 *
13 */
14
fbc04127 15#include "qemu/osdep.h"
86e42d74 16#include "fsdev/qemu-fsdev.h"
1de7afc9 17#include "qemu/thread.h"
10817bf0 18#include "qemu/coroutine.h"
fe52840c 19#include "coth.h"
86e42d74 20
4fa4ce71
CG
21static ssize_t __readlink(V9fsState *s, V9fsPath *path, V9fsString *buf)
22{
23 ssize_t len, maxlen = PATH_MAX;
24
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) {
34 /*
35 * We dodn't have space to put the NULL or we have more
36 * to read. Increase the size and try again
37 */
38 maxlen *= 2;
39 g_free(buf->data);
40 buf->data = g_malloc(maxlen);
41 continue;
42 }
43 /*
44 * Null terminate the readlink output
45 */
46 buf->data[len] = '\0';
47 buf->size = len;
48 break;
49 }
50 return len;
51}
52
bccacf6c 53int v9fs_co_readlink(V9fsPDU *pdu, V9fsPath *path, V9fsString *buf)
86e42d74
VJ
54{
55 int err;
bccacf6c 56 V9fsState *s = pdu->s;
86e42d74 57
bccacf6c
AK
58 if (v9fs_request_cancelled(pdu)) {
59 return -EINTR;
60 }
532decb7 61 v9fs_path_read_lock(s);
86e42d74
VJ
62 v9fs_co_run_in_worker(
63 {
4fa4ce71
CG
64 err = __readlink(s, path, buf);
65 if (err < 0) {
86e42d74
VJ
66 err = -errno;
67 }
68 });
532decb7 69 v9fs_path_unlock(s);
86e42d74
VJ
70 return err;
71}
94840ff9 72
bccacf6c 73int v9fs_co_statfs(V9fsPDU *pdu, V9fsPath *path, struct statfs *stbuf)
94840ff9
AK
74{
75 int err;
bccacf6c 76 V9fsState *s = pdu->s;
94840ff9 77
bccacf6c
AK
78 if (v9fs_request_cancelled(pdu)) {
79 return -EINTR;
80 }
532decb7 81 v9fs_path_read_lock(s);
94840ff9
AK
82 v9fs_co_run_in_worker(
83 {
2289be19 84 err = s->ops->statfs(&s->ctx, path, stbuf);
94840ff9
AK
85 if (err < 0) {
86 err = -errno;
87 }
88 });
532decb7 89 v9fs_path_unlock(s);
94840ff9
AK
90 return err;
91}
4011ead2 92
bccacf6c 93int v9fs_co_chmod(V9fsPDU *pdu, V9fsPath *path, mode_t mode)
4011ead2
AK
94{
95 int err;
96 FsCred cred;
bccacf6c 97 V9fsState *s = pdu->s;
4011ead2 98
bccacf6c
AK
99 if (v9fs_request_cancelled(pdu)) {
100 return -EINTR;
101 }
4011ead2
AK
102 cred_init(&cred);
103 cred.fc_mode = mode;
532decb7 104 v9fs_path_read_lock(s);
4011ead2
AK
105 v9fs_co_run_in_worker(
106 {
2289be19 107 err = s->ops->chmod(&s->ctx, path, &cred);
4011ead2
AK
108 if (err < 0) {
109 err = -errno;
110 }
111 });
532decb7 112 v9fs_path_unlock(s);
4011ead2
AK
113 return err;
114}
115
bccacf6c 116int v9fs_co_utimensat(V9fsPDU *pdu, V9fsPath *path,
4011ead2
AK
117 struct timespec times[2])
118{
119 int err;
bccacf6c 120 V9fsState *s = pdu->s;
4011ead2 121
bccacf6c
AK
122 if (v9fs_request_cancelled(pdu)) {
123 return -EINTR;
124 }
532decb7 125 v9fs_path_read_lock(s);
4011ead2
AK
126 v9fs_co_run_in_worker(
127 {
2289be19 128 err = s->ops->utimensat(&s->ctx, path, times);
4011ead2
AK
129 if (err < 0) {
130 err = -errno;
131 }
132 });
532decb7 133 v9fs_path_unlock(s);
4011ead2
AK
134 return err;
135}
136
bccacf6c 137int v9fs_co_chown(V9fsPDU *pdu, V9fsPath *path, uid_t uid, gid_t gid)
4011ead2
AK
138{
139 int err;
140 FsCred cred;
bccacf6c 141 V9fsState *s = pdu->s;
4011ead2 142
bccacf6c
AK
143 if (v9fs_request_cancelled(pdu)) {
144 return -EINTR;
145 }
4011ead2
AK
146 cred_init(&cred);
147 cred.fc_uid = uid;
148 cred.fc_gid = gid;
532decb7 149 v9fs_path_read_lock(s);
4011ead2
AK
150 v9fs_co_run_in_worker(
151 {
2289be19 152 err = s->ops->chown(&s->ctx, path, &cred);
4011ead2
AK
153 if (err < 0) {
154 err = -errno;
155 }
156 });
532decb7 157 v9fs_path_unlock(s);
4011ead2
AK
158 return err;
159}
160
bccacf6c 161int v9fs_co_truncate(V9fsPDU *pdu, V9fsPath *path, off_t size)
4011ead2
AK
162{
163 int err;
bccacf6c 164 V9fsState *s = pdu->s;
4011ead2 165
bccacf6c
AK
166 if (v9fs_request_cancelled(pdu)) {
167 return -EINTR;
168 }
532decb7 169 v9fs_path_read_lock(s);
4011ead2
AK
170 v9fs_co_run_in_worker(
171 {
2289be19 172 err = s->ops->truncate(&s->ctx, path, size);
4011ead2
AK
173 if (err < 0) {
174 err = -errno;
175 }
176 });
532decb7 177 v9fs_path_unlock(s);
4011ead2
AK
178 return err;
179}
00ace8c5 180
bccacf6c 181int v9fs_co_mknod(V9fsPDU *pdu, V9fsFidState *fidp, V9fsString *name, uid_t uid,
02cb7f3a 182 gid_t gid, dev_t dev, mode_t mode, struct stat *stbuf)
00ace8c5
AK
183{
184 int err;
2289be19 185 V9fsPath path;
00ace8c5 186 FsCred cred;
bccacf6c 187 V9fsState *s = pdu->s;
00ace8c5 188
bccacf6c
AK
189 if (v9fs_request_cancelled(pdu)) {
190 return -EINTR;
191 }
00ace8c5
AK
192 cred_init(&cred);
193 cred.fc_uid = uid;
194 cred.fc_gid = gid;
195 cred.fc_mode = mode;
196 cred.fc_rdev = dev;
532decb7 197 v9fs_path_read_lock(s);
00ace8c5
AK
198 v9fs_co_run_in_worker(
199 {
2289be19 200 err = s->ops->mknod(&s->ctx, &fidp->path, name->data, &cred);
00ace8c5
AK
201 if (err < 0) {
202 err = -errno;
02cb7f3a 203 } else {
2289be19
AK
204 v9fs_path_init(&path);
205 err = v9fs_name_to_path(s, &fidp->path, name->data, &path);
206 if (!err) {
207 err = s->ops->lstat(&s->ctx, &path, stbuf);
208 if (err < 0) {
209 err = -errno;
210 }
02cb7f3a 211 }
2289be19 212 v9fs_path_free(&path);
00ace8c5
AK
213 }
214 });
532decb7 215 v9fs_path_unlock(s);
00ace8c5
AK
216 return err;
217}
b4b1537b 218
2289be19 219/* Only works with path name based fid */
bccacf6c 220int v9fs_co_remove(V9fsPDU *pdu, V9fsPath *path)
b4b1537b
VJ
221{
222 int err;
bccacf6c 223 V9fsState *s = pdu->s;
b4b1537b 224
bccacf6c
AK
225 if (v9fs_request_cancelled(pdu)) {
226 return -EINTR;
227 }
532decb7 228 v9fs_path_read_lock(s);
b4b1537b
VJ
229 v9fs_co_run_in_worker(
230 {
231 err = s->ops->remove(&s->ctx, path->data);
232 if (err < 0) {
233 err = -errno;
234 }
235 });
532decb7 236 v9fs_path_unlock(s);
b4b1537b
VJ
237 return err;
238}
2a487e05 239
bccacf6c 240int v9fs_co_unlinkat(V9fsPDU *pdu, V9fsPath *path, V9fsString *name, int flags)
2289be19
AK
241{
242 int err;
bccacf6c 243 V9fsState *s = pdu->s;
2289be19 244
bccacf6c
AK
245 if (v9fs_request_cancelled(pdu)) {
246 return -EINTR;
247 }
532decb7 248 v9fs_path_read_lock(s);
2289be19
AK
249 v9fs_co_run_in_worker(
250 {
251 err = s->ops->unlinkat(&s->ctx, path, name->data, flags);
252 if (err < 0) {
253 err = -errno;
254 }
255 });
532decb7 256 v9fs_path_unlock(s);
2289be19
AK
257 return err;
258}
259
260/* Only work with path name based fid */
bccacf6c 261int v9fs_co_rename(V9fsPDU *pdu, V9fsPath *oldpath, V9fsPath *newpath)
2a487e05
AK
262{
263 int err;
bccacf6c 264 V9fsState *s = pdu->s;
2a487e05 265
bccacf6c
AK
266 if (v9fs_request_cancelled(pdu)) {
267 return -EINTR;
268 }
2a487e05
AK
269 v9fs_co_run_in_worker(
270 {
271 err = s->ops->rename(&s->ctx, oldpath->data, newpath->data);
272 if (err < 0) {
273 err = -errno;
274 }
275 });
276 return err;
277}
02ac7a34 278
bccacf6c 279int v9fs_co_renameat(V9fsPDU *pdu, V9fsPath *olddirpath, V9fsString *oldname,
2289be19
AK
280 V9fsPath *newdirpath, V9fsString *newname)
281{
282 int err;
bccacf6c 283 V9fsState *s = pdu->s;
2289be19 284
bccacf6c
AK
285 if (v9fs_request_cancelled(pdu)) {
286 return -EINTR;
287 }
2289be19
AK
288 v9fs_co_run_in_worker(
289 {
290 err = s->ops->renameat(&s->ctx, olddirpath, oldname->data,
291 newdirpath, newname->data);
292 if (err < 0) {
293 err = -errno;
294 }
295 });
296 return err;
297}
298
bccacf6c 299int v9fs_co_symlink(V9fsPDU *pdu, V9fsFidState *dfidp, V9fsString *name,
02cb7f3a 300 const char *oldpath, gid_t gid, struct stat *stbuf)
02ac7a34
VJ
301{
302 int err;
303 FsCred cred;
2289be19 304 V9fsPath path;
bccacf6c 305 V9fsState *s = pdu->s;
02cb7f3a 306
bccacf6c
AK
307 if (v9fs_request_cancelled(pdu)) {
308 return -EINTR;
309 }
02ac7a34 310 cred_init(&cred);
02cb7f3a 311 cred.fc_uid = dfidp->uid;
02ac7a34
VJ
312 cred.fc_gid = gid;
313 cred.fc_mode = 0777;
532decb7 314 v9fs_path_read_lock(s);
02ac7a34
VJ
315 v9fs_co_run_in_worker(
316 {
2289be19
AK
317 err = s->ops->symlink(&s->ctx, oldpath, &dfidp->path,
318 name->data, &cred);
02ac7a34
VJ
319 if (err < 0) {
320 err = -errno;
02cb7f3a 321 } else {
2289be19
AK
322 v9fs_path_init(&path);
323 err = v9fs_name_to_path(s, &dfidp->path, name->data, &path);
324 if (!err) {
325 err = s->ops->lstat(&s->ctx, &path, stbuf);
326 if (err < 0) {
327 err = -errno;
328 }
02cb7f3a 329 }
2289be19 330 v9fs_path_free(&path);
02ac7a34
VJ
331 }
332 });
532decb7 333 v9fs_path_unlock(s);
2289be19
AK
334 return err;
335}
336
337/*
338 * For path name based fid we don't block. So we can
339 * directly call the fs driver ops.
340 */
bccacf6c 341int v9fs_co_name_to_path(V9fsPDU *pdu, V9fsPath *dirpath,
2289be19
AK
342 const char *name, V9fsPath *path)
343{
344 int err;
bccacf6c 345 V9fsState *s = pdu->s;
532decb7 346
c98f1d4a 347 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) {
532decb7
AK
348 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
349 if (err < 0) {
350 err = -errno;
351 }
352 } else {
bccacf6c
AK
353 if (v9fs_request_cancelled(pdu)) {
354 return -EINTR;
355 }
532decb7
AK
356 v9fs_co_run_in_worker(
357 {
358 err = s->ops->name_to_path(&s->ctx, dirpath, name, path);
359 if (err < 0) {
360 err = -errno;
361 }
362 });
2289be19 363 }
02ac7a34
VJ
364 return err;
365}