xref: /original-bsd/sys/miscfs/specfs/spec_vnops.c (revision 1d37a0a0)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)spec_vnops.c	7.42 (Berkeley) 02/04/92
8  */
9 
10 #include <sys/param.h>
11 #include <sys/proc.h>
12 #include <sys/systm.h>
13 #include <sys/kernel.h>
14 #include <sys/conf.h>
15 #include <sys/buf.h>
16 #include <sys/mount.h>
17 #include <sys/namei.h>
18 #include <sys/vnode.h>
19 #include <sys/specdev.h>
20 #include <sys/stat.h>
21 #include <sys/errno.h>
22 #include <sys/ioctl.h>
23 #include <sys/file.h>
24 #include <sys/disklabel.h>
25 
26 /* symbolic sleep message strings for devices */
27 char	devopn[] = "devopn";
28 char	devio[] = "devio";
29 char	devwait[] = "devwait";
30 char	devin[] = "devin";
31 char	devout[] = "devout";
32 char	devioc[] = "devioc";
33 char	devcls[] = "devcls";
34 
35 struct vnodeops spec_vnodeops = {
36 	spec_lookup,		/* lookup */
37 	spec_create,		/* create */
38 	spec_mknod,		/* mknod */
39 	spec_open,		/* open */
40 	spec_close,		/* close */
41 	spec_access,		/* access */
42 	spec_getattr,		/* getattr */
43 	spec_setattr,		/* setattr */
44 	spec_read,		/* read */
45 	spec_write,		/* write */
46 	spec_ioctl,		/* ioctl */
47 	spec_select,		/* select */
48 	spec_mmap,		/* mmap */
49 	spec_fsync,		/* fsync */
50 	spec_seek,		/* seek */
51 	spec_remove,		/* remove */
52 	spec_link,		/* link */
53 	spec_rename,		/* rename */
54 	spec_mkdir,		/* mkdir */
55 	spec_rmdir,		/* rmdir */
56 	spec_symlink,		/* symlink */
57 	spec_readdir,		/* readdir */
58 	spec_readlink,		/* readlink */
59 	spec_abortop,		/* abortop */
60 	spec_inactive,		/* inactive */
61 	spec_reclaim,		/* reclaim */
62 	spec_lock,		/* lock */
63 	spec_unlock,		/* unlock */
64 	spec_bmap,		/* bmap */
65 	spec_strategy,		/* strategy */
66 	spec_print,		/* print */
67 	spec_islocked,		/* islocked */
68 	spec_advlock,		/* advlock */
69 	spec_blkatoff,		/* blkatoff */
70 	spec_vget,		/* vget */
71 	spec_valloc,		/* valloc */
72 	spec_vfree,		/* vfree */
73 	spec_truncate,		/* truncate */
74 	spec_update,		/* update */
75 	spec_bwrite,		/* bwrite */
76 };
77 
78 /*
79  * Trivial lookup routine that always fails.
80  */
81 int
82 spec_lookup(dvp, vpp, cnp)
83 	struct vnode *dvp;
84 	struct vnode **vpp;
85 	struct componentname *cnp;
86 {
87 
88 	*vpp = NULL;
89 	return (ENOTDIR);
90 }
91 
92 /*
93  * Open a special file: Don't allow open if fs is mounted -nodev,
94  * and don't allow opens of block devices that are currently mounted.
95  * Otherwise, call device driver open function.
96  */
97 /* ARGSUSED */
98 spec_open(vp, mode, cred, p)
99 	register struct vnode *vp;
100 	int mode;
101 	struct ucred *cred;
102 	struct proc *p;
103 {
104 	dev_t dev = (dev_t)vp->v_rdev;
105 	register int maj = major(dev);
106 	int error;
107 
108 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
109 		return (ENXIO);
110 
111 	switch (vp->v_type) {
112 
113 	case VCHR:
114 		if ((u_int)maj >= nchrdev)
115 			return (ENXIO);
116 		VOP_UNLOCK(vp);
117 		error = (*cdevsw[maj].d_open)(dev, mode, S_IFCHR, p);
118 		VOP_LOCK(vp);
119 		return (error);
120 
121 	case VBLK:
122 		if ((u_int)maj >= nblkdev)
123 			return (ENXIO);
124 		if (error = ufs_mountedon(vp))
125 			return (error);
126 		return ((*bdevsw[maj].d_open)(dev, mode, S_IFBLK, p));
127 	}
128 	return (0);
129 }
130 
131 /*
132  * Vnode op for read
133  */
134 /* ARGSUSED */
135 spec_read(vp, uio, ioflag, cred)
136 	register struct vnode *vp;
137 	register struct uio *uio;
138 	int ioflag;
139 	struct ucred *cred;
140 {
141 	struct proc *p = uio->uio_procp;
142 	struct buf *bp;
143 	daddr_t bn, nextbn;
144 	long bsize, bscale;
145 	struct partinfo dpart;
146 	register int n, on;
147 	int error = 0;
148 
149 #ifdef DIAGNOSTIC
150 	if (uio->uio_rw != UIO_READ)
151 		panic("spec_read mode");
152 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
153 		panic("spec_read proc");
154 #endif
155 	if (uio->uio_resid == 0)
156 		return (0);
157 
158 	switch (vp->v_type) {
159 
160 	case VCHR:
161 		VOP_UNLOCK(vp);
162 		error = (*cdevsw[major(vp->v_rdev)].d_read)
163 			(vp->v_rdev, uio, ioflag);
164 		VOP_LOCK(vp);
165 		return (error);
166 
167 	case VBLK:
168 		if (uio->uio_offset < 0)
169 			return (EINVAL);
170 		bsize = BLKDEV_IOSIZE;
171 		if ((*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, DIOCGPART,
172 		    (caddr_t)&dpart, FREAD, p) == 0) {
173 			if (dpart.part->p_fstype == FS_BSDFFS &&
174 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
175 				bsize = dpart.part->p_frag *
176 				    dpart.part->p_fsize;
177 		}
178 		bscale = bsize / DEV_BSIZE;
179 		do {
180 			bn = (uio->uio_offset / DEV_BSIZE) &~ (bscale - 1);
181 			on = uio->uio_offset % bsize;
182 			n = MIN((unsigned)(bsize - on), uio->uio_resid);
183 			if (vp->v_lastr + bscale == bn) {
184 				nextbn = bn + bscale;
185 				error = breadn(vp, bn, (int)bsize, &nextbn,
186 					(int *)&bsize, 1, NOCRED, &bp);
187 			} else
188 				error = bread(vp, bn, (int)bsize, NOCRED, &bp);
189 			vp->v_lastr = bn;
190 			n = MIN(n, bsize - bp->b_resid);
191 			if (error) {
192 				brelse(bp);
193 				return (error);
194 			}
195 			error = uiomove(bp->b_un.b_addr + on, n, uio);
196 			if (n + on == bsize)
197 				bp->b_flags |= B_AGE;
198 			brelse(bp);
199 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
200 		return (error);
201 
202 	default:
203 		panic("spec_read type");
204 	}
205 	/* NOTREACHED */
206 }
207 
208 /*
209  * Vnode op for write
210  */
211 /* ARGSUSED */
212 spec_write(vp, uio, ioflag, cred)
213 	register struct vnode *vp;
214 	register struct uio *uio;
215 	int ioflag;
216 	struct ucred *cred;
217 {
218 	struct proc *p = uio->uio_procp;
219 	struct buf *bp;
220 	daddr_t bn;
221 	int bsize, blkmask;
222 	struct partinfo dpart;
223 	register int n, on;
224 	int error = 0;
225 
226 #ifdef DIAGNOSTIC
227 	if (uio->uio_rw != UIO_WRITE)
228 		panic("spec_write mode");
229 	if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
230 		panic("spec_write proc");
231 #endif
232 
233 	switch (vp->v_type) {
234 
235 	case VCHR:
236 		VOP_UNLOCK(vp);
237 		error = (*cdevsw[major(vp->v_rdev)].d_write)
238 			(vp->v_rdev, uio, ioflag);
239 		VOP_LOCK(vp);
240 		return (error);
241 
242 	case VBLK:
243 		if (uio->uio_resid == 0)
244 			return (0);
245 		if (uio->uio_offset < 0)
246 			return (EINVAL);
247 		bsize = BLKDEV_IOSIZE;
248 		if ((*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, DIOCGPART,
249 		    (caddr_t)&dpart, FREAD, p) == 0) {
250 			if (dpart.part->p_fstype == FS_BSDFFS &&
251 			    dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
252 				bsize = dpart.part->p_frag *
253 				    dpart.part->p_fsize;
254 		}
255 		blkmask = (bsize / DEV_BSIZE) - 1;
256 		do {
257 			bn = (uio->uio_offset / DEV_BSIZE) &~ blkmask;
258 			on = uio->uio_offset % bsize;
259 			n = MIN((unsigned)(bsize - on), uio->uio_resid);
260 			if (n == bsize)
261 				bp = getblk(vp, bn, bsize);
262 			else
263 				error = bread(vp, bn, bsize, NOCRED, &bp);
264 			n = MIN(n, bsize - bp->b_resid);
265 			if (error) {
266 				brelse(bp);
267 				return (error);
268 			}
269 			error = uiomove(bp->b_un.b_addr + on, n, uio);
270 			if (n + on == bsize) {
271 				bp->b_flags |= B_AGE;
272 				bawrite(bp);
273 			} else
274 				bdwrite(bp);
275 		} while (error == 0 && uio->uio_resid > 0 && n != 0);
276 		return (error);
277 
278 	default:
279 		panic("spec_write type");
280 	}
281 	/* NOTREACHED */
282 }
283 
284 /*
285  * Device ioctl operation.
286  */
287 /* ARGSUSED */
288 spec_ioctl(vp, com, data, fflag, cred, p)
289 	struct vnode *vp;
290 	int com;
291 	caddr_t data;
292 	int fflag;
293 	struct ucred *cred;
294 	struct proc *p;
295 {
296 	dev_t dev = vp->v_rdev;
297 
298 	switch (vp->v_type) {
299 
300 	case VCHR:
301 		return ((*cdevsw[major(dev)].d_ioctl)(dev, com, data,
302 		    fflag, p));
303 
304 	case VBLK:
305 		if (com == 0 && (int)data == B_TAPE)
306 			if (bdevsw[major(dev)].d_flags & B_TAPE)
307 				return (0);
308 			else
309 				return (1);
310 		return ((*bdevsw[major(dev)].d_ioctl)(dev, com, data,
311 		   fflag, p));
312 
313 	default:
314 		panic("spec_ioctl");
315 		/* NOTREACHED */
316 	}
317 }
318 
319 /* ARGSUSED */
320 spec_select(vp, which, fflags, cred, p)
321 	struct vnode *vp;
322 	int which, fflags;
323 	struct ucred *cred;
324 	struct proc *p;
325 {
326 	register dev_t dev;
327 
328 	switch (vp->v_type) {
329 
330 	default:
331 		return (1);		/* XXX */
332 
333 	case VCHR:
334 		dev = vp->v_rdev;
335 		return (*cdevsw[major(dev)].d_select)(dev, which, p);
336 	}
337 }
338 
339 /*
340  * Just call the device strategy routine
341  */
342 spec_strategy(bp)
343 	register struct buf *bp;
344 {
345 
346 	(*bdevsw[major(bp->b_dev)].d_strategy)(bp);
347 	return (0);
348 }
349 
350 /*
351  * This is a noop, simply returning what one has been given.
352  */
353 spec_bmap(vp, bn, vpp, bnp)
354 	struct vnode *vp;
355 	daddr_t bn;
356 	struct vnode **vpp;
357 	daddr_t *bnp;
358 {
359 
360 	if (vpp != NULL)
361 		*vpp = vp;
362 	if (bnp != NULL)
363 		*bnp = bn;
364 	return (0);
365 }
366 
367 /*
368  * At the moment we do not do any locking.
369  */
370 /* ARGSUSED */
371 spec_lock(vp)
372 	struct vnode *vp;
373 {
374 
375 	return (0);
376 }
377 
378 /* ARGSUSED */
379 spec_unlock(vp)
380 	struct vnode *vp;
381 {
382 
383 	return (0);
384 }
385 
386 /*
387  * Device close routine
388  */
389 /* ARGSUSED */
390 spec_close(vp, flag, cred, p)
391 	register struct vnode *vp;
392 	int flag;
393 	struct ucred *cred;
394 	struct proc *p;
395 {
396 	dev_t dev = vp->v_rdev;
397 	int (*devclose) __P((dev_t, int, int, struct proc *));
398 	int mode;
399 
400 	switch (vp->v_type) {
401 
402 	case VCHR:
403 		/*
404 		 * If the vnode is locked, then we are in the midst
405 		 * of forcably closing the device, otherwise we only
406 		 * close on last reference.
407 		 */
408 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
409 			return (0);
410 		devclose = cdevsw[major(dev)].d_close;
411 		mode = S_IFCHR;
412 		break;
413 
414 	case VBLK:
415 		/*
416 		 * On last close of a block device (that isn't mounted)
417 		 * we must invalidate any in core blocks, so that
418 		 * we can, for instance, change floppy disks.
419 		 */
420 		vflushbuf(vp, 0);
421 		if (vinvalbuf(vp, 1))
422 			return (0);
423 		/*
424 		 * We do not want to really close the device if it
425 		 * is still in use unless we are trying to close it
426 		 * forcibly. Since every use (buffer, vnode, swap, cmap)
427 		 * holds a reference to the vnode, and because we mark
428 		 * any other vnodes that alias this device, when the
429 		 * sum of the reference counts on all the aliased
430 		 * vnodes descends to one, we are on last close.
431 		 */
432 		if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
433 			return (0);
434 		devclose = bdevsw[major(dev)].d_close;
435 		mode = S_IFBLK;
436 		break;
437 
438 	default:
439 		panic("spec_close: not special");
440 	}
441 
442 	return ((*devclose)(dev, flag, mode, p));
443 }
444 
445 /*
446  * Print out the contents of a special device vnode.
447  */
448 spec_print(vp)
449 	struct vnode *vp;
450 {
451 
452 	printf("tag VT_NON, dev %d, %d\n", major(vp->v_rdev),
453 		minor(vp->v_rdev));
454 }
455 
456 /*
457  * Special device advisory byte-level locks.
458  */
459 /* ARGSUSED */
460 spec_advlock(vp, id, op, fl, flags)
461 	struct vnode *vp;
462 	caddr_t id;
463 	int op;
464 	struct flock *fl;
465 	int flags;
466 {
467 
468 	return (EOPNOTSUPP);
469 }
470 
471 /*
472  * Special device failed operation
473  */
474 spec_ebadf()
475 {
476 
477 	return (EBADF);
478 }
479 
480 /*
481  * Special device bad operation
482  */
483 spec_badop()
484 {
485 
486 	panic("spec_badop called");
487 	/* NOTREACHED */
488 }
489