1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 27 /* All Rights Reserved */ 28 29 /* 30 * University Copyright- Copyright (c) 1982, 1986, 1988 31 * The Regents of the University of California 32 * All Rights Reserved 33 * 34 * University Acknowledgment- Portions of this document are derived from 35 * software developed by the University of California, Berkeley, and its 36 * contributors. 37 */ 38 39 40 #pragma ident "%Z%%M% %I% %E% SMI" 41 42 #include <sys/types.h> 43 #include <sys/thread.h> 44 #include <sys/t_lock.h> 45 #include <sys/param.h> 46 #include <sys/systm.h> 47 #include <sys/bitmap.h> 48 #include <sys/buf.h> 49 #include <sys/cmn_err.h> 50 #include <sys/conf.h> 51 #include <sys/ddi.h> 52 #include <sys/debug.h> 53 #include <sys/errno.h> 54 #include <sys/time.h> 55 #include <sys/fcntl.h> 56 #include <sys/flock.h> 57 #include <sys/file.h> 58 #include <sys/kmem.h> 59 #include <sys/mman.h> 60 #include <sys/open.h> 61 #include <sys/swap.h> 62 #include <sys/sysmacros.h> 63 #include <sys/uio.h> 64 #include <sys/vfs.h> 65 #include <sys/vnode.h> 66 #include <sys/stat.h> 67 #include <sys/poll.h> 68 #include <sys/stream.h> 69 #include <sys/strsubr.h> 70 #include <sys/policy.h> 71 #include <sys/devpolicy.h> 72 73 #include <sys/proc.h> 74 #include <sys/user.h> 75 #include <sys/session.h> 76 #include <sys/vmsystm.h> 77 #include <sys/vtrace.h> 78 #include <sys/pathname.h> 79 80 #include <sys/fs/snode.h> 81 82 #include <vm/seg.h> 83 #include <vm/seg_map.h> 84 #include <vm/page.h> 85 #include <vm/pvn.h> 86 #include <vm/seg_dev.h> 87 #include <vm/seg_vn.h> 88 89 #include <fs/fs_subr.h> 90 91 #include <sys/esunddi.h> 92 #include <sys/autoconf.h> 93 #include <sys/sunndi.h> 94 95 96 static int spec_open(struct vnode **, int, struct cred *); 97 static int spec_close(struct vnode *, int, int, offset_t, struct cred *); 98 static int spec_read(struct vnode *, struct uio *, int, struct cred *, 99 struct caller_context *); 100 static int spec_write(struct vnode *, struct uio *, int, struct cred *, 101 struct caller_context *); 102 static int spec_ioctl(struct vnode *, int, intptr_t, int, struct cred *, int *); 103 static int spec_getattr(struct vnode *, struct vattr *, int, struct cred *); 104 static int spec_setattr(struct vnode *, struct vattr *, int, struct cred *, 105 caller_context_t *); 106 static int spec_access(struct vnode *, int, int, struct cred *); 107 static int spec_create(struct vnode *, char *, vattr_t *, enum vcexcl, 108 int, struct vnode **, struct cred *, int); 109 static int spec_fsync(struct vnode *, int, struct cred *); 110 static void spec_inactive(struct vnode *, struct cred *); 111 static int spec_fid(struct vnode *, struct fid *); 112 static int spec_seek(struct vnode *, offset_t, offset_t *); 113 static int spec_frlock(struct vnode *, int, struct flock64 *, int, offset_t, 114 struct flk_callback *, struct cred *); 115 static int spec_realvp(struct vnode *, struct vnode **); 116 117 static int spec_getpage(struct vnode *, offset_t, size_t, uint_t *, page_t **, 118 size_t, struct seg *, caddr_t, enum seg_rw, struct cred *); 119 static int spec_putapage(struct vnode *, page_t *, u_offset_t *, size_t *, int, 120 struct cred *); 121 static struct buf *spec_startio(struct vnode *, page_t *, u_offset_t, size_t, 122 int); 123 static int spec_getapage(struct vnode *, u_offset_t, size_t, uint_t *, 124 page_t **, size_t, struct seg *, caddr_t, enum seg_rw, struct cred *); 125 static int spec_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t, 126 uchar_t, uchar_t, uint_t, struct cred *); 127 static int spec_addmap(struct vnode *, offset_t, struct as *, caddr_t, size_t, 128 uchar_t, uchar_t, uint_t, struct cred *); 129 static int spec_delmap(struct vnode *, offset_t, struct as *, caddr_t, size_t, 130 uint_t, uint_t, uint_t, struct cred *); 131 132 static int spec_poll(struct vnode *, short, int, short *, struct pollhead **); 133 static int spec_dump(struct vnode *, caddr_t, int, int); 134 static int spec_pageio(struct vnode *, page_t *, u_offset_t, size_t, int, 135 cred_t *); 136 137 static int spec_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *); 138 static int spec_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *); 139 static int spec_pathconf(struct vnode *, int, ulong_t *, struct cred *); 140 141 #define SN_HOLD(csp) { \ 142 mutex_enter(&csp->s_lock); \ 143 csp->s_count++; \ 144 mutex_exit(&csp->s_lock); \ 145 } 146 147 #define SN_RELE(csp) { \ 148 mutex_enter(&csp->s_lock); \ 149 csp->s_count--; \ 150 ASSERT((csp->s_count > 0) || (csp->s_vnode->v_stream == NULL)); \ 151 mutex_exit(&csp->s_lock); \ 152 } 153 154 struct vnodeops *spec_vnodeops; 155 156 const fs_operation_def_t spec_vnodeops_template[] = { 157 VOPNAME_OPEN, spec_open, 158 VOPNAME_CLOSE, spec_close, 159 VOPNAME_READ, spec_read, 160 VOPNAME_WRITE, spec_write, 161 VOPNAME_IOCTL, spec_ioctl, 162 VOPNAME_GETATTR, spec_getattr, 163 VOPNAME_SETATTR, spec_setattr, 164 VOPNAME_ACCESS, spec_access, 165 VOPNAME_CREATE, spec_create, 166 VOPNAME_FSYNC, spec_fsync, 167 VOPNAME_INACTIVE, (fs_generic_func_p) spec_inactive, 168 VOPNAME_FID, spec_fid, 169 VOPNAME_SEEK, spec_seek, 170 VOPNAME_PATHCONF, spec_pathconf, 171 VOPNAME_FRLOCK, spec_frlock, 172 VOPNAME_REALVP, spec_realvp, 173 VOPNAME_GETPAGE, spec_getpage, 174 VOPNAME_PUTPAGE, spec_putpage, 175 VOPNAME_MAP, (fs_generic_func_p) spec_map, 176 VOPNAME_ADDMAP, (fs_generic_func_p) spec_addmap, 177 VOPNAME_DELMAP, spec_delmap, 178 VOPNAME_POLL, (fs_generic_func_p) spec_poll, 179 VOPNAME_DUMP, spec_dump, 180 VOPNAME_PAGEIO, spec_pageio, 181 VOPNAME_SETSECATTR, spec_setsecattr, 182 VOPNAME_GETSECATTR, spec_getsecattr, 183 NULL, NULL 184 }; 185 186 /* 187 * Return address of spec_vnodeops 188 */ 189 struct vnodeops * 190 spec_getvnodeops(void) 191 { 192 return (spec_vnodeops); 193 } 194 195 extern vnode_t *rconsvp; 196 197 /* 198 * Acquire the serial lock on the common snode. 199 */ 200 #define LOCK_CSP(csp) \ 201 mutex_enter(&csp->s_lock); \ 202 while (csp->s_flag & SLOCKED) { \ 203 csp->s_flag |= SWANT; \ 204 cv_wait(&csp->s_cv, &csp->s_lock); \ 205 } \ 206 csp->s_flag |= SLOCKED; \ 207 mutex_exit(&csp->s_lock); 208 209 #define LOCK_CSP_SIG(csp) lock_csp_sig(csp) 210 211 /* 212 * Acquire the serial lock on the common snode checking for a signal. 213 * cv_wait_sig is used to allow signals to pull us out. 214 * Return 1 if locked, 0 if interrupted 215 */ 216 static int 217 lock_csp_sig(struct snode *csp) 218 { 219 mutex_enter(&csp->s_lock); 220 while (csp->s_flag & SLOCKED) { 221 csp->s_flag |= SWANT; 222 if (!cv_wait_sig(&csp->s_cv, &csp->s_lock)) { 223 mutex_exit(&csp->s_lock); 224 /* interrupted */ 225 return (0); 226 } 227 } 228 csp->s_flag |= SLOCKED; 229 mutex_exit(&csp->s_lock); 230 231 return (1); 232 } 233 234 /* 235 * Unlock the serial lock on the common snode 236 */ 237 #define UNLOCK_CSP_LOCK_HELD(csp) \ 238 ASSERT(mutex_owned(&csp->s_lock)); \ 239 if (csp->s_flag & SWANT) \ 240 cv_broadcast(&csp->s_cv); \ 241 csp->s_flag &= ~(SWANT|SLOCKED); 242 243 #define UNLOCK_CSP(csp) \ 244 mutex_enter(&csp->s_lock); \ 245 UNLOCK_CSP_LOCK_HELD(csp); \ 246 mutex_exit(&csp->s_lock); 247 248 /* 249 * compute/return the size of the device 250 */ 251 #define SPEC_SIZE(csp) \ 252 (((csp)->s_flag & SSIZEVALID) ? (csp)->s_size : spec_size(csp)) 253 254 /* 255 * Compute and return the size. If the size in the common snode is valid then 256 * return it. If not valid then get the size from the driver and set size in 257 * the common snode. If the device has not been attached then we don't ask for 258 * an update from the driver- for non-streams SSIZEVALID stays unset until the 259 * device is attached. A stat of a mknod outside /devices (non-devfs) may 260 * report UNKNOWN_SIZE because the device may not be attached yet (SDIPSET not 261 * established in mknod until open time). An stat in /devices will report the 262 * size correctly. Specfs should always call SPEC_SIZE instead of referring 263 * directly to s_size to initialize/retrieve the size of a device. 264 * 265 * XXX There is an inconsistency between block and raw - "unknown" is 266 * UNKNOWN_SIZE for VBLK and 0 for VCHR(raw). 267 */ 268 static u_offset_t 269 spec_size(struct snode *csp) 270 { 271 struct vnode *cvp = STOV(csp); 272 u_offset_t size; 273 int plen; 274 uint32_t size32; 275 dev_t dev; 276 dev_info_t *devi; 277 major_t maj; 278 279 ASSERT((csp)->s_commonvp == cvp); /* must be common node */ 280 281 /* return cached value */ 282 mutex_enter(&csp->s_lock); 283 if (csp->s_flag & SSIZEVALID) { 284 mutex_exit(&csp->s_lock); 285 return (csp->s_size); 286 } 287 288 /* VOP_GETATTR of mknod has not had devcnt restriction applied */ 289 dev = cvp->v_rdev; 290 maj = getmajor(dev); 291 if (maj >= devcnt) { 292 /* return non-cached UNKNOWN_SIZE */ 293 mutex_exit(&csp->s_lock); 294 return ((cvp->v_type == VCHR) ? 0 : UNKNOWN_SIZE); 295 } 296 297 /* establish cached zero size for streams */ 298 if (STREAMSTAB(maj)) { 299 csp->s_size = 0; 300 csp->s_flag |= SSIZEVALID; 301 mutex_exit(&csp->s_lock); 302 return (0); 303 } 304 305 /* 306 * Return non-cached UNKNOWN_SIZE if not open. 307 * 308 * NB: This check is bogus, calling prop_op(9E) should be gated by 309 * attach, not open. Not having this check however opens up a new 310 * context under which a driver's prop_op(9E) could be called. Calling 311 * prop_op(9E) in this new context has been shown to expose latent 312 * driver bugs (insufficient NULL pointer checks that lead to panic). 313 * We are keeping this open check for now to avoid these panics. 314 */ 315 if (csp->s_count == 0) { 316 mutex_exit(&csp->s_lock); 317 return ((cvp->v_type == VCHR) ? 0 : UNKNOWN_SIZE); 318 } 319 320 /* Return non-cached UNKNOWN_SIZE if not attached. */ 321 if (((csp->s_flag & SDIPSET) == 0) || (csp->s_dip == NULL) || 322 !i_ddi_devi_attached(csp->s_dip)) { 323 mutex_exit(&csp->s_lock); 324 return ((cvp->v_type == VCHR) ? 0 : UNKNOWN_SIZE); 325 } 326 327 devi = csp->s_dip; 328 329 /* 330 * Established cached size obtained from the attached driver. Since we 331 * know the devinfo node, for efficiency we use cdev_prop_op directly 332 * instead of [cb]dev_[Ss]size. 333 */ 334 if (cvp->v_type == VCHR) { 335 size = 0; 336 plen = sizeof (size); 337 if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF, 338 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS | 339 DDI_PROP_CONSUMER_TYPED, "Size", (caddr_t)&size, 340 &plen) != DDI_PROP_SUCCESS) { 341 plen = sizeof (size32); 342 if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF, 343 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 344 "size", (caddr_t)&size32, &plen) == 345 DDI_PROP_SUCCESS) 346 size = size32; 347 } 348 } else { 349 size = UNKNOWN_SIZE; 350 plen = sizeof (size); 351 if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF, 352 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS | 353 DDI_PROP_CONSUMER_TYPED, "Nblocks", (caddr_t)&size, 354 &plen) != DDI_PROP_SUCCESS) { 355 plen = sizeof (size32); 356 if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF, 357 DDI_PROP_NOTPROM | DDI_PROP_DONTPASS, 358 "nblocks", (caddr_t)&size32, &plen) == 359 DDI_PROP_SUCCESS) 360 size = size32; 361 } 362 363 if (size != UNKNOWN_SIZE) { 364 /* convert from block size to byte size */ 365 if (size < (MAXOFFSET_T >> DEV_BSHIFT)) 366 size = size << DEV_BSHIFT; 367 else 368 size = UNKNOWN_SIZE; 369 } 370 } 371 372 csp->s_size = size; 373 csp->s_flag |= SSIZEVALID; 374 375 mutex_exit(&csp->s_lock); 376 return (size); 377 } 378 379 /* 380 * This function deal with vnode substitution in the case of 381 * device cloning. 382 */ 383 static int 384 spec_clone(struct vnode **vpp, dev_t newdev, int vtype, struct stdata *stp) 385 { 386 dev_t dev = (*vpp)->v_rdev; 387 major_t maj = getmajor(dev); 388 major_t newmaj = getmajor(newdev); 389 int sysclone = (maj == clone_major); 390 int qassociate_used = 0; 391 struct snode *oldsp, *oldcsp; 392 struct snode *newsp, *newcsp; 393 struct vnode *newvp, *newcvp; 394 dev_info_t *dip; 395 queue_t *dq; 396 397 ASSERT(dev != newdev); 398 399 /* 400 * Check for cloning across different drivers. 401 * We only support this under the system provided clone driver 402 */ 403 if ((maj != newmaj) && !sysclone) { 404 cmn_err(CE_NOTE, 405 "unsupported clone open maj = %u, newmaj = %u", 406 maj, newmaj); 407 return (ENXIO); 408 } 409 410 /* old */ 411 oldsp = VTOS(*vpp); 412 oldcsp = VTOS(oldsp->s_commonvp); 413 414 /* new */ 415 newvp = makespecvp(newdev, vtype); 416 ASSERT(newvp != NULL); 417 newsp = VTOS(newvp); 418 newcvp = newsp->s_commonvp; 419 newcsp = VTOS(newcvp); 420 421 /* 422 * Clones inherit fsid, realvp, and dip. 423 * XXX realvp inherit is not occurring, does fstat of clone work? 424 */ 425 newsp->s_fsid = oldsp->s_fsid; 426 if (sysclone) { 427 newsp->s_flag |= SCLONE; 428 dip = NULL; 429 } else { 430 newsp->s_flag |= SSELFCLONE; 431 dip = oldcsp->s_dip; 432 } 433 434 /* 435 * If we cloned to an opened newdev that already has called 436 * spec_assoc_vp_with_devi (SDIPSET set) then the association is 437 * already established. 438 */ 439 if (!(newcsp->s_flag & SDIPSET)) { 440 /* 441 * Establish s_dip association for newdev. 442 * 443 * If we trusted the getinfo(9E) DDI_INFO_DEVT2INSTANCE 444 * implementation of all cloning drivers (SCLONE and SELFCLONE) 445 * we would always use e_ddi_hold_devi_by_dev(). We know that 446 * many drivers have had (still have?) problems with 447 * DDI_INFO_DEVT2INSTANCE, so we try to minimize reliance by 448 * detecting drivers that use QASSOCIATE (by looking down the 449 * stream) and setting their s_dip association to NULL. 450 */ 451 qassociate_used = 0; 452 if (stp) { 453 for (dq = stp->sd_wrq; dq; dq = dq->q_next) { 454 if (_RD(dq)->q_flag & _QASSOCIATED) { 455 qassociate_used = 1; 456 dip = NULL; 457 break; 458 } 459 } 460 } 461 462 if (dip || qassociate_used) { 463 spec_assoc_vp_with_devi(newvp, dip); 464 } else { 465 /* derive association from newdev */ 466 dip = e_ddi_hold_devi_by_dev(newdev, 0); 467 spec_assoc_vp_with_devi(newvp, dip); 468 if (dip) 469 ddi_release_devi(dip); 470 } 471 } 472 473 SN_HOLD(newcsp); 474 475 /* deal with stream stuff */ 476 if (stp != NULL) { 477 LOCK_CSP(newcsp); /* synchronize stream open/close */ 478 mutex_enter(&newcsp->s_lock); 479 newcvp->v_stream = newvp->v_stream = stp; 480 stp->sd_vnode = newcvp; 481 stp->sd_strtab = STREAMSTAB(newmaj); 482 mutex_exit(&newcsp->s_lock); 483 UNLOCK_CSP(newcsp); 484 } 485 486 /* substitute the vnode */ 487 SN_RELE(oldcsp); 488 VN_RELE(*vpp); 489 *vpp = newvp; 490 491 return (0); 492 } 493 494 static int 495 spec_open(struct vnode **vpp, int flag, struct cred *cr) 496 { 497 major_t maj; 498 dev_t dev, newdev; 499 struct vnode *vp, *cvp; 500 struct snode *sp, *csp; 501 struct stdata *stp; 502 dev_info_t *dip; 503 int error, type; 504 505 flag &= ~FCREAT; /* paranoia */ 506 507 vp = *vpp; 508 sp = VTOS(vp); 509 ASSERT((vp->v_type == VCHR) || (vp->v_type == VBLK)); 510 if ((vp->v_type != VCHR) && (vp->v_type != VBLK)) 511 return (ENXIO); 512 513 /* 514 * If the VFS_NODEVICES bit was set for the mount, 515 * do not allow opens of special devices. 516 */ 517 if (sp->s_realvp && (sp->s_realvp->v_vfsp->vfs_flag & VFS_NODEVICES)) 518 return (ENXIO); 519 520 newdev = dev = vp->v_rdev; 521 522 /* 523 * If we are opening a node that has not had spec_assoc_vp_with_devi 524 * called against it (mknod outside /devices or a non-dacf makespecvp 525 * node) then SDIPSET will not be set. In this case we call an 526 * interface which will reconstruct the path and lookup (drive attach) 527 * through devfs (e_ddi_hold_devi_by_dev -> e_ddi_hold_devi_by_path -> 528 * devfs_lookupname). For support of broken drivers that don't call 529 * ddi_create_minor_node for all minor nodes in their instance space, 530 * we call interfaces that operates at the directory/devinfo 531 * (major/instance) level instead of to the leaf/minor node level. 532 * After finding and attaching the dip we associate it with the 533 * common specfs vnode (s_dip), which sets SDIPSET. A DL_DETACH_REQ 534 * to style-2 stream driver may set s_dip to NULL with SDIPSET set. 535 * 536 * NOTE: Although e_ddi_hold_devi_by_dev takes a dev_t argument, its 537 * implementation operates at the major/instance level since it only 538 * need to return a dip. 539 */ 540 cvp = sp->s_commonvp; 541 csp = VTOS(cvp); 542 if (!(csp->s_flag & SDIPSET)) { 543 /* try to attach, return error if we fail */ 544 if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL) 545 return (ENXIO); 546 547 /* associate dip with the common snode s_dip */ 548 spec_assoc_vp_with_devi(vp, dip); 549 ddi_release_devi(dip); /* from e_ddi_hold_devi_by_dev */ 550 } 551 552 #ifdef DEBUG 553 /* verify attach/open exclusion guarantee */ 554 dip = csp->s_dip; 555 ASSERT((dip == NULL) || i_ddi_devi_attached(dip)); 556 #endif /* DEBUG */ 557 558 if ((error = secpolicy_spec_open(cr, cvp, flag)) != 0) 559 return (error); 560 561 maj = getmajor(dev); 562 if (STREAMSTAB(maj)) 563 goto streams_open; 564 565 SN_HOLD(csp); /* increment open count */ 566 567 /* non streams open */ 568 type = (vp->v_type == VBLK ? OTYP_BLK : OTYP_CHR); 569 error = dev_open(&newdev, flag, type, cr); 570 571 /* deal with clone case */ 572 if (error == 0 && dev != newdev) { 573 error = spec_clone(vpp, newdev, vp->v_type, NULL); 574 /* 575 * bail on clone failure, further processing 576 * results in undefined behaviors. 577 */ 578 if (error != 0) 579 return (error); 580 sp = VTOS(*vpp); 581 csp = VTOS(sp->s_commonvp); 582 } 583 584 if (error == 0) { 585 sp->s_size = SPEC_SIZE(csp); 586 587 if ((csp->s_flag & SNEEDCLOSE) == 0) { 588 int nmaj = getmajor(newdev); 589 mutex_enter(&csp->s_lock); 590 /* successful open needs a close later */ 591 csp->s_flag |= SNEEDCLOSE; 592 593 /* 594 * Invalidate possible cached "unknown" size 595 * established by a VOP_GETATTR while open was in 596 * progress, and the driver might fail prop_op(9E). 597 */ 598 if (((cvp->v_type == VCHR) && (csp->s_size == 0)) || 599 ((cvp->v_type == VBLK) && 600 (csp->s_size == UNKNOWN_SIZE))) 601 csp->s_flag &= ~SSIZEVALID; 602 603 if (devopsp[nmaj]->devo_cb_ops->cb_flag & D_64BIT) 604 csp->s_flag |= SLOFFSET; 605 if (devopsp[nmaj]->devo_cb_ops->cb_flag & D_U64BIT) 606 csp->s_flag |= SLOFFSET | SANYOFFSET; 607 mutex_exit(&csp->s_lock); 608 } 609 return (0); 610 } 611 612 /* 613 * Open failed. If we missed a close operation because 614 * we were trying to get the device open and it is the 615 * last in progress open that is failing then call close. 616 * 617 * NOTE: Only non-streams open has this race condition. 618 */ 619 mutex_enter(&csp->s_lock); 620 csp->s_count--; /* decrement open count : SN_RELE */ 621 if ((csp->s_count == 0) && /* no outstanding open */ 622 (csp->s_mapcnt == 0) && /* no mapping */ 623 (csp->s_flag & SNEEDCLOSE)) { /* need a close */ 624 csp->s_flag &= ~(SNEEDCLOSE | SSIZEVALID); 625 626 /* See comment in spec_close() */ 627 if (csp->s_flag & (SCLONE | SSELFCLONE)) 628 csp->s_flag &= ~SDIPSET; 629 630 mutex_exit(&csp->s_lock); 631 ASSERT(*vpp != NULL); 632 (void) device_close(*vpp, flag, cr); 633 } else { 634 mutex_exit(&csp->s_lock); 635 } 636 return (error); 637 638 streams_open: 639 if (vp->v_type != VCHR) 640 return (ENXIO); 641 642 /* 643 * Lock common snode to prevent any new clone opens 644 * on this stream while one is in progress. 645 * This is necessary since the stream currently 646 * associated with the clone device will not be part 647 * of it after the clone open completes. 648 * Unfortunately we don't know in advance if this is 649 * a clone device so we have to lock all opens. 650 * 651 * If we fail, it's because of an interrupt. 652 */ 653 if (LOCK_CSP_SIG(csp) == 0) 654 return (EINTR); 655 656 SN_HOLD(csp); /* increment open count */ 657 658 error = stropen(cvp, &newdev, flag, cr); 659 stp = cvp->v_stream; 660 661 /* deal with the clone case */ 662 if ((error == 0) && (dev != newdev)) { 663 vp->v_stream = cvp->v_stream = NULL; 664 UNLOCK_CSP(csp); 665 error = spec_clone(vpp, newdev, vp->v_type, stp); 666 /* 667 * bail on clone failure, further processing 668 * results in undefined behaviors. 669 */ 670 if (error != 0) 671 return (error); 672 sp = VTOS(*vpp); 673 csp = VTOS(sp->s_commonvp); 674 } else if (error == 0) { 675 vp->v_stream = stp; 676 UNLOCK_CSP(csp); 677 } 678 679 if (error == 0) { 680 /* STREAMS devices don't have a size */ 681 sp->s_size = csp->s_size = 0; 682 683 /* 684 * try to allocate it as a controlling terminal 685 */ 686 if ((stp->sd_flag & STRISTTY) && !(flag & FNOCTTY)) 687 stralloctty(stp); 688 689 return (0); 690 } 691 692 /* 693 * Deal with stropen failure. 694 * 695 * sd_flag in the stream head cannot change since the 696 * common snode is locked before the call to stropen(). 697 */ 698 if ((stp != NULL) && (stp->sd_flag & STREOPENFAIL)) { 699 /* 700 * Open failed part way through. 701 */ 702 mutex_enter(&stp->sd_lock); 703 stp->sd_flag &= ~STREOPENFAIL; 704 mutex_exit(&stp->sd_lock); 705 706 UNLOCK_CSP(csp); 707 (void) spec_close(vp, flag, 1, 0, cr); 708 } else { 709 UNLOCK_CSP(csp); 710 SN_RELE(csp); 711 } 712 713 return (error); 714 } 715 716 /*ARGSUSED2*/ 717 static int 718 spec_close( 719 struct vnode *vp, 720 int flag, 721 int count, 722 offset_t offset, 723 struct cred *cr) 724 { 725 struct vnode *cvp; 726 struct snode *sp, *csp; 727 enum vtype type; 728 dev_t dev; 729 int error = 0; 730 int sysclone; 731 732 if (!(flag & FKLYR)) { 733 /* this only applies to closes of devices from userland */ 734 cleanlocks(vp, ttoproc(curthread)->p_pid, 0); 735 cleanshares(vp, ttoproc(curthread)->p_pid); 736 if (vp->v_stream) 737 strclean(vp); 738 } 739 if (count > 1) 740 return (0); 741 742 sp = VTOS(vp); 743 cvp = sp->s_commonvp; 744 745 dev = sp->s_dev; 746 type = vp->v_type; 747 748 ASSERT(type == VCHR || type == VBLK); 749 750 /* 751 * Prevent close/close and close/open races by serializing closes 752 * on this common snode. Clone opens are held up until after 753 * we have closed this device so the streams linkage is maintained 754 */ 755 csp = VTOS(cvp); 756 757 LOCK_CSP(csp); 758 mutex_enter(&csp->s_lock); 759 760 csp->s_count--; /* one fewer open reference : SN_RELE */ 761 sysclone = sp->s_flag & SCLONE; 762 763 /* 764 * Invalidate size on each close. 765 * 766 * XXX We do this on each close because we don't have interfaces that 767 * allow a driver to invalidate the size. Since clearing this on each 768 * close this causes property overhead we skip /dev/null and 769 * /dev/zero to avoid degrading kenbus performance. 770 */ 771 if (getmajor(dev) != mm_major) 772 csp->s_flag &= ~SSIZEVALID; 773 774 /* 775 * Only call the close routine when the last open reference through 776 * any [s, v]node goes away. This can be checked by looking at 777 * s_count on the common vnode. 778 */ 779 if ((csp->s_count == 0) && (csp->s_mapcnt == 0)) { 780 /* we don't need a close */ 781 csp->s_flag &= ~(SNEEDCLOSE | SSIZEVALID); 782 783 /* 784 * A cloning driver may open-clone to the same dev_t that we 785 * are closing before spec_inactive destroys the common snode. 786 * If this occurs the s_dip association needs to be reevaluated. 787 * We clear SDIPSET to force reevaluation in this case. When 788 * reevaluation occurs (by spec_clone after open), if the 789 * devinfo association has changed then the old association 790 * will be released as the new association is established by 791 * spec_assoc_vp_with_devi(). 792 */ 793 if (csp->s_flag & (SCLONE | SSELFCLONE)) 794 csp->s_flag &= ~SDIPSET; 795 796 mutex_exit(&csp->s_lock); 797 error = device_close(vp, flag, cr); 798 799 /* 800 * Decrement the devops held in clnopen() 801 */ 802 if (sysclone) { 803 ddi_rele_driver(getmajor(dev)); 804 } 805 mutex_enter(&csp->s_lock); 806 } 807 808 UNLOCK_CSP_LOCK_HELD(csp); 809 mutex_exit(&csp->s_lock); 810 811 return (error); 812 } 813 814 /*ARGSUSED2*/ 815 static int 816 spec_read( 817 struct vnode *vp, 818 struct uio *uiop, 819 int ioflag, 820 struct cred *cr, 821 struct caller_context *ct) 822 { 823 int error; 824 struct snode *sp = VTOS(vp); 825 dev_t dev = sp->s_dev; 826 size_t n; 827 ulong_t on; 828 u_offset_t bdevsize; 829 offset_t maxoff; 830 offset_t off; 831 struct vnode *blkvp; 832 833 ASSERT(vp->v_type == VCHR || vp->v_type == VBLK); 834 835 if (STREAMSTAB(getmajor(dev))) { /* stream */ 836 ASSERT(vp->v_type == VCHR); 837 smark(sp, SACC); 838 return (strread(vp, uiop, cr)); 839 } 840 841 if (uiop->uio_resid == 0) 842 return (0); 843 844 /* 845 * Plain old character devices that set D_U64BIT can have 846 * unrestricted offsets. 847 */ 848 maxoff = spec_maxoffset(vp); 849 ASSERT(maxoff != -1 || vp->v_type == VCHR); 850 851 if (maxoff != -1 && (uiop->uio_loffset < 0 || 852 uiop->uio_loffset + uiop->uio_resid > maxoff)) 853 return (EINVAL); 854 855 if (vp->v_type == VCHR) { 856 smark(sp, SACC); 857 ASSERT(STREAMSTAB(getmajor(dev)) == 0); 858 return (cdev_read(dev, uiop, cr)); 859 } 860 861 /* 862 * Block device. 863 */ 864 error = 0; 865 blkvp = sp->s_commonvp; 866 bdevsize = SPEC_SIZE(VTOS(blkvp)); 867 868 do { 869 caddr_t base; 870 offset_t diff; 871 872 off = uiop->uio_loffset & (offset_t)MAXBMASK; 873 on = (size_t)(uiop->uio_loffset & MAXBOFFSET); 874 n = (size_t)MIN(MAXBSIZE - on, uiop->uio_resid); 875 diff = bdevsize - uiop->uio_loffset; 876 877 if (diff <= 0) 878 break; 879 if (diff < n) 880 n = (size_t)diff; 881 882 if (vpm_enable) { 883 error = vpm_data_copy(blkvp, (u_offset_t)(off + on), 884 n, uiop, 1, NULL, 0, S_READ); 885 } else { 886 base = segmap_getmapflt(segkmap, blkvp, 887 (u_offset_t)(off + on), n, 1, S_READ); 888 889 error = uiomove(base + on, n, UIO_READ, uiop); 890 } 891 if (!error) { 892 int flags = 0; 893 /* 894 * If we read a whole block, we won't need this 895 * buffer again soon. 896 */ 897 if (n + on == MAXBSIZE) 898 flags = SM_DONTNEED | SM_FREE; 899 if (vpm_enable) { 900 error = vpm_sync_pages(blkvp, off, n, flags); 901 } else { 902 error = segmap_release(segkmap, base, flags); 903 } 904 } else { 905 if (vpm_enable) { 906 (void) vpm_sync_pages(blkvp, off, n, 0); 907 } else { 908 (void) segmap_release(segkmap, base, 0); 909 } 910 if (bdevsize == UNKNOWN_SIZE) { 911 error = 0; 912 break; 913 } 914 } 915 } while (error == 0 && uiop->uio_resid > 0 && n != 0); 916 917 return (error); 918 } 919 920 /*ARGSUSED*/ 921 static int 922 spec_write( 923 struct vnode *vp, 924 struct uio *uiop, 925 int ioflag, 926 struct cred *cr, 927 struct caller_context *ct) 928 { 929 int error; 930 struct snode *sp = VTOS(vp); 931 dev_t dev = sp->s_dev; 932 size_t n; 933 ulong_t on; 934 u_offset_t bdevsize; 935 offset_t maxoff; 936 offset_t off; 937 struct vnode *blkvp; 938 939 ASSERT(vp->v_type == VCHR || vp->v_type == VBLK); 940 941 if (STREAMSTAB(getmajor(dev))) { 942 ASSERT(vp->v_type == VCHR); 943 smark(sp, SUPD); 944 return (strwrite(vp, uiop, cr)); 945 } 946 947 /* 948 * Plain old character devices that set D_U64BIT can have 949 * unrestricted offsets. 950 */ 951 maxoff = spec_maxoffset(vp); 952 ASSERT(maxoff != -1 || vp->v_type == VCHR); 953 954 if (maxoff != -1 && (uiop->uio_loffset < 0 || 955 uiop->uio_loffset + uiop->uio_resid > maxoff)) 956 return (EINVAL); 957 958 if (vp->v_type == VCHR) { 959 smark(sp, SUPD); 960 ASSERT(STREAMSTAB(getmajor(dev)) == 0); 961 return (cdev_write(dev, uiop, cr)); 962 } 963 964 if (uiop->uio_resid == 0) 965 return (0); 966 967 error = 0; 968 blkvp = sp->s_commonvp; 969 bdevsize = SPEC_SIZE(VTOS(blkvp)); 970 971 do { 972 int pagecreate; 973 int newpage; 974 caddr_t base; 975 offset_t diff; 976 977 off = uiop->uio_loffset & (offset_t)MAXBMASK; 978 on = (ulong_t)(uiop->uio_loffset & MAXBOFFSET); 979 n = (size_t)MIN(MAXBSIZE - on, uiop->uio_resid); 980 pagecreate = 0; 981 982 diff = bdevsize - uiop->uio_loffset; 983 if (diff <= 0) { 984 error = ENXIO; 985 break; 986 } 987 if (diff < n) 988 n = (size_t)diff; 989 990 /* 991 * Check to see if we can skip reading in the page 992 * and just allocate the memory. We can do this 993 * if we are going to rewrite the entire mapping 994 * or if we are going to write to end of the device 995 * from the beginning of the mapping. 996 */ 997 if (n == MAXBSIZE || (on == 0 && (off + n) == bdevsize)) 998 pagecreate = 1; 999 1000 newpage = 0; 1001 if (vpm_enable) { 1002 error = vpm_data_copy(blkvp, (u_offset_t)(off + on), 1003 n, uiop, !pagecreate, NULL, 0, S_WRITE); 1004 } else { 1005 base = segmap_getmapflt(segkmap, blkvp, 1006 (u_offset_t)(off + on), n, !pagecreate, S_WRITE); 1007 1008 /* 1009 * segmap_pagecreate() returns 1 if it calls 1010 * page_create_va() to allocate any pages. 1011 */ 1012 1013 if (pagecreate) 1014 newpage = segmap_pagecreate(segkmap, base + on, 1015 n, 0); 1016 1017 error = uiomove(base + on, n, UIO_WRITE, uiop); 1018 } 1019 1020 if (!vpm_enable && pagecreate && 1021 uiop->uio_loffset < 1022 P2ROUNDUP_TYPED(off + on + n, PAGESIZE, offset_t)) { 1023 /* 1024 * We created pages w/o initializing them completely, 1025 * thus we need to zero the part that wasn't set up. 1026 * This can happen if we write to the end of the device 1027 * or if we had some sort of error during the uiomove. 1028 */ 1029 long nzero; 1030 offset_t nmoved; 1031 1032 nmoved = (uiop->uio_loffset - (off + on)); 1033 if (nmoved < 0 || nmoved > n) { 1034 panic("spec_write: nmoved bogus"); 1035 /*NOTREACHED*/ 1036 } 1037 nzero = (long)P2ROUNDUP(on + n, PAGESIZE) - 1038 (on + nmoved); 1039 if (nzero < 0 || (on + nmoved + nzero > MAXBSIZE)) { 1040 panic("spec_write: nzero bogus"); 1041 /*NOTREACHED*/ 1042 } 1043 (void) kzero(base + on + nmoved, (size_t)nzero); 1044 } 1045 1046 /* 1047 * Unlock the pages which have been allocated by 1048 * page_create_va() in segmap_pagecreate(). 1049 */ 1050 if (!vpm_enable && newpage) 1051 segmap_pageunlock(segkmap, base + on, 1052 (size_t)n, S_WRITE); 1053 1054 if (error == 0) { 1055 int flags = 0; 1056 1057 /* 1058 * Force write back for synchronous write cases. 1059 */ 1060 if (ioflag & (FSYNC|FDSYNC)) 1061 flags = SM_WRITE; 1062 else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) { 1063 /* 1064 * Have written a whole block. 1065 * Start an asynchronous write and 1066 * mark the buffer to indicate that 1067 * it won't be needed again soon. 1068 * Push swap files here, since it 1069 * won't happen anywhere else. 1070 */ 1071 flags = SM_WRITE | SM_ASYNC | SM_DONTNEED; 1072 } 1073 smark(sp, SUPD|SCHG); 1074 if (vpm_enable) { 1075 error = vpm_sync_pages(blkvp, off, n, flags); 1076 } else { 1077 error = segmap_release(segkmap, base, flags); 1078 } 1079 } else { 1080 if (vpm_enable) { 1081 (void) vpm_sync_pages(blkvp, off, n, SM_INVAL); 1082 } else { 1083 (void) segmap_release(segkmap, base, SM_INVAL); 1084 } 1085 } 1086 1087 } while (error == 0 && uiop->uio_resid > 0 && n != 0); 1088 1089 return (error); 1090 } 1091 1092 static int 1093 spec_ioctl(struct vnode *vp, int cmd, intptr_t arg, int mode, struct cred *cr, 1094 int *rvalp) 1095 { 1096 struct snode *sp; 1097 dev_t dev; 1098 int error; 1099 1100 if (vp->v_type != VCHR) 1101 return (ENOTTY); 1102 sp = VTOS(vp); 1103 dev = sp->s_dev; 1104 if (STREAMSTAB(getmajor(dev))) { 1105 error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp); 1106 } else { 1107 error = cdev_ioctl(dev, cmd, arg, mode, cr, rvalp); 1108 } 1109 return (error); 1110 } 1111 1112 static int 1113 spec_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr) 1114 { 1115 int error; 1116 struct snode *sp; 1117 struct vnode *realvp; 1118 1119 /* With ATTR_COMM we will not get attributes from realvp */ 1120 if (flags & ATTR_COMM) { 1121 sp = VTOS(vp); 1122 vp = sp->s_commonvp; 1123 } 1124 sp = VTOS(vp); 1125 realvp = sp->s_realvp; 1126 1127 if (realvp == NULL) { 1128 static int snode_shift = 0; 1129 1130 /* 1131 * Calculate the amount of bitshift to a snode pointer which 1132 * will still keep it unique. See below. 1133 */ 1134 if (snode_shift == 0) 1135 snode_shift = highbit(sizeof (struct snode)); 1136 ASSERT(snode_shift > 0); 1137 1138 /* 1139 * No real vnode behind this one. Fill in the fields 1140 * from the snode. 1141 * 1142 * This code should be refined to return only the 1143 * attributes asked for instead of all of them. 1144 */ 1145 vap->va_type = vp->v_type; 1146 vap->va_mode = 0; 1147 vap->va_uid = vap->va_gid = 0; 1148 vap->va_fsid = sp->s_fsid; 1149 1150 /* 1151 * If the va_nodeid is > MAX_USHORT, then i386 stats might 1152 * fail. So we shift down the snode pointer to try and get 1153 * the most uniqueness into 16-bits. 1154 */ 1155 vap->va_nodeid = ((ino64_t)(uintptr_t)sp >> snode_shift) & 1156 0xFFFF; 1157 vap->va_nlink = 0; 1158 vap->va_rdev = sp->s_dev; 1159 1160 /* 1161 * va_nblocks is the number of 512 byte blocks used to store 1162 * the mknod for the device, not the number of blocks on the 1163 * device itself. This is typically zero since the mknod is 1164 * represented directly in the inode itself. 1165 */ 1166 vap->va_nblocks = 0; 1167 } else { 1168 error = VOP_GETATTR(realvp, vap, flags, cr); 1169 if (error != 0) 1170 return (error); 1171 } 1172 1173 /* set the size from the snode */ 1174 vap->va_size = SPEC_SIZE(VTOS(sp->s_commonvp)); 1175 vap->va_blksize = MAXBSIZE; 1176 1177 mutex_enter(&sp->s_lock); 1178 vap->va_atime.tv_sec = sp->s_atime; 1179 vap->va_mtime.tv_sec = sp->s_mtime; 1180 vap->va_ctime.tv_sec = sp->s_ctime; 1181 mutex_exit(&sp->s_lock); 1182 1183 vap->va_atime.tv_nsec = 0; 1184 vap->va_mtime.tv_nsec = 0; 1185 vap->va_ctime.tv_nsec = 0; 1186 vap->va_seq = 0; 1187 1188 return (0); 1189 } 1190 1191 static int 1192 spec_setattr( 1193 struct vnode *vp, 1194 struct vattr *vap, 1195 int flags, 1196 struct cred *cr, 1197 caller_context_t *ctp) 1198 { 1199 struct snode *sp = VTOS(vp); 1200 struct vnode *realvp; 1201 int error; 1202 1203 if (vp->v_type == VCHR && vp->v_stream && (vap->va_mask & AT_SIZE)) { 1204 /* 1205 * 1135080: O_TRUNC should have no effect on 1206 * named pipes and terminal devices. 1207 */ 1208 ASSERT(vap->va_mask == AT_SIZE); 1209 return (0); 1210 } 1211 1212 if ((realvp = sp->s_realvp) == NULL) 1213 error = 0; /* no real vnode to update */ 1214 else 1215 error = VOP_SETATTR(realvp, vap, flags, cr, ctp); 1216 if (error == 0) { 1217 /* 1218 * If times were changed, update snode. 1219 */ 1220 mutex_enter(&sp->s_lock); 1221 if (vap->va_mask & AT_ATIME) 1222 sp->s_atime = vap->va_atime.tv_sec; 1223 if (vap->va_mask & AT_MTIME) { 1224 sp->s_mtime = vap->va_mtime.tv_sec; 1225 sp->s_ctime = gethrestime_sec(); 1226 } 1227 mutex_exit(&sp->s_lock); 1228 } 1229 return (error); 1230 } 1231 1232 static int 1233 spec_access(struct vnode *vp, int mode, int flags, struct cred *cr) 1234 { 1235 struct vnode *realvp; 1236 struct snode *sp = VTOS(vp); 1237 1238 if ((realvp = sp->s_realvp) != NULL) 1239 return (VOP_ACCESS(realvp, mode, flags, cr)); 1240 else 1241 return (0); /* Allow all access. */ 1242 } 1243 1244 /* 1245 * This can be called if creat or an open with O_CREAT is done on the root 1246 * of a lofs mount where the mounted entity is a special file. 1247 */ 1248 /*ARGSUSED*/ 1249 static int 1250 spec_create(struct vnode *dvp, char *name, vattr_t *vap, enum vcexcl excl, 1251 int mode, struct vnode **vpp, struct cred *cr, int flag) 1252 { 1253 int error; 1254 1255 ASSERT(dvp && (dvp->v_flag & VROOT) && *name == '\0'); 1256 if (excl == NONEXCL) { 1257 if (mode && (error = spec_access(dvp, mode, 0, cr))) 1258 return (error); 1259 VN_HOLD(dvp); 1260 return (0); 1261 } 1262 return (EEXIST); 1263 } 1264 1265 /* 1266 * In order to sync out the snode times without multi-client problems, 1267 * make sure the times written out are never earlier than the times 1268 * already set in the vnode. 1269 */ 1270 static int 1271 spec_fsync(struct vnode *vp, int syncflag, struct cred *cr) 1272 { 1273 struct snode *sp = VTOS(vp); 1274 struct vnode *realvp; 1275 struct vnode *cvp; 1276 struct vattr va, vatmp; 1277 1278 /* If times didn't change, don't flush anything. */ 1279 mutex_enter(&sp->s_lock); 1280 if ((sp->s_flag & (SACC|SUPD|SCHG)) == 0 && vp->v_type != VBLK) { 1281 mutex_exit(&sp->s_lock); 1282 return (0); 1283 } 1284 sp->s_flag &= ~(SACC|SUPD|SCHG); 1285 mutex_exit(&sp->s_lock); 1286 cvp = sp->s_commonvp; 1287 realvp = sp->s_realvp; 1288 1289 if (vp->v_type == VBLK && cvp != vp && vn_has_cached_data(cvp) && 1290 (cvp->v_flag & VISSWAP) == 0) 1291 (void) VOP_PUTPAGE(cvp, (offset_t)0, 0, 0, cr); 1292 1293 /* 1294 * If no real vnode to update, don't flush anything. 1295 */ 1296 if (realvp == NULL) 1297 return (0); 1298 1299 vatmp.va_mask = AT_ATIME|AT_MTIME; 1300 if (VOP_GETATTR(realvp, &vatmp, 0, cr) == 0) { 1301 1302 mutex_enter(&sp->s_lock); 1303 if (vatmp.va_atime.tv_sec > sp->s_atime) 1304 va.va_atime = vatmp.va_atime; 1305 else { 1306 va.va_atime.tv_sec = sp->s_atime; 1307 va.va_atime.tv_nsec = 0; 1308 } 1309 if (vatmp.va_mtime.tv_sec > sp->s_mtime) 1310 va.va_mtime = vatmp.va_mtime; 1311 else { 1312 va.va_mtime.tv_sec = sp->s_mtime; 1313 va.va_mtime.tv_nsec = 0; 1314 } 1315 mutex_exit(&sp->s_lock); 1316 1317 va.va_mask = AT_ATIME|AT_MTIME; 1318 (void) VOP_SETATTR(realvp, &va, 0, cr, NULL); 1319 } 1320 (void) VOP_FSYNC(realvp, syncflag, cr); 1321 return (0); 1322 } 1323 1324 /*ARGSUSED*/ 1325 static void 1326 spec_inactive(struct vnode *vp, struct cred *cr) 1327 { 1328 struct snode *sp = VTOS(vp); 1329 struct vnode *cvp; 1330 struct vnode *rvp; 1331 1332 /* 1333 * If no one has reclaimed the vnode, remove from the 1334 * cache now. 1335 */ 1336 if (vp->v_count < 1) { 1337 panic("spec_inactive: Bad v_count"); 1338 /*NOTREACHED*/ 1339 } 1340 mutex_enter(&stable_lock); 1341 1342 mutex_enter(&vp->v_lock); 1343 /* 1344 * Drop the temporary hold by vn_rele now 1345 */ 1346 if (--vp->v_count != 0) { 1347 mutex_exit(&vp->v_lock); 1348 mutex_exit(&stable_lock); 1349 return; 1350 } 1351 mutex_exit(&vp->v_lock); 1352 1353 sdelete(sp); 1354 mutex_exit(&stable_lock); 1355 1356 /* We are the sole owner of sp now */ 1357 cvp = sp->s_commonvp; 1358 rvp = sp->s_realvp; 1359 1360 if (rvp) { 1361 /* 1362 * If the snode times changed, then update the times 1363 * associated with the "realvp". 1364 */ 1365 if ((sp->s_flag & (SACC|SUPD|SCHG)) != 0) { 1366 1367 struct vattr va, vatmp; 1368 1369 mutex_enter(&sp->s_lock); 1370 sp->s_flag &= ~(SACC|SUPD|SCHG); 1371 mutex_exit(&sp->s_lock); 1372 vatmp.va_mask = AT_ATIME|AT_MTIME; 1373 /* 1374 * The user may not own the device, but we 1375 * want to update the attributes anyway. 1376 */ 1377 if (VOP_GETATTR(rvp, &vatmp, 0, kcred) == 0) { 1378 if (vatmp.va_atime.tv_sec > sp->s_atime) 1379 va.va_atime = vatmp.va_atime; 1380 else { 1381 va.va_atime.tv_sec = sp->s_atime; 1382 va.va_atime.tv_nsec = 0; 1383 } 1384 if (vatmp.va_mtime.tv_sec > sp->s_mtime) 1385 va.va_mtime = vatmp.va_mtime; 1386 else { 1387 va.va_mtime.tv_sec = sp->s_mtime; 1388 va.va_mtime.tv_nsec = 0; 1389 } 1390 1391 va.va_mask = AT_ATIME|AT_MTIME; 1392 (void) VOP_SETATTR(rvp, &va, 0, kcred, NULL); 1393 } 1394 } 1395 } 1396 ASSERT(!vn_has_cached_data(vp)); 1397 vn_invalid(vp); 1398 1399 /* if we are sharing another file systems vfs, release it */ 1400 if (vp->v_vfsp && (vp->v_vfsp != &spec_vfs)) 1401 VFS_RELE(vp->v_vfsp); 1402 1403 /* if we have a realvp, release the realvp */ 1404 if (rvp) 1405 VN_RELE(rvp); 1406 1407 /* if we have a common, release the common */ 1408 if (cvp && (cvp != vp)) { 1409 VN_RELE(cvp); 1410 #ifdef DEBUG 1411 } else if (cvp) { 1412 /* 1413 * if this is the last reference to a common vnode, any 1414 * associated stream had better have been closed 1415 */ 1416 ASSERT(cvp == vp); 1417 ASSERT(cvp->v_stream == NULL); 1418 #endif /* DEBUG */ 1419 } 1420 1421 /* 1422 * if we have a hold on a devinfo node (established by 1423 * spec_assoc_vp_with_devi), release the hold 1424 */ 1425 if (sp->s_dip) 1426 ddi_release_devi(sp->s_dip); 1427 1428 /* 1429 * If we have an associated device policy, release it. 1430 */ 1431 if (sp->s_plcy != NULL) 1432 dpfree(sp->s_plcy); 1433 1434 /* 1435 * If all holds on the devinfo node are through specfs/devfs 1436 * and we just destroyed the last specfs node associated with the 1437 * device, then the devinfo node reference count should now be 1438 * zero. We can't check this because there may be other holds 1439 * on the node from non file system sources: ddi_hold_devi_by_instance 1440 * for example. 1441 */ 1442 kmem_cache_free(snode_cache, sp); 1443 } 1444 1445 static int 1446 spec_fid(struct vnode *vp, struct fid *fidp) 1447 { 1448 struct vnode *realvp; 1449 struct snode *sp = VTOS(vp); 1450 1451 if ((realvp = sp->s_realvp) != NULL) 1452 return (VOP_FID(realvp, fidp)); 1453 else 1454 return (EINVAL); 1455 } 1456 1457 /*ARGSUSED1*/ 1458 static int 1459 spec_seek(struct vnode *vp, offset_t ooff, offset_t *noffp) 1460 { 1461 offset_t maxoff = spec_maxoffset(vp); 1462 1463 if (maxoff == -1 || *noffp <= maxoff) 1464 return (0); 1465 else 1466 return (EINVAL); 1467 } 1468 1469 static int 1470 spec_frlock( 1471 struct vnode *vp, 1472 int cmd, 1473 struct flock64 *bfp, 1474 int flag, 1475 offset_t offset, 1476 struct flk_callback *flk_cbp, 1477 struct cred *cr) 1478 { 1479 struct snode *sp = VTOS(vp); 1480 struct snode *csp; 1481 1482 csp = VTOS(sp->s_commonvp); 1483 /* 1484 * If file is being mapped, disallow frlock. 1485 */ 1486 if (csp->s_mapcnt > 0) 1487 return (EAGAIN); 1488 1489 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr)); 1490 } 1491 1492 static int 1493 spec_realvp(struct vnode *vp, struct vnode **vpp) 1494 { 1495 struct vnode *rvp; 1496 1497 if ((rvp = VTOS(vp)->s_realvp) != NULL) { 1498 vp = rvp; 1499 if (VOP_REALVP(vp, &rvp) == 0) 1500 vp = rvp; 1501 } 1502 1503 *vpp = vp; 1504 return (0); 1505 } 1506 1507 /* 1508 * Return all the pages from [off..off + len] in block 1509 * or character device. 1510 */ 1511 static int 1512 spec_getpage( 1513 struct vnode *vp, 1514 offset_t off, 1515 size_t len, 1516 uint_t *protp, 1517 page_t *pl[], 1518 size_t plsz, 1519 struct seg *seg, 1520 caddr_t addr, 1521 enum seg_rw rw, 1522 struct cred *cr) 1523 { 1524 struct snode *sp = VTOS(vp); 1525 int err; 1526 1527 ASSERT(sp->s_commonvp == vp); 1528 1529 /* 1530 * XXX Given the above assertion, this might not do 1531 * what is wanted here. 1532 */ 1533 if (vp->v_flag & VNOMAP) 1534 return (ENOSYS); 1535 TRACE_4(TR_FAC_SPECFS, TR_SPECFS_GETPAGE, 1536 "specfs getpage:vp %p off %llx len %ld snode %p", 1537 vp, off, len, sp); 1538 1539 switch (vp->v_type) { 1540 case VBLK: 1541 if (protp != NULL) 1542 *protp = PROT_ALL; 1543 1544 if (((u_offset_t)off + len) > (SPEC_SIZE(sp) + PAGEOFFSET)) 1545 return (EFAULT); /* beyond EOF */ 1546 1547 if (len <= PAGESIZE) 1548 err = spec_getapage(vp, (u_offset_t)off, len, protp, pl, 1549 plsz, seg, addr, rw, cr); 1550 else 1551 err = pvn_getpages(spec_getapage, vp, (u_offset_t)off, 1552 len, protp, pl, plsz, seg, addr, rw, cr); 1553 break; 1554 1555 case VCHR: 1556 cmn_err(CE_NOTE, "spec_getpage called for character device. " 1557 "Check any non-ON consolidation drivers"); 1558 err = 0; 1559 pl[0] = (page_t *)0; 1560 break; 1561 1562 default: 1563 panic("spec_getpage: bad v_type 0x%x", vp->v_type); 1564 /*NOTREACHED*/ 1565 } 1566 1567 return (err); 1568 } 1569 1570 extern int klustsize; /* set in machdep.c */ 1571 1572 int spec_ra = 1; 1573 int spec_lostpage; /* number of times we lost original page */ 1574 1575 /*ARGSUSED2*/ 1576 static int 1577 spec_getapage( 1578 struct vnode *vp, 1579 u_offset_t off, 1580 size_t len, 1581 uint_t *protp, 1582 page_t *pl[], 1583 size_t plsz, 1584 struct seg *seg, 1585 caddr_t addr, 1586 enum seg_rw rw, 1587 struct cred *cr) 1588 { 1589 struct snode *sp; 1590 struct buf *bp; 1591 page_t *pp, *pp2; 1592 u_offset_t io_off1, io_off2; 1593 size_t io_len1; 1594 size_t io_len2; 1595 size_t blksz; 1596 u_offset_t blkoff; 1597 int dora, err; 1598 page_t *pagefound; 1599 uint_t xlen; 1600 size_t adj_klustsize; 1601 u_offset_t size; 1602 u_offset_t tmpoff; 1603 1604 sp = VTOS(vp); 1605 TRACE_3(TR_FAC_SPECFS, TR_SPECFS_GETAPAGE, 1606 "specfs getapage:vp %p off %llx snode %p", vp, off, sp); 1607 reread: 1608 1609 err = 0; 1610 bp = NULL; 1611 pp = NULL; 1612 pp2 = NULL; 1613 1614 if (pl != NULL) 1615 pl[0] = NULL; 1616 1617 size = SPEC_SIZE(VTOS(sp->s_commonvp)); 1618 1619 if (spec_ra && sp->s_nextr == off) 1620 dora = 1; 1621 else 1622 dora = 0; 1623 1624 if (size == UNKNOWN_SIZE) { 1625 dora = 0; 1626 adj_klustsize = PAGESIZE; 1627 } else { 1628 adj_klustsize = dora ? klustsize : PAGESIZE; 1629 } 1630 1631 again: 1632 if ((pagefound = page_exists(vp, off)) == NULL) { 1633 if (rw == S_CREATE) { 1634 /* 1635 * We're allocating a swap slot and it's 1636 * associated page was not found, so allocate 1637 * and return it. 1638 */ 1639 if ((pp = page_create_va(vp, off, 1640 PAGESIZE, PG_WAIT, seg, addr)) == NULL) { 1641 panic("spec_getapage: page_create"); 1642 /*NOTREACHED*/ 1643 } 1644 io_len1 = PAGESIZE; 1645 sp->s_nextr = off + PAGESIZE; 1646 } else { 1647 /* 1648 * Need to really do disk I/O to get the page(s). 1649 */ 1650 blkoff = (off / adj_klustsize) * adj_klustsize; 1651 if (size == UNKNOWN_SIZE) { 1652 blksz = PAGESIZE; 1653 } else { 1654 if (blkoff + adj_klustsize <= size) 1655 blksz = adj_klustsize; 1656 else 1657 blksz = 1658 MIN(size - blkoff, adj_klustsize); 1659 } 1660 1661 pp = pvn_read_kluster(vp, off, seg, addr, &tmpoff, 1662 &io_len1, blkoff, blksz, 0); 1663 io_off1 = tmpoff; 1664 /* 1665 * Make sure the page didn't sneek into the 1666 * cache while we blocked in pvn_read_kluster. 1667 */ 1668 if (pp == NULL) 1669 goto again; 1670 1671 /* 1672 * Zero part of page which we are not 1673 * going to be reading from disk now. 1674 */ 1675 xlen = (uint_t)(io_len1 & PAGEOFFSET); 1676 if (xlen != 0) 1677 pagezero(pp->p_prev, xlen, PAGESIZE - xlen); 1678 1679 bp = spec_startio(vp, pp, io_off1, io_len1, 1680 pl == NULL ? (B_ASYNC | B_READ) : B_READ); 1681 sp->s_nextr = io_off1 + io_len1; 1682 } 1683 } 1684 1685 if (dora && rw != S_CREATE) { 1686 u_offset_t off2; 1687 caddr_t addr2; 1688 1689 off2 = ((off / adj_klustsize) + 1) * adj_klustsize; 1690 addr2 = addr + (off2 - off); 1691 1692 pp2 = NULL; 1693 /* 1694 * If we are past EOF then don't bother trying 1695 * with read-ahead. 1696 */ 1697 if (off2 >= size) 1698 pp2 = NULL; 1699 else { 1700 if (off2 + adj_klustsize <= size) 1701 blksz = adj_klustsize; 1702 else 1703 blksz = MIN(size - off2, adj_klustsize); 1704 1705 pp2 = pvn_read_kluster(vp, off2, seg, addr2, &tmpoff, 1706 &io_len2, off2, blksz, 1); 1707 io_off2 = tmpoff; 1708 } 1709 1710 if (pp2 != NULL) { 1711 /* 1712 * Zero part of page which we are not 1713 * going to be reading from disk now. 1714 */ 1715 xlen = (uint_t)(io_len2 & PAGEOFFSET); 1716 if (xlen != 0) 1717 pagezero(pp2->p_prev, xlen, PAGESIZE - xlen); 1718 1719 (void) spec_startio(vp, pp2, io_off2, io_len2, 1720 B_READ | B_ASYNC); 1721 } 1722 } 1723 1724 if (pl == NULL) 1725 return (err); 1726 1727 if (bp != NULL) { 1728 err = biowait(bp); 1729 pageio_done(bp); 1730 1731 if (err) { 1732 if (pp != NULL) 1733 pvn_read_done(pp, B_ERROR); 1734 return (err); 1735 } 1736 } 1737 1738 if (pagefound) { 1739 se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED); 1740 /* 1741 * Page exists in the cache, acquire the appropriate 1742 * lock. If this fails, start all over again. 1743 */ 1744 1745 if ((pp = page_lookup(vp, off, se)) == NULL) { 1746 spec_lostpage++; 1747 goto reread; 1748 } 1749 pl[0] = pp; 1750 pl[1] = NULL; 1751 1752 sp->s_nextr = off + PAGESIZE; 1753 return (0); 1754 } 1755 1756 if (pp != NULL) 1757 pvn_plist_init(pp, pl, plsz, off, io_len1, rw); 1758 return (0); 1759 } 1760 1761 /* 1762 * Flags are composed of {B_INVAL, B_DIRTY B_FREE, B_DONTNEED, B_FORCE}. 1763 * If len == 0, do from off to EOF. 1764 * 1765 * The normal cases should be len == 0 & off == 0 (entire vp list), 1766 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE 1767 * (from pageout). 1768 */ 1769 int 1770 spec_putpage( 1771 struct vnode *vp, 1772 offset_t off, 1773 size_t len, 1774 int flags, 1775 struct cred *cr) 1776 { 1777 struct snode *sp = VTOS(vp); 1778 struct vnode *cvp; 1779 page_t *pp; 1780 u_offset_t io_off; 1781 size_t io_len = 0; /* for lint */ 1782 int err = 0; 1783 u_offset_t size; 1784 u_offset_t tmpoff; 1785 1786 ASSERT(vp->v_count != 0); 1787 1788 if (vp->v_flag & VNOMAP) 1789 return (ENOSYS); 1790 1791 cvp = sp->s_commonvp; 1792 size = SPEC_SIZE(VTOS(cvp)); 1793 1794 if (!vn_has_cached_data(vp) || off >= size) 1795 return (0); 1796 1797 ASSERT(vp->v_type == VBLK && cvp == vp); 1798 TRACE_4(TR_FAC_SPECFS, TR_SPECFS_PUTPAGE, 1799 "specfs putpage:vp %p off %llx len %ld snode %p", 1800 vp, off, len, sp); 1801 1802 if (len == 0) { 1803 /* 1804 * Search the entire vp list for pages >= off. 1805 */ 1806 err = pvn_vplist_dirty(vp, off, spec_putapage, 1807 flags, cr); 1808 } else { 1809 u_offset_t eoff; 1810 1811 /* 1812 * Loop over all offsets in the range [off...off + len] 1813 * looking for pages to deal with. We set limits so 1814 * that we kluster to klustsize boundaries. 1815 */ 1816 eoff = off + len; 1817 for (io_off = off; io_off < eoff && io_off < size; 1818 io_off += io_len) { 1819 /* 1820 * If we are not invalidating, synchronously 1821 * freeing or writing pages use the routine 1822 * page_lookup_nowait() to prevent reclaiming 1823 * them from the free list. 1824 */ 1825 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 1826 pp = page_lookup(vp, io_off, 1827 (flags & (B_INVAL | B_FREE)) ? 1828 SE_EXCL : SE_SHARED); 1829 } else { 1830 pp = page_lookup_nowait(vp, io_off, 1831 (flags & B_FREE) ? SE_EXCL : SE_SHARED); 1832 } 1833 1834 if (pp == NULL || pvn_getdirty(pp, flags) == 0) 1835 io_len = PAGESIZE; 1836 else { 1837 err = spec_putapage(vp, pp, &tmpoff, &io_len, 1838 flags, cr); 1839 io_off = tmpoff; 1840 if (err != 0) 1841 break; 1842 /* 1843 * "io_off" and "io_len" are returned as 1844 * the range of pages we actually wrote. 1845 * This allows us to skip ahead more quickly 1846 * since several pages may've been dealt 1847 * with by this iteration of the loop. 1848 */ 1849 } 1850 } 1851 } 1852 return (err); 1853 } 1854 1855 1856 /* 1857 * Write out a single page, possibly klustering adjacent 1858 * dirty pages. 1859 */ 1860 /*ARGSUSED5*/ 1861 static int 1862 spec_putapage( 1863 struct vnode *vp, 1864 page_t *pp, 1865 u_offset_t *offp, /* return value */ 1866 size_t *lenp, /* return value */ 1867 int flags, 1868 struct cred *cr) 1869 { 1870 struct snode *sp = VTOS(vp); 1871 u_offset_t io_off; 1872 size_t io_len; 1873 size_t blksz; 1874 u_offset_t blkoff; 1875 int err = 0; 1876 struct buf *bp; 1877 u_offset_t size; 1878 size_t adj_klustsize; 1879 u_offset_t tmpoff; 1880 1881 /* 1882 * Destroy read ahead value since we are really going to write. 1883 */ 1884 sp->s_nextr = 0; 1885 size = SPEC_SIZE(VTOS(sp->s_commonvp)); 1886 1887 adj_klustsize = klustsize; 1888 1889 blkoff = (pp->p_offset / adj_klustsize) * adj_klustsize; 1890 1891 if (blkoff + adj_klustsize <= size) 1892 blksz = adj_klustsize; 1893 else 1894 blksz = size - blkoff; 1895 1896 /* 1897 * Find a kluster that fits in one contiguous chunk. 1898 */ 1899 pp = pvn_write_kluster(vp, pp, &tmpoff, &io_len, blkoff, 1900 blksz, flags); 1901 io_off = tmpoff; 1902 1903 /* 1904 * Check for page length rounding problems 1905 * XXX - Is this necessary? 1906 */ 1907 if (io_off + io_len > size) { 1908 ASSERT((io_off + io_len) - size < PAGESIZE); 1909 io_len = size - io_off; 1910 } 1911 1912 bp = spec_startio(vp, pp, io_off, io_len, B_WRITE | flags); 1913 1914 /* 1915 * Wait for i/o to complete if the request is not B_ASYNC. 1916 */ 1917 if ((flags & B_ASYNC) == 0) { 1918 err = biowait(bp); 1919 pageio_done(bp); 1920 pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags); 1921 } 1922 1923 if (offp) 1924 *offp = io_off; 1925 if (lenp) 1926 *lenp = io_len; 1927 TRACE_4(TR_FAC_SPECFS, TR_SPECFS_PUTAPAGE, 1928 "specfs putapage:vp %p offp %p snode %p err %d", 1929 vp, offp, sp, err); 1930 return (err); 1931 } 1932 1933 /* 1934 * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED} 1935 */ 1936 static struct buf * 1937 spec_startio( 1938 struct vnode *vp, 1939 page_t *pp, 1940 u_offset_t io_off, 1941 size_t io_len, 1942 int flags) 1943 { 1944 struct buf *bp; 1945 1946 bp = pageio_setup(pp, io_len, vp, flags); 1947 1948 bp->b_edev = vp->v_rdev; 1949 bp->b_dev = cmpdev(vp->v_rdev); 1950 bp->b_blkno = btodt(io_off); 1951 bp->b_un.b_addr = (caddr_t)0; 1952 1953 (void) bdev_strategy(bp); 1954 1955 if (flags & B_READ) 1956 lwp_stat_update(LWP_STAT_INBLK, 1); 1957 else 1958 lwp_stat_update(LWP_STAT_OUBLK, 1); 1959 1960 return (bp); 1961 } 1962 1963 static int 1964 spec_poll( 1965 struct vnode *vp, 1966 short events, 1967 int anyyet, 1968 short *reventsp, 1969 struct pollhead **phpp) 1970 { 1971 dev_t dev; 1972 int error; 1973 1974 if (vp->v_type == VBLK) 1975 error = fs_poll(vp, events, anyyet, reventsp, phpp); 1976 else { 1977 ASSERT(vp->v_type == VCHR); 1978 dev = vp->v_rdev; 1979 if (STREAMSTAB(getmajor(dev))) { 1980 ASSERT(vp->v_stream != NULL); 1981 error = strpoll(vp->v_stream, events, anyyet, 1982 reventsp, phpp); 1983 } else if (devopsp[getmajor(dev)]->devo_cb_ops->cb_chpoll) { 1984 error = cdev_poll(dev, events, anyyet, reventsp, phpp); 1985 } else { 1986 error = fs_poll(vp, events, anyyet, reventsp, phpp); 1987 } 1988 } 1989 return (error); 1990 } 1991 1992 /* 1993 * This routine is called through the cdevsw[] table to handle 1994 * traditional mmap'able devices that support a d_mmap function. 1995 */ 1996 /*ARGSUSED8*/ 1997 int 1998 spec_segmap( 1999 dev_t dev, 2000 off_t off, 2001 struct as *as, 2002 caddr_t *addrp, 2003 off_t len, 2004 uint_t prot, 2005 uint_t maxprot, 2006 uint_t flags, 2007 struct cred *cred) 2008 { 2009 struct segdev_crargs dev_a; 2010 int (*mapfunc)(dev_t dev, off_t off, int prot); 2011 size_t i; 2012 int error; 2013 2014 if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev) 2015 return (ENODEV); 2016 TRACE_4(TR_FAC_SPECFS, TR_SPECFS_SEGMAP, 2017 "specfs segmap:dev %x as %p len %lx prot %x", 2018 dev, as, len, prot); 2019 2020 /* 2021 * Character devices that support the d_mmap 2022 * interface can only be mmap'ed shared. 2023 */ 2024 if ((flags & MAP_TYPE) != MAP_SHARED) 2025 return (EINVAL); 2026 2027 /* 2028 * Check to ensure that the entire range is 2029 * legal and we are not trying to map in 2030 * more than the device will let us. 2031 */ 2032 for (i = 0; i < len; i += PAGESIZE) { 2033 if (cdev_mmap(mapfunc, dev, off + i, maxprot) == -1) 2034 return (ENXIO); 2035 } 2036 2037 as_rangelock(as); 2038 if ((flags & MAP_FIXED) == 0) { 2039 /* 2040 * Pick an address w/o worrying about 2041 * any vac alignment constraints. 2042 */ 2043 map_addr(addrp, len, (offset_t)off, 0, flags); 2044 if (*addrp == NULL) { 2045 as_rangeunlock(as); 2046 return (ENOMEM); 2047 } 2048 } else { 2049 /* 2050 * User-specified address; blow away any previous mappings. 2051 */ 2052 (void) as_unmap(as, *addrp, len); 2053 } 2054 2055 dev_a.mapfunc = mapfunc; 2056 dev_a.dev = dev; 2057 dev_a.offset = off; 2058 dev_a.prot = (uchar_t)prot; 2059 dev_a.maxprot = (uchar_t)maxprot; 2060 dev_a.hat_flags = 0; 2061 dev_a.hat_attr = 0; 2062 dev_a.devmap_data = NULL; 2063 2064 error = as_map(as, *addrp, len, segdev_create, &dev_a); 2065 as_rangeunlock(as); 2066 return (error); 2067 } 2068 2069 int 2070 spec_char_map( 2071 dev_t dev, 2072 offset_t off, 2073 struct as *as, 2074 caddr_t *addrp, 2075 size_t len, 2076 uchar_t prot, 2077 uchar_t maxprot, 2078 uint_t flags, 2079 struct cred *cred) 2080 { 2081 int error = 0; 2082 major_t maj = getmajor(dev); 2083 int map_flag; 2084 int (*segmap)(dev_t, off_t, struct as *, 2085 caddr_t *, off_t, uint_t, uint_t, uint_t, cred_t *); 2086 int (*devmap)(dev_t, devmap_cookie_t, offset_t, 2087 size_t, size_t *, uint_t); 2088 int (*mmap)(dev_t dev, off_t off, int prot); 2089 2090 /* 2091 * Character device: let the device driver 2092 * pick the appropriate segment driver. 2093 * 2094 * 4.x compat.: allow 'NULL' cb_segmap => spec_segmap 2095 * Kindness: allow 'nulldev' cb_segmap => spec_segmap 2096 */ 2097 segmap = devopsp[maj]->devo_cb_ops->cb_segmap; 2098 if (segmap == NULL || segmap == nulldev || segmap == nodev) { 2099 mmap = devopsp[maj]->devo_cb_ops->cb_mmap; 2100 map_flag = devopsp[maj]->devo_cb_ops->cb_flag; 2101 2102 /* 2103 * Use old mmap framework if the driver has both mmap 2104 * and devmap entry points. This is to prevent the 2105 * system from calling invalid devmap entry point 2106 * for some drivers that might have put garbage in the 2107 * devmap entry point. 2108 */ 2109 if ((map_flag & D_DEVMAP) || mmap == NULL || 2110 mmap == nulldev || mmap == nodev) { 2111 devmap = devopsp[maj]->devo_cb_ops->cb_devmap; 2112 2113 /* 2114 * If driver provides devmap entry point in 2115 * cb_ops but not xx_segmap(9E), call 2116 * devmap_setup with default settings 2117 * (NULL) for callback_ops and driver 2118 * callback private data 2119 */ 2120 if (devmap == nodev || devmap == NULL || 2121 devmap == nulldev) 2122 return (ENODEV); 2123 2124 error = devmap_setup(dev, off, as, addrp, 2125 len, prot, maxprot, flags, cred); 2126 2127 return (error); 2128 } else 2129 segmap = spec_segmap; 2130 } else 2131 segmap = cdev_segmap; 2132 2133 return ((*segmap)(dev, (off_t)off, as, addrp, len, prot, 2134 maxprot, flags, cred)); 2135 } 2136 2137 static int 2138 spec_map( 2139 struct vnode *vp, 2140 offset_t off, 2141 struct as *as, 2142 caddr_t *addrp, 2143 size_t len, 2144 uchar_t prot, 2145 uchar_t maxprot, 2146 uint_t flags, 2147 struct cred *cred) 2148 { 2149 int error = 0; 2150 2151 if (vp->v_flag & VNOMAP) 2152 return (ENOSYS); 2153 2154 /* 2155 * If file is locked, fail mapping attempt. 2156 */ 2157 if (vn_has_flocks(vp)) 2158 return (EAGAIN); 2159 2160 if (vp->v_type == VCHR) { 2161 return (spec_char_map(vp->v_rdev, off, as, addrp, len, prot, 2162 maxprot, flags, cred)); 2163 } else if (vp->v_type == VBLK) { 2164 struct segvn_crargs vn_a; 2165 struct vnode *cvp; 2166 struct snode *sp; 2167 2168 /* 2169 * Block device, use segvn mapping to the underlying commonvp 2170 * for pages. 2171 */ 2172 if (off > spec_maxoffset(vp)) 2173 return (ENXIO); 2174 2175 sp = VTOS(vp); 2176 cvp = sp->s_commonvp; 2177 ASSERT(cvp != NULL); 2178 2179 if (off < 0 || (off + len) < 0) 2180 return (ENXIO); 2181 2182 as_rangelock(as); 2183 if ((flags & MAP_FIXED) == 0) { 2184 map_addr(addrp, len, off, 1, flags); 2185 if (*addrp == NULL) { 2186 as_rangeunlock(as); 2187 return (ENOMEM); 2188 } 2189 } else { 2190 /* 2191 * User-specified address; blow away any 2192 * previous mappings. 2193 */ 2194 (void) as_unmap(as, *addrp, len); 2195 } 2196 2197 vn_a.vp = cvp; 2198 vn_a.offset = off; 2199 vn_a.type = flags & MAP_TYPE; 2200 vn_a.prot = (uchar_t)prot; 2201 vn_a.maxprot = (uchar_t)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 } else 2211 return (ENODEV); 2212 2213 return (error); 2214 } 2215 2216 /*ARGSUSED1*/ 2217 static int 2218 spec_addmap( 2219 struct vnode *vp, /* the common vnode */ 2220 offset_t off, 2221 struct as *as, 2222 caddr_t addr, 2223 size_t len, /* how many bytes to add */ 2224 uchar_t prot, 2225 uchar_t maxprot, 2226 uint_t flags, 2227 struct cred *cred) 2228 { 2229 int error = 0; 2230 struct snode *csp = VTOS(vp); 2231 ulong_t npages; 2232 2233 ASSERT(vp != NULL && VTOS(vp)->s_commonvp == vp); 2234 2235 /* 2236 * XXX Given the above assertion, this might not 2237 * be a particularly sensible thing to test. 2238 */ 2239 if (vp->v_flag & VNOMAP) 2240 return (ENOSYS); 2241 2242 npages = btopr(len); 2243 LOCK_CSP(csp); 2244 csp->s_mapcnt += npages; 2245 2246 UNLOCK_CSP(csp); 2247 return (error); 2248 } 2249 2250 /*ARGSUSED1*/ 2251 static int 2252 spec_delmap( 2253 struct vnode *vp, /* the common vnode */ 2254 offset_t off, 2255 struct as *as, 2256 caddr_t addr, 2257 size_t len, /* how many bytes to take away */ 2258 uint_t prot, 2259 uint_t maxprot, 2260 uint_t flags, 2261 struct cred *cred) 2262 { 2263 struct snode *csp = VTOS(vp); 2264 ulong_t npages; 2265 long mcnt; 2266 2267 /* segdev passes us the common vp */ 2268 2269 ASSERT(vp != NULL && VTOS(vp)->s_commonvp == vp); 2270 2271 /* 2272 * XXX Given the above assertion, this might not 2273 * be a particularly sensible thing to test.. 2274 */ 2275 if (vp->v_flag & VNOMAP) 2276 return (ENOSYS); 2277 2278 npages = btopr(len); 2279 2280 LOCK_CSP(csp); 2281 mutex_enter(&csp->s_lock); 2282 mcnt = (csp->s_mapcnt -= npages); 2283 2284 if (mcnt == 0) { 2285 /* 2286 * Call the close routine when the last reference of any 2287 * kind through any [s, v]node goes away. The s_dip hold 2288 * on the devinfo node is released when the vnode is 2289 * destroyed. 2290 */ 2291 if (csp->s_count == 0) { 2292 csp->s_flag &= ~(SNEEDCLOSE | SSIZEVALID); 2293 2294 /* See comment in spec_close() */ 2295 if (csp->s_flag & (SCLONE | SSELFCLONE)) 2296 csp->s_flag &= ~SDIPSET; 2297 2298 mutex_exit(&csp->s_lock); 2299 2300 (void) device_close(vp, 0, cred); 2301 } else 2302 mutex_exit(&csp->s_lock); 2303 2304 mutex_enter(&csp->s_lock); 2305 } 2306 ASSERT(mcnt >= 0); 2307 2308 UNLOCK_CSP_LOCK_HELD(csp); 2309 mutex_exit(&csp->s_lock); 2310 2311 return (0); 2312 } 2313 2314 static int 2315 spec_dump(struct vnode *vp, caddr_t addr, int bn, int count) 2316 { 2317 ASSERT(vp->v_type == VBLK); 2318 return (bdev_dump(vp->v_rdev, addr, bn, count)); 2319 } 2320 2321 2322 /* 2323 * Do i/o on the given page list from/to vp, io_off for io_len. 2324 * Flags are composed of: 2325 * {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED, B_READ, B_WRITE} 2326 * If B_ASYNC is not set i/o is waited for. 2327 */ 2328 /*ARGSUSED5*/ 2329 static int 2330 spec_pageio( 2331 struct vnode *vp, 2332 page_t *pp, 2333 u_offset_t io_off, 2334 size_t io_len, 2335 int flags, 2336 cred_t *cr) 2337 { 2338 struct buf *bp = NULL; 2339 int err = 0; 2340 2341 if (pp == NULL) 2342 return (EINVAL); 2343 2344 bp = spec_startio(vp, pp, io_off, io_len, flags); 2345 2346 /* 2347 * Wait for i/o to complete if the request is not B_ASYNC. 2348 */ 2349 if ((flags & B_ASYNC) == 0) { 2350 err = biowait(bp); 2351 pageio_done(bp); 2352 } 2353 return (err); 2354 } 2355 2356 /* 2357 * Set ACL on underlying vnode if one exists, or return ENOSYS otherwise. 2358 */ 2359 int 2360 spec_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr) 2361 { 2362 struct vnode *realvp; 2363 struct snode *sp = VTOS(vp); 2364 int error; 2365 2366 /* 2367 * The acl(2) system calls VOP_RWLOCK on the file before setting an 2368 * ACL, but since specfs does not serialize reads and writes, this 2369 * VOP does not do anything. However, some backing file systems may 2370 * expect the lock to be held before setting an ACL, so it is taken 2371 * here privately to avoid serializing specfs reads and writes. 2372 */ 2373 if ((realvp = sp->s_realvp) != NULL) { 2374 (void) VOP_RWLOCK(realvp, V_WRITELOCK_TRUE, NULL); 2375 error = VOP_SETSECATTR(realvp, vsap, flag, cr); 2376 (void) VOP_RWUNLOCK(realvp, V_WRITELOCK_TRUE, NULL); 2377 return (error); 2378 } else 2379 return (fs_nosys()); 2380 } 2381 2382 /* 2383 * Get ACL from underlying vnode if one exists, or fabricate it from 2384 * the permissions returned by spec_getattr() otherwise. 2385 */ 2386 int 2387 spec_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr) 2388 { 2389 struct vnode *realvp; 2390 struct snode *sp = VTOS(vp); 2391 2392 if ((realvp = sp->s_realvp) != NULL) 2393 return (VOP_GETSECATTR(realvp, vsap, flag, cr)); 2394 else 2395 return (fs_fab_acl(vp, vsap, flag, cr)); 2396 } 2397 2398 int 2399 spec_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 2400 { 2401 vnode_t *realvp; 2402 struct snode *sp = VTOS(vp); 2403 2404 if ((realvp = sp->s_realvp) != NULL) 2405 return (VOP_PATHCONF(realvp, cmd, valp, cr)); 2406 else 2407 return (fs_pathconf(vp, cmd, valp, cr)); 2408 } 2409