]>
Commit | Line | Data |
---|---|---|
9f107513 AL |
1 | /* |
2 | * Virtio 9p backend | |
3 | * | |
4 | * Copyright IBM, Corp. 2010 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
fbc04127 | 14 | #include "qemu/osdep.h" |
e3e83f2e | 15 | #include <glib/gprintf.h> |
0d09e41a | 16 | #include "hw/virtio/virtio.h" |
da34e65c | 17 | #include "qapi/error.h" |
d49b6836 | 18 | #include "qemu/error-report.h" |
cd4bfbb2 | 19 | #include "qemu/iov.h" |
1de7afc9 | 20 | #include "qemu/sockets.h" |
9f107513 AL |
21 | #include "virtio-9p.h" |
22 | #include "fsdev/qemu-fsdev.h" | |
267ae092 | 23 | #include "9p-xattr.h" |
fe52840c | 24 | #include "coth.h" |
c572f23a | 25 | #include "trace.h" |
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 AL |
981 | ssize_t err; |
982 | ||
ddca7f86 MK |
983 | v9fs_string_init(&uname); |
984 | v9fs_string_init(&aname); | |
985 | err = pdu_unmarshal(pdu, offset, "ddssd", &fid, | |
986 | &afid, &uname, &aname, &n_uname); | |
987 | if (err < 0) { | |
988 | goto out_nofid; | |
989 | } | |
c572f23a | 990 | trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data); |
955efc47 AL |
991 | |
992 | fidp = alloc_fid(s, fid); | |
993 | if (fidp == NULL) { | |
994 | err = -EINVAL; | |
84dfb926 | 995 | goto out_nofid; |
9f107513 | 996 | } |
955efc47 | 997 | fidp->uid = n_uname; |
bccacf6c | 998 | err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path); |
2289be19 AK |
999 | if (err < 0) { |
1000 | err = -EINVAL; | |
1001 | clunk_fid(s, fid); | |
1002 | goto out; | |
1003 | } | |
bccacf6c | 1004 | err = fid_to_qid(pdu, fidp, &qid); |
8c158561 | 1005 | if (err < 0) { |
955efc47 | 1006 | err = -EINVAL; |
84dfb926 | 1007 | clunk_fid(s, fid); |
955efc47 AL |
1008 | goto out; |
1009 | } | |
ddca7f86 MK |
1010 | err = pdu_marshal(pdu, offset, "Q", &qid); |
1011 | if (err < 0) { | |
1012 | clunk_fid(s, fid); | |
1013 | goto out; | |
1014 | } | |
1015 | err += offset; | |
56f101ec | 1016 | memcpy(&s->root_qid, &qid, sizeof(qid)); |
7999f7e1 AK |
1017 | trace_v9fs_attach_return(pdu->tag, pdu->id, |
1018 | qid.type, qid.version, qid.path); | |
4cdc0789 AK |
1019 | /* |
1020 | * disable migration if we haven't done already. | |
1021 | * attach could get called multiple times for the same export. | |
1022 | */ | |
1023 | if (!s->migration_blocker) { | |
1024 | s->root_fid = fid; | |
f231b88d CR |
1025 | error_setg(&s->migration_blocker, |
1026 | "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'", | |
1027 | s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag); | |
4cdc0789 AK |
1028 | migrate_add_blocker(s->migration_blocker); |
1029 | } | |
955efc47 | 1030 | out: |
bccacf6c | 1031 | put_fid(pdu, fidp); |
84dfb926 | 1032 | out_nofid: |
dc295f83 | 1033 | pdu_complete(pdu, err); |
955efc47 AL |
1034 | v9fs_string_free(&uname); |
1035 | v9fs_string_free(&aname); | |
9f107513 AL |
1036 | } |
1037 | ||
8440e22e | 1038 | static void coroutine_fn v9fs_stat(void *opaque) |
9f107513 | 1039 | { |
4da7d3fa | 1040 | int32_t fid; |
d8e0c29e | 1041 | V9fsStat v9stat; |
4da7d3fa | 1042 | ssize_t err = 0; |
d8e0c29e AK |
1043 | size_t offset = 7; |
1044 | struct stat stbuf; | |
1045 | V9fsFidState *fidp; | |
1046 | V9fsPDU *pdu = opaque; | |
4da7d3fa | 1047 | |
ddca7f86 MK |
1048 | err = pdu_unmarshal(pdu, offset, "d", &fid); |
1049 | if (err < 0) { | |
1050 | goto out_nofid; | |
1051 | } | |
c572f23a | 1052 | trace_v9fs_stat(pdu->tag, pdu->id, fid); |
84dfb926 | 1053 | |
bccacf6c | 1054 | fidp = get_fid(pdu, fid); |
d8e0c29e | 1055 | if (fidp == NULL) { |
4da7d3fa | 1056 | err = -ENOENT; |
84dfb926 | 1057 | goto out_nofid; |
9f107513 | 1058 | } |
bccacf6c | 1059 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
d8e0c29e AK |
1060 | if (err < 0) { |
1061 | goto out; | |
1062 | } | |
bccacf6c | 1063 | err = stat_to_v9stat(pdu, &fidp->path, &stbuf, &v9stat); |
d8e0c29e AK |
1064 | if (err < 0) { |
1065 | goto out; | |
1066 | } | |
ddca7f86 MK |
1067 | err = pdu_marshal(pdu, offset, "wS", 0, &v9stat); |
1068 | if (err < 0) { | |
1069 | v9fs_stat_free(&v9stat); | |
1070 | goto out; | |
1071 | } | |
7999f7e1 AK |
1072 | trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode, |
1073 | v9stat.atime, v9stat.mtime, v9stat.length); | |
ddca7f86 | 1074 | err += offset; |
d8e0c29e | 1075 | v9fs_stat_free(&v9stat); |
4da7d3fa | 1076 | out: |
bccacf6c | 1077 | put_fid(pdu, fidp); |
84dfb926 | 1078 | out_nofid: |
dc295f83 | 1079 | pdu_complete(pdu, err); |
9f107513 AL |
1080 | } |
1081 | ||
8440e22e | 1082 | static void coroutine_fn v9fs_getattr(void *opaque) |
00ede4c2 SK |
1083 | { |
1084 | int32_t fid; | |
8db21ce7 AK |
1085 | size_t offset = 7; |
1086 | ssize_t retval = 0; | |
1087 | struct stat stbuf; | |
00ede4c2 SK |
1088 | V9fsFidState *fidp; |
1089 | uint64_t request_mask; | |
8db21ce7 AK |
1090 | V9fsStatDotl v9stat_dotl; |
1091 | V9fsPDU *pdu = opaque; | |
1092 | V9fsState *s = pdu->s; | |
00ede4c2 | 1093 | |
ddca7f86 MK |
1094 | retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); |
1095 | if (retval < 0) { | |
1096 | goto out_nofid; | |
1097 | } | |
c572f23a | 1098 | trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask); |
00ede4c2 | 1099 | |
bccacf6c | 1100 | fidp = get_fid(pdu, fid); |
00ede4c2 | 1101 | if (fidp == NULL) { |
8db21ce7 | 1102 | retval = -ENOENT; |
84dfb926 | 1103 | goto out_nofid; |
00ede4c2 | 1104 | } |
8db21ce7 AK |
1105 | /* |
1106 | * Currently we only support BASIC fields in stat, so there is no | |
00ede4c2 SK |
1107 | * need to look at request_mask. |
1108 | */ | |
bccacf6c | 1109 | retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
8db21ce7 AK |
1110 | if (retval < 0) { |
1111 | goto out; | |
1112 | } | |
1113 | stat_to_v9stat_dotl(s, &stbuf, &v9stat_dotl); | |
e06a765e HPB |
1114 | |
1115 | /* fill st_gen if requested and supported by underlying fs */ | |
1116 | if (request_mask & P9_STATS_GEN) { | |
1117 | retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl); | |
f8b7ee38 KS |
1118 | switch (retval) { |
1119 | case 0: | |
1120 | /* we have valid st_gen: update result mask */ | |
1121 | v9stat_dotl.st_result_mask |= P9_STATS_GEN; | |
1122 | break; | |
1123 | case -EINTR: | |
1124 | /* request cancelled, e.g. by Tflush */ | |
e06a765e | 1125 | goto out; |
f8b7ee38 KS |
1126 | default: |
1127 | /* failed to get st_gen: not fatal, ignore */ | |
1128 | break; | |
e06a765e | 1129 | } |
e06a765e | 1130 | } |
ddca7f86 MK |
1131 | retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl); |
1132 | if (retval < 0) { | |
1133 | goto out; | |
1134 | } | |
1135 | retval += offset; | |
c572f23a HPB |
1136 | trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask, |
1137 | v9stat_dotl.st_mode, v9stat_dotl.st_uid, | |
1138 | v9stat_dotl.st_gid); | |
7999f7e1 AK |
1139 | out: |
1140 | put_fid(pdu, fidp); | |
1141 | out_nofid: | |
dc295f83 | 1142 | pdu_complete(pdu, retval); |
00ede4c2 SK |
1143 | } |
1144 | ||
e4027caf AK |
1145 | /* Attribute flags */ |
1146 | #define P9_ATTR_MODE (1 << 0) | |
1147 | #define P9_ATTR_UID (1 << 1) | |
1148 | #define P9_ATTR_GID (1 << 2) | |
1149 | #define P9_ATTR_SIZE (1 << 3) | |
1150 | #define P9_ATTR_ATIME (1 << 4) | |
1151 | #define P9_ATTR_MTIME (1 << 5) | |
1152 | #define P9_ATTR_CTIME (1 << 6) | |
1153 | #define P9_ATTR_ATIME_SET (1 << 7) | |
1154 | #define P9_ATTR_MTIME_SET (1 << 8) | |
1155 | ||
1156 | #define P9_ATTR_MASK 127 | |
c79ce737 | 1157 | |
8440e22e | 1158 | static void coroutine_fn v9fs_setattr(void *opaque) |
c79ce737 | 1159 | { |
65c05f9a AK |
1160 | int err = 0; |
1161 | int32_t fid; | |
1162 | V9fsFidState *fidp; | |
1163 | size_t offset = 7; | |
1164 | V9fsIattr v9iattr; | |
1165 | V9fsPDU *pdu = opaque; | |
c79ce737 | 1166 | |
ddca7f86 MK |
1167 | err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); |
1168 | if (err < 0) { | |
1169 | goto out_nofid; | |
1170 | } | |
c79ce737 | 1171 | |
bccacf6c | 1172 | fidp = get_fid(pdu, fid); |
65c05f9a AK |
1173 | if (fidp == NULL) { |
1174 | err = -EINVAL; | |
84dfb926 | 1175 | goto out_nofid; |
c79ce737 | 1176 | } |
e4027caf | 1177 | if (v9iattr.valid & P9_ATTR_MODE) { |
bccacf6c | 1178 | err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode); |
65c05f9a AK |
1179 | if (err < 0) { |
1180 | goto out; | |
c79ce737 | 1181 | } |
c79ce737 | 1182 | } |
e4027caf | 1183 | if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) { |
c79ce737 | 1184 | struct timespec times[2]; |
e4027caf AK |
1185 | if (v9iattr.valid & P9_ATTR_ATIME) { |
1186 | if (v9iattr.valid & P9_ATTR_ATIME_SET) { | |
65c05f9a AK |
1187 | times[0].tv_sec = v9iattr.atime_sec; |
1188 | times[0].tv_nsec = v9iattr.atime_nsec; | |
c79ce737 SK |
1189 | } else { |
1190 | times[0].tv_nsec = UTIME_NOW; | |
1191 | } | |
1192 | } else { | |
1193 | times[0].tv_nsec = UTIME_OMIT; | |
1194 | } | |
e4027caf AK |
1195 | if (v9iattr.valid & P9_ATTR_MTIME) { |
1196 | if (v9iattr.valid & P9_ATTR_MTIME_SET) { | |
65c05f9a AK |
1197 | times[1].tv_sec = v9iattr.mtime_sec; |
1198 | times[1].tv_nsec = v9iattr.mtime_nsec; | |
c79ce737 SK |
1199 | } else { |
1200 | times[1].tv_nsec = UTIME_NOW; | |
1201 | } | |
1202 | } else { | |
1203 | times[1].tv_nsec = UTIME_OMIT; | |
1204 | } | |
bccacf6c | 1205 | err = v9fs_co_utimensat(pdu, &fidp->path, times); |
65c05f9a AK |
1206 | if (err < 0) { |
1207 | goto out; | |
1208 | } | |
c79ce737 | 1209 | } |
65c05f9a AK |
1210 | /* |
1211 | * If the only valid entry in iattr is ctime we can call | |
1212 | * chown(-1,-1) to update the ctime of the file | |
1213 | */ | |
e4027caf AK |
1214 | if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) || |
1215 | ((v9iattr.valid & P9_ATTR_CTIME) | |
1216 | && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) { | |
1217 | if (!(v9iattr.valid & P9_ATTR_UID)) { | |
65c05f9a AK |
1218 | v9iattr.uid = -1; |
1219 | } | |
e4027caf | 1220 | if (!(v9iattr.valid & P9_ATTR_GID)) { |
65c05f9a AK |
1221 | v9iattr.gid = -1; |
1222 | } | |
bccacf6c | 1223 | err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid, |
65c05f9a AK |
1224 | v9iattr.gid); |
1225 | if (err < 0) { | |
1226 | goto out; | |
1227 | } | |
c79ce737 | 1228 | } |
e4027caf | 1229 | if (v9iattr.valid & (P9_ATTR_SIZE)) { |
bccacf6c | 1230 | err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size); |
65c05f9a AK |
1231 | if (err < 0) { |
1232 | goto out; | |
1233 | } | |
c79ce737 | 1234 | } |
65c05f9a | 1235 | err = offset; |
c79ce737 | 1236 | out: |
bccacf6c | 1237 | put_fid(pdu, fidp); |
84dfb926 | 1238 | out_nofid: |
dc295f83 | 1239 | pdu_complete(pdu, err); |
c79ce737 SK |
1240 | } |
1241 | ||
3cc19c0c | 1242 | static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids) |
ff5e54c9 AL |
1243 | { |
1244 | int i; | |
ddca7f86 | 1245 | ssize_t err; |
3cc19c0c | 1246 | size_t offset = 7; |
ddca7f86 MK |
1247 | |
1248 | err = pdu_marshal(pdu, offset, "w", nwnames); | |
1249 | if (err < 0) { | |
1250 | return err; | |
1251 | } | |
1252 | offset += err; | |
3cc19c0c | 1253 | for (i = 0; i < nwnames; i++) { |
ddca7f86 MK |
1254 | err = pdu_marshal(pdu, offset, "Q", &qids[i]); |
1255 | if (err < 0) { | |
1256 | return err; | |
1257 | } | |
1258 | offset += err; | |
ff5e54c9 | 1259 | } |
3cc19c0c | 1260 | return offset; |
ff5e54c9 AL |
1261 | } |
1262 | ||
fff39a7a GK |
1263 | static bool name_is_illegal(const char *name) |
1264 | { | |
1265 | return !*name || strchr(name, '/') != NULL; | |
1266 | } | |
1267 | ||
56f101ec GK |
1268 | static bool not_same_qid(const V9fsQID *qid1, const V9fsQID *qid2) |
1269 | { | |
1270 | return | |
1271 | qid1->type != qid2->type || | |
1272 | qid1->version != qid2->version || | |
1273 | qid1->path != qid2->path; | |
1274 | } | |
1275 | ||
8440e22e | 1276 | static void coroutine_fn v9fs_walk(void *opaque) |
9f107513 | 1277 | { |
3cc19c0c AK |
1278 | int name_idx; |
1279 | V9fsQID *qids = NULL; | |
1280 | int i, err = 0; | |
2289be19 | 1281 | V9fsPath dpath, path; |
3cc19c0c AK |
1282 | uint16_t nwnames; |
1283 | struct stat stbuf; | |
1284 | size_t offset = 7; | |
1285 | int32_t fid, newfid; | |
1286 | V9fsString *wnames = NULL; | |
1287 | V9fsFidState *fidp; | |
3a93113a | 1288 | V9fsFidState *newfidp = NULL; |
ff06030f VJ |
1289 | V9fsPDU *pdu = opaque; |
1290 | V9fsState *s = pdu->s; | |
56f101ec | 1291 | V9fsQID qid; |
ff5e54c9 | 1292 | |
ddca7f86 MK |
1293 | err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames); |
1294 | if (err < 0) { | |
dc295f83 | 1295 | pdu_complete(pdu, err); |
ddca7f86 MK |
1296 | return ; |
1297 | } | |
1298 | offset += err; | |
ff5e54c9 | 1299 | |
c572f23a HPB |
1300 | trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames); |
1301 | ||
3cc19c0c AK |
1302 | if (nwnames && nwnames <= P9_MAXWELEM) { |
1303 | wnames = g_malloc0(sizeof(wnames[0]) * nwnames); | |
1304 | qids = g_malloc0(sizeof(qids[0]) * nwnames); | |
1305 | for (i = 0; i < nwnames; i++) { | |
ddca7f86 MK |
1306 | err = pdu_unmarshal(pdu, offset, "s", &wnames[i]); |
1307 | if (err < 0) { | |
1308 | goto out_nofid; | |
1309 | } | |
fff39a7a GK |
1310 | if (name_is_illegal(wnames[i].data)) { |
1311 | err = -ENOENT; | |
1312 | goto out_nofid; | |
1313 | } | |
ddca7f86 | 1314 | offset += err; |
ff5e54c9 | 1315 | } |
3cc19c0c | 1316 | } else if (nwnames > P9_MAXWELEM) { |
4f8dee2d | 1317 | err = -EINVAL; |
84dfb926 | 1318 | goto out_nofid; |
ff5e54c9 | 1319 | } |
bccacf6c | 1320 | fidp = get_fid(pdu, fid); |
3cc19c0c | 1321 | if (fidp == NULL) { |
ff5e54c9 | 1322 | err = -ENOENT; |
84dfb926 | 1323 | goto out_nofid; |
ff5e54c9 | 1324 | } |
56f101ec | 1325 | |
13fd08e6 GK |
1326 | v9fs_path_init(&dpath); |
1327 | v9fs_path_init(&path); | |
1328 | ||
56f101ec GK |
1329 | err = fid_to_qid(pdu, fidp, &qid); |
1330 | if (err < 0) { | |
1331 | goto out; | |
1332 | } | |
1333 | ||
2289be19 AK |
1334 | /* |
1335 | * Both dpath and path initially poin to fidp. | |
1336 | * Needed to handle request with nwnames == 0 | |
1337 | */ | |
1338 | v9fs_path_copy(&dpath, &fidp->path); | |
1339 | v9fs_path_copy(&path, &fidp->path); | |
1340 | for (name_idx = 0; name_idx < nwnames; name_idx++) { | |
56f101ec GK |
1341 | if (not_same_qid(&pdu->s->root_qid, &qid) || |
1342 | strcmp("..", wnames[name_idx].data)) { | |
1343 | err = v9fs_co_name_to_path(pdu, &dpath, wnames[name_idx].data, | |
1344 | &path); | |
1345 | if (err < 0) { | |
1346 | goto out; | |
1347 | } | |
1348 | ||
1349 | err = v9fs_co_lstat(pdu, &path, &stbuf); | |
1350 | if (err < 0) { | |
1351 | goto out; | |
1352 | } | |
1353 | stat_to_qid(&stbuf, &qid); | |
1354 | v9fs_path_copy(&dpath, &path); | |
2289be19 | 1355 | } |
56f101ec | 1356 | memcpy(&qids[name_idx], &qid, sizeof(qid)); |
2289be19 | 1357 | } |
ff5e54c9 | 1358 | if (fid == newfid) { |
49dd946b GK |
1359 | if (fidp->fid_type != P9_FID_NONE) { |
1360 | err = -EINVAL; | |
1361 | goto out; | |
1362 | } | |
2289be19 | 1363 | v9fs_path_copy(&fidp->path, &path); |
ff5e54c9 | 1364 | } else { |
3cc19c0c AK |
1365 | newfidp = alloc_fid(s, newfid); |
1366 | if (newfidp == NULL) { | |
ff5e54c9 AL |
1367 | err = -EINVAL; |
1368 | goto out; | |
1369 | } | |
3cc19c0c | 1370 | newfidp->uid = fidp->uid; |
2289be19 | 1371 | v9fs_path_copy(&newfidp->path, &path); |
9f107513 | 1372 | } |
3cc19c0c | 1373 | err = v9fs_walk_marshal(pdu, nwnames, qids); |
7999f7e1 | 1374 | trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids); |
ff5e54c9 | 1375 | out: |
bccacf6c | 1376 | put_fid(pdu, fidp); |
84dfb926 | 1377 | if (newfidp) { |
bccacf6c | 1378 | put_fid(pdu, newfidp); |
84dfb926 | 1379 | } |
2289be19 AK |
1380 | v9fs_path_free(&dpath); |
1381 | v9fs_path_free(&path); | |
84dfb926 | 1382 | out_nofid: |
dc295f83 | 1383 | pdu_complete(pdu, err); |
3cc19c0c AK |
1384 | if (nwnames && nwnames <= P9_MAXWELEM) { |
1385 | for (name_idx = 0; name_idx < nwnames; name_idx++) { | |
1386 | v9fs_string_free(&wnames[name_idx]); | |
1387 | } | |
1388 | g_free(wnames); | |
1389 | g_free(qids); | |
1390 | } | |
9f107513 AL |
1391 | } |
1392 | ||
8440e22e | 1393 | static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) |
5e94c103 MK |
1394 | { |
1395 | struct statfs stbuf; | |
1396 | int32_t iounit = 0; | |
bccacf6c | 1397 | V9fsState *s = pdu->s; |
5e94c103 MK |
1398 | |
1399 | /* | |
1400 | * iounit should be multiples of f_bsize (host filesystem block size | |
1401 | * and as well as less than (client msize - P9_IOHDRSZ)) | |
1402 | */ | |
bccacf6c | 1403 | if (!v9fs_co_statfs(pdu, path, &stbuf)) { |
5e94c103 MK |
1404 | iounit = stbuf.f_bsize; |
1405 | iounit *= (s->msize - P9_IOHDRSZ)/stbuf.f_bsize; | |
1406 | } | |
5e94c103 MK |
1407 | if (!iounit) { |
1408 | iounit = s->msize - P9_IOHDRSZ; | |
1409 | } | |
1410 | return iounit; | |
1411 | } | |
1412 | ||
8440e22e | 1413 | static void coroutine_fn v9fs_open(void *opaque) |
5e94c103 | 1414 | { |
857bc158 | 1415 | int flags; |
857bc158 AK |
1416 | int32_t fid; |
1417 | int32_t mode; | |
1418 | V9fsQID qid; | |
7999f7e1 | 1419 | int iounit = 0; |
857bc158 AK |
1420 | ssize_t err = 0; |
1421 | size_t offset = 7; | |
1422 | struct stat stbuf; | |
1423 | V9fsFidState *fidp; | |
1424 | V9fsPDU *pdu = opaque; | |
1425 | V9fsState *s = pdu->s; | |
5e94c103 | 1426 | |
857bc158 | 1427 | if (s->proto_version == V9FS_PROTO_2000L) { |
ddca7f86 | 1428 | err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode); |
857bc158 | 1429 | } else { |
67d6fa53 BH |
1430 | uint8_t modebyte; |
1431 | err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte); | |
1432 | mode = modebyte; | |
ddca7f86 MK |
1433 | } |
1434 | if (err < 0) { | |
1435 | goto out_nofid; | |
857bc158 | 1436 | } |
c572f23a HPB |
1437 | trace_v9fs_open(pdu->tag, pdu->id, fid, mode); |
1438 | ||
bccacf6c | 1439 | fidp = get_fid(pdu, fid); |
857bc158 AK |
1440 | if (fidp == NULL) { |
1441 | err = -ENOENT; | |
84dfb926 | 1442 | goto out_nofid; |
a6568fe2 | 1443 | } |
49dd946b GK |
1444 | if (fidp->fid_type != P9_FID_NONE) { |
1445 | err = -EINVAL; | |
1446 | goto out; | |
1447 | } | |
a6568fe2 | 1448 | |
bccacf6c | 1449 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
857bc158 | 1450 | if (err < 0) { |
a6568fe2 AL |
1451 | goto out; |
1452 | } | |
857bc158 AK |
1453 | stat_to_qid(&stbuf, &qid); |
1454 | if (S_ISDIR(stbuf.st_mode)) { | |
bccacf6c | 1455 | err = v9fs_co_opendir(pdu, fidp); |
857bc158 AK |
1456 | if (err < 0) { |
1457 | goto out; | |
1458 | } | |
1459 | fidp->fid_type = P9_FID_DIR; | |
ddca7f86 MK |
1460 | err = pdu_marshal(pdu, offset, "Qd", &qid, 0); |
1461 | if (err < 0) { | |
1462 | goto out; | |
1463 | } | |
1464 | err += offset; | |
a6568fe2 | 1465 | } else { |
771e9d4c | 1466 | if (s->proto_version == V9FS_PROTO_2000L) { |
d3ab98e6 | 1467 | flags = get_dotl_openflags(s, mode); |
771e9d4c | 1468 | } else { |
857bc158 | 1469 | flags = omode_to_uflags(mode); |
771e9d4c | 1470 | } |
2c74c2cb MK |
1471 | if (is_ro_export(&s->ctx)) { |
1472 | if (mode & O_WRONLY || mode & O_RDWR || | |
1473 | mode & O_APPEND || mode & O_TRUNC) { | |
1474 | err = -EROFS; | |
1475 | goto out; | |
1476 | } | |
2c74c2cb | 1477 | } |
bccacf6c | 1478 | err = v9fs_co_open(pdu, fidp, flags); |
857bc158 AK |
1479 | if (err < 0) { |
1480 | goto out; | |
1481 | } | |
1482 | fidp->fid_type = P9_FID_FILE; | |
7a462745 AK |
1483 | fidp->open_flags = flags; |
1484 | if (flags & O_EXCL) { | |
1485 | /* | |
1486 | * We let the host file system do O_EXCL check | |
1487 | * We should not reclaim such fd | |
1488 | */ | |
1489 | fidp->flags |= FID_NON_RECLAIMABLE; | |
1490 | } | |
bccacf6c | 1491 | iounit = get_iounit(pdu, &fidp->path); |
ddca7f86 MK |
1492 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); |
1493 | if (err < 0) { | |
1494 | goto out; | |
1495 | } | |
1496 | err += offset; | |
a6568fe2 | 1497 | } |
7999f7e1 AK |
1498 | trace_v9fs_open_return(pdu->tag, pdu->id, |
1499 | qid.type, qid.version, qid.path, iounit); | |
a6568fe2 | 1500 | out: |
bccacf6c | 1501 | put_fid(pdu, fidp); |
84dfb926 | 1502 | out_nofid: |
dc295f83 | 1503 | pdu_complete(pdu, err); |
a6568fe2 AL |
1504 | } |
1505 | ||
8440e22e | 1506 | static void coroutine_fn v9fs_lcreate(void *opaque) |
c1568af5 VJ |
1507 | { |
1508 | int32_t dfid, flags, mode; | |
1509 | gid_t gid; | |
c1568af5 | 1510 | ssize_t err = 0; |
36f8981f | 1511 | ssize_t offset = 7; |
36f8981f VJ |
1512 | V9fsString name; |
1513 | V9fsFidState *fidp; | |
1514 | struct stat stbuf; | |
1515 | V9fsQID qid; | |
1516 | int32_t iounit; | |
1517 | V9fsPDU *pdu = opaque; | |
c1568af5 | 1518 | |
ddca7f86 MK |
1519 | v9fs_string_init(&name); |
1520 | err = pdu_unmarshal(pdu, offset, "dsddd", &dfid, | |
1521 | &name, &flags, &mode, &gid); | |
1522 | if (err < 0) { | |
1523 | goto out_nofid; | |
1524 | } | |
c572f23a | 1525 | trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid); |
c1568af5 | 1526 | |
fff39a7a GK |
1527 | if (name_is_illegal(name.data)) { |
1528 | err = -ENOENT; | |
1529 | goto out_nofid; | |
1530 | } | |
1531 | ||
805b5d98 GK |
1532 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
1533 | err = -EEXIST; | |
1534 | goto out_nofid; | |
1535 | } | |
1536 | ||
bccacf6c | 1537 | fidp = get_fid(pdu, dfid); |
36f8981f | 1538 | if (fidp == NULL) { |
c1568af5 | 1539 | err = -ENOENT; |
84dfb926 | 1540 | goto out_nofid; |
c1568af5 | 1541 | } |
c1568af5 | 1542 | |
d3ab98e6 | 1543 | flags = get_dotl_openflags(pdu->s, flags); |
bccacf6c | 1544 | err = v9fs_co_open2(pdu, fidp, &name, gid, |
02cb7f3a | 1545 | flags | O_CREAT, mode, &stbuf); |
36f8981f VJ |
1546 | if (err < 0) { |
1547 | goto out; | |
1548 | } | |
1549 | fidp->fid_type = P9_FID_FILE; | |
7a462745 AK |
1550 | fidp->open_flags = flags; |
1551 | if (flags & O_EXCL) { | |
1552 | /* | |
1553 | * We let the host file system do O_EXCL check | |
1554 | * We should not reclaim such fd | |
1555 | */ | |
1556 | fidp->flags |= FID_NON_RECLAIMABLE; | |
1557 | } | |
bccacf6c | 1558 | iounit = get_iounit(pdu, &fidp->path); |
36f8981f | 1559 | stat_to_qid(&stbuf, &qid); |
ddca7f86 MK |
1560 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); |
1561 | if (err < 0) { | |
1562 | goto out; | |
1563 | } | |
1564 | err += offset; | |
7999f7e1 AK |
1565 | trace_v9fs_lcreate_return(pdu->tag, pdu->id, |
1566 | qid.type, qid.version, qid.path, iounit); | |
c1568af5 | 1567 | out: |
bccacf6c | 1568 | put_fid(pdu, fidp); |
84dfb926 | 1569 | out_nofid: |
dc295f83 | 1570 | pdu_complete(pdu, err); |
36f8981f | 1571 | v9fs_string_free(&name); |
c1568af5 VJ |
1572 | } |
1573 | ||
ff06030f | 1574 | static void v9fs_fsync(void *opaque) |
b41e95d3 | 1575 | { |
4e9ad444 | 1576 | int err; |
b41e95d3 | 1577 | int32_t fid; |
4e9ad444 | 1578 | int datasync; |
b41e95d3 VJ |
1579 | size_t offset = 7; |
1580 | V9fsFidState *fidp; | |
4e9ad444 | 1581 | V9fsPDU *pdu = opaque; |
b41e95d3 | 1582 | |
ddca7f86 MK |
1583 | err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); |
1584 | if (err < 0) { | |
1585 | goto out_nofid; | |
1586 | } | |
c572f23a HPB |
1587 | trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync); |
1588 | ||
bccacf6c | 1589 | fidp = get_fid(pdu, fid); |
b41e95d3 VJ |
1590 | if (fidp == NULL) { |
1591 | err = -ENOENT; | |
84dfb926 | 1592 | goto out_nofid; |
b41e95d3 | 1593 | } |
bccacf6c | 1594 | err = v9fs_co_fsync(pdu, fidp, datasync); |
4e9ad444 AK |
1595 | if (!err) { |
1596 | err = offset; | |
1597 | } | |
bccacf6c | 1598 | put_fid(pdu, fidp); |
84dfb926 | 1599 | out_nofid: |
dc295f83 | 1600 | pdu_complete(pdu, err); |
b41e95d3 VJ |
1601 | } |
1602 | ||
8440e22e | 1603 | static void coroutine_fn v9fs_clunk(void *opaque) |
a6568fe2 | 1604 | { |
c540ee51 | 1605 | int err; |
bbd5697b AL |
1606 | int32_t fid; |
1607 | size_t offset = 7; | |
84dfb926 | 1608 | V9fsFidState *fidp; |
c540ee51 AK |
1609 | V9fsPDU *pdu = opaque; |
1610 | V9fsState *s = pdu->s; | |
bbd5697b | 1611 | |
ddca7f86 MK |
1612 | err = pdu_unmarshal(pdu, offset, "d", &fid); |
1613 | if (err < 0) { | |
1614 | goto out_nofid; | |
1615 | } | |
c572f23a | 1616 | trace_v9fs_clunk(pdu->tag, pdu->id, fid); |
84dfb926 | 1617 | |
ce421a19 | 1618 | fidp = clunk_fid(s, fid); |
84dfb926 AK |
1619 | if (fidp == NULL) { |
1620 | err = -ENOENT; | |
1621 | goto out_nofid; | |
1622 | } | |
ce421a19 AK |
1623 | /* |
1624 | * Bump the ref so that put_fid will | |
1625 | * free the fid. | |
1626 | */ | |
1627 | fidp->ref++; | |
a911a182 AK |
1628 | err = put_fid(pdu, fidp); |
1629 | if (!err) { | |
1630 | err = offset; | |
1631 | } | |
84dfb926 | 1632 | out_nofid: |
dc295f83 | 1633 | pdu_complete(pdu, err); |
9f107513 AL |
1634 | } |
1635 | ||
bcb8998f SS |
1636 | /* |
1637 | * Create a QEMUIOVector for a sub-region of PDU iovecs | |
1638 | * | |
1639 | * @qiov: uninitialized QEMUIOVector | |
1640 | * @skip: number of bytes to skip from beginning of PDU | |
1641 | * @size: number of bytes to include | |
1642 | * @is_write: true - write, false - read | |
1643 | * | |
1644 | * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up | |
1645 | * with qemu_iovec_destroy(). | |
1646 | */ | |
1647 | static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu, | |
1648 | size_t skip, size_t size, | |
1649 | bool is_write) | |
1650 | { | |
1651 | QEMUIOVector elem; | |
1652 | struct iovec *iov; | |
1653 | unsigned int niov; | |
1654 | ||
88da0b03 SS |
1655 | if (is_write) { |
1656 | pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov); | |
1657 | } else { | |
1658 | pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size); | |
1659 | } | |
bcb8998f SS |
1660 | |
1661 | qemu_iovec_init_external(&elem, iov, niov); | |
1662 | qemu_iovec_init(qiov, niov); | |
1663 | qemu_iovec_concat(qiov, &elem, skip, size); | |
1664 | } | |
1665 | ||
2f008a8c AK |
1666 | static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, |
1667 | uint64_t off, uint32_t max_count) | |
a9231555 | 1668 | { |
ddca7f86 | 1669 | ssize_t err; |
d208a0e0 | 1670 | size_t offset = 7; |
7e55d65c | 1671 | uint64_t read_count; |
bcb8998f | 1672 | QEMUIOVector qiov_full; |
a9231555 | 1673 | |
7e55d65c LQ |
1674 | if (fidp->fs.xattr.len < off) { |
1675 | read_count = 0; | |
1676 | } else { | |
1677 | read_count = fidp->fs.xattr.len - off; | |
1678 | } | |
d208a0e0 AK |
1679 | if (read_count > max_count) { |
1680 | read_count = max_count; | |
a9231555 | 1681 | } |
ddca7f86 MK |
1682 | err = pdu_marshal(pdu, offset, "d", read_count); |
1683 | if (err < 0) { | |
1684 | return err; | |
1685 | } | |
1686 | offset += err; | |
00588a0a | 1687 | |
bcb8998f SS |
1688 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, 0, read_count, false); |
1689 | err = v9fs_pack(qiov_full.iov, qiov_full.niov, offset, | |
ddca7f86 MK |
1690 | ((char *)fidp->fs.xattr.value) + off, |
1691 | read_count); | |
bcb8998f | 1692 | qemu_iovec_destroy(&qiov_full); |
ddca7f86 MK |
1693 | if (err < 0) { |
1694 | return err; | |
1695 | } | |
1696 | offset += err; | |
d208a0e0 | 1697 | return offset; |
a9231555 AL |
1698 | } |
1699 | ||
8440e22e GK |
1700 | static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, |
1701 | V9fsFidState *fidp, | |
1702 | uint32_t max_count) | |
a9231555 | 1703 | { |
2289be19 | 1704 | V9fsPath path; |
d208a0e0 AK |
1705 | V9fsStat v9stat; |
1706 | int len, err = 0; | |
1707 | int32_t count = 0; | |
1708 | struct stat stbuf; | |
1709 | off_t saved_dir_pos; | |
635324e8 | 1710 | struct dirent *dent; |
a9231555 | 1711 | |
d208a0e0 | 1712 | /* save the directory position */ |
bccacf6c | 1713 | saved_dir_pos = v9fs_co_telldir(pdu, fidp); |
d208a0e0 AK |
1714 | if (saved_dir_pos < 0) { |
1715 | return saved_dir_pos; | |
a9231555 | 1716 | } |
5f524c1e | 1717 | |
d208a0e0 | 1718 | while (1) { |
2289be19 | 1719 | v9fs_path_init(&path); |
7cde47d4 GK |
1720 | |
1721 | v9fs_readdir_lock(&fidp->fs.dir); | |
1722 | ||
635324e8 GK |
1723 | err = v9fs_co_readdir(pdu, fidp, &dent); |
1724 | if (err || !dent) { | |
d208a0e0 AK |
1725 | break; |
1726 | } | |
bccacf6c | 1727 | err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); |
d208a0e0 | 1728 | if (err < 0) { |
8762a46d | 1729 | break; |
d208a0e0 | 1730 | } |
bccacf6c | 1731 | err = v9fs_co_lstat(pdu, &path, &stbuf); |
2289be19 | 1732 | if (err < 0) { |
8762a46d | 1733 | break; |
2289be19 | 1734 | } |
bccacf6c | 1735 | err = stat_to_v9stat(pdu, &path, &stbuf, &v9stat); |
d208a0e0 | 1736 | if (err < 0) { |
8762a46d | 1737 | break; |
a9231555 | 1738 | } |
d208a0e0 AK |
1739 | /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ |
1740 | len = pdu_marshal(pdu, 11 + count, "S", &v9stat); | |
7cde47d4 GK |
1741 | |
1742 | v9fs_readdir_unlock(&fidp->fs.dir); | |
1743 | ||
d208a0e0 AK |
1744 | if ((len != (v9stat.size + 2)) || ((count + len) > max_count)) { |
1745 | /* Ran out of buffer. Set dir back to old position and return */ | |
bccacf6c | 1746 | v9fs_co_seekdir(pdu, fidp, saved_dir_pos); |
d208a0e0 | 1747 | v9fs_stat_free(&v9stat); |
2289be19 | 1748 | v9fs_path_free(&path); |
d208a0e0 AK |
1749 | return count; |
1750 | } | |
1751 | count += len; | |
1752 | v9fs_stat_free(&v9stat); | |
2289be19 | 1753 | v9fs_path_free(&path); |
d208a0e0 | 1754 | saved_dir_pos = dent->d_off; |
a9231555 | 1755 | } |
8762a46d | 1756 | |
7cde47d4 GK |
1757 | v9fs_readdir_unlock(&fidp->fs.dir); |
1758 | ||
2289be19 | 1759 | v9fs_path_free(&path); |
d208a0e0 AK |
1760 | if (err < 0) { |
1761 | return err; | |
fa32ef88 | 1762 | } |
d208a0e0 | 1763 | return count; |
fa32ef88 AK |
1764 | } |
1765 | ||
8440e22e | 1766 | static void coroutine_fn v9fs_read(void *opaque) |
9f107513 | 1767 | { |
a9231555 | 1768 | int32_t fid; |
2f008a8c | 1769 | uint64_t off; |
a9231555 | 1770 | ssize_t err = 0; |
d208a0e0 AK |
1771 | int32_t count = 0; |
1772 | size_t offset = 7; | |
2f008a8c | 1773 | uint32_t max_count; |
d208a0e0 AK |
1774 | V9fsFidState *fidp; |
1775 | V9fsPDU *pdu = opaque; | |
1776 | V9fsState *s = pdu->s; | |
a9231555 | 1777 | |
ddca7f86 MK |
1778 | err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); |
1779 | if (err < 0) { | |
1780 | goto out_nofid; | |
1781 | } | |
c572f23a | 1782 | trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count); |
84dfb926 | 1783 | |
bccacf6c | 1784 | fidp = get_fid(pdu, fid); |
d208a0e0 | 1785 | if (fidp == NULL) { |
a9231555 | 1786 | err = -EINVAL; |
84dfb926 | 1787 | goto out_nofid; |
a9231555 | 1788 | } |
d208a0e0 | 1789 | if (fidp->fid_type == P9_FID_DIR) { |
a9231555 | 1790 | |
d208a0e0 | 1791 | if (off == 0) { |
bccacf6c | 1792 | v9fs_co_rewinddir(pdu, fidp); |
a9231555 | 1793 | } |
bccacf6c | 1794 | count = v9fs_do_readdir_with_stat(pdu, fidp, max_count); |
d208a0e0 AK |
1795 | if (count < 0) { |
1796 | err = count; | |
1797 | goto out; | |
56d15a53 | 1798 | } |
ddca7f86 MK |
1799 | err = pdu_marshal(pdu, offset, "d", count); |
1800 | if (err < 0) { | |
1801 | goto out; | |
1802 | } | |
1803 | err += offset + count; | |
d208a0e0 | 1804 | } else if (fidp->fid_type == P9_FID_FILE) { |
302a0d3e SH |
1805 | QEMUIOVector qiov_full; |
1806 | QEMUIOVector qiov; | |
d208a0e0 | 1807 | int32_t len; |
d208a0e0 | 1808 | |
302a0d3e SH |
1809 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false); |
1810 | qemu_iovec_init(&qiov, qiov_full.niov); | |
d208a0e0 | 1811 | do { |
302a0d3e | 1812 | qemu_iovec_reset(&qiov); |
1b093c48 | 1813 | qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count); |
d208a0e0 | 1814 | if (0) { |
302a0d3e | 1815 | print_sg(qiov.iov, qiov.niov); |
d208a0e0 AK |
1816 | } |
1817 | /* Loop in case of EINTR */ | |
1818 | do { | |
302a0d3e | 1819 | len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off); |
d208a0e0 AK |
1820 | if (len >= 0) { |
1821 | off += len; | |
1822 | count += len; | |
1823 | } | |
bccacf6c | 1824 | } while (len == -EINTR && !pdu->cancelled); |
d208a0e0 AK |
1825 | if (len < 0) { |
1826 | /* IO error return the error */ | |
1827 | err = len; | |
e95c9a49 | 1828 | goto out_free_iovec; |
d208a0e0 | 1829 | } |
d208a0e0 | 1830 | } while (count < max_count && len > 0); |
ddca7f86 MK |
1831 | err = pdu_marshal(pdu, offset, "d", count); |
1832 | if (err < 0) { | |
e95c9a49 | 1833 | goto out_free_iovec; |
ddca7f86 MK |
1834 | } |
1835 | err += offset + count; | |
e95c9a49 | 1836 | out_free_iovec: |
302a0d3e SH |
1837 | qemu_iovec_destroy(&qiov); |
1838 | qemu_iovec_destroy(&qiov_full); | |
d208a0e0 AK |
1839 | } else if (fidp->fid_type == P9_FID_XATTR) { |
1840 | err = v9fs_xattr_read(s, pdu, fidp, off, max_count); | |
a9231555 AL |
1841 | } else { |
1842 | err = -EINVAL; | |
9f107513 | 1843 | } |
7999f7e1 | 1844 | trace_v9fs_read_return(pdu->tag, pdu->id, count, err); |
a9231555 | 1845 | out: |
bccacf6c | 1846 | put_fid(pdu, fidp); |
84dfb926 | 1847 | out_nofid: |
dc295f83 | 1848 | pdu_complete(pdu, err); |
9f107513 AL |
1849 | } |
1850 | ||
5e4eaa79 | 1851 | static size_t v9fs_readdir_data_size(V9fsString *name) |
c18e2f94 | 1852 | { |
5e4eaa79 AK |
1853 | /* |
1854 | * Size of each dirent on the wire: size of qid (13) + size of offset (8) | |
1855 | * size of type (1) + size of name.size (2) + strlen(name.data) | |
1856 | */ | |
1857 | return 24 + v9fs_string_size(name); | |
c18e2f94 SK |
1858 | } |
1859 | ||
8440e22e GK |
1860 | static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, |
1861 | int32_t max_count) | |
c18e2f94 | 1862 | { |
c18e2f94 | 1863 | size_t size; |
5e4eaa79 AK |
1864 | V9fsQID qid; |
1865 | V9fsString name; | |
1866 | int len, err = 0; | |
1867 | int32_t count = 0; | |
1868 | off_t saved_dir_pos; | |
635324e8 | 1869 | struct dirent *dent; |
c18e2f94 | 1870 | |
5e4eaa79 | 1871 | /* save the directory position */ |
bccacf6c | 1872 | saved_dir_pos = v9fs_co_telldir(pdu, fidp); |
5e4eaa79 AK |
1873 | if (saved_dir_pos < 0) { |
1874 | return saved_dir_pos; | |
1875 | } | |
5f524c1e | 1876 | |
5e4eaa79 | 1877 | while (1) { |
7cde47d4 GK |
1878 | v9fs_readdir_lock(&fidp->fs.dir); |
1879 | ||
635324e8 GK |
1880 | err = v9fs_co_readdir(pdu, fidp, &dent); |
1881 | if (err || !dent) { | |
5e4eaa79 AK |
1882 | break; |
1883 | } | |
1884 | v9fs_string_init(&name); | |
1885 | v9fs_string_sprintf(&name, "%s", dent->d_name); | |
1886 | if ((count + v9fs_readdir_data_size(&name)) > max_count) { | |
7cde47d4 GK |
1887 | v9fs_readdir_unlock(&fidp->fs.dir); |
1888 | ||
c18e2f94 | 1889 | /* Ran out of buffer. Set dir back to old position and return */ |
bccacf6c | 1890 | v9fs_co_seekdir(pdu, fidp, saved_dir_pos); |
5e4eaa79 AK |
1891 | v9fs_string_free(&name); |
1892 | return count; | |
c18e2f94 | 1893 | } |
5e4eaa79 AK |
1894 | /* |
1895 | * Fill up just the path field of qid because the client uses | |
c18e2f94 SK |
1896 | * only that. To fill the entire qid structure we will have |
1897 | * to stat each dirent found, which is expensive | |
1898 | */ | |
5e4eaa79 AK |
1899 | size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); |
1900 | memcpy(&qid.path, &dent->d_ino, size); | |
c18e2f94 | 1901 | /* Fill the other fields with dummy values */ |
5e4eaa79 AK |
1902 | qid.type = 0; |
1903 | qid.version = 0; | |
c18e2f94 | 1904 | |
5e4eaa79 AK |
1905 | /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ |
1906 | len = pdu_marshal(pdu, 11 + count, "Qqbs", | |
1907 | &qid, dent->d_off, | |
1908 | dent->d_type, &name); | |
7cde47d4 GK |
1909 | |
1910 | v9fs_readdir_unlock(&fidp->fs.dir); | |
1911 | ||
ddca7f86 MK |
1912 | if (len < 0) { |
1913 | v9fs_co_seekdir(pdu, fidp, saved_dir_pos); | |
1914 | v9fs_string_free(&name); | |
ddca7f86 MK |
1915 | return len; |
1916 | } | |
5e4eaa79 AK |
1917 | count += len; |
1918 | v9fs_string_free(&name); | |
1919 | saved_dir_pos = dent->d_off; | |
1920 | } | |
7cde47d4 GK |
1921 | |
1922 | v9fs_readdir_unlock(&fidp->fs.dir); | |
1923 | ||
5e4eaa79 AK |
1924 | if (err < 0) { |
1925 | return err; | |
1926 | } | |
1927 | return count; | |
c18e2f94 SK |
1928 | } |
1929 | ||
8440e22e | 1930 | static void coroutine_fn v9fs_readdir(void *opaque) |
c18e2f94 SK |
1931 | { |
1932 | int32_t fid; | |
5e4eaa79 AK |
1933 | V9fsFidState *fidp; |
1934 | ssize_t retval = 0; | |
c18e2f94 | 1935 | size_t offset = 7; |
2f008a8c AK |
1936 | uint64_t initial_offset; |
1937 | int32_t count; | |
1938 | uint32_t max_count; | |
5e4eaa79 | 1939 | V9fsPDU *pdu = opaque; |
c18e2f94 | 1940 | |
ddca7f86 MK |
1941 | retval = pdu_unmarshal(pdu, offset, "dqd", &fid, |
1942 | &initial_offset, &max_count); | |
1943 | if (retval < 0) { | |
1944 | goto out_nofid; | |
1945 | } | |
c572f23a HPB |
1946 | trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count); |
1947 | ||
bccacf6c | 1948 | fidp = get_fid(pdu, fid); |
84dfb926 AK |
1949 | if (fidp == NULL) { |
1950 | retval = -EINVAL; | |
1951 | goto out_nofid; | |
1952 | } | |
f314ea4e | 1953 | if (!fidp->fs.dir.stream) { |
5e4eaa79 | 1954 | retval = -EINVAL; |
c18e2f94 SK |
1955 | goto out; |
1956 | } | |
5e4eaa79 | 1957 | if (initial_offset == 0) { |
bccacf6c | 1958 | v9fs_co_rewinddir(pdu, fidp); |
c18e2f94 | 1959 | } else { |
bccacf6c | 1960 | v9fs_co_seekdir(pdu, fidp, initial_offset); |
c18e2f94 | 1961 | } |
bccacf6c | 1962 | count = v9fs_do_readdir(pdu, fidp, max_count); |
5e4eaa79 AK |
1963 | if (count < 0) { |
1964 | retval = count; | |
1965 | goto out; | |
1966 | } | |
ddca7f86 MK |
1967 | retval = pdu_marshal(pdu, offset, "d", count); |
1968 | if (retval < 0) { | |
1969 | goto out; | |
1970 | } | |
1971 | retval += count + offset; | |
7999f7e1 | 1972 | trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval); |
c18e2f94 | 1973 | out: |
bccacf6c | 1974 | put_fid(pdu, fidp); |
84dfb926 | 1975 | out_nofid: |
dc295f83 | 1976 | pdu_complete(pdu, retval); |
c18e2f94 SK |
1977 | } |
1978 | ||
d7a90491 | 1979 | static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, |
2f008a8c | 1980 | uint64_t off, uint32_t count, |
d7a90491 | 1981 | struct iovec *sg, int cnt) |
10b468bd AK |
1982 | { |
1983 | int i, to_copy; | |
1984 | ssize_t err = 0; | |
7e55d65c | 1985 | uint64_t write_count; |
d7a90491 | 1986 | size_t offset = 7; |
10b468bd | 1987 | |
d7a90491 | 1988 | |
7e55d65c | 1989 | if (fidp->fs.xattr.len < off) { |
10b468bd AK |
1990 | err = -ENOSPC; |
1991 | goto out; | |
1992 | } | |
7e55d65c LQ |
1993 | write_count = fidp->fs.xattr.len - off; |
1994 | if (write_count > count) { | |
1995 | write_count = count; | |
1996 | } | |
ddca7f86 MK |
1997 | err = pdu_marshal(pdu, offset, "d", write_count); |
1998 | if (err < 0) { | |
1999 | return err; | |
2000 | } | |
2001 | err += offset; | |
d7a90491 | 2002 | fidp->fs.xattr.copied_len += write_count; |
10b468bd AK |
2003 | /* |
2004 | * Now copy the content from sg list | |
2005 | */ | |
d7a90491 AK |
2006 | for (i = 0; i < cnt; i++) { |
2007 | if (write_count > sg[i].iov_len) { | |
2008 | to_copy = sg[i].iov_len; | |
10b468bd AK |
2009 | } else { |
2010 | to_copy = write_count; | |
2011 | } | |
d7a90491 | 2012 | memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy); |
10b468bd | 2013 | /* updating vs->off since we are not using below */ |
d7a90491 | 2014 | off += to_copy; |
10b468bd AK |
2015 | write_count -= to_copy; |
2016 | } | |
2017 | out: | |
d7a90491 | 2018 | return err; |
10b468bd AK |
2019 | } |
2020 | ||
8440e22e | 2021 | static void coroutine_fn v9fs_write(void *opaque) |
9f107513 | 2022 | { |
d7a90491 AK |
2023 | ssize_t err; |
2024 | int32_t fid; | |
2f008a8c AK |
2025 | uint64_t off; |
2026 | uint32_t count; | |
d7a90491 AK |
2027 | int32_t len = 0; |
2028 | int32_t total = 0; | |
2029 | size_t offset = 7; | |
2030 | V9fsFidState *fidp; | |
ff06030f VJ |
2031 | V9fsPDU *pdu = opaque; |
2032 | V9fsState *s = pdu->s; | |
302a0d3e SH |
2033 | QEMUIOVector qiov_full; |
2034 | QEMUIOVector qiov; | |
8449360c | 2035 | |
ddca7f86 MK |
2036 | err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count); |
2037 | if (err < 0) { | |
dc295f83 | 2038 | pdu_complete(pdu, err); |
0289a412 | 2039 | return; |
ddca7f86 MK |
2040 | } |
2041 | offset += err; | |
302a0d3e SH |
2042 | v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true); |
2043 | trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov); | |
84dfb926 | 2044 | |
bccacf6c | 2045 | fidp = get_fid(pdu, fid); |
d7a90491 | 2046 | if (fidp == NULL) { |
8449360c | 2047 | err = -EINVAL; |
84dfb926 | 2048 | goto out_nofid; |
9f107513 | 2049 | } |
d7a90491 AK |
2050 | if (fidp->fid_type == P9_FID_FILE) { |
2051 | if (fidp->fs.fd == -1) { | |
10b468bd AK |
2052 | err = -EINVAL; |
2053 | goto out; | |
2054 | } | |
d7a90491 | 2055 | } else if (fidp->fid_type == P9_FID_XATTR) { |
10b468bd AK |
2056 | /* |
2057 | * setxattr operation | |
2058 | */ | |
302a0d3e SH |
2059 | err = v9fs_xattr_write(s, pdu, fidp, off, count, |
2060 | qiov_full.iov, qiov_full.niov); | |
d7a90491 | 2061 | goto out; |
10b468bd | 2062 | } else { |
8449360c AL |
2063 | err = -EINVAL; |
2064 | goto out; | |
2065 | } | |
302a0d3e | 2066 | qemu_iovec_init(&qiov, qiov_full.niov); |
d7a90491 | 2067 | do { |
302a0d3e | 2068 | qemu_iovec_reset(&qiov); |
1b093c48 | 2069 | qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total); |
d7a90491 | 2070 | if (0) { |
302a0d3e | 2071 | print_sg(qiov.iov, qiov.niov); |
56d15a53 | 2072 | } |
d7a90491 AK |
2073 | /* Loop in case of EINTR */ |
2074 | do { | |
302a0d3e | 2075 | len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off); |
d7a90491 AK |
2076 | if (len >= 0) { |
2077 | off += len; | |
2078 | total += len; | |
2079 | } | |
bccacf6c | 2080 | } while (len == -EINTR && !pdu->cancelled); |
d7a90491 AK |
2081 | if (len < 0) { |
2082 | /* IO error return the error */ | |
2083 | err = len; | |
302a0d3e | 2084 | goto out_qiov; |
d7a90491 | 2085 | } |
d7a90491 | 2086 | } while (total < count && len > 0); |
302a0d3e SH |
2087 | |
2088 | offset = 7; | |
ddca7f86 MK |
2089 | err = pdu_marshal(pdu, offset, "d", total); |
2090 | if (err < 0) { | |
fdfcc9ae | 2091 | goto out_qiov; |
ddca7f86 MK |
2092 | } |
2093 | err += offset; | |
7999f7e1 | 2094 | trace_v9fs_write_return(pdu->tag, pdu->id, total, err); |
302a0d3e SH |
2095 | out_qiov: |
2096 | qemu_iovec_destroy(&qiov); | |
8449360c | 2097 | out: |
bccacf6c | 2098 | put_fid(pdu, fidp); |
84dfb926 | 2099 | out_nofid: |
302a0d3e | 2100 | qemu_iovec_destroy(&qiov_full); |
dc295f83 | 2101 | pdu_complete(pdu, err); |
9f107513 AL |
2102 | } |
2103 | ||
8440e22e | 2104 | static void coroutine_fn v9fs_create(void *opaque) |
5e94c103 | 2105 | { |
baaa86d9 VJ |
2106 | int32_t fid; |
2107 | int err = 0; | |
2108 | size_t offset = 7; | |
2109 | V9fsFidState *fidp; | |
2110 | V9fsQID qid; | |
2111 | int32_t perm; | |
2112 | int8_t mode; | |
2289be19 | 2113 | V9fsPath path; |
baaa86d9 VJ |
2114 | struct stat stbuf; |
2115 | V9fsString name; | |
2116 | V9fsString extension; | |
baaa86d9 VJ |
2117 | int iounit; |
2118 | V9fsPDU *pdu = opaque; | |
c494dd6f | 2119 | |
2289be19 | 2120 | v9fs_path_init(&path); |
ddca7f86 MK |
2121 | v9fs_string_init(&name); |
2122 | v9fs_string_init(&extension); | |
2123 | err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, | |
2124 | &perm, &mode, &extension); | |
2125 | if (err < 0) { | |
2126 | goto out_nofid; | |
2127 | } | |
c572f23a HPB |
2128 | trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode); |
2129 | ||
fff39a7a GK |
2130 | if (name_is_illegal(name.data)) { |
2131 | err = -ENOENT; | |
2132 | goto out_nofid; | |
2133 | } | |
2134 | ||
805b5d98 GK |
2135 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
2136 | err = -EEXIST; | |
2137 | goto out_nofid; | |
2138 | } | |
2139 | ||
bccacf6c | 2140 | fidp = get_fid(pdu, fid); |
baaa86d9 VJ |
2141 | if (fidp == NULL) { |
2142 | err = -EINVAL; | |
84dfb926 | 2143 | goto out_nofid; |
c494dd6f | 2144 | } |
baaa86d9 | 2145 | if (perm & P9_STAT_MODE_DIR) { |
bccacf6c | 2146 | err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777, |
02cb7f3a | 2147 | fidp->uid, -1, &stbuf); |
baaa86d9 VJ |
2148 | if (err < 0) { |
2149 | goto out; | |
2150 | } | |
bccacf6c | 2151 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
2289be19 AK |
2152 | if (err < 0) { |
2153 | goto out; | |
2154 | } | |
2155 | v9fs_path_copy(&fidp->path, &path); | |
bccacf6c | 2156 | err = v9fs_co_opendir(pdu, fidp); |
baaa86d9 VJ |
2157 | if (err < 0) { |
2158 | goto out; | |
2159 | } | |
2160 | fidp->fid_type = P9_FID_DIR; | |
2161 | } else if (perm & P9_STAT_MODE_SYMLINK) { | |
bccacf6c | 2162 | err = v9fs_co_symlink(pdu, fidp, &name, |
02cb7f3a | 2163 | extension.data, -1 , &stbuf); |
baaa86d9 | 2164 | if (err < 0) { |
baaa86d9 VJ |
2165 | goto out; |
2166 | } | |
bccacf6c | 2167 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
2289be19 AK |
2168 | if (err < 0) { |
2169 | goto out; | |
2170 | } | |
2171 | v9fs_path_copy(&fidp->path, &path); | |
baaa86d9 | 2172 | } else if (perm & P9_STAT_MODE_LINK) { |
2289be19 | 2173 | int32_t ofid = atoi(extension.data); |
bccacf6c | 2174 | V9fsFidState *ofidp = get_fid(pdu, ofid); |
2289be19 | 2175 | if (ofidp == NULL) { |
baaa86d9 VJ |
2176 | err = -EINVAL; |
2177 | goto out; | |
2178 | } | |
bccacf6c AK |
2179 | err = v9fs_co_link(pdu, ofidp, fidp, &name); |
2180 | put_fid(pdu, ofidp); | |
2289be19 AK |
2181 | if (err < 0) { |
2182 | goto out; | |
2183 | } | |
bccacf6c | 2184 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
baaa86d9 | 2185 | if (err < 0) { |
2289be19 | 2186 | fidp->fid_type = P9_FID_NONE; |
baaa86d9 | 2187 | goto out; |
c494dd6f | 2188 | } |
2289be19 | 2189 | v9fs_path_copy(&fidp->path, &path); |
bccacf6c | 2190 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
02cb7f3a AK |
2191 | if (err < 0) { |
2192 | fidp->fid_type = P9_FID_NONE; | |
2193 | goto out; | |
2194 | } | |
baaa86d9 | 2195 | } else if (perm & P9_STAT_MODE_DEVICE) { |
c494dd6f AL |
2196 | char ctype; |
2197 | uint32_t major, minor; | |
2198 | mode_t nmode = 0; | |
2199 | ||
baaa86d9 | 2200 | if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) { |
c494dd6f | 2201 | err = -errno; |
baaa86d9 | 2202 | goto out; |
c494dd6f AL |
2203 | } |
2204 | ||
2205 | switch (ctype) { | |
2206 | case 'c': | |
2207 | nmode = S_IFCHR; | |
2208 | break; | |
2209 | case 'b': | |
2210 | nmode = S_IFBLK; | |
2211 | break; | |
2212 | default: | |
2213 | err = -EIO; | |
baaa86d9 VJ |
2214 | goto out; |
2215 | } | |
c1568af5 | 2216 | |
baaa86d9 | 2217 | nmode |= perm & 0777; |
bccacf6c | 2218 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, |
02cb7f3a | 2219 | makedev(major, minor), nmode, &stbuf); |
baaa86d9 VJ |
2220 | if (err < 0) { |
2221 | goto out; | |
2222 | } | |
bccacf6c | 2223 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
2289be19 AK |
2224 | if (err < 0) { |
2225 | goto out; | |
2226 | } | |
2227 | v9fs_path_copy(&fidp->path, &path); | |
baaa86d9 | 2228 | } else if (perm & P9_STAT_MODE_NAMED_PIPE) { |
bccacf6c | 2229 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, |
02cb7f3a | 2230 | 0, S_IFIFO | (perm & 0777), &stbuf); |
baaa86d9 VJ |
2231 | if (err < 0) { |
2232 | goto out; | |
2233 | } | |
bccacf6c | 2234 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
2289be19 AK |
2235 | if (err < 0) { |
2236 | goto out; | |
2237 | } | |
2238 | v9fs_path_copy(&fidp->path, &path); | |
baaa86d9 | 2239 | } else if (perm & P9_STAT_MODE_SOCKET) { |
bccacf6c | 2240 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, |
02cb7f3a | 2241 | 0, S_IFSOCK | (perm & 0777), &stbuf); |
baaa86d9 VJ |
2242 | if (err < 0) { |
2243 | goto out; | |
2244 | } | |
bccacf6c | 2245 | err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); |
2289be19 AK |
2246 | if (err < 0) { |
2247 | goto out; | |
2248 | } | |
2249 | v9fs_path_copy(&fidp->path, &path); | |
baaa86d9 | 2250 | } else { |
bccacf6c | 2251 | err = v9fs_co_open2(pdu, fidp, &name, -1, |
02cb7f3a | 2252 | omode_to_uflags(mode)|O_CREAT, perm, &stbuf); |
baaa86d9 VJ |
2253 | if (err < 0) { |
2254 | goto out; | |
2255 | } | |
2256 | fidp->fid_type = P9_FID_FILE; | |
7a462745 AK |
2257 | fidp->open_flags = omode_to_uflags(mode); |
2258 | if (fidp->open_flags & O_EXCL) { | |
2259 | /* | |
2260 | * We let the host file system do O_EXCL check | |
2261 | * We should not reclaim such fd | |
2262 | */ | |
2263 | fidp->flags |= FID_NON_RECLAIMABLE; | |
2264 | } | |
c494dd6f | 2265 | } |
bccacf6c | 2266 | iounit = get_iounit(pdu, &fidp->path); |
baaa86d9 | 2267 | stat_to_qid(&stbuf, &qid); |
ddca7f86 MK |
2268 | err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); |
2269 | if (err < 0) { | |
2270 | goto out; | |
2271 | } | |
2272 | err += offset; | |
7999f7e1 AK |
2273 | trace_v9fs_create_return(pdu->tag, pdu->id, |
2274 | qid.type, qid.version, qid.path, iounit); | |
c494dd6f | 2275 | out: |
bccacf6c | 2276 | put_fid(pdu, fidp); |
84dfb926 | 2277 | out_nofid: |
dc295f83 | 2278 | pdu_complete(pdu, err); |
baaa86d9 VJ |
2279 | v9fs_string_free(&name); |
2280 | v9fs_string_free(&extension); | |
2289be19 | 2281 | v9fs_path_free(&path); |
9f107513 AL |
2282 | } |
2283 | ||
8440e22e | 2284 | static void coroutine_fn v9fs_symlink(void *opaque) |
08c60fc9 | 2285 | { |
ff06030f | 2286 | V9fsPDU *pdu = opaque; |
3fa2a8d1 VJ |
2287 | V9fsString name; |
2288 | V9fsString symname; | |
3fa2a8d1 VJ |
2289 | V9fsFidState *dfidp; |
2290 | V9fsQID qid; | |
2291 | struct stat stbuf; | |
08c60fc9 | 2292 | int32_t dfid; |
08c60fc9 VJ |
2293 | int err = 0; |
2294 | gid_t gid; | |
3fa2a8d1 | 2295 | size_t offset = 7; |
08c60fc9 | 2296 | |
ddca7f86 MK |
2297 | v9fs_string_init(&name); |
2298 | v9fs_string_init(&symname); | |
2299 | err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); | |
2300 | if (err < 0) { | |
2301 | goto out_nofid; | |
2302 | } | |
c572f23a | 2303 | trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid); |
08c60fc9 | 2304 | |
fff39a7a GK |
2305 | if (name_is_illegal(name.data)) { |
2306 | err = -ENOENT; | |
2307 | goto out_nofid; | |
2308 | } | |
2309 | ||
805b5d98 GK |
2310 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
2311 | err = -EEXIST; | |
2312 | goto out_nofid; | |
2313 | } | |
2314 | ||
bccacf6c | 2315 | dfidp = get_fid(pdu, dfid); |
3fa2a8d1 | 2316 | if (dfidp == NULL) { |
08c60fc9 | 2317 | err = -EINVAL; |
84dfb926 | 2318 | goto out_nofid; |
08c60fc9 | 2319 | } |
bccacf6c | 2320 | err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf); |
3fa2a8d1 VJ |
2321 | if (err < 0) { |
2322 | goto out; | |
2323 | } | |
2324 | stat_to_qid(&stbuf, &qid); | |
ddca7f86 MK |
2325 | err = pdu_marshal(pdu, offset, "Q", &qid); |
2326 | if (err < 0) { | |
2327 | goto out; | |
2328 | } | |
2329 | err += offset; | |
7999f7e1 AK |
2330 | trace_v9fs_symlink_return(pdu->tag, pdu->id, |
2331 | qid.type, qid.version, qid.path); | |
08c60fc9 | 2332 | out: |
bccacf6c | 2333 | put_fid(pdu, dfidp); |
84dfb926 | 2334 | out_nofid: |
dc295f83 | 2335 | pdu_complete(pdu, err); |
3fa2a8d1 VJ |
2336 | v9fs_string_free(&name); |
2337 | v9fs_string_free(&symname); | |
08c60fc9 VJ |
2338 | } |
2339 | ||
ff06030f | 2340 | static void v9fs_flush(void *opaque) |
9f107513 | 2341 | { |
ddca7f86 | 2342 | ssize_t err; |
bccacf6c AK |
2343 | int16_t tag; |
2344 | size_t offset = 7; | |
2345 | V9fsPDU *cancel_pdu; | |
ff06030f VJ |
2346 | V9fsPDU *pdu = opaque; |
2347 | V9fsState *s = pdu->s; | |
bccacf6c | 2348 | |
ddca7f86 MK |
2349 | err = pdu_unmarshal(pdu, offset, "w", &tag); |
2350 | if (err < 0) { | |
dc295f83 | 2351 | pdu_complete(pdu, err); |
ddca7f86 MK |
2352 | return; |
2353 | } | |
c572f23a | 2354 | trace_v9fs_flush(pdu->tag, pdu->id, tag); |
bccacf6c AK |
2355 | |
2356 | QLIST_FOREACH(cancel_pdu, &s->active_list, next) { | |
2357 | if (cancel_pdu->tag == tag) { | |
2358 | break; | |
2359 | } | |
2360 | } | |
2361 | if (cancel_pdu) { | |
2362 | cancel_pdu->cancelled = 1; | |
2363 | /* | |
2364 | * Wait for pdu to complete. | |
2365 | */ | |
2366 | qemu_co_queue_wait(&cancel_pdu->complete); | |
2367 | cancel_pdu->cancelled = 0; | |
dc295f83 | 2368 | pdu_free(cancel_pdu); |
bccacf6c | 2369 | } |
dc295f83 | 2370 | pdu_complete(pdu, 7); |
9f107513 AL |
2371 | } |
2372 | ||
8440e22e | 2373 | static void coroutine_fn v9fs_link(void *opaque) |
b2c224be | 2374 | { |
ff06030f | 2375 | V9fsPDU *pdu = opaque; |
b2c224be VJ |
2376 | int32_t dfid, oldfid; |
2377 | V9fsFidState *dfidp, *oldfidp; | |
3a93113a | 2378 | V9fsString name; |
b2c224be VJ |
2379 | size_t offset = 7; |
2380 | int err = 0; | |
2381 | ||
ddca7f86 MK |
2382 | v9fs_string_init(&name); |
2383 | err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); | |
2384 | if (err < 0) { | |
2385 | goto out_nofid; | |
2386 | } | |
c572f23a | 2387 | trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data); |
b2c224be | 2388 | |
fff39a7a GK |
2389 | if (name_is_illegal(name.data)) { |
2390 | err = -ENOENT; | |
2391 | goto out_nofid; | |
2392 | } | |
2393 | ||
805b5d98 GK |
2394 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
2395 | err = -EEXIST; | |
2396 | goto out_nofid; | |
2397 | } | |
2398 | ||
bccacf6c | 2399 | dfidp = get_fid(pdu, dfid); |
b2c224be | 2400 | if (dfidp == NULL) { |
ffd66876 | 2401 | err = -ENOENT; |
84dfb926 | 2402 | goto out_nofid; |
b2c224be VJ |
2403 | } |
2404 | ||
bccacf6c | 2405 | oldfidp = get_fid(pdu, oldfid); |
b2c224be | 2406 | if (oldfidp == NULL) { |
ffd66876 | 2407 | err = -ENOENT; |
b2c224be VJ |
2408 | goto out; |
2409 | } | |
bccacf6c | 2410 | err = v9fs_co_link(pdu, oldfidp, dfidp, &name); |
ffd66876 VJ |
2411 | if (!err) { |
2412 | err = offset; | |
b2c224be | 2413 | } |
4c158678 | 2414 | put_fid(pdu, oldfidp); |
b2c224be | 2415 | out: |
bccacf6c | 2416 | put_fid(pdu, dfidp); |
84dfb926 | 2417 | out_nofid: |
b2c224be | 2418 | v9fs_string_free(&name); |
dc295f83 | 2419 | pdu_complete(pdu, err); |
b2c224be VJ |
2420 | } |
2421 | ||
532decb7 | 2422 | /* Only works with path name based fid */ |
8440e22e | 2423 | static void coroutine_fn v9fs_remove(void *opaque) |
9f107513 | 2424 | { |
5bae1900 | 2425 | int32_t fid; |
5bae1900 | 2426 | int err = 0; |
ae1ef571 VJ |
2427 | size_t offset = 7; |
2428 | V9fsFidState *fidp; | |
2429 | V9fsPDU *pdu = opaque; | |
5bae1900 | 2430 | |
ddca7f86 MK |
2431 | err = pdu_unmarshal(pdu, offset, "d", &fid); |
2432 | if (err < 0) { | |
2433 | goto out_nofid; | |
2434 | } | |
c572f23a | 2435 | trace_v9fs_remove(pdu->tag, pdu->id, fid); |
5bae1900 | 2436 | |
bccacf6c | 2437 | fidp = get_fid(pdu, fid); |
ae1ef571 | 2438 | if (fidp == NULL) { |
5bae1900 | 2439 | err = -EINVAL; |
84dfb926 | 2440 | goto out_nofid; |
9f107513 | 2441 | } |
532decb7 | 2442 | /* if fs driver is not path based, return EOPNOTSUPP */ |
c98f1d4a | 2443 | if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { |
532decb7 AK |
2444 | err = -EOPNOTSUPP; |
2445 | goto out_err; | |
2446 | } | |
7a462745 AK |
2447 | /* |
2448 | * IF the file is unlinked, we cannot reopen | |
2449 | * the file later. So don't reclaim fd | |
2450 | */ | |
bccacf6c | 2451 | err = v9fs_mark_fids_unreclaim(pdu, &fidp->path); |
7a462745 AK |
2452 | if (err < 0) { |
2453 | goto out_err; | |
2454 | } | |
bccacf6c | 2455 | err = v9fs_co_remove(pdu, &fidp->path); |
ae1ef571 VJ |
2456 | if (!err) { |
2457 | err = offset; | |
2458 | } | |
7a462745 | 2459 | out_err: |
ae1ef571 | 2460 | /* For TREMOVE we need to clunk the fid even on failed remove */ |
84dfb926 | 2461 | clunk_fid(pdu->s, fidp->fid); |
bccacf6c | 2462 | put_fid(pdu, fidp); |
84dfb926 | 2463 | out_nofid: |
dc295f83 | 2464 | pdu_complete(pdu, err); |
9f107513 AL |
2465 | } |
2466 | ||
8440e22e | 2467 | static void coroutine_fn v9fs_unlinkat(void *opaque) |
7834cf77 AK |
2468 | { |
2469 | int err = 0; | |
2470 | V9fsString name; | |
2471 | int32_t dfid, flags; | |
2472 | size_t offset = 7; | |
2289be19 | 2473 | V9fsPath path; |
7834cf77 AK |
2474 | V9fsFidState *dfidp; |
2475 | V9fsPDU *pdu = opaque; | |
7834cf77 | 2476 | |
ddca7f86 MK |
2477 | v9fs_string_init(&name); |
2478 | err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags); | |
2479 | if (err < 0) { | |
2480 | goto out_nofid; | |
2481 | } | |
fff39a7a GK |
2482 | |
2483 | if (name_is_illegal(name.data)) { | |
2484 | err = -ENOENT; | |
2485 | goto out_nofid; | |
2486 | } | |
2487 | ||
805b5d98 GK |
2488 | if (!strcmp(".", name.data)) { |
2489 | err = -EINVAL; | |
2490 | goto out_nofid; | |
2491 | } | |
2492 | ||
2493 | if (!strcmp("..", name.data)) { | |
2494 | err = -ENOTEMPTY; | |
2495 | goto out_nofid; | |
2496 | } | |
2497 | ||
bccacf6c | 2498 | dfidp = get_fid(pdu, dfid); |
7834cf77 AK |
2499 | if (dfidp == NULL) { |
2500 | err = -EINVAL; | |
2501 | goto out_nofid; | |
2502 | } | |
7834cf77 AK |
2503 | /* |
2504 | * IF the file is unlinked, we cannot reopen | |
2505 | * the file later. So don't reclaim fd | |
2506 | */ | |
2289be19 | 2507 | v9fs_path_init(&path); |
bccacf6c | 2508 | err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path); |
2289be19 AK |
2509 | if (err < 0) { |
2510 | goto out_err; | |
2511 | } | |
bccacf6c | 2512 | err = v9fs_mark_fids_unreclaim(pdu, &path); |
7834cf77 AK |
2513 | if (err < 0) { |
2514 | goto out_err; | |
2515 | } | |
bccacf6c | 2516 | err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, flags); |
7834cf77 AK |
2517 | if (!err) { |
2518 | err = offset; | |
2519 | } | |
2520 | out_err: | |
bccacf6c | 2521 | put_fid(pdu, dfidp); |
2289be19 | 2522 | v9fs_path_free(&path); |
7834cf77 | 2523 | out_nofid: |
dc295f83 | 2524 | pdu_complete(pdu, err); |
7834cf77 AK |
2525 | v9fs_string_free(&name); |
2526 | } | |
2527 | ||
2289be19 AK |
2528 | |
2529 | /* Only works with path name based fid */ | |
8440e22e GK |
2530 | static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp, |
2531 | int32_t newdirfid, | |
2532 | V9fsString *name) | |
8cf89e00 | 2533 | { |
930b1e17 | 2534 | char *end; |
c7b4b0b3 | 2535 | int err = 0; |
2289be19 AK |
2536 | V9fsPath new_path; |
2537 | V9fsFidState *tfidp; | |
bccacf6c | 2538 | V9fsState *s = pdu->s; |
84dfb926 | 2539 | V9fsFidState *dirfidp = NULL; |
c7b4b0b3 | 2540 | char *old_name, *new_name; |
8cf89e00 | 2541 | |
2289be19 | 2542 | v9fs_path_init(&new_path); |
930b1e17 | 2543 | if (newdirfid != -1) { |
bccacf6c | 2544 | dirfidp = get_fid(pdu, newdirfid); |
c7b4b0b3 MK |
2545 | if (dirfidp == NULL) { |
2546 | err = -ENOENT; | |
84dfb926 | 2547 | goto out_nofid; |
c7b4b0b3 | 2548 | } |
49dd946b GK |
2549 | if (fidp->fid_type != P9_FID_NONE) { |
2550 | err = -EINVAL; | |
2551 | goto out; | |
2552 | } | |
bccacf6c | 2553 | v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path); |
c7b4b0b3 | 2554 | } else { |
930b1e17 | 2555 | old_name = fidp->path.data; |
8cf89e00 AL |
2556 | end = strrchr(old_name, '/'); |
2557 | if (end) { | |
2558 | end++; | |
2559 | } else { | |
2560 | end = old_name; | |
2561 | } | |
7267c094 | 2562 | new_name = g_malloc0(end - old_name + name->size + 1); |
c7b4b0b3 | 2563 | strncat(new_name, old_name, end - old_name); |
930b1e17 | 2564 | strncat(new_name + (end - old_name), name->data, name->size); |
bccacf6c | 2565 | v9fs_co_name_to_path(pdu, NULL, new_name, &new_path); |
2289be19 | 2566 | g_free(new_name); |
c7b4b0b3 | 2567 | } |
bccacf6c | 2568 | err = v9fs_co_rename(pdu, &fidp->path, &new_path); |
2289be19 AK |
2569 | if (err < 0) { |
2570 | goto out; | |
2571 | } | |
2572 | /* | |
2573 | * Fixup fid's pointing to the old name to | |
2574 | * start pointing to the new name | |
2575 | */ | |
2576 | for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) { | |
2577 | if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { | |
2578 | /* replace the name */ | |
2579 | v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)); | |
8cf89e00 AL |
2580 | } |
2581 | } | |
c7b4b0b3 | 2582 | out: |
84dfb926 | 2583 | if (dirfidp) { |
bccacf6c | 2584 | put_fid(pdu, dirfidp); |
84dfb926 | 2585 | } |
2289be19 | 2586 | v9fs_path_free(&new_path); |
84dfb926 | 2587 | out_nofid: |
c7b4b0b3 MK |
2588 | return err; |
2589 | } | |
2590 | ||
532decb7 | 2591 | /* Only works with path name based fid */ |
8440e22e | 2592 | static void coroutine_fn v9fs_rename(void *opaque) |
c7b4b0b3 MK |
2593 | { |
2594 | int32_t fid; | |
c7b4b0b3 | 2595 | ssize_t err = 0; |
930b1e17 AK |
2596 | size_t offset = 7; |
2597 | V9fsString name; | |
2598 | int32_t newdirfid; | |
2599 | V9fsFidState *fidp; | |
2600 | V9fsPDU *pdu = opaque; | |
2601 | V9fsState *s = pdu->s; | |
c7b4b0b3 | 2602 | |
ddca7f86 MK |
2603 | v9fs_string_init(&name); |
2604 | err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); | |
2605 | if (err < 0) { | |
2606 | goto out_nofid; | |
2607 | } | |
fff39a7a GK |
2608 | |
2609 | if (name_is_illegal(name.data)) { | |
2610 | err = -ENOENT; | |
2611 | goto out_nofid; | |
2612 | } | |
2613 | ||
805b5d98 GK |
2614 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
2615 | err = -EISDIR; | |
2616 | goto out_nofid; | |
2617 | } | |
2618 | ||
bccacf6c | 2619 | fidp = get_fid(pdu, fid); |
930b1e17 | 2620 | if (fidp == NULL) { |
c7b4b0b3 | 2621 | err = -ENOENT; |
84dfb926 | 2622 | goto out_nofid; |
c7b4b0b3 | 2623 | } |
49dd946b GK |
2624 | if (fidp->fid_type != P9_FID_NONE) { |
2625 | err = -EINVAL; | |
2626 | goto out; | |
2627 | } | |
532decb7 | 2628 | /* if fs driver is not path based, return EOPNOTSUPP */ |
c98f1d4a | 2629 | if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { |
532decb7 AK |
2630 | err = -EOPNOTSUPP; |
2631 | goto out; | |
2632 | } | |
2633 | v9fs_path_write_lock(s); | |
bccacf6c | 2634 | err = v9fs_complete_rename(pdu, fidp, newdirfid, &name); |
532decb7 | 2635 | v9fs_path_unlock(s); |
930b1e17 AK |
2636 | if (!err) { |
2637 | err = offset; | |
2638 | } | |
532decb7 | 2639 | out: |
bccacf6c | 2640 | put_fid(pdu, fidp); |
84dfb926 | 2641 | out_nofid: |
dc295f83 | 2642 | pdu_complete(pdu, err); |
930b1e17 | 2643 | v9fs_string_free(&name); |
c7b4b0b3 MK |
2644 | } |
2645 | ||
8440e22e GK |
2646 | static void coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir, |
2647 | V9fsString *old_name, | |
2648 | V9fsPath *newdir, | |
2649 | V9fsString *new_name) | |
2289be19 AK |
2650 | { |
2651 | V9fsFidState *tfidp; | |
2652 | V9fsPath oldpath, newpath; | |
bccacf6c | 2653 | V9fsState *s = pdu->s; |
2289be19 AK |
2654 | |
2655 | ||
2656 | v9fs_path_init(&oldpath); | |
2657 | v9fs_path_init(&newpath); | |
bccacf6c AK |
2658 | v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath); |
2659 | v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath); | |
2289be19 AK |
2660 | |
2661 | /* | |
2662 | * Fixup fid's pointing to the old name to | |
2663 | * start pointing to the new name | |
2664 | */ | |
2665 | for (tfidp = s->fid_list; tfidp; tfidp = tfidp->next) { | |
2666 | if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { | |
2667 | /* replace the name */ | |
2668 | v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); | |
2669 | } | |
2670 | } | |
2671 | v9fs_path_free(&oldpath); | |
2672 | v9fs_path_free(&newpath); | |
2673 | } | |
2674 | ||
8440e22e GK |
2675 | static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid, |
2676 | V9fsString *old_name, | |
2677 | int32_t newdirfid, | |
2678 | V9fsString *new_name) | |
89bf6593 AK |
2679 | { |
2680 | int err = 0; | |
bccacf6c | 2681 | V9fsState *s = pdu->s; |
89bf6593 AK |
2682 | V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL; |
2683 | ||
bccacf6c | 2684 | olddirfidp = get_fid(pdu, olddirfid); |
89bf6593 AK |
2685 | if (olddirfidp == NULL) { |
2686 | err = -ENOENT; | |
2687 | goto out; | |
2688 | } | |
89bf6593 | 2689 | if (newdirfid != -1) { |
bccacf6c | 2690 | newdirfidp = get_fid(pdu, newdirfid); |
89bf6593 AK |
2691 | if (newdirfidp == NULL) { |
2692 | err = -ENOENT; | |
2693 | goto out; | |
2694 | } | |
89bf6593 | 2695 | } else { |
bccacf6c | 2696 | newdirfidp = get_fid(pdu, olddirfid); |
89bf6593 AK |
2697 | } |
2698 | ||
bccacf6c | 2699 | err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name, |
2289be19 AK |
2700 | &newdirfidp->path, new_name); |
2701 | if (err < 0) { | |
2702 | goto out; | |
89bf6593 | 2703 | } |
c98f1d4a | 2704 | if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { |
532decb7 | 2705 | /* Only for path based fid we need to do the below fixup */ |
bccacf6c | 2706 | v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name, |
532decb7 AK |
2707 | &newdirfidp->path, new_name); |
2708 | } | |
89bf6593 AK |
2709 | out: |
2710 | if (olddirfidp) { | |
bccacf6c | 2711 | put_fid(pdu, olddirfidp); |
89bf6593 AK |
2712 | } |
2713 | if (newdirfidp) { | |
bccacf6c | 2714 | put_fid(pdu, newdirfidp); |
89bf6593 | 2715 | } |
89bf6593 AK |
2716 | return err; |
2717 | } | |
2718 | ||
8440e22e | 2719 | static void coroutine_fn v9fs_renameat(void *opaque) |
89bf6593 AK |
2720 | { |
2721 | ssize_t err = 0; | |
2722 | size_t offset = 7; | |
2723 | V9fsPDU *pdu = opaque; | |
2724 | V9fsState *s = pdu->s; | |
2725 | int32_t olddirfid, newdirfid; | |
2726 | V9fsString old_name, new_name; | |
2727 | ||
ddca7f86 MK |
2728 | v9fs_string_init(&old_name); |
2729 | v9fs_string_init(&new_name); | |
2730 | err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid, | |
2731 | &old_name, &newdirfid, &new_name); | |
2732 | if (err < 0) { | |
2733 | goto out_err; | |
2734 | } | |
89bf6593 | 2735 | |
fff39a7a GK |
2736 | if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) { |
2737 | err = -ENOENT; | |
2738 | goto out_err; | |
2739 | } | |
2740 | ||
805b5d98 GK |
2741 | if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) || |
2742 | !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) { | |
2743 | err = -EISDIR; | |
2744 | goto out_err; | |
2745 | } | |
2746 | ||
532decb7 | 2747 | v9fs_path_write_lock(s); |
bccacf6c AK |
2748 | err = v9fs_complete_renameat(pdu, olddirfid, |
2749 | &old_name, newdirfid, &new_name); | |
532decb7 | 2750 | v9fs_path_unlock(s); |
89bf6593 AK |
2751 | if (!err) { |
2752 | err = offset; | |
2753 | } | |
ddca7f86 MK |
2754 | |
2755 | out_err: | |
dc295f83 | 2756 | pdu_complete(pdu, err); |
89bf6593 AK |
2757 | v9fs_string_free(&old_name); |
2758 | v9fs_string_free(&new_name); | |
2759 | } | |
2760 | ||
8440e22e | 2761 | static void coroutine_fn v9fs_wstat(void *opaque) |
8cf89e00 | 2762 | { |
b81d685e AK |
2763 | int32_t fid; |
2764 | int err = 0; | |
2765 | int16_t unused; | |
2766 | V9fsStat v9stat; | |
2767 | size_t offset = 7; | |
2768 | struct stat stbuf; | |
2769 | V9fsFidState *fidp; | |
2770 | V9fsPDU *pdu = opaque; | |
8cf89e00 | 2771 | |
ddca7f86 MK |
2772 | v9fs_stat_init(&v9stat); |
2773 | err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); | |
2774 | if (err < 0) { | |
2775 | goto out_nofid; | |
2776 | } | |
c572f23a HPB |
2777 | trace_v9fs_wstat(pdu->tag, pdu->id, fid, |
2778 | v9stat.mode, v9stat.atime, v9stat.mtime); | |
84dfb926 | 2779 | |
bccacf6c | 2780 | fidp = get_fid(pdu, fid); |
b81d685e AK |
2781 | if (fidp == NULL) { |
2782 | err = -EINVAL; | |
84dfb926 | 2783 | goto out_nofid; |
8cf89e00 | 2784 | } |
b81d685e AK |
2785 | /* do we need to sync the file? */ |
2786 | if (donttouch_stat(&v9stat)) { | |
bccacf6c | 2787 | err = v9fs_co_fsync(pdu, fidp, 0); |
8cf89e00 AL |
2788 | goto out; |
2789 | } | |
b81d685e AK |
2790 | if (v9stat.mode != -1) { |
2791 | uint32_t v9_mode; | |
bccacf6c | 2792 | err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); |
b81d685e AK |
2793 | if (err < 0) { |
2794 | goto out; | |
2795 | } | |
2796 | v9_mode = stat_to_v9mode(&stbuf); | |
2797 | if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) != | |
2798 | (v9_mode & P9_STAT_MODE_TYPE_BITS)) { | |
2799 | /* Attempting to change the type */ | |
2800 | err = -EIO; | |
2801 | goto out; | |
2802 | } | |
bccacf6c | 2803 | err = v9fs_co_chmod(pdu, &fidp->path, |
b81d685e AK |
2804 | v9mode_to_mode(v9stat.mode, |
2805 | &v9stat.extension)); | |
2806 | if (err < 0) { | |
2807 | goto out; | |
2808 | } | |
2809 | } | |
2810 | if (v9stat.mtime != -1 || v9stat.atime != -1) { | |
8fc39ae4 | 2811 | struct timespec times[2]; |
b81d685e AK |
2812 | if (v9stat.atime != -1) { |
2813 | times[0].tv_sec = v9stat.atime; | |
8fc39ae4 SK |
2814 | times[0].tv_nsec = 0; |
2815 | } else { | |
2816 | times[0].tv_nsec = UTIME_OMIT; | |
2817 | } | |
b81d685e AK |
2818 | if (v9stat.mtime != -1) { |
2819 | times[1].tv_sec = v9stat.mtime; | |
8fc39ae4 SK |
2820 | times[1].tv_nsec = 0; |
2821 | } else { | |
2822 | times[1].tv_nsec = UTIME_OMIT; | |
2823 | } | |
bccacf6c | 2824 | err = v9fs_co_utimensat(pdu, &fidp->path, times); |
b81d685e AK |
2825 | if (err < 0) { |
2826 | goto out; | |
8cf89e00 AL |
2827 | } |
2828 | } | |
b81d685e | 2829 | if (v9stat.n_gid != -1 || v9stat.n_uid != -1) { |
bccacf6c | 2830 | err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid); |
b81d685e | 2831 | if (err < 0) { |
8cf89e00 | 2832 | goto out; |
b81d685e | 2833 | } |
8cf89e00 | 2834 | } |
b81d685e | 2835 | if (v9stat.name.size != 0) { |
bccacf6c | 2836 | err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name); |
b81d685e AK |
2837 | if (err < 0) { |
2838 | goto out; | |
2839 | } | |
8cf89e00 | 2840 | } |
b81d685e | 2841 | if (v9stat.length != -1) { |
bccacf6c | 2842 | err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length); |
b81d685e AK |
2843 | if (err < 0) { |
2844 | goto out; | |
2845 | } | |
8cf89e00 | 2846 | } |
b81d685e | 2847 | err = offset; |
8cf89e00 | 2848 | out: |
bccacf6c | 2849 | put_fid(pdu, fidp); |
84dfb926 | 2850 | out_nofid: |
b81d685e | 2851 | v9fs_stat_free(&v9stat); |
dc295f83 | 2852 | pdu_complete(pdu, err); |
9f107513 AL |
2853 | } |
2854 | ||
88a4763e AK |
2855 | static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) |
2856 | { | |
2857 | uint32_t f_type; | |
2858 | uint32_t f_bsize; | |
2859 | uint64_t f_blocks; | |
2860 | uint64_t f_bfree; | |
2861 | uint64_t f_bavail; | |
2862 | uint64_t f_files; | |
2863 | uint64_t f_ffree; | |
2864 | uint64_t fsid_val; | |
2865 | uint32_t f_namelen; | |
2866 | size_t offset = 7; | |
5e94c103 MK |
2867 | int32_t bsize_factor; |
2868 | ||
5e94c103 MK |
2869 | /* |
2870 | * compute bsize factor based on host file system block size | |
2871 | * and client msize | |
2872 | */ | |
88a4763e | 2873 | bsize_factor = (s->msize - P9_IOHDRSZ)/stbuf->f_bsize; |
5e94c103 MK |
2874 | if (!bsize_factor) { |
2875 | bsize_factor = 1; | |
2876 | } | |
88a4763e AK |
2877 | f_type = stbuf->f_type; |
2878 | f_bsize = stbuf->f_bsize; | |
2879 | f_bsize *= bsize_factor; | |
5e94c103 MK |
2880 | /* |
2881 | * f_bsize is adjusted(multiplied) by bsize factor, so we need to | |
2882 | * adjust(divide) the number of blocks, free blocks and available | |
2883 | * blocks by bsize factor | |
2884 | */ | |
88a4763e AK |
2885 | f_blocks = stbuf->f_blocks/bsize_factor; |
2886 | f_bfree = stbuf->f_bfree/bsize_factor; | |
2887 | f_bavail = stbuf->f_bavail/bsize_factor; | |
2888 | f_files = stbuf->f_files; | |
2889 | f_ffree = stbuf->f_ffree; | |
2890 | fsid_val = (unsigned int) stbuf->f_fsid.__val[0] | | |
2891 | (unsigned long long)stbuf->f_fsid.__val[1] << 32; | |
2892 | f_namelen = stbuf->f_namelen; | |
be940c87 | 2893 | |
88a4763e AK |
2894 | return pdu_marshal(pdu, offset, "ddqqqqqqd", |
2895 | f_type, f_bsize, f_blocks, f_bfree, | |
2896 | f_bavail, f_files, f_ffree, | |
2897 | fsid_val, f_namelen); | |
be940c87 MK |
2898 | } |
2899 | ||
8440e22e | 2900 | static void coroutine_fn v9fs_statfs(void *opaque) |
be940c87 | 2901 | { |
88a4763e AK |
2902 | int32_t fid; |
2903 | ssize_t retval = 0; | |
2904 | size_t offset = 7; | |
2905 | V9fsFidState *fidp; | |
2906 | struct statfs stbuf; | |
ff06030f VJ |
2907 | V9fsPDU *pdu = opaque; |
2908 | V9fsState *s = pdu->s; | |
be940c87 | 2909 | |
ddca7f86 MK |
2910 | retval = pdu_unmarshal(pdu, offset, "d", &fid); |
2911 | if (retval < 0) { | |
2912 | goto out_nofid; | |
2913 | } | |
bccacf6c | 2914 | fidp = get_fid(pdu, fid); |
88a4763e AK |
2915 | if (fidp == NULL) { |
2916 | retval = -ENOENT; | |
84dfb926 | 2917 | goto out_nofid; |
be940c87 | 2918 | } |
bccacf6c | 2919 | retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf); |
88a4763e AK |
2920 | if (retval < 0) { |
2921 | goto out; | |
2922 | } | |
ddca7f86 MK |
2923 | retval = v9fs_fill_statfs(s, pdu, &stbuf); |
2924 | if (retval < 0) { | |
2925 | goto out; | |
2926 | } | |
2927 | retval += offset; | |
be940c87 | 2928 | out: |
bccacf6c | 2929 | put_fid(pdu, fidp); |
84dfb926 | 2930 | out_nofid: |
dc295f83 | 2931 | pdu_complete(pdu, retval); |
be940c87 MK |
2932 | } |
2933 | ||
8440e22e | 2934 | static void coroutine_fn v9fs_mknod(void *opaque) |
5268cecc | 2935 | { |
1b733fed AK |
2936 | |
2937 | int mode; | |
2938 | gid_t gid; | |
5268cecc | 2939 | int32_t fid; |
1b733fed | 2940 | V9fsQID qid; |
5268cecc | 2941 | int err = 0; |
5268cecc | 2942 | int major, minor; |
1b733fed AK |
2943 | size_t offset = 7; |
2944 | V9fsString name; | |
2945 | struct stat stbuf; | |
1b733fed AK |
2946 | V9fsFidState *fidp; |
2947 | V9fsPDU *pdu = opaque; | |
5268cecc | 2948 | |
ddca7f86 MK |
2949 | v9fs_string_init(&name); |
2950 | err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, | |
2951 | &major, &minor, &gid); | |
2952 | if (err < 0) { | |
2953 | goto out_nofid; | |
2954 | } | |
c572f23a | 2955 | trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor); |
5268cecc | 2956 | |
fff39a7a GK |
2957 | if (name_is_illegal(name.data)) { |
2958 | err = -ENOENT; | |
2959 | goto out_nofid; | |
2960 | } | |
2961 | ||
805b5d98 GK |
2962 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
2963 | err = -EEXIST; | |
2964 | goto out_nofid; | |
2965 | } | |
2966 | ||
bccacf6c | 2967 | fidp = get_fid(pdu, fid); |
5268cecc MK |
2968 | if (fidp == NULL) { |
2969 | err = -ENOENT; | |
84dfb926 | 2970 | goto out_nofid; |
5268cecc | 2971 | } |
bccacf6c | 2972 | err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid, |
02cb7f3a | 2973 | makedev(major, minor), mode, &stbuf); |
1b733fed AK |
2974 | if (err < 0) { |
2975 | goto out; | |
2976 | } | |
2977 | stat_to_qid(&stbuf, &qid); | |
ddca7f86 MK |
2978 | err = pdu_marshal(pdu, offset, "Q", &qid); |
2979 | if (err < 0) { | |
2980 | goto out; | |
2981 | } | |
2982 | err += offset; | |
7999f7e1 AK |
2983 | trace_v9fs_mknod_return(pdu->tag, pdu->id, |
2984 | qid.type, qid.version, qid.path); | |
5268cecc | 2985 | out: |
bccacf6c | 2986 | put_fid(pdu, fidp); |
84dfb926 | 2987 | out_nofid: |
dc295f83 | 2988 | pdu_complete(pdu, err); |
1b733fed | 2989 | v9fs_string_free(&name); |
5268cecc MK |
2990 | } |
2991 | ||
82cc3ee8 MK |
2992 | /* |
2993 | * Implement posix byte range locking code | |
2994 | * Server side handling of locking code is very simple, because 9p server in | |
2995 | * QEMU can handle only one client. And most of the lock handling | |
2996 | * (like conflict, merging) etc is done by the VFS layer itself, so no need to | |
2997 | * do any thing in * qemu 9p server side lock code path. | |
2998 | * So when a TLOCK request comes, always return success | |
2999 | */ | |
8440e22e | 3000 | static void coroutine_fn v9fs_lock(void *opaque) |
82cc3ee8 | 3001 | { |
0c27bf2a | 3002 | int8_t status; |
ddca7f86 | 3003 | V9fsFlock flock; |
0c27bf2a AK |
3004 | size_t offset = 7; |
3005 | struct stat stbuf; | |
3006 | V9fsFidState *fidp; | |
3007 | int32_t fid, err = 0; | |
ff06030f | 3008 | V9fsPDU *pdu = opaque; |
82cc3ee8 | 3009 | |
ddca7f86 MK |
3010 | status = P9_LOCK_ERROR; |
3011 | v9fs_string_init(&flock.client_id); | |
3012 | err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type, | |
3013 | &flock.flags, &flock.start, &flock.length, | |
3014 | &flock.proc_id, &flock.client_id); | |
3015 | if (err < 0) { | |
3016 | goto out_nofid; | |
3017 | } | |
c572f23a | 3018 | trace_v9fs_lock(pdu->tag, pdu->id, fid, |
ddca7f86 | 3019 | flock.type, flock.start, flock.length); |
c572f23a | 3020 | |
82cc3ee8 MK |
3021 | |
3022 | /* We support only block flag now (that too ignored currently) */ | |
ddca7f86 | 3023 | if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) { |
82cc3ee8 | 3024 | err = -EINVAL; |
84dfb926 | 3025 | goto out_nofid; |
82cc3ee8 | 3026 | } |
bccacf6c | 3027 | fidp = get_fid(pdu, fid); |
0c27bf2a | 3028 | if (fidp == NULL) { |
82cc3ee8 | 3029 | err = -ENOENT; |
84dfb926 | 3030 | goto out_nofid; |
82cc3ee8 | 3031 | } |
cc720ddb | 3032 | err = v9fs_co_fstat(pdu, fidp, &stbuf); |
82cc3ee8 | 3033 | if (err < 0) { |
82cc3ee8 MK |
3034 | goto out; |
3035 | } | |
0c27bf2a | 3036 | status = P9_LOCK_SUCCESS; |
82cc3ee8 | 3037 | out: |
bccacf6c | 3038 | put_fid(pdu, fidp); |
84dfb926 | 3039 | out_nofid: |
ddca7f86 MK |
3040 | err = pdu_marshal(pdu, offset, "b", status); |
3041 | if (err > 0) { | |
3042 | err += offset; | |
3043 | } | |
c572f23a | 3044 | trace_v9fs_lock_return(pdu->tag, pdu->id, status); |
dc295f83 | 3045 | pdu_complete(pdu, err); |
ddca7f86 | 3046 | v9fs_string_free(&flock.client_id); |
82cc3ee8 MK |
3047 | } |
3048 | ||
8f354003 MK |
3049 | /* |
3050 | * When a TGETLOCK request comes, always return success because all lock | |
3051 | * handling is done by client's VFS layer. | |
3052 | */ | |
8440e22e | 3053 | static void coroutine_fn v9fs_getlock(void *opaque) |
8f354003 | 3054 | { |
e4e414a4 AK |
3055 | size_t offset = 7; |
3056 | struct stat stbuf; | |
3057 | V9fsFidState *fidp; | |
ddca7f86 | 3058 | V9fsGetlock glock; |
e4e414a4 | 3059 | int32_t fid, err = 0; |
ff06030f | 3060 | V9fsPDU *pdu = opaque; |
8f354003 | 3061 | |
ddca7f86 MK |
3062 | v9fs_string_init(&glock.client_id); |
3063 | err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type, | |
3064 | &glock.start, &glock.length, &glock.proc_id, | |
3065 | &glock.client_id); | |
3066 | if (err < 0) { | |
3067 | goto out_nofid; | |
3068 | } | |
c572f23a | 3069 | trace_v9fs_getlock(pdu->tag, pdu->id, fid, |
ddca7f86 | 3070 | glock.type, glock.start, glock.length); |
c572f23a | 3071 | |
bccacf6c | 3072 | fidp = get_fid(pdu, fid); |
e4e414a4 | 3073 | if (fidp == NULL) { |
8f354003 | 3074 | err = -ENOENT; |
84dfb926 | 3075 | goto out_nofid; |
8f354003 | 3076 | } |
cc720ddb | 3077 | err = v9fs_co_fstat(pdu, fidp, &stbuf); |
8f354003 | 3078 | if (err < 0) { |
8f354003 MK |
3079 | goto out; |
3080 | } | |
ddca7f86 MK |
3081 | glock.type = P9_LOCK_TYPE_UNLCK; |
3082 | err = pdu_marshal(pdu, offset, "bqqds", glock.type, | |
3083 | glock.start, glock.length, glock.proc_id, | |
3084 | &glock.client_id); | |
3085 | if (err < 0) { | |
3086 | goto out; | |
3087 | } | |
3088 | err += offset; | |
3089 | trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start, | |
3090 | glock.length, glock.proc_id); | |
8f354003 | 3091 | out: |
bccacf6c | 3092 | put_fid(pdu, fidp); |
84dfb926 | 3093 | out_nofid: |
dc295f83 | 3094 | pdu_complete(pdu, err); |
ddca7f86 | 3095 | v9fs_string_free(&glock.client_id); |
8f354003 MK |
3096 | } |
3097 | ||
8440e22e | 3098 | static void coroutine_fn v9fs_mkdir(void *opaque) |
b67592ea | 3099 | { |
ff06030f | 3100 | V9fsPDU *pdu = opaque; |
e84861f7 | 3101 | size_t offset = 7; |
b67592ea | 3102 | int32_t fid; |
e84861f7 | 3103 | struct stat stbuf; |
e84861f7 | 3104 | V9fsQID qid; |
02cb7f3a | 3105 | V9fsString name; |
b67592ea MK |
3106 | V9fsFidState *fidp; |
3107 | gid_t gid; | |
3108 | int mode; | |
e84861f7 | 3109 | int err = 0; |
b67592ea | 3110 | |
ddca7f86 MK |
3111 | v9fs_string_init(&name); |
3112 | err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); | |
3113 | if (err < 0) { | |
3114 | goto out_nofid; | |
3115 | } | |
c572f23a HPB |
3116 | trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid); |
3117 | ||
fff39a7a GK |
3118 | if (name_is_illegal(name.data)) { |
3119 | err = -ENOENT; | |
3120 | goto out_nofid; | |
3121 | } | |
3122 | ||
805b5d98 GK |
3123 | if (!strcmp(".", name.data) || !strcmp("..", name.data)) { |
3124 | err = -EEXIST; | |
3125 | goto out_nofid; | |
3126 | } | |
3127 | ||
bccacf6c | 3128 | fidp = get_fid(pdu, fid); |
b67592ea MK |
3129 | if (fidp == NULL) { |
3130 | err = -ENOENT; | |
84dfb926 | 3131 | goto out_nofid; |
b67592ea | 3132 | } |
bccacf6c | 3133 | err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf); |
e84861f7 VJ |
3134 | if (err < 0) { |
3135 | goto out; | |
3136 | } | |
3137 | stat_to_qid(&stbuf, &qid); | |
ddca7f86 MK |
3138 | err = pdu_marshal(pdu, offset, "Q", &qid); |
3139 | if (err < 0) { | |
3140 | goto out; | |
3141 | } | |
3142 | err += offset; | |
7999f7e1 AK |
3143 | trace_v9fs_mkdir_return(pdu->tag, pdu->id, |
3144 | qid.type, qid.version, qid.path, err); | |
b67592ea | 3145 | out: |
bccacf6c | 3146 | put_fid(pdu, fidp); |
84dfb926 | 3147 | out_nofid: |
dc295f83 | 3148 | pdu_complete(pdu, err); |
e84861f7 | 3149 | v9fs_string_free(&name); |
b67592ea MK |
3150 | } |
3151 | ||
8440e22e | 3152 | static void coroutine_fn v9fs_xattrwalk(void *opaque) |
fa32ef88 | 3153 | { |
670185a6 AK |
3154 | int64_t size; |
3155 | V9fsString name; | |
fa32ef88 | 3156 | ssize_t err = 0; |
670185a6 | 3157 | size_t offset = 7; |
fa32ef88 | 3158 | int32_t fid, newfid; |
670185a6 | 3159 | V9fsFidState *file_fidp; |
84dfb926 | 3160 | V9fsFidState *xattr_fidp = NULL; |
670185a6 AK |
3161 | V9fsPDU *pdu = opaque; |
3162 | V9fsState *s = pdu->s; | |
fa32ef88 | 3163 | |
ddca7f86 MK |
3164 | v9fs_string_init(&name); |
3165 | err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); | |
3166 | if (err < 0) { | |
3167 | goto out_nofid; | |
3168 | } | |
c572f23a HPB |
3169 | trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data); |
3170 | ||
bccacf6c | 3171 | file_fidp = get_fid(pdu, fid); |
670185a6 | 3172 | if (file_fidp == NULL) { |
fa32ef88 | 3173 | err = -ENOENT; |
84dfb926 | 3174 | goto out_nofid; |
fa32ef88 | 3175 | } |
670185a6 AK |
3176 | xattr_fidp = alloc_fid(s, newfid); |
3177 | if (xattr_fidp == NULL) { | |
fa32ef88 AK |
3178 | err = -EINVAL; |
3179 | goto out; | |
3180 | } | |
2289be19 | 3181 | v9fs_path_copy(&xattr_fidp->path, &file_fidp->path); |
ba42ebb8 | 3182 | if (!v9fs_string_size(&name)) { |
fa32ef88 AK |
3183 | /* |
3184 | * listxattr request. Get the size first | |
3185 | */ | |
bccacf6c | 3186 | size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0); |
670185a6 AK |
3187 | if (size < 0) { |
3188 | err = size; | |
84dfb926 | 3189 | clunk_fid(s, xattr_fidp->fid); |
670185a6 | 3190 | goto out; |
fa32ef88 | 3191 | } |
670185a6 AK |
3192 | /* |
3193 | * Read the xattr value | |
3194 | */ | |
3195 | xattr_fidp->fs.xattr.len = size; | |
3196 | xattr_fidp->fid_type = P9_FID_XATTR; | |
dd28fbbc | 3197 | xattr_fidp->fs.xattr.xattrwalk_fid = true; |
670185a6 | 3198 | if (size) { |
7267c094 | 3199 | xattr_fidp->fs.xattr.value = g_malloc(size); |
bccacf6c | 3200 | err = v9fs_co_llistxattr(pdu, &xattr_fidp->path, |
670185a6 AK |
3201 | xattr_fidp->fs.xattr.value, |
3202 | xattr_fidp->fs.xattr.len); | |
3203 | if (err < 0) { | |
84dfb926 | 3204 | clunk_fid(s, xattr_fidp->fid); |
670185a6 AK |
3205 | goto out; |
3206 | } | |
3207 | } | |
ddca7f86 MK |
3208 | err = pdu_marshal(pdu, offset, "q", size); |
3209 | if (err < 0) { | |
3210 | goto out; | |
3211 | } | |
3212 | err += offset; | |
fa32ef88 AK |
3213 | } else { |
3214 | /* | |
3215 | * specific xattr fid. We check for xattr | |
3216 | * presence also collect the xattr size | |
3217 | */ | |
bccacf6c | 3218 | size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, |
670185a6 AK |
3219 | &name, NULL, 0); |
3220 | if (size < 0) { | |
3221 | err = size; | |
84dfb926 | 3222 | clunk_fid(s, xattr_fidp->fid); |
670185a6 | 3223 | goto out; |
fa32ef88 | 3224 | } |
670185a6 AK |
3225 | /* |
3226 | * Read the xattr value | |
3227 | */ | |
3228 | xattr_fidp->fs.xattr.len = size; | |
3229 | xattr_fidp->fid_type = P9_FID_XATTR; | |
dd28fbbc | 3230 | xattr_fidp->fs.xattr.xattrwalk_fid = true; |
670185a6 | 3231 | if (size) { |
7267c094 | 3232 | xattr_fidp->fs.xattr.value = g_malloc(size); |
bccacf6c | 3233 | err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, |
670185a6 AK |
3234 | &name, xattr_fidp->fs.xattr.value, |
3235 | xattr_fidp->fs.xattr.len); | |
3236 | if (err < 0) { | |
84dfb926 | 3237 | clunk_fid(s, xattr_fidp->fid); |
670185a6 AK |
3238 | goto out; |
3239 | } | |
3240 | } | |
ddca7f86 MK |
3241 | err = pdu_marshal(pdu, offset, "q", size); |
3242 | if (err < 0) { | |
3243 | goto out; | |
3244 | } | |
3245 | err += offset; | |
fa32ef88 | 3246 | } |
7999f7e1 | 3247 | trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size); |
fa32ef88 | 3248 | out: |
bccacf6c | 3249 | put_fid(pdu, file_fidp); |
84dfb926 | 3250 | if (xattr_fidp) { |
bccacf6c | 3251 | put_fid(pdu, xattr_fidp); |
84dfb926 AK |
3252 | } |
3253 | out_nofid: | |
dc295f83 | 3254 | pdu_complete(pdu, err); |
670185a6 | 3255 | v9fs_string_free(&name); |
fa32ef88 AK |
3256 | } |
3257 | ||
8440e22e | 3258 | static void coroutine_fn v9fs_xattrcreate(void *opaque) |
10b468bd AK |
3259 | { |
3260 | int flags; | |
3261 | int32_t fid; | |
3b79ef2c | 3262 | uint64_t size; |
10b468bd | 3263 | ssize_t err = 0; |
f10ff58d AK |
3264 | V9fsString name; |
3265 | size_t offset = 7; | |
3266 | V9fsFidState *file_fidp; | |
3267 | V9fsFidState *xattr_fidp; | |
3268 | V9fsPDU *pdu = opaque; | |
10b468bd | 3269 | |
ddca7f86 MK |
3270 | v9fs_string_init(&name); |
3271 | err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); | |
3272 | if (err < 0) { | |
3273 | goto out_nofid; | |
3274 | } | |
c572f23a | 3275 | trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags); |
10b468bd | 3276 | |
3b79ef2c GK |
3277 | if (size > XATTR_SIZE_MAX) { |
3278 | err = -E2BIG; | |
3279 | goto out_nofid; | |
3280 | } | |
3281 | ||
bccacf6c | 3282 | file_fidp = get_fid(pdu, fid); |
f10ff58d | 3283 | if (file_fidp == NULL) { |
10b468bd | 3284 | err = -EINVAL; |
84dfb926 | 3285 | goto out_nofid; |
10b468bd | 3286 | } |
dd654e03 GK |
3287 | if (file_fidp->fid_type != P9_FID_NONE) { |
3288 | err = -EINVAL; | |
3289 | goto out_put_fid; | |
3290 | } | |
3291 | ||
10b468bd | 3292 | /* Make the file fid point to xattr */ |
f10ff58d AK |
3293 | xattr_fidp = file_fidp; |
3294 | xattr_fidp->fid_type = P9_FID_XATTR; | |
3295 | xattr_fidp->fs.xattr.copied_len = 0; | |
dd28fbbc | 3296 | xattr_fidp->fs.xattr.xattrwalk_fid = false; |
f10ff58d AK |
3297 | xattr_fidp->fs.xattr.len = size; |
3298 | xattr_fidp->fs.xattr.flags = flags; | |
3299 | v9fs_string_init(&xattr_fidp->fs.xattr.name); | |
3300 | v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name); | |
eb687602 | 3301 | xattr_fidp->fs.xattr.value = g_malloc0(size); |
f10ff58d | 3302 | err = offset; |
dd654e03 | 3303 | out_put_fid: |
bccacf6c | 3304 | put_fid(pdu, file_fidp); |
84dfb926 | 3305 | out_nofid: |
dc295f83 | 3306 | pdu_complete(pdu, err); |
f10ff58d | 3307 | v9fs_string_free(&name); |
10b468bd | 3308 | } |
fa32ef88 | 3309 | |
8440e22e | 3310 | static void coroutine_fn v9fs_readlink(void *opaque) |
df0973a4 | 3311 | { |
ff06030f | 3312 | V9fsPDU *pdu = opaque; |
7a5ca31e VJ |
3313 | size_t offset = 7; |
3314 | V9fsString target; | |
df0973a4 | 3315 | int32_t fid; |
df0973a4 MK |
3316 | int err = 0; |
3317 | V9fsFidState *fidp; | |
3318 | ||
ddca7f86 MK |
3319 | err = pdu_unmarshal(pdu, offset, "d", &fid); |
3320 | if (err < 0) { | |
3321 | goto out_nofid; | |
3322 | } | |
c572f23a | 3323 | trace_v9fs_readlink(pdu->tag, pdu->id, fid); |
bccacf6c | 3324 | fidp = get_fid(pdu, fid); |
df0973a4 MK |
3325 | if (fidp == NULL) { |
3326 | err = -ENOENT; | |
84dfb926 | 3327 | goto out_nofid; |
df0973a4 MK |
3328 | } |
3329 | ||
7a5ca31e | 3330 | v9fs_string_init(&target); |
bccacf6c | 3331 | err = v9fs_co_readlink(pdu, &fidp->path, &target); |
7a5ca31e VJ |
3332 | if (err < 0) { |
3333 | goto out; | |
3334 | } | |
ddca7f86 MK |
3335 | err = pdu_marshal(pdu, offset, "s", &target); |
3336 | if (err < 0) { | |
3337 | v9fs_string_free(&target); | |
3338 | goto out; | |
3339 | } | |
3340 | err += offset; | |
7999f7e1 | 3341 | trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data); |
7a5ca31e | 3342 | v9fs_string_free(&target); |
df0973a4 | 3343 | out: |
bccacf6c | 3344 | put_fid(pdu, fidp); |
84dfb926 | 3345 | out_nofid: |
dc295f83 | 3346 | pdu_complete(pdu, err); |
df0973a4 MK |
3347 | } |
3348 | ||
ff06030f | 3349 | static CoroutineEntry *pdu_co_handlers[] = { |
c18e2f94 | 3350 | [P9_TREADDIR] = v9fs_readdir, |
be940c87 | 3351 | [P9_TSTATFS] = v9fs_statfs, |
00ede4c2 | 3352 | [P9_TGETATTR] = v9fs_getattr, |
c79ce737 | 3353 | [P9_TSETATTR] = v9fs_setattr, |
fa32ef88 | 3354 | [P9_TXATTRWALK] = v9fs_xattrwalk, |
10b468bd | 3355 | [P9_TXATTRCREATE] = v9fs_xattrcreate, |
5268cecc | 3356 | [P9_TMKNOD] = v9fs_mknod, |
c7b4b0b3 | 3357 | [P9_TRENAME] = v9fs_rename, |
82cc3ee8 | 3358 | [P9_TLOCK] = v9fs_lock, |
8f354003 | 3359 | [P9_TGETLOCK] = v9fs_getlock, |
89bf6593 | 3360 | [P9_TRENAMEAT] = v9fs_renameat, |
df0973a4 | 3361 | [P9_TREADLINK] = v9fs_readlink, |
7834cf77 | 3362 | [P9_TUNLINKAT] = v9fs_unlinkat, |
b67592ea | 3363 | [P9_TMKDIR] = v9fs_mkdir, |
9f107513 | 3364 | [P9_TVERSION] = v9fs_version, |
771e9d4c | 3365 | [P9_TLOPEN] = v9fs_open, |
9f107513 AL |
3366 | [P9_TATTACH] = v9fs_attach, |
3367 | [P9_TSTAT] = v9fs_stat, | |
3368 | [P9_TWALK] = v9fs_walk, | |
3369 | [P9_TCLUNK] = v9fs_clunk, | |
b41e95d3 | 3370 | [P9_TFSYNC] = v9fs_fsync, |
9f107513 AL |
3371 | [P9_TOPEN] = v9fs_open, |
3372 | [P9_TREAD] = v9fs_read, | |
3373 | #if 0 | |
3374 | [P9_TAUTH] = v9fs_auth, | |
3375 | #endif | |
3376 | [P9_TFLUSH] = v9fs_flush, | |
b2c224be | 3377 | [P9_TLINK] = v9fs_link, |
08c60fc9 | 3378 | [P9_TSYMLINK] = v9fs_symlink, |
9f107513 | 3379 | [P9_TCREATE] = v9fs_create, |
c1568af5 | 3380 | [P9_TLCREATE] = v9fs_lcreate, |
9f107513 AL |
3381 | [P9_TWRITE] = v9fs_write, |
3382 | [P9_TWSTAT] = v9fs_wstat, | |
3383 | [P9_TREMOVE] = v9fs_remove, | |
3384 | }; | |
3385 | ||
8440e22e | 3386 | static void coroutine_fn v9fs_op_not_supp(void *opaque) |
5c3234c6 | 3387 | { |
ff06030f | 3388 | V9fsPDU *pdu = opaque; |
dc295f83 | 3389 | pdu_complete(pdu, -EOPNOTSUPP); |
5c3234c6 AK |
3390 | } |
3391 | ||
8440e22e | 3392 | static void coroutine_fn v9fs_fs_ro(void *opaque) |
2c74c2cb MK |
3393 | { |
3394 | V9fsPDU *pdu = opaque; | |
dc295f83 | 3395 | pdu_complete(pdu, -EROFS); |
2c74c2cb MK |
3396 | } |
3397 | ||
3398 | static inline bool is_read_only_op(V9fsPDU *pdu) | |
3399 | { | |
3400 | switch (pdu->id) { | |
3401 | case P9_TREADDIR: | |
3402 | case P9_TSTATFS: | |
3403 | case P9_TGETATTR: | |
3404 | case P9_TXATTRWALK: | |
3405 | case P9_TLOCK: | |
3406 | case P9_TGETLOCK: | |
3407 | case P9_TREADLINK: | |
3408 | case P9_TVERSION: | |
3409 | case P9_TLOPEN: | |
3410 | case P9_TATTACH: | |
3411 | case P9_TSTAT: | |
3412 | case P9_TWALK: | |
3413 | case P9_TCLUNK: | |
3414 | case P9_TFSYNC: | |
3415 | case P9_TOPEN: | |
3416 | case P9_TREAD: | |
3417 | case P9_TAUTH: | |
3418 | case P9_TFLUSH: | |
3419 | return 1; | |
3420 | default: | |
3421 | return 0; | |
3422 | } | |
3423 | } | |
3424 | ||
4b311c5f | 3425 | void pdu_submit(V9fsPDU *pdu) |
9f107513 | 3426 | { |
ff06030f VJ |
3427 | Coroutine *co; |
3428 | CoroutineEntry *handler; | |
ad38ce9e | 3429 | V9fsState *s = pdu->s; |
9f107513 | 3430 | |
ff06030f VJ |
3431 | if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) || |
3432 | (pdu_co_handlers[pdu->id] == NULL)) { | |
5c3234c6 AK |
3433 | handler = v9fs_op_not_supp; |
3434 | } else { | |
ff06030f | 3435 | handler = pdu_co_handlers[pdu->id]; |
5c3234c6 | 3436 | } |
2c74c2cb MK |
3437 | |
3438 | if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { | |
3439 | handler = v9fs_fs_ro; | |
3440 | } | |
0b8b8753 PB |
3441 | co = qemu_coroutine_create(handler, pdu); |
3442 | qemu_coroutine_enter(co); | |
9f107513 AL |
3443 | } |
3444 | ||
2a0c56aa WL |
3445 | /* Returns 0 on success, 1 on failure. */ |
3446 | int v9fs_device_realize_common(V9fsState *s, Error **errp) | |
3447 | { | |
3448 | int i, len; | |
3449 | struct stat stat; | |
3450 | FsDriverEntry *fse; | |
3451 | V9fsPath path; | |
3452 | int rc = 1; | |
3453 | ||
3454 | /* initialize pdu allocator */ | |
3455 | QLIST_INIT(&s->free_list); | |
3456 | QLIST_INIT(&s->active_list); | |
3457 | for (i = 0; i < (MAX_REQ - 1); i++) { | |
583f21f8 SS |
3458 | QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); |
3459 | s->pdus[i].s = s; | |
3460 | s->pdus[i].idx = i; | |
2a0c56aa WL |
3461 | } |
3462 | ||
3463 | v9fs_path_init(&path); | |
3464 | ||
3465 | fse = get_fsdev_fsentry(s->fsconf.fsdev_id); | |
3466 | ||
3467 | if (!fse) { | |
3468 | /* We don't have a fsdev identified by fsdev_id */ | |
3469 | error_setg(errp, "9pfs device couldn't find fsdev with the " | |
3470 | "id = %s", | |
3471 | s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); | |
3472 | goto out; | |
3473 | } | |
3474 | ||
3475 | if (!s->fsconf.tag) { | |
3476 | /* we haven't specified a mount_tag */ | |
3477 | error_setg(errp, "fsdev with id %s needs mount_tag arguments", | |
3478 | s->fsconf.fsdev_id); | |
3479 | goto out; | |
3480 | } | |
3481 | ||
3482 | s->ctx.export_flags = fse->export_flags; | |
3483 | s->ctx.fs_root = g_strdup(fse->path); | |
3484 | s->ctx.exops.get_st_gen = NULL; | |
3485 | len = strlen(s->fsconf.tag); | |
3486 | if (len > MAX_TAG_LEN - 1) { | |
3487 | error_setg(errp, "mount tag '%s' (%d bytes) is longer than " | |
3488 | "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); | |
3489 | goto out; | |
3490 | } | |
3491 | ||
3492 | s->tag = g_strdup(s->fsconf.tag); | |
3493 | s->ctx.uid = -1; | |
3494 | ||
3495 | s->ops = fse->ops; | |
3496 | ||
3497 | s->fid_list = NULL; | |
3498 | qemu_co_rwlock_init(&s->rename_lock); | |
3499 | ||
3500 | if (s->ops->init(&s->ctx) < 0) { | |
3501 | error_setg(errp, "9pfs Failed to initialize fs-driver with id:%s" | |
3502 | " and export path:%s", s->fsconf.fsdev_id, s->ctx.fs_root); | |
3503 | goto out; | |
3504 | } | |
3505 | ||
3506 | /* | |
3507 | * Check details of export path, We need to use fs driver | |
3508 | * call back to do that. Since we are in the init path, we don't | |
3509 | * use co-routines here. | |
3510 | */ | |
3511 | if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { | |
3512 | error_setg(errp, | |
3513 | "error in converting name to path %s", strerror(errno)); | |
3514 | goto out; | |
3515 | } | |
3516 | if (s->ops->lstat(&s->ctx, &path, &stat)) { | |
3517 | error_setg(errp, "share path %s does not exist", fse->path); | |
3518 | goto out; | |
3519 | } else if (!S_ISDIR(stat.st_mode)) { | |
3520 | error_setg(errp, "share path %s is not a directory", fse->path); | |
3521 | goto out; | |
3522 | } | |
3523 | v9fs_path_free(&path); | |
3524 | ||
3525 | rc = 0; | |
3526 | out: | |
3527 | if (rc) { | |
702dbcc2 LQ |
3528 | if (s->ops->cleanup && s->ctx.private) { |
3529 | s->ops->cleanup(&s->ctx); | |
3530 | } | |
2a0c56aa | 3531 | g_free(s->tag); |
4774718e | 3532 | g_free(s->ctx.fs_root); |
2a0c56aa WL |
3533 | v9fs_path_free(&path); |
3534 | } | |
3535 | return rc; | |
3536 | } | |
3537 | ||
3538 | void v9fs_device_unrealize_common(V9fsState *s, Error **errp) | |
3539 | { | |
702dbcc2 LQ |
3540 | if (s->ops->cleanup) { |
3541 | s->ops->cleanup(&s->ctx); | |
3542 | } | |
2a0c56aa | 3543 | g_free(s->tag); |
4774718e | 3544 | g_free(s->ctx.fs_root); |
2a0c56aa WL |
3545 | } |
3546 | ||
0e44a0fd GK |
3547 | typedef struct VirtfsCoResetData { |
3548 | V9fsPDU pdu; | |
3549 | bool done; | |
3550 | } VirtfsCoResetData; | |
3551 | ||
3552 | static void coroutine_fn virtfs_co_reset(void *opaque) | |
3553 | { | |
3554 | VirtfsCoResetData *data = opaque; | |
3555 | ||
3556 | virtfs_reset(&data->pdu); | |
3557 | data->done = true; | |
3558 | } | |
3559 | ||
3560 | void v9fs_reset(V9fsState *s) | |
3561 | { | |
3562 | VirtfsCoResetData data = { .pdu = { .s = s }, .done = false }; | |
3563 | Coroutine *co; | |
3564 | ||
3565 | while (!QLIST_EMPTY(&s->active_list)) { | |
3566 | aio_poll(qemu_get_aio_context(), true); | |
3567 | } | |
3568 | ||
3569 | co = qemu_coroutine_create(virtfs_co_reset, &data); | |
3570 | qemu_coroutine_enter(co); | |
3571 | ||
3572 | while (!data.done) { | |
3573 | aio_poll(qemu_get_aio_context(), true); | |
3574 | } | |
3575 | } | |
3576 | ||
72a18977 | 3577 | static void __attribute__((__constructor__)) v9fs_set_fd_limit(void) |
7a462745 AK |
3578 | { |
3579 | struct rlimit rlim; | |
3580 | if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { | |
63325b18 | 3581 | error_report("Failed to get the resource limit"); |
7a462745 AK |
3582 | exit(1); |
3583 | } | |
3584 | open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur/3); | |
3585 | open_fd_rc = rlim.rlim_cur/2; | |
3586 | } |