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