vnd.c revision 1.34 1 1.34 pk /* $NetBSD: vnd.c,v 1.34 1997/05/19 22:08:56 pk Exp $ */
2 1.11 cgd
3 1.1 brezak /*
4 1.1 brezak * Copyright (c) 1988 University of Utah.
5 1.1 brezak * Copyright (c) 1990, 1993
6 1.1 brezak * The Regents of the University of California. All rights reserved.
7 1.1 brezak *
8 1.1 brezak * This code is derived from software contributed to Berkeley by
9 1.1 brezak * the Systems Programming Group of the University of Utah Computer
10 1.1 brezak * Science Department.
11 1.1 brezak *
12 1.1 brezak * Redistribution and use in source and binary forms, with or without
13 1.1 brezak * modification, are permitted provided that the following conditions
14 1.1 brezak * are met:
15 1.1 brezak * 1. Redistributions of source code must retain the above copyright
16 1.1 brezak * notice, this list of conditions and the following disclaimer.
17 1.1 brezak * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 brezak * notice, this list of conditions and the following disclaimer in the
19 1.1 brezak * documentation and/or other materials provided with the distribution.
20 1.1 brezak * 3. All advertising materials mentioning features or use of this software
21 1.1 brezak * must display the following acknowledgement:
22 1.1 brezak * This product includes software developed by the University of
23 1.1 brezak * California, Berkeley and its contributors.
24 1.1 brezak * 4. Neither the name of the University nor the names of its contributors
25 1.1 brezak * may be used to endorse or promote products derived from this software
26 1.1 brezak * without specific prior written permission.
27 1.1 brezak *
28 1.1 brezak * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 brezak * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 brezak * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 brezak * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 brezak * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 brezak * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 brezak * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 brezak * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 brezak * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 brezak * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 brezak * SUCH DAMAGE.
39 1.1 brezak *
40 1.5 cgd * from: Utah $Hdr: vn.c 1.13 94/04/02$
41 1.1 brezak *
42 1.5 cgd * @(#)vn.c 8.6 (Berkeley) 4/1/94
43 1.1 brezak */
44 1.1 brezak
45 1.1 brezak /*
46 1.1 brezak * Vnode disk driver.
47 1.1 brezak *
48 1.1 brezak * Block/character interface to a vnode. Allows one to treat a file
49 1.1 brezak * as a disk (e.g. build a filesystem in it, mount it, etc.).
50 1.1 brezak *
51 1.1 brezak * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
52 1.1 brezak * instead of a simple VOP_RDWR. We do this to avoid distorting the
53 1.1 brezak * local buffer cache.
54 1.1 brezak *
55 1.1 brezak * NOTE 2: There is a security issue involved with this driver.
56 1.1 brezak * Once mounted all access to the contents of the "mapped" file via
57 1.1 brezak * the special file is controlled by the permissions on the special
58 1.1 brezak * file, the protection of the mapped file is ignored (effectively,
59 1.1 brezak * by using root credentials in all transactions).
60 1.1 brezak *
61 1.1 brezak * NOTE 3: Doesn't interact with leases, should it?
62 1.1 brezak */
63 1.1 brezak
64 1.1 brezak #include <sys/param.h>
65 1.1 brezak #include <sys/systm.h>
66 1.1 brezak #include <sys/namei.h>
67 1.1 brezak #include <sys/proc.h>
68 1.1 brezak #include <sys/errno.h>
69 1.1 brezak #include <sys/buf.h>
70 1.1 brezak #include <sys/malloc.h>
71 1.1 brezak #include <sys/ioctl.h>
72 1.16 cgd #include <sys/disklabel.h>
73 1.22 thorpej #include <sys/device.h>
74 1.22 thorpej #include <sys/disk.h>
75 1.22 thorpej #include <sys/stat.h>
76 1.1 brezak #include <sys/mount.h>
77 1.1 brezak #include <sys/vnode.h>
78 1.1 brezak #include <sys/file.h>
79 1.1 brezak #include <sys/uio.h>
80 1.26 christos #include <sys/conf.h>
81 1.1 brezak
82 1.1 brezak #include <miscfs/specfs/specdev.h>
83 1.1 brezak
84 1.17 cgd #include <dev/vndioctl.h>
85 1.1 brezak
86 1.1 brezak #ifdef DEBUG
87 1.17 cgd int dovndcluster = 1;
88 1.17 cgd int vnddebug = 0x00;
89 1.1 brezak #define VDB_FOLLOW 0x01
90 1.1 brezak #define VDB_INIT 0x02
91 1.1 brezak #define VDB_IO 0x04
92 1.1 brezak #endif
93 1.1 brezak
94 1.1 brezak #define b_cylin b_resid
95 1.1 brezak
96 1.18 cgd #define vndunit(x) DISKUNIT(x)
97 1.18 cgd
98 1.18 cgd struct vndbuf {
99 1.18 cgd struct buf vb_buf;
100 1.18 cgd struct buf *vb_obp;
101 1.18 cgd };
102 1.1 brezak
103 1.17 cgd #define getvndbuf() \
104 1.18 cgd ((struct vndbuf *)malloc(sizeof(struct vndbuf), M_DEVBUF, M_WAITOK))
105 1.18 cgd #define putvndbuf(vbp) \
106 1.18 cgd free((caddr_t)(vbp), M_DEVBUF)
107 1.1 brezak
108 1.17 cgd struct vnd_softc {
109 1.1 brezak int sc_flags; /* flags */
110 1.17 cgd size_t sc_size; /* size of vnd */
111 1.1 brezak struct vnode *sc_vp; /* vnode */
112 1.1 brezak struct ucred *sc_cred; /* credentials */
113 1.1 brezak int sc_maxactive; /* max # of active requests */
114 1.1 brezak struct buf sc_tab; /* transfer queue */
115 1.23 thorpej char sc_xname[8]; /* XXX external name */
116 1.23 thorpej struct disk sc_dkdev; /* generic disk device info */
117 1.1 brezak };
118 1.1 brezak
119 1.1 brezak /* sc_flags */
120 1.1 brezak #define VNF_ALIVE 0x01
121 1.1 brezak #define VNF_INITED 0x02
122 1.22 thorpej #define VNF_WANTED 0x40
123 1.22 thorpej #define VNF_LOCKED 0x80
124 1.1 brezak
125 1.17 cgd struct vnd_softc *vnd_softc;
126 1.22 thorpej int numvnd = 0;
127 1.22 thorpej
128 1.22 thorpej /* called by main() at boot time */
129 1.22 thorpej void vndattach __P((int));
130 1.1 brezak
131 1.17 cgd void vndclear __P((struct vnd_softc *));
132 1.17 cgd void vndstart __P((struct vnd_softc *));
133 1.17 cgd int vndsetcred __P((struct vnd_softc *, struct ucred *));
134 1.17 cgd void vndthrottle __P((struct vnd_softc *, struct vnode *));
135 1.24 christos void vndiodone __P((struct buf *));
136 1.24 christos void vndshutdown __P((void));
137 1.16 cgd
138 1.22 thorpej static int vndlock __P((struct vnd_softc *));
139 1.22 thorpej static void vndunlock __P((struct vnd_softc *));
140 1.22 thorpej
141 1.1 brezak void
142 1.17 cgd vndattach(num)
143 1.1 brezak int num;
144 1.1 brezak {
145 1.1 brezak char *mem;
146 1.1 brezak register u_long size;
147 1.1 brezak
148 1.1 brezak if (num <= 0)
149 1.1 brezak return;
150 1.17 cgd size = num * sizeof(struct vnd_softc);
151 1.1 brezak mem = malloc(size, M_DEVBUF, M_NOWAIT);
152 1.1 brezak if (mem == NULL) {
153 1.30 christos printf("WARNING: no memory for vnode disks\n");
154 1.1 brezak return;
155 1.1 brezak }
156 1.1 brezak bzero(mem, size);
157 1.17 cgd vnd_softc = (struct vnd_softc *)mem;
158 1.1 brezak numvnd = num;
159 1.1 brezak }
160 1.1 brezak
161 1.1 brezak int
162 1.17 cgd vndopen(dev, flags, mode, p)
163 1.1 brezak dev_t dev;
164 1.1 brezak int flags, mode;
165 1.1 brezak struct proc *p;
166 1.1 brezak {
167 1.17 cgd int unit = vndunit(dev);
168 1.22 thorpej struct vnd_softc *sc;
169 1.22 thorpej int error = 0, part, pmask;
170 1.22 thorpej
171 1.22 thorpej /*
172 1.22 thorpej * XXX Should support disklabels.
173 1.22 thorpej */
174 1.1 brezak
175 1.1 brezak #ifdef DEBUG
176 1.17 cgd if (vnddebug & VDB_FOLLOW)
177 1.30 christos printf("vndopen(%x, %x, %x, %p)\n", dev, flags, mode, p);
178 1.1 brezak #endif
179 1.1 brezak if (unit >= numvnd)
180 1.22 thorpej return (ENXIO);
181 1.22 thorpej sc = &vnd_softc[unit];
182 1.22 thorpej
183 1.24 christos if ((error = vndlock(sc)) != 0)
184 1.22 thorpej return (error);
185 1.22 thorpej
186 1.22 thorpej part = DISKPART(dev);
187 1.22 thorpej pmask = (1 << part);
188 1.22 thorpej
189 1.22 thorpej /* Prevent our unit from being unconfigured while open. */
190 1.22 thorpej switch (mode) {
191 1.22 thorpej case S_IFCHR:
192 1.22 thorpej sc->sc_dkdev.dk_copenmask |= pmask;
193 1.22 thorpej break;
194 1.22 thorpej
195 1.22 thorpej case S_IFBLK:
196 1.22 thorpej sc->sc_dkdev.dk_bopenmask |= pmask;
197 1.22 thorpej break;
198 1.22 thorpej }
199 1.22 thorpej sc->sc_dkdev.dk_openmask =
200 1.22 thorpej sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
201 1.22 thorpej
202 1.22 thorpej vndunlock(sc);
203 1.22 thorpej return (0);
204 1.1 brezak }
205 1.1 brezak
206 1.4 deraadt int
207 1.17 cgd vndclose(dev, flags, mode, p)
208 1.4 deraadt dev_t dev;
209 1.4 deraadt int flags, mode;
210 1.4 deraadt struct proc *p;
211 1.4 deraadt {
212 1.22 thorpej int unit = vndunit(dev);
213 1.22 thorpej struct vnd_softc *sc;
214 1.22 thorpej int error = 0, part;
215 1.22 thorpej
216 1.4 deraadt #ifdef DEBUG
217 1.17 cgd if (vnddebug & VDB_FOLLOW)
218 1.30 christos printf("vndclose(%x, %x, %x, %p)\n", dev, flags, mode, p);
219 1.4 deraadt #endif
220 1.22 thorpej
221 1.22 thorpej if (unit >= numvnd)
222 1.22 thorpej return (ENXIO);
223 1.22 thorpej sc = &vnd_softc[unit];
224 1.22 thorpej
225 1.24 christos if ((error = vndlock(sc)) != 0)
226 1.22 thorpej return (error);
227 1.22 thorpej
228 1.22 thorpej part = DISKPART(dev);
229 1.22 thorpej
230 1.22 thorpej /* ...that much closer to allowing unconfiguration... */
231 1.22 thorpej switch (mode) {
232 1.22 thorpej case S_IFCHR:
233 1.22 thorpej sc->sc_dkdev.dk_copenmask &= ~(1 << part);
234 1.22 thorpej break;
235 1.22 thorpej
236 1.22 thorpej case S_IFBLK:
237 1.22 thorpej sc->sc_dkdev.dk_bopenmask &= ~(1 << part);
238 1.22 thorpej break;
239 1.22 thorpej }
240 1.22 thorpej sc->sc_dkdev.dk_openmask =
241 1.22 thorpej sc->sc_dkdev.dk_copenmask | sc->sc_dkdev.dk_bopenmask;
242 1.22 thorpej
243 1.22 thorpej vndunlock(sc);
244 1.22 thorpej return (0);
245 1.4 deraadt }
246 1.4 deraadt
247 1.1 brezak /*
248 1.1 brezak * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
249 1.1 brezak * Note that this driver can only be used for swapping over NFS on the hp
250 1.1 brezak * since nfs_strategy on the vax cannot handle u-areas and page tables.
251 1.1 brezak */
252 1.6 mycroft void
253 1.17 cgd vndstrategy(bp)
254 1.1 brezak register struct buf *bp;
255 1.1 brezak {
256 1.17 cgd int unit = vndunit(bp->b_dev);
257 1.17 cgd register struct vnd_softc *vnd = &vnd_softc[unit];
258 1.18 cgd register struct vndbuf *nbp;
259 1.1 brezak register int bn, bsize, resid;
260 1.1 brezak register caddr_t addr;
261 1.5 cgd int sz, flags, error;
262 1.1 brezak
263 1.1 brezak #ifdef DEBUG
264 1.17 cgd if (vnddebug & VDB_FOLLOW)
265 1.30 christos printf("vndstrategy(%p): unit %d\n", bp, unit);
266 1.1 brezak #endif
267 1.17 cgd if ((vnd->sc_flags & VNF_INITED) == 0) {
268 1.1 brezak bp->b_error = ENXIO;
269 1.1 brezak bp->b_flags |= B_ERROR;
270 1.1 brezak biodone(bp);
271 1.1 brezak return;
272 1.1 brezak }
273 1.1 brezak bn = bp->b_blkno;
274 1.1 brezak sz = howmany(bp->b_bcount, DEV_BSIZE);
275 1.1 brezak bp->b_resid = bp->b_bcount;
276 1.17 cgd if (bn < 0 || bn + sz > vnd->sc_size) {
277 1.17 cgd if (bn != vnd->sc_size) {
278 1.1 brezak bp->b_error = EINVAL;
279 1.1 brezak bp->b_flags |= B_ERROR;
280 1.1 brezak }
281 1.1 brezak biodone(bp);
282 1.1 brezak return;
283 1.1 brezak }
284 1.1 brezak bn = dbtob(bn);
285 1.17 cgd bsize = vnd->sc_vp->v_mount->mnt_stat.f_iosize;
286 1.5 cgd addr = bp->b_data;
287 1.1 brezak flags = bp->b_flags | B_CALL;
288 1.1 brezak for (resid = bp->b_resid; resid; resid -= sz) {
289 1.1 brezak struct vnode *vp;
290 1.1 brezak daddr_t nbn;
291 1.5 cgd int off, s, nra;
292 1.1 brezak
293 1.5 cgd nra = 0;
294 1.21 mycroft VOP_LOCK(vnd->sc_vp);
295 1.17 cgd error = VOP_BMAP(vnd->sc_vp, bn / bsize, &vp, &nbn, &nra);
296 1.21 mycroft VOP_UNLOCK(vnd->sc_vp);
297 1.5 cgd if (error == 0 && (long)nbn == -1)
298 1.5 cgd error = EIO;
299 1.5 cgd #ifdef DEBUG
300 1.17 cgd if (!dovndcluster)
301 1.5 cgd nra = 0;
302 1.1 brezak #endif
303 1.5 cgd
304 1.24 christos if ((off = bn % bsize) != 0)
305 1.5 cgd sz = bsize - off;
306 1.5 cgd else
307 1.5 cgd sz = (1 + nra) * bsize;
308 1.5 cgd if (resid < sz)
309 1.5 cgd sz = resid;
310 1.1 brezak #ifdef DEBUG
311 1.17 cgd if (vnddebug & VDB_IO)
312 1.30 christos printf("vndstrategy: vp %p/%p bn %x/%x sz %x\n",
313 1.29 christos vnd->sc_vp, vp, bn, nbn, sz);
314 1.1 brezak #endif
315 1.5 cgd
316 1.17 cgd nbp = getvndbuf();
317 1.18 cgd nbp->vb_buf.b_flags = flags;
318 1.18 cgd nbp->vb_buf.b_bcount = sz;
319 1.18 cgd nbp->vb_buf.b_bufsize = bp->b_bufsize;
320 1.18 cgd nbp->vb_buf.b_error = 0;
321 1.1 brezak if (vp->v_type == VBLK || vp->v_type == VCHR)
322 1.18 cgd nbp->vb_buf.b_dev = vp->v_rdev;
323 1.1 brezak else
324 1.18 cgd nbp->vb_buf.b_dev = NODEV;
325 1.18 cgd nbp->vb_buf.b_data = addr;
326 1.18 cgd nbp->vb_buf.b_blkno = nbn + btodb(off);
327 1.18 cgd nbp->vb_buf.b_proc = bp->b_proc;
328 1.18 cgd nbp->vb_buf.b_iodone = vndiodone;
329 1.18 cgd nbp->vb_buf.b_vp = vp;
330 1.18 cgd nbp->vb_buf.b_rcred = vnd->sc_cred; /* XXX crdup? */
331 1.18 cgd nbp->vb_buf.b_wcred = vnd->sc_cred; /* XXX crdup? */
332 1.33 pk if (bp->b_dirtyend == 0) {
333 1.33 pk nbp->vb_buf.b_dirtyoff = 0;
334 1.33 pk nbp->vb_buf.b_dirtyend = sz;
335 1.33 pk } else {
336 1.33 pk nbp->vb_buf.b_dirtyoff =
337 1.33 pk max(0, bp->b_dirtyoff - (bp->b_bcount - resid));
338 1.33 pk nbp->vb_buf.b_dirtyend =
339 1.34 pk min(sz,
340 1.34 pk max(0, bp->b_dirtyend - (bp->b_bcount-resid)));
341 1.33 pk }
342 1.33 pk if (bp->b_validend == 0) {
343 1.33 pk nbp->vb_buf.b_validoff = 0;
344 1.33 pk nbp->vb_buf.b_validend = sz;
345 1.33 pk } else {
346 1.33 pk nbp->vb_buf.b_validoff =
347 1.33 pk max(0, bp->b_validoff - (bp->b_bcount - resid));
348 1.33 pk nbp->vb_buf.b_validend =
349 1.34 pk min(sz,
350 1.34 pk max(0, bp->b_validend - (bp->b_bcount-resid)));
351 1.33 pk }
352 1.18 cgd
353 1.18 cgd /* save a reference to the old buffer */
354 1.18 cgd nbp->vb_obp = bp;
355 1.18 cgd
356 1.1 brezak /*
357 1.5 cgd * If there was an error or a hole in the file...punt.
358 1.1 brezak * Note that we deal with this after the nbp allocation.
359 1.1 brezak * This ensures that we properly clean up any operations
360 1.1 brezak * that we have already fired off.
361 1.1 brezak *
362 1.5 cgd * XXX we could deal with holes here but it would be
363 1.1 brezak * a hassle (in the write case).
364 1.1 brezak */
365 1.5 cgd if (error) {
366 1.18 cgd nbp->vb_buf.b_error = error;
367 1.18 cgd nbp->vb_buf.b_flags |= B_ERROR;
368 1.1 brezak bp->b_resid -= (resid - sz);
369 1.18 cgd biodone(&nbp->vb_buf);
370 1.1 brezak return;
371 1.1 brezak }
372 1.1 brezak /*
373 1.1 brezak * Just sort by block number
374 1.1 brezak */
375 1.18 cgd nbp->vb_buf.b_cylin = nbp->vb_buf.b_blkno;
376 1.1 brezak s = splbio();
377 1.18 cgd disksort(&vnd->sc_tab, &nbp->vb_buf);
378 1.17 cgd if (vnd->sc_tab.b_active < vnd->sc_maxactive) {
379 1.17 cgd vnd->sc_tab.b_active++;
380 1.17 cgd vndstart(vnd);
381 1.1 brezak }
382 1.1 brezak splx(s);
383 1.1 brezak bn += sz;
384 1.1 brezak addr += sz;
385 1.1 brezak }
386 1.1 brezak }
387 1.1 brezak
388 1.1 brezak /*
389 1.1 brezak * Feed requests sequentially.
390 1.1 brezak * We do it this way to keep from flooding NFS servers if we are connected
391 1.1 brezak * to an NFS file. This places the burden on the client rather than the
392 1.1 brezak * server.
393 1.1 brezak */
394 1.16 cgd void
395 1.17 cgd vndstart(vnd)
396 1.17 cgd register struct vnd_softc *vnd;
397 1.1 brezak {
398 1.1 brezak register struct buf *bp;
399 1.1 brezak
400 1.1 brezak /*
401 1.1 brezak * Dequeue now since lower level strategy routine might
402 1.1 brezak * queue using same links
403 1.1 brezak */
404 1.17 cgd bp = vnd->sc_tab.b_actf;
405 1.17 cgd vnd->sc_tab.b_actf = bp->b_actf;
406 1.1 brezak #ifdef DEBUG
407 1.17 cgd if (vnddebug & VDB_IO)
408 1.30 christos printf("vndstart(%ld): bp %p vp %p blkno %x addr %p cnt %lx\n",
409 1.28 christos (long) (vnd-vnd_softc), bp, bp->b_vp, bp->b_blkno,
410 1.28 christos bp->b_data, bp->b_bcount);
411 1.1 brezak #endif
412 1.23 thorpej
413 1.23 thorpej /* Instrumentation. */
414 1.23 thorpej disk_busy(&vnd->sc_dkdev);
415 1.23 thorpej
416 1.1 brezak if ((bp->b_flags & B_READ) == 0)
417 1.1 brezak bp->b_vp->v_numoutput++;
418 1.1 brezak VOP_STRATEGY(bp);
419 1.1 brezak }
420 1.1 brezak
421 1.1 brezak void
422 1.24 christos vndiodone(bp)
423 1.24 christos struct buf *bp;
424 1.1 brezak {
425 1.24 christos register struct vndbuf *vbp = (struct vndbuf *) bp;
426 1.18 cgd register struct buf *pbp = vbp->vb_obp;
427 1.17 cgd register struct vnd_softc *vnd = &vnd_softc[vndunit(pbp->b_dev)];
428 1.1 brezak int s;
429 1.1 brezak
430 1.1 brezak s = splbio();
431 1.1 brezak #ifdef DEBUG
432 1.17 cgd if (vnddebug & VDB_IO)
433 1.30 christos printf("vndiodone(%ld): vbp %p vp %p blkno %x addr %p cnt %lx\n",
434 1.28 christos (long) (vnd-vnd_softc), vbp, vbp->vb_buf.b_vp,
435 1.28 christos vbp->vb_buf.b_blkno, vbp->vb_buf.b_data,
436 1.28 christos vbp->vb_buf.b_bcount);
437 1.1 brezak #endif
438 1.23 thorpej
439 1.18 cgd if (vbp->vb_buf.b_error) {
440 1.1 brezak #ifdef DEBUG
441 1.17 cgd if (vnddebug & VDB_IO)
442 1.30 christos printf("vndiodone: vbp %p error %d\n", vbp,
443 1.18 cgd vbp->vb_buf.b_error);
444 1.1 brezak #endif
445 1.1 brezak pbp->b_flags |= B_ERROR;
446 1.18 cgd pbp->b_error = biowait(&vbp->vb_buf);
447 1.1 brezak }
448 1.18 cgd pbp->b_resid -= vbp->vb_buf.b_bcount;
449 1.18 cgd putvndbuf(vbp);
450 1.23 thorpej disk_unbusy(&vnd->sc_dkdev, (pbp->b_bcount - pbp->b_resid));
451 1.1 brezak if (pbp->b_resid == 0) {
452 1.1 brezak #ifdef DEBUG
453 1.17 cgd if (vnddebug & VDB_IO)
454 1.30 christos printf("vndiodone: pbp %p iodone\n", pbp);
455 1.1 brezak #endif
456 1.1 brezak biodone(pbp);
457 1.1 brezak }
458 1.17 cgd if (vnd->sc_tab.b_actf)
459 1.17 cgd vndstart(vnd);
460 1.1 brezak else
461 1.17 cgd vnd->sc_tab.b_active--;
462 1.1 brezak splx(s);
463 1.20 mycroft }
464 1.20 mycroft
465 1.22 thorpej /* ARGSUSED */
466 1.20 mycroft int
467 1.22 thorpej vndread(dev, uio, flags)
468 1.20 mycroft dev_t dev;
469 1.20 mycroft struct uio *uio;
470 1.22 thorpej int flags;
471 1.20 mycroft {
472 1.22 thorpej int unit = vndunit(dev);
473 1.22 thorpej struct vnd_softc *sc;
474 1.20 mycroft
475 1.20 mycroft #ifdef DEBUG
476 1.20 mycroft if (vnddebug & VDB_FOLLOW)
477 1.30 christos printf("vndread(%x, %p)\n", dev, uio);
478 1.20 mycroft #endif
479 1.22 thorpej
480 1.22 thorpej if (unit >= numvnd)
481 1.22 thorpej return (ENXIO);
482 1.22 thorpej sc = &vnd_softc[unit];
483 1.22 thorpej
484 1.22 thorpej if ((sc->sc_flags & VNF_INITED) == 0)
485 1.22 thorpej return (ENXIO);
486 1.22 thorpej
487 1.20 mycroft return (physio(vndstrategy, NULL, dev, B_READ, minphys, uio));
488 1.20 mycroft }
489 1.20 mycroft
490 1.22 thorpej /* ARGSUSED */
491 1.20 mycroft int
492 1.22 thorpej vndwrite(dev, uio, flags)
493 1.20 mycroft dev_t dev;
494 1.20 mycroft struct uio *uio;
495 1.22 thorpej int flags;
496 1.20 mycroft {
497 1.22 thorpej int unit = vndunit(dev);
498 1.22 thorpej struct vnd_softc *sc;
499 1.20 mycroft
500 1.20 mycroft #ifdef DEBUG
501 1.20 mycroft if (vnddebug & VDB_FOLLOW)
502 1.30 christos printf("vndwrite(%x, %p)\n", dev, uio);
503 1.20 mycroft #endif
504 1.22 thorpej
505 1.22 thorpej if (unit >= numvnd)
506 1.22 thorpej return (ENXIO);
507 1.22 thorpej sc = &vnd_softc[unit];
508 1.22 thorpej
509 1.22 thorpej if ((sc->sc_flags & VNF_INITED) == 0)
510 1.22 thorpej return (ENXIO);
511 1.22 thorpej
512 1.20 mycroft return (physio(vndstrategy, NULL, dev, B_WRITE, minphys, uio));
513 1.1 brezak }
514 1.1 brezak
515 1.1 brezak /* ARGSUSED */
516 1.16 cgd int
517 1.17 cgd vndioctl(dev, cmd, data, flag, p)
518 1.1 brezak dev_t dev;
519 1.1 brezak u_long cmd;
520 1.1 brezak caddr_t data;
521 1.1 brezak int flag;
522 1.1 brezak struct proc *p;
523 1.1 brezak {
524 1.17 cgd int unit = vndunit(dev);
525 1.17 cgd register struct vnd_softc *vnd;
526 1.17 cgd struct vnd_ioctl *vio;
527 1.1 brezak struct vattr vattr;
528 1.1 brezak struct nameidata nd;
529 1.32 mycroft int error, part, pmask;
530 1.1 brezak
531 1.1 brezak #ifdef DEBUG
532 1.17 cgd if (vnddebug & VDB_FOLLOW)
533 1.30 christos printf("vndioctl(%x, %lx, %p, %x, %p): unit %d\n",
534 1.5 cgd dev, cmd, data, flag, p, unit);
535 1.1 brezak #endif
536 1.1 brezak error = suser(p->p_ucred, &p->p_acflag);
537 1.1 brezak if (error)
538 1.1 brezak return (error);
539 1.1 brezak if (unit >= numvnd)
540 1.1 brezak return (ENXIO);
541 1.1 brezak
542 1.17 cgd vnd = &vnd_softc[unit];
543 1.17 cgd vio = (struct vnd_ioctl *)data;
544 1.1 brezak switch (cmd) {
545 1.1 brezak
546 1.17 cgd case VNDIOCSET:
547 1.17 cgd if (vnd->sc_flags & VNF_INITED)
548 1.22 thorpej return (EBUSY);
549 1.22 thorpej
550 1.24 christos if ((error = vndlock(vnd)) != 0)
551 1.22 thorpej return (error);
552 1.22 thorpej
553 1.1 brezak /*
554 1.1 brezak * Always open for read and write.
555 1.1 brezak * This is probably bogus, but it lets vn_open()
556 1.1 brezak * weed out directories, sockets, etc. so we don't
557 1.1 brezak * have to worry about them.
558 1.1 brezak */
559 1.17 cgd NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vnd_file, p);
560 1.24 christos if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
561 1.22 thorpej vndunlock(vnd);
562 1.1 brezak return(error);
563 1.22 thorpej }
564 1.24 christos error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p);
565 1.24 christos if (error) {
566 1.1 brezak VOP_UNLOCK(nd.ni_vp);
567 1.1 brezak (void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
568 1.22 thorpej vndunlock(vnd);
569 1.1 brezak return(error);
570 1.1 brezak }
571 1.1 brezak VOP_UNLOCK(nd.ni_vp);
572 1.17 cgd vnd->sc_vp = nd.ni_vp;
573 1.17 cgd vnd->sc_size = btodb(vattr.va_size); /* note truncation */
574 1.24 christos if ((error = vndsetcred(vnd, p->p_ucred)) != 0) {
575 1.5 cgd (void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
576 1.22 thorpej vndunlock(vnd);
577 1.1 brezak return(error);
578 1.1 brezak }
579 1.17 cgd vndthrottle(vnd, vnd->sc_vp);
580 1.17 cgd vio->vnd_size = dbtob(vnd->sc_size);
581 1.17 cgd vnd->sc_flags |= VNF_INITED;
582 1.17 cgd #ifdef DEBUG
583 1.17 cgd if (vnddebug & VDB_INIT)
584 1.30 christos printf("vndioctl: SET vp %p size %lx\n",
585 1.28 christos vnd->sc_vp, (unsigned long) vnd->sc_size);
586 1.1 brezak #endif
587 1.22 thorpej
588 1.23 thorpej /* Attach the disk. */
589 1.23 thorpej bzero(vnd->sc_xname, sizeof(vnd->sc_xname)); /* XXX */
590 1.30 christos sprintf(vnd->sc_xname, "vnd%d", unit); /* XXX */
591 1.23 thorpej vnd->sc_dkdev.dk_name = vnd->sc_xname;
592 1.23 thorpej disk_attach(&vnd->sc_dkdev);
593 1.23 thorpej
594 1.22 thorpej vndunlock(vnd);
595 1.22 thorpej
596 1.1 brezak break;
597 1.1 brezak
598 1.17 cgd case VNDIOCCLR:
599 1.17 cgd if ((vnd->sc_flags & VNF_INITED) == 0)
600 1.22 thorpej return (ENXIO);
601 1.22 thorpej
602 1.24 christos if ((error = vndlock(vnd)) != 0)
603 1.22 thorpej return (error);
604 1.22 thorpej
605 1.22 thorpej /*
606 1.22 thorpej * Don't unconfigure if any other partitions are open
607 1.22 thorpej * or if both the character and block flavors of this
608 1.22 thorpej * partition are open.
609 1.22 thorpej */
610 1.22 thorpej part = DISKPART(dev);
611 1.22 thorpej pmask = (1 << part);
612 1.22 thorpej if ((vnd->sc_dkdev.dk_openmask & ~pmask) ||
613 1.22 thorpej ((vnd->sc_dkdev.dk_bopenmask & pmask) &&
614 1.22 thorpej (vnd->sc_dkdev.dk_copenmask & pmask))) {
615 1.22 thorpej vndunlock(vnd);
616 1.22 thorpej return (EBUSY);
617 1.22 thorpej }
618 1.22 thorpej
619 1.17 cgd vndclear(vnd);
620 1.1 brezak #ifdef DEBUG
621 1.17 cgd if (vnddebug & VDB_INIT)
622 1.30 christos printf("vndioctl: CLRed\n");
623 1.1 brezak #endif
624 1.23 thorpej
625 1.23 thorpej /* Detatch the disk. */
626 1.24 christos disk_detach(&vnd->sc_dkdev);
627 1.22 thorpej
628 1.22 thorpej vndunlock(vnd);
629 1.22 thorpej
630 1.1 brezak break;
631 1.1 brezak
632 1.22 thorpej /*
633 1.22 thorpej * XXX Should support disklabels.
634 1.22 thorpej */
635 1.22 thorpej
636 1.1 brezak default:
637 1.12 cgd return(ENOTTY);
638 1.1 brezak }
639 1.22 thorpej
640 1.22 thorpej return (0);
641 1.1 brezak }
642 1.1 brezak
643 1.1 brezak /*
644 1.1 brezak * Duplicate the current processes' credentials. Since we are called only
645 1.1 brezak * as the result of a SET ioctl and only root can do that, any future access
646 1.1 brezak * to this "disk" is essentially as root. Note that credentials may change
647 1.1 brezak * if some other uid can write directly to the mapped file (NFS).
648 1.1 brezak */
649 1.16 cgd int
650 1.17 cgd vndsetcred(vnd, cred)
651 1.17 cgd register struct vnd_softc *vnd;
652 1.1 brezak struct ucred *cred;
653 1.1 brezak {
654 1.1 brezak struct uio auio;
655 1.1 brezak struct iovec aiov;
656 1.5 cgd char *tmpbuf;
657 1.5 cgd int error;
658 1.1 brezak
659 1.17 cgd vnd->sc_cred = crdup(cred);
660 1.5 cgd tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
661 1.5 cgd
662 1.1 brezak /* XXX: Horrible kludge to establish credentials for NFS */
663 1.1 brezak aiov.iov_base = tmpbuf;
664 1.17 cgd aiov.iov_len = min(DEV_BSIZE, dbtob(vnd->sc_size));
665 1.1 brezak auio.uio_iov = &aiov;
666 1.1 brezak auio.uio_iovcnt = 1;
667 1.1 brezak auio.uio_offset = 0;
668 1.1 brezak auio.uio_rw = UIO_READ;
669 1.1 brezak auio.uio_segflg = UIO_SYSSPACE;
670 1.1 brezak auio.uio_resid = aiov.iov_len;
671 1.21 mycroft VOP_LOCK(vnd->sc_vp);
672 1.17 cgd error = VOP_READ(vnd->sc_vp, &auio, 0, vnd->sc_cred);
673 1.21 mycroft VOP_UNLOCK(vnd->sc_vp);
674 1.5 cgd
675 1.5 cgd free(tmpbuf, M_TEMP);
676 1.5 cgd return (error);
677 1.1 brezak }
678 1.1 brezak
679 1.1 brezak /*
680 1.1 brezak * Set maxactive based on FS type
681 1.1 brezak */
682 1.16 cgd void
683 1.17 cgd vndthrottle(vnd, vp)
684 1.17 cgd register struct vnd_softc *vnd;
685 1.1 brezak struct vnode *vp;
686 1.1 brezak {
687 1.31 thorpej #ifdef NFS
688 1.24 christos extern int (**nfsv2_vnodeop_p) __P((void *));
689 1.5 cgd
690 1.2 brezak if (vp->v_op == nfsv2_vnodeop_p)
691 1.17 cgd vnd->sc_maxactive = 2;
692 1.1 brezak else
693 1.1 brezak #endif
694 1.17 cgd vnd->sc_maxactive = 8;
695 1.1 brezak
696 1.17 cgd if (vnd->sc_maxactive < 1)
697 1.17 cgd vnd->sc_maxactive = 1;
698 1.1 brezak }
699 1.1 brezak
700 1.16 cgd void
701 1.17 cgd vndshutdown()
702 1.1 brezak {
703 1.17 cgd register struct vnd_softc *vnd;
704 1.1 brezak
705 1.17 cgd for (vnd = &vnd_softc[0]; vnd < &vnd_softc[numvnd]; vnd++)
706 1.17 cgd if (vnd->sc_flags & VNF_INITED)
707 1.17 cgd vndclear(vnd);
708 1.1 brezak }
709 1.1 brezak
710 1.16 cgd void
711 1.17 cgd vndclear(vnd)
712 1.17 cgd register struct vnd_softc *vnd;
713 1.1 brezak {
714 1.17 cgd register struct vnode *vp = vnd->sc_vp;
715 1.1 brezak struct proc *p = curproc; /* XXX */
716 1.1 brezak
717 1.1 brezak #ifdef DEBUG
718 1.17 cgd if (vnddebug & VDB_FOLLOW)
719 1.30 christos printf("vndclear(%p): vp %p\n", vnd, vp);
720 1.1 brezak #endif
721 1.17 cgd vnd->sc_flags &= ~VNF_INITED;
722 1.1 brezak if (vp == (struct vnode *)0)
723 1.17 cgd panic("vndioctl: null vp");
724 1.17 cgd (void) vn_close(vp, FREAD|FWRITE, vnd->sc_cred, p);
725 1.17 cgd crfree(vnd->sc_cred);
726 1.17 cgd vnd->sc_vp = (struct vnode *)0;
727 1.17 cgd vnd->sc_cred = (struct ucred *)0;
728 1.17 cgd vnd->sc_size = 0;
729 1.1 brezak }
730 1.1 brezak
731 1.16 cgd int
732 1.17 cgd vndsize(dev)
733 1.1 brezak dev_t dev;
734 1.1 brezak {
735 1.17 cgd int unit = vndunit(dev);
736 1.17 cgd register struct vnd_softc *vnd = &vnd_softc[unit];
737 1.1 brezak
738 1.17 cgd if (unit >= numvnd || (vnd->sc_flags & VNF_INITED) == 0)
739 1.1 brezak return(-1);
740 1.17 cgd return(vnd->sc_size);
741 1.1 brezak }
742 1.1 brezak
743 1.16 cgd int
744 1.19 cgd vnddump(dev, blkno, va, size)
745 1.16 cgd dev_t dev;
746 1.19 cgd daddr_t blkno;
747 1.19 cgd caddr_t va;
748 1.19 cgd size_t size;
749 1.1 brezak {
750 1.16 cgd
751 1.19 cgd /* Not implemented. */
752 1.19 cgd return ENXIO;
753 1.1 brezak }
754 1.22 thorpej
755 1.22 thorpej /*
756 1.22 thorpej * Wait interruptibly for an exclusive lock.
757 1.22 thorpej *
758 1.22 thorpej * XXX
759 1.22 thorpej * Several drivers do this; it should be abstracted and made MP-safe.
760 1.22 thorpej */
761 1.22 thorpej static int
762 1.22 thorpej vndlock(sc)
763 1.22 thorpej struct vnd_softc *sc;
764 1.22 thorpej {
765 1.22 thorpej int error;
766 1.22 thorpej
767 1.22 thorpej while ((sc->sc_flags & VNF_LOCKED) != 0) {
768 1.22 thorpej sc->sc_flags |= VNF_WANTED;
769 1.22 thorpej if ((error = tsleep(sc, PRIBIO | PCATCH, "vndlck", 0)) != 0)
770 1.22 thorpej return (error);
771 1.22 thorpej }
772 1.22 thorpej sc->sc_flags |= VNF_LOCKED;
773 1.22 thorpej return (0);
774 1.22 thorpej }
775 1.22 thorpej
776 1.22 thorpej /*
777 1.22 thorpej * Unlock and wake up any waiters.
778 1.22 thorpej */
779 1.22 thorpej static void
780 1.22 thorpej vndunlock(sc)
781 1.22 thorpej struct vnd_softc *sc;
782 1.22 thorpej {
783 1.22 thorpej
784 1.22 thorpej sc->sc_flags &= ~VNF_LOCKED;
785 1.22 thorpej if ((sc->sc_flags & VNF_WANTED) != 0) {
786 1.22 thorpej sc->sc_flags &= ~VNF_WANTED;
787 1.22 thorpej wakeup(sc);
788 1.22 thorpej }
789 1.22 thorpej }
790