hw/9pfs: Use read-write lock for protecting fid path.
[qemu/kevin.git] / hw / 9pfs / codir.c
blobc9a88ecb60a2390cb6b5e3d7fb11d4d66fb05c0f
2 /*
3 * Virtio 9p backend
5 * Copyright IBM, Corp. 2011
7 * Authors:
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 "qemu-coroutine.h"
18 #include "virtio-9p-coth.h"
20 int v9fs_co_readdir_r(V9fsState *s, V9fsFidState *fidp, struct dirent *dent,
21 struct dirent **result)
23 int err;
25 v9fs_co_run_in_worker(
27 errno = 0;
28 err = s->ops->readdir_r(&s->ctx, fidp->fs.dir, dent, result);
29 if (!*result && errno) {
30 err = -errno;
31 } else {
32 err = 0;
34 });
35 return err;
38 off_t v9fs_co_telldir(V9fsState *s, V9fsFidState *fidp)
40 off_t err;
42 v9fs_co_run_in_worker(
44 err = s->ops->telldir(&s->ctx, fidp->fs.dir);
45 if (err < 0) {
46 err = -errno;
48 });
49 return err;
52 void v9fs_co_seekdir(V9fsState *s, V9fsFidState *fidp, off_t offset)
54 v9fs_co_run_in_worker(
56 s->ops->seekdir(&s->ctx, fidp->fs.dir, offset);
57 });
60 void v9fs_co_rewinddir(V9fsState *s, V9fsFidState *fidp)
62 v9fs_co_run_in_worker(
64 s->ops->rewinddir(&s->ctx, fidp->fs.dir);
65 });
68 int v9fs_co_mkdir(V9fsState *s, V9fsFidState *fidp, V9fsString *name,
69 mode_t mode, uid_t uid, gid_t gid, struct stat *stbuf)
71 int err;
72 FsCred cred;
73 V9fsString fullname;
75 cred_init(&cred);
76 cred.fc_mode = mode;
77 cred.fc_uid = uid;
78 cred.fc_gid = gid;
79 v9fs_string_init(&fullname);
80 qemu_co_rwlock_rdlock(&s->rename_lock);
81 v9fs_string_sprintf(&fullname, "%s/%s", fidp->path.data, name->data);
82 v9fs_co_run_in_worker(
84 err = s->ops->mkdir(&s->ctx, fullname.data, &cred);
85 if (err < 0) {
86 err = -errno;
87 } else {
88 err = s->ops->lstat(&s->ctx, fullname.data, stbuf);
89 if (err < 0) {
90 err = -errno;
93 });
94 qemu_co_rwlock_unlock(&s->rename_lock);
95 v9fs_string_free(&fullname);
96 return err;
99 int v9fs_co_opendir(V9fsState *s, V9fsFidState *fidp)
101 int err;
103 qemu_co_rwlock_rdlock(&s->rename_lock);
104 v9fs_co_run_in_worker(
106 fidp->fs.dir = s->ops->opendir(&s->ctx, fidp->path.data);
107 if (!fidp->fs.dir) {
108 err = -errno;
109 } else {
110 err = 0;
113 qemu_co_rwlock_unlock(&s->rename_lock);
114 if (!err) {
115 total_open_fd++;
116 if (total_open_fd > open_fd_hw) {
117 v9fs_reclaim_fd(s);
120 return err;
123 int v9fs_co_closedir(V9fsState *s, DIR *dir)
125 int err;
127 v9fs_co_run_in_worker(
129 err = s->ops->closedir(&s->ctx, dir);
130 if (err < 0) {
131 err = -errno;
134 if (!err) {
135 total_open_fd--;
137 return err;