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