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