spec_vnops.c revision 1.30 1 /* $NetBSD: spec_vnops.c,v 1.30 1996/09/01 23:48:28 mycroft Exp $ */
2
3 /*
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)spec_vnops.c 8.8 (Berkeley) 11/21/94
36 */
37
38 #include <sys/param.h>
39 #include <sys/proc.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/conf.h>
43 #include <sys/buf.h>
44 #include <sys/mount.h>
45 #include <sys/namei.h>
46 #include <sys/vnode.h>
47 #include <sys/stat.h>
48 #include <sys/errno.h>
49 #include <sys/ioctl.h>
50 #include <sys/file.h>
51 #include <sys/disklabel.h>
52
53 #include <miscfs/genfs/genfs.h>
54 #include <miscfs/specfs/specdev.h>
55
56 /* symbolic sleep message strings for devices */
57 char devopn[] = "devopn";
58 char devio[] = "devio";
59 char devwait[] = "devwait";
60 char devin[] = "devin";
61 char devout[] = "devout";
62 char devioc[] = "devioc";
63 char devcls[] = "devcls";
64
65 int (**spec_vnodeop_p) __P((void *));
66 struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
67 { &vop_default_desc, vn_default_error },
68 { &vop_lookup_desc, spec_lookup }, /* lookup */
69 { &vop_create_desc, spec_create }, /* create */
70 { &vop_mknod_desc, spec_mknod }, /* mknod */
71 { &vop_open_desc, spec_open }, /* open */
72 { &vop_close_desc, spec_close }, /* close */
73 { &vop_access_desc, spec_access }, /* access */
74 { &vop_getattr_desc, spec_getattr }, /* getattr */
75 { &vop_setattr_desc, spec_setattr }, /* setattr */
76 { &vop_read_desc, spec_read }, /* read */
77 { &vop_write_desc, spec_write }, /* write */
78 { &vop_lease_desc, spec_lease_check }, /* lease */
79 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
80 { &vop_select_desc, spec_select }, /* select */
81 { &vop_mmap_desc, spec_mmap }, /* mmap */
82 { &vop_fsync_desc, spec_fsync }, /* fsync */
83 { &vop_seek_desc, spec_seek }, /* seek */
84 { &vop_remove_desc, spec_remove }, /* remove */
85 { &vop_link_desc, spec_link }, /* link */
86 { &vop_rename_desc, spec_rename }, /* rename */
87 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
88 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
89 { &vop_symlink_desc, spec_symlink }, /* symlink */
90 { &vop_readdir_desc, spec_readdir }, /* readdir */
91 { &vop_readlink_desc, spec_readlink }, /* readlink */
92 { &vop_abortop_desc, spec_abortop }, /* abortop */
93 { &vop_inactive_desc, spec_inactive }, /* inactive */
94 { &vop_reclaim_desc, spec_reclaim }, /* reclaim */
95 { &vop_lock_desc, spec_lock }, /* lock */
96 { &vop_unlock_desc, spec_unlock }, /* unlock */
97 { &vop_bmap_desc, spec_bmap }, /* bmap */
98 { &vop_strategy_desc, spec_strategy }, /* strategy */
99 { &vop_print_desc, spec_print }, /* print */
100 { &vop_islocked_desc, spec_islocked }, /* islocked */
101 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
102 { &vop_advlock_desc, spec_advlock }, /* advlock */
103 { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
104 { &vop_valloc_desc, spec_valloc }, /* valloc */
105 { &vop_vfree_desc, spec_vfree }, /* vfree */
106 { &vop_truncate_desc, spec_truncate }, /* truncate */
107 { &vop_update_desc, spec_update }, /* update */
108 { &vop_bwrite_desc, spec_bwrite }, /* bwrite */
109 { (struct vnodeop_desc*)NULL, (int(*) __P((void *)))NULL }
110 };
111 struct vnodeopv_desc spec_vnodeop_opv_desc =
112 { &spec_vnodeop_p, spec_vnodeop_entries };
113
114 /*
115 * Trivial lookup routine that always fails.
116 */
117 int
118 spec_lookup(v)
119 void *v;
120 {
121 struct vop_lookup_args /* {
122 struct vnode *a_dvp;
123 struct vnode **a_vpp;
124 struct componentname *a_cnp;
125 } */ *ap = v;
126
127 *ap->a_vpp = NULL;
128 return (ENOTDIR);
129 }
130
131 /*
132 * Open a special file.
133 */
134 /* ARGSUSED */
135 int
136 spec_open(v)
137 void *v;
138 {
139 struct vop_open_args /* {
140 struct vnode *a_vp;
141 int a_mode;
142 struct ucred *a_cred;
143 struct proc *a_p;
144 } */ *ap = v;
145 struct vnode *bvp, *vp = ap->a_vp;
146 dev_t bdev, dev = (dev_t)vp->v_rdev;
147 register int maj = major(dev);
148 int error;
149
150 /*
151 * Don't allow open if fs is mounted -nodev.
152 */
153 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
154 return (ENXIO);
155
156 switch (vp->v_type) {
157
158 case VCHR:
159 if ((u_int)maj >= nchrdev)
160 return (ENXIO);
161 if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
162 /*
163 * When running in very secure mode, do not allow
164 * opens for writing of any disk character devices.
165 */
166 if (securelevel >= 2 && cdevsw[maj].d_type == D_DISK)
167 return (EPERM);
168 /*
169 * When running in secure mode, do not allow opens
170 * for writing of /dev/mem, /dev/kmem, or character
171 * devices whose corresponding block devices are
172 * currently mounted.
173 */
174 if (securelevel >= 1) {
175 if ((bdev = chrtoblk(dev)) != NODEV &&
176 vfinddev(bdev, VBLK, &bvp) &&
177 bvp->v_usecount > 0 &&
178 (error = vfs_mountedon(bvp)))
179 return (error);
180 if (iskmemdev(dev))
181 return (EPERM);
182 }
183 }
184 if (cdevsw[maj].d_type == D_TTY)
185 vp->v_flag |= VISTTY;
186 VOP_UNLOCK(vp);
187 error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, ap->a_p);
188 VOP_LOCK(vp);
189 return (error);
190
191 case VBLK:
192 if ((u_int)maj >= nblkdev)
193 return (ENXIO);
194 /*
195 * When running in very secure mode, do not allow
196 * opens for writing of any disk block devices.
197 */
198 if (securelevel >= 2 && ap->a_cred != FSCRED &&
199 (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK)
200 return (EPERM);
201 /*
202 * Do not allow opens of block devices that are
203 * currently mounted.
204 */
205 if ((error = vfs_mountedon(vp)) != 0)
206 return (error);
207 return ((*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, ap->a_p));
208 case VNON:
209 case VLNK:
210 case VDIR:
211 case VREG:
212 case VBAD:
213 case VFIFO:
214 case VSOCK:
215 break;
216 }
217 return (0);
218 }
219
220 /*
221 * Vnode op for read
222 */
223 /* ARGSUSED */
224 int
225 spec_read(v)
226 void *v;
227 {
228 struct vop_read_args /* {
229 struct vnode *a_vp;
230 struct uio *a_uio;
231 int a_ioflag;
232 struct ucred *a_cred;
233 } */ *ap = v;
234 register struct vnode *vp = ap->a_vp;
235 register struct uio *uio = ap->a_uio;
236 struct proc *p = uio->uio_procp;
237 struct buf *bp;
238 daddr_t bn, nextbn;
239 long bsize, bscale, ssize;
240 struct partinfo dpart;
241 int n, on, majordev;
242 int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
243 int error = 0;
244
245 #ifdef DIAGNOSTIC
246 if (uio->uio_rw != UIO_READ)
247 panic("spec_read mode");
248 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
249 panic("spec_read proc");
250 #endif
251 if (uio->uio_resid == 0)
252 return (0);
253
254 switch (vp->v_type) {
255
256 case VCHR:
257 VOP_UNLOCK(vp);
258 error = (*cdevsw[major(vp->v_rdev)].d_read)
259 (vp->v_rdev, uio, ap->a_ioflag);
260 VOP_LOCK(vp);
261 return (error);
262
263 case VBLK:
264 if (uio->uio_resid == 0)
265 return (0);
266 if (uio->uio_offset < 0)
267 return (EINVAL);
268 bsize = BLKDEV_IOSIZE;
269 ssize = DEV_BSIZE;
270 if ((majordev = major(vp->v_rdev)) < nblkdev &&
271 (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
272 (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
273 if (dpart.part->p_fstype == FS_BSDFFS &&
274 dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
275 bsize = dpart.part->p_frag *
276 dpart.part->p_fsize;
277 if (dpart.disklab->d_secsize != 0)
278 ssize = dpart.disklab->d_secsize;
279 }
280 bscale = bsize / ssize;
281 do {
282 bn = (uio->uio_offset / ssize) &~ (bscale - 1);
283 on = uio->uio_offset % bsize;
284 n = min((unsigned)(bsize - on), uio->uio_resid);
285 if (vp->v_lastr + bscale == bn) {
286 nextbn = bn + bscale;
287 error = breadn(vp, bn, (int)bsize, &nextbn,
288 (int *)&bsize, 1, NOCRED, &bp);
289 } else
290 error = bread(vp, bn, (int)bsize, NOCRED, &bp);
291 vp->v_lastr = bn;
292 n = min(n, bsize - bp->b_resid);
293 if (error) {
294 brelse(bp);
295 return (error);
296 }
297 error = uiomove((char *)bp->b_data + on, n, uio);
298 brelse(bp);
299 } while (error == 0 && uio->uio_resid > 0 && n != 0);
300 return (error);
301
302 default:
303 panic("spec_read type");
304 }
305 /* NOTREACHED */
306 }
307
308 /*
309 * Vnode op for write
310 */
311 /* ARGSUSED */
312 int
313 spec_write(v)
314 void *v;
315 {
316 struct vop_write_args /* {
317 struct vnode *a_vp;
318 struct uio *a_uio;
319 int a_ioflag;
320 struct ucred *a_cred;
321 } */ *ap = v;
322 register struct vnode *vp = ap->a_vp;
323 register struct uio *uio = ap->a_uio;
324 struct proc *p = uio->uio_procp;
325 struct buf *bp;
326 daddr_t bn;
327 long bsize, bscale, ssize;
328 struct partinfo dpart;
329 int n, on, majordev;
330 int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
331 int error = 0;
332
333 #ifdef DIAGNOSTIC
334 if (uio->uio_rw != UIO_WRITE)
335 panic("spec_write mode");
336 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
337 panic("spec_write proc");
338 #endif
339
340 switch (vp->v_type) {
341
342 case VCHR:
343 VOP_UNLOCK(vp);
344 error = (*cdevsw[major(vp->v_rdev)].d_write)
345 (vp->v_rdev, uio, ap->a_ioflag);
346 VOP_LOCK(vp);
347 return (error);
348
349 case VBLK:
350 if (uio->uio_resid == 0)
351 return (0);
352 if (uio->uio_offset < 0)
353 return (EINVAL);
354 bsize = BLKDEV_IOSIZE;
355 ssize = DEV_BSIZE;
356 if ((majordev = major(vp->v_rdev)) < nblkdev &&
357 (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
358 (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
359 if (dpart.part->p_fstype == FS_BSDFFS &&
360 dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
361 bsize = dpart.part->p_frag *
362 dpart.part->p_fsize;
363 if (dpart.disklab->d_secsize != 0)
364 ssize = dpart.disklab->d_secsize;
365 }
366 bscale = bsize / ssize;
367 do {
368 bn = (uio->uio_offset / ssize) &~ (bscale - 1);
369 on = uio->uio_offset % bsize;
370 n = min((unsigned)(bsize - on), uio->uio_resid);
371 if (n == bsize)
372 bp = getblk(vp, bn, bsize, 0, 0);
373 else
374 error = bread(vp, bn, bsize, NOCRED, &bp);
375 n = min(n, bsize - bp->b_resid);
376 if (error) {
377 brelse(bp);
378 return (error);
379 }
380 error = uiomove((char *)bp->b_data + on, n, uio);
381 if (n + on == bsize)
382 bawrite(bp);
383 else
384 bdwrite(bp);
385 } while (error == 0 && uio->uio_resid > 0 && n != 0);
386 return (error);
387
388 default:
389 panic("spec_write type");
390 }
391 /* NOTREACHED */
392 }
393
394 /*
395 * Device ioctl operation.
396 */
397 /* ARGSUSED */
398 int
399 spec_ioctl(v)
400 void *v;
401 {
402 struct vop_ioctl_args /* {
403 struct vnode *a_vp;
404 u_long a_command;
405 caddr_t a_data;
406 int a_fflag;
407 struct ucred *a_cred;
408 struct proc *a_p;
409 } */ *ap = v;
410 dev_t dev = ap->a_vp->v_rdev;
411 int maj = major(dev);
412
413 switch (ap->a_vp->v_type) {
414
415 case VCHR:
416 return ((*cdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
417 ap->a_fflag, ap->a_p));
418
419 case VBLK:
420 if (ap->a_command == 0 && (long)ap->a_data == B_TAPE)
421 if (bdevsw[maj].d_type == D_TAPE)
422 return (0);
423 else
424 return (1);
425 return ((*bdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
426 ap->a_fflag, ap->a_p));
427
428 default:
429 panic("spec_ioctl");
430 /* NOTREACHED */
431 }
432 }
433
434 /* ARGSUSED */
435 int
436 spec_select(v)
437 void *v;
438 {
439 struct vop_select_args /* {
440 struct vnode *a_vp;
441 int a_which;
442 int a_fflags;
443 struct ucred *a_cred;
444 struct proc *a_p;
445 } */ *ap = v;
446 register dev_t dev;
447
448 switch (ap->a_vp->v_type) {
449
450 case VCHR:
451 dev = ap->a_vp->v_rdev;
452 return (*cdevsw[major(dev)].d_select)(dev, ap->a_which, ap->a_p);
453
454 default:
455 return (genfs_select(v));
456 }
457 }
458 /*
459 * Synch buffers associated with a block device
460 */
461 /* ARGSUSED */
462 int
463 spec_fsync(v)
464 void *v;
465 {
466 struct vop_fsync_args /* {
467 struct vnode *a_vp;
468 struct ucred *a_cred;
469 int a_waitfor;
470 struct proc *a_p;
471 } */ *ap = v;
472 register struct vnode *vp = ap->a_vp;
473 register struct buf *bp;
474 struct buf *nbp;
475 int s;
476
477 if (vp->v_type == VBLK)
478 vflushbuf(vp, ap->a_waitfor == MNT_WAIT);
479 return (0);
480 }
481
482 /*
483 * Just call the device strategy routine
484 */
485 int
486 spec_strategy(v)
487 void *v;
488 {
489 struct vop_strategy_args /* {
490 struct buf *a_bp;
491 } */ *ap = v;
492
493 (*bdevsw[major(ap->a_bp->b_dev)].d_strategy)(ap->a_bp);
494 return (0);
495 }
496
497 /*
498 * This is a noop, simply returning what one has been given.
499 */
500 int
501 spec_bmap(v)
502 void *v;
503 {
504 struct vop_bmap_args /* {
505 struct vnode *a_vp;
506 daddr_t a_bn;
507 struct vnode **a_vpp;
508 daddr_t *a_bnp;
509 } */ *ap = v;
510
511 if (ap->a_vpp != NULL)
512 *ap->a_vpp = ap->a_vp;
513 if (ap->a_bnp != NULL)
514 *ap->a_bnp = ap->a_bn;
515 return (0);
516 }
517
518 /*
519 * At the moment we do not do any locking.
520 */
521 /* ARGSUSED */
522 int
523 spec_lock(v)
524 void *v;
525 {
526
527 return (0);
528 }
529
530 /* ARGSUSED */
531 int
532 spec_unlock(v)
533 void *v;
534 {
535
536 return (0);
537 }
538
539 /*
540 * Device close routine
541 */
542 /* ARGSUSED */
543 int
544 spec_close(v)
545 void *v;
546 {
547 struct vop_close_args /* {
548 struct vnode *a_vp;
549 int a_fflag;
550 struct ucred *a_cred;
551 struct proc *a_p;
552 } */ *ap = v;
553 register struct vnode *vp = ap->a_vp;
554 dev_t dev = vp->v_rdev;
555 int (*devclose) __P((dev_t, int, int, struct proc *));
556 int mode, error;
557
558 switch (vp->v_type) {
559
560 case VCHR:
561 /*
562 * Hack: a tty device that is a controlling terminal
563 * has a reference from the session structure.
564 * We cannot easily tell that a character device is
565 * a controlling terminal, unless it is the closing
566 * process' controlling terminal. In that case,
567 * if the reference count is 2 (this last descriptor
568 * plus the session), release the reference from the session.
569 */
570 if (vcount(vp) == 2 && ap->a_p &&
571 vp == ap->a_p->p_session->s_ttyvp) {
572 vrele(vp);
573 ap->a_p->p_session->s_ttyvp = NULL;
574 }
575 /*
576 * If the vnode is locked, then we are in the midst
577 * of forcably closing the device, otherwise we only
578 * close on last reference.
579 */
580 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
581 return (0);
582 devclose = cdevsw[major(dev)].d_close;
583 mode = S_IFCHR;
584 break;
585
586 case VBLK:
587 /*
588 * On last close of a block device (that isn't mounted)
589 * we must invalidate any in core blocks, so that
590 * we can, for instance, change floppy disks.
591 */
592 error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
593 if (error)
594 return (error);
595 /*
596 * We do not want to really close the device if it
597 * is still in use unless we are trying to close it
598 * forcibly. Since every use (buffer, vnode, swap, cmap)
599 * holds a reference to the vnode, and because we mark
600 * any other vnodes that alias this device, when the
601 * sum of the reference counts on all the aliased
602 * vnodes descends to one, we are on last close.
603 */
604 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
605 return (0);
606 devclose = bdevsw[major(dev)].d_close;
607 mode = S_IFBLK;
608 break;
609
610 default:
611 panic("spec_close: not special");
612 }
613
614 return ((*devclose)(dev, ap->a_fflag, mode, ap->a_p));
615 }
616
617 /*
618 * Print out the contents of a special device vnode.
619 */
620 int
621 spec_print(v)
622 void *v;
623 {
624 struct vop_print_args /* {
625 struct vnode *a_vp;
626 } */ *ap = v;
627
628 printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
629 minor(ap->a_vp->v_rdev));
630 return 0;
631 }
632
633 /*
634 * Return POSIX pathconf information applicable to special devices.
635 */
636 int
637 spec_pathconf(v)
638 void *v;
639 {
640 struct vop_pathconf_args /* {
641 struct vnode *a_vp;
642 int a_name;
643 register_t *a_retval;
644 } */ *ap = v;
645
646 switch (ap->a_name) {
647 case _PC_LINK_MAX:
648 *ap->a_retval = LINK_MAX;
649 return (0);
650 case _PC_MAX_CANON:
651 *ap->a_retval = MAX_CANON;
652 return (0);
653 case _PC_MAX_INPUT:
654 *ap->a_retval = MAX_INPUT;
655 return (0);
656 case _PC_PIPE_BUF:
657 *ap->a_retval = PIPE_BUF;
658 return (0);
659 case _PC_CHOWN_RESTRICTED:
660 *ap->a_retval = 1;
661 return (0);
662 case _PC_VDISABLE:
663 *ap->a_retval = _POSIX_VDISABLE;
664 return (0);
665 default:
666 return (EINVAL);
667 }
668 /* NOTREACHED */
669 }
670