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