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