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