]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2019 Apple Computer, Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1989, 1993, 1995 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
36 | * 1. Redistributions of source code must retain the above copyright | |
37 | * notice, this list of conditions and the following disclaimer. | |
38 | * 2. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in the | |
40 | * documentation and/or other materials provided with the distribution. | |
41 | * 3. 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. | |
48 | * | |
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 | |
59 | * SUCH DAMAGE. | |
60 | * | |
61 | * @(#)spec_vnops.c 8.14 (Berkeley) 5/21/95 | |
62 | */ | |
63 | ||
64 | #include <sys/param.h> | |
91447636 A |
65 | #include <sys/proc_internal.h> |
66 | #include <sys/kauth.h> | |
1c79356b A |
67 | #include <sys/systm.h> |
68 | #include <sys/kernel.h> | |
69 | #include <sys/conf.h> | |
91447636 A |
70 | #include <sys/buf_internal.h> |
71 | #include <sys/mount_internal.h> | |
91447636 | 72 | #include <sys/vnode_internal.h> |
6d2010ae A |
73 | #include <sys/file_internal.h> |
74 | #include <sys/namei.h> | |
1c79356b A |
75 | #include <sys/stat.h> |
76 | #include <sys/errno.h> | |
77 | #include <sys/ioctl.h> | |
78 | #include <sys/file.h> | |
91447636 | 79 | #include <sys/user.h> |
1c79356b | 80 | #include <sys/malloc.h> |
55e303ae | 81 | #include <sys/disk.h> |
91447636 | 82 | #include <sys/uio_internal.h> |
2d21ac55 | 83 | #include <sys/resource.h> |
39037602 | 84 | #include <machine/machine_routines.h> |
1c79356b A |
85 | #include <miscfs/specfs/specdev.h> |
86 | #include <vfs/vfs_support.h> | |
5ba3f43e | 87 | #include <vfs/vfs_disk_conditioner.h> |
39037602 | 88 | |
6d2010ae A |
89 | #include <kern/assert.h> |
90 | #include <kern/task.h> | |
39037602 A |
91 | #include <kern/sched_prim.h> |
92 | #include <kern/thread.h> | |
93 | #include <kern/policy_internal.h> | |
d190cdc3 | 94 | #include <kern/timer_call.h> |
5ba3f43e | 95 | #include <kern/waitq.h> |
39037602 | 96 | |
39236c6e | 97 | #include <pexpert/pexpert.h> |
f427ee49 | 98 | #include <IOKit/IOBSD.h> |
1c79356b | 99 | |
9bccf70c | 100 | #include <sys/kdebug.h> |
5ba3f43e | 101 | #include <libkern/section_keywords.h> |
1c79356b | 102 | |
c3c9b80d A |
103 | #if CONFIG_IO_COMPRESSION_STATS |
104 | #include <vfs/vfs_io_compression_stats.h> | |
105 | #endif /* CONFIG_IO_COMPRESSION_STATS */ | |
106 | ||
2d21ac55 | 107 | /* XXX following three prototypes should be in a header file somewhere */ |
0a7de745 A |
108 | extern dev_t chrtoblk(dev_t dev); |
109 | extern boolean_t iskmemdev(dev_t dev); | |
cb323159 | 110 | extern int bpfkqfilter(dev_t dev, struct knote *kn); |
5ba3f43e | 111 | extern int ptsd_kqfilter(dev_t, struct knote *); |
5c9f4661 | 112 | extern int ptmx_kqfilter(dev_t, struct knote *); |
f427ee49 A |
113 | #if CONFIG_PHYS_WRITE_ACCT |
114 | uint64_t kernel_pm_writes; // to track the sync writes occuring during power management transitions | |
115 | #endif /* CONFIG_PHYS_WRITE_ACCT */ | |
116 | ||
316670eb | 117 | |
1c79356b A |
118 | struct vnode *speclisth[SPECHSZ]; |
119 | ||
120 | /* symbolic sleep message strings for devices */ | |
0a7de745 A |
121 | char devopn[] = "devopn"; |
122 | char devio[] = "devio"; | |
123 | char devwait[] = "devwait"; | |
124 | char devin[] = "devin"; | |
125 | char devout[] = "devout"; | |
126 | char devioc[] = "devioc"; | |
127 | char devcls[] = "devcls"; | |
1c79356b A |
128 | |
129 | #define VOPFUNC int (*)(void *) | |
130 | ||
0a7de745 | 131 | int(**spec_vnodeop_p)(void *); |
cb323159 A |
132 | const struct vnodeopv_entry_desc spec_vnodeop_entries[] = { |
133 | { .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)vn_default_error }, | |
134 | { .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)spec_lookup }, /* lookup */ | |
135 | { .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)err_create }, /* create */ | |
136 | { .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)err_mknod }, /* mknod */ | |
137 | { .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)spec_open }, /* open */ | |
138 | { .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)spec_close }, /* close */ | |
139 | { .opve_op = &vnop_access_desc, .opve_impl = (VOPFUNC)spec_access }, /* access */ | |
140 | { .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)spec_getattr }, /* getattr */ | |
141 | { .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)spec_setattr }, /* setattr */ | |
142 | { .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)spec_read }, /* read */ | |
143 | { .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)spec_write }, /* write */ | |
144 | { .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)spec_ioctl }, /* ioctl */ | |
145 | { .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)spec_select }, /* select */ | |
146 | { .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)nop_revoke }, /* revoke */ | |
147 | { .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)err_mmap }, /* mmap */ | |
148 | { .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)spec_fsync }, /* fsync */ | |
149 | { .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)err_remove }, /* remove */ | |
150 | { .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)err_link }, /* link */ | |
151 | { .opve_op = &vnop_rename_desc, .opve_impl = (VOPFUNC)err_rename }, /* rename */ | |
152 | { .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)err_mkdir }, /* mkdir */ | |
153 | { .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)err_rmdir }, /* rmdir */ | |
154 | { .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)err_symlink }, /* symlink */ | |
155 | { .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)err_readdir }, /* readdir */ | |
156 | { .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)err_readlink }, /* readlink */ | |
157 | { .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)nop_inactive }, /* inactive */ | |
158 | { .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)nop_reclaim }, /* reclaim */ | |
159 | { .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)spec_strategy }, /* strategy */ | |
160 | { .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)spec_pathconf }, /* pathconf */ | |
161 | { .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)err_advlock }, /* advlock */ | |
162 | { .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)spec_bwrite }, /* bwrite */ | |
163 | { .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein }, /* Pagein */ | |
164 | { .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout }, /* Pageout */ | |
165 | { .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile }, /* Copyfile */ | |
166 | { .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)spec_blktooff }, /* blktooff */ | |
167 | { .opve_op = &vnop_offtoblk_desc, .opve_impl = (VOPFUNC)spec_offtoblk }, /* offtoblk */ | |
168 | { .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)spec_blockmap }, /* blockmap */ | |
169 | { .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (int (*)(void *))NULL } | |
1c79356b | 170 | }; |
cb323159 A |
171 | const struct vnodeopv_desc spec_vnodeop_opv_desc = |
172 | { .opv_desc_vector_p = &spec_vnodeop_p, .opv_desc_ops = spec_vnodeop_entries }; | |
1c79356b | 173 | |
91447636 A |
174 | |
175 | static void set_blocksize(vnode_t, dev_t); | |
176 | ||
0a7de745 A |
177 | #define LOWPRI_TIER1_WINDOW_MSECS 25 |
178 | #define LOWPRI_TIER2_WINDOW_MSECS 100 | |
179 | #define LOWPRI_TIER3_WINDOW_MSECS 500 | |
91447636 | 180 | |
0a7de745 A |
181 | #define LOWPRI_TIER1_IO_PERIOD_MSECS 40 |
182 | #define LOWPRI_TIER2_IO_PERIOD_MSECS 85 | |
183 | #define LOWPRI_TIER3_IO_PERIOD_MSECS 200 | |
316670eb | 184 | |
39236c6e A |
185 | #define LOWPRI_TIER1_IO_PERIOD_SSD_MSECS 5 |
186 | #define LOWPRI_TIER2_IO_PERIOD_SSD_MSECS 15 | |
187 | #define LOWPRI_TIER3_IO_PERIOD_SSD_MSECS 25 | |
316670eb | 188 | |
316670eb | 189 | |
0a7de745 | 190 | int throttle_windows_msecs[THROTTLE_LEVEL_END + 1] = { |
39236c6e A |
191 | 0, |
192 | LOWPRI_TIER1_WINDOW_MSECS, | |
193 | LOWPRI_TIER2_WINDOW_MSECS, | |
194 | LOWPRI_TIER3_WINDOW_MSECS, | |
195 | }; | |
196 | ||
0a7de745 | 197 | int throttle_io_period_msecs[THROTTLE_LEVEL_END + 1] = { |
39236c6e A |
198 | 0, |
199 | LOWPRI_TIER1_IO_PERIOD_MSECS, | |
200 | LOWPRI_TIER2_IO_PERIOD_MSECS, | |
201 | LOWPRI_TIER3_IO_PERIOD_MSECS, | |
202 | }; | |
203 | ||
0a7de745 | 204 | int throttle_io_period_ssd_msecs[THROTTLE_LEVEL_END + 1] = { |
39236c6e A |
205 | 0, |
206 | LOWPRI_TIER1_IO_PERIOD_SSD_MSECS, | |
207 | LOWPRI_TIER2_IO_PERIOD_SSD_MSECS, | |
208 | LOWPRI_TIER3_IO_PERIOD_SSD_MSECS, | |
209 | }; | |
210 | ||
211 | ||
0a7de745 | 212 | int throttled_count[THROTTLE_LEVEL_END + 1]; |
316670eb | 213 | |
7ddcb079 | 214 | struct _throttle_io_info_t { |
0a7de745 A |
215 | lck_mtx_t throttle_lock; |
216 | ||
217 | struct timeval throttle_last_write_timestamp; | |
218 | struct timeval throttle_min_timer_deadline; | |
219 | struct timeval throttle_window_start_timestamp[THROTTLE_LEVEL_END + 1]; /* window starts at both the beginning and completion of an I/O */ | |
220 | struct timeval throttle_last_IO_timestamp[THROTTLE_LEVEL_END + 1]; | |
221 | pid_t throttle_last_IO_pid[THROTTLE_LEVEL_END + 1]; | |
222 | struct timeval throttle_start_IO_period_timestamp[THROTTLE_LEVEL_END + 1]; | |
39037602 | 223 | int32_t throttle_inflight_count[THROTTLE_LEVEL_END + 1]; |
316670eb | 224 | |
0a7de745 A |
225 | TAILQ_HEAD(, uthread) throttle_uthlist[THROTTLE_LEVEL_END + 1]; /* Lists of throttled uthreads */ |
226 | int throttle_next_wake_level; | |
316670eb | 227 | |
0a7de745 A |
228 | thread_call_t throttle_timer_call; |
229 | int32_t throttle_timer_ref; | |
230 | int32_t throttle_timer_active; | |
39236c6e | 231 | |
0a7de745 A |
232 | int32_t throttle_io_count; |
233 | int32_t throttle_io_count_begin; | |
234 | int *throttle_io_periods; | |
316670eb | 235 | uint32_t throttle_io_period_num; |
39236c6e | 236 | |
316670eb A |
237 | int32_t throttle_refcnt; |
238 | int32_t throttle_alloc; | |
fe8ab488 | 239 | int32_t throttle_disabled; |
3e170ce0 | 240 | int32_t throttle_is_fusion_with_priority; |
7ddcb079 A |
241 | }; |
242 | ||
243 | struct _throttle_io_info_t _throttle_io_info[LOWPRI_MAX_NUM_DEV]; | |
244 | ||
7ddcb079 | 245 | |
0a7de745 | 246 | int lowpri_throttle_enabled = 1; |
39236c6e A |
247 | |
248 | ||
39037602 A |
249 | static void throttle_info_end_io_internal(struct _throttle_io_info_t *info, int throttle_level); |
250 | 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); | |
39236c6e | 251 | static int throttle_get_thread_throttle_level(uthread_t ut); |
d190cdc3 | 252 | static int throttle_get_thread_throttle_level_internal(uthread_t ut, int io_tier); |
5ba3f43e | 253 | void throttle_info_mount_reset_period(mount_t mp, int isssd); |
7ddcb079 | 254 | |
1c79356b A |
255 | /* |
256 | * Trivial lookup routine that always fails. | |
257 | */ | |
258 | int | |
2d21ac55 | 259 | spec_lookup(struct vnop_lookup_args *ap) |
1c79356b | 260 | { |
1c79356b | 261 | *ap->a_vpp = NULL; |
0a7de745 | 262 | return ENOTDIR; |
1c79356b A |
263 | } |
264 | ||
91447636 | 265 | static void |
1c79356b A |
266 | set_blocksize(struct vnode *vp, dev_t dev) |
267 | { | |
0a7de745 A |
268 | int (*size)(dev_t); |
269 | int rsize; | |
1c79356b | 270 | |
0a7de745 A |
271 | if ((major(dev) < nblkdev) && (size = bdevsw[major(dev)].d_psize)) { |
272 | rsize = (*size)(dev); | |
273 | if (rsize <= 0) { /* did size fail? */ | |
274 | vp->v_specsize = DEV_BSIZE; | |
275 | } else { | |
276 | vp->v_specsize = rsize; | |
277 | } | |
278 | } else { | |
279 | vp->v_specsize = DEV_BSIZE; | |
280 | } | |
1c79356b A |
281 | } |
282 | ||
283 | void | |
284 | set_fsblocksize(struct vnode *vp) | |
285 | { | |
1c79356b A |
286 | if (vp->v_type == VBLK) { |
287 | dev_t dev = (dev_t)vp->v_rdev; | |
288 | int maj = major(dev); | |
289 | ||
0a7de745 | 290 | if ((u_int)maj >= (u_int)nblkdev) { |
1c79356b | 291 | return; |
0a7de745 | 292 | } |
1c79356b | 293 | |
91447636 | 294 | vnode_lock(vp); |
1c79356b | 295 | set_blocksize(vp, dev); |
91447636 | 296 | vnode_unlock(vp); |
1c79356b | 297 | } |
1c79356b A |
298 | } |
299 | ||
300 | ||
301 | /* | |
302 | * Open a special file. | |
303 | */ | |
91447636 | 304 | int |
2d21ac55 | 305 | spec_open(struct vnop_open_args *ap) |
1c79356b | 306 | { |
f427ee49 A |
307 | static const char *OPEN_MOUNTED_ENTITLEMENT = "com.apple.private.vfs.open-mounted"; |
308 | ||
91447636 A |
309 | struct proc *p = vfs_context_proc(ap->a_context); |
310 | kauth_cred_t cred = vfs_context_ucred(ap->a_context); | |
311 | struct vnode *vp = ap->a_vp; | |
1c79356b A |
312 | dev_t bdev, dev = (dev_t)vp->v_rdev; |
313 | int maj = major(dev); | |
314 | int error; | |
315 | ||
316 | /* | |
317 | * Don't allow open if fs is mounted -nodev. | |
318 | */ | |
0a7de745 A |
319 | if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV)) { |
320 | return ENXIO; | |
321 | } | |
1c79356b A |
322 | |
323 | switch (vp->v_type) { | |
1c79356b | 324 | case VCHR: |
0a7de745 A |
325 | if ((u_int)maj >= (u_int)nchrdev) { |
326 | return ENXIO; | |
327 | } | |
91447636 | 328 | if (cred != FSCRED && (ap->a_mode & FWRITE)) { |
cb323159 | 329 | #if 0 |
1c79356b A |
330 | /* |
331 | * When running in very secure mode, do not allow | |
332 | * opens for writing of any disk character devices. | |
333 | */ | |
0a7de745 A |
334 | if (securelevel >= 2 && isdisk(dev, VCHR)) { |
335 | return EPERM; | |
336 | } | |
cb323159 | 337 | #endif |
fe8ab488 A |
338 | |
339 | /* Never allow writing to /dev/mem or /dev/kmem */ | |
0a7de745 A |
340 | if (iskmemdev(dev)) { |
341 | return EPERM; | |
342 | } | |
1c79356b | 343 | /* |
fe8ab488 A |
344 | * When running in secure mode, do not allow opens for |
345 | * writing of character devices whose corresponding block | |
346 | * devices are currently mounted. | |
1c79356b A |
347 | */ |
348 | if (securelevel >= 1) { | |
0a7de745 A |
349 | if ((bdev = chrtoblk(dev)) != NODEV && check_mountedon(bdev, VBLK, &error)) { |
350 | return error; | |
351 | } | |
1c79356b A |
352 | } |
353 | } | |
316670eb | 354 | |
6d2010ae | 355 | devsw_lock(dev, S_IFCHR); |
1c79356b | 356 | error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, p); |
6d2010ae A |
357 | |
358 | if (error == 0) { | |
359 | vp->v_specinfo->si_opencount++; | |
360 | } | |
361 | ||
362 | devsw_unlock(dev, S_IFCHR); | |
7ddcb079 | 363 | |
39236c6e | 364 | if (error == 0 && cdevsw[maj].d_type == D_DISK && !vp->v_un.vu_specinfo->si_initted) { |
0a7de745 | 365 | int isssd = 0; |
7ddcb079 A |
366 | uint64_t throttle_mask = 0; |
367 | uint32_t devbsdunit = 0; | |
368 | ||
369 | if (VNOP_IOCTL(vp, DKIOCGETTHROTTLEMASK, (caddr_t)&throttle_mask, 0, NULL) == 0) { | |
316670eb A |
370 | if (throttle_mask != 0 && |
371 | VNOP_IOCTL(vp, DKIOCISSOLIDSTATE, (caddr_t)&isssd, 0, ap->a_context) == 0) { | |
7ddcb079 A |
372 | /* |
373 | * as a reasonable approximation, only use the lowest bit of the mask | |
374 | * to generate a disk unit number | |
375 | */ | |
376 | devbsdunit = num_trailing_0(throttle_mask); | |
377 | ||
378 | vnode_lock(vp); | |
0a7de745 | 379 | |
f427ee49 | 380 | vp->v_un.vu_specinfo->si_isssd = isssd ? 1 : 0; |
7ddcb079 A |
381 | vp->v_un.vu_specinfo->si_devbsdunit = devbsdunit; |
382 | vp->v_un.vu_specinfo->si_throttle_mask = throttle_mask; | |
383 | vp->v_un.vu_specinfo->si_throttleable = 1; | |
384 | vp->v_un.vu_specinfo->si_initted = 1; | |
385 | ||
386 | vnode_unlock(vp); | |
387 | } | |
388 | } | |
389 | if (vp->v_un.vu_specinfo->si_initted == 0) { | |
390 | vnode_lock(vp); | |
391 | vp->v_un.vu_specinfo->si_initted = 1; | |
392 | vnode_unlock(vp); | |
393 | } | |
394 | } | |
0a7de745 | 395 | return error; |
1c79356b A |
396 | |
397 | case VBLK: | |
0a7de745 A |
398 | if ((u_int)maj >= (u_int)nblkdev) { |
399 | return ENXIO; | |
400 | } | |
1c79356b A |
401 | /* |
402 | * When running in very secure mode, do not allow | |
403 | * opens for writing of any disk block devices. | |
404 | */ | |
91447636 | 405 | if (securelevel >= 2 && cred != FSCRED && |
0a7de745 A |
406 | (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK) { |
407 | return EPERM; | |
408 | } | |
1c79356b A |
409 | /* |
410 | * Do not allow opens of block devices that are | |
411 | * currently mounted. | |
412 | */ | |
f427ee49 A |
413 | if (!IOTaskHasEntitlement(current_task(), OPEN_MOUNTED_ENTITLEMENT)) { |
414 | if ((error = vfs_mountedon(vp))) { | |
415 | return error; | |
416 | } | |
0a7de745 | 417 | } |
6d2010ae A |
418 | |
419 | devsw_lock(dev, S_IFBLK); | |
1c79356b | 420 | error = (*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, p); |
6d2010ae A |
421 | if (!error) { |
422 | vp->v_specinfo->si_opencount++; | |
423 | } | |
424 | devsw_unlock(dev, S_IFBLK); | |
425 | ||
1c79356b | 426 | if (!error) { |
0a7de745 A |
427 | u_int64_t blkcnt; |
428 | u_int32_t blksize; | |
91447636 A |
429 | int setsize = 0; |
430 | u_int32_t size512 = 512; | |
431 | ||
432 | ||
0a7de745 | 433 | if (!VNOP_IOCTL(vp, DKIOCGETBLOCKSIZE, (caddr_t)&blksize, 0, ap->a_context)) { |
91447636 | 434 | /* Switch to 512 byte sectors (temporarily) */ |
55e303ae | 435 | |
91447636 | 436 | if (!VNOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&size512, FWRITE, ap->a_context)) { |
0a7de745 A |
437 | /* Get the number of 512 byte physical blocks. */ |
438 | if (!VNOP_IOCTL(vp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, ap->a_context)) { | |
91447636 | 439 | setsize = 1; |
0a7de745 | 440 | } |
91447636 A |
441 | } |
442 | /* If it doesn't set back, we can't recover */ | |
0a7de745 A |
443 | if (VNOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&blksize, FWRITE, ap->a_context)) { |
444 | error = ENXIO; | |
445 | } | |
446 | } | |
91447636 A |
447 | |
448 | ||
449 | vnode_lock(vp); | |
0a7de745 | 450 | set_blocksize(vp, dev); |
55e303ae | 451 | |
0a7de745 A |
452 | /* |
453 | * Cache the size in bytes of the block device for later | |
454 | * use by spec_write(). | |
455 | */ | |
456 | if (setsize) { | |
55e303ae | 457 | vp->v_specdevsize = blkcnt * (u_int64_t)size512; |
0a7de745 A |
458 | } else { |
459 | vp->v_specdevsize = (u_int64_t)0; /* Default: Can't get */ | |
460 | } | |
91447636 | 461 | vnode_unlock(vp); |
1c79356b | 462 | } |
0a7de745 | 463 | return error; |
91447636 | 464 | default: |
0a7de745 | 465 | panic("spec_open type"); |
1c79356b | 466 | } |
0a7de745 | 467 | return 0; |
1c79356b A |
468 | } |
469 | ||
470 | /* | |
471 | * Vnode op for read | |
472 | */ | |
91447636 | 473 | int |
2d21ac55 | 474 | spec_read(struct vnop_read_args *ap) |
1c79356b | 475 | { |
2d21ac55 A |
476 | struct vnode *vp = ap->a_vp; |
477 | struct uio *uio = ap->a_uio; | |
1c79356b | 478 | struct buf *bp; |
91447636 | 479 | daddr64_t bn, nextbn; |
f427ee49 | 480 | long bscale; |
0a7de745 | 481 | int devBlockSize = 0; |
f427ee49 | 482 | size_t bsize, n, on; |
1c79356b A |
483 | int error = 0; |
484 | dev_t dev; | |
485 | ||
486 | #if DIAGNOSTIC | |
0a7de745 | 487 | if (uio->uio_rw != UIO_READ) { |
1c79356b | 488 | panic("spec_read mode"); |
0a7de745 A |
489 | } |
490 | if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) { | |
1c79356b | 491 | panic("spec_read proc"); |
0a7de745 | 492 | } |
1c79356b | 493 | #endif |
0a7de745 A |
494 | if (uio_resid(uio) == 0) { |
495 | return 0; | |
496 | } | |
1c79356b A |
497 | |
498 | switch (vp->v_type) { | |
1c79356b | 499 | case VCHR: |
0a7de745 A |
500 | { |
501 | struct _throttle_io_info_t *throttle_info = NULL; | |
502 | int thread_throttle_level; | |
cb323159 A |
503 | uint64_t blkno = 0; |
504 | uint32_t iolen = 0; | |
505 | int ddisk = 0; | |
506 | int ktrace_code = DKIO_READ; | |
507 | devBlockSize = vp->v_specsize; | |
508 | uintptr_t our_id; | |
509 | ||
510 | if (cdevsw[major(vp->v_rdev)].d_type == D_DISK) { | |
511 | ddisk = 1; | |
512 | } | |
513 | ||
514 | if (ddisk && vp->v_un.vu_specinfo->si_throttleable) { | |
7ddcb079 | 515 | throttle_info = &_throttle_io_info[vp->v_un.vu_specinfo->si_devbsdunit]; |
0a7de745 A |
516 | thread_throttle_level = throttle_info_update_internal(throttle_info, NULL, 0, vp->v_un.vu_specinfo->si_isssd, TRUE, NULL); |
517 | } | |
cb323159 A |
518 | |
519 | if (kdebug_enable && ddisk) { | |
520 | if (devBlockSize == 0) { | |
521 | devBlockSize = 512; // default sector size | |
522 | } | |
523 | ||
524 | if (uio_offset(uio) && devBlockSize) { | |
525 | blkno = ((uint64_t) uio_offset(uio) / ((uint64_t)devBlockSize)); | |
526 | } | |
527 | iolen = (int) uio_resid(uio); | |
528 | our_id = (uintptr_t)thread_tid(current_thread()); | |
529 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, | |
530 | (FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id, | |
531 | vp->v_rdev, blkno, iolen, 0); | |
532 | } | |
533 | ||
1c79356b | 534 | error = (*cdevsw[major(vp->v_rdev)].d_read) |
0a7de745 | 535 | (vp->v_rdev, uio, ap->a_ioflag); |
7ddcb079 | 536 | |
cb323159 A |
537 | |
538 | if (kdebug_enable && ddisk) { | |
539 | uint32_t residual = (uint32_t)uio_resid(uio); | |
540 | ktrace_code |= DKIO_DONE; | |
541 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, | |
542 | (FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id, | |
543 | (uintptr_t)VM_KERNEL_ADDRPERM(vp), residual, error, 0); | |
544 | } | |
545 | ||
0a7de745 A |
546 | if (throttle_info) { |
547 | throttle_info_end_io_internal(throttle_info, thread_throttle_level); | |
39037602 | 548 | } |
1c79356b | 549 | |
0a7de745 A |
550 | return error; |
551 | } | |
552 | ||
1c79356b | 553 | case VBLK: |
0a7de745 A |
554 | if (uio->uio_offset < 0) { |
555 | return EINVAL; | |
556 | } | |
1c79356b A |
557 | |
558 | dev = vp->v_rdev; | |
559 | ||
560 | devBlockSize = vp->v_specsize; | |
561 | ||
0a7de745 A |
562 | if (devBlockSize > PAGE_SIZE) { |
563 | return EINVAL; | |
564 | } | |
1c79356b | 565 | |
0a7de745 | 566 | bscale = PAGE_SIZE / devBlockSize; |
1c79356b A |
567 | bsize = bscale * devBlockSize; |
568 | ||
569 | do { | |
570 | on = uio->uio_offset % bsize; | |
571 | ||
0a7de745 A |
572 | bn = (daddr64_t)((uio->uio_offset / devBlockSize) & ~(bscale - 1)); |
573 | ||
91447636 | 574 | if (vp->v_speclastr + bscale == bn) { |
0a7de745 | 575 | nextbn = bn + bscale; |
91447636 | 576 | error = buf_breadn(vp, bn, (int)bsize, &nextbn, |
0a7de745 A |
577 | (int *)&bsize, 1, NOCRED, &bp); |
578 | } else { | |
579 | error = buf_bread(vp, bn, (int)bsize, NOCRED, &bp); | |
580 | } | |
91447636 A |
581 | |
582 | vnode_lock(vp); | |
583 | vp->v_speclastr = bn; | |
584 | vnode_unlock(vp); | |
1c79356b | 585 | |
91447636 | 586 | n = bsize - buf_resid(bp); |
1c79356b | 587 | if ((on > n) || error) { |
0a7de745 A |
588 | if (!error) { |
589 | error = EINVAL; | |
590 | } | |
91447636 | 591 | buf_brelse(bp); |
0a7de745 | 592 | return error; |
1c79356b | 593 | } |
f427ee49 | 594 | n = MIN((n - on), (size_t)uio_resid(uio)); |
1c79356b | 595 | |
f427ee49 | 596 | error = uiomove((char *)buf_dataptr(bp) + on, (int)n, uio); |
0a7de745 | 597 | if (n + on == bsize) { |
91447636 | 598 | buf_markaged(bp); |
0a7de745 | 599 | } |
91447636 A |
600 | buf_brelse(bp); |
601 | } while (error == 0 && uio_resid(uio) > 0 && n != 0); | |
0a7de745 | 602 | return error; |
1c79356b A |
603 | |
604 | default: | |
605 | panic("spec_read type"); | |
606 | } | |
607 | /* NOTREACHED */ | |
91447636 | 608 | |
0a7de745 | 609 | return 0; |
1c79356b A |
610 | } |
611 | ||
612 | /* | |
613 | * Vnode op for write | |
614 | */ | |
91447636 | 615 | int |
2d21ac55 | 616 | spec_write(struct vnop_write_args *ap) |
1c79356b | 617 | { |
2d21ac55 A |
618 | struct vnode *vp = ap->a_vp; |
619 | struct uio *uio = ap->a_uio; | |
1c79356b | 620 | struct buf *bp; |
91447636 | 621 | daddr64_t bn; |
f427ee49 | 622 | int blkmask, bscale; |
2d21ac55 | 623 | int io_sync; |
0a7de745 | 624 | int devBlockSize = 0; |
f427ee49 | 625 | size_t bsize, n, on; |
1c79356b A |
626 | int error = 0; |
627 | dev_t dev; | |
628 | ||
629 | #if DIAGNOSTIC | |
0a7de745 | 630 | if (uio->uio_rw != UIO_WRITE) { |
1c79356b | 631 | panic("spec_write mode"); |
0a7de745 A |
632 | } |
633 | if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg)) { | |
1c79356b | 634 | panic("spec_write proc"); |
0a7de745 | 635 | } |
1c79356b A |
636 | #endif |
637 | ||
638 | switch (vp->v_type) { | |
1c79356b | 639 | case VCHR: |
0a7de745 A |
640 | { |
641 | struct _throttle_io_info_t *throttle_info = NULL; | |
642 | int thread_throttle_level; | |
cb323159 A |
643 | dev = vp->v_rdev; |
644 | devBlockSize = vp->v_specsize; | |
645 | uint32_t iolen = 0; | |
646 | uint64_t blkno = 0; | |
647 | int ddisk = 0; | |
648 | int ktrace_code = 0; // write is implied; read must be OR'd in. | |
649 | uintptr_t our_id; | |
650 | ||
651 | if (cdevsw[major(dev)].d_type == D_DISK) { | |
652 | ddisk = 1; | |
653 | } | |
654 | ||
655 | if (ddisk && vp->v_un.vu_specinfo->si_throttleable) { | |
7ddcb079 A |
656 | throttle_info = &_throttle_io_info[vp->v_un.vu_specinfo->si_devbsdunit]; |
657 | ||
0a7de745 | 658 | thread_throttle_level = throttle_info_update_internal(throttle_info, NULL, 0, vp->v_un.vu_specinfo->si_isssd, TRUE, NULL); |
7ddcb079 | 659 | |
316670eb | 660 | microuptime(&throttle_info->throttle_last_write_timestamp); |
0a7de745 | 661 | } |
cb323159 A |
662 | |
663 | if (kdebug_enable && ddisk) { | |
664 | if (devBlockSize == 0) { | |
665 | devBlockSize = 512; // default sector size | |
666 | } | |
667 | if ((uio_offset(uio) != 0) && devBlockSize) { | |
668 | blkno = ((uint64_t)uio_offset(uio)) / ((uint64_t)devBlockSize); | |
669 | } | |
670 | iolen = (int)uio_resid(uio); | |
671 | our_id = (uintptr_t)thread_tid(current_thread()); | |
672 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, | |
673 | (FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id, | |
674 | vp->v_rdev, blkno, iolen, 0); | |
675 | } | |
1c79356b | 676 | error = (*cdevsw[major(vp->v_rdev)].d_write) |
0a7de745 | 677 | (vp->v_rdev, uio, ap->a_ioflag); |
39037602 | 678 | |
cb323159 A |
679 | if (kdebug_enable && ddisk) { |
680 | //emit the I/O completion | |
681 | uint32_t residual = (uint32_t)uio_resid(uio); | |
682 | ktrace_code |= DKIO_DONE; | |
683 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, | |
684 | (FSDBG_CODE(DBG_DKRW, ktrace_code)) | DBG_FUNC_NONE, our_id, | |
685 | (uintptr_t)VM_KERNEL_ADDRPERM(vp), residual, error, 0); | |
686 | } | |
687 | ||
0a7de745 A |
688 | if (throttle_info) { |
689 | throttle_info_end_io_internal(throttle_info, thread_throttle_level); | |
39037602 | 690 | } |
1c79356b | 691 | |
0a7de745 A |
692 | return error; |
693 | } | |
694 | ||
1c79356b | 695 | case VBLK: |
0a7de745 A |
696 | if (uio_resid(uio) == 0) { |
697 | return 0; | |
698 | } | |
699 | if (uio->uio_offset < 0) { | |
700 | return EINVAL; | |
701 | } | |
1c79356b A |
702 | |
703 | io_sync = (ap->a_ioflag & IO_SYNC); | |
1c79356b A |
704 | |
705 | dev = (vp->v_rdev); | |
706 | ||
707 | devBlockSize = vp->v_specsize; | |
0a7de745 A |
708 | if (devBlockSize > PAGE_SIZE) { |
709 | return EINVAL; | |
710 | } | |
1c79356b | 711 | |
0a7de745 | 712 | bscale = PAGE_SIZE / devBlockSize; |
1c79356b A |
713 | blkmask = bscale - 1; |
714 | bsize = bscale * devBlockSize; | |
0a7de745 | 715 | |
1c79356b A |
716 | |
717 | do { | |
0a7de745 | 718 | bn = (daddr64_t)((uio->uio_offset / devBlockSize) & ~blkmask); |
1c79356b A |
719 | on = uio->uio_offset % bsize; |
720 | ||
f427ee49 | 721 | n = MIN((bsize - on), (size_t)uio_resid(uio)); |
1c79356b | 722 | |
55e303ae | 723 | /* |
91447636 | 724 | * Use buf_getblk() as an optimization IFF: |
55e303ae A |
725 | * |
726 | * 1) We are reading exactly a block on a block | |
727 | * aligned boundary | |
728 | * 2) We know the size of the device from spec_open | |
729 | * 3) The read doesn't span the end of the device | |
730 | * | |
91447636 | 731 | * Otherwise, we fall back on buf_bread(). |
55e303ae A |
732 | */ |
733 | if (n == bsize && | |
734 | vp->v_specdevsize != (u_int64_t)0 && | |
735 | (uio->uio_offset + (u_int64_t)n) > vp->v_specdevsize) { | |
0a7de745 A |
736 | /* reduce the size of the read to what is there */ |
737 | n = (uio->uio_offset + (u_int64_t)n) - vp->v_specdevsize; | |
55e303ae A |
738 | } |
739 | ||
0a7de745 | 740 | if (n == bsize) { |
f427ee49 | 741 | bp = buf_getblk(vp, bn, (int)bsize, 0, 0, BLK_WRITE); |
0a7de745 | 742 | } else { |
f427ee49 | 743 | error = (int)buf_bread(vp, bn, (int)bsize, NOCRED, &bp); |
0a7de745 | 744 | } |
1c79356b | 745 | |
55e303ae | 746 | /* Translate downstream error for upstream, if needed */ |
0a7de745 | 747 | if (!error) { |
91447636 | 748 | error = (int)buf_error(bp); |
0a7de745 | 749 | } |
1c79356b | 750 | if (error) { |
91447636 | 751 | buf_brelse(bp); |
0a7de745 | 752 | return error; |
1c79356b | 753 | } |
f427ee49 | 754 | n = MIN(n, bsize - buf_resid(bp)); |
1c79356b | 755 | |
f427ee49 | 756 | error = uiomove((char *)buf_dataptr(bp) + on, (int)n, uio); |
91447636 A |
757 | if (error) { |
758 | buf_brelse(bp); | |
0a7de745 | 759 | return error; |
91447636 A |
760 | } |
761 | buf_markaged(bp); | |
1c79356b | 762 | |
0a7de745 A |
763 | if (io_sync) { |
764 | error = buf_bwrite(bp); | |
765 | } else { | |
766 | if ((n + on) == bsize) { | |
767 | error = buf_bawrite(bp); | |
768 | } else { | |
769 | error = buf_bdwrite(bp); | |
770 | } | |
1c79356b | 771 | } |
91447636 | 772 | } while (error == 0 && uio_resid(uio) > 0 && n != 0); |
0a7de745 | 773 | return error; |
1c79356b A |
774 | |
775 | default: | |
776 | panic("spec_write type"); | |
777 | } | |
778 | /* NOTREACHED */ | |
91447636 | 779 | |
0a7de745 | 780 | return 0; |
1c79356b A |
781 | } |
782 | ||
783 | /* | |
784 | * Device ioctl operation. | |
785 | */ | |
91447636 | 786 | int |
2d21ac55 | 787 | spec_ioctl(struct vnop_ioctl_args *ap) |
1c79356b | 788 | { |
91447636 | 789 | proc_t p = vfs_context_proc(ap->a_context); |
1c79356b | 790 | dev_t dev = ap->a_vp->v_rdev; |
0a7de745 | 791 | int retval = 0; |
b0d623f7 A |
792 | |
793 | KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL, 0) | DBG_FUNC_START, | |
0a7de745 | 794 | dev, ap->a_command, ap->a_fflag, ap->a_vp->v_type, 0); |
1c79356b A |
795 | |
796 | switch (ap->a_vp->v_type) { | |
1c79356b | 797 | case VCHR: |
b0d623f7 | 798 | retval = (*cdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data, |
0a7de745 | 799 | ap->a_fflag, p); |
b0d623f7 | 800 | break; |
1c79356b A |
801 | |
802 | case VBLK: | |
316670eb | 803 | retval = (*bdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data, ap->a_fflag, p); |
0a7de745 | 804 | if (!retval && ap->a_command == DKIOCSETBLOCKSIZE) { |
39037602 | 805 | ap->a_vp->v_specsize = *(uint32_t *)ap->a_data; |
0a7de745 | 806 | } |
b0d623f7 | 807 | break; |
1c79356b A |
808 | |
809 | default: | |
810 | panic("spec_ioctl"); | |
811 | /* NOTREACHED */ | |
812 | } | |
b0d623f7 | 813 | KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_IOCTL, 0) | DBG_FUNC_END, |
0a7de745 | 814 | dev, ap->a_command, ap->a_fflag, retval, 0); |
b0d623f7 | 815 | |
0a7de745 | 816 | return retval; |
1c79356b A |
817 | } |
818 | ||
91447636 | 819 | int |
2d21ac55 | 820 | spec_select(struct vnop_select_args *ap) |
1c79356b | 821 | { |
91447636 | 822 | proc_t p = vfs_context_proc(ap->a_context); |
2d21ac55 | 823 | dev_t dev; |
1c79356b A |
824 | |
825 | switch (ap->a_vp->v_type) { | |
1c79356b | 826 | default: |
0a7de745 | 827 | return 1; /* XXX */ |
1c79356b A |
828 | |
829 | case VCHR: | |
830 | dev = ap->a_vp->v_rdev; | |
91447636 | 831 | return (*cdevsw[major(dev)].d_select)(dev, ap->a_which, ap->a_wql, p); |
1c79356b A |
832 | } |
833 | } | |
91447636 | 834 | |
cb323159 | 835 | static int filt_specattach(struct knote *kn, struct kevent_qos_s *kev); |
6d2010ae | 836 | |
b0d623f7 | 837 | int |
cb323159 | 838 | spec_kqfilter(vnode_t vp, struct knote *kn, struct kevent_qos_s *kev) |
b0d623f7 A |
839 | { |
840 | dev_t dev; | |
3e170ce0 A |
841 | |
842 | assert(vnode_ischr(vp)); | |
b0d623f7 | 843 | |
b0d623f7 A |
844 | dev = vnode_specrdev(vp); |
845 | ||
39236c6e | 846 | #if NETWORKING |
39037602 A |
847 | /* |
848 | * Try a bpf device, as defined in bsd/net/bpf.c | |
849 | * If it doesn't error out the attach, then it | |
850 | * claimed it. Otherwise, fall through and try | |
5ba3f43e | 851 | * other attaches. |
39037602 A |
852 | */ |
853 | int32_t tmp_flags = kn->kn_flags; | |
cb323159 | 854 | int64_t tmp_sdata = kn->kn_sdata; |
39037602 A |
855 | int res; |
856 | ||
857 | res = bpfkqfilter(dev, kn); | |
858 | if ((kn->kn_flags & EV_ERROR) == 0) { | |
859 | return res; | |
b0d623f7 | 860 | } |
39037602 | 861 | kn->kn_flags = tmp_flags; |
cb323159 | 862 | kn->kn_sdata = tmp_sdata; |
3e170ce0 | 863 | #endif |
b0d623f7 | 864 | |
5ba3f43e A |
865 | if (major(dev) > nchrdev) { |
866 | knote_set_error(kn, ENXIO); | |
867 | return 0; | |
868 | } | |
869 | ||
870 | kn->kn_vnode_kqok = !!(cdevsw_flags[major(dev)] & CDEVSW_SELECT_KQUEUE); | |
871 | kn->kn_vnode_use_ofst = !!(cdevsw_flags[major(dev)] & CDEVSW_USE_OFFSET); | |
872 | ||
873 | if (cdevsw_flags[major(dev)] & CDEVSW_IS_PTS) { | |
874 | kn->kn_filtid = EVFILTID_PTSD; | |
875 | return ptsd_kqfilter(dev, kn); | |
5c9f4661 A |
876 | } else if (cdevsw_flags[major(dev)] & CDEVSW_IS_PTC) { |
877 | kn->kn_filtid = EVFILTID_PTMX; | |
878 | return ptmx_kqfilter(dev, kn); | |
879 | } else if (cdevsw[major(dev)].d_type == D_TTY && kn->kn_vnode_kqok) { | |
5ba3f43e A |
880 | /* |
881 | * TTYs from drivers that use struct ttys use their own filter | |
882 | * routines. The PTC driver doesn't use the tty for character | |
883 | * counts, so it must go through the select fallback. | |
884 | */ | |
885 | kn->kn_filtid = EVFILTID_TTY; | |
886 | return knote_fops(kn)->f_attach(kn, kev); | |
887 | } | |
888 | ||
39037602 | 889 | /* Try to attach to other char special devices */ |
5ba3f43e | 890 | return filt_specattach(kn, kev); |
b0d623f7 A |
891 | } |
892 | ||
1c79356b A |
893 | /* |
894 | * Synch buffers associated with a block device | |
895 | */ | |
1c79356b | 896 | int |
91447636 | 897 | spec_fsync_internal(vnode_t vp, int waitfor, __unused vfs_context_t context) |
1c79356b | 898 | { |
0a7de745 A |
899 | if (vp->v_type == VCHR) { |
900 | return 0; | |
901 | } | |
1c79356b A |
902 | /* |
903 | * Flush all dirty buffers associated with a block device. | |
904 | */ | |
b0d623f7 | 905 | buf_flushdirtyblks(vp, (waitfor == MNT_WAIT || waitfor == MNT_DWAIT), 0, "spec_fsync"); |
91447636 | 906 | |
0a7de745 | 907 | return 0; |
1c79356b A |
908 | } |
909 | ||
91447636 | 910 | int |
2d21ac55 | 911 | spec_fsync(struct vnop_fsync_args *ap) |
91447636 A |
912 | { |
913 | return spec_fsync_internal(ap->a_vp, ap->a_waitfor, ap->a_context); | |
914 | } | |
915 | ||
316670eb | 916 | |
1c79356b A |
917 | /* |
918 | * Just call the device strategy routine | |
919 | */ | |
316670eb A |
920 | void throttle_init(void); |
921 | ||
2d21ac55 | 922 | |
0a7de745 A |
923 | #if 0 |
924 | #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...) \ | |
925 | do { \ | |
926 | if ((debug_info)->alloc) \ | |
927 | printf("%s: "format, __FUNCTION__, ## args); \ | |
b0d623f7 A |
928 | } while(0) |
929 | ||
0a7de745 | 930 | #else |
b0d623f7 A |
931 | #define DEBUG_ALLOC_THROTTLE_INFO(format, debug_info, args...) |
932 | #endif | |
933 | ||
39236c6e A |
934 | |
935 | SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier1_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER1], 0, ""); | |
936 | SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier2_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER2], 0, ""); | |
937 | SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier3_window_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_windows_msecs[THROTTLE_LEVEL_TIER3], 0, ""); | |
938 | ||
939 | SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier1_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER1], 0, ""); | |
940 | SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier2_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER2], 0, ""); | |
941 | SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_tier3_io_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &throttle_io_period_msecs[THROTTLE_LEVEL_TIER3], 0, ""); | |
942 | ||
943 | 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, ""); | |
944 | 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, ""); | |
945 | 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, ""); | |
946 | ||
947 | SYSCTL_INT(_debug, OID_AUTO, lowpri_throttle_enabled, CTLFLAG_RW | CTLFLAG_LOCKED, &lowpri_throttle_enabled, 0, ""); | |
948 | ||
316670eb | 949 | |
c3c9b80d | 950 | static LCK_GRP_DECLARE(throttle_lock_grp, "throttle I/O"); |
316670eb | 951 | |
6d2010ae A |
952 | |
953 | /* | |
954 | * throttled I/O helper function | |
955 | * convert the index of the lowest set bit to a device index | |
956 | */ | |
957 | int | |
958 | num_trailing_0(uint64_t n) | |
959 | { | |
960 | /* | |
961 | * since in most cases the number of trailing 0s is very small, | |
316670eb | 962 | * we simply counting sequentially from the lowest bit |
6d2010ae | 963 | */ |
0a7de745 | 964 | if (n == 0) { |
6d2010ae | 965 | return sizeof(n) * 8; |
0a7de745 | 966 | } |
6d2010ae A |
967 | int count = 0; |
968 | while (!ISSET(n, 1)) { | |
969 | n >>= 1; | |
970 | ++count; | |
971 | } | |
972 | return count; | |
973 | } | |
2d21ac55 | 974 | |
316670eb | 975 | |
b0d623f7 A |
976 | /* |
977 | * Release the reference and if the item was allocated and this is the last | |
978 | * reference then free it. | |
979 | * | |
980 | * This routine always returns the old value. | |
981 | */ | |
982 | static int | |
983 | throttle_info_rel(struct _throttle_io_info_t *info) | |
984 | { | |
316670eb | 985 | SInt32 oldValue = OSDecrementAtomic(&info->throttle_refcnt); |
b0d623f7 | 986 | |
0a7de745 A |
987 | DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n", |
988 | info, (int)(oldValue - 1), info ); | |
b0d623f7 A |
989 | |
990 | /* The reference count just went negative, very bad */ | |
0a7de745 | 991 | if (oldValue == 0) { |
b0d623f7 | 992 | panic("throttle info ref cnt went negative!"); |
0a7de745 | 993 | } |
b0d623f7 | 994 | |
0a7de745 A |
995 | /* |
996 | * Once reference count is zero, no one else should be able to take a | |
997 | * reference | |
b0d623f7 | 998 | */ |
316670eb A |
999 | if ((info->throttle_refcnt == 0) && (info->throttle_alloc)) { |
1000 | DEBUG_ALLOC_THROTTLE_INFO("Freeing info = %p\n", info); | |
0a7de745 | 1001 | |
c3c9b80d | 1002 | lck_mtx_destroy(&info->throttle_lock, &throttle_lock_grp); |
0a7de745 | 1003 | FREE(info, M_TEMP); |
b0d623f7 A |
1004 | } |
1005 | return oldValue; | |
1006 | } | |
1007 | ||
316670eb | 1008 | |
b0d623f7 A |
1009 | /* |
1010 | * Just take a reference on the throttle info structure. | |
1011 | * | |
1012 | * This routine always returns the old value. | |
1013 | */ | |
1014 | static SInt32 | |
1015 | throttle_info_ref(struct _throttle_io_info_t *info) | |
1016 | { | |
316670eb | 1017 | SInt32 oldValue = OSIncrementAtomic(&info->throttle_refcnt); |
b0d623f7 | 1018 | |
0a7de745 A |
1019 | DEBUG_ALLOC_THROTTLE_INFO("refcnt = %d info = %p\n", |
1020 | info, (int)(oldValue - 1), info ); | |
b0d623f7 | 1021 | /* Allocated items should never have a reference of zero */ |
0a7de745 | 1022 | if (info->throttle_alloc && (oldValue == 0)) { |
b0d623f7 | 1023 | panic("Taking a reference without calling create throttle info!\n"); |
0a7de745 | 1024 | } |
b0d623f7 A |
1025 | |
1026 | return oldValue; | |
1027 | } | |
1028 | ||
316670eb A |
1029 | /* |
1030 | * on entry the throttle_lock is held... | |
1031 | * this function is responsible for taking | |
1032 | * and dropping the reference on the info | |
1033 | * structure which will keep it from going | |
1034 | * away while the timer is running if it | |
1035 | * happens to have been dynamically allocated by | |
1036 | * a network fileystem kext which is now trying | |
1037 | * to free it | |
1038 | */ | |
1039 | static uint32_t | |
39236c6e | 1040 | throttle_timer_start(struct _throttle_io_info_t *info, boolean_t update_io_count, int wakelevel) |
0a7de745 | 1041 | { |
316670eb | 1042 | struct timeval elapsed; |
39236c6e A |
1043 | struct timeval now; |
1044 | struct timeval period; | |
0a7de745 A |
1045 | uint64_t elapsed_msecs; |
1046 | int throttle_level; | |
1047 | int level; | |
1048 | int msecs; | |
1049 | boolean_t throttled = FALSE; | |
1050 | boolean_t need_timer = FALSE; | |
39236c6e A |
1051 | |
1052 | microuptime(&now); | |
316670eb A |
1053 | |
1054 | if (update_io_count == TRUE) { | |
1055 | info->throttle_io_count_begin = info->throttle_io_count; | |
1056 | info->throttle_io_period_num++; | |
1057 | ||
0a7de745 | 1058 | while (wakelevel >= THROTTLE_LEVEL_THROTTLED) { |
39236c6e | 1059 | info->throttle_start_IO_period_timestamp[wakelevel--] = now; |
0a7de745 | 1060 | } |
39236c6e A |
1061 | |
1062 | info->throttle_min_timer_deadline = now; | |
1063 | ||
1064 | msecs = info->throttle_io_periods[THROTTLE_LEVEL_THROTTLED]; | |
1065 | period.tv_sec = msecs / 1000; | |
1066 | period.tv_usec = (msecs % 1000) * 1000; | |
1067 | ||
1068 | timevaladd(&info->throttle_min_timer_deadline, &period); | |
316670eb A |
1069 | } |
1070 | for (throttle_level = THROTTLE_LEVEL_START; throttle_level < THROTTLE_LEVEL_END; throttle_level++) { | |
39236c6e A |
1071 | elapsed = now; |
1072 | timevalsub(&elapsed, &info->throttle_window_start_timestamp[throttle_level]); | |
db609669 | 1073 | elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000); |
316670eb | 1074 | |
39236c6e | 1075 | for (level = throttle_level + 1; level <= THROTTLE_LEVEL_END; level++) { |
39236c6e | 1076 | if (!TAILQ_EMPTY(&info->throttle_uthlist[level])) { |
d190cdc3 | 1077 | if (elapsed_msecs < (uint64_t)throttle_windows_msecs[level] || info->throttle_inflight_count[throttle_level]) { |
39236c6e A |
1078 | /* |
1079 | * we had an I/O occur at a higher priority tier within | |
1080 | * this tier's throttle window | |
1081 | */ | |
1082 | throttled = TRUE; | |
1083 | } | |
1084 | /* | |
1085 | * we assume that the windows are the same or longer | |
1086 | * as we drop through the throttling tiers... thus | |
1087 | * we can stop looking once we run into a tier with | |
1088 | * threads to schedule regardless of whether it's | |
1089 | * still in its throttling window or not | |
1090 | */ | |
1091 | break; | |
1092 | } | |
316670eb | 1093 | } |
0a7de745 | 1094 | if (throttled == TRUE) { |
39236c6e | 1095 | break; |
0a7de745 | 1096 | } |
316670eb | 1097 | } |
39236c6e | 1098 | if (throttled == TRUE) { |
0a7de745 | 1099 | uint64_t deadline = 0; |
39236c6e A |
1100 | struct timeval target; |
1101 | struct timeval min_target; | |
316670eb | 1102 | |
0a7de745 | 1103 | /* |
39236c6e A |
1104 | * we've got at least one tier still in a throttled window |
1105 | * so we need a timer running... compute the next deadline | |
1106 | * and schedule it | |
316670eb | 1107 | */ |
0a7de745 A |
1108 | for (level = throttle_level + 1; level <= THROTTLE_LEVEL_END; level++) { |
1109 | if (TAILQ_EMPTY(&info->throttle_uthlist[level])) { | |
39236c6e | 1110 | continue; |
0a7de745 | 1111 | } |
39236c6e A |
1112 | |
1113 | target = info->throttle_start_IO_period_timestamp[level]; | |
1114 | ||
1115 | msecs = info->throttle_io_periods[level]; | |
1116 | period.tv_sec = msecs / 1000; | |
1117 | period.tv_usec = (msecs % 1000) * 1000; | |
1118 | ||
1119 | timevaladd(&target, &period); | |
0a7de745 | 1120 | |
39236c6e A |
1121 | if (need_timer == FALSE || timevalcmp(&target, &min_target, <)) { |
1122 | min_target = target; | |
1123 | need_timer = TRUE; | |
1124 | } | |
1125 | } | |
1126 | if (timevalcmp(&info->throttle_min_timer_deadline, &now, >)) { | |
0a7de745 A |
1127 | if (timevalcmp(&info->throttle_min_timer_deadline, &min_target, >)) { |
1128 | min_target = info->throttle_min_timer_deadline; | |
1129 | } | |
39236c6e A |
1130 | } |
1131 | ||
1132 | if (info->throttle_timer_active) { | |
1133 | if (thread_call_cancel(info->throttle_timer_call) == FALSE) { | |
1134 | /* | |
1135 | * couldn't kill the timer because it's already | |
1136 | * been dispatched, so don't try to start a new | |
1137 | * one... once we drop the lock, the timer will | |
1138 | * proceed and eventually re-run this function | |
1139 | */ | |
1140 | need_timer = FALSE; | |
0a7de745 | 1141 | } else { |
39236c6e | 1142 | info->throttle_timer_active = 0; |
0a7de745 | 1143 | } |
39236c6e A |
1144 | } |
1145 | if (need_timer == TRUE) { | |
1146 | /* | |
1147 | * This is defined as an int (32-bit) rather than a 64-bit | |
1148 | * value because it would need a really big period in the | |
1149 | * order of ~500 days to overflow this. So, we let this be | |
1150 | * 32-bit which allows us to use the clock_interval_to_deadline() | |
1151 | * routine. | |
1152 | */ | |
0a7de745 | 1153 | int target_msecs; |
316670eb | 1154 | |
39236c6e A |
1155 | if (info->throttle_timer_ref == 0) { |
1156 | /* | |
1157 | * take a reference for the timer | |
1158 | */ | |
1159 | throttle_info_ref(info); | |
316670eb | 1160 | |
39236c6e A |
1161 | info->throttle_timer_ref = 1; |
1162 | } | |
1163 | elapsed = min_target; | |
1164 | timevalsub(&elapsed, &now); | |
f427ee49 | 1165 | target_msecs = (int)(elapsed.tv_sec * 1000 + elapsed.tv_usec / 1000); |
39236c6e A |
1166 | |
1167 | if (target_msecs <= 0) { | |
1168 | /* | |
1169 | * we may have computed a deadline slightly in the past | |
1170 | * due to various factors... if so, just set the timer | |
1171 | * to go off in the near future (we don't need to be precise) | |
1172 | */ | |
1173 | target_msecs = 1; | |
1174 | } | |
1175 | clock_interval_to_deadline(target_msecs, 1000000, &deadline); | |
1176 | ||
1177 | thread_call_enter_delayed(info->throttle_timer_call, deadline); | |
1178 | info->throttle_timer_active = 1; | |
1179 | } | |
1180 | } | |
0a7de745 | 1181 | return throttle_level; |
316670eb A |
1182 | } |
1183 | ||
1184 | ||
1185 | static void | |
1186 | throttle_timer(struct _throttle_io_info_t *info) | |
1187 | { | |
1188 | uthread_t ut, utlist; | |
0a7de745 A |
1189 | struct timeval elapsed; |
1190 | struct timeval now; | |
1191 | uint64_t elapsed_msecs; | |
1192 | int throttle_level; | |
1193 | int level; | |
1194 | int wake_level; | |
1195 | caddr_t wake_address = NULL; | |
1196 | boolean_t update_io_count = FALSE; | |
1197 | boolean_t need_wakeup = FALSE; | |
1198 | boolean_t need_release = FALSE; | |
316670eb | 1199 | |
39236c6e | 1200 | ut = NULL; |
0a7de745 | 1201 | lck_mtx_lock(&info->throttle_lock); |
39236c6e A |
1202 | |
1203 | info->throttle_timer_active = 0; | |
1204 | microuptime(&now); | |
1205 | ||
1206 | elapsed = now; | |
1207 | timevalsub(&elapsed, &info->throttle_start_IO_period_timestamp[THROTTLE_LEVEL_THROTTLED]); | |
db609669 | 1208 | elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000); |
316670eb | 1209 | |
39236c6e | 1210 | if (elapsed_msecs >= (uint64_t)info->throttle_io_periods[THROTTLE_LEVEL_THROTTLED]) { |
39236c6e A |
1211 | wake_level = info->throttle_next_wake_level; |
1212 | ||
1213 | for (level = THROTTLE_LEVEL_START; level < THROTTLE_LEVEL_END; level++) { | |
39236c6e A |
1214 | elapsed = now; |
1215 | timevalsub(&elapsed, &info->throttle_start_IO_period_timestamp[wake_level]); | |
1216 | elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000); | |
1217 | ||
1218 | if (elapsed_msecs >= (uint64_t)info->throttle_io_periods[wake_level] && !TAILQ_EMPTY(&info->throttle_uthlist[wake_level])) { | |
1219 | /* | |
1220 | * we're closing out the current IO period... | |
1221 | * if we have a waiting thread, wake it up | |
1222 | * after we have reset the I/O window info | |
1223 | */ | |
1224 | need_wakeup = TRUE; | |
1225 | update_io_count = TRUE; | |
1226 | ||
1227 | info->throttle_next_wake_level = wake_level - 1; | |
1228 | ||
0a7de745 | 1229 | if (info->throttle_next_wake_level == THROTTLE_LEVEL_START) { |
39236c6e | 1230 | info->throttle_next_wake_level = THROTTLE_LEVEL_END; |
0a7de745 | 1231 | } |
39236c6e A |
1232 | |
1233 | break; | |
1234 | } | |
1235 | wake_level--; | |
1236 | ||
0a7de745 | 1237 | if (wake_level == THROTTLE_LEVEL_START) { |
39236c6e | 1238 | wake_level = THROTTLE_LEVEL_END; |
0a7de745 | 1239 | } |
39236c6e | 1240 | } |
316670eb | 1241 | } |
39236c6e A |
1242 | if (need_wakeup == TRUE) { |
1243 | if (!TAILQ_EMPTY(&info->throttle_uthlist[wake_level])) { | |
39236c6e A |
1244 | ut = (uthread_t)TAILQ_FIRST(&info->throttle_uthlist[wake_level]); |
1245 | TAILQ_REMOVE(&info->throttle_uthlist[wake_level], ut, uu_throttlelist); | |
1246 | ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE; | |
d9a64523 | 1247 | ut->uu_is_throttled = false; |
39236c6e A |
1248 | |
1249 | wake_address = (caddr_t)&ut->uu_on_throttlelist; | |
1250 | } | |
0a7de745 | 1251 | } else { |
39236c6e | 1252 | wake_level = THROTTLE_LEVEL_START; |
0a7de745 | 1253 | } |
39236c6e | 1254 | |
0a7de745 | 1255 | throttle_level = throttle_timer_start(info, update_io_count, wake_level); |
39236c6e | 1256 | |
0a7de745 | 1257 | if (wake_address != NULL) { |
39236c6e | 1258 | wakeup(wake_address); |
0a7de745 | 1259 | } |
39236c6e A |
1260 | |
1261 | for (level = THROTTLE_LEVEL_THROTTLED; level <= throttle_level; level++) { | |
39236c6e | 1262 | TAILQ_FOREACH_SAFE(ut, &info->throttle_uthlist[level], uu_throttlelist, utlist) { |
39236c6e A |
1263 | TAILQ_REMOVE(&info->throttle_uthlist[level], ut, uu_throttlelist); |
1264 | ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE; | |
d9a64523 | 1265 | ut->uu_is_throttled = false; |
316670eb A |
1266 | |
1267 | wakeup(&ut->uu_on_throttlelist); | |
1268 | } | |
1269 | } | |
39236c6e A |
1270 | if (info->throttle_timer_active == 0 && info->throttle_timer_ref) { |
1271 | info->throttle_timer_ref = 0; | |
1272 | need_release = TRUE; | |
316670eb | 1273 | } |
0a7de745 | 1274 | lck_mtx_unlock(&info->throttle_lock); |
316670eb | 1275 | |
0a7de745 | 1276 | if (need_release == TRUE) { |
316670eb | 1277 | throttle_info_rel(info); |
0a7de745 | 1278 | } |
316670eb A |
1279 | } |
1280 | ||
1281 | ||
39236c6e A |
1282 | static int |
1283 | throttle_add_to_list(struct _throttle_io_info_t *info, uthread_t ut, int mylevel, boolean_t insert_tail) | |
1284 | { | |
1285 | boolean_t start_timer = FALSE; | |
1286 | int level = THROTTLE_LEVEL_START; | |
1287 | ||
1288 | if (TAILQ_EMPTY(&info->throttle_uthlist[mylevel])) { | |
1289 | info->throttle_start_IO_period_timestamp[mylevel] = info->throttle_last_IO_timestamp[mylevel]; | |
1290 | start_timer = TRUE; | |
1291 | } | |
1292 | ||
0a7de745 | 1293 | if (insert_tail == TRUE) { |
39236c6e | 1294 | TAILQ_INSERT_TAIL(&info->throttle_uthlist[mylevel], ut, uu_throttlelist); |
0a7de745 | 1295 | } else { |
39236c6e | 1296 | TAILQ_INSERT_HEAD(&info->throttle_uthlist[mylevel], ut, uu_throttlelist); |
0a7de745 | 1297 | } |
39236c6e | 1298 | |
f427ee49 | 1299 | ut->uu_on_throttlelist = (int8_t)mylevel; |
39236c6e A |
1300 | |
1301 | if (start_timer == TRUE) { | |
1302 | /* we may need to start or rearm the timer */ | |
1303 | level = throttle_timer_start(info, FALSE, THROTTLE_LEVEL_START); | |
1304 | ||
1305 | if (level == THROTTLE_LEVEL_END) { | |
1306 | if (ut->uu_on_throttlelist >= THROTTLE_LEVEL_THROTTLED) { | |
1307 | TAILQ_REMOVE(&info->throttle_uthlist[ut->uu_on_throttlelist], ut, uu_throttlelist); | |
1308 | ||
1309 | ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE; | |
1310 | } | |
1311 | } | |
1312 | } | |
0a7de745 | 1313 | return level; |
39236c6e A |
1314 | } |
1315 | ||
1316 | static void | |
1317 | throttle_init_throttle_window(void) | |
1318 | { | |
1319 | int throttle_window_size; | |
1320 | ||
1321 | /* | |
1322 | * The hierarchy of throttle window values is as follows: | |
1323 | * - Global defaults | |
1324 | * - Device tree properties | |
1325 | * - Boot-args | |
1326 | * All values are specified in msecs. | |
1327 | */ | |
1328 | ||
1329 | /* Override global values with device-tree properties */ | |
0a7de745 | 1330 | if (PE_get_default("kern.io_throttle_window_tier1", &throttle_window_size, sizeof(throttle_window_size))) { |
39236c6e | 1331 | throttle_windows_msecs[THROTTLE_LEVEL_TIER1] = throttle_window_size; |
0a7de745 | 1332 | } |
39236c6e | 1333 | |
0a7de745 | 1334 | if (PE_get_default("kern.io_throttle_window_tier2", &throttle_window_size, sizeof(throttle_window_size))) { |
39236c6e | 1335 | throttle_windows_msecs[THROTTLE_LEVEL_TIER2] = throttle_window_size; |
0a7de745 | 1336 | } |
39236c6e | 1337 | |
0a7de745 | 1338 | if (PE_get_default("kern.io_throttle_window_tier3", &throttle_window_size, sizeof(throttle_window_size))) { |
39236c6e | 1339 | throttle_windows_msecs[THROTTLE_LEVEL_TIER3] = throttle_window_size; |
0a7de745 A |
1340 | } |
1341 | ||
39236c6e | 1342 | /* Override with boot-args */ |
0a7de745 | 1343 | if (PE_parse_boot_argn("io_throttle_window_tier1", &throttle_window_size, sizeof(throttle_window_size))) { |
39236c6e | 1344 | throttle_windows_msecs[THROTTLE_LEVEL_TIER1] = throttle_window_size; |
0a7de745 | 1345 | } |
39236c6e | 1346 | |
0a7de745 | 1347 | if (PE_parse_boot_argn("io_throttle_window_tier2", &throttle_window_size, sizeof(throttle_window_size))) { |
39236c6e | 1348 | throttle_windows_msecs[THROTTLE_LEVEL_TIER2] = throttle_window_size; |
0a7de745 A |
1349 | } |
1350 | ||
1351 | if (PE_parse_boot_argn("io_throttle_window_tier3", &throttle_window_size, sizeof(throttle_window_size))) { | |
39236c6e | 1352 | throttle_windows_msecs[THROTTLE_LEVEL_TIER3] = throttle_window_size; |
0a7de745 | 1353 | } |
39236c6e A |
1354 | } |
1355 | ||
1356 | static void | |
1357 | throttle_init_throttle_period(struct _throttle_io_info_t *info, boolean_t isssd) | |
1358 | { | |
1359 | int throttle_period_size; | |
1360 | ||
1361 | /* | |
1362 | * The hierarchy of throttle period values is as follows: | |
1363 | * - Global defaults | |
1364 | * - Device tree properties | |
1365 | * - Boot-args | |
1366 | * All values are specified in msecs. | |
1367 | */ | |
1368 | ||
1369 | /* Assign global defaults */ | |
0a7de745 | 1370 | if ((isssd == TRUE) && (info->throttle_is_fusion_with_priority == 0)) { |
39236c6e | 1371 | info->throttle_io_periods = &throttle_io_period_ssd_msecs[0]; |
0a7de745 | 1372 | } else { |
39236c6e | 1373 | info->throttle_io_periods = &throttle_io_period_msecs[0]; |
0a7de745 | 1374 | } |
39236c6e A |
1375 | |
1376 | /* Override global values with device-tree properties */ | |
0a7de745 | 1377 | if (PE_get_default("kern.io_throttle_period_tier1", &throttle_period_size, sizeof(throttle_period_size))) { |
39236c6e | 1378 | info->throttle_io_periods[THROTTLE_LEVEL_TIER1] = throttle_period_size; |
0a7de745 A |
1379 | } |
1380 | ||
1381 | if (PE_get_default("kern.io_throttle_period_tier2", &throttle_period_size, sizeof(throttle_period_size))) { | |
39236c6e | 1382 | info->throttle_io_periods[THROTTLE_LEVEL_TIER2] = throttle_period_size; |
0a7de745 | 1383 | } |
39236c6e | 1384 | |
0a7de745 | 1385 | if (PE_get_default("kern.io_throttle_period_tier3", &throttle_period_size, sizeof(throttle_period_size))) { |
39236c6e | 1386 | info->throttle_io_periods[THROTTLE_LEVEL_TIER3] = throttle_period_size; |
0a7de745 A |
1387 | } |
1388 | ||
39236c6e | 1389 | /* Override with boot-args */ |
0a7de745 | 1390 | if (PE_parse_boot_argn("io_throttle_period_tier1", &throttle_period_size, sizeof(throttle_period_size))) { |
39236c6e | 1391 | info->throttle_io_periods[THROTTLE_LEVEL_TIER1] = throttle_period_size; |
0a7de745 A |
1392 | } |
1393 | ||
1394 | if (PE_parse_boot_argn("io_throttle_period_tier2", &throttle_period_size, sizeof(throttle_period_size))) { | |
39236c6e | 1395 | info->throttle_io_periods[THROTTLE_LEVEL_TIER2] = throttle_period_size; |
0a7de745 | 1396 | } |
39236c6e | 1397 | |
0a7de745 | 1398 | if (PE_parse_boot_argn("io_throttle_period_tier3", &throttle_period_size, sizeof(throttle_period_size))) { |
39236c6e | 1399 | info->throttle_io_periods[THROTTLE_LEVEL_TIER3] = throttle_period_size; |
0a7de745 | 1400 | } |
39236c6e A |
1401 | } |
1402 | ||
fe8ab488 | 1403 | #if CONFIG_IOSCHED |
0a7de745 A |
1404 | extern void vm_io_reprioritize_init(void); |
1405 | int iosched_enabled = 1; | |
fe8ab488 A |
1406 | #endif |
1407 | ||
316670eb A |
1408 | void |
1409 | throttle_init(void) | |
1410 | { | |
0a7de745 A |
1411 | struct _throttle_io_info_t *info; |
1412 | int i; | |
1413 | int level; | |
fe8ab488 | 1414 | #if CONFIG_IOSCHED |
0a7de745 | 1415 | int iosched; |
fe8ab488 | 1416 | #endif |
316670eb | 1417 | |
39236c6e A |
1418 | /* Update throttle parameters based on device tree configuration */ |
1419 | throttle_init_throttle_window(); | |
1420 | ||
316670eb | 1421 | for (i = 0; i < LOWPRI_MAX_NUM_DEV; i++) { |
0a7de745 A |
1422 | info = &_throttle_io_info[i]; |
1423 | ||
c3c9b80d | 1424 | lck_mtx_init(&info->throttle_lock, &throttle_lock_grp, LCK_ATTR_NULL); |
316670eb A |
1425 | info->throttle_timer_call = thread_call_allocate((thread_call_func_t)throttle_timer, (thread_call_param_t)info); |
1426 | ||
39236c6e A |
1427 | for (level = 0; level <= THROTTLE_LEVEL_END; level++) { |
1428 | TAILQ_INIT(&info->throttle_uthlist[level]); | |
1429 | info->throttle_last_IO_pid[level] = 0; | |
39037602 | 1430 | info->throttle_inflight_count[level] = 0; |
39236c6e A |
1431 | } |
1432 | info->throttle_next_wake_level = THROTTLE_LEVEL_END; | |
fe8ab488 | 1433 | info->throttle_disabled = 0; |
3e170ce0 | 1434 | info->throttle_is_fusion_with_priority = 0; |
fe8ab488 A |
1435 | } |
1436 | #if CONFIG_IOSCHED | |
1437 | if (PE_parse_boot_argn("iosched", &iosched, sizeof(iosched))) { | |
1438 | iosched_enabled = iosched; | |
1439 | } | |
1440 | if (iosched_enabled) { | |
1441 | /* Initialize I/O Reprioritization mechanism */ | |
1442 | vm_io_reprioritize_init(); | |
316670eb | 1443 | } |
fe8ab488 | 1444 | #endif |
316670eb A |
1445 | } |
1446 | ||
39236c6e | 1447 | void |
d9a64523 | 1448 | sys_override_io_throttle(boolean_t enable_override) |
39236c6e | 1449 | { |
0a7de745 | 1450 | if (enable_override) { |
39236c6e | 1451 | lowpri_throttle_enabled = 0; |
0a7de745 | 1452 | } else { |
d9a64523 | 1453 | lowpri_throttle_enabled = 1; |
0a7de745 | 1454 | } |
39236c6e A |
1455 | } |
1456 | ||
39037602 | 1457 | int rethrottle_wakeups = 0; |
316670eb A |
1458 | |
1459 | /* | |
39037602 A |
1460 | * the uu_rethrottle_lock is used to synchronize this function |
1461 | * with "throttle_lowpri_io" which is where a throttled thread | |
1462 | * will block... that function will grab this lock before beginning | |
1463 | * it's decision making process concerning the need to block, and | |
1464 | * hold it through the assert_wait. When that thread is awakened | |
1465 | * for any reason (timer or rethrottle), it will reacquire the | |
1466 | * uu_rethrottle_lock before determining if it really is ok for | |
1467 | * it to now run. This is the point at which the thread could | |
1468 | * enter a different throttling queue and reblock or return from | |
1469 | * the throttle w/o having waited out it's entire throttle if | |
1470 | * the rethrottle has now moved it out of any currently | |
1471 | * active throttle window. | |
39236c6e | 1472 | * |
39037602 A |
1473 | * |
1474 | * NOTES: | |
1475 | * 1 - This may be called with the task lock held. | |
1476 | * 2 - This may be called with preemption and interrupts disabled | |
1477 | * in the kqueue wakeup path so we can't take the throttle_lock which is a mutex | |
1478 | * 3 - This cannot safely dereference uu_throttle_info, as it may | |
1479 | * get deallocated out from under us | |
316670eb | 1480 | */ |
39236c6e | 1481 | |
316670eb | 1482 | void |
39236c6e | 1483 | rethrottle_thread(uthread_t ut) |
316670eb | 1484 | { |
39037602 A |
1485 | /* |
1486 | * If uthread doesn't have throttle state, then there's no chance | |
1487 | * of it needing a rethrottle. | |
1488 | */ | |
0a7de745 | 1489 | if (ut->uu_throttle_info == NULL) { |
39236c6e | 1490 | return; |
0a7de745 | 1491 | } |
316670eb | 1492 | |
39037602 A |
1493 | boolean_t s = ml_set_interrupts_enabled(FALSE); |
1494 | lck_spin_lock(&ut->uu_rethrottle_lock); | |
316670eb | 1495 | |
0a7de745 | 1496 | if (!ut->uu_is_throttled) { |
d9a64523 | 1497 | ut->uu_was_rethrottled = true; |
0a7de745 | 1498 | } else { |
39037602 | 1499 | int my_new_level = throttle_get_thread_throttle_level(ut); |
316670eb | 1500 | |
39236c6e | 1501 | if (my_new_level != ut->uu_on_throttlelist) { |
39037602 A |
1502 | /* |
1503 | * ut is currently blocked (as indicated by | |
d9a64523 | 1504 | * ut->uu_is_throttled == true) |
39037602 A |
1505 | * and we're changing it's throttle level, so |
1506 | * we need to wake it up. | |
1507 | */ | |
d9a64523 | 1508 | ut->uu_is_throttled = false; |
39037602 | 1509 | wakeup(&ut->uu_on_throttlelist); |
39236c6e | 1510 | |
39037602 A |
1511 | rethrottle_wakeups++; |
1512 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW, 102)), thread_tid(ut->uu_thread), ut->uu_on_throttlelist, my_new_level, 0, 0); | |
39236c6e A |
1513 | } |
1514 | } | |
39037602 A |
1515 | lck_spin_unlock(&ut->uu_rethrottle_lock); |
1516 | ml_set_interrupts_enabled(s); | |
316670eb A |
1517 | } |
1518 | ||
1519 | ||
b0d623f7 A |
1520 | /* |
1521 | * KPI routine | |
1522 | * | |
1523 | * Create and take a reference on a throttle info structure and return a | |
1524 | * pointer for the file system to use when calling throttle_info_update. | |
1525 | * Calling file system must have a matching release for every create. | |
1526 | */ | |
1527 | void * | |
1528 | throttle_info_create(void) | |
1529 | { | |
0a7de745 A |
1530 | struct _throttle_io_info_t *info; |
1531 | int level; | |
b0d623f7 A |
1532 | |
1533 | MALLOC(info, struct _throttle_io_info_t *, sizeof(*info), M_TEMP, M_ZERO | M_WAITOK); | |
1534 | /* Should never happen but just in case */ | |
0a7de745 | 1535 | if (info == NULL) { |
b0d623f7 | 1536 | return NULL; |
0a7de745 | 1537 | } |
b0d623f7 A |
1538 | /* Mark that this one was allocated and needs to be freed */ |
1539 | DEBUG_ALLOC_THROTTLE_INFO("Creating info = %p\n", info, info ); | |
316670eb A |
1540 | info->throttle_alloc = TRUE; |
1541 | ||
c3c9b80d | 1542 | lck_mtx_init(&info->throttle_lock, &throttle_lock_grp, LCK_ATTR_NULL); |
316670eb A |
1543 | info->throttle_timer_call = thread_call_allocate((thread_call_func_t)throttle_timer, (thread_call_param_t)info); |
1544 | ||
39236c6e A |
1545 | for (level = 0; level <= THROTTLE_LEVEL_END; level++) { |
1546 | TAILQ_INIT(&info->throttle_uthlist[level]); | |
1547 | } | |
1548 | info->throttle_next_wake_level = THROTTLE_LEVEL_END; | |
316670eb | 1549 | |
b0d623f7 | 1550 | /* Take a reference */ |
316670eb | 1551 | OSIncrementAtomic(&info->throttle_refcnt); |
b0d623f7 A |
1552 | return info; |
1553 | } | |
1554 | ||
1555 | /* | |
1556 | * KPI routine | |
1557 | * | |
0a7de745 A |
1558 | * Release the throttle info pointer if all the reference are gone. Should be |
1559 | * called to release reference taken by throttle_info_create | |
1560 | */ | |
b0d623f7 A |
1561 | void |
1562 | throttle_info_release(void *throttle_info) | |
1563 | { | |
1564 | DEBUG_ALLOC_THROTTLE_INFO("Releaseing info = %p\n", | |
0a7de745 A |
1565 | (struct _throttle_io_info_t *)throttle_info, |
1566 | (struct _throttle_io_info_t *)throttle_info); | |
1567 | if (throttle_info) { /* Just to be careful */ | |
b0d623f7 | 1568 | throttle_info_rel(throttle_info); |
0a7de745 | 1569 | } |
b0d623f7 A |
1570 | } |
1571 | ||
1572 | /* | |
1573 | * KPI routine | |
1574 | * | |
1575 | * File Systems that create an info structure, need to call this routine in | |
1576 | * their mount routine (used by cluster code). File Systems that call this in | |
1577 | * their mount routines must call throttle_info_mount_rel in their unmount | |
0a7de745 | 1578 | * routines. |
b0d623f7 | 1579 | */ |
0a7de745 | 1580 | void |
b0d623f7 A |
1581 | throttle_info_mount_ref(mount_t mp, void *throttle_info) |
1582 | { | |
0a7de745 | 1583 | if ((throttle_info == NULL) || (mp == NULL)) { |
b0d623f7 | 1584 | return; |
0a7de745 | 1585 | } |
b0d623f7 | 1586 | throttle_info_ref(throttle_info); |
316670eb A |
1587 | |
1588 | /* | |
1589 | * We already have a reference release it before adding the new one | |
1590 | */ | |
0a7de745 | 1591 | if (mp->mnt_throttle_info) { |
b0d623f7 | 1592 | throttle_info_rel(mp->mnt_throttle_info); |
0a7de745 | 1593 | } |
b0d623f7 A |
1594 | mp->mnt_throttle_info = throttle_info; |
1595 | } | |
1596 | ||
6d2010ae A |
1597 | /* |
1598 | * Private KPI routine | |
1599 | * | |
1600 | * return a handle for accessing throttle_info given a throttle_mask. The | |
1601 | * handle must be released by throttle_info_rel_by_mask | |
1602 | */ | |
1603 | int | |
316670eb | 1604 | throttle_info_ref_by_mask(uint64_t throttle_mask, throttle_info_handle_t *throttle_info_handle) |
6d2010ae | 1605 | { |
0a7de745 | 1606 | int dev_index; |
6d2010ae A |
1607 | struct _throttle_io_info_t *info; |
1608 | ||
f427ee49 A |
1609 | /* |
1610 | * The 'throttle_mask' is not expected to be 0 otherwise num_trailing_0() | |
1611 | * would return value of 64 and this will cause '_throttle_io_info' to | |
1612 | * go out of bounds as '_throttle_io_info' is only LOWPRI_MAX_NUM_DEV (64) | |
1613 | * elements long. | |
1614 | */ | |
1615 | if (throttle_info_handle == NULL || throttle_mask == 0) { | |
6d2010ae | 1616 | return EINVAL; |
0a7de745 A |
1617 | } |
1618 | ||
6d2010ae A |
1619 | dev_index = num_trailing_0(throttle_mask); |
1620 | info = &_throttle_io_info[dev_index]; | |
1621 | throttle_info_ref(info); | |
1622 | *(struct _throttle_io_info_t**)throttle_info_handle = info; | |
316670eb | 1623 | |
6d2010ae A |
1624 | return 0; |
1625 | } | |
1626 | ||
1627 | /* | |
1628 | * Private KPI routine | |
1629 | * | |
1630 | * release the handle obtained by throttle_info_ref_by_mask | |
1631 | */ | |
1632 | void | |
1633 | throttle_info_rel_by_mask(throttle_info_handle_t throttle_info_handle) | |
1634 | { | |
316670eb A |
1635 | /* |
1636 | * for now the handle is just a pointer to _throttle_io_info_t | |
1637 | */ | |
6d2010ae A |
1638 | throttle_info_rel((struct _throttle_io_info_t*)throttle_info_handle); |
1639 | } | |
1640 | ||
b0d623f7 A |
1641 | /* |
1642 | * KPI routine | |
1643 | * | |
1644 | * File Systems that throttle_info_mount_ref, must call this routine in their | |
1645 | * umount routine. | |
0a7de745 | 1646 | */ |
b0d623f7 A |
1647 | void |
1648 | throttle_info_mount_rel(mount_t mp) | |
1649 | { | |
0a7de745 | 1650 | if (mp->mnt_throttle_info) { |
b0d623f7 | 1651 | throttle_info_rel(mp->mnt_throttle_info); |
0a7de745 | 1652 | } |
b0d623f7 A |
1653 | mp->mnt_throttle_info = NULL; |
1654 | } | |
1655 | ||
5ba3f43e A |
1656 | /* |
1657 | * Reset throttling periods for the given mount point | |
1658 | * | |
1659 | * private interface used by disk conditioner to reset | |
1660 | * throttling periods when 'is_ssd' status changes | |
1661 | */ | |
1662 | void | |
1663 | throttle_info_mount_reset_period(mount_t mp, int isssd) | |
1664 | { | |
1665 | struct _throttle_io_info_t *info; | |
1666 | ||
0a7de745 | 1667 | if (mp == NULL) { |
5ba3f43e | 1668 | info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1]; |
0a7de745 | 1669 | } else if (mp->mnt_throttle_info == NULL) { |
5ba3f43e | 1670 | info = &_throttle_io_info[mp->mnt_devbsdunit]; |
0a7de745 | 1671 | } else { |
5ba3f43e | 1672 | info = mp->mnt_throttle_info; |
0a7de745 | 1673 | } |
5ba3f43e A |
1674 | |
1675 | throttle_init_throttle_period(info, isssd); | |
1676 | } | |
1677 | ||
e2fac8b1 A |
1678 | void |
1679 | throttle_info_get_last_io_time(mount_t mp, struct timeval *tv) | |
1680 | { | |
0a7de745 | 1681 | struct _throttle_io_info_t *info; |
e2fac8b1 | 1682 | |
0a7de745 | 1683 | if (mp == NULL) { |
316670eb | 1684 | info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1]; |
0a7de745 | 1685 | } else if (mp->mnt_throttle_info == NULL) { |
316670eb | 1686 | info = &_throttle_io_info[mp->mnt_devbsdunit]; |
0a7de745 | 1687 | } else { |
316670eb | 1688 | info = mp->mnt_throttle_info; |
0a7de745 | 1689 | } |
b0d623f7 | 1690 | |
316670eb | 1691 | *tv = info->throttle_last_write_timestamp; |
e2fac8b1 A |
1692 | } |
1693 | ||
1694 | void | |
1695 | update_last_io_time(mount_t mp) | |
1696 | { | |
0a7de745 A |
1697 | struct _throttle_io_info_t *info; |
1698 | ||
1699 | if (mp == NULL) { | |
316670eb | 1700 | info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1]; |
0a7de745 | 1701 | } else if (mp->mnt_throttle_info == NULL) { |
316670eb | 1702 | info = &_throttle_io_info[mp->mnt_devbsdunit]; |
0a7de745 | 1703 | } else { |
316670eb | 1704 | info = mp->mnt_throttle_info; |
0a7de745 | 1705 | } |
e2fac8b1 | 1706 | |
316670eb | 1707 | microuptime(&info->throttle_last_write_timestamp); |
0a7de745 | 1708 | if (mp != NULL) { |
39236c6e | 1709 | mp->mnt_last_write_completed_timestamp = info->throttle_last_write_timestamp; |
0a7de745 | 1710 | } |
e2fac8b1 A |
1711 | } |
1712 | ||
316670eb A |
1713 | int |
1714 | throttle_get_io_policy(uthread_t *ut) | |
6d2010ae | 1715 | { |
0a7de745 | 1716 | if (ut != NULL) { |
39236c6e | 1717 | *ut = get_bsdthread_info(current_thread()); |
0a7de745 | 1718 | } |
316670eb | 1719 | |
0a7de745 | 1720 | return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO); |
6d2010ae | 1721 | } |
6d2010ae | 1722 | |
39236c6e A |
1723 | int |
1724 | throttle_get_passive_io_policy(uthread_t *ut) | |
1725 | { | |
0a7de745 | 1726 | if (ut != NULL) { |
39236c6e | 1727 | *ut = get_bsdthread_info(current_thread()); |
0a7de745 | 1728 | } |
39236c6e | 1729 | |
0a7de745 | 1730 | return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_PASSIVE_IO); |
39236c6e | 1731 | } |
6d2010ae | 1732 | |
316670eb A |
1733 | |
1734 | static int | |
39236c6e A |
1735 | throttle_get_thread_throttle_level(uthread_t ut) |
1736 | { | |
d190cdc3 A |
1737 | uthread_t *ut_p = (ut == NULL) ? &ut : NULL; |
1738 | int io_tier = throttle_get_io_policy(ut_p); | |
316670eb | 1739 | |
d190cdc3 A |
1740 | return throttle_get_thread_throttle_level_internal(ut, io_tier); |
1741 | } | |
1742 | ||
1743 | /* | |
1744 | * Return a throttle level given an existing I/O tier (such as returned by throttle_get_io_policy) | |
1745 | */ | |
1746 | static int | |
0a7de745 A |
1747 | throttle_get_thread_throttle_level_internal(uthread_t ut, int io_tier) |
1748 | { | |
d190cdc3 A |
1749 | int thread_throttle_level = io_tier; |
1750 | int user_idle_level; | |
316670eb | 1751 | |
d190cdc3 | 1752 | assert(ut != NULL); |
316670eb | 1753 | |
39236c6e | 1754 | /* Bootcache misses should always be throttled */ |
0a7de745 | 1755 | if (ut->uu_throttle_bc) { |
39236c6e | 1756 | thread_throttle_level = THROTTLE_LEVEL_TIER3; |
0a7de745 | 1757 | } |
316670eb | 1758 | |
d190cdc3 A |
1759 | /* |
1760 | * Issue tier3 I/O as tier2 when the user is idle | |
1761 | * to allow maintenance tasks to make more progress. | |
1762 | * | |
1763 | * Assume any positive idle level is enough... for now it's | |
1764 | * only ever 0 or 128 but this is not defined anywhere. | |
1765 | */ | |
1766 | if (thread_throttle_level >= THROTTLE_LEVEL_TIER3) { | |
1767 | user_idle_level = timer_get_user_idle_level(); | |
1768 | if (user_idle_level > 0) { | |
1769 | thread_throttle_level--; | |
1770 | } | |
1771 | } | |
1772 | ||
0a7de745 | 1773 | return thread_throttle_level; |
6d2010ae | 1774 | } |
6d2010ae | 1775 | |
39037602 A |
1776 | /* |
1777 | * I/O will be throttled if either of the following are true: | |
1778 | * - Higher tiers have in-flight I/O | |
1779 | * - The time delta since the last start/completion of a higher tier is within the throttle window interval | |
1780 | * | |
1781 | * In-flight I/O is bookended by throttle_info_update_internal/throttle_info_end_io_internal | |
1782 | */ | |
b0d623f7 | 1783 | static int |
39236c6e | 1784 | throttle_io_will_be_throttled_internal(void * throttle_info, int * mylevel, int * throttling_level) |
2d21ac55 | 1785 | { |
0a7de745 | 1786 | struct _throttle_io_info_t *info = throttle_info; |
2d21ac55 | 1787 | struct timeval elapsed; |
39037602 | 1788 | struct timeval now; |
db609669 | 1789 | uint64_t elapsed_msecs; |
0a7de745 A |
1790 | int thread_throttle_level; |
1791 | int throttle_level; | |
6d2010ae | 1792 | |
0a7de745 A |
1793 | if ((thread_throttle_level = throttle_get_thread_throttle_level(NULL)) < THROTTLE_LEVEL_THROTTLED) { |
1794 | return THROTTLE_DISENGAGED; | |
1795 | } | |
2d21ac55 | 1796 | |
39037602 | 1797 | microuptime(&now); |
2d21ac55 | 1798 | |
39037602 A |
1799 | for (throttle_level = THROTTLE_LEVEL_START; throttle_level < thread_throttle_level; throttle_level++) { |
1800 | if (info->throttle_inflight_count[throttle_level]) { | |
1801 | break; | |
1802 | } | |
1803 | elapsed = now; | |
39236c6e | 1804 | timevalsub(&elapsed, &info->throttle_window_start_timestamp[throttle_level]); |
db609669 | 1805 | elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000); |
2d21ac55 | 1806 | |
0a7de745 | 1807 | if (elapsed_msecs < (uint64_t)throttle_windows_msecs[thread_throttle_level]) { |
316670eb | 1808 | break; |
0a7de745 | 1809 | } |
316670eb A |
1810 | } |
1811 | if (throttle_level >= thread_throttle_level) { | |
1812 | /* | |
1813 | * we're beyond all of the throttle windows | |
1814 | * that affect the throttle level of this thread, | |
1815 | * so go ahead and treat as normal I/O | |
1816 | */ | |
0a7de745 | 1817 | return THROTTLE_DISENGAGED; |
316670eb | 1818 | } |
0a7de745 | 1819 | if (mylevel) { |
39236c6e | 1820 | *mylevel = thread_throttle_level; |
0a7de745 A |
1821 | } |
1822 | if (throttling_level) { | |
39236c6e | 1823 | *throttling_level = throttle_level; |
0a7de745 | 1824 | } |
39236c6e | 1825 | |
316670eb A |
1826 | if (info->throttle_io_count != info->throttle_io_count_begin) { |
1827 | /* | |
1828 | * we've already issued at least one throttleable I/O | |
1829 | * in the current I/O window, so avoid issuing another one | |
1830 | */ | |
0a7de745 | 1831 | return THROTTLE_NOW; |
316670eb A |
1832 | } |
1833 | /* | |
1834 | * we're in the throttle window, so | |
1835 | * cut the I/O size back | |
1836 | */ | |
0a7de745 | 1837 | return THROTTLE_ENGAGED; |
593a1d5f | 1838 | } |
2d21ac55 | 1839 | |
0a7de745 | 1840 | /* |
b0d623f7 A |
1841 | * If we have a mount point and it has a throttle info pointer then |
1842 | * use it to do the check, otherwise use the device unit number to find | |
1843 | * the correct throttle info array element. | |
1844 | */ | |
1845 | int | |
316670eb | 1846 | throttle_io_will_be_throttled(__unused int lowpri_window_msecs, mount_t mp) |
b0d623f7 | 1847 | { |
0a7de745 | 1848 | struct _throttle_io_info_t *info; |
b0d623f7 | 1849 | |
316670eb A |
1850 | /* |
1851 | * Should we just return zero if no mount point | |
1852 | */ | |
0a7de745 A |
1853 | if (mp == NULL) { |
1854 | info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1]; | |
1855 | } else if (mp->mnt_throttle_info == NULL) { | |
1856 | info = &_throttle_io_info[mp->mnt_devbsdunit]; | |
1857 | } else { | |
1858 | info = mp->mnt_throttle_info; | |
1859 | } | |
316670eb | 1860 | |
3e170ce0 A |
1861 | if (info->throttle_is_fusion_with_priority) { |
1862 | uthread_t ut = get_bsdthread_info(current_thread()); | |
0a7de745 A |
1863 | if (ut->uu_lowpri_window == 0) { |
1864 | return THROTTLE_DISENGAGED; | |
1865 | } | |
3e170ce0 A |
1866 | } |
1867 | ||
0a7de745 A |
1868 | if (info->throttle_disabled) { |
1869 | return THROTTLE_DISENGAGED; | |
1870 | } else { | |
fe8ab488 | 1871 | return throttle_io_will_be_throttled_internal(info, NULL, NULL); |
0a7de745 | 1872 | } |
b0d623f7 A |
1873 | } |
1874 | ||
0a7de745 | 1875 | /* |
39236c6e A |
1876 | * Routine to increment I/O throttling counters maintained in the proc |
1877 | */ | |
1878 | ||
0a7de745 | 1879 | static void |
fe8ab488 | 1880 | throttle_update_proc_stats(pid_t throttling_pid, int count) |
39236c6e A |
1881 | { |
1882 | proc_t throttling_proc; | |
1883 | proc_t throttled_proc = current_proc(); | |
1884 | ||
1885 | /* The throttled_proc is always the current proc; so we are not concerned with refs */ | |
fe8ab488 | 1886 | OSAddAtomic64(count, &(throttled_proc->was_throttled)); |
0a7de745 | 1887 | |
39236c6e A |
1888 | /* The throttling pid might have exited by now */ |
1889 | throttling_proc = proc_find(throttling_pid); | |
1890 | if (throttling_proc != PROC_NULL) { | |
fe8ab488 | 1891 | OSAddAtomic64(count, &(throttling_proc->did_throttle)); |
39236c6e A |
1892 | proc_rele(throttling_proc); |
1893 | } | |
1894 | } | |
316670eb | 1895 | |
39236c6e A |
1896 | /* |
1897 | * Block until woken up by the throttle timer or by a rethrottle call. | |
1898 | * As long as we hold the throttle_lock while querying the throttle tier, we're | |
1899 | * safe against seeing an old throttle tier after a rethrottle. | |
1900 | */ | |
6d2010ae A |
1901 | uint32_t |
1902 | throttle_lowpri_io(int sleep_amount) | |
593a1d5f | 1903 | { |
316670eb | 1904 | uthread_t ut; |
b0d623f7 | 1905 | struct _throttle_io_info_t *info; |
0a7de745 A |
1906 | int throttle_type = 0; |
1907 | int mylevel = 0; | |
1908 | int throttling_level = THROTTLE_LEVEL_NONE; | |
1909 | int sleep_cnt = 0; | |
316670eb A |
1910 | uint32_t throttle_io_period_num = 0; |
1911 | boolean_t insert_tail = TRUE; | |
39037602 | 1912 | boolean_t s; |
2d21ac55 | 1913 | |
593a1d5f A |
1914 | ut = get_bsdthread_info(current_thread()); |
1915 | ||
0a7de745 A |
1916 | if (ut->uu_lowpri_window == 0) { |
1917 | return 0; | |
1918 | } | |
593a1d5f | 1919 | |
b0d623f7 | 1920 | info = ut->uu_throttle_info; |
593a1d5f | 1921 | |
39236c6e | 1922 | if (info == NULL) { |
d9a64523 | 1923 | ut->uu_throttle_bc = false; |
39236c6e | 1924 | ut->uu_lowpri_window = 0; |
0a7de745 | 1925 | return 0; |
39236c6e | 1926 | } |
39236c6e | 1927 | lck_mtx_lock(&info->throttle_lock); |
39037602 | 1928 | assert(ut->uu_on_throttlelist < THROTTLE_LEVEL_THROTTLED); |
39236c6e | 1929 | |
0a7de745 | 1930 | if (sleep_amount == 0) { |
316670eb | 1931 | goto done; |
0a7de745 | 1932 | } |
b0d623f7 | 1933 | |
0a7de745 | 1934 | if (sleep_amount == 1 && !ut->uu_throttle_bc) { |
316670eb | 1935 | sleep_amount = 0; |
0a7de745 | 1936 | } |
6d2010ae | 1937 | |
316670eb A |
1938 | throttle_io_period_num = info->throttle_io_period_num; |
1939 | ||
d9a64523 | 1940 | ut->uu_was_rethrottled = false; |
39037602 | 1941 | |
0a7de745 | 1942 | while ((throttle_type = throttle_io_will_be_throttled_internal(info, &mylevel, &throttling_level))) { |
39236c6e | 1943 | if (throttle_type == THROTTLE_ENGAGED) { |
0a7de745 | 1944 | if (sleep_amount == 0) { |
316670eb | 1945 | break; |
0a7de745 A |
1946 | } |
1947 | if (info->throttle_io_period_num < throttle_io_period_num) { | |
593a1d5f | 1948 | break; |
0a7de745 A |
1949 | } |
1950 | if ((info->throttle_io_period_num - throttle_io_period_num) >= (uint32_t)sleep_amount) { | |
1951 | break; | |
1952 | } | |
2d21ac55 | 1953 | } |
39037602 A |
1954 | /* |
1955 | * keep the same position in the list if "rethrottle_thread" changes our throttle level and | |
1956 | * then puts us back to the original level before we get a chance to run | |
1957 | */ | |
1958 | if (ut->uu_on_throttlelist >= THROTTLE_LEVEL_THROTTLED && ut->uu_on_throttlelist != mylevel) { | |
1959 | /* | |
1960 | * must have been awakened via "rethrottle_thread" (the timer pulls us off the list) | |
1961 | * and we've changed our throttling level, so pull ourselves off of the appropriate list | |
1962 | * and make sure we get put on the tail of the new list since we're starting anew w/r to | |
1963 | * the throttling engine | |
1964 | */ | |
1965 | TAILQ_REMOVE(&info->throttle_uthlist[ut->uu_on_throttlelist], ut, uu_throttlelist); | |
1966 | ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE; | |
1967 | insert_tail = TRUE; | |
1968 | } | |
39236c6e | 1969 | if (ut->uu_on_throttlelist < THROTTLE_LEVEL_THROTTLED) { |
0a7de745 | 1970 | if (throttle_add_to_list(info, ut, mylevel, insert_tail) == THROTTLE_LEVEL_END) { |
316670eb | 1971 | goto done; |
0a7de745 | 1972 | } |
316670eb | 1973 | } |
39236c6e | 1974 | assert(throttling_level >= THROTTLE_LEVEL_START && throttling_level <= THROTTLE_LEVEL_END); |
39037602 A |
1975 | |
1976 | s = ml_set_interrupts_enabled(FALSE); | |
1977 | lck_spin_lock(&ut->uu_rethrottle_lock); | |
1978 | ||
1979 | /* | |
1980 | * this is the critical section w/r to our interaction | |
1981 | * with "rethrottle_thread" | |
1982 | */ | |
d9a64523 | 1983 | if (ut->uu_was_rethrottled) { |
39037602 A |
1984 | lck_spin_unlock(&ut->uu_rethrottle_lock); |
1985 | ml_set_interrupts_enabled(s); | |
1986 | lck_mtx_yield(&info->throttle_lock); | |
1987 | ||
1988 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW, 103)), thread_tid(ut->uu_thread), ut->uu_on_throttlelist, 0, 0, 0); | |
1989 | ||
d9a64523 | 1990 | ut->uu_was_rethrottled = false; |
39037602 A |
1991 | continue; |
1992 | } | |
39236c6e | 1993 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE, PROCESS_THROTTLED)) | DBG_FUNC_NONE, |
0a7de745 A |
1994 | info->throttle_last_IO_pid[throttling_level], throttling_level, proc_selfpid(), mylevel, 0); |
1995 | ||
316670eb A |
1996 | if (sleep_cnt == 0) { |
1997 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_START, | |
0a7de745 | 1998 | throttle_windows_msecs[mylevel], info->throttle_io_periods[mylevel], info->throttle_io_count, 0, 0); |
39236c6e | 1999 | throttled_count[mylevel]++; |
316670eb | 2000 | } |
39037602 A |
2001 | ut->uu_wmesg = "throttle_lowpri_io"; |
2002 | ||
2003 | assert_wait((caddr_t)&ut->uu_on_throttlelist, THREAD_UNINT); | |
2004 | ||
d9a64523 | 2005 | ut->uu_is_throttled = true; |
39037602 A |
2006 | lck_spin_unlock(&ut->uu_rethrottle_lock); |
2007 | ml_set_interrupts_enabled(s); | |
2008 | ||
2009 | lck_mtx_unlock(&info->throttle_lock); | |
2010 | ||
2011 | thread_block(THREAD_CONTINUE_NULL); | |
2012 | ||
2013 | ut->uu_wmesg = NULL; | |
2014 | ||
d9a64523 A |
2015 | ut->uu_is_throttled = false; |
2016 | ut->uu_was_rethrottled = false; | |
39037602 A |
2017 | |
2018 | lck_mtx_lock(&info->throttle_lock); | |
593a1d5f | 2019 | |
316670eb | 2020 | sleep_cnt++; |
0a7de745 A |
2021 | |
2022 | if (sleep_amount == 0) { | |
316670eb | 2023 | insert_tail = FALSE; |
0a7de745 A |
2024 | } else if (info->throttle_io_period_num < throttle_io_period_num || |
2025 | (info->throttle_io_period_num - throttle_io_period_num) >= (uint32_t)sleep_amount) { | |
316670eb A |
2026 | insert_tail = FALSE; |
2027 | sleep_amount = 0; | |
2028 | } | |
593a1d5f | 2029 | } |
b0d623f7 | 2030 | done: |
39236c6e A |
2031 | if (ut->uu_on_throttlelist >= THROTTLE_LEVEL_THROTTLED) { |
2032 | TAILQ_REMOVE(&info->throttle_uthlist[ut->uu_on_throttlelist], ut, uu_throttlelist); | |
2033 | ut->uu_on_throttlelist = THROTTLE_LEVEL_NONE; | |
316670eb | 2034 | } |
39236c6e A |
2035 | lck_mtx_unlock(&info->throttle_lock); |
2036 | ||
2037 | if (sleep_cnt) { | |
316670eb | 2038 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_END, |
0a7de745 | 2039 | throttle_windows_msecs[mylevel], info->throttle_io_periods[mylevel], info->throttle_io_count, 0, 0); |
fe8ab488 A |
2040 | /* |
2041 | * We update the stats for the last pid which opened a throttle window for the throttled thread. | |
2042 | * This might not be completely accurate since the multiple throttles seen by the lower tier pid | |
0a7de745 | 2043 | * might have been caused by various higher prio pids. However, updating these stats accurately |
fe8ab488 A |
2044 | * means doing a proc_find while holding the throttle lock which leads to deadlock. |
2045 | */ | |
2046 | throttle_update_proc_stats(info->throttle_last_IO_pid[throttling_level], sleep_cnt); | |
39236c6e A |
2047 | } |
2048 | ||
b0d623f7 | 2049 | ut->uu_throttle_info = NULL; |
d9a64523 | 2050 | ut->uu_throttle_bc = false; |
316670eb | 2051 | ut->uu_lowpri_window = 0; |
6d2010ae | 2052 | |
39037602 A |
2053 | throttle_info_rel(info); |
2054 | ||
0a7de745 | 2055 | return sleep_cnt; |
593a1d5f A |
2056 | } |
2057 | ||
cb323159 A |
2058 | /* |
2059 | * returns TRUE if the throttle_lowpri_io called with the same sleep_amount would've slept | |
2060 | * This function mimics the most of the throttle_lowpri_io checks but without actual sleeping | |
2061 | */ | |
2062 | int | |
2063 | throttle_lowpri_io_will_be_throttled(int sleep_amount) | |
2064 | { | |
2065 | if (sleep_amount == 0) { | |
2066 | return FALSE; | |
2067 | } | |
2068 | ||
2069 | uthread_t ut = get_bsdthread_info(current_thread()); | |
2070 | if (ut->uu_lowpri_window == 0) { | |
2071 | return FALSE; | |
2072 | } | |
2073 | ||
2074 | struct _throttle_io_info_t *info = ut->uu_throttle_info; | |
2075 | if (info == NULL) { | |
2076 | return FALSE; | |
2077 | } | |
2078 | ||
2079 | lck_mtx_lock(&info->throttle_lock); | |
2080 | assert(ut->uu_on_throttlelist < THROTTLE_LEVEL_THROTTLED); | |
2081 | ||
2082 | if (sleep_amount == 1 && !ut->uu_throttle_bc) { | |
2083 | sleep_amount = 0; | |
2084 | } | |
2085 | ||
2086 | int result = FALSE; | |
2087 | ||
2088 | int throttle_type = throttle_io_will_be_throttled_internal(info, NULL, NULL); | |
2089 | if (throttle_type > THROTTLE_DISENGAGED) { | |
2090 | result = TRUE; | |
2091 | if ((throttle_type == THROTTLE_ENGAGED) && (sleep_amount == 0)) { | |
2092 | result = FALSE; | |
2093 | } | |
2094 | } | |
2095 | ||
2096 | lck_mtx_unlock(&info->throttle_lock); | |
2097 | ||
2098 | return result; | |
2099 | } | |
2100 | ||
2101 | ||
6d2010ae A |
2102 | /* |
2103 | * KPI routine | |
2104 | * | |
2105 | * set a kernel thread's IO policy. policy can be: | |
39236c6e | 2106 | * IOPOL_NORMAL, IOPOL_THROTTLE, IOPOL_PASSIVE, IOPOL_UTILITY, IOPOL_STANDARD |
6d2010ae A |
2107 | * |
2108 | * explanations about these policies are in the man page of setiopolicy_np | |
2109 | */ | |
0a7de745 A |
2110 | void |
2111 | throttle_set_thread_io_policy(int policy) | |
593a1d5f | 2112 | { |
39037602 | 2113 | proc_set_thread_policy(current_thread(), TASK_POLICY_INTERNAL, TASK_POLICY_IOPOL, policy); |
6d2010ae | 2114 | } |
593a1d5f | 2115 | |
0a7de745 A |
2116 | int |
2117 | throttle_get_thread_effective_io_policy() | |
a39ff7e2 A |
2118 | { |
2119 | return proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO); | |
2120 | } | |
2121 | ||
c3c9b80d A |
2122 | int |
2123 | throttle_thread_io_tier_above_metadata(void) | |
2124 | { | |
2125 | return throttle_get_thread_effective_io_policy() < IOSCHED_METADATA_TIER; | |
2126 | } | |
2127 | ||
0a7de745 A |
2128 | void |
2129 | throttle_info_reset_window(uthread_t ut) | |
6d2010ae A |
2130 | { |
2131 | struct _throttle_io_info_t *info; | |
2132 | ||
0a7de745 | 2133 | if (ut == NULL) { |
3e170ce0 | 2134 | ut = get_bsdthread_info(current_thread()); |
0a7de745 | 2135 | } |
3e170ce0 | 2136 | |
0a7de745 | 2137 | if ((info = ut->uu_throttle_info)) { |
316670eb | 2138 | throttle_info_rel(info); |
6d2010ae | 2139 | |
316670eb A |
2140 | ut->uu_throttle_info = NULL; |
2141 | ut->uu_lowpri_window = 0; | |
d9a64523 | 2142 | ut->uu_throttle_bc = false; |
316670eb | 2143 | } |
6d2010ae A |
2144 | } |
2145 | ||
2146 | static | |
0a7de745 A |
2147 | void |
2148 | throttle_info_set_initial_window(uthread_t ut, struct _throttle_io_info_t *info, boolean_t BC_throttle, boolean_t isssd) | |
6d2010ae | 2149 | { |
0a7de745 | 2150 | if (lowpri_throttle_enabled == 0 || info->throttle_disabled) { |
39236c6e | 2151 | return; |
0a7de745 | 2152 | } |
39236c6e A |
2153 | |
2154 | if (info->throttle_io_periods == 0) { | |
2155 | throttle_init_throttle_period(info, isssd); | |
2156 | } | |
316670eb | 2157 | if (ut->uu_throttle_info == NULL) { |
316670eb A |
2158 | ut->uu_throttle_info = info; |
2159 | throttle_info_ref(info); | |
2160 | DEBUG_ALLOC_THROTTLE_INFO("updating info = %p\n", info, info ); | |
6d2010ae | 2161 | |
39236c6e | 2162 | ut->uu_lowpri_window = 1; |
316670eb | 2163 | ut->uu_throttle_bc = BC_throttle; |
593a1d5f | 2164 | } |
2d21ac55 | 2165 | } |
91447636 | 2166 | |
39037602 A |
2167 | /* |
2168 | * Update inflight IO count and throttling window | |
2169 | * Should be called when an IO is done | |
2170 | * | |
2171 | * Only affects IO that was sent through spec_strategy | |
2172 | */ | |
0a7de745 A |
2173 | void |
2174 | throttle_info_end_io(buf_t bp) | |
2175 | { | |
39037602 A |
2176 | mount_t mp; |
2177 | struct bufattr *bap; | |
2178 | struct _throttle_io_info_t *info; | |
d190cdc3 | 2179 | int io_tier; |
39037602 A |
2180 | |
2181 | bap = &bp->b_attr; | |
2182 | if (!ISSET(bap->ba_flags, BA_STRATEGY_TRACKED_IO)) { | |
2183 | return; | |
2184 | } | |
2185 | CLR(bap->ba_flags, BA_STRATEGY_TRACKED_IO); | |
2186 | ||
2187 | mp = buf_vnode(bp)->v_mount; | |
2188 | if (mp != NULL) { | |
2189 | info = &_throttle_io_info[mp->mnt_devbsdunit]; | |
2190 | } else { | |
2191 | info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1]; | |
2192 | } | |
2193 | ||
d190cdc3 A |
2194 | io_tier = GET_BUFATTR_IO_TIER(bap); |
2195 | if (ISSET(bap->ba_flags, BA_IO_TIER_UPGRADE)) { | |
2196 | io_tier--; | |
2197 | } | |
2198 | ||
2199 | throttle_info_end_io_internal(info, io_tier); | |
39037602 A |
2200 | } |
2201 | ||
2202 | /* | |
2203 | * Decrement inflight count initially incremented by throttle_info_update_internal | |
2204 | */ | |
2205 | static | |
0a7de745 A |
2206 | void |
2207 | throttle_info_end_io_internal(struct _throttle_io_info_t *info, int throttle_level) | |
2208 | { | |
39037602 A |
2209 | if (throttle_level == THROTTLE_LEVEL_NONE) { |
2210 | return; | |
2211 | } | |
6d2010ae | 2212 | |
39037602 A |
2213 | microuptime(&info->throttle_window_start_timestamp[throttle_level]); |
2214 | OSDecrementAtomic(&info->throttle_inflight_count[throttle_level]); | |
2215 | assert(info->throttle_inflight_count[throttle_level] >= 0); | |
2216 | } | |
2217 | ||
2218 | /* | |
2219 | * If inflight is TRUE and bap is NULL then the caller is responsible for calling | |
2220 | * throttle_info_end_io_internal to avoid leaking in-flight I/O. | |
2221 | */ | |
6d2010ae | 2222 | static |
0a7de745 A |
2223 | int |
2224 | throttle_info_update_internal(struct _throttle_io_info_t *info, uthread_t ut, int flags, boolean_t isssd, boolean_t inflight, struct bufattr *bap) | |
b0d623f7 | 2225 | { |
0a7de745 | 2226 | int thread_throttle_level; |
b0d623f7 | 2227 | |
0a7de745 | 2228 | if (lowpri_throttle_enabled == 0 || info->throttle_disabled) { |
39037602 | 2229 | return THROTTLE_LEVEL_NONE; |
0a7de745 | 2230 | } |
b0d623f7 | 2231 | |
0a7de745 | 2232 | if (ut == NULL) { |
316670eb | 2233 | ut = get_bsdthread_info(current_thread()); |
0a7de745 | 2234 | } |
b0d623f7 | 2235 | |
39037602 A |
2236 | if (bap && inflight && !ut->uu_throttle_bc) { |
2237 | thread_throttle_level = GET_BUFATTR_IO_TIER(bap); | |
d190cdc3 A |
2238 | if (ISSET(bap->ba_flags, BA_IO_TIER_UPGRADE)) { |
2239 | thread_throttle_level--; | |
2240 | } | |
39037602 A |
2241 | } else { |
2242 | thread_throttle_level = throttle_get_thread_throttle_level(ut); | |
2243 | } | |
316670eb | 2244 | |
39236c6e | 2245 | if (thread_throttle_level != THROTTLE_LEVEL_NONE) { |
0a7de745 | 2246 | if (!ISSET(flags, B_PASSIVE)) { |
39236c6e | 2247 | info->throttle_last_IO_pid[thread_throttle_level] = proc_selfpid(); |
39037602 A |
2248 | if (inflight && !ut->uu_throttle_bc) { |
2249 | if (NULL != bap) { | |
2250 | SET(bap->ba_flags, BA_STRATEGY_TRACKED_IO); | |
2251 | } | |
2252 | OSIncrementAtomic(&info->throttle_inflight_count[thread_throttle_level]); | |
2253 | } else { | |
2254 | microuptime(&info->throttle_window_start_timestamp[thread_throttle_level]); | |
2255 | } | |
39236c6e | 2256 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE, OPEN_THROTTLE_WINDOW)) | DBG_FUNC_NONE, |
0a7de745 | 2257 | current_proc()->p_pid, thread_throttle_level, 0, 0, 0); |
39236c6e | 2258 | } |
316670eb | 2259 | microuptime(&info->throttle_last_IO_timestamp[thread_throttle_level]); |
39236c6e A |
2260 | } |
2261 | ||
316670eb A |
2262 | |
2263 | if (thread_throttle_level >= THROTTLE_LEVEL_THROTTLED) { | |
b0d623f7 A |
2264 | /* |
2265 | * I'd really like to do the IOSleep here, but | |
2266 | * we may be holding all kinds of filesystem related locks | |
2267 | * and the pages for this I/O marked 'busy'... | |
2268 | * we don't want to cause a normal task to block on | |
2269 | * one of these locks while we're throttling a task marked | |
2270 | * for low priority I/O... we'll mark the uthread and | |
2271 | * do the delay just before we return from the system | |
2272 | * call that triggered this I/O or from vnode_pagein | |
2273 | */ | |
0a7de745 | 2274 | OSAddAtomic(1, &info->throttle_io_count); |
316670eb | 2275 | |
39236c6e | 2276 | throttle_info_set_initial_window(ut, info, FALSE, isssd); |
316670eb | 2277 | } |
39037602 A |
2278 | |
2279 | return thread_throttle_level; | |
316670eb A |
2280 | } |
2281 | ||
0a7de745 A |
2282 | void * |
2283 | throttle_info_update_by_mount(mount_t mp) | |
316670eb A |
2284 | { |
2285 | struct _throttle_io_info_t *info; | |
2286 | uthread_t ut; | |
2287 | boolean_t isssd = FALSE; | |
2288 | ||
2289 | ut = get_bsdthread_info(current_thread()); | |
2290 | ||
316670eb | 2291 | if (mp != NULL) { |
0a7de745 | 2292 | if (disk_conditioner_mount_is_ssd(mp)) { |
316670eb | 2293 | isssd = TRUE; |
0a7de745 | 2294 | } |
316670eb | 2295 | info = &_throttle_io_info[mp->mnt_devbsdunit]; |
0a7de745 | 2296 | } else { |
316670eb | 2297 | info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1]; |
0a7de745 | 2298 | } |
316670eb | 2299 | |
0a7de745 | 2300 | if (!ut->uu_lowpri_window) { |
39236c6e | 2301 | throttle_info_set_initial_window(ut, info, FALSE, isssd); |
0a7de745 | 2302 | } |
316670eb | 2303 | |
39236c6e | 2304 | return info; |
b0d623f7 A |
2305 | } |
2306 | ||
316670eb | 2307 | |
6d2010ae A |
2308 | /* |
2309 | * KPI routine | |
2310 | * | |
2311 | * this is usually called before every I/O, used for throttled I/O | |
2312 | * book keeping. This routine has low overhead and does not sleep | |
2313 | */ | |
0a7de745 A |
2314 | void |
2315 | throttle_info_update(void *throttle_info, int flags) | |
6d2010ae | 2316 | { |
0a7de745 | 2317 | if (throttle_info) { |
39037602 | 2318 | throttle_info_update_internal(throttle_info, NULL, flags, FALSE, FALSE, NULL); |
0a7de745 | 2319 | } |
6d2010ae A |
2320 | } |
2321 | ||
2322 | /* | |
2323 | * KPI routine | |
2324 | * | |
2325 | * this is usually called before every I/O, used for throttled I/O | |
2326 | * book keeping. This routine has low overhead and does not sleep | |
2327 | */ | |
0a7de745 A |
2328 | void |
2329 | throttle_info_update_by_mask(void *throttle_info_handle, int flags) | |
6d2010ae A |
2330 | { |
2331 | void *throttle_info = throttle_info_handle; | |
316670eb A |
2332 | |
2333 | /* | |
2334 | * for now we only use the lowest bit of the throttle mask, so the | |
6d2010ae A |
2335 | * handle is the same as the throttle_info. Later if we store a |
2336 | * set of throttle infos in the handle, we will want to loop through | |
2337 | * them and call throttle_info_update in a loop | |
2338 | */ | |
2339 | throttle_info_update(throttle_info, flags); | |
2340 | } | |
fe8ab488 A |
2341 | /* |
2342 | * KPI routine | |
0a7de745 A |
2343 | * |
2344 | * This routine marks the throttle info as disabled. Used for mount points which | |
fe8ab488 A |
2345 | * support I/O scheduling. |
2346 | */ | |
2347 | ||
0a7de745 A |
2348 | void |
2349 | throttle_info_disable_throttle(int devno, boolean_t isfusion) | |
fe8ab488 A |
2350 | { |
2351 | struct _throttle_io_info_t *info; | |
2352 | ||
0a7de745 | 2353 | if (devno < 0 || devno >= LOWPRI_MAX_NUM_DEV) { |
fe8ab488 | 2354 | panic("Illegal devno (%d) passed into throttle_info_disable_throttle()", devno); |
0a7de745 | 2355 | } |
fe8ab488 A |
2356 | |
2357 | info = &_throttle_io_info[devno]; | |
3e170ce0 A |
2358 | // don't disable software throttling on devices that are part of a fusion device |
2359 | // and override the software throttle periods to use HDD periods | |
2360 | if (isfusion) { | |
2361 | info->throttle_is_fusion_with_priority = isfusion; | |
2362 | throttle_init_throttle_period(info, FALSE); | |
2363 | } | |
2364 | info->throttle_disabled = !info->throttle_is_fusion_with_priority; | |
fe8ab488 | 2365 | return; |
0a7de745 | 2366 | } |
fe8ab488 | 2367 | |
6d2010ae | 2368 | |
39236c6e A |
2369 | /* |
2370 | * KPI routine (private) | |
2371 | * Called to determine if this IO is being throttled to this level so that it can be treated specially | |
2372 | */ | |
0a7de745 A |
2373 | int |
2374 | throttle_info_io_will_be_throttled(void * throttle_info, int policy) | |
316670eb | 2375 | { |
0a7de745 | 2376 | struct _throttle_io_info_t *info = throttle_info; |
316670eb | 2377 | struct timeval elapsed; |
db609669 | 2378 | uint64_t elapsed_msecs; |
0a7de745 A |
2379 | int throttle_level; |
2380 | int thread_throttle_level; | |
2381 | ||
2382 | switch (policy) { | |
2383 | case IOPOL_THROTTLE: | |
2384 | thread_throttle_level = THROTTLE_LEVEL_TIER3; | |
2385 | break; | |
2386 | case IOPOL_UTILITY: | |
2387 | thread_throttle_level = THROTTLE_LEVEL_TIER2; | |
2388 | break; | |
2389 | case IOPOL_STANDARD: | |
2390 | thread_throttle_level = THROTTLE_LEVEL_TIER1; | |
2391 | break; | |
2392 | default: | |
2393 | thread_throttle_level = THROTTLE_LEVEL_TIER0; | |
316670eb A |
2394 | break; |
2395 | } | |
2396 | for (throttle_level = THROTTLE_LEVEL_START; throttle_level < thread_throttle_level; throttle_level++) { | |
39037602 A |
2397 | if (info->throttle_inflight_count[throttle_level]) { |
2398 | break; | |
2399 | } | |
316670eb A |
2400 | |
2401 | microuptime(&elapsed); | |
39236c6e | 2402 | timevalsub(&elapsed, &info->throttle_window_start_timestamp[throttle_level]); |
db609669 | 2403 | elapsed_msecs = (uint64_t)elapsed.tv_sec * (uint64_t)1000 + (elapsed.tv_usec / 1000); |
316670eb | 2404 | |
0a7de745 | 2405 | if (elapsed_msecs < (uint64_t)throttle_windows_msecs[thread_throttle_level]) { |
316670eb | 2406 | break; |
0a7de745 | 2407 | } |
316670eb A |
2408 | } |
2409 | if (throttle_level >= thread_throttle_level) { | |
2410 | /* | |
2411 | * we're beyond all of the throttle windows | |
2412 | * so go ahead and treat as normal I/O | |
2413 | */ | |
0a7de745 | 2414 | return THROTTLE_DISENGAGED; |
316670eb A |
2415 | } |
2416 | /* | |
2417 | * we're in the throttle window | |
2418 | */ | |
0a7de745 | 2419 | return THROTTLE_ENGAGED; |
316670eb A |
2420 | } |
2421 | ||
0a7de745 A |
2422 | int |
2423 | throttle_lowpri_window(void) | |
39037602 A |
2424 | { |
2425 | struct uthread *ut = get_bsdthread_info(current_thread()); | |
2426 | return ut->uu_lowpri_window; | |
2427 | } | |
2428 | ||
5ba3f43e A |
2429 | |
2430 | #if CONFIG_IOSCHED | |
2431 | int upl_get_cached_tier(void *); | |
2432 | #endif | |
2433 | ||
f427ee49 A |
2434 | #if CONFIG_PHYS_WRITE_ACCT |
2435 | extern thread_t pm_sync_thread; | |
2436 | #endif /* CONFIG_PHYS_WRITE_ACCT */ | |
2437 | ||
91447636 | 2438 | int |
2d21ac55 | 2439 | spec_strategy(struct vnop_strategy_args *ap) |
1c79356b | 2440 | { |
0a7de745 A |
2441 | buf_t bp; |
2442 | int bflags; | |
2443 | int io_tier; | |
2444 | int passive; | |
2445 | dev_t bdev; | |
b0d623f7 | 2446 | uthread_t ut; |
b0d623f7 | 2447 | mount_t mp; |
0a7de745 A |
2448 | struct bufattr *bap; |
2449 | int strategy_ret; | |
6d2010ae A |
2450 | struct _throttle_io_info_t *throttle_info; |
2451 | boolean_t isssd = FALSE; | |
39037602 | 2452 | boolean_t inflight = FALSE; |
d190cdc3 | 2453 | boolean_t upgrade = FALSE; |
fe8ab488 A |
2454 | int code = 0; |
2455 | ||
f427ee49 | 2456 | #if CONFIG_DELAY_IDLE_SLEEP |
316670eb | 2457 | proc_t curproc = current_proc(); |
f427ee49 | 2458 | #endif /* CONFIG_DELAY_IDLE_SLEEP */ |
9bccf70c | 2459 | |
0a7de745 | 2460 | bp = ap->a_bp; |
91447636 | 2461 | bdev = buf_device(bp); |
b0d623f7 | 2462 | mp = buf_vnode(bp)->v_mount; |
39236c6e | 2463 | bap = &bp->b_attr; |
9bccf70c | 2464 | |
f427ee49 A |
2465 | #if CONFIG_PHYS_WRITE_ACCT |
2466 | if (current_thread() == pm_sync_thread) { | |
2467 | OSAddAtomic64(buf_count(bp), (SInt64 *)&(kernel_pm_writes)); | |
2468 | } | |
2469 | #endif /* CONFIG_PHYS_WRITE_ACCT */ | |
2470 | ||
5ba3f43e | 2471 | #if CONFIG_IOSCHED |
0a7de745 A |
2472 | if (bp->b_flags & B_CLUSTER) { |
2473 | io_tier = upl_get_cached_tier(bp->b_upl); | |
5ba3f43e | 2474 | |
0a7de745 A |
2475 | if (io_tier == -1) { |
2476 | io_tier = throttle_get_io_policy(&ut); | |
2477 | } | |
5ba3f43e | 2478 | #if DEVELOPMENT || DEBUG |
0a7de745 A |
2479 | else { |
2480 | int my_io_tier = throttle_get_io_policy(&ut); | |
5ba3f43e | 2481 | |
0a7de745 A |
2482 | if (io_tier != my_io_tier) { |
2483 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_THROTTLE, IO_TIER_UPL_MISMATCH)) | DBG_FUNC_NONE, buf_kernel_addrperm_addr(bp), my_io_tier, io_tier, 0, 0); | |
2484 | } | |
2485 | } | |
5ba3f43e | 2486 | #endif |
0a7de745 A |
2487 | } else { |
2488 | io_tier = throttle_get_io_policy(&ut); | |
2489 | } | |
5ba3f43e | 2490 | #else |
39236c6e | 2491 | io_tier = throttle_get_io_policy(&ut); |
5ba3f43e | 2492 | #endif |
39236c6e | 2493 | passive = throttle_get_passive_io_policy(&ut); |
6d2010ae | 2494 | |
d190cdc3 A |
2495 | /* |
2496 | * Mark if the I/O was upgraded by throttle_get_thread_throttle_level | |
2497 | * while preserving the original issued tier (throttle_get_io_policy | |
2498 | * does not return upgraded tiers) | |
2499 | */ | |
2500 | if (mp && io_tier > throttle_get_thread_throttle_level_internal(ut, io_tier)) { | |
2501 | #if CONFIG_IOSCHED | |
2502 | if (!(mp->mnt_ioflags & MNT_IOFLAGS_IOSCHED_SUPPORTED)) { | |
2503 | upgrade = TRUE; | |
2504 | } | |
2505 | #else /* CONFIG_IOSCHED */ | |
2506 | upgrade = TRUE; | |
2507 | #endif /* CONFIG_IOSCHED */ | |
2508 | } | |
2509 | ||
0a7de745 | 2510 | if (bp->b_flags & B_META) { |
39236c6e | 2511 | bap->ba_flags |= BA_META; |
0a7de745 | 2512 | } |
316670eb | 2513 | |
fe8ab488 | 2514 | #if CONFIG_IOSCHED |
0a7de745 | 2515 | /* |
c3c9b80d A |
2516 | * For metadata reads, ceil the I/O tier to IOSCHED_METADATA_EXPEDITED_TIER if they are expedited, otherwise |
2517 | * ceil it to IOSCHED_METADATA_TIER. Mark them passive if the I/O tier was upgraded. | |
2518 | * For metadata writes, set the I/O tier to IOSCHED_METADATA_EXPEDITED_TIER if they are expedited. Otherwise | |
2519 | * set it to IOSCHED_METADATA_TIER. In addition, mark them as passive. | |
fe8ab488 A |
2520 | */ |
2521 | if (bap->ba_flags & BA_META) { | |
cb323159 | 2522 | if ((mp && (mp->mnt_ioflags & MNT_IOFLAGS_IOSCHED_SUPPORTED)) || (bap->ba_flags & BA_IO_SCHEDULED)) { |
fe8ab488 | 2523 | if (bp->b_flags & B_READ) { |
c3c9b80d A |
2524 | if ((bap->ba_flags & BA_EXPEDITED_META_IO) && (io_tier > IOSCHED_METADATA_EXPEDITED_TIER)) { |
2525 | io_tier = IOSCHED_METADATA_EXPEDITED_TIER; | |
2526 | passive = 1; | |
2527 | } else if (io_tier > IOSCHED_METADATA_TIER) { | |
fe8ab488 A |
2528 | io_tier = IOSCHED_METADATA_TIER; |
2529 | passive = 1; | |
2530 | } | |
2531 | } else { | |
c3c9b80d A |
2532 | if (bap->ba_flags & BA_EXPEDITED_META_IO) { |
2533 | io_tier = IOSCHED_METADATA_EXPEDITED_TIER; | |
2534 | } else { | |
2535 | io_tier = IOSCHED_METADATA_TIER; | |
2536 | } | |
fe8ab488 A |
2537 | passive = 1; |
2538 | } | |
2539 | } | |
2540 | } | |
2541 | #endif /* CONFIG_IOSCHED */ | |
0a7de745 | 2542 | |
39236c6e A |
2543 | SET_BUFATTR_IO_TIER(bap, io_tier); |
2544 | ||
fe8ab488 | 2545 | if (passive) { |
6d2010ae | 2546 | bp->b_flags |= B_PASSIVE; |
fe8ab488 A |
2547 | bap->ba_flags |= BA_PASSIVE; |
2548 | } | |
6d2010ae | 2549 | |
f427ee49 | 2550 | #if CONFIG_DELAY_IDLE_SLEEP |
0a7de745 | 2551 | if ((curproc != NULL) && ((curproc->p_flag & P_DELAYIDLESLEEP) == P_DELAYIDLESLEEP)) { |
39236c6e | 2552 | bap->ba_flags |= BA_DELAYIDLESLEEP; |
0a7de745 | 2553 | } |
f427ee49 | 2554 | #endif /* CONFIG_DELAY_IDLE_SLEEP */ |
0a7de745 | 2555 | |
6d2010ae A |
2556 | bflags = bp->b_flags; |
2557 | ||
0a7de745 | 2558 | if (((bflags & B_READ) == 0) && ((bflags & B_ASYNC) == 0)) { |
39236c6e | 2559 | bufattr_markquickcomplete(bap); |
0a7de745 | 2560 | } |
39236c6e | 2561 | |
0a7de745 A |
2562 | if (bflags & B_READ) { |
2563 | code |= DKIO_READ; | |
2564 | } | |
2565 | if (bflags & B_ASYNC) { | |
2566 | code |= DKIO_ASYNC; | |
2567 | } | |
39037602 | 2568 | |
0a7de745 A |
2569 | if (bap->ba_flags & BA_META) { |
2570 | code |= DKIO_META; | |
2571 | } else if (bflags & B_PAGEIO) { | |
2572 | code |= DKIO_PAGING; | |
2573 | } | |
9bccf70c | 2574 | |
0a7de745 | 2575 | if (io_tier != 0) { |
fe8ab488 | 2576 | code |= DKIO_THROTTLE; |
0a7de745 | 2577 | } |
9bccf70c | 2578 | |
fe8ab488 | 2579 | code |= ((io_tier << DKIO_TIER_SHIFT) & DKIO_TIER_MASK); |
39236c6e | 2580 | |
0a7de745 | 2581 | if (bflags & B_PASSIVE) { |
fe8ab488 | 2582 | code |= DKIO_PASSIVE; |
0a7de745 | 2583 | } |
39236c6e | 2584 | |
0a7de745 | 2585 | if (bap->ba_flags & BA_NOCACHE) { |
fe8ab488 | 2586 | code |= DKIO_NOCACHE; |
0a7de745 | 2587 | } |
316670eb | 2588 | |
d190cdc3 A |
2589 | if (upgrade) { |
2590 | code |= DKIO_TIER_UPGRADE; | |
2591 | SET(bap->ba_flags, BA_IO_TIER_UPGRADE); | |
2592 | } | |
2593 | ||
fe8ab488 | 2594 | if (kdebug_enable) { |
316670eb | 2595 | KERNEL_DEBUG_CONSTANT_IST(KDEBUG_COMMON, FSDBG_CODE(DBG_DKRW, code) | DBG_FUNC_NONE, |
0a7de745 A |
2596 | buf_kernel_addrperm_addr(bp), bdev, buf_blkno(bp), buf_count(bp), 0); |
2597 | } | |
fe8ab488 | 2598 | |
c3c9b80d A |
2599 | #if CONFIG_IO_COMPRESSION_STATS |
2600 | io_compression_stats(bp); | |
2601 | #endif /* CONFIG_IO_COMPRESSION_STATS */ | |
fe8ab488 A |
2602 | thread_update_io_stats(current_thread(), buf_count(bp), code); |
2603 | ||
6d2010ae | 2604 | if (mp != NULL) { |
0a7de745 | 2605 | if (disk_conditioner_mount_is_ssd(mp)) { |
6d2010ae | 2606 | isssd = TRUE; |
0a7de745 | 2607 | } |
39037602 A |
2608 | /* |
2609 | * Partially initialized mounts don't have a final devbsdunit and should not be tracked. | |
2610 | * Verify that devbsdunit is initialized (non-zero) or that 0 is the correct initialized value | |
2611 | * (mnt_throttle_mask is initialized and num_trailing_0 would be 0) | |
2612 | */ | |
2613 | if (mp->mnt_devbsdunit || (mp->mnt_throttle_mask != LOWPRI_MAX_NUM_DEV - 1 && mp->mnt_throttle_mask & 0x1)) { | |
2614 | inflight = TRUE; | |
2615 | } | |
6d2010ae | 2616 | throttle_info = &_throttle_io_info[mp->mnt_devbsdunit]; |
0a7de745 | 2617 | } else { |
6d2010ae | 2618 | throttle_info = &_throttle_io_info[LOWPRI_MAX_NUM_DEV - 1]; |
0a7de745 | 2619 | } |
2d21ac55 | 2620 | |
39037602 | 2621 | throttle_info_update_internal(throttle_info, ut, bflags, isssd, inflight, bap); |
e2fac8b1 | 2622 | |
b0d623f7 | 2623 | if ((bflags & B_READ) == 0) { |
316670eb A |
2624 | microuptime(&throttle_info->throttle_last_write_timestamp); |
2625 | ||
b0d623f7 | 2626 | if (mp) { |
39236c6e | 2627 | mp->mnt_last_write_issued_timestamp = throttle_info->throttle_last_write_timestamp; |
b0d623f7 A |
2628 | INCR_PENDING_IO(buf_count(bp), mp->mnt_pending_write_size); |
2629 | } | |
2630 | } else if (mp) { | |
2631 | INCR_PENDING_IO(buf_count(bp), mp->mnt_pending_read_size); | |
e2fac8b1 | 2632 | } |
6d2010ae A |
2633 | /* |
2634 | * The BootCache may give us special information about | |
2635 | * the IO, so it returns special values that we check | |
2636 | * for here. | |
2637 | * | |
2638 | * IO_SATISFIED_BY_CACHE | |
2639 | * The read has been satisfied by the boot cache. Don't | |
2640 | * throttle the thread unnecessarily. | |
2641 | * | |
2642 | * IO_SHOULD_BE_THROTTLED | |
2643 | * The boot cache is playing back a playlist and this IO | |
2644 | * cut through. Throttle it so we're not cutting through | |
2645 | * the boot cache too often. | |
2646 | * | |
2647 | * Note that typical strategy routines are defined with | |
0a7de745 | 2648 | * a void return so we'll get garbage here. In the |
6d2010ae A |
2649 | * unlikely case the garbage matches our special return |
2650 | * value, it's not a big deal since we're only adjusting | |
2651 | * the throttling delay. | |
0a7de745 | 2652 | */ |
6d2010ae A |
2653 | #define IO_SATISFIED_BY_CACHE ((int)0xcafefeed) |
2654 | #define IO_SHOULD_BE_THROTTLED ((int)0xcafebeef) | |
0a7de745 A |
2655 | typedef int strategy_fcn_ret_t(struct buf *bp); |
2656 | ||
6d2010ae | 2657 | strategy_ret = (*(strategy_fcn_ret_t*)bdevsw[major(bdev)].d_strategy)(bp); |
5ba3f43e A |
2658 | |
2659 | // disk conditioner needs to track when this I/O actually starts | |
2660 | // which means track it after `strategy` which may include delays | |
2661 | // from inflight I/Os | |
2662 | microuptime(&bp->b_timestamp_tv); | |
0a7de745 | 2663 | |
316670eb | 2664 | if (IO_SATISFIED_BY_CACHE == strategy_ret) { |
6d2010ae A |
2665 | /* |
2666 | * If this was a throttled IO satisfied by the boot cache, | |
2667 | * don't delay the thread. | |
2668 | */ | |
2669 | throttle_info_reset_window(ut); | |
316670eb | 2670 | } else if (IO_SHOULD_BE_THROTTLED == strategy_ret) { |
6d2010ae A |
2671 | /* |
2672 | * If the boot cache indicates this IO should be throttled, | |
2673 | * delay the thread. | |
2674 | */ | |
39236c6e | 2675 | throttle_info_set_initial_window(ut, throttle_info, TRUE, isssd); |
6d2010ae | 2676 | } |
0a7de745 | 2677 | return 0; |
ccc36f2f A |
2678 | } |
2679 | ||
1c79356b A |
2680 | |
2681 | /* | |
2682 | * This is a noop, simply returning what one has been given. | |
2683 | */ | |
91447636 A |
2684 | int |
2685 | spec_blockmap(__unused struct vnop_blockmap_args *ap) | |
1c79356b | 2686 | { |
0a7de745 | 2687 | return ENOTSUP; |
1c79356b A |
2688 | } |
2689 | ||
2690 | ||
2691 | /* | |
2692 | * Device close routine | |
2693 | */ | |
91447636 | 2694 | int |
2d21ac55 | 2695 | spec_close(struct vnop_close_args *ap) |
1c79356b | 2696 | { |
2d21ac55 | 2697 | struct vnode *vp = ap->a_vp; |
1c79356b | 2698 | dev_t dev = vp->v_rdev; |
6d2010ae | 2699 | int error = 0; |
2d21ac55 | 2700 | int flags = ap->a_fflag; |
91447636 | 2701 | struct proc *p = vfs_context_proc(ap->a_context); |
2d21ac55 | 2702 | struct session *sessp; |
1c79356b A |
2703 | |
2704 | switch (vp->v_type) { | |
1c79356b A |
2705 | case VCHR: |
2706 | /* | |
2707 | * Hack: a tty device that is a controlling terminal | |
2708 | * has a reference from the session structure. | |
2709 | * We cannot easily tell that a character device is | |
2710 | * a controlling terminal, unless it is the closing | |
2711 | * process' controlling terminal. In that case, | |
b0d623f7 | 2712 | * if the reference count is 1 (this is the very |
316670eb | 2713 | * last close) |
1c79356b | 2714 | */ |
2d21ac55 | 2715 | sessp = proc_session(p); |
39236c6e | 2716 | devsw_lock(dev, S_IFCHR); |
2d21ac55 | 2717 | if (sessp != SESSION_NULL) { |
316670eb | 2718 | if (vp == sessp->s_ttyvp && vcount(vp) == 1) { |
fe8ab488 | 2719 | struct tty *tp = TTY_NULL; |
6d2010ae | 2720 | |
39236c6e | 2721 | devsw_unlock(dev, S_IFCHR); |
2d21ac55 | 2722 | session_lock(sessp); |
6d2010ae | 2723 | if (vp == sessp->s_ttyvp) { |
316670eb | 2724 | tp = SESSION_TP(sessp); |
6d2010ae A |
2725 | sessp->s_ttyvp = NULL; |
2726 | sessp->s_ttyvid = 0; | |
2727 | sessp->s_ttyp = TTY_NULL; | |
2728 | sessp->s_ttypgrpid = NO_PID; | |
0a7de745 | 2729 | } |
2d21ac55 | 2730 | session_unlock(sessp); |
6d2010ae | 2731 | |
fe8ab488 | 2732 | if (tp != TTY_NULL) { |
fe8ab488 | 2733 | ttyfree(tp); |
6d2010ae | 2734 | } |
39236c6e | 2735 | devsw_lock(dev, S_IFCHR); |
2d21ac55 A |
2736 | } |
2737 | session_rele(sessp); | |
1c79356b | 2738 | } |
2d21ac55 | 2739 | |
0a7de745 | 2740 | if (--vp->v_specinfo->si_opencount < 0) { |
316670eb | 2741 | panic("negative open count (c, %u, %u)", major(dev), minor(dev)); |
0a7de745 | 2742 | } |
6d2010ae | 2743 | |
1c79356b | 2744 | /* |
39236c6e | 2745 | * close on last reference or on vnode revoke call |
1c79356b | 2746 | */ |
0a7de745 | 2747 | if (vcount(vp) == 0 || (flags & IO_REVOKE) != 0) { |
316670eb | 2748 | error = cdevsw[major(dev)].d_close(dev, flags, S_IFCHR, p); |
0a7de745 | 2749 | } |
6d2010ae A |
2750 | |
2751 | devsw_unlock(dev, S_IFCHR); | |
1c79356b A |
2752 | break; |
2753 | ||
2754 | case VBLK: | |
1c79356b | 2755 | /* |
6d2010ae A |
2756 | * If there is more than one outstanding open, don't |
2757 | * send the close to the device. | |
0b4e3aa0 | 2758 | */ |
6d2010ae A |
2759 | devsw_lock(dev, S_IFBLK); |
2760 | if (vcount(vp) > 1) { | |
2761 | vp->v_specinfo->si_opencount--; | |
2762 | devsw_unlock(dev, S_IFBLK); | |
0a7de745 | 2763 | return 0; |
6d2010ae A |
2764 | } |
2765 | devsw_unlock(dev, S_IFBLK); | |
0b4e3aa0 A |
2766 | |
2767 | /* | |
2768 | * On last close of a block device (that isn't mounted) | |
2769 | * we must invalidate any in core blocks, so that | |
2770 | * we can, for instance, change floppy disks. | |
2771 | */ | |
0a7de745 A |
2772 | if ((error = spec_fsync_internal(vp, MNT_WAIT, ap->a_context))) { |
2773 | return error; | |
2774 | } | |
91447636 A |
2775 | |
2776 | error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0); | |
0a7de745 A |
2777 | if (error) { |
2778 | return error; | |
2779 | } | |
b0d623f7 | 2780 | |
6d2010ae A |
2781 | devsw_lock(dev, S_IFBLK); |
2782 | ||
0a7de745 | 2783 | if (--vp->v_specinfo->si_opencount < 0) { |
316670eb | 2784 | panic("negative open count (b, %u, %u)", major(dev), minor(dev)); |
0a7de745 | 2785 | } |
6d2010ae | 2786 | |
0a7de745 | 2787 | if (vcount(vp) == 0) { |
316670eb | 2788 | error = bdevsw[major(dev)].d_close(dev, flags, S_IFBLK, p); |
0a7de745 | 2789 | } |
6d2010ae A |
2790 | |
2791 | devsw_unlock(dev, S_IFBLK); | |
1c79356b A |
2792 | break; |
2793 | ||
2794 | default: | |
2795 | panic("spec_close: not special"); | |
0a7de745 | 2796 | return EBADF; |
1c79356b A |
2797 | } |
2798 | ||
6d2010ae | 2799 | return error; |
1c79356b A |
2800 | } |
2801 | ||
2802 | /* | |
2803 | * Return POSIX pathconf information applicable to special devices. | |
2804 | */ | |
91447636 | 2805 | int |
2d21ac55 | 2806 | spec_pathconf(struct vnop_pathconf_args *ap) |
1c79356b | 2807 | { |
1c79356b A |
2808 | switch (ap->a_name) { |
2809 | case _PC_LINK_MAX: | |
2810 | *ap->a_retval = LINK_MAX; | |
0a7de745 | 2811 | return 0; |
1c79356b A |
2812 | case _PC_MAX_CANON: |
2813 | *ap->a_retval = MAX_CANON; | |
0a7de745 | 2814 | return 0; |
1c79356b A |
2815 | case _PC_MAX_INPUT: |
2816 | *ap->a_retval = MAX_INPUT; | |
0a7de745 | 2817 | return 0; |
1c79356b A |
2818 | case _PC_PIPE_BUF: |
2819 | *ap->a_retval = PIPE_BUF; | |
0a7de745 | 2820 | return 0; |
1c79356b | 2821 | case _PC_CHOWN_RESTRICTED: |
0a7de745 A |
2822 | *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */ |
2823 | return 0; | |
1c79356b A |
2824 | case _PC_VDISABLE: |
2825 | *ap->a_retval = _POSIX_VDISABLE; | |
0a7de745 | 2826 | return 0; |
1c79356b | 2827 | default: |
0a7de745 | 2828 | return EINVAL; |
1c79356b A |
2829 | } |
2830 | /* NOTREACHED */ | |
2831 | } | |
2832 | ||
1c79356b A |
2833 | /* |
2834 | * Special device failed operation | |
2835 | */ | |
91447636 A |
2836 | int |
2837 | spec_ebadf(__unused void *dummy) | |
1c79356b | 2838 | { |
0a7de745 | 2839 | return EBADF; |
1c79356b A |
2840 | } |
2841 | ||
1c79356b A |
2842 | /* Blktooff derives file offset from logical block number */ |
2843 | int | |
2d21ac55 | 2844 | spec_blktooff(struct vnop_blktooff_args *ap) |
1c79356b | 2845 | { |
2d21ac55 | 2846 | struct vnode *vp = ap->a_vp; |
1c79356b A |
2847 | |
2848 | switch (vp->v_type) { | |
2849 | case VCHR: | |
2850 | *ap->a_offset = (off_t)-1; /* failure */ | |
0a7de745 | 2851 | return ENOTSUP; |
1c79356b A |
2852 | |
2853 | case VBLK: | |
2854 | printf("spec_blktooff: not implemented for VBLK\n"); | |
2855 | *ap->a_offset = (off_t)-1; /* failure */ | |
0a7de745 | 2856 | return ENOTSUP; |
1c79356b A |
2857 | |
2858 | default: | |
2859 | panic("spec_blktooff type"); | |
2860 | } | |
2861 | /* NOTREACHED */ | |
91447636 | 2862 | |
0a7de745 | 2863 | return 0; |
1c79356b A |
2864 | } |
2865 | ||
2866 | /* Offtoblk derives logical block number from file offset */ | |
2867 | int | |
2d21ac55 | 2868 | spec_offtoblk(struct vnop_offtoblk_args *ap) |
1c79356b | 2869 | { |
2d21ac55 | 2870 | struct vnode *vp = ap->a_vp; |
1c79356b A |
2871 | |
2872 | switch (vp->v_type) { | |
2873 | case VCHR: | |
91447636 | 2874 | *ap->a_lblkno = (daddr64_t)-1; /* failure */ |
0a7de745 | 2875 | return ENOTSUP; |
1c79356b A |
2876 | |
2877 | case VBLK: | |
2878 | printf("spec_offtoblk: not implemented for VBLK\n"); | |
91447636 | 2879 | *ap->a_lblkno = (daddr64_t)-1; /* failure */ |
0a7de745 | 2880 | return ENOTSUP; |
1c79356b A |
2881 | |
2882 | default: | |
2883 | panic("spec_offtoblk type"); | |
2884 | } | |
2885 | /* NOTREACHED */ | |
91447636 | 2886 | |
0a7de745 | 2887 | return 0; |
1c79356b | 2888 | } |
6d2010ae A |
2889 | |
2890 | static void filt_specdetach(struct knote *kn); | |
5ba3f43e | 2891 | static int filt_specevent(struct knote *kn, long hint); |
cb323159 A |
2892 | static int filt_spectouch(struct knote *kn, struct kevent_qos_s *kev); |
2893 | static int filt_specprocess(struct knote *kn, struct kevent_qos_s *kev); | |
d9a64523 | 2894 | static int filt_specpeek(struct knote *kn); |
6d2010ae | 2895 | |
5ba3f43e A |
2896 | SECURITY_READ_ONLY_EARLY(struct filterops) spec_filtops = { |
2897 | .f_isfd = 1, | |
2898 | .f_attach = filt_specattach, | |
2899 | .f_detach = filt_specdetach, | |
2900 | .f_event = filt_specevent, | |
2901 | .f_touch = filt_spectouch, | |
2902 | .f_process = filt_specprocess, | |
2903 | .f_peek = filt_specpeek | |
6d2010ae A |
2904 | }; |
2905 | ||
5ba3f43e A |
2906 | |
2907 | /* | |
2908 | * Given a waitq that is assumed to be embedded within a selinfo structure, | |
2909 | * return the containing selinfo structure. While 'wq' is not really a queue | |
2910 | * element, this macro simply does the offset_of calculation to get back to a | |
2911 | * containing struct given the struct type and member name. | |
2912 | */ | |
2913 | #define selinfo_from_waitq(wq) \ | |
2914 | qe_element((wq), struct selinfo, si_waitq) | |
2915 | ||
6d2010ae | 2916 | static int |
5ba3f43e | 2917 | spec_knote_select_and_link(struct knote *kn) |
6d2010ae | 2918 | { |
5ba3f43e A |
2919 | uthread_t uth; |
2920 | vfs_context_t ctx; | |
2921 | vnode_t vp; | |
2922 | struct waitq_set *old_wqs; | |
2923 | uint64_t rsvd, rsvd_arg; | |
2924 | uint64_t *rlptr = NULL; | |
2925 | struct selinfo *si = NULL; | |
2926 | int selres = 0; | |
2927 | ||
2928 | uth = get_bsdthread_info(current_thread()); | |
2929 | ||
2930 | ctx = vfs_context_current(); | |
f427ee49 | 2931 | vp = (vnode_t)kn->kn_fp->fp_glob->fg_data; |
5ba3f43e | 2932 | |
cb323159 | 2933 | int error = vnode_getwithvid(vp, vnode_vid(vp)); |
5ba3f43e A |
2934 | if (error != 0) { |
2935 | knote_set_error(kn, ENOENT); | |
6d2010ae A |
2936 | return 0; |
2937 | } | |
5ba3f43e A |
2938 | |
2939 | /* | |
2940 | * This function may be called many times to link or re-link the | |
2941 | * underlying vnode to the kqueue. If we've already linked the two, | |
94ff46dc | 2942 | * we will have a valid kn_hook_waitqid which ties us to the underlying |
5ba3f43e A |
2943 | * device's waitq via a the waitq's prepost table object. However, |
2944 | * devices can abort any select action by calling selthreadclear(). | |
2945 | * This is OK because the table object will be invalidated by the | |
2946 | * driver (through a call to selthreadclear), so any attempt to access | |
2947 | * the associated waitq will fail because the table object is invalid. | |
2948 | * | |
2949 | * Even if we've already registered, we need to pass a pointer | |
2950 | * to a reserved link structure. Otherwise, selrecord() will | |
2951 | * infer that we're in the second pass of select() and won't | |
2952 | * actually do anything! | |
2953 | */ | |
2954 | rsvd = rsvd_arg = waitq_link_reserve(NULL); | |
2955 | rlptr = (void *)&rsvd_arg; | |
2956 | ||
2957 | /* | |
a39ff7e2 A |
2958 | * Trick selrecord() into hooking kqueue's wait queue set into the device's |
2959 | * selinfo wait queue. | |
5ba3f43e A |
2960 | */ |
2961 | old_wqs = uth->uu_wqset; | |
2962 | uth->uu_wqset = &(knote_get_kq(kn)->kq_wqs); | |
d9a64523 A |
2963 | |
2964 | /* | |
2965 | * Be sure that the waitq set is linked | |
2966 | * before calling select to avoid possible | |
2967 | * allocation under spinlocks. | |
2968 | */ | |
2969 | waitq_set_lazy_init_link(uth->uu_wqset); | |
2970 | ||
a39ff7e2 A |
2971 | /* |
2972 | * Now these are the laws of VNOP_SELECT, as old and as true as the sky, | |
2973 | * And the device that shall keep it may prosper, but the device that shall | |
2974 | * break it must receive ENODEV: | |
2975 | * | |
2976 | * 1. Take a lock to protect against other selects on the same vnode. | |
2977 | * 2. Return 1 if data is ready to be read. | |
2978 | * 3. Return 0 and call `selrecord` on a handy `selinfo` structure if there | |
2979 | * is no data. | |
2980 | * 4. Call `selwakeup` when the vnode has an active `selrecord` and data | |
2981 | * can be read or written (depending on the seltype). | |
2982 | * 5. If there's a `selrecord` and no corresponding `selwakeup`, but the | |
2983 | * vnode is going away, call `selthreadclear`. | |
2984 | */ | |
5ba3f43e A |
2985 | selres = VNOP_SELECT(vp, knote_get_seltype(kn), 0, rlptr, ctx); |
2986 | uth->uu_wqset = old_wqs; | |
2987 | ||
2988 | /* | |
a39ff7e2 | 2989 | * Make sure to cleanup the reserved link - this guards against |
5ba3f43e A |
2990 | * drivers that may not actually call selrecord(). |
2991 | */ | |
2992 | waitq_link_release(rsvd); | |
2993 | if (rsvd != rsvd_arg) { | |
a39ff7e2 | 2994 | /* The driver / handler called selrecord() */ |
5ba3f43e A |
2995 | struct waitq *wq; |
2996 | memcpy(&wq, rlptr, sizeof(void *)); | |
2997 | ||
2998 | /* | |
2999 | * The waitq is part of the selinfo structure managed by the | |
3000 | * driver. For certain drivers, we want to hook the knote into | |
3001 | * the selinfo structure's si_note field so selwakeup can call | |
3002 | * KNOTE. | |
3003 | */ | |
3004 | si = selinfo_from_waitq(wq); | |
3005 | ||
3006 | /* | |
3007 | * The waitq_get_prepost_id() function will (potentially) | |
3008 | * allocate a prepost table object for the waitq and return | |
3009 | * the table object's ID to us. It will also set the | |
3010 | * waitq_prepost_id field within the waitq structure. | |
3011 | * | |
94ff46dc | 3012 | * We can just overwrite kn_hook_waitqid because it's simply a |
5ba3f43e A |
3013 | * table ID used to grab a reference when needed. |
3014 | * | |
3015 | * We have a reference on the vnode, so we know that the | |
3016 | * device won't go away while we get this ID. | |
94ff46dc A |
3017 | * |
3018 | * Note: on 32bit this field is 32bit only. | |
5ba3f43e | 3019 | */ |
94ff46dc | 3020 | kn->kn_hook_waitqid = (typeof(kn->kn_hook_waitqid))waitq_get_prepost_id(wq); |
a39ff7e2 A |
3021 | } else if (selres == 0) { |
3022 | /* | |
3023 | * The device indicated that there's no data to read, but didn't call | |
3024 | * `selrecord`. Nothing will be notified of changes to this vnode, so | |
3025 | * return an error back to user space, to make it clear that the knote | |
3026 | * is not attached. | |
3027 | */ | |
3028 | knote_set_error(kn, ENODEV); | |
5ba3f43e A |
3029 | } |
3030 | ||
3031 | vnode_put(vp); | |
3032 | ||
3033 | return selres; | |
6d2010ae A |
3034 | } |
3035 | ||
cb323159 A |
3036 | static int |
3037 | filt_spec_common(struct knote *kn, struct kevent_qos_s *kev, int selres) | |
5ba3f43e | 3038 | { |
cb323159 A |
3039 | int64_t data; |
3040 | int ret; | |
3041 | ||
5ba3f43e | 3042 | if (kn->kn_vnode_use_ofst) { |
f427ee49 | 3043 | if (kn->kn_fp->fp_glob->fg_offset >= (uint32_t)selres) { |
cb323159 | 3044 | data = 0; |
5ba3f43e | 3045 | } else { |
f427ee49 | 3046 | data = ((uint32_t)selres) - kn->kn_fp->fp_glob->fg_offset; |
5ba3f43e A |
3047 | } |
3048 | } else { | |
cb323159 A |
3049 | data = selres; |
3050 | } | |
3051 | ||
3052 | ret = data >= knote_low_watermark(kn); | |
3053 | ||
3054 | if (ret && kev) { | |
3055 | knote_fill_kevent(kn, kev, data); | |
5ba3f43e | 3056 | } |
cb323159 A |
3057 | |
3058 | return ret; | |
5ba3f43e A |
3059 | } |
3060 | ||
3061 | static int | |
cb323159 | 3062 | filt_specattach(struct knote *kn, __unused struct kevent_qos_s *kev) |
6d2010ae A |
3063 | { |
3064 | vnode_t vp; | |
3065 | dev_t dev; | |
3066 | ||
f427ee49 | 3067 | vp = (vnode_t)kn->kn_fp->fp_glob->fg_data; /* Already have iocount, and vnode is alive */ |
6d2010ae A |
3068 | |
3069 | assert(vnode_ischr(vp)); | |
3070 | ||
3071 | dev = vnode_specrdev(vp); | |
3072 | ||
3e170ce0 A |
3073 | /* |
3074 | * For a few special kinds of devices, we can attach knotes with | |
3075 | * no restrictions because their "select" vectors return the amount | |
3076 | * of data available. Others require an explicit NOTE_LOWAT with | |
3077 | * data of 1, indicating that the caller doesn't care about actual | |
3078 | * data counts, just an indication that the device has data. | |
3079 | */ | |
5ba3f43e | 3080 | if (!kn->kn_vnode_kqok && |
3e170ce0 | 3081 | ((kn->kn_sfflags & NOTE_LOWAT) == 0 || kn->kn_sdata != 1)) { |
5ba3f43e | 3082 | knote_set_error(kn, EINVAL); |
39037602 | 3083 | return 0; |
6d2010ae A |
3084 | } |
3085 | ||
5ba3f43e A |
3086 | /* |
3087 | * This forces the select fallback to call through VNOP_SELECT and hook | |
3088 | * up selinfo on every filter routine. | |
3089 | * | |
3090 | * Pseudo-terminal controllers are opted out of native kevent support -- | |
3091 | * remove this when they get their own EVFILTID. | |
3092 | */ | |
3093 | if (cdevsw_flags[major(dev)] & CDEVSW_IS_PTC) { | |
3094 | kn->kn_vnode_kqok = 0; | |
3095 | } | |
6d2010ae | 3096 | |
39037602 | 3097 | kn->kn_filtid = EVFILTID_SPEC; |
94ff46dc | 3098 | kn->kn_hook_waitqid = 0; |
6d2010ae | 3099 | |
39037602 | 3100 | knote_markstayactive(kn); |
5ba3f43e | 3101 | return spec_knote_select_and_link(kn); |
6d2010ae A |
3102 | } |
3103 | ||
5ba3f43e | 3104 | static void |
6d2010ae A |
3105 | filt_specdetach(struct knote *kn) |
3106 | { | |
39037602 | 3107 | knote_clearstayactive(kn); |
6d2010ae | 3108 | |
3e170ce0 A |
3109 | /* |
3110 | * This is potentially tricky: the device's selinfo waitq that was | |
3111 | * tricked into being part of this knote's waitq set may not be a part | |
3112 | * of any other set, and the device itself may have revoked the memory | |
94ff46dc | 3113 | * in which the waitq was held. We use the knote's kn_hook_waitqid field |
3e170ce0 A |
3114 | * to keep the ID of the waitq's prepost table object. This |
3115 | * object keeps a pointer back to the waitq, and gives us a safe way | |
3116 | * to decouple the dereferencing of driver allocated memory: if the | |
3117 | * driver goes away (taking the waitq with it) then the prepost table | |
3118 | * object will be invalidated. The waitq details are handled in the | |
3119 | * waitq API invoked here. | |
6d2010ae | 3120 | */ |
94ff46dc A |
3121 | if (kn->kn_hook_waitqid) { |
3122 | waitq_unlink_by_prepost_id(kn->kn_hook_waitqid, &(knote_get_kq(kn)->kq_wqs)); | |
3123 | kn->kn_hook_waitqid = 0; | |
6d2010ae | 3124 | } |
6d2010ae A |
3125 | } |
3126 | ||
5ba3f43e A |
3127 | static int |
3128 | filt_specevent(struct knote *kn, __unused long hint) | |
6d2010ae | 3129 | { |
5ba3f43e A |
3130 | /* |
3131 | * Nothing should call knote or knote_vanish on this knote. | |
3132 | */ | |
3133 | panic("filt_specevent(%p)", kn); | |
39037602 A |
3134 | return 0; |
3135 | } | |
3136 | ||
39037602 | 3137 | static int |
cb323159 | 3138 | filt_spectouch(struct knote *kn, struct kevent_qos_s *kev) |
39037602 A |
3139 | { |
3140 | kn->kn_sdata = kev->data; | |
3141 | kn->kn_sfflags = kev->fflags; | |
39037602 | 3142 | |
5ba3f43e A |
3143 | if (kev->flags & EV_ENABLE) { |
3144 | return spec_knote_select_and_link(kn); | |
3145 | } | |
3146 | ||
39037602 A |
3147 | return 0; |
3148 | } | |
3149 | ||
3150 | static int | |
cb323159 | 3151 | filt_specprocess(struct knote *kn, struct kevent_qos_s *kev) |
39037602 | 3152 | { |
6d2010ae A |
3153 | vnode_t vp; |
3154 | uthread_t uth; | |
6d2010ae | 3155 | vfs_context_t ctx; |
39037602 | 3156 | int res; |
6d2010ae A |
3157 | int selres; |
3158 | int error; | |
6d2010ae | 3159 | |
6d2010ae A |
3160 | uth = get_bsdthread_info(current_thread()); |
3161 | ctx = vfs_context_current(); | |
f427ee49 | 3162 | vp = (vnode_t)kn->kn_fp->fp_glob->fg_data; |
6d2010ae | 3163 | |
cb323159 | 3164 | error = vnode_getwithvid(vp, vnode_vid(vp)); |
6d2010ae A |
3165 | if (error != 0) { |
3166 | kn->kn_flags |= (EV_EOF | EV_ONESHOT); | |
cb323159 | 3167 | knote_fill_kevent(kn, kev, 0); |
6d2010ae A |
3168 | return 1; |
3169 | } | |
6d2010ae | 3170 | |
5ba3f43e | 3171 | selres = spec_knote_select_and_link(kn); |
cb323159 | 3172 | res = filt_spec_common(kn, kev, selres); |
6d2010ae A |
3173 | |
3174 | vnode_put(vp); | |
3175 | ||
39037602 | 3176 | return res; |
6d2010ae A |
3177 | } |
3178 | ||
d9a64523 | 3179 | static int |
6d2010ae A |
3180 | filt_specpeek(struct knote *kn) |
3181 | { | |
5ba3f43e | 3182 | int selres = 0; |
3e170ce0 | 3183 | |
5ba3f43e | 3184 | selres = spec_knote_select_and_link(kn); |
cb323159 | 3185 | return filt_spec_common(kn, NULL, selres); |
6d2010ae | 3186 | } |