5 * Copyright IBM, Corp. 2011
8 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
10 * This work is licensed under the terms of the GNU GPL, version 2. See
11 * the COPYING file in the top-level directory.
15 #include "fsdev/qemu-fsdev.h"
16 #include "qemu/thread.h"
17 #include "block/coroutine.h"
18 #include "virtio-9p-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
);
26 len
= s
->ops
->readlink(&s
->ctx
, path
, buf
->data
, maxlen
);
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
39 buf
->data
= g_malloc(maxlen
);
43 * Null terminate the readlink output
45 buf
->data
[len
] = '\0';
52 int v9fs_co_readlink(V9fsPDU
*pdu
, V9fsPath
*path
, V9fsString
*buf
)
55 V9fsState
*s
= pdu
->s
;
57 if (v9fs_request_cancelled(pdu
)) {
60 v9fs_path_read_lock(s
);
61 v9fs_co_run_in_worker(
63 err
= __readlink(s
, path
, buf
);
72 int v9fs_co_statfs(V9fsPDU
*pdu
, V9fsPath
*path
, struct statfs
*stbuf
)
75 V9fsState
*s
= pdu
->s
;
77 if (v9fs_request_cancelled(pdu
)) {
80 v9fs_path_read_lock(s
);
81 v9fs_co_run_in_worker(
83 err
= s
->ops
->statfs(&s
->ctx
, path
, stbuf
);
92 int v9fs_co_chmod(V9fsPDU
*pdu
, V9fsPath
*path
, mode_t mode
)
96 V9fsState
*s
= pdu
->s
;
98 if (v9fs_request_cancelled(pdu
)) {
103 v9fs_path_read_lock(s
);
104 v9fs_co_run_in_worker(
106 err
= s
->ops
->chmod(&s
->ctx
, path
, &cred
);
115 int v9fs_co_utimensat(V9fsPDU
*pdu
, V9fsPath
*path
,
116 struct timespec times
[2])
119 V9fsState
*s
= pdu
->s
;
121 if (v9fs_request_cancelled(pdu
)) {
124 v9fs_path_read_lock(s
);
125 v9fs_co_run_in_worker(
127 err
= s
->ops
->utimensat(&s
->ctx
, path
, times
);
136 int v9fs_co_chown(V9fsPDU
*pdu
, V9fsPath
*path
, uid_t uid
, gid_t gid
)
140 V9fsState
*s
= pdu
->s
;
142 if (v9fs_request_cancelled(pdu
)) {
148 v9fs_path_read_lock(s
);
149 v9fs_co_run_in_worker(
151 err
= s
->ops
->chown(&s
->ctx
, path
, &cred
);
160 int v9fs_co_truncate(V9fsPDU
*pdu
, V9fsPath
*path
, off_t size
)
163 V9fsState
*s
= pdu
->s
;
165 if (v9fs_request_cancelled(pdu
)) {
168 v9fs_path_read_lock(s
);
169 v9fs_co_run_in_worker(
171 err
= s
->ops
->truncate(&s
->ctx
, path
, size
);
180 int v9fs_co_mknod(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsString
*name
, uid_t uid
,
181 gid_t gid
, dev_t dev
, mode_t mode
, struct stat
*stbuf
)
186 V9fsState
*s
= pdu
->s
;
188 if (v9fs_request_cancelled(pdu
)) {
196 v9fs_path_read_lock(s
);
197 v9fs_co_run_in_worker(
199 err
= s
->ops
->mknod(&s
->ctx
, &fidp
->path
, name
->data
, &cred
);
203 v9fs_path_init(&path
);
204 err
= v9fs_name_to_path(s
, &fidp
->path
, name
->data
, &path
);
206 err
= s
->ops
->lstat(&s
->ctx
, &path
, stbuf
);
211 v9fs_path_free(&path
);
218 /* Only works with path name based fid */
219 int v9fs_co_remove(V9fsPDU
*pdu
, V9fsPath
*path
)
222 V9fsState
*s
= pdu
->s
;
224 if (v9fs_request_cancelled(pdu
)) {
227 v9fs_path_read_lock(s
);
228 v9fs_co_run_in_worker(
230 err
= s
->ops
->remove(&s
->ctx
, path
->data
);
239 int v9fs_co_unlinkat(V9fsPDU
*pdu
, V9fsPath
*path
, V9fsString
*name
, int flags
)
242 V9fsState
*s
= pdu
->s
;
244 if (v9fs_request_cancelled(pdu
)) {
247 v9fs_path_read_lock(s
);
248 v9fs_co_run_in_worker(
250 err
= s
->ops
->unlinkat(&s
->ctx
, path
, name
->data
, flags
);
259 /* Only work with path name based fid */
260 int v9fs_co_rename(V9fsPDU
*pdu
, V9fsPath
*oldpath
, V9fsPath
*newpath
)
263 V9fsState
*s
= pdu
->s
;
265 if (v9fs_request_cancelled(pdu
)) {
268 v9fs_co_run_in_worker(
270 err
= s
->ops
->rename(&s
->ctx
, oldpath
->data
, newpath
->data
);
278 int v9fs_co_renameat(V9fsPDU
*pdu
, V9fsPath
*olddirpath
, V9fsString
*oldname
,
279 V9fsPath
*newdirpath
, V9fsString
*newname
)
282 V9fsState
*s
= pdu
->s
;
284 if (v9fs_request_cancelled(pdu
)) {
287 v9fs_co_run_in_worker(
289 err
= s
->ops
->renameat(&s
->ctx
, olddirpath
, oldname
->data
,
290 newdirpath
, newname
->data
);
298 int v9fs_co_symlink(V9fsPDU
*pdu
, V9fsFidState
*dfidp
, V9fsString
*name
,
299 const char *oldpath
, gid_t gid
, struct stat
*stbuf
)
304 V9fsState
*s
= pdu
->s
;
306 if (v9fs_request_cancelled(pdu
)) {
310 cred
.fc_uid
= dfidp
->uid
;
313 v9fs_path_read_lock(s
);
314 v9fs_co_run_in_worker(
316 err
= s
->ops
->symlink(&s
->ctx
, oldpath
, &dfidp
->path
,
321 v9fs_path_init(&path
);
322 err
= v9fs_name_to_path(s
, &dfidp
->path
, name
->data
, &path
);
324 err
= s
->ops
->lstat(&s
->ctx
, &path
, stbuf
);
329 v9fs_path_free(&path
);
337 * For path name based fid we don't block. So we can
338 * directly call the fs driver ops.
340 int v9fs_co_name_to_path(V9fsPDU
*pdu
, V9fsPath
*dirpath
,
341 const char *name
, V9fsPath
*path
)
344 V9fsState
*s
= pdu
->s
;
346 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
347 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
352 if (v9fs_request_cancelled(pdu
)) {
355 v9fs_co_run_in_worker(
357 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);