10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52170Sevanl * Common Development and Distribution License (the "License"). 62170Sevanl * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 215891Sraf 220Sstevel@tonic-gate /* 235891Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/param.h> 300Sstevel@tonic-gate #include <sys/kmem.h> 310Sstevel@tonic-gate #include <sys/errno.h> 320Sstevel@tonic-gate #include <sys/proc.h> 330Sstevel@tonic-gate #include <sys/disp.h> 340Sstevel@tonic-gate #include <sys/vfs.h> 350Sstevel@tonic-gate #include <sys/vnode.h> 360Sstevel@tonic-gate #include <sys/pathname.h> 370Sstevel@tonic-gate #include <sys/cred.h> 380Sstevel@tonic-gate #include <sys/mount.h> 390Sstevel@tonic-gate #include <sys/cmn_err.h> 400Sstevel@tonic-gate #include <sys/debug.h> 410Sstevel@tonic-gate #include <sys/systm.h> 420Sstevel@tonic-gate #include <sys/dirent.h> 430Sstevel@tonic-gate #include <fs/fs_subr.h> 440Sstevel@tonic-gate #include <sys/fs/autofs.h> 450Sstevel@tonic-gate #include <sys/callb.h> 460Sstevel@tonic-gate #include <sys/sysmacros.h> 470Sstevel@tonic-gate #include <sys/zone.h> 482170Sevanl #include <sys/door.h> 490Sstevel@tonic-gate #include <sys/fs/mntdata.h> 502170Sevanl #include <nfs/mount.h> 512170Sevanl #include <rpc/clnt.h> 522170Sevanl #include <rpcsvc/autofs_prot.h> 532170Sevanl #include <nfs/rnode.h> 542170Sevanl #include <sys/utsname.h> 555891Sraf #include <sys/schedctl.h> 560Sstevel@tonic-gate 570Sstevel@tonic-gate /* 580Sstevel@tonic-gate * Autofs and Zones: 590Sstevel@tonic-gate * 600Sstevel@tonic-gate * Zones are delegated the responsibility of managing their own autofs mounts 610Sstevel@tonic-gate * and maps. Each zone runs its own copy of automountd, with its own timeouts, 620Sstevel@tonic-gate * and other logically "global" parameters. kRPC and virtualization in the 630Sstevel@tonic-gate * loopback transport (tl) will prevent a zone from communicating with another 640Sstevel@tonic-gate * zone's automountd. 650Sstevel@tonic-gate * 660Sstevel@tonic-gate * Each zone has its own "rootfnnode" and associated tree of auto nodes. 670Sstevel@tonic-gate * 680Sstevel@tonic-gate * Each zone also has its own set of "unmounter" kernel threads; these are 690Sstevel@tonic-gate * created and run within the zone's context (ie, they are created via 700Sstevel@tonic-gate * zthread_create()). 710Sstevel@tonic-gate * 720Sstevel@tonic-gate * Cross-zone mount triggers are disallowed. There is a check in 730Sstevel@tonic-gate * auto_trigger_mount() to this effect; EPERM is returned to indicate that the 740Sstevel@tonic-gate * mount is not owned by the caller. 750Sstevel@tonic-gate * 760Sstevel@tonic-gate * autofssys() enables a caller in the global zone to clean up in-kernel (as 770Sstevel@tonic-gate * well as regular) autofs mounts via the unmount_tree() mechanism. This is 780Sstevel@tonic-gate * routinely done when all mounts are removed as part of zone shutdown. 790Sstevel@tonic-gate */ 800Sstevel@tonic-gate #define TYPICALMAXPATHLEN 64 810Sstevel@tonic-gate 820Sstevel@tonic-gate static kmutex_t autofs_nodeid_lock; 830Sstevel@tonic-gate 840Sstevel@tonic-gate static int auto_perform_link(fnnode_t *, struct linka *, cred_t *); 850Sstevel@tonic-gate static int auto_perform_actions(fninfo_t *, fnnode_t *, 860Sstevel@tonic-gate action_list *, cred_t *); 870Sstevel@tonic-gate static int auto_getmntpnt(vnode_t *, char *, vnode_t **, cred_t *); 880Sstevel@tonic-gate static int auto_lookup_request(fninfo_t *, char *, struct linka *, 893391Ssemery bool_t, bool_t *, cred_t *); 903391Ssemery static int auto_mount_request(fninfo_t *, char *, action_list **, cred_t *, 912170Sevanl bool_t); 922170Sevanl 930Sstevel@tonic-gate /* 940Sstevel@tonic-gate * Clears the MF_INPROG flag, and wakes up those threads sleeping on 950Sstevel@tonic-gate * fn_cv_mount if MF_WAITING is set. 960Sstevel@tonic-gate */ 970Sstevel@tonic-gate void 980Sstevel@tonic-gate auto_unblock_others( 990Sstevel@tonic-gate fnnode_t *fnp, 1000Sstevel@tonic-gate uint_t operation) /* either MF_INPROG or MF_LOOKUP */ 1010Sstevel@tonic-gate { 1020Sstevel@tonic-gate ASSERT(operation & (MF_INPROG | MF_LOOKUP)); 1030Sstevel@tonic-gate fnp->fn_flags &= ~operation; 1040Sstevel@tonic-gate if (fnp->fn_flags & MF_WAITING) { 1050Sstevel@tonic-gate fnp->fn_flags &= ~MF_WAITING; 1060Sstevel@tonic-gate cv_broadcast(&fnp->fn_cv_mount); 1070Sstevel@tonic-gate } 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate int 1110Sstevel@tonic-gate auto_wait4mount(fnnode_t *fnp) 1120Sstevel@tonic-gate { 1130Sstevel@tonic-gate int error; 1140Sstevel@tonic-gate k_sigset_t smask; 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_wait4mount: fnp=%p\n", (void *)fnp)); 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 1190Sstevel@tonic-gate while (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) { 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * There is a mount or a lookup in progress. 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate fnp->fn_flags |= MF_WAITING; 1240Sstevel@tonic-gate sigintr(&smask, 1); 1250Sstevel@tonic-gate if (!cv_wait_sig(&fnp->fn_cv_mount, &fnp->fn_lock)) { 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * Decided not to wait for operation to 1280Sstevel@tonic-gate * finish after all. 1290Sstevel@tonic-gate */ 1300Sstevel@tonic-gate sigunintr(&smask); 1310Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 1320Sstevel@tonic-gate return (EINTR); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate sigunintr(&smask); 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate error = fnp->fn_error; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate if (error == EINTR) { 1390Sstevel@tonic-gate /* 1400Sstevel@tonic-gate * The thread doing the mount got interrupted, we need to 1410Sstevel@tonic-gate * try again, by returning EAGAIN. 1420Sstevel@tonic-gate */ 1430Sstevel@tonic-gate error = EAGAIN; 1440Sstevel@tonic-gate } 1450Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_wait4mount: fnp=%p error=%d\n", (void *)fnp, 1480Sstevel@tonic-gate error)); 1490Sstevel@tonic-gate return (error); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate 1520Sstevel@tonic-gate int 1530Sstevel@tonic-gate auto_lookup_aux(fnnode_t *fnp, char *name, cred_t *cred) 1540Sstevel@tonic-gate { 1550Sstevel@tonic-gate struct fninfo *fnip; 1560Sstevel@tonic-gate struct linka link; 1570Sstevel@tonic-gate bool_t mountreq = FALSE; 1580Sstevel@tonic-gate int error = 0; 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate fnip = vfstofni(fntovn(fnp)->v_vfsp); 1610Sstevel@tonic-gate bzero(&link, sizeof (link)); 1623391Ssemery error = auto_lookup_request(fnip, name, &link, TRUE, &mountreq, cred); 1630Sstevel@tonic-gate if (!error) { 1642170Sevanl if (link.link != NULL || link.link != '\0') { 1650Sstevel@tonic-gate /* 1660Sstevel@tonic-gate * This node should be a symlink 1670Sstevel@tonic-gate */ 1680Sstevel@tonic-gate error = auto_perform_link(fnp, &link, cred); 1690Sstevel@tonic-gate } else if (mountreq) { 1700Sstevel@tonic-gate /* 1710Sstevel@tonic-gate * The automount daemon is requesting a mount, 1720Sstevel@tonic-gate * implying this entry must be a wildcard match and 1730Sstevel@tonic-gate * therefore in need of verification that the entry 1740Sstevel@tonic-gate * exists on the server. 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 1770Sstevel@tonic-gate AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG); 1780Sstevel@tonic-gate fnp->fn_error = 0; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate /* 1810Sstevel@tonic-gate * Unblock other lookup requests on this node, 1820Sstevel@tonic-gate * this is needed to let the lookup generated by 1830Sstevel@tonic-gate * the mount call to complete. The caveat is 1840Sstevel@tonic-gate * other lookups on this node can also get by, 1850Sstevel@tonic-gate * i.e., another lookup on this node that occurs 1860Sstevel@tonic-gate * while this lookup is attempting the mount 1870Sstevel@tonic-gate * would return a positive result no matter what. 1880Sstevel@tonic-gate * Therefore two lookups on the this node could 1890Sstevel@tonic-gate * potentially get disparate results. 1900Sstevel@tonic-gate */ 1910Sstevel@tonic-gate AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP); 1920Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 1930Sstevel@tonic-gate /* 1940Sstevel@tonic-gate * auto_new_mount_thread fires up a new thread which 1950Sstevel@tonic-gate * calls automountd finishing up the work 1960Sstevel@tonic-gate */ 1970Sstevel@tonic-gate auto_new_mount_thread(fnp, name, cred); 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate /* 2000Sstevel@tonic-gate * At this point, we are simply another thread 2010Sstevel@tonic-gate * waiting for the mount to complete 2020Sstevel@tonic-gate */ 2030Sstevel@tonic-gate error = auto_wait4mount(fnp); 2040Sstevel@tonic-gate if (error == AUTOFS_SHUTDOWN) 2050Sstevel@tonic-gate error = ENOENT; 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2092170Sevanl if (link.link) 2102170Sevanl kmem_free(link.link, strlen(link.link) + 1); 2112170Sevanl if (link.dir) 2122170Sevanl kmem_free(link.dir, strlen(link.dir) + 1); 2130Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 2140Sstevel@tonic-gate fnp->fn_error = error; 2150Sstevel@tonic-gate 2160Sstevel@tonic-gate /* 2170Sstevel@tonic-gate * Notify threads waiting for lookup/mount that 2180Sstevel@tonic-gate * it's done. 2190Sstevel@tonic-gate */ 2200Sstevel@tonic-gate if (mountreq) { 2210Sstevel@tonic-gate AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 2220Sstevel@tonic-gate } else { 2230Sstevel@tonic-gate AUTOFS_UNBLOCK_OTHERS(fnp, MF_LOOKUP); 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 2260Sstevel@tonic-gate return (error); 2270Sstevel@tonic-gate } 2280Sstevel@tonic-gate 2290Sstevel@tonic-gate /* 2300Sstevel@tonic-gate * Starting point for thread to handle mount requests with automountd. 2310Sstevel@tonic-gate * XXX auto_mount_thread() is not suspend-safe within the scope of 2320Sstevel@tonic-gate * the present model defined for cpr to suspend the system. Calls 2330Sstevel@tonic-gate * made by the auto_mount_thread() that have been identified to be unsafe 2340Sstevel@tonic-gate * are (1) RPC client handle setup and client calls to automountd which 2350Sstevel@tonic-gate * can block deep down in the RPC library, (2) kmem_alloc() calls with the 2360Sstevel@tonic-gate * KM_SLEEP flag which can block if memory is low, and (3) VFS_*(), and 2370Sstevel@tonic-gate * lookuppnvp() calls which can result in over the wire calls to servers. 2380Sstevel@tonic-gate * The thread should be completely reevaluated to make it suspend-safe in 2390Sstevel@tonic-gate * case of future updates to the cpr model. 2400Sstevel@tonic-gate */ 2410Sstevel@tonic-gate static void 2420Sstevel@tonic-gate auto_mount_thread(struct autofs_callargs *argsp) 2430Sstevel@tonic-gate { 2442170Sevanl struct fninfo *fnip; 2452170Sevanl fnnode_t *fnp; 2462170Sevanl vnode_t *vp; 2472170Sevanl char *name; 2482170Sevanl size_t namelen; 2492170Sevanl cred_t *cred; 2502170Sevanl action_list *alp = NULL; 2512170Sevanl int error; 2522170Sevanl callb_cpr_t cprinfo; 2532170Sevanl kmutex_t auto_mount_thread_cpr_lock; 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate mutex_init(&auto_mount_thread_cpr_lock, NULL, MUTEX_DEFAULT, NULL); 2562170Sevanl CALLB_CPR_INIT(&cprinfo, &auto_mount_thread_cpr_lock, 2576046Srmesta callb_generic_cpr, "auto_mount_thread"); 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate fnp = argsp->fnc_fnp; 2600Sstevel@tonic-gate vp = fntovn(fnp); 2610Sstevel@tonic-gate fnip = vfstofni(vp->v_vfsp); 2620Sstevel@tonic-gate name = argsp->fnc_name; 2630Sstevel@tonic-gate cred = argsp->fnc_cred; 2640Sstevel@tonic-gate ASSERT(crgetzoneid(argsp->fnc_cred) == fnip->fi_zoneid); 2650Sstevel@tonic-gate 2663391Ssemery error = auto_mount_request(fnip, name, &alp, cred, TRUE); 2670Sstevel@tonic-gate if (!error) 2680Sstevel@tonic-gate error = auto_perform_actions(fnip, fnp, alp, cred); 2690Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 2700Sstevel@tonic-gate fnp->fn_error = error; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate /* 2730Sstevel@tonic-gate * Notify threads waiting for mount that 2740Sstevel@tonic-gate * it's done. 2750Sstevel@tonic-gate */ 2760Sstevel@tonic-gate AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 2770Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 2780Sstevel@tonic-gate 2790Sstevel@tonic-gate VN_RELE(vp); 2800Sstevel@tonic-gate crfree(argsp->fnc_cred); 2810Sstevel@tonic-gate namelen = strlen(argsp->fnc_name) + 1; 2820Sstevel@tonic-gate kmem_free(argsp->fnc_name, namelen); 2830Sstevel@tonic-gate kmem_free(argsp, sizeof (*argsp)); 2840Sstevel@tonic-gate 2850Sstevel@tonic-gate mutex_enter(&auto_mount_thread_cpr_lock); 2860Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 2870Sstevel@tonic-gate mutex_destroy(&auto_mount_thread_cpr_lock); 2880Sstevel@tonic-gate zthread_exit(); 2890Sstevel@tonic-gate /* NOTREACHED */ 2900Sstevel@tonic-gate } 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate static int autofs_thr_success = 0; 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate /* 2950Sstevel@tonic-gate * Creates new thread which calls auto_mount_thread which does 2960Sstevel@tonic-gate * the bulk of the work calling automountd, via 'auto_perform_actions'. 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate void 2990Sstevel@tonic-gate auto_new_mount_thread(fnnode_t *fnp, char *name, cred_t *cred) 3000Sstevel@tonic-gate { 3010Sstevel@tonic-gate struct autofs_callargs *argsp; 3020Sstevel@tonic-gate 3030Sstevel@tonic-gate argsp = kmem_alloc(sizeof (*argsp), KM_SLEEP); 3040Sstevel@tonic-gate VN_HOLD(fntovn(fnp)); 3050Sstevel@tonic-gate argsp->fnc_fnp = fnp; 3060Sstevel@tonic-gate argsp->fnc_name = kmem_alloc(strlen(name) + 1, KM_SLEEP); 3070Sstevel@tonic-gate (void) strcpy(argsp->fnc_name, name); 3080Sstevel@tonic-gate argsp->fnc_origin = curthread; 3090Sstevel@tonic-gate crhold(cred); 3100Sstevel@tonic-gate argsp->fnc_cred = cred; 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate (void) zthread_create(NULL, 0, auto_mount_thread, argsp, 0, 3130Sstevel@tonic-gate minclsyspri); 3140Sstevel@tonic-gate autofs_thr_success++; 3150Sstevel@tonic-gate } 3160Sstevel@tonic-gate 3176046Srmesta #define DOOR_BUF_ALIGN (1024*1024) 3186046Srmesta #define DOOR_BUF_MULTIPLIER 3 3196046Srmesta #define DOOR_BUF_DEFAULT_SZ (DOOR_BUF_MULTIPLIER * DOOR_BUF_ALIGN) 3206046Srmesta int doorbuf_defsz = DOOR_BUF_DEFAULT_SZ; 3216046Srmesta 3226046Srmesta /*ARGSUSED*/ 3230Sstevel@tonic-gate int 3240Sstevel@tonic-gate auto_calldaemon( 3252170Sevanl zoneid_t zoneid, 3262170Sevanl int which, 3272170Sevanl xdrproc_t xarg_func, 3282170Sevanl void *argsp, 3292170Sevanl xdrproc_t xresp_func, 3302170Sevanl void *resp, 3312170Sevanl int reslen, 3322170Sevanl bool_t hard) /* retry forever? */ 3330Sstevel@tonic-gate { 3346046Srmesta int retry; 3356046Srmesta int error = 0; 3366046Srmesta k_sigset_t smask; 3376046Srmesta door_arg_t door_args; 3386046Srmesta door_handle_t dh; 3396046Srmesta XDR xdrarg; 3406046Srmesta XDR xdrres; 3412170Sevanl struct autofs_globals *fngp = NULL; 3426046Srmesta void *orp = NULL; 3436046Srmesta int orl; 344*6060Srmesta int rlen = 0; /* MUST be initialized */ 3452170Sevanl autofs_door_args_t *xdr_argsp; 3466046Srmesta int xdr_len = 0; 3476046Srmesta int printed_not_running_msg = 0; 3484092Sevanl klwp_t *lwp = ttolwp(curthread); 3492170Sevanl 3502170Sevanl /* 3512170Sevanl * We know that the current thread is doing work on 3522170Sevanl * behalf of its own zone, so it's ok to use 3532170Sevanl * curproc->p_zone. 3542170Sevanl */ 3552170Sevanl ASSERT(zoneid == getzoneid()); 3566046Srmesta if (zone_status_get(curproc->p_zone) >= ZONE_IS_SHUTTING_DOWN) { 3572170Sevanl /* 3582170Sevanl * There's no point in trying to talk to 3592170Sevanl * automountd. Plus, zone_shutdown() is 3602170Sevanl * waiting for us. 3612170Sevanl */ 3622170Sevanl return (ECONNREFUSED); 3632170Sevanl } 3640Sstevel@tonic-gate 3654092Sevanl do { 3664092Sevanl retry = 0; 3674092Sevanl mutex_enter(&autofs_minor_lock); 3684092Sevanl fngp = zone_getspecific(autofs_key, curproc->p_zone); 3694092Sevanl mutex_exit(&autofs_minor_lock); 3704092Sevanl if (fngp == NULL) { 3714092Sevanl if (hard) { 3724092Sevanl AUTOFS_DPRINT((5, 3734092Sevanl "auto_calldaemon: "\ 3744092Sevanl "failed to get door handle\n")); 3754092Sevanl if (!printed_not_running_msg) { 3764092Sevanl printed_not_running_msg = 1; 3774092Sevanl zprintf(zoneid, "automountd not "\ 3784092Sevanl "running, retrying\n"); 3794092Sevanl } 3804092Sevanl delay(hz); 3814092Sevanl retry = 1; 3824092Sevanl } else { 3834092Sevanl /* 3844092Sevanl * There is no global data so no door. 3854092Sevanl * There's no point in attempting to talk 3864092Sevanl * to automountd if we can't get the door 3874092Sevanl * handle. 3884092Sevanl */ 3894092Sevanl return (ECONNREFUSED); 3904092Sevanl } 3914092Sevanl } 3924092Sevanl } while (retry); 3934092Sevanl 3944092Sevanl if (printed_not_running_msg) { 3954092Sevanl fngp->fng_printed_not_running_msg = printed_not_running_msg; 3962170Sevanl } 3972170Sevanl 3982170Sevanl ASSERT(fngp != NULL); 3992170Sevanl 4002170Sevanl if (argsp != NULL && (xdr_len = xdr_sizeof(xarg_func, argsp)) == 0) 4012170Sevanl return (EINVAL); 4022170Sevanl xdr_argsp = kmem_zalloc(xdr_len + sizeof (*xdr_argsp), KM_SLEEP); 4032170Sevanl xdr_argsp->xdr_len = xdr_len; 4042170Sevanl xdr_argsp->cmd = which; 4052170Sevanl 4062170Sevanl if (argsp) { 4072170Sevanl xdrmem_create(&xdrarg, (char *)&xdr_argsp->xdr_arg, 4086046Srmesta xdr_argsp->xdr_len, XDR_ENCODE); 4092170Sevanl 4102170Sevanl if (!(*xarg_func)(&xdrarg, argsp)) { 4112170Sevanl kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp)); 4122170Sevanl return (EINVAL); 4132170Sevanl } 4140Sstevel@tonic-gate } 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate /* 4172170Sevanl * We're saving off the original pointer and length due to the 4182170Sevanl * possibility that the results buffer returned by the door 4192170Sevanl * upcall can be different then what we passed in. This is because 4202170Sevanl * the door will allocate new memory if the results buffer passed 4212170Sevanl * in isn't large enough to hold what we need to send back. 4222170Sevanl * In this case we need to free the memory originally allocated 4232170Sevanl * for that buffer. 4240Sstevel@tonic-gate */ 4256046Srmesta if (resp) 4266046Srmesta rlen = xdr_sizeof(xresp_func, resp); 4276046Srmesta orl = (rlen == 0) ? doorbuf_defsz : MAX(rlen, doorbuf_defsz); 4286046Srmesta orp = kmem_zalloc(orl, KM_SLEEP); 4290Sstevel@tonic-gate 4300Sstevel@tonic-gate do { 4312170Sevanl retry = 0; 4322170Sevanl mutex_enter(&fngp->fng_autofs_daemon_lock); 4332170Sevanl dh = fngp->fng_autofs_daemon_dh; 4342170Sevanl if (dh) 4352170Sevanl door_ki_hold(dh); 4362170Sevanl mutex_exit(&fngp->fng_autofs_daemon_lock); 4370Sstevel@tonic-gate 4382170Sevanl if (dh == NULL) { 4396046Srmesta if (orp) 4406046Srmesta kmem_free(orp, orl); 4412170Sevanl kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp)); 4422170Sevanl return (ENOENT); 4432170Sevanl } 4442170Sevanl door_args.data_ptr = (char *)xdr_argsp; 4452170Sevanl door_args.data_size = sizeof (*xdr_argsp) + xdr_argsp->xdr_len; 4462170Sevanl door_args.desc_ptr = NULL; 4472170Sevanl door_args.desc_num = 0; 4486046Srmesta door_args.rbuf = orp ? (char *)orp : NULL; 4496046Srmesta door_args.rsize = orl; 4500Sstevel@tonic-gate 4512170Sevanl sigintr(&smask, 1); 4522170Sevanl error = door_ki_upcall(dh, &door_args); 4530Sstevel@tonic-gate sigunintr(&smask); 4540Sstevel@tonic-gate 4552170Sevanl door_ki_rele(dh); 4560Sstevel@tonic-gate 4576046Srmesta /* 4586046Srmesta * Handle daemon errors 4596046Srmesta */ 4602170Sevanl if (!error) { 4616046Srmesta /* 4626046Srmesta * Upcall successful. Let's check for soft errors 4636046Srmesta * from the daemon. We only recover from overflow 4646046Srmesta * type scenarios. Any other errors, we return to 4656046Srmesta * the caller. 4666046Srmesta */ 4672170Sevanl autofs_door_res_t *adr = 4686046Srmesta (autofs_door_res_t *)door_args.rbuf; 4696046Srmesta 4706046Srmesta if (door_args.rbuf != NULL) { 4716046Srmesta int nl; 4726046Srmesta 4736046Srmesta switch (error = adr->res_status) { 4746046Srmesta case 0: /* no error; continue */ 4756046Srmesta break; 4766046Srmesta 4776046Srmesta case EOVERFLOW: 4786046Srmesta /* 4796046Srmesta * orig landing buf not big enough. 4806046Srmesta * xdr_len in XDR_BYTES_PER_UNIT 4816046Srmesta */ 4826046Srmesta if ((nl = adr->xdr_len) > 0 && 4836046Srmesta (btopr(nl) < freemem/64)) { 4846046Srmesta if (orp) 4856046Srmesta kmem_free(orp, orl); 4866046Srmesta orp = kmem_zalloc(nl, KM_SLEEP); 4876046Srmesta orl = nl; 4886046Srmesta retry = 1; 4896046Srmesta break; 4906046Srmesta } 4916046Srmesta /*FALLTHROUGH*/ 4926046Srmesta 4936046Srmesta default: 4946046Srmesta kmem_free(xdr_argsp, 4956046Srmesta xdr_len + sizeof (*xdr_argsp)); 4966046Srmesta if (orp) 4976046Srmesta kmem_free(orp, orl); 4986046Srmesta return (error); 4996046Srmesta } 5002170Sevanl } 5012170Sevanl continue; 5022170Sevanl } 5036046Srmesta 5046046Srmesta /* 5056046Srmesta * no daemon errors; now process door/comm errors (if any) 5066046Srmesta */ 5072170Sevanl switch (error) { 5082170Sevanl case EINTR: 5090Sstevel@tonic-gate /* 5102170Sevanl * interrupts should be handled properly by the 5114092Sevanl * door upcall. If the door doesn't handle the 5124092Sevanl * interupt completely then we need to bail out. 5134092Sevanl */ 5144092Sevanl if (lwp && (ISSIG(curthread, 5154092Sevanl JUSTLOOKING) || MUSTRETURN(curproc, curthread))) { 5164092Sevanl if (ISSIG(curthread, FORREAL) || 5174092Sevanl lwp->lwp_sysabort || 5184092Sevanl MUSTRETURN(curproc, curthread)) { 5194092Sevanl lwp->lwp_sysabort = 0; 5204092Sevanl return (EINTR); 5214092Sevanl } 5224092Sevanl } 5234092Sevanl /* 5242170Sevanl * We may have gotten EINTR for other reasons 5252170Sevanl * like the door being revoked on us. Instead 5262170Sevanl * of trying to extract this out of the door 5272170Sevanl * handle, sleep and try again, if still 5282170Sevanl * revoked we will get EBADF next time 5292170Sevanl * through. 5305891Sraf * 5315891Sraf * If we have a pending cancellation and we don't 5325891Sraf * have cancellation disabled, we will get EINTR 5335891Sraf * forever, no matter how many times we retry, 5345891Sraf * so just get out now if this is the case. 5350Sstevel@tonic-gate */ 5365891Sraf if (schedctl_cancel_pending()) 5375891Sraf break; 5384092Sevanl /* FALLTHROUGH */ 5392170Sevanl case EAGAIN: /* process may be forking */ 5402170Sevanl /* 5412170Sevanl * Back off for a bit 5422170Sevanl */ 5432170Sevanl delay(hz); 5442170Sevanl retry = 1; 5452170Sevanl break; 5462170Sevanl case EBADF: /* Invalid door */ 5472170Sevanl case EINVAL: /* Not a door, wrong target */ 5480Sstevel@tonic-gate /* 5492170Sevanl * A fatal door error, if our failing door 5502170Sevanl * handle is the current door handle, clean 5512170Sevanl * up our state. 5520Sstevel@tonic-gate */ 5532170Sevanl mutex_enter(&fngp->fng_autofs_daemon_lock); 5542170Sevanl if (dh == fngp->fng_autofs_daemon_dh) { 5552170Sevanl door_ki_rele(fngp->fng_autofs_daemon_dh); 5562170Sevanl fngp->fng_autofs_daemon_dh = NULL; 5570Sstevel@tonic-gate } 5582170Sevanl mutex_exit(&fngp->fng_autofs_daemon_lock); 5596046Srmesta AUTOFS_DPRINT((5, "auto_calldaemon error=%d\n", error)); 5602170Sevanl if (hard) { 5612170Sevanl if (!fngp->fng_printed_not_running_msg) { 5626046Srmesta fngp->fng_printed_not_running_msg = 1; 5636046Srmesta zprintf(zoneid, "automountd not " 5646046Srmesta "running, retrying\n"); 5652170Sevanl } 5662170Sevanl delay(hz); 5672170Sevanl retry = 1; 5682170Sevanl break; 5692170Sevanl } else { 5702170Sevanl error = ECONNREFUSED; 5712170Sevanl kmem_free(xdr_argsp, 5726046Srmesta xdr_len + sizeof (*xdr_argsp)); 5736046Srmesta if (orp) 5746046Srmesta kmem_free(orp, orl); 5752170Sevanl return (error); 5762170Sevanl } 5772170Sevanl default: /* Unknown must be fatal */ 5780Sstevel@tonic-gate error = ENOENT; 5792170Sevanl kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp)); 5806046Srmesta if (orp) 5816046Srmesta kmem_free(orp, orl); 5822170Sevanl return (error); 5830Sstevel@tonic-gate } 5842170Sevanl } while (retry); 5850Sstevel@tonic-gate 5862170Sevanl if (fngp->fng_printed_not_running_msg == 1) { 5872170Sevanl fngp->fng_printed_not_running_msg = 0; 5882170Sevanl zprintf(zoneid, "automountd OK\n"); 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate 5916046Srmesta if (orp && orl) { 5922170Sevanl autofs_door_res_t *door_resp; 5936046Srmesta door_resp = (autofs_door_res_t *)door_args.rbuf; 5946046Srmesta 5956046Srmesta if ((void *)door_args.rbuf != orp) 5966046Srmesta kmem_free(orp, orl); 5976046Srmesta 5982170Sevanl xdrmem_create(&xdrres, (char *)&door_resp->xdr_res, 5996046Srmesta door_resp->xdr_len, XDR_DECODE); 6006046Srmesta 6012170Sevanl if (!((*xresp_func)(&xdrres, resp))) 6022170Sevanl error = EINVAL; 6032170Sevanl kmem_free(door_args.rbuf, door_args.rsize); 6042170Sevanl } 6052170Sevanl kmem_free(xdr_argsp, xdr_len + sizeof (*xdr_argsp)); 6060Sstevel@tonic-gate return (error); 6070Sstevel@tonic-gate } 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate static int 6102170Sevanl auto_null_request(fninfo_t *fnip, bool_t hard) 6110Sstevel@tonic-gate { 6120Sstevel@tonic-gate int error; 6132170Sevanl struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals; 6140Sstevel@tonic-gate 6150Sstevel@tonic-gate AUTOFS_DPRINT((4, "\tauto_null_request\n")); 6160Sstevel@tonic-gate 6176046Srmesta error = auto_calldaemon(fngp->fng_zoneid, NULLPROC, 6186046Srmesta xdr_void, NULL, xdr_void, NULL, 0, hard); 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate AUTOFS_DPRINT((5, "\tauto_null_request: error=%d\n", error)); 6210Sstevel@tonic-gate return (error); 6220Sstevel@tonic-gate } 6230Sstevel@tonic-gate 6240Sstevel@tonic-gate static int 6250Sstevel@tonic-gate auto_lookup_request( 6260Sstevel@tonic-gate fninfo_t *fnip, 6270Sstevel@tonic-gate char *key, 6280Sstevel@tonic-gate struct linka *lnp, 6290Sstevel@tonic-gate bool_t hard, 6303391Ssemery bool_t *mountreq, 6313391Ssemery cred_t *cred) 6320Sstevel@tonic-gate { 6332170Sevanl int error; 6342170Sevanl struct autofs_globals *fngp; 6356046Srmesta struct autofs_lookupargs reqst; 6362170Sevanl autofs_lookupres *resp; 6372170Sevanl struct linka *p; 6380Sstevel@tonic-gate 6392170Sevanl 6402170Sevanl AUTOFS_DPRINT((4, "auto_lookup_equest: path=%s name=%s\n", 6410Sstevel@tonic-gate fnip->fi_path, key)); 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate fngp = vntofn(fnip->fi_rootvp)->fn_globals; 6442170Sevanl 6452170Sevanl reqst.map = fnip->fi_map; 6462170Sevanl reqst.path = fnip->fi_path; 6470Sstevel@tonic-gate 6480Sstevel@tonic-gate if (fnip->fi_flags & MF_DIRECT) 6492170Sevanl reqst.name = fnip->fi_key; 6500Sstevel@tonic-gate else 6512170Sevanl reqst.name = key; 6522170Sevanl AUTOFS_DPRINT((4, "auto_lookup_request: using key=%s\n", reqst.name)); 6530Sstevel@tonic-gate 6542170Sevanl reqst.subdir = fnip->fi_subdir; 6552170Sevanl reqst.opts = fnip->fi_opts; 6562170Sevanl reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE; 6573391Ssemery reqst.uid = crgetuid(cred); 6582170Sevanl 6592170Sevanl resp = kmem_zalloc(sizeof (*resp), KM_SLEEP); 6600Sstevel@tonic-gate 6616046Srmesta error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_LOOKUP, 6626046Srmesta xdr_autofs_lookupargs, &reqst, xdr_autofs_lookupres, 6636046Srmesta (void *)resp, sizeof (autofs_lookupres), hard); 6642170Sevanl 6652170Sevanl if (error) { 6662170Sevanl xdr_free(xdr_autofs_lookupres, (char *)resp); 6672170Sevanl kmem_free(resp, sizeof (*resp)); 6682170Sevanl return (error); 6692170Sevanl } 6702170Sevanl 6710Sstevel@tonic-gate if (!error) { 6722170Sevanl fngp->fng_verbose = resp->lu_verbose; 6732170Sevanl switch (resp->lu_res) { 6740Sstevel@tonic-gate case AUTOFS_OK: 6752170Sevanl switch (resp->lu_type.action) { 6760Sstevel@tonic-gate case AUTOFS_MOUNT_RQ: 6770Sstevel@tonic-gate lnp->link = NULL; 6780Sstevel@tonic-gate lnp->dir = NULL; 6790Sstevel@tonic-gate *mountreq = TRUE; 6800Sstevel@tonic-gate break; 6816046Srmesta 6820Sstevel@tonic-gate case AUTOFS_LINK_RQ: 6836046Srmesta p = &resp->lu_type.lookup_result_type_u.lt_linka; 6840Sstevel@tonic-gate lnp->dir = kmem_alloc(strlen(p->dir) + 1, 6856046Srmesta KM_SLEEP); 6860Sstevel@tonic-gate (void) strcpy(lnp->dir, p->dir); 6870Sstevel@tonic-gate lnp->link = kmem_alloc(strlen(p->link) + 1, 6886046Srmesta KM_SLEEP); 6890Sstevel@tonic-gate (void) strcpy(lnp->link, p->link); 6900Sstevel@tonic-gate break; 6916046Srmesta 6920Sstevel@tonic-gate case AUTOFS_NONE: 6930Sstevel@tonic-gate lnp->link = NULL; 6940Sstevel@tonic-gate lnp->dir = NULL; 6950Sstevel@tonic-gate break; 6966046Srmesta 6970Sstevel@tonic-gate default: 6986046Srmesta auto_log(fngp->fng_verbose, fngp->fng_zoneid, 6996046Srmesta CE_WARN, "auto_lookup_request: bad action " 7006046Srmesta "type %d", resp->lu_res); 7010Sstevel@tonic-gate error = ENOENT; 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate break; 7046046Srmesta 7050Sstevel@tonic-gate case AUTOFS_NOENT: 7060Sstevel@tonic-gate error = ENOENT; 7070Sstevel@tonic-gate break; 7086046Srmesta 7090Sstevel@tonic-gate default: 7100Sstevel@tonic-gate error = ENOENT; 7112170Sevanl auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN, 7120Sstevel@tonic-gate "auto_lookup_request: unknown result: %d", 7132170Sevanl resp->lu_res); 7140Sstevel@tonic-gate break; 7150Sstevel@tonic-gate } 7160Sstevel@tonic-gate } 7170Sstevel@tonic-gate done: 7182170Sevanl xdr_free(xdr_autofs_lookupres, (char *)resp); 7192170Sevanl kmem_free(resp, sizeof (*resp)); 7200Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_lookup_request: path=%s name=%s error=%d\n", 7210Sstevel@tonic-gate fnip->fi_path, key, error)); 7220Sstevel@tonic-gate return (error); 7230Sstevel@tonic-gate } 7240Sstevel@tonic-gate 7250Sstevel@tonic-gate static int 7260Sstevel@tonic-gate auto_mount_request( 7270Sstevel@tonic-gate fninfo_t *fnip, 7280Sstevel@tonic-gate char *key, 7290Sstevel@tonic-gate action_list **alpp, 7303391Ssemery cred_t *cred, 7310Sstevel@tonic-gate bool_t hard) 7320Sstevel@tonic-gate { 7332170Sevanl int error; 7342170Sevanl struct autofs_globals *fngp; 7352170Sevanl autofs_lookupargs reqst; 7362170Sevanl autofs_mountres *xdrres = NULL; 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_mount_request: path=%s name=%s\n", 7390Sstevel@tonic-gate fnip->fi_path, key)); 7400Sstevel@tonic-gate 7410Sstevel@tonic-gate fngp = vntofn(fnip->fi_rootvp)->fn_globals; 7422170Sevanl reqst.map = fnip->fi_map; 7432170Sevanl reqst.path = fnip->fi_path; 7440Sstevel@tonic-gate 7450Sstevel@tonic-gate if (fnip->fi_flags & MF_DIRECT) 7462170Sevanl reqst.name = fnip->fi_key; 7470Sstevel@tonic-gate else 7482170Sevanl reqst.name = key; 7492170Sevanl 7502170Sevanl AUTOFS_DPRINT((4, "auto_mount_request: using key=%s\n", reqst.name)); 7510Sstevel@tonic-gate 7522170Sevanl reqst.subdir = fnip->fi_subdir; 7532170Sevanl reqst.opts = fnip->fi_opts; 7542170Sevanl reqst.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE; 7553391Ssemery reqst.uid = crgetuid(cred); 7562170Sevanl 7572170Sevanl xdrres = kmem_zalloc(sizeof (*xdrres), KM_SLEEP); 7580Sstevel@tonic-gate 7596046Srmesta error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_MNTINFO, 7606046Srmesta xdr_autofs_lookupargs, &reqst, xdr_autofs_mountres, 7616046Srmesta (void *)xdrres, sizeof (autofs_mountres), hard); 7622170Sevanl 7630Sstevel@tonic-gate if (!error) { 7642170Sevanl fngp->fng_verbose = xdrres->mr_verbose; 7652170Sevanl switch (xdrres->mr_type.status) { 7660Sstevel@tonic-gate case AUTOFS_ACTION: 7670Sstevel@tonic-gate error = 0; 7680Sstevel@tonic-gate /* 7690Sstevel@tonic-gate * Save the action list since it is used by 7700Sstevel@tonic-gate * the caller. We NULL the action list pointer 7710Sstevel@tonic-gate * in 'result' so that xdr_free() will not free 7720Sstevel@tonic-gate * the list. 7730Sstevel@tonic-gate */ 7742170Sevanl *alpp = xdrres->mr_type.mount_result_type_u.list; 7752170Sevanl xdrres->mr_type.mount_result_type_u.list = NULL; 7760Sstevel@tonic-gate break; 7770Sstevel@tonic-gate case AUTOFS_DONE: 7782170Sevanl error = xdrres->mr_type.mount_result_type_u.error; 7790Sstevel@tonic-gate break; 7800Sstevel@tonic-gate default: 7810Sstevel@tonic-gate error = ENOENT; 7822170Sevanl auto_log(fngp->fng_verbose, fngp->fng_zoneid, CE_WARN, 7830Sstevel@tonic-gate "auto_mount_request: unknown status %d", 7842170Sevanl xdrres->mr_type.status); 7850Sstevel@tonic-gate break; 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate } 7880Sstevel@tonic-gate 7892170Sevanl xdr_free(xdr_autofs_mountres, (char *)xdrres); 7902170Sevanl kmem_free(xdrres, sizeof (*xdrres)); 7912170Sevanl 7920Sstevel@tonic-gate 7930Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_mount_request: path=%s name=%s error=%d\n", 7940Sstevel@tonic-gate fnip->fi_path, key, error)); 7950Sstevel@tonic-gate return (error); 7960Sstevel@tonic-gate } 7970Sstevel@tonic-gate 7980Sstevel@tonic-gate 7990Sstevel@tonic-gate static int 8000Sstevel@tonic-gate auto_send_unmount_request( 8010Sstevel@tonic-gate fninfo_t *fnip, 8020Sstevel@tonic-gate umntrequest *ul, 8030Sstevel@tonic-gate bool_t hard) 8040Sstevel@tonic-gate { 8052170Sevanl int error; 8062170Sevanl umntres xdrres; 8072170Sevanl 8082170Sevanl struct autofs_globals *fngp = vntofn(fnip->fi_rootvp)->fn_globals; 8090Sstevel@tonic-gate 8100Sstevel@tonic-gate AUTOFS_DPRINT((4, "\tauto_send_unmount_request: fstype=%s " 8116046Srmesta " mntpnt=%s\n", ul->fstype, ul->mntpnt)); 8120Sstevel@tonic-gate 8132170Sevanl bzero(&xdrres, sizeof (umntres)); 8146046Srmesta error = auto_calldaemon(fngp->fng_zoneid, AUTOFS_UNMOUNT, 8156046Srmesta xdr_umntrequest, (void *)ul, xdr_umntres, (void *)&xdrres, 8166046Srmesta sizeof (umntres), hard); 8170Sstevel@tonic-gate 8182395Sevanl if (!error) 8192395Sevanl error = xdrres.status; 8202395Sevanl 8210Sstevel@tonic-gate AUTOFS_DPRINT((5, "\tauto_send_unmount_request: error=%d\n", error)); 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate return (error); 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate static int 8270Sstevel@tonic-gate auto_perform_link(fnnode_t *fnp, struct linka *linkp, cred_t *cred) 8280Sstevel@tonic-gate { 8290Sstevel@tonic-gate vnode_t *vp; 8300Sstevel@tonic-gate size_t len; 8310Sstevel@tonic-gate char *tmp; 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate AUTOFS_DPRINT((3, "auto_perform_link: fnp=%p dir=%s link=%s\n", 8340Sstevel@tonic-gate (void *)fnp, linkp->dir, linkp->link)); 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate len = strlen(linkp->link) + 1; /* include '\0' */ 8370Sstevel@tonic-gate tmp = kmem_zalloc(len, KM_SLEEP); 8380Sstevel@tonic-gate (void) kcopy(linkp->link, tmp, len); 8390Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 8400Sstevel@tonic-gate fnp->fn_symlink = tmp; 8410Sstevel@tonic-gate fnp->fn_symlinklen = (uint_t)len; 8420Sstevel@tonic-gate fnp->fn_flags |= MF_THISUID_MATCH_RQD; 8430Sstevel@tonic-gate crhold(cred); 8440Sstevel@tonic-gate fnp->fn_cred = cred; 8450Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 8460Sstevel@tonic-gate 8470Sstevel@tonic-gate vp = fntovn(fnp); 8480Sstevel@tonic-gate vp->v_type = VLNK; 8490Sstevel@tonic-gate 8500Sstevel@tonic-gate return (0); 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate 8534068Sdm120769 static void 8544068Sdm120769 auto_free_autofs_args(struct mounta *m) 8554068Sdm120769 { 8564068Sdm120769 autofs_args *aargs = (autofs_args *)m->dataptr; 8574068Sdm120769 8584068Sdm120769 if (aargs->addr.buf) 8594068Sdm120769 kmem_free(aargs->addr.buf, aargs->addr.len); 8604068Sdm120769 if (aargs->path) 8614068Sdm120769 kmem_free(aargs->path, strlen(aargs->path) + 1); 8624068Sdm120769 if (aargs->opts) 8634068Sdm120769 kmem_free(aargs->opts, strlen(aargs->opts) + 1); 8644068Sdm120769 if (aargs->map) 8654068Sdm120769 kmem_free(aargs->map, strlen(aargs->map) + 1); 8664068Sdm120769 if (aargs->subdir) 8674068Sdm120769 kmem_free(aargs->subdir, strlen(aargs->subdir) + 1); 8684068Sdm120769 if (aargs->key) 8694068Sdm120769 kmem_free(aargs->key, strlen(aargs->key) + 1); 8704068Sdm120769 kmem_free(aargs, sizeof (*aargs)); 8714068Sdm120769 } 8724068Sdm120769 8734068Sdm120769 static void 8744068Sdm120769 auto_free_action_list(action_list *alp) 8754068Sdm120769 { 8764068Sdm120769 struct mounta *m; 8774068Sdm120769 action_list *lastalp; 8784068Sdm120769 char *fstype; 8794068Sdm120769 8804068Sdm120769 m = &alp->action.action_list_entry_u.mounta; 8814068Sdm120769 while (alp != NULL) { 8824068Sdm120769 fstype = alp->action.action_list_entry_u.mounta.fstype; 8834068Sdm120769 m = &alp->action.action_list_entry_u.mounta; 8844068Sdm120769 if (m->dataptr) { 8854068Sdm120769 if (strcmp(fstype, "autofs") == 0) { 8864068Sdm120769 auto_free_autofs_args(m); 8874068Sdm120769 } 8884068Sdm120769 } 8894068Sdm120769 if (m->spec) 8904068Sdm120769 kmem_free(m->spec, strlen(m->spec) + 1); 8914068Sdm120769 if (m->dir) 8924068Sdm120769 kmem_free(m->dir, strlen(m->dir) + 1); 8934068Sdm120769 if (m->fstype) 8944068Sdm120769 kmem_free(m->fstype, strlen(m->fstype) + 1); 8954068Sdm120769 if (m->optptr) 8964068Sdm120769 kmem_free(m->optptr, m->optlen); 8974068Sdm120769 lastalp = alp; 8984068Sdm120769 alp = alp->next; 8994068Sdm120769 kmem_free(lastalp, sizeof (*lastalp)); 9004068Sdm120769 } 9014068Sdm120769 } 9024068Sdm120769 9030Sstevel@tonic-gate static boolean_t 9042170Sevanl auto_invalid_autofs(fninfo_t *dfnip, fnnode_t *dfnp, action_list *p) 9050Sstevel@tonic-gate { 9060Sstevel@tonic-gate struct mounta *m; 9070Sstevel@tonic-gate struct autofs_args *argsp; 9080Sstevel@tonic-gate vnode_t *dvp; 9090Sstevel@tonic-gate char buff[AUTOFS_MAXPATHLEN]; 9100Sstevel@tonic-gate size_t len; 9110Sstevel@tonic-gate struct autofs_globals *fngp; 9124068Sdm120769 9130Sstevel@tonic-gate fngp = dfnp->fn_globals; 9140Sstevel@tonic-gate dvp = fntovn(dfnp); 9152170Sevanl 9160Sstevel@tonic-gate m = &p->action.action_list_entry_u.mounta; 9170Sstevel@tonic-gate /* 9180Sstevel@tonic-gate * Make sure we aren't geting passed NULL values or a "dir" that 9190Sstevel@tonic-gate * isn't "." and doesn't begin with "./". 9200Sstevel@tonic-gate * 9210Sstevel@tonic-gate * We also only want to perform autofs mounts, so make sure 9220Sstevel@tonic-gate * no-one is trying to trick us into doing anything else. 9230Sstevel@tonic-gate */ 9244068Sdm120769 if (m->spec == NULL || m->dir == NULL || m->dir[0] != '.' || 9250Sstevel@tonic-gate (m->dir[1] != '/' && m->dir[1] != '\0') || 9264068Sdm120769 m->fstype == NULL || strcmp(m->fstype, "autofs") != 0 || 9274068Sdm120769 m->dataptr == NULL || m->datalen != sizeof (struct autofs_args) || 9280Sstevel@tonic-gate m->optptr == NULL) 9290Sstevel@tonic-gate return (B_TRUE); 9300Sstevel@tonic-gate /* 9310Sstevel@tonic-gate * We also don't like ".."s in the pathname. Symlinks are 9320Sstevel@tonic-gate * handled by the fact that we'll use NOFOLLOW when we do 9330Sstevel@tonic-gate * lookup()s. 9340Sstevel@tonic-gate */ 9350Sstevel@tonic-gate if (strstr(m->dir, "/../") != NULL || 9360Sstevel@tonic-gate (len = strlen(m->dir)) > sizeof ("/..") - 1 && 9370Sstevel@tonic-gate m->dir[len] == '.' && m->dir[len - 1] == '.' && 9380Sstevel@tonic-gate m->dir[len - 2] == '/') 9390Sstevel@tonic-gate return (B_TRUE); 9400Sstevel@tonic-gate argsp = (struct autofs_args *)m->dataptr; 9410Sstevel@tonic-gate /* 9420Sstevel@tonic-gate * We don't want NULL values here either. 9430Sstevel@tonic-gate */ 9440Sstevel@tonic-gate if (argsp->addr.buf == NULL || argsp->path == NULL || 9450Sstevel@tonic-gate argsp->opts == NULL || argsp->map == NULL || argsp->subdir == NULL) 9460Sstevel@tonic-gate return (B_TRUE); 9470Sstevel@tonic-gate /* 9480Sstevel@tonic-gate * We know what the claimed pathname *should* look like: 9490Sstevel@tonic-gate * 9500Sstevel@tonic-gate * If the parent (dfnp) is a mount point (VROOT), then 9510Sstevel@tonic-gate * the path should be (dfnip->fi_path + m->dir). 9520Sstevel@tonic-gate * 9530Sstevel@tonic-gate * Else, we know we're only two levels deep, so we use 9540Sstevel@tonic-gate * (dfnip->fi_path + dfnp->fn_name + m->dir). 9550Sstevel@tonic-gate * 9560Sstevel@tonic-gate * Furthermore, "." only makes sense if dfnp is a 9570Sstevel@tonic-gate * trigger node. 9580Sstevel@tonic-gate * 9590Sstevel@tonic-gate * At this point it seems like the passed-in path is 9600Sstevel@tonic-gate * redundant. 9610Sstevel@tonic-gate */ 9620Sstevel@tonic-gate if (dvp->v_flag & VROOT) { 9630Sstevel@tonic-gate if (m->dir[1] == '\0' && !(dfnp->fn_flags & MF_TRIGGER)) 9640Sstevel@tonic-gate return (B_TRUE); 9650Sstevel@tonic-gate (void) snprintf(buff, sizeof (buff), "%s%s", 9660Sstevel@tonic-gate dfnip->fi_path, m->dir + 1); 9670Sstevel@tonic-gate } else { 9680Sstevel@tonic-gate (void) snprintf(buff, sizeof (buff), "%s/%s%s", 9690Sstevel@tonic-gate dfnip->fi_path, dfnp->fn_name, m->dir + 1); 9700Sstevel@tonic-gate } 9710Sstevel@tonic-gate if (strcmp(argsp->path, buff) != 0) { 9722170Sevanl auto_log(fngp->fng_verbose, fngp->fng_zoneid, 9732170Sevanl CE_WARN, "autofs: expected path of '%s', " 9740Sstevel@tonic-gate "got '%s' instead.", buff, argsp->path); 9750Sstevel@tonic-gate return (B_TRUE); 9760Sstevel@tonic-gate } 9770Sstevel@tonic-gate return (B_FALSE); /* looks OK */ 9780Sstevel@tonic-gate } 9790Sstevel@tonic-gate 9802170Sevanl /* 9812170Sevanl * auto_invalid_action will validate the action_list received. If all is good 9824068Sdm120769 * this function returns FALSE, if there is a problem it returns TRUE. 9832170Sevanl */ 9842170Sevanl static boolean_t 9852170Sevanl auto_invalid_action(fninfo_t *dfnip, fnnode_t *dfnp, action_list *alistpp) 9862170Sevanl { 9872170Sevanl 9882170Sevanl /* 9892170Sevanl * Before we go any further, this better be a mount request. 9902170Sevanl */ 9912170Sevanl if (alistpp->action.action != AUTOFS_MOUNT_RQ) 9922170Sevanl return (B_TRUE); 9934068Sdm120769 return (auto_invalid_autofs(dfnip, dfnp, alistpp)); 9942170Sevanl 9952170Sevanl } 9962170Sevanl 9970Sstevel@tonic-gate static int 9980Sstevel@tonic-gate auto_perform_actions( 9990Sstevel@tonic-gate fninfo_t *dfnip, 10000Sstevel@tonic-gate fnnode_t *dfnp, 10010Sstevel@tonic-gate action_list *alp, 10020Sstevel@tonic-gate cred_t *cred) /* Credentials of the caller */ 10030Sstevel@tonic-gate { 10042170Sevanl 10050Sstevel@tonic-gate action_list *p; 10062170Sevanl struct mounta *m, margs; 10072170Sevanl struct autofs_args *argsp; 10082170Sevanl int error, success = 0; 10092170Sevanl vnode_t *mvp, *dvp, *newvp; 10102170Sevanl fnnode_t *newfnp, *mfnp; 10112170Sevanl int auto_mount = 0; 10122170Sevanl int save_triggers = 0; 10132170Sevanl int update_times = 0; 10142170Sevanl char *mntpnt; 10152170Sevanl char buff[AUTOFS_MAXPATHLEN]; 10162170Sevanl timestruc_t now; 10172170Sevanl struct autofs_globals *fngp; 10182170Sevanl cred_t *zcred; 10190Sstevel@tonic-gate 10206046Srmesta AUTOFS_DPRINT((4, "auto_perform_actions: alp=%p\n", (void *)alp)); 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate fngp = dfnp->fn_globals; 10230Sstevel@tonic-gate dvp = fntovn(dfnp); 10240Sstevel@tonic-gate 10250Sstevel@tonic-gate /* 10260Sstevel@tonic-gate * As automountd running in a zone may be compromised, and this may be 10270Sstevel@tonic-gate * an attack, we can't trust everything passed in by automountd, and we 10280Sstevel@tonic-gate * need to do argument verification. We'll issue a warning and drop 10290Sstevel@tonic-gate * the request if it doesn't seem right. 10300Sstevel@tonic-gate */ 10312170Sevanl 10320Sstevel@tonic-gate for (p = alp; p != NULL; p = p->next) { 10330Sstevel@tonic-gate if (auto_invalid_action(dfnip, dfnp, p)) { 10340Sstevel@tonic-gate /* 10350Sstevel@tonic-gate * This warning should be sent to the global zone, 10360Sstevel@tonic-gate * since presumably the zone administrator is the same 10370Sstevel@tonic-gate * as the attacker. 10380Sstevel@tonic-gate */ 10390Sstevel@tonic-gate cmn_err(CE_WARN, "autofs: invalid action list received " 10400Sstevel@tonic-gate "by automountd in zone %s.", 10410Sstevel@tonic-gate curproc->p_zone->zone_name); 10420Sstevel@tonic-gate /* 10430Sstevel@tonic-gate * This conversation is over. 10440Sstevel@tonic-gate */ 10450Sstevel@tonic-gate xdr_free(xdr_action_list, (char *)alp); 10460Sstevel@tonic-gate return (EINVAL); 10470Sstevel@tonic-gate } 10480Sstevel@tonic-gate } 10490Sstevel@tonic-gate 10500Sstevel@tonic-gate zcred = zone_get_kcred(getzoneid()); 10510Sstevel@tonic-gate ASSERT(zcred != NULL); 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate if (vn_mountedvfs(dvp) != NULL) { 10540Sstevel@tonic-gate /* 10550Sstevel@tonic-gate * The daemon successfully mounted a filesystem 10560Sstevel@tonic-gate * on the AUTOFS root node. 10570Sstevel@tonic-gate */ 10580Sstevel@tonic-gate mutex_enter(&dfnp->fn_lock); 10590Sstevel@tonic-gate dfnp->fn_flags |= MF_MOUNTPOINT; 10600Sstevel@tonic-gate ASSERT(dfnp->fn_dirents == NULL); 10610Sstevel@tonic-gate mutex_exit(&dfnp->fn_lock); 10620Sstevel@tonic-gate success++; 10630Sstevel@tonic-gate } else { 10640Sstevel@tonic-gate /* 10650Sstevel@tonic-gate * Clear MF_MOUNTPOINT. 10660Sstevel@tonic-gate */ 10670Sstevel@tonic-gate mutex_enter(&dfnp->fn_lock); 10680Sstevel@tonic-gate if (dfnp->fn_flags & MF_MOUNTPOINT) { 10690Sstevel@tonic-gate AUTOFS_DPRINT((10, "autofs: clearing mountpoint " 10706046Srmesta "flag on %s.", dfnp->fn_name)); 10710Sstevel@tonic-gate ASSERT(dfnp->fn_dirents == NULL); 10720Sstevel@tonic-gate ASSERT(dfnp->fn_trigger == NULL); 10730Sstevel@tonic-gate } 10740Sstevel@tonic-gate dfnp->fn_flags &= ~MF_MOUNTPOINT; 10750Sstevel@tonic-gate mutex_exit(&dfnp->fn_lock); 10760Sstevel@tonic-gate } 10770Sstevel@tonic-gate 10780Sstevel@tonic-gate for (p = alp; p != NULL; p = p->next) { 10792170Sevanl 10800Sstevel@tonic-gate vfs_t *vfsp; /* dummy argument */ 10810Sstevel@tonic-gate vfs_t *mvfsp; 10820Sstevel@tonic-gate 10830Sstevel@tonic-gate auto_mount = 0; 10840Sstevel@tonic-gate 10850Sstevel@tonic-gate m = &p->action.action_list_entry_u.mounta; 10860Sstevel@tonic-gate argsp = (struct autofs_args *)m->dataptr; 10874068Sdm120769 ASSERT(strcmp(m->fstype, "autofs") == 0); 10884068Sdm120769 /* 10894068Sdm120769 * use the parent directory's timeout since it's the 10904068Sdm120769 * one specified/inherited by automount. 10914068Sdm120769 */ 10924068Sdm120769 argsp->mount_to = dfnip->fi_mount_to; 10934068Sdm120769 /* 10944068Sdm120769 * The mountpoint is relative, and it is guaranteed to 10954068Sdm120769 * begin with "." 10964068Sdm120769 * 10974068Sdm120769 */ 10984068Sdm120769 ASSERT(m->dir[0] == '.'); 10994068Sdm120769 if (m->dir[0] == '.' && m->dir[1] == '\0') { 11000Sstevel@tonic-gate /* 11014068Sdm120769 * mounting on the trigger node 11020Sstevel@tonic-gate */ 11034068Sdm120769 mvp = dvp; 11044068Sdm120769 VN_HOLD(mvp); 11054068Sdm120769 goto mount; 11064068Sdm120769 } 11074068Sdm120769 /* 11084068Sdm120769 * ignore "./" in front of mountpoint 11094068Sdm120769 */ 11104068Sdm120769 ASSERT(m->dir[1] == '/'); 11114068Sdm120769 mntpnt = m->dir + 2; 11120Sstevel@tonic-gate 11134068Sdm120769 AUTOFS_DPRINT((10, "\tdfnip->fi_path=%s\n", dfnip->fi_path)); 11144068Sdm120769 AUTOFS_DPRINT((10, "\tdfnip->fi_flags=%x\n", dfnip->fi_flags)); 11154068Sdm120769 AUTOFS_DPRINT((10, "\tmntpnt=%s\n", mntpnt)); 11160Sstevel@tonic-gate 11174068Sdm120769 if (dfnip->fi_flags & MF_DIRECT) { 11184068Sdm120769 AUTOFS_DPRINT((10, "\tDIRECT\n")); 11196046Srmesta (void) sprintf(buff, "%s/%s", dfnip->fi_path, mntpnt); 11204068Sdm120769 } else { 11214068Sdm120769 AUTOFS_DPRINT((10, "\tINDIRECT\n")); 11224068Sdm120769 (void) sprintf(buff, "%s/%s/%s", 11236046Srmesta dfnip->fi_path, dfnp->fn_name, mntpnt); 11244068Sdm120769 } 11250Sstevel@tonic-gate 11264068Sdm120769 if (vn_mountedvfs(dvp) == NULL) { 11274068Sdm120769 /* 11284068Sdm120769 * Daemon didn't mount anything on the root 11294068Sdm120769 * We have to create the mountpoint if it 11304068Sdm120769 * doesn't exist already 11314068Sdm120769 * 11324068Sdm120769 * We use the caller's credentials in case a 11334068Sdm120769 * UID-match is required 11344068Sdm120769 * (MF_THISUID_MATCH_RQD). 11354068Sdm120769 */ 11364068Sdm120769 rw_enter(&dfnp->fn_rwlock, RW_WRITER); 11374068Sdm120769 error = auto_search(dfnp, mntpnt, &mfnp, cred); 11384068Sdm120769 if (error == 0) { 11390Sstevel@tonic-gate /* 11404068Sdm120769 * AUTOFS mountpoint exists 11410Sstevel@tonic-gate */ 11424068Sdm120769 if (vn_mountedvfs(fntovn(mfnp)) != NULL) { 11434068Sdm120769 cmn_err(CE_PANIC, 11446046Srmesta "auto_perform_actions:" 11456046Srmesta " mfnp=%p covered", (void *)mfnp); 11460Sstevel@tonic-gate } 11470Sstevel@tonic-gate } else { 11480Sstevel@tonic-gate /* 11494068Sdm120769 * Create AUTOFS mountpoint 11500Sstevel@tonic-gate */ 11514068Sdm120769 ASSERT((dfnp->fn_flags & MF_MOUNTPOINT) == 0); 11524068Sdm120769 error = auto_enter(dfnp, mntpnt, &mfnp, cred); 11534068Sdm120769 ASSERT(mfnp->fn_linkcnt == 1); 11544068Sdm120769 mfnp->fn_linkcnt++; 11554068Sdm120769 } 11564068Sdm120769 if (!error) 11574068Sdm120769 update_times = 1; 11584068Sdm120769 rw_exit(&dfnp->fn_rwlock); 11594068Sdm120769 ASSERT(error != EEXIST); 11604068Sdm120769 if (!error) { 11614068Sdm120769 /* 11624068Sdm120769 * mfnp is already held. 11634068Sdm120769 */ 11644068Sdm120769 mvp = fntovn(mfnp); 11654068Sdm120769 } else { 11664068Sdm120769 auto_log(fngp->fng_verbose, fngp->fng_zoneid, 11676046Srmesta CE_WARN, "autofs: mount of %s " 11686046Srmesta "failed - can't create" 11696046Srmesta " mountpoint.", buff); 11706046Srmesta continue; 11710Sstevel@tonic-gate } 11724068Sdm120769 } else { 11734068Sdm120769 /* 11744068Sdm120769 * Find mountpoint in VFS mounted here. If not 11754068Sdm120769 * found, fail the submount, though the overall 11764068Sdm120769 * mount has succeeded since the root is 11774068Sdm120769 * mounted. 11784068Sdm120769 */ 11796046Srmesta if (error = auto_getmntpnt(dvp, mntpnt, &mvp, kcred)) { 11806046Srmesta auto_log(fngp->fng_verbose, fngp->fng_zoneid, 11816046Srmesta CE_WARN, "autofs: mount of %s " 11826046Srmesta "failed - mountpoint doesn't" 11836046Srmesta " exist.", buff); 11844068Sdm120769 continue; 11854068Sdm120769 } 11864068Sdm120769 if (mvp->v_type == VLNK) { 11876046Srmesta auto_log(fngp->fng_verbose, fngp->fng_zoneid, 11886046Srmesta CE_WARN, "autofs: %s symbolic " 11896046Srmesta "link: not a valid mountpoint " 11906046Srmesta "- mount failed", buff); 11914068Sdm120769 VN_RELE(mvp); 11924068Sdm120769 error = ENOENT; 11934068Sdm120769 continue; 11944068Sdm120769 } 11954068Sdm120769 } 11960Sstevel@tonic-gate mount: 11974068Sdm120769 m->flags |= MS_SYSSPACE | MS_OPTIONSTR; 11984068Sdm120769 11994068Sdm120769 /* 12004068Sdm120769 * Copy mounta struct here so we can substitute a 12014068Sdm120769 * buffer that is large enough to hold the returned 12024068Sdm120769 * option string, if that string is longer than the 12034068Sdm120769 * input option string. 12044068Sdm120769 * This can happen if there are default options enabled 12054068Sdm120769 * that were not in the input option string. 12064068Sdm120769 */ 12074068Sdm120769 bcopy(m, &margs, sizeof (*m)); 12084068Sdm120769 margs.optptr = kmem_alloc(MAX_MNTOPT_STR, KM_SLEEP); 12094068Sdm120769 margs.optlen = MAX_MNTOPT_STR; 12104068Sdm120769 (void) strcpy(margs.optptr, m->optptr); 12114068Sdm120769 margs.dir = argsp->path; 12124068Sdm120769 12134068Sdm120769 /* 12144068Sdm120769 * We use the zone's kcred because we don't want the 12154068Sdm120769 * zone to be able to thus do something it wouldn't 12164068Sdm120769 * normally be able to. 12174068Sdm120769 */ 12184068Sdm120769 error = domount(NULL, &margs, mvp, zcred, &vfsp); 12194068Sdm120769 kmem_free(margs.optptr, MAX_MNTOPT_STR); 12204068Sdm120769 if (error != 0) { 12214068Sdm120769 auto_log(fngp->fng_verbose, fngp->fng_zoneid, 12226046Srmesta CE_WARN, "autofs: domount of %s failed " 12236046Srmesta "error=%d", buff, error); 12244068Sdm120769 VN_RELE(mvp); 12254068Sdm120769 continue; 12264068Sdm120769 } 12274068Sdm120769 VFS_RELE(vfsp); 12282170Sevanl 12294068Sdm120769 /* 12304068Sdm120769 * If mountpoint is an AUTOFS node, then I'm going to 12314068Sdm120769 * flag it that the Filesystem mounted on top was 12324068Sdm120769 * mounted in the kernel so that the unmount can be 12334068Sdm120769 * done inside the kernel as well. 12344068Sdm120769 * I don't care to flag non-AUTOFS mountpoints when an 12354068Sdm120769 * AUTOFS in-kernel mount was done on top, because the 12364068Sdm120769 * unmount routine already knows that such case was 12374068Sdm120769 * done in the kernel. 12384068Sdm120769 */ 12396046Srmesta if (vfs_matchops(dvp->v_vfsp, vfs_getops(mvp->v_vfsp))) { 12404068Sdm120769 mfnp = vntofn(mvp); 12414068Sdm120769 mutex_enter(&mfnp->fn_lock); 12424068Sdm120769 mfnp->fn_flags |= MF_IK_MOUNT; 12434068Sdm120769 mutex_exit(&mfnp->fn_lock); 12444068Sdm120769 } 12450Sstevel@tonic-gate 12464068Sdm120769 (void) vn_vfswlock_wait(mvp); 12474068Sdm120769 mvfsp = vn_mountedvfs(mvp); 12484068Sdm120769 if (mvfsp != NULL) { 12494068Sdm120769 vfs_lock_wait(mvfsp); 12504068Sdm120769 vn_vfsunlock(mvp); 12514068Sdm120769 error = VFS_ROOT(mvfsp, &newvp); 12524068Sdm120769 vfs_unlock(mvfsp); 12534068Sdm120769 if (error) { 12544068Sdm120769 /* 12554068Sdm120769 * We've dropped the locks, so let's 12564068Sdm120769 * get the mounted vfs again in case 12574068Sdm120769 * it changed. 12584068Sdm120769 */ 12594068Sdm120769 (void) vn_vfswlock_wait(mvp); 12604068Sdm120769 mvfsp = vn_mountedvfs(mvp); 12614068Sdm120769 if (mvfsp != NULL) { 12624068Sdm120769 error = dounmount(mvfsp, 0, CRED()); 12634068Sdm120769 if (error) { 12644068Sdm120769 cmn_err(CE_WARN, 12656046Srmesta "autofs: could not unmount" 12666046Srmesta " vfs=%p", (void *)mvfsp); 12674068Sdm120769 } 12684068Sdm120769 } else 12694068Sdm120769 vn_vfsunlock(mvp); 12700Sstevel@tonic-gate VN_RELE(mvp); 12710Sstevel@tonic-gate continue; 12720Sstevel@tonic-gate } 12734068Sdm120769 } else { 12744068Sdm120769 vn_vfsunlock(mvp); 12754068Sdm120769 VN_RELE(mvp); 12764068Sdm120769 continue; 12774068Sdm120769 } 12783901Skr143551 12794068Sdm120769 auto_mount = vfs_matchops(dvp->v_vfsp, 12806046Srmesta vfs_getops(newvp->v_vfsp)); 12814068Sdm120769 newfnp = vntofn(newvp); 12824068Sdm120769 newfnp->fn_parent = dfnp; 12833901Skr143551 12844068Sdm120769 /* 12854068Sdm120769 * At this time we want to save the AUTOFS filesystem 12864068Sdm120769 * as a trigger node. (We only do this if the mount 12874068Sdm120769 * occurred on a node different from the root. 12884068Sdm120769 * We look at the trigger nodes during 12894068Sdm120769 * the automatic unmounting to make sure we remove them 12904068Sdm120769 * as a unit and remount them as a unit if the 12914068Sdm120769 * filesystem mounted at the root could not be 12924068Sdm120769 * unmounted. 12934068Sdm120769 */ 12944068Sdm120769 if (auto_mount && (error == 0) && (mvp != dvp)) { 12954068Sdm120769 save_triggers++; 12960Sstevel@tonic-gate /* 12974068Sdm120769 * Add AUTOFS mount to hierarchy 12980Sstevel@tonic-gate */ 12994068Sdm120769 newfnp->fn_flags |= MF_TRIGGER; 13004068Sdm120769 rw_enter(&newfnp->fn_rwlock, RW_WRITER); 13014068Sdm120769 newfnp->fn_next = dfnp->fn_trigger; 13024068Sdm120769 rw_exit(&newfnp->fn_rwlock); 13034068Sdm120769 rw_enter(&dfnp->fn_rwlock, RW_WRITER); 13044068Sdm120769 dfnp->fn_trigger = newfnp; 13054068Sdm120769 rw_exit(&dfnp->fn_rwlock); 13064068Sdm120769 /* 13074068Sdm120769 * Don't VN_RELE(newvp) here since dfnp now 13084068Sdm120769 * holds reference to it as its trigger node. 13094068Sdm120769 */ 13104068Sdm120769 AUTOFS_DPRINT((10, "\tadding trigger %s to %s\n", 13116046Srmesta newfnp->fn_name, dfnp->fn_name)); 13124068Sdm120769 AUTOFS_DPRINT((10, "\tfirst trigger is %s\n", 13136046Srmesta dfnp->fn_trigger->fn_name)); 13144068Sdm120769 if (newfnp->fn_next != NULL) 13156046Srmesta AUTOFS_DPRINT((10, "\tnext trigger is %s\n", 13166046Srmesta newfnp->fn_next->fn_name)); 13174068Sdm120769 else 13186046Srmesta AUTOFS_DPRINT((10, "\tno next trigger\n")); 13194068Sdm120769 } else 13204068Sdm120769 VN_RELE(newvp); 13210Sstevel@tonic-gate 13224068Sdm120769 if (!error) 13234068Sdm120769 success++; 13240Sstevel@tonic-gate 13254068Sdm120769 if (update_times) { 13264068Sdm120769 gethrestime(&now); 13274068Sdm120769 dfnp->fn_atime = dfnp->fn_mtime = now; 13284068Sdm120769 } 13293901Skr143551 13304068Sdm120769 VN_RELE(mvp); 13310Sstevel@tonic-gate } 13320Sstevel@tonic-gate 13330Sstevel@tonic-gate if (save_triggers) { 13340Sstevel@tonic-gate /* 13350Sstevel@tonic-gate * Make sure the parent can't be freed while it has triggers. 13360Sstevel@tonic-gate */ 13370Sstevel@tonic-gate VN_HOLD(dvp); 13380Sstevel@tonic-gate } 13390Sstevel@tonic-gate 13400Sstevel@tonic-gate crfree(zcred); 13410Sstevel@tonic-gate 13420Sstevel@tonic-gate done: 13430Sstevel@tonic-gate /* 13440Sstevel@tonic-gate * Return failure if daemon didn't mount anything, and all 13450Sstevel@tonic-gate * kernel mounts attempted failed. 13460Sstevel@tonic-gate */ 13470Sstevel@tonic-gate error = success ? 0 : ENOENT; 13480Sstevel@tonic-gate 13490Sstevel@tonic-gate if (alp != NULL) { 13500Sstevel@tonic-gate if ((error == 0) && save_triggers) { 13510Sstevel@tonic-gate /* 13520Sstevel@tonic-gate * Save action_list information, so that we can use it 13530Sstevel@tonic-gate * when it comes time to remount the trigger nodes 13540Sstevel@tonic-gate * The action list is freed when the directory node 13550Sstevel@tonic-gate * containing the reference to it is unmounted in 13560Sstevel@tonic-gate * unmount_tree(). 13570Sstevel@tonic-gate */ 13580Sstevel@tonic-gate mutex_enter(&dfnp->fn_lock); 13590Sstevel@tonic-gate ASSERT(dfnp->fn_alp == NULL); 13600Sstevel@tonic-gate dfnp->fn_alp = alp; 13610Sstevel@tonic-gate mutex_exit(&dfnp->fn_lock); 13620Sstevel@tonic-gate } else { 13630Sstevel@tonic-gate /* 13640Sstevel@tonic-gate * free the action list now, 13650Sstevel@tonic-gate */ 13660Sstevel@tonic-gate xdr_free(xdr_action_list, (char *)alp); 13670Sstevel@tonic-gate } 13680Sstevel@tonic-gate } 13690Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_perform_actions: error=%d\n", error)); 13700Sstevel@tonic-gate return (error); 13710Sstevel@tonic-gate } 13720Sstevel@tonic-gate 13730Sstevel@tonic-gate fnnode_t * 13740Sstevel@tonic-gate auto_makefnnode( 13750Sstevel@tonic-gate vtype_t type, 13760Sstevel@tonic-gate vfs_t *vfsp, 13770Sstevel@tonic-gate char *name, 13780Sstevel@tonic-gate cred_t *cred, 13790Sstevel@tonic-gate struct autofs_globals *fngp) 13800Sstevel@tonic-gate { 13810Sstevel@tonic-gate fnnode_t *fnp; 13820Sstevel@tonic-gate vnode_t *vp; 13830Sstevel@tonic-gate char *tmpname; 13840Sstevel@tonic-gate timestruc_t now; 13850Sstevel@tonic-gate /* 13860Sstevel@tonic-gate * autofs uses odd inode numbers 13870Sstevel@tonic-gate * automountd uses even inode numbers 13880Sstevel@tonic-gate * 13890Sstevel@tonic-gate * To preserve the age-old semantics that inum+devid is unique across 13900Sstevel@tonic-gate * the system, this variable must be global across zones. 13910Sstevel@tonic-gate */ 13920Sstevel@tonic-gate static ino_t nodeid = 3; 13930Sstevel@tonic-gate 13940Sstevel@tonic-gate fnp = kmem_zalloc(sizeof (*fnp), KM_SLEEP); 13950Sstevel@tonic-gate fnp->fn_vnode = vn_alloc(KM_SLEEP); 13960Sstevel@tonic-gate 13970Sstevel@tonic-gate vp = fntovn(fnp); 13980Sstevel@tonic-gate tmpname = kmem_alloc(strlen(name) + 1, KM_SLEEP); 13990Sstevel@tonic-gate (void) strcpy(tmpname, name); 14000Sstevel@tonic-gate fnp->fn_name = &tmpname[0]; 14010Sstevel@tonic-gate fnp->fn_namelen = (int)strlen(tmpname) + 1; /* include '\0' */ 14020Sstevel@tonic-gate fnp->fn_uid = crgetuid(cred); 14030Sstevel@tonic-gate fnp->fn_gid = crgetgid(cred); 14040Sstevel@tonic-gate /* 14050Sstevel@tonic-gate * ".." is added in auto_enter and auto_mount. 14060Sstevel@tonic-gate * "." is added in auto_mkdir and auto_mount. 14070Sstevel@tonic-gate */ 14080Sstevel@tonic-gate /* 14090Sstevel@tonic-gate * Note that fn_size and fn_linkcnt are already 0 since 14100Sstevel@tonic-gate * we used kmem_zalloc to allocated fnp 14110Sstevel@tonic-gate */ 14120Sstevel@tonic-gate fnp->fn_mode = AUTOFS_MODE; 14130Sstevel@tonic-gate gethrestime(&now); 14140Sstevel@tonic-gate fnp->fn_atime = fnp->fn_mtime = fnp->fn_ctime = now; 14150Sstevel@tonic-gate fnp->fn_ref_time = now.tv_sec; 14160Sstevel@tonic-gate mutex_enter(&autofs_nodeid_lock); 14170Sstevel@tonic-gate fnp->fn_nodeid = nodeid; 14180Sstevel@tonic-gate nodeid += 2; 14190Sstevel@tonic-gate fnp->fn_globals = fngp; 14200Sstevel@tonic-gate fngp->fng_fnnode_count++; 14210Sstevel@tonic-gate mutex_exit(&autofs_nodeid_lock); 14220Sstevel@tonic-gate vn_setops(vp, auto_vnodeops); 14230Sstevel@tonic-gate vp->v_type = type; 14240Sstevel@tonic-gate vp->v_data = (void *)fnp; 14250Sstevel@tonic-gate vp->v_vfsp = vfsp; 14260Sstevel@tonic-gate mutex_init(&fnp->fn_lock, NULL, MUTEX_DEFAULT, NULL); 14270Sstevel@tonic-gate rw_init(&fnp->fn_rwlock, NULL, RW_DEFAULT, NULL); 14280Sstevel@tonic-gate cv_init(&fnp->fn_cv_mount, NULL, CV_DEFAULT, NULL); 14290Sstevel@tonic-gate vn_exists(vp); 14300Sstevel@tonic-gate return (fnp); 14310Sstevel@tonic-gate } 14320Sstevel@tonic-gate 14330Sstevel@tonic-gate 14340Sstevel@tonic-gate void 14350Sstevel@tonic-gate auto_freefnnode(fnnode_t *fnp) 14360Sstevel@tonic-gate { 14370Sstevel@tonic-gate vnode_t *vp = fntovn(fnp); 14380Sstevel@tonic-gate 14390Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_freefnnode: fnp=%p\n", (void *)fnp)); 14400Sstevel@tonic-gate 14410Sstevel@tonic-gate ASSERT(fnp->fn_linkcnt == 0); 14420Sstevel@tonic-gate ASSERT(vp->v_count == 0); 14430Sstevel@tonic-gate ASSERT(fnp->fn_dirents == NULL); 14440Sstevel@tonic-gate ASSERT(fnp->fn_parent == NULL); 14450Sstevel@tonic-gate 14460Sstevel@tonic-gate vn_invalid(vp); 14470Sstevel@tonic-gate kmem_free(fnp->fn_name, fnp->fn_namelen); 14480Sstevel@tonic-gate if (fnp->fn_symlink) { 14490Sstevel@tonic-gate ASSERT(fnp->fn_flags & MF_THISUID_MATCH_RQD); 14500Sstevel@tonic-gate kmem_free(fnp->fn_symlink, fnp->fn_symlinklen); 14510Sstevel@tonic-gate } 14520Sstevel@tonic-gate if (fnp->fn_cred) 14530Sstevel@tonic-gate crfree(fnp->fn_cred); 14540Sstevel@tonic-gate mutex_destroy(&fnp->fn_lock); 14550Sstevel@tonic-gate rw_destroy(&fnp->fn_rwlock); 14560Sstevel@tonic-gate cv_destroy(&fnp->fn_cv_mount); 14570Sstevel@tonic-gate vn_free(vp); 14580Sstevel@tonic-gate 14590Sstevel@tonic-gate mutex_enter(&autofs_nodeid_lock); 14600Sstevel@tonic-gate fnp->fn_globals->fng_fnnode_count--; 14610Sstevel@tonic-gate mutex_exit(&autofs_nodeid_lock); 14620Sstevel@tonic-gate kmem_free(fnp, sizeof (*fnp)); 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate void 14660Sstevel@tonic-gate auto_disconnect( 14670Sstevel@tonic-gate fnnode_t *dfnp, 14680Sstevel@tonic-gate fnnode_t *fnp) 14690Sstevel@tonic-gate { 14700Sstevel@tonic-gate fnnode_t *tmp, **fnpp; 14710Sstevel@tonic-gate vnode_t *vp = fntovn(fnp); 14720Sstevel@tonic-gate timestruc_t now; 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate AUTOFS_DPRINT((4, 14750Sstevel@tonic-gate "auto_disconnect: dfnp=%p fnp=%p linkcnt=%d\n v_count=%d", 14760Sstevel@tonic-gate (void *)dfnp, (void *)fnp, fnp->fn_linkcnt, vp->v_count)); 14770Sstevel@tonic-gate 14780Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock)); 14790Sstevel@tonic-gate ASSERT(fnp->fn_linkcnt == 1); 14800Sstevel@tonic-gate 14810Sstevel@tonic-gate if (vn_mountedvfs(vp) != NULL) { 14820Sstevel@tonic-gate cmn_err(CE_PANIC, "auto_disconnect: vp %p mounted on", 14830Sstevel@tonic-gate (void *)vp); 14840Sstevel@tonic-gate } 14850Sstevel@tonic-gate 14860Sstevel@tonic-gate /* 14870Sstevel@tonic-gate * Decrement by 1 because we're removing the entry in dfnp. 14880Sstevel@tonic-gate */ 14890Sstevel@tonic-gate fnp->fn_linkcnt--; 14900Sstevel@tonic-gate fnp->fn_size--; 14910Sstevel@tonic-gate 14920Sstevel@tonic-gate /* 14930Sstevel@tonic-gate * only changed while holding parent's (dfnp) rw_lock 14940Sstevel@tonic-gate */ 14950Sstevel@tonic-gate fnp->fn_parent = NULL; 14960Sstevel@tonic-gate 14970Sstevel@tonic-gate fnpp = &dfnp->fn_dirents; 14980Sstevel@tonic-gate for (;;) { 14990Sstevel@tonic-gate tmp = *fnpp; 15000Sstevel@tonic-gate if (tmp == NULL) { 15010Sstevel@tonic-gate cmn_err(CE_PANIC, 15020Sstevel@tonic-gate "auto_disconnect: %p not in %p dirent list", 15030Sstevel@tonic-gate (void *)fnp, (void *)dfnp); 15040Sstevel@tonic-gate } 15050Sstevel@tonic-gate if (tmp == fnp) { 15060Sstevel@tonic-gate *fnpp = tmp->fn_next; /* remove it from the list */ 15070Sstevel@tonic-gate ASSERT(vp->v_count == 0); 15080Sstevel@tonic-gate /* child had a pointer to parent ".." */ 15090Sstevel@tonic-gate dfnp->fn_linkcnt--; 15100Sstevel@tonic-gate dfnp->fn_size--; 15110Sstevel@tonic-gate break; 15120Sstevel@tonic-gate } 15130Sstevel@tonic-gate fnpp = &tmp->fn_next; 15140Sstevel@tonic-gate } 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 15170Sstevel@tonic-gate gethrestime(&now); 15180Sstevel@tonic-gate fnp->fn_atime = fnp->fn_mtime = now; 15190Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 15200Sstevel@tonic-gate 15210Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_disconnect: done\n")); 15220Sstevel@tonic-gate } 15230Sstevel@tonic-gate 15240Sstevel@tonic-gate int 15250Sstevel@tonic-gate auto_enter(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred) 15260Sstevel@tonic-gate { 15270Sstevel@tonic-gate struct fnnode *cfnp, **spp; 15280Sstevel@tonic-gate vnode_t *dvp = fntovn(dfnp); 15290Sstevel@tonic-gate ushort_t offset = 0; 15300Sstevel@tonic-gate ushort_t diff; 15310Sstevel@tonic-gate 15320Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_enter: dfnp=%p, name=%s ", (void *)dfnp, name)); 15330Sstevel@tonic-gate 15340Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&dfnp->fn_rwlock)); 15350Sstevel@tonic-gate 15360Sstevel@tonic-gate cfnp = dfnp->fn_dirents; 15370Sstevel@tonic-gate if (cfnp == NULL) { 15380Sstevel@tonic-gate /* 15390Sstevel@tonic-gate * offset = 0 for '.' and offset = 1 for '..' 15400Sstevel@tonic-gate */ 15410Sstevel@tonic-gate spp = &dfnp->fn_dirents; 15420Sstevel@tonic-gate offset = 2; 15430Sstevel@tonic-gate } 15440Sstevel@tonic-gate 15450Sstevel@tonic-gate for (; cfnp; cfnp = cfnp->fn_next) { 15460Sstevel@tonic-gate if (strcmp(cfnp->fn_name, name) == 0) { 15470Sstevel@tonic-gate mutex_enter(&cfnp->fn_lock); 15480Sstevel@tonic-gate if (cfnp->fn_flags & MF_THISUID_MATCH_RQD) { 15490Sstevel@tonic-gate /* 15500Sstevel@tonic-gate * "thisuser" kind of node, need to 15510Sstevel@tonic-gate * match CREDs as well 15520Sstevel@tonic-gate */ 15530Sstevel@tonic-gate mutex_exit(&cfnp->fn_lock); 15540Sstevel@tonic-gate if (crcmp(cfnp->fn_cred, cred) == 0) 15550Sstevel@tonic-gate return (EEXIST); 15560Sstevel@tonic-gate } else { 15570Sstevel@tonic-gate mutex_exit(&cfnp->fn_lock); 15580Sstevel@tonic-gate return (EEXIST); 15590Sstevel@tonic-gate } 15600Sstevel@tonic-gate } 15610Sstevel@tonic-gate 15620Sstevel@tonic-gate if (cfnp->fn_next != NULL) { 15630Sstevel@tonic-gate diff = (ushort_t) 15640Sstevel@tonic-gate (cfnp->fn_next->fn_offset - cfnp->fn_offset); 15650Sstevel@tonic-gate ASSERT(diff != 0); 15660Sstevel@tonic-gate if (diff > 1 && offset == 0) { 15670Sstevel@tonic-gate offset = (ushort_t)cfnp->fn_offset + 1; 15680Sstevel@tonic-gate spp = &cfnp->fn_next; 15690Sstevel@tonic-gate } 15700Sstevel@tonic-gate } else if (offset == 0) { 15710Sstevel@tonic-gate offset = (ushort_t)cfnp->fn_offset + 1; 15720Sstevel@tonic-gate spp = &cfnp->fn_next; 15730Sstevel@tonic-gate } 15740Sstevel@tonic-gate } 15750Sstevel@tonic-gate 15760Sstevel@tonic-gate *fnpp = auto_makefnnode(VDIR, dvp->v_vfsp, name, cred, 15770Sstevel@tonic-gate dfnp->fn_globals); 15780Sstevel@tonic-gate if (*fnpp == NULL) 15790Sstevel@tonic-gate return (ENOMEM); 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate /* 15820Sstevel@tonic-gate * I don't hold the mutex on fnpp because I created it, and 15830Sstevel@tonic-gate * I'm already holding the writers lock for it's parent 15840Sstevel@tonic-gate * directory, therefore nobody can reference it without me first 15850Sstevel@tonic-gate * releasing the writers lock. 15860Sstevel@tonic-gate */ 15870Sstevel@tonic-gate (*fnpp)->fn_offset = offset; 15880Sstevel@tonic-gate (*fnpp)->fn_next = *spp; 15890Sstevel@tonic-gate *spp = *fnpp; 15900Sstevel@tonic-gate (*fnpp)->fn_parent = dfnp; 15910Sstevel@tonic-gate (*fnpp)->fn_linkcnt++; /* parent now holds reference to entry */ 15920Sstevel@tonic-gate (*fnpp)->fn_size++; 15930Sstevel@tonic-gate 15940Sstevel@tonic-gate /* 15950Sstevel@tonic-gate * dfnp->fn_linkcnt and dfnp->fn_size protected by dfnp->rw_lock 15960Sstevel@tonic-gate */ 15970Sstevel@tonic-gate dfnp->fn_linkcnt++; /* child now holds reference to parent '..' */ 15980Sstevel@tonic-gate dfnp->fn_size++; 15990Sstevel@tonic-gate 16000Sstevel@tonic-gate dfnp->fn_ref_time = gethrestime_sec(); 16010Sstevel@tonic-gate 16020Sstevel@tonic-gate AUTOFS_DPRINT((5, "*fnpp=%p\n", (void *)*fnpp)); 16030Sstevel@tonic-gate return (0); 16040Sstevel@tonic-gate } 16050Sstevel@tonic-gate 16060Sstevel@tonic-gate int 16070Sstevel@tonic-gate auto_search(fnnode_t *dfnp, char *name, fnnode_t **fnpp, cred_t *cred) 16080Sstevel@tonic-gate { 16090Sstevel@tonic-gate vnode_t *dvp; 16100Sstevel@tonic-gate fnnode_t *p; 16110Sstevel@tonic-gate int error = ENOENT, match = 0; 16120Sstevel@tonic-gate 16130Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_search: dfnp=%p, name=%s...\n", 16140Sstevel@tonic-gate (void *)dfnp, name)); 16150Sstevel@tonic-gate 16160Sstevel@tonic-gate dvp = fntovn(dfnp); 16170Sstevel@tonic-gate if (dvp->v_type != VDIR) { 16180Sstevel@tonic-gate cmn_err(CE_PANIC, "auto_search: dvp=%p not a directory", 16190Sstevel@tonic-gate (void *)dvp); 16200Sstevel@tonic-gate } 16210Sstevel@tonic-gate 16220Sstevel@tonic-gate ASSERT(RW_LOCK_HELD(&dfnp->fn_rwlock)); 16230Sstevel@tonic-gate for (p = dfnp->fn_dirents; p != NULL; p = p->fn_next) { 16240Sstevel@tonic-gate if (strcmp(p->fn_name, name) == 0) { 16250Sstevel@tonic-gate mutex_enter(&p->fn_lock); 16260Sstevel@tonic-gate if (p->fn_flags & MF_THISUID_MATCH_RQD) { 16270Sstevel@tonic-gate /* 16280Sstevel@tonic-gate * "thisuser" kind of node 16290Sstevel@tonic-gate * Need to match CREDs as well 16300Sstevel@tonic-gate */ 16310Sstevel@tonic-gate mutex_exit(&p->fn_lock); 16320Sstevel@tonic-gate match = crcmp(p->fn_cred, cred) == 0; 16330Sstevel@tonic-gate } else { 16340Sstevel@tonic-gate /* 16350Sstevel@tonic-gate * No need to check CRED 16360Sstevel@tonic-gate */ 16370Sstevel@tonic-gate mutex_exit(&p->fn_lock); 16380Sstevel@tonic-gate match = 1; 16390Sstevel@tonic-gate } 16400Sstevel@tonic-gate } 16410Sstevel@tonic-gate if (match) { 16420Sstevel@tonic-gate error = 0; 16430Sstevel@tonic-gate if (fnpp) { 16440Sstevel@tonic-gate *fnpp = p; 16450Sstevel@tonic-gate VN_HOLD(fntovn(*fnpp)); 16460Sstevel@tonic-gate } 16470Sstevel@tonic-gate break; 16480Sstevel@tonic-gate } 16490Sstevel@tonic-gate } 16500Sstevel@tonic-gate 16510Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_search: error=%d\n", error)); 16520Sstevel@tonic-gate return (error); 16530Sstevel@tonic-gate } 16540Sstevel@tonic-gate 16550Sstevel@tonic-gate /* 16560Sstevel@tonic-gate * If dvp is mounted on, get path's vnode in the mounted on 16570Sstevel@tonic-gate * filesystem. Path is relative to dvp, ie "./path". 16580Sstevel@tonic-gate * If successful, *mvp points to a the held mountpoint vnode. 16590Sstevel@tonic-gate */ 16600Sstevel@tonic-gate /* ARGSUSED */ 16610Sstevel@tonic-gate static int 16620Sstevel@tonic-gate auto_getmntpnt( 16630Sstevel@tonic-gate vnode_t *dvp, 16640Sstevel@tonic-gate char *path, 16650Sstevel@tonic-gate vnode_t **mvpp, /* vnode for mountpoint */ 16660Sstevel@tonic-gate cred_t *cred) 16670Sstevel@tonic-gate { 16680Sstevel@tonic-gate int error = 0; 16690Sstevel@tonic-gate vnode_t *newvp; 16700Sstevel@tonic-gate char namebuf[TYPICALMAXPATHLEN]; 16710Sstevel@tonic-gate struct pathname lookpn; 16720Sstevel@tonic-gate vfs_t *vfsp; 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate AUTOFS_DPRINT((4, "auto_getmntpnt: path=%s\n", path)); 16750Sstevel@tonic-gate 16761153Snr123932 if (error = vn_vfsrlock_wait(dvp)) 16770Sstevel@tonic-gate return (error); 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate /* 16800Sstevel@tonic-gate * Now that we have the vfswlock, check to see if dvp 16810Sstevel@tonic-gate * is still mounted on. If not, then just bail out as 16820Sstevel@tonic-gate * there is no need to remount the triggers since the 16830Sstevel@tonic-gate * higher level mount point has gotten unmounted. 16840Sstevel@tonic-gate */ 16850Sstevel@tonic-gate vfsp = vn_mountedvfs(dvp); 16860Sstevel@tonic-gate if (vfsp == NULL) { 16870Sstevel@tonic-gate vn_vfsunlock(dvp); 16880Sstevel@tonic-gate error = EBUSY; 16890Sstevel@tonic-gate goto done; 16900Sstevel@tonic-gate } 16910Sstevel@tonic-gate /* 16920Sstevel@tonic-gate * Since mounted on, lookup "path" in the new filesystem, 16930Sstevel@tonic-gate * it is important that we do the filesystem jump here to 16940Sstevel@tonic-gate * avoid lookuppn() calling auto_lookup on dvp and deadlock. 16950Sstevel@tonic-gate */ 16961153Snr123932 error = VFS_ROOT(vfsp, &newvp); 16970Sstevel@tonic-gate vn_vfsunlock(dvp); 16980Sstevel@tonic-gate if (error) 16990Sstevel@tonic-gate goto done; 17000Sstevel@tonic-gate 17010Sstevel@tonic-gate /* 17020Sstevel@tonic-gate * We do a VN_HOLD on newvp just in case the first call to 17030Sstevel@tonic-gate * lookuppnvp() fails with ENAMETOOLONG. We should still have a 17040Sstevel@tonic-gate * reference to this vnode for the second call to lookuppnvp(). 17050Sstevel@tonic-gate */ 17060Sstevel@tonic-gate VN_HOLD(newvp); 17070Sstevel@tonic-gate 17080Sstevel@tonic-gate /* 17090Sstevel@tonic-gate * Now create the pathname struct so we can make use of lookuppnvp, 17100Sstevel@tonic-gate * and pn_getcomponent. 17110Sstevel@tonic-gate * This code is similar to lookupname() in fs/lookup.c. 17120Sstevel@tonic-gate */ 17130Sstevel@tonic-gate error = pn_get_buf(path, UIO_SYSSPACE, &lookpn, 17146046Srmesta namebuf, sizeof (namebuf)); 17150Sstevel@tonic-gate if (error == 0) { 17160Sstevel@tonic-gate error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP, 17170Sstevel@tonic-gate mvpp, rootdir, newvp, cred); 17180Sstevel@tonic-gate } else 17190Sstevel@tonic-gate VN_RELE(newvp); 17200Sstevel@tonic-gate if (error == ENAMETOOLONG) { 17210Sstevel@tonic-gate /* 17220Sstevel@tonic-gate * This thread used a pathname > TYPICALMAXPATHLEN bytes long. 17230Sstevel@tonic-gate * newvp is VN_RELE'd by this call to lookuppnvp. 17240Sstevel@tonic-gate * 17250Sstevel@tonic-gate * Using 'rootdir' in a zone's context is OK here: we already 17260Sstevel@tonic-gate * ascertained that there are no '..'s in the path, and we're 17270Sstevel@tonic-gate * not following symlinks. 17280Sstevel@tonic-gate */ 17290Sstevel@tonic-gate if ((error = pn_get(path, UIO_SYSSPACE, &lookpn)) == 0) { 17300Sstevel@tonic-gate error = lookuppnvp(&lookpn, NULL, NO_FOLLOW, NULLVPP, 17310Sstevel@tonic-gate mvpp, rootdir, newvp, cred); 17320Sstevel@tonic-gate pn_free(&lookpn); 17330Sstevel@tonic-gate } else 17340Sstevel@tonic-gate VN_RELE(newvp); 17350Sstevel@tonic-gate } else { 17360Sstevel@tonic-gate /* 17370Sstevel@tonic-gate * Need to release newvp here since we held it. 17380Sstevel@tonic-gate */ 17390Sstevel@tonic-gate VN_RELE(newvp); 17400Sstevel@tonic-gate } 17410Sstevel@tonic-gate 17420Sstevel@tonic-gate done: 17430Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_getmntpnt: path=%s *mvpp=%p error=%d\n", 17440Sstevel@tonic-gate path, (void *)*mvpp, error)); 17450Sstevel@tonic-gate return (error); 17460Sstevel@tonic-gate } 17470Sstevel@tonic-gate 17480Sstevel@tonic-gate #define DEEPER(x) (((x)->fn_dirents != NULL) || \ 17490Sstevel@tonic-gate (vn_mountedvfs(fntovn((x)))) != NULL) 17500Sstevel@tonic-gate 17510Sstevel@tonic-gate /* 17520Sstevel@tonic-gate * The caller, should have already VN_RELE'd its reference to the 17530Sstevel@tonic-gate * root vnode of this filesystem. 17540Sstevel@tonic-gate */ 17550Sstevel@tonic-gate static int 17560Sstevel@tonic-gate auto_inkernel_unmount(vfs_t *vfsp) 17570Sstevel@tonic-gate { 17580Sstevel@tonic-gate vnode_t *cvp = vfsp->vfs_vnodecovered; 17590Sstevel@tonic-gate int error; 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate AUTOFS_DPRINT((4, 17620Sstevel@tonic-gate "auto_inkernel_unmount: devid=%lx mntpnt(%p) count %u\n", 17630Sstevel@tonic-gate vfsp->vfs_dev, (void *)cvp, cvp->v_count)); 17640Sstevel@tonic-gate 17650Sstevel@tonic-gate ASSERT(vn_vfswlock_held(cvp)); 17660Sstevel@tonic-gate 17670Sstevel@tonic-gate /* 17680Sstevel@tonic-gate * Perform the unmount 17690Sstevel@tonic-gate * The mountpoint has already been locked by the caller. 17700Sstevel@tonic-gate */ 17710Sstevel@tonic-gate error = dounmount(vfsp, 0, kcred); 17720Sstevel@tonic-gate 17730Sstevel@tonic-gate AUTOFS_DPRINT((5, "auto_inkernel_unmount: exit count %u\n", 17740Sstevel@tonic-gate cvp->v_count)); 17750Sstevel@tonic-gate return (error); 17760Sstevel@tonic-gate } 17770Sstevel@tonic-gate 17780Sstevel@tonic-gate /* 17790Sstevel@tonic-gate * unmounts trigger nodes in the kernel. 17800Sstevel@tonic-gate */ 17810Sstevel@tonic-gate static void 17820Sstevel@tonic-gate unmount_triggers(fnnode_t *fnp, action_list **alp) 17830Sstevel@tonic-gate { 17840Sstevel@tonic-gate fnnode_t *tp, *next; 17850Sstevel@tonic-gate int error = 0; 17860Sstevel@tonic-gate vfs_t *vfsp; 17870Sstevel@tonic-gate vnode_t *tvp; 17880Sstevel@tonic-gate 17890Sstevel@tonic-gate AUTOFS_DPRINT((4, "unmount_triggers: fnp=%p\n", (void *)fnp)); 17900Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock)); 17910Sstevel@tonic-gate 17920Sstevel@tonic-gate *alp = fnp->fn_alp; 17930Sstevel@tonic-gate next = fnp->fn_trigger; 17940Sstevel@tonic-gate while ((tp = next) != NULL) { 17950Sstevel@tonic-gate tvp = fntovn(tp); 17960Sstevel@tonic-gate ASSERT(tvp->v_count >= 2); 17970Sstevel@tonic-gate next = tp->fn_next; 17980Sstevel@tonic-gate /* 17990Sstevel@tonic-gate * drop writer's lock since the unmount will end up 18000Sstevel@tonic-gate * disconnecting this node from fnp and needs to acquire 18010Sstevel@tonic-gate * the writer's lock again. 18020Sstevel@tonic-gate * next has at least a reference count >= 2 since it's 18030Sstevel@tonic-gate * a trigger node, therefore can not be accidentally freed 18040Sstevel@tonic-gate * by a VN_RELE 18050Sstevel@tonic-gate */ 18060Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock); 18070Sstevel@tonic-gate 18080Sstevel@tonic-gate vfsp = tvp->v_vfsp; 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate /* 18110Sstevel@tonic-gate * Its parent was holding a reference to it, since this 18120Sstevel@tonic-gate * is a trigger vnode. 18130Sstevel@tonic-gate */ 18140Sstevel@tonic-gate VN_RELE(tvp); 18150Sstevel@tonic-gate if (error = auto_inkernel_unmount(vfsp)) { 18160Sstevel@tonic-gate cmn_err(CE_PANIC, "unmount_triggers: " 18170Sstevel@tonic-gate "unmount of vp=%p failed error=%d", 18180Sstevel@tonic-gate (void *)tvp, error); 18190Sstevel@tonic-gate } 18200Sstevel@tonic-gate /* 18210Sstevel@tonic-gate * reacquire writer's lock 18220Sstevel@tonic-gate */ 18230Sstevel@tonic-gate rw_enter(&fnp->fn_rwlock, RW_WRITER); 18240Sstevel@tonic-gate } 18250Sstevel@tonic-gate 18260Sstevel@tonic-gate /* 18270Sstevel@tonic-gate * We were holding a reference to our parent. Drop that. 18280Sstevel@tonic-gate */ 18290Sstevel@tonic-gate VN_RELE(fntovn(fnp)); 18300Sstevel@tonic-gate fnp->fn_trigger = NULL; 18310Sstevel@tonic-gate fnp->fn_alp = NULL; 18320Sstevel@tonic-gate 18330Sstevel@tonic-gate AUTOFS_DPRINT((5, "unmount_triggers: finished\n")); 18340Sstevel@tonic-gate } 18350Sstevel@tonic-gate 18360Sstevel@tonic-gate /* 18370Sstevel@tonic-gate * This routine locks the mountpoint of every trigger node if they're 18380Sstevel@tonic-gate * not busy, or returns EBUSY if any node is busy. If a trigger node should 18390Sstevel@tonic-gate * be unmounted first, then it sets nfnp to point to it, otherwise nfnp 18400Sstevel@tonic-gate * points to NULL. 18410Sstevel@tonic-gate */ 18420Sstevel@tonic-gate static int 18430Sstevel@tonic-gate triggers_busy(fnnode_t *fnp, fnnode_t **nfnp) 18440Sstevel@tonic-gate { 18450Sstevel@tonic-gate int error = 0, done; 18460Sstevel@tonic-gate int lck_error = 0; 18470Sstevel@tonic-gate fnnode_t *tp, *t1p; 18480Sstevel@tonic-gate vfs_t *vfsp; 18490Sstevel@tonic-gate 18500Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock)); 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate *nfnp = NULL; 18530Sstevel@tonic-gate for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) { 18540Sstevel@tonic-gate AUTOFS_DPRINT((10, "\ttrigger: %s\n", tp->fn_name)); 18550Sstevel@tonic-gate vfsp = fntovn(tp)->v_vfsp; 18560Sstevel@tonic-gate error = 0; 18570Sstevel@tonic-gate /* 18580Sstevel@tonic-gate * The vn_vfsunlock will be done in auto_inkernel_unmount. 18590Sstevel@tonic-gate */ 18600Sstevel@tonic-gate lck_error = vn_vfswlock(vfsp->vfs_vnodecovered); 18610Sstevel@tonic-gate if (lck_error == 0) { 18620Sstevel@tonic-gate mutex_enter(&tp->fn_lock); 18630Sstevel@tonic-gate ASSERT((tp->fn_flags & MF_LOOKUP) == 0); 18640Sstevel@tonic-gate if (tp->fn_flags & MF_INPROG) { 18650Sstevel@tonic-gate /* 18660Sstevel@tonic-gate * a mount is in progress 18670Sstevel@tonic-gate */ 18680Sstevel@tonic-gate error = EBUSY; 18690Sstevel@tonic-gate } 18700Sstevel@tonic-gate mutex_exit(&tp->fn_lock); 18710Sstevel@tonic-gate } 18720Sstevel@tonic-gate if (lck_error || error || DEEPER(tp) || 18730Sstevel@tonic-gate ((fntovn(tp))->v_count) > 2) { 18740Sstevel@tonic-gate /* 18750Sstevel@tonic-gate * couldn't lock it because it's busy, 18760Sstevel@tonic-gate * It is mounted on or has dirents? 18770Sstevel@tonic-gate * If reference count is greater than two, then 18780Sstevel@tonic-gate * somebody else is holding a reference to this vnode. 18790Sstevel@tonic-gate * One reference is for the mountpoint, and the second 18800Sstevel@tonic-gate * is for the trigger node. 18810Sstevel@tonic-gate */ 18820Sstevel@tonic-gate AUTOFS_DPRINT((10, "\ttrigger busy\n")); 18830Sstevel@tonic-gate if ((lck_error == 0) && (error == 0)) { 18840Sstevel@tonic-gate *nfnp = tp; 18850Sstevel@tonic-gate /* 18860Sstevel@tonic-gate * The matching VN_RELE is done in 18870Sstevel@tonic-gate * unmount_tree(). 18880Sstevel@tonic-gate */ 18890Sstevel@tonic-gate VN_HOLD(fntovn(*nfnp)); 18900Sstevel@tonic-gate } 18910Sstevel@tonic-gate /* 18920Sstevel@tonic-gate * Unlock previously locked mountpoints 18930Sstevel@tonic-gate */ 18940Sstevel@tonic-gate for (done = 0, t1p = fnp->fn_trigger; !done; 18950Sstevel@tonic-gate t1p = t1p->fn_next) { 18960Sstevel@tonic-gate /* 18970Sstevel@tonic-gate * Unlock all nodes previously 18980Sstevel@tonic-gate * locked. All nodes up to 'tp' 18990Sstevel@tonic-gate * were successfully locked. If 'lck_err' is 19000Sstevel@tonic-gate * set, then 'tp' was not locked, and thus 19010Sstevel@tonic-gate * should not be unlocked. If 19020Sstevel@tonic-gate * 'lck_err' is not set, then 'tp' was 19030Sstevel@tonic-gate * successfully locked, and it should 19040Sstevel@tonic-gate * be unlocked. 19050Sstevel@tonic-gate */ 19060Sstevel@tonic-gate if (t1p != tp || !lck_error) { 19070Sstevel@tonic-gate vfsp = fntovn(t1p)->v_vfsp; 19080Sstevel@tonic-gate vn_vfsunlock(vfsp->vfs_vnodecovered); 19090Sstevel@tonic-gate } 19100Sstevel@tonic-gate done = (t1p == tp); 19110Sstevel@tonic-gate } 19120Sstevel@tonic-gate error = EBUSY; 19130Sstevel@tonic-gate break; 19140Sstevel@tonic-gate } 19150Sstevel@tonic-gate } 19160Sstevel@tonic-gate 19170Sstevel@tonic-gate AUTOFS_DPRINT((4, "triggers_busy: error=%d\n", error)); 19180Sstevel@tonic-gate return (error); 19190Sstevel@tonic-gate } 19200Sstevel@tonic-gate 19210Sstevel@tonic-gate /* 19220Sstevel@tonic-gate * Unlock previously locked trigger nodes. 19230Sstevel@tonic-gate */ 19240Sstevel@tonic-gate static int 19250Sstevel@tonic-gate triggers_unlock(fnnode_t *fnp) 19260Sstevel@tonic-gate { 19270Sstevel@tonic-gate fnnode_t *tp; 19280Sstevel@tonic-gate vfs_t *vfsp; 19290Sstevel@tonic-gate 19300Sstevel@tonic-gate ASSERT(RW_WRITE_HELD(&fnp->fn_rwlock)); 19310Sstevel@tonic-gate 19320Sstevel@tonic-gate for (tp = fnp->fn_trigger; tp != NULL; tp = tp->fn_next) { 19330Sstevel@tonic-gate AUTOFS_DPRINT((10, "\tunlock trigger: %s\n", tp->fn_name)); 19340Sstevel@tonic-gate vfsp = fntovn(tp)->v_vfsp; 19350Sstevel@tonic-gate vn_vfsunlock(vfsp->vfs_vnodecovered); 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate 19380Sstevel@tonic-gate return (0); 19390Sstevel@tonic-gate } 19400Sstevel@tonic-gate 19410Sstevel@tonic-gate /* 19420Sstevel@tonic-gate * It is the caller's responsibility to grab the VVFSLOCK. 19430Sstevel@tonic-gate * Releases the VVFSLOCK upon return. 19440Sstevel@tonic-gate */ 19450Sstevel@tonic-gate static int 19460Sstevel@tonic-gate unmount_node(vnode_t *cvp, int force) 19470Sstevel@tonic-gate { 19480Sstevel@tonic-gate int error = 0; 19490Sstevel@tonic-gate fnnode_t *cfnp; 19500Sstevel@tonic-gate vfs_t *vfsp; 19510Sstevel@tonic-gate umntrequest ul; 19520Sstevel@tonic-gate fninfo_t *fnip; 19530Sstevel@tonic-gate 19540Sstevel@tonic-gate AUTOFS_DPRINT((4, "\tunmount_node cvp=%p\n", (void *)cvp)); 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate ASSERT(vn_vfswlock_held(cvp)); 19570Sstevel@tonic-gate cfnp = vntofn(cvp); 19580Sstevel@tonic-gate vfsp = vn_mountedvfs(cvp); 19590Sstevel@tonic-gate 19600Sstevel@tonic-gate if (force || cfnp->fn_flags & MF_IK_MOUNT) { 19610Sstevel@tonic-gate /* 19620Sstevel@tonic-gate * Mount was performed in the kernel, so 19630Sstevel@tonic-gate * do an in-kernel unmount. auto_inkernel_unmount() 19640Sstevel@tonic-gate * will vn_vfsunlock(cvp). 19650Sstevel@tonic-gate */ 19660Sstevel@tonic-gate error = auto_inkernel_unmount(vfsp); 19670Sstevel@tonic-gate } else { 19680Sstevel@tonic-gate zone_t *zone = NULL; 19690Sstevel@tonic-gate refstr_t *mntpt, *resource; 19700Sstevel@tonic-gate size_t mntoptslen; 19710Sstevel@tonic-gate 19720Sstevel@tonic-gate /* 19730Sstevel@tonic-gate * Get the mnttab information of the node 19740Sstevel@tonic-gate * and ask the daemon to unmount it. 19750Sstevel@tonic-gate */ 19760Sstevel@tonic-gate bzero(&ul, sizeof (ul)); 19770Sstevel@tonic-gate mntfs_getmntopts(vfsp, &ul.mntopts, &mntoptslen); 19780Sstevel@tonic-gate if (ul.mntopts == NULL) { 19792170Sevanl auto_log(cfnp->fn_globals->fng_verbose, 19806046Srmesta cfnp->fn_globals->fng_zoneid, CE_WARN, 19816046Srmesta "unmount_node: no memory"); 19820Sstevel@tonic-gate vn_vfsunlock(cvp); 19830Sstevel@tonic-gate error = ENOMEM; 19840Sstevel@tonic-gate goto done; 19850Sstevel@tonic-gate } 19860Sstevel@tonic-gate if (mntoptslen > AUTOFS_MAXOPTSLEN) 19870Sstevel@tonic-gate ul.mntopts[AUTOFS_MAXOPTSLEN - 1] = '\0'; 19880Sstevel@tonic-gate 19890Sstevel@tonic-gate mntpt = vfs_getmntpoint(vfsp); 19900Sstevel@tonic-gate ul.mntpnt = (char *)refstr_value(mntpt); 19910Sstevel@tonic-gate resource = vfs_getresource(vfsp); 19920Sstevel@tonic-gate ul.mntresource = (char *)refstr_value(resource); 19930Sstevel@tonic-gate 19940Sstevel@tonic-gate fnip = vfstofni(cvp->v_vfsp); 19950Sstevel@tonic-gate ul.isdirect = fnip->fi_flags & MF_DIRECT ? TRUE : FALSE; 19960Sstevel@tonic-gate 19970Sstevel@tonic-gate /* 19980Sstevel@tonic-gate * Since a zone'd automountd's view of the autofs mount points 19990Sstevel@tonic-gate * differs from those in the kernel, we need to make sure we 20000Sstevel@tonic-gate * give it consistent mount points. 20010Sstevel@tonic-gate */ 20020Sstevel@tonic-gate ASSERT(fnip->fi_zoneid == getzoneid()); 20030Sstevel@tonic-gate zone = curproc->p_zone; 20040Sstevel@tonic-gate 20050Sstevel@tonic-gate if (fnip->fi_zoneid != GLOBAL_ZONEID) { 20060Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(ul.mntpnt, zone)) { 20070Sstevel@tonic-gate ul.mntpnt = 20080Sstevel@tonic-gate ZONE_PATH_TRANSLATE(ul.mntpnt, zone); 20090Sstevel@tonic-gate } 20100Sstevel@tonic-gate if (ZONE_PATH_VISIBLE(ul.mntresource, zone)) { 20110Sstevel@tonic-gate ul.mntresource = 20120Sstevel@tonic-gate ZONE_PATH_TRANSLATE(ul.mntresource, zone); 20130Sstevel@tonic-gate } 20140Sstevel@tonic-gate } 20152170Sevanl 20160Sstevel@tonic-gate ul.fstype = vfssw[vfsp->vfs_fstype].vsw_name; 20170Sstevel@tonic-gate vn_vfsunlock(cvp); 20180Sstevel@tonic-gate 20192170Sevanl error = auto_send_unmount_request(fnip, &ul, FALSE); 20200Sstevel@tonic-gate kmem_free(ul.mntopts, mntoptslen); 20210Sstevel@tonic-gate refstr_rele(mntpt); 20220Sstevel@tonic-gate refstr_rele(resource); 20230Sstevel@tonic-gate } 20240Sstevel@tonic-gate 20250Sstevel@tonic-gate done: 20260Sstevel@tonic-gate AUTOFS_DPRINT((5, "\tunmount_node cvp=%p error=%d\n", (void *)cvp, 20270Sstevel@tonic-gate error)); 20280Sstevel@tonic-gate return (error); 20290Sstevel@tonic-gate } 20300Sstevel@tonic-gate 20310Sstevel@tonic-gate /* 20320Sstevel@tonic-gate * vp is the "root" of the AUTOFS filesystem. 20330Sstevel@tonic-gate * return EBUSY if any thread is holding a reference to this vnode 20340Sstevel@tonic-gate * other than us. 20350Sstevel@tonic-gate */ 20360Sstevel@tonic-gate static int 20370Sstevel@tonic-gate check_auto_node(vnode_t *vp) 20380Sstevel@tonic-gate { 20390Sstevel@tonic-gate fnnode_t *fnp; 20400Sstevel@tonic-gate int error = 0; 20410Sstevel@tonic-gate /* 20420Sstevel@tonic-gate * number of references to expect for 20430Sstevel@tonic-gate * a non-busy vnode. 20440Sstevel@tonic-gate */ 20450Sstevel@tonic-gate uint_t count; 20460Sstevel@tonic-gate 20470Sstevel@tonic-gate AUTOFS_DPRINT((4, "\tcheck_auto_node vp=%p ", (void *)vp)); 20480Sstevel@tonic-gate fnp = vntofn(vp); 20490Sstevel@tonic-gate ASSERT(fnp->fn_flags & MF_INPROG); 20500Sstevel@tonic-gate ASSERT((fnp->fn_flags & MF_LOOKUP) == 0); 20510Sstevel@tonic-gate 20520Sstevel@tonic-gate count = 1; /* we are holding a reference to vp */ 20530Sstevel@tonic-gate if (fnp->fn_flags & MF_TRIGGER) { 20540Sstevel@tonic-gate /* 20550Sstevel@tonic-gate * parent holds a pointer to us (trigger) 20560Sstevel@tonic-gate */ 20570Sstevel@tonic-gate count++; 20580Sstevel@tonic-gate } 20590Sstevel@tonic-gate if (fnp->fn_trigger != NULL) { 20600Sstevel@tonic-gate /* 20610Sstevel@tonic-gate * The trigger nodes have a hold on us. 20620Sstevel@tonic-gate */ 20630Sstevel@tonic-gate count++; 20640Sstevel@tonic-gate } 20650Sstevel@tonic-gate mutex_enter(&vp->v_lock); 20660Sstevel@tonic-gate if (vp->v_flag & VROOT) 20670Sstevel@tonic-gate count++; 20680Sstevel@tonic-gate ASSERT(vp->v_count > 0); 20690Sstevel@tonic-gate AUTOFS_DPRINT((10, "\tcount=%u ", vp->v_count)); 20700Sstevel@tonic-gate if (vp->v_count > count) 20710Sstevel@tonic-gate error = EBUSY; 20720Sstevel@tonic-gate mutex_exit(&vp->v_lock); 20730Sstevel@tonic-gate 20740Sstevel@tonic-gate AUTOFS_DPRINT((5, "\tcheck_auto_node error=%d ", error)); 20750Sstevel@tonic-gate return (error); 20760Sstevel@tonic-gate } 20770Sstevel@tonic-gate 20780Sstevel@tonic-gate /* 20790Sstevel@tonic-gate * rootvp is the root of the AUTOFS filesystem. 20800Sstevel@tonic-gate * If rootvp is busy (v_count > 1) returns EBUSY. 20810Sstevel@tonic-gate * else removes every vnode under this tree. 20820Sstevel@tonic-gate * ASSUMPTION: Assumes that the only node which can be busy is 20830Sstevel@tonic-gate * the root vnode. This filesystem better be two levels deep only, 20840Sstevel@tonic-gate * the root and its immediate subdirs. 20850Sstevel@tonic-gate * The daemon will "AUTOFS direct-mount" only one level below the root. 20860Sstevel@tonic-gate */ 20870Sstevel@tonic-gate static int 20880Sstevel@tonic-gate unmount_autofs(vnode_t *rootvp) 20890Sstevel@tonic-gate { 20900Sstevel@tonic-gate fnnode_t *fnp, *rootfnp, *nfnp; 20910Sstevel@tonic-gate int error; 20920Sstevel@tonic-gate 20930Sstevel@tonic-gate AUTOFS_DPRINT((4, "\tunmount_autofs rootvp=%p ", (void *)rootvp)); 20940Sstevel@tonic-gate 20950Sstevel@tonic-gate error = check_auto_node(rootvp); 20960Sstevel@tonic-gate if (error == 0) { 20970Sstevel@tonic-gate /* 20980Sstevel@tonic-gate * Remove all its immediate subdirectories. 20990Sstevel@tonic-gate */ 21000Sstevel@tonic-gate rootfnp = vntofn(rootvp); 21010Sstevel@tonic-gate rw_enter(&rootfnp->fn_rwlock, RW_WRITER); 21020Sstevel@tonic-gate nfnp = NULL; /* lint clean */ 21030Sstevel@tonic-gate for (fnp = rootfnp->fn_dirents; fnp != NULL; fnp = nfnp) { 21040Sstevel@tonic-gate ASSERT(fntovn(fnp)->v_count == 0); 21050Sstevel@tonic-gate ASSERT(fnp->fn_dirents == NULL); 21060Sstevel@tonic-gate ASSERT(fnp->fn_linkcnt == 2); 21070Sstevel@tonic-gate fnp->fn_linkcnt--; 21080Sstevel@tonic-gate auto_disconnect(rootfnp, fnp); 21090Sstevel@tonic-gate nfnp = fnp->fn_next; 21100Sstevel@tonic-gate auto_freefnnode(fnp); 21110Sstevel@tonic-gate } 21120Sstevel@tonic-gate rw_exit(&rootfnp->fn_rwlock); 21130Sstevel@tonic-gate } 21140Sstevel@tonic-gate AUTOFS_DPRINT((5, "\tunmount_autofs error=%d ", error)); 21150Sstevel@tonic-gate return (error); 21160Sstevel@tonic-gate } 21170Sstevel@tonic-gate 21180Sstevel@tonic-gate /* 21190Sstevel@tonic-gate * max number of unmount threads running 21200Sstevel@tonic-gate */ 21210Sstevel@tonic-gate static int autofs_unmount_threads = 5; 21220Sstevel@tonic-gate 21230Sstevel@tonic-gate /* 21240Sstevel@tonic-gate * XXX unmount_tree() is not suspend-safe within the scope of 21250Sstevel@tonic-gate * the present model defined for cpr to suspend the system. Calls made 21260Sstevel@tonic-gate * by the unmount_tree() that have been identified to be unsafe are 21270Sstevel@tonic-gate * (1) RPC client handle setup and client calls to automountd which can 21280Sstevel@tonic-gate * block deep down in the RPC library, (2) kmem_alloc() calls with the 21290Sstevel@tonic-gate * KM_SLEEP flag which can block if memory is low, and (3) VFS_*() and 21300Sstevel@tonic-gate * VOP_*() calls which can result in over the wire calls to servers. 21310Sstevel@tonic-gate * The thread should be completely reevaluated to make it suspend-safe in 21320Sstevel@tonic-gate * case of future updates to the cpr model. 21330Sstevel@tonic-gate */ 21340Sstevel@tonic-gate void 21350Sstevel@tonic-gate unmount_tree(struct autofs_globals *fngp, int force) 21360Sstevel@tonic-gate { 21370Sstevel@tonic-gate vnode_t *vp, *newvp; 21380Sstevel@tonic-gate vfs_t *vfsp; 21390Sstevel@tonic-gate fnnode_t *fnp, *nfnp, *pfnp; 21400Sstevel@tonic-gate action_list *alp; 21410Sstevel@tonic-gate int error, ilocked_it = 0; 21420Sstevel@tonic-gate fninfo_t *fnip; 21430Sstevel@tonic-gate time_t ref_time; 21440Sstevel@tonic-gate int autofs_busy_root, unmount_as_unit, unmount_done = 0; 21450Sstevel@tonic-gate timestruc_t now; 21460Sstevel@tonic-gate 21470Sstevel@tonic-gate callb_cpr_t cprinfo; 21480Sstevel@tonic-gate kmutex_t unmount_tree_cpr_lock; 21490Sstevel@tonic-gate 21500Sstevel@tonic-gate mutex_init(&unmount_tree_cpr_lock, NULL, MUTEX_DEFAULT, NULL); 21510Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &unmount_tree_cpr_lock, callb_generic_cpr, 21526046Srmesta "unmount_tree"); 21530Sstevel@tonic-gate 21540Sstevel@tonic-gate /* 21550Sstevel@tonic-gate * Got to release lock before attempting unmount in case 21560Sstevel@tonic-gate * it hangs. 21570Sstevel@tonic-gate */ 21580Sstevel@tonic-gate rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_READER); 21590Sstevel@tonic-gate if ((fnp = fngp->fng_rootfnnodep->fn_dirents) == NULL) { 21600Sstevel@tonic-gate ASSERT(fngp->fng_fnnode_count == 1); 21610Sstevel@tonic-gate /* 21620Sstevel@tonic-gate * no autofs mounted, done. 21630Sstevel@tonic-gate */ 21640Sstevel@tonic-gate rw_exit(&fngp->fng_rootfnnodep->fn_rwlock); 21650Sstevel@tonic-gate goto done; 21660Sstevel@tonic-gate } 21670Sstevel@tonic-gate VN_HOLD(fntovn(fnp)); 21680Sstevel@tonic-gate rw_exit(&fngp->fng_rootfnnodep->fn_rwlock); 21690Sstevel@tonic-gate 21700Sstevel@tonic-gate vp = fntovn(fnp); 21710Sstevel@tonic-gate fnip = vfstofni(vp->v_vfsp); 21720Sstevel@tonic-gate /* 21730Sstevel@tonic-gate * autofssys() will be calling in from the global zone and doing 21740Sstevel@tonic-gate * work on the behalf of the given zone, hence we can't always assert 21750Sstevel@tonic-gate * that we have the right credentials, nor that the caller is always in 21760Sstevel@tonic-gate * the correct zone. 21770Sstevel@tonic-gate * 21780Sstevel@tonic-gate * We do, however, know that if this is a "forced unmount" operation 21790Sstevel@tonic-gate * (which autofssys() does), then we won't go down to the krpc layers, 21800Sstevel@tonic-gate * so we don't need to fudge with the credentials. 21810Sstevel@tonic-gate */ 21820Sstevel@tonic-gate ASSERT(force || fnip->fi_zoneid == getzoneid()); 21832170Sevanl if (!force && auto_null_request(fnip, FALSE) != 0) { 21840Sstevel@tonic-gate /* 21850Sstevel@tonic-gate * automountd not running in this zone, 21860Sstevel@tonic-gate * don't attempt unmounting this round. 21870Sstevel@tonic-gate */ 21880Sstevel@tonic-gate VN_RELE(vp); 21890Sstevel@tonic-gate goto done; 21900Sstevel@tonic-gate } 21910Sstevel@tonic-gate /* reference time for this unmount round */ 21920Sstevel@tonic-gate ref_time = gethrestime_sec(); 21930Sstevel@tonic-gate /* 21940Sstevel@tonic-gate * If this an autofssys() call, we need to make sure we don't skip 21950Sstevel@tonic-gate * nodes because we think we saw them recently. 21960Sstevel@tonic-gate */ 21970Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 21980Sstevel@tonic-gate if (force && fnp->fn_unmount_ref_time >= ref_time) 21990Sstevel@tonic-gate ref_time = fnp->fn_unmount_ref_time + 1; 22000Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 22010Sstevel@tonic-gate 22020Sstevel@tonic-gate AUTOFS_DPRINT((4, "unmount_tree (ID=%ld)\n", ref_time)); 22030Sstevel@tonic-gate top: 22040Sstevel@tonic-gate AUTOFS_DPRINT((10, "unmount_tree: %s\n", fnp->fn_name)); 22050Sstevel@tonic-gate ASSERT(fnp); 22060Sstevel@tonic-gate vp = fntovn(fnp); 22070Sstevel@tonic-gate if (vp->v_type == VLNK) { 22080Sstevel@tonic-gate /* 22090Sstevel@tonic-gate * can't unmount symbolic links 22100Sstevel@tonic-gate */ 22110Sstevel@tonic-gate goto next; 22120Sstevel@tonic-gate } 22130Sstevel@tonic-gate fnip = vfstofni(vp->v_vfsp); 22140Sstevel@tonic-gate ASSERT(vp->v_count > 0); 22150Sstevel@tonic-gate error = 0; 22160Sstevel@tonic-gate autofs_busy_root = unmount_as_unit = 0; 22170Sstevel@tonic-gate alp = NULL; 22180Sstevel@tonic-gate 22190Sstevel@tonic-gate ilocked_it = 0; 22200Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 22210Sstevel@tonic-gate if (fnp->fn_flags & (MF_INPROG | MF_LOOKUP)) { 22220Sstevel@tonic-gate /* 22230Sstevel@tonic-gate * Either a mount, lookup or another unmount of this 22240Sstevel@tonic-gate * subtree is in progress, don't attempt to unmount at 22250Sstevel@tonic-gate * this time. 22260Sstevel@tonic-gate */ 22270Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 22280Sstevel@tonic-gate error = EBUSY; 22290Sstevel@tonic-gate goto next; 22300Sstevel@tonic-gate } 22310Sstevel@tonic-gate if (fnp->fn_unmount_ref_time >= ref_time) { 22320Sstevel@tonic-gate /* 22330Sstevel@tonic-gate * Already been here, try next node. 22340Sstevel@tonic-gate */ 22350Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 22360Sstevel@tonic-gate error = EBUSY; 22370Sstevel@tonic-gate goto next; 22380Sstevel@tonic-gate } 22390Sstevel@tonic-gate fnp->fn_unmount_ref_time = ref_time; 22400Sstevel@tonic-gate 22410Sstevel@tonic-gate /* 22420Sstevel@tonic-gate * If forced operation ignore timeout values 22430Sstevel@tonic-gate */ 22440Sstevel@tonic-gate if (!force && fnp->fn_ref_time + fnip->fi_mount_to > 22450Sstevel@tonic-gate gethrestime_sec()) { 22460Sstevel@tonic-gate /* 22470Sstevel@tonic-gate * Node has been referenced recently, try the 22480Sstevel@tonic-gate * unmount of its children if any. 22490Sstevel@tonic-gate */ 22500Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 22510Sstevel@tonic-gate AUTOFS_DPRINT((10, "fn_ref_time within range\n")); 22520Sstevel@tonic-gate rw_enter(&fnp->fn_rwlock, RW_READER); 22530Sstevel@tonic-gate if (fnp->fn_dirents) { 22540Sstevel@tonic-gate /* 22550Sstevel@tonic-gate * Has subdirectory, attempt their 22560Sstevel@tonic-gate * unmount first 22570Sstevel@tonic-gate */ 22580Sstevel@tonic-gate nfnp = fnp->fn_dirents; 22590Sstevel@tonic-gate VN_HOLD(fntovn(nfnp)); 22600Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock); 22610Sstevel@tonic-gate 22620Sstevel@tonic-gate VN_RELE(vp); 22630Sstevel@tonic-gate fnp = nfnp; 22640Sstevel@tonic-gate goto top; 22650Sstevel@tonic-gate } 22660Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock); 22670Sstevel@tonic-gate /* 22680Sstevel@tonic-gate * No children, try next node. 22690Sstevel@tonic-gate */ 22700Sstevel@tonic-gate error = EBUSY; 22710Sstevel@tonic-gate goto next; 22720Sstevel@tonic-gate } 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate AUTOFS_BLOCK_OTHERS(fnp, MF_INPROG); 22750Sstevel@tonic-gate fnp->fn_error = 0; 22760Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 22770Sstevel@tonic-gate ilocked_it = 1; 22780Sstevel@tonic-gate 22790Sstevel@tonic-gate rw_enter(&fnp->fn_rwlock, RW_WRITER); 22800Sstevel@tonic-gate if (fnp->fn_trigger != NULL) { 22810Sstevel@tonic-gate unmount_as_unit = 1; 22820Sstevel@tonic-gate if ((vn_mountedvfs(vp) == NULL) && (check_auto_node(vp))) { 22830Sstevel@tonic-gate /* 22840Sstevel@tonic-gate * AUTOFS mountpoint is busy, there's 22850Sstevel@tonic-gate * no point trying to unmount. Fall through 22860Sstevel@tonic-gate * to attempt to unmount subtrees rooted 22870Sstevel@tonic-gate * at a possible trigger node, but remember 22880Sstevel@tonic-gate * not to unmount this tree. 22890Sstevel@tonic-gate */ 22900Sstevel@tonic-gate autofs_busy_root = 1; 22910Sstevel@tonic-gate } 22920Sstevel@tonic-gate 22930Sstevel@tonic-gate if (triggers_busy(fnp, &nfnp)) { 22940Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock); 22950Sstevel@tonic-gate if (nfnp == NULL) { 22960Sstevel@tonic-gate error = EBUSY; 22970Sstevel@tonic-gate goto next; 22980Sstevel@tonic-gate } 22990Sstevel@tonic-gate /* 23000Sstevel@tonic-gate * nfnp is busy, try to unmount it first 23010Sstevel@tonic-gate */ 23020Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 23030Sstevel@tonic-gate AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 23040Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 23050Sstevel@tonic-gate VN_RELE(vp); 23060Sstevel@tonic-gate ASSERT(fntovn(nfnp)->v_count > 1); 23070Sstevel@tonic-gate fnp = nfnp; 23080Sstevel@tonic-gate goto top; 23090Sstevel@tonic-gate } 23100Sstevel@tonic-gate 23110Sstevel@tonic-gate /* 23120Sstevel@tonic-gate * At this point, we know all trigger nodes are locked, 23130Sstevel@tonic-gate * and they're not busy or mounted on. 23140Sstevel@tonic-gate */ 23150Sstevel@tonic-gate 23160Sstevel@tonic-gate if (autofs_busy_root) { 23170Sstevel@tonic-gate /* 23180Sstevel@tonic-gate * Got to unlock the the trigger nodes since 23190Sstevel@tonic-gate * I'm not really going to unmount the filesystem. 23200Sstevel@tonic-gate */ 23210Sstevel@tonic-gate (void) triggers_unlock(fnp); 23220Sstevel@tonic-gate } else { 23230Sstevel@tonic-gate /* 23240Sstevel@tonic-gate * Attempt to unmount all the trigger nodes, 23250Sstevel@tonic-gate * save the action_list in case we need to 23262170Sevanl * remount them later. The action_list will be 23270Sstevel@tonic-gate * freed later if there was no need to remount the 23280Sstevel@tonic-gate * trigger nodes. 23290Sstevel@tonic-gate */ 23300Sstevel@tonic-gate unmount_triggers(fnp, &alp); 23310Sstevel@tonic-gate } 23320Sstevel@tonic-gate } 23330Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock); 23340Sstevel@tonic-gate 23350Sstevel@tonic-gate if (autofs_busy_root) 23360Sstevel@tonic-gate goto next; 23370Sstevel@tonic-gate 23380Sstevel@tonic-gate (void) vn_vfswlock_wait(vp); 23390Sstevel@tonic-gate 23400Sstevel@tonic-gate vfsp = vn_mountedvfs(vp); 23410Sstevel@tonic-gate if (vfsp != NULL) { 23420Sstevel@tonic-gate /* 23430Sstevel@tonic-gate * Node is mounted on. 23440Sstevel@tonic-gate */ 23450Sstevel@tonic-gate AUTOFS_DPRINT((10, "\tNode is mounted on\n")); 23460Sstevel@tonic-gate 23470Sstevel@tonic-gate /* 23480Sstevel@tonic-gate * Deal with /xfn/host/jurassic alikes here... 23490Sstevel@tonic-gate */ 23500Sstevel@tonic-gate if (vfs_matchops(vfsp, vfs_getops(vp->v_vfsp))) { 23510Sstevel@tonic-gate /* 23520Sstevel@tonic-gate * If the filesystem mounted here is AUTOFS, and it 23530Sstevel@tonic-gate * is busy, try to unmount the tree rooted on it 23540Sstevel@tonic-gate * first. We know this call to VFS_ROOT is safe to 23550Sstevel@tonic-gate * call while holding VVFSLOCK, since it resolves 23560Sstevel@tonic-gate * to a call to auto_root(). 23570Sstevel@tonic-gate */ 23580Sstevel@tonic-gate AUTOFS_DPRINT((10, "\t\tAUTOFS mounted here\n")); 23590Sstevel@tonic-gate if (VFS_ROOT(vfsp, &newvp)) { 23600Sstevel@tonic-gate cmn_err(CE_PANIC, 23610Sstevel@tonic-gate "unmount_tree: VFS_ROOT(vfs=%p) failed", 23620Sstevel@tonic-gate (void *)vfsp); 23630Sstevel@tonic-gate } 23640Sstevel@tonic-gate nfnp = vntofn(newvp); 23650Sstevel@tonic-gate if (DEEPER(nfnp)) { 23660Sstevel@tonic-gate vn_vfsunlock(vp); 23670Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 23680Sstevel@tonic-gate AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 23690Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 23700Sstevel@tonic-gate VN_RELE(vp); 23710Sstevel@tonic-gate fnp = nfnp; 23720Sstevel@tonic-gate goto top; 23730Sstevel@tonic-gate } 23740Sstevel@tonic-gate /* 23750Sstevel@tonic-gate * Fall through to unmount this filesystem 23760Sstevel@tonic-gate */ 23770Sstevel@tonic-gate VN_RELE(newvp); 23780Sstevel@tonic-gate } 23790Sstevel@tonic-gate 23800Sstevel@tonic-gate /* 23810Sstevel@tonic-gate * vn_vfsunlock(vp) is done inside unmount_node() 23820Sstevel@tonic-gate */ 23830Sstevel@tonic-gate error = unmount_node(vp, force); 23840Sstevel@tonic-gate if (error == ECONNRESET) { 23850Sstevel@tonic-gate AUTOFS_DPRINT((10, "\tConnection dropped\n")); 23860Sstevel@tonic-gate if (vn_mountedvfs(vp) == NULL) { 23870Sstevel@tonic-gate /* 23880Sstevel@tonic-gate * The filesystem was unmounted before the 23890Sstevel@tonic-gate * daemon died. Unfortunately we can not 23900Sstevel@tonic-gate * determine whether all the cleanup work was 23910Sstevel@tonic-gate * successfully finished (i.e. update mnttab, 23920Sstevel@tonic-gate * or notify NFS server of the unmount). 23930Sstevel@tonic-gate * We should not retry the operation since the 23940Sstevel@tonic-gate * filesystem has already been unmounted, and 23950Sstevel@tonic-gate * may have already been removed from mnttab, 23960Sstevel@tonic-gate * in such case the devid/rdevid we send to 23970Sstevel@tonic-gate * the daemon will not be matched. So we have 23982170Sevanl * to be content with the partial unmount. 23990Sstevel@tonic-gate * Since the mountpoint is no longer covered, we 24000Sstevel@tonic-gate * clear the error condition. 24010Sstevel@tonic-gate */ 24020Sstevel@tonic-gate error = 0; 24032170Sevanl auto_log(fngp->fng_verbose, fngp->fng_zoneid, 24046046Srmesta CE_WARN, "unmount_tree: automountd " 24056046Srmesta "connection dropped"); 24060Sstevel@tonic-gate if (fnip->fi_flags & MF_DIRECT) { 24072170Sevanl auto_log(fngp->fng_verbose, 24086046Srmesta fngp->fng_zoneid, CE_WARN, 24096046Srmesta "unmount_tree: " 24100Sstevel@tonic-gate "%s successfully unmounted - " 24110Sstevel@tonic-gate "do not remount triggers", 24120Sstevel@tonic-gate fnip->fi_path); 24130Sstevel@tonic-gate } else { 24142170Sevanl auto_log(fngp->fng_verbose, 24156046Srmesta fngp->fng_zoneid, CE_WARN, 24166046Srmesta "unmount_tree: " 24170Sstevel@tonic-gate "%s/%s successfully unmounted - " 24180Sstevel@tonic-gate "do not remount triggers", 24190Sstevel@tonic-gate fnip->fi_path, fnp->fn_name); 24200Sstevel@tonic-gate } 24210Sstevel@tonic-gate } 24220Sstevel@tonic-gate } 24230Sstevel@tonic-gate } else { 24240Sstevel@tonic-gate vn_vfsunlock(vp); 24250Sstevel@tonic-gate AUTOFS_DPRINT((10, "\tNode is AUTOFS\n")); 24260Sstevel@tonic-gate if (unmount_as_unit) { 24270Sstevel@tonic-gate AUTOFS_DPRINT((10, "\tunmount as unit\n")); 24280Sstevel@tonic-gate error = unmount_autofs(vp); 24290Sstevel@tonic-gate } else { 24300Sstevel@tonic-gate AUTOFS_DPRINT((10, "\tunmount one at a time\n")); 24310Sstevel@tonic-gate rw_enter(&fnp->fn_rwlock, RW_READER); 24320Sstevel@tonic-gate if (fnp->fn_dirents != NULL) { 24330Sstevel@tonic-gate /* 24340Sstevel@tonic-gate * Has subdirectory, attempt their 24350Sstevel@tonic-gate * unmount first 24360Sstevel@tonic-gate */ 24370Sstevel@tonic-gate nfnp = fnp->fn_dirents; 24380Sstevel@tonic-gate VN_HOLD(fntovn(nfnp)); 24390Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock); 24400Sstevel@tonic-gate 24410Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 24420Sstevel@tonic-gate AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 24430Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 24440Sstevel@tonic-gate VN_RELE(vp); 24450Sstevel@tonic-gate fnp = nfnp; 24460Sstevel@tonic-gate goto top; 24470Sstevel@tonic-gate } 24480Sstevel@tonic-gate rw_exit(&fnp->fn_rwlock); 24490Sstevel@tonic-gate goto next; 24500Sstevel@tonic-gate } 24510Sstevel@tonic-gate } 24520Sstevel@tonic-gate 24530Sstevel@tonic-gate if (error) { 24540Sstevel@tonic-gate AUTOFS_DPRINT((10, "\tUnmount failed\n")); 24550Sstevel@tonic-gate if (alp != NULL) { 24560Sstevel@tonic-gate /* 24570Sstevel@tonic-gate * Unmount failed, got to remount triggers. 24580Sstevel@tonic-gate */ 24590Sstevel@tonic-gate ASSERT((fnp->fn_flags & MF_THISUID_MATCH_RQD) == 0); 24606046Srmesta error = auto_perform_actions(fnip, fnp, alp, CRED()); 24610Sstevel@tonic-gate if (error) { 24626046Srmesta auto_log(fngp->fng_verbose, fngp->fng_zoneid, 24636046Srmesta CE_WARN, "autofs: can't remount triggers " 24646046Srmesta "fnp=%p error=%d", (void *)fnp, error); 24650Sstevel@tonic-gate error = 0; 24660Sstevel@tonic-gate /* 24670Sstevel@tonic-gate * The action list should have been 24682170Sevanl * free'd by auto_perform_actions 24690Sstevel@tonic-gate * since an error occured 24700Sstevel@tonic-gate */ 24710Sstevel@tonic-gate alp = NULL; 24720Sstevel@tonic-gate } 24730Sstevel@tonic-gate } 24740Sstevel@tonic-gate } else { 24750Sstevel@tonic-gate /* 24760Sstevel@tonic-gate * The unmount succeeded, which will cause this node to 24770Sstevel@tonic-gate * be removed from its parent if its an indirect mount, 24780Sstevel@tonic-gate * therefore update the parent's atime and mtime now. 24790Sstevel@tonic-gate * I don't update them in auto_disconnect() because I 24800Sstevel@tonic-gate * don't want atime and mtime changing every time a 24810Sstevel@tonic-gate * lookup goes to the daemon and creates a new node. 24820Sstevel@tonic-gate */ 24830Sstevel@tonic-gate unmount_done = 1; 24840Sstevel@tonic-gate if ((fnip->fi_flags & MF_DIRECT) == 0) { 24850Sstevel@tonic-gate gethrestime(&now); 24860Sstevel@tonic-gate if (fnp->fn_parent == fngp->fng_rootfnnodep) 24870Sstevel@tonic-gate fnp->fn_atime = fnp->fn_mtime = now; 24886046Srmesta else { 24896046Srmesta fnp->fn_parent->fn_atime = now; 24906046Srmesta fnp->fn_parent->fn_mtime = now; 24916046Srmesta } 24920Sstevel@tonic-gate } 24930Sstevel@tonic-gate 24940Sstevel@tonic-gate /* 24950Sstevel@tonic-gate * Free the action list here 24960Sstevel@tonic-gate */ 24970Sstevel@tonic-gate if (alp != NULL) { 24980Sstevel@tonic-gate xdr_free(xdr_action_list, (char *)alp); 24990Sstevel@tonic-gate alp = NULL; 25000Sstevel@tonic-gate } 25010Sstevel@tonic-gate } 25020Sstevel@tonic-gate 25030Sstevel@tonic-gate fnp->fn_ref_time = gethrestime_sec(); 25040Sstevel@tonic-gate 25050Sstevel@tonic-gate next: 25060Sstevel@tonic-gate /* 25070Sstevel@tonic-gate * Obtain parent's readers lock before grabbing 25080Sstevel@tonic-gate * reference to next sibling. 25090Sstevel@tonic-gate * XXX Note that nodes in the top level list (mounted 25100Sstevel@tonic-gate * in user space not by the daemon in the kernel) parent is itself, 25110Sstevel@tonic-gate * therefore grabbing the lock makes no sense, but doesn't 25120Sstevel@tonic-gate * hurt either. 25130Sstevel@tonic-gate */ 25140Sstevel@tonic-gate pfnp = fnp->fn_parent; 25150Sstevel@tonic-gate ASSERT(pfnp != NULL); 25160Sstevel@tonic-gate rw_enter(&pfnp->fn_rwlock, RW_READER); 25170Sstevel@tonic-gate if ((nfnp = fnp->fn_next) != NULL) 25180Sstevel@tonic-gate VN_HOLD(fntovn(nfnp)); 25190Sstevel@tonic-gate rw_exit(&pfnp->fn_rwlock); 25200Sstevel@tonic-gate 25210Sstevel@tonic-gate if (ilocked_it) { 25220Sstevel@tonic-gate mutex_enter(&fnp->fn_lock); 25230Sstevel@tonic-gate if (unmount_done) { 25240Sstevel@tonic-gate /* 25250Sstevel@tonic-gate * Other threads may be waiting for this unmount to 25260Sstevel@tonic-gate * finish. We must let it know that in order to 25270Sstevel@tonic-gate * proceed, it must trigger the mount itself. 25280Sstevel@tonic-gate */ 25290Sstevel@tonic-gate fnp->fn_flags &= ~MF_IK_MOUNT; 25300Sstevel@tonic-gate if (fnp->fn_flags & MF_WAITING) 25310Sstevel@tonic-gate fnp->fn_error = EAGAIN; 25320Sstevel@tonic-gate unmount_done = 0; 25330Sstevel@tonic-gate } 25340Sstevel@tonic-gate AUTOFS_UNBLOCK_OTHERS(fnp, MF_INPROG); 25350Sstevel@tonic-gate mutex_exit(&fnp->fn_lock); 25360Sstevel@tonic-gate ilocked_it = 0; 25370Sstevel@tonic-gate } 25380Sstevel@tonic-gate 25390Sstevel@tonic-gate if (nfnp != NULL) { 25400Sstevel@tonic-gate VN_RELE(vp); 25410Sstevel@tonic-gate fnp = nfnp; 25420Sstevel@tonic-gate /* 25430Sstevel@tonic-gate * Unmount next element 25440Sstevel@tonic-gate */ 25450Sstevel@tonic-gate goto top; 25460Sstevel@tonic-gate } 25470Sstevel@tonic-gate 25480Sstevel@tonic-gate /* 25490Sstevel@tonic-gate * We don't want to unmount rootfnnodep, so the check is made here 25500Sstevel@tonic-gate */ 25510Sstevel@tonic-gate ASSERT(pfnp != fnp); 25520Sstevel@tonic-gate if (pfnp != fngp->fng_rootfnnodep) { 25530Sstevel@tonic-gate /* 25540Sstevel@tonic-gate * Now attempt to unmount my parent 25550Sstevel@tonic-gate */ 25560Sstevel@tonic-gate VN_HOLD(fntovn(pfnp)); 25570Sstevel@tonic-gate VN_RELE(vp); 25580Sstevel@tonic-gate fnp = pfnp; 25590Sstevel@tonic-gate 25600Sstevel@tonic-gate goto top; 25610Sstevel@tonic-gate } 25620Sstevel@tonic-gate 25630Sstevel@tonic-gate VN_RELE(vp); 25640Sstevel@tonic-gate 25650Sstevel@tonic-gate /* 25660Sstevel@tonic-gate * At this point we've walked the entire tree and attempted to unmount 25670Sstevel@tonic-gate * as much as we can one level at a time. 25680Sstevel@tonic-gate */ 25690Sstevel@tonic-gate done: 25700Sstevel@tonic-gate mutex_enter(&unmount_tree_cpr_lock); 25710Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 25720Sstevel@tonic-gate mutex_destroy(&unmount_tree_cpr_lock); 25730Sstevel@tonic-gate } 25740Sstevel@tonic-gate 25750Sstevel@tonic-gate static void 25760Sstevel@tonic-gate unmount_zone_tree(struct autofs_globals *fngp) 25770Sstevel@tonic-gate { 25780Sstevel@tonic-gate unmount_tree(fngp, 0); 25790Sstevel@tonic-gate mutex_enter(&fngp->fng_unmount_threads_lock); 25800Sstevel@tonic-gate fngp->fng_unmount_threads--; 25810Sstevel@tonic-gate mutex_exit(&fngp->fng_unmount_threads_lock); 25820Sstevel@tonic-gate 25830Sstevel@tonic-gate AUTOFS_DPRINT((5, "unmount_tree done. Thread exiting.\n")); 25840Sstevel@tonic-gate 25850Sstevel@tonic-gate zthread_exit(); 25860Sstevel@tonic-gate /* NOTREACHED */ 25870Sstevel@tonic-gate } 25880Sstevel@tonic-gate 25890Sstevel@tonic-gate static int autofs_unmount_thread_timer = 120; /* in seconds */ 25900Sstevel@tonic-gate 25910Sstevel@tonic-gate void 25920Sstevel@tonic-gate auto_do_unmount(struct autofs_globals *fngp) 25930Sstevel@tonic-gate { 25940Sstevel@tonic-gate callb_cpr_t cprinfo; 25950Sstevel@tonic-gate clock_t timeleft; 25960Sstevel@tonic-gate zone_t *zone = curproc->p_zone; 25970Sstevel@tonic-gate 25980Sstevel@tonic-gate CALLB_CPR_INIT(&cprinfo, &fngp->fng_unmount_threads_lock, 25996046Srmesta callb_generic_cpr, "auto_do_unmount"); 26000Sstevel@tonic-gate 26010Sstevel@tonic-gate for (;;) { /* forever */ 26020Sstevel@tonic-gate mutex_enter(&fngp->fng_unmount_threads_lock); 26030Sstevel@tonic-gate CALLB_CPR_SAFE_BEGIN(&cprinfo); 26040Sstevel@tonic-gate newthread: 26050Sstevel@tonic-gate mutex_exit(&fngp->fng_unmount_threads_lock); 26060Sstevel@tonic-gate timeleft = zone_status_timedwait(zone, lbolt + 26070Sstevel@tonic-gate autofs_unmount_thread_timer * hz, ZONE_IS_SHUTTING_DOWN); 26080Sstevel@tonic-gate mutex_enter(&fngp->fng_unmount_threads_lock); 26090Sstevel@tonic-gate 26100Sstevel@tonic-gate if (timeleft != -1) { /* didn't time out */ 26110Sstevel@tonic-gate ASSERT(zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN); 26120Sstevel@tonic-gate /* 26130Sstevel@tonic-gate * zone is exiting... don't create any new threads. 26140Sstevel@tonic-gate * fng_unmount_threads_lock is released implicitly by 26150Sstevel@tonic-gate * the below. 26160Sstevel@tonic-gate */ 26170Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, 26186046Srmesta &fngp->fng_unmount_threads_lock); 26190Sstevel@tonic-gate CALLB_CPR_EXIT(&cprinfo); 26200Sstevel@tonic-gate zthread_exit(); 26210Sstevel@tonic-gate /* NOTREACHED */ 26220Sstevel@tonic-gate } 26230Sstevel@tonic-gate if (fngp->fng_unmount_threads < autofs_unmount_threads) { 26240Sstevel@tonic-gate fngp->fng_unmount_threads++; 26250Sstevel@tonic-gate CALLB_CPR_SAFE_END(&cprinfo, 26266046Srmesta &fngp->fng_unmount_threads_lock); 26270Sstevel@tonic-gate mutex_exit(&fngp->fng_unmount_threads_lock); 26280Sstevel@tonic-gate 26290Sstevel@tonic-gate (void) zthread_create(NULL, 0, unmount_zone_tree, fngp, 26300Sstevel@tonic-gate 0, minclsyspri); 26310Sstevel@tonic-gate } else 26320Sstevel@tonic-gate goto newthread; 26330Sstevel@tonic-gate } 26340Sstevel@tonic-gate /* NOTREACHED */ 26350Sstevel@tonic-gate } 26360Sstevel@tonic-gate 26370Sstevel@tonic-gate /* 26380Sstevel@tonic-gate * Is nobrowse specified in option string? 26390Sstevel@tonic-gate * opts should be a null ('\0') terminated string. 26400Sstevel@tonic-gate * Returns non-zero if nobrowse has been specified. 26410Sstevel@tonic-gate */ 26420Sstevel@tonic-gate int 26430Sstevel@tonic-gate auto_nobrowse_option(char *opts) 26440Sstevel@tonic-gate { 26450Sstevel@tonic-gate char *buf; 26460Sstevel@tonic-gate char *p; 26470Sstevel@tonic-gate char *t; 26480Sstevel@tonic-gate int nobrowse = 0; 26490Sstevel@tonic-gate int last_opt = 0; 26500Sstevel@tonic-gate size_t len; 26510Sstevel@tonic-gate 26520Sstevel@tonic-gate len = strlen(opts) + 1; 26530Sstevel@tonic-gate p = buf = kmem_alloc(len, KM_SLEEP); 26540Sstevel@tonic-gate (void) strcpy(buf, opts); 26550Sstevel@tonic-gate do { 26560Sstevel@tonic-gate if (t = strchr(p, ',')) 26570Sstevel@tonic-gate *t++ = '\0'; 26580Sstevel@tonic-gate else 26590Sstevel@tonic-gate last_opt++; 26600Sstevel@tonic-gate if (strcmp(p, MNTOPT_NOBROWSE) == 0) 26610Sstevel@tonic-gate nobrowse = 1; 26620Sstevel@tonic-gate else if (strcmp(p, MNTOPT_BROWSE) == 0) 26630Sstevel@tonic-gate nobrowse = 0; 26640Sstevel@tonic-gate p = t; 26650Sstevel@tonic-gate } while (!last_opt); 26660Sstevel@tonic-gate kmem_free(buf, len); 26670Sstevel@tonic-gate 26680Sstevel@tonic-gate return (nobrowse); 26690Sstevel@tonic-gate } 26700Sstevel@tonic-gate 26710Sstevel@tonic-gate /* 26720Sstevel@tonic-gate * used to log warnings only if automountd is running 26730Sstevel@tonic-gate * with verbose mode set 26740Sstevel@tonic-gate */ 26752170Sevanl 26760Sstevel@tonic-gate void 26772170Sevanl auto_log(int verbose, zoneid_t zoneid, int level, const char *fmt, ...) 26780Sstevel@tonic-gate { 26792170Sevanl va_list args; 26800Sstevel@tonic-gate 26812170Sevanl if (verbose) { 26820Sstevel@tonic-gate va_start(args, fmt); 26832170Sevanl vzcmn_err(zoneid, level, fmt, args); 26840Sstevel@tonic-gate va_end(args); 26850Sstevel@tonic-gate } 26860Sstevel@tonic-gate } 26870Sstevel@tonic-gate 26880Sstevel@tonic-gate #ifdef DEBUG 26890Sstevel@tonic-gate static int autofs_debug = 0; 26900Sstevel@tonic-gate 26910Sstevel@tonic-gate /* 26920Sstevel@tonic-gate * Utilities used by both client and server 26930Sstevel@tonic-gate * Standard levels: 26940Sstevel@tonic-gate * 0) no debugging 26950Sstevel@tonic-gate * 1) hard failures 26960Sstevel@tonic-gate * 2) soft failures 26970Sstevel@tonic-gate * 3) current test software 26980Sstevel@tonic-gate * 4) main procedure entry points 26990Sstevel@tonic-gate * 5) main procedure exit points 27000Sstevel@tonic-gate * 6) utility procedure entry points 27010Sstevel@tonic-gate * 7) utility procedure exit points 27020Sstevel@tonic-gate * 8) obscure procedure entry points 27030Sstevel@tonic-gate * 9) obscure procedure exit points 27040Sstevel@tonic-gate * 10) random stuff 27050Sstevel@tonic-gate * 11) all <= 1 27060Sstevel@tonic-gate * 12) all <= 2 27070Sstevel@tonic-gate * 13) all <= 3 27080Sstevel@tonic-gate * ... 27090Sstevel@tonic-gate */ 27100Sstevel@tonic-gate /* PRINTFLIKE2 */ 27110Sstevel@tonic-gate void 27120Sstevel@tonic-gate auto_dprint(int level, const char *fmt, ...) 27130Sstevel@tonic-gate { 27140Sstevel@tonic-gate va_list args; 27150Sstevel@tonic-gate 27160Sstevel@tonic-gate if (autofs_debug == level || 27170Sstevel@tonic-gate (autofs_debug > 10 && (autofs_debug - 10) >= level)) { 27180Sstevel@tonic-gate va_start(args, fmt); 27190Sstevel@tonic-gate (void) vprintf(fmt, args); 27200Sstevel@tonic-gate va_end(args); 27210Sstevel@tonic-gate } 27220Sstevel@tonic-gate } 27230Sstevel@tonic-gate #endif /* DEBUG */ 2724