]> git.saurik.com Git - apple/xnu.git/blob - bsd/miscfs/specfs/spec_vnops.c
43961b3dc9ca504eb98a70ecc3f64442b07bf099
[apple/xnu.git] / bsd / miscfs / specfs / spec_vnops.c
1 /*
2 * Copyright (c) 2000-2002 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.h>
60 #include <sys/systm.h>
61 #include <sys/kernel.h>
62 #include <sys/conf.h>
63 #include <sys/buf.h>
64 #include <sys/mount.h>
65 #include <sys/namei.h>
66 #include <sys/vnode.h>
67 #include <sys/stat.h>
68 #include <sys/errno.h>
69 #include <sys/ioctl.h>
70 #include <sys/file.h>
71 #include <sys/malloc.h>
72 #include <sys/disk.h>
73 #include <miscfs/specfs/specdev.h>
74 #include <vfs/vfs_support.h>
75
76 #include <sys/kdebug.h>
77
78 struct vnode *speclisth[SPECHSZ];
79
80 /* symbolic sleep message strings for devices */
81 char devopn[] = "devopn";
82 char devio[] = "devio";
83 char devwait[] = "devwait";
84 char devin[] = "devin";
85 char devout[] = "devout";
86 char devioc[] = "devioc";
87 char devcls[] = "devcls";
88
89 #define VOPFUNC int (*)(void *)
90
91 int (**spec_vnodeop_p)(void *);
92 struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
93 { &vop_default_desc, (VOPFUNC)vn_default_error },
94 { &vop_lookup_desc, (VOPFUNC)spec_lookup }, /* lookup */
95 { &vop_create_desc, (VOPFUNC)err_create }, /* create */
96 { &vop_mknod_desc, (VOPFUNC)err_mknod }, /* mknod */
97 { &vop_open_desc, (VOPFUNC)spec_open }, /* open */
98 { &vop_close_desc, (VOPFUNC)spec_close }, /* close */
99 { &vop_access_desc, (VOPFUNC)spec_access }, /* access */
100 { &vop_getattr_desc, (VOPFUNC)spec_getattr }, /* getattr */
101 { &vop_setattr_desc, (VOPFUNC)spec_setattr }, /* setattr */
102 { &vop_read_desc, (VOPFUNC)spec_read }, /* read */
103 { &vop_write_desc, (VOPFUNC)spec_write }, /* write */
104 { &vop_lease_desc, (VOPFUNC)nop_lease }, /* lease */
105 { &vop_ioctl_desc, (VOPFUNC)spec_ioctl }, /* ioctl */
106 { &vop_select_desc, (VOPFUNC)spec_select }, /* select */
107 { &vop_revoke_desc, (VOPFUNC)nop_revoke }, /* revoke */
108 { &vop_mmap_desc, (VOPFUNC)err_mmap }, /* mmap */
109 { &vop_fsync_desc, (VOPFUNC)spec_fsync }, /* fsync */
110 { &vop_seek_desc, (VOPFUNC)err_seek }, /* seek */
111 { &vop_remove_desc, (VOPFUNC)err_remove }, /* remove */
112 { &vop_link_desc, (VOPFUNC)err_link }, /* link */
113 { &vop_rename_desc, (VOPFUNC)err_rename }, /* rename */
114 { &vop_mkdir_desc, (VOPFUNC)err_mkdir }, /* mkdir */
115 { &vop_rmdir_desc, (VOPFUNC)err_rmdir }, /* rmdir */
116 { &vop_symlink_desc, (VOPFUNC)err_symlink }, /* symlink */
117 { &vop_readdir_desc, (VOPFUNC)err_readdir }, /* readdir */
118 { &vop_readlink_desc, (VOPFUNC)err_readlink }, /* readlink */
119 { &vop_abortop_desc, (VOPFUNC)err_abortop }, /* abortop */
120 { &vop_inactive_desc, (VOPFUNC)nop_inactive }, /* inactive */
121 { &vop_reclaim_desc, (VOPFUNC)nop_reclaim }, /* reclaim */
122 { &vop_lock_desc, (VOPFUNC)nop_lock }, /* lock */
123 { &vop_unlock_desc, (VOPFUNC)nop_unlock }, /* unlock */
124 { &vop_bmap_desc, (VOPFUNC)spec_bmap }, /* bmap */
125 { &vop_strategy_desc, (VOPFUNC)spec_strategy }, /* strategy */
126 { &vop_print_desc, (VOPFUNC)spec_print }, /* print */
127 { &vop_islocked_desc, (VOPFUNC)nop_islocked }, /* islocked */
128 { &vop_pathconf_desc, (VOPFUNC)spec_pathconf }, /* pathconf */
129 { &vop_advlock_desc, (VOPFUNC)err_advlock }, /* advlock */
130 { &vop_blkatoff_desc, (VOPFUNC)err_blkatoff }, /* blkatoff */
131 { &vop_valloc_desc, (VOPFUNC)err_valloc }, /* valloc */
132 { &vop_vfree_desc, (VOPFUNC)err_vfree }, /* vfree */
133 { &vop_truncate_desc, (VOPFUNC)nop_truncate }, /* truncate */
134 { &vop_update_desc, (VOPFUNC)nop_update }, /* update */
135 { &vop_bwrite_desc, (VOPFUNC)spec_bwrite }, /* bwrite */
136 { &vop_devblocksize_desc, (VOPFUNC)spec_devblocksize }, /* devblocksize */
137 { &vop_pagein_desc, (VOPFUNC)err_pagein }, /* Pagein */
138 { &vop_pageout_desc, (VOPFUNC)err_pageout }, /* Pageout */
139 { &vop_copyfile_desc, (VOPFUNC)err_copyfile }, /* Copyfile */
140 { &vop_blktooff_desc, (VOPFUNC)spec_blktooff }, /* blktooff */
141 { &vop_offtoblk_desc, (VOPFUNC)spec_offtoblk }, /* offtoblk */
142 { &vop_cmap_desc, (VOPFUNC)spec_cmap }, /* cmap */
143 { (struct vnodeop_desc*)NULL, (int(*)())NULL }
144 };
145 struct vnodeopv_desc spec_vnodeop_opv_desc =
146 { &spec_vnodeop_p, spec_vnodeop_entries };
147
148 /*
149 * Trivial lookup routine that always fails.
150 */
151 int
152 spec_lookup(ap)
153 struct vop_lookup_args /* {
154 struct vnode *a_dvp;
155 struct vnode **a_vpp;
156 struct componentname *a_cnp;
157 } */ *ap;
158 {
159
160 *ap->a_vpp = NULL;
161 return (ENOTDIR);
162 }
163
164 void
165 set_blocksize(struct vnode *vp, dev_t dev)
166 {
167 int (*size)();
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 >= nblkdev)
190 return;
191
192 set_blocksize(vp, dev);
193 }
194
195 }
196
197
198 /*
199 * Open a special file.
200 */
201 /* ARGSUSED */
202 spec_open(ap)
203 struct vop_open_args /* {
204 struct vnode *a_vp;
205 int a_mode;
206 struct ucred *a_cred;
207 struct proc *a_p;
208 } */ *ap;
209 {
210 struct proc *p = ap->a_p;
211 struct vnode *bvp, *vp = ap->a_vp;
212 dev_t bdev, dev = (dev_t)vp->v_rdev;
213 int maj = major(dev);
214 int error;
215
216 /*
217 * Don't allow open if fs is mounted -nodev.
218 */
219 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
220 return (ENXIO);
221
222 switch (vp->v_type) {
223
224 case VCHR:
225 if ((u_int)maj >= nchrdev)
226 return (ENXIO);
227 if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
228 /*
229 * When running in very secure mode, do not allow
230 * opens for writing of any disk character devices.
231 */
232 if (securelevel >= 2 && isdisk(dev, VCHR))
233 return (EPERM);
234 /*
235 * When running in secure mode, do not allow opens
236 * for writing of /dev/mem, /dev/kmem, or character
237 * devices whose corresponding block devices are
238 * currently mounted.
239 */
240 if (securelevel >= 1) {
241 if ((bdev = chrtoblk(dev)) != NODEV &&
242 vfinddev(bdev, VBLK, &bvp) &&
243 bvp->v_usecount > 0 &&
244 (error = vfs_mountedon(bvp)))
245 return (error);
246 if (iskmemdev(dev))
247 return (EPERM);
248 }
249 }
250 if (cdevsw[maj].d_type == D_TTY)
251 vp->v_flag |= VISTTY;
252 VOP_UNLOCK(vp, 0, p);
253 error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, p);
254 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
255 return (error);
256
257 case VBLK:
258 if ((u_int)maj >= nblkdev)
259 return (ENXIO);
260 /*
261 * When running in very secure mode, do not allow
262 * opens for writing of any disk block devices.
263 */
264 if (securelevel >= 2 && ap->a_cred != FSCRED &&
265 (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK)
266 return (EPERM);
267 /*
268 * Do not allow opens of block devices that are
269 * currently mounted.
270 */
271 if (error = vfs_mountedon(vp))
272 return (error);
273 error = (*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, p);
274 if (!error) {
275 u_int64_t blkcnt;
276 u_int32_t blksize;
277
278 set_blocksize(vp, dev);
279
280 /*
281 * Cache the size in bytes of the block device for later
282 * use by spec_write().
283 */
284 vp->v_specdevsize = (u_int64_t)0; /* Default: Can't get */
285 if (!VOP_IOCTL(vp, DKIOCGETBLOCKSIZE, (caddr_t)&blksize, 0, NOCRED, p)) {
286 /* Switch to 512 byte sectors (temporarily) */
287 u_int32_t size512 = 512;
288
289 if (!VOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&size512, FWRITE, NOCRED, p)) {
290 /* Get the number of 512 byte physical blocks. */
291 if (!VOP_IOCTL(vp, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt, 0, NOCRED, p)) {
292 vp->v_specdevsize = blkcnt * (u_int64_t)size512;
293 }
294 }
295 /* If it doesn't set back, we can't recover */
296 if (VOP_IOCTL(vp, DKIOCSETBLOCKSIZE, (caddr_t)&blksize, FWRITE, NOCRED, p))
297 error = ENXIO;
298 }
299 }
300 return(error);
301 }
302 return (0);
303 }
304
305 /*
306 * Vnode op for read
307 */
308 /* ARGSUSED */
309 spec_read(ap)
310 struct vop_read_args /* {
311 struct vnode *a_vp;
312 struct uio *a_uio;
313 int a_ioflag;
314 struct ucred *a_cred;
315 } */ *ap;
316 {
317 register struct vnode *vp = ap->a_vp;
318 register struct uio *uio = ap->a_uio;
319 struct proc *p = uio->uio_procp;
320 struct buf *bp;
321 daddr_t bn, nextbn;
322 long bsize, bscale;
323 int devBlockSize=0;
324 int n, on, majordev, (*ioctl)();
325 int error = 0;
326 dev_t dev;
327
328 #if DIAGNOSTIC
329 if (uio->uio_rw != UIO_READ)
330 panic("spec_read mode");
331 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != current_proc())
332 panic("spec_read proc");
333 #endif
334 if (uio->uio_resid == 0)
335 return (0);
336
337 switch (vp->v_type) {
338
339 case VCHR:
340 VOP_UNLOCK(vp, 0, p);
341 error = (*cdevsw[major(vp->v_rdev)].d_read)
342 (vp->v_rdev, uio, ap->a_ioflag);
343 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
344 return (error);
345
346 case VBLK:
347 if (uio->uio_offset < 0)
348 return (EINVAL);
349
350 dev = vp->v_rdev;
351
352 devBlockSize = vp->v_specsize;
353
354 if (devBlockSize > PAGE_SIZE)
355 return (EINVAL);
356
357 bscale = PAGE_SIZE / devBlockSize;
358 bsize = bscale * devBlockSize;
359
360 do {
361 on = uio->uio_offset % bsize;
362
363 bn = (uio->uio_offset / devBlockSize) &~ (bscale - 1);
364
365 if (vp->v_lastr + bscale == bn) {
366 nextbn = bn + bscale;
367 error = breadn(vp, bn, (int)bsize, &nextbn,
368 (int *)&bsize, 1, NOCRED, &bp);
369 } else
370 error = bread(vp, bn, (int)bsize, NOCRED, &bp);
371
372 vp->v_lastr = bn;
373 n = bsize - bp->b_resid;
374 if ((on > n) || error) {
375 if (!error)
376 error = EINVAL;
377 brelse(bp);
378 return (error);
379 }
380 n = min((unsigned)(n - on), uio->uio_resid);
381
382 error = uiomove((char *)bp->b_data + on, n, uio);
383 if (n + on == bsize)
384 bp->b_flags |= B_AGE;
385 brelse(bp);
386 } while (error == 0 && uio->uio_resid > 0 && n != 0);
387 return (error);
388
389 default:
390 panic("spec_read type");
391 }
392 /* NOTREACHED */
393 }
394
395 /*
396 * Vnode op for write
397 */
398 /* ARGSUSED */
399 spec_write(ap)
400 struct vop_write_args /* {
401 struct vnode *a_vp;
402 struct uio *a_uio;
403 int a_ioflag;
404 struct ucred *a_cred;
405 } */ *ap;
406 {
407 register struct vnode *vp = ap->a_vp;
408 register struct uio *uio = ap->a_uio;
409 struct proc *p = uio->uio_procp;
410 struct buf *bp;
411 daddr_t bn;
412 int bsize, blkmask, bscale;
413 register int io_sync;
414 register int io_size;
415 int devBlockSize=0;
416 register int n, on;
417 int error = 0;
418 dev_t dev;
419
420 #if DIAGNOSTIC
421 if (uio->uio_rw != UIO_WRITE)
422 panic("spec_write mode");
423 if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != current_proc())
424 panic("spec_write proc");
425 #endif
426
427 switch (vp->v_type) {
428
429 case VCHR:
430 VOP_UNLOCK(vp, 0, p);
431 error = (*cdevsw[major(vp->v_rdev)].d_write)
432 (vp->v_rdev, uio, ap->a_ioflag);
433 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
434 return (error);
435
436 case VBLK:
437 if (uio->uio_resid == 0)
438 return (0);
439 if (uio->uio_offset < 0)
440 return (EINVAL);
441
442 io_sync = (ap->a_ioflag & IO_SYNC);
443 io_size = uio->uio_resid;
444
445 dev = (vp->v_rdev);
446
447 devBlockSize = vp->v_specsize;
448 if (devBlockSize > PAGE_SIZE)
449 return(EINVAL);
450
451 bscale = PAGE_SIZE / devBlockSize;
452 blkmask = bscale - 1;
453 bsize = bscale * devBlockSize;
454
455
456 do {
457 bn = (uio->uio_offset / devBlockSize) &~ blkmask;
458 on = uio->uio_offset % bsize;
459
460 n = min((unsigned)(bsize - on), uio->uio_resid);
461
462 /*
463 * Use getblk() as an optimization IFF:
464 *
465 * 1) We are reading exactly a block on a block
466 * aligned boundary
467 * 2) We know the size of the device from spec_open
468 * 3) The read doesn't span the end of the device
469 *
470 * Otherwise, we fall back on bread().
471 */
472 if (n == bsize &&
473 vp->v_specdevsize != (u_int64_t)0 &&
474 (uio->uio_offset + (u_int64_t)n) > vp->v_specdevsize) {
475 /* reduce the size of the read to what is there */
476 n = (uio->uio_offset + (u_int64_t)n) - vp->v_specdevsize;
477 }
478
479 if (n == bsize)
480 bp = getblk(vp, bn, bsize, 0, 0, BLK_WRITE);
481 else
482 error = bread(vp, bn, bsize, NOCRED, &bp);
483
484 /* Translate downstream error for upstream, if needed */
485 if (!error) {
486 error = bp->b_error;
487 if (!error && (bp->b_flags & B_ERROR) != 0) {
488 error = EIO;
489 }
490 }
491 if (error) {
492 brelse(bp);
493 return (error);
494 }
495 n = min(n, bsize - bp->b_resid);
496
497 error = uiomove((char *)bp->b_data + on, n, uio);
498
499 bp->b_flags |= B_AGE;
500
501 if (io_sync)
502 bwrite(bp);
503 else {
504 if ((n + on) == bsize)
505 bawrite(bp);
506 else
507 bdwrite(bp);
508 }
509 } while (error == 0 && uio->uio_resid > 0 && n != 0);
510 return (error);
511
512 default:
513 panic("spec_write type");
514 }
515 /* NOTREACHED */
516 }
517
518 /*
519 * Device ioctl operation.
520 */
521 /* ARGSUSED */
522 spec_ioctl(ap)
523 struct vop_ioctl_args /* {
524 struct vnode *a_vp;
525 int a_command;
526 caddr_t a_data;
527 int a_fflag;
528 struct ucred *a_cred;
529 struct proc *a_p;
530 } */ *ap;
531 {
532 dev_t dev = ap->a_vp->v_rdev;
533
534 switch (ap->a_vp->v_type) {
535
536 case VCHR:
537 return ((*cdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data,
538 ap->a_fflag, ap->a_p));
539
540 case VBLK:
541 if (ap->a_command == 0 && (int)ap->a_data == B_TAPE)
542 if (bdevsw[major(dev)].d_type == D_TAPE)
543 return (0);
544 else
545 return (1);
546 return ((*bdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data,
547 ap->a_fflag, ap->a_p));
548
549 default:
550 panic("spec_ioctl");
551 /* NOTREACHED */
552 }
553 }
554
555 /* ARGSUSED */
556 spec_select(ap)
557 struct vop_select_args /* {
558 struct vnode *a_vp;
559 int a_which;
560 int a_fflags;
561 struct ucred *a_cred;
562 void * a_wql;
563 struct proc *a_p;
564 } */ *ap;
565 {
566 register dev_t dev;
567
568 switch (ap->a_vp->v_type) {
569
570 default:
571 return (1); /* XXX */
572
573 case VCHR:
574 dev = ap->a_vp->v_rdev;
575 return (*cdevsw[major(dev)].d_select)(dev, ap->a_which, ap->a_wql, ap->a_p);
576 }
577 }
578 /*
579 * Synch buffers associated with a block device
580 */
581 /* ARGSUSED */
582 int
583 spec_fsync(ap)
584 struct vop_fsync_args /* {
585 struct vnode *a_vp;
586 struct ucred *a_cred;
587 int a_waitfor;
588 struct proc *a_p;
589 } */ *ap;
590 {
591 register struct vnode *vp = ap->a_vp;
592 register struct buf *bp;
593 struct buf *nbp;
594 int s;
595
596 if (vp->v_type == VCHR)
597 return (0);
598 /*
599 * Flush all dirty buffers associated with a block device.
600 */
601 loop:
602 s = splbio();
603 for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
604 nbp = bp->b_vnbufs.le_next;
605 // XXXdbg - don't flush locked blocks. they may be journaled.
606 if ((bp->b_flags & B_BUSY) || (bp->b_flags & B_LOCKED))
607 continue;
608 if ((bp->b_flags & B_DELWRI) == 0)
609 panic("spec_fsync: not dirty");
610 bremfree(bp);
611 bp->b_flags |= B_BUSY;
612 splx(s);
613 bawrite(bp);
614 goto loop;
615 }
616 if (ap->a_waitfor == MNT_WAIT) {
617 while (vp->v_numoutput) {
618 vp->v_flag |= VBWAIT;
619 tsleep((caddr_t)&vp->v_numoutput, PRIBIO + 1, "spec_fsync", 0);
620 }
621 #if DIAGNOSTIC
622 if (vp->v_dirtyblkhd.lh_first) {
623 vprint("spec_fsync: dirty", vp);
624 splx(s);
625 goto loop;
626 }
627 #endif
628 }
629 splx(s);
630 return (0);
631 }
632
633 /*
634 * Just call the device strategy routine
635 */
636 spec_strategy(ap)
637 struct vop_strategy_args /* {
638 struct buf *a_bp;
639 } */ *ap;
640 {
641 struct buf *bp;
642 extern int hard_throttle_on_root;
643
644 bp = ap->a_bp;
645
646 if (kdebug_enable) {
647 int code = 0;
648
649 if (bp->b_flags & B_READ)
650 code |= DKIO_READ;
651 if (bp->b_flags & B_ASYNC)
652 code |= DKIO_ASYNC;
653
654 if (bp->b_flags & B_META)
655 code |= DKIO_META;
656 else if (bp->b_flags & (B_PGIN | B_PAGEOUT))
657 code |= DKIO_PAGING;
658
659 KERNEL_DEBUG_CONSTANT(FSDBG_CODE(DBG_DKRW, code) | DBG_FUNC_NONE,
660 (unsigned int)bp, bp->b_dev, bp->b_blkno, bp->b_bcount, 0);
661 }
662 if ((bp->b_flags & B_PGIN) && (bp->b_vp->v_mount->mnt_kern_flag & MNTK_ROOTDEV))
663 hard_throttle_on_root = 1;
664
665 (*bdevsw[major(bp->b_dev)].d_strategy)(bp);
666 return (0);
667 }
668
669 /*
670 * This is a noop, simply returning what one has been given.
671 */
672 spec_bmap(ap)
673 struct vop_bmap_args /* {
674 struct vnode *a_vp;
675 daddr_t a_bn;
676 struct vnode **a_vpp;
677 daddr_t *a_bnp;
678 int *a_runp;
679 } */ *ap;
680 {
681
682 if (ap->a_vpp != NULL)
683 *ap->a_vpp = ap->a_vp;
684 if (ap->a_bnp != NULL)
685 *ap->a_bnp = ap->a_bn * (PAGE_SIZE / ap->a_vp->v_specsize);
686 if (ap->a_runp != NULL)
687 *ap->a_runp = (MAXPHYSIO / PAGE_SIZE) - 1;
688 return (0);
689 }
690
691 /*
692 * This is a noop, simply returning what one has been given.
693 */
694 spec_cmap(ap)
695 struct vop_cmap_args /* {
696 struct vnode *a_vp;
697 off_t a_offset;
698 size_t a_size;
699 daddr_t *a_bpn;
700 size_t *a_run;
701 void *a_poff;
702 } */ *ap;
703 {
704 return (EOPNOTSUPP);
705 }
706
707
708 /*
709 * Device close routine
710 */
711 /* ARGSUSED */
712 spec_close(ap)
713 struct vop_close_args /* {
714 struct vnode *a_vp;
715 int a_fflag;
716 struct ucred *a_cred;
717 struct proc *a_p;
718 } */ *ap;
719 {
720 register struct vnode *vp = ap->a_vp;
721 dev_t dev = vp->v_rdev;
722 int (*devclose) __P((dev_t, int, int, struct proc *));
723 int mode, error;
724
725 switch (vp->v_type) {
726
727 case VCHR:
728 /*
729 * Hack: a tty device that is a controlling terminal
730 * has a reference from the session structure.
731 * We cannot easily tell that a character device is
732 * a controlling terminal, unless it is the closing
733 * process' controlling terminal. In that case,
734 * if the reference count is 2 (this last descriptor
735 * plus the session), release the reference from the session.
736 */
737 if (vcount(vp) == 2 && ap->a_p &&
738 vp == ap->a_p->p_session->s_ttyvp) {
739 ap->a_p->p_session->s_ttyvp = NULL;
740 vrele(vp);
741 }
742 /*
743 * If the vnode is locked, then we are in the midst
744 * of forcably closing the device, otherwise we only
745 * close on last reference.
746 */
747 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
748 return (0);
749 devclose = cdevsw[major(dev)].d_close;
750 mode = S_IFCHR;
751 break;
752
753 case VBLK:
754 #ifdef DEVFS_IMPLEMENTS_LOCKING
755 /*
756 * On last close of a block device (that isn't mounted)
757 * we must invalidate any in core blocks, so that
758 * we can, for instance, change floppy disks.
759 */
760 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, ap->a_p);
761 error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
762 VOP_UNLOCK(vp, 0, ap->a_p);
763 if (error)
764 return (error);
765 /*
766 * We do not want to really close the device if it
767 * is still in use unless we are trying to close it
768 * forcibly. Since every use (buffer, vnode, swap, cmap)
769 * holds a reference to the vnode, and because we mark
770 * any other vnodes that alias this device, when the
771 * sum of the reference counts on all the aliased
772 * vnodes descends to one, we are on last close.
773 */
774 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
775 return (0);
776 #else /* DEVFS_IMPLEMENTS_LOCKING */
777 /*
778 * We do not want to really close the device if it
779 * is still in use unless we are trying to close it
780 * forcibly. Since every use (buffer, vnode, swap, cmap)
781 * holds a reference to the vnode, and because we mark
782 * any other vnodes that alias this device, when the
783 * sum of the reference counts on all the aliased
784 * vnodes descends to one, we are on last close.
785 */
786 if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
787 return (0);
788
789 /*
790 * On last close of a block device (that isn't mounted)
791 * we must invalidate any in core blocks, so that
792 * we can, for instance, change floppy disks.
793 */
794 error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
795 if (error)
796 return (error);
797 #endif /* DEVFS_IMPLEMENTS_LOCKING */
798 devclose = bdevsw[major(dev)].d_close;
799 mode = S_IFBLK;
800 break;
801
802 default:
803 panic("spec_close: not special");
804 }
805
806 return ((*devclose)(dev, ap->a_fflag, mode, ap->a_p));
807 }
808
809 /*
810 * Print out the contents of a special device vnode.
811 */
812 spec_print(ap)
813 struct vop_print_args /* {
814 struct vnode *a_vp;
815 } */ *ap;
816 {
817
818 printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
819 minor(ap->a_vp->v_rdev));
820 }
821
822 /*
823 * Return POSIX pathconf information applicable to special devices.
824 */
825 spec_pathconf(ap)
826 struct vop_pathconf_args /* {
827 struct vnode *a_vp;
828 int a_name;
829 int *a_retval;
830 } */ *ap;
831 {
832
833 switch (ap->a_name) {
834 case _PC_LINK_MAX:
835 *ap->a_retval = LINK_MAX;
836 return (0);
837 case _PC_MAX_CANON:
838 *ap->a_retval = MAX_CANON;
839 return (0);
840 case _PC_MAX_INPUT:
841 *ap->a_retval = MAX_INPUT;
842 return (0);
843 case _PC_PIPE_BUF:
844 *ap->a_retval = PIPE_BUF;
845 return (0);
846 case _PC_CHOWN_RESTRICTED:
847 *ap->a_retval = 1;
848 return (0);
849 case _PC_VDISABLE:
850 *ap->a_retval = _POSIX_VDISABLE;
851 return (0);
852 default:
853 return (EINVAL);
854 }
855 /* NOTREACHED */
856 }
857
858 int
859 spec_devblocksize(ap)
860 struct vop_devblocksize_args /* {
861 struct vnode *a_vp;
862 int *a_retval;
863 } */ *ap;
864 {
865 *ap->a_retval = (ap->a_vp->v_specsize);
866 return (0);
867 }
868
869 /*
870 * Special device failed operation
871 */
872 spec_ebadf()
873 {
874
875 return (EBADF);
876 }
877
878 /*
879 * Special device bad operation
880 */
881 spec_badop()
882 {
883
884 panic("spec_badop called");
885 /* NOTREACHED */
886 }
887
888 /* Blktooff derives file offset from logical block number */
889 int
890 spec_blktooff(ap)
891 struct vop_blktooff_args /* {
892 struct vnode *a_vp;
893 daddr_t a_lblkno;
894 off_t *a_offset;
895 } */ *ap;
896 {
897 register struct vnode *vp = ap->a_vp;
898
899 switch (vp->v_type) {
900 case VCHR:
901 *ap->a_offset = (off_t)-1; /* failure */
902 return (EOPNOTSUPP);
903
904 case VBLK:
905 printf("spec_blktooff: not implemented for VBLK\n");
906 *ap->a_offset = (off_t)-1; /* failure */
907 return (EOPNOTSUPP);
908
909 default:
910 panic("spec_blktooff type");
911 }
912 /* NOTREACHED */
913 }
914
915 /* Offtoblk derives logical block number from file offset */
916 int
917 spec_offtoblk(ap)
918 struct vop_offtoblk_args /* {
919 struct vnode *a_vp;
920 off_t a_offset;
921 daddr_t *a_lblkno;
922 } */ *ap;
923 {
924 register struct vnode *vp = ap->a_vp;
925
926 switch (vp->v_type) {
927 case VCHR:
928 *ap->a_lblkno = (daddr_t)-1; /* failure */
929 return (EOPNOTSUPP);
930
931 case VBLK:
932 printf("spec_offtoblk: not implemented for VBLK\n");
933 *ap->a_lblkno = (daddr_t)-1; /* failure */
934 return (EOPNOTSUPP);
935
936 default:
937 panic("spec_offtoblk type");
938 }
939 /* NOTREACHED */
940 }