1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
  24  */
  25 
  26 #include <sys/param.h>
  27 #include <sys/errno.h>
  28 #include <sys/proc.h>
  29 #include <sys/disp.h>
  30 #include <sys/vfs.h>
  31 #include <sys/vfs_opreg.h>
  32 #include <sys/vnode.h>
  33 #include <sys/uio.h>
  34 #include <sys/kmem.h>
  35 #include <sys/cred.h>
  36 #include <sys/statvfs.h>
  37 #include <sys/mount.h>
  38 #include <sys/tiuser.h>
  39 #include <sys/cmn_err.h>
  40 #include <sys/debug.h>
  41 #include <sys/systm.h>
  42 #include <sys/sysmacros.h>
  43 #include <sys/pathname.h>
  44 #include <rpc/types.h>
  45 #include <rpc/auth.h>
  46 #include <rpc/clnt.h>
  47 #include <fs/fs_subr.h>
  48 #include <sys/fs/autofs.h>
  49 #include <sys/modctl.h>
  50 #include <sys/mntent.h>
  51 #include <sys/policy.h>
  52 #include <sys/zone.h>
  53 
  54 static int autofs_init(int, char *);
  55 
  56 static major_t autofs_major;
  57 static minor_t autofs_minor;
  58 
  59 kmutex_t autofs_minor_lock;
  60 zone_key_t autofs_key;
  61 
  62 static mntopts_t auto_mntopts;
  63 
  64 /*
  65  * The AUTOFS system call.
  66  */
  67 static struct sysent autofssysent = {
  68         2,
  69         SE_32RVAL1 | SE_ARGC | SE_NOUNLOAD,
  70         autofssys
  71 };
  72 
  73 static struct modlsys modlsys = {
  74         &mod_syscallops,
  75         "AUTOFS syscall",
  76         &autofssysent
  77 };
  78 
  79 #ifdef  _SYSCALL32_IMPL
  80 static struct modlsys  modlsys32 = {
  81         &mod_syscallops32,
  82         "AUTOFS syscall (32-bit)",
  83         &autofssysent
  84 };
  85 #endif  /* _SYSCALL32_IMPL */
  86 
  87 static vfsdef_t vfw = {
  88         VFSDEF_VERSION,
  89         "autofs",
  90         autofs_init,
  91         VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_STATS|VSW_ZMOUNT,
  92         &auto_mntopts
  93 };
  94 
  95 /*
  96  * Module linkage information for the kernel.
  97  */
  98 static struct modlfs modlfs = {
  99         &mod_fsops, "filesystem for autofs", &vfw
 100 };
 101 
 102 static struct modlinkage modlinkage = {
 103         MODREV_1,
 104         {   &modlfs,
 105             &modlsys,
 106 #ifdef  _SYSCALL32_IMPL
 107             &modlsys32,
 108 #endif
 109             NULL
 110         }
 111 };
 112 
 113 /*
 114  * This is the module initialization routine.
 115  */
 116 int
 117 _init(void)
 118 {
 119         return (mod_install(&modlinkage));
 120 }
 121 
 122 int
 123 _fini(void)
 124 {
 125         /*
 126          * Don't allow the autofs module to be unloaded for now.
 127          */
 128         return (EBUSY);
 129 }
 130 
 131 int
 132 _info(struct modinfo *modinfop)
 133 {
 134         return (mod_info(&modlinkage, modinfop));
 135 }
 136 
 137 static int autofs_fstype;
 138 
 139 /*
 140  * autofs VFS operations
 141  */
 142 static int auto_mount(vfs_t *, vnode_t *, struct mounta *, cred_t *);
 143 static int auto_unmount(vfs_t *, int, cred_t *);
 144 static int auto_root(vfs_t *, vnode_t **);
 145 static int auto_statvfs(vfs_t *, struct statvfs64 *);
 146 
 147 /*
 148  * Auto Mount options table
 149  */
 150 
 151 static char *direct_cancel[] = { MNTOPT_INDIRECT, NULL };
 152 static char *indirect_cancel[] = { MNTOPT_DIRECT, NULL };
 153 static char *browse_cancel[] = { MNTOPT_NOBROWSE, NULL };
 154 static char *nobrowse_cancel[] = { MNTOPT_BROWSE, NULL };
 155 
 156 static mntopt_t mntopts[] = {
 157 /*
 158  *      option name             cancel options  default arg     flags
 159  */
 160         { MNTOPT_DIRECT,        direct_cancel,  NULL,           0,
 161                 NULL },
 162         { MNTOPT_INDIRECT,      indirect_cancel, NULL,          0,
 163                 NULL },
 164         { MNTOPT_IGNORE,        NULL,           NULL,
 165                 MO_DEFAULT|MO_TAG,      NULL },
 166         { "nest",               NULL,           NULL,           MO_TAG,
 167                 NULL },
 168         { MNTOPT_BROWSE,        browse_cancel,  NULL,           MO_TAG,
 169                 NULL },
 170         { MNTOPT_NOBROWSE,      nobrowse_cancel, NULL,          MO_TAG,
 171                 NULL },
 172         { MNTOPT_RESTRICT,      NULL,           NULL,           MO_TAG,
 173                 NULL },
 174 };
 175 
 176 static mntopts_t auto_mntopts = {
 177         sizeof (mntopts) / sizeof (mntopt_t),
 178         mntopts
 179 };
 180 
 181 /*ARGSUSED*/
 182 static void
 183 autofs_zone_destructor(zoneid_t zoneid, void *arg)
 184 {
 185         struct autofs_globals *fngp = arg;
 186         vnode_t *vp;
 187 
 188         if (fngp == NULL)
 189                 return;
 190         ASSERT(fngp->fng_fnnode_count == 1);
 191         ASSERT(fngp->fng_unmount_threads == 0);
 192 
 193         if (fngp->fng_autofs_daemon_dh != NULL)
 194                 door_ki_rele(fngp->fng_autofs_daemon_dh);
 195         /*
 196          * vn_alloc() initialized the rootnode with a count of 1; we need to
 197          * make this 0 to placate auto_freefnnode().
 198          */
 199         vp = fntovn(fngp->fng_rootfnnodep);
 200         ASSERT(vp->v_count == 1);
 201         vp->v_count--;
 202         auto_freefnnode(fngp->fng_rootfnnodep);
 203         mutex_destroy(&fngp->fng_unmount_threads_lock);
 204         kmem_free(fngp, sizeof (*fngp));
 205 }
 206 
 207 /*
 208  * rootfnnodep is allocated here.  Its sole purpose is to provide
 209  * read/write locking for top level fnnodes.  This object is
 210  * persistent and will not be deallocated until the zone is destroyed.
 211  *
 212  * The current zone is implied as the zone of interest, since we will be
 213  * calling zthread_create() which must be called from the correct zone.
 214  */
 215 struct autofs_globals *
 216 autofs_zone_init(void)
 217 {
 218         char rootname[sizeof ("root_fnnode_zone_") + ZONEID_WIDTH];
 219         struct autofs_globals *fngp;
 220         zoneid_t zoneid = getzoneid();
 221 
 222         fngp = kmem_zalloc(sizeof (*fngp), KM_SLEEP);
 223         (void) snprintf(rootname, sizeof (rootname), "root_fnnode_zone_%d",
 224             zoneid);
 225         fngp->fng_rootfnnodep = auto_makefnnode(VNON, NULL, rootname, CRED(),
 226             fngp);
 227         /*
 228          * Don't need to hold fng_rootfnnodep as it's never really used for
 229          * anything.
 230          */
 231         fngp->fng_fnnode_count = 1;
 232         fngp->fng_printed_not_running_msg = 0;
 233         fngp->fng_zoneid = zoneid;
 234         mutex_init(&fngp->fng_unmount_threads_lock, NULL, MUTEX_DEFAULT,
 235             NULL);
 236         fngp->fng_unmount_threads = 0;
 237 
 238         mutex_init(&fngp->fng_autofs_daemon_lock, NULL, MUTEX_DEFAULT, NULL);
 239 
 240         /*
 241          * Start the unmounter thread for this zone.
 242          */
 243         (void) zthread_create(NULL, 0, auto_do_unmount, fngp, 0, minclsyspri);
 244         return (fngp);
 245 }
 246 
 247 int
 248 autofs_init(int fstype, char *name)
 249 {
 250         static const fs_operation_def_t auto_vfsops_template[] = {
 251                 { VFSNAME_MOUNT,        { .vfs_mount = auto_mount } },
 252                 { VFSNAME_UNMOUNT,      { .vfs_unmount = auto_unmount } },
 253                 { VFSNAME_ROOT,         { .vfs_root = auto_root } },
 254                 { VFSNAME_STATVFS,      { .vfs_statvfs = auto_statvfs } },
 255                 { NULL,                 { NULL } }
 256         };
 257         int error;
 258 
 259         autofs_fstype = fstype;
 260         ASSERT(autofs_fstype != 0);
 261         /*
 262          * Associate VFS ops vector with this fstype
 263          */
 264         error = vfs_setfsops(fstype, auto_vfsops_template, NULL);
 265         if (error != 0) {
 266                 cmn_err(CE_WARN, "autofs_init: bad vfs ops template");
 267                 return (error);
 268         }
 269 
 270         error = vn_make_ops(name, auto_vnodeops_template, &auto_vnodeops);
 271         if (error != 0) {
 272                 (void) vfs_freevfsops_by_type(fstype);
 273                 cmn_err(CE_WARN, "autofs_init: bad vnode ops template");
 274                 return (error);
 275         }
 276 
 277         mutex_init(&autofs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
 278         /*
 279          * Assign unique major number for all autofs mounts
 280          */
 281         if ((autofs_major = getudev()) == (major_t)-1) {
 282                 cmn_err(CE_WARN,
 283                     "autofs: autofs_init: can't get unique device number");
 284                 mutex_destroy(&autofs_minor_lock);
 285                 return (1);
 286         }
 287 
 288         /*
 289          * We'd like to be able to provide a constructor here, but we can't
 290          * since it wants to zthread_create(), something it can't do in a ZSD
 291          * constructor.
 292          */
 293         zone_key_create(&autofs_key, NULL, NULL, autofs_zone_destructor);
 294 
 295         return (0);
 296 }
 297 
 298 static char *restropts[] = {
 299         RESTRICTED_MNTOPTS
 300 };
 301 
 302 /*
 303  * This routine adds those options to the option string `buf' which are
 304  * forced by secpolicy_fs_mount.  If the automatic "security" options
 305  * are set, the option string gets them added if they aren't already
 306  * there.  We search the string with "strstr" and make sure that
 307  * the string we find is bracketed with <start|",">MNTOPT<","|"\0">
 308  *
 309  * This is one half of the option inheritence algorithm which
 310  * implements the "restrict" option.  The other half is implemented
 311  * in automountd; it takes its cue from the options we add here.
 312  */
 313 static int
 314 autofs_restrict_opts(struct vfs *vfsp, char *buf, size_t maxlen, size_t *curlen)
 315 {
 316         int i;
 317         char *p;
 318         size_t len = *curlen - 1;
 319 
 320         /* Unrestricted */
 321         if (!vfs_optionisset(vfsp, restropts[0], NULL))
 322                 return (0);
 323 
 324         for (i = 0; i < sizeof (restropts)/sizeof (restropts[0]); i++) {
 325                 size_t olen = strlen(restropts[i]);
 326 
 327                 /* Add "restrict" always and the others insofar set */
 328                 if ((i == 0 || vfs_optionisset(vfsp, restropts[i], NULL)) &&
 329                     ((p = strstr(buf, restropts[i])) == NULL ||
 330                     !((p == buf || p[-1] == ',') &&
 331                     (p[olen] == '\0' || p[olen] == ',')))) {
 332 
 333                         if (len + olen + 1 > maxlen)
 334                                 return (-1);
 335 
 336                         if (*buf != '\0')
 337                                 buf[len++] = ',';
 338                         (void) strcpy(&buf[len], restropts[i]);
 339                         len += olen;
 340                 }
 341         }
 342         *curlen = len + 1;
 343         return (0);
 344 }
 345 
 346 /* ARGSUSED */
 347 static int
 348 auto_mount(vfs_t *vfsp, vnode_t *vp, struct mounta *uap, cred_t *cr)
 349 {
 350         int error;
 351         size_t len = 0;
 352         autofs_args args;
 353         fninfo_t *fnip = NULL;
 354         vnode_t *rootvp = NULL;
 355         fnnode_t *rootfnp = NULL;
 356         char *data = uap->dataptr;
 357         char datalen = uap->datalen;
 358         dev_t autofs_dev;
 359         char strbuff[MAXPATHLEN + 1];
 360         vnode_t *kkvp;
 361         struct autofs_globals *fngp;
 362         zone_t *zone = curproc->p_zone;
 363 
 364         AUTOFS_DPRINT((4, "auto_mount: vfs %p vp %p\n", (void *)vfsp,
 365             (void *)vp));
 366 
 367         if ((error = secpolicy_fs_mount(cr, vp, vfsp)) != 0)
 368                 return (EPERM);
 369 
 370         if (zone == global_zone) {
 371                 zone_t *mntzone;
 372 
 373                 mntzone = zone_find_by_path(refstr_value(vfsp->vfs_mntpt));
 374                 ASSERT(mntzone != NULL);
 375                 zone_rele(mntzone);
 376                 if (mntzone != zone) {
 377                         return (EBUSY);
 378                 }
 379         }
 380 
 381         /*
 382          * Stop the mount from going any further if the zone is going away.
 383          */
 384         if (zone_status_get(zone) >= ZONE_IS_SHUTTING_DOWN)
 385                 return (EBUSY);
 386 
 387         /*
 388          * We need a lock to serialize this; minor_lock is as good as any.
 389          */
 390         mutex_enter(&autofs_minor_lock);
 391         if ((fngp = zone_getspecific(autofs_key, zone)) == NULL) {
 392                 fngp = autofs_zone_init();
 393                 (void) zone_setspecific(autofs_key, zone, fngp);
 394         }
 395         mutex_exit(&autofs_minor_lock);
 396         ASSERT(fngp != NULL);
 397 
 398         /*
 399          * Get arguments
 400          */
 401         if (uap->flags & MS_SYSSPACE) {
 402                 if (datalen != sizeof (args))
 403                         return (EINVAL);
 404                 error = kcopy(data, &args, sizeof (args));
 405         } else {
 406                 if (get_udatamodel() == DATAMODEL_NATIVE) {
 407                         if (datalen != sizeof (args))
 408                                 return (EINVAL);
 409                         error = copyin(data, &args, sizeof (args));
 410                 } else {
 411                         struct autofs_args32 args32;
 412 
 413                         if (datalen != sizeof (args32))
 414                                 return (EINVAL);
 415                         error = copyin(data, &args32, sizeof (args32));
 416 
 417                         args.addr.maxlen = args32.addr.maxlen;
 418                         args.addr.len = args32.addr.len;
 419                         args.addr.buf = (char *)(uintptr_t)args32.addr.buf;
 420                         args.path = (char *)(uintptr_t)args32.path;
 421                         args.opts = (char *)(uintptr_t)args32.opts;
 422                         args.map = (char *)(uintptr_t)args32.map;
 423                         args.subdir = (char *)(uintptr_t)args32.subdir;
 424                         args.key = (char *)(uintptr_t)args32.key;
 425                         args.mount_to = args32.mount_to;
 426                         args.rpc_to = args32.rpc_to;
 427                         args.direct = args32.direct;
 428                 }
 429         }
 430         if (error)
 431                 return (EFAULT);
 432 
 433         /*
 434          * For a remount, only update mount information
 435          * i.e. default mount options, map name, etc.
 436          */
 437         if (uap->flags & MS_REMOUNT) {
 438                 fnip = vfstofni(vfsp);
 439                 if (fnip == NULL)
 440                         return (EINVAL);
 441 
 442                 if (args.direct == 1)
 443                         fnip->fi_flags |= MF_DIRECT;
 444                 else
 445                         fnip->fi_flags &= ~MF_DIRECT;
 446                 fnip->fi_mount_to = args.mount_to;
 447                 fnip->fi_rpc_to = args.rpc_to;
 448 
 449                 /*
 450                  * Get default options
 451                  */
 452                 if (uap->flags & MS_SYSSPACE)
 453                         error = copystr(args.opts, strbuff, sizeof (strbuff),
 454                             &len);
 455                 else
 456                         error = copyinstr(args.opts, strbuff, sizeof (strbuff),
 457                             &len);
 458                 if (error)
 459                         return (EFAULT);
 460 
 461                 if (autofs_restrict_opts(vfsp, strbuff, sizeof (strbuff), &len)
 462                     != 0) {
 463                         return (EFAULT);
 464                 }
 465 
 466                 kmem_free(fnip->fi_opts, fnip->fi_optslen);
 467                 fnip->fi_opts = kmem_alloc(len, KM_SLEEP);
 468                 fnip->fi_optslen = (int)len;
 469                 bcopy(strbuff, fnip->fi_opts, len);
 470 
 471                 /*
 472                  * Get context/map name
 473                  */
 474                 if (uap->flags & MS_SYSSPACE)
 475                         error = copystr(args.map, strbuff, sizeof (strbuff),
 476                             &len);
 477                 else
 478                         error = copyinstr(args.map, strbuff, sizeof (strbuff),
 479                             &len);
 480                 if (error)
 481                         return (EFAULT);
 482 
 483                 kmem_free(fnip->fi_map, fnip->fi_maplen);
 484                 fnip->fi_map = kmem_alloc(len, KM_SLEEP);
 485                 fnip->fi_maplen = (int)len;
 486                 bcopy(strbuff, fnip->fi_map, len);
 487 
 488                 return (0);
 489         }
 490 
 491         /*
 492          * Allocate fninfo struct and attach it to vfs
 493          */
 494         fnip = kmem_zalloc(sizeof (*fnip), KM_SLEEP);
 495         fnip->fi_mountvfs = vfsp;
 496 
 497         fnip->fi_mount_to = args.mount_to;
 498         fnip->fi_rpc_to = args.rpc_to;
 499         fnip->fi_refcnt = 0;
 500         vfsp->vfs_bsize = AUTOFS_BLOCKSIZE;
 501         vfsp->vfs_fstype = autofs_fstype;
 502 
 503         /*
 504          * Assign a unique device id to the mount
 505          */
 506         mutex_enter(&autofs_minor_lock);
 507         do {
 508                 autofs_minor = (autofs_minor + 1) & L_MAXMIN32;
 509                 autofs_dev = makedevice(autofs_major, autofs_minor);
 510         } while (vfs_devismounted(autofs_dev));
 511         mutex_exit(&autofs_minor_lock);
 512         vfsp->vfs_dev = autofs_dev;
 513         vfs_make_fsid(&vfsp->vfs_fsid, autofs_dev, autofs_fstype);
 514         vfsp->vfs_data = (void *)fnip;
 515         vfsp->vfs_bcount = 0;
 516 
 517         /*
 518          * Get daemon address
 519          */
 520         fnip->fi_addr.len = args.addr.len;
 521         fnip->fi_addr.maxlen = fnip->fi_addr.len;
 522         fnip->fi_addr.buf = kmem_alloc(args.addr.len, KM_SLEEP);
 523         if (uap->flags & MS_SYSSPACE)
 524                 error = kcopy(args.addr.buf, fnip->fi_addr.buf, args.addr.len);
 525         else
 526                 error = copyin(args.addr.buf, fnip->fi_addr.buf, args.addr.len);
 527         if (error) {
 528                 error = EFAULT;
 529                 goto errout;
 530         }
 531 
 532         fnip->fi_zoneid = getzoneid();
 533         /*
 534          * Get path for mountpoint
 535          */
 536         if (uap->flags & MS_SYSSPACE)
 537                 error = copystr(args.path, strbuff, sizeof (strbuff), &len);
 538         else
 539                 error = copyinstr(args.path, strbuff, sizeof (strbuff), &len);
 540         if (error) {
 541                 error = EFAULT;
 542                 goto errout;
 543         }
 544         fnip->fi_path = kmem_alloc(len, KM_SLEEP);
 545         fnip->fi_pathlen = (int)len;
 546         bcopy(strbuff, fnip->fi_path, len);
 547 
 548         /*
 549          * Get default options
 550          */
 551         if (uap->flags & MS_SYSSPACE)
 552                 error = copystr(args.opts, strbuff, sizeof (strbuff), &len);
 553         else
 554                 error = copyinstr(args.opts, strbuff, sizeof (strbuff), &len);
 555 
 556         if (error != 0 ||
 557             autofs_restrict_opts(vfsp, strbuff, sizeof (strbuff), &len) != 0) {
 558                 error = EFAULT;
 559                 goto errout;
 560         }
 561         fnip->fi_opts = kmem_alloc(len, KM_SLEEP);
 562         fnip->fi_optslen = (int)len;
 563         bcopy(strbuff, fnip->fi_opts, len);
 564 
 565         /*
 566          * Get context/map name
 567          */
 568         if (uap->flags & MS_SYSSPACE)
 569                 error = copystr(args.map, strbuff, sizeof (strbuff), &len);
 570         else
 571                 error = copyinstr(args.map, strbuff, sizeof (strbuff), &len);
 572         if (error) {
 573                 error = EFAULT;
 574                 goto errout;
 575         }
 576         fnip->fi_map = kmem_alloc(len, KM_SLEEP);
 577         fnip->fi_maplen = (int)len;
 578         bcopy(strbuff, fnip->fi_map, len);
 579 
 580         /*
 581          * Get subdirectory within map
 582          */
 583         if (uap->flags & MS_SYSSPACE)
 584                 error = copystr(args.subdir, strbuff, sizeof (strbuff), &len);
 585         else
 586                 error = copyinstr(args.subdir, strbuff, sizeof (strbuff), &len);
 587         if (error) {
 588                 error = EFAULT;
 589                 goto errout;
 590         }
 591         fnip->fi_subdir = kmem_alloc(len, KM_SLEEP);
 592         fnip->fi_subdirlen = (int)len;
 593         bcopy(strbuff, fnip->fi_subdir, len);
 594 
 595         /*
 596          * Get the key
 597          */
 598         if (uap->flags & MS_SYSSPACE)
 599                 error = copystr(args.key, strbuff, sizeof (strbuff), &len);
 600         else
 601                 error = copyinstr(args.key, strbuff, sizeof (strbuff), &len);
 602         if (error) {
 603                 error = EFAULT;
 604                 goto errout;
 605         }
 606         fnip->fi_key = kmem_alloc(len, KM_SLEEP);
 607         fnip->fi_keylen = (int)len;
 608         bcopy(strbuff, fnip->fi_key, len);
 609 
 610         /*
 611          * Is this a direct mount?
 612          */
 613         if (args.direct == 1)
 614                 fnip->fi_flags |= MF_DIRECT;
 615 
 616         /*
 617          * Setup netconfig.
 618          * Can I pass in knconf as mount argument? what
 619          * happens when the daemon gets restarted?
 620          */
 621         if ((error = lookupname("/dev/ticotsord", UIO_SYSSPACE, FOLLOW,
 622             NULLVPP, &kkvp)) != 0) {
 623                 cmn_err(CE_WARN, "autofs: lookupname: %d", error);
 624                 goto errout;
 625         }
 626 
 627         fnip->fi_knconf.knc_rdev = kkvp->v_rdev;
 628         fnip->fi_knconf.knc_protofmly = NC_LOOPBACK;
 629         fnip->fi_knconf.knc_semantics = NC_TPI_COTS_ORD;
 630         VN_RELE(kkvp);
 631 
 632         /*
 633          * Make the root vnode
 634          */
 635         rootfnp = auto_makefnnode(VDIR, vfsp, fnip->fi_path, cr, fngp);
 636         if (rootfnp == NULL) {
 637                 error = ENOMEM;
 638                 goto errout;
 639         }
 640         rootvp = fntovn(rootfnp);
 641 
 642         rootvp->v_flag |= VROOT;
 643         rootfnp->fn_mode = AUTOFS_MODE;
 644         rootfnp->fn_parent = rootfnp;
 645         /* account for ".." entry */
 646         rootfnp->fn_linkcnt = rootfnp->fn_size = 1;
 647         fnip->fi_rootvp = rootvp;
 648 
 649         /*
 650          * Add to list of top level AUTOFS' if it is being mounted by
 651          * a user level process.
 652          */
 653         if (!(uap->flags & MS_SYSSPACE)) {
 654                 rw_enter(&fngp->fng_rootfnnodep->fn_rwlock, RW_WRITER);
 655                 rootfnp->fn_parent = fngp->fng_rootfnnodep;
 656                 rootfnp->fn_next = fngp->fng_rootfnnodep->fn_dirents;
 657                 fngp->fng_rootfnnodep->fn_dirents = rootfnp;
 658                 rw_exit(&fngp->fng_rootfnnodep->fn_rwlock);
 659         }
 660 
 661         AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
 662             (void *)vfsp, (void *)rootvp, (void *)fnip, error));
 663 
 664         return (0);
 665 
 666 errout:
 667         ASSERT(fnip != NULL);
 668         ASSERT((uap->flags & MS_REMOUNT) == 0);
 669 
 670         if (fnip->fi_addr.buf != NULL)
 671                 kmem_free(fnip->fi_addr.buf, fnip->fi_addr.len);
 672         if (fnip->fi_path != NULL)
 673                 kmem_free(fnip->fi_path, fnip->fi_pathlen);
 674         if (fnip->fi_opts != NULL)
 675                 kmem_free(fnip->fi_opts, fnip->fi_optslen);
 676         if (fnip->fi_map != NULL)
 677                 kmem_free(fnip->fi_map, fnip->fi_maplen);
 678         if (fnip->fi_subdir != NULL)
 679                 kmem_free(fnip->fi_subdir, fnip->fi_subdirlen);
 680         if (fnip->fi_key != NULL)
 681                 kmem_free(fnip->fi_key, fnip->fi_keylen);
 682         kmem_free(fnip, sizeof (*fnip));
 683 
 684         AUTOFS_DPRINT((5, "auto_mount: vfs %p root %p fnip %p return %d\n",
 685             (void *)vfsp, (void *)rootvp, (void *)fnip, error));
 686 
 687         return (error);
 688 }
 689 
 690 /* ARGSUSED */
 691 static int
 692 auto_unmount(vfs_t *vfsp, int flag, cred_t *cr)
 693 {
 694         fninfo_t *fnip;
 695         vnode_t *rvp;
 696         fnnode_t *rfnp, *fnp, *pfnp;
 697         fnnode_t *myrootfnnodep;
 698 
 699         fnip = vfstofni(vfsp);
 700         AUTOFS_DPRINT((4, "auto_unmount vfsp %p fnip %p\n", (void *)vfsp,
 701             (void *)fnip));
 702 
 703         if (secpolicy_fs_unmount(cr, vfsp) != 0)
 704                 return (EPERM);
 705         /*
 706          * forced unmount is not supported by this file system
 707          * and thus, ENOTSUP, is being returned.
 708          */
 709         if (flag & MS_FORCE)
 710                 return (ENOTSUP);
 711 
 712         ASSERT(vn_vfswlock_held(vfsp->vfs_vnodecovered));
 713         rvp = fnip->fi_rootvp;
 714         rfnp = vntofn(rvp);
 715 
 716         if (rvp->v_count > 1 || rfnp->fn_dirents != NULL)
 717                 return (EBUSY);
 718 
 719         /*
 720          * The root vnode is on the linked list of root fnnodes only if
 721          * this was not a trigger node. Since we have no way of knowing,
 722          * if we don't find it, then we assume it was a trigger node.
 723          */
 724         myrootfnnodep = rfnp->fn_globals->fng_rootfnnodep;
 725         pfnp = NULL;
 726         rw_enter(&myrootfnnodep->fn_rwlock, RW_WRITER);
 727         fnp = myrootfnnodep->fn_dirents;
 728         while (fnp != NULL) {
 729                 if (fnp == rfnp) {
 730                         /*
 731                          * A check here is made to see if rvp is busy.  If
 732                          * so, return EBUSY.  Otherwise proceed with
 733                          * disconnecting it from the list.
 734                          */
 735                         if (rvp->v_count > 1 || rfnp->fn_dirents != NULL) {
 736                                 rw_exit(&myrootfnnodep->fn_rwlock);
 737                                 return (EBUSY);
 738                         }
 739                         if (pfnp)
 740                                 pfnp->fn_next = fnp->fn_next;
 741                         else
 742                                 myrootfnnodep->fn_dirents = fnp->fn_next;
 743                         fnp->fn_next = NULL;
 744                         break;
 745                 }
 746                 pfnp = fnp;
 747                 fnp = fnp->fn_next;
 748         }
 749         rw_exit(&myrootfnnodep->fn_rwlock);
 750 
 751         ASSERT(rvp->v_count == 1);
 752         ASSERT(rfnp->fn_size == 1);
 753         ASSERT(rfnp->fn_linkcnt == 1);
 754         /*
 755          * The following drops linkcnt to 0, therefore the disconnect is
 756          * not attempted when auto_inactive() is called by
 757          * vn_rele(). This is necessary because we have nothing to get
 758          * disconnected from since we're the root of the filesystem. As a
 759          * side effect the node is not freed, therefore I should free the
 760          * node here.
 761          *
 762          * XXX - I really need to think of a better way of doing this.
 763          */
 764         rfnp->fn_size--;
 765         rfnp->fn_linkcnt--;
 766 
 767         /*
 768          * release of last reference causes node
 769          * to be freed
 770          */
 771         VN_RELE(rvp);
 772         rfnp->fn_parent = NULL;
 773 
 774         auto_freefnnode(rfnp);
 775 
 776         kmem_free(fnip->fi_addr.buf, fnip->fi_addr.len);
 777         kmem_free(fnip->fi_path, fnip->fi_pathlen);
 778         kmem_free(fnip->fi_map, fnip->fi_maplen);
 779         kmem_free(fnip->fi_subdir, fnip->fi_subdirlen);
 780         kmem_free(fnip->fi_key, fnip->fi_keylen);
 781         kmem_free(fnip->fi_opts, fnip->fi_optslen);
 782         kmem_free(fnip, sizeof (*fnip));
 783         AUTOFS_DPRINT((5, "auto_unmount: return=0\n"));
 784 
 785         return (0);
 786 }
 787 
 788 
 789 /*
 790  * find root of autofs
 791  */
 792 static int
 793 auto_root(vfs_t *vfsp, vnode_t **vpp)
 794 {
 795         *vpp = (vnode_t *)vfstofni(vfsp)->fi_rootvp;
 796         VN_HOLD(*vpp);
 797 
 798         AUTOFS_DPRINT((5, "auto_root: vfs %p, *vpp %p\n", (void *)vfsp,
 799             (void *)*vpp));
 800         return (0);
 801 }
 802 
 803 /*
 804  * Get file system statistics.
 805  */
 806 static int
 807 auto_statvfs(vfs_t *vfsp, struct statvfs64 *sbp)
 808 {
 809         dev32_t d32;
 810 
 811         AUTOFS_DPRINT((4, "auto_statvfs %p\n", (void *)vfsp));
 812 
 813         bzero(sbp, sizeof (*sbp));
 814         sbp->f_bsize = vfsp->vfs_bsize;
 815         sbp->f_frsize        = sbp->f_bsize;
 816         sbp->f_blocks        = (fsblkcnt64_t)0;
 817         sbp->f_bfree = (fsblkcnt64_t)0;
 818         sbp->f_bavail        = (fsblkcnt64_t)0;
 819         sbp->f_files = (fsfilcnt64_t)0;
 820         sbp->f_ffree = (fsfilcnt64_t)0;
 821         sbp->f_favail        = (fsfilcnt64_t)0;
 822         (void) cmpldev(&d32, vfsp->vfs_dev);
 823         sbp->f_fsid  = d32;
 824         (void) strcpy(sbp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
 825         sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
 826         sbp->f_namemax = MAXNAMELEN;
 827         (void) strcpy(sbp->f_fstr, MNTTYPE_AUTOFS);
 828 
 829         return (0);
 830 }