spec_vnops.c revision 1.33 1 /* $NetBSD: spec_vnops.c,v 1.33 1996/10/10 22:54:19 christos 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_poll_desc, spec_poll }, /* poll */
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_poll(v)
437 void *v;
438 {
439 struct vop_poll_args /* {
440 struct vnode *a_vp;
441 int a_events;
442 struct proc *a_p;
443 } */ *ap = v;
444 register dev_t dev;
445
446 switch (ap->a_vp->v_type) {
447
448 case VCHR:
449 dev = ap->a_vp->v_rdev;
450 return (*cdevsw[major(dev)].d_poll)(dev, ap->a_events, ap->a_p);
451
452 default:
453 return (genfs_poll(v));
454 }
455 }
456 /*
457 * Synch buffers associated with a block device
458 */
459 /* ARGSUSED */
460 int
461 spec_fsync(v)
462 void *v;
463 {
464 struct vop_fsync_args /* {
465 struct vnode *a_vp;
466 struct ucred *a_cred;
467 int a_waitfor;
468 struct proc *a_p;
469 } */ *ap = v;
470 register struct vnode *vp = ap->a_vp;
471
472 if (vp->v_type == VBLK)
473 vflushbuf(vp, ap->a_waitfor == MNT_WAIT);
474 return (0);
475 }
476
477 /*
478 * Just call the device strategy routine
479 */
480 int
481 spec_strategy(v)
482 void *v;
483 {
484 struct vop_strategy_args /* {
485 struct buf *a_bp;
486 } */ *ap = v;
487
488 (*bdevsw[major(ap->a_bp->b_dev)].d_strategy)(ap->a_bp);
489 return (0);
490 }
491
492 /*
493 * This is a noop, simply returning what one has been given.
494 */
495 int
496 spec_bmap(v)
497 void *v;
498 {
499 struct vop_bmap_args /* {
500 struct vnode *a_vp;
501 daddr_t a_bn;
502 struct vnode **a_vpp;
503 daddr_t *a_bnp;
504 } */ *ap = v;
505
506 if (ap->a_vpp != NULL)
507 *ap->a_vpp = ap->a_vp;
508 if (ap->a_bnp != NULL)
509 *ap->a_bnp = ap->a_bn;
510 return (0);
511 }
512
513 /*
514 * At the moment we do not do any locking.
515 */
516 /* ARGSUSED */
517 int
518 spec_lock(v)
519 void *v;
520 {
521
522 return (0);
523 }
524
525 /* ARGSUSED */
526 int
527 spec_unlock(v)
528 void *v;
529 {
530
531 return (0);
532 }
533
534 /*
535 * Device close routine
536 */
537 /* ARGSUSED */
538 int
539 spec_close(v)
540 void *v;
541 {
542 struct vop_close_args /* {
543 struct vnode *a_vp;
544 int a_fflag;
545 struct ucred *a_cred;
546 struct proc *a_p;
547 } */ *ap = v;
548 register struct vnode *vp = ap->a_vp;
549 dev_t dev = vp->v_rdev;
550 int (*devclose) __P((dev_t, int, int, struct proc *));
551 int mode, error;
552
553 switch (vp->v_type) {
554
555 case VCHR:
556 /*
557 * Hack: a tty device that is a controlling terminal
558 * has a reference from the session structure.
559 * We cannot easily tell that a character device is
560 * a controlling terminal, unless it is the closing
561 * process' controlling terminal. In that case,
562 * if the reference count is 2 (this last descriptor
563 * plus the session), release the reference from the session.
564 */
565 if (vcount(vp) == 2 && ap->a_p &&
566 vp == ap->a_p->p_session->s_ttyvp) {
567 vrele(vp);
568 ap->a_p->p_session->s_ttyvp = NULL;
569 }
570 /*
571 * If the vnode is locked, then we are in the midst
572 * of forcably closing the device, otherwise we only
573 * close on last reference.
574 */
575 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
576 return (0);
577 devclose = cdevsw[major(dev)].d_close;
578 mode = S_IFCHR;
579 break;
580
581 case VBLK:
582 /*
583 * On last close of a block device (that isn't mounted)
584 * we must invalidate any in core blocks, so that
585 * we can, for instance, change floppy disks.
586 */
587 error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
588 if (error)
589 return (error);
590 /*
591 * We do not want to really close the device if it
592 * is still in use unless we are trying to close it
593 * forcibly. Since every use (buffer, vnode, swap, cmap)
594 * holds a reference to the vnode, and because we mark
595 * any other vnodes that alias this device, when the
596 * sum of the reference counts on all the aliased
597 * vnodes descends to one, we are on last close.
598 */
599 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
600 return (0);
601 devclose = bdevsw[major(dev)].d_close;
602 mode = S_IFBLK;
603 break;
604
605 default:
606 panic("spec_close: not special");
607 }
608
609 return ((*devclose)(dev, ap->a_fflag, mode, ap->a_p));
610 }
611
612 /*
613 * Print out the contents of a special device vnode.
614 */
615 int
616 spec_print(v)
617 void *v;
618 {
619 struct vop_print_args /* {
620 struct vnode *a_vp;
621 } */ *ap = v;
622
623 kprintf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
624 minor(ap->a_vp->v_rdev));
625 return 0;
626 }
627
628 /*
629 * Return POSIX pathconf information applicable to special devices.
630 */
631 int
632 spec_pathconf(v)
633 void *v;
634 {
635 struct vop_pathconf_args /* {
636 struct vnode *a_vp;
637 int a_name;
638 register_t *a_retval;
639 } */ *ap = v;
640
641 switch (ap->a_name) {
642 case _PC_LINK_MAX:
643 *ap->a_retval = LINK_MAX;
644 return (0);
645 case _PC_MAX_CANON:
646 *ap->a_retval = MAX_CANON;
647 return (0);
648 case _PC_MAX_INPUT:
649 *ap->a_retval = MAX_INPUT;
650 return (0);
651 case _PC_PIPE_BUF:
652 *ap->a_retval = PIPE_BUF;
653 return (0);
654 case _PC_CHOWN_RESTRICTED:
655 *ap->a_retval = 1;
656 return (0);
657 case _PC_VDISABLE:
658 *ap->a_retval = _POSIX_VDISABLE;
659 return (0);
660 default:
661 return (EINVAL);
662 }
663 /* NOTREACHED */
664 }
665