spec_vnops.c revision 1.60 1 1.60 lukem /* $NetBSD: spec_vnops.c,v 1.60 2001/11/10 13:33:44 lukem Exp $ */
2 1.16 cgd
3 1.1 cgd /*
4 1.15 mycroft * Copyright (c) 1989, 1993
5 1.15 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.39 fvdl * @(#)spec_vnops.c 8.15 (Berkeley) 7/14/95
36 1.1 cgd */
37 1.60 lukem
38 1.60 lukem #include <sys/cdefs.h>
39 1.60 lukem __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.60 2001/11/10 13:33:44 lukem Exp $");
40 1.1 cgd
41 1.9 mycroft #include <sys/param.h>
42 1.9 mycroft #include <sys/proc.h>
43 1.9 mycroft #include <sys/systm.h>
44 1.9 mycroft #include <sys/kernel.h>
45 1.9 mycroft #include <sys/conf.h>
46 1.9 mycroft #include <sys/buf.h>
47 1.9 mycroft #include <sys/mount.h>
48 1.9 mycroft #include <sys/namei.h>
49 1.9 mycroft #include <sys/vnode.h>
50 1.9 mycroft #include <sys/stat.h>
51 1.9 mycroft #include <sys/errno.h>
52 1.9 mycroft #include <sys/ioctl.h>
53 1.9 mycroft #include <sys/file.h>
54 1.9 mycroft #include <sys/disklabel.h>
55 1.35 kleink #include <sys/lockf.h>
56 1.28 christos
57 1.30 mycroft #include <miscfs/genfs/genfs.h>
58 1.15 mycroft #include <miscfs/specfs/specdev.h>
59 1.1 cgd
60 1.1 cgd /* symbolic sleep message strings for devices */
61 1.37 mycroft const char devopn[] = "devopn";
62 1.37 mycroft const char devio[] = "devio";
63 1.37 mycroft const char devwait[] = "devwait";
64 1.37 mycroft const char devin[] = "devin";
65 1.37 mycroft const char devout[] = "devout";
66 1.37 mycroft const char devioc[] = "devioc";
67 1.37 mycroft const char devcls[] = "devcls";
68 1.46 sommerfe
69 1.46 sommerfe /*
70 1.46 sommerfe * This vnode operations vector is used for two things only:
71 1.46 sommerfe * - special device nodes created from whole cloth by the kernel.
72 1.46 sommerfe * - as a temporary vnodeops replacement for vnodes which were found to
73 1.46 sommerfe * be aliased by callers of checkalias().
74 1.46 sommerfe * For the ops vector for vnodes built from special devices found in a
75 1.46 sommerfe * filesystem, see (e.g) ffs_specop_entries[] in ffs_vnops.c or the
76 1.46 sommerfe * equivalent for other filesystems.
77 1.46 sommerfe */
78 1.1 cgd
79 1.28 christos int (**spec_vnodeop_p) __P((void *));
80 1.53 jdolecek const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
81 1.15 mycroft { &vop_default_desc, vn_default_error },
82 1.15 mycroft { &vop_lookup_desc, spec_lookup }, /* lookup */
83 1.15 mycroft { &vop_create_desc, spec_create }, /* create */
84 1.15 mycroft { &vop_mknod_desc, spec_mknod }, /* mknod */
85 1.15 mycroft { &vop_open_desc, spec_open }, /* open */
86 1.15 mycroft { &vop_close_desc, spec_close }, /* close */
87 1.15 mycroft { &vop_access_desc, spec_access }, /* access */
88 1.15 mycroft { &vop_getattr_desc, spec_getattr }, /* getattr */
89 1.15 mycroft { &vop_setattr_desc, spec_setattr }, /* setattr */
90 1.15 mycroft { &vop_read_desc, spec_read }, /* read */
91 1.15 mycroft { &vop_write_desc, spec_write }, /* write */
92 1.21 mycroft { &vop_lease_desc, spec_lease_check }, /* lease */
93 1.47 sommerfe { &vop_fcntl_desc, spec_fcntl }, /* fcntl */
94 1.15 mycroft { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
95 1.32 mycroft { &vop_poll_desc, spec_poll }, /* poll */
96 1.39 fvdl { &vop_revoke_desc, spec_revoke }, /* revoke */
97 1.15 mycroft { &vop_mmap_desc, spec_mmap }, /* mmap */
98 1.15 mycroft { &vop_fsync_desc, spec_fsync }, /* fsync */
99 1.15 mycroft { &vop_seek_desc, spec_seek }, /* seek */
100 1.15 mycroft { &vop_remove_desc, spec_remove }, /* remove */
101 1.15 mycroft { &vop_link_desc, spec_link }, /* link */
102 1.15 mycroft { &vop_rename_desc, spec_rename }, /* rename */
103 1.15 mycroft { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
104 1.15 mycroft { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
105 1.15 mycroft { &vop_symlink_desc, spec_symlink }, /* symlink */
106 1.15 mycroft { &vop_readdir_desc, spec_readdir }, /* readdir */
107 1.15 mycroft { &vop_readlink_desc, spec_readlink }, /* readlink */
108 1.15 mycroft { &vop_abortop_desc, spec_abortop }, /* abortop */
109 1.15 mycroft { &vop_inactive_desc, spec_inactive }, /* inactive */
110 1.15 mycroft { &vop_reclaim_desc, spec_reclaim }, /* reclaim */
111 1.15 mycroft { &vop_lock_desc, spec_lock }, /* lock */
112 1.15 mycroft { &vop_unlock_desc, spec_unlock }, /* unlock */
113 1.15 mycroft { &vop_bmap_desc, spec_bmap }, /* bmap */
114 1.15 mycroft { &vop_strategy_desc, spec_strategy }, /* strategy */
115 1.15 mycroft { &vop_print_desc, spec_print }, /* print */
116 1.15 mycroft { &vop_islocked_desc, spec_islocked }, /* islocked */
117 1.15 mycroft { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
118 1.15 mycroft { &vop_advlock_desc, spec_advlock }, /* advlock */
119 1.15 mycroft { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
120 1.15 mycroft { &vop_valloc_desc, spec_valloc }, /* valloc */
121 1.15 mycroft { &vop_vfree_desc, spec_vfree }, /* vfree */
122 1.15 mycroft { &vop_truncate_desc, spec_truncate }, /* truncate */
123 1.15 mycroft { &vop_update_desc, spec_update }, /* update */
124 1.28 christos { &vop_bwrite_desc, spec_bwrite }, /* bwrite */
125 1.55 chs { &vop_getpages_desc, spec_getpages }, /* getpages */
126 1.55 chs { &vop_putpages_desc, spec_putpages }, /* putpages */
127 1.55 chs { NULL, NULL }
128 1.1 cgd };
129 1.53 jdolecek const struct vnodeopv_desc spec_vnodeop_opv_desc =
130 1.15 mycroft { &spec_vnodeop_p, spec_vnodeop_entries };
131 1.1 cgd
132 1.1 cgd /*
133 1.1 cgd * Trivial lookup routine that always fails.
134 1.1 cgd */
135 1.4 andrew int
136 1.28 christos spec_lookup(v)
137 1.28 christos void *v;
138 1.28 christos {
139 1.15 mycroft struct vop_lookup_args /* {
140 1.15 mycroft struct vnode *a_dvp;
141 1.15 mycroft struct vnode **a_vpp;
142 1.15 mycroft struct componentname *a_cnp;
143 1.28 christos } */ *ap = v;
144 1.1 cgd
145 1.15 mycroft *ap->a_vpp = NULL;
146 1.1 cgd return (ENOTDIR);
147 1.1 cgd }
148 1.1 cgd
149 1.1 cgd /*
150 1.15 mycroft * Open a special file.
151 1.1 cgd */
152 1.1 cgd /* ARGSUSED */
153 1.28 christos int
154 1.28 christos spec_open(v)
155 1.28 christos void *v;
156 1.28 christos {
157 1.15 mycroft struct vop_open_args /* {
158 1.15 mycroft struct vnode *a_vp;
159 1.15 mycroft int a_mode;
160 1.15 mycroft struct ucred *a_cred;
161 1.15 mycroft struct proc *a_p;
162 1.28 christos } */ *ap = v;
163 1.39 fvdl struct proc *p = ap->a_p;
164 1.15 mycroft struct vnode *bvp, *vp = ap->a_vp;
165 1.15 mycroft dev_t bdev, dev = (dev_t)vp->v_rdev;
166 1.48 augustss int maj = major(dev);
167 1.1 cgd int error;
168 1.55 chs struct partinfo pi;
169 1.1 cgd
170 1.15 mycroft /*
171 1.15 mycroft * Don't allow open if fs is mounted -nodev.
172 1.15 mycroft */
173 1.1 cgd if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
174 1.1 cgd return (ENXIO);
175 1.1 cgd
176 1.1 cgd switch (vp->v_type) {
177 1.1 cgd
178 1.1 cgd case VCHR:
179 1.1 cgd if ((u_int)maj >= nchrdev)
180 1.1 cgd return (ENXIO);
181 1.15 mycroft if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
182 1.15 mycroft /*
183 1.15 mycroft * When running in very secure mode, do not allow
184 1.15 mycroft * opens for writing of any disk character devices.
185 1.15 mycroft */
186 1.23 mycroft if (securelevel >= 2 && cdevsw[maj].d_type == D_DISK)
187 1.15 mycroft return (EPERM);
188 1.15 mycroft /*
189 1.15 mycroft * When running in secure mode, do not allow opens
190 1.15 mycroft * for writing of /dev/mem, /dev/kmem, or character
191 1.15 mycroft * devices whose corresponding block devices are
192 1.15 mycroft * currently mounted.
193 1.15 mycroft */
194 1.15 mycroft if (securelevel >= 1) {
195 1.38 christos if ((bdev = chrtoblk(dev)) != (dev_t)NODEV &&
196 1.15 mycroft vfinddev(bdev, VBLK, &bvp) &&
197 1.15 mycroft (error = vfs_mountedon(bvp)))
198 1.15 mycroft return (error);
199 1.15 mycroft if (iskmemdev(dev))
200 1.15 mycroft return (EPERM);
201 1.15 mycroft }
202 1.15 mycroft }
203 1.23 mycroft if (cdevsw[maj].d_type == D_TTY)
204 1.23 mycroft vp->v_flag |= VISTTY;
205 1.39 fvdl VOP_UNLOCK(vp, 0);
206 1.39 fvdl error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, p);
207 1.39 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
208 1.1 cgd return (error);
209 1.1 cgd
210 1.1 cgd case VBLK:
211 1.1 cgd if ((u_int)maj >= nblkdev)
212 1.1 cgd return (ENXIO);
213 1.15 mycroft /*
214 1.15 mycroft * When running in very secure mode, do not allow
215 1.15 mycroft * opens for writing of any disk block devices.
216 1.15 mycroft */
217 1.15 mycroft if (securelevel >= 2 && ap->a_cred != FSCRED &&
218 1.23 mycroft (ap->a_mode & FWRITE) && bdevsw[maj].d_type == D_DISK)
219 1.15 mycroft return (EPERM);
220 1.15 mycroft /*
221 1.15 mycroft * Do not allow opens of block devices that are
222 1.15 mycroft * currently mounted.
223 1.15 mycroft */
224 1.28 christos if ((error = vfs_mountedon(vp)) != 0)
225 1.1 cgd return (error);
226 1.55 chs error = (*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, p);
227 1.55 chs if (error) {
228 1.55 chs return error;
229 1.55 chs }
230 1.55 chs error = (*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev,
231 1.55 chs DIOCGPART, (caddr_t)&pi, FREAD, curproc);
232 1.55 chs if (error == 0) {
233 1.57 chs vp->v_size = (voff_t)pi.disklab->d_secsize *
234 1.55 chs pi.part->p_size;
235 1.55 chs }
236 1.55 chs return 0;
237 1.55 chs
238 1.28 christos case VNON:
239 1.28 christos case VLNK:
240 1.28 christos case VDIR:
241 1.28 christos case VREG:
242 1.28 christos case VBAD:
243 1.28 christos case VFIFO:
244 1.28 christos case VSOCK:
245 1.28 christos break;
246 1.1 cgd }
247 1.15 mycroft return (0);
248 1.1 cgd }
249 1.1 cgd
250 1.1 cgd /*
251 1.1 cgd * Vnode op for read
252 1.1 cgd */
253 1.1 cgd /* ARGSUSED */
254 1.28 christos int
255 1.28 christos spec_read(v)
256 1.28 christos void *v;
257 1.28 christos {
258 1.15 mycroft struct vop_read_args /* {
259 1.15 mycroft struct vnode *a_vp;
260 1.15 mycroft struct uio *a_uio;
261 1.15 mycroft int a_ioflag;
262 1.15 mycroft struct ucred *a_cred;
263 1.28 christos } */ *ap = v;
264 1.48 augustss struct vnode *vp = ap->a_vp;
265 1.48 augustss struct uio *uio = ap->a_uio;
266 1.56 chs struct proc *p = uio->uio_procp;
267 1.56 chs struct buf *bp;
268 1.57 chs daddr_t bn;
269 1.59 chs int bsize, bscale;
270 1.56 chs struct partinfo dpart;
271 1.56 chs int n, on, majordev;
272 1.56 chs int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
273 1.1 cgd int error = 0;
274 1.1 cgd
275 1.1 cgd #ifdef DIAGNOSTIC
276 1.1 cgd if (uio->uio_rw != UIO_READ)
277 1.1 cgd panic("spec_read mode");
278 1.1 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
279 1.1 cgd panic("spec_read proc");
280 1.1 cgd #endif
281 1.1 cgd if (uio->uio_resid == 0)
282 1.1 cgd return (0);
283 1.1 cgd
284 1.56 chs switch (vp->v_type) {
285 1.56 chs
286 1.56 chs case VCHR:
287 1.39 fvdl VOP_UNLOCK(vp, 0);
288 1.1 cgd error = (*cdevsw[major(vp->v_rdev)].d_read)
289 1.15 mycroft (vp->v_rdev, uio, ap->a_ioflag);
290 1.58 chs vn_lock(vp, LK_SHARED | LK_RETRY);
291 1.1 cgd return (error);
292 1.1 cgd
293 1.56 chs case VBLK:
294 1.56 chs if (uio->uio_offset < 0)
295 1.56 chs return (EINVAL);
296 1.56 chs bsize = BLKDEV_IOSIZE;
297 1.56 chs if ((majordev = major(vp->v_rdev)) < nblkdev &&
298 1.56 chs (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
299 1.56 chs (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
300 1.56 chs if (dpart.part->p_fstype == FS_BSDFFS &&
301 1.56 chs dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
302 1.56 chs bsize = dpart.part->p_frag *
303 1.56 chs dpart.part->p_fsize;
304 1.56 chs }
305 1.59 chs bscale = bsize >> DEV_BSHIFT;
306 1.56 chs do {
307 1.59 chs bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
308 1.56 chs on = uio->uio_offset % bsize;
309 1.56 chs n = min((unsigned)(bsize - on), uio->uio_resid);
310 1.57 chs error = bread(vp, bn, bsize, NOCRED, &bp);
311 1.56 chs n = min(n, bsize - bp->b_resid);
312 1.56 chs if (error) {
313 1.56 chs brelse(bp);
314 1.56 chs return (error);
315 1.56 chs }
316 1.56 chs error = uiomove((char *)bp->b_data + on, n, uio);
317 1.56 chs brelse(bp);
318 1.56 chs } while (error == 0 && uio->uio_resid > 0 && n != 0);
319 1.56 chs return (error);
320 1.56 chs
321 1.56 chs default:
322 1.56 chs panic("spec_read type");
323 1.1 cgd }
324 1.56 chs /* NOTREACHED */
325 1.1 cgd }
326 1.1 cgd
327 1.1 cgd /*
328 1.1 cgd * Vnode op for write
329 1.1 cgd */
330 1.1 cgd /* ARGSUSED */
331 1.28 christos int
332 1.28 christos spec_write(v)
333 1.28 christos void *v;
334 1.28 christos {
335 1.15 mycroft struct vop_write_args /* {
336 1.15 mycroft struct vnode *a_vp;
337 1.15 mycroft struct uio *a_uio;
338 1.15 mycroft int a_ioflag;
339 1.15 mycroft struct ucred *a_cred;
340 1.28 christos } */ *ap = v;
341 1.48 augustss struct vnode *vp = ap->a_vp;
342 1.48 augustss struct uio *uio = ap->a_uio;
343 1.56 chs struct proc *p = uio->uio_procp;
344 1.56 chs struct buf *bp;
345 1.56 chs daddr_t bn;
346 1.59 chs int bsize, bscale;
347 1.56 chs struct partinfo dpart;
348 1.56 chs int n, on, majordev;
349 1.56 chs int (*ioctl) __P((dev_t, u_long, caddr_t, int, struct proc *));
350 1.1 cgd int error = 0;
351 1.1 cgd
352 1.1 cgd #ifdef DIAGNOSTIC
353 1.1 cgd if (uio->uio_rw != UIO_WRITE)
354 1.1 cgd panic("spec_write mode");
355 1.1 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
356 1.1 cgd panic("spec_write proc");
357 1.1 cgd #endif
358 1.1 cgd
359 1.56 chs switch (vp->v_type) {
360 1.56 chs
361 1.56 chs case VCHR:
362 1.39 fvdl VOP_UNLOCK(vp, 0);
363 1.1 cgd error = (*cdevsw[major(vp->v_rdev)].d_write)
364 1.15 mycroft (vp->v_rdev, uio, ap->a_ioflag);
365 1.39 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
366 1.1 cgd return (error);
367 1.56 chs
368 1.56 chs case VBLK:
369 1.56 chs if (uio->uio_resid == 0)
370 1.56 chs return (0);
371 1.56 chs if (uio->uio_offset < 0)
372 1.56 chs return (EINVAL);
373 1.56 chs bsize = BLKDEV_IOSIZE;
374 1.56 chs if ((majordev = major(vp->v_rdev)) < nblkdev &&
375 1.56 chs (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
376 1.56 chs (*ioctl)(vp->v_rdev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0) {
377 1.56 chs if (dpart.part->p_fstype == FS_BSDFFS &&
378 1.56 chs dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
379 1.56 chs bsize = dpart.part->p_frag *
380 1.56 chs dpart.part->p_fsize;
381 1.56 chs }
382 1.59 chs bscale = bsize >> DEV_BSHIFT;
383 1.56 chs do {
384 1.59 chs bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
385 1.56 chs on = uio->uio_offset % bsize;
386 1.56 chs n = min((unsigned)(bsize - on), uio->uio_resid);
387 1.56 chs if (n == bsize)
388 1.56 chs bp = getblk(vp, bn, bsize, 0, 0);
389 1.56 chs else
390 1.56 chs error = bread(vp, bn, bsize, NOCRED, &bp);
391 1.56 chs if (error) {
392 1.56 chs brelse(bp);
393 1.56 chs return (error);
394 1.56 chs }
395 1.56 chs n = min(n, bsize - bp->b_resid);
396 1.56 chs error = uiomove((char *)bp->b_data + on, n, uio);
397 1.56 chs if (error)
398 1.56 chs brelse(bp);
399 1.56 chs else {
400 1.56 chs if (n + on == bsize)
401 1.56 chs bawrite(bp);
402 1.56 chs else
403 1.56 chs bdwrite(bp);
404 1.56 chs if (bp->b_flags & B_ERROR)
405 1.56 chs error = bp->b_error;
406 1.56 chs }
407 1.56 chs } while (error == 0 && uio->uio_resid > 0 && n != 0);
408 1.56 chs return (error);
409 1.56 chs
410 1.56 chs default:
411 1.56 chs panic("spec_write type");
412 1.55 chs }
413 1.56 chs /* NOTREACHED */
414 1.1 cgd }
415 1.1 cgd
416 1.1 cgd /*
417 1.1 cgd * Device ioctl operation.
418 1.1 cgd */
419 1.1 cgd /* ARGSUSED */
420 1.28 christos int
421 1.28 christos spec_ioctl(v)
422 1.28 christos void *v;
423 1.28 christos {
424 1.15 mycroft struct vop_ioctl_args /* {
425 1.15 mycroft struct vnode *a_vp;
426 1.19 cgd u_long a_command;
427 1.15 mycroft caddr_t a_data;
428 1.15 mycroft int a_fflag;
429 1.15 mycroft struct ucred *a_cred;
430 1.15 mycroft struct proc *a_p;
431 1.28 christos } */ *ap = v;
432 1.15 mycroft dev_t dev = ap->a_vp->v_rdev;
433 1.23 mycroft int maj = major(dev);
434 1.1 cgd
435 1.15 mycroft switch (ap->a_vp->v_type) {
436 1.1 cgd
437 1.1 cgd case VCHR:
438 1.23 mycroft return ((*cdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
439 1.15 mycroft ap->a_fflag, ap->a_p));
440 1.1 cgd
441 1.1 cgd case VBLK:
442 1.42 thorpej if (ap->a_command == 0 && (long)ap->a_data == B_TAPE) {
443 1.23 mycroft if (bdevsw[maj].d_type == D_TAPE)
444 1.1 cgd return (0);
445 1.1 cgd else
446 1.1 cgd return (1);
447 1.42 thorpej }
448 1.23 mycroft return ((*bdevsw[maj].d_ioctl)(dev, ap->a_command, ap->a_data,
449 1.15 mycroft ap->a_fflag, ap->a_p));
450 1.1 cgd
451 1.1 cgd default:
452 1.1 cgd panic("spec_ioctl");
453 1.1 cgd /* NOTREACHED */
454 1.1 cgd }
455 1.1 cgd }
456 1.1 cgd
457 1.1 cgd /* ARGSUSED */
458 1.28 christos int
459 1.32 mycroft spec_poll(v)
460 1.28 christos void *v;
461 1.28 christos {
462 1.32 mycroft struct vop_poll_args /* {
463 1.15 mycroft struct vnode *a_vp;
464 1.32 mycroft int a_events;
465 1.15 mycroft struct proc *a_p;
466 1.28 christos } */ *ap = v;
467 1.48 augustss dev_t dev;
468 1.1 cgd
469 1.15 mycroft switch (ap->a_vp->v_type) {
470 1.1 cgd
471 1.1 cgd case VCHR:
472 1.15 mycroft dev = ap->a_vp->v_rdev;
473 1.32 mycroft return (*cdevsw[major(dev)].d_poll)(dev, ap->a_events, ap->a_p);
474 1.30 mycroft
475 1.30 mycroft default:
476 1.32 mycroft return (genfs_poll(v));
477 1.15 mycroft }
478 1.15 mycroft }
479 1.15 mycroft /*
480 1.15 mycroft * Synch buffers associated with a block device
481 1.15 mycroft */
482 1.15 mycroft /* ARGSUSED */
483 1.15 mycroft int
484 1.28 christos spec_fsync(v)
485 1.28 christos void *v;
486 1.28 christos {
487 1.15 mycroft struct vop_fsync_args /* {
488 1.15 mycroft struct vnode *a_vp;
489 1.15 mycroft struct ucred *a_cred;
490 1.40 kleink int a_flags;
491 1.50 fvdl off_t offlo;
492 1.50 fvdl off_t offhi;
493 1.15 mycroft struct proc *a_p;
494 1.28 christos } */ *ap = v;
495 1.48 augustss struct vnode *vp = ap->a_vp;
496 1.15 mycroft
497 1.30 mycroft if (vp->v_type == VBLK)
498 1.40 kleink vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
499 1.15 mycroft return (0);
500 1.1 cgd }
501 1.1 cgd
502 1.1 cgd /*
503 1.1 cgd * Just call the device strategy routine
504 1.1 cgd */
505 1.28 christos int
506 1.28 christos spec_strategy(v)
507 1.28 christos void *v;
508 1.28 christos {
509 1.15 mycroft struct vop_strategy_args /* {
510 1.15 mycroft struct buf *a_bp;
511 1.28 christos } */ *ap = v;
512 1.45 fvdl struct buf *bp;
513 1.1 cgd
514 1.45 fvdl bp = ap->a_bp;
515 1.45 fvdl if (!(bp->b_flags & B_READ) &&
516 1.45 fvdl (LIST_FIRST(&bp->b_dep)) != NULL && bioops.io_start)
517 1.45 fvdl (*bioops.io_start)(bp);
518 1.45 fvdl (*bdevsw[major(bp->b_dev)].d_strategy)(bp);
519 1.1 cgd return (0);
520 1.1 cgd }
521 1.1 cgd
522 1.39 fvdl int
523 1.39 fvdl spec_inactive(v)
524 1.39 fvdl void *v;
525 1.39 fvdl {
526 1.39 fvdl struct vop_inactive_args /* {
527 1.39 fvdl struct vnode *a_vp;
528 1.39 fvdl struct proc *a_p;
529 1.39 fvdl } */ *ap = v;
530 1.39 fvdl
531 1.39 fvdl VOP_UNLOCK(ap->a_vp, 0);
532 1.39 fvdl return (0);
533 1.39 fvdl }
534 1.39 fvdl
535 1.1 cgd /*
536 1.1 cgd * This is a noop, simply returning what one has been given.
537 1.1 cgd */
538 1.28 christos int
539 1.28 christos spec_bmap(v)
540 1.28 christos void *v;
541 1.28 christos {
542 1.15 mycroft struct vop_bmap_args /* {
543 1.15 mycroft struct vnode *a_vp;
544 1.15 mycroft daddr_t a_bn;
545 1.15 mycroft struct vnode **a_vpp;
546 1.15 mycroft daddr_t *a_bnp;
547 1.39 fvdl int *a_runp;
548 1.28 christos } */ *ap = v;
549 1.1 cgd
550 1.15 mycroft if (ap->a_vpp != NULL)
551 1.15 mycroft *ap->a_vpp = ap->a_vp;
552 1.15 mycroft if (ap->a_bnp != NULL)
553 1.15 mycroft *ap->a_bnp = ap->a_bn;
554 1.39 fvdl if (ap->a_runp != NULL)
555 1.55 chs *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
556 1.1 cgd return (0);
557 1.1 cgd }
558 1.1 cgd
559 1.1 cgd /*
560 1.1 cgd * Device close routine
561 1.1 cgd */
562 1.1 cgd /* ARGSUSED */
563 1.28 christos int
564 1.28 christos spec_close(v)
565 1.28 christos void *v;
566 1.28 christos {
567 1.15 mycroft struct vop_close_args /* {
568 1.15 mycroft struct vnode *a_vp;
569 1.15 mycroft int a_fflag;
570 1.15 mycroft struct ucred *a_cred;
571 1.15 mycroft struct proc *a_p;
572 1.28 christos } */ *ap = v;
573 1.48 augustss struct vnode *vp = ap->a_vp;
574 1.1 cgd dev_t dev = vp->v_rdev;
575 1.1 cgd int (*devclose) __P((dev_t, int, int, struct proc *));
576 1.44 wrstuden int mode, error, count, flags, flags1;
577 1.44 wrstuden
578 1.54 thorpej count = vcount(vp);
579 1.44 wrstuden simple_lock(&vp->v_interlock);
580 1.44 wrstuden flags = vp->v_flag;
581 1.44 wrstuden simple_unlock(&vp->v_interlock);
582 1.1 cgd
583 1.1 cgd switch (vp->v_type) {
584 1.1 cgd
585 1.1 cgd case VCHR:
586 1.11 cgd /*
587 1.11 cgd * Hack: a tty device that is a controlling terminal
588 1.11 cgd * has a reference from the session structure.
589 1.11 cgd * We cannot easily tell that a character device is
590 1.11 cgd * a controlling terminal, unless it is the closing
591 1.11 cgd * process' controlling terminal. In that case,
592 1.11 cgd * if the reference count is 2 (this last descriptor
593 1.11 cgd * plus the session), release the reference from the session.
594 1.11 cgd */
595 1.44 wrstuden if (count == 2 && ap->a_p &&
596 1.15 mycroft vp == ap->a_p->p_session->s_ttyvp) {
597 1.11 cgd vrele(vp);
598 1.44 wrstuden count--;
599 1.15 mycroft ap->a_p->p_session->s_ttyvp = NULL;
600 1.11 cgd }
601 1.1 cgd /*
602 1.1 cgd * If the vnode is locked, then we are in the midst
603 1.1 cgd * of forcably closing the device, otherwise we only
604 1.1 cgd * close on last reference.
605 1.1 cgd */
606 1.44 wrstuden if (count > 1 && (flags & VXLOCK) == 0)
607 1.1 cgd return (0);
608 1.1 cgd devclose = cdevsw[major(dev)].d_close;
609 1.1 cgd mode = S_IFCHR;
610 1.1 cgd break;
611 1.1 cgd
612 1.1 cgd case VBLK:
613 1.1 cgd /*
614 1.1 cgd * On last close of a block device (that isn't mounted)
615 1.1 cgd * we must invalidate any in core blocks, so that
616 1.1 cgd * we can, for instance, change floppy disks.
617 1.1 cgd */
618 1.28 christos error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0);
619 1.28 christos if (error)
620 1.15 mycroft return (error);
621 1.1 cgd /*
622 1.1 cgd * We do not want to really close the device if it
623 1.1 cgd * is still in use unless we are trying to close it
624 1.1 cgd * forcibly. Since every use (buffer, vnode, swap, cmap)
625 1.1 cgd * holds a reference to the vnode, and because we mark
626 1.1 cgd * any other vnodes that alias this device, when the
627 1.1 cgd * sum of the reference counts on all the aliased
628 1.1 cgd * vnodes descends to one, we are on last close.
629 1.1 cgd */
630 1.44 wrstuden if (count > 1 && (flags & VXLOCK) == 0)
631 1.1 cgd return (0);
632 1.1 cgd devclose = bdevsw[major(dev)].d_close;
633 1.1 cgd mode = S_IFBLK;
634 1.1 cgd break;
635 1.5 cgd
636 1.1 cgd default:
637 1.1 cgd panic("spec_close: not special");
638 1.1 cgd }
639 1.1 cgd
640 1.44 wrstuden flags1 = ap->a_fflag;
641 1.44 wrstuden
642 1.44 wrstuden /*
643 1.44 wrstuden * if VXLOCK is set, then we're going away soon, so make this
644 1.44 wrstuden * non-blocking. Also ensures that we won't wedge in vn_lock below.
645 1.44 wrstuden */
646 1.44 wrstuden if (flags & VXLOCK)
647 1.44 wrstuden flags1 |= FNONBLOCK;
648 1.44 wrstuden
649 1.44 wrstuden /*
650 1.44 wrstuden * If we're able to block, release the vnode lock & reaquire. We
651 1.44 wrstuden * might end up sleaping for someone else who wants our queues. They
652 1.44 wrstuden * won't get them if we hold the vnode locked. Also, if VXLOCK is set,
653 1.44 wrstuden * don't release the lock as we won't be able to regain it.
654 1.44 wrstuden */
655 1.44 wrstuden if (!(flags1 & FNONBLOCK))
656 1.44 wrstuden VOP_UNLOCK(vp, 0);
657 1.44 wrstuden
658 1.44 wrstuden error = (*devclose)(dev, flags1, mode, ap->a_p);
659 1.44 wrstuden
660 1.44 wrstuden if (!(flags1 & FNONBLOCK))
661 1.44 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
662 1.44 wrstuden
663 1.44 wrstuden return (error);
664 1.1 cgd }
665 1.1 cgd
666 1.1 cgd /*
667 1.1 cgd * Print out the contents of a special device vnode.
668 1.1 cgd */
669 1.28 christos int
670 1.28 christos spec_print(v)
671 1.28 christos void *v;
672 1.28 christos {
673 1.15 mycroft struct vop_print_args /* {
674 1.15 mycroft struct vnode *a_vp;
675 1.28 christos } */ *ap = v;
676 1.15 mycroft
677 1.34 christos printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
678 1.33 christos minor(ap->a_vp->v_rdev));
679 1.28 christos return 0;
680 1.15 mycroft }
681 1.15 mycroft
682 1.15 mycroft /*
683 1.15 mycroft * Return POSIX pathconf information applicable to special devices.
684 1.15 mycroft */
685 1.28 christos int
686 1.28 christos spec_pathconf(v)
687 1.28 christos void *v;
688 1.28 christos {
689 1.15 mycroft struct vop_pathconf_args /* {
690 1.15 mycroft struct vnode *a_vp;
691 1.15 mycroft int a_name;
692 1.18 cgd register_t *a_retval;
693 1.28 christos } */ *ap = v;
694 1.1 cgd
695 1.15 mycroft switch (ap->a_name) {
696 1.15 mycroft case _PC_LINK_MAX:
697 1.15 mycroft *ap->a_retval = LINK_MAX;
698 1.15 mycroft return (0);
699 1.15 mycroft case _PC_MAX_CANON:
700 1.15 mycroft *ap->a_retval = MAX_CANON;
701 1.15 mycroft return (0);
702 1.15 mycroft case _PC_MAX_INPUT:
703 1.15 mycroft *ap->a_retval = MAX_INPUT;
704 1.15 mycroft return (0);
705 1.15 mycroft case _PC_PIPE_BUF:
706 1.15 mycroft *ap->a_retval = PIPE_BUF;
707 1.15 mycroft return (0);
708 1.15 mycroft case _PC_CHOWN_RESTRICTED:
709 1.15 mycroft *ap->a_retval = 1;
710 1.15 mycroft return (0);
711 1.15 mycroft case _PC_VDISABLE:
712 1.15 mycroft *ap->a_retval = _POSIX_VDISABLE;
713 1.41 kleink return (0);
714 1.41 kleink case _PC_SYNC_IO:
715 1.41 kleink *ap->a_retval = 1;
716 1.15 mycroft return (0);
717 1.15 mycroft default:
718 1.15 mycroft return (EINVAL);
719 1.15 mycroft }
720 1.1 cgd /* NOTREACHED */
721 1.35 kleink }
722 1.35 kleink
723 1.35 kleink /*
724 1.35 kleink * Advisory record locking support.
725 1.35 kleink */
726 1.35 kleink int
727 1.35 kleink spec_advlock(v)
728 1.35 kleink void *v;
729 1.35 kleink {
730 1.35 kleink struct vop_advlock_args /* {
731 1.35 kleink struct vnode *a_vp;
732 1.35 kleink caddr_t a_id;
733 1.35 kleink int a_op;
734 1.35 kleink struct flock *a_fl;
735 1.35 kleink int a_flags;
736 1.35 kleink } */ *ap = v;
737 1.48 augustss struct vnode *vp = ap->a_vp;
738 1.35 kleink
739 1.49 jdolecek return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
740 1.1 cgd }
741