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) 1990, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright (c) 2011, Joyent, Inc. All rights reserved.
  24  * Copyright 2016 RackTop Systems.
  25  */
  26 
  27 #include <sys/types.h>
  28 #include <sys/param.h>
  29 #include <sys/sysmacros.h>
  30 #include <sys/kmem.h>
  31 #include <sys/time.h>
  32 #include <sys/pathname.h>
  33 #include <sys/vfs.h>
  34 #include <sys/vfs_opreg.h>
  35 #include <sys/vnode.h>
  36 #include <sys/stat.h>
  37 #include <sys/uio.h>
  38 #include <sys/stat.h>
  39 #include <sys/errno.h>
  40 #include <sys/cmn_err.h>
  41 #include <sys/cred.h>
  42 #include <sys/statvfs.h>
  43 #include <sys/mount.h>
  44 #include <sys/debug.h>
  45 #include <sys/systm.h>
  46 #include <sys/mntent.h>
  47 #include <fs/fs_subr.h>
  48 #include <vm/page.h>
  49 #include <vm/anon.h>
  50 #include <sys/model.h>
  51 #include <sys/policy.h>
  52 
  53 #include <sys/fs/swapnode.h>
  54 #include <sys/fs/tmp.h>
  55 #include <sys/fs/tmpnode.h>
  56 
  57 static int tmpfsfstype;
  58 
  59 /*
  60  * tmpfs vfs operations.
  61  */
  62 static int tmpfsinit(int, char *);
  63 static int tmp_mount(struct vfs *, struct vnode *,
  64         struct mounta *, struct cred *);
  65 static int tmp_unmount(struct vfs *, int, struct cred *);
  66 static int tmp_root(struct vfs *, struct vnode **);
  67 static int tmp_statvfs(struct vfs *, struct statvfs64 *);
  68 static int tmp_vget(struct vfs *, struct vnode **, struct fid *);
  69 
  70 /*
  71  * Loadable module wrapper
  72  */
  73 #include <sys/modctl.h>
  74 
  75 static mntopts_t tmpfs_proto_opttbl;
  76 
  77 static vfsdef_t vfw = {
  78         VFSDEF_VERSION,
  79         "tmpfs",
  80         tmpfsinit,
  81         VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS|VSW_ZMOUNT,
  82         &tmpfs_proto_opttbl
  83 };
  84 
  85 /*
  86  * in-kernel mnttab options
  87  */
  88 static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
  89 static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
  90 
  91 static mntopt_t tmpfs_options[] = {
  92         /* Option name          Cancel Opt      Arg     Flags           Data */
  93         { MNTOPT_XATTR,         xattr_cancel,   NULL,   MO_DEFAULT,     NULL},
  94         { MNTOPT_NOXATTR,       noxattr_cancel, NULL,   NULL,           NULL},
  95         { "size",               NULL,           "0",    MO_HASVALUE,    NULL}
  96 };
  97 
  98 
  99 static mntopts_t tmpfs_proto_opttbl = {
 100         sizeof (tmpfs_options) / sizeof (mntopt_t),
 101         tmpfs_options
 102 };
 103 
 104 /*
 105  * Module linkage information
 106  */
 107 static struct modlfs modlfs = {
 108         &mod_fsops, "filesystem for tmpfs", &vfw
 109 };
 110 
 111 static struct modlinkage modlinkage = {
 112         MODREV_1, &modlfs, NULL
 113 };
 114 
 115 int
 116 _init()
 117 {
 118         return (mod_install(&modlinkage));
 119 }
 120 
 121 int
 122 _fini()
 123 {
 124         int error;
 125 
 126         error = mod_remove(&modlinkage);
 127         if (error)
 128                 return (error);
 129         /*
 130          * Tear down the operations vectors
 131          */
 132         (void) vfs_freevfsops_by_type(tmpfsfstype);
 133         vn_freevnodeops(tmp_vnodeops);
 134         return (0);
 135 }
 136 
 137 int
 138 _info(struct modinfo *modinfop)
 139 {
 140         return (mod_info(&modlinkage, modinfop));
 141 }
 142 
 143 /*
 144  * The following are patchable variables limiting the amount of system
 145  * resources tmpfs can use.
 146  *
 147  * tmpfs_maxkmem limits the amount of kernel kmem_alloc memory
 148  * tmpfs can use for it's data structures (e.g. tmpnodes, directory entries)
 149  * It is not determined by setting a hard limit but rather as a percentage of
 150  * physical memory which is determined when tmpfs is first used in the system.
 151  *
 152  * tmpfs_minfree is the minimum amount of swap space that tmpfs leaves for
 153  * the rest of the system.  In other words, if the amount of free swap space
 154  * in the system (i.e. anoninfo.ani_free) drops below tmpfs_minfree, tmpfs
 155  * anon allocations will fail.
 156  *
 157  * There is also a per mount limit on the amount of swap space
 158  * (tmount.tm_anonmax) settable via a mount option.
 159  */
 160 size_t tmpfs_maxkmem = 0;
 161 size_t tmpfs_minfree = 0;
 162 size_t tmp_kmemspace;           /* bytes of kernel heap used by all tmpfs */
 163 
 164 static major_t tmpfs_major;
 165 static minor_t tmpfs_minor;
 166 static kmutex_t tmpfs_minor_lock;
 167 
 168 /*
 169  * initialize global tmpfs locks and such
 170  * called when loading tmpfs module
 171  */
 172 static int
 173 tmpfsinit(int fstype, char *name)
 174 {
 175         static const fs_operation_def_t tmp_vfsops_template[] = {
 176                 VFSNAME_MOUNT,          { .vfs_mount = tmp_mount },
 177                 VFSNAME_UNMOUNT,        { .vfs_unmount = tmp_unmount },
 178                 VFSNAME_ROOT,           { .vfs_root = tmp_root },
 179                 VFSNAME_STATVFS,        { .vfs_statvfs = tmp_statvfs },
 180                 VFSNAME_VGET,           { .vfs_vget = tmp_vget },
 181                 NULL,                   NULL
 182         };
 183         int error;
 184         extern  void    tmpfs_hash_init();
 185 
 186         tmpfs_hash_init();
 187         tmpfsfstype = fstype;
 188         ASSERT(tmpfsfstype != 0);
 189 
 190         error = vfs_setfsops(fstype, tmp_vfsops_template, NULL);
 191         if (error != 0) {
 192                 cmn_err(CE_WARN, "tmpfsinit: bad vfs ops template");
 193                 return (error);
 194         }
 195 
 196         error = vn_make_ops(name, tmp_vnodeops_template, &tmp_vnodeops);
 197         if (error != 0) {
 198                 (void) vfs_freevfsops_by_type(fstype);
 199                 cmn_err(CE_WARN, "tmpfsinit: bad vnode ops template");
 200                 return (error);
 201         }
 202 
 203         /*
 204          * tmpfs_minfree doesn't need to be some function of configured
 205          * swap space since it really is an absolute limit of swap space
 206          * which still allows other processes to execute.
 207          */
 208         if (tmpfs_minfree == 0) {
 209                 /*
 210                  * Set if not patched
 211                  */
 212                 tmpfs_minfree = btopr(TMPMINFREE);
 213         }
 214 
 215         /*
 216          * The maximum amount of space tmpfs can allocate is
 217          * TMPMAXPROCKMEM percent of kernel memory
 218          */
 219         if (tmpfs_maxkmem == 0)
 220                 tmpfs_maxkmem = MAX(PAGESIZE, kmem_maxavail() / TMPMAXFRACKMEM);
 221 
 222         if ((tmpfs_major = getudev()) == (major_t)-1) {
 223                 cmn_err(CE_WARN, "tmpfsinit: Can't get unique device number.");
 224                 tmpfs_major = 0;
 225         }
 226         mutex_init(&tmpfs_minor_lock, NULL, MUTEX_DEFAULT, NULL);
 227         return (0);
 228 }
 229 
 230 static int
 231 tmp_mount(
 232         struct vfs *vfsp,
 233         struct vnode *mvp,
 234         struct mounta *uap,
 235         struct cred *cr)
 236 {
 237         struct tmount *tm = NULL;
 238         struct tmpnode *tp;
 239         struct pathname dpn;
 240         int error;
 241         pgcnt_t anonmax;
 242         struct vattr rattr;
 243         int got_attrs;
 244 
 245         char *sizestr;
 246 
 247         if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
 248                 return (error);
 249 
 250         if (mvp->v_type != VDIR)
 251                 return (ENOTDIR);
 252 
 253         mutex_enter(&mvp->v_lock);
 254         if ((uap->flags & MS_REMOUNT) == 0 && (uap->flags & MS_OVERLAY) == 0 &&
 255             (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
 256                 mutex_exit(&mvp->v_lock);
 257                 return (EBUSY);
 258         }
 259         mutex_exit(&mvp->v_lock);
 260 
 261         /*
 262          * Having the resource be anything but "swap" doesn't make sense.
 263          */
 264         vfs_setresource(vfsp, "swap", 0);
 265 
 266         /*
 267          * now look for options we understand...
 268          */
 269 
 270         /* tmpfs doesn't support read-only mounts */
 271         if (vfs_optionisset(vfsp, MNTOPT_RO, NULL)) {
 272                 error = EINVAL;
 273                 goto out;
 274         }
 275 
 276         /*
 277          * tm_anonmax is set according to the mount arguments
 278          * if any.  Otherwise, it is set to a maximum value.
 279          */
 280         if (vfs_optionisset(vfsp, "size", &sizestr)) {
 281                 if ((error = tmp_convnum(sizestr, &anonmax)) != 0)
 282                         goto out;
 283         } else {
 284                 anonmax = ULONG_MAX;
 285         }
 286 
 287         if (error = pn_get(uap->dir,
 288             (uap->flags & MS_SYSSPACE) ? UIO_SYSSPACE : UIO_USERSPACE, &dpn))
 289                 goto out;
 290 
 291         if (uap->flags & MS_REMOUNT) {
 292                 tm = (struct tmount *)VFSTOTM(vfsp);
 293 
 294                 /*
 295                  * If we change the size so its less than what is currently
 296                  * being used, we allow that. The file system will simply be
 297                  * full until enough files have been removed to get below the
 298                  * new max.
 299                  */
 300                 mutex_enter(&tm->tm_contents);
 301                 tm->tm_anonmax = anonmax;
 302                 mutex_exit(&tm->tm_contents);
 303                 goto out;
 304         }
 305 
 306         if ((tm = tmp_memalloc(sizeof (struct tmount), 0)) == NULL) {
 307                 pn_free(&dpn);
 308                 error = ENOMEM;
 309                 goto out;
 310         }
 311 
 312         /*
 313          * find an available minor device number for this mount
 314          */
 315         mutex_enter(&tmpfs_minor_lock);
 316         do {
 317                 tmpfs_minor = (tmpfs_minor + 1) & L_MAXMIN32;
 318                 tm->tm_dev = makedevice(tmpfs_major, tmpfs_minor);
 319         } while (vfs_devismounted(tm->tm_dev));
 320         mutex_exit(&tmpfs_minor_lock);
 321 
 322         /*
 323          * Set but don't bother entering the mutex
 324          * (tmount not on mount list yet)
 325          */
 326         mutex_init(&tm->tm_contents, NULL, MUTEX_DEFAULT, NULL);
 327         mutex_init(&tm->tm_renamelck, NULL, MUTEX_DEFAULT, NULL);
 328 
 329         tm->tm_vfsp = vfsp;
 330         tm->tm_anonmax = anonmax;
 331 
 332         vfsp->vfs_data = (caddr_t)tm;
 333         vfsp->vfs_fstype = tmpfsfstype;
 334         vfsp->vfs_dev = tm->tm_dev;
 335         vfsp->vfs_bsize = PAGESIZE;
 336         vfsp->vfs_flag |= VFS_NOTRUNC;
 337         vfs_make_fsid(&vfsp->vfs_fsid, tm->tm_dev, tmpfsfstype);
 338         tm->tm_mntpath = tmp_memalloc(dpn.pn_pathlen + 1, TMP_MUSTHAVE);
 339         (void) strcpy(tm->tm_mntpath, dpn.pn_path);
 340 
 341         /*
 342          * allocate and initialize root tmpnode structure
 343          */
 344         bzero(&rattr, sizeof (struct vattr));
 345         rattr.va_mode = (mode_t)(S_IFDIR | 0777);       /* XXX modes */
 346         rattr.va_type = VDIR;
 347         rattr.va_rdev = 0;
 348         tp = tmp_memalloc(sizeof (struct tmpnode), TMP_MUSTHAVE);
 349         tmpnode_init(tm, tp, &rattr, cr);
 350 
 351         /*
 352          * Get the mode, uid, and gid from the underlying mount point.
 353          */
 354         rattr.va_mask = AT_MODE|AT_UID|AT_GID;  /* Hint to getattr */
 355         got_attrs = VOP_GETATTR(mvp, &rattr, 0, cr, NULL);
 356 
 357         rw_enter(&tp->tn_rwlock, RW_WRITER);
 358         TNTOV(tp)->v_flag |= VROOT;
 359 
 360         /*
 361          * If the getattr succeeded, use its results.  Otherwise allow
 362          * the previously set hardwired defaults to prevail.
 363          */
 364         if (got_attrs == 0) {
 365                 tp->tn_mode = rattr.va_mode;
 366                 tp->tn_uid = rattr.va_uid;
 367                 tp->tn_gid = rattr.va_gid;
 368         }
 369 
 370         /*
 371          * initialize linked list of tmpnodes so that the back pointer of
 372          * the root tmpnode always points to the last one on the list
 373          * and the forward pointer of the last node is null
 374          */
 375         tp->tn_back = tp;
 376         tp->tn_forw = NULL;
 377         tp->tn_nlink = 0;
 378         tm->tm_rootnode = tp;
 379 
 380         tdirinit(tp, tp);
 381 
 382         rw_exit(&tp->tn_rwlock);
 383 
 384         pn_free(&dpn);
 385         error = 0;
 386 
 387 out:
 388         if (error == 0)
 389                 vfs_set_feature(vfsp, VFSFT_SYSATTR_VIEWS);
 390 
 391         return (error);
 392 }
 393 
 394 static int
 395 tmp_unmount(struct vfs *vfsp, int flag, struct cred *cr)
 396 {
 397         struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
 398         struct tmpnode *tnp, *cancel;
 399         struct vnode    *vp;
 400         int error;
 401 
 402         if ((error = secpolicy_fs_unmount(cr, vfsp)) != 0)
 403                 return (error);
 404 
 405         /*
 406          * forced unmount is not supported by this file system
 407          * and thus, ENOTSUP, is being returned.
 408          */
 409         if (flag & MS_FORCE)
 410                 return (ENOTSUP);
 411 
 412         mutex_enter(&tm->tm_contents);
 413 
 414         /*
 415          * If there are no open files, only the root node should have
 416          * a reference count.
 417          * With tm_contents held, nothing can be added or removed.
 418          * There may be some dirty pages.  To prevent fsflush from
 419          * disrupting the unmount, put a hold on each node while scanning.
 420          * If we find a previously referenced node, undo the holds we have
 421          * placed and fail EBUSY.
 422          */
 423         tnp = tm->tm_rootnode;
 424         if (TNTOV(tnp)->v_count > 1) {
 425                 mutex_exit(&tm->tm_contents);
 426                 return (EBUSY);
 427         }
 428 
 429         for (tnp = tnp->tn_forw; tnp; tnp = tnp->tn_forw) {
 430                 if ((vp = TNTOV(tnp))->v_count > 0) {
 431                         cancel = tm->tm_rootnode->tn_forw;
 432                         while (cancel != tnp) {
 433                                 vp = TNTOV(cancel);
 434                                 ASSERT(vp->v_count > 0);
 435                                 VN_RELE(vp);
 436                                 cancel = cancel->tn_forw;
 437                         }
 438                         mutex_exit(&tm->tm_contents);
 439                         return (EBUSY);
 440                 }
 441                 VN_HOLD(vp);
 442         }
 443 
 444         /*
 445          * We can drop the mutex now because no one can find this mount
 446          */
 447         mutex_exit(&tm->tm_contents);
 448 
 449         /*
 450          * Free all kmemalloc'd and anonalloc'd memory associated with
 451          * this filesystem.  To do this, we go through the file list twice,
 452          * once to remove all the directory entries, and then to remove
 453          * all the files.  We do this because there is useful code in
 454          * tmpnode_free which assumes that the directory entry has been
 455          * removed before the file.
 456          */
 457         /*
 458          * Remove all directory entries
 459          */
 460         for (tnp = tm->tm_rootnode; tnp; tnp = tnp->tn_forw) {
 461                 rw_enter(&tnp->tn_rwlock, RW_WRITER);
 462                 if (tnp->tn_type == VDIR)
 463                         tdirtrunc(tnp);
 464                 if (tnp->tn_vnode->v_flag & V_XATTRDIR) {
 465                         /*
 466                          * Account for implicit attrdir reference.
 467                          */
 468                         ASSERT(tnp->tn_nlink > 0);
 469                         DECR_COUNT(&tnp->tn_nlink, &tnp->tn_tlock);
 470                 }
 471                 rw_exit(&tnp->tn_rwlock);
 472         }
 473 
 474         ASSERT(tm->tm_rootnode);
 475 
 476         /*
 477          * All links are gone, v_count is keeping nodes in place.
 478          * VN_RELE should make the node disappear, unless somebody
 479          * is holding pages against it.  Nap and retry until it disappears.
 480          *
 481          * We re-acquire the lock to prevent others who have a HOLD on
 482          * a tmpnode via its pages or anon slots from blowing it away
 483          * (in tmp_inactive) while we're trying to get to it here. Once
 484          * we have a HOLD on it we know it'll stick around.
 485          *
 486          */
 487         mutex_enter(&tm->tm_contents);
 488         /*
 489          * Remove all the files (except the rootnode) backwards.
 490          */
 491         while ((tnp = tm->tm_rootnode->tn_back) != tm->tm_rootnode) {
 492                 mutex_exit(&tm->tm_contents);
 493                 /*
 494                  * Inhibit tmp_inactive from touching attribute directory
 495                  * as all nodes will be released here.
 496                  * Note we handled the link count in pass 2 above.
 497                  */
 498                 rw_enter(&tnp->tn_rwlock, RW_WRITER);
 499                 tnp->tn_xattrdp = NULL;
 500                 rw_exit(&tnp->tn_rwlock);
 501                 vp = TNTOV(tnp);
 502                 VN_RELE(vp);
 503                 mutex_enter(&tm->tm_contents);
 504                 /*
 505                  * It's still there after the RELE. Someone else like pageout
 506                  * has a hold on it so wait a bit and then try again - we know
 507                  * they'll give it up soon.
 508                  */
 509                 if (tnp == tm->tm_rootnode->tn_back) {
 510                         VN_HOLD(vp);
 511                         mutex_exit(&tm->tm_contents);
 512                         delay(hz / 4);
 513                         mutex_enter(&tm->tm_contents);
 514                 }
 515         }
 516         mutex_exit(&tm->tm_contents);
 517 
 518         tm->tm_rootnode->tn_xattrdp = NULL;
 519         VN_RELE(TNTOV(tm->tm_rootnode));
 520 
 521         ASSERT(tm->tm_mntpath);
 522 
 523         tmp_memfree(tm->tm_mntpath, strlen(tm->tm_mntpath) + 1);
 524 
 525         ASSERT(tm->tm_anonmem == 0);
 526 
 527         mutex_destroy(&tm->tm_contents);
 528         mutex_destroy(&tm->tm_renamelck);
 529         tmp_memfree(tm, sizeof (struct tmount));
 530 
 531         return (0);
 532 }
 533 
 534 /*
 535  * return root tmpnode for given vnode
 536  */
 537 static int
 538 tmp_root(struct vfs *vfsp, struct vnode **vpp)
 539 {
 540         struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
 541         struct tmpnode *tp = tm->tm_rootnode;
 542         struct vnode *vp;
 543 
 544         ASSERT(tp);
 545 
 546         vp = TNTOV(tp);
 547         VN_HOLD(vp);
 548         *vpp = vp;
 549         return (0);
 550 }
 551 
 552 static int
 553 tmp_statvfs(struct vfs *vfsp, struct statvfs64 *sbp)
 554 {
 555         struct tmount   *tm = (struct tmount *)VFSTOTM(vfsp);
 556         ulong_t blocks;
 557         dev32_t d32;
 558         zoneid_t eff_zid;
 559         struct zone *zp;
 560 
 561         /*
 562          * The file system may have been mounted by the global zone on
 563          * behalf of the non-global zone.  In that case, the tmount zone_id
 564          * will be the global zone.  We still want to show the swap cap inside
 565          * the zone in this case, even though the file system was mounted by
 566          * the global zone.
 567          */
 568         if (curproc->p_zone->zone_id != GLOBAL_ZONEUNIQID)
 569                 zp = curproc->p_zone;
 570         else
 571                 zp = tm->tm_vfsp->vfs_zone;
 572 
 573         if (zp == NULL)
 574                 eff_zid = GLOBAL_ZONEUNIQID;
 575         else
 576                 eff_zid = zp->zone_id;
 577 
 578         sbp->f_bsize = PAGESIZE;
 579         sbp->f_frsize = PAGESIZE;
 580 
 581         /*
 582          * Find the amount of available physical and memory swap
 583          */
 584         mutex_enter(&anoninfo_lock);
 585         ASSERT(k_anoninfo.ani_max >= k_anoninfo.ani_phys_resv);
 586         blocks = (ulong_t)CURRENT_TOTAL_AVAILABLE_SWAP;
 587         mutex_exit(&anoninfo_lock);
 588 
 589         /*
 590          * Compare the amount of space that should be free to this mount
 591          * (which may be limited by tm_anonmax) with the amount of space
 592          * that's actually free to all tmpfs and use the lowest value.
 593          */
 594         if (tm->tm_anonmax == ULONG_MAX)
 595                 sbp->f_bfree = MAX(blocks - tmpfs_minfree, 0);
 596         else if (blocks > tmpfs_minfree && tm->tm_anonmax > tm->tm_anonmem)
 597                 sbp->f_bfree = MIN(blocks - tmpfs_minfree,
 598                     tm->tm_anonmax - tm->tm_anonmem);
 599         else
 600                 sbp->f_bfree = 0;
 601 
 602         sbp->f_bavail = sbp->f_bfree;
 603 
 604         /*
 605          * Total number of blocks is what's available plus what's been used
 606          */
 607         sbp->f_blocks = (fsblkcnt64_t)(sbp->f_bfree + tm->tm_anonmem);
 608 
 609         if (eff_zid != GLOBAL_ZONEUNIQID &&
 610             zp->zone_max_swap_ctl != UINT64_MAX) {
 611                 /*
 612                  * If the fs is used by a non-global zone with a swap cap,
 613                  * then report the capped size.
 614                  */
 615                 rctl_qty_t cap, used;
 616                 pgcnt_t pgcap, pgused;
 617 
 618                 mutex_enter(&zp->zone_mem_lock);
 619                 cap = zp->zone_max_swap_ctl;
 620                 used = zp->zone_max_swap;
 621                 mutex_exit(&zp->zone_mem_lock);
 622 
 623                 pgcap = btop(cap);
 624                 pgused = btop(used);
 625 
 626                 sbp->f_bfree = MIN(pgcap - pgused, sbp->f_bfree);
 627                 sbp->f_bavail = sbp->f_bfree;
 628                 sbp->f_blocks = MIN(pgcap, sbp->f_blocks);
 629         }
 630 
 631         /*
 632          * The maximum number of files available is approximately the number
 633          * of tmpnodes we can allocate from the number of blocks available
 634          * to this mount.  This is fairly inaccurate since it doesn't take
 635          * into account the names stored in the directory entries.
 636          */
 637         sbp->f_ffree = sbp->f_bfree == 0 ? 0 : sbp->f_bfree /
 638             (sizeof (struct tmpnode) + sizeof (struct tdirent));
 639         sbp->f_files = sbp->f_blocks == 0 ? 0 : sbp->f_blocks /
 640             (sizeof (struct tmpnode) + sizeof (struct tdirent));
 641         sbp->f_favail = (fsfilcnt64_t)(sbp->f_ffree);
 642         (void) cmpldev(&d32, vfsp->vfs_dev);
 643         sbp->f_fsid = d32;
 644         (void) strcpy(sbp->f_basetype, vfssw[tmpfsfstype].vsw_name);
 645         (void) strncpy(sbp->f_fstr, tm->tm_mntpath, sizeof (sbp->f_fstr));
 646         /*
 647          * ensure null termination
 648          */
 649         sbp->f_fstr[sizeof (sbp->f_fstr) - 1] = '\0';
 650         sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
 651         sbp->f_namemax = MAXNAMELEN - 1;
 652         return (0);
 653 }
 654 
 655 static int
 656 tmp_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
 657 {
 658         struct tfid *tfid;
 659         struct tmount *tm = (struct tmount *)VFSTOTM(vfsp);
 660         struct tmpnode *tp = NULL;
 661 
 662         tfid = (struct tfid *)fidp;
 663         *vpp = NULL;
 664 
 665         mutex_enter(&tm->tm_contents);
 666         for (tp = tm->tm_rootnode; tp; tp = tp->tn_forw) {
 667                 mutex_enter(&tp->tn_tlock);
 668                 if (tp->tn_nodeid == tfid->tfid_ino) {
 669                         /*
 670                          * If the gen numbers don't match we know the
 671                          * file won't be found since only one tmpnode
 672                          * can have this number at a time.
 673                          */
 674                         if (tp->tn_gen != tfid->tfid_gen || tp->tn_nlink == 0) {
 675                                 mutex_exit(&tp->tn_tlock);
 676                                 mutex_exit(&tm->tm_contents);
 677                                 return (0);
 678                         }
 679                         *vpp = (struct vnode *)TNTOV(tp);
 680 
 681                         VN_HOLD(*vpp);
 682 
 683                         if ((tp->tn_mode & S_ISVTX) &&
 684                             !(tp->tn_mode & (S_IXUSR | S_IFDIR))) {
 685                                 mutex_enter(&(*vpp)->v_lock);
 686                                 (*vpp)->v_flag |= VISSWAP;
 687                                 mutex_exit(&(*vpp)->v_lock);
 688                         }
 689                         mutex_exit(&tp->tn_tlock);
 690                         mutex_exit(&tm->tm_contents);
 691                         return (0);
 692                 }
 693                 mutex_exit(&tp->tn_tlock);
 694         }
 695         mutex_exit(&tm->tm_contents);
 696         return (0);
 697 }