2 * Copyright (c) 2009 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Alex Hornung <ahornung@gmail.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/fcntl.h>
42 #include <sys/signalvar.h>
43 #include <sys/vnode.h>
45 #include <sys/mount.h>
47 #include <sys/fcntl.h>
48 #include <sys/namei.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/devfs.h>
60 #include <sys/pioctl.h>
62 #include <machine/limits.h>
63 #include <vm/vm_page2.h>
65 #include <sys/sysref2.h>
67 MALLOC_DECLARE(M_DEVFS
);
68 #define DEVFS_BADOP (void *)devfs_badop
70 static int devfs_badop(struct vop_generic_args
*);
71 static int devfs_access(struct vop_access_args
*);
72 static int devfs_inactive(struct vop_inactive_args
*);
73 static int devfs_reclaim(struct vop_reclaim_args
*);
74 static int devfs_readdir(struct vop_readdir_args
*);
75 static int devfs_getattr(struct vop_getattr_args
*);
76 static int devfs_setattr(struct vop_setattr_args
*);
77 static int devfs_readlink(struct vop_readlink_args
*);
78 static int devfs_print(struct vop_print_args
*);
80 static int devfs_nresolve(struct vop_nresolve_args
*);
81 static int devfs_nlookupdotdot(struct vop_nlookupdotdot_args
*);
82 static int devfs_nsymlink(struct vop_nsymlink_args
*);
83 static int devfs_nremove(struct vop_nremove_args
*);
85 static int devfs_spec_open(struct vop_open_args
*);
86 static int devfs_spec_close(struct vop_close_args
*);
87 static int devfs_spec_fsync(struct vop_fsync_args
*);
89 static int devfs_spec_read(struct vop_read_args
*);
90 static int devfs_spec_write(struct vop_write_args
*);
91 static int devfs_spec_ioctl(struct vop_ioctl_args
*);
92 static int devfs_spec_poll(struct vop_poll_args
*);
93 static int devfs_spec_kqfilter(struct vop_kqfilter_args
*);
94 static int devfs_spec_strategy(struct vop_strategy_args
*);
95 static void devfs_spec_strategy_done(struct bio
*);
96 static int devfs_spec_freeblks(struct vop_freeblks_args
*);
97 static int devfs_spec_bmap(struct vop_bmap_args
*);
98 static int devfs_spec_advlock(struct vop_advlock_args
*);
99 static void devfs_spec_getpages_iodone(struct bio
*);
100 static int devfs_spec_getpages(struct vop_getpages_args
*);
103 static int devfs_specf_close(struct file
*);
104 static int devfs_specf_read(struct file
*, struct uio
*, struct ucred
*, int);
105 static int devfs_specf_write(struct file
*, struct uio
*, struct ucred
*, int);
106 static int devfs_specf_stat(struct file
*, struct stat
*, struct ucred
*);
107 static int devfs_specf_kqfilter(struct file
*, struct knote
*);
108 static int devfs_specf_poll(struct file
*, int, struct ucred
*);
109 static int devfs_specf_ioctl(struct file
*, u_long
, caddr_t
,
110 struct ucred
*, struct sysmsg
*);
111 static __inline
int sequential_heuristic(struct uio
*, struct file
*);
113 extern struct lock devfs_lock
;
116 * devfs vnode operations for regular files
118 struct vop_ops devfs_vnode_norm_vops
= {
119 .vop_default
= vop_defaultop
,
120 .vop_access
= devfs_access
,
121 .vop_advlock
= DEVFS_BADOP
,
122 .vop_bmap
= DEVFS_BADOP
,
123 .vop_close
= vop_stdclose
,
124 .vop_getattr
= devfs_getattr
,
125 .vop_inactive
= devfs_inactive
,
126 .vop_ncreate
= DEVFS_BADOP
,
127 .vop_nresolve
= devfs_nresolve
,
128 .vop_nlookupdotdot
= devfs_nlookupdotdot
,
129 .vop_nlink
= DEVFS_BADOP
,
130 .vop_nmkdir
= DEVFS_BADOP
,
131 .vop_nmknod
= DEVFS_BADOP
,
132 .vop_nremove
= devfs_nremove
,
133 .vop_nrename
= DEVFS_BADOP
,
134 .vop_nrmdir
= DEVFS_BADOP
,
135 .vop_nsymlink
= devfs_nsymlink
,
136 .vop_open
= vop_stdopen
,
137 .vop_pathconf
= vop_stdpathconf
,
138 .vop_print
= devfs_print
,
139 .vop_read
= DEVFS_BADOP
,
140 .vop_readdir
= devfs_readdir
,
141 .vop_readlink
= devfs_readlink
,
142 .vop_reclaim
= devfs_reclaim
,
143 .vop_setattr
= devfs_setattr
,
144 .vop_write
= DEVFS_BADOP
,
145 .vop_ioctl
= DEVFS_BADOP
149 * devfs vnode operations for character devices
151 struct vop_ops devfs_vnode_dev_vops
= {
152 .vop_default
= vop_defaultop
,
153 .vop_access
= devfs_access
,
154 .vop_advlock
= devfs_spec_advlock
,
155 .vop_bmap
= devfs_spec_bmap
,
156 .vop_close
= devfs_spec_close
,
157 .vop_freeblks
= devfs_spec_freeblks
,
158 .vop_fsync
= devfs_spec_fsync
,
159 .vop_getattr
= devfs_getattr
,
160 .vop_getpages
= devfs_spec_getpages
,
161 .vop_inactive
= devfs_inactive
,
162 .vop_open
= devfs_spec_open
,
163 .vop_pathconf
= vop_stdpathconf
,
164 .vop_print
= devfs_print
,
165 .vop_poll
= devfs_spec_poll
,
166 .vop_kqfilter
= devfs_spec_kqfilter
,
167 .vop_read
= devfs_spec_read
,
168 .vop_readdir
= DEVFS_BADOP
,
169 .vop_readlink
= DEVFS_BADOP
,
170 .vop_reclaim
= devfs_reclaim
,
171 .vop_setattr
= devfs_setattr
,
172 .vop_strategy
= devfs_spec_strategy
,
173 .vop_write
= devfs_spec_write
,
174 .vop_ioctl
= devfs_spec_ioctl
177 struct vop_ops
*devfs_vnode_dev_vops_p
= &devfs_vnode_dev_vops
;
179 struct fileops devfs_dev_fileops
= {
180 .fo_read
= devfs_specf_read
,
181 .fo_write
= devfs_specf_write
,
182 .fo_ioctl
= devfs_specf_ioctl
,
183 .fo_poll
= devfs_specf_poll
,
184 .fo_kqfilter
= devfs_specf_kqfilter
,
185 .fo_stat
= devfs_specf_stat
,
186 .fo_close
= devfs_specf_close
,
187 .fo_shutdown
= nofo_shutdown
191 * These two functions are possibly temporary hacks for
192 * devices (aka the pty code) which want to control the
193 * node attributes themselves.
195 * XXX we may ultimately desire to simply remove the uid/gid/mode
196 * from the node entirely.
199 node_sync_dev_get(struct devfs_node
*node
)
203 if ((dev
= node
->d_dev
) && (dev
->si_flags
& SI_OVERRIDE
)) {
204 node
->uid
= dev
->si_uid
;
205 node
->gid
= dev
->si_gid
;
206 node
->mode
= dev
->si_perms
;
211 node_sync_dev_set(struct devfs_node
*node
)
215 if ((dev
= node
->d_dev
) && (dev
->si_flags
& SI_OVERRIDE
)) {
216 dev
->si_uid
= node
->uid
;
217 dev
->si_gid
= node
->gid
;
218 dev
->si_perms
= node
->mode
;
223 * generic entry point for unsupported operations
226 devfs_badop(struct vop_generic_args
*ap
)
233 devfs_access(struct vop_access_args
*ap
)
235 struct devfs_node
*node
= DEVFS_NODE(ap
->a_vp
);
238 if (!devfs_node_is_accessible(node
))
240 node_sync_dev_get(node
);
241 error
= vop_helper_access(ap
, node
->uid
, node
->gid
,
242 node
->mode
, node
->flags
);
249 devfs_inactive(struct vop_inactive_args
*ap
)
251 struct devfs_node
*node
= DEVFS_NODE(ap
->a_vp
);
253 if (node
== NULL
|| (node
->flags
& DEVFS_NODE_LINKED
) == 0)
260 devfs_reclaim(struct vop_reclaim_args
*ap
)
262 struct devfs_node
*node
;
267 * Check if it is locked already. if not, we acquire the devfs lock
269 if (!(lockstatus(&devfs_lock
, curthread
)) == LK_EXCLUSIVE
) {
270 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
277 * Get rid of the devfs_node if it is no longer linked into the
281 if ((node
= DEVFS_NODE(vp
)) != NULL
) {
283 if ((node
->flags
& DEVFS_NODE_LINKED
) == 0)
288 lockmgr(&devfs_lock
, LK_RELEASE
);
291 * v_rdev needs to be properly released using v_release_rdev
292 * Make sure v_data is NULL as well.
301 devfs_readdir(struct vop_readdir_args
*ap
)
303 struct devfs_node
*dnode
= DEVFS_NODE(ap
->a_vp
);
304 struct devfs_node
*node
;
313 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_readdir() called!\n");
315 if (ap
->a_uio
->uio_offset
< 0 || ap
->a_uio
->uio_offset
> INT_MAX
)
317 if ((error
= vn_lock(ap
->a_vp
, LK_EXCLUSIVE
| LK_RETRY
)) != 0)
320 if (!devfs_node_is_accessible(dnode
))
323 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
325 saveoff
= ap
->a_uio
->uio_offset
;
327 if (ap
->a_ncookies
) {
328 ncookies
= ap
->a_uio
->uio_resid
/ 16 + 1; /* Why / 16 ?? */
331 cookies
= kmalloc(256 * sizeof(off_t
), M_TEMP
, M_WAITOK
);
339 nanotime(&dnode
->atime
);
342 r
= vop_write_dirent(&error
, ap
->a_uio
, dnode
->d_dir
.d_ino
,
347 cookies
[cookie_index
] = saveoff
;
350 if (cookie_index
== ncookies
)
356 r
= vop_write_dirent(&error
, ap
->a_uio
,
357 dnode
->parent
->d_dir
.d_ino
,
360 r
= vop_write_dirent(&error
, ap
->a_uio
,
367 cookies
[cookie_index
] = saveoff
;
370 if (cookie_index
== ncookies
)
374 TAILQ_FOREACH(node
, DEVFS_DENODE_HEAD(dnode
), link
) {
375 if ((node
->flags
& DEVFS_HIDDEN
) ||
376 (node
->flags
& DEVFS_INVISIBLE
)) {
381 * If the node type is a valid devfs alias, then we make sure that the
382 * target isn't hidden. If it is, we don't show the link in the
385 if ((node
->node_type
== Plink
) && (node
->link_target
!= NULL
) &&
386 (node
->link_target
->flags
& DEVFS_HIDDEN
))
389 if (node
->cookie
< saveoff
)
392 saveoff
= node
->cookie
;
394 error2
= vop_write_dirent(&error
, ap
->a_uio
, node
->d_dir
.d_ino
,
396 node
->d_dir
.d_namlen
,
405 cookies
[cookie_index
] = node
->cookie
;
407 if (cookie_index
== ncookies
)
412 lockmgr(&devfs_lock
, LK_RELEASE
);
415 ap
->a_uio
->uio_offset
= saveoff
;
416 if (error
&& cookie_index
== 0) {
418 kfree(cookies
, M_TEMP
);
420 *ap
->a_cookies
= NULL
;
424 *ap
->a_ncookies
= cookie_index
;
425 *ap
->a_cookies
= cookies
;
433 devfs_nresolve(struct vop_nresolve_args
*ap
)
435 struct devfs_node
*dnode
= DEVFS_NODE(ap
->a_dvp
);
436 struct devfs_node
*node
, *found
= NULL
;
437 struct namecache
*ncp
;
438 struct vnode
*vp
= NULL
;
444 ncp
= ap
->a_nch
->ncp
;
447 if (!devfs_node_is_accessible(dnode
))
450 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
452 if ((dnode
->node_type
!= Proot
) && (dnode
->node_type
!= Pdir
)) {
454 cache_setvp(ap
->a_nch
, NULL
);
458 TAILQ_FOREACH(node
, DEVFS_DENODE_HEAD(dnode
), link
) {
459 if (len
== node
->d_dir
.d_namlen
) {
460 if (!memcmp(ncp
->nc_name
, node
->d_dir
.d_name
, len
)) {
469 while ((found
->node_type
== Plink
) && (found
->link_target
)) {
471 devfs_debug(DEVFS_DEBUG_SHOW
, "Recursive link or depth >= 8");
475 found
= found
->link_target
;
479 if (!(found
->flags
& DEVFS_HIDDEN
))
480 devfs_allocv(/*ap->a_dvp->v_mount, */ &vp
, found
);
487 cache_setvp(ap
->a_nch
, NULL
);
493 cache_setvp(ap
->a_nch
, vp
);
496 lockmgr(&devfs_lock
, LK_RELEASE
);
503 devfs_nlookupdotdot(struct vop_nlookupdotdot_args
*ap
)
505 struct devfs_node
*dnode
= DEVFS_NODE(ap
->a_dvp
);
508 if (!devfs_node_is_accessible(dnode
))
511 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
512 if (dnode
->parent
!= NULL
) {
513 devfs_allocv(ap
->a_vpp
, dnode
->parent
);
514 vn_unlock(*ap
->a_vpp
);
516 lockmgr(&devfs_lock
, LK_RELEASE
);
518 return ((*ap
->a_vpp
== NULL
) ? ENOENT
: 0);
523 devfs_getattr(struct vop_getattr_args
*ap
)
525 struct devfs_node
*node
= DEVFS_NODE(ap
->a_vp
);
526 struct vattr
*vap
= ap
->a_vap
;
530 if (!devfs_node_is_accessible(node
))
533 node_sync_dev_get(node
);
535 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
537 /* start by zeroing out the attributes */
540 /* next do all the common fields */
541 vap
->va_type
= ap
->a_vp
->v_type
;
542 vap
->va_mode
= node
->mode
;
543 vap
->va_fileid
= DEVFS_NODE(ap
->a_vp
)->d_dir
.d_ino
;
544 vap
->va_flags
= 0; /* XXX: what should this be? */
545 vap
->va_blocksize
= DEV_BSIZE
;
546 vap
->va_bytes
= vap
->va_size
= sizeof(struct devfs_node
);
548 vap
->va_fsid
= ap
->a_vp
->v_mount
->mnt_stat
.f_fsid
.val
[0];
550 vap
->va_atime
= node
->atime
;
551 vap
->va_mtime
= node
->mtime
;
552 vap
->va_ctime
= node
->ctime
;
554 vap
->va_nlink
= 1; /* number of references to file */
556 vap
->va_uid
= node
->uid
;
557 vap
->va_gid
= node
->gid
;
562 if ((node
->node_type
== Pdev
) && node
->d_dev
) {
563 reference_dev(node
->d_dev
);
564 vap
->va_rminor
= node
->d_dev
->si_uminor
;
565 release_dev(node
->d_dev
);
568 /* For a softlink the va_size is the length of the softlink */
569 if (node
->symlink_name
!= 0) {
570 vap
->va_size
= node
->symlink_namelen
;
572 lockmgr(&devfs_lock
, LK_RELEASE
);
579 devfs_setattr(struct vop_setattr_args
*ap
)
581 struct devfs_node
*node
= DEVFS_NODE(ap
->a_vp
);
585 if (!devfs_node_is_accessible(node
))
587 node_sync_dev_get(node
);
589 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
593 if (vap
->va_uid
!= (uid_t
)VNOVAL
) {
594 if ((ap
->a_cred
->cr_uid
!= node
->uid
) &&
595 (!groupmember(node
->gid
, ap
->a_cred
))) {
596 error
= priv_check(curthread
, PRIV_VFS_CHOWN
);
600 node
->uid
= vap
->va_uid
;
603 if (vap
->va_gid
!= (uid_t
)VNOVAL
) {
604 if ((ap
->a_cred
->cr_uid
!= node
->uid
) &&
605 (!groupmember(node
->gid
, ap
->a_cred
))) {
606 error
= priv_check(curthread
, PRIV_VFS_CHOWN
);
610 node
->gid
= vap
->va_gid
;
613 if (vap
->va_mode
!= (mode_t
)VNOVAL
) {
614 if (ap
->a_cred
->cr_uid
!= node
->uid
) {
615 error
= priv_check(curthread
, PRIV_VFS_ADMIN
);
619 node
->mode
= vap
->va_mode
;
623 node_sync_dev_set(node
);
624 nanotime(&node
->ctime
);
625 lockmgr(&devfs_lock
, LK_RELEASE
);
632 devfs_readlink(struct vop_readlink_args
*ap
)
634 struct devfs_node
*node
= DEVFS_NODE(ap
->a_vp
);
637 if (!devfs_node_is_accessible(node
))
640 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
641 ret
= uiomove(node
->symlink_name
, node
->symlink_namelen
, ap
->a_uio
);
642 lockmgr(&devfs_lock
, LK_RELEASE
);
649 devfs_print(struct vop_print_args
*ap
)
656 devfs_nsymlink(struct vop_nsymlink_args
*ap
)
658 struct devfs_node
*dnode
= DEVFS_NODE(ap
->a_dvp
);
659 struct devfs_node
*node
;
662 if (!devfs_node_is_accessible(dnode
))
665 ap
->a_vap
->va_type
= VLNK
;
667 if ((dnode
->node_type
!= Proot
) && (dnode
->node_type
!= Pdir
))
670 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
671 devfs_allocvp(ap
->a_dvp
->v_mount
, ap
->a_vpp
, Plink
,
672 ap
->a_nch
->ncp
->nc_name
, dnode
, NULL
);
674 targetlen
= strlen(ap
->a_target
);
676 node
= DEVFS_NODE(*ap
->a_vpp
);
677 node
->flags
|= DEVFS_USER_CREATED
;
678 node
->symlink_namelen
= targetlen
;
679 node
->symlink_name
= kmalloc(targetlen
+ 1, M_DEVFS
, M_WAITOK
);
680 memcpy(node
->symlink_name
, ap
->a_target
, targetlen
);
681 node
->symlink_name
[targetlen
] = '\0';
682 cache_setunresolved(ap
->a_nch
);
683 cache_setvp(ap
->a_nch
, *ap
->a_vpp
);
685 lockmgr(&devfs_lock
, LK_RELEASE
);
687 return ((*ap
->a_vpp
== NULL
) ? ENOTDIR
: 0);
692 devfs_nremove(struct vop_nremove_args
*ap
)
694 struct devfs_node
*dnode
= DEVFS_NODE(ap
->a_dvp
);
695 struct devfs_node
*node
;
696 struct namecache
*ncp
;
699 ncp
= ap
->a_nch
->ncp
;
701 if (!devfs_node_is_accessible(dnode
))
704 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
706 if ((dnode
->node_type
!= Proot
) && (dnode
->node_type
!= Pdir
))
709 TAILQ_FOREACH(node
, DEVFS_DENODE_HEAD(dnode
), link
) {
710 if (ncp
->nc_nlen
!= node
->d_dir
.d_namlen
)
712 if (memcmp(ncp
->nc_name
, node
->d_dir
.d_name
, ncp
->nc_nlen
))
716 * only allow removal of user created stuff (e.g. symlinks)
718 if ((node
->flags
& DEVFS_USER_CREATED
) == 0) {
723 cache_inval_vp(node
->v_node
, CINV_DESTROY
);
730 cache_setunresolved(ap
->a_nch
);
731 cache_setvp(ap
->a_nch
, NULL
);
734 lockmgr(&devfs_lock
, LK_RELEASE
);
740 devfs_spec_open(struct vop_open_args
*ap
)
742 struct vnode
*vp
= ap
->a_vp
;
743 struct vnode
*orig_vp
= NULL
;
744 struct devfs_node
*node
= DEVFS_NODE(vp
);
745 struct devfs_node
*newnode
;
746 cdev_t dev
, ndev
= NULL
;
750 if (node
->d_dev
== NULL
)
752 if (!devfs_node_is_accessible(node
))
756 if ((dev
= vp
->v_rdev
) == NULL
)
759 if (node
&& ap
->a_fp
) {
760 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_spec_open: -1.1-\n");
761 lockmgr(&devfs_lock
, LK_EXCLUSIVE
);
763 ndev
= devfs_clone(dev
, node
->d_dir
.d_name
, node
->d_dir
.d_namlen
,
764 ap
->a_mode
, ap
->a_cred
);
766 newnode
= devfs_create_device_node(
767 DEVFS_MNTDATA(vp
->v_mount
)->root_node
,
769 /* XXX: possibly destroy device if this happens */
771 if (newnode
!= NULL
) {
775 devfs_debug(DEVFS_DEBUG_DEBUG
,
776 "parent here is: %s, node is: |%s|\n",
777 ((node
->parent
->node_type
== Proot
) ?
778 "ROOT!" : node
->parent
->d_dir
.d_name
),
779 newnode
->d_dir
.d_name
);
780 devfs_debug(DEVFS_DEBUG_DEBUG
,
782 ((struct devfs_node
*)(TAILQ_LAST(DEVFS_DENODE_HEAD(node
->parent
), devfs_node_head
)))->d_dir
.d_name
);
785 * orig_vp is set to the original vp if we cloned.
787 /* node->flags |= DEVFS_CLONED; */
788 devfs_allocv(&vp
, newnode
);
793 lockmgr(&devfs_lock
, LK_RELEASE
);
796 devfs_debug(DEVFS_DEBUG_DEBUG
,
797 "devfs_spec_open() called on %s! \n",
801 * Make this field valid before any I/O in ->d_open
803 if (!dev
->si_iosize_max
)
804 dev
->si_iosize_max
= DFLTPHYS
;
806 if (dev_dflags(dev
) & D_TTY
)
807 vp
->v_flag
|= VISTTY
;
810 error
= dev_dopen(dev
, ap
->a_mode
, S_IFCHR
, ap
->a_cred
);
811 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
814 * Clean up any cloned vp if we error out.
820 /* orig_vp = NULL; */
826 if (dev_dflags(dev
) & D_TTY
) {
831 devfs_debug(DEVFS_DEBUG_DEBUG
,
832 "devfs: no t_stop\n");
833 tp
->t_stop
= nottystop
;
839 if (vn_isdisk(vp
, NULL
)) {
840 if (!dev
->si_bsize_phys
)
841 dev
->si_bsize_phys
= DEV_BSIZE
;
842 vinitvmio(vp
, IDX_TO_OFF(INT_MAX
));
848 nanotime(&node
->atime
);
854 /* Ugly pty magic, to make pty devices appear once they are opened */
855 if (node
&& (node
->flags
& DEVFS_PTY
) == DEVFS_PTY
)
856 node
->flags
&= ~DEVFS_INVISIBLE
;
859 ap
->a_fp
->f_type
= DTYPE_VNODE
;
860 ap
->a_fp
->f_flag
= ap
->a_mode
& FMASK
;
861 ap
->a_fp
->f_ops
= &devfs_dev_fileops
;
862 ap
->a_fp
->f_data
= vp
;
870 devfs_spec_close(struct vop_close_args
*ap
)
872 struct devfs_node
*node
= DEVFS_NODE(ap
->a_vp
);
873 struct proc
*p
= curproc
;
874 struct vnode
*vp
= ap
->a_vp
;
875 cdev_t dev
= vp
->v_rdev
;
879 devfs_debug(DEVFS_DEBUG_DEBUG
,
880 "devfs_spec_close() called on %s! \n",
884 * A couple of hacks for devices and tty devices. The
885 * vnode ref count cannot be used to figure out the
886 * last close, but we can use v_opencount now that
887 * revoke works properly.
889 * Detect the last close on a controlling terminal and clear
890 * the session (half-close).
895 if (p
&& vp
->v_opencount
<= 1 && vp
== p
->p_session
->s_ttyvp
) {
896 p
->p_session
->s_ttyvp
= NULL
;
901 * Vnodes can be opened and closed multiple times. Do not really
902 * close the device unless (1) it is being closed forcibly,
903 * (2) the device wants to track closes, or (3) this is the last
904 * vnode doing its last close on the device.
906 * XXX the VXLOCK (force close) case can leave vnodes referencing
907 * a closed device. This might not occur now that our revoke is
910 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_spec_close() -1- \n");
911 if (dev
&& ((vp
->v_flag
& VRECLAIMED
) ||
912 (dev_dflags(dev
) & D_TRACKCLOSE
) ||
913 (vp
->v_opencount
== 1))) {
915 * Unlock around dev_dclose()
918 if (vn_islocked(vp
)) {
922 error
= dev_dclose(dev
, ap
->a_fflag
, S_IFCHR
);
925 * Ugly pty magic, to make pty devices disappear again once
928 if (node
&& (node
->flags
& DEVFS_PTY
) == DEVFS_PTY
)
929 node
->flags
|= DEVFS_INVISIBLE
;
932 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
936 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_spec_close() -2- \n");
939 * Track the actual opens and closes on the vnode. The last close
940 * disassociates the rdev. If the rdev is already disassociated or
941 * the opencount is already 0, the vnode might have been revoked
942 * and no further opencount tracking occurs.
946 if (vp
->v_opencount
> 0)
954 devfs_specf_close(struct file
*fp
)
956 struct vnode
*vp
= (struct vnode
*)fp
->f_data
;
960 fp
->f_ops
= &badfileops
;
961 error
= vn_close(vp
, fp
->f_flag
);
969 * Device-optimized file table vnode read routine.
971 * This bypasses the VOP table and talks directly to the device. Most
972 * filesystems just route to specfs and can make this optimization.
974 * MPALMOSTSAFE - acquires mplock
977 devfs_specf_read(struct file
*fp
, struct uio
*uio
,
978 struct ucred
*cred
, int flags
)
980 struct devfs_node
*node
;
987 KASSERT(uio
->uio_td
== curthread
,
988 ("uio_td %p is not td %p", uio
->uio_td
, curthread
));
990 vp
= (struct vnode
*)fp
->f_data
;
991 if (vp
== NULL
|| vp
->v_type
== VBAD
) {
995 node
= DEVFS_NODE(vp
);
997 if ((dev
= vp
->v_rdev
) == NULL
) {
1004 if (uio
->uio_resid
== 0) {
1008 if ((flags
& O_FOFFSET
) == 0)
1009 uio
->uio_offset
= fp
->f_offset
;
1012 if (flags
& O_FBLOCKING
) {
1013 /* ioflag &= ~IO_NDELAY; */
1014 } else if (flags
& O_FNONBLOCKING
) {
1015 ioflag
|= IO_NDELAY
;
1016 } else if (fp
->f_flag
& FNONBLOCK
) {
1017 ioflag
|= IO_NDELAY
;
1019 if (flags
& O_FBUFFERED
) {
1020 /* ioflag &= ~IO_DIRECT; */
1021 } else if (flags
& O_FUNBUFFERED
) {
1022 ioflag
|= IO_DIRECT
;
1023 } else if (fp
->f_flag
& O_DIRECT
) {
1024 ioflag
|= IO_DIRECT
;
1026 ioflag
|= sequential_heuristic(uio
, fp
);
1028 error
= dev_dread(dev
, uio
, ioflag
);
1032 nanotime(&node
->atime
);
1033 if ((flags
& O_FOFFSET
) == 0)
1034 fp
->f_offset
= uio
->uio_offset
;
1035 fp
->f_nextoff
= uio
->uio_offset
;
1043 devfs_specf_write(struct file
*fp
, struct uio
*uio
,
1044 struct ucred
*cred
, int flags
)
1046 struct devfs_node
*node
;
1053 KASSERT(uio
->uio_td
== curthread
,
1054 ("uio_td %p is not p %p", uio
->uio_td
, curthread
));
1056 vp
= (struct vnode
*)fp
->f_data
;
1057 if (vp
== NULL
|| vp
->v_type
== VBAD
) {
1061 node
= DEVFS_NODE(vp
);
1062 if (vp
->v_type
== VREG
)
1063 bwillwrite(uio
->uio_resid
);
1064 vp
= (struct vnode
*)fp
->f_data
;
1066 if ((dev
= vp
->v_rdev
) == NULL
) {
1072 if ((flags
& O_FOFFSET
) == 0)
1073 uio
->uio_offset
= fp
->f_offset
;
1076 if (vp
->v_type
== VREG
&&
1077 ((fp
->f_flag
& O_APPEND
) || (flags
& O_FAPPEND
))) {
1078 ioflag
|= IO_APPEND
;
1081 if (flags
& O_FBLOCKING
) {
1082 /* ioflag &= ~IO_NDELAY; */
1083 } else if (flags
& O_FNONBLOCKING
) {
1084 ioflag
|= IO_NDELAY
;
1085 } else if (fp
->f_flag
& FNONBLOCK
) {
1086 ioflag
|= IO_NDELAY
;
1088 if (flags
& O_FBUFFERED
) {
1089 /* ioflag &= ~IO_DIRECT; */
1090 } else if (flags
& O_FUNBUFFERED
) {
1091 ioflag
|= IO_DIRECT
;
1092 } else if (fp
->f_flag
& O_DIRECT
) {
1093 ioflag
|= IO_DIRECT
;
1095 if (flags
& O_FASYNCWRITE
) {
1096 /* ioflag &= ~IO_SYNC; */
1097 } else if (flags
& O_FSYNCWRITE
) {
1099 } else if (fp
->f_flag
& O_FSYNC
) {
1103 if (vp
->v_mount
&& (vp
->v_mount
->mnt_flag
& MNT_SYNCHRONOUS
))
1105 ioflag
|= sequential_heuristic(uio
, fp
);
1107 error
= dev_dwrite(dev
, uio
, ioflag
);
1111 nanotime(&node
->atime
);
1112 nanotime(&node
->mtime
);
1115 if ((flags
& O_FOFFSET
) == 0)
1116 fp
->f_offset
= uio
->uio_offset
;
1117 fp
->f_nextoff
= uio
->uio_offset
;
1125 devfs_specf_stat(struct file
*fp
, struct stat
*sb
, struct ucred
*cred
)
1131 vp
= (struct vnode
*)fp
->f_data
;
1132 error
= vn_stat(vp
, sb
, cred
);
1144 error
= VOP_GETATTR(vp
, vap
);
1151 * Zero the spare stat fields
1157 * Copy from vattr table ... or not in case it's a cloned device
1159 if (vap
->va_fsid
!= VNOVAL
)
1160 sb
->st_dev
= vap
->va_fsid
;
1162 sb
->st_dev
= vp
->v_mount
->mnt_stat
.f_fsid
.val
[0];
1164 sb
->st_ino
= vap
->va_fileid
;
1166 mode
= vap
->va_mode
;
1170 if (vap
->va_nlink
> (nlink_t
)-1)
1171 sb
->st_nlink
= (nlink_t
)-1;
1173 sb
->st_nlink
= vap
->va_nlink
;
1174 sb
->st_uid
= vap
->va_uid
;
1175 sb
->st_gid
= vap
->va_gid
;
1176 sb
->st_rdev
= dev2udev(DEVFS_NODE(vp
)->d_dev
);
1177 sb
->st_size
= vap
->va_size
;
1178 sb
->st_atimespec
= vap
->va_atime
;
1179 sb
->st_mtimespec
= vap
->va_mtime
;
1180 sb
->st_ctimespec
= vap
->va_ctime
;
1183 * A VCHR and VBLK device may track the last access and last modified
1184 * time independantly of the filesystem. This is particularly true
1185 * because device read and write calls may bypass the filesystem.
1187 if (vp
->v_type
== VCHR
|| vp
->v_type
== VBLK
) {
1190 if (dev
->si_lastread
) {
1191 sb
->st_atimespec
.tv_sec
= dev
->si_lastread
;
1192 sb
->st_atimespec
.tv_nsec
= 0;
1194 if (dev
->si_lastwrite
) {
1195 sb
->st_atimespec
.tv_sec
= dev
->si_lastwrite
;
1196 sb
->st_atimespec
.tv_nsec
= 0;
1202 * According to www.opengroup.org, the meaning of st_blksize is
1203 * "a filesystem-specific preferred I/O block size for this
1204 * object. In some filesystem types, this may vary from file
1206 * Default to PAGE_SIZE after much discussion.
1209 sb
->st_blksize
= PAGE_SIZE
;
1211 sb
->st_flags
= vap
->va_flags
;
1213 error
= priv_check_cred(cred
, PRIV_VFS_GENERATION
, 0);
1217 sb
->st_gen
= (u_int32_t
)vap
->va_gen
;
1219 sb
->st_blocks
= vap
->va_bytes
/ S_BLKSIZE
;
1220 sb
->st_fsmid
= vap
->va_fsmid
;
1228 devfs_specf_kqfilter(struct file
*fp
, struct knote
*kn
)
1230 struct devfs_node
*node
;
1237 vp
= (struct vnode
*)fp
->f_data
;
1238 if (vp
== NULL
|| vp
->v_type
== VBAD
) {
1242 node
= DEVFS_NODE(vp
);
1244 if ((dev
= vp
->v_rdev
) == NULL
) {
1250 error
= dev_dkqfilter(dev
, kn
);
1261 devfs_specf_poll(struct file
*fp
, int events
, struct ucred
*cred
)
1263 struct devfs_node
*node
;
1270 vp
= (struct vnode
*)fp
->f_data
;
1271 if (vp
== NULL
|| vp
->v_type
== VBAD
) {
1275 node
= DEVFS_NODE(vp
);
1277 if ((dev
= vp
->v_rdev
) == NULL
) {
1282 error
= dev_dpoll(dev
, events
);
1288 nanotime(&node
->atime
);
1297 * MPALMOSTSAFE - acquires mplock
1300 devfs_specf_ioctl(struct file
*fp
, u_long com
, caddr_t data
,
1301 struct ucred
*ucred
, struct sysmsg
*msg
)
1303 struct devfs_node
*node
;
1308 struct fiodname_args
*name_args
;
1313 vp
= ((struct vnode
*)fp
->f_data
);
1314 if ((dev
= vp
->v_rdev
) == NULL
) {
1315 error
= EBADF
; /* device was revoked */
1319 node
= DEVFS_NODE(vp
);
1321 devfs_debug(DEVFS_DEBUG_DEBUG
,
1322 "devfs_specf_ioctl() called! for dev %s\n",
1325 if (com
== FIODTYPE
) {
1326 *(int *)data
= dev_dflags(dev
) & D_TYPEMASK
;
1329 } else if (com
== FIODNAME
) {
1330 name_args
= (struct fiodname_args
*)data
;
1331 name
= dev
->si_name
;
1332 namlen
= strlen(name
) + 1;
1334 devfs_debug(DEVFS_DEBUG_DEBUG
,
1335 "ioctl, got: FIODNAME for %s\n", name
);
1337 if (namlen
<= name_args
->len
)
1338 error
= copyout(dev
->si_name
, name_args
->name
, namlen
);
1342 devfs_debug(DEVFS_DEBUG_DEBUG
,
1343 "ioctl stuff: error: %d\n", error
);
1347 error
= dev_dioctl(dev
, com
, data
, fp
->f_flag
, ucred
, msg
);
1351 nanotime(&node
->atime
);
1352 nanotime(&node
->mtime
);
1356 if (com
== TIOCSCTTY
) {
1357 devfs_debug(DEVFS_DEBUG_DEBUG
,
1358 "devfs_specf_ioctl: got TIOCSCTTY on %s\n",
1361 if (error
== 0 && com
== TIOCSCTTY
) {
1362 struct proc
*p
= curthread
->td_proc
;
1363 struct session
*sess
;
1365 devfs_debug(DEVFS_DEBUG_DEBUG
,
1366 "devfs_specf_ioctl: dealing with TIOCSCTTY on %s\n",
1372 sess
= p
->p_session
;
1375 * Do nothing if reassigning same control tty
1377 if (sess
->s_ttyvp
== vp
) {
1383 * Get rid of reference to old control tty
1385 ovp
= sess
->s_ttyvp
;
1394 devfs_debug(DEVFS_DEBUG_DEBUG
, "devfs_specf_ioctl() finished! \n");
1400 devfs_spec_fsync(struct vop_fsync_args
*ap
)
1402 struct vnode
*vp
= ap
->a_vp
;
1405 if (!vn_isdisk(vp
, NULL
))
1409 * Flush all dirty buffers associated with a block device.
1411 error
= vfsync(vp
, ap
->a_waitfor
, 10000, NULL
, NULL
);
1416 devfs_spec_read(struct vop_read_args
*ap
)
1418 struct devfs_node
*node
;
1427 node
= DEVFS_NODE(vp
);
1429 if (dev
== NULL
) /* device was revoked */
1431 if (uio
->uio_resid
== 0)
1435 error
= dev_dread(dev
, uio
, ap
->a_ioflag
);
1436 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1439 nanotime(&node
->atime
);
1445 * Vnode op for write
1447 * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
1448 * struct ucred *a_cred)
1451 devfs_spec_write(struct vop_write_args
*ap
)
1453 struct devfs_node
*node
;
1462 node
= DEVFS_NODE(vp
);
1464 KKASSERT(uio
->uio_segflg
!= UIO_NOCOPY
);
1466 if (dev
== NULL
) /* device was revoked */
1470 error
= dev_dwrite(dev
, uio
, ap
->a_ioflag
);
1471 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1474 nanotime(&node
->atime
);
1475 nanotime(&node
->mtime
);
1482 * Device ioctl operation.
1484 * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
1485 * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
1488 devfs_spec_ioctl(struct vop_ioctl_args
*ap
)
1490 struct vnode
*vp
= ap
->a_vp
;
1491 struct devfs_node
*node
;
1494 if ((dev
= vp
->v_rdev
) == NULL
)
1495 return (EBADF
); /* device was revoked */
1496 node
= DEVFS_NODE(vp
);
1500 nanotime(&node
->atime
);
1501 nanotime(&node
->mtime
);
1505 return (dev_dioctl(dev
, ap
->a_command
, ap
->a_data
, ap
->a_fflag
,
1506 ap
->a_cred
, ap
->a_sysmsg
));
1510 * spec_poll(struct vnode *a_vp, int a_events, struct ucred *a_cred)
1514 devfs_spec_poll(struct vop_poll_args
*ap
)
1516 struct vnode
*vp
= ap
->a_vp
;
1517 struct devfs_node
*node
;
1520 if ((dev
= vp
->v_rdev
) == NULL
)
1521 return (EBADF
); /* device was revoked */
1522 node
= DEVFS_NODE(vp
);
1526 nanotime(&node
->atime
);
1529 return (dev_dpoll(dev
, ap
->a_events
));
1533 * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
1537 devfs_spec_kqfilter(struct vop_kqfilter_args
*ap
)
1539 struct vnode
*vp
= ap
->a_vp
;
1540 struct devfs_node
*node
;
1543 if ((dev
= vp
->v_rdev
) == NULL
)
1544 return (EBADF
); /* device was revoked */
1545 node
= DEVFS_NODE(vp
);
1549 nanotime(&node
->atime
);
1552 return (dev_dkqfilter(dev
, ap
->a_kn
));
1556 * Convert a vnode strategy call into a device strategy call. Vnode strategy
1557 * calls are not limited to device DMA limits so we have to deal with the
1560 * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
1563 devfs_spec_strategy(struct vop_strategy_args
*ap
)
1565 struct bio
*bio
= ap
->a_bio
;
1566 struct buf
*bp
= bio
->bio_buf
;
1573 if (bp
->b_cmd
!= BUF_CMD_READ
&& LIST_FIRST(&bp
->b_dep
) != NULL
)
1577 * Collect statistics on synchronous and asynchronous read
1578 * and write counts for disks that have associated filesystems.
1581 KKASSERT(vp
->v_rdev
!= NULL
); /* XXX */
1582 if (vn_isdisk(vp
, NULL
) && (mp
= vp
->v_rdev
->si_mountpoint
) != NULL
) {
1583 if (bp
->b_cmd
== BUF_CMD_READ
) {
1584 if (bp
->b_flags
& BIO_SYNC
)
1585 mp
->mnt_stat
.f_syncreads
++;
1587 mp
->mnt_stat
.f_asyncreads
++;
1589 if (bp
->b_flags
& BIO_SYNC
)
1590 mp
->mnt_stat
.f_syncwrites
++;
1592 mp
->mnt_stat
.f_asyncwrites
++;
1597 * Device iosize limitations only apply to read and write. Shortcut
1598 * the I/O if it fits.
1600 if ((maxiosize
= vp
->v_rdev
->si_iosize_max
) == 0) {
1601 devfs_debug(DEVFS_DEBUG_DEBUG
,
1602 "%s: si_iosize_max not set!\n",
1603 dev_dname(vp
->v_rdev
));
1604 maxiosize
= MAXPHYS
;
1606 #if SPEC_CHAIN_DEBUG & 2
1609 if (bp
->b_bcount
<= maxiosize
||
1610 (bp
->b_cmd
!= BUF_CMD_READ
&& bp
->b_cmd
!= BUF_CMD_WRITE
)) {
1611 dev_dstrategy_chain(vp
->v_rdev
, bio
);
1616 * Clone the buffer and set up an I/O chain to chunk up the I/O.
1618 nbp
= kmalloc(sizeof(*bp
), M_DEVBUF
, M_INTWAIT
|M_ZERO
);
1622 BUF_LOCK(nbp
, LK_EXCLUSIVE
);
1625 nbp
->b_flags
= B_PAGING
| (bp
->b_flags
& B_BNOCLIP
);
1626 nbp
->b_data
= bp
->b_data
;
1627 nbp
->b_bio1
.bio_done
= devfs_spec_strategy_done
;
1628 nbp
->b_bio1
.bio_offset
= bio
->bio_offset
;
1629 nbp
->b_bio1
.bio_caller_info1
.ptr
= bio
;
1632 * Start the first transfer
1634 if (vn_isdisk(vp
, NULL
))
1635 chunksize
= vp
->v_rdev
->si_bsize_phys
;
1637 chunksize
= DEV_BSIZE
;
1638 chunksize
= maxiosize
/ chunksize
* chunksize
;
1639 #if SPEC_CHAIN_DEBUG & 1
1640 devfs_debug(DEVFS_DEBUG_DEBUG
,
1641 "spec_strategy chained I/O chunksize=%d\n",
1644 nbp
->b_cmd
= bp
->b_cmd
;
1645 nbp
->b_bcount
= chunksize
;
1646 nbp
->b_bufsize
= chunksize
; /* used to detect a short I/O */
1647 nbp
->b_bio1
.bio_caller_info2
.index
= chunksize
;
1649 #if SPEC_CHAIN_DEBUG & 1
1650 devfs_debug(DEVFS_DEBUG_DEBUG
,
1651 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1652 bp
, 0, bp
->b_bcount
, nbp
->b_bcount
);
1655 dev_dstrategy(vp
->v_rdev
, &nbp
->b_bio1
);
1657 if (DEVFS_NODE(vp
)) {
1658 nanotime(&DEVFS_NODE(vp
)->atime
);
1659 nanotime(&DEVFS_NODE(vp
)->mtime
);
1666 * Chunked up transfer completion routine - chain transfers until done
1670 devfs_spec_strategy_done(struct bio
*nbio
)
1672 struct buf
*nbp
= nbio
->bio_buf
;
1673 struct bio
*bio
= nbio
->bio_caller_info1
.ptr
; /* original bio */
1674 struct buf
*bp
= bio
->bio_buf
; /* original bp */
1675 int chunksize
= nbio
->bio_caller_info2
.index
; /* chunking */
1676 int boffset
= nbp
->b_data
- bp
->b_data
;
1678 if (nbp
->b_flags
& B_ERROR
) {
1680 * An error terminates the chain, propogate the error back
1681 * to the original bp
1683 bp
->b_flags
|= B_ERROR
;
1684 bp
->b_error
= nbp
->b_error
;
1685 bp
->b_resid
= bp
->b_bcount
- boffset
+
1686 (nbp
->b_bcount
- nbp
->b_resid
);
1687 #if SPEC_CHAIN_DEBUG & 1
1688 devfs_debug(DEVFS_DEBUG_DEBUG
,
1689 "spec_strategy: chain %p error %d bcount %d/%d\n",
1690 bp
, bp
->b_error
, bp
->b_bcount
,
1691 bp
->b_bcount
- bp
->b_resid
);
1693 kfree(nbp
, M_DEVBUF
);
1695 } else if (nbp
->b_resid
) {
1697 * A short read or write terminates the chain
1699 bp
->b_error
= nbp
->b_error
;
1700 bp
->b_resid
= bp
->b_bcount
- boffset
+
1701 (nbp
->b_bcount
- nbp
->b_resid
);
1702 #if SPEC_CHAIN_DEBUG & 1
1703 devfs_debug(DEVFS_DEBUG_DEBUG
,
1704 "spec_strategy: chain %p short read(1) "
1706 bp
, bp
->b_bcount
- bp
->b_resid
, bp
->b_bcount
);
1708 kfree(nbp
, M_DEVBUF
);
1710 } else if (nbp
->b_bcount
!= nbp
->b_bufsize
) {
1712 * A short read or write can also occur by truncating b_bcount
1714 #if SPEC_CHAIN_DEBUG & 1
1715 devfs_debug(DEVFS_DEBUG_DEBUG
,
1716 "spec_strategy: chain %p short read(2) "
1718 bp
, nbp
->b_bcount
+ boffset
, bp
->b_bcount
);
1721 bp
->b_bcount
= nbp
->b_bcount
+ boffset
;
1722 bp
->b_resid
= nbp
->b_resid
;
1723 kfree(nbp
, M_DEVBUF
);
1725 } else if (nbp
->b_bcount
+ boffset
== bp
->b_bcount
) {
1727 * No more data terminates the chain
1729 #if SPEC_CHAIN_DEBUG & 1
1730 devfs_debug(DEVFS_DEBUG_DEBUG
,
1731 "spec_strategy: chain %p finished bcount %d\n",
1736 kfree(nbp
, M_DEVBUF
);
1740 * Continue the chain
1742 boffset
+= nbp
->b_bcount
;
1743 nbp
->b_data
= bp
->b_data
+ boffset
;
1744 nbp
->b_bcount
= bp
->b_bcount
- boffset
;
1745 if (nbp
->b_bcount
> chunksize
)
1746 nbp
->b_bcount
= chunksize
;
1747 nbp
->b_bio1
.bio_done
= devfs_spec_strategy_done
;
1748 nbp
->b_bio1
.bio_offset
= bio
->bio_offset
+ boffset
;
1750 #if SPEC_CHAIN_DEBUG & 1
1751 devfs_debug(DEVFS_DEBUG_DEBUG
,
1752 "spec_strategy: chain %p offset %d/%d bcount %d\n",
1753 bp
, boffset
, bp
->b_bcount
, nbp
->b_bcount
);
1756 dev_dstrategy(nbp
->b_vp
->v_rdev
, &nbp
->b_bio1
);
1761 * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
1764 devfs_spec_freeblks(struct vop_freeblks_args
*ap
)
1769 * XXX: This assumes that strategy does the deed right away.
1770 * XXX: this may not be TRTTD.
1772 KKASSERT(ap
->a_vp
->v_rdev
!= NULL
);
1773 if ((dev_dflags(ap
->a_vp
->v_rdev
) & D_CANFREE
) == 0)
1775 bp
= geteblk(ap
->a_length
);
1776 bp
->b_cmd
= BUF_CMD_FREEBLKS
;
1777 bp
->b_bio1
.bio_offset
= ap
->a_offset
;
1778 bp
->b_bcount
= ap
->a_length
;
1779 dev_dstrategy(ap
->a_vp
->v_rdev
, &bp
->b_bio1
);
1784 * Implement degenerate case where the block requested is the block
1785 * returned, and assume that the entire device is contiguous in regards
1786 * to the contiguous block range (runp and runb).
1788 * spec_bmap(struct vnode *a_vp, off_t a_loffset,
1789 * off_t *a_doffsetp, int *a_runp, int *a_runb)
1792 devfs_spec_bmap(struct vop_bmap_args
*ap
)
1794 if (ap
->a_doffsetp
!= NULL
)
1795 *ap
->a_doffsetp
= ap
->a_loffset
;
1796 if (ap
->a_runp
!= NULL
)
1797 *ap
->a_runp
= MAXBSIZE
;
1798 if (ap
->a_runb
!= NULL
) {
1799 if (ap
->a_loffset
< MAXBSIZE
)
1800 *ap
->a_runb
= (int)ap
->a_loffset
;
1802 *ap
->a_runb
= MAXBSIZE
;
1809 * Special device advisory byte-level locks.
1811 * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
1812 * struct flock *a_fl, int a_flags)
1816 devfs_spec_advlock(struct vop_advlock_args
*ap
)
1818 return ((ap
->a_flags
& F_POSIX
) ? EINVAL
: EOPNOTSUPP
);
1822 devfs_spec_getpages_iodone(struct bio
*bio
)
1824 bio
->bio_buf
->b_cmd
= BUF_CMD_DONE
;
1825 wakeup(bio
->bio_buf
);
1829 * spec_getpages() - get pages associated with device vnode.
1831 * Note that spec_read and spec_write do not use the buffer cache, so we
1832 * must fully implement getpages here.
1835 devfs_spec_getpages(struct vop_getpages_args
*ap
)
1839 int i
, pcount
, size
;
1842 vm_ooffset_t offset
;
1843 int toff
, nextoff
, nread
;
1844 struct vnode
*vp
= ap
->a_vp
;
1849 pcount
= round_page(ap
->a_count
) / PAGE_SIZE
;
1852 * Calculate the offset of the transfer and do sanity check.
1854 offset
= IDX_TO_OFF(ap
->a_m
[0]->pindex
) + ap
->a_offset
;
1857 * Round up physical size for real devices. We cannot round using
1858 * v_mount's block size data because v_mount has nothing to do with
1859 * the device. i.e. it's usually '/dev'. We need the physical block
1860 * size for the device itself.
1862 * We can't use v_rdev->si_mountpoint because it only exists when the
1863 * block device is mounted. However, we can use v_rdev.
1865 if (vn_isdisk(vp
, NULL
))
1866 blksiz
= vp
->v_rdev
->si_bsize_phys
;
1870 size
= (ap
->a_count
+ blksiz
- 1) & ~(blksiz
- 1);
1873 kva
= (vm_offset_t
)bp
->b_data
;
1876 * Map the pages to be read into the kva.
1878 pmap_qenter(kva
, ap
->a_m
, pcount
);
1880 /* Build a minimal buffer header. */
1881 bp
->b_cmd
= BUF_CMD_READ
;
1882 bp
->b_bcount
= size
;
1884 bp
->b_runningbufspace
= size
;
1886 runningbufspace
+= bp
->b_runningbufspace
;
1890 bp
->b_bio1
.bio_offset
= offset
;
1891 bp
->b_bio1
.bio_done
= devfs_spec_getpages_iodone
;
1893 mycpu
->gd_cnt
.v_vnodein
++;
1894 mycpu
->gd_cnt
.v_vnodepgsin
+= pcount
;
1897 vn_strategy(ap
->a_vp
, &bp
->b_bio1
);
1901 /* We definitely need to be at splbio here. */
1902 while (bp
->b_cmd
!= BUF_CMD_DONE
)
1903 tsleep(bp
, 0, "spread", 0);
1907 if (bp
->b_flags
& B_ERROR
) {
1909 error
= bp
->b_error
;
1915 * If EOF is encountered we must zero-extend the result in order
1916 * to ensure that the page does not contain garabge. When no
1917 * error occurs, an early EOF is indicated if b_bcount got truncated.
1918 * b_resid is relative to b_bcount and should be 0, but some devices
1919 * might indicate an EOF with b_resid instead of truncating b_bcount.
1921 nread
= bp
->b_bcount
- bp
->b_resid
;
1922 if (nread
< ap
->a_count
)
1923 bzero((caddr_t
)kva
+ nread
, ap
->a_count
- nread
);
1924 pmap_qremove(kva
, pcount
);
1927 for (i
= 0, toff
= 0; i
< pcount
; i
++, toff
= nextoff
) {
1928 nextoff
= toff
+ PAGE_SIZE
;
1931 m
->flags
&= ~PG_ZERO
;
1933 if (nextoff
<= nread
) {
1934 m
->valid
= VM_PAGE_BITS_ALL
;
1936 } else if (toff
< nread
) {
1938 * Since this is a VM request, we have to supply the
1939 * unaligned offset to allow vm_page_set_validclean()
1940 * to zero sub-DEV_BSIZE'd portions of the page.
1942 vm_page_set_valid(m
, 0, nread
- toff
);
1943 vm_page_clear_dirty_end_nonincl(m
, 0, nread
- toff
);
1949 if (i
!= ap
->a_reqpage
) {
1951 * Just in case someone was asking for this page we
1952 * now tell them that it is ok to use.
1954 if (!error
|| (m
->valid
== VM_PAGE_BITS_ALL
)) {
1956 if (m
->flags
& PG_WANTED
) {
1957 vm_page_activate(m
);
1959 vm_page_deactivate(m
);
1968 } else if (m
->valid
) {
1971 * Since this is a VM request, we need to make the
1972 * entire page presentable by zeroing invalid sections.
1974 if (m
->valid
!= VM_PAGE_BITS_ALL
)
1975 vm_page_zero_invalid(m
, FALSE
);
1979 m
= ap
->a_m
[ap
->a_reqpage
];
1980 devfs_debug(DEVFS_DEBUG_WARNING
,
1981 "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
1982 devtoname(vp
->v_rdev
), error
, bp
, bp
->b_vp
);
1983 devfs_debug(DEVFS_DEBUG_WARNING
,
1984 " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
1985 size
, bp
->b_resid
, ap
->a_count
, m
->valid
);
1986 devfs_debug(DEVFS_DEBUG_WARNING
,
1987 " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
1988 nread
, ap
->a_reqpage
, (u_long
)m
->pindex
, pcount
);
1990 * Free the buffer header back to the swap buffer pool.
1993 return VM_PAGER_ERROR
;
1996 * Free the buffer header back to the swap buffer pool.
1999 if (DEVFS_NODE(ap
->a_vp
))
2000 nanotime(&DEVFS_NODE(ap
->a_vp
)->mtime
);
2006 sequential_heuristic(struct uio
*uio
, struct file
*fp
)
2009 * Sequential heuristic - detect sequential operation
2011 if ((uio
->uio_offset
== 0 && fp
->f_seqcount
> 0) ||
2012 uio
->uio_offset
== fp
->f_nextoff
) {
2014 * XXX we assume that the filesystem block size is
2015 * the default. Not true, but still gives us a pretty
2016 * good indicator of how sequential the read operations
2019 int tmpseq
= fp
->f_seqcount
;
2021 tmpseq
+= (uio
->uio_resid
+ BKVASIZE
- 1) / BKVASIZE
;
2022 if (tmpseq
> IO_SEQMAX
)
2024 fp
->f_seqcount
= tmpseq
;
2025 return(fp
->f_seqcount
<< IO_SEQSHIFT
);
2029 * Not sequential, quick draw-down of seqcount
2031 if (fp
->f_seqcount
> 1)