]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/specfs/spec_vnops.c
aad1b0250e1d9f5cad5ca0c166c6c54ef09c82db
[apple/xnu.git] / bsd / miscfs / specfs / spec_vnops.c
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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>
65 #include <sys/proc_internal.h>
66 #include <sys/kauth.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/conf.h>
70 #include <sys/buf_internal.h>
71 #include <sys/mount_internal.h>
72 #include <sys/namei.h>
73 #include <sys/vnode_internal.h>
74 #include <sys/stat.h>
75 #include <sys/errno.h>
76 #include <sys/ioctl.h>
77 #include <sys/file.h>
78 #include <sys/user.h>
79 #include <sys/malloc.h>
80 #include <sys/disk.h>
81 #include <sys/uio_internal.h>
82 #include <sys/resource.h>
83 #include <miscfs/specfs/specdev.h>
84 #include <vfs/vfs_support.h>
85
86 #include <sys/kdebug.h>
87
88 /* XXX following three prototypes should be in a header file somewhere */
89 extern int isdisk(dev_t dev, int type);
90 extern dev_t chrtoblk(dev_t dev);
91 extern int iskmemdev(dev_t dev);
92
93 struct vnode *speclisth[SPECHSZ];
94
95 /* symbolic sleep message strings for devices */
96 char devopn[] = "devopn";
97 char devio[] = "devio";
98 char devwait[] = "devwait";
99 char devin[] = "devin";
100 char devout[] = "devout";
101 char devioc[] = "devioc";
102 char devcls[] = "devcls";
103
104 #define VOPFUNC int (*)(void *)
105
106 int (**spec_vnodeop_p)(void *);
107 struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
108 { &vnop_default_desc, (VOPFUNC)vn_default_error },
109 { &vnop_lookup_desc, (VOPFUNC)spec_lookup }, /* lookup */
110 { &vnop_create_desc, (VOPFUNC)err_create }, /* create */
111 { &vnop_mknod_desc, (VOPFUNC)err_mknod }, /* mknod */
112 { &vnop_open_desc, (VOPFUNC)spec_open }, /* open */
113 { &vnop_close_desc, (VOPFUNC)spec_close }, /* close */
114 { &vnop_access_desc, (VOPFUNC)spec_access }, /* access */
115 { &vnop_getattr_desc, (VOPFUNC)spec_getattr }, /* getattr */
116 { &vnop_setattr_desc, (VOPFUNC)spec_setattr }, /* setattr */
117 { &vnop_read_desc, (VOPFUNC)spec_read }, /* read */
118 { &vnop_write_desc, (VOPFUNC)spec_write }, /* write */
119 { &vnop_ioctl_desc, (VOPFUNC)spec_ioctl }, /* ioctl */
120 { &vnop_select_desc, (VOPFUNC)spec_select }, /* select */
121 { &vnop_revoke_desc, (VOPFUNC)nop_revoke }, /* revoke */
122 { &vnop_mmap_desc, (VOPFUNC)err_mmap }, /* mmap */
123 { &vnop_fsync_desc, (VOPFUNC)spec_fsync }, /* fsync */
124 { &vnop_remove_desc, (VOPFUNC)err_remove }, /* remove */
125 { &vnop_link_desc, (VOPFUNC)err_link }, /* link */
126 { &vnop_rename_desc, (VOPFUNC)err_rename }, /* rename */
127 { &vnop_mkdir_desc, (VOPFUNC)err_mkdir }, /* mkdir */
128 { &vnop_rmdir_desc, (VOPFUNC)err_rmdir }, /* rmdir */
129 { &vnop_symlink_desc, (VOPFUNC)err_symlink }, /* symlink */
130 { &vnop_readdir_desc, (VOPFUNC)err_readdir }, /* readdir */
131 { &vnop_readlink_desc, (VOPFUNC)err_readlink }, /* readlink */
132 { &vnop_inactive_desc, (VOPFUNC)nop_inactive }, /* inactive */
133 { &vnop_reclaim_desc, (VOPFUNC)nop_reclaim }, /* reclaim */
134 { &vnop_strategy_desc, (VOPFUNC)spec_strategy }, /* strategy */
135 { &vnop_pathconf_desc, (VOPFUNC)spec_pathconf }, /* pathconf */
136 { &vnop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
137 { &vnop_bwrite_desc, (VOPFUNC)spec_bwrite }, /* bwrite */
138 { &vnop_pagein_desc, (VOPFUNC)err_pagein }, /* Pagein */
139 { &vnop_pageout_desc, (VOPFUNC)err_pageout }, /* Pageout */
140 { &vnop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copyfile */
141 { &vnop_blktooff_desc, (VOPFUNC)spec_blktooff }, /* blktooff */
142 { &vnop_offtoblk_desc, (VOPFUNC)spec_offtoblk }, /* offtoblk */
143 { &vnop_blockmap_desc, (VOPFUNC)spec_blockmap }, /* blockmap */
144 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
145 };
146 struct vnodeopv_desc spec_vnodeop_opv_desc =
147 { &spec_vnodeop_p, spec_vnodeop_entries };
148
149
150 static void set_blocksize(vnode_t, dev_t);
151
152
153 /*
154 * Trivial lookup routine that always fails.
155 */
156 int
157 spec_lookup(struct vnop_lookup_args *ap)
158 {
159
160 *ap->a_vpp = NULL;
161 return (ENOTDIR);
162 }
163
164 static void
165 set_blocksize(struct vnode *vp, dev_t dev)
166 {
167 int (*size)(dev_t);
168 int rsize;
169
170 if ((major(dev) < nblkdev) && (size = bdevsw[major(dev)].d_psize)) {
171 rsize = (*size)(dev);
172 if (rsize <= 0) /* did size fail? */
173 vp->v_specsize = DEV_BSIZE;
174 else
175 vp->v_specsize = rsize;
176 }
177 else
178 vp->v_specsize = DEV_BSIZE;
179 }
180
181 void
182 set_fsblocksize(struct vnode *vp)
183 {
184
185 if (vp->v_type == VBLK) {
186 dev_t dev = (dev_t)vp->v_rdev;
187 int maj = major(dev);
188
189 if ((u_int)maj >= (u_int)nblkdev)
190 return;
191
192 vnode_lock(vp);
193 set_blocksize(vp, dev);
194 vnode_unlock(vp);
195 }
196
197 }
198
199
200 /*
201 * Open a special file.
202 */
203 int
204 spec_open(struct vnop_open_args *ap)
205 {
206 struct proc *p = vfs_context_proc(ap->a_context);
207 kauth_cred_t cred = vfs_context_ucred(ap->a_context);
208 struct vnode *vp = ap->a_vp;
209 dev_t bdev, dev = (dev_t)vp->v_rdev;
210 int maj = major(dev);
211 int error;
212
213 /*
214 * Don't allow open if fs is mounted -nodev.
215 */
216 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
217 return (ENXIO);
218
219 switch (vp->v_type) {
220
221 case VCHR:
222 if ((u_int)maj >= (u_int)nchrdev)
223 return (ENXIO);
224 if (cred != FSCRED && (ap->a_mode & FWRITE)) {
225 /*
226 * When running in very secure mode, do not allow
227 * opens for writing of any disk character devices.
228 */
229 if (securelevel >= 2 && isdisk(dev, VCHR))
230 return (EPERM);
231 /*
232 * When running in secure mode, do not allow opens
233 * for writing of /dev/mem, /dev/kmem, or character
234 * devices whose corresponding block devices are
235 * currently mounted.
236 */
237 if (securelevel >= 1) {
238 if ((bdev = chrtoblk(dev)) != NODEV && check_mountedon(bdev, VBLK, &error))
239 return (error);
240 if (iskmemdev(dev))
241 return (EPERM);
242 }
243 }
244 if (cdevsw[maj].d_type == D_TTY) {
245 vnode_lock(vp);
246 vp->v_flag |= VISTTY;
247 vnode_unlock(vp);
248 }
249 error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, p);
250 return (error);
251
252 case VBLK:
253 if ((u_int)maj >= (u_int)nblkdev)
254 return (ENXIO);
255 /*
256 * When running in very secure mode, do not allow
257 * opens for writing of any disk block devices.
258 */
259 if (securelevel >= 2 && cred != FSCRED &&
260 (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK)
261 return (EPERM);
262 /*
263 * Do not allow opens of block devices that are
264 * currently mounted.
265 */
266 if ( (error = vfs_mountedon(vp)) )
267 return (error);
268 error = (*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, p);
269 if (!error) {
270 u_int64_t blkcnt;
271 u_int32_t blksize;
272 int setsize = 0;
273 u_int32_t size512 = 512;
274
275
276 if (!VNOP_IOCTL(vp, DKIOCGETBLOCKSIZE, (caddr_t)&blksize, 0, ap->a_context)) {
277 /* Switch to 512 byte sectors (temporarily) */
278
279 if (!VNOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&size512, FWRITE, ap->a_context)) {
280 /* Get the number of 512 byte physical blocks. */
281 if (!VNOP_IOCTL(vp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, ap->a_context)) {
282 setsize = 1;
283 }
284 }
285 /* If it doesn't set back, we can't recover */
286 if (VNOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&blksize, FWRITE, ap->a_context))
287 error = ENXIO;
288 }
289
290
291 vnode_lock(vp);
292 set_blocksize(vp, dev);
293
294 /*
295 * Cache the size in bytes of the block device for later
296 * use by spec_write().
297 */
298 if (setsize)
299 vp->v_specdevsize = blkcnt * (u_int64_t)size512;
300 else
301 vp->v_specdevsize = (u_int64_t)0; /* Default: Can't get */
302
303 vnode_unlock(vp);
304
305 }
306 return(error);
307 default:
308 panic("spec_open type");
309 }
310 return (0);
311 }
312
313 /*
314 * Vnode op for read
315 */
316 int
317 spec_read(struct vnop_read_args *ap)
318 {
319 struct vnode *vp = ap->a_vp;
320 struct uio *uio = ap->a_uio;
321 struct buf *bp;
322 daddr64_t bn, nextbn;
323 long bsize, bscale;
324 int devBlockSize=0;
325 int n, on;
326 int error = 0;
327 dev_t dev;
328
329 #if DIAGNOSTIC
330 if (uio->uio_rw != UIO_READ)
331 panic("spec_read mode");
332 if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg))
333 panic("spec_read proc");
334 #endif
335 if (uio_resid(uio) == 0)
336 return (0);
337
338 switch (vp->v_type) {
339
340 case VCHR:
341 error = (*cdevsw[major(vp->v_rdev)].d_read)
342 (vp->v_rdev, uio, ap->a_ioflag);
343 return (error);
344
345 case VBLK:
346 if (uio->uio_offset < 0)
347 return (EINVAL);
348
349 dev = vp->v_rdev;
350
351 devBlockSize = vp->v_specsize;
352
353 if (devBlockSize > PAGE_SIZE)
354 return (EINVAL);
355
356 bscale = PAGE_SIZE / devBlockSize;
357 bsize = bscale * devBlockSize;
358
359 do {
360 on = uio->uio_offset % bsize;
361
362 bn = (daddr64_t)((uio->uio_offset / devBlockSize) &~ (bscale - 1));
363
364 if (vp->v_speclastr + bscale == bn) {
365 nextbn = bn + bscale;
366 error = buf_breadn(vp, bn, (int)bsize, &nextbn,
367 (int *)&bsize, 1, NOCRED, &bp);
368 } else
369 error = buf_bread(vp, bn, (int)bsize, NOCRED, &bp);
370
371 vnode_lock(vp);
372 vp->v_speclastr = bn;
373 vnode_unlock(vp);
374
375 n = bsize - buf_resid(bp);
376 if ((on > n) || error) {
377 if (!error)
378 error = EINVAL;
379 buf_brelse(bp);
380 return (error);
381 }
382 // LP64todo - fix this!
383 n = min((unsigned)(n - on), uio_resid(uio));
384
385 error = uiomove((char *)0 + buf_dataptr(bp) + on, n, uio);
386 if (n + on == bsize)
387 buf_markaged(bp);
388 buf_brelse(bp);
389 } while (error == 0 && uio_resid(uio) > 0 && n != 0);
390 return (error);
391
392 default:
393 panic("spec_read type");
394 }
395 /* NOTREACHED */
396
397 return (0);
398 }
399
400 /*
401 * Vnode op for write
402 */
403 int
404 spec_write(struct vnop_write_args *ap)
405 {
406 struct vnode *vp = ap->a_vp;
407 struct uio *uio = ap->a_uio;
408 struct buf *bp;
409 daddr64_t bn;
410 int bsize, blkmask, bscale;
411 int io_sync;
412 int io_size;
413 int devBlockSize=0;
414 int n, on;
415 int error = 0;
416 dev_t dev;
417
418 #if DIAGNOSTIC
419 if (uio->uio_rw != UIO_WRITE)
420 panic("spec_write mode");
421 if (UIO_SEG_IS_USER_SPACE(uio->uio_segflg))
422 panic("spec_write proc");
423 #endif
424
425 switch (vp->v_type) {
426
427 case VCHR:
428 error = (*cdevsw[major(vp->v_rdev)].d_write)
429 (vp->v_rdev, uio, ap->a_ioflag);
430 return (error);
431
432 case VBLK:
433 if (uio_resid(uio) == 0)
434 return (0);
435 if (uio->uio_offset < 0)
436 return (EINVAL);
437
438 io_sync = (ap->a_ioflag & IO_SYNC);
439 // LP64todo - fix this!
440 io_size = uio_resid(uio);
441
442 dev = (vp->v_rdev);
443
444 devBlockSize = vp->v_specsize;
445 if (devBlockSize > PAGE_SIZE)
446 return(EINVAL);
447
448 bscale = PAGE_SIZE / devBlockSize;
449 blkmask = bscale - 1;
450 bsize = bscale * devBlockSize;
451
452
453 do {
454 bn = (daddr64_t)((uio->uio_offset / devBlockSize) &~ blkmask);
455 on = uio->uio_offset % bsize;
456
457 // LP64todo - fix this!
458 n = min((unsigned)(bsize - on), uio_resid(uio));
459
460 /*
461 * Use buf_getblk() as an optimization IFF:
462 *
463 * 1) We are reading exactly a block on a block
464 * aligned boundary
465 * 2) We know the size of the device from spec_open
466 * 3) The read doesn't span the end of the device
467 *
468 * Otherwise, we fall back on buf_bread().
469 */
470 if (n == bsize &&
471 vp->v_specdevsize != (u_int64_t)0 &&
472 (uio->uio_offset + (u_int64_t)n) > vp->v_specdevsize) {
473 /* reduce the size of the read to what is there */
474 n = (uio->uio_offset + (u_int64_t)n) - vp->v_specdevsize;
475 }
476
477 if (n == bsize)
478 bp = buf_getblk(vp, bn, bsize, 0, 0, BLK_WRITE);
479 else
480 error = (int)buf_bread(vp, bn, bsize, NOCRED, &bp);
481
482 /* Translate downstream error for upstream, if needed */
483 if (!error)
484 error = (int)buf_error(bp);
485 if (error) {
486 buf_brelse(bp);
487 return (error);
488 }
489 n = min(n, bsize - buf_resid(bp));
490
491 error = uiomove((char *)0 + buf_dataptr(bp) + on, n, uio);
492 if (error) {
493 buf_brelse(bp);
494 return (error);
495 }
496 buf_markaged(bp);
497
498 if (io_sync)
499 error = buf_bwrite(bp);
500 else {
501 if ((n + on) == bsize)
502 error = buf_bawrite(bp);
503 else
504 error = buf_bdwrite(bp);
505 }
506 } while (error == 0 && uio_resid(uio) > 0 && n != 0);
507 return (error);
508
509 default:
510 panic("spec_write type");
511 }
512 /* NOTREACHED */
513
514 return (0);
515 }
516
517 /*
518 * Device ioctl operation.
519 */
520 int
521 spec_ioctl(struct vnop_ioctl_args *ap)
522 {
523 proc_t p = vfs_context_proc(ap->a_context);
524 dev_t dev = ap->a_vp->v_rdev;
525
526 switch (ap->a_vp->v_type) {
527
528 case VCHR:
529 return ((*cdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data,
530 ap->a_fflag, p));
531
532 case VBLK:
533 if (ap->a_command == 0 && (unsigned int)ap->a_data == B_TAPE) {
534 if (bdevsw[major(dev)].d_type == D_TAPE)
535 return (0);
536 else
537 return (1);
538 }
539 return ((*bdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data,
540 ap->a_fflag, p));
541
542 default:
543 panic("spec_ioctl");
544 /* NOTREACHED */
545 }
546 return (0);
547 }
548
549 int
550 spec_select(struct vnop_select_args *ap)
551 {
552 proc_t p = vfs_context_proc(ap->a_context);
553 dev_t dev;
554
555 switch (ap->a_vp->v_type) {
556
557 default:
558 return (1); /* XXX */
559
560 case VCHR:
561 dev = ap->a_vp->v_rdev;
562 return (*cdevsw[major(dev)].d_select)(dev, ap->a_which, ap->a_wql, p);
563 }
564 }
565
566 /*
567 * Synch buffers associated with a block device
568 */
569 int
570 spec_fsync_internal(vnode_t vp, int waitfor, __unused vfs_context_t context)
571 {
572 if (vp->v_type == VCHR)
573 return (0);
574 /*
575 * Flush all dirty buffers associated with a block device.
576 */
577 buf_flushdirtyblks(vp, waitfor == MNT_WAIT, 0, "spec_fsync");
578
579 return (0);
580 }
581
582 int
583 spec_fsync(struct vnop_fsync_args *ap)
584 {
585 return spec_fsync_internal(ap->a_vp, ap->a_waitfor, ap->a_context);
586 }
587
588 /*
589 * Just call the device strategy routine
590 */
591 extern int hard_throttle_on_root;
592 void IOSleep(int);
593
594 // the low priority process may wait for at most LOWPRI_MAX_DELAY millisecond
595 #define LOWPRI_INITIAL_WINDOW_MSECS 100
596 #define LOWPRI_WINDOW_MSECS_INC 50
597 #define LOWPRI_MAX_WINDOW_MSECS 200
598 #define LOWPRI_MAX_WAITING_MSECS 200
599 #define LOWPRI_SLEEP_INTERVAL 5
600
601 struct _throttle_io_info_t {
602 struct timeval last_normal_IO_timestamp;
603 SInt32 numthreads_throttling;
604 };
605
606 struct _throttle_io_info_t _throttle_io_info[LOWPRI_MAX_NUM_DEV];
607 int lowpri_IO_initial_window_msecs = LOWPRI_INITIAL_WINDOW_MSECS;
608 int lowpri_IO_window_msecs_inc = LOWPRI_WINDOW_MSECS_INC;
609 int lowpri_max_window_msecs = LOWPRI_MAX_WINDOW_MSECS;
610 int lowpri_max_waiting_msecs = LOWPRI_MAX_WAITING_MSECS;
611
612 SYSCTL_INT(_debug, OID_AUTO, lowpri_IO_initial_window_msecs, CTLFLAG_RW, &lowpri_IO_initial_window_msecs, LOWPRI_INITIAL_WINDOW_MSECS, "");
613 SYSCTL_INT(_debug, OID_AUTO, lowpri_IO_window_inc, CTLFLAG_RW, &lowpri_IO_window_msecs_inc, LOWPRI_INITIAL_WINDOW_MSECS, "");
614 SYSCTL_INT(_debug, OID_AUTO, lowpri_max_window_msecs, CTLFLAG_RW, &lowpri_max_window_msecs, LOWPRI_INITIAL_WINDOW_MSECS, "");
615 SYSCTL_INT(_debug, OID_AUTO, lowpri_max_waiting_msecs, CTLFLAG_RW, &lowpri_max_waiting_msecs, LOWPRI_INITIAL_WINDOW_MSECS, "");
616
617 int throttle_io_will_be_throttled(int lowpri_window_msecs, size_t devbsdunit)
618 {
619 struct timeval elapsed;
620 int elapsed_msecs;
621
622 microuptime(&elapsed);
623 timevalsub(&elapsed, &_throttle_io_info[devbsdunit].last_normal_IO_timestamp);
624 elapsed_msecs = elapsed.tv_sec * 1000 + elapsed.tv_usec / 1000;
625
626 if (lowpri_window_msecs == -1) // use the max waiting time
627 lowpri_window_msecs = lowpri_max_waiting_msecs;
628
629 return elapsed_msecs < lowpri_window_msecs;
630 }
631
632 void throttle_lowpri_io(boolean_t ok_to_sleep)
633 {
634 int i;
635 int max_try_num;
636 struct uthread *ut;
637
638 ut = get_bsdthread_info(current_thread());
639
640 if (ut->uu_lowpri_window == 0)
641 return;
642
643 max_try_num = lowpri_max_waiting_msecs / LOWPRI_SLEEP_INTERVAL * MAX(1, _throttle_io_info[ut->uu_devbsdunit].numthreads_throttling);
644
645 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_START,
646 ut->uu_lowpri_window, 0, 0, 0, 0);
647
648 if (ok_to_sleep == TRUE) {
649 for (i=0; i<max_try_num; i++) {
650 if (throttle_io_will_be_throttled(ut->uu_lowpri_window, ut->uu_devbsdunit)) {
651 IOSleep(LOWPRI_SLEEP_INTERVAL);
652 } else {
653 break;
654 }
655 }
656 }
657 KERNEL_DEBUG((FSDBG_CODE(DBG_FSRW, 97)) | DBG_FUNC_END,
658 ut->uu_lowpri_window, i*5, 0, 0, 0);
659 SInt32 oldValue;
660 oldValue = OSDecrementAtomic(&_throttle_io_info[ut->uu_devbsdunit].numthreads_throttling);
661 ut->uu_lowpri_window = 0;
662
663 if (oldValue <= 0) {
664 panic("%s: numthreads negative", __func__);
665 }
666 }
667
668 int throttle_get_io_policy(struct uthread **ut)
669 {
670 int policy = IOPOL_DEFAULT;
671 proc_t p = current_proc();
672
673 *ut = get_bsdthread_info(current_thread());
674
675 if (p != NULL)
676 policy = p->p_iopol_disk;
677
678 if (*ut != NULL) {
679 // the I/O policy of the thread overrides that of the process
680 // unless the I/O policy of the thread is default
681 if ((*ut)->uu_iopol_disk != IOPOL_DEFAULT)
682 policy = (*ut)->uu_iopol_disk;
683 }
684 return policy;
685 }
686
687 int
688 spec_strategy(struct vnop_strategy_args *ap)
689 {
690 buf_t bp;
691 int bflags;
692 dev_t bdev;
693
694 bp = ap->a_bp;
695 bdev = buf_device(bp);
696 bflags = buf_flags(bp);
697
698 if (kdebug_enable) {
699 int code = 0;
700
701 if (bflags & B_READ)
702 code |= DKIO_READ;
703 if (bflags & B_ASYNC)
704 code |= DKIO_ASYNC;
705
706 if (bflags & B_META)
707 code |= DKIO_META;
708 else if (bflags & B_PAGEIO)
709 code |= DKIO_PAGING;
710
711 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_DKRW, code) | DBG_FUNC_NONE,
712 (unsigned int)bp, bdev, (int)buf_blkno(bp), buf_count(bp), 0);
713 }
714 if (((bflags & (B_PAGEIO | B_READ)) == (B_PAGEIO | B_READ)) &&
715 (buf_vnode(bp)->v_mount->mnt_kern_flag & MNTK_ROOTDEV))
716 hard_throttle_on_root = 1;
717
718 if (lowpri_IO_initial_window_msecs) {
719 struct uthread *ut;
720 int policy;
721 int is_throttleable_io = 0;
722 int is_passive_io = 0;
723 size_t devbsdunit;
724 SInt32 oldValue;
725
726 policy = throttle_get_io_policy(&ut);
727
728 switch (policy) {
729 case IOPOL_DEFAULT:
730 case IOPOL_NORMAL:
731 break;
732 case IOPOL_THROTTLE:
733 is_throttleable_io = 1;
734 break;
735 case IOPOL_PASSIVE:
736 is_passive_io = 1;
737 break;
738 default:
739 printf("unknown I/O policy %d", policy);
740 break;
741 }
742
743 if (!is_throttleable_io && ISSET(bflags, B_PASSIVE))
744 is_passive_io |= 1;
745
746 if (buf_vnode(bp)->v_mount != NULL)
747 devbsdunit = buf_vnode(bp)->v_mount->mnt_devbsdunit;
748 else
749 devbsdunit = LOWPRI_MAX_NUM_DEV - 1;
750 if (!is_throttleable_io) {
751 if (!is_passive_io){
752 microuptime(&_throttle_io_info[devbsdunit].last_normal_IO_timestamp);
753 }
754 } else {
755 /*
756 * I'd really like to do the IOSleep here, but
757 * we may be holding all kinds of filesystem related locks
758 * and the pages for this I/O marked 'busy'...
759 * we don't want to cause a normal task to block on
760 * one of these locks while we're throttling a task marked
761 * for low priority I/O... we'll mark the uthread and
762 * do the delay just before we return from the system
763 * call that triggered this I/O or from vnode_pagein
764 */
765 if (ut->uu_lowpri_window == 0) {
766 ut->uu_devbsdunit = devbsdunit;
767 oldValue = OSIncrementAtomic(&_throttle_io_info[devbsdunit].numthreads_throttling);
768 if (oldValue < 0) {
769 panic("%s: numthreads negative", __func__);
770 }
771 ut->uu_lowpri_window = lowpri_IO_initial_window_msecs;
772 ut->uu_lowpri_window += lowpri_IO_window_msecs_inc * oldValue;
773 } else {
774 if (ut->uu_devbsdunit != devbsdunit) { // the thread sends I/Os to different devices within the same system call
775 // keep track of the numthreads in the right device
776 OSDecrementAtomic(&_throttle_io_info[ut->uu_devbsdunit].numthreads_throttling);
777 OSIncrementAtomic(&_throttle_io_info[devbsdunit].numthreads_throttling);
778 ut->uu_devbsdunit = devbsdunit;
779 }
780 int numthreads = MAX(1, _throttle_io_info[devbsdunit].numthreads_throttling);
781 ut->uu_lowpri_window += lowpri_IO_window_msecs_inc * numthreads;
782 if (ut->uu_lowpri_window > lowpri_max_window_msecs * numthreads)
783 ut->uu_lowpri_window = lowpri_max_window_msecs * numthreads;
784 }
785 }
786 }
787 (*bdevsw[major(bdev)].d_strategy)(bp);
788
789 return (0);
790 }
791
792
793 /*
794 * This is a noop, simply returning what one has been given.
795 */
796 int
797 spec_blockmap(__unused struct vnop_blockmap_args *ap)
798 {
799 return (ENOTSUP);
800 }
801
802
803 /*
804 * Device close routine
805 */
806 int
807 spec_close(struct vnop_close_args *ap)
808 {
809 struct vnode *vp = ap->a_vp;
810 dev_t dev = vp->v_rdev;
811 int (*devclose)(dev_t, int, int, struct proc *);
812 int mode, error;
813 int flags = ap->a_fflag;
814 struct proc *p = vfs_context_proc(ap->a_context);
815 struct session *sessp;
816
817 switch (vp->v_type) {
818
819 case VCHR:
820 /*
821 * Hack: a tty device that is a controlling terminal
822 * has a reference from the session structure.
823 * We cannot easily tell that a character device is
824 * a controlling terminal, unless it is the closing
825 * process' controlling terminal. In that case,
826 * if the reference count is 2 (this last descriptor
827 * plus the session), release the reference from the session.
828 */
829 sessp = proc_session(p);
830 if (sessp != SESSION_NULL) {
831 if ((vcount(vp) == 2) &&
832 (vp == sessp->s_ttyvp)) {
833 session_lock(sessp);
834 sessp->s_ttyvp = NULL;
835 sessp->s_ttyvid = 0;
836 sessp->s_ttyp = NULL;
837 sessp->s_ttypgrpid = NO_PID;
838 session_unlock(sessp);
839 vnode_rele(vp);
840 }
841 session_rele(sessp);
842 }
843
844 devclose = cdevsw[major(dev)].d_close;
845 mode = S_IFCHR;
846 /*
847 * close on last reference or on vnode revoke call
848 */
849 if ((flags & IO_REVOKE) != 0)
850 break;
851 if (vcount(vp) > 1)
852 return (0);
853 break;
854
855 case VBLK:
856 #ifdef DEVFS_IMPLEMENTS_LOCKING
857 /*
858 * On last close of a block device (that isn't mounted)
859 * we must invalidate any in core blocks, so that
860 * we can, for instance, change floppy disks.
861 */
862 if ((error = spec_fsync_internal(vp, MNT_WAIT, ap->a_context)))
863 return (error);
864
865 error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0);
866 if (error)
867 return (error);
868 /*
869 * Since every use (buffer, vnode, swap, blockmap)
870 * holds a reference to the vnode, and because we mark
871 * any other vnodes that alias this device, when the
872 * sum of the reference counts on all the aliased
873 * vnodes descends to one, we are on last close.
874 */
875 if (vcount(vp) > 0)
876 return (0);
877 #else /* DEVFS_IMPLEMENTS_LOCKING */
878 /*
879 * Since every use (buffer, vnode, swap, blockmap)
880 * holds a reference to the vnode, and because we mark
881 * any other vnodes that alias this device, when the
882 * sum of the reference counts on all the aliased
883 * vnodes descends to one, we are on last close.
884 */
885 if (vcount(vp) > 0)
886 return (0);
887
888 /*
889 * On last close of a block device (that isn't mounted)
890 * we must invalidate any in core blocks, so that
891 * we can, for instance, change floppy disks.
892 */
893 if ((error = spec_fsync_internal(vp, MNT_WAIT, ap->a_context)))
894 return (error);
895
896 error = buf_invalidateblks(vp, BUF_WRITE_DATA, 0, 0);
897 if (error)
898 return (error);
899 #endif /* DEVFS_IMPLEMENTS_LOCKING */
900 devclose = bdevsw[major(dev)].d_close;
901 mode = S_IFBLK;
902 break;
903
904 default:
905 panic("spec_close: not special");
906 return(EBADF);
907 }
908
909 return ((*devclose)(dev, flags, mode, p));
910 }
911
912 /*
913 * Return POSIX pathconf information applicable to special devices.
914 */
915 int
916 spec_pathconf(struct vnop_pathconf_args *ap)
917 {
918
919 switch (ap->a_name) {
920 case _PC_LINK_MAX:
921 *ap->a_retval = LINK_MAX;
922 return (0);
923 case _PC_MAX_CANON:
924 *ap->a_retval = MAX_CANON;
925 return (0);
926 case _PC_MAX_INPUT:
927 *ap->a_retval = MAX_INPUT;
928 return (0);
929 case _PC_PIPE_BUF:
930 *ap->a_retval = PIPE_BUF;
931 return (0);
932 case _PC_CHOWN_RESTRICTED:
933 *ap->a_retval = 200112; /* _POSIX_CHOWN_RESTRICTED */
934 return (0);
935 case _PC_VDISABLE:
936 *ap->a_retval = _POSIX_VDISABLE;
937 return (0);
938 default:
939 return (EINVAL);
940 }
941 /* NOTREACHED */
942 }
943
944 /*
945 * Special device failed operation
946 */
947 int
948 spec_ebadf(__unused void *dummy)
949 {
950
951 return (EBADF);
952 }
953
954 /* Blktooff derives file offset from logical block number */
955 int
956 spec_blktooff(struct vnop_blktooff_args *ap)
957 {
958 struct vnode *vp = ap->a_vp;
959
960 switch (vp->v_type) {
961 case VCHR:
962 *ap->a_offset = (off_t)-1; /* failure */
963 return (ENOTSUP);
964
965 case VBLK:
966 printf("spec_blktooff: not implemented for VBLK\n");
967 *ap->a_offset = (off_t)-1; /* failure */
968 return (ENOTSUP);
969
970 default:
971 panic("spec_blktooff type");
972 }
973 /* NOTREACHED */
974
975 return (0);
976 }
977
978 /* Offtoblk derives logical block number from file offset */
979 int
980 spec_offtoblk(struct vnop_offtoblk_args *ap)
981 {
982 struct vnode *vp = ap->a_vp;
983
984 switch (vp->v_type) {
985 case VCHR:
986 *ap->a_lblkno = (daddr64_t)-1; /* failure */
987 return (ENOTSUP);
988
989 case VBLK:
990 printf("spec_offtoblk: not implemented for VBLK\n");
991 *ap->a_lblkno = (daddr64_t)-1; /* failure */
992 return (ENOTSUP);
993
994 default:
995 panic("spec_offtoblk type");
996 }
997 /* NOTREACHED */
998
999 return (0);
1000 }