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