xref: /illumos-gate/usr/src/uts/common/os/fbio.c (revision 2d6eb4a5)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/buf.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/conf.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/fbuf.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/cred.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
44*7c478bd9Sstevel@tonic-gate #include <vm/hat.h>
45*7c478bd9Sstevel@tonic-gate #include <vm/as.h>
46*7c478bd9Sstevel@tonic-gate #include <vm/seg.h>
47*7c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
48*7c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
49*7c478bd9Sstevel@tonic-gate #include <vm/seg_kpm.h>
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * Pseudo-bio routines which use a segmap mapping to address file data.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate /*
57*7c478bd9Sstevel@tonic-gate  * Return a pointer to locked kernel virtual address for
58*7c478bd9Sstevel@tonic-gate  * the given <vp, off> for len bytes.  It is not allowed to
59*7c478bd9Sstevel@tonic-gate  * have the offset cross a MAXBSIZE boundary over len bytes.
60*7c478bd9Sstevel@tonic-gate  */
61*7c478bd9Sstevel@tonic-gate int
fbread(vnode_t * vp,offset_t off,uint_t len,enum seg_rw rw,struct fbuf ** fbpp)62*7c478bd9Sstevel@tonic-gate fbread(vnode_t *vp, offset_t off, uint_t len, enum seg_rw rw,
63*7c478bd9Sstevel@tonic-gate 	struct fbuf **fbpp)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	caddr_t addr;
66*7c478bd9Sstevel@tonic-gate 	ulong_t o;
67*7c478bd9Sstevel@tonic-gate 	struct fbuf *fbp;
68*7c478bd9Sstevel@tonic-gate 	faultcode_t err;
69*7c478bd9Sstevel@tonic-gate 	caddr_t	raddr;
70*7c478bd9Sstevel@tonic-gate 	uint_t	rsize;
71*7c478bd9Sstevel@tonic-gate 	uintptr_t pgoff = PAGEOFFSET;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate 	o = (ulong_t)(off & (offset_t)MAXBOFFSET);
74*7c478bd9Sstevel@tonic-gate 	if (o + len > MAXBSIZE)
75*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "fbread");
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	if (segmap_kpm) {
78*7c478bd9Sstevel@tonic-gate 		addr = segmap_getmapflt(segkmap, vp, off & (offset_t)MAXBMASK,
79*7c478bd9Sstevel@tonic-gate 					MAXBSIZE, SM_LOCKPROTO, rw);
80*7c478bd9Sstevel@tonic-gate 	} else {
81*7c478bd9Sstevel@tonic-gate 		addr = segmap_getmapflt(segkmap, vp,
82*7c478bd9Sstevel@tonic-gate 				off & (offset_t)MAXBMASK, MAXBSIZE, 0, rw);
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	raddr = (caddr_t)((uintptr_t)(addr + o) & ~pgoff);
86*7c478bd9Sstevel@tonic-gate 	rsize = (((uintptr_t)(addr + o) + len + pgoff) & ~pgoff) -
87*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)raddr;
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	err = segmap_fault(kas.a_hat, segkmap, raddr, rsize, F_SOFTLOCK, rw);
90*7c478bd9Sstevel@tonic-gate 	if (err) {
91*7c478bd9Sstevel@tonic-gate 		(void) segmap_release(segkmap, addr, 0);
92*7c478bd9Sstevel@tonic-gate 		if (FC_CODE(err) == FC_OBJERR)
93*7c478bd9Sstevel@tonic-gate 			return (FC_ERRNO(err));
94*7c478bd9Sstevel@tonic-gate 		else
95*7c478bd9Sstevel@tonic-gate 			return (EIO);
96*7c478bd9Sstevel@tonic-gate 	}
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	*fbpp = fbp = kmem_alloc(sizeof (struct fbuf), KM_SLEEP);
99*7c478bd9Sstevel@tonic-gate 	fbp->fb_addr = addr + o;
100*7c478bd9Sstevel@tonic-gate 	fbp->fb_count = len;
101*7c478bd9Sstevel@tonic-gate 	return (0);
102*7c478bd9Sstevel@tonic-gate }
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate /*
105*7c478bd9Sstevel@tonic-gate  * Similar to fbread() but we call segmap_pagecreate instead of using
106*7c478bd9Sstevel@tonic-gate  * segmap_fault for SOFTLOCK to create the pages without using VOP_GETPAGE
107*7c478bd9Sstevel@tonic-gate  * and then we zero up to the length rounded to a page boundary.
108*7c478bd9Sstevel@tonic-gate  * XXX - this won't work right when bsize < PAGESIZE!!!
109*7c478bd9Sstevel@tonic-gate  */
110*7c478bd9Sstevel@tonic-gate void
fbzero(vnode_t * vp,offset_t off,uint_t len,struct fbuf ** fbpp)111*7c478bd9Sstevel@tonic-gate fbzero(vnode_t *vp, offset_t off, uint_t len, struct fbuf **fbpp)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	caddr_t addr;
114*7c478bd9Sstevel@tonic-gate 	ulong_t o, zlen;
115*7c478bd9Sstevel@tonic-gate 	struct fbuf *fbp;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	o = (ulong_t)(off & MAXBOFFSET);
118*7c478bd9Sstevel@tonic-gate 	if (o + len > MAXBSIZE)
119*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "fbzero: Bad offset/length");
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (segmap_kpm) {
122*7c478bd9Sstevel@tonic-gate 		addr = segmap_getmapflt(segkmap, vp, off & (offset_t)MAXBMASK,
123*7c478bd9Sstevel@tonic-gate 				MAXBSIZE, SM_PAGECREATE, S_WRITE) + o;
124*7c478bd9Sstevel@tonic-gate 	} else {
125*7c478bd9Sstevel@tonic-gate 		addr = segmap_getmap(segkmap, vp, off & (offset_t)MAXBMASK) + o;
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	*fbpp = fbp = kmem_alloc(sizeof (struct fbuf), KM_SLEEP);
129*7c478bd9Sstevel@tonic-gate 	fbp->fb_addr = addr;
130*7c478bd9Sstevel@tonic-gate 	fbp->fb_count = len;
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	(void) segmap_pagecreate(segkmap, addr, len, 1);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	/*
135*7c478bd9Sstevel@tonic-gate 	 * Now we zero all the memory in the mapping we are interested in.
136*7c478bd9Sstevel@tonic-gate 	 */
137*7c478bd9Sstevel@tonic-gate 	zlen = (caddr_t)ptob(btopr((uintptr_t)(len + addr))) - addr;
138*7c478bd9Sstevel@tonic-gate 	if (zlen < len || (o + zlen > MAXBSIZE))
139*7c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "fbzero: Bad zlen");
140*7c478bd9Sstevel@tonic-gate 	bzero(addr, zlen);
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*
144*7c478bd9Sstevel@tonic-gate  * FBCOMMON() is the common code for fbrelse, fbwrite and variants thereof:
145*7c478bd9Sstevel@tonic-gate  *
146*7c478bd9Sstevel@tonic-gate  * fbrelse()	release fbp
147*7c478bd9Sstevel@tonic-gate  * fbwrite()	direct write
148*7c478bd9Sstevel@tonic-gate  * fbdwrite()	delayed write
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate #define	FBCOMMON(fbp, rw, flags, howtoreturn) \
151*7c478bd9Sstevel@tonic-gate { \
152*7c478bd9Sstevel@tonic-gate 	caddr_t addr; \
153*7c478bd9Sstevel@tonic-gate 	size_t size; \
154*7c478bd9Sstevel@tonic-gate 	uintptr_t pgoff = PAGEOFFSET; \
155*7c478bd9Sstevel@tonic-gate 	addr = (caddr_t)((uintptr_t)fbp->fb_addr & ~pgoff); \
156*7c478bd9Sstevel@tonic-gate 	size = ((fbp->fb_addr - addr) + fbp->fb_count + pgoff) & ~pgoff; \
157*7c478bd9Sstevel@tonic-gate 	(void) segmap_fault(kas.a_hat, segkmap, addr, size, F_SOFTUNLOCK, rw); \
158*7c478bd9Sstevel@tonic-gate 	addr = (caddr_t)((uintptr_t)fbp->fb_addr & MAXBMASK); \
159*7c478bd9Sstevel@tonic-gate 	kmem_free(fbp, sizeof (struct fbuf)); \
160*7c478bd9Sstevel@tonic-gate 	howtoreturn(segmap_release(segkmap, addr, flags)); \
161*7c478bd9Sstevel@tonic-gate }
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate void
fbrelse(struct fbuf * fbp,enum seg_rw rw)164*7c478bd9Sstevel@tonic-gate fbrelse(struct fbuf *fbp, enum seg_rw rw)
165*7c478bd9Sstevel@tonic-gate {
166*7c478bd9Sstevel@tonic-gate 	FBCOMMON(fbp, rw, 0, (void))
167*7c478bd9Sstevel@tonic-gate }
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate int
fbwrite(struct fbuf * fbp)170*7c478bd9Sstevel@tonic-gate fbwrite(struct fbuf *fbp)
171*7c478bd9Sstevel@tonic-gate {
172*7c478bd9Sstevel@tonic-gate 	FBCOMMON(fbp, S_WRITE, SM_WRITE, return)
173*7c478bd9Sstevel@tonic-gate }
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate int
fbdwrite(struct fbuf * fbp)176*7c478bd9Sstevel@tonic-gate fbdwrite(struct fbuf *fbp)
177*7c478bd9Sstevel@tonic-gate {
178*7c478bd9Sstevel@tonic-gate 	FBCOMMON(fbp, S_WRITE, 0, return)
179*7c478bd9Sstevel@tonic-gate }
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate /*
182*7c478bd9Sstevel@tonic-gate  * Perform a synchronous indirect write of the given block number
183*7c478bd9Sstevel@tonic-gate  * on the given device, using the given fbuf.  Upon return the fbp
184*7c478bd9Sstevel@tonic-gate  * is invalid.
185*7c478bd9Sstevel@tonic-gate  */
186*7c478bd9Sstevel@tonic-gate int
fbiwrite(struct fbuf * fbp,vnode_t * devvp,daddr_t bn,int bsize)187*7c478bd9Sstevel@tonic-gate fbiwrite(struct fbuf *fbp, vnode_t *devvp, daddr_t bn, int bsize)
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate 	struct buf *bp;
190*7c478bd9Sstevel@tonic-gate 	int error, fberror;
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	/*
193*7c478bd9Sstevel@tonic-gate 	 * Allocate a temp bp using pageio_setup, but then use it
194*7c478bd9Sstevel@tonic-gate 	 * for physio to the area mapped by fbuf which is currently
195*7c478bd9Sstevel@tonic-gate 	 * all locked down in place.
196*7c478bd9Sstevel@tonic-gate 	 *
197*7c478bd9Sstevel@tonic-gate 	 * XXX - need to have a generalized bp header facility
198*7c478bd9Sstevel@tonic-gate 	 * which we build up pageio_setup on top of.  Other places
199*7c478bd9Sstevel@tonic-gate 	 * (like here and in device drivers for the raw I/O case)
200*7c478bd9Sstevel@tonic-gate 	 * could then use these new facilities in a more straight
201*7c478bd9Sstevel@tonic-gate 	 * forward fashion instead of playing all these games.
202*7c478bd9Sstevel@tonic-gate 	 */
203*7c478bd9Sstevel@tonic-gate 	bp = pageio_setup((struct page *)NULL, fbp->fb_count, devvp, B_WRITE);
204*7c478bd9Sstevel@tonic-gate 	bp->b_flags &= ~B_PAGEIO;		/* XXX */
205*7c478bd9Sstevel@tonic-gate 	bp->b_un.b_addr = fbp->fb_addr;
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 	bp->b_blkno = bn * btod(bsize);
208*7c478bd9Sstevel@tonic-gate 	bp->b_dev = cmpdev(devvp->v_rdev);	/* store in old dev format */
209*7c478bd9Sstevel@tonic-gate 	bp->b_edev = devvp->v_rdev;
210*7c478bd9Sstevel@tonic-gate 	bp->b_proc = NULL;			/* i.e. the kernel */
211*7c478bd9Sstevel@tonic-gate 
212*7c478bd9Sstevel@tonic-gate 	(void) bdev_strategy(bp);
213*7c478bd9Sstevel@tonic-gate 	error = biowait(bp);
214*7c478bd9Sstevel@tonic-gate 	pageio_done(bp);
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	/*CSTYLED*/
217*7c478bd9Sstevel@tonic-gate 	FBCOMMON(fbp, S_OTHER, 0, fberror = )
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 	return (error ? error : fberror);
220*7c478bd9Sstevel@tonic-gate }
221