2 * Copyright (c) 2000-2012 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1989, 1993, 1995
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
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. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)spec_vnops.c 8.14 (Berkeley) 5/21/95
64 #include <sys/param.h>
65 #include <sys/proc_internal.h>
66 #include <sys/kauth.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
70 #include <sys/buf_internal.h>
71 #include <sys/mount_internal.h>
72 #include <sys/vnode_internal.h>
73 #include <sys/file_internal.h>
74 #include <sys/namei.h>
76 #include <sys/errno.h>
77 #include <sys/ioctl.h>
80 #include <sys/malloc.h>
82 #include <sys/uio_internal.h>
83 #include <sys/resource.h>
84 #include <miscfs/specfs/specdev.h>
85 #include <vfs/vfs_support.h>
86 #include <kern/assert.h>
87 #include <kern/task.h>
88 #include <pexpert/pexpert.h>
90 #include <sys/kdebug.h>
92 /* XXX following three prototypes should be in a header file somewhere */
93 extern dev_t
chrtoblk(dev_t dev
);
94 extern int iskmemdev(dev_t dev
);
95 extern int bpfkqfilter(dev_t dev
, struct knote
*kn
);
96 extern int ptsd_kqfilter(dev_t dev
, struct knote
*kn
);
98 extern int ignore_is_ssd
;
100 struct vnode
*speclisth
[SPECHSZ
];
102 /* symbolic sleep message strings for devices */
103 char devopn
[] = "devopn";
104 char devio
[] = "devio";
105 char devwait
[] = "devwait";
106 char devin
[] = "devin";
107 char devout
[] = "devout";
108 char devioc
[] = "devioc";
109 char devcls
[] = "devcls";
111 #define VOPFUNC int (*)(void *)
113 int (**spec_vnodeop_p
)(void *);
114 struct vnodeopv_entry_desc spec_vnodeop_entries
[] = {
115 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
116 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
117 { &vnop_create_desc
, (VOPFUNC
)err_create
}, /* create */
118 { &vnop_mknod_desc
, (VOPFUNC
)err_mknod
}, /* mknod */
119 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
120 { &vnop_close_desc
, (VOPFUNC
)spec_close
}, /* close */
121 { &vnop_access_desc
, (VOPFUNC
)spec_access
}, /* access */
122 { &vnop_getattr_desc
, (VOPFUNC
)spec_getattr
}, /* getattr */
123 { &vnop_setattr_desc
, (VOPFUNC
)spec_setattr
}, /* setattr */
124 { &vnop_read_desc
, (VOPFUNC
)spec_read
}, /* read */
125 { &vnop_write_desc
, (VOPFUNC
)spec_write
}, /* write */
126 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
127 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
128 { &vnop_revoke_desc
, (VOPFUNC
)nop_revoke
}, /* revoke */
129 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
130 { &vnop_fsync_desc
, (VOPFUNC
)spec_fsync
}, /* fsync */
131 { &vnop_remove_desc
, (VOPFUNC
)err_remove
}, /* remove */
132 { &vnop_link_desc
, (VOPFUNC
)err_link
}, /* link */
133 { &vnop_rename_desc
, (VOPFUNC
)err_rename
}, /* rename */
134 { &vnop_mkdir_desc
, (VOPFUNC
)err_mkdir
}, /* mkdir */
135 { &vnop_rmdir_desc
, (VOPFUNC
)err_rmdir
}, /* rmdir */
136 { &vnop_symlink_desc
, (VOPFUNC
)err_symlink
}, /* symlink */
137 { &vnop_readdir_desc
, (VOPFUNC
)err_readdir
}, /* readdir */
138 { &vnop_readlink_desc
, (VOPFUNC
)err_readlink
}, /* readlink */
139 { &vnop_inactive_desc
, (VOPFUNC
)nop_inactive
}, /* inactive */
140 { &vnop_reclaim_desc
, (VOPFUNC
)nop_reclaim
}, /* reclaim */
141 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
142 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
143 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
144 { &vnop_bwrite_desc
, (VOPFUNC
)spec_bwrite
}, /* bwrite */
145 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
146 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
147 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
148 { &vnop_blktooff_desc
, (VOPFUNC
)spec_blktooff
}, /* blktooff */
149 { &vnop_offtoblk_desc
, (VOPFUNC
)spec_offtoblk
}, /* offtoblk */
150 { &vnop_blockmap_desc
, (VOPFUNC
)spec_blockmap
}, /* blockmap */
151 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
153 struct vnodeopv_desc spec_vnodeop_opv_desc
=
154 { &spec_vnodeop_p
, spec_vnodeop_entries
};
157 static void set_blocksize(vnode_t
, dev_t
);
159 #define LOWPRI_TIER1_WINDOW_MSECS 25
160 #define LOWPRI_TIER2_WINDOW_MSECS 100
161 #define LOWPRI_TIER3_WINDOW_MSECS 500
163 #define LOWPRI_TIER1_IO_PERIOD_MSECS 15
164 #define LOWPRI_TIER2_IO_PERIOD_MSECS 50
165 #define LOWPRI_TIER3_IO_PERIOD_MSECS 200
167 #define LOWPRI_TIER1_IO_PERIOD_SSD_MSECS 5
168 #define LOWPRI_TIER2_IO_PERIOD_SSD_MSECS 15
169 #define LOWPRI_TIER3_IO_PERIOD_SSD_MSECS 25
172 int throttle_windows_msecs
[THROTTLE_LEVEL_END
+ 1] = {
174 LOWPRI_TIER1_WINDOW_MSECS
,
175 LOWPRI_TIER2_WINDOW_MSECS
,
176 LOWPRI_TIER3_WINDOW_MSECS
,
179 int throttle_io_period_msecs
[THROTTLE_LEVEL_END
+ 1] = {
181 LOWPRI_TIER1_IO_PERIOD_MSECS
,
182 LOWPRI_TIER2_IO_PERIOD_MSECS
,
183 LOWPRI_TIER3_IO_PERIOD_MSECS
,
186 int throttle_io_period_ssd_msecs
[THROTTLE_LEVEL_END
+ 1] = {
188 LOWPRI_TIER1_IO_PERIOD_SSD_MSECS
,
189 LOWPRI_TIER2_IO_PERIOD_SSD_MSECS
,
190 LOWPRI_TIER3_IO_PERIOD_SSD_MSECS
,
194 int throttled_count
[THROTTLE_LEVEL_END
+ 1];
196 struct _throttle_io_info_t
{
197 lck_mtx_t throttle_lock
;
199 struct timeval throttle_last_write_timestamp
;
200 struct timeval throttle_min_timer_deadline
;
201 struct timeval throttle_window_start_timestamp
[THROTTLE_LEVEL_END
+ 1];
202 struct timeval throttle_last_IO_timestamp
[THROTTLE_LEVEL_END
+ 1];
203 pid_t throttle_last_IO_pid
[THROTTLE_LEVEL_END
+ 1];
204 struct timeval throttle_start_IO_period_timestamp
[THROTTLE_LEVEL_END
+ 1];
206 TAILQ_HEAD( , uthread
) throttle_uthlist
[THROTTLE_LEVEL_END
+ 1]; /* Lists of throttled uthreads */
207 int throttle_next_wake_level
;
209 thread_call_t throttle_timer_call
;
210 int32_t throttle_timer_ref
;
211 int32_t throttle_timer_active
;
213 int32_t throttle_io_count
;
214 int32_t throttle_io_count_begin
;
215 int *throttle_io_periods
;
216 uint32_t throttle_io_period_num
;
218 int32_t throttle_refcnt
;
219 int32_t throttle_alloc
;
222 struct _throttle_io_info_t _throttle_io_info
[LOWPRI_MAX_NUM_DEV
];
225 int lowpri_throttle_enabled
= 1;
229 static void throttle_info_update_internal(struct _throttle_io_info_t
*info
, uthread_t ut
, int flags
, boolean_t isssd
);
230 static int throttle_get_thread_throttle_level(uthread_t ut
);
233 * Trivial lookup routine that always fails.
236 spec_lookup(struct vnop_lookup_args
*ap
)
244 set_blocksize(struct vnode
*vp
, dev_t dev
)
249 if ((major(dev
) < nblkdev
) && (size
= bdevsw
[major(dev
)].d_psize
)) {
250 rsize
= (*size
)(dev
);
251 if (rsize
<= 0) /* did size fail? */
252 vp
->v_specsize
= DEV_BSIZE
;
254 vp
->v_specsize
= rsize
;
257 vp
->v_specsize
= DEV_BSIZE
;
261 set_fsblocksize(struct vnode
*vp
)
264 if (vp
->v_type
== VBLK
) {
265 dev_t dev
= (dev_t
)vp
->v_rdev
;
266 int maj
= major(dev
);
268 if ((u_int
)maj
>= (u_int
)nblkdev
)
272 set_blocksize(vp
, dev
);
280 * Open a special file.
283 spec_open(struct vnop_open_args
*ap
)
285 struct proc
*p
= vfs_context_proc(ap
->a_context
);
286 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
287 struct vnode
*vp
= ap
->a_vp
;
288 dev_t bdev
, dev
= (dev_t
)vp
->v_rdev
;
289 int maj
= major(dev
);
293 * Don't allow open if fs is mounted -nodev.
295 if (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_NODEV
))
298 switch (vp
->v_type
) {
301 if ((u_int
)maj
>= (u_int
)nchrdev
)
303 if (cred
!= FSCRED
&& (ap
->a_mode
& FWRITE
)) {
305 * When running in very secure mode, do not allow
306 * opens for writing of any disk character devices.
308 if (securelevel
>= 2 && isdisk(dev
, VCHR
))
311 * When running in secure mode, do not allow opens
312 * for writing of /dev/mem, /dev/kmem, or character
313 * devices whose corresponding block devices are
316 if (securelevel
>= 1) {
317 if ((bdev
= chrtoblk(dev
)) != NODEV
&& check_mountedon(bdev
, VBLK
, &error
))
324 devsw_lock(dev
, S_IFCHR
);
325 error
= (*cdevsw
[maj
].d_open
)(dev
, ap
->a_mode
, S_IFCHR
, p
);
328 vp
->v_specinfo
->si_opencount
++;
331 devsw_unlock(dev
, S_IFCHR
);
333 if (error
== 0 && cdevsw
[maj
].d_type
== D_DISK
&& !vp
->v_un
.vu_specinfo
->si_initted
) {
335 uint64_t throttle_mask
= 0;
336 uint32_t devbsdunit
= 0;
338 if (VNOP_IOCTL(vp
, DKIOCGETTHROTTLEMASK
, (caddr_t
)&throttle_mask
, 0, NULL
) == 0) {
340 if (throttle_mask
!= 0 &&
341 VNOP_IOCTL(vp
, DKIOCISSOLIDSTATE
, (caddr_t
)&isssd
, 0, ap
->a_context
) == 0) {
343 * as a reasonable approximation, only use the lowest bit of the mask
344 * to generate a disk unit number
346 devbsdunit
= num_trailing_0(throttle_mask
);
350 vp
->v_un
.vu_specinfo
->si_isssd
= isssd
;
351 vp
->v_un
.vu_specinfo
->si_devbsdunit
= devbsdunit
;
352 vp
->v_un
.vu_specinfo
->si_throttle_mask
= throttle_mask
;
353 vp
->v_un
.vu_specinfo
->si_throttleable
= 1;
354 vp
->v_un
.vu_specinfo
->si_initted
= 1;
359 if (vp
->v_un
.vu_specinfo
->si_initted
== 0) {
361 vp
->v_un
.vu_specinfo
->si_initted
= 1;
368 if ((u_int
)maj
>= (u_int
)nblkdev
)
371 * When running in very secure mode, do not allow
372 * opens for writing of any disk block devices.
374 if (securelevel
>= 2 && cred
!= FSCRED
&&
375 (ap
->a_mode
& FWRITE
) && bdevsw
[maj
].d_type
== D_DISK
)
378 * Do not allow opens of block devices that are
381 if ( (error
= vfs_mountedon(vp
)) )
384 devsw_lock(dev
, S_IFBLK
);
385 error
= (*bdevsw
[maj
].d_open
)(dev
, ap
->a_mode
, S_IFBLK
, p
);
387 vp
->v_specinfo
->si_opencount
++;
389 devsw_unlock(dev
, S_IFBLK
);
395 u_int32_t size512
= 512;
398 if (!VNOP_IOCTL(vp
, DKIOCGETBLOCKSIZE
, (caddr_t
)&blksize
, 0, ap
->a_context
)) {
399 /* Switch to 512 byte sectors (temporarily) */
401 if (!VNOP_IOCTL(vp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&size512
, FWRITE
, ap
->a_context
)) {
402 /* Get the number of 512 byte physical blocks. */
403 if (!VNOP_IOCTL(vp
, DKIOCGETBLOCKCOUNT
, (caddr_t
)&blkcnt
, 0, ap
->a_context
)) {
407 /* If it doesn't set back, we can't recover */
408 if (VNOP_IOCTL(vp
, DKIOCSETBLOCKSIZE
, (caddr_t
)&blksize
, FWRITE
, ap
->a_context
))
414 set_blocksize(vp
, dev
);
417 * Cache the size in bytes of the block device for later
418 * use by spec_write().
421 vp
->v_specdevsize
= blkcnt
* (u_int64_t
)size512
;
423 vp
->v_specdevsize
= (u_int64_t
)0; /* Default: Can't get */
430 panic("spec_open type");
439 spec_read(struct vnop_read_args
*ap
)
441 struct vnode
*vp
= ap
->a_vp
;
442 struct uio
*uio
= ap
->a_uio
;
444 daddr64_t bn
, nextbn
;
452 if (uio
->uio_rw
!= UIO_READ
)
453 panic("spec_read mode");
454 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
455 panic("spec_read proc");
457 if (uio_resid(uio
) == 0)
460 switch (vp
->v_type
) {
463 if (cdevsw
[major(vp
->v_rdev
)].d_type
== D_DISK
&& vp
->v_un
.vu_specinfo
->si_throttleable
) {
464 struct _throttle_io_info_t
*throttle_info
;
466 throttle_info
= &_throttle_io_info
[vp
->v_un
.vu_specinfo
->si_devbsdunit
];
468 throttle_info_update_internal(throttle_info
, NULL
, 0, vp
->v_un
.vu_specinfo
->si_isssd
);
470 error
= (*cdevsw
[major(vp
->v_rdev
)].d_read
)
471 (vp
->v_rdev
, uio
, ap
->a_ioflag
);
476 if (uio
->uio_offset
< 0)
481 devBlockSize
= vp
->v_specsize
;
483 if (devBlockSize
> PAGE_SIZE
)
486 bscale
= PAGE_SIZE
/ devBlockSize
;
487 bsize
= bscale
* devBlockSize
;
490 on
= uio
->uio_offset
% bsize
;
492 bn
= (daddr64_t
)((uio
->uio_offset
/ devBlockSize
) &~ (bscale
- 1));
494 if (vp
->v_speclastr
+ bscale
== bn
) {
495 nextbn
= bn
+ bscale
;
496 error
= buf_breadn(vp
, bn
, (int)bsize
, &nextbn
,
497 (int *)&bsize
, 1, NOCRED
, &bp
);
499 error
= buf_bread(vp
, bn
, (int)bsize
, NOCRED
, &bp
);
502 vp
->v_speclastr
= bn
;
505 n
= bsize
- buf_resid(bp
);
506 if ((on
> n
) || error
) {
512 n
= min((unsigned)(n
- on
), uio_resid(uio
));
514 error
= uiomove((char *)buf_dataptr(bp
) + on
, n
, uio
);
518 } while (error
== 0 && uio_resid(uio
) > 0 && n
!= 0);
522 panic("spec_read type");
533 spec_write(struct vnop_write_args
*ap
)
535 struct vnode
*vp
= ap
->a_vp
;
536 struct uio
*uio
= ap
->a_uio
;
539 int bsize
, blkmask
, bscale
;
547 if (uio
->uio_rw
!= UIO_WRITE
)
548 panic("spec_write mode");
549 if (UIO_SEG_IS_USER_SPACE(uio
->uio_segflg
))
550 panic("spec_write proc");
553 switch (vp
->v_type
) {
556 if (cdevsw
[major(vp
->v_rdev
)].d_type
== D_DISK
&& vp
->v_un
.vu_specinfo
->si_throttleable
) {
557 struct _throttle_io_info_t
*throttle_info
;
559 throttle_info
= &_throttle_io_info
[vp
->v_un
.vu_specinfo
->si_devbsdunit
];
561 throttle_info_update_internal(throttle_info
, NULL
, 0, vp
->v_un
.vu_specinfo
->si_isssd
);
563 microuptime(&throttle_info
->throttle_last_write_timestamp
);
565 error
= (*cdevsw
[major(vp
->v_rdev
)].d_write
)
566 (vp
->v_rdev
, uio
, ap
->a_ioflag
);
571 if (uio_resid(uio
) == 0)
573 if (uio
->uio_offset
< 0)
576 io_sync
= (ap
->a_ioflag
& IO_SYNC
);
580 devBlockSize
= vp
->v_specsize
;
581 if (devBlockSize
> PAGE_SIZE
)
584 bscale
= PAGE_SIZE
/ devBlockSize
;
585 blkmask
= bscale
- 1;
586 bsize
= bscale
* devBlockSize
;
590 bn
= (daddr64_t
)((uio
->uio_offset
/ devBlockSize
) &~ blkmask
);
591 on
= uio
->uio_offset
% bsize
;
593 n
= min((unsigned)(bsize
- on
), uio_resid(uio
));
596 * Use buf_getblk() as an optimization IFF:
598 * 1) We are reading exactly a block on a block
600 * 2) We know the size of the device from spec_open
601 * 3) The read doesn't span the end of the device
603 * Otherwise, we fall back on buf_bread().
606 vp
->v_specdevsize
!= (u_int64_t
)0 &&
607 (uio
->uio_offset
+ (u_int64_t
)n
) > vp
->v_specdevsize
) {
608 /* reduce the size of the read to what is there */
609 n
= (uio
->uio_offset
+ (u_int64_t
)n
) - vp
->v_specdevsize
;
613 bp
= buf_getblk(vp
, bn
, bsize
, 0, 0, BLK_WRITE
);
615 error
= (int)buf_bread(vp
, bn
, bsize
, NOCRED
, &bp
);
617 /* Translate downstream error for upstream, if needed */
619 error
= (int)buf_error(bp
);
624 n
= min(n
, bsize
- buf_resid(bp
));
626 error
= uiomove((char *)buf_dataptr(bp
) + on
, n
, uio
);
634 error
= buf_bwrite(bp
);
636 if ((n
+ on
) == bsize
)
637 error
= buf_bawrite(bp
);
639 error
= buf_bdwrite(bp
);
641 } while (error
== 0 && uio_resid(uio
) > 0 && n
!= 0);
645 panic("spec_write type");
653 * Device ioctl operation.
656 spec_ioctl(struct vnop_ioctl_args
*ap
)
658 proc_t p
= vfs_context_proc(ap
->a_context
);
659 dev_t dev
= ap
->a_vp
->v_rdev
;
662 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 0) | DBG_FUNC_START
,
663 (unsigned int)dev
, (unsigned int)ap
->a_command
, (unsigned int)ap
->a_fflag
, (unsigned int)ap
->a_vp
->v_type
, 0);
665 switch (ap
->a_vp
->v_type
) {
668 retval
= (*cdevsw
[major(dev
)].d_ioctl
)(dev
, ap
->a_command
, ap
->a_data
,
674 if (ap
->a_command
== DKIOCUNMAP
) {
679 unmap
= (dk_unmap_t
*)ap
->a_data
;
680 extent
= unmap
->extents
;
682 for (i
= 0; i
< unmap
->extentsCount
; i
++, extent
++) {
683 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 1) | DBG_FUNC_NONE
, dev
, extent
->offset
/ap
->a_vp
->v_specsize
, extent
->length
, 0, 0);
687 retval
= (*bdevsw
[major(dev
)].d_ioctl
)(dev
, ap
->a_command
, ap
->a_data
, ap
->a_fflag
, p
);
694 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL
, 0) | DBG_FUNC_END
,
695 (unsigned int)dev
, (unsigned int)ap
->a_command
, (unsigned int)ap
->a_fflag
, retval
, 0);
701 spec_select(struct vnop_select_args
*ap
)
703 proc_t p
= vfs_context_proc(ap
->a_context
);
706 switch (ap
->a_vp
->v_type
) {
709 return (1); /* XXX */
712 dev
= ap
->a_vp
->v_rdev
;
713 return (*cdevsw
[major(dev
)].d_select
)(dev
, ap
->a_which
, ap
->a_wql
, p
);
717 static int filt_specattach(struct knote
*kn
);
720 spec_kqfilter(vnode_t vp
, struct knote
*kn
)
726 * For a few special kinds of devices, we can attach knotes.
727 * Each filter function must check whether the dev type matches it.
729 dev
= vnode_specrdev(vp
);
731 if (vnode_istty(vp
)) {
732 /* We can hook into TTYs... */
733 err
= filt_specattach(kn
);
736 /* Try a bpf device, as defined in bsd/net/bpf.c */
737 err
= bpfkqfilter(dev
, kn
);
745 * Synch buffers associated with a block device
748 spec_fsync_internal(vnode_t vp
, int waitfor
, __unused vfs_context_t context
)
750 if (vp
->v_type
== VCHR
)
753 * Flush all dirty buffers associated with a block device.
755 buf_flushdirtyblks(vp
, (waitfor
== MNT_WAIT
|| waitfor
== MNT_DWAIT
), 0, "spec_fsync");
761 spec_fsync(struct vnop_fsync_args
*ap
)
763 return spec_fsync_internal(ap
->a_vp
, ap
->a_waitfor
, ap
->a_context
);
768 * Just call the device strategy routine
770 void throttle_init(void);
774 #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...) \
776 if ((debug_info)->alloc) \
777 printf("%s: "format, __FUNCTION__, ## args); \
781 #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...)
785 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
786 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
787 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_window_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
789 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier1_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER1
], 0, "");
790 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier2_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER2
], 0, "");
791 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_tier3_io_period_msecs
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &throttle_io_period_msecs
[THROTTLE_LEVEL_TIER3
], 0, "");
793 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, "");
794 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, "");
795 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, "");
797 SYSCTL_INT(_debug
, OID_AUTO
, lowpri_throttle_enabled
, CTLFLAG_RW
| CTLFLAG_LOCKED
, &lowpri_throttle_enabled
, 0, "");
800 static lck_grp_t
*throttle_mtx_grp
;
801 static lck_attr_t
*throttle_mtx_attr
;
802 static lck_grp_attr_t
*throttle_mtx_grp_attr
;
806 * throttled I/O helper function
807 * convert the index of the lowest set bit to a device index
810 num_trailing_0(uint64_t n
)
813 * since in most cases the number of trailing 0s is very small,
814 * we simply counting sequentially from the lowest bit
817 return sizeof(n
) * 8;
819 while (!ISSET(n
, 1)) {
828 * Release the reference and if the item was allocated and this is the last
829 * reference then free it.
831 * This routine always returns the old value.
834 throttle_info_rel(struct _throttle_io_info_t
*info
)
836 SInt32 oldValue
= OSDecrementAtomic(&info
->throttle_refcnt
);
838 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
839 info
, (int)(oldValue
-1), info
);
841 /* The reference count just went negative, very bad */
843 panic("throttle info ref cnt went negative!");
846 * Once reference count is zero, no one else should be able to take a
849 if ((info
->throttle_refcnt
== 0) && (info
->throttle_alloc
)) {
850 DEBUG_ALLOC_THROTTLE_INFO("Freeing info = %p\n", info
);
852 lck_mtx_destroy(&info
->throttle_lock
, throttle_mtx_grp
);
860 * Just take a reference on the throttle info structure.
862 * This routine always returns the old value.
865 throttle_info_ref(struct _throttle_io_info_t
*info
)
867 SInt32 oldValue
= OSIncrementAtomic(&info
->throttle_refcnt
);
869 DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n",
870 info
, (int)(oldValue
-1), info
);
871 /* Allocated items should never have a reference of zero */
872 if (info
->throttle_alloc
&& (oldValue
== 0))
873 panic("Taking a reference without calling create throttle info!\n");
879 * on entry the throttle_lock is held...
880 * this function is responsible for taking
881 * and dropping the reference on the info
882 * structure which will keep it from going
883 * away while the timer is running if it
884 * happens to have been dynamically allocated by
885 * a network fileystem kext which is now trying
889 throttle_timer_start(struct _throttle_io_info_t
*info
, boolean_t update_io_count
, int wakelevel
)
891 struct timeval elapsed
;
893 struct timeval period
;
894 uint64_t elapsed_msecs
;
898 boolean_t throttled
= FALSE
;
899 boolean_t need_timer
= FALSE
;
903 if (update_io_count
== TRUE
) {
904 info
->throttle_io_count_begin
= info
->throttle_io_count
;
905 info
->throttle_io_period_num
++;
907 while (wakelevel
>= THROTTLE_LEVEL_THROTTLED
)
908 info
->throttle_start_IO_period_timestamp
[wakelevel
--] = now
;
910 info
->throttle_min_timer_deadline
= now
;
912 msecs
= info
->throttle_io_periods
[THROTTLE_LEVEL_THROTTLED
];
913 period
.tv_sec
= msecs
/ 1000;
914 period
.tv_usec
= (msecs
% 1000) * 1000;
916 timevaladd(&info
->throttle_min_timer_deadline
, &period
);
918 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< THROTTLE_LEVEL_END
; throttle_level
++) {
921 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
922 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
924 for (level
= throttle_level
+ 1; level
<= THROTTLE_LEVEL_END
; level
++) {
926 if (!TAILQ_EMPTY(&info
->throttle_uthlist
[level
])) {
928 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[level
]) {
930 * we had an I/O occur at a higher priority tier within
931 * this tier's throttle window
936 * we assume that the windows are the same or longer
937 * as we drop through the throttling tiers... thus
938 * we can stop looking once we run into a tier with
939 * threads to schedule regardless of whether it's
940 * still in its throttling window or not
945 if (throttled
== TRUE
)
948 if (throttled
== TRUE
) {
949 uint64_t deadline
= 0;
950 struct timeval target
;
951 struct timeval min_target
;
954 * we've got at least one tier still in a throttled window
955 * so we need a timer running... compute the next deadline
958 for (level
= throttle_level
+1; level
<= THROTTLE_LEVEL_END
; level
++) {
960 if (TAILQ_EMPTY(&info
->throttle_uthlist
[level
]))
963 target
= info
->throttle_start_IO_period_timestamp
[level
];
965 msecs
= info
->throttle_io_periods
[level
];
966 period
.tv_sec
= msecs
/ 1000;
967 period
.tv_usec
= (msecs
% 1000) * 1000;
969 timevaladd(&target
, &period
);
971 if (need_timer
== FALSE
|| timevalcmp(&target
, &min_target
, <)) {
976 if (timevalcmp(&info
->throttle_min_timer_deadline
, &now
, >)) {
977 if (timevalcmp(&info
->throttle_min_timer_deadline
, &min_target
, >))
978 min_target
= info
->throttle_min_timer_deadline
;
981 if (info
->throttle_timer_active
) {
982 if (thread_call_cancel(info
->throttle_timer_call
) == FALSE
) {
984 * couldn't kill the timer because it's already
985 * been dispatched, so don't try to start a new
986 * one... once we drop the lock, the timer will
987 * proceed and eventually re-run this function
991 info
->throttle_timer_active
= 0;
993 if (need_timer
== TRUE
) {
995 * This is defined as an int (32-bit) rather than a 64-bit
996 * value because it would need a really big period in the
997 * order of ~500 days to overflow this. So, we let this be
998 * 32-bit which allows us to use the clock_interval_to_deadline()
1003 if (info
->throttle_timer_ref
== 0) {
1005 * take a reference for the timer
1007 throttle_info_ref(info
);
1009 info
->throttle_timer_ref
= 1;
1011 elapsed
= min_target
;
1012 timevalsub(&elapsed
, &now
);
1013 target_msecs
= elapsed
.tv_sec
* 1000 + elapsed
.tv_usec
/ 1000;
1015 if (target_msecs
<= 0) {
1017 * we may have computed a deadline slightly in the past
1018 * due to various factors... if so, just set the timer
1019 * to go off in the near future (we don't need to be precise)
1023 clock_interval_to_deadline(target_msecs
, 1000000, &deadline
);
1025 thread_call_enter_delayed(info
->throttle_timer_call
, deadline
);
1026 info
->throttle_timer_active
= 1;
1029 return (throttle_level
);
1034 throttle_timer(struct _throttle_io_info_t
*info
)
1036 uthread_t ut
, utlist
;
1037 struct timeval elapsed
;
1039 uint64_t elapsed_msecs
;
1043 caddr_t wake_address
= NULL
;
1044 boolean_t update_io_count
= FALSE
;
1045 boolean_t need_wakeup
= FALSE
;
1046 boolean_t need_release
= FALSE
;
1049 lck_mtx_lock(&info
->throttle_lock
);
1051 info
->throttle_timer_active
= 0;
1055 timevalsub(&elapsed
, &info
->throttle_start_IO_period_timestamp
[THROTTLE_LEVEL_THROTTLED
]);
1056 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1058 if (elapsed_msecs
>= (uint64_t)info
->throttle_io_periods
[THROTTLE_LEVEL_THROTTLED
]) {
1060 wake_level
= info
->throttle_next_wake_level
;
1062 for (level
= THROTTLE_LEVEL_START
; level
< THROTTLE_LEVEL_END
; level
++) {
1065 timevalsub(&elapsed
, &info
->throttle_start_IO_period_timestamp
[wake_level
]);
1066 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1068 if (elapsed_msecs
>= (uint64_t)info
->throttle_io_periods
[wake_level
] && !TAILQ_EMPTY(&info
->throttle_uthlist
[wake_level
])) {
1070 * we're closing out the current IO period...
1071 * if we have a waiting thread, wake it up
1072 * after we have reset the I/O window info
1075 update_io_count
= TRUE
;
1077 info
->throttle_next_wake_level
= wake_level
- 1;
1079 if (info
->throttle_next_wake_level
== THROTTLE_LEVEL_START
)
1080 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1086 if (wake_level
== THROTTLE_LEVEL_START
)
1087 wake_level
= THROTTLE_LEVEL_END
;
1090 if (need_wakeup
== TRUE
) {
1091 if (!TAILQ_EMPTY(&info
->throttle_uthlist
[wake_level
])) {
1093 ut
= (uthread_t
)TAILQ_FIRST(&info
->throttle_uthlist
[wake_level
]);
1094 TAILQ_REMOVE(&info
->throttle_uthlist
[wake_level
], ut
, uu_throttlelist
);
1095 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1097 wake_address
= (caddr_t
)&ut
->uu_on_throttlelist
;
1100 wake_level
= THROTTLE_LEVEL_START
;
1102 throttle_level
= throttle_timer_start(info
, update_io_count
, wake_level
);
1104 if (wake_address
!= NULL
)
1105 wakeup(wake_address
);
1107 for (level
= THROTTLE_LEVEL_THROTTLED
; level
<= throttle_level
; level
++) {
1109 TAILQ_FOREACH_SAFE(ut
, &info
->throttle_uthlist
[level
], uu_throttlelist
, utlist
) {
1111 TAILQ_REMOVE(&info
->throttle_uthlist
[level
], ut
, uu_throttlelist
);
1112 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1114 wakeup(&ut
->uu_on_throttlelist
);
1117 if (info
->throttle_timer_active
== 0 && info
->throttle_timer_ref
) {
1118 info
->throttle_timer_ref
= 0;
1119 need_release
= TRUE
;
1121 lck_mtx_unlock(&info
->throttle_lock
);
1123 if (need_release
== TRUE
)
1124 throttle_info_rel(info
);
1129 throttle_add_to_list(struct _throttle_io_info_t
*info
, uthread_t ut
, int mylevel
, boolean_t insert_tail
)
1131 boolean_t start_timer
= FALSE
;
1132 int level
= THROTTLE_LEVEL_START
;
1134 if (TAILQ_EMPTY(&info
->throttle_uthlist
[mylevel
])) {
1135 info
->throttle_start_IO_period_timestamp
[mylevel
] = info
->throttle_last_IO_timestamp
[mylevel
];
1139 if (insert_tail
== TRUE
)
1140 TAILQ_INSERT_TAIL(&info
->throttle_uthlist
[mylevel
], ut
, uu_throttlelist
);
1142 TAILQ_INSERT_HEAD(&info
->throttle_uthlist
[mylevel
], ut
, uu_throttlelist
);
1144 ut
->uu_on_throttlelist
= mylevel
;
1146 if (start_timer
== TRUE
) {
1147 /* we may need to start or rearm the timer */
1148 level
= throttle_timer_start(info
, FALSE
, THROTTLE_LEVEL_START
);
1150 if (level
== THROTTLE_LEVEL_END
) {
1151 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1152 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1154 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1162 throttle_init_throttle_window(void)
1164 int throttle_window_size
;
1167 * The hierarchy of throttle window values is as follows:
1169 * - Device tree properties
1171 * All values are specified in msecs.
1174 /* Override global values with device-tree properties */
1175 if (PE_get_default("kern.io_throttle_window_tier1", &throttle_window_size
, sizeof(throttle_window_size
)))
1176 throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
] = throttle_window_size
;
1178 if (PE_get_default("kern.io_throttle_window_tier2", &throttle_window_size
, sizeof(throttle_window_size
)))
1179 throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
] = throttle_window_size
;
1181 if (PE_get_default("kern.io_throttle_window_tier3", &throttle_window_size
, sizeof(throttle_window_size
)))
1182 throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
] = throttle_window_size
;
1184 /* Override with boot-args */
1185 if (PE_parse_boot_argn("io_throttle_window_tier1", &throttle_window_size
, sizeof(throttle_window_size
)))
1186 throttle_windows_msecs
[THROTTLE_LEVEL_TIER1
] = throttle_window_size
;
1188 if (PE_parse_boot_argn("io_throttle_window_tier2", &throttle_window_size
, sizeof(throttle_window_size
)))
1189 throttle_windows_msecs
[THROTTLE_LEVEL_TIER2
] = throttle_window_size
;
1191 if (PE_parse_boot_argn("io_throttle_window_tier3", &throttle_window_size
, sizeof(throttle_window_size
)))
1192 throttle_windows_msecs
[THROTTLE_LEVEL_TIER3
] = throttle_window_size
;
1196 throttle_init_throttle_period(struct _throttle_io_info_t
*info
, boolean_t isssd
)
1198 int throttle_period_size
;
1201 * The hierarchy of throttle period values is as follows:
1203 * - Device tree properties
1205 * All values are specified in msecs.
1208 /* Assign global defaults */
1210 info
->throttle_io_periods
= &throttle_io_period_ssd_msecs
[0];
1212 info
->throttle_io_periods
= &throttle_io_period_msecs
[0];
1214 /* Override global values with device-tree properties */
1215 if (PE_get_default("kern.io_throttle_period_tier1", &throttle_period_size
, sizeof(throttle_period_size
)))
1216 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER1
] = throttle_period_size
;
1218 if (PE_get_default("kern.io_throttle_period_tier2", &throttle_period_size
, sizeof(throttle_period_size
)))
1219 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER2
] = throttle_period_size
;
1221 if (PE_get_default("kern.io_throttle_period_tier3", &throttle_period_size
, sizeof(throttle_period_size
)))
1222 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER3
] = throttle_period_size
;
1224 /* Override with boot-args */
1225 if (PE_parse_boot_argn("io_throttle_period_tier1", &throttle_period_size
, sizeof(throttle_period_size
)))
1226 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER1
] = throttle_period_size
;
1228 if (PE_parse_boot_argn("io_throttle_period_tier2", &throttle_period_size
, sizeof(throttle_period_size
)))
1229 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER2
] = throttle_period_size
;
1231 if (PE_parse_boot_argn("io_throttle_period_tier3", &throttle_period_size
, sizeof(throttle_period_size
)))
1232 info
->throttle_io_periods
[THROTTLE_LEVEL_TIER3
] = throttle_period_size
;
1239 struct _throttle_io_info_t
*info
;
1244 * allocate lock group attribute and group
1246 throttle_mtx_grp_attr
= lck_grp_attr_alloc_init();
1247 throttle_mtx_grp
= lck_grp_alloc_init("throttle I/O", throttle_mtx_grp_attr
);
1249 /* Update throttle parameters based on device tree configuration */
1250 throttle_init_throttle_window();
1253 * allocate the lock attribute
1255 throttle_mtx_attr
= lck_attr_alloc_init();
1257 for (i
= 0; i
< LOWPRI_MAX_NUM_DEV
; i
++) {
1258 info
= &_throttle_io_info
[i
];
1260 lck_mtx_init(&info
->throttle_lock
, throttle_mtx_grp
, throttle_mtx_attr
);
1261 info
->throttle_timer_call
= thread_call_allocate((thread_call_func_t
)throttle_timer
, (thread_call_param_t
)info
);
1263 for (level
= 0; level
<= THROTTLE_LEVEL_END
; level
++) {
1264 TAILQ_INIT(&info
->throttle_uthlist
[level
]);
1265 info
->throttle_last_IO_pid
[level
] = 0;
1267 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1272 sys_override_io_throttle(int flag
)
1274 if (flag
== THROTTLE_IO_ENABLE
)
1275 lowpri_throttle_enabled
= 1;
1276 if (flag
== THROTTLE_IO_DISABLE
)
1277 lowpri_throttle_enabled
= 0;
1280 int rethrottle_removed_from_list
= 0;
1281 int rethrottle_moved_to_new_list
= 0;
1284 * move a throttled thread to the appropriate state based
1285 * on it's new throttle level... throttle_add_to_list will
1286 * reset the timer deadline if necessary... it may also
1287 * leave the thread off of the queue if we're already outside
1288 * the throttle window for the new level
1289 * takes a valid uthread (which may or may not be on the
1290 * throttle queue) as input
1292 * NOTE: This is called with the task lock held.
1296 rethrottle_thread(uthread_t ut
)
1298 struct _throttle_io_info_t
*info
;
1301 if ((info
= ut
->uu_throttle_info
) == NULL
)
1304 lck_mtx_lock(&info
->throttle_lock
);
1306 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1308 my_new_level
= throttle_get_thread_throttle_level(ut
);
1310 if (my_new_level
!= ut
->uu_on_throttlelist
) {
1312 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1313 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1315 if (my_new_level
>= THROTTLE_LEVEL_THROTTLED
) {
1316 throttle_add_to_list(info
, ut
, my_new_level
, TRUE
);
1317 rethrottle_moved_to_new_list
++;
1320 /* Thread no longer in window, need to wake it up */
1321 if (ut
->uu_on_throttlelist
== THROTTLE_LEVEL_NONE
) {
1322 wakeup(&ut
->uu_on_throttlelist
);
1323 rethrottle_removed_from_list
++;
1328 lck_mtx_unlock(&info
->throttle_lock
);
1335 * Create and take a reference on a throttle info structure and return a
1336 * pointer for the file system to use when calling throttle_info_update.
1337 * Calling file system must have a matching release for every create.
1340 throttle_info_create(void)
1342 struct _throttle_io_info_t
*info
;
1345 MALLOC(info
, struct _throttle_io_info_t
*, sizeof(*info
), M_TEMP
, M_ZERO
| M_WAITOK
);
1346 /* Should never happen but just in case */
1349 /* Mark that this one was allocated and needs to be freed */
1350 DEBUG_ALLOC_THROTTLE_INFO("Creating info = %p\n", info
, info
);
1351 info
->throttle_alloc
= TRUE
;
1353 lck_mtx_init(&info
->throttle_lock
, throttle_mtx_grp
, throttle_mtx_attr
);
1354 info
->throttle_timer_call
= thread_call_allocate((thread_call_func_t
)throttle_timer
, (thread_call_param_t
)info
);
1356 for (level
= 0; level
<= THROTTLE_LEVEL_END
; level
++) {
1357 TAILQ_INIT(&info
->throttle_uthlist
[level
]);
1359 info
->throttle_next_wake_level
= THROTTLE_LEVEL_END
;
1361 /* Take a reference */
1362 OSIncrementAtomic(&info
->throttle_refcnt
);
1369 * Release the throttle info pointer if all the reference are gone. Should be
1370 * called to release reference taken by throttle_info_create
1373 throttle_info_release(void *throttle_info
)
1375 DEBUG_ALLOC_THROTTLE_INFO("Releaseing info = %p\n",
1376 (struct _throttle_io_info_t
*)throttle_info
,
1377 (struct _throttle_io_info_t
*)throttle_info
);
1378 if (throttle_info
) /* Just to be careful */
1379 throttle_info_rel(throttle_info
);
1385 * File Systems that create an info structure, need to call this routine in
1386 * their mount routine (used by cluster code). File Systems that call this in
1387 * their mount routines must call throttle_info_mount_rel in their unmount
1391 throttle_info_mount_ref(mount_t mp
, void *throttle_info
)
1393 if ((throttle_info
== NULL
) || (mp
== NULL
))
1395 throttle_info_ref(throttle_info
);
1398 * We already have a reference release it before adding the new one
1400 if (mp
->mnt_throttle_info
)
1401 throttle_info_rel(mp
->mnt_throttle_info
);
1402 mp
->mnt_throttle_info
= throttle_info
;
1406 * Private KPI routine
1408 * return a handle for accessing throttle_info given a throttle_mask. The
1409 * handle must be released by throttle_info_rel_by_mask
1412 throttle_info_ref_by_mask(uint64_t throttle_mask
, throttle_info_handle_t
*throttle_info_handle
)
1415 struct _throttle_io_info_t
*info
;
1417 if (throttle_info_handle
== NULL
)
1420 dev_index
= num_trailing_0(throttle_mask
);
1421 info
= &_throttle_io_info
[dev_index
];
1422 throttle_info_ref(info
);
1423 *(struct _throttle_io_info_t
**)throttle_info_handle
= info
;
1429 * Private KPI routine
1431 * release the handle obtained by throttle_info_ref_by_mask
1434 throttle_info_rel_by_mask(throttle_info_handle_t throttle_info_handle
)
1437 * for now the handle is just a pointer to _throttle_io_info_t
1439 throttle_info_rel((struct _throttle_io_info_t
*)throttle_info_handle
);
1445 * File Systems that throttle_info_mount_ref, must call this routine in their
1449 throttle_info_mount_rel(mount_t mp
)
1451 if (mp
->mnt_throttle_info
)
1452 throttle_info_rel(mp
->mnt_throttle_info
);
1453 mp
->mnt_throttle_info
= NULL
;
1457 throttle_info_get_last_io_time(mount_t mp
, struct timeval
*tv
)
1459 struct _throttle_io_info_t
*info
;
1462 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1463 else if (mp
->mnt_throttle_info
== NULL
)
1464 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1466 info
= mp
->mnt_throttle_info
;
1468 *tv
= info
->throttle_last_write_timestamp
;
1472 update_last_io_time(mount_t mp
)
1474 struct _throttle_io_info_t
*info
;
1477 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1478 else if (mp
->mnt_throttle_info
== NULL
)
1479 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1481 info
= mp
->mnt_throttle_info
;
1483 microuptime(&info
->throttle_last_write_timestamp
);
1485 mp
->mnt_last_write_completed_timestamp
= info
->throttle_last_write_timestamp
;
1490 throttle_get_io_policy(uthread_t
*ut
)
1493 *ut
= get_bsdthread_info(current_thread());
1495 return (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO
));
1499 throttle_get_passive_io_policy(uthread_t
*ut
)
1502 *ut
= get_bsdthread_info(current_thread());
1504 return (proc_get_effective_thread_policy(current_thread(), TASK_POLICY_PASSIVE_IO
));
1509 throttle_get_thread_throttle_level(uthread_t ut
)
1511 int thread_throttle_level
;
1514 ut
= get_bsdthread_info(current_thread());
1516 thread_throttle_level
= proc_get_effective_thread_policy(ut
->uu_thread
, TASK_POLICY_IO
);
1518 /* Bootcache misses should always be throttled */
1519 if (ut
->uu_throttle_bc
== TRUE
)
1520 thread_throttle_level
= THROTTLE_LEVEL_TIER3
;
1522 return (thread_throttle_level
);
1527 throttle_io_will_be_throttled_internal(void * throttle_info
, int * mylevel
, int * throttling_level
)
1529 struct _throttle_io_info_t
*info
= throttle_info
;
1530 struct timeval elapsed
;
1531 uint64_t elapsed_msecs
;
1532 int thread_throttle_level
;
1535 if ((thread_throttle_level
= throttle_get_thread_throttle_level(NULL
)) < THROTTLE_LEVEL_THROTTLED
)
1536 return (THROTTLE_DISENGAGED
);
1538 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< thread_throttle_level
; throttle_level
++) {
1540 microuptime(&elapsed
);
1541 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
1542 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1544 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[thread_throttle_level
])
1547 if (throttle_level
>= thread_throttle_level
) {
1549 * we're beyond all of the throttle windows
1550 * that affect the throttle level of this thread,
1551 * so go ahead and treat as normal I/O
1553 return (THROTTLE_DISENGAGED
);
1556 *mylevel
= thread_throttle_level
;
1557 if (throttling_level
)
1558 *throttling_level
= throttle_level
;
1560 if (info
->throttle_io_count
!= info
->throttle_io_count_begin
) {
1562 * we've already issued at least one throttleable I/O
1563 * in the current I/O window, so avoid issuing another one
1565 return (THROTTLE_NOW
);
1568 * we're in the throttle window, so
1569 * cut the I/O size back
1571 return (THROTTLE_ENGAGED
);
1575 * If we have a mount point and it has a throttle info pointer then
1576 * use it to do the check, otherwise use the device unit number to find
1577 * the correct throttle info array element.
1580 throttle_io_will_be_throttled(__unused
int lowpri_window_msecs
, mount_t mp
)
1585 * Should we just return zero if no mount point
1588 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1589 else if (mp
->mnt_throttle_info
== NULL
)
1590 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1592 info
= mp
->mnt_throttle_info
;
1594 return throttle_io_will_be_throttled_internal(info
, NULL
, NULL
);
1598 * Routine to increment I/O throttling counters maintained in the proc
1602 throttle_update_proc_stats(pid_t throttling_pid
)
1604 proc_t throttling_proc
;
1605 proc_t throttled_proc
= current_proc();
1607 /* The throttled_proc is always the current proc; so we are not concerned with refs */
1608 OSAddAtomic64(1, &(throttled_proc
->was_throttled
));
1610 /* The throttling pid might have exited by now */
1611 throttling_proc
= proc_find(throttling_pid
);
1612 if (throttling_proc
!= PROC_NULL
) {
1613 OSAddAtomic64(1, &(throttling_proc
->did_throttle
));
1614 proc_rele(throttling_proc
);
1619 * Block until woken up by the throttle timer or by a rethrottle call.
1620 * As long as we hold the throttle_lock while querying the throttle tier, we're
1621 * safe against seeing an old throttle tier after a rethrottle.
1624 throttle_lowpri_io(int sleep_amount
)
1627 struct _throttle_io_info_t
*info
;
1628 int throttle_type
= 0;
1630 int throttling_level
= THROTTLE_LEVEL_NONE
;
1632 uint32_t throttle_io_period_num
= 0;
1633 boolean_t insert_tail
= TRUE
;
1635 ut
= get_bsdthread_info(current_thread());
1637 if (ut
->uu_lowpri_window
== 0)
1640 info
= ut
->uu_throttle_info
;
1643 ut
->uu_throttle_bc
= FALSE
;
1644 ut
->uu_lowpri_window
= 0;
1648 lck_mtx_lock(&info
->throttle_lock
);
1650 if (sleep_amount
== 0)
1653 if (sleep_amount
== 1 && ut
->uu_throttle_bc
== FALSE
)
1656 throttle_io_period_num
= info
->throttle_io_period_num
;
1658 while ( (throttle_type
= throttle_io_will_be_throttled_internal(info
, &mylevel
, &throttling_level
)) ) {
1660 if (throttle_type
== THROTTLE_ENGAGED
) {
1661 if (sleep_amount
== 0)
1663 if (info
->throttle_io_period_num
< throttle_io_period_num
)
1665 if ((info
->throttle_io_period_num
- throttle_io_period_num
) >= (uint32_t)sleep_amount
)
1668 if (ut
->uu_on_throttlelist
< THROTTLE_LEVEL_THROTTLED
) {
1669 if (throttle_add_to_list(info
, ut
, mylevel
, insert_tail
) == THROTTLE_LEVEL_END
)
1672 assert(throttling_level
>= THROTTLE_LEVEL_START
&& throttling_level
<= THROTTLE_LEVEL_END
);
1673 throttle_update_proc_stats(info
->throttle_last_IO_pid
[throttling_level
]);
1674 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, PROCESS_THROTTLED
)) | DBG_FUNC_NONE
,
1675 info
->throttle_last_IO_pid
[throttling_level
], throttling_level
, proc_selfpid(), mylevel
, 0);
1678 if (sleep_cnt
== 0) {
1679 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_START
,
1680 throttle_windows_msecs
[mylevel
], info
->throttle_io_periods
[mylevel
], info
->throttle_io_count
, 0, 0);
1681 throttled_count
[mylevel
]++;
1683 msleep((caddr_t
)&ut
->uu_on_throttlelist
, &info
->throttle_lock
, PRIBIO
+ 1, "throttle_lowpri_io", NULL
);
1687 if (sleep_amount
== 0)
1688 insert_tail
= FALSE
;
1689 else if (info
->throttle_io_period_num
< throttle_io_period_num
||
1690 (info
->throttle_io_period_num
- throttle_io_period_num
) >= (uint32_t)sleep_amount
) {
1691 insert_tail
= FALSE
;
1696 if (ut
->uu_on_throttlelist
>= THROTTLE_LEVEL_THROTTLED
) {
1697 TAILQ_REMOVE(&info
->throttle_uthlist
[ut
->uu_on_throttlelist
], ut
, uu_throttlelist
);
1698 ut
->uu_on_throttlelist
= THROTTLE_LEVEL_NONE
;
1701 lck_mtx_unlock(&info
->throttle_lock
);
1704 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW
, 97)) | DBG_FUNC_END
,
1705 throttle_windows_msecs
[mylevel
], info
->throttle_io_periods
[mylevel
], info
->throttle_io_count
, 0, 0);
1708 throttle_info_rel(info
);
1710 ut
->uu_throttle_info
= NULL
;
1711 ut
->uu_throttle_bc
= FALSE
;
1712 ut
->uu_lowpri_window
= 0;
1720 * set a kernel thread's IO policy. policy can be:
1721 * IOPOL_NORMAL, IOPOL_THROTTLE, IOPOL_PASSIVE, IOPOL_UTILITY, IOPOL_STANDARD
1723 * explanations about these policies are in the man page of setiopolicy_np
1725 void throttle_set_thread_io_policy(int policy
)
1727 proc_set_task_policy(current_task(), current_thread(),
1728 TASK_POLICY_INTERNAL
, TASK_POLICY_IOPOL
,
1734 void throttle_info_reset_window(uthread_t ut
)
1736 struct _throttle_io_info_t
*info
;
1738 if ( (info
= ut
->uu_throttle_info
) ) {
1739 throttle_info_rel(info
);
1741 ut
->uu_throttle_info
= NULL
;
1742 ut
->uu_lowpri_window
= 0;
1743 ut
->uu_throttle_bc
= FALSE
;
1748 void throttle_info_set_initial_window(uthread_t ut
, struct _throttle_io_info_t
*info
, boolean_t BC_throttle
, boolean_t isssd
)
1750 if (lowpri_throttle_enabled
== 0)
1753 if (info
->throttle_io_periods
== 0) {
1754 throttle_init_throttle_period(info
, isssd
);
1756 if (ut
->uu_throttle_info
== NULL
) {
1758 ut
->uu_throttle_info
= info
;
1759 throttle_info_ref(info
);
1760 DEBUG_ALLOC_THROTTLE_INFO("updating info = %p\n", info
, info
);
1762 ut
->uu_lowpri_window
= 1;
1763 ut
->uu_throttle_bc
= BC_throttle
;
1769 void throttle_info_update_internal(struct _throttle_io_info_t
*info
, uthread_t ut
, int flags
, boolean_t isssd
)
1771 int thread_throttle_level
;
1773 if (lowpri_throttle_enabled
== 0)
1777 ut
= get_bsdthread_info(current_thread());
1779 thread_throttle_level
= throttle_get_thread_throttle_level(ut
);
1781 if (thread_throttle_level
!= THROTTLE_LEVEL_NONE
) {
1782 if(!ISSET(flags
, B_PASSIVE
)) {
1783 microuptime(&info
->throttle_window_start_timestamp
[thread_throttle_level
]);
1784 info
->throttle_last_IO_pid
[thread_throttle_level
] = proc_selfpid();
1785 KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE
, OPEN_THROTTLE_WINDOW
)) | DBG_FUNC_NONE
,
1786 current_proc()->p_pid
, thread_throttle_level
, 0, 0, 0);
1788 microuptime(&info
->throttle_last_IO_timestamp
[thread_throttle_level
]);
1792 if (thread_throttle_level
>= THROTTLE_LEVEL_THROTTLED
) {
1794 * I'd really like to do the IOSleep here, but
1795 * we may be holding all kinds of filesystem related locks
1796 * and the pages for this I/O marked 'busy'...
1797 * we don't want to cause a normal task to block on
1798 * one of these locks while we're throttling a task marked
1799 * for low priority I/O... we'll mark the uthread and
1800 * do the delay just before we return from the system
1801 * call that triggered this I/O or from vnode_pagein
1803 OSAddAtomic(1, &info
->throttle_io_count
);
1805 throttle_info_set_initial_window(ut
, info
, FALSE
, isssd
);
1809 void *throttle_info_update_by_mount(mount_t mp
)
1811 struct _throttle_io_info_t
*info
;
1813 boolean_t isssd
= FALSE
;
1815 ut
= get_bsdthread_info(current_thread());
1818 if ((mp
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
)
1820 info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1822 info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1824 if (!ut
->uu_lowpri_window
)
1825 throttle_info_set_initial_window(ut
, info
, FALSE
, isssd
);
1834 * this is usually called before every I/O, used for throttled I/O
1835 * book keeping. This routine has low overhead and does not sleep
1837 void throttle_info_update(void *throttle_info
, int flags
)
1840 throttle_info_update_internal(throttle_info
, NULL
, flags
, FALSE
);
1846 * this is usually called before every I/O, used for throttled I/O
1847 * book keeping. This routine has low overhead and does not sleep
1849 void throttle_info_update_by_mask(void *throttle_info_handle
, int flags
)
1851 void *throttle_info
= throttle_info_handle
;
1854 * for now we only use the lowest bit of the throttle mask, so the
1855 * handle is the same as the throttle_info. Later if we store a
1856 * set of throttle infos in the handle, we will want to loop through
1857 * them and call throttle_info_update in a loop
1859 throttle_info_update(throttle_info
, flags
);
1863 * KPI routine (private)
1864 * Called to determine if this IO is being throttled to this level so that it can be treated specially
1866 int throttle_info_io_will_be_throttled(void * throttle_info
, int policy
)
1868 struct _throttle_io_info_t
*info
= throttle_info
;
1869 struct timeval elapsed
;
1870 uint64_t elapsed_msecs
;
1872 int thread_throttle_level
;
1876 case IOPOL_THROTTLE
:
1877 thread_throttle_level
= THROTTLE_LEVEL_TIER3
;
1880 thread_throttle_level
= THROTTLE_LEVEL_TIER2
;
1882 case IOPOL_STANDARD
:
1883 thread_throttle_level
= THROTTLE_LEVEL_TIER1
;
1886 thread_throttle_level
= THROTTLE_LEVEL_TIER0
;
1889 for (throttle_level
= THROTTLE_LEVEL_START
; throttle_level
< thread_throttle_level
; throttle_level
++) {
1891 microuptime(&elapsed
);
1892 timevalsub(&elapsed
, &info
->throttle_window_start_timestamp
[throttle_level
]);
1893 elapsed_msecs
= (uint64_t)elapsed
.tv_sec
* (uint64_t)1000 + (elapsed
.tv_usec
/ 1000);
1895 if (elapsed_msecs
< (uint64_t)throttle_windows_msecs
[thread_throttle_level
])
1898 if (throttle_level
>= thread_throttle_level
) {
1900 * we're beyond all of the throttle windows
1901 * so go ahead and treat as normal I/O
1903 return (THROTTLE_DISENGAGED
);
1906 * we're in the throttle window
1908 return (THROTTLE_ENGAGED
);
1912 spec_strategy(struct vnop_strategy_args
*ap
)
1921 struct bufattr
*bap
;
1923 struct _throttle_io_info_t
*throttle_info
;
1924 boolean_t isssd
= FALSE
;
1925 proc_t curproc
= current_proc();
1928 bdev
= buf_device(bp
);
1929 mp
= buf_vnode(bp
)->v_mount
;
1932 io_tier
= throttle_get_io_policy(&ut
);
1933 passive
= throttle_get_passive_io_policy(&ut
);
1935 if (bp
->b_flags
& B_META
)
1936 bap
->ba_flags
|= BA_META
;
1938 SET_BUFATTR_IO_TIER(bap
, io_tier
);
1941 bp
->b_flags
|= B_PASSIVE
;
1943 if ((curproc
!= NULL
) && ((curproc
->p_flag
& P_DELAYIDLESLEEP
) == P_DELAYIDLESLEEP
))
1944 bap
->ba_flags
|= BA_DELAYIDLESLEEP
;
1946 bflags
= bp
->b_flags
;
1948 if (((bflags
& B_READ
) == 0) && ((bflags
& B_ASYNC
) == 0))
1949 bufattr_markquickcomplete(bap
);
1951 if (kdebug_enable
) {
1954 if (bflags
& B_READ
)
1956 if (bflags
& B_ASYNC
)
1959 if (bflags
& B_META
)
1961 else if (bflags
& B_PAGEIO
)
1962 code
|= DKIO_PAGING
;
1965 code
|= DKIO_THROTTLE
;
1967 code
|= ((io_tier
<< DKIO_TIER_SHIFT
) & DKIO_TIER_MASK
);
1969 if (bflags
& B_PASSIVE
)
1970 code
|= DKIO_PASSIVE
;
1972 if (bap
->ba_flags
& BA_NOCACHE
)
1973 code
|= DKIO_NOCACHE
;
1975 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON
, FSDBG_CODE(DBG_DKRW
, code
) | DBG_FUNC_NONE
,
1976 buf_kernel_addrperm_addr(bp
), bdev
, (int)buf_blkno(bp
), buf_count(bp
), 0);
1979 if ((mp
->mnt_kern_flag
& MNTK_SSD
) && !ignore_is_ssd
)
1981 throttle_info
= &_throttle_io_info
[mp
->mnt_devbsdunit
];
1983 throttle_info
= &_throttle_io_info
[LOWPRI_MAX_NUM_DEV
- 1];
1985 throttle_info_update_internal(throttle_info
, ut
, bflags
, isssd
);
1987 if ((bflags
& B_READ
) == 0) {
1988 microuptime(&throttle_info
->throttle_last_write_timestamp
);
1991 mp
->mnt_last_write_issued_timestamp
= throttle_info
->throttle_last_write_timestamp
;
1992 INCR_PENDING_IO(buf_count(bp
), mp
->mnt_pending_write_size
);
1995 INCR_PENDING_IO(buf_count(bp
), mp
->mnt_pending_read_size
);
1998 * The BootCache may give us special information about
1999 * the IO, so it returns special values that we check
2002 * IO_SATISFIED_BY_CACHE
2003 * The read has been satisfied by the boot cache. Don't
2004 * throttle the thread unnecessarily.
2006 * IO_SHOULD_BE_THROTTLED
2007 * The boot cache is playing back a playlist and this IO
2008 * cut through. Throttle it so we're not cutting through
2009 * the boot cache too often.
2011 * Note that typical strategy routines are defined with
2012 * a void return so we'll get garbage here. In the
2013 * unlikely case the garbage matches our special return
2014 * value, it's not a big deal since we're only adjusting
2015 * the throttling delay.
2017 #define IO_SATISFIED_BY_CACHE ((int)0xcafefeed)
2018 #define IO_SHOULD_BE_THROTTLED ((int)0xcafebeef)
2019 typedef int strategy_fcn_ret_t(struct buf
*bp
);
2021 strategy_ret
= (*(strategy_fcn_ret_t
*)bdevsw
[major(bdev
)].d_strategy
)(bp
);
2023 if (IO_SATISFIED_BY_CACHE
== strategy_ret
) {
2025 * If this was a throttled IO satisfied by the boot cache,
2026 * don't delay the thread.
2028 throttle_info_reset_window(ut
);
2030 } else if (IO_SHOULD_BE_THROTTLED
== strategy_ret
) {
2032 * If the boot cache indicates this IO should be throttled,
2035 throttle_info_set_initial_window(ut
, throttle_info
, TRUE
, isssd
);
2042 * This is a noop, simply returning what one has been given.
2045 spec_blockmap(__unused
struct vnop_blockmap_args
*ap
)
2052 * Device close routine
2055 spec_close(struct vnop_close_args
*ap
)
2057 struct vnode
*vp
= ap
->a_vp
;
2058 dev_t dev
= vp
->v_rdev
;
2060 int flags
= ap
->a_fflag
;
2061 struct proc
*p
= vfs_context_proc(ap
->a_context
);
2062 struct session
*sessp
;
2065 switch (vp
->v_type
) {
2069 * Hack: a tty device that is a controlling terminal
2070 * has a reference from the session structure.
2071 * We cannot easily tell that a character device is
2072 * a controlling terminal, unless it is the closing
2073 * process' controlling terminal. In that case,
2074 * if the reference count is 1 (this is the very
2077 sessp
= proc_session(p
);
2078 devsw_lock(dev
, S_IFCHR
);
2079 if (sessp
!= SESSION_NULL
) {
2080 if (vp
== sessp
->s_ttyvp
&& vcount(vp
) == 1) {
2083 devsw_unlock(dev
, S_IFCHR
);
2084 session_lock(sessp
);
2085 if (vp
== sessp
->s_ttyvp
) {
2086 tp
= SESSION_TP(sessp
);
2087 sessp
->s_ttyvp
= NULL
;
2088 sessp
->s_ttyvid
= 0;
2089 sessp
->s_ttyp
= TTY_NULL
;
2090 sessp
->s_ttypgrpid
= NO_PID
;
2093 session_unlock(sessp
);
2100 devsw_lock(dev
, S_IFCHR
);
2102 session_rele(sessp
);
2105 if (--vp
->v_specinfo
->si_opencount
< 0)
2106 panic("negative open count (c, %u, %u)", major(dev
), minor(dev
));
2109 * close on last reference or on vnode revoke call
2111 if (vcount(vp
) == 0 || (flags
& IO_REVOKE
) != 0)
2112 error
= cdevsw
[major(dev
)].d_close(dev
, flags
, S_IFCHR
, p
);
2114 devsw_unlock(dev
, S_IFCHR
);
2119 * If there is more than one outstanding open, don't
2120 * send the close to the device.
2122 devsw_lock(dev
, S_IFBLK
);
2123 if (vcount(vp
) > 1) {
2124 vp
->v_specinfo
->si_opencount
--;
2125 devsw_unlock(dev
, S_IFBLK
);
2128 devsw_unlock(dev
, S_IFBLK
);
2131 * On last close of a block device (that isn't mounted)
2132 * we must invalidate any in core blocks, so that
2133 * we can, for instance, change floppy disks.
2135 if ((error
= spec_fsync_internal(vp
, MNT_WAIT
, ap
->a_context
)))
2138 error
= buf_invalidateblks(vp
, BUF_WRITE_DATA
, 0, 0);
2142 devsw_lock(dev
, S_IFBLK
);
2144 if (--vp
->v_specinfo
->si_opencount
< 0)
2145 panic("negative open count (b, %u, %u)", major(dev
), minor(dev
));
2147 if (vcount(vp
) == 0)
2148 error
= bdevsw
[major(dev
)].d_close(dev
, flags
, S_IFBLK
, p
);
2150 devsw_unlock(dev
, S_IFBLK
);
2154 panic("spec_close: not special");
2162 * Return POSIX pathconf information applicable to special devices.
2165 spec_pathconf(struct vnop_pathconf_args
*ap
)
2168 switch (ap
->a_name
) {
2170 *ap
->a_retval
= LINK_MAX
;
2173 *ap
->a_retval
= MAX_CANON
;
2176 *ap
->a_retval
= MAX_INPUT
;
2179 *ap
->a_retval
= PIPE_BUF
;
2181 case _PC_CHOWN_RESTRICTED
:
2182 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
2185 *ap
->a_retval
= _POSIX_VDISABLE
;
2194 * Special device failed operation
2197 spec_ebadf(__unused
void *dummy
)
2203 /* Blktooff derives file offset from logical block number */
2205 spec_blktooff(struct vnop_blktooff_args
*ap
)
2207 struct vnode
*vp
= ap
->a_vp
;
2209 switch (vp
->v_type
) {
2211 *ap
->a_offset
= (off_t
)-1; /* failure */
2215 printf("spec_blktooff: not implemented for VBLK\n");
2216 *ap
->a_offset
= (off_t
)-1; /* failure */
2220 panic("spec_blktooff type");
2227 /* Offtoblk derives logical block number from file offset */
2229 spec_offtoblk(struct vnop_offtoblk_args
*ap
)
2231 struct vnode
*vp
= ap
->a_vp
;
2233 switch (vp
->v_type
) {
2235 *ap
->a_lblkno
= (daddr64_t
)-1; /* failure */
2239 printf("spec_offtoblk: not implemented for VBLK\n");
2240 *ap
->a_lblkno
= (daddr64_t
)-1; /* failure */
2244 panic("spec_offtoblk type");
2251 static void filt_specdetach(struct knote
*kn
);
2252 static int filt_spec(struct knote
*kn
, long hint
);
2253 static unsigned filt_specpeek(struct knote
*kn
);
2255 struct filterops spec_filtops
= {
2257 .f_attach
= filt_specattach
,
2258 .f_detach
= filt_specdetach
,
2259 .f_event
= filt_spec
,
2260 .f_peek
= filt_specpeek
2264 filter_to_seltype(int16_t filter
)
2273 panic("filt_to_seltype(): invalid filter %d\n", filter
);
2279 filt_specattach(struct knote
*kn
)
2284 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
; /* Already have iocount, and vnode is alive */
2286 assert(vnode_ischr(vp
));
2288 dev
= vnode_specrdev(vp
);
2290 if (major(dev
) > nchrdev
) {
2294 if ((cdevsw_flags
[major(dev
)] & CDEVSW_SELECT_KQUEUE
) == 0) {
2298 /* Resulting wql is safe to unlink even if it has never been linked */
2299 kn
->kn_hook
= wait_queue_link_allocate();
2300 if (kn
->kn_hook
== NULL
) {
2304 kn
->kn_fop
= &spec_filtops
;
2305 kn
->kn_hookid
= vnode_vid(vp
);
2307 knote_markstayqueued(kn
);
2313 filt_specdetach(struct knote
*kn
)
2318 * Given wait queue link and wait queue set, unlink. This is subtle.
2319 * If the device has been revoked from under us, selclearthread() will
2320 * have removed our link from the kqueue's wait queue set, which
2321 * wait_queue_set_unlink_one() will detect and handle.
2323 ret
= wait_queue_set_unlink_one(kn
->kn_kq
->kq_wqs
, kn
->kn_hook
);
2324 if (ret
!= KERN_SUCCESS
) {
2325 panic("filt_specdetach(): failed to unlink wait queue link.");
2328 (void)wait_queue_link_free(kn
->kn_hook
);
2330 kn
->kn_status
&= ~KN_STAYQUEUED
;
2334 filt_spec(struct knote
*kn
, long hint
)
2338 wait_queue_set_t old_wqs
;
2346 assert(kn
->kn_hook
!= NULL
);
2349 panic("filt_spec(): nonzero hint?");
2352 uth
= get_bsdthread_info(current_thread());
2353 ctx
= vfs_context_current();
2354 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
2356 error
= vnode_getwithvid(vp
, kn
->kn_hookid
);
2358 kn
->kn_flags
|= (EV_EOF
| EV_ONESHOT
);
2362 dev
= vnode_specrdev(vp
);
2363 flags
= cdevsw_flags
[major(dev
)];
2364 use_offset
= ((flags
& CDEVSW_USE_OFFSET
) != 0);
2365 assert((flags
& CDEVSW_SELECT_KQUEUE
) != 0);
2367 /* Trick selrecord() into hooking kqueue's wait queue set into device wait queue */
2368 old_wqs
= uth
->uu_wqset
;
2369 uth
->uu_wqset
= kn
->kn_kq
->kq_wqs
;
2370 selres
= VNOP_SELECT(vp
, filter_to_seltype(kn
->kn_filter
), 0, kn
->kn_hook
, ctx
);
2371 uth
->uu_wqset
= old_wqs
;
2374 if (kn
->kn_fp
->f_fglob
->fg_offset
>= (uint32_t)selres
) {
2377 kn
->kn_data
= ((uint32_t)selres
) - kn
->kn_fp
->f_fglob
->fg_offset
;
2380 kn
->kn_data
= selres
;
2385 return (kn
->kn_data
!= 0);
2389 filt_specpeek(struct knote
*kn
)
2393 wait_queue_set_t old_wqs
;
2397 uth
= get_bsdthread_info(current_thread());
2398 ctx
= vfs_context_current();
2399 vp
= (vnode_t
)kn
->kn_fp
->f_fglob
->fg_data
;
2401 error
= vnode_getwithvid(vp
, kn
->kn_hookid
);
2403 return 1; /* Just like VNOP_SELECT() on recycled vnode */
2407 * Why pass the link here? Because we may not have registered in the past...
2409 old_wqs
= uth
->uu_wqset
;
2410 uth
->uu_wqset
= kn
->kn_kq
->kq_wqs
;
2411 selres
= VNOP_SELECT(vp
, filter_to_seltype(kn
->kn_filter
), 0, kn
->kn_hook
, ctx
);
2412 uth
->uu_wqset
= old_wqs
;