4 * Copyright IBM, Corp. 2011
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"
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
);
31 len
= s
->ops
->readlink(&s
->ctx
, path
, buf
->data
, maxlen
);
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
44 buf
->data
= g_malloc(maxlen
);
48 * Null terminate the readlink output
50 buf
->data
[len
] = '\0';
57 int coroutine_fn
v9fs_co_readlink(V9fsPDU
*pdu
, V9fsPath
*path
, V9fsString
*buf
)
60 V9fsState
*s
= pdu
->s
;
62 if (v9fs_request_cancelled(pdu
)) {
65 v9fs_path_read_lock(s
);
66 v9fs_co_run_in_worker(
68 err
= __readlink(s
, path
, buf
);
77 int coroutine_fn
v9fs_co_statfs(V9fsPDU
*pdu
, V9fsPath
*path
,
81 V9fsState
*s
= pdu
->s
;
83 if (v9fs_request_cancelled(pdu
)) {
86 v9fs_path_read_lock(s
);
87 v9fs_co_run_in_worker(
89 err
= s
->ops
->statfs(&s
->ctx
, path
, stbuf
);
98 int coroutine_fn
v9fs_co_chmod(V9fsPDU
*pdu
, V9fsPath
*path
, mode_t mode
)
102 V9fsState
*s
= pdu
->s
;
104 if (v9fs_request_cancelled(pdu
)) {
109 v9fs_path_read_lock(s
);
110 v9fs_co_run_in_worker(
112 err
= s
->ops
->chmod(&s
->ctx
, path
, &cred
);
121 int coroutine_fn
v9fs_co_utimensat(V9fsPDU
*pdu
, V9fsPath
*path
,
122 struct timespec times
[2])
125 V9fsState
*s
= pdu
->s
;
127 if (v9fs_request_cancelled(pdu
)) {
130 v9fs_path_read_lock(s
);
131 v9fs_co_run_in_worker(
133 err
= s
->ops
->utimensat(&s
->ctx
, path
, times
);
142 int coroutine_fn
v9fs_co_chown(V9fsPDU
*pdu
, V9fsPath
*path
, uid_t uid
,
147 V9fsState
*s
= pdu
->s
;
149 if (v9fs_request_cancelled(pdu
)) {
155 v9fs_path_read_lock(s
);
156 v9fs_co_run_in_worker(
158 err
= s
->ops
->chown(&s
->ctx
, path
, &cred
);
167 int coroutine_fn
v9fs_co_truncate(V9fsPDU
*pdu
, V9fsPath
*path
, off_t size
)
170 V9fsState
*s
= pdu
->s
;
172 if (v9fs_request_cancelled(pdu
)) {
175 v9fs_path_read_lock(s
);
176 v9fs_co_run_in_worker(
178 err
= s
->ops
->truncate(&s
->ctx
, path
, size
);
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
)
194 V9fsState
*s
= pdu
->s
;
196 if (v9fs_request_cancelled(pdu
)) {
204 v9fs_path_read_lock(s
);
205 v9fs_co_run_in_worker(
207 err
= s
->ops
->mknod(&s
->ctx
, &fidp
->path
, name
->data
, &cred
);
211 v9fs_path_init(&path
);
212 err
= v9fs_name_to_path(s
, &fidp
->path
, name
->data
, &path
);
214 err
= s
->ops
->lstat(&s
->ctx
, &path
, stbuf
);
219 v9fs_path_free(&path
);
226 /* Only works with path name based fid */
227 int coroutine_fn
v9fs_co_remove(V9fsPDU
*pdu
, V9fsPath
*path
)
230 V9fsState
*s
= pdu
->s
;
232 if (v9fs_request_cancelled(pdu
)) {
235 v9fs_path_read_lock(s
);
236 v9fs_co_run_in_worker(
238 err
= s
->ops
->remove(&s
->ctx
, path
->data
);
247 int coroutine_fn
v9fs_co_unlinkat(V9fsPDU
*pdu
, V9fsPath
*path
,
248 V9fsString
*name
, int flags
)
251 V9fsState
*s
= pdu
->s
;
253 if (v9fs_request_cancelled(pdu
)) {
256 v9fs_path_read_lock(s
);
257 v9fs_co_run_in_worker(
259 err
= s
->ops
->unlinkat(&s
->ctx
, path
, name
->data
, flags
);
268 /* Only work with path name based fid */
269 int coroutine_fn
v9fs_co_rename(V9fsPDU
*pdu
, V9fsPath
*oldpath
,
273 V9fsState
*s
= pdu
->s
;
275 if (v9fs_request_cancelled(pdu
)) {
278 v9fs_co_run_in_worker(
280 err
= s
->ops
->rename(&s
->ctx
, oldpath
->data
, newpath
->data
);
288 int coroutine_fn
v9fs_co_renameat(V9fsPDU
*pdu
, V9fsPath
*olddirpath
,
289 V9fsString
*oldname
, V9fsPath
*newdirpath
,
293 V9fsState
*s
= pdu
->s
;
295 if (v9fs_request_cancelled(pdu
)) {
298 v9fs_co_run_in_worker(
300 err
= s
->ops
->renameat(&s
->ctx
, olddirpath
, oldname
->data
,
301 newdirpath
, newname
->data
);
309 int coroutine_fn
v9fs_co_symlink(V9fsPDU
*pdu
, V9fsFidState
*dfidp
,
310 V9fsString
*name
, const char *oldpath
,
311 gid_t gid
, struct stat
*stbuf
)
316 V9fsState
*s
= pdu
->s
;
318 if (v9fs_request_cancelled(pdu
)) {
322 cred
.fc_uid
= dfidp
->uid
;
325 v9fs_path_read_lock(s
);
326 v9fs_co_run_in_worker(
328 err
= s
->ops
->symlink(&s
->ctx
, oldpath
, &dfidp
->path
,
333 v9fs_path_init(&path
);
334 err
= v9fs_name_to_path(s
, &dfidp
->path
, name
->data
, &path
);
336 err
= s
->ops
->lstat(&s
->ctx
, &path
, stbuf
);
341 v9fs_path_free(&path
);
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
)
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
);
364 if (v9fs_request_cancelled(pdu
)) {
367 v9fs_co_run_in_worker(
369 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);