vnd.c revision 1.5 1 /*
2 * Copyright (c) 1988 University of Utah.
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Systems Programming Group of the University of Utah Computer
8 * Science Department.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: Utah $Hdr: vn.c 1.13 94/04/02$
39 *
40 * @(#)vn.c 8.6 (Berkeley) 4/1/94
41 */
42
43 /*
44 * Vnode disk driver.
45 *
46 * Block/character interface to a vnode. Allows one to treat a file
47 * as a disk (e.g. build a filesystem in it, mount it, etc.).
48 *
49 * NOTE 1: This uses the VOP_BMAP/VOP_STRATEGY interface to the vnode
50 * instead of a simple VOP_RDWR. We do this to avoid distorting the
51 * local buffer cache.
52 *
53 * NOTE 2: There is a security issue involved with this driver.
54 * Once mounted all access to the contents of the "mapped" file via
55 * the special file is controlled by the permissions on the special
56 * file, the protection of the mapped file is ignored (effectively,
57 * by using root credentials in all transactions).
58 *
59 * NOTE 3: Doesn't interact with leases, should it?
60 */
61 #include "vn.h"
62 #if NVN > 0
63
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/namei.h>
67 #include <sys/proc.h>
68 #include <sys/errno.h>
69 #include <sys/dkstat.h>
70 #include <sys/buf.h>
71 #include <sys/malloc.h>
72 #include <sys/ioctl.h>
73 #include <sys/mount.h>
74 #include <sys/vnode.h>
75 #include <sys/file.h>
76 #include <sys/uio.h>
77
78 #include <miscfs/specfs/specdev.h>
79
80 #include <dev/vnioctl.h>
81
82 #ifdef DEBUG
83 int dovncluster = 1;
84 int vndebug = 0x00;
85 #define VDB_FOLLOW 0x01
86 #define VDB_INIT 0x02
87 #define VDB_IO 0x04
88 #endif
89
90 #define b_cylin b_resid
91
92 #define vnunit(x) ((minor(x) >> 3) & 0x7) /* for consistency */
93
94 #define getvnbuf() \
95 ((struct buf *)malloc(sizeof(struct buf), M_DEVBUF, M_WAITOK))
96 #define putvnbuf(bp) \
97 free((caddr_t)(bp), M_DEVBUF)
98
99 struct vn_softc {
100 int sc_flags; /* flags */
101 size_t sc_size; /* size of vn */
102 struct vnode *sc_vp; /* vnode */
103 struct ucred *sc_cred; /* credentials */
104 int sc_maxactive; /* max # of active requests */
105 struct buf sc_tab; /* transfer queue */
106 };
107
108 /* sc_flags */
109 #define VNF_ALIVE 0x01
110 #define VNF_INITED 0x02
111
112 #if 0 /* if you need static allocation */
113 struct vn_softc vn_softc[NVN];
114 int numvnd = NVN;
115 #else
116 struct vn_softc *vn_softc;
117 int numvnd;
118 #endif
119
120 void
121 vnattach(num)
122 int num;
123 {
124 char *mem;
125 register u_long size;
126
127 if (num <= 0)
128 return;
129 size = num * sizeof(struct vn_softc);
130 mem = malloc(size, M_DEVBUF, M_NOWAIT);
131 if (mem == NULL) {
132 printf("WARNING: no memory for vnode disks\n");
133 return;
134 }
135 bzero(mem, size);
136 vn_softc = (struct vn_softc *)mem;
137 numvnd = num;
138 }
139
140 int
141 vnopen(dev, flags, mode, p)
142 dev_t dev;
143 int flags, mode;
144 struct proc *p;
145 {
146 int unit = vnunit(dev);
147
148 #ifdef DEBUG
149 if (vndebug & VDB_FOLLOW)
150 printf("vnopen(%x, %x, %x, %x)\n", dev, flags, mode, p);
151 #endif
152 if (unit >= numvnd)
153 return(ENXIO);
154 return(0);
155 }
156
157 int
158 vnclose(dev, flags, mode, p)
159 dev_t dev;
160 int flags, mode;
161 struct proc *p;
162 {
163 #ifdef DEBUG
164 if (vndebug & VDB_FOLLOW)
165 printf("vnclose(%x, %x, %x, %x)\n", dev, flags, mode, p);
166 #endif
167 return 0;
168 }
169
170 /*
171 * Break the request into bsize pieces and submit using VOP_BMAP/VOP_STRATEGY.
172 * Note that this driver can only be used for swapping over NFS on the hp
173 * since nfs_strategy on the vax cannot handle u-areas and page tables.
174 */
175 vnstrategy(bp)
176 register struct buf *bp;
177 {
178 int unit = vnunit(bp->b_dev);
179 register struct vn_softc *vn = &vn_softc[unit];
180 register struct buf *nbp;
181 register int bn, bsize, resid;
182 register caddr_t addr;
183 int sz, flags, error;
184 extern void vniodone();
185
186 #ifdef DEBUG
187 if (vndebug & VDB_FOLLOW)
188 printf("vnstrategy(%x): unit %d\n", bp, unit);
189 #endif
190 if ((vn->sc_flags & VNF_INITED) == 0) {
191 bp->b_error = ENXIO;
192 bp->b_flags |= B_ERROR;
193 biodone(bp);
194 return;
195 }
196 bn = bp->b_blkno;
197 sz = howmany(bp->b_bcount, DEV_BSIZE);
198 bp->b_resid = bp->b_bcount;
199 if (bn < 0 || bn + sz > vn->sc_size) {
200 if (bn != vn->sc_size) {
201 bp->b_error = EINVAL;
202 bp->b_flags |= B_ERROR;
203 }
204 biodone(bp);
205 return;
206 }
207 bn = dbtob(bn);
208 bsize = vn->sc_vp->v_mount->mnt_stat.f_iosize;
209 addr = bp->b_data;
210 flags = bp->b_flags | B_CALL;
211 for (resid = bp->b_resid; resid; resid -= sz) {
212 struct vnode *vp;
213 daddr_t nbn;
214 int off, s, nra;
215
216 nra = 0;
217 #if (BSD > 199103)
218 error = VOP_BMAP(vn->sc_vp, bn / bsize, &vp, &nbn, &nra);
219 #else
220 error = VOP_BMAP(vn->sc_vp, bn / bsize, &vp, &nbn);
221 #endif
222 if (error == 0 && (long)nbn == -1)
223 error = EIO;
224 #ifdef DEBUG
225 if (!dovncluster)
226 nra = 0;
227 #endif
228
229 if (off = bn % bsize)
230 sz = bsize - off;
231 else
232 sz = (1 + nra) * bsize;
233 if (resid < sz)
234 sz = resid;
235 #ifdef DEBUG
236 if (vndebug & VDB_IO)
237 printf("vnstrategy: vp %x/%x bn %x/%x sz %x\n",
238 vn->sc_vp, vp, bn, nbn, sz);
239 #endif
240
241 nbp = getvnbuf();
242 nbp->b_flags = flags;
243 nbp->b_bcount = sz;
244 nbp->b_bufsize = bp->b_bufsize;
245 nbp->b_error = 0;
246 if (vp->v_type == VBLK || vp->v_type == VCHR)
247 nbp->b_dev = vp->v_rdev;
248 else
249 nbp->b_dev = NODEV;
250 nbp->b_data = addr;
251 nbp->b_blkno = nbn + btodb(off);
252 nbp->b_proc = bp->b_proc;
253 nbp->b_iodone = vniodone;
254 nbp->b_vp = vp;
255 nbp->b_pfcent = (int) bp; /* XXX */
256 nbp->b_rcred = vn->sc_cred; /* XXX crdup? */
257 nbp->b_wcred = vn->sc_cred; /* XXX crdup? */
258 nbp->b_dirtyoff = bp->b_dirtyoff;
259 nbp->b_dirtyend = bp->b_dirtyend;
260 nbp->b_validoff = bp->b_validoff;
261 nbp->b_validend = bp->b_validend;
262 /*
263 * If there was an error or a hole in the file...punt.
264 * Note that we deal with this after the nbp allocation.
265 * This ensures that we properly clean up any operations
266 * that we have already fired off.
267 *
268 * XXX we could deal with holes here but it would be
269 * a hassle (in the write case).
270 */
271 if (error) {
272 nbp->b_error = error;
273 nbp->b_flags |= B_ERROR;
274 bp->b_resid -= (resid - sz);
275 biodone(nbp);
276 return;
277 }
278 /*
279 * Just sort by block number
280 */
281 nbp->b_cylin = nbp->b_blkno;
282 s = splbio();
283 disksort(&vn->sc_tab, nbp);
284 if (vn->sc_tab.b_active < vn->sc_maxactive) {
285 vn->sc_tab.b_active++;
286 vnstart(vn);
287 }
288 splx(s);
289 bn += sz;
290 addr += sz;
291 }
292 }
293
294 /*
295 * Feed requests sequentially.
296 * We do it this way to keep from flooding NFS servers if we are connected
297 * to an NFS file. This places the burden on the client rather than the
298 * server.
299 */
300 vnstart(vn)
301 register struct vn_softc *vn;
302 {
303 register struct buf *bp;
304
305 /*
306 * Dequeue now since lower level strategy routine might
307 * queue using same links
308 */
309 bp = vn->sc_tab.b_actf;
310 vn->sc_tab.b_actf = bp->b_actf;
311 #ifdef DEBUG
312 if (vndebug & VDB_IO)
313 printf("vnstart(%d): bp %x vp %x blkno %x addr %x cnt %x\n",
314 vn-vn_softc, bp, bp->b_vp, bp->b_blkno, bp->b_data,
315 bp->b_bcount);
316 #endif
317 if ((bp->b_flags & B_READ) == 0)
318 bp->b_vp->v_numoutput++;
319 VOP_STRATEGY(bp);
320 }
321
322 void
323 vniodone(bp)
324 register struct buf *bp;
325 {
326 register struct buf *pbp = (struct buf *)bp->b_pfcent; /* XXX */
327 register struct vn_softc *vn = &vn_softc[vnunit(pbp->b_dev)];
328 int s;
329
330 s = splbio();
331 #ifdef DEBUG
332 if (vndebug & VDB_IO)
333 printf("vniodone(%d): bp %x vp %x blkno %x addr %x cnt %x\n",
334 vn-vn_softc, bp, bp->b_vp, bp->b_blkno, bp->b_data,
335 bp->b_bcount);
336 #endif
337 if (bp->b_error) {
338 #ifdef DEBUG
339 if (vndebug & VDB_IO)
340 printf("vniodone: bp %x error %d\n", bp, bp->b_error);
341 #endif
342 pbp->b_flags |= B_ERROR;
343 pbp->b_error = biowait(bp);
344 }
345 pbp->b_resid -= bp->b_bcount;
346 putvnbuf(bp);
347 if (pbp->b_resid == 0) {
348 #ifdef DEBUG
349 if (vndebug & VDB_IO)
350 printf("vniodone: pbp %x iodone\n", pbp);
351 #endif
352 biodone(pbp);
353 }
354 if (vn->sc_tab.b_actf)
355 vnstart(vn);
356 else
357 vn->sc_tab.b_active--;
358 splx(s);
359 }
360
361 vnread(dev, uio, flags, p)
362 dev_t dev;
363 struct uio *uio;
364 int flags;
365 struct proc *p;
366 {
367
368 #ifdef DEBUG
369 if (vndebug & VDB_FOLLOW)
370 printf("vnread(%x, %x, %x, %x)\n", dev, uio, flags, p);
371 #endif
372 return(physio(vnstrategy, NULL, dev, B_READ, minphys, uio));
373 }
374
375 vnwrite(dev, uio, flags, p)
376 dev_t dev;
377 struct uio *uio;
378 int flags;
379 struct proc *p;
380 {
381
382 #ifdef DEBUG
383 if (vndebug & VDB_FOLLOW)
384 printf("vnwrite(%x, %x, %x, %x)\n", dev, uio, flags, p);
385 #endif
386 return(physio(vnstrategy, NULL, dev, B_WRITE, minphys, uio));
387 }
388
389 /* ARGSUSED */
390 vnioctl(dev, cmd, data, flag, p)
391 dev_t dev;
392 u_long cmd;
393 caddr_t data;
394 int flag;
395 struct proc *p;
396 {
397 int unit = vnunit(dev);
398 register struct vn_softc *vn;
399 struct vn_ioctl *vio;
400 struct vattr vattr;
401 struct nameidata nd;
402 int error;
403
404 #ifdef DEBUG
405 if (vndebug & VDB_FOLLOW)
406 printf("vnioctl(%x, %x, %x, %x, %x): unit %d\n",
407 dev, cmd, data, flag, p, unit);
408 #endif
409 error = suser(p->p_ucred, &p->p_acflag);
410 if (error)
411 return (error);
412 if (unit >= numvnd)
413 return (ENXIO);
414
415 vn = &vn_softc[unit];
416 vio = (struct vn_ioctl *)data;
417 switch (cmd) {
418
419 case VNIOCSET:
420 if (vn->sc_flags & VNF_INITED)
421 return(EBUSY);
422 /*
423 * Always open for read and write.
424 * This is probably bogus, but it lets vn_open()
425 * weed out directories, sockets, etc. so we don't
426 * have to worry about them.
427 */
428 #if (BSD > 199103)
429 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vn_file, p);
430 if (error = vn_open(&nd, FREAD|FWRITE, 0))
431 return(error);
432 #else
433 nd.ni_nameiop = LOOKUP | FOLLOW;
434 nd.ni_segflg = UIO_USERSPACE;
435 nd.ni_dirp = vio->vn_file;
436 if (error = vn_open(&nd, p, FREAD|FWRITE, 0))
437 return(error);
438 #endif
439 if (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p)) {
440 VOP_UNLOCK(nd.ni_vp);
441 (void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
442 return(error);
443 }
444 VOP_UNLOCK(nd.ni_vp);
445 vn->sc_vp = nd.ni_vp;
446 vn->sc_size = btodb(vattr.va_size); /* note truncation */
447 if (error = vnsetcred(vn, p->p_ucred)) {
448 (void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
449 return(error);
450 }
451 vnthrottle(vn, vn->sc_vp);
452 vio->vn_size = dbtob(vn->sc_size);
453 vn->sc_flags |= VNF_INITED;
454 #ifdef DEBUG
455 if (vndebug & VDB_INIT)
456 printf("vnioctl: SET vp %x size %x\n",
457 vn->sc_vp, vn->sc_size);
458 #endif
459 break;
460
461 case VNIOCCLR:
462 if ((vn->sc_flags & VNF_INITED) == 0)
463 return(ENXIO);
464 vnclear(vn);
465 #ifdef DEBUG
466 if (vndebug & VDB_INIT)
467 printf("vnioctl: CLRed\n");
468 #endif
469 break;
470
471 default:
472 return(ENXIO);
473 }
474 return(0);
475 }
476
477 /*
478 * Duplicate the current processes' credentials. Since we are called only
479 * as the result of a SET ioctl and only root can do that, any future access
480 * to this "disk" is essentially as root. Note that credentials may change
481 * if some other uid can write directly to the mapped file (NFS).
482 */
483 vnsetcred(vn, cred)
484 register struct vn_softc *vn;
485 struct ucred *cred;
486 {
487 struct uio auio;
488 struct iovec aiov;
489 char *tmpbuf;
490 int error;
491
492 vn->sc_cred = crdup(cred);
493 tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
494
495 /* XXX: Horrible kludge to establish credentials for NFS */
496 aiov.iov_base = tmpbuf;
497 aiov.iov_len = min(DEV_BSIZE, dbtob(vn->sc_size));
498 auio.uio_iov = &aiov;
499 auio.uio_iovcnt = 1;
500 auio.uio_offset = 0;
501 auio.uio_rw = UIO_READ;
502 auio.uio_segflg = UIO_SYSSPACE;
503 auio.uio_resid = aiov.iov_len;
504 error = VOP_READ(vn->sc_vp, &auio, 0, vn->sc_cred);
505
506 free(tmpbuf, M_TEMP);
507 return (error);
508 }
509
510 /*
511 * Set maxactive based on FS type
512 */
513 vnthrottle(vn, vp)
514 register struct vn_softc *vn;
515 struct vnode *vp;
516 {
517 #ifdef NFSCLIENT
518 #if (BSD > 199103)
519 extern int (**nfsv2_vnodeop_p)();
520
521 if (vp->v_op == nfsv2_vnodeop_p)
522 #else
523 extern struct vnodeops nfsv2_vnodeops;
524
525 if (vp->v_op == &nfsv2_vnodeops)
526 #endif
527 vn->sc_maxactive = 2;
528 else
529 #endif
530 vn->sc_maxactive = 8;
531
532 if (vn->sc_maxactive < 1)
533 vn->sc_maxactive = 1;
534 }
535
536 vnshutdown()
537 {
538 register struct vn_softc *vn;
539
540 for (vn = &vn_softc[0]; vn < &vn_softc[numvnd]; vn++)
541 if (vn->sc_flags & VNF_INITED)
542 vnclear(vn);
543 }
544
545 vnclear(vn)
546 register struct vn_softc *vn;
547 {
548 register struct vnode *vp = vn->sc_vp;
549 struct proc *p = curproc; /* XXX */
550
551 #ifdef DEBUG
552 if (vndebug & VDB_FOLLOW)
553 printf("vnclear(%x): vp %x\n", vp);
554 #endif
555 vn->sc_flags &= ~VNF_INITED;
556 if (vp == (struct vnode *)0)
557 panic("vnioctl: null vp");
558 (void) vn_close(vp, FREAD|FWRITE, vn->sc_cred, p);
559 crfree(vn->sc_cred);
560 vn->sc_vp = (struct vnode *)0;
561 vn->sc_cred = (struct ucred *)0;
562 vn->sc_size = 0;
563 }
564
565 vnsize(dev)
566 dev_t dev;
567 {
568 int unit = vnunit(dev);
569 register struct vn_softc *vn = &vn_softc[unit];
570
571 if (unit >= numvnd || (vn->sc_flags & VNF_INITED) == 0)
572 return(-1);
573 return(vn->sc_size);
574 }
575
576 vndump(dev)
577 {
578 return(ENXIO);
579 }
580 #endif
581