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