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/namei.h>
50 #include <sys/dirent.h>
51 #include <sys/malloc.h>
54 #include <vm/vm_pager.h>
55 #include <vm/vm_zone.h>
56 #include <vm/vm_object.h>
57 #include <sys/filio.h>
58 #include <sys/ttycom.h>
60 #include <sys/diskslice.h>
61 #include <sys/sysctl.h>
62 #include <sys/devfs.h>
63 #include <sys/pioctl.h>
64 #include <vfs/fifofs/fifo.h>
66 #include <machine/limits.h>
69 #include <vm/vm_page2.h>
71 #ifndef SPEC_CHAIN_DEBUG
72 #define SPEC_CHAIN_DEBUG 0
75 MALLOC_DECLARE(M_DEVFS);
76 #define DEVFS_BADOP (void *)devfs_vop_badop
78 static int devfs_vop_badop(struct vop_generic_args *);
79 static int devfs_vop_access(struct vop_access_args *);
80 static int devfs_vop_inactive(struct vop_inactive_args *);
81 static int devfs_vop_reclaim(struct vop_reclaim_args *);
82 static int devfs_vop_readdir(struct vop_readdir_args *);
83 static int devfs_vop_getattr(struct vop_getattr_args *);
84 static int devfs_vop_setattr(struct vop_setattr_args *);
85 static int devfs_vop_readlink(struct vop_readlink_args *);
86 static int devfs_vop_print(struct vop_print_args *);
88 static int devfs_vop_nresolve(struct vop_nresolve_args *);
89 static int devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
90 static int devfs_vop_nmkdir(struct vop_nmkdir_args *);
91 static int devfs_vop_nsymlink(struct vop_nsymlink_args *);
92 static int devfs_vop_nrmdir(struct vop_nrmdir_args *);
93 static int devfs_vop_nremove(struct vop_nremove_args *);
95 static int devfs_spec_open(struct vop_open_args *);
96 static int devfs_spec_close(struct vop_close_args *);
97 static int devfs_spec_fsync(struct vop_fsync_args *);
99 static int devfs_spec_read(struct vop_read_args *);
100 static int devfs_spec_write(struct vop_write_args *);
101 static int devfs_spec_ioctl(struct vop_ioctl_args *);
102 static int devfs_spec_kqfilter(struct vop_kqfilter_args *);
103 static int devfs_spec_strategy(struct vop_strategy_args *);
104 static void devfs_spec_strategy_done(struct bio *);
105 static int devfs_spec_freeblks(struct vop_freeblks_args *);
106 static int devfs_spec_bmap(struct vop_bmap_args *);
107 static int devfs_spec_advlock(struct vop_advlock_args *);
108 static void devfs_spec_getpages_iodone(struct bio *);
109 static int devfs_spec_getpages(struct vop_getpages_args *);
111 static int devfs_fo_close(struct file *);
112 static int devfs_fo_read(struct file *, struct uio *, struct ucred *, int);
113 static int devfs_fo_write(struct file *, struct uio *, struct ucred *, int);
114 static int devfs_fo_stat(struct file *, struct stat *, struct ucred *);
115 static int devfs_fo_kqfilter(struct file *, struct knote *);
116 static int devfs_fo_ioctl(struct file *, u_long, caddr_t,
117 struct ucred *, struct sysmsg *);
118 static __inline int sequential_heuristic(struct uio *, struct file *);
120 extern struct lock devfs_lock;
123 * devfs vnode operations for regular files. All vnode ops are MPSAFE.
125 struct vop_ops devfs_vnode_norm_vops = {
126 .vop_default = vop_defaultop,
127 .vop_access = devfs_vop_access,
128 .vop_advlock = DEVFS_BADOP,
129 .vop_bmap = DEVFS_BADOP,
130 .vop_close = vop_stdclose,
131 .vop_getattr = devfs_vop_getattr,
132 .vop_inactive = devfs_vop_inactive,
133 .vop_ncreate = DEVFS_BADOP,
134 .vop_nresolve = devfs_vop_nresolve,
135 .vop_nlookupdotdot = devfs_vop_nlookupdotdot,
136 .vop_nlink = DEVFS_BADOP,
137 .vop_nmkdir = devfs_vop_nmkdir,
138 .vop_nmknod = DEVFS_BADOP,
139 .vop_nremove = devfs_vop_nremove,
140 .vop_nrename = DEVFS_BADOP,
141 .vop_nrmdir = devfs_vop_nrmdir,
142 .vop_nsymlink = devfs_vop_nsymlink,
143 .vop_open = vop_stdopen,
144 .vop_pathconf = vop_stdpathconf,
145 .vop_print = devfs_vop_print,
146 .vop_read = DEVFS_BADOP,
147 .vop_readdir = devfs_vop_readdir,
148 .vop_readlink = devfs_vop_readlink,
149 .vop_reallocblks = DEVFS_BADOP,
150 .vop_reclaim = devfs_vop_reclaim,
151 .vop_setattr = devfs_vop_setattr,
152 .vop_write = DEVFS_BADOP,
153 .vop_ioctl = DEVFS_BADOP
157 * devfs vnode operations for character devices. All vnode ops are MPSAFE.
159 struct vop_ops devfs_vnode_dev_vops = {
160 .vop_default = vop_defaultop,
161 .vop_access = devfs_vop_access,
162 .vop_advlock = devfs_spec_advlock,
163 .vop_bmap = devfs_spec_bmap,
164 .vop_close = devfs_spec_close,
165 .vop_freeblks = devfs_spec_freeblks,
166 .vop_fsync = devfs_spec_fsync,
167 .vop_getattr = devfs_vop_getattr,
168 .vop_getpages = devfs_spec_getpages,
169 .vop_inactive = devfs_vop_inactive,
170 .vop_open = devfs_spec_open,
171 .vop_pathconf = vop_stdpathconf,
172 .vop_print = devfs_vop_print,
173 .vop_kqfilter = devfs_spec_kqfilter,
174 .vop_read = devfs_spec_read,
175 .vop_readdir = DEVFS_BADOP,
176 .vop_readlink = DEVFS_BADOP,
177 .vop_reallocblks = DEVFS_BADOP,
178 .vop_reclaim = devfs_vop_reclaim,
179 .vop_setattr = devfs_vop_setattr,
180 .vop_strategy = devfs_spec_strategy,
181 .vop_write = devfs_spec_write,
182 .vop_ioctl = devfs_spec_ioctl
186 * devfs file pointer operations. All fileops are MPSAFE.
188 struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops;
190 struct fileops devfs_dev_fileops = {
191 .fo_read = devfs_fo_read,
192 .fo_write = devfs_fo_write,
193 .fo_ioctl = devfs_fo_ioctl,
194 .fo_kqfilter = devfs_fo_kqfilter,
195 .fo_stat = devfs_fo_stat,
196 .fo_close = devfs_fo_close,
197 .fo_shutdown = nofo_shutdown
201 * These two functions are possibly temporary hacks for devices (aka
202 * the pty code) which want to control the node attributes themselves.
204 * XXX we may ultimately desire to simply remove the uid/gid/mode
205 * from the node entirely.
207 * MPSAFE - sorta. Theoretically the overwrite can compete since they
208 * are loading from the same fields.
211 node_sync_dev_get(struct devfs_node *node)
215 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
216 node->uid = dev->si_uid;
217 node->gid = dev->si_gid;
218 node->mode = dev->si_perms;
223 node_sync_dev_set(struct devfs_node *node)
227 if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
228 dev->si_uid = node->uid;
229 dev->si_gid = node->gid;
230 dev->si_perms = node->mode;
235 * generic entry point for unsupported operations
238 devfs_vop_badop(struct vop_generic_args *ap)
245 devfs_vop_access(struct vop_access_args *ap)
247 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
250 if (!devfs_node_is_accessible(node))
252 node_sync_dev_get(node);
253 error = vop_helper_access(ap, node->uid, node->gid,
254 node->mode, node->flags);
261 devfs_vop_inactive(struct vop_inactive_args *ap)
263 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
265 if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0)
272 devfs_vop_reclaim(struct vop_reclaim_args *ap)
274 struct devfs_node *node;
279 * Check if it is locked already. if not, we acquire the devfs lock
281 if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) {
282 lockmgr(&devfs_lock, LK_EXCLUSIVE);
289 * Get rid of the devfs_node if it is no longer linked into the
290 * topology. Interlocked by devfs_lock. However, be careful
291 * interposing other operations between cleaning out v_data and
292 * devfs_freep() as the node is only protected by devfs_lock
293 * once the vnode is disassociated.
296 node = DEVFS_NODE(vp);
299 if (node->v_node != vp) {
300 kprintf("NODE->V_NODE MISMATCH VP=%p NODEVP=%p\n",
305 if ((node->flags & DEVFS_NODE_LINKED) == 0)
311 lockmgr(&devfs_lock, LK_RELEASE);
314 * v_rdev needs to be properly released using v_release_rdev
315 * Make sure v_data is NULL as well.
322 devfs_vop_readdir(struct vop_readdir_args *ap)
324 struct devfs_node *dnode = DEVFS_NODE(ap->a_vp);
325 struct devfs_node *node;
334 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
336 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
338 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
342 if (!devfs_node_is_accessible(dnode)) {
347 lockmgr(&devfs_lock, LK_EXCLUSIVE);
349 saveoff = ap->a_uio->uio_offset;
351 if (ap->a_ncookies) {
352 ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
355 cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
363 nanotime(&dnode->atime);
366 r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino,
371 cookies[cookie_index] = saveoff;
374 if (cookie_index == ncookies)
380 r = vop_write_dirent(&error, ap->a_uio,
381 dnode->parent->d_dir.d_ino,
384 r = vop_write_dirent(&error, ap->a_uio,
391 cookies[cookie_index] = saveoff;
394 if (cookie_index == ncookies)
398 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
399 if ((node->flags & DEVFS_HIDDEN) ||
400 (node->flags & DEVFS_INVISIBLE)) {
405 * If the node type is a valid devfs alias, then we make
406 * sure that the target isn't hidden. If it is, we don't
407 * show the link in the directory listing.
409 if ((node->node_type == Nlink) && (node->link_target != NULL) &&
410 (node->link_target->flags & DEVFS_HIDDEN))
413 if (node->cookie < saveoff)
416 saveoff = node->cookie;
418 error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino,
420 node->d_dir.d_namlen,
429 cookies[cookie_index] = node->cookie;
431 if (cookie_index == ncookies)
436 lockmgr(&devfs_lock, LK_RELEASE);
439 ap->a_uio->uio_offset = saveoff;
440 if (error && cookie_index == 0) {
442 kfree(cookies, M_TEMP);
444 *ap->a_cookies = NULL;
448 *ap->a_ncookies = cookie_index;
449 *ap->a_cookies = cookies;
457 devfs_vop_nresolve(struct vop_nresolve_args *ap)
459 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
460 struct devfs_node *node, *found = NULL;
461 struct namecache *ncp;
462 struct vnode *vp = NULL;
467 ncp = ap->a_nch->ncp;
470 if (!devfs_node_is_accessible(dnode))
473 lockmgr(&devfs_lock, LK_EXCLUSIVE);
475 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) {
477 cache_setvp(ap->a_nch, NULL);
481 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
482 if (len == node->d_dir.d_namlen) {
483 if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
492 while ((found->node_type == Nlink) && (found->link_target)) {
494 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
498 found = found->link_target;
502 if (!(found->flags & DEVFS_HIDDEN))
503 devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
508 cache_setvp(ap->a_nch, NULL);
514 cache_setvp(ap->a_nch, vp);
517 lockmgr(&devfs_lock, LK_RELEASE);
524 devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
526 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
529 if (!devfs_node_is_accessible(dnode))
532 lockmgr(&devfs_lock, LK_EXCLUSIVE);
533 if (dnode->parent != NULL) {
534 devfs_allocv(ap->a_vpp, dnode->parent);
535 vn_unlock(*ap->a_vpp);
537 lockmgr(&devfs_lock, LK_RELEASE);
539 return ((*ap->a_vpp == NULL) ? ENOENT : 0);
544 * getattr() - Does not need a lock since the vp is refd
547 devfs_vop_getattr(struct vop_getattr_args *ap)
549 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
550 struct vattr *vap = ap->a_vap;
551 struct partinfo pinfo;
555 if (!devfs_node_is_accessible(node))
560 * XXX This is a temporary hack to prevent crashes when the device is
561 * being destroyed (and so the underlying node will be gone) while
562 * a userland program is blocked in a read().
567 node_sync_dev_get(node);
569 /* start by zeroing out the attributes */
572 /* next do all the common fields */
573 vap->va_type = ap->a_vp->v_type;
574 vap->va_mode = node->mode;
575 vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
577 vap->va_blocksize = DEV_BSIZE;
578 vap->va_bytes = vap->va_size = 0;
580 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
582 vap->va_atime = node->atime;
583 vap->va_mtime = node->mtime;
584 vap->va_ctime = node->ctime;
586 vap->va_nlink = 1; /* number of references to file */
588 vap->va_uid = node->uid;
589 vap->va_gid = node->gid;
594 if ((node->node_type == Ndev) && node->d_dev) {
595 reference_dev(node->d_dev);
596 vap->va_rminor = node->d_dev->si_uminor;
597 release_dev(node->d_dev);
600 /* For a softlink the va_size is the length of the softlink */
601 if (node->symlink_name != 0) {
602 vap->va_bytes = vap->va_size = node->symlink_namelen;
606 * For a disk-type device, va_size is the size of the underlying
607 * device, so that lseek() works properly.
609 if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) {
610 bzero(&pinfo, sizeof(pinfo));
611 error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo,
612 0, proc0.p_ucred, NULL, NULL);
613 if ((error == 0) && (pinfo.media_blksize != 0)) {
614 vap->va_size = pinfo.media_size;
625 devfs_vop_setattr(struct vop_setattr_args *ap)
627 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
634 if (!devfs_node_is_accessible(node))
636 node_sync_dev_get(node);
638 lockmgr(&devfs_lock, LK_EXCLUSIVE);
642 if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) {
645 cur_mode = node->mode;
646 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
647 ap->a_cred, &cur_uid, &cur_gid, &cur_mode);
651 if (node->uid != cur_uid || node->gid != cur_gid) {
654 node->mode = cur_mode;
658 if (vap->va_mode != (mode_t)VNOVAL) {
659 cur_mode = node->mode;
660 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
661 node->uid, node->gid, &cur_mode);
662 if (error == 0 && node->mode != cur_mode) {
663 node->mode = cur_mode;
668 node_sync_dev_set(node);
669 nanotime(&node->ctime);
670 lockmgr(&devfs_lock, LK_RELEASE);
677 devfs_vop_readlink(struct vop_readlink_args *ap)
679 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
682 if (!devfs_node_is_accessible(node))
685 lockmgr(&devfs_lock, LK_SHARED);
686 ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
687 lockmgr(&devfs_lock, LK_RELEASE);
694 devfs_vop_print(struct vop_print_args *ap)
700 devfs_vop_nmkdir(struct vop_nmkdir_args *ap)
702 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
703 struct devfs_node *node;
705 if (!devfs_node_is_accessible(dnode))
708 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
711 lockmgr(&devfs_lock, LK_EXCLUSIVE);
712 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Ndir,
713 ap->a_nch->ncp->nc_name, dnode, NULL);
716 node = DEVFS_NODE(*ap->a_vpp);
717 node->flags |= DEVFS_USER_CREATED;
718 cache_setunresolved(ap->a_nch);
719 cache_setvp(ap->a_nch, *ap->a_vpp);
721 lockmgr(&devfs_lock, LK_RELEASE);
723 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
727 devfs_vop_nsymlink(struct vop_nsymlink_args *ap)
729 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
730 struct devfs_node *node;
733 if (!devfs_node_is_accessible(dnode))
736 ap->a_vap->va_type = VLNK;
738 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
741 lockmgr(&devfs_lock, LK_EXCLUSIVE);
742 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Nlink,
743 ap->a_nch->ncp->nc_name, dnode, NULL);
745 targetlen = strlen(ap->a_target);
747 node = DEVFS_NODE(*ap->a_vpp);
748 node->flags |= DEVFS_USER_CREATED;
749 node->symlink_namelen = targetlen;
750 node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
751 memcpy(node->symlink_name, ap->a_target, targetlen);
752 node->symlink_name[targetlen] = '\0';
753 cache_setunresolved(ap->a_nch);
754 cache_setvp(ap->a_nch, *ap->a_vpp);
756 lockmgr(&devfs_lock, LK_RELEASE);
758 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
762 devfs_vop_nrmdir(struct vop_nrmdir_args *ap)
764 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
765 struct devfs_node *node;
766 struct namecache *ncp;
769 ncp = ap->a_nch->ncp;
771 if (!devfs_node_is_accessible(dnode))
774 lockmgr(&devfs_lock, LK_EXCLUSIVE);
776 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
779 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
780 if (ncp->nc_nlen != node->d_dir.d_namlen)
782 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
786 * only allow removal of user created dirs
788 if ((node->flags & DEVFS_USER_CREATED) == 0) {
791 } else if (node->node_type != Ndir) {
794 } else if (node->nchildren > 2) {
799 cache_inval_vp(node->v_node, CINV_DESTROY);
806 cache_unlink(ap->a_nch);
808 lockmgr(&devfs_lock, LK_RELEASE);
813 devfs_vop_nremove(struct vop_nremove_args *ap)
815 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
816 struct devfs_node *node;
817 struct namecache *ncp;
820 ncp = ap->a_nch->ncp;
822 if (!devfs_node_is_accessible(dnode))
825 lockmgr(&devfs_lock, LK_EXCLUSIVE);
827 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
830 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
831 if (ncp->nc_nlen != node->d_dir.d_namlen)
833 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
837 * only allow removal of user created stuff (e.g. symlinks)
839 if ((node->flags & DEVFS_USER_CREATED) == 0) {
842 } else if (node->node_type == Ndir) {
847 cache_inval_vp(node->v_node, CINV_DESTROY);
854 cache_unlink(ap->a_nch);
856 lockmgr(&devfs_lock, LK_RELEASE);
862 devfs_spec_open(struct vop_open_args *ap)
864 struct vnode *vp = ap->a_vp;
865 struct vnode *orig_vp = NULL;
866 struct devfs_node *node = DEVFS_NODE(vp);
867 struct devfs_node *newnode;
868 cdev_t dev, ndev = NULL;
872 if (node->d_dev == NULL)
874 if (!devfs_node_is_accessible(node))
878 if ((dev = vp->v_rdev) == NULL)
882 * Simple devices that don't care. Retain the shared lock.
884 if (dev_dflags(dev) & D_QUICK) {
886 error = dev_dopen(dev, ap->a_mode, S_IFCHR,
887 ap->a_cred, ap->a_fp);
888 vn_lock(vp, LK_SHARED | LK_RETRY);
896 vn_lock(vp, LK_UPGRADE | LK_RETRY);
897 if (node && ap->a_fp) {
900 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
901 lockmgr(&devfs_lock, LK_EXCLUSIVE);
903 ndev = devfs_clone(dev, node->d_dir.d_name,
904 node->d_dir.d_namlen,
905 ap->a_mode, ap->a_cred);
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
969 error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred, ap->a_fp);
970 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
973 * Clean up any cloned vp if we error out.
979 /* orig_vp = NULL; */
985 * This checks if the disk device is going to be opened for writing.
986 * It will be only allowed in the cases where securelevel permits it
987 * and it's not mounted R/W.
989 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
990 (ap->a_cred != FSCRED)) {
992 /* Very secure mode. No open for writing allowed */
993 if (securelevel >= 2)
997 * If it is mounted R/W, do not allow to open for writing.
998 * In the case it's mounted read-only but securelevel
999 * is >= 1, then do not allow opening for writing either.
1001 if (vfs_mountedon(vp)) {
1002 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
1004 else if (securelevel >= 1)
1010 * NOTE: vnode is still locked shared. t_stop assignment should
1011 * remain consistent so we should be ok.
1013 if (dev_dflags(dev) & D_TTY) {
1018 devfs_debug(DEVFS_DEBUG_DEBUG,
1019 "devfs: no t_stop\n");
1020 tp->t_stop = nottystop;
1026 * NOTE: vnode is still locked shared. assignments should
1027 * remain consistent so we should be ok. However,
1028 * upgrade to exclusive if we need a VM object.
1030 if (vn_isdisk(vp, NULL)) {
1031 if (!dev->si_bsize_phys)
1032 dev->si_bsize_phys = DEV_BSIZE;
1033 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
1039 nanotime(&node->atime);
1042 * If we replaced the vp the vop_stdopen() call will have loaded
1043 * it into fp->f_data and vref()d the vp, giving us two refs. So
1044 * instead of just unlocking it here we have to vput() it.
1049 /* Ugly pty magic, to make pty devices appear once they are opened */
1050 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY) {
1051 if (node->flags & DEVFS_INVISIBLE)
1052 node->flags &= ~DEVFS_INVISIBLE;
1057 KKASSERT(ap->a_fp->f_type == DTYPE_VNODE);
1058 KKASSERT((ap->a_fp->f_flag & FMASK) == (ap->a_mode & FMASK));
1059 ap->a_fp->f_ops = &devfs_dev_fileops;
1060 KKASSERT(ap->a_fp->f_data == (void *)vp);
1067 devfs_spec_close(struct vop_close_args *ap)
1069 struct devfs_node *node;
1070 struct proc *p = curproc;
1071 struct vnode *vp = ap->a_vp;
1072 cdev_t dev = vp->v_rdev;
1078 * Devices flagged D_QUICK require no special handling.
1080 if (dev && dev_dflags(dev) & D_QUICK) {
1081 opencount = vp->v_opencount;
1083 opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1084 if (((vp->v_flag & VRECLAIMED) ||
1085 (dev_dflags(dev) & D_TRACKCLOSE) ||
1086 (opencount == 1))) {
1088 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1089 vn_lock(vp, LK_SHARED | LK_RETRY);
1095 * We do special tests on the opencount so unfortunately we need
1096 * an exclusive lock.
1098 vn_lock(vp, LK_UPGRADE | LK_RETRY);
1101 devfs_debug(DEVFS_DEBUG_DEBUG,
1102 "devfs_spec_close() called on %s! \n",
1105 devfs_debug(DEVFS_DEBUG_DEBUG,
1106 "devfs_spec_close() called, null vode!\n");
1109 * A couple of hacks for devices and tty devices. The
1110 * vnode ref count cannot be used to figure out the
1111 * last close, but we can use v_opencount now that
1112 * revoke works properly.
1114 * Detect the last close on a controlling terminal and clear
1115 * the session (half-close).
1117 * XXX opencount is not SMP safe. The vnode is locked but there
1118 * may be multiple vnodes referencing the same device.
1122 * NOTE: Try to avoid global tokens when testing opencount
1123 * XXX hack, fixme. needs a struct lock and opencount in
1124 * struct cdev itself.
1127 opencount = vp->v_opencount;
1129 opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1134 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
1135 p->p_session->s_ttyvp = NULL;
1140 * Vnodes can be opened and closed multiple times. Do not really
1141 * close the device unless (1) it is being closed forcibly,
1142 * (2) the device wants to track closes, or (3) this is the last
1143 * vnode doing its last close on the device.
1145 * XXX the VXLOCK (force close) case can leave vnodes referencing
1146 * a closed device. This might not occur now that our revoke is
1149 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
1150 if (dev && ((vp->v_flag & VRECLAIMED) ||
1151 (dev_dflags(dev) & D_TRACKCLOSE) ||
1152 (opencount == 1))) {
1154 * Ugly pty magic, to make pty devices disappear again once
1157 node = DEVFS_NODE(ap->a_vp);
1158 if (node && (node->flags & DEVFS_PTY))
1159 node->flags |= DEVFS_INVISIBLE;
1162 * Unlock around dev_dclose(), unless the vnode is
1163 * undergoing a vgone/reclaim (during umount).
1166 if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) {
1172 * WARNING! If the device destroys itself the devfs node
1173 * can disappear here.
1175 * WARNING! vn_lock() will fail if the vp is in a VRECLAIM,
1176 * which can occur during umount.
1178 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1179 /* node is now stale */
1182 if (vn_lock(vp, LK_EXCLUSIVE |
1184 LK_FAILRECLAIM) != 0) {
1185 panic("devfs_spec_close: vnode %p "
1186 "unexpectedly could not be relocked",
1193 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
1196 * Track the actual opens and closes on the vnode. The last close
1197 * disassociates the rdev. If the rdev is already disassociated or
1198 * the opencount is already 0, the vnode might have been revoked
1199 * and no further opencount tracking occurs.
1204 if (vp->v_opencount > 0)
1212 devfs_fo_close(struct file *fp)
1214 struct vnode *vp = (struct vnode *)fp->f_data;
1217 fp->f_ops = &badfileops;
1218 error = vn_close(vp, fp->f_flag, fp);
1219 devfs_clear_cdevpriv(fp);
1226 * Device-optimized file table vnode read routine.
1228 * This bypasses the VOP table and talks directly to the device. Most
1229 * filesystems just route to specfs and can make this optimization.
1232 devfs_fo_read(struct file *fp, struct uio *uio,
1233 struct ucred *cred, int flags)
1235 struct devfs_node *node;
1241 KASSERT(uio->uio_td == curthread,
1242 ("uio_td %p is not td %p", uio->uio_td, curthread));
1244 if (uio->uio_resid == 0)
1247 vp = (struct vnode *)fp->f_data;
1248 if (vp == NULL || vp->v_type == VBAD)
1251 node = DEVFS_NODE(vp);
1253 if ((dev = vp->v_rdev) == NULL)
1258 if ((flags & O_FOFFSET) == 0)
1259 uio->uio_offset = fp->f_offset;
1262 if (flags & O_FBLOCKING) {
1263 /* ioflag &= ~IO_NDELAY; */
1264 } else if (flags & O_FNONBLOCKING) {
1265 ioflag |= IO_NDELAY;
1266 } else if (fp->f_flag & FNONBLOCK) {
1267 ioflag |= IO_NDELAY;
1269 if (fp->f_flag & O_DIRECT) {
1270 ioflag |= IO_DIRECT;
1272 ioflag |= sequential_heuristic(uio, fp);
1274 error = dev_dread(dev, uio, ioflag, fp);
1278 nanotime(&node->atime);
1279 if ((flags & O_FOFFSET) == 0)
1280 fp->f_offset = uio->uio_offset;
1281 fp->f_nextoff = uio->uio_offset;
1288 devfs_fo_write(struct file *fp, struct uio *uio,
1289 struct ucred *cred, int flags)
1291 struct devfs_node *node;
1297 KASSERT(uio->uio_td == curthread,
1298 ("uio_td %p is not p %p", uio->uio_td, curthread));
1300 vp = (struct vnode *)fp->f_data;
1301 if (vp == NULL || vp->v_type == VBAD)
1304 node = DEVFS_NODE(vp);
1306 if (vp->v_type == VREG)
1307 bwillwrite(uio->uio_resid);
1309 vp = (struct vnode *)fp->f_data;
1311 if ((dev = vp->v_rdev) == NULL)
1316 if ((flags & O_FOFFSET) == 0)
1317 uio->uio_offset = fp->f_offset;
1320 if (vp->v_type == VREG &&
1321 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1322 ioflag |= IO_APPEND;
1325 if (flags & O_FBLOCKING) {
1326 /* ioflag &= ~IO_NDELAY; */
1327 } else if (flags & O_FNONBLOCKING) {
1328 ioflag |= IO_NDELAY;
1329 } else if (fp->f_flag & FNONBLOCK) {
1330 ioflag |= IO_NDELAY;
1332 if (fp->f_flag & O_DIRECT) {
1333 ioflag |= IO_DIRECT;
1335 if (flags & O_FASYNCWRITE) {
1336 /* ioflag &= ~IO_SYNC; */
1337 } else if (flags & O_FSYNCWRITE) {
1339 } else if (fp->f_flag & O_FSYNC) {
1343 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1345 ioflag |= sequential_heuristic(uio, fp);
1347 error = dev_dwrite(dev, uio, ioflag, fp);
1351 nanotime(&node->atime);
1352 nanotime(&node->mtime);
1355 if ((flags & O_FOFFSET) == 0)
1356 fp->f_offset = uio->uio_offset;
1357 fp->f_nextoff = uio->uio_offset;
1364 devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
1373 vp = (struct vnode *)fp->f_data;
1374 if (vp == NULL || vp->v_type == VBAD)
1377 error = vn_stat(vp, sb, cred);
1382 error = VOP_GETATTR(vp, vap);
1387 * Zero the spare stat fields
1393 * Copy from vattr table ... or not in case it's a cloned device
1395 if (vap->va_fsid != VNOVAL)
1396 sb->st_dev = vap->va_fsid;
1398 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1400 sb->st_ino = vap->va_fileid;
1402 mode = vap->va_mode;
1406 if (vap->va_nlink > (nlink_t)-1)
1407 sb->st_nlink = (nlink_t)-1;
1409 sb->st_nlink = vap->va_nlink;
1411 sb->st_uid = vap->va_uid;
1412 sb->st_gid = vap->va_gid;
1413 sb->st_rdev = dev2udev(DEVFS_NODE(vp)->d_dev);
1414 sb->st_size = vap->va_bytes;
1415 sb->st_atimespec = vap->va_atime;
1416 sb->st_mtimespec = vap->va_mtime;
1417 sb->st_ctimespec = vap->va_ctime;
1420 * A VCHR and VBLK device may track the last access and last modified
1421 * time independantly of the filesystem. This is particularly true
1422 * because device read and write calls may bypass the filesystem.
1424 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1427 if (dev->si_lastread) {
1428 sb->st_atimespec.tv_sec = time_second +
1431 sb->st_atimespec.tv_nsec = 0;
1433 if (dev->si_lastwrite) {
1434 sb->st_atimespec.tv_sec = time_second +
1437 sb->st_atimespec.tv_nsec = 0;
1443 * According to www.opengroup.org, the meaning of st_blksize is
1444 * "a filesystem-specific preferred I/O block size for this
1445 * object. In some filesystem types, this may vary from file
1447 * Default to PAGE_SIZE after much discussion.
1450 sb->st_blksize = PAGE_SIZE;
1452 sb->st_flags = vap->va_flags;
1454 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1458 sb->st_gen = (u_int32_t)vap->va_gen;
1460 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1463 * This is for ABI compatibility <= 5.7 (for ABI change made in
1466 sb->__old_st_blksize = sb->st_blksize;
1473 devfs_fo_kqfilter(struct file *fp, struct knote *kn)
1479 vp = (struct vnode *)fp->f_data;
1480 if (vp == NULL || vp->v_type == VBAD) {
1484 if ((dev = vp->v_rdev) == NULL) {
1490 error = dev_dkqfilter(dev, kn, fp);
1499 devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
1500 struct ucred *ucred, struct sysmsg *msg)
1503 struct devfs_node *node;
1509 struct fiodname_args *name_args;
1513 vp = ((struct vnode *)fp->f_data);
1515 if ((dev = vp->v_rdev) == NULL)
1516 return EBADF; /* device was revoked */
1521 node = DEVFS_NODE(vp);
1524 devfs_debug(DEVFS_DEBUG_DEBUG,
1525 "devfs_fo_ioctl() called! for dev %s\n",
1528 if (com == FIODTYPE) {
1529 *(int *)data = dev_dflags(dev) & D_TYPEMASK;
1532 } else if (com == FIODNAME) {
1533 name_args = (struct fiodname_args *)data;
1534 name = dev->si_name;
1535 namlen = strlen(name) + 1;
1537 devfs_debug(DEVFS_DEBUG_DEBUG,
1538 "ioctl, got: FIODNAME for %s\n", name);
1540 if (namlen <= name_args->len)
1541 error = copyout(dev->si_name, name_args->name, namlen);
1545 devfs_debug(DEVFS_DEBUG_DEBUG,
1546 "ioctl stuff: error: %d\n", error);
1550 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp);
1554 nanotime(&node->atime);
1555 nanotime(&node->mtime);
1558 if (com == TIOCSCTTY) {
1559 devfs_debug(DEVFS_DEBUG_DEBUG,
1560 "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
1563 if (error == 0 && com == TIOCSCTTY) {
1564 struct proc *p = curthread->td_proc;
1565 struct session *sess;
1567 devfs_debug(DEVFS_DEBUG_DEBUG,
1568 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
1574 sess = p->p_session;
1577 * Do nothing if reassigning same control tty
1579 if (sess->s_ttyvp == vp) {
1585 * Get rid of reference to old control tty
1587 ovp = sess->s_ttyvp;
1596 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
1602 devfs_spec_fsync(struct vop_fsync_args *ap)
1604 struct vnode *vp = ap->a_vp;
1607 if (!vn_isdisk(vp, NULL))
1611 * Flush all dirty buffers associated with a block device.
1613 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1618 devfs_spec_read(struct vop_read_args *ap)
1620 struct devfs_node *node;
1629 node = DEVFS_NODE(vp);
1631 if (dev == NULL) /* device was revoked */
1633 if (uio->uio_resid == 0)
1637 error = dev_dread(dev, uio, ap->a_ioflag, NULL);
1638 vn_lock(vp, LK_SHARED | LK_RETRY);
1641 nanotime(&node->atime);
1647 * Vnode op for write
1649 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1650 * struct ucred *a_cred)
1653 devfs_spec_write(struct vop_write_args *ap)
1655 struct devfs_node *node;
1664 node = DEVFS_NODE(vp);
1666 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1668 if (dev == NULL) /* device was revoked */
1672 error = dev_dwrite(dev, uio, ap->a_ioflag, NULL);
1673 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1676 nanotime(&node->atime);
1677 nanotime(&node->mtime);
1684 * Device ioctl operation.
1686 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
1687 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
1690 devfs_spec_ioctl(struct vop_ioctl_args *ap)
1692 struct vnode *vp = ap->a_vp;
1694 struct devfs_node *node;
1698 if ((dev = vp->v_rdev) == NULL)
1699 return (EBADF); /* device was revoked */
1701 node = DEVFS_NODE(vp);
1704 nanotime(&node->atime);
1705 nanotime(&node->mtime);
1709 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
1710 ap->a_cred, ap->a_sysmsg, NULL));
1714 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1718 devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1720 struct vnode *vp = ap->a_vp;
1722 struct devfs_node *node;
1726 if ((dev = vp->v_rdev) == NULL)
1727 return (EBADF); /* device was revoked (EBADF) */
1729 node = DEVFS_NODE(vp);
1732 nanotime(&node->atime);
1735 return (dev_dkqfilter(dev, ap->a_kn, NULL));
1739 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1740 * calls are not limited to device DMA limits so we have to deal with the
1743 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1746 devfs_spec_strategy(struct vop_strategy_args *ap)
1748 struct bio *bio = ap->a_bio;
1749 struct buf *bp = bio->bio_buf;
1756 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1760 * Collect statistics on synchronous and asynchronous read
1761 * and write counts for disks that have associated filesystems.
1764 KKASSERT(vp->v_rdev != NULL); /* XXX */
1765 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1766 if (bp->b_cmd == BUF_CMD_READ) {
1767 if (bp->b_flags & BIO_SYNC)
1768 mp->mnt_stat.f_syncreads++;
1770 mp->mnt_stat.f_asyncreads++;
1772 if (bp->b_flags & BIO_SYNC)
1773 mp->mnt_stat.f_syncwrites++;
1775 mp->mnt_stat.f_asyncwrites++;
1780 * Device iosize limitations only apply to read and write. Shortcut
1781 * the I/O if it fits.
1783 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1784 devfs_debug(DEVFS_DEBUG_DEBUG,
1785 "%s: si_iosize_max not set!\n",
1786 dev_dname(vp->v_rdev));
1787 maxiosize = MAXPHYS;
1789 #if SPEC_CHAIN_DEBUG & 2
1792 if (bp->b_bcount <= maxiosize ||
1793 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1794 dev_dstrategy_chain(vp->v_rdev, bio);
1799 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1801 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1804 BUF_LOCK(nbp, LK_EXCLUSIVE);
1807 nbp->b_flags = B_PAGING | B_KVABIO | (bp->b_flags & B_BNOCLIP);
1808 nbp->b_cpumask = bp->b_cpumask;
1809 nbp->b_data = bp->b_data;
1810 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1811 nbp->b_bio1.bio_offset = bio->bio_offset;
1812 nbp->b_bio1.bio_caller_info1.ptr = bio;
1815 * Start the first transfer
1817 if (vn_isdisk(vp, NULL))
1818 chunksize = vp->v_rdev->si_bsize_phys;
1820 chunksize = DEV_BSIZE;
1821 chunksize = maxiosize / chunksize * chunksize;
1822 #if SPEC_CHAIN_DEBUG & 1
1823 devfs_debug(DEVFS_DEBUG_DEBUG,
1824 "spec_strategy chained I/O chunksize=%d\n",
1827 nbp->b_cmd = bp->b_cmd;
1828 nbp->b_bcount = chunksize;
1829 nbp->b_bufsize = chunksize; /* used to detect a short I/O */
1830 nbp->b_bio1.bio_caller_info2.index = chunksize;
1832 #if SPEC_CHAIN_DEBUG & 1
1833 devfs_debug(DEVFS_DEBUG_DEBUG,
1834 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1835 bp, 0, bp->b_bcount, nbp->b_bcount);
1838 dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1840 if (DEVFS_NODE(vp)) {
1841 nanotime(&DEVFS_NODE(vp)->atime);
1842 nanotime(&DEVFS_NODE(vp)->mtime);
1849 * Chunked up transfer completion routine - chain transfers until done
1851 * NOTE: MPSAFE callback.
1855 devfs_spec_strategy_done(struct bio *nbio)
1857 struct buf *nbp = nbio->bio_buf;
1858 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
1859 struct buf *bp = bio->bio_buf; /* original bp */
1860 int chunksize = nbio->bio_caller_info2.index; /* chunking */
1861 int boffset = nbp->b_data - bp->b_data;
1863 if (nbp->b_flags & B_ERROR) {
1865 * An error terminates the chain, propogate the error back
1866 * to the original bp
1868 bp->b_flags |= B_ERROR;
1869 bp->b_error = nbp->b_error;
1870 bp->b_resid = bp->b_bcount - boffset +
1871 (nbp->b_bcount - nbp->b_resid);
1872 #if SPEC_CHAIN_DEBUG & 1
1873 devfs_debug(DEVFS_DEBUG_DEBUG,
1874 "spec_strategy: chain %p error %d bcount %d/%d\n",
1875 bp, bp->b_error, bp->b_bcount,
1876 bp->b_bcount - bp->b_resid);
1878 } else if (nbp->b_resid) {
1880 * A short read or write terminates the chain
1882 bp->b_error = nbp->b_error;
1883 bp->b_resid = bp->b_bcount - boffset +
1884 (nbp->b_bcount - nbp->b_resid);
1885 #if SPEC_CHAIN_DEBUG & 1
1886 devfs_debug(DEVFS_DEBUG_DEBUG,
1887 "spec_strategy: chain %p short read(1) "
1889 bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
1891 } else if (nbp->b_bcount != nbp->b_bufsize) {
1893 * A short read or write can also occur by truncating b_bcount
1895 #if SPEC_CHAIN_DEBUG & 1
1896 devfs_debug(DEVFS_DEBUG_DEBUG,
1897 "spec_strategy: chain %p short read(2) "
1899 bp, nbp->b_bcount + boffset, bp->b_bcount);
1902 bp->b_bcount = nbp->b_bcount + boffset;
1903 bp->b_resid = nbp->b_resid;
1904 } else if (nbp->b_bcount + boffset == bp->b_bcount) {
1906 * No more data terminates the chain
1908 #if SPEC_CHAIN_DEBUG & 1
1909 devfs_debug(DEVFS_DEBUG_DEBUG,
1910 "spec_strategy: chain %p finished bcount %d\n",
1917 * Continue the chain
1919 boffset += nbp->b_bcount;
1920 nbp->b_data = bp->b_data + boffset;
1921 nbp->b_bcount = bp->b_bcount - boffset;
1922 if (nbp->b_bcount > chunksize)
1923 nbp->b_bcount = chunksize;
1924 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1925 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1927 #if SPEC_CHAIN_DEBUG & 1
1928 devfs_debug(DEVFS_DEBUG_DEBUG,
1929 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1930 bp, boffset, bp->b_bcount, nbp->b_bcount);
1933 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
1938 * Fall through to here on termination. biodone(bp) and
1939 * clean up and free nbp.
1944 kfree(nbp, M_DEVBUF);
1948 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1951 devfs_spec_freeblks(struct vop_freeblks_args *ap)
1956 * Must be a synchronous operation
1958 KKASSERT(ap->a_vp->v_rdev != NULL);
1959 if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0)
1962 bp->b_cmd = BUF_CMD_FREEBLKS;
1963 bp->b_bio1.bio_flags |= BIO_SYNC;
1964 bp->b_bio1.bio_offset = ap->a_offset;
1965 bp->b_bio1.bio_done = biodone_sync;
1966 bp->b_bcount = ap->a_length;
1967 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1968 biowait(&bp->b_bio1, "TRIM");
1975 * Implement degenerate case where the block requested is the block
1976 * returned, and assume that the entire device is contiguous in regards
1977 * to the contiguous block range (runp and runb).
1979 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1980 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1983 devfs_spec_bmap(struct vop_bmap_args *ap)
1985 if (ap->a_doffsetp != NULL)
1986 *ap->a_doffsetp = ap->a_loffset;
1987 if (ap->a_runp != NULL)
1988 *ap->a_runp = MAXBSIZE;
1989 if (ap->a_runb != NULL) {
1990 if (ap->a_loffset < MAXBSIZE)
1991 *ap->a_runb = (int)ap->a_loffset;
1993 *ap->a_runb = MAXBSIZE;
2000 * Special device advisory byte-level locks.
2002 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
2003 * struct flock *a_fl, int a_flags)
2007 devfs_spec_advlock(struct vop_advlock_args *ap)
2009 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
2013 * NOTE: MPSAFE callback.
2016 devfs_spec_getpages_iodone(struct bio *bio)
2018 bio->bio_buf->b_cmd = BUF_CMD_DONE;
2019 wakeup(bio->bio_buf);
2023 * spec_getpages() - get pages associated with device vnode.
2025 * Note that spec_read and spec_write do not use the buffer cache, so we
2026 * must fully implement getpages here.
2029 devfs_spec_getpages(struct vop_getpages_args *ap)
2033 int i, pcount, size;
2036 vm_ooffset_t offset;
2037 int toff, nextoff, nread;
2038 struct vnode *vp = ap->a_vp;
2043 pcount = round_page(ap->a_count) / PAGE_SIZE;
2046 * Calculate the offset of the transfer and do sanity check.
2048 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
2051 * Round up physical size for real devices. We cannot round using
2052 * v_mount's block size data because v_mount has nothing to do with
2053 * the device. i.e. it's usually '/dev'. We need the physical block
2054 * size for the device itself.
2056 * We can't use v_rdev->si_mountpoint because it only exists when the
2057 * block device is mounted. However, we can use v_rdev.
2059 if (vn_isdisk(vp, NULL))
2060 blksiz = vp->v_rdev->si_bsize_phys;
2064 size = roundup2(ap->a_count, blksiz);
2066 bp = getpbuf_kva(NULL);
2067 kva = (vm_offset_t)bp->b_data;
2070 * Map the pages to be read into the kva.
2072 pmap_qenter_noinval(kva, ap->a_m, pcount);
2074 /* Build a minimal buffer header. */
2075 bp->b_cmd = BUF_CMD_READ;
2076 bp->b_flags |= B_KVABIO;
2077 bp->b_bcount = size;
2079 bsetrunningbufspace(bp, size);
2081 bp->b_bio1.bio_offset = offset;
2082 bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
2084 mycpu->gd_cnt.v_vnodein++;
2085 mycpu->gd_cnt.v_vnodepgsin += pcount;
2088 vn_strategy(ap->a_vp, &bp->b_bio1);
2092 /* We definitely need to be at splbio here. */
2093 while (bp->b_cmd != BUF_CMD_DONE)
2094 tsleep(bp, 0, "spread", 0);
2098 if (bp->b_flags & B_ERROR) {
2100 error = bp->b_error;
2106 * If EOF is encountered we must zero-extend the result in order
2107 * to ensure that the page does not contain garabge. When no
2108 * error occurs, an early EOF is indicated if b_bcount got truncated.
2109 * b_resid is relative to b_bcount and should be 0, but some devices
2110 * might indicate an EOF with b_resid instead of truncating b_bcount.
2112 nread = bp->b_bcount - bp->b_resid;
2113 if (nread < ap->a_count) {
2115 bzero((caddr_t)kva + nread, ap->a_count - nread);
2117 pmap_qremove_noinval(kva, pcount);
2120 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
2121 nextoff = toff + PAGE_SIZE;
2125 * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2126 * pmap modified bit. pmap modified bit should have
2127 * already been cleared.
2129 if (nextoff <= nread) {
2130 m->valid = VM_PAGE_BITS_ALL;
2132 } else if (toff < nread) {
2134 * Since this is a VM request, we have to supply the
2135 * unaligned offset to allow vm_page_set_valid()
2136 * to zero sub-DEV_BSIZE'd portions of the page.
2138 vm_page_set_valid(m, 0, nread - toff);
2139 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
2145 if (i != ap->a_reqpage) {
2147 * Just in case someone was asking for this page we
2148 * now tell them that it is ok to use.
2150 if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
2152 if (m->flags & PG_REFERENCED) {
2153 vm_page_activate(m);
2155 vm_page_deactivate(m);
2164 } else if (m->valid) {
2167 * Since this is a VM request, we need to make the
2168 * entire page presentable by zeroing invalid sections.
2170 if (m->valid != VM_PAGE_BITS_ALL)
2171 vm_page_zero_invalid(m, FALSE);
2175 m = ap->a_m[ap->a_reqpage];
2176 devfs_debug(DEVFS_DEBUG_WARNING,
2177 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
2178 devtoname(vp->v_rdev), error, bp, bp->b_vp);
2179 devfs_debug(DEVFS_DEBUG_WARNING,
2180 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
2181 size, bp->b_resid, ap->a_count, m->valid);
2182 devfs_debug(DEVFS_DEBUG_WARNING,
2183 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
2184 nread, ap->a_reqpage, (u_long)m->pindex, pcount);
2186 * Free the buffer header back to the swap buffer pool.
2189 return VM_PAGER_ERROR;
2192 * Free the buffer header back to the swap buffer pool.
2195 if (DEVFS_NODE(ap->a_vp))
2196 nanotime(&DEVFS_NODE(ap->a_vp)->mtime);
2202 sequential_heuristic(struct uio *uio, struct file *fp)
2205 * Sequential heuristic - detect sequential operation
2207 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2208 uio->uio_offset == fp->f_nextoff) {
2210 * XXX we assume that the filesystem block size is
2211 * the default. Not true, but still gives us a pretty
2212 * good indicator of how sequential the read operations
2215 int tmpseq = fp->f_seqcount;
2217 tmpseq += (uio->uio_resid + MAXBSIZE - 1) / MAXBSIZE;
2218 if (tmpseq > IO_SEQMAX)
2220 fp->f_seqcount = tmpseq;
2221 return(fp->f_seqcount << IO_SEQSHIFT);
2225 * Not sequential, quick draw-down of seqcount
2227 if (fp->f_seqcount > 1)