xref: /titanic_52/usr/src/uts/common/fs/specfs/specvnops.c (revision 9fb1159054bb89619213a7b2be87ea3bca25fcce)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * University Copyright- Copyright (c) 1982, 1986, 1988
32  * The Regents of the University of California
33  * All Rights Reserved
34  *
35  * University Acknowledgment- Portions of this document are derived from
36  * software developed by the University of California, Berkeley, and its
37  * contributors.
38  */
39 
40 
41 #pragma ident	"%Z%%M%	%I%	%E% SMI"
42 
43 #include <sys/types.h>
44 #include <sys/thread.h>
45 #include <sys/t_lock.h>
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/bitmap.h>
49 #include <sys/buf.h>
50 #include <sys/cmn_err.h>
51 #include <sys/conf.h>
52 #include <sys/ddi.h>
53 #include <sys/debug.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/vnode.h>
67 #include <sys/stat.h>
68 #include <sys/poll.h>
69 #include <sys/stream.h>
70 #include <sys/strsubr.h>
71 #include <sys/policy.h>
72 #include <sys/devpolicy.h>
73 
74 #include <sys/proc.h>
75 #include <sys/user.h>
76 #include <sys/session.h>
77 #include <sys/vmsystm.h>
78 #include <sys/vtrace.h>
79 #include <sys/pathname.h>
80 
81 #include <sys/fs/snode.h>
82 
83 #include <vm/seg.h>
84 #include <vm/seg_map.h>
85 #include <vm/page.h>
86 #include <vm/pvn.h>
87 #include <vm/seg_dev.h>
88 #include <vm/seg_vn.h>
89 
90 #include <fs/fs_subr.h>
91 
92 #include <sys/esunddi.h>
93 #include <sys/autoconf.h>
94 #include <sys/sunndi.h>
95 
96 
97 static int spec_open(struct vnode **, int, struct cred *);
98 static int spec_close(struct vnode *, int, int, offset_t, struct cred *);
99 static int spec_read(struct vnode *, struct uio *, int, struct cred *,
100 	struct caller_context *);
101 static int spec_write(struct vnode *, struct uio *, int, struct cred *,
102 	struct caller_context *);
103 static int spec_ioctl(struct vnode *, int, intptr_t, int, struct cred *, int *);
104 static int spec_getattr(struct vnode *, struct vattr *, int, struct cred *);
105 static int spec_setattr(struct vnode *, struct vattr *, int, struct cred *,
106 	caller_context_t *);
107 static int spec_access(struct vnode *, int, int, struct cred *);
108 static int spec_create(struct vnode *, char *, vattr_t *, enum vcexcl,
109     int, struct vnode **, struct cred *, int);
110 static int spec_fsync(struct vnode *, int, struct cred *);
111 static void spec_inactive(struct vnode *, struct cred *);
112 static int spec_fid(struct vnode *, struct fid *);
113 static int spec_seek(struct vnode *, offset_t, offset_t *);
114 static int spec_frlock(struct vnode *, int, struct flock64 *, int, offset_t,
115     struct flk_callback *, struct cred *);
116 static int spec_realvp(struct vnode *, struct vnode **);
117 
118 static int spec_getpage(struct vnode *, offset_t, size_t, uint_t *, page_t **,
119     size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
120 static int spec_putapage(struct vnode *, page_t *, u_offset_t *, size_t *, int,
121 	struct cred *);
122 static struct buf *spec_startio(struct vnode *, page_t *, u_offset_t, size_t,
123 	int);
124 static int spec_getapage(struct vnode *, u_offset_t, size_t, uint_t *,
125     page_t **, size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
126 static int spec_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
127     uchar_t, uchar_t, uint_t, struct cred *);
128 static int spec_addmap(struct vnode *, offset_t, struct as *, caddr_t, size_t,
129     uchar_t, uchar_t, uint_t, struct cred *);
130 static int spec_delmap(struct vnode *, offset_t, struct as *, caddr_t, size_t,
131     uint_t, uint_t, uint_t, struct cred *);
132 
133 static int spec_poll(struct vnode *, short, int, short *, struct pollhead **);
134 static int spec_dump(struct vnode *, caddr_t, int, int);
135 static int spec_pageio(struct vnode *, page_t *, u_offset_t, size_t, int,
136     cred_t *);
137 
138 static int spec_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
139 static int spec_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
140 static int spec_pathconf(struct	vnode *, int, ulong_t *, struct cred *);
141 
142 #define	SN_HOLD(csp)	{ \
143 	mutex_enter(&csp->s_lock); \
144 	csp->s_count++; \
145 	mutex_exit(&csp->s_lock); \
146 }
147 
148 #define	SN_RELE(csp)	{ \
149 	mutex_enter(&csp->s_lock); \
150 	csp->s_count--; \
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_node_state(csp->s_dip) < DS_ATTACHED)) {
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_node_state(dip) >= DS_ATTACHED));
556 #endif  /* DEBUG */
557 
558 	if ((error = secpolicy_spec_open(cr, cvp, flag)) != 0)
559 		return (error);
560 
561 	SN_HOLD(csp);			/* increment open count */
562 
563 	maj = getmajor(dev);
564 	if (STREAMSTAB(maj))
565 		goto streams_open;
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 		SN_RELE(csp);
641 		return (ENXIO);
642 	}
643 
644 	/*
645 	 * Lock common snode to prevent any new clone opens
646 	 * on this stream while one is in progress.
647 	 * This is necessary since the stream currently
648 	 * associated with the clone device will not be part
649 	 * of it after the clone open completes.
650 	 * Unfortunately we don't know in advance if this is
651 	 * a clone device so we have to lock all opens.
652 	 *
653 	 * If we fail, it's because of an interrupt.
654 	 */
655 	if (LOCK_CSP_SIG(csp) == 0) {
656 		SN_RELE(csp);
657 		return (EINTR);
658 	}
659 
660 	error = stropen(cvp, &newdev, flag, cr);
661 	stp = cvp->v_stream;
662 
663 	/* deal with the clone case */
664 	if ((error == 0) && (dev != newdev)) {
665 		vp->v_stream = cvp->v_stream = NULL;
666 		UNLOCK_CSP(csp);
667 		error = spec_clone(vpp, newdev, vp->v_type, stp);
668 		/*
669 		 * bail on clone failure, further processing
670 		 * results in undefined behaviors.
671 		 */
672 		if (error != 0)
673 			return (error);
674 		sp = VTOS(*vpp);
675 		csp = VTOS(sp->s_commonvp);
676 	} else if (error == 0) {
677 		vp->v_stream = stp;
678 		UNLOCK_CSP(csp);
679 	}
680 
681 	if (error == 0) {
682 		/* STREAMS devices don't have a size */
683 		sp->s_size = csp->s_size = 0;
684 
685 		/*
686 		 * try to allocate it as a controlling terminal
687 		 */
688 		if ((stp->sd_flag & STRISTTY) && !(flag & FNOCTTY)) {
689 			strctty(stp);
690 		}
691 		return (0);
692 	}
693 
694 	/*
695 	 * Deal with stropen failure.
696 	 *
697 	 * sd_flag in the stream head cannot change since the
698 	 * common snode is locked before the call to stropen().
699 	 */
700 	if ((stp != NULL) && (stp->sd_flag & STREOPENFAIL)) {
701 		/*
702 		 * Open failed part way through.
703 		 */
704 		mutex_enter(&stp->sd_lock);
705 		stp->sd_flag &= ~STREOPENFAIL;
706 		mutex_exit(&stp->sd_lock);
707 
708 		UNLOCK_CSP(csp);
709 		(void) spec_close(vp, flag, 1, 0, cr);
710 	} else {
711 		UNLOCK_CSP(csp);
712 		SN_RELE(csp);
713 	}
714 
715 	return (error);
716 }
717 
718 /*ARGSUSED2*/
719 static int
720 spec_close(
721 	struct vnode	*vp,
722 	int		flag,
723 	int		count,
724 	offset_t	offset,
725 	struct cred	*cr)
726 {
727 	struct vnode *cvp;
728 	struct snode *sp, *csp;
729 	enum vtype type;
730 	dev_t dev;
731 	int error = 0;
732 	int sysclone;
733 
734 	if (!(flag & FKLYR)) {
735 		/* this only applies to closes of devices from userland */
736 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
737 		cleanshares(vp, ttoproc(curthread)->p_pid);
738 		if (vp->v_stream)
739 			strclean(vp);
740 	}
741 	if (count > 1)
742 		return (0);
743 
744 	sp = VTOS(vp);
745 	cvp = sp->s_commonvp;
746 
747 	dev = sp->s_dev;
748 	type = vp->v_type;
749 
750 	ASSERT(type == VCHR || type == VBLK);
751 
752 	/*
753 	 * Prevent close/close and close/open races by serializing closes
754 	 * on this common snode. Clone opens are held up until after
755 	 * we have closed this device so the streams linkage is maintained
756 	 */
757 	csp = VTOS(cvp);
758 
759 	LOCK_CSP(csp);
760 	mutex_enter(&csp->s_lock);
761 
762 	csp->s_count--;			/* one fewer open reference : SN_RELE */
763 	sysclone = sp->s_flag & SCLONE;
764 
765 	/*
766 	 * Invalidate size on each close.
767 	 *
768 	 * XXX We do this on each close because we don't have interfaces that
769 	 * allow a driver to invalidate the size.  Since clearing this on each
770 	 * close this causes property overhead we skip /dev/null and
771 	 * /dev/zero to avoid degrading kenbus performance.
772 	 */
773 	if (getmajor(dev) != mm_major)
774 		csp->s_flag &= ~SSIZEVALID;
775 
776 	/*
777 	 * Only call the close routine when the last open reference through
778 	 * any [s, v]node goes away.  This can be checked by looking at
779 	 * s_count on the common vnode.
780 	 */
781 	if ((csp->s_count == 0) && (csp->s_mapcnt == 0)) {
782 		/* we don't need a close */
783 		csp->s_flag &= ~(SNEEDCLOSE | SSIZEVALID);
784 
785 		/*
786 		 * A cloning driver may open-clone to the same dev_t that we
787 		 * are closing before spec_inactive destroys the common snode.
788 		 * If this occurs the s_dip association needs to be reevaluated.
789 		 * We clear SDIPSET to force reevaluation in this case.  When
790 		 * reevaluation occurs (by spec_clone after open), if the
791 		 * devinfo association has changed then the old association
792 		 * will be released as the new association is established by
793 		 * spec_assoc_vp_with_devi().
794 		 */
795 		if (csp->s_flag & (SCLONE | SSELFCLONE))
796 			csp->s_flag &= ~SDIPSET;
797 
798 		mutex_exit(&csp->s_lock);
799 		error = device_close(vp, flag, cr);
800 
801 		/*
802 		 * Decrement the devops held in clnopen()
803 		 */
804 		if (sysclone) {
805 			ddi_rele_driver(getmajor(dev));
806 		}
807 		mutex_enter(&csp->s_lock);
808 	}
809 
810 	UNLOCK_CSP_LOCK_HELD(csp);
811 	mutex_exit(&csp->s_lock);
812 
813 	return (error);
814 }
815 
816 /*ARGSUSED2*/
817 static int
818 spec_read(
819 	struct vnode	*vp,
820 	struct uio	*uiop,
821 	int		ioflag,
822 	struct cred	*cr,
823 	struct caller_context *ct)
824 {
825 	int error;
826 	struct snode *sp = VTOS(vp);
827 	dev_t dev = sp->s_dev;
828 	size_t n;
829 	ulong_t on;
830 	u_offset_t bdevsize;
831 	offset_t maxoff;
832 	offset_t off;
833 	struct vnode *blkvp;
834 
835 	ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
836 
837 	if (STREAMSTAB(getmajor(dev))) {	/* stream */
838 		ASSERT(vp->v_type == VCHR);
839 		smark(sp, SACC);
840 		return (strread(vp, uiop, cr));
841 	}
842 
843 	if (uiop->uio_resid == 0)
844 		return (0);
845 
846 	/*
847 	 * Plain old character devices that set D_U64BIT can have
848 	 * unrestricted offsets.
849 	 */
850 	maxoff = spec_maxoffset(vp);
851 	ASSERT(maxoff != -1 || vp->v_type == VCHR);
852 
853 	if (maxoff != -1 && (uiop->uio_loffset < 0 ||
854 	    uiop->uio_loffset + uiop->uio_resid > maxoff))
855 		return (EINVAL);
856 
857 	if (vp->v_type == VCHR) {
858 		smark(sp, SACC);
859 		ASSERT(STREAMSTAB(getmajor(dev)) == 0);
860 		return (cdev_read(dev, uiop, cr));
861 	}
862 
863 	/*
864 	 * Block device.
865 	 */
866 	error = 0;
867 	blkvp = sp->s_commonvp;
868 	bdevsize = SPEC_SIZE(VTOS(blkvp));
869 
870 	do {
871 		caddr_t base;
872 		offset_t diff;
873 
874 		off = uiop->uio_loffset & (offset_t)MAXBMASK;
875 		on = (size_t)(uiop->uio_loffset & MAXBOFFSET);
876 		n = (size_t)MIN(MAXBSIZE - on, uiop->uio_resid);
877 		diff = bdevsize - uiop->uio_loffset;
878 
879 		if (diff <= 0)
880 			break;
881 		if (diff < n)
882 			n = (size_t)diff;
883 
884 		base = segmap_getmapflt(segkmap, blkvp,
885 			(u_offset_t)(off + on), n, 1, S_READ);
886 
887 		if ((error = uiomove(base + on, n, UIO_READ, uiop)) == 0) {
888 			int flags = 0;
889 			/*
890 			 * If we read a whole block, we won't need this
891 			 * buffer again soon.
892 			 */
893 			if (n + on == MAXBSIZE)
894 				flags = SM_DONTNEED | SM_FREE;
895 			error = segmap_release(segkmap, base, flags);
896 		} else {
897 			(void) segmap_release(segkmap, base, 0);
898 			if (bdevsize == UNKNOWN_SIZE) {
899 				error = 0;
900 				break;
901 			}
902 		}
903 	} while (error == 0 && uiop->uio_resid > 0 && n != 0);
904 
905 	return (error);
906 }
907 
908 /*ARGSUSED*/
909 static int
910 spec_write(
911 	struct vnode *vp,
912 	struct uio *uiop,
913 	int ioflag,
914 	struct cred *cr,
915 	struct caller_context *ct)
916 {
917 	int error;
918 	struct snode *sp = VTOS(vp);
919 	dev_t dev = sp->s_dev;
920 	size_t n;
921 	ulong_t on;
922 	u_offset_t bdevsize;
923 	offset_t maxoff;
924 	offset_t off;
925 	struct vnode *blkvp;
926 
927 	ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
928 
929 	if (STREAMSTAB(getmajor(dev))) {
930 		ASSERT(vp->v_type == VCHR);
931 		smark(sp, SUPD);
932 		return (strwrite(vp, uiop, cr));
933 	}
934 
935 	/*
936 	 * Plain old character devices that set D_U64BIT can have
937 	 * unrestricted offsets.
938 	 */
939 	maxoff = spec_maxoffset(vp);
940 	ASSERT(maxoff != -1 || vp->v_type == VCHR);
941 
942 	if (maxoff != -1 && (uiop->uio_loffset < 0 ||
943 	    uiop->uio_loffset + uiop->uio_resid > maxoff))
944 		return (EINVAL);
945 
946 	if (vp->v_type == VCHR) {
947 		smark(sp, SUPD);
948 		ASSERT(STREAMSTAB(getmajor(dev)) == 0);
949 		return (cdev_write(dev, uiop, cr));
950 	}
951 
952 	if (uiop->uio_resid == 0)
953 		return (0);
954 
955 	error = 0;
956 	blkvp = sp->s_commonvp;
957 	bdevsize = SPEC_SIZE(VTOS(blkvp));
958 
959 	do {
960 		int pagecreate;
961 		int newpage;
962 		caddr_t base;
963 		offset_t diff;
964 
965 		off = uiop->uio_loffset & (offset_t)MAXBMASK;
966 		on = (ulong_t)(uiop->uio_loffset & MAXBOFFSET);
967 		n = (size_t)MIN(MAXBSIZE - on, uiop->uio_resid);
968 		pagecreate = 0;
969 
970 		diff = bdevsize - uiop->uio_loffset;
971 		if (diff <= 0) {
972 			error = ENXIO;
973 			break;
974 		}
975 		if (diff < n)
976 			n = (size_t)diff;
977 
978 		/*
979 		 * Check to see if we can skip reading in the page
980 		 * and just allocate the memory.  We can do this
981 		 * if we are going to rewrite the entire mapping
982 		 * or if we are going to write to end of the device
983 		 * from the beginning of the mapping.
984 		 */
985 		if (n == MAXBSIZE || (on == 0 && (off + n) == bdevsize))
986 			pagecreate = 1;
987 
988 		base = segmap_getmapflt(segkmap, blkvp,
989 		    (u_offset_t)(off + on), n, !pagecreate, S_WRITE);
990 
991 		/*
992 		 * segmap_pagecreate() returns 1 if it calls
993 		 * page_create_va() to allocate any pages.
994 		 */
995 		newpage = 0;
996 
997 		if (pagecreate)
998 			newpage = segmap_pagecreate(segkmap, base + on,
999 				n, 0);
1000 
1001 		error = uiomove(base + on, n, UIO_WRITE, uiop);
1002 
1003 		if (pagecreate &&
1004 		    uiop->uio_loffset <
1005 		    P2ROUNDUP_TYPED(off + on + n, PAGESIZE, offset_t)) {
1006 			/*
1007 			 * We created pages w/o initializing them completely,
1008 			 * thus we need to zero the part that wasn't set up.
1009 			 * This can happen if we write to the end of the device
1010 			 * or if we had some sort of error during the uiomove.
1011 			 */
1012 			long nzero;
1013 			offset_t nmoved;
1014 
1015 			nmoved = (uiop->uio_loffset - (off + on));
1016 			if (nmoved < 0 || nmoved > n) {
1017 				panic("spec_write: nmoved bogus");
1018 				/*NOTREACHED*/
1019 			}
1020 			nzero = (long)P2ROUNDUP(on + n, PAGESIZE) -
1021 			    (on + nmoved);
1022 			if (nzero < 0 || (on + nmoved + nzero > MAXBSIZE)) {
1023 				panic("spec_write: nzero bogus");
1024 				/*NOTREACHED*/
1025 			}
1026 			(void) kzero(base + on + nmoved, (size_t)nzero);
1027 		}
1028 
1029 		/*
1030 		 * Unlock the pages which have been allocated by
1031 		 * page_create_va() in segmap_pagecreate().
1032 		 */
1033 		if (newpage)
1034 			segmap_pageunlock(segkmap, base + on,
1035 				(size_t)n, S_WRITE);
1036 
1037 		if (error == 0) {
1038 			int flags = 0;
1039 
1040 			/*
1041 			 * Force write back for synchronous write cases.
1042 			 */
1043 			if (ioflag & (FSYNC|FDSYNC))
1044 				flags = SM_WRITE;
1045 			else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
1046 				/*
1047 				 * Have written a whole block.
1048 				 * Start an asynchronous write and
1049 				 * mark the buffer to indicate that
1050 				 * it won't be needed again soon.
1051 				 * Push swap files here, since it
1052 				 * won't happen anywhere else.
1053 				 */
1054 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
1055 			}
1056 			smark(sp, SUPD|SCHG);
1057 			error = segmap_release(segkmap, base, flags);
1058 		} else
1059 			(void) segmap_release(segkmap, base, SM_INVAL);
1060 
1061 	} while (error == 0 && uiop->uio_resid > 0 && n != 0);
1062 
1063 	return (error);
1064 }
1065 
1066 static int
1067 spec_ioctl(struct vnode *vp, int cmd, intptr_t arg, int mode, struct cred *cr,
1068     int *rvalp)
1069 {
1070 	struct snode *sp;
1071 	dev_t dev;
1072 	int error;
1073 
1074 	if (vp->v_type != VCHR)
1075 		return (ENOTTY);
1076 	sp = VTOS(vp);
1077 	dev = sp->s_dev;
1078 	if (STREAMSTAB(getmajor(dev))) {
1079 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
1080 	} else {
1081 		error = cdev_ioctl(dev, cmd, arg, mode, cr, rvalp);
1082 	}
1083 	return (error);
1084 }
1085 
1086 static int
1087 spec_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr)
1088 {
1089 	int error;
1090 	struct snode *sp;
1091 	struct vnode *realvp;
1092 
1093 	/* With ATTR_COMM we will not get attributes from realvp */
1094 	if (flags & ATTR_COMM) {
1095 		sp = VTOS(vp);
1096 		vp = sp->s_commonvp;
1097 	}
1098 	sp = VTOS(vp);
1099 	realvp = sp->s_realvp;
1100 
1101 	if (realvp == NULL) {
1102 		static int snode_shift	= 0;
1103 
1104 		/*
1105 		 * Calculate the amount of bitshift to a snode pointer which
1106 		 * will still keep it unique.  See below.
1107 		 */
1108 		if (snode_shift == 0)
1109 			snode_shift = highbit(sizeof (struct snode));
1110 		ASSERT(snode_shift > 0);
1111 
1112 		/*
1113 		 * No real vnode behind this one.  Fill in the fields
1114 		 * from the snode.
1115 		 *
1116 		 * This code should be refined to return only the
1117 		 * attributes asked for instead of all of them.
1118 		 */
1119 		vap->va_type = vp->v_type;
1120 		vap->va_mode = 0;
1121 		vap->va_uid = vap->va_gid = 0;
1122 		vap->va_fsid = sp->s_fsid;
1123 
1124 		/*
1125 		 * If the va_nodeid is > MAX_USHORT, then i386 stats might
1126 		 * fail. So we shift down the snode pointer to try and get
1127 		 * the most uniqueness into 16-bits.
1128 		 */
1129 		vap->va_nodeid = ((ino64_t)(uintptr_t)sp >> snode_shift) &
1130 		    0xFFFF;
1131 		vap->va_nlink = 0;
1132 		vap->va_rdev = sp->s_dev;
1133 
1134 		/*
1135 		 * va_nblocks is the number of 512 byte blocks used to store
1136 		 * the mknod for the device, not the number of blocks on the
1137 		 * device itself.  This is typically zero since the mknod is
1138 		 * represented directly in the inode itself.
1139 		 */
1140 		vap->va_nblocks = 0;
1141 	} else {
1142 		error = VOP_GETATTR(realvp, vap, flags, cr);
1143 		if (error != 0)
1144 			return (error);
1145 	}
1146 
1147 	/* set the size from the snode */
1148 	vap->va_size = SPEC_SIZE(VTOS(sp->s_commonvp));
1149 	vap->va_blksize = MAXBSIZE;
1150 
1151 	mutex_enter(&sp->s_lock);
1152 	vap->va_atime.tv_sec = sp->s_atime;
1153 	vap->va_mtime.tv_sec = sp->s_mtime;
1154 	vap->va_ctime.tv_sec = sp->s_ctime;
1155 	mutex_exit(&sp->s_lock);
1156 
1157 	vap->va_atime.tv_nsec = 0;
1158 	vap->va_mtime.tv_nsec = 0;
1159 	vap->va_ctime.tv_nsec = 0;
1160 	vap->va_seq = 0;
1161 
1162 	return (0);
1163 }
1164 
1165 static int
1166 spec_setattr(
1167 	struct vnode *vp,
1168 	struct vattr *vap,
1169 	int flags,
1170 	struct cred *cr,
1171 	caller_context_t *ctp)
1172 {
1173 	struct snode *sp = VTOS(vp);
1174 	struct vnode *realvp;
1175 	int error;
1176 
1177 	if (vp->v_type == VCHR && vp->v_stream && (vap->va_mask & AT_SIZE)) {
1178 		/*
1179 		 * 1135080:	O_TRUNC should have no effect on
1180 		 *		named pipes and terminal devices.
1181 		 */
1182 		ASSERT(vap->va_mask == AT_SIZE);
1183 		return (0);
1184 	}
1185 
1186 	if ((realvp = sp->s_realvp) == NULL)
1187 		error = 0;	/* no real vnode to update */
1188 	else
1189 		error = VOP_SETATTR(realvp, vap, flags, cr, ctp);
1190 	if (error == 0) {
1191 		/*
1192 		 * If times were changed, update snode.
1193 		 */
1194 		mutex_enter(&sp->s_lock);
1195 		if (vap->va_mask & AT_ATIME)
1196 			sp->s_atime = vap->va_atime.tv_sec;
1197 		if (vap->va_mask & AT_MTIME) {
1198 			sp->s_mtime = vap->va_mtime.tv_sec;
1199 			sp->s_ctime = gethrestime_sec();
1200 		}
1201 		mutex_exit(&sp->s_lock);
1202 	}
1203 	return (error);
1204 }
1205 
1206 static int
1207 spec_access(struct vnode *vp, int mode, int flags, struct cred *cr)
1208 {
1209 	struct vnode *realvp;
1210 	struct snode *sp = VTOS(vp);
1211 
1212 	if ((realvp = sp->s_realvp) != NULL)
1213 		return (VOP_ACCESS(realvp, mode, flags, cr));
1214 	else
1215 		return (0);	/* Allow all access. */
1216 }
1217 
1218 /*
1219  * This can be called if creat or an open with O_CREAT is done on the root
1220  * of a lofs mount where the mounted entity is a special file.
1221  */
1222 /*ARGSUSED*/
1223 static int
1224 spec_create(struct vnode *dvp, char *name, vattr_t *vap, enum vcexcl excl,
1225     int mode, struct vnode **vpp, struct cred *cr, int flag)
1226 {
1227 	int error;
1228 
1229 	ASSERT(dvp && (dvp->v_flag & VROOT) && *name == '\0');
1230 	if (excl == NONEXCL) {
1231 		if (mode && (error = spec_access(dvp, mode, 0, cr)))
1232 			return (error);
1233 		VN_HOLD(dvp);
1234 		return (0);
1235 	}
1236 	return (EEXIST);
1237 }
1238 
1239 /*
1240  * In order to sync out the snode times without multi-client problems,
1241  * make sure the times written out are never earlier than the times
1242  * already set in the vnode.
1243  */
1244 static int
1245 spec_fsync(struct vnode *vp, int syncflag, struct cred *cr)
1246 {
1247 	struct snode *sp = VTOS(vp);
1248 	struct vnode *realvp;
1249 	struct vnode *cvp;
1250 	struct vattr va, vatmp;
1251 
1252 	/* If times didn't change, don't flush anything. */
1253 	mutex_enter(&sp->s_lock);
1254 	if ((sp->s_flag & (SACC|SUPD|SCHG)) == 0 && vp->v_type != VBLK) {
1255 		mutex_exit(&sp->s_lock);
1256 		return (0);
1257 	}
1258 	sp->s_flag &= ~(SACC|SUPD|SCHG);
1259 	mutex_exit(&sp->s_lock);
1260 	cvp = sp->s_commonvp;
1261 	realvp = sp->s_realvp;
1262 
1263 	if (vp->v_type == VBLK && cvp != vp && vn_has_cached_data(cvp) &&
1264 	    (cvp->v_flag & VISSWAP) == 0)
1265 		(void) VOP_PUTPAGE(cvp, (offset_t)0, 0, 0, cr);
1266 
1267 	/*
1268 	 * If no real vnode to update, don't flush anything.
1269 	 */
1270 	if (realvp == NULL)
1271 		return (0);
1272 
1273 	vatmp.va_mask = AT_ATIME|AT_MTIME;
1274 	if (VOP_GETATTR(realvp, &vatmp, 0, cr) == 0) {
1275 
1276 		mutex_enter(&sp->s_lock);
1277 		if (vatmp.va_atime.tv_sec > sp->s_atime)
1278 			va.va_atime = vatmp.va_atime;
1279 		else {
1280 			va.va_atime.tv_sec = sp->s_atime;
1281 			va.va_atime.tv_nsec = 0;
1282 		}
1283 		if (vatmp.va_mtime.tv_sec > sp->s_mtime)
1284 			va.va_mtime = vatmp.va_mtime;
1285 		else {
1286 			va.va_mtime.tv_sec = sp->s_mtime;
1287 			va.va_mtime.tv_nsec = 0;
1288 		}
1289 		mutex_exit(&sp->s_lock);
1290 
1291 		va.va_mask = AT_ATIME|AT_MTIME;
1292 		(void) VOP_SETATTR(realvp, &va, 0, cr, NULL);
1293 	}
1294 	(void) VOP_FSYNC(realvp, syncflag, cr);
1295 	return (0);
1296 }
1297 
1298 /*ARGSUSED*/
1299 static void
1300 spec_inactive(struct vnode *vp, struct cred *cr)
1301 {
1302 	struct snode *sp = VTOS(vp);
1303 	struct vnode *cvp;
1304 	struct vnode *rvp;
1305 
1306 	/*
1307 	 * If no one has reclaimed the vnode, remove from the
1308 	 * cache now.
1309 	 */
1310 	if (vp->v_count < 1) {
1311 		panic("spec_inactive: Bad v_count");
1312 		/*NOTREACHED*/
1313 	}
1314 	mutex_enter(&stable_lock);
1315 
1316 	mutex_enter(&vp->v_lock);
1317 	/*
1318 	 * Drop the temporary hold by vn_rele now
1319 	 */
1320 	if (--vp->v_count != 0) {
1321 		mutex_exit(&vp->v_lock);
1322 		mutex_exit(&stable_lock);
1323 		return;
1324 	}
1325 	mutex_exit(&vp->v_lock);
1326 
1327 	sdelete(sp);
1328 	mutex_exit(&stable_lock);
1329 
1330 	/* We are the sole owner of sp now */
1331 	cvp = sp->s_commonvp;
1332 	rvp = sp->s_realvp;
1333 
1334 	if (rvp) {
1335 		/*
1336 		 * If the snode times changed, then update the times
1337 		 * associated with the "realvp".
1338 		 */
1339 		if ((sp->s_flag & (SACC|SUPD|SCHG)) != 0) {
1340 
1341 			struct vattr va, vatmp;
1342 
1343 			mutex_enter(&sp->s_lock);
1344 			sp->s_flag &= ~(SACC|SUPD|SCHG);
1345 			mutex_exit(&sp->s_lock);
1346 			vatmp.va_mask = AT_ATIME|AT_MTIME;
1347 			/*
1348 			 * The user may not own the device, but we
1349 			 * want to update the attributes anyway.
1350 			 */
1351 			if (VOP_GETATTR(rvp, &vatmp, 0, kcred) == 0) {
1352 				if (vatmp.va_atime.tv_sec > sp->s_atime)
1353 					va.va_atime = vatmp.va_atime;
1354 				else {
1355 					va.va_atime.tv_sec = sp->s_atime;
1356 					va.va_atime.tv_nsec = 0;
1357 				}
1358 				if (vatmp.va_mtime.tv_sec > sp->s_mtime)
1359 					va.va_mtime = vatmp.va_mtime;
1360 				else {
1361 					va.va_mtime.tv_sec = sp->s_mtime;
1362 					va.va_mtime.tv_nsec = 0;
1363 				}
1364 
1365 				va.va_mask = AT_ATIME|AT_MTIME;
1366 				(void) VOP_SETATTR(rvp, &va, 0, kcred, NULL);
1367 			}
1368 		}
1369 	}
1370 	ASSERT(!vn_has_cached_data(vp));
1371 	vn_invalid(vp);
1372 
1373 	/* if we are sharing another file systems vfs, release it */
1374 	if (vp->v_vfsp && (vp->v_vfsp != &spec_vfs))
1375 		VFS_RELE(vp->v_vfsp);
1376 
1377 	/* if we have a realvp, release the realvp */
1378 	if (rvp)
1379 		VN_RELE(rvp);
1380 
1381 	/* if we have a common, release the common */
1382 	if (cvp && (cvp != vp))
1383 		VN_RELE(cvp);
1384 
1385 	/*
1386 	 * if we have a hold on a devinfo node (established by
1387 	 * spec_assoc_vp_with_devi), release the hold
1388 	 */
1389 	if (sp->s_dip)
1390 		ddi_release_devi(sp->s_dip);
1391 
1392 	/*
1393 	 * If we have an associated device policy, release it.
1394 	 */
1395 	if (sp->s_plcy != NULL)
1396 		dpfree(sp->s_plcy);
1397 
1398 	/*
1399 	 * If all holds on the devinfo node are through specfs/devfs
1400 	 * and we just destroyed the last specfs node associated with the
1401 	 * device, then the devinfo node reference count should now be
1402 	 * zero.  We can't check this because there may be other holds
1403 	 * on the node from non file system sources: ddi_hold_devi_by_instance
1404 	 * for example.
1405 	 */
1406 	kmem_cache_free(snode_cache, sp);
1407 }
1408 
1409 static int
1410 spec_fid(struct vnode *vp, struct fid *fidp)
1411 {
1412 	struct vnode *realvp;
1413 	struct snode *sp = VTOS(vp);
1414 
1415 	if ((realvp = sp->s_realvp) != NULL)
1416 		return (VOP_FID(realvp, fidp));
1417 	else
1418 		return (EINVAL);
1419 }
1420 
1421 /*ARGSUSED1*/
1422 static int
1423 spec_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
1424 {
1425 	offset_t maxoff = spec_maxoffset(vp);
1426 
1427 	if (maxoff == -1 || *noffp <= maxoff)
1428 		return (0);
1429 	else
1430 		return (EINVAL);
1431 }
1432 
1433 static int
1434 spec_frlock(
1435 	struct vnode *vp,
1436 	int		cmd,
1437 	struct flock64	*bfp,
1438 	int		flag,
1439 	offset_t	offset,
1440 	struct flk_callback *flk_cbp,
1441 	struct cred	*cr)
1442 {
1443 	struct snode *sp = VTOS(vp);
1444 	struct snode *csp;
1445 
1446 	csp = VTOS(sp->s_commonvp);
1447 	/*
1448 	 * If file is being mapped, disallow frlock.
1449 	 */
1450 	if (csp->s_mapcnt > 0)
1451 		return (EAGAIN);
1452 
1453 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr));
1454 }
1455 
1456 static int
1457 spec_realvp(struct vnode *vp, struct vnode **vpp)
1458 {
1459 	struct vnode *rvp;
1460 
1461 	if ((rvp = VTOS(vp)->s_realvp) != NULL) {
1462 		vp = rvp;
1463 		if (VOP_REALVP(vp, &rvp) == 0)
1464 			vp = rvp;
1465 	}
1466 
1467 	*vpp = vp;
1468 	return (0);
1469 }
1470 
1471 /*
1472  * Return all the pages from [off..off + len] in block
1473  * or character device.
1474  */
1475 static int
1476 spec_getpage(
1477 	struct vnode	*vp,
1478 	offset_t	off,
1479 	size_t		len,
1480 	uint_t		*protp,
1481 	page_t		*pl[],
1482 	size_t		plsz,
1483 	struct seg	*seg,
1484 	caddr_t		addr,
1485 	enum seg_rw	rw,
1486 	struct cred	*cr)
1487 {
1488 	struct snode *sp = VTOS(vp);
1489 	int err;
1490 
1491 	ASSERT(sp->s_commonvp == vp);
1492 
1493 	/*
1494 	 * XXX	Given the above assertion, this might not do
1495 	 *	what is wanted here.
1496 	 */
1497 	if (vp->v_flag & VNOMAP)
1498 		return (ENOSYS);
1499 	TRACE_4(TR_FAC_SPECFS, TR_SPECFS_GETPAGE,
1500 		"specfs getpage:vp %p off %llx len %ld snode %p",
1501 		vp, off, len, sp);
1502 
1503 	switch (vp->v_type) {
1504 	case VBLK:
1505 		if (protp != NULL)
1506 			*protp = PROT_ALL;
1507 
1508 		if (((u_offset_t)off + len) > (SPEC_SIZE(sp) + PAGEOFFSET))
1509 			return (EFAULT);	/* beyond EOF */
1510 
1511 		if (len <= PAGESIZE)
1512 			err = spec_getapage(vp, (u_offset_t)off, len, protp, pl,
1513 			    plsz, seg, addr, rw, cr);
1514 		else
1515 			err = pvn_getpages(spec_getapage, vp, (u_offset_t)off,
1516 			    len, protp, pl, plsz, seg, addr, rw, cr);
1517 		break;
1518 
1519 	case VCHR:
1520 		cmn_err(CE_NOTE, "spec_getpage called for character device. "
1521 		    "Check any non-ON consolidation drivers");
1522 		err = 0;
1523 		pl[0] = (page_t *)0;
1524 		break;
1525 
1526 	default:
1527 		panic("spec_getpage: bad v_type 0x%x", vp->v_type);
1528 		/*NOTREACHED*/
1529 	}
1530 
1531 	return (err);
1532 }
1533 
1534 extern int klustsize;	/* set in machdep.c */
1535 
1536 int spec_ra = 1;
1537 int spec_lostpage;	/* number of times we lost original page */
1538 
1539 /*ARGSUSED2*/
1540 static int
1541 spec_getapage(
1542 	struct vnode *vp,
1543 	u_offset_t	off,
1544 	size_t		len,
1545 	uint_t		*protp,
1546 	page_t		*pl[],
1547 	size_t		plsz,
1548 	struct seg	*seg,
1549 	caddr_t		addr,
1550 	enum seg_rw	rw,
1551 	struct cred	*cr)
1552 {
1553 	struct snode *sp;
1554 	struct buf *bp;
1555 	page_t *pp, *pp2;
1556 	u_offset_t io_off1, io_off2;
1557 	size_t io_len1;
1558 	size_t io_len2;
1559 	size_t blksz;
1560 	u_offset_t blkoff;
1561 	int dora, err;
1562 	page_t *pagefound;
1563 	uint_t xlen;
1564 	size_t adj_klustsize;
1565 	u_offset_t size;
1566 	u_offset_t tmpoff;
1567 
1568 	sp = VTOS(vp);
1569 	TRACE_3(TR_FAC_SPECFS, TR_SPECFS_GETAPAGE,
1570 		"specfs getapage:vp %p off %llx snode %p", vp, off, sp);
1571 reread:
1572 
1573 	err = 0;
1574 	bp = NULL;
1575 	pp = NULL;
1576 	pp2 = NULL;
1577 
1578 	if (pl != NULL)
1579 		pl[0] = NULL;
1580 
1581 	size = SPEC_SIZE(VTOS(sp->s_commonvp));
1582 
1583 	if (spec_ra && sp->s_nextr == off)
1584 		dora = 1;
1585 	else
1586 		dora = 0;
1587 
1588 	if (size == UNKNOWN_SIZE) {
1589 		dora = 0;
1590 		adj_klustsize = PAGESIZE;
1591 	} else {
1592 		adj_klustsize = dora ? klustsize : PAGESIZE;
1593 	}
1594 
1595 again:
1596 	if ((pagefound = page_exists(vp, off)) == NULL) {
1597 		if (rw == S_CREATE) {
1598 			/*
1599 			 * We're allocating a swap slot and it's
1600 			 * associated page was not found, so allocate
1601 			 * and return it.
1602 			 */
1603 			if ((pp = page_create_va(vp, off,
1604 			    PAGESIZE, PG_WAIT, seg, addr)) == NULL) {
1605 				panic("spec_getapage: page_create");
1606 				/*NOTREACHED*/
1607 			}
1608 			io_len1 = PAGESIZE;
1609 			sp->s_nextr = off + PAGESIZE;
1610 		} else {
1611 			/*
1612 			 * Need to really do disk I/O to get the page(s).
1613 			 */
1614 			blkoff = (off / adj_klustsize) * adj_klustsize;
1615 			if (size == UNKNOWN_SIZE) {
1616 				blksz = PAGESIZE;
1617 			} else {
1618 				if (blkoff + adj_klustsize <= size)
1619 					blksz = adj_klustsize;
1620 				else
1621 					blksz =
1622 					    MIN(size - blkoff, adj_klustsize);
1623 			}
1624 
1625 			pp = pvn_read_kluster(vp, off, seg, addr, &tmpoff,
1626 			    &io_len1, blkoff, blksz, 0);
1627 			io_off1 = tmpoff;
1628 			/*
1629 			 * Make sure the page didn't sneek into the
1630 			 * cache while we blocked in pvn_read_kluster.
1631 			 */
1632 			if (pp == NULL)
1633 				goto again;
1634 
1635 			/*
1636 			 * Zero part of page which we are not
1637 			 * going to be reading from disk now.
1638 			 */
1639 			xlen = (uint_t)(io_len1 & PAGEOFFSET);
1640 			if (xlen != 0)
1641 				pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
1642 
1643 			bp = spec_startio(vp, pp, io_off1, io_len1,
1644 			    pl == NULL ? (B_ASYNC | B_READ) : B_READ);
1645 			sp->s_nextr = io_off1 + io_len1;
1646 		}
1647 	}
1648 
1649 	if (dora && rw != S_CREATE) {
1650 		u_offset_t off2;
1651 		caddr_t addr2;
1652 
1653 		off2 = ((off / adj_klustsize) + 1) * adj_klustsize;
1654 		addr2 = addr + (off2 - off);
1655 
1656 		pp2 = NULL;
1657 		/*
1658 		 * If we are past EOF then don't bother trying
1659 		 * with read-ahead.
1660 		 */
1661 		if (off2 >= size)
1662 			pp2 = NULL;
1663 		else {
1664 			if (off2 + adj_klustsize <= size)
1665 				blksz = adj_klustsize;
1666 			else
1667 				blksz = MIN(size - off2, adj_klustsize);
1668 
1669 			pp2 = pvn_read_kluster(vp, off2, seg, addr2, &tmpoff,
1670 			    &io_len2, off2, blksz, 1);
1671 			io_off2 = tmpoff;
1672 		}
1673 
1674 		if (pp2 != NULL) {
1675 			/*
1676 			 * Zero part of page which we are not
1677 			 * going to be reading from disk now.
1678 			 */
1679 			xlen = (uint_t)(io_len2 & PAGEOFFSET);
1680 			if (xlen != 0)
1681 				pagezero(pp2->p_prev, xlen, PAGESIZE - xlen);
1682 
1683 			(void) spec_startio(vp, pp2, io_off2, io_len2,
1684 			    B_READ | B_ASYNC);
1685 		}
1686 	}
1687 
1688 	if (pl == NULL)
1689 		return (err);
1690 
1691 	if (bp != NULL) {
1692 		err = biowait(bp);
1693 		pageio_done(bp);
1694 
1695 		if (err) {
1696 			if (pp != NULL)
1697 				pvn_read_done(pp, B_ERROR);
1698 			return (err);
1699 		}
1700 	}
1701 
1702 	if (pagefound) {
1703 		se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
1704 		/*
1705 		 * Page exists in the cache, acquire the appropriate
1706 		 * lock.  If this fails, start all over again.
1707 		 */
1708 
1709 		if ((pp = page_lookup(vp, off, se)) == NULL) {
1710 			spec_lostpage++;
1711 			goto reread;
1712 		}
1713 		pl[0] = pp;
1714 		pl[1] = NULL;
1715 
1716 		sp->s_nextr = off + PAGESIZE;
1717 		return (0);
1718 	}
1719 
1720 	if (pp != NULL)
1721 		pvn_plist_init(pp, pl, plsz, off, io_len1, rw);
1722 	return (0);
1723 }
1724 
1725 /*
1726  * Flags are composed of {B_INVAL, B_DIRTY B_FREE, B_DONTNEED, B_FORCE}.
1727  * If len == 0, do from off to EOF.
1728  *
1729  * The normal cases should be len == 0 & off == 0 (entire vp list),
1730  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
1731  * (from pageout).
1732  */
1733 int
1734 spec_putpage(
1735 	struct vnode *vp,
1736 	offset_t	off,
1737 	size_t		len,
1738 	int		flags,
1739 	struct cred	*cr)
1740 {
1741 	struct snode *sp = VTOS(vp);
1742 	struct vnode *cvp;
1743 	page_t *pp;
1744 	u_offset_t io_off;
1745 	size_t io_len = 0;	/* for lint */
1746 	int err = 0;
1747 	u_offset_t size;
1748 	u_offset_t tmpoff;
1749 
1750 	ASSERT(vp->v_count != 0);
1751 
1752 	if (vp->v_flag & VNOMAP)
1753 		return (ENOSYS);
1754 
1755 	cvp = sp->s_commonvp;
1756 	size = SPEC_SIZE(VTOS(cvp));
1757 
1758 	if (!vn_has_cached_data(vp) || off >= size)
1759 		return (0);
1760 
1761 	ASSERT(vp->v_type == VBLK && cvp == vp);
1762 	TRACE_4(TR_FAC_SPECFS, TR_SPECFS_PUTPAGE,
1763 		"specfs putpage:vp %p off %llx len %ld snode %p",
1764 		vp, off, len, sp);
1765 
1766 	if (len == 0) {
1767 		/*
1768 		 * Search the entire vp list for pages >= off.
1769 		 */
1770 		err = pvn_vplist_dirty(vp, off, spec_putapage,
1771 		    flags, cr);
1772 	} else {
1773 		u_offset_t eoff;
1774 
1775 		/*
1776 		 * Loop over all offsets in the range [off...off + len]
1777 		 * looking for pages to deal with.  We set limits so
1778 		 * that we kluster to klustsize boundaries.
1779 		 */
1780 		eoff = off + len;
1781 		for (io_off = off; io_off < eoff && io_off < size;
1782 		    io_off += io_len) {
1783 			/*
1784 			 * If we are not invalidating, synchronously
1785 			 * freeing or writing pages use the routine
1786 			 * page_lookup_nowait() to prevent reclaiming
1787 			 * them from the free list.
1788 			 */
1789 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
1790 				pp = page_lookup(vp, io_off,
1791 					(flags & (B_INVAL | B_FREE)) ?
1792 					    SE_EXCL : SE_SHARED);
1793 			} else {
1794 				pp = page_lookup_nowait(vp, io_off,
1795 					(flags & B_FREE) ? SE_EXCL : SE_SHARED);
1796 			}
1797 
1798 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
1799 				io_len = PAGESIZE;
1800 			else {
1801 				err = spec_putapage(vp, pp, &tmpoff, &io_len,
1802 				    flags, cr);
1803 				io_off = tmpoff;
1804 				if (err != 0)
1805 					break;
1806 				/*
1807 				 * "io_off" and "io_len" are returned as
1808 				 * the range of pages we actually wrote.
1809 				 * This allows us to skip ahead more quickly
1810 				 * since several pages may've been dealt
1811 				 * with by this iteration of the loop.
1812 				 */
1813 			}
1814 		}
1815 	}
1816 	return (err);
1817 }
1818 
1819 
1820 /*
1821  * Write out a single page, possibly klustering adjacent
1822  * dirty pages.
1823  */
1824 /*ARGSUSED5*/
1825 static int
1826 spec_putapage(
1827 	struct vnode	*vp,
1828 	page_t		*pp,
1829 	u_offset_t	*offp,		/* return value */
1830 	size_t		*lenp,		/* return value */
1831 	int		flags,
1832 	struct cred	*cr)
1833 {
1834 	struct snode *sp = VTOS(vp);
1835 	u_offset_t io_off;
1836 	size_t io_len;
1837 	size_t blksz;
1838 	u_offset_t blkoff;
1839 	int err = 0;
1840 	struct buf *bp;
1841 	u_offset_t size;
1842 	size_t adj_klustsize;
1843 	u_offset_t tmpoff;
1844 
1845 	/*
1846 	 * Destroy read ahead value since we are really going to write.
1847 	 */
1848 	sp->s_nextr = 0;
1849 	size = SPEC_SIZE(VTOS(sp->s_commonvp));
1850 
1851 	adj_klustsize = klustsize;
1852 
1853 	blkoff = (pp->p_offset / adj_klustsize) * adj_klustsize;
1854 
1855 	if (blkoff + adj_klustsize <= size)
1856 		blksz = adj_klustsize;
1857 	else
1858 		blksz = size - blkoff;
1859 
1860 	/*
1861 	 * Find a kluster that fits in one contiguous chunk.
1862 	 */
1863 	pp = pvn_write_kluster(vp, pp, &tmpoff, &io_len, blkoff,
1864 		blksz, flags);
1865 	io_off = tmpoff;
1866 
1867 	/*
1868 	 * Check for page length rounding problems
1869 	 * XXX - Is this necessary?
1870 	 */
1871 	if (io_off + io_len > size) {
1872 		ASSERT((io_off + io_len) - size < PAGESIZE);
1873 		io_len = size - io_off;
1874 	}
1875 
1876 	bp = spec_startio(vp, pp, io_off, io_len, B_WRITE | flags);
1877 
1878 	/*
1879 	 * Wait for i/o to complete if the request is not B_ASYNC.
1880 	 */
1881 	if ((flags & B_ASYNC) == 0) {
1882 		err = biowait(bp);
1883 		pageio_done(bp);
1884 		pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
1885 	}
1886 
1887 	if (offp)
1888 		*offp = io_off;
1889 	if (lenp)
1890 		*lenp = io_len;
1891 	TRACE_4(TR_FAC_SPECFS, TR_SPECFS_PUTAPAGE,
1892 		"specfs putapage:vp %p offp %p snode %p err %d",
1893 		vp, offp, sp, err);
1894 	return (err);
1895 }
1896 
1897 /*
1898  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
1899  */
1900 static struct buf *
1901 spec_startio(
1902 	struct vnode *vp,
1903 	page_t		*pp,
1904 	u_offset_t	io_off,
1905 	size_t		io_len,
1906 	int		flags)
1907 {
1908 	struct buf *bp;
1909 
1910 	bp = pageio_setup(pp, io_len, vp, flags);
1911 
1912 	bp->b_edev = vp->v_rdev;
1913 	bp->b_dev = cmpdev(vp->v_rdev);
1914 	bp->b_blkno = btodt(io_off);
1915 	bp->b_un.b_addr = (caddr_t)0;
1916 
1917 	(void) bdev_strategy(bp);
1918 
1919 	if (flags & B_READ)
1920 		lwp_stat_update(LWP_STAT_INBLK, 1);
1921 	else
1922 		lwp_stat_update(LWP_STAT_OUBLK, 1);
1923 
1924 	return (bp);
1925 }
1926 
1927 static int
1928 spec_poll(
1929 	struct vnode	*vp,
1930 	short		events,
1931 	int		anyyet,
1932 	short		*reventsp,
1933 	struct pollhead **phpp)
1934 {
1935 	dev_t dev;
1936 	int error;
1937 
1938 	if (vp->v_type == VBLK)
1939 		error = fs_poll(vp, events, anyyet, reventsp, phpp);
1940 	else {
1941 		ASSERT(vp->v_type == VCHR);
1942 		dev = vp->v_rdev;
1943 		if (STREAMSTAB(getmajor(dev))) {
1944 			ASSERT(vp->v_stream != NULL);
1945 			error = strpoll(vp->v_stream, events, anyyet,
1946 			    reventsp, phpp);
1947 		} else if (devopsp[getmajor(dev)]->devo_cb_ops->cb_chpoll) {
1948 			error = cdev_poll(dev, events, anyyet, reventsp, phpp);
1949 		} else {
1950 			error = fs_poll(vp, events, anyyet, reventsp, phpp);
1951 		}
1952 	}
1953 	return (error);
1954 }
1955 
1956 /*
1957  * This routine is called through the cdevsw[] table to handle
1958  * traditional mmap'able devices that support a d_mmap function.
1959  */
1960 /*ARGSUSED8*/
1961 int
1962 spec_segmap(
1963 	dev_t dev,
1964 	off_t off,
1965 	struct as *as,
1966 	caddr_t *addrp,
1967 	off_t len,
1968 	uint_t prot,
1969 	uint_t maxprot,
1970 	uint_t flags,
1971 	struct cred *cred)
1972 {
1973 	struct segdev_crargs dev_a;
1974 	int (*mapfunc)(dev_t dev, off_t off, int prot);
1975 	size_t i;
1976 	int	error;
1977 
1978 	if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev)
1979 		return (ENODEV);
1980 	TRACE_4(TR_FAC_SPECFS, TR_SPECFS_SEGMAP,
1981 		"specfs segmap:dev %x as %p len %lx prot %x",
1982 		dev, as, len, prot);
1983 
1984 	/*
1985 	 * Character devices that support the d_mmap
1986 	 * interface can only be mmap'ed shared.
1987 	 */
1988 	if ((flags & MAP_TYPE) != MAP_SHARED)
1989 		return (EINVAL);
1990 
1991 	/*
1992 	 * Check to ensure that the entire range is
1993 	 * legal and we are not trying to map in
1994 	 * more than the device will let us.
1995 	 */
1996 	for (i = 0; i < len; i += PAGESIZE) {
1997 		if (cdev_mmap(mapfunc, dev, off + i, maxprot) == -1)
1998 			return (ENXIO);
1999 	}
2000 
2001 	as_rangelock(as);
2002 	if ((flags & MAP_FIXED) == 0) {
2003 		/*
2004 		 * Pick an address w/o worrying about
2005 		 * any vac alignment constraints.
2006 		 */
2007 		map_addr(addrp, len, (offset_t)off, 0, flags);
2008 		if (*addrp == NULL) {
2009 			as_rangeunlock(as);
2010 			return (ENOMEM);
2011 		}
2012 	} else {
2013 		/*
2014 		 * User-specified address; blow away any previous mappings.
2015 		 */
2016 		(void) as_unmap(as, *addrp, len);
2017 	}
2018 
2019 	dev_a.mapfunc = mapfunc;
2020 	dev_a.dev = dev;
2021 	dev_a.offset = off;
2022 	dev_a.prot = (uchar_t)prot;
2023 	dev_a.maxprot = (uchar_t)maxprot;
2024 	dev_a.hat_flags = 0;
2025 	dev_a.hat_attr = 0;
2026 	dev_a.devmap_data = NULL;
2027 
2028 	error = as_map(as, *addrp, len, segdev_create, &dev_a);
2029 	as_rangeunlock(as);
2030 	return (error);
2031 }
2032 
2033 int
2034 spec_char_map(
2035 	dev_t dev,
2036 	offset_t off,
2037 	struct as *as,
2038 	caddr_t *addrp,
2039 	size_t len,
2040 	uchar_t prot,
2041 	uchar_t maxprot,
2042 	uint_t flags,
2043 	struct cred *cred)
2044 {
2045 	int error = 0;
2046 	major_t maj = getmajor(dev);
2047 	int map_flag;
2048 	int (*segmap)(dev_t, off_t, struct as *,
2049 	    caddr_t *, off_t, uint_t, uint_t, uint_t, cred_t *);
2050 	int (*devmap)(dev_t, devmap_cookie_t, offset_t,
2051 		size_t, size_t *, uint_t);
2052 	int (*mmap)(dev_t dev, off_t off, int prot);
2053 
2054 	/*
2055 	 * Character device: let the device driver
2056 	 * pick the appropriate segment driver.
2057 	 *
2058 	 * 4.x compat.: allow 'NULL' cb_segmap => spec_segmap
2059 	 * Kindness: allow 'nulldev' cb_segmap => spec_segmap
2060 	 */
2061 	segmap = devopsp[maj]->devo_cb_ops->cb_segmap;
2062 	if (segmap == NULL || segmap == nulldev || segmap == nodev) {
2063 		mmap = devopsp[maj]->devo_cb_ops->cb_mmap;
2064 		map_flag = devopsp[maj]->devo_cb_ops->cb_flag;
2065 
2066 		/*
2067 		 * Use old mmap framework if the driver has both mmap
2068 		 * and devmap entry points.  This is to prevent the
2069 		 * system from calling invalid devmap entry point
2070 		 * for some drivers that might have put garbage in the
2071 		 * devmap entry point.
2072 		 */
2073 		if ((map_flag & D_DEVMAP) || mmap == NULL ||
2074 		    mmap == nulldev || mmap == nodev) {
2075 			devmap = devopsp[maj]->devo_cb_ops->cb_devmap;
2076 
2077 			/*
2078 			 * If driver provides devmap entry point in
2079 			 * cb_ops but not xx_segmap(9E), call
2080 			 * devmap_setup with default settings
2081 			 * (NULL) for callback_ops and driver
2082 			 * callback private data
2083 			 */
2084 			if (devmap == nodev || devmap == NULL ||
2085 			    devmap == nulldev)
2086 				return (ENODEV);
2087 
2088 			error = devmap_setup(dev, off, as, addrp,
2089 			    len, prot, maxprot, flags, cred);
2090 
2091 			return (error);
2092 		} else
2093 			segmap = spec_segmap;
2094 	} else
2095 		segmap = cdev_segmap;
2096 
2097 	return ((*segmap)(dev, (off_t)off, as, addrp, len, prot,
2098 	    maxprot, flags, cred));
2099 }
2100 
2101 static int
2102 spec_map(
2103 	struct vnode *vp,
2104 	offset_t off,
2105 	struct as *as,
2106 	caddr_t *addrp,
2107 	size_t len,
2108 	uchar_t prot,
2109 	uchar_t maxprot,
2110 	uint_t flags,
2111 	struct cred *cred)
2112 {
2113 	int error = 0;
2114 
2115 	if (vp->v_flag & VNOMAP)
2116 		return (ENOSYS);
2117 
2118 	/*
2119 	 * If file is locked, fail mapping attempt.
2120 	 */
2121 	if (vn_has_flocks(vp))
2122 		return (EAGAIN);
2123 
2124 	if (vp->v_type == VCHR) {
2125 		return (spec_char_map(vp->v_rdev, off, as, addrp, len, prot,
2126 		    maxprot, flags, cred));
2127 	} else if (vp->v_type == VBLK) {
2128 		struct segvn_crargs vn_a;
2129 		struct vnode *cvp;
2130 		struct snode *sp;
2131 
2132 		/*
2133 		 * Block device, use segvn mapping to the underlying commonvp
2134 		 * for pages.
2135 		 */
2136 		if (off > spec_maxoffset(vp))
2137 			return (ENXIO);
2138 
2139 		sp = VTOS(vp);
2140 		cvp = sp->s_commonvp;
2141 		ASSERT(cvp != NULL);
2142 
2143 		if (off < 0 || (off + len) < 0)
2144 			return (ENXIO);
2145 
2146 		as_rangelock(as);
2147 		if ((flags & MAP_FIXED) == 0) {
2148 			map_addr(addrp, len, off, 1, flags);
2149 			if (*addrp == NULL) {
2150 				as_rangeunlock(as);
2151 				return (ENOMEM);
2152 			}
2153 		} else {
2154 			/*
2155 			 * User-specified address; blow away any
2156 			 * previous mappings.
2157 			 */
2158 			(void) as_unmap(as, *addrp, len);
2159 		}
2160 
2161 		vn_a.vp = cvp;
2162 		vn_a.offset = off;
2163 		vn_a.type = flags & MAP_TYPE;
2164 		vn_a.prot = (uchar_t)prot;
2165 		vn_a.maxprot = (uchar_t)maxprot;
2166 		vn_a.flags = flags & ~MAP_TYPE;
2167 		vn_a.cred = cred;
2168 		vn_a.amp = NULL;
2169 		vn_a.szc = 0;
2170 		vn_a.lgrp_mem_policy_flags = 0;
2171 
2172 		error = as_map(as, *addrp, len, segvn_create, &vn_a);
2173 		as_rangeunlock(as);
2174 	} else
2175 		return (ENODEV);
2176 
2177 	return (error);
2178 }
2179 
2180 /*ARGSUSED1*/
2181 static int
2182 spec_addmap(
2183 	struct vnode *vp,	/* the common vnode */
2184 	offset_t off,
2185 	struct as *as,
2186 	caddr_t addr,
2187 	size_t len,		/* how many bytes to add */
2188 	uchar_t prot,
2189 	uchar_t maxprot,
2190 	uint_t flags,
2191 	struct cred *cred)
2192 {
2193 	int error = 0;
2194 	struct snode *csp = VTOS(vp);
2195 	ulong_t npages;
2196 
2197 	ASSERT(vp != NULL && VTOS(vp)->s_commonvp == vp);
2198 
2199 	/*
2200 	 * XXX	Given the above assertion, this might not
2201 	 *	be a particularly sensible thing to test.
2202 	 */
2203 	if (vp->v_flag & VNOMAP)
2204 		return (ENOSYS);
2205 
2206 	npages = btopr(len);
2207 	LOCK_CSP(csp);
2208 	csp->s_mapcnt += npages;
2209 
2210 	UNLOCK_CSP(csp);
2211 	return (error);
2212 }
2213 
2214 /*ARGSUSED1*/
2215 static int
2216 spec_delmap(
2217 	struct vnode *vp,	/* the common vnode */
2218 	offset_t off,
2219 	struct as *as,
2220 	caddr_t addr,
2221 	size_t len,		/* how many bytes to take away */
2222 	uint_t prot,
2223 	uint_t maxprot,
2224 	uint_t flags,
2225 	struct cred *cred)
2226 {
2227 	struct snode *csp = VTOS(vp);
2228 	ulong_t npages;
2229 	long mcnt;
2230 
2231 	/* segdev passes us the common vp */
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 
2244 	LOCK_CSP(csp);
2245 	mutex_enter(&csp->s_lock);
2246 	mcnt = (csp->s_mapcnt -= npages);
2247 
2248 	if (mcnt == 0) {
2249 		/*
2250 		 * Call the close routine when the last reference of any
2251 		 * kind through any [s, v]node goes away.  The s_dip hold
2252 		 * on the devinfo node is released when the vnode is
2253 		 * destroyed.
2254 		 */
2255 		if (csp->s_count == 0) {
2256 			csp->s_flag &= ~(SNEEDCLOSE | SSIZEVALID);
2257 
2258 			/* See comment in spec_close() */
2259 			if (csp->s_flag & (SCLONE | SSELFCLONE))
2260 				csp->s_flag &= ~SDIPSET;
2261 
2262 			mutex_exit(&csp->s_lock);
2263 
2264 			(void) device_close(vp, 0, cred);
2265 		} else
2266 			mutex_exit(&csp->s_lock);
2267 
2268 		mutex_enter(&csp->s_lock);
2269 	}
2270 	ASSERT(mcnt >= 0);
2271 
2272 	UNLOCK_CSP_LOCK_HELD(csp);
2273 	mutex_exit(&csp->s_lock);
2274 
2275 	return (0);
2276 }
2277 
2278 static int
2279 spec_dump(struct vnode *vp, caddr_t addr, int bn, int count)
2280 {
2281 	ASSERT(vp->v_type == VBLK);
2282 	return (bdev_dump(vp->v_rdev, addr, bn, count));
2283 }
2284 
2285 
2286 /*
2287  * Do i/o on the given page list from/to vp, io_off for io_len.
2288  * Flags are composed of:
2289  * 	{B_ASYNC, B_INVAL, B_FREE, B_DONTNEED, B_READ, B_WRITE}
2290  * If B_ASYNC is not set i/o is waited for.
2291  */
2292 /*ARGSUSED5*/
2293 static int
2294 spec_pageio(
2295 	struct vnode *vp,
2296 	page_t	*pp,
2297 	u_offset_t io_off,
2298 	size_t	io_len,
2299 	int	flags,
2300 	cred_t	*cr)
2301 {
2302 	struct buf *bp = NULL;
2303 	int err = 0;
2304 
2305 	if (pp == NULL)
2306 		return (EINVAL);
2307 
2308 	bp = spec_startio(vp, pp, io_off, io_len, flags);
2309 
2310 	/*
2311 	 * Wait for i/o to complete if the request is not B_ASYNC.
2312 	 */
2313 	if ((flags & B_ASYNC) == 0) {
2314 		err = biowait(bp);
2315 		pageio_done(bp);
2316 	}
2317 	return (err);
2318 }
2319 
2320 /*
2321  * Set ACL on underlying vnode if one exists, or return ENOSYS otherwise.
2322  */
2323 int
2324 spec_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr)
2325 {
2326 	struct vnode *realvp;
2327 	struct snode *sp = VTOS(vp);
2328 	int error;
2329 
2330 	/*
2331 	 * The acl(2) system calls VOP_RWLOCK on the file before setting an
2332 	 * ACL, but since specfs does not serialize reads and writes, this
2333 	 * VOP does not do anything.  However, some backing file systems may
2334 	 * expect the lock to be held before setting an ACL, so it is taken
2335 	 * here privately to avoid serializing specfs reads and writes.
2336 	 */
2337 	if ((realvp = sp->s_realvp) != NULL) {
2338 		(void) VOP_RWLOCK(realvp, V_WRITELOCK_TRUE, NULL);
2339 		error = VOP_SETSECATTR(realvp, vsap, flag, cr);
2340 		(void) VOP_RWUNLOCK(realvp, V_WRITELOCK_TRUE, NULL);
2341 		return (error);
2342 	} else
2343 		return (fs_nosys());
2344 }
2345 
2346 /*
2347  * Get ACL from underlying vnode if one exists, or fabricate it from
2348  * the permissions returned by spec_getattr() otherwise.
2349  */
2350 int
2351 spec_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr)
2352 {
2353 	struct vnode *realvp;
2354 	struct snode *sp = VTOS(vp);
2355 
2356 	if ((realvp = sp->s_realvp) != NULL)
2357 		return (VOP_GETSECATTR(realvp, vsap, flag, cr));
2358 	else
2359 		return (fs_fab_acl(vp, vsap, flag, cr));
2360 }
2361 
2362 int
2363 spec_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
2364 {
2365 	vnode_t *realvp;
2366 	struct snode *sp = VTOS(vp);
2367 
2368 	if ((realvp = sp->s_realvp) != NULL)
2369 		return (VOP_PATHCONF(realvp, cmd, valp, cr));
2370 	else
2371 		return (fs_pathconf(vp, cmd, valp, cr));
2372 }
2373