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 /*
  23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright (c) 2015, Joyent, Inc. All rights reserved.
  29  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
  30  * Copyright 2016 RackTop Systems.
  31  */
  32 
  33 #include <sys/types.h>
  34 #include <sys/param.h>
  35 #include <sys/t_lock.h>
  36 #include <sys/systm.h>
  37 #include <sys/sysmacros.h>
  38 #include <sys/user.h>
  39 #include <sys/time.h>
  40 #include <sys/vfs.h>
  41 #include <sys/vfs_opreg.h>
  42 #include <sys/vnode.h>
  43 #include <sys/file.h>
  44 #include <sys/fcntl.h>
  45 #include <sys/flock.h>
  46 #include <sys/kmem.h>
  47 #include <sys/uio.h>
  48 #include <sys/errno.h>
  49 #include <sys/stat.h>
  50 #include <sys/cred.h>
  51 #include <sys/dirent.h>
  52 #include <sys/pathname.h>
  53 #include <sys/vmsystm.h>
  54 #include <sys/fs/tmp.h>
  55 #include <sys/fs/tmpnode.h>
  56 #include <sys/mman.h>
  57 #include <vm/hat.h>
  58 #include <vm/seg_vn.h>
  59 #include <vm/seg_map.h>
  60 #include <vm/seg.h>
  61 #include <vm/anon.h>
  62 #include <vm/as.h>
  63 #include <vm/page.h>
  64 #include <vm/pvn.h>
  65 #include <sys/cmn_err.h>
  66 #include <sys/debug.h>
  67 #include <sys/swap.h>
  68 #include <sys/buf.h>
  69 #include <sys/vm.h>
  70 #include <sys/vtrace.h>
  71 #include <sys/policy.h>
  72 #include <fs/fs_subr.h>
  73 
  74 static int      tmp_getapage(struct vnode *, u_offset_t, size_t, uint_t *,
  75         page_t **, size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
  76 static int      tmp_putapage(struct vnode *, page_t *, u_offset_t *, size_t *,
  77         int, struct cred *);
  78 
  79 /* ARGSUSED1 */
  80 static int
  81 tmp_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct)
  82 {
  83         /*
  84          * swapon to a tmpfs file is not supported so access
  85          * is denied on open if VISSWAP is set.
  86          */
  87         if ((*vpp)->v_flag & VISSWAP)
  88                 return (EINVAL);
  89         return (0);
  90 }
  91 
  92 /* ARGSUSED1 */
  93 static int
  94 tmp_close(
  95         struct vnode *vp,
  96         int flag,
  97         int count,
  98         offset_t offset,
  99         struct cred *cred,
 100         caller_context_t *ct)
 101 {
 102         cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
 103         cleanshares(vp, ttoproc(curthread)->p_pid);
 104         return (0);
 105 }
 106 
 107 /*
 108  * wrtmp does the real work of write requests for tmpfs.
 109  */
 110 static int
 111 wrtmp(
 112         struct tmount *tm,
 113         struct tmpnode *tp,
 114         struct uio *uio,
 115         struct cred *cr,
 116         struct caller_context *ct)
 117 {
 118         pgcnt_t pageoffset;     /* offset in pages */
 119         ulong_t segmap_offset;  /* pagesize byte offset into segmap */
 120         caddr_t base;           /* base of segmap */
 121         ssize_t bytes;          /* bytes to uiomove */
 122         pfn_t pagenumber;       /* offset in pages into tmp file */
 123         struct vnode *vp;
 124         int error = 0;
 125         int     pagecreate;     /* == 1 if we allocated a page */
 126         int     newpage;
 127         rlim64_t limit = uio->uio_llimit;
 128         long oresid = uio->uio_resid;
 129         timestruc_t now;
 130 
 131         long tn_size_changed = 0;
 132         long old_tn_size;
 133         long new_tn_size;
 134 
 135         vp = TNTOV(tp);
 136         ASSERT(vp->v_type == VREG);
 137 
 138         TRACE_1(TR_FAC_TMPFS, TR_TMPFS_RWTMP_START,
 139             "tmp_wrtmp_start:vp %p", vp);
 140 
 141         ASSERT(RW_WRITE_HELD(&tp->tn_contents));
 142         ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
 143 
 144         if (MANDLOCK(vp, tp->tn_mode)) {
 145                 rw_exit(&tp->tn_contents);
 146                 /*
 147                  * tmp_getattr ends up being called by chklock
 148                  */
 149                 error = chklock(vp, FWRITE, uio->uio_loffset, uio->uio_resid,
 150                     uio->uio_fmode, ct);
 151                 rw_enter(&tp->tn_contents, RW_WRITER);
 152                 if (error != 0) {
 153                         TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
 154                             "tmp_wrtmp_end:vp %p error %d", vp, error);
 155                         return (error);
 156                 }
 157         }
 158 
 159         if (uio->uio_loffset < 0)
 160                 return (EINVAL);
 161 
 162         if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
 163                 limit = MAXOFFSET_T;
 164 
 165         if (uio->uio_loffset >= limit) {
 166                 proc_t *p = ttoproc(curthread);
 167 
 168                 mutex_enter(&p->p_lock);
 169                 (void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
 170                     p, RCA_UNSAFE_SIGINFO);
 171                 mutex_exit(&p->p_lock);
 172                 return (EFBIG);
 173         }
 174 
 175         if (uio->uio_loffset >= MAXOFF_T) {
 176                 TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
 177                     "tmp_wrtmp_end:vp %p error %d", vp, EINVAL);
 178                 return (EFBIG);
 179         }
 180 
 181         if (uio->uio_resid == 0) {
 182                 TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
 183                     "tmp_wrtmp_end:vp %p error %d", vp, 0);
 184                 return (0);
 185         }
 186 
 187         if (limit > MAXOFF_T)
 188                 limit = MAXOFF_T;
 189 
 190         do {
 191                 long    offset;
 192                 long    delta;
 193 
 194                 offset = (long)uio->uio_offset;
 195                 pageoffset = offset & PAGEOFFSET;
 196                 /*
 197                  * A maximum of PAGESIZE bytes of data is transferred
 198                  * each pass through this loop
 199                  */
 200                 bytes = MIN(PAGESIZE - pageoffset, uio->uio_resid);
 201 
 202                 if (offset + bytes >= limit) {
 203                         if (offset >= limit) {
 204                                 error = EFBIG;
 205                                 goto out;
 206                         }
 207                         bytes = limit - offset;
 208                 }
 209                 pagenumber = btop(offset);
 210 
 211                 /*
 212                  * delta is the amount of anonymous memory
 213                  * to reserve for the file.
 214                  * We always reserve in pagesize increments so
 215                  * unless we're extending the file into a new page,
 216                  * we don't need to call tmp_resv.
 217                  */
 218                 delta = offset + bytes -
 219                     P2ROUNDUP_TYPED(tp->tn_size, PAGESIZE, u_offset_t);
 220                 if (delta > 0) {
 221                         pagecreate = 1;
 222                         if (tmp_resv(tm, tp, delta, pagecreate)) {
 223                                 /*
 224                                  * Log file system full in the zone that owns
 225                                  * the tmpfs mount, as well as in the global
 226                                  * zone if necessary.
 227                                  */
 228                                 zcmn_err(tm->tm_vfsp->vfs_zone->zone_id,
 229                                     CE_WARN, "%s: File system full, "
 230                                     "swap space limit exceeded",
 231                                     tm->tm_mntpath);
 232 
 233                                 if (tm->tm_vfsp->vfs_zone->zone_id !=
 234                                     GLOBAL_ZONEID) {
 235 
 236                                         vfs_t *vfs = tm->tm_vfsp;
 237 
 238                                         zcmn_err(GLOBAL_ZONEID,
 239                                             CE_WARN, "%s: File system full, "
 240                                             "swap space limit exceeded",
 241                                             vfs->vfs_vnodecovered->v_path);
 242                                 }
 243                                 error = ENOSPC;
 244                                 break;
 245                         }
 246                         tmpnode_growmap(tp, (ulong_t)offset + bytes);
 247                 }
 248                 /* grow the file to the new length */
 249                 if (offset + bytes > tp->tn_size) {
 250                         tn_size_changed = 1;
 251                         old_tn_size = tp->tn_size;
 252                         /*
 253                          * Postpone updating tp->tn_size until uiomove() is
 254                          * done.
 255                          */
 256                         new_tn_size = offset + bytes;
 257                 }
 258                 if (bytes == PAGESIZE) {
 259                         /*
 260                          * Writing whole page so reading from disk
 261                          * is a waste
 262                          */
 263                         pagecreate = 1;
 264                 } else {
 265                         pagecreate = 0;
 266                 }
 267                 /*
 268                  * If writing past EOF or filling in a hole
 269                  * we need to allocate an anon slot.
 270                  */
 271                 if (anon_get_ptr(tp->tn_anon, pagenumber) == NULL) {
 272                         (void) anon_set_ptr(tp->tn_anon, pagenumber,
 273                             anon_alloc(vp, ptob(pagenumber)), ANON_SLEEP);
 274                         pagecreate = 1;
 275                         tp->tn_nblocks++;
 276                 }
 277 
 278                 /*
 279                  * We have to drop the contents lock to allow the VM
 280                  * system to reacquire it in tmp_getpage()
 281                  */
 282                 rw_exit(&tp->tn_contents);
 283 
 284                 /*
 285                  * Touch the page and fault it in if it is not in core
 286                  * before segmap_getmapflt or vpm_data_copy can lock it.
 287                  * This is to avoid the deadlock if the buffer is mapped
 288                  * to the same file through mmap which we want to write.
 289                  */
 290                 uio_prefaultpages((long)bytes, uio);
 291 
 292                 newpage = 0;
 293                 if (vpm_enable) {
 294                         /*
 295                          * Copy data. If new pages are created, part of
 296                          * the page that is not written will be initizliazed
 297                          * with zeros.
 298                          */
 299                         error = vpm_data_copy(vp, offset, bytes, uio,
 300                             !pagecreate, &newpage, 1, S_WRITE);
 301                 } else {
 302                         /* Get offset within the segmap mapping */
 303                         segmap_offset = (offset & PAGEMASK) & MAXBOFFSET;
 304                         base = segmap_getmapflt(segkmap, vp,
 305                             (offset &  MAXBMASK), PAGESIZE, !pagecreate,
 306                             S_WRITE);
 307                 }
 308 
 309 
 310                 if (!vpm_enable && pagecreate) {
 311                         /*
 312                          * segmap_pagecreate() returns 1 if it calls
 313                          * page_create_va() to allocate any pages.
 314                          */
 315                         newpage = segmap_pagecreate(segkmap,
 316                             base + segmap_offset, (size_t)PAGESIZE, 0);
 317                         /*
 318                          * Clear from the beginning of the page to the starting
 319                          * offset of the data.
 320                          */
 321                         if (pageoffset != 0)
 322                                 (void) kzero(base + segmap_offset,
 323                                     (size_t)pageoffset);
 324                 }
 325 
 326                 if (!vpm_enable) {
 327                         error = uiomove(base + segmap_offset + pageoffset,
 328                             (long)bytes, UIO_WRITE, uio);
 329                 }
 330 
 331                 if (!vpm_enable && pagecreate &&
 332                     uio->uio_offset < P2ROUNDUP(offset + bytes, PAGESIZE)) {
 333                         long    zoffset; /* zero from offset into page */
 334                         /*
 335                          * We created pages w/o initializing them completely,
 336                          * thus we need to zero the part that wasn't set up.
 337                          * This happens on most EOF write cases and if
 338                          * we had some sort of error during the uiomove.
 339                          */
 340                         long nmoved;
 341 
 342                         nmoved = uio->uio_offset - offset;
 343                         ASSERT((nmoved + pageoffset) <= PAGESIZE);
 344 
 345                         /*
 346                          * Zero from the end of data in the page to the
 347                          * end of the page.
 348                          */
 349                         if ((zoffset = pageoffset + nmoved) < PAGESIZE)
 350                                 (void) kzero(base + segmap_offset + zoffset,
 351                                     (size_t)PAGESIZE - zoffset);
 352                 }
 353 
 354                 /*
 355                  * Unlock the pages which have been allocated by
 356                  * page_create_va() in segmap_pagecreate()
 357                  */
 358                 if (!vpm_enable && newpage) {
 359                         segmap_pageunlock(segkmap, base + segmap_offset,
 360                             (size_t)PAGESIZE, S_WRITE);
 361                 }
 362 
 363                 if (error) {
 364                         /*
 365                          * If we failed on a write, we must
 366                          * be sure to invalidate any pages that may have
 367                          * been allocated.
 368                          */
 369                         if (vpm_enable) {
 370                                 (void) vpm_sync_pages(vp, offset, PAGESIZE,
 371                                     SM_INVAL);
 372                         } else {
 373                                 (void) segmap_release(segkmap, base, SM_INVAL);
 374                         }
 375                 } else {
 376                         if (vpm_enable) {
 377                                 error = vpm_sync_pages(vp, offset, PAGESIZE,
 378                                     0);
 379                         } else {
 380                                 error = segmap_release(segkmap, base, 0);
 381                         }
 382                 }
 383 
 384                 /*
 385                  * Re-acquire contents lock.
 386                  */
 387                 rw_enter(&tp->tn_contents, RW_WRITER);
 388 
 389                 /*
 390                  * Update tn_size.
 391                  */
 392                 if (tn_size_changed)
 393                         tp->tn_size = new_tn_size;
 394 
 395                 /*
 396                  * If the uiomove failed, fix up tn_size.
 397                  */
 398                 if (error) {
 399                         if (tn_size_changed) {
 400                                 /*
 401                                  * The uiomove failed, and we
 402                                  * allocated blocks,so get rid
 403                                  * of them.
 404                                  */
 405                                 (void) tmpnode_trunc(tm, tp,
 406                                     (ulong_t)old_tn_size);
 407                         }
 408                 } else {
 409                         /*
 410                          * XXX - Can this be out of the loop?
 411                          */
 412                         if ((tp->tn_mode & (S_IXUSR | S_IXGRP | S_IXOTH)) &&
 413                             (tp->tn_mode & (S_ISUID | S_ISGID)) &&
 414                             secpolicy_vnode_setid_retain(cr,
 415                             (tp->tn_mode & S_ISUID) != 0 && tp->tn_uid == 0)) {
 416                                 /*
 417                                  * Clear Set-UID & Set-GID bits on
 418                                  * successful write if not privileged
 419                                  * and at least one of the execute bits
 420                                  * is set.  If we always clear Set-GID,
 421                                  * mandatory file and record locking is
 422                                  * unuseable.
 423                                  */
 424                                 tp->tn_mode &= ~(S_ISUID | S_ISGID);
 425                         }
 426                         gethrestime(&now);
 427                         tp->tn_mtime = now;
 428                         tp->tn_ctime = now;
 429                 }
 430         } while (error == 0 && uio->uio_resid > 0 && bytes != 0);
 431 
 432 out:
 433         /*
 434          * If we've already done a partial-write, terminate
 435          * the write but return no error.
 436          */
 437         if (oresid != uio->uio_resid)
 438                 error = 0;
 439         TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
 440             "tmp_wrtmp_end:vp %p error %d", vp, error);
 441         return (error);
 442 }
 443 
 444 /*
 445  * rdtmp does the real work of read requests for tmpfs.
 446  */
 447 static int
 448 rdtmp(
 449         struct tmount *tm,
 450         struct tmpnode *tp,
 451         struct uio *uio,
 452         struct caller_context *ct)
 453 {
 454         ulong_t pageoffset;     /* offset in tmpfs file (uio_offset) */
 455         ulong_t segmap_offset;  /* pagesize byte offset into segmap */
 456         caddr_t base;           /* base of segmap */
 457         ssize_t bytes;          /* bytes to uiomove */
 458         struct vnode *vp;
 459         int error;
 460         long oresid = uio->uio_resid;
 461 
 462 #if defined(lint)
 463         tm = tm;
 464 #endif
 465         vp = TNTOV(tp);
 466 
 467         TRACE_1(TR_FAC_TMPFS, TR_TMPFS_RWTMP_START, "tmp_rdtmp_start:vp %p",
 468             vp);
 469 
 470         ASSERT(RW_LOCK_HELD(&tp->tn_contents));
 471 
 472         if (MANDLOCK(vp, tp->tn_mode)) {
 473                 rw_exit(&tp->tn_contents);
 474                 /*
 475                  * tmp_getattr ends up being called by chklock
 476                  */
 477                 error = chklock(vp, FREAD, uio->uio_loffset, uio->uio_resid,
 478                     uio->uio_fmode, ct);
 479                 rw_enter(&tp->tn_contents, RW_READER);
 480                 if (error != 0) {
 481                         TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
 482                             "tmp_rdtmp_end:vp %p error %d", vp, error);
 483                         return (error);
 484                 }
 485         }
 486         ASSERT(tp->tn_type == VREG);
 487 
 488         if (uio->uio_loffset >= MAXOFF_T) {
 489                 TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
 490                     "tmp_rdtmp_end:vp %p error %d", vp, EINVAL);
 491                 return (0);
 492         }
 493         if (uio->uio_loffset < 0)
 494                 return (EINVAL);
 495         if (uio->uio_resid == 0) {
 496                 TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
 497                     "tmp_rdtmp_end:vp %p error %d", vp, 0);
 498                 return (0);
 499         }
 500 
 501         vp = TNTOV(tp);
 502 
 503         do {
 504                 long diff;
 505                 long offset;
 506 
 507                 offset = uio->uio_offset;
 508                 pageoffset = offset & PAGEOFFSET;
 509                 bytes = MIN(PAGESIZE - pageoffset, uio->uio_resid);
 510 
 511                 diff = tp->tn_size - offset;
 512 
 513                 if (diff <= 0) {
 514                         error = 0;
 515                         goto out;
 516                 }
 517                 if (diff < bytes)
 518                         bytes = diff;
 519 
 520                 /*
 521                  * We have to drop the contents lock to allow the VM system
 522                  * to reacquire it in tmp_getpage() should the uiomove cause a
 523                  * pagefault.
 524                  */
 525                 rw_exit(&tp->tn_contents);
 526 
 527                 if (vpm_enable) {
 528                         /*
 529                          * Copy data.
 530                          */
 531                         error = vpm_data_copy(vp, offset, bytes, uio, 1, NULL,
 532                             0, S_READ);
 533                 } else {
 534                         segmap_offset = (offset & PAGEMASK) & MAXBOFFSET;
 535                         base = segmap_getmapflt(segkmap, vp, offset & MAXBMASK,
 536                             bytes, 1, S_READ);
 537 
 538                         error = uiomove(base + segmap_offset + pageoffset,
 539                             (long)bytes, UIO_READ, uio);
 540                 }
 541 
 542                 if (error) {
 543                         if (vpm_enable) {
 544                                 (void) vpm_sync_pages(vp, offset, PAGESIZE, 0);
 545                         } else {
 546                                 (void) segmap_release(segkmap, base, 0);
 547                         }
 548                 } else {
 549                         if (vpm_enable) {
 550                                 error = vpm_sync_pages(vp, offset, PAGESIZE,
 551                                     0);
 552                         } else {
 553                                 error = segmap_release(segkmap, base, 0);
 554                         }
 555                 }
 556 
 557                 /*
 558                  * Re-acquire contents lock.
 559                  */
 560                 rw_enter(&tp->tn_contents, RW_READER);
 561 
 562         } while (error == 0 && uio->uio_resid > 0);
 563 
 564 out:
 565         gethrestime(&tp->tn_atime);
 566 
 567         /*
 568          * If we've already done a partial read, terminate
 569          * the read but return no error.
 570          */
 571         if (oresid != uio->uio_resid)
 572                 error = 0;
 573 
 574         TRACE_2(TR_FAC_TMPFS, TR_TMPFS_RWTMP_END,
 575             "tmp_rdtmp_end:vp %x error %d", vp, error);
 576         return (error);
 577 }
 578 
 579 /* ARGSUSED2 */
 580 static int
 581 tmp_read(struct vnode *vp, struct uio *uiop, int ioflag, cred_t *cred,
 582     struct caller_context *ct)
 583 {
 584         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
 585         struct tmount *tm = (struct tmount *)VTOTM(vp);
 586         int error;
 587 
 588         /*
 589          * We don't currently support reading non-regular files
 590          */
 591         if (vp->v_type == VDIR)
 592                 return (EISDIR);
 593         if (vp->v_type != VREG)
 594                 return (EINVAL);
 595         /*
 596          * tmp_rwlock should have already been called from layers above
 597          */
 598         ASSERT(RW_READ_HELD(&tp->tn_rwlock));
 599 
 600         rw_enter(&tp->tn_contents, RW_READER);
 601 
 602         error = rdtmp(tm, tp, uiop, ct);
 603 
 604         rw_exit(&tp->tn_contents);
 605 
 606         return (error);
 607 }
 608 
 609 static int
 610 tmp_write(struct vnode *vp, struct uio *uiop, int ioflag, struct cred *cred,
 611     struct caller_context *ct)
 612 {
 613         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
 614         struct tmount *tm = (struct tmount *)VTOTM(vp);
 615         int error;
 616 
 617         /*
 618          * We don't currently support writing to non-regular files
 619          */
 620         if (vp->v_type != VREG)
 621                 return (EINVAL);        /* XXX EISDIR? */
 622 
 623         /*
 624          * tmp_rwlock should have already been called from layers above
 625          */
 626         ASSERT(RW_WRITE_HELD(&tp->tn_rwlock));
 627 
 628         rw_enter(&tp->tn_contents, RW_WRITER);
 629 
 630         if (ioflag & FAPPEND) {
 631                 /*
 632                  * In append mode start at end of file.
 633                  */
 634                 uiop->uio_loffset = tp->tn_size;
 635         }
 636 
 637         error = wrtmp(tm, tp, uiop, cred, ct);
 638 
 639         rw_exit(&tp->tn_contents);
 640 
 641         return (error);
 642 }
 643 
 644 /* ARGSUSED */
 645 static int
 646 tmp_ioctl(
 647         struct vnode *vp,
 648         int com,
 649         intptr_t data,
 650         int flag,
 651         struct cred *cred,
 652         int *rvalp,
 653         caller_context_t *ct)
 654 {
 655         return (ENOTTY);
 656 }
 657 
 658 /* ARGSUSED2 */
 659 static int
 660 tmp_getattr(
 661         struct vnode *vp,
 662         struct vattr *vap,
 663         int flags,
 664         struct cred *cred,
 665         caller_context_t *ct)
 666 {
 667         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
 668         struct vnode *mvp;
 669         struct vattr va;
 670         int attrs = 1;
 671 
 672         /*
 673          * A special case to handle the root tnode on a diskless nfs
 674          * client who may have had its uid and gid inherited
 675          * from an nfs vnode with nobody ownership.  Likely the
 676          * root filesystem. After nfs is fully functional the uid/gid
 677          * may be mapable so ask again.
 678          * vfsp can't get unmounted because we hold vp.
 679          */
 680         if (vp->v_flag & VROOT &&
 681             (mvp = vp->v_vfsp->vfs_vnodecovered) != NULL) {
 682                 mutex_enter(&tp->tn_tlock);
 683                 if (tp->tn_uid == UID_NOBODY || tp->tn_gid == GID_NOBODY) {
 684                         mutex_exit(&tp->tn_tlock);
 685                         bzero(&va, sizeof (struct vattr));
 686                         va.va_mask = AT_UID|AT_GID;
 687                         attrs = VOP_GETATTR(mvp, &va, 0, cred, ct);
 688                 } else {
 689                         mutex_exit(&tp->tn_tlock);
 690                 }
 691         }
 692         mutex_enter(&tp->tn_tlock);
 693         if (attrs == 0) {
 694                 tp->tn_uid = va.va_uid;
 695                 tp->tn_gid = va.va_gid;
 696         }
 697         vap->va_type = vp->v_type;
 698         vap->va_mode = tp->tn_mode & MODEMASK;
 699         vap->va_uid = tp->tn_uid;
 700         vap->va_gid = tp->tn_gid;
 701         vap->va_fsid = tp->tn_fsid;
 702         vap->va_nodeid = (ino64_t)tp->tn_nodeid;
 703         vap->va_nlink = tp->tn_nlink;
 704         vap->va_size = (u_offset_t)tp->tn_size;
 705         vap->va_atime = tp->tn_atime;
 706         vap->va_mtime = tp->tn_mtime;
 707         vap->va_ctime = tp->tn_ctime;
 708         vap->va_blksize = PAGESIZE;
 709         vap->va_rdev = tp->tn_rdev;
 710         vap->va_seq = tp->tn_seq;
 711 
 712         /*
 713          * XXX Holes are not taken into account.  We could take the time to
 714          * run through the anon array looking for allocated slots...
 715          */
 716         vap->va_nblocks = (fsblkcnt64_t)btodb(ptob(btopr(vap->va_size)));
 717         mutex_exit(&tp->tn_tlock);
 718         return (0);
 719 }
 720 
 721 /*ARGSUSED4*/
 722 static int
 723 tmp_setattr(
 724         struct vnode *vp,
 725         struct vattr *vap,
 726         int flags,
 727         struct cred *cred,
 728         caller_context_t *ct)
 729 {
 730         struct tmount *tm = (struct tmount *)VTOTM(vp);
 731         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
 732         int error = 0;
 733         struct vattr *get;
 734         long mask;
 735 
 736         /*
 737          * Cannot set these attributes
 738          */
 739         if ((vap->va_mask & AT_NOSET) || (vap->va_mask & AT_XVATTR))
 740                 return (EINVAL);
 741 
 742         mutex_enter(&tp->tn_tlock);
 743 
 744         get = &tp->tn_attr;
 745         /*
 746          * Change file access modes. Must be owner or have sufficient
 747          * privileges.
 748          */
 749         error = secpolicy_vnode_setattr(cred, vp, vap, get, flags, tmp_taccess,
 750             tp);
 751 
 752         if (error)
 753                 goto out;
 754 
 755         mask = vap->va_mask;
 756 
 757         if (mask & AT_MODE) {
 758                 get->va_mode &= S_IFMT;
 759                 get->va_mode |= vap->va_mode & ~S_IFMT;
 760         }
 761 
 762         if (mask & AT_UID)
 763                 get->va_uid = vap->va_uid;
 764         if (mask & AT_GID)
 765                 get->va_gid = vap->va_gid;
 766         if (mask & AT_ATIME)
 767                 get->va_atime = vap->va_atime;
 768         if (mask & AT_MTIME)
 769                 get->va_mtime = vap->va_mtime;
 770 
 771         if (mask & (AT_UID | AT_GID | AT_MODE | AT_MTIME))
 772                 gethrestime(&tp->tn_ctime);
 773 
 774         if (mask & AT_SIZE) {
 775                 ASSERT(vp->v_type != VDIR);
 776 
 777                 /* Don't support large files. */
 778                 if (vap->va_size > MAXOFF_T) {
 779                         error = EFBIG;
 780                         goto out;
 781                 }
 782                 mutex_exit(&tp->tn_tlock);
 783 
 784                 rw_enter(&tp->tn_rwlock, RW_WRITER);
 785                 rw_enter(&tp->tn_contents, RW_WRITER);
 786                 error = tmpnode_trunc(tm, tp, (ulong_t)vap->va_size);
 787                 rw_exit(&tp->tn_contents);
 788                 rw_exit(&tp->tn_rwlock);
 789 
 790                 if (error == 0 && vap->va_size == 0)
 791                         vnevent_truncate(vp, ct);
 792 
 793                 goto out1;
 794         }
 795 out:
 796         mutex_exit(&tp->tn_tlock);
 797 out1:
 798         return (error);
 799 }
 800 
 801 /* ARGSUSED2 */
 802 static int
 803 tmp_access(
 804         struct vnode *vp,
 805         int mode,
 806         int flags,
 807         struct cred *cred,
 808         caller_context_t *ct)
 809 {
 810         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
 811         int error;
 812 
 813         mutex_enter(&tp->tn_tlock);
 814         error = tmp_taccess(tp, mode, cred);
 815         mutex_exit(&tp->tn_tlock);
 816         return (error);
 817 }
 818 
 819 /* ARGSUSED3 */
 820 static int
 821 tmp_lookup(
 822         struct vnode *dvp,
 823         char *nm,
 824         struct vnode **vpp,
 825         struct pathname *pnp,
 826         int flags,
 827         struct vnode *rdir,
 828         struct cred *cred,
 829         caller_context_t *ct,
 830         int *direntflags,
 831         pathname_t *realpnp)
 832 {
 833         struct tmpnode *tp = (struct tmpnode *)VTOTN(dvp);
 834         struct tmpnode *ntp = NULL;
 835         int error;
 836 
 837 
 838         /* allow cd into @ dir */
 839         if (flags & LOOKUP_XATTR) {
 840                 struct tmpnode *xdp;
 841                 struct tmount *tm;
 842 
 843                 /*
 844                  * don't allow attributes if not mounted XATTR support
 845                  */
 846                 if (!(dvp->v_vfsp->vfs_flag & VFS_XATTR))
 847                         return (EINVAL);
 848 
 849                 if (tp->tn_flags & ISXATTR)
 850                         /* No attributes on attributes */
 851                         return (EINVAL);
 852 
 853                 rw_enter(&tp->tn_rwlock, RW_WRITER);
 854                 if (tp->tn_xattrdp == NULL) {
 855                         if (!(flags & CREATE_XATTR_DIR)) {
 856                                 rw_exit(&tp->tn_rwlock);
 857                                 return (ENOENT);
 858                         }
 859 
 860                         /*
 861                          * No attribute directory exists for this
 862                          * node - create the attr dir as a side effect
 863                          * of this lookup.
 864                          */
 865 
 866                         /*
 867                          * Make sure we have adequate permission...
 868                          */
 869 
 870                         if ((error = tmp_taccess(tp, VWRITE, cred)) != 0) {
 871                                 rw_exit(&tp->tn_rwlock);
 872                                 return (error);
 873                         }
 874 
 875                         xdp = tmp_memalloc(sizeof (struct tmpnode),
 876                             TMP_MUSTHAVE);
 877                         tm = VTOTM(dvp);
 878                         tmpnode_init(tm, xdp, &tp->tn_attr, NULL);
 879                         /*
 880                          * Fix-up fields unique to attribute directories.
 881                          */
 882                         xdp->tn_flags = ISXATTR;
 883                         xdp->tn_type = VDIR;
 884                         if (tp->tn_type == VDIR) {
 885                                 xdp->tn_mode = tp->tn_attr.va_mode;
 886                         } else {
 887                                 xdp->tn_mode = 0700;
 888                                 if (tp->tn_attr.va_mode & 0040)
 889                                         xdp->tn_mode |= 0750;
 890                                 if (tp->tn_attr.va_mode & 0004)
 891                                         xdp->tn_mode |= 0705;
 892                         }
 893                         xdp->tn_vnode->v_type = VDIR;
 894                         xdp->tn_vnode->v_flag |= V_XATTRDIR;
 895                         tdirinit(tp, xdp);
 896                         tp->tn_xattrdp = xdp;
 897                 } else {
 898                         VN_HOLD(tp->tn_xattrdp->tn_vnode);
 899                 }
 900                 *vpp = TNTOV(tp->tn_xattrdp);
 901                 rw_exit(&tp->tn_rwlock);
 902                 return (0);
 903         }
 904 
 905         /*
 906          * Null component name is a synonym for directory being searched.
 907          */
 908         if (*nm == '\0') {
 909                 VN_HOLD(dvp);
 910                 *vpp = dvp;
 911                 return (0);
 912         }
 913         ASSERT(tp);
 914 
 915         error = tdirlookup(tp, nm, &ntp, cred);
 916 
 917         if (error == 0) {
 918                 ASSERT(ntp);
 919                 *vpp = TNTOV(ntp);
 920                 /*
 921                  * If vnode is a device return special vnode instead
 922                  */
 923                 if (IS_DEVVP(*vpp)) {
 924                         struct vnode *newvp;
 925 
 926                         newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
 927                             cred);
 928                         VN_RELE(*vpp);
 929                         *vpp = newvp;
 930                 }
 931         }
 932         TRACE_4(TR_FAC_TMPFS, TR_TMPFS_LOOKUP,
 933             "tmpfs lookup:vp %p name %s vpp %p error %d",
 934             dvp, nm, vpp, error);
 935         return (error);
 936 }
 937 
 938 /*ARGSUSED7*/
 939 static int
 940 tmp_create(
 941         struct vnode *dvp,
 942         char *nm,
 943         struct vattr *vap,
 944         enum vcexcl exclusive,
 945         int mode,
 946         struct vnode **vpp,
 947         struct cred *cred,
 948         int flag,
 949         caller_context_t *ct,
 950         vsecattr_t *vsecp)
 951 {
 952         struct tmpnode *parent;
 953         struct tmount *tm;
 954         struct tmpnode *self;
 955         int error;
 956         struct tmpnode *oldtp;
 957 
 958 again:
 959         parent = (struct tmpnode *)VTOTN(dvp);
 960         tm = (struct tmount *)VTOTM(dvp);
 961         self = NULL;
 962         error = 0;
 963         oldtp = NULL;
 964 
 965         /* device files not allowed in ext. attr dirs */
 966         if ((parent->tn_flags & ISXATTR) &&
 967             (vap->va_type == VBLK || vap->va_type == VCHR ||
 968             vap->va_type == VFIFO || vap->va_type == VDOOR ||
 969             vap->va_type == VSOCK || vap->va_type == VPORT))
 970                         return (EINVAL);
 971 
 972         if (vap->va_type == VREG && (vap->va_mode & VSVTX)) {
 973                 /* Must be privileged to set sticky bit */
 974                 if (secpolicy_vnode_stky_modify(cred))
 975                         vap->va_mode &= ~VSVTX;
 976         } else if (vap->va_type == VNON) {
 977                 return (EINVAL);
 978         }
 979 
 980         /*
 981          * Null component name is a synonym for directory being searched.
 982          */
 983         if (*nm == '\0') {
 984                 VN_HOLD(dvp);
 985                 oldtp = parent;
 986         } else {
 987                 error = tdirlookup(parent, nm, &oldtp, cred);
 988         }
 989 
 990         if (error == 0) {       /* name found */
 991                 boolean_t trunc = B_FALSE;
 992 
 993                 ASSERT(oldtp);
 994 
 995                 rw_enter(&oldtp->tn_rwlock, RW_WRITER);
 996 
 997                 /*
 998                  * if create/read-only an existing
 999                  * directory, allow it
1000                  */
1001                 if (exclusive == EXCL)
1002                         error = EEXIST;
1003                 else if ((oldtp->tn_type == VDIR) && (mode & VWRITE))
1004                         error = EISDIR;
1005                 else {
1006                         error = tmp_taccess(oldtp, mode, cred);
1007                 }
1008 
1009                 if (error) {
1010                         rw_exit(&oldtp->tn_rwlock);
1011                         tmpnode_rele(oldtp);
1012                         return (error);
1013                 }
1014                 *vpp = TNTOV(oldtp);
1015                 if ((*vpp)->v_type == VREG && (vap->va_mask & AT_SIZE) &&
1016                     vap->va_size == 0) {
1017                         rw_enter(&oldtp->tn_contents, RW_WRITER);
1018                         (void) tmpnode_trunc(tm, oldtp, 0);
1019                         rw_exit(&oldtp->tn_contents);
1020                         trunc = B_TRUE;
1021                 }
1022                 rw_exit(&oldtp->tn_rwlock);
1023                 if (IS_DEVVP(*vpp)) {
1024                         struct vnode *newvp;
1025 
1026                         newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type,
1027                             cred);
1028                         VN_RELE(*vpp);
1029                         if (newvp == NULL) {
1030                                 return (ENOSYS);
1031                         }
1032                         *vpp = newvp;
1033                 }
1034 
1035                 if (trunc)
1036                         vnevent_create(*vpp, ct);
1037 
1038                 return (0);
1039         }
1040 
1041         if (error != ENOENT)
1042                 return (error);
1043 
1044         rw_enter(&parent->tn_rwlock, RW_WRITER);
1045         error = tdirenter(tm, parent, nm, DE_CREATE,
1046             (struct tmpnode *)NULL, (struct tmpnode *)NULL,
1047             vap, &self, cred, ct);
1048         rw_exit(&parent->tn_rwlock);
1049 
1050         if (error) {
1051                 if (self)
1052                         tmpnode_rele(self);
1053 
1054                 if (error == EEXIST) {
1055                         /*
1056                          * This means that the file was created sometime
1057                          * after we checked and did not find it and when
1058                          * we went to create it.
1059                          * Since creat() is supposed to truncate a file
1060                          * that already exits go back to the begining
1061                          * of the function. This time we will find it
1062                          * and go down the tmp_trunc() path
1063                          */
1064                         goto again;
1065                 }
1066                 return (error);
1067         }
1068 
1069         *vpp = TNTOV(self);
1070 
1071         if (!error && IS_DEVVP(*vpp)) {
1072                 struct vnode *newvp;
1073 
1074                 newvp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cred);
1075                 VN_RELE(*vpp);
1076                 if (newvp == NULL)
1077                         return (ENOSYS);
1078                 *vpp = newvp;
1079         }
1080         TRACE_3(TR_FAC_TMPFS, TR_TMPFS_CREATE,
1081             "tmpfs create:dvp %p nm %s vpp %p", dvp, nm, vpp);
1082         return (0);
1083 }
1084 
1085 /* ARGSUSED3 */
1086 static int
1087 tmp_remove(
1088         struct vnode *dvp,
1089         char *nm,
1090         struct cred *cred,
1091         caller_context_t *ct,
1092         int flags)
1093 {
1094         struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
1095         int error;
1096         struct tmpnode *tp = NULL;
1097 
1098         error = tdirlookup(parent, nm, &tp, cred);
1099         if (error)
1100                 return (error);
1101 
1102         ASSERT(tp);
1103         rw_enter(&parent->tn_rwlock, RW_WRITER);
1104         rw_enter(&tp->tn_rwlock, RW_WRITER);
1105 
1106         if (tp->tn_type != VDIR ||
1107             (error = secpolicy_fs_linkdir(cred, dvp->v_vfsp)) == 0)
1108                 error = tdirdelete(parent, tp, nm, DR_REMOVE, cred);
1109 
1110         rw_exit(&tp->tn_rwlock);
1111         rw_exit(&parent->tn_rwlock);
1112         vnevent_remove(TNTOV(tp), dvp, nm, ct);
1113         tmpnode_rele(tp);
1114 
1115         TRACE_3(TR_FAC_TMPFS, TR_TMPFS_REMOVE,
1116             "tmpfs remove:dvp %p nm %s error %d", dvp, nm, error);
1117         return (error);
1118 }
1119 
1120 /* ARGSUSED4 */
1121 static int
1122 tmp_link(
1123         struct vnode *dvp,
1124         struct vnode *srcvp,
1125         char *tnm,
1126         struct cred *cred,
1127         caller_context_t *ct,
1128         int flags)
1129 {
1130         struct tmpnode *parent;
1131         struct tmpnode *from;
1132         struct tmount *tm = (struct tmount *)VTOTM(dvp);
1133         int error;
1134         struct tmpnode *found = NULL;
1135         struct vnode *realvp;
1136 
1137         if (VOP_REALVP(srcvp, &realvp, ct) == 0)
1138                 srcvp = realvp;
1139 
1140         parent = (struct tmpnode *)VTOTN(dvp);
1141         from = (struct tmpnode *)VTOTN(srcvp);
1142 
1143         if ((srcvp->v_type == VDIR &&
1144             secpolicy_fs_linkdir(cred, dvp->v_vfsp)) ||
1145             (from->tn_uid != crgetuid(cred) && secpolicy_basic_link(cred)))
1146                 return (EPERM);
1147 
1148         /*
1149          * Make sure link for extended attributes is valid
1150          * We only support hard linking of xattr's in xattrdir to an xattrdir
1151          */
1152         if ((from->tn_flags & ISXATTR) != (parent->tn_flags & ISXATTR))
1153                 return (EINVAL);
1154 
1155         error = tdirlookup(parent, tnm, &found, cred);
1156         if (error == 0) {
1157                 ASSERT(found);
1158                 tmpnode_rele(found);
1159                 return (EEXIST);
1160         }
1161 
1162         if (error != ENOENT)
1163                 return (error);
1164 
1165         rw_enter(&parent->tn_rwlock, RW_WRITER);
1166         error = tdirenter(tm, parent, tnm, DE_LINK, (struct tmpnode *)NULL,
1167             from, NULL, (struct tmpnode **)NULL, cred, ct);
1168         rw_exit(&parent->tn_rwlock);
1169         if (error == 0) {
1170                 vnevent_link(srcvp, ct);
1171         }
1172         return (error);
1173 }
1174 
1175 /* ARGSUSED5 */
1176 static int
1177 tmp_rename(
1178         struct vnode *odvp,     /* source parent vnode */
1179         char *onm,              /* source name */
1180         struct vnode *ndvp,     /* destination parent vnode */
1181         char *nnm,              /* destination name */
1182         struct cred *cred,
1183         caller_context_t *ct,
1184         int flags)
1185 {
1186         struct tmpnode *fromparent;
1187         struct tmpnode *toparent;
1188         struct tmpnode *fromtp = NULL;  /* source tmpnode */
1189         struct tmpnode *totp;           /* target tmpnode */
1190         struct tmount *tm = (struct tmount *)VTOTM(odvp);
1191         int error;
1192         int samedir = 0;        /* set if odvp == ndvp */
1193         struct vnode *realvp;
1194 
1195         if (VOP_REALVP(ndvp, &realvp, ct) == 0)
1196                 ndvp = realvp;
1197 
1198         fromparent = (struct tmpnode *)VTOTN(odvp);
1199         toparent = (struct tmpnode *)VTOTN(ndvp);
1200 
1201         if ((fromparent->tn_flags & ISXATTR) != (toparent->tn_flags & ISXATTR))
1202                 return (EINVAL);
1203 
1204         mutex_enter(&tm->tm_renamelck);
1205 
1206         /*
1207          * Look up tmpnode of file we're supposed to rename.
1208          */
1209         error = tdirlookup(fromparent, onm, &fromtp, cred);
1210         if (error) {
1211                 mutex_exit(&tm->tm_renamelck);
1212                 return (error);
1213         }
1214 
1215         /*
1216          * Make sure we can delete the old (source) entry.  This
1217          * requires write permission on the containing directory.  If
1218          * that directory is "sticky" it requires further checks.
1219          */
1220         if (((error = tmp_taccess(fromparent, VWRITE, cred)) != 0) ||
1221             (error = tmp_sticky_remove_access(fromparent, fromtp, cred)) != 0)
1222                 goto done;
1223 
1224         /*
1225          * Check for renaming to or from '.' or '..' or that
1226          * fromtp == fromparent
1227          */
1228         if ((onm[0] == '.' &&
1229             (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) ||
1230             (nnm[0] == '.' &&
1231             (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0'))) ||
1232             (fromparent == fromtp)) {
1233                 error = EINVAL;
1234                 goto done;
1235         }
1236 
1237         samedir = (fromparent == toparent);
1238         /*
1239          * Make sure we can search and rename into the new
1240          * (destination) directory.
1241          */
1242         if (!samedir) {
1243                 error = tmp_taccess(toparent, VEXEC|VWRITE, cred);
1244                 if (error)
1245                         goto done;
1246         }
1247 
1248         if (tdirlookup(toparent, nnm, &totp, cred) == 0) {
1249                 vnevent_pre_rename_dest(TNTOV(totp), ndvp, nnm, ct);
1250                 tmpnode_rele(totp);
1251         }
1252 
1253         /* Notify the target dir. if not the same as the source dir. */
1254         if (ndvp != odvp) {
1255                 vnevent_pre_rename_dest_dir(ndvp, TNTOV(fromtp), nnm, ct);
1256         }
1257 
1258         vnevent_pre_rename_src(TNTOV(fromtp), odvp, onm, ct);
1259 
1260         /*
1261          * Link source to new target
1262          */
1263         rw_enter(&toparent->tn_rwlock, RW_WRITER);
1264         error = tdirenter(tm, toparent, nnm, DE_RENAME,
1265             fromparent, fromtp, (struct vattr *)NULL,
1266             (struct tmpnode **)NULL, cred, ct);
1267         rw_exit(&toparent->tn_rwlock);
1268 
1269         if (error) {
1270                 /*
1271                  * ESAME isn't really an error; it indicates that the
1272                  * operation should not be done because the source and target
1273                  * are the same file, but that no error should be reported.
1274                  */
1275                 if (error == ESAME)
1276                         error = 0;
1277                 goto done;
1278         }
1279 
1280         /*
1281          * Unlink from source.
1282          */
1283         rw_enter(&fromparent->tn_rwlock, RW_WRITER);
1284         rw_enter(&fromtp->tn_rwlock, RW_WRITER);
1285 
1286         error = tdirdelete(fromparent, fromtp, onm, DR_RENAME, cred);
1287 
1288         /*
1289          * The following handles the case where our source tmpnode was
1290          * removed before we got to it.
1291          *
1292          * XXX We should also cleanup properly in the case where tdirdelete
1293          * fails for some other reason.  Currently this case shouldn't happen.
1294          * (see 1184991).
1295          */
1296         if (error == ENOENT)
1297                 error = 0;
1298 
1299         rw_exit(&fromtp->tn_rwlock);
1300         rw_exit(&fromparent->tn_rwlock);
1301 
1302         if (error == 0) {
1303                 vnevent_rename_src(TNTOV(fromtp), odvp, onm, ct);
1304                 /*
1305                  * vnevent_rename_dest is called in tdirenter().
1306                  * Notify the target dir if not same as source dir.
1307                  */
1308                 if (ndvp != odvp)
1309                         vnevent_rename_dest_dir(ndvp, ct);
1310         }
1311 
1312 done:
1313         tmpnode_rele(fromtp);
1314         mutex_exit(&tm->tm_renamelck);
1315 
1316         TRACE_5(TR_FAC_TMPFS, TR_TMPFS_RENAME,
1317             "tmpfs rename:ovp %p onm %s nvp %p nnm %s error %d", odvp, onm,
1318             ndvp, nnm, error);
1319         return (error);
1320 }
1321 
1322 /* ARGSUSED5 */
1323 static int
1324 tmp_mkdir(
1325         struct vnode *dvp,
1326         char *nm,
1327         struct vattr *va,
1328         struct vnode **vpp,
1329         struct cred *cred,
1330         caller_context_t *ct,
1331         int flags,
1332         vsecattr_t *vsecp)
1333 {
1334         struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
1335         struct tmpnode *self = NULL;
1336         struct tmount *tm = (struct tmount *)VTOTM(dvp);
1337         int error;
1338 
1339         /* no new dirs allowed in xattr dirs */
1340         if (parent->tn_flags & ISXATTR)
1341                 return (EINVAL);
1342 
1343         /*
1344          * Might be dangling directory.  Catch it here,
1345          * because a ENOENT return from tdirlookup() is
1346          * an "o.k. return".
1347          */
1348         if (parent->tn_nlink == 0)
1349                 return (ENOENT);
1350 
1351         error = tdirlookup(parent, nm, &self, cred);
1352         if (error == 0) {
1353                 ASSERT(self);
1354                 tmpnode_rele(self);
1355                 return (EEXIST);
1356         }
1357         if (error != ENOENT)
1358                 return (error);
1359 
1360         rw_enter(&parent->tn_rwlock, RW_WRITER);
1361         error = tdirenter(tm, parent, nm, DE_MKDIR, (struct tmpnode *)NULL,
1362             (struct tmpnode *)NULL, va, &self, cred, ct);
1363         if (error) {
1364                 rw_exit(&parent->tn_rwlock);
1365                 if (self)
1366                         tmpnode_rele(self);
1367                 return (error);
1368         }
1369         rw_exit(&parent->tn_rwlock);
1370         *vpp = TNTOV(self);
1371         return (0);
1372 }
1373 
1374 /* ARGSUSED4 */
1375 static int
1376 tmp_rmdir(
1377         struct vnode *dvp,
1378         char *nm,
1379         struct vnode *cdir,
1380         struct cred *cred,
1381         caller_context_t *ct,
1382         int flags)
1383 {
1384         struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
1385         struct tmpnode *self = NULL;
1386         struct vnode *vp;
1387         int error = 0;
1388 
1389         /*
1390          * Return error when removing . and ..
1391          */
1392         if (strcmp(nm, ".") == 0)
1393                 return (EINVAL);
1394         if (strcmp(nm, "..") == 0)
1395                 return (EEXIST); /* Should be ENOTEMPTY */
1396         error = tdirlookup(parent, nm, &self, cred);
1397         if (error)
1398                 return (error);
1399 
1400         rw_enter(&parent->tn_rwlock, RW_WRITER);
1401         rw_enter(&self->tn_rwlock, RW_WRITER);
1402 
1403         vp = TNTOV(self);
1404         if (vp == dvp || vp == cdir) {
1405                 error = EINVAL;
1406                 goto done1;
1407         }
1408         if (self->tn_type != VDIR) {
1409                 error = ENOTDIR;
1410                 goto done1;
1411         }
1412 
1413         mutex_enter(&self->tn_tlock);
1414         if (self->tn_nlink > 2) {
1415                 mutex_exit(&self->tn_tlock);
1416                 error = EEXIST;
1417                 goto done1;
1418         }
1419         mutex_exit(&self->tn_tlock);
1420 
1421         if (vn_vfswlock(vp)) {
1422                 error = EBUSY;
1423                 goto done1;
1424         }
1425         if (vn_mountedvfs(vp) != NULL) {
1426                 error = EBUSY;
1427                 goto done;
1428         }
1429 
1430         error = tdirdelete(parent, self, nm, DR_RMDIR, cred);
1431 done:
1432         vn_vfsunlock(vp);
1433 done1:
1434         rw_exit(&self->tn_rwlock);
1435         rw_exit(&parent->tn_rwlock);
1436         vnevent_rmdir(TNTOV(self), dvp, nm, ct);
1437         tmpnode_rele(self);
1438 
1439         return (error);
1440 }
1441 
1442 /* ARGSUSED2 */
1443 static int
1444 tmp_readdir(
1445         struct vnode *vp,
1446         struct uio *uiop,
1447         struct cred *cred,
1448         int *eofp,
1449         caller_context_t *ct,
1450         int flags)
1451 {
1452         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
1453         struct tdirent *tdp;
1454         int error = 0;
1455         size_t namelen;
1456         struct dirent64 *dp;
1457         ulong_t offset;
1458         ulong_t total_bytes_wanted;
1459         long outcount = 0;
1460         long bufsize;
1461         int reclen;
1462         caddr_t outbuf;
1463 
1464         if (uiop->uio_loffset >= MAXOFF_T) {
1465                 if (eofp)
1466                         *eofp = 1;
1467                 return (0);
1468         }
1469         /*
1470          * assuming system call has already called tmp_rwlock
1471          */
1472         ASSERT(RW_READ_HELD(&tp->tn_rwlock));
1473 
1474         if (uiop->uio_iovcnt != 1)
1475                 return (EINVAL);
1476 
1477         if (vp->v_type != VDIR)
1478                 return (ENOTDIR);
1479 
1480         /*
1481          * There's a window here where someone could have removed
1482          * all the entries in the directory after we put a hold on the
1483          * vnode but before we grabbed the rwlock.  Just return.
1484          */
1485         if (tp->tn_dir == NULL) {
1486                 if (tp->tn_nlink) {
1487                         panic("empty directory 0x%p", (void *)tp);
1488                         /*NOTREACHED*/
1489                 }
1490                 return (0);
1491         }
1492 
1493         /*
1494          * Get space for multiple directory entries
1495          */
1496         total_bytes_wanted = uiop->uio_iov->iov_len;
1497         bufsize = total_bytes_wanted + sizeof (struct dirent64);
1498         outbuf = kmem_alloc(bufsize, KM_SLEEP);
1499 
1500         dp = (struct dirent64 *)outbuf;
1501 
1502 
1503         offset = 0;
1504         tdp = tp->tn_dir;
1505         while (tdp) {
1506                 namelen = strlen(tdp->td_name);      /* no +1 needed */
1507                 offset = tdp->td_offset;
1508                 if (offset >= uiop->uio_offset) {
1509                         reclen = (int)DIRENT64_RECLEN(namelen);
1510                         if (outcount + reclen > total_bytes_wanted) {
1511                                 if (!outcount)
1512                                         /*
1513                                          * Buffer too small for any entries.
1514                                          */
1515                                         error = EINVAL;
1516                                 break;
1517                         }
1518                         ASSERT(tdp->td_tmpnode != NULL);
1519 
1520                         /* use strncpy(9f) to zero out uninitialized bytes */
1521 
1522                         (void) strncpy(dp->d_name, tdp->td_name,
1523                             DIRENT64_NAMELEN(reclen));
1524                         dp->d_reclen = (ushort_t)reclen;
1525                         dp->d_ino = (ino64_t)tdp->td_tmpnode->tn_nodeid;
1526                         dp->d_off = (offset_t)tdp->td_offset + 1;
1527                         dp = (struct dirent64 *)
1528                             ((uintptr_t)dp + dp->d_reclen);
1529                         outcount += reclen;
1530                         ASSERT(outcount <= bufsize);
1531                 }
1532                 tdp = tdp->td_next;
1533         }
1534 
1535         if (!error)
1536                 error = uiomove(outbuf, outcount, UIO_READ, uiop);
1537 
1538         if (!error) {
1539                 /* If we reached the end of the list our offset */
1540                 /* should now be just past the end. */
1541                 if (!tdp) {
1542                         offset += 1;
1543                         if (eofp)
1544                                 *eofp = 1;
1545                 } else if (eofp)
1546                         *eofp = 0;
1547                 uiop->uio_offset = offset;
1548         }
1549         gethrestime(&tp->tn_atime);
1550         kmem_free(outbuf, bufsize);
1551         return (error);
1552 }
1553 
1554 /* ARGSUSED5 */
1555 static int
1556 tmp_symlink(
1557         struct vnode *dvp,
1558         char *lnm,
1559         struct vattr *tva,
1560         char *tnm,
1561         struct cred *cred,
1562         caller_context_t *ct,
1563         int flags)
1564 {
1565         struct tmpnode *parent = (struct tmpnode *)VTOTN(dvp);
1566         struct tmpnode *self = (struct tmpnode *)NULL;
1567         struct tmount *tm = (struct tmount *)VTOTM(dvp);
1568         char *cp = NULL;
1569         int error;
1570         size_t len;
1571 
1572         /* no symlinks allowed to files in xattr dirs */
1573         if (parent->tn_flags & ISXATTR)
1574                 return (EINVAL);
1575 
1576         error = tdirlookup(parent, lnm, &self, cred);
1577         if (error == 0) {
1578                 /*
1579                  * The entry already exists
1580                  */
1581                 tmpnode_rele(self);
1582                 return (EEXIST);        /* was 0 */
1583         }
1584 
1585         if (error != ENOENT) {
1586                 if (self != NULL)
1587                         tmpnode_rele(self);
1588                 return (error);
1589         }
1590 
1591         rw_enter(&parent->tn_rwlock, RW_WRITER);
1592         error = tdirenter(tm, parent, lnm, DE_CREATE, (struct tmpnode *)NULL,
1593             (struct tmpnode *)NULL, tva, &self, cred, ct);
1594         rw_exit(&parent->tn_rwlock);
1595 
1596         if (error) {
1597                 if (self)
1598                         tmpnode_rele(self);
1599                 return (error);
1600         }
1601         len = strlen(tnm) + 1;
1602         cp = tmp_memalloc(len, 0);
1603         if (cp == NULL) {
1604                 tmpnode_rele(self);
1605                 return (ENOSPC);
1606         }
1607         (void) strcpy(cp, tnm);
1608 
1609         self->tn_symlink = cp;
1610         self->tn_size = len - 1;
1611         tmpnode_rele(self);
1612         return (error);
1613 }
1614 
1615 /* ARGSUSED2 */
1616 static int
1617 tmp_readlink(
1618         struct vnode *vp,
1619         struct uio *uiop,
1620         struct cred *cred,
1621         caller_context_t *ct)
1622 {
1623         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
1624         int error = 0;
1625 
1626         if (vp->v_type != VLNK)
1627                 return (EINVAL);
1628 
1629         rw_enter(&tp->tn_rwlock, RW_READER);
1630         rw_enter(&tp->tn_contents, RW_READER);
1631         error = uiomove(tp->tn_symlink, tp->tn_size, UIO_READ, uiop);
1632         gethrestime(&tp->tn_atime);
1633         rw_exit(&tp->tn_contents);
1634         rw_exit(&tp->tn_rwlock);
1635         return (error);
1636 }
1637 
1638 /* ARGSUSED */
1639 static int
1640 tmp_fsync(
1641         struct vnode *vp,
1642         int syncflag,
1643         struct cred *cred,
1644         caller_context_t *ct)
1645 {
1646         return (0);
1647 }
1648 
1649 /* ARGSUSED */
1650 static void
1651 tmp_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
1652 {
1653         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
1654         struct tmount *tm = (struct tmount *)VFSTOTM(vp->v_vfsp);
1655 
1656         rw_enter(&tp->tn_rwlock, RW_WRITER);
1657 top:
1658         mutex_enter(&tp->tn_tlock);
1659         mutex_enter(&vp->v_lock);
1660         ASSERT(vp->v_count >= 1);
1661 
1662         /*
1663          * If we don't have the last hold or the link count is non-zero,
1664          * there's little to do -- just drop our hold.
1665          */
1666         if (vp->v_count > 1 || tp->tn_nlink != 0) {
1667                 vp->v_count--;
1668                 mutex_exit(&vp->v_lock);
1669                 mutex_exit(&tp->tn_tlock);
1670                 rw_exit(&tp->tn_rwlock);
1671                 return;
1672         }
1673 
1674         /*
1675          * We have the last hold *and* the link count is zero, so this
1676          * tmpnode is dead from the filesystem's viewpoint.  However,
1677          * if the tmpnode has any pages associated with it (i.e. if it's
1678          * a normal file with non-zero size), the tmpnode can still be
1679          * discovered by pageout or fsflush via the page vnode pointers.
1680          * In this case we must drop all our locks, truncate the tmpnode,
1681          * and try the whole dance again.
1682          */
1683         if (tp->tn_size != 0) {
1684                 if (tp->tn_type == VREG) {
1685                         mutex_exit(&vp->v_lock);
1686                         mutex_exit(&tp->tn_tlock);
1687                         rw_enter(&tp->tn_contents, RW_WRITER);
1688                         (void) tmpnode_trunc(tm, tp, 0);
1689                         rw_exit(&tp->tn_contents);
1690                         ASSERT(tp->tn_size == 0);
1691                         ASSERT(tp->tn_nblocks == 0);
1692                         goto top;
1693                 }
1694                 if (tp->tn_type == VLNK)
1695                         tmp_memfree(tp->tn_symlink, tp->tn_size + 1);
1696         }
1697 
1698         /*
1699          * Remove normal file/dir's xattr dir and xattrs.
1700          */
1701         if (tp->tn_xattrdp) {
1702                 struct tmpnode *xtp = tp->tn_xattrdp;
1703 
1704                 ASSERT(xtp->tn_flags & ISXATTR);
1705                 tmpnode_hold(xtp);
1706                 rw_enter(&xtp->tn_rwlock, RW_WRITER);
1707                 tdirtrunc(xtp);
1708                 DECR_COUNT(&xtp->tn_nlink, &xtp->tn_tlock);
1709                 tp->tn_xattrdp = NULL;
1710                 rw_exit(&xtp->tn_rwlock);
1711                 tmpnode_rele(xtp);
1712         }
1713 
1714         mutex_exit(&vp->v_lock);
1715         mutex_exit(&tp->tn_tlock);
1716         /* Here's our chance to send invalid event while we're between locks */
1717         vn_invalid(TNTOV(tp));
1718         mutex_enter(&tm->tm_contents);
1719         if (tp->tn_forw == NULL)
1720                 tm->tm_rootnode->tn_back = tp->tn_back;
1721         else
1722                 tp->tn_forw->tn_back = tp->tn_back;
1723         tp->tn_back->tn_forw = tp->tn_forw;
1724         mutex_exit(&tm->tm_contents);
1725         rw_exit(&tp->tn_rwlock);
1726         rw_destroy(&tp->tn_rwlock);
1727         mutex_destroy(&tp->tn_tlock);
1728         vn_free(TNTOV(tp));
1729         tmp_memfree(tp, sizeof (struct tmpnode));
1730 }
1731 
1732 /* ARGSUSED2 */
1733 static int
1734 tmp_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
1735 {
1736         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
1737         struct tfid *tfid;
1738 
1739         if (fidp->fid_len < (sizeof (struct tfid) - sizeof (ushort_t))) {
1740                 fidp->fid_len = sizeof (struct tfid) - sizeof (ushort_t);
1741                 return (ENOSPC);
1742         }
1743 
1744         tfid = (struct tfid *)fidp;
1745         bzero(tfid, sizeof (struct tfid));
1746         tfid->tfid_len = (int)sizeof (struct tfid) - sizeof (ushort_t);
1747 
1748         tfid->tfid_ino = tp->tn_nodeid;
1749         tfid->tfid_gen = tp->tn_gen;
1750 
1751         return (0);
1752 }
1753 
1754 
1755 /*
1756  * Return all the pages from [off..off+len] in given file
1757  */
1758 /* ARGSUSED */
1759 static int
1760 tmp_getpage(
1761         struct vnode *vp,
1762         offset_t off,
1763         size_t len,
1764         uint_t *protp,
1765         page_t *pl[],
1766         size_t plsz,
1767         struct seg *seg,
1768         caddr_t addr,
1769         enum seg_rw rw,
1770         struct cred *cr,
1771         caller_context_t *ct)
1772 {
1773         int err = 0;
1774         struct tmpnode *tp = VTOTN(vp);
1775         anoff_t toff = (anoff_t)off;
1776         size_t tlen = len;
1777         u_offset_t tmpoff;
1778         timestruc_t now;
1779 
1780         rw_enter(&tp->tn_contents, RW_READER);
1781 
1782         if (off + len  > tp->tn_size + PAGEOFFSET) {
1783                 err = EFAULT;
1784                 goto out;
1785         }
1786         /*
1787          * Look for holes (no anon slot) in faulting range. If there are
1788          * holes we have to switch to a write lock and fill them in. Swap
1789          * space for holes was already reserved when the file was grown.
1790          */
1791         tmpoff = toff;
1792         if (non_anon(tp->tn_anon, btop(off), &tmpoff, &tlen)) {
1793                 if (!rw_tryupgrade(&tp->tn_contents)) {
1794                         rw_exit(&tp->tn_contents);
1795                         rw_enter(&tp->tn_contents, RW_WRITER);
1796                         /* Size may have changed when lock was dropped */
1797                         if (off + len  > tp->tn_size + PAGEOFFSET) {
1798                                 err = EFAULT;
1799                                 goto out;
1800                         }
1801                 }
1802                 for (toff = (anoff_t)off; toff < (anoff_t)off + len;
1803                     toff += PAGESIZE) {
1804                         if (anon_get_ptr(tp->tn_anon, btop(toff)) == NULL) {
1805                                 /* XXX - may allocate mem w. write lock held */
1806                                 (void) anon_set_ptr(tp->tn_anon, btop(toff),
1807                                     anon_alloc(vp, toff), ANON_SLEEP);
1808                                 tp->tn_nblocks++;
1809                         }
1810                 }
1811                 rw_downgrade(&tp->tn_contents);
1812         }
1813 
1814 
1815         err = pvn_getpages(tmp_getapage, vp, (u_offset_t)off, len, protp,
1816             pl, plsz, seg, addr, rw, cr);
1817 
1818         gethrestime(&now);
1819         tp->tn_atime = now;
1820         if (rw == S_WRITE)
1821                 tp->tn_mtime = now;
1822 
1823 out:
1824         rw_exit(&tp->tn_contents);
1825         return (err);
1826 }
1827 
1828 /*
1829  * Called from pvn_getpages to get a particular page.
1830  */
1831 /*ARGSUSED*/
1832 static int
1833 tmp_getapage(
1834         struct vnode *vp,
1835         u_offset_t off,
1836         size_t len,
1837         uint_t *protp,
1838         page_t *pl[],
1839         size_t plsz,
1840         struct seg *seg,
1841         caddr_t addr,
1842         enum seg_rw rw,
1843         struct cred *cr)
1844 {
1845         struct page *pp;
1846         int flags;
1847         int err = 0;
1848         struct vnode *pvp;
1849         u_offset_t poff;
1850 
1851         if (protp != NULL)
1852                 *protp = PROT_ALL;
1853 again:
1854         if (pp = page_lookup(vp, off, rw == S_CREATE ? SE_EXCL : SE_SHARED)) {
1855                 if (pl) {
1856                         pl[0] = pp;
1857                         pl[1] = NULL;
1858                 } else {
1859                         page_unlock(pp);
1860                 }
1861         } else {
1862                 pp = page_create_va(vp, off, PAGESIZE,
1863                     PG_WAIT | PG_EXCL, seg, addr);
1864                 /*
1865                  * Someone raced in and created the page after we did the
1866                  * lookup but before we did the create, so go back and
1867                  * try to look it up again.
1868                  */
1869                 if (pp == NULL)
1870                         goto again;
1871                 /*
1872                  * Fill page from backing store, if any. If none, then
1873                  * either this is a newly filled hole or page must have
1874                  * been unmodified and freed so just zero it out.
1875                  */
1876                 err = swap_getphysname(vp, off, &pvp, &poff);
1877                 if (err) {
1878                         panic("tmp_getapage: no anon slot vp %p "
1879                             "off %llx pp %p\n", (void *)vp, off, (void *)pp);
1880                 }
1881                 if (pvp) {
1882                         flags = (pl == NULL ? B_ASYNC|B_READ : B_READ);
1883                         err = VOP_PAGEIO(pvp, pp, (u_offset_t)poff, PAGESIZE,
1884                             flags, cr, NULL);
1885                         if (flags & B_ASYNC)
1886                                 pp = NULL;
1887                 } else if (rw != S_CREATE) {
1888                         pagezero(pp, 0, PAGESIZE);
1889                 }
1890                 if (err && pp)
1891                         pvn_read_done(pp, B_ERROR);
1892                 if (err == 0) {
1893                         if (pl)
1894                                 pvn_plist_init(pp, pl, plsz, off, PAGESIZE, rw);
1895                         else
1896                                 pvn_io_done(pp);
1897                 }
1898         }
1899         return (err);
1900 }
1901 
1902 
1903 /*
1904  * Flags are composed of {B_INVAL, B_DIRTY B_FREE, B_DONTNEED}.
1905  * If len == 0, do from off to EOF.
1906  */
1907 static int tmp_nopage = 0;      /* Don't do tmp_putpage's if set */
1908 
1909 /* ARGSUSED */
1910 int
1911 tmp_putpage(
1912         register struct vnode *vp,
1913         offset_t off,
1914         size_t len,
1915         int flags,
1916         struct cred *cr,
1917         caller_context_t *ct)
1918 {
1919         register page_t *pp;
1920         u_offset_t io_off;
1921         size_t io_len = 0;
1922         int err = 0;
1923         struct tmpnode *tp = VTOTN(vp);
1924         int dolock;
1925 
1926         if (tmp_nopage)
1927                 return (0);
1928 
1929         ASSERT(vp->v_count != 0);
1930 
1931         if (vp->v_flag & VNOMAP)
1932                 return (ENOSYS);
1933 
1934         /*
1935          * This being tmpfs, we don't ever do i/o unless we really
1936          * have to (when we're low on memory and pageout calls us
1937          * with B_ASYNC | B_FREE or the user explicitly asks for it with
1938          * B_DONTNEED).
1939          * XXX to approximately track the mod time like ufs we should
1940          * update the times here. The problem is, once someone does a
1941          * store we never clear the mod bit and do i/o, thus fsflush
1942          * will keep calling us every 30 seconds to do the i/o and we'll
1943          * continually update the mod time. At least we update the mod
1944          * time on the first store because this results in a call to getpage.
1945          */
1946         if (flags != (B_ASYNC | B_FREE) && (flags & B_INVAL) == 0 &&
1947             (flags & B_DONTNEED) == 0)
1948                 return (0);
1949         /*
1950          * If this thread owns the lock, i.e., this thread grabbed it
1951          * as writer somewhere above, then we don't need to grab the
1952          * lock as reader in this routine.
1953          */
1954         dolock = (rw_owner(&tp->tn_contents) != curthread);
1955 
1956         /*
1957          * If this is pageout don't block on the lock as you could deadlock
1958          * when freemem == 0 (another thread has the read lock and is blocked
1959          * creating a page, and a third thread is waiting to get the writers
1960          * lock - waiting writers priority blocks us from getting the read
1961          * lock). Of course, if the only freeable pages are on this tmpnode
1962          * we're hosed anyways. A better solution might be a new lock type.
1963          * Note: ufs has the same problem.
1964          */
1965         if (curproc == proc_pageout) {
1966                 if (!rw_tryenter(&tp->tn_contents, RW_READER))
1967                         return (ENOMEM);
1968         } else if (dolock)
1969                 rw_enter(&tp->tn_contents, RW_READER);
1970 
1971         if (!vn_has_cached_data(vp))
1972                 goto out;
1973 
1974         if (len == 0) {
1975                 if (curproc == proc_pageout) {
1976                         panic("tmp: pageout can't block");
1977                         /*NOTREACHED*/
1978                 }
1979 
1980                 /* Search the entire vp list for pages >= off. */
1981                 err = pvn_vplist_dirty(vp, (u_offset_t)off, tmp_putapage,
1982                     flags, cr);
1983         } else {
1984                 u_offset_t eoff;
1985 
1986                 /*
1987                  * Loop over all offsets in the range [off...off + len]
1988                  * looking for pages to deal with.
1989                  */
1990                 eoff = MIN(off + len, tp->tn_size);
1991                 for (io_off = off; io_off < eoff; io_off += io_len) {
1992                         /*
1993                          * If we are not invalidating, synchronously
1994                          * freeing or writing pages use the routine
1995                          * page_lookup_nowait() to prevent reclaiming
1996                          * them from the free list.
1997                          */
1998                         if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
1999                                 pp = page_lookup(vp, io_off,
2000                                     (flags & (B_INVAL | B_FREE)) ?
2001                                     SE_EXCL : SE_SHARED);
2002                         } else {
2003                                 pp = page_lookup_nowait(vp, io_off,
2004                                     (flags & B_FREE) ? SE_EXCL : SE_SHARED);
2005                         }
2006 
2007                         if (pp == NULL || pvn_getdirty(pp, flags) == 0)
2008                                 io_len = PAGESIZE;
2009                         else {
2010                                 err = tmp_putapage(vp, pp, &io_off, &io_len,
2011                                     flags, cr);
2012                                 if (err != 0)
2013                                         break;
2014                         }
2015                 }
2016         }
2017         /* If invalidating, verify all pages on vnode list are gone. */
2018         if (err == 0 && off == 0 && len == 0 &&
2019             (flags & B_INVAL) && vn_has_cached_data(vp)) {
2020                 panic("tmp_putpage: B_INVAL, pages not gone");
2021                 /*NOTREACHED*/
2022         }
2023 out:
2024         if ((curproc == proc_pageout) || dolock)
2025                 rw_exit(&tp->tn_contents);
2026         /*
2027          * Only reason putapage is going to give us SE_NOSWAP as error
2028          * is when we ask a page to be written to physical backing store
2029          * and there is none. Ignore this because we might be dealing
2030          * with a swap page which does not have any backing store
2031          * on disk. In any other case we won't get this error over here.
2032          */
2033         if (err == SE_NOSWAP)
2034                 err = 0;
2035         return (err);
2036 }
2037 
2038 long tmp_putpagecnt, tmp_pagespushed;
2039 
2040 /*
2041  * Write out a single page.
2042  * For tmpfs this means choose a physical swap slot and write the page
2043  * out using VOP_PAGEIO. For performance, we attempt to kluster; i.e.,
2044  * we try to find a bunch of other dirty pages adjacent in the file
2045  * and a bunch of contiguous swap slots, and then write all the pages
2046  * out in a single i/o.
2047  */
2048 /*ARGSUSED*/
2049 static int
2050 tmp_putapage(
2051         struct vnode *vp,
2052         page_t *pp,
2053         u_offset_t *offp,
2054         size_t *lenp,
2055         int flags,
2056         struct cred *cr)
2057 {
2058         int err;
2059         ulong_t klstart, kllen;
2060         page_t *pplist, *npplist;
2061         extern int klustsize;
2062         long tmp_klustsize;
2063         struct tmpnode *tp;
2064         size_t pp_off, pp_len;
2065         u_offset_t io_off;
2066         size_t io_len;
2067         struct vnode *pvp;
2068         u_offset_t pstart;
2069         u_offset_t offset;
2070         u_offset_t tmpoff;
2071 
2072         ASSERT(PAGE_LOCKED(pp));
2073 
2074         /* Kluster in tmp_klustsize chunks */
2075         tp = VTOTN(vp);
2076         tmp_klustsize = klustsize;
2077         offset = pp->p_offset;
2078         klstart = (offset / tmp_klustsize) * tmp_klustsize;
2079         kllen = MIN(tmp_klustsize, tp->tn_size - klstart);
2080 
2081         /* Get a kluster of pages */
2082         pplist =
2083             pvn_write_kluster(vp, pp, &tmpoff, &pp_len, klstart, kllen, flags);
2084 
2085         pp_off = (size_t)tmpoff;
2086 
2087         /*
2088          * Get a cluster of physical offsets for the pages; the amount we
2089          * get may be some subrange of what we ask for (io_off, io_len).
2090          */
2091         io_off = pp_off;
2092         io_len = pp_len;
2093         err = swap_newphysname(vp, offset, &io_off, &io_len, &pvp, &pstart);
2094         ASSERT(err != SE_NOANON); /* anon slot must have been filled */
2095         if (err) {
2096                 pvn_write_done(pplist, B_ERROR | B_WRITE | flags);
2097                 /*
2098                  * If this routine is called as a result of segvn_sync
2099                  * operation and we have no physical swap then we can get an
2100                  * error here. In such case we would return SE_NOSWAP as error.
2101                  * At this point, we expect only SE_NOSWAP.
2102                  */
2103                 ASSERT(err == SE_NOSWAP);
2104                 if (flags & B_INVAL)
2105                         err = ENOMEM;
2106                 goto out;
2107         }
2108         ASSERT(pp_off <= io_off && io_off + io_len <= pp_off + pp_len);
2109         ASSERT(io_off <= offset && offset < io_off + io_len);
2110 
2111         /* Toss pages at front/rear that we couldn't get physical backing for */
2112         if (io_off != pp_off) {
2113                 npplist = NULL;
2114                 page_list_break(&pplist, &npplist, btop(io_off - pp_off));
2115                 ASSERT(pplist->p_offset == pp_off);
2116                 ASSERT(pplist->p_prev->p_offset == io_off - PAGESIZE);
2117                 pvn_write_done(pplist, B_ERROR | B_WRITE | flags);
2118                 pplist = npplist;
2119         }
2120         if (io_off + io_len < pp_off + pp_len) {
2121                 npplist = NULL;
2122                 page_list_break(&pplist, &npplist, btop(io_len));
2123                 ASSERT(npplist->p_offset == io_off + io_len);
2124                 ASSERT(npplist->p_prev->p_offset == pp_off + pp_len - PAGESIZE);
2125                 pvn_write_done(npplist, B_ERROR | B_WRITE | flags);
2126         }
2127 
2128         ASSERT(pplist->p_offset == io_off);
2129         ASSERT(pplist->p_prev->p_offset == io_off + io_len - PAGESIZE);
2130         ASSERT(btopr(io_len) <= btopr(kllen));
2131 
2132         /* Do i/o on the remaining kluster */
2133         err = VOP_PAGEIO(pvp, pplist, (u_offset_t)pstart, io_len,
2134             B_WRITE | flags, cr, NULL);
2135 
2136         if ((flags & B_ASYNC) == 0) {
2137                 pvn_write_done(pplist, ((err) ? B_ERROR : 0) | B_WRITE | flags);
2138         }
2139 out:
2140         if (!err) {
2141                 if (offp)
2142                         *offp = io_off;
2143                 if (lenp)
2144                         *lenp = io_len;
2145                 tmp_putpagecnt++;
2146                 tmp_pagespushed += btop(io_len);
2147         }
2148         if (err && err != ENOMEM && err != SE_NOSWAP)
2149                 cmn_err(CE_WARN, "tmp_putapage: err %d\n", err);
2150         return (err);
2151 }
2152 
2153 /* ARGSUSED */
2154 static int
2155 tmp_map(
2156         struct vnode *vp,
2157         offset_t off,
2158         struct as *as,
2159         caddr_t *addrp,
2160         size_t len,
2161         uchar_t prot,
2162         uchar_t maxprot,
2163         uint_t flags,
2164         struct cred *cred,
2165         caller_context_t *ct)
2166 {
2167         struct segvn_crargs vn_a;
2168         struct tmpnode *tp = (struct tmpnode *)VTOTN(vp);
2169         int error;
2170 
2171 #ifdef _ILP32
2172         if (len > MAXOFF_T)
2173                 return (ENOMEM);
2174 #endif
2175 
2176         if (vp->v_flag & VNOMAP)
2177                 return (ENOSYS);
2178 
2179         if (off < 0 || (offset_t)(off + len) < 0 ||
2180             off > MAXOFF_T || (off + len) > MAXOFF_T)
2181                 return (ENXIO);
2182 
2183         if (vp->v_type != VREG)
2184                 return (ENODEV);
2185 
2186         /*
2187          * Don't allow mapping to locked file
2188          */
2189         if (vn_has_mandatory_locks(vp, tp->tn_mode)) {
2190                 return (EAGAIN);
2191         }
2192 
2193         as_rangelock(as);
2194         error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
2195         if (error != 0) {
2196                 as_rangeunlock(as);
2197                 return (error);
2198         }
2199 
2200         vn_a.vp = vp;
2201         vn_a.offset = (u_offset_t)off;
2202         vn_a.type = flags & MAP_TYPE;
2203         vn_a.prot = prot;
2204         vn_a.maxprot = maxprot;
2205         vn_a.flags = flags & ~MAP_TYPE;
2206         vn_a.cred = cred;
2207         vn_a.amp = NULL;
2208         vn_a.szc = 0;
2209         vn_a.lgrp_mem_policy_flags = 0;
2210 
2211         error = as_map(as, *addrp, len, segvn_create, &vn_a);
2212         as_rangeunlock(as);
2213         return (error);
2214 }
2215 
2216 /*
2217  * tmp_addmap and tmp_delmap can't be called since the vp
2218  * maintained in the segvn mapping is NULL.
2219  */
2220 /* ARGSUSED */
2221 static int
2222 tmp_addmap(
2223         struct vnode *vp,
2224         offset_t off,
2225         struct as *as,
2226         caddr_t addr,
2227         size_t len,
2228         uchar_t prot,
2229         uchar_t maxprot,
2230         uint_t flags,
2231         struct cred *cred,
2232         caller_context_t *ct)
2233 {
2234         return (0);
2235 }
2236 
2237 /* ARGSUSED */
2238 static int
2239 tmp_delmap(
2240         struct vnode *vp,
2241         offset_t off,
2242         struct as *as,
2243         caddr_t addr,
2244         size_t len,
2245         uint_t prot,
2246         uint_t maxprot,
2247         uint_t flags,
2248         struct cred *cred,
2249         caller_context_t *ct)
2250 {
2251         return (0);
2252 }
2253 
2254 static int
2255 tmp_freesp(struct vnode *vp, struct flock64 *lp, int flag)
2256 {
2257         register int i;
2258         register struct tmpnode *tp = VTOTN(vp);
2259         int error;
2260 
2261         ASSERT(vp->v_type == VREG);
2262         ASSERT(lp->l_start >= 0);
2263 
2264         if (lp->l_len != 0)
2265                 return (EINVAL);
2266 
2267         rw_enter(&tp->tn_rwlock, RW_WRITER);
2268         if (tp->tn_size == lp->l_start) {
2269                 rw_exit(&tp->tn_rwlock);
2270                 return (0);
2271         }
2272 
2273         /*
2274          * Check for any mandatory locks on the range
2275          */
2276         if (MANDLOCK(vp, tp->tn_mode)) {
2277                 long save_start;
2278 
2279                 save_start = lp->l_start;
2280 
2281                 if (tp->tn_size < lp->l_start) {
2282                         /*
2283                          * "Truncate up" case: need to make sure there
2284                          * is no lock beyond current end-of-file. To
2285                          * do so, we need to set l_start to the size
2286                          * of the file temporarily.
2287                          */
2288                         lp->l_start = tp->tn_size;
2289                 }
2290                 lp->l_type = F_WRLCK;
2291                 lp->l_sysid = 0;
2292                 lp->l_pid = ttoproc(curthread)->p_pid;
2293                 i = (flag & (FNDELAY|FNONBLOCK)) ? 0 : SLPFLCK;
2294                 if ((i = reclock(vp, lp, i, 0, lp->l_start, NULL)) != 0 ||
2295                     lp->l_type != F_UNLCK) {
2296                         rw_exit(&tp->tn_rwlock);
2297                         return (i ? i : EAGAIN);
2298                 }
2299 
2300                 lp->l_start = save_start;
2301         }
2302         VFSTOTM(vp->v_vfsp);
2303 
2304         rw_enter(&tp->tn_contents, RW_WRITER);
2305         error = tmpnode_trunc((struct tmount *)VFSTOTM(vp->v_vfsp),
2306             tp, (ulong_t)lp->l_start);
2307         rw_exit(&tp->tn_contents);
2308         rw_exit(&tp->tn_rwlock);
2309         return (error);
2310 }
2311 
2312 /* ARGSUSED */
2313 static int
2314 tmp_space(
2315         struct vnode *vp,
2316         int cmd,
2317         struct flock64 *bfp,
2318         int flag,
2319         offset_t offset,
2320         cred_t *cred,
2321         caller_context_t *ct)
2322 {
2323         int error;
2324 
2325         if (cmd != F_FREESP)
2326                 return (EINVAL);
2327         if ((error = convoff(vp, bfp, 0, (offset_t)offset)) == 0) {
2328                 if ((bfp->l_start > MAXOFF_T) || (bfp->l_len > MAXOFF_T))
2329                         return (EFBIG);
2330                 error = tmp_freesp(vp, bfp, flag);
2331 
2332                 if (error == 0 && bfp->l_start == 0)
2333                         vnevent_truncate(vp, ct);
2334         }
2335         return (error);
2336 }
2337 
2338 /* ARGSUSED */
2339 static int
2340 tmp_seek(
2341         struct vnode *vp,
2342         offset_t ooff,
2343         offset_t *noffp,
2344         caller_context_t *ct)
2345 {
2346         return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
2347 }
2348 
2349 /* ARGSUSED2 */
2350 static int
2351 tmp_rwlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
2352 {
2353         struct tmpnode *tp = VTOTN(vp);
2354 
2355         if (write_lock) {
2356                 rw_enter(&tp->tn_rwlock, RW_WRITER);
2357         } else {
2358                 rw_enter(&tp->tn_rwlock, RW_READER);
2359         }
2360         return (write_lock);
2361 }
2362 
2363 /* ARGSUSED1 */
2364 static void
2365 tmp_rwunlock(struct vnode *vp, int write_lock, caller_context_t *ctp)
2366 {
2367         struct tmpnode *tp = VTOTN(vp);
2368 
2369         rw_exit(&tp->tn_rwlock);
2370 }
2371 
2372 static int
2373 tmp_pathconf(
2374         struct vnode *vp,
2375         int cmd,
2376         ulong_t *valp,
2377         cred_t *cr,
2378         caller_context_t *ct)
2379 {
2380         struct tmpnode *tp = NULL;
2381         int error;
2382 
2383         switch (cmd) {
2384         case _PC_XATTR_EXISTS:
2385                 if (vp->v_vfsp->vfs_flag & VFS_XATTR) {
2386                         *valp = 0;      /* assume no attributes */
2387                         error = 0;      /* okay to ask */
2388                         tp = VTOTN(vp);
2389                         rw_enter(&tp->tn_rwlock, RW_READER);
2390                         if (tp->tn_xattrdp) {
2391                                 rw_enter(&tp->tn_xattrdp->tn_rwlock, RW_READER);
2392                                 /* do not count "." and ".." */
2393                                 if (tp->tn_xattrdp->tn_dirents > 2)
2394                                         *valp = 1;
2395                                 rw_exit(&tp->tn_xattrdp->tn_rwlock);
2396                         }
2397                         rw_exit(&tp->tn_rwlock);
2398                 } else {
2399                         error = EINVAL;
2400                 }
2401                 break;
2402         case _PC_SATTR_ENABLED:
2403         case _PC_SATTR_EXISTS:
2404                 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2405                     (vp->v_type == VREG || vp->v_type == VDIR);
2406                 error = 0;
2407                 break;
2408         case _PC_TIMESTAMP_RESOLUTION:
2409                 /* nanosecond timestamp resolution */
2410                 *valp = 1L;
2411                 error = 0;
2412                 break;
2413         default:
2414                 error = fs_pathconf(vp, cmd, valp, cr, ct);
2415         }
2416         return (error);
2417 }
2418 
2419 
2420 struct vnodeops *tmp_vnodeops;
2421 
2422 const fs_operation_def_t tmp_vnodeops_template[] = {
2423         VOPNAME_OPEN,           { .vop_open = tmp_open },
2424         VOPNAME_CLOSE,          { .vop_close = tmp_close },
2425         VOPNAME_READ,           { .vop_read = tmp_read },
2426         VOPNAME_WRITE,          { .vop_write = tmp_write },
2427         VOPNAME_IOCTL,          { .vop_ioctl = tmp_ioctl },
2428         VOPNAME_GETATTR,        { .vop_getattr = tmp_getattr },
2429         VOPNAME_SETATTR,        { .vop_setattr = tmp_setattr },
2430         VOPNAME_ACCESS,         { .vop_access = tmp_access },
2431         VOPNAME_LOOKUP,         { .vop_lookup = tmp_lookup },
2432         VOPNAME_CREATE,         { .vop_create = tmp_create },
2433         VOPNAME_REMOVE,         { .vop_remove = tmp_remove },
2434         VOPNAME_LINK,           { .vop_link = tmp_link },
2435         VOPNAME_RENAME,         { .vop_rename = tmp_rename },
2436         VOPNAME_MKDIR,          { .vop_mkdir = tmp_mkdir },
2437         VOPNAME_RMDIR,          { .vop_rmdir = tmp_rmdir },
2438         VOPNAME_READDIR,        { .vop_readdir = tmp_readdir },
2439         VOPNAME_SYMLINK,        { .vop_symlink = tmp_symlink },
2440         VOPNAME_READLINK,       { .vop_readlink = tmp_readlink },
2441         VOPNAME_FSYNC,          { .vop_fsync = tmp_fsync },
2442         VOPNAME_INACTIVE,       { .vop_inactive = tmp_inactive },
2443         VOPNAME_FID,            { .vop_fid = tmp_fid },
2444         VOPNAME_RWLOCK,         { .vop_rwlock = tmp_rwlock },
2445         VOPNAME_RWUNLOCK,       { .vop_rwunlock = tmp_rwunlock },
2446         VOPNAME_SEEK,           { .vop_seek = tmp_seek },
2447         VOPNAME_SPACE,          { .vop_space = tmp_space },
2448         VOPNAME_GETPAGE,        { .vop_getpage = tmp_getpage },
2449         VOPNAME_PUTPAGE,        { .vop_putpage = tmp_putpage },
2450         VOPNAME_MAP,            { .vop_map = tmp_map },
2451         VOPNAME_ADDMAP,         { .vop_addmap = tmp_addmap },
2452         VOPNAME_DELMAP,         { .vop_delmap = tmp_delmap },
2453         VOPNAME_PATHCONF,       { .vop_pathconf = tmp_pathconf },
2454         VOPNAME_VNEVENT,        { .vop_vnevent = fs_vnevent_support },
2455         NULL,                   NULL
2456 };