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