spec_vnops.c revision 1.157 1 /* $NetBSD: spec_vnops.c,v 1.157 2015/12/08 20:36:15 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * Copyright (c) 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 *
57 * @(#)spec_vnops.c 8.15 (Berkeley) 7/14/95
58 */
59
60 #include <sys/cdefs.h>
61 __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.157 2015/12/08 20:36:15 christos Exp $");
62
63 #include <sys/param.h>
64 #include <sys/proc.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/conf.h>
68 #include <sys/buf.h>
69 #include <sys/mount.h>
70 #include <sys/namei.h>
71 #include <sys/vnode.h>
72 #include <sys/stat.h>
73 #include <sys/errno.h>
74 #include <sys/ioctl.h>
75 #include <sys/poll.h>
76 #include <sys/file.h>
77 #include <sys/disklabel.h>
78 #include <sys/lockf.h>
79 #include <sys/tty.h>
80 #include <sys/kauth.h>
81 #include <sys/fstrans.h>
82 #include <sys/module.h>
83
84 #include <miscfs/genfs/genfs.h>
85 #include <miscfs/specfs/specdev.h>
86
87 /* symbolic sleep message strings for devices */
88 const char devopn[] = "devopn";
89 const char devio[] = "devio";
90 const char devwait[] = "devwait";
91 const char devin[] = "devin";
92 const char devout[] = "devout";
93 const char devioc[] = "devioc";
94 const char devcls[] = "devcls";
95
96 #define SPECHSZ 64
97 #if ((SPECHSZ&(SPECHSZ-1)) == 0)
98 #define SPECHASH(rdev) (((rdev>>5)+(rdev))&(SPECHSZ-1))
99 #else
100 #define SPECHASH(rdev) (((unsigned)((rdev>>5)+(rdev)))%SPECHSZ)
101 #endif
102
103 static vnode_t *specfs_hash[SPECHSZ];
104 extern struct mount *dead_rootmount;
105
106 /*
107 * This vnode operations vector is used for special device nodes
108 * created from whole cloth by the kernel. For the ops vector for
109 * vnodes built from special devices found in a filesystem, see (e.g)
110 * ffs_specop_entries[] in ffs_vnops.c or the equivalent for other
111 * filesystems.
112 */
113
114 int (**spec_vnodeop_p)(void *);
115 const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
116 { &vop_default_desc, vn_default_error },
117 { &vop_lookup_desc, spec_lookup }, /* lookup */
118 { &vop_create_desc, spec_create }, /* create */
119 { &vop_mknod_desc, spec_mknod }, /* mknod */
120 { &vop_open_desc, spec_open }, /* open */
121 { &vop_close_desc, spec_close }, /* close */
122 { &vop_access_desc, spec_access }, /* access */
123 { &vop_getattr_desc, spec_getattr }, /* getattr */
124 { &vop_setattr_desc, spec_setattr }, /* setattr */
125 { &vop_read_desc, spec_read }, /* read */
126 { &vop_write_desc, spec_write }, /* write */
127 { &vop_fallocate_desc, spec_fallocate }, /* fallocate */
128 { &vop_fdiscard_desc, spec_fdiscard }, /* fdiscard */
129 { &vop_fcntl_desc, spec_fcntl }, /* fcntl */
130 { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
131 { &vop_poll_desc, spec_poll }, /* poll */
132 { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
133 { &vop_revoke_desc, spec_revoke }, /* revoke */
134 { &vop_mmap_desc, spec_mmap }, /* mmap */
135 { &vop_fsync_desc, spec_fsync }, /* fsync */
136 { &vop_seek_desc, spec_seek }, /* seek */
137 { &vop_remove_desc, spec_remove }, /* remove */
138 { &vop_link_desc, spec_link }, /* link */
139 { &vop_rename_desc, spec_rename }, /* rename */
140 { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
141 { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
142 { &vop_symlink_desc, spec_symlink }, /* symlink */
143 { &vop_readdir_desc, spec_readdir }, /* readdir */
144 { &vop_readlink_desc, spec_readlink }, /* readlink */
145 { &vop_abortop_desc, spec_abortop }, /* abortop */
146 { &vop_inactive_desc, spec_inactive }, /* inactive */
147 { &vop_reclaim_desc, spec_reclaim }, /* reclaim */
148 { &vop_lock_desc, spec_lock }, /* lock */
149 { &vop_unlock_desc, spec_unlock }, /* unlock */
150 { &vop_bmap_desc, spec_bmap }, /* bmap */
151 { &vop_strategy_desc, spec_strategy }, /* strategy */
152 { &vop_print_desc, spec_print }, /* print */
153 { &vop_islocked_desc, spec_islocked }, /* islocked */
154 { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
155 { &vop_advlock_desc, spec_advlock }, /* advlock */
156 { &vop_bwrite_desc, spec_bwrite }, /* bwrite */
157 { &vop_getpages_desc, spec_getpages }, /* getpages */
158 { &vop_putpages_desc, spec_putpages }, /* putpages */
159 { NULL, NULL }
160 };
161 const struct vnodeopv_desc spec_vnodeop_opv_desc =
162 { &spec_vnodeop_p, spec_vnodeop_entries };
163
164 static kauth_listener_t rawio_listener;
165
166 /* Returns true if vnode is /dev/mem or /dev/kmem. */
167 bool
168 iskmemvp(struct vnode *vp)
169 {
170 return ((vp->v_type == VCHR) && iskmemdev(vp->v_rdev));
171 }
172
173 /*
174 * Returns true if dev is /dev/mem or /dev/kmem.
175 */
176 int
177 iskmemdev(dev_t dev)
178 {
179 /* mem_no is emitted by config(8) to generated devsw.c */
180 extern const int mem_no;
181
182 /* minor 14 is /dev/io on i386 with COMPAT_10 */
183 return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
184 }
185
186 static int
187 rawio_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
188 void *arg0, void *arg1, void *arg2, void *arg3)
189 {
190 int result;
191
192 result = KAUTH_RESULT_DEFER;
193
194 if ((action != KAUTH_DEVICE_RAWIO_SPEC) &&
195 (action != KAUTH_DEVICE_RAWIO_PASSTHRU))
196 return result;
197
198 /* Access is mandated by permissions. */
199 result = KAUTH_RESULT_ALLOW;
200
201 return result;
202 }
203
204 void
205 spec_init(void)
206 {
207
208 rawio_listener = kauth_listen_scope(KAUTH_SCOPE_DEVICE,
209 rawio_listener_cb, NULL);
210 }
211
212 /*
213 * Initialize a vnode that represents a device.
214 */
215 void
216 spec_node_init(vnode_t *vp, dev_t rdev)
217 {
218 specnode_t *sn;
219 specdev_t *sd;
220 vnode_t *vp2;
221 vnode_t **vpp;
222
223 KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
224 KASSERT(vp->v_specnode == NULL);
225
226 /*
227 * Search the hash table for this device. If known, add a
228 * reference to the device structure. If not known, create
229 * a new entry to represent the device. In all cases add
230 * the vnode to the hash table.
231 */
232 sn = kmem_alloc(sizeof(*sn), KM_SLEEP);
233 if (sn == NULL) {
234 /* XXX */
235 panic("spec_node_init: unable to allocate memory");
236 }
237 sd = kmem_alloc(sizeof(*sd), KM_SLEEP);
238 if (sd == NULL) {
239 /* XXX */
240 panic("spec_node_init: unable to allocate memory");
241 }
242 mutex_enter(&device_lock);
243 vpp = &specfs_hash[SPECHASH(rdev)];
244 for (vp2 = *vpp; vp2 != NULL; vp2 = vp2->v_specnext) {
245 KASSERT(vp2->v_specnode != NULL);
246 if (rdev == vp2->v_rdev && vp->v_type == vp2->v_type) {
247 break;
248 }
249 }
250 if (vp2 == NULL) {
251 /* No existing record, create a new one. */
252 sd->sd_rdev = rdev;
253 sd->sd_mountpoint = NULL;
254 sd->sd_lockf = NULL;
255 sd->sd_refcnt = 1;
256 sd->sd_opencnt = 0;
257 sd->sd_bdevvp = NULL;
258 sn->sn_dev = sd;
259 sd = NULL;
260 } else {
261 /* Use the existing record. */
262 sn->sn_dev = vp2->v_specnode->sn_dev;
263 sn->sn_dev->sd_refcnt++;
264 }
265 /* Insert vnode into the hash chain. */
266 sn->sn_opencnt = 0;
267 sn->sn_rdev = rdev;
268 sn->sn_gone = false;
269 vp->v_specnode = sn;
270 vp->v_specnext = *vpp;
271 *vpp = vp;
272 mutex_exit(&device_lock);
273
274 /* Free the record we allocated if unused. */
275 if (sd != NULL) {
276 kmem_free(sd, sizeof(*sd));
277 }
278 }
279
280 /*
281 * Lookup a vnode by device number and return it referenced.
282 */
283 int
284 spec_node_lookup_by_dev(enum vtype type, dev_t dev, vnode_t **vpp)
285 {
286 int error;
287 vnode_t *vp;
288
289 mutex_enter(&device_lock);
290 for (vp = specfs_hash[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
291 if (type == vp->v_type && dev == vp->v_rdev) {
292 mutex_enter(vp->v_interlock);
293 /* If clean or being cleaned, then ignore it. */
294 if (vdead_check(vp, VDEAD_NOWAIT) == 0)
295 break;
296 mutex_exit(vp->v_interlock);
297 }
298 }
299 KASSERT(vp == NULL || mutex_owned(vp->v_interlock));
300 if (vp == NULL) {
301 mutex_exit(&device_lock);
302 return ENOENT;
303 }
304 /*
305 * If it is an opened block device return the opened vnode.
306 */
307 if (type == VBLK && vp->v_specnode->sn_dev->sd_bdevvp != NULL) {
308 mutex_exit(vp->v_interlock);
309 vp = vp->v_specnode->sn_dev->sd_bdevvp;
310 mutex_enter(vp->v_interlock);
311 }
312 mutex_exit(&device_lock);
313 error = vget(vp, 0, true /* wait */);
314 if (error != 0)
315 return error;
316 *vpp = vp;
317
318 return 0;
319 }
320
321 /*
322 * Lookup a vnode by file system mounted on and return it referenced.
323 */
324 int
325 spec_node_lookup_by_mount(struct mount *mp, vnode_t **vpp)
326 {
327 int i, error;
328 vnode_t *vp, *vq;
329
330 mutex_enter(&device_lock);
331 for (i = 0, vq = NULL; i < SPECHSZ && vq == NULL; i++) {
332 for (vp = specfs_hash[i]; vp; vp = vp->v_specnext) {
333 if (vp->v_type != VBLK)
334 continue;
335 vq = vp->v_specnode->sn_dev->sd_bdevvp;
336 if (vq != NULL &&
337 vq->v_specnode->sn_dev->sd_mountpoint == mp)
338 break;
339 vq = NULL;
340 }
341 }
342 if (vq == NULL) {
343 mutex_exit(&device_lock);
344 return ENOENT;
345 }
346 mutex_enter(vq->v_interlock);
347 mutex_exit(&device_lock);
348 error = vget(vq, 0, true /* wait */);
349 if (error != 0)
350 return error;
351 *vpp = vq;
352
353 return 0;
354
355 }
356
357 /*
358 * Get the file system mounted on this block device.
359 */
360 struct mount *
361 spec_node_getmountedfs(vnode_t *devvp)
362 {
363 struct mount *mp;
364
365 KASSERT(devvp->v_type == VBLK);
366 mp = devvp->v_specnode->sn_dev->sd_mountpoint;
367
368 return mp;
369 }
370
371 /*
372 * Set the file system mounted on this block device.
373 */
374 void
375 spec_node_setmountedfs(vnode_t *devvp, struct mount *mp)
376 {
377
378 KASSERT(devvp->v_type == VBLK);
379 KASSERT(devvp->v_specnode->sn_dev->sd_mountpoint == NULL || mp == NULL);
380 devvp->v_specnode->sn_dev->sd_mountpoint = mp;
381 }
382
383 /*
384 * A vnode representing a special device is going away. Close
385 * the device if the vnode holds it open.
386 */
387 void
388 spec_node_revoke(vnode_t *vp)
389 {
390 specnode_t *sn;
391 specdev_t *sd;
392
393 sn = vp->v_specnode;
394 sd = sn->sn_dev;
395
396 KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
397 KASSERT(vp->v_specnode != NULL);
398 KASSERT(sn->sn_gone == false);
399
400 mutex_enter(&device_lock);
401 KASSERT(sn->sn_opencnt <= sd->sd_opencnt);
402 if (sn->sn_opencnt != 0) {
403 sd->sd_opencnt -= (sn->sn_opencnt - 1);
404 sn->sn_opencnt = 1;
405 sn->sn_gone = true;
406 mutex_exit(&device_lock);
407
408 VOP_CLOSE(vp, FNONBLOCK, NOCRED);
409
410 mutex_enter(&device_lock);
411 KASSERT(sn->sn_opencnt == 0);
412 }
413 mutex_exit(&device_lock);
414 }
415
416 /*
417 * A vnode representing a special device is being recycled.
418 * Destroy the specfs component.
419 */
420 void
421 spec_node_destroy(vnode_t *vp)
422 {
423 specnode_t *sn;
424 specdev_t *sd;
425 vnode_t **vpp, *vp2;
426 int refcnt;
427
428 sn = vp->v_specnode;
429 sd = sn->sn_dev;
430
431 KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
432 KASSERT(vp->v_specnode != NULL);
433 KASSERT(sn->sn_opencnt == 0);
434
435 mutex_enter(&device_lock);
436 /* Remove from the hash and destroy the node. */
437 vpp = &specfs_hash[SPECHASH(vp->v_rdev)];
438 for (vp2 = *vpp;; vp2 = vp2->v_specnext) {
439 if (vp2 == NULL) {
440 panic("spec_node_destroy: corrupt hash");
441 }
442 if (vp2 == vp) {
443 KASSERT(vp == *vpp);
444 *vpp = vp->v_specnext;
445 break;
446 }
447 if (vp2->v_specnext == vp) {
448 vp2->v_specnext = vp->v_specnext;
449 break;
450 }
451 }
452 sn = vp->v_specnode;
453 vp->v_specnode = NULL;
454 refcnt = sd->sd_refcnt--;
455 KASSERT(refcnt > 0);
456 mutex_exit(&device_lock);
457
458 /* If the device is no longer in use, destroy our record. */
459 if (refcnt == 1) {
460 KASSERT(sd->sd_opencnt == 0);
461 KASSERT(sd->sd_bdevvp == NULL);
462 kmem_free(sd, sizeof(*sd));
463 }
464 kmem_free(sn, sizeof(*sn));
465 }
466
467 /*
468 * Trivial lookup routine that always fails.
469 */
470 int
471 spec_lookup(void *v)
472 {
473 struct vop_lookup_v2_args /* {
474 struct vnode *a_dvp;
475 struct vnode **a_vpp;
476 struct componentname *a_cnp;
477 } */ *ap = v;
478
479 *ap->a_vpp = NULL;
480 return (ENOTDIR);
481 }
482
483 typedef int (*spec_ioctl_t)(dev_t, u_long, void *, int, struct lwp *);
484
485 /*
486 * Open a special file.
487 */
488 /* ARGSUSED */
489 int
490 spec_open(void *v)
491 {
492 struct vop_open_args /* {
493 struct vnode *a_vp;
494 int a_mode;
495 kauth_cred_t a_cred;
496 } */ *ap = v;
497 struct lwp *l;
498 struct vnode *vp;
499 dev_t dev;
500 int error;
501 enum kauth_device_req req;
502 specnode_t *sn;
503 specdev_t *sd;
504 spec_ioctl_t ioctl;
505 u_int gen;
506 const char *name;
507 struct partinfo pi;
508
509 l = curlwp;
510 vp = ap->a_vp;
511 dev = vp->v_rdev;
512 sn = vp->v_specnode;
513 sd = sn->sn_dev;
514 name = NULL;
515 gen = 0;
516
517 /*
518 * Don't allow open if fs is mounted -nodev.
519 */
520 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
521 return (ENXIO);
522
523 switch (ap->a_mode & (FREAD | FWRITE)) {
524 case FREAD | FWRITE:
525 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_RW;
526 break;
527 case FWRITE:
528 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE;
529 break;
530 default:
531 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_READ;
532 break;
533 }
534
535 switch (vp->v_type) {
536 case VCHR:
537 error = kauth_authorize_device_spec(ap->a_cred, req, vp);
538 if (error != 0)
539 return (error);
540
541 /*
542 * Character devices can accept opens from multiple
543 * vnodes.
544 */
545 mutex_enter(&device_lock);
546 if (sn->sn_gone) {
547 mutex_exit(&device_lock);
548 return (EBADF);
549 }
550 sd->sd_opencnt++;
551 sn->sn_opencnt++;
552 mutex_exit(&device_lock);
553 if (cdev_type(dev) == D_TTY)
554 vp->v_vflag |= VV_ISTTY;
555 VOP_UNLOCK(vp);
556 do {
557 const struct cdevsw *cdev;
558
559 gen = module_gen;
560 error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
561 if (error != ENXIO)
562 break;
563
564 /* Check if we already have a valid driver */
565 mutex_enter(&device_lock);
566 cdev = cdevsw_lookup(dev);
567 mutex_exit(&device_lock);
568 if (cdev != NULL)
569 break;
570
571 /* Get device name from devsw_conv array */
572 if ((name = cdevsw_getname(major(dev))) == NULL)
573 break;
574
575 /* Try to autoload device module */
576 (void) module_autoload(name, MODULE_CLASS_DRIVER);
577 } while (gen != module_gen);
578
579 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
580 break;
581
582 case VBLK:
583 error = kauth_authorize_device_spec(ap->a_cred, req, vp);
584 if (error != 0)
585 return (error);
586
587 /*
588 * For block devices, permit only one open. The buffer
589 * cache cannot remain self-consistent with multiple
590 * vnodes holding a block device open.
591 */
592 mutex_enter(&device_lock);
593 if (sn->sn_gone) {
594 mutex_exit(&device_lock);
595 return (EBADF);
596 }
597 if (sd->sd_opencnt != 0) {
598 mutex_exit(&device_lock);
599 return EBUSY;
600 }
601 sn->sn_opencnt = 1;
602 sd->sd_opencnt = 1;
603 sd->sd_bdevvp = vp;
604 mutex_exit(&device_lock);
605 do {
606 const struct bdevsw *bdev;
607
608 gen = module_gen;
609 error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
610 if (error != ENXIO)
611 break;
612
613 /* Check if we already have a valid driver */
614 mutex_enter(&device_lock);
615 bdev = bdevsw_lookup(dev);
616 mutex_exit(&device_lock);
617 if (bdev != NULL)
618 break;
619
620 /* Get device name from devsw_conv array */
621 if ((name = bdevsw_getname(major(dev))) == NULL)
622 break;
623
624 VOP_UNLOCK(vp);
625
626 /* Try to autoload device module */
627 (void) module_autoload(name, MODULE_CLASS_DRIVER);
628
629 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
630 } while (gen != module_gen);
631
632 break;
633
634 case VNON:
635 case VLNK:
636 case VDIR:
637 case VREG:
638 case VBAD:
639 case VFIFO:
640 case VSOCK:
641 default:
642 return 0;
643 }
644
645 mutex_enter(&device_lock);
646 if (sn->sn_gone) {
647 if (error == 0)
648 error = EBADF;
649 } else if (error != 0) {
650 sd->sd_opencnt--;
651 sn->sn_opencnt--;
652 if (vp->v_type == VBLK)
653 sd->sd_bdevvp = NULL;
654
655 }
656 mutex_exit(&device_lock);
657
658 if (cdev_type(dev) != D_DISK || error != 0)
659 return error;
660
661
662 ioctl = vp->v_type == VCHR ? cdev_ioctl : bdev_ioctl;
663 error = (*ioctl)(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, curlwp);
664 if (error == 0)
665 uvm_vnp_setsize(vp, (voff_t)pi.pi_secsize * pi.pi_size);
666
667 return 0;
668 }
669
670 /*
671 * Vnode op for read
672 */
673 /* ARGSUSED */
674 int
675 spec_read(void *v)
676 {
677 struct vop_read_args /* {
678 struct vnode *a_vp;
679 struct uio *a_uio;
680 int a_ioflag;
681 kauth_cred_t a_cred;
682 } */ *ap = v;
683 struct vnode *vp = ap->a_vp;
684 struct uio *uio = ap->a_uio;
685 struct lwp *l = curlwp;
686 struct buf *bp;
687 daddr_t bn;
688 int bsize, bscale;
689 struct partinfo pi;
690 int n, on;
691 int error = 0;
692
693 #ifdef DIAGNOSTIC
694 if (uio->uio_rw != UIO_READ)
695 panic("spec_read mode");
696 if (&uio->uio_vmspace->vm_map != kernel_map &&
697 uio->uio_vmspace != curproc->p_vmspace)
698 panic("spec_read proc");
699 #endif
700 if (uio->uio_resid == 0)
701 return (0);
702
703 switch (vp->v_type) {
704
705 case VCHR:
706 VOP_UNLOCK(vp);
707 error = cdev_read(vp->v_rdev, uio, ap->a_ioflag);
708 vn_lock(vp, LK_SHARED | LK_RETRY);
709 return (error);
710
711 case VBLK:
712 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
713 if (uio->uio_offset < 0)
714 return (EINVAL);
715
716 if (bdev_ioctl(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, l) == 0)
717 bsize = pi.pi_bsize;
718 else
719 bsize = BLKDEV_IOSIZE;
720
721 bscale = bsize >> DEV_BSHIFT;
722 do {
723 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
724 on = uio->uio_offset % bsize;
725 n = min((unsigned)(bsize - on), uio->uio_resid);
726 error = bread(vp, bn, bsize, 0, &bp);
727 if (error) {
728 return (error);
729 }
730 n = min(n, bsize - bp->b_resid);
731 error = uiomove((char *)bp->b_data + on, n, uio);
732 brelse(bp, 0);
733 } while (error == 0 && uio->uio_resid > 0 && n != 0);
734 return (error);
735
736 default:
737 panic("spec_read type");
738 }
739 /* NOTREACHED */
740 }
741
742 /*
743 * Vnode op for write
744 */
745 /* ARGSUSED */
746 int
747 spec_write(void *v)
748 {
749 struct vop_write_args /* {
750 struct vnode *a_vp;
751 struct uio *a_uio;
752 int a_ioflag;
753 kauth_cred_t a_cred;
754 } */ *ap = v;
755 struct vnode *vp = ap->a_vp;
756 struct uio *uio = ap->a_uio;
757 struct lwp *l = curlwp;
758 struct buf *bp;
759 daddr_t bn;
760 int bsize, bscale;
761 struct partinfo pi;
762 int n, on;
763 int error = 0;
764
765 #ifdef DIAGNOSTIC
766 if (uio->uio_rw != UIO_WRITE)
767 panic("spec_write mode");
768 if (&uio->uio_vmspace->vm_map != kernel_map &&
769 uio->uio_vmspace != curproc->p_vmspace)
770 panic("spec_write proc");
771 #endif
772
773 switch (vp->v_type) {
774
775 case VCHR:
776 VOP_UNLOCK(vp);
777 error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
778 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
779 return (error);
780
781 case VBLK:
782 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
783 if (uio->uio_resid == 0)
784 return (0);
785 if (uio->uio_offset < 0)
786 return (EINVAL);
787
788 if (bdev_ioctl(vp->v_rdev, DIOCGPARTINFO, &pi, FREAD, l) == 0)
789 bsize = pi.pi_bsize;
790 else
791 bsize = BLKDEV_IOSIZE;
792
793 bscale = bsize >> DEV_BSHIFT;
794 do {
795 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
796 on = uio->uio_offset % bsize;
797 n = min((unsigned)(bsize - on), uio->uio_resid);
798 if (n == bsize)
799 bp = getblk(vp, bn, bsize, 0, 0);
800 else
801 error = bread(vp, bn, bsize, B_MODIFY, &bp);
802 if (error) {
803 return (error);
804 }
805 n = min(n, bsize - bp->b_resid);
806 error = uiomove((char *)bp->b_data + on, n, uio);
807 if (error)
808 brelse(bp, 0);
809 else {
810 if (n + on == bsize)
811 bawrite(bp);
812 else
813 bdwrite(bp);
814 error = bp->b_error;
815 }
816 } while (error == 0 && uio->uio_resid > 0 && n != 0);
817 return (error);
818
819 default:
820 panic("spec_write type");
821 }
822 /* NOTREACHED */
823 }
824
825 /*
826 * fdiscard, which on disk devices becomes TRIM.
827 */
828 int
829 spec_fdiscard(void *v)
830 {
831 struct vop_fdiscard_args /* {
832 struct vnode *a_vp;
833 off_t a_pos;
834 off_t a_len;
835 } */ *ap = v;
836 struct vnode *vp;
837 dev_t dev;
838
839 vp = ap->a_vp;
840 dev = NODEV;
841
842 mutex_enter(vp->v_interlock);
843 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode != NULL) {
844 dev = vp->v_rdev;
845 }
846 mutex_exit(vp->v_interlock);
847
848 if (dev == NODEV) {
849 return ENXIO;
850 }
851
852 switch (vp->v_type) {
853 case VCHR:
854 // this is not stored for character devices
855 //KASSERT(vp == vp->v_specnode->sn_dev->sd_cdevvp);
856 return cdev_discard(dev, ap->a_pos, ap->a_len);
857 case VBLK:
858 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
859 return bdev_discard(dev, ap->a_pos, ap->a_len);
860 default:
861 panic("spec_fdiscard: not a device\n");
862 }
863 }
864
865 /*
866 * Device ioctl operation.
867 */
868 /* ARGSUSED */
869 int
870 spec_ioctl(void *v)
871 {
872 struct vop_ioctl_args /* {
873 struct vnode *a_vp;
874 u_long a_command;
875 void *a_data;
876 int a_fflag;
877 kauth_cred_t a_cred;
878 } */ *ap = v;
879 struct vnode *vp;
880 dev_t dev;
881
882 /*
883 * Extract all the info we need from the vnode, taking care to
884 * avoid a race with VOP_REVOKE().
885 */
886
887 vp = ap->a_vp;
888 dev = NODEV;
889 mutex_enter(vp->v_interlock);
890 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) {
891 dev = vp->v_rdev;
892 }
893 mutex_exit(vp->v_interlock);
894 if (dev == NODEV) {
895 return ENXIO;
896 }
897
898 switch (vp->v_type) {
899
900 case VCHR:
901 return cdev_ioctl(dev, ap->a_command, ap->a_data,
902 ap->a_fflag, curlwp);
903
904 case VBLK:
905 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
906 return bdev_ioctl(dev, ap->a_command, ap->a_data,
907 ap->a_fflag, curlwp);
908
909 default:
910 panic("spec_ioctl");
911 /* NOTREACHED */
912 }
913 }
914
915 /* ARGSUSED */
916 int
917 spec_poll(void *v)
918 {
919 struct vop_poll_args /* {
920 struct vnode *a_vp;
921 int a_events;
922 } */ *ap = v;
923 struct vnode *vp;
924 dev_t dev;
925
926 /*
927 * Extract all the info we need from the vnode, taking care to
928 * avoid a race with VOP_REVOKE().
929 */
930
931 vp = ap->a_vp;
932 dev = NODEV;
933 mutex_enter(vp->v_interlock);
934 if (vdead_check(vp, VDEAD_NOWAIT) == 0 && vp->v_specnode) {
935 dev = vp->v_rdev;
936 }
937 mutex_exit(vp->v_interlock);
938 if (dev == NODEV) {
939 return POLLERR;
940 }
941
942 switch (vp->v_type) {
943
944 case VCHR:
945 return cdev_poll(dev, ap->a_events, curlwp);
946
947 default:
948 return (genfs_poll(v));
949 }
950 }
951
952 /* ARGSUSED */
953 int
954 spec_kqfilter(void *v)
955 {
956 struct vop_kqfilter_args /* {
957 struct vnode *a_vp;
958 struct proc *a_kn;
959 } */ *ap = v;
960 dev_t dev;
961
962 switch (ap->a_vp->v_type) {
963
964 case VCHR:
965 dev = ap->a_vp->v_rdev;
966 return cdev_kqfilter(dev, ap->a_kn);
967 default:
968 /*
969 * Block devices don't support kqfilter, and refuse it
970 * for any other files (like those vflush()ed) too.
971 */
972 return (EOPNOTSUPP);
973 }
974 }
975
976 /*
977 * Allow mapping of only D_DISK. This is called only for VBLK.
978 */
979 int
980 spec_mmap(void *v)
981 {
982 struct vop_mmap_args /* {
983 struct vnode *a_vp;
984 vm_prot_t a_prot;
985 kauth_cred_t a_cred;
986 } */ *ap = v;
987 struct vnode *vp = ap->a_vp;
988
989 KASSERT(vp->v_type == VBLK);
990 if (bdev_type(vp->v_rdev) != D_DISK)
991 return EINVAL;
992
993 return 0;
994 }
995
996 /*
997 * Synch buffers associated with a block device
998 */
999 /* ARGSUSED */
1000 int
1001 spec_fsync(void *v)
1002 {
1003 struct vop_fsync_args /* {
1004 struct vnode *a_vp;
1005 kauth_cred_t a_cred;
1006 int a_flags;
1007 off_t offlo;
1008 off_t offhi;
1009 } */ *ap = v;
1010 struct vnode *vp = ap->a_vp;
1011 struct mount *mp;
1012 int error;
1013
1014 if (vp->v_type == VBLK) {
1015 if ((mp = spec_node_getmountedfs(vp)) != NULL) {
1016 error = VFS_FSYNC(mp, vp, ap->a_flags);
1017 if (error != EOPNOTSUPP)
1018 return error;
1019 }
1020 return vflushbuf(vp, ap->a_flags);
1021 }
1022 return (0);
1023 }
1024
1025 /*
1026 * Just call the device strategy routine
1027 */
1028 int
1029 spec_strategy(void *v)
1030 {
1031 struct vop_strategy_args /* {
1032 struct vnode *a_vp;
1033 struct buf *a_bp;
1034 } */ *ap = v;
1035 struct vnode *vp = ap->a_vp;
1036 struct buf *bp = ap->a_bp;
1037 int error;
1038
1039 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
1040
1041 error = 0;
1042 bp->b_dev = vp->v_rdev;
1043
1044 if (!(bp->b_flags & B_READ))
1045 error = fscow_run(bp, false);
1046
1047 if (error) {
1048 bp->b_error = error;
1049 bp->b_resid = bp->b_bcount;
1050 biodone(bp);
1051 return (error);
1052 }
1053
1054 bdev_strategy(bp);
1055
1056 return (0);
1057 }
1058
1059 int
1060 spec_inactive(void *v)
1061 {
1062 struct vop_inactive_args /* {
1063 struct vnode *a_vp;
1064 struct bool *a_recycle;
1065 } */ *ap = v;
1066 struct vnode *vp = ap->a_vp;
1067
1068 KASSERT(vp->v_mount == dead_rootmount);
1069 *ap->a_recycle = true;
1070 VOP_UNLOCK(vp);
1071 return 0;
1072 }
1073
1074 int
1075 spec_reclaim(void *v)
1076 {
1077 struct vop_reclaim_args /* {
1078 struct vnode *a_vp;
1079 } */ *ap = v;
1080 struct vnode *vp = ap->a_vp;
1081
1082 KASSERT(vp->v_mount == dead_rootmount);
1083 vcache_remove(vp->v_mount, &vp->v_interlock, sizeof(vp->v_interlock));
1084 return 0;
1085 }
1086
1087 /*
1088 * This is a noop, simply returning what one has been given.
1089 */
1090 int
1091 spec_bmap(void *v)
1092 {
1093 struct vop_bmap_args /* {
1094 struct vnode *a_vp;
1095 daddr_t a_bn;
1096 struct vnode **a_vpp;
1097 daddr_t *a_bnp;
1098 int *a_runp;
1099 } */ *ap = v;
1100
1101 if (ap->a_vpp != NULL)
1102 *ap->a_vpp = ap->a_vp;
1103 if (ap->a_bnp != NULL)
1104 *ap->a_bnp = ap->a_bn;
1105 if (ap->a_runp != NULL)
1106 *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
1107 return (0);
1108 }
1109
1110 /*
1111 * Device close routine
1112 */
1113 /* ARGSUSED */
1114 int
1115 spec_close(void *v)
1116 {
1117 struct vop_close_args /* {
1118 struct vnode *a_vp;
1119 int a_fflag;
1120 kauth_cred_t a_cred;
1121 } */ *ap = v;
1122 struct vnode *vp = ap->a_vp;
1123 struct session *sess;
1124 dev_t dev = vp->v_rdev;
1125 int flags = ap->a_fflag;
1126 int mode, error, count;
1127 specnode_t *sn;
1128 specdev_t *sd;
1129
1130 mutex_enter(vp->v_interlock);
1131 sn = vp->v_specnode;
1132 sd = sn->sn_dev;
1133 /*
1134 * If we're going away soon, make this non-blocking.
1135 * Also ensures that we won't wedge in vn_lock below.
1136 */
1137 if (vdead_check(vp, VDEAD_NOWAIT) != 0)
1138 flags |= FNONBLOCK;
1139 mutex_exit(vp->v_interlock);
1140
1141 switch (vp->v_type) {
1142
1143 case VCHR:
1144 /*
1145 * Hack: a tty device that is a controlling terminal
1146 * has a reference from the session structure. We
1147 * cannot easily tell that a character device is a
1148 * controlling terminal, unless it is the closing
1149 * process' controlling terminal. In that case, if the
1150 * open count is 1 release the reference from the
1151 * session. Also, remove the link from the tty back to
1152 * the session and pgrp.
1153 *
1154 * XXX V. fishy.
1155 */
1156 mutex_enter(proc_lock);
1157 sess = curlwp->l_proc->p_session;
1158 if (sn->sn_opencnt == 1 && vp == sess->s_ttyvp) {
1159 mutex_spin_enter(&tty_lock);
1160 sess->s_ttyvp = NULL;
1161 if (sess->s_ttyp->t_session != NULL) {
1162 sess->s_ttyp->t_pgrp = NULL;
1163 sess->s_ttyp->t_session = NULL;
1164 mutex_spin_exit(&tty_lock);
1165 /* Releases proc_lock. */
1166 proc_sessrele(sess);
1167 } else {
1168 mutex_spin_exit(&tty_lock);
1169 if (sess->s_ttyp->t_pgrp != NULL)
1170 panic("spec_close: spurious pgrp ref");
1171 mutex_exit(proc_lock);
1172 }
1173 vrele(vp);
1174 } else
1175 mutex_exit(proc_lock);
1176
1177 /*
1178 * If the vnode is locked, then we are in the midst
1179 * of forcably closing the device, otherwise we only
1180 * close on last reference.
1181 */
1182 mode = S_IFCHR;
1183 break;
1184
1185 case VBLK:
1186 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
1187 /*
1188 * On last close of a block device (that isn't mounted)
1189 * we must invalidate any in core blocks, so that
1190 * we can, for instance, change floppy disks.
1191 */
1192 error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0);
1193 if (error)
1194 return (error);
1195 /*
1196 * We do not want to really close the device if it
1197 * is still in use unless we are trying to close it
1198 * forcibly. Since every use (buffer, vnode, swap, cmap)
1199 * holds a reference to the vnode, and because we mark
1200 * any other vnodes that alias this device, when the
1201 * sum of the reference counts on all the aliased
1202 * vnodes descends to one, we are on last close.
1203 */
1204 mode = S_IFBLK;
1205 break;
1206
1207 default:
1208 panic("spec_close: not special");
1209 }
1210
1211 mutex_enter(&device_lock);
1212 sn->sn_opencnt--;
1213 count = --sd->sd_opencnt;
1214 if (vp->v_type == VBLK)
1215 sd->sd_bdevvp = NULL;
1216 mutex_exit(&device_lock);
1217
1218 if (count != 0)
1219 return 0;
1220
1221 /*
1222 * If we're able to block, release the vnode lock & reacquire. We
1223 * might end up sleeping for someone else who wants our queues. They
1224 * won't get them if we hold the vnode locked.
1225 */
1226 if (!(flags & FNONBLOCK))
1227 VOP_UNLOCK(vp);
1228
1229 if (vp->v_type == VBLK)
1230 error = bdev_close(dev, flags, mode, curlwp);
1231 else
1232 error = cdev_close(dev, flags, mode, curlwp);
1233
1234 if (!(flags & FNONBLOCK))
1235 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1236
1237 return (error);
1238 }
1239
1240 /*
1241 * Print out the contents of a special device vnode.
1242 */
1243 int
1244 spec_print(void *v)
1245 {
1246 struct vop_print_args /* {
1247 struct vnode *a_vp;
1248 } */ *ap = v;
1249
1250 printf("dev %llu, %llu\n", (unsigned long long)major(ap->a_vp->v_rdev),
1251 (unsigned long long)minor(ap->a_vp->v_rdev));
1252 return 0;
1253 }
1254
1255 /*
1256 * Return POSIX pathconf information applicable to special devices.
1257 */
1258 int
1259 spec_pathconf(void *v)
1260 {
1261 struct vop_pathconf_args /* {
1262 struct vnode *a_vp;
1263 int a_name;
1264 register_t *a_retval;
1265 } */ *ap = v;
1266
1267 switch (ap->a_name) {
1268 case _PC_LINK_MAX:
1269 *ap->a_retval = LINK_MAX;
1270 return (0);
1271 case _PC_MAX_CANON:
1272 *ap->a_retval = MAX_CANON;
1273 return (0);
1274 case _PC_MAX_INPUT:
1275 *ap->a_retval = MAX_INPUT;
1276 return (0);
1277 case _PC_PIPE_BUF:
1278 *ap->a_retval = PIPE_BUF;
1279 return (0);
1280 case _PC_CHOWN_RESTRICTED:
1281 *ap->a_retval = 1;
1282 return (0);
1283 case _PC_VDISABLE:
1284 *ap->a_retval = _POSIX_VDISABLE;
1285 return (0);
1286 case _PC_SYNC_IO:
1287 *ap->a_retval = 1;
1288 return (0);
1289 default:
1290 return (EINVAL);
1291 }
1292 /* NOTREACHED */
1293 }
1294
1295 /*
1296 * Advisory record locking support.
1297 */
1298 int
1299 spec_advlock(void *v)
1300 {
1301 struct vop_advlock_args /* {
1302 struct vnode *a_vp;
1303 void *a_id;
1304 int a_op;
1305 struct flock *a_fl;
1306 int a_flags;
1307 } */ *ap = v;
1308 struct vnode *vp = ap->a_vp;
1309
1310 return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
1311 }
1312