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