spec_vnops.c revision 1.16 1 1.16 cgd /* $NetBSD: spec_vnops.c,v 1.16 1994/06/29 06:35:03 cgd Exp $ */
2 1.16 cgd
3 1.1 cgd /*
4 1.15 mycroft * Copyright (c) 1989, 1993
5 1.15 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.16 cgd * @(#)spec_vnops.c 8.6 (Berkeley) 4/9/94
36 1.1 cgd */
37 1.1 cgd
38 1.9 mycroft #include <sys/param.h>
39 1.9 mycroft #include <sys/proc.h>
40 1.9 mycroft #include <sys/systm.h>
41 1.9 mycroft #include <sys/kernel.h>
42 1.9 mycroft #include <sys/conf.h>
43 1.9 mycroft #include <sys/buf.h>
44 1.9 mycroft #include <sys/mount.h>
45 1.9 mycroft #include <sys/namei.h>
46 1.9 mycroft #include <sys/vnode.h>
47 1.9 mycroft #include <sys/stat.h>
48 1.9 mycroft #include <sys/errno.h>
49 1.9 mycroft #include <sys/ioctl.h>
50 1.9 mycroft #include <sys/file.h>
51 1.9 mycroft #include <sys/disklabel.h>
52 1.15 mycroft #include <miscfs/specfs/specdev.h>
53 1.1 cgd
54 1.1 cgd /* symbolic sleep message strings for devices */
55 1.1 cgd char devopn[] = "devopn";
56 1.1 cgd char devio[] = "devio";
57 1.1 cgd char devwait[] = "devwait";
58 1.1 cgd char devin[] = "devin";
59 1.1 cgd char devout[] = "devout";
60 1.1 cgd char devioc[] = "devioc";
61 1.1 cgd char devcls[] = "devcls";
62 1.1 cgd
63 1.15 mycroft int (**spec_vnodeop_p)();
64 1.15 mycroft struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
65 1.15 mycroft { &vop_default_desc, vn_default_error },
66 1.15 mycroft { &vop_lookup_desc, spec_lookup }, /* lookup */
67 1.15 mycroft { &vop_create_desc, spec_create }, /* create */
68 1.15 mycroft { &vop_mknod_desc, spec_mknod }, /* mknod */
69 1.15 mycroft { &vop_open_desc, spec_open }, /* open */
70 1.15 mycroft { &vop_close_desc, spec_close }, /* close */
71 1.15 mycroft { &vop_access_desc, spec_access }, /* access */
72 1.15 mycroft { &vop_getattr_desc, spec_getattr }, /* getattr */
73 1.15 mycroft { &vop_setattr_desc, spec_setattr }, /* setattr */
74 1.15 mycroft { &vop_read_desc, spec_read }, /* read */
75 1.15 mycroft { &vop_write_desc, spec_write }, /* write */
76 1.15 mycroft { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
77 1.15 mycroft { &vop_select_desc, spec_select }, /* select */
78 1.15 mycroft { &vop_mmap_desc, spec_mmap }, /* mmap */
79 1.15 mycroft { &vop_fsync_desc, spec_fsync }, /* fsync */
80 1.15 mycroft { &vop_seek_desc, spec_seek }, /* seek */
81 1.15 mycroft { &vop_remove_desc, spec_remove }, /* remove */
82 1.15 mycroft { &vop_link_desc, spec_link }, /* link */
83 1.15 mycroft { &vop_rename_desc, spec_rename }, /* rename */
84 1.15 mycroft { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
85 1.15 mycroft { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
86 1.15 mycroft { &vop_symlink_desc, spec_symlink }, /* symlink */
87 1.15 mycroft { &vop_readdir_desc, spec_readdir }, /* readdir */
88 1.15 mycroft { &vop_readlink_desc, spec_readlink }, /* readlink */
89 1.15 mycroft { &vop_abortop_desc, spec_abortop }, /* abortop */
90 1.15 mycroft { &vop_inactive_desc, spec_inactive }, /* inactive */
91 1.15 mycroft { &vop_reclaim_desc, spec_reclaim }, /* reclaim */
92 1.15 mycroft { &vop_lock_desc, spec_lock }, /* lock */
93 1.15 mycroft { &vop_unlock_desc, spec_unlock }, /* unlock */
94 1.15 mycroft { &vop_bmap_desc, spec_bmap }, /* bmap */
95 1.15 mycroft { &vop_strategy_desc, spec_strategy }, /* strategy */
96 1.15 mycroft { &vop_print_desc, spec_print }, /* print */
97 1.15 mycroft { &vop_islocked_desc, spec_islocked }, /* islocked */
98 1.15 mycroft { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
99 1.15 mycroft { &vop_advlock_desc, spec_advlock }, /* advlock */
100 1.15 mycroft { &vop_blkatoff_desc, spec_blkatoff }, /* blkatoff */
101 1.15 mycroft { &vop_valloc_desc, spec_valloc }, /* valloc */
102 1.15 mycroft { &vop_vfree_desc, spec_vfree }, /* vfree */
103 1.15 mycroft { &vop_truncate_desc, spec_truncate }, /* truncate */
104 1.15 mycroft { &vop_update_desc, spec_update }, /* update */
105 1.15 mycroft { &vop_bwrite_desc, spec_bwrite }, /* bwrite */
106 1.15 mycroft { (struct vnodeop_desc*)NULL, (int(*)())NULL }
107 1.1 cgd };
108 1.15 mycroft struct vnodeopv_desc spec_vnodeop_opv_desc =
109 1.15 mycroft { &spec_vnodeop_p, spec_vnodeop_entries };
110 1.1 cgd
111 1.1 cgd /*
112 1.1 cgd * Trivial lookup routine that always fails.
113 1.1 cgd */
114 1.4 andrew int
115 1.15 mycroft spec_lookup(ap)
116 1.15 mycroft struct vop_lookup_args /* {
117 1.15 mycroft struct vnode *a_dvp;
118 1.15 mycroft struct vnode **a_vpp;
119 1.15 mycroft struct componentname *a_cnp;
120 1.15 mycroft } */ *ap;
121 1.1 cgd {
122 1.1 cgd
123 1.15 mycroft *ap->a_vpp = NULL;
124 1.1 cgd return (ENOTDIR);
125 1.1 cgd }
126 1.1 cgd
127 1.1 cgd /*
128 1.15 mycroft * Open a special file.
129 1.1 cgd */
130 1.1 cgd /* ARGSUSED */
131 1.15 mycroft spec_open(ap)
132 1.15 mycroft struct vop_open_args /* {
133 1.15 mycroft struct vnode *a_vp;
134 1.15 mycroft int a_mode;
135 1.15 mycroft struct ucred *a_cred;
136 1.15 mycroft struct proc *a_p;
137 1.15 mycroft } */ *ap;
138 1.1 cgd {
139 1.15 mycroft struct vnode *bvp, *vp = ap->a_vp;
140 1.15 mycroft dev_t bdev, dev = (dev_t)vp->v_rdev;
141 1.1 cgd register int maj = major(dev);
142 1.1 cgd int error;
143 1.1 cgd
144 1.15 mycroft /*
145 1.15 mycroft * Don't allow open if fs is mounted -nodev.
146 1.15 mycroft */
147 1.1 cgd if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
148 1.1 cgd return (ENXIO);
149 1.1 cgd
150 1.1 cgd switch (vp->v_type) {
151 1.1 cgd
152 1.1 cgd case VCHR:
153 1.1 cgd if ((u_int)maj >= nchrdev)
154 1.1 cgd return (ENXIO);
155 1.15 mycroft if (ap->a_cred != FSCRED && (ap->a_mode & FWRITE)) {
156 1.15 mycroft /*
157 1.15 mycroft * When running in very secure mode, do not allow
158 1.15 mycroft * opens for writing of any disk character devices.
159 1.15 mycroft */
160 1.15 mycroft if (securelevel >= 2 && isdisk(dev, VCHR))
161 1.15 mycroft return (EPERM);
162 1.15 mycroft /*
163 1.15 mycroft * When running in secure mode, do not allow opens
164 1.15 mycroft * for writing of /dev/mem, /dev/kmem, or character
165 1.15 mycroft * devices whose corresponding block devices are
166 1.15 mycroft * currently mounted.
167 1.15 mycroft */
168 1.15 mycroft if (securelevel >= 1) {
169 1.15 mycroft if ((bdev = chrtoblk(dev)) != NODEV &&
170 1.15 mycroft vfinddev(bdev, VBLK, &bvp) &&
171 1.15 mycroft bvp->v_usecount > 0 &&
172 1.15 mycroft (error = vfs_mountedon(bvp)))
173 1.15 mycroft return (error);
174 1.15 mycroft if (iskmemdev(dev))
175 1.15 mycroft return (EPERM);
176 1.15 mycroft }
177 1.15 mycroft }
178 1.1 cgd VOP_UNLOCK(vp);
179 1.15 mycroft error = (*cdevsw[maj].d_open)(dev, ap->a_mode, S_IFCHR, ap->a_p);
180 1.1 cgd VOP_LOCK(vp);
181 1.1 cgd return (error);
182 1.1 cgd
183 1.1 cgd case VBLK:
184 1.1 cgd if ((u_int)maj >= nblkdev)
185 1.1 cgd return (ENXIO);
186 1.15 mycroft /*
187 1.15 mycroft * When running in very secure mode, do not allow
188 1.15 mycroft * opens for writing of any disk block devices.
189 1.15 mycroft */
190 1.15 mycroft if (securelevel >= 2 && ap->a_cred != FSCRED &&
191 1.15 mycroft (ap->a_mode & FWRITE) && isdisk(dev, VBLK))
192 1.15 mycroft return (EPERM);
193 1.15 mycroft /*
194 1.15 mycroft * Do not allow opens of block devices that are
195 1.15 mycroft * currently mounted.
196 1.15 mycroft */
197 1.15 mycroft if (error = vfs_mountedon(vp))
198 1.1 cgd return (error);
199 1.15 mycroft return ((*bdevsw[maj].d_open)(dev, ap->a_mode, S_IFBLK, ap->a_p));
200 1.1 cgd }
201 1.15 mycroft return (0);
202 1.1 cgd }
203 1.1 cgd
204 1.1 cgd /*
205 1.1 cgd * Vnode op for read
206 1.1 cgd */
207 1.1 cgd /* ARGSUSED */
208 1.15 mycroft spec_read(ap)
209 1.15 mycroft struct vop_read_args /* {
210 1.15 mycroft struct vnode *a_vp;
211 1.15 mycroft struct uio *a_uio;
212 1.15 mycroft int a_ioflag;
213 1.15 mycroft struct ucred *a_cred;
214 1.15 mycroft } */ *ap;
215 1.15 mycroft {
216 1.15 mycroft register struct vnode *vp = ap->a_vp;
217 1.15 mycroft register struct uio *uio = ap->a_uio;
218 1.15 mycroft struct proc *p = uio->uio_procp;
219 1.1 cgd struct buf *bp;
220 1.15 mycroft daddr_t bn, nextbn;
221 1.1 cgd long bsize, bscale;
222 1.1 cgd struct partinfo dpart;
223 1.15 mycroft int n, on, majordev, (*ioctl)();
224 1.1 cgd int error = 0;
225 1.15 mycroft dev_t dev;
226 1.1 cgd
227 1.1 cgd #ifdef DIAGNOSTIC
228 1.1 cgd if (uio->uio_rw != UIO_READ)
229 1.1 cgd panic("spec_read mode");
230 1.1 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
231 1.1 cgd panic("spec_read proc");
232 1.1 cgd #endif
233 1.1 cgd if (uio->uio_resid == 0)
234 1.1 cgd return (0);
235 1.1 cgd
236 1.1 cgd switch (vp->v_type) {
237 1.1 cgd
238 1.1 cgd case VCHR:
239 1.1 cgd VOP_UNLOCK(vp);
240 1.1 cgd error = (*cdevsw[major(vp->v_rdev)].d_read)
241 1.15 mycroft (vp->v_rdev, uio, ap->a_ioflag);
242 1.1 cgd VOP_LOCK(vp);
243 1.1 cgd return (error);
244 1.1 cgd
245 1.1 cgd case VBLK:
246 1.1 cgd if (uio->uio_offset < 0)
247 1.1 cgd return (EINVAL);
248 1.1 cgd bsize = BLKDEV_IOSIZE;
249 1.15 mycroft dev = vp->v_rdev;
250 1.15 mycroft if ((majordev = major(dev)) < nblkdev &&
251 1.15 mycroft (ioctl = bdevsw[majordev].d_ioctl) != NULL &&
252 1.15 mycroft (*ioctl)(dev, DIOCGPART, (caddr_t)&dpart, FREAD, p) == 0 &&
253 1.15 mycroft dpart.part->p_fstype == FS_BSDFFS &&
254 1.15 mycroft dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
255 1.15 mycroft bsize = dpart.part->p_frag * dpart.part->p_fsize;
256 1.1 cgd bscale = bsize / DEV_BSIZE;
257 1.1 cgd do {
258 1.1 cgd bn = (uio->uio_offset / DEV_BSIZE) &~ (bscale - 1);
259 1.1 cgd on = uio->uio_offset % bsize;
260 1.14 cgd n = min((unsigned)(bsize - on), uio->uio_resid);
261 1.15 mycroft if (vp->v_lastr + bscale == bn) {
262 1.15 mycroft nextbn = bn + bscale;
263 1.15 mycroft error = breadn(vp, bn, (int)bsize, &nextbn,
264 1.15 mycroft (int *)&bsize, 1, NOCRED, &bp);
265 1.15 mycroft } else
266 1.1 cgd error = bread(vp, bn, (int)bsize, NOCRED, &bp);
267 1.1 cgd vp->v_lastr = bn;
268 1.14 cgd n = min(n, bsize - bp->b_resid);
269 1.1 cgd if (error) {
270 1.1 cgd brelse(bp);
271 1.1 cgd return (error);
272 1.1 cgd }
273 1.15 mycroft error = uiomove((char *)bp->b_data + on, n, uio);
274 1.1 cgd if (n + on == bsize)
275 1.1 cgd bp->b_flags |= B_AGE;
276 1.1 cgd brelse(bp);
277 1.1 cgd } while (error == 0 && uio->uio_resid > 0 && n != 0);
278 1.1 cgd return (error);
279 1.1 cgd
280 1.1 cgd default:
281 1.1 cgd panic("spec_read type");
282 1.1 cgd }
283 1.1 cgd /* NOTREACHED */
284 1.1 cgd }
285 1.1 cgd
286 1.1 cgd /*
287 1.1 cgd * Vnode op for write
288 1.1 cgd */
289 1.1 cgd /* ARGSUSED */
290 1.15 mycroft spec_write(ap)
291 1.15 mycroft struct vop_write_args /* {
292 1.15 mycroft struct vnode *a_vp;
293 1.15 mycroft struct uio *a_uio;
294 1.15 mycroft int a_ioflag;
295 1.15 mycroft struct ucred *a_cred;
296 1.15 mycroft } */ *ap;
297 1.1 cgd {
298 1.15 mycroft register struct vnode *vp = ap->a_vp;
299 1.15 mycroft register struct uio *uio = ap->a_uio;
300 1.1 cgd struct proc *p = uio->uio_procp;
301 1.1 cgd struct buf *bp;
302 1.1 cgd daddr_t bn;
303 1.1 cgd int bsize, blkmask;
304 1.1 cgd struct partinfo dpart;
305 1.1 cgd register int n, on;
306 1.1 cgd int error = 0;
307 1.1 cgd
308 1.1 cgd #ifdef DIAGNOSTIC
309 1.1 cgd if (uio->uio_rw != UIO_WRITE)
310 1.1 cgd panic("spec_write mode");
311 1.1 cgd if (uio->uio_segflg == UIO_USERSPACE && uio->uio_procp != curproc)
312 1.1 cgd panic("spec_write proc");
313 1.1 cgd #endif
314 1.1 cgd
315 1.1 cgd switch (vp->v_type) {
316 1.1 cgd
317 1.1 cgd case VCHR:
318 1.1 cgd VOP_UNLOCK(vp);
319 1.1 cgd error = (*cdevsw[major(vp->v_rdev)].d_write)
320 1.15 mycroft (vp->v_rdev, uio, ap->a_ioflag);
321 1.1 cgd VOP_LOCK(vp);
322 1.1 cgd return (error);
323 1.1 cgd
324 1.1 cgd case VBLK:
325 1.1 cgd if (uio->uio_resid == 0)
326 1.1 cgd return (0);
327 1.1 cgd if (uio->uio_offset < 0)
328 1.1 cgd return (EINVAL);
329 1.1 cgd bsize = BLKDEV_IOSIZE;
330 1.1 cgd if ((*bdevsw[major(vp->v_rdev)].d_ioctl)(vp->v_rdev, DIOCGPART,
331 1.1 cgd (caddr_t)&dpart, FREAD, p) == 0) {
332 1.1 cgd if (dpart.part->p_fstype == FS_BSDFFS &&
333 1.1 cgd dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
334 1.1 cgd bsize = dpart.part->p_frag *
335 1.1 cgd dpart.part->p_fsize;
336 1.1 cgd }
337 1.1 cgd blkmask = (bsize / DEV_BSIZE) - 1;
338 1.1 cgd do {
339 1.1 cgd bn = (uio->uio_offset / DEV_BSIZE) &~ blkmask;
340 1.1 cgd on = uio->uio_offset % bsize;
341 1.14 cgd n = min((unsigned)(bsize - on), uio->uio_resid);
342 1.1 cgd if (n == bsize)
343 1.13 cgd bp = getblk(vp, bn, bsize, 0, 0);
344 1.1 cgd else
345 1.1 cgd error = bread(vp, bn, bsize, NOCRED, &bp);
346 1.14 cgd n = min(n, bsize - bp->b_resid);
347 1.1 cgd if (error) {
348 1.1 cgd brelse(bp);
349 1.1 cgd return (error);
350 1.1 cgd }
351 1.15 mycroft error = uiomove((char *)bp->b_data + on, n, uio);
352 1.1 cgd if (n + on == bsize) {
353 1.1 cgd bp->b_flags |= B_AGE;
354 1.1 cgd bawrite(bp);
355 1.1 cgd } else
356 1.1 cgd bdwrite(bp);
357 1.1 cgd } while (error == 0 && uio->uio_resid > 0 && n != 0);
358 1.1 cgd return (error);
359 1.1 cgd
360 1.1 cgd default:
361 1.1 cgd panic("spec_write type");
362 1.1 cgd }
363 1.1 cgd /* NOTREACHED */
364 1.1 cgd }
365 1.1 cgd
366 1.1 cgd /*
367 1.1 cgd * Device ioctl operation.
368 1.1 cgd */
369 1.1 cgd /* ARGSUSED */
370 1.15 mycroft spec_ioctl(ap)
371 1.15 mycroft struct vop_ioctl_args /* {
372 1.15 mycroft struct vnode *a_vp;
373 1.15 mycroft int a_command;
374 1.15 mycroft caddr_t a_data;
375 1.15 mycroft int a_fflag;
376 1.15 mycroft struct ucred *a_cred;
377 1.15 mycroft struct proc *a_p;
378 1.15 mycroft } */ *ap;
379 1.1 cgd {
380 1.15 mycroft dev_t dev = ap->a_vp->v_rdev;
381 1.1 cgd
382 1.15 mycroft switch (ap->a_vp->v_type) {
383 1.1 cgd
384 1.1 cgd case VCHR:
385 1.15 mycroft return ((*cdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data,
386 1.15 mycroft ap->a_fflag, ap->a_p));
387 1.1 cgd
388 1.1 cgd case VBLK:
389 1.15 mycroft if (ap->a_command == 0 && (int)ap->a_data == B_TAPE)
390 1.1 cgd if (bdevsw[major(dev)].d_flags & B_TAPE)
391 1.1 cgd return (0);
392 1.1 cgd else
393 1.1 cgd return (1);
394 1.15 mycroft return ((*bdevsw[major(dev)].d_ioctl)(dev, ap->a_command, ap->a_data,
395 1.15 mycroft ap->a_fflag, ap->a_p));
396 1.1 cgd
397 1.1 cgd default:
398 1.1 cgd panic("spec_ioctl");
399 1.1 cgd /* NOTREACHED */
400 1.1 cgd }
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd /* ARGSUSED */
404 1.15 mycroft spec_select(ap)
405 1.15 mycroft struct vop_select_args /* {
406 1.15 mycroft struct vnode *a_vp;
407 1.15 mycroft int a_which;
408 1.15 mycroft int a_fflags;
409 1.15 mycroft struct ucred *a_cred;
410 1.15 mycroft struct proc *a_p;
411 1.15 mycroft } */ *ap;
412 1.1 cgd {
413 1.1 cgd register dev_t dev;
414 1.1 cgd
415 1.15 mycroft switch (ap->a_vp->v_type) {
416 1.1 cgd
417 1.1 cgd default:
418 1.1 cgd return (1); /* XXX */
419 1.1 cgd
420 1.1 cgd case VCHR:
421 1.15 mycroft dev = ap->a_vp->v_rdev;
422 1.15 mycroft return (*cdevsw[major(dev)].d_select)(dev, ap->a_which, ap->a_p);
423 1.15 mycroft }
424 1.15 mycroft }
425 1.15 mycroft /*
426 1.15 mycroft * Synch buffers associated with a block device
427 1.15 mycroft */
428 1.15 mycroft /* ARGSUSED */
429 1.15 mycroft int
430 1.15 mycroft spec_fsync(ap)
431 1.15 mycroft struct vop_fsync_args /* {
432 1.15 mycroft struct vnode *a_vp;
433 1.15 mycroft struct ucred *a_cred;
434 1.15 mycroft int a_waitfor;
435 1.15 mycroft struct proc *a_p;
436 1.15 mycroft } */ *ap;
437 1.15 mycroft {
438 1.15 mycroft register struct vnode *vp = ap->a_vp;
439 1.15 mycroft register struct buf *bp;
440 1.15 mycroft struct buf *nbp;
441 1.15 mycroft int s;
442 1.15 mycroft
443 1.15 mycroft if (vp->v_type == VCHR)
444 1.15 mycroft return (0);
445 1.15 mycroft /*
446 1.15 mycroft * Flush all dirty buffers associated with a block device.
447 1.15 mycroft */
448 1.15 mycroft loop:
449 1.15 mycroft s = splbio();
450 1.15 mycroft for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = nbp) {
451 1.15 mycroft nbp = bp->b_vnbufs.le_next;
452 1.15 mycroft if ((bp->b_flags & B_BUSY))
453 1.15 mycroft continue;
454 1.15 mycroft if ((bp->b_flags & B_DELWRI) == 0)
455 1.15 mycroft panic("spec_fsync: not dirty");
456 1.15 mycroft bremfree(bp);
457 1.15 mycroft bp->b_flags |= B_BUSY;
458 1.15 mycroft splx(s);
459 1.15 mycroft bawrite(bp);
460 1.15 mycroft goto loop;
461 1.15 mycroft }
462 1.15 mycroft if (ap->a_waitfor == MNT_WAIT) {
463 1.15 mycroft while (vp->v_numoutput) {
464 1.15 mycroft vp->v_flag |= VBWAIT;
465 1.15 mycroft sleep((caddr_t)&vp->v_numoutput, PRIBIO + 1);
466 1.15 mycroft }
467 1.15 mycroft #ifdef DIAGNOSTIC
468 1.15 mycroft if (vp->v_dirtyblkhd.lh_first) {
469 1.15 mycroft vprint("spec_fsync: dirty", vp);
470 1.15 mycroft goto loop;
471 1.15 mycroft }
472 1.15 mycroft #endif
473 1.1 cgd }
474 1.15 mycroft splx(s);
475 1.15 mycroft return (0);
476 1.1 cgd }
477 1.1 cgd
478 1.1 cgd /*
479 1.1 cgd * Just call the device strategy routine
480 1.1 cgd */
481 1.15 mycroft spec_strategy(ap)
482 1.15 mycroft struct vop_strategy_args /* {
483 1.15 mycroft struct buf *a_bp;
484 1.15 mycroft } */ *ap;
485 1.1 cgd {
486 1.1 cgd
487 1.15 mycroft (*bdevsw[major(ap->a_bp->b_dev)].d_strategy)(ap->a_bp);
488 1.1 cgd return (0);
489 1.1 cgd }
490 1.1 cgd
491 1.1 cgd /*
492 1.1 cgd * This is a noop, simply returning what one has been given.
493 1.1 cgd */
494 1.15 mycroft spec_bmap(ap)
495 1.15 mycroft struct vop_bmap_args /* {
496 1.15 mycroft struct vnode *a_vp;
497 1.15 mycroft daddr_t a_bn;
498 1.15 mycroft struct vnode **a_vpp;
499 1.15 mycroft daddr_t *a_bnp;
500 1.15 mycroft } */ *ap;
501 1.1 cgd {
502 1.1 cgd
503 1.15 mycroft if (ap->a_vpp != NULL)
504 1.15 mycroft *ap->a_vpp = ap->a_vp;
505 1.15 mycroft if (ap->a_bnp != NULL)
506 1.15 mycroft *ap->a_bnp = ap->a_bn;
507 1.1 cgd return (0);
508 1.1 cgd }
509 1.1 cgd
510 1.1 cgd /*
511 1.1 cgd * At the moment we do not do any locking.
512 1.1 cgd */
513 1.1 cgd /* ARGSUSED */
514 1.15 mycroft spec_lock(ap)
515 1.15 mycroft struct vop_lock_args /* {
516 1.15 mycroft struct vnode *a_vp;
517 1.15 mycroft } */ *ap;
518 1.1 cgd {
519 1.1 cgd
520 1.1 cgd return (0);
521 1.1 cgd }
522 1.1 cgd
523 1.1 cgd /* ARGSUSED */
524 1.15 mycroft spec_unlock(ap)
525 1.15 mycroft struct vop_unlock_args /* {
526 1.15 mycroft struct vnode *a_vp;
527 1.15 mycroft } */ *ap;
528 1.1 cgd {
529 1.1 cgd
530 1.1 cgd return (0);
531 1.1 cgd }
532 1.1 cgd
533 1.1 cgd /*
534 1.1 cgd * Device close routine
535 1.1 cgd */
536 1.1 cgd /* ARGSUSED */
537 1.15 mycroft spec_close(ap)
538 1.15 mycroft struct vop_close_args /* {
539 1.15 mycroft struct vnode *a_vp;
540 1.15 mycroft int a_fflag;
541 1.15 mycroft struct ucred *a_cred;
542 1.15 mycroft struct proc *a_p;
543 1.15 mycroft } */ *ap;
544 1.1 cgd {
545 1.15 mycroft register struct vnode *vp = ap->a_vp;
546 1.1 cgd dev_t dev = vp->v_rdev;
547 1.1 cgd int (*devclose) __P((dev_t, int, int, struct proc *));
548 1.15 mycroft int mode, error;
549 1.1 cgd
550 1.1 cgd switch (vp->v_type) {
551 1.1 cgd
552 1.1 cgd case VCHR:
553 1.11 cgd /*
554 1.11 cgd * Hack: a tty device that is a controlling terminal
555 1.11 cgd * has a reference from the session structure.
556 1.11 cgd * We cannot easily tell that a character device is
557 1.11 cgd * a controlling terminal, unless it is the closing
558 1.11 cgd * process' controlling terminal. In that case,
559 1.11 cgd * if the reference count is 2 (this last descriptor
560 1.11 cgd * plus the session), release the reference from the session.
561 1.11 cgd */
562 1.15 mycroft if (vcount(vp) == 2 && ap->a_p &&
563 1.15 mycroft vp == ap->a_p->p_session->s_ttyvp) {
564 1.11 cgd vrele(vp);
565 1.15 mycroft ap->a_p->p_session->s_ttyvp = NULL;
566 1.11 cgd }
567 1.1 cgd /*
568 1.1 cgd * If the vnode is locked, then we are in the midst
569 1.1 cgd * of forcably closing the device, otherwise we only
570 1.1 cgd * close on last reference.
571 1.1 cgd */
572 1.1 cgd if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
573 1.1 cgd return (0);
574 1.1 cgd devclose = cdevsw[major(dev)].d_close;
575 1.1 cgd mode = S_IFCHR;
576 1.1 cgd break;
577 1.1 cgd
578 1.1 cgd case VBLK:
579 1.1 cgd /*
580 1.1 cgd * On last close of a block device (that isn't mounted)
581 1.1 cgd * we must invalidate any in core blocks, so that
582 1.1 cgd * we can, for instance, change floppy disks.
583 1.1 cgd */
584 1.15 mycroft if (error = vinvalbuf(vp, V_SAVE, ap->a_cred, ap->a_p, 0, 0))
585 1.15 mycroft return (error);
586 1.1 cgd /*
587 1.1 cgd * We do not want to really close the device if it
588 1.1 cgd * is still in use unless we are trying to close it
589 1.1 cgd * forcibly. Since every use (buffer, vnode, swap, cmap)
590 1.1 cgd * holds a reference to the vnode, and because we mark
591 1.1 cgd * any other vnodes that alias this device, when the
592 1.1 cgd * sum of the reference counts on all the aliased
593 1.1 cgd * vnodes descends to one, we are on last close.
594 1.1 cgd */
595 1.1 cgd if (vcount(vp) > 1 && (vp->v_flag & VXLOCK) == 0)
596 1.1 cgd return (0);
597 1.1 cgd devclose = bdevsw[major(dev)].d_close;
598 1.1 cgd mode = S_IFBLK;
599 1.1 cgd break;
600 1.5 cgd
601 1.1 cgd default:
602 1.1 cgd panic("spec_close: not special");
603 1.1 cgd }
604 1.1 cgd
605 1.15 mycroft return ((*devclose)(dev, ap->a_fflag, mode, ap->a_p));
606 1.1 cgd }
607 1.1 cgd
608 1.1 cgd /*
609 1.1 cgd * Print out the contents of a special device vnode.
610 1.1 cgd */
611 1.15 mycroft spec_print(ap)
612 1.15 mycroft struct vop_print_args /* {
613 1.15 mycroft struct vnode *a_vp;
614 1.15 mycroft } */ *ap;
615 1.15 mycroft {
616 1.15 mycroft
617 1.15 mycroft printf("tag VT_NON, dev %d, %d\n", major(ap->a_vp->v_rdev),
618 1.15 mycroft minor(ap->a_vp->v_rdev));
619 1.15 mycroft }
620 1.15 mycroft
621 1.15 mycroft /*
622 1.15 mycroft * Return POSIX pathconf information applicable to special devices.
623 1.15 mycroft */
624 1.15 mycroft spec_pathconf(ap)
625 1.15 mycroft struct vop_pathconf_args /* {
626 1.15 mycroft struct vnode *a_vp;
627 1.15 mycroft int a_name;
628 1.15 mycroft int *a_retval;
629 1.15 mycroft } */ *ap;
630 1.1 cgd {
631 1.1 cgd
632 1.15 mycroft switch (ap->a_name) {
633 1.15 mycroft case _PC_LINK_MAX:
634 1.15 mycroft *ap->a_retval = LINK_MAX;
635 1.15 mycroft return (0);
636 1.15 mycroft case _PC_MAX_CANON:
637 1.15 mycroft *ap->a_retval = MAX_CANON;
638 1.15 mycroft return (0);
639 1.15 mycroft case _PC_MAX_INPUT:
640 1.15 mycroft *ap->a_retval = MAX_INPUT;
641 1.15 mycroft return (0);
642 1.15 mycroft case _PC_PIPE_BUF:
643 1.15 mycroft *ap->a_retval = PIPE_BUF;
644 1.15 mycroft return (0);
645 1.15 mycroft case _PC_CHOWN_RESTRICTED:
646 1.15 mycroft *ap->a_retval = 1;
647 1.15 mycroft return (0);
648 1.15 mycroft case _PC_VDISABLE:
649 1.15 mycroft *ap->a_retval = _POSIX_VDISABLE;
650 1.15 mycroft return (0);
651 1.15 mycroft default:
652 1.15 mycroft return (EINVAL);
653 1.15 mycroft }
654 1.15 mycroft /* NOTREACHED */
655 1.1 cgd }
656 1.1 cgd
657 1.1 cgd /*
658 1.1 cgd * Special device advisory byte-level locks.
659 1.1 cgd */
660 1.1 cgd /* ARGSUSED */
661 1.15 mycroft spec_advlock(ap)
662 1.15 mycroft struct vop_advlock_args /* {
663 1.15 mycroft struct vnode *a_vp;
664 1.15 mycroft caddr_t a_id;
665 1.15 mycroft int a_op;
666 1.15 mycroft struct flock *a_fl;
667 1.15 mycroft int a_flags;
668 1.15 mycroft } */ *ap;
669 1.1 cgd {
670 1.1 cgd
671 1.1 cgd return (EOPNOTSUPP);
672 1.1 cgd }
673 1.1 cgd
674 1.1 cgd /*
675 1.1 cgd * Special device failed operation
676 1.1 cgd */
677 1.1 cgd spec_ebadf()
678 1.1 cgd {
679 1.1 cgd
680 1.1 cgd return (EBADF);
681 1.1 cgd }
682 1.1 cgd
683 1.1 cgd /*
684 1.1 cgd * Special device bad operation
685 1.1 cgd */
686 1.1 cgd spec_badop()
687 1.1 cgd {
688 1.1 cgd
689 1.1 cgd panic("spec_badop called");
690 1.1 cgd /* NOTREACHED */
691 1.1 cgd }
692