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