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