This repository was archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathspec_vnops.c
More file actions
3186 lines (2711 loc) · 87.8 KB
/
Copy pathspec_vnops.c
File metadata and controls
3186 lines (2711 loc) · 87.8 KB
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
/*
* Copyright (c) 2000-2019 Apple Computer, Inc. All rights reserved.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. The rights granted to you under the License
* may not be used to create, or enable the creation or redistribution of,
* unlawful or unlicensed copies of an Apple operating system, or to
* circumvent, violate, or enable the circumvention or violation of, any
* terms of an Apple operating system software license agreement.
*
* Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_OSREFERENCE_LICENSE_HEADER_END@
*/
/* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
/*
* Copyright (c) 1989, 1993, 1995
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.14 (Berkeley) 5/21/95
*/
#include <sys/param.h>
#include <sys/proc_internal.h>
#include <sys/kauth.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/buf_internal.h>
#include <sys/mount_internal.h>
#include <sys/vnode_internal.h>
#include <sys/file_internal.h>
#include <sys/namei.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <sys/ioctl.h>
#include <sys/file.h>
#include <sys/user.h>
#include <sys/malloc.h>
#include <sys/disk.h>
#include <sys/uio_internal.h>
#include <sys/resource.h>
#include <machine/machine_routines.h>
#include <miscfs/specfs/specdev.h>
#include <vfs/vfs_support.h>
#include <vfs/vfs_disk_conditioner.h>
#include <kern/assert.h>
#include <kern/task.h>
#include <kern/sched_prim.h>
#include <kern/thread.h>
#include <kern/policy_internal.h>
#include <kern/timer_call.h>
#include <kern/waitq.h>
#include <pexpert/pexpert.h>
#include <IOKit/IOBSD.h>
#include <sys/kdebug.h>
#include <libkern/section_keywords.h>
#if CONFIG_IO_COMPRESSION_STATS
#include <vfs/vfs_io_compression_stats.h>
#endif /* CONFIG_IO_COMPRESSION_STATS */
/* XXX following three prototypes should be in a header file somewhere */
extern dev_t chrtoblk(dev_t dev);
extern boolean_t iskmemdev(dev_t dev);
extern int bpfkqfilter(dev_t dev, struct knote *kn);
extern int ptsd_kqfilter(dev_t, struct knote *);
extern int ptmx_kqfilter(dev_t, struct knote *);
#if CONFIG_PHYS_WRITE_ACCT
uint64_t kernel_pm_writes; // to track the sync writes occuring during power management transitions
#endif /* CONFIG_PHYS_WRITE_ACCT */
struct vnode *speclisth[SPECHSZ];
/* symbolic sleep message strings for devices */
char devopn[] = "devopn";
char devio[] = "devio";
char devwait[] = "devwait";
char devin[] = "devin";
char devout[] = "devout";
char devioc[] = "devioc";
char devcls[] = "devcls";
#define VOPFUNC int (*)(void *)
int(**spec_vnodeop_p)(void *);
const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
{ .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)vn_default_error },
{ .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)spec_lookup }, /* lookup */
{ .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)err_create }, /* create */
{ .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)err_mknod }, /* mknod */
{ .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)spec_open }, /* open */
{ .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)spec_close }, /* close */
{ .opve_op = &vnop_access_desc, .opve_impl = (VOPFUNC)spec_access }, /* access */
{ .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)spec_getattr }, /* getattr */
{ .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)spec_setattr }, /* setattr */
{ .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)spec_read }, /* read */
{ .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)spec_write }, /* write */
{ .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)spec_ioctl }, /* ioctl */
{ .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)spec_select }, /* select */
{ .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)nop_revoke }, /* revoke */
{ .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)err_mmap }, /* mmap */
{ .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)spec_fsync }, /* fsync */
{ .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)err_remove }, /* remove */
{ .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)err_link }, /* link */
{ .opve_op = &vnop_rename_desc, .opve_impl = (VOPFUNC)err_rename }, /* rename */
{ .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)err_mkdir }, /* mkdir */
{ .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)err_rmdir }, /* rmdir */
{ .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)err_symlink }, /* symlink */
{ .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)err_readdir }, /* readdir */
{ .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)err_readlink }, /* readlink */
{ .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)nop_inactive }, /* inactive */
{ .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)nop_reclaim }, /* reclaim */
{ .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)spec_strategy }, /* strategy */
{ .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)spec_pathconf }, /* pathconf */
{ .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)err_advlock }, /* advlock */
{ .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)spec_bwrite }, /* bwrite */
{ .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein }, /* Pagein */
{ .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout }, /* Pageout */
{ .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile }, /* Copyfile */
{ .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)spec_blktooff }, /* blktooff */
{ .opve_op = &vnop_offtoblk_desc, .opve_impl = (VOPFUNC)spec_offtoblk }, /* offtoblk */
{ .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)spec_blockmap }, /* blockmap */
{ .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (int (*)(void *))NULL }
};
const struct vnodeopv_desc spec_vnodeop_opv_desc =
{ .opv_desc_vector_p = &spec_vnodeop_p, .opv_desc_ops = spec_vnodeop_entries };
static void set_blocksize(vnode_t, dev_t);
#define LOWPRI_TIER1_WINDOW_MSECS 25
#define LOWPRI_TIER2_WINDOW_MSECS 100
#define LOWPRI_TIER3_WINDOW_MSECS 500
#define LOWPRI_TIER1_IO_PERIOD_MSECS 40
#define LOWPRI_TIER2_IO_PERIOD_MSECS 85
#define LOWPRI_TIER3_IO_PERIOD_MSECS 200
#define LOWPRI_TIER1_IO_PERIOD_SSD_MSECS 5
#define LOWPRI_TIER2_IO_PERIOD_SSD_MSECS 15
#define LOWPRI_TIER3_IO_PERIOD_SSD_MSECS 25
int throttle_windows_msecs[THROTTLE_LEVEL_END + 1] = {
0,
LOWPRI_TIER1_WINDOW_MSECS,
LOWPRI_TIER2_WINDOW_MSECS,
LOWPRI_TIER3_WINDOW_MSECS,
};
int throttle_io_period_msecs[THROTTLE_LEVEL_END + 1] = {
0,
LOWPRI_TIER1_IO_PERIOD_MSECS,
LOWPRI_TIER2_IO_PERIOD_MSECS,
LOWPRI_TIER3_IO_PERIOD_MSECS,
};
int throttle_io_period_ssd_msecs[THROTTLE_LEVEL_END + 1] = {
0,
LOWPRI_TIER1_IO_PERIOD_SSD_MSECS,
LOWPRI_TIER2_IO_PERIOD_SSD_MSECS,
LOWPRI_TIER3_IO_PERIOD_SSD_MSECS,
};
int throttled_count[THROTTLE_LEVEL_END + 1];
struct _throttle_io_info_t {
lck_mtx_t throttle_lock;
struct timeval throttle_last_write_timestamp;
struct timeval throttle_min_timer_deadline;
struct timeval throttle_window_start_timestamp[THROTTLE_LEVEL_END + 1]; /* window starts at both the beginning and completion of an I/O */
struct timeval throttle_last_IO_timestamp[THROTTLE_LEVEL_END + 1];
pid_t throttle_last_IO_pid[THROTTLE_LEVEL_END + 1];
struct timeval throttle_start_IO_period_timestamp[THROTTLE_LEVEL_END + 1];
int32_t throttle_inflight_count[THROTTLE_LEVEL_END + 1];
TAILQ_HEAD(, uthread) throttle_uthlist[THROTTLE_LEVEL_END + 1]; /* Lists of throttled uthreads */
int throttle_next_wake_level;
thread_call_t throttle_timer_call;
int32_t throttle_timer_ref;
int32_t throttle_timer_active;
int32_t throttle_io_count;
int32_t throttle_io_count_begin;
int *throttle_io_periods;
uint32_t throttle_io_period_num;
int32_t throttle_refcnt;
int32_t throttle_alloc;
int32_t throttle_disabled;
int32_t throttle_is_fusion_with_priority;
};
struct _throttle_io_info_t _throttle_io_info[LOWPRI_MAX_NUM_DEV];
int lowpri_throttle_enabled = 1;
static void throttle_info_end_io_internal(struct _throttle_io_info_t *info, int throttle_level);
static int throttle_info_update_internal(struct _throttle_io_info_t *info, uthread_t ut, int flags, boolean_t isssd, boolean_t inflight, struct bufattr *bap);
static int throttle_get_thread_throttle_level(uthread_t ut);
static int throttle_get_thread_throttle_level_internal(uthread_t ut, int io_tier);
void throttle_info_mount_reset_period(mount_t mp, int isssd);
/*
* Trivial lookup routine that always fails.
*/
int
spec_lookup(struct vnop_lookup_args *ap)
{
*ap->a_vpp = NULL;
return ENOTDIR;
}
static void
set_blocksize(struct vnode *vp, dev_t dev)
{
int (*size)(dev_t);
int rsize;
if ((major(dev) < nblkdev) && (size = bdevsw[major(dev)].d_psize)) {
rsize = (*size)(dev);
if (rsize <= 0) { /* did size fail? */
vp->v_specsize = DEV_BSIZE;
} else {
vp->v_specsize = rsize;
}
} else {
vp->v_specsize = DEV_BSIZE;
}
}
void
set_fsblocksize(struct vnode *vp)
{
if (vp->v_type == VBLK) {
dev_t dev = (dev_t)vp->v_rdev;
int maj = major(dev);
if ((u_int)maj >= (u_int)nblkdev) {
return;
}
vnode_lock(vp);
set_blocksize(vp, dev);
vnode_unlock(vp);
}
}
/*
* Open a special file.
*/
int
spec_open(struct vnop_open_args *ap)
{
static const char *OPEN_MOUNTED_ENTITLEMENT = "com.apple.private.vfs.open-mounted";
struct proc *p = vfs_context_proc(ap->a_context);
kauth_cred_t cred = vfs_context_ucred(ap->a_context);
struct vnode *vp = ap->a_vp;
dev_t bdev, dev = (dev_t)vp->v_rdev;
int maj = major(dev);
int error;
/*
* Don't allow open if fs is mounted -nodev.
*/
if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV)) {
return ENXIO;
}
switch (vp->v_type) {
case VCHR:
if ((u_int)maj >= (u_int)nchrdev) {
return ENXIO;
}
if (cred != FSCRED && (ap->a_mode & FWRITE)) {
#if 0
/*
* When running in very secure mode, do not allow
* opens for writing of any disk character devices.
*/
if (securelevel >= 2 && isdisk(dev, VCHR)) {
return EPERM;
}
#endif
/* Never allow writing to /dev/mem or /dev/kmem */
if (iskmemdev(dev)) {
return EPERM;
}
/*
* When running in secure mode, do not allow opens for
* writing of character devices whose corresponding block
* devices are currently mounted.
*/
if (securelevel >= 1) {
if ((bdev = chrtoblk(dev)) != NODEV && check_mountedon(bdev, VBLK, &error)) {
return error;
}
}
}
devsw_lock(dev, S_IFCHR);
error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, p);
if (error == 0) {
vp->v_specinfo->si_opencount++;
}
devsw_unlock(dev, S_IFCHR);
if (error == 0 && cdevsw[maj].d_type == D_DISK && !vp->v_un.vu_specinfo->si_initted) {
int isssd = 0;
uint64_t throttle_mask = 0;
uint32_t devbsdunit = 0;
if (VNOP_IOCTL(vp, DKIOCGETTHROTTLEMASK, (caddr_t)&throttle_mask, 0, NULL) == 0) {
if (throttle_mask != 0 &&
VNOP_IOCTL(vp, DKIOCISSOLIDSTATE, (caddr_t)&isssd, 0, ap->a_context) == 0) {
/*
* as a reasonable approximation, only use the lowest bit of the mask
* to generate a disk unit number
*/
devbsdunit = num_trailing_0(throttle_mask);
vnode_lock(vp);
vp->v_un.vu_specinfo->si_isssd = isssd ? 1 : 0;
vp->v_un.vu_specinfo->si_devbsdunit = devbsdunit;
vp->v_un.vu_specinfo->si_throttle_mask = throttle_mask;
vp->v_un.vu_specinfo->si_throttleable = 1;
vp->v_un.vu_specinfo->si_initted = 1;
vnode_unlock(vp);
}
}
if (vp->v_un.vu_specinfo->si_initted == 0) {
vnode_lock(vp);
vp->v_un.vu_specinfo->si_initted = 1;
vnode_unlock(vp);
}
}
return error;
case VBLK:
if ((u_int)maj >= (u_int)nblkdev) {
return ENXIO;
}
/*
* When running in very secure mode, do not allow
* opens for writing of any disk block devices.
*/
if (securelevel >= 2 && cred != FSCRED &&
(ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK) {
return EPERM;
}
/*
* Do not allow opens of block devices that are
* currently mounted.
*/
if (!IOTaskHasEntitlement(current_task(), OPEN_MOUNTED_ENTITLEMENT)) {
if ((error = vfs_mountedon(vp))) {
return error;
}
}
devsw_lock(dev, S_IFBLK);
error = (*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, p);
if (!error) {
vp->v_specinfo->si_opencount++;
}
devsw_unlock(dev, S_IFBLK);
if (!error) {
u_int64_t blkcnt;
u_int32_t blksize;
int setsize = 0;
u_int32_t size512 = 512;
if (!VNOP_IOCTL(vp, DKIOCGETBLOCKSIZE, (caddr_t)&blksize, 0, ap->a_context)) {
/* Switch to 512 byte sectors (temporarily) */
if (!VNOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&size512, FWRITE, ap->a_context)) {
/* Get the number of 512 byte physical blocks. */
if (!VNOP_IOCTL(vp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, ap->a_context)) {
setsize = 1;
}
}
/* If it doesn't set back, we can't recover */
if (VNOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&blksize, FWRITE, ap->a_context)) {
error = ENXIO;
}
}
vnode_lock(vp);
set_blocksize(vp, dev);
/*
* Cache the size in bytes of the block device for later
* use by spec_write().
*/
if (setsize) {
vp->v_specdevsize = blkcnt * (u_int64_t)size512;
} else {
vp->v_specdevsize = (u_int64_t)0; /* Default: Can't get */
}
vnode_unlock(vp);
}
return error;
default:
panic("spec_open type");
}
return 0;
}
/*
* Vnode op for read
*/
int
spec_read(struct vnop_read_args *ap)
{
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
struct buf *bp;
daddr64_t bn, nextbn;
long bscale;
int devBlockSize = 0;
size_t bsize, n, on;
int error = 0;
dev_t dev;
#if DIAGNOSTIC
if (uio->uio_rw != UIO_READ) {
panic("spec_read mode");
}
if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) {
panic("spec_read proc");
}
#endif
if (uio_resid(uio) == 0) {
return 0;
}
switch (vp->v_type) {
case VCHR:
{
struct _throttle_io_info_t *throttle_info = NULL;
int thread_throttle_level;
uint64_t blkno = 0;
uint32_t iolen = 0;
int ddisk = 0;
int ktrace_code = DKIO_READ;
devBlockSize = vp->v_specsize;
uintptr_t our_id;
if (cdevsw[major(vp->v_rdev)].d_type == D_DISK) {
ddisk = 1;
}
if (ddisk && vp->v_un.vu_specinfo->si_throttleable) {
throttle_info = &_throttle_io_info[vp->v_un.vu_specinfo->si_devbsdunit];
thread_throttle_level = throttle_info_update_internal(throttle_info, NULL, 0, vp->v_un.vu_specinfo->si_isssd, TRUE, NULL);
}
if (kdebug_enable && ddisk) {
if (devBlockSize == 0) {
devBlockSize = 512; // default sector size
}
if (uio_offset(uio) && devBlockSize) {
blkno = ((uint64_t) uio_offset(uio) / ((uint64_t)devBlockSize));
}
iolen = (int) uio_resid(uio);
our_id = (uintptr_t)thread_tid(current_thread());
KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
(FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id,
vp->v_rdev, blkno, iolen, 0);
}
error = (*cdevsw[major(vp->v_rdev)].d_read)
(vp->v_rdev, uio, ap->a_ioflag);
if (kdebug_enable && ddisk) {
uint32_t residual = (uint32_t)uio_resid(uio);
ktrace_code |= DKIO_DONE;
KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
(FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id,
(uintptr_t)VM_KERNEL_ADDRPERM(vp), residual, error, 0);
}
if (throttle_info) {
throttle_info_end_io_internal(throttle_info, thread_throttle_level);
}
return error;
}
case VBLK:
if (uio->uio_offset < 0) {
return EINVAL;
}
dev = vp->v_rdev;
devBlockSize = vp->v_specsize;
if (devBlockSize > PAGE_SIZE) {
return EINVAL;
}
bscale = PAGE_SIZE / devBlockSize;
bsize = bscale * devBlockSize;
do {
on = uio->uio_offset % bsize;
bn = (daddr64_t)((uio->uio_offset / devBlockSize) & ~(bscale - 1));
if (vp->v_speclastr + bscale == bn) {
nextbn = bn + bscale;
error = buf_breadn(vp, bn, (int)bsize, &nextbn,
(int *)&bsize, 1, NOCRED, &bp);
} else {
error = buf_bread(vp, bn, (int)bsize, NOCRED, &bp);
}
vnode_lock(vp);
vp->v_speclastr = bn;
vnode_unlock(vp);
n = bsize - buf_resid(bp);
if ((on > n) || error) {
if (!error) {
error = EINVAL;
}
buf_brelse(bp);
return error;
}
n = MIN((n - on), (size_t)uio_resid(uio));
error = uiomove((char *)buf_dataptr(bp) + on, (int)n, uio);
if (n + on == bsize) {
buf_markaged(bp);
}
buf_brelse(bp);
} while (error == 0 && uio_resid(uio) > 0 && n != 0);
return error;
default:
panic("spec_read type");
}
/* NOTREACHED */
return 0;
}
/*
* Vnode op for write
*/
int
spec_write(struct vnop_write_args *ap)
{
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
struct buf *bp;
daddr64_t bn;
int blkmask, bscale;
int io_sync;
int devBlockSize = 0;
size_t bsize, n, on;
int error = 0;
dev_t dev;
#if DIAGNOSTIC
if (uio->uio_rw != UIO_WRITE) {
panic("spec_write mode");
}
if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) {
panic("spec_write proc");
}
#endif
switch (vp->v_type) {
case VCHR:
{
struct _throttle_io_info_t *throttle_info = NULL;
int thread_throttle_level;
dev = vp->v_rdev;
devBlockSize = vp->v_specsize;
uint32_t iolen = 0;
uint64_t blkno = 0;
int ddisk = 0;
int ktrace_code = 0; // write is implied; read must be OR'd in.
uintptr_t our_id;
if (cdevsw[major(dev)].d_type == D_DISK) {
ddisk = 1;
}
if (ddisk && vp->v_un.vu_specinfo->si_throttleable) {
throttle_info = &_throttle_io_info[vp->v_un.vu_specinfo->si_devbsdunit];
thread_throttle_level = throttle_info_update_internal(throttle_info, NULL, 0, vp->v_un.vu_specinfo->si_isssd, TRUE, NULL);
microuptime(&throttle_info->throttle_last_write_timestamp);
}
if (kdebug_enable && ddisk) {
if (devBlockSize == 0) {
devBlockSize = 512; // default sector size
}
if ((uio_offset(uio) != 0) && devBlockSize) {
blkno = ((uint64_t)uio_offset(uio)) / ((uint64_t)devBlockSize);
}
iolen = (int)uio_resid(uio);
our_id = (uintptr_t)thread_tid(current_thread());
KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
(FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id,
vp->v_rdev, blkno, iolen, 0);
}
error = (*cdevsw[major(vp->v_rdev)].d_write)
(vp->v_rdev, uio, ap->a_ioflag);
if (kdebug_enable && ddisk) {
//emit the I/O completion
uint32_t residual = (uint32_t)uio_resid(uio);
ktrace_code |= DKIO_DONE;
KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON,
(FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id,
(uintptr_t)VM_KERNEL_ADDRPERM(vp), residual, error, 0);
}
if (throttle_info) {
throttle_info_end_io_internal(throttle_info, thread_throttle_level);
}
return error;
}
case VBLK:
if (uio_resid(uio) == 0) {
return 0;
}
if (uio->uio_offset < 0) {
return EINVAL;
}
io_sync = (ap->a_ioflag & IO_SYNC);
dev = (vp->v_rdev);
devBlockSize = vp->v_specsize;
if (devBlockSize > PAGE_SIZE) {
return EINVAL;
}
bscale = PAGE_SIZE / devBlockSize;
blkmask = bscale - 1;
bsize = bscale * devBlockSize;
do {
bn = (daddr64_t)((uio->uio_offset / devBlockSize) & ~blkmask);
on = uio->uio_offset % bsize;
n = MIN((bsize - on), (size_t)uio_resid(uio));
/*
* Use buf_getblk() as an optimization IFF:
*
* 1) We are reading exactly a block on a block
* aligned boundary
* 2) We know the size of the device from spec_open
* 3) The read doesn't span the end of the device
*
* Otherwise, we fall back on buf_bread().
*/
if (n == bsize &&
vp->v_specdevsize != (u_int64_t)0 &&
(uio->uio_offset + (u_int64_t)n) > vp->v_specdevsize) {
/* reduce the size of the read to what is there */
n = (uio->uio_offset + (u_int64_t)n) - vp->v_specdevsize;
}
if (n == bsize) {
bp = buf_getblk(vp, bn, (int)bsize, 0, 0, BLK_WRITE);
} else {
error = (int)buf_bread(vp, bn, (int)bsize, NOCRED, &bp);
}
/* Translate downstream error for upstream, if needed */
if (!error) {
error = (int)buf_error(bp);
}
if (error) {
buf_brelse(bp);
return error;
}
n = MIN(n, bsize - buf_resid(bp));
error = uiomove((char *)buf_dataptr(bp) + on, (int)n, uio);
if (error) {
buf_brelse(bp);
return error;
}
buf_markaged(bp);
if (io_sync) {
error = buf_bwrite(bp);
} else {
if ((n + on) == bsize) {
error = buf_bawrite(bp);
} else {
error = buf_bdwrite(bp);
}
}
} while (error == 0 && uio_resid(uio) > 0 && n != 0);
return error;
default:
panic("spec_write type");
}
/* NOTREACHED */
return 0;
}
/*
* Device ioctl operation.
*/
int
spec_ioctl(struct vnop_ioctl_args *ap)
{
proc_t p = vfs_context_proc(ap->a_context);
dev_t dev = ap->a_vp->v_rdev;
int retval = 0;
KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL, 0) | DBG_FUNC_START,
dev, ap->a_command, ap->a_fflag, ap->a_vp->v_type, 0);
switch (ap->a_vp->v_type) {
case VCHR:
retval = (*cdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data,
ap->a_fflag, p);
break;
case VBLK:
retval = (*bdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data, ap->a_fflag, p);
if (!retval && ap->a_command == DKIOCSETBLOCKSIZE) {
ap->a_vp->v_specsize = *(uint32_t *)ap->a_data;
}
break;
default:
panic("spec_ioctl");
/* NOTREACHED */
}
KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL, 0) | DBG_FUNC_END,
dev, ap->a_command, ap->a_fflag, retval, 0);
return retval;
}
int
spec_select(struct vnop_select_args *ap)
{
proc_t p = vfs_context_proc(ap->a_context);
dev_t dev;
switch (ap->a_vp->v_type) {
default:
return 1; /* XXX */
case VCHR:
dev = ap->a_vp->v_rdev;
return (*cdevsw[major(dev)].d_select)(dev, ap->a_which, ap->a_wql, p);
}
}
static int filt_specattach(struct knote *kn, struct kevent_qos_s *kev);
int
spec_kqfilter(vnode_t vp, struct knote *kn, struct kevent_qos_s *kev)
{
dev_t dev;
assert(vnode_ischr(vp));
dev = vnode_specrdev(vp);
#if NETWORKING
/*
* Try a bpf device, as defined in bsd/net/bpf.c
* If it doesn't error out the attach, then it
* claimed it. Otherwise, fall through and try
* other attaches.
*/
int32_t tmp_flags = kn->kn_flags;
int64_t tmp_sdata = kn->kn_sdata;
int res;
res = bpfkqfilter(dev, kn);
if ((kn->kn_flags & EV_ERROR) == 0) {
return res;
}
kn->kn_flags = tmp_flags;
kn->kn_sdata = tmp_sdata;
#endif
if (major(dev) > nchrdev) {
knote_set_error(kn, ENXIO);
return 0;
}
kn->kn_vnode_kqok = !!(cdevsw_flags[major(dev)] & CDEVSW_SELECT_KQUEUE);
kn->kn_vnode_use_ofst = !!(cdevsw_flags[major(dev)] & CDEVSW_USE_OFFSET);
if (cdevsw_flags[major(dev)] & CDEVSW_IS_PTS) {
kn->kn_filtid = EVFILTID_PTSD;
return ptsd_kqfilter(dev, kn);
} else if (cdevsw_flags[major(dev)] & CDEVSW_IS_PTC) {
kn->kn_filtid = EVFILTID_PTMX;
return ptmx_kqfilter(dev, kn);
} else if (cdevsw[major(dev)].d_type == D_TTY && kn->kn_vnode_kqok) {
/*
* TTYs from drivers that use struct ttys use their own filter
* routines. The PTC driver doesn't use the tty for character
* counts, so it must go through the select fallback.
*/
kn->kn_filtid = EVFILTID_TTY;
return knote_fops(kn)->f_attach(kn, kev);
}
/* Try to attach to other char special devices */
return filt_specattach(kn, kev);
}
/*
* Synch buffers associated with a block device
*/
int
spec_fsync_internal(vnode_t vp, int waitfor, __unused vfs_context_t context)
{
if (vp->v_type == VCHR) {
return 0;
}
/*
* Flush all dirty buffers associated with a block device.
*/
buf_flushdirtyblks(vp, (waitfor == MNT_WAIT || waitfor == MNT_DWAIT), 0, "spec_fsync");
return 0;
}
int
spec_fsync(struct vnop_fsync_args *ap)
{
return spec_fsync_internal(ap->a_vp, ap->a_waitfor, ap->a_context);
}
/*
* Just call the device strategy routine
*/
void throttle_init(void);
#if 0
#define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...) \
do { \
if ((debug_info)->alloc) \
printf("%s: "format, __FUNCTION__, ## args); \
} while(0)
#else
#define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...)
#endif
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier1_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER1], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier2_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER2], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier3_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER3], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier1_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER1], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier2_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER2], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier3_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER3], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier1_io_period_ssd_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_ssd_msecs[THROTTLE_LEVEL_TIER1], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier2_io_period_ssd_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_ssd_msecs[THROTTLE_LEVEL_TIER2], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier3_io_period_ssd_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_ssd_msecs[THROTTLE_LEVEL_TIER3], 0, "");
SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &lowpri_throttle_enabled, 0, "");
static LCK_GRP_DECLARE(throttle_lock_grp, "throttle I/O");
/*
* throttled I/O helper function
* convert the index of the lowest set bit to a device index
*/
int
num_trailing_0(uint64_t n)
{
/*
* since in most cases the number of trailing 0s is very small,
* we simply counting sequentially from the lowest bit
*/
if (n == 0) {
return sizeof(n) * 8;
}
int count = 0;
while (!ISSET(n, 1)) {
n >>= 1;
++count;
}
return count;
}
/*
* Release the reference and if the item was allocated and this is the last
* reference then free it.
*
* This routine always returns the old value.
*/
static int
throttle_info_rel(struct _throttle_io_info_t *info)
{
SInt32 oldValue = OSDecrementAtomic(&info->throttle_refcnt);
DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
info, (int)(oldValue - 1), info );
/* The reference count just went negative, very bad */
if (oldValue == 0) {
panic("throttle info ref cnt went negative!");
}
/*
* Once reference count is zero, no one else should be able to take a
* reference
*/
if ((info->throttle_refcnt == 0) && (info->throttle_alloc)) {
DEBUG_ALLOC_THROTTLE_INFO("Freeing info = %p\n", info);