1 /* $NetBSD: puffs_vfsops.c,v 1.86 2010/01/14 19:50:07 pooka Exp $ */ 2 3 /* 4 * Copyright (c) 2005, 2006 Antti Kantee. All Rights Reserved. 5 * 6 * Development of this software was supported by the 7 * Google Summer of Code program and the Ulla Tuominen Foundation. 8 * The Google SoC project was mentored by Bill Studenmund. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __KERNEL_RCSID(0, "$NetBSD: puffs_vfsops.c,v 1.86 2010/01/14 19:50:07 pooka Exp $"); 34 35 #include <sys/param.h> 36 #include <sys/mount.h> 37 #include <sys/malloc.h> 38 #include <sys/extattr.h> 39 #include <sys/queue.h> 40 #include <sys/vnode.h> 41 #include <sys/dirent.h> 42 #include <sys/kauth.h> 43 #include <sys/proc.h> 44 #include <sys/module.h> 45 #include <sys/kthread.h> 46 47 #include <dev/putter/putter_sys.h> 48 49 #include <miscfs/genfs/genfs.h> 50 51 #include <fs/puffs/puffs_msgif.h> 52 #include <fs/puffs/puffs_sys.h> 53 54 #include <lib/libkern/libkern.h> 55 56 #include <nfs/nfsproto.h> /* for fh sizes */ 57 58 MODULE(MODULE_CLASS_VFS, puffs, "putter"); 59 60 VFS_PROTOS(puffs_vfsop); 61 62 #ifndef PUFFS_PNODEBUCKETS 63 #define PUFFS_PNODEBUCKETS 256 64 #endif 65 #ifndef PUFFS_MAXPNODEBUCKETS 66 #define PUFFS_MAXPNODEBUCKETS 8192 67 #endif 68 int puffs_pnodebuckets_default = PUFFS_PNODEBUCKETS; 69 int puffs_maxpnodebuckets = PUFFS_MAXPNODEBUCKETS; 70 71 #define BUCKETALLOC(a) (sizeof(struct puffs_pnode_hashlist *) * (a)) 72 73 static struct putter_ops puffs_putter = { 74 .pop_getout = puffs_msgif_getout, 75 .pop_releaseout = puffs_msgif_releaseout, 76 .pop_waitcount = puffs_msgif_waitcount, 77 .pop_dispatch = puffs_msgif_dispatch, 78 .pop_close = puffs_msgif_close, 79 }; 80 81 int 82 puffs_vfsop_mount(struct mount *mp, const char *path, void *data, 83 size_t *data_len) 84 { 85 struct puffs_mount *pmp = NULL; 86 struct puffs_kargs *args; 87 char fstype[_VFS_NAMELEN]; 88 char *p; 89 int error = 0, i; 90 pid_t mntpid = curlwp->l_proc->p_pid; 91 92 if (*data_len < sizeof *args) 93 return EINVAL; 94 95 if (mp->mnt_flag & MNT_GETARGS) { 96 pmp = MPTOPUFFSMP(mp); 97 *(struct puffs_kargs *)data = pmp->pmp_args; 98 *data_len = sizeof *args; 99 return 0; 100 } 101 102 /* update is not supported currently */ 103 if (mp->mnt_flag & MNT_UPDATE) 104 return EOPNOTSUPP; 105 106 /* 107 * We need the file system name 108 */ 109 if (!data) 110 return EINVAL; 111 112 args = (struct puffs_kargs *)data; 113 114 /* devel phase */ 115 if (args->pa_vers != (PUFFSVERSION | PUFFSDEVELVERS)) { 116 printf("puffs_mount: development version mismatch: " 117 "kernel %d, lib %d\n", 118 PUFFSVERSION, args->pa_vers & ~PUFFSDEVELVERS); 119 error = EINVAL; 120 goto out; 121 } 122 123 if ((args->pa_flags & ~PUFFS_KFLAG_MASK) != 0) { 124 printf("puffs_mount: invalid KFLAGs 0x%x\n", args->pa_flags); 125 error = EINVAL; 126 goto out; 127 } 128 if ((args->pa_fhflags & ~PUFFS_FHFLAG_MASK) != 0) { 129 printf("puffs_mount: invalid FHFLAGs 0x%x\n", args->pa_fhflags); 130 error = EINVAL; 131 goto out; 132 } 133 134 /* use dummy value for passthrough */ 135 if (args->pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) 136 args->pa_fhsize = sizeof(struct fid); 137 138 /* sanitize file handle length */ 139 if (PUFFS_TOFHSIZE(args->pa_fhsize) > FHANDLE_SIZE_MAX) { 140 printf("puffs_mount: handle size %zu too large\n", 141 args->pa_fhsize); 142 error = EINVAL; 143 goto out; 144 } 145 /* sanity check file handle max sizes */ 146 if (args->pa_fhsize && args->pa_fhflags & PUFFS_FHFLAG_PROTOMASK) { 147 size_t kfhsize = PUFFS_TOFHSIZE(args->pa_fhsize); 148 149 if (args->pa_fhflags & PUFFS_FHFLAG_NFSV2) { 150 if (NFSX_FHTOOBIG_P(kfhsize, 0)) { 151 printf("puffs_mount: fhsize larger than " 152 "NFSv2 max %d\n", 153 PUFFS_FROMFHSIZE(NFSX_V2FH)); 154 error = EINVAL; 155 goto out; 156 } 157 } 158 159 if (args->pa_fhflags & PUFFS_FHFLAG_NFSV3) { 160 if (NFSX_FHTOOBIG_P(kfhsize, 1)) { 161 printf("puffs_mount: fhsize larger than " 162 "NFSv3 max %d\n", 163 PUFFS_FROMFHSIZE(NFSX_V3FHMAX)); 164 error = EINVAL; 165 goto out; 166 } 167 } 168 } 169 170 /* don't allow non-printing characters (like my sweet umlauts.. snif) */ 171 args->pa_typename[sizeof(args->pa_typename)-1] = '\0'; 172 for (p = args->pa_typename; *p; p++) 173 if (*p < ' ' || *p > '~') 174 *p = '.'; 175 176 args->pa_mntfromname[sizeof(args->pa_mntfromname)-1] = '\0'; 177 for (p = args->pa_mntfromname; *p; p++) 178 if (*p < ' ' || *p > '~') 179 *p = '.'; 180 181 /* build real name */ 182 (void)strlcpy(fstype, PUFFS_TYPEPREFIX, sizeof(fstype)); 183 (void)strlcat(fstype, args->pa_typename, sizeof(fstype)); 184 185 /* inform user server if it got the max request size it wanted */ 186 if (args->pa_maxmsglen == 0 || args->pa_maxmsglen > PUFFS_MSG_MAXSIZE) 187 args->pa_maxmsglen = PUFFS_MSG_MAXSIZE; 188 else if (args->pa_maxmsglen < 2*PUFFS_MSGSTRUCT_MAX) 189 args->pa_maxmsglen = 2*PUFFS_MSGSTRUCT_MAX; 190 191 (void)strlcpy(args->pa_typename, fstype, sizeof(args->pa_typename)); 192 193 if (args->pa_nhashbuckets == 0) 194 args->pa_nhashbuckets = puffs_pnodebuckets_default; 195 if (args->pa_nhashbuckets < 1) 196 args->pa_nhashbuckets = 1; 197 if (args->pa_nhashbuckets > PUFFS_MAXPNODEBUCKETS) { 198 args->pa_nhashbuckets = puffs_maxpnodebuckets; 199 printf("puffs_mount: using %d hash buckets. " 200 "adjust puffs_maxpnodebuckets for more\n", 201 puffs_maxpnodebuckets); 202 } 203 204 error = set_statvfs_info(path, UIO_USERSPACE, args->pa_mntfromname, 205 UIO_SYSSPACE, fstype, mp, curlwp); 206 if (error) 207 goto out; 208 mp->mnt_stat.f_iosize = DEV_BSIZE; 209 210 /* 211 * We can't handle the VFS_STATVFS() mount_domount() does 212 * after VFS_MOUNT() because we'd deadlock, so handle it 213 * here already. 214 */ 215 copy_statvfs_info(&args->pa_svfsb, mp); 216 (void)memcpy(&mp->mnt_stat, &args->pa_svfsb, sizeof(mp->mnt_stat)); 217 218 pmp = kmem_zalloc(sizeof(struct puffs_mount), KM_SLEEP); 219 220 mp->mnt_fs_bshift = DEV_BSHIFT; 221 mp->mnt_dev_bshift = DEV_BSHIFT; 222 mp->mnt_flag &= ~MNT_LOCAL; /* we don't really know, so ... */ 223 mp->mnt_data = pmp; 224 225 pmp->pmp_status = PUFFSTAT_MOUNTING; 226 pmp->pmp_mp = mp; 227 pmp->pmp_msg_maxsize = args->pa_maxmsglen; 228 pmp->pmp_args = *args; 229 230 pmp->pmp_npnodehash = args->pa_nhashbuckets; 231 pmp->pmp_pnodehash = kmem_alloc(BUCKETALLOC(pmp->pmp_npnodehash), KM_SLEEP); 232 for (i = 0; i < pmp->pmp_npnodehash; i++) 233 LIST_INIT(&pmp->pmp_pnodehash[i]); 234 LIST_INIT(&pmp->pmp_newcookie); 235 236 /* 237 * Inform the fileops processing code that we have a mountpoint. 238 * If it doesn't know about anyone with our pid/fd having the 239 * device open, punt 240 */ 241 if ((pmp->pmp_pi 242 = putter_attach(mntpid, args->pa_fd, pmp, &puffs_putter)) == NULL) { 243 error = ENOENT; 244 goto out; 245 } 246 247 /* XXX: check parameters */ 248 pmp->pmp_root_cookie = args->pa_root_cookie; 249 pmp->pmp_root_vtype = args->pa_root_vtype; 250 pmp->pmp_root_vsize = args->pa_root_vsize; 251 pmp->pmp_root_rdev = args->pa_root_rdev; 252 253 mutex_init(&pmp->pmp_lock, MUTEX_DEFAULT, IPL_NONE); 254 mutex_init(&pmp->pmp_sopmtx, MUTEX_DEFAULT, IPL_NONE); 255 cv_init(&pmp->pmp_msg_waiter_cv, "puffsget"); 256 cv_init(&pmp->pmp_refcount_cv, "puffsref"); 257 cv_init(&pmp->pmp_unmounting_cv, "puffsum"); 258 cv_init(&pmp->pmp_sopcv, "puffsop"); 259 TAILQ_INIT(&pmp->pmp_msg_touser); 260 TAILQ_INIT(&pmp->pmp_msg_replywait); 261 TAILQ_INIT(&pmp->pmp_sopreqs); 262 263 if ((error = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, 264 puffs_sop_thread, pmp, NULL, "puffsop")) != 0) 265 goto out; 266 pmp->pmp_sopthrcount = 1; 267 268 DPRINTF(("puffs_mount: mount point at %p, puffs specific at %p\n", 269 mp, MPTOPUFFSMP(mp))); 270 271 vfs_getnewfsid(mp); 272 273 out: 274 if (error && pmp && pmp->pmp_pi) 275 putter_detach(pmp->pmp_pi); 276 if (error && pmp && pmp->pmp_pnodehash) 277 kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash)); 278 if (error && pmp) 279 kmem_free(pmp, sizeof(struct puffs_mount)); 280 return error; 281 } 282 283 int 284 puffs_vfsop_start(struct mount *mp, int flags) 285 { 286 struct puffs_mount *pmp = MPTOPUFFSMP(mp); 287 288 KASSERT(pmp->pmp_status == PUFFSTAT_MOUNTING); 289 pmp->pmp_status = PUFFSTAT_RUNNING; 290 291 return 0; 292 } 293 294 int 295 puffs_vfsop_unmount(struct mount *mp, int mntflags) 296 { 297 PUFFS_MSG_VARS(vfs, unmount); 298 struct puffs_mount *pmp; 299 int error, force; 300 301 error = 0; 302 force = mntflags & MNT_FORCE; 303 pmp = MPTOPUFFSMP(mp); 304 305 DPRINTF(("puffs_unmount: detach filesystem from vfs, current " 306 "status 0x%x\n", pmp->pmp_status)); 307 308 /* 309 * flush all the vnodes. VOP_RECLAIM() takes care that the 310 * root vnode does not get flushed until unmount. The 311 * userspace root node cookie is stored in the mount 312 * structure, so we can always re-instantiate a root vnode, 313 * should userspace unmount decide it doesn't want to 314 * cooperate. 315 */ 316 error = vflush(mp, NULLVP, force ? FORCECLOSE : 0); 317 if (error) 318 goto out; 319 320 /* 321 * If we are not DYING, we should ask userspace's opinion 322 * about the situation 323 */ 324 mutex_enter(&pmp->pmp_lock); 325 if (pmp->pmp_status != PUFFSTAT_DYING) { 326 pmp->pmp_unmounting = 1; 327 mutex_exit(&pmp->pmp_lock); 328 329 PUFFS_MSG_ALLOC(vfs, unmount); 330 puffs_msg_setinfo(park_unmount, 331 PUFFSOP_VFS, PUFFS_VFS_UNMOUNT, NULL); 332 unmount_msg->pvfsr_flags = mntflags; 333 334 PUFFS_MSG_ENQUEUEWAIT(pmp, park_unmount, error); 335 PUFFS_MSG_RELEASE(unmount); 336 337 error = checkerr(pmp, error, __func__); 338 DPRINTF(("puffs_unmount: error %d force %d\n", error, force)); 339 340 mutex_enter(&pmp->pmp_lock); 341 pmp->pmp_unmounting = 0; 342 cv_broadcast(&pmp->pmp_unmounting_cv); 343 } 344 345 /* 346 * if userspace cooperated or we really need to die, 347 * screw what userland thinks and just die. 348 */ 349 if (error == 0 || force) { 350 struct puffs_sopreq *psopr; 351 352 /* tell waiters & other resources to go unwait themselves */ 353 puffs_userdead(pmp); 354 putter_detach(pmp->pmp_pi); 355 356 /* 357 * Wait until there are no more users for the mount resource. 358 * Notice that this is hooked against transport_close 359 * and return from touser. In an ideal world, it would 360 * be hooked against final return from all operations. 361 * But currently it works well enough, since nobody 362 * does weird blocking voodoo after return from touser(). 363 */ 364 while (pmp->pmp_refcount != 0) 365 cv_wait(&pmp->pmp_refcount_cv, &pmp->pmp_lock); 366 mutex_exit(&pmp->pmp_lock); 367 368 /* 369 * Release kernel thread now that there is nothing 370 * it would be wanting to lock. 371 */ 372 psopr = kmem_alloc(sizeof(*psopr), KM_SLEEP); 373 psopr->psopr_sopreq = PUFFS_SOPREQSYS_EXIT; 374 mutex_enter(&pmp->pmp_sopmtx); 375 if (pmp->pmp_sopthrcount == 0) { 376 mutex_exit(&pmp->pmp_sopmtx); 377 kmem_free(psopr, sizeof(*psopr)); 378 mutex_enter(&pmp->pmp_sopmtx); 379 KASSERT(pmp->pmp_sopthrcount == 0); 380 } else { 381 TAILQ_INSERT_TAIL(&pmp->pmp_sopreqs, 382 psopr, psopr_entries); 383 cv_signal(&pmp->pmp_sopcv); 384 } 385 while (pmp->pmp_sopthrcount > 0) 386 cv_wait(&pmp->pmp_sopcv, &pmp->pmp_sopmtx); 387 mutex_exit(&pmp->pmp_sopmtx); 388 389 /* free resources now that we hopefully have no waiters left */ 390 cv_destroy(&pmp->pmp_unmounting_cv); 391 cv_destroy(&pmp->pmp_refcount_cv); 392 cv_destroy(&pmp->pmp_msg_waiter_cv); 393 cv_destroy(&pmp->pmp_sopcv); 394 mutex_destroy(&pmp->pmp_lock); 395 mutex_destroy(&pmp->pmp_sopmtx); 396 397 kmem_free(pmp->pmp_pnodehash, BUCKETALLOC(pmp->pmp_npnodehash)); 398 kmem_free(pmp, sizeof(struct puffs_mount)); 399 error = 0; 400 } else { 401 mutex_exit(&pmp->pmp_lock); 402 } 403 404 out: 405 DPRINTF(("puffs_unmount: return %d\n", error)); 406 return error; 407 } 408 409 /* 410 * This doesn't need to travel to userspace 411 */ 412 int 413 puffs_vfsop_root(struct mount *mp, struct vnode **vpp) 414 { 415 struct puffs_mount *pmp = MPTOPUFFSMP(mp); 416 int rv; 417 418 rv = puffs_cookie2vnode(pmp, pmp->pmp_root_cookie, 1, 1, vpp); 419 KASSERT(rv != PUFFS_NOSUCHCOOKIE); 420 return rv; 421 } 422 423 int 424 puffs_vfsop_statvfs(struct mount *mp, struct statvfs *sbp) 425 { 426 PUFFS_MSG_VARS(vfs, statvfs); 427 struct puffs_mount *pmp; 428 int error = 0; 429 430 pmp = MPTOPUFFSMP(mp); 431 432 /* 433 * If we are mounting, it means that the userspace counterpart 434 * is calling mount(2), but mount(2) also calls statvfs. So 435 * requesting statvfs from userspace would mean a deadlock. 436 * Compensate. 437 */ 438 if (pmp->pmp_status == PUFFSTAT_MOUNTING) 439 return EINPROGRESS; 440 441 PUFFS_MSG_ALLOC(vfs, statvfs); 442 puffs_msg_setinfo(park_statvfs, PUFFSOP_VFS, PUFFS_VFS_STATVFS, NULL); 443 444 PUFFS_MSG_ENQUEUEWAIT(pmp, park_statvfs, error); 445 error = checkerr(pmp, error, __func__); 446 statvfs_msg->pvfsr_sb.f_iosize = DEV_BSIZE; 447 448 /* 449 * Try to produce a sensible result even in the event 450 * of userspace error. 451 * 452 * XXX: cache the copy in non-error case 453 */ 454 if (!error) { 455 copy_statvfs_info(&statvfs_msg->pvfsr_sb, mp); 456 (void)memcpy(sbp, &statvfs_msg->pvfsr_sb, 457 sizeof(struct statvfs)); 458 } else { 459 copy_statvfs_info(sbp, mp); 460 } 461 462 PUFFS_MSG_RELEASE(statvfs); 463 return error; 464 } 465 466 static int 467 pageflush(struct mount *mp, kauth_cred_t cred, int waitfor) 468 { 469 struct puffs_node *pn; 470 struct vnode *vp, *mvp; 471 int error, rv; 472 473 error = 0; 474 475 /* Allocate a marker vnode. */ 476 if ((mvp = vnalloc(mp)) == NULL) 477 return ENOMEM; 478 479 /* 480 * Sync all cached data from regular vnodes (which are not 481 * currently locked, see below). After this we call VFS_SYNC 482 * for the fs server, which should handle data and metadata for 483 * all the nodes it knows to exist. 484 */ 485 mutex_enter(&mntvnode_lock); 486 loop: 487 for (vp = TAILQ_FIRST(&mp->mnt_vnodelist); vp; vp = vunmark(mvp)) { 488 vmark(mvp, vp); 489 if (vp->v_mount != mp || vismarker(vp)) 490 continue; 491 492 mutex_enter(&vp->v_interlock); 493 pn = VPTOPP(vp); 494 if (vp->v_type != VREG || UVM_OBJ_IS_CLEAN(&vp->v_uobj)) { 495 mutex_exit(&vp->v_interlock); 496 continue; 497 } 498 499 mutex_exit(&mntvnode_lock); 500 501 /* 502 * Here we try to get a reference to the vnode and to 503 * lock it. This is mostly cargo-culted, but I will 504 * offer an explanation to why I believe this might 505 * actually do the right thing. 506 * 507 * If the vnode is a goner, we quite obviously don't need 508 * to sync it. 509 * 510 * If the vnode was busy, we don't need to sync it because 511 * this is never called with MNT_WAIT except from 512 * dounmount(), when we are wait-flushing all the dirty 513 * vnodes through other routes in any case. So there, 514 * sync() doesn't actually sync. Happy now? 515 */ 516 rv = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK); 517 if (rv) { 518 mutex_enter(&mntvnode_lock); 519 if (rv == ENOENT) { 520 (void)vunmark(mvp); 521 goto loop; 522 } 523 continue; 524 } 525 526 /* hmm.. is the FAF thing entirely sensible? */ 527 if (waitfor == MNT_LAZY) { 528 mutex_enter(&vp->v_interlock); 529 pn->pn_stat |= PNODE_FAF; 530 mutex_exit(&vp->v_interlock); 531 } 532 rv = VOP_FSYNC(vp, cred, waitfor, 0, 0); 533 if (waitfor == MNT_LAZY) { 534 mutex_enter(&vp->v_interlock); 535 pn->pn_stat &= ~PNODE_FAF; 536 mutex_exit(&vp->v_interlock); 537 } 538 if (rv) 539 error = rv; 540 vput(vp); 541 mutex_enter(&mntvnode_lock); 542 } 543 mutex_exit(&mntvnode_lock); 544 vnfree(mvp); 545 546 return error; 547 } 548 549 int 550 puffs_vfsop_sync(struct mount *mp, int waitfor, struct kauth_cred *cred) 551 { 552 PUFFS_MSG_VARS(vfs, sync); 553 struct puffs_mount *pmp = MPTOPUFFSMP(mp); 554 int error, rv; 555 556 error = pageflush(mp, cred, waitfor); 557 558 /* sync fs */ 559 PUFFS_MSG_ALLOC(vfs, sync); 560 sync_msg->pvfsr_waitfor = waitfor; 561 puffs_credcvt(&sync_msg->pvfsr_cred, cred); 562 puffs_msg_setinfo(park_sync, PUFFSOP_VFS, PUFFS_VFS_SYNC, NULL); 563 564 PUFFS_MSG_ENQUEUEWAIT(pmp, park_sync, rv); 565 rv = checkerr(pmp, rv, __func__); 566 if (rv) 567 error = rv; 568 569 PUFFS_MSG_RELEASE(sync); 570 return error; 571 } 572 573 int 574 puffs_vfsop_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp) 575 { 576 PUFFS_MSG_VARS(vfs, fhtonode); 577 struct puffs_mount *pmp = MPTOPUFFSMP(mp); 578 struct vnode *vp; 579 void *fhdata; 580 size_t argsize, fhlen; 581 int error; 582 583 if (pmp->pmp_args.pa_fhsize == 0) 584 return EOPNOTSUPP; 585 586 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) { 587 fhlen = fhp->fid_len; 588 fhdata = fhp; 589 } else { 590 fhlen = PUFFS_FROMFHSIZE(fhp->fid_len); 591 fhdata = fhp->fid_data; 592 593 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) { 594 if (pmp->pmp_args.pa_fhsize < fhlen) 595 return EINVAL; 596 } else { 597 if (pmp->pmp_args.pa_fhsize != fhlen) 598 return EINVAL; 599 } 600 } 601 602 argsize = sizeof(struct puffs_vfsmsg_fhtonode) + fhlen; 603 puffs_msgmem_alloc(argsize, &park_fhtonode, (void *)&fhtonode_msg, 1); 604 fhtonode_msg->pvfsr_dsize = fhlen; 605 memcpy(fhtonode_msg->pvfsr_data, fhdata, fhlen); 606 puffs_msg_setinfo(park_fhtonode, PUFFSOP_VFS, PUFFS_VFS_FHTOVP, NULL); 607 608 PUFFS_MSG_ENQUEUEWAIT(pmp, park_fhtonode, error); 609 error = checkerr(pmp, error, __func__); 610 if (error) 611 goto out; 612 613 error = puffs_cookie2vnode(pmp, fhtonode_msg->pvfsr_fhcookie, 1,1,&vp); 614 DPRINTF(("puffs_fhtovp: got cookie %p, existing vnode %p\n", 615 fhtonode_msg->pvfsr_fhcookie, vp)); 616 if (error == PUFFS_NOSUCHCOOKIE) { 617 error = puffs_getvnode(mp, fhtonode_msg->pvfsr_fhcookie, 618 fhtonode_msg->pvfsr_vtype, fhtonode_msg->pvfsr_size, 619 fhtonode_msg->pvfsr_rdev, &vp); 620 if (error) 621 goto out; 622 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); 623 } else if (error) { 624 goto out; 625 } 626 627 *vpp = vp; 628 out: 629 puffs_msgmem_release(park_fhtonode); 630 return error; 631 } 632 633 int 634 puffs_vfsop_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size) 635 { 636 PUFFS_MSG_VARS(vfs, nodetofh); 637 struct puffs_mount *pmp = MPTOPUFFSMP(vp->v_mount); 638 size_t argsize, fhlen; 639 int error; 640 641 if (pmp->pmp_args.pa_fhsize == 0) 642 return EOPNOTSUPP; 643 644 /* if file handles are static len, we can test len immediately */ 645 if (((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) == 0) 646 && ((pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) == 0) 647 && (PUFFS_FROMFHSIZE(*fh_size) < pmp->pmp_args.pa_fhsize)) { 648 *fh_size = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize); 649 return E2BIG; 650 } 651 652 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) 653 fhlen = *fh_size; 654 else 655 fhlen = PUFFS_FROMFHSIZE(*fh_size); 656 657 argsize = sizeof(struct puffs_vfsmsg_nodetofh) + fhlen; 658 puffs_msgmem_alloc(argsize, &park_nodetofh, (void *)&nodetofh_msg, 1); 659 nodetofh_msg->pvfsr_fhcookie = VPTOPNC(vp); 660 nodetofh_msg->pvfsr_dsize = fhlen; 661 puffs_msg_setinfo(park_nodetofh, PUFFSOP_VFS, PUFFS_VFS_VPTOFH, NULL); 662 663 PUFFS_MSG_ENQUEUEWAIT(pmp, park_nodetofh, error); 664 error = checkerr(pmp, error, __func__); 665 666 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) 667 fhlen = nodetofh_msg->pvfsr_dsize; 668 else if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_DYNAMIC) 669 fhlen = PUFFS_TOFHSIZE(nodetofh_msg->pvfsr_dsize); 670 else 671 fhlen = PUFFS_TOFHSIZE(pmp->pmp_args.pa_fhsize); 672 673 if (error) { 674 if (error == E2BIG) 675 *fh_size = fhlen; 676 goto out; 677 } 678 679 if (fhlen > FHANDLE_SIZE_MAX) { 680 puffs_senderr(pmp, PUFFS_ERR_VPTOFH, E2BIG, 681 "file handle too big", VPTOPNC(vp)); 682 error = EPROTO; 683 goto out; 684 } 685 686 if (*fh_size < fhlen) { 687 *fh_size = fhlen; 688 error = E2BIG; 689 goto out; 690 } 691 *fh_size = fhlen; 692 693 if (fhp) { 694 if (pmp->pmp_args.pa_fhflags & PUFFS_FHFLAG_PASSTHROUGH) { 695 memcpy(fhp, nodetofh_msg->pvfsr_data, fhlen); 696 } else { 697 fhp->fid_len = *fh_size; 698 memcpy(fhp->fid_data, nodetofh_msg->pvfsr_data, 699 nodetofh_msg->pvfsr_dsize); 700 } 701 } 702 703 out: 704 puffs_msgmem_release(park_nodetofh); 705 return error; 706 } 707 708 void 709 puffs_vfsop_init(void) 710 { 711 712 /* some checks depend on this */ 713 KASSERT(VNOVAL == VSIZENOTSET); 714 715 pool_init(&puffs_pnpool, sizeof(struct puffs_node), 0, 0, 0, 716 "puffpnpl", &pool_allocator_nointr, IPL_NONE); 717 puffs_msgif_init(); 718 } 719 720 void 721 puffs_vfsop_done(void) 722 { 723 724 puffs_msgif_destroy(); 725 pool_destroy(&puffs_pnpool); 726 } 727 728 int 729 puffs_vfsop_snapshot(struct mount *mp, struct vnode *vp, struct timespec *ts) 730 { 731 732 return EOPNOTSUPP; 733 } 734 735 const struct vnodeopv_desc * const puffs_vnodeopv_descs[] = { 736 &puffs_vnodeop_opv_desc, 737 &puffs_specop_opv_desc, 738 &puffs_fifoop_opv_desc, 739 &puffs_msgop_opv_desc, 740 NULL, 741 }; 742 743 struct vfsops puffs_vfsops = { 744 MOUNT_PUFFS, 745 sizeof (struct puffs_kargs), 746 puffs_vfsop_mount, /* mount */ 747 puffs_vfsop_start, /* start */ 748 puffs_vfsop_unmount, /* unmount */ 749 puffs_vfsop_root, /* root */ 750 (void *)eopnotsupp, /* quotactl */ 751 puffs_vfsop_statvfs, /* statvfs */ 752 puffs_vfsop_sync, /* sync */ 753 (void *)eopnotsupp, /* vget */ 754 puffs_vfsop_fhtovp, /* fhtovp */ 755 puffs_vfsop_vptofh, /* vptofh */ 756 puffs_vfsop_init, /* init */ 757 NULL, /* reinit */ 758 puffs_vfsop_done, /* done */ 759 NULL, /* mountroot */ 760 puffs_vfsop_snapshot, /* snapshot */ 761 vfs_stdextattrctl, /* extattrctl */ 762 (void *)eopnotsupp, /* suspendctl */ 763 genfs_renamelock_enter, 764 genfs_renamelock_exit, 765 (void *)eopnotsupp, 766 puffs_vnodeopv_descs, /* vnodeops */ 767 0, /* refcount */ 768 { NULL, NULL } 769 }; 770 771 static int 772 puffs_modcmd(modcmd_t cmd, void *arg) 773 { 774 775 switch (cmd) { 776 case MODULE_CMD_INIT: 777 return vfs_attach(&puffs_vfsops); 778 case MODULE_CMD_FINI: 779 return vfs_detach(&puffs_vfsops); 780 default: 781 return ENOTTY; 782 } 783 } 784