4 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
6 * This code is derived from software contributed to The DragonFly Project
7 * by Alex Hornung <ahornung@gmail.com>
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
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.
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
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/kernel.h>
41 #include <sys/fcntl.h>
44 #include <sys/signalvar.h>
45 #include <sys/vnode.h>
47 #include <sys/mount.h>
49 #include <sys/fcntl.h>
50 #include <sys/namei.h>
51 #include <sys/dirent.h>
52 #include <sys/malloc.h>
55 #include <vm/vm_pager.h>
56 #include <vm/vm_zone.h>
57 #include <vm/vm_object.h>
58 #include <sys/filio.h>
59 #include <sys/ttycom.h>
61 #include <sys/diskslice.h>
62 #include <sys/sysctl.h>
63 #include <sys/devfs.h>
64 #include <sys/pioctl.h>
65 #include <vfs/fifofs/fifo.h>
67 #include <machine/limits.h>
70 #include <sys/sysref2.h>
71 #include <sys/mplock2.h>
72 #include <vm/vm_page2.h>
74 MALLOC_DECLARE(M_DEVFS);
75 #define DEVFS_BADOP (void *)devfs_vop_badop
77 static int devfs_vop_badop(struct vop_generic_args *);
78 static int devfs_vop_access(struct vop_access_args *);
79 static int devfs_vop_inactive(struct vop_inactive_args *);
80 static int devfs_vop_reclaim(struct vop_reclaim_args *);
81 static int devfs_vop_readdir(struct vop_readdir_args *);
82 static int devfs_vop_getattr(struct vop_getattr_args *);
83 static int devfs_vop_setattr(struct vop_setattr_args *);
84 static int devfs_vop_readlink(struct vop_readlink_args *);
85 static int devfs_vop_print(struct vop_print_args *);
87 static int devfs_vop_nresolve(struct vop_nresolve_args *);
88 static int devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
89 static int devfs_vop_nmkdir(struct vop_nmkdir_args *);
90 static int devfs_vop_nsymlink(struct vop_nsymlink_args *);
91 static int devfs_vop_nrmdir(struct vop_nrmdir_args *);
92 static int devfs_vop_nremove(struct vop_nremove_args *);
94 static int devfs_spec_open(struct vop_open_args *);
95 static int devfs_spec_close(struct vop_close_args *);
96 static int devfs_spec_fsync(struct vop_fsync_args *);
98 static int devfs_spec_read(struct vop_read_args *);
99 static int devfs_spec_write(struct vop_write_args *);
100 static int devfs_spec_ioctl(struct vop_ioctl_args *);
101 static int devfs_spec_kqfilter(struct vop_kqfilter_args *);
102 static int devfs_spec_strategy(struct vop_strategy_args *);
103 static void devfs_spec_strategy_done(struct bio *);
104 static int devfs_spec_freeblks(struct vop_freeblks_args *);
105 static int devfs_spec_bmap(struct vop_bmap_args *);
106 static int devfs_spec_advlock(struct vop_advlock_args *);
107 static void devfs_spec_getpages_iodone(struct bio *);
108 static int devfs_spec_getpages(struct vop_getpages_args *);
110 static int devfs_fo_close(struct file *);
111 static int devfs_fo_read(struct file *, struct uio *, struct ucred *, int);
112 static int devfs_fo_write(struct file *, struct uio *, struct ucred *, int);
113 static int devfs_fo_stat(struct file *, struct stat *, struct ucred *);
114 static int devfs_fo_kqfilter(struct file *, struct knote *);
115 static int devfs_fo_ioctl(struct file *, u_long, caddr_t,
116 struct ucred *, struct sysmsg *);
117 static __inline int sequential_heuristic(struct uio *, struct file *);
119 extern struct lock devfs_lock;
122 * devfs vnode operations for regular files. All vnode ops are MPSAFE.
124 struct vop_ops devfs_vnode_norm_vops = {
125 .vop_default = vop_defaultop,
126 .vop_access = devfs_vop_access,
127 .vop_advlock = DEVFS_BADOP,
128 .vop_bmap = DEVFS_BADOP,
129 .vop_close = vop_stdclose,
130 .vop_getattr = devfs_vop_getattr,
131 .vop_inactive = devfs_vop_inactive,
132 .vop_ncreate = DEVFS_BADOP,
133 .vop_nresolve = devfs_vop_nresolve,
134 .vop_nlookupdotdot = devfs_vop_nlookupdotdot,
135 .vop_nlink = DEVFS_BADOP,
136 .vop_nmkdir = devfs_vop_nmkdir,
137 .vop_nmknod = DEVFS_BADOP,
138 .vop_nremove = devfs_vop_nremove,
139 .vop_nrename = DEVFS_BADOP,
140 .vop_nrmdir = devfs_vop_nrmdir,
141 .vop_nsymlink = devfs_vop_nsymlink,
142 .vop_open = vop_stdopen,
143 .vop_pathconf = vop_stdpathconf,
144 .vop_print = devfs_vop_print,
145 .vop_read = DEVFS_BADOP,
146 .vop_readdir = devfs_vop_readdir,
147 .vop_readlink = devfs_vop_readlink,
148 .vop_reclaim = devfs_vop_reclaim,
149 .vop_setattr = devfs_vop_setattr,
150 .vop_write = DEVFS_BADOP,
151 .vop_ioctl = DEVFS_BADOP
155 * devfs vnode operations for character devices. All vnode ops are MPSAFE.
157 struct vop_ops devfs_vnode_dev_vops = {
158 .vop_default = vop_defaultop,
159 .vop_access = devfs_vop_access,
160 .vop_advlock = devfs_spec_advlock,
161 .vop_bmap = devfs_spec_bmap,
162 .vop_close = devfs_spec_close,
163 .vop_freeblks = devfs_spec_freeblks,
164 .vop_fsync = devfs_spec_fsync,
165 .vop_getattr = devfs_vop_getattr,
166 .vop_getpages = devfs_spec_getpages,
167 .vop_inactive = devfs_vop_inactive,
168 .vop_open = devfs_spec_open,
169 .vop_pathconf = vop_stdpathconf,
170 .vop_print = devfs_vop_print,
171 .vop_kqfilter = devfs_spec_kqfilter,
172 .vop_read = devfs_spec_read,
173 .vop_readdir = DEVFS_BADOP,
174 .vop_readlink = DEVFS_BADOP,
175 .vop_reclaim = devfs_vop_reclaim,
176 .vop_setattr = devfs_vop_setattr,
177 .vop_strategy = devfs_spec_strategy,
178 .vop_write = devfs_spec_write,
179 .vop_ioctl = devfs_spec_ioctl
183 * devfs file pointer operations. All fileops are MPSAFE.
185 struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops;
187 struct fileops devfs_dev_fileops = {
188 .fo_read = devfs_fo_read,
189 .fo_write = devfs_fo_write,
190 .fo_ioctl = devfs_fo_ioctl,
191 .fo_kqfilter = devfs_fo_kqfilter,
192 .fo_stat = devfs_fo_stat,
193 .fo_close = devfs_fo_close,
194 .fo_shutdown = nofo_shutdown
198 * These two functions are possibly temporary hacks for devices (aka
199 * the pty code) which want to control the node attributes themselves.
201 * XXX we may ultimately desire to simply remove the uid/gid/mode
202 * from the node entirely.
204 * MPSAFE - sorta. Theoretically the overwrite can compete since they
205 * are loading from the same fields.
208 node_sync_dev_get(struct devfs_node *node)
212 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
213 node->uid = dev->si_uid;
214 node->gid = dev->si_gid;
215 node->mode = dev->si_perms;
220 node_sync_dev_set(struct devfs_node *node)
224 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
225 dev->si_uid = node->uid;
226 dev->si_gid = node->gid;
227 dev->si_perms = node->mode;
232 * generic entry point for unsupported operations
235 devfs_vop_badop(struct vop_generic_args *ap)
242 devfs_vop_access(struct vop_access_args *ap)
244 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
247 if (!devfs_node_is_accessible(node))
249 node_sync_dev_get(node);
250 error = vop_helper_access(ap, node->uid, node->gid,
251 node->mode, node->flags);
258 devfs_vop_inactive(struct vop_inactive_args *ap)
260 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
262 if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0)
269 devfs_vop_reclaim(struct vop_reclaim_args *ap)
271 struct devfs_node *node;
276 * Check if it is locked already. if not, we acquire the devfs lock
278 if (!(lockstatus(&devfs_lock, curthread)) == LK_EXCLUSIVE) {
279 lockmgr(&devfs_lock, LK_EXCLUSIVE);
286 * Get rid of the devfs_node if it is no longer linked into the
290 if ((node = DEVFS_NODE(vp)) != NULL) {
292 if ((node->flags & DEVFS_NODE_LINKED) == 0)
297 lockmgr(&devfs_lock, LK_RELEASE);
300 * v_rdev needs to be properly released using v_release_rdev
301 * Make sure v_data is NULL as well.
310 devfs_vop_readdir(struct vop_readdir_args *ap)
312 struct devfs_node *dnode = DEVFS_NODE(ap->a_vp);
313 struct devfs_node *node;
322 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
324 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
326 if ((error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY)) != 0)
329 if (!devfs_node_is_accessible(dnode)) {
334 lockmgr(&devfs_lock, LK_EXCLUSIVE);
336 saveoff = ap->a_uio->uio_offset;
338 if (ap->a_ncookies) {
339 ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
342 cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
350 nanotime(&dnode->atime);
353 r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino,
358 cookies[cookie_index] = saveoff;
361 if (cookie_index == ncookies)
367 r = vop_write_dirent(&error, ap->a_uio,
368 dnode->parent->d_dir.d_ino,
371 r = vop_write_dirent(&error, ap->a_uio,
378 cookies[cookie_index] = saveoff;
381 if (cookie_index == ncookies)
385 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
386 if ((node->flags & DEVFS_HIDDEN) ||
387 (node->flags & DEVFS_INVISIBLE)) {
392 * If the node type is a valid devfs alias, then we make
393 * sure that the target isn't hidden. If it is, we don't
394 * show the link in the directory listing.
396 if ((node->node_type == Plink) && (node->link_target != NULL) &&
397 (node->link_target->flags & DEVFS_HIDDEN))
400 if (node->cookie < saveoff)
403 saveoff = node->cookie;
405 error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino,
407 node->d_dir.d_namlen,
416 cookies[cookie_index] = node->cookie;
418 if (cookie_index == ncookies)
423 lockmgr(&devfs_lock, LK_RELEASE);
426 ap->a_uio->uio_offset = saveoff;
427 if (error && cookie_index == 0) {
429 kfree(cookies, M_TEMP);
431 *ap->a_cookies = NULL;
435 *ap->a_ncookies = cookie_index;
436 *ap->a_cookies = cookies;
444 devfs_vop_nresolve(struct vop_nresolve_args *ap)
446 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
447 struct devfs_node *node, *found = NULL;
448 struct namecache *ncp;
449 struct vnode *vp = NULL;
454 ncp = ap->a_nch->ncp;
457 if (!devfs_node_is_accessible(dnode))
460 lockmgr(&devfs_lock, LK_EXCLUSIVE);
462 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir)) {
464 cache_setvp(ap->a_nch, NULL);
468 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
469 if (len == node->d_dir.d_namlen) {
470 if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
479 while ((found->node_type == Plink) && (found->link_target)) {
481 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
485 found = found->link_target;
489 if (!(found->flags & DEVFS_HIDDEN))
490 devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
495 cache_setvp(ap->a_nch, NULL);
501 cache_setvp(ap->a_nch, vp);
504 lockmgr(&devfs_lock, LK_RELEASE);
511 devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
513 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
516 if (!devfs_node_is_accessible(dnode))
519 lockmgr(&devfs_lock, LK_EXCLUSIVE);
520 if (dnode->parent != NULL) {
521 devfs_allocv(ap->a_vpp, dnode->parent);
522 vn_unlock(*ap->a_vpp);
524 lockmgr(&devfs_lock, LK_RELEASE);
526 return ((*ap->a_vpp == NULL) ? ENOENT : 0);
531 devfs_vop_getattr(struct vop_getattr_args *ap)
533 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
534 struct vattr *vap = ap->a_vap;
535 struct partinfo pinfo;
539 if (!devfs_node_is_accessible(node))
542 node_sync_dev_get(node);
544 lockmgr(&devfs_lock, LK_EXCLUSIVE);
546 /* start by zeroing out the attributes */
549 /* next do all the common fields */
550 vap->va_type = ap->a_vp->v_type;
551 vap->va_mode = node->mode;
552 vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
554 vap->va_blocksize = DEV_BSIZE;
555 vap->va_bytes = vap->va_size = 0;
557 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
559 vap->va_atime = node->atime;
560 vap->va_mtime = node->mtime;
561 vap->va_ctime = node->ctime;
563 vap->va_nlink = 1; /* number of references to file */
565 vap->va_uid = node->uid;
566 vap->va_gid = node->gid;
571 if ((node->node_type == Pdev) && node->d_dev) {
572 reference_dev(node->d_dev);
573 vap->va_rminor = node->d_dev->si_uminor;
574 release_dev(node->d_dev);
577 /* For a softlink the va_size is the length of the softlink */
578 if (node->symlink_name != 0) {
579 vap->va_bytes = vap->va_size = node->symlink_namelen;
583 * For a disk-type device, va_size is the size of the underlying
584 * device, so that lseek() works properly.
586 if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) {
587 bzero(&pinfo, sizeof(pinfo));
588 error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo,
589 0, proc0.p_ucred, NULL);
590 if ((error == 0) && (pinfo.media_blksize != 0)) {
591 vap->va_size = pinfo.media_size;
598 lockmgr(&devfs_lock, LK_RELEASE);
605 devfs_vop_setattr(struct vop_setattr_args *ap)
607 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
614 if (!devfs_node_is_accessible(node))
616 node_sync_dev_get(node);
618 lockmgr(&devfs_lock, LK_EXCLUSIVE);
622 if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) {
625 cur_mode = node->mode;
626 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
627 ap->a_cred, &cur_uid, &cur_gid, &cur_mode);
631 if (node->uid != cur_uid || node->gid != cur_gid) {
634 node->mode = cur_mode;
638 if (vap->va_mode != (mode_t)VNOVAL) {
639 cur_mode = node->mode;
640 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
641 node->uid, node->gid, &cur_mode);
642 if (error == 0 && node->mode != cur_mode) {
643 node->mode = cur_mode;
648 node_sync_dev_set(node);
649 nanotime(&node->ctime);
650 lockmgr(&devfs_lock, LK_RELEASE);
657 devfs_vop_readlink(struct vop_readlink_args *ap)
659 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
662 if (!devfs_node_is_accessible(node))
665 lockmgr(&devfs_lock, LK_EXCLUSIVE);
666 ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
667 lockmgr(&devfs_lock, LK_RELEASE);
674 devfs_vop_print(struct vop_print_args *ap)
680 devfs_vop_nmkdir(struct vop_nmkdir_args *ap)
682 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
683 struct devfs_node *node;
685 if (!devfs_node_is_accessible(dnode))
688 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
691 lockmgr(&devfs_lock, LK_EXCLUSIVE);
692 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Pdir,
693 ap->a_nch->ncp->nc_name, dnode, NULL);
696 node = DEVFS_NODE(*ap->a_vpp);
697 node->flags |= DEVFS_USER_CREATED;
698 cache_setunresolved(ap->a_nch);
699 cache_setvp(ap->a_nch, *ap->a_vpp);
701 lockmgr(&devfs_lock, LK_RELEASE);
703 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
707 devfs_vop_nsymlink(struct vop_nsymlink_args *ap)
709 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
710 struct devfs_node *node;
713 if (!devfs_node_is_accessible(dnode))
716 ap->a_vap->va_type = VLNK;
718 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
721 lockmgr(&devfs_lock, LK_EXCLUSIVE);
722 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Plink,
723 ap->a_nch->ncp->nc_name, dnode, NULL);
725 targetlen = strlen(ap->a_target);
727 node = DEVFS_NODE(*ap->a_vpp);
728 node->flags |= DEVFS_USER_CREATED;
729 node->symlink_namelen = targetlen;
730 node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
731 memcpy(node->symlink_name, ap->a_target, targetlen);
732 node->symlink_name[targetlen] = '\0';
733 cache_setunresolved(ap->a_nch);
734 cache_setvp(ap->a_nch, *ap->a_vpp);
736 lockmgr(&devfs_lock, LK_RELEASE);
738 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
742 devfs_vop_nrmdir(struct vop_nrmdir_args *ap)
744 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
745 struct devfs_node *node;
746 struct namecache *ncp;
749 ncp = ap->a_nch->ncp;
751 if (!devfs_node_is_accessible(dnode))
754 lockmgr(&devfs_lock, LK_EXCLUSIVE);
756 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
759 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
760 if (ncp->nc_nlen != node->d_dir.d_namlen)
762 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
766 * only allow removal of user created dirs
768 if ((node->flags & DEVFS_USER_CREATED) == 0) {
771 } else if (node->node_type != Pdir) {
774 } else if (node->nchildren > 2) {
779 cache_inval_vp(node->v_node, CINV_DESTROY);
786 cache_setunresolved(ap->a_nch);
787 cache_setvp(ap->a_nch, NULL);
790 lockmgr(&devfs_lock, LK_RELEASE);
795 devfs_vop_nremove(struct vop_nremove_args *ap)
797 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
798 struct devfs_node *node;
799 struct namecache *ncp;
802 ncp = ap->a_nch->ncp;
804 if (!devfs_node_is_accessible(dnode))
807 lockmgr(&devfs_lock, LK_EXCLUSIVE);
809 if ((dnode->node_type != Proot) && (dnode->node_type != Pdir))
812 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
813 if (ncp->nc_nlen != node->d_dir.d_namlen)
815 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
819 * only allow removal of user created stuff (e.g. symlinks)
821 if ((node->flags & DEVFS_USER_CREATED) == 0) {
824 } else if (node->node_type == Pdir) {
829 cache_inval_vp(node->v_node, CINV_DESTROY);
836 cache_setunresolved(ap->a_nch);
837 cache_setvp(ap->a_nch, NULL);
840 lockmgr(&devfs_lock, LK_RELEASE);
846 devfs_spec_open(struct vop_open_args *ap)
848 struct vnode *vp = ap->a_vp;
849 struct vnode *orig_vp = NULL;
850 struct devfs_node *node = DEVFS_NODE(vp);
851 struct devfs_node *newnode;
852 cdev_t dev, ndev = NULL;
856 if (node->d_dev == NULL)
858 if (!devfs_node_is_accessible(node))
862 if ((dev = vp->v_rdev) == NULL)
865 if (node && ap->a_fp) {
866 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
867 lockmgr(&devfs_lock, LK_EXCLUSIVE);
869 ndev = devfs_clone(dev, node->d_dir.d_name, node->d_dir.d_namlen,
870 ap->a_mode, ap->a_cred);
872 newnode = devfs_create_device_node(
873 DEVFS_MNTDATA(vp->v_mount)->root_node,
875 /* XXX: possibly destroy device if this happens */
877 if (newnode != NULL) {
881 devfs_debug(DEVFS_DEBUG_DEBUG,
882 "parent here is: %s, node is: |%s|\n",
883 ((node->parent->node_type == Proot) ?
884 "ROOT!" : node->parent->d_dir.d_name),
885 newnode->d_dir.d_name);
886 devfs_debug(DEVFS_DEBUG_DEBUG,
888 ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
891 * orig_vp is set to the original vp if we cloned.
893 /* node->flags |= DEVFS_CLONED; */
894 devfs_allocv(&vp, newnode);
899 lockmgr(&devfs_lock, LK_RELEASE);
902 devfs_debug(DEVFS_DEBUG_DEBUG,
903 "devfs_spec_open() called on %s! \n",
907 * Make this field valid before any I/O in ->d_open
909 if (!dev->si_iosize_max)
910 /* XXX: old DFLTPHYS == 64KB dependency */
911 dev->si_iosize_max = min(MAXPHYS,64*1024);
913 if (dev_dflags(dev) & D_TTY)
914 vsetflags(vp, VISTTY);
917 * Open underlying device
920 error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred);
921 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
924 * Clean up any cloned vp if we error out.
930 /* orig_vp = NULL; */
936 * This checks if the disk device is going to be opened for writing.
937 * It will be only allowed in the cases where securelevel permits it
938 * and it's not mounted R/W.
940 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
941 (ap->a_cred != FSCRED)) {
943 /* Very secure mode. No open for writing allowed */
944 if (securelevel >= 2)
948 * If it is mounted R/W, do not allow to open for writing.
949 * In the case it's mounted read-only but securelevel
950 * is >= 1, then do not allow opening for writing either.
952 if (vfs_mountedon(vp)) {
953 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
955 else if (securelevel >= 1)
960 if (dev_dflags(dev) & D_TTY) {
965 devfs_debug(DEVFS_DEBUG_DEBUG,
966 "devfs: no t_stop\n");
967 tp->t_stop = nottystop;
973 if (vn_isdisk(vp, NULL)) {
974 if (!dev->si_bsize_phys)
975 dev->si_bsize_phys = DEV_BSIZE;
976 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
982 nanotime(&node->atime);
986 * If we replaced the vp the vop_stdopen() call will have loaded
987 * it into fp->f_data and vref()d the vp, giving us two refs. So
988 * instead of just unlocking it here we have to vput() it.
993 /* Ugly pty magic, to make pty devices appear once they are opened */
994 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY)
995 node->flags &= ~DEVFS_INVISIBLE;
998 KKASSERT(ap->a_fp->f_type == DTYPE_VNODE);
999 KKASSERT((ap->a_fp->f_flag & FMASK) == (ap->a_mode & FMASK));
1000 ap->a_fp->f_ops = &devfs_dev_fileops;
1001 KKASSERT(ap->a_fp->f_data == (void *)vp);
1009 devfs_spec_close(struct vop_close_args *ap)
1011 struct devfs_node *node;
1012 struct proc *p = curproc;
1013 struct vnode *vp = ap->a_vp;
1014 cdev_t dev = vp->v_rdev;
1019 devfs_debug(DEVFS_DEBUG_DEBUG,
1020 "devfs_spec_close() called on %s! \n",
1023 devfs_debug(DEVFS_DEBUG_DEBUG,
1024 "devfs_spec_close() called, null vode!\n");
1027 * A couple of hacks for devices and tty devices. The
1028 * vnode ref count cannot be used to figure out the
1029 * last close, but we can use v_opencount now that
1030 * revoke works properly.
1032 * Detect the last close on a controlling terminal and clear
1033 * the session (half-close).
1038 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
1039 p->p_session->s_ttyvp = NULL;
1044 * Vnodes can be opened and closed multiple times. Do not really
1045 * close the device unless (1) it is being closed forcibly,
1046 * (2) the device wants to track closes, or (3) this is the last
1047 * vnode doing its last close on the device.
1049 * XXX the VXLOCK (force close) case can leave vnodes referencing
1050 * a closed device. This might not occur now that our revoke is
1053 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
1054 if (dev && ((vp->v_flag & VRECLAIMED) ||
1055 (dev_dflags(dev) & D_TRACKCLOSE) ||
1056 (vp->v_opencount == 1))) {
1058 * Ugly pty magic, to make pty devices disappear again once
1061 node = DEVFS_NODE(ap->a_vp);
1062 if (node && (node->flags & DEVFS_PTY))
1063 node->flags |= DEVFS_INVISIBLE;
1066 * Unlock around dev_dclose(), unless the vnode is
1067 * undergoing a vgone/reclaim (during umount).
1070 if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) {
1076 * WARNING! If the device destroys itself the devfs node
1077 * can disappear here.
1079 * WARNING! vn_lock() will fail if the vp is in a VRECLAIM,
1080 * which can occur during umount.
1082 error = dev_dclose(dev, ap->a_fflag, S_IFCHR);
1083 /* node is now stale */
1086 if (vn_lock(vp, LK_EXCLUSIVE | LK_RETRY) != 0) {
1087 panic("devfs_spec_close: vnode %p "
1088 "unexpectedly could not be relocked",
1095 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
1098 * Track the actual opens and closes on the vnode. The last close
1099 * disassociates the rdev. If the rdev is already disassociated or
1100 * the opencount is already 0, the vnode might have been revoked
1101 * and no further opencount tracking occurs.
1105 if (vp->v_opencount > 0)
1113 devfs_fo_close(struct file *fp)
1115 struct vnode *vp = (struct vnode *)fp->f_data;
1118 fp->f_ops = &badfileops;
1119 error = vn_close(vp, fp->f_flag);
1126 * Device-optimized file table vnode read routine.
1128 * This bypasses the VOP table and talks directly to the device. Most
1129 * filesystems just route to specfs and can make this optimization.
1131 * MPALMOSTSAFE - acquires mplock
1134 devfs_fo_read(struct file *fp, struct uio *uio,
1135 struct ucred *cred, int flags)
1137 struct devfs_node *node;
1143 KASSERT(uio->uio_td == curthread,
1144 ("uio_td %p is not td %p", uio->uio_td, curthread));
1146 if (uio->uio_resid == 0)
1149 vp = (struct vnode *)fp->f_data;
1150 if (vp == NULL || vp->v_type == VBAD)
1153 node = DEVFS_NODE(vp);
1155 if ((dev = vp->v_rdev) == NULL)
1160 if ((flags & O_FOFFSET) == 0)
1161 uio->uio_offset = fp->f_offset;
1164 if (flags & O_FBLOCKING) {
1165 /* ioflag &= ~IO_NDELAY; */
1166 } else if (flags & O_FNONBLOCKING) {
1167 ioflag |= IO_NDELAY;
1168 } else if (fp->f_flag & FNONBLOCK) {
1169 ioflag |= IO_NDELAY;
1171 if (flags & O_FBUFFERED) {
1172 /* ioflag &= ~IO_DIRECT; */
1173 } else if (flags & O_FUNBUFFERED) {
1174 ioflag |= IO_DIRECT;
1175 } else if (fp->f_flag & O_DIRECT) {
1176 ioflag |= IO_DIRECT;
1178 ioflag |= sequential_heuristic(uio, fp);
1180 error = dev_dread(dev, uio, ioflag);
1184 nanotime(&node->atime);
1185 if ((flags & O_FOFFSET) == 0)
1186 fp->f_offset = uio->uio_offset;
1187 fp->f_nextoff = uio->uio_offset;
1194 devfs_fo_write(struct file *fp, struct uio *uio,
1195 struct ucred *cred, int flags)
1197 struct devfs_node *node;
1203 KASSERT(uio->uio_td == curthread,
1204 ("uio_td %p is not p %p", uio->uio_td, curthread));
1206 vp = (struct vnode *)fp->f_data;
1207 if (vp == NULL || vp->v_type == VBAD)
1210 node = DEVFS_NODE(vp);
1212 if (vp->v_type == VREG)
1213 bwillwrite(uio->uio_resid);
1215 vp = (struct vnode *)fp->f_data;
1217 if ((dev = vp->v_rdev) == NULL)
1222 if ((flags & O_FOFFSET) == 0)
1223 uio->uio_offset = fp->f_offset;
1226 if (vp->v_type == VREG &&
1227 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1228 ioflag |= IO_APPEND;
1231 if (flags & O_FBLOCKING) {
1232 /* ioflag &= ~IO_NDELAY; */
1233 } else if (flags & O_FNONBLOCKING) {
1234 ioflag |= IO_NDELAY;
1235 } else if (fp->f_flag & FNONBLOCK) {
1236 ioflag |= IO_NDELAY;
1238 if (flags & O_FBUFFERED) {
1239 /* ioflag &= ~IO_DIRECT; */
1240 } else if (flags & O_FUNBUFFERED) {
1241 ioflag |= IO_DIRECT;
1242 } else if (fp->f_flag & O_DIRECT) {
1243 ioflag |= IO_DIRECT;
1245 if (flags & O_FASYNCWRITE) {
1246 /* ioflag &= ~IO_SYNC; */
1247 } else if (flags & O_FSYNCWRITE) {
1249 } else if (fp->f_flag & O_FSYNC) {
1253 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1255 ioflag |= sequential_heuristic(uio, fp);
1257 error = dev_dwrite(dev, uio, ioflag);
1261 nanotime(&node->atime);
1262 nanotime(&node->mtime);
1265 if ((flags & O_FOFFSET) == 0)
1266 fp->f_offset = uio->uio_offset;
1267 fp->f_nextoff = uio->uio_offset;
1274 devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
1283 vp = (struct vnode *)fp->f_data;
1284 if (vp == NULL || vp->v_type == VBAD)
1287 error = vn_stat(vp, sb, cred);
1292 error = VOP_GETATTR(vp, vap);
1297 * Zero the spare stat fields
1304 * Copy from vattr table ... or not in case it's a cloned device
1306 if (vap->va_fsid != VNOVAL)
1307 sb->st_dev = vap->va_fsid;
1309 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1311 sb->st_ino = vap->va_fileid;
1313 mode = vap->va_mode;
1317 if (vap->va_nlink > (nlink_t)-1)
1318 sb->st_nlink = (nlink_t)-1;
1320 sb->st_nlink = vap->va_nlink;
1322 sb->st_uid = vap->va_uid;
1323 sb->st_gid = vap->va_gid;
1324 sb->st_rdev = dev2udev(DEVFS_NODE(vp)->d_dev);
1325 sb->st_size = vap->va_bytes;
1326 sb->st_atimespec = vap->va_atime;
1327 sb->st_mtimespec = vap->va_mtime;
1328 sb->st_ctimespec = vap->va_ctime;
1331 * A VCHR and VBLK device may track the last access and last modified
1332 * time independantly of the filesystem. This is particularly true
1333 * because device read and write calls may bypass the filesystem.
1335 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1338 if (dev->si_lastread) {
1339 sb->st_atimespec.tv_sec = dev->si_lastread;
1340 sb->st_atimespec.tv_nsec = 0;
1342 if (dev->si_lastwrite) {
1343 sb->st_atimespec.tv_sec = dev->si_lastwrite;
1344 sb->st_atimespec.tv_nsec = 0;
1350 * According to www.opengroup.org, the meaning of st_blksize is
1351 * "a filesystem-specific preferred I/O block size for this
1352 * object. In some filesystem types, this may vary from file
1354 * Default to PAGE_SIZE after much discussion.
1357 sb->st_blksize = PAGE_SIZE;
1359 sb->st_flags = vap->va_flags;
1361 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1365 sb->st_gen = (u_int32_t)vap->va_gen;
1367 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1374 devfs_fo_kqfilter(struct file *fp, struct knote *kn)
1380 vp = (struct vnode *)fp->f_data;
1381 if (vp == NULL || vp->v_type == VBAD) {
1385 if ((dev = vp->v_rdev) == NULL) {
1391 error = dev_dkqfilter(dev, kn);
1400 * MPALMOSTSAFE - acquires mplock
1403 devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
1404 struct ucred *ucred, struct sysmsg *msg)
1406 struct devfs_node *node;
1411 struct fiodname_args *name_args;
1415 vp = ((struct vnode *)fp->f_data);
1417 if ((dev = vp->v_rdev) == NULL)
1418 return EBADF; /* device was revoked */
1422 node = DEVFS_NODE(vp);
1424 devfs_debug(DEVFS_DEBUG_DEBUG,
1425 "devfs_fo_ioctl() called! for dev %s\n",
1428 if (com == FIODTYPE) {
1429 *(int *)data = dev_dflags(dev) & D_TYPEMASK;
1432 } else if (com == FIODNAME) {
1433 name_args = (struct fiodname_args *)data;
1434 name = dev->si_name;
1435 namlen = strlen(name) + 1;
1437 devfs_debug(DEVFS_DEBUG_DEBUG,
1438 "ioctl, got: FIODNAME for %s\n", name);
1440 if (namlen <= name_args->len)
1441 error = copyout(dev->si_name, name_args->name, namlen);
1445 devfs_debug(DEVFS_DEBUG_DEBUG,
1446 "ioctl stuff: error: %d\n", error);
1450 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg);
1454 nanotime(&node->atime);
1455 nanotime(&node->mtime);
1458 if (com == TIOCSCTTY) {
1459 devfs_debug(DEVFS_DEBUG_DEBUG,
1460 "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
1463 if (error == 0 && com == TIOCSCTTY) {
1464 struct proc *p = curthread->td_proc;
1465 struct session *sess;
1467 devfs_debug(DEVFS_DEBUG_DEBUG,
1468 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
1474 sess = p->p_session;
1477 * Do nothing if reassigning same control tty
1479 if (sess->s_ttyvp == vp) {
1485 * Get rid of reference to old control tty
1487 ovp = sess->s_ttyvp;
1496 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
1502 devfs_spec_fsync(struct vop_fsync_args *ap)
1504 struct vnode *vp = ap->a_vp;
1507 if (!vn_isdisk(vp, NULL))
1511 * Flush all dirty buffers associated with a block device.
1513 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1518 devfs_spec_read(struct vop_read_args *ap)
1520 struct devfs_node *node;
1529 node = DEVFS_NODE(vp);
1531 if (dev == NULL) /* device was revoked */
1533 if (uio->uio_resid == 0)
1537 error = dev_dread(dev, uio, ap->a_ioflag);
1538 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1541 nanotime(&node->atime);
1547 * Vnode op for write
1549 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1550 * struct ucred *a_cred)
1553 devfs_spec_write(struct vop_write_args *ap)
1555 struct devfs_node *node;
1564 node = DEVFS_NODE(vp);
1566 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1568 if (dev == NULL) /* device was revoked */
1572 error = dev_dwrite(dev, uio, ap->a_ioflag);
1573 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1576 nanotime(&node->atime);
1577 nanotime(&node->mtime);
1584 * Device ioctl operation.
1586 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
1587 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
1590 devfs_spec_ioctl(struct vop_ioctl_args *ap)
1592 struct vnode *vp = ap->a_vp;
1593 struct devfs_node *node;
1596 if ((dev = vp->v_rdev) == NULL)
1597 return (EBADF); /* device was revoked */
1598 node = DEVFS_NODE(vp);
1602 nanotime(&node->atime);
1603 nanotime(&node->mtime);
1607 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
1608 ap->a_cred, ap->a_sysmsg));
1612 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1616 devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1618 struct vnode *vp = ap->a_vp;
1619 struct devfs_node *node;
1622 if ((dev = vp->v_rdev) == NULL)
1623 return (EBADF); /* device was revoked (EBADF) */
1624 node = DEVFS_NODE(vp);
1628 nanotime(&node->atime);
1631 return (dev_dkqfilter(dev, ap->a_kn));
1635 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1636 * calls are not limited to device DMA limits so we have to deal with the
1639 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1642 devfs_spec_strategy(struct vop_strategy_args *ap)
1644 struct bio *bio = ap->a_bio;
1645 struct buf *bp = bio->bio_buf;
1652 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1656 * Collect statistics on synchronous and asynchronous read
1657 * and write counts for disks that have associated filesystems.
1660 KKASSERT(vp->v_rdev != NULL); /* XXX */
1661 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1662 if (bp->b_cmd == BUF_CMD_READ) {
1663 if (bp->b_flags & BIO_SYNC)
1664 mp->mnt_stat.f_syncreads++;
1666 mp->mnt_stat.f_asyncreads++;
1668 if (bp->b_flags & BIO_SYNC)
1669 mp->mnt_stat.f_syncwrites++;
1671 mp->mnt_stat.f_asyncwrites++;
1676 * Device iosize limitations only apply to read and write. Shortcut
1677 * the I/O if it fits.
1679 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1680 devfs_debug(DEVFS_DEBUG_DEBUG,
1681 "%s: si_iosize_max not set!\n",
1682 dev_dname(vp->v_rdev));
1683 maxiosize = MAXPHYS;
1685 #if SPEC_CHAIN_DEBUG & 2
1688 if (bp->b_bcount <= maxiosize ||
1689 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1690 dev_dstrategy_chain(vp->v_rdev, bio);
1695 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1697 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1700 BUF_LOCK(nbp, LK_EXCLUSIVE);
1703 nbp->b_flags = B_PAGING | (bp->b_flags & B_BNOCLIP);
1704 nbp->b_data = bp->b_data;
1705 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1706 nbp->b_bio1.bio_offset = bio->bio_offset;
1707 nbp->b_bio1.bio_caller_info1.ptr = bio;
1710 * Start the first transfer
1712 if (vn_isdisk(vp, NULL))
1713 chunksize = vp->v_rdev->si_bsize_phys;
1715 chunksize = DEV_BSIZE;
1716 chunksize = maxiosize / chunksize * chunksize;
1717 #if SPEC_CHAIN_DEBUG & 1
1718 devfs_debug(DEVFS_DEBUG_DEBUG,
1719 "spec_strategy chained I/O chunksize=%d\n",
1722 nbp->b_cmd = bp->b_cmd;
1723 nbp->b_bcount = chunksize;
1724 nbp->b_bufsize = chunksize; /* used to detect a short I/O */
1725 nbp->b_bio1.bio_caller_info2.index = chunksize;
1727 #if SPEC_CHAIN_DEBUG & 1
1728 devfs_debug(DEVFS_DEBUG_DEBUG,
1729 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1730 bp, 0, bp->b_bcount, nbp->b_bcount);
1733 dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1735 if (DEVFS_NODE(vp)) {
1736 nanotime(&DEVFS_NODE(vp)->atime);
1737 nanotime(&DEVFS_NODE(vp)->mtime);
1744 * Chunked up transfer completion routine - chain transfers until done
1746 * NOTE: MPSAFE callback.
1750 devfs_spec_strategy_done(struct bio *nbio)
1752 struct buf *nbp = nbio->bio_buf;
1753 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
1754 struct buf *bp = bio->bio_buf; /* original bp */
1755 int chunksize = nbio->bio_caller_info2.index; /* chunking */
1756 int boffset = nbp->b_data - bp->b_data;
1758 if (nbp->b_flags & B_ERROR) {
1760 * An error terminates the chain, propogate the error back
1761 * to the original bp
1763 bp->b_flags |= B_ERROR;
1764 bp->b_error = nbp->b_error;
1765 bp->b_resid = bp->b_bcount - boffset +
1766 (nbp->b_bcount - nbp->b_resid);
1767 #if SPEC_CHAIN_DEBUG & 1
1768 devfs_debug(DEVFS_DEBUG_DEBUG,
1769 "spec_strategy: chain %p error %d bcount %d/%d\n",
1770 bp, bp->b_error, bp->b_bcount,
1771 bp->b_bcount - bp->b_resid);
1773 } else if (nbp->b_resid) {
1775 * A short read or write terminates the chain
1777 bp->b_error = nbp->b_error;
1778 bp->b_resid = bp->b_bcount - boffset +
1779 (nbp->b_bcount - nbp->b_resid);
1780 #if SPEC_CHAIN_DEBUG & 1
1781 devfs_debug(DEVFS_DEBUG_DEBUG,
1782 "spec_strategy: chain %p short read(1) "
1784 bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
1786 } else if (nbp->b_bcount != nbp->b_bufsize) {
1788 * A short read or write can also occur by truncating b_bcount
1790 #if SPEC_CHAIN_DEBUG & 1
1791 devfs_debug(DEVFS_DEBUG_DEBUG,
1792 "spec_strategy: chain %p short read(2) "
1794 bp, nbp->b_bcount + boffset, bp->b_bcount);
1797 bp->b_bcount = nbp->b_bcount + boffset;
1798 bp->b_resid = nbp->b_resid;
1799 } else if (nbp->b_bcount + boffset == bp->b_bcount) {
1801 * No more data terminates the chain
1803 #if SPEC_CHAIN_DEBUG & 1
1804 devfs_debug(DEVFS_DEBUG_DEBUG,
1805 "spec_strategy: chain %p finished bcount %d\n",
1812 * Continue the chain
1814 boffset += nbp->b_bcount;
1815 nbp->b_data = bp->b_data + boffset;
1816 nbp->b_bcount = bp->b_bcount - boffset;
1817 if (nbp->b_bcount > chunksize)
1818 nbp->b_bcount = chunksize;
1819 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1820 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1822 #if SPEC_CHAIN_DEBUG & 1
1823 devfs_debug(DEVFS_DEBUG_DEBUG,
1824 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1825 bp, boffset, bp->b_bcount, nbp->b_bcount);
1828 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
1833 * Fall through to here on termination. biodone(bp) and
1834 * clean up and free nbp.
1839 kfree(nbp, M_DEVBUF);
1843 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1846 devfs_spec_freeblks(struct vop_freeblks_args *ap)
1851 * XXX: This assumes that strategy does the deed right away.
1852 * XXX: this may not be TRTTD.
1854 KKASSERT(ap->a_vp->v_rdev != NULL);
1855 if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0)
1857 bp = geteblk(ap->a_length);
1858 bp->b_cmd = BUF_CMD_FREEBLKS;
1859 bp->b_bio1.bio_offset = ap->a_offset;
1860 bp->b_bcount = ap->a_length;
1861 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1866 * Implement degenerate case where the block requested is the block
1867 * returned, and assume that the entire device is contiguous in regards
1868 * to the contiguous block range (runp and runb).
1870 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1871 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1874 devfs_spec_bmap(struct vop_bmap_args *ap)
1876 if (ap->a_doffsetp != NULL)
1877 *ap->a_doffsetp = ap->a_loffset;
1878 if (ap->a_runp != NULL)
1879 *ap->a_runp = MAXBSIZE;
1880 if (ap->a_runb != NULL) {
1881 if (ap->a_loffset < MAXBSIZE)
1882 *ap->a_runb = (int)ap->a_loffset;
1884 *ap->a_runb = MAXBSIZE;
1891 * Special device advisory byte-level locks.
1893 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1894 * struct flock *a_fl, int a_flags)
1898 devfs_spec_advlock(struct vop_advlock_args *ap)
1900 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
1904 * NOTE: MPSAFE callback.
1907 devfs_spec_getpages_iodone(struct bio *bio)
1909 bio->bio_buf->b_cmd = BUF_CMD_DONE;
1910 wakeup(bio->bio_buf);
1914 * spec_getpages() - get pages associated with device vnode.
1916 * Note that spec_read and spec_write do not use the buffer cache, so we
1917 * must fully implement getpages here.
1920 devfs_spec_getpages(struct vop_getpages_args *ap)
1924 int i, pcount, size;
1927 vm_ooffset_t offset;
1928 int toff, nextoff, nread;
1929 struct vnode *vp = ap->a_vp;
1934 pcount = round_page(ap->a_count) / PAGE_SIZE;
1937 * Calculate the offset of the transfer and do sanity check.
1939 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
1942 * Round up physical size for real devices. We cannot round using
1943 * v_mount's block size data because v_mount has nothing to do with
1944 * the device. i.e. it's usually '/dev'. We need the physical block
1945 * size for the device itself.
1947 * We can't use v_rdev->si_mountpoint because it only exists when the
1948 * block device is mounted. However, we can use v_rdev.
1950 if (vn_isdisk(vp, NULL))
1951 blksiz = vp->v_rdev->si_bsize_phys;
1955 size = (ap->a_count + blksiz - 1) & ~(blksiz - 1);
1957 bp = getpbuf_kva(NULL);
1958 kva = (vm_offset_t)bp->b_data;
1961 * Map the pages to be read into the kva.
1963 pmap_qenter(kva, ap->a_m, pcount);
1965 /* Build a minimal buffer header. */
1966 bp->b_cmd = BUF_CMD_READ;
1967 bp->b_bcount = size;
1969 bsetrunningbufspace(bp, size);
1971 bp->b_bio1.bio_offset = offset;
1972 bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
1974 mycpu->gd_cnt.v_vnodein++;
1975 mycpu->gd_cnt.v_vnodepgsin += pcount;
1978 vn_strategy(ap->a_vp, &bp->b_bio1);
1982 /* We definitely need to be at splbio here. */
1983 while (bp->b_cmd != BUF_CMD_DONE)
1984 tsleep(bp, 0, "spread", 0);
1988 if (bp->b_flags & B_ERROR) {
1990 error = bp->b_error;
1996 * If EOF is encountered we must zero-extend the result in order
1997 * to ensure that the page does not contain garabge. When no
1998 * error occurs, an early EOF is indicated if b_bcount got truncated.
1999 * b_resid is relative to b_bcount and should be 0, but some devices
2000 * might indicate an EOF with b_resid instead of truncating b_bcount.
2002 nread = bp->b_bcount - bp->b_resid;
2003 if (nread < ap->a_count)
2004 bzero((caddr_t)kva + nread, ap->a_count - nread);
2005 pmap_qremove(kva, pcount);
2008 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
2009 nextoff = toff + PAGE_SIZE;
2012 m->flags &= ~PG_ZERO;
2015 * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2016 * pmap modified bit. pmap modified bit should have
2017 * already been cleared.
2019 if (nextoff <= nread) {
2020 m->valid = VM_PAGE_BITS_ALL;
2022 } else if (toff < nread) {
2024 * Since this is a VM request, we have to supply the
2025 * unaligned offset to allow vm_page_set_valid()
2026 * to zero sub-DEV_BSIZE'd portions of the page.
2028 vm_page_set_valid(m, 0, nread - toff);
2029 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
2035 if (i != ap->a_reqpage) {
2037 * Just in case someone was asking for this page we
2038 * now tell them that it is ok to use.
2040 if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
2042 if (m->flags & PG_REFERENCED) {
2043 vm_page_activate(m);
2045 vm_page_deactivate(m);
2054 } else if (m->valid) {
2057 * Since this is a VM request, we need to make the
2058 * entire page presentable by zeroing invalid sections.
2060 if (m->valid != VM_PAGE_BITS_ALL)
2061 vm_page_zero_invalid(m, FALSE);
2065 m = ap->a_m[ap->a_reqpage];
2066 devfs_debug(DEVFS_DEBUG_WARNING,
2067 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
2068 devtoname(vp->v_rdev), error, bp, bp->b_vp);
2069 devfs_debug(DEVFS_DEBUG_WARNING,
2070 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
2071 size, bp->b_resid, ap->a_count, m->valid);
2072 devfs_debug(DEVFS_DEBUG_WARNING,
2073 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
2074 nread, ap->a_reqpage, (u_long)m->pindex, pcount);
2076 * Free the buffer header back to the swap buffer pool.
2079 return VM_PAGER_ERROR;
2082 * Free the buffer header back to the swap buffer pool.
2085 if (DEVFS_NODE(ap->a_vp))
2086 nanotime(&DEVFS_NODE(ap->a_vp)->mtime);
2092 sequential_heuristic(struct uio *uio, struct file *fp)
2095 * Sequential heuristic - detect sequential operation
2097 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2098 uio->uio_offset == fp->f_nextoff) {
2100 * XXX we assume that the filesystem block size is
2101 * the default. Not true, but still gives us a pretty
2102 * good indicator of how sequential the read operations
2105 int tmpseq = fp->f_seqcount;
2107 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
2108 if (tmpseq > IO_SEQMAX)
2110 fp->f_seqcount = tmpseq;
2111 return(fp->f_seqcount << IO_SEQSHIFT);
2115 * Not sequential, quick draw-down of seqcount
2117 if (fp->f_seqcount > 1)