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