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