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