spec_vnops.c revision 1.115 1 1.115 hannken /* $NetBSD: spec_vnops.c,v 1.115 2008/01/25 16:21:04 hannken Exp $ */
2 1.112 ad
3 1.112 ad /*-
4 1.112 ad * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.112 ad * All rights reserved.
6 1.112 ad *
7 1.112 ad * Redistribution and use in source and binary forms, with or without
8 1.112 ad * modification, are permitted provided that the following conditions
9 1.112 ad * are met:
10 1.112 ad * 1. Redistributions of source code must retain the above copyright
11 1.112 ad * notice, this list of conditions and the following disclaimer.
12 1.112 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.112 ad * notice, this list of conditions and the following disclaimer in the
14 1.112 ad * documentation and/or other materials provided with the distribution.
15 1.112 ad * 3. All advertising materials mentioning features or use of this software
16 1.112 ad * must display the following acknowledgement:
17 1.112 ad * This product includes software developed by the NetBSD
18 1.112 ad * Foundation, Inc. and its contributors.
19 1.112 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
20 1.112 ad * contributors may be used to endorse or promote products derived
21 1.112 ad * from this software without specific prior written permission.
22 1.112 ad *
23 1.112 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.112 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.112 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.112 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.112 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.112 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.112 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.112 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.112 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.112 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.112 ad * POSSIBILITY OF SUCH DAMAGE.
34 1.112 ad */
35 1.16 cgd
36 1.1 cgd /*
37 1.15 mycroft * Copyright (c) 1989, 1993
38 1.15 mycroft * The Regents of the University of California. All rights reserved.
39 1.1 cgd *
40 1.1 cgd * Redistribution and use in source and binary forms, with or without
41 1.1 cgd * modification, are permitted provided that the following conditions
42 1.1 cgd * are met:
43 1.1 cgd * 1. Redistributions of source code must retain the above copyright
44 1.1 cgd * notice, this list of conditions and the following disclaimer.
45 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
46 1.1 cgd * notice, this list of conditions and the following disclaimer in the
47 1.1 cgd * documentation and/or other materials provided with the distribution.
48 1.69 agc * 3. Neither the name of the University nor the names of its contributors
49 1.1 cgd * may be used to endorse or promote products derived from this software
50 1.1 cgd * without specific prior written permission.
51 1.1 cgd *
52 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 1.1 cgd * SUCH DAMAGE.
63 1.1 cgd *
64 1.39 fvdl * @(#)spec_vnops.c 8.15 (Berkeley) 7/14/95
65 1.1 cgd */
66 1.60 lukem
67 1.60 lukem #include <sys/cdefs.h>
68 1.115 hannken __KERNEL_RCSID(0, "$NetBSD: spec_vnops.c,v 1.115 2008/01/25 16:21:04 hannken Exp $");
69 1.1 cgd
70 1.9 mycroft #include <sys/param.h>
71 1.9 mycroft #include <sys/proc.h>
72 1.9 mycroft #include <sys/systm.h>
73 1.9 mycroft #include <sys/kernel.h>
74 1.9 mycroft #include <sys/conf.h>
75 1.9 mycroft #include <sys/buf.h>
76 1.9 mycroft #include <sys/mount.h>
77 1.9 mycroft #include <sys/namei.h>
78 1.9 mycroft #include <sys/vnode.h>
79 1.9 mycroft #include <sys/stat.h>
80 1.9 mycroft #include <sys/errno.h>
81 1.9 mycroft #include <sys/ioctl.h>
82 1.81 ws #include <sys/poll.h>
83 1.9 mycroft #include <sys/file.h>
84 1.9 mycroft #include <sys/disklabel.h>
85 1.35 kleink #include <sys/lockf.h>
86 1.71 dsl #include <sys/tty.h>
87 1.87 elad #include <sys/kauth.h>
88 1.106 hannken #include <sys/fstrans.h>
89 1.28 christos
90 1.30 mycroft #include <miscfs/genfs/genfs.h>
91 1.15 mycroft #include <miscfs/specfs/specdev.h>
92 1.1 cgd
93 1.1 cgd /* symbolic sleep message strings for devices */
94 1.37 mycroft const char devopn[] = "devopn";
95 1.37 mycroft const char devio[] = "devio";
96 1.37 mycroft const char devwait[] = "devwait";
97 1.37 mycroft const char devin[] = "devin";
98 1.37 mycroft const char devout[] = "devout";
99 1.37 mycroft const char devioc[] = "devioc";
100 1.37 mycroft const char devcls[] = "devcls";
101 1.61 matt
102 1.112 ad vnode_t *specfs_hash[SPECHSZ];
103 1.112 ad kmutex_t specfs_lock;
104 1.46 sommerfe
105 1.46 sommerfe /*
106 1.112 ad * This vnode operations vector is used for special device nodes
107 1.112 ad * created from whole cloth by the kernel. For the ops vector for
108 1.112 ad * vnodes built from special devices found in a filesystem, see (e.g)
109 1.112 ad * ffs_specop_entries[] in ffs_vnops.c or the equivalent for other
110 1.112 ad * filesystems.
111 1.46 sommerfe */
112 1.1 cgd
113 1.82 xtraeme int (**spec_vnodeop_p)(void *);
114 1.53 jdolecek const struct vnodeopv_entry_desc spec_vnodeop_entries[] = {
115 1.15 mycroft { &vop_default_desc, vn_default_error },
116 1.15 mycroft { &vop_lookup_desc, spec_lookup }, /* lookup */
117 1.15 mycroft { &vop_create_desc, spec_create }, /* create */
118 1.15 mycroft { &vop_mknod_desc, spec_mknod }, /* mknod */
119 1.15 mycroft { &vop_open_desc, spec_open }, /* open */
120 1.15 mycroft { &vop_close_desc, spec_close }, /* close */
121 1.15 mycroft { &vop_access_desc, spec_access }, /* access */
122 1.15 mycroft { &vop_getattr_desc, spec_getattr }, /* getattr */
123 1.15 mycroft { &vop_setattr_desc, spec_setattr }, /* setattr */
124 1.15 mycroft { &vop_read_desc, spec_read }, /* read */
125 1.15 mycroft { &vop_write_desc, spec_write }, /* write */
126 1.47 sommerfe { &vop_fcntl_desc, spec_fcntl }, /* fcntl */
127 1.15 mycroft { &vop_ioctl_desc, spec_ioctl }, /* ioctl */
128 1.32 mycroft { &vop_poll_desc, spec_poll }, /* poll */
129 1.65 jdolecek { &vop_kqfilter_desc, spec_kqfilter }, /* kqfilter */
130 1.39 fvdl { &vop_revoke_desc, spec_revoke }, /* revoke */
131 1.15 mycroft { &vop_mmap_desc, spec_mmap }, /* mmap */
132 1.15 mycroft { &vop_fsync_desc, spec_fsync }, /* fsync */
133 1.15 mycroft { &vop_seek_desc, spec_seek }, /* seek */
134 1.15 mycroft { &vop_remove_desc, spec_remove }, /* remove */
135 1.15 mycroft { &vop_link_desc, spec_link }, /* link */
136 1.15 mycroft { &vop_rename_desc, spec_rename }, /* rename */
137 1.15 mycroft { &vop_mkdir_desc, spec_mkdir }, /* mkdir */
138 1.15 mycroft { &vop_rmdir_desc, spec_rmdir }, /* rmdir */
139 1.15 mycroft { &vop_symlink_desc, spec_symlink }, /* symlink */
140 1.15 mycroft { &vop_readdir_desc, spec_readdir }, /* readdir */
141 1.15 mycroft { &vop_readlink_desc, spec_readlink }, /* readlink */
142 1.15 mycroft { &vop_abortop_desc, spec_abortop }, /* abortop */
143 1.15 mycroft { &vop_inactive_desc, spec_inactive }, /* inactive */
144 1.15 mycroft { &vop_reclaim_desc, spec_reclaim }, /* reclaim */
145 1.15 mycroft { &vop_lock_desc, spec_lock }, /* lock */
146 1.15 mycroft { &vop_unlock_desc, spec_unlock }, /* unlock */
147 1.15 mycroft { &vop_bmap_desc, spec_bmap }, /* bmap */
148 1.15 mycroft { &vop_strategy_desc, spec_strategy }, /* strategy */
149 1.15 mycroft { &vop_print_desc, spec_print }, /* print */
150 1.15 mycroft { &vop_islocked_desc, spec_islocked }, /* islocked */
151 1.15 mycroft { &vop_pathconf_desc, spec_pathconf }, /* pathconf */
152 1.15 mycroft { &vop_advlock_desc, spec_advlock }, /* advlock */
153 1.28 christos { &vop_bwrite_desc, spec_bwrite }, /* bwrite */
154 1.55 chs { &vop_getpages_desc, spec_getpages }, /* getpages */
155 1.55 chs { &vop_putpages_desc, spec_putpages }, /* putpages */
156 1.55 chs { NULL, NULL }
157 1.1 cgd };
158 1.53 jdolecek const struct vnodeopv_desc spec_vnodeop_opv_desc =
159 1.15 mycroft { &spec_vnodeop_p, spec_vnodeop_entries };
160 1.1 cgd
161 1.1 cgd /*
162 1.112 ad * Returns true if dev is /dev/mem or /dev/kmem.
163 1.112 ad */
164 1.112 ad int
165 1.112 ad iskmemdev(dev_t dev)
166 1.112 ad {
167 1.112 ad /* mem_no is emitted by config(8) to generated devsw.c */
168 1.112 ad extern const int mem_no;
169 1.112 ad
170 1.112 ad /* minor 14 is /dev/io on i386 with COMPAT_10 */
171 1.112 ad return (major(dev) == mem_no && (minor(dev) < 2 || minor(dev) == 14));
172 1.112 ad }
173 1.112 ad
174 1.112 ad /*
175 1.112 ad * Initialize a vnode that represents a device.
176 1.112 ad */
177 1.112 ad void
178 1.112 ad spec_node_init(vnode_t *vp, dev_t rdev)
179 1.112 ad {
180 1.112 ad specnode_t *sn;
181 1.112 ad specdev_t *sd;
182 1.112 ad vnode_t *vp2;
183 1.112 ad vnode_t **vpp;
184 1.112 ad
185 1.112 ad KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
186 1.112 ad KASSERT(vp->v_specnode == NULL);
187 1.112 ad
188 1.112 ad /*
189 1.112 ad * Search the hash table for this device. If known, add a
190 1.112 ad * reference to the device structure. If not known, create
191 1.112 ad * a new entry to represent the device. In all cases add
192 1.112 ad * the vnode to the hash table.
193 1.112 ad */
194 1.112 ad sn = kmem_alloc(sizeof(*sn), KM_SLEEP);
195 1.112 ad if (sn == NULL) {
196 1.112 ad /* XXX */
197 1.112 ad panic("spec_node_init: unable to allocate memory");
198 1.112 ad }
199 1.112 ad sd = kmem_alloc(sizeof(*sd), KM_SLEEP);
200 1.112 ad if (sd == NULL) {
201 1.112 ad /* XXX */
202 1.112 ad panic("spec_node_init: unable to allocate memory");
203 1.112 ad }
204 1.112 ad mutex_enter(&specfs_lock);
205 1.112 ad vpp = &specfs_hash[SPECHASH(rdev)];
206 1.112 ad for (vp2 = *vpp; vp2 != NULL; vp2 = vp2->v_specnext) {
207 1.112 ad KASSERT(vp2->v_specnode != NULL);
208 1.112 ad if (rdev == vp2->v_rdev && vp->v_type == vp2->v_type) {
209 1.112 ad break;
210 1.112 ad }
211 1.112 ad }
212 1.112 ad if (vp2 == NULL) {
213 1.112 ad /* No existing record, create a new one. */
214 1.112 ad sd->sd_rdev = rdev;
215 1.112 ad sd->sd_mountpoint = NULL;
216 1.112 ad sd->sd_lockf = NULL;
217 1.112 ad sd->sd_refcnt = 1;
218 1.112 ad sd->sd_opencnt = 0;
219 1.112 ad sd->sd_bdevvp = NULL;
220 1.112 ad sn->sn_dev = sd;
221 1.112 ad sd = NULL;
222 1.112 ad } else {
223 1.112 ad /* Use the existing record. */
224 1.112 ad sn->sn_dev = vp2->v_specnode->sn_dev;
225 1.112 ad sn->sn_dev->sd_refcnt++;
226 1.112 ad }
227 1.112 ad /* Insert vnode into the hash chain. */
228 1.112 ad sn->sn_opencnt = 0;
229 1.112 ad sn->sn_rdev = rdev;
230 1.112 ad sn->sn_gone = false;
231 1.112 ad vp->v_specnode = sn;
232 1.112 ad vp->v_specnext = *vpp;
233 1.112 ad *vpp = vp;
234 1.112 ad mutex_exit(&specfs_lock);
235 1.112 ad
236 1.112 ad /* Free the record we allocated if unused. */
237 1.112 ad if (sd != NULL) {
238 1.112 ad kmem_free(sd, sizeof(*sd));
239 1.112 ad }
240 1.112 ad }
241 1.112 ad
242 1.112 ad /*
243 1.112 ad * A vnode representing a special device is going away. Close
244 1.112 ad * the device if the vnode holds it open.
245 1.112 ad */
246 1.112 ad void
247 1.112 ad spec_node_revoke(vnode_t *vp)
248 1.112 ad {
249 1.112 ad specnode_t *sn;
250 1.112 ad specdev_t *sd;
251 1.112 ad
252 1.112 ad sn = vp->v_specnode;
253 1.112 ad sd = sn->sn_dev;
254 1.112 ad
255 1.112 ad KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
256 1.112 ad KASSERT(vp->v_specnode != NULL);
257 1.112 ad KASSERT((vp->v_iflag & VI_XLOCK) != 0);
258 1.112 ad KASSERT(sn->sn_gone == false);
259 1.112 ad
260 1.112 ad mutex_enter(&specfs_lock);
261 1.112 ad KASSERT(sn->sn_opencnt <= sd->sd_opencnt);
262 1.112 ad if (sn->sn_opencnt != 0) {
263 1.112 ad sd->sd_opencnt -= (sn->sn_opencnt - 1);
264 1.112 ad sn->sn_opencnt = 1;
265 1.112 ad sn->sn_gone = true;
266 1.112 ad mutex_exit(&specfs_lock);
267 1.112 ad
268 1.112 ad VOP_CLOSE(vp, FNONBLOCK, NOCRED);
269 1.112 ad
270 1.112 ad mutex_enter(&specfs_lock);
271 1.112 ad KASSERT(sn->sn_opencnt == 0);
272 1.112 ad }
273 1.112 ad mutex_exit(&specfs_lock);
274 1.112 ad }
275 1.112 ad
276 1.112 ad /*
277 1.112 ad * A vnode representing a special device is being recycled.
278 1.112 ad * Destroy the specfs component.
279 1.112 ad */
280 1.112 ad void
281 1.112 ad spec_node_destroy(vnode_t *vp)
282 1.112 ad {
283 1.112 ad specnode_t *sn;
284 1.112 ad specdev_t *sd;
285 1.112 ad vnode_t **vpp, *vp2;
286 1.112 ad int refcnt;
287 1.112 ad
288 1.112 ad sn = vp->v_specnode;
289 1.112 ad sd = sn->sn_dev;
290 1.112 ad
291 1.112 ad KASSERT(vp->v_type == VBLK || vp->v_type == VCHR);
292 1.112 ad KASSERT(vp->v_specnode != NULL);
293 1.112 ad KASSERT(sn->sn_opencnt == 0);
294 1.112 ad
295 1.112 ad mutex_enter(&specfs_lock);
296 1.112 ad /* Remove from the hash and destroy the node. */
297 1.112 ad vpp = &specfs_hash[SPECHASH(vp->v_rdev)];
298 1.112 ad for (vp2 = *vpp;; vp2 = vp2->v_specnext) {
299 1.112 ad if (vp2 == NULL) {
300 1.112 ad panic("spec_node_destroy: corrupt hash");
301 1.112 ad }
302 1.112 ad if (vp2 == vp) {
303 1.112 ad KASSERT(vp == *vpp);
304 1.112 ad *vpp = vp->v_specnext;
305 1.112 ad break;
306 1.112 ad }
307 1.112 ad if (vp2->v_specnext == vp) {
308 1.112 ad vp2->v_specnext = vp->v_specnext;
309 1.112 ad break;
310 1.112 ad }
311 1.112 ad }
312 1.112 ad sn = vp->v_specnode;
313 1.112 ad vp->v_specnode = NULL;
314 1.112 ad refcnt = sd->sd_refcnt--;
315 1.112 ad KASSERT(refcnt > 0);
316 1.112 ad mutex_exit(&specfs_lock);
317 1.112 ad
318 1.112 ad /* If the device is no longer in use, destroy our record. */
319 1.112 ad if (refcnt == 1) {
320 1.112 ad KASSERT(sd->sd_opencnt == 0);
321 1.112 ad KASSERT(sd->sd_bdevvp == NULL);
322 1.112 ad kmem_free(sd, sizeof(*sd));
323 1.112 ad }
324 1.112 ad kmem_free(sn, sizeof(*sn));
325 1.112 ad }
326 1.112 ad
327 1.112 ad /*
328 1.1 cgd * Trivial lookup routine that always fails.
329 1.1 cgd */
330 1.4 andrew int
331 1.104 pooka spec_lookup(void *v)
332 1.28 christos {
333 1.15 mycroft struct vop_lookup_args /* {
334 1.15 mycroft struct vnode *a_dvp;
335 1.15 mycroft struct vnode **a_vpp;
336 1.15 mycroft struct componentname *a_cnp;
337 1.28 christos } */ *ap = v;
338 1.1 cgd
339 1.15 mycroft *ap->a_vpp = NULL;
340 1.1 cgd return (ENOTDIR);
341 1.66 jdolecek }
342 1.66 jdolecek
343 1.66 jdolecek /*
344 1.15 mycroft * Open a special file.
345 1.1 cgd */
346 1.1 cgd /* ARGSUSED */
347 1.28 christos int
348 1.104 pooka spec_open(void *v)
349 1.28 christos {
350 1.15 mycroft struct vop_open_args /* {
351 1.15 mycroft struct vnode *a_vp;
352 1.15 mycroft int a_mode;
353 1.87 elad kauth_cred_t a_cred;
354 1.28 christos } */ *ap = v;
355 1.112 ad struct lwp *l;
356 1.112 ad struct vnode *vp;
357 1.112 ad dev_t dev;
358 1.1 cgd int error;
359 1.55 chs struct partinfo pi;
360 1.96 elad enum kauth_device_req req;
361 1.112 ad specnode_t *sn;
362 1.112 ad specdev_t *sd;
363 1.112 ad
364 1.112 ad l = curlwp;
365 1.112 ad vp = ap->a_vp;
366 1.112 ad dev = vp->v_rdev;
367 1.112 ad sn = vp->v_specnode;
368 1.112 ad sd = sn->sn_dev;
369 1.1 cgd
370 1.15 mycroft /*
371 1.15 mycroft * Don't allow open if fs is mounted -nodev.
372 1.15 mycroft */
373 1.1 cgd if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_NODEV))
374 1.1 cgd return (ENXIO);
375 1.1 cgd
376 1.112 ad switch (ap->a_mode & (FREAD | FWRITE)) {
377 1.112 ad case FREAD | FWRITE:
378 1.112 ad req = KAUTH_REQ_DEVICE_RAWIO_SPEC_RW;
379 1.112 ad break;
380 1.112 ad case FWRITE:
381 1.112 ad req = KAUTH_REQ_DEVICE_RAWIO_SPEC_WRITE;
382 1.112 ad break;
383 1.112 ad default:
384 1.112 ad req = KAUTH_REQ_DEVICE_RAWIO_SPEC_READ;
385 1.112 ad break;
386 1.112 ad }
387 1.89 elad
388 1.1 cgd switch (vp->v_type) {
389 1.1 cgd case VCHR:
390 1.96 elad error = kauth_authorize_device_spec(ap->a_cred, req, vp);
391 1.112 ad if (error != 0)
392 1.96 elad return (error);
393 1.89 elad
394 1.112 ad /*
395 1.112 ad * Character devices can accept opens from multiple
396 1.112 ad * vnodes.
397 1.112 ad */
398 1.112 ad mutex_enter(&specfs_lock);
399 1.112 ad if (sn->sn_gone) {
400 1.112 ad mutex_exit(&specfs_lock);
401 1.112 ad return (EBADF);
402 1.112 ad }
403 1.112 ad sd->sd_opencnt++;
404 1.112 ad sn->sn_opencnt++;
405 1.112 ad mutex_exit(&specfs_lock);
406 1.100 ad if (cdev_type(dev) == D_TTY)
407 1.108 ad vp->v_vflag |= VV_ISTTY;
408 1.39 fvdl VOP_UNLOCK(vp, 0);
409 1.100 ad error = cdev_open(dev, ap->a_mode, S_IFCHR, l);
410 1.39 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
411 1.70 dsl break;
412 1.1 cgd
413 1.1 cgd case VBLK:
414 1.96 elad error = kauth_authorize_device_spec(ap->a_cred, req, vp);
415 1.112 ad if (error != 0)
416 1.96 elad return (error);
417 1.112 ad
418 1.112 ad /*
419 1.112 ad * For block devices, permit only one open. The buffer
420 1.112 ad * cache cannot remain self-consistent with multiple
421 1.112 ad * vnodes holding a block device open.
422 1.112 ad */
423 1.112 ad mutex_enter(&specfs_lock);
424 1.112 ad if (sn->sn_gone) {
425 1.112 ad mutex_exit(&specfs_lock);
426 1.112 ad return (EBADF);
427 1.112 ad }
428 1.112 ad if (sd->sd_opencnt != 0) {
429 1.112 ad mutex_exit(&specfs_lock);
430 1.112 ad return EBUSY;
431 1.112 ad }
432 1.112 ad sn->sn_opencnt = 1;
433 1.112 ad sd->sd_opencnt = 1;
434 1.112 ad sd->sd_bdevvp = vp;
435 1.112 ad mutex_exit(&specfs_lock);
436 1.112 ad
437 1.100 ad error = bdev_open(dev, ap->a_mode, S_IFBLK, l);
438 1.70 dsl break;
439 1.55 chs
440 1.28 christos case VNON:
441 1.28 christos case VLNK:
442 1.28 christos case VDIR:
443 1.28 christos case VREG:
444 1.28 christos case VBAD:
445 1.28 christos case VFIFO:
446 1.28 christos case VSOCK:
447 1.70 dsl default:
448 1.70 dsl return 0;
449 1.1 cgd }
450 1.70 dsl
451 1.112 ad mutex_enter(&specfs_lock);
452 1.112 ad if (sn->sn_gone) {
453 1.112 ad if (error == 0)
454 1.112 ad error = EBADF;
455 1.112 ad } else if (error != 0) {
456 1.112 ad sd->sd_opencnt--;
457 1.112 ad sn->sn_opencnt--;
458 1.115 hannken if (vp->v_type == VBLK)
459 1.115 hannken sd->sd_bdevvp = NULL;
460 1.115 hannken
461 1.112 ad }
462 1.112 ad mutex_exit(&specfs_lock);
463 1.89 elad
464 1.112 ad if (cdev_type(dev) != D_DISK || error != 0)
465 1.70 dsl return error;
466 1.112 ad
467 1.100 ad if (vp->v_type == VCHR)
468 1.100 ad error = cdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
469 1.100 ad else
470 1.100 ad error = bdev_ioctl(vp->v_rdev, DIOCGPART, &pi, FREAD, curlwp);
471 1.100 ad if (error == 0)
472 1.99 yamt uvm_vnp_setsize(vp,
473 1.99 yamt (voff_t)pi.disklab->d_secsize * pi.part->p_size);
474 1.70 dsl return 0;
475 1.1 cgd }
476 1.1 cgd
477 1.1 cgd /*
478 1.1 cgd * Vnode op for read
479 1.1 cgd */
480 1.1 cgd /* ARGSUSED */
481 1.28 christos int
482 1.104 pooka spec_read(void *v)
483 1.28 christos {
484 1.15 mycroft struct vop_read_args /* {
485 1.15 mycroft struct vnode *a_vp;
486 1.15 mycroft struct uio *a_uio;
487 1.15 mycroft int a_ioflag;
488 1.87 elad kauth_cred_t a_cred;
489 1.28 christos } */ *ap = v;
490 1.48 augustss struct vnode *vp = ap->a_vp;
491 1.48 augustss struct uio *uio = ap->a_uio;
492 1.86 yamt struct lwp *l = curlwp;
493 1.56 chs struct buf *bp;
494 1.57 chs daddr_t bn;
495 1.59 chs int bsize, bscale;
496 1.56 chs struct partinfo dpart;
497 1.64 gehenna int n, on;
498 1.1 cgd int error = 0;
499 1.1 cgd
500 1.1 cgd #ifdef DIAGNOSTIC
501 1.1 cgd if (uio->uio_rw != UIO_READ)
502 1.1 cgd panic("spec_read mode");
503 1.86 yamt if (&uio->uio_vmspace->vm_map != kernel_map &&
504 1.86 yamt uio->uio_vmspace != curproc->p_vmspace)
505 1.1 cgd panic("spec_read proc");
506 1.1 cgd #endif
507 1.1 cgd if (uio->uio_resid == 0)
508 1.1 cgd return (0);
509 1.1 cgd
510 1.56 chs switch (vp->v_type) {
511 1.56 chs
512 1.56 chs case VCHR:
513 1.39 fvdl VOP_UNLOCK(vp, 0);
514 1.100 ad error = cdev_read(vp->v_rdev, uio, ap->a_ioflag);
515 1.58 chs vn_lock(vp, LK_SHARED | LK_RETRY);
516 1.1 cgd return (error);
517 1.1 cgd
518 1.56 chs case VBLK:
519 1.112 ad KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
520 1.56 chs if (uio->uio_offset < 0)
521 1.56 chs return (EINVAL);
522 1.56 chs bsize = BLKDEV_IOSIZE;
523 1.100 ad if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
524 1.56 chs if (dpart.part->p_fstype == FS_BSDFFS &&
525 1.56 chs dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
526 1.56 chs bsize = dpart.part->p_frag *
527 1.56 chs dpart.part->p_fsize;
528 1.56 chs }
529 1.59 chs bscale = bsize >> DEV_BSHIFT;
530 1.56 chs do {
531 1.59 chs bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
532 1.56 chs on = uio->uio_offset % bsize;
533 1.56 chs n = min((unsigned)(bsize - on), uio->uio_resid);
534 1.57 chs error = bread(vp, bn, bsize, NOCRED, &bp);
535 1.56 chs n = min(n, bsize - bp->b_resid);
536 1.56 chs if (error) {
537 1.107 ad brelse(bp, 0);
538 1.56 chs return (error);
539 1.56 chs }
540 1.56 chs error = uiomove((char *)bp->b_data + on, n, uio);
541 1.107 ad brelse(bp, 0);
542 1.56 chs } while (error == 0 && uio->uio_resid > 0 && n != 0);
543 1.56 chs return (error);
544 1.56 chs
545 1.56 chs default:
546 1.56 chs panic("spec_read type");
547 1.1 cgd }
548 1.56 chs /* NOTREACHED */
549 1.1 cgd }
550 1.1 cgd
551 1.1 cgd /*
552 1.1 cgd * Vnode op for write
553 1.1 cgd */
554 1.1 cgd /* ARGSUSED */
555 1.28 christos int
556 1.104 pooka spec_write(void *v)
557 1.28 christos {
558 1.15 mycroft struct vop_write_args /* {
559 1.15 mycroft struct vnode *a_vp;
560 1.15 mycroft struct uio *a_uio;
561 1.15 mycroft int a_ioflag;
562 1.87 elad kauth_cred_t a_cred;
563 1.28 christos } */ *ap = v;
564 1.48 augustss struct vnode *vp = ap->a_vp;
565 1.48 augustss struct uio *uio = ap->a_uio;
566 1.86 yamt struct lwp *l = curlwp;
567 1.56 chs struct buf *bp;
568 1.56 chs daddr_t bn;
569 1.59 chs int bsize, bscale;
570 1.56 chs struct partinfo dpart;
571 1.64 gehenna int n, on;
572 1.1 cgd int error = 0;
573 1.1 cgd
574 1.1 cgd #ifdef DIAGNOSTIC
575 1.1 cgd if (uio->uio_rw != UIO_WRITE)
576 1.1 cgd panic("spec_write mode");
577 1.86 yamt if (&uio->uio_vmspace->vm_map != kernel_map &&
578 1.86 yamt uio->uio_vmspace != curproc->p_vmspace)
579 1.1 cgd panic("spec_write proc");
580 1.1 cgd #endif
581 1.1 cgd
582 1.56 chs switch (vp->v_type) {
583 1.56 chs
584 1.56 chs case VCHR:
585 1.39 fvdl VOP_UNLOCK(vp, 0);
586 1.100 ad error = cdev_write(vp->v_rdev, uio, ap->a_ioflag);
587 1.39 fvdl vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
588 1.1 cgd return (error);
589 1.56 chs
590 1.56 chs case VBLK:
591 1.112 ad KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
592 1.56 chs if (uio->uio_resid == 0)
593 1.56 chs return (0);
594 1.56 chs if (uio->uio_offset < 0)
595 1.56 chs return (EINVAL);
596 1.56 chs bsize = BLKDEV_IOSIZE;
597 1.100 ad if (bdev_ioctl(vp->v_rdev, DIOCGPART, &dpart, FREAD, l) == 0) {
598 1.56 chs if (dpart.part->p_fstype == FS_BSDFFS &&
599 1.56 chs dpart.part->p_frag != 0 && dpart.part->p_fsize != 0)
600 1.56 chs bsize = dpart.part->p_frag *
601 1.56 chs dpart.part->p_fsize;
602 1.56 chs }
603 1.59 chs bscale = bsize >> DEV_BSHIFT;
604 1.56 chs do {
605 1.59 chs bn = (uio->uio_offset >> DEV_BSHIFT) &~ (bscale - 1);
606 1.56 chs on = uio->uio_offset % bsize;
607 1.56 chs n = min((unsigned)(bsize - on), uio->uio_resid);
608 1.56 chs if (n == bsize)
609 1.56 chs bp = getblk(vp, bn, bsize, 0, 0);
610 1.56 chs else
611 1.56 chs error = bread(vp, bn, bsize, NOCRED, &bp);
612 1.56 chs if (error) {
613 1.107 ad brelse(bp, 0);
614 1.56 chs return (error);
615 1.56 chs }
616 1.56 chs n = min(n, bsize - bp->b_resid);
617 1.56 chs error = uiomove((char *)bp->b_data + on, n, uio);
618 1.56 chs if (error)
619 1.107 ad brelse(bp, 0);
620 1.56 chs else {
621 1.56 chs if (n + on == bsize)
622 1.56 chs bawrite(bp);
623 1.56 chs else
624 1.56 chs bdwrite(bp);
625 1.107 ad error = bp->b_error;
626 1.56 chs }
627 1.56 chs } while (error == 0 && uio->uio_resid > 0 && n != 0);
628 1.56 chs return (error);
629 1.56 chs
630 1.56 chs default:
631 1.56 chs panic("spec_write type");
632 1.55 chs }
633 1.56 chs /* NOTREACHED */
634 1.1 cgd }
635 1.1 cgd
636 1.1 cgd /*
637 1.1 cgd * Device ioctl operation.
638 1.1 cgd */
639 1.1 cgd /* ARGSUSED */
640 1.28 christos int
641 1.104 pooka spec_ioctl(void *v)
642 1.28 christos {
643 1.15 mycroft struct vop_ioctl_args /* {
644 1.15 mycroft struct vnode *a_vp;
645 1.19 cgd u_long a_command;
646 1.78 jrf void *a_data;
647 1.15 mycroft int a_fflag;
648 1.87 elad kauth_cred_t a_cred;
649 1.28 christos } */ *ap = v;
650 1.83 chs struct vnode *vp;
651 1.83 chs dev_t dev;
652 1.1 cgd
653 1.83 chs /*
654 1.83 chs * Extract all the info we need from the vnode, taking care to
655 1.83 chs * avoid a race with VOP_REVOKE().
656 1.83 chs */
657 1.83 chs
658 1.83 chs vp = ap->a_vp;
659 1.83 chs dev = NODEV;
660 1.111 ad mutex_enter(&vp->v_interlock);
661 1.112 ad if ((vp->v_iflag & VI_XLOCK) == 0 && vp->v_specnode) {
662 1.83 chs dev = vp->v_rdev;
663 1.83 chs }
664 1.111 ad mutex_exit(&vp->v_interlock);
665 1.83 chs if (dev == NODEV) {
666 1.83 chs return ENXIO;
667 1.83 chs }
668 1.83 chs
669 1.83 chs switch (vp->v_type) {
670 1.1 cgd
671 1.1 cgd case VCHR:
672 1.100 ad return cdev_ioctl(dev, ap->a_command, ap->a_data,
673 1.109 pooka ap->a_fflag, curlwp);
674 1.1 cgd
675 1.1 cgd case VBLK:
676 1.112 ad KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
677 1.100 ad return bdev_ioctl(dev, ap->a_command, ap->a_data,
678 1.109 pooka ap->a_fflag, curlwp);
679 1.1 cgd
680 1.1 cgd default:
681 1.1 cgd panic("spec_ioctl");
682 1.1 cgd /* NOTREACHED */
683 1.1 cgd }
684 1.1 cgd }
685 1.1 cgd
686 1.1 cgd /* ARGSUSED */
687 1.28 christos int
688 1.104 pooka spec_poll(void *v)
689 1.28 christos {
690 1.32 mycroft struct vop_poll_args /* {
691 1.15 mycroft struct vnode *a_vp;
692 1.32 mycroft int a_events;
693 1.28 christos } */ *ap = v;
694 1.91 jld struct vnode *vp;
695 1.48 augustss dev_t dev;
696 1.1 cgd
697 1.91 jld /*
698 1.91 jld * Extract all the info we need from the vnode, taking care to
699 1.91 jld * avoid a race with VOP_REVOKE().
700 1.91 jld */
701 1.91 jld
702 1.91 jld vp = ap->a_vp;
703 1.91 jld dev = NODEV;
704 1.111 ad mutex_enter(&vp->v_interlock);
705 1.112 ad if ((vp->v_iflag & VI_XLOCK) == 0 && vp->v_specnode) {
706 1.91 jld dev = vp->v_rdev;
707 1.91 jld }
708 1.111 ad mutex_exit(&vp->v_interlock);
709 1.91 jld if (dev == NODEV) {
710 1.92 jld return POLLERR;
711 1.91 jld }
712 1.91 jld
713 1.91 jld switch (vp->v_type) {
714 1.1 cgd
715 1.1 cgd case VCHR:
716 1.109 pooka return cdev_poll(dev, ap->a_events, curlwp);
717 1.30 mycroft
718 1.30 mycroft default:
719 1.32 mycroft return (genfs_poll(v));
720 1.15 mycroft }
721 1.15 mycroft }
722 1.65 jdolecek
723 1.65 jdolecek /* ARGSUSED */
724 1.65 jdolecek int
725 1.104 pooka spec_kqfilter(void *v)
726 1.65 jdolecek {
727 1.65 jdolecek struct vop_kqfilter_args /* {
728 1.65 jdolecek struct vnode *a_vp;
729 1.65 jdolecek struct proc *a_kn;
730 1.65 jdolecek } */ *ap = v;
731 1.65 jdolecek dev_t dev;
732 1.65 jdolecek
733 1.65 jdolecek switch (ap->a_vp->v_type) {
734 1.65 jdolecek
735 1.65 jdolecek case VCHR:
736 1.65 jdolecek dev = ap->a_vp->v_rdev;
737 1.100 ad return cdev_kqfilter(dev, ap->a_kn);
738 1.65 jdolecek default:
739 1.65 jdolecek /*
740 1.65 jdolecek * Block devices don't support kqfilter, and refuse it
741 1.65 jdolecek * for any other files (like those vflush()ed) too.
742 1.65 jdolecek */
743 1.65 jdolecek return (EOPNOTSUPP);
744 1.65 jdolecek }
745 1.65 jdolecek }
746 1.65 jdolecek
747 1.15 mycroft /*
748 1.101 pooka * Allow mapping of only D_DISK. This is called only for VBLK.
749 1.101 pooka */
750 1.101 pooka int
751 1.104 pooka spec_mmap(void *v)
752 1.101 pooka {
753 1.101 pooka struct vop_mmap_args /* {
754 1.101 pooka struct vnode *a_vp;
755 1.102 pooka vm_prot_t a_prot;
756 1.101 pooka kauth_cred_t a_cred;
757 1.101 pooka } */ *ap = v;
758 1.101 pooka struct vnode *vp = ap->a_vp;
759 1.101 pooka
760 1.101 pooka KASSERT(vp->v_type == VBLK);
761 1.101 pooka if (bdev_type(vp->v_rdev) != D_DISK)
762 1.101 pooka return EINVAL;
763 1.101 pooka
764 1.101 pooka return 0;
765 1.101 pooka }
766 1.101 pooka
767 1.101 pooka /*
768 1.15 mycroft * Synch buffers associated with a block device
769 1.15 mycroft */
770 1.15 mycroft /* ARGSUSED */
771 1.15 mycroft int
772 1.104 pooka spec_fsync(void *v)
773 1.28 christos {
774 1.15 mycroft struct vop_fsync_args /* {
775 1.15 mycroft struct vnode *a_vp;
776 1.87 elad kauth_cred_t a_cred;
777 1.40 kleink int a_flags;
778 1.50 fvdl off_t offlo;
779 1.50 fvdl off_t offhi;
780 1.28 christos } */ *ap = v;
781 1.48 augustss struct vnode *vp = ap->a_vp;
782 1.15 mycroft
783 1.112 ad if (vp->v_type == VBLK) {
784 1.40 kleink vflushbuf(vp, (ap->a_flags & FSYNC_WAIT) != 0);
785 1.112 ad }
786 1.15 mycroft return (0);
787 1.1 cgd }
788 1.1 cgd
789 1.1 cgd /*
790 1.1 cgd * Just call the device strategy routine
791 1.1 cgd */
792 1.28 christos int
793 1.104 pooka spec_strategy(void *v)
794 1.28 christos {
795 1.15 mycroft struct vop_strategy_args /* {
796 1.76 hannken struct vnode *a_vp;
797 1.15 mycroft struct buf *a_bp;
798 1.28 christos } */ *ap = v;
799 1.76 hannken struct vnode *vp = ap->a_vp;
800 1.76 hannken struct buf *bp = ap->a_bp;
801 1.106 hannken int error;
802 1.1 cgd
803 1.112 ad KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
804 1.112 ad
805 1.79 hannken error = 0;
806 1.76 hannken bp->b_dev = vp->v_rdev;
807 1.45 fvdl if (!(bp->b_flags & B_READ) &&
808 1.105 pooka (LIST_FIRST(&bp->b_dep)) != NULL && bioopsp)
809 1.105 pooka bioopsp->io_start(bp);
810 1.77 hannken
811 1.106 hannken if (!(bp->b_flags & B_READ))
812 1.110 hannken error = fscow_run(bp, false);
813 1.77 hannken
814 1.79 hannken if (error) {
815 1.79 hannken bp->b_error = error;
816 1.79 hannken biodone(bp);
817 1.79 hannken return (error);
818 1.79 hannken }
819 1.79 hannken
820 1.100 ad bdev_strategy(bp);
821 1.76 hannken
822 1.1 cgd return (0);
823 1.1 cgd }
824 1.1 cgd
825 1.39 fvdl int
826 1.104 pooka spec_inactive(void *v)
827 1.39 fvdl {
828 1.39 fvdl struct vop_inactive_args /* {
829 1.39 fvdl struct vnode *a_vp;
830 1.85 christos struct proc *a_l;
831 1.39 fvdl } */ *ap = v;
832 1.39 fvdl
833 1.39 fvdl VOP_UNLOCK(ap->a_vp, 0);
834 1.39 fvdl return (0);
835 1.39 fvdl }
836 1.39 fvdl
837 1.1 cgd /*
838 1.1 cgd * This is a noop, simply returning what one has been given.
839 1.1 cgd */
840 1.28 christos int
841 1.104 pooka spec_bmap(void *v)
842 1.28 christos {
843 1.15 mycroft struct vop_bmap_args /* {
844 1.15 mycroft struct vnode *a_vp;
845 1.15 mycroft daddr_t a_bn;
846 1.15 mycroft struct vnode **a_vpp;
847 1.15 mycroft daddr_t *a_bnp;
848 1.39 fvdl int *a_runp;
849 1.28 christos } */ *ap = v;
850 1.1 cgd
851 1.15 mycroft if (ap->a_vpp != NULL)
852 1.15 mycroft *ap->a_vpp = ap->a_vp;
853 1.15 mycroft if (ap->a_bnp != NULL)
854 1.15 mycroft *ap->a_bnp = ap->a_bn;
855 1.39 fvdl if (ap->a_runp != NULL)
856 1.55 chs *ap->a_runp = (MAXBSIZE >> DEV_BSHIFT) - 1;
857 1.1 cgd return (0);
858 1.1 cgd }
859 1.1 cgd
860 1.1 cgd /*
861 1.1 cgd * Device close routine
862 1.1 cgd */
863 1.1 cgd /* ARGSUSED */
864 1.28 christos int
865 1.104 pooka spec_close(void *v)
866 1.28 christos {
867 1.15 mycroft struct vop_close_args /* {
868 1.15 mycroft struct vnode *a_vp;
869 1.15 mycroft int a_fflag;
870 1.87 elad kauth_cred_t a_cred;
871 1.28 christos } */ *ap = v;
872 1.48 augustss struct vnode *vp = ap->a_vp;
873 1.71 dsl struct session *sess;
874 1.1 cgd dev_t dev = vp->v_rdev;
875 1.112 ad int mode, error, flags, flags1, count;
876 1.112 ad specnode_t *sn;
877 1.112 ad specdev_t *sd;
878 1.44 wrstuden
879 1.108 ad flags = vp->v_iflag;
880 1.112 ad sn = vp->v_specnode;
881 1.112 ad sd = sn->sn_dev;
882 1.1 cgd
883 1.1 cgd switch (vp->v_type) {
884 1.1 cgd
885 1.1 cgd case VCHR:
886 1.11 cgd /*
887 1.11 cgd * Hack: a tty device that is a controlling terminal
888 1.112 ad * has a reference from the session structure. We
889 1.112 ad * cannot easily tell that a character device is a
890 1.112 ad * controlling terminal, unless it is the closing
891 1.112 ad * process' controlling terminal. In that case, if the
892 1.112 ad * open count is 1 release the reference from the
893 1.112 ad * session. Also, remove the link from the tty back to
894 1.112 ad * the session and pgrp.
895 1.112 ad *
896 1.112 ad * XXX V. fishy.
897 1.11 cgd */
898 1.100 ad mutex_enter(&proclist_lock);
899 1.112 ad sess = curlwp->l_proc->p_session;
900 1.112 ad if (sn->sn_opencnt == 1 && vp == sess->s_ttyvp) {
901 1.112 ad mutex_spin_enter(&tty_lock);
902 1.71 dsl sess->s_ttyvp = NULL;
903 1.72 pk if (sess->s_ttyp->t_session != NULL) {
904 1.72 pk sess->s_ttyp->t_pgrp = NULL;
905 1.72 pk sess->s_ttyp->t_session = NULL;
906 1.112 ad mutex_spin_exit(&tty_lock);
907 1.112 ad SESSRELE(sess);
908 1.100 ad mutex_exit(&proclist_lock);
909 1.100 ad } else {
910 1.112 ad mutex_spin_exit(&tty_lock);
911 1.100 ad if (sess->s_ttyp->t_pgrp != NULL)
912 1.100 ad panic("spec_close: spurious pgrp ref");
913 1.100 ad mutex_exit(&proclist_lock);
914 1.100 ad }
915 1.11 cgd vrele(vp);
916 1.100 ad } else
917 1.100 ad mutex_exit(&proclist_lock);
918 1.100 ad
919 1.1 cgd /*
920 1.1 cgd * If the vnode is locked, then we are in the midst
921 1.1 cgd * of forcably closing the device, otherwise we only
922 1.1 cgd * close on last reference.
923 1.1 cgd */
924 1.1 cgd mode = S_IFCHR;
925 1.1 cgd break;
926 1.1 cgd
927 1.1 cgd case VBLK:
928 1.112 ad KASSERT(vp == vp->v_specnode->sn_dev->sd_bdevvp);
929 1.1 cgd /*
930 1.1 cgd * On last close of a block device (that isn't mounted)
931 1.1 cgd * we must invalidate any in core blocks, so that
932 1.1 cgd * we can, for instance, change floppy disks.
933 1.1 cgd */
934 1.109 pooka error = vinvalbuf(vp, V_SAVE, ap->a_cred, curlwp, 0, 0);
935 1.28 christos if (error)
936 1.15 mycroft return (error);
937 1.1 cgd /*
938 1.1 cgd * We do not want to really close the device if it
939 1.1 cgd * is still in use unless we are trying to close it
940 1.1 cgd * forcibly. Since every use (buffer, vnode, swap, cmap)
941 1.1 cgd * holds a reference to the vnode, and because we mark
942 1.1 cgd * any other vnodes that alias this device, when the
943 1.1 cgd * sum of the reference counts on all the aliased
944 1.1 cgd * vnodes descends to one, we are on last close.
945 1.1 cgd */
946 1.1 cgd mode = S_IFBLK;
947 1.1 cgd break;
948 1.5 cgd
949 1.1 cgd default:
950 1.1 cgd panic("spec_close: not special");
951 1.1 cgd }
952 1.1 cgd
953 1.112 ad mutex_enter(&specfs_lock);
954 1.112 ad sn->sn_opencnt--;
955 1.112 ad count = --sd->sd_opencnt;
956 1.112 ad if (vp->v_type == VBLK)
957 1.112 ad sd->sd_bdevvp = NULL;
958 1.112 ad mutex_exit(&specfs_lock);
959 1.112 ad
960 1.112 ad if (count != 0)
961 1.112 ad return 0;
962 1.112 ad
963 1.44 wrstuden flags1 = ap->a_fflag;
964 1.44 wrstuden
965 1.44 wrstuden /*
966 1.108 ad * if VI_XLOCK is set, then we're going away soon, so make this
967 1.44 wrstuden * non-blocking. Also ensures that we won't wedge in vn_lock below.
968 1.44 wrstuden */
969 1.108 ad if (flags & VI_XLOCK)
970 1.44 wrstuden flags1 |= FNONBLOCK;
971 1.44 wrstuden
972 1.44 wrstuden /*
973 1.62 wiz * If we're able to block, release the vnode lock & reacquire. We
974 1.72 pk * might end up sleeping for someone else who wants our queues. They
975 1.108 ad * won't get them if we hold the vnode locked. Also, if VI_XLOCK is
976 1.100 ad * set, don't release the lock as we won't be able to regain it.
977 1.44 wrstuden */
978 1.44 wrstuden if (!(flags1 & FNONBLOCK))
979 1.44 wrstuden VOP_UNLOCK(vp, 0);
980 1.44 wrstuden
981 1.100 ad if (vp->v_type == VBLK)
982 1.109 pooka error = bdev_close(dev, flags1, mode, curlwp);
983 1.64 gehenna else
984 1.109 pooka error = cdev_close(dev, flags1, mode, curlwp);
985 1.44 wrstuden
986 1.44 wrstuden if (!(flags1 & FNONBLOCK))
987 1.44 wrstuden vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
988 1.44 wrstuden
989 1.44 wrstuden return (error);
990 1.1 cgd }
991 1.1 cgd
992 1.1 cgd /*
993 1.1 cgd * Print out the contents of a special device vnode.
994 1.1 cgd */
995 1.28 christos int
996 1.104 pooka spec_print(void *v)
997 1.28 christos {
998 1.15 mycroft struct vop_print_args /* {
999 1.15 mycroft struct vnode *a_vp;
1000 1.28 christos } */ *ap = v;
1001 1.15 mycroft
1002 1.112 ad printf("dev %d, %d\n", major(ap->a_vp->v_rdev),
1003 1.33 christos minor(ap->a_vp->v_rdev));
1004 1.28 christos return 0;
1005 1.15 mycroft }
1006 1.15 mycroft
1007 1.15 mycroft /*
1008 1.15 mycroft * Return POSIX pathconf information applicable to special devices.
1009 1.15 mycroft */
1010 1.28 christos int
1011 1.104 pooka spec_pathconf(void *v)
1012 1.28 christos {
1013 1.15 mycroft struct vop_pathconf_args /* {
1014 1.15 mycroft struct vnode *a_vp;
1015 1.15 mycroft int a_name;
1016 1.18 cgd register_t *a_retval;
1017 1.28 christos } */ *ap = v;
1018 1.1 cgd
1019 1.15 mycroft switch (ap->a_name) {
1020 1.15 mycroft case _PC_LINK_MAX:
1021 1.15 mycroft *ap->a_retval = LINK_MAX;
1022 1.15 mycroft return (0);
1023 1.15 mycroft case _PC_MAX_CANON:
1024 1.15 mycroft *ap->a_retval = MAX_CANON;
1025 1.15 mycroft return (0);
1026 1.15 mycroft case _PC_MAX_INPUT:
1027 1.15 mycroft *ap->a_retval = MAX_INPUT;
1028 1.15 mycroft return (0);
1029 1.15 mycroft case _PC_PIPE_BUF:
1030 1.15 mycroft *ap->a_retval = PIPE_BUF;
1031 1.15 mycroft return (0);
1032 1.15 mycroft case _PC_CHOWN_RESTRICTED:
1033 1.15 mycroft *ap->a_retval = 1;
1034 1.15 mycroft return (0);
1035 1.15 mycroft case _PC_VDISABLE:
1036 1.15 mycroft *ap->a_retval = _POSIX_VDISABLE;
1037 1.41 kleink return (0);
1038 1.41 kleink case _PC_SYNC_IO:
1039 1.41 kleink *ap->a_retval = 1;
1040 1.15 mycroft return (0);
1041 1.15 mycroft default:
1042 1.15 mycroft return (EINVAL);
1043 1.15 mycroft }
1044 1.1 cgd /* NOTREACHED */
1045 1.35 kleink }
1046 1.35 kleink
1047 1.80 perry /*
1048 1.35 kleink * Advisory record locking support.
1049 1.35 kleink */
1050 1.35 kleink int
1051 1.104 pooka spec_advlock(void *v)
1052 1.35 kleink {
1053 1.35 kleink struct vop_advlock_args /* {
1054 1.35 kleink struct vnode *a_vp;
1055 1.78 jrf void *a_id;
1056 1.35 kleink int a_op;
1057 1.35 kleink struct flock *a_fl;
1058 1.35 kleink int a_flags;
1059 1.35 kleink } */ *ap = v;
1060 1.48 augustss struct vnode *vp = ap->a_vp;
1061 1.35 kleink
1062 1.49 jdolecek return lf_advlock(ap, &vp->v_speclockf, (off_t)0);
1063 1.1 cgd }
1064