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