1 /* $NetBSD: ops.c,v 1.60 2012/11/03 15:43:20 manu Exp $ */ 2 3 /*- 4 * Copyright (c) 2010-2011 Emmanuel Dreyfus. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <stdio.h> 29 #include <unistd.h> 30 #include <stdlib.h> 31 #include <libgen.h> 32 #include <errno.h> 33 #include <err.h> 34 #include <sysexits.h> 35 #include <syslog.h> 36 #include <puffs.h> 37 #include <sys/socket.h> 38 #include <sys/socket.h> 39 #include <sys/extattr.h> 40 #include <sys/time.h> 41 #include <machine/vmparam.h> 42 43 #include "perfuse_priv.h" 44 #include "fuse.h" 45 46 extern int perfuse_diagflags; 47 48 #if 0 49 static void print_node(const char *, puffs_cookie_t); 50 #endif 51 #ifdef PUFFS_KFLAG_CACHE_FS_TTL 52 static void perfuse_newinfo_setttl(struct puffs_newinfo *, 53 struct puffs_node *, struct fuse_entry_out *, struct fuse_attr_out *); 54 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */ 55 static int xchg_msg(struct puffs_usermount *, puffs_cookie_t, 56 perfuse_msg_t *, size_t, enum perfuse_xchg_pb_reply); 57 static int mode_access(puffs_cookie_t, const struct puffs_cred *, mode_t); 58 static int sticky_access(puffs_cookie_t, struct puffs_node *, 59 const struct puffs_cred *); 60 static void fuse_attr_to_vap(struct perfuse_state *, 61 struct vattr *, struct fuse_attr *); 62 static int node_lookup_common(struct puffs_usermount *, puffs_cookie_t, 63 struct puffs_newinfo *, const char *, const struct puffs_cred *, 64 struct puffs_node **); 65 static int node_mk_common(struct puffs_usermount *, puffs_cookie_t, 66 struct puffs_newinfo *, const struct puffs_cn *pcn, perfuse_msg_t *); 67 static uint64_t readdir_last_cookie(struct fuse_dirent *, size_t); 68 static ssize_t fuse_to_dirent(struct puffs_usermount *, puffs_cookie_t, 69 struct fuse_dirent *, size_t); 70 static void readdir_buffered(puffs_cookie_t, struct dirent *, off_t *, 71 size_t *); 72 static void node_ref(puffs_cookie_t); 73 static void node_rele(puffs_cookie_t); 74 static void requeue_request(struct puffs_usermount *, 75 puffs_cookie_t opc, enum perfuse_qtype); 76 static int dequeue_requests(puffs_cookie_t opc, enum perfuse_qtype, int); 77 #define DEQUEUE_ALL 0 78 79 /* 80 * From <sys/vnode>, inside #ifdef _KERNEL section 81 */ 82 #define IO_SYNC (0x40|IO_DSYNC) 83 #define IO_DSYNC 0x00200 84 #define IO_DIRECT 0x02000 85 86 /* 87 * From <fcntl>, inside #ifdef _KERNEL section 88 */ 89 #define F_WAIT 0x010 90 #define F_FLOCK 0x020 91 #define OFLAGS(fflags) ((fflags) - 1) 92 93 /* 94 * Borrowed from src/sys/kern/vfs_subr.c and src/sys/sys/vnode.h 95 */ 96 const enum vtype iftovt_tab[16] = { 97 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 98 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 99 }; 100 const int vttoif_tab[9] = { 101 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 102 S_IFSOCK, S_IFIFO, S_IFMT, 103 }; 104 105 #define IFTOVT(mode) (iftovt_tab[((mode) & S_IFMT) >> 12]) 106 #define VTTOIF(indx) (vttoif_tab[(int)(indx)]) 107 108 #if 0 109 static void 110 print_node(const char *func, puffs_cookie_t opc) 111 { 112 struct puffs_node *pn; 113 struct perfuse_node_data *pnd; 114 struct vattr *vap; 115 116 pn = (struct puffs_node *)opc; 117 pnd = PERFUSE_NODE_DATA(opc); 118 vap = &pn->pn_va; 119 120 printf("%s: \"%s\", opc = %p, nodeid = 0x%"PRIx64" ino = %"PRIu64"\n", 121 func, pnd->pnd_name, opc, pnd->pnd_nodeid, vap->va_fileid); 122 123 return; 124 } 125 #endif /* PERFUSE_DEBUG */ 126 127 int 128 perfuse_node_close_common(struct puffs_usermount *pu, puffs_cookie_t opc, 129 int mode) 130 { 131 struct perfuse_state *ps; 132 perfuse_msg_t *pm; 133 int op; 134 uint64_t fh; 135 struct fuse_release_in *fri; 136 struct perfuse_node_data *pnd; 137 struct puffs_node *pn; 138 int error; 139 140 ps = puffs_getspecific(pu); 141 pn = (struct puffs_node *)opc; 142 pnd = PERFUSE_NODE_DATA(pn); 143 144 if (puffs_pn_getvap(pn)->va_type == VDIR) { 145 op = FUSE_RELEASEDIR; 146 mode = FREAD; 147 } else { 148 op = FUSE_RELEASE; 149 } 150 151 /* 152 * Destroy the filehandle before sending the 153 * request to the FUSE filesystem, otherwise 154 * we may get a second close() while we wait 155 * for the reply, and we would end up closing 156 * the same fh twice instead of closng both. 157 */ 158 fh = perfuse_get_fh(opc, mode); 159 perfuse_destroy_fh(pn, fh); 160 161 /* 162 * release_flags may be set to FUSE_RELEASE_FLUSH 163 * to flush locks. lock_owner must be set in that case 164 * 165 * ps_new_msg() is called with NULL creds, which will 166 * be interpreted as FUSE superuser. We come here from the 167 * inactive method, which provides no creds, but obviously 168 * runs with kernel privilege. 169 */ 170 pm = ps->ps_new_msg(pu, opc, op, sizeof(*fri), NULL); 171 fri = GET_INPAYLOAD(ps, pm, fuse_release_in); 172 fri->fh = fh; 173 fri->flags = 0; 174 fri->release_flags = 0; 175 fri->lock_owner = pnd->pnd_lock_owner; 176 fri->flags = (fri->lock_owner != 0) ? FUSE_RELEASE_FLUSH : 0; 177 178 #ifdef PERFUSE_DEBUG 179 if (perfuse_diagflags & PDF_FH) 180 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n", 181 __func__, (void *)opc, pnd->pnd_nodeid, fri->fh); 182 #endif 183 184 if ((error = xchg_msg(pu, opc, pm, 185 NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0) 186 DERRX(EX_SOFTWARE, "%s: freed fh = 0x%"PRIx64" but filesystem " 187 "returned error = %d", __func__, fh, error); 188 189 ps->ps_destroy_msg(pm); 190 191 return 0; 192 } 193 194 static int 195 xchg_msg(struct puffs_usermount *pu, puffs_cookie_t opc, perfuse_msg_t *pm, 196 size_t len, enum perfuse_xchg_pb_reply wait) 197 { 198 struct perfuse_state *ps; 199 struct perfuse_node_data *pnd; 200 struct perfuse_trace *pt = NULL; 201 int error; 202 203 ps = puffs_getspecific(pu); 204 pnd = NULL; 205 if ((struct puffs_node *)opc != NULL) 206 pnd = PERFUSE_NODE_DATA(opc); 207 208 #ifdef PERFUSE_DEBUG 209 if ((perfuse_diagflags & PDF_FILENAME) && (opc != 0)) 210 DPRINTF("file = \"%s\", ino = %"PRIu64" flags = 0x%x\n", 211 perfuse_node_path(ps, opc), 212 ((struct puffs_node *)opc)->pn_va.va_fileid, 213 PERFUSE_NODE_DATA(opc)->pnd_flags); 214 #endif 215 ps->ps_xchgcount++; 216 if (pnd) 217 pnd->pnd_inxchg++; 218 219 /* 220 * Record FUSE call start if requested 221 */ 222 if (perfuse_diagflags & PDF_TRACE) 223 pt = perfuse_trace_begin(ps, opc, pm); 224 225 /* 226 * Do actual FUSE exchange 227 */ 228 if ((error = ps->ps_xchg_msg(pu, pm, len, wait)) != 0) 229 ps->ps_destroy_msg(pm); 230 231 /* 232 * Record FUSE call end if requested 233 */ 234 if (pt != NULL) 235 perfuse_trace_end(ps, pt, error); 236 237 ps->ps_xchgcount--; 238 if (pnd) { 239 pnd->pnd_inxchg--; 240 (void)dequeue_requests(opc, PCQ_AFTERXCHG, DEQUEUE_ALL); 241 } 242 243 return error; 244 } 245 246 static int 247 mode_access(puffs_cookie_t opc, const struct puffs_cred *pcr, mode_t mode) 248 { 249 struct puffs_node *pn; 250 struct vattr *va; 251 252 /* 253 * pcr is NULL for self open through fsync or readdir. 254 * In both case, access control is useless, as it was 255 * done before, at open time. 256 */ 257 if (pcr == NULL) 258 return 0; 259 260 pn = (struct puffs_node *)opc; 261 va = puffs_pn_getvap(pn); 262 return puffs_access(va->va_type, va->va_mode, 263 va->va_uid, va->va_gid, 264 mode, pcr); 265 } 266 267 static int 268 sticky_access(puffs_cookie_t opc, struct puffs_node *targ, 269 const struct puffs_cred *pcr) 270 { 271 uid_t uid; 272 int sticky, owner; 273 274 /* 275 * This covers the case where the kernel requests a DELETE 276 * or RENAME on its own, and where puffs_cred_getuid would 277 * return -1. While such a situation should not happen, 278 * we allow it here. 279 * 280 * This also allows root to tamper with other users' files 281 * that have the sticky bit. 282 */ 283 if (puffs_cred_isjuggernaut(pcr)) 284 return 0; 285 286 if (puffs_cred_getuid(pcr, &uid) != 0) 287 DERRX(EX_SOFTWARE, "puffs_cred_getuid fails in %s", __func__); 288 289 sticky = puffs_pn_getvap(opc)->va_mode & S_ISTXT; 290 owner = puffs_pn_getvap(targ)->va_uid == uid; 291 292 if (sticky && !owner) 293 return EACCES; 294 295 return 0; 296 } 297 298 299 static void 300 fuse_attr_to_vap(struct perfuse_state *ps, struct vattr *vap, 301 struct fuse_attr *fa) 302 { 303 vap->va_type = IFTOVT(fa->mode); 304 vap->va_mode = fa->mode & ALLPERMS; 305 vap->va_nlink = fa->nlink; 306 vap->va_uid = fa->uid; 307 vap->va_gid = fa->gid; 308 vap->va_fsid = (long)ps->ps_fsid; 309 vap->va_fileid = fa->ino; 310 vap->va_size = fa->size; 311 vap->va_blocksize = fa->blksize; 312 vap->va_atime.tv_sec = (time_t)fa->atime; 313 vap->va_atime.tv_nsec = (long) fa->atimensec; 314 vap->va_mtime.tv_sec = (time_t)fa->mtime; 315 vap->va_mtime.tv_nsec = (long)fa->mtimensec; 316 vap->va_ctime.tv_sec = (time_t)fa->ctime; 317 vap->va_ctime.tv_nsec = (long)fa->ctimensec; 318 vap->va_birthtime.tv_sec = 0; 319 vap->va_birthtime.tv_nsec = 0; 320 vap->va_gen = 0; 321 vap->va_flags = 0; 322 vap->va_rdev = fa->rdev; 323 vap->va_bytes = fa->size; 324 vap->va_filerev = (u_quad_t)PUFFS_VNOVAL; 325 vap->va_vaflags = 0; 326 327 if (vap->va_blocksize == 0) 328 vap->va_blocksize = DEV_BSIZE; 329 330 if (vap->va_size == (size_t)PUFFS_VNOVAL) /* XXX */ 331 vap->va_size = 0; 332 333 return; 334 } 335 336 #ifdef PUFFS_KFLAG_CACHE_FS_TTL 337 static void 338 perfuse_newinfo_setttl(struct puffs_newinfo *pni, 339 struct puffs_node *pn, struct fuse_entry_out *feo, 340 struct fuse_attr_out *fao) 341 { 342 #ifdef PERFUSE_DEBUG 343 if ((feo == NULL) && (fao == NULL)) 344 DERRX(EX_SOFTWARE, "%s: feo and fao NULL", __func__); 345 346 if ((feo != NULL) && (fao != NULL)) 347 DERRX(EX_SOFTWARE, "%s: feo and fao != NULL", __func__); 348 #endif /* PERFUSE_DEBUG */ 349 350 if (fao != NULL) { 351 struct timespec va_ttl; 352 353 va_ttl.tv_sec = fao->attr_valid; 354 va_ttl.tv_nsec = fao->attr_valid_nsec; 355 356 puffs_newinfo_setvattl(pni, &va_ttl); 357 } 358 359 if (feo != NULL) { 360 struct timespec va_ttl; 361 struct timespec cn_ttl; 362 struct timespec now; 363 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(pn); 364 365 va_ttl.tv_sec = feo->attr_valid; 366 va_ttl.tv_nsec = feo->attr_valid_nsec; 367 cn_ttl.tv_sec = feo->entry_valid; 368 cn_ttl.tv_nsec = feo->entry_valid_nsec; 369 370 puffs_newinfo_setvattl(pni, &va_ttl); 371 puffs_newinfo_setcnttl(pni, &cn_ttl); 372 373 if (clock_gettime(CLOCK_REALTIME, &now) != 0) 374 DERR(EX_OSERR, "clock_gettime failed"); 375 376 timespecadd(&now, &cn_ttl, &pnd->pnd_cn_expire); 377 } 378 379 return; 380 } 381 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */ 382 383 static int 384 node_lookup_common(struct puffs_usermount *pu, puffs_cookie_t opc, 385 struct puffs_newinfo *pni, const char *path, 386 const struct puffs_cred *pcr, struct puffs_node **pnp) 387 { 388 struct perfuse_state *ps; 389 struct perfuse_node_data *oldpnd; 390 perfuse_msg_t *pm; 391 struct fuse_entry_out *feo; 392 struct puffs_node *pn; 393 size_t len; 394 int error; 395 396 /* 397 * Prevent further lookups if the parent was removed 398 */ 399 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) 400 return ESTALE; 401 402 if (pnp == NULL) 403 DERRX(EX_SOFTWARE, "pnp must be != NULL"); 404 405 ps = puffs_getspecific(pu); 406 407 #ifdef PERFUSE_DEBUG 408 if (perfuse_diagflags & PDF_FILENAME) 409 DPRINTF("%s: opc = %p, file = \"%s\" looking up \"%s\"\n", 410 __func__, (void *)opc, 411 perfuse_node_path(ps, opc), path); 412 413 if (strcmp(path, ".") == 0) 414 DERRX(EX_SOFTWARE, "unexpected dot-lookup"); 415 416 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_RECLAIMED) 417 DERRX(EX_SOFTWARE, 418 "looking up reclaimed node opc = %p, name = \"%s\"", 419 opc, path); 420 421 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_INVALID) 422 DERRX(EX_SOFTWARE, 423 "looking up freed node opc = %p, name = \"%s\"", 424 opc, path); 425 #endif /* PERFUSE_DEBUG */ 426 427 len = strlen(path) + 1; 428 pm = ps->ps_new_msg(pu, opc, FUSE_LOOKUP, len, pcr); 429 (void)strlcpy(_GET_INPAYLOAD(ps, pm, char *), path, len); 430 431 if ((error = xchg_msg(pu, opc, pm, sizeof(*feo), wait_reply)) != 0) 432 return error; 433 434 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out); 435 436 /* 437 * Starting with ABI 7.4, inode number 0 means ENOENT, 438 * with entry_valid / entry_valid_nsec giving negative 439 * cache timeout (which we do not implement yet). 440 */ 441 if (feo->attr.ino == 0) { 442 ps->ps_destroy_msg(pm); 443 return ENOENT; 444 } 445 446 /* 447 * Check for a known node, not reclaimed, with another name. 448 * It may have been moved, or we can lookup ../ 449 */ 450 if (((oldpnd = perfuse_node_bynodeid(ps, feo->nodeid)) != NULL) && 451 !(oldpnd->pnd_flags & PND_RECLAIMED)) { 452 /* 453 * Save the new node name if not .. 454 */ 455 if (strncmp(path, "..", len) != 0) 456 (void)strlcpy(oldpnd->pnd_name, 457 path, MAXPATHLEN); 458 pn = oldpnd->pnd_pn; 459 460 } else { 461 pn = perfuse_new_pn(pu, path, opc); 462 PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid; 463 perfuse_node_cache(ps, pn); 464 } 465 466 #ifdef PERFUSE_DEBUG 467 if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_RECLAIMED) 468 DERRX(EX_SOFTWARE, 469 "reclaimed in lookup opc = %p, name = \"%s\", ck = %p", 470 opc, path, pn); 471 472 if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_INVALID) 473 DERRX(EX_SOFTWARE, 474 "freed in lookup opc = %p, name = \"%s\", ck = %p", 475 opc, path, pn); 476 #endif /* PERFUSE_DEBUG */ 477 478 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr); 479 pn->pn_va.va_gen = (u_long)(feo->generation); 480 PERFUSE_NODE_DATA(pn)->pnd_fuse_nlookup++; 481 482 *pnp = pn; 483 484 #ifdef PERFUSE_DEBUG 485 if (perfuse_diagflags & PDF_FILENAME) 486 DPRINTF("%s: opc = %p, looked up opc = %p, " 487 "nodeid = 0x%"PRIx64" file = \"%s\"\n", __func__, 488 (void *)opc, pn, feo->nodeid, path); 489 #endif 490 491 if (pni != NULL) { 492 #ifdef PUFFS_KFLAG_CACHE_FS_TTL 493 puffs_newinfo_setva(pni, &pn->pn_va); 494 perfuse_newinfo_setttl(pni, pn, feo, NULL); 495 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */ 496 puffs_newinfo_setcookie(pni, pn); 497 puffs_newinfo_setvtype(pni, pn->pn_va.va_type); 498 puffs_newinfo_setsize(pni, (voff_t)pn->pn_va.va_size); 499 puffs_newinfo_setrdev(pni, pn->pn_va.va_rdev); 500 } 501 502 if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_NODELEAK) { 503 PERFUSE_NODE_DATA(pn)->pnd_flags &= ~PND_NODELEAK; 504 ps->ps_nodeleakcount--; 505 } 506 507 ps->ps_destroy_msg(pm); 508 509 return 0; 510 } 511 512 513 /* 514 * Common code for methods that create objects: 515 * perfuse_node_mkdir 516 * perfuse_node_mknod 517 * perfuse_node_symlink 518 */ 519 static int 520 node_mk_common(struct puffs_usermount *pu, puffs_cookie_t opc, 521 struct puffs_newinfo *pni, const struct puffs_cn *pcn, 522 perfuse_msg_t *pm) 523 { 524 struct perfuse_state *ps; 525 struct puffs_node *pn; 526 struct fuse_entry_out *feo; 527 int error; 528 529 ps = puffs_getspecific(pu); 530 531 if ((error = xchg_msg(pu, opc, pm, sizeof(*feo), wait_reply)) != 0) 532 return error; 533 534 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out); 535 if (feo->nodeid == PERFUSE_UNKNOWN_NODEID) 536 DERRX(EX_SOFTWARE, "%s: no nodeid", __func__); 537 538 pn = perfuse_new_pn(pu, pcn->pcn_name, opc); 539 PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid; 540 PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++; 541 perfuse_node_cache(ps, pn); 542 543 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr); 544 pn->pn_va.va_gen = (u_long)(feo->generation); 545 546 puffs_newinfo_setcookie(pni, pn); 547 #ifdef PUFFS_KFLAG_CACHE_FS_TTL 548 puffs_newinfo_setva(pni, &pn->pn_va); 549 perfuse_newinfo_setttl(pni, pn, feo, NULL); 550 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */ 551 552 553 #ifdef PERFUSE_DEBUG 554 if (perfuse_diagflags & PDF_FILENAME) 555 DPRINTF("%s: opc = %p, file = \"%s\", flags = 0x%x " 556 "nodeid = 0x%"PRIx64"\n", 557 __func__, (void *)pn, pcn->pcn_name, 558 PERFUSE_NODE_DATA(pn)->pnd_flags, feo->nodeid); 559 #endif 560 ps->ps_destroy_msg(pm); 561 562 /* Parents is now dirty */ 563 PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY; 564 565 return 0; 566 } 567 568 static uint64_t 569 readdir_last_cookie(struct fuse_dirent *fd, size_t fd_len) 570 { 571 size_t len; 572 size_t seen = 0; 573 char *ndp; 574 575 do { 576 len = FUSE_DIRENT_ALIGN(sizeof(*fd) + fd->namelen); 577 seen += len; 578 579 if (seen >= fd_len) 580 break; 581 582 ndp = (char *)(void *)fd + (size_t)len; 583 fd = (struct fuse_dirent *)(void *)ndp; 584 } while (1 /* CONSTCOND */); 585 586 return fd->off; 587 } 588 589 static ssize_t 590 fuse_to_dirent(struct puffs_usermount *pu, puffs_cookie_t opc, 591 struct fuse_dirent *fd, size_t fd_len) 592 { 593 struct dirent *dents; 594 size_t dents_len; 595 ssize_t written; 596 uint64_t fd_offset; 597 struct fuse_dirent *fd_base; 598 size_t len; 599 600 fd_base = fd; 601 fd_offset = 0; 602 written = 0; 603 dents = PERFUSE_NODE_DATA(opc)->pnd_dirent; 604 dents_len = (size_t)PERFUSE_NODE_DATA(opc)->pnd_dirent_len; 605 606 do { 607 char *ndp; 608 size_t reclen; 609 610 reclen = _DIRENT_RECLEN(dents, fd->namelen); 611 612 /* 613 * Check we do not overflow the output buffer 614 * struct fuse_dirent is bigger than struct dirent, 615 * so we should always use fd_len and never reallocate 616 * later. 617 * If we have to reallocate,try to double the buffer 618 * each time so that we do not have to do it too often. 619 */ 620 if (written + reclen > dents_len) { 621 if (dents_len == 0) 622 dents_len = fd_len; 623 else 624 dents_len = 625 MAX(2 * dents_len, written + reclen); 626 627 dents = PERFUSE_NODE_DATA(opc)->pnd_dirent; 628 if ((dents = realloc(dents, dents_len)) == NULL) 629 DERR(EX_OSERR, "%s: malloc failed", __func__); 630 631 PERFUSE_NODE_DATA(opc)->pnd_dirent = dents; 632 PERFUSE_NODE_DATA(opc)->pnd_dirent_len = dents_len; 633 634 /* 635 * (void *) for delint 636 */ 637 ndp = (char *)(void *)dents + written; 638 dents = (struct dirent *)(void *)ndp; 639 } 640 641 /* 642 * Filesystem was mounted without -o use_ino 643 * Perform a lookup to find it. 644 */ 645 if (fd->ino == PERFUSE_UNKNOWN_INO) { 646 struct puffs_node *pn; 647 648 if (node_lookup_common(pu, opc, NULL, fd->name, 649 NULL, &pn) != 0) { 650 DWARNX("node_lookup_common failed"); 651 } else { 652 fd->ino = pn->pn_va.va_fileid; 653 (void)perfuse_node_reclaim(pu, pn); 654 } 655 } 656 657 dents->d_fileno = fd->ino; 658 dents->d_reclen = (unsigned short)reclen; 659 dents->d_namlen = fd->namelen; 660 dents->d_type = fd->type; 661 strlcpy(dents->d_name, fd->name, fd->namelen + 1); 662 663 #ifdef PERFUSE_DEBUG 664 if (perfuse_diagflags & PDF_READDIR) 665 DPRINTF("%s: translated \"%s\" ino = %"PRIu64"\n", 666 __func__, dents->d_name, dents->d_fileno); 667 #endif 668 669 dents = _DIRENT_NEXT(dents); 670 written += reclen; 671 672 /* 673 * Move to the next record. 674 * fd->off is not the offset, it is an opaque cookie 675 * given by the filesystem to keep state across multiple 676 * readdir() operation. 677 * Use record alignement instead. 678 */ 679 len = FUSE_DIRENT_ALIGN(sizeof(*fd) + fd->namelen); 680 #ifdef PERFUSE_DEBUG 681 if (perfuse_diagflags & PDF_READDIR) 682 DPRINTF("%s: record at %"PRId64"/0x%"PRIx64" " 683 "length = %zd/0x%zx. " 684 "next record at %"PRId64"/0x%"PRIx64" " 685 "max %zd/0x%zx\n", 686 __func__, fd_offset, fd_offset, len, len, 687 fd_offset + len, fd_offset + len, 688 fd_len, fd_len); 689 #endif 690 fd_offset += len; 691 692 /* 693 * Check if next record is still within the packet 694 * If it is not, we reached the end of the buffer. 695 */ 696 if (fd_offset >= fd_len) 697 break; 698 699 /* 700 * (void *) for delint 701 */ 702 ndp = (char *)(void *)fd_base + (size_t)fd_offset; 703 fd = (struct fuse_dirent *)(void *)ndp; 704 705 } while (1 /* CONSTCOND */); 706 707 /* 708 * Adjust the dirent output length 709 */ 710 if (written != -1) 711 PERFUSE_NODE_DATA(opc)->pnd_dirent_len = written; 712 713 return written; 714 } 715 716 static void 717 readdir_buffered(puffs_cookie_t opc, struct dirent *dent, off_t *readoff, 718 size_t *reslen) 719 { 720 struct dirent *fromdent; 721 struct perfuse_node_data *pnd; 722 char *ndp; 723 724 pnd = PERFUSE_NODE_DATA(opc); 725 726 while (*readoff < pnd->pnd_dirent_len) { 727 /* 728 * (void *) for delint 729 */ 730 ndp = (char *)(void *)pnd->pnd_dirent + (size_t)*readoff; 731 fromdent = (struct dirent *)(void *)ndp; 732 733 if (*reslen < _DIRENT_SIZE(fromdent)) 734 break; 735 736 memcpy(dent, fromdent, _DIRENT_SIZE(fromdent)); 737 *readoff += _DIRENT_SIZE(fromdent); 738 *reslen -= _DIRENT_SIZE(fromdent); 739 740 dent = _DIRENT_NEXT(dent); 741 } 742 743 #ifdef PERFUSE_DEBUG 744 if (perfuse_diagflags & PDF_READDIR) 745 DPRINTF("%s: readoff = %"PRId64", " 746 "pnd->pnd_dirent_len = %"PRId64"\n", 747 __func__, *readoff, pnd->pnd_dirent_len); 748 #endif 749 if (*readoff >= pnd->pnd_dirent_len) { 750 free(pnd->pnd_dirent); 751 pnd->pnd_dirent = NULL; 752 pnd->pnd_dirent_len = 0; 753 } 754 755 return; 756 } 757 758 759 static void 760 node_ref(puffs_cookie_t opc) 761 { 762 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc); 763 764 #ifdef PERFUSE_DEBUG 765 if (pnd->pnd_flags & PND_INVALID) 766 DERRX(EX_SOFTWARE, "Use of freed node opc = %p", opc); 767 #endif /* PERFUSE_DEBUG */ 768 769 pnd->pnd_ref++; 770 return; 771 } 772 773 static void 774 node_rele(puffs_cookie_t opc) 775 { 776 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc); 777 778 #ifdef PERFUSE_DEBUG 779 if (pnd->pnd_flags & PND_INVALID) 780 DERRX(EX_SOFTWARE, "Use of freed node opc = %p", opc); 781 #endif /* PERFUSE_DEBUG */ 782 783 pnd->pnd_ref--; 784 785 if (pnd->pnd_ref == 0) 786 (void)dequeue_requests(opc, PCQ_REF, DEQUEUE_ALL); 787 788 return; 789 } 790 791 static void 792 requeue_request(struct puffs_usermount *pu, puffs_cookie_t opc, 793 enum perfuse_qtype type) 794 { 795 struct perfuse_cc_queue pcq; 796 struct perfuse_node_data *pnd; 797 #ifdef PERFUSE_DEBUG 798 struct perfuse_state *ps; 799 800 ps = perfuse_getspecific(pu); 801 #endif 802 803 pnd = PERFUSE_NODE_DATA(opc); 804 pcq.pcq_type = type; 805 pcq.pcq_cc = puffs_cc_getcc(pu); 806 TAILQ_INSERT_TAIL(&pnd->pnd_pcq, &pcq, pcq_next); 807 808 #ifdef PERFUSE_DEBUG 809 if (perfuse_diagflags & PDF_REQUEUE) 810 DPRINTF("%s: REQUEUE opc = %p, pcc = %p (%s)\n", 811 __func__, (void *)opc, pcq.pcq_cc, 812 perfuse_qtypestr[type]); 813 #endif 814 815 puffs_cc_yield(pcq.pcq_cc); 816 TAILQ_REMOVE(&pnd->pnd_pcq, &pcq, pcq_next); 817 818 #ifdef PERFUSE_DEBUG 819 if (perfuse_diagflags & PDF_REQUEUE) 820 DPRINTF("%s: RESUME opc = %p, pcc = %p (%s)\n", 821 __func__, (void *)opc, pcq.pcq_cc, 822 perfuse_qtypestr[type]); 823 #endif 824 825 return; 826 } 827 828 static int 829 dequeue_requests(puffs_cookie_t opc, enum perfuse_qtype type, int max) 830 { 831 struct perfuse_cc_queue *pcq; 832 struct perfuse_node_data *pnd; 833 int dequeued; 834 835 pnd = PERFUSE_NODE_DATA(opc); 836 dequeued = 0; 837 TAILQ_FOREACH(pcq, &pnd->pnd_pcq, pcq_next) { 838 if (pcq->pcq_type != type) 839 continue; 840 841 #ifdef PERFUSE_DEBUG 842 if (perfuse_diagflags & PDF_REQUEUE) 843 DPRINTF("%s: SCHEDULE opc = %p, pcc = %p (%s)\n", 844 __func__, (void *)opc, pcq->pcq_cc, 845 perfuse_qtypestr[type]); 846 #endif 847 puffs_cc_schedule(pcq->pcq_cc); 848 849 if (++dequeued == max) 850 break; 851 } 852 853 #ifdef PERFUSE_DEBUG 854 if (perfuse_diagflags & PDF_REQUEUE) 855 DPRINTF("%s: DONE opc = %p\n", __func__, (void *)opc); 856 #endif 857 858 return dequeued; 859 } 860 861 void 862 perfuse_fs_init(struct puffs_usermount *pu) 863 { 864 struct perfuse_state *ps; 865 perfuse_msg_t *pm; 866 struct fuse_init_in *fii; 867 struct fuse_init_out *fio; 868 int error; 869 870 ps = puffs_getspecific(pu); 871 872 if (puffs_mount(pu, ps->ps_target, ps->ps_mountflags, ps->ps_root) != 0) 873 DERR(EX_OSERR, "%s: puffs_mount failed", __func__); 874 875 /* 876 * Linux 2.6.34.1 sends theses flags: 877 * FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC 878 * FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK 879 * 880 * Linux also sets max_readahead at 32 pages (128 kB) 881 * 882 * ps_new_msg() is called with NULL creds, which will 883 * be interpreted as FUSE superuser. 884 */ 885 pm = ps->ps_new_msg(pu, 0, FUSE_INIT, sizeof(*fii), NULL); 886 fii = GET_INPAYLOAD(ps, pm, fuse_init_in); 887 fii->major = FUSE_KERNEL_VERSION; 888 fii->minor = FUSE_KERNEL_MINOR_VERSION; 889 fii->max_readahead = (unsigned int)(32 * sysconf(_SC_PAGESIZE)); 890 fii->flags = (FUSE_ASYNC_READ|FUSE_POSIX_LOCKS|FUSE_ATOMIC_O_TRUNC); 891 892 if ((error = xchg_msg(pu, 0, pm, sizeof(*fio), wait_reply)) != 0) 893 DERRX(EX_SOFTWARE, "init message exchange failed (%d)", error); 894 895 fio = GET_OUTPAYLOAD(ps, pm, fuse_init_out); 896 ps->ps_max_readahead = fio->max_readahead; 897 ps->ps_max_write = fio->max_write; 898 899 ps->ps_destroy_msg(pm); 900 901 return; 902 } 903 904 int 905 perfuse_fs_unmount(struct puffs_usermount *pu, int flags) 906 { 907 perfuse_msg_t *pm; 908 struct perfuse_state *ps; 909 puffs_cookie_t opc; 910 int error; 911 912 ps = puffs_getspecific(pu); 913 opc = (puffs_cookie_t)puffs_getroot(pu); 914 915 /* 916 * ps_new_msg() is called with NULL creds, which will 917 * be interpreted as FUSE superuser. 918 */ 919 pm = ps->ps_new_msg(pu, opc, FUSE_DESTROY, 0, NULL); 920 921 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0){ 922 DWARN("unmount %s", ps->ps_target); 923 if (!(flags & MNT_FORCE)) 924 return error; 925 else 926 error = 0; 927 } else { 928 ps->ps_destroy_msg(pm); 929 } 930 931 ps->ps_umount(pu); 932 933 if (perfuse_diagflags & PDF_MISC) 934 DPRINTF("%s unmounted, exit\n", ps->ps_target); 935 936 return 0; 937 } 938 939 int 940 perfuse_fs_statvfs(struct puffs_usermount *pu, struct statvfs *svfsb) 941 { 942 struct perfuse_state *ps; 943 perfuse_msg_t *pm; 944 puffs_cookie_t opc; 945 struct fuse_statfs_out *fso; 946 int error; 947 948 ps = puffs_getspecific(pu); 949 opc = (puffs_cookie_t)puffs_getroot(pu); 950 951 /* 952 * ps_new_msg() is called with NULL creds, which will 953 * be interpreted as FUSE superuser. 954 */ 955 pm = ps->ps_new_msg(pu, opc, FUSE_STATFS, 0, NULL); 956 957 if ((error = xchg_msg(pu, opc, pm, sizeof(*fso), wait_reply)) != 0) 958 return error; 959 960 fso = GET_OUTPAYLOAD(ps, pm, fuse_statfs_out); 961 svfsb->f_flag = ps->ps_mountflags; 962 svfsb->f_bsize = fso->st.bsize; 963 svfsb->f_frsize = fso->st.frsize; 964 svfsb->f_iosize = ((struct puffs_node *)opc)->pn_va.va_blocksize; 965 svfsb->f_blocks = fso->st.blocks; 966 svfsb->f_bfree = fso->st.bfree; 967 svfsb->f_bavail = fso->st.bavail; 968 svfsb->f_bresvd = fso->st.bfree - fso->st.bavail; 969 svfsb->f_files = fso->st.files; 970 svfsb->f_ffree = fso->st.ffree; 971 svfsb->f_favail = fso->st.ffree;/* files not reserved for root */ 972 svfsb->f_fresvd = 0; /* files reserved for root */ 973 974 svfsb->f_syncreads = ps->ps_syncreads; 975 svfsb->f_syncwrites = ps->ps_syncwrites; 976 977 svfsb->f_asyncreads = ps->ps_asyncreads; 978 svfsb->f_asyncwrites = ps->ps_asyncwrites; 979 980 (void)memcpy(&svfsb->f_fsidx, &ps->ps_fsid, sizeof(ps->ps_fsid)); 981 svfsb->f_fsid = (unsigned long)ps->ps_fsid; 982 svfsb->f_namemax = MAXPATHLEN; /* XXX */ 983 svfsb->f_owner = ps->ps_owner_uid; 984 985 (void)strlcpy(svfsb->f_mntonname, ps->ps_target, _VFS_NAMELEN); 986 987 if (ps->ps_filesystemtype != NULL) 988 (void)strlcpy(svfsb->f_fstypename, 989 ps->ps_filesystemtype, _VFS_NAMELEN); 990 else 991 (void)strlcpy(svfsb->f_fstypename, "fuse", _VFS_NAMELEN); 992 993 if (ps->ps_source != NULL) 994 strlcpy(svfsb->f_mntfromname, ps->ps_source, _VFS_NAMELEN); 995 else 996 strlcpy(svfsb->f_mntfromname, _PATH_FUSE, _VFS_NAMELEN); 997 998 ps->ps_destroy_msg(pm); 999 1000 return 0; 1001 } 1002 1003 int 1004 perfuse_fs_sync(struct puffs_usermount *pu, int waitfor, 1005 const struct puffs_cred *pcr) 1006 { 1007 /* 1008 * FUSE does not seem to have a FS sync callback. 1009 * Maybe do not even register this callback 1010 */ 1011 return puffs_fsnop_sync(pu, waitfor, pcr); 1012 } 1013 1014 /* ARGSUSED0 */ 1015 int 1016 perfuse_fs_fhtonode(struct puffs_usermount *pu, void *fid, size_t fidsize, 1017 struct puffs_newinfo *pni) 1018 { 1019 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__); 1020 return 0; 1021 } 1022 1023 /* ARGSUSED0 */ 1024 int 1025 perfuse_fs_nodetofh(struct puffs_usermount *pu, puffs_cookie_t cookie, 1026 void *fid, size_t *fidsize) 1027 { 1028 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__); 1029 return 0; 1030 } 1031 1032 #if 0 1033 /* ARGSUSED0 */ 1034 void 1035 perfuse_fs_extattrctl(struct puffs_usermount *pu, int cmd, 1036 puffs_cookie_t *cookie, int flags, int namespace, const char *attrname) 1037 { 1038 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__); 1039 return 0; 1040 } 1041 #endif /* 0 */ 1042 1043 /* ARGSUSED0 */ 1044 void 1045 perfuse_fs_suspend(struct puffs_usermount *pu, int status) 1046 { 1047 return; 1048 } 1049 1050 1051 int 1052 perfuse_node_lookup(struct puffs_usermount *pu, puffs_cookie_t opc, 1053 struct puffs_newinfo *pni, const struct puffs_cn *pcn) 1054 { 1055 struct perfuse_state *ps; 1056 struct puffs_node *pn; 1057 mode_t mode; 1058 int error; 1059 1060 ps = puffs_getspecific(pu); 1061 node_ref(opc); 1062 1063 /* 1064 * Check permissions 1065 */ 1066 switch(pcn->pcn_nameiop) { 1067 case NAMEI_DELETE: /* FALLTHROUGH */ 1068 case NAMEI_RENAME: /* FALLTHROUGH */ 1069 case NAMEI_CREATE: 1070 if (pcn->pcn_flags & NAMEI_ISLASTCN) 1071 mode = PUFFS_VEXEC|PUFFS_VWRITE; 1072 else 1073 mode = PUFFS_VEXEC; 1074 break; 1075 case NAMEI_LOOKUP: /* FALLTHROUGH */ 1076 default: 1077 mode = PUFFS_VEXEC; 1078 break; 1079 } 1080 1081 if ((error = mode_access(opc, pcn->pcn_cred, mode)) != 0) 1082 goto out; 1083 1084 error = node_lookup_common(pu, (puffs_cookie_t)opc, pni, 1085 pcn->pcn_name, pcn->pcn_cred, &pn); 1086 1087 if (error != 0) 1088 goto out; 1089 1090 /* 1091 * Kernel would kill us if the filesystem returned the parent 1092 * itself. If we want to live, hide that! 1093 */ 1094 if ((opc == (puffs_cookie_t)pn) && (strcmp(pcn->pcn_name, ".") != 0)) { 1095 DERRX(EX_SOFTWARE, "lookup \"%s\" in \"%s\" returned parent", 1096 pcn->pcn_name, perfuse_node_path(ps, opc)); 1097 /* NOTREACHED */ 1098 error = ESTALE; 1099 goto out; 1100 } 1101 1102 /* 1103 * Removed node 1104 */ 1105 if (PERFUSE_NODE_DATA(pn)->pnd_flags & PND_REMOVED) { 1106 error = ENOENT; 1107 goto out; 1108 } 1109 1110 /* 1111 * Check for sticky bit. Unfortunately there is no way to 1112 * do this before creating the puffs_node, since we require 1113 * this operation to get the node owner. 1114 */ 1115 switch (pcn->pcn_nameiop) { 1116 case NAMEI_DELETE: /* FALLTHROUGH */ 1117 case NAMEI_RENAME: 1118 error = sticky_access(opc, pn, pcn->pcn_cred); 1119 if (error != 0) { 1120 (void)perfuse_node_reclaim(pu, pn); 1121 goto out; 1122 } 1123 break; 1124 default: 1125 break; 1126 } 1127 1128 PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++; 1129 1130 error = 0; 1131 1132 out: 1133 node_rele(opc); 1134 return error; 1135 } 1136 1137 int 1138 perfuse_node_create(struct puffs_usermount *pu, puffs_cookie_t opc, 1139 struct puffs_newinfo *pni, const struct puffs_cn *pcn, 1140 const struct vattr *vap) 1141 { 1142 perfuse_msg_t *pm; 1143 struct perfuse_state *ps; 1144 struct fuse_create_in *fci; 1145 struct fuse_entry_out *feo; 1146 struct fuse_open_out *foo; 1147 struct puffs_node *pn; 1148 const char *name; 1149 size_t namelen; 1150 size_t len; 1151 int error; 1152 1153 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) 1154 return ENOENT; 1155 1156 node_ref(opc); 1157 1158 /* 1159 * If create is unimplemented: Check that it does not 1160 * already exists, and if not, do mknod and open 1161 */ 1162 ps = puffs_getspecific(pu); 1163 if (ps->ps_flags & PS_NO_CREAT) { 1164 error = node_lookup_common(pu, opc, NULL, pcn->pcn_name, 1165 pcn->pcn_cred, &pn); 1166 if (error == 0) { 1167 (void)perfuse_node_reclaim(pu, pn); 1168 error = EEXIST; 1169 goto out; 1170 } 1171 1172 error = perfuse_node_mknod(pu, opc, pni, pcn, vap); 1173 if (error != 0) 1174 goto out; 1175 1176 error = node_lookup_common(pu, opc, NULL, pcn->pcn_name, 1177 pcn->pcn_cred, &pn); 1178 if (error != 0) 1179 goto out; 1180 1181 /* 1182 * FUSE does the open at create time, while 1183 * NetBSD will open in a subsequent operation. 1184 * We need to open now, in order to retain FUSE 1185 * semantics. The calling process will not get 1186 * a file descriptor before the kernel sends 1187 * the open operation. 1188 */ 1189 error = perfuse_node_open(pu, (puffs_cookie_t)pn, 1190 FWRITE, pcn->pcn_cred); 1191 goto out; 1192 } 1193 1194 name = pcn->pcn_name; 1195 namelen = pcn->pcn_namelen + 1; 1196 len = sizeof(*fci) + namelen; 1197 1198 /* 1199 * flags should use O_WRONLY instead of O_RDWR, but it 1200 * breaks when the caller tries to read from file. 1201 * 1202 * mode must contain file type (ie: S_IFREG), use VTTOIF(vap->va_type) 1203 */ 1204 pm = ps->ps_new_msg(pu, opc, FUSE_CREATE, len, pcn->pcn_cred); 1205 fci = GET_INPAYLOAD(ps, pm, fuse_create_in); 1206 fci->flags = O_CREAT | O_TRUNC | O_RDWR; 1207 fci->mode = vap->va_mode | VTTOIF(vap->va_type); 1208 fci->umask = 0; /* Seems unused by libfuse */ 1209 (void)strlcpy((char*)(void *)(fci + 1), name, namelen); 1210 1211 len = sizeof(*feo) + sizeof(*foo); 1212 if ((error = xchg_msg(pu, opc, pm, len, wait_reply)) != 0) { 1213 /* 1214 * create is unimplmented, remember it for later, 1215 * and start over using mknod and open instead. 1216 */ 1217 if (error == ENOSYS) { 1218 ps->ps_flags |= PS_NO_CREAT; 1219 error = perfuse_node_create(pu, opc, pni, pcn, vap); 1220 } 1221 1222 goto out; 1223 } 1224 1225 feo = GET_OUTPAYLOAD(ps, pm, fuse_entry_out); 1226 foo = (struct fuse_open_out *)(void *)(feo + 1); 1227 if (feo->nodeid == PERFUSE_UNKNOWN_NODEID) 1228 DERRX(EX_SOFTWARE, "%s: no nodeid", __func__); 1229 1230 /* 1231 * Save the file handle and inode in node private data 1232 * so that we can reuse it later 1233 */ 1234 pn = perfuse_new_pn(pu, name, opc); 1235 perfuse_new_fh((puffs_cookie_t)pn, foo->fh, FWRITE); 1236 PERFUSE_NODE_DATA(pn)->pnd_nodeid = feo->nodeid; 1237 PERFUSE_NODE_DATA(pn)->pnd_puffs_nlookup++; 1238 perfuse_node_cache(ps, pn); 1239 1240 fuse_attr_to_vap(ps, &pn->pn_va, &feo->attr); 1241 pn->pn_va.va_gen = (u_long)(feo->generation); 1242 1243 puffs_newinfo_setcookie(pni, pn); 1244 #ifdef PUFFS_KFLAG_CACHE_FS_TTL 1245 puffs_newinfo_setva(pni, &pn->pn_va); 1246 perfuse_newinfo_setttl(pni, pn, feo, NULL); 1247 #endif /* PUFFS_KFLAG_CACHE_FS_TTL */ 1248 1249 #ifdef PERFUSE_DEBUG 1250 if (perfuse_diagflags & (PDF_FH|PDF_FILENAME)) 1251 DPRINTF("%s: opc = %p, file = \"%s\", flags = 0x%x " 1252 "nodeid = 0x%"PRIx64", wfh = 0x%"PRIx64"\n", 1253 __func__, (void *)pn, pcn->pcn_name, 1254 PERFUSE_NODE_DATA(pn)->pnd_flags, feo->nodeid, 1255 foo->fh); 1256 #endif 1257 1258 ps->ps_destroy_msg(pm); 1259 error = 0; 1260 1261 out: 1262 node_rele(opc); 1263 return error; 1264 } 1265 1266 1267 int 1268 perfuse_node_mknod(struct puffs_usermount *pu, puffs_cookie_t opc, 1269 struct puffs_newinfo *pni, const struct puffs_cn *pcn, 1270 const struct vattr *vap) 1271 { 1272 struct perfuse_state *ps; 1273 perfuse_msg_t *pm; 1274 struct fuse_mknod_in *fmi; 1275 const char* path; 1276 size_t len; 1277 int error; 1278 1279 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) 1280 return ENOENT; 1281 1282 node_ref(opc); 1283 1284 /* 1285 * Only superuser can mknod objects other than 1286 * directories, files, socks, fifo and links. 1287 * 1288 * Create an object require -WX permission in the parent directory 1289 */ 1290 switch (vap->va_type) { 1291 case VDIR: /* FALLTHROUGH */ 1292 case VREG: /* FALLTHROUGH */ 1293 case VFIFO: /* FALLTHROUGH */ 1294 case VSOCK: 1295 break; 1296 default: /* VNON, VBLK, VCHR, VBAD */ 1297 if (!puffs_cred_isjuggernaut(pcn->pcn_cred)) { 1298 error = EACCES; 1299 goto out; 1300 } 1301 break; 1302 } 1303 1304 1305 ps = puffs_getspecific(pu); 1306 path = pcn->pcn_name; 1307 len = sizeof(*fmi) + pcn->pcn_namelen + 1; 1308 1309 /* 1310 * mode must contain file type (ie: S_IFREG), use VTTOIF(vap->va_type) 1311 */ 1312 pm = ps->ps_new_msg(pu, opc, FUSE_MKNOD, len, pcn->pcn_cred); 1313 fmi = GET_INPAYLOAD(ps, pm, fuse_mknod_in); 1314 fmi->mode = vap->va_mode | VTTOIF(vap->va_type); 1315 fmi->rdev = (uint32_t)vap->va_rdev; 1316 fmi->umask = 0; /* Seems unused bu libfuse */ 1317 (void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi)); 1318 1319 error = node_mk_common(pu, opc, pni, pcn, pm); 1320 1321 out: 1322 node_rele(opc); 1323 return error; 1324 } 1325 1326 1327 int 1328 perfuse_node_open(struct puffs_usermount *pu, puffs_cookie_t opc, int mode, 1329 const struct puffs_cred *pcr) 1330 { 1331 struct perfuse_state *ps; 1332 struct perfuse_node_data *pnd; 1333 perfuse_msg_t *pm; 1334 mode_t fmode; 1335 int op; 1336 struct fuse_open_in *foi; 1337 struct fuse_open_out *foo; 1338 struct puffs_node *pn; 1339 int error; 1340 1341 ps = puffs_getspecific(pu); 1342 pn = (struct puffs_node *)opc; 1343 pnd = PERFUSE_NODE_DATA(opc); 1344 error = 0; 1345 1346 if (pnd->pnd_flags & PND_REMOVED) 1347 return ENOENT; 1348 1349 node_ref(opc); 1350 1351 if (puffs_pn_getvap(pn)->va_type == VDIR) 1352 op = FUSE_OPENDIR; 1353 else 1354 op = FUSE_OPEN; 1355 1356 /* 1357 * libfuse docs says 1358 * - O_CREAT and O_EXCL should never be set. 1359 * - O_TRUNC may be used if mount option atomic_o_trunc is used XXX 1360 * 1361 * O_APPEND makes no sense since FUSE always sends 1362 * the file offset for write operations. If the 1363 * filesystem uses pwrite(), O_APPEND would cause 1364 * the offset to be ignored and cause file corruption. 1365 */ 1366 mode &= ~(O_CREAT|O_EXCL|O_APPEND); 1367 1368 /* 1369 * Do not open twice, and do not reopen for reading 1370 * if we already have write handle. 1371 */ 1372 if (((mode & FREAD) && (pnd->pnd_flags & PND_RFH)) || 1373 ((mode & FREAD) && (pnd->pnd_flags & PND_WFH)) || 1374 ((mode & FWRITE) && (pnd->pnd_flags & PND_WFH))) 1375 goto out; 1376 1377 /* 1378 * Queue open on a node so that we do not open 1379 * twice. This would be better with read and 1380 * write distinguished. 1381 */ 1382 while (pnd->pnd_flags & PND_INOPEN) 1383 requeue_request(pu, opc, PCQ_OPEN); 1384 pnd->pnd_flags |= PND_INOPEN; 1385 1386 /* 1387 * Convert PUFFS mode to FUSE mode: convert FREAD/FWRITE 1388 * to O_RDONLY/O_WRONLY while perserving the other options. 1389 */ 1390 fmode = mode & ~(FREAD|FWRITE); 1391 fmode |= (mode & FWRITE) ? O_RDWR : O_RDONLY; 1392 1393 pm = ps->ps_new_msg(pu, opc, op, sizeof(*foi), pcr); 1394 foi = GET_INPAYLOAD(ps, pm, fuse_open_in); 1395 foi->flags = fmode; 1396 foi->unused = 0; 1397 1398 if ((error = xchg_msg(pu, opc, pm, sizeof(*foo), wait_reply)) != 0) 1399 goto out; 1400 1401 foo = GET_OUTPAYLOAD(ps, pm, fuse_open_out); 1402 1403 /* 1404 * Save the file handle in node private data 1405 * so that we can reuse it later 1406 */ 1407 perfuse_new_fh(opc, foo->fh, mode); 1408 1409 #ifdef PERFUSE_DEBUG 1410 if (perfuse_diagflags & (PDF_FH|PDF_FILENAME)) 1411 DPRINTF("%s: opc = %p, file = \"%s\", " 1412 "nodeid = 0x%"PRIx64", %s%sfh = 0x%"PRIx64"\n", 1413 __func__, (void *)opc, perfuse_node_path(ps, opc), 1414 pnd->pnd_nodeid, mode & FREAD ? "r" : "", 1415 mode & FWRITE ? "w" : "", foo->fh); 1416 #endif 1417 1418 ps->ps_destroy_msg(pm); 1419 out: 1420 1421 pnd->pnd_flags &= ~PND_INOPEN; 1422 (void)dequeue_requests(opc, PCQ_OPEN, DEQUEUE_ALL); 1423 1424 node_rele(opc); 1425 return error; 1426 } 1427 1428 /* ARGSUSED0 */ 1429 int 1430 perfuse_node_close(struct puffs_usermount *pu, puffs_cookie_t opc, int flags, 1431 const struct puffs_cred *pcr) 1432 { 1433 struct perfuse_node_data *pnd; 1434 1435 pnd = PERFUSE_NODE_DATA(opc); 1436 1437 if (!(pnd->pnd_flags & PND_OPEN)) 1438 return EBADF; 1439 1440 /* 1441 * Actual close is postponed at inactive time. 1442 */ 1443 return 0; 1444 } 1445 1446 int 1447 perfuse_node_access(struct puffs_usermount *pu, puffs_cookie_t opc, int mode, 1448 const struct puffs_cred *pcr) 1449 { 1450 perfuse_msg_t *pm; 1451 struct perfuse_state *ps; 1452 struct fuse_access_in *fai; 1453 int error; 1454 1455 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) 1456 return ENOENT; 1457 1458 node_ref(opc); 1459 1460 /* 1461 * If we previously detected the filesystem does not 1462 * implement access(), short-circuit the call and skip 1463 * to libpuffs access() emulation. 1464 */ 1465 ps = puffs_getspecific(pu); 1466 if (ps->ps_flags & PS_NO_ACCESS) { 1467 const struct vattr *vap; 1468 1469 vap = puffs_pn_getvap((struct puffs_node *)opc); 1470 1471 error = puffs_access(IFTOVT(vap->va_mode), 1472 vap->va_mode & ACCESSPERMS, 1473 vap->va_uid, vap->va_gid, 1474 (mode_t)mode, pcr); 1475 goto out; 1476 } 1477 1478 /* 1479 * Plain access call 1480 */ 1481 pm = ps->ps_new_msg(pu, opc, FUSE_ACCESS, sizeof(*fai), pcr); 1482 fai = GET_INPAYLOAD(ps, pm, fuse_access_in); 1483 fai->mask = 0; 1484 fai->mask |= (mode & PUFFS_VREAD) ? R_OK : 0; 1485 fai->mask |= (mode & PUFFS_VWRITE) ? W_OK : 0; 1486 fai->mask |= (mode & PUFFS_VEXEC) ? X_OK : 0; 1487 1488 error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply); 1489 1490 ps->ps_destroy_msg(pm); 1491 1492 /* 1493 * If unimplemented, start over with emulation 1494 */ 1495 if (error == ENOSYS) { 1496 ps->ps_flags |= PS_NO_ACCESS; 1497 error = perfuse_node_access(pu, opc, mode, pcr); 1498 } 1499 1500 out: 1501 node_rele(opc); 1502 return error; 1503 } 1504 1505 int 1506 perfuse_node_getattr(struct puffs_usermount *pu, puffs_cookie_t opc, 1507 struct vattr *vap, const struct puffs_cred *pcr) 1508 { 1509 return perfuse_node_getattr_ttl(pu, opc, vap, pcr, NULL); 1510 } 1511 1512 int 1513 perfuse_node_getattr_ttl(struct puffs_usermount *pu, puffs_cookie_t opc, 1514 struct vattr *vap, const struct puffs_cred *pcr, 1515 struct timespec *va_ttl) 1516 { 1517 perfuse_msg_t *pm = NULL; 1518 struct perfuse_state *ps; 1519 struct perfuse_node_data *pnd = PERFUSE_NODE_DATA(opc); 1520 struct fuse_getattr_in *fgi; 1521 struct fuse_attr_out *fao; 1522 int error = 0; 1523 1524 if (pnd->pnd_flags & PND_REMOVED) 1525 return ENOENT; 1526 1527 node_ref(opc); 1528 1529 /* 1530 * Serialize size access, see comment in perfuse_node_setattr(). 1531 */ 1532 while (pnd->pnd_flags & PND_INRESIZE) 1533 requeue_request(pu, opc, PCQ_RESIZE); 1534 pnd->pnd_flags |= PND_INRESIZE; 1535 1536 ps = puffs_getspecific(pu); 1537 1538 /* 1539 * FUSE_GETATTR_FH must be set in fgi->flags 1540 * if we use for fgi->fh 1541 */ 1542 pm = ps->ps_new_msg(pu, opc, FUSE_GETATTR, sizeof(*fgi), pcr); 1543 fgi = GET_INPAYLOAD(ps, pm, fuse_getattr_in); 1544 fgi->getattr_flags = 0; 1545 fgi->dummy = 0; 1546 fgi->fh = 0; 1547 1548 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_OPEN) { 1549 fgi->fh = perfuse_get_fh(opc, FREAD); 1550 fgi->getattr_flags |= FUSE_GETATTR_FH; 1551 } 1552 1553 #ifdef PERFUSE_DEBUG 1554 if (perfuse_diagflags & PDF_RESIZE) 1555 DPRINTF(">> %s %p %" PRIu64 "\n", __func__, (void *)opc, 1556 vap->va_size); 1557 #endif 1558 1559 if ((error = xchg_msg(pu, opc, pm, sizeof(*fao), wait_reply)) != 0) 1560 goto out; 1561 1562 fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out); 1563 1564 #ifdef PERFUSE_DEBUG 1565 if (perfuse_diagflags & PDF_RESIZE) 1566 DPRINTF("<< %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__, 1567 (void *)opc, vap->va_size, fao->attr.size); 1568 #endif 1569 1570 /* 1571 * We set birthtime, flags, filerev,vaflags to 0. 1572 * This seems the best bet, since the information is 1573 * not available from filesystem. 1574 */ 1575 fuse_attr_to_vap(ps, vap, &fao->attr); 1576 1577 if (va_ttl != NULL) { 1578 va_ttl->tv_sec = fao->attr_valid; 1579 va_ttl->tv_nsec = fao->attr_valid_nsec; 1580 } 1581 1582 ps->ps_destroy_msg(pm); 1583 error = 0; 1584 out: 1585 1586 pnd->pnd_flags &= ~PND_INRESIZE; 1587 (void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL); 1588 1589 node_rele(opc); 1590 return error; 1591 } 1592 1593 int 1594 perfuse_node_setattr(struct puffs_usermount *pu, puffs_cookie_t opc, 1595 const struct vattr *vap, const struct puffs_cred *pcr) 1596 { 1597 return perfuse_node_setattr_ttl(pu, opc, 1598 __UNCONST(vap), pcr, NULL, 0); 1599 } 1600 1601 int 1602 perfuse_node_setattr_ttl(struct puffs_usermount *pu, puffs_cookie_t opc, 1603 struct vattr *vap, const struct puffs_cred *pcr, 1604 struct timespec *va_ttl, int xflag) 1605 { 1606 perfuse_msg_t *pm; 1607 uint64_t fh; 1608 struct perfuse_state *ps; 1609 struct perfuse_node_data *pnd; 1610 struct fuse_setattr_in *fsi; 1611 struct fuse_attr_out *fao; 1612 struct vattr *old_va; 1613 enum perfuse_xchg_pb_reply reply; 1614 int error; 1615 #ifdef PERFUSE_DEBUG 1616 struct vattr *old_vap; 1617 int resize_debug = 0; 1618 #endif 1619 ps = puffs_getspecific(pu); 1620 pnd = PERFUSE_NODE_DATA(opc); 1621 1622 /* 1623 * The only operation we can do once the file is removed 1624 * is to resize it, and we can do it only if it is open. 1625 * Do not even send the operation to the filesystem: the 1626 * file is not there anymore. 1627 */ 1628 if (pnd->pnd_flags & PND_REMOVED) { 1629 if (!(pnd->pnd_flags & PND_OPEN)) 1630 return ENOENT; 1631 1632 return 0; 1633 } 1634 1635 old_va = puffs_pn_getvap((struct puffs_node *)opc); 1636 1637 /* 1638 * Check for permission to change size 1639 * It is always allowed if we already have a write file handle 1640 */ 1641 if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) && 1642 !(pnd->pnd_flags & PND_WFH) && 1643 (error = mode_access(opc, pcr, PUFFS_VWRITE)) != 0) 1644 return error; 1645 1646 /* 1647 * Check for permission to change dates 1648 */ 1649 if (((vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) || 1650 (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL)) && 1651 (puffs_access_times(old_va->va_uid, old_va->va_gid, 1652 old_va->va_mode, 0, pcr) != 0)) 1653 return EACCES; 1654 1655 /* 1656 * Check for permission to change owner and group 1657 */ 1658 if (((vap->va_uid != (uid_t)PUFFS_VNOVAL) || 1659 (vap->va_gid != (gid_t)PUFFS_VNOVAL)) && 1660 (puffs_access_chown(old_va->va_uid, old_va->va_gid, 1661 vap->va_uid, vap->va_gid, pcr)) != 0) 1662 return EACCES; 1663 1664 /* 1665 * Check for permission to change permissions 1666 */ 1667 if ((vap->va_mode != (mode_t)PUFFS_VNOVAL) && 1668 (puffs_access_chmod(old_va->va_uid, old_va->va_gid, 1669 old_va->va_type, vap->va_mode, pcr)) != 0) 1670 return EACCES; 1671 1672 node_ref(opc); 1673 1674 if (pnd->pnd_flags & PND_WFH) 1675 fh = perfuse_get_fh(opc, FWRITE); 1676 else 1677 fh = FUSE_UNKNOWN_FH; 1678 1679 /* 1680 * fchmod() sets mode and fh, and it may carry 1681 * a resize as well. That may break if the 1682 * filesystem does chmod then resize, and fails 1683 * because it does not have permission anymore. 1684 * We work this around by splitting into two setattr. 1685 */ 1686 if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) && 1687 (vap->va_mode != (mode_t)PUFFS_VNOVAL) && 1688 (fh != FUSE_UNKNOWN_FH)) { 1689 struct vattr resize_va; 1690 1691 (void)memcpy(&resize_va, vap, sizeof(resize_va)); 1692 resize_va.va_mode = (mode_t)PUFFS_VNOVAL; 1693 if ((error = perfuse_node_setattr_ttl(pu, opc, &resize_va, 1694 pcr, va_ttl, xflag)) != 0) 1695 goto out2; 1696 1697 vap->va_size = (u_quad_t)PUFFS_VNOVAL; 1698 } 1699 1700 pm = ps->ps_new_msg(pu, opc, FUSE_SETATTR, sizeof(*fsi), pcr); 1701 fsi = GET_INPAYLOAD(ps, pm, fuse_setattr_in); 1702 fsi->valid = 0; 1703 1704 /* 1705 * Get a fh if the node is open for writing 1706 */ 1707 if (fh != FUSE_UNKNOWN_FH) { 1708 fsi->fh = fh; 1709 fsi->valid |= FUSE_FATTR_FH; 1710 } 1711 1712 1713 if (vap->va_size != (u_quad_t)PUFFS_VNOVAL) { 1714 fsi->size = vap->va_size; 1715 fsi->valid |= FUSE_FATTR_SIZE; 1716 1717 /* 1718 * Serialize anything that can touch file size 1719 * to avoid reordered GETATTR and SETATTR. 1720 * Out of order SETATTR can report stale size, 1721 * which will cause the kernel to truncate the file. 1722 * XXX Probably useless now we have a lock on GETATTR 1723 */ 1724 while (pnd->pnd_flags & PND_INRESIZE) 1725 requeue_request(pu, opc, PCQ_RESIZE); 1726 pnd->pnd_flags |= PND_INRESIZE; 1727 } 1728 1729 /* 1730 * Setting mtime without atime or vice versa leads to 1731 * dates being reset to Epoch on glusterfs. If one 1732 * is missing, use the old value. 1733 */ 1734 if ((vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) || 1735 (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL)) { 1736 1737 if (vap->va_atime.tv_sec != (time_t)PUFFS_VNOVAL) { 1738 fsi->atime = vap->va_atime.tv_sec; 1739 fsi->atimensec = (uint32_t)vap->va_atime.tv_nsec; 1740 } else { 1741 fsi->atime = old_va->va_atime.tv_sec; 1742 fsi->atimensec = (uint32_t)old_va->va_atime.tv_nsec; 1743 } 1744 1745 if (vap->va_mtime.tv_sec != (time_t)PUFFS_VNOVAL) { 1746 fsi->mtime = vap->va_mtime.tv_sec; 1747 fsi->mtimensec = (uint32_t)vap->va_mtime.tv_nsec; 1748 } else { 1749 fsi->mtime = old_va->va_mtime.tv_sec; 1750 fsi->mtimensec = (uint32_t)old_va->va_mtime.tv_nsec; 1751 } 1752 1753 fsi->valid |= (FUSE_FATTR_MTIME|FUSE_FATTR_ATIME); 1754 } 1755 1756 if (vap->va_mode != (mode_t)PUFFS_VNOVAL) { 1757 fsi->mode = vap->va_mode; 1758 fsi->valid |= FUSE_FATTR_MODE; 1759 } 1760 1761 if (vap->va_uid != (uid_t)PUFFS_VNOVAL) { 1762 fsi->uid = vap->va_uid; 1763 fsi->valid |= FUSE_FATTR_UID; 1764 } 1765 1766 if (vap->va_gid != (gid_t)PUFFS_VNOVAL) { 1767 fsi->gid = vap->va_gid; 1768 fsi->valid |= FUSE_FATTR_GID; 1769 } 1770 1771 if (pnd->pnd_lock_owner != 0) { 1772 fsi->lock_owner = pnd->pnd_lock_owner; 1773 fsi->valid |= FUSE_FATTR_LOCKOWNER; 1774 } 1775 1776 /* 1777 * ftruncate() sends only va_size, and metadata cache 1778 * flush adds va_atime and va_mtime. Some FUSE 1779 * filesystems will attempt to detect ftruncate by 1780 * checking for FATTR_SIZE being set without 1781 * FATTR_UID|FATTR_GID|FATTR_ATIME|FATTR_MTIME|FATTR_MODE 1782 * 1783 * Try to adapt and remove FATTR_ATIME|FATTR_MTIME 1784 * if we suspect a ftruncate(). 1785 */ 1786 if ((vap->va_size != (u_quad_t)PUFFS_VNOVAL) && 1787 ((vap->va_mode == (mode_t)PUFFS_VNOVAL) && 1788 (vap->va_uid == (uid_t)PUFFS_VNOVAL) && 1789 (vap->va_gid == (gid_t)PUFFS_VNOVAL))) { 1790 fsi->atime = 0; 1791 fsi->atimensec = 0; 1792 fsi->mtime = 0; 1793 fsi->mtimensec = 0; 1794 fsi->valid &= ~(FUSE_FATTR_ATIME|FUSE_FATTR_MTIME); 1795 } 1796 1797 /* 1798 * If nothing remain, discard the operation. 1799 */ 1800 if (!(fsi->valid & (FUSE_FATTR_SIZE|FUSE_FATTR_ATIME|FUSE_FATTR_MTIME| 1801 FUSE_FATTR_MODE|FUSE_FATTR_UID|FUSE_FATTR_GID))) { 1802 error = 0; 1803 ps->ps_destroy_msg(pm); 1804 goto out; 1805 } 1806 1807 #ifdef PERFUSE_DEBUG 1808 old_vap = puffs_pn_getvap((struct puffs_node *)opc); 1809 1810 if ((perfuse_diagflags & PDF_RESIZE) && 1811 (old_vap->va_size != (u_quad_t)PUFFS_VNOVAL)) { 1812 resize_debug = 1; 1813 1814 DPRINTF(">> %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__, 1815 (void *)opc, 1816 puffs_pn_getvap((struct puffs_node *)opc)->va_size, 1817 fsi->size); 1818 } 1819 #endif 1820 1821 /* 1822 * Do not honour FAF when changing size. How do 1823 * you want such a thing to work? 1824 */ 1825 reply = wait_reply; 1826 #ifdef PUFFS_SETATTR_FAF 1827 if ((xflag & PUFFS_SETATTR_FAF) && !(fsi->valid & FUSE_FATTR_SIZE)) 1828 reply = no_reply; 1829 #endif 1830 if ((error = xchg_msg(pu, opc, pm, sizeof(*fao), reply)) != 0) 1831 goto out; 1832 1833 if (reply == no_reply) 1834 goto out; 1835 1836 /* 1837 * Copy back the new values 1838 */ 1839 fao = GET_OUTPAYLOAD(ps, pm, fuse_attr_out); 1840 1841 #ifdef PERFUSE_DEBUG 1842 if (resize_debug) 1843 DPRINTF("<< %s %p %" PRIu64 " -> %" PRIu64 "\n", __func__, 1844 (void *)opc, old_vap->va_size, fao->attr.size); 1845 #endif 1846 1847 fuse_attr_to_vap(ps, old_va, &fao->attr); 1848 1849 if (va_ttl != NULL) { 1850 va_ttl->tv_sec = fao->attr_valid; 1851 va_ttl->tv_nsec = fao->attr_valid_nsec; 1852 (void)memcpy(vap, old_va, sizeof(*vap)); 1853 } 1854 1855 ps->ps_destroy_msg(pm); 1856 error = 0; 1857 1858 out: 1859 if (pnd->pnd_flags & PND_INRESIZE) { 1860 pnd->pnd_flags &= ~PND_INRESIZE; 1861 (void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL); 1862 } 1863 1864 out2: 1865 node_rele(opc); 1866 return error; 1867 } 1868 1869 int 1870 perfuse_node_poll(struct puffs_usermount *pu, puffs_cookie_t opc, int *events) 1871 { 1872 struct perfuse_state *ps; 1873 perfuse_msg_t *pm; 1874 struct fuse_poll_in *fpi; 1875 struct fuse_poll_out *fpo; 1876 int error; 1877 1878 node_ref(opc); 1879 ps = puffs_getspecific(pu); 1880 /* 1881 * kh is set if FUSE_POLL_SCHEDULE_NOTIFY is set. 1882 * 1883 * XXX ps_new_msg() is called with NULL creds, which will 1884 * be interpreted as FUSE superuser. We have no way to 1885 * know the requesting process' credential, but since poll 1886 * is supposed to operate on a file that has been open, 1887 * permission should have already been checked at open time. 1888 * That still may breaks on filesystems that provides odd 1889 * semantics. 1890 */ 1891 pm = ps->ps_new_msg(pu, opc, FUSE_POLL, sizeof(*fpi), NULL); 1892 fpi = GET_INPAYLOAD(ps, pm, fuse_poll_in); 1893 fpi->fh = perfuse_get_fh(opc, FREAD); 1894 fpi->kh = 0; 1895 fpi->flags = 0; 1896 1897 #ifdef PERFUSE_DEBUG 1898 if (perfuse_diagflags & PDF_FH) 1899 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", " 1900 "fh = 0x%"PRIx64"\n", __func__, (void *)opc, 1901 PERFUSE_NODE_DATA(opc)->pnd_nodeid, fpi->fh); 1902 #endif 1903 if ((error = xchg_msg(pu, opc, pm, sizeof(*fpo), wait_reply)) != 0) 1904 goto out; 1905 1906 fpo = GET_OUTPAYLOAD(ps, pm, fuse_poll_out); 1907 *events = fpo->revents; 1908 1909 ps->ps_destroy_msg(pm); 1910 error = 0; 1911 1912 out: 1913 node_rele(opc); 1914 return error; 1915 } 1916 1917 /* ARGSUSED0 */ 1918 int 1919 perfuse_node_mmap(struct puffs_usermount *pu, puffs_cookie_t opc, int flags, 1920 const struct puffs_cred *pcr) 1921 { 1922 /* 1923 * Not implemented anymore in libfuse 1924 */ 1925 return ENOSYS; 1926 } 1927 1928 /* ARGSUSED2 */ 1929 int 1930 perfuse_node_fsync(struct puffs_usermount *pu, puffs_cookie_t opc, 1931 const struct puffs_cred *pcr, int flags, off_t offlo, off_t offhi) 1932 { 1933 int op; 1934 perfuse_msg_t *pm; 1935 struct perfuse_state *ps; 1936 struct perfuse_node_data *pnd; 1937 struct fuse_fsync_in *ffi; 1938 uint64_t fh; 1939 int error = 0; 1940 1941 pm = NULL; 1942 ps = puffs_getspecific(pu); 1943 pnd = PERFUSE_NODE_DATA(opc); 1944 1945 /* 1946 * No need to sync a removed node 1947 */ 1948 if (pnd->pnd_flags & PND_REMOVED) 1949 return 0; 1950 1951 /* 1952 * We do not sync closed files. They have been 1953 * sync at inactive time already. 1954 */ 1955 if (!(pnd->pnd_flags & PND_OPEN)) 1956 return 0; 1957 1958 node_ref(opc); 1959 1960 if (puffs_pn_getvap((struct puffs_node *)opc)->va_type == VDIR) 1961 op = FUSE_FSYNCDIR; 1962 else /* VREG but also other types such as VLNK */ 1963 op = FUSE_FSYNC; 1964 1965 /* 1966 * Do not sync if there are no change to sync 1967 * XXX remove that test on files if we implement mmap 1968 */ 1969 #ifdef PERFUSE_DEBUG 1970 if (perfuse_diagflags & PDF_SYNC) 1971 DPRINTF("%s: TEST opc = %p, file = \"%s\" is %sdirty\n", 1972 __func__, (void*)opc, perfuse_node_path(ps, opc), 1973 pnd->pnd_flags & PND_DIRTY ? "" : "not "); 1974 #endif 1975 if (!(pnd->pnd_flags & PND_DIRTY)) 1976 goto out; 1977 1978 /* 1979 * It seems NetBSD can call fsync without open first 1980 * glusterfs complain in such a situation: 1981 * "FSYNC() ERR => -1 (Invalid argument)" 1982 * The file will be closed at inactive time. 1983 * 1984 * We open the directory for reading in order to sync. 1985 * This sounds rather counterintuitive, but it works. 1986 */ 1987 if (!(pnd->pnd_flags & PND_WFH)) { 1988 if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0) 1989 goto out; 1990 } 1991 1992 if (op == FUSE_FSYNCDIR) 1993 fh = perfuse_get_fh(opc, FREAD); 1994 else 1995 fh = perfuse_get_fh(opc, FWRITE); 1996 1997 /* 1998 * If fsync_flags is set, meta data should not be flushed. 1999 */ 2000 pm = ps->ps_new_msg(pu, opc, op, sizeof(*ffi), pcr); 2001 ffi = GET_INPAYLOAD(ps, pm, fuse_fsync_in); 2002 ffi->fh = fh; 2003 ffi->fsync_flags = (flags & FFILESYNC) ? 0 : 1; 2004 2005 #ifdef PERFUSE_DEBUG 2006 if (perfuse_diagflags & PDF_FH) 2007 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n", 2008 __func__, (void *)opc, 2009 PERFUSE_NODE_DATA(opc)->pnd_nodeid, ffi->fh); 2010 #endif 2011 2012 if ((error = xchg_msg(pu, opc, pm, 2013 NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0) 2014 goto out; 2015 2016 /* 2017 * No reply beyond fuse_out_header: nothing to do on success 2018 * just clear the dirty flag 2019 */ 2020 pnd->pnd_flags &= ~PND_DIRTY; 2021 2022 #ifdef PERFUSE_DEBUG 2023 if (perfuse_diagflags & PDF_SYNC) 2024 DPRINTF("%s: CLEAR opc = %p, file = \"%s\"\n", 2025 __func__, (void*)opc, perfuse_node_path(ps, opc)); 2026 #endif 2027 2028 ps->ps_destroy_msg(pm); 2029 error = 0; 2030 2031 out: 2032 /* 2033 * ENOSYS is not returned to kernel, 2034 */ 2035 if (error == ENOSYS) 2036 error = 0; 2037 2038 node_rele(opc); 2039 return error; 2040 } 2041 2042 /* ARGSUSED0 */ 2043 int 2044 perfuse_node_seek(struct puffs_usermount *pu, puffs_cookie_t opc, 2045 off_t oldoff, off_t newoff, const struct puffs_cred *pcr) 2046 { 2047 return 0; 2048 } 2049 2050 int 2051 perfuse_node_remove(struct puffs_usermount *pu, puffs_cookie_t opc, 2052 puffs_cookie_t targ, const struct puffs_cn *pcn) 2053 { 2054 struct perfuse_state *ps; 2055 struct perfuse_node_data *pnd; 2056 perfuse_msg_t *pm; 2057 char *path; 2058 const char *name; 2059 size_t len; 2060 int error; 2061 2062 pnd = PERFUSE_NODE_DATA(opc); 2063 2064 if ((pnd->pnd_flags & PND_REMOVED) || 2065 (PERFUSE_NODE_DATA(targ)->pnd_flags & PND_REMOVED)) 2066 return ENOENT; 2067 2068 #ifdef PERFUSE_DEBUG 2069 if (targ == NULL) 2070 DERRX(EX_SOFTWARE, "%s: targ is NULL", __func__); 2071 2072 if (perfuse_diagflags & (PDF_FH|PDF_FILENAME)) 2073 DPRINTF("%s: opc = %p, remove opc = %p, file = \"%s\"\n", 2074 __func__, (void *)opc, (void *)targ, pcn->pcn_name); 2075 #endif 2076 node_ref(opc); 2077 node_ref(targ); 2078 2079 /* 2080 * Await for all operations on the deleted node to drain, 2081 * as the filesystem may be confused to have it deleted 2082 * during a getattr 2083 */ 2084 while (PERFUSE_NODE_DATA(targ)->pnd_inxchg) 2085 requeue_request(pu, targ, PCQ_AFTERXCHG); 2086 2087 ps = puffs_getspecific(pu); 2088 pnd = PERFUSE_NODE_DATA(opc); 2089 name = pcn->pcn_name; 2090 len = pcn->pcn_namelen + 1; 2091 2092 pm = ps->ps_new_msg(pu, opc, FUSE_UNLINK, len, pcn->pcn_cred); 2093 path = _GET_INPAYLOAD(ps, pm, char *); 2094 (void)strlcpy(path, name, len); 2095 2096 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0) 2097 goto out; 2098 2099 perfuse_cache_flush(targ); 2100 PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED; 2101 2102 if (!(PERFUSE_NODE_DATA(targ)->pnd_flags & PND_OPEN)) 2103 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2); 2104 2105 /* 2106 * The parent directory needs a sync 2107 */ 2108 PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY; 2109 2110 #ifdef PERFUSE_DEBUG 2111 if (perfuse_diagflags & PDF_FILENAME) 2112 DPRINTF("%s: remove nodeid = 0x%"PRIx64" file = \"%s\"\n", 2113 __func__, PERFUSE_NODE_DATA(targ)->pnd_nodeid, 2114 pcn->pcn_name); 2115 #endif 2116 ps->ps_destroy_msg(pm); 2117 error = 0; 2118 2119 out: 2120 node_rele(opc); 2121 node_rele(targ); 2122 return error; 2123 } 2124 2125 int 2126 perfuse_node_link(struct puffs_usermount *pu, puffs_cookie_t opc, 2127 puffs_cookie_t targ, const struct puffs_cn *pcn) 2128 { 2129 struct perfuse_state *ps; 2130 perfuse_msg_t *pm; 2131 const char *name; 2132 size_t len; 2133 struct puffs_node *pn; 2134 struct fuse_link_in *fli; 2135 int error; 2136 2137 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) 2138 return ENOENT; 2139 2140 node_ref(opc); 2141 node_ref(targ); 2142 ps = puffs_getspecific(pu); 2143 pn = (struct puffs_node *)targ; 2144 name = pcn->pcn_name; 2145 len = sizeof(*fli) + pcn->pcn_namelen + 1; 2146 2147 pm = ps->ps_new_msg(pu, opc, FUSE_LINK, len, pcn->pcn_cred); 2148 fli = GET_INPAYLOAD(ps, pm, fuse_link_in); 2149 fli->oldnodeid = PERFUSE_NODE_DATA(pn)->pnd_nodeid; 2150 (void)strlcpy((char *)(void *)(fli + 1), name, len - sizeof(*fli)); 2151 2152 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0) 2153 goto out; 2154 2155 ps->ps_destroy_msg(pm); 2156 error = 0; 2157 2158 out: 2159 node_rele(opc); 2160 node_rele(targ); 2161 return error; 2162 } 2163 2164 int 2165 perfuse_node_rename(struct puffs_usermount *pu, puffs_cookie_t opc, 2166 puffs_cookie_t src, const struct puffs_cn *pcn_src, 2167 puffs_cookie_t targ_dir, puffs_cookie_t targ, 2168 const struct puffs_cn *pcn_targ) 2169 { 2170 struct perfuse_state *ps; 2171 struct perfuse_node_data *dstdir_pnd; 2172 perfuse_msg_t *pm; 2173 struct fuse_rename_in *fri; 2174 const char *newname; 2175 const char *oldname; 2176 char *np; 2177 int error; 2178 size_t len; 2179 size_t newname_len; 2180 size_t oldname_len; 2181 2182 if ((PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) || 2183 (PERFUSE_NODE_DATA(src)->pnd_flags & PND_REMOVED) || 2184 (PERFUSE_NODE_DATA(targ_dir)->pnd_flags & PND_REMOVED)) 2185 return ENOENT; 2186 2187 node_ref(opc); 2188 node_ref(src); 2189 2190 /* 2191 * Await for all operations on the deleted node to drain, 2192 * as the filesystem may be confused to have it deleted 2193 * during a getattr 2194 */ 2195 if ((struct puffs_node *)targ != NULL) { 2196 node_ref(targ); 2197 while (PERFUSE_NODE_DATA(targ)->pnd_inxchg) 2198 requeue_request(pu, targ, PCQ_AFTERXCHG); 2199 } else { 2200 while (PERFUSE_NODE_DATA(src)->pnd_inxchg) 2201 requeue_request(pu, src, PCQ_AFTERXCHG); 2202 } 2203 2204 ps = puffs_getspecific(pu); 2205 newname = pcn_targ->pcn_name; 2206 newname_len = pcn_targ->pcn_namelen + 1; 2207 oldname = pcn_src->pcn_name; 2208 oldname_len = pcn_src->pcn_namelen + 1; 2209 2210 len = sizeof(*fri) + oldname_len + newname_len; 2211 pm = ps->ps_new_msg(pu, opc, FUSE_RENAME, len, pcn_targ->pcn_cred); 2212 fri = GET_INPAYLOAD(ps, pm, fuse_rename_in); 2213 fri->newdir = PERFUSE_NODE_DATA(targ_dir)->pnd_nodeid; 2214 np = (char *)(void *)(fri + 1); 2215 (void)strlcpy(np, oldname, oldname_len); 2216 np += oldname_len; 2217 (void)strlcpy(np, newname, newname_len); 2218 2219 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0) 2220 goto out; 2221 2222 2223 /* 2224 * Record new parent nodeid 2225 */ 2226 dstdir_pnd = PERFUSE_NODE_DATA(targ_dir); 2227 PERFUSE_NODE_DATA(src)->pnd_parent_nodeid = dstdir_pnd->pnd_nodeid; 2228 2229 if (opc != targ_dir) 2230 dstdir_pnd->pnd_flags |= PND_DIRTY; 2231 2232 if (strcmp(newname, "..") != 0) 2233 (void)strlcpy(PERFUSE_NODE_DATA(src)->pnd_name, 2234 newname, MAXPATHLEN); 2235 else 2236 PERFUSE_NODE_DATA(src)->pnd_name[0] = 0; /* forget name */ 2237 2238 PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY; 2239 2240 if ((struct puffs_node *)targ != NULL) { 2241 perfuse_cache_flush(targ); 2242 PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED; 2243 } 2244 2245 #ifdef PERFUSE_DEBUG 2246 if (perfuse_diagflags & PDF_FILENAME) 2247 DPRINTF("%s: nodeid = 0x%"PRIx64" file = \"%s\" renamed \"%s\" " 2248 "nodeid = 0x%"PRIx64" -> nodeid = 0x%"PRIx64" \"%s\"\n", 2249 __func__, PERFUSE_NODE_DATA(src)->pnd_nodeid, 2250 pcn_src->pcn_name, pcn_targ->pcn_name, 2251 PERFUSE_NODE_DATA(opc)->pnd_nodeid, 2252 PERFUSE_NODE_DATA(targ_dir)->pnd_nodeid, 2253 perfuse_node_path(ps, targ_dir)); 2254 #endif 2255 2256 ps->ps_destroy_msg(pm); 2257 error = 0; 2258 2259 out: 2260 node_rele(opc); 2261 node_rele(src); 2262 if ((struct puffs_node *)targ != NULL) 2263 node_rele(targ); 2264 2265 return error; 2266 } 2267 2268 int 2269 perfuse_node_mkdir(struct puffs_usermount *pu, puffs_cookie_t opc, 2270 struct puffs_newinfo *pni, const struct puffs_cn *pcn, 2271 const struct vattr *vap) 2272 { 2273 struct perfuse_state *ps; 2274 perfuse_msg_t *pm; 2275 struct fuse_mkdir_in *fmi; 2276 const char *path; 2277 size_t len; 2278 int error; 2279 2280 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) 2281 return ENOENT; 2282 2283 node_ref(opc); 2284 ps = puffs_getspecific(pu); 2285 path = pcn->pcn_name; 2286 len = sizeof(*fmi) + pcn->pcn_namelen + 1; 2287 2288 pm = ps->ps_new_msg(pu, opc, FUSE_MKDIR, len, pcn->pcn_cred); 2289 fmi = GET_INPAYLOAD(ps, pm, fuse_mkdir_in); 2290 fmi->mode = vap->va_mode; 2291 fmi->umask = 0; /* Seems unused by libfuse? */ 2292 (void)strlcpy((char *)(void *)(fmi + 1), path, len - sizeof(*fmi)); 2293 2294 error = node_mk_common(pu, opc, pni, pcn, pm); 2295 2296 node_rele(opc); 2297 return error; 2298 } 2299 2300 2301 int 2302 perfuse_node_rmdir(struct puffs_usermount *pu, puffs_cookie_t opc, 2303 puffs_cookie_t targ, const struct puffs_cn *pcn) 2304 { 2305 struct perfuse_state *ps; 2306 struct perfuse_node_data *pnd; 2307 perfuse_msg_t *pm; 2308 char *path; 2309 const char *name; 2310 size_t len; 2311 int error; 2312 2313 pnd = PERFUSE_NODE_DATA(opc); 2314 2315 if ((pnd->pnd_flags & PND_REMOVED) || 2316 (PERFUSE_NODE_DATA(targ)->pnd_flags & PND_REMOVED)) 2317 return ENOENT; 2318 2319 node_ref(opc); 2320 node_ref(targ); 2321 2322 /* 2323 * Await for all operations on the deleted node to drain, 2324 * as the filesystem may be confused to have it deleted 2325 * during a getattr 2326 */ 2327 while (PERFUSE_NODE_DATA(targ)->pnd_inxchg) 2328 requeue_request(pu, targ, PCQ_AFTERXCHG); 2329 2330 ps = puffs_getspecific(pu); 2331 name = pcn->pcn_name; 2332 len = pcn->pcn_namelen + 1; 2333 2334 pm = ps->ps_new_msg(pu, opc, FUSE_RMDIR, len, pcn->pcn_cred); 2335 path = _GET_INPAYLOAD(ps, pm, char *); 2336 (void)strlcpy(path, name, len); 2337 2338 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0) 2339 goto out; 2340 2341 perfuse_cache_flush(targ); 2342 PERFUSE_NODE_DATA(targ)->pnd_flags |= PND_REMOVED; 2343 2344 if (!(PERFUSE_NODE_DATA(targ)->pnd_flags & PND_OPEN)) 2345 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N2); 2346 2347 /* 2348 * The parent directory needs a sync 2349 */ 2350 PERFUSE_NODE_DATA(opc)->pnd_flags |= PND_DIRTY; 2351 2352 #ifdef PERFUSE_DEBUG 2353 if (perfuse_diagflags & PDF_FILENAME) 2354 DPRINTF("%s: remove nodeid = 0x%"PRIx64" file = \"%s\"\n", 2355 __func__, PERFUSE_NODE_DATA(targ)->pnd_nodeid, 2356 perfuse_node_path(ps, targ)); 2357 #endif 2358 ps->ps_destroy_msg(pm); 2359 error = 0; 2360 2361 out: 2362 node_rele(opc); 2363 node_rele(targ); 2364 return error; 2365 } 2366 2367 /* vap is unused */ 2368 /* ARGSUSED4 */ 2369 int 2370 perfuse_node_symlink(struct puffs_usermount *pu, puffs_cookie_t opc, 2371 struct puffs_newinfo *pni, const struct puffs_cn *pcn_src, 2372 const struct vattr *vap, const char *link_target) 2373 { 2374 struct perfuse_state *ps; 2375 perfuse_msg_t *pm; 2376 char *np; 2377 const char *path; 2378 size_t path_len; 2379 size_t linkname_len; 2380 size_t len; 2381 int error; 2382 2383 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) 2384 return ENOENT; 2385 2386 node_ref(opc); 2387 ps = puffs_getspecific(pu); 2388 path = pcn_src->pcn_name; 2389 path_len = pcn_src->pcn_namelen + 1; 2390 linkname_len = strlen(link_target) + 1; 2391 len = path_len + linkname_len; 2392 2393 pm = ps->ps_new_msg(pu, opc, FUSE_SYMLINK, len, pcn_src->pcn_cred); 2394 np = _GET_INPAYLOAD(ps, pm, char *); 2395 (void)strlcpy(np, path, path_len); 2396 np += path_len; 2397 (void)strlcpy(np, link_target, linkname_len); 2398 2399 error = node_mk_common(pu, opc, pni, pcn_src, pm); 2400 2401 node_rele(opc); 2402 return error; 2403 } 2404 2405 /* ARGSUSED4 */ 2406 int 2407 perfuse_node_readdir(struct puffs_usermount *pu, puffs_cookie_t opc, 2408 struct dirent *dent, off_t *readoff, size_t *reslen, 2409 const struct puffs_cred *pcr, int *eofflag, off_t *cookies, 2410 size_t *ncookies) 2411 { 2412 perfuse_msg_t *pm; 2413 uint64_t fh; 2414 struct perfuse_state *ps; 2415 struct perfuse_node_data *pnd; 2416 struct fuse_read_in *fri; 2417 struct fuse_out_header *foh; 2418 struct fuse_dirent *fd; 2419 size_t foh_len; 2420 int error; 2421 size_t fd_maxlen; 2422 2423 error = 0; 2424 node_ref(opc); 2425 ps = puffs_getspecific(pu); 2426 2427 /* 2428 * readdir state is kept at node level, and several readdir 2429 * requests can be issued at the same time on the same node. 2430 * We need to queue requests so that only one is in readdir 2431 * code at the same time. 2432 */ 2433 pnd = PERFUSE_NODE_DATA(opc); 2434 while (pnd->pnd_flags & PND_INREADDIR) 2435 requeue_request(pu, opc, PCQ_READDIR); 2436 pnd->pnd_flags |= PND_INREADDIR; 2437 2438 #ifdef PERFUSE_DEBUG 2439 if (perfuse_diagflags & PDF_READDIR) 2440 DPRINTF("%s: READDIR opc = %p enter critical section\n", 2441 __func__, (void *)opc); 2442 #endif 2443 /* 2444 * Re-initialize pnd->pnd_fd_cookie on the first readdir for a node 2445 */ 2446 if (*readoff == 0) 2447 pnd->pnd_fd_cookie = 0; 2448 2449 /* 2450 * Do we already have the data bufered? 2451 */ 2452 if (pnd->pnd_dirent != NULL) 2453 goto out; 2454 pnd->pnd_dirent_len = 0; 2455 2456 /* 2457 * It seems NetBSD can call readdir without open first 2458 * libfuse will crash if it is done that way, hence open first. 2459 */ 2460 if (!(pnd->pnd_flags & PND_OPEN)) { 2461 if ((error = perfuse_node_open(pu, opc, FREAD, pcr)) != 0) 2462 goto out; 2463 } 2464 2465 fh = perfuse_get_fh(opc, FREAD); 2466 2467 #ifdef PERFUSE_DEBUG 2468 if (perfuse_diagflags & PDF_FH) 2469 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", " 2470 "rfh = 0x%"PRIx64"\n", __func__, (void *)opc, 2471 PERFUSE_NODE_DATA(opc)->pnd_nodeid, fh); 2472 #endif 2473 2474 pnd->pnd_all_fd = NULL; 2475 pnd->pnd_all_fd_len = 0; 2476 fd_maxlen = ps->ps_max_readahead - sizeof(*foh); 2477 2478 do { 2479 size_t fd_len; 2480 char *afdp; 2481 2482 pm = ps->ps_new_msg(pu, opc, FUSE_READDIR, sizeof(*fri), pcr); 2483 2484 /* 2485 * read_flags, lock_owner and flags are unused in libfuse 2486 */ 2487 fri = GET_INPAYLOAD(ps, pm, fuse_read_in); 2488 fri->fh = fh; 2489 fri->offset = pnd->pnd_fd_cookie; 2490 fri->size = (uint32_t)fd_maxlen; 2491 fri->read_flags = 0; 2492 fri->lock_owner = 0; 2493 fri->flags = 0; 2494 2495 if ((error = xchg_msg(pu, opc, pm, 2496 UNSPEC_REPLY_LEN, wait_reply)) != 0) 2497 goto out; 2498 2499 /* 2500 * There are many puffs_framebufs calls later, 2501 * therefore foh will not be valid for a long time. 2502 * Just get the length and forget it. 2503 */ 2504 foh = GET_OUTHDR(ps, pm); 2505 foh_len = foh->len; 2506 2507 /* 2508 * Empty read: we reached the end of the buffer. 2509 */ 2510 if (foh_len == sizeof(*foh)) { 2511 ps->ps_destroy_msg(pm); 2512 *eofflag = 1; 2513 break; 2514 } 2515 2516 /* 2517 * Check for corrupted message. 2518 */ 2519 if (foh_len < sizeof(*foh) + sizeof(*fd)) { 2520 ps->ps_destroy_msg(pm); 2521 DWARNX("readdir reply too short"); 2522 error = EIO; 2523 goto out; 2524 } 2525 2526 2527 fd = GET_OUTPAYLOAD(ps, pm, fuse_dirent); 2528 fd_len = foh_len - sizeof(*foh); 2529 2530 pnd->pnd_all_fd = realloc(pnd->pnd_all_fd, 2531 pnd->pnd_all_fd_len + fd_len); 2532 if (pnd->pnd_all_fd == NULL) 2533 DERR(EX_OSERR, "%s: malloc failed", __func__); 2534 2535 afdp = (char *)(void *)pnd->pnd_all_fd + pnd->pnd_all_fd_len; 2536 (void)memcpy(afdp, fd, fd_len); 2537 2538 pnd->pnd_all_fd_len += fd_len; 2539 2540 /* 2541 * The fd->off field is used as a cookie for 2542 * resuming the next readdir() where this one was left. 2543 */ 2544 pnd->pnd_fd_cookie = readdir_last_cookie(fd, fd_len); 2545 2546 ps->ps_destroy_msg(pm); 2547 } while (1 /* CONSTCOND */); 2548 2549 if (pnd->pnd_all_fd != NULL) { 2550 if (fuse_to_dirent(pu, opc, pnd->pnd_all_fd, 2551 pnd->pnd_all_fd_len) == -1) 2552 error = EIO; 2553 } 2554 2555 out: 2556 if (pnd->pnd_all_fd != NULL) { 2557 free(pnd->pnd_all_fd); 2558 pnd->pnd_all_fd = NULL; 2559 pnd->pnd_all_fd_len = 0; 2560 } 2561 2562 if (error == 0) 2563 readdir_buffered(opc, dent, readoff, reslen); 2564 2565 /* 2566 * Schedule queued readdir requests 2567 */ 2568 pnd->pnd_flags &= ~PND_INREADDIR; 2569 (void)dequeue_requests(opc, PCQ_READDIR, DEQUEUE_ALL); 2570 2571 #ifdef PERFUSE_DEBUG 2572 if (perfuse_diagflags & PDF_READDIR) 2573 DPRINTF("%s: READDIR opc = %p exit critical section\n", 2574 __func__, (void *)opc); 2575 #endif 2576 2577 node_rele(opc); 2578 return error; 2579 } 2580 2581 int 2582 perfuse_node_readlink(struct puffs_usermount *pu, puffs_cookie_t opc, 2583 const struct puffs_cred *pcr, char *linkname, size_t *linklen) 2584 { 2585 struct perfuse_state *ps; 2586 perfuse_msg_t *pm; 2587 int error; 2588 size_t len; 2589 struct fuse_out_header *foh; 2590 2591 if (PERFUSE_NODE_DATA(opc)->pnd_flags & PND_REMOVED) 2592 return ENOENT; 2593 2594 node_ref(opc); 2595 ps = puffs_getspecific(pu); 2596 2597 pm = ps->ps_new_msg(pu, opc, FUSE_READLINK, 0, pcr); 2598 2599 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0) 2600 goto out; 2601 2602 foh = GET_OUTHDR(ps, pm); 2603 len = foh->len - sizeof(*foh); 2604 if (len > *linklen) 2605 DERRX(EX_PROTOCOL, "path len = %zd too long", len); 2606 if (len == 0) 2607 DERRX(EX_PROTOCOL, "path len = %zd too short", len); 2608 2609 /* 2610 * FUSE filesystems return a NUL terminated string, we 2611 * do not want to trailing \0 2612 */ 2613 *linklen = len - 1; 2614 (void)memcpy(linkname, _GET_OUTPAYLOAD(ps, pm, char *), len); 2615 2616 ps->ps_destroy_msg(pm); 2617 error = 0; 2618 2619 out: 2620 node_rele(opc); 2621 return error; 2622 } 2623 2624 int 2625 perfuse_node_reclaim(struct puffs_usermount *pu, puffs_cookie_t opc) 2626 { 2627 struct perfuse_state *ps; 2628 perfuse_msg_t *pm; 2629 struct perfuse_node_data *pnd; 2630 struct fuse_forget_in *ffi; 2631 int nlookup; 2632 struct timespec now; 2633 2634 if (opc == 0) 2635 return 0; 2636 2637 ps = puffs_getspecific(pu); 2638 pnd = PERFUSE_NODE_DATA(opc); 2639 2640 /* 2641 * Never forget the root. 2642 */ 2643 if (pnd->pnd_nodeid == FUSE_ROOT_ID) 2644 return 0; 2645 2646 /* 2647 * There is a race condition between reclaim and lookup. 2648 * When looking up an already known node, the kernel cannot 2649 * hold a reference on the result until it gets the PUFFS 2650 * reply. It mayy therefore reclaim the node after the 2651 * userland looked it up, and before it gets the reply. 2652 * On rely, the kernel re-creates the node, but at that 2653 * time the node has been reclaimed in userland. 2654 * 2655 * In order to avoid this, we refuse reclaiming nodes that 2656 * are too young since the last lookup - and that we do 2657 * not have removed on our own, of course. 2658 */ 2659 if (clock_gettime(CLOCK_REALTIME, &now) != 0) 2660 DERR(EX_OSERR, "clock_gettime failed"); 2661 2662 if (timespeccmp(&pnd->pnd_cn_expire, &now, >) && 2663 !(pnd->pnd_flags & PND_REMOVED)) { 2664 if (!(pnd->pnd_flags & PND_NODELEAK)) { 2665 ps->ps_nodeleakcount++; 2666 pnd->pnd_flags |= PND_NODELEAK; 2667 } 2668 DWARNX("possible leaked node:: opc = %p \"%s\"", 2669 opc, pnd->pnd_name); 2670 return 0; 2671 } 2672 2673 node_ref(opc); 2674 pnd->pnd_flags |= PND_RECLAIMED; 2675 pnd->pnd_puffs_nlookup--; 2676 nlookup = pnd->pnd_puffs_nlookup; 2677 2678 #ifdef PERFUSE_DEBUG 2679 if (perfuse_diagflags & PDF_RECLAIM) 2680 DPRINTF("%s (nodeid %"PRId64") reclaimed\n", 2681 perfuse_node_path(ps, opc), pnd->pnd_nodeid); 2682 #endif 2683 2684 #ifdef PERFUSE_DEBUG 2685 if (perfuse_diagflags & PDF_RECLAIM) 2686 DPRINTF("%s (nodeid %"PRId64") is %sreclaimed, nlookup = %d " 2687 "%s%s%s%s, pending ops:%s%s%s\n", 2688 perfuse_node_path(ps, opc), pnd->pnd_nodeid, 2689 pnd->pnd_flags & PND_RECLAIMED ? "" : "not ", 2690 pnd->pnd_puffs_nlookup, 2691 pnd->pnd_flags & PND_OPEN ? "open " : "not open", 2692 pnd->pnd_flags & PND_RFH ? "r" : "", 2693 pnd->pnd_flags & PND_WFH ? "w" : "", 2694 pnd->pnd_flags & PND_BUSY ? "" : " none", 2695 pnd->pnd_flags & PND_INREADDIR ? " readdir" : "", 2696 pnd->pnd_flags & PND_INWRITE ? " write" : "", 2697 pnd->pnd_flags & PND_INOPEN ? " open" : ""); 2698 #endif 2699 /* 2700 * Make sure it is not looked up again 2701 */ 2702 if (!(pnd->pnd_flags & PND_REMOVED)) 2703 perfuse_cache_flush(opc); 2704 2705 /* 2706 * Purge any activity on the node, while checking 2707 * that it remains eligible for a reclaim. 2708 */ 2709 while (pnd->pnd_ref > 1) 2710 requeue_request(pu, opc, PCQ_REF); 2711 2712 /* 2713 * reclaim cancel? 2714 */ 2715 if (pnd->pnd_puffs_nlookup > nlookup) { 2716 pnd->pnd_flags &= ~PND_RECLAIMED; 2717 perfuse_node_cache(ps, opc); 2718 node_rele(opc); 2719 return 0; 2720 } 2721 2722 2723 #ifdef PERFUSE_DEBUG 2724 if ((pnd->pnd_flags & PND_OPEN) || 2725 !TAILQ_EMPTY(&pnd->pnd_pcq)) 2726 DERRX(EX_SOFTWARE, "%s: opc = %p: still open", 2727 __func__, opc); 2728 2729 if ((pnd->pnd_flags & PND_BUSY) || 2730 !TAILQ_EMPTY(&pnd->pnd_pcq)) 2731 DERRX(EX_SOFTWARE, "%s: opc = %p: queued operations", 2732 __func__, opc); 2733 2734 if (pnd->pnd_inxchg != 0) 2735 DERRX(EX_SOFTWARE, "%s: opc = %p: ongoing operations", 2736 __func__, opc); 2737 #endif 2738 2739 /* 2740 * Send the FORGET message 2741 * 2742 * ps_new_msg() is called with NULL creds, which will 2743 * be interpreted as FUSE superuser. This is obviously 2744 * fine since we operate with kernel creds here. 2745 */ 2746 pm = ps->ps_new_msg(pu, opc, FUSE_FORGET, 2747 sizeof(*ffi), NULL); 2748 ffi = GET_INPAYLOAD(ps, pm, fuse_forget_in); 2749 ffi->nlookup = pnd->pnd_fuse_nlookup; 2750 2751 /* 2752 * No reply is expected, pm is freed in xchg_msg 2753 */ 2754 (void)xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, no_reply); 2755 2756 perfuse_destroy_pn(pu, opc); 2757 2758 return 0; 2759 } 2760 2761 int 2762 perfuse_node_inactive(struct puffs_usermount *pu, puffs_cookie_t opc) 2763 { 2764 struct perfuse_node_data *pnd; 2765 int error; 2766 2767 if (opc == 0) 2768 return 0; 2769 2770 node_ref(opc); 2771 pnd = PERFUSE_NODE_DATA(opc); 2772 2773 if (!(pnd->pnd_flags & (PND_OPEN|PND_REMOVED))) 2774 goto out; 2775 2776 /* 2777 * Make sure all operation are finished 2778 * There can be an ongoing write. Other 2779 * operation wait for all data before 2780 * the close/inactive. 2781 */ 2782 while (pnd->pnd_flags & PND_INWRITE) 2783 requeue_request(pu, opc, PCQ_AFTERWRITE); 2784 2785 /* 2786 * The inactive operation may be cancelled, 2787 * If no open is in progress, set PND_INOPEN 2788 * so that a new open will be queued. 2789 */ 2790 if (pnd->pnd_flags & PND_INOPEN) 2791 goto out; 2792 2793 pnd->pnd_flags |= PND_INOPEN; 2794 2795 /* 2796 * Sync data 2797 */ 2798 if (pnd->pnd_flags & PND_DIRTY) { 2799 if ((error = perfuse_node_fsync(pu, opc, NULL, 0, 0, 0)) != 0) 2800 DWARN("%s: perfuse_node_fsync failed error = %d", 2801 __func__, error); 2802 } 2803 2804 2805 /* 2806 * Close handles 2807 */ 2808 if (pnd->pnd_flags & PND_WFH) { 2809 if ((error = perfuse_node_close_common(pu, opc, FWRITE)) != 0) 2810 DWARN("%s: close write FH failed error = %d", 2811 __func__, error); 2812 } 2813 2814 if (pnd->pnd_flags & PND_RFH) { 2815 if ((error = perfuse_node_close_common(pu, opc, FREAD)) != 0) 2816 DWARN("%s: close read FH failed error = %d", 2817 __func__, error); 2818 } 2819 2820 /* 2821 * This will cause a reclaim to be sent 2822 */ 2823 if (pnd->pnd_flags & PND_REMOVED) 2824 puffs_setback(puffs_cc_getcc(pu), PUFFS_SETBACK_NOREF_N1); 2825 2826 /* 2827 * Schedule awaiting operations 2828 */ 2829 pnd->pnd_flags &= ~PND_INOPEN; 2830 (void)dequeue_requests(opc, PCQ_OPEN, DEQUEUE_ALL); 2831 2832 /* 2833 * errors are ignored, since the kernel ignores the return code. 2834 */ 2835 out: 2836 node_rele(opc); 2837 return 0; 2838 } 2839 2840 2841 /* ARGSUSED0 */ 2842 int 2843 perfuse_node_print(struct puffs_usermount *pu, puffs_cookie_t opc) 2844 { 2845 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__); 2846 return 0; 2847 } 2848 2849 /* ARGSUSED0 */ 2850 int 2851 perfuse_node_pathconf(struct puffs_usermount *pu, puffs_cookie_t opc, 2852 int name, int *retval) 2853 { 2854 DERRX(EX_SOFTWARE, "%s: UNIMPLEMENTED (FATAL)", __func__); 2855 return 0; 2856 } 2857 2858 int 2859 perfuse_node_advlock(struct puffs_usermount *pu, puffs_cookie_t opc, 2860 void *id, int op, struct flock *fl, int flags) 2861 { 2862 struct perfuse_state *ps; 2863 int fop; 2864 perfuse_msg_t *pm; 2865 uint64_t fh; 2866 struct fuse_lk_in *fli; 2867 struct fuse_out_header *foh; 2868 struct fuse_lk_out *flo; 2869 uint32_t owner; 2870 size_t len; 2871 int error; 2872 2873 node_ref(opc); 2874 2875 /* 2876 * Make sure we do have a filehandle, as the FUSE filesystem 2877 * expect one. E.g.: if we provide none, GlusterFS logs an error 2878 * "0-glusterfs-fuse: xl is NULL" 2879 * 2880 * We need the read file handle if the file is open read only, 2881 * in order to support shared locks on read-only files. 2882 * NB: The kernel always sends advlock for read-only 2883 * files at exit time when the process used lock, see 2884 * sys_exit -> exit1 -> fd_free -> fd_close -> VOP_ADVLOCK 2885 */ 2886 if ((fh = perfuse_get_fh(opc, FREAD)) == FUSE_UNKNOWN_FH) { 2887 error = EBADF; 2888 goto out; 2889 } 2890 2891 ps = puffs_getspecific(pu); 2892 2893 if (op == F_GETLK) 2894 fop = FUSE_GETLK; 2895 else 2896 fop = (flags & F_WAIT) ? FUSE_SETLKW : FUSE_SETLK; 2897 2898 /* 2899 * XXX ps_new_msg() is called with NULL creds, which will 2900 * be interpreted as FUSE superuser. We have no way to 2901 * know the requesting process' credential, but since advlock() 2902 * is supposed to operate on a file that has been open(), 2903 * permission should have already been checked at open() time. 2904 */ 2905 pm = ps->ps_new_msg(pu, opc, fop, sizeof(*fli), NULL); 2906 fli = GET_INPAYLOAD(ps, pm, fuse_lk_in); 2907 fli->fh = fh; 2908 fli->owner = (uint64_t)(vaddr_t)id; 2909 fli->lk.start = fl->l_start; 2910 fli->lk.end = fl->l_start + fl->l_len; 2911 fli->lk.type = fl->l_type; 2912 fli->lk.pid = fl->l_pid; 2913 fli->lk_flags = (flags & F_FLOCK) ? FUSE_LK_FLOCK : 0; 2914 2915 owner = (uint32_t)(vaddr_t)id; 2916 2917 #ifdef PERFUSE_DEBUG 2918 if (perfuse_diagflags & PDF_FH) 2919 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n", 2920 __func__, (void *)opc, 2921 PERFUSE_NODE_DATA(opc)->pnd_nodeid, fli->fh); 2922 #endif 2923 2924 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0) 2925 goto out; 2926 2927 foh = GET_OUTHDR(ps, pm); 2928 len = foh->len - sizeof(*foh); 2929 2930 /* 2931 * Save or clear the lock 2932 */ 2933 switch (op) { 2934 case F_GETLK: 2935 if (len != sizeof(*flo)) 2936 DERRX(EX_SOFTWARE, 2937 "%s: Unexpected lock reply len %zd", 2938 __func__, len); 2939 2940 flo = GET_OUTPAYLOAD(ps, pm, fuse_lk_out); 2941 fl->l_start = flo->lk.start; 2942 fl->l_len = flo->lk.end - flo->lk.start; 2943 fl->l_pid = flo->lk.pid; 2944 fl->l_type = flo->lk.type; 2945 fl->l_whence = SEEK_SET; /* libfuse hardcodes it */ 2946 2947 PERFUSE_NODE_DATA(opc)->pnd_lock_owner = flo->lk.pid; 2948 break; 2949 case F_UNLCK: 2950 owner = 0; 2951 /* FALLTHROUGH */ 2952 case F_SETLK: 2953 /* FALLTHROUGH */ 2954 case F_SETLKW: 2955 if (error != 0) 2956 PERFUSE_NODE_DATA(opc)->pnd_lock_owner = owner; 2957 2958 if (len != 0) 2959 DERRX(EX_SOFTWARE, 2960 "%s: Unexpected unlock reply len %zd", 2961 __func__, len); 2962 2963 break; 2964 default: 2965 DERRX(EX_SOFTWARE, "%s: Unexpected op %d", __func__, op); 2966 break; 2967 } 2968 2969 ps->ps_destroy_msg(pm); 2970 error = 0; 2971 2972 out: 2973 node_rele(opc); 2974 return error; 2975 } 2976 2977 int 2978 perfuse_node_read(struct puffs_usermount *pu, puffs_cookie_t opc, uint8_t *buf, 2979 off_t offset, size_t *resid, const struct puffs_cred *pcr, int ioflag) 2980 { 2981 struct perfuse_state *ps; 2982 struct perfuse_node_data *pnd; 2983 const struct vattr *vap; 2984 perfuse_msg_t *pm; 2985 struct fuse_read_in *fri; 2986 struct fuse_out_header *foh; 2987 size_t readen; 2988 int error; 2989 2990 ps = puffs_getspecific(pu); 2991 pnd = PERFUSE_NODE_DATA(opc); 2992 vap = puffs_pn_getvap((struct puffs_node *)opc); 2993 2994 /* 2995 * NetBSD turns that into a getdents(2) output 2996 * We just do a EISDIR as this feature is of little use. 2997 */ 2998 if (vap->va_type == VDIR) 2999 return EISDIR; 3000 3001 if ((u_quad_t)offset + *resid > vap->va_size) 3002 DWARNX("%s %p read %lld@%zu beyond EOF %" PRIu64 "\n", 3003 __func__, (void *)opc, (long long)offset, 3004 *resid, vap->va_size); 3005 3006 do { 3007 size_t max_read; 3008 3009 max_read = ps->ps_max_readahead - sizeof(*foh); 3010 /* 3011 * flags may be set to FUSE_READ_LOCKOWNER 3012 * if lock_owner is provided. 3013 */ 3014 pm = ps->ps_new_msg(pu, opc, FUSE_READ, sizeof(*fri), pcr); 3015 fri = GET_INPAYLOAD(ps, pm, fuse_read_in); 3016 fri->fh = perfuse_get_fh(opc, FREAD); 3017 fri->offset = offset; 3018 fri->size = (uint32_t)MIN(*resid, max_read); 3019 fri->read_flags = 0; /* XXX Unused by libfuse? */ 3020 fri->lock_owner = pnd->pnd_lock_owner; 3021 fri->flags = 0; 3022 fri->flags |= (fri->lock_owner != 0) ? FUSE_READ_LOCKOWNER : 0; 3023 3024 #ifdef PERFUSE_DEBUG 3025 if (perfuse_diagflags & PDF_FH) 3026 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", fh = 0x%"PRIx64"\n", 3027 __func__, (void *)opc, pnd->pnd_nodeid, fri->fh); 3028 #endif 3029 error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply); 3030 if (error != 0) 3031 return error; 3032 3033 foh = GET_OUTHDR(ps, pm); 3034 readen = foh->len - sizeof(*foh); 3035 3036 #ifdef PERFUSE_DEBUG 3037 if (readen > *resid) 3038 DERRX(EX_SOFTWARE, "%s: Unexpected big read %zd", 3039 __func__, readen); 3040 #endif 3041 3042 (void)memcpy(buf, _GET_OUTPAYLOAD(ps, pm, char *), readen); 3043 3044 buf += readen; 3045 offset += readen; 3046 *resid -= readen; 3047 3048 ps->ps_destroy_msg(pm); 3049 } while ((*resid != 0) && (readen != 0)); 3050 3051 if (ioflag & (IO_SYNC|IO_DSYNC)) 3052 ps->ps_syncreads++; 3053 else 3054 ps->ps_asyncreads++; 3055 3056 return 0; 3057 } 3058 3059 int 3060 perfuse_node_write(struct puffs_usermount *pu, puffs_cookie_t opc, 3061 uint8_t *buf, off_t offset, size_t *resid, 3062 const struct puffs_cred *pcr, int ioflag) 3063 { 3064 return perfuse_node_write2(pu, opc, buf, offset, resid, pcr, ioflag, 0); 3065 } 3066 3067 /* ARGSUSED7 */ 3068 int 3069 perfuse_node_write2(struct puffs_usermount *pu, puffs_cookie_t opc, 3070 uint8_t *buf, off_t offset, size_t *resid, 3071 const struct puffs_cred *pcr, int ioflag, int xflag) 3072 { 3073 struct perfuse_state *ps; 3074 struct perfuse_node_data *pnd; 3075 struct vattr *vap; 3076 perfuse_msg_t *pm; 3077 struct fuse_write_in *fwi; 3078 struct fuse_write_out *fwo; 3079 size_t data_len; 3080 size_t payload_len; 3081 size_t written; 3082 int inresize; 3083 int error; 3084 3085 ps = puffs_getspecific(pu); 3086 pnd = PERFUSE_NODE_DATA(opc); 3087 vap = puffs_pn_getvap((struct puffs_node *)opc); 3088 written = 0; 3089 inresize = 0; 3090 error = 0; 3091 3092 if (vap->va_type == VDIR) 3093 return EISDIR; 3094 3095 node_ref(opc); 3096 3097 /* 3098 * We need to queue write requests in order to avoid 3099 * dequeueing PCQ_AFTERWRITE when there are pending writes. 3100 */ 3101 while (pnd->pnd_flags & PND_INWRITE) 3102 requeue_request(pu, opc, PCQ_WRITE); 3103 pnd->pnd_flags |= PND_INWRITE; 3104 3105 /* 3106 * Serialize size access, see comment in perfuse_node_setattr(). 3107 */ 3108 if ((u_quad_t)offset + *resid > vap->va_size) { 3109 while (pnd->pnd_flags & PND_INRESIZE) 3110 requeue_request(pu, opc, PCQ_RESIZE); 3111 pnd->pnd_flags |= PND_INRESIZE; 3112 inresize = 1; 3113 } 3114 3115 /* 3116 * append flag: re-read the file size so that 3117 * we get the latest value. 3118 */ 3119 if (ioflag & PUFFS_IO_APPEND) { 3120 DWARNX("%s: PUFFS_IO_APPEND set, untested code", __func__); 3121 3122 if ((error = perfuse_node_getattr(pu, opc, vap, pcr)) != 0) 3123 goto out; 3124 3125 offset = vap->va_size; 3126 } 3127 3128 #ifdef PERFUSE_DEBUG 3129 if (perfuse_diagflags & PDF_RESIZE) 3130 DPRINTF(">> %s %p %" PRIu64 "\n", __func__, 3131 (void *)opc, vap->va_size); 3132 #endif 3133 3134 do { 3135 size_t max_write; 3136 /* 3137 * There is a writepage flag when data 3138 * is aligned to page size. Use it for 3139 * everything but the data after the last 3140 * page boundary. 3141 */ 3142 max_write = ps->ps_max_write - sizeof(*fwi); 3143 3144 data_len = MIN(*resid, max_write); 3145 if (data_len > (size_t)sysconf(_SC_PAGESIZE)) 3146 data_len = data_len & ~(sysconf(_SC_PAGESIZE) - 1); 3147 3148 payload_len = data_len + sizeof(*fwi); 3149 3150 /* 3151 * flags may be set to FUSE_WRITE_CACHE (XXX usage?) 3152 * or FUSE_WRITE_LOCKOWNER, if lock_owner is provided. 3153 * write_flags is set to 1 for writepage. 3154 */ 3155 pm = ps->ps_new_msg(pu, opc, FUSE_WRITE, payload_len, pcr); 3156 fwi = GET_INPAYLOAD(ps, pm, fuse_write_in); 3157 fwi->fh = perfuse_get_fh(opc, FWRITE); 3158 fwi->offset = offset; 3159 fwi->size = (uint32_t)data_len; 3160 fwi->write_flags = (fwi->size % sysconf(_SC_PAGESIZE)) ? 0 : 1; 3161 fwi->lock_owner = pnd->pnd_lock_owner; 3162 fwi->flags = 0; 3163 fwi->flags |= (fwi->lock_owner != 0) ? FUSE_WRITE_LOCKOWNER : 0; 3164 fwi->flags |= (ioflag & IO_DIRECT) ? 0 : FUSE_WRITE_CACHE; 3165 (void)memcpy((fwi + 1), buf, data_len); 3166 3167 3168 #ifdef PERFUSE_DEBUG 3169 if (perfuse_diagflags & PDF_FH) 3170 DPRINTF("%s: opc = %p, nodeid = 0x%"PRIx64", " 3171 "fh = 0x%"PRIx64"\n", __func__, 3172 (void *)opc, pnd->pnd_nodeid, fwi->fh); 3173 #endif 3174 if ((error = xchg_msg(pu, opc, pm, 3175 sizeof(*fwo), wait_reply)) != 0) 3176 goto out; 3177 3178 fwo = GET_OUTPAYLOAD(ps, pm, fuse_write_out); 3179 written = fwo->size; 3180 ps->ps_destroy_msg(pm); 3181 3182 #ifdef PERFUSE_DEBUG 3183 if (written > *resid) 3184 DERRX(EX_SOFTWARE, "%s: Unexpected big write %zd", 3185 __func__, written); 3186 #endif 3187 *resid -= written; 3188 offset += written; 3189 buf += written; 3190 3191 } while (*resid != 0); 3192 3193 /* 3194 * puffs_ops(3) says 3195 * "everything must be written or an error will be generated" 3196 */ 3197 if (*resid != 0) 3198 error = EFBIG; 3199 3200 #ifdef PERFUSE_DEBUG 3201 if (perfuse_diagflags & PDF_RESIZE) { 3202 if (offset > (off_t)vap->va_size) 3203 DPRINTF("<< %s %p %" PRIu64 " -> %lld\n", __func__, 3204 (void *)opc, vap->va_size, (long long)offset); 3205 else 3206 DPRINTF("<< %s %p \n", __func__, (void *)opc); 3207 } 3208 #endif 3209 3210 /* 3211 * Update file size if we wrote beyond the end 3212 */ 3213 if (offset > (off_t)vap->va_size) 3214 vap->va_size = offset; 3215 3216 if (inresize) { 3217 #ifdef PERFUSE_DEBUG 3218 if (!(pnd->pnd_flags & PND_INRESIZE)) 3219 DERRX(EX_SOFTWARE, "file write grow without resize"); 3220 #endif 3221 pnd->pnd_flags &= ~PND_INRESIZE; 3222 (void)dequeue_requests(opc, PCQ_RESIZE, DEQUEUE_ALL); 3223 } 3224 3225 3226 /* 3227 * Statistics 3228 */ 3229 if (ioflag & (IO_SYNC|IO_DSYNC)) 3230 ps->ps_syncwrites++; 3231 else 3232 ps->ps_asyncwrites++; 3233 3234 /* 3235 * Remember to sync the file 3236 */ 3237 pnd->pnd_flags |= PND_DIRTY; 3238 3239 #ifdef PERFUSE_DEBUG 3240 if (perfuse_diagflags & PDF_SYNC) 3241 DPRINTF("%s: DIRTY opc = %p, file = \"%s\"\n", 3242 __func__, (void*)opc, perfuse_node_path(ps, opc)); 3243 #endif 3244 3245 out: 3246 /* 3247 * VOP_PUTPAGE causes FAF write where kernel does not 3248 * check operation result. At least warn if it failed. 3249 */ 3250 #ifdef PUFFS_WRITE_FAF 3251 if (error && (xflag & PUFFS_WRITE_FAF)) 3252 DWARN("Data loss caused by FAF write failed on \"%s\"", 3253 pnd->pnd_name); 3254 #endif /* PUFFS_WRITE_FAF */ 3255 3256 /* 3257 * If there are no more queued write, we can resume 3258 * an operation awaiting write completion. 3259 */ 3260 pnd->pnd_flags &= ~PND_INWRITE; 3261 if (dequeue_requests(opc, PCQ_WRITE, 1) == 0) 3262 (void)dequeue_requests(opc, PCQ_AFTERWRITE, DEQUEUE_ALL); 3263 3264 node_rele(opc); 3265 return error; 3266 } 3267 3268 /* ARGSUSED0 */ 3269 void 3270 perfuse_cache_write(struct puffs_usermount *pu, puffs_cookie_t opc, size_t size, 3271 struct puffs_cacherun *runs) 3272 { 3273 return; 3274 } 3275 3276 /* ARGSUSED4 */ 3277 int 3278 perfuse_node_getextattr(struct puffs_usermount *pu, puffs_cookie_t opc, 3279 int attrns, const char *attrname, size_t *attrsize, uint8_t *attr, 3280 size_t *resid, const struct puffs_cred *pcr) 3281 { 3282 struct perfuse_state *ps; 3283 char fuse_attrname[LINUX_XATTR_NAME_MAX + 1]; 3284 perfuse_msg_t *pm; 3285 struct fuse_getxattr_in *fgi; 3286 struct fuse_getxattr_out *fgo; 3287 struct fuse_out_header *foh; 3288 size_t attrnamelen; 3289 size_t len; 3290 char *np; 3291 int error; 3292 3293 node_ref(opc); 3294 ps = puffs_getspecific(pu); 3295 attrname = perfuse_native_ns(attrns, attrname, fuse_attrname); 3296 attrnamelen = strlen(attrname) + 1; 3297 len = sizeof(*fgi) + attrnamelen; 3298 3299 pm = ps->ps_new_msg(pu, opc, FUSE_GETXATTR, len, pcr); 3300 fgi = GET_INPAYLOAD(ps, pm, fuse_getxattr_in); 3301 fgi->size = (unsigned int)((resid != NULL) ? *resid : 0); 3302 np = (char *)(void *)(fgi + 1); 3303 (void)strlcpy(np, attrname, attrnamelen); 3304 3305 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0) 3306 goto out; 3307 3308 /* 3309 * We just get fuse_getattr_out with list size if we requested 3310 * a null size. 3311 */ 3312 if (resid == NULL) { 3313 fgo = GET_OUTPAYLOAD(ps, pm, fuse_getxattr_out); 3314 3315 if (attrsize != NULL) 3316 *attrsize = fgo->size; 3317 3318 ps->ps_destroy_msg(pm); 3319 error = 0; 3320 goto out; 3321 } 3322 3323 /* 3324 * And with a non null requested size, we get the list just 3325 * after the header 3326 */ 3327 foh = GET_OUTHDR(ps, pm); 3328 np = (char *)(void *)(foh + 1); 3329 3330 if (resid != NULL) { 3331 len = MAX(foh->len - sizeof(*foh), *resid); 3332 (void)memcpy(attr, np, len); 3333 *resid -= len; 3334 } 3335 3336 ps->ps_destroy_msg(pm); 3337 error = 0; 3338 3339 out: 3340 node_rele(opc); 3341 return error; 3342 } 3343 3344 int 3345 perfuse_node_setextattr(struct puffs_usermount *pu, puffs_cookie_t opc, 3346 int attrns, const char *attrname, uint8_t *attr, size_t *resid, 3347 const struct puffs_cred *pcr) 3348 { 3349 struct perfuse_state *ps; 3350 char fuse_attrname[LINUX_XATTR_NAME_MAX + 1]; 3351 perfuse_msg_t *pm; 3352 struct fuse_setxattr_in *fsi; 3353 size_t attrnamelen; 3354 size_t len; 3355 char *np; 3356 int error; 3357 3358 node_ref(opc); 3359 ps = puffs_getspecific(pu); 3360 attrname = perfuse_native_ns(attrns, attrname, fuse_attrname); 3361 attrnamelen = strlen(attrname) + 1; 3362 len = sizeof(*fsi) + attrnamelen + *resid; 3363 3364 pm = ps->ps_new_msg(pu, opc, FUSE_SETXATTR, len, pcr); 3365 fsi = GET_INPAYLOAD(ps, pm, fuse_setxattr_in); 3366 fsi->size = (unsigned int)*resid; 3367 fsi->flags = 0; 3368 np = (char *)(void *)(fsi + 1); 3369 (void)strlcpy(np, attrname, attrnamelen); 3370 np += attrnamelen; 3371 (void)memcpy(np, (char *)attr, *resid); 3372 3373 if ((error = xchg_msg(pu, opc, pm, 3374 NO_PAYLOAD_REPLY_LEN, wait_reply)) != 0) 3375 goto out; 3376 3377 ps->ps_destroy_msg(pm); 3378 *resid = 0; 3379 error = 0; 3380 3381 out: 3382 node_rele(opc); 3383 return error; 3384 } 3385 3386 /* ARGSUSED2 */ 3387 int 3388 perfuse_node_listextattr(struct puffs_usermount *pu, puffs_cookie_t opc, 3389 int attrns, size_t *attrsize, uint8_t *attrs, size_t *resid, int flag, 3390 const struct puffs_cred *pcr) 3391 { 3392 struct perfuse_state *ps; 3393 perfuse_msg_t *pm; 3394 struct fuse_getxattr_in *fgi; 3395 struct fuse_getxattr_out *fgo; 3396 struct fuse_out_header *foh; 3397 char *np; 3398 size_t len, puffs_len; 3399 int error; 3400 3401 node_ref(opc); 3402 3403 ps = puffs_getspecific(pu); 3404 len = sizeof(*fgi); 3405 3406 pm = ps->ps_new_msg(pu, opc, FUSE_LISTXATTR, len, pcr); 3407 fgi = GET_INPAYLOAD(ps, pm, fuse_getxattr_in); 3408 if (resid != NULL) 3409 fgi->size = (unsigned int)*resid; 3410 else 3411 fgi->size = 0; 3412 3413 if ((error = xchg_msg(pu, opc, pm, UNSPEC_REPLY_LEN, wait_reply)) != 0) 3414 goto out; 3415 3416 /* 3417 * We just get fuse_getattr_out with list size if we requested 3418 * a null size. 3419 */ 3420 if (resid == NULL) { 3421 fgo = GET_OUTPAYLOAD(ps, pm, fuse_getxattr_out); 3422 3423 if (attrsize != NULL) 3424 *attrsize = fgo->size; 3425 3426 ps->ps_destroy_msg(pm); 3427 3428 error = 0; 3429 goto out; 3430 } 3431 3432 /* 3433 * And with a non null requested size, we get the list just 3434 * after the header 3435 */ 3436 foh = GET_OUTHDR(ps, pm); 3437 np = (char *)(void *)(foh + 1); 3438 puffs_len = foh->len - sizeof(*foh); 3439 3440 if (attrs != NULL) { 3441 #ifdef PUFFS_EXTATTR_LIST_LENPREFIX 3442 /* 3443 * Convert the FUSE reply to length prefixed strings 3444 * if this is what the kernel wants. 3445 */ 3446 if (flag & PUFFS_EXTATTR_LIST_LENPREFIX) { 3447 size_t i, attrlen; 3448 3449 for (i = 0; i < puffs_len; i += attrlen + 1) { 3450 attrlen = strlen(np + i); 3451 (void)memmove(np + i + 1, np + i, attrlen); 3452 *(np + i) = (uint8_t)attrlen; 3453 } 3454 } 3455 #endif /* PUFFS_EXTATTR_LIST_LENPREFIX */ 3456 (void)memcpy(attrs, np, puffs_len); 3457 *resid -= puffs_len; 3458 } 3459 3460 if (attrsize != NULL) 3461 *attrsize = puffs_len; 3462 3463 ps->ps_destroy_msg(pm); 3464 error = 0; 3465 3466 out: 3467 node_rele(opc); 3468 return error; 3469 } 3470 3471 int 3472 perfuse_node_deleteextattr(struct puffs_usermount *pu, puffs_cookie_t opc, 3473 int attrns, const char *attrname, const struct puffs_cred *pcr) 3474 { 3475 struct perfuse_state *ps; 3476 char fuse_attrname[LINUX_XATTR_NAME_MAX + 1]; 3477 perfuse_msg_t *pm; 3478 size_t attrnamelen; 3479 char *np; 3480 int error; 3481 3482 node_ref(opc); 3483 3484 ps = puffs_getspecific(pu); 3485 attrname = perfuse_native_ns(attrns, attrname, fuse_attrname); 3486 attrnamelen = strlen(attrname) + 1; 3487 3488 pm = ps->ps_new_msg(pu, opc, FUSE_REMOVEXATTR, attrnamelen, pcr); 3489 np = _GET_INPAYLOAD(ps, pm, char *); 3490 (void)strlcpy(np, attrname, attrnamelen); 3491 3492 error = xchg_msg(pu, opc, pm, NO_PAYLOAD_REPLY_LEN, wait_reply); 3493 3494 ps->ps_destroy_msg(pm); 3495 3496 node_rele(opc); 3497 return error; 3498 } 3499