121864bc5SMatthew Dillon /*
29f889dc4SMatthew Dillon * (MPSAFE)
39f889dc4SMatthew Dillon *
421864bc5SMatthew Dillon * Copyright (c) 2009 The DragonFly Project. All rights reserved.
521864bc5SMatthew Dillon *
621864bc5SMatthew Dillon * This code is derived from software contributed to The DragonFly Project
721864bc5SMatthew Dillon * by Alex Hornung <ahornung@gmail.com>
821864bc5SMatthew Dillon *
921864bc5SMatthew Dillon * Redistribution and use in source and binary forms, with or without
1021864bc5SMatthew Dillon * modification, are permitted provided that the following conditions
1121864bc5SMatthew Dillon * are met:
1221864bc5SMatthew Dillon *
1321864bc5SMatthew Dillon * 1. Redistributions of source code must retain the above copyright
1421864bc5SMatthew Dillon * notice, this list of conditions and the following disclaimer.
1521864bc5SMatthew Dillon * 2. Redistributions in binary form must reproduce the above copyright
1621864bc5SMatthew Dillon * notice, this list of conditions and the following disclaimer in
1721864bc5SMatthew Dillon * the documentation and/or other materials provided with the
1821864bc5SMatthew Dillon * distribution.
1921864bc5SMatthew Dillon * 3. Neither the name of The DragonFly Project nor the names of its
2021864bc5SMatthew Dillon * contributors may be used to endorse or promote products derived
2121864bc5SMatthew Dillon * from this software without specific, prior written permission.
2221864bc5SMatthew Dillon *
2321864bc5SMatthew Dillon * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2421864bc5SMatthew Dillon * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2521864bc5SMatthew Dillon * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2621864bc5SMatthew Dillon * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2721864bc5SMatthew Dillon * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2821864bc5SMatthew Dillon * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
2921864bc5SMatthew Dillon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
3021864bc5SMatthew Dillon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
3121864bc5SMatthew Dillon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
3221864bc5SMatthew Dillon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
3321864bc5SMatthew Dillon * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3421864bc5SMatthew Dillon * SUCH DAMAGE.
3521864bc5SMatthew Dillon */
3621864bc5SMatthew Dillon #include <sys/param.h>
3721864bc5SMatthew Dillon #include <sys/systm.h>
3821864bc5SMatthew Dillon #include <sys/time.h>
3921864bc5SMatthew Dillon #include <sys/kernel.h>
4021864bc5SMatthew Dillon #include <sys/lock.h>
4121864bc5SMatthew Dillon #include <sys/fcntl.h>
4221864bc5SMatthew Dillon #include <sys/proc.h>
432b3f93eaSMatthew Dillon #include <sys/caps.h>
4421864bc5SMatthew Dillon #include <sys/signalvar.h>
4521864bc5SMatthew Dillon #include <sys/vnode.h>
4621864bc5SMatthew Dillon #include <sys/uio.h>
4721864bc5SMatthew Dillon #include <sys/mount.h>
4821864bc5SMatthew Dillon #include <sys/file.h>
4921864bc5SMatthew Dillon #include <sys/dirent.h>
5021864bc5SMatthew Dillon #include <sys/malloc.h>
5121864bc5SMatthew Dillon #include <sys/stat.h>
5221864bc5SMatthew Dillon #include <sys/reg.h>
5321864bc5SMatthew Dillon #include <vm/vm_pager.h>
5421864bc5SMatthew Dillon #include <vm/vm_zone.h>
5521864bc5SMatthew Dillon #include <vm/vm_object.h>
5621864bc5SMatthew Dillon #include <sys/filio.h>
5721864bc5SMatthew Dillon #include <sys/ttycom.h>
5821864bc5SMatthew Dillon #include <sys/tty.h>
592d076755SAlex Hornung #include <sys/diskslice.h>
603a1032a6SAlex Hornung #include <sys/sysctl.h>
612c1e28ddSAlex Hornung #include <sys/devfs.h>
6221864bc5SMatthew Dillon #include <sys/pioctl.h>
63c705e298SSamuel J. Greear #include <vfs/fifofs/fifo.h>
6421864bc5SMatthew Dillon
6521864bc5SMatthew Dillon #include <machine/limits.h>
66684a93c4SMatthew Dillon
671a54183bSMatthew Dillon #include <sys/buf2.h>
68684a93c4SMatthew Dillon #include <vm/vm_page2.h>
6921864bc5SMatthew Dillon
70752b2d38SSascha Wildner #ifndef SPEC_CHAIN_DEBUG
71752b2d38SSascha Wildner #define SPEC_CHAIN_DEBUG 0
72752b2d38SSascha Wildner #endif
73752b2d38SSascha Wildner
7421864bc5SMatthew Dillon MALLOC_DECLARE(M_DEVFS);
759f889dc4SMatthew Dillon #define DEVFS_BADOP (void *)devfs_vop_badop
7621864bc5SMatthew Dillon
779f889dc4SMatthew Dillon static int devfs_vop_badop(struct vop_generic_args *);
789f889dc4SMatthew Dillon static int devfs_vop_access(struct vop_access_args *);
799f889dc4SMatthew Dillon static int devfs_vop_inactive(struct vop_inactive_args *);
809f889dc4SMatthew Dillon static int devfs_vop_reclaim(struct vop_reclaim_args *);
819f889dc4SMatthew Dillon static int devfs_vop_readdir(struct vop_readdir_args *);
829f889dc4SMatthew Dillon static int devfs_vop_getattr(struct vop_getattr_args *);
839f889dc4SMatthew Dillon static int devfs_vop_setattr(struct vop_setattr_args *);
849f889dc4SMatthew Dillon static int devfs_vop_readlink(struct vop_readlink_args *);
859f889dc4SMatthew Dillon static int devfs_vop_print(struct vop_print_args *);
8621864bc5SMatthew Dillon
879f889dc4SMatthew Dillon static int devfs_vop_nresolve(struct vop_nresolve_args *);
889f889dc4SMatthew Dillon static int devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
899f889dc4SMatthew Dillon static int devfs_vop_nmkdir(struct vop_nmkdir_args *);
909f889dc4SMatthew Dillon static int devfs_vop_nsymlink(struct vop_nsymlink_args *);
919f889dc4SMatthew Dillon static int devfs_vop_nrmdir(struct vop_nrmdir_args *);
929f889dc4SMatthew Dillon static int devfs_vop_nremove(struct vop_nremove_args *);
9321864bc5SMatthew Dillon
9421864bc5SMatthew Dillon static int devfs_spec_open(struct vop_open_args *);
9521864bc5SMatthew Dillon static int devfs_spec_close(struct vop_close_args *);
9621864bc5SMatthew Dillon static int devfs_spec_fsync(struct vop_fsync_args *);
9721864bc5SMatthew Dillon
9821864bc5SMatthew Dillon static int devfs_spec_read(struct vop_read_args *);
9921864bc5SMatthew Dillon static int devfs_spec_write(struct vop_write_args *);
10021864bc5SMatthew Dillon static int devfs_spec_ioctl(struct vop_ioctl_args *);
10121864bc5SMatthew Dillon static int devfs_spec_kqfilter(struct vop_kqfilter_args *);
10221864bc5SMatthew Dillon static int devfs_spec_strategy(struct vop_strategy_args *);
10321864bc5SMatthew Dillon static void devfs_spec_strategy_done(struct bio *);
10421864bc5SMatthew Dillon static int devfs_spec_freeblks(struct vop_freeblks_args *);
10521864bc5SMatthew Dillon static int devfs_spec_bmap(struct vop_bmap_args *);
10621864bc5SMatthew Dillon static int devfs_spec_advlock(struct vop_advlock_args *);
10721864bc5SMatthew Dillon static void devfs_spec_getpages_iodone(struct bio *);
10821864bc5SMatthew Dillon static int devfs_spec_getpages(struct vop_getpages_args *);
10921864bc5SMatthew Dillon
1109f889dc4SMatthew Dillon static int devfs_fo_close(struct file *);
1119f889dc4SMatthew Dillon static int devfs_fo_read(struct file *, struct uio *, struct ucred *, int);
1129f889dc4SMatthew Dillon static int devfs_fo_write(struct file *, struct uio *, struct ucred *, int);
1139f889dc4SMatthew Dillon static int devfs_fo_stat(struct file *, struct stat *, struct ucred *);
1149f889dc4SMatthew Dillon static int devfs_fo_kqfilter(struct file *, struct knote *);
1159f889dc4SMatthew Dillon static int devfs_fo_ioctl(struct file *, u_long, caddr_t,
11687baaf0cSMatthew Dillon struct ucred *, struct sysmsg *);
11721864bc5SMatthew Dillon static __inline int sequential_heuristic(struct uio *, struct file *);
11887baaf0cSMatthew Dillon
11921864bc5SMatthew Dillon extern struct lock devfs_lock;
12021864bc5SMatthew Dillon
12121864bc5SMatthew Dillon /*
1229f889dc4SMatthew Dillon * devfs vnode operations for regular files. All vnode ops are MPSAFE.
12321864bc5SMatthew Dillon */
12421864bc5SMatthew Dillon struct vop_ops devfs_vnode_norm_vops = {
12521864bc5SMatthew Dillon .vop_default = vop_defaultop,
1269f889dc4SMatthew Dillon .vop_access = devfs_vop_access,
12721864bc5SMatthew Dillon .vop_advlock = DEVFS_BADOP,
12821864bc5SMatthew Dillon .vop_bmap = DEVFS_BADOP,
12921864bc5SMatthew Dillon .vop_close = vop_stdclose,
1309f889dc4SMatthew Dillon .vop_getattr = devfs_vop_getattr,
1319f889dc4SMatthew Dillon .vop_inactive = devfs_vop_inactive,
13221864bc5SMatthew Dillon .vop_ncreate = DEVFS_BADOP,
1339f889dc4SMatthew Dillon .vop_nresolve = devfs_vop_nresolve,
1349f889dc4SMatthew Dillon .vop_nlookupdotdot = devfs_vop_nlookupdotdot,
13521864bc5SMatthew Dillon .vop_nlink = DEVFS_BADOP,
1369f889dc4SMatthew Dillon .vop_nmkdir = devfs_vop_nmkdir,
13721864bc5SMatthew Dillon .vop_nmknod = DEVFS_BADOP,
1389f889dc4SMatthew Dillon .vop_nremove = devfs_vop_nremove,
13921864bc5SMatthew Dillon .vop_nrename = DEVFS_BADOP,
1409f889dc4SMatthew Dillon .vop_nrmdir = devfs_vop_nrmdir,
1419f889dc4SMatthew Dillon .vop_nsymlink = devfs_vop_nsymlink,
14221864bc5SMatthew Dillon .vop_open = vop_stdopen,
14321864bc5SMatthew Dillon .vop_pathconf = vop_stdpathconf,
1449f889dc4SMatthew Dillon .vop_print = devfs_vop_print,
14521864bc5SMatthew Dillon .vop_read = DEVFS_BADOP,
1469f889dc4SMatthew Dillon .vop_readdir = devfs_vop_readdir,
1479f889dc4SMatthew Dillon .vop_readlink = devfs_vop_readlink,
1489a20a70dSMatthew Dillon .vop_reallocblks = DEVFS_BADOP,
1499f889dc4SMatthew Dillon .vop_reclaim = devfs_vop_reclaim,
1509f889dc4SMatthew Dillon .vop_setattr = devfs_vop_setattr,
15121864bc5SMatthew Dillon .vop_write = DEVFS_BADOP,
15221864bc5SMatthew Dillon .vop_ioctl = DEVFS_BADOP
15321864bc5SMatthew Dillon };
15421864bc5SMatthew Dillon
15521864bc5SMatthew Dillon /*
1569f889dc4SMatthew Dillon * devfs vnode operations for character devices. All vnode ops are MPSAFE.
15721864bc5SMatthew Dillon */
15821864bc5SMatthew Dillon struct vop_ops devfs_vnode_dev_vops = {
15921864bc5SMatthew Dillon .vop_default = vop_defaultop,
1609f889dc4SMatthew Dillon .vop_access = devfs_vop_access,
16121864bc5SMatthew Dillon .vop_advlock = devfs_spec_advlock,
16221864bc5SMatthew Dillon .vop_bmap = devfs_spec_bmap,
16321864bc5SMatthew Dillon .vop_close = devfs_spec_close,
16421864bc5SMatthew Dillon .vop_freeblks = devfs_spec_freeblks,
16521864bc5SMatthew Dillon .vop_fsync = devfs_spec_fsync,
1669f889dc4SMatthew Dillon .vop_getattr = devfs_vop_getattr,
16721864bc5SMatthew Dillon .vop_getpages = devfs_spec_getpages,
1689f889dc4SMatthew Dillon .vop_inactive = devfs_vop_inactive,
16921864bc5SMatthew Dillon .vop_open = devfs_spec_open,
17021864bc5SMatthew Dillon .vop_pathconf = vop_stdpathconf,
1719f889dc4SMatthew Dillon .vop_print = devfs_vop_print,
17221864bc5SMatthew Dillon .vop_kqfilter = devfs_spec_kqfilter,
17321864bc5SMatthew Dillon .vop_read = devfs_spec_read,
17421864bc5SMatthew Dillon .vop_readdir = DEVFS_BADOP,
17521864bc5SMatthew Dillon .vop_readlink = DEVFS_BADOP,
1769a20a70dSMatthew Dillon .vop_reallocblks = DEVFS_BADOP,
1779f889dc4SMatthew Dillon .vop_reclaim = devfs_vop_reclaim,
1789f889dc4SMatthew Dillon .vop_setattr = devfs_vop_setattr,
17921864bc5SMatthew Dillon .vop_strategy = devfs_spec_strategy,
18021864bc5SMatthew Dillon .vop_write = devfs_spec_write,
18121864bc5SMatthew Dillon .vop_ioctl = devfs_spec_ioctl
18221864bc5SMatthew Dillon };
18321864bc5SMatthew Dillon
1849f889dc4SMatthew Dillon /*
1859f889dc4SMatthew Dillon * devfs file pointer operations. All fileops are MPSAFE.
1869f889dc4SMatthew Dillon */
18721864bc5SMatthew Dillon struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops;
18821864bc5SMatthew Dillon
18921864bc5SMatthew Dillon struct fileops devfs_dev_fileops = {
1909f889dc4SMatthew Dillon .fo_read = devfs_fo_read,
1919f889dc4SMatthew Dillon .fo_write = devfs_fo_write,
1929f889dc4SMatthew Dillon .fo_ioctl = devfs_fo_ioctl,
1939f889dc4SMatthew Dillon .fo_kqfilter = devfs_fo_kqfilter,
1949f889dc4SMatthew Dillon .fo_stat = devfs_fo_stat,
1959f889dc4SMatthew Dillon .fo_close = devfs_fo_close,
19621864bc5SMatthew Dillon .fo_shutdown = nofo_shutdown
19721864bc5SMatthew Dillon };
19821864bc5SMatthew Dillon
1994062d050SMatthew Dillon /*
2009f889dc4SMatthew Dillon * These two functions are possibly temporary hacks for devices (aka
2019f889dc4SMatthew Dillon * the pty code) which want to control the node attributes themselves.
2024062d050SMatthew Dillon *
2034062d050SMatthew Dillon * XXX we may ultimately desire to simply remove the uid/gid/mode
2044062d050SMatthew Dillon * from the node entirely.
2059f889dc4SMatthew Dillon *
2069f889dc4SMatthew Dillon * MPSAFE - sorta. Theoretically the overwrite can compete since they
2079f889dc4SMatthew Dillon * are loading from the same fields.
2084062d050SMatthew Dillon */
2094062d050SMatthew Dillon static __inline void
node_sync_dev_get(struct devfs_node * node)2104062d050SMatthew Dillon node_sync_dev_get(struct devfs_node *node)
2114062d050SMatthew Dillon {
2124062d050SMatthew Dillon cdev_t dev;
2134062d050SMatthew Dillon
2144062d050SMatthew Dillon if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
2154062d050SMatthew Dillon node->uid = dev->si_uid;
2164062d050SMatthew Dillon node->gid = dev->si_gid;
2174062d050SMatthew Dillon node->mode = dev->si_perms;
2184062d050SMatthew Dillon }
2194062d050SMatthew Dillon }
2204062d050SMatthew Dillon
2214062d050SMatthew Dillon static __inline void
node_sync_dev_set(struct devfs_node * node)2224062d050SMatthew Dillon node_sync_dev_set(struct devfs_node *node)
2234062d050SMatthew Dillon {
2244062d050SMatthew Dillon cdev_t dev;
2254062d050SMatthew Dillon
2264062d050SMatthew Dillon if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
2274062d050SMatthew Dillon dev->si_uid = node->uid;
2284062d050SMatthew Dillon dev->si_gid = node->gid;
2294062d050SMatthew Dillon dev->si_perms = node->mode;
2304062d050SMatthew Dillon }
2314062d050SMatthew Dillon }
23221864bc5SMatthew Dillon
23321864bc5SMatthew Dillon /*
23421864bc5SMatthew Dillon * generic entry point for unsupported operations
23521864bc5SMatthew Dillon */
23621864bc5SMatthew Dillon static int
devfs_vop_badop(struct vop_generic_args * ap)2379f889dc4SMatthew Dillon devfs_vop_badop(struct vop_generic_args *ap)
23821864bc5SMatthew Dillon {
23921864bc5SMatthew Dillon return (EIO);
24021864bc5SMatthew Dillon }
24121864bc5SMatthew Dillon
24221864bc5SMatthew Dillon
24321864bc5SMatthew Dillon static int
devfs_vop_access(struct vop_access_args * ap)2449f889dc4SMatthew Dillon devfs_vop_access(struct vop_access_args *ap)
24521864bc5SMatthew Dillon {
24621864bc5SMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
247898c91eeSMatthew Dillon int error;
24821864bc5SMatthew Dillon
249894bbb25SAlex Hornung if (!devfs_node_is_accessible(node))
250894bbb25SAlex Hornung return ENOENT;
2514062d050SMatthew Dillon node_sync_dev_get(node);
25221864bc5SMatthew Dillon error = vop_helper_access(ap, node->uid, node->gid,
25321864bc5SMatthew Dillon node->mode, node->flags);
25421864bc5SMatthew Dillon
25521864bc5SMatthew Dillon return error;
25621864bc5SMatthew Dillon }
25721864bc5SMatthew Dillon
25821864bc5SMatthew Dillon
25921864bc5SMatthew Dillon static int
devfs_vop_inactive(struct vop_inactive_args * ap)2609f889dc4SMatthew Dillon devfs_vop_inactive(struct vop_inactive_args *ap)
26121864bc5SMatthew Dillon {
262ca8d7677SMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
26321864bc5SMatthew Dillon
264ca8d7677SMatthew Dillon if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0)
26521864bc5SMatthew Dillon vrecycle(ap->a_vp);
26621864bc5SMatthew Dillon return 0;
26721864bc5SMatthew Dillon }
26821864bc5SMatthew Dillon
26921864bc5SMatthew Dillon
27021864bc5SMatthew Dillon static int
devfs_vop_reclaim(struct vop_reclaim_args * ap)2719f889dc4SMatthew Dillon devfs_vop_reclaim(struct vop_reclaim_args *ap)
27221864bc5SMatthew Dillon {
273be6f2e86SMatthew Dillon struct devfs_node *node;
274be6f2e86SMatthew Dillon struct vnode *vp;
275be6f2e86SMatthew Dillon int locked;
276be6f2e86SMatthew Dillon
277be6f2e86SMatthew Dillon /*
278be6f2e86SMatthew Dillon * Check if it is locked already. if not, we acquire the devfs lock
279be6f2e86SMatthew Dillon */
280ab08ac79SSascha Wildner if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) {
28121864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
28221864bc5SMatthew Dillon locked = 1;
283be6f2e86SMatthew Dillon } else {
284be6f2e86SMatthew Dillon locked = 0;
28521864bc5SMatthew Dillon }
28621864bc5SMatthew Dillon
287be6f2e86SMatthew Dillon /*
288be6f2e86SMatthew Dillon * Get rid of the devfs_node if it is no longer linked into the
289731fd4ccSMatthew Dillon * topology. Interlocked by devfs_lock. However, be careful
290731fd4ccSMatthew Dillon * interposing other operations between cleaning out v_data and
291731fd4ccSMatthew Dillon * devfs_freep() as the node is only protected by devfs_lock
292731fd4ccSMatthew Dillon * once the vnode is disassociated.
293be6f2e86SMatthew Dillon */
294be6f2e86SMatthew Dillon vp = ap->a_vp;
295770f8279SMatthew Dillon node = DEVFS_NODE(vp);
296770f8279SMatthew Dillon
297770f8279SMatthew Dillon if (node) {
298731fd4ccSMatthew Dillon if (node->v_node != vp) {
299731fd4ccSMatthew Dillon kprintf("NODE->V_NODE MISMATCH VP=%p NODEVP=%p\n",
300731fd4ccSMatthew Dillon vp, node->v_node);
301731fd4ccSMatthew Dillon }
302731fd4ccSMatthew Dillon vp->v_data = NULL;
303731fd4ccSMatthew Dillon node->v_node = NULL;
3044062d050SMatthew Dillon if ((node->flags & DEVFS_NODE_LINKED) == 0)
3054062d050SMatthew Dillon devfs_freep(node);
30621864bc5SMatthew Dillon }
307731fd4ccSMatthew Dillon v_release_rdev(vp);
30821864bc5SMatthew Dillon
30921864bc5SMatthew Dillon if (locked)
31021864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
31121864bc5SMatthew Dillon
312be6f2e86SMatthew Dillon /*
3139b823501SAlex Hornung * v_rdev needs to be properly released using v_release_rdev
3149b823501SAlex Hornung * Make sure v_data is NULL as well.
315be6f2e86SMatthew Dillon */
31621864bc5SMatthew Dillon return 0;
31721864bc5SMatthew Dillon }
31821864bc5SMatthew Dillon
31921864bc5SMatthew Dillon
32021864bc5SMatthew Dillon static int
devfs_vop_readdir(struct vop_readdir_args * ap)3219f889dc4SMatthew Dillon devfs_vop_readdir(struct vop_readdir_args *ap)
32221864bc5SMatthew Dillon {
323898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_vp);
32421864bc5SMatthew Dillon struct devfs_node *node;
32521864bc5SMatthew Dillon int cookie_index;
32621864bc5SMatthew Dillon int ncookies;
327898c91eeSMatthew Dillon int error2;
328898c91eeSMatthew Dillon int error;
329898c91eeSMatthew Dillon int r;
33021864bc5SMatthew Dillon off_t *cookies;
33121864bc5SMatthew Dillon off_t saveoff;
33221864bc5SMatthew Dillon
33321864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
33421864bc5SMatthew Dillon
33521864bc5SMatthew Dillon if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
33621864bc5SMatthew Dillon return (EINVAL);
337b458d1abSMatthew Dillon error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
338b458d1abSMatthew Dillon if (error)
33921864bc5SMatthew Dillon return (error);
34021864bc5SMatthew Dillon
341c512ab96SMatthew Dillon if (!devfs_node_is_accessible(dnode)) {
342c512ab96SMatthew Dillon vn_unlock(ap->a_vp);
343ca8d7677SMatthew Dillon return ENOENT;
344c512ab96SMatthew Dillon }
345ca8d7677SMatthew Dillon
346ca8d7677SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
347ca8d7677SMatthew Dillon
34821864bc5SMatthew Dillon saveoff = ap->a_uio->uio_offset;
34921864bc5SMatthew Dillon
35021864bc5SMatthew Dillon if (ap->a_ncookies) {
35121864bc5SMatthew Dillon ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
35221864bc5SMatthew Dillon if (ncookies > 256)
35321864bc5SMatthew Dillon ncookies = 256;
35421864bc5SMatthew Dillon cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
35521864bc5SMatthew Dillon cookie_index = 0;
35621864bc5SMatthew Dillon } else {
35721864bc5SMatthew Dillon ncookies = -1;
35821864bc5SMatthew Dillon cookies = NULL;
35921864bc5SMatthew Dillon cookie_index = 0;
36021864bc5SMatthew Dillon }
36121864bc5SMatthew Dillon
362d489a79aSMatthew Dillon vfs_timestamp(&dnode->atime);
36321864bc5SMatthew Dillon
36421864bc5SMatthew Dillon if (saveoff == 0) {
365898c91eeSMatthew Dillon r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino,
366898c91eeSMatthew Dillon DT_DIR, 1, ".");
36721864bc5SMatthew Dillon if (r)
36821864bc5SMatthew Dillon goto done;
36921864bc5SMatthew Dillon if (cookies)
37021864bc5SMatthew Dillon cookies[cookie_index] = saveoff;
37121864bc5SMatthew Dillon saveoff++;
37221864bc5SMatthew Dillon cookie_index++;
37321864bc5SMatthew Dillon if (cookie_index == ncookies)
37421864bc5SMatthew Dillon goto done;
37521864bc5SMatthew Dillon }
37621864bc5SMatthew Dillon
37721864bc5SMatthew Dillon if (saveoff == 1) {
378898c91eeSMatthew Dillon if (dnode->parent) {
37921864bc5SMatthew Dillon r = vop_write_dirent(&error, ap->a_uio,
380898c91eeSMatthew Dillon dnode->parent->d_dir.d_ino,
38121864bc5SMatthew Dillon DT_DIR, 2, "..");
38221864bc5SMatthew Dillon } else {
38321864bc5SMatthew Dillon r = vop_write_dirent(&error, ap->a_uio,
384898c91eeSMatthew Dillon dnode->d_dir.d_ino,
385898c91eeSMatthew Dillon DT_DIR, 2, "..");
38621864bc5SMatthew Dillon }
38721864bc5SMatthew Dillon if (r)
38821864bc5SMatthew Dillon goto done;
38921864bc5SMatthew Dillon if (cookies)
39021864bc5SMatthew Dillon cookies[cookie_index] = saveoff;
39121864bc5SMatthew Dillon saveoff++;
39221864bc5SMatthew Dillon cookie_index++;
39321864bc5SMatthew Dillon if (cookie_index == ncookies)
39421864bc5SMatthew Dillon goto done;
39521864bc5SMatthew Dillon }
39621864bc5SMatthew Dillon
397898c91eeSMatthew Dillon TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
398898c91eeSMatthew Dillon if ((node->flags & DEVFS_HIDDEN) ||
399898c91eeSMatthew Dillon (node->flags & DEVFS_INVISIBLE)) {
40021864bc5SMatthew Dillon continue;
401898c91eeSMatthew Dillon }
40221864bc5SMatthew Dillon
403f7e8960cSAlex Hornung /*
4049f889dc4SMatthew Dillon * If the node type is a valid devfs alias, then we make
4059f889dc4SMatthew Dillon * sure that the target isn't hidden. If it is, we don't
4069f889dc4SMatthew Dillon * show the link in the directory listing.
407f7e8960cSAlex Hornung */
4088e78a293SSascha Wildner if ((node->node_type == Nlink) && (node->link_target != NULL) &&
409f7e8960cSAlex Hornung (node->link_target->flags & DEVFS_HIDDEN))
410f7e8960cSAlex Hornung continue;
411f7e8960cSAlex Hornung
41221864bc5SMatthew Dillon if (node->cookie < saveoff)
41321864bc5SMatthew Dillon continue;
414f7e8960cSAlex Hornung
41521864bc5SMatthew Dillon saveoff = node->cookie;
41621864bc5SMatthew Dillon
417898c91eeSMatthew Dillon error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino,
418898c91eeSMatthew Dillon node->d_dir.d_type,
419898c91eeSMatthew Dillon node->d_dir.d_namlen,
420898c91eeSMatthew Dillon node->d_dir.d_name);
42121864bc5SMatthew Dillon
42221864bc5SMatthew Dillon if (error2)
42321864bc5SMatthew Dillon break;
42421864bc5SMatthew Dillon
42521864bc5SMatthew Dillon saveoff++;
42621864bc5SMatthew Dillon
42721864bc5SMatthew Dillon if (cookies)
42821864bc5SMatthew Dillon cookies[cookie_index] = node->cookie;
42921864bc5SMatthew Dillon ++cookie_index;
43021864bc5SMatthew Dillon if (cookie_index == ncookies)
43121864bc5SMatthew Dillon break;
43221864bc5SMatthew Dillon }
43321864bc5SMatthew Dillon
43421864bc5SMatthew Dillon done:
435ca8d7677SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
43621864bc5SMatthew Dillon vn_unlock(ap->a_vp);
43721864bc5SMatthew Dillon
43821864bc5SMatthew Dillon ap->a_uio->uio_offset = saveoff;
43921864bc5SMatthew Dillon if (error && cookie_index == 0) {
44021864bc5SMatthew Dillon if (cookies) {
44121864bc5SMatthew Dillon kfree(cookies, M_TEMP);
44221864bc5SMatthew Dillon *ap->a_ncookies = 0;
44321864bc5SMatthew Dillon *ap->a_cookies = NULL;
44421864bc5SMatthew Dillon }
44521864bc5SMatthew Dillon } else {
44621864bc5SMatthew Dillon if (cookies) {
44721864bc5SMatthew Dillon *ap->a_ncookies = cookie_index;
44821864bc5SMatthew Dillon *ap->a_cookies = cookies;
44921864bc5SMatthew Dillon }
45021864bc5SMatthew Dillon }
45121864bc5SMatthew Dillon return (error);
45221864bc5SMatthew Dillon }
45321864bc5SMatthew Dillon
45421864bc5SMatthew Dillon
45521864bc5SMatthew Dillon static int
devfs_vop_nresolve(struct vop_nresolve_args * ap)4569f889dc4SMatthew Dillon devfs_vop_nresolve(struct vop_nresolve_args *ap)
45721864bc5SMatthew Dillon {
458898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
45921864bc5SMatthew Dillon struct devfs_node *node, *found = NULL;
46021864bc5SMatthew Dillon struct namecache *ncp;
46121864bc5SMatthew Dillon struct vnode *vp = NULL;
46221864bc5SMatthew Dillon int error = 0;
46321864bc5SMatthew Dillon int len;
464260e4e8bSAlex Hornung int depth;
46521864bc5SMatthew Dillon
46621864bc5SMatthew Dillon ncp = ap->a_nch->ncp;
46721864bc5SMatthew Dillon len = ncp->nc_nlen;
46821864bc5SMatthew Dillon
469898c91eeSMatthew Dillon if (!devfs_node_is_accessible(dnode))
470ca8d7677SMatthew Dillon return ENOENT;
471ca8d7677SMatthew Dillon
47221864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
47321864bc5SMatthew Dillon
4748e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) {
475e23485a5SMatthew Dillon error = ENOENT;
47621864bc5SMatthew Dillon cache_setvp(ap->a_nch, NULL);
47721864bc5SMatthew Dillon goto out;
47821864bc5SMatthew Dillon }
47921864bc5SMatthew Dillon
480898c91eeSMatthew Dillon TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
48121864bc5SMatthew Dillon if (len == node->d_dir.d_namlen) {
48221864bc5SMatthew Dillon if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
48321864bc5SMatthew Dillon found = node;
48421864bc5SMatthew Dillon break;
48521864bc5SMatthew Dillon }
48621864bc5SMatthew Dillon }
48721864bc5SMatthew Dillon }
48821864bc5SMatthew Dillon
48921864bc5SMatthew Dillon if (found) {
490260e4e8bSAlex Hornung depth = 0;
4918e78a293SSascha Wildner while ((found->node_type == Nlink) && (found->link_target)) {
492260e4e8bSAlex Hornung if (depth >= 8) {
493260e4e8bSAlex Hornung devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
494260e4e8bSAlex Hornung break;
495260e4e8bSAlex Hornung }
496260e4e8bSAlex Hornung
49721864bc5SMatthew Dillon found = found->link_target;
498260e4e8bSAlex Hornung ++depth;
499260e4e8bSAlex Hornung }
50021864bc5SMatthew Dillon
50121864bc5SMatthew Dillon if (!(found->flags & DEVFS_HIDDEN))
50221864bc5SMatthew Dillon devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
50321864bc5SMatthew Dillon }
50421864bc5SMatthew Dillon
50521864bc5SMatthew Dillon if (vp == NULL) {
50621864bc5SMatthew Dillon error = ENOENT;
50721864bc5SMatthew Dillon cache_setvp(ap->a_nch, NULL);
50821864bc5SMatthew Dillon goto out;
50921864bc5SMatthew Dillon
51021864bc5SMatthew Dillon }
51121864bc5SMatthew Dillon KKASSERT(vp);
51221864bc5SMatthew Dillon vn_unlock(vp);
51321864bc5SMatthew Dillon cache_setvp(ap->a_nch, vp);
51421864bc5SMatthew Dillon vrele(vp);
51521864bc5SMatthew Dillon out:
51621864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
517898c91eeSMatthew Dillon
51821864bc5SMatthew Dillon return error;
51921864bc5SMatthew Dillon }
52021864bc5SMatthew Dillon
52121864bc5SMatthew Dillon
52221864bc5SMatthew Dillon static int
devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args * ap)5239f889dc4SMatthew Dillon devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
52421864bc5SMatthew Dillon {
525898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
52621864bc5SMatthew Dillon
527898c91eeSMatthew Dillon *ap->a_vpp = NULL;
528898c91eeSMatthew Dillon if (!devfs_node_is_accessible(dnode))
529894bbb25SAlex Hornung return ENOENT;
530894bbb25SAlex Hornung
53121864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
532898c91eeSMatthew Dillon if (dnode->parent != NULL) {
533898c91eeSMatthew Dillon devfs_allocv(ap->a_vpp, dnode->parent);
53421864bc5SMatthew Dillon vn_unlock(*ap->a_vpp);
53521864bc5SMatthew Dillon }
53621864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
53721864bc5SMatthew Dillon
53821864bc5SMatthew Dillon return ((*ap->a_vpp == NULL) ? ENOENT : 0);
53921864bc5SMatthew Dillon }
54021864bc5SMatthew Dillon
54121864bc5SMatthew Dillon
542845bd036SMatthew Dillon /*
543845bd036SMatthew Dillon * getattr() - Does not need a lock since the vp is refd
544845bd036SMatthew Dillon */
54521864bc5SMatthew Dillon static int
devfs_vop_getattr(struct vop_getattr_args * ap)5469f889dc4SMatthew Dillon devfs_vop_getattr(struct vop_getattr_args *ap)
54721864bc5SMatthew Dillon {
54821864bc5SMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
549898c91eeSMatthew Dillon struct vattr *vap = ap->a_vap;
5502d076755SAlex Hornung struct partinfo pinfo;
55121864bc5SMatthew Dillon int error = 0;
55221864bc5SMatthew Dillon
553952f0188SAlex Hornung #if 0
554894bbb25SAlex Hornung if (!devfs_node_is_accessible(node))
555ca8d7677SMatthew Dillon return ENOENT;
556952f0188SAlex Hornung #endif
557758d6a9eSPeeter Must
558758d6a9eSPeeter Must /*
559758d6a9eSPeeter Must * XXX This is a temporary hack to prevent crashes when the device is
560758d6a9eSPeeter Must * being destroyed (and so the underlying node will be gone) while
561758d6a9eSPeeter Must * a userland program is blocked in a read().
562758d6a9eSPeeter Must */
563758d6a9eSPeeter Must if (node == NULL)
564758d6a9eSPeeter Must return EIO;
565758d6a9eSPeeter Must
5664062d050SMatthew Dillon node_sync_dev_get(node);
567ca8d7677SMatthew Dillon
56821864bc5SMatthew Dillon /* start by zeroing out the attributes */
56921864bc5SMatthew Dillon VATTR_NULL(vap);
57021864bc5SMatthew Dillon
57121864bc5SMatthew Dillon /* next do all the common fields */
57221864bc5SMatthew Dillon vap->va_type = ap->a_vp->v_type;
57321864bc5SMatthew Dillon vap->va_mode = node->mode;
57421864bc5SMatthew Dillon vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
575a3c7ebb9SMatthew Dillon vap->va_flags = 0;
57621864bc5SMatthew Dillon vap->va_blocksize = DEV_BSIZE;
577a3c7ebb9SMatthew Dillon vap->va_bytes = vap->va_size = 0;
57821864bc5SMatthew Dillon
57921864bc5SMatthew Dillon vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
58021864bc5SMatthew Dillon
58121864bc5SMatthew Dillon vap->va_atime = node->atime;
58221864bc5SMatthew Dillon vap->va_mtime = node->mtime;
58321864bc5SMatthew Dillon vap->va_ctime = node->ctime;
58421864bc5SMatthew Dillon
58521864bc5SMatthew Dillon vap->va_nlink = 1; /* number of references to file */
58621864bc5SMatthew Dillon
58721864bc5SMatthew Dillon vap->va_uid = node->uid;
58821864bc5SMatthew Dillon vap->va_gid = node->gid;
58921864bc5SMatthew Dillon
59021864bc5SMatthew Dillon vap->va_rmajor = 0;
59121864bc5SMatthew Dillon vap->va_rminor = 0;
59221864bc5SMatthew Dillon
5938e78a293SSascha Wildner if ((node->node_type == Ndev) && node->d_dev) {
594898c91eeSMatthew Dillon reference_dev(node->d_dev);
595898c91eeSMatthew Dillon vap->va_rminor = node->d_dev->si_uminor;
596898c91eeSMatthew Dillon release_dev(node->d_dev);
59721864bc5SMatthew Dillon }
59821864bc5SMatthew Dillon
59921864bc5SMatthew Dillon /* For a softlink the va_size is the length of the softlink */
600898c91eeSMatthew Dillon if (node->symlink_name != 0) {
6012d076755SAlex Hornung vap->va_bytes = vap->va_size = node->symlink_namelen;
60221864bc5SMatthew Dillon }
6032d076755SAlex Hornung
6042d076755SAlex Hornung /*
6052d076755SAlex Hornung * For a disk-type device, va_size is the size of the underlying
6062d076755SAlex Hornung * device, so that lseek() works properly.
6072d076755SAlex Hornung */
6082d076755SAlex Hornung if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) {
6092d076755SAlex Hornung bzero(&pinfo, sizeof(pinfo));
6102d076755SAlex Hornung error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo,
6118c530b23SJohannes Hofmann 0, proc0.p_ucred, NULL, NULL);
6122d076755SAlex Hornung if ((error == 0) && (pinfo.media_blksize != 0)) {
6132d076755SAlex Hornung vap->va_size = pinfo.media_size;
6142d076755SAlex Hornung } else {
6152d076755SAlex Hornung vap->va_size = 0;
6162d076755SAlex Hornung error = 0;
6172d076755SAlex Hornung }
6182d076755SAlex Hornung }
6192d076755SAlex Hornung
620894bbb25SAlex Hornung return (error);
62121864bc5SMatthew Dillon }
62221864bc5SMatthew Dillon
62321864bc5SMatthew Dillon static int
devfs_vop_setattr(struct vop_setattr_args * ap)6249f889dc4SMatthew Dillon devfs_vop_setattr(struct vop_setattr_args *ap)
62521864bc5SMatthew Dillon {
626898c91eeSMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
62721864bc5SMatthew Dillon struct vattr *vap;
628dffaed1bSAlex Hornung uid_t cur_uid;
629dffaed1bSAlex Hornung gid_t cur_gid;
630dffaed1bSAlex Hornung mode_t cur_mode;
63121864bc5SMatthew Dillon int error = 0;
63221864bc5SMatthew Dillon
633894bbb25SAlex Hornung if (!devfs_node_is_accessible(node))
634ca8d7677SMatthew Dillon return ENOENT;
6354062d050SMatthew Dillon node_sync_dev_get(node);
63621864bc5SMatthew Dillon
63721864bc5SMatthew Dillon vap = ap->a_vap;
63821864bc5SMatthew Dillon
639dffaed1bSAlex Hornung if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) {
640dffaed1bSAlex Hornung cur_uid = node->uid;
641dffaed1bSAlex Hornung cur_gid = node->gid;
642dffaed1bSAlex Hornung cur_mode = node->mode;
643dffaed1bSAlex Hornung error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
644dffaed1bSAlex Hornung ap->a_cred, &cur_uid, &cur_gid, &cur_mode);
645898c91eeSMatthew Dillon if (error)
64621864bc5SMatthew Dillon goto out;
64721864bc5SMatthew Dillon
648dffaed1bSAlex Hornung if (node->uid != cur_uid || node->gid != cur_gid) {
649dffaed1bSAlex Hornung node->uid = cur_uid;
650dffaed1bSAlex Hornung node->gid = cur_gid;
651dffaed1bSAlex Hornung node->mode = cur_mode;
65221864bc5SMatthew Dillon }
65321864bc5SMatthew Dillon }
65421864bc5SMatthew Dillon
65521864bc5SMatthew Dillon if (vap->va_mode != (mode_t)VNOVAL) {
656dffaed1bSAlex Hornung cur_mode = node->mode;
657dffaed1bSAlex Hornung error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
658dffaed1bSAlex Hornung node->uid, node->gid, &cur_mode);
659dffaed1bSAlex Hornung if (error == 0 && node->mode != cur_mode) {
660dffaed1bSAlex Hornung node->mode = cur_mode;
66121864bc5SMatthew Dillon }
66221864bc5SMatthew Dillon }
66321864bc5SMatthew Dillon
66421864bc5SMatthew Dillon out:
6654062d050SMatthew Dillon node_sync_dev_set(node);
666d489a79aSMatthew Dillon vfs_timestamp(&node->ctime);
667898c91eeSMatthew Dillon
66821864bc5SMatthew Dillon return error;
66921864bc5SMatthew Dillon }
67021864bc5SMatthew Dillon
67121864bc5SMatthew Dillon
67221864bc5SMatthew Dillon static int
devfs_vop_readlink(struct vop_readlink_args * ap)6739f889dc4SMatthew Dillon devfs_vop_readlink(struct vop_readlink_args *ap)
67421864bc5SMatthew Dillon {
67521864bc5SMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
676ca8d7677SMatthew Dillon int ret;
677ca8d7677SMatthew Dillon
678894bbb25SAlex Hornung if (!devfs_node_is_accessible(node))
679ca8d7677SMatthew Dillon return ENOENT;
68021864bc5SMatthew Dillon
681845bd036SMatthew Dillon lockmgr(&devfs_lock, LK_SHARED);
682ca8d7677SMatthew Dillon ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
683ca8d7677SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
684ca8d7677SMatthew Dillon
685ca8d7677SMatthew Dillon return ret;
68621864bc5SMatthew Dillon }
68721864bc5SMatthew Dillon
68821864bc5SMatthew Dillon
68921864bc5SMatthew Dillon static int
devfs_vop_print(struct vop_print_args * ap)6909f889dc4SMatthew Dillon devfs_vop_print(struct vop_print_args *ap)
69121864bc5SMatthew Dillon {
69221864bc5SMatthew Dillon return (0);
69321864bc5SMatthew Dillon }
69421864bc5SMatthew Dillon
69539a08947SAlex Hornung static int
devfs_vop_nmkdir(struct vop_nmkdir_args * ap)6969f889dc4SMatthew Dillon devfs_vop_nmkdir(struct vop_nmkdir_args *ap)
69739a08947SAlex Hornung {
69839a08947SAlex Hornung struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
69939a08947SAlex Hornung struct devfs_node *node;
70039a08947SAlex Hornung
70139a08947SAlex Hornung if (!devfs_node_is_accessible(dnode))
70239a08947SAlex Hornung return ENOENT;
70339a08947SAlex Hornung
7048e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
70539a08947SAlex Hornung goto out;
70639a08947SAlex Hornung
70739a08947SAlex Hornung lockmgr(&devfs_lock, LK_EXCLUSIVE);
7088e78a293SSascha Wildner devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Ndir,
70939a08947SAlex Hornung ap->a_nch->ncp->nc_name, dnode, NULL);
71039a08947SAlex Hornung
71139a08947SAlex Hornung if (*ap->a_vpp) {
71239a08947SAlex Hornung node = DEVFS_NODE(*ap->a_vpp);
71339a08947SAlex Hornung node->flags |= DEVFS_USER_CREATED;
71439a08947SAlex Hornung cache_setunresolved(ap->a_nch);
71539a08947SAlex Hornung cache_setvp(ap->a_nch, *ap->a_vpp);
71639a08947SAlex Hornung }
71739a08947SAlex Hornung lockmgr(&devfs_lock, LK_RELEASE);
71839a08947SAlex Hornung out:
71939a08947SAlex Hornung return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
72039a08947SAlex Hornung }
72121864bc5SMatthew Dillon
72221864bc5SMatthew Dillon static int
devfs_vop_nsymlink(struct vop_nsymlink_args * ap)7239f889dc4SMatthew Dillon devfs_vop_nsymlink(struct vop_nsymlink_args *ap)
72421864bc5SMatthew Dillon {
725898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
726898c91eeSMatthew Dillon struct devfs_node *node;
727898c91eeSMatthew Dillon size_t targetlen;
72821864bc5SMatthew Dillon
729898c91eeSMatthew Dillon if (!devfs_node_is_accessible(dnode))
730ca8d7677SMatthew Dillon return ENOENT;
731ca8d7677SMatthew Dillon
732894bbb25SAlex Hornung ap->a_vap->va_type = VLNK;
733894bbb25SAlex Hornung
7348e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
73521864bc5SMatthew Dillon goto out;
736898c91eeSMatthew Dillon
73721864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
7388e78a293SSascha Wildner devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Nlink,
739898c91eeSMatthew Dillon ap->a_nch->ncp->nc_name, dnode, NULL);
74021864bc5SMatthew Dillon
741898c91eeSMatthew Dillon targetlen = strlen(ap->a_target);
74221864bc5SMatthew Dillon if (*ap->a_vpp) {
743898c91eeSMatthew Dillon node = DEVFS_NODE(*ap->a_vpp);
744898c91eeSMatthew Dillon node->flags |= DEVFS_USER_CREATED;
745898c91eeSMatthew Dillon node->symlink_namelen = targetlen;
746898c91eeSMatthew Dillon node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
747898c91eeSMatthew Dillon memcpy(node->symlink_name, ap->a_target, targetlen);
748898c91eeSMatthew Dillon node->symlink_name[targetlen] = '\0';
74921864bc5SMatthew Dillon cache_setunresolved(ap->a_nch);
75021864bc5SMatthew Dillon cache_setvp(ap->a_nch, *ap->a_vpp);
75121864bc5SMatthew Dillon }
75221864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
75321864bc5SMatthew Dillon out:
75421864bc5SMatthew Dillon return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
75521864bc5SMatthew Dillon }
75621864bc5SMatthew Dillon
75739a08947SAlex Hornung static int
devfs_vop_nrmdir(struct vop_nrmdir_args * ap)7589f889dc4SMatthew Dillon devfs_vop_nrmdir(struct vop_nrmdir_args *ap)
75939a08947SAlex Hornung {
76039a08947SAlex Hornung struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
76139a08947SAlex Hornung struct devfs_node *node;
76239a08947SAlex Hornung struct namecache *ncp;
76339a08947SAlex Hornung int error = ENOENT;
76439a08947SAlex Hornung
76539a08947SAlex Hornung ncp = ap->a_nch->ncp;
76639a08947SAlex Hornung
76739a08947SAlex Hornung if (!devfs_node_is_accessible(dnode))
76839a08947SAlex Hornung return ENOENT;
76939a08947SAlex Hornung
77039a08947SAlex Hornung lockmgr(&devfs_lock, LK_EXCLUSIVE);
77139a08947SAlex Hornung
7728e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
77339a08947SAlex Hornung goto out;
77439a08947SAlex Hornung
77539a08947SAlex Hornung TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
77639a08947SAlex Hornung if (ncp->nc_nlen != node->d_dir.d_namlen)
77739a08947SAlex Hornung continue;
77839a08947SAlex Hornung if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
77939a08947SAlex Hornung continue;
78039a08947SAlex Hornung
78139a08947SAlex Hornung /*
78239a08947SAlex Hornung * only allow removal of user created dirs
78339a08947SAlex Hornung */
78439a08947SAlex Hornung if ((node->flags & DEVFS_USER_CREATED) == 0) {
78539a08947SAlex Hornung error = EPERM;
78639a08947SAlex Hornung goto out;
7878e78a293SSascha Wildner } else if (node->node_type != Ndir) {
78839a08947SAlex Hornung error = ENOTDIR;
78939a08947SAlex Hornung goto out;
79039a08947SAlex Hornung } else if (node->nchildren > 2) {
79139a08947SAlex Hornung error = ENOTEMPTY;
79239a08947SAlex Hornung goto out;
79339a08947SAlex Hornung } else {
79439a08947SAlex Hornung if (node->v_node)
79539a08947SAlex Hornung cache_inval_vp(node->v_node, CINV_DESTROY);
79639a08947SAlex Hornung devfs_unlinkp(node);
79739a08947SAlex Hornung error = 0;
79839a08947SAlex Hornung break;
79939a08947SAlex Hornung }
80039a08947SAlex Hornung }
80139a08947SAlex Hornung
8025323ed62SMatthew Dillon cache_unlink(ap->a_nch);
80339a08947SAlex Hornung out:
80439a08947SAlex Hornung lockmgr(&devfs_lock, LK_RELEASE);
80539a08947SAlex Hornung return error;
80639a08947SAlex Hornung }
80721864bc5SMatthew Dillon
80821864bc5SMatthew Dillon static int
devfs_vop_nremove(struct vop_nremove_args * ap)8099f889dc4SMatthew Dillon devfs_vop_nremove(struct vop_nremove_args *ap)
81021864bc5SMatthew Dillon {
811898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
81221864bc5SMatthew Dillon struct devfs_node *node;
81321864bc5SMatthew Dillon struct namecache *ncp;
81421864bc5SMatthew Dillon int error = ENOENT;
81521864bc5SMatthew Dillon
81621864bc5SMatthew Dillon ncp = ap->a_nch->ncp;
81721864bc5SMatthew Dillon
818898c91eeSMatthew Dillon if (!devfs_node_is_accessible(dnode))
819ca8d7677SMatthew Dillon return ENOENT;
820ca8d7677SMatthew Dillon
82121864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
82221864bc5SMatthew Dillon
8238e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
82421864bc5SMatthew Dillon goto out;
82521864bc5SMatthew Dillon
826898c91eeSMatthew Dillon TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
827898c91eeSMatthew Dillon if (ncp->nc_nlen != node->d_dir.d_namlen)
828898c91eeSMatthew Dillon continue;
829898c91eeSMatthew Dillon if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
830898c91eeSMatthew Dillon continue;
831898c91eeSMatthew Dillon
832898c91eeSMatthew Dillon /*
833898c91eeSMatthew Dillon * only allow removal of user created stuff (e.g. symlinks)
834898c91eeSMatthew Dillon */
83521864bc5SMatthew Dillon if ((node->flags & DEVFS_USER_CREATED) == 0) {
83621864bc5SMatthew Dillon error = EPERM;
83721864bc5SMatthew Dillon goto out;
8388e78a293SSascha Wildner } else if (node->node_type == Ndir) {
83939a08947SAlex Hornung error = EISDIR;
84039a08947SAlex Hornung goto out;
84121864bc5SMatthew Dillon } else {
84221864bc5SMatthew Dillon if (node->v_node)
84321864bc5SMatthew Dillon cache_inval_vp(node->v_node, CINV_DESTROY);
84421864bc5SMatthew Dillon devfs_unlinkp(node);
84521864bc5SMatthew Dillon error = 0;
84621864bc5SMatthew Dillon break;
84721864bc5SMatthew Dillon }
84821864bc5SMatthew Dillon }
84921864bc5SMatthew Dillon
8505323ed62SMatthew Dillon cache_unlink(ap->a_nch);
85121864bc5SMatthew Dillon out:
85221864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
85321864bc5SMatthew Dillon return error;
85421864bc5SMatthew Dillon }
85521864bc5SMatthew Dillon
85621864bc5SMatthew Dillon
85721864bc5SMatthew Dillon static int
devfs_spec_open(struct vop_open_args * ap)85821864bc5SMatthew Dillon devfs_spec_open(struct vop_open_args *ap)
85921864bc5SMatthew Dillon {
86021864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
861ca8d7677SMatthew Dillon struct vnode *orig_vp = NULL;
862898c91eeSMatthew Dillon struct devfs_node *node = DEVFS_NODE(vp);
863898c91eeSMatthew Dillon struct devfs_node *newnode;
86421864bc5SMatthew Dillon cdev_t dev, ndev = NULL;
86521864bc5SMatthew Dillon int error = 0;
86621864bc5SMatthew Dillon
867898c91eeSMatthew Dillon if (node) {
868898c91eeSMatthew Dillon if (node->d_dev == NULL)
86921864bc5SMatthew Dillon return ENXIO;
870898c91eeSMatthew Dillon if (!devfs_node_is_accessible(node))
871894bbb25SAlex Hornung return ENOENT;
87221864bc5SMatthew Dillon }
87321864bc5SMatthew Dillon
87421864bc5SMatthew Dillon if ((dev = vp->v_rdev) == NULL)
87521864bc5SMatthew Dillon return ENXIO;
87621864bc5SMatthew Dillon
877845bd036SMatthew Dillon /*
878845bd036SMatthew Dillon * Simple devices that don't care. Retain the shared lock.
879845bd036SMatthew Dillon */
880845bd036SMatthew Dillon if (dev_dflags(dev) & D_QUICK) {
881845bd036SMatthew Dillon vn_unlock(vp);
882845bd036SMatthew Dillon error = dev_dopen(dev, ap->a_mode, S_IFCHR,
8835bd45597SMatthew Dillon ap->a_cred, ap->a_fpp, vp);
884845bd036SMatthew Dillon vn_lock(vp, LK_SHARED | LK_RETRY);
885*9d388b3bSMatthew Dillon if (error)
886*9d388b3bSMatthew Dillon return error;
887845bd036SMatthew Dillon vop_stdopen(ap);
888845bd036SMatthew Dillon goto skip;
889845bd036SMatthew Dillon }
89012cdc371SMatthew Dillon
891845bd036SMatthew Dillon /*
892845bd036SMatthew Dillon * Slow code
893845bd036SMatthew Dillon */
894845bd036SMatthew Dillon vn_lock(vp, LK_UPGRADE | LK_RETRY);
8955bd45597SMatthew Dillon if (node && ap->a_fpp) {
8962c94b9eeSMatthew Dillon int exists;
8972c94b9eeSMatthew Dillon
89821864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
899948d6431SMatthew Dillon lockmgr(&devfs_lock, LK_SHARED);
90007dfa375SAlex Hornung
90112cdc371SMatthew Dillon ndev = devfs_clone(dev, node->d_dir.d_name,
90212cdc371SMatthew Dillon node->d_dir.d_namlen,
90307dfa375SAlex Hornung ap->a_mode, ap->a_cred);
90407dfa375SAlex Hornung if (ndev != NULL) {
905948d6431SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
906948d6431SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
907898c91eeSMatthew Dillon newnode = devfs_create_device_node(
908898c91eeSMatthew Dillon DEVFS_MNTDATA(vp->v_mount)->root_node,
9092c94b9eeSMatthew Dillon ndev, &exists, NULL, NULL);
91007dfa375SAlex Hornung /* XXX: possibly destroy device if this happens */
91107dfa375SAlex Hornung
91207dfa375SAlex Hornung if (newnode != NULL) {
91307dfa375SAlex Hornung dev = ndev;
9142c94b9eeSMatthew Dillon if (exists == 0)
91507dfa375SAlex Hornung devfs_link_dev(dev);
91621864bc5SMatthew Dillon
917898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
918898c91eeSMatthew Dillon "parent here is: %s, node is: |%s|\n",
9198e78a293SSascha Wildner ((node->parent->node_type == Nroot) ?
920898c91eeSMatthew Dillon "ROOT!" : node->parent->d_dir.d_name),
921898c91eeSMatthew Dillon newnode->d_dir.d_name);
922898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
923898c91eeSMatthew Dillon "test: %s\n",
924898c91eeSMatthew Dillon ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
92521864bc5SMatthew Dillon
926ca8d7677SMatthew Dillon /*
9272c94b9eeSMatthew Dillon * orig_vp is set to the original vp if we
9282c94b9eeSMatthew Dillon * cloned.
929ca8d7677SMatthew Dillon */
930ca8d7677SMatthew Dillon /* node->flags |= DEVFS_CLONED; */
931898c91eeSMatthew Dillon devfs_allocv(&vp, newnode);
932ca8d7677SMatthew Dillon orig_vp = ap->a_vp;
93321864bc5SMatthew Dillon ap->a_vp = vp;
93421864bc5SMatthew Dillon }
93507dfa375SAlex Hornung }
93621864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
937845bd036SMatthew Dillon
93818baefa4SAlex Hornung /*
9392c94b9eeSMatthew Dillon * Synchronize devfs here to make sure that, if the cloned
9402c94b9eeSMatthew Dillon * device creates other device nodes in addition to the
9412c94b9eeSMatthew Dillon * cloned one, all of them are created by the time we return
9422c94b9eeSMatthew Dillon * from opening the cloned one.
94318baefa4SAlex Hornung */
94418baefa4SAlex Hornung if (ndev)
94518baefa4SAlex Hornung devfs_config();
94621864bc5SMatthew Dillon }
94721864bc5SMatthew Dillon
948898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
949898c91eeSMatthew Dillon "devfs_spec_open() called on %s! \n",
950898c91eeSMatthew Dillon dev->si_name);
951898c91eeSMatthew Dillon
95221864bc5SMatthew Dillon /*
95321864bc5SMatthew Dillon * Make this field valid before any I/O in ->d_open
954845bd036SMatthew Dillon *
955845bd036SMatthew Dillon * NOTE: Shared vnode lock probably held, but its ok as long
956845bd036SMatthew Dillon * as assignments are consistent.
95721864bc5SMatthew Dillon */
95821864bc5SMatthew Dillon if (!dev->si_iosize_max)
95995df18e4SFrançois Tigeot /* XXX: old DFLTPHYS == 64KB dependency */
96095df18e4SFrançois Tigeot dev->si_iosize_max = min(MAXPHYS,64*1024);
96121864bc5SMatthew Dillon
96221864bc5SMatthew Dillon if (dev_dflags(dev) & D_TTY)
9632247fe02SMatthew Dillon vsetflags(vp, VISTTY);
96421864bc5SMatthew Dillon
9659f889dc4SMatthew Dillon /*
9665bd45597SMatthew Dillon * Open the underlying device.
9675bd45597SMatthew Dillon *
9685bd45597SMatthew Dillon * NOTE: If the dev open returns EALREADY it has completed the open
9695bd45597SMatthew Dillon * operation and is returning a fully initialized *a->a_fpp
9705bd45597SMatthew Dillon * (which it may also have replaced). This includes issuing
9715bd45597SMatthew Dillon * any necessary VOP_OPEN().
9725bd45597SMatthew Dillon *
9735bd45597SMatthew Dillon * Also, the returned ap->a_fpp might not be DTYPE_VNODE and
9745bd45597SMatthew Dillon * if it is might not be using the vp we supplied to it.
9759f889dc4SMatthew Dillon */
97621864bc5SMatthew Dillon vn_unlock(vp);
9775bd45597SMatthew Dillon error = dev_dopen(dev, ap->a_mode, S_IFCHR,
9785bd45597SMatthew Dillon ap->a_cred, ap->a_fpp, vp);
97921864bc5SMatthew Dillon vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
98021864bc5SMatthew Dillon
9815bd45597SMatthew Dillon if (__predict_false(error == EALREADY)) {
9825bd45597SMatthew Dillon if (orig_vp)
9835bd45597SMatthew Dillon vput(vp);
9845bd45597SMatthew Dillon return 0;
9855bd45597SMatthew Dillon }
9865bd45597SMatthew Dillon
987ca8d7677SMatthew Dillon /*
988ca8d7677SMatthew Dillon * Clean up any cloned vp if we error out.
989ca8d7677SMatthew Dillon */
9905bd45597SMatthew Dillon if (__predict_false(error != 0)) {
991ca8d7677SMatthew Dillon if (orig_vp) {
99221864bc5SMatthew Dillon vput(vp);
993ca8d7677SMatthew Dillon ap->a_vp = orig_vp;
994ca8d7677SMatthew Dillon /* orig_vp = NULL; */
995ca8d7677SMatthew Dillon }
99621864bc5SMatthew Dillon return error;
99721864bc5SMatthew Dillon }
99821864bc5SMatthew Dillon
999d894b0ebSAntonio Huete /*
1000ddd7de82SAntonio Huete * This checks if the disk device is going to be opened for writing.
1001ddd7de82SAntonio Huete * It will be only allowed in the cases where securelevel permits it
1002ddd7de82SAntonio Huete * and it's not mounted R/W.
1003d894b0ebSAntonio Huete */
1004ddd7de82SAntonio Huete if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
1005d894b0ebSAntonio Huete (ap->a_cred != FSCRED)) {
1006ddd7de82SAntonio Huete
1007ddd7de82SAntonio Huete /* Very secure mode. No open for writing allowed */
1008d894b0ebSAntonio Huete if (securelevel >= 2)
1009d894b0ebSAntonio Huete return EPERM;
1010ddd7de82SAntonio Huete
1011ddd7de82SAntonio Huete /*
1012ddd7de82SAntonio Huete * If it is mounted R/W, do not allow to open for writing.
1013ddd7de82SAntonio Huete * In the case it's mounted read-only but securelevel
1014ddd7de82SAntonio Huete * is >= 1, then do not allow opening for writing either.
1015ddd7de82SAntonio Huete */
1016ddd7de82SAntonio Huete if (vfs_mountedon(vp)) {
1017ddd7de82SAntonio Huete if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
1018ddd7de82SAntonio Huete return EBUSY;
1019ddd7de82SAntonio Huete else if (securelevel >= 1)
1020ddd7de82SAntonio Huete return EPERM;
1021d894b0ebSAntonio Huete }
1022d894b0ebSAntonio Huete }
102321864bc5SMatthew Dillon
1024845bd036SMatthew Dillon /*
1025845bd036SMatthew Dillon * NOTE: vnode is still locked shared. t_stop assignment should
1026845bd036SMatthew Dillon * remain consistent so we should be ok.
1027845bd036SMatthew Dillon */
102821864bc5SMatthew Dillon if (dev_dflags(dev) & D_TTY) {
102921864bc5SMatthew Dillon if (dev->si_tty) {
103021864bc5SMatthew Dillon struct tty *tp;
103121864bc5SMatthew Dillon tp = dev->si_tty;
103221864bc5SMatthew Dillon if (!tp->t_stop) {
1033898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1034898c91eeSMatthew Dillon "devfs: no t_stop\n");
103521864bc5SMatthew Dillon tp->t_stop = nottystop;
103621864bc5SMatthew Dillon }
103721864bc5SMatthew Dillon }
103821864bc5SMatthew Dillon }
103921864bc5SMatthew Dillon
1040845bd036SMatthew Dillon /*
1041845bd036SMatthew Dillon * NOTE: vnode is still locked shared. assignments should
1042845bd036SMatthew Dillon * remain consistent so we should be ok. However,
1043845bd036SMatthew Dillon * upgrade to exclusive if we need a VM object.
1044845bd036SMatthew Dillon */
104521864bc5SMatthew Dillon if (vn_isdisk(vp, NULL)) {
104621864bc5SMatthew Dillon if (!dev->si_bsize_phys)
104721864bc5SMatthew Dillon dev->si_bsize_phys = DEV_BSIZE;
1048b0d18f7dSMatthew Dillon vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
104921864bc5SMatthew Dillon }
105021864bc5SMatthew Dillon
105121864bc5SMatthew Dillon vop_stdopen(ap);
105207dfa375SAlex Hornung #if 0
1053898c91eeSMatthew Dillon if (node)
1054d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
105507dfa375SAlex Hornung #endif
1056b80b4c32SMatthew Dillon /*
1057b80b4c32SMatthew Dillon * If we replaced the vp the vop_stdopen() call will have loaded
1058b80b4c32SMatthew Dillon * it into fp->f_data and vref()d the vp, giving us two refs. So
1059b80b4c32SMatthew Dillon * instead of just unlocking it here we have to vput() it.
1060b80b4c32SMatthew Dillon */
1061ca8d7677SMatthew Dillon if (orig_vp)
1062b80b4c32SMatthew Dillon vput(vp);
106321864bc5SMatthew Dillon
106421864bc5SMatthew Dillon /* Ugly pty magic, to make pty devices appear once they are opened */
1065845bd036SMatthew Dillon if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY) {
1066845bd036SMatthew Dillon if (node->flags & DEVFS_INVISIBLE)
1067898c91eeSMatthew Dillon node->flags &= ~DEVFS_INVISIBLE;
1068845bd036SMatthew Dillon }
106921864bc5SMatthew Dillon
1070845bd036SMatthew Dillon skip:
10715bd45597SMatthew Dillon if (ap->a_fpp) {
10725bd45597SMatthew Dillon struct file *fp = *ap->a_fpp;
10735bd45597SMatthew Dillon
10745bd45597SMatthew Dillon KKASSERT(fp->f_type == DTYPE_VNODE);
10755bd45597SMatthew Dillon KKASSERT((fp->f_flag & FMASK) == (ap->a_mode & FMASK));
10765bd45597SMatthew Dillon fp->f_ops = &devfs_dev_fileops;
10775bd45597SMatthew Dillon KKASSERT(fp->f_data == (void *)vp);
107821864bc5SMatthew Dillon }
107921864bc5SMatthew Dillon
108021864bc5SMatthew Dillon return 0;
108121864bc5SMatthew Dillon }
108221864bc5SMatthew Dillon
108321864bc5SMatthew Dillon static int
devfs_spec_close(struct vop_close_args * ap)108421864bc5SMatthew Dillon devfs_spec_close(struct vop_close_args *ap)
108521864bc5SMatthew Dillon {
108692fb0c6aSMatthew Dillon struct devfs_node *node;
108721864bc5SMatthew Dillon struct proc *p = curproc;
108821864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
108921864bc5SMatthew Dillon cdev_t dev = vp->v_rdev;
109021864bc5SMatthew Dillon int error = 0;
109121864bc5SMatthew Dillon int needrelock;
109297248700SMatthew Dillon int opencount;
109321864bc5SMatthew Dillon
109412cdc371SMatthew Dillon /*
1095845bd036SMatthew Dillon * Devices flagged D_QUICK require no special handling.
1096845bd036SMatthew Dillon */
1097845bd036SMatthew Dillon if (dev && dev_dflags(dev) & D_QUICK) {
1098845bd036SMatthew Dillon opencount = vp->v_opencount;
1099845bd036SMatthew Dillon if (opencount <= 1)
1100845bd036SMatthew Dillon opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1101845bd036SMatthew Dillon if (((vp->v_flag & VRECLAIMED) ||
1102845bd036SMatthew Dillon (dev_dflags(dev) & D_TRACKCLOSE) ||
1103845bd036SMatthew Dillon (opencount == 1))) {
1104845bd036SMatthew Dillon vn_unlock(vp);
1105845bd036SMatthew Dillon error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1106845bd036SMatthew Dillon vn_lock(vp, LK_SHARED | LK_RETRY);
1107845bd036SMatthew Dillon }
1108845bd036SMatthew Dillon goto skip;
1109845bd036SMatthew Dillon }
1110845bd036SMatthew Dillon
1111845bd036SMatthew Dillon /*
111212cdc371SMatthew Dillon * We do special tests on the opencount so unfortunately we need
111312cdc371SMatthew Dillon * an exclusive lock.
111412cdc371SMatthew Dillon */
111512cdc371SMatthew Dillon vn_lock(vp, LK_UPGRADE | LK_RETRY);
111612cdc371SMatthew Dillon
111752f98df9SSamuel J. Greear if (dev)
1118898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1119898c91eeSMatthew Dillon "devfs_spec_close() called on %s! \n",
1120898c91eeSMatthew Dillon dev->si_name);
112152f98df9SSamuel J. Greear else
112252f98df9SSamuel J. Greear devfs_debug(DEVFS_DEBUG_DEBUG,
112352f98df9SSamuel J. Greear "devfs_spec_close() called, null vode!\n");
112421864bc5SMatthew Dillon
112521864bc5SMatthew Dillon /*
112621864bc5SMatthew Dillon * A couple of hacks for devices and tty devices. The
112721864bc5SMatthew Dillon * vnode ref count cannot be used to figure out the
112821864bc5SMatthew Dillon * last close, but we can use v_opencount now that
112921864bc5SMatthew Dillon * revoke works properly.
113021864bc5SMatthew Dillon *
113121864bc5SMatthew Dillon * Detect the last close on a controlling terminal and clear
113221864bc5SMatthew Dillon * the session (half-close).
113397248700SMatthew Dillon *
113497248700SMatthew Dillon * XXX opencount is not SMP safe. The vnode is locked but there
113597248700SMatthew Dillon * may be multiple vnodes referencing the same device.
113621864bc5SMatthew Dillon */
113797248700SMatthew Dillon if (dev) {
113897248700SMatthew Dillon /*
113997248700SMatthew Dillon * NOTE: Try to avoid global tokens when testing opencount
114097248700SMatthew Dillon * XXX hack, fixme. needs a struct lock and opencount in
114197248700SMatthew Dillon * struct cdev itself.
114297248700SMatthew Dillon */
114321864bc5SMatthew Dillon reference_dev(dev);
114497248700SMatthew Dillon opencount = vp->v_opencount;
114597248700SMatthew Dillon if (opencount <= 1)
114697248700SMatthew Dillon opencount = count_dev(dev); /* XXX NOT SMP SAFE */
114797248700SMatthew Dillon } else {
114897248700SMatthew Dillon opencount = 0;
114997248700SMatthew Dillon }
115021864bc5SMatthew Dillon
115121864bc5SMatthew Dillon if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
115221864bc5SMatthew Dillon p->p_session->s_ttyvp = NULL;
115321864bc5SMatthew Dillon vrele(vp);
115421864bc5SMatthew Dillon }
115521864bc5SMatthew Dillon
115621864bc5SMatthew Dillon /*
115721864bc5SMatthew Dillon * Vnodes can be opened and closed multiple times. Do not really
115821864bc5SMatthew Dillon * close the device unless (1) it is being closed forcibly,
115921864bc5SMatthew Dillon * (2) the device wants to track closes, or (3) this is the last
116021864bc5SMatthew Dillon * vnode doing its last close on the device.
116121864bc5SMatthew Dillon *
116221864bc5SMatthew Dillon * XXX the VXLOCK (force close) case can leave vnodes referencing
116321864bc5SMatthew Dillon * a closed device. This might not occur now that our revoke is
116421864bc5SMatthew Dillon * fixed.
116521864bc5SMatthew Dillon */
116621864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
116721864bc5SMatthew Dillon if (dev && ((vp->v_flag & VRECLAIMED) ||
116821864bc5SMatthew Dillon (dev_dflags(dev) & D_TRACKCLOSE) ||
116997248700SMatthew Dillon (opencount == 1))) {
1170898c91eeSMatthew Dillon /*
117192fb0c6aSMatthew Dillon * Ugly pty magic, to make pty devices disappear again once
117292fb0c6aSMatthew Dillon * they are closed.
117392fb0c6aSMatthew Dillon */
117492fb0c6aSMatthew Dillon node = DEVFS_NODE(ap->a_vp);
117592fb0c6aSMatthew Dillon if (node && (node->flags & DEVFS_PTY))
117692fb0c6aSMatthew Dillon node->flags |= DEVFS_INVISIBLE;
117792fb0c6aSMatthew Dillon
117892fb0c6aSMatthew Dillon /*
1179495d3a1eSMatthew Dillon * Unlock around dev_dclose(), unless the vnode is
1180495d3a1eSMatthew Dillon * undergoing a vgone/reclaim (during umount).
1181898c91eeSMatthew Dillon */
118221864bc5SMatthew Dillon needrelock = 0;
1183495d3a1eSMatthew Dillon if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) {
118421864bc5SMatthew Dillon needrelock = 1;
118521864bc5SMatthew Dillon vn_unlock(vp);
118621864bc5SMatthew Dillon }
1187898c91eeSMatthew Dillon
1188898c91eeSMatthew Dillon /*
118992fb0c6aSMatthew Dillon * WARNING! If the device destroys itself the devfs node
119092fb0c6aSMatthew Dillon * can disappear here.
1191495d3a1eSMatthew Dillon *
1192495d3a1eSMatthew Dillon * WARNING! vn_lock() will fail if the vp is in a VRECLAIM,
1193495d3a1eSMatthew Dillon * which can occur during umount.
1194898c91eeSMatthew Dillon */
1195ce486e08SMarkus Pfeiffer error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
119692fb0c6aSMatthew Dillon /* node is now stale */
119721864bc5SMatthew Dillon
1198495d3a1eSMatthew Dillon if (needrelock) {
1199b458d1abSMatthew Dillon if (vn_lock(vp, LK_EXCLUSIVE |
1200b458d1abSMatthew Dillon LK_RETRY |
1201b458d1abSMatthew Dillon LK_FAILRECLAIM) != 0) {
1202495d3a1eSMatthew Dillon panic("devfs_spec_close: vnode %p "
1203495d3a1eSMatthew Dillon "unexpectedly could not be relocked",
1204495d3a1eSMatthew Dillon vp);
1205495d3a1eSMatthew Dillon }
1206495d3a1eSMatthew Dillon }
120721864bc5SMatthew Dillon } else {
120821864bc5SMatthew Dillon error = 0;
120921864bc5SMatthew Dillon }
121021864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
1211898c91eeSMatthew Dillon
121221864bc5SMatthew Dillon /*
121321864bc5SMatthew Dillon * Track the actual opens and closes on the vnode. The last close
1214898c91eeSMatthew Dillon * disassociates the rdev. If the rdev is already disassociated or
1215898c91eeSMatthew Dillon * the opencount is already 0, the vnode might have been revoked
1216898c91eeSMatthew Dillon * and no further opencount tracking occurs.
121721864bc5SMatthew Dillon */
1218898c91eeSMatthew Dillon if (dev)
121921864bc5SMatthew Dillon release_dev(dev);
1220845bd036SMatthew Dillon skip:
1221898c91eeSMatthew Dillon if (vp->v_opencount > 0)
122221864bc5SMatthew Dillon vop_stdclose(ap);
122321864bc5SMatthew Dillon return(error);
122421864bc5SMatthew Dillon
122521864bc5SMatthew Dillon }
122621864bc5SMatthew Dillon
122721864bc5SMatthew Dillon
122821864bc5SMatthew Dillon static int
devfs_fo_close(struct file * fp)12299f889dc4SMatthew Dillon devfs_fo_close(struct file *fp)
123021864bc5SMatthew Dillon {
123121864bc5SMatthew Dillon struct vnode *vp = (struct vnode *)fp->f_data;
1232898c91eeSMatthew Dillon int error;
123321864bc5SMatthew Dillon
123421864bc5SMatthew Dillon fp->f_ops = &badfileops;
12353596743eSMarkus Pfeiffer error = vn_close(vp, fp->f_flag, fp);
1236ce486e08SMarkus Pfeiffer devfs_clear_cdevpriv(fp);
123721864bc5SMatthew Dillon
123821864bc5SMatthew Dillon return (error);
123921864bc5SMatthew Dillon }
124021864bc5SMatthew Dillon
124121864bc5SMatthew Dillon
124221864bc5SMatthew Dillon /*
124321864bc5SMatthew Dillon * Device-optimized file table vnode read routine.
124421864bc5SMatthew Dillon *
124521864bc5SMatthew Dillon * This bypasses the VOP table and talks directly to the device. Most
124621864bc5SMatthew Dillon * filesystems just route to specfs and can make this optimization.
124721864bc5SMatthew Dillon */
124821864bc5SMatthew Dillon static int
devfs_fo_read(struct file * fp,struct uio * uio,struct ucred * cred,int flags)12499f889dc4SMatthew Dillon devfs_fo_read(struct file *fp, struct uio *uio,
1250898c91eeSMatthew Dillon struct ucred *cred, int flags)
125121864bc5SMatthew Dillon {
1252898c91eeSMatthew Dillon struct devfs_node *node;
125321864bc5SMatthew Dillon struct vnode *vp;
125421864bc5SMatthew Dillon int ioflag;
125521864bc5SMatthew Dillon int error;
125621864bc5SMatthew Dillon cdev_t dev;
125721864bc5SMatthew Dillon
125821864bc5SMatthew Dillon KASSERT(uio->uio_td == curthread,
125921864bc5SMatthew Dillon ("uio_td %p is not td %p", uio->uio_td, curthread));
126021864bc5SMatthew Dillon
12613a1032a6SAlex Hornung if (uio->uio_resid == 0)
12623a1032a6SAlex Hornung return 0;
12633a1032a6SAlex Hornung
126421864bc5SMatthew Dillon vp = (struct vnode *)fp->f_data;
12653a1032a6SAlex Hornung if (vp == NULL || vp->v_type == VBAD)
12663a1032a6SAlex Hornung return EBADF;
12673a1032a6SAlex Hornung
1268898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
126921864bc5SMatthew Dillon
12703a1032a6SAlex Hornung if ((dev = vp->v_rdev) == NULL)
12713a1032a6SAlex Hornung return EBADF;
12723a1032a6SAlex Hornung
127321864bc5SMatthew Dillon reference_dev(dev);
127421864bc5SMatthew Dillon
127521864bc5SMatthew Dillon if ((flags & O_FOFFSET) == 0)
127621864bc5SMatthew Dillon uio->uio_offset = fp->f_offset;
127721864bc5SMatthew Dillon
127821864bc5SMatthew Dillon ioflag = 0;
127921864bc5SMatthew Dillon if (flags & O_FBLOCKING) {
128021864bc5SMatthew Dillon /* ioflag &= ~IO_NDELAY; */
128121864bc5SMatthew Dillon } else if (flags & O_FNONBLOCKING) {
128221864bc5SMatthew Dillon ioflag |= IO_NDELAY;
128321864bc5SMatthew Dillon } else if (fp->f_flag & FNONBLOCK) {
128421864bc5SMatthew Dillon ioflag |= IO_NDELAY;
128521864bc5SMatthew Dillon }
1286c72df65dSMatthew Dillon if (fp->f_flag & O_DIRECT) {
128721864bc5SMatthew Dillon ioflag |= IO_DIRECT;
128821864bc5SMatthew Dillon }
128921864bc5SMatthew Dillon ioflag |= sequential_heuristic(uio, fp);
129021864bc5SMatthew Dillon
12918c530b23SJohannes Hofmann error = dev_dread(dev, uio, ioflag, fp);
129221864bc5SMatthew Dillon
129321864bc5SMatthew Dillon release_dev(dev);
1294898c91eeSMatthew Dillon if (node)
1295d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
129621864bc5SMatthew Dillon if ((flags & O_FOFFSET) == 0)
129721864bc5SMatthew Dillon fp->f_offset = uio->uio_offset;
129821864bc5SMatthew Dillon fp->f_nextoff = uio->uio_offset;
12993a1032a6SAlex Hornung
130021864bc5SMatthew Dillon return (error);
130121864bc5SMatthew Dillon }
130221864bc5SMatthew Dillon
130321864bc5SMatthew Dillon
130421864bc5SMatthew Dillon static int
devfs_fo_write(struct file * fp,struct uio * uio,struct ucred * cred,int flags)13059f889dc4SMatthew Dillon devfs_fo_write(struct file *fp, struct uio *uio,
1306898c91eeSMatthew Dillon struct ucred *cred, int flags)
130721864bc5SMatthew Dillon {
1308898c91eeSMatthew Dillon struct devfs_node *node;
130921864bc5SMatthew Dillon struct vnode *vp;
131021864bc5SMatthew Dillon int ioflag;
131121864bc5SMatthew Dillon int error;
131221864bc5SMatthew Dillon cdev_t dev;
131321864bc5SMatthew Dillon
131421864bc5SMatthew Dillon KASSERT(uio->uio_td == curthread,
131521864bc5SMatthew Dillon ("uio_td %p is not p %p", uio->uio_td, curthread));
131621864bc5SMatthew Dillon
131721864bc5SMatthew Dillon vp = (struct vnode *)fp->f_data;
13183a1032a6SAlex Hornung if (vp == NULL || vp->v_type == VBAD)
13193a1032a6SAlex Hornung return EBADF;
13203a1032a6SAlex Hornung
1321898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
13223a1032a6SAlex Hornung
132321864bc5SMatthew Dillon if (vp->v_type == VREG)
132421864bc5SMatthew Dillon bwillwrite(uio->uio_resid);
13253a1032a6SAlex Hornung
132621864bc5SMatthew Dillon vp = (struct vnode *)fp->f_data;
132721864bc5SMatthew Dillon
13283a1032a6SAlex Hornung if ((dev = vp->v_rdev) == NULL)
13293a1032a6SAlex Hornung return EBADF;
13303a1032a6SAlex Hornung
133121864bc5SMatthew Dillon reference_dev(dev);
133221864bc5SMatthew Dillon
133321864bc5SMatthew Dillon if ((flags & O_FOFFSET) == 0)
133421864bc5SMatthew Dillon uio->uio_offset = fp->f_offset;
133521864bc5SMatthew Dillon
133621864bc5SMatthew Dillon ioflag = IO_UNIT;
133721864bc5SMatthew Dillon if (vp->v_type == VREG &&
133821864bc5SMatthew Dillon ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
133921864bc5SMatthew Dillon ioflag |= IO_APPEND;
134021864bc5SMatthew Dillon }
134121864bc5SMatthew Dillon
134221864bc5SMatthew Dillon if (flags & O_FBLOCKING) {
134321864bc5SMatthew Dillon /* ioflag &= ~IO_NDELAY; */
134421864bc5SMatthew Dillon } else if (flags & O_FNONBLOCKING) {
134521864bc5SMatthew Dillon ioflag |= IO_NDELAY;
134621864bc5SMatthew Dillon } else if (fp->f_flag & FNONBLOCK) {
134721864bc5SMatthew Dillon ioflag |= IO_NDELAY;
134821864bc5SMatthew Dillon }
1349c72df65dSMatthew Dillon if (fp->f_flag & O_DIRECT) {
135021864bc5SMatthew Dillon ioflag |= IO_DIRECT;
135121864bc5SMatthew Dillon }
135221864bc5SMatthew Dillon if (flags & O_FASYNCWRITE) {
135321864bc5SMatthew Dillon /* ioflag &= ~IO_SYNC; */
135421864bc5SMatthew Dillon } else if (flags & O_FSYNCWRITE) {
135521864bc5SMatthew Dillon ioflag |= IO_SYNC;
135621864bc5SMatthew Dillon } else if (fp->f_flag & O_FSYNC) {
135721864bc5SMatthew Dillon ioflag |= IO_SYNC;
135821864bc5SMatthew Dillon }
135921864bc5SMatthew Dillon
136021864bc5SMatthew Dillon if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
136121864bc5SMatthew Dillon ioflag |= IO_SYNC;
136221864bc5SMatthew Dillon ioflag |= sequential_heuristic(uio, fp);
136321864bc5SMatthew Dillon
13648c530b23SJohannes Hofmann error = dev_dwrite(dev, uio, ioflag, fp);
136521864bc5SMatthew Dillon
136621864bc5SMatthew Dillon release_dev(dev);
136707dfa375SAlex Hornung if (node) {
1368d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
1369d489a79aSMatthew Dillon vfs_timestamp(&node->mtime);
137007dfa375SAlex Hornung }
137121864bc5SMatthew Dillon
137221864bc5SMatthew Dillon if ((flags & O_FOFFSET) == 0)
137321864bc5SMatthew Dillon fp->f_offset = uio->uio_offset;
137421864bc5SMatthew Dillon fp->f_nextoff = uio->uio_offset;
13753a1032a6SAlex Hornung
137621864bc5SMatthew Dillon return (error);
137721864bc5SMatthew Dillon }
137821864bc5SMatthew Dillon
137921864bc5SMatthew Dillon
138021864bc5SMatthew Dillon static int
devfs_fo_stat(struct file * fp,struct stat * sb,struct ucred * cred)13819f889dc4SMatthew Dillon devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
138221864bc5SMatthew Dillon {
138321864bc5SMatthew Dillon struct vnode *vp;
138421864bc5SMatthew Dillon struct vattr vattr;
138521864bc5SMatthew Dillon struct vattr *vap;
138621864bc5SMatthew Dillon u_short mode;
138721864bc5SMatthew Dillon cdev_t dev;
13883a1032a6SAlex Hornung int error;
13893a1032a6SAlex Hornung
13903a1032a6SAlex Hornung vp = (struct vnode *)fp->f_data;
13913a1032a6SAlex Hornung if (vp == NULL || vp->v_type == VBAD)
13923a1032a6SAlex Hornung return EBADF;
13933a1032a6SAlex Hornung
13943a1032a6SAlex Hornung error = vn_stat(vp, sb, cred);
13953a1032a6SAlex Hornung if (error)
13963a1032a6SAlex Hornung return (error);
139721864bc5SMatthew Dillon
139821864bc5SMatthew Dillon vap = &vattr;
139921864bc5SMatthew Dillon error = VOP_GETATTR(vp, vap);
14003a1032a6SAlex Hornung if (error)
140121864bc5SMatthew Dillon return (error);
140221864bc5SMatthew Dillon
140321864bc5SMatthew Dillon /*
140421864bc5SMatthew Dillon * Zero the spare stat fields
140521864bc5SMatthew Dillon */
140621864bc5SMatthew Dillon sb->st_lspare = 0;
1407d98152a8SMatthew Dillon sb->st_qspare2 = 0;
140821864bc5SMatthew Dillon
140921864bc5SMatthew Dillon /*
141021864bc5SMatthew Dillon * Copy from vattr table ... or not in case it's a cloned device
141121864bc5SMatthew Dillon */
141221864bc5SMatthew Dillon if (vap->va_fsid != VNOVAL)
141321864bc5SMatthew Dillon sb->st_dev = vap->va_fsid;
141421864bc5SMatthew Dillon else
141521864bc5SMatthew Dillon sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
141621864bc5SMatthew Dillon
141721864bc5SMatthew Dillon sb->st_ino = vap->va_fileid;
141821864bc5SMatthew Dillon
141921864bc5SMatthew Dillon mode = vap->va_mode;
142021864bc5SMatthew Dillon mode |= S_IFCHR;
142121864bc5SMatthew Dillon sb->st_mode = mode;
142221864bc5SMatthew Dillon
142321864bc5SMatthew Dillon if (vap->va_nlink > (nlink_t)-1)
142421864bc5SMatthew Dillon sb->st_nlink = (nlink_t)-1;
142521864bc5SMatthew Dillon else
142621864bc5SMatthew Dillon sb->st_nlink = vap->va_nlink;
14273a1032a6SAlex Hornung
142821864bc5SMatthew Dillon sb->st_uid = vap->va_uid;
142921864bc5SMatthew Dillon sb->st_gid = vap->va_gid;
14302ac7d105SSascha Wildner sb->st_rdev = devid_from_dev(DEVFS_NODE(vp)->d_dev);
14312d076755SAlex Hornung sb->st_size = vap->va_bytes;
143221864bc5SMatthew Dillon sb->st_atimespec = vap->va_atime;
143321864bc5SMatthew Dillon sb->st_mtimespec = vap->va_mtime;
143421864bc5SMatthew Dillon sb->st_ctimespec = vap->va_ctime;
143521864bc5SMatthew Dillon
143621864bc5SMatthew Dillon /*
143721864bc5SMatthew Dillon * A VCHR and VBLK device may track the last access and last modified
143821864bc5SMatthew Dillon * time independantly of the filesystem. This is particularly true
143921864bc5SMatthew Dillon * because device read and write calls may bypass the filesystem.
144021864bc5SMatthew Dillon */
144121864bc5SMatthew Dillon if (vp->v_type == VCHR || vp->v_type == VBLK) {
144221864bc5SMatthew Dillon dev = vp->v_rdev;
144321864bc5SMatthew Dillon if (dev != NULL) {
144421864bc5SMatthew Dillon if (dev->si_lastread) {
1445cec73927SMatthew Dillon sb->st_atimespec.tv_sec = time_second +
14461e45dd8cSMatthew Dillon (dev->si_lastread -
14471e45dd8cSMatthew Dillon time_uptime);
144821864bc5SMatthew Dillon sb->st_atimespec.tv_nsec = 0;
144921864bc5SMatthew Dillon }
145021864bc5SMatthew Dillon if (dev->si_lastwrite) {
14511e45dd8cSMatthew Dillon sb->st_mtimespec.tv_sec = time_second +
14521e45dd8cSMatthew Dillon (dev->si_lastwrite -
14531e45dd8cSMatthew Dillon time_uptime);
14541e45dd8cSMatthew Dillon sb->st_mtimespec.tv_nsec = 0;
145521864bc5SMatthew Dillon }
145621864bc5SMatthew Dillon }
145721864bc5SMatthew Dillon }
145821864bc5SMatthew Dillon
145921864bc5SMatthew Dillon /*
146021864bc5SMatthew Dillon * According to www.opengroup.org, the meaning of st_blksize is
146121864bc5SMatthew Dillon * "a filesystem-specific preferred I/O block size for this
146221864bc5SMatthew Dillon * object. In some filesystem types, this may vary from file
146321864bc5SMatthew Dillon * to file"
146421864bc5SMatthew Dillon * Default to PAGE_SIZE after much discussion.
146521864bc5SMatthew Dillon */
146621864bc5SMatthew Dillon
146721864bc5SMatthew Dillon sb->st_blksize = PAGE_SIZE;
146821864bc5SMatthew Dillon
146921864bc5SMatthew Dillon sb->st_flags = vap->va_flags;
147021864bc5SMatthew Dillon
14712b3f93eaSMatthew Dillon error = caps_priv_check(cred, SYSCAP_NOVFS_GENERATION);
147221864bc5SMatthew Dillon if (error)
147321864bc5SMatthew Dillon sb->st_gen = 0;
147421864bc5SMatthew Dillon else
147521864bc5SMatthew Dillon sb->st_gen = (u_int32_t)vap->va_gen;
147621864bc5SMatthew Dillon
147721864bc5SMatthew Dillon sb->st_blocks = vap->va_bytes / S_BLKSIZE;
147821864bc5SMatthew Dillon
147934c6728eSMatthew Dillon /*
148034c6728eSMatthew Dillon * This is for ABI compatibility <= 5.7 (for ABI change made in
148134c6728eSMatthew Dillon * 5.7 master).
148234c6728eSMatthew Dillon */
148334c6728eSMatthew Dillon sb->__old_st_blksize = sb->st_blksize;
148434c6728eSMatthew Dillon
148521864bc5SMatthew Dillon return (0);
148621864bc5SMatthew Dillon }
148721864bc5SMatthew Dillon
148821864bc5SMatthew Dillon
148921864bc5SMatthew Dillon static int
devfs_fo_kqfilter(struct file * fp,struct knote * kn)14909f889dc4SMatthew Dillon devfs_fo_kqfilter(struct file *fp, struct knote *kn)
149121864bc5SMatthew Dillon {
149221864bc5SMatthew Dillon struct vnode *vp;
149321864bc5SMatthew Dillon int error;
149421864bc5SMatthew Dillon cdev_t dev;
149521864bc5SMatthew Dillon
149621864bc5SMatthew Dillon vp = (struct vnode *)fp->f_data;
149721864bc5SMatthew Dillon if (vp == NULL || vp->v_type == VBAD) {
149821864bc5SMatthew Dillon error = EBADF;
149921864bc5SMatthew Dillon goto done;
150021864bc5SMatthew Dillon }
150121864bc5SMatthew Dillon if ((dev = vp->v_rdev) == NULL) {
150221864bc5SMatthew Dillon error = EBADF;
150321864bc5SMatthew Dillon goto done;
150421864bc5SMatthew Dillon }
150521864bc5SMatthew Dillon reference_dev(dev);
150621864bc5SMatthew Dillon
15078c530b23SJohannes Hofmann error = dev_dkqfilter(dev, kn, fp);
150821864bc5SMatthew Dillon
150921864bc5SMatthew Dillon release_dev(dev);
151021864bc5SMatthew Dillon
151121864bc5SMatthew Dillon done:
1512b287d649SMatthew Dillon return (error);
151321864bc5SMatthew Dillon }
151421864bc5SMatthew Dillon
151521864bc5SMatthew Dillon static int
devfs_fo_ioctl(struct file * fp,u_long com,caddr_t data,struct ucred * ucred,struct sysmsg * msg)15169f889dc4SMatthew Dillon devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
151787baaf0cSMatthew Dillon struct ucred *ucred, struct sysmsg *msg)
151821864bc5SMatthew Dillon {
15191d0de3d3SSascha Wildner #if 0
1520898c91eeSMatthew Dillon struct devfs_node *node;
15211d0de3d3SSascha Wildner #endif
1522898c91eeSMatthew Dillon struct vnode *vp;
152321864bc5SMatthew Dillon struct vnode *ovp;
152421864bc5SMatthew Dillon cdev_t dev;
152521864bc5SMatthew Dillon int error;
152621864bc5SMatthew Dillon struct fiodname_args *name_args;
152721864bc5SMatthew Dillon size_t namlen;
152821864bc5SMatthew Dillon const char *name;
152921864bc5SMatthew Dillon
1530898c91eeSMatthew Dillon vp = ((struct vnode *)fp->f_data);
15313a1032a6SAlex Hornung
15323a1032a6SAlex Hornung if ((dev = vp->v_rdev) == NULL)
15333a1032a6SAlex Hornung return EBADF; /* device was revoked */
15343a1032a6SAlex Hornung
15353a1032a6SAlex Hornung reference_dev(dev);
153621864bc5SMatthew Dillon
15371d0de3d3SSascha Wildner #if 0
1538898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
15391d0de3d3SSascha Wildner #endif
1540898c91eeSMatthew Dillon
1541898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
15429f889dc4SMatthew Dillon "devfs_fo_ioctl() called! for dev %s\n",
1543898c91eeSMatthew Dillon dev->si_name);
154421864bc5SMatthew Dillon
154521864bc5SMatthew Dillon if (com == FIODTYPE) {
154621864bc5SMatthew Dillon *(int *)data = dev_dflags(dev) & D_TYPEMASK;
154721864bc5SMatthew Dillon error = 0;
154821864bc5SMatthew Dillon goto out;
154921864bc5SMatthew Dillon } else if (com == FIODNAME) {
155021864bc5SMatthew Dillon name_args = (struct fiodname_args *)data;
155121864bc5SMatthew Dillon name = dev->si_name;
155221864bc5SMatthew Dillon namlen = strlen(name) + 1;
155321864bc5SMatthew Dillon
1554898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1555898c91eeSMatthew Dillon "ioctl, got: FIODNAME for %s\n", name);
155621864bc5SMatthew Dillon
155721864bc5SMatthew Dillon if (namlen <= name_args->len)
155821864bc5SMatthew Dillon error = copyout(dev->si_name, name_args->name, namlen);
155921864bc5SMatthew Dillon else
156021864bc5SMatthew Dillon error = EINVAL;
156121864bc5SMatthew Dillon
1562898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1563898c91eeSMatthew Dillon "ioctl stuff: error: %d\n", error);
156421864bc5SMatthew Dillon goto out;
156521864bc5SMatthew Dillon }
15663a1032a6SAlex Hornung
15678c530b23SJohannes Hofmann error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp);
15683a1032a6SAlex Hornung
156907dfa375SAlex Hornung #if 0
1570898c91eeSMatthew Dillon if (node) {
1571d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
1572d489a79aSMatthew Dillon vfs_timestamp(&node->mtime);
157321864bc5SMatthew Dillon }
157407dfa375SAlex Hornung #endif
1575898c91eeSMatthew Dillon if (com == TIOCSCTTY) {
1576898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
15779f889dc4SMatthew Dillon "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
1578898c91eeSMatthew Dillon dev->si_name);
1579898c91eeSMatthew Dillon }
158021864bc5SMatthew Dillon if (error == 0 && com == TIOCSCTTY) {
158121864bc5SMatthew Dillon struct proc *p = curthread->td_proc;
158221864bc5SMatthew Dillon struct session *sess;
1583898c91eeSMatthew Dillon
1584898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
15859f889dc4SMatthew Dillon "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
1586898c91eeSMatthew Dillon dev->si_name);
158721864bc5SMatthew Dillon if (p == NULL) {
158821864bc5SMatthew Dillon error = ENOTTY;
158921864bc5SMatthew Dillon goto out;
159021864bc5SMatthew Dillon }
159121864bc5SMatthew Dillon sess = p->p_session;
1592898c91eeSMatthew Dillon
1593898c91eeSMatthew Dillon /*
1594898c91eeSMatthew Dillon * Do nothing if reassigning same control tty
1595898c91eeSMatthew Dillon */
159621864bc5SMatthew Dillon if (sess->s_ttyvp == vp) {
159721864bc5SMatthew Dillon error = 0;
159821864bc5SMatthew Dillon goto out;
159921864bc5SMatthew Dillon }
1600898c91eeSMatthew Dillon
1601898c91eeSMatthew Dillon /*
1602898c91eeSMatthew Dillon * Get rid of reference to old control tty
1603898c91eeSMatthew Dillon */
160421864bc5SMatthew Dillon ovp = sess->s_ttyvp;
160521864bc5SMatthew Dillon vref(vp);
160621864bc5SMatthew Dillon sess->s_ttyvp = vp;
160721864bc5SMatthew Dillon if (ovp)
160821864bc5SMatthew Dillon vrele(ovp);
160921864bc5SMatthew Dillon }
161021864bc5SMatthew Dillon
161121864bc5SMatthew Dillon out:
16123a1032a6SAlex Hornung release_dev(dev);
16139f889dc4SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
161421864bc5SMatthew Dillon return (error);
161521864bc5SMatthew Dillon }
161621864bc5SMatthew Dillon
161721864bc5SMatthew Dillon
161821864bc5SMatthew Dillon static int
devfs_spec_fsync(struct vop_fsync_args * ap)161921864bc5SMatthew Dillon devfs_spec_fsync(struct vop_fsync_args *ap)
162021864bc5SMatthew Dillon {
162121864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
162221864bc5SMatthew Dillon int error;
162321864bc5SMatthew Dillon
162421864bc5SMatthew Dillon if (!vn_isdisk(vp, NULL))
162521864bc5SMatthew Dillon return (0);
162621864bc5SMatthew Dillon
162721864bc5SMatthew Dillon /*
162821864bc5SMatthew Dillon * Flush all dirty buffers associated with a block device.
162921864bc5SMatthew Dillon */
163021864bc5SMatthew Dillon error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
163121864bc5SMatthew Dillon return (error);
163221864bc5SMatthew Dillon }
163321864bc5SMatthew Dillon
163421864bc5SMatthew Dillon static int
devfs_spec_read(struct vop_read_args * ap)163521864bc5SMatthew Dillon devfs_spec_read(struct vop_read_args *ap)
163621864bc5SMatthew Dillon {
1637898c91eeSMatthew Dillon struct devfs_node *node;
163821864bc5SMatthew Dillon struct vnode *vp;
163921864bc5SMatthew Dillon struct uio *uio;
164021864bc5SMatthew Dillon cdev_t dev;
164121864bc5SMatthew Dillon int error;
164221864bc5SMatthew Dillon
164321864bc5SMatthew Dillon vp = ap->a_vp;
164421864bc5SMatthew Dillon dev = vp->v_rdev;
164521864bc5SMatthew Dillon uio = ap->a_uio;
1646898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
164721864bc5SMatthew Dillon
164821864bc5SMatthew Dillon if (dev == NULL) /* device was revoked */
164921864bc5SMatthew Dillon return (EBADF);
165021864bc5SMatthew Dillon if (uio->uio_resid == 0)
165121864bc5SMatthew Dillon return (0);
165221864bc5SMatthew Dillon
165321864bc5SMatthew Dillon vn_unlock(vp);
16548c530b23SJohannes Hofmann error = dev_dread(dev, uio, ap->a_ioflag, NULL);
1655845bd036SMatthew Dillon vn_lock(vp, LK_SHARED | LK_RETRY);
165621864bc5SMatthew Dillon
1657898c91eeSMatthew Dillon if (node)
1658d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
165921864bc5SMatthew Dillon
166021864bc5SMatthew Dillon return (error);
166121864bc5SMatthew Dillon }
166221864bc5SMatthew Dillon
166321864bc5SMatthew Dillon /*
166421864bc5SMatthew Dillon * Vnode op for write
166521864bc5SMatthew Dillon *
166621864bc5SMatthew Dillon * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
166721864bc5SMatthew Dillon * struct ucred *a_cred)
166821864bc5SMatthew Dillon */
166921864bc5SMatthew Dillon static int
devfs_spec_write(struct vop_write_args * ap)167021864bc5SMatthew Dillon devfs_spec_write(struct vop_write_args *ap)
167121864bc5SMatthew Dillon {
1672898c91eeSMatthew Dillon struct devfs_node *node;
167321864bc5SMatthew Dillon struct vnode *vp;
167421864bc5SMatthew Dillon struct uio *uio;
167521864bc5SMatthew Dillon cdev_t dev;
167621864bc5SMatthew Dillon int error;
167721864bc5SMatthew Dillon
167821864bc5SMatthew Dillon vp = ap->a_vp;
167921864bc5SMatthew Dillon dev = vp->v_rdev;
168021864bc5SMatthew Dillon uio = ap->a_uio;
1681898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
168221864bc5SMatthew Dillon
168321864bc5SMatthew Dillon KKASSERT(uio->uio_segflg != UIO_NOCOPY);
168421864bc5SMatthew Dillon
168521864bc5SMatthew Dillon if (dev == NULL) /* device was revoked */
168621864bc5SMatthew Dillon return (EBADF);
168721864bc5SMatthew Dillon
168821864bc5SMatthew Dillon vn_unlock(vp);
16898c530b23SJohannes Hofmann error = dev_dwrite(dev, uio, ap->a_ioflag, NULL);
169021864bc5SMatthew Dillon vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
169121864bc5SMatthew Dillon
169207dfa375SAlex Hornung if (node) {
1693d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
1694d489a79aSMatthew Dillon vfs_timestamp(&node->mtime);
169507dfa375SAlex Hornung }
169621864bc5SMatthew Dillon
169721864bc5SMatthew Dillon return (error);
169821864bc5SMatthew Dillon }
169921864bc5SMatthew Dillon
170021864bc5SMatthew Dillon /*
170121864bc5SMatthew Dillon * Device ioctl operation.
170221864bc5SMatthew Dillon *
170321864bc5SMatthew Dillon * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
170487baaf0cSMatthew Dillon * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
170521864bc5SMatthew Dillon */
170621864bc5SMatthew Dillon static int
devfs_spec_ioctl(struct vop_ioctl_args * ap)170721864bc5SMatthew Dillon devfs_spec_ioctl(struct vop_ioctl_args *ap)
170821864bc5SMatthew Dillon {
170921864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
17101d0de3d3SSascha Wildner #if 0
1711898c91eeSMatthew Dillon struct devfs_node *node;
17121d0de3d3SSascha Wildner #endif
1713898c91eeSMatthew Dillon cdev_t dev;
171421864bc5SMatthew Dillon
171521864bc5SMatthew Dillon if ((dev = vp->v_rdev) == NULL)
171621864bc5SMatthew Dillon return (EBADF); /* device was revoked */
17171d0de3d3SSascha Wildner #if 0
1718898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
171921864bc5SMatthew Dillon
1720898c91eeSMatthew Dillon if (node) {
1721d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
1722d489a79aSMatthew Dillon vfs_timestamp(&node->mtime);
172321864bc5SMatthew Dillon }
172407dfa375SAlex Hornung #endif
172521864bc5SMatthew Dillon
172687baaf0cSMatthew Dillon return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
17278c530b23SJohannes Hofmann ap->a_cred, ap->a_sysmsg, NULL));
172821864bc5SMatthew Dillon }
172921864bc5SMatthew Dillon
173021864bc5SMatthew Dillon /*
173121864bc5SMatthew Dillon * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
173221864bc5SMatthew Dillon */
173321864bc5SMatthew Dillon /* ARGSUSED */
173421864bc5SMatthew Dillon static int
devfs_spec_kqfilter(struct vop_kqfilter_args * ap)173521864bc5SMatthew Dillon devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
173621864bc5SMatthew Dillon {
173721864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
17381d0de3d3SSascha Wildner #if 0
1739898c91eeSMatthew Dillon struct devfs_node *node;
17401d0de3d3SSascha Wildner #endif
1741898c91eeSMatthew Dillon cdev_t dev;
174221864bc5SMatthew Dillon
174321864bc5SMatthew Dillon if ((dev = vp->v_rdev) == NULL)
1744b287d649SMatthew Dillon return (EBADF); /* device was revoked (EBADF) */
17451d0de3d3SSascha Wildner #if 0
1746898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
174721864bc5SMatthew Dillon
1748898c91eeSMatthew Dillon if (node)
1749d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
175007dfa375SAlex Hornung #endif
175121864bc5SMatthew Dillon
17528c530b23SJohannes Hofmann return (dev_dkqfilter(dev, ap->a_kn, NULL));
175321864bc5SMatthew Dillon }
175421864bc5SMatthew Dillon
175521864bc5SMatthew Dillon /*
175621864bc5SMatthew Dillon * Convert a vnode strategy call into a device strategy call. Vnode strategy
175721864bc5SMatthew Dillon * calls are not limited to device DMA limits so we have to deal with the
175821864bc5SMatthew Dillon * case.
175921864bc5SMatthew Dillon *
176021864bc5SMatthew Dillon * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
176121864bc5SMatthew Dillon */
176221864bc5SMatthew Dillon static int
devfs_spec_strategy(struct vop_strategy_args * ap)176321864bc5SMatthew Dillon devfs_spec_strategy(struct vop_strategy_args *ap)
176421864bc5SMatthew Dillon {
176521864bc5SMatthew Dillon struct bio *bio = ap->a_bio;
176621864bc5SMatthew Dillon struct buf *bp = bio->bio_buf;
176721864bc5SMatthew Dillon struct buf *nbp;
176821864bc5SMatthew Dillon struct vnode *vp;
176921864bc5SMatthew Dillon struct mount *mp;
177021864bc5SMatthew Dillon int chunksize;
177121864bc5SMatthew Dillon int maxiosize;
177221864bc5SMatthew Dillon
177321864bc5SMatthew Dillon if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
177421864bc5SMatthew Dillon buf_start(bp);
177521864bc5SMatthew Dillon
177621864bc5SMatthew Dillon /*
177721864bc5SMatthew Dillon * Collect statistics on synchronous and asynchronous read
177821864bc5SMatthew Dillon * and write counts for disks that have associated filesystems.
177921864bc5SMatthew Dillon */
178021864bc5SMatthew Dillon vp = ap->a_vp;
178121864bc5SMatthew Dillon KKASSERT(vp->v_rdev != NULL); /* XXX */
178221864bc5SMatthew Dillon if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
178321864bc5SMatthew Dillon if (bp->b_cmd == BUF_CMD_READ) {
178421864bc5SMatthew Dillon if (bp->b_flags & BIO_SYNC)
178521864bc5SMatthew Dillon mp->mnt_stat.f_syncreads++;
178621864bc5SMatthew Dillon else
178721864bc5SMatthew Dillon mp->mnt_stat.f_asyncreads++;
178821864bc5SMatthew Dillon } else {
178921864bc5SMatthew Dillon if (bp->b_flags & BIO_SYNC)
179021864bc5SMatthew Dillon mp->mnt_stat.f_syncwrites++;
179121864bc5SMatthew Dillon else
179221864bc5SMatthew Dillon mp->mnt_stat.f_asyncwrites++;
179321864bc5SMatthew Dillon }
179421864bc5SMatthew Dillon }
179521864bc5SMatthew Dillon
179621864bc5SMatthew Dillon /*
179721864bc5SMatthew Dillon * Device iosize limitations only apply to read and write. Shortcut
179821864bc5SMatthew Dillon * the I/O if it fits.
179921864bc5SMatthew Dillon */
180021864bc5SMatthew Dillon if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1801898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1802898c91eeSMatthew Dillon "%s: si_iosize_max not set!\n",
1803898c91eeSMatthew Dillon dev_dname(vp->v_rdev));
180421864bc5SMatthew Dillon maxiosize = MAXPHYS;
180521864bc5SMatthew Dillon }
180621864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 2
180721864bc5SMatthew Dillon maxiosize = 4096;
180821864bc5SMatthew Dillon #endif
180921864bc5SMatthew Dillon if (bp->b_bcount <= maxiosize ||
181021864bc5SMatthew Dillon (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
181121864bc5SMatthew Dillon dev_dstrategy_chain(vp->v_rdev, bio);
181221864bc5SMatthew Dillon return (0);
181321864bc5SMatthew Dillon }
181421864bc5SMatthew Dillon
181521864bc5SMatthew Dillon /*
181621864bc5SMatthew Dillon * Clone the buffer and set up an I/O chain to chunk up the I/O.
181721864bc5SMatthew Dillon */
181821864bc5SMatthew Dillon nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
181921864bc5SMatthew Dillon initbufbio(nbp);
182021864bc5SMatthew Dillon buf_dep_init(nbp);
182121864bc5SMatthew Dillon BUF_LOCK(nbp, LK_EXCLUSIVE);
182221864bc5SMatthew Dillon BUF_KERNPROC(nbp);
182321864bc5SMatthew Dillon nbp->b_vp = vp;
1824b3f55d88SMatthew Dillon nbp->b_flags = B_PAGING | B_KVABIO | (bp->b_flags & B_BNOCLIP);
1825b3f55d88SMatthew Dillon nbp->b_cpumask = bp->b_cpumask;
182621864bc5SMatthew Dillon nbp->b_data = bp->b_data;
182721864bc5SMatthew Dillon nbp->b_bio1.bio_done = devfs_spec_strategy_done;
182821864bc5SMatthew Dillon nbp->b_bio1.bio_offset = bio->bio_offset;
182921864bc5SMatthew Dillon nbp->b_bio1.bio_caller_info1.ptr = bio;
183021864bc5SMatthew Dillon
183121864bc5SMatthew Dillon /*
183221864bc5SMatthew Dillon * Start the first transfer
183321864bc5SMatthew Dillon */
183421864bc5SMatthew Dillon if (vn_isdisk(vp, NULL))
183521864bc5SMatthew Dillon chunksize = vp->v_rdev->si_bsize_phys;
183621864bc5SMatthew Dillon else
183721864bc5SMatthew Dillon chunksize = DEV_BSIZE;
1838ed183f8cSSascha Wildner chunksize = rounddown(maxiosize, chunksize);
183921864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1840898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1841898c91eeSMatthew Dillon "spec_strategy chained I/O chunksize=%d\n",
1842898c91eeSMatthew Dillon chunksize);
184321864bc5SMatthew Dillon #endif
184421864bc5SMatthew Dillon nbp->b_cmd = bp->b_cmd;
184521864bc5SMatthew Dillon nbp->b_bcount = chunksize;
184621864bc5SMatthew Dillon nbp->b_bufsize = chunksize; /* used to detect a short I/O */
184721864bc5SMatthew Dillon nbp->b_bio1.bio_caller_info2.index = chunksize;
184821864bc5SMatthew Dillon
184921864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1850898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1851898c91eeSMatthew Dillon "spec_strategy: chain %p offset %d/%d bcount %d\n",
185221864bc5SMatthew Dillon bp, 0, bp->b_bcount, nbp->b_bcount);
185321864bc5SMatthew Dillon #endif
185421864bc5SMatthew Dillon
185521864bc5SMatthew Dillon dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
185621864bc5SMatthew Dillon
185721864bc5SMatthew Dillon if (DEVFS_NODE(vp)) {
1858d489a79aSMatthew Dillon vfs_timestamp(&DEVFS_NODE(vp)->atime);
1859d489a79aSMatthew Dillon vfs_timestamp(&DEVFS_NODE(vp)->mtime);
186021864bc5SMatthew Dillon }
186121864bc5SMatthew Dillon
186221864bc5SMatthew Dillon return (0);
186321864bc5SMatthew Dillon }
186421864bc5SMatthew Dillon
186521864bc5SMatthew Dillon /*
186621864bc5SMatthew Dillon * Chunked up transfer completion routine - chain transfers until done
186777912481SMatthew Dillon *
186877912481SMatthew Dillon * NOTE: MPSAFE callback.
186921864bc5SMatthew Dillon */
187021864bc5SMatthew Dillon static
187121864bc5SMatthew Dillon void
devfs_spec_strategy_done(struct bio * nbio)187221864bc5SMatthew Dillon devfs_spec_strategy_done(struct bio *nbio)
187321864bc5SMatthew Dillon {
187421864bc5SMatthew Dillon struct buf *nbp = nbio->bio_buf;
187521864bc5SMatthew Dillon struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
187621864bc5SMatthew Dillon struct buf *bp = bio->bio_buf; /* original bp */
187721864bc5SMatthew Dillon int chunksize = nbio->bio_caller_info2.index; /* chunking */
187821864bc5SMatthew Dillon int boffset = nbp->b_data - bp->b_data;
187921864bc5SMatthew Dillon
188021864bc5SMatthew Dillon if (nbp->b_flags & B_ERROR) {
188121864bc5SMatthew Dillon /*
188221864bc5SMatthew Dillon * An error terminates the chain, propogate the error back
188321864bc5SMatthew Dillon * to the original bp
188421864bc5SMatthew Dillon */
188521864bc5SMatthew Dillon bp->b_flags |= B_ERROR;
188621864bc5SMatthew Dillon bp->b_error = nbp->b_error;
188721864bc5SMatthew Dillon bp->b_resid = bp->b_bcount - boffset +
188821864bc5SMatthew Dillon (nbp->b_bcount - nbp->b_resid);
188921864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1890898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1891898c91eeSMatthew Dillon "spec_strategy: chain %p error %d bcount %d/%d\n",
189221864bc5SMatthew Dillon bp, bp->b_error, bp->b_bcount,
189321864bc5SMatthew Dillon bp->b_bcount - bp->b_resid);
189421864bc5SMatthew Dillon #endif
189521864bc5SMatthew Dillon } else if (nbp->b_resid) {
189621864bc5SMatthew Dillon /*
189721864bc5SMatthew Dillon * A short read or write terminates the chain
189821864bc5SMatthew Dillon */
189921864bc5SMatthew Dillon bp->b_error = nbp->b_error;
190021864bc5SMatthew Dillon bp->b_resid = bp->b_bcount - boffset +
190121864bc5SMatthew Dillon (nbp->b_bcount - nbp->b_resid);
190221864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1903898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1904898c91eeSMatthew Dillon "spec_strategy: chain %p short read(1) "
1905898c91eeSMatthew Dillon "bcount %d/%d\n",
190621864bc5SMatthew Dillon bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
190721864bc5SMatthew Dillon #endif
190821864bc5SMatthew Dillon } else if (nbp->b_bcount != nbp->b_bufsize) {
190921864bc5SMatthew Dillon /*
191021864bc5SMatthew Dillon * A short read or write can also occur by truncating b_bcount
191121864bc5SMatthew Dillon */
191221864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1913898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1914898c91eeSMatthew Dillon "spec_strategy: chain %p short read(2) "
1915898c91eeSMatthew Dillon "bcount %d/%d\n",
191621864bc5SMatthew Dillon bp, nbp->b_bcount + boffset, bp->b_bcount);
191721864bc5SMatthew Dillon #endif
191821864bc5SMatthew Dillon bp->b_error = 0;
191921864bc5SMatthew Dillon bp->b_bcount = nbp->b_bcount + boffset;
192021864bc5SMatthew Dillon bp->b_resid = nbp->b_resid;
192121864bc5SMatthew Dillon } else if (nbp->b_bcount + boffset == bp->b_bcount) {
192221864bc5SMatthew Dillon /*
192321864bc5SMatthew Dillon * No more data terminates the chain
192421864bc5SMatthew Dillon */
192521864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1926898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1927898c91eeSMatthew Dillon "spec_strategy: chain %p finished bcount %d\n",
192821864bc5SMatthew Dillon bp, bp->b_bcount);
192921864bc5SMatthew Dillon #endif
193021864bc5SMatthew Dillon bp->b_error = 0;
193121864bc5SMatthew Dillon bp->b_resid = 0;
193221864bc5SMatthew Dillon } else {
193321864bc5SMatthew Dillon /*
193421864bc5SMatthew Dillon * Continue the chain
193521864bc5SMatthew Dillon */
193621864bc5SMatthew Dillon boffset += nbp->b_bcount;
193721864bc5SMatthew Dillon nbp->b_data = bp->b_data + boffset;
193821864bc5SMatthew Dillon nbp->b_bcount = bp->b_bcount - boffset;
193921864bc5SMatthew Dillon if (nbp->b_bcount > chunksize)
194021864bc5SMatthew Dillon nbp->b_bcount = chunksize;
194121864bc5SMatthew Dillon nbp->b_bio1.bio_done = devfs_spec_strategy_done;
194221864bc5SMatthew Dillon nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
194321864bc5SMatthew Dillon
194421864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1945898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1946898c91eeSMatthew Dillon "spec_strategy: chain %p offset %d/%d bcount %d\n",
194721864bc5SMatthew Dillon bp, boffset, bp->b_bcount, nbp->b_bcount);
194821864bc5SMatthew Dillon #endif
194921864bc5SMatthew Dillon
195021864bc5SMatthew Dillon dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
1951b5d7061dSMatthew Dillon return;
195221864bc5SMatthew Dillon }
1953b5d7061dSMatthew Dillon
1954b5d7061dSMatthew Dillon /*
1955b5d7061dSMatthew Dillon * Fall through to here on termination. biodone(bp) and
1956b5d7061dSMatthew Dillon * clean up and free nbp.
1957b5d7061dSMatthew Dillon */
1958b5d7061dSMatthew Dillon biodone(bio);
1959b5d7061dSMatthew Dillon BUF_UNLOCK(nbp);
1960b5d7061dSMatthew Dillon uninitbufbio(nbp);
1961b5d7061dSMatthew Dillon kfree(nbp, M_DEVBUF);
196221864bc5SMatthew Dillon }
196321864bc5SMatthew Dillon
196421864bc5SMatthew Dillon /*
196521864bc5SMatthew Dillon * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
196621864bc5SMatthew Dillon */
196721864bc5SMatthew Dillon static int
devfs_spec_freeblks(struct vop_freeblks_args * ap)196821864bc5SMatthew Dillon devfs_spec_freeblks(struct vop_freeblks_args *ap)
196921864bc5SMatthew Dillon {
197021864bc5SMatthew Dillon struct buf *bp;
197121864bc5SMatthew Dillon
197221864bc5SMatthew Dillon /*
197353005b09SMatthew Dillon * Must be a synchronous operation
197421864bc5SMatthew Dillon */
197521864bc5SMatthew Dillon KKASSERT(ap->a_vp->v_rdev != NULL);
1976bf390b25SAlex Hornung if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0)
197721864bc5SMatthew Dillon return (0);
1978bf20632cSMatthew Dillon bp = getpbuf(NULL);
197921864bc5SMatthew Dillon bp->b_cmd = BUF_CMD_FREEBLKS;
198053005b09SMatthew Dillon bp->b_bio1.bio_flags |= BIO_SYNC;
198121864bc5SMatthew Dillon bp->b_bio1.bio_offset = ap->a_offset;
198253005b09SMatthew Dillon bp->b_bio1.bio_done = biodone_sync;
198321864bc5SMatthew Dillon bp->b_bcount = ap->a_length;
198421864bc5SMatthew Dillon dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
198553005b09SMatthew Dillon biowait(&bp->b_bio1, "TRIM");
1986bf20632cSMatthew Dillon relpbuf(bp, NULL);
198753005b09SMatthew Dillon
198821864bc5SMatthew Dillon return (0);
198921864bc5SMatthew Dillon }
199021864bc5SMatthew Dillon
199121864bc5SMatthew Dillon /*
199221864bc5SMatthew Dillon * Implement degenerate case where the block requested is the block
199321864bc5SMatthew Dillon * returned, and assume that the entire device is contiguous in regards
199421864bc5SMatthew Dillon * to the contiguous block range (runp and runb).
199521864bc5SMatthew Dillon *
199621864bc5SMatthew Dillon * spec_bmap(struct vnode *a_vp, off_t a_loffset,
199721864bc5SMatthew Dillon * off_t *a_doffsetp, int *a_runp, int *a_runb)
199821864bc5SMatthew Dillon */
199921864bc5SMatthew Dillon static int
devfs_spec_bmap(struct vop_bmap_args * ap)200021864bc5SMatthew Dillon devfs_spec_bmap(struct vop_bmap_args *ap)
200121864bc5SMatthew Dillon {
200221864bc5SMatthew Dillon if (ap->a_doffsetp != NULL)
200321864bc5SMatthew Dillon *ap->a_doffsetp = ap->a_loffset;
200421864bc5SMatthew Dillon if (ap->a_runp != NULL)
200521864bc5SMatthew Dillon *ap->a_runp = MAXBSIZE;
200621864bc5SMatthew Dillon if (ap->a_runb != NULL) {
200721864bc5SMatthew Dillon if (ap->a_loffset < MAXBSIZE)
200821864bc5SMatthew Dillon *ap->a_runb = (int)ap->a_loffset;
200921864bc5SMatthew Dillon else
201021864bc5SMatthew Dillon *ap->a_runb = MAXBSIZE;
201121864bc5SMatthew Dillon }
201221864bc5SMatthew Dillon return (0);
201321864bc5SMatthew Dillon }
201421864bc5SMatthew Dillon
201521864bc5SMatthew Dillon
201621864bc5SMatthew Dillon /*
201721864bc5SMatthew Dillon * Special device advisory byte-level locks.
201821864bc5SMatthew Dillon *
201921864bc5SMatthew Dillon * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
202021864bc5SMatthew Dillon * struct flock *a_fl, int a_flags)
202121864bc5SMatthew Dillon */
202221864bc5SMatthew Dillon /* ARGSUSED */
202321864bc5SMatthew Dillon static int
devfs_spec_advlock(struct vop_advlock_args * ap)202421864bc5SMatthew Dillon devfs_spec_advlock(struct vop_advlock_args *ap)
202521864bc5SMatthew Dillon {
202621864bc5SMatthew Dillon return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
202721864bc5SMatthew Dillon }
202821864bc5SMatthew Dillon
202977912481SMatthew Dillon /*
203077912481SMatthew Dillon * NOTE: MPSAFE callback.
203177912481SMatthew Dillon */
203221864bc5SMatthew Dillon static void
devfs_spec_getpages_iodone(struct bio * bio)203321864bc5SMatthew Dillon devfs_spec_getpages_iodone(struct bio *bio)
203421864bc5SMatthew Dillon {
203521864bc5SMatthew Dillon bio->bio_buf->b_cmd = BUF_CMD_DONE;
203621864bc5SMatthew Dillon wakeup(bio->bio_buf);
203721864bc5SMatthew Dillon }
203821864bc5SMatthew Dillon
203921864bc5SMatthew Dillon /*
204021864bc5SMatthew Dillon * spec_getpages() - get pages associated with device vnode.
204121864bc5SMatthew Dillon *
204221864bc5SMatthew Dillon * Note that spec_read and spec_write do not use the buffer cache, so we
204321864bc5SMatthew Dillon * must fully implement getpages here.
204421864bc5SMatthew Dillon */
204521864bc5SMatthew Dillon static int
devfs_spec_getpages(struct vop_getpages_args * ap)204621864bc5SMatthew Dillon devfs_spec_getpages(struct vop_getpages_args *ap)
204721864bc5SMatthew Dillon {
204821864bc5SMatthew Dillon vm_offset_t kva;
204921864bc5SMatthew Dillon int error;
205021864bc5SMatthew Dillon int i, pcount, size;
205121864bc5SMatthew Dillon struct buf *bp;
205221864bc5SMatthew Dillon vm_page_t m;
205321864bc5SMatthew Dillon vm_ooffset_t offset;
205421864bc5SMatthew Dillon int toff, nextoff, nread;
205521864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
205621864bc5SMatthew Dillon int blksiz;
205721864bc5SMatthew Dillon int gotreqpage;
205821864bc5SMatthew Dillon
205921864bc5SMatthew Dillon error = 0;
206021864bc5SMatthew Dillon pcount = round_page(ap->a_count) / PAGE_SIZE;
206121864bc5SMatthew Dillon
206221864bc5SMatthew Dillon /*
206321864bc5SMatthew Dillon * Calculate the offset of the transfer and do sanity check.
206421864bc5SMatthew Dillon */
206521864bc5SMatthew Dillon offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
206621864bc5SMatthew Dillon
206721864bc5SMatthew Dillon /*
206821864bc5SMatthew Dillon * Round up physical size for real devices. We cannot round using
206921864bc5SMatthew Dillon * v_mount's block size data because v_mount has nothing to do with
207021864bc5SMatthew Dillon * the device. i.e. it's usually '/dev'. We need the physical block
207121864bc5SMatthew Dillon * size for the device itself.
207221864bc5SMatthew Dillon *
207321864bc5SMatthew Dillon * We can't use v_rdev->si_mountpoint because it only exists when the
207421864bc5SMatthew Dillon * block device is mounted. However, we can use v_rdev.
207521864bc5SMatthew Dillon */
207621864bc5SMatthew Dillon if (vn_isdisk(vp, NULL))
207721864bc5SMatthew Dillon blksiz = vp->v_rdev->si_bsize_phys;
207821864bc5SMatthew Dillon else
207921864bc5SMatthew Dillon blksiz = DEV_BSIZE;
208021864bc5SMatthew Dillon
2081965b839fSSascha Wildner size = roundup2(ap->a_count, blksiz);
208221864bc5SMatthew Dillon
2083ad8b1a17SMatthew Dillon bp = getpbuf_kva(NULL);
208421864bc5SMatthew Dillon kva = (vm_offset_t)bp->b_data;
208521864bc5SMatthew Dillon
208621864bc5SMatthew Dillon /*
208721864bc5SMatthew Dillon * Map the pages to be read into the kva.
208821864bc5SMatthew Dillon */
2089b3f55d88SMatthew Dillon pmap_qenter_noinval(kva, ap->a_m, pcount);
209021864bc5SMatthew Dillon
209121864bc5SMatthew Dillon /* Build a minimal buffer header. */
209221864bc5SMatthew Dillon bp->b_cmd = BUF_CMD_READ;
2093b3f55d88SMatthew Dillon bp->b_flags |= B_KVABIO;
209421864bc5SMatthew Dillon bp->b_bcount = size;
209521864bc5SMatthew Dillon bp->b_resid = 0;
209677912481SMatthew Dillon bsetrunningbufspace(bp, size);
209721864bc5SMatthew Dillon
209821864bc5SMatthew Dillon bp->b_bio1.bio_offset = offset;
209921864bc5SMatthew Dillon bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
210021864bc5SMatthew Dillon
210121864bc5SMatthew Dillon mycpu->gd_cnt.v_vnodein++;
210221864bc5SMatthew Dillon mycpu->gd_cnt.v_vnodepgsin += pcount;
210321864bc5SMatthew Dillon
210421864bc5SMatthew Dillon /* Do the input. */
210521864bc5SMatthew Dillon vn_strategy(ap->a_vp, &bp->b_bio1);
210621864bc5SMatthew Dillon
210721864bc5SMatthew Dillon crit_enter();
210821864bc5SMatthew Dillon
210921864bc5SMatthew Dillon /* We definitely need to be at splbio here. */
211021864bc5SMatthew Dillon while (bp->b_cmd != BUF_CMD_DONE)
211121864bc5SMatthew Dillon tsleep(bp, 0, "spread", 0);
211221864bc5SMatthew Dillon
211321864bc5SMatthew Dillon crit_exit();
211421864bc5SMatthew Dillon
211521864bc5SMatthew Dillon if (bp->b_flags & B_ERROR) {
211621864bc5SMatthew Dillon if (bp->b_error)
211721864bc5SMatthew Dillon error = bp->b_error;
211821864bc5SMatthew Dillon else
211921864bc5SMatthew Dillon error = EIO;
212021864bc5SMatthew Dillon }
212121864bc5SMatthew Dillon
212221864bc5SMatthew Dillon /*
212321864bc5SMatthew Dillon * If EOF is encountered we must zero-extend the result in order
212421864bc5SMatthew Dillon * to ensure that the page does not contain garabge. When no
212521864bc5SMatthew Dillon * error occurs, an early EOF is indicated if b_bcount got truncated.
212621864bc5SMatthew Dillon * b_resid is relative to b_bcount and should be 0, but some devices
212721864bc5SMatthew Dillon * might indicate an EOF with b_resid instead of truncating b_bcount.
212821864bc5SMatthew Dillon */
212921864bc5SMatthew Dillon nread = bp->b_bcount - bp->b_resid;
2130b3f55d88SMatthew Dillon if (nread < ap->a_count) {
2131b3f55d88SMatthew Dillon bkvasync(bp);
213221864bc5SMatthew Dillon bzero((caddr_t)kva + nread, ap->a_count - nread);
2133b3f55d88SMatthew Dillon }
2134b3f55d88SMatthew Dillon pmap_qremove_noinval(kva, pcount);
213521864bc5SMatthew Dillon
213621864bc5SMatthew Dillon gotreqpage = 0;
213721864bc5SMatthew Dillon for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
213821864bc5SMatthew Dillon nextoff = toff + PAGE_SIZE;
213921864bc5SMatthew Dillon m = ap->a_m[i];
214021864bc5SMatthew Dillon
2141cb1cf930SMatthew Dillon /*
2142cb1cf930SMatthew Dillon * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2143cb1cf930SMatthew Dillon * pmap modified bit. pmap modified bit should have
2144cb1cf930SMatthew Dillon * already been cleared.
2145cb1cf930SMatthew Dillon */
214621864bc5SMatthew Dillon if (nextoff <= nread) {
214721864bc5SMatthew Dillon m->valid = VM_PAGE_BITS_ALL;
214821864bc5SMatthew Dillon vm_page_undirty(m);
214921864bc5SMatthew Dillon } else if (toff < nread) {
215021864bc5SMatthew Dillon /*
215121864bc5SMatthew Dillon * Since this is a VM request, we have to supply the
2152cb1cf930SMatthew Dillon * unaligned offset to allow vm_page_set_valid()
215321864bc5SMatthew Dillon * to zero sub-DEV_BSIZE'd portions of the page.
215421864bc5SMatthew Dillon */
21551a54183bSMatthew Dillon vm_page_set_valid(m, 0, nread - toff);
21561a54183bSMatthew Dillon vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
215721864bc5SMatthew Dillon } else {
215821864bc5SMatthew Dillon m->valid = 0;
215921864bc5SMatthew Dillon vm_page_undirty(m);
216021864bc5SMatthew Dillon }
216121864bc5SMatthew Dillon
216221864bc5SMatthew Dillon if (i != ap->a_reqpage) {
216321864bc5SMatthew Dillon /*
216421864bc5SMatthew Dillon * Just in case someone was asking for this page we
216521864bc5SMatthew Dillon * now tell them that it is ok to use.
216621864bc5SMatthew Dillon */
216721864bc5SMatthew Dillon if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
216821864bc5SMatthew Dillon if (m->valid) {
2169b12defdcSMatthew Dillon if (m->flags & PG_REFERENCED) {
217021864bc5SMatthew Dillon vm_page_activate(m);
217121864bc5SMatthew Dillon } else {
217221864bc5SMatthew Dillon vm_page_deactivate(m);
217321864bc5SMatthew Dillon }
217421864bc5SMatthew Dillon vm_page_wakeup(m);
217521864bc5SMatthew Dillon } else {
217621864bc5SMatthew Dillon vm_page_free(m);
217721864bc5SMatthew Dillon }
217821864bc5SMatthew Dillon } else {
217921864bc5SMatthew Dillon vm_page_free(m);
218021864bc5SMatthew Dillon }
218121864bc5SMatthew Dillon } else if (m->valid) {
218221864bc5SMatthew Dillon gotreqpage = 1;
218321864bc5SMatthew Dillon /*
218421864bc5SMatthew Dillon * Since this is a VM request, we need to make the
218521864bc5SMatthew Dillon * entire page presentable by zeroing invalid sections.
218621864bc5SMatthew Dillon */
218721864bc5SMatthew Dillon if (m->valid != VM_PAGE_BITS_ALL)
218821864bc5SMatthew Dillon vm_page_zero_invalid(m, FALSE);
218921864bc5SMatthew Dillon }
219021864bc5SMatthew Dillon }
219121864bc5SMatthew Dillon if (!gotreqpage) {
219221864bc5SMatthew Dillon m = ap->a_m[ap->a_reqpage];
219321864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_WARNING,
219421864bc5SMatthew Dillon "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
219521864bc5SMatthew Dillon devtoname(vp->v_rdev), error, bp, bp->b_vp);
219621864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_WARNING,
219721864bc5SMatthew Dillon " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
219821864bc5SMatthew Dillon size, bp->b_resid, ap->a_count, m->valid);
219921864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_WARNING,
220021864bc5SMatthew Dillon " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
220121864bc5SMatthew Dillon nread, ap->a_reqpage, (u_long)m->pindex, pcount);
220221864bc5SMatthew Dillon /*
220321864bc5SMatthew Dillon * Free the buffer header back to the swap buffer pool.
220421864bc5SMatthew Dillon */
220521864bc5SMatthew Dillon relpbuf(bp, NULL);
220621864bc5SMatthew Dillon return VM_PAGER_ERROR;
220721864bc5SMatthew Dillon }
220821864bc5SMatthew Dillon /*
220921864bc5SMatthew Dillon * Free the buffer header back to the swap buffer pool.
221021864bc5SMatthew Dillon */
221121864bc5SMatthew Dillon relpbuf(bp, NULL);
221207dfa375SAlex Hornung if (DEVFS_NODE(ap->a_vp))
2213d489a79aSMatthew Dillon vfs_timestamp(&DEVFS_NODE(ap->a_vp)->mtime);
221421864bc5SMatthew Dillon return VM_PAGER_OK;
221521864bc5SMatthew Dillon }
221621864bc5SMatthew Dillon
221721864bc5SMatthew Dillon static __inline
221821864bc5SMatthew Dillon int
sequential_heuristic(struct uio * uio,struct file * fp)221921864bc5SMatthew Dillon sequential_heuristic(struct uio *uio, struct file *fp)
222021864bc5SMatthew Dillon {
222121864bc5SMatthew Dillon /*
222221864bc5SMatthew Dillon * Sequential heuristic - detect sequential operation
222321864bc5SMatthew Dillon */
222421864bc5SMatthew Dillon if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
222521864bc5SMatthew Dillon uio->uio_offset == fp->f_nextoff) {
222621864bc5SMatthew Dillon /*
222721864bc5SMatthew Dillon * XXX we assume that the filesystem block size is
222821864bc5SMatthew Dillon * the default. Not true, but still gives us a pretty
222921864bc5SMatthew Dillon * good indicator of how sequential the read operations
223021864bc5SMatthew Dillon * are.
223121864bc5SMatthew Dillon */
2232898c91eeSMatthew Dillon int tmpseq = fp->f_seqcount;
2233898c91eeSMatthew Dillon
22344f048b1cSSascha Wildner tmpseq += howmany(uio->uio_resid, MAXBSIZE);
223521864bc5SMatthew Dillon if (tmpseq > IO_SEQMAX)
223621864bc5SMatthew Dillon tmpseq = IO_SEQMAX;
223721864bc5SMatthew Dillon fp->f_seqcount = tmpseq;
223821864bc5SMatthew Dillon return(fp->f_seqcount << IO_SEQSHIFT);
223921864bc5SMatthew Dillon }
224021864bc5SMatthew Dillon
224121864bc5SMatthew Dillon /*
224221864bc5SMatthew Dillon * Not sequential, quick draw-down of seqcount
224321864bc5SMatthew Dillon */
224421864bc5SMatthew Dillon if (fp->f_seqcount > 1)
224521864bc5SMatthew Dillon fp->f_seqcount = 1;
224621864bc5SMatthew Dillon else
224721864bc5SMatthew Dillon fp->f_seqcount = 0;
224821864bc5SMatthew Dillon return(0);
224921864bc5SMatthew Dillon }
2250