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