xref: /dflybsd-src/sys/vfs/devfs/devfs_vnops.c (revision ae23dbdb346411139ff33e1c4e06d60a5b825344)
1 /*
2  * Copyright (c) 2009 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Alex Hornung <ahornung@gmail.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/time.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/fcntl.h>
40 #include <sys/proc.h>
41 #include <sys/priv.h>
42 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
44 #include <sys/uio.h>
45 #include <sys/mount.h>
46 #include <sys/file.h>
47 #include <sys/fcntl.h>
48 #include <sys/namei.h>
49 #include <sys/dirent.h>
50 #include <sys/malloc.h>
51 #include <sys/stat.h>
52 #include <sys/reg.h>
53 #include <vm/vm_pager.h>
54 #include <vm/vm_zone.h>
55 #include <vm/vm_object.h>
56 #include <sys/filio.h>
57 #include <sys/ttycom.h>
58 #include <sys/tty.h>
59 #include <sys/diskslice.h>
60 #include <sys/sysctl.h>
61 #include <sys/devfs.h>
62 #include <sys/pioctl.h>
63 #include <vfs/fifofs/fifo.h>
64 
65 #include <machine/limits.h>
66 
67 #include <sys/buf2.h>
68 #include <sys/sysref2.h>
69 #include <sys/mplock2.h>
70 #include <vm/vm_page2.h>
71 
72 MALLOC_DECLARE(M_DEVFS);
73 #define DEVFS_BADOP	(void *)devfs_badop
74 
75 static int devfs_badop(struct vop_generic_args *);
76 static int devfs_access(struct vop_access_args *);
77 static int devfs_inactive(struct vop_inactive_args *);
78 static int devfs_reclaim(struct vop_reclaim_args *);
79 static int devfs_readdir(struct vop_readdir_args *);
80 static int devfs_getattr(struct vop_getattr_args *);
81 static int devfs_setattr(struct vop_setattr_args *);
82 static int devfs_readlink(struct vop_readlink_args *);
83 static int devfs_print(struct vop_print_args *);
84 
85 static int devfs_nresolve(struct vop_nresolve_args *);
86 static int devfs_nlookupdotdot(struct vop_nlookupdotdot_args *);
87 static int devfs_nmkdir(struct vop_nmkdir_args *);
88 static int devfs_nsymlink(struct vop_nsymlink_args *);
89 static int devfs_nrmdir(struct vop_nrmdir_args *);
90 static int devfs_nremove(struct vop_nremove_args *);
91 
92 static int devfs_spec_open(struct vop_open_args *);
93 static int devfs_spec_close(struct vop_close_args *);
94 static int devfs_spec_fsync(struct vop_fsync_args *);
95 
96 static int devfs_spec_read(struct vop_read_args *);
97 static int devfs_spec_write(struct vop_write_args *);
98 static int devfs_spec_ioctl(struct vop_ioctl_args *);
99 static int devfs_spec_kqfilter(struct vop_kqfilter_args *);
100 static int devfs_spec_strategy(struct vop_strategy_args *);
101 static void devfs_spec_strategy_done(struct bio *);
102 static int devfs_spec_freeblks(struct vop_freeblks_args *);
103 static int devfs_spec_bmap(struct vop_bmap_args *);
104 static int devfs_spec_advlock(struct vop_advlock_args *);
105 static void devfs_spec_getpages_iodone(struct bio *);
106 static int devfs_spec_getpages(struct vop_getpages_args *);
107 
108 
109 static int devfs_specf_close(struct file *);
110 static int devfs_specf_read(struct file *, struct uio *, struct ucred *, int);
111 static int devfs_specf_write(struct file *, struct uio *, struct ucred *, int);
112 static int devfs_specf_stat(struct file *, struct stat *, struct ucred *);
113 static int devfs_specf_kqfilter(struct file *, struct knote *);
114 static int devfs_specf_ioctl(struct file *, u_long, caddr_t,
115 				struct ucred *, struct sysmsg *);
116 static __inline int sequential_heuristic(struct uio *, struct file *);
117 
118 extern struct lock devfs_lock;
119 
120 static int mpsafe_reads, mpsafe_writes, mplock_reads, mplock_writes;
121 
122 /*
123  * devfs vnode operations for regular files
124  */
125 struct vop_ops devfs_vnode_norm_vops = {
126 	.vop_default =		vop_defaultop,
127 	.vop_access =		devfs_access,
128 	.vop_advlock =		DEVFS_BADOP,
129 	.vop_bmap =			DEVFS_BADOP,
130 	.vop_close =		vop_stdclose,
131 	.vop_getattr =		devfs_getattr,
132 	.vop_inactive =		devfs_inactive,
133 	.vop_ncreate =		DEVFS_BADOP,
134 	.vop_nresolve =		devfs_nresolve,
135 	.vop_nlookupdotdot =	devfs_nlookupdotdot,
136 	.vop_nlink =		DEVFS_BADOP,
137 	.vop_nmkdir =		devfs_nmkdir,
138 	.vop_nmknod =		DEVFS_BADOP,
139 	.vop_nremove =		devfs_nremove,
140 	.vop_nrename =		DEVFS_BADOP,
141 	.vop_nrmdir =		devfs_nrmdir,
142 	.vop_nsymlink =		devfs_nsymlink,
143 	.vop_open =			vop_stdopen,
144 	.vop_pathconf =		vop_stdpathconf,
145 	.vop_print =		devfs_print,
146 	.vop_read =			DEVFS_BADOP,
147 	.vop_readdir =		devfs_readdir,
148 	.vop_readlink =		devfs_readlink,
149 	.vop_reclaim =		devfs_reclaim,
150 	.vop_setattr =		devfs_setattr,
151 	.vop_write =		DEVFS_BADOP,
152 	.vop_ioctl =		DEVFS_BADOP
153 };
154 
155 /*
156  * devfs vnode operations for character devices
157  */
158 struct vop_ops devfs_vnode_dev_vops = {
159 	.vop_default =		vop_defaultop,
160 	.vop_access =		devfs_access,
161 	.vop_advlock =		devfs_spec_advlock,
162 	.vop_bmap =			devfs_spec_bmap,
163 	.vop_close =		devfs_spec_close,
164 	.vop_freeblks =		devfs_spec_freeblks,
165 	.vop_fsync =		devfs_spec_fsync,
166 	.vop_getattr =		devfs_getattr,
167 	.vop_getpages =		devfs_spec_getpages,
168 	.vop_inactive =		devfs_inactive,
169 	.vop_open =			devfs_spec_open,
170 	.vop_pathconf =		vop_stdpathconf,
171 	.vop_print =		devfs_print,
172 	.vop_kqfilter =		devfs_spec_kqfilter,
173 	.vop_read =			devfs_spec_read,
174 	.vop_readdir =		DEVFS_BADOP,
175 	.vop_readlink =		DEVFS_BADOP,
176 	.vop_reclaim =		devfs_reclaim,
177 	.vop_setattr =		devfs_setattr,
178 	.vop_strategy =		devfs_spec_strategy,
179 	.vop_write =		devfs_spec_write,
180 	.vop_ioctl =		devfs_spec_ioctl
181 };
182 
183 struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops;
184 
185 struct fileops devfs_dev_fileops = {
186 	.fo_read = devfs_specf_read,
187 	.fo_write = devfs_specf_write,
188 	.fo_ioctl = devfs_specf_ioctl,
189 	.fo_kqfilter = devfs_specf_kqfilter,
190 	.fo_stat = devfs_specf_stat,
191 	.fo_close = devfs_specf_close,
192 	.fo_shutdown = nofo_shutdown
193 };
194 
195 /*
196  * These two functions are possibly temporary hacks for
197  * devices (aka the pty code) which want to control the
198  * node attributes themselves.
199  *
200  * XXX we may ultimately desire to simply remove the uid/gid/mode
201  * from the node entirely.
202  */
203 static __inline void
204 node_sync_dev_get(struct devfs_node *node)
205 {
206 	cdev_t dev;
207 
208 	if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
209 		node->uid = dev->si_uid;
210 		node->gid = dev->si_gid;
211 		node->mode = dev->si_perms;
212 	}
213 }
214 
215 static __inline void
216 node_sync_dev_set(struct devfs_node *node)
217 {
218 	cdev_t dev;
219 
220 	if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
221 		dev->si_uid = node->uid;
222 		dev->si_gid = node->gid;
223 		dev->si_perms = node->mode;
224 	}
225 }
226 
227 /*
228  * generic entry point for unsupported operations
229  */
230 static int
231 devfs_badop(struct vop_generic_args *ap)
232 {
233 	return (EIO);
234 }
235 
236 
237 static int
238 devfs_access(struct vop_access_args *ap)
239 {
240 	struct devfs_node *node = DEVFS_NODE(ap->a_vp);
241 	int error;
242 
243 	if (!devfs_node_is_accessible(node))
244 		return ENOENT;
245 	node_sync_dev_get(node);
246 	error = vop_helper_access(ap, node->uid, node->gid,
247 				  node->mode, node->flags);
248 
249 	return error;
250 }
251 
252 
253 static int
254 devfs_inactive(struct vop_inactive_args *ap)
255 {
256 	struct devfs_node *node = DEVFS_NODE(ap->a_vp);
257 
258 	if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0)
259 		vrecycle(ap->a_vp);
260 	return 0;
261 }
262 
263 
264 static int
265 devfs_reclaim(struct vop_reclaim_args *ap)
266 {
267 	struct devfs_node *node;
268 	struct vnode *vp;
269 	int locked;
270 
271 	/*
272 	 * Check if it is locked already. if not, we acquire the devfs lock
273 	 */
274 	if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
275 		lockmgr(&devfs_lock, LK_EXCLUSIVE);
276 		locked = 1;
277 	} else {
278 		locked = 0;
279 	}
280 
281 	/*
282 	 * Get rid of the devfs_node if it is no longer linked into the
283 	 * topology.
284 	 */
285 	vp = ap->a_vp;
286 	if ((node = DEVFS_NODE(vp)) != NULL) {
287 		node->v_node = NULL;
288 		if ((node->flags & DEVFS_NODE_LINKED) == 0)
289 			devfs_freep(node);
290 	}
291 
292 	if (locked)
293 		lockmgr(&devfs_lock, LK_RELEASE);
294 
295 	/*
296 	 * v_rdev needs to be properly released using v_release_rdev
297 	 * Make sure v_data is NULL as well.
298 	 */
299 	vp->v_data = NULL;
300 	v_release_rdev(vp);
301 	return 0;
302 }
303 
304 
305 static int
306 devfs_readdir(struct vop_readdir_args *ap)
307 {
308 	struct devfs_node *dnode = DEVFS_NODE(ap->a_vp);
309 	struct devfs_node *node;
310 	int cookie_index;
311 	int ncookies;
312 	int error2;
313 	int error;
314 	int r;
315 	off_t *cookies;
316 	off_t saveoff;
317 
318 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
319 
320 	if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
321 		return (EINVAL);
322 	if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
323 		return (error);
324 
325 	if (!devfs_node_is_accessible(dnode)) {
326 		vn_unlock(ap->a_vp);
327 		return ENOENT;
328 	}
329 
330 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
331 
332 	saveoff = ap->a_uio->uio_offset;
333 
334 	if (ap->a_ncookies) {
335 		ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
336 		if (ncookies > 256)
337 			ncookies = 256;
338 		cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
339 		cookie_index = 0;
340 	} else {
341 		ncookies = -1;
342 		cookies = NULL;
343 		cookie_index = 0;
344 	}
345 
346 	nanotime(&dnode->atime);
347 
348 	if (saveoff == 0) {
349 		r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino,
350 				     DT_DIR, 1, ".");
351 		if (r)
352 			goto done;
353 		if (cookies)
354 			cookies[cookie_index] = saveoff;
355 		saveoff++;
356 		cookie_index++;
357 		if (cookie_index == ncookies)
358 			goto done;
359 	}
360 
361 	if (saveoff == 1) {
362 		if (dnode->parent) {
363 			r = vop_write_dirent(&error, ap->a_uio,
364 					     dnode->parent->d_dir.d_ino,
365 					     DT_DIR, 2, "..");
366 		} else {
367 			r = vop_write_dirent(&error, ap->a_uio,
368 					     dnode->d_dir.d_ino,
369 					     DT_DIR, 2, "..");
370 		}
371 		if (r)
372 			goto done;
373 		if (cookies)
374 			cookies[cookie_index] = saveoff;
375 		saveoff++;
376 		cookie_index++;
377 		if (cookie_index == ncookies)
378 			goto done;
379 	}
380 
381 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
382 		if ((node->flags & DEVFS_HIDDEN) ||
383 		    (node->flags & DEVFS_INVISIBLE)) {
384 			continue;
385 		}
386 
387 		/*
388 		 * If the node type is a valid devfs alias, then we make sure that the
389 		 * target isn't hidden. If it is, we don't show the link in the
390 		 * directory listing.
391 		 */
392 		if ((node->node_type == Plink) && (node->link_target != NULL) &&
393 			(node->link_target->flags & DEVFS_HIDDEN))
394 			continue;
395 
396 		if (node->cookie < saveoff)
397 			continue;
398 
399 		saveoff = node->cookie;
400 
401 		error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino,
402 					  node->d_dir.d_type,
403 					  node->d_dir.d_namlen,
404 					  node->d_dir.d_name);
405 
406 		if (error2)
407 			break;
408 
409 		saveoff++;
410 
411 		if (cookies)
412 			cookies[cookie_index] = node->cookie;
413 		++cookie_index;
414 		if (cookie_index == ncookies)
415 			break;
416 	}
417 
418 done:
419 	lockmgr(&devfs_lock, LK_RELEASE);
420 	vn_unlock(ap->a_vp);
421 
422 	ap->a_uio->uio_offset = saveoff;
423 	if (error && cookie_index == 0) {
424 		if (cookies) {
425 			kfree(cookies, M_TEMP);
426 			*ap->a_ncookies = 0;
427 			*ap->a_cookies = NULL;
428 		}
429 	} else {
430 		if (cookies) {
431 			*ap->a_ncookies = cookie_index;
432 			*ap->a_cookies = cookies;
433 		}
434 	}
435 	return (error);
436 }
437 
438 
439 static int
440 devfs_nresolve(struct vop_nresolve_args *ap)
441 {
442 	struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
443 	struct devfs_node *node, *found = NULL;
444 	struct namecache *ncp;
445 	struct vnode *vp = NULL;
446 	int error = 0;
447 	int len;
448 	int depth;
449 
450 	ncp = ap->a_nch->ncp;
451 	len = ncp->nc_nlen;
452 
453 	if (!devfs_node_is_accessible(dnode))
454 		return ENOENT;
455 
456 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
457 
458 	if ((dnode->node_type != Proot) && (dnode->node_type != Pdir)) {
459 		error = ENOENT;
460 		cache_setvp(ap->a_nch, NULL);
461 		goto out;
462 	}
463 
464 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
465 		if (len == node->d_dir.d_namlen) {
466 			if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
467 				found = node;
468 				break;
469 			}
470 		}
471 	}
472 
473 	if (found) {
474 		depth = 0;
475 		while ((found->node_type == Plink) && (found->link_target)) {
476 			if (depth >= 8) {
477 				devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
478 				break;
479 			}
480 
481 			found = found->link_target;
482 			++depth;
483 		}
484 
485 		if (!(found->flags & DEVFS_HIDDEN))
486 			devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
487 	}
488 
489 	if (vp == NULL) {
490 		error = ENOENT;
491 		cache_setvp(ap->a_nch, NULL);
492 		goto out;
493 
494 	}
495 	KKASSERT(vp);
496 	vn_unlock(vp);
497 	cache_setvp(ap->a_nch, vp);
498 	vrele(vp);
499 out:
500 	lockmgr(&devfs_lock, LK_RELEASE);
501 
502 	return error;
503 }
504 
505 
506 static int
507 devfs_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
508 {
509 	struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
510 
511 	*ap->a_vpp = NULL;
512 	if (!devfs_node_is_accessible(dnode))
513 		return ENOENT;
514 
515 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
516 	if (dnode->parent != NULL) {
517 		devfs_allocv(ap->a_vpp, dnode->parent);
518 		vn_unlock(*ap->a_vpp);
519 	}
520 	lockmgr(&devfs_lock, LK_RELEASE);
521 
522 	return ((*ap->a_vpp == NULL) ? ENOENT : 0);
523 }
524 
525 
526 static int
527 devfs_getattr(struct vop_getattr_args *ap)
528 {
529 	struct devfs_node *node = DEVFS_NODE(ap->a_vp);
530 	struct vattr *vap = ap->a_vap;
531 	struct partinfo pinfo;
532 	int error = 0;
533 
534 #if 0
535 	if (!devfs_node_is_accessible(node))
536 		return ENOENT;
537 #endif
538 	node_sync_dev_get(node);
539 
540 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
541 
542 	/* start by zeroing out the attributes */
543 	VATTR_NULL(vap);
544 
545 	/* next do all the common fields */
546 	vap->va_type = ap->a_vp->v_type;
547 	vap->va_mode = node->mode;
548 	vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
549 	vap->va_flags = 0;
550 	vap->va_blocksize = DEV_BSIZE;
551 	vap->va_bytes = vap->va_size = 0;
552 
553 	vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
554 
555 	vap->va_atime = node->atime;
556 	vap->va_mtime = node->mtime;
557 	vap->va_ctime = node->ctime;
558 
559 	vap->va_nlink = 1; /* number of references to file */
560 
561 	vap->va_uid = node->uid;
562 	vap->va_gid = node->gid;
563 
564 	vap->va_rmajor = 0;
565 	vap->va_rminor = 0;
566 
567 	if ((node->node_type == Pdev) && node->d_dev)  {
568 		reference_dev(node->d_dev);
569 		vap->va_rminor = node->d_dev->si_uminor;
570 		release_dev(node->d_dev);
571 	}
572 
573 	/* For a softlink the va_size is the length of the softlink */
574 	if (node->symlink_name != 0) {
575 		vap->va_bytes = vap->va_size = node->symlink_namelen;
576 	}
577 
578 	/*
579 	 * For a disk-type device, va_size is the size of the underlying
580 	 * device, so that lseek() works properly.
581 	 */
582 	if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) {
583 		bzero(&pinfo, sizeof(pinfo));
584 		error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo,
585 				   0, proc0.p_ucred, NULL);
586 		if ((error == 0) && (pinfo.media_blksize != 0)) {
587 			vap->va_size = pinfo.media_size;
588 		} else {
589 			vap->va_size = 0;
590 			error = 0;
591 		}
592 	}
593 
594 	lockmgr(&devfs_lock, LK_RELEASE);
595 
596 	return (error);
597 }
598 
599 
600 static int
601 devfs_setattr(struct vop_setattr_args *ap)
602 {
603 	struct devfs_node *node = DEVFS_NODE(ap->a_vp);
604 	struct vattr *vap;
605 	int error = 0;
606 
607 	if (!devfs_node_is_accessible(node))
608 		return ENOENT;
609 	node_sync_dev_get(node);
610 
611 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
612 
613 	vap = ap->a_vap;
614 
615 	if (vap->va_uid != (uid_t)VNOVAL) {
616 		if ((ap->a_cred->cr_uid != node->uid) &&
617 		    (!groupmember(node->gid, ap->a_cred))) {
618 			error = priv_check(curthread, PRIV_VFS_CHOWN);
619 			if (error)
620 				goto out;
621 		}
622 		node->uid = vap->va_uid;
623 	}
624 
625 	if (vap->va_gid != (uid_t)VNOVAL) {
626 		if ((ap->a_cred->cr_uid != node->uid) &&
627 		    (!groupmember(node->gid, ap->a_cred))) {
628 			error = priv_check(curthread, PRIV_VFS_CHOWN);
629 			if (error)
630 				goto out;
631 		}
632 		node->gid = vap->va_gid;
633 	}
634 
635 	if (vap->va_mode != (mode_t)VNOVAL) {
636 		if (ap->a_cred->cr_uid != node->uid) {
637 			error = priv_check(curthread, PRIV_VFS_ADMIN);
638 			if (error)
639 				goto out;
640 		}
641 		node->mode = vap->va_mode;
642 	}
643 
644 out:
645 	node_sync_dev_set(node);
646 	nanotime(&node->ctime);
647 	lockmgr(&devfs_lock, LK_RELEASE);
648 
649 	return error;
650 }
651 
652 
653 static int
654 devfs_readlink(struct vop_readlink_args *ap)
655 {
656 	struct devfs_node *node = DEVFS_NODE(ap->a_vp);
657 	int ret;
658 
659 	if (!devfs_node_is_accessible(node))
660 		return ENOENT;
661 
662 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
663 	ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
664 	lockmgr(&devfs_lock, LK_RELEASE);
665 
666 	return ret;
667 }
668 
669 
670 static int
671 devfs_print(struct vop_print_args *ap)
672 {
673 	return (0);
674 }
675 
676 static int
677 devfs_nmkdir(struct vop_nmkdir_args *ap)
678 {
679 	struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
680 	struct devfs_node *node;
681 
682 	if (!devfs_node_is_accessible(dnode))
683 		return ENOENT;
684 
685 	if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
686 		goto out;
687 
688 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
689 	devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Pdir,
690 		      ap->a_nch->ncp->nc_name, dnode, NULL);
691 
692 	if (*ap->a_vpp) {
693 		node = DEVFS_NODE(*ap->a_vpp);
694 		node->flags |= DEVFS_USER_CREATED;
695 		cache_setunresolved(ap->a_nch);
696 		cache_setvp(ap->a_nch, *ap->a_vpp);
697 	}
698 	lockmgr(&devfs_lock, LK_RELEASE);
699 out:
700 	return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
701 }
702 
703 static int
704 devfs_nsymlink(struct vop_nsymlink_args *ap)
705 {
706 	struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
707 	struct devfs_node *node;
708 	size_t targetlen;
709 
710 	if (!devfs_node_is_accessible(dnode))
711 		return ENOENT;
712 
713 	ap->a_vap->va_type = VLNK;
714 
715 	if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
716 		goto out;
717 
718 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
719 	devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Plink,
720 		      ap->a_nch->ncp->nc_name, dnode, NULL);
721 
722 	targetlen = strlen(ap->a_target);
723 	if (*ap->a_vpp) {
724 		node = DEVFS_NODE(*ap->a_vpp);
725 		node->flags |= DEVFS_USER_CREATED;
726 		node->symlink_namelen = targetlen;
727 		node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
728 		memcpy(node->symlink_name, ap->a_target, targetlen);
729 		node->symlink_name[targetlen] = '\0';
730 		cache_setunresolved(ap->a_nch);
731 		cache_setvp(ap->a_nch, *ap->a_vpp);
732 	}
733 	lockmgr(&devfs_lock, LK_RELEASE);
734 out:
735 	return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
736 }
737 
738 static int
739 devfs_nrmdir(struct vop_nrmdir_args *ap)
740 {
741 	struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
742 	struct devfs_node *node;
743 	struct namecache *ncp;
744 	int error = ENOENT;
745 
746 	ncp = ap->a_nch->ncp;
747 
748 	if (!devfs_node_is_accessible(dnode))
749 		return ENOENT;
750 
751 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
752 
753 	if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
754 		goto out;
755 
756 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
757 		if (ncp->nc_nlen != node->d_dir.d_namlen)
758 			continue;
759 		if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
760 			continue;
761 
762 		/*
763 		 * only allow removal of user created dirs
764 		 */
765 		if ((node->flags & DEVFS_USER_CREATED) == 0) {
766 			error = EPERM;
767 			goto out;
768 		} else if (node->node_type != Pdir) {
769 			error = ENOTDIR;
770 			goto out;
771 		} else if (node->nchildren > 2) {
772 			error = ENOTEMPTY;
773 			goto out;
774 		} else {
775 			if (node->v_node)
776 				cache_inval_vp(node->v_node, CINV_DESTROY);
777 			devfs_unlinkp(node);
778 			error = 0;
779 			break;
780 		}
781 	}
782 
783 	cache_setunresolved(ap->a_nch);
784 	cache_setvp(ap->a_nch, NULL);
785 
786 out:
787 	lockmgr(&devfs_lock, LK_RELEASE);
788 	return error;
789 }
790 
791 static int
792 devfs_nremove(struct vop_nremove_args *ap)
793 {
794 	struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
795 	struct devfs_node *node;
796 	struct namecache *ncp;
797 	int error = ENOENT;
798 
799 	ncp = ap->a_nch->ncp;
800 
801 	if (!devfs_node_is_accessible(dnode))
802 		return ENOENT;
803 
804 	lockmgr(&devfs_lock, LK_EXCLUSIVE);
805 
806 	if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
807 		goto out;
808 
809 	TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
810 		if (ncp->nc_nlen != node->d_dir.d_namlen)
811 			continue;
812 		if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
813 			continue;
814 
815 		/*
816 		 * only allow removal of user created stuff (e.g. symlinks)
817 		 */
818 		if ((node->flags & DEVFS_USER_CREATED) == 0) {
819 			error = EPERM;
820 			goto out;
821 		} else if (node->node_type == Pdir) {
822 			error = EISDIR;
823 			goto out;
824 		} else {
825 			if (node->v_node)
826 				cache_inval_vp(node->v_node, CINV_DESTROY);
827 			devfs_unlinkp(node);
828 			error = 0;
829 			break;
830 		}
831 	}
832 
833 	cache_setunresolved(ap->a_nch);
834 	cache_setvp(ap->a_nch, NULL);
835 
836 out:
837 	lockmgr(&devfs_lock, LK_RELEASE);
838 	return error;
839 }
840 
841 
842 static int
843 devfs_spec_open(struct vop_open_args *ap)
844 {
845 	struct vnode *vp = ap->a_vp;
846 	struct vnode *orig_vp = NULL;
847 	struct devfs_node *node = DEVFS_NODE(vp);
848 	struct devfs_node *newnode;
849 	cdev_t dev, ndev = NULL;
850 	int error = 0;
851 
852 	if (node) {
853 		if (node->d_dev == NULL)
854 			return ENXIO;
855 		if (!devfs_node_is_accessible(node))
856 			return ENOENT;
857 	}
858 
859 	if ((dev = vp->v_rdev) == NULL)
860 		return ENXIO;
861 
862 	if (node && ap->a_fp) {
863 		devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
864 		lockmgr(&devfs_lock, LK_EXCLUSIVE);
865 
866 		ndev = devfs_clone(dev, node->d_dir.d_name, node->d_dir.d_namlen,
867 						ap->a_mode, ap->a_cred);
868 		if (ndev != NULL) {
869 			newnode = devfs_create_device_node(
870 					DEVFS_MNTDATA(vp->v_mount)->root_node,
871 					ndev, NULL, NULL);
872 			/* XXX: possibly destroy device if this happens */
873 
874 			if (newnode != NULL) {
875 				dev = ndev;
876 				devfs_link_dev(dev);
877 
878 				devfs_debug(DEVFS_DEBUG_DEBUG,
879 						"parent here is: %s, node is: |%s|\n",
880 						((node->parent->node_type == Proot) ?
881 						"ROOT!" : node->parent->d_dir.d_name),
882 						newnode->d_dir.d_name);
883 				devfs_debug(DEVFS_DEBUG_DEBUG,
884 						"test: %s\n",
885 						((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
886 
887 				/*
888 				 * orig_vp is set to the original vp if we cloned.
889 				 */
890 				/* node->flags |= DEVFS_CLONED; */
891 				devfs_allocv(&vp, newnode);
892 				orig_vp = ap->a_vp;
893 				ap->a_vp = vp;
894 			}
895 		}
896 		lockmgr(&devfs_lock, LK_RELEASE);
897 	}
898 
899 	devfs_debug(DEVFS_DEBUG_DEBUG,
900 		    "devfs_spec_open() called on %s! \n",
901 		    dev->si_name);
902 
903 	/*
904 	 * Make this field valid before any I/O in ->d_open
905 	 */
906 	if (!dev->si_iosize_max)
907 		dev->si_iosize_max = DFLTPHYS;
908 
909 	if (dev_dflags(dev) & D_TTY)
910 		vsetflags(vp, VISTTY);
911 
912 	vn_unlock(vp);
913 	error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred);
914 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
915 
916 	/*
917 	 * Clean up any cloned vp if we error out.
918 	 */
919 	if (error) {
920 		if (orig_vp) {
921 			vput(vp);
922 			ap->a_vp = orig_vp;
923 			/* orig_vp = NULL; */
924 		}
925 		return error;
926 	}
927 
928 	/*
929 	 * This checks if the disk device is going to be opened for writing.
930 	 * It will be only allowed in the cases where securelevel permits it
931 	 * and it's not mounted R/W.
932 	 */
933 	if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
934 	    (ap->a_cred != FSCRED)) {
935 
936 		/* Very secure mode. No open for writing allowed */
937 		if (securelevel >= 2)
938 			return EPERM;
939 
940 		/*
941 		 * If it is mounted R/W, do not allow to open for writing.
942 		 * In the case it's mounted read-only but securelevel
943 		 * is >= 1, then do not allow opening for writing either.
944 		 */
945 		if (vfs_mountedon(vp)) {
946 			if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
947 				return EBUSY;
948 			else if (securelevel >= 1)
949 				return EPERM;
950 		}
951 	}
952 
953 	if (dev_dflags(dev) & D_TTY) {
954 		if (dev->si_tty) {
955 			struct tty *tp;
956 			tp = dev->si_tty;
957 			if (!tp->t_stop) {
958 				devfs_debug(DEVFS_DEBUG_DEBUG,
959 					    "devfs: no t_stop\n");
960 				tp->t_stop = nottystop;
961 			}
962 		}
963 	}
964 
965 
966 	if (vn_isdisk(vp, NULL)) {
967 		if (!dev->si_bsize_phys)
968 			dev->si_bsize_phys = DEV_BSIZE;
969 		vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
970 	}
971 
972 	vop_stdopen(ap);
973 #if 0
974 	if (node)
975 		nanotime(&node->atime);
976 #endif
977 
978 	/*
979 	 * If we replaced the vp the vop_stdopen() call will have loaded
980 	 * it into fp->f_data and vref()d the vp, giving us two refs.  So
981 	 * instead of just unlocking it here we have to vput() it.
982 	 */
983 	if (orig_vp)
984 		vput(vp);
985 
986 	/* Ugly pty magic, to make pty devices appear once they are opened */
987 	if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY)
988 		node->flags &= ~DEVFS_INVISIBLE;
989 
990 	if (ap->a_fp) {
991 		KKASSERT(ap->a_fp->f_type == DTYPE_VNODE);
992 		KKASSERT(ap->a_fp->f_flag == (ap->a_mode & FMASK));
993 		ap->a_fp->f_ops = &devfs_dev_fileops;
994 		KKASSERT(ap->a_fp->f_data == (void *)vp);
995 	}
996 
997 	return 0;
998 }
999 
1000 
1001 static int
1002 devfs_spec_close(struct vop_close_args *ap)
1003 {
1004 	struct devfs_node *node = DEVFS_NODE(ap->a_vp);
1005 	struct proc *p = curproc;
1006 	struct vnode *vp = ap->a_vp;
1007 	cdev_t dev = vp->v_rdev;
1008 	int error = 0;
1009 	int needrelock;
1010 
1011 	devfs_debug(DEVFS_DEBUG_DEBUG,
1012 		    "devfs_spec_close() called on %s! \n",
1013 		    dev->si_name);
1014 
1015 	/*
1016 	 * A couple of hacks for devices and tty devices.  The
1017 	 * vnode ref count cannot be used to figure out the
1018 	 * last close, but we can use v_opencount now that
1019 	 * revoke works properly.
1020 	 *
1021 	 * Detect the last close on a controlling terminal and clear
1022 	 * the session (half-close).
1023 	 */
1024 	if (dev)
1025 		reference_dev(dev);
1026 
1027 	if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
1028 		p->p_session->s_ttyvp = NULL;
1029 		vrele(vp);
1030 	}
1031 
1032 	/*
1033 	 * Vnodes can be opened and closed multiple times.  Do not really
1034 	 * close the device unless (1) it is being closed forcibly,
1035 	 * (2) the device wants to track closes, or (3) this is the last
1036 	 * vnode doing its last close on the device.
1037 	 *
1038 	 * XXX the VXLOCK (force close) case can leave vnodes referencing
1039 	 * a closed device.  This might not occur now that our revoke is
1040 	 * fixed.
1041 	 */
1042 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
1043 	if (dev && ((vp->v_flag & VRECLAIMED) ||
1044 	    (dev_dflags(dev) & D_TRACKCLOSE) ||
1045 	    (vp->v_opencount == 1))) {
1046 		/*
1047 		 * Unlock around dev_dclose()
1048 		 */
1049 		needrelock = 0;
1050 		if (vn_islocked(vp)) {
1051 			needrelock = 1;
1052 			vn_unlock(vp);
1053 		}
1054 		error = dev_dclose(dev, ap->a_fflag, S_IFCHR);
1055 
1056 		/*
1057 		 * Ugly pty magic, to make pty devices disappear again once
1058 		 * they are closed
1059 		 */
1060 		if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY)
1061 			node->flags |= DEVFS_INVISIBLE;
1062 
1063 		if (needrelock)
1064 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1065 	} else {
1066 		error = 0;
1067 	}
1068 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
1069 
1070 	/*
1071 	 * Track the actual opens and closes on the vnode.  The last close
1072 	 * disassociates the rdev.  If the rdev is already disassociated or
1073 	 * the opencount is already 0, the vnode might have been revoked
1074 	 * and no further opencount tracking occurs.
1075 	 */
1076 	if (dev)
1077 		release_dev(dev);
1078 	if (vp->v_opencount > 0)
1079 		vop_stdclose(ap);
1080 	return(error);
1081 
1082 }
1083 
1084 
1085 static int
1086 devfs_specf_close(struct file *fp)
1087 {
1088 	struct vnode *vp = (struct vnode *)fp->f_data;
1089 	int error;
1090 
1091 	get_mplock();
1092 	fp->f_ops = &badfileops;
1093 	error = vn_close(vp, fp->f_flag);
1094 	rel_mplock();
1095 
1096 	return (error);
1097 }
1098 
1099 
1100 /*
1101  * Device-optimized file table vnode read routine.
1102  *
1103  * This bypasses the VOP table and talks directly to the device.  Most
1104  * filesystems just route to specfs and can make this optimization.
1105  *
1106  * MPALMOSTSAFE - acquires mplock
1107  */
1108 static int
1109 devfs_specf_read(struct file *fp, struct uio *uio,
1110 		 struct ucred *cred, int flags)
1111 {
1112 	struct devfs_node *node;
1113 	struct vnode *vp;
1114 	int ioflag;
1115 	int error;
1116 	cdev_t dev;
1117 
1118 	KASSERT(uio->uio_td == curthread,
1119 		("uio_td %p is not td %p", uio->uio_td, curthread));
1120 
1121 	if (uio->uio_resid == 0)
1122 		return 0;
1123 
1124 	vp = (struct vnode *)fp->f_data;
1125 	if (vp == NULL || vp->v_type == VBAD)
1126 		return EBADF;
1127 
1128 	node = DEVFS_NODE(vp);
1129 
1130 	if ((dev = vp->v_rdev) == NULL)
1131 		return EBADF;
1132 
1133 	/* only acquire mplock for devices that require it */
1134 	if (!(dev_dflags(dev) & D_MPSAFE_READ)) {
1135 		atomic_add_int(&mplock_reads, 1);
1136 		get_mplock();
1137 	} else {
1138 		atomic_add_int(&mpsafe_reads, 1);
1139 	}
1140 
1141 	reference_dev(dev);
1142 
1143 	if ((flags & O_FOFFSET) == 0)
1144 		uio->uio_offset = fp->f_offset;
1145 
1146 	ioflag = 0;
1147 	if (flags & O_FBLOCKING) {
1148 		/* ioflag &= ~IO_NDELAY; */
1149 	} else if (flags & O_FNONBLOCKING) {
1150 		ioflag |= IO_NDELAY;
1151 	} else if (fp->f_flag & FNONBLOCK) {
1152 		ioflag |= IO_NDELAY;
1153 	}
1154 	if (flags & O_FBUFFERED) {
1155 		/* ioflag &= ~IO_DIRECT; */
1156 	} else if (flags & O_FUNBUFFERED) {
1157 		ioflag |= IO_DIRECT;
1158 	} else if (fp->f_flag & O_DIRECT) {
1159 		ioflag |= IO_DIRECT;
1160 	}
1161 	ioflag |= sequential_heuristic(uio, fp);
1162 
1163 	error = dev_dread(dev, uio, ioflag);
1164 
1165 	release_dev(dev);
1166 	if (node)
1167 		nanotime(&node->atime);
1168 	if ((flags & O_FOFFSET) == 0)
1169 		fp->f_offset = uio->uio_offset;
1170 	fp->f_nextoff = uio->uio_offset;
1171 
1172 	if (!(dev_dflags(dev) & D_MPSAFE_READ))
1173 		rel_mplock();
1174 
1175 	return (error);
1176 }
1177 
1178 
1179 static int
1180 devfs_specf_write(struct file *fp, struct uio *uio,
1181 		  struct ucred *cred, int flags)
1182 {
1183 	struct devfs_node *node;
1184 	struct vnode *vp;
1185 	int ioflag;
1186 	int error;
1187 	cdev_t dev;
1188 
1189 	KASSERT(uio->uio_td == curthread,
1190 		("uio_td %p is not p %p", uio->uio_td, curthread));
1191 
1192 	vp = (struct vnode *)fp->f_data;
1193 	if (vp == NULL || vp->v_type == VBAD)
1194 		return EBADF;
1195 
1196 	node = DEVFS_NODE(vp);
1197 
1198 	if (vp->v_type == VREG)
1199 		bwillwrite(uio->uio_resid);
1200 
1201 	vp = (struct vnode *)fp->f_data;
1202 
1203 	if ((dev = vp->v_rdev) == NULL)
1204 		return EBADF;
1205 
1206 	/* only acquire mplock for devices that require it */
1207 	if (!(dev_dflags(dev) & D_MPSAFE_WRITE)) {
1208 		atomic_add_int(&mplock_writes, 1);
1209 		get_mplock();
1210 	} else {
1211 		atomic_add_int(&mpsafe_writes, 1);
1212 	}
1213 
1214 	reference_dev(dev);
1215 
1216 	if ((flags & O_FOFFSET) == 0)
1217 		uio->uio_offset = fp->f_offset;
1218 
1219 	ioflag = IO_UNIT;
1220 	if (vp->v_type == VREG &&
1221 	   ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1222 		ioflag |= IO_APPEND;
1223 	}
1224 
1225 	if (flags & O_FBLOCKING) {
1226 		/* ioflag &= ~IO_NDELAY; */
1227 	} else if (flags & O_FNONBLOCKING) {
1228 		ioflag |= IO_NDELAY;
1229 	} else if (fp->f_flag & FNONBLOCK) {
1230 		ioflag |= IO_NDELAY;
1231 	}
1232 	if (flags & O_FBUFFERED) {
1233 		/* ioflag &= ~IO_DIRECT; */
1234 	} else if (flags & O_FUNBUFFERED) {
1235 		ioflag |= IO_DIRECT;
1236 	} else if (fp->f_flag & O_DIRECT) {
1237 		ioflag |= IO_DIRECT;
1238 	}
1239 	if (flags & O_FASYNCWRITE) {
1240 		/* ioflag &= ~IO_SYNC; */
1241 	} else if (flags & O_FSYNCWRITE) {
1242 		ioflag |= IO_SYNC;
1243 	} else if (fp->f_flag & O_FSYNC) {
1244 		ioflag |= IO_SYNC;
1245 	}
1246 
1247 	if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1248 		ioflag |= IO_SYNC;
1249 	ioflag |= sequential_heuristic(uio, fp);
1250 
1251 	error = dev_dwrite(dev, uio, ioflag);
1252 
1253 	release_dev(dev);
1254 	if (node) {
1255 		nanotime(&node->atime);
1256 		nanotime(&node->mtime);
1257 	}
1258 
1259 	if ((flags & O_FOFFSET) == 0)
1260 		fp->f_offset = uio->uio_offset;
1261 	fp->f_nextoff = uio->uio_offset;
1262 
1263 	if (!(dev_dflags(dev) & D_MPSAFE_WRITE))
1264 		rel_mplock();
1265 	return (error);
1266 }
1267 
1268 
1269 static int
1270 devfs_specf_stat(struct file *fp, struct stat *sb, struct ucred *cred)
1271 {
1272 	struct vnode *vp;
1273 	struct vattr vattr;
1274 	struct vattr *vap;
1275 	u_short mode;
1276 	cdev_t dev;
1277 	int error;
1278 
1279 	vp = (struct vnode *)fp->f_data;
1280 	if (vp == NULL || vp->v_type == VBAD)
1281 		return EBADF;
1282 
1283 	error = vn_stat(vp, sb, cred);
1284 	if (error)
1285 		return (error);
1286 
1287 	vap = &vattr;
1288 	error = VOP_GETATTR(vp, vap);
1289 	if (error)
1290 		return (error);
1291 
1292 	/*
1293 	 * Zero the spare stat fields
1294 	 */
1295 	sb->st_lspare = 0;
1296 	sb->st_qspare1 = 0;
1297 	sb->st_qspare2 = 0;
1298 
1299 	/*
1300 	 * Copy from vattr table ... or not in case it's a cloned device
1301 	 */
1302 	if (vap->va_fsid != VNOVAL)
1303 		sb->st_dev = vap->va_fsid;
1304 	else
1305 		sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1306 
1307 	sb->st_ino = vap->va_fileid;
1308 
1309 	mode = vap->va_mode;
1310 	mode |= S_IFCHR;
1311 	sb->st_mode = mode;
1312 
1313 	if (vap->va_nlink > (nlink_t)-1)
1314 		sb->st_nlink = (nlink_t)-1;
1315 	else
1316 		sb->st_nlink = vap->va_nlink;
1317 
1318 	sb->st_uid = vap->va_uid;
1319 	sb->st_gid = vap->va_gid;
1320 	sb->st_rdev = dev2udev(DEVFS_NODE(vp)->d_dev);
1321 	sb->st_size = vap->va_bytes;
1322 	sb->st_atimespec = vap->va_atime;
1323 	sb->st_mtimespec = vap->va_mtime;
1324 	sb->st_ctimespec = vap->va_ctime;
1325 
1326 	/*
1327 	 * A VCHR and VBLK device may track the last access and last modified
1328 	 * time independantly of the filesystem.  This is particularly true
1329 	 * because device read and write calls may bypass the filesystem.
1330 	 */
1331 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
1332 		dev = vp->v_rdev;
1333 		if (dev != NULL) {
1334 			if (dev->si_lastread) {
1335 				sb->st_atimespec.tv_sec = dev->si_lastread;
1336 				sb->st_atimespec.tv_nsec = 0;
1337 			}
1338 			if (dev->si_lastwrite) {
1339 				sb->st_atimespec.tv_sec = dev->si_lastwrite;
1340 				sb->st_atimespec.tv_nsec = 0;
1341 			}
1342 		}
1343 	}
1344 
1345         /*
1346 	 * According to www.opengroup.org, the meaning of st_blksize is
1347 	 *   "a filesystem-specific preferred I/O block size for this
1348 	 *    object.  In some filesystem types, this may vary from file
1349 	 *    to file"
1350 	 * Default to PAGE_SIZE after much discussion.
1351 	 */
1352 
1353 	sb->st_blksize = PAGE_SIZE;
1354 
1355 	sb->st_flags = vap->va_flags;
1356 
1357 	error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1358 	if (error)
1359 		sb->st_gen = 0;
1360 	else
1361 		sb->st_gen = (u_int32_t)vap->va_gen;
1362 
1363 	sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1364 
1365 	return (0);
1366 }
1367 
1368 
1369 static int
1370 devfs_specf_kqfilter(struct file *fp, struct knote *kn)
1371 {
1372 	struct vnode *vp;
1373 	int error;
1374 	cdev_t dev;
1375 
1376 	get_mplock();
1377 
1378 	vp = (struct vnode *)fp->f_data;
1379 	if (vp == NULL || vp->v_type == VBAD) {
1380 		error = EBADF;
1381 		goto done;
1382 	}
1383 	if ((dev = vp->v_rdev) == NULL) {
1384 		error = EBADF;
1385 		goto done;
1386 	}
1387 	reference_dev(dev);
1388 
1389 	error = dev_dkqfilter(dev, kn);
1390 
1391 	release_dev(dev);
1392 
1393 done:
1394 	rel_mplock();
1395 	return (error);
1396 }
1397 
1398 /*
1399  * MPALMOSTSAFE - acquires mplock
1400  */
1401 static int
1402 devfs_specf_ioctl(struct file *fp, u_long com, caddr_t data,
1403 		  struct ucred *ucred, struct sysmsg *msg)
1404 {
1405 	struct devfs_node *node;
1406 	struct vnode *vp;
1407 	struct vnode *ovp;
1408 	cdev_t	dev;
1409 	int error;
1410 	struct fiodname_args *name_args;
1411 	size_t namlen;
1412 	const char *name;
1413 
1414 	vp = ((struct vnode *)fp->f_data);
1415 
1416 	if ((dev = vp->v_rdev) == NULL)
1417 		return EBADF;		/* device was revoked */
1418 
1419 	reference_dev(dev);
1420 
1421 	node = DEVFS_NODE(vp);
1422 
1423 	devfs_debug(DEVFS_DEBUG_DEBUG,
1424 		    "devfs_specf_ioctl() called! for dev %s\n",
1425 		    dev->si_name);
1426 
1427 	if (com == FIODTYPE) {
1428 		*(int *)data = dev_dflags(dev) & D_TYPEMASK;
1429 		error = 0;
1430 		goto out;
1431 	} else if (com == FIODNAME) {
1432 		name_args = (struct fiodname_args *)data;
1433 		name = dev->si_name;
1434 		namlen = strlen(name) + 1;
1435 
1436 		devfs_debug(DEVFS_DEBUG_DEBUG,
1437 			    "ioctl, got: FIODNAME for %s\n", name);
1438 
1439 		if (namlen <= name_args->len)
1440 			error = copyout(dev->si_name, name_args->name, namlen);
1441 		else
1442 			error = EINVAL;
1443 
1444 		devfs_debug(DEVFS_DEBUG_DEBUG,
1445 			    "ioctl stuff: error: %d\n", error);
1446 		goto out;
1447 	}
1448 
1449 	/* only acquire mplock for devices that require it */
1450 	if (!(dev_dflags(dev) & D_MPSAFE_IOCTL))
1451 		get_mplock();
1452 
1453 	error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg);
1454 
1455 #if 0
1456 	if (node) {
1457 		nanotime(&node->atime);
1458 		nanotime(&node->mtime);
1459 	}
1460 #endif
1461 
1462 	if (!(dev_dflags(dev) & D_MPSAFE_IOCTL))
1463 		rel_mplock();
1464 
1465 	if (com == TIOCSCTTY) {
1466 		devfs_debug(DEVFS_DEBUG_DEBUG,
1467 			    "devfs_specf_ioctl: got TIOCSCTTY on %s\n",
1468 			    dev->si_name);
1469 	}
1470 	if (error == 0 && com == TIOCSCTTY) {
1471 		struct proc *p = curthread->td_proc;
1472 		struct session *sess;
1473 
1474 		devfs_debug(DEVFS_DEBUG_DEBUG,
1475 			    "devfs_specf_ioctl: dealing with TIOCSCTTY on %s\n",
1476 			    dev->si_name);
1477 		if (p == NULL) {
1478 			error = ENOTTY;
1479 			goto out;
1480 		}
1481 		sess = p->p_session;
1482 
1483 		/*
1484 		 * Do nothing if reassigning same control tty
1485 		 */
1486 		if (sess->s_ttyvp == vp) {
1487 			error = 0;
1488 			goto out;
1489 		}
1490 
1491 		/*
1492 		 * Get rid of reference to old control tty
1493 		 */
1494 		ovp = sess->s_ttyvp;
1495 		vref(vp);
1496 		sess->s_ttyvp = vp;
1497 		if (ovp)
1498 			vrele(ovp);
1499 	}
1500 
1501 out:
1502 	release_dev(dev);
1503 	devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_ioctl() finished! \n");
1504 	return (error);
1505 }
1506 
1507 
1508 static int
1509 devfs_spec_fsync(struct vop_fsync_args *ap)
1510 {
1511 	struct vnode *vp = ap->a_vp;
1512 	int error;
1513 
1514 	if (!vn_isdisk(vp, NULL))
1515 		return (0);
1516 
1517 	/*
1518 	 * Flush all dirty buffers associated with a block device.
1519 	 */
1520 	error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1521 	return (error);
1522 }
1523 
1524 static int
1525 devfs_spec_read(struct vop_read_args *ap)
1526 {
1527 	struct devfs_node *node;
1528 	struct vnode *vp;
1529 	struct uio *uio;
1530 	cdev_t dev;
1531 	int error;
1532 
1533 	vp = ap->a_vp;
1534 	dev = vp->v_rdev;
1535 	uio = ap->a_uio;
1536 	node = DEVFS_NODE(vp);
1537 
1538 	if (dev == NULL)		/* device was revoked */
1539 		return (EBADF);
1540 	if (uio->uio_resid == 0)
1541 		return (0);
1542 
1543 	vn_unlock(vp);
1544 	error = dev_dread(dev, uio, ap->a_ioflag);
1545 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1546 
1547 	if (node)
1548 		nanotime(&node->atime);
1549 
1550 	return (error);
1551 }
1552 
1553 /*
1554  * Vnode op for write
1555  *
1556  * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1557  *	      struct ucred *a_cred)
1558  */
1559 static int
1560 devfs_spec_write(struct vop_write_args *ap)
1561 {
1562 	struct devfs_node *node;
1563 	struct vnode *vp;
1564 	struct uio *uio;
1565 	cdev_t dev;
1566 	int error;
1567 
1568 	vp = ap->a_vp;
1569 	dev = vp->v_rdev;
1570 	uio = ap->a_uio;
1571 	node = DEVFS_NODE(vp);
1572 
1573 	KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1574 
1575 	if (dev == NULL)		/* device was revoked */
1576 		return (EBADF);
1577 
1578 	vn_unlock(vp);
1579 	error = dev_dwrite(dev, uio, ap->a_ioflag);
1580 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1581 
1582 	if (node) {
1583 		nanotime(&node->atime);
1584 		nanotime(&node->mtime);
1585 	}
1586 
1587 	return (error);
1588 }
1589 
1590 /*
1591  * Device ioctl operation.
1592  *
1593  * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
1594  *	      int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
1595  */
1596 static int
1597 devfs_spec_ioctl(struct vop_ioctl_args *ap)
1598 {
1599 	struct vnode *vp = ap->a_vp;
1600 	struct devfs_node *node;
1601 	cdev_t dev;
1602 
1603 	if ((dev = vp->v_rdev) == NULL)
1604 		return (EBADF);		/* device was revoked */
1605 	node = DEVFS_NODE(vp);
1606 
1607 #if 0
1608 	if (node) {
1609 		nanotime(&node->atime);
1610 		nanotime(&node->mtime);
1611 	}
1612 #endif
1613 
1614 	return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
1615 			   ap->a_cred, ap->a_sysmsg));
1616 }
1617 
1618 /*
1619  * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1620  */
1621 /* ARGSUSED */
1622 static int
1623 devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1624 {
1625 	struct vnode *vp = ap->a_vp;
1626 	struct devfs_node *node;
1627 	cdev_t dev;
1628 
1629 	if ((dev = vp->v_rdev) == NULL)
1630 		return (EBADF);		/* device was revoked (EBADF) */
1631 	node = DEVFS_NODE(vp);
1632 
1633 #if 0
1634 	if (node)
1635 		nanotime(&node->atime);
1636 #endif
1637 
1638 	return (dev_dkqfilter(dev, ap->a_kn));
1639 }
1640 
1641 /*
1642  * Convert a vnode strategy call into a device strategy call.  Vnode strategy
1643  * calls are not limited to device DMA limits so we have to deal with the
1644  * case.
1645  *
1646  * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1647  */
1648 static int
1649 devfs_spec_strategy(struct vop_strategy_args *ap)
1650 {
1651 	struct bio *bio = ap->a_bio;
1652 	struct buf *bp = bio->bio_buf;
1653 	struct buf *nbp;
1654 	struct vnode *vp;
1655 	struct mount *mp;
1656 	int chunksize;
1657 	int maxiosize;
1658 
1659 	if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1660 		buf_start(bp);
1661 
1662 	/*
1663 	 * Collect statistics on synchronous and asynchronous read
1664 	 * and write counts for disks that have associated filesystems.
1665 	 */
1666 	vp = ap->a_vp;
1667 	KKASSERT(vp->v_rdev != NULL);	/* XXX */
1668 	if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1669 		if (bp->b_cmd == BUF_CMD_READ) {
1670 			if (bp->b_flags & BIO_SYNC)
1671 				mp->mnt_stat.f_syncreads++;
1672 			else
1673 				mp->mnt_stat.f_asyncreads++;
1674 		} else {
1675 			if (bp->b_flags & BIO_SYNC)
1676 				mp->mnt_stat.f_syncwrites++;
1677 			else
1678 				mp->mnt_stat.f_asyncwrites++;
1679 		}
1680 	}
1681 
1682         /*
1683          * Device iosize limitations only apply to read and write.  Shortcut
1684          * the I/O if it fits.
1685          */
1686 	if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1687 		devfs_debug(DEVFS_DEBUG_DEBUG,
1688 			    "%s: si_iosize_max not set!\n",
1689 			    dev_dname(vp->v_rdev));
1690 		maxiosize = MAXPHYS;
1691 	}
1692 #if SPEC_CHAIN_DEBUG & 2
1693 	maxiosize = 4096;
1694 #endif
1695         if (bp->b_bcount <= maxiosize ||
1696             (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1697                 dev_dstrategy_chain(vp->v_rdev, bio);
1698                 return (0);
1699         }
1700 
1701 	/*
1702 	 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1703 	 */
1704 	nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1705 	initbufbio(nbp);
1706 	buf_dep_init(nbp);
1707 	BUF_LOCKINIT(nbp);
1708 	BUF_LOCK(nbp, LK_EXCLUSIVE);
1709 	BUF_KERNPROC(nbp);
1710 	nbp->b_vp = vp;
1711 	nbp->b_flags = B_PAGING | (bp->b_flags & B_BNOCLIP);
1712 	nbp->b_data = bp->b_data;
1713 	nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1714 	nbp->b_bio1.bio_offset = bio->bio_offset;
1715 	nbp->b_bio1.bio_caller_info1.ptr = bio;
1716 
1717 	/*
1718 	 * Start the first transfer
1719 	 */
1720 	if (vn_isdisk(vp, NULL))
1721 		chunksize = vp->v_rdev->si_bsize_phys;
1722 	else
1723 		chunksize = DEV_BSIZE;
1724 	chunksize = maxiosize / chunksize * chunksize;
1725 #if SPEC_CHAIN_DEBUG & 1
1726 	devfs_debug(DEVFS_DEBUG_DEBUG,
1727 		    "spec_strategy chained I/O chunksize=%d\n",
1728 		    chunksize);
1729 #endif
1730 	nbp->b_cmd = bp->b_cmd;
1731 	nbp->b_bcount = chunksize;
1732 	nbp->b_bufsize = chunksize;	/* used to detect a short I/O */
1733 	nbp->b_bio1.bio_caller_info2.index = chunksize;
1734 
1735 #if SPEC_CHAIN_DEBUG & 1
1736 	devfs_debug(DEVFS_DEBUG_DEBUG,
1737 		    "spec_strategy: chain %p offset %d/%d bcount %d\n",
1738 		    bp, 0, bp->b_bcount, nbp->b_bcount);
1739 #endif
1740 
1741 	dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1742 
1743 	if (DEVFS_NODE(vp)) {
1744 		nanotime(&DEVFS_NODE(vp)->atime);
1745 		nanotime(&DEVFS_NODE(vp)->mtime);
1746 	}
1747 
1748 	return (0);
1749 }
1750 
1751 /*
1752  * Chunked up transfer completion routine - chain transfers until done
1753  */
1754 static
1755 void
1756 devfs_spec_strategy_done(struct bio *nbio)
1757 {
1758 	struct buf *nbp = nbio->bio_buf;
1759 	struct bio *bio = nbio->bio_caller_info1.ptr;	/* original bio */
1760 	struct buf *bp = bio->bio_buf;			/* original bp */
1761 	int chunksize = nbio->bio_caller_info2.index;	/* chunking */
1762 	int boffset = nbp->b_data - bp->b_data;
1763 
1764 	if (nbp->b_flags & B_ERROR) {
1765 		/*
1766 		 * An error terminates the chain, propogate the error back
1767 		 * to the original bp
1768 		 */
1769 		bp->b_flags |= B_ERROR;
1770 		bp->b_error = nbp->b_error;
1771 		bp->b_resid = bp->b_bcount - boffset +
1772 			      (nbp->b_bcount - nbp->b_resid);
1773 #if SPEC_CHAIN_DEBUG & 1
1774 		devfs_debug(DEVFS_DEBUG_DEBUG,
1775 			    "spec_strategy: chain %p error %d bcount %d/%d\n",
1776 			    bp, bp->b_error, bp->b_bcount,
1777 			    bp->b_bcount - bp->b_resid);
1778 #endif
1779 		kfree(nbp, M_DEVBUF);
1780 		biodone(bio);
1781 	} else if (nbp->b_resid) {
1782 		/*
1783 		 * A short read or write terminates the chain
1784 		 */
1785 		bp->b_error = nbp->b_error;
1786 		bp->b_resid = bp->b_bcount - boffset +
1787 			      (nbp->b_bcount - nbp->b_resid);
1788 #if SPEC_CHAIN_DEBUG & 1
1789 		devfs_debug(DEVFS_DEBUG_DEBUG,
1790 			    "spec_strategy: chain %p short read(1) "
1791 			    "bcount %d/%d\n",
1792 			    bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
1793 #endif
1794 		kfree(nbp, M_DEVBUF);
1795 		biodone(bio);
1796 	} else if (nbp->b_bcount != nbp->b_bufsize) {
1797 		/*
1798 		 * A short read or write can also occur by truncating b_bcount
1799 		 */
1800 #if SPEC_CHAIN_DEBUG & 1
1801 		devfs_debug(DEVFS_DEBUG_DEBUG,
1802 			    "spec_strategy: chain %p short read(2) "
1803 			    "bcount %d/%d\n",
1804 			    bp, nbp->b_bcount + boffset, bp->b_bcount);
1805 #endif
1806 		bp->b_error = 0;
1807 		bp->b_bcount = nbp->b_bcount + boffset;
1808 		bp->b_resid = nbp->b_resid;
1809 		kfree(nbp, M_DEVBUF);
1810 		biodone(bio);
1811 	} else if (nbp->b_bcount + boffset == bp->b_bcount) {
1812 		/*
1813 		 * No more data terminates the chain
1814 		 */
1815 #if SPEC_CHAIN_DEBUG & 1
1816 		devfs_debug(DEVFS_DEBUG_DEBUG,
1817 			    "spec_strategy: chain %p finished bcount %d\n",
1818 			    bp, bp->b_bcount);
1819 #endif
1820 		bp->b_error = 0;
1821 		bp->b_resid = 0;
1822 		kfree(nbp, M_DEVBUF);
1823 		biodone(bio);
1824 	} else {
1825 		/*
1826 		 * Continue the chain
1827 		 */
1828 		boffset += nbp->b_bcount;
1829 		nbp->b_data = bp->b_data + boffset;
1830 		nbp->b_bcount = bp->b_bcount - boffset;
1831 		if (nbp->b_bcount > chunksize)
1832 			nbp->b_bcount = chunksize;
1833 		nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1834 		nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1835 
1836 #if SPEC_CHAIN_DEBUG & 1
1837 		devfs_debug(DEVFS_DEBUG_DEBUG,
1838 			    "spec_strategy: chain %p offset %d/%d bcount %d\n",
1839 			    bp, boffset, bp->b_bcount, nbp->b_bcount);
1840 #endif
1841 
1842 		dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
1843 	}
1844 }
1845 
1846 /*
1847  * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1848  */
1849 static int
1850 devfs_spec_freeblks(struct vop_freeblks_args *ap)
1851 {
1852 	struct buf *bp;
1853 
1854 	/*
1855 	 * XXX: This assumes that strategy does the deed right away.
1856 	 * XXX: this may not be TRTTD.
1857 	 */
1858 	KKASSERT(ap->a_vp->v_rdev != NULL);
1859 	if ((dev_dflags(ap->a_vp->v_rdev) & D_CANFREE) == 0)
1860 		return (0);
1861 	bp = geteblk(ap->a_length);
1862 	bp->b_cmd = BUF_CMD_FREEBLKS;
1863 	bp->b_bio1.bio_offset = ap->a_offset;
1864 	bp->b_bcount = ap->a_length;
1865 	dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1866 	return (0);
1867 }
1868 
1869 /*
1870  * Implement degenerate case where the block requested is the block
1871  * returned, and assume that the entire device is contiguous in regards
1872  * to the contiguous block range (runp and runb).
1873  *
1874  * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1875  *	     off_t *a_doffsetp, int *a_runp, int *a_runb)
1876  */
1877 static int
1878 devfs_spec_bmap(struct vop_bmap_args *ap)
1879 {
1880 	if (ap->a_doffsetp != NULL)
1881 		*ap->a_doffsetp = ap->a_loffset;
1882 	if (ap->a_runp != NULL)
1883 		*ap->a_runp = MAXBSIZE;
1884 	if (ap->a_runb != NULL) {
1885 		if (ap->a_loffset < MAXBSIZE)
1886 			*ap->a_runb = (int)ap->a_loffset;
1887 		else
1888 			*ap->a_runb = MAXBSIZE;
1889 	}
1890 	return (0);
1891 }
1892 
1893 
1894 /*
1895  * Special device advisory byte-level locks.
1896  *
1897  * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1898  *		struct flock *a_fl, int a_flags)
1899  */
1900 /* ARGSUSED */
1901 static int
1902 devfs_spec_advlock(struct vop_advlock_args *ap)
1903 {
1904 	return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
1905 }
1906 
1907 static void
1908 devfs_spec_getpages_iodone(struct bio *bio)
1909 {
1910 	bio->bio_buf->b_cmd = BUF_CMD_DONE;
1911 	wakeup(bio->bio_buf);
1912 }
1913 
1914 /*
1915  * spec_getpages() - get pages associated with device vnode.
1916  *
1917  * Note that spec_read and spec_write do not use the buffer cache, so we
1918  * must fully implement getpages here.
1919  */
1920 static int
1921 devfs_spec_getpages(struct vop_getpages_args *ap)
1922 {
1923 	vm_offset_t kva;
1924 	int error;
1925 	int i, pcount, size;
1926 	struct buf *bp;
1927 	vm_page_t m;
1928 	vm_ooffset_t offset;
1929 	int toff, nextoff, nread;
1930 	struct vnode *vp = ap->a_vp;
1931 	int blksiz;
1932 	int gotreqpage;
1933 
1934 	error = 0;
1935 	pcount = round_page(ap->a_count) / PAGE_SIZE;
1936 
1937 	/*
1938 	 * Calculate the offset of the transfer and do sanity check.
1939 	 */
1940 	offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
1941 
1942 	/*
1943 	 * Round up physical size for real devices.  We cannot round using
1944 	 * v_mount's block size data because v_mount has nothing to do with
1945 	 * the device.  i.e. it's usually '/dev'.  We need the physical block
1946 	 * size for the device itself.
1947 	 *
1948 	 * We can't use v_rdev->si_mountpoint because it only exists when the
1949 	 * block device is mounted.  However, we can use v_rdev.
1950 	 */
1951 	if (vn_isdisk(vp, NULL))
1952 		blksiz = vp->v_rdev->si_bsize_phys;
1953 	else
1954 		blksiz = DEV_BSIZE;
1955 
1956 	size = (ap->a_count + blksiz - 1) & ~(blksiz - 1);
1957 
1958 	bp = getpbuf(NULL);
1959 	kva = (vm_offset_t)bp->b_data;
1960 
1961 	/*
1962 	 * Map the pages to be read into the kva.
1963 	 */
1964 	pmap_qenter(kva, ap->a_m, pcount);
1965 
1966 	/* Build a minimal buffer header. */
1967 	bp->b_cmd = BUF_CMD_READ;
1968 	bp->b_bcount = size;
1969 	bp->b_resid = 0;
1970 	bp->b_runningbufspace = size;
1971 	if (size) {
1972 		runningbufspace += bp->b_runningbufspace;
1973 		++runningbufcount;
1974 	}
1975 
1976 	bp->b_bio1.bio_offset = offset;
1977 	bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
1978 
1979 	mycpu->gd_cnt.v_vnodein++;
1980 	mycpu->gd_cnt.v_vnodepgsin += pcount;
1981 
1982 	/* Do the input. */
1983 	vn_strategy(ap->a_vp, &bp->b_bio1);
1984 
1985 	crit_enter();
1986 
1987 	/* We definitely need to be at splbio here. */
1988 	while (bp->b_cmd != BUF_CMD_DONE)
1989 		tsleep(bp, 0, "spread", 0);
1990 
1991 	crit_exit();
1992 
1993 	if (bp->b_flags & B_ERROR) {
1994 		if (bp->b_error)
1995 			error = bp->b_error;
1996 		else
1997 			error = EIO;
1998 	}
1999 
2000 	/*
2001 	 * If EOF is encountered we must zero-extend the result in order
2002 	 * to ensure that the page does not contain garabge.  When no
2003 	 * error occurs, an early EOF is indicated if b_bcount got truncated.
2004 	 * b_resid is relative to b_bcount and should be 0, but some devices
2005 	 * might indicate an EOF with b_resid instead of truncating b_bcount.
2006 	 */
2007 	nread = bp->b_bcount - bp->b_resid;
2008 	if (nread < ap->a_count)
2009 		bzero((caddr_t)kva + nread, ap->a_count - nread);
2010 	pmap_qremove(kva, pcount);
2011 
2012 	gotreqpage = 0;
2013 	for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
2014 		nextoff = toff + PAGE_SIZE;
2015 		m = ap->a_m[i];
2016 
2017 		m->flags &= ~PG_ZERO;
2018 
2019 		/*
2020 		 * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2021 		 *	 pmap modified bit.  pmap modified bit should have
2022 		 *	 already been cleared.
2023 		 */
2024 		if (nextoff <= nread) {
2025 			m->valid = VM_PAGE_BITS_ALL;
2026 			vm_page_undirty(m);
2027 		} else if (toff < nread) {
2028 			/*
2029 			 * Since this is a VM request, we have to supply the
2030 			 * unaligned offset to allow vm_page_set_valid()
2031 			 * to zero sub-DEV_BSIZE'd portions of the page.
2032 			 */
2033 			vm_page_set_valid(m, 0, nread - toff);
2034 			vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
2035 		} else {
2036 			m->valid = 0;
2037 			vm_page_undirty(m);
2038 		}
2039 
2040 		if (i != ap->a_reqpage) {
2041 			/*
2042 			 * Just in case someone was asking for this page we
2043 			 * now tell them that it is ok to use.
2044 			 */
2045 			if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
2046 				if (m->valid) {
2047 					if (m->flags & PG_WANTED) {
2048 						vm_page_activate(m);
2049 					} else {
2050 						vm_page_deactivate(m);
2051 					}
2052 					vm_page_wakeup(m);
2053 				} else {
2054 					vm_page_free(m);
2055 				}
2056 			} else {
2057 				vm_page_free(m);
2058 			}
2059 		} else if (m->valid) {
2060 			gotreqpage = 1;
2061 			/*
2062 			 * Since this is a VM request, we need to make the
2063 			 * entire page presentable by zeroing invalid sections.
2064 			 */
2065 			if (m->valid != VM_PAGE_BITS_ALL)
2066 			    vm_page_zero_invalid(m, FALSE);
2067 		}
2068 	}
2069 	if (!gotreqpage) {
2070 		m = ap->a_m[ap->a_reqpage];
2071 		devfs_debug(DEVFS_DEBUG_WARNING,
2072 	    "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
2073 			devtoname(vp->v_rdev), error, bp, bp->b_vp);
2074 		devfs_debug(DEVFS_DEBUG_WARNING,
2075 	    "               size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
2076 		    size, bp->b_resid, ap->a_count, m->valid);
2077 		devfs_debug(DEVFS_DEBUG_WARNING,
2078 	    "               nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
2079 		    nread, ap->a_reqpage, (u_long)m->pindex, pcount);
2080 		/*
2081 		 * Free the buffer header back to the swap buffer pool.
2082 		 */
2083 		relpbuf(bp, NULL);
2084 		return VM_PAGER_ERROR;
2085 	}
2086 	/*
2087 	 * Free the buffer header back to the swap buffer pool.
2088 	 */
2089 	relpbuf(bp, NULL);
2090 	if (DEVFS_NODE(ap->a_vp))
2091 		nanotime(&DEVFS_NODE(ap->a_vp)->mtime);
2092 	return VM_PAGER_OK;
2093 }
2094 
2095 static __inline
2096 int
2097 sequential_heuristic(struct uio *uio, struct file *fp)
2098 {
2099 	/*
2100 	 * Sequential heuristic - detect sequential operation
2101 	 */
2102 	if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2103 	    uio->uio_offset == fp->f_nextoff) {
2104 		/*
2105 		 * XXX we assume that the filesystem block size is
2106 		 * the default.  Not true, but still gives us a pretty
2107 		 * good indicator of how sequential the read operations
2108 		 * are.
2109 		 */
2110 		int tmpseq = fp->f_seqcount;
2111 
2112 		tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
2113 		if (tmpseq > IO_SEQMAX)
2114 			tmpseq = IO_SEQMAX;
2115 		fp->f_seqcount = tmpseq;
2116 		return(fp->f_seqcount << IO_SEQSHIFT);
2117 	}
2118 
2119 	/*
2120 	 * Not sequential, quick draw-down of seqcount
2121 	 */
2122 	if (fp->f_seqcount > 1)
2123 		fp->f_seqcount = 1;
2124 	else
2125 		fp->f_seqcount = 0;
2126 	return(0);
2127 }
2128 
2129 extern SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "devfs");
2130 
2131 SYSCTL_INT(_vfs_devfs, OID_AUTO, mpsafe_writes, CTLFLAG_RD, &mpsafe_writes,
2132 		0, "mpsafe writes");
2133 SYSCTL_INT(_vfs_devfs, OID_AUTO, mplock_writes, CTLFLAG_RD, &mplock_writes,
2134 		0, "non-mpsafe writes");
2135 SYSCTL_INT(_vfs_devfs, OID_AUTO, mpsafe_reads, CTLFLAG_RD, &mpsafe_reads,
2136 		0, "mpsafe reads");
2137 SYSCTL_INT(_vfs_devfs, OID_AUTO, mplock_reads, CTLFLAG_RD, &mplock_reads,
2138 		0, "non-mpsafe reads");
2139