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 "qemu/osdep.h"
16 #include "fsdev/qemu-fsdev.h"
17 #include "qemu/thread.h"
18 #include "qemu/coroutine.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
);
27 len
= s
->ops
->readlink(&s
->ctx
, path
, buf
->data
, maxlen
);
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
40 buf
->data
= g_malloc(maxlen
);
44 * Null terminate the readlink output
46 buf
->data
[len
] = '\0';
53 int v9fs_co_readlink(V9fsPDU
*pdu
, V9fsPath
*path
, V9fsString
*buf
)
56 V9fsState
*s
= pdu
->s
;
58 if (v9fs_request_cancelled(pdu
)) {
61 v9fs_path_read_lock(s
);
62 v9fs_co_run_in_worker(
64 err
= __readlink(s
, path
, buf
);
73 int v9fs_co_statfs(V9fsPDU
*pdu
, V9fsPath
*path
, struct statfs
*stbuf
)
76 V9fsState
*s
= pdu
->s
;
78 if (v9fs_request_cancelled(pdu
)) {
81 v9fs_path_read_lock(s
);
82 v9fs_co_run_in_worker(
84 err
= s
->ops
->statfs(&s
->ctx
, path
, stbuf
);
93 int v9fs_co_chmod(V9fsPDU
*pdu
, V9fsPath
*path
, mode_t mode
)
97 V9fsState
*s
= pdu
->s
;
99 if (v9fs_request_cancelled(pdu
)) {
104 v9fs_path_read_lock(s
);
105 v9fs_co_run_in_worker(
107 err
= s
->ops
->chmod(&s
->ctx
, path
, &cred
);
116 int v9fs_co_utimensat(V9fsPDU
*pdu
, V9fsPath
*path
,
117 struct timespec times
[2])
120 V9fsState
*s
= pdu
->s
;
122 if (v9fs_request_cancelled(pdu
)) {
125 v9fs_path_read_lock(s
);
126 v9fs_co_run_in_worker(
128 err
= s
->ops
->utimensat(&s
->ctx
, path
, times
);
137 int v9fs_co_chown(V9fsPDU
*pdu
, V9fsPath
*path
, uid_t uid
, gid_t gid
)
141 V9fsState
*s
= pdu
->s
;
143 if (v9fs_request_cancelled(pdu
)) {
149 v9fs_path_read_lock(s
);
150 v9fs_co_run_in_worker(
152 err
= s
->ops
->chown(&s
->ctx
, path
, &cred
);
161 int v9fs_co_truncate(V9fsPDU
*pdu
, V9fsPath
*path
, off_t size
)
164 V9fsState
*s
= pdu
->s
;
166 if (v9fs_request_cancelled(pdu
)) {
169 v9fs_path_read_lock(s
);
170 v9fs_co_run_in_worker(
172 err
= s
->ops
->truncate(&s
->ctx
, path
, size
);
181 int v9fs_co_mknod(V9fsPDU
*pdu
, V9fsFidState
*fidp
, V9fsString
*name
, uid_t uid
,
182 gid_t gid
, dev_t dev
, mode_t mode
, struct stat
*stbuf
)
187 V9fsState
*s
= pdu
->s
;
189 if (v9fs_request_cancelled(pdu
)) {
197 v9fs_path_read_lock(s
);
198 v9fs_co_run_in_worker(
200 err
= s
->ops
->mknod(&s
->ctx
, &fidp
->path
, name
->data
, &cred
);
204 v9fs_path_init(&path
);
205 err
= v9fs_name_to_path(s
, &fidp
->path
, name
->data
, &path
);
207 err
= s
->ops
->lstat(&s
->ctx
, &path
, stbuf
);
212 v9fs_path_free(&path
);
219 /* Only works with path name based fid */
220 int v9fs_co_remove(V9fsPDU
*pdu
, V9fsPath
*path
)
223 V9fsState
*s
= pdu
->s
;
225 if (v9fs_request_cancelled(pdu
)) {
228 v9fs_path_read_lock(s
);
229 v9fs_co_run_in_worker(
231 err
= s
->ops
->remove(&s
->ctx
, path
->data
);
240 int v9fs_co_unlinkat(V9fsPDU
*pdu
, V9fsPath
*path
, V9fsString
*name
, int flags
)
243 V9fsState
*s
= pdu
->s
;
245 if (v9fs_request_cancelled(pdu
)) {
248 v9fs_path_read_lock(s
);
249 v9fs_co_run_in_worker(
251 err
= s
->ops
->unlinkat(&s
->ctx
, path
, name
->data
, flags
);
260 /* Only work with path name based fid */
261 int v9fs_co_rename(V9fsPDU
*pdu
, V9fsPath
*oldpath
, V9fsPath
*newpath
)
264 V9fsState
*s
= pdu
->s
;
266 if (v9fs_request_cancelled(pdu
)) {
269 v9fs_co_run_in_worker(
271 err
= s
->ops
->rename(&s
->ctx
, oldpath
->data
, newpath
->data
);
279 int v9fs_co_renameat(V9fsPDU
*pdu
, V9fsPath
*olddirpath
, V9fsString
*oldname
,
280 V9fsPath
*newdirpath
, V9fsString
*newname
)
283 V9fsState
*s
= pdu
->s
;
285 if (v9fs_request_cancelled(pdu
)) {
288 v9fs_co_run_in_worker(
290 err
= s
->ops
->renameat(&s
->ctx
, olddirpath
, oldname
->data
,
291 newdirpath
, newname
->data
);
299 int v9fs_co_symlink(V9fsPDU
*pdu
, V9fsFidState
*dfidp
, V9fsString
*name
,
300 const char *oldpath
, gid_t gid
, struct stat
*stbuf
)
305 V9fsState
*s
= pdu
->s
;
307 if (v9fs_request_cancelled(pdu
)) {
311 cred
.fc_uid
= dfidp
->uid
;
314 v9fs_path_read_lock(s
);
315 v9fs_co_run_in_worker(
317 err
= s
->ops
->symlink(&s
->ctx
, oldpath
, &dfidp
->path
,
322 v9fs_path_init(&path
);
323 err
= v9fs_name_to_path(s
, &dfidp
->path
, name
->data
, &path
);
325 err
= s
->ops
->lstat(&s
->ctx
, &path
, stbuf
);
330 v9fs_path_free(&path
);
338 * For path name based fid we don't block. So we can
339 * directly call the fs driver ops.
341 int v9fs_co_name_to_path(V9fsPDU
*pdu
, V9fsPath
*dirpath
,
342 const char *name
, V9fsPath
*path
)
345 V9fsState
*s
= pdu
->s
;
347 if (s
->ctx
.export_flags
& V9FS_PATHNAME_FSCONTEXT
) {
348 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);
353 if (v9fs_request_cancelled(pdu
)) {
356 v9fs_co_run_in_worker(
358 err
= s
->ops
->name_to_path(&s
->ctx
, dirpath
, name
, path
);