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