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