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