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/dirent.h>
50 #include <sys/malloc.h>
53 #include <vm/vm_pager.h>
54 #include <vm/vm_zone.h>
55 #include <vm/vm_object.h>
56 #include <sys/filio.h>
57 #include <sys/ttycom.h>
59 #include <sys/diskslice.h>
60 #include <sys/sysctl.h>
61 #include <sys/devfs.h>
62 #include <sys/pioctl.h>
63 #include <vfs/fifofs/fifo.h>
65 #include <machine/limits.h>
68 #include <vm/vm_page2.h>
70 #ifndef SPEC_CHAIN_DEBUG
71 #define SPEC_CHAIN_DEBUG 0
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_reallocblks = DEVFS_BADOP,
149 .vop_reclaim = devfs_vop_reclaim,
150 .vop_setattr = devfs_vop_setattr,
151 .vop_write = DEVFS_BADOP,
152 .vop_ioctl = DEVFS_BADOP
156 * devfs vnode operations for character devices. All vnode ops are MPSAFE.
158 struct vop_ops devfs_vnode_dev_vops = {
159 .vop_default = vop_defaultop,
160 .vop_access = devfs_vop_access,
161 .vop_advlock = devfs_spec_advlock,
162 .vop_bmap = devfs_spec_bmap,
163 .vop_close = devfs_spec_close,
164 .vop_freeblks = devfs_spec_freeblks,
165 .vop_fsync = devfs_spec_fsync,
166 .vop_getattr = devfs_vop_getattr,
167 .vop_getpages = devfs_spec_getpages,
168 .vop_inactive = devfs_vop_inactive,
169 .vop_open = devfs_spec_open,
170 .vop_pathconf = vop_stdpathconf,
171 .vop_print = devfs_vop_print,
172 .vop_kqfilter = devfs_spec_kqfilter,
173 .vop_read = devfs_spec_read,
174 .vop_readdir = DEVFS_BADOP,
175 .vop_readlink = DEVFS_BADOP,
176 .vop_reallocblks = DEVFS_BADOP,
177 .vop_reclaim = devfs_vop_reclaim,
178 .vop_setattr = devfs_vop_setattr,
179 .vop_strategy = devfs_spec_strategy,
180 .vop_write = devfs_spec_write,
181 .vop_ioctl = devfs_spec_ioctl
185 * devfs file pointer operations. All fileops are MPSAFE.
187 struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops;
189 struct fileops devfs_dev_fileops = {
190 .fo_read = devfs_fo_read,
191 .fo_write = devfs_fo_write,
192 .fo_ioctl = devfs_fo_ioctl,
193 .fo_kqfilter = devfs_fo_kqfilter,
194 .fo_stat = devfs_fo_stat,
195 .fo_close = devfs_fo_close,
196 .fo_shutdown = nofo_shutdown
200 * These two functions are possibly temporary hacks for devices (aka
201 * the pty code) which want to control the node attributes themselves.
203 * XXX we may ultimately desire to simply remove the uid/gid/mode
204 * from the node entirely.
206 * MPSAFE - sorta. Theoretically the overwrite can compete since they
207 * are loading from the same fields.
210 node_sync_dev_get(struct devfs_node *node)
214 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
215 node->uid = dev->si_uid;
216 node->gid = dev->si_gid;
217 node->mode = dev->si_perms;
222 node_sync_dev_set(struct devfs_node *node)
226 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
227 dev->si_uid = node->uid;
228 dev->si_gid = node->gid;
229 dev->si_perms = node->mode;
234 * generic entry point for unsupported operations
237 devfs_vop_badop(struct vop_generic_args *ap)
244 devfs_vop_access(struct vop_access_args *ap)
246 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
249 if (!devfs_node_is_accessible(node))
251 node_sync_dev_get(node);
252 error = vop_helper_access(ap, node->uid, node->gid,
253 node->mode, node->flags);
260 devfs_vop_inactive(struct vop_inactive_args *ap)
262 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
264 if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0)
271 devfs_vop_reclaim(struct vop_reclaim_args *ap)
273 struct devfs_node *node;
278 * Check if it is locked already. if not, we acquire the devfs lock
280 if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) {
281 lockmgr(&devfs_lock, LK_EXCLUSIVE);
288 * Get rid of the devfs_node if it is no longer linked into the
289 * topology. Interlocked by devfs_lock. However, be careful
290 * interposing other operations between cleaning out v_data and
291 * devfs_freep() as the node is only protected by devfs_lock
292 * once the vnode is disassociated.
295 node = DEVFS_NODE(vp);
298 if (node->v_node != vp) {
299 kprintf("NODE->V_NODE MISMATCH VP=%p NODEVP=%p\n",
304 if ((node->flags & DEVFS_NODE_LINKED) == 0)
310 lockmgr(&devfs_lock, LK_RELEASE);
313 * v_rdev needs to be properly released using v_release_rdev
314 * Make sure v_data is NULL as well.
321 devfs_vop_readdir(struct vop_readdir_args *ap)
323 struct devfs_node *dnode = DEVFS_NODE(ap->a_vp);
324 struct devfs_node *node;
333 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
335 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
337 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
341 if (!devfs_node_is_accessible(dnode)) {
346 lockmgr(&devfs_lock, LK_EXCLUSIVE);
348 saveoff = ap->a_uio->uio_offset;
350 if (ap->a_ncookies) {
351 ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
354 cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
362 vfs_timestamp(&dnode->atime);
365 r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino,
370 cookies[cookie_index] = saveoff;
373 if (cookie_index == ncookies)
379 r = vop_write_dirent(&error, ap->a_uio,
380 dnode->parent->d_dir.d_ino,
383 r = vop_write_dirent(&error, ap->a_uio,
390 cookies[cookie_index] = saveoff;
393 if (cookie_index == ncookies)
397 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
398 if ((node->flags & DEVFS_HIDDEN) ||
399 (node->flags & DEVFS_INVISIBLE)) {
404 * If the node type is a valid devfs alias, then we make
405 * sure that the target isn't hidden. If it is, we don't
406 * show the link in the directory listing.
408 if ((node->node_type == Nlink) && (node->link_target != NULL) &&
409 (node->link_target->flags & DEVFS_HIDDEN))
412 if (node->cookie < saveoff)
415 saveoff = node->cookie;
417 error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino,
419 node->d_dir.d_namlen,
428 cookies[cookie_index] = node->cookie;
430 if (cookie_index == ncookies)
435 lockmgr(&devfs_lock, LK_RELEASE);
438 ap->a_uio->uio_offset = saveoff;
439 if (error && cookie_index == 0) {
441 kfree(cookies, M_TEMP);
443 *ap->a_cookies = NULL;
447 *ap->a_ncookies = cookie_index;
448 *ap->a_cookies = cookies;
456 devfs_vop_nresolve(struct vop_nresolve_args *ap)
458 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
459 struct devfs_node *node, *found = NULL;
460 struct namecache *ncp;
461 struct vnode *vp = NULL;
466 ncp = ap->a_nch->ncp;
469 if (!devfs_node_is_accessible(dnode))
472 lockmgr(&devfs_lock, LK_EXCLUSIVE);
474 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) {
476 cache_setvp(ap->a_nch, NULL);
480 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
481 if (len == node->d_dir.d_namlen) {
482 if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
491 while ((found->node_type == Nlink) && (found->link_target)) {
493 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
497 found = found->link_target;
501 if (!(found->flags & DEVFS_HIDDEN))
502 devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
507 cache_setvp(ap->a_nch, NULL);
513 cache_setvp(ap->a_nch, vp);
516 lockmgr(&devfs_lock, LK_RELEASE);
523 devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
525 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
528 if (!devfs_node_is_accessible(dnode))
531 lockmgr(&devfs_lock, LK_EXCLUSIVE);
532 if (dnode->parent != NULL) {
533 devfs_allocv(ap->a_vpp, dnode->parent);
534 vn_unlock(*ap->a_vpp);
536 lockmgr(&devfs_lock, LK_RELEASE);
538 return ((*ap->a_vpp == NULL) ? ENOENT : 0);
543 * getattr() - Does not need a lock since the vp is refd
546 devfs_vop_getattr(struct vop_getattr_args *ap)
548 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
549 struct vattr *vap = ap->a_vap;
550 struct partinfo pinfo;
554 if (!devfs_node_is_accessible(node))
559 * XXX This is a temporary hack to prevent crashes when the device is
560 * being destroyed (and so the underlying node will be gone) while
561 * a userland program is blocked in a read().
566 node_sync_dev_get(node);
568 /* start by zeroing out the attributes */
571 /* next do all the common fields */
572 vap->va_type = ap->a_vp->v_type;
573 vap->va_mode = node->mode;
574 vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
576 vap->va_blocksize = DEV_BSIZE;
577 vap->va_bytes = vap->va_size = 0;
579 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
581 vap->va_atime = node->atime;
582 vap->va_mtime = node->mtime;
583 vap->va_ctime = node->ctime;
585 vap->va_nlink = 1; /* number of references to file */
587 vap->va_uid = node->uid;
588 vap->va_gid = node->gid;
593 if ((node->node_type == Ndev) && node->d_dev) {
594 reference_dev(node->d_dev);
595 vap->va_rminor = node->d_dev->si_uminor;
596 release_dev(node->d_dev);
599 /* For a softlink the va_size is the length of the softlink */
600 if (node->symlink_name != 0) {
601 vap->va_bytes = vap->va_size = node->symlink_namelen;
605 * For a disk-type device, va_size is the size of the underlying
606 * device, so that lseek() works properly.
608 if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) {
609 bzero(&pinfo, sizeof(pinfo));
610 error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo,
611 0, proc0.p_ucred, NULL, NULL);
612 if ((error == 0) && (pinfo.media_blksize != 0)) {
613 vap->va_size = pinfo.media_size;
624 devfs_vop_setattr(struct vop_setattr_args *ap)
626 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
633 if (!devfs_node_is_accessible(node))
635 node_sync_dev_get(node);
639 if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) {
642 cur_mode = node->mode;
643 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
644 ap->a_cred, &cur_uid, &cur_gid, &cur_mode);
648 if (node->uid != cur_uid || node->gid != cur_gid) {
651 node->mode = cur_mode;
655 if (vap->va_mode != (mode_t)VNOVAL) {
656 cur_mode = node->mode;
657 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
658 node->uid, node->gid, &cur_mode);
659 if (error == 0 && node->mode != cur_mode) {
660 node->mode = cur_mode;
665 node_sync_dev_set(node);
666 vfs_timestamp(&node->ctime);
673 devfs_vop_readlink(struct vop_readlink_args *ap)
675 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
678 if (!devfs_node_is_accessible(node))
681 lockmgr(&devfs_lock, LK_SHARED);
682 ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
683 lockmgr(&devfs_lock, LK_RELEASE);
690 devfs_vop_print(struct vop_print_args *ap)
696 devfs_vop_nmkdir(struct vop_nmkdir_args *ap)
698 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
699 struct devfs_node *node;
701 if (!devfs_node_is_accessible(dnode))
704 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
707 lockmgr(&devfs_lock, LK_EXCLUSIVE);
708 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Ndir,
709 ap->a_nch->ncp->nc_name, dnode, NULL);
712 node = DEVFS_NODE(*ap->a_vpp);
713 node->flags |= DEVFS_USER_CREATED;
714 cache_setunresolved(ap->a_nch);
715 cache_setvp(ap->a_nch, *ap->a_vpp);
717 lockmgr(&devfs_lock, LK_RELEASE);
719 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
723 devfs_vop_nsymlink(struct vop_nsymlink_args *ap)
725 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
726 struct devfs_node *node;
729 if (!devfs_node_is_accessible(dnode))
732 ap->a_vap->va_type = VLNK;
734 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
737 lockmgr(&devfs_lock, LK_EXCLUSIVE);
738 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Nlink,
739 ap->a_nch->ncp->nc_name, dnode, NULL);
741 targetlen = strlen(ap->a_target);
743 node = DEVFS_NODE(*ap->a_vpp);
744 node->flags |= DEVFS_USER_CREATED;
745 node->symlink_namelen = targetlen;
746 node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
747 memcpy(node->symlink_name, ap->a_target, targetlen);
748 node->symlink_name[targetlen] = '\0';
749 cache_setunresolved(ap->a_nch);
750 cache_setvp(ap->a_nch, *ap->a_vpp);
752 lockmgr(&devfs_lock, LK_RELEASE);
754 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
758 devfs_vop_nrmdir(struct vop_nrmdir_args *ap)
760 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
761 struct devfs_node *node;
762 struct namecache *ncp;
765 ncp = ap->a_nch->ncp;
767 if (!devfs_node_is_accessible(dnode))
770 lockmgr(&devfs_lock, LK_EXCLUSIVE);
772 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
775 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
776 if (ncp->nc_nlen != node->d_dir.d_namlen)
778 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
782 * only allow removal of user created dirs
784 if ((node->flags & DEVFS_USER_CREATED) == 0) {
787 } else if (node->node_type != Ndir) {
790 } else if (node->nchildren > 2) {
795 cache_inval_vp(node->v_node, CINV_DESTROY);
802 cache_unlink(ap->a_nch);
804 lockmgr(&devfs_lock, LK_RELEASE);
809 devfs_vop_nremove(struct vop_nremove_args *ap)
811 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
812 struct devfs_node *node;
813 struct namecache *ncp;
816 ncp = ap->a_nch->ncp;
818 if (!devfs_node_is_accessible(dnode))
821 lockmgr(&devfs_lock, LK_EXCLUSIVE);
823 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
826 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
827 if (ncp->nc_nlen != node->d_dir.d_namlen)
829 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
833 * only allow removal of user created stuff (e.g. symlinks)
835 if ((node->flags & DEVFS_USER_CREATED) == 0) {
838 } else if (node->node_type == Ndir) {
843 cache_inval_vp(node->v_node, CINV_DESTROY);
850 cache_unlink(ap->a_nch);
852 lockmgr(&devfs_lock, LK_RELEASE);
858 devfs_spec_open(struct vop_open_args *ap)
860 struct vnode *vp = ap->a_vp;
861 struct vnode *orig_vp = NULL;
862 struct devfs_node *node = DEVFS_NODE(vp);
863 struct devfs_node *newnode;
864 cdev_t dev, ndev = NULL;
868 if (node->d_dev == NULL)
870 if (!devfs_node_is_accessible(node))
874 if ((dev = vp->v_rdev) == NULL)
878 * Simple devices that don't care. Retain the shared lock.
880 if (dev_dflags(dev) & D_QUICK) {
882 error = dev_dopen(dev, ap->a_mode, S_IFCHR,
883 ap->a_cred, ap->a_fpp, vp);
884 vn_lock(vp, LK_SHARED | LK_RETRY);
894 vn_lock(vp, LK_UPGRADE | LK_RETRY);
895 if (node && ap->a_fpp) {
898 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
899 lockmgr(&devfs_lock, LK_SHARED);
901 ndev = devfs_clone(dev, node->d_dir.d_name,
902 node->d_dir.d_namlen,
903 ap->a_mode, ap->a_cred);
905 lockmgr(&devfs_lock, LK_RELEASE);
906 lockmgr(&devfs_lock, LK_EXCLUSIVE);
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 */
912 if (newnode != NULL) {
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,
924 ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
927 * orig_vp is set to the original vp if we
930 /* node->flags |= DEVFS_CLONED; */
931 devfs_allocv(&vp, newnode);
936 lockmgr(&devfs_lock, LK_RELEASE);
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.
948 devfs_debug(DEVFS_DEBUG_DEBUG,
949 "devfs_spec_open() called on %s! \n",
953 * Make this field valid before any I/O in ->d_open
955 * NOTE: Shared vnode lock probably held, but its ok as long
956 * as assignments are consistent.
958 if (!dev->si_iosize_max)
959 /* XXX: old DFLTPHYS == 64KB dependency */
960 dev->si_iosize_max = min(MAXPHYS,64*1024);
962 if (dev_dflags(dev) & D_TTY)
963 vsetflags(vp, VISTTY);
966 * Open the underlying device.
968 * NOTE: If the dev open returns EALREADY it has completed the open
969 * operation and is returning a fully initialized *a->a_fpp
970 * (which it may also have replaced). This includes issuing
971 * any necessary VOP_OPEN().
973 * Also, the returned ap->a_fpp might not be DTYPE_VNODE and
974 * if it is might not be using the vp we supplied to it.
977 error = dev_dopen(dev, ap->a_mode, S_IFCHR,
978 ap->a_cred, ap->a_fpp, vp);
979 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
981 if (__predict_false(error == EALREADY)) {
988 * Clean up any cloned vp if we error out.
990 if (__predict_false(error != 0)) {
994 /* orig_vp = NULL; */
1000 * This checks if the disk device is going to be opened for writing.
1001 * It will be only allowed in the cases where securelevel permits it
1002 * and it's not mounted R/W.
1004 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
1005 (ap->a_cred != FSCRED)) {
1007 /* Very secure mode. No open for writing allowed */
1008 if (securelevel >= 2)
1012 * If it is mounted R/W, do not allow to open for writing.
1013 * In the case it's mounted read-only but securelevel
1014 * is >= 1, then do not allow opening for writing either.
1016 if (vfs_mountedon(vp)) {
1017 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
1019 else if (securelevel >= 1)
1025 * NOTE: vnode is still locked shared. t_stop assignment should
1026 * remain consistent so we should be ok.
1028 if (dev_dflags(dev) & D_TTY) {
1033 devfs_debug(DEVFS_DEBUG_DEBUG,
1034 "devfs: no t_stop\n");
1035 tp->t_stop = nottystop;
1041 * NOTE: vnode is still locked shared. assignments should
1042 * remain consistent so we should be ok. However,
1043 * upgrade to exclusive if we need a VM object.
1045 if (vn_isdisk(vp, NULL)) {
1046 if (!dev->si_bsize_phys)
1047 dev->si_bsize_phys = DEV_BSIZE;
1048 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
1054 vfs_timestamp(&node->atime);
1057 * If we replaced the vp the vop_stdopen() call will have loaded
1058 * it into fp->f_data and vref()d the vp, giving us two refs. So
1059 * instead of just unlocking it here we have to vput() it.
1064 /* Ugly pty magic, to make pty devices appear once they are opened */
1065 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY) {
1066 if (node->flags & DEVFS_INVISIBLE)
1067 node->flags &= ~DEVFS_INVISIBLE;
1072 struct file *fp = *ap->a_fpp;
1074 KKASSERT(fp->f_type == DTYPE_VNODE);
1075 KKASSERT((fp->f_flag & FMASK) == (ap->a_mode & FMASK));
1076 fp->f_ops = &devfs_dev_fileops;
1077 KKASSERT(fp->f_data == (void *)vp);
1084 devfs_spec_close(struct vop_close_args *ap)
1086 struct devfs_node *node;
1087 struct proc *p = curproc;
1088 struct vnode *vp = ap->a_vp;
1089 cdev_t dev = vp->v_rdev;
1095 * Devices flagged D_QUICK require no special handling.
1097 if (dev && dev_dflags(dev) & D_QUICK) {
1098 opencount = vp->v_opencount;
1100 opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1101 if (((vp->v_flag & VRECLAIMED) ||
1102 (dev_dflags(dev) & D_TRACKCLOSE) ||
1103 (opencount == 1))) {
1105 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1106 vn_lock(vp, LK_SHARED | LK_RETRY);
1112 * We do special tests on the opencount so unfortunately we need
1113 * an exclusive lock.
1115 vn_lock(vp, LK_UPGRADE | LK_RETRY);
1118 devfs_debug(DEVFS_DEBUG_DEBUG,
1119 "devfs_spec_close() called on %s! \n",
1122 devfs_debug(DEVFS_DEBUG_DEBUG,
1123 "devfs_spec_close() called, null vode!\n");
1126 * A couple of hacks for devices and tty devices. The
1127 * vnode ref count cannot be used to figure out the
1128 * last close, but we can use v_opencount now that
1129 * revoke works properly.
1131 * Detect the last close on a controlling terminal and clear
1132 * the session (half-close).
1134 * XXX opencount is not SMP safe. The vnode is locked but there
1135 * may be multiple vnodes referencing the same device.
1139 * NOTE: Try to avoid global tokens when testing opencount
1140 * XXX hack, fixme. needs a struct lock and opencount in
1141 * struct cdev itself.
1144 opencount = vp->v_opencount;
1146 opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1151 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
1152 p->p_session->s_ttyvp = NULL;
1157 * Vnodes can be opened and closed multiple times. Do not really
1158 * close the device unless (1) it is being closed forcibly,
1159 * (2) the device wants to track closes, or (3) this is the last
1160 * vnode doing its last close on the device.
1162 * XXX the VXLOCK (force close) case can leave vnodes referencing
1163 * a closed device. This might not occur now that our revoke is
1166 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
1167 if (dev && ((vp->v_flag & VRECLAIMED) ||
1168 (dev_dflags(dev) & D_TRACKCLOSE) ||
1169 (opencount == 1))) {
1171 * Ugly pty magic, to make pty devices disappear again once
1174 node = DEVFS_NODE(ap->a_vp);
1175 if (node && (node->flags & DEVFS_PTY))
1176 node->flags |= DEVFS_INVISIBLE;
1179 * Unlock around dev_dclose(), unless the vnode is
1180 * undergoing a vgone/reclaim (during umount).
1183 if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) {
1189 * WARNING! If the device destroys itself the devfs node
1190 * can disappear here.
1192 * WARNING! vn_lock() will fail if the vp is in a VRECLAIM,
1193 * which can occur during umount.
1195 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1196 /* node is now stale */
1199 if (vn_lock(vp, LK_EXCLUSIVE |
1201 LK_FAILRECLAIM) != 0) {
1202 panic("devfs_spec_close: vnode %p "
1203 "unexpectedly could not be relocked",
1210 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
1213 * Track the actual opens and closes on the vnode. The last close
1214 * disassociates the rdev. If the rdev is already disassociated or
1215 * the opencount is already 0, the vnode might have been revoked
1216 * and no further opencount tracking occurs.
1221 if (vp->v_opencount > 0)
1229 devfs_fo_close(struct file *fp)
1231 struct vnode *vp = (struct vnode *)fp->f_data;
1234 fp->f_ops = &badfileops;
1235 error = vn_close(vp, fp->f_flag, fp);
1236 devfs_clear_cdevpriv(fp);
1243 * Device-optimized file table vnode read routine.
1245 * This bypasses the VOP table and talks directly to the device. Most
1246 * filesystems just route to specfs and can make this optimization.
1249 devfs_fo_read(struct file *fp, struct uio *uio,
1250 struct ucred *cred, int flags)
1252 struct devfs_node *node;
1258 KASSERT(uio->uio_td == curthread,
1259 ("uio_td %p is not td %p", uio->uio_td, curthread));
1261 if (uio->uio_resid == 0)
1264 vp = (struct vnode *)fp->f_data;
1265 if (vp == NULL || vp->v_type == VBAD)
1268 node = DEVFS_NODE(vp);
1270 if ((dev = vp->v_rdev) == NULL)
1275 if ((flags & O_FOFFSET) == 0)
1276 uio->uio_offset = fp->f_offset;
1279 if (flags & O_FBLOCKING) {
1280 /* ioflag &= ~IO_NDELAY; */
1281 } else if (flags & O_FNONBLOCKING) {
1282 ioflag |= IO_NDELAY;
1283 } else if (fp->f_flag & FNONBLOCK) {
1284 ioflag |= IO_NDELAY;
1286 if (fp->f_flag & O_DIRECT) {
1287 ioflag |= IO_DIRECT;
1289 ioflag |= sequential_heuristic(uio, fp);
1291 error = dev_dread(dev, uio, ioflag, fp);
1295 vfs_timestamp(&node->atime);
1296 if ((flags & O_FOFFSET) == 0)
1297 fp->f_offset = uio->uio_offset;
1298 fp->f_nextoff = uio->uio_offset;
1305 devfs_fo_write(struct file *fp, struct uio *uio,
1306 struct ucred *cred, int flags)
1308 struct devfs_node *node;
1314 KASSERT(uio->uio_td == curthread,
1315 ("uio_td %p is not p %p", uio->uio_td, curthread));
1317 vp = (struct vnode *)fp->f_data;
1318 if (vp == NULL || vp->v_type == VBAD)
1321 node = DEVFS_NODE(vp);
1323 if (vp->v_type == VREG)
1324 bwillwrite(uio->uio_resid);
1326 vp = (struct vnode *)fp->f_data;
1328 if ((dev = vp->v_rdev) == NULL)
1333 if ((flags & O_FOFFSET) == 0)
1334 uio->uio_offset = fp->f_offset;
1337 if (vp->v_type == VREG &&
1338 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1339 ioflag |= IO_APPEND;
1342 if (flags & O_FBLOCKING) {
1343 /* ioflag &= ~IO_NDELAY; */
1344 } else if (flags & O_FNONBLOCKING) {
1345 ioflag |= IO_NDELAY;
1346 } else if (fp->f_flag & FNONBLOCK) {
1347 ioflag |= IO_NDELAY;
1349 if (fp->f_flag & O_DIRECT) {
1350 ioflag |= IO_DIRECT;
1352 if (flags & O_FASYNCWRITE) {
1353 /* ioflag &= ~IO_SYNC; */
1354 } else if (flags & O_FSYNCWRITE) {
1356 } else if (fp->f_flag & O_FSYNC) {
1360 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1362 ioflag |= sequential_heuristic(uio, fp);
1364 error = dev_dwrite(dev, uio, ioflag, fp);
1368 vfs_timestamp(&node->atime);
1369 vfs_timestamp(&node->mtime);
1372 if ((flags & O_FOFFSET) == 0)
1373 fp->f_offset = uio->uio_offset;
1374 fp->f_nextoff = uio->uio_offset;
1381 devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
1390 vp = (struct vnode *)fp->f_data;
1391 if (vp == NULL || vp->v_type == VBAD)
1394 error = vn_stat(vp, sb, cred);
1399 error = VOP_GETATTR(vp, vap);
1404 * Zero the spare stat fields
1410 * Copy from vattr table ... or not in case it's a cloned device
1412 if (vap->va_fsid != VNOVAL)
1413 sb->st_dev = vap->va_fsid;
1415 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1417 sb->st_ino = vap->va_fileid;
1419 mode = vap->va_mode;
1423 if (vap->va_nlink > (nlink_t)-1)
1424 sb->st_nlink = (nlink_t)-1;
1426 sb->st_nlink = vap->va_nlink;
1428 sb->st_uid = vap->va_uid;
1429 sb->st_gid = vap->va_gid;
1430 sb->st_rdev = devid_from_dev(DEVFS_NODE(vp)->d_dev);
1431 sb->st_size = vap->va_bytes;
1432 sb->st_atimespec = vap->va_atime;
1433 sb->st_mtimespec = vap->va_mtime;
1434 sb->st_ctimespec = vap->va_ctime;
1437 * A VCHR and VBLK device may track the last access and last modified
1438 * time independantly of the filesystem. This is particularly true
1439 * because device read and write calls may bypass the filesystem.
1441 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1444 if (dev->si_lastread) {
1445 sb->st_atimespec.tv_sec = time_second +
1448 sb->st_atimespec.tv_nsec = 0;
1450 if (dev->si_lastwrite) {
1451 sb->st_mtimespec.tv_sec = time_second +
1452 (dev->si_lastwrite -
1454 sb->st_mtimespec.tv_nsec = 0;
1460 * According to www.opengroup.org, the meaning of st_blksize is
1461 * "a filesystem-specific preferred I/O block size for this
1462 * object. In some filesystem types, this may vary from file
1464 * Default to PAGE_SIZE after much discussion.
1467 sb->st_blksize = PAGE_SIZE;
1469 sb->st_flags = vap->va_flags;
1471 error = caps_priv_check(cred, SYSCAP_NOVFS_GENERATION);
1475 sb->st_gen = (u_int32_t)vap->va_gen;
1477 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1480 * This is for ABI compatibility <= 5.7 (for ABI change made in
1483 sb->__old_st_blksize = sb->st_blksize;
1490 devfs_fo_kqfilter(struct file *fp, struct knote *kn)
1496 vp = (struct vnode *)fp->f_data;
1497 if (vp == NULL || vp->v_type == VBAD) {
1501 if ((dev = vp->v_rdev) == NULL) {
1507 error = dev_dkqfilter(dev, kn, fp);
1516 devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
1517 struct ucred *ucred, struct sysmsg *msg)
1520 struct devfs_node *node;
1526 struct fiodname_args *name_args;
1530 vp = ((struct vnode *)fp->f_data);
1532 if ((dev = vp->v_rdev) == NULL)
1533 return EBADF; /* device was revoked */
1538 node = DEVFS_NODE(vp);
1541 devfs_debug(DEVFS_DEBUG_DEBUG,
1542 "devfs_fo_ioctl() called! for dev %s\n",
1545 if (com == FIODTYPE) {
1546 *(int *)data = dev_dflags(dev) & D_TYPEMASK;
1549 } else if (com == FIODNAME) {
1550 name_args = (struct fiodname_args *)data;
1551 name = dev->si_name;
1552 namlen = strlen(name) + 1;
1554 devfs_debug(DEVFS_DEBUG_DEBUG,
1555 "ioctl, got: FIODNAME for %s\n", name);
1557 if (namlen <= name_args->len)
1558 error = copyout(dev->si_name, name_args->name, namlen);
1562 devfs_debug(DEVFS_DEBUG_DEBUG,
1563 "ioctl stuff: error: %d\n", error);
1567 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp);
1571 vfs_timestamp(&node->atime);
1572 vfs_timestamp(&node->mtime);
1575 if (com == TIOCSCTTY) {
1576 devfs_debug(DEVFS_DEBUG_DEBUG,
1577 "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
1580 if (error == 0 && com == TIOCSCTTY) {
1581 struct proc *p = curthread->td_proc;
1582 struct session *sess;
1584 devfs_debug(DEVFS_DEBUG_DEBUG,
1585 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
1591 sess = p->p_session;
1594 * Do nothing if reassigning same control tty
1596 if (sess->s_ttyvp == vp) {
1602 * Get rid of reference to old control tty
1604 ovp = sess->s_ttyvp;
1613 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
1619 devfs_spec_fsync(struct vop_fsync_args *ap)
1621 struct vnode *vp = ap->a_vp;
1624 if (!vn_isdisk(vp, NULL))
1628 * Flush all dirty buffers associated with a block device.
1630 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1635 devfs_spec_read(struct vop_read_args *ap)
1637 struct devfs_node *node;
1646 node = DEVFS_NODE(vp);
1648 if (dev == NULL) /* device was revoked */
1650 if (uio->uio_resid == 0)
1654 error = dev_dread(dev, uio, ap->a_ioflag, NULL);
1655 vn_lock(vp, LK_SHARED | LK_RETRY);
1658 vfs_timestamp(&node->atime);
1664 * Vnode op for write
1666 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1667 * struct ucred *a_cred)
1670 devfs_spec_write(struct vop_write_args *ap)
1672 struct devfs_node *node;
1681 node = DEVFS_NODE(vp);
1683 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1685 if (dev == NULL) /* device was revoked */
1689 error = dev_dwrite(dev, uio, ap->a_ioflag, NULL);
1690 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1693 vfs_timestamp(&node->atime);
1694 vfs_timestamp(&node->mtime);
1701 * Device ioctl operation.
1703 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
1704 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
1707 devfs_spec_ioctl(struct vop_ioctl_args *ap)
1709 struct vnode *vp = ap->a_vp;
1711 struct devfs_node *node;
1715 if ((dev = vp->v_rdev) == NULL)
1716 return (EBADF); /* device was revoked */
1718 node = DEVFS_NODE(vp);
1721 vfs_timestamp(&node->atime);
1722 vfs_timestamp(&node->mtime);
1726 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
1727 ap->a_cred, ap->a_sysmsg, NULL));
1731 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1735 devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1737 struct vnode *vp = ap->a_vp;
1739 struct devfs_node *node;
1743 if ((dev = vp->v_rdev) == NULL)
1744 return (EBADF); /* device was revoked (EBADF) */
1746 node = DEVFS_NODE(vp);
1749 vfs_timestamp(&node->atime);
1752 return (dev_dkqfilter(dev, ap->a_kn, NULL));
1756 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1757 * calls are not limited to device DMA limits so we have to deal with the
1760 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1763 devfs_spec_strategy(struct vop_strategy_args *ap)
1765 struct bio *bio = ap->a_bio;
1766 struct buf *bp = bio->bio_buf;
1773 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1777 * Collect statistics on synchronous and asynchronous read
1778 * and write counts for disks that have associated filesystems.
1781 KKASSERT(vp->v_rdev != NULL); /* XXX */
1782 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1783 if (bp->b_cmd == BUF_CMD_READ) {
1784 if (bp->b_flags & BIO_SYNC)
1785 mp->mnt_stat.f_syncreads++;
1787 mp->mnt_stat.f_asyncreads++;
1789 if (bp->b_flags & BIO_SYNC)
1790 mp->mnt_stat.f_syncwrites++;
1792 mp->mnt_stat.f_asyncwrites++;
1797 * Device iosize limitations only apply to read and write. Shortcut
1798 * the I/O if it fits.
1800 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1801 devfs_debug(DEVFS_DEBUG_DEBUG,
1802 "%s: si_iosize_max not set!\n",
1803 dev_dname(vp->v_rdev));
1804 maxiosize = MAXPHYS;
1806 #if SPEC_CHAIN_DEBUG & 2
1809 if (bp->b_bcount <= maxiosize ||
1810 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1811 dev_dstrategy_chain(vp->v_rdev, bio);
1816 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1818 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1821 BUF_LOCK(nbp, LK_EXCLUSIVE);
1824 nbp->b_flags = B_PAGING | B_KVABIO | (bp->b_flags & B_BNOCLIP);
1825 nbp->b_cpumask = bp->b_cpumask;
1826 nbp->b_data = bp->b_data;
1827 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1828 nbp->b_bio1.bio_offset = bio->bio_offset;
1829 nbp->b_bio1.bio_caller_info1.ptr = bio;
1832 * Start the first transfer
1834 if (vn_isdisk(vp, NULL))
1835 chunksize = vp->v_rdev->si_bsize_phys;
1837 chunksize = DEV_BSIZE;
1838 chunksize = rounddown(maxiosize, chunksize);
1839 #if SPEC_CHAIN_DEBUG & 1
1840 devfs_debug(DEVFS_DEBUG_DEBUG,
1841 "spec_strategy chained I/O chunksize=%d\n",
1844 nbp->b_cmd = bp->b_cmd;
1845 nbp->b_bcount = chunksize;
1846 nbp->b_bufsize = chunksize; /* used to detect a short I/O */
1847 nbp->b_bio1.bio_caller_info2.index = chunksize;
1849 #if SPEC_CHAIN_DEBUG & 1
1850 devfs_debug(DEVFS_DEBUG_DEBUG,
1851 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1852 bp, 0, bp->b_bcount, nbp->b_bcount);
1855 dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1857 if (DEVFS_NODE(vp)) {
1858 vfs_timestamp(&DEVFS_NODE(vp)->atime);
1859 vfs_timestamp(&DEVFS_NODE(vp)->mtime);
1866 * Chunked up transfer completion routine - chain transfers until done
1868 * NOTE: MPSAFE callback.
1872 devfs_spec_strategy_done(struct bio *nbio)
1874 struct buf *nbp = nbio->bio_buf;
1875 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
1876 struct buf *bp = bio->bio_buf; /* original bp */
1877 int chunksize = nbio->bio_caller_info2.index; /* chunking */
1878 int boffset = nbp->b_data - bp->b_data;
1880 if (nbp->b_flags & B_ERROR) {
1882 * An error terminates the chain, propogate the error back
1883 * to the original bp
1885 bp->b_flags |= B_ERROR;
1886 bp->b_error = nbp->b_error;
1887 bp->b_resid = bp->b_bcount - boffset +
1888 (nbp->b_bcount - nbp->b_resid);
1889 #if SPEC_CHAIN_DEBUG & 1
1890 devfs_debug(DEVFS_DEBUG_DEBUG,
1891 "spec_strategy: chain %p error %d bcount %d/%d\n",
1892 bp, bp->b_error, bp->b_bcount,
1893 bp->b_bcount - bp->b_resid);
1895 } else if (nbp->b_resid) {
1897 * A short read or write terminates the chain
1899 bp->b_error = nbp->b_error;
1900 bp->b_resid = bp->b_bcount - boffset +
1901 (nbp->b_bcount - nbp->b_resid);
1902 #if SPEC_CHAIN_DEBUG & 1
1903 devfs_debug(DEVFS_DEBUG_DEBUG,
1904 "spec_strategy: chain %p short read(1) "
1906 bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
1908 } else if (nbp->b_bcount != nbp->b_bufsize) {
1910 * A short read or write can also occur by truncating b_bcount
1912 #if SPEC_CHAIN_DEBUG & 1
1913 devfs_debug(DEVFS_DEBUG_DEBUG,
1914 "spec_strategy: chain %p short read(2) "
1916 bp, nbp->b_bcount + boffset, bp->b_bcount);
1919 bp->b_bcount = nbp->b_bcount + boffset;
1920 bp->b_resid = nbp->b_resid;
1921 } else if (nbp->b_bcount + boffset == bp->b_bcount) {
1923 * No more data terminates the chain
1925 #if SPEC_CHAIN_DEBUG & 1
1926 devfs_debug(DEVFS_DEBUG_DEBUG,
1927 "spec_strategy: chain %p finished bcount %d\n",
1934 * Continue the chain
1936 boffset += nbp->b_bcount;
1937 nbp->b_data = bp->b_data + boffset;
1938 nbp->b_bcount = bp->b_bcount - boffset;
1939 if (nbp->b_bcount > chunksize)
1940 nbp->b_bcount = chunksize;
1941 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1942 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1944 #if SPEC_CHAIN_DEBUG & 1
1945 devfs_debug(DEVFS_DEBUG_DEBUG,
1946 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1947 bp, boffset, bp->b_bcount, nbp->b_bcount);
1950 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
1955 * Fall through to here on termination. biodone(bp) and
1956 * clean up and free nbp.
1961 kfree(nbp, M_DEVBUF);
1965 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1968 devfs_spec_freeblks(struct vop_freeblks_args *ap)
1973 * Must be a synchronous operation
1975 KKASSERT(ap->a_vp->v_rdev != NULL);
1976 if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0)
1979 bp->b_cmd = BUF_CMD_FREEBLKS;
1980 bp->b_bio1.bio_flags |= BIO_SYNC;
1981 bp->b_bio1.bio_offset = ap->a_offset;
1982 bp->b_bio1.bio_done = biodone_sync;
1983 bp->b_bcount = ap->a_length;
1984 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1985 biowait(&bp->b_bio1, "TRIM");
1992 * Implement degenerate case where the block requested is the block
1993 * returned, and assume that the entire device is contiguous in regards
1994 * to the contiguous block range (runp and runb).
1996 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1997 * off_t *a_doffsetp, int *a_runp, int *a_runb)
2000 devfs_spec_bmap(struct vop_bmap_args *ap)
2002 if (ap->a_doffsetp != NULL)
2003 *ap->a_doffsetp = ap->a_loffset;
2004 if (ap->a_runp != NULL)
2005 *ap->a_runp = MAXBSIZE;
2006 if (ap->a_runb != NULL) {
2007 if (ap->a_loffset < MAXBSIZE)
2008 *ap->a_runb = (int)ap->a_loffset;
2010 *ap->a_runb = MAXBSIZE;
2017 * Special device advisory byte-level locks.
2019 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
2020 * struct flock *a_fl, int a_flags)
2024 devfs_spec_advlock(struct vop_advlock_args *ap)
2026 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
2030 * NOTE: MPSAFE callback.
2033 devfs_spec_getpages_iodone(struct bio *bio)
2035 bio->bio_buf->b_cmd = BUF_CMD_DONE;
2036 wakeup(bio->bio_buf);
2040 * spec_getpages() - get pages associated with device vnode.
2042 * Note that spec_read and spec_write do not use the buffer cache, so we
2043 * must fully implement getpages here.
2046 devfs_spec_getpages(struct vop_getpages_args *ap)
2050 int i, pcount, size;
2053 vm_ooffset_t offset;
2054 int toff, nextoff, nread;
2055 struct vnode *vp = ap->a_vp;
2060 pcount = round_page(ap->a_count) / PAGE_SIZE;
2063 * Calculate the offset of the transfer and do sanity check.
2065 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
2068 * Round up physical size for real devices. We cannot round using
2069 * v_mount's block size data because v_mount has nothing to do with
2070 * the device. i.e. it's usually '/dev'. We need the physical block
2071 * size for the device itself.
2073 * We can't use v_rdev->si_mountpoint because it only exists when the
2074 * block device is mounted. However, we can use v_rdev.
2076 if (vn_isdisk(vp, NULL))
2077 blksiz = vp->v_rdev->si_bsize_phys;
2081 size = roundup2(ap->a_count, blksiz);
2083 bp = getpbuf_kva(NULL);
2084 kva = (vm_offset_t)bp->b_data;
2087 * Map the pages to be read into the kva.
2089 pmap_qenter_noinval(kva, ap->a_m, pcount);
2091 /* Build a minimal buffer header. */
2092 bp->b_cmd = BUF_CMD_READ;
2093 bp->b_flags |= B_KVABIO;
2094 bp->b_bcount = size;
2096 bsetrunningbufspace(bp, size);
2098 bp->b_bio1.bio_offset = offset;
2099 bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
2101 mycpu->gd_cnt.v_vnodein++;
2102 mycpu->gd_cnt.v_vnodepgsin += pcount;
2105 vn_strategy(ap->a_vp, &bp->b_bio1);
2109 /* We definitely need to be at splbio here. */
2110 while (bp->b_cmd != BUF_CMD_DONE)
2111 tsleep(bp, 0, "spread", 0);
2115 if (bp->b_flags & B_ERROR) {
2117 error = bp->b_error;
2123 * If EOF is encountered we must zero-extend the result in order
2124 * to ensure that the page does not contain garabge. When no
2125 * error occurs, an early EOF is indicated if b_bcount got truncated.
2126 * b_resid is relative to b_bcount and should be 0, but some devices
2127 * might indicate an EOF with b_resid instead of truncating b_bcount.
2129 nread = bp->b_bcount - bp->b_resid;
2130 if (nread < ap->a_count) {
2132 bzero((caddr_t)kva + nread, ap->a_count - nread);
2134 pmap_qremove_noinval(kva, pcount);
2137 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
2138 nextoff = toff + PAGE_SIZE;
2142 * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2143 * pmap modified bit. pmap modified bit should have
2144 * already been cleared.
2146 if (nextoff <= nread) {
2147 m->valid = VM_PAGE_BITS_ALL;
2149 } else if (toff < nread) {
2151 * Since this is a VM request, we have to supply the
2152 * unaligned offset to allow vm_page_set_valid()
2153 * to zero sub-DEV_BSIZE'd portions of the page.
2155 vm_page_set_valid(m, 0, nread - toff);
2156 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
2162 if (i != ap->a_reqpage) {
2164 * Just in case someone was asking for this page we
2165 * now tell them that it is ok to use.
2167 if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
2169 if (m->flags & PG_REFERENCED) {
2170 vm_page_activate(m);
2172 vm_page_deactivate(m);
2181 } else if (m->valid) {
2184 * Since this is a VM request, we need to make the
2185 * entire page presentable by zeroing invalid sections.
2187 if (m->valid != VM_PAGE_BITS_ALL)
2188 vm_page_zero_invalid(m, FALSE);
2192 m = ap->a_m[ap->a_reqpage];
2193 devfs_debug(DEVFS_DEBUG_WARNING,
2194 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
2195 devtoname(vp->v_rdev), error, bp, bp->b_vp);
2196 devfs_debug(DEVFS_DEBUG_WARNING,
2197 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
2198 size, bp->b_resid, ap->a_count, m->valid);
2199 devfs_debug(DEVFS_DEBUG_WARNING,
2200 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
2201 nread, ap->a_reqpage, (u_long)m->pindex, pcount);
2203 * Free the buffer header back to the swap buffer pool.
2206 return VM_PAGER_ERROR;
2209 * Free the buffer header back to the swap buffer pool.
2212 if (DEVFS_NODE(ap->a_vp))
2213 vfs_timestamp(&DEVFS_NODE(ap->a_vp)->mtime);
2219 sequential_heuristic(struct uio *uio, struct file *fp)
2222 * Sequential heuristic - detect sequential operation
2224 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2225 uio->uio_offset == fp->f_nextoff) {
2227 * XXX we assume that the filesystem block size is
2228 * the default. Not true, but still gives us a pretty
2229 * good indicator of how sequential the read operations
2232 int tmpseq = fp->f_seqcount;
2234 tmpseq += howmany(uio->uio_resid, MAXBSIZE);
2235 if (tmpseq > IO_SEQMAX)
2237 fp->f_seqcount = tmpseq;
2238 return(fp->f_seqcount << IO_SEQSHIFT);
2242 * Not sequential, quick draw-down of seqcount
2244 if (fp->f_seqcount > 1)