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