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