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