17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a5652762Spraks  * Common Development and Distribution License (the "License").
6a5652762Spraks  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/thread.h>
447c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <sys/systm.h>
477c478bd9Sstevel@tonic-gate #include <sys/bitmap.h>
487c478bd9Sstevel@tonic-gate #include <sys/buf.h>
497c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
507c478bd9Sstevel@tonic-gate #include <sys/conf.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
53feb08c6bSbillm #include <sys/dkio.h>
547c478bd9Sstevel@tonic-gate #include <sys/errno.h>
557c478bd9Sstevel@tonic-gate #include <sys/time.h>
567c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
577c478bd9Sstevel@tonic-gate #include <sys/flock.h>
587c478bd9Sstevel@tonic-gate #include <sys/file.h>
597c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
607c478bd9Sstevel@tonic-gate #include <sys/mman.h>
617c478bd9Sstevel@tonic-gate #include <sys/open.h>
627c478bd9Sstevel@tonic-gate #include <sys/swap.h>
637c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
647c478bd9Sstevel@tonic-gate #include <sys/uio.h>
657c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
66aa59c4cbSrsb #include <sys/vfs_opreg.h>
677c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
687c478bd9Sstevel@tonic-gate #include <sys/stat.h>
697c478bd9Sstevel@tonic-gate #include <sys/poll.h>
707c478bd9Sstevel@tonic-gate #include <sys/stream.h>
717c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
727c478bd9Sstevel@tonic-gate #include <sys/policy.h>
737c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #include <sys/proc.h>
767c478bd9Sstevel@tonic-gate #include <sys/user.h>
777c478bd9Sstevel@tonic-gate #include <sys/session.h>
787c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
797c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
807c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #include <vm/seg.h>
857c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
867c478bd9Sstevel@tonic-gate #include <vm/page.h>
877c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
887c478bd9Sstevel@tonic-gate #include <vm/seg_dev.h>
897c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
947c478bd9Sstevel@tonic-gate #include <sys/autoconf.h>
957c478bd9Sstevel@tonic-gate #include <sys/sunndi.h>
9625e8c5aaSvikram #include <sys/contract/device_impl.h>
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate static int spec_open(struct vnode **, int, struct cred *);
1007c478bd9Sstevel@tonic-gate static int spec_close(struct vnode *, int, int, offset_t, struct cred *);
1017c478bd9Sstevel@tonic-gate static int spec_read(struct vnode *, struct uio *, int, struct cred *,
1027c478bd9Sstevel@tonic-gate 	struct caller_context *);
1037c478bd9Sstevel@tonic-gate static int spec_write(struct vnode *, struct uio *, int, struct cred *,
1047c478bd9Sstevel@tonic-gate 	struct caller_context *);
1057c478bd9Sstevel@tonic-gate static int spec_ioctl(struct vnode *, int, intptr_t, int, struct cred *, int *);
1067c478bd9Sstevel@tonic-gate static int spec_getattr(struct vnode *, struct vattr *, int, struct cred *);
1077c478bd9Sstevel@tonic-gate static int spec_setattr(struct vnode *, struct vattr *, int, struct cred *,
1087c478bd9Sstevel@tonic-gate 	caller_context_t *);
1097c478bd9Sstevel@tonic-gate static int spec_access(struct vnode *, int, int, struct cred *);
1107c478bd9Sstevel@tonic-gate static int spec_create(struct vnode *, char *, vattr_t *, enum vcexcl,
1117c478bd9Sstevel@tonic-gate     int, struct vnode **, struct cred *, int);
1127c478bd9Sstevel@tonic-gate static int spec_fsync(struct vnode *, int, struct cred *);
1137c478bd9Sstevel@tonic-gate static void spec_inactive(struct vnode *, struct cred *);
1147c478bd9Sstevel@tonic-gate static int spec_fid(struct vnode *, struct fid *);
1157c478bd9Sstevel@tonic-gate static int spec_seek(struct vnode *, offset_t, offset_t *);
1167c478bd9Sstevel@tonic-gate static int spec_frlock(struct vnode *, int, struct flock64 *, int, offset_t,
1177c478bd9Sstevel@tonic-gate     struct flk_callback *, struct cred *);
1187c478bd9Sstevel@tonic-gate static int spec_realvp(struct vnode *, struct vnode **);
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate static int spec_getpage(struct vnode *, offset_t, size_t, uint_t *, page_t **,
1217c478bd9Sstevel@tonic-gate     size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
1227c478bd9Sstevel@tonic-gate static int spec_putapage(struct vnode *, page_t *, u_offset_t *, size_t *, int,
1237c478bd9Sstevel@tonic-gate 	struct cred *);
1247c478bd9Sstevel@tonic-gate static struct buf *spec_startio(struct vnode *, page_t *, u_offset_t, size_t,
1257c478bd9Sstevel@tonic-gate 	int);
1267c478bd9Sstevel@tonic-gate static int spec_getapage(struct vnode *, u_offset_t, size_t, uint_t *,
1277c478bd9Sstevel@tonic-gate     page_t **, size_t, struct seg *, caddr_t, enum seg_rw, struct cred *);
1287c478bd9Sstevel@tonic-gate static int spec_map(struct vnode *, offset_t, struct as *, caddr_t *, size_t,
1297c478bd9Sstevel@tonic-gate     uchar_t, uchar_t, uint_t, struct cred *);
1307c478bd9Sstevel@tonic-gate static int spec_addmap(struct vnode *, offset_t, struct as *, caddr_t, size_t,
1317c478bd9Sstevel@tonic-gate     uchar_t, uchar_t, uint_t, struct cred *);
1327c478bd9Sstevel@tonic-gate static int spec_delmap(struct vnode *, offset_t, struct as *, caddr_t, size_t,
1337c478bd9Sstevel@tonic-gate     uint_t, uint_t, uint_t, struct cred *);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static int spec_poll(struct vnode *, short, int, short *, struct pollhead **);
1367c478bd9Sstevel@tonic-gate static int spec_dump(struct vnode *, caddr_t, int, int);
1377c478bd9Sstevel@tonic-gate static int spec_pageio(struct vnode *, page_t *, u_offset_t, size_t, int,
1387c478bd9Sstevel@tonic-gate     cred_t *);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate static int spec_getsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
1417c478bd9Sstevel@tonic-gate static int spec_setsecattr(struct vnode *, vsecattr_t *, int, struct cred *);
1427c478bd9Sstevel@tonic-gate static int spec_pathconf(struct	vnode *, int, ulong_t *, struct cred *);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #define	SN_HOLD(csp)	{ \
1457c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock); \
1467c478bd9Sstevel@tonic-gate 	csp->s_count++; \
1477c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock); \
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #define	SN_RELE(csp)	{ \
1517c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock); \
1527c478bd9Sstevel@tonic-gate 	csp->s_count--; \
153fbe27353Sedp 	ASSERT((csp->s_count > 0) || (csp->s_vnode->v_stream == NULL)); \
1547c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock); \
1557c478bd9Sstevel@tonic-gate }
1567c478bd9Sstevel@tonic-gate 
15725e8c5aaSvikram #define	S_ISFENCED(sp)	((VTOS((sp)->s_commonvp))->s_flag & SFENCED)
15825e8c5aaSvikram 
1597c478bd9Sstevel@tonic-gate struct vnodeops *spec_vnodeops;
1607c478bd9Sstevel@tonic-gate 
16125e8c5aaSvikram /*
16225e8c5aaSvikram  * *PLEASE NOTE*: If you add new entry points to specfs, do
16325e8c5aaSvikram  * not forget to add support for fencing. A fenced snode
16425e8c5aaSvikram  * is indicated by the SFENCED flag in the common snode.
16525e8c5aaSvikram  * If a snode is fenced, determine if your entry point is
16625e8c5aaSvikram  * a configuration operation (Example: open), a detection
16725e8c5aaSvikram  * operation (Example: gettattr), an I/O operation (Example: ioctl())
16825e8c5aaSvikram  * or an unconfiguration operation (Example: close). If it is
16925e8c5aaSvikram  * a configuration or detection operation, fail the operation
17025e8c5aaSvikram  * for a fenced snode with an ENXIO or EIO as appropriate. If
17125e8c5aaSvikram  * it is any other operation, let it through.
17225e8c5aaSvikram  */
17325e8c5aaSvikram 
1747c478bd9Sstevel@tonic-gate const fs_operation_def_t spec_vnodeops_template[] = {
175aa59c4cbSrsb 	VOPNAME_OPEN,		{ .vop_open = spec_open },
176aa59c4cbSrsb 	VOPNAME_CLOSE,		{ .vop_close = spec_close },
177aa59c4cbSrsb 	VOPNAME_READ,		{ .vop_read = spec_read },
178aa59c4cbSrsb 	VOPNAME_WRITE,		{ .vop_write = spec_write },
179aa59c4cbSrsb 	VOPNAME_IOCTL,		{ .vop_ioctl = spec_ioctl },
180aa59c4cbSrsb 	VOPNAME_GETATTR,	{ .vop_getattr = spec_getattr },
181aa59c4cbSrsb 	VOPNAME_SETATTR,	{ .vop_setattr = spec_setattr },
182aa59c4cbSrsb 	VOPNAME_ACCESS,		{ .vop_access = spec_access },
183aa59c4cbSrsb 	VOPNAME_CREATE,		{ .vop_create = spec_create },
184aa59c4cbSrsb 	VOPNAME_FSYNC,		{ .vop_fsync = spec_fsync },
185aa59c4cbSrsb 	VOPNAME_INACTIVE,	{ .vop_inactive = spec_inactive },
186aa59c4cbSrsb 	VOPNAME_FID,		{ .vop_fid = spec_fid },
187aa59c4cbSrsb 	VOPNAME_SEEK,		{ .vop_seek = spec_seek },
188aa59c4cbSrsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = spec_pathconf },
189aa59c4cbSrsb 	VOPNAME_FRLOCK,		{ .vop_frlock = spec_frlock },
190aa59c4cbSrsb 	VOPNAME_REALVP,		{ .vop_realvp = spec_realvp },
191aa59c4cbSrsb 	VOPNAME_GETPAGE,	{ .vop_getpage = spec_getpage },
192aa59c4cbSrsb 	VOPNAME_PUTPAGE,	{ .vop_putpage = spec_putpage },
193aa59c4cbSrsb 	VOPNAME_MAP,		{ .vop_map = spec_map },
194aa59c4cbSrsb 	VOPNAME_ADDMAP,		{ .vop_addmap = spec_addmap },
195aa59c4cbSrsb 	VOPNAME_DELMAP,		{ .vop_delmap = spec_delmap },
196aa59c4cbSrsb 	VOPNAME_POLL,		{ .vop_poll = spec_poll },
197aa59c4cbSrsb 	VOPNAME_DUMP,		{ .vop_dump = spec_dump },
198aa59c4cbSrsb 	VOPNAME_PAGEIO,		{ .vop_pageio = spec_pageio },
199aa59c4cbSrsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = spec_setsecattr },
200aa59c4cbSrsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = spec_getsecattr },
2017c478bd9Sstevel@tonic-gate 	NULL,			NULL
2027c478bd9Sstevel@tonic-gate };
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * Return address of spec_vnodeops
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate struct vnodeops *
2087c478bd9Sstevel@tonic-gate spec_getvnodeops(void)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	return (spec_vnodeops);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate extern vnode_t *rconsvp;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * Acquire the serial lock on the common snode.
2177c478bd9Sstevel@tonic-gate  */
218e099bf07Scth #define	LOCK_CSP(csp)			(void) spec_lockcsp(csp, 0, 1, 0)
219e099bf07Scth #define	LOCKHOLD_CSP_SIG(csp)		spec_lockcsp(csp, 1, 1, 1)
220e099bf07Scth #define	SYNCHOLD_CSP_SIG(csp, intr)	spec_lockcsp(csp, intr, 0, 1)
2217c478bd9Sstevel@tonic-gate 
222*7f9b0c87Scg13442 typedef enum {
223*7f9b0c87Scg13442 	LOOP,
224*7f9b0c87Scg13442 	INTR,
225*7f9b0c87Scg13442 	SUCCESS
226*7f9b0c87Scg13442 } slock_ret_t;
227*7f9b0c87Scg13442 
2287c478bd9Sstevel@tonic-gate /*
229*7f9b0c87Scg13442  * Synchronize with active SLOCKED snode, optionally checking for a signal and
230e099bf07Scth  * optionally returning with SLOCKED set and SN_HOLD done.  The 'intr'
231e099bf07Scth  * argument determines if the thread is interruptible by a signal while
232*7f9b0c87Scg13442  * waiting, the function returns INTR if interrupted while there is another
233*7f9b0c87Scg13442  * thread closing this snonde and LOOP if interrupted otherwise.
234*7f9b0c87Scg13442  * When SUCCESS is returned the 'hold' argument determines if the open
235*7f9b0c87Scg13442  * count (SN_HOLD) has been incremented and the 'setlock' argument
236*7f9b0c87Scg13442  * determines if the function returns with SLOCKED set.
2377c478bd9Sstevel@tonic-gate  */
238*7f9b0c87Scg13442 static slock_ret_t
239e099bf07Scth spec_lockcsp(struct snode *csp, int intr, int setlock, int hold)
2407c478bd9Sstevel@tonic-gate {
241*7f9b0c87Scg13442 	slock_ret_t ret = SUCCESS;
2427c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
2437c478bd9Sstevel@tonic-gate 	while (csp->s_flag & SLOCKED) {
2447c478bd9Sstevel@tonic-gate 		csp->s_flag |= SWANT;
245e099bf07Scth 		if (intr) {
2467c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&csp->s_cv, &csp->s_lock)) {
247*7f9b0c87Scg13442 				if (csp->s_flag & SCLOSING)
248*7f9b0c87Scg13442 					ret = INTR;
249*7f9b0c87Scg13442 				else
250*7f9b0c87Scg13442 					ret = LOOP;
2517c478bd9Sstevel@tonic-gate 				mutex_exit(&csp->s_lock);
252*7f9b0c87Scg13442 				return (ret);		/* interrupted */
253e099bf07Scth 			}
254e099bf07Scth 		} else {
255e099bf07Scth 			cv_wait(&csp->s_cv, &csp->s_lock);
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 	}
258e099bf07Scth 	if (setlock)
2597c478bd9Sstevel@tonic-gate 		csp->s_flag |= SLOCKED;
260e099bf07Scth 	if (hold)
261e099bf07Scth 		csp->s_count++;		/* one more open reference : SN_HOLD */
2627c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
263*7f9b0c87Scg13442 	return (ret);			/* serialized/locked */
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Unlock the serial lock on the common snode
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate #define	UNLOCK_CSP_LOCK_HELD(csp)			\
2707c478bd9Sstevel@tonic-gate 	ASSERT(mutex_owned(&csp->s_lock));		\
2717c478bd9Sstevel@tonic-gate 	if (csp->s_flag & SWANT)			\
2727c478bd9Sstevel@tonic-gate 		cv_broadcast(&csp->s_cv);		\
2737c478bd9Sstevel@tonic-gate 	csp->s_flag &= ~(SWANT|SLOCKED);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate #define	UNLOCK_CSP(csp)					\
2767c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);			\
2777c478bd9Sstevel@tonic-gate 	UNLOCK_CSP_LOCK_HELD(csp);			\
2787c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate /*
2817c478bd9Sstevel@tonic-gate  * compute/return the size of the device
2827c478bd9Sstevel@tonic-gate  */
2837c478bd9Sstevel@tonic-gate #define	SPEC_SIZE(csp)	\
2847c478bd9Sstevel@tonic-gate 	(((csp)->s_flag & SSIZEVALID) ? (csp)->s_size : spec_size(csp))
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate /*
2877c478bd9Sstevel@tonic-gate  * Compute and return the size.  If the size in the common snode is valid then
2887c478bd9Sstevel@tonic-gate  * return it.  If not valid then get the size from the driver and set size in
2897c478bd9Sstevel@tonic-gate  * the common snode.  If the device has not been attached then we don't ask for
2907c478bd9Sstevel@tonic-gate  * an update from the driver- for non-streams SSIZEVALID stays unset until the
2917c478bd9Sstevel@tonic-gate  * device is attached. A stat of a mknod outside /devices (non-devfs) may
2927c478bd9Sstevel@tonic-gate  * report UNKNOWN_SIZE because the device may not be attached yet (SDIPSET not
2937c478bd9Sstevel@tonic-gate  * established in mknod until open time). An stat in /devices will report the
2947c478bd9Sstevel@tonic-gate  * size correctly.  Specfs should always call SPEC_SIZE instead of referring
2957c478bd9Sstevel@tonic-gate  * directly to s_size to initialize/retrieve the size of a device.
2967c478bd9Sstevel@tonic-gate  *
2977c478bd9Sstevel@tonic-gate  * XXX There is an inconsistency between block and raw - "unknown" is
2987c478bd9Sstevel@tonic-gate  * UNKNOWN_SIZE for VBLK and 0 for VCHR(raw).
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate static u_offset_t
3017c478bd9Sstevel@tonic-gate spec_size(struct snode *csp)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	struct vnode	*cvp = STOV(csp);
3047c478bd9Sstevel@tonic-gate 	u_offset_t	size;
3057c478bd9Sstevel@tonic-gate 	int		plen;
3067c478bd9Sstevel@tonic-gate 	uint32_t	size32;
3077c478bd9Sstevel@tonic-gate 	dev_t		dev;
3087c478bd9Sstevel@tonic-gate 	dev_info_t	*devi;
3097c478bd9Sstevel@tonic-gate 	major_t		maj;
310184cd04cScth 	uint_t		blksize;
311184cd04cScth 	int		blkshift;
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	ASSERT((csp)->s_commonvp == cvp);	/* must be common node */
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	/* return cached value */
3167c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
3177c478bd9Sstevel@tonic-gate 	if (csp->s_flag & SSIZEVALID) {
3187c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
3197c478bd9Sstevel@tonic-gate 		return (csp->s_size);
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	/* VOP_GETATTR of mknod has not had devcnt restriction applied */
3237c478bd9Sstevel@tonic-gate 	dev = cvp->v_rdev;
3247c478bd9Sstevel@tonic-gate 	maj = getmajor(dev);
3257c478bd9Sstevel@tonic-gate 	if (maj >= devcnt) {
3267c478bd9Sstevel@tonic-gate 		/* return non-cached UNKNOWN_SIZE */
3277c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
3287c478bd9Sstevel@tonic-gate 		return ((cvp->v_type == VCHR) ? 0 : UNKNOWN_SIZE);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/* establish cached zero size for streams */
3327c478bd9Sstevel@tonic-gate 	if (STREAMSTAB(maj)) {
3337c478bd9Sstevel@tonic-gate 		csp->s_size = 0;
3347c478bd9Sstevel@tonic-gate 		csp->s_flag |= SSIZEVALID;
3357c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
3367c478bd9Sstevel@tonic-gate 		return (0);
3377c478bd9Sstevel@tonic-gate 	}
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	/*
3407c478bd9Sstevel@tonic-gate 	 * Return non-cached UNKNOWN_SIZE if not open.
3417c478bd9Sstevel@tonic-gate 	 *
3427c478bd9Sstevel@tonic-gate 	 * NB: This check is bogus, calling prop_op(9E) should be gated by
3437c478bd9Sstevel@tonic-gate 	 * attach, not open. Not having this check however opens up a new
3447c478bd9Sstevel@tonic-gate 	 * context under which a driver's prop_op(9E) could be called. Calling
3457c478bd9Sstevel@tonic-gate 	 * prop_op(9E) in this new context has been shown to expose latent
3467c478bd9Sstevel@tonic-gate 	 * driver bugs (insufficient NULL pointer checks that lead to panic).
3477c478bd9Sstevel@tonic-gate 	 * We are keeping this open check for now to avoid these panics.
3487c478bd9Sstevel@tonic-gate 	 */
3497c478bd9Sstevel@tonic-gate 	if (csp->s_count == 0) {
3507c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
3517c478bd9Sstevel@tonic-gate 		return ((cvp->v_type == VCHR) ? 0 : UNKNOWN_SIZE);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/* Return non-cached UNKNOWN_SIZE if not attached. */
3557c478bd9Sstevel@tonic-gate 	if (((csp->s_flag & SDIPSET) == 0) || (csp->s_dip == NULL) ||
356737d277aScth 	    !i_ddi_devi_attached(csp->s_dip)) {
3577c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
3587c478bd9Sstevel@tonic-gate 		return ((cvp->v_type == VCHR) ? 0 : UNKNOWN_SIZE);
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	devi = csp->s_dip;
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	/*
3647c478bd9Sstevel@tonic-gate 	 * Established cached size obtained from the attached driver. Since we
3657c478bd9Sstevel@tonic-gate 	 * know the devinfo node, for efficiency we use cdev_prop_op directly
3667c478bd9Sstevel@tonic-gate 	 * instead of [cb]dev_[Ss]size.
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	if (cvp->v_type == VCHR) {
3697c478bd9Sstevel@tonic-gate 		size = 0;
3707c478bd9Sstevel@tonic-gate 		plen = sizeof (size);
3717c478bd9Sstevel@tonic-gate 		if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF,
3727c478bd9Sstevel@tonic-gate 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS |
3737c478bd9Sstevel@tonic-gate 		    DDI_PROP_CONSUMER_TYPED, "Size", (caddr_t)&size,
3747c478bd9Sstevel@tonic-gate 		    &plen) != DDI_PROP_SUCCESS) {
3757c478bd9Sstevel@tonic-gate 			plen = sizeof (size32);
3767c478bd9Sstevel@tonic-gate 			if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF,
3777c478bd9Sstevel@tonic-gate 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
3787c478bd9Sstevel@tonic-gate 			    "size", (caddr_t)&size32, &plen) ==
3797c478bd9Sstevel@tonic-gate 			    DDI_PROP_SUCCESS)
3807c478bd9Sstevel@tonic-gate 				size = size32;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 	} else {
3837c478bd9Sstevel@tonic-gate 		size = UNKNOWN_SIZE;
3847c478bd9Sstevel@tonic-gate 		plen = sizeof (size);
3857c478bd9Sstevel@tonic-gate 		if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF,
3867c478bd9Sstevel@tonic-gate 		    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS |
3877c478bd9Sstevel@tonic-gate 		    DDI_PROP_CONSUMER_TYPED, "Nblocks", (caddr_t)&size,
3887c478bd9Sstevel@tonic-gate 		    &plen) != DDI_PROP_SUCCESS) {
3897c478bd9Sstevel@tonic-gate 			plen = sizeof (size32);
3907c478bd9Sstevel@tonic-gate 			if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF,
3917c478bd9Sstevel@tonic-gate 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
3927c478bd9Sstevel@tonic-gate 			    "nblocks", (caddr_t)&size32, &plen) ==
3937c478bd9Sstevel@tonic-gate 			    DDI_PROP_SUCCESS)
3947c478bd9Sstevel@tonic-gate 				size = size32;
3957c478bd9Sstevel@tonic-gate 		}
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 		if (size != UNKNOWN_SIZE) {
398184cd04cScth 			blksize = DEV_BSIZE;		/* default */
399184cd04cScth 			plen = sizeof (blksize);
400184cd04cScth 
401184cd04cScth 			/* try to get dev_t specific "blksize" */
402184cd04cScth 			if (cdev_prop_op(dev, devi, PROP_LEN_AND_VAL_BUF,
403184cd04cScth 			    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
404184cd04cScth 			    "blksize", (caddr_t)&blksize, &plen) !=
405184cd04cScth 			    DDI_PROP_SUCCESS) {
406184cd04cScth 				/*
407184cd04cScth 				 * Try for dev_info node "device-blksize".
408184cd04cScth 				 * If this fails then blksize will still be
409184cd04cScth 				 * DEV_BSIZE default value.
410184cd04cScth 				 */
411184cd04cScth 				(void) cdev_prop_op(DDI_DEV_T_ANY, devi,
412184cd04cScth 				    PROP_LEN_AND_VAL_BUF,
413184cd04cScth 				    DDI_PROP_NOTPROM | DDI_PROP_DONTPASS,
414184cd04cScth 				    "device-blksize", (caddr_t)&blksize, &plen);
415184cd04cScth 			}
416184cd04cScth 
417184cd04cScth 			/* blksize must be a power of two */
418184cd04cScth 			ASSERT(BIT_ONLYONESET(blksize));
419184cd04cScth 			blkshift = highbit(blksize) - 1;
420184cd04cScth 
4217c478bd9Sstevel@tonic-gate 			/* convert from block size to byte size */
422184cd04cScth 			if (size < (MAXOFFSET_T >> blkshift))
423184cd04cScth 				size = size << blkshift;
4247c478bd9Sstevel@tonic-gate 			else
4257c478bd9Sstevel@tonic-gate 				size = UNKNOWN_SIZE;
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 	}
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	csp->s_size = size;
4307c478bd9Sstevel@tonic-gate 	csp->s_flag |= SSIZEVALID;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
4337c478bd9Sstevel@tonic-gate 	return (size);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * This function deal with vnode substitution in the case of
4387c478bd9Sstevel@tonic-gate  * device cloning.
4397c478bd9Sstevel@tonic-gate  */
4407c478bd9Sstevel@tonic-gate static int
4417c478bd9Sstevel@tonic-gate spec_clone(struct vnode **vpp, dev_t newdev, int vtype, struct stdata *stp)
4427c478bd9Sstevel@tonic-gate {
4437c478bd9Sstevel@tonic-gate 	dev_t		dev = (*vpp)->v_rdev;
4447c478bd9Sstevel@tonic-gate 	major_t		maj = getmajor(dev);
4457c478bd9Sstevel@tonic-gate 	major_t 	newmaj = getmajor(newdev);
4467c478bd9Sstevel@tonic-gate 	int		sysclone = (maj == clone_major);
4477c478bd9Sstevel@tonic-gate 	int		qassociate_used = 0;
4487c478bd9Sstevel@tonic-gate 	struct snode	*oldsp, *oldcsp;
4497c478bd9Sstevel@tonic-gate 	struct snode	*newsp, *newcsp;
4507c478bd9Sstevel@tonic-gate 	struct vnode	*newvp, *newcvp;
4517c478bd9Sstevel@tonic-gate 	dev_info_t	*dip;
4527c478bd9Sstevel@tonic-gate 	queue_t		*dq;
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 	ASSERT(dev != newdev);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/*
4577c478bd9Sstevel@tonic-gate 	 * Check for cloning across different drivers.
4587c478bd9Sstevel@tonic-gate 	 * We only support this under the system provided clone driver
4597c478bd9Sstevel@tonic-gate 	 */
4607c478bd9Sstevel@tonic-gate 	if ((maj != newmaj) && !sysclone) {
4617c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
4627c478bd9Sstevel@tonic-gate 		    "unsupported clone open maj = %u, newmaj = %u",
4637c478bd9Sstevel@tonic-gate 		    maj, newmaj);
4647c478bd9Sstevel@tonic-gate 		return (ENXIO);
4657c478bd9Sstevel@tonic-gate 	}
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	/* old */
4687c478bd9Sstevel@tonic-gate 	oldsp = VTOS(*vpp);
4697c478bd9Sstevel@tonic-gate 	oldcsp = VTOS(oldsp->s_commonvp);
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/* new */
4727c478bd9Sstevel@tonic-gate 	newvp = makespecvp(newdev, vtype);
4737c478bd9Sstevel@tonic-gate 	ASSERT(newvp != NULL);
4747c478bd9Sstevel@tonic-gate 	newsp = VTOS(newvp);
4757c478bd9Sstevel@tonic-gate 	newcvp = newsp->s_commonvp;
4767c478bd9Sstevel@tonic-gate 	newcsp = VTOS(newcvp);
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	/*
4797c478bd9Sstevel@tonic-gate 	 * Clones inherit fsid, realvp, and dip.
4807c478bd9Sstevel@tonic-gate 	 * XXX realvp inherit is not occurring, does fstat of clone work?
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	newsp->s_fsid = oldsp->s_fsid;
4837c478bd9Sstevel@tonic-gate 	if (sysclone) {
4847c478bd9Sstevel@tonic-gate 		newsp->s_flag |= SCLONE;
4857c478bd9Sstevel@tonic-gate 		dip = NULL;
4867c478bd9Sstevel@tonic-gate 	} else {
4877c478bd9Sstevel@tonic-gate 		newsp->s_flag |= SSELFCLONE;
4887c478bd9Sstevel@tonic-gate 		dip = oldcsp->s_dip;
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * If we cloned to an opened newdev that already has called
4937c478bd9Sstevel@tonic-gate 	 * spec_assoc_vp_with_devi (SDIPSET set) then the association is
4947c478bd9Sstevel@tonic-gate 	 * already established.
4957c478bd9Sstevel@tonic-gate 	 */
4967c478bd9Sstevel@tonic-gate 	if (!(newcsp->s_flag & SDIPSET)) {
4977c478bd9Sstevel@tonic-gate 		/*
4987c478bd9Sstevel@tonic-gate 		 * Establish s_dip association for newdev.
4997c478bd9Sstevel@tonic-gate 		 *
5007c478bd9Sstevel@tonic-gate 		 * If we trusted the getinfo(9E) DDI_INFO_DEVT2INSTANCE
5017c478bd9Sstevel@tonic-gate 		 * implementation of all cloning drivers  (SCLONE and SELFCLONE)
5027c478bd9Sstevel@tonic-gate 		 * we would always use e_ddi_hold_devi_by_dev().  We know that
5037c478bd9Sstevel@tonic-gate 		 * many drivers have had (still have?) problems with
5047c478bd9Sstevel@tonic-gate 		 * DDI_INFO_DEVT2INSTANCE, so we try to minimize reliance by
5057c478bd9Sstevel@tonic-gate 		 * detecting drivers that use QASSOCIATE (by looking down the
5067c478bd9Sstevel@tonic-gate 		 * stream) and setting their s_dip association to NULL.
5077c478bd9Sstevel@tonic-gate 		 */
5087c478bd9Sstevel@tonic-gate 		qassociate_used = 0;
5097c478bd9Sstevel@tonic-gate 		if (stp) {
5107c478bd9Sstevel@tonic-gate 			for (dq = stp->sd_wrq; dq; dq = dq->q_next) {
5117c478bd9Sstevel@tonic-gate 				if (_RD(dq)->q_flag & _QASSOCIATED) {
5127c478bd9Sstevel@tonic-gate 					qassociate_used = 1;
5137c478bd9Sstevel@tonic-gate 					dip = NULL;
5147c478bd9Sstevel@tonic-gate 					break;
5157c478bd9Sstevel@tonic-gate 				}
5167c478bd9Sstevel@tonic-gate 			}
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		if (dip || qassociate_used) {
5207c478bd9Sstevel@tonic-gate 			spec_assoc_vp_with_devi(newvp, dip);
5217c478bd9Sstevel@tonic-gate 		} else {
5227c478bd9Sstevel@tonic-gate 			/* derive association from newdev */
5237c478bd9Sstevel@tonic-gate 			dip = e_ddi_hold_devi_by_dev(newdev, 0);
5247c478bd9Sstevel@tonic-gate 			spec_assoc_vp_with_devi(newvp, dip);
5257c478bd9Sstevel@tonic-gate 			if (dip)
5267c478bd9Sstevel@tonic-gate 				ddi_release_devi(dip);
5277c478bd9Sstevel@tonic-gate 		}
5287c478bd9Sstevel@tonic-gate 	}
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate 	SN_HOLD(newcsp);
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	/* deal with stream stuff */
5337c478bd9Sstevel@tonic-gate 	if (stp != NULL) {
5347c478bd9Sstevel@tonic-gate 		LOCK_CSP(newcsp);	/* synchronize stream open/close */
5357c478bd9Sstevel@tonic-gate 		mutex_enter(&newcsp->s_lock);
5367c478bd9Sstevel@tonic-gate 		newcvp->v_stream = newvp->v_stream = stp;
5377c478bd9Sstevel@tonic-gate 		stp->sd_vnode = newcvp;
5387c478bd9Sstevel@tonic-gate 		stp->sd_strtab = STREAMSTAB(newmaj);
5397c478bd9Sstevel@tonic-gate 		mutex_exit(&newcsp->s_lock);
5407c478bd9Sstevel@tonic-gate 		UNLOCK_CSP(newcsp);
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	/* substitute the vnode */
5447c478bd9Sstevel@tonic-gate 	SN_RELE(oldcsp);
5457c478bd9Sstevel@tonic-gate 	VN_RELE(*vpp);
5467c478bd9Sstevel@tonic-gate 	*vpp = newvp;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	return (0);
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate static int
5527c478bd9Sstevel@tonic-gate spec_open(struct vnode **vpp, int flag, struct cred *cr)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	major_t maj;
5557c478bd9Sstevel@tonic-gate 	dev_t dev, newdev;
5567c478bd9Sstevel@tonic-gate 	struct vnode *vp, *cvp;
5577c478bd9Sstevel@tonic-gate 	struct snode *sp, *csp;
5587c478bd9Sstevel@tonic-gate 	struct stdata *stp;
5597c478bd9Sstevel@tonic-gate 	dev_info_t *dip;
5607c478bd9Sstevel@tonic-gate 	int error, type;
56125e8c5aaSvikram 	contract_t *ct = NULL;
562e099bf07Scth 	int open_returns_eintr;
563*7f9b0c87Scg13442 	slock_ret_t spec_locksp_ret;
564*7f9b0c87Scg13442 
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	flag &= ~FCREAT;		/* paranoia */
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	vp = *vpp;
5697c478bd9Sstevel@tonic-gate 	sp = VTOS(vp);
5707c478bd9Sstevel@tonic-gate 	ASSERT((vp->v_type == VCHR) || (vp->v_type == VBLK));
5717c478bd9Sstevel@tonic-gate 	if ((vp->v_type != VCHR) && (vp->v_type != VBLK))
5727c478bd9Sstevel@tonic-gate 		return (ENXIO);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	/*
5757c478bd9Sstevel@tonic-gate 	 * If the VFS_NODEVICES bit was set for the mount,
5767c478bd9Sstevel@tonic-gate 	 * do not allow opens of special devices.
5777c478bd9Sstevel@tonic-gate 	 */
5787c478bd9Sstevel@tonic-gate 	if (sp->s_realvp && (sp->s_realvp->v_vfsp->vfs_flag & VFS_NODEVICES))
5797c478bd9Sstevel@tonic-gate 		return (ENXIO);
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 	newdev = dev = vp->v_rdev;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * If we are opening a node that has not had spec_assoc_vp_with_devi
5857c478bd9Sstevel@tonic-gate 	 * called against it (mknod outside /devices or a non-dacf makespecvp
5867c478bd9Sstevel@tonic-gate 	 * node) then SDIPSET will not be set. In this case we call an
5877c478bd9Sstevel@tonic-gate 	 * interface which will reconstruct the path and lookup (drive attach)
5887c478bd9Sstevel@tonic-gate 	 * through devfs (e_ddi_hold_devi_by_dev -> e_ddi_hold_devi_by_path ->
5897c478bd9Sstevel@tonic-gate 	 * devfs_lookupname).  For support of broken drivers that don't call
5907c478bd9Sstevel@tonic-gate 	 * ddi_create_minor_node for all minor nodes in their instance space,
5917c478bd9Sstevel@tonic-gate 	 * we call interfaces that operates at the directory/devinfo
5927c478bd9Sstevel@tonic-gate 	 * (major/instance) level instead of to the leaf/minor node level.
5937c478bd9Sstevel@tonic-gate 	 * After finding and attaching the dip we associate it with the
5947c478bd9Sstevel@tonic-gate 	 * common specfs vnode (s_dip), which sets SDIPSET.  A DL_DETACH_REQ
5957c478bd9Sstevel@tonic-gate 	 * to style-2 stream driver may set s_dip to NULL with SDIPSET set.
5967c478bd9Sstevel@tonic-gate 	 *
5977c478bd9Sstevel@tonic-gate 	 * NOTE: Although e_ddi_hold_devi_by_dev takes a dev_t argument, its
5987c478bd9Sstevel@tonic-gate 	 * implementation operates at the major/instance level since it only
5997c478bd9Sstevel@tonic-gate 	 * need to return a dip.
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	cvp = sp->s_commonvp;
6027c478bd9Sstevel@tonic-gate 	csp = VTOS(cvp);
6037c478bd9Sstevel@tonic-gate 	if (!(csp->s_flag & SDIPSET)) {
6047c478bd9Sstevel@tonic-gate 		/* try to attach, return error if we fail */
6057c478bd9Sstevel@tonic-gate 		if ((dip = e_ddi_hold_devi_by_dev(dev, 0)) == NULL)
6067c478bd9Sstevel@tonic-gate 			return (ENXIO);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 		/* associate dip with the common snode s_dip */
6097c478bd9Sstevel@tonic-gate 		spec_assoc_vp_with_devi(vp, dip);
6107c478bd9Sstevel@tonic-gate 		ddi_release_devi(dip);	/* from e_ddi_hold_devi_by_dev */
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate 
61325e8c5aaSvikram 	/* check if device fenced off */
61425e8c5aaSvikram 	if (S_ISFENCED(sp))
61525e8c5aaSvikram 		return (ENXIO);
61625e8c5aaSvikram 
6177c478bd9Sstevel@tonic-gate #ifdef  DEBUG
6187c478bd9Sstevel@tonic-gate 	/* verify attach/open exclusion guarantee */
6197c478bd9Sstevel@tonic-gate 	dip = csp->s_dip;
620737d277aScth 	ASSERT((dip == NULL) || i_ddi_devi_attached(dip));
6217c478bd9Sstevel@tonic-gate #endif  /* DEBUG */
6227c478bd9Sstevel@tonic-gate 
623853de45fSdh155122 	if ((error = secpolicy_spec_open(cr, vp, flag)) != 0)
6247c478bd9Sstevel@tonic-gate 		return (error);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	maj = getmajor(dev);
6277c478bd9Sstevel@tonic-gate 	if (STREAMSTAB(maj))
6287c478bd9Sstevel@tonic-gate 		goto streams_open;
6297c478bd9Sstevel@tonic-gate 
630e099bf07Scth 	/*
631e099bf07Scth 	 * Wait for in progress last close to complete. This guarantees
632e099bf07Scth 	 * to the driver writer that we will never be in the drivers
633e099bf07Scth 	 * open and close on the same (dev_t, otype) at the same time.
634e099bf07Scth 	 * Open count already incremented (SN_HOLD) on non-zero return.
635e099bf07Scth 	 * The wait is interruptible by a signal if the driver sets the
636e099bf07Scth 	 * D_OPEN_RETURNS_EINTR cb_ops(9S) cb_flag or sets the
637e099bf07Scth 	 * ddi-open-returns-eintr(9P) property in its driver.conf.
638e099bf07Scth 	 */
639e099bf07Scth 	if ((devopsp[maj]->devo_cb_ops->cb_flag & D_OPEN_RETURNS_EINTR) ||
640e099bf07Scth 	    (devnamesp[maj].dn_flags & DN_OPEN_RETURNS_EINTR))
641e099bf07Scth 		open_returns_eintr = 1;
642e099bf07Scth 	else
643e099bf07Scth 		open_returns_eintr = 0;
644*7f9b0c87Scg13442 	while ((spec_locksp_ret = SYNCHOLD_CSP_SIG(csp, open_returns_eintr)) !=
645*7f9b0c87Scg13442 	    SUCCESS) {
646*7f9b0c87Scg13442 		if (spec_locksp_ret == INTR)
647e099bf07Scth 			return (EINTR);
648e099bf07Scth 	}
649fbe27353Sedp 
6507c478bd9Sstevel@tonic-gate 	/* non streams open */
6517c478bd9Sstevel@tonic-gate 	type = (vp->v_type == VBLK ? OTYP_BLK : OTYP_CHR);
6527c478bd9Sstevel@tonic-gate 	error = dev_open(&newdev, flag, type, cr);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	/* deal with clone case */
6557c478bd9Sstevel@tonic-gate 	if (error == 0 && dev != newdev) {
6567c478bd9Sstevel@tonic-gate 		error = spec_clone(vpp, newdev, vp->v_type, NULL);
6577c478bd9Sstevel@tonic-gate 		/*
6587c478bd9Sstevel@tonic-gate 		 * bail on clone failure, further processing
6597c478bd9Sstevel@tonic-gate 		 * results in undefined behaviors.
6607c478bd9Sstevel@tonic-gate 		 */
6617c478bd9Sstevel@tonic-gate 		if (error != 0)
6627c478bd9Sstevel@tonic-gate 			return (error);
6637c478bd9Sstevel@tonic-gate 		sp = VTOS(*vpp);
6647c478bd9Sstevel@tonic-gate 		csp = VTOS(sp->s_commonvp);
6657c478bd9Sstevel@tonic-gate 	}
6667c478bd9Sstevel@tonic-gate 
66725e8c5aaSvikram 	/*
66825e8c5aaSvikram 	 * create contracts only for userland opens
66925e8c5aaSvikram 	 * Successful open and cloning is done at this point.
67025e8c5aaSvikram 	 */
67125e8c5aaSvikram 	if (error == 0 && !(flag & FKLYR)) {
67225e8c5aaSvikram 		int spec_type;
67325e8c5aaSvikram 		spec_type = (STOV(csp)->v_type == VCHR) ? S_IFCHR : S_IFBLK;
67425e8c5aaSvikram 		if (contract_device_open(newdev, spec_type, NULL) != 0) {
67525e8c5aaSvikram 			error = EIO;
67625e8c5aaSvikram 		}
67725e8c5aaSvikram 	}
67825e8c5aaSvikram 
6797c478bd9Sstevel@tonic-gate 	if (error == 0) {
6807c478bd9Sstevel@tonic-gate 		sp->s_size = SPEC_SIZE(csp);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		if ((csp->s_flag & SNEEDCLOSE) == 0) {
6837c478bd9Sstevel@tonic-gate 			int nmaj = getmajor(newdev);
6847c478bd9Sstevel@tonic-gate 			mutex_enter(&csp->s_lock);
6857c478bd9Sstevel@tonic-gate 			/* successful open needs a close later */
6867c478bd9Sstevel@tonic-gate 			csp->s_flag |= SNEEDCLOSE;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 			/*
6897c478bd9Sstevel@tonic-gate 			 * Invalidate possible cached "unknown" size
6907c478bd9Sstevel@tonic-gate 			 * established by a VOP_GETATTR while open was in
6917c478bd9Sstevel@tonic-gate 			 * progress, and the driver might fail prop_op(9E).
6927c478bd9Sstevel@tonic-gate 			 */
6937c478bd9Sstevel@tonic-gate 			if (((cvp->v_type == VCHR) && (csp->s_size == 0)) ||
6947c478bd9Sstevel@tonic-gate 			    ((cvp->v_type == VBLK) &&
6957c478bd9Sstevel@tonic-gate 			    (csp->s_size == UNKNOWN_SIZE)))
6967c478bd9Sstevel@tonic-gate 				csp->s_flag &= ~SSIZEVALID;
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			if (devopsp[nmaj]->devo_cb_ops->cb_flag & D_64BIT)
6997c478bd9Sstevel@tonic-gate 				csp->s_flag |= SLOFFSET;
7007c478bd9Sstevel@tonic-gate 			if (devopsp[nmaj]->devo_cb_ops->cb_flag & D_U64BIT)
7017c478bd9Sstevel@tonic-gate 				csp->s_flag |= SLOFFSET | SANYOFFSET;
7027c478bd9Sstevel@tonic-gate 			mutex_exit(&csp->s_lock);
7037c478bd9Sstevel@tonic-gate 		}
7047c478bd9Sstevel@tonic-gate 		return (0);
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	/*
7087c478bd9Sstevel@tonic-gate 	 * Open failed. If we missed a close operation because
7097c478bd9Sstevel@tonic-gate 	 * we were trying to get the device open and it is the
7107c478bd9Sstevel@tonic-gate 	 * last in progress open that is failing then call close.
7117c478bd9Sstevel@tonic-gate 	 *
7127c478bd9Sstevel@tonic-gate 	 * NOTE: Only non-streams open has this race condition.
7137c478bd9Sstevel@tonic-gate 	 */
7147c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
7157c478bd9Sstevel@tonic-gate 	csp->s_count--;			/* decrement open count : SN_RELE */
7167c478bd9Sstevel@tonic-gate 	if ((csp->s_count == 0) &&	/* no outstanding open */
7177c478bd9Sstevel@tonic-gate 	    (csp->s_mapcnt == 0) &&	/* no mapping */
7187c478bd9Sstevel@tonic-gate 	    (csp->s_flag & SNEEDCLOSE)) { /* need a close */
7197c478bd9Sstevel@tonic-gate 		csp->s_flag &= ~(SNEEDCLOSE | SSIZEVALID);
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 		/* See comment in spec_close() */
7227c478bd9Sstevel@tonic-gate 		if (csp->s_flag & (SCLONE | SSELFCLONE))
7237c478bd9Sstevel@tonic-gate 			csp->s_flag &= ~SDIPSET;
7247c478bd9Sstevel@tonic-gate 
725e099bf07Scth 		csp->s_flag |= SCLOSING;
7267c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
727e099bf07Scth 
7287c478bd9Sstevel@tonic-gate 		ASSERT(*vpp != NULL);
7297c478bd9Sstevel@tonic-gate 		(void) device_close(*vpp, flag, cr);
730e099bf07Scth 
731e099bf07Scth 		mutex_enter(&csp->s_lock);
732e099bf07Scth 		csp->s_flag &= ~SCLOSING;
733e099bf07Scth 		mutex_exit(&csp->s_lock);
7347c478bd9Sstevel@tonic-gate 	} else {
7357c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 	return (error);
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate streams_open:
740fbe27353Sedp 	if (vp->v_type != VCHR)
7417c478bd9Sstevel@tonic-gate 		return (ENXIO);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 	/*
744e099bf07Scth 	 * Lock common snode to prevent any new clone opens on this
745e099bf07Scth 	 * stream while one is in progress. This is necessary since
746e099bf07Scth 	 * the stream currently associated with the clone device will
747e099bf07Scth 	 * not be part of it after the clone open completes. Unfortunately
748e099bf07Scth 	 * we don't know in advance if this is a clone
749e099bf07Scth 	 * device so we have to lock all opens.
7507c478bd9Sstevel@tonic-gate 	 *
751e099bf07Scth 	 * If we fail, it's because of an interrupt - EINTR return is an
752e099bf07Scth 	 * expected aspect of opening a stream so we don't need to check
753e099bf07Scth 	 * D_OPEN_RETURNS_EINTR. Open count already incremented (SN_HOLD)
754e099bf07Scth 	 * on non-zero return.
7557c478bd9Sstevel@tonic-gate 	 */
756*7f9b0c87Scg13442 	if (LOCKHOLD_CSP_SIG(csp) != SUCCESS)
7577c478bd9Sstevel@tonic-gate 		return (EINTR);
758fbe27353Sedp 
7597c478bd9Sstevel@tonic-gate 	error = stropen(cvp, &newdev, flag, cr);
7607c478bd9Sstevel@tonic-gate 	stp = cvp->v_stream;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	/* deal with the clone case */
7637c478bd9Sstevel@tonic-gate 	if ((error == 0) && (dev != newdev)) {
7647c478bd9Sstevel@tonic-gate 		vp->v_stream = cvp->v_stream = NULL;
7657c478bd9Sstevel@tonic-gate 		UNLOCK_CSP(csp);
7667c478bd9Sstevel@tonic-gate 		error = spec_clone(vpp, newdev, vp->v_type, stp);
7677c478bd9Sstevel@tonic-gate 		/*
7687c478bd9Sstevel@tonic-gate 		 * bail on clone failure, further processing
7697c478bd9Sstevel@tonic-gate 		 * results in undefined behaviors.
7707c478bd9Sstevel@tonic-gate 		 */
7717c478bd9Sstevel@tonic-gate 		if (error != 0)
7727c478bd9Sstevel@tonic-gate 			return (error);
7737c478bd9Sstevel@tonic-gate 		sp = VTOS(*vpp);
7747c478bd9Sstevel@tonic-gate 		csp = VTOS(sp->s_commonvp);
7757c478bd9Sstevel@tonic-gate 	} else if (error == 0) {
7767c478bd9Sstevel@tonic-gate 		vp->v_stream = stp;
7777c478bd9Sstevel@tonic-gate 		UNLOCK_CSP(csp);
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
78025e8c5aaSvikram 	/*
78125e8c5aaSvikram 	 * create contracts only for userland opens
78225e8c5aaSvikram 	 * Successful open and cloning is done at this point.
78325e8c5aaSvikram 	 */
78425e8c5aaSvikram 	if (error == 0 && !(flag & FKLYR)) {
78525e8c5aaSvikram 		/* STREAM is of type S_IFCHR */
78625e8c5aaSvikram 		if (contract_device_open(newdev, S_IFCHR, &ct) != 0) {
78725e8c5aaSvikram 			UNLOCK_CSP(csp);
78825e8c5aaSvikram 			(void) spec_close(vp, flag, 1, 0, cr);
78925e8c5aaSvikram 			return (EIO);
79025e8c5aaSvikram 		}
79125e8c5aaSvikram 	}
79225e8c5aaSvikram 
7937c478bd9Sstevel@tonic-gate 	if (error == 0) {
7947c478bd9Sstevel@tonic-gate 		/* STREAMS devices don't have a size */
7957c478bd9Sstevel@tonic-gate 		sp->s_size = csp->s_size = 0;
7967c478bd9Sstevel@tonic-gate 
7979acbbeafSnn35248 		if (!(stp->sd_flag & STRISTTY) || (flag & FNOCTTY))
7987c478bd9Sstevel@tonic-gate 			return (0);
7999acbbeafSnn35248 
8009acbbeafSnn35248 		/* try to allocate it as a controlling terminal */
8019acbbeafSnn35248 		if (strctty(stp) != EINTR)
8029acbbeafSnn35248 			return (0);
8039acbbeafSnn35248 
8049acbbeafSnn35248 		/* strctty() was interrupted by a signal */
80525e8c5aaSvikram 		if (ct) {
80625e8c5aaSvikram 			/* we only create contracts for userland opens */
80725e8c5aaSvikram 			ASSERT(ttoproc(curthread));
80825e8c5aaSvikram 			(void) contract_abandon(ct, ttoproc(curthread), 0);
80925e8c5aaSvikram 		}
8109acbbeafSnn35248 		(void) spec_close(vp, flag, 1, 0, cr);
8119acbbeafSnn35248 		return (EINTR);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * Deal with stropen failure.
8167c478bd9Sstevel@tonic-gate 	 *
8177c478bd9Sstevel@tonic-gate 	 * sd_flag in the stream head cannot change since the
8187c478bd9Sstevel@tonic-gate 	 * common snode is locked before the call to stropen().
8197c478bd9Sstevel@tonic-gate 	 */
8207c478bd9Sstevel@tonic-gate 	if ((stp != NULL) && (stp->sd_flag & STREOPENFAIL)) {
8217c478bd9Sstevel@tonic-gate 		/*
8227c478bd9Sstevel@tonic-gate 		 * Open failed part way through.
8237c478bd9Sstevel@tonic-gate 		 */
8247c478bd9Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
8257c478bd9Sstevel@tonic-gate 		stp->sd_flag &= ~STREOPENFAIL;
8267c478bd9Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 		UNLOCK_CSP(csp);
8297c478bd9Sstevel@tonic-gate 		(void) spec_close(vp, flag, 1, 0, cr);
8307c478bd9Sstevel@tonic-gate 	} else {
8317c478bd9Sstevel@tonic-gate 		UNLOCK_CSP(csp);
8327c478bd9Sstevel@tonic-gate 		SN_RELE(csp);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	return (error);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
8397c478bd9Sstevel@tonic-gate static int
8407c478bd9Sstevel@tonic-gate spec_close(
8417c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
8427c478bd9Sstevel@tonic-gate 	int		flag,
8437c478bd9Sstevel@tonic-gate 	int		count,
8447c478bd9Sstevel@tonic-gate 	offset_t	offset,
8457c478bd9Sstevel@tonic-gate 	struct cred	*cr)
8467c478bd9Sstevel@tonic-gate {
8477c478bd9Sstevel@tonic-gate 	struct vnode *cvp;
8487c478bd9Sstevel@tonic-gate 	struct snode *sp, *csp;
8497c478bd9Sstevel@tonic-gate 	enum vtype type;
8507c478bd9Sstevel@tonic-gate 	dev_t dev;
8517c478bd9Sstevel@tonic-gate 	int error = 0;
8527c478bd9Sstevel@tonic-gate 	int sysclone;
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (!(flag & FKLYR)) {
8557c478bd9Sstevel@tonic-gate 		/* this only applies to closes of devices from userland */
8567c478bd9Sstevel@tonic-gate 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
8577c478bd9Sstevel@tonic-gate 		cleanshares(vp, ttoproc(curthread)->p_pid);
8587c478bd9Sstevel@tonic-gate 		if (vp->v_stream)
8597c478bd9Sstevel@tonic-gate 			strclean(vp);
8607c478bd9Sstevel@tonic-gate 	}
8617c478bd9Sstevel@tonic-gate 	if (count > 1)
8627c478bd9Sstevel@tonic-gate 		return (0);
8637c478bd9Sstevel@tonic-gate 
86425e8c5aaSvikram 	/* we allow close to succeed even if device is fenced off */
8657c478bd9Sstevel@tonic-gate 	sp = VTOS(vp);
8667c478bd9Sstevel@tonic-gate 	cvp = sp->s_commonvp;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	dev = sp->s_dev;
8697c478bd9Sstevel@tonic-gate 	type = vp->v_type;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	ASSERT(type == VCHR || type == VBLK);
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 	/*
8747c478bd9Sstevel@tonic-gate 	 * Prevent close/close and close/open races by serializing closes
8757c478bd9Sstevel@tonic-gate 	 * on this common snode. Clone opens are held up until after
8767c478bd9Sstevel@tonic-gate 	 * we have closed this device so the streams linkage is maintained
8777c478bd9Sstevel@tonic-gate 	 */
8787c478bd9Sstevel@tonic-gate 	csp = VTOS(cvp);
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	LOCK_CSP(csp);
8817c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	csp->s_count--;			/* one fewer open reference : SN_RELE */
8847c478bd9Sstevel@tonic-gate 	sysclone = sp->s_flag & SCLONE;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/*
8877c478bd9Sstevel@tonic-gate 	 * Invalidate size on each close.
8887c478bd9Sstevel@tonic-gate 	 *
8897c478bd9Sstevel@tonic-gate 	 * XXX We do this on each close because we don't have interfaces that
8907c478bd9Sstevel@tonic-gate 	 * allow a driver to invalidate the size.  Since clearing this on each
8917c478bd9Sstevel@tonic-gate 	 * close this causes property overhead we skip /dev/null and
8927c478bd9Sstevel@tonic-gate 	 * /dev/zero to avoid degrading kenbus performance.
8937c478bd9Sstevel@tonic-gate 	 */
8947c478bd9Sstevel@tonic-gate 	if (getmajor(dev) != mm_major)
8957c478bd9Sstevel@tonic-gate 		csp->s_flag &= ~SSIZEVALID;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 	/*
8987c478bd9Sstevel@tonic-gate 	 * Only call the close routine when the last open reference through
8997c478bd9Sstevel@tonic-gate 	 * any [s, v]node goes away.  This can be checked by looking at
9007c478bd9Sstevel@tonic-gate 	 * s_count on the common vnode.
9017c478bd9Sstevel@tonic-gate 	 */
9027c478bd9Sstevel@tonic-gate 	if ((csp->s_count == 0) && (csp->s_mapcnt == 0)) {
9037c478bd9Sstevel@tonic-gate 		/* we don't need a close */
9047c478bd9Sstevel@tonic-gate 		csp->s_flag &= ~(SNEEDCLOSE | SSIZEVALID);
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 		/*
9077c478bd9Sstevel@tonic-gate 		 * A cloning driver may open-clone to the same dev_t that we
9087c478bd9Sstevel@tonic-gate 		 * are closing before spec_inactive destroys the common snode.
9097c478bd9Sstevel@tonic-gate 		 * If this occurs the s_dip association needs to be reevaluated.
9107c478bd9Sstevel@tonic-gate 		 * We clear SDIPSET to force reevaluation in this case.  When
9117c478bd9Sstevel@tonic-gate 		 * reevaluation occurs (by spec_clone after open), if the
9127c478bd9Sstevel@tonic-gate 		 * devinfo association has changed then the old association
9137c478bd9Sstevel@tonic-gate 		 * will be released as the new association is established by
9147c478bd9Sstevel@tonic-gate 		 * spec_assoc_vp_with_devi().
9157c478bd9Sstevel@tonic-gate 		 */
9167c478bd9Sstevel@tonic-gate 		if (csp->s_flag & (SCLONE | SSELFCLONE))
9177c478bd9Sstevel@tonic-gate 			csp->s_flag &= ~SDIPSET;
9187c478bd9Sstevel@tonic-gate 
919e099bf07Scth 		csp->s_flag |= SCLOSING;
9207c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
9217c478bd9Sstevel@tonic-gate 		error = device_close(vp, flag, cr);
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 		/*
9247c478bd9Sstevel@tonic-gate 		 * Decrement the devops held in clnopen()
9257c478bd9Sstevel@tonic-gate 		 */
9267c478bd9Sstevel@tonic-gate 		if (sysclone) {
9277c478bd9Sstevel@tonic-gate 			ddi_rele_driver(getmajor(dev));
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 		mutex_enter(&csp->s_lock);
930e099bf07Scth 		csp->s_flag &= ~SCLOSING;
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate 	UNLOCK_CSP_LOCK_HELD(csp);
9347c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	return (error);
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
9407c478bd9Sstevel@tonic-gate static int
9417c478bd9Sstevel@tonic-gate spec_read(
9427c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
9437c478bd9Sstevel@tonic-gate 	struct uio	*uiop,
9447c478bd9Sstevel@tonic-gate 	int		ioflag,
9457c478bd9Sstevel@tonic-gate 	struct cred	*cr,
9467c478bd9Sstevel@tonic-gate 	struct caller_context *ct)
9477c478bd9Sstevel@tonic-gate {
9487c478bd9Sstevel@tonic-gate 	int error;
9497c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
9507c478bd9Sstevel@tonic-gate 	dev_t dev = sp->s_dev;
9517c478bd9Sstevel@tonic-gate 	size_t n;
9527c478bd9Sstevel@tonic-gate 	ulong_t on;
9537c478bd9Sstevel@tonic-gate 	u_offset_t bdevsize;
9547c478bd9Sstevel@tonic-gate 	offset_t maxoff;
9557c478bd9Sstevel@tonic-gate 	offset_t off;
9567c478bd9Sstevel@tonic-gate 	struct vnode *blkvp;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(dev))) {	/* stream */
9617c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR);
9627c478bd9Sstevel@tonic-gate 		smark(sp, SACC);
9637c478bd9Sstevel@tonic-gate 		return (strread(vp, uiop, cr));
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	if (uiop->uio_resid == 0)
9677c478bd9Sstevel@tonic-gate 		return (0);
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	/*
9707c478bd9Sstevel@tonic-gate 	 * Plain old character devices that set D_U64BIT can have
9717c478bd9Sstevel@tonic-gate 	 * unrestricted offsets.
9727c478bd9Sstevel@tonic-gate 	 */
9737c478bd9Sstevel@tonic-gate 	maxoff = spec_maxoffset(vp);
9747c478bd9Sstevel@tonic-gate 	ASSERT(maxoff != -1 || vp->v_type == VCHR);
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	if (maxoff != -1 && (uiop->uio_loffset < 0 ||
9777c478bd9Sstevel@tonic-gate 	    uiop->uio_loffset + uiop->uio_resid > maxoff))
9787c478bd9Sstevel@tonic-gate 		return (EINVAL);
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	if (vp->v_type == VCHR) {
9817c478bd9Sstevel@tonic-gate 		smark(sp, SACC);
9827c478bd9Sstevel@tonic-gate 		ASSERT(STREAMSTAB(getmajor(dev)) == 0);
9837c478bd9Sstevel@tonic-gate 		return (cdev_read(dev, uiop, cr));
9847c478bd9Sstevel@tonic-gate 	}
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	/*
9877c478bd9Sstevel@tonic-gate 	 * Block device.
9887c478bd9Sstevel@tonic-gate 	 */
9897c478bd9Sstevel@tonic-gate 	error = 0;
9907c478bd9Sstevel@tonic-gate 	blkvp = sp->s_commonvp;
9917c478bd9Sstevel@tonic-gate 	bdevsize = SPEC_SIZE(VTOS(blkvp));
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	do {
9947c478bd9Sstevel@tonic-gate 		caddr_t base;
9957c478bd9Sstevel@tonic-gate 		offset_t diff;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 		off = uiop->uio_loffset & (offset_t)MAXBMASK;
9987c478bd9Sstevel@tonic-gate 		on = (size_t)(uiop->uio_loffset & MAXBOFFSET);
9997c478bd9Sstevel@tonic-gate 		n = (size_t)MIN(MAXBSIZE - on, uiop->uio_resid);
10007c478bd9Sstevel@tonic-gate 		diff = bdevsize - uiop->uio_loffset;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 		if (diff <= 0)
10037c478bd9Sstevel@tonic-gate 			break;
10047c478bd9Sstevel@tonic-gate 		if (diff < n)
10057c478bd9Sstevel@tonic-gate 			n = (size_t)diff;
10067c478bd9Sstevel@tonic-gate 
1007a5652762Spraks 		if (vpm_enable) {
1008a5652762Spraks 			error = vpm_data_copy(blkvp, (u_offset_t)(off + on),
1009a5652762Spraks 			    n, uiop, 1, NULL, 0, S_READ);
1010a5652762Spraks 		} else {
10117c478bd9Sstevel@tonic-gate 			base = segmap_getmapflt(segkmap, blkvp,
10127c478bd9Sstevel@tonic-gate 			    (u_offset_t)(off + on), n, 1, S_READ);
10137c478bd9Sstevel@tonic-gate 
1014a5652762Spraks 			error = uiomove(base + on, n, UIO_READ, uiop);
1015a5652762Spraks 		}
1016a5652762Spraks 		if (!error) {
10177c478bd9Sstevel@tonic-gate 			int flags = 0;
10187c478bd9Sstevel@tonic-gate 			/*
10197c478bd9Sstevel@tonic-gate 			 * If we read a whole block, we won't need this
10207c478bd9Sstevel@tonic-gate 			 * buffer again soon.
10217c478bd9Sstevel@tonic-gate 			 */
10227c478bd9Sstevel@tonic-gate 			if (n + on == MAXBSIZE)
10237c478bd9Sstevel@tonic-gate 				flags = SM_DONTNEED | SM_FREE;
1024a5652762Spraks 			if (vpm_enable) {
1025a5652762Spraks 				error = vpm_sync_pages(blkvp, off, n, flags);
1026a5652762Spraks 			} else {
10277c478bd9Sstevel@tonic-gate 				error = segmap_release(segkmap, base, flags);
1028a5652762Spraks 			}
1029a5652762Spraks 		} else {
1030a5652762Spraks 			if (vpm_enable) {
1031a5652762Spraks 				(void) vpm_sync_pages(blkvp, off, n, 0);
10327c478bd9Sstevel@tonic-gate 			} else {
10337c478bd9Sstevel@tonic-gate 				(void) segmap_release(segkmap, base, 0);
1034a5652762Spraks 			}
10357c478bd9Sstevel@tonic-gate 			if (bdevsize == UNKNOWN_SIZE) {
10367c478bd9Sstevel@tonic-gate 				error = 0;
10377c478bd9Sstevel@tonic-gate 				break;
10387c478bd9Sstevel@tonic-gate 			}
10397c478bd9Sstevel@tonic-gate 		}
10407c478bd9Sstevel@tonic-gate 	} while (error == 0 && uiop->uio_resid > 0 && n != 0);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 	return (error);
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10467c478bd9Sstevel@tonic-gate static int
10477c478bd9Sstevel@tonic-gate spec_write(
10487c478bd9Sstevel@tonic-gate 	struct vnode *vp,
10497c478bd9Sstevel@tonic-gate 	struct uio *uiop,
10507c478bd9Sstevel@tonic-gate 	int ioflag,
10517c478bd9Sstevel@tonic-gate 	struct cred *cr,
10527c478bd9Sstevel@tonic-gate 	struct caller_context *ct)
10537c478bd9Sstevel@tonic-gate {
10547c478bd9Sstevel@tonic-gate 	int error;
10557c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
10567c478bd9Sstevel@tonic-gate 	dev_t dev = sp->s_dev;
10577c478bd9Sstevel@tonic-gate 	size_t n;
10587c478bd9Sstevel@tonic-gate 	ulong_t on;
10597c478bd9Sstevel@tonic-gate 	u_offset_t bdevsize;
10607c478bd9Sstevel@tonic-gate 	offset_t maxoff;
10617c478bd9Sstevel@tonic-gate 	offset_t off;
10627c478bd9Sstevel@tonic-gate 	struct vnode *blkvp;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VCHR || vp->v_type == VBLK);
10657c478bd9Sstevel@tonic-gate 
10667c478bd9Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(dev))) {
10677c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR);
10687c478bd9Sstevel@tonic-gate 		smark(sp, SUPD);
10697c478bd9Sstevel@tonic-gate 		return (strwrite(vp, uiop, cr));
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	/*
10737c478bd9Sstevel@tonic-gate 	 * Plain old character devices that set D_U64BIT can have
10747c478bd9Sstevel@tonic-gate 	 * unrestricted offsets.
10757c478bd9Sstevel@tonic-gate 	 */
10767c478bd9Sstevel@tonic-gate 	maxoff = spec_maxoffset(vp);
10777c478bd9Sstevel@tonic-gate 	ASSERT(maxoff != -1 || vp->v_type == VCHR);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	if (maxoff != -1 && (uiop->uio_loffset < 0 ||
10807c478bd9Sstevel@tonic-gate 	    uiop->uio_loffset + uiop->uio_resid > maxoff))
10817c478bd9Sstevel@tonic-gate 		return (EINVAL);
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	if (vp->v_type == VCHR) {
10847c478bd9Sstevel@tonic-gate 		smark(sp, SUPD);
10857c478bd9Sstevel@tonic-gate 		ASSERT(STREAMSTAB(getmajor(dev)) == 0);
10867c478bd9Sstevel@tonic-gate 		return (cdev_write(dev, uiop, cr));
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	if (uiop->uio_resid == 0)
10907c478bd9Sstevel@tonic-gate 		return (0);
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	error = 0;
10937c478bd9Sstevel@tonic-gate 	blkvp = sp->s_commonvp;
10947c478bd9Sstevel@tonic-gate 	bdevsize = SPEC_SIZE(VTOS(blkvp));
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 	do {
10977c478bd9Sstevel@tonic-gate 		int pagecreate;
10987c478bd9Sstevel@tonic-gate 		int newpage;
10997c478bd9Sstevel@tonic-gate 		caddr_t base;
11007c478bd9Sstevel@tonic-gate 		offset_t diff;
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 		off = uiop->uio_loffset & (offset_t)MAXBMASK;
11037c478bd9Sstevel@tonic-gate 		on = (ulong_t)(uiop->uio_loffset & MAXBOFFSET);
11047c478bd9Sstevel@tonic-gate 		n = (size_t)MIN(MAXBSIZE - on, uiop->uio_resid);
11057c478bd9Sstevel@tonic-gate 		pagecreate = 0;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 		diff = bdevsize - uiop->uio_loffset;
11087c478bd9Sstevel@tonic-gate 		if (diff <= 0) {
11097c478bd9Sstevel@tonic-gate 			error = ENXIO;
11107c478bd9Sstevel@tonic-gate 			break;
11117c478bd9Sstevel@tonic-gate 		}
11127c478bd9Sstevel@tonic-gate 		if (diff < n)
11137c478bd9Sstevel@tonic-gate 			n = (size_t)diff;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 		/*
11167c478bd9Sstevel@tonic-gate 		 * Check to see if we can skip reading in the page
11177c478bd9Sstevel@tonic-gate 		 * and just allocate the memory.  We can do this
11187c478bd9Sstevel@tonic-gate 		 * if we are going to rewrite the entire mapping
11197c478bd9Sstevel@tonic-gate 		 * or if we are going to write to end of the device
11207c478bd9Sstevel@tonic-gate 		 * from the beginning of the mapping.
11217c478bd9Sstevel@tonic-gate 		 */
11227c478bd9Sstevel@tonic-gate 		if (n == MAXBSIZE || (on == 0 && (off + n) == bdevsize))
11237c478bd9Sstevel@tonic-gate 			pagecreate = 1;
11247c478bd9Sstevel@tonic-gate 
1125a5652762Spraks 		newpage = 0;
1126a5652762Spraks 		if (vpm_enable) {
1127a5652762Spraks 			error = vpm_data_copy(blkvp, (u_offset_t)(off + on),
1128a5652762Spraks 			    n, uiop, !pagecreate, NULL, 0, S_WRITE);
1129a5652762Spraks 		} else {
11307c478bd9Sstevel@tonic-gate 			base = segmap_getmapflt(segkmap, blkvp,
11317c478bd9Sstevel@tonic-gate 			    (u_offset_t)(off + on), n, !pagecreate, S_WRITE);
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 			/*
11347c478bd9Sstevel@tonic-gate 			 * segmap_pagecreate() returns 1 if it calls
11357c478bd9Sstevel@tonic-gate 			 * page_create_va() to allocate any pages.
11367c478bd9Sstevel@tonic-gate 			 */
11377c478bd9Sstevel@tonic-gate 
11387c478bd9Sstevel@tonic-gate 			if (pagecreate)
11397c478bd9Sstevel@tonic-gate 				newpage = segmap_pagecreate(segkmap, base + on,
11407c478bd9Sstevel@tonic-gate 				    n, 0);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 			error = uiomove(base + on, n, UIO_WRITE, uiop);
1143a5652762Spraks 		}
11447c478bd9Sstevel@tonic-gate 
1145a5652762Spraks 		if (!vpm_enable && pagecreate &&
11467c478bd9Sstevel@tonic-gate 		    uiop->uio_loffset <
11477c478bd9Sstevel@tonic-gate 		    P2ROUNDUP_TYPED(off + on + n, PAGESIZE, offset_t)) {
11487c478bd9Sstevel@tonic-gate 			/*
11497c478bd9Sstevel@tonic-gate 			 * We created pages w/o initializing them completely,
11507c478bd9Sstevel@tonic-gate 			 * thus we need to zero the part that wasn't set up.
11517c478bd9Sstevel@tonic-gate 			 * This can happen if we write to the end of the device
11527c478bd9Sstevel@tonic-gate 			 * or if we had some sort of error during the uiomove.
11537c478bd9Sstevel@tonic-gate 			 */
11547c478bd9Sstevel@tonic-gate 			long nzero;
11557c478bd9Sstevel@tonic-gate 			offset_t nmoved;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 			nmoved = (uiop->uio_loffset - (off + on));
11587c478bd9Sstevel@tonic-gate 			if (nmoved < 0 || nmoved > n) {
11597c478bd9Sstevel@tonic-gate 				panic("spec_write: nmoved bogus");
11607c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
11617c478bd9Sstevel@tonic-gate 			}
11627c478bd9Sstevel@tonic-gate 			nzero = (long)P2ROUNDUP(on + n, PAGESIZE) -
11637c478bd9Sstevel@tonic-gate 			    (on + nmoved);
11647c478bd9Sstevel@tonic-gate 			if (nzero < 0 || (on + nmoved + nzero > MAXBSIZE)) {
11657c478bd9Sstevel@tonic-gate 				panic("spec_write: nzero bogus");
11667c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
11677c478bd9Sstevel@tonic-gate 			}
11687c478bd9Sstevel@tonic-gate 			(void) kzero(base + on + nmoved, (size_t)nzero);
11697c478bd9Sstevel@tonic-gate 		}
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 		/*
11727c478bd9Sstevel@tonic-gate 		 * Unlock the pages which have been allocated by
11737c478bd9Sstevel@tonic-gate 		 * page_create_va() in segmap_pagecreate().
11747c478bd9Sstevel@tonic-gate 		 */
1175a5652762Spraks 		if (!vpm_enable && newpage)
11767c478bd9Sstevel@tonic-gate 			segmap_pageunlock(segkmap, base + on,
11777c478bd9Sstevel@tonic-gate 			    (size_t)n, S_WRITE);
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 		if (error == 0) {
11807c478bd9Sstevel@tonic-gate 			int flags = 0;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 			/*
11837c478bd9Sstevel@tonic-gate 			 * Force write back for synchronous write cases.
11847c478bd9Sstevel@tonic-gate 			 */
11857c478bd9Sstevel@tonic-gate 			if (ioflag & (FSYNC|FDSYNC))
11867c478bd9Sstevel@tonic-gate 				flags = SM_WRITE;
11877c478bd9Sstevel@tonic-gate 			else if (n + on == MAXBSIZE || IS_SWAPVP(vp)) {
11887c478bd9Sstevel@tonic-gate 				/*
11897c478bd9Sstevel@tonic-gate 				 * Have written a whole block.
11907c478bd9Sstevel@tonic-gate 				 * Start an asynchronous write and
11917c478bd9Sstevel@tonic-gate 				 * mark the buffer to indicate that
11927c478bd9Sstevel@tonic-gate 				 * it won't be needed again soon.
11937c478bd9Sstevel@tonic-gate 				 * Push swap files here, since it
11947c478bd9Sstevel@tonic-gate 				 * won't happen anywhere else.
11957c478bd9Sstevel@tonic-gate 				 */
11967c478bd9Sstevel@tonic-gate 				flags = SM_WRITE | SM_ASYNC | SM_DONTNEED;
11977c478bd9Sstevel@tonic-gate 			}
11987c478bd9Sstevel@tonic-gate 			smark(sp, SUPD|SCHG);
1199a5652762Spraks 			if (vpm_enable) {
1200a5652762Spraks 				error = vpm_sync_pages(blkvp, off, n, flags);
1201a5652762Spraks 			} else {
12027c478bd9Sstevel@tonic-gate 				error = segmap_release(segkmap, base, flags);
1203a5652762Spraks 			}
1204a5652762Spraks 		} else {
1205a5652762Spraks 			if (vpm_enable) {
1206a5652762Spraks 				(void) vpm_sync_pages(blkvp, off, n, SM_INVAL);
1207a5652762Spraks 			} else {
12087c478bd9Sstevel@tonic-gate 				(void) segmap_release(segkmap, base, SM_INVAL);
1209a5652762Spraks 			}
1210a5652762Spraks 		}
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 	} while (error == 0 && uiop->uio_resid > 0 && n != 0);
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate 	return (error);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate static int
12187c478bd9Sstevel@tonic-gate spec_ioctl(struct vnode *vp, int cmd, intptr_t arg, int mode, struct cred *cr,
12197c478bd9Sstevel@tonic-gate     int *rvalp)
12207c478bd9Sstevel@tonic-gate {
12217c478bd9Sstevel@tonic-gate 	struct snode *sp;
12227c478bd9Sstevel@tonic-gate 	dev_t dev;
12237c478bd9Sstevel@tonic-gate 	int error;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	if (vp->v_type != VCHR)
12267c478bd9Sstevel@tonic-gate 		return (ENOTTY);
122725e8c5aaSvikram 
122825e8c5aaSvikram 	/*
122925e8c5aaSvikram 	 * allow ioctls() to go through even for fenced snodes, as they
123025e8c5aaSvikram 	 * may include unconfiguration operation - for example popping of
123125e8c5aaSvikram 	 * streams modules.
123225e8c5aaSvikram 	 */
123325e8c5aaSvikram 
12347c478bd9Sstevel@tonic-gate 	sp = VTOS(vp);
12357c478bd9Sstevel@tonic-gate 	dev = sp->s_dev;
12367c478bd9Sstevel@tonic-gate 	if (STREAMSTAB(getmajor(dev))) {
12377c478bd9Sstevel@tonic-gate 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
12387c478bd9Sstevel@tonic-gate 	} else {
12397c478bd9Sstevel@tonic-gate 		error = cdev_ioctl(dev, cmd, arg, mode, cr, rvalp);
12407c478bd9Sstevel@tonic-gate 	}
12417c478bd9Sstevel@tonic-gate 	return (error);
12427c478bd9Sstevel@tonic-gate }
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate static int
12457c478bd9Sstevel@tonic-gate spec_getattr(struct vnode *vp, struct vattr *vap, int flags, struct cred *cr)
12467c478bd9Sstevel@tonic-gate {
12477c478bd9Sstevel@tonic-gate 	int error;
12487c478bd9Sstevel@tonic-gate 	struct snode *sp;
12497c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	/* With ATTR_COMM we will not get attributes from realvp */
12527c478bd9Sstevel@tonic-gate 	if (flags & ATTR_COMM) {
12537c478bd9Sstevel@tonic-gate 		sp = VTOS(vp);
12547c478bd9Sstevel@tonic-gate 		vp = sp->s_commonvp;
12557c478bd9Sstevel@tonic-gate 	}
12567c478bd9Sstevel@tonic-gate 	sp = VTOS(vp);
125725e8c5aaSvikram 
125825e8c5aaSvikram 	/* we want stat() to fail with ENXIO if the device is fenced off */
125925e8c5aaSvikram 	if (S_ISFENCED(sp))
126025e8c5aaSvikram 		return (ENXIO);
126125e8c5aaSvikram 
12627c478bd9Sstevel@tonic-gate 	realvp = sp->s_realvp;
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 	if (realvp == NULL) {
12657c478bd9Sstevel@tonic-gate 		static int snode_shift	= 0;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		/*
12687c478bd9Sstevel@tonic-gate 		 * Calculate the amount of bitshift to a snode pointer which
12697c478bd9Sstevel@tonic-gate 		 * will still keep it unique.  See below.
12707c478bd9Sstevel@tonic-gate 		 */
12717c478bd9Sstevel@tonic-gate 		if (snode_shift == 0)
12727c478bd9Sstevel@tonic-gate 			snode_shift = highbit(sizeof (struct snode));
12737c478bd9Sstevel@tonic-gate 		ASSERT(snode_shift > 0);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 		/*
12767c478bd9Sstevel@tonic-gate 		 * No real vnode behind this one.  Fill in the fields
12777c478bd9Sstevel@tonic-gate 		 * from the snode.
12787c478bd9Sstevel@tonic-gate 		 *
12797c478bd9Sstevel@tonic-gate 		 * This code should be refined to return only the
12807c478bd9Sstevel@tonic-gate 		 * attributes asked for instead of all of them.
12817c478bd9Sstevel@tonic-gate 		 */
12827c478bd9Sstevel@tonic-gate 		vap->va_type = vp->v_type;
12837c478bd9Sstevel@tonic-gate 		vap->va_mode = 0;
12847c478bd9Sstevel@tonic-gate 		vap->va_uid = vap->va_gid = 0;
12857c478bd9Sstevel@tonic-gate 		vap->va_fsid = sp->s_fsid;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 		/*
12887c478bd9Sstevel@tonic-gate 		 * If the va_nodeid is > MAX_USHORT, then i386 stats might
12897c478bd9Sstevel@tonic-gate 		 * fail. So we shift down the snode pointer to try and get
12907c478bd9Sstevel@tonic-gate 		 * the most uniqueness into 16-bits.
12917c478bd9Sstevel@tonic-gate 		 */
12927c478bd9Sstevel@tonic-gate 		vap->va_nodeid = ((ino64_t)(uintptr_t)sp >> snode_shift) &
12937c478bd9Sstevel@tonic-gate 		    0xFFFF;
12947c478bd9Sstevel@tonic-gate 		vap->va_nlink = 0;
12957c478bd9Sstevel@tonic-gate 		vap->va_rdev = sp->s_dev;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 		/*
12987c478bd9Sstevel@tonic-gate 		 * va_nblocks is the number of 512 byte blocks used to store
12997c478bd9Sstevel@tonic-gate 		 * the mknod for the device, not the number of blocks on the
13007c478bd9Sstevel@tonic-gate 		 * device itself.  This is typically zero since the mknod is
13017c478bd9Sstevel@tonic-gate 		 * represented directly in the inode itself.
13027c478bd9Sstevel@tonic-gate 		 */
13037c478bd9Sstevel@tonic-gate 		vap->va_nblocks = 0;
13047c478bd9Sstevel@tonic-gate 	} else {
13057c478bd9Sstevel@tonic-gate 		error = VOP_GETATTR(realvp, vap, flags, cr);
13067c478bd9Sstevel@tonic-gate 		if (error != 0)
13077c478bd9Sstevel@tonic-gate 			return (error);
13087c478bd9Sstevel@tonic-gate 	}
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	/* set the size from the snode */
13117c478bd9Sstevel@tonic-gate 	vap->va_size = SPEC_SIZE(VTOS(sp->s_commonvp));
13127c478bd9Sstevel@tonic-gate 	vap->va_blksize = MAXBSIZE;
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
13157c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_sec = sp->s_atime;
13167c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_sec = sp->s_mtime;
13177c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_sec = sp->s_ctime;
13187c478bd9Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	vap->va_atime.tv_nsec = 0;
13217c478bd9Sstevel@tonic-gate 	vap->va_mtime.tv_nsec = 0;
13227c478bd9Sstevel@tonic-gate 	vap->va_ctime.tv_nsec = 0;
13237c478bd9Sstevel@tonic-gate 	vap->va_seq = 0;
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	return (0);
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate 
13287c478bd9Sstevel@tonic-gate static int
13297c478bd9Sstevel@tonic-gate spec_setattr(
13307c478bd9Sstevel@tonic-gate 	struct vnode *vp,
13317c478bd9Sstevel@tonic-gate 	struct vattr *vap,
13327c478bd9Sstevel@tonic-gate 	int flags,
13337c478bd9Sstevel@tonic-gate 	struct cred *cr,
13347c478bd9Sstevel@tonic-gate 	caller_context_t *ctp)
13357c478bd9Sstevel@tonic-gate {
13367c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
13377c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
13387c478bd9Sstevel@tonic-gate 	int error;
13397c478bd9Sstevel@tonic-gate 
134025e8c5aaSvikram 	/* fail with ENXIO if the device is fenced off */
134125e8c5aaSvikram 	if (S_ISFENCED(sp))
134225e8c5aaSvikram 		return (ENXIO);
134325e8c5aaSvikram 
13447c478bd9Sstevel@tonic-gate 	if (vp->v_type == VCHR && vp->v_stream && (vap->va_mask & AT_SIZE)) {
13457c478bd9Sstevel@tonic-gate 		/*
13467c478bd9Sstevel@tonic-gate 		 * 1135080:	O_TRUNC should have no effect on
13477c478bd9Sstevel@tonic-gate 		 *		named pipes and terminal devices.
13487c478bd9Sstevel@tonic-gate 		 */
13497c478bd9Sstevel@tonic-gate 		ASSERT(vap->va_mask == AT_SIZE);
13507c478bd9Sstevel@tonic-gate 		return (0);
13517c478bd9Sstevel@tonic-gate 	}
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	if ((realvp = sp->s_realvp) == NULL)
13547c478bd9Sstevel@tonic-gate 		error = 0;	/* no real vnode to update */
13557c478bd9Sstevel@tonic-gate 	else
13567c478bd9Sstevel@tonic-gate 		error = VOP_SETATTR(realvp, vap, flags, cr, ctp);
13577c478bd9Sstevel@tonic-gate 	if (error == 0) {
13587c478bd9Sstevel@tonic-gate 		/*
13597c478bd9Sstevel@tonic-gate 		 * If times were changed, update snode.
13607c478bd9Sstevel@tonic-gate 		 */
13617c478bd9Sstevel@tonic-gate 		mutex_enter(&sp->s_lock);
13627c478bd9Sstevel@tonic-gate 		if (vap->va_mask & AT_ATIME)
13637c478bd9Sstevel@tonic-gate 			sp->s_atime = vap->va_atime.tv_sec;
13647c478bd9Sstevel@tonic-gate 		if (vap->va_mask & AT_MTIME) {
13657c478bd9Sstevel@tonic-gate 			sp->s_mtime = vap->va_mtime.tv_sec;
13667c478bd9Sstevel@tonic-gate 			sp->s_ctime = gethrestime_sec();
13677c478bd9Sstevel@tonic-gate 		}
13687c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
13697c478bd9Sstevel@tonic-gate 	}
13707c478bd9Sstevel@tonic-gate 	return (error);
13717c478bd9Sstevel@tonic-gate }
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate static int
13747c478bd9Sstevel@tonic-gate spec_access(struct vnode *vp, int mode, int flags, struct cred *cr)
13757c478bd9Sstevel@tonic-gate {
13767c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
13777c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
13787c478bd9Sstevel@tonic-gate 
137925e8c5aaSvikram 	/* fail with ENXIO if the device is fenced off */
138025e8c5aaSvikram 	if (S_ISFENCED(sp))
138125e8c5aaSvikram 		return (ENXIO);
138225e8c5aaSvikram 
13837c478bd9Sstevel@tonic-gate 	if ((realvp = sp->s_realvp) != NULL)
13847c478bd9Sstevel@tonic-gate 		return (VOP_ACCESS(realvp, mode, flags, cr));
13857c478bd9Sstevel@tonic-gate 	else
13867c478bd9Sstevel@tonic-gate 		return (0);	/* Allow all access. */
13877c478bd9Sstevel@tonic-gate }
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate /*
13907c478bd9Sstevel@tonic-gate  * This can be called if creat or an open with O_CREAT is done on the root
13917c478bd9Sstevel@tonic-gate  * of a lofs mount where the mounted entity is a special file.
13927c478bd9Sstevel@tonic-gate  */
13937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
13947c478bd9Sstevel@tonic-gate static int
13957c478bd9Sstevel@tonic-gate spec_create(struct vnode *dvp, char *name, vattr_t *vap, enum vcexcl excl,
13967c478bd9Sstevel@tonic-gate     int mode, struct vnode **vpp, struct cred *cr, int flag)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	int error;
139925e8c5aaSvikram 	struct snode *sp = VTOS(dvp);
140025e8c5aaSvikram 
140125e8c5aaSvikram 	/* fail with ENXIO if the device is fenced off */
140225e8c5aaSvikram 	if (S_ISFENCED(sp))
140325e8c5aaSvikram 		return (ENXIO);
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate 	ASSERT(dvp && (dvp->v_flag & VROOT) && *name == '\0');
14067c478bd9Sstevel@tonic-gate 	if (excl == NONEXCL) {
14077c478bd9Sstevel@tonic-gate 		if (mode && (error = spec_access(dvp, mode, 0, cr)))
14087c478bd9Sstevel@tonic-gate 			return (error);
14097c478bd9Sstevel@tonic-gate 		VN_HOLD(dvp);
14107c478bd9Sstevel@tonic-gate 		return (0);
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate 	return (EEXIST);
14137c478bd9Sstevel@tonic-gate }
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate /*
14167c478bd9Sstevel@tonic-gate  * In order to sync out the snode times without multi-client problems,
14177c478bd9Sstevel@tonic-gate  * make sure the times written out are never earlier than the times
14187c478bd9Sstevel@tonic-gate  * already set in the vnode.
14197c478bd9Sstevel@tonic-gate  */
14207c478bd9Sstevel@tonic-gate static int
14217c478bd9Sstevel@tonic-gate spec_fsync(struct vnode *vp, int syncflag, struct cred *cr)
14227c478bd9Sstevel@tonic-gate {
14237c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
14247c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
14257c478bd9Sstevel@tonic-gate 	struct vnode *cvp;
14267c478bd9Sstevel@tonic-gate 	struct vattr va, vatmp;
14277c478bd9Sstevel@tonic-gate 
142825e8c5aaSvikram 	/* allow syncing even if device is fenced off */
142925e8c5aaSvikram 
14307c478bd9Sstevel@tonic-gate 	/* If times didn't change, don't flush anything. */
14317c478bd9Sstevel@tonic-gate 	mutex_enter(&sp->s_lock);
14327c478bd9Sstevel@tonic-gate 	if ((sp->s_flag & (SACC|SUPD|SCHG)) == 0 && vp->v_type != VBLK) {
14337c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
14347c478bd9Sstevel@tonic-gate 		return (0);
14357c478bd9Sstevel@tonic-gate 	}
14367c478bd9Sstevel@tonic-gate 	sp->s_flag &= ~(SACC|SUPD|SCHG);
14377c478bd9Sstevel@tonic-gate 	mutex_exit(&sp->s_lock);
14387c478bd9Sstevel@tonic-gate 	cvp = sp->s_commonvp;
14397c478bd9Sstevel@tonic-gate 	realvp = sp->s_realvp;
14407c478bd9Sstevel@tonic-gate 
14417c478bd9Sstevel@tonic-gate 	if (vp->v_type == VBLK && cvp != vp && vn_has_cached_data(cvp) &&
14427c478bd9Sstevel@tonic-gate 	    (cvp->v_flag & VISSWAP) == 0)
14437c478bd9Sstevel@tonic-gate 		(void) VOP_PUTPAGE(cvp, (offset_t)0, 0, 0, cr);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	/*
1446feb08c6bSbillm 	 * For devices that support it, force write cache to stable storage.
1447feb08c6bSbillm 	 * We don't need the lock to check s_flags since we can treat
1448feb08c6bSbillm 	 * SNOFLUSH as a hint.
1449feb08c6bSbillm 	 */
1450feb08c6bSbillm 	if ((vp->v_type == VBLK || vp->v_type == VCHR) &&
1451feb08c6bSbillm 	    !(sp->s_flag & SNOFLUSH)) {
1452feb08c6bSbillm 		int rval, rc;
1453feb08c6bSbillm 		rc = cdev_ioctl(vp->v_rdev, DKIOCFLUSHWRITECACHE,
1454feb08c6bSbillm 		    NULL, FNATIVE|FKIOCTL, cr, &rval);
1455feb08c6bSbillm 		if (rc == ENOTSUP || rc == ENOTTY) {
1456feb08c6bSbillm 			mutex_enter(&sp->s_lock);
1457feb08c6bSbillm 			sp->s_flag |= SNOFLUSH;
1458feb08c6bSbillm 			mutex_exit(&sp->s_lock);
1459feb08c6bSbillm 		}
1460feb08c6bSbillm 	}
1461feb08c6bSbillm 
1462feb08c6bSbillm 	/*
14637c478bd9Sstevel@tonic-gate 	 * If no real vnode to update, don't flush anything.
14647c478bd9Sstevel@tonic-gate 	 */
14657c478bd9Sstevel@tonic-gate 	if (realvp == NULL)
14667c478bd9Sstevel@tonic-gate 		return (0);
14677c478bd9Sstevel@tonic-gate 
14687c478bd9Sstevel@tonic-gate 	vatmp.va_mask = AT_ATIME|AT_MTIME;
14697c478bd9Sstevel@tonic-gate 	if (VOP_GETATTR(realvp, &vatmp, 0, cr) == 0) {
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 		mutex_enter(&sp->s_lock);
14727c478bd9Sstevel@tonic-gate 		if (vatmp.va_atime.tv_sec > sp->s_atime)
14737c478bd9Sstevel@tonic-gate 			va.va_atime = vatmp.va_atime;
14747c478bd9Sstevel@tonic-gate 		else {
14757c478bd9Sstevel@tonic-gate 			va.va_atime.tv_sec = sp->s_atime;
14767c478bd9Sstevel@tonic-gate 			va.va_atime.tv_nsec = 0;
14777c478bd9Sstevel@tonic-gate 		}
14787c478bd9Sstevel@tonic-gate 		if (vatmp.va_mtime.tv_sec > sp->s_mtime)
14797c478bd9Sstevel@tonic-gate 			va.va_mtime = vatmp.va_mtime;
14807c478bd9Sstevel@tonic-gate 		else {
14817c478bd9Sstevel@tonic-gate 			va.va_mtime.tv_sec = sp->s_mtime;
14827c478bd9Sstevel@tonic-gate 			va.va_mtime.tv_nsec = 0;
14837c478bd9Sstevel@tonic-gate 		}
14847c478bd9Sstevel@tonic-gate 		mutex_exit(&sp->s_lock);
14857c478bd9Sstevel@tonic-gate 
14867c478bd9Sstevel@tonic-gate 		va.va_mask = AT_ATIME|AT_MTIME;
14877c478bd9Sstevel@tonic-gate 		(void) VOP_SETATTR(realvp, &va, 0, cr, NULL);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 	(void) VOP_FSYNC(realvp, syncflag, cr);
14907c478bd9Sstevel@tonic-gate 	return (0);
14917c478bd9Sstevel@tonic-gate }
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14947c478bd9Sstevel@tonic-gate static void
14957c478bd9Sstevel@tonic-gate spec_inactive(struct vnode *vp, struct cred *cr)
14967c478bd9Sstevel@tonic-gate {
14977c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
14987c478bd9Sstevel@tonic-gate 	struct vnode *cvp;
14997c478bd9Sstevel@tonic-gate 	struct vnode *rvp;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	/*
15027c478bd9Sstevel@tonic-gate 	 * If no one has reclaimed the vnode, remove from the
15037c478bd9Sstevel@tonic-gate 	 * cache now.
15047c478bd9Sstevel@tonic-gate 	 */
15057c478bd9Sstevel@tonic-gate 	if (vp->v_count < 1) {
15067c478bd9Sstevel@tonic-gate 		panic("spec_inactive: Bad v_count");
15077c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
15087c478bd9Sstevel@tonic-gate 	}
15097c478bd9Sstevel@tonic-gate 	mutex_enter(&stable_lock);
15107c478bd9Sstevel@tonic-gate 
15117c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
15127c478bd9Sstevel@tonic-gate 	/*
15137c478bd9Sstevel@tonic-gate 	 * Drop the temporary hold by vn_rele now
15147c478bd9Sstevel@tonic-gate 	 */
15157c478bd9Sstevel@tonic-gate 	if (--vp->v_count != 0) {
15167c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
15177c478bd9Sstevel@tonic-gate 		mutex_exit(&stable_lock);
15187c478bd9Sstevel@tonic-gate 		return;
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	sdelete(sp);
15237c478bd9Sstevel@tonic-gate 	mutex_exit(&stable_lock);
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	/* We are the sole owner of sp now */
15267c478bd9Sstevel@tonic-gate 	cvp = sp->s_commonvp;
15277c478bd9Sstevel@tonic-gate 	rvp = sp->s_realvp;
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 	if (rvp) {
15307c478bd9Sstevel@tonic-gate 		/*
15317c478bd9Sstevel@tonic-gate 		 * If the snode times changed, then update the times
15327c478bd9Sstevel@tonic-gate 		 * associated with the "realvp".
15337c478bd9Sstevel@tonic-gate 		 */
15347c478bd9Sstevel@tonic-gate 		if ((sp->s_flag & (SACC|SUPD|SCHG)) != 0) {
15357c478bd9Sstevel@tonic-gate 
15367c478bd9Sstevel@tonic-gate 			struct vattr va, vatmp;
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 			mutex_enter(&sp->s_lock);
15397c478bd9Sstevel@tonic-gate 			sp->s_flag &= ~(SACC|SUPD|SCHG);
15407c478bd9Sstevel@tonic-gate 			mutex_exit(&sp->s_lock);
15417c478bd9Sstevel@tonic-gate 			vatmp.va_mask = AT_ATIME|AT_MTIME;
15427c478bd9Sstevel@tonic-gate 			/*
15437c478bd9Sstevel@tonic-gate 			 * The user may not own the device, but we
15447c478bd9Sstevel@tonic-gate 			 * want to update the attributes anyway.
15457c478bd9Sstevel@tonic-gate 			 */
15467c478bd9Sstevel@tonic-gate 			if (VOP_GETATTR(rvp, &vatmp, 0, kcred) == 0) {
15477c478bd9Sstevel@tonic-gate 				if (vatmp.va_atime.tv_sec > sp->s_atime)
15487c478bd9Sstevel@tonic-gate 					va.va_atime = vatmp.va_atime;
15497c478bd9Sstevel@tonic-gate 				else {
15507c478bd9Sstevel@tonic-gate 					va.va_atime.tv_sec = sp->s_atime;
15517c478bd9Sstevel@tonic-gate 					va.va_atime.tv_nsec = 0;
15527c478bd9Sstevel@tonic-gate 				}
15537c478bd9Sstevel@tonic-gate 				if (vatmp.va_mtime.tv_sec > sp->s_mtime)
15547c478bd9Sstevel@tonic-gate 					va.va_mtime = vatmp.va_mtime;
15557c478bd9Sstevel@tonic-gate 				else {
15567c478bd9Sstevel@tonic-gate 					va.va_mtime.tv_sec = sp->s_mtime;
15577c478bd9Sstevel@tonic-gate 					va.va_mtime.tv_nsec = 0;
15587c478bd9Sstevel@tonic-gate 				}
15597c478bd9Sstevel@tonic-gate 
15607c478bd9Sstevel@tonic-gate 				va.va_mask = AT_ATIME|AT_MTIME;
15617c478bd9Sstevel@tonic-gate 				(void) VOP_SETATTR(rvp, &va, 0, kcred, NULL);
15627c478bd9Sstevel@tonic-gate 			}
15637c478bd9Sstevel@tonic-gate 		}
15647c478bd9Sstevel@tonic-gate 	}
15657c478bd9Sstevel@tonic-gate 	ASSERT(!vn_has_cached_data(vp));
15667c478bd9Sstevel@tonic-gate 	vn_invalid(vp);
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate 	/* if we are sharing another file systems vfs, release it */
15697c478bd9Sstevel@tonic-gate 	if (vp->v_vfsp && (vp->v_vfsp != &spec_vfs))
15707c478bd9Sstevel@tonic-gate 		VFS_RELE(vp->v_vfsp);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	/* if we have a realvp, release the realvp */
15737c478bd9Sstevel@tonic-gate 	if (rvp)
15747c478bd9Sstevel@tonic-gate 		VN_RELE(rvp);
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	/* if we have a common, release the common */
1577fbe27353Sedp 	if (cvp && (cvp != vp)) {
15787c478bd9Sstevel@tonic-gate 		VN_RELE(cvp);
1579fbe27353Sedp #ifdef DEBUG
1580fbe27353Sedp 	} else if (cvp) {
1581fbe27353Sedp 		/*
1582fbe27353Sedp 		 * if this is the last reference to a common vnode, any
1583fbe27353Sedp 		 * associated stream had better have been closed
1584fbe27353Sedp 		 */
1585fbe27353Sedp 		ASSERT(cvp == vp);
1586fbe27353Sedp 		ASSERT(cvp->v_stream == NULL);
1587fbe27353Sedp #endif /* DEBUG */
1588fbe27353Sedp 	}
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	/*
15917c478bd9Sstevel@tonic-gate 	 * if we have a hold on a devinfo node (established by
15927c478bd9Sstevel@tonic-gate 	 * spec_assoc_vp_with_devi), release the hold
15937c478bd9Sstevel@tonic-gate 	 */
15947c478bd9Sstevel@tonic-gate 	if (sp->s_dip)
15957c478bd9Sstevel@tonic-gate 		ddi_release_devi(sp->s_dip);
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	/*
15987c478bd9Sstevel@tonic-gate 	 * If we have an associated device policy, release it.
15997c478bd9Sstevel@tonic-gate 	 */
16007c478bd9Sstevel@tonic-gate 	if (sp->s_plcy != NULL)
16017c478bd9Sstevel@tonic-gate 		dpfree(sp->s_plcy);
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	/*
16047c478bd9Sstevel@tonic-gate 	 * If all holds on the devinfo node are through specfs/devfs
16057c478bd9Sstevel@tonic-gate 	 * and we just destroyed the last specfs node associated with the
16067c478bd9Sstevel@tonic-gate 	 * device, then the devinfo node reference count should now be
16077c478bd9Sstevel@tonic-gate 	 * zero.  We can't check this because there may be other holds
16087c478bd9Sstevel@tonic-gate 	 * on the node from non file system sources: ddi_hold_devi_by_instance
16097c478bd9Sstevel@tonic-gate 	 * for example.
16107c478bd9Sstevel@tonic-gate 	 */
16117c478bd9Sstevel@tonic-gate 	kmem_cache_free(snode_cache, sp);
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate 
16147c478bd9Sstevel@tonic-gate static int
16157c478bd9Sstevel@tonic-gate spec_fid(struct vnode *vp, struct fid *fidp)
16167c478bd9Sstevel@tonic-gate {
16177c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
16187c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 	if ((realvp = sp->s_realvp) != NULL)
16217c478bd9Sstevel@tonic-gate 		return (VOP_FID(realvp, fidp));
16227c478bd9Sstevel@tonic-gate 	else
16237c478bd9Sstevel@tonic-gate 		return (EINVAL);
16247c478bd9Sstevel@tonic-gate }
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
16277c478bd9Sstevel@tonic-gate static int
16287c478bd9Sstevel@tonic-gate spec_seek(struct vnode *vp, offset_t ooff, offset_t *noffp)
16297c478bd9Sstevel@tonic-gate {
16307c478bd9Sstevel@tonic-gate 	offset_t maxoff = spec_maxoffset(vp);
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	if (maxoff == -1 || *noffp <= maxoff)
16337c478bd9Sstevel@tonic-gate 		return (0);
16347c478bd9Sstevel@tonic-gate 	else
16357c478bd9Sstevel@tonic-gate 		return (EINVAL);
16367c478bd9Sstevel@tonic-gate }
16377c478bd9Sstevel@tonic-gate 
16387c478bd9Sstevel@tonic-gate static int
16397c478bd9Sstevel@tonic-gate spec_frlock(
16407c478bd9Sstevel@tonic-gate 	struct vnode *vp,
16417c478bd9Sstevel@tonic-gate 	int		cmd,
16427c478bd9Sstevel@tonic-gate 	struct flock64	*bfp,
16437c478bd9Sstevel@tonic-gate 	int		flag,
16447c478bd9Sstevel@tonic-gate 	offset_t	offset,
16457c478bd9Sstevel@tonic-gate 	struct flk_callback *flk_cbp,
16467c478bd9Sstevel@tonic-gate 	struct cred	*cr)
16477c478bd9Sstevel@tonic-gate {
16487c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
16497c478bd9Sstevel@tonic-gate 	struct snode *csp;
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	csp = VTOS(sp->s_commonvp);
16527c478bd9Sstevel@tonic-gate 	/*
16537c478bd9Sstevel@tonic-gate 	 * If file is being mapped, disallow frlock.
16547c478bd9Sstevel@tonic-gate 	 */
16557c478bd9Sstevel@tonic-gate 	if (csp->s_mapcnt > 0)
16567c478bd9Sstevel@tonic-gate 		return (EAGAIN);
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr));
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
16617c478bd9Sstevel@tonic-gate static int
16627c478bd9Sstevel@tonic-gate spec_realvp(struct vnode *vp, struct vnode **vpp)
16637c478bd9Sstevel@tonic-gate {
16647c478bd9Sstevel@tonic-gate 	struct vnode *rvp;
16657c478bd9Sstevel@tonic-gate 
16667c478bd9Sstevel@tonic-gate 	if ((rvp = VTOS(vp)->s_realvp) != NULL) {
16677c478bd9Sstevel@tonic-gate 		vp = rvp;
16687c478bd9Sstevel@tonic-gate 		if (VOP_REALVP(vp, &rvp) == 0)
16697c478bd9Sstevel@tonic-gate 			vp = rvp;
16707c478bd9Sstevel@tonic-gate 	}
16717c478bd9Sstevel@tonic-gate 
16727c478bd9Sstevel@tonic-gate 	*vpp = vp;
16737c478bd9Sstevel@tonic-gate 	return (0);
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate /*
16777c478bd9Sstevel@tonic-gate  * Return all the pages from [off..off + len] in block
16787c478bd9Sstevel@tonic-gate  * or character device.
16797c478bd9Sstevel@tonic-gate  */
16807c478bd9Sstevel@tonic-gate static int
16817c478bd9Sstevel@tonic-gate spec_getpage(
16827c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
16837c478bd9Sstevel@tonic-gate 	offset_t	off,
16847c478bd9Sstevel@tonic-gate 	size_t		len,
16857c478bd9Sstevel@tonic-gate 	uint_t		*protp,
16867c478bd9Sstevel@tonic-gate 	page_t		*pl[],
16877c478bd9Sstevel@tonic-gate 	size_t		plsz,
16887c478bd9Sstevel@tonic-gate 	struct seg	*seg,
16897c478bd9Sstevel@tonic-gate 	caddr_t		addr,
16907c478bd9Sstevel@tonic-gate 	enum seg_rw	rw,
16917c478bd9Sstevel@tonic-gate 	struct cred	*cr)
16927c478bd9Sstevel@tonic-gate {
16937c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
16947c478bd9Sstevel@tonic-gate 	int err;
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate 	ASSERT(sp->s_commonvp == vp);
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate 	/*
16997c478bd9Sstevel@tonic-gate 	 * XXX	Given the above assertion, this might not do
17007c478bd9Sstevel@tonic-gate 	 *	what is wanted here.
17017c478bd9Sstevel@tonic-gate 	 */
17027c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
17037c478bd9Sstevel@tonic-gate 		return (ENOSYS);
17047c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_SPECFS, TR_SPECFS_GETPAGE,
17057c478bd9Sstevel@tonic-gate 	    "specfs getpage:vp %p off %llx len %ld snode %p",
17067c478bd9Sstevel@tonic-gate 	    vp, off, len, sp);
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	switch (vp->v_type) {
17097c478bd9Sstevel@tonic-gate 	case VBLK:
17107c478bd9Sstevel@tonic-gate 		if (protp != NULL)
17117c478bd9Sstevel@tonic-gate 			*protp = PROT_ALL;
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 		if (((u_offset_t)off + len) > (SPEC_SIZE(sp) + PAGEOFFSET))
17147c478bd9Sstevel@tonic-gate 			return (EFAULT);	/* beyond EOF */
17157c478bd9Sstevel@tonic-gate 
17167c478bd9Sstevel@tonic-gate 		if (len <= PAGESIZE)
17177c478bd9Sstevel@tonic-gate 			err = spec_getapage(vp, (u_offset_t)off, len, protp, pl,
17187c478bd9Sstevel@tonic-gate 			    plsz, seg, addr, rw, cr);
17197c478bd9Sstevel@tonic-gate 		else
17207c478bd9Sstevel@tonic-gate 			err = pvn_getpages(spec_getapage, vp, (u_offset_t)off,
17217c478bd9Sstevel@tonic-gate 			    len, protp, pl, plsz, seg, addr, rw, cr);
17227c478bd9Sstevel@tonic-gate 		break;
17237c478bd9Sstevel@tonic-gate 
17247c478bd9Sstevel@tonic-gate 	case VCHR:
17257c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "spec_getpage called for character device. "
17267c478bd9Sstevel@tonic-gate 		    "Check any non-ON consolidation drivers");
17277c478bd9Sstevel@tonic-gate 		err = 0;
17287c478bd9Sstevel@tonic-gate 		pl[0] = (page_t *)0;
17297c478bd9Sstevel@tonic-gate 		break;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	default:
17327c478bd9Sstevel@tonic-gate 		panic("spec_getpage: bad v_type 0x%x", vp->v_type);
17337c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
17347c478bd9Sstevel@tonic-gate 	}
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate 	return (err);
17377c478bd9Sstevel@tonic-gate }
17387c478bd9Sstevel@tonic-gate 
17397c478bd9Sstevel@tonic-gate extern int klustsize;	/* set in machdep.c */
17407c478bd9Sstevel@tonic-gate 
17417c478bd9Sstevel@tonic-gate int spec_ra = 1;
17427c478bd9Sstevel@tonic-gate int spec_lostpage;	/* number of times we lost original page */
17437c478bd9Sstevel@tonic-gate 
17447c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
17457c478bd9Sstevel@tonic-gate static int
17467c478bd9Sstevel@tonic-gate spec_getapage(
17477c478bd9Sstevel@tonic-gate 	struct vnode *vp,
17487c478bd9Sstevel@tonic-gate 	u_offset_t	off,
17497c478bd9Sstevel@tonic-gate 	size_t		len,
17507c478bd9Sstevel@tonic-gate 	uint_t		*protp,
17517c478bd9Sstevel@tonic-gate 	page_t		*pl[],
17527c478bd9Sstevel@tonic-gate 	size_t		plsz,
17537c478bd9Sstevel@tonic-gate 	struct seg	*seg,
17547c478bd9Sstevel@tonic-gate 	caddr_t		addr,
17557c478bd9Sstevel@tonic-gate 	enum seg_rw	rw,
17567c478bd9Sstevel@tonic-gate 	struct cred	*cr)
17577c478bd9Sstevel@tonic-gate {
17587c478bd9Sstevel@tonic-gate 	struct snode *sp;
17597c478bd9Sstevel@tonic-gate 	struct buf *bp;
17607c478bd9Sstevel@tonic-gate 	page_t *pp, *pp2;
17617c478bd9Sstevel@tonic-gate 	u_offset_t io_off1, io_off2;
17627c478bd9Sstevel@tonic-gate 	size_t io_len1;
17637c478bd9Sstevel@tonic-gate 	size_t io_len2;
17647c478bd9Sstevel@tonic-gate 	size_t blksz;
17657c478bd9Sstevel@tonic-gate 	u_offset_t blkoff;
17667c478bd9Sstevel@tonic-gate 	int dora, err;
17677c478bd9Sstevel@tonic-gate 	page_t *pagefound;
17687c478bd9Sstevel@tonic-gate 	uint_t xlen;
17697c478bd9Sstevel@tonic-gate 	size_t adj_klustsize;
17707c478bd9Sstevel@tonic-gate 	u_offset_t size;
17717c478bd9Sstevel@tonic-gate 	u_offset_t tmpoff;
17727c478bd9Sstevel@tonic-gate 
17737c478bd9Sstevel@tonic-gate 	sp = VTOS(vp);
17747c478bd9Sstevel@tonic-gate 	TRACE_3(TR_FAC_SPECFS, TR_SPECFS_GETAPAGE,
17757c478bd9Sstevel@tonic-gate 	    "specfs getapage:vp %p off %llx snode %p", vp, off, sp);
17767c478bd9Sstevel@tonic-gate reread:
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	err = 0;
17797c478bd9Sstevel@tonic-gate 	bp = NULL;
17807c478bd9Sstevel@tonic-gate 	pp = NULL;
17817c478bd9Sstevel@tonic-gate 	pp2 = NULL;
17827c478bd9Sstevel@tonic-gate 
17837c478bd9Sstevel@tonic-gate 	if (pl != NULL)
17847c478bd9Sstevel@tonic-gate 		pl[0] = NULL;
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate 	size = SPEC_SIZE(VTOS(sp->s_commonvp));
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate 	if (spec_ra && sp->s_nextr == off)
17897c478bd9Sstevel@tonic-gate 		dora = 1;
17907c478bd9Sstevel@tonic-gate 	else
17917c478bd9Sstevel@tonic-gate 		dora = 0;
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	if (size == UNKNOWN_SIZE) {
17947c478bd9Sstevel@tonic-gate 		dora = 0;
17957c478bd9Sstevel@tonic-gate 		adj_klustsize = PAGESIZE;
17967c478bd9Sstevel@tonic-gate 	} else {
17977c478bd9Sstevel@tonic-gate 		adj_klustsize = dora ? klustsize : PAGESIZE;
17987c478bd9Sstevel@tonic-gate 	}
17997c478bd9Sstevel@tonic-gate 
18007c478bd9Sstevel@tonic-gate again:
18017c478bd9Sstevel@tonic-gate 	if ((pagefound = page_exists(vp, off)) == NULL) {
18027c478bd9Sstevel@tonic-gate 		if (rw == S_CREATE) {
18037c478bd9Sstevel@tonic-gate 			/*
18047c478bd9Sstevel@tonic-gate 			 * We're allocating a swap slot and it's
18057c478bd9Sstevel@tonic-gate 			 * associated page was not found, so allocate
18067c478bd9Sstevel@tonic-gate 			 * and return it.
18077c478bd9Sstevel@tonic-gate 			 */
18087c478bd9Sstevel@tonic-gate 			if ((pp = page_create_va(vp, off,
18097c478bd9Sstevel@tonic-gate 			    PAGESIZE, PG_WAIT, seg, addr)) == NULL) {
18107c478bd9Sstevel@tonic-gate 				panic("spec_getapage: page_create");
18117c478bd9Sstevel@tonic-gate 				/*NOTREACHED*/
18127c478bd9Sstevel@tonic-gate 			}
18137c478bd9Sstevel@tonic-gate 			io_len1 = PAGESIZE;
18147c478bd9Sstevel@tonic-gate 			sp->s_nextr = off + PAGESIZE;
18157c478bd9Sstevel@tonic-gate 		} else {
18167c478bd9Sstevel@tonic-gate 			/*
18177c478bd9Sstevel@tonic-gate 			 * Need to really do disk I/O to get the page(s).
18187c478bd9Sstevel@tonic-gate 			 */
18197c478bd9Sstevel@tonic-gate 			blkoff = (off / adj_klustsize) * adj_klustsize;
18207c478bd9Sstevel@tonic-gate 			if (size == UNKNOWN_SIZE) {
18217c478bd9Sstevel@tonic-gate 				blksz = PAGESIZE;
18227c478bd9Sstevel@tonic-gate 			} else {
18237c478bd9Sstevel@tonic-gate 				if (blkoff + adj_klustsize <= size)
18247c478bd9Sstevel@tonic-gate 					blksz = adj_klustsize;
18257c478bd9Sstevel@tonic-gate 				else
18267c478bd9Sstevel@tonic-gate 					blksz =
18277c478bd9Sstevel@tonic-gate 					    MIN(size - blkoff, adj_klustsize);
18287c478bd9Sstevel@tonic-gate 			}
18297c478bd9Sstevel@tonic-gate 
18307c478bd9Sstevel@tonic-gate 			pp = pvn_read_kluster(vp, off, seg, addr, &tmpoff,
18317c478bd9Sstevel@tonic-gate 			    &io_len1, blkoff, blksz, 0);
18327c478bd9Sstevel@tonic-gate 			io_off1 = tmpoff;
18337c478bd9Sstevel@tonic-gate 			/*
18347c478bd9Sstevel@tonic-gate 			 * Make sure the page didn't sneek into the
18357c478bd9Sstevel@tonic-gate 			 * cache while we blocked in pvn_read_kluster.
18367c478bd9Sstevel@tonic-gate 			 */
18377c478bd9Sstevel@tonic-gate 			if (pp == NULL)
18387c478bd9Sstevel@tonic-gate 				goto again;
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 			/*
18417c478bd9Sstevel@tonic-gate 			 * Zero part of page which we are not
18427c478bd9Sstevel@tonic-gate 			 * going to be reading from disk now.
18437c478bd9Sstevel@tonic-gate 			 */
18447c478bd9Sstevel@tonic-gate 			xlen = (uint_t)(io_len1 & PAGEOFFSET);
18457c478bd9Sstevel@tonic-gate 			if (xlen != 0)
18467c478bd9Sstevel@tonic-gate 				pagezero(pp->p_prev, xlen, PAGESIZE - xlen);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 			bp = spec_startio(vp, pp, io_off1, io_len1,
18497c478bd9Sstevel@tonic-gate 			    pl == NULL ? (B_ASYNC | B_READ) : B_READ);
18507c478bd9Sstevel@tonic-gate 			sp->s_nextr = io_off1 + io_len1;
18517c478bd9Sstevel@tonic-gate 		}
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	if (dora && rw != S_CREATE) {
18557c478bd9Sstevel@tonic-gate 		u_offset_t off2;
18567c478bd9Sstevel@tonic-gate 		caddr_t addr2;
18577c478bd9Sstevel@tonic-gate 
18587c478bd9Sstevel@tonic-gate 		off2 = ((off / adj_klustsize) + 1) * adj_klustsize;
18597c478bd9Sstevel@tonic-gate 		addr2 = addr + (off2 - off);
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 		pp2 = NULL;
18627c478bd9Sstevel@tonic-gate 		/*
18637c478bd9Sstevel@tonic-gate 		 * If we are past EOF then don't bother trying
18647c478bd9Sstevel@tonic-gate 		 * with read-ahead.
18657c478bd9Sstevel@tonic-gate 		 */
18667c478bd9Sstevel@tonic-gate 		if (off2 >= size)
18677c478bd9Sstevel@tonic-gate 			pp2 = NULL;
18687c478bd9Sstevel@tonic-gate 		else {
18697c478bd9Sstevel@tonic-gate 			if (off2 + adj_klustsize <= size)
18707c478bd9Sstevel@tonic-gate 				blksz = adj_klustsize;
18717c478bd9Sstevel@tonic-gate 			else
18727c478bd9Sstevel@tonic-gate 				blksz = MIN(size - off2, adj_klustsize);
18737c478bd9Sstevel@tonic-gate 
18747c478bd9Sstevel@tonic-gate 			pp2 = pvn_read_kluster(vp, off2, seg, addr2, &tmpoff,
18757c478bd9Sstevel@tonic-gate 			    &io_len2, off2, blksz, 1);
18767c478bd9Sstevel@tonic-gate 			io_off2 = tmpoff;
18777c478bd9Sstevel@tonic-gate 		}
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 		if (pp2 != NULL) {
18807c478bd9Sstevel@tonic-gate 			/*
18817c478bd9Sstevel@tonic-gate 			 * Zero part of page which we are not
18827c478bd9Sstevel@tonic-gate 			 * going to be reading from disk now.
18837c478bd9Sstevel@tonic-gate 			 */
18847c478bd9Sstevel@tonic-gate 			xlen = (uint_t)(io_len2 & PAGEOFFSET);
18857c478bd9Sstevel@tonic-gate 			if (xlen != 0)
18867c478bd9Sstevel@tonic-gate 				pagezero(pp2->p_prev, xlen, PAGESIZE - xlen);
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 			(void) spec_startio(vp, pp2, io_off2, io_len2,
18897c478bd9Sstevel@tonic-gate 			    B_READ | B_ASYNC);
18907c478bd9Sstevel@tonic-gate 		}
18917c478bd9Sstevel@tonic-gate 	}
18927c478bd9Sstevel@tonic-gate 
18937c478bd9Sstevel@tonic-gate 	if (pl == NULL)
18947c478bd9Sstevel@tonic-gate 		return (err);
18957c478bd9Sstevel@tonic-gate 
18967c478bd9Sstevel@tonic-gate 	if (bp != NULL) {
18977c478bd9Sstevel@tonic-gate 		err = biowait(bp);
18987c478bd9Sstevel@tonic-gate 		pageio_done(bp);
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 		if (err) {
19017c478bd9Sstevel@tonic-gate 			if (pp != NULL)
19027c478bd9Sstevel@tonic-gate 				pvn_read_done(pp, B_ERROR);
19037c478bd9Sstevel@tonic-gate 			return (err);
19047c478bd9Sstevel@tonic-gate 		}
19057c478bd9Sstevel@tonic-gate 	}
19067c478bd9Sstevel@tonic-gate 
19077c478bd9Sstevel@tonic-gate 	if (pagefound) {
19087c478bd9Sstevel@tonic-gate 		se_t se = (rw == S_CREATE ? SE_EXCL : SE_SHARED);
19097c478bd9Sstevel@tonic-gate 		/*
19107c478bd9Sstevel@tonic-gate 		 * Page exists in the cache, acquire the appropriate
19117c478bd9Sstevel@tonic-gate 		 * lock.  If this fails, start all over again.
19127c478bd9Sstevel@tonic-gate 		 */
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate 		if ((pp = page_lookup(vp, off, se)) == NULL) {
19157c478bd9Sstevel@tonic-gate 			spec_lostpage++;
19167c478bd9Sstevel@tonic-gate 			goto reread;
19177c478bd9Sstevel@tonic-gate 		}
19187c478bd9Sstevel@tonic-gate 		pl[0] = pp;
19197c478bd9Sstevel@tonic-gate 		pl[1] = NULL;
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 		sp->s_nextr = off + PAGESIZE;
19227c478bd9Sstevel@tonic-gate 		return (0);
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 
19257c478bd9Sstevel@tonic-gate 	if (pp != NULL)
19267c478bd9Sstevel@tonic-gate 		pvn_plist_init(pp, pl, plsz, off, io_len1, rw);
19277c478bd9Sstevel@tonic-gate 	return (0);
19287c478bd9Sstevel@tonic-gate }
19297c478bd9Sstevel@tonic-gate 
19307c478bd9Sstevel@tonic-gate /*
19317c478bd9Sstevel@tonic-gate  * Flags are composed of {B_INVAL, B_DIRTY B_FREE, B_DONTNEED, B_FORCE}.
19327c478bd9Sstevel@tonic-gate  * If len == 0, do from off to EOF.
19337c478bd9Sstevel@tonic-gate  *
19347c478bd9Sstevel@tonic-gate  * The normal cases should be len == 0 & off == 0 (entire vp list),
19357c478bd9Sstevel@tonic-gate  * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
19367c478bd9Sstevel@tonic-gate  * (from pageout).
19377c478bd9Sstevel@tonic-gate  */
19387c478bd9Sstevel@tonic-gate int
19397c478bd9Sstevel@tonic-gate spec_putpage(
19407c478bd9Sstevel@tonic-gate 	struct vnode *vp,
19417c478bd9Sstevel@tonic-gate 	offset_t	off,
19427c478bd9Sstevel@tonic-gate 	size_t		len,
19437c478bd9Sstevel@tonic-gate 	int		flags,
19447c478bd9Sstevel@tonic-gate 	struct cred	*cr)
19457c478bd9Sstevel@tonic-gate {
19467c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
19477c478bd9Sstevel@tonic-gate 	struct vnode *cvp;
19487c478bd9Sstevel@tonic-gate 	page_t *pp;
19497c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
19507c478bd9Sstevel@tonic-gate 	size_t io_len = 0;	/* for lint */
19517c478bd9Sstevel@tonic-gate 	int err = 0;
19527c478bd9Sstevel@tonic-gate 	u_offset_t size;
19537c478bd9Sstevel@tonic-gate 	u_offset_t tmpoff;
19547c478bd9Sstevel@tonic-gate 
19557c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_count != 0);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
19587c478bd9Sstevel@tonic-gate 		return (ENOSYS);
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	cvp = sp->s_commonvp;
19617c478bd9Sstevel@tonic-gate 	size = SPEC_SIZE(VTOS(cvp));
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate 	if (!vn_has_cached_data(vp) || off >= size)
19647c478bd9Sstevel@tonic-gate 		return (0);
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VBLK && cvp == vp);
19677c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_SPECFS, TR_SPECFS_PUTPAGE,
19687c478bd9Sstevel@tonic-gate 	    "specfs putpage:vp %p off %llx len %ld snode %p",
19697c478bd9Sstevel@tonic-gate 	    vp, off, len, sp);
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	if (len == 0) {
19727c478bd9Sstevel@tonic-gate 		/*
19737c478bd9Sstevel@tonic-gate 		 * Search the entire vp list for pages >= off.
19747c478bd9Sstevel@tonic-gate 		 */
19757c478bd9Sstevel@tonic-gate 		err = pvn_vplist_dirty(vp, off, spec_putapage,
19767c478bd9Sstevel@tonic-gate 		    flags, cr);
19777c478bd9Sstevel@tonic-gate 	} else {
19787c478bd9Sstevel@tonic-gate 		u_offset_t eoff;
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate 		/*
19817c478bd9Sstevel@tonic-gate 		 * Loop over all offsets in the range [off...off + len]
19827c478bd9Sstevel@tonic-gate 		 * looking for pages to deal with.  We set limits so
19837c478bd9Sstevel@tonic-gate 		 * that we kluster to klustsize boundaries.
19847c478bd9Sstevel@tonic-gate 		 */
19857c478bd9Sstevel@tonic-gate 		eoff = off + len;
19867c478bd9Sstevel@tonic-gate 		for (io_off = off; io_off < eoff && io_off < size;
19877c478bd9Sstevel@tonic-gate 		    io_off += io_len) {
19887c478bd9Sstevel@tonic-gate 			/*
19897c478bd9Sstevel@tonic-gate 			 * If we are not invalidating, synchronously
19907c478bd9Sstevel@tonic-gate 			 * freeing or writing pages use the routine
19917c478bd9Sstevel@tonic-gate 			 * page_lookup_nowait() to prevent reclaiming
19927c478bd9Sstevel@tonic-gate 			 * them from the free list.
19937c478bd9Sstevel@tonic-gate 			 */
19947c478bd9Sstevel@tonic-gate 			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
19957c478bd9Sstevel@tonic-gate 				pp = page_lookup(vp, io_off,
19967c478bd9Sstevel@tonic-gate 				    (flags & (B_INVAL | B_FREE)) ?
19977c478bd9Sstevel@tonic-gate 				    SE_EXCL : SE_SHARED);
19987c478bd9Sstevel@tonic-gate 			} else {
19997c478bd9Sstevel@tonic-gate 				pp = page_lookup_nowait(vp, io_off,
20007c478bd9Sstevel@tonic-gate 				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
20017c478bd9Sstevel@tonic-gate 			}
20027c478bd9Sstevel@tonic-gate 
20037c478bd9Sstevel@tonic-gate 			if (pp == NULL || pvn_getdirty(pp, flags) == 0)
20047c478bd9Sstevel@tonic-gate 				io_len = PAGESIZE;
20057c478bd9Sstevel@tonic-gate 			else {
20067c478bd9Sstevel@tonic-gate 				err = spec_putapage(vp, pp, &tmpoff, &io_len,
20077c478bd9Sstevel@tonic-gate 				    flags, cr);
20087c478bd9Sstevel@tonic-gate 				io_off = tmpoff;
20097c478bd9Sstevel@tonic-gate 				if (err != 0)
20107c478bd9Sstevel@tonic-gate 					break;
20117c478bd9Sstevel@tonic-gate 				/*
20127c478bd9Sstevel@tonic-gate 				 * "io_off" and "io_len" are returned as
20137c478bd9Sstevel@tonic-gate 				 * the range of pages we actually wrote.
20147c478bd9Sstevel@tonic-gate 				 * This allows us to skip ahead more quickly
20157c478bd9Sstevel@tonic-gate 				 * since several pages may've been dealt
20167c478bd9Sstevel@tonic-gate 				 * with by this iteration of the loop.
20177c478bd9Sstevel@tonic-gate 				 */
20187c478bd9Sstevel@tonic-gate 			}
20197c478bd9Sstevel@tonic-gate 		}
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 	return (err);
20227c478bd9Sstevel@tonic-gate }
20237c478bd9Sstevel@tonic-gate 
20247c478bd9Sstevel@tonic-gate 
20257c478bd9Sstevel@tonic-gate /*
20267c478bd9Sstevel@tonic-gate  * Write out a single page, possibly klustering adjacent
20277c478bd9Sstevel@tonic-gate  * dirty pages.
20287c478bd9Sstevel@tonic-gate  */
20297c478bd9Sstevel@tonic-gate /*ARGSUSED5*/
20307c478bd9Sstevel@tonic-gate static int
20317c478bd9Sstevel@tonic-gate spec_putapage(
20327c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
20337c478bd9Sstevel@tonic-gate 	page_t		*pp,
20347c478bd9Sstevel@tonic-gate 	u_offset_t	*offp,		/* return value */
20357c478bd9Sstevel@tonic-gate 	size_t		*lenp,		/* return value */
20367c478bd9Sstevel@tonic-gate 	int		flags,
20377c478bd9Sstevel@tonic-gate 	struct cred	*cr)
20387c478bd9Sstevel@tonic-gate {
20397c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
20407c478bd9Sstevel@tonic-gate 	u_offset_t io_off;
20417c478bd9Sstevel@tonic-gate 	size_t io_len;
20427c478bd9Sstevel@tonic-gate 	size_t blksz;
20437c478bd9Sstevel@tonic-gate 	u_offset_t blkoff;
20447c478bd9Sstevel@tonic-gate 	int err = 0;
20457c478bd9Sstevel@tonic-gate 	struct buf *bp;
20467c478bd9Sstevel@tonic-gate 	u_offset_t size;
20477c478bd9Sstevel@tonic-gate 	size_t adj_klustsize;
20487c478bd9Sstevel@tonic-gate 	u_offset_t tmpoff;
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate 	/*
20517c478bd9Sstevel@tonic-gate 	 * Destroy read ahead value since we are really going to write.
20527c478bd9Sstevel@tonic-gate 	 */
20537c478bd9Sstevel@tonic-gate 	sp->s_nextr = 0;
20547c478bd9Sstevel@tonic-gate 	size = SPEC_SIZE(VTOS(sp->s_commonvp));
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate 	adj_klustsize = klustsize;
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate 	blkoff = (pp->p_offset / adj_klustsize) * adj_klustsize;
20597c478bd9Sstevel@tonic-gate 
20607c478bd9Sstevel@tonic-gate 	if (blkoff + adj_klustsize <= size)
20617c478bd9Sstevel@tonic-gate 		blksz = adj_klustsize;
20627c478bd9Sstevel@tonic-gate 	else
20637c478bd9Sstevel@tonic-gate 		blksz = size - blkoff;
20647c478bd9Sstevel@tonic-gate 
20657c478bd9Sstevel@tonic-gate 	/*
20667c478bd9Sstevel@tonic-gate 	 * Find a kluster that fits in one contiguous chunk.
20677c478bd9Sstevel@tonic-gate 	 */
20687c478bd9Sstevel@tonic-gate 	pp = pvn_write_kluster(vp, pp, &tmpoff, &io_len, blkoff,
20697c478bd9Sstevel@tonic-gate 	    blksz, flags);
20707c478bd9Sstevel@tonic-gate 	io_off = tmpoff;
20717c478bd9Sstevel@tonic-gate 
20727c478bd9Sstevel@tonic-gate 	/*
20737c478bd9Sstevel@tonic-gate 	 * Check for page length rounding problems
20747c478bd9Sstevel@tonic-gate 	 * XXX - Is this necessary?
20757c478bd9Sstevel@tonic-gate 	 */
20767c478bd9Sstevel@tonic-gate 	if (io_off + io_len > size) {
20777c478bd9Sstevel@tonic-gate 		ASSERT((io_off + io_len) - size < PAGESIZE);
20787c478bd9Sstevel@tonic-gate 		io_len = size - io_off;
20797c478bd9Sstevel@tonic-gate 	}
20807c478bd9Sstevel@tonic-gate 
20817c478bd9Sstevel@tonic-gate 	bp = spec_startio(vp, pp, io_off, io_len, B_WRITE | flags);
20827c478bd9Sstevel@tonic-gate 
20837c478bd9Sstevel@tonic-gate 	/*
20847c478bd9Sstevel@tonic-gate 	 * Wait for i/o to complete if the request is not B_ASYNC.
20857c478bd9Sstevel@tonic-gate 	 */
20867c478bd9Sstevel@tonic-gate 	if ((flags & B_ASYNC) == 0) {
20877c478bd9Sstevel@tonic-gate 		err = biowait(bp);
20887c478bd9Sstevel@tonic-gate 		pageio_done(bp);
20897c478bd9Sstevel@tonic-gate 		pvn_write_done(pp, ((err) ? B_ERROR : 0) | B_WRITE | flags);
20907c478bd9Sstevel@tonic-gate 	}
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate 	if (offp)
20937c478bd9Sstevel@tonic-gate 		*offp = io_off;
20947c478bd9Sstevel@tonic-gate 	if (lenp)
20957c478bd9Sstevel@tonic-gate 		*lenp = io_len;
20967c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_SPECFS, TR_SPECFS_PUTAPAGE,
20977c478bd9Sstevel@tonic-gate 	    "specfs putapage:vp %p offp %p snode %p err %d",
20987c478bd9Sstevel@tonic-gate 	    vp, offp, sp, err);
20997c478bd9Sstevel@tonic-gate 	return (err);
21007c478bd9Sstevel@tonic-gate }
21017c478bd9Sstevel@tonic-gate 
21027c478bd9Sstevel@tonic-gate /*
21037c478bd9Sstevel@tonic-gate  * Flags are composed of {B_ASYNC, B_INVAL, B_FREE, B_DONTNEED}
21047c478bd9Sstevel@tonic-gate  */
21057c478bd9Sstevel@tonic-gate static struct buf *
21067c478bd9Sstevel@tonic-gate spec_startio(
21077c478bd9Sstevel@tonic-gate 	struct vnode *vp,
21087c478bd9Sstevel@tonic-gate 	page_t		*pp,
21097c478bd9Sstevel@tonic-gate 	u_offset_t	io_off,
21107c478bd9Sstevel@tonic-gate 	size_t		io_len,
21117c478bd9Sstevel@tonic-gate 	int		flags)
21127c478bd9Sstevel@tonic-gate {
21137c478bd9Sstevel@tonic-gate 	struct buf *bp;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	bp = pageio_setup(pp, io_len, vp, flags);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	bp->b_edev = vp->v_rdev;
21187c478bd9Sstevel@tonic-gate 	bp->b_dev = cmpdev(vp->v_rdev);
21197c478bd9Sstevel@tonic-gate 	bp->b_blkno = btodt(io_off);
21207c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = (caddr_t)0;
21217c478bd9Sstevel@tonic-gate 
21227c478bd9Sstevel@tonic-gate 	(void) bdev_strategy(bp);
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	if (flags & B_READ)
21257c478bd9Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_INBLK, 1);
21267c478bd9Sstevel@tonic-gate 	else
21277c478bd9Sstevel@tonic-gate 		lwp_stat_update(LWP_STAT_OUBLK, 1);
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate 	return (bp);
21307c478bd9Sstevel@tonic-gate }
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate static int
21337c478bd9Sstevel@tonic-gate spec_poll(
21347c478bd9Sstevel@tonic-gate 	struct vnode	*vp,
21357c478bd9Sstevel@tonic-gate 	short		events,
21367c478bd9Sstevel@tonic-gate 	int		anyyet,
21377c478bd9Sstevel@tonic-gate 	short		*reventsp,
21387c478bd9Sstevel@tonic-gate 	struct pollhead **phpp)
21397c478bd9Sstevel@tonic-gate {
21407c478bd9Sstevel@tonic-gate 	dev_t dev;
21417c478bd9Sstevel@tonic-gate 	int error;
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	if (vp->v_type == VBLK)
21447c478bd9Sstevel@tonic-gate 		error = fs_poll(vp, events, anyyet, reventsp, phpp);
21457c478bd9Sstevel@tonic-gate 	else {
21467c478bd9Sstevel@tonic-gate 		ASSERT(vp->v_type == VCHR);
21477c478bd9Sstevel@tonic-gate 		dev = vp->v_rdev;
21487c478bd9Sstevel@tonic-gate 		if (STREAMSTAB(getmajor(dev))) {
21497c478bd9Sstevel@tonic-gate 			ASSERT(vp->v_stream != NULL);
21507c478bd9Sstevel@tonic-gate 			error = strpoll(vp->v_stream, events, anyyet,
21517c478bd9Sstevel@tonic-gate 			    reventsp, phpp);
21527c478bd9Sstevel@tonic-gate 		} else if (devopsp[getmajor(dev)]->devo_cb_ops->cb_chpoll) {
21537c478bd9Sstevel@tonic-gate 			error = cdev_poll(dev, events, anyyet, reventsp, phpp);
21547c478bd9Sstevel@tonic-gate 		} else {
21557c478bd9Sstevel@tonic-gate 			error = fs_poll(vp, events, anyyet, reventsp, phpp);
21567c478bd9Sstevel@tonic-gate 		}
21577c478bd9Sstevel@tonic-gate 	}
21587c478bd9Sstevel@tonic-gate 	return (error);
21597c478bd9Sstevel@tonic-gate }
21607c478bd9Sstevel@tonic-gate 
21617c478bd9Sstevel@tonic-gate /*
21627c478bd9Sstevel@tonic-gate  * This routine is called through the cdevsw[] table to handle
21637c478bd9Sstevel@tonic-gate  * traditional mmap'able devices that support a d_mmap function.
21647c478bd9Sstevel@tonic-gate  */
21657c478bd9Sstevel@tonic-gate /*ARGSUSED8*/
21667c478bd9Sstevel@tonic-gate int
21677c478bd9Sstevel@tonic-gate spec_segmap(
21687c478bd9Sstevel@tonic-gate 	dev_t dev,
21697c478bd9Sstevel@tonic-gate 	off_t off,
21707c478bd9Sstevel@tonic-gate 	struct as *as,
21717c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
21727c478bd9Sstevel@tonic-gate 	off_t len,
21737c478bd9Sstevel@tonic-gate 	uint_t prot,
21747c478bd9Sstevel@tonic-gate 	uint_t maxprot,
21757c478bd9Sstevel@tonic-gate 	uint_t flags,
21767c478bd9Sstevel@tonic-gate 	struct cred *cred)
21777c478bd9Sstevel@tonic-gate {
21787c478bd9Sstevel@tonic-gate 	struct segdev_crargs dev_a;
21797c478bd9Sstevel@tonic-gate 	int (*mapfunc)(dev_t dev, off_t off, int prot);
21807c478bd9Sstevel@tonic-gate 	size_t i;
21817c478bd9Sstevel@tonic-gate 	int	error;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	if ((mapfunc = devopsp[getmajor(dev)]->devo_cb_ops->cb_mmap) == nodev)
21847c478bd9Sstevel@tonic-gate 		return (ENODEV);
21857c478bd9Sstevel@tonic-gate 	TRACE_4(TR_FAC_SPECFS, TR_SPECFS_SEGMAP,
21867c478bd9Sstevel@tonic-gate 	    "specfs segmap:dev %x as %p len %lx prot %x",
21877c478bd9Sstevel@tonic-gate 	    dev, as, len, prot);
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate 	/*
21907c478bd9Sstevel@tonic-gate 	 * Character devices that support the d_mmap
21917c478bd9Sstevel@tonic-gate 	 * interface can only be mmap'ed shared.
21927c478bd9Sstevel@tonic-gate 	 */
21937c478bd9Sstevel@tonic-gate 	if ((flags & MAP_TYPE) != MAP_SHARED)
21947c478bd9Sstevel@tonic-gate 		return (EINVAL);
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	/*
21977c478bd9Sstevel@tonic-gate 	 * Check to ensure that the entire range is
21987c478bd9Sstevel@tonic-gate 	 * legal and we are not trying to map in
21997c478bd9Sstevel@tonic-gate 	 * more than the device will let us.
22007c478bd9Sstevel@tonic-gate 	 */
22017c478bd9Sstevel@tonic-gate 	for (i = 0; i < len; i += PAGESIZE) {
22027c478bd9Sstevel@tonic-gate 		if (cdev_mmap(mapfunc, dev, off + i, maxprot) == -1)
22037c478bd9Sstevel@tonic-gate 			return (ENXIO);
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	as_rangelock(as);
22077c478bd9Sstevel@tonic-gate 	if ((flags & MAP_FIXED) == 0) {
22087c478bd9Sstevel@tonic-gate 		/*
22097c478bd9Sstevel@tonic-gate 		 * Pick an address w/o worrying about
22107c478bd9Sstevel@tonic-gate 		 * any vac alignment constraints.
22117c478bd9Sstevel@tonic-gate 		 */
22127c478bd9Sstevel@tonic-gate 		map_addr(addrp, len, (offset_t)off, 0, flags);
22137c478bd9Sstevel@tonic-gate 		if (*addrp == NULL) {
22147c478bd9Sstevel@tonic-gate 			as_rangeunlock(as);
22157c478bd9Sstevel@tonic-gate 			return (ENOMEM);
22167c478bd9Sstevel@tonic-gate 		}
22177c478bd9Sstevel@tonic-gate 	} else {
22187c478bd9Sstevel@tonic-gate 		/*
22197c478bd9Sstevel@tonic-gate 		 * User-specified address; blow away any previous mappings.
22207c478bd9Sstevel@tonic-gate 		 */
22217c478bd9Sstevel@tonic-gate 		(void) as_unmap(as, *addrp, len);
22227c478bd9Sstevel@tonic-gate 	}
22237c478bd9Sstevel@tonic-gate 
22247c478bd9Sstevel@tonic-gate 	dev_a.mapfunc = mapfunc;
22257c478bd9Sstevel@tonic-gate 	dev_a.dev = dev;
22267c478bd9Sstevel@tonic-gate 	dev_a.offset = off;
22277c478bd9Sstevel@tonic-gate 	dev_a.prot = (uchar_t)prot;
22287c478bd9Sstevel@tonic-gate 	dev_a.maxprot = (uchar_t)maxprot;
22297c478bd9Sstevel@tonic-gate 	dev_a.hat_flags = 0;
22307c478bd9Sstevel@tonic-gate 	dev_a.hat_attr = 0;
22317c478bd9Sstevel@tonic-gate 	dev_a.devmap_data = NULL;
22327c478bd9Sstevel@tonic-gate 
22337c478bd9Sstevel@tonic-gate 	error = as_map(as, *addrp, len, segdev_create, &dev_a);
22347c478bd9Sstevel@tonic-gate 	as_rangeunlock(as);
22357c478bd9Sstevel@tonic-gate 	return (error);
22367c478bd9Sstevel@tonic-gate }
22377c478bd9Sstevel@tonic-gate 
22387c478bd9Sstevel@tonic-gate int
22397c478bd9Sstevel@tonic-gate spec_char_map(
22407c478bd9Sstevel@tonic-gate 	dev_t dev,
22417c478bd9Sstevel@tonic-gate 	offset_t off,
22427c478bd9Sstevel@tonic-gate 	struct as *as,
22437c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
22447c478bd9Sstevel@tonic-gate 	size_t len,
22457c478bd9Sstevel@tonic-gate 	uchar_t prot,
22467c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
22477c478bd9Sstevel@tonic-gate 	uint_t flags,
22487c478bd9Sstevel@tonic-gate 	struct cred *cred)
22497c478bd9Sstevel@tonic-gate {
22507c478bd9Sstevel@tonic-gate 	int error = 0;
22517c478bd9Sstevel@tonic-gate 	major_t maj = getmajor(dev);
22527c478bd9Sstevel@tonic-gate 	int map_flag;
22537c478bd9Sstevel@tonic-gate 	int (*segmap)(dev_t, off_t, struct as *,
22547c478bd9Sstevel@tonic-gate 	    caddr_t *, off_t, uint_t, uint_t, uint_t, cred_t *);
22557c478bd9Sstevel@tonic-gate 	int (*devmap)(dev_t, devmap_cookie_t, offset_t,
22567c478bd9Sstevel@tonic-gate 	    size_t, size_t *, uint_t);
22577c478bd9Sstevel@tonic-gate 	int (*mmap)(dev_t dev, off_t off, int prot);
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	/*
22607c478bd9Sstevel@tonic-gate 	 * Character device: let the device driver
22617c478bd9Sstevel@tonic-gate 	 * pick the appropriate segment driver.
22627c478bd9Sstevel@tonic-gate 	 *
22637c478bd9Sstevel@tonic-gate 	 * 4.x compat.: allow 'NULL' cb_segmap => spec_segmap
22647c478bd9Sstevel@tonic-gate 	 * Kindness: allow 'nulldev' cb_segmap => spec_segmap
22657c478bd9Sstevel@tonic-gate 	 */
22667c478bd9Sstevel@tonic-gate 	segmap = devopsp[maj]->devo_cb_ops->cb_segmap;
22677c478bd9Sstevel@tonic-gate 	if (segmap == NULL || segmap == nulldev || segmap == nodev) {
22687c478bd9Sstevel@tonic-gate 		mmap = devopsp[maj]->devo_cb_ops->cb_mmap;
22697c478bd9Sstevel@tonic-gate 		map_flag = devopsp[maj]->devo_cb_ops->cb_flag;
22707c478bd9Sstevel@tonic-gate 
22717c478bd9Sstevel@tonic-gate 		/*
22727c478bd9Sstevel@tonic-gate 		 * Use old mmap framework if the driver has both mmap
22737c478bd9Sstevel@tonic-gate 		 * and devmap entry points.  This is to prevent the
22747c478bd9Sstevel@tonic-gate 		 * system from calling invalid devmap entry point
22757c478bd9Sstevel@tonic-gate 		 * for some drivers that might have put garbage in the
22767c478bd9Sstevel@tonic-gate 		 * devmap entry point.
22777c478bd9Sstevel@tonic-gate 		 */
22787c478bd9Sstevel@tonic-gate 		if ((map_flag & D_DEVMAP) || mmap == NULL ||
22797c478bd9Sstevel@tonic-gate 		    mmap == nulldev || mmap == nodev) {
22807c478bd9Sstevel@tonic-gate 			devmap = devopsp[maj]->devo_cb_ops->cb_devmap;
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate 			/*
22837c478bd9Sstevel@tonic-gate 			 * If driver provides devmap entry point in
22847c478bd9Sstevel@tonic-gate 			 * cb_ops but not xx_segmap(9E), call
22857c478bd9Sstevel@tonic-gate 			 * devmap_setup with default settings
22867c478bd9Sstevel@tonic-gate 			 * (NULL) for callback_ops and driver
22877c478bd9Sstevel@tonic-gate 			 * callback private data
22887c478bd9Sstevel@tonic-gate 			 */
22897c478bd9Sstevel@tonic-gate 			if (devmap == nodev || devmap == NULL ||
22907c478bd9Sstevel@tonic-gate 			    devmap == nulldev)
22917c478bd9Sstevel@tonic-gate 				return (ENODEV);
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 			error = devmap_setup(dev, off, as, addrp,
22947c478bd9Sstevel@tonic-gate 			    len, prot, maxprot, flags, cred);
22957c478bd9Sstevel@tonic-gate 
22967c478bd9Sstevel@tonic-gate 			return (error);
22977c478bd9Sstevel@tonic-gate 		} else
22987c478bd9Sstevel@tonic-gate 			segmap = spec_segmap;
22997c478bd9Sstevel@tonic-gate 	} else
23007c478bd9Sstevel@tonic-gate 		segmap = cdev_segmap;
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 	return ((*segmap)(dev, (off_t)off, as, addrp, len, prot,
23037c478bd9Sstevel@tonic-gate 	    maxprot, flags, cred));
23047c478bd9Sstevel@tonic-gate }
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate static int
23077c478bd9Sstevel@tonic-gate spec_map(
23087c478bd9Sstevel@tonic-gate 	struct vnode *vp,
23097c478bd9Sstevel@tonic-gate 	offset_t off,
23107c478bd9Sstevel@tonic-gate 	struct as *as,
23117c478bd9Sstevel@tonic-gate 	caddr_t *addrp,
23127c478bd9Sstevel@tonic-gate 	size_t len,
23137c478bd9Sstevel@tonic-gate 	uchar_t prot,
23147c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
23157c478bd9Sstevel@tonic-gate 	uint_t flags,
23167c478bd9Sstevel@tonic-gate 	struct cred *cred)
23177c478bd9Sstevel@tonic-gate {
23187c478bd9Sstevel@tonic-gate 	int error = 0;
231925e8c5aaSvikram 	struct snode *sp = VTOS(vp);
23207c478bd9Sstevel@tonic-gate 
23217c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
23227c478bd9Sstevel@tonic-gate 		return (ENOSYS);
23237c478bd9Sstevel@tonic-gate 
232425e8c5aaSvikram 	/* fail map with ENXIO if the device is fenced off */
232525e8c5aaSvikram 	if (S_ISFENCED(sp))
232625e8c5aaSvikram 		return (ENXIO);
232725e8c5aaSvikram 
23287c478bd9Sstevel@tonic-gate 	/*
23297c478bd9Sstevel@tonic-gate 	 * If file is locked, fail mapping attempt.
23307c478bd9Sstevel@tonic-gate 	 */
23317c478bd9Sstevel@tonic-gate 	if (vn_has_flocks(vp))
23327c478bd9Sstevel@tonic-gate 		return (EAGAIN);
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	if (vp->v_type == VCHR) {
23357c478bd9Sstevel@tonic-gate 		return (spec_char_map(vp->v_rdev, off, as, addrp, len, prot,
23367c478bd9Sstevel@tonic-gate 		    maxprot, flags, cred));
23377c478bd9Sstevel@tonic-gate 	} else if (vp->v_type == VBLK) {
23387c478bd9Sstevel@tonic-gate 		struct segvn_crargs vn_a;
23397c478bd9Sstevel@tonic-gate 		struct vnode *cvp;
23407c478bd9Sstevel@tonic-gate 		struct snode *sp;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 		/*
23437c478bd9Sstevel@tonic-gate 		 * Block device, use segvn mapping to the underlying commonvp
23447c478bd9Sstevel@tonic-gate 		 * for pages.
23457c478bd9Sstevel@tonic-gate 		 */
23467c478bd9Sstevel@tonic-gate 		if (off > spec_maxoffset(vp))
23470dee76a0Speterte 			return (ENXIO);
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 		sp = VTOS(vp);
23507c478bd9Sstevel@tonic-gate 		cvp = sp->s_commonvp;
23517c478bd9Sstevel@tonic-gate 		ASSERT(cvp != NULL);
23527c478bd9Sstevel@tonic-gate 
2353ae115bc7Smrj 		if (off < 0 || ((offset_t)(off + len) < 0))
23540dee76a0Speterte 			return (ENXIO);
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate 		as_rangelock(as);
23577c478bd9Sstevel@tonic-gate 		if ((flags & MAP_FIXED) == 0) {
23587c478bd9Sstevel@tonic-gate 			map_addr(addrp, len, off, 1, flags);
23597c478bd9Sstevel@tonic-gate 			if (*addrp == NULL) {
23607c478bd9Sstevel@tonic-gate 				as_rangeunlock(as);
23617c478bd9Sstevel@tonic-gate 				return (ENOMEM);
23627c478bd9Sstevel@tonic-gate 			}
23637c478bd9Sstevel@tonic-gate 		} else {
23647c478bd9Sstevel@tonic-gate 			/*
23657c478bd9Sstevel@tonic-gate 			 * User-specified address; blow away any
23667c478bd9Sstevel@tonic-gate 			 * previous mappings.
23677c478bd9Sstevel@tonic-gate 			 */
23687c478bd9Sstevel@tonic-gate 			(void) as_unmap(as, *addrp, len);
23697c478bd9Sstevel@tonic-gate 		}
23707c478bd9Sstevel@tonic-gate 
23717c478bd9Sstevel@tonic-gate 		vn_a.vp = cvp;
23727c478bd9Sstevel@tonic-gate 		vn_a.offset = off;
23737c478bd9Sstevel@tonic-gate 		vn_a.type = flags & MAP_TYPE;
23747c478bd9Sstevel@tonic-gate 		vn_a.prot = (uchar_t)prot;
23757c478bd9Sstevel@tonic-gate 		vn_a.maxprot = (uchar_t)maxprot;
23767c478bd9Sstevel@tonic-gate 		vn_a.flags = flags & ~MAP_TYPE;
23777c478bd9Sstevel@tonic-gate 		vn_a.cred = cred;
23787c478bd9Sstevel@tonic-gate 		vn_a.amp = NULL;
23797c478bd9Sstevel@tonic-gate 		vn_a.szc = 0;
23807c478bd9Sstevel@tonic-gate 		vn_a.lgrp_mem_policy_flags = 0;
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 		error = as_map(as, *addrp, len, segvn_create, &vn_a);
23837c478bd9Sstevel@tonic-gate 		as_rangeunlock(as);
23847c478bd9Sstevel@tonic-gate 	} else
23857c478bd9Sstevel@tonic-gate 		return (ENODEV);
23867c478bd9Sstevel@tonic-gate 
23877c478bd9Sstevel@tonic-gate 	return (error);
23887c478bd9Sstevel@tonic-gate }
23897c478bd9Sstevel@tonic-gate 
23907c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
23917c478bd9Sstevel@tonic-gate static int
23927c478bd9Sstevel@tonic-gate spec_addmap(
23937c478bd9Sstevel@tonic-gate 	struct vnode *vp,	/* the common vnode */
23947c478bd9Sstevel@tonic-gate 	offset_t off,
23957c478bd9Sstevel@tonic-gate 	struct as *as,
23967c478bd9Sstevel@tonic-gate 	caddr_t addr,
23977c478bd9Sstevel@tonic-gate 	size_t len,		/* how many bytes to add */
23987c478bd9Sstevel@tonic-gate 	uchar_t prot,
23997c478bd9Sstevel@tonic-gate 	uchar_t maxprot,
24007c478bd9Sstevel@tonic-gate 	uint_t flags,
24017c478bd9Sstevel@tonic-gate 	struct cred *cred)
24027c478bd9Sstevel@tonic-gate {
24037c478bd9Sstevel@tonic-gate 	int error = 0;
24047c478bd9Sstevel@tonic-gate 	struct snode *csp = VTOS(vp);
24057c478bd9Sstevel@tonic-gate 	ulong_t npages;
24067c478bd9Sstevel@tonic-gate 
24077c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL && VTOS(vp)->s_commonvp == vp);
24087c478bd9Sstevel@tonic-gate 
24097c478bd9Sstevel@tonic-gate 	/*
24107c478bd9Sstevel@tonic-gate 	 * XXX	Given the above assertion, this might not
24117c478bd9Sstevel@tonic-gate 	 *	be a particularly sensible thing to test.
24127c478bd9Sstevel@tonic-gate 	 */
24137c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
24147c478bd9Sstevel@tonic-gate 		return (ENOSYS);
24157c478bd9Sstevel@tonic-gate 
241625e8c5aaSvikram 	/* fail with EIO if the device is fenced off */
241725e8c5aaSvikram 	if (S_ISFENCED(csp))
241825e8c5aaSvikram 		return (EIO);
241925e8c5aaSvikram 
24207c478bd9Sstevel@tonic-gate 	npages = btopr(len);
24217c478bd9Sstevel@tonic-gate 	LOCK_CSP(csp);
24227c478bd9Sstevel@tonic-gate 	csp->s_mapcnt += npages;
24237c478bd9Sstevel@tonic-gate 
24247c478bd9Sstevel@tonic-gate 	UNLOCK_CSP(csp);
24257c478bd9Sstevel@tonic-gate 	return (error);
24267c478bd9Sstevel@tonic-gate }
24277c478bd9Sstevel@tonic-gate 
24287c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
24297c478bd9Sstevel@tonic-gate static int
24307c478bd9Sstevel@tonic-gate spec_delmap(
24317c478bd9Sstevel@tonic-gate 	struct vnode *vp,	/* the common vnode */
24327c478bd9Sstevel@tonic-gate 	offset_t off,
24337c478bd9Sstevel@tonic-gate 	struct as *as,
24347c478bd9Sstevel@tonic-gate 	caddr_t addr,
24357c478bd9Sstevel@tonic-gate 	size_t len,		/* how many bytes to take away */
24367c478bd9Sstevel@tonic-gate 	uint_t prot,
24377c478bd9Sstevel@tonic-gate 	uint_t maxprot,
24387c478bd9Sstevel@tonic-gate 	uint_t flags,
24397c478bd9Sstevel@tonic-gate 	struct cred *cred)
24407c478bd9Sstevel@tonic-gate {
24417c478bd9Sstevel@tonic-gate 	struct snode *csp = VTOS(vp);
24427c478bd9Sstevel@tonic-gate 	ulong_t npages;
24437c478bd9Sstevel@tonic-gate 	long mcnt;
24447c478bd9Sstevel@tonic-gate 
24457c478bd9Sstevel@tonic-gate 	/* segdev passes us the common vp */
24467c478bd9Sstevel@tonic-gate 
24477c478bd9Sstevel@tonic-gate 	ASSERT(vp != NULL && VTOS(vp)->s_commonvp == vp);
24487c478bd9Sstevel@tonic-gate 
244925e8c5aaSvikram 	/* allow delmap to succeed even if device fenced off */
245025e8c5aaSvikram 
24517c478bd9Sstevel@tonic-gate 	/*
24527c478bd9Sstevel@tonic-gate 	 * XXX	Given the above assertion, this might not
24537c478bd9Sstevel@tonic-gate 	 *	be a particularly sensible thing to test..
24547c478bd9Sstevel@tonic-gate 	 */
24557c478bd9Sstevel@tonic-gate 	if (vp->v_flag & VNOMAP)
24567c478bd9Sstevel@tonic-gate 		return (ENOSYS);
24577c478bd9Sstevel@tonic-gate 
24587c478bd9Sstevel@tonic-gate 	npages = btopr(len);
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	LOCK_CSP(csp);
24617c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
24627c478bd9Sstevel@tonic-gate 	mcnt = (csp->s_mapcnt -= npages);
24637c478bd9Sstevel@tonic-gate 
24647c478bd9Sstevel@tonic-gate 	if (mcnt == 0) {
24657c478bd9Sstevel@tonic-gate 		/*
24667c478bd9Sstevel@tonic-gate 		 * Call the close routine when the last reference of any
24677c478bd9Sstevel@tonic-gate 		 * kind through any [s, v]node goes away.  The s_dip hold
24687c478bd9Sstevel@tonic-gate 		 * on the devinfo node is released when the vnode is
24697c478bd9Sstevel@tonic-gate 		 * destroyed.
24707c478bd9Sstevel@tonic-gate 		 */
24717c478bd9Sstevel@tonic-gate 		if (csp->s_count == 0) {
24727c478bd9Sstevel@tonic-gate 			csp->s_flag &= ~(SNEEDCLOSE | SSIZEVALID);
24737c478bd9Sstevel@tonic-gate 
24747c478bd9Sstevel@tonic-gate 			/* See comment in spec_close() */
24757c478bd9Sstevel@tonic-gate 			if (csp->s_flag & (SCLONE | SSELFCLONE))
24767c478bd9Sstevel@tonic-gate 				csp->s_flag &= ~SDIPSET;
24777c478bd9Sstevel@tonic-gate 
24787c478bd9Sstevel@tonic-gate 			mutex_exit(&csp->s_lock);
24797c478bd9Sstevel@tonic-gate 
24807c478bd9Sstevel@tonic-gate 			(void) device_close(vp, 0, cred);
24817c478bd9Sstevel@tonic-gate 		} else
24827c478bd9Sstevel@tonic-gate 			mutex_exit(&csp->s_lock);
24837c478bd9Sstevel@tonic-gate 
24847c478bd9Sstevel@tonic-gate 		mutex_enter(&csp->s_lock);
24857c478bd9Sstevel@tonic-gate 	}
24867c478bd9Sstevel@tonic-gate 	ASSERT(mcnt >= 0);
24877c478bd9Sstevel@tonic-gate 
24887c478bd9Sstevel@tonic-gate 	UNLOCK_CSP_LOCK_HELD(csp);
24897c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
24907c478bd9Sstevel@tonic-gate 
24917c478bd9Sstevel@tonic-gate 	return (0);
24927c478bd9Sstevel@tonic-gate }
24937c478bd9Sstevel@tonic-gate 
24947c478bd9Sstevel@tonic-gate static int
24957c478bd9Sstevel@tonic-gate spec_dump(struct vnode *vp, caddr_t addr, int bn, int count)
24967c478bd9Sstevel@tonic-gate {
249725e8c5aaSvikram 	/* allow dump to succeed even if device fenced off */
249825e8c5aaSvikram 
24997c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_type == VBLK);
25007c478bd9Sstevel@tonic-gate 	return (bdev_dump(vp->v_rdev, addr, bn, count));
25017c478bd9Sstevel@tonic-gate }
25027c478bd9Sstevel@tonic-gate 
25037c478bd9Sstevel@tonic-gate 
25047c478bd9Sstevel@tonic-gate /*
25057c478bd9Sstevel@tonic-gate  * Do i/o on the given page list from/to vp, io_off for io_len.
25067c478bd9Sstevel@tonic-gate  * Flags are composed of:
25077c478bd9Sstevel@tonic-gate  * 	{B_ASYNC, B_INVAL, B_FREE, B_DONTNEED, B_READ, B_WRITE}
25087c478bd9Sstevel@tonic-gate  * If B_ASYNC is not set i/o is waited for.
25097c478bd9Sstevel@tonic-gate  */
25107c478bd9Sstevel@tonic-gate /*ARGSUSED5*/
25117c478bd9Sstevel@tonic-gate static int
25127c478bd9Sstevel@tonic-gate spec_pageio(
25137c478bd9Sstevel@tonic-gate 	struct vnode *vp,
25147c478bd9Sstevel@tonic-gate 	page_t	*pp,
25157c478bd9Sstevel@tonic-gate 	u_offset_t io_off,
25167c478bd9Sstevel@tonic-gate 	size_t	io_len,
25177c478bd9Sstevel@tonic-gate 	int	flags,
25187c478bd9Sstevel@tonic-gate 	cred_t	*cr)
25197c478bd9Sstevel@tonic-gate {
25207c478bd9Sstevel@tonic-gate 	struct buf *bp = NULL;
25217c478bd9Sstevel@tonic-gate 	int err = 0;
25227c478bd9Sstevel@tonic-gate 
25237c478bd9Sstevel@tonic-gate 	if (pp == NULL)
25247c478bd9Sstevel@tonic-gate 		return (EINVAL);
25257c478bd9Sstevel@tonic-gate 
25267c478bd9Sstevel@tonic-gate 	bp = spec_startio(vp, pp, io_off, io_len, flags);
25277c478bd9Sstevel@tonic-gate 
25287c478bd9Sstevel@tonic-gate 	/*
25297c478bd9Sstevel@tonic-gate 	 * Wait for i/o to complete if the request is not B_ASYNC.
25307c478bd9Sstevel@tonic-gate 	 */
25317c478bd9Sstevel@tonic-gate 	if ((flags & B_ASYNC) == 0) {
25327c478bd9Sstevel@tonic-gate 		err = biowait(bp);
25337c478bd9Sstevel@tonic-gate 		pageio_done(bp);
25347c478bd9Sstevel@tonic-gate 	}
25357c478bd9Sstevel@tonic-gate 	return (err);
25367c478bd9Sstevel@tonic-gate }
25377c478bd9Sstevel@tonic-gate 
25387c478bd9Sstevel@tonic-gate /*
25397c478bd9Sstevel@tonic-gate  * Set ACL on underlying vnode if one exists, or return ENOSYS otherwise.
25407c478bd9Sstevel@tonic-gate  */
25417c478bd9Sstevel@tonic-gate int
25427c478bd9Sstevel@tonic-gate spec_setsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr)
25437c478bd9Sstevel@tonic-gate {
25447c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
25457c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
25467c478bd9Sstevel@tonic-gate 	int error;
25477c478bd9Sstevel@tonic-gate 
254825e8c5aaSvikram 	/* fail with ENXIO if the device is fenced off */
254925e8c5aaSvikram 	if (S_ISFENCED(sp))
255025e8c5aaSvikram 		return (ENXIO);
255125e8c5aaSvikram 
25527c478bd9Sstevel@tonic-gate 	/*
25537c478bd9Sstevel@tonic-gate 	 * The acl(2) system calls VOP_RWLOCK on the file before setting an
25547c478bd9Sstevel@tonic-gate 	 * ACL, but since specfs does not serialize reads and writes, this
25557c478bd9Sstevel@tonic-gate 	 * VOP does not do anything.  However, some backing file systems may
25567c478bd9Sstevel@tonic-gate 	 * expect the lock to be held before setting an ACL, so it is taken
25577c478bd9Sstevel@tonic-gate 	 * here privately to avoid serializing specfs reads and writes.
25587c478bd9Sstevel@tonic-gate 	 */
25597c478bd9Sstevel@tonic-gate 	if ((realvp = sp->s_realvp) != NULL) {
25607c478bd9Sstevel@tonic-gate 		(void) VOP_RWLOCK(realvp, V_WRITELOCK_TRUE, NULL);
25617c478bd9Sstevel@tonic-gate 		error = VOP_SETSECATTR(realvp, vsap, flag, cr);
25627c478bd9Sstevel@tonic-gate 		(void) VOP_RWUNLOCK(realvp, V_WRITELOCK_TRUE, NULL);
25637c478bd9Sstevel@tonic-gate 		return (error);
25647c478bd9Sstevel@tonic-gate 	} else
25657c478bd9Sstevel@tonic-gate 		return (fs_nosys());
25667c478bd9Sstevel@tonic-gate }
25677c478bd9Sstevel@tonic-gate 
25687c478bd9Sstevel@tonic-gate /*
25697c478bd9Sstevel@tonic-gate  * Get ACL from underlying vnode if one exists, or fabricate it from
25707c478bd9Sstevel@tonic-gate  * the permissions returned by spec_getattr() otherwise.
25717c478bd9Sstevel@tonic-gate  */
25727c478bd9Sstevel@tonic-gate int
25737c478bd9Sstevel@tonic-gate spec_getsecattr(struct vnode *vp, vsecattr_t *vsap, int flag, struct cred *cr)
25747c478bd9Sstevel@tonic-gate {
25757c478bd9Sstevel@tonic-gate 	struct vnode *realvp;
25767c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
25777c478bd9Sstevel@tonic-gate 
257825e8c5aaSvikram 	/* fail with ENXIO if the device is fenced off */
257925e8c5aaSvikram 	if (S_ISFENCED(sp))
258025e8c5aaSvikram 		return (ENXIO);
258125e8c5aaSvikram 
25827c478bd9Sstevel@tonic-gate 	if ((realvp = sp->s_realvp) != NULL)
25837c478bd9Sstevel@tonic-gate 		return (VOP_GETSECATTR(realvp, vsap, flag, cr));
25847c478bd9Sstevel@tonic-gate 	else
25857c478bd9Sstevel@tonic-gate 		return (fs_fab_acl(vp, vsap, flag, cr));
25867c478bd9Sstevel@tonic-gate }
25877c478bd9Sstevel@tonic-gate 
25887c478bd9Sstevel@tonic-gate int
25897c478bd9Sstevel@tonic-gate spec_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
25907c478bd9Sstevel@tonic-gate {
25917c478bd9Sstevel@tonic-gate 	vnode_t *realvp;
25927c478bd9Sstevel@tonic-gate 	struct snode *sp = VTOS(vp);
25937c478bd9Sstevel@tonic-gate 
259425e8c5aaSvikram 	/* fail with ENXIO if the device is fenced off */
259525e8c5aaSvikram 	if (S_ISFENCED(sp))
259625e8c5aaSvikram 		return (ENXIO);
259725e8c5aaSvikram 
25987c478bd9Sstevel@tonic-gate 	if ((realvp = sp->s_realvp) != NULL)
25997c478bd9Sstevel@tonic-gate 		return (VOP_PATHCONF(realvp, cmd, valp, cr));
26007c478bd9Sstevel@tonic-gate 	else
26017c478bd9Sstevel@tonic-gate 		return (fs_pathconf(vp, cmd, valp, cr));
26027c478bd9Sstevel@tonic-gate }
2603