]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Virtio 9p backend | |
3 | * | |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
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 | ||
14 | /* | |
15 | * Not so fast! You might want to read the 9p developer docs first: | |
16 | * https://wiki.qemu.org/Documentation/9p | |
17 | */ | |
18 | ||
19 | #include "qemu/osdep.h" | |
20 | #include <glib/gprintf.h> | |
21 | #include "hw/virtio/virtio.h" | |
22 | #include "qapi/error.h" | |
23 | #include "qemu/error-report.h" | |
24 | #include "qemu/iov.h" | |
25 | #include "qemu/main-loop.h" | |
26 | #include "qemu/sockets.h" | |
27 | #include "virtio-9p.h" | |
28 | #include "fsdev/qemu-fsdev.h" | |
29 | #include "9p-xattr.h" | |
30 | #include "coth.h" | |
31 | #include "trace.h" | |
32 | #include "migration/blocker.h" | |
33 | #include "qemu/xxhash.h" | |
34 | #include <math.h> | |
35 | #ifdef CONFIG_LINUX | |
36 | #include <linux/limits.h> | |
37 | #else | |
38 | #include <limits.h> | |
39 | #endif | |
40 | ||
41 | int open_fd_hw; | |
42 | int total_open_fd; | |
43 | static int open_fd_rc; | |
44 | ||
45 | enum { | |
46 | Oread = 0x00, | |
47 | Owrite = 0x01, | |
48 | Ordwr = 0x02, | |
49 | Oexec = 0x03, | |
50 | Oexcl = 0x04, | |
51 | Otrunc = 0x10, | |
52 | Orexec = 0x20, | |
53 | Orclose = 0x40, | |
54 | Oappend = 0x80, | |
55 | }; | |
56 | ||
57 | P9ARRAY_DEFINE_TYPE(V9fsPath, v9fs_path_free); | |
58 | ||
59 | static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) | |
60 | { | |
61 | ssize_t ret; | |
62 | va_list ap; | |
63 | ||
64 | va_start(ap, fmt); | |
65 | ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap); | |
66 | va_end(ap); | |
67 | ||
68 | return ret; | |
69 | } | |
70 | ||
71 | static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) | |
72 | { | |
73 | ssize_t ret; | |
74 | va_list ap; | |
75 | ||
76 | va_start(ap, fmt); | |
77 | ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap); | |
78 | va_end(ap); | |
79 | ||
80 | return ret; | |
81 | } | |
82 | ||
83 | static int omode_to_uflags(int8_t mode) | |
84 | { | |
85 | int ret = 0; | |
86 | ||
87 | switch (mode & 3) { | |
88 | case Oread: | |
89 | ret = O_RDONLY; | |
90 | break; | |
91 | case Ordwr: | |
92 | ret = O_RDWR; | |
93 | break; | |
94 | case Owrite: | |
95 | ret = O_WRONLY; | |
96 | break; | |
97 | case Oexec: | |
98 | ret = O_RDONLY; | |
99 | break; | |
100 | } | |
101 | ||
102 | if (mode & Otrunc) { | |
103 | ret |= O_TRUNC; | |
104 | } | |
105 | ||
106 | if (mode & Oappend) { | |
107 | ret |= O_APPEND; | |
108 | } | |
109 | ||
110 | if (mode & Oexcl) { | |
111 | ret |= O_EXCL; | |
112 | } | |
113 | ||
114 | return ret; | |
115 | } | |
116 | ||
117 | typedef struct DotlOpenflagMap { | |
118 | int dotl_flag; | |
119 | int open_flag; | |
120 | } DotlOpenflagMap; | |
121 | ||
122 | static int dotl_to_open_flags(int flags) | |
123 | { | |
124 | int i; | |
125 | /* | |
126 | * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY | |
127 | * and P9_DOTL_NOACCESS | |
128 | */ | |
129 | int oflags = flags & O_ACCMODE; | |
130 | ||
131 | DotlOpenflagMap dotl_oflag_map[] = { | |
132 | { P9_DOTL_CREATE, O_CREAT }, | |
133 | { P9_DOTL_EXCL, O_EXCL }, | |
134 | { P9_DOTL_NOCTTY , O_NOCTTY }, | |
135 | { P9_DOTL_TRUNC, O_TRUNC }, | |
136 | { P9_DOTL_APPEND, O_APPEND }, | |
137 | { P9_DOTL_NONBLOCK, O_NONBLOCK } , | |
138 | { P9_DOTL_DSYNC, O_DSYNC }, | |
139 | { P9_DOTL_FASYNC, FASYNC }, | |
140 | { P9_DOTL_DIRECT, O_DIRECT }, | |
141 | { P9_DOTL_LARGEFILE, O_LARGEFILE }, | |
142 | { P9_DOTL_DIRECTORY, O_DIRECTORY }, | |
143 | { P9_DOTL_NOFOLLOW, O_NOFOLLOW }, | |
144 | { P9_DOTL_NOATIME, O_NOATIME }, | |
145 | { P9_DOTL_SYNC, O_SYNC }, | |
146 | }; | |
147 | ||
148 | for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) { | |
149 | if (flags & dotl_oflag_map[i].dotl_flag) { | |
150 | oflags |= dotl_oflag_map[i].open_flag; | |
151 | } | |
152 | } | |
153 | ||
154 | return oflags; | |
155 | } | |
156 | ||
157 | void cred_init(FsCred *credp) | |
158 | { | |
159 | credp->fc_uid = -1; | |
160 | credp->fc_gid = -1; | |
161 | credp->fc_mode = -1; | |
162 | credp->fc_rdev = -1; | |
163 | } | |
164 | ||
165 | static int get_dotl_openflags(V9fsState *s, int oflags) | |
166 | { | |
167 | int flags; | |
168 | /* | |
169 | * Filter the client open flags | |
170 | */ | |
171 | flags = dotl_to_open_flags(oflags); | |
172 | flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT); | |
173 | /* | |
174 | * Ignore direct disk access hint until the server supports it. | |
175 | */ | |
176 | flags &= ~O_DIRECT; | |
177 | return flags; | |
178 | } | |
179 | ||
180 | void v9fs_path_init(V9fsPath *path) | |
181 | { | |
182 | path->data = NULL; | |
183 | path->size = 0; | |
184 | } | |
185 | ||
186 | void v9fs_path_free(V9fsPath *path) | |
187 | { | |
188 | g_free(path->data); | |
189 | path->data = NULL; | |
190 | path->size = 0; | |
191 | } | |
192 | ||
193 | ||
194 | void GCC_FMT_ATTR(2, 3) | |
195 | v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...) | |
196 | { | |
197 | va_list ap; | |
198 | ||
199 | v9fs_path_free(path); | |
200 | ||
201 | va_start(ap, fmt); | |
202 | /* Bump the size for including terminating NULL */ | |
203 | path->size = g_vasprintf(&path->data, fmt, ap) + 1; | |
204 | va_end(ap); | |
205 | } | |
206 | ||
207 | void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) | |
208 | { | |
209 | v9fs_path_free(dst); | |
210 | dst->size = src->size; | |
211 | dst->data = g_memdup(src->data, src->size); | |
212 | } | |
213 | ||
214 | int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, | |
215 | const char *name, V9fsPath *path) | |
216 | { | |
217 | int err; | |
218 | err = s->ops->name_to_path(&s->ctx, dirpath, name, path); | |
219 | if (err < 0) { | |
220 | err = -errno; | |
221 | } | |
222 | return err; | |
223 | } | |
224 | ||
225 | /* | |
226 | * Return TRUE if s1 is an ancestor of s2. | |
227 | * | |
228 | * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d". | |
229 | * As a special case, We treat s1 as ancestor of s2 if they are same! | |
230 | */ | |
231 | static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2) | |
232 | { | |
233 | if (!strncmp(s1->data, s2->data, s1->size - 1)) { | |
234 | if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') { | |
235 | return 1; | |
236 | } | |
237 | } | |
238 | return 0; | |
239 | } | |
240 | ||
241 | static size_t v9fs_string_size(V9fsString *str) | |
242 | { | |
243 | return str->size; | |
244 | } | |
245 | ||
246 | /* | |
247 | * returns 0 if fid got re-opened, 1 if not, < 0 on error */ | |
248 | static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f) | |
249 | { | |
250 | int err = 1; | |
251 | if (f->fid_type == P9_FID_FILE) { | |
252 | if (f->fs.fd == -1) { | |
253 | do { | |
254 | err = v9fs_co_open(pdu, f, f->open_flags); | |
255 | } while (err == -EINTR && !pdu->cancelled); | |
256 | } | |
257 | } else if (f->fid_type == P9_FID_DIR) { | |
258 | if (f->fs.dir.stream == NULL) { | |
259 | do { | |
260 | err = v9fs_co_opendir(pdu, f); | |
261 | } while (err == -EINTR && !pdu->cancelled); | |
262 | } | |
263 | } | |
264 | return err; | |
265 | } | |
266 | ||
267 | static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid) | |
268 | { | |
269 | int err; | |
270 | V9fsFidState *f; | |
271 | V9fsState *s = pdu->s; | |
272 | ||
273 | QSIMPLEQ_FOREACH(f, &s->fid_list, next) { | |
274 | BUG_ON(f->clunked); | |
275 | if (f->fid == fid) { | |
276 | /* | |
277 | * Update the fid ref upfront so that | |
278 | * we don't get reclaimed when we yield | |
279 | * in open later. | |
280 | */ | |
281 | f->ref++; | |
282 | /* | |
283 | * check whether we need to reopen the | |
284 | * file. We might have closed the fd | |
285 | * while trying to free up some file | |
286 | * descriptors. | |
287 | */ | |
288 | err = v9fs_reopen_fid(pdu, f); | |
289 | if (err < 0) { | |
290 | f->ref--; | |
291 | return NULL; | |
292 | } | |
293 | /* | |
294 | * Mark the fid as referenced so that the LRU | |
295 | * reclaim won't close the file descriptor | |
296 | */ | |
297 | f->flags |= FID_REFERENCED; | |
298 | return f; | |
299 | } | |
300 | } | |
301 | return NULL; | |
302 | } | |
303 | ||
304 | static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) | |
305 | { | |
306 | V9fsFidState *f; | |
307 | ||
308 | QSIMPLEQ_FOREACH(f, &s->fid_list, next) { | |
309 | /* If fid is already there return NULL */ | |
310 | BUG_ON(f->clunked); | |
311 | if (f->fid == fid) { | |
312 | return NULL; | |
313 | } | |
314 | } | |
315 | f = g_malloc0(sizeof(V9fsFidState)); | |
316 | f->fid = fid; | |
317 | f->fid_type = P9_FID_NONE; | |
318 | f->ref = 1; | |
319 | /* | |
320 | * Mark the fid as referenced so that the LRU | |
321 | * reclaim won't close the file descriptor | |
322 | */ | |
323 | f->flags |= FID_REFERENCED; | |
324 | QSIMPLEQ_INSERT_TAIL(&s->fid_list, f, next); | |
325 | ||
326 | v9fs_readdir_init(s->proto_version, &f->fs.dir); | |
327 | v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir); | |
328 | ||
329 | return f; | |
330 | } | |
331 | ||
332 | static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp) | |
333 | { | |
334 | int retval = 0; | |
335 | ||
336 | if (fidp->fs.xattr.xattrwalk_fid) { | |
337 | /* getxattr/listxattr fid */ | |
338 | goto free_value; | |
339 | } | |
340 | /* | |
341 | * if this is fid for setxattr. clunk should | |
342 | * result in setxattr localcall | |
343 | */ | |
344 | if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) { | |
345 | /* clunk after partial write */ | |
346 | retval = -EINVAL; | |
347 | goto free_out; | |
348 | } | |
349 | if (fidp->fs.xattr.len) { | |
350 | retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name, | |
351 | fidp->fs.xattr.value, | |
352 | fidp->fs.xattr.len, | |
353 | fidp->fs.xattr.flags); | |
354 | } else { | |
355 | retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name); | |
356 | } | |
357 | free_out: | |
358 | v9fs_string_free(&fidp->fs.xattr.name); | |
359 | free_value: | |
360 | g_free(fidp->fs.xattr.value); | |
361 | return retval; | |
362 | } | |
363 | ||
364 | static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp) | |
365 | { | |
366 | int retval = 0; | |
367 | ||
368 | if (fidp->fid_type == P9_FID_FILE) { | |
369 | /* If we reclaimed the fd no need to close */ | |
370 | if (fidp->fs.fd != -1) { | |
371 | retval = v9fs_co_close(pdu, &fidp->fs); | |
372 | } | |
373 | } else if (fidp->fid_type == P9_FID_DIR) { | |
374 | if (fidp->fs.dir.stream != NULL) { | |
375 | retval = v9fs_co_closedir(pdu, &fidp->fs); | |
376 | } | |
377 | } else if (fidp->fid_type == P9_FID_XATTR) { | |
378 | retval = v9fs_xattr_fid_clunk(pdu, fidp); | |
379 | } | |
380 | v9fs_path_free(&fidp->path); | |
381 | g_free(fidp); | |
382 | return retval; | |
383 | } | |
384 | ||
385 | static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp) | |
386 | { | |
387 | BUG_ON(!fidp->ref); | |
388 | fidp->ref--; | |
389 | /* | |
390 | * Don't free the fid if it is in reclaim list | |
391 | */ | |
392 | if (!fidp->ref && fidp->clunked) { | |
393 | if (fidp->fid == pdu->s->root_fid) { | |
394 | /* | |
395 | * if the clunked fid is root fid then we | |
396 | * have unmounted the fs on the client side. | |
397 | * delete the migration blocker. Ideally, this | |
398 | * should be hooked to transport close notification | |
399 | */ | |
400 | if (pdu->s->migration_blocker) { | |
401 | migrate_del_blocker(pdu->s->migration_blocker); | |
402 | error_free(pdu->s->migration_blocker); | |
403 | pdu->s->migration_blocker = NULL; | |
404 | } | |
405 | } | |
406 | return free_fid(pdu, fidp); | |
407 | } | |
408 | return 0; | |
409 | } | |
410 | ||
411 | static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid) | |
412 | { | |
413 | V9fsFidState *fidp; | |
414 | ||
415 | QSIMPLEQ_FOREACH(fidp, &s->fid_list, next) { | |
416 | if (fidp->fid == fid) { | |
417 | QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); | |
418 | fidp->clunked = true; | |
419 | return fidp; | |
420 | } | |
421 | } | |
422 | return NULL; | |
423 | } | |
424 | ||
425 | void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) | |
426 | { | |
427 | int reclaim_count = 0; | |
428 | V9fsState *s = pdu->s; | |
429 | V9fsFidState *f; | |
430 | QSLIST_HEAD(, V9fsFidState) reclaim_list = | |
431 | QSLIST_HEAD_INITIALIZER(reclaim_list); | |
432 | ||
433 | QSIMPLEQ_FOREACH(f, &s->fid_list, next) { | |
434 | /* | |
435 | * Unlink fids cannot be reclaimed. Check | |
436 | * for them and skip them. Also skip fids | |
437 | * currently being operated on. | |
438 | */ | |
439 | if (f->ref || f->flags & FID_NON_RECLAIMABLE) { | |
440 | continue; | |
441 | } | |
442 | /* | |
443 | * if it is a recently referenced fid | |
444 | * we leave the fid untouched and clear the | |
445 | * reference bit. We come back to it later | |
446 | * in the next iteration. (a simple LRU without | |
447 | * moving list elements around) | |
448 | */ | |
449 | if (f->flags & FID_REFERENCED) { | |
450 | f->flags &= ~FID_REFERENCED; | |
451 | continue; | |
452 | } | |
453 | /* | |
454 | * Add fids to reclaim list. | |
455 | */ | |
456 | if (f->fid_type == P9_FID_FILE) { | |
457 | if (f->fs.fd != -1) { | |
458 | /* | |
459 | * Up the reference count so that | |
460 | * a clunk request won't free this fid | |
461 | */ | |
462 | f->ref++; | |
463 | QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); | |
464 | f->fs_reclaim.fd = f->fs.fd; | |
465 | f->fs.fd = -1; | |
466 | reclaim_count++; | |
467 | } | |
468 | } else if (f->fid_type == P9_FID_DIR) { | |
469 | if (f->fs.dir.stream != NULL) { | |
470 | /* | |
471 | * Up the reference count so that | |
472 | * a clunk request won't free this fid | |
473 | */ | |
474 | f->ref++; | |
475 | QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); | |
476 | f->fs_reclaim.dir.stream = f->fs.dir.stream; | |
477 | f->fs.dir.stream = NULL; | |
478 | reclaim_count++; | |
479 | } | |
480 | } | |
481 | if (reclaim_count >= open_fd_rc) { | |
482 | break; | |
483 | } | |
484 | } | |
485 | /* | |
486 | * Now close the fid in reclaim list. Free them if they | |
487 | * are already clunked. | |
488 | */ | |
489 | while (!QSLIST_EMPTY(&reclaim_list)) { | |
490 | f = QSLIST_FIRST(&reclaim_list); | |
491 | QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next); | |
492 | if (f->fid_type == P9_FID_FILE) { | |
493 | v9fs_co_close(pdu, &f->fs_reclaim); | |
494 | } else if (f->fid_type == P9_FID_DIR) { | |
495 | v9fs_co_closedir(pdu, &f->fs_reclaim); | |
496 | } | |
497 | /* | |
498 | * Now drop the fid reference, free it | |
499 | * if clunked. | |
500 | */ | |
501 | put_fid(pdu, f); | |
502 | } | |
503 | } | |
504 | ||
505 | static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path) | |
506 | { | |
507 | int err; | |
508 | V9fsState *s = pdu->s; | |
509 | V9fsFidState *fidp, *fidp_next; | |
510 | ||
511 | fidp = QSIMPLEQ_FIRST(&s->fid_list); | |
512 | if (!fidp) { | |
513 | return 0; | |
514 | } | |
515 | ||
516 | /* | |
517 | * v9fs_reopen_fid() can yield : a reference on the fid must be held | |
518 | * to ensure its pointer remains valid and we can safely pass it to | |
519 | * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so | |
520 | * we must keep a reference on the next fid as well. So the logic here | |
521 | * is to get a reference on a fid and only put it back during the next | |
522 | * iteration after we could get a reference on the next fid. Start with | |
523 | * the first one. | |
524 | */ | |
525 | for (fidp->ref++; fidp; fidp = fidp_next) { | |
526 | if (fidp->path.size == path->size && | |
527 | !memcmp(fidp->path.data, path->data, path->size)) { | |
528 | /* Mark the fid non reclaimable. */ | |
529 | fidp->flags |= FID_NON_RECLAIMABLE; | |
530 | ||
531 | /* reopen the file/dir if already closed */ | |
532 | err = v9fs_reopen_fid(pdu, fidp); | |
533 | if (err < 0) { | |
534 | put_fid(pdu, fidp); | |
535 | return err; | |
536 | } | |
537 | } | |
538 | ||
539 | fidp_next = QSIMPLEQ_NEXT(fidp, next); | |
540 | ||
541 | if (fidp_next) { | |
542 | /* | |
543 | * Ensure the next fid survives a potential clunk request during | |
544 | * put_fid() below and v9fs_reopen_fid() in the next iteration. | |
545 | */ | |
546 | fidp_next->ref++; | |
547 | } | |
548 | ||
549 | /* We're done with this fid */ | |
550 | put_fid(pdu, fidp); | |
551 | } | |
552 | ||
553 | return 0; | |
554 | } | |
555 | ||
556 | static void coroutine_fn virtfs_reset(V9fsPDU *pdu) | |
557 | { | |
558 | V9fsState *s = pdu->s; | |
559 | V9fsFidState *fidp; | |
560 | ||
561 | /* Free all fids */ | |
562 | while (!QSIMPLEQ_EMPTY(&s->fid_list)) { | |
563 | /* Get fid */ | |
564 | fidp = QSIMPLEQ_FIRST(&s->fid_list); | |
565 | fidp->ref++; | |
566 | ||
567 | /* Clunk fid */ | |
568 | QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); | |
569 | fidp->clunked = true; | |
570 | ||
571 | put_fid(pdu, fidp); | |
572 | } | |
573 | } | |
574 | ||
575 | #define P9_QID_TYPE_DIR 0x80 | |
576 | #define P9_QID_TYPE_SYMLINK 0x02 | |
577 | ||
578 | #define P9_STAT_MODE_DIR 0x80000000 | |
579 | #define P9_STAT_MODE_APPEND 0x40000000 | |
580 | #define P9_STAT_MODE_EXCL 0x20000000 | |
581 | #define P9_STAT_MODE_MOUNT 0x10000000 | |
582 | #define P9_STAT_MODE_AUTH 0x08000000 | |
583 | #define P9_STAT_MODE_TMP 0x04000000 | |
584 | #define P9_STAT_MODE_SYMLINK 0x02000000 | |
585 | #define P9_STAT_MODE_LINK 0x01000000 | |
586 | #define P9_STAT_MODE_DEVICE 0x00800000 | |
587 | #define P9_STAT_MODE_NAMED_PIPE 0x00200000 | |
588 | #define P9_STAT_MODE_SOCKET 0x00100000 | |
589 | #define P9_STAT_MODE_SETUID 0x00080000 | |
590 | #define P9_STAT_MODE_SETGID 0x00040000 | |
591 | #define P9_STAT_MODE_SETVTX 0x00010000 | |
592 | ||
593 | #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ | |
594 | P9_STAT_MODE_SYMLINK | \ | |
595 | P9_STAT_MODE_LINK | \ | |
596 | P9_STAT_MODE_DEVICE | \ | |
597 | P9_STAT_MODE_NAMED_PIPE | \ | |
598 | P9_STAT_MODE_SOCKET) | |
599 | ||
600 | /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */ | |
601 | static inline uint8_t mirror8bit(uint8_t byte) | |
602 | { | |
603 | return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023; | |
604 | } | |
605 | ||
606 | /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */ | |
607 | static inline uint64_t mirror64bit(uint64_t value) | |
608 | { | |
609 | return ((uint64_t)mirror8bit(value & 0xff) << 56) | | |
610 | ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) | | |
611 | ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) | | |
612 | ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) | | |
613 | ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) | | |
614 | ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) | | |
615 | ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) | | |
616 | ((uint64_t)mirror8bit((value >> 56) & 0xff)); | |
617 | } | |
618 | ||
619 | /** | |
620 | * @brief Parameter k for the Exponential Golomb algorihm to be used. | |
621 | * | |
622 | * The smaller this value, the smaller the minimum bit count for the Exp. | |
623 | * Golomb generated affixes will be (at lowest index) however for the | |
624 | * price of having higher maximum bit count of generated affixes (at highest | |
625 | * index). Likewise increasing this parameter yields in smaller maximum bit | |
626 | * count for the price of having higher minimum bit count. | |
627 | * | |
628 | * In practice that means: a good value for k depends on the expected amount | |
629 | * of devices to be exposed by one export. For a small amount of devices k | |
630 | * should be small, for a large amount of devices k might be increased | |
631 | * instead. The default of k=0 should be fine for most users though. | |
632 | * | |
633 | * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of | |
634 | * k should not change as long as guest is still running! Because that would | |
635 | * cause completely different inode numbers to be generated on guest. | |
636 | */ | |
637 | #define EXP_GOLOMB_K 0 | |
638 | ||
639 | /** | |
640 | * @brief Exponential Golomb algorithm for arbitrary k (including k=0). | |
641 | * | |
642 | * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!) | |
643 | * with growing length and with the mathematical property of being | |
644 | * "prefix-free". The latter means the generated prefixes can be prepended | |
645 | * in front of arbitrary numbers and the resulting concatenated numbers are | |
646 | * guaranteed to be always unique. | |
647 | * | |
648 | * This is a minor adjustment to the original Exp. Golomb algorithm in the | |
649 | * sense that lowest allowed index (@param n) starts with 1, not with zero. | |
650 | * | |
651 | * @param n - natural number (or index) of the prefix to be generated | |
652 | * (1, 2, 3, ...) | |
653 | * @param k - parameter k of Exp. Golomb algorithm to be used | |
654 | * (see comment on EXP_GOLOMB_K macro for details about k) | |
655 | */ | |
656 | static VariLenAffix expGolombEncode(uint64_t n, int k) | |
657 | { | |
658 | const uint64_t value = n + (1 << k) - 1; | |
659 | const int bits = (int) log2(value) + 1; | |
660 | return (VariLenAffix) { | |
661 | .type = AffixType_Prefix, | |
662 | .value = value, | |
663 | .bits = bits + MAX((bits - 1 - k), 0) | |
664 | }; | |
665 | } | |
666 | ||
667 | /** | |
668 | * @brief Converts a suffix into a prefix, or a prefix into a suffix. | |
669 | * | |
670 | * Simply mirror all bits of the affix value, for the purpose to preserve | |
671 | * respectively the mathematical "prefix-free" or "suffix-free" property | |
672 | * after the conversion. | |
673 | * | |
674 | * If a passed prefix is suitable to create unique numbers, then the | |
675 | * returned suffix is suitable to create unique numbers as well (and vice | |
676 | * versa). | |
677 | */ | |
678 | static VariLenAffix invertAffix(const VariLenAffix *affix) | |
679 | { | |
680 | return (VariLenAffix) { | |
681 | .type = | |
682 | (affix->type == AffixType_Suffix) ? | |
683 | AffixType_Prefix : AffixType_Suffix, | |
684 | .value = | |
685 | mirror64bit(affix->value) >> | |
686 | ((sizeof(affix->value) * 8) - affix->bits), | |
687 | .bits = affix->bits | |
688 | }; | |
689 | } | |
690 | ||
691 | /** | |
692 | * @brief Generates suffix numbers with "suffix-free" property. | |
693 | * | |
694 | * This is just a wrapper function on top of the Exp. Golomb algorithm. | |
695 | * | |
696 | * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes, | |
697 | * this function converts the Exp. Golomb prefixes into appropriate suffixes | |
698 | * which are still suitable for generating unique numbers. | |
699 | * | |
700 | * @param n - natural number (or index) of the suffix to be generated | |
701 | * (1, 2, 3, ...) | |
702 | */ | |
703 | static VariLenAffix affixForIndex(uint64_t index) | |
704 | { | |
705 | VariLenAffix prefix; | |
706 | prefix = expGolombEncode(index, EXP_GOLOMB_K); | |
707 | return invertAffix(&prefix); /* convert prefix to suffix */ | |
708 | } | |
709 | ||
710 | /* creative abuse of tb_hash_func7, which is based on xxhash */ | |
711 | static uint32_t qpp_hash(QppEntry e) | |
712 | { | |
713 | return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0); | |
714 | } | |
715 | ||
716 | static uint32_t qpf_hash(QpfEntry e) | |
717 | { | |
718 | return qemu_xxhash7(e.ino, e.dev, 0, 0, 0); | |
719 | } | |
720 | ||
721 | static bool qpd_cmp_func(const void *obj, const void *userp) | |
722 | { | |
723 | const QpdEntry *e1 = obj, *e2 = userp; | |
724 | return e1->dev == e2->dev; | |
725 | } | |
726 | ||
727 | static bool qpp_cmp_func(const void *obj, const void *userp) | |
728 | { | |
729 | const QppEntry *e1 = obj, *e2 = userp; | |
730 | return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix; | |
731 | } | |
732 | ||
733 | static bool qpf_cmp_func(const void *obj, const void *userp) | |
734 | { | |
735 | const QpfEntry *e1 = obj, *e2 = userp; | |
736 | return e1->dev == e2->dev && e1->ino == e2->ino; | |
737 | } | |
738 | ||
739 | static void qp_table_remove(void *p, uint32_t h, void *up) | |
740 | { | |
741 | g_free(p); | |
742 | } | |
743 | ||
744 | static void qp_table_destroy(struct qht *ht) | |
745 | { | |
746 | if (!ht || !ht->map) { | |
747 | return; | |
748 | } | |
749 | qht_iter(ht, qp_table_remove, NULL); | |
750 | qht_destroy(ht); | |
751 | } | |
752 | ||
753 | static void qpd_table_init(struct qht *ht) | |
754 | { | |
755 | qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE); | |
756 | } | |
757 | ||
758 | static void qpp_table_init(struct qht *ht) | |
759 | { | |
760 | qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE); | |
761 | } | |
762 | ||
763 | static void qpf_table_init(struct qht *ht) | |
764 | { | |
765 | qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE); | |
766 | } | |
767 | ||
768 | /* | |
769 | * Returns how many (high end) bits of inode numbers of the passed fs | |
770 | * device shall be used (in combination with the device number) to | |
771 | * generate hash values for qpp_table entries. | |
772 | * | |
773 | * This function is required if variable length suffixes are used for inode | |
774 | * number mapping on guest level. Since a device may end up having multiple | |
775 | * entries in qpp_table, each entry most probably with a different suffix | |
776 | * length, we thus need this function in conjunction with qpd_table to | |
777 | * "agree" about a fix amount of bits (per device) to be always used for | |
778 | * generating hash values for the purpose of accessing qpp_table in order | |
779 | * get consistent behaviour when accessing qpp_table. | |
780 | */ | |
781 | static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev) | |
782 | { | |
783 | QpdEntry lookup = { | |
784 | .dev = dev | |
785 | }, *val; | |
786 | uint32_t hash = dev; | |
787 | VariLenAffix affix; | |
788 | ||
789 | val = qht_lookup(&pdu->s->qpd_table, &lookup, hash); | |
790 | if (!val) { | |
791 | val = g_malloc0(sizeof(QpdEntry)); | |
792 | *val = lookup; | |
793 | affix = affixForIndex(pdu->s->qp_affix_next); | |
794 | val->prefix_bits = affix.bits; | |
795 | qht_insert(&pdu->s->qpd_table, val, hash, NULL); | |
796 | pdu->s->qp_ndevices++; | |
797 | } | |
798 | return val->prefix_bits; | |
799 | } | |
800 | ||
801 | /** | |
802 | * @brief Slow / full mapping host inode nr -> guest inode nr. | |
803 | * | |
804 | * This function performs a slower and much more costly remapping of an | |
805 | * original file inode number on host to an appropriate different inode | |
806 | * number on guest. For every (dev, inode) combination on host a new | |
807 | * sequential number is generated, cached and exposed as inode number on | |
808 | * guest. | |
809 | * | |
810 | * This is just a "last resort" fallback solution if the much faster/cheaper | |
811 | * qid_path_suffixmap() failed. In practice this slow / full mapping is not | |
812 | * expected ever to be used at all though. | |
813 | * | |
814 | * @see qid_path_suffixmap() for details | |
815 | * | |
816 | */ | |
817 | static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf, | |
818 | uint64_t *path) | |
819 | { | |
820 | QpfEntry lookup = { | |
821 | .dev = stbuf->st_dev, | |
822 | .ino = stbuf->st_ino | |
823 | }, *val; | |
824 | uint32_t hash = qpf_hash(lookup); | |
825 | VariLenAffix affix; | |
826 | ||
827 | val = qht_lookup(&pdu->s->qpf_table, &lookup, hash); | |
828 | ||
829 | if (!val) { | |
830 | if (pdu->s->qp_fullpath_next == 0) { | |
831 | /* no more files can be mapped :'( */ | |
832 | error_report_once( | |
833 | "9p: No more prefixes available for remapping inodes from " | |
834 | "host to guest." | |
835 | ); | |
836 | return -ENFILE; | |
837 | } | |
838 | ||
839 | val = g_malloc0(sizeof(QppEntry)); | |
840 | *val = lookup; | |
841 | ||
842 | /* new unique inode and device combo */ | |
843 | affix = affixForIndex( | |
844 | 1ULL << (sizeof(pdu->s->qp_affix_next) * 8) | |
845 | ); | |
846 | val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value; | |
847 | pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1); | |
848 | qht_insert(&pdu->s->qpf_table, val, hash, NULL); | |
849 | } | |
850 | ||
851 | *path = val->path; | |
852 | return 0; | |
853 | } | |
854 | ||
855 | /** | |
856 | * @brief Quick mapping host inode nr -> guest inode nr. | |
857 | * | |
858 | * This function performs quick remapping of an original file inode number | |
859 | * on host to an appropriate different inode number on guest. This remapping | |
860 | * of inodes is required to avoid inode nr collisions on guest which would | |
861 | * happen if the 9p export contains more than 1 exported file system (or | |
862 | * more than 1 file system data set), because unlike on host level where the | |
863 | * files would have different device nrs, all files exported by 9p would | |
864 | * share the same device nr on guest (the device nr of the virtual 9p device | |
865 | * that is). | |
866 | * | |
867 | * Inode remapping is performed by chopping off high end bits of the original | |
868 | * inode number from host, shifting the result upwards and then assigning a | |
869 | * generated suffix number for the low end bits, where the same suffix number | |
870 | * will be shared by all inodes with the same device id AND the same high end | |
871 | * bits that have been chopped off. That approach utilizes the fact that inode | |
872 | * numbers very likely share the same high end bits (i.e. due to their common | |
873 | * sequential generation by file systems) and hence we only have to generate | |
874 | * and track a very limited amount of suffixes in practice due to that. | |
875 | * | |
876 | * We generate variable size suffixes for that purpose. The 1st generated | |
877 | * suffix will only have 1 bit and hence we only need to chop off 1 bit from | |
878 | * the original inode number. The subsequent suffixes being generated will | |
879 | * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being | |
880 | * generated will have 3 bits and hence we have to chop off 3 bits from their | |
881 | * original inodes, and so on. That approach of using variable length suffixes | |
882 | * (i.e. over fixed size ones) utilizes the fact that in practice only a very | |
883 | * limited amount of devices are shared by the same export (e.g. typically | |
884 | * less than 2 dozen devices per 9p export), so in practice we need to chop | |
885 | * off less bits than with fixed size prefixes and yet are flexible to add | |
886 | * new devices at runtime below host's export directory at any time without | |
887 | * having to reboot guest nor requiring to reconfigure guest for that. And due | |
888 | * to the very limited amount of original high end bits that we chop off that | |
889 | * way, the total amount of suffixes we need to generate is less than by using | |
890 | * fixed size prefixes and hence it also improves performance of the inode | |
891 | * remapping algorithm, and finally has the nice side effect that the inode | |
892 | * numbers on guest will be much smaller & human friendly. ;-) | |
893 | */ | |
894 | static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf, | |
895 | uint64_t *path) | |
896 | { | |
897 | const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev); | |
898 | QppEntry lookup = { | |
899 | .dev = stbuf->st_dev, | |
900 | .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits)) | |
901 | }, *val; | |
902 | uint32_t hash = qpp_hash(lookup); | |
903 | ||
904 | val = qht_lookup(&pdu->s->qpp_table, &lookup, hash); | |
905 | ||
906 | if (!val) { | |
907 | if (pdu->s->qp_affix_next == 0) { | |
908 | /* we ran out of affixes */ | |
909 | warn_report_once( | |
910 | "9p: Potential degraded performance of inode remapping" | |
911 | ); | |
912 | return -ENFILE; | |
913 | } | |
914 | ||
915 | val = g_malloc0(sizeof(QppEntry)); | |
916 | *val = lookup; | |
917 | ||
918 | /* new unique inode affix and device combo */ | |
919 | val->qp_affix_index = pdu->s->qp_affix_next++; | |
920 | val->qp_affix = affixForIndex(val->qp_affix_index); | |
921 | qht_insert(&pdu->s->qpp_table, val, hash, NULL); | |
922 | } | |
923 | /* assuming generated affix to be suffix type, not prefix */ | |
924 | *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value; | |
925 | return 0; | |
926 | } | |
927 | ||
928 | static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp) | |
929 | { | |
930 | int err; | |
931 | size_t size; | |
932 | ||
933 | if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { | |
934 | /* map inode+device to qid path (fast path) */ | |
935 | err = qid_path_suffixmap(pdu, stbuf, &qidp->path); | |
936 | if (err == -ENFILE) { | |
937 | /* fast path didn't work, fall back to full map */ | |
938 | err = qid_path_fullmap(pdu, stbuf, &qidp->path); | |
939 | } | |
940 | if (err) { | |
941 | return err; | |
942 | } | |
943 | } else { | |
944 | if (pdu->s->dev_id != stbuf->st_dev) { | |
945 | if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) { | |
946 | error_report_once( | |
947 | "9p: Multiple devices detected in same VirtFS export. " | |
948 | "Access of guest to additional devices is (partly) " | |
949 | "denied due to virtfs option 'multidevs=forbid' being " | |
950 | "effective." | |
951 | ); | |
952 | return -ENODEV; | |
953 | } else { | |
954 | warn_report_once( | |
955 | "9p: Multiple devices detected in same VirtFS export, " | |
956 | "which might lead to file ID collisions and severe " | |
957 | "misbehaviours on guest! You should either use a " | |
958 | "separate export for each device shared from host or " | |
959 | "use virtfs option 'multidevs=remap'!" | |
960 | ); | |
961 | } | |
962 | } | |
963 | memset(&qidp->path, 0, sizeof(qidp->path)); | |
964 | size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); | |
965 | memcpy(&qidp->path, &stbuf->st_ino, size); | |
966 | } | |
967 | ||
968 | qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); | |
969 | qidp->type = 0; | |
970 | if (S_ISDIR(stbuf->st_mode)) { | |
971 | qidp->type |= P9_QID_TYPE_DIR; | |
972 | } | |
973 | if (S_ISLNK(stbuf->st_mode)) { | |
974 | qidp->type |= P9_QID_TYPE_SYMLINK; | |
975 | } | |
976 | ||
977 | return 0; | |
978 | } | |
979 | ||
980 | V9fsPDU *pdu_alloc(V9fsState *s) | |
981 | { | |
982 | V9fsPDU *pdu = NULL; | |
983 | ||
984 | if (!QLIST_EMPTY(&s->free_list)) { | |
985 | pdu = QLIST_FIRST(&s->free_list); | |
986 | QLIST_REMOVE(pdu, next); | |
987 | QLIST_INSERT_HEAD(&s->active_list, pdu, next); | |
988 | } | |
989 | return pdu; | |
990 | } | |
991 | ||
992 | void pdu_free(V9fsPDU *pdu) | |
993 | { | |
994 | V9fsState *s = pdu->s; | |
995 | ||
996 | g_assert(!pdu->cancelled); | |
997 | QLIST_REMOVE(pdu, next); | |
998 | QLIST_INSERT_HEAD(&s->free_list, pdu, next); | |
999 | } | |
1000 | ||
1001 | static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len) | |
1002 | { | |
1003 | int8_t id = pdu->id + 1; /* Response */ | |
1004 | V9fsState *s = pdu->s; | |
1005 | int ret; | |
1006 | ||
1007 | /* | |
1008 | * The 9p spec requires that successfully cancelled pdus receive no reply. | |
1009 | * Sending a reply would confuse clients because they would | |
1010 | * assume that any EINTR is the actual result of the operation, | |
1011 | * rather than a consequence of the cancellation. However, if | |
1012 | * the operation completed (succesfully or with an error other | |
1013 | * than caused be cancellation), we do send out that reply, both | |
1014 | * for efficiency and to avoid confusing the rest of the state machine | |
1015 | * that assumes passing a non-error here will mean a successful | |
1016 | * transmission of the reply. | |
1017 | */ | |
1018 | bool discard = pdu->cancelled && len == -EINTR; | |
1019 | if (discard) { | |
1020 | trace_v9fs_rcancel(pdu->tag, pdu->id); | |
1021 | pdu->size = 0; | |
1022 | goto out_notify; | |
1023 | } | |
1024 | ||
1025 | if (len < 0) { | |
1026 | int err = -len; | |
1027 | len = 7; | |
1028 | ||
1029 | if (s->proto_version != V9FS_PROTO_2000L) { | |
1030 | V9fsString str; | |
1031 | ||
1032 | str.data = strerror(err); | |
1033 | str.size = strlen(str.data); | |
1034 | ||
1035 | ret = pdu_marshal(pdu, len, "s", &str); | |
1036 | if (ret < 0) { | |
1037 | goto out_notify; | |
1038 | } | |
1039 | len += ret; | |
1040 | id = P9_RERROR; | |
1041 | } | |
1042 | ||
1043 | ret = pdu_marshal(pdu, len, "d", err); | |
1044 | if (ret < 0) { | |
1045 | goto out_notify; | |
1046 | } | |
1047 | len += ret; | |
1048 | ||
1049 | if (s->proto_version == V9FS_PROTO_2000L) { | |
1050 | id = P9_RLERROR; | |
1051 | } | |
1052 | trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */ | |
1053 | } | |
1054 | ||
1055 | /* fill out the header */ | |
1056 | if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) { | |
1057 | goto out_notify; | |
1058 | } | |
1059 | ||
1060 | /* keep these in sync */ | |
1061 | pdu->size = len; | |
1062 | pdu->id = id; | |
1063 | ||
1064 | out_notify: | |
1065 | pdu->s->transport->push_and_notify(pdu); | |
1066 | ||
1067 | /* Now wakeup anybody waiting in flush for this request */ | |
1068 | if (!qemu_co_queue_next(&pdu->complete)) { | |
1069 | pdu_free(pdu); | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension) | |
1074 | { | |
1075 | mode_t ret; | |
1076 | ||
1077 | ret = mode & 0777; | |
1078 | if (mode & P9_STAT_MODE_DIR) { | |
1079 | ret |= S_IFDIR; | |
1080 | } | |
1081 | ||
1082 | if (mode & P9_STAT_MODE_SYMLINK) { | |
1083 | ret |= S_IFLNK; | |
1084 | } | |
1085 | if (mode & P9_STAT_MODE_SOCKET) { | |
1086 | ret |= S_IFSOCK; | |
1087 | } | |
1088 | if (mode & P9_STAT_MODE_NAMED_PIPE) { | |
1089 | ret |= S_IFIFO; | |
1090 | } | |
1091 | if (mode & P9_STAT_MODE_DEVICE) { | |
1092 | if (extension->size && extension->data[0] == 'c') { | |
1093 | ret |= S_IFCHR; | |
1094 | } else { | |
1095 | ret |= S_IFBLK; | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | if (!(ret & ~0777)) { | |
1100 | ret |= S_IFREG; | |
1101 | } | |
1102 | ||
1103 | if (mode & P9_STAT_MODE_SETUID) { | |
1104 | ret |= S_ISUID; | |
1105 | } | |
1106 | if (mode & P9_STAT_MODE_SETGID) { | |
1107 | ret |= S_ISGID; | |
1108 | } | |
1109 | if (mode & P9_STAT_MODE_SETVTX) { | |
1110 | ret |= S_ISVTX; | |
1111 | } | |
1112 | ||
1113 | return ret; | |
1114 | } | |
1115 | ||
1116 | static int donttouch_stat(V9fsStat *stat) | |
1117 | { | |
1118 | if (stat->type == -1 && | |
1119 | stat->dev == -1 && | |
1120 | stat->qid.type == 0xff && | |
1121 | stat->qid.version == (uint32_t) -1 && | |
1122 | stat->qid.path == (uint64_t) -1 && | |
1123 | stat->mode == -1 && | |
1124 | stat->atime == -1 && | |
1125 | stat->mtime == -1 && | |
1126 | stat->length == -1 && | |
1127 | !stat->name.size && | |
1128 | !stat->uid.size && | |
1129 | !stat->gid.size && | |
1130 | !stat->muid.size && | |
1131 | stat->n_uid == -1 && | |
1132 | stat->n_gid == -1 && | |
1133 | stat->n_muid == -1) { | |
1134 | return 1; | |
1135 | } | |
1136 | ||
1137 | return 0; | |
1138 | } | |
1139 | ||
1140 | static void v9fs_stat_init(V9fsStat *stat) | |
1141 | { | |
1142 | v9fs_string_init(&stat->name); | |
1143 | v9fs_string_init(&stat->uid); | |
1144 | v9fs_string_init(&stat->gid); | |
1145 | v9fs_string_init(&stat->muid); | |
1146 | v9fs_string_init(&stat->extension); | |
1147 | } | |
1148 | ||
1149 | static void v9fs_stat_free(V9fsStat *stat) | |
1150 | { | |
1151 | v9fs_string_free(&stat->name); | |
1152 | v9fs_string_free(&stat->uid); | |
1153 | v9fs_string_free(&stat->gid); | |
1154 | v9fs_string_free(&stat->muid); | |
1155 | v9fs_string_free(&stat->extension); | |
1156 | } | |
1157 | ||
1158 | static uint32_t stat_to_v9mode(const struct stat *stbuf) | |
1159 | { | |
1160 | uint32_t mode; | |
1161 | ||
1162 | mode = stbuf->st_mode & 0777; | |
1163 | if (S_ISDIR(stbuf->st_mode)) { | |
1164 | mode |= P9_STAT_MODE_DIR; | |
1165 | } | |
1166 | ||
1167 | if (S_ISLNK(stbuf->st_mode)) { | |
1168 | mode |= P9_STAT_MODE_SYMLINK; | |
1169 | } | |
1170 | ||
1171 | if (S_ISSOCK(stbuf->st_mode)) { | |
1172 | mode |= P9_STAT_MODE_SOCKET; | |
1173 | } | |
1174 | ||
1175 | if (S_ISFIFO(stbuf->st_mode)) { | |
1176 | mode |= P9_STAT_MODE_NAMED_PIPE; | |
1177 | } | |
1178 | ||
1179 | if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) { | |
1180 | mode |= P9_STAT_MODE_DEVICE; | |
1181 | } | |
1182 | ||
1183 | if (stbuf->st_mode & S_ISUID) { | |
1184 | mode |= P9_STAT_MODE_SETUID; | |
1185 | } | |
1186 | ||
1187 | if (stbuf->st_mode & S_ISGID) { | |
1188 | mode |= P9_STAT_MODE_SETGID; | |
1189 | } | |
1190 | ||
1191 | if (stbuf->st_mode & S_ISVTX) { | |
1192 | mode |= P9_STAT_MODE_SETVTX; | |
1193 | } | |
1194 | ||
1195 | return mode; | |
1196 | } | |
1197 | ||
1198 | static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, | |
1199 | const char *basename, | |
1200 | const struct stat *stbuf, | |
1201 | V9fsStat *v9stat) | |
1202 | { | |
1203 | int err; | |
1204 | ||
1205 | memset(v9stat, 0, sizeof(*v9stat)); | |
1206 | ||
1207 | err = stat_to_qid(pdu, stbuf, &v9stat->qid); | |
1208 | if (err < 0) { | |
1209 | return err; | |
1210 | } | |
1211 | v9stat->mode = stat_to_v9mode(stbuf); | |
1212 | v9stat->atime = stbuf->st_atime; | |
1213 | v9stat->mtime = stbuf->st_mtime; | |
1214 | v9stat->length = stbuf->st_size; | |
1215 | ||
1216 | v9fs_string_free(&v9stat->uid); | |
1217 | v9fs_string_free(&v9stat->gid); | |
1218 | v9fs_string_free(&v9stat->muid); | |
1219 | ||
1220 | v9stat->n_uid = stbuf->st_uid; | |
1221 | v9stat->n_gid = stbuf->st_gid; | |
1222 | v9stat->n_muid = 0; | |
1223 | ||
1224 | v9fs_string_free(&v9stat->extension); | |
1225 | ||
1226 | if (v9stat->mode & P9_STAT_MODE_SYMLINK) { | |
1227 | err = v9fs_co_readlink(pdu, path, &v9stat->extension); | |
1228 | if (err < 0) { | |
1229 | return err; | |
1230 | } | |
1231 | } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { | |
1232 | v9fs_string_sprintf(&v9stat->extension, "%c %u %u", | |
1233 | S_ISCHR(stbuf->st_mode) ? 'c' : 'b', | |
1234 | major(stbuf->st_rdev), minor(stbuf->st_rdev)); | |
1235 | } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { | |
1236 | v9fs_string_sprintf(&v9stat->extension, "%s %lu", | |
1237 | "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink); | |
1238 | } | |
1239 | ||
1240 | v9fs_string_sprintf(&v9stat->name, "%s", basename); | |
1241 | ||
1242 | v9stat->size = 61 + | |
1243 | v9fs_string_size(&v9stat->name) + | |
1244 | v9fs_string_size(&v9stat->uid) + | |
1245 | v9fs_string_size(&v9stat->gid) + | |
1246 | v9fs_string_size(&v9stat->muid) + | |
1247 | v9fs_string_size(&v9stat->extension); | |
1248 | return 0; | |
1249 | } | |
1250 | ||
1251 | #define P9_STATS_MODE 0x00000001ULL | |
1252 | #define P9_STATS_NLINK 0x00000002ULL | |
1253 | #define P9_STATS_UID 0x00000004ULL | |
1254 | #define P9_STATS_GID 0x00000008ULL | |
1255 | #define P9_STATS_RDEV 0x00000010ULL | |
1256 | #define P9_STATS_ATIME 0x00000020ULL | |
1257 | #define P9_STATS_MTIME 0x00000040ULL | |
1258 | #define P9_STATS_CTIME 0x00000080ULL | |
1259 | #define P9_STATS_INO 0x00000100ULL | |
1260 | #define P9_STATS_SIZE 0x00000200ULL | |
1261 | #define P9_STATS_BLOCKS 0x00000400ULL | |
1262 | ||
1263 | #define P9_STATS_BTIME 0x00000800ULL | |
1264 | #define P9_STATS_GEN 0x00001000ULL | |
1265 | #define P9_STATS_DATA_VERSION 0x00002000ULL | |
1266 | ||
1267 | #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */ | |
1268 | #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */ | |
1269 | ||
1270 | ||
1271 | /** | |
1272 | * Convert host filesystem's block size into an appropriate block size for | |
1273 | * 9p client (guest OS side). The value returned suggests an "optimum" block | |
1274 | * size for 9p I/O, i.e. to maximize performance. | |
1275 | * | |
1276 | * @pdu: 9p client request | |
1277 | * @blksize: host filesystem's block size | |
1278 | */ | |
1279 | static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize) | |
1280 | { | |
1281 | int32_t iounit = 0; | |
1282 | V9fsState *s = pdu->s; | |
1283 | ||
1284 | /* | |
1285 | * iounit should be multiples of blksize (host filesystem block size) | |
1286 | * as well as less than (client msize - P9_IOHDRSZ) | |
1287 | */ | |
1288 | if (blksize) { | |
1289 | iounit = QEMU_ALIGN_DOWN(s->msize - P9_IOHDRSZ, blksize); | |
1290 | } | |
1291 | if (!iounit) { | |
1292 | iounit = s->msize - P9_IOHDRSZ; | |
1293 | } | |
1294 | return iounit; | |
1295 | } | |
1296 | ||
1297 | static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf) | |
1298 | { | |
1299 | return blksize_to_iounit(pdu, stbuf->st_blksize); | |
1300 | } | |
1301 | ||
1302 | static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, | |
1303 | V9fsStatDotl *v9lstat) | |
1304 | { | |
1305 | memset(v9lstat, 0, sizeof(*v9lstat)); | |
1306 | ||
1307 | v9lstat->st_mode = stbuf->st_mode; | |
1308 | v9lstat->st_nlink = stbuf->st_nlink; | |
1309 | v9lstat->st_uid = stbuf->st_uid; | |
1310 | v9lstat->st_gid = stbuf->st_gid; | |
1311 | v9lstat->st_rdev = stbuf->st_rdev; | |
1312 | v9lstat->st_size = stbuf->st_size; | |
1313 | v9lstat->st_blksize = stat_to_iounit(pdu, stbuf); | |
1314 | v9lstat->st_blocks = stbuf->st_blocks; | |
1315 | v9lstat->st_atime_sec = stbuf->st_atime; | |
1316 | v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; | |
1317 | v9lstat->st_mtime_sec = stbuf->st_mtime; | |
1318 | v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; | |
1319 | v9lstat->st_ctime_sec = stbuf->st_ctime; | |
1320 | v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec; | |
1321 | /* Currently we only support BASIC fields in stat */ | |
1322 | v9lstat->st_result_mask = P9_STATS_BASIC; | |
1323 | ||
1324 | return stat_to_qid(pdu, stbuf, &v9lstat->qid); | |
1325 | } | |
1326 | ||
1327 | static void print_sg(struct iovec *sg, int cnt) | |
1328 | { | |
1329 | int i; | |
1330 | ||
1331 | printf("sg[%d]: {", cnt); | |
1332 | for (i = 0; i < cnt; i++) { | |
1333 | if (i) { | |
1334 | printf(", "); | |
1335 | } | |
1336 | printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); | |
1337 | } | |
1338 | printf("}\n"); | |
1339 | } | |
1340 | ||
1341 | /* Will call this only for path name based fid */ | |
1342 | static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len) | |
1343 | { | |
1344 | V9fsPath str; | |
1345 | v9fs_path_init(&str); | |
1346 | v9fs_path_copy(&str, dst); | |
1347 | v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len); | |
1348 | v9fs_path_free(&str); | |
1349 | } | |
1350 | ||
1351 | static inline bool is_ro_export(FsContext *ctx) | |
1352 | { | |
1353 | return ctx->export_flags & V9FS_RDONLY; | |
1354 | } | |
1355 | ||
1356 | static void coroutine_fn v9fs_version(void *opaque) | |
1357 | { | |
1358 | ssize_t err; | |
1359 | V9fsPDU *pdu = opaque; | |
1360 | V9fsState *s = pdu->s; | |
1361 | V9fsString version; | |
1362 | size_t offset = 7; | |
1363 | ||
1364 | v9fs_string_init(&version); | |
1365 | err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version); | |
1366 | if (err < 0) { | |
1367 | goto out; | |
1368 | } | |
1369 | trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data); | |
1370 | ||
1371 | virtfs_reset(pdu); | |
1372 | ||
1373 | if (!strcmp(version.data, "9P2000.u")) { | |
1374 | s->proto_version = V9FS_PROTO_2000U; | |
1375 | } else if (!strcmp(version.data, "9P2000.L")) { | |
1376 | s->proto_version = V9FS_PROTO_2000L; | |
1377 | } else { | |
1378 | v9fs_string_sprintf(&version, "unknown"); | |
1379 | /* skip min. msize check, reporting invalid version has priority */ | |
1380 | goto marshal; | |
1381 | } | |
1382 | ||
1383 | if (s->msize < P9_MIN_MSIZE) { | |
1384 | err = -EMSGSIZE; | |
1385 | error_report( | |
1386 | "9pfs: Client requested msize < minimum msize (" | |
1387 | stringify(P9_MIN_MSIZE) ") supported by this server." | |
1388 | ); | |
1389 | goto out; | |
1390 | } | |
1391 | ||
1392 | /* 8192 is the default msize of Linux clients */ | |
1393 | if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) { | |
1394 | warn_report_once( | |
1395 | "9p: degraded performance: a reasonable high msize should be " | |
1396 | "chosen on client/guest side (chosen msize is <= 8192). See " | |
1397 | "https://wiki.qemu.org/Documentation/9psetup#msize for details." | |
1398 | ); | |
1399 | } | |
1400 | ||
1401 | marshal: | |
1402 | err = pdu_marshal(pdu, offset, "ds", s->msize, &version); | |
1403 | if (err < 0) { | |
1404 | goto out; | |
1405 | } | |
1406 | err += offset; | |
1407 | trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data); | |
1408 | out: | |
1409 | pdu_complete(pdu, err); | |
1410 | v9fs_string_free(&version); | |
1411 | } | |
1412 | ||
1413 | static void coroutine_fn v9fs_attach(void *opaque) | |
1414 | { | |
1415 | V9fsPDU *pdu = opaque; | |
1416 | V9fsState *s = pdu->s; | |
1417 | int32_t fid, afid, n_uname; | |
1418 | V9fsString uname, aname; | |
1419 | V9fsFidState *fidp; | |
1420 | size_t offset = 7; | |
1421 | V9fsQID qid; | |
1422 | ssize_t err; | |
1423 | struct stat stbuf; | |
1424 | ||
1425 | v9fs_string_init(&uname); | |
1426 | v9fs_string_init(&aname); | |
1427 | err = pdu_unmarshal(pdu, offset, "ddssd", &fid, | |
1428 | &afid, &uname, &aname, &n_uname); | |
1429 | if (err < 0) { | |
1430 | goto out_nofid; | |
1431 | } | |
1432 | trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data); | |
1433 | ||
1434 | fidp = alloc_fid(s, fid); | |
1435 | if (fidp == NULL) { | |
1436 | err = -EINVAL; | |
1437 | goto out_nofid; | |
1438 | } | |
1439 | fidp->uid = n_uname; | |
1440 | err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path); | |
1441 | if (err < 0) { | |
1442 | err = -EINVAL; | |
1443 | clunk_fid(s, fid); | |
1444 | goto out; | |
1445 | } | |
1446 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); | |
1447 | if (err < 0) { | |
1448 | err = -EINVAL; | |
1449 | clunk_fid(s, fid); | |
1450 | goto out; | |
1451 | } | |
1452 | err = stat_to_qid(pdu, &stbuf, &qid); | |
1453 | if (err < 0) { | |
1454 | err = -EINVAL; | |
1455 | clunk_fid(s, fid); | |
1456 | goto out; | |
1457 | } | |
1458 | ||
1459 | /* | |
1460 | * disable migration if we haven't done already. | |
1461 | * attach could get called multiple times for the same export. | |
1462 | */ | |
1463 | if (!s->migration_blocker) { | |
1464 | error_setg(&s->migration_blocker, | |
1465 | "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'", | |
1466 | s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag); | |
1467 | err = migrate_add_blocker(s->migration_blocker, NULL); | |
1468 | if (err < 0) { | |
1469 | error_free(s->migration_blocker); | |
1470 | s->migration_blocker = NULL; | |
1471 | clunk_fid(s, fid); | |
1472 | goto out; | |
1473 | } | |
1474 | s->root_fid = fid; | |
1475 | } | |
1476 | ||
1477 | err = pdu_marshal(pdu, offset, "Q", &qid); | |
1478 | if (err < 0) { | |
1479 | clunk_fid(s, fid); | |
1480 | goto out; | |
1481 | } | |
1482 | err += offset; | |
1483 | ||
1484 | memcpy(&s->root_st, &stbuf, sizeof(stbuf)); | |
1485 | trace_v9fs_attach_return(pdu->tag, pdu->id, | |
1486 | qid.type, qid.version, qid.path); | |
1487 | out: | |
1488 | put_fid(pdu, fidp); | |
1489 | out_nofid: | |
1490 | pdu_complete(pdu, err); | |
1491 | v9fs_string_free(&uname); | |
1492 | v9fs_string_free(&aname); | |
1493 | } | |
1494 | ||
1495 | static void coroutine_fn v9fs_stat(void *opaque) | |
1496 | { | |
1497 | int32_t fid; | |
1498 | V9fsStat v9stat; | |
1499 | ssize_t err = 0; | |
1500 | size_t offset = 7; | |
1501 | struct stat stbuf; | |
1502 | V9fsFidState *fidp; | |
1503 | V9fsPDU *pdu = opaque; | |
1504 | char *basename; | |
1505 | ||
1506 | err = pdu_unmarshal(pdu, offset, "d", &fid); | |
1507 | if (err < 0) { | |
1508 | goto out_nofid; | |
1509 | } | |
1510 | trace_v9fs_stat(pdu->tag, pdu->id, fid); | |
1511 | ||
1512 | fidp = get_fid(pdu, fid); | |
1513 | if (fidp == NULL) { | |
1514 | err = -ENOENT; | |
1515 | goto out_nofid; | |
1516 | } | |
1517 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); | |
1518 | if (err < 0) { | |
1519 | goto out; | |
1520 | } | |
1521 | basename = g_path_get_basename(fidp->path.data); | |
1522 | err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat); | |
1523 | g_free(basename); | |
1524 | if (err < 0) { | |
1525 | goto out; | |
1526 | } | |
1527 | err = pdu_marshal(pdu, offset, "wS", 0, &v9stat); | |
1528 | if (err < 0) { | |
1529 | v9fs_stat_free(&v9stat); | |
1530 | goto out; | |
1531 | } | |
1532 | trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode, | |
1533 | v9stat.atime, v9stat.mtime, v9stat.length); | |
1534 | err += offset; | |
1535 | v9fs_stat_free(&v9stat); | |
1536 | out: | |
1537 | put_fid(pdu, fidp); | |
1538 | out_nofid: | |
1539 | pdu_complete(pdu, err); | |
1540 | } | |
1541 | ||
1542 | static void coroutine_fn v9fs_getattr(void *opaque) | |
1543 | { | |
1544 | int32_t fid; | |
1545 | size_t offset = 7; | |
1546 | ssize_t retval = 0; | |
1547 | struct stat stbuf; | |
1548 | V9fsFidState *fidp; | |
1549 | uint64_t request_mask; | |
1550 | V9fsStatDotl v9stat_dotl; | |
1551 | V9fsPDU *pdu = opaque; | |
1552 | ||
1553 | retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); | |
1554 | if (retval < 0) { | |
1555 | goto out_nofid; | |
1556 | } | |
1557 | trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask); | |
1558 | ||
1559 | fidp = get_fid(pdu, fid); | |
1560 | if (fidp == NULL) { | |
1561 | retval = -ENOENT; | |
1562 | goto out_nofid; | |
1563 | } | |
1564 | /* | |
1565 | * Currently we only support BASIC fields in stat, so there is no | |
1566 | * need to look at request_mask. | |
1567 | */ | |
1568 | retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf); | |
1569 | if (retval < 0) { | |
1570 | goto out; | |
1571 | } | |
1572 | retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl); | |
1573 | if (retval < 0) { | |
1574 | goto out; | |
1575 | } | |
1576 | ||
1577 | /* fill st_gen if requested and supported by underlying fs */ | |
1578 | if (request_mask & P9_STATS_GEN) { | |
1579 | retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl); | |
1580 | switch (retval) { | |
1581 | case 0: | |
1582 | /* we have valid st_gen: update result mask */ | |
1583 | v9stat_dotl.st_result_mask |= P9_STATS_GEN; | |
1584 | break; | |
1585 | case -EINTR: | |
1586 | /* request cancelled, e.g. by Tflush */ | |
1587 | goto out; | |
1588 | default: | |
1589 | /* failed to get st_gen: not fatal, ignore */ | |
1590 | break; | |
1591 | } | |
1592 | } | |
1593 | retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl); | |
1594 | if (retval < 0) { | |
1595 | goto out; | |
1596 | } | |
1597 | retval += offset; | |
1598 | trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask, | |
1599 | v9stat_dotl.st_mode, v9stat_dotl.st_uid, | |
1600 | v9stat_dotl.st_gid); | |
1601 | out: | |
1602 | put_fid(pdu, fidp); | |
1603 | out_nofid: | |
1604 | pdu_complete(pdu, retval); | |
1605 | } | |
1606 | ||
1607 | /* Attribute flags */ | |
1608 | #define P9_ATTR_MODE (1 << 0) | |
1609 | #define P9_ATTR_UID (1 << 1) | |
1610 | #define P9_ATTR_GID (1 << 2) | |
1611 | #define P9_ATTR_SIZE (1 << 3) | |
1612 | #define P9_ATTR_ATIME (1 << 4) | |
1613 | #define P9_ATTR_MTIME (1 << 5) | |
1614 | #define P9_ATTR_CTIME (1 << 6) | |
1615 | #define P9_ATTR_ATIME_SET (1 << 7) | |
1616 | #define P9_ATTR_MTIME_SET (1 << 8) | |
1617 | ||
1618 | #define P9_ATTR_MASK 127 | |
1619 | ||
1620 | static void coroutine_fn v9fs_setattr(void *opaque) | |
1621 | { | |
1622 | int err = 0; | |
1623 | int32_t fid; | |
1624 | V9fsFidState *fidp; | |
1625 | size_t offset = 7; | |
1626 | V9fsIattr v9iattr; | |
1627 | V9fsPDU *pdu = opaque; | |
1628 | ||
1629 | err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); | |
1630 | if (err < 0) { | |
1631 | goto out_nofid; | |
1632 | } | |
1633 | ||
1634 | trace_v9fs_setattr(pdu->tag, pdu->id, fid, | |
1635 | v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid, | |
1636 | v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec); | |
1637 | ||
1638 | fidp = get_fid(pdu, fid); | |
1639 | if (fidp == NULL) { | |
1640 | err = -EINVAL; | |
1641 | goto out_nofid; | |
1642 | } | |
1643 | if (v9iattr.valid & P9_ATTR_MODE) { | |
1644 | err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode); | |
1645 | if (err < 0) { | |
1646 | goto out; | |
1647 | } | |
1648 | } | |
1649 | if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) { | |
1650 | struct timespec times[2]; | |
1651 | if (v9iattr.valid & P9_ATTR_ATIME) { | |
1652 | if (v9iattr.valid & P9_ATTR_ATIME_SET) { | |
1653 | times[0].tv_sec = v9iattr.atime_sec; | |
1654 | times[0].tv_nsec = v9iattr.atime_nsec; | |
1655 | } else { | |
1656 | times[0].tv_nsec = UTIME_NOW; | |
1657 | } | |
1658 | } else { | |
1659 | times[0].tv_nsec = UTIME_OMIT; | |
1660 | } | |
1661 | if (v9iattr.valid & P9_ATTR_MTIME) { | |
1662 | if (v9iattr.valid & P9_ATTR_MTIME_SET) { | |
1663 | times[1].tv_sec = v9iattr.mtime_sec; | |
1664 | times[1].tv_nsec = v9iattr.mtime_nsec; | |
1665 | } else { | |
1666 | times[1].tv_nsec = UTIME_NOW; | |
1667 | } | |
1668 | } else { | |
1669 | times[1].tv_nsec = UTIME_OMIT; | |
1670 | } | |
1671 | err = v9fs_co_utimensat(pdu, &fidp->path, times); | |
1672 | if (err < 0) { | |
1673 | goto out; | |
1674 | } | |
1675 | } | |
1676 | /* | |
1677 | * If the only valid entry in iattr is ctime we can call | |
1678 | * chown(-1,-1) to update the ctime of the file | |
1679 | */ | |
1680 | if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) || | |
1681 | ((v9iattr.valid & P9_ATTR_CTIME) | |
1682 | && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) { | |
1683 | if (!(v9iattr.valid & P9_ATTR_UID)) { | |
1684 | v9iattr.uid = -1; | |
1685 | } | |
1686 | if (!(v9iattr.valid & P9_ATTR_GID)) { | |
1687 | v9iattr.gid = -1; | |
1688 | } | |
1689 | err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid, | |
1690 | v9iattr.gid); | |
1691 | if (err < 0) { | |
1692 | goto out; | |
1693 | } | |
1694 | } | |
1695 | if (v9iattr.valid & (P9_ATTR_SIZE)) { | |
1696 | err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size); | |
1697 | if (err < 0) { | |
1698 | goto out; | |
1699 | } | |
1700 | } | |
1701 | err = offset; | |
1702 | trace_v9fs_setattr_return(pdu->tag, pdu->id); | |
1703 | out: | |
1704 | put_fid(pdu, fidp); | |
1705 | out_nofid: | |
1706 | pdu_complete(pdu, err); | |
1707 | } | |
1708 | ||
1709 | static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids) | |
1710 | { | |
1711 | int i; | |
1712 | ssize_t err; | |
1713 | size_t offset = 7; | |
1714 | ||
1715 | err = pdu_marshal(pdu, offset, "w", nwnames); | |
1716 | if (err < 0) { | |
1717 | return err; | |
1718 | } | |
1719 | offset += err; | |
1720 | for (i = 0; i < nwnames; i++) { | |
1721 | err = pdu_marshal(pdu, offset, "Q", &qids[i]); | |
1722 | if (err < 0) { | |
1723 | return err; | |
1724 | } | |
1725 | offset += err; | |
1726 | } | |
1727 | return offset; | |
1728 | } | |
1729 | ||
1730 | static bool name_is_illegal(const char *name) | |
1731 | { | |
1732 | return !*name || strchr(name, '/') != NULL; | |
1733 | } | |
1734 | ||
1735 | static bool same_stat_id(const struct stat *a, const struct stat *b) | |
1736 | { | |
1737 | return a->st_dev == b->st_dev && a->st_ino == b->st_ino; | |
1738 | } | |
1739 | ||
1740 | static void coroutine_fn v9fs_walk(void *opaque) | |
1741 | { | |
1742 | int name_idx; | |
1743 | g_autofree V9fsQID *qids = NULL; | |
1744 | int i, err = 0; | |
1745 | V9fsPath dpath, path; | |
1746 | P9ARRAY_REF(V9fsPath) pathes = NULL; | |
1747 | uint16_t nwnames; | |
1748 | struct stat stbuf, fidst; | |
1749 | g_autofree struct stat *stbufs = NULL; | |
1750 | size_t offset = 7; | |
1751 | int32_t fid, newfid; | |
1752 | P9ARRAY_REF(V9fsString) wnames = NULL; | |
1753 | V9fsFidState *fidp; | |
1754 | V9fsFidState *newfidp = NULL; | |
1755 | V9fsPDU *pdu = opaque; | |
1756 | V9fsState *s = pdu->s; | |
1757 | V9fsQID qid; | |
1758 | ||
1759 | err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames); | |
1760 | if (err < 0) { | |
1761 | pdu_complete(pdu, err); | |
1762 | return ; | |
1763 | } | |
1764 | offset += err; | |
1765 | ||
1766 | trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames); | |
1767 | ||
1768 | if (nwnames > P9_MAXWELEM) { | |
1769 | err = -EINVAL; | |
1770 | goto out_nofid; | |
1771 | } | |
1772 | if (nwnames) { | |
1773 | P9ARRAY_NEW(V9fsString, wnames, nwnames); | |
1774 | qids = g_new0(V9fsQID, nwnames); | |
1775 | stbufs = g_new0(struct stat, nwnames); | |
1776 | P9ARRAY_NEW(V9fsPath, pathes, nwnames); | |
1777 | for (i = 0; i < nwnames; i++) { | |
1778 | err = pdu_unmarshal(pdu, offset, "s", &wnames[i]); | |
1779 | if (err < 0) { | |
1780 | goto out_nofid; | |
1781 | } | |
1782 | if (name_is_illegal(wnames[i].data)) { | |
1783 | err = -ENOENT; | |
1784 | goto out_nofid; | |
1785 | } | |
1786 | offset += err; | |
1787 | } | |
1788 | } | |
1789 | fidp = get_fid(pdu, fid); | |
1790 | if (fidp == NULL) { | |
1791 | err = -ENOENT; | |
1792 | goto out_nofid; | |
1793 | } | |
1794 | ||
1795 | v9fs_path_init(&dpath); | |
1796 | v9fs_path_init(&path); | |
1797 | /* | |
1798 | * Both dpath and path initially point to fidp. | |
1799 | * Needed to handle request with nwnames == 0 | |
1800 | */ | |
1801 | v9fs_path_copy(&dpath, &fidp->path); | |
1802 | v9fs_path_copy(&path, &fidp->path); | |
1803 | ||
1804 | /* | |
1805 | * To keep latency (i.e. overall execution time for processing this | |
1806 | * Twalk client request) as small as possible, run all the required fs | |
1807 | * driver code altogether inside the following block. | |
1808 | */ | |
1809 | v9fs_co_run_in_worker({ | |
1810 | if (v9fs_request_cancelled(pdu)) { | |
1811 | err = -EINTR; | |
1812 | break; | |
1813 | } | |
1814 | err = s->ops->lstat(&s->ctx, &dpath, &fidst); | |
1815 | if (err < 0) { | |
1816 | err = -errno; | |
1817 | break; | |
1818 | } | |
1819 | stbuf = fidst; | |
1820 | for (name_idx = 0; name_idx < nwnames; name_idx++) { | |
1821 | if (v9fs_request_cancelled(pdu)) { | |
1822 | err = -EINTR; | |
1823 | break; | |
1824 | } | |
1825 | if (!same_stat_id(&pdu->s->root_st, &stbuf) || | |
1826 | strcmp("..", wnames[name_idx].data)) | |
1827 | { | |
1828 | err = s->ops->name_to_path(&s->ctx, &dpath, | |
1829 | wnames[name_idx].data, | |
1830 | &pathes[name_idx]); | |
1831 | if (err < 0) { | |
1832 | err = -errno; | |
1833 | break; | |
1834 | } | |
1835 | if (v9fs_request_cancelled(pdu)) { | |
1836 | err = -EINTR; | |
1837 | break; | |
1838 | } | |
1839 | err = s->ops->lstat(&s->ctx, &pathes[name_idx], &stbuf); | |
1840 | if (err < 0) { | |
1841 | err = -errno; | |
1842 | break; | |
1843 | } | |
1844 | stbufs[name_idx] = stbuf; | |
1845 | v9fs_path_copy(&dpath, &pathes[name_idx]); | |
1846 | } | |
1847 | } | |
1848 | }); | |
1849 | /* | |
1850 | * Handle all the rest of this Twalk request on main thread ... | |
1851 | */ | |
1852 | if (err < 0) { | |
1853 | goto out; | |
1854 | } | |
1855 | ||
1856 | err = stat_to_qid(pdu, &fidst, &qid); | |
1857 | if (err < 0) { | |
1858 | goto out; | |
1859 | } | |
1860 | stbuf = fidst; | |
1861 | ||
1862 | /* reset dpath and path */ | |
1863 | v9fs_path_copy(&dpath, &fidp->path); | |
1864 | v9fs_path_copy(&path, &fidp->path); | |
1865 | ||
1866 | for (name_idx = 0; name_idx < nwnames; name_idx++) { | |
1867 | if (!same_stat_id(&pdu->s->root_st, &stbuf) || | |
1868 | strcmp("..", wnames[name_idx].data)) | |
1869 | { | |
1870 | stbuf = stbufs[name_idx]; | |
1871 | err = stat_to_qid(pdu, &stbuf, &qid); | |
1872 | if (err < 0) { | |
1873 | goto out; | |
1874 | } | |
1875 | v9fs_path_copy(&path, &pathes[name_idx]); | |
1876 | v9fs_path_copy(&dpath, &path); | |
1877 | } | |
1878 | memcpy(&qids[name_idx], &qid, sizeof(qid)); | |
1879 | } | |
1880 | if (fid == newfid) { | |
1881 | if (fidp->fid_type != P9_FID_NONE) { | |
1882 | err = -EINVAL; | |
1883 | goto out; | |
1884 | } | |
1885 | v9fs_path_write_lock(s); | |
1886 | v9fs_path_copy(&fidp->path, &path); | |
1887 | v9fs_path_unlock(s); | |
1888 | } else { | |
1889 | newfidp = alloc_fid(s, newfid); | |
1890 | if (newfidp == NULL) { | |
1891 | err = -EINVAL; | |
1892 | goto out; | |
1893 | } | |
1894 | newfidp->uid = fidp->uid; | |
1895 | v9fs_path_copy(&newfidp->path, &path); | |
1896 | } | |
1897 | err = v9fs_walk_marshal(pdu, nwnames, qids); | |
1898 | trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids); | |
1899 | out: | |
1900 | put_fid(pdu, fidp); | |
1901 | if (newfidp) { | |
1902 | put_fid(pdu, newfidp); | |
1903 | } | |
1904 | v9fs_path_free(&dpath); | |
1905 | v9fs_path_free(&path); | |
1906 | out_nofid: | |
1907 | pdu_complete(pdu, err); | |
1908 | } | |
1909 | ||
1910 | static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) | |
1911 | { | |
1912 | struct statfs stbuf; | |
1913 | int err = v9fs_co_statfs(pdu, path, &stbuf); | |
1914 | ||
1915 | return blksize_to_iounit(pdu, (err >= 0) ? stbuf.f_bsize : 0); | |
1916 | } | |
1917 | ||
1918 | static void coroutine_fn v9fs_open(void *opaque) | |
1919 | { | |
1920 | int flags; | |
1921 | int32_t fid; | |
1922 | int32_t mode; | |
1923 | V9fsQID qid; | |
1924 | int iounit = 0; | |
1925 | ssize_t err = 0; | |
1926 | size_t offset = 7; | |
1927 | struct stat stbuf; | |
1928 | V9fsFidState *fidp; | |
1929 | V9fsPDU *pdu = opaque; | |
1930 | V9fsState *s = pdu->s; | |
1931 | ||
1932 | if (s->proto_version == V9FS_PROTO_2000L) { | |
1933 | err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode); | |
1934 | } else { | |
1935 | uint8_t modebyte; | |
1936 | err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte); | |
1937 | mode = modebyte; | |
1938 | } | |
1939 | if (err < 0) { | |
1940 | goto out_nofid; | |
1941 | } | |
1942 | trace_v9fs_open(pdu->tag, pdu->id, fid, mode); | |
1943 | ||
1944 | fidp = get_fid(pdu, fid); | |
1945 | if (fidp == NULL) { | |
1946 | err = -ENOENT; | |
1947 | goto out_nofid; | |
1948 | } | |
1949 | if (fidp->fid_type != P9_FID_NONE) { | |
1950 | err = -EINVAL; | |
1951 | goto out; | |
1952 | } | |
1953 | ||
1954 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); | |
1955 | if (err < 0) { | |
1956 | goto out; | |
1957 | } | |
1958 | err = stat_to_qid(pdu, &stbuf, &qid); | |
1959 | if (err < 0) { | |
1960 | goto out; | |
1961 | } | |
1962 | if (S_ISDIR(stbuf.st_mode)) { | |
1963 | err = v9fs_co_opendir(pdu, fidp); | |
1964 | if (err < 0) { | |
1965 | goto out; | |
1966 | } | |
1967 | fidp->fid_type = P9_FID_DIR; | |
1968 | err = pdu_marshal(pdu, offset, "Qd", &qid, 0); | |
1969 | if (err < 0) { | |
1970 | goto out; | |
1971 | } | |
1972 | err += offset; | |
1973 | } else { | |
1974 | if (s->proto_version == V9FS_PROTO_2000L) { | |
1975 | flags = get_dotl_openflags(s, mode); | |
1976 | } else { | |
1977 | flags = omode_to_uflags(mode); | |
1978 | } | |
1979 | if (is_ro_export(&s->ctx)) { | |
1980 | if (mode & O_WRONLY || mode & O_RDWR || | |
1981 | mode & O_APPEND || mode & O_TRUNC) { | |
1982 | err = -EROFS; | |
1983 | goto out; | |
1984 | } | |
1985 | } | |
1986 | err = v9fs_co_open(pdu, fidp, flags); | |
1987 | if (err < 0) { | |
1988 | goto out; | |
1989 | } | |
1990 | fidp->fid_type = P9_FID_FILE; | |
1991 | fidp->open_flags = flags; | |
1992 | if (flags & O_EXCL) { | |
1993 | /* | |
1994 | * We let the host file system do O_EXCL check | |
1995 | * We should not reclaim such fd | |
1996 | */ | |
1997 | fidp->flags |= FID_NON_RECLAIMABLE; | |
1998 | } | |
1999 | iounit = get_iounit(pdu, &fidp->path); | |
2000 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); | |
2001 | if (err < 0) { | |
2002 | goto out; | |
2003 | } | |
2004 | err += offset; | |
2005 | } | |
2006 | trace_v9fs_open_return(pdu->tag, pdu->id, | |
2007 | qid.type, qid.version, qid.path, iounit); | |
2008 | out: | |
2009 | put_fid(pdu, fidp); | |
2010 | out_nofid: | |
2011 | pdu_complete(pdu, err); | |
2012 | } | |
2013 | ||
2014 | static void coroutine_fn v9fs_lcreate(void *opaque) | |
2015 | { | |
2016 | int32_t dfid, flags, mode; | |
2017 | gid_t gid; | |
2018 | ssize_t err = 0; | |
2019 | ssize_t offset = 7; | |
2020 | V9fsString name; | |
2021 | V9fsFidState *fidp; | |
2022 | struct stat stbuf; | |
2023 | V9fsQID qid; | |
2024 | int32_t iounit; | |
2025 | V9fsPDU *pdu = opaque; | |
2026 | ||
2027 | v9fs_string_init(&name); | |
2028 | err = pdu_unmarshal(pdu, offset, "dsddd", &dfid, | |
2029 | &name, &flags, &mode, &gid); | |
2030 | if (err < 0) { | |
2031 | goto out_nofid; | |
2032 | } | |
2033 | trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid); | |
2034 | ||
2035 | if (name_is_illegal(name.data)) { | |
2036 | err = -ENOENT; | |
2037 | goto out_nofid; | |
2038 | } | |
2039 | ||
2040 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { | |
2041 | err = -EEXIST; | |
2042 | goto out_nofid; | |
2043 | } | |
2044 | ||
2045 | fidp = get_fid(pdu, dfid); | |
2046 | if (fidp == NULL) { | |
2047 | err = -ENOENT; | |
2048 | goto out_nofid; | |
2049 | } | |
2050 | if (fidp->fid_type != P9_FID_NONE) { | |
2051 | err = -EINVAL; | |
2052 | goto out; | |
2053 | } | |
2054 | ||
2055 | flags = get_dotl_openflags(pdu->s, flags); | |
2056 | err = v9fs_co_open2(pdu, fidp, &name, gid, | |
2057 | flags | O_CREAT, mode, &stbuf); | |
2058 | if (err < 0) { | |
2059 | goto out; | |
2060 | } | |
2061 | fidp->fid_type = P9_FID_FILE; | |
2062 | fidp->open_flags = flags; | |
2063 | if (flags & O_EXCL) { | |
2064 | /* | |
2065 | * We let the host file system do O_EXCL check | |
2066 | * We should not reclaim such fd | |
2067 | */ | |
2068 | fidp->flags |= FID_NON_RECLAIMABLE; | |
2069 | } | |
2070 | iounit = get_iounit(pdu, &fidp->path); | |
2071 | err = stat_to_qid(pdu, &stbuf, &qid); | |
2072 | if (err < 0) { | |
2073 | goto out; | |
2074 | } | |
2075 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); | |
2076 | if (err < 0) { | |
2077 | goto out; | |
2078 | } | |
2079 | err += offset; | |
2080 | trace_v9fs_lcreate_return(pdu->tag, pdu->id, | |
2081 | qid.type, qid.version, qid.path, iounit); | |
2082 | out: | |
2083 | put_fid(pdu, fidp); | |
2084 | out_nofid: | |
2085 | pdu_complete(pdu, err); | |
2086 | v9fs_string_free(&name); | |
2087 | } | |
2088 | ||
2089 | static void coroutine_fn v9fs_fsync(void *opaque) | |
2090 | { | |
2091 | int err; | |
2092 | int32_t fid; | |
2093 | int datasync; | |
2094 | size_t offset = 7; | |
2095 | V9fsFidState *fidp; | |
2096 | V9fsPDU *pdu = opaque; | |
2097 | ||
2098 | err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); | |
2099 | if (err < 0) { | |
2100 | goto out_nofid; | |
2101 | } | |
2102 | trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync); | |
2103 | ||
2104 | fidp = get_fid(pdu, fid); | |
2105 | if (fidp == NULL) { | |
2106 | err = -ENOENT; | |
2107 | goto out_nofid; | |
2108 | } | |
2109 | err = v9fs_co_fsync(pdu, fidp, datasync); | |
2110 | if (!err) { | |
2111 | err = offset; | |
2112 | } | |
2113 | put_fid(pdu, fidp); | |
2114 | out_nofid: | |
2115 | pdu_complete(pdu, err); | |
2116 | } | |
2117 | ||
2118 | static void coroutine_fn v9fs_clunk(void *opaque) | |
2119 | { | |
2120 | int err; | |
2121 | int32_t fid; | |
2122 | size_t offset = 7; | |
2123 | V9fsFidState *fidp; | |
2124 | V9fsPDU *pdu = opaque; | |
2125 | V9fsState *s = pdu->s; | |
2126 | ||
2127 | err = pdu_unmarshal(pdu, offset, "d", &fid); | |
2128 | if (err < 0) { | |
2129 | goto out_nofid; | |
2130 | } | |
2131 | trace_v9fs_clunk(pdu->tag, pdu->id, fid); | |
2132 | ||
2133 | fidp = clunk_fid(s, fid); | |
2134 | if (fidp == NULL) { | |
2135 | err = -ENOENT; | |
2136 | goto out_nofid; | |
2137 | } | |
2138 | /* | |
2139 | * Bump the ref so that put_fid will | |
2140 | * free the fid. | |
2141 | */ | |
2142 | fidp->ref++; | |
2143 | err = put_fid(pdu, fidp); | |
2144 | if (!err) { | |
2145 | err = offset; | |
2146 | } | |
2147 | out_nofid: | |
2148 | pdu_complete(pdu, err); | |
2149 | } | |
2150 | ||
2151 | /* | |
2152 | * Create a QEMUIOVector for a sub-region of PDU iovecs | |
2153 | * | |
2154 | * @qiov: uninitialized QEMUIOVector | |
2155 | * @skip: number of bytes to skip from beginning of PDU | |
2156 | * @size: number of bytes to include | |
2157 | * @is_write: true - write, false - read | |
2158 | * | |
2159 | * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up | |
2160 | * with qemu_iovec_destroy(). | |
2161 | */ | |
2162 | static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu, | |
2163 | size_t skip, size_t size, | |
2164 | bool is_write) | |
2165 | { | |
2166 | QEMUIOVector elem; | |
2167 | struct iovec *iov; | |
2168 | unsigned int niov; | |
2169 | ||
2170 | if (is_write) { | |
2171 | pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip); | |
2172 | } else { | |
2173 | pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip); | |
2174 | } | |
2175 | ||
2176 | qemu_iovec_init_external(&elem, iov, niov); | |
2177 | qemu_iovec_init(qiov, niov); | |
2178 | qemu_iovec_concat(qiov, &elem, skip, size); | |
2179 | } | |
2180 | ||
2181 | static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, | |
2182 | uint64_t off, uint32_t max_count) | |
2183 | { | |
2184 | ssize_t err; | |
2185 | size_t offset = 7; | |
2186 | uint64_t read_count; | |
2187 | QEMUIOVector qiov_full; | |
2188 | ||
2189 | if (fidp->fs.xattr.len < off) { | |
2190 | read_count = 0; | |
2191 | } else { | |
2192 | read_count = fidp->fs.xattr.len - off; | |
2193 | } | |
2194 | if (read_count > max_count) { | |
2195 | read_count = max_count; | |
2196 | } | |
2197 | err = pdu_marshal(pdu, offset, "d", read_count); | |
2198 | if (err < 0) { | |
2199 | return err; | |
2200 | } | |
2201 | offset += err; | |
2202 | ||
2203 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false); | |
2204 | err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0, | |
2205 | ((char *)fidp->fs.xattr.value) + off, | |
2206 | read_count); | |
2207 | qemu_iovec_destroy(&qiov_full); | |
2208 | if (err < 0) { | |
2209 | return err; | |
2210 | } | |
2211 | offset += err; | |
2212 | return offset; | |
2213 | } | |
2214 | ||
2215 | static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, | |
2216 | V9fsFidState *fidp, | |
2217 | uint32_t max_count) | |
2218 | { | |
2219 | V9fsPath path; | |
2220 | V9fsStat v9stat; | |
2221 | int len, err = 0; | |
2222 | int32_t count = 0; | |
2223 | struct stat stbuf; | |
2224 | off_t saved_dir_pos; | |
2225 | struct dirent *dent; | |
2226 | ||
2227 | /* save the directory position */ | |
2228 | saved_dir_pos = v9fs_co_telldir(pdu, fidp); | |
2229 | if (saved_dir_pos < 0) { | |
2230 | return saved_dir_pos; | |
2231 | } | |
2232 | ||
2233 | while (1) { | |
2234 | v9fs_path_init(&path); | |
2235 | ||
2236 | v9fs_readdir_lock(&fidp->fs.dir); | |
2237 | ||
2238 | err = v9fs_co_readdir(pdu, fidp, &dent); | |
2239 | if (err || !dent) { | |
2240 | break; | |
2241 | } | |
2242 | err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); | |
2243 | if (err < 0) { | |
2244 | break; | |
2245 | } | |
2246 | err = v9fs_co_lstat(pdu, &path, &stbuf); | |
2247 | if (err < 0) { | |
2248 | break; | |
2249 | } | |
2250 | err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat); | |
2251 | if (err < 0) { | |
2252 | break; | |
2253 | } | |
2254 | if ((count + v9stat.size + 2) > max_count) { | |
2255 | v9fs_readdir_unlock(&fidp->fs.dir); | |
2256 | ||
2257 | /* Ran out of buffer. Set dir back to old position and return */ | |
2258 | v9fs_co_seekdir(pdu, fidp, saved_dir_pos); | |
2259 | v9fs_stat_free(&v9stat); | |
2260 | v9fs_path_free(&path); | |
2261 | return count; | |
2262 | } | |
2263 | ||
2264 | /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ | |
2265 | len = pdu_marshal(pdu, 11 + count, "S", &v9stat); | |
2266 | ||
2267 | v9fs_readdir_unlock(&fidp->fs.dir); | |
2268 | ||
2269 | if (len < 0) { | |
2270 | v9fs_co_seekdir(pdu, fidp, saved_dir_pos); | |
2271 | v9fs_stat_free(&v9stat); | |
2272 | v9fs_path_free(&path); | |
2273 | return len; | |
2274 | } | |
2275 | count += len; | |
2276 | v9fs_stat_free(&v9stat); | |
2277 | v9fs_path_free(&path); | |
2278 | saved_dir_pos = dent->d_off; | |
2279 | } | |
2280 | ||
2281 | v9fs_readdir_unlock(&fidp->fs.dir); | |
2282 | ||
2283 | v9fs_path_free(&path); | |
2284 | if (err < 0) { | |
2285 | return err; | |
2286 | } | |
2287 | return count; | |
2288 | } | |
2289 | ||
2290 | static void coroutine_fn v9fs_read(void *opaque) | |
2291 | { | |
2292 | int32_t fid; | |
2293 | uint64_t off; | |
2294 | ssize_t err = 0; | |
2295 | int32_t count = 0; | |
2296 | size_t offset = 7; | |
2297 | uint32_t max_count; | |
2298 | V9fsFidState *fidp; | |
2299 | V9fsPDU *pdu = opaque; | |
2300 | V9fsState *s = pdu->s; | |
2301 | ||
2302 | err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); | |
2303 | if (err < 0) { | |
2304 | goto out_nofid; | |
2305 | } | |
2306 | trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count); | |
2307 | ||
2308 | fidp = get_fid(pdu, fid); | |
2309 | if (fidp == NULL) { | |
2310 | err = -EINVAL; | |
2311 | goto out_nofid; | |
2312 | } | |
2313 | if (fidp->fid_type == P9_FID_DIR) { | |
2314 | if (s->proto_version != V9FS_PROTO_2000U) { | |
2315 | warn_report_once( | |
2316 | "9p: bad client: T_read request on directory only expected " | |
2317 | "with 9P2000.u protocol version" | |
2318 | ); | |
2319 | err = -EOPNOTSUPP; | |
2320 | goto out; | |
2321 | } | |
2322 | if (off == 0) { | |
2323 | v9fs_co_rewinddir(pdu, fidp); | |
2324 | } | |
2325 | count = v9fs_do_readdir_with_stat(pdu, fidp, max_count); | |
2326 | if (count < 0) { | |
2327 | err = count; | |
2328 | goto out; | |
2329 | } | |
2330 | err = pdu_marshal(pdu, offset, "d", count); | |
2331 | if (err < 0) { | |
2332 | goto out; | |
2333 | } | |
2334 | err += offset + count; | |
2335 | } else if (fidp->fid_type == P9_FID_FILE) { | |
2336 | QEMUIOVector qiov_full; | |
2337 | QEMUIOVector qiov; | |
2338 | int32_t len; | |
2339 | ||
2340 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false); | |
2341 | qemu_iovec_init(&qiov, qiov_full.niov); | |
2342 | do { | |
2343 | qemu_iovec_reset(&qiov); | |
2344 | qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count); | |
2345 | if (0) { | |
2346 | print_sg(qiov.iov, qiov.niov); | |
2347 | } | |
2348 | /* Loop in case of EINTR */ | |
2349 | do { | |
2350 | len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off); | |
2351 | if (len >= 0) { | |
2352 | off += len; | |
2353 | count += len; | |
2354 | } | |
2355 | } while (len == -EINTR && !pdu->cancelled); | |
2356 | if (len < 0) { | |
2357 | /* IO error return the error */ | |
2358 | err = len; | |
2359 | goto out_free_iovec; | |
2360 | } | |
2361 | } while (count < max_count && len > 0); | |
2362 | err = pdu_marshal(pdu, offset, "d", count); | |
2363 | if (err < 0) { | |
2364 | goto out_free_iovec; | |
2365 | } | |
2366 | err += offset + count; | |
2367 | out_free_iovec: | |
2368 | qemu_iovec_destroy(&qiov); | |
2369 | qemu_iovec_destroy(&qiov_full); | |
2370 | } else if (fidp->fid_type == P9_FID_XATTR) { | |
2371 | err = v9fs_xattr_read(s, pdu, fidp, off, max_count); | |
2372 | } else { | |
2373 | err = -EINVAL; | |
2374 | } | |
2375 | trace_v9fs_read_return(pdu->tag, pdu->id, count, err); | |
2376 | out: | |
2377 | put_fid(pdu, fidp); | |
2378 | out_nofid: | |
2379 | pdu_complete(pdu, err); | |
2380 | } | |
2381 | ||
2382 | /** | |
2383 | * Returns size required in Rreaddir response for the passed dirent @p name. | |
2384 | * | |
2385 | * @param name - directory entry's name (i.e. file name, directory name) | |
2386 | * @returns required size in bytes | |
2387 | */ | |
2388 | size_t v9fs_readdir_response_size(V9fsString *name) | |
2389 | { | |
2390 | /* | |
2391 | * Size of each dirent on the wire: size of qid (13) + size of offset (8) | |
2392 | * size of type (1) + size of name.size (2) + strlen(name.data) | |
2393 | */ | |
2394 | return 24 + v9fs_string_size(name); | |
2395 | } | |
2396 | ||
2397 | static void v9fs_free_dirents(struct V9fsDirEnt *e) | |
2398 | { | |
2399 | struct V9fsDirEnt *next = NULL; | |
2400 | ||
2401 | for (; e; e = next) { | |
2402 | next = e->next; | |
2403 | g_free(e->dent); | |
2404 | g_free(e->st); | |
2405 | g_free(e); | |
2406 | } | |
2407 | } | |
2408 | ||
2409 | static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, | |
2410 | off_t offset, int32_t max_count) | |
2411 | { | |
2412 | size_t size; | |
2413 | V9fsQID qid; | |
2414 | V9fsString name; | |
2415 | int len, err = 0; | |
2416 | int32_t count = 0; | |
2417 | struct dirent *dent; | |
2418 | struct stat *st; | |
2419 | struct V9fsDirEnt *entries = NULL; | |
2420 | ||
2421 | /* | |
2422 | * inode remapping requires the device id, which in turn might be | |
2423 | * different for different directory entries, so if inode remapping is | |
2424 | * enabled we have to make a full stat for each directory entry | |
2425 | */ | |
2426 | const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES; | |
2427 | ||
2428 | /* | |
2429 | * Fetch all required directory entries altogether on a background IO | |
2430 | * thread from fs driver. We don't want to do that for each entry | |
2431 | * individually, because hopping between threads (this main IO thread | |
2432 | * and background IO driver thread) would sum up to huge latencies. | |
2433 | */ | |
2434 | count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count, | |
2435 | dostat); | |
2436 | if (count < 0) { | |
2437 | err = count; | |
2438 | count = 0; | |
2439 | goto out; | |
2440 | } | |
2441 | count = 0; | |
2442 | ||
2443 | for (struct V9fsDirEnt *e = entries; e; e = e->next) { | |
2444 | dent = e->dent; | |
2445 | ||
2446 | if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { | |
2447 | st = e->st; | |
2448 | /* e->st should never be NULL, but just to be sure */ | |
2449 | if (!st) { | |
2450 | err = -1; | |
2451 | break; | |
2452 | } | |
2453 | ||
2454 | /* remap inode */ | |
2455 | err = stat_to_qid(pdu, st, &qid); | |
2456 | if (err < 0) { | |
2457 | break; | |
2458 | } | |
2459 | } else { | |
2460 | /* | |
2461 | * Fill up just the path field of qid because the client uses | |
2462 | * only that. To fill the entire qid structure we will have | |
2463 | * to stat each dirent found, which is expensive. For the | |
2464 | * latter reason we don't call stat_to_qid() here. Only drawback | |
2465 | * is that no multi-device export detection of stat_to_qid() | |
2466 | * would be done and provided as error to the user here. But | |
2467 | * user would get that error anyway when accessing those | |
2468 | * files/dirs through other ways. | |
2469 | */ | |
2470 | size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); | |
2471 | memcpy(&qid.path, &dent->d_ino, size); | |
2472 | /* Fill the other fields with dummy values */ | |
2473 | qid.type = 0; | |
2474 | qid.version = 0; | |
2475 | } | |
2476 | ||
2477 | v9fs_string_init(&name); | |
2478 | v9fs_string_sprintf(&name, "%s", dent->d_name); | |
2479 | ||
2480 | /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ | |
2481 | len = pdu_marshal(pdu, 11 + count, "Qqbs", | |
2482 | &qid, dent->d_off, | |
2483 | dent->d_type, &name); | |
2484 | ||
2485 | v9fs_string_free(&name); | |
2486 | ||
2487 | if (len < 0) { | |
2488 | err = len; | |
2489 | break; | |
2490 | } | |
2491 | ||
2492 | count += len; | |
2493 | } | |
2494 | ||
2495 | out: | |
2496 | v9fs_free_dirents(entries); | |
2497 | if (err < 0) { | |
2498 | return err; | |
2499 | } | |
2500 | return count; | |
2501 | } | |
2502 | ||
2503 | static void coroutine_fn v9fs_readdir(void *opaque) | |
2504 | { | |
2505 | int32_t fid; | |
2506 | V9fsFidState *fidp; | |
2507 | ssize_t retval = 0; | |
2508 | size_t offset = 7; | |
2509 | uint64_t initial_offset; | |
2510 | int32_t count; | |
2511 | uint32_t max_count; | |
2512 | V9fsPDU *pdu = opaque; | |
2513 | V9fsState *s = pdu->s; | |
2514 | ||
2515 | retval = pdu_unmarshal(pdu, offset, "dqd", &fid, | |
2516 | &initial_offset, &max_count); | |
2517 | if (retval < 0) { | |
2518 | goto out_nofid; | |
2519 | } | |
2520 | trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count); | |
2521 | ||
2522 | /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */ | |
2523 | if (max_count > s->msize - 11) { | |
2524 | max_count = s->msize - 11; | |
2525 | warn_report_once( | |
2526 | "9p: bad client: T_readdir with count > msize - 11" | |
2527 | ); | |
2528 | } | |
2529 | ||
2530 | fidp = get_fid(pdu, fid); | |
2531 | if (fidp == NULL) { | |
2532 | retval = -EINVAL; | |
2533 | goto out_nofid; | |
2534 | } | |
2535 | if (!fidp->fs.dir.stream) { | |
2536 | retval = -EINVAL; | |
2537 | goto out; | |
2538 | } | |
2539 | if (s->proto_version != V9FS_PROTO_2000L) { | |
2540 | warn_report_once( | |
2541 | "9p: bad client: T_readdir request only expected with 9P2000.L " | |
2542 | "protocol version" | |
2543 | ); | |
2544 | retval = -EOPNOTSUPP; | |
2545 | goto out; | |
2546 | } | |
2547 | count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count); | |
2548 | if (count < 0) { | |
2549 | retval = count; | |
2550 | goto out; | |
2551 | } | |
2552 | retval = pdu_marshal(pdu, offset, "d", count); | |
2553 | if (retval < 0) { | |
2554 | goto out; | |
2555 | } | |
2556 | retval += count + offset; | |
2557 | trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval); | |
2558 | out: | |
2559 | put_fid(pdu, fidp); | |
2560 | out_nofid: | |
2561 | pdu_complete(pdu, retval); | |
2562 | } | |
2563 | ||
2564 | static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, | |
2565 | uint64_t off, uint32_t count, | |
2566 | struct iovec *sg, int cnt) | |
2567 | { | |
2568 | int i, to_copy; | |
2569 | ssize_t err = 0; | |
2570 | uint64_t write_count; | |
2571 | size_t offset = 7; | |
2572 | ||
2573 | ||
2574 | if (fidp->fs.xattr.len < off) { | |
2575 | return -ENOSPC; | |
2576 | } | |
2577 | write_count = fidp->fs.xattr.len - off; | |
2578 | if (write_count > count) { | |
2579 | write_count = count; | |
2580 | } | |
2581 | err = pdu_marshal(pdu, offset, "d", write_count); | |
2582 | if (err < 0) { | |
2583 | return err; | |
2584 | } | |
2585 | err += offset; | |
2586 | fidp->fs.xattr.copied_len += write_count; | |
2587 | /* | |
2588 | * Now copy the content from sg list | |
2589 | */ | |
2590 | for (i = 0; i < cnt; i++) { | |
2591 | if (write_count > sg[i].iov_len) { | |
2592 | to_copy = sg[i].iov_len; | |
2593 | } else { | |
2594 | to_copy = write_count; | |
2595 | } | |
2596 | memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy); | |
2597 | /* updating vs->off since we are not using below */ | |
2598 | off += to_copy; | |
2599 | write_count -= to_copy; | |
2600 | } | |
2601 | ||
2602 | return err; | |
2603 | } | |
2604 | ||
2605 | static void coroutine_fn v9fs_write(void *opaque) | |
2606 | { | |
2607 | ssize_t err; | |
2608 | int32_t fid; | |
2609 | uint64_t off; | |
2610 | uint32_t count; | |
2611 | int32_t len = 0; | |
2612 | int32_t total = 0; | |
2613 | size_t offset = 7; | |
2614 | V9fsFidState *fidp; | |
2615 | V9fsPDU *pdu = opaque; | |
2616 | V9fsState *s = pdu->s; | |
2617 | QEMUIOVector qiov_full; | |
2618 | QEMUIOVector qiov; | |
2619 | ||
2620 | err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count); | |
2621 | if (err < 0) { | |
2622 | pdu_complete(pdu, err); | |
2623 | return; | |
2624 | } | |
2625 | offset += err; | |
2626 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true); | |
2627 | trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov); | |
2628 | ||
2629 | fidp = get_fid(pdu, fid); | |
2630 | if (fidp == NULL) { | |
2631 | err = -EINVAL; | |
2632 | goto out_nofid; | |
2633 | } | |
2634 | if (fidp->fid_type == P9_FID_FILE) { | |
2635 | if (fidp->fs.fd == -1) { | |
2636 | err = -EINVAL; | |
2637 | goto out; | |
2638 | } | |
2639 | } else if (fidp->fid_type == P9_FID_XATTR) { | |
2640 | /* | |
2641 | * setxattr operation | |
2642 | */ | |
2643 | err = v9fs_xattr_write(s, pdu, fidp, off, count, | |
2644 | qiov_full.iov, qiov_full.niov); | |
2645 | goto out; | |
2646 | } else { | |
2647 | err = -EINVAL; | |
2648 | goto out; | |
2649 | } | |
2650 | qemu_iovec_init(&qiov, qiov_full.niov); | |
2651 | do { | |
2652 | qemu_iovec_reset(&qiov); | |
2653 | qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total); | |
2654 | if (0) { | |
2655 | print_sg(qiov.iov, qiov.niov); | |
2656 | } | |
2657 | /* Loop in case of EINTR */ | |
2658 | do { | |
2659 | len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off); | |
2660 | if (len >= 0) { | |
2661 | off += len; | |
2662 | total += len; | |
2663 | } | |
2664 | } while (len == -EINTR && !pdu->cancelled); | |
2665 | if (len < 0) { | |
2666 | /* IO error return the error */ | |
2667 | err = len; | |
2668 | goto out_qiov; | |
2669 | } | |
2670 | } while (total < count && len > 0); | |
2671 | ||
2672 | offset = 7; | |
2673 | err = pdu_marshal(pdu, offset, "d", total); | |
2674 | if (err < 0) { | |
2675 | goto out_qiov; | |
2676 | } | |
2677 | err += offset; | |
2678 | trace_v9fs_write_return(pdu->tag, pdu->id, total, err); | |
2679 | out_qiov: | |
2680 | qemu_iovec_destroy(&qiov); | |
2681 | out: | |
2682 | put_fid(pdu, fidp); | |
2683 | out_nofid: | |
2684 | qemu_iovec_destroy(&qiov_full); | |
2685 | pdu_complete(pdu, err); | |
2686 | } | |
2687 | ||
2688 | static void coroutine_fn v9fs_create(void *opaque) | |
2689 | { | |
2690 | int32_t fid; | |
2691 | int err = 0; | |
2692 | size_t offset = 7; | |
2693 | V9fsFidState *fidp; | |
2694 | V9fsQID qid; | |
2695 | int32_t perm; | |
2696 | int8_t mode; | |
2697 | V9fsPath path; | |
2698 | struct stat stbuf; | |
2699 | V9fsString name; | |
2700 | V9fsString extension; | |
2701 | int iounit; | |
2702 | V9fsPDU *pdu = opaque; | |
2703 | V9fsState *s = pdu->s; | |
2704 | ||
2705 | v9fs_path_init(&path); | |
2706 | v9fs_string_init(&name); | |
2707 | v9fs_string_init(&extension); | |
2708 | err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, | |
2709 | &perm, &mode, &extension); | |
2710 | if (err < 0) { | |
2711 | goto out_nofid; | |
2712 | } | |
2713 | trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode); | |
2714 | ||
2715 | if (name_is_illegal(name.data)) { | |
2716 | err = -ENOENT; | |
2717 | goto out_nofid; | |
2718 | } | |
2719 | ||
2720 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { | |
2721 | err = -EEXIST; | |
2722 | goto out_nofid; | |
2723 | } | |
2724 | ||
2725 | fidp = get_fid(pdu, fid); | |
2726 | if (fidp == NULL) { | |
2727 | err = -EINVAL; | |
2728 | goto out_nofid; | |
2729 | } | |
2730 | if (fidp->fid_type != P9_FID_NONE) { | |
2731 | err = -EINVAL; | |
2732 | goto out; | |
2733 | } | |
2734 | if (perm & P9_STAT_MODE_DIR) { | |
2735 | err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777, | |
2736 | fidp->uid, -1, &stbuf); | |
2737 | if (err < 0) { | |
2738 | goto out; | |
2739 | } | |
2740 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); | |
2741 | if (err < 0) { | |
2742 | goto out; | |
2743 | } | |
2744 | v9fs_path_write_lock(s); | |
2745 | v9fs_path_copy(&fidp->path, &path); | |
2746 | v9fs_path_unlock(s); | |
2747 | err = v9fs_co_opendir(pdu, fidp); | |
2748 | if (err < 0) { | |
2749 | goto out; | |
2750 | } | |
2751 | fidp->fid_type = P9_FID_DIR; | |
2752 | } else if (perm & P9_STAT_MODE_SYMLINK) { | |
2753 | err = v9fs_co_symlink(pdu, fidp, &name, | |
2754 | extension.data, -1 , &stbuf); | |
2755 | if (err < 0) { | |
2756 | goto out; | |
2757 | } | |
2758 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); | |
2759 | if (err < 0) { | |
2760 | goto out; | |
2761 | } | |
2762 | v9fs_path_write_lock(s); | |
2763 | v9fs_path_copy(&fidp->path, &path); | |
2764 | v9fs_path_unlock(s); | |
2765 | } else if (perm & P9_STAT_MODE_LINK) { | |
2766 | int32_t ofid = atoi(extension.data); | |
2767 | V9fsFidState *ofidp = get_fid(pdu, ofid); | |
2768 | if (ofidp == NULL) { | |
2769 | err = -EINVAL; | |
2770 | goto out; | |
2771 | } | |
2772 | err = v9fs_co_link(pdu, ofidp, fidp, &name); | |
2773 | put_fid(pdu, ofidp); | |
2774 | if (err < 0) { | |
2775 | goto out; | |
2776 | } | |
2777 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); | |
2778 | if (err < 0) { | |
2779 | fidp->fid_type = P9_FID_NONE; | |
2780 | goto out; | |
2781 | } | |
2782 | v9fs_path_write_lock(s); | |
2783 | v9fs_path_copy(&fidp->path, &path); | |
2784 | v9fs_path_unlock(s); | |
2785 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); | |
2786 | if (err < 0) { | |
2787 | fidp->fid_type = P9_FID_NONE; | |
2788 | goto out; | |
2789 | } | |
2790 | } else if (perm & P9_STAT_MODE_DEVICE) { | |
2791 | char ctype; | |
2792 | uint32_t major, minor; | |
2793 | mode_t nmode = 0; | |
2794 | ||
2795 | if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) { | |
2796 | err = -errno; | |
2797 | goto out; | |
2798 | } | |
2799 | ||
2800 | switch (ctype) { | |
2801 | case 'c': | |
2802 | nmode = S_IFCHR; | |
2803 | break; | |
2804 | case 'b': | |
2805 | nmode = S_IFBLK; | |
2806 | break; | |
2807 | default: | |
2808 | err = -EIO; | |
2809 | goto out; | |
2810 | } | |
2811 | ||
2812 | nmode |= perm & 0777; | |
2813 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, | |
2814 | makedev(major, minor), nmode, &stbuf); | |
2815 | if (err < 0) { | |
2816 | goto out; | |
2817 | } | |
2818 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); | |
2819 | if (err < 0) { | |
2820 | goto out; | |
2821 | } | |
2822 | v9fs_path_write_lock(s); | |
2823 | v9fs_path_copy(&fidp->path, &path); | |
2824 | v9fs_path_unlock(s); | |
2825 | } else if (perm & P9_STAT_MODE_NAMED_PIPE) { | |
2826 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, | |
2827 | 0, S_IFIFO | (perm & 0777), &stbuf); | |
2828 | if (err < 0) { | |
2829 | goto out; | |
2830 | } | |
2831 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); | |
2832 | if (err < 0) { | |
2833 | goto out; | |
2834 | } | |
2835 | v9fs_path_write_lock(s); | |
2836 | v9fs_path_copy(&fidp->path, &path); | |
2837 | v9fs_path_unlock(s); | |
2838 | } else if (perm & P9_STAT_MODE_SOCKET) { | |
2839 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, | |
2840 | 0, S_IFSOCK | (perm & 0777), &stbuf); | |
2841 | if (err < 0) { | |
2842 | goto out; | |
2843 | } | |
2844 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); | |
2845 | if (err < 0) { | |
2846 | goto out; | |
2847 | } | |
2848 | v9fs_path_write_lock(s); | |
2849 | v9fs_path_copy(&fidp->path, &path); | |
2850 | v9fs_path_unlock(s); | |
2851 | } else { | |
2852 | err = v9fs_co_open2(pdu, fidp, &name, -1, | |
2853 | omode_to_uflags(mode) | O_CREAT, perm, &stbuf); | |
2854 | if (err < 0) { | |
2855 | goto out; | |
2856 | } | |
2857 | fidp->fid_type = P9_FID_FILE; | |
2858 | fidp->open_flags = omode_to_uflags(mode); | |
2859 | if (fidp->open_flags & O_EXCL) { | |
2860 | /* | |
2861 | * We let the host file system do O_EXCL check | |
2862 | * We should not reclaim such fd | |
2863 | */ | |
2864 | fidp->flags |= FID_NON_RECLAIMABLE; | |
2865 | } | |
2866 | } | |
2867 | iounit = get_iounit(pdu, &fidp->path); | |
2868 | err = stat_to_qid(pdu, &stbuf, &qid); | |
2869 | if (err < 0) { | |
2870 | goto out; | |
2871 | } | |
2872 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); | |
2873 | if (err < 0) { | |
2874 | goto out; | |
2875 | } | |
2876 | err += offset; | |
2877 | trace_v9fs_create_return(pdu->tag, pdu->id, | |
2878 | qid.type, qid.version, qid.path, iounit); | |
2879 | out: | |
2880 | put_fid(pdu, fidp); | |
2881 | out_nofid: | |
2882 | pdu_complete(pdu, err); | |
2883 | v9fs_string_free(&name); | |
2884 | v9fs_string_free(&extension); | |
2885 | v9fs_path_free(&path); | |
2886 | } | |
2887 | ||
2888 | static void coroutine_fn v9fs_symlink(void *opaque) | |
2889 | { | |
2890 | V9fsPDU *pdu = opaque; | |
2891 | V9fsString name; | |
2892 | V9fsString symname; | |
2893 | V9fsFidState *dfidp; | |
2894 | V9fsQID qid; | |
2895 | struct stat stbuf; | |
2896 | int32_t dfid; | |
2897 | int err = 0; | |
2898 | gid_t gid; | |
2899 | size_t offset = 7; | |
2900 | ||
2901 | v9fs_string_init(&name); | |
2902 | v9fs_string_init(&symname); | |
2903 | err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); | |
2904 | if (err < 0) { | |
2905 | goto out_nofid; | |
2906 | } | |
2907 | trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid); | |
2908 | ||
2909 | if (name_is_illegal(name.data)) { | |
2910 | err = -ENOENT; | |
2911 | goto out_nofid; | |
2912 | } | |
2913 | ||
2914 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { | |
2915 | err = -EEXIST; | |
2916 | goto out_nofid; | |
2917 | } | |
2918 | ||
2919 | dfidp = get_fid(pdu, dfid); | |
2920 | if (dfidp == NULL) { | |
2921 | err = -EINVAL; | |
2922 | goto out_nofid; | |
2923 | } | |
2924 | err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf); | |
2925 | if (err < 0) { | |
2926 | goto out; | |
2927 | } | |
2928 | err = stat_to_qid(pdu, &stbuf, &qid); | |
2929 | if (err < 0) { | |
2930 | goto out; | |
2931 | } | |
2932 | err = pdu_marshal(pdu, offset, "Q", &qid); | |
2933 | if (err < 0) { | |
2934 | goto out; | |
2935 | } | |
2936 | err += offset; | |
2937 | trace_v9fs_symlink_return(pdu->tag, pdu->id, | |
2938 | qid.type, qid.version, qid.path); | |
2939 | out: | |
2940 | put_fid(pdu, dfidp); | |
2941 | out_nofid: | |
2942 | pdu_complete(pdu, err); | |
2943 | v9fs_string_free(&name); | |
2944 | v9fs_string_free(&symname); | |
2945 | } | |
2946 | ||
2947 | static void coroutine_fn v9fs_flush(void *opaque) | |
2948 | { | |
2949 | ssize_t err; | |
2950 | int16_t tag; | |
2951 | size_t offset = 7; | |
2952 | V9fsPDU *cancel_pdu = NULL; | |
2953 | V9fsPDU *pdu = opaque; | |
2954 | V9fsState *s = pdu->s; | |
2955 | ||
2956 | err = pdu_unmarshal(pdu, offset, "w", &tag); | |
2957 | if (err < 0) { | |
2958 | pdu_complete(pdu, err); | |
2959 | return; | |
2960 | } | |
2961 | trace_v9fs_flush(pdu->tag, pdu->id, tag); | |
2962 | ||
2963 | if (pdu->tag == tag) { | |
2964 | warn_report("the guest sent a self-referencing 9P flush request"); | |
2965 | } else { | |
2966 | QLIST_FOREACH(cancel_pdu, &s->active_list, next) { | |
2967 | if (cancel_pdu->tag == tag) { | |
2968 | break; | |
2969 | } | |
2970 | } | |
2971 | } | |
2972 | if (cancel_pdu) { | |
2973 | cancel_pdu->cancelled = 1; | |
2974 | /* | |
2975 | * Wait for pdu to complete. | |
2976 | */ | |
2977 | qemu_co_queue_wait(&cancel_pdu->complete, NULL); | |
2978 | if (!qemu_co_queue_next(&cancel_pdu->complete)) { | |
2979 | cancel_pdu->cancelled = 0; | |
2980 | pdu_free(cancel_pdu); | |
2981 | } | |
2982 | } | |
2983 | pdu_complete(pdu, 7); | |
2984 | } | |
2985 | ||
2986 | static void coroutine_fn v9fs_link(void *opaque) | |
2987 | { | |
2988 | V9fsPDU *pdu = opaque; | |
2989 | int32_t dfid, oldfid; | |
2990 | V9fsFidState *dfidp, *oldfidp; | |
2991 | V9fsString name; | |
2992 | size_t offset = 7; | |
2993 | int err = 0; | |
2994 | ||
2995 | v9fs_string_init(&name); | |
2996 | err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); | |
2997 | if (err < 0) { | |
2998 | goto out_nofid; | |
2999 | } | |
3000 | trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data); | |
3001 | ||
3002 | if (name_is_illegal(name.data)) { | |
3003 | err = -ENOENT; | |
3004 | goto out_nofid; | |
3005 | } | |
3006 | ||
3007 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { | |
3008 | err = -EEXIST; | |
3009 | goto out_nofid; | |
3010 | } | |
3011 | ||
3012 | dfidp = get_fid(pdu, dfid); | |
3013 | if (dfidp == NULL) { | |
3014 | err = -ENOENT; | |
3015 | goto out_nofid; | |
3016 | } | |
3017 | ||
3018 | oldfidp = get_fid(pdu, oldfid); | |
3019 | if (oldfidp == NULL) { | |
3020 | err = -ENOENT; | |
3021 | goto out; | |
3022 | } | |
3023 | err = v9fs_co_link(pdu, oldfidp, dfidp, &name); | |
3024 | if (!err) { | |
3025 | err = offset; | |
3026 | } | |
3027 | put_fid(pdu, oldfidp); | |
3028 | out: | |
3029 | put_fid(pdu, dfidp); | |
3030 | out_nofid: | |
3031 | v9fs_string_free(&name); | |
3032 | pdu_complete(pdu, err); | |
3033 | } | |
3034 | ||
3035 | /* Only works with path name based fid */ | |
3036 | static void coroutine_fn v9fs_remove(void *opaque) | |
3037 | { | |
3038 | int32_t fid; | |
3039 | int err = 0; | |
3040 | size_t offset = 7; | |
3041 | V9fsFidState *fidp; | |
3042 | V9fsPDU *pdu = opaque; | |
3043 | ||
3044 | err = pdu_unmarshal(pdu, offset, "d", &fid); | |
3045 | if (err < 0) { | |
3046 | goto out_nofid; | |
3047 | } | |
3048 | trace_v9fs_remove(pdu->tag, pdu->id, fid); | |
3049 | ||
3050 | fidp = get_fid(pdu, fid); | |
3051 | if (fidp == NULL) { | |
3052 | err = -EINVAL; | |
3053 | goto out_nofid; | |
3054 | } | |
3055 | /* if fs driver is not path based, return EOPNOTSUPP */ | |
3056 | if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { | |
3057 | err = -EOPNOTSUPP; | |
3058 | goto out_err; | |
3059 | } | |
3060 | /* | |
3061 | * IF the file is unlinked, we cannot reopen | |
3062 | * the file later. So don't reclaim fd | |
3063 | */ | |
3064 | err = v9fs_mark_fids_unreclaim(pdu, &fidp->path); | |
3065 | if (err < 0) { | |
3066 | goto out_err; | |
3067 | } | |
3068 | err = v9fs_co_remove(pdu, &fidp->path); | |
3069 | if (!err) { | |
3070 | err = offset; | |
3071 | } | |
3072 | out_err: | |
3073 | /* For TREMOVE we need to clunk the fid even on failed remove */ | |
3074 | clunk_fid(pdu->s, fidp->fid); | |
3075 | put_fid(pdu, fidp); | |
3076 | out_nofid: | |
3077 | pdu_complete(pdu, err); | |
3078 | } | |
3079 | ||
3080 | static void coroutine_fn v9fs_unlinkat(void *opaque) | |
3081 | { | |
3082 | int err = 0; | |
3083 | V9fsString name; | |
3084 | int32_t dfid, flags, rflags = 0; | |
3085 | size_t offset = 7; | |
3086 | V9fsPath path; | |
3087 | V9fsFidState *dfidp; | |
3088 | V9fsPDU *pdu = opaque; | |
3089 | ||
3090 | v9fs_string_init(&name); | |
3091 | err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags); | |
3092 | if (err < 0) { | |
3093 | goto out_nofid; | |
3094 | } | |
3095 | ||
3096 | if (name_is_illegal(name.data)) { | |
3097 | err = -ENOENT; | |
3098 | goto out_nofid; | |
3099 | } | |
3100 | ||
3101 | if (!strcmp(".", name.data)) { | |
3102 | err = -EINVAL; | |
3103 | goto out_nofid; | |
3104 | } | |
3105 | ||
3106 | if (!strcmp("..", name.data)) { | |
3107 | err = -ENOTEMPTY; | |
3108 | goto out_nofid; | |
3109 | } | |
3110 | ||
3111 | if (flags & ~P9_DOTL_AT_REMOVEDIR) { | |
3112 | err = -EINVAL; | |
3113 | goto out_nofid; | |
3114 | } | |
3115 | ||
3116 | if (flags & P9_DOTL_AT_REMOVEDIR) { | |
3117 | rflags |= AT_REMOVEDIR; | |
3118 | } | |
3119 | ||
3120 | dfidp = get_fid(pdu, dfid); | |
3121 | if (dfidp == NULL) { | |
3122 | err = -EINVAL; | |
3123 | goto out_nofid; | |
3124 | } | |
3125 | /* | |
3126 | * IF the file is unlinked, we cannot reopen | |
3127 | * the file later. So don't reclaim fd | |
3128 | */ | |
3129 | v9fs_path_init(&path); | |
3130 | err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path); | |
3131 | if (err < 0) { | |
3132 | goto out_err; | |
3133 | } | |
3134 | err = v9fs_mark_fids_unreclaim(pdu, &path); | |
3135 | if (err < 0) { | |
3136 | goto out_err; | |
3137 | } | |
3138 | err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags); | |
3139 | if (!err) { | |
3140 | err = offset; | |
3141 | } | |
3142 | out_err: | |
3143 | put_fid(pdu, dfidp); | |
3144 | v9fs_path_free(&path); | |
3145 | out_nofid: | |
3146 | pdu_complete(pdu, err); | |
3147 | v9fs_string_free(&name); | |
3148 | } | |
3149 | ||
3150 | ||
3151 | /* Only works with path name based fid */ | |
3152 | static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp, | |
3153 | int32_t newdirfid, | |
3154 | V9fsString *name) | |
3155 | { | |
3156 | int err = 0; | |
3157 | V9fsPath new_path; | |
3158 | V9fsFidState *tfidp; | |
3159 | V9fsState *s = pdu->s; | |
3160 | V9fsFidState *dirfidp = NULL; | |
3161 | ||
3162 | v9fs_path_init(&new_path); | |
3163 | if (newdirfid != -1) { | |
3164 | dirfidp = get_fid(pdu, newdirfid); | |
3165 | if (dirfidp == NULL) { | |
3166 | return -ENOENT; | |
3167 | } | |
3168 | if (fidp->fid_type != P9_FID_NONE) { | |
3169 | err = -EINVAL; | |
3170 | goto out; | |
3171 | } | |
3172 | err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path); | |
3173 | if (err < 0) { | |
3174 | goto out; | |
3175 | } | |
3176 | } else { | |
3177 | char *dir_name = g_path_get_dirname(fidp->path.data); | |
3178 | V9fsPath dir_path; | |
3179 | ||
3180 | v9fs_path_init(&dir_path); | |
3181 | v9fs_path_sprintf(&dir_path, "%s", dir_name); | |
3182 | g_free(dir_name); | |
3183 | ||
3184 | err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path); | |
3185 | v9fs_path_free(&dir_path); | |
3186 | if (err < 0) { | |
3187 | goto out; | |
3188 | } | |
3189 | } | |
3190 | err = v9fs_co_rename(pdu, &fidp->path, &new_path); | |
3191 | if (err < 0) { | |
3192 | goto out; | |
3193 | } | |
3194 | /* | |
3195 | * Fixup fid's pointing to the old name to | |
3196 | * start pointing to the new name | |
3197 | */ | |
3198 | QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { | |
3199 | if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { | |
3200 | /* replace the name */ | |
3201 | v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)); | |
3202 | } | |
3203 | } | |
3204 | out: | |
3205 | if (dirfidp) { | |
3206 | put_fid(pdu, dirfidp); | |
3207 | } | |
3208 | v9fs_path_free(&new_path); | |
3209 | return err; | |
3210 | } | |
3211 | ||
3212 | /* Only works with path name based fid */ | |
3213 | static void coroutine_fn v9fs_rename(void *opaque) | |
3214 | { | |
3215 | int32_t fid; | |
3216 | ssize_t err = 0; | |
3217 | size_t offset = 7; | |
3218 | V9fsString name; | |
3219 | int32_t newdirfid; | |
3220 | V9fsFidState *fidp; | |
3221 | V9fsPDU *pdu = opaque; | |
3222 | V9fsState *s = pdu->s; | |
3223 | ||
3224 | v9fs_string_init(&name); | |
3225 | err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); | |
3226 | if (err < 0) { | |
3227 | goto out_nofid; | |
3228 | } | |
3229 | ||
3230 | if (name_is_illegal(name.data)) { | |
3231 | err = -ENOENT; | |
3232 | goto out_nofid; | |
3233 | } | |
3234 | ||
3235 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { | |
3236 | err = -EISDIR; | |
3237 | goto out_nofid; | |
3238 | } | |
3239 | ||
3240 | fidp = get_fid(pdu, fid); | |
3241 | if (fidp == NULL) { | |
3242 | err = -ENOENT; | |
3243 | goto out_nofid; | |
3244 | } | |
3245 | if (fidp->fid_type != P9_FID_NONE) { | |
3246 | err = -EINVAL; | |
3247 | goto out; | |
3248 | } | |
3249 | /* if fs driver is not path based, return EOPNOTSUPP */ | |
3250 | if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { | |
3251 | err = -EOPNOTSUPP; | |
3252 | goto out; | |
3253 | } | |
3254 | v9fs_path_write_lock(s); | |
3255 | err = v9fs_complete_rename(pdu, fidp, newdirfid, &name); | |
3256 | v9fs_path_unlock(s); | |
3257 | if (!err) { | |
3258 | err = offset; | |
3259 | } | |
3260 | out: | |
3261 | put_fid(pdu, fidp); | |
3262 | out_nofid: | |
3263 | pdu_complete(pdu, err); | |
3264 | v9fs_string_free(&name); | |
3265 | } | |
3266 | ||
3267 | static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir, | |
3268 | V9fsString *old_name, | |
3269 | V9fsPath *newdir, | |
3270 | V9fsString *new_name) | |
3271 | { | |
3272 | V9fsFidState *tfidp; | |
3273 | V9fsPath oldpath, newpath; | |
3274 | V9fsState *s = pdu->s; | |
3275 | int err; | |
3276 | ||
3277 | v9fs_path_init(&oldpath); | |
3278 | v9fs_path_init(&newpath); | |
3279 | err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath); | |
3280 | if (err < 0) { | |
3281 | goto out; | |
3282 | } | |
3283 | err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath); | |
3284 | if (err < 0) { | |
3285 | goto out; | |
3286 | } | |
3287 | ||
3288 | /* | |
3289 | * Fixup fid's pointing to the old name to | |
3290 | * start pointing to the new name | |
3291 | */ | |
3292 | QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { | |
3293 | if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { | |
3294 | /* replace the name */ | |
3295 | v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); | |
3296 | } | |
3297 | } | |
3298 | out: | |
3299 | v9fs_path_free(&oldpath); | |
3300 | v9fs_path_free(&newpath); | |
3301 | return err; | |
3302 | } | |
3303 | ||
3304 | static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid, | |
3305 | V9fsString *old_name, | |
3306 | int32_t newdirfid, | |
3307 | V9fsString *new_name) | |
3308 | { | |
3309 | int err = 0; | |
3310 | V9fsState *s = pdu->s; | |
3311 | V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL; | |
3312 | ||
3313 | olddirfidp = get_fid(pdu, olddirfid); | |
3314 | if (olddirfidp == NULL) { | |
3315 | err = -ENOENT; | |
3316 | goto out; | |
3317 | } | |
3318 | if (newdirfid != -1) { | |
3319 | newdirfidp = get_fid(pdu, newdirfid); | |
3320 | if (newdirfidp == NULL) { | |
3321 | err = -ENOENT; | |
3322 | goto out; | |
3323 | } | |
3324 | } else { | |
3325 | newdirfidp = get_fid(pdu, olddirfid); | |
3326 | } | |
3327 | ||
3328 | err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name, | |
3329 | &newdirfidp->path, new_name); | |
3330 | if (err < 0) { | |
3331 | goto out; | |
3332 | } | |
3333 | if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { | |
3334 | /* Only for path based fid we need to do the below fixup */ | |
3335 | err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name, | |
3336 | &newdirfidp->path, new_name); | |
3337 | } | |
3338 | out: | |
3339 | if (olddirfidp) { | |
3340 | put_fid(pdu, olddirfidp); | |
3341 | } | |
3342 | if (newdirfidp) { | |
3343 | put_fid(pdu, newdirfidp); | |
3344 | } | |
3345 | return err; | |
3346 | } | |
3347 | ||
3348 | static void coroutine_fn v9fs_renameat(void *opaque) | |
3349 | { | |
3350 | ssize_t err = 0; | |
3351 | size_t offset = 7; | |
3352 | V9fsPDU *pdu = opaque; | |
3353 | V9fsState *s = pdu->s; | |
3354 | int32_t olddirfid, newdirfid; | |
3355 | V9fsString old_name, new_name; | |
3356 | ||
3357 | v9fs_string_init(&old_name); | |
3358 | v9fs_string_init(&new_name); | |
3359 | err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid, | |
3360 | &old_name, &newdirfid, &new_name); | |
3361 | if (err < 0) { | |
3362 | goto out_err; | |
3363 | } | |
3364 | ||
3365 | if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) { | |
3366 | err = -ENOENT; | |
3367 | goto out_err; | |
3368 | } | |
3369 | ||
3370 | if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) || | |
3371 | !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) { | |
3372 | err = -EISDIR; | |
3373 | goto out_err; | |
3374 | } | |
3375 | ||
3376 | v9fs_path_write_lock(s); | |
3377 | err = v9fs_complete_renameat(pdu, olddirfid, | |
3378 | &old_name, newdirfid, &new_name); | |
3379 | v9fs_path_unlock(s); | |
3380 | if (!err) { | |
3381 | err = offset; | |
3382 | } | |
3383 | ||
3384 | out_err: | |
3385 | pdu_complete(pdu, err); | |
3386 | v9fs_string_free(&old_name); | |
3387 | v9fs_string_free(&new_name); | |
3388 | } | |
3389 | ||
3390 | static void coroutine_fn v9fs_wstat(void *opaque) | |
3391 | { | |
3392 | int32_t fid; | |
3393 | int err = 0; | |
3394 | int16_t unused; | |
3395 | V9fsStat v9stat; | |
3396 | size_t offset = 7; | |
3397 | struct stat stbuf; | |
3398 | V9fsFidState *fidp; | |
3399 | V9fsPDU *pdu = opaque; | |
3400 | V9fsState *s = pdu->s; | |
3401 | ||
3402 | v9fs_stat_init(&v9stat); | |
3403 | err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); | |
3404 | if (err < 0) { | |
3405 | goto out_nofid; | |
3406 | } | |
3407 | trace_v9fs_wstat(pdu->tag, pdu->id, fid, | |
3408 | v9stat.mode, v9stat.atime, v9stat.mtime); | |
3409 | ||
3410 | fidp = get_fid(pdu, fid); | |
3411 | if (fidp == NULL) { | |
3412 | err = -EINVAL; | |
3413 | goto out_nofid; | |
3414 | } | |
3415 | /* do we need to sync the file? */ | |
3416 | if (donttouch_stat(&v9stat)) { | |
3417 | err = v9fs_co_fsync(pdu, fidp, 0); | |
3418 | goto out; | |
3419 | } | |
3420 | if (v9stat.mode != -1) { | |
3421 | uint32_t v9_mode; | |
3422 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); | |
3423 | if (err < 0) { | |
3424 | goto out; | |
3425 | } | |
3426 | v9_mode = stat_to_v9mode(&stbuf); | |
3427 | if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) != | |
3428 | (v9_mode & P9_STAT_MODE_TYPE_BITS)) { | |
3429 | /* Attempting to change the type */ | |
3430 | err = -EIO; | |
3431 | goto out; | |
3432 | } | |
3433 | err = v9fs_co_chmod(pdu, &fidp->path, | |
3434 | v9mode_to_mode(v9stat.mode, | |
3435 | &v9stat.extension)); | |
3436 | if (err < 0) { | |
3437 | goto out; | |
3438 | } | |
3439 | } | |
3440 | if (v9stat.mtime != -1 || v9stat.atime != -1) { | |
3441 | struct timespec times[2]; | |
3442 | if (v9stat.atime != -1) { | |
3443 | times[0].tv_sec = v9stat.atime; | |
3444 | times[0].tv_nsec = 0; | |
3445 | } else { | |
3446 | times[0].tv_nsec = UTIME_OMIT; | |
3447 | } | |
3448 | if (v9stat.mtime != -1) { | |
3449 | times[1].tv_sec = v9stat.mtime; | |
3450 | times[1].tv_nsec = 0; | |
3451 | } else { | |
3452 | times[1].tv_nsec = UTIME_OMIT; | |
3453 | } | |
3454 | err = v9fs_co_utimensat(pdu, &fidp->path, times); | |
3455 | if (err < 0) { | |
3456 | goto out; | |
3457 | } | |
3458 | } | |
3459 | if (v9stat.n_gid != -1 || v9stat.n_uid != -1) { | |
3460 | err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid); | |
3461 | if (err < 0) { | |
3462 | goto out; | |
3463 | } | |
3464 | } | |
3465 | if (v9stat.name.size != 0) { | |
3466 | v9fs_path_write_lock(s); | |
3467 | err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name); | |
3468 | v9fs_path_unlock(s); | |
3469 | if (err < 0) { | |
3470 | goto out; | |
3471 | } | |
3472 | } | |
3473 | if (v9stat.length != -1) { | |
3474 | err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length); | |
3475 | if (err < 0) { | |
3476 | goto out; | |
3477 | } | |
3478 | } | |
3479 | err = offset; | |
3480 | out: | |
3481 | put_fid(pdu, fidp); | |
3482 | out_nofid: | |
3483 | v9fs_stat_free(&v9stat); | |
3484 | pdu_complete(pdu, err); | |
3485 | } | |
3486 | ||
3487 | static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) | |
3488 | { | |
3489 | uint32_t f_type; | |
3490 | uint32_t f_bsize; | |
3491 | uint64_t f_blocks; | |
3492 | uint64_t f_bfree; | |
3493 | uint64_t f_bavail; | |
3494 | uint64_t f_files; | |
3495 | uint64_t f_ffree; | |
3496 | uint64_t fsid_val; | |
3497 | uint32_t f_namelen; | |
3498 | size_t offset = 7; | |
3499 | int32_t bsize_factor; | |
3500 | ||
3501 | /* | |
3502 | * compute bsize factor based on host file system block size | |
3503 | * and client msize | |
3504 | */ | |
3505 | bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize; | |
3506 | if (!bsize_factor) { | |
3507 | bsize_factor = 1; | |
3508 | } | |
3509 | f_type = stbuf->f_type; | |
3510 | f_bsize = stbuf->f_bsize; | |
3511 | f_bsize *= bsize_factor; | |
3512 | /* | |
3513 | * f_bsize is adjusted(multiplied) by bsize factor, so we need to | |
3514 | * adjust(divide) the number of blocks, free blocks and available | |
3515 | * blocks by bsize factor | |
3516 | */ | |
3517 | f_blocks = stbuf->f_blocks / bsize_factor; | |
3518 | f_bfree = stbuf->f_bfree / bsize_factor; | |
3519 | f_bavail = stbuf->f_bavail / bsize_factor; | |
3520 | f_files = stbuf->f_files; | |
3521 | f_ffree = stbuf->f_ffree; | |
3522 | fsid_val = (unsigned int) stbuf->f_fsid.__val[0] | | |
3523 | (unsigned long long)stbuf->f_fsid.__val[1] << 32; | |
3524 | f_namelen = stbuf->f_namelen; | |
3525 | ||
3526 | return pdu_marshal(pdu, offset, "ddqqqqqqd", | |
3527 | f_type, f_bsize, f_blocks, f_bfree, | |
3528 | f_bavail, f_files, f_ffree, | |
3529 | fsid_val, f_namelen); | |
3530 | } | |
3531 | ||
3532 | static void coroutine_fn v9fs_statfs(void *opaque) | |
3533 | { | |
3534 | int32_t fid; | |
3535 | ssize_t retval = 0; | |
3536 | size_t offset = 7; | |
3537 | V9fsFidState *fidp; | |
3538 | struct statfs stbuf; | |
3539 | V9fsPDU *pdu = opaque; | |
3540 | V9fsState *s = pdu->s; | |
3541 | ||
3542 | retval = pdu_unmarshal(pdu, offset, "d", &fid); | |
3543 | if (retval < 0) { | |
3544 | goto out_nofid; | |
3545 | } | |
3546 | fidp = get_fid(pdu, fid); | |
3547 | if (fidp == NULL) { | |
3548 | retval = -ENOENT; | |
3549 | goto out_nofid; | |
3550 | } | |
3551 | retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf); | |
3552 | if (retval < 0) { | |
3553 | goto out; | |
3554 | } | |
3555 | retval = v9fs_fill_statfs(s, pdu, &stbuf); | |
3556 | if (retval < 0) { | |
3557 | goto out; | |
3558 | } | |
3559 | retval += offset; | |
3560 | out: | |
3561 | put_fid(pdu, fidp); | |
3562 | out_nofid: | |
3563 | pdu_complete(pdu, retval); | |
3564 | } | |
3565 | ||
3566 | static void coroutine_fn v9fs_mknod(void *opaque) | |
3567 | { | |
3568 | ||
3569 | int mode; | |
3570 | gid_t gid; | |
3571 | int32_t fid; | |
3572 | V9fsQID qid; | |
3573 | int err = 0; | |
3574 | int major, minor; | |
3575 | size_t offset = 7; | |
3576 | V9fsString name; | |
3577 | struct stat stbuf; | |
3578 | V9fsFidState *fidp; | |
3579 | V9fsPDU *pdu = opaque; | |
3580 | ||
3581 | v9fs_string_init(&name); | |
3582 | err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, | |
3583 | &major, &minor, &gid); | |
3584 | if (err < 0) { | |
3585 | goto out_nofid; | |
3586 | } | |
3587 | trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor); | |
3588 | ||
3589 | if (name_is_illegal(name.data)) { | |
3590 | err = -ENOENT; | |
3591 | goto out_nofid; | |
3592 | } | |
3593 | ||
3594 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { | |
3595 | err = -EEXIST; | |
3596 | goto out_nofid; | |
3597 | } | |
3598 | ||
3599 | fidp = get_fid(pdu, fid); | |
3600 | if (fidp == NULL) { | |
3601 | err = -ENOENT; | |
3602 | goto out_nofid; | |
3603 | } | |
3604 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid, | |
3605 | makedev(major, minor), mode, &stbuf); | |
3606 | if (err < 0) { | |
3607 | goto out; | |
3608 | } | |
3609 | err = stat_to_qid(pdu, &stbuf, &qid); | |
3610 | if (err < 0) { | |
3611 | goto out; | |
3612 | } | |
3613 | err = pdu_marshal(pdu, offset, "Q", &qid); | |
3614 | if (err < 0) { | |
3615 | goto out; | |
3616 | } | |
3617 | err += offset; | |
3618 | trace_v9fs_mknod_return(pdu->tag, pdu->id, | |
3619 | qid.type, qid.version, qid.path); | |
3620 | out: | |
3621 | put_fid(pdu, fidp); | |
3622 | out_nofid: | |
3623 | pdu_complete(pdu, err); | |
3624 | v9fs_string_free(&name); | |
3625 | } | |
3626 | ||
3627 | /* | |
3628 | * Implement posix byte range locking code | |
3629 | * Server side handling of locking code is very simple, because 9p server in | |
3630 | * QEMU can handle only one client. And most of the lock handling | |
3631 | * (like conflict, merging) etc is done by the VFS layer itself, so no need to | |
3632 | * do any thing in * qemu 9p server side lock code path. | |
3633 | * So when a TLOCK request comes, always return success | |
3634 | */ | |
3635 | static void coroutine_fn v9fs_lock(void *opaque) | |
3636 | { | |
3637 | V9fsFlock flock; | |
3638 | size_t offset = 7; | |
3639 | struct stat stbuf; | |
3640 | V9fsFidState *fidp; | |
3641 | int32_t fid, err = 0; | |
3642 | V9fsPDU *pdu = opaque; | |
3643 | ||
3644 | v9fs_string_init(&flock.client_id); | |
3645 | err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type, | |
3646 | &flock.flags, &flock.start, &flock.length, | |
3647 | &flock.proc_id, &flock.client_id); | |
3648 | if (err < 0) { | |
3649 | goto out_nofid; | |
3650 | } | |
3651 | trace_v9fs_lock(pdu->tag, pdu->id, fid, | |
3652 | flock.type, flock.start, flock.length); | |
3653 | ||
3654 | ||
3655 | /* We support only block flag now (that too ignored currently) */ | |
3656 | if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) { | |
3657 | err = -EINVAL; | |
3658 | goto out_nofid; | |
3659 | } | |
3660 | fidp = get_fid(pdu, fid); | |
3661 | if (fidp == NULL) { | |
3662 | err = -ENOENT; | |
3663 | goto out_nofid; | |
3664 | } | |
3665 | err = v9fs_co_fstat(pdu, fidp, &stbuf); | |
3666 | if (err < 0) { | |
3667 | goto out; | |
3668 | } | |
3669 | err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS); | |
3670 | if (err < 0) { | |
3671 | goto out; | |
3672 | } | |
3673 | err += offset; | |
3674 | trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS); | |
3675 | out: | |
3676 | put_fid(pdu, fidp); | |
3677 | out_nofid: | |
3678 | pdu_complete(pdu, err); | |
3679 | v9fs_string_free(&flock.client_id); | |
3680 | } | |
3681 | ||
3682 | /* | |
3683 | * When a TGETLOCK request comes, always return success because all lock | |
3684 | * handling is done by client's VFS layer. | |
3685 | */ | |
3686 | static void coroutine_fn v9fs_getlock(void *opaque) | |
3687 | { | |
3688 | size_t offset = 7; | |
3689 | struct stat stbuf; | |
3690 | V9fsFidState *fidp; | |
3691 | V9fsGetlock glock; | |
3692 | int32_t fid, err = 0; | |
3693 | V9fsPDU *pdu = opaque; | |
3694 | ||
3695 | v9fs_string_init(&glock.client_id); | |
3696 | err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type, | |
3697 | &glock.start, &glock.length, &glock.proc_id, | |
3698 | &glock.client_id); | |
3699 | if (err < 0) { | |
3700 | goto out_nofid; | |
3701 | } | |
3702 | trace_v9fs_getlock(pdu->tag, pdu->id, fid, | |
3703 | glock.type, glock.start, glock.length); | |
3704 | ||
3705 | fidp = get_fid(pdu, fid); | |
3706 | if (fidp == NULL) { | |
3707 | err = -ENOENT; | |
3708 | goto out_nofid; | |
3709 | } | |
3710 | err = v9fs_co_fstat(pdu, fidp, &stbuf); | |
3711 | if (err < 0) { | |
3712 | goto out; | |
3713 | } | |
3714 | glock.type = P9_LOCK_TYPE_UNLCK; | |
3715 | err = pdu_marshal(pdu, offset, "bqqds", glock.type, | |
3716 | glock.start, glock.length, glock.proc_id, | |
3717 | &glock.client_id); | |
3718 | if (err < 0) { | |
3719 | goto out; | |
3720 | } | |
3721 | err += offset; | |
3722 | trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start, | |
3723 | glock.length, glock.proc_id); | |
3724 | out: | |
3725 | put_fid(pdu, fidp); | |
3726 | out_nofid: | |
3727 | pdu_complete(pdu, err); | |
3728 | v9fs_string_free(&glock.client_id); | |
3729 | } | |
3730 | ||
3731 | static void coroutine_fn v9fs_mkdir(void *opaque) | |
3732 | { | |
3733 | V9fsPDU *pdu = opaque; | |
3734 | size_t offset = 7; | |
3735 | int32_t fid; | |
3736 | struct stat stbuf; | |
3737 | V9fsQID qid; | |
3738 | V9fsString name; | |
3739 | V9fsFidState *fidp; | |
3740 | gid_t gid; | |
3741 | int mode; | |
3742 | int err = 0; | |
3743 | ||
3744 | v9fs_string_init(&name); | |
3745 | err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); | |
3746 | if (err < 0) { | |
3747 | goto out_nofid; | |
3748 | } | |
3749 | trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid); | |
3750 | ||
3751 | if (name_is_illegal(name.data)) { | |
3752 | err = -ENOENT; | |
3753 | goto out_nofid; | |
3754 | } | |
3755 | ||
3756 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { | |
3757 | err = -EEXIST; | |
3758 | goto out_nofid; | |
3759 | } | |
3760 | ||
3761 | fidp = get_fid(pdu, fid); | |
3762 | if (fidp == NULL) { | |
3763 | err = -ENOENT; | |
3764 | goto out_nofid; | |
3765 | } | |
3766 | err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf); | |
3767 | if (err < 0) { | |
3768 | goto out; | |
3769 | } | |
3770 | err = stat_to_qid(pdu, &stbuf, &qid); | |
3771 | if (err < 0) { | |
3772 | goto out; | |
3773 | } | |
3774 | err = pdu_marshal(pdu, offset, "Q", &qid); | |
3775 | if (err < 0) { | |
3776 | goto out; | |
3777 | } | |
3778 | err += offset; | |
3779 | trace_v9fs_mkdir_return(pdu->tag, pdu->id, | |
3780 | qid.type, qid.version, qid.path, err); | |
3781 | out: | |
3782 | put_fid(pdu, fidp); | |
3783 | out_nofid: | |
3784 | pdu_complete(pdu, err); | |
3785 | v9fs_string_free(&name); | |
3786 | } | |
3787 | ||
3788 | static void coroutine_fn v9fs_xattrwalk(void *opaque) | |
3789 | { | |
3790 | int64_t size; | |
3791 | V9fsString name; | |
3792 | ssize_t err = 0; | |
3793 | size_t offset = 7; | |
3794 | int32_t fid, newfid; | |
3795 | V9fsFidState *file_fidp; | |
3796 | V9fsFidState *xattr_fidp = NULL; | |
3797 | V9fsPDU *pdu = opaque; | |
3798 | V9fsState *s = pdu->s; | |
3799 | ||
3800 | v9fs_string_init(&name); | |
3801 | err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); | |
3802 | if (err < 0) { | |
3803 | goto out_nofid; | |
3804 | } | |
3805 | trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data); | |
3806 | ||
3807 | file_fidp = get_fid(pdu, fid); | |
3808 | if (file_fidp == NULL) { | |
3809 | err = -ENOENT; | |
3810 | goto out_nofid; | |
3811 | } | |
3812 | xattr_fidp = alloc_fid(s, newfid); | |
3813 | if (xattr_fidp == NULL) { | |
3814 | err = -EINVAL; | |
3815 | goto out; | |
3816 | } | |
3817 | v9fs_path_copy(&xattr_fidp->path, &file_fidp->path); | |
3818 | if (!v9fs_string_size(&name)) { | |
3819 | /* | |
3820 | * listxattr request. Get the size first | |
3821 | */ | |
3822 | size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0); | |
3823 | if (size < 0) { | |
3824 | err = size; | |
3825 | clunk_fid(s, xattr_fidp->fid); | |
3826 | goto out; | |
3827 | } | |
3828 | /* | |
3829 | * Read the xattr value | |
3830 | */ | |
3831 | xattr_fidp->fs.xattr.len = size; | |
3832 | xattr_fidp->fid_type = P9_FID_XATTR; | |
3833 | xattr_fidp->fs.xattr.xattrwalk_fid = true; | |
3834 | xattr_fidp->fs.xattr.value = g_malloc0(size); | |
3835 | if (size) { | |
3836 | err = v9fs_co_llistxattr(pdu, &xattr_fidp->path, | |
3837 | xattr_fidp->fs.xattr.value, | |
3838 | xattr_fidp->fs.xattr.len); | |
3839 | if (err < 0) { | |
3840 | clunk_fid(s, xattr_fidp->fid); | |
3841 | goto out; | |
3842 | } | |
3843 | } | |
3844 | err = pdu_marshal(pdu, offset, "q", size); | |
3845 | if (err < 0) { | |
3846 | goto out; | |
3847 | } | |
3848 | err += offset; | |
3849 | } else { | |
3850 | /* | |
3851 | * specific xattr fid. We check for xattr | |
3852 | * presence also collect the xattr size | |
3853 | */ | |
3854 | size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, | |
3855 | &name, NULL, 0); | |
3856 | if (size < 0) { | |
3857 | err = size; | |
3858 | clunk_fid(s, xattr_fidp->fid); | |
3859 | goto out; | |
3860 | } | |
3861 | /* | |
3862 | * Read the xattr value | |
3863 | */ | |
3864 | xattr_fidp->fs.xattr.len = size; | |
3865 | xattr_fidp->fid_type = P9_FID_XATTR; | |
3866 | xattr_fidp->fs.xattr.xattrwalk_fid = true; | |
3867 | xattr_fidp->fs.xattr.value = g_malloc0(size); | |
3868 | if (size) { | |
3869 | err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, | |
3870 | &name, xattr_fidp->fs.xattr.value, | |
3871 | xattr_fidp->fs.xattr.len); | |
3872 | if (err < 0) { | |
3873 | clunk_fid(s, xattr_fidp->fid); | |
3874 | goto out; | |
3875 | } | |
3876 | } | |
3877 | err = pdu_marshal(pdu, offset, "q", size); | |
3878 | if (err < 0) { | |
3879 | goto out; | |
3880 | } | |
3881 | err += offset; | |
3882 | } | |
3883 | trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size); | |
3884 | out: | |
3885 | put_fid(pdu, file_fidp); | |
3886 | if (xattr_fidp) { | |
3887 | put_fid(pdu, xattr_fidp); | |
3888 | } | |
3889 | out_nofid: | |
3890 | pdu_complete(pdu, err); | |
3891 | v9fs_string_free(&name); | |
3892 | } | |
3893 | ||
3894 | static void coroutine_fn v9fs_xattrcreate(void *opaque) | |
3895 | { | |
3896 | int flags, rflags = 0; | |
3897 | int32_t fid; | |
3898 | uint64_t size; | |
3899 | ssize_t err = 0; | |
3900 | V9fsString name; | |
3901 | size_t offset = 7; | |
3902 | V9fsFidState *file_fidp; | |
3903 | V9fsFidState *xattr_fidp; | |
3904 | V9fsPDU *pdu = opaque; | |
3905 | ||
3906 | v9fs_string_init(&name); | |
3907 | err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); | |
3908 | if (err < 0) { | |
3909 | goto out_nofid; | |
3910 | } | |
3911 | trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags); | |
3912 | ||
3913 | if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) { | |
3914 | err = -EINVAL; | |
3915 | goto out_nofid; | |
3916 | } | |
3917 | ||
3918 | if (flags & P9_XATTR_CREATE) { | |
3919 | rflags |= XATTR_CREATE; | |
3920 | } | |
3921 | ||
3922 | if (flags & P9_XATTR_REPLACE) { | |
3923 | rflags |= XATTR_REPLACE; | |
3924 | } | |
3925 | ||
3926 | if (size > XATTR_SIZE_MAX) { | |
3927 | err = -E2BIG; | |
3928 | goto out_nofid; | |
3929 | } | |
3930 | ||
3931 | file_fidp = get_fid(pdu, fid); | |
3932 | if (file_fidp == NULL) { | |
3933 | err = -EINVAL; | |
3934 | goto out_nofid; | |
3935 | } | |
3936 | if (file_fidp->fid_type != P9_FID_NONE) { | |
3937 | err = -EINVAL; | |
3938 | goto out_put_fid; | |
3939 | } | |
3940 | ||
3941 | /* Make the file fid point to xattr */ | |
3942 | xattr_fidp = file_fidp; | |
3943 | xattr_fidp->fid_type = P9_FID_XATTR; | |
3944 | xattr_fidp->fs.xattr.copied_len = 0; | |
3945 | xattr_fidp->fs.xattr.xattrwalk_fid = false; | |
3946 | xattr_fidp->fs.xattr.len = size; | |
3947 | xattr_fidp->fs.xattr.flags = rflags; | |
3948 | v9fs_string_init(&xattr_fidp->fs.xattr.name); | |
3949 | v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name); | |
3950 | xattr_fidp->fs.xattr.value = g_malloc0(size); | |
3951 | err = offset; | |
3952 | out_put_fid: | |
3953 | put_fid(pdu, file_fidp); | |
3954 | out_nofid: | |
3955 | pdu_complete(pdu, err); | |
3956 | v9fs_string_free(&name); | |
3957 | } | |
3958 | ||
3959 | static void coroutine_fn v9fs_readlink(void *opaque) | |
3960 | { | |
3961 | V9fsPDU *pdu = opaque; | |
3962 | size_t offset = 7; | |
3963 | V9fsString target; | |
3964 | int32_t fid; | |
3965 | int err = 0; | |
3966 | V9fsFidState *fidp; | |
3967 | ||
3968 | err = pdu_unmarshal(pdu, offset, "d", &fid); | |
3969 | if (err < 0) { | |
3970 | goto out_nofid; | |
3971 | } | |
3972 | trace_v9fs_readlink(pdu->tag, pdu->id, fid); | |
3973 | fidp = get_fid(pdu, fid); | |
3974 | if (fidp == NULL) { | |
3975 | err = -ENOENT; | |
3976 | goto out_nofid; | |
3977 | } | |
3978 | ||
3979 | v9fs_string_init(&target); | |
3980 | err = v9fs_co_readlink(pdu, &fidp->path, &target); | |
3981 | if (err < 0) { | |
3982 | goto out; | |
3983 | } | |
3984 | err = pdu_marshal(pdu, offset, "s", &target); | |
3985 | if (err < 0) { | |
3986 | v9fs_string_free(&target); | |
3987 | goto out; | |
3988 | } | |
3989 | err += offset; | |
3990 | trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data); | |
3991 | v9fs_string_free(&target); | |
3992 | out: | |
3993 | put_fid(pdu, fidp); | |
3994 | out_nofid: | |
3995 | pdu_complete(pdu, err); | |
3996 | } | |
3997 | ||
3998 | static CoroutineEntry *pdu_co_handlers[] = { | |
3999 | [P9_TREADDIR] = v9fs_readdir, | |
4000 | [P9_TSTATFS] = v9fs_statfs, | |
4001 | [P9_TGETATTR] = v9fs_getattr, | |
4002 | [P9_TSETATTR] = v9fs_setattr, | |
4003 | [P9_TXATTRWALK] = v9fs_xattrwalk, | |
4004 | [P9_TXATTRCREATE] = v9fs_xattrcreate, | |
4005 | [P9_TMKNOD] = v9fs_mknod, | |
4006 | [P9_TRENAME] = v9fs_rename, | |
4007 | [P9_TLOCK] = v9fs_lock, | |
4008 | [P9_TGETLOCK] = v9fs_getlock, | |
4009 | [P9_TRENAMEAT] = v9fs_renameat, | |
4010 | [P9_TREADLINK] = v9fs_readlink, | |
4011 | [P9_TUNLINKAT] = v9fs_unlinkat, | |
4012 | [P9_TMKDIR] = v9fs_mkdir, | |
4013 | [P9_TVERSION] = v9fs_version, | |
4014 | [P9_TLOPEN] = v9fs_open, | |
4015 | [P9_TATTACH] = v9fs_attach, | |
4016 | [P9_TSTAT] = v9fs_stat, | |
4017 | [P9_TWALK] = v9fs_walk, | |
4018 | [P9_TCLUNK] = v9fs_clunk, | |
4019 | [P9_TFSYNC] = v9fs_fsync, | |
4020 | [P9_TOPEN] = v9fs_open, | |
4021 | [P9_TREAD] = v9fs_read, | |
4022 | #if 0 | |
4023 | [P9_TAUTH] = v9fs_auth, | |
4024 | #endif | |
4025 | [P9_TFLUSH] = v9fs_flush, | |
4026 | [P9_TLINK] = v9fs_link, | |
4027 | [P9_TSYMLINK] = v9fs_symlink, | |
4028 | [P9_TCREATE] = v9fs_create, | |
4029 | [P9_TLCREATE] = v9fs_lcreate, | |
4030 | [P9_TWRITE] = v9fs_write, | |
4031 | [P9_TWSTAT] = v9fs_wstat, | |
4032 | [P9_TREMOVE] = v9fs_remove, | |
4033 | }; | |
4034 | ||
4035 | static void coroutine_fn v9fs_op_not_supp(void *opaque) | |
4036 | { | |
4037 | V9fsPDU *pdu = opaque; | |
4038 | pdu_complete(pdu, -EOPNOTSUPP); | |
4039 | } | |
4040 | ||
4041 | static void coroutine_fn v9fs_fs_ro(void *opaque) | |
4042 | { | |
4043 | V9fsPDU *pdu = opaque; | |
4044 | pdu_complete(pdu, -EROFS); | |
4045 | } | |
4046 | ||
4047 | static inline bool is_read_only_op(V9fsPDU *pdu) | |
4048 | { | |
4049 | switch (pdu->id) { | |
4050 | case P9_TREADDIR: | |
4051 | case P9_TSTATFS: | |
4052 | case P9_TGETATTR: | |
4053 | case P9_TXATTRWALK: | |
4054 | case P9_TLOCK: | |
4055 | case P9_TGETLOCK: | |
4056 | case P9_TREADLINK: | |
4057 | case P9_TVERSION: | |
4058 | case P9_TLOPEN: | |
4059 | case P9_TATTACH: | |
4060 | case P9_TSTAT: | |
4061 | case P9_TWALK: | |
4062 | case P9_TCLUNK: | |
4063 | case P9_TFSYNC: | |
4064 | case P9_TOPEN: | |
4065 | case P9_TREAD: | |
4066 | case P9_TAUTH: | |
4067 | case P9_TFLUSH: | |
4068 | return 1; | |
4069 | default: | |
4070 | return 0; | |
4071 | } | |
4072 | } | |
4073 | ||
4074 | void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr) | |
4075 | { | |
4076 | Coroutine *co; | |
4077 | CoroutineEntry *handler; | |
4078 | V9fsState *s = pdu->s; | |
4079 | ||
4080 | pdu->size = le32_to_cpu(hdr->size_le); | |
4081 | pdu->id = hdr->id; | |
4082 | pdu->tag = le16_to_cpu(hdr->tag_le); | |
4083 | ||
4084 | if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) || | |
4085 | (pdu_co_handlers[pdu->id] == NULL)) { | |
4086 | handler = v9fs_op_not_supp; | |
4087 | } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { | |
4088 | handler = v9fs_fs_ro; | |
4089 | } else { | |
4090 | handler = pdu_co_handlers[pdu->id]; | |
4091 | } | |
4092 | ||
4093 | qemu_co_queue_init(&pdu->complete); | |
4094 | co = qemu_coroutine_create(handler, pdu); | |
4095 | qemu_coroutine_enter(co); | |
4096 | } | |
4097 | ||
4098 | /* Returns 0 on success, 1 on failure. */ | |
4099 | int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, | |
4100 | Error **errp) | |
4101 | { | |
4102 | ERRP_GUARD(); | |
4103 | int i, len; | |
4104 | struct stat stat; | |
4105 | FsDriverEntry *fse; | |
4106 | V9fsPath path; | |
4107 | int rc = 1; | |
4108 | ||
4109 | assert(!s->transport); | |
4110 | s->transport = t; | |
4111 | ||
4112 | /* initialize pdu allocator */ | |
4113 | QLIST_INIT(&s->free_list); | |
4114 | QLIST_INIT(&s->active_list); | |
4115 | for (i = 0; i < MAX_REQ; i++) { | |
4116 | QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); | |
4117 | s->pdus[i].s = s; | |
4118 | s->pdus[i].idx = i; | |
4119 | } | |
4120 | ||
4121 | v9fs_path_init(&path); | |
4122 | ||
4123 | fse = get_fsdev_fsentry(s->fsconf.fsdev_id); | |
4124 | ||
4125 | if (!fse) { | |
4126 | /* We don't have a fsdev identified by fsdev_id */ | |
4127 | error_setg(errp, "9pfs device couldn't find fsdev with the " | |
4128 | "id = %s", | |
4129 | s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); | |
4130 | goto out; | |
4131 | } | |
4132 | ||
4133 | if (!s->fsconf.tag) { | |
4134 | /* we haven't specified a mount_tag */ | |
4135 | error_setg(errp, "fsdev with id %s needs mount_tag arguments", | |
4136 | s->fsconf.fsdev_id); | |
4137 | goto out; | |
4138 | } | |
4139 | ||
4140 | s->ctx.export_flags = fse->export_flags; | |
4141 | s->ctx.fs_root = g_strdup(fse->path); | |
4142 | s->ctx.exops.get_st_gen = NULL; | |
4143 | len = strlen(s->fsconf.tag); | |
4144 | if (len > MAX_TAG_LEN - 1) { | |
4145 | error_setg(errp, "mount tag '%s' (%d bytes) is longer than " | |
4146 | "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); | |
4147 | goto out; | |
4148 | } | |
4149 | ||
4150 | s->tag = g_strdup(s->fsconf.tag); | |
4151 | s->ctx.uid = -1; | |
4152 | ||
4153 | s->ops = fse->ops; | |
4154 | ||
4155 | s->ctx.fmode = fse->fmode; | |
4156 | s->ctx.dmode = fse->dmode; | |
4157 | ||
4158 | QSIMPLEQ_INIT(&s->fid_list); | |
4159 | qemu_co_rwlock_init(&s->rename_lock); | |
4160 | ||
4161 | if (s->ops->init(&s->ctx, errp) < 0) { | |
4162 | error_prepend(errp, "cannot initialize fsdev '%s': ", | |
4163 | s->fsconf.fsdev_id); | |
4164 | goto out; | |
4165 | } | |
4166 | ||
4167 | /* | |
4168 | * Check details of export path, We need to use fs driver | |
4169 | * call back to do that. Since we are in the init path, we don't | |
4170 | * use co-routines here. | |
4171 | */ | |
4172 | if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { | |
4173 | error_setg(errp, | |
4174 | "error in converting name to path %s", strerror(errno)); | |
4175 | goto out; | |
4176 | } | |
4177 | if (s->ops->lstat(&s->ctx, &path, &stat)) { | |
4178 | error_setg(errp, "share path %s does not exist", fse->path); | |
4179 | goto out; | |
4180 | } else if (!S_ISDIR(stat.st_mode)) { | |
4181 | error_setg(errp, "share path %s is not a directory", fse->path); | |
4182 | goto out; | |
4183 | } | |
4184 | ||
4185 | s->dev_id = stat.st_dev; | |
4186 | ||
4187 | /* init inode remapping : */ | |
4188 | /* hash table for variable length inode suffixes */ | |
4189 | qpd_table_init(&s->qpd_table); | |
4190 | /* hash table for slow/full inode remapping (most users won't need it) */ | |
4191 | qpf_table_init(&s->qpf_table); | |
4192 | /* hash table for quick inode remapping */ | |
4193 | qpp_table_init(&s->qpp_table); | |
4194 | s->qp_ndevices = 0; | |
4195 | s->qp_affix_next = 1; /* reserve 0 to detect overflow */ | |
4196 | s->qp_fullpath_next = 1; | |
4197 | ||
4198 | s->ctx.fst = &fse->fst; | |
4199 | fsdev_throttle_init(s->ctx.fst); | |
4200 | ||
4201 | rc = 0; | |
4202 | out: | |
4203 | if (rc) { | |
4204 | v9fs_device_unrealize_common(s); | |
4205 | } | |
4206 | v9fs_path_free(&path); | |
4207 | return rc; | |
4208 | } | |
4209 | ||
4210 | void v9fs_device_unrealize_common(V9fsState *s) | |
4211 | { | |
4212 | if (s->ops && s->ops->cleanup) { | |
4213 | s->ops->cleanup(&s->ctx); | |
4214 | } | |
4215 | if (s->ctx.fst) { | |
4216 | fsdev_throttle_cleanup(s->ctx.fst); | |
4217 | } | |
4218 | g_free(s->tag); | |
4219 | qp_table_destroy(&s->qpd_table); | |
4220 | qp_table_destroy(&s->qpp_table); | |
4221 | qp_table_destroy(&s->qpf_table); | |
4222 | g_free(s->ctx.fs_root); | |
4223 | } | |
4224 | ||
4225 | typedef struct VirtfsCoResetData { | |
4226 | V9fsPDU pdu; | |
4227 | bool done; | |
4228 | } VirtfsCoResetData; | |
4229 | ||
4230 | static void coroutine_fn virtfs_co_reset(void *opaque) | |
4231 | { | |
4232 | VirtfsCoResetData *data = opaque; | |
4233 | ||
4234 | virtfs_reset(&data->pdu); | |
4235 | data->done = true; | |
4236 | } | |
4237 | ||
4238 | void v9fs_reset(V9fsState *s) | |
4239 | { | |
4240 | VirtfsCoResetData data = { .pdu = { .s = s }, .done = false }; | |
4241 | Coroutine *co; | |
4242 | ||
4243 | while (!QLIST_EMPTY(&s->active_list)) { | |
4244 | aio_poll(qemu_get_aio_context(), true); | |
4245 | } | |
4246 | ||
4247 | co = qemu_coroutine_create(virtfs_co_reset, &data); | |
4248 | qemu_coroutine_enter(co); | |
4249 | ||
4250 | while (!data.done) { | |
4251 | aio_poll(qemu_get_aio_context(), true); | |
4252 | } | |
4253 | } | |
4254 | ||
4255 | static void __attribute__((__constructor__)) v9fs_set_fd_limit(void) | |
4256 | { | |
4257 | struct rlimit rlim; | |
4258 | if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { | |
4259 | error_report("Failed to get the resource limit"); | |
4260 | exit(1); | |
4261 | } | |
4262 | open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3); | |
4263 | open_fd_rc = rlim.rlim_cur / 2; | |
4264 | } |