/
spec_vnops.c
1841 lines (1659 loc) · 47 KB
/
spec_vnops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: spec_vnops.c,v 1.215 2022/09/21 10:59:10 riastradh Exp $ */
/*-
* Copyright (c) 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1989, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)spec_vnops.c 8.15 (Berkeley) 7/14/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.215 2022/09/21 10:59:10 riastradh Exp $");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/buf.h>
#include <sys/mount.h>
#include <sys/namei.h>
#include <sys/vnode_impl.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/file.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/lockf.h>
#include <sys/tty.h>
#include <sys/kauth.h>
#include <sys/fstrans.h>
#include <sys/module.h>
#include <sys/atomic.h>
#include <miscfs/genfs/genfs.h>
#include <miscfs/specfs/specdev.h>
/*
* Lock order:
*
* vnode lock
* -> device_lock
* -> struct vnode::v_interlock
*/
/* symbolic sleep message strings for devices */
const char devopn[] = "devopn";
const char devio[] = "devio";
const char devwait[] = "devwait";
const char devin[] = "devin";
const char devout[] = "devout";
const char devioc[] = "devioc";
const char devcls[] = "devcls";
#define SPECHSZ 64
#if ((SPECHSZ&(SPECHSZ-1)) == 0)
#define SPECHASH(rdev) (((rdev>>5)+(rdev))&(SPECHSZ-1))
#else
#define SPECHASH(rdev) (((unsigned)((rdev>>5)+(rdev)))%SPECHSZ)
#endif
static vnode_t *specfs_hash[SPECHSZ];
extern struct mount *dead_rootmount;
/*
* This vnode operations vector is used for special device nodes
* created from whole cloth by the kernel. For the ops vector for
* vnodes built from special devices found in a filesystem, see (e.g)
* ffs_specop_entries[] in ffs_vnops.c or the equivalent for other
* filesystems.
*/
int (**spec_vnodeop_p)(void *);
const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
{ &vop_default_desc, vn_default_error },
{ &vop_parsepath_desc, genfs_parsepath }, /* parsepath */
{ &vop_lookup_desc, spec_lookup }, /* lookup */
{ &vop_create_desc, genfs_badop }, /* create */
{ &vop_mknod_desc, genfs_badop }, /* mknod */
{ &vop_open_desc, spec_open }, /* open */
{ &vop_close_desc, spec_close }, /* close */
{ &vop_access_desc, genfs_ebadf }, /* access */
{ &vop_accessx_desc, genfs_ebadf }, /* accessx */
{ &vop_getattr_desc, genfs_ebadf }, /* getattr */
{ &vop_setattr_desc, genfs_ebadf }, /* setattr */
{ &vop_read_desc, spec_read }, /* read */
{ &vop_write_desc, spec_write }, /* write */
{ &vop_fallocate_desc, genfs_eopnotsupp }, /* fallocate */
{ &vop_fdiscard_desc, spec_fdiscard }, /* fdiscard */
{ &vop_fcntl_desc, genfs_fcntl }, /* fcntl */
{ &vop_ioctl_desc, spec_ioctl }, /* ioctl */
{ &vop_poll_desc, spec_poll }, /* poll */
{ &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
{ &vop_revoke_desc, genfs_revoke }, /* revoke */
{ &vop_mmap_desc, spec_mmap }, /* mmap */
{ &vop_fsync_desc, spec_fsync }, /* fsync */
{ &vop_seek_desc, spec_seek }, /* seek */
{ &vop_remove_desc, genfs_badop }, /* remove */
{ &vop_link_desc, genfs_badop }, /* link */
{ &vop_rename_desc, genfs_badop }, /* rename */
{ &vop_mkdir_desc, genfs_badop }, /* mkdir */
{ &vop_rmdir_desc, genfs_badop }, /* rmdir */
{ &vop_symlink_desc, genfs_badop }, /* symlink */
{ &vop_readdir_desc, genfs_badop }, /* readdir */
{ &vop_readlink_desc, genfs_badop }, /* readlink */
{ &vop_abortop_desc, genfs_badop }, /* abortop */
{ &vop_inactive_desc, spec_inactive }, /* inactive */
{ &vop_reclaim_desc, spec_reclaim }, /* reclaim */
{ &vop_lock_desc, genfs_lock }, /* lock */
{ &vop_unlock_desc, genfs_unlock }, /* unlock */
{ &vop_bmap_desc, spec_bmap }, /* bmap */
{ &vop_strategy_desc, spec_strategy }, /* strategy */
{ &vop_print_desc, spec_print }, /* print */
{ &vop_islocked_desc, genfs_islocked }, /* islocked */
{ &vop_pathconf_desc, spec_pathconf }, /* pathconf */
{ &vop_advlock_desc, spec_advlock }, /* advlock */
{ &vop_bwrite_desc, vn_bwrite }, /* bwrite */
{ &vop_getpages_desc, genfs_getpages }, /* getpages */
{ &vop_putpages_desc, genfs_putpages }, /* putpages */
{ NULL, NULL }
};
const struct vnodeopv_desc spec_vnodeop_opv_desc =
{ &spec_vnodeop_p, spec_vnodeop_entries };
static kauth_listener_t rawio_listener;
static struct kcondvar specfs_iocv;
/* Returns true if vnode is /dev/mem or /dev/kmem. */
bool
iskmemvp(struct vnode *vp)
{
return ((vp->v_type == VCHR) && iskmemdev(vp->v_rdev));
}
/*
* Returns true if dev is /dev/mem or /dev/kmem.
*/
int
iskmemdev(dev_t dev)
{
/* mem_no is emitted by config(8) to generated devsw.c */
extern const int mem_no;
/* minor 14 is /dev/io on i386 with COMPAT_10 */
return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
}
static int
rawio_listener_cb(kauth_cred_t cred, kauth_action_t action, void *cookie,
void *arg0, void *arg1, void *arg2, void *arg3)
{
int result;
result = KAUTH_RESULT_DEFER;
if ((action != KAUTH_DEVICE_RAWIO_SPEC) &&
(action != KAUTH_DEVICE_RAWIO_PASSTHRU))
return result;
/* Access is mandated by permissions. */
result = KAUTH_RESULT_ALLOW;
return result;
}
void
spec_init(void)
{
rawio_listener = kauth_listen_scope(KAUTH_SCOPE_DEVICE,
rawio_listener_cb, NULL);
cv_init(&specfs_iocv, "specio");
}
/*
* spec_io_enter(vp, &sn, &dev)
*
* Enter an operation that may not hold vp's vnode lock or an
* fstrans on vp's mount. Until spec_io_exit, the vnode will not
* be revoked.
*
* On success, set sn to the specnode pointer and dev to the dev_t
* number and return zero. Caller must later call spec_io_exit
* when done.
*
* On failure, return ENXIO -- the device has been revoked and no
* longer exists.
*/
static int
spec_io_enter(struct vnode *vp, struct specnode **snp, dev_t *devp)
{
dev_t dev;
struct specnode *sn;
unsigned iocnt;
int error = 0;
mutex_enter(vp->v_interlock);
/*
* Extract all the info we need from the vnode, unless the
* vnode has already been reclaimed. This can happen if the
* underlying device has been removed and all the device nodes
* for it have been revoked. The caller may not hold a vnode
* lock or fstrans to prevent this from happening before it has
* had an opportunity to notice the vnode is dead.
*/
if (vdead_check(vp, VDEAD_NOWAIT) != 0 ||
(sn = vp->v_specnode) == NULL ||
(dev = vp->v_rdev) == NODEV) {
error = ENXIO;
goto out;
}
/*
* Notify spec_close that we are doing an I/O operation which
* may not be not bracketed by fstrans(9) and thus is not
* blocked by vfs suspension.
*
* We could hold this reference with psref(9) instead, but we
* already have to take the interlock for vdead_check, so
* there's not much more cost here to another atomic operation.
*/
do {
iocnt = atomic_load_relaxed(&sn->sn_dev->sd_iocnt);
if (__predict_false(iocnt == UINT_MAX)) {
/*
* The I/O count is limited by the number of
* LWPs (which will never overflow this) --
* unless one driver uses another driver via
* specfs, which is rather unusual, but which
* could happen via pud(4) userspace drivers.
* We could use a 64-bit count, but can't use
* atomics for that on all platforms.
* (Probably better to switch to psref or
* localcount instead.)
*/
error = EBUSY;
goto out;
}
} while (atomic_cas_uint(&sn->sn_dev->sd_iocnt, iocnt, iocnt + 1)
!= iocnt);
/* Success! */
*snp = sn;
*devp = dev;
error = 0;
out: mutex_exit(vp->v_interlock);
return error;
}
/*
* spec_io_exit(vp, sn)
*
* Exit an operation entered with a successful spec_io_enter --
* allow concurrent spec_node_revoke to proceed. The argument sn
* must match the struct specnode pointer returned by spec_io_exit
* for vp.
*/
static void
spec_io_exit(struct vnode *vp, struct specnode *sn)
{
struct specdev *sd = sn->sn_dev;
unsigned iocnt;
KASSERT(vp->v_specnode == sn);
/*
* We are done. Notify spec_close if appropriate. The
* transition of 1 -> 0 must happen under device_lock so
* spec_close doesn't miss a wakeup.
*/
do {
iocnt = atomic_load_relaxed(&sd->sd_iocnt);
KASSERT(iocnt > 0);
if (iocnt == 1) {
mutex_enter(&device_lock);
if (atomic_dec_uint_nv(&sd->sd_iocnt) == 0)
cv_broadcast(&specfs_iocv);
mutex_exit(&device_lock);
break;
}
} while (atomic_cas_uint(&sd->sd_iocnt, iocnt, iocnt - 1) != iocnt);
}
/*
* spec_io_drain(sd)
*
* Wait for all existing spec_io_enter/exit sections to complete.
* Caller must ensure spec_io_enter will fail at this point.
*/
static void
spec_io_drain(struct specdev *sd)
{
/*
* I/O at the same time as closing is unlikely -- it often
* indicates an application bug.
*/
if (__predict_true(atomic_load_relaxed(&sd->sd_iocnt) == 0))
return;
mutex_enter(&device_lock);
while (atomic_load_relaxed(&sd->sd_iocnt) > 0)
cv_wait(&specfs_iocv, &device_lock);
mutex_exit(&device_lock);
}
/*
* Initialize a vnode that represents a device.
*/
void
spec_node_init(vnode_t *vp, dev_t rdev)
{
specnode_t *sn;
specdev_t *sd;
vnode_t *vp2;
vnode_t **vpp;
KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
KASSERT(vp->v_specnode == NULL);
/*
* Search the hash table for this device. If known, add a
* reference to the device structure. If not known, create
* a new entry to represent the device. In all cases add
* the vnode to the hash table.
*/
sn = kmem_alloc(sizeof(*sn), KM_SLEEP);
sd = kmem_alloc(sizeof(*sd), KM_SLEEP);
mutex_enter(&device_lock);
vpp = &specfs_hash[SPECHASH(rdev)];
for (vp2 = *vpp; vp2 != NULL; vp2 = vp2->v_specnext) {
KASSERT(vp2->v_specnode != NULL);
if (rdev == vp2->v_rdev && vp->v_type == vp2->v_type) {
break;
}
}
if (vp2 == NULL) {
/* No existing record, create a new one. */
sd->sd_rdev = rdev;
sd->sd_mountpoint = NULL;
sd->sd_lockf = NULL;
sd->sd_refcnt = 1;
sd->sd_opencnt = 0;
sd->sd_bdevvp = NULL;
sd->sd_iocnt = 0;
sd->sd_opened = false;
sd->sd_closing = false;
sn->sn_dev = sd;
sd = NULL;
} else {
/* Use the existing record. */
sn->sn_dev = vp2->v_specnode->sn_dev;
sn->sn_dev->sd_refcnt++;
}
/* Insert vnode into the hash chain. */
sn->sn_opencnt = 0;
sn->sn_rdev = rdev;
sn->sn_gone = false;
vp->v_specnode = sn;
vp->v_specnext = *vpp;
*vpp = vp;
mutex_exit(&device_lock);
/* Free the record we allocated if unused. */
if (sd != NULL) {
kmem_free(sd, sizeof(*sd));
}
}
/*
* Lookup a vnode by device number and return it referenced.
*/
int
spec_node_lookup_by_dev(enum vtype type, dev_t dev, int flags, vnode_t **vpp)
{
int error;
vnode_t *vp;
top: mutex_enter(&device_lock);
for (vp = specfs_hash[SPECHASH(dev)]; vp; vp = vp->v_specnext) {
if (type == vp->v_type && dev == vp->v_rdev) {
mutex_enter(vp->v_interlock);
/* If clean or being cleaned, then ignore it. */
if (vdead_check(vp, VDEAD_NOWAIT) == 0)
break;
if ((flags & VDEAD_NOWAIT) == 0) {
mutex_exit(&device_lock);
/*
* It may be being revoked as we speak,
* and the caller wants to wait until
* all revocation has completed. Let
* vcache_vget wait for it to finish
* dying; as a side effect, vcache_vget
* releases vp->v_interlock. Note that
* vcache_vget cannot succeed at this
* point because vdead_check already
* failed.
*/
error = vcache_vget(vp);
KASSERT(error);
goto top;
}
mutex_exit(vp->v_interlock);
}
}
KASSERT(vp == NULL || mutex_owned(vp->v_interlock));
if (vp == NULL) {
mutex_exit(&device_lock);
return ENOENT;
}
/*
* If it is an opened block device return the opened vnode.
*/
if (type == VBLK && vp->v_specnode->sn_dev->sd_bdevvp != NULL) {
mutex_exit(vp->v_interlock);
vp = vp->v_specnode->sn_dev->sd_bdevvp;
mutex_enter(vp->v_interlock);
}
mutex_exit(&device_lock);
error = vcache_vget(vp);
if (error != 0)
return error;
*vpp = vp;
return 0;
}
/*
* Lookup a vnode by file system mounted on and return it referenced.
*/
int
spec_node_lookup_by_mount(struct mount *mp, vnode_t **vpp)
{
int i, error;
vnode_t *vp, *vq;
mutex_enter(&device_lock);
for (i = 0, vq = NULL; i < SPECHSZ && vq == NULL; i++) {
for (vp = specfs_hash[i]; vp; vp = vp->v_specnext) {
if (vp->v_type != VBLK)
continue;
vq = vp->v_specnode->sn_dev->sd_bdevvp;
if (vq != NULL &&
vq->v_specnode->sn_dev->sd_mountpoint == mp)
break;
vq = NULL;
}
}
if (vq == NULL) {
mutex_exit(&device_lock);
return ENOENT;
}
mutex_enter(vq->v_interlock);
mutex_exit(&device_lock);
error = vcache_vget(vq);
if (error != 0)
return error;
*vpp = vq;
return 0;
}
/*
* Get the file system mounted on this block device.
*
* XXX Caller should hold the vnode lock -- shared or exclusive -- so
* that this can't changed, and the vnode can't be revoked while we
* examine it. But not all callers do, and they're scattered through a
* lot of file systems, so we can't assert this yet.
*/
struct mount *
spec_node_getmountedfs(vnode_t *devvp)
{
struct mount *mp;
KASSERT(devvp->v_type == VBLK);
mp = devvp->v_specnode->sn_dev->sd_mountpoint;
return mp;
}
/*
* Set the file system mounted on this block device.
*
* XXX Caller should hold the vnode lock exclusively so this can't be
* changed or assumed by spec_node_getmountedfs while we change it, and
* the vnode can't be revoked while we handle it. But not all callers
* do, and they're scattered through a lot of file systems, so we can't
* assert this yet. Instead, for now, we'll take an I/O reference so
* at least the ioctl doesn't race with revoke/detach.
*
* If you do change this to assert an exclusive vnode lock, you must
* also do vdead_check before trying bdev_ioctl, because the vnode may
* have been revoked by the time the caller locked it, and this is
* _not_ a vop -- calls to spec_node_setmountedfs don't go through
* v_op, so revoking the vnode doesn't prevent further calls.
*
* XXX Caller should additionally have the vnode open, at least if mp
* is nonnull, but I'm not sure all callers do that -- need to audit.
* Currently udf closes the vnode before clearing the mount.
*/
void
spec_node_setmountedfs(vnode_t *devvp, struct mount *mp)
{
struct dkwedge_info dkw;
struct specnode *sn;
dev_t dev;
int error;
KASSERT(devvp->v_type == VBLK);
error = spec_io_enter(devvp, &sn, &dev);
if (error)
return;
KASSERT(sn->sn_dev->sd_mountpoint == NULL || mp == NULL);
sn->sn_dev->sd_mountpoint = mp;
if (mp == NULL)
goto out;
error = bdev_ioctl(dev, DIOCGWEDGEINFO, &dkw, FREAD, curlwp);
if (error)
goto out;
strlcpy(mp->mnt_stat.f_mntfromlabel, dkw.dkw_wname,
sizeof(mp->mnt_stat.f_mntfromlabel));
out: spec_io_exit(devvp, sn);
}
/*
* A vnode representing a special device is going away. Close
* the device if the vnode holds it open.
*/
void
spec_node_revoke(vnode_t *vp)
{
specnode_t *sn;
specdev_t *sd;
struct vnode **vpp;
KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
sn = vp->v_specnode;
sd = sn->sn_dev;
KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
KASSERT(vp->v_specnode != NULL);
KASSERT(sn->sn_gone == false);
mutex_enter(&device_lock);
KASSERTMSG(sn->sn_opencnt <= sd->sd_opencnt,
"sn_opencnt=%u > sd_opencnt=%u",
sn->sn_opencnt, sd->sd_opencnt);
sn->sn_gone = true;
if (sn->sn_opencnt != 0) {
sd->sd_opencnt -= (sn->sn_opencnt - 1);
sn->sn_opencnt = 1;
mutex_exit(&device_lock);
VOP_CLOSE(vp, FNONBLOCK, NOCRED);
mutex_enter(&device_lock);
KASSERT(sn->sn_opencnt == 0);
}
/*
* We may have revoked the vnode in this thread while another
* thread was in the middle of spec_close, in the window when
* spec_close releases the vnode lock to call .d_close for the
* last close. In that case, wait for the concurrent
* spec_close to complete.
*/
while (sd->sd_closing)
cv_wait(&specfs_iocv, &device_lock);
/*
* Remove from the hash so lookups stop returning this
* specnode. We will dissociate it from the specdev -- and
* possibly free the specdev -- in spec_node_destroy.
*/
KASSERT(sn->sn_gone);
KASSERT(sn->sn_opencnt == 0);
for (vpp = &specfs_hash[SPECHASH(vp->v_rdev)];;
vpp = &(*vpp)->v_specnext) {
if (*vpp == vp) {
*vpp = vp->v_specnext;
vp->v_specnext = NULL;
break;
}
}
mutex_exit(&device_lock);
}
/*
* A vnode representing a special device is being recycled.
* Destroy the specfs component.
*/
void
spec_node_destroy(vnode_t *vp)
{
specnode_t *sn;
specdev_t *sd;
int refcnt;
sn = vp->v_specnode;
sd = sn->sn_dev;
KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
KASSERT(vp->v_specnode != NULL);
KASSERT(sn->sn_opencnt == 0);
mutex_enter(&device_lock);
sn = vp->v_specnode;
vp->v_specnode = NULL;
refcnt = sd->sd_refcnt--;
KASSERT(refcnt > 0);
mutex_exit(&device_lock);
/* If the device is no longer in use, destroy our record. */
if (refcnt == 1) {
KASSERT(sd->sd_iocnt == 0);
KASSERT(sd->sd_opencnt == 0);
KASSERT(sd->sd_bdevvp == NULL);
kmem_free(sd, sizeof(*sd));
}
kmem_free(sn, sizeof(*sn));
}
/*
* Trivial lookup routine that always fails.
*/
int
spec_lookup(void *v)
{
struct vop_lookup_v2_args /* {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
} */ *ap = v;
*ap->a_vpp = NULL;
return (ENOTDIR);
}
typedef int (*spec_ioctl_t)(dev_t, u_long, void *, int, struct lwp *);
/*
* Open a special file.
*/
/* ARGSUSED */
int
spec_open(void *v)
{
struct vop_open_args /* {
struct vnode *a_vp;
int a_mode;
kauth_cred_t a_cred;
} */ *ap = v;
struct lwp *l = curlwp;
struct vnode *vp = ap->a_vp;
dev_t dev, dev1;
int error;
enum kauth_device_req req;
specnode_t *sn, *sn1;
specdev_t *sd;
spec_ioctl_t ioctl;
u_int gen = 0;
const char *name = NULL;
bool needclose = false;
struct partinfo pi;
KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
KASSERTMSG(vp->v_type == VBLK || vp->v_type == VCHR, "type=%d",
vp->v_type);
dev = vp->v_rdev;
sn = vp->v_specnode;
sd = sn->sn_dev;
/*
* Don't allow open if fs is mounted -nodev.
*/
if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
return (ENXIO);
switch (ap->a_mode & (FREAD | FWRITE)) {
case FREAD | FWRITE:
req = KAUTH_REQ_DEVICE_RAWIO_SPEC_RW;
break;
case FWRITE:
req = KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE;
break;
default:
req = KAUTH_REQ_DEVICE_RAWIO_SPEC_READ;
break;
}
error = kauth_authorize_device_spec(ap->a_cred, req, vp);
if (error != 0)
return (error);
/*
* Acquire an open reference -- as long as we hold onto it, and
* the vnode isn't revoked, it can't be closed, and the vnode
* can't be revoked until we release the vnode lock.
*/
mutex_enter(&device_lock);
KASSERT(!sn->sn_gone);
switch (vp->v_type) {
case VCHR:
/*
* Character devices can accept opens from multiple
* vnodes. But first, wait for any close to finish.
* Wait under the vnode lock so we don't have to worry
* about the vnode being revoked while we wait.
*/
while (sd->sd_closing) {
error = cv_wait_sig(&specfs_iocv, &device_lock);
if (error)
break;
}
if (error)
break;
sd->sd_opencnt++;
sn->sn_opencnt++;
KASSERTMSG(sn->sn_opencnt <= sd->sd_opencnt,
"sn_opencnt=%u > sd_opencnt=%u",
sn->sn_opencnt, sd->sd_opencnt);
break;
case VBLK:
/*
* For block devices, permit only one open. The buffer
* cache cannot remain self-consistent with multiple
* vnodes holding a block device open.
*
* Treat zero opencnt with non-NULL mountpoint as open.
* This may happen after forced detach of a mounted device.
*
* Also treat sd_closing, meaning there is a concurrent
* close in progress, as still open.
*/
if (sd->sd_opencnt != 0 ||
sd->sd_mountpoint != NULL ||
sd->sd_closing) {
error = EBUSY;
break;
}
KASSERTMSG(sn->sn_opencnt == 0, "sn_opencnt=%u",
sn->sn_opencnt);
sn->sn_opencnt = 1;
sd->sd_opencnt = 1;
sd->sd_bdevvp = vp;
break;
default:
panic("invalid specfs vnode type: %d", vp->v_type);
}
mutex_exit(&device_lock);
if (error)
return error;
/*
* Set VV_ISTTY if this is a tty cdev.
*
* XXX This does the wrong thing if the module has to be
* autoloaded. We should maybe set this after autoloading
* modules and calling .d_open successfully, except (a) we need
* the vnode lock to touch it, and (b) once we acquire the
* vnode lock again, the vnode may have been revoked, and
* deadfs's dead_read needs VV_ISTTY to be already set in order
* to return the right answer. So this needs some additional
* synchronization to be made to work correctly with tty driver
* module autoload. For now, let's just hope it doesn't cause
* too much trouble for a tty from an autoloaded driver module
* to fail with EIO instead of returning EOF.
*/
if (vp->v_type == VCHR) {
if (cdev_type(dev) == D_TTY)
vp->v_vflag |= VV_ISTTY;
}
/*
* Because opening the device may block indefinitely, e.g. when
* opening a tty, and loading a module may cross into many
* other subsystems, we must not hold the vnode lock while
* calling .d_open, so release it now and reacquire it when
* done.
*
* Take an I/O reference so that any concurrent spec_close via
* spec_node_revoke will wait for us to finish calling .d_open.
* The vnode can't be dead at this point because we have it
* locked. Note that if revoked, the driver must interrupt
* .d_open before spec_close starts waiting for I/O to drain so
* this doesn't deadlock.
*/
VOP_UNLOCK(vp);
error = spec_io_enter(vp, &sn1, &dev1);
if (error) {
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
return error;
}
KASSERT(sn1 == sn);
KASSERT(dev1 == dev);
/*
* Open the device. If .d_open returns ENXIO (device not
* configured), the driver may not be loaded, so try
* autoloading a module and then try .d_open again if anything
* got loaded.
*/
switch (vp->v_type) {
case VCHR:
do {
const struct cdevsw *cdev;
gen = module_gen;
error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
if (error != ENXIO)
break;
/* Check if we already have a valid driver */
mutex_enter(&device_lock);
cdev = cdevsw_lookup(dev);
mutex_exit(&device_lock);
if (cdev != NULL)
break;
/* Get device name from devsw_conv array */
if ((name = cdevsw_getname(major(dev))) == NULL)
break;
/* Try to autoload device module */
(void) module_autoload(name, MODULE_CLASS_DRIVER);
} while (gen != module_gen);
break;
case VBLK:
do {
const struct bdevsw *bdev;
gen = module_gen;
error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
if (error != ENXIO)
break;
/* Check if we already have a valid driver */
mutex_enter(&device_lock);
bdev = bdevsw_lookup(dev);
mutex_exit(&device_lock);
if (bdev != NULL)
break;
/* Get device name from devsw_conv array */
if ((name = bdevsw_getname(major(dev))) == NULL)
break;
/* Try to autoload device module */
(void) module_autoload(name, MODULE_CLASS_DRIVER);
} while (gen != module_gen);
break;
default:
__unreachable();
}
/*
* Release the I/O reference now that we have called .d_open,
* and reacquire the vnode lock. At this point, the device may
* have been revoked, so we must tread carefully. However, sn
* and sd remain valid pointers until we drop our reference.
*/
spec_io_exit(vp, sn);
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
KASSERT(vp->v_specnode == sn);
/*
* If it has been revoked since we released the vnode lock and
* reacquired it, then spec_node_revoke has closed it, and we
* must fail with EBADF.
*
* Otherwise, if opening it failed, back out and release the
* open reference. If it was ever successfully opened and we
* got the last reference this way, it's now our job to close
* it. This might happen in the following scenario:
*
* Thread 1 Thread 2
* VOP_OPEN
* ...
* .d_open -> 0 (success)
* acquire vnode lock
* do stuff VOP_OPEN
* release vnode lock ...
* .d_open -> EBUSY
* VOP_CLOSE
* acquire vnode lock
* --sd_opencnt != 0
* => no .d_close
* release vnode lock
* acquire vnode lock
* --sd_opencnt == 0
*
* We can't resolve this by making spec_close wait for .d_open
* to complete before examining sd_opencnt, because .d_open can
* hang indefinitely, e.g. for a tty.
*/
mutex_enter(&device_lock);
if (sn->sn_gone) {
if (error == 0)
error = EBADF;
} else if (error == 0) {
/*
* Device has not been revoked, so our opencnt can't
* have gone away at this point -- transition to
* sn_gone=true happens before transition to
* sn_opencnt=0 in spec_node_revoke.
*/
KASSERT(sd->sd_opencnt);
KASSERT(sn->sn_opencnt);
KASSERTMSG(sn->sn_opencnt <= sd->sd_opencnt,
"sn_opencnt=%u > sd_opencnt=%u",
sn->sn_opencnt, sd->sd_opencnt);
KASSERT(!sd->sd_closing);
sd->sd_opened = true;
} else if (sd->sd_opencnt == 1 && sd->sd_opened) {
/*
* We're the last reference to a _previous_ open even
* though this one failed, so we have to close it.
* Don't decrement the reference count here --
* spec_close will do that.
*/
KASSERT(sn->sn_opencnt == 1);
needclose = true;
} else {
KASSERT(sd->sd_opencnt);
KASSERT(sn->sn_opencnt);
KASSERTMSG(sn->sn_opencnt <= sd->sd_opencnt,
"sn_opencnt=%u > sd_opencnt=%u",
sn->sn_opencnt, sd->sd_opencnt);
sd->sd_opencnt--;
sn->sn_opencnt--;
if (vp->v_type == VBLK)
sd->sd_bdevvp = NULL;
}
mutex_exit(&device_lock);
/*
* If this open failed, but the device was previously opened,