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