spec_vnops.c revision 1.153 1 /* $NetBSD: spec_vnops.c,v 1.153 2015/07/01 08:13:52 hannken 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.153 2015/07/01 08:13:52 hannken 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 /*
484 * Open a special file.
485 */
486 /* ARGSUSED */
487 int
488 spec_open(void *v)
489 {
490 struct vop_open_args /* {
491 struct vnode *a_vp;
492 int a_mode;
493 kauth_cred_t a_cred;
494 } */ *ap = v;
495 struct lwp *l;
496 struct vnode *vp;
497 dev_t dev;
498 int error;
499 struct partinfo pi;
500 enum kauth_device_req req;
501 specnode_t *sn;
502 specdev_t *sd;
503
504 u_int gen;
505 const char *name;
506
507 l = curlwp;
508 vp = ap->a_vp;
509 dev = vp->v_rdev;
510 sn = vp->v_specnode;
511 sd = sn->sn_dev;
512 name = NULL;
513 gen = 0;
514
515 /*
516 * Don't allow open if fs is mounted -nodev.
517 */
518 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
519 return (ENXIO);
520
521 switch (ap->a_mode & (FREAD | FWRITE)) {
522 case FREAD | FWRITE:
523 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_RW;
524 break;
525 case FWRITE:
526 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE;
527 break;
528 default:
529 req = KAUTH_REQ_DEVICE_RAWIO_SPEC_READ;
530 break;
531 }
532
533 switch (vp->v_type) {
534 case VCHR:
535 error = kauth_authorize_device_spec(ap->a_cred, req, vp);
536 if (error != 0)
537 return (error);
538
539 /*
540 * Character devices can accept opens from multiple
541 * vnodes.
542 */
543 mutex_enter(&device_lock);
544 if (sn->sn_gone) {
545 mutex_exit(&device_lock);
546 return (EBADF);
547 }
548 sd->sd_opencnt++;
549 sn->sn_opencnt++;
550 mutex_exit(&device_lock);
551 if (cdev_type(dev) == D_TTY)
552 vp->v_vflag |= VV_ISTTY;
553 VOP_UNLOCK(vp);
554 do {
555 const struct cdevsw *cdev;
556
557 gen = module_gen;
558 error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
559 if (error != ENXIO)
560 break;
561
562 /* Check if we already have a valid driver */
563 mutex_enter(&device_lock);
564 cdev = cdevsw_lookup(dev);
565 mutex_exit(&device_lock);
566 if (cdev != NULL)
567 break;
568
569 /* Get device name from devsw_conv array */
570 if ((name = cdevsw_getname(major(dev))) == NULL)
571 break;
572
573 /* Try to autoload device module */
574 (void) module_autoload(name, MODULE_CLASS_DRIVER);
575 } while (gen != module_gen);
576
577 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
578 break;
579
580 case VBLK:
581 error = kauth_authorize_device_spec(ap->a_cred, req, vp);
582 if (error != 0)
583 return (error);
584
585 /*
586 * For block devices, permit only one open. The buffer
587 * cache cannot remain self-consistent with multiple
588 * vnodes holding a block device open.
589 */
590 mutex_enter(&device_lock);
591 if (sn->sn_gone) {
592 mutex_exit(&device_lock);
593 return (EBADF);
594 }
595 if (sd->sd_opencnt != 0) {
596 mutex_exit(&device_lock);
597 return EBUSY;
598 }
599 sn->sn_opencnt = 1;
600 sd->sd_opencnt = 1;
601 sd->sd_bdevvp = vp;
602 mutex_exit(&device_lock);
603 do {
604 const struct bdevsw *bdev;
605
606 gen = module_gen;
607 error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
608 if (error != ENXIO)
609 break;
610
611 /* Check if we already have a valid driver */
612 mutex_enter(&device_lock);
613 bdev = bdevsw_lookup(dev);
614 mutex_exit(&device_lock);
615 if (bdev != NULL)
616 break;
617
618 /* Get device name from devsw_conv array */
619 if ((name = bdevsw_getname(major(dev))) == NULL)
620 break;
621
622 VOP_UNLOCK(vp);
623
624 /* Try to autoload device module */
625 (void) module_autoload(name, MODULE_CLASS_DRIVER);
626
627 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
628 } while (gen != module_gen);
629
630 break;
631
632 case VNON:
633 case VLNK:
634 case VDIR:
635 case VREG:
636 case VBAD:
637 case VFIFO:
638 case VSOCK:
639 default:
640 return 0;
641 }
642
643 mutex_enter(&device_lock);
644 if (sn->sn_gone) {
645 if (error == 0)
646 error = EBADF;
647 } else if (error != 0) {
648 sd->sd_opencnt--;
649 sn->sn_opencnt--;
650 if (vp->v_type == VBLK)
651 sd->sd_bdevvp = NULL;
652
653 }
654 mutex_exit(&device_lock);
655
656 if (cdev_type(dev) != D_DISK || error != 0)
657 return error;
658
659 if (vp->v_type == VCHR)
660 error = cdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
661 else
662 error = bdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
663 if (error == 0)
664 uvm_vnp_setsize(vp,
665 (voff_t)pi.disklab->d_secsize * pi.part->p_size);
666 return 0;
667 }
668
669 /*
670 * Vnode op for read
671 */
672 /* ARGSUSED */
673 int
674 spec_read(void *v)
675 {
676 struct vop_read_args /* {
677 struct vnode *a_vp;
678 struct uio *a_uio;
679 int a_ioflag;
680 kauth_cred_t a_cred;
681 } */ *ap = v;
682 struct vnode *vp = ap->a_vp;
683 struct uio *uio = ap->a_uio;
684 struct lwp *l = curlwp;
685 struct buf *bp;
686 daddr_t bn;
687 int bsize, bscale;
688 struct partinfo dpart;
689 int n, on;
690 int error = 0;
691
692 #ifdef DIAGNOSTIC
693 if (uio->uio_rw != UIO_READ)
694 panic("spec_read mode");
695 if (&uio->uio_vmspace->vm_map != kernel_map &&
696 uio->uio_vmspace != curproc->p_vmspace)
697 panic("spec_read proc");
698 #endif
699 if (uio->uio_resid == 0)
700 return (0);
701
702 switch (vp->v_type) {
703
704 case VCHR:
705 VOP_UNLOCK(vp);
706 error = cdev_read(vp->v_rdev, uio, ap->a_ioflag);
707 vn_lock(vp, LK_SHARED | LK_RETRY);
708 return (error);
709
710 case VBLK:
711 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
712 if (uio->uio_offset < 0)
713 return (EINVAL);
714 bsize = BLKDEV_IOSIZE;
715
716 /*
717 * dholland 20130616: XXX this logic should not be
718 * here. It is here because the old buffer cache
719 * demands that all accesses to the same blocks need
720 * to be the same size; but it only works for FFS and
721 * nowadays I think it'll fail silently if the size
722 * info in the disklabel is wrong. (Or missing.) The
723 * buffer cache needs to be smarter; or failing that
724 * we need a reliable way here to get the right block
725 * size; or a reliable way to guarantee that (a) the
726 * fs is not mounted when we get here and (b) any
727 * buffers generated here will get purged when the fs
728 * does get mounted.
729 */
730 if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
731 if (dpart.part->p_fstype == FS_BSDFFS &&
732 dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
733 bsize = dpart.part->p_frag *
734 dpart.part->p_fsize;
735 }
736
737 bscale = bsize >> DEV_BSHIFT;
738 do {
739 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
740 on = uio->uio_offset % bsize;
741 n = min((unsigned)(bsize - on), uio->uio_resid);
742 error = bread(vp, bn, bsize, 0, &bp);
743 if (error) {
744 return (error);
745 }
746 n = min(n, bsize - bp->b_resid);
747 error = uiomove((char *)bp->b_data + on, n, uio);
748 brelse(bp, 0);
749 } while (error == 0 && uio->uio_resid > 0 && n != 0);
750 return (error);
751
752 default:
753 panic("spec_read type");
754 }
755 /* NOTREACHED */
756 }
757
758 /*
759 * Vnode op for write
760 */
761 /* ARGSUSED */
762 int
763 spec_write(void *v)
764 {
765 struct vop_write_args /* {
766 struct vnode *a_vp;
767 struct uio *a_uio;
768 int a_ioflag;
769 kauth_cred_t a_cred;
770 } */ *ap = v;
771 struct vnode *vp = ap->a_vp;
772 struct uio *uio = ap->a_uio;
773 struct lwp *l = curlwp;
774 struct buf *bp;
775 daddr_t bn;
776 int bsize, bscale;
777 struct partinfo dpart;
778 int n, on;
779 int error = 0;
780
781 #ifdef DIAGNOSTIC
782 if (uio->uio_rw != UIO_WRITE)
783 panic("spec_write mode");
784 if (&uio->uio_vmspace->vm_map != kernel_map &&
785 uio->uio_vmspace != curproc->p_vmspace)
786 panic("spec_write proc");
787 #endif
788
789 switch (vp->v_type) {
790
791 case VCHR:
792 VOP_UNLOCK(vp);
793 error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
794 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
795 return (error);
796
797 case VBLK:
798 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
799 if (uio->uio_resid == 0)
800 return (0);
801 if (uio->uio_offset < 0)
802 return (EINVAL);
803 bsize = BLKDEV_IOSIZE;
804 if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
805 if (dpart.part->p_fstype == FS_BSDFFS &&
806 dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
807 bsize = dpart.part->p_frag *
808 dpart.part->p_fsize;
809 }
810 bscale = bsize >> DEV_BSHIFT;
811 do {
812 bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
813 on = uio->uio_offset % bsize;
814 n = min((unsigned)(bsize - on), uio->uio_resid);
815 if (n == bsize)
816 bp = getblk(vp, bn, bsize, 0, 0);
817 else
818 error = bread(vp, bn, bsize, 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 bool *a_recycle;
1082 } */ *ap = v;
1083 struct vnode *vp = ap->a_vp;
1084
1085 KASSERT(vp->v_mount == dead_rootmount);
1086 *ap->a_recycle = true;
1087 VOP_UNLOCK(vp);
1088 return 0;
1089 }
1090
1091 int
1092 spec_reclaim(void *v)
1093 {
1094 struct vop_reclaim_args /* {
1095 struct vnode *a_vp;
1096 } */ *ap = v;
1097 struct vnode *vp = ap->a_vp;
1098
1099 KASSERT(vp->v_mount == dead_rootmount);
1100 vcache_remove(vp->v_mount, &vp->v_interlock, sizeof(vp->v_interlock));
1101 return 0;
1102 }
1103
1104 /*
1105 * This is a noop, simply returning what one has been given.
1106 */
1107 int
1108 spec_bmap(void *v)
1109 {
1110 struct vop_bmap_args /* {
1111 struct vnode *a_vp;
1112 daddr_t a_bn;
1113 struct vnode **a_vpp;
1114 daddr_t *a_bnp;
1115 int *a_runp;
1116 } */ *ap = v;
1117
1118 if (ap->a_vpp != NULL)
1119 *ap->a_vpp = ap->a_vp;
1120 if (ap->a_bnp != NULL)
1121 *ap->a_bnp = ap->a_bn;
1122 if (ap->a_runp != NULL)
1123 *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
1124 return (0);
1125 }
1126
1127 /*
1128 * Device close routine
1129 */
1130 /* ARGSUSED */
1131 int
1132 spec_close(void *v)
1133 {
1134 struct vop_close_args /* {
1135 struct vnode *a_vp;
1136 int a_fflag;
1137 kauth_cred_t a_cred;
1138 } */ *ap = v;
1139 struct vnode *vp = ap->a_vp;
1140 struct session *sess;
1141 dev_t dev = vp->v_rdev;
1142 int flags = ap->a_fflag;
1143 int mode, error, count;
1144 specnode_t *sn;
1145 specdev_t *sd;
1146
1147 mutex_enter(vp->v_interlock);
1148 sn = vp->v_specnode;
1149 sd = sn->sn_dev;
1150 /*
1151 * If we're going away soon, make this non-blocking.
1152 * Also ensures that we won't wedge in vn_lock below.
1153 */
1154 if (vdead_check(vp, VDEAD_NOWAIT) != 0)
1155 flags |= FNONBLOCK;
1156 mutex_exit(vp->v_interlock);
1157
1158 switch (vp->v_type) {
1159
1160 case VCHR:
1161 /*
1162 * Hack: a tty device that is a controlling terminal
1163 * has a reference from the session structure. We
1164 * cannot easily tell that a character device is a
1165 * controlling terminal, unless it is the closing
1166 * process' controlling terminal. In that case, if the
1167 * open count is 1 release the reference from the
1168 * session. Also, remove the link from the tty back to
1169 * the session and pgrp.
1170 *
1171 * XXX V. fishy.
1172 */
1173 mutex_enter(proc_lock);
1174 sess = curlwp->l_proc->p_session;
1175 if (sn->sn_opencnt == 1 && vp == sess->s_ttyvp) {
1176 mutex_spin_enter(&tty_lock);
1177 sess->s_ttyvp = NULL;
1178 if (sess->s_ttyp->t_session != NULL) {
1179 sess->s_ttyp->t_pgrp = NULL;
1180 sess->s_ttyp->t_session = NULL;
1181 mutex_spin_exit(&tty_lock);
1182 /* Releases proc_lock. */
1183 proc_sessrele(sess);
1184 } else {
1185 mutex_spin_exit(&tty_lock);
1186 if (sess->s_ttyp->t_pgrp != NULL)
1187 panic("spec_close: spurious pgrp ref");
1188 mutex_exit(proc_lock);
1189 }
1190 vrele(vp);
1191 } else
1192 mutex_exit(proc_lock);
1193
1194 /*
1195 * If the vnode is locked, then we are in the midst
1196 * of forcably closing the device, otherwise we only
1197 * close on last reference.
1198 */
1199 mode = S_IFCHR;
1200 break;
1201
1202 case VBLK:
1203 KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
1204 /*
1205 * On last close of a block device (that isn't mounted)
1206 * we must invalidate any in core blocks, so that
1207 * we can, for instance, change floppy disks.
1208 */
1209 error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0);
1210 if (error)
1211 return (error);
1212 /*
1213 * We do not want to really close the device if it
1214 * is still in use unless we are trying to close it
1215 * forcibly. Since every use (buffer, vnode, swap, cmap)
1216 * holds a reference to the vnode, and because we mark
1217 * any other vnodes that alias this device, when the
1218 * sum of the reference counts on all the aliased
1219 * vnodes descends to one, we are on last close.
1220 */
1221 mode = S_IFBLK;
1222 break;
1223
1224 default:
1225 panic("spec_close: not special");
1226 }
1227
1228 mutex_enter(&device_lock);
1229 sn->sn_opencnt--;
1230 count = --sd->sd_opencnt;
1231 if (vp->v_type == VBLK)
1232 sd->sd_bdevvp = NULL;
1233 mutex_exit(&device_lock);
1234
1235 if (count != 0)
1236 return 0;
1237
1238 /*
1239 * If we're able to block, release the vnode lock & reacquire. We
1240 * might end up sleeping for someone else who wants our queues. They
1241 * won't get them if we hold the vnode locked.
1242 */
1243 if (!(flags & FNONBLOCK))
1244 VOP_UNLOCK(vp);
1245
1246 if (vp->v_type == VBLK)
1247 error = bdev_close(dev, flags, mode, curlwp);
1248 else
1249 error = cdev_close(dev, flags, mode, curlwp);
1250
1251 if (!(flags & FNONBLOCK))
1252 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1253
1254 return (error);
1255 }
1256
1257 /*
1258 * Print out the contents of a special device vnode.
1259 */
1260 int
1261 spec_print(void *v)
1262 {
1263 struct vop_print_args /* {
1264 struct vnode *a_vp;
1265 } */ *ap = v;
1266
1267 printf("dev %llu, %llu\n", (unsigned long long)major(ap->a_vp->v_rdev),
1268 (unsigned long long)minor(ap->a_vp->v_rdev));
1269 return 0;
1270 }
1271
1272 /*
1273 * Return POSIX pathconf information applicable to special devices.
1274 */
1275 int
1276 spec_pathconf(void *v)
1277 {
1278 struct vop_pathconf_args /* {
1279 struct vnode *a_vp;
1280 int a_name;
1281 register_t *a_retval;
1282 } */ *ap = v;
1283
1284 switch (ap->a_name) {
1285 case _PC_LINK_MAX:
1286 *ap->a_retval = LINK_MAX;
1287 return (0);
1288 case _PC_MAX_CANON:
1289 *ap->a_retval = MAX_CANON;
1290 return (0);
1291 case _PC_MAX_INPUT:
1292 *ap->a_retval = MAX_INPUT;
1293 return (0);
1294 case _PC_PIPE_BUF:
1295 *ap->a_retval = PIPE_BUF;
1296 return (0);
1297 case _PC_CHOWN_RESTRICTED:
1298 *ap->a_retval = 1;
1299 return (0);
1300 case _PC_VDISABLE:
1301 *ap->a_retval = _POSIX_VDISABLE;
1302 return (0);
1303 case _PC_SYNC_IO:
1304 *ap->a_retval = 1;
1305 return (0);
1306 default:
1307 return (EINVAL);
1308 }
1309 /* NOTREACHED */
1310 }
1311
1312 /*
1313 * Advisory record locking support.
1314 */
1315 int
1316 spec_advlock(void *v)
1317 {
1318 struct vop_advlock_args /* {
1319 struct vnode *a_vp;
1320 void *a_id;
1321 int a_op;
1322 struct flock *a_fl;
1323 int a_flags;
1324 } */ *ap = v;
1325 struct vnode *vp = ap->a_vp;
1326
1327 return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
1328 }
1329