14bff34e3Sthurlow /*
24bff34e3Sthurlow * Copyright (c) 2000-2001, Boris Popov
34bff34e3Sthurlow * All rights reserved.
44bff34e3Sthurlow *
54bff34e3Sthurlow * Redistribution and use in source and binary forms, with or without
64bff34e3Sthurlow * modification, are permitted provided that the following conditions
74bff34e3Sthurlow * are met:
84bff34e3Sthurlow * 1. Redistributions of source code must retain the above copyright
94bff34e3Sthurlow * notice, this list of conditions and the following disclaimer.
104bff34e3Sthurlow * 2. Redistributions in binary form must reproduce the above copyright
114bff34e3Sthurlow * notice, this list of conditions and the following disclaimer in the
124bff34e3Sthurlow * documentation and/or other materials provided with the distribution.
134bff34e3Sthurlow * 3. All advertising materials mentioning features or use of this software
144bff34e3Sthurlow * must display the following acknowledgement:
154bff34e3Sthurlow * This product includes software developed by Boris Popov.
164bff34e3Sthurlow * 4. Neither the name of the author nor the names of any co-contributors
174bff34e3Sthurlow * may be used to endorse or promote products derived from this software
184bff34e3Sthurlow * without specific prior written permission.
194bff34e3Sthurlow *
204bff34e3Sthurlow * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
214bff34e3Sthurlow * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
224bff34e3Sthurlow * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
234bff34e3Sthurlow * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
244bff34e3Sthurlow * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
254bff34e3Sthurlow * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
264bff34e3Sthurlow * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
274bff34e3Sthurlow * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
284bff34e3Sthurlow * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
294bff34e3Sthurlow * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
304bff34e3Sthurlow * SUCH DAMAGE.
314bff34e3Sthurlow *
324bff34e3Sthurlow * $Id: smbfs_vfsops.c,v 1.73.64.1 2005/05/27 02:35:28 lindak Exp $
334bff34e3Sthurlow */
344bff34e3Sthurlow
354bff34e3Sthurlow /*
360fbb751dSJohn Levon * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
378cd81a20SJerry Jelinek * Copyright 2013, Joyent, Inc. All rights reserved.
3848bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
39*8329232eSGordon Ross * Copyright 2017 Nexenta Systems, Inc. All rights reserved.
404bff34e3Sthurlow */
414bff34e3Sthurlow
424bff34e3Sthurlow #include <sys/systm.h>
434bff34e3Sthurlow #include <sys/cred.h>
4402d09e03SGordon Ross #include <sys/time.h>
454bff34e3Sthurlow #include <sys/vfs.h>
464bff34e3Sthurlow #include <sys/vnode.h>
474bff34e3Sthurlow #include <fs/fs_subr.h>
484bff34e3Sthurlow #include <sys/sysmacros.h>
494bff34e3Sthurlow #include <sys/kmem.h>
504bff34e3Sthurlow #include <sys/mkdev.h>
514bff34e3Sthurlow #include <sys/mount.h>
524bff34e3Sthurlow #include <sys/statvfs.h>
534bff34e3Sthurlow #include <sys/errno.h>
544bff34e3Sthurlow #include <sys/debug.h>
554e72ade1SGordon Ross #include <sys/disp.h>
564bff34e3Sthurlow #include <sys/cmn_err.h>
574bff34e3Sthurlow #include <sys/modctl.h>
584bff34e3Sthurlow #include <sys/policy.h>
594bff34e3Sthurlow #include <sys/atomic.h>
604bff34e3Sthurlow #include <sys/zone.h>
614bff34e3Sthurlow #include <sys/vfs_opreg.h>
624bff34e3Sthurlow #include <sys/mntent.h>
634bff34e3Sthurlow #include <sys/priv.h>
644e72ade1SGordon Ross #include <sys/taskq.h>
654bff34e3Sthurlow #include <sys/tsol/label.h>
664bff34e3Sthurlow #include <sys/tsol/tndb.h>
674bff34e3Sthurlow #include <inet/ip.h>
684bff34e3Sthurlow
694bff34e3Sthurlow #include <netsmb/smb_osdep.h>
704bff34e3Sthurlow #include <netsmb/smb.h>
714bff34e3Sthurlow #include <netsmb/smb_conn.h>
724bff34e3Sthurlow #include <netsmb/smb_subr.h>
734bff34e3Sthurlow #include <netsmb/smb_dev.h>
744bff34e3Sthurlow
754bff34e3Sthurlow #include <smbfs/smbfs.h>
764bff34e3Sthurlow #include <smbfs/smbfs_node.h>
774bff34e3Sthurlow #include <smbfs/smbfs_subr.h>
784bff34e3Sthurlow
79*8329232eSGordon Ross #ifndef _KERNEL
80*8329232eSGordon Ross
81*8329232eSGordon Ross #include <libfksmbfs.h>
82*8329232eSGordon Ross
83*8329232eSGordon Ross #define STRUCT_DECL(s, a) struct s a
84*8329232eSGordon Ross #define STRUCT_FGET(handle, field) ((handle).field)
85*8329232eSGordon Ross #define _init(v) fksmbfs_init(v)
86*8329232eSGordon Ross #define _fini(v) fksmbfs_fini(v)
87*8329232eSGordon Ross
88*8329232eSGordon Ross #endif /* !_KERNEL */
89*8329232eSGordon Ross
909005860cSGordon Ross /*
919005860cSGordon Ross * Should smbfs mount enable "-o acl" by default? There are good
929005860cSGordon Ross * arguments for both. The most common use case is individual users
939005860cSGordon Ross * accessing files on some SMB server, for which "noacl" is the more
949005860cSGordon Ross * convenient default. A less common use case is data migration,
959005860cSGordon Ross * where the "acl" option might be a desirable default. We'll make
969005860cSGordon Ross * the common use case the default. This default can be changed via
979005860cSGordon Ross * /etc/system, and/or set per-mount via the "acl" mount option.
989005860cSGordon Ross */
999005860cSGordon Ross int smbfs_default_opt_acl = 0;
1009005860cSGordon Ross
1014e72ade1SGordon Ross /*
1024e72ade1SGordon Ross * How many taskq threads per-mount should we use.
1034e72ade1SGordon Ross * Just one is fine (until we do more async work).
1044e72ade1SGordon Ross */
1054e72ade1SGordon Ross int smbfs_tq_nthread = 1;
1064e72ade1SGordon Ross
1074bff34e3Sthurlow /*
1084bff34e3Sthurlow * Local functions definitions.
1094bff34e3Sthurlow */
1104bff34e3Sthurlow int smbfsinit(int fstyp, char *name);
1114bff34e3Sthurlow void smbfsfini();
112*8329232eSGordon Ross
113*8329232eSGordon Ross #ifdef _KERNEL
1144bff34e3Sthurlow static int smbfs_mount_label_policy(vfs_t *, void *, int, cred_t *);
115*8329232eSGordon Ross #endif /* _KERNEL */
1164bff34e3Sthurlow
11791d632c8Sgwr /*
11891d632c8Sgwr * SMBFS Mount options table for MS_OPTIONSTR
11991d632c8Sgwr * Note: These are not all the options.
12091d632c8Sgwr * Some options come in via MS_DATA.
12191d632c8Sgwr * Others are generic (see vfs.c)
12291d632c8Sgwr */
12391d632c8Sgwr static char *intr_cancel[] = { MNTOPT_NOINTR, NULL };
12491d632c8Sgwr static char *nointr_cancel[] = { MNTOPT_INTR, NULL };
125bd7c6f51SGordon Ross static char *acl_cancel[] = { MNTOPT_NOACL, NULL };
126bd7c6f51SGordon Ross static char *noacl_cancel[] = { MNTOPT_ACL, NULL };
12791d632c8Sgwr static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
12891d632c8Sgwr static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
12991d632c8Sgwr
13091d632c8Sgwr static mntopt_t mntopts[] = {
13191d632c8Sgwr /*
13291d632c8Sgwr * option name cancel option default arg flags
13391d632c8Sgwr * ufs arg flag
13491d632c8Sgwr */
13591d632c8Sgwr { MNTOPT_INTR, intr_cancel, NULL, MO_DEFAULT, 0 },
13691d632c8Sgwr { MNTOPT_NOINTR, nointr_cancel, NULL, 0, 0 },
1379005860cSGordon Ross { MNTOPT_ACL, acl_cancel, NULL, 0, 0 },
138bd7c6f51SGordon Ross { MNTOPT_NOACL, noacl_cancel, NULL, 0, 0 },
13991d632c8Sgwr { MNTOPT_XATTR, xattr_cancel, NULL, MO_DEFAULT, 0 },
140*8329232eSGordon Ross { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, 0 },
141*8329232eSGordon Ross #ifndef _KERNEL
142*8329232eSGordon Ross /* See vfs_optionisset MNTOPT_NOAC below. */
143*8329232eSGordon Ross { MNTOPT_NOAC, NULL, NULL, 0, 0 },
144*8329232eSGordon Ross #endif /* !_KERNEL */
14591d632c8Sgwr };
14691d632c8Sgwr
14791d632c8Sgwr static mntopts_t smbfs_mntopts = {
14891d632c8Sgwr sizeof (mntopts) / sizeof (mntopt_t),
14991d632c8Sgwr mntopts
15091d632c8Sgwr };
15191d632c8Sgwr
152613a2f6bSGordon Ross static const char fs_type_name[FSTYPSZ] = "smbfs";
153613a2f6bSGordon Ross
1544bff34e3Sthurlow static vfsdef_t vfw = {
1554bff34e3Sthurlow VFSDEF_VERSION,
156613a2f6bSGordon Ross (char *)fs_type_name,
1574bff34e3Sthurlow smbfsinit, /* init routine */
1588cd81a20SJerry Jelinek VSW_HASPROTO|VSW_NOTZONESAFE, /* flags */
15991d632c8Sgwr &smbfs_mntopts /* mount options table prototype */
1604bff34e3Sthurlow };
1614bff34e3Sthurlow
162*8329232eSGordon Ross #ifdef _KERNEL
1634bff34e3Sthurlow static struct modlfs modlfs = {
1644bff34e3Sthurlow &mod_fsops,
165613a2f6bSGordon Ross "SMBFS filesystem",
1664bff34e3Sthurlow &vfw
1674bff34e3Sthurlow };
1684bff34e3Sthurlow
1694bff34e3Sthurlow static struct modlinkage modlinkage = {
1704bff34e3Sthurlow MODREV_1, (void *)&modlfs, NULL
1714bff34e3Sthurlow };
172*8329232eSGordon Ross #endif /* _KERNEL */
1734bff34e3Sthurlow
1744bff34e3Sthurlow /*
1754bff34e3Sthurlow * Mutex to protect the following variables:
1764bff34e3Sthurlow * smbfs_major
1774bff34e3Sthurlow * smbfs_minor
1784bff34e3Sthurlow */
1794bff34e3Sthurlow extern kmutex_t smbfs_minor_lock;
1804bff34e3Sthurlow extern int smbfs_major;
1814bff34e3Sthurlow extern int smbfs_minor;
1824bff34e3Sthurlow
1834bff34e3Sthurlow /*
1844bff34e3Sthurlow * Prevent unloads while we have mounts
1854bff34e3Sthurlow */
1864bff34e3Sthurlow uint32_t smbfs_mountcount;
1874bff34e3Sthurlow
1884bff34e3Sthurlow /*
1894bff34e3Sthurlow * smbfs vfs operations.
1904bff34e3Sthurlow */
1914bff34e3Sthurlow static int smbfs_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
1924bff34e3Sthurlow static int smbfs_unmount(vfs_t *, int, cred_t *);
1934bff34e3Sthurlow static int smbfs_root(vfs_t *, vnode_t **);
1944bff34e3Sthurlow static int smbfs_statvfs(vfs_t *, statvfs64_t *);
1954bff34e3Sthurlow static int smbfs_sync(vfs_t *, short, cred_t *);
1964bff34e3Sthurlow static void smbfs_freevfs(vfs_t *);
1974bff34e3Sthurlow
1984bff34e3Sthurlow /*
1994bff34e3Sthurlow * Module loading
2004bff34e3Sthurlow */
2014bff34e3Sthurlow
2024bff34e3Sthurlow /*
2034bff34e3Sthurlow * This routine is invoked automatically when the kernel module
2044bff34e3Sthurlow * containing this routine is loaded. This allows module specific
2054bff34e3Sthurlow * initialization to be done when the module is loaded.
2064bff34e3Sthurlow */
2074bff34e3Sthurlow int
_init(void)2084bff34e3Sthurlow _init(void)
2094bff34e3Sthurlow {
21002d09e03SGordon Ross int error;
2114bff34e3Sthurlow
2124bff34e3Sthurlow /*
2134bff34e3Sthurlow * Check compiled-in version of "nsmb"
2144bff34e3Sthurlow * that we're linked with. (paranoid)
2154bff34e3Sthurlow */
2164bff34e3Sthurlow if (nsmb_version != NSMB_VERSION) {
2174bff34e3Sthurlow cmn_err(CE_WARN, "_init: nsmb version mismatch");
2184bff34e3Sthurlow return (ENOTTY);
2194bff34e3Sthurlow }
2204bff34e3Sthurlow
2214bff34e3Sthurlow smbfs_mountcount = 0;
2224bff34e3Sthurlow
22302d09e03SGordon Ross /*
22402d09e03SGordon Ross * NFS calls these two in _clntinit
22502d09e03SGordon Ross * Easier to follow this way.
22602d09e03SGordon Ross */
22702d09e03SGordon Ross if ((error = smbfs_subrinit()) != 0) {
22802d09e03SGordon Ross cmn_err(CE_WARN, "_init: smbfs_subrinit failed");
22902d09e03SGordon Ross return (error);
23002d09e03SGordon Ross }
23102d09e03SGordon Ross
23202d09e03SGordon Ross if ((error = smbfs_vfsinit()) != 0) {
23302d09e03SGordon Ross cmn_err(CE_WARN, "_init: smbfs_vfsinit failed");
23402d09e03SGordon Ross smbfs_subrfini();
23502d09e03SGordon Ross return (error);
23602d09e03SGordon Ross }
23702d09e03SGordon Ross
23802d09e03SGordon Ross if ((error = smbfs_clntinit()) != 0) {
2394bff34e3Sthurlow cmn_err(CE_WARN, "_init: smbfs_clntinit failed");
24002d09e03SGordon Ross smbfs_vfsfini();
24102d09e03SGordon Ross smbfs_subrfini();
24202d09e03SGordon Ross return (error);
2434bff34e3Sthurlow }
2444bff34e3Sthurlow
245*8329232eSGordon Ross #ifdef _KERNEL
24602d09e03SGordon Ross error = mod_install((struct modlinkage *)&modlinkage);
247*8329232eSGordon Ross #else /* _KERNEL */
248*8329232eSGordon Ross error = fake_installfs(&vfw);
249*8329232eSGordon Ross #endif /* _KERNEL */
250*8329232eSGordon Ross
25102d09e03SGordon Ross return (error);
2524bff34e3Sthurlow }
2534bff34e3Sthurlow
2544bff34e3Sthurlow /*
2554bff34e3Sthurlow * Free kernel module resources that were allocated in _init
2564bff34e3Sthurlow * and remove the linkage information into the kernel
2574bff34e3Sthurlow */
2584bff34e3Sthurlow int
_fini(void)2594bff34e3Sthurlow _fini(void)
2604bff34e3Sthurlow {
2614bff34e3Sthurlow int error;
2624bff34e3Sthurlow
2634bff34e3Sthurlow /*
2644bff34e3Sthurlow * If a forcedly unmounted instance is still hanging around,
2654bff34e3Sthurlow * we cannot allow the module to be unloaded because that would
2664bff34e3Sthurlow * cause panics once the VFS framework decides it's time to call
2674bff34e3Sthurlow * into VFS_FREEVFS().
2684bff34e3Sthurlow */
2694bff34e3Sthurlow if (smbfs_mountcount)
2704bff34e3Sthurlow return (EBUSY);
2714bff34e3Sthurlow
272*8329232eSGordon Ross #ifdef _KERNEL
2734bff34e3Sthurlow error = mod_remove(&modlinkage);
274*8329232eSGordon Ross #else /* _KERNEL */
275*8329232eSGordon Ross error = fake_removefs(&vfw);
276*8329232eSGordon Ross #endif /* _KERNEL */
2774bff34e3Sthurlow if (error)
2784bff34e3Sthurlow return (error);
2794bff34e3Sthurlow
2804bff34e3Sthurlow /*
2814bff34e3Sthurlow * Free the allocated smbnodes, etc.
2824bff34e3Sthurlow */
2834bff34e3Sthurlow smbfs_clntfini();
2844bff34e3Sthurlow
28502d09e03SGordon Ross /* NFS calls these two in _clntfini */
28602d09e03SGordon Ross smbfs_vfsfini();
28702d09e03SGordon Ross smbfs_subrfini();
28802d09e03SGordon Ross
2894bff34e3Sthurlow /*
2904bff34e3Sthurlow * Free the ops vectors
2914bff34e3Sthurlow */
2924bff34e3Sthurlow smbfsfini();
2934bff34e3Sthurlow return (0);
2944bff34e3Sthurlow }
2954bff34e3Sthurlow
2964bff34e3Sthurlow /*
2974bff34e3Sthurlow * Return information about the module
2984bff34e3Sthurlow */
299*8329232eSGordon Ross #ifdef _KERNEL
3004bff34e3Sthurlow int
_info(struct modinfo * modinfop)3014bff34e3Sthurlow _info(struct modinfo *modinfop)
3024bff34e3Sthurlow {
3034bff34e3Sthurlow return (mod_info((struct modlinkage *)&modlinkage, modinfop));
3044bff34e3Sthurlow }
305*8329232eSGordon Ross #endif /* _KERNEL */
3064bff34e3Sthurlow
3074bff34e3Sthurlow /*
3084bff34e3Sthurlow * Initialize the vfs structure
3094bff34e3Sthurlow */
3104bff34e3Sthurlow
311*8329232eSGordon Ross int smbfs_fstyp;
3124bff34e3Sthurlow vfsops_t *smbfs_vfsops = NULL;
3134bff34e3Sthurlow
3144bff34e3Sthurlow static const fs_operation_def_t smbfs_vfsops_template[] = {
3154bff34e3Sthurlow { VFSNAME_MOUNT, { .vfs_mount = smbfs_mount } },
3164bff34e3Sthurlow { VFSNAME_UNMOUNT, { .vfs_unmount = smbfs_unmount } },
3174bff34e3Sthurlow { VFSNAME_ROOT, { .vfs_root = smbfs_root } },
3184bff34e3Sthurlow { VFSNAME_STATVFS, { .vfs_statvfs = smbfs_statvfs } },
3194bff34e3Sthurlow { VFSNAME_SYNC, { .vfs_sync = smbfs_sync } },
3204bff34e3Sthurlow { VFSNAME_VGET, { .error = fs_nosys } },
3214bff34e3Sthurlow { VFSNAME_MOUNTROOT, { .error = fs_nosys } },
3224bff34e3Sthurlow { VFSNAME_FREEVFS, { .vfs_freevfs = smbfs_freevfs } },
3234bff34e3Sthurlow { NULL, NULL }
3244bff34e3Sthurlow };
3254bff34e3Sthurlow
326*8329232eSGordon Ross /*
327*8329232eSGordon Ross * This is the VFS switch initialization routine, normally called
328*8329232eSGordon Ross * via vfssw[x].vsw_init by vfsinit() or mod_install
329*8329232eSGordon Ross */
3304bff34e3Sthurlow int
smbfsinit(int fstyp,char * name)3314bff34e3Sthurlow smbfsinit(int fstyp, char *name)
3324bff34e3Sthurlow {
3334bff34e3Sthurlow int error;
3344bff34e3Sthurlow
3354bff34e3Sthurlow error = vfs_setfsops(fstyp, smbfs_vfsops_template, &smbfs_vfsops);
3364bff34e3Sthurlow if (error != 0) {
337*8329232eSGordon Ross cmn_err(CE_WARN,
3384bff34e3Sthurlow "smbfsinit: bad vfs ops template");
3394bff34e3Sthurlow return (error);
3404bff34e3Sthurlow }
3414bff34e3Sthurlow
3424bff34e3Sthurlow error = vn_make_ops(name, smbfs_vnodeops_template, &smbfs_vnodeops);
3434bff34e3Sthurlow if (error != 0) {
3444bff34e3Sthurlow (void) vfs_freevfsops_by_type(fstyp);
345*8329232eSGordon Ross cmn_err(CE_WARN,
3464bff34e3Sthurlow "smbfsinit: bad vnode ops template");
3474bff34e3Sthurlow return (error);
3484bff34e3Sthurlow }
3494bff34e3Sthurlow
350*8329232eSGordon Ross smbfs_fstyp = fstyp;
3514bff34e3Sthurlow
3524bff34e3Sthurlow return (0);
3534bff34e3Sthurlow }
3544bff34e3Sthurlow
3554bff34e3Sthurlow void
smbfsfini()3564bff34e3Sthurlow smbfsfini()
3574bff34e3Sthurlow {
3584bff34e3Sthurlow if (smbfs_vfsops) {
359*8329232eSGordon Ross (void) vfs_freevfsops_by_type(smbfs_fstyp);
3604bff34e3Sthurlow smbfs_vfsops = NULL;
3614bff34e3Sthurlow }
3624bff34e3Sthurlow if (smbfs_vnodeops) {
3634bff34e3Sthurlow vn_freevnodeops(smbfs_vnodeops);
3644bff34e3Sthurlow smbfs_vnodeops = NULL;
3654bff34e3Sthurlow }
3664bff34e3Sthurlow }
3674bff34e3Sthurlow
3684bff34e3Sthurlow void
smbfs_free_smi(smbmntinfo_t * smi)3694bff34e3Sthurlow smbfs_free_smi(smbmntinfo_t *smi)
3704bff34e3Sthurlow {
37102d09e03SGordon Ross if (smi == NULL)
37202d09e03SGordon Ross return;
37302d09e03SGordon Ross
374*8329232eSGordon Ross #ifdef _KERNEL
375a19609f8Sjv if (smi->smi_zone_ref.zref_zone != NULL)
376a19609f8Sjv zone_rele_ref(&smi->smi_zone_ref, ZONE_REF_SMBFS);
377*8329232eSGordon Ross #endif /* _KERNEL */
37802d09e03SGordon Ross
37902d09e03SGordon Ross if (smi->smi_share != NULL)
38002d09e03SGordon Ross smb_share_rele(smi->smi_share);
38102d09e03SGordon Ross
38202d09e03SGordon Ross avl_destroy(&smi->smi_hash_avl);
38302d09e03SGordon Ross rw_destroy(&smi->smi_hash_lk);
38402d09e03SGordon Ross cv_destroy(&smi->smi_statvfs_cv);
38502d09e03SGordon Ross mutex_destroy(&smi->smi_lock);
38602d09e03SGordon Ross
38702d09e03SGordon Ross kmem_free(smi, sizeof (smbmntinfo_t));
3884bff34e3Sthurlow }
3894bff34e3Sthurlow
3904bff34e3Sthurlow /*
3914bff34e3Sthurlow * smbfs mount vfsop
3924bff34e3Sthurlow * Set up mount info record and attach it to vfs struct.
3934bff34e3Sthurlow */
3944bff34e3Sthurlow static int
smbfs_mount(vfs_t * vfsp,vnode_t * mvp,struct mounta * uap,cred_t * cr)3954bff34e3Sthurlow smbfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
3964bff34e3Sthurlow {
3974bff34e3Sthurlow char *data = uap->dataptr;
3984bff34e3Sthurlow int error;
3995f4fc069Sjilinxpd smbnode_t *rtnp = NULL; /* root of this fs */
4005f4fc069Sjilinxpd smbmntinfo_t *smi = NULL;
4015f4fc069Sjilinxpd dev_t smbfs_dev;
4025f4fc069Sjilinxpd int version;
4035f4fc069Sjilinxpd int devfd;
404*8329232eSGordon Ross zone_t *zone = curzone;
405*8329232eSGordon Ross #ifdef _KERNEL
4064bff34e3Sthurlow zone_t *mntzone = NULL;
407*8329232eSGordon Ross #else /* _KERNEL */
408*8329232eSGordon Ross short minclsyspri = MINCLSYSPRI;
409*8329232eSGordon Ross #endif /* _KERNEL */
4105f4fc069Sjilinxpd smb_share_t *ssp = NULL;
4115f4fc069Sjilinxpd smb_cred_t scred;
41202d09e03SGordon Ross int flags, sec;
4134bff34e3Sthurlow STRUCT_DECL(smbfs_args, args); /* smbfs mount arguments */
4144bff34e3Sthurlow
415*8329232eSGordon Ross #ifdef _KERNEL
4164bff34e3Sthurlow if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
4174bff34e3Sthurlow return (error);
418*8329232eSGordon Ross #endif /* _KERNEL */
4194bff34e3Sthurlow
4204bff34e3Sthurlow if (mvp->v_type != VDIR)
4214bff34e3Sthurlow return (ENOTDIR);
4224bff34e3Sthurlow
4234bff34e3Sthurlow /*
4244bff34e3Sthurlow * get arguments
4254bff34e3Sthurlow *
4264bff34e3Sthurlow * uap->datalen might be different from sizeof (args)
4274bff34e3Sthurlow * in a compatible situation.
4284bff34e3Sthurlow */
429*8329232eSGordon Ross #ifdef _KERNEL
4304bff34e3Sthurlow STRUCT_INIT(args, get_udatamodel());
4314bff34e3Sthurlow bzero(STRUCT_BUF(args), SIZEOF_STRUCT(smbfs_args, DATAMODEL_NATIVE));
4324bff34e3Sthurlow if (copyin(data, STRUCT_BUF(args), MIN(uap->datalen,
4334bff34e3Sthurlow SIZEOF_STRUCT(smbfs_args, DATAMODEL_NATIVE))))
4344bff34e3Sthurlow return (EFAULT);
435*8329232eSGordon Ross #else /* _KERNEL */
436*8329232eSGordon Ross bzero(&args, sizeof (args));
437*8329232eSGordon Ross if (copyin(data, &args, MIN(uap->datalen, sizeof (args))))
438*8329232eSGordon Ross return (EFAULT);
439*8329232eSGordon Ross #endif /* _KERNEL */
4404bff34e3Sthurlow
4414bff34e3Sthurlow /*
4424bff34e3Sthurlow * Check mount program version
4434bff34e3Sthurlow */
4444bff34e3Sthurlow version = STRUCT_FGET(args, version);
4454bff34e3Sthurlow if (version != SMBFS_VERSION) {
4464bff34e3Sthurlow cmn_err(CE_WARN, "mount version mismatch:"
4474bff34e3Sthurlow " kernel=%d, mount=%d\n",
4484bff34e3Sthurlow SMBFS_VERSION, version);
4494bff34e3Sthurlow return (EINVAL);
4504bff34e3Sthurlow }
4514bff34e3Sthurlow
45202d09e03SGordon Ross /*
45302d09e03SGordon Ross * Deal with re-mount requests.
45402d09e03SGordon Ross */
4554bff34e3Sthurlow if (uap->flags & MS_REMOUNT) {
4564bff34e3Sthurlow cmn_err(CE_WARN, "MS_REMOUNT not implemented");
4574bff34e3Sthurlow return (ENOTSUP);
4584bff34e3Sthurlow }
4594bff34e3Sthurlow
4604bff34e3Sthurlow /*
4614bff34e3Sthurlow * Check for busy
4624bff34e3Sthurlow */
4634bff34e3Sthurlow mutex_enter(&mvp->v_lock);
4644bff34e3Sthurlow if (!(uap->flags & MS_OVERLAY) &&
4654bff34e3Sthurlow (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
4664bff34e3Sthurlow mutex_exit(&mvp->v_lock);
4674bff34e3Sthurlow return (EBUSY);
4684bff34e3Sthurlow }
4694bff34e3Sthurlow mutex_exit(&mvp->v_lock);
4704bff34e3Sthurlow
4714bff34e3Sthurlow /*
4724bff34e3Sthurlow * Get the "share" from the netsmb driver (ssp).
4734bff34e3Sthurlow * It is returned with a "ref" (hold) for us.
4744bff34e3Sthurlow * Release this hold: at errout below, or in
4754bff34e3Sthurlow * smbfs_freevfs().
4764bff34e3Sthurlow */
4774bff34e3Sthurlow devfd = STRUCT_FGET(args, devfd);
4784bff34e3Sthurlow error = smb_dev2share(devfd, &ssp);
4794bff34e3Sthurlow if (error) {
4804bff34e3Sthurlow cmn_err(CE_WARN, "invalid device handle %d (%d)\n",
4814bff34e3Sthurlow devfd, error);
4824bff34e3Sthurlow return (error);
4834bff34e3Sthurlow }
4844bff34e3Sthurlow
4854bff34e3Sthurlow /*
4864bff34e3Sthurlow * Use "goto errout" from here on.
48702d09e03SGordon Ross * See: ssp, smi, rtnp, mntzone
4884bff34e3Sthurlow */
4894bff34e3Sthurlow
490*8329232eSGordon Ross #ifdef _KERNEL
4914bff34e3Sthurlow /*
4924bff34e3Sthurlow * Determine the zone we're being mounted into.
4934bff34e3Sthurlow */
4944bff34e3Sthurlow zone_hold(mntzone = zone); /* start with this assumption */
4954bff34e3Sthurlow if (getzoneid() == GLOBAL_ZONEID) {
4964bff34e3Sthurlow zone_rele(mntzone);
4974bff34e3Sthurlow mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
4984bff34e3Sthurlow ASSERT(mntzone != NULL);
4994bff34e3Sthurlow if (mntzone != zone) {
5004bff34e3Sthurlow error = EBUSY;
5014bff34e3Sthurlow goto errout;
5024bff34e3Sthurlow }
5034bff34e3Sthurlow }
5044bff34e3Sthurlow
5054bff34e3Sthurlow /*
5064bff34e3Sthurlow * Stop the mount from going any further if the zone is going away.
5074bff34e3Sthurlow */
5084bff34e3Sthurlow if (zone_status_get(mntzone) >= ZONE_IS_SHUTTING_DOWN) {
5094bff34e3Sthurlow error = EBUSY;
5104bff34e3Sthurlow goto errout;
5114bff34e3Sthurlow }
5124bff34e3Sthurlow
5134bff34e3Sthurlow /*
5144bff34e3Sthurlow * On a Trusted Extensions client, we may have to force read-only
5154bff34e3Sthurlow * for read-down mounts.
5164bff34e3Sthurlow */
5174bff34e3Sthurlow if (is_system_labeled()) {
5184bff34e3Sthurlow void *addr;
5194bff34e3Sthurlow int ipvers = 0;
5204bff34e3Sthurlow struct smb_vc *vcp;
5214bff34e3Sthurlow
5224bff34e3Sthurlow vcp = SSTOVC(ssp);
5234bff34e3Sthurlow addr = smb_vc_getipaddr(vcp, &ipvers);
5244bff34e3Sthurlow error = smbfs_mount_label_policy(vfsp, addr, ipvers, cr);
5254bff34e3Sthurlow
5264bff34e3Sthurlow if (error > 0)
5274bff34e3Sthurlow goto errout;
5284bff34e3Sthurlow
5294bff34e3Sthurlow if (error == -1) {
5304bff34e3Sthurlow /* change mount to read-only to prevent write-down */
5314bff34e3Sthurlow vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
5324bff34e3Sthurlow }
5334bff34e3Sthurlow }
534*8329232eSGordon Ross #endif /* _KERNEL */
5354bff34e3Sthurlow
53602d09e03SGordon Ross /* Prevent unload. */
53702d09e03SGordon Ross atomic_inc_32(&smbfs_mountcount);
5384bff34e3Sthurlow
5394bff34e3Sthurlow /*
5404bff34e3Sthurlow * Create a mount record and link it to the vfs struct.
54102d09e03SGordon Ross * No more possiblities for errors from here on.
54202d09e03SGordon Ross * Tear-down of this stuff is in smbfs_free_smi()
54302d09e03SGordon Ross *
5444bff34e3Sthurlow * Compare with NFS: nfsrootvp()
5454bff34e3Sthurlow */
54602d09e03SGordon Ross smi = kmem_zalloc(sizeof (*smi), KM_SLEEP);
54702d09e03SGordon Ross
54802d09e03SGordon Ross mutex_init(&smi->smi_lock, NULL, MUTEX_DEFAULT, NULL);
54902d09e03SGordon Ross cv_init(&smi->smi_statvfs_cv, NULL, CV_DEFAULT, NULL);
5504bff34e3Sthurlow
55102d09e03SGordon Ross rw_init(&smi->smi_hash_lk, NULL, RW_DEFAULT, NULL);
55202d09e03SGordon Ross smbfs_init_hash_avl(&smi->smi_hash_avl);
55302d09e03SGordon Ross
55402d09e03SGordon Ross smi->smi_share = ssp;
55502d09e03SGordon Ross ssp = NULL;
556a19609f8Sjv
557*8329232eSGordon Ross #ifdef _KERNEL
558a19609f8Sjv /*
559a19609f8Sjv * Convert the anonymous zone hold acquired via zone_hold() above
560a19609f8Sjv * into a zone reference.
561a19609f8Sjv */
562a19609f8Sjv zone_init_ref(&smi->smi_zone_ref);
563a19609f8Sjv zone_hold_ref(mntzone, &smi->smi_zone_ref, ZONE_REF_SMBFS);
564a19609f8Sjv zone_rele(mntzone);
56502d09e03SGordon Ross mntzone = NULL;
566*8329232eSGordon Ross #else /* _KERNEL */
567*8329232eSGordon Ross smi->smi_zone_ref.zref_zone = curzone;
568*8329232eSGordon Ross #endif /* _KERNEL */
56902d09e03SGordon Ross
57002d09e03SGordon Ross /*
57102d09e03SGordon Ross * Initialize option defaults
57202d09e03SGordon Ross */
57302d09e03SGordon Ross smi->smi_acregmin = SEC2HR(SMBFS_ACREGMIN);
57402d09e03SGordon Ross smi->smi_acregmax = SEC2HR(SMBFS_ACREGMAX);
57502d09e03SGordon Ross smi->smi_acdirmin = SEC2HR(SMBFS_ACDIRMIN);
57602d09e03SGordon Ross smi->smi_acdirmax = SEC2HR(SMBFS_ACDIRMAX);
577*8329232eSGordon Ross smi->smi_flags = SMI_LLOCK;
578*8329232eSGordon Ross #ifndef _KERNEL
579*8329232eSGordon Ross /* Always direct IO with fakekernel */
580*8329232eSGordon Ross smi->smi_flags |= SMI_DIRECTIO;
581*8329232eSGordon Ross #endif /* _KERNEL */
58291d632c8Sgwr
58391d632c8Sgwr /*
58402d09e03SGordon Ross * All "generic" mount options have already been
58502d09e03SGordon Ross * handled in vfs.c:domount() - see mntopts stuff.
58602d09e03SGordon Ross * Query generic options using vfs_optionisset().
5879005860cSGordon Ross * Give ACL an adjustable system-wide default.
58891d632c8Sgwr */
5899005860cSGordon Ross if (smbfs_default_opt_acl ||
5909005860cSGordon Ross vfs_optionisset(vfsp, MNTOPT_ACL, NULL))
5919005860cSGordon Ross smi->smi_flags |= SMI_ACL;
5929005860cSGordon Ross if (vfs_optionisset(vfsp, MNTOPT_NOACL, NULL))
5939005860cSGordon Ross smi->smi_flags &= ~SMI_ACL;
59491d632c8Sgwr if (vfs_optionisset(vfsp, MNTOPT_INTR, NULL))
59591d632c8Sgwr smi->smi_flags |= SMI_INT;
5964bff34e3Sthurlow
5974bff34e3Sthurlow /*
59802d09e03SGordon Ross * Get the mount options that come in as smbfs_args,
59902d09e03SGordon Ross * starting with args.flags (SMBFS_MF_xxx)
60002d09e03SGordon Ross */
60102d09e03SGordon Ross flags = STRUCT_FGET(args, flags);
60202d09e03SGordon Ross smi->smi_fmode = STRUCT_FGET(args, file_mode) & 0777;
60302d09e03SGordon Ross smi->smi_dmode = STRUCT_FGET(args, dir_mode) & 0777;
604*8329232eSGordon Ross #ifdef _KERNEL
605*8329232eSGordon Ross smi->smi_uid = STRUCT_FGET(args, uid);
606*8329232eSGordon Ross smi->smi_gid = STRUCT_FGET(args, gid);
607*8329232eSGordon Ross #else /* _KERNEL */
608*8329232eSGordon Ross /*
609*8329232eSGordon Ross * Need uid/gid to match our fake cred we'll fail in
610*8329232eSGordon Ross * smbfs_access_rwx later.
611*8329232eSGordon Ross */
612*8329232eSGordon Ross smi->smi_uid = crgetuid(cr);
613*8329232eSGordon Ross smi->smi_gid = crgetgid(cr);
614*8329232eSGordon Ross
615*8329232eSGordon Ross /*
616*8329232eSGordon Ross * Our user-level do_mount() passes the mount options sting
617*8329232eSGordon Ross * as-is, where the real mount program would convert some
618*8329232eSGordon Ross * of those options to bits set in smbfs_args.flags.
619*8329232eSGordon Ross * To avoid replicating all that conversion code, this
620*8329232eSGordon Ross * uses the generic vfs option support to handle those
621*8329232eSGordon Ross * option flag bits we need, i.e.: "noac"
622*8329232eSGordon Ross */
623*8329232eSGordon Ross if (vfs_optionisset(vfsp, MNTOPT_NOAC, NULL))
624*8329232eSGordon Ross flags |= SMBFS_MF_NOAC;
625*8329232eSGordon Ross #endif /* _KERNEL */
62602d09e03SGordon Ross
62702d09e03SGordon Ross /*
62802d09e03SGordon Ross * Hande the SMBFS_MF_xxx flags.
6294bff34e3Sthurlow */
63002d09e03SGordon Ross if (flags & SMBFS_MF_NOAC)
63102d09e03SGordon Ross smi->smi_flags |= SMI_NOAC;
63202d09e03SGordon Ross if (flags & SMBFS_MF_ACREGMIN) {
63302d09e03SGordon Ross sec = STRUCT_FGET(args, acregmin);
63402d09e03SGordon Ross if (sec < 0 || sec > SMBFS_ACMINMAX)
63502d09e03SGordon Ross sec = SMBFS_ACMINMAX;
63602d09e03SGordon Ross smi->smi_acregmin = SEC2HR(sec);
63702d09e03SGordon Ross }
63802d09e03SGordon Ross if (flags & SMBFS_MF_ACREGMAX) {
63902d09e03SGordon Ross sec = STRUCT_FGET(args, acregmax);
64002d09e03SGordon Ross if (sec < 0 || sec > SMBFS_ACMAXMAX)
64102d09e03SGordon Ross sec = SMBFS_ACMAXMAX;
64202d09e03SGordon Ross smi->smi_acregmax = SEC2HR(sec);
64302d09e03SGordon Ross }
64402d09e03SGordon Ross if (flags & SMBFS_MF_ACDIRMIN) {
64502d09e03SGordon Ross sec = STRUCT_FGET(args, acdirmin);
64602d09e03SGordon Ross if (sec < 0 || sec > SMBFS_ACMINMAX)
64702d09e03SGordon Ross sec = SMBFS_ACMINMAX;
64802d09e03SGordon Ross smi->smi_acdirmin = SEC2HR(sec);
64902d09e03SGordon Ross }
65002d09e03SGordon Ross if (flags & SMBFS_MF_ACDIRMAX) {
65102d09e03SGordon Ross sec = STRUCT_FGET(args, acdirmax);
65202d09e03SGordon Ross if (sec < 0 || sec > SMBFS_ACMAXMAX)
65302d09e03SGordon Ross sec = SMBFS_ACMAXMAX;
65402d09e03SGordon Ross smi->smi_acdirmax = SEC2HR(sec);
65502d09e03SGordon Ross }
6564bff34e3Sthurlow
65791d632c8Sgwr /*
65891d632c8Sgwr * Get attributes of the remote file system,
65991d632c8Sgwr * i.e. ACL support, named streams, etc.
66091d632c8Sgwr */
66102d09e03SGordon Ross smb_credinit(&scred, cr);
66202d09e03SGordon Ross error = smbfs_smb_qfsattr(smi->smi_share, &smi->smi_fsa, &scred);
66302d09e03SGordon Ross smb_credrele(&scred);
6644bff34e3Sthurlow if (error) {
6654bff34e3Sthurlow SMBVDEBUG("smbfs_smb_qfsattr error %d\n", error);
6664bff34e3Sthurlow }
6674bff34e3Sthurlow
66891d632c8Sgwr /*
66991d632c8Sgwr * We enable XATTR by default (via smbfs_mntopts)
67091d632c8Sgwr * but if the share does not support named streams,
67191d632c8Sgwr * force the NOXATTR option (also clears XATTR).
67291d632c8Sgwr * Caller will set or clear VFS_XATTR after this.
67391d632c8Sgwr */
67491d632c8Sgwr if ((smi->smi_fsattr & FILE_NAMED_STREAMS) == 0)
67591d632c8Sgwr vfs_setmntopt(vfsp, MNTOPT_NOXATTR, NULL, 0);
6764bff34e3Sthurlow
677bd7c6f51SGordon Ross /*
678bd7c6f51SGordon Ross * Ditto ACLs (disable if not supported on this share)
679bd7c6f51SGordon Ross */
680bd7c6f51SGordon Ross if ((smi->smi_fsattr & FILE_PERSISTENT_ACLS) == 0) {
681bd7c6f51SGordon Ross vfs_setmntopt(vfsp, MNTOPT_NOACL, NULL, 0);
682bd7c6f51SGordon Ross smi->smi_flags &= ~SMI_ACL;
683bd7c6f51SGordon Ross }
684bd7c6f51SGordon Ross
6854bff34e3Sthurlow /*
6864bff34e3Sthurlow * Assign a unique device id to the mount
6874bff34e3Sthurlow */
6884bff34e3Sthurlow mutex_enter(&smbfs_minor_lock);
6894bff34e3Sthurlow do {
6904bff34e3Sthurlow smbfs_minor = (smbfs_minor + 1) & MAXMIN32;
6914bff34e3Sthurlow smbfs_dev = makedevice(smbfs_major, smbfs_minor);
6924bff34e3Sthurlow } while (vfs_devismounted(smbfs_dev));
6934bff34e3Sthurlow mutex_exit(&smbfs_minor_lock);
6944bff34e3Sthurlow
6954bff34e3Sthurlow vfsp->vfs_dev = smbfs_dev;
696*8329232eSGordon Ross vfs_make_fsid(&vfsp->vfs_fsid, smbfs_dev, smbfs_fstyp);
6974bff34e3Sthurlow vfsp->vfs_data = (caddr_t)smi;
698*8329232eSGordon Ross vfsp->vfs_fstype = smbfs_fstyp;
6994bff34e3Sthurlow vfsp->vfs_bsize = MAXBSIZE;
7004bff34e3Sthurlow vfsp->vfs_bcount = 0;
7014bff34e3Sthurlow
7024bff34e3Sthurlow smi->smi_vfsp = vfsp;
70302d09e03SGordon Ross smbfs_zonelist_add(smi); /* undo in smbfs_freevfs */
7044bff34e3Sthurlow
70528162916SGordon Ross /* PSARC 2007/227 VFS Feature Registration */
70628162916SGordon Ross vfs_set_feature(vfsp, VFSFT_XVATTR);
70728162916SGordon Ross vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
70828162916SGordon Ross
7094bff34e3Sthurlow /*
7104bff34e3Sthurlow * Create the root vnode, which we need in unmount
71102d09e03SGordon Ross * for the call to smbfs_check_table(), etc.
71202d09e03SGordon Ross * Release this hold in smbfs_unmount.
7134bff34e3Sthurlow */
71402d09e03SGordon Ross rtnp = smbfs_node_findcreate(smi, "\\", 1, NULL, 0, 0,
71502d09e03SGordon Ross &smbfs_fattr0);
71602d09e03SGordon Ross ASSERT(rtnp != NULL);
71702d09e03SGordon Ross rtnp->r_vnode->v_type = VDIR;
71802d09e03SGordon Ross rtnp->r_vnode->v_flag |= VROOT;
71902d09e03SGordon Ross smi->smi_root = rtnp;
7204bff34e3Sthurlow
7214e72ade1SGordon Ross /*
7224e72ade1SGordon Ross * Create a taskq for async work (i.e. putpage)
7234e72ade1SGordon Ross */
7244e72ade1SGordon Ross smi->smi_taskq = taskq_create_proc("smbfs",
7254e72ade1SGordon Ross smbfs_tq_nthread, minclsyspri,
7264e72ade1SGordon Ross smbfs_tq_nthread, smbfs_tq_nthread * 2,
7274e72ade1SGordon Ross zone->zone_zsched, TASKQ_PREPOPULATE);
7284e72ade1SGordon Ross
7294bff34e3Sthurlow /*
7304bff34e3Sthurlow * NFS does other stuff here too:
7314bff34e3Sthurlow * async worker threads
7324bff34e3Sthurlow * init kstats
7334bff34e3Sthurlow *
7344bff34e3Sthurlow * End of code from NFS nfsrootvp()
7354bff34e3Sthurlow */
7364bff34e3Sthurlow return (0);
7374bff34e3Sthurlow
738*8329232eSGordon Ross #ifdef _KERNEL
7394bff34e3Sthurlow errout:
7404bff34e3Sthurlow vfsp->vfs_data = NULL;
74102d09e03SGordon Ross if (smi != NULL)
7424bff34e3Sthurlow smbfs_free_smi(smi);
7434bff34e3Sthurlow
7444bff34e3Sthurlow if (mntzone != NULL)
7454bff34e3Sthurlow zone_rele(mntzone);
7464bff34e3Sthurlow
74702d09e03SGordon Ross if (ssp != NULL)
7484bff34e3Sthurlow smb_share_rele(ssp);
7494bff34e3Sthurlow
7504bff34e3Sthurlow return (error);
751*8329232eSGordon Ross #endif /* _KERNEL */
7524bff34e3Sthurlow }
7534bff34e3Sthurlow
7544bff34e3Sthurlow /*
7554bff34e3Sthurlow * vfs operations
7564bff34e3Sthurlow */
7574bff34e3Sthurlow static int
smbfs_unmount(vfs_t * vfsp,int flag,cred_t * cr)7584bff34e3Sthurlow smbfs_unmount(vfs_t *vfsp, int flag, cred_t *cr)
7594bff34e3Sthurlow {
7604bff34e3Sthurlow smbmntinfo_t *smi;
7614bff34e3Sthurlow smbnode_t *rtnp;
7624bff34e3Sthurlow
7634bff34e3Sthurlow smi = VFTOSMI(vfsp);
7644bff34e3Sthurlow
765*8329232eSGordon Ross #ifdef _KERNEL
7664bff34e3Sthurlow if (secpolicy_fs_unmount(cr, vfsp) != 0)
7674bff34e3Sthurlow return (EPERM);
768*8329232eSGordon Ross #endif /* _KERNEL */
7694bff34e3Sthurlow
7704bff34e3Sthurlow if ((flag & MS_FORCE) == 0) {
7714bff34e3Sthurlow smbfs_rflush(vfsp, cr);
7724bff34e3Sthurlow
7734bff34e3Sthurlow /*
7744bff34e3Sthurlow * If there are any active vnodes on this file system,
7754bff34e3Sthurlow * (other than the root vnode) then the file system is
7764bff34e3Sthurlow * busy and can't be umounted.
7774bff34e3Sthurlow */
77802d09e03SGordon Ross if (smbfs_check_table(vfsp, smi->smi_root))
7794bff34e3Sthurlow return (EBUSY);
7804bff34e3Sthurlow
7814bff34e3Sthurlow /*
7824bff34e3Sthurlow * We normally hold a ref to the root vnode, so
7834bff34e3Sthurlow * check for references beyond the one we expect:
7844bff34e3Sthurlow * smbmntinfo_t -> smi_root
7854bff34e3Sthurlow * Note that NFS does not hold the root vnode.
7864bff34e3Sthurlow */
7874bff34e3Sthurlow if (smi->smi_root &&
7884bff34e3Sthurlow smi->smi_root->r_vnode->v_count > 1)
7894bff34e3Sthurlow return (EBUSY);
7904bff34e3Sthurlow }
7914bff34e3Sthurlow
7924bff34e3Sthurlow /*
7934bff34e3Sthurlow * common code for both forced and non-forced
7944bff34e3Sthurlow *
7954bff34e3Sthurlow * Setting VFS_UNMOUNTED prevents new operations.
7964bff34e3Sthurlow * Operations already underway may continue,
7974bff34e3Sthurlow * but not for long.
7984bff34e3Sthurlow */
7994bff34e3Sthurlow vfsp->vfs_flag |= VFS_UNMOUNTED;
8004bff34e3Sthurlow
8014bff34e3Sthurlow /*
8024bff34e3Sthurlow * If we hold the root VP (and we normally do)
8034bff34e3Sthurlow * then it's safe to release it now.
8044bff34e3Sthurlow */
8054bff34e3Sthurlow if (smi->smi_root) {
8064bff34e3Sthurlow rtnp = smi->smi_root;
8074bff34e3Sthurlow smi->smi_root = NULL;
8084bff34e3Sthurlow VN_RELE(rtnp->r_vnode); /* release root vnode */
8094bff34e3Sthurlow }
8104bff34e3Sthurlow
8114bff34e3Sthurlow /*
8124bff34e3Sthurlow * Remove all nodes from the node hash tables.
81302d09e03SGordon Ross * This (indirectly) calls: smbfs_addfree, smbinactive,
8144bff34e3Sthurlow * which will try to flush dirty pages, etc. so
8154bff34e3Sthurlow * don't destroy the underlying share just yet.
8164bff34e3Sthurlow *
8174bff34e3Sthurlow * Also, with a forced unmount, some nodes may
8184bff34e3Sthurlow * remain active, and those will get cleaned up
8194bff34e3Sthurlow * after their last vn_rele.
8204bff34e3Sthurlow */
8214bff34e3Sthurlow smbfs_destroy_table(vfsp);
8224bff34e3Sthurlow
8234e72ade1SGordon Ross /*
8244e72ade1SGordon Ross * Shutdown any outstanding I/O requests on this share,
8254e72ade1SGordon Ross * and force a tree disconnect. The share object will
8264e72ade1SGordon Ross * continue to hang around until smb_share_rele().
8274e72ade1SGordon Ross * This should also cause most active nodes to be
8284e72ade1SGordon Ross * released as their operations fail with EIO.
8294e72ade1SGordon Ross */
8304e72ade1SGordon Ross smb_share_kill(smi->smi_share);
8314e72ade1SGordon Ross
8324e72ade1SGordon Ross /*
8334e72ade1SGordon Ross * Any async taskq work should be giving up.
8344e72ade1SGordon Ross * Wait for those to exit.
8354e72ade1SGordon Ross */
8364e72ade1SGordon Ross taskq_destroy(smi->smi_taskq);
8374e72ade1SGordon Ross
8384bff34e3Sthurlow /*
8394bff34e3Sthurlow * Delete our kstats...
8404bff34e3Sthurlow *
8414bff34e3Sthurlow * Doing it here, rather than waiting until
8424bff34e3Sthurlow * smbfs_freevfs so these are not visible
8434bff34e3Sthurlow * after the unmount.
8444bff34e3Sthurlow */
8454bff34e3Sthurlow if (smi->smi_io_kstats) {
8464bff34e3Sthurlow kstat_delete(smi->smi_io_kstats);
8474bff34e3Sthurlow smi->smi_io_kstats = NULL;
8484bff34e3Sthurlow }
8494bff34e3Sthurlow if (smi->smi_ro_kstats) {
8504bff34e3Sthurlow kstat_delete(smi->smi_ro_kstats);
8514bff34e3Sthurlow smi->smi_ro_kstats = NULL;
8524bff34e3Sthurlow }
8534bff34e3Sthurlow
8544bff34e3Sthurlow /*
85502d09e03SGordon Ross * The rest happens in smbfs_freevfs()
8564bff34e3Sthurlow */
8574bff34e3Sthurlow return (0);
8584bff34e3Sthurlow }
8594bff34e3Sthurlow
8604bff34e3Sthurlow
8614bff34e3Sthurlow /*
8624bff34e3Sthurlow * find root of smbfs
8634bff34e3Sthurlow */
8644bff34e3Sthurlow static int
smbfs_root(vfs_t * vfsp,vnode_t ** vpp)8654bff34e3Sthurlow smbfs_root(vfs_t *vfsp, vnode_t **vpp)
8664bff34e3Sthurlow {
8674bff34e3Sthurlow smbmntinfo_t *smi;
8684bff34e3Sthurlow vnode_t *vp;
8694bff34e3Sthurlow
8704bff34e3Sthurlow smi = VFTOSMI(vfsp);
8714bff34e3Sthurlow
872a19609f8Sjv if (curproc->p_zone != smi->smi_zone_ref.zref_zone)
8734bff34e3Sthurlow return (EPERM);
8744bff34e3Sthurlow
8754bff34e3Sthurlow if (smi->smi_flags & SMI_DEAD || vfsp->vfs_flag & VFS_UNMOUNTED)
8764bff34e3Sthurlow return (EIO);
8774bff34e3Sthurlow
8784bff34e3Sthurlow /*
8794bff34e3Sthurlow * The root vp is created in mount and held
8804bff34e3Sthurlow * until unmount, so this is paranoia.
8814bff34e3Sthurlow */
8824bff34e3Sthurlow if (smi->smi_root == NULL)
8834bff34e3Sthurlow return (EIO);
8844bff34e3Sthurlow
8854bff34e3Sthurlow /* Just take a reference and return it. */
8864bff34e3Sthurlow vp = SMBTOV(smi->smi_root);
8874bff34e3Sthurlow VN_HOLD(vp);
8884bff34e3Sthurlow *vpp = vp;
8894bff34e3Sthurlow
8904bff34e3Sthurlow return (0);
8914bff34e3Sthurlow }
8924bff34e3Sthurlow
8934bff34e3Sthurlow /*
8944bff34e3Sthurlow * Get file system statistics.
8954bff34e3Sthurlow */
8964bff34e3Sthurlow static int
smbfs_statvfs(vfs_t * vfsp,statvfs64_t * sbp)8974bff34e3Sthurlow smbfs_statvfs(vfs_t *vfsp, statvfs64_t *sbp)
8984bff34e3Sthurlow {
8994bff34e3Sthurlow int error;
9004bff34e3Sthurlow smbmntinfo_t *smi = VFTOSMI(vfsp);
9014bff34e3Sthurlow smb_share_t *ssp = smi->smi_share;
9024bff34e3Sthurlow statvfs64_t stvfs;
9034bff34e3Sthurlow hrtime_t now;
9044bff34e3Sthurlow smb_cred_t scred;
9054bff34e3Sthurlow
906a19609f8Sjv if (curproc->p_zone != smi->smi_zone_ref.zref_zone)
9074bff34e3Sthurlow return (EPERM);
9084bff34e3Sthurlow
9094bff34e3Sthurlow if (smi->smi_flags & SMI_DEAD || vfsp->vfs_flag & VFS_UNMOUNTED)
9104bff34e3Sthurlow return (EIO);
9114bff34e3Sthurlow
9124bff34e3Sthurlow mutex_enter(&smi->smi_lock);
9134bff34e3Sthurlow
9144bff34e3Sthurlow /*
9154bff34e3Sthurlow * Use cached result if still valid.
9164bff34e3Sthurlow */
9174bff34e3Sthurlow recheck:
9184bff34e3Sthurlow now = gethrtime();
9194bff34e3Sthurlow if (now < smi->smi_statfstime) {
920613a2f6bSGordon Ross error = 0;
9214bff34e3Sthurlow goto cache_hit;
9224bff34e3Sthurlow }
9234bff34e3Sthurlow
9244bff34e3Sthurlow /*
9254bff34e3Sthurlow * FS attributes are stale, so someone
9264bff34e3Sthurlow * needs to do an OTW call to get them.
9274bff34e3Sthurlow * Serialize here so only one thread
9284bff34e3Sthurlow * does the OTW call.
9294bff34e3Sthurlow */
9304bff34e3Sthurlow if (smi->smi_status & SM_STATUS_STATFS_BUSY) {
9314bff34e3Sthurlow smi->smi_status |= SM_STATUS_STATFS_WANT;
9324bff34e3Sthurlow if (!cv_wait_sig(&smi->smi_statvfs_cv, &smi->smi_lock)) {
9334bff34e3Sthurlow mutex_exit(&smi->smi_lock);
9344bff34e3Sthurlow return (EINTR);
9354bff34e3Sthurlow }
9364bff34e3Sthurlow /* Hope status is valid now. */
9374bff34e3Sthurlow goto recheck;
9384bff34e3Sthurlow }
9394bff34e3Sthurlow smi->smi_status |= SM_STATUS_STATFS_BUSY;
9404bff34e3Sthurlow mutex_exit(&smi->smi_lock);
9414bff34e3Sthurlow
9424bff34e3Sthurlow /*
9434bff34e3Sthurlow * Do the OTW call. Note: lock NOT held.
9444bff34e3Sthurlow */
945613a2f6bSGordon Ross smb_credinit(&scred, NULL);
9464bff34e3Sthurlow bzero(&stvfs, sizeof (stvfs));
9474bff34e3Sthurlow error = smbfs_smb_statfs(ssp, &stvfs, &scred);
9484bff34e3Sthurlow smb_credrele(&scred);
949613a2f6bSGordon Ross if (error) {
950613a2f6bSGordon Ross SMBVDEBUG("statfs error=%d\n", error);
951613a2f6bSGordon Ross } else {
952613a2f6bSGordon Ross
953613a2f6bSGordon Ross /*
954613a2f6bSGordon Ross * Set a few things the OTW call didn't get.
955613a2f6bSGordon Ross */
956613a2f6bSGordon Ross stvfs.f_frsize = stvfs.f_bsize;
957613a2f6bSGordon Ross stvfs.f_favail = stvfs.f_ffree;
958613a2f6bSGordon Ross stvfs.f_fsid = (unsigned long)vfsp->vfs_fsid.val[0];
959613a2f6bSGordon Ross bcopy(fs_type_name, stvfs.f_basetype, FSTYPSZ);
960613a2f6bSGordon Ross stvfs.f_flag = vf_to_stf(vfsp->vfs_flag);
961613a2f6bSGordon Ross stvfs.f_namemax = smi->smi_fsa.fsa_maxname;
962613a2f6bSGordon Ross
963613a2f6bSGordon Ross /*
964613a2f6bSGordon Ross * Save the result, update lifetime
965613a2f6bSGordon Ross */
966613a2f6bSGordon Ross now = gethrtime();
967613a2f6bSGordon Ross smi->smi_statfstime = now +
968613a2f6bSGordon Ross (SM_MAX_STATFSTIME * (hrtime_t)NANOSEC);
969613a2f6bSGordon Ross smi->smi_statvfsbuf = stvfs; /* struct assign! */
970613a2f6bSGordon Ross }
9714bff34e3Sthurlow
9724bff34e3Sthurlow mutex_enter(&smi->smi_lock);
9734bff34e3Sthurlow if (smi->smi_status & SM_STATUS_STATFS_WANT)
9744bff34e3Sthurlow cv_broadcast(&smi->smi_statvfs_cv);
9754bff34e3Sthurlow smi->smi_status &= ~(SM_STATUS_STATFS_BUSY | SM_STATUS_STATFS_WANT);
9764bff34e3Sthurlow
9774bff34e3Sthurlow /*
9784bff34e3Sthurlow * Copy the statvfs data to caller's buf.
9794bff34e3Sthurlow * Note: struct assignment
9804bff34e3Sthurlow */
9814bff34e3Sthurlow cache_hit:
982613a2f6bSGordon Ross if (error == 0)
983613a2f6bSGordon Ross *sbp = smi->smi_statvfsbuf;
9844bff34e3Sthurlow mutex_exit(&smi->smi_lock);
9854bff34e3Sthurlow return (error);
9864bff34e3Sthurlow }
9874bff34e3Sthurlow
9884bff34e3Sthurlow /*
9894bff34e3Sthurlow * Flush dirty smbfs files for file system vfsp.
9904bff34e3Sthurlow * If vfsp == NULL, all smbfs files are flushed.
9914bff34e3Sthurlow */
9924bff34e3Sthurlow /*ARGSUSED*/
9934bff34e3Sthurlow static int
smbfs_sync(vfs_t * vfsp,short flag,cred_t * cr)9944bff34e3Sthurlow smbfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
9954bff34e3Sthurlow {
9965f4fc069Sjilinxpd
9974bff34e3Sthurlow /*
9985f4fc069Sjilinxpd * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
9995f4fc069Sjilinxpd * to sync metadata, which they would otherwise cache indefinitely.
10005f4fc069Sjilinxpd * Semantically, the only requirement is that the sync be initiated.
10015f4fc069Sjilinxpd * Assume the server-side takes care of attribute sync.
10024bff34e3Sthurlow */
10035f4fc069Sjilinxpd if (flag & SYNC_ATTR)
10045f4fc069Sjilinxpd return (0);
10055f4fc069Sjilinxpd
10065f4fc069Sjilinxpd if (vfsp == NULL) {
10075f4fc069Sjilinxpd /*
10085f4fc069Sjilinxpd * Flush ALL smbfs mounts in this zone.
10095f4fc069Sjilinxpd */
10105f4fc069Sjilinxpd smbfs_flushall(cr);
10115f4fc069Sjilinxpd return (0);
10124bff34e3Sthurlow }
101302d09e03SGordon Ross
10145f4fc069Sjilinxpd smbfs_rflush(vfsp, cr);
10155f4fc069Sjilinxpd
10164bff34e3Sthurlow return (0);
10174bff34e3Sthurlow }
10184bff34e3Sthurlow
10194bff34e3Sthurlow /*
10204bff34e3Sthurlow * Initialization routine for VFS routines. Should only be called once
10214bff34e3Sthurlow */
10224bff34e3Sthurlow int
smbfs_vfsinit(void)10234bff34e3Sthurlow smbfs_vfsinit(void)
10244bff34e3Sthurlow {
10254bff34e3Sthurlow return (0);
10264bff34e3Sthurlow }
10274bff34e3Sthurlow
10284bff34e3Sthurlow /*
10294bff34e3Sthurlow * Shutdown routine for VFS routines. Should only be called once
10304bff34e3Sthurlow */
10314bff34e3Sthurlow void
smbfs_vfsfini(void)10324bff34e3Sthurlow smbfs_vfsfini(void)
10334bff34e3Sthurlow {
10344bff34e3Sthurlow }
10354bff34e3Sthurlow
10364bff34e3Sthurlow void
smbfs_freevfs(vfs_t * vfsp)10374bff34e3Sthurlow smbfs_freevfs(vfs_t *vfsp)
10384bff34e3Sthurlow {
10394bff34e3Sthurlow smbmntinfo_t *smi;
10404bff34e3Sthurlow
10414bff34e3Sthurlow /* free up the resources */
10424bff34e3Sthurlow smi = VFTOSMI(vfsp);
10434bff34e3Sthurlow
10444bff34e3Sthurlow /*
10454bff34e3Sthurlow * By this time we should have already deleted the
10464bff34e3Sthurlow * smi kstats in the unmount code. If they are still around
10474bff34e3Sthurlow * something is wrong
10484bff34e3Sthurlow */
10494bff34e3Sthurlow ASSERT(smi->smi_io_kstats == NULL);
10504bff34e3Sthurlow
105102d09e03SGordon Ross smbfs_zonelist_remove(smi);
10524bff34e3Sthurlow
10534bff34e3Sthurlow smbfs_free_smi(smi);
10544bff34e3Sthurlow
10554bff34e3Sthurlow /*
10564bff34e3Sthurlow * Allow _fini() to succeed now, if so desired.
10574bff34e3Sthurlow */
10584bff34e3Sthurlow atomic_dec_32(&smbfs_mountcount);
10594bff34e3Sthurlow }
10604bff34e3Sthurlow
1061*8329232eSGordon Ross #ifdef _KERNEL
10624bff34e3Sthurlow /*
10634bff34e3Sthurlow * smbfs_mount_label_policy:
10644bff34e3Sthurlow * Determine whether the mount is allowed according to MAC check,
10654bff34e3Sthurlow * by comparing (where appropriate) label of the remote server
10664bff34e3Sthurlow * against the label of the zone being mounted into.
10674bff34e3Sthurlow *
10684bff34e3Sthurlow * Returns:
10694bff34e3Sthurlow * 0 : access allowed
10704bff34e3Sthurlow * -1 : read-only access allowed (i.e., read-down)
10714bff34e3Sthurlow * >0 : error code, such as EACCES
10724bff34e3Sthurlow *
10734bff34e3Sthurlow * NB:
10744bff34e3Sthurlow * NFS supports Cipso labels by parsing the vfs_resource
10754bff34e3Sthurlow * to see what the Solaris server global zone has shared.
10764bff34e3Sthurlow * We can't support that for CIFS since resource names
10774bff34e3Sthurlow * contain share names, not paths.
10784bff34e3Sthurlow */
10794bff34e3Sthurlow static int
smbfs_mount_label_policy(vfs_t * vfsp,void * ipaddr,int addr_type,cred_t * cr)10804bff34e3Sthurlow smbfs_mount_label_policy(vfs_t *vfsp, void *ipaddr, int addr_type, cred_t *cr)
10814bff34e3Sthurlow {
10824bff34e3Sthurlow bslabel_t *server_sl, *mntlabel;
10834bff34e3Sthurlow zone_t *mntzone = NULL;
10844bff34e3Sthurlow ts_label_t *zlabel;
10854bff34e3Sthurlow tsol_tpc_t *tp;
10864bff34e3Sthurlow ts_label_t *tsl = NULL;
10874bff34e3Sthurlow int retv;
10884bff34e3Sthurlow
10894bff34e3Sthurlow /*
10904bff34e3Sthurlow * Get the zone's label. Each zone on a labeled system has a label.
10914bff34e3Sthurlow */
10924bff34e3Sthurlow mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
10934bff34e3Sthurlow zlabel = mntzone->zone_slabel;
10944bff34e3Sthurlow ASSERT(zlabel != NULL);
10954bff34e3Sthurlow label_hold(zlabel);
10964bff34e3Sthurlow
10974bff34e3Sthurlow retv = EACCES; /* assume the worst */
10984bff34e3Sthurlow
10994bff34e3Sthurlow /*
11004bff34e3Sthurlow * Next, get the assigned label of the remote server.
11014bff34e3Sthurlow */
11024bff34e3Sthurlow tp = find_tpc(ipaddr, addr_type, B_FALSE);
11034bff34e3Sthurlow if (tp == NULL)
11044bff34e3Sthurlow goto out; /* error getting host entry */
11054bff34e3Sthurlow
11064bff34e3Sthurlow if (tp->tpc_tp.tp_doi != zlabel->tsl_doi)
11074bff34e3Sthurlow goto rel_tpc; /* invalid domain */
11084bff34e3Sthurlow if ((tp->tpc_tp.host_type != UNLABELED))
11094bff34e3Sthurlow goto rel_tpc; /* invalid hosttype */
11104bff34e3Sthurlow
11114bff34e3Sthurlow server_sl = &tp->tpc_tp.tp_def_label;
11124bff34e3Sthurlow mntlabel = label2bslabel(zlabel);
11134bff34e3Sthurlow
11144bff34e3Sthurlow /*
11154bff34e3Sthurlow * Now compare labels to complete the MAC check. If the labels
11164bff34e3Sthurlow * are equal or if the requestor is in the global zone and has
11174bff34e3Sthurlow * NET_MAC_AWARE, then allow read-write access. (Except for
11184bff34e3Sthurlow * mounts into the global zone itself; restrict these to
11194bff34e3Sthurlow * read-only.)
11204bff34e3Sthurlow *
112148bbca81SDaniel Hoffman * If the requestor is in some other zone, but their label
11224bff34e3Sthurlow * dominates the server, then allow read-down.
11234bff34e3Sthurlow *
11244bff34e3Sthurlow * Otherwise, access is denied.
11254bff34e3Sthurlow */
11264bff34e3Sthurlow if (blequal(mntlabel, server_sl) ||
11274bff34e3Sthurlow (crgetzoneid(cr) == GLOBAL_ZONEID &&
11284bff34e3Sthurlow getpflags(NET_MAC_AWARE, cr) != 0)) {
11294bff34e3Sthurlow if ((mntzone == global_zone) ||
11304bff34e3Sthurlow !blequal(mntlabel, server_sl))
11314bff34e3Sthurlow retv = -1; /* read-only */
11324bff34e3Sthurlow else
11334bff34e3Sthurlow retv = 0; /* access OK */
11344bff34e3Sthurlow } else if (bldominates(mntlabel, server_sl)) {
11354bff34e3Sthurlow retv = -1; /* read-only */
11364bff34e3Sthurlow } else {
11374bff34e3Sthurlow retv = EACCES;
11384bff34e3Sthurlow }
11394bff34e3Sthurlow
11404bff34e3Sthurlow if (tsl != NULL)
11414bff34e3Sthurlow label_rele(tsl);
11424bff34e3Sthurlow
11434bff34e3Sthurlow rel_tpc:
11444bff34e3Sthurlow /*LINTED*/
11454bff34e3Sthurlow TPC_RELE(tp);
11464bff34e3Sthurlow out:
11474bff34e3Sthurlow if (mntzone)
11484bff34e3Sthurlow zone_rele(mntzone);
11494bff34e3Sthurlow label_rele(zlabel);
11504bff34e3Sthurlow return (retv);
11514bff34e3Sthurlow }
1152*8329232eSGordon Ross #endif /* _KERNEL */
1153