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