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 <sys/sysref2.h>
70 #include <sys/mplock2.h>
71 #include <vm/vm_page2.h>
73 #ifndef SPEC_CHAIN_DEBUG
74 #define SPEC_CHAIN_DEBUG 0
77 MALLOC_DECLARE(M_DEVFS);
78 #define DEVFS_BADOP (void *)devfs_vop_badop
80 static int devfs_vop_badop(struct vop_generic_args *);
81 static int devfs_vop_access(struct vop_access_args *);
82 static int devfs_vop_inactive(struct vop_inactive_args *);
83 static int devfs_vop_reclaim(struct vop_reclaim_args *);
84 static int devfs_vop_readdir(struct vop_readdir_args *);
85 static int devfs_vop_getattr(struct vop_getattr_args *);
86 static int devfs_vop_setattr(struct vop_setattr_args *);
87 static int devfs_vop_readlink(struct vop_readlink_args *);
88 static int devfs_vop_print(struct vop_print_args *);
90 static int devfs_vop_nresolve(struct vop_nresolve_args *);
91 static int devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
92 static int devfs_vop_nmkdir(struct vop_nmkdir_args *);
93 static int devfs_vop_nsymlink(struct vop_nsymlink_args *);
94 static int devfs_vop_nrmdir(struct vop_nrmdir_args *);
95 static int devfs_vop_nremove(struct vop_nremove_args *);
97 static int devfs_spec_open(struct vop_open_args *);
98 static int devfs_spec_close(struct vop_close_args *);
99 static int devfs_spec_fsync(struct vop_fsync_args *);
101 static int devfs_spec_read(struct vop_read_args *);
102 static int devfs_spec_write(struct vop_write_args *);
103 static int devfs_spec_ioctl(struct vop_ioctl_args *);
104 static int devfs_spec_kqfilter(struct vop_kqfilter_args *);
105 static int devfs_spec_strategy(struct vop_strategy_args *);
106 static void devfs_spec_strategy_done(struct bio *);
107 static int devfs_spec_freeblks(struct vop_freeblks_args *);
108 static int devfs_spec_bmap(struct vop_bmap_args *);
109 static int devfs_spec_advlock(struct vop_advlock_args *);
110 static void devfs_spec_getpages_iodone(struct bio *);
111 static int devfs_spec_getpages(struct vop_getpages_args *);
113 static int devfs_fo_close(struct file *);
114 static int devfs_fo_read(struct file *, struct uio *, struct ucred *, int);
115 static int devfs_fo_write(struct file *, struct uio *, struct ucred *, int);
116 static int devfs_fo_stat(struct file *, struct stat *, struct ucred *);
117 static int devfs_fo_kqfilter(struct file *, struct knote *);
118 static int devfs_fo_ioctl(struct file *, u_long, caddr_t,
119 struct ucred *, struct sysmsg *);
120 static __inline int sequential_heuristic(struct uio *, struct file *);
122 extern struct lock devfs_lock;
125 * devfs vnode operations for regular files. All vnode ops are MPSAFE.
127 struct vop_ops devfs_vnode_norm_vops = {
128 .vop_default = vop_defaultop,
129 .vop_access = devfs_vop_access,
130 .vop_advlock = DEVFS_BADOP,
131 .vop_bmap = DEVFS_BADOP,
132 .vop_close = vop_stdclose,
133 .vop_getattr = devfs_vop_getattr,
134 .vop_inactive = devfs_vop_inactive,
135 .vop_ncreate = DEVFS_BADOP,
136 .vop_nresolve = devfs_vop_nresolve,
137 .vop_nlookupdotdot = devfs_vop_nlookupdotdot,
138 .vop_nlink = DEVFS_BADOP,
139 .vop_nmkdir = devfs_vop_nmkdir,
140 .vop_nmknod = DEVFS_BADOP,
141 .vop_nremove = devfs_vop_nremove,
142 .vop_nrename = DEVFS_BADOP,
143 .vop_nrmdir = devfs_vop_nrmdir,
144 .vop_nsymlink = devfs_vop_nsymlink,
145 .vop_open = vop_stdopen,
146 .vop_pathconf = vop_stdpathconf,
147 .vop_print = devfs_vop_print,
148 .vop_read = DEVFS_BADOP,
149 .vop_readdir = devfs_vop_readdir,
150 .vop_readlink = devfs_vop_readlink,
151 .vop_reclaim = devfs_vop_reclaim,
152 .vop_setattr = devfs_vop_setattr,
153 .vop_write = DEVFS_BADOP,
154 .vop_ioctl = DEVFS_BADOP
158 * devfs vnode operations for character devices. All vnode ops are MPSAFE.
160 struct vop_ops devfs_vnode_dev_vops = {
161 .vop_default = vop_defaultop,
162 .vop_access = devfs_vop_access,
163 .vop_advlock = devfs_spec_advlock,
164 .vop_bmap = devfs_spec_bmap,
165 .vop_close = devfs_spec_close,
166 .vop_freeblks = devfs_spec_freeblks,
167 .vop_fsync = devfs_spec_fsync,
168 .vop_getattr = devfs_vop_getattr,
169 .vop_getpages = devfs_spec_getpages,
170 .vop_inactive = devfs_vop_inactive,
171 .vop_open = devfs_spec_open,
172 .vop_pathconf = vop_stdpathconf,
173 .vop_print = devfs_vop_print,
174 .vop_kqfilter = devfs_spec_kqfilter,
175 .vop_read = devfs_spec_read,
176 .vop_readdir = DEVFS_BADOP,
177 .vop_readlink = 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
293 if ((node = DEVFS_NODE(vp)) != NULL) {
295 if ((node->flags & DEVFS_NODE_LINKED) == 0)
300 lockmgr(&devfs_lock, LK_RELEASE);
303 * v_rdev needs to be properly released using v_release_rdev
304 * Make sure v_data is NULL as well.
313 devfs_vop_readdir(struct vop_readdir_args *ap)
315 struct devfs_node *dnode = DEVFS_NODE(ap->a_vp);
316 struct devfs_node *node;
325 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
327 if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
329 error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
333 if (!devfs_node_is_accessible(dnode)) {
338 lockmgr(&devfs_lock, LK_EXCLUSIVE);
340 saveoff = ap->a_uio->uio_offset;
342 if (ap->a_ncookies) {
343 ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
346 cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
354 nanotime(&dnode->atime);
357 r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino,
362 cookies[cookie_index] = saveoff;
365 if (cookie_index == ncookies)
371 r = vop_write_dirent(&error, ap->a_uio,
372 dnode->parent->d_dir.d_ino,
375 r = vop_write_dirent(&error, ap->a_uio,
382 cookies[cookie_index] = saveoff;
385 if (cookie_index == ncookies)
389 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
390 if ((node->flags & DEVFS_HIDDEN) ||
391 (node->flags & DEVFS_INVISIBLE)) {
396 * If the node type is a valid devfs alias, then we make
397 * sure that the target isn't hidden. If it is, we don't
398 * show the link in the directory listing.
400 if ((node->node_type == Nlink) && (node->link_target != NULL) &&
401 (node->link_target->flags & DEVFS_HIDDEN))
404 if (node->cookie < saveoff)
407 saveoff = node->cookie;
409 error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino,
411 node->d_dir.d_namlen,
420 cookies[cookie_index] = node->cookie;
422 if (cookie_index == ncookies)
427 lockmgr(&devfs_lock, LK_RELEASE);
430 ap->a_uio->uio_offset = saveoff;
431 if (error && cookie_index == 0) {
433 kfree(cookies, M_TEMP);
435 *ap->a_cookies = NULL;
439 *ap->a_ncookies = cookie_index;
440 *ap->a_cookies = cookies;
448 devfs_vop_nresolve(struct vop_nresolve_args *ap)
450 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
451 struct devfs_node *node, *found = NULL;
452 struct namecache *ncp;
453 struct vnode *vp = NULL;
458 ncp = ap->a_nch->ncp;
461 if (!devfs_node_is_accessible(dnode))
464 lockmgr(&devfs_lock, LK_EXCLUSIVE);
466 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) {
468 cache_setvp(ap->a_nch, NULL);
472 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
473 if (len == node->d_dir.d_namlen) {
474 if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
483 while ((found->node_type == Nlink) && (found->link_target)) {
485 devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
489 found = found->link_target;
493 if (!(found->flags & DEVFS_HIDDEN))
494 devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
499 cache_setvp(ap->a_nch, NULL);
505 cache_setvp(ap->a_nch, vp);
508 lockmgr(&devfs_lock, LK_RELEASE);
515 devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
517 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
520 if (!devfs_node_is_accessible(dnode))
523 lockmgr(&devfs_lock, LK_EXCLUSIVE);
524 if (dnode->parent != NULL) {
525 devfs_allocv(ap->a_vpp, dnode->parent);
526 vn_unlock(*ap->a_vpp);
528 lockmgr(&devfs_lock, LK_RELEASE);
530 return ((*ap->a_vpp == NULL) ? ENOENT : 0);
535 devfs_vop_getattr(struct vop_getattr_args *ap)
537 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
538 struct vattr *vap = ap->a_vap;
539 struct partinfo pinfo;
543 if (!devfs_node_is_accessible(node))
546 node_sync_dev_get(node);
548 lockmgr(&devfs_lock, LK_EXCLUSIVE);
550 /* start by zeroing out the attributes */
553 /* next do all the common fields */
554 vap->va_type = ap->a_vp->v_type;
555 vap->va_mode = node->mode;
556 vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
558 vap->va_blocksize = DEV_BSIZE;
559 vap->va_bytes = vap->va_size = 0;
561 vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
563 vap->va_atime = node->atime;
564 vap->va_mtime = node->mtime;
565 vap->va_ctime = node->ctime;
567 vap->va_nlink = 1; /* number of references to file */
569 vap->va_uid = node->uid;
570 vap->va_gid = node->gid;
575 if ((node->node_type == Ndev) && node->d_dev) {
576 reference_dev(node->d_dev);
577 vap->va_rminor = node->d_dev->si_uminor;
578 release_dev(node->d_dev);
581 /* For a softlink the va_size is the length of the softlink */
582 if (node->symlink_name != 0) {
583 vap->va_bytes = vap->va_size = node->symlink_namelen;
587 * For a disk-type device, va_size is the size of the underlying
588 * device, so that lseek() works properly.
590 if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) {
591 bzero(&pinfo, sizeof(pinfo));
592 error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo,
593 0, proc0.p_ucred, NULL, NULL);
594 if ((error == 0) && (pinfo.media_blksize != 0)) {
595 vap->va_size = pinfo.media_size;
602 lockmgr(&devfs_lock, LK_RELEASE);
609 devfs_vop_setattr(struct vop_setattr_args *ap)
611 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
618 if (!devfs_node_is_accessible(node))
620 node_sync_dev_get(node);
622 lockmgr(&devfs_lock, LK_EXCLUSIVE);
626 if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) {
629 cur_mode = node->mode;
630 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
631 ap->a_cred, &cur_uid, &cur_gid, &cur_mode);
635 if (node->uid != cur_uid || node->gid != cur_gid) {
638 node->mode = cur_mode;
642 if (vap->va_mode != (mode_t)VNOVAL) {
643 cur_mode = node->mode;
644 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
645 node->uid, node->gid, &cur_mode);
646 if (error == 0 && node->mode != cur_mode) {
647 node->mode = cur_mode;
652 node_sync_dev_set(node);
653 nanotime(&node->ctime);
654 lockmgr(&devfs_lock, LK_RELEASE);
661 devfs_vop_readlink(struct vop_readlink_args *ap)
663 struct devfs_node *node = DEVFS_NODE(ap->a_vp);
666 if (!devfs_node_is_accessible(node))
669 lockmgr(&devfs_lock, LK_EXCLUSIVE);
670 ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
671 lockmgr(&devfs_lock, LK_RELEASE);
678 devfs_vop_print(struct vop_print_args *ap)
684 devfs_vop_nmkdir(struct vop_nmkdir_args *ap)
686 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
687 struct devfs_node *node;
689 if (!devfs_node_is_accessible(dnode))
692 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
695 lockmgr(&devfs_lock, LK_EXCLUSIVE);
696 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Ndir,
697 ap->a_nch->ncp->nc_name, dnode, NULL);
700 node = DEVFS_NODE(*ap->a_vpp);
701 node->flags |= DEVFS_USER_CREATED;
702 cache_setunresolved(ap->a_nch);
703 cache_setvp(ap->a_nch, *ap->a_vpp);
705 lockmgr(&devfs_lock, LK_RELEASE);
707 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
711 devfs_vop_nsymlink(struct vop_nsymlink_args *ap)
713 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
714 struct devfs_node *node;
717 if (!devfs_node_is_accessible(dnode))
720 ap->a_vap->va_type = VLNK;
722 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
725 lockmgr(&devfs_lock, LK_EXCLUSIVE);
726 devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Nlink,
727 ap->a_nch->ncp->nc_name, dnode, NULL);
729 targetlen = strlen(ap->a_target);
731 node = DEVFS_NODE(*ap->a_vpp);
732 node->flags |= DEVFS_USER_CREATED;
733 node->symlink_namelen = targetlen;
734 node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
735 memcpy(node->symlink_name, ap->a_target, targetlen);
736 node->symlink_name[targetlen] = '\0';
737 cache_setunresolved(ap->a_nch);
738 cache_setvp(ap->a_nch, *ap->a_vpp);
740 lockmgr(&devfs_lock, LK_RELEASE);
742 return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
746 devfs_vop_nrmdir(struct vop_nrmdir_args *ap)
748 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
749 struct devfs_node *node;
750 struct namecache *ncp;
753 ncp = ap->a_nch->ncp;
755 if (!devfs_node_is_accessible(dnode))
758 lockmgr(&devfs_lock, LK_EXCLUSIVE);
760 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
763 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
764 if (ncp->nc_nlen != node->d_dir.d_namlen)
766 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
770 * only allow removal of user created dirs
772 if ((node->flags & DEVFS_USER_CREATED) == 0) {
775 } else if (node->node_type != Ndir) {
778 } else if (node->nchildren > 2) {
783 cache_inval_vp(node->v_node, CINV_DESTROY);
790 cache_unlink(ap->a_nch);
792 lockmgr(&devfs_lock, LK_RELEASE);
797 devfs_vop_nremove(struct vop_nremove_args *ap)
799 struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
800 struct devfs_node *node;
801 struct namecache *ncp;
804 ncp = ap->a_nch->ncp;
806 if (!devfs_node_is_accessible(dnode))
809 lockmgr(&devfs_lock, LK_EXCLUSIVE);
811 if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
814 TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
815 if (ncp->nc_nlen != node->d_dir.d_namlen)
817 if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
821 * only allow removal of user created stuff (e.g. symlinks)
823 if ((node->flags & DEVFS_USER_CREATED) == 0) {
826 } else if (node->node_type == Ndir) {
831 cache_inval_vp(node->v_node, CINV_DESTROY);
838 cache_unlink(ap->a_nch);
840 lockmgr(&devfs_lock, LK_RELEASE);
846 devfs_spec_open(struct vop_open_args *ap)
848 struct vnode *vp = ap->a_vp;
849 struct vnode *orig_vp = NULL;
850 struct devfs_node *node = DEVFS_NODE(vp);
851 struct devfs_node *newnode;
852 cdev_t dev, ndev = NULL;
856 if (node->d_dev == NULL)
858 if (!devfs_node_is_accessible(node))
862 if ((dev = vp->v_rdev) == NULL)
865 vn_lock(vp, LK_UPGRADE | LK_RETRY);
867 if (node && ap->a_fp) {
868 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
869 lockmgr(&devfs_lock, LK_EXCLUSIVE);
871 ndev = devfs_clone(dev, node->d_dir.d_name,
872 node->d_dir.d_namlen,
873 ap->a_mode, ap->a_cred);
875 newnode = devfs_create_device_node(
876 DEVFS_MNTDATA(vp->v_mount)->root_node,
878 /* XXX: possibly destroy device if this happens */
880 if (newnode != NULL) {
884 devfs_debug(DEVFS_DEBUG_DEBUG,
885 "parent here is: %s, node is: |%s|\n",
886 ((node->parent->node_type == Nroot) ?
887 "ROOT!" : node->parent->d_dir.d_name),
888 newnode->d_dir.d_name);
889 devfs_debug(DEVFS_DEBUG_DEBUG,
891 ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
894 * orig_vp is set to the original vp if we cloned.
896 /* node->flags |= DEVFS_CLONED; */
897 devfs_allocv(&vp, newnode);
902 lockmgr(&devfs_lock, LK_RELEASE);
904 * Synchronize devfs here to make sure that, if the cloned device
905 * creates other device nodes in addition to the cloned one,
906 * all of them are created by the time we return from opening
913 devfs_debug(DEVFS_DEBUG_DEBUG,
914 "devfs_spec_open() called on %s! \n",
918 * Make this field valid before any I/O in ->d_open
920 if (!dev->si_iosize_max)
921 /* XXX: old DFLTPHYS == 64KB dependency */
922 dev->si_iosize_max = min(MAXPHYS,64*1024);
924 if (dev_dflags(dev) & D_TTY)
925 vsetflags(vp, VISTTY);
928 * Open underlying device
931 error = dev_dopen(dev, ap->a_mode, S_IFCHR, ap->a_cred, ap->a_fp);
932 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
935 * Clean up any cloned vp if we error out.
941 /* orig_vp = NULL; */
947 * This checks if the disk device is going to be opened for writing.
948 * It will be only allowed in the cases where securelevel permits it
949 * and it's not mounted R/W.
951 if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
952 (ap->a_cred != FSCRED)) {
954 /* Very secure mode. No open for writing allowed */
955 if (securelevel >= 2)
959 * If it is mounted R/W, do not allow to open for writing.
960 * In the case it's mounted read-only but securelevel
961 * is >= 1, then do not allow opening for writing either.
963 if (vfs_mountedon(vp)) {
964 if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
966 else if (securelevel >= 1)
971 if (dev_dflags(dev) & D_TTY) {
976 devfs_debug(DEVFS_DEBUG_DEBUG,
977 "devfs: no t_stop\n");
978 tp->t_stop = nottystop;
984 if (vn_isdisk(vp, NULL)) {
985 if (!dev->si_bsize_phys)
986 dev->si_bsize_phys = DEV_BSIZE;
987 vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
993 nanotime(&node->atime);
997 * If we replaced the vp the vop_stdopen() call will have loaded
998 * it into fp->f_data and vref()d the vp, giving us two refs. So
999 * instead of just unlocking it here we have to vput() it.
1004 /* Ugly pty magic, to make pty devices appear once they are opened */
1005 if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY)
1006 node->flags &= ~DEVFS_INVISIBLE;
1009 KKASSERT(ap->a_fp->f_type == DTYPE_VNODE);
1010 KKASSERT((ap->a_fp->f_flag & FMASK) == (ap->a_mode & FMASK));
1011 ap->a_fp->f_ops = &devfs_dev_fileops;
1012 KKASSERT(ap->a_fp->f_data == (void *)vp);
1019 devfs_spec_close(struct vop_close_args *ap)
1021 struct devfs_node *node;
1022 struct proc *p = curproc;
1023 struct vnode *vp = ap->a_vp;
1024 cdev_t dev = vp->v_rdev;
1030 * We do special tests on the opencount so unfortunately we need
1031 * an exclusive lock.
1033 vn_lock(vp, LK_UPGRADE | LK_RETRY);
1036 devfs_debug(DEVFS_DEBUG_DEBUG,
1037 "devfs_spec_close() called on %s! \n",
1040 devfs_debug(DEVFS_DEBUG_DEBUG,
1041 "devfs_spec_close() called, null vode!\n");
1044 * A couple of hacks for devices and tty devices. The
1045 * vnode ref count cannot be used to figure out the
1046 * last close, but we can use v_opencount now that
1047 * revoke works properly.
1049 * Detect the last close on a controlling terminal and clear
1050 * the session (half-close).
1052 * XXX opencount is not SMP safe. The vnode is locked but there
1053 * may be multiple vnodes referencing the same device.
1057 * NOTE: Try to avoid global tokens when testing opencount
1058 * XXX hack, fixme. needs a struct lock and opencount in
1059 * struct cdev itself.
1062 opencount = vp->v_opencount;
1064 opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1069 if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
1070 p->p_session->s_ttyvp = NULL;
1075 * Vnodes can be opened and closed multiple times. Do not really
1076 * close the device unless (1) it is being closed forcibly,
1077 * (2) the device wants to track closes, or (3) this is the last
1078 * vnode doing its last close on the device.
1080 * XXX the VXLOCK (force close) case can leave vnodes referencing
1081 * a closed device. This might not occur now that our revoke is
1084 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
1085 if (dev && ((vp->v_flag & VRECLAIMED) ||
1086 (dev_dflags(dev) & D_TRACKCLOSE) ||
1087 (opencount == 1))) {
1089 * Ugly pty magic, to make pty devices disappear again once
1092 node = DEVFS_NODE(ap->a_vp);
1093 if (node && (node->flags & DEVFS_PTY))
1094 node->flags |= DEVFS_INVISIBLE;
1097 * Unlock around dev_dclose(), unless the vnode is
1098 * undergoing a vgone/reclaim (during umount).
1101 if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) {
1107 * WARNING! If the device destroys itself the devfs node
1108 * can disappear here.
1110 * WARNING! vn_lock() will fail if the vp is in a VRECLAIM,
1111 * which can occur during umount.
1113 error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1114 /* node is now stale */
1117 if (vn_lock(vp, LK_EXCLUSIVE |
1119 LK_FAILRECLAIM) != 0) {
1120 panic("devfs_spec_close: vnode %p "
1121 "unexpectedly could not be relocked",
1128 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
1131 * Track the actual opens and closes on the vnode. The last close
1132 * disassociates the rdev. If the rdev is already disassociated or
1133 * the opencount is already 0, the vnode might have been revoked
1134 * and no further opencount tracking occurs.
1138 if (vp->v_opencount > 0)
1146 devfs_fo_close(struct file *fp)
1148 struct vnode *vp = (struct vnode *)fp->f_data;
1151 fp->f_ops = &badfileops;
1152 error = vn_close(vp, fp->f_flag, fp);
1153 devfs_clear_cdevpriv(fp);
1160 * Device-optimized file table vnode read routine.
1162 * This bypasses the VOP table and talks directly to the device. Most
1163 * filesystems just route to specfs and can make this optimization.
1165 * MPALMOSTSAFE - acquires mplock
1168 devfs_fo_read(struct file *fp, struct uio *uio,
1169 struct ucred *cred, int flags)
1171 struct devfs_node *node;
1177 KASSERT(uio->uio_td == curthread,
1178 ("uio_td %p is not td %p", uio->uio_td, curthread));
1180 if (uio->uio_resid == 0)
1183 vp = (struct vnode *)fp->f_data;
1184 if (vp == NULL || vp->v_type == VBAD)
1187 node = DEVFS_NODE(vp);
1189 if ((dev = vp->v_rdev) == NULL)
1194 if ((flags & O_FOFFSET) == 0)
1195 uio->uio_offset = fp->f_offset;
1198 if (flags & O_FBLOCKING) {
1199 /* ioflag &= ~IO_NDELAY; */
1200 } else if (flags & O_FNONBLOCKING) {
1201 ioflag |= IO_NDELAY;
1202 } else if (fp->f_flag & FNONBLOCK) {
1203 ioflag |= IO_NDELAY;
1205 if (flags & O_FBUFFERED) {
1206 /* ioflag &= ~IO_DIRECT; */
1207 } else if (flags & O_FUNBUFFERED) {
1208 ioflag |= IO_DIRECT;
1209 } else if (fp->f_flag & O_DIRECT) {
1210 ioflag |= IO_DIRECT;
1212 ioflag |= sequential_heuristic(uio, fp);
1214 error = dev_dread(dev, uio, ioflag, fp);
1218 nanotime(&node->atime);
1219 if ((flags & O_FOFFSET) == 0)
1220 fp->f_offset = uio->uio_offset;
1221 fp->f_nextoff = uio->uio_offset;
1228 devfs_fo_write(struct file *fp, struct uio *uio,
1229 struct ucred *cred, int flags)
1231 struct devfs_node *node;
1237 KASSERT(uio->uio_td == curthread,
1238 ("uio_td %p is not p %p", uio->uio_td, curthread));
1240 vp = (struct vnode *)fp->f_data;
1241 if (vp == NULL || vp->v_type == VBAD)
1244 node = DEVFS_NODE(vp);
1246 if (vp->v_type == VREG)
1247 bwillwrite(uio->uio_resid);
1249 vp = (struct vnode *)fp->f_data;
1251 if ((dev = vp->v_rdev) == NULL)
1256 if ((flags & O_FOFFSET) == 0)
1257 uio->uio_offset = fp->f_offset;
1260 if (vp->v_type == VREG &&
1261 ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
1262 ioflag |= IO_APPEND;
1265 if (flags & O_FBLOCKING) {
1266 /* ioflag &= ~IO_NDELAY; */
1267 } else if (flags & O_FNONBLOCKING) {
1268 ioflag |= IO_NDELAY;
1269 } else if (fp->f_flag & FNONBLOCK) {
1270 ioflag |= IO_NDELAY;
1272 if (flags & O_FBUFFERED) {
1273 /* ioflag &= ~IO_DIRECT; */
1274 } else if (flags & O_FUNBUFFERED) {
1275 ioflag |= IO_DIRECT;
1276 } else if (fp->f_flag & O_DIRECT) {
1277 ioflag |= IO_DIRECT;
1279 if (flags & O_FASYNCWRITE) {
1280 /* ioflag &= ~IO_SYNC; */
1281 } else if (flags & O_FSYNCWRITE) {
1283 } else if (fp->f_flag & O_FSYNC) {
1287 if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
1289 ioflag |= sequential_heuristic(uio, fp);
1291 error = dev_dwrite(dev, uio, ioflag, fp);
1295 nanotime(&node->atime);
1296 nanotime(&node->mtime);
1299 if ((flags & O_FOFFSET) == 0)
1300 fp->f_offset = uio->uio_offset;
1301 fp->f_nextoff = uio->uio_offset;
1308 devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
1317 vp = (struct vnode *)fp->f_data;
1318 if (vp == NULL || vp->v_type == VBAD)
1321 error = vn_stat(vp, sb, cred);
1326 error = VOP_GETATTR(vp, vap);
1331 * Zero the spare stat fields
1338 * Copy from vattr table ... or not in case it's a cloned device
1340 if (vap->va_fsid != VNOVAL)
1341 sb->st_dev = vap->va_fsid;
1343 sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
1345 sb->st_ino = vap->va_fileid;
1347 mode = vap->va_mode;
1351 if (vap->va_nlink > (nlink_t)-1)
1352 sb->st_nlink = (nlink_t)-1;
1354 sb->st_nlink = vap->va_nlink;
1356 sb->st_uid = vap->va_uid;
1357 sb->st_gid = vap->va_gid;
1358 sb->st_rdev = dev2udev(DEVFS_NODE(vp)->d_dev);
1359 sb->st_size = vap->va_bytes;
1360 sb->st_atimespec = vap->va_atime;
1361 sb->st_mtimespec = vap->va_mtime;
1362 sb->st_ctimespec = vap->va_ctime;
1365 * A VCHR and VBLK device may track the last access and last modified
1366 * time independantly of the filesystem. This is particularly true
1367 * because device read and write calls may bypass the filesystem.
1369 if (vp->v_type == VCHR || vp->v_type == VBLK) {
1372 if (dev->si_lastread) {
1373 sb->st_atimespec.tv_sec = time_second +
1376 sb->st_atimespec.tv_nsec = 0;
1378 if (dev->si_lastwrite) {
1379 sb->st_atimespec.tv_sec = time_second +
1382 sb->st_atimespec.tv_nsec = 0;
1388 * According to www.opengroup.org, the meaning of st_blksize is
1389 * "a filesystem-specific preferred I/O block size for this
1390 * object. In some filesystem types, this may vary from file
1392 * Default to PAGE_SIZE after much discussion.
1395 sb->st_blksize = PAGE_SIZE;
1397 sb->st_flags = vap->va_flags;
1399 error = priv_check_cred(cred, PRIV_VFS_GENERATION, 0);
1403 sb->st_gen = (u_int32_t)vap->va_gen;
1405 sb->st_blocks = vap->va_bytes / S_BLKSIZE;
1412 devfs_fo_kqfilter(struct file *fp, struct knote *kn)
1418 vp = (struct vnode *)fp->f_data;
1419 if (vp == NULL || vp->v_type == VBAD) {
1423 if ((dev = vp->v_rdev) == NULL) {
1429 error = dev_dkqfilter(dev, kn, fp);
1438 * MPALMOSTSAFE - acquires mplock
1441 devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
1442 struct ucred *ucred, struct sysmsg *msg)
1445 struct devfs_node *node;
1451 struct fiodname_args *name_args;
1455 vp = ((struct vnode *)fp->f_data);
1457 if ((dev = vp->v_rdev) == NULL)
1458 return EBADF; /* device was revoked */
1463 node = DEVFS_NODE(vp);
1466 devfs_debug(DEVFS_DEBUG_DEBUG,
1467 "devfs_fo_ioctl() called! for dev %s\n",
1470 if (com == FIODTYPE) {
1471 *(int *)data = dev_dflags(dev) & D_TYPEMASK;
1474 } else if (com == FIODNAME) {
1475 name_args = (struct fiodname_args *)data;
1476 name = dev->si_name;
1477 namlen = strlen(name) + 1;
1479 devfs_debug(DEVFS_DEBUG_DEBUG,
1480 "ioctl, got: FIODNAME for %s\n", name);
1482 if (namlen <= name_args->len)
1483 error = copyout(dev->si_name, name_args->name, namlen);
1487 devfs_debug(DEVFS_DEBUG_DEBUG,
1488 "ioctl stuff: error: %d\n", error);
1492 error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp);
1496 nanotime(&node->atime);
1497 nanotime(&node->mtime);
1500 if (com == TIOCSCTTY) {
1501 devfs_debug(DEVFS_DEBUG_DEBUG,
1502 "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
1505 if (error == 0 && com == TIOCSCTTY) {
1506 struct proc *p = curthread->td_proc;
1507 struct session *sess;
1509 devfs_debug(DEVFS_DEBUG_DEBUG,
1510 "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
1516 sess = p->p_session;
1519 * Do nothing if reassigning same control tty
1521 if (sess->s_ttyvp == vp) {
1527 * Get rid of reference to old control tty
1529 ovp = sess->s_ttyvp;
1538 devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
1544 devfs_spec_fsync(struct vop_fsync_args *ap)
1546 struct vnode *vp = ap->a_vp;
1549 if (!vn_isdisk(vp, NULL))
1553 * Flush all dirty buffers associated with a block device.
1555 error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
1560 devfs_spec_read(struct vop_read_args *ap)
1562 struct devfs_node *node;
1571 node = DEVFS_NODE(vp);
1573 if (dev == NULL) /* device was revoked */
1575 if (uio->uio_resid == 0)
1579 error = dev_dread(dev, uio, ap->a_ioflag, NULL);
1580 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1583 nanotime(&node->atime);
1589 * Vnode op for write
1591 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1592 * struct ucred *a_cred)
1595 devfs_spec_write(struct vop_write_args *ap)
1597 struct devfs_node *node;
1606 node = DEVFS_NODE(vp);
1608 KKASSERT(uio->uio_segflg != UIO_NOCOPY);
1610 if (dev == NULL) /* device was revoked */
1614 error = dev_dwrite(dev, uio, ap->a_ioflag, NULL);
1615 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1618 nanotime(&node->atime);
1619 nanotime(&node->mtime);
1626 * Device ioctl operation.
1628 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
1629 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
1632 devfs_spec_ioctl(struct vop_ioctl_args *ap)
1634 struct vnode *vp = ap->a_vp;
1636 struct devfs_node *node;
1640 if ((dev = vp->v_rdev) == NULL)
1641 return (EBADF); /* device was revoked */
1643 node = DEVFS_NODE(vp);
1646 nanotime(&node->atime);
1647 nanotime(&node->mtime);
1651 return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
1652 ap->a_cred, ap->a_sysmsg, NULL));
1656 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1660 devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
1662 struct vnode *vp = ap->a_vp;
1664 struct devfs_node *node;
1668 if ((dev = vp->v_rdev) == NULL)
1669 return (EBADF); /* device was revoked (EBADF) */
1671 node = DEVFS_NODE(vp);
1674 nanotime(&node->atime);
1677 return (dev_dkqfilter(dev, ap->a_kn, NULL));
1681 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1682 * calls are not limited to device DMA limits so we have to deal with the
1685 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1688 devfs_spec_strategy(struct vop_strategy_args *ap)
1690 struct bio *bio = ap->a_bio;
1691 struct buf *bp = bio->bio_buf;
1698 if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
1702 * Collect statistics on synchronous and asynchronous read
1703 * and write counts for disks that have associated filesystems.
1706 KKASSERT(vp->v_rdev != NULL); /* XXX */
1707 if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
1708 if (bp->b_cmd == BUF_CMD_READ) {
1709 if (bp->b_flags & BIO_SYNC)
1710 mp->mnt_stat.f_syncreads++;
1712 mp->mnt_stat.f_asyncreads++;
1714 if (bp->b_flags & BIO_SYNC)
1715 mp->mnt_stat.f_syncwrites++;
1717 mp->mnt_stat.f_asyncwrites++;
1722 * Device iosize limitations only apply to read and write. Shortcut
1723 * the I/O if it fits.
1725 if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1726 devfs_debug(DEVFS_DEBUG_DEBUG,
1727 "%s: si_iosize_max not set!\n",
1728 dev_dname(vp->v_rdev));
1729 maxiosize = MAXPHYS;
1731 #if SPEC_CHAIN_DEBUG & 2
1734 if (bp->b_bcount <= maxiosize ||
1735 (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
1736 dev_dstrategy_chain(vp->v_rdev, bio);
1741 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1743 nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
1746 BUF_LOCK(nbp, LK_EXCLUSIVE);
1749 nbp->b_flags = B_PAGING | (bp->b_flags & B_BNOCLIP);
1750 nbp->b_data = bp->b_data;
1751 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1752 nbp->b_bio1.bio_offset = bio->bio_offset;
1753 nbp->b_bio1.bio_caller_info1.ptr = bio;
1756 * Start the first transfer
1758 if (vn_isdisk(vp, NULL))
1759 chunksize = vp->v_rdev->si_bsize_phys;
1761 chunksize = DEV_BSIZE;
1762 chunksize = maxiosize / chunksize * chunksize;
1763 #if SPEC_CHAIN_DEBUG & 1
1764 devfs_debug(DEVFS_DEBUG_DEBUG,
1765 "spec_strategy chained I/O chunksize=%d\n",
1768 nbp->b_cmd = bp->b_cmd;
1769 nbp->b_bcount = chunksize;
1770 nbp->b_bufsize = chunksize; /* used to detect a short I/O */
1771 nbp->b_bio1.bio_caller_info2.index = chunksize;
1773 #if SPEC_CHAIN_DEBUG & 1
1774 devfs_debug(DEVFS_DEBUG_DEBUG,
1775 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1776 bp, 0, bp->b_bcount, nbp->b_bcount);
1779 dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
1781 if (DEVFS_NODE(vp)) {
1782 nanotime(&DEVFS_NODE(vp)->atime);
1783 nanotime(&DEVFS_NODE(vp)->mtime);
1790 * Chunked up transfer completion routine - chain transfers until done
1792 * NOTE: MPSAFE callback.
1796 devfs_spec_strategy_done(struct bio *nbio)
1798 struct buf *nbp = nbio->bio_buf;
1799 struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
1800 struct buf *bp = bio->bio_buf; /* original bp */
1801 int chunksize = nbio->bio_caller_info2.index; /* chunking */
1802 int boffset = nbp->b_data - bp->b_data;
1804 if (nbp->b_flags & B_ERROR) {
1806 * An error terminates the chain, propogate the error back
1807 * to the original bp
1809 bp->b_flags |= B_ERROR;
1810 bp->b_error = nbp->b_error;
1811 bp->b_resid = bp->b_bcount - boffset +
1812 (nbp->b_bcount - nbp->b_resid);
1813 #if SPEC_CHAIN_DEBUG & 1
1814 devfs_debug(DEVFS_DEBUG_DEBUG,
1815 "spec_strategy: chain %p error %d bcount %d/%d\n",
1816 bp, bp->b_error, bp->b_bcount,
1817 bp->b_bcount - bp->b_resid);
1819 } else if (nbp->b_resid) {
1821 * A short read or write terminates the chain
1823 bp->b_error = nbp->b_error;
1824 bp->b_resid = bp->b_bcount - boffset +
1825 (nbp->b_bcount - nbp->b_resid);
1826 #if SPEC_CHAIN_DEBUG & 1
1827 devfs_debug(DEVFS_DEBUG_DEBUG,
1828 "spec_strategy: chain %p short read(1) "
1830 bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
1832 } else if (nbp->b_bcount != nbp->b_bufsize) {
1834 * A short read or write can also occur by truncating b_bcount
1836 #if SPEC_CHAIN_DEBUG & 1
1837 devfs_debug(DEVFS_DEBUG_DEBUG,
1838 "spec_strategy: chain %p short read(2) "
1840 bp, nbp->b_bcount + boffset, bp->b_bcount);
1843 bp->b_bcount = nbp->b_bcount + boffset;
1844 bp->b_resid = nbp->b_resid;
1845 } else if (nbp->b_bcount + boffset == bp->b_bcount) {
1847 * No more data terminates the chain
1849 #if SPEC_CHAIN_DEBUG & 1
1850 devfs_debug(DEVFS_DEBUG_DEBUG,
1851 "spec_strategy: chain %p finished bcount %d\n",
1858 * Continue the chain
1860 boffset += nbp->b_bcount;
1861 nbp->b_data = bp->b_data + boffset;
1862 nbp->b_bcount = bp->b_bcount - boffset;
1863 if (nbp->b_bcount > chunksize)
1864 nbp->b_bcount = chunksize;
1865 nbp->b_bio1.bio_done = devfs_spec_strategy_done;
1866 nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
1868 #if SPEC_CHAIN_DEBUG & 1
1869 devfs_debug(DEVFS_DEBUG_DEBUG,
1870 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1871 bp, boffset, bp->b_bcount, nbp->b_bcount);
1874 dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
1879 * Fall through to here on termination. biodone(bp) and
1880 * clean up and free nbp.
1885 kfree(nbp, M_DEVBUF);
1889 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1892 devfs_spec_freeblks(struct vop_freeblks_args *ap)
1897 * XXX: This assumes that strategy does the deed right away.
1898 * XXX: this may not be TRTTD.
1900 KKASSERT(ap->a_vp->v_rdev != NULL);
1901 if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0)
1903 bp = geteblk(ap->a_length);
1904 bp->b_cmd = BUF_CMD_FREEBLKS;
1905 bp->b_bio1.bio_offset = ap->a_offset;
1906 bp->b_bcount = ap->a_length;
1907 dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
1912 * Implement degenerate case where the block requested is the block
1913 * returned, and assume that the entire device is contiguous in regards
1914 * to the contiguous block range (runp and runb).
1916 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1917 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1920 devfs_spec_bmap(struct vop_bmap_args *ap)
1922 if (ap->a_doffsetp != NULL)
1923 *ap->a_doffsetp = ap->a_loffset;
1924 if (ap->a_runp != NULL)
1925 *ap->a_runp = MAXBSIZE;
1926 if (ap->a_runb != NULL) {
1927 if (ap->a_loffset < MAXBSIZE)
1928 *ap->a_runb = (int)ap->a_loffset;
1930 *ap->a_runb = MAXBSIZE;
1937 * Special device advisory byte-level locks.
1939 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1940 * struct flock *a_fl, int a_flags)
1944 devfs_spec_advlock(struct vop_advlock_args *ap)
1946 return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
1950 * NOTE: MPSAFE callback.
1953 devfs_spec_getpages_iodone(struct bio *bio)
1955 bio->bio_buf->b_cmd = BUF_CMD_DONE;
1956 wakeup(bio->bio_buf);
1960 * spec_getpages() - get pages associated with device vnode.
1962 * Note that spec_read and spec_write do not use the buffer cache, so we
1963 * must fully implement getpages here.
1966 devfs_spec_getpages(struct vop_getpages_args *ap)
1970 int i, pcount, size;
1973 vm_ooffset_t offset;
1974 int toff, nextoff, nread;
1975 struct vnode *vp = ap->a_vp;
1980 pcount = round_page(ap->a_count) / PAGE_SIZE;
1983 * Calculate the offset of the transfer and do sanity check.
1985 offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
1988 * Round up physical size for real devices. We cannot round using
1989 * v_mount's block size data because v_mount has nothing to do with
1990 * the device. i.e. it's usually '/dev'. We need the physical block
1991 * size for the device itself.
1993 * We can't use v_rdev->si_mountpoint because it only exists when the
1994 * block device is mounted. However, we can use v_rdev.
1996 if (vn_isdisk(vp, NULL))
1997 blksiz = vp->v_rdev->si_bsize_phys;
2001 size = (ap->a_count + blksiz - 1) & ~(blksiz - 1);
2003 bp = getpbuf_kva(NULL);
2004 kva = (vm_offset_t)bp->b_data;
2007 * Map the pages to be read into the kva.
2009 pmap_qenter(kva, ap->a_m, pcount);
2011 /* Build a minimal buffer header. */
2012 bp->b_cmd = BUF_CMD_READ;
2013 bp->b_bcount = size;
2015 bsetrunningbufspace(bp, size);
2017 bp->b_bio1.bio_offset = offset;
2018 bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
2020 mycpu->gd_cnt.v_vnodein++;
2021 mycpu->gd_cnt.v_vnodepgsin += pcount;
2024 vn_strategy(ap->a_vp, &bp->b_bio1);
2028 /* We definitely need to be at splbio here. */
2029 while (bp->b_cmd != BUF_CMD_DONE)
2030 tsleep(bp, 0, "spread", 0);
2034 if (bp->b_flags & B_ERROR) {
2036 error = bp->b_error;
2042 * If EOF is encountered we must zero-extend the result in order
2043 * to ensure that the page does not contain garabge. When no
2044 * error occurs, an early EOF is indicated if b_bcount got truncated.
2045 * b_resid is relative to b_bcount and should be 0, but some devices
2046 * might indicate an EOF with b_resid instead of truncating b_bcount.
2048 nread = bp->b_bcount - bp->b_resid;
2049 if (nread < ap->a_count)
2050 bzero((caddr_t)kva + nread, ap->a_count - nread);
2051 pmap_qremove(kva, pcount);
2054 for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
2055 nextoff = toff + PAGE_SIZE;
2058 m->flags &= ~PG_ZERO;
2061 * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2062 * pmap modified bit. pmap modified bit should have
2063 * already been cleared.
2065 if (nextoff <= nread) {
2066 m->valid = VM_PAGE_BITS_ALL;
2068 } else if (toff < nread) {
2070 * Since this is a VM request, we have to supply the
2071 * unaligned offset to allow vm_page_set_valid()
2072 * to zero sub-DEV_BSIZE'd portions of the page.
2074 vm_page_set_valid(m, 0, nread - toff);
2075 vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
2081 if (i != ap->a_reqpage) {
2083 * Just in case someone was asking for this page we
2084 * now tell them that it is ok to use.
2086 if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
2088 if (m->flags & PG_REFERENCED) {
2089 vm_page_activate(m);
2091 vm_page_deactivate(m);
2100 } else if (m->valid) {
2103 * Since this is a VM request, we need to make the
2104 * entire page presentable by zeroing invalid sections.
2106 if (m->valid != VM_PAGE_BITS_ALL)
2107 vm_page_zero_invalid(m, FALSE);
2111 m = ap->a_m[ap->a_reqpage];
2112 devfs_debug(DEVFS_DEBUG_WARNING,
2113 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
2114 devtoname(vp->v_rdev), error, bp, bp->b_vp);
2115 devfs_debug(DEVFS_DEBUG_WARNING,
2116 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
2117 size, bp->b_resid, ap->a_count, m->valid);
2118 devfs_debug(DEVFS_DEBUG_WARNING,
2119 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
2120 nread, ap->a_reqpage, (u_long)m->pindex, pcount);
2122 * Free the buffer header back to the swap buffer pool.
2125 return VM_PAGER_ERROR;
2128 * Free the buffer header back to the swap buffer pool.
2131 if (DEVFS_NODE(ap->a_vp))
2132 nanotime(&DEVFS_NODE(ap->a_vp)->mtime);
2138 sequential_heuristic(struct uio *uio, struct file *fp)
2141 * Sequential heuristic - detect sequential operation
2143 if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
2144 uio->uio_offset == fp->f_nextoff) {
2146 * XXX we assume that the filesystem block size is
2147 * the default. Not true, but still gives us a pretty
2148 * good indicator of how sequential the read operations
2151 int tmpseq = fp->f_seqcount;
2153 tmpseq += (uio->uio_resid + BKVASIZE - 1) / BKVASIZE;
2154 if (tmpseq > IO_SEQMAX)
2156 fp->f_seqcount = tmpseq;
2157 return(fp->f_seqcount << IO_SEQSHIFT);
2161 * Not sequential, quick draw-down of seqcount
2163 if (fp->f_seqcount > 1)