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