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