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