2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
42 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
45 #include <sys/mount.h>
47 #include <sys/fcntl.h>
48 #include <sys/namei.h>
49 #include <sys/dirent.h>
50 #include <sys/malloc.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/sysref2.h>
61 #include <vfs/devfs/devfs.h>
62 #include <sys/pioctl.h>
64 #include <machine/limits.h>
66 MALLOC_DECLARE(M_DEVFS);
67 #define DEVFS_BADOP (void *)devfs_badop
69 static int devfs_badop(struct vop_generic_args *);
70 static int devfs_access(struct vop_access_args *);
71 static int devfs_inactive(struct vop_inactive_args *);
72 static int devfs_reclaim(struct vop_reclaim_args *);
73 static int devfs_readdir(struct vop_readdir_args *);
74 static int devfs_getattr(struct vop_getattr_args *);
75 static int devfs_setattr(struct vop_setattr_args *);
76 static int devfs_readlink(struct vop_readlink_args *);
77 static int devfs_print(struct vop_print_args *);
79 static int devfs_nresolve(struct vop_nresolve_args *);
80 static int devfs_nlookupdotdot(struct vop_nlookupdotdot_args *);
81 static int devfs_nsymlink(struct vop_nsymlink_args *);
82 static int devfs_nremove(struct vop_nremove_args *);
84 static int devfs_spec_open(struct vop_open_args *);
85 static int devfs_spec_close(struct vop_close_args *);
86 static int devfs_spec_fsync(struct vop_fsync_args *);
88 static int devfs_spec_read(struct vop_read_args *);
89 static int devfs_spec_write(struct vop_write_args *);
90 static int devfs_spec_ioctl(struct vop_ioctl_args *);
91 static int devfs_spec_poll(struct vop_poll_args *);
92 static int devfs_spec_kqfilter(struct vop_kqfilter_args *);
93 static int devfs_spec_strategy(struct vop_strategy_args *);
94 static void devfs_spec_strategy_done(struct bio *);
95 static int devfs_spec_freeblks(struct vop_freeblks_args *);
96 static int devfs_spec_bmap(struct vop_bmap_args *);
97 static int devfs_spec_advlock(struct vop_advlock_args *);
98 static void devfs_spec_getpages_iodone(struct bio *);
99 static int devfs_spec_getpages(struct vop_getpages_args *);
102 static int devfs_specf_close(struct file *);
103 static int devfs_specf_read(struct file *, struct uio *, struct ucred *, int);
104 static int devfs_specf_write(struct file *, struct uio *, struct ucred *, int);
105 static int devfs_specf_stat(struct file *, struct stat *, struct ucred *);
106 static int devfs_specf_kqfilter(struct file *, struct knote *);
107 static int devfs_specf_poll(struct file *, int, struct ucred *);
108 static int devfs_specf_ioctl(struct file *, u_long, caddr_t, struct ucred *);
111 static __inline int sequential_heuristic(struct uio *, struct file *);
112 extern struct lock devfs_lock;
115 * devfs vnode operations for regular files
117 struct vop_ops devfs_vnode_norm_vops = {
118 .vop_default = vop_defaultop,
119 .vop_access = devfs_access,
120 .vop_advlock = DEVFS_BADOP,
121 .vop_bmap = DEVFS_BADOP,
122 .vop_close = vop_stdclose,
123 .vop_getattr = devfs_getattr,
124 .vop_inactive = devfs_inactive,
125 .vop_ncreate = DEVFS_BADOP,
126 .vop_nresolve = devfs_nresolve,
127 .vop_nlookupdotdot = devfs_nlookupdotdot,
128 .vop_nlink = DEVFS_BADOP,
129 .vop_nmkdir = DEVFS_BADOP,
130 .vop_nmknod = DEVFS_BADOP,
131 .vop_nremove = devfs_nremove,
132 .vop_nrename = DEVFS_BADOP,
133 .vop_nrmdir = DEVFS_BADOP,
134 .vop_nsymlink = devfs_nsymlink,
135 .vop_open = vop_stdopen,
136 .vop_pathconf = vop_stdpathconf,
137 .vop_print = devfs_print,
138 .vop_read = DEVFS_BADOP,
139 .vop_readdir = devfs_readdir,
140 .vop_readlink = devfs_readlink,
141 .vop_reclaim = devfs_reclaim,
142 .vop_setattr = devfs_setattr,
143 .vop_write = DEVFS_BADOP,
144 .vop_ioctl = DEVFS_BADOP
148 * devfs vnode operations for character devices
150 struct vop_ops devfs_vnode_dev_vops = {
151 .vop_default = vop_defaultop,
152 .vop_access = devfs_access,
153 .vop_advlock = devfs_spec_advlock,
154 .vop_bmap = devfs_spec_bmap,
155 .vop_close = devfs_spec_close,
156 .vop_freeblks = devfs_spec_freeblks,
157 .vop_fsync = devfs_spec_fsync,
158 .vop_getattr = devfs_getattr,
159 .vop_getpages = devfs_spec_getpages,
160 .vop_inactive = devfs_inactive,
161 .vop_open = devfs_spec_open,
162 .vop_pathconf = vop_stdpathconf,
163 .vop_print = devfs_print,
164 .vop_poll = devfs_spec_poll,
165 .vop_kqfilter = devfs_spec_kqfilter,
166 .vop_read = devfs_spec_read,
167 .vop_readdir = DEVFS_BADOP,
168 .vop_readlink = DEVFS_BADOP,
169 .vop_reclaim = devfs_reclaim,
170 .vop_setattr = devfs_setattr,
171 .vop_strategy = devfs_spec_strategy,
172 .vop_write = devfs_spec_write,
173 .vop_ioctl = devfs_spec_ioctl
176 struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops;
178 struct fileops devfs_dev_fileops = {
179 .fo_read = devfs_specf_read,
180 .fo_write = devfs_specf_write,
181 .fo_ioctl = devfs_specf_ioctl,
182 .fo_poll = devfs_specf_poll,
183 .fo_kqfilter = devfs_specf_kqfilter,
184 .fo_stat = devfs_specf_stat,
185 .fo_close = devfs_specf_close,
186 .fo_shutdown = nofo_shutdown
191 * generic entry point for unsupported operations
194 devfs_badop(struct vop_generic_args *ap)
196 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs: specified vnode operation is not implemented (yet)\n");
202 devfs_access(struct vop_access_args *ap)
204 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
207 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_access() called!\n");
209 error = vop_helper_access(ap, node->uid, node->gid,
210 node->mode, node->flags);
212 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_access ruled over %s: %d\n", "UNKNOWN", error);
215 //XXX: consider possible special cases? terminal, ...?
220 devfs_inactive(struct vop_inactive_args *ap)
222 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
224 if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0)
231 devfs_reclaim(struct vop_reclaim_args *ap)
234 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_reclaim() called!\n");
236 /* Check if it is locked already. if not, we acquire the devfs lock */
237 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
238 lockmgr(&devfs_lock, LK_EXCLUSIVE);
242 /* Check if the devfs_node is not linked anymore into the topology.
243 * If this is the case, we get rid of the devfs_node. */
244 if (DEVFS_NODE(ap->a_vp)) {
245 if ((DEVFS_NODE(ap->a_vp)->flags & DEVFS_NODE_LINKED) == 0) {
246 devfs_freep(DEVFS_NODE(ap->a_vp));
247 //devfs_tracer_del_orphan(DEVFS_NODE(ap->a_vp));
250 /* unlink vnode <--> devfs_node */
251 DEVFS_NODE(ap->a_vp)->v_node = NULL;
254 /* If we acquired the lock, we also get rid of it */
256 lockmgr(&devfs_lock, LK_RELEASE);
258 ap->a_vp->v_data = NULL;
259 /* avoid a panic on release because of not adding it with v_associate_rdev */
260 ap->a_vp->v_rdev = NULL;
267 devfs_readdir(struct vop_readdir_args *ap)
269 struct devfs_node *node;
270 int error2 = 0, r, error = 0;
277 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
279 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
281 if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
284 if (DEVFS_NODE(ap->a_vp) == NULL)
287 lockmgr(&devfs_lock, LK_EXCLUSIVE);
289 saveoff = ap->a_uio->uio_offset;
291 if (ap->a_ncookies) {
292 ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
295 cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
303 nanotime(&DEVFS_NODE(ap->a_vp)->atime);
306 r = vop_write_dirent(&error, ap->a_uio, DEVFS_NODE(ap->a_vp)->d_dir.d_ino, DT_DIR, 1, ".");
310 cookies[cookie_index] = saveoff;
313 if (cookie_index == ncookies)
318 if (DEVFS_NODE(ap->a_vp)->parent) {
319 r = vop_write_dirent(&error, ap->a_uio,
320 DEVFS_NODE(ap->a_vp)->d_dir.d_ino,
323 r = vop_write_dirent(&error, ap->a_uio,
324 DEVFS_NODE(ap->a_vp)->d_dir.d_ino, DT_DIR, 2, "..");
329 cookies[cookie_index] = saveoff;
332 if (cookie_index == ncookies)
336 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(DEVFS_NODE(ap->a_vp)), link) {
337 if ((node->flags & DEVFS_HIDDEN) || (node->flags & DEVFS_INVISIBLE))
340 if (node->cookie < saveoff)
348 saveoff = node->cookie;
350 error2 = vop_write_dirent(&error, ap->a_uio,
351 node->d_dir.d_ino, node->d_dir.d_type,
352 node->d_dir.d_namlen, node->d_dir.d_name);
360 cookies[cookie_index] = node->cookie;
362 if (cookie_index == ncookies)
369 lockmgr(&devfs_lock, LK_RELEASE);
372 ap->a_uio->uio_offset = saveoff;
373 if (error && cookie_index == 0) {
375 kfree(cookies, M_TEMP);
377 *ap->a_cookies = NULL;
381 *ap->a_ncookies = cookie_index;
382 *ap->a_cookies = cookies;
390 devfs_nresolve(struct vop_nresolve_args *ap)
392 struct devfs_node *node, *found = NULL;
393 struct namecache *ncp;
394 struct vnode *vp = NULL;
400 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve() called!\n");
402 ncp = ap->a_nch->ncp;
405 if (DEVFS_NODE(ap->a_dvp) == NULL)
408 lockmgr(&devfs_lock, LK_EXCLUSIVE);
410 if ((DEVFS_NODE(ap->a_dvp)->node_type != Proot) &&
411 (DEVFS_NODE(ap->a_dvp)->node_type != Pdir)) {
412 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve: ap->a_dvp is not a dir!!!\n");
413 cache_setvp(ap->a_nch, NULL);
418 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve -search- \n");
419 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(DEVFS_NODE(ap->a_dvp)), link) {
420 if (len == node->d_dir.d_namlen) {
421 if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
422 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve: found: %s\n", ncp->nc_name);
430 if ((found->node_type == Plink) && (found->link_target))
431 found = found->link_target;
433 if (!(found->flags & DEVFS_HIDDEN))
434 devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
437 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve -2- \n");
440 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve -3- %c%c%c\n", ncp->nc_name[0], ncp->nc_name[1], ncp->nc_name[2]);
442 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve vp==NULL \n");
444 /* XXX: len is int, devfs_clone expects size_t*, not int* */
445 if ((!hidden) && (!devfs_clone(ncp->nc_name, &len, NULL, 0, ap->a_cred))) {
449 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve -4- \n");
451 cache_setvp(ap->a_nch, NULL);
452 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve -5- \n");
456 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve -6- \n");
459 cache_setvp(ap->a_nch, vp);
462 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve -9- \n");
464 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nresolve -end:10- failed? %s \n", (error)?"FAILED!":"OK!");
465 lockmgr(&devfs_lock, LK_RELEASE);
471 devfs_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
473 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nlookupdotdot() called!\n");
476 lockmgr(&devfs_lock, LK_EXCLUSIVE);
477 if (DEVFS_NODE(ap->a_dvp)->parent != NULL) {
478 devfs_allocv(/*ap->a_dvp->v_mount, */ap->a_vpp, DEVFS_NODE(ap->a_dvp)->parent);
479 vn_unlock(*ap->a_vpp);
481 lockmgr(&devfs_lock, LK_RELEASE);
483 return ((*ap->a_vpp == NULL) ? ENOENT : 0);
488 devfs_getattr(struct vop_getattr_args *ap)
490 struct vattr *vap = ap->a_vap;
491 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
497 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_getattr() called for %s!\n", DEVFS_NODE(ap->a_vp)->d_dir.d_name);
498 lockmgr(&devfs_lock, LK_EXCLUSIVE);
500 /* start by zeroing out the attributes */
503 /* next do all the common fields */
504 vap->va_type = ap->a_vp->v_type;
505 vap->va_mode = node->mode;
506 vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
507 vap->va_flags = 0; //what should this be?
508 vap->va_blocksize = DEV_BSIZE;
509 vap->va_bytes = vap->va_size = sizeof(struct devfs_node);
511 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_getattr() check dev %s!\n", (DEVFS_NODE(ap->a_vp)->d_dev)?(DEVFS_NODE(ap->a_vp)->d_dev->si_name):"Not a device");
513 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
516 vap->va_atime = node->atime;
517 vap->va_mtime = node->mtime;
518 vap->va_ctime = node->ctime;
520 vap->va_nlink = 1; /* number of references to file */
522 vap->va_uid = node->uid;
523 vap->va_gid = node->gid;
528 if ((DEVFS_NODE(ap->a_vp)->node_type == Pdev) &&
529 (DEVFS_NODE(ap->a_vp)->d_dev)) {
530 devfs_debug(DEVFS_DEBUG_DEBUG, "getattr: dev is: %p\n", DEVFS_NODE(ap->a_vp)->d_dev);
531 reference_dev(DEVFS_NODE(ap->a_vp)->d_dev);
532 vap->va_rminor = DEVFS_NODE(ap->a_vp)->d_dev->si_uminor;
533 release_dev(DEVFS_NODE(ap->a_vp)->d_dev);
536 /* For a softlink the va_size is the length of the softlink */
537 if (DEVFS_NODE(ap->a_vp)->symlink_name != 0) {
538 vap->va_size = DEVFS_NODE(ap->a_vp)->symlink_namelen;
540 nanotime(&node->atime);
541 lockmgr(&devfs_lock, LK_RELEASE);
542 return (error); //XXX: set error usefully
547 devfs_setattr(struct vop_setattr_args *ap)
549 struct devfs_node *node;
553 node = DEVFS_NODE(ap->a_vp);
558 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_setattr() called!\n");
559 lockmgr(&devfs_lock, LK_EXCLUSIVE);
563 if (vap->va_uid != (uid_t)VNOVAL) {
564 if ((ap->a_cred->cr_uid != node->uid) &&
565 (!groupmember(node->gid, ap->a_cred))) {
566 error = priv_check(curthread, PRIV_VFS_CHOWN);
568 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_setattr, erroring out -1-\n");
572 node->uid = vap->va_uid;
575 if (vap->va_gid != (uid_t)VNOVAL) {
576 if ((ap->a_cred->cr_uid != node->uid) &&
577 (!groupmember(node->gid, ap->a_cred))) {
578 error = priv_check(curthread, PRIV_VFS_CHOWN);
580 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_setattr, erroring out -2-\n");
584 node->gid = vap->va_gid;
587 if (vap->va_mode != (mode_t)VNOVAL) {
588 if (ap->a_cred->cr_uid != node->uid) {
589 error = priv_check(curthread, PRIV_VFS_ADMIN);
591 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_setattr, erroring out -3-\n");
595 node->mode = vap->va_mode;
599 nanotime(&node->mtime);
600 lockmgr(&devfs_lock, LK_RELEASE);
606 devfs_readlink(struct vop_readlink_args *ap)
608 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
614 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readlink() called!\n");
616 lockmgr(&devfs_lock, LK_EXCLUSIVE);
617 ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
618 lockmgr(&devfs_lock, LK_RELEASE);
625 devfs_print(struct vop_print_args *ap)
627 //struct devfs_node *node = DEVFS_NODE(ap->a_vp);
629 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_print() called!\n");
631 //XXX: print some useful debugging about node.
637 devfs_nsymlink(struct vop_nsymlink_args *ap)
639 size_t targetlen = strlen(ap->a_target);
641 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nsymlink() called!\n");
643 ap->a_vap->va_type = VLNK;
645 if (DEVFS_NODE(ap->a_dvp) == NULL)
648 if ((DEVFS_NODE(ap->a_dvp)->node_type != Proot) &&
649 (DEVFS_NODE(ap->a_dvp)->node_type != Pdir)) {
650 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nsymlink: ap->a_dvp is not a dir!!!\n");
653 lockmgr(&devfs_lock, LK_EXCLUSIVE);
654 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Plink,
655 ap->a_nch->ncp->nc_name, DEVFS_NODE(ap->a_dvp), NULL);
658 DEVFS_NODE(*ap->a_vpp)->flags |= DEVFS_USER_CREATED;
660 DEVFS_NODE(*ap->a_vpp)->symlink_namelen = targetlen;
661 DEVFS_NODE(*ap->a_vpp)->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
662 memcpy(DEVFS_NODE(*ap->a_vpp)->symlink_name, ap->a_target, targetlen);
663 DEVFS_NODE(*ap->a_vpp)->symlink_name[targetlen] = '\0';
664 cache_setunresolved(ap->a_nch);
665 //problematic to use cache_* inside lockmgr() ? Probably not...
666 cache_setvp(ap->a_nch, *ap->a_vpp);
668 lockmgr(&devfs_lock, LK_RELEASE);
670 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
676 devfs_nremove(struct vop_nremove_args *ap)
678 struct devfs_node *node;
679 struct namecache *ncp;
680 //struct vnode *vp = NULL;
683 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nremove() called!\n");
685 ncp = ap->a_nch->ncp;
687 if (DEVFS_NODE(ap->a_dvp) == NULL)
690 lockmgr(&devfs_lock, LK_EXCLUSIVE);
692 if ((DEVFS_NODE(ap->a_dvp)->node_type != Proot) &&
693 (DEVFS_NODE(ap->a_dvp)->node_type != Pdir)) {
694 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_nremove: ap->a_dvp is not a dir!!!\n");
698 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(DEVFS_NODE(ap->a_dvp)), link) {
699 if (ncp->nc_nlen == node->d_dir.d_namlen) {
700 if (!memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen)) {
701 // allow only removal of user created stuff (e.g. symlinks)
702 if ((node->flags & DEVFS_USER_CREATED) == 0) {
707 cache_inval_vp(node->v_node, CINV_DESTROY);
717 cache_setunresolved(ap->a_nch);
718 cache_setvp(ap->a_nch, NULL);
719 //cache_inval_vp(node->v_node, CINV_DESTROY);
722 lockmgr(&devfs_lock, LK_RELEASE);
730 devfs_spec_open(struct vop_open_args *ap)
732 struct vnode *vp = ap->a_vp;
733 struct vnode *orig_vp = NULL;
734 cdev_t dev, ndev = NULL;
735 struct devfs_node *node = NULL;
739 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open() called\n");
741 if (DEVFS_NODE(vp)) {
742 if (DEVFS_NODE(vp)->d_dev == NULL)
746 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1-\n");
748 if ((dev = vp->v_rdev) == NULL)
751 if (DEVFS_NODE(vp) && ap->a_fp) {
752 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
753 lockmgr(&devfs_lock, LK_EXCLUSIVE);
754 len = DEVFS_NODE(vp)->d_dir.d_namlen;
755 if (!(devfs_clone(DEVFS_NODE(vp)->d_dir.d_name, &len, &ndev, 1, ap->a_cred))) {
756 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.2- |%s|\n", ndev->si_name);
760 node = devfs_create_device_node(DEVFS_MNTDATA(vp->v_mount)->root_node, dev, NULL, NULL);
762 devfs_debug(DEVFS_DEBUG_DEBUG, "parent here is: %s, node is: |%s|\n", (DEVFS_NODE(vp)->parent->node_type == Proot)?"ROOT!":DEVFS_NODE(vp)->parent->d_dir.d_name, node->d_dir.d_name);
763 devfs_debug(DEVFS_DEBUG_DEBUG, "test: %s\n", ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(DEVFS_NODE(vp)->parent), devfs_node_head)))->d_dir.d_name);
766 * orig_vp is set to the original vp if we cloned.
768 /* node->flags |= DEVFS_CLONED; */
769 devfs_allocv(&vp, node);
773 lockmgr(&devfs_lock, LK_RELEASE);
776 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open() called on %s! \n", dev->si_name);
778 * Make this field valid before any I/O in ->d_open
780 if (!dev->si_iosize_max)
781 dev->si_iosize_max = DFLTPHYS;
783 if (dev_dflags(dev) & D_TTY)
784 vp->v_flag |= VISTTY;
787 error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred);
788 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
791 * Clean up any cloned vp if we error out.
794 devfs_debug(DEVFS_DEBUG_DEBUG,
795 "devfs_spec_open() error out: %x\n", error);
799 /* orig_vp = NULL; */
805 if (dev_dflags(dev) & D_TTY) {
810 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs: no t_stop\n");
811 tp->t_stop = nottystop;
817 if (vn_isdisk(vp, NULL)) {
818 if (!dev->si_bsize_phys)
819 dev->si_bsize_phys = DEV_BSIZE;
820 vinitvmio(vp, IDX_TO_OFF(INT_MAX));
825 nanotime(&DEVFS_NODE(vp)->atime);
830 /* Ugly pty magic, to make pty devices appear once they are opened */
831 if (DEVFS_NODE(vp) && ((DEVFS_NODE(vp)->flags & DEVFS_PTY) == DEVFS_PTY))
832 DEVFS_NODE(vp)->flags &= ~DEVFS_INVISIBLE;
835 ap->a_fp->f_type = DTYPE_VNODE;
836 ap->a_fp->f_flag = ap->a_mode & FMASK;
837 ap->a_fp->f_ops = &devfs_dev_fileops;
838 ap->a_fp->f_data = vp;
841 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -end:3-\n");
848 devfs_spec_close(struct vop_close_args *ap)
850 struct proc *p = curproc;
851 struct vnode *vp = ap->a_vp;
852 cdev_t dev = vp->v_rdev;
856 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() called on %s! \n", dev->si_name);
859 * A couple of hacks for devices and tty devices. The
860 * vnode ref count cannot be used to figure out the
861 * last close, but we can use v_opencount now that
862 * revoke works properly.
864 * Detect the last close on a controlling terminal and clear
865 * the session (half-close).
870 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
871 p->p_session->s_ttyvp = NULL;
876 * Vnodes can be opened and closed multiple times. Do not really
877 * close the device unless (1) it is being closed forcibly,
878 * (2) the device wants to track closes, or (3) this is the last
879 * vnode doing its last close on the device.
881 * XXX the VXLOCK (force close) case can leave vnodes referencing
882 * a closed device. This might not occur now that our revoke is
885 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
886 if (dev && ((vp->v_flag & VRECLAIMED) ||
887 (dev_dflags(dev) & D_TRACKCLOSE) ||
888 (vp->v_opencount == 1))) {
890 if (vn_islocked(vp)) {
894 error = dev_dclose(dev, ap->a_fflag, S_IFCHR);
896 if (DEVFS_NODE(vp) && (DEVFS_NODE(vp)->flags & DEVFS_CLONED) == DEVFS_CLONED) {
897 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close: last of the cloned ones, so delete node %s\n", dev->si_name);
898 devfs_unlinkp(DEVFS_NODE(vp));
899 devfs_freep(DEVFS_NODE(vp));
902 /* Ugly pty magic, to make pty devices disappear again once they are closed */
903 if (DEVFS_NODE(vp) && ((DEVFS_NODE(vp)->flags & DEVFS_PTY) == DEVFS_PTY))
904 DEVFS_NODE(vp)->flags |= DEVFS_INVISIBLE;
907 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
911 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
913 * Track the actual opens and closes on the vnode. The last close
914 * disassociates the rdev. If the rdev is already disassociated or the
915 * opencount is already 0, the vnode might have been revoked and no
916 * further opencount tracking occurs.
919 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -3- \n");
920 if (vp->v_opencount == 1) {
922 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -3.5- \n");
926 if (vp->v_opencount > 0) {
927 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -4- \n");
929 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -5- \n");
932 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -end:6- \n");
939 devfs_specf_close(struct file *fp)
942 struct vnode *vp = (struct vnode *)fp->f_data;
944 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_close() called! \n");
946 fp->f_ops = &badfileops;
948 error = vn_close(vp, fp->f_flag);
956 * Device-optimized file table vnode read routine.
958 * This bypasses the VOP table and talks directly to the device. Most
959 * filesystems just route to specfs and can make this optimization.
961 * MPALMOSTSAFE - acquires mplock
964 devfs_specf_read(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
972 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_read() called! \n");
973 KASSERT(uio->uio_td == curthread,
974 ("uio_td %p is not td %p", uio->uio_td, curthread));
976 vp = (struct vnode *)fp->f_data;
977 if (vp == NULL || vp->v_type == VBAD) {
982 if ((dev = vp->v_rdev) == NULL) {
986 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_read() called! for dev %s\n", dev->si_name);
990 if (uio->uio_resid == 0) {
994 if ((flags & O_FOFFSET) == 0)
995 uio->uio_offset = fp->f_offset;
998 if (flags & O_FBLOCKING) {
999 /* ioflag &= ~IO_NDELAY; */
1000 } else if (flags & O_FNONBLOCKING) {
1001 ioflag |= IO_NDELAY;
1002 } else if (fp->f_flag & FNONBLOCK) {
1003 ioflag |= IO_NDELAY;
1005 if (flags & O_FBUFFERED) {
1006 /* ioflag &= ~IO_DIRECT; */
1007 } else if (flags & O_FUNBUFFERED) {
1008 ioflag |= IO_DIRECT;
1009 } else if (fp->f_flag & O_DIRECT) {
1010 ioflag |= IO_DIRECT;
1012 ioflag |= sequential_heuristic(uio, fp);
1014 error = dev_dread(dev, uio, ioflag);
1018 nanotime(&DEVFS_NODE(vp)->atime);
1019 if ((flags & O_FOFFSET) == 0)
1020 fp->f_offset = uio->uio_offset;
1021 fp->f_nextoff = uio->uio_offset;
1024 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_read finished\n");
1030 devfs_specf_write(struct file *fp, struct uio *uio, struct ucred *cred, int flags)
1037 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_write() called! \n");
1039 KASSERT(uio->uio_td == curthread,
1040 ("uio_td %p is not p %p", uio->uio_td, curthread));
1042 vp = (struct vnode *)fp->f_data;
1043 if (vp == NULL || vp->v_type == VBAD) {
1047 if (vp->v_type == VREG)
1048 bwillwrite(uio->uio_resid);
1049 vp = (struct vnode *)fp->f_data;
1051 if ((dev = vp->v_rdev) == NULL) {
1055 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_write() called! for dev %s\n", dev->si_name);
1058 if ((flags & O_FOFFSET) == 0)
1059 uio->uio_offset = fp->f_offset;
1062 if (vp->v_type == VREG &&
1063 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1064 ioflag |= IO_APPEND;
1067 if (flags & O_FBLOCKING) {
1068 /* ioflag &= ~IO_NDELAY; */
1069 } else if (flags & O_FNONBLOCKING) {
1070 ioflag |= IO_NDELAY;
1071 } else if (fp->f_flag & FNONBLOCK) {
1072 ioflag |= IO_NDELAY;
1074 if (flags & O_FBUFFERED) {
1075 /* ioflag &= ~IO_DIRECT; */
1076 } else if (flags & O_FUNBUFFERED) {
1077 ioflag |= IO_DIRECT;
1078 } else if (fp->f_flag & O_DIRECT) {
1079 ioflag |= IO_DIRECT;
1081 if (flags & O_FASYNCWRITE) {
1082 /* ioflag &= ~IO_SYNC; */
1083 } else if (flags & O_FSYNCWRITE) {
1085 } else if (fp->f_flag & O_FSYNC) {
1089 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1091 ioflag |= sequential_heuristic(uio, fp);
1093 error = dev_dwrite(dev, uio, ioflag);
1097 nanotime(&DEVFS_NODE(vp)->mtime);
1099 if ((flags & O_FOFFSET) == 0)
1100 fp->f_offset = uio->uio_offset;
1101 fp->f_nextoff = uio->uio_offset;
1104 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_write done\n");
1110 devfs_specf_stat(struct file *fp, struct stat *sb, struct ucred *cred)
1115 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_stat() called\n");
1118 vp = (struct vnode *)fp->f_data;
1119 error = vn_stat(vp, sb, cred);
1131 error = VOP_GETATTR(vp, vap);
1138 * Zero the spare stat fields
1144 * Copy from vattr table ... or not in case it's a cloned device
1146 if (vap->va_fsid != VNOVAL)
1147 sb->st_dev = vap->va_fsid;
1149 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1151 sb->st_ino = vap->va_fileid;
1153 mode = vap->va_mode;
1157 if (vap->va_nlink > (nlink_t)-1)
1158 sb->st_nlink = (nlink_t)-1;
1160 sb->st_nlink = vap->va_nlink;
1161 sb->st_uid = vap->va_uid;
1162 sb->st_gid = vap->va_gid;
1163 sb->st_rdev = dev2udev(DEVFS_NODE(vp)->d_dev);
1164 sb->st_size = vap->va_size;
1165 sb->st_atimespec = vap->va_atime;
1166 sb->st_mtimespec = vap->va_mtime;
1167 sb->st_ctimespec = vap->va_ctime;
1170 * A VCHR and VBLK device may track the last access and last modified
1171 * time independantly of the filesystem. This is particularly true
1172 * because device read and write calls may bypass the filesystem.
1174 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1177 if (dev->si_lastread) {
1178 sb->st_atimespec.tv_sec = dev->si_lastread;
1179 sb->st_atimespec.tv_nsec = 0;
1181 if (dev->si_lastwrite) {
1182 sb->st_atimespec.tv_sec = dev->si_lastwrite;
1183 sb->st_atimespec.tv_nsec = 0;
1189 * According to www.opengroup.org, the meaning of st_blksize is
1190 * "a filesystem-specific preferred I/O block size for this
1191 * object. In some filesystem types, this may vary from file
1193 * Default to PAGE_SIZE after much discussion.
1196 sb->st_blksize = PAGE_SIZE;
1198 sb->st_flags = vap->va_flags;
1200 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1204 sb->st_gen = (u_int32_t)vap->va_gen;
1206 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1207 sb->st_fsmid = vap->va_fsmid;
1215 devfs_specf_kqfilter(struct file *fp, struct knote *kn)
1222 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_kqfilter() called! \n");
1226 vp = (struct vnode *)fp->f_data;
1227 if (vp == NULL || vp->v_type == VBAD) {
1232 if ((dev = vp->v_rdev) == NULL) {
1238 error = dev_dkqfilter(dev, kn);
1243 nanotime(&DEVFS_NODE(vp)->atime);
1251 devfs_specf_poll(struct file *fp, int events, struct ucred *cred)
1258 //devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_poll() called! \n");
1262 vp = (struct vnode *)fp->f_data;
1263 if (vp == NULL || vp->v_type == VBAD) {
1268 if ((dev = vp->v_rdev) == NULL) {
1273 error = dev_dpoll(dev, events);
1278 nanotime(&DEVFS_NODE(vp)->atime);
1286 * MPALMOSTSAFE - acquires mplock
1289 devfs_specf_ioctl(struct file *fp, u_long com, caddr_t data, struct ucred *ucred)
1291 struct vnode *vp = ((struct vnode *)fp->f_data);
1293 //struct vattr vattr;
1296 struct fiodname_args *name_args;
1300 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_ioctl() called! \n");
1304 if ((dev = vp->v_rdev) == NULL) {
1305 error = EBADF; /* device was revoked */
1308 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_ioctl() called! for dev %s\n", dev->si_name);
1310 if (!(dev_dflags(dev) & D_TTY))
1311 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_ioctl() called on %s! com is: %x\n", dev->si_name, com);
1313 if (com == FIODTYPE) {
1314 *(int *)data = dev_dflags(dev) & D_TYPEMASK;
1317 } else if (com == FIODNAME) {
1318 name_args = (struct fiodname_args *)data;
1319 name = dev->si_name;
1320 namlen = strlen(name) + 1;
1322 devfs_debug(DEVFS_DEBUG_DEBUG, "ioctl, got: FIODNAME for %s\n", name);
1324 if (namlen <= name_args->len)
1325 error = copyout(dev->si_name, name_args->name, namlen);
1329 //name_args->len = namlen; //need _IOWR to enable this
1330 devfs_debug(DEVFS_DEBUG_DEBUG, "ioctl stuff: error: %d\n", error);
1334 error = dev_dioctl(dev, com, data, fp->f_flag, ucred);
1336 if (DEVFS_NODE(vp)) {
1337 nanotime(&DEVFS_NODE(vp)->atime);
1338 nanotime(&DEVFS_NODE(vp)->mtime);
1341 if (com == TIOCSCTTY)
1342 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_ioctl: got TIOCSCTTY on %s\n", dev->si_name);
1343 if (error == 0 && com == TIOCSCTTY) {
1344 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_ioctl: dealing with TIOCSCTTY on %s\n", dev->si_name);
1345 struct proc *p = curthread->td_proc;
1346 struct session *sess;
1351 sess = p->p_session;
1352 /* Do nothing if reassigning same control tty */
1353 if (sess->s_ttyvp == vp) {
1357 /* Get rid of reference to old control tty */
1358 ovp = sess->s_ttyvp;
1367 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_specf_ioctl() finished! \n");
1373 devfs_spec_fsync(struct vop_fsync_args *ap)
1375 struct vnode *vp = ap->a_vp;
1378 if (!vn_isdisk(vp, NULL))
1382 * Flush all dirty buffers associated with a block device.
1384 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1408 devfs_spec_read(struct vop_read_args *ap)
1419 if (dev == NULL) /* device was revoked */
1421 if (uio->uio_resid == 0)
1425 error = dev_dread(dev, uio, ap->a_ioflag);
1426 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1429 nanotime(&DEVFS_NODE(vp)->atime);
1435 * Vnode op for write
1437 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1438 * struct ucred *a_cred)
1442 devfs_spec_write(struct vop_write_args *ap)
1453 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1455 if (dev == NULL) /* device was revoked */
1459 error = dev_dwrite(dev, uio, ap->a_ioflag);
1460 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1463 nanotime(&DEVFS_NODE(vp)->mtime);
1469 * Device ioctl operation.
1471 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
1472 * int a_fflag, struct ucred *a_cred)
1476 devfs_spec_ioctl(struct vop_ioctl_args *ap)
1479 struct vnode *vp = ap->a_vp;
1481 if ((dev = vp->v_rdev) == NULL)
1482 return (EBADF); /* device was revoked */
1483 if ( ap->a_command == TIOCSCTTY )
1484 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_*SPEC*_ioctl: got TIOCSCTTY\n");
1486 if (DEVFS_NODE(vp)) {
1487 nanotime(&DEVFS_NODE(vp)->atime);
1488 nanotime(&DEVFS_NODE(vp)->mtime);
1491 return (dev_dioctl(dev, ap->a_command, ap->a_data,
1492 ap->a_fflag, ap->a_cred));
1496 * spec_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred)
1500 devfs_spec_poll(struct vop_poll_args *ap)
1503 struct vnode *vp = ap->a_vp;
1505 if ((dev = vp->v_rdev) == NULL)
1506 return (EBADF); /* device was revoked */
1509 nanotime(&DEVFS_NODE(vp)->atime);
1511 return (dev_dpoll(dev, ap->a_events));
1515 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1519 devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1522 struct vnode *vp = ap->a_vp;
1524 if ((dev = vp->v_rdev) == NULL)
1525 return (EBADF); /* device was revoked */
1528 nanotime(&DEVFS_NODE(vp)->atime);
1530 return (dev_dkqfilter(dev, ap->a_kn));
1575 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1576 * calls are not limited to device DMA limits so we have to deal with the
1579 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1582 devfs_spec_strategy(struct vop_strategy_args *ap)
1584 struct bio *bio = ap->a_bio;
1585 struct buf *bp = bio->bio_buf;
1592 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1596 * Collect statistics on synchronous and asynchronous read
1597 * and write counts for disks that have associated filesystems.
1600 KKASSERT(vp->v_rdev != NULL); /* XXX */
1601 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1602 if (bp->b_cmd == BUF_CMD_READ) {
1603 //XXX: no idea what has changed here...
1604 if (bp->b_flags & BIO_SYNC)
1605 mp->mnt_stat.f_syncreads++;
1607 mp->mnt_stat.f_asyncreads++;
1609 if (bp->b_flags & BIO_SYNC)
1610 mp->mnt_stat.f_syncwrites++;
1612 mp->mnt_stat.f_asyncwrites++;
1617 * Device iosize limitations only apply to read and write. Shortcut
1618 * the I/O if it fits.
1620 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1621 devfs_debug(DEVFS_DEBUG_DEBUG, "%s: si_iosize_max not set!\n", dev_dname(vp->v_rdev));
1622 maxiosize = MAXPHYS;
1624 #if SPEC_CHAIN_DEBUG & 2
1627 if (bp->b_bcount <= maxiosize ||
1628 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1629 dev_dstrategy_chain(vp->v_rdev, bio);
1634 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1636 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1640 BUF_LOCK(nbp, LK_EXCLUSIVE);
1643 nbp->b_flags = B_PAGING | (bp->b_flags & B_BNOCLIP);
1644 nbp->b_data = bp->b_data;
1645 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1646 nbp->b_bio1.bio_offset = bio->bio_offset;
1647 nbp->b_bio1.bio_caller_info1.ptr = bio;
1650 * Start the first transfer
1652 if (vn_isdisk(vp, NULL))
1653 chunksize = vp->v_rdev->si_bsize_phys;
1655 chunksize = DEV_BSIZE;
1656 chunksize = maxiosize / chunksize * chunksize;
1657 #if SPEC_CHAIN_DEBUG & 1
1658 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy chained I/O chunksize=%d\n", chunksize);
1660 nbp->b_cmd = bp->b_cmd;
1661 nbp->b_bcount = chunksize;
1662 nbp->b_bufsize = chunksize; /* used to detect a short I/O */
1663 nbp->b_bio1.bio_caller_info2.index = chunksize;
1665 #if SPEC_CHAIN_DEBUG & 1
1666 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p offset %d/%d bcount %d\n",
1667 bp, 0, bp->b_bcount, nbp->b_bcount);
1670 dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1672 if (DEVFS_NODE(vp)) {
1673 nanotime(&DEVFS_NODE(vp)->atime);
1674 nanotime(&DEVFS_NODE(vp)->mtime);
1681 * Chunked up transfer completion routine - chain transfers until done
1685 devfs_spec_strategy_done(struct bio *nbio)
1687 struct buf *nbp = nbio->bio_buf;
1688 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
1689 struct buf *bp = bio->bio_buf; /* original bp */
1690 int chunksize = nbio->bio_caller_info2.index; /* chunking */
1691 int boffset = nbp->b_data - bp->b_data;
1693 if (nbp->b_flags & B_ERROR) {
1695 * An error terminates the chain, propogate the error back
1696 * to the original bp
1698 bp->b_flags |= B_ERROR;
1699 bp->b_error = nbp->b_error;
1700 bp->b_resid = bp->b_bcount - boffset +
1701 (nbp->b_bcount - nbp->b_resid);
1702 #if SPEC_CHAIN_DEBUG & 1
1703 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p error %d bcount %d/%d\n",
1704 bp, bp->b_error, bp->b_bcount,
1705 bp->b_bcount - bp->b_resid);
1707 kfree(nbp, M_DEVBUF);
1709 } else if (nbp->b_resid) {
1711 * A short read or write terminates the chain
1713 bp->b_error = nbp->b_error;
1714 bp->b_resid = bp->b_bcount - boffset +
1715 (nbp->b_bcount - nbp->b_resid);
1716 #if SPEC_CHAIN_DEBUG & 1
1717 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p short read(1) bcount %d/%d\n",
1718 bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
1720 kfree(nbp, M_DEVBUF);
1722 } else if (nbp->b_bcount != nbp->b_bufsize) {
1724 * A short read or write can also occur by truncating b_bcount
1726 #if SPEC_CHAIN_DEBUG & 1
1727 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p short read(2) bcount %d/%d\n",
1728 bp, nbp->b_bcount + boffset, bp->b_bcount);
1731 bp->b_bcount = nbp->b_bcount + boffset;
1732 bp->b_resid = nbp->b_resid;
1733 kfree(nbp, M_DEVBUF);
1735 } else if (nbp->b_bcount + boffset == bp->b_bcount) {
1737 * No more data terminates the chain
1739 #if SPEC_CHAIN_DEBUG & 1
1740 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p finished bcount %d\n",
1745 kfree(nbp, M_DEVBUF);
1749 * Continue the chain
1751 boffset += nbp->b_bcount;
1752 nbp->b_data = bp->b_data + boffset;
1753 nbp->b_bcount = bp->b_bcount - boffset;
1754 if (nbp->b_bcount > chunksize)
1755 nbp->b_bcount = chunksize;
1756 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1757 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1759 #if SPEC_CHAIN_DEBUG & 1
1760 devfs_debug(DEVFS_DEBUG_DEBUG, "spec_strategy: chain %p offset %d/%d bcount %d\n",
1761 bp, boffset, bp->b_bcount, nbp->b_bcount);
1764 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
1769 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1772 devfs_spec_freeblks(struct vop_freeblks_args *ap)
1777 * XXX: This assumes that strategy does the deed right away.
1778 * XXX: this may not be TRTTD.
1780 KKASSERT(ap->a_vp->v_rdev != NULL);
1781 if ((dev_dflags(ap->a_vp->v_rdev) & D_CANFREE) == 0)
1783 bp = geteblk(ap->a_length);
1784 bp->b_cmd = BUF_CMD_FREEBLKS;
1785 bp->b_bio1.bio_offset = ap->a_offset;
1786 bp->b_bcount = ap->a_length;
1787 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1792 * Implement degenerate case where the block requested is the block
1793 * returned, and assume that the entire device is contiguous in regards
1794 * to the contiguous block range (runp and runb).
1796 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1797 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1800 devfs_spec_bmap(struct vop_bmap_args *ap)
1802 if (ap->a_doffsetp != NULL)
1803 *ap->a_doffsetp = ap->a_loffset;
1804 if (ap->a_runp != NULL)
1805 *ap->a_runp = MAXBSIZE;
1806 if (ap->a_runb != NULL) {
1807 if (ap->a_loffset < MAXBSIZE)
1808 *ap->a_runb = (int)ap->a_loffset;
1810 *ap->a_runb = MAXBSIZE;
1817 * Special device advisory byte-level locks.
1819 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1820 * struct flock *a_fl, int a_flags)
1824 devfs_spec_advlock(struct vop_advlock_args *ap)
1826 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
1830 devfs_spec_getpages_iodone(struct bio *bio)
1832 bio->bio_buf->b_cmd = BUF_CMD_DONE;
1833 wakeup(bio->bio_buf);
1837 * spec_getpages() - get pages associated with device vnode.
1839 * Note that spec_read and spec_write do not use the buffer cache, so we
1840 * must fully implement getpages here.
1843 devfs_spec_getpages(struct vop_getpages_args *ap)
1847 int i, pcount, size;
1850 vm_ooffset_t offset;
1851 int toff, nextoff, nread;
1852 struct vnode *vp = ap->a_vp;
1857 pcount = round_page(ap->a_count) / PAGE_SIZE;
1860 * Calculate the offset of the transfer and do sanity check.
1862 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
1865 * Round up physical size for real devices. We cannot round using
1866 * v_mount's block size data because v_mount has nothing to do with
1867 * the device. i.e. it's usually '/dev'. We need the physical block
1868 * size for the device itself.
1870 * We can't use v_rdev->si_mountpoint because it only exists when the
1871 * block device is mounted. However, we can use v_rdev.
1874 if (vn_isdisk(vp, NULL))
1875 blksiz = vp->v_rdev->si_bsize_phys;
1879 size = (ap->a_count + blksiz - 1) & ~(blksiz - 1);
1882 kva = (vm_offset_t)bp->b_data;
1885 * Map the pages to be read into the kva.
1887 pmap_qenter(kva, ap->a_m, pcount);
1889 /* Build a minimal buffer header. */
1890 bp->b_cmd = BUF_CMD_READ;
1891 bp->b_bcount = size;
1893 bp->b_runningbufspace = size;
1895 runningbufspace += bp->b_runningbufspace;
1899 bp->b_bio1.bio_offset = offset;
1900 bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
1902 mycpu->gd_cnt.v_vnodein++;
1903 mycpu->gd_cnt.v_vnodepgsin += pcount;
1906 vn_strategy(ap->a_vp, &bp->b_bio1);
1910 /* We definitely need to be at splbio here. */
1911 while (bp->b_cmd != BUF_CMD_DONE)
1912 tsleep(bp, 0, "spread", 0);
1916 if (bp->b_flags & B_ERROR) {
1918 error = bp->b_error;
1924 * If EOF is encountered we must zero-extend the result in order
1925 * to ensure that the page does not contain garabge. When no
1926 * error occurs, an early EOF is indicated if b_bcount got truncated.
1927 * b_resid is relative to b_bcount and should be 0, but some devices
1928 * might indicate an EOF with b_resid instead of truncating b_bcount.
1930 nread = bp->b_bcount - bp->b_resid;
1931 if (nread < ap->a_count)
1932 bzero((caddr_t)kva + nread, ap->a_count - nread);
1933 pmap_qremove(kva, pcount);
1936 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
1937 nextoff = toff + PAGE_SIZE;
1940 m->flags &= ~PG_ZERO;
1942 if (nextoff <= nread) {
1943 m->valid = VM_PAGE_BITS_ALL;
1945 } else if (toff < nread) {
1947 * Since this is a VM request, we have to supply the
1948 * unaligned offset to allow vm_page_set_validclean()
1949 * to zero sub-DEV_BSIZE'd portions of the page.
1951 vm_page_set_validclean(m, 0, nread - toff);
1957 if (i != ap->a_reqpage) {
1959 * Just in case someone was asking for this page we
1960 * now tell them that it is ok to use.
1962 if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
1964 if (m->flags & PG_WANTED) {
1965 vm_page_activate(m);
1967 vm_page_deactivate(m);
1976 } else if (m->valid) {
1979 * Since this is a VM request, we need to make the
1980 * entire page presentable by zeroing invalid sections.
1982 if (m->valid != VM_PAGE_BITS_ALL)
1983 vm_page_zero_invalid(m, FALSE);
1987 m = ap->a_m[ap->a_reqpage];
1988 devfs_debug(DEVFS_DEBUG_WARNING,
1989 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
1990 devtoname(vp->v_rdev), error, bp, bp->b_vp);
1991 devfs_debug(DEVFS_DEBUG_WARNING,
1992 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
1993 size, bp->b_resid, ap->a_count, m->valid);
1994 devfs_debug(DEVFS_DEBUG_WARNING,
1995 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
1996 nread, ap->a_reqpage, (u_long)m->pindex, pcount);
1998 * Free the buffer header back to the swap buffer pool.
2001 return VM_PAGER_ERROR;
2004 * Free the buffer header back to the swap buffer pool.
2050 sequential_heuristic(struct uio *uio, struct file *fp)
2053 * Sequential heuristic - detect sequential operation
2055 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2056 uio->uio_offset == fp->f_nextoff) {
2057 int tmpseq = fp->f_seqcount;
2059 * XXX we assume that the filesystem block size is
2060 * the default. Not true, but still gives us a pretty
2061 * good indicator of how sequential the read operations
2064 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
2065 if (tmpseq > IO_SEQMAX)
2067 fp->f_seqcount = tmpseq;
2068 return(fp->f_seqcount << IO_SEQSHIFT);
2072 * Not sequential, quick draw-down of seqcount
2074 if (fp->f_seqcount > 1)