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_fp, vp);
884 vn_lock(vp, LK_SHARED | LK_RETRY);
892 vn_lock(vp, LK_UPGRADE | LK_RETRY);
893 if (node && ap->a_fp) {
896 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
897 lockmgr(&devfs_lock, LK_SHARED);
899 ndev = devfs_clone(dev, node->d_dir.d_name,
900 node->d_dir.d_namlen,
901 ap->a_mode, ap->a_cred);
903 lockmgr(&devfs_lock, LK_RELEASE);
904 lockmgr(&devfs_lock, LK_EXCLUSIVE);
905 newnode = devfs_create_device_node(
906 DEVFS_MNTDATA(vp->v_mount)->root_node,
907 ndev, &exists, NULL, NULL);
908 /* XXX: possibly destroy device if this happens */
910 if (newnode != NULL) {
915 devfs_debug(DEVFS_DEBUG_DEBUG,
916 "parent here is: %s, node is: |%s|\n",
917 ((node->parent->node_type == Nroot) ?
918 "ROOT!" : node->parent->d_dir.d_name),
919 newnode->d_dir.d_name);
920 devfs_debug(DEVFS_DEBUG_DEBUG,
922 ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
925 * orig_vp is set to the original vp if we
928 /* node->flags |= DEVFS_CLONED; */
929 devfs_allocv(&vp, newnode);
934 lockmgr(&devfs_lock, LK_RELEASE);
937 * Synchronize devfs here to make sure that, if the cloned
938 * device creates other device nodes in addition to the
939 * cloned one, all of them are created by the time we return
940 * from opening the cloned one.
946 devfs_debug(DEVFS_DEBUG_DEBUG,
947 "devfs_spec_open() called on %s! \n",
951 * Make this field valid before any I/O in ->d_open
953 * NOTE: Shared vnode lock probably held, but its ok as long
954 * as assignments are consistent.
956 if (!dev->si_iosize_max)
957 /* XXX: old DFLTPHYS == 64KB dependency */
958 dev->si_iosize_max = min(MAXPHYS,64*1024);
960 if (dev_dflags(dev) & D_TTY)
961 vsetflags(vp, VISTTY);
964 * Open the underlying device
967 error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred, ap->a_fp, vp);
968 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
971 * Clean up any cloned vp if we error out.
977 /* orig_vp = NULL; */
983 * This checks if the disk device is going to be opened for writing.
984 * It will be only allowed in the cases where securelevel permits it
985 * and it's not mounted R/W.
987 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
988 (ap->a_cred != FSCRED)) {
990 /* Very secure mode. No open for writing allowed */
991 if (securelevel >= 2)
995 * If it is mounted R/W, do not allow to open for writing.
996 * In the case it's mounted read-only but securelevel
997 * is >= 1, then do not allow opening for writing either.
999 if (vfs_mountedon(vp)) {
1000 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
1002 else if (securelevel >= 1)
1008 * NOTE: vnode is still locked shared. t_stop assignment should
1009 * remain consistent so we should be ok.
1011 if (dev_dflags(dev) & D_TTY) {
1016 devfs_debug(DEVFS_DEBUG_DEBUG,
1017 "devfs: no t_stop\n");
1018 tp->t_stop = nottystop;
1024 * NOTE: vnode is still locked shared. assignments should
1025 * remain consistent so we should be ok. However,
1026 * upgrade to exclusive if we need a VM object.
1028 if (vn_isdisk(vp, NULL)) {
1029 if (!dev->si_bsize_phys)
1030 dev->si_bsize_phys = DEV_BSIZE;
1031 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
1037 vfs_timestamp(&node->atime);
1040 * If we replaced the vp the vop_stdopen() call will have loaded
1041 * it into fp->f_data and vref()d the vp, giving us two refs. So
1042 * instead of just unlocking it here we have to vput() it.
1047 /* Ugly pty magic, to make pty devices appear once they are opened */
1048 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY) {
1049 if (node->flags & DEVFS_INVISIBLE)
1050 node->flags &= ~DEVFS_INVISIBLE;
1055 KKASSERT(ap->a_fp->f_type == DTYPE_VNODE);
1056 KKASSERT((ap->a_fp->f_flag & FMASK) == (ap->a_mode & FMASK));
1057 ap->a_fp->f_ops = &devfs_dev_fileops;
1058 KKASSERT(ap->a_fp->f_data == (void *)vp);
1065 devfs_spec_close(struct vop_close_args *ap)
1067 struct devfs_node *node;
1068 struct proc *p = curproc;
1069 struct vnode *vp = ap->a_vp;
1070 cdev_t dev = vp->v_rdev;
1076 * Devices flagged D_QUICK require no special handling.
1078 if (dev && dev_dflags(dev) & D_QUICK) {
1079 opencount = vp->v_opencount;
1081 opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1082 if (((vp->v_flag & VRECLAIMED) ||
1083 (dev_dflags(dev) & D_TRACKCLOSE) ||
1084 (opencount == 1))) {
1086 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1087 vn_lock(vp, LK_SHARED | LK_RETRY);
1093 * We do special tests on the opencount so unfortunately we need
1094 * an exclusive lock.
1096 vn_lock(vp, LK_UPGRADE | LK_RETRY);
1099 devfs_debug(DEVFS_DEBUG_DEBUG,
1100 "devfs_spec_close() called on %s! \n",
1103 devfs_debug(DEVFS_DEBUG_DEBUG,
1104 "devfs_spec_close() called, null vode!\n");
1107 * A couple of hacks for devices and tty devices. The
1108 * vnode ref count cannot be used to figure out the
1109 * last close, but we can use v_opencount now that
1110 * revoke works properly.
1112 * Detect the last close on a controlling terminal and clear
1113 * the session (half-close).
1115 * XXX opencount is not SMP safe. The vnode is locked but there
1116 * may be multiple vnodes referencing the same device.
1120 * NOTE: Try to avoid global tokens when testing opencount
1121 * XXX hack, fixme. needs a struct lock and opencount in
1122 * struct cdev itself.
1125 opencount = vp->v_opencount;
1127 opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1132 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
1133 p->p_session->s_ttyvp = NULL;
1138 * Vnodes can be opened and closed multiple times. Do not really
1139 * close the device unless (1) it is being closed forcibly,
1140 * (2) the device wants to track closes, or (3) this is the last
1141 * vnode doing its last close on the device.
1143 * XXX the VXLOCK (force close) case can leave vnodes referencing
1144 * a closed device. This might not occur now that our revoke is
1147 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
1148 if (dev && ((vp->v_flag & VRECLAIMED) ||
1149 (dev_dflags(dev) & D_TRACKCLOSE) ||
1150 (opencount == 1))) {
1152 * Ugly pty magic, to make pty devices disappear again once
1155 node = DEVFS_NODE(ap->a_vp);
1156 if (node && (node->flags & DEVFS_PTY))
1157 node->flags |= DEVFS_INVISIBLE;
1160 * Unlock around dev_dclose(), unless the vnode is
1161 * undergoing a vgone/reclaim (during umount).
1164 if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) {
1170 * WARNING! If the device destroys itself the devfs node
1171 * can disappear here.
1173 * WARNING! vn_lock() will fail if the vp is in a VRECLAIM,
1174 * which can occur during umount.
1176 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1177 /* node is now stale */
1180 if (vn_lock(vp, LK_EXCLUSIVE |
1182 LK_FAILRECLAIM) != 0) {
1183 panic("devfs_spec_close: vnode %p "
1184 "unexpectedly could not be relocked",
1191 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
1194 * Track the actual opens and closes on the vnode. The last close
1195 * disassociates the rdev. If the rdev is already disassociated or
1196 * the opencount is already 0, the vnode might have been revoked
1197 * and no further opencount tracking occurs.
1202 if (vp->v_opencount > 0)
1210 devfs_fo_close(struct file *fp)
1212 struct vnode *vp = (struct vnode *)fp->f_data;
1215 fp->f_ops = &badfileops;
1216 error = vn_close(vp, fp->f_flag, fp);
1217 devfs_clear_cdevpriv(fp);
1224 * Device-optimized file table vnode read routine.
1226 * This bypasses the VOP table and talks directly to the device. Most
1227 * filesystems just route to specfs and can make this optimization.
1230 devfs_fo_read(struct file *fp, struct uio *uio,
1231 struct ucred *cred, int flags)
1233 struct devfs_node *node;
1239 KASSERT(uio->uio_td == curthread,
1240 ("uio_td %p is not td %p", uio->uio_td, curthread));
1242 if (uio->uio_resid == 0)
1245 vp = (struct vnode *)fp->f_data;
1246 if (vp == NULL || vp->v_type == VBAD)
1249 node = DEVFS_NODE(vp);
1251 if ((dev = vp->v_rdev) == NULL)
1256 if ((flags & O_FOFFSET) == 0)
1257 uio->uio_offset = fp->f_offset;
1260 if (flags & O_FBLOCKING) {
1261 /* ioflag &= ~IO_NDELAY; */
1262 } else if (flags & O_FNONBLOCKING) {
1263 ioflag |= IO_NDELAY;
1264 } else if (fp->f_flag & FNONBLOCK) {
1265 ioflag |= IO_NDELAY;
1267 if (fp->f_flag & O_DIRECT) {
1268 ioflag |= IO_DIRECT;
1270 ioflag |= sequential_heuristic(uio, fp);
1272 error = dev_dread(dev, uio, ioflag, fp);
1276 vfs_timestamp(&node->atime);
1277 if ((flags & O_FOFFSET) == 0)
1278 fp->f_offset = uio->uio_offset;
1279 fp->f_nextoff = uio->uio_offset;
1286 devfs_fo_write(struct file *fp, struct uio *uio,
1287 struct ucred *cred, int flags)
1289 struct devfs_node *node;
1295 KASSERT(uio->uio_td == curthread,
1296 ("uio_td %p is not p %p", uio->uio_td, curthread));
1298 vp = (struct vnode *)fp->f_data;
1299 if (vp == NULL || vp->v_type == VBAD)
1302 node = DEVFS_NODE(vp);
1304 if (vp->v_type == VREG)
1305 bwillwrite(uio->uio_resid);
1307 vp = (struct vnode *)fp->f_data;
1309 if ((dev = vp->v_rdev) == NULL)
1314 if ((flags & O_FOFFSET) == 0)
1315 uio->uio_offset = fp->f_offset;
1318 if (vp->v_type == VREG &&
1319 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1320 ioflag |= IO_APPEND;
1323 if (flags & O_FBLOCKING) {
1324 /* ioflag &= ~IO_NDELAY; */
1325 } else if (flags & O_FNONBLOCKING) {
1326 ioflag |= IO_NDELAY;
1327 } else if (fp->f_flag & FNONBLOCK) {
1328 ioflag |= IO_NDELAY;
1330 if (fp->f_flag & O_DIRECT) {
1331 ioflag |= IO_DIRECT;
1333 if (flags & O_FASYNCWRITE) {
1334 /* ioflag &= ~IO_SYNC; */
1335 } else if (flags & O_FSYNCWRITE) {
1337 } else if (fp->f_flag & O_FSYNC) {
1341 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1343 ioflag |= sequential_heuristic(uio, fp);
1345 error = dev_dwrite(dev, uio, ioflag, fp);
1349 vfs_timestamp(&node->atime);
1350 vfs_timestamp(&node->mtime);
1353 if ((flags & O_FOFFSET) == 0)
1354 fp->f_offset = uio->uio_offset;
1355 fp->f_nextoff = uio->uio_offset;
1362 devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
1371 vp = (struct vnode *)fp->f_data;
1372 if (vp == NULL || vp->v_type == VBAD)
1375 error = vn_stat(vp, sb, cred);
1380 error = VOP_GETATTR(vp, vap);
1385 * Zero the spare stat fields
1391 * Copy from vattr table ... or not in case it's a cloned device
1393 if (vap->va_fsid != VNOVAL)
1394 sb->st_dev = vap->va_fsid;
1396 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1398 sb->st_ino = vap->va_fileid;
1400 mode = vap->va_mode;
1404 if (vap->va_nlink > (nlink_t)-1)
1405 sb->st_nlink = (nlink_t)-1;
1407 sb->st_nlink = vap->va_nlink;
1409 sb->st_uid = vap->va_uid;
1410 sb->st_gid = vap->va_gid;
1411 sb->st_rdev = devid_from_dev(DEVFS_NODE(vp)->d_dev);
1412 sb->st_size = vap->va_bytes;
1413 sb->st_atimespec = vap->va_atime;
1414 sb->st_mtimespec = vap->va_mtime;
1415 sb->st_ctimespec = vap->va_ctime;
1418 * A VCHR and VBLK device may track the last access and last modified
1419 * time independantly of the filesystem. This is particularly true
1420 * because device read and write calls may bypass the filesystem.
1422 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1425 if (dev->si_lastread) {
1426 sb->st_atimespec.tv_sec = time_second +
1429 sb->st_atimespec.tv_nsec = 0;
1431 if (dev->si_lastwrite) {
1432 sb->st_atimespec.tv_sec = time_second +
1435 sb->st_atimespec.tv_nsec = 0;
1441 * According to www.opengroup.org, the meaning of st_blksize is
1442 * "a filesystem-specific preferred I/O block size for this
1443 * object. In some filesystem types, this may vary from file
1445 * Default to PAGE_SIZE after much discussion.
1448 sb->st_blksize = PAGE_SIZE;
1450 sb->st_flags = vap->va_flags;
1452 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1456 sb->st_gen = (u_int32_t)vap->va_gen;
1458 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1461 * This is for ABI compatibility <= 5.7 (for ABI change made in
1464 sb->__old_st_blksize = sb->st_blksize;
1471 devfs_fo_kqfilter(struct file *fp, struct knote *kn)
1477 vp = (struct vnode *)fp->f_data;
1478 if (vp == NULL || vp->v_type == VBAD) {
1482 if ((dev = vp->v_rdev) == NULL) {
1488 error = dev_dkqfilter(dev, kn, fp);
1497 devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
1498 struct ucred *ucred, struct sysmsg *msg)
1501 struct devfs_node *node;
1507 struct fiodname_args *name_args;
1511 vp = ((struct vnode *)fp->f_data);
1513 if ((dev = vp->v_rdev) == NULL)
1514 return EBADF; /* device was revoked */
1519 node = DEVFS_NODE(vp);
1522 devfs_debug(DEVFS_DEBUG_DEBUG,
1523 "devfs_fo_ioctl() called! for dev %s\n",
1526 if (com == FIODTYPE) {
1527 *(int *)data = dev_dflags(dev) & D_TYPEMASK;
1530 } else if (com == FIODNAME) {
1531 name_args = (struct fiodname_args *)data;
1532 name = dev->si_name;
1533 namlen = strlen(name) + 1;
1535 devfs_debug(DEVFS_DEBUG_DEBUG,
1536 "ioctl, got: FIODNAME for %s\n", name);
1538 if (namlen <= name_args->len)
1539 error = copyout(dev->si_name, name_args->name, namlen);
1543 devfs_debug(DEVFS_DEBUG_DEBUG,
1544 "ioctl stuff: error: %d\n", error);
1548 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp);
1552 vfs_timestamp(&node->atime);
1553 vfs_timestamp(&node->mtime);
1556 if (com == TIOCSCTTY) {
1557 devfs_debug(DEVFS_DEBUG_DEBUG,
1558 "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
1561 if (error == 0 && com == TIOCSCTTY) {
1562 struct proc *p = curthread->td_proc;
1563 struct session *sess;
1565 devfs_debug(DEVFS_DEBUG_DEBUG,
1566 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
1572 sess = p->p_session;
1575 * Do nothing if reassigning same control tty
1577 if (sess->s_ttyvp == vp) {
1583 * Get rid of reference to old control tty
1585 ovp = sess->s_ttyvp;
1594 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
1600 devfs_spec_fsync(struct vop_fsync_args *ap)
1602 struct vnode *vp = ap->a_vp;
1605 if (!vn_isdisk(vp, NULL))
1609 * Flush all dirty buffers associated with a block device.
1611 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1616 devfs_spec_read(struct vop_read_args *ap)
1618 struct devfs_node *node;
1627 node = DEVFS_NODE(vp);
1629 if (dev == NULL) /* device was revoked */
1631 if (uio->uio_resid == 0)
1635 error = dev_dread(dev, uio, ap->a_ioflag, NULL);
1636 vn_lock(vp, LK_SHARED | LK_RETRY);
1639 vfs_timestamp(&node->atime);
1645 * Vnode op for write
1647 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1648 * struct ucred *a_cred)
1651 devfs_spec_write(struct vop_write_args *ap)
1653 struct devfs_node *node;
1662 node = DEVFS_NODE(vp);
1664 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1666 if (dev == NULL) /* device was revoked */
1670 error = dev_dwrite(dev, uio, ap->a_ioflag, NULL);
1671 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1674 vfs_timestamp(&node->atime);
1675 vfs_timestamp(&node->mtime);
1682 * Device ioctl operation.
1684 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
1685 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
1688 devfs_spec_ioctl(struct vop_ioctl_args *ap)
1690 struct vnode *vp = ap->a_vp;
1692 struct devfs_node *node;
1696 if ((dev = vp->v_rdev) == NULL)
1697 return (EBADF); /* device was revoked */
1699 node = DEVFS_NODE(vp);
1702 vfs_timestamp(&node->atime);
1703 vfs_timestamp(&node->mtime);
1707 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
1708 ap->a_cred, ap->a_sysmsg, NULL));
1712 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1716 devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1718 struct vnode *vp = ap->a_vp;
1720 struct devfs_node *node;
1724 if ((dev = vp->v_rdev) == NULL)
1725 return (EBADF); /* device was revoked (EBADF) */
1727 node = DEVFS_NODE(vp);
1730 vfs_timestamp(&node->atime);
1733 return (dev_dkqfilter(dev, ap->a_kn, NULL));
1737 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1738 * calls are not limited to device DMA limits so we have to deal with the
1741 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1744 devfs_spec_strategy(struct vop_strategy_args *ap)
1746 struct bio *bio = ap->a_bio;
1747 struct buf *bp = bio->bio_buf;
1754 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1758 * Collect statistics on synchronous and asynchronous read
1759 * and write counts for disks that have associated filesystems.
1762 KKASSERT(vp->v_rdev != NULL); /* XXX */
1763 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1764 if (bp->b_cmd == BUF_CMD_READ) {
1765 if (bp->b_flags & BIO_SYNC)
1766 mp->mnt_stat.f_syncreads++;
1768 mp->mnt_stat.f_asyncreads++;
1770 if (bp->b_flags & BIO_SYNC)
1771 mp->mnt_stat.f_syncwrites++;
1773 mp->mnt_stat.f_asyncwrites++;
1778 * Device iosize limitations only apply to read and write. Shortcut
1779 * the I/O if it fits.
1781 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1782 devfs_debug(DEVFS_DEBUG_DEBUG,
1783 "%s: si_iosize_max not set!\n",
1784 dev_dname(vp->v_rdev));
1785 maxiosize = MAXPHYS;
1787 #if SPEC_CHAIN_DEBUG & 2
1790 if (bp->b_bcount <= maxiosize ||
1791 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1792 dev_dstrategy_chain(vp->v_rdev, bio);
1797 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1799 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1802 BUF_LOCK(nbp, LK_EXCLUSIVE);
1805 nbp->b_flags = B_PAGING | B_KVABIO | (bp->b_flags & B_BNOCLIP);
1806 nbp->b_cpumask = bp->b_cpumask;
1807 nbp->b_data = bp->b_data;
1808 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1809 nbp->b_bio1.bio_offset = bio->bio_offset;
1810 nbp->b_bio1.bio_caller_info1.ptr = bio;
1813 * Start the first transfer
1815 if (vn_isdisk(vp, NULL))
1816 chunksize = vp->v_rdev->si_bsize_phys;
1818 chunksize = DEV_BSIZE;
1819 chunksize = rounddown(maxiosize, chunksize);
1820 #if SPEC_CHAIN_DEBUG & 1
1821 devfs_debug(DEVFS_DEBUG_DEBUG,
1822 "spec_strategy chained I/O chunksize=%d\n",
1825 nbp->b_cmd = bp->b_cmd;
1826 nbp->b_bcount = chunksize;
1827 nbp->b_bufsize = chunksize; /* used to detect a short I/O */
1828 nbp->b_bio1.bio_caller_info2.index = chunksize;
1830 #if SPEC_CHAIN_DEBUG & 1
1831 devfs_debug(DEVFS_DEBUG_DEBUG,
1832 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1833 bp, 0, bp->b_bcount, nbp->b_bcount);
1836 dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1838 if (DEVFS_NODE(vp)) {
1839 vfs_timestamp(&DEVFS_NODE(vp)->atime);
1840 vfs_timestamp(&DEVFS_NODE(vp)->mtime);
1847 * Chunked up transfer completion routine - chain transfers until done
1849 * NOTE: MPSAFE callback.
1853 devfs_spec_strategy_done(struct bio *nbio)
1855 struct buf *nbp = nbio->bio_buf;
1856 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
1857 struct buf *bp = bio->bio_buf; /* original bp */
1858 int chunksize = nbio->bio_caller_info2.index; /* chunking */
1859 int boffset = nbp->b_data - bp->b_data;
1861 if (nbp->b_flags & B_ERROR) {
1863 * An error terminates the chain, propogate the error back
1864 * to the original bp
1866 bp->b_flags |= B_ERROR;
1867 bp->b_error = nbp->b_error;
1868 bp->b_resid = bp->b_bcount - boffset +
1869 (nbp->b_bcount - nbp->b_resid);
1870 #if SPEC_CHAIN_DEBUG & 1
1871 devfs_debug(DEVFS_DEBUG_DEBUG,
1872 "spec_strategy: chain %p error %d bcount %d/%d\n",
1873 bp, bp->b_error, bp->b_bcount,
1874 bp->b_bcount - bp->b_resid);
1876 } else if (nbp->b_resid) {
1878 * A short read or write terminates the chain
1880 bp->b_error = nbp->b_error;
1881 bp->b_resid = bp->b_bcount - boffset +
1882 (nbp->b_bcount - nbp->b_resid);
1883 #if SPEC_CHAIN_DEBUG & 1
1884 devfs_debug(DEVFS_DEBUG_DEBUG,
1885 "spec_strategy: chain %p short read(1) "
1887 bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
1889 } else if (nbp->b_bcount != nbp->b_bufsize) {
1891 * A short read or write can also occur by truncating b_bcount
1893 #if SPEC_CHAIN_DEBUG & 1
1894 devfs_debug(DEVFS_DEBUG_DEBUG,
1895 "spec_strategy: chain %p short read(2) "
1897 bp, nbp->b_bcount + boffset, bp->b_bcount);
1900 bp->b_bcount = nbp->b_bcount + boffset;
1901 bp->b_resid = nbp->b_resid;
1902 } else if (nbp->b_bcount + boffset == bp->b_bcount) {
1904 * No more data terminates the chain
1906 #if SPEC_CHAIN_DEBUG & 1
1907 devfs_debug(DEVFS_DEBUG_DEBUG,
1908 "spec_strategy: chain %p finished bcount %d\n",
1915 * Continue the chain
1917 boffset += nbp->b_bcount;
1918 nbp->b_data = bp->b_data + boffset;
1919 nbp->b_bcount = bp->b_bcount - boffset;
1920 if (nbp->b_bcount > chunksize)
1921 nbp->b_bcount = chunksize;
1922 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1923 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1925 #if SPEC_CHAIN_DEBUG & 1
1926 devfs_debug(DEVFS_DEBUG_DEBUG,
1927 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1928 bp, boffset, bp->b_bcount, nbp->b_bcount);
1931 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
1936 * Fall through to here on termination. biodone(bp) and
1937 * clean up and free nbp.
1942 kfree(nbp, M_DEVBUF);
1946 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1949 devfs_spec_freeblks(struct vop_freeblks_args *ap)
1954 * Must be a synchronous operation
1956 KKASSERT(ap->a_vp->v_rdev != NULL);
1957 if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0)
1960 bp->b_cmd = BUF_CMD_FREEBLKS;
1961 bp->b_bio1.bio_flags |= BIO_SYNC;
1962 bp->b_bio1.bio_offset = ap->a_offset;
1963 bp->b_bio1.bio_done = biodone_sync;
1964 bp->b_bcount = ap->a_length;
1965 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1966 biowait(&bp->b_bio1, "TRIM");
1973 * Implement degenerate case where the block requested is the block
1974 * returned, and assume that the entire device is contiguous in regards
1975 * to the contiguous block range (runp and runb).
1977 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1978 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1981 devfs_spec_bmap(struct vop_bmap_args *ap)
1983 if (ap->a_doffsetp != NULL)
1984 *ap->a_doffsetp = ap->a_loffset;
1985 if (ap->a_runp != NULL)
1986 *ap->a_runp = MAXBSIZE;
1987 if (ap->a_runb != NULL) {
1988 if (ap->a_loffset < MAXBSIZE)
1989 *ap->a_runb = (int)ap->a_loffset;
1991 *ap->a_runb = MAXBSIZE;
1998 * Special device advisory byte-level locks.
2000 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
2001 * struct flock *a_fl, int a_flags)
2005 devfs_spec_advlock(struct vop_advlock_args *ap)
2007 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
2011 * NOTE: MPSAFE callback.
2014 devfs_spec_getpages_iodone(struct bio *bio)
2016 bio->bio_buf->b_cmd = BUF_CMD_DONE;
2017 wakeup(bio->bio_buf);
2021 * spec_getpages() - get pages associated with device vnode.
2023 * Note that spec_read and spec_write do not use the buffer cache, so we
2024 * must fully implement getpages here.
2027 devfs_spec_getpages(struct vop_getpages_args *ap)
2031 int i, pcount, size;
2034 vm_ooffset_t offset;
2035 int toff, nextoff, nread;
2036 struct vnode *vp = ap->a_vp;
2041 pcount = round_page(ap->a_count) / PAGE_SIZE;
2044 * Calculate the offset of the transfer and do sanity check.
2046 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
2049 * Round up physical size for real devices. We cannot round using
2050 * v_mount's block size data because v_mount has nothing to do with
2051 * the device. i.e. it's usually '/dev'. We need the physical block
2052 * size for the device itself.
2054 * We can't use v_rdev->si_mountpoint because it only exists when the
2055 * block device is mounted. However, we can use v_rdev.
2057 if (vn_isdisk(vp, NULL))
2058 blksiz = vp->v_rdev->si_bsize_phys;
2062 size = roundup2(ap->a_count, blksiz);
2064 bp = getpbuf_kva(NULL);
2065 kva = (vm_offset_t)bp->b_data;
2068 * Map the pages to be read into the kva.
2070 pmap_qenter_noinval(kva, ap->a_m, pcount);
2072 /* Build a minimal buffer header. */
2073 bp->b_cmd = BUF_CMD_READ;
2074 bp->b_flags |= B_KVABIO;
2075 bp->b_bcount = size;
2077 bsetrunningbufspace(bp, size);
2079 bp->b_bio1.bio_offset = offset;
2080 bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
2082 mycpu->gd_cnt.v_vnodein++;
2083 mycpu->gd_cnt.v_vnodepgsin += pcount;
2086 vn_strategy(ap->a_vp, &bp->b_bio1);
2090 /* We definitely need to be at splbio here. */
2091 while (bp->b_cmd != BUF_CMD_DONE)
2092 tsleep(bp, 0, "spread", 0);
2096 if (bp->b_flags & B_ERROR) {
2098 error = bp->b_error;
2104 * If EOF is encountered we must zero-extend the result in order
2105 * to ensure that the page does not contain garabge. When no
2106 * error occurs, an early EOF is indicated if b_bcount got truncated.
2107 * b_resid is relative to b_bcount and should be 0, but some devices
2108 * might indicate an EOF with b_resid instead of truncating b_bcount.
2110 nread = bp->b_bcount - bp->b_resid;
2111 if (nread < ap->a_count) {
2113 bzero((caddr_t)kva + nread, ap->a_count - nread);
2115 pmap_qremove_noinval(kva, pcount);
2118 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
2119 nextoff = toff + PAGE_SIZE;
2123 * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2124 * pmap modified bit. pmap modified bit should have
2125 * already been cleared.
2127 if (nextoff <= nread) {
2128 m->valid = VM_PAGE_BITS_ALL;
2130 } else if (toff < nread) {
2132 * Since this is a VM request, we have to supply the
2133 * unaligned offset to allow vm_page_set_valid()
2134 * to zero sub-DEV_BSIZE'd portions of the page.
2136 vm_page_set_valid(m, 0, nread - toff);
2137 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
2143 if (i != ap->a_reqpage) {
2145 * Just in case someone was asking for this page we
2146 * now tell them that it is ok to use.
2148 if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
2150 if (m->flags & PG_REFERENCED) {
2151 vm_page_activate(m);
2153 vm_page_deactivate(m);
2162 } else if (m->valid) {
2165 * Since this is a VM request, we need to make the
2166 * entire page presentable by zeroing invalid sections.
2168 if (m->valid != VM_PAGE_BITS_ALL)
2169 vm_page_zero_invalid(m, FALSE);
2173 m = ap->a_m[ap->a_reqpage];
2174 devfs_debug(DEVFS_DEBUG_WARNING,
2175 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
2176 devtoname(vp->v_rdev), error, bp, bp->b_vp);
2177 devfs_debug(DEVFS_DEBUG_WARNING,
2178 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
2179 size, bp->b_resid, ap->a_count, m->valid);
2180 devfs_debug(DEVFS_DEBUG_WARNING,
2181 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
2182 nread, ap->a_reqpage, (u_long)m->pindex, pcount);
2184 * Free the buffer header back to the swap buffer pool.
2187 return VM_PAGER_ERROR;
2190 * Free the buffer header back to the swap buffer pool.
2193 if (DEVFS_NODE(ap->a_vp))
2194 vfs_timestamp(&DEVFS_NODE(ap->a_vp)->mtime);
2200 sequential_heuristic(struct uio *uio, struct file *fp)
2203 * Sequential heuristic - detect sequential operation
2205 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2206 uio->uio_offset == fp->f_nextoff) {
2208 * XXX we assume that the filesystem block size is
2209 * the default. Not true, but still gives us a pretty
2210 * good indicator of how sequential the read operations
2213 int tmpseq = fp->f_seqcount;
2215 tmpseq += (uio->uio_resid + MAXBSIZE - 1) / MAXBSIZE;
2216 if (tmpseq > IO_SEQMAX)
2218 fp->f_seqcount = tmpseq;
2219 return(fp->f_seqcount << IO_SEQSHIFT);
2223 * Not sequential, quick draw-down of seqcount
2225 if (fp->f_seqcount > 1)