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