1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 #include <sys/param.h> 29 #include <sys/kmem.h> 30 #include <sys/errno.h> 31 #include <sys/proc.h> 32 #include <sys/disp.h> 33 #include <sys/vfs.h> 34 #include <sys/vnode.h> 35 #include <sys/pathname.h> 36 #include <sys/cred.h> 37 #include <sys/mount.h> 38 #include <sys/cmn_err.h> 39 #include <sys/debug.h> 40 #include <sys/systm.h> 41 #include <sys/dirent.h> 42 #include <fs/fs_subr.h> 43 #include <sys/fs/autofs.h> 44 #include <sys/callb.h> 45 #include <sys/sysmacros.h> 46 #include <sys/zone.h> 47 #include <sys/door.h> 48 #include <sys/fs/mntdata.h> 49 #include <nfs/mount.h> 50 #include <rpc/clnt.h> 51 #include <rpcsvc/autofs_prot.h> 52 #include <nfs/rnode.h> 53 #include <sys/utsname.h> 54 55 /* 56 * Autofs and Zones: 57 * 58 * Zones are delegated the responsibility of managing their own autofs mounts 59 * and maps. Each zone runs its own copy of automountd, with its own timeouts, 60 * and other logically "global" parameters. kRPC and virtualization in the 61 * loopback transport (tl) will prevent a zone from communicating with another 62 * zone's automountd. 63 * 64 * Each zone has its own "rootfnnode" and associated tree of auto nodes. 65 * 66 * Each zone also has its own set of "unmounter" kernel threads; these are 67 * created and run within the zone's context (ie, they are created via 68 * zthread_create()). 69 * 70 * Cross-zone mount triggers are disallowed. There is a check in 71 * auto_trigger_mount() to this effect; EPERM is returned to indicate that the 72 * mount is not owned by the caller. 73 * 74 * autofssys() enables a caller in the global zone to clean up in-kernel (as 75 * well as regular) autofs mounts via the unmount_tree() mechanism. This is 76 * routinely done when all mounts are removed as part of zone shutdown. 77 */ 78 #define TYPICALMAXPATHLEN 64 79 80 static kmutex_t autofs_nodeid_lock; 81 82 static int auto_perform_link(fnnode_t *, struct linka *, cred_t *); 83 static int auto_perform_actions(fninfo_t *, fnnode_t *, 84 action_list *, cred_t *); 85 static int auto_getmntpnt(vnode_t *, char *, vnode_t **, cred_t *); 86 static int auto_lookup_request(fninfo_t *, char *, struct linka *, 87 bool_t, bool_t *, cred_t *); 88 static int auto_mount_request(fninfo_t *, char *, action_list **, cred_t *, 89 bool_t); 90 91 extern struct autofs_globals *autofs_zone_init(void); 92 93 /* 94 * Clears the MF_INPROG flag, and wakes up those threads sleeping on 95 * fn_cv_mount if MF_WAITING is set. 96 */ 97 void 98 auto_unblock_others( 99 fnnode_t *fnp, 100 uint_t operation) /* either MF_INPROG or MF_LOOKUP */ 101 { 102 ASSERT(operation & (MF_INPROG | MF_LOOKUP)); 103 fnp->fn_flags &= ~operation; 104 if (fnp->fn_flags & MF_WAITING) { 105 fnp->fn_flags &= ~MF_WAITING; 106 cv_broadcast(&fnp->fn_cv_mount); 107 } 108 } 109 110 int 111 auto_wait4mount(fnnode_t *fnp) 112 { 113 int error; 114 k_sigset_t smask; 115 116 AUTOFS_DPRINT((4, "auto_wait4mount: fnp=%p\n", (void *)fnp)); 117 118 mutex_enter(&fnp->fn_lock); 119 while (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) { 120 /* 121 * There is a mount or a lookup in progress. 122 */ 123 fnp->fn_flags |= MF_WAITING; 124 sigintr(&smask, 1); 125 if (!cv_wait_sig(&fnp->fn_cv_mount, &fnp->fn_lock)) { 126 /* 127 * Decided not to wait for operation to 128 * finish after all. 129 */ 130 sigunintr(&smask); 131 mutex_exit(&fnp->fn_lock); 132 return (EINTR); 133 } 134 sigunintr(&smask); 135 } 136 error = fnp->fn_error; 137 138 if (error == EINTR) { 139 /* 140 * The thread doing the mount got interrupted, we need to 141 * try again, by returning EAGAIN. 142 */ 143 error = EAGAIN; 144 } 145 mutex_exit(&fnp->fn_lock); 146 147 AUTOFS_DPRINT((5, "auto_wait4mount: fnp=%p error=%d\n", (void *)fnp, 148 error)); 149 return (error); 150 } 151 152 int 153 auto_lookup_aux(fnnode_t *fnp, char *name, cred_t *cred) 154 { 155 struct fninfo *fnip; 156 struct linka link; 157 bool_t mountreq = FALSE; 158 int error = 0; 159 160 fnip = vfstofni(fntovn(fnp)->v_vfsp); 161 bzero(&link, sizeof (link)); 162 error = auto_lookup_request(fnip, name, &link, TRUE, &mountreq, cred); 163 if (!error) { 164 if (link.link != NULL || link.link != '\0') { 165 /* 166 * This node should be a symlink 167 */ 168 error = auto_perform_link(fnp, &link, cred); 169 } else if (mountreq) { 170 /* 171 * The automount daemon is requesting a mount, 172 * implying this entry must be a wildcard match and 173 * therefore in need of verification that the entry 174 * exists on the server. 175 */ 176 mutex_enter(&fnp->fn_lock); 177 AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG); 178 fnp->fn_error = 0; 179 180 /* 181 * Unblock other lookup requests on this node, 182 * this is needed to let the lookup generated by 183 * the mount call to complete. The caveat is 184 * other lookups on this node can also get by, 185 * i.e., another lookup on this node that occurs 186 * while this lookup is attempting the mount 187 * would return a positive result no matter what. 188 * Therefore two lookups on the this node could 189 * potentially get disparate results. 190 */ 191 AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP); 192 mutex_exit(&fnp->fn_lock); 193 /* 194 * auto_new_mount_thread fires up a new thread which 195 * calls automountd finishing up the work 196 */ 197 auto_new_mount_thread(fnp, name, cred); 198 199 /* 200 * At this point, we are simply another thread 201 * waiting for the mount to complete 202 */ 203 error = auto_wait4mount(fnp); 204 if (error == AUTOFS_SHUTDOWN) 205 error = ENOENT; 206 } 207 } 208 209 if (link.link) 210 kmem_free(link.link, strlen(link.link) + 1); 211 if (link.dir) 212 kmem_free(link.dir, strlen(link.dir) + 1); 213 mutex_enter(&fnp->fn_lock); 214 fnp->fn_error = error; 215 216 /* 217 * Notify threads waiting for lookup/mount that 218 * it's done. 219 */ 220 if (mountreq) { 221 AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 222 } else { 223 AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP); 224 } 225 mutex_exit(&fnp->fn_lock); 226 return (error); 227 } 228 229 /* 230 * Starting point for thread to handle mount requests with automountd. 231 * XXX auto_mount_thread() is not suspend-safe within the scope of 232 * the present model defined for cpr to suspend the system. Calls 233 * made by the auto_mount_thread() that have been identified to be unsafe 234 * are (1) RPC client handle setup and client calls to automountd which 235 * can block deep down in the RPC library, (2) kmem_alloc() calls with the 236 * KM_SLEEP flag which can block if memory is low, and (3) VFS_*(), and 237 * lookuppnvp() calls which can result in over the wire calls to servers. 238 * The thread should be completely reevaluated to make it suspend-safe in 239 * case of future updates to the cpr model. 240 */ 241 static void 242 auto_mount_thread(struct autofs_callargs *argsp) 243 { 244 struct fninfo *fnip; 245 fnnode_t *fnp; 246 vnode_t *vp; 247 char *name; 248 size_t namelen; 249 cred_t *cred; 250 action_list *alp = NULL; 251 int error; 252 callb_cpr_t cprinfo; 253 kmutex_t auto_mount_thread_cpr_lock; 254 255 mutex_init(&auto_mount_thread_cpr_lock, NULL, MUTEX_DEFAULT, NULL); 256 CALLB_CPR_INIT(&cprinfo, &auto_mount_thread_cpr_lock, 257 callb_generic_cpr, "auto_mount_thread"); 258 259 fnp = argsp->fnc_fnp; 260 vp = fntovn(fnp); 261 fnip = vfstofni(vp->v_vfsp); 262 name = argsp->fnc_name; 263 cred = argsp->fnc_cred; 264 ASSERT(crgetzoneid(argsp->fnc_cred) == fnip->fi_zoneid); 265 266 error = auto_mount_request(fnip, name, &alp, cred, TRUE); 267 if (!error) 268 error = auto_perform_actions(fnip, fnp, alp, cred); 269 mutex_enter(&fnp->fn_lock); 270 fnp->fn_error = error; 271 272 /* 273 * Notify threads waiting for mount that 274 * it's done. 275 */ 276 AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 277 mutex_exit(&fnp->fn_lock); 278 279 VN_RELE(vp); 280 crfree(argsp->fnc_cred); 281 namelen = strlen(argsp->fnc_name) + 1; 282 kmem_free(argsp->fnc_name, namelen); 283 kmem_free(argsp, sizeof (*argsp)); 284 285 mutex_enter(&auto_mount_thread_cpr_lock); 286 CALLB_CPR_EXIT(&cprinfo); 287 mutex_destroy(&auto_mount_thread_cpr_lock); 288 zthread_exit(); 289 /* NOTREACHED */ 290 } 291 292 static int autofs_thr_success = 0; 293 294 /* 295 * Creates new thread which calls auto_mount_thread which does 296 * the bulk of the work calling automountd, via 'auto_perform_actions'. 297 */ 298 void 299 auto_new_mount_thread(fnnode_t *fnp, char *name, cred_t *cred) 300 { 301 struct autofs_callargs *argsp; 302 303 argsp = kmem_alloc(sizeof (*argsp), KM_SLEEP); 304 VN_HOLD(fntovn(fnp)); 305 argsp->fnc_fnp = fnp; 306 argsp->fnc_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 307 (void) strcpy(argsp->fnc_name, name); 308 argsp->fnc_origin = curthread; 309 crhold(cred); 310 argsp->fnc_cred = cred; 311 312 (void) zthread_create(NULL, 0, auto_mount_thread, argsp, 0, 313 minclsyspri); 314 autofs_thr_success++; 315 } 316 317 318 int 319 auto_calldaemon( 320 zoneid_t zoneid, 321 int which, 322 xdrproc_t xarg_func, 323 void *argsp, 324 xdrproc_t xresp_func, 325 void *resp, 326 int reslen, 327 bool_t hard) /* retry forever? */ 328 { 329 330 int retry, error = 0; 331 k_sigset_t smask; 332 door_arg_t door_args; 333 door_handle_t dh; 334 XDR xdrarg, xdrres; 335 struct autofs_globals *fngp = NULL; 336 void *orig_resp = NULL; 337 int orig_reslen = reslen; 338 autofs_door_args_t *xdr_argsp; 339 int xdr_len = 0; 340 341 /* 342 * We know that the current thread is doing work on 343 * behalf of its own zone, so it's ok to use 344 * curproc->p_zone. 345 */ 346 ASSERT(zoneid == getzoneid()); 347 if (zone_status_get(curproc->p_zone) >= 348 ZONE_IS_SHUTTING_DOWN) { 349 /* 350 * There's no point in trying to talk to 351 * automountd. Plus, zone_shutdown() is 352 * waiting for us. 353 */ 354 return (ECONNREFUSED); 355 } 356 357 if ((fngp = zone_getspecific(autofs_key, curproc->p_zone)) == 358 NULL) { 359 fngp = autofs_zone_init(); 360 (void) zone_setspecific(autofs_key, curproc->p_zone, fngp); 361 } 362 363 ASSERT(fngp != NULL); 364 365 if (argsp != NULL && (xdr_len = xdr_sizeof(xarg_func, argsp)) == 0) 366 return (EINVAL); 367 xdr_argsp = kmem_zalloc(xdr_len + sizeof (*xdr_argsp), KM_SLEEP); 368 xdr_argsp->xdr_len = xdr_len; 369 xdr_argsp->cmd = which; 370 371 if (argsp) { 372 xdrmem_create(&xdrarg, (char *)&xdr_argsp->xdr_arg, 373 xdr_argsp->xdr_len, XDR_ENCODE); 374 375 if (!(*xarg_func)(&xdrarg, argsp)) { 376 kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp)); 377 return (EINVAL); 378 } 379 } 380 381 /* 382 * We're saving off the original pointer and length due to the 383 * possibility that the results buffer returned by the door 384 * upcall can be different then what we passed in. This is because 385 * the door will allocate new memory if the results buffer passed 386 * in isn't large enough to hold what we need to send back. 387 * In this case we need to free the memory originally allocated 388 * for that buffer. 389 */ 390 if (orig_reslen) 391 orig_resp = kmem_zalloc(orig_reslen, KM_SLEEP); 392 393 do { 394 retry = 0; 395 mutex_enter(&fngp->fng_autofs_daemon_lock); 396 dh = fngp->fng_autofs_daemon_dh; 397 if (dh) 398 door_ki_hold(dh); 399 mutex_exit(&fngp->fng_autofs_daemon_lock); 400 401 if (dh == NULL) { 402 if (orig_resp) 403 kmem_free(orig_resp, orig_reslen); 404 kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp)); 405 return (ENOENT); 406 } 407 door_args.data_ptr = (char *)xdr_argsp; 408 door_args.data_size = sizeof (*xdr_argsp) + xdr_argsp->xdr_len; 409 door_args.desc_ptr = NULL; 410 door_args.desc_num = 0; 411 door_args.rbuf = orig_resp ? (char *)orig_resp : NULL; 412 door_args.rsize = reslen; 413 414 sigintr(&smask, 1); 415 error = door_ki_upcall(dh, &door_args); 416 sigunintr(&smask); 417 418 door_ki_rele(dh); 419 420 if (!error) { 421 autofs_door_res_t *adr = 422 (autofs_door_res_t *)door_args.rbuf; 423 if (door_args.rbuf != NULL && 424 (error = adr->res_status)) { 425 kmem_free(xdr_argsp, 426 xdr_len + sizeof (*xdr_argsp)); 427 if (orig_resp) 428 kmem_free(orig_resp, orig_reslen); 429 return (error); 430 } 431 continue; 432 } 433 switch (error) { 434 case EINTR: 435 /* 436 * interrupts should be handled properly by the 437 * door upcall. 438 * 439 * We may have gotten EINTR for other reasons 440 * like the door being revoked on us. Instead 441 * of trying to extract this out of the door 442 * handle, sleep and try again, if still 443 * revoked we will get EBADF next time 444 * through. 445 */ 446 case EAGAIN: /* process may be forking */ 447 /* 448 * Back off for a bit 449 */ 450 delay(hz); 451 retry = 1; 452 break; 453 case EBADF: /* Invalid door */ 454 case EINVAL: /* Not a door, wrong target */ 455 /* 456 * A fatal door error, if our failing door 457 * handle is the current door handle, clean 458 * up our state. 459 */ 460 mutex_enter(&fngp->fng_autofs_daemon_lock); 461 if (dh == fngp->fng_autofs_daemon_dh) { 462 door_ki_rele(fngp->fng_autofs_daemon_dh); 463 fngp->fng_autofs_daemon_dh = NULL; 464 } 465 mutex_exit(&fngp->fng_autofs_daemon_lock); 466 AUTOFS_DPRINT((5, 467 "auto_calldaemon error=%d\n", error)); 468 if (hard) { 469 if (!fngp->fng_printed_not_running_msg) { 470 fngp->fng_printed_not_running_msg = 1; 471 zprintf(zoneid, "automountd not "\ 472 "running, retrying\n"); 473 } 474 delay(hz); 475 retry = 1; 476 break; 477 } else { 478 error = ECONNREFUSED; 479 kmem_free(xdr_argsp, 480 xdr_len + sizeof (*xdr_argsp)); 481 if (orig_resp) 482 kmem_free(orig_resp, orig_reslen); 483 return (error); 484 } 485 default: /* Unknown must be fatal */ 486 error = ENOENT; 487 kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp)); 488 if (orig_resp) 489 kmem_free(orig_resp, orig_reslen); 490 return (error); 491 } 492 } while (retry); 493 494 if (fngp->fng_printed_not_running_msg == 1) { 495 fngp->fng_printed_not_running_msg = 0; 496 zprintf(zoneid, "automountd OK\n"); 497 } 498 499 if (orig_resp && orig_reslen) { 500 autofs_door_res_t *door_resp; 501 door_resp = 502 (autofs_door_res_t *)door_args.rbuf; 503 if ((void *)door_args.rbuf != orig_resp) 504 kmem_free(orig_resp, orig_reslen); 505 xdrmem_create(&xdrres, (char *)&door_resp->xdr_res, 506 door_resp->xdr_len, XDR_DECODE); 507 if (!((*xresp_func)(&xdrres, resp))) 508 error = EINVAL; 509 kmem_free(door_args.rbuf, door_args.rsize); 510 } 511 kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp)); 512 return (error); 513 } 514 515 static int 516 auto_null_request(fninfo_t *fnip, bool_t hard) 517 { 518 int error; 519 struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals; 520 521 AUTOFS_DPRINT((4, "\tauto_null_request\n")); 522 523 error = auto_calldaemon(fngp->fng_zoneid, 524 NULLPROC, 525 xdr_void, 526 NULL, 527 xdr_void, 528 NULL, 529 0, 530 hard); 531 532 AUTOFS_DPRINT((5, "\tauto_null_request: error=%d\n", error)); 533 return (error); 534 } 535 536 static int 537 auto_lookup_request( 538 fninfo_t *fnip, 539 char *key, 540 struct linka *lnp, 541 bool_t hard, 542 bool_t *mountreq, 543 cred_t *cred) 544 { 545 int error; 546 struct autofs_globals *fngp; 547 struct autofs_lookupargs reqst; 548 autofs_lookupres *resp; 549 struct linka *p; 550 551 552 AUTOFS_DPRINT((4, "auto_lookup_equest: path=%s name=%s\n", 553 fnip->fi_path, key)); 554 555 fngp = vntofn(fnip->fi_rootvp)->fn_globals; 556 557 reqst.map = fnip->fi_map; 558 reqst.path = fnip->fi_path; 559 560 if (fnip->fi_flags & MF_DIRECT) 561 reqst.name = fnip->fi_key; 562 else 563 reqst.name = key; 564 AUTOFS_DPRINT((4, "auto_lookup_request: using key=%s\n", reqst.name)); 565 566 reqst.subdir = fnip->fi_subdir; 567 reqst.opts = fnip->fi_opts; 568 reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE; 569 reqst.uid = crgetuid(cred); 570 571 resp = kmem_zalloc(sizeof (*resp), KM_SLEEP); 572 573 error = auto_calldaemon(fngp->fng_zoneid, 574 AUTOFS_LOOKUP, 575 xdr_autofs_lookupargs, 576 &reqst, 577 xdr_autofs_lookupres, 578 (void *)resp, 579 sizeof (autofs_lookupres), 580 hard); 581 582 583 if (error) { 584 xdr_free(xdr_autofs_lookupres, (char *)resp); 585 kmem_free(resp, sizeof (*resp)); 586 return (error); 587 } 588 589 if (!error) { 590 fngp->fng_verbose = resp->lu_verbose; 591 switch (resp->lu_res) { 592 case AUTOFS_OK: 593 switch (resp->lu_type.action) { 594 case AUTOFS_MOUNT_RQ: 595 lnp->link = NULL; 596 lnp->dir = NULL; 597 *mountreq = TRUE; 598 break; 599 case AUTOFS_LINK_RQ: 600 p = 601 &resp->lu_type.lookup_result_type_u.lt_linka; 602 lnp->dir = kmem_alloc(strlen(p->dir) + 1, 603 KM_SLEEP); 604 (void) strcpy(lnp->dir, p->dir); 605 lnp->link = kmem_alloc(strlen(p->link) + 1, 606 KM_SLEEP); 607 (void) strcpy(lnp->link, p->link); 608 break; 609 case AUTOFS_NONE: 610 lnp->link = NULL; 611 lnp->dir = NULL; 612 break; 613 default: 614 auto_log(fngp->fng_verbose, 615 fngp->fng_zoneid, CE_WARN, 616 "auto_lookup_request: bad action type %d", 617 resp->lu_res); 618 error = ENOENT; 619 } 620 break; 621 case AUTOFS_NOENT: 622 error = ENOENT; 623 break; 624 default: 625 error = ENOENT; 626 auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN, 627 "auto_lookup_request: unknown result: %d", 628 resp->lu_res); 629 break; 630 } 631 } 632 done: 633 xdr_free(xdr_autofs_lookupres, (char *)resp); 634 kmem_free(resp, sizeof (*resp)); 635 AUTOFS_DPRINT((5, "auto_lookup_request: path=%s name=%s error=%d\n", 636 fnip->fi_path, key, error)); 637 return (error); 638 } 639 640 static int 641 auto_mount_request( 642 fninfo_t *fnip, 643 char *key, 644 action_list **alpp, 645 cred_t *cred, 646 bool_t hard) 647 { 648 int error; 649 struct autofs_globals *fngp; 650 autofs_lookupargs reqst; 651 autofs_mountres *xdrres = NULL; 652 653 AUTOFS_DPRINT((4, "auto_mount_request: path=%s name=%s\n", 654 fnip->fi_path, key)); 655 656 fngp = vntofn(fnip->fi_rootvp)->fn_globals; 657 reqst.map = fnip->fi_map; 658 reqst.path = fnip->fi_path; 659 660 if (fnip->fi_flags & MF_DIRECT) 661 reqst.name = fnip->fi_key; 662 else 663 reqst.name = key; 664 665 AUTOFS_DPRINT((4, "auto_mount_request: using key=%s\n", reqst.name)); 666 667 reqst.subdir = fnip->fi_subdir; 668 reqst.opts = fnip->fi_opts; 669 reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE; 670 reqst.uid = crgetuid(cred); 671 672 xdrres = kmem_zalloc(sizeof (*xdrres), KM_SLEEP); 673 674 error = auto_calldaemon(fngp->fng_zoneid, 675 AUTOFS_MNTINFO, 676 xdr_autofs_lookupargs, 677 &reqst, 678 xdr_autofs_mountres, 679 (void *)xdrres, 680 sizeof (autofs_mountres), 681 hard); 682 683 if (!error) { 684 fngp->fng_verbose = xdrres->mr_verbose; 685 switch (xdrres->mr_type.status) { 686 case AUTOFS_ACTION: 687 error = 0; 688 /* 689 * Save the action list since it is used by 690 * the caller. We NULL the action list pointer 691 * in 'result' so that xdr_free() will not free 692 * the list. 693 */ 694 *alpp = xdrres->mr_type.mount_result_type_u.list; 695 xdrres->mr_type.mount_result_type_u.list = NULL; 696 break; 697 case AUTOFS_DONE: 698 error = xdrres->mr_type.mount_result_type_u.error; 699 break; 700 default: 701 error = ENOENT; 702 auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN, 703 "auto_mount_request: unknown status %d", 704 xdrres->mr_type.status); 705 break; 706 } 707 } 708 709 xdr_free(xdr_autofs_mountres, (char *)xdrres); 710 kmem_free(xdrres, sizeof (*xdrres)); 711 712 713 AUTOFS_DPRINT((5, "auto_mount_request: path=%s name=%s error=%d\n", 714 fnip->fi_path, key, error)); 715 return (error); 716 } 717 718 719 static int 720 auto_send_unmount_request( 721 fninfo_t *fnip, 722 umntrequest *ul, 723 bool_t hard) 724 { 725 int error; 726 umntres xdrres; 727 728 struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals; 729 730 AUTOFS_DPRINT((4, "\tauto_send_unmount_request: fstype=%s " 731 " mntpnt=%s\n", ul->fstype, ul->mntpnt)); 732 733 bzero(&xdrres, sizeof (umntres)); 734 error = auto_calldaemon(fngp->fng_zoneid, 735 AUTOFS_UNMOUNT, 736 xdr_umntrequest, 737 (void *)ul, 738 xdr_umntres, 739 (void *)&xdrres, 740 sizeof (umntres), 741 hard); 742 743 if (!error) 744 error = xdrres.status; 745 746 AUTOFS_DPRINT((5, "\tauto_send_unmount_request: error=%d\n", error)); 747 748 return (error); 749 } 750 751 static int 752 auto_perform_link(fnnode_t *fnp, struct linka *linkp, cred_t *cred) 753 { 754 vnode_t *vp; 755 size_t len; 756 char *tmp; 757 758 AUTOFS_DPRINT((3, "auto_perform_link: fnp=%p dir=%s link=%s\n", 759 (void *)fnp, linkp->dir, linkp->link)); 760 761 len = strlen(linkp->link) + 1; /* include '\0' */ 762 tmp = kmem_zalloc(len, KM_SLEEP); 763 (void) kcopy(linkp->link, tmp, len); 764 mutex_enter(&fnp->fn_lock); 765 fnp->fn_symlink = tmp; 766 fnp->fn_symlinklen = (uint_t)len; 767 fnp->fn_flags |= MF_THISUID_MATCH_RQD; 768 crhold(cred); 769 fnp->fn_cred = cred; 770 mutex_exit(&fnp->fn_lock); 771 772 vp = fntovn(fnp); 773 vp->v_type = VLNK; 774 775 return (0); 776 } 777 778 static void 779 auto_free_autofs_args(struct mounta *m) 780 { 781 autofs_args *aargs = (autofs_args *)m->dataptr; 782 783 if (aargs->addr.buf) 784 kmem_free(aargs->addr.buf, aargs->addr.len); 785 if (aargs->path) 786 kmem_free(aargs->path, strlen(aargs->path) + 1); 787 if (aargs->opts) 788 kmem_free(aargs->opts, strlen(aargs->opts) + 1); 789 if (aargs->map) 790 kmem_free(aargs->map, strlen(aargs->map) + 1); 791 if (aargs->subdir) 792 kmem_free(aargs->subdir, strlen(aargs->subdir) + 1); 793 if (aargs->key) 794 kmem_free(aargs->key, strlen(aargs->key) + 1); 795 kmem_free(aargs, sizeof (*aargs)); 796 } 797 798 static void 799 auto_free_action_list(action_list *alp) 800 { 801 struct mounta *m; 802 action_list *lastalp; 803 char *fstype; 804 805 m = &alp->action.action_list_entry_u.mounta; 806 while (alp != NULL) { 807 fstype = alp->action.action_list_entry_u.mounta.fstype; 808 m = &alp->action.action_list_entry_u.mounta; 809 if (m->dataptr) { 810 if (strcmp(fstype, "autofs") == 0) { 811 auto_free_autofs_args(m); 812 } 813 } 814 if (m->spec) 815 kmem_free(m->spec, strlen(m->spec) + 1); 816 if (m->dir) 817 kmem_free(m->dir, strlen(m->dir) + 1); 818 if (m->fstype) 819 kmem_free(m->fstype, strlen(m->fstype) + 1); 820 if (m->optptr) 821 kmem_free(m->optptr, m->optlen); 822 lastalp = alp; 823 alp = alp->next; 824 kmem_free(lastalp, sizeof (*lastalp)); 825 } 826 } 827 828 static boolean_t 829 auto_invalid_autofs(fninfo_t *dfnip, fnnode_t *dfnp, action_list *p) 830 { 831 struct mounta *m; 832 struct autofs_args *argsp; 833 vnode_t *dvp; 834 char buff[AUTOFS_MAXPATHLEN]; 835 size_t len; 836 struct autofs_globals *fngp; 837 838 fngp = dfnp->fn_globals; 839 dvp = fntovn(dfnp); 840 841 m = &p->action.action_list_entry_u.mounta; 842 /* 843 * Make sure we aren't geting passed NULL values or a "dir" that 844 * isn't "." and doesn't begin with "./". 845 * 846 * We also only want to perform autofs mounts, so make sure 847 * no-one is trying to trick us into doing anything else. 848 */ 849 if (m->spec == NULL || m->dir == NULL || m->dir[0] != '.' || 850 (m->dir[1] != '/' && m->dir[1] != '\0') || 851 m->fstype == NULL || strcmp(m->fstype, "autofs") != 0 || 852 m->dataptr == NULL || m->datalen != sizeof (struct autofs_args) || 853 m->optptr == NULL) 854 return (B_TRUE); 855 /* 856 * We also don't like ".."s in the pathname. Symlinks are 857 * handled by the fact that we'll use NOFOLLOW when we do 858 * lookup()s. 859 */ 860 if (strstr(m->dir, "/../") != NULL || 861 (len = strlen(m->dir)) > sizeof ("/..") - 1 && 862 m->dir[len] == '.' && m->dir[len - 1] == '.' && 863 m->dir[len - 2] == '/') 864 return (B_TRUE); 865 argsp = (struct autofs_args *)m->dataptr; 866 /* 867 * We don't want NULL values here either. 868 */ 869 if (argsp->addr.buf == NULL || argsp->path == NULL || 870 argsp->opts == NULL || argsp->map == NULL || argsp->subdir == NULL) 871 return (B_TRUE); 872 /* 873 * We know what the claimed pathname *should* look like: 874 * 875 * If the parent (dfnp) is a mount point (VROOT), then 876 * the path should be (dfnip->fi_path + m->dir). 877 * 878 * Else, we know we're only two levels deep, so we use 879 * (dfnip->fi_path + dfnp->fn_name + m->dir). 880 * 881 * Furthermore, "." only makes sense if dfnp is a 882 * trigger node. 883 * 884 * At this point it seems like the passed-in path is 885 * redundant. 886 */ 887 if (dvp->v_flag & VROOT) { 888 if (m->dir[1] == '\0' && !(dfnp->fn_flags & MF_TRIGGER)) 889 return (B_TRUE); 890 (void) snprintf(buff, sizeof (buff), "%s%s", 891 dfnip->fi_path, m->dir + 1); 892 } else { 893 (void) snprintf(buff, sizeof (buff), "%s/%s%s", 894 dfnip->fi_path, dfnp->fn_name, m->dir + 1); 895 } 896 if (strcmp(argsp->path, buff) != 0) { 897 auto_log(fngp->fng_verbose, fngp->fng_zoneid, 898 CE_WARN, "autofs: expected path of '%s', " 899 "got '%s' instead.", buff, argsp->path); 900 return (B_TRUE); 901 } 902 return (B_FALSE); /* looks OK */ 903 } 904 905 /* 906 * auto_invalid_action will validate the action_list received. If all is good 907 * this function returns FALSE, if there is a problem it returns TRUE. 908 */ 909 static boolean_t 910 auto_invalid_action(fninfo_t *dfnip, fnnode_t *dfnp, action_list *alistpp) 911 { 912 913 /* 914 * Before we go any further, this better be a mount request. 915 */ 916 if (alistpp->action.action != AUTOFS_MOUNT_RQ) 917 return (B_TRUE); 918 return (auto_invalid_autofs(dfnip, dfnp, alistpp)); 919 920 } 921 922 static int 923 auto_perform_actions( 924 fninfo_t *dfnip, 925 fnnode_t *dfnp, 926 action_list *alp, 927 cred_t *cred) /* Credentials of the caller */ 928 { 929 930 action_list *p; 931 struct mounta *m, margs; 932 struct autofs_args *argsp; 933 int error, success = 0; 934 vnode_t *mvp, *dvp, *newvp; 935 fnnode_t *newfnp, *mfnp; 936 int auto_mount = 0; 937 int save_triggers = 0; 938 int update_times = 0; 939 char *mntpnt; 940 char buff[AUTOFS_MAXPATHLEN]; 941 timestruc_t now; 942 struct autofs_globals *fngp; 943 cred_t *zcred; 944 945 AUTOFS_DPRINT((4, "auto_perform_actions: alp=%p\n", 946 (void *)alp)); 947 948 fngp = dfnp->fn_globals; 949 dvp = fntovn(dfnp); 950 951 /* 952 * As automountd running in a zone may be compromised, and this may be 953 * an attack, we can't trust everything passed in by automountd, and we 954 * need to do argument verification. We'll issue a warning and drop 955 * the request if it doesn't seem right. 956 */ 957 958 for (p = alp; p != NULL; p = p->next) { 959 if (auto_invalid_action(dfnip, dfnp, p)) { 960 /* 961 * This warning should be sent to the global zone, 962 * since presumably the zone administrator is the same 963 * as the attacker. 964 */ 965 cmn_err(CE_WARN, "autofs: invalid action list received " 966 "by automountd in zone %s.", 967 curproc->p_zone->zone_name); 968 /* 969 * This conversation is over. 970 */ 971 xdr_free(xdr_action_list, (char *)alp); 972 return (EINVAL); 973 } 974 } 975 976 zcred = zone_get_kcred(getzoneid()); 977 ASSERT(zcred != NULL); 978 979 if (vn_mountedvfs(dvp) != NULL) { 980 /* 981 * The daemon successfully mounted a filesystem 982 * on the AUTOFS root node. 983 */ 984 mutex_enter(&dfnp->fn_lock); 985 dfnp->fn_flags |= MF_MOUNTPOINT; 986 ASSERT(dfnp->fn_dirents == NULL); 987 mutex_exit(&dfnp->fn_lock); 988 success++; 989 } else { 990 /* 991 * Clear MF_MOUNTPOINT. 992 */ 993 mutex_enter(&dfnp->fn_lock); 994 if (dfnp->fn_flags & MF_MOUNTPOINT) { 995 AUTOFS_DPRINT((10, "autofs: clearing mountpoint " 996 "flag on %s.", dfnp->fn_name)); 997 ASSERT(dfnp->fn_dirents == NULL); 998 ASSERT(dfnp->fn_trigger == NULL); 999 } 1000 dfnp->fn_flags &= ~MF_MOUNTPOINT; 1001 mutex_exit(&dfnp->fn_lock); 1002 } 1003 1004 for (p = alp; p != NULL; p = p->next) { 1005 1006 vfs_t *vfsp; /* dummy argument */ 1007 vfs_t *mvfsp; 1008 1009 auto_mount = 0; 1010 1011 m = &p->action.action_list_entry_u.mounta; 1012 argsp = (struct autofs_args *)m->dataptr; 1013 ASSERT(strcmp(m->fstype, "autofs") == 0); 1014 /* 1015 * use the parent directory's timeout since it's the 1016 * one specified/inherited by automount. 1017 */ 1018 argsp->mount_to = dfnip->fi_mount_to; 1019 /* 1020 * The mountpoint is relative, and it is guaranteed to 1021 * begin with "." 1022 * 1023 */ 1024 ASSERT(m->dir[0] == '.'); 1025 if (m->dir[0] == '.' && m->dir[1] == '\0') { 1026 /* 1027 * mounting on the trigger node 1028 */ 1029 mvp = dvp; 1030 VN_HOLD(mvp); 1031 goto mount; 1032 } 1033 /* 1034 * ignore "./" in front of mountpoint 1035 */ 1036 ASSERT(m->dir[1] == '/'); 1037 mntpnt = m->dir + 2; 1038 1039 AUTOFS_DPRINT((10, "\tdfnip->fi_path=%s\n", dfnip->fi_path)); 1040 AUTOFS_DPRINT((10, "\tdfnip->fi_flags=%x\n", dfnip->fi_flags)); 1041 AUTOFS_DPRINT((10, "\tmntpnt=%s\n", mntpnt)); 1042 1043 if (dfnip->fi_flags & MF_DIRECT) { 1044 AUTOFS_DPRINT((10, "\tDIRECT\n")); 1045 (void) sprintf(buff, "%s/%s", dfnip->fi_path, 1046 mntpnt); 1047 } else { 1048 AUTOFS_DPRINT((10, "\tINDIRECT\n")); 1049 (void) sprintf(buff, "%s/%s/%s", 1050 dfnip->fi_path, 1051 dfnp->fn_name, mntpnt); 1052 } 1053 1054 if (vn_mountedvfs(dvp) == NULL) { 1055 /* 1056 * Daemon didn't mount anything on the root 1057 * We have to create the mountpoint if it 1058 * doesn't exist already 1059 * 1060 * We use the caller's credentials in case a 1061 * UID-match is required 1062 * (MF_THISUID_MATCH_RQD). 1063 */ 1064 rw_enter(&dfnp->fn_rwlock, RW_WRITER); 1065 error = auto_search(dfnp, mntpnt, &mfnp, cred); 1066 if (error == 0) { 1067 /* 1068 * AUTOFS mountpoint exists 1069 */ 1070 if (vn_mountedvfs(fntovn(mfnp)) != NULL) { 1071 cmn_err(CE_PANIC, 1072 "auto_perform_actions:" 1073 " mfnp=%p covered", 1074 (void *)mfnp); 1075 } 1076 } else { 1077 /* 1078 * Create AUTOFS mountpoint 1079 */ 1080 ASSERT((dfnp->fn_flags & MF_MOUNTPOINT) == 0); 1081 error = auto_enter(dfnp, mntpnt, &mfnp, cred); 1082 ASSERT(mfnp->fn_linkcnt == 1); 1083 mfnp->fn_linkcnt++; 1084 } 1085 if (!error) 1086 update_times = 1; 1087 rw_exit(&dfnp->fn_rwlock); 1088 ASSERT(error != EEXIST); 1089 if (!error) { 1090 /* 1091 * mfnp is already held. 1092 */ 1093 mvp = fntovn(mfnp); 1094 } else { 1095 auto_log(fngp->fng_verbose, fngp->fng_zoneid, 1096 CE_WARN, "autofs: mount of %s " 1097 "failed - can't create" 1098 " mountpoint.", buff); 1099 continue; 1100 } 1101 } else { 1102 /* 1103 * Find mountpoint in VFS mounted here. If not 1104 * found, fail the submount, though the overall 1105 * mount has succeeded since the root is 1106 * mounted. 1107 */ 1108 if (error = auto_getmntpnt(dvp, mntpnt, &mvp, 1109 kcred)) { 1110 auto_log(fngp->fng_verbose, 1111 fngp->fng_zoneid, 1112 CE_WARN, "autofs: mount of %s " 1113 "failed - mountpoint doesn't" 1114 " exist.", buff); 1115 continue; 1116 } 1117 if (mvp->v_type == VLNK) { 1118 auto_log(fngp->fng_verbose, 1119 fngp->fng_zoneid, 1120 CE_WARN, "autofs: %s symbolic " 1121 "link: not a valid mountpoint " 1122 "- mount failed", buff); 1123 VN_RELE(mvp); 1124 error = ENOENT; 1125 continue; 1126 } 1127 } 1128 mount: 1129 m->flags |= MS_SYSSPACE | MS_OPTIONSTR; 1130 1131 /* 1132 * Copy mounta struct here so we can substitute a 1133 * buffer that is large enough to hold the returned 1134 * option string, if that string is longer than the 1135 * input option string. 1136 * This can happen if there are default options enabled 1137 * that were not in the input option string. 1138 */ 1139 bcopy(m, &margs, sizeof (*m)); 1140 margs.optptr = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP); 1141 margs.optlen = MAX_MNTOPT_STR; 1142 (void) strcpy(margs.optptr, m->optptr); 1143 margs.dir = argsp->path; 1144 1145 /* 1146 * We use the zone's kcred because we don't want the 1147 * zone to be able to thus do something it wouldn't 1148 * normally be able to. 1149 */ 1150 error = domount(NULL, &margs, mvp, zcred, &vfsp); 1151 kmem_free(margs.optptr, MAX_MNTOPT_STR); 1152 if (error != 0) { 1153 auto_log(fngp->fng_verbose, fngp->fng_zoneid, 1154 CE_WARN, "autofs: domount of %s failed " 1155 "error=%d", buff, error); 1156 VN_RELE(mvp); 1157 continue; 1158 } 1159 VFS_RELE(vfsp); 1160 1161 /* 1162 * If mountpoint is an AUTOFS node, then I'm going to 1163 * flag it that the Filesystem mounted on top was 1164 * mounted in the kernel so that the unmount can be 1165 * done inside the kernel as well. 1166 * I don't care to flag non-AUTOFS mountpoints when an 1167 * AUTOFS in-kernel mount was done on top, because the 1168 * unmount routine already knows that such case was 1169 * done in the kernel. 1170 */ 1171 if (vfs_matchops(dvp->v_vfsp, 1172 vfs_getops(mvp->v_vfsp))) { 1173 mfnp = vntofn(mvp); 1174 mutex_enter(&mfnp->fn_lock); 1175 mfnp->fn_flags |= MF_IK_MOUNT; 1176 mutex_exit(&mfnp->fn_lock); 1177 } 1178 1179 (void) vn_vfswlock_wait(mvp); 1180 mvfsp = vn_mountedvfs(mvp); 1181 if (mvfsp != NULL) { 1182 vfs_lock_wait(mvfsp); 1183 vn_vfsunlock(mvp); 1184 error = VFS_ROOT(mvfsp, &newvp); 1185 vfs_unlock(mvfsp); 1186 if (error) { 1187 /* 1188 * We've dropped the locks, so let's 1189 * get the mounted vfs again in case 1190 * it changed. 1191 */ 1192 (void) vn_vfswlock_wait(mvp); 1193 mvfsp = vn_mountedvfs(mvp); 1194 if (mvfsp != NULL) { 1195 error = dounmount(mvfsp, 0, CRED()); 1196 if (error) { 1197 cmn_err(CE_WARN, 1198 "autofs: could" 1199 " not unmount" 1200 " vfs=%p", 1201 (void *)mvfsp); 1202 } 1203 } else 1204 vn_vfsunlock(mvp); 1205 VN_RELE(mvp); 1206 continue; 1207 } 1208 } else { 1209 vn_vfsunlock(mvp); 1210 VN_RELE(mvp); 1211 continue; 1212 } 1213 1214 auto_mount = vfs_matchops(dvp->v_vfsp, 1215 vfs_getops(newvp->v_vfsp)); 1216 newfnp = vntofn(newvp); 1217 newfnp->fn_parent = dfnp; 1218 1219 /* 1220 * At this time we want to save the AUTOFS filesystem 1221 * as a trigger node. (We only do this if the mount 1222 * occurred on a node different from the root. 1223 * We look at the trigger nodes during 1224 * the automatic unmounting to make sure we remove them 1225 * as a unit and remount them as a unit if the 1226 * filesystem mounted at the root could not be 1227 * unmounted. 1228 */ 1229 if (auto_mount && (error == 0) && (mvp != dvp)) { 1230 save_triggers++; 1231 /* 1232 * Add AUTOFS mount to hierarchy 1233 */ 1234 newfnp->fn_flags |= MF_TRIGGER; 1235 rw_enter(&newfnp->fn_rwlock, RW_WRITER); 1236 newfnp->fn_next = dfnp->fn_trigger; 1237 rw_exit(&newfnp->fn_rwlock); 1238 rw_enter(&dfnp->fn_rwlock, RW_WRITER); 1239 dfnp->fn_trigger = newfnp; 1240 rw_exit(&dfnp->fn_rwlock); 1241 /* 1242 * Don't VN_RELE(newvp) here since dfnp now 1243 * holds reference to it as its trigger node. 1244 */ 1245 AUTOFS_DPRINT((10, "\tadding trigger %s to %s\n", 1246 newfnp->fn_name, dfnp->fn_name)); 1247 AUTOFS_DPRINT((10, "\tfirst trigger is %s\n", 1248 dfnp->fn_trigger->fn_name)); 1249 if (newfnp->fn_next != NULL) 1250 AUTOFS_DPRINT((10, 1251 "\tnext trigger is %s\n", 1252 newfnp->fn_next->fn_name)); 1253 else 1254 AUTOFS_DPRINT((10, 1255 "\tno next trigger\n")); 1256 } else 1257 VN_RELE(newvp); 1258 1259 if (!error) 1260 success++; 1261 1262 if (update_times) { 1263 gethrestime(&now); 1264 dfnp->fn_atime = dfnp->fn_mtime = now; 1265 } 1266 1267 VN_RELE(mvp); 1268 } 1269 1270 if (save_triggers) { 1271 /* 1272 * Make sure the parent can't be freed while it has triggers. 1273 */ 1274 VN_HOLD(dvp); 1275 } 1276 1277 crfree(zcred); 1278 1279 done: 1280 /* 1281 * Return failure if daemon didn't mount anything, and all 1282 * kernel mounts attempted failed. 1283 */ 1284 error = success ? 0 : ENOENT; 1285 1286 if (alp != NULL) { 1287 if ((error == 0) && save_triggers) { 1288 /* 1289 * Save action_list information, so that we can use it 1290 * when it comes time to remount the trigger nodes 1291 * The action list is freed when the directory node 1292 * containing the reference to it is unmounted in 1293 * unmount_tree(). 1294 */ 1295 mutex_enter(&dfnp->fn_lock); 1296 ASSERT(dfnp->fn_alp == NULL); 1297 dfnp->fn_alp = alp; 1298 mutex_exit(&dfnp->fn_lock); 1299 } else { 1300 /* 1301 * free the action list now, 1302 */ 1303 xdr_free(xdr_action_list, (char *)alp); 1304 } 1305 } 1306 AUTOFS_DPRINT((5, "auto_perform_actions: error=%d\n", error)); 1307 return (error); 1308 } 1309 1310 fnnode_t * 1311 auto_makefnnode( 1312 vtype_t type, 1313 vfs_t *vfsp, 1314 char *name, 1315 cred_t *cred, 1316 struct autofs_globals *fngp) 1317 { 1318 fnnode_t *fnp; 1319 vnode_t *vp; 1320 char *tmpname; 1321 timestruc_t now; 1322 /* 1323 * autofs uses odd inode numbers 1324 * automountd uses even inode numbers 1325 * 1326 * To preserve the age-old semantics that inum+devid is unique across 1327 * the system, this variable must be global across zones. 1328 */ 1329 static ino_t nodeid = 3; 1330 1331 fnp = kmem_zalloc(sizeof (*fnp), KM_SLEEP); 1332 fnp->fn_vnode = vn_alloc(KM_SLEEP); 1333 1334 vp = fntovn(fnp); 1335 tmpname = kmem_alloc(strlen(name) + 1, KM_SLEEP); 1336 (void) strcpy(tmpname, name); 1337 fnp->fn_name = &tmpname[0]; 1338 fnp->fn_namelen = (int)strlen(tmpname) + 1; /* include '\0' */ 1339 fnp->fn_uid = crgetuid(cred); 1340 fnp->fn_gid = crgetgid(cred); 1341 /* 1342 * ".." is added in auto_enter and auto_mount. 1343 * "." is added in auto_mkdir and auto_mount. 1344 */ 1345 /* 1346 * Note that fn_size and fn_linkcnt are already 0 since 1347 * we used kmem_zalloc to allocated fnp 1348 */ 1349 fnp->fn_mode = AUTOFS_MODE; 1350 gethrestime(&now); 1351 fnp->fn_atime = fnp->fn_mtime = fnp->fn_ctime = now; 1352 fnp->fn_ref_time = now.tv_sec; 1353 mutex_enter(&autofs_nodeid_lock); 1354 fnp->fn_nodeid = nodeid; 1355 nodeid += 2; 1356 fnp->fn_globals = fngp; 1357 fngp->fng_fnnode_count++; 1358 mutex_exit(&autofs_nodeid_lock); 1359 vn_setops(vp, auto_vnodeops); 1360 vp->v_type = type; 1361 vp->v_data = (void *)fnp; 1362 vp->v_vfsp = vfsp; 1363 mutex_init(&fnp->fn_lock, NULL, MUTEX_DEFAULT, NULL); 1364 rw_init(&fnp->fn_rwlock, NULL, RW_DEFAULT, NULL); 1365 cv_init(&fnp->fn_cv_mount, NULL, CV_DEFAULT, NULL); 1366 vn_exists(vp); 1367 return (fnp); 1368 } 1369 1370 1371 void 1372 auto_freefnnode(fnnode_t *fnp) 1373 { 1374 vnode_t *vp = fntovn(fnp); 1375 1376 AUTOFS_DPRINT((4, "auto_freefnnode: fnp=%p\n", (void *)fnp)); 1377 1378 ASSERT(fnp->fn_linkcnt == 0); 1379 ASSERT(vp->v_count == 0); 1380 ASSERT(fnp->fn_dirents == NULL); 1381 ASSERT(fnp->fn_parent == NULL); 1382 1383 vn_invalid(vp); 1384 kmem_free(fnp->fn_name, fnp->fn_namelen); 1385 if (fnp->fn_symlink) { 1386 ASSERT(fnp->fn_flags & MF_THISUID_MATCH_RQD); 1387 kmem_free(fnp->fn_symlink, fnp->fn_symlinklen); 1388 } 1389 if (fnp->fn_cred) 1390 crfree(fnp->fn_cred); 1391 mutex_destroy(&fnp->fn_lock); 1392 rw_destroy(&fnp->fn_rwlock); 1393 cv_destroy(&fnp->fn_cv_mount); 1394 vn_free(vp); 1395 1396 mutex_enter(&autofs_nodeid_lock); 1397 fnp->fn_globals->fng_fnnode_count--; 1398 mutex_exit(&autofs_nodeid_lock); 1399 kmem_free(fnp, sizeof (*fnp)); 1400 } 1401 1402 void 1403 auto_disconnect( 1404 fnnode_t *dfnp, 1405 fnnode_t *fnp) 1406 { 1407 fnnode_t *tmp, **fnpp; 1408 vnode_t *vp = fntovn(fnp); 1409 timestruc_t now; 1410 1411 AUTOFS_DPRINT((4, 1412 "auto_disconnect: dfnp=%p fnp=%p linkcnt=%d\n v_count=%d", 1413 (void *)dfnp, (void *)fnp, fnp->fn_linkcnt, vp->v_count)); 1414 1415 ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock)); 1416 ASSERT(fnp->fn_linkcnt == 1); 1417 1418 if (vn_mountedvfs(vp) != NULL) { 1419 cmn_err(CE_PANIC, "auto_disconnect: vp %p mounted on", 1420 (void *)vp); 1421 } 1422 1423 /* 1424 * Decrement by 1 because we're removing the entry in dfnp. 1425 */ 1426 fnp->fn_linkcnt--; 1427 fnp->fn_size--; 1428 1429 /* 1430 * only changed while holding parent's (dfnp) rw_lock 1431 */ 1432 fnp->fn_parent = NULL; 1433 1434 fnpp = &dfnp->fn_dirents; 1435 for (;;) { 1436 tmp = *fnpp; 1437 if (tmp == NULL) { 1438 cmn_err(CE_PANIC, 1439 "auto_disconnect: %p not in %p dirent list", 1440 (void *)fnp, (void *)dfnp); 1441 } 1442 if (tmp == fnp) { 1443 *fnpp = tmp->fn_next; /* remove it from the list */ 1444 ASSERT(vp->v_count == 0); 1445 /* child had a pointer to parent ".." */ 1446 dfnp->fn_linkcnt--; 1447 dfnp->fn_size--; 1448 break; 1449 } 1450 fnpp = &tmp->fn_next; 1451 } 1452 1453 mutex_enter(&fnp->fn_lock); 1454 gethrestime(&now); 1455 fnp->fn_atime = fnp->fn_mtime = now; 1456 mutex_exit(&fnp->fn_lock); 1457 1458 AUTOFS_DPRINT((5, "auto_disconnect: done\n")); 1459 } 1460 1461 int 1462 auto_enter(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred) 1463 { 1464 struct fnnode *cfnp, **spp; 1465 vnode_t *dvp = fntovn(dfnp); 1466 ushort_t offset = 0; 1467 ushort_t diff; 1468 1469 AUTOFS_DPRINT((4, "auto_enter: dfnp=%p, name=%s ", (void *)dfnp, name)); 1470 1471 ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock)); 1472 1473 cfnp = dfnp->fn_dirents; 1474 if (cfnp == NULL) { 1475 /* 1476 * offset = 0 for '.' and offset = 1 for '..' 1477 */ 1478 spp = &dfnp->fn_dirents; 1479 offset = 2; 1480 } 1481 1482 for (; cfnp; cfnp = cfnp->fn_next) { 1483 if (strcmp(cfnp->fn_name, name) == 0) { 1484 mutex_enter(&cfnp->fn_lock); 1485 if (cfnp->fn_flags & MF_THISUID_MATCH_RQD) { 1486 /* 1487 * "thisuser" kind of node, need to 1488 * match CREDs as well 1489 */ 1490 mutex_exit(&cfnp->fn_lock); 1491 if (crcmp(cfnp->fn_cred, cred) == 0) 1492 return (EEXIST); 1493 } else { 1494 mutex_exit(&cfnp->fn_lock); 1495 return (EEXIST); 1496 } 1497 } 1498 1499 if (cfnp->fn_next != NULL) { 1500 diff = (ushort_t) 1501 (cfnp->fn_next->fn_offset - cfnp->fn_offset); 1502 ASSERT(diff != 0); 1503 if (diff > 1 && offset == 0) { 1504 offset = (ushort_t)cfnp->fn_offset + 1; 1505 spp = &cfnp->fn_next; 1506 } 1507 } else if (offset == 0) { 1508 offset = (ushort_t)cfnp->fn_offset + 1; 1509 spp = &cfnp->fn_next; 1510 } 1511 } 1512 1513 *fnpp = auto_makefnnode(VDIR, dvp->v_vfsp, name, cred, 1514 dfnp->fn_globals); 1515 if (*fnpp == NULL) 1516 return (ENOMEM); 1517 1518 /* 1519 * I don't hold the mutex on fnpp because I created it, and 1520 * I'm already holding the writers lock for it's parent 1521 * directory, therefore nobody can reference it without me first 1522 * releasing the writers lock. 1523 */ 1524 (*fnpp)->fn_offset = offset; 1525 (*fnpp)->fn_next = *spp; 1526 *spp = *fnpp; 1527 (*fnpp)->fn_parent = dfnp; 1528 (*fnpp)->fn_linkcnt++; /* parent now holds reference to entry */ 1529 (*fnpp)->fn_size++; 1530 1531 /* 1532 * dfnp->fn_linkcnt and dfnp->fn_size protected by dfnp->rw_lock 1533 */ 1534 dfnp->fn_linkcnt++; /* child now holds reference to parent '..' */ 1535 dfnp->fn_size++; 1536 1537 dfnp->fn_ref_time = gethrestime_sec(); 1538 1539 AUTOFS_DPRINT((5, "*fnpp=%p\n", (void *)*fnpp)); 1540 return (0); 1541 } 1542 1543 int 1544 auto_search(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred) 1545 { 1546 vnode_t *dvp; 1547 fnnode_t *p; 1548 int error = ENOENT, match = 0; 1549 1550 AUTOFS_DPRINT((4, "auto_search: dfnp=%p, name=%s...\n", 1551 (void *)dfnp, name)); 1552 1553 dvp = fntovn(dfnp); 1554 if (dvp->v_type != VDIR) { 1555 cmn_err(CE_PANIC, "auto_search: dvp=%p not a directory", 1556 (void *)dvp); 1557 } 1558 1559 ASSERT(RW_LOCK_HELD(&dfnp->fn_rwlock)); 1560 for (p = dfnp->fn_dirents; p != NULL; p = p->fn_next) { 1561 if (strcmp(p->fn_name, name) == 0) { 1562 mutex_enter(&p->fn_lock); 1563 if (p->fn_flags & MF_THISUID_MATCH_RQD) { 1564 /* 1565 * "thisuser" kind of node 1566 * Need to match CREDs as well 1567 */ 1568 mutex_exit(&p->fn_lock); 1569 match = crcmp(p->fn_cred, cred) == 0; 1570 } else { 1571 /* 1572 * No need to check CRED 1573 */ 1574 mutex_exit(&p->fn_lock); 1575 match = 1; 1576 } 1577 } 1578 if (match) { 1579 error = 0; 1580 if (fnpp) { 1581 *fnpp = p; 1582 VN_HOLD(fntovn(*fnpp)); 1583 } 1584 break; 1585 } 1586 } 1587 1588 AUTOFS_DPRINT((5, "auto_search: error=%d\n", error)); 1589 return (error); 1590 } 1591 1592 /* 1593 * If dvp is mounted on, get path's vnode in the mounted on 1594 * filesystem. Path is relative to dvp, ie "./path". 1595 * If successful, *mvp points to a the held mountpoint vnode. 1596 */ 1597 /* ARGSUSED */ 1598 static int 1599 auto_getmntpnt( 1600 vnode_t *dvp, 1601 char *path, 1602 vnode_t **mvpp, /* vnode for mountpoint */ 1603 cred_t *cred) 1604 { 1605 int error = 0; 1606 vnode_t *newvp; 1607 char namebuf[TYPICALMAXPATHLEN]; 1608 struct pathname lookpn; 1609 vfs_t *vfsp; 1610 1611 AUTOFS_DPRINT((4, "auto_getmntpnt: path=%s\n", path)); 1612 1613 if (error = vn_vfsrlock_wait(dvp)) 1614 return (error); 1615 1616 /* 1617 * Now that we have the vfswlock, check to see if dvp 1618 * is still mounted on. If not, then just bail out as 1619 * there is no need to remount the triggers since the 1620 * higher level mount point has gotten unmounted. 1621 */ 1622 vfsp = vn_mountedvfs(dvp); 1623 if (vfsp == NULL) { 1624 vn_vfsunlock(dvp); 1625 error = EBUSY; 1626 goto done; 1627 } 1628 /* 1629 * Since mounted on, lookup "path" in the new filesystem, 1630 * it is important that we do the filesystem jump here to 1631 * avoid lookuppn() calling auto_lookup on dvp and deadlock. 1632 */ 1633 error = VFS_ROOT(vfsp, &newvp); 1634 vn_vfsunlock(dvp); 1635 if (error) 1636 goto done; 1637 1638 /* 1639 * We do a VN_HOLD on newvp just in case the first call to 1640 * lookuppnvp() fails with ENAMETOOLONG. We should still have a 1641 * reference to this vnode for the second call to lookuppnvp(). 1642 */ 1643 VN_HOLD(newvp); 1644 1645 /* 1646 * Now create the pathname struct so we can make use of lookuppnvp, 1647 * and pn_getcomponent. 1648 * This code is similar to lookupname() in fs/lookup.c. 1649 */ 1650 error = pn_get_buf(path, UIO_SYSSPACE, &lookpn, 1651 namebuf, sizeof (namebuf)); 1652 if (error == 0) { 1653 error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP, 1654 mvpp, rootdir, newvp, cred); 1655 } else 1656 VN_RELE(newvp); 1657 if (error == ENAMETOOLONG) { 1658 /* 1659 * This thread used a pathname > TYPICALMAXPATHLEN bytes long. 1660 * newvp is VN_RELE'd by this call to lookuppnvp. 1661 * 1662 * Using 'rootdir' in a zone's context is OK here: we already 1663 * ascertained that there are no '..'s in the path, and we're 1664 * not following symlinks. 1665 */ 1666 if ((error = pn_get(path, UIO_SYSSPACE, &lookpn)) == 0) { 1667 error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP, 1668 mvpp, rootdir, newvp, cred); 1669 pn_free(&lookpn); 1670 } else 1671 VN_RELE(newvp); 1672 } else { 1673 /* 1674 * Need to release newvp here since we held it. 1675 */ 1676 VN_RELE(newvp); 1677 } 1678 1679 done: 1680 AUTOFS_DPRINT((5, "auto_getmntpnt: path=%s *mvpp=%p error=%d\n", 1681 path, (void *)*mvpp, error)); 1682 return (error); 1683 } 1684 1685 #define DEEPER(x) (((x)->fn_dirents != NULL) || \ 1686 (vn_mountedvfs(fntovn((x)))) != NULL) 1687 1688 /* 1689 * The caller, should have already VN_RELE'd its reference to the 1690 * root vnode of this filesystem. 1691 */ 1692 static int 1693 auto_inkernel_unmount(vfs_t *vfsp) 1694 { 1695 vnode_t *cvp = vfsp->vfs_vnodecovered; 1696 int error; 1697 1698 AUTOFS_DPRINT((4, 1699 "auto_inkernel_unmount: devid=%lx mntpnt(%p) count %u\n", 1700 vfsp->vfs_dev, (void *)cvp, cvp->v_count)); 1701 1702 ASSERT(vn_vfswlock_held(cvp)); 1703 1704 /* 1705 * Perform the unmount 1706 * The mountpoint has already been locked by the caller. 1707 */ 1708 error = dounmount(vfsp, 0, kcred); 1709 1710 AUTOFS_DPRINT((5, "auto_inkernel_unmount: exit count %u\n", 1711 cvp->v_count)); 1712 return (error); 1713 } 1714 1715 /* 1716 * unmounts trigger nodes in the kernel. 1717 */ 1718 static void 1719 unmount_triggers(fnnode_t *fnp, action_list **alp) 1720 { 1721 fnnode_t *tp, *next; 1722 int error = 0; 1723 vfs_t *vfsp; 1724 vnode_t *tvp; 1725 1726 AUTOFS_DPRINT((4, "unmount_triggers: fnp=%p\n", (void *)fnp)); 1727 ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock)); 1728 1729 *alp = fnp->fn_alp; 1730 next = fnp->fn_trigger; 1731 while ((tp = next) != NULL) { 1732 tvp = fntovn(tp); 1733 ASSERT(tvp->v_count >= 2); 1734 next = tp->fn_next; 1735 /* 1736 * drop writer's lock since the unmount will end up 1737 * disconnecting this node from fnp and needs to acquire 1738 * the writer's lock again. 1739 * next has at least a reference count >= 2 since it's 1740 * a trigger node, therefore can not be accidentally freed 1741 * by a VN_RELE 1742 */ 1743 rw_exit(&fnp->fn_rwlock); 1744 1745 vfsp = tvp->v_vfsp; 1746 1747 /* 1748 * Its parent was holding a reference to it, since this 1749 * is a trigger vnode. 1750 */ 1751 VN_RELE(tvp); 1752 if (error = auto_inkernel_unmount(vfsp)) { 1753 cmn_err(CE_PANIC, "unmount_triggers: " 1754 "unmount of vp=%p failed error=%d", 1755 (void *)tvp, error); 1756 } 1757 /* 1758 * reacquire writer's lock 1759 */ 1760 rw_enter(&fnp->fn_rwlock, RW_WRITER); 1761 } 1762 1763 /* 1764 * We were holding a reference to our parent. Drop that. 1765 */ 1766 VN_RELE(fntovn(fnp)); 1767 fnp->fn_trigger = NULL; 1768 fnp->fn_alp = NULL; 1769 1770 AUTOFS_DPRINT((5, "unmount_triggers: finished\n")); 1771 } 1772 1773 /* 1774 * This routine locks the mountpoint of every trigger node if they're 1775 * not busy, or returns EBUSY if any node is busy. If a trigger node should 1776 * be unmounted first, then it sets nfnp to point to it, otherwise nfnp 1777 * points to NULL. 1778 */ 1779 static int 1780 triggers_busy(fnnode_t *fnp, fnnode_t **nfnp) 1781 { 1782 int error = 0, done; 1783 int lck_error = 0; 1784 fnnode_t *tp, *t1p; 1785 vfs_t *vfsp; 1786 1787 ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock)); 1788 1789 *nfnp = NULL; 1790 for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) { 1791 AUTOFS_DPRINT((10, "\ttrigger: %s\n", tp->fn_name)); 1792 vfsp = fntovn(tp)->v_vfsp; 1793 error = 0; 1794 /* 1795 * The vn_vfsunlock will be done in auto_inkernel_unmount. 1796 */ 1797 lck_error = vn_vfswlock(vfsp->vfs_vnodecovered); 1798 if (lck_error == 0) { 1799 mutex_enter(&tp->fn_lock); 1800 ASSERT((tp->fn_flags & MF_LOOKUP) == 0); 1801 if (tp->fn_flags & MF_INPROG) { 1802 /* 1803 * a mount is in progress 1804 */ 1805 error = EBUSY; 1806 } 1807 mutex_exit(&tp->fn_lock); 1808 } 1809 if (lck_error || error || DEEPER(tp) || 1810 ((fntovn(tp))->v_count) > 2) { 1811 /* 1812 * couldn't lock it because it's busy, 1813 * It is mounted on or has dirents? 1814 * If reference count is greater than two, then 1815 * somebody else is holding a reference to this vnode. 1816 * One reference is for the mountpoint, and the second 1817 * is for the trigger node. 1818 */ 1819 AUTOFS_DPRINT((10, "\ttrigger busy\n")); 1820 if ((lck_error == 0) && (error == 0)) { 1821 *nfnp = tp; 1822 /* 1823 * The matching VN_RELE is done in 1824 * unmount_tree(). 1825 */ 1826 VN_HOLD(fntovn(*nfnp)); 1827 } 1828 /* 1829 * Unlock previously locked mountpoints 1830 */ 1831 for (done = 0, t1p = fnp->fn_trigger; !done; 1832 t1p = t1p->fn_next) { 1833 /* 1834 * Unlock all nodes previously 1835 * locked. All nodes up to 'tp' 1836 * were successfully locked. If 'lck_err' is 1837 * set, then 'tp' was not locked, and thus 1838 * should not be unlocked. If 1839 * 'lck_err' is not set, then 'tp' was 1840 * successfully locked, and it should 1841 * be unlocked. 1842 */ 1843 if (t1p != tp || !lck_error) { 1844 vfsp = fntovn(t1p)->v_vfsp; 1845 vn_vfsunlock(vfsp->vfs_vnodecovered); 1846 } 1847 done = (t1p == tp); 1848 } 1849 error = EBUSY; 1850 break; 1851 } 1852 } 1853 1854 AUTOFS_DPRINT((4, "triggers_busy: error=%d\n", error)); 1855 return (error); 1856 } 1857 1858 /* 1859 * Unlock previously locked trigger nodes. 1860 */ 1861 static int 1862 triggers_unlock(fnnode_t *fnp) 1863 { 1864 fnnode_t *tp; 1865 vfs_t *vfsp; 1866 1867 ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock)); 1868 1869 for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) { 1870 AUTOFS_DPRINT((10, "\tunlock trigger: %s\n", tp->fn_name)); 1871 vfsp = fntovn(tp)->v_vfsp; 1872 vn_vfsunlock(vfsp->vfs_vnodecovered); 1873 } 1874 1875 return (0); 1876 } 1877 1878 /* 1879 * It is the caller's responsibility to grab the VVFSLOCK. 1880 * Releases the VVFSLOCK upon return. 1881 */ 1882 static int 1883 unmount_node(vnode_t *cvp, int force) 1884 { 1885 int error = 0; 1886 fnnode_t *cfnp; 1887 vfs_t *vfsp; 1888 umntrequest ul; 1889 fninfo_t *fnip; 1890 1891 AUTOFS_DPRINT((4, "\tunmount_node cvp=%p\n", (void *)cvp)); 1892 1893 ASSERT(vn_vfswlock_held(cvp)); 1894 cfnp = vntofn(cvp); 1895 vfsp = vn_mountedvfs(cvp); 1896 1897 if (force || cfnp->fn_flags & MF_IK_MOUNT) { 1898 /* 1899 * Mount was performed in the kernel, so 1900 * do an in-kernel unmount. auto_inkernel_unmount() 1901 * will vn_vfsunlock(cvp). 1902 */ 1903 error = auto_inkernel_unmount(vfsp); 1904 } else { 1905 zone_t *zone = NULL; 1906 refstr_t *mntpt, *resource; 1907 size_t mntoptslen; 1908 1909 /* 1910 * Get the mnttab information of the node 1911 * and ask the daemon to unmount it. 1912 */ 1913 bzero(&ul, sizeof (ul)); 1914 mntfs_getmntopts(vfsp, &ul.mntopts, &mntoptslen); 1915 if (ul.mntopts == NULL) { 1916 auto_log(cfnp->fn_globals->fng_verbose, 1917 cfnp->fn_globals->fng_zoneid, 1918 CE_WARN, "unmount_node: " 1919 "no memory"); 1920 vn_vfsunlock(cvp); 1921 error = ENOMEM; 1922 goto done; 1923 } 1924 if (mntoptslen > AUTOFS_MAXOPTSLEN) 1925 ul.mntopts[AUTOFS_MAXOPTSLEN - 1] = '\0'; 1926 1927 mntpt = vfs_getmntpoint(vfsp); 1928 ul.mntpnt = (char *)refstr_value(mntpt); 1929 resource = vfs_getresource(vfsp); 1930 ul.mntresource = (char *)refstr_value(resource); 1931 1932 fnip = vfstofni(cvp->v_vfsp); 1933 ul.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE; 1934 1935 /* 1936 * Since a zone'd automountd's view of the autofs mount points 1937 * differs from those in the kernel, we need to make sure we 1938 * give it consistent mount points. 1939 */ 1940 ASSERT(fnip->fi_zoneid == getzoneid()); 1941 zone = curproc->p_zone; 1942 1943 if (fnip->fi_zoneid != GLOBAL_ZONEID) { 1944 if (ZONE_PATH_VISIBLE(ul.mntpnt, zone)) { 1945 ul.mntpnt = 1946 ZONE_PATH_TRANSLATE(ul.mntpnt, zone); 1947 } 1948 if (ZONE_PATH_VISIBLE(ul.mntresource, zone)) { 1949 ul.mntresource = 1950 ZONE_PATH_TRANSLATE(ul.mntresource, zone); 1951 } 1952 } 1953 1954 ul.fstype = vfssw[vfsp->vfs_fstype].vsw_name; 1955 vn_vfsunlock(cvp); 1956 1957 error = auto_send_unmount_request(fnip, &ul, FALSE); 1958 kmem_free(ul.mntopts, mntoptslen); 1959 refstr_rele(mntpt); 1960 refstr_rele(resource); 1961 } 1962 1963 done: 1964 AUTOFS_DPRINT((5, "\tunmount_node cvp=%p error=%d\n", (void *)cvp, 1965 error)); 1966 return (error); 1967 } 1968 1969 /* 1970 * vp is the "root" of the AUTOFS filesystem. 1971 * return EBUSY if any thread is holding a reference to this vnode 1972 * other than us. 1973 */ 1974 static int 1975 check_auto_node(vnode_t *vp) 1976 { 1977 fnnode_t *fnp; 1978 int error = 0; 1979 /* 1980 * number of references to expect for 1981 * a non-busy vnode. 1982 */ 1983 uint_t count; 1984 1985 AUTOFS_DPRINT((4, "\tcheck_auto_node vp=%p ", (void *)vp)); 1986 fnp = vntofn(vp); 1987 ASSERT(fnp->fn_flags & MF_INPROG); 1988 ASSERT((fnp->fn_flags & MF_LOOKUP) == 0); 1989 1990 count = 1; /* we are holding a reference to vp */ 1991 if (fnp->fn_flags & MF_TRIGGER) { 1992 /* 1993 * parent holds a pointer to us (trigger) 1994 */ 1995 count++; 1996 } 1997 if (fnp->fn_trigger != NULL) { 1998 /* 1999 * The trigger nodes have a hold on us. 2000 */ 2001 count++; 2002 } 2003 mutex_enter(&vp->v_lock); 2004 if (vp->v_flag & VROOT) 2005 count++; 2006 ASSERT(vp->v_count > 0); 2007 AUTOFS_DPRINT((10, "\tcount=%u ", vp->v_count)); 2008 if (vp->v_count > count) 2009 error = EBUSY; 2010 mutex_exit(&vp->v_lock); 2011 2012 AUTOFS_DPRINT((5, "\tcheck_auto_node error=%d ", error)); 2013 return (error); 2014 } 2015 2016 /* 2017 * rootvp is the root of the AUTOFS filesystem. 2018 * If rootvp is busy (v_count > 1) returns EBUSY. 2019 * else removes every vnode under this tree. 2020 * ASSUMPTION: Assumes that the only node which can be busy is 2021 * the root vnode. This filesystem better be two levels deep only, 2022 * the root and its immediate subdirs. 2023 * The daemon will "AUTOFS direct-mount" only one level below the root. 2024 */ 2025 static int 2026 unmount_autofs(vnode_t *rootvp) 2027 { 2028 fnnode_t *fnp, *rootfnp, *nfnp; 2029 int error; 2030 2031 AUTOFS_DPRINT((4, "\tunmount_autofs rootvp=%p ", (void *)rootvp)); 2032 2033 error = check_auto_node(rootvp); 2034 if (error == 0) { 2035 /* 2036 * Remove all its immediate subdirectories. 2037 */ 2038 rootfnp = vntofn(rootvp); 2039 rw_enter(&rootfnp->fn_rwlock, RW_WRITER); 2040 nfnp = NULL; /* lint clean */ 2041 for (fnp = rootfnp->fn_dirents; fnp != NULL; fnp = nfnp) { 2042 ASSERT(fntovn(fnp)->v_count == 0); 2043 ASSERT(fnp->fn_dirents == NULL); 2044 ASSERT(fnp->fn_linkcnt == 2); 2045 fnp->fn_linkcnt--; 2046 auto_disconnect(rootfnp, fnp); 2047 nfnp = fnp->fn_next; 2048 auto_freefnnode(fnp); 2049 } 2050 rw_exit(&rootfnp->fn_rwlock); 2051 } 2052 AUTOFS_DPRINT((5, "\tunmount_autofs error=%d ", error)); 2053 return (error); 2054 } 2055 2056 /* 2057 * max number of unmount threads running 2058 */ 2059 static int autofs_unmount_threads = 5; 2060 2061 /* 2062 * XXX unmount_tree() is not suspend-safe within the scope of 2063 * the present model defined for cpr to suspend the system. Calls made 2064 * by the unmount_tree() that have been identified to be unsafe are 2065 * (1) RPC client handle setup and client calls to automountd which can 2066 * block deep down in the RPC library, (2) kmem_alloc() calls with the 2067 * KM_SLEEP flag which can block if memory is low, and (3) VFS_*() and 2068 * VOP_*() calls which can result in over the wire calls to servers. 2069 * The thread should be completely reevaluated to make it suspend-safe in 2070 * case of future updates to the cpr model. 2071 */ 2072 void 2073 unmount_tree(struct autofs_globals *fngp, int force) 2074 { 2075 vnode_t *vp, *newvp; 2076 vfs_t *vfsp; 2077 fnnode_t *fnp, *nfnp, *pfnp; 2078 action_list *alp; 2079 int error, ilocked_it = 0; 2080 fninfo_t *fnip; 2081 time_t ref_time; 2082 int autofs_busy_root, unmount_as_unit, unmount_done = 0; 2083 timestruc_t now; 2084 2085 callb_cpr_t cprinfo; 2086 kmutex_t unmount_tree_cpr_lock; 2087 2088 mutex_init(&unmount_tree_cpr_lock, NULL, MUTEX_DEFAULT, NULL); 2089 CALLB_CPR_INIT(&cprinfo, &unmount_tree_cpr_lock, callb_generic_cpr, 2090 "unmount_tree"); 2091 2092 /* 2093 * Got to release lock before attempting unmount in case 2094 * it hangs. 2095 */ 2096 rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_READER); 2097 if ((fnp = fngp->fng_rootfnnodep->fn_dirents) == NULL) { 2098 ASSERT(fngp->fng_fnnode_count == 1); 2099 /* 2100 * no autofs mounted, done. 2101 */ 2102 rw_exit(&fngp->fng_rootfnnodep->fn_rwlock); 2103 goto done; 2104 } 2105 VN_HOLD(fntovn(fnp)); 2106 rw_exit(&fngp->fng_rootfnnodep->fn_rwlock); 2107 2108 vp = fntovn(fnp); 2109 fnip = vfstofni(vp->v_vfsp); 2110 /* 2111 * autofssys() will be calling in from the global zone and doing 2112 * work on the behalf of the given zone, hence we can't always assert 2113 * that we have the right credentials, nor that the caller is always in 2114 * the correct zone. 2115 * 2116 * We do, however, know that if this is a "forced unmount" operation 2117 * (which autofssys() does), then we won't go down to the krpc layers, 2118 * so we don't need to fudge with the credentials. 2119 */ 2120 ASSERT(force || fnip->fi_zoneid == getzoneid()); 2121 if (!force && auto_null_request(fnip, FALSE) != 0) { 2122 /* 2123 * automountd not running in this zone, 2124 * don't attempt unmounting this round. 2125 */ 2126 VN_RELE(vp); 2127 goto done; 2128 } 2129 /* reference time for this unmount round */ 2130 ref_time = gethrestime_sec(); 2131 /* 2132 * If this an autofssys() call, we need to make sure we don't skip 2133 * nodes because we think we saw them recently. 2134 */ 2135 mutex_enter(&fnp->fn_lock); 2136 if (force && fnp->fn_unmount_ref_time >= ref_time) 2137 ref_time = fnp->fn_unmount_ref_time + 1; 2138 mutex_exit(&fnp->fn_lock); 2139 2140 AUTOFS_DPRINT((4, "unmount_tree (ID=%ld)\n", ref_time)); 2141 top: 2142 AUTOFS_DPRINT((10, "unmount_tree: %s\n", fnp->fn_name)); 2143 ASSERT(fnp); 2144 vp = fntovn(fnp); 2145 if (vp->v_type == VLNK) { 2146 /* 2147 * can't unmount symbolic links 2148 */ 2149 goto next; 2150 } 2151 fnip = vfstofni(vp->v_vfsp); 2152 ASSERT(vp->v_count > 0); 2153 error = 0; 2154 autofs_busy_root = unmount_as_unit = 0; 2155 alp = NULL; 2156 2157 ilocked_it = 0; 2158 mutex_enter(&fnp->fn_lock); 2159 if (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) { 2160 /* 2161 * Either a mount, lookup or another unmount of this 2162 * subtree is in progress, don't attempt to unmount at 2163 * this time. 2164 */ 2165 mutex_exit(&fnp->fn_lock); 2166 error = EBUSY; 2167 goto next; 2168 } 2169 if (fnp->fn_unmount_ref_time >= ref_time) { 2170 /* 2171 * Already been here, try next node. 2172 */ 2173 mutex_exit(&fnp->fn_lock); 2174 error = EBUSY; 2175 goto next; 2176 } 2177 fnp->fn_unmount_ref_time = ref_time; 2178 2179 /* 2180 * If forced operation ignore timeout values 2181 */ 2182 if (!force && fnp->fn_ref_time + fnip->fi_mount_to > 2183 gethrestime_sec()) { 2184 /* 2185 * Node has been referenced recently, try the 2186 * unmount of its children if any. 2187 */ 2188 mutex_exit(&fnp->fn_lock); 2189 AUTOFS_DPRINT((10, "fn_ref_time within range\n")); 2190 rw_enter(&fnp->fn_rwlock, RW_READER); 2191 if (fnp->fn_dirents) { 2192 /* 2193 * Has subdirectory, attempt their 2194 * unmount first 2195 */ 2196 nfnp = fnp->fn_dirents; 2197 VN_HOLD(fntovn(nfnp)); 2198 rw_exit(&fnp->fn_rwlock); 2199 2200 VN_RELE(vp); 2201 fnp = nfnp; 2202 goto top; 2203 } 2204 rw_exit(&fnp->fn_rwlock); 2205 /* 2206 * No children, try next node. 2207 */ 2208 error = EBUSY; 2209 goto next; 2210 } 2211 2212 AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG); 2213 fnp->fn_error = 0; 2214 mutex_exit(&fnp->fn_lock); 2215 ilocked_it = 1; 2216 2217 rw_enter(&fnp->fn_rwlock, RW_WRITER); 2218 if (fnp->fn_trigger != NULL) { 2219 unmount_as_unit = 1; 2220 if ((vn_mountedvfs(vp) == NULL) && (check_auto_node(vp))) { 2221 /* 2222 * AUTOFS mountpoint is busy, there's 2223 * no point trying to unmount. Fall through 2224 * to attempt to unmount subtrees rooted 2225 * at a possible trigger node, but remember 2226 * not to unmount this tree. 2227 */ 2228 autofs_busy_root = 1; 2229 } 2230 2231 if (triggers_busy(fnp, &nfnp)) { 2232 rw_exit(&fnp->fn_rwlock); 2233 if (nfnp == NULL) { 2234 error = EBUSY; 2235 goto next; 2236 } 2237 /* 2238 * nfnp is busy, try to unmount it first 2239 */ 2240 mutex_enter(&fnp->fn_lock); 2241 AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 2242 mutex_exit(&fnp->fn_lock); 2243 VN_RELE(vp); 2244 ASSERT(fntovn(nfnp)->v_count > 1); 2245 fnp = nfnp; 2246 goto top; 2247 } 2248 2249 /* 2250 * At this point, we know all trigger nodes are locked, 2251 * and they're not busy or mounted on. 2252 */ 2253 2254 if (autofs_busy_root) { 2255 /* 2256 * Got to unlock the the trigger nodes since 2257 * I'm not really going to unmount the filesystem. 2258 */ 2259 (void) triggers_unlock(fnp); 2260 } else { 2261 /* 2262 * Attempt to unmount all the trigger nodes, 2263 * save the action_list in case we need to 2264 * remount them later. The action_list will be 2265 * freed later if there was no need to remount the 2266 * trigger nodes. 2267 */ 2268 unmount_triggers(fnp, &alp); 2269 } 2270 } 2271 rw_exit(&fnp->fn_rwlock); 2272 2273 if (autofs_busy_root) 2274 goto next; 2275 2276 (void) vn_vfswlock_wait(vp); 2277 2278 vfsp = vn_mountedvfs(vp); 2279 if (vfsp != NULL) { 2280 /* 2281 * Node is mounted on. 2282 */ 2283 AUTOFS_DPRINT((10, "\tNode is mounted on\n")); 2284 2285 /* 2286 * Deal with /xfn/host/jurassic alikes here... 2287 */ 2288 if (vfs_matchops(vfsp, vfs_getops(vp->v_vfsp))) { 2289 /* 2290 * If the filesystem mounted here is AUTOFS, and it 2291 * is busy, try to unmount the tree rooted on it 2292 * first. We know this call to VFS_ROOT is safe to 2293 * call while holding VVFSLOCK, since it resolves 2294 * to a call to auto_root(). 2295 */ 2296 AUTOFS_DPRINT((10, "\t\tAUTOFS mounted here\n")); 2297 if (VFS_ROOT(vfsp, &newvp)) { 2298 cmn_err(CE_PANIC, 2299 "unmount_tree: VFS_ROOT(vfs=%p) failed", 2300 (void *)vfsp); 2301 } 2302 nfnp = vntofn(newvp); 2303 if (DEEPER(nfnp)) { 2304 vn_vfsunlock(vp); 2305 mutex_enter(&fnp->fn_lock); 2306 AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 2307 mutex_exit(&fnp->fn_lock); 2308 VN_RELE(vp); 2309 fnp = nfnp; 2310 goto top; 2311 } 2312 /* 2313 * Fall through to unmount this filesystem 2314 */ 2315 VN_RELE(newvp); 2316 } 2317 2318 /* 2319 * vn_vfsunlock(vp) is done inside unmount_node() 2320 */ 2321 error = unmount_node(vp, force); 2322 if (error == ECONNRESET) { 2323 AUTOFS_DPRINT((10, "\tConnection dropped\n")); 2324 if (vn_mountedvfs(vp) == NULL) { 2325 /* 2326 * The filesystem was unmounted before the 2327 * daemon died. Unfortunately we can not 2328 * determine whether all the cleanup work was 2329 * successfully finished (i.e. update mnttab, 2330 * or notify NFS server of the unmount). 2331 * We should not retry the operation since the 2332 * filesystem has already been unmounted, and 2333 * may have already been removed from mnttab, 2334 * in such case the devid/rdevid we send to 2335 * the daemon will not be matched. So we have 2336 * to be content with the partial unmount. 2337 * Since the mountpoint is no longer covered, we 2338 * clear the error condition. 2339 */ 2340 error = 0; 2341 auto_log(fngp->fng_verbose, fngp->fng_zoneid, 2342 CE_WARN, 2343 "unmount_tree: automountd connection " 2344 "dropped"); 2345 if (fnip->fi_flags & MF_DIRECT) { 2346 auto_log(fngp->fng_verbose, 2347 fngp->fng_zoneid, CE_WARN, 2348 "unmount_tree: " 2349 "%s successfully unmounted - " 2350 "do not remount triggers", 2351 fnip->fi_path); 2352 } else { 2353 auto_log(fngp->fng_verbose, 2354 fngp->fng_zoneid, CE_WARN, 2355 "unmount_tree: " 2356 "%s/%s successfully unmounted - " 2357 "do not remount triggers", 2358 fnip->fi_path, fnp->fn_name); 2359 } 2360 } 2361 } 2362 } else { 2363 vn_vfsunlock(vp); 2364 AUTOFS_DPRINT((10, "\tNode is AUTOFS\n")); 2365 if (unmount_as_unit) { 2366 AUTOFS_DPRINT((10, "\tunmount as unit\n")); 2367 error = unmount_autofs(vp); 2368 } else { 2369 AUTOFS_DPRINT((10, "\tunmount one at a time\n")); 2370 rw_enter(&fnp->fn_rwlock, RW_READER); 2371 if (fnp->fn_dirents != NULL) { 2372 /* 2373 * Has subdirectory, attempt their 2374 * unmount first 2375 */ 2376 nfnp = fnp->fn_dirents; 2377 VN_HOLD(fntovn(nfnp)); 2378 rw_exit(&fnp->fn_rwlock); 2379 2380 mutex_enter(&fnp->fn_lock); 2381 AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 2382 mutex_exit(&fnp->fn_lock); 2383 VN_RELE(vp); 2384 fnp = nfnp; 2385 goto top; 2386 } 2387 rw_exit(&fnp->fn_rwlock); 2388 goto next; 2389 } 2390 } 2391 2392 if (error) { 2393 AUTOFS_DPRINT((10, "\tUnmount failed\n")); 2394 if (alp != NULL) { 2395 /* 2396 * Unmount failed, got to remount triggers. 2397 */ 2398 ASSERT((fnp->fn_flags & MF_THISUID_MATCH_RQD) == 0); 2399 error = auto_perform_actions(fnip, fnp, alp, 2400 CRED()); 2401 if (error) { 2402 auto_log(fngp->fng_verbose, 2403 fngp->fng_zoneid, CE_WARN, 2404 "autofs: can't remount " 2405 "triggers fnp=%p error=%d", (void *)fnp, 2406 error); 2407 error = 0; 2408 /* 2409 * The action list should have been 2410 * free'd by auto_perform_actions 2411 * since an error occured 2412 */ 2413 alp = NULL; 2414 2415 } 2416 } 2417 } else { 2418 /* 2419 * The unmount succeeded, which will cause this node to 2420 * be removed from its parent if its an indirect mount, 2421 * therefore update the parent's atime and mtime now. 2422 * I don't update them in auto_disconnect() because I 2423 * don't want atime and mtime changing every time a 2424 * lookup goes to the daemon and creates a new node. 2425 */ 2426 unmount_done = 1; 2427 if ((fnip->fi_flags & MF_DIRECT) == 0) { 2428 gethrestime(&now); 2429 if (fnp->fn_parent == fngp->fng_rootfnnodep) 2430 fnp->fn_atime = fnp->fn_mtime = now; 2431 else 2432 fnp->fn_parent->fn_atime = 2433 fnp->fn_parent->fn_mtime = now; 2434 } 2435 2436 /* 2437 * Free the action list here 2438 */ 2439 if (alp != NULL) { 2440 xdr_free(xdr_action_list, (char *)alp); 2441 alp = NULL; 2442 } 2443 } 2444 2445 fnp->fn_ref_time = gethrestime_sec(); 2446 2447 next: 2448 /* 2449 * Obtain parent's readers lock before grabbing 2450 * reference to next sibling. 2451 * XXX Note that nodes in the top level list (mounted 2452 * in user space not by the daemon in the kernel) parent is itself, 2453 * therefore grabbing the lock makes no sense, but doesn't 2454 * hurt either. 2455 */ 2456 pfnp = fnp->fn_parent; 2457 ASSERT(pfnp != NULL); 2458 rw_enter(&pfnp->fn_rwlock, RW_READER); 2459 if ((nfnp = fnp->fn_next) != NULL) 2460 VN_HOLD(fntovn(nfnp)); 2461 rw_exit(&pfnp->fn_rwlock); 2462 2463 if (ilocked_it) { 2464 mutex_enter(&fnp->fn_lock); 2465 if (unmount_done) { 2466 /* 2467 * Other threads may be waiting for this unmount to 2468 * finish. We must let it know that in order to 2469 * proceed, it must trigger the mount itself. 2470 */ 2471 fnp->fn_flags &= ~MF_IK_MOUNT; 2472 if (fnp->fn_flags & MF_WAITING) 2473 fnp->fn_error = EAGAIN; 2474 unmount_done = 0; 2475 } 2476 AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 2477 mutex_exit(&fnp->fn_lock); 2478 ilocked_it = 0; 2479 } 2480 2481 if (nfnp != NULL) { 2482 VN_RELE(vp); 2483 fnp = nfnp; 2484 /* 2485 * Unmount next element 2486 */ 2487 goto top; 2488 } 2489 2490 /* 2491 * We don't want to unmount rootfnnodep, so the check is made here 2492 */ 2493 ASSERT(pfnp != fnp); 2494 if (pfnp != fngp->fng_rootfnnodep) { 2495 /* 2496 * Now attempt to unmount my parent 2497 */ 2498 VN_HOLD(fntovn(pfnp)); 2499 VN_RELE(vp); 2500 fnp = pfnp; 2501 2502 goto top; 2503 } 2504 2505 VN_RELE(vp); 2506 2507 /* 2508 * At this point we've walked the entire tree and attempted to unmount 2509 * as much as we can one level at a time. 2510 */ 2511 done: 2512 mutex_enter(&unmount_tree_cpr_lock); 2513 CALLB_CPR_EXIT(&cprinfo); 2514 mutex_destroy(&unmount_tree_cpr_lock); 2515 } 2516 2517 static void 2518 unmount_zone_tree(struct autofs_globals *fngp) 2519 { 2520 unmount_tree(fngp, 0); 2521 mutex_enter(&fngp->fng_unmount_threads_lock); 2522 fngp->fng_unmount_threads--; 2523 mutex_exit(&fngp->fng_unmount_threads_lock); 2524 2525 AUTOFS_DPRINT((5, "unmount_tree done. Thread exiting.\n")); 2526 2527 zthread_exit(); 2528 /* NOTREACHED */ 2529 } 2530 2531 static int autofs_unmount_thread_timer = 120; /* in seconds */ 2532 2533 void 2534 auto_do_unmount(struct autofs_globals *fngp) 2535 { 2536 callb_cpr_t cprinfo; 2537 clock_t timeleft; 2538 zone_t *zone = curproc->p_zone; 2539 2540 CALLB_CPR_INIT(&cprinfo, &fngp->fng_unmount_threads_lock, 2541 callb_generic_cpr, "auto_do_unmount"); 2542 2543 for (;;) { /* forever */ 2544 mutex_enter(&fngp->fng_unmount_threads_lock); 2545 CALLB_CPR_SAFE_BEGIN(&cprinfo); 2546 newthread: 2547 mutex_exit(&fngp->fng_unmount_threads_lock); 2548 timeleft = zone_status_timedwait(zone, lbolt + 2549 autofs_unmount_thread_timer * hz, ZONE_IS_SHUTTING_DOWN); 2550 mutex_enter(&fngp->fng_unmount_threads_lock); 2551 2552 if (timeleft != -1) { /* didn't time out */ 2553 ASSERT(zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN); 2554 /* 2555 * zone is exiting... don't create any new threads. 2556 * fng_unmount_threads_lock is released implicitly by 2557 * the below. 2558 */ 2559 CALLB_CPR_SAFE_END(&cprinfo, 2560 &fngp->fng_unmount_threads_lock); 2561 CALLB_CPR_EXIT(&cprinfo); 2562 zthread_exit(); 2563 /* NOTREACHED */ 2564 } 2565 if (fngp->fng_unmount_threads < autofs_unmount_threads) { 2566 fngp->fng_unmount_threads++; 2567 CALLB_CPR_SAFE_END(&cprinfo, 2568 &fngp->fng_unmount_threads_lock); 2569 mutex_exit(&fngp->fng_unmount_threads_lock); 2570 2571 (void) zthread_create(NULL, 0, unmount_zone_tree, fngp, 2572 0, minclsyspri); 2573 } else 2574 goto newthread; 2575 } 2576 /* NOTREACHED */ 2577 } 2578 2579 /* 2580 * Is nobrowse specified in option string? 2581 * opts should be a null ('\0') terminated string. 2582 * Returns non-zero if nobrowse has been specified. 2583 */ 2584 int 2585 auto_nobrowse_option(char *opts) 2586 { 2587 char *buf; 2588 char *p; 2589 char *t; 2590 int nobrowse = 0; 2591 int last_opt = 0; 2592 size_t len; 2593 2594 len = strlen(opts) + 1; 2595 p = buf = kmem_alloc(len, KM_SLEEP); 2596 (void) strcpy(buf, opts); 2597 do { 2598 if (t = strchr(p, ',')) 2599 *t++ = '\0'; 2600 else 2601 last_opt++; 2602 if (strcmp(p, MNTOPT_NOBROWSE) == 0) 2603 nobrowse = 1; 2604 else if (strcmp(p, MNTOPT_BROWSE) == 0) 2605 nobrowse = 0; 2606 p = t; 2607 } while (!last_opt); 2608 kmem_free(buf, len); 2609 2610 return (nobrowse); 2611 } 2612 2613 /* 2614 * used to log warnings only if automountd is running 2615 * with verbose mode set 2616 */ 2617 2618 void 2619 auto_log(int verbose, zoneid_t zoneid, int level, const char *fmt, ...) 2620 { 2621 va_list args; 2622 2623 if (verbose) { 2624 va_start(args, fmt); 2625 vzcmn_err(zoneid, level, fmt, args); 2626 va_end(args); 2627 } 2628 } 2629 2630 #ifdef DEBUG 2631 static int autofs_debug = 0; 2632 2633 /* 2634 * Utilities used by both client and server 2635 * Standard levels: 2636 * 0) no debugging 2637 * 1) hard failures 2638 * 2) soft failures 2639 * 3) current test software 2640 * 4) main procedure entry points 2641 * 5) main procedure exit points 2642 * 6) utility procedure entry points 2643 * 7) utility procedure exit points 2644 * 8) obscure procedure entry points 2645 * 9) obscure procedure exit points 2646 * 10) random stuff 2647 * 11) all <= 1 2648 * 12) all <= 2 2649 * 13) all <= 3 2650 * ... 2651 */ 2652 /* PRINTFLIKE2 */ 2653 void 2654 auto_dprint(int level, const char *fmt, ...) 2655 { 2656 va_list args; 2657 2658 if (autofs_debug == level || 2659 (autofs_debug > 10 && (autofs_debug - 10) >= level)) { 2660 va_start(args, fmt); 2661 (void) vprintf(fmt, args); 2662 va_end(args); 2663 } 2664 } 2665 #endif /* DEBUG */ 2666