xref: /netbsd-src/sys/dev/vnd.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
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 void
176 vnstrategy(bp)
177 	register struct buf *bp;
178 {
179 	int unit = vnunit(bp->b_dev);
180 	register struct vn_softc *vn = &vn_softc[unit];
181 	register struct buf *nbp;
182 	register int bn, bsize, resid;
183 	register caddr_t addr;
184 	int sz, flags, error;
185 	extern void vniodone();
186 
187 #ifdef DEBUG
188 	if (vndebug & VDB_FOLLOW)
189 		printf("vnstrategy(%x): unit %d\n", bp, unit);
190 #endif
191 	if ((vn->sc_flags & VNF_INITED) == 0) {
192 		bp->b_error = ENXIO;
193 		bp->b_flags |= B_ERROR;
194 		biodone(bp);
195 		return;
196 	}
197 	bn = bp->b_blkno;
198 	sz = howmany(bp->b_bcount, DEV_BSIZE);
199 	bp->b_resid = bp->b_bcount;
200 	if (bn < 0 || bn + sz > vn->sc_size) {
201 		if (bn != vn->sc_size) {
202 			bp->b_error = EINVAL;
203 			bp->b_flags |= B_ERROR;
204 		}
205 		biodone(bp);
206 		return;
207 	}
208 	bn = dbtob(bn);
209  	bsize = vn->sc_vp->v_mount->mnt_stat.f_iosize;
210 	addr = bp->b_data;
211 	flags = bp->b_flags | B_CALL;
212 	for (resid = bp->b_resid; resid; resid -= sz) {
213 		struct vnode *vp;
214 		daddr_t nbn;
215 		int off, s, nra;
216 
217 		nra = 0;
218 #if (BSD > 199103)
219 #warning if should go away now
220 		error = VOP_BMAP(vn->sc_vp, bn / bsize, &vp, &nbn, &nra);
221 #else
222 		error = VOP_BMAP(vn->sc_vp, bn / bsize, &vp, &nbn);
223 #endif
224 		if (error == 0 && (long)nbn == -1)
225 			error = EIO;
226 #ifdef DEBUG
227 		if (!dovncluster)
228 			nra = 0;
229 #endif
230 
231 		if (off = bn % bsize)
232 			sz = bsize - off;
233 		else
234 			sz = (1 + nra) * bsize;
235 		if (resid < sz)
236 			sz = resid;
237 #ifdef DEBUG
238 		if (vndebug & VDB_IO)
239 			printf("vnstrategy: vp %x/%x bn %x/%x sz %x\n",
240 			       vn->sc_vp, vp, bn, nbn, sz);
241 #endif
242 
243 		nbp = getvnbuf();
244 		nbp->b_flags = flags;
245 		nbp->b_bcount = sz;
246 		nbp->b_bufsize = bp->b_bufsize;
247 		nbp->b_error = 0;
248 		if (vp->v_type == VBLK || vp->v_type == VCHR)
249 			nbp->b_dev = vp->v_rdev;
250 		else
251 			nbp->b_dev = NODEV;
252 		nbp->b_data = addr;
253 		nbp->b_blkno = nbn + btodb(off);
254 		nbp->b_proc = bp->b_proc;
255 		nbp->b_iodone = vniodone;
256 		nbp->b_vp = vp;
257 		nbp->b_pfcent = (int) bp;	/* XXX */
258 		nbp->b_rcred = vn->sc_cred;	/* XXX crdup? */
259 		nbp->b_wcred = vn->sc_cred;	/* XXX crdup? */
260 		nbp->b_dirtyoff = bp->b_dirtyoff;
261 		nbp->b_dirtyend = bp->b_dirtyend;
262 		nbp->b_validoff = bp->b_validoff;
263 		nbp->b_validend = bp->b_validend;
264 		/*
265 		 * If there was an error or a hole in the file...punt.
266 		 * Note that we deal with this after the nbp allocation.
267 		 * This ensures that we properly clean up any operations
268 		 * that we have already fired off.
269 		 *
270 		 * XXX we could deal with holes here but it would be
271 		 * a hassle (in the write case).
272 		 */
273 		if (error) {
274 			nbp->b_error = error;
275 			nbp->b_flags |= B_ERROR;
276 			bp->b_resid -= (resid - sz);
277 			biodone(nbp);
278 			return;
279 		}
280 		/*
281 		 * Just sort by block number
282 		 */
283 		nbp->b_cylin = nbp->b_blkno;
284 		s = splbio();
285 		disksort(&vn->sc_tab, nbp);
286 		if (vn->sc_tab.b_active < vn->sc_maxactive) {
287 			vn->sc_tab.b_active++;
288 			vnstart(vn);
289 		}
290 		splx(s);
291 		bn += sz;
292 		addr += sz;
293 	}
294 }
295 
296 /*
297  * Feed requests sequentially.
298  * We do it this way to keep from flooding NFS servers if we are connected
299  * to an NFS file.  This places the burden on the client rather than the
300  * server.
301  */
302 vnstart(vn)
303 	register struct vn_softc *vn;
304 {
305 	register struct buf *bp;
306 
307 	/*
308 	 * Dequeue now since lower level strategy routine might
309 	 * queue using same links
310 	 */
311 	bp = vn->sc_tab.b_actf;
312 	vn->sc_tab.b_actf = bp->b_actf;
313 #ifdef DEBUG
314 	if (vndebug & VDB_IO)
315 		printf("vnstart(%d): bp %x vp %x blkno %x addr %x cnt %x\n",
316 		    vn-vn_softc, bp, bp->b_vp, bp->b_blkno, bp->b_data,
317 		    bp->b_bcount);
318 #endif
319 	if ((bp->b_flags & B_READ) == 0)
320 		bp->b_vp->v_numoutput++;
321 	VOP_STRATEGY(bp);
322 }
323 
324 void
325 vniodone(bp)
326 	register struct buf *bp;
327 {
328 	register struct buf *pbp = (struct buf *)bp->b_pfcent;	/* XXX */
329 	register struct vn_softc *vn = &vn_softc[vnunit(pbp->b_dev)];
330 	int s;
331 
332 	s = splbio();
333 #ifdef DEBUG
334 	if (vndebug & VDB_IO)
335 		printf("vniodone(%d): bp %x vp %x blkno %x addr %x cnt %x\n",
336 		    vn-vn_softc, bp, bp->b_vp, bp->b_blkno, bp->b_data,
337 		    bp->b_bcount);
338 #endif
339 	if (bp->b_error) {
340 #ifdef DEBUG
341 		if (vndebug & VDB_IO)
342 			printf("vniodone: bp %x error %d\n", bp, bp->b_error);
343 #endif
344 		pbp->b_flags |= B_ERROR;
345 		pbp->b_error = biowait(bp);
346 	}
347 	pbp->b_resid -= bp->b_bcount;
348 	putvnbuf(bp);
349 	if (pbp->b_resid == 0) {
350 #ifdef DEBUG
351 		if (vndebug & VDB_IO)
352 			printf("vniodone: pbp %x iodone\n", pbp);
353 #endif
354 		biodone(pbp);
355 	}
356 	if (vn->sc_tab.b_actf)
357 		vnstart(vn);
358 	else
359 		vn->sc_tab.b_active--;
360 	splx(s);
361 }
362 
363 /* ARGSUSED */
364 vnioctl(dev, cmd, data, flag, p)
365 	dev_t dev;
366 	u_long cmd;
367 	caddr_t data;
368 	int flag;
369 	struct proc *p;
370 {
371 	int unit = vnunit(dev);
372 	register struct vn_softc *vn;
373 	struct vn_ioctl *vio;
374 	struct vattr vattr;
375 	struct nameidata nd;
376 	int error;
377 
378 #ifdef DEBUG
379 	if (vndebug & VDB_FOLLOW)
380 		printf("vnioctl(%x, %x, %x, %x, %x): unit %d\n",
381 		    dev, cmd, data, flag, p, unit);
382 #endif
383 	error = suser(p->p_ucred, &p->p_acflag);
384 	if (error)
385 		return (error);
386 	if (unit >= numvnd)
387 		return (ENXIO);
388 
389 	vn = &vn_softc[unit];
390 	vio = (struct vn_ioctl *)data;
391 	switch (cmd) {
392 
393 	case VNIOCSET:
394 		if (vn->sc_flags & VNF_INITED)
395 			return(EBUSY);
396 		/*
397 		 * Always open for read and write.
398 		 * This is probably bogus, but it lets vn_open()
399 		 * weed out directories, sockets, etc. so we don't
400 		 * have to worry about them.
401 		 */
402 #if (BSD > 199103)
403 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, vio->vn_file, p);
404 		if (error = vn_open(&nd, FREAD|FWRITE, 0))
405 			return(error);
406 #else
407 		nd.ni_nameiop = LOOKUP | FOLLOW;
408 		nd.ni_segflg = UIO_USERSPACE;
409 		nd.ni_dirp = vio->vn_file;
410 		if (error = vn_open(&nd, p, FREAD|FWRITE, 0))
411 			return(error);
412 #endif
413 		if (error = VOP_GETATTR(nd.ni_vp, &vattr, p->p_ucred, p)) {
414 			VOP_UNLOCK(nd.ni_vp);
415 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
416 			return(error);
417 		}
418 		VOP_UNLOCK(nd.ni_vp);
419 		vn->sc_vp = nd.ni_vp;
420 		vn->sc_size = btodb(vattr.va_size);	/* note truncation */
421 		if (error = vnsetcred(vn, p->p_ucred)) {
422 			(void) vn_close(nd.ni_vp, FREAD|FWRITE, p->p_ucred, p);
423 			return(error);
424 		}
425 		vnthrottle(vn, vn->sc_vp);
426 		vio->vn_size = dbtob(vn->sc_size);
427 		vn->sc_flags |= VNF_INITED;
428 #ifdef DEBUG
429 		if (vndebug & VDB_INIT)
430 			printf("vnioctl: SET vp %x size %x\n",
431 			    vn->sc_vp, vn->sc_size);
432 #endif
433 		break;
434 
435 	case VNIOCCLR:
436 		if ((vn->sc_flags & VNF_INITED) == 0)
437 			return(ENXIO);
438 		vnclear(vn);
439 #ifdef DEBUG
440 		if (vndebug & VDB_INIT)
441 			printf("vnioctl: CLRed\n");
442 #endif
443 		break;
444 
445 	default:
446 		return(ENXIO);
447 	}
448 	return(0);
449 }
450 
451 /*
452  * Duplicate the current processes' credentials.  Since we are called only
453  * as the result of a SET ioctl and only root can do that, any future access
454  * to this "disk" is essentially as root.  Note that credentials may change
455  * if some other uid can write directly to the mapped file (NFS).
456  */
457 vnsetcred(vn, cred)
458 	register struct vn_softc *vn;
459 	struct ucred *cred;
460 {
461 	struct uio auio;
462 	struct iovec aiov;
463 	char *tmpbuf;
464 	int error;
465 
466 	vn->sc_cred = crdup(cred);
467 	tmpbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
468 
469 	/* XXX: Horrible kludge to establish credentials for NFS */
470 	aiov.iov_base = tmpbuf;
471 	aiov.iov_len = min(DEV_BSIZE, dbtob(vn->sc_size));
472 	auio.uio_iov = &aiov;
473 	auio.uio_iovcnt = 1;
474 	auio.uio_offset = 0;
475 	auio.uio_rw = UIO_READ;
476 	auio.uio_segflg = UIO_SYSSPACE;
477 	auio.uio_resid = aiov.iov_len;
478 	error = VOP_READ(vn->sc_vp, &auio, 0, vn->sc_cred);
479 
480 	free(tmpbuf, M_TEMP);
481 	return (error);
482 }
483 
484 /*
485  * Set maxactive based on FS type
486  */
487 vnthrottle(vn, vp)
488 	register struct vn_softc *vn;
489 	struct vnode *vp;
490 {
491 #ifdef NFSCLIENT
492 #if (BSD > 199103)
493 	extern int (**nfsv2_vnodeop_p)();
494 
495 	if (vp->v_op == nfsv2_vnodeop_p)
496 #else
497  	extern struct vnodeops nfsv2_vnodeops;
498 
499 	if (vp->v_op == &nfsv2_vnodeops)
500 #endif
501 		vn->sc_maxactive = 2;
502 	else
503 #endif
504 		vn->sc_maxactive = 8;
505 
506 	if (vn->sc_maxactive < 1)
507 		vn->sc_maxactive = 1;
508 }
509 
510 vnshutdown()
511 {
512 	register struct vn_softc *vn;
513 
514 	for (vn = &vn_softc[0]; vn < &vn_softc[numvnd]; vn++)
515 		if (vn->sc_flags & VNF_INITED)
516 			vnclear(vn);
517 }
518 
519 vnclear(vn)
520 	register struct vn_softc *vn;
521 {
522 	register struct vnode *vp = vn->sc_vp;
523 	struct proc *p = curproc;		/* XXX */
524 
525 #ifdef DEBUG
526 	if (vndebug & VDB_FOLLOW)
527 		printf("vnclear(%x): vp %x\n", vp);
528 #endif
529 	vn->sc_flags &= ~VNF_INITED;
530 	if (vp == (struct vnode *)0)
531 		panic("vnioctl: null vp");
532 	(void) vn_close(vp, FREAD|FWRITE, vn->sc_cred, p);
533 	crfree(vn->sc_cred);
534 	vn->sc_vp = (struct vnode *)0;
535 	vn->sc_cred = (struct ucred *)0;
536 	vn->sc_size = 0;
537 }
538 
539 vnsize(dev)
540 	dev_t dev;
541 {
542 	int unit = vnunit(dev);
543 	register struct vn_softc *vn = &vn_softc[unit];
544 
545 	if (unit >= numvnd || (vn->sc_flags & VNF_INITED) == 0)
546 		return(-1);
547 	return(vn->sc_size);
548 }
549 
550 vndump(dev)
551 {
552 	return(ENXIO);
553 }
554 #endif
555