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