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 *);
117*cc8e70bdSSergey Zigachev static int devfs_fo_seek(struct file *, off_t, int, off_t *);
11821864bc5SMatthew Dillon static __inline int sequential_heuristic(struct uio *, struct file *);
11987baaf0cSMatthew Dillon
12021864bc5SMatthew Dillon extern struct lock devfs_lock;
12121864bc5SMatthew Dillon
12221864bc5SMatthew Dillon /*
1239f889dc4SMatthew Dillon * devfs vnode operations for regular files. All vnode ops are MPSAFE.
12421864bc5SMatthew Dillon */
12521864bc5SMatthew Dillon struct vop_ops devfs_vnode_norm_vops = {
12621864bc5SMatthew Dillon .vop_default = vop_defaultop,
1279f889dc4SMatthew Dillon .vop_access = devfs_vop_access,
12821864bc5SMatthew Dillon .vop_advlock = DEVFS_BADOP,
12921864bc5SMatthew Dillon .vop_bmap = DEVFS_BADOP,
13021864bc5SMatthew Dillon .vop_close = vop_stdclose,
1319f889dc4SMatthew Dillon .vop_getattr = devfs_vop_getattr,
1329f889dc4SMatthew Dillon .vop_inactive = devfs_vop_inactive,
13321864bc5SMatthew Dillon .vop_ncreate = DEVFS_BADOP,
1349f889dc4SMatthew Dillon .vop_nresolve = devfs_vop_nresolve,
1359f889dc4SMatthew Dillon .vop_nlookupdotdot = devfs_vop_nlookupdotdot,
13621864bc5SMatthew Dillon .vop_nlink = DEVFS_BADOP,
1379f889dc4SMatthew Dillon .vop_nmkdir = devfs_vop_nmkdir,
13821864bc5SMatthew Dillon .vop_nmknod = DEVFS_BADOP,
1399f889dc4SMatthew Dillon .vop_nremove = devfs_vop_nremove,
14021864bc5SMatthew Dillon .vop_nrename = DEVFS_BADOP,
1419f889dc4SMatthew Dillon .vop_nrmdir = devfs_vop_nrmdir,
1429f889dc4SMatthew Dillon .vop_nsymlink = devfs_vop_nsymlink,
14321864bc5SMatthew Dillon .vop_open = vop_stdopen,
14421864bc5SMatthew Dillon .vop_pathconf = vop_stdpathconf,
1459f889dc4SMatthew Dillon .vop_print = devfs_vop_print,
14621864bc5SMatthew Dillon .vop_read = DEVFS_BADOP,
1479f889dc4SMatthew Dillon .vop_readdir = devfs_vop_readdir,
1489f889dc4SMatthew Dillon .vop_readlink = devfs_vop_readlink,
1499a20a70dSMatthew Dillon .vop_reallocblks = DEVFS_BADOP,
1509f889dc4SMatthew Dillon .vop_reclaim = devfs_vop_reclaim,
1519f889dc4SMatthew Dillon .vop_setattr = devfs_vop_setattr,
15221864bc5SMatthew Dillon .vop_write = DEVFS_BADOP,
15321864bc5SMatthew Dillon .vop_ioctl = DEVFS_BADOP
15421864bc5SMatthew Dillon };
15521864bc5SMatthew Dillon
15621864bc5SMatthew Dillon /*
1579f889dc4SMatthew Dillon * devfs vnode operations for character devices. All vnode ops are MPSAFE.
15821864bc5SMatthew Dillon */
15921864bc5SMatthew Dillon struct vop_ops devfs_vnode_dev_vops = {
16021864bc5SMatthew Dillon .vop_default = vop_defaultop,
1619f889dc4SMatthew Dillon .vop_access = devfs_vop_access,
16221864bc5SMatthew Dillon .vop_advlock = devfs_spec_advlock,
16321864bc5SMatthew Dillon .vop_bmap = devfs_spec_bmap,
16421864bc5SMatthew Dillon .vop_close = devfs_spec_close,
16521864bc5SMatthew Dillon .vop_freeblks = devfs_spec_freeblks,
16621864bc5SMatthew Dillon .vop_fsync = devfs_spec_fsync,
1679f889dc4SMatthew Dillon .vop_getattr = devfs_vop_getattr,
16821864bc5SMatthew Dillon .vop_getpages = devfs_spec_getpages,
1699f889dc4SMatthew Dillon .vop_inactive = devfs_vop_inactive,
17021864bc5SMatthew Dillon .vop_open = devfs_spec_open,
17121864bc5SMatthew Dillon .vop_pathconf = vop_stdpathconf,
1729f889dc4SMatthew Dillon .vop_print = devfs_vop_print,
17321864bc5SMatthew Dillon .vop_kqfilter = devfs_spec_kqfilter,
17421864bc5SMatthew Dillon .vop_read = devfs_spec_read,
17521864bc5SMatthew Dillon .vop_readdir = DEVFS_BADOP,
17621864bc5SMatthew Dillon .vop_readlink = DEVFS_BADOP,
1779a20a70dSMatthew Dillon .vop_reallocblks = DEVFS_BADOP,
1789f889dc4SMatthew Dillon .vop_reclaim = devfs_vop_reclaim,
1799f889dc4SMatthew Dillon .vop_setattr = devfs_vop_setattr,
18021864bc5SMatthew Dillon .vop_strategy = devfs_spec_strategy,
18121864bc5SMatthew Dillon .vop_write = devfs_spec_write,
18221864bc5SMatthew Dillon .vop_ioctl = devfs_spec_ioctl
18321864bc5SMatthew Dillon };
18421864bc5SMatthew Dillon
1859f889dc4SMatthew Dillon /*
1869f889dc4SMatthew Dillon * devfs file pointer operations. All fileops are MPSAFE.
1879f889dc4SMatthew Dillon */
18821864bc5SMatthew Dillon struct vop_ops *devfs_vnode_dev_vops_p = &devfs_vnode_dev_vops;
18921864bc5SMatthew Dillon
19021864bc5SMatthew Dillon struct fileops devfs_dev_fileops = {
1919f889dc4SMatthew Dillon .fo_read = devfs_fo_read,
1929f889dc4SMatthew Dillon .fo_write = devfs_fo_write,
1939f889dc4SMatthew Dillon .fo_ioctl = devfs_fo_ioctl,
1949f889dc4SMatthew Dillon .fo_kqfilter = devfs_fo_kqfilter,
1959f889dc4SMatthew Dillon .fo_stat = devfs_fo_stat,
1969f889dc4SMatthew Dillon .fo_close = devfs_fo_close,
197*cc8e70bdSSergey Zigachev .fo_shutdown = nofo_shutdown,
198*cc8e70bdSSergey Zigachev .fo_seek = devfs_fo_seek
19921864bc5SMatthew Dillon };
20021864bc5SMatthew Dillon
2014062d050SMatthew Dillon /*
2029f889dc4SMatthew Dillon * These two functions are possibly temporary hacks for devices (aka
2039f889dc4SMatthew Dillon * the pty code) which want to control the node attributes themselves.
2044062d050SMatthew Dillon *
2054062d050SMatthew Dillon * XXX we may ultimately desire to simply remove the uid/gid/mode
2064062d050SMatthew Dillon * from the node entirely.
2079f889dc4SMatthew Dillon *
2089f889dc4SMatthew Dillon * MPSAFE - sorta. Theoretically the overwrite can compete since they
2099f889dc4SMatthew Dillon * are loading from the same fields.
2104062d050SMatthew Dillon */
2114062d050SMatthew Dillon static __inline void
node_sync_dev_get(struct devfs_node * node)2124062d050SMatthew Dillon node_sync_dev_get(struct devfs_node *node)
2134062d050SMatthew Dillon {
2144062d050SMatthew Dillon cdev_t dev;
2154062d050SMatthew Dillon
2164062d050SMatthew Dillon if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
2174062d050SMatthew Dillon node->uid = dev->si_uid;
2184062d050SMatthew Dillon node->gid = dev->si_gid;
2194062d050SMatthew Dillon node->mode = dev->si_perms;
2204062d050SMatthew Dillon }
2214062d050SMatthew Dillon }
2224062d050SMatthew Dillon
2234062d050SMatthew Dillon static __inline void
node_sync_dev_set(struct devfs_node * node)2244062d050SMatthew Dillon node_sync_dev_set(struct devfs_node *node)
2254062d050SMatthew Dillon {
2264062d050SMatthew Dillon cdev_t dev;
2274062d050SMatthew Dillon
2284062d050SMatthew Dillon if ((dev = node->d_dev) && (dev->si_flags & SI_OVERRIDE)) {
2294062d050SMatthew Dillon dev->si_uid = node->uid;
2304062d050SMatthew Dillon dev->si_gid = node->gid;
2314062d050SMatthew Dillon dev->si_perms = node->mode;
2324062d050SMatthew Dillon }
2334062d050SMatthew Dillon }
23421864bc5SMatthew Dillon
23521864bc5SMatthew Dillon /*
23621864bc5SMatthew Dillon * generic entry point for unsupported operations
23721864bc5SMatthew Dillon */
23821864bc5SMatthew Dillon static int
devfs_vop_badop(struct vop_generic_args * ap)2399f889dc4SMatthew Dillon devfs_vop_badop(struct vop_generic_args *ap)
24021864bc5SMatthew Dillon {
24121864bc5SMatthew Dillon return (EIO);
24221864bc5SMatthew Dillon }
24321864bc5SMatthew Dillon
24421864bc5SMatthew Dillon
24521864bc5SMatthew Dillon static int
devfs_vop_access(struct vop_access_args * ap)2469f889dc4SMatthew Dillon devfs_vop_access(struct vop_access_args *ap)
24721864bc5SMatthew Dillon {
24821864bc5SMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
249898c91eeSMatthew Dillon int error;
25021864bc5SMatthew Dillon
251894bbb25SAlex Hornung if (!devfs_node_is_accessible(node))
252894bbb25SAlex Hornung return ENOENT;
2534062d050SMatthew Dillon node_sync_dev_get(node);
25421864bc5SMatthew Dillon error = vop_helper_access(ap, node->uid, node->gid,
25521864bc5SMatthew Dillon node->mode, node->flags);
25621864bc5SMatthew Dillon
25721864bc5SMatthew Dillon return error;
25821864bc5SMatthew Dillon }
25921864bc5SMatthew Dillon
26021864bc5SMatthew Dillon
26121864bc5SMatthew Dillon static int
devfs_vop_inactive(struct vop_inactive_args * ap)2629f889dc4SMatthew Dillon devfs_vop_inactive(struct vop_inactive_args *ap)
26321864bc5SMatthew Dillon {
264ca8d7677SMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
26521864bc5SMatthew Dillon
266ca8d7677SMatthew Dillon if (node == NULL || (node->flags & DEVFS_NODE_LINKED) == 0)
26721864bc5SMatthew Dillon vrecycle(ap->a_vp);
26821864bc5SMatthew Dillon return 0;
26921864bc5SMatthew Dillon }
27021864bc5SMatthew Dillon
27121864bc5SMatthew Dillon
27221864bc5SMatthew Dillon static int
devfs_vop_reclaim(struct vop_reclaim_args * ap)2739f889dc4SMatthew Dillon devfs_vop_reclaim(struct vop_reclaim_args *ap)
27421864bc5SMatthew Dillon {
275be6f2e86SMatthew Dillon struct devfs_node *node;
276be6f2e86SMatthew Dillon struct vnode *vp;
277be6f2e86SMatthew Dillon int locked;
278be6f2e86SMatthew Dillon
279be6f2e86SMatthew Dillon /*
280be6f2e86SMatthew Dillon * Check if it is locked already. if not, we acquire the devfs lock
281be6f2e86SMatthew Dillon */
282ab08ac79SSascha Wildner if ((lockstatus(&devfs_lock, curthread)) != LK_EXCLUSIVE) {
28321864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
28421864bc5SMatthew Dillon locked = 1;
285be6f2e86SMatthew Dillon } else {
286be6f2e86SMatthew Dillon locked = 0;
28721864bc5SMatthew Dillon }
28821864bc5SMatthew Dillon
289be6f2e86SMatthew Dillon /*
290be6f2e86SMatthew Dillon * Get rid of the devfs_node if it is no longer linked into the
291731fd4ccSMatthew Dillon * topology. Interlocked by devfs_lock. However, be careful
292731fd4ccSMatthew Dillon * interposing other operations between cleaning out v_data and
293731fd4ccSMatthew Dillon * devfs_freep() as the node is only protected by devfs_lock
294731fd4ccSMatthew Dillon * once the vnode is disassociated.
295be6f2e86SMatthew Dillon */
296be6f2e86SMatthew Dillon vp = ap->a_vp;
297770f8279SMatthew Dillon node = DEVFS_NODE(vp);
298770f8279SMatthew Dillon
299770f8279SMatthew Dillon if (node) {
300731fd4ccSMatthew Dillon if (node->v_node != vp) {
301731fd4ccSMatthew Dillon kprintf("NODE->V_NODE MISMATCH VP=%p NODEVP=%p\n",
302731fd4ccSMatthew Dillon vp, node->v_node);
303731fd4ccSMatthew Dillon }
304731fd4ccSMatthew Dillon vp->v_data = NULL;
305731fd4ccSMatthew Dillon node->v_node = NULL;
3064062d050SMatthew Dillon if ((node->flags & DEVFS_NODE_LINKED) == 0)
3074062d050SMatthew Dillon devfs_freep(node);
30821864bc5SMatthew Dillon }
309731fd4ccSMatthew Dillon v_release_rdev(vp);
31021864bc5SMatthew Dillon
31121864bc5SMatthew Dillon if (locked)
31221864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
31321864bc5SMatthew Dillon
314be6f2e86SMatthew Dillon /*
3159b823501SAlex Hornung * v_rdev needs to be properly released using v_release_rdev
3169b823501SAlex Hornung * Make sure v_data is NULL as well.
317be6f2e86SMatthew Dillon */
31821864bc5SMatthew Dillon return 0;
31921864bc5SMatthew Dillon }
32021864bc5SMatthew Dillon
32121864bc5SMatthew Dillon
32221864bc5SMatthew Dillon static int
devfs_vop_readdir(struct vop_readdir_args * ap)3239f889dc4SMatthew Dillon devfs_vop_readdir(struct vop_readdir_args *ap)
32421864bc5SMatthew Dillon {
325898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_vp);
32621864bc5SMatthew Dillon struct devfs_node *node;
32721864bc5SMatthew Dillon int cookie_index;
32821864bc5SMatthew Dillon int ncookies;
329898c91eeSMatthew Dillon int error2;
330898c91eeSMatthew Dillon int error;
331898c91eeSMatthew Dillon int r;
33221864bc5SMatthew Dillon off_t *cookies;
33321864bc5SMatthew Dillon off_t saveoff;
33421864bc5SMatthew Dillon
33521864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_readdir() called!\n");
33621864bc5SMatthew Dillon
33721864bc5SMatthew Dillon if (ap->a_uio->uio_offset < 0 || ap->a_uio->uio_offset > INT_MAX)
33821864bc5SMatthew Dillon return (EINVAL);
339b458d1abSMatthew Dillon error = vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY | LK_FAILRECLAIM);
340b458d1abSMatthew Dillon if (error)
34121864bc5SMatthew Dillon return (error);
34221864bc5SMatthew Dillon
343c512ab96SMatthew Dillon if (!devfs_node_is_accessible(dnode)) {
344c512ab96SMatthew Dillon vn_unlock(ap->a_vp);
345ca8d7677SMatthew Dillon return ENOENT;
346c512ab96SMatthew Dillon }
347ca8d7677SMatthew Dillon
348ca8d7677SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
349ca8d7677SMatthew Dillon
35021864bc5SMatthew Dillon saveoff = ap->a_uio->uio_offset;
35121864bc5SMatthew Dillon
35221864bc5SMatthew Dillon if (ap->a_ncookies) {
35321864bc5SMatthew Dillon ncookies = ap->a_uio->uio_resid / 16 + 1; /* Why / 16 ?? */
35421864bc5SMatthew Dillon if (ncookies > 256)
35521864bc5SMatthew Dillon ncookies = 256;
35621864bc5SMatthew Dillon cookies = kmalloc(256 * sizeof(off_t), M_TEMP, M_WAITOK);
35721864bc5SMatthew Dillon cookie_index = 0;
35821864bc5SMatthew Dillon } else {
35921864bc5SMatthew Dillon ncookies = -1;
36021864bc5SMatthew Dillon cookies = NULL;
36121864bc5SMatthew Dillon cookie_index = 0;
36221864bc5SMatthew Dillon }
36321864bc5SMatthew Dillon
364d489a79aSMatthew Dillon vfs_timestamp(&dnode->atime);
36521864bc5SMatthew Dillon
36621864bc5SMatthew Dillon if (saveoff == 0) {
367898c91eeSMatthew Dillon r = vop_write_dirent(&error, ap->a_uio, dnode->d_dir.d_ino,
368898c91eeSMatthew Dillon DT_DIR, 1, ".");
36921864bc5SMatthew Dillon if (r)
37021864bc5SMatthew Dillon goto done;
37121864bc5SMatthew Dillon if (cookies)
37221864bc5SMatthew Dillon cookies[cookie_index] = saveoff;
37321864bc5SMatthew Dillon saveoff++;
37421864bc5SMatthew Dillon cookie_index++;
37521864bc5SMatthew Dillon if (cookie_index == ncookies)
37621864bc5SMatthew Dillon goto done;
37721864bc5SMatthew Dillon }
37821864bc5SMatthew Dillon
37921864bc5SMatthew Dillon if (saveoff == 1) {
380898c91eeSMatthew Dillon if (dnode->parent) {
38121864bc5SMatthew Dillon r = vop_write_dirent(&error, ap->a_uio,
382898c91eeSMatthew Dillon dnode->parent->d_dir.d_ino,
38321864bc5SMatthew Dillon DT_DIR, 2, "..");
38421864bc5SMatthew Dillon } else {
38521864bc5SMatthew Dillon r = vop_write_dirent(&error, ap->a_uio,
386898c91eeSMatthew Dillon dnode->d_dir.d_ino,
387898c91eeSMatthew Dillon DT_DIR, 2, "..");
38821864bc5SMatthew Dillon }
38921864bc5SMatthew Dillon if (r)
39021864bc5SMatthew Dillon goto done;
39121864bc5SMatthew Dillon if (cookies)
39221864bc5SMatthew Dillon cookies[cookie_index] = saveoff;
39321864bc5SMatthew Dillon saveoff++;
39421864bc5SMatthew Dillon cookie_index++;
39521864bc5SMatthew Dillon if (cookie_index == ncookies)
39621864bc5SMatthew Dillon goto done;
39721864bc5SMatthew Dillon }
39821864bc5SMatthew Dillon
399898c91eeSMatthew Dillon TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
400898c91eeSMatthew Dillon if ((node->flags & DEVFS_HIDDEN) ||
401898c91eeSMatthew Dillon (node->flags & DEVFS_INVISIBLE)) {
40221864bc5SMatthew Dillon continue;
403898c91eeSMatthew Dillon }
40421864bc5SMatthew Dillon
405f7e8960cSAlex Hornung /*
4069f889dc4SMatthew Dillon * If the node type is a valid devfs alias, then we make
4079f889dc4SMatthew Dillon * sure that the target isn't hidden. If it is, we don't
4089f889dc4SMatthew Dillon * show the link in the directory listing.
409f7e8960cSAlex Hornung */
4108e78a293SSascha Wildner if ((node->node_type == Nlink) && (node->link_target != NULL) &&
411f7e8960cSAlex Hornung (node->link_target->flags & DEVFS_HIDDEN))
412f7e8960cSAlex Hornung continue;
413f7e8960cSAlex Hornung
41421864bc5SMatthew Dillon if (node->cookie < saveoff)
41521864bc5SMatthew Dillon continue;
416f7e8960cSAlex Hornung
41721864bc5SMatthew Dillon saveoff = node->cookie;
41821864bc5SMatthew Dillon
419898c91eeSMatthew Dillon error2 = vop_write_dirent(&error, ap->a_uio, node->d_dir.d_ino,
420898c91eeSMatthew Dillon node->d_dir.d_type,
421898c91eeSMatthew Dillon node->d_dir.d_namlen,
422898c91eeSMatthew Dillon node->d_dir.d_name);
42321864bc5SMatthew Dillon
42421864bc5SMatthew Dillon if (error2)
42521864bc5SMatthew Dillon break;
42621864bc5SMatthew Dillon
42721864bc5SMatthew Dillon saveoff++;
42821864bc5SMatthew Dillon
42921864bc5SMatthew Dillon if (cookies)
43021864bc5SMatthew Dillon cookies[cookie_index] = node->cookie;
43121864bc5SMatthew Dillon ++cookie_index;
43221864bc5SMatthew Dillon if (cookie_index == ncookies)
43321864bc5SMatthew Dillon break;
43421864bc5SMatthew Dillon }
43521864bc5SMatthew Dillon
43621864bc5SMatthew Dillon done:
437ca8d7677SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
43821864bc5SMatthew Dillon vn_unlock(ap->a_vp);
43921864bc5SMatthew Dillon
44021864bc5SMatthew Dillon ap->a_uio->uio_offset = saveoff;
44121864bc5SMatthew Dillon if (error && cookie_index == 0) {
44221864bc5SMatthew Dillon if (cookies) {
44321864bc5SMatthew Dillon kfree(cookies, M_TEMP);
44421864bc5SMatthew Dillon *ap->a_ncookies = 0;
44521864bc5SMatthew Dillon *ap->a_cookies = NULL;
44621864bc5SMatthew Dillon }
44721864bc5SMatthew Dillon } else {
44821864bc5SMatthew Dillon if (cookies) {
44921864bc5SMatthew Dillon *ap->a_ncookies = cookie_index;
45021864bc5SMatthew Dillon *ap->a_cookies = cookies;
45121864bc5SMatthew Dillon }
45221864bc5SMatthew Dillon }
45321864bc5SMatthew Dillon return (error);
45421864bc5SMatthew Dillon }
45521864bc5SMatthew Dillon
45621864bc5SMatthew Dillon
45721864bc5SMatthew Dillon static int
devfs_vop_nresolve(struct vop_nresolve_args * ap)4589f889dc4SMatthew Dillon devfs_vop_nresolve(struct vop_nresolve_args *ap)
45921864bc5SMatthew Dillon {
460898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
46121864bc5SMatthew Dillon struct devfs_node *node, *found = NULL;
46221864bc5SMatthew Dillon struct namecache *ncp;
46321864bc5SMatthew Dillon struct vnode *vp = NULL;
46421864bc5SMatthew Dillon int error = 0;
46521864bc5SMatthew Dillon int len;
466260e4e8bSAlex Hornung int depth;
46721864bc5SMatthew Dillon
46821864bc5SMatthew Dillon ncp = ap->a_nch->ncp;
46921864bc5SMatthew Dillon len = ncp->nc_nlen;
47021864bc5SMatthew Dillon
471898c91eeSMatthew Dillon if (!devfs_node_is_accessible(dnode))
472ca8d7677SMatthew Dillon return ENOENT;
473ca8d7677SMatthew Dillon
47421864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
47521864bc5SMatthew Dillon
4768e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir)) {
477e23485a5SMatthew Dillon error = ENOENT;
47821864bc5SMatthew Dillon cache_setvp(ap->a_nch, NULL);
47921864bc5SMatthew Dillon goto out;
48021864bc5SMatthew Dillon }
48121864bc5SMatthew Dillon
482898c91eeSMatthew Dillon TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
48321864bc5SMatthew Dillon if (len == node->d_dir.d_namlen) {
48421864bc5SMatthew Dillon if (!memcmp(ncp->nc_name, node->d_dir.d_name, len)) {
48521864bc5SMatthew Dillon found = node;
48621864bc5SMatthew Dillon break;
48721864bc5SMatthew Dillon }
48821864bc5SMatthew Dillon }
48921864bc5SMatthew Dillon }
49021864bc5SMatthew Dillon
49121864bc5SMatthew Dillon if (found) {
492260e4e8bSAlex Hornung depth = 0;
4938e78a293SSascha Wildner while ((found->node_type == Nlink) && (found->link_target)) {
494260e4e8bSAlex Hornung if (depth >= 8) {
495260e4e8bSAlex Hornung devfs_debug(DEVFS_DEBUG_SHOW, "Recursive link or depth >= 8");
496260e4e8bSAlex Hornung break;
497260e4e8bSAlex Hornung }
498260e4e8bSAlex Hornung
49921864bc5SMatthew Dillon found = found->link_target;
500260e4e8bSAlex Hornung ++depth;
501260e4e8bSAlex Hornung }
50221864bc5SMatthew Dillon
50321864bc5SMatthew Dillon if (!(found->flags & DEVFS_HIDDEN))
50421864bc5SMatthew Dillon devfs_allocv(/*ap->a_dvp->v_mount, */ &vp, found);
50521864bc5SMatthew Dillon }
50621864bc5SMatthew Dillon
50721864bc5SMatthew Dillon if (vp == NULL) {
50821864bc5SMatthew Dillon error = ENOENT;
50921864bc5SMatthew Dillon cache_setvp(ap->a_nch, NULL);
51021864bc5SMatthew Dillon goto out;
51121864bc5SMatthew Dillon
51221864bc5SMatthew Dillon }
51321864bc5SMatthew Dillon KKASSERT(vp);
51421864bc5SMatthew Dillon vn_unlock(vp);
51521864bc5SMatthew Dillon cache_setvp(ap->a_nch, vp);
51621864bc5SMatthew Dillon vrele(vp);
51721864bc5SMatthew Dillon out:
51821864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
519898c91eeSMatthew Dillon
52021864bc5SMatthew Dillon return error;
52121864bc5SMatthew Dillon }
52221864bc5SMatthew Dillon
52321864bc5SMatthew Dillon
52421864bc5SMatthew Dillon static int
devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args * ap)5259f889dc4SMatthew Dillon devfs_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
52621864bc5SMatthew Dillon {
527898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
52821864bc5SMatthew Dillon
529898c91eeSMatthew Dillon *ap->a_vpp = NULL;
530898c91eeSMatthew Dillon if (!devfs_node_is_accessible(dnode))
531894bbb25SAlex Hornung return ENOENT;
532894bbb25SAlex Hornung
53321864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
534898c91eeSMatthew Dillon if (dnode->parent != NULL) {
535898c91eeSMatthew Dillon devfs_allocv(ap->a_vpp, dnode->parent);
53621864bc5SMatthew Dillon vn_unlock(*ap->a_vpp);
53721864bc5SMatthew Dillon }
53821864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
53921864bc5SMatthew Dillon
54021864bc5SMatthew Dillon return ((*ap->a_vpp == NULL) ? ENOENT : 0);
54121864bc5SMatthew Dillon }
54221864bc5SMatthew Dillon
54321864bc5SMatthew Dillon
544845bd036SMatthew Dillon /*
545845bd036SMatthew Dillon * getattr() - Does not need a lock since the vp is refd
546845bd036SMatthew Dillon */
54721864bc5SMatthew Dillon static int
devfs_vop_getattr(struct vop_getattr_args * ap)5489f889dc4SMatthew Dillon devfs_vop_getattr(struct vop_getattr_args *ap)
54921864bc5SMatthew Dillon {
55021864bc5SMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
551898c91eeSMatthew Dillon struct vattr *vap = ap->a_vap;
5522d076755SAlex Hornung struct partinfo pinfo;
55321864bc5SMatthew Dillon int error = 0;
55421864bc5SMatthew Dillon
555952f0188SAlex Hornung #if 0
556894bbb25SAlex Hornung if (!devfs_node_is_accessible(node))
557ca8d7677SMatthew Dillon return ENOENT;
558952f0188SAlex Hornung #endif
559758d6a9eSPeeter Must
560758d6a9eSPeeter Must /*
561758d6a9eSPeeter Must * XXX This is a temporary hack to prevent crashes when the device is
562758d6a9eSPeeter Must * being destroyed (and so the underlying node will be gone) while
563758d6a9eSPeeter Must * a userland program is blocked in a read().
564758d6a9eSPeeter Must */
565758d6a9eSPeeter Must if (node == NULL)
566758d6a9eSPeeter Must return EIO;
567758d6a9eSPeeter Must
5684062d050SMatthew Dillon node_sync_dev_get(node);
569ca8d7677SMatthew Dillon
57021864bc5SMatthew Dillon /* start by zeroing out the attributes */
57121864bc5SMatthew Dillon VATTR_NULL(vap);
57221864bc5SMatthew Dillon
57321864bc5SMatthew Dillon /* next do all the common fields */
57421864bc5SMatthew Dillon vap->va_type = ap->a_vp->v_type;
57521864bc5SMatthew Dillon vap->va_mode = node->mode;
57621864bc5SMatthew Dillon vap->va_fileid = DEVFS_NODE(ap->a_vp)->d_dir.d_ino ;
577a3c7ebb9SMatthew Dillon vap->va_flags = 0;
57821864bc5SMatthew Dillon vap->va_blocksize = DEV_BSIZE;
579a3c7ebb9SMatthew Dillon vap->va_bytes = vap->va_size = 0;
58021864bc5SMatthew Dillon
58121864bc5SMatthew Dillon vap->va_fsid = ap->a_vp->v_mount->mnt_stat.f_fsid.val[0];
58221864bc5SMatthew Dillon
58321864bc5SMatthew Dillon vap->va_atime = node->atime;
58421864bc5SMatthew Dillon vap->va_mtime = node->mtime;
58521864bc5SMatthew Dillon vap->va_ctime = node->ctime;
58621864bc5SMatthew Dillon
58721864bc5SMatthew Dillon vap->va_nlink = 1; /* number of references to file */
58821864bc5SMatthew Dillon
58921864bc5SMatthew Dillon vap->va_uid = node->uid;
59021864bc5SMatthew Dillon vap->va_gid = node->gid;
59121864bc5SMatthew Dillon
59221864bc5SMatthew Dillon vap->va_rmajor = 0;
59321864bc5SMatthew Dillon vap->va_rminor = 0;
59421864bc5SMatthew Dillon
5958e78a293SSascha Wildner if ((node->node_type == Ndev) && node->d_dev) {
596898c91eeSMatthew Dillon reference_dev(node->d_dev);
597898c91eeSMatthew Dillon vap->va_rminor = node->d_dev->si_uminor;
598898c91eeSMatthew Dillon release_dev(node->d_dev);
59921864bc5SMatthew Dillon }
60021864bc5SMatthew Dillon
60121864bc5SMatthew Dillon /* For a softlink the va_size is the length of the softlink */
602898c91eeSMatthew Dillon if (node->symlink_name != 0) {
6032d076755SAlex Hornung vap->va_bytes = vap->va_size = node->symlink_namelen;
60421864bc5SMatthew Dillon }
6052d076755SAlex Hornung
6062d076755SAlex Hornung /*
6072d076755SAlex Hornung * For a disk-type device, va_size is the size of the underlying
6082d076755SAlex Hornung * device, so that lseek() works properly.
6092d076755SAlex Hornung */
6102d076755SAlex Hornung if ((node->d_dev) && (dev_dflags(node->d_dev) & D_DISK)) {
6112d076755SAlex Hornung bzero(&pinfo, sizeof(pinfo));
6122d076755SAlex Hornung error = dev_dioctl(node->d_dev, DIOCGPART, (void *)&pinfo,
6138c530b23SJohannes Hofmann 0, proc0.p_ucred, NULL, NULL);
6142d076755SAlex Hornung if ((error == 0) && (pinfo.media_blksize != 0)) {
6152d076755SAlex Hornung vap->va_size = pinfo.media_size;
6162d076755SAlex Hornung } else {
6172d076755SAlex Hornung vap->va_size = 0;
6182d076755SAlex Hornung error = 0;
6192d076755SAlex Hornung }
6202d076755SAlex Hornung }
6212d076755SAlex Hornung
622894bbb25SAlex Hornung return (error);
62321864bc5SMatthew Dillon }
62421864bc5SMatthew Dillon
62521864bc5SMatthew Dillon static int
devfs_vop_setattr(struct vop_setattr_args * ap)6269f889dc4SMatthew Dillon devfs_vop_setattr(struct vop_setattr_args *ap)
62721864bc5SMatthew Dillon {
628898c91eeSMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
62921864bc5SMatthew Dillon struct vattr *vap;
630dffaed1bSAlex Hornung uid_t cur_uid;
631dffaed1bSAlex Hornung gid_t cur_gid;
632dffaed1bSAlex Hornung mode_t cur_mode;
63321864bc5SMatthew Dillon int error = 0;
63421864bc5SMatthew Dillon
635894bbb25SAlex Hornung if (!devfs_node_is_accessible(node))
636ca8d7677SMatthew Dillon return ENOENT;
6374062d050SMatthew Dillon node_sync_dev_get(node);
63821864bc5SMatthew Dillon
63921864bc5SMatthew Dillon vap = ap->a_vap;
64021864bc5SMatthew Dillon
641dffaed1bSAlex Hornung if ((vap->va_uid != (uid_t)VNOVAL) || (vap->va_gid != (gid_t)VNOVAL)) {
642dffaed1bSAlex Hornung cur_uid = node->uid;
643dffaed1bSAlex Hornung cur_gid = node->gid;
644dffaed1bSAlex Hornung cur_mode = node->mode;
645dffaed1bSAlex Hornung error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
646dffaed1bSAlex Hornung ap->a_cred, &cur_uid, &cur_gid, &cur_mode);
647898c91eeSMatthew Dillon if (error)
64821864bc5SMatthew Dillon goto out;
64921864bc5SMatthew Dillon
650dffaed1bSAlex Hornung if (node->uid != cur_uid || node->gid != cur_gid) {
651dffaed1bSAlex Hornung node->uid = cur_uid;
652dffaed1bSAlex Hornung node->gid = cur_gid;
653dffaed1bSAlex Hornung node->mode = cur_mode;
65421864bc5SMatthew Dillon }
65521864bc5SMatthew Dillon }
65621864bc5SMatthew Dillon
65721864bc5SMatthew Dillon if (vap->va_mode != (mode_t)VNOVAL) {
658dffaed1bSAlex Hornung cur_mode = node->mode;
659dffaed1bSAlex Hornung error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
660dffaed1bSAlex Hornung node->uid, node->gid, &cur_mode);
661dffaed1bSAlex Hornung if (error == 0 && node->mode != cur_mode) {
662dffaed1bSAlex Hornung node->mode = cur_mode;
66321864bc5SMatthew Dillon }
66421864bc5SMatthew Dillon }
66521864bc5SMatthew Dillon
66621864bc5SMatthew Dillon out:
6674062d050SMatthew Dillon node_sync_dev_set(node);
668d489a79aSMatthew Dillon vfs_timestamp(&node->ctime);
669898c91eeSMatthew Dillon
67021864bc5SMatthew Dillon return error;
67121864bc5SMatthew Dillon }
67221864bc5SMatthew Dillon
67321864bc5SMatthew Dillon
67421864bc5SMatthew Dillon static int
devfs_vop_readlink(struct vop_readlink_args * ap)6759f889dc4SMatthew Dillon devfs_vop_readlink(struct vop_readlink_args *ap)
67621864bc5SMatthew Dillon {
67721864bc5SMatthew Dillon struct devfs_node *node = DEVFS_NODE(ap->a_vp);
678ca8d7677SMatthew Dillon int ret;
679ca8d7677SMatthew Dillon
680894bbb25SAlex Hornung if (!devfs_node_is_accessible(node))
681ca8d7677SMatthew Dillon return ENOENT;
68221864bc5SMatthew Dillon
683845bd036SMatthew Dillon lockmgr(&devfs_lock, LK_SHARED);
684ca8d7677SMatthew Dillon ret = uiomove(node->symlink_name, node->symlink_namelen, ap->a_uio);
685ca8d7677SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
686ca8d7677SMatthew Dillon
687ca8d7677SMatthew Dillon return ret;
68821864bc5SMatthew Dillon }
68921864bc5SMatthew Dillon
69021864bc5SMatthew Dillon
69121864bc5SMatthew Dillon static int
devfs_vop_print(struct vop_print_args * ap)6929f889dc4SMatthew Dillon devfs_vop_print(struct vop_print_args *ap)
69321864bc5SMatthew Dillon {
69421864bc5SMatthew Dillon return (0);
69521864bc5SMatthew Dillon }
69621864bc5SMatthew Dillon
69739a08947SAlex Hornung static int
devfs_vop_nmkdir(struct vop_nmkdir_args * ap)6989f889dc4SMatthew Dillon devfs_vop_nmkdir(struct vop_nmkdir_args *ap)
69939a08947SAlex Hornung {
70039a08947SAlex Hornung struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
70139a08947SAlex Hornung struct devfs_node *node;
70239a08947SAlex Hornung
70339a08947SAlex Hornung if (!devfs_node_is_accessible(dnode))
70439a08947SAlex Hornung return ENOENT;
70539a08947SAlex Hornung
7068e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
70739a08947SAlex Hornung goto out;
70839a08947SAlex Hornung
70939a08947SAlex Hornung lockmgr(&devfs_lock, LK_EXCLUSIVE);
7108e78a293SSascha Wildner devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Ndir,
71139a08947SAlex Hornung ap->a_nch->ncp->nc_name, dnode, NULL);
71239a08947SAlex Hornung
71339a08947SAlex Hornung if (*ap->a_vpp) {
71439a08947SAlex Hornung node = DEVFS_NODE(*ap->a_vpp);
71539a08947SAlex Hornung node->flags |= DEVFS_USER_CREATED;
71639a08947SAlex Hornung cache_setunresolved(ap->a_nch);
71739a08947SAlex Hornung cache_setvp(ap->a_nch, *ap->a_vpp);
71839a08947SAlex Hornung }
71939a08947SAlex Hornung lockmgr(&devfs_lock, LK_RELEASE);
72039a08947SAlex Hornung out:
72139a08947SAlex Hornung return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
72239a08947SAlex Hornung }
72321864bc5SMatthew Dillon
72421864bc5SMatthew Dillon static int
devfs_vop_nsymlink(struct vop_nsymlink_args * ap)7259f889dc4SMatthew Dillon devfs_vop_nsymlink(struct vop_nsymlink_args *ap)
72621864bc5SMatthew Dillon {
727898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
728898c91eeSMatthew Dillon struct devfs_node *node;
729898c91eeSMatthew Dillon size_t targetlen;
73021864bc5SMatthew Dillon
731898c91eeSMatthew Dillon if (!devfs_node_is_accessible(dnode))
732ca8d7677SMatthew Dillon return ENOENT;
733ca8d7677SMatthew Dillon
734894bbb25SAlex Hornung ap->a_vap->va_type = VLNK;
735894bbb25SAlex Hornung
7368e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
73721864bc5SMatthew Dillon goto out;
738898c91eeSMatthew Dillon
73921864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
7408e78a293SSascha Wildner devfs_allocvp(ap->a_dvp->v_mount, ap->a_vpp, Nlink,
741898c91eeSMatthew Dillon ap->a_nch->ncp->nc_name, dnode, NULL);
74221864bc5SMatthew Dillon
743898c91eeSMatthew Dillon targetlen = strlen(ap->a_target);
74421864bc5SMatthew Dillon if (*ap->a_vpp) {
745898c91eeSMatthew Dillon node = DEVFS_NODE(*ap->a_vpp);
746898c91eeSMatthew Dillon node->flags |= DEVFS_USER_CREATED;
747898c91eeSMatthew Dillon node->symlink_namelen = targetlen;
748898c91eeSMatthew Dillon node->symlink_name = kmalloc(targetlen + 1, M_DEVFS, M_WAITOK);
749898c91eeSMatthew Dillon memcpy(node->symlink_name, ap->a_target, targetlen);
750898c91eeSMatthew Dillon node->symlink_name[targetlen] = '\0';
75121864bc5SMatthew Dillon cache_setunresolved(ap->a_nch);
75221864bc5SMatthew Dillon cache_setvp(ap->a_nch, *ap->a_vpp);
75321864bc5SMatthew Dillon }
75421864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
75521864bc5SMatthew Dillon out:
75621864bc5SMatthew Dillon return ((*ap->a_vpp == NULL) ? ENOTDIR : 0);
75721864bc5SMatthew Dillon }
75821864bc5SMatthew Dillon
75939a08947SAlex Hornung static int
devfs_vop_nrmdir(struct vop_nrmdir_args * ap)7609f889dc4SMatthew Dillon devfs_vop_nrmdir(struct vop_nrmdir_args *ap)
76139a08947SAlex Hornung {
76239a08947SAlex Hornung struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
76339a08947SAlex Hornung struct devfs_node *node;
76439a08947SAlex Hornung struct namecache *ncp;
76539a08947SAlex Hornung int error = ENOENT;
76639a08947SAlex Hornung
76739a08947SAlex Hornung ncp = ap->a_nch->ncp;
76839a08947SAlex Hornung
76939a08947SAlex Hornung if (!devfs_node_is_accessible(dnode))
77039a08947SAlex Hornung return ENOENT;
77139a08947SAlex Hornung
77239a08947SAlex Hornung lockmgr(&devfs_lock, LK_EXCLUSIVE);
77339a08947SAlex Hornung
7748e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
77539a08947SAlex Hornung goto out;
77639a08947SAlex Hornung
77739a08947SAlex Hornung TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
77839a08947SAlex Hornung if (ncp->nc_nlen != node->d_dir.d_namlen)
77939a08947SAlex Hornung continue;
78039a08947SAlex Hornung if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
78139a08947SAlex Hornung continue;
78239a08947SAlex Hornung
78339a08947SAlex Hornung /*
78439a08947SAlex Hornung * only allow removal of user created dirs
78539a08947SAlex Hornung */
78639a08947SAlex Hornung if ((node->flags & DEVFS_USER_CREATED) == 0) {
78739a08947SAlex Hornung error = EPERM;
78839a08947SAlex Hornung goto out;
7898e78a293SSascha Wildner } else if (node->node_type != Ndir) {
79039a08947SAlex Hornung error = ENOTDIR;
79139a08947SAlex Hornung goto out;
79239a08947SAlex Hornung } else if (node->nchildren > 2) {
79339a08947SAlex Hornung error = ENOTEMPTY;
79439a08947SAlex Hornung goto out;
79539a08947SAlex Hornung } else {
79639a08947SAlex Hornung if (node->v_node)
79739a08947SAlex Hornung cache_inval_vp(node->v_node, CINV_DESTROY);
79839a08947SAlex Hornung devfs_unlinkp(node);
79939a08947SAlex Hornung error = 0;
80039a08947SAlex Hornung break;
80139a08947SAlex Hornung }
80239a08947SAlex Hornung }
80339a08947SAlex Hornung
8045323ed62SMatthew Dillon cache_unlink(ap->a_nch);
80539a08947SAlex Hornung out:
80639a08947SAlex Hornung lockmgr(&devfs_lock, LK_RELEASE);
80739a08947SAlex Hornung return error;
80839a08947SAlex Hornung }
80921864bc5SMatthew Dillon
81021864bc5SMatthew Dillon static int
devfs_vop_nremove(struct vop_nremove_args * ap)8119f889dc4SMatthew Dillon devfs_vop_nremove(struct vop_nremove_args *ap)
81221864bc5SMatthew Dillon {
813898c91eeSMatthew Dillon struct devfs_node *dnode = DEVFS_NODE(ap->a_dvp);
81421864bc5SMatthew Dillon struct devfs_node *node;
81521864bc5SMatthew Dillon struct namecache *ncp;
81621864bc5SMatthew Dillon int error = ENOENT;
81721864bc5SMatthew Dillon
81821864bc5SMatthew Dillon ncp = ap->a_nch->ncp;
81921864bc5SMatthew Dillon
820898c91eeSMatthew Dillon if (!devfs_node_is_accessible(dnode))
821ca8d7677SMatthew Dillon return ENOENT;
822ca8d7677SMatthew Dillon
82321864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
82421864bc5SMatthew Dillon
8258e78a293SSascha Wildner if ((dnode->node_type != Nroot) && (dnode->node_type != Ndir))
82621864bc5SMatthew Dillon goto out;
82721864bc5SMatthew Dillon
828898c91eeSMatthew Dillon TAILQ_FOREACH(node, DEVFS_DENODE_HEAD(dnode), link) {
829898c91eeSMatthew Dillon if (ncp->nc_nlen != node->d_dir.d_namlen)
830898c91eeSMatthew Dillon continue;
831898c91eeSMatthew Dillon if (memcmp(ncp->nc_name, node->d_dir.d_name, ncp->nc_nlen))
832898c91eeSMatthew Dillon continue;
833898c91eeSMatthew Dillon
834898c91eeSMatthew Dillon /*
835898c91eeSMatthew Dillon * only allow removal of user created stuff (e.g. symlinks)
836898c91eeSMatthew Dillon */
83721864bc5SMatthew Dillon if ((node->flags & DEVFS_USER_CREATED) == 0) {
83821864bc5SMatthew Dillon error = EPERM;
83921864bc5SMatthew Dillon goto out;
8408e78a293SSascha Wildner } else if (node->node_type == Ndir) {
84139a08947SAlex Hornung error = EISDIR;
84239a08947SAlex Hornung goto out;
84321864bc5SMatthew Dillon } else {
84421864bc5SMatthew Dillon if (node->v_node)
84521864bc5SMatthew Dillon cache_inval_vp(node->v_node, CINV_DESTROY);
84621864bc5SMatthew Dillon devfs_unlinkp(node);
84721864bc5SMatthew Dillon error = 0;
84821864bc5SMatthew Dillon break;
84921864bc5SMatthew Dillon }
85021864bc5SMatthew Dillon }
85121864bc5SMatthew Dillon
8525323ed62SMatthew Dillon cache_unlink(ap->a_nch);
85321864bc5SMatthew Dillon out:
85421864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
85521864bc5SMatthew Dillon return error;
85621864bc5SMatthew Dillon }
85721864bc5SMatthew Dillon
85821864bc5SMatthew Dillon
85921864bc5SMatthew Dillon static int
devfs_spec_open(struct vop_open_args * ap)86021864bc5SMatthew Dillon devfs_spec_open(struct vop_open_args *ap)
86121864bc5SMatthew Dillon {
86221864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
863ca8d7677SMatthew Dillon struct vnode *orig_vp = NULL;
864898c91eeSMatthew Dillon struct devfs_node *node = DEVFS_NODE(vp);
865898c91eeSMatthew Dillon struct devfs_node *newnode;
86621864bc5SMatthew Dillon cdev_t dev, ndev = NULL;
86721864bc5SMatthew Dillon int error = 0;
86821864bc5SMatthew Dillon
869898c91eeSMatthew Dillon if (node) {
870898c91eeSMatthew Dillon if (node->d_dev == NULL)
87121864bc5SMatthew Dillon return ENXIO;
872898c91eeSMatthew Dillon if (!devfs_node_is_accessible(node))
873894bbb25SAlex Hornung return ENOENT;
87421864bc5SMatthew Dillon }
87521864bc5SMatthew Dillon
87621864bc5SMatthew Dillon if ((dev = vp->v_rdev) == NULL)
87721864bc5SMatthew Dillon return ENXIO;
87821864bc5SMatthew Dillon
879845bd036SMatthew Dillon /*
880845bd036SMatthew Dillon * Simple devices that don't care. Retain the shared lock.
881845bd036SMatthew Dillon */
882845bd036SMatthew Dillon if (dev_dflags(dev) & D_QUICK) {
883845bd036SMatthew Dillon vn_unlock(vp);
884845bd036SMatthew Dillon error = dev_dopen(dev, ap->a_mode, S_IFCHR,
8855bd45597SMatthew Dillon ap->a_cred, ap->a_fpp, vp);
886845bd036SMatthew Dillon vn_lock(vp, LK_SHARED | LK_RETRY);
8879d388b3bSMatthew Dillon if (error)
8889d388b3bSMatthew Dillon return error;
889845bd036SMatthew Dillon vop_stdopen(ap);
890845bd036SMatthew Dillon goto skip;
891845bd036SMatthew Dillon }
89212cdc371SMatthew Dillon
893845bd036SMatthew Dillon /*
894845bd036SMatthew Dillon * Slow code
895845bd036SMatthew Dillon */
896845bd036SMatthew Dillon vn_lock(vp, LK_UPGRADE | LK_RETRY);
8975bd45597SMatthew Dillon if (node && ap->a_fpp) {
8982c94b9eeSMatthew Dillon int exists;
8992c94b9eeSMatthew Dillon
90021864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_open: -1.1-\n");
901948d6431SMatthew Dillon lockmgr(&devfs_lock, LK_SHARED);
90207dfa375SAlex Hornung
90312cdc371SMatthew Dillon ndev = devfs_clone(dev, node->d_dir.d_name,
90412cdc371SMatthew Dillon node->d_dir.d_namlen,
90507dfa375SAlex Hornung ap->a_mode, ap->a_cred);
90607dfa375SAlex Hornung if (ndev != NULL) {
907948d6431SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
908948d6431SMatthew Dillon lockmgr(&devfs_lock, LK_EXCLUSIVE);
909898c91eeSMatthew Dillon newnode = devfs_create_device_node(
910898c91eeSMatthew Dillon DEVFS_MNTDATA(vp->v_mount)->root_node,
9112c94b9eeSMatthew Dillon ndev, &exists, NULL, NULL);
91207dfa375SAlex Hornung /* XXX: possibly destroy device if this happens */
91307dfa375SAlex Hornung
91407dfa375SAlex Hornung if (newnode != NULL) {
91507dfa375SAlex Hornung dev = ndev;
9162c94b9eeSMatthew Dillon if (exists == 0)
91707dfa375SAlex Hornung devfs_link_dev(dev);
91821864bc5SMatthew Dillon
919898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
920898c91eeSMatthew Dillon "parent here is: %s, node is: |%s|\n",
9218e78a293SSascha Wildner ((node->parent->node_type == Nroot) ?
922898c91eeSMatthew Dillon "ROOT!" : node->parent->d_dir.d_name),
923898c91eeSMatthew Dillon newnode->d_dir.d_name);
924898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
925898c91eeSMatthew Dillon "test: %s\n",
926898c91eeSMatthew Dillon ((struct devfs_node *)(TAILQ_LAST(DEVFS_DENODE_HEAD(node->parent), devfs_node_head)))->d_dir.d_name);
92721864bc5SMatthew Dillon
928ca8d7677SMatthew Dillon /*
9292c94b9eeSMatthew Dillon * orig_vp is set to the original vp if we
9302c94b9eeSMatthew Dillon * cloned.
931ca8d7677SMatthew Dillon */
932ca8d7677SMatthew Dillon /* node->flags |= DEVFS_CLONED; */
933898c91eeSMatthew Dillon devfs_allocv(&vp, newnode);
934ca8d7677SMatthew Dillon orig_vp = ap->a_vp;
93521864bc5SMatthew Dillon ap->a_vp = vp;
93621864bc5SMatthew Dillon }
93707dfa375SAlex Hornung }
93821864bc5SMatthew Dillon lockmgr(&devfs_lock, LK_RELEASE);
939845bd036SMatthew Dillon
94018baefa4SAlex Hornung /*
9412c94b9eeSMatthew Dillon * Synchronize devfs here to make sure that, if the cloned
9422c94b9eeSMatthew Dillon * device creates other device nodes in addition to the
9432c94b9eeSMatthew Dillon * cloned one, all of them are created by the time we return
9442c94b9eeSMatthew Dillon * from opening the cloned one.
94518baefa4SAlex Hornung */
94618baefa4SAlex Hornung if (ndev)
94718baefa4SAlex Hornung devfs_config();
94821864bc5SMatthew Dillon }
94921864bc5SMatthew Dillon
950898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
951898c91eeSMatthew Dillon "devfs_spec_open() called on %s! \n",
952898c91eeSMatthew Dillon dev->si_name);
953898c91eeSMatthew Dillon
95421864bc5SMatthew Dillon /*
95521864bc5SMatthew Dillon * Make this field valid before any I/O in ->d_open
956845bd036SMatthew Dillon *
957845bd036SMatthew Dillon * NOTE: Shared vnode lock probably held, but its ok as long
958845bd036SMatthew Dillon * as assignments are consistent.
95921864bc5SMatthew Dillon */
96021864bc5SMatthew Dillon if (!dev->si_iosize_max)
96195df18e4SFrançois Tigeot /* XXX: old DFLTPHYS == 64KB dependency */
96295df18e4SFrançois Tigeot dev->si_iosize_max = min(MAXPHYS,64*1024);
96321864bc5SMatthew Dillon
96421864bc5SMatthew Dillon if (dev_dflags(dev) & D_TTY)
9652247fe02SMatthew Dillon vsetflags(vp, VISTTY);
96621864bc5SMatthew Dillon
9679f889dc4SMatthew Dillon /*
9685bd45597SMatthew Dillon * Open the underlying device.
9695bd45597SMatthew Dillon *
9705bd45597SMatthew Dillon * NOTE: If the dev open returns EALREADY it has completed the open
9715bd45597SMatthew Dillon * operation and is returning a fully initialized *a->a_fpp
9725bd45597SMatthew Dillon * (which it may also have replaced). This includes issuing
9735bd45597SMatthew Dillon * any necessary VOP_OPEN().
9745bd45597SMatthew Dillon *
9755bd45597SMatthew Dillon * Also, the returned ap->a_fpp might not be DTYPE_VNODE and
9765bd45597SMatthew Dillon * if it is might not be using the vp we supplied to it.
9779f889dc4SMatthew Dillon */
97821864bc5SMatthew Dillon vn_unlock(vp);
9795bd45597SMatthew Dillon error = dev_dopen(dev, ap->a_mode, S_IFCHR,
9805bd45597SMatthew Dillon ap->a_cred, ap->a_fpp, vp);
98121864bc5SMatthew Dillon vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
98221864bc5SMatthew Dillon
9835bd45597SMatthew Dillon if (__predict_false(error == EALREADY)) {
9845bd45597SMatthew Dillon if (orig_vp)
9855bd45597SMatthew Dillon vput(vp);
9865bd45597SMatthew Dillon return 0;
9875bd45597SMatthew Dillon }
9885bd45597SMatthew Dillon
989ca8d7677SMatthew Dillon /*
990ca8d7677SMatthew Dillon * Clean up any cloned vp if we error out.
991ca8d7677SMatthew Dillon */
9925bd45597SMatthew Dillon if (__predict_false(error != 0)) {
993ca8d7677SMatthew Dillon if (orig_vp) {
99421864bc5SMatthew Dillon vput(vp);
995ca8d7677SMatthew Dillon ap->a_vp = orig_vp;
996ca8d7677SMatthew Dillon /* orig_vp = NULL; */
997ca8d7677SMatthew Dillon }
99821864bc5SMatthew Dillon return error;
99921864bc5SMatthew Dillon }
100021864bc5SMatthew Dillon
1001d894b0ebSAntonio Huete /*
1002ddd7de82SAntonio Huete * This checks if the disk device is going to be opened for writing.
1003ddd7de82SAntonio Huete * It will be only allowed in the cases where securelevel permits it
1004ddd7de82SAntonio Huete * and it's not mounted R/W.
1005d894b0ebSAntonio Huete */
1006ddd7de82SAntonio Huete if ((dev_dflags(dev) & D_DISK) && (ap->a_mode & FWRITE) &&
1007d894b0ebSAntonio Huete (ap->a_cred != FSCRED)) {
1008ddd7de82SAntonio Huete
1009ddd7de82SAntonio Huete /* Very secure mode. No open for writing allowed */
1010d894b0ebSAntonio Huete if (securelevel >= 2)
1011d894b0ebSAntonio Huete return EPERM;
1012ddd7de82SAntonio Huete
1013ddd7de82SAntonio Huete /*
1014ddd7de82SAntonio Huete * If it is mounted R/W, do not allow to open for writing.
1015ddd7de82SAntonio Huete * In the case it's mounted read-only but securelevel
1016ddd7de82SAntonio Huete * is >= 1, then do not allow opening for writing either.
1017ddd7de82SAntonio Huete */
1018ddd7de82SAntonio Huete if (vfs_mountedon(vp)) {
1019ddd7de82SAntonio Huete if (!(dev->si_mountpoint->mnt_flag & MNT_RDONLY))
1020ddd7de82SAntonio Huete return EBUSY;
1021ddd7de82SAntonio Huete else if (securelevel >= 1)
1022ddd7de82SAntonio Huete return EPERM;
1023d894b0ebSAntonio Huete }
1024d894b0ebSAntonio Huete }
102521864bc5SMatthew Dillon
1026845bd036SMatthew Dillon /*
1027845bd036SMatthew Dillon * NOTE: vnode is still locked shared. t_stop assignment should
1028845bd036SMatthew Dillon * remain consistent so we should be ok.
1029845bd036SMatthew Dillon */
103021864bc5SMatthew Dillon if (dev_dflags(dev) & D_TTY) {
103121864bc5SMatthew Dillon if (dev->si_tty) {
103221864bc5SMatthew Dillon struct tty *tp;
103321864bc5SMatthew Dillon tp = dev->si_tty;
103421864bc5SMatthew Dillon if (!tp->t_stop) {
1035898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1036898c91eeSMatthew Dillon "devfs: no t_stop\n");
103721864bc5SMatthew Dillon tp->t_stop = nottystop;
103821864bc5SMatthew Dillon }
103921864bc5SMatthew Dillon }
104021864bc5SMatthew Dillon }
104121864bc5SMatthew Dillon
1042845bd036SMatthew Dillon /*
1043845bd036SMatthew Dillon * NOTE: vnode is still locked shared. assignments should
1044845bd036SMatthew Dillon * remain consistent so we should be ok. However,
1045845bd036SMatthew Dillon * upgrade to exclusive if we need a VM object.
1046845bd036SMatthew Dillon */
104721864bc5SMatthew Dillon if (vn_isdisk(vp, NULL)) {
104821864bc5SMatthew Dillon if (!dev->si_bsize_phys)
104921864bc5SMatthew Dillon dev->si_bsize_phys = DEV_BSIZE;
1050b0d18f7dSMatthew Dillon vinitvmio(vp, IDX_TO_OFF(INT_MAX), PAGE_SIZE, -1);
105121864bc5SMatthew Dillon }
105221864bc5SMatthew Dillon
105321864bc5SMatthew Dillon vop_stdopen(ap);
105407dfa375SAlex Hornung #if 0
1055898c91eeSMatthew Dillon if (node)
1056d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
105707dfa375SAlex Hornung #endif
1058b80b4c32SMatthew Dillon /*
1059b80b4c32SMatthew Dillon * If we replaced the vp the vop_stdopen() call will have loaded
1060b80b4c32SMatthew Dillon * it into fp->f_data and vref()d the vp, giving us two refs. So
1061b80b4c32SMatthew Dillon * instead of just unlocking it here we have to vput() it.
1062b80b4c32SMatthew Dillon */
1063ca8d7677SMatthew Dillon if (orig_vp)
1064b80b4c32SMatthew Dillon vput(vp);
106521864bc5SMatthew Dillon
106621864bc5SMatthew Dillon /* Ugly pty magic, to make pty devices appear once they are opened */
1067845bd036SMatthew Dillon if (node && (node->flags & DEVFS_PTY) == DEVFS_PTY) {
1068845bd036SMatthew Dillon if (node->flags & DEVFS_INVISIBLE)
1069898c91eeSMatthew Dillon node->flags &= ~DEVFS_INVISIBLE;
1070845bd036SMatthew Dillon }
107121864bc5SMatthew Dillon
1072845bd036SMatthew Dillon skip:
10735bd45597SMatthew Dillon if (ap->a_fpp) {
10745bd45597SMatthew Dillon struct file *fp = *ap->a_fpp;
10755bd45597SMatthew Dillon
10765bd45597SMatthew Dillon KKASSERT(fp->f_type == DTYPE_VNODE);
10775bd45597SMatthew Dillon KKASSERT((fp->f_flag & FMASK) == (ap->a_mode & FMASK));
10785bd45597SMatthew Dillon fp->f_ops = &devfs_dev_fileops;
10795bd45597SMatthew Dillon KKASSERT(fp->f_data == (void *)vp);
108021864bc5SMatthew Dillon }
108121864bc5SMatthew Dillon
108221864bc5SMatthew Dillon return 0;
108321864bc5SMatthew Dillon }
108421864bc5SMatthew Dillon
108521864bc5SMatthew Dillon static int
devfs_spec_close(struct vop_close_args * ap)108621864bc5SMatthew Dillon devfs_spec_close(struct vop_close_args *ap)
108721864bc5SMatthew Dillon {
108892fb0c6aSMatthew Dillon struct devfs_node *node;
108921864bc5SMatthew Dillon struct proc *p = curproc;
109021864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
109121864bc5SMatthew Dillon cdev_t dev = vp->v_rdev;
109221864bc5SMatthew Dillon int error = 0;
109321864bc5SMatthew Dillon int needrelock;
109497248700SMatthew Dillon int opencount;
109521864bc5SMatthew Dillon
109612cdc371SMatthew Dillon /*
1097845bd036SMatthew Dillon * Devices flagged D_QUICK require no special handling.
1098845bd036SMatthew Dillon */
1099845bd036SMatthew Dillon if (dev && dev_dflags(dev) & D_QUICK) {
1100845bd036SMatthew Dillon opencount = vp->v_opencount;
1101845bd036SMatthew Dillon if (opencount <= 1)
1102845bd036SMatthew Dillon opencount = count_dev(dev); /* XXX NOT SMP SAFE */
1103845bd036SMatthew Dillon if (((vp->v_flag & VRECLAIMED) ||
1104845bd036SMatthew Dillon (dev_dflags(dev) & D_TRACKCLOSE) ||
1105845bd036SMatthew Dillon (opencount == 1))) {
1106845bd036SMatthew Dillon vn_unlock(vp);
1107845bd036SMatthew Dillon error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
1108845bd036SMatthew Dillon vn_lock(vp, LK_SHARED | LK_RETRY);
1109845bd036SMatthew Dillon }
1110845bd036SMatthew Dillon goto skip;
1111845bd036SMatthew Dillon }
1112845bd036SMatthew Dillon
1113845bd036SMatthew Dillon /*
111412cdc371SMatthew Dillon * We do special tests on the opencount so unfortunately we need
111512cdc371SMatthew Dillon * an exclusive lock.
111612cdc371SMatthew Dillon */
111712cdc371SMatthew Dillon vn_lock(vp, LK_UPGRADE | LK_RETRY);
111812cdc371SMatthew Dillon
111952f98df9SSamuel J. Greear if (dev)
1120898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1121898c91eeSMatthew Dillon "devfs_spec_close() called on %s! \n",
1122898c91eeSMatthew Dillon dev->si_name);
112352f98df9SSamuel J. Greear else
112452f98df9SSamuel J. Greear devfs_debug(DEVFS_DEBUG_DEBUG,
112552f98df9SSamuel J. Greear "devfs_spec_close() called, null vode!\n");
112621864bc5SMatthew Dillon
112721864bc5SMatthew Dillon /*
112821864bc5SMatthew Dillon * A couple of hacks for devices and tty devices. The
112921864bc5SMatthew Dillon * vnode ref count cannot be used to figure out the
113021864bc5SMatthew Dillon * last close, but we can use v_opencount now that
113121864bc5SMatthew Dillon * revoke works properly.
113221864bc5SMatthew Dillon *
113321864bc5SMatthew Dillon * Detect the last close on a controlling terminal and clear
113421864bc5SMatthew Dillon * the session (half-close).
113597248700SMatthew Dillon *
113697248700SMatthew Dillon * XXX opencount is not SMP safe. The vnode is locked but there
113797248700SMatthew Dillon * may be multiple vnodes referencing the same device.
113821864bc5SMatthew Dillon */
113997248700SMatthew Dillon if (dev) {
114097248700SMatthew Dillon /*
114197248700SMatthew Dillon * NOTE: Try to avoid global tokens when testing opencount
114297248700SMatthew Dillon * XXX hack, fixme. needs a struct lock and opencount in
114397248700SMatthew Dillon * struct cdev itself.
114497248700SMatthew Dillon */
114521864bc5SMatthew Dillon reference_dev(dev);
114697248700SMatthew Dillon opencount = vp->v_opencount;
114797248700SMatthew Dillon if (opencount <= 1)
114897248700SMatthew Dillon opencount = count_dev(dev); /* XXX NOT SMP SAFE */
114997248700SMatthew Dillon } else {
115097248700SMatthew Dillon opencount = 0;
115197248700SMatthew Dillon }
115221864bc5SMatthew Dillon
115321864bc5SMatthew Dillon if (p && vp->v_opencount <= 1 && vp == p->p_session->s_ttyvp) {
115421864bc5SMatthew Dillon p->p_session->s_ttyvp = NULL;
115521864bc5SMatthew Dillon vrele(vp);
115621864bc5SMatthew Dillon }
115721864bc5SMatthew Dillon
115821864bc5SMatthew Dillon /*
115921864bc5SMatthew Dillon * Vnodes can be opened and closed multiple times. Do not really
116021864bc5SMatthew Dillon * close the device unless (1) it is being closed forcibly,
116121864bc5SMatthew Dillon * (2) the device wants to track closes, or (3) this is the last
116221864bc5SMatthew Dillon * vnode doing its last close on the device.
116321864bc5SMatthew Dillon *
116421864bc5SMatthew Dillon * XXX the VXLOCK (force close) case can leave vnodes referencing
116521864bc5SMatthew Dillon * a closed device. This might not occur now that our revoke is
116621864bc5SMatthew Dillon * fixed.
116721864bc5SMatthew Dillon */
116821864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -1- \n");
116921864bc5SMatthew Dillon if (dev && ((vp->v_flag & VRECLAIMED) ||
117021864bc5SMatthew Dillon (dev_dflags(dev) & D_TRACKCLOSE) ||
117197248700SMatthew Dillon (opencount == 1))) {
1172898c91eeSMatthew Dillon /*
117392fb0c6aSMatthew Dillon * Ugly pty magic, to make pty devices disappear again once
117492fb0c6aSMatthew Dillon * they are closed.
117592fb0c6aSMatthew Dillon */
117692fb0c6aSMatthew Dillon node = DEVFS_NODE(ap->a_vp);
117792fb0c6aSMatthew Dillon if (node && (node->flags & DEVFS_PTY))
117892fb0c6aSMatthew Dillon node->flags |= DEVFS_INVISIBLE;
117992fb0c6aSMatthew Dillon
118092fb0c6aSMatthew Dillon /*
1181495d3a1eSMatthew Dillon * Unlock around dev_dclose(), unless the vnode is
1182495d3a1eSMatthew Dillon * undergoing a vgone/reclaim (during umount).
1183898c91eeSMatthew Dillon */
118421864bc5SMatthew Dillon needrelock = 0;
1185495d3a1eSMatthew Dillon if ((vp->v_flag & VRECLAIMED) == 0 && vn_islocked(vp)) {
118621864bc5SMatthew Dillon needrelock = 1;
118721864bc5SMatthew Dillon vn_unlock(vp);
118821864bc5SMatthew Dillon }
1189898c91eeSMatthew Dillon
1190898c91eeSMatthew Dillon /*
119192fb0c6aSMatthew Dillon * WARNING! If the device destroys itself the devfs node
119292fb0c6aSMatthew Dillon * can disappear here.
1193495d3a1eSMatthew Dillon *
1194495d3a1eSMatthew Dillon * WARNING! vn_lock() will fail if the vp is in a VRECLAIM,
1195495d3a1eSMatthew Dillon * which can occur during umount.
1196898c91eeSMatthew Dillon */
1197ce486e08SMarkus Pfeiffer error = dev_dclose(dev, ap->a_fflag, S_IFCHR, ap->a_fp);
119892fb0c6aSMatthew Dillon /* node is now stale */
119921864bc5SMatthew Dillon
1200495d3a1eSMatthew Dillon if (needrelock) {
1201b458d1abSMatthew Dillon if (vn_lock(vp, LK_EXCLUSIVE |
1202b458d1abSMatthew Dillon LK_RETRY |
1203b458d1abSMatthew Dillon LK_FAILRECLAIM) != 0) {
1204495d3a1eSMatthew Dillon panic("devfs_spec_close: vnode %p "
1205495d3a1eSMatthew Dillon "unexpectedly could not be relocked",
1206495d3a1eSMatthew Dillon vp);
1207495d3a1eSMatthew Dillon }
1208495d3a1eSMatthew Dillon }
120921864bc5SMatthew Dillon } else {
121021864bc5SMatthew Dillon error = 0;
121121864bc5SMatthew Dillon }
121221864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_spec_close() -2- \n");
1213898c91eeSMatthew Dillon
121421864bc5SMatthew Dillon /*
121521864bc5SMatthew Dillon * Track the actual opens and closes on the vnode. The last close
1216898c91eeSMatthew Dillon * disassociates the rdev. If the rdev is already disassociated or
1217898c91eeSMatthew Dillon * the opencount is already 0, the vnode might have been revoked
1218898c91eeSMatthew Dillon * and no further opencount tracking occurs.
121921864bc5SMatthew Dillon */
1220898c91eeSMatthew Dillon if (dev)
122121864bc5SMatthew Dillon release_dev(dev);
1222845bd036SMatthew Dillon skip:
1223898c91eeSMatthew Dillon if (vp->v_opencount > 0)
122421864bc5SMatthew Dillon vop_stdclose(ap);
122521864bc5SMatthew Dillon return(error);
122621864bc5SMatthew Dillon
122721864bc5SMatthew Dillon }
122821864bc5SMatthew Dillon
122921864bc5SMatthew Dillon
123021864bc5SMatthew Dillon static int
devfs_fo_close(struct file * fp)12319f889dc4SMatthew Dillon devfs_fo_close(struct file *fp)
123221864bc5SMatthew Dillon {
123321864bc5SMatthew Dillon struct vnode *vp = (struct vnode *)fp->f_data;
1234898c91eeSMatthew Dillon int error;
123521864bc5SMatthew Dillon
123621864bc5SMatthew Dillon fp->f_ops = &badfileops;
12373596743eSMarkus Pfeiffer error = vn_close(vp, fp->f_flag, fp);
1238ce486e08SMarkus Pfeiffer devfs_clear_cdevpriv(fp);
123921864bc5SMatthew Dillon
124021864bc5SMatthew Dillon return (error);
124121864bc5SMatthew Dillon }
124221864bc5SMatthew Dillon
124321864bc5SMatthew Dillon
124421864bc5SMatthew Dillon /*
124521864bc5SMatthew Dillon * Device-optimized file table vnode read routine.
124621864bc5SMatthew Dillon *
124721864bc5SMatthew Dillon * This bypasses the VOP table and talks directly to the device. Most
124821864bc5SMatthew Dillon * filesystems just route to specfs and can make this optimization.
124921864bc5SMatthew Dillon */
125021864bc5SMatthew Dillon static int
devfs_fo_read(struct file * fp,struct uio * uio,struct ucred * cred,int flags)12519f889dc4SMatthew Dillon devfs_fo_read(struct file *fp, struct uio *uio,
1252898c91eeSMatthew Dillon struct ucred *cred, int flags)
125321864bc5SMatthew Dillon {
1254898c91eeSMatthew Dillon struct devfs_node *node;
125521864bc5SMatthew Dillon struct vnode *vp;
125621864bc5SMatthew Dillon int ioflag;
125721864bc5SMatthew Dillon int error;
125821864bc5SMatthew Dillon cdev_t dev;
125921864bc5SMatthew Dillon
126021864bc5SMatthew Dillon KASSERT(uio->uio_td == curthread,
126121864bc5SMatthew Dillon ("uio_td %p is not td %p", uio->uio_td, curthread));
126221864bc5SMatthew Dillon
12633a1032a6SAlex Hornung if (uio->uio_resid == 0)
12643a1032a6SAlex Hornung return 0;
12653a1032a6SAlex Hornung
126621864bc5SMatthew Dillon vp = (struct vnode *)fp->f_data;
12673a1032a6SAlex Hornung if (vp == NULL || vp->v_type == VBAD)
12683a1032a6SAlex Hornung return EBADF;
12693a1032a6SAlex Hornung
1270898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
127121864bc5SMatthew Dillon
12723a1032a6SAlex Hornung if ((dev = vp->v_rdev) == NULL)
12733a1032a6SAlex Hornung return EBADF;
12743a1032a6SAlex Hornung
127521864bc5SMatthew Dillon reference_dev(dev);
127621864bc5SMatthew Dillon
127721864bc5SMatthew Dillon if ((flags & O_FOFFSET) == 0)
127821864bc5SMatthew Dillon uio->uio_offset = fp->f_offset;
127921864bc5SMatthew Dillon
128021864bc5SMatthew Dillon ioflag = 0;
128121864bc5SMatthew Dillon if (flags & O_FBLOCKING) {
128221864bc5SMatthew Dillon /* ioflag &= ~IO_NDELAY; */
128321864bc5SMatthew Dillon } else if (flags & O_FNONBLOCKING) {
128421864bc5SMatthew Dillon ioflag |= IO_NDELAY;
128521864bc5SMatthew Dillon } else if (fp->f_flag & FNONBLOCK) {
128621864bc5SMatthew Dillon ioflag |= IO_NDELAY;
128721864bc5SMatthew Dillon }
1288c72df65dSMatthew Dillon if (fp->f_flag & O_DIRECT) {
128921864bc5SMatthew Dillon ioflag |= IO_DIRECT;
129021864bc5SMatthew Dillon }
129121864bc5SMatthew Dillon ioflag |= sequential_heuristic(uio, fp);
129221864bc5SMatthew Dillon
12938c530b23SJohannes Hofmann error = dev_dread(dev, uio, ioflag, fp);
129421864bc5SMatthew Dillon
129521864bc5SMatthew Dillon release_dev(dev);
1296898c91eeSMatthew Dillon if (node)
1297d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
129821864bc5SMatthew Dillon if ((flags & O_FOFFSET) == 0)
129921864bc5SMatthew Dillon fp->f_offset = uio->uio_offset;
130021864bc5SMatthew Dillon fp->f_nextoff = uio->uio_offset;
13013a1032a6SAlex Hornung
130221864bc5SMatthew Dillon return (error);
130321864bc5SMatthew Dillon }
130421864bc5SMatthew Dillon
130521864bc5SMatthew Dillon
130621864bc5SMatthew Dillon static int
devfs_fo_write(struct file * fp,struct uio * uio,struct ucred * cred,int flags)13079f889dc4SMatthew Dillon devfs_fo_write(struct file *fp, struct uio *uio,
1308898c91eeSMatthew Dillon struct ucred *cred, int flags)
130921864bc5SMatthew Dillon {
1310898c91eeSMatthew Dillon struct devfs_node *node;
131121864bc5SMatthew Dillon struct vnode *vp;
131221864bc5SMatthew Dillon int ioflag;
131321864bc5SMatthew Dillon int error;
131421864bc5SMatthew Dillon cdev_t dev;
131521864bc5SMatthew Dillon
131621864bc5SMatthew Dillon KASSERT(uio->uio_td == curthread,
131721864bc5SMatthew Dillon ("uio_td %p is not p %p", uio->uio_td, curthread));
131821864bc5SMatthew Dillon
131921864bc5SMatthew Dillon vp = (struct vnode *)fp->f_data;
13203a1032a6SAlex Hornung if (vp == NULL || vp->v_type == VBAD)
13213a1032a6SAlex Hornung return EBADF;
13223a1032a6SAlex Hornung
1323898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
13243a1032a6SAlex Hornung
132521864bc5SMatthew Dillon if (vp->v_type == VREG)
132621864bc5SMatthew Dillon bwillwrite(uio->uio_resid);
13273a1032a6SAlex Hornung
132821864bc5SMatthew Dillon vp = (struct vnode *)fp->f_data;
132921864bc5SMatthew Dillon
13303a1032a6SAlex Hornung if ((dev = vp->v_rdev) == NULL)
13313a1032a6SAlex Hornung return EBADF;
13323a1032a6SAlex Hornung
133321864bc5SMatthew Dillon reference_dev(dev);
133421864bc5SMatthew Dillon
133521864bc5SMatthew Dillon if ((flags & O_FOFFSET) == 0)
133621864bc5SMatthew Dillon uio->uio_offset = fp->f_offset;
133721864bc5SMatthew Dillon
133821864bc5SMatthew Dillon ioflag = IO_UNIT;
133921864bc5SMatthew Dillon if (vp->v_type == VREG &&
134021864bc5SMatthew Dillon ((fp->f_flag & O_APPEND) || (flags & O_FAPPEND))) {
134121864bc5SMatthew Dillon ioflag |= IO_APPEND;
134221864bc5SMatthew Dillon }
134321864bc5SMatthew Dillon
134421864bc5SMatthew Dillon if (flags & O_FBLOCKING) {
134521864bc5SMatthew Dillon /* ioflag &= ~IO_NDELAY; */
134621864bc5SMatthew Dillon } else if (flags & O_FNONBLOCKING) {
134721864bc5SMatthew Dillon ioflag |= IO_NDELAY;
134821864bc5SMatthew Dillon } else if (fp->f_flag & FNONBLOCK) {
134921864bc5SMatthew Dillon ioflag |= IO_NDELAY;
135021864bc5SMatthew Dillon }
1351c72df65dSMatthew Dillon if (fp->f_flag & O_DIRECT) {
135221864bc5SMatthew Dillon ioflag |= IO_DIRECT;
135321864bc5SMatthew Dillon }
135421864bc5SMatthew Dillon if (flags & O_FASYNCWRITE) {
135521864bc5SMatthew Dillon /* ioflag &= ~IO_SYNC; */
135621864bc5SMatthew Dillon } else if (flags & O_FSYNCWRITE) {
135721864bc5SMatthew Dillon ioflag |= IO_SYNC;
135821864bc5SMatthew Dillon } else if (fp->f_flag & O_FSYNC) {
135921864bc5SMatthew Dillon ioflag |= IO_SYNC;
136021864bc5SMatthew Dillon }
136121864bc5SMatthew Dillon
136221864bc5SMatthew Dillon if (vp->v_mount && (vp->v_mount->mnt_flag & MNT_SYNCHRONOUS))
136321864bc5SMatthew Dillon ioflag |= IO_SYNC;
136421864bc5SMatthew Dillon ioflag |= sequential_heuristic(uio, fp);
136521864bc5SMatthew Dillon
13668c530b23SJohannes Hofmann error = dev_dwrite(dev, uio, ioflag, fp);
136721864bc5SMatthew Dillon
136821864bc5SMatthew Dillon release_dev(dev);
136907dfa375SAlex Hornung if (node) {
1370d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
1371d489a79aSMatthew Dillon vfs_timestamp(&node->mtime);
137207dfa375SAlex Hornung }
137321864bc5SMatthew Dillon
137421864bc5SMatthew Dillon if ((flags & O_FOFFSET) == 0)
137521864bc5SMatthew Dillon fp->f_offset = uio->uio_offset;
137621864bc5SMatthew Dillon fp->f_nextoff = uio->uio_offset;
13773a1032a6SAlex Hornung
137821864bc5SMatthew Dillon return (error);
137921864bc5SMatthew Dillon }
138021864bc5SMatthew Dillon
138121864bc5SMatthew Dillon
138221864bc5SMatthew Dillon static int
devfs_fo_stat(struct file * fp,struct stat * sb,struct ucred * cred)13839f889dc4SMatthew Dillon devfs_fo_stat(struct file *fp, struct stat *sb, struct ucred *cred)
138421864bc5SMatthew Dillon {
138521864bc5SMatthew Dillon struct vnode *vp;
138621864bc5SMatthew Dillon struct vattr vattr;
138721864bc5SMatthew Dillon struct vattr *vap;
138821864bc5SMatthew Dillon u_short mode;
138921864bc5SMatthew Dillon cdev_t dev;
13903a1032a6SAlex Hornung int error;
13913a1032a6SAlex Hornung
13923a1032a6SAlex Hornung vp = (struct vnode *)fp->f_data;
13933a1032a6SAlex Hornung if (vp == NULL || vp->v_type == VBAD)
13943a1032a6SAlex Hornung return EBADF;
13953a1032a6SAlex Hornung
13963a1032a6SAlex Hornung error = vn_stat(vp, sb, cred);
13973a1032a6SAlex Hornung if (error)
13983a1032a6SAlex Hornung return (error);
139921864bc5SMatthew Dillon
140021864bc5SMatthew Dillon vap = &vattr;
140121864bc5SMatthew Dillon error = VOP_GETATTR(vp, vap);
14023a1032a6SAlex Hornung if (error)
140321864bc5SMatthew Dillon return (error);
140421864bc5SMatthew Dillon
140521864bc5SMatthew Dillon /*
140621864bc5SMatthew Dillon * Zero the spare stat fields
140721864bc5SMatthew Dillon */
140821864bc5SMatthew Dillon sb->st_lspare = 0;
1409d98152a8SMatthew Dillon sb->st_qspare2 = 0;
141021864bc5SMatthew Dillon
141121864bc5SMatthew Dillon /*
141221864bc5SMatthew Dillon * Copy from vattr table ... or not in case it's a cloned device
141321864bc5SMatthew Dillon */
141421864bc5SMatthew Dillon if (vap->va_fsid != VNOVAL)
141521864bc5SMatthew Dillon sb->st_dev = vap->va_fsid;
141621864bc5SMatthew Dillon else
141721864bc5SMatthew Dillon sb->st_dev = vp->v_mount->mnt_stat.f_fsid.val[0];
141821864bc5SMatthew Dillon
141921864bc5SMatthew Dillon sb->st_ino = vap->va_fileid;
142021864bc5SMatthew Dillon
142121864bc5SMatthew Dillon mode = vap->va_mode;
142221864bc5SMatthew Dillon mode |= S_IFCHR;
142321864bc5SMatthew Dillon sb->st_mode = mode;
142421864bc5SMatthew Dillon
142521864bc5SMatthew Dillon if (vap->va_nlink > (nlink_t)-1)
142621864bc5SMatthew Dillon sb->st_nlink = (nlink_t)-1;
142721864bc5SMatthew Dillon else
142821864bc5SMatthew Dillon sb->st_nlink = vap->va_nlink;
14293a1032a6SAlex Hornung
143021864bc5SMatthew Dillon sb->st_uid = vap->va_uid;
143121864bc5SMatthew Dillon sb->st_gid = vap->va_gid;
14322ac7d105SSascha Wildner sb->st_rdev = devid_from_dev(DEVFS_NODE(vp)->d_dev);
14332d076755SAlex Hornung sb->st_size = vap->va_bytes;
143421864bc5SMatthew Dillon sb->st_atimespec = vap->va_atime;
143521864bc5SMatthew Dillon sb->st_mtimespec = vap->va_mtime;
143621864bc5SMatthew Dillon sb->st_ctimespec = vap->va_ctime;
143721864bc5SMatthew Dillon
143821864bc5SMatthew Dillon /*
143921864bc5SMatthew Dillon * A VCHR and VBLK device may track the last access and last modified
144021864bc5SMatthew Dillon * time independantly of the filesystem. This is particularly true
144121864bc5SMatthew Dillon * because device read and write calls may bypass the filesystem.
144221864bc5SMatthew Dillon */
144321864bc5SMatthew Dillon if (vp->v_type == VCHR || vp->v_type == VBLK) {
144421864bc5SMatthew Dillon dev = vp->v_rdev;
144521864bc5SMatthew Dillon if (dev != NULL) {
144621864bc5SMatthew Dillon if (dev->si_lastread) {
1447cec73927SMatthew Dillon sb->st_atimespec.tv_sec = time_second +
14481e45dd8cSMatthew Dillon (dev->si_lastread -
14491e45dd8cSMatthew Dillon time_uptime);
145021864bc5SMatthew Dillon sb->st_atimespec.tv_nsec = 0;
145121864bc5SMatthew Dillon }
145221864bc5SMatthew Dillon if (dev->si_lastwrite) {
14531e45dd8cSMatthew Dillon sb->st_mtimespec.tv_sec = time_second +
14541e45dd8cSMatthew Dillon (dev->si_lastwrite -
14551e45dd8cSMatthew Dillon time_uptime);
14561e45dd8cSMatthew Dillon sb->st_mtimespec.tv_nsec = 0;
145721864bc5SMatthew Dillon }
145821864bc5SMatthew Dillon }
145921864bc5SMatthew Dillon }
146021864bc5SMatthew Dillon
146121864bc5SMatthew Dillon /*
146221864bc5SMatthew Dillon * According to www.opengroup.org, the meaning of st_blksize is
146321864bc5SMatthew Dillon * "a filesystem-specific preferred I/O block size for this
146421864bc5SMatthew Dillon * object. In some filesystem types, this may vary from file
146521864bc5SMatthew Dillon * to file"
146621864bc5SMatthew Dillon * Default to PAGE_SIZE after much discussion.
146721864bc5SMatthew Dillon */
146821864bc5SMatthew Dillon
146921864bc5SMatthew Dillon sb->st_blksize = PAGE_SIZE;
147021864bc5SMatthew Dillon
147121864bc5SMatthew Dillon sb->st_flags = vap->va_flags;
147221864bc5SMatthew Dillon
14732b3f93eaSMatthew Dillon error = caps_priv_check(cred, SYSCAP_NOVFS_GENERATION);
147421864bc5SMatthew Dillon if (error)
147521864bc5SMatthew Dillon sb->st_gen = 0;
147621864bc5SMatthew Dillon else
147721864bc5SMatthew Dillon sb->st_gen = (u_int32_t)vap->va_gen;
147821864bc5SMatthew Dillon
147921864bc5SMatthew Dillon sb->st_blocks = vap->va_bytes / S_BLKSIZE;
148021864bc5SMatthew Dillon
148134c6728eSMatthew Dillon /*
148234c6728eSMatthew Dillon * This is for ABI compatibility <= 5.7 (for ABI change made in
148334c6728eSMatthew Dillon * 5.7 master).
148434c6728eSMatthew Dillon */
148534c6728eSMatthew Dillon sb->__old_st_blksize = sb->st_blksize;
148634c6728eSMatthew Dillon
148721864bc5SMatthew Dillon return (0);
148821864bc5SMatthew Dillon }
148921864bc5SMatthew Dillon
149021864bc5SMatthew Dillon
149121864bc5SMatthew Dillon static int
devfs_fo_kqfilter(struct file * fp,struct knote * kn)14929f889dc4SMatthew Dillon devfs_fo_kqfilter(struct file *fp, struct knote *kn)
149321864bc5SMatthew Dillon {
149421864bc5SMatthew Dillon struct vnode *vp;
149521864bc5SMatthew Dillon int error;
149621864bc5SMatthew Dillon cdev_t dev;
149721864bc5SMatthew Dillon
149821864bc5SMatthew Dillon vp = (struct vnode *)fp->f_data;
149921864bc5SMatthew Dillon if (vp == NULL || vp->v_type == VBAD) {
150021864bc5SMatthew Dillon error = EBADF;
150121864bc5SMatthew Dillon goto done;
150221864bc5SMatthew Dillon }
150321864bc5SMatthew Dillon if ((dev = vp->v_rdev) == NULL) {
150421864bc5SMatthew Dillon error = EBADF;
150521864bc5SMatthew Dillon goto done;
150621864bc5SMatthew Dillon }
150721864bc5SMatthew Dillon reference_dev(dev);
150821864bc5SMatthew Dillon
15098c530b23SJohannes Hofmann error = dev_dkqfilter(dev, kn, fp);
151021864bc5SMatthew Dillon
151121864bc5SMatthew Dillon release_dev(dev);
151221864bc5SMatthew Dillon
151321864bc5SMatthew Dillon done:
1514b287d649SMatthew Dillon return (error);
151521864bc5SMatthew Dillon }
151621864bc5SMatthew Dillon
151721864bc5SMatthew Dillon static int
devfs_fo_ioctl(struct file * fp,u_long com,caddr_t data,struct ucred * ucred,struct sysmsg * msg)15189f889dc4SMatthew Dillon devfs_fo_ioctl(struct file *fp, u_long com, caddr_t data,
151987baaf0cSMatthew Dillon struct ucred *ucred, struct sysmsg *msg)
152021864bc5SMatthew Dillon {
15211d0de3d3SSascha Wildner #if 0
1522898c91eeSMatthew Dillon struct devfs_node *node;
15231d0de3d3SSascha Wildner #endif
1524898c91eeSMatthew Dillon struct vnode *vp;
152521864bc5SMatthew Dillon struct vnode *ovp;
152621864bc5SMatthew Dillon cdev_t dev;
152721864bc5SMatthew Dillon int error;
152821864bc5SMatthew Dillon struct fiodname_args *name_args;
152921864bc5SMatthew Dillon size_t namlen;
153021864bc5SMatthew Dillon const char *name;
153121864bc5SMatthew Dillon
1532898c91eeSMatthew Dillon vp = ((struct vnode *)fp->f_data);
15333a1032a6SAlex Hornung
15343a1032a6SAlex Hornung if ((dev = vp->v_rdev) == NULL)
15353a1032a6SAlex Hornung return EBADF; /* device was revoked */
15363a1032a6SAlex Hornung
15373a1032a6SAlex Hornung reference_dev(dev);
153821864bc5SMatthew Dillon
15391d0de3d3SSascha Wildner #if 0
1540898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
15411d0de3d3SSascha Wildner #endif
1542898c91eeSMatthew Dillon
1543898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
15449f889dc4SMatthew Dillon "devfs_fo_ioctl() called! for dev %s\n",
1545898c91eeSMatthew Dillon dev->si_name);
154621864bc5SMatthew Dillon
154721864bc5SMatthew Dillon if (com == FIODTYPE) {
154821864bc5SMatthew Dillon *(int *)data = dev_dflags(dev) & D_TYPEMASK;
154921864bc5SMatthew Dillon error = 0;
155021864bc5SMatthew Dillon goto out;
155121864bc5SMatthew Dillon } else if (com == FIODNAME) {
155221864bc5SMatthew Dillon name_args = (struct fiodname_args *)data;
155321864bc5SMatthew Dillon name = dev->si_name;
155421864bc5SMatthew Dillon namlen = strlen(name) + 1;
155521864bc5SMatthew Dillon
1556898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1557898c91eeSMatthew Dillon "ioctl, got: FIODNAME for %s\n", name);
155821864bc5SMatthew Dillon
155921864bc5SMatthew Dillon if (namlen <= name_args->len)
156021864bc5SMatthew Dillon error = copyout(dev->si_name, name_args->name, namlen);
156121864bc5SMatthew Dillon else
156221864bc5SMatthew Dillon error = EINVAL;
156321864bc5SMatthew Dillon
1564898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1565898c91eeSMatthew Dillon "ioctl stuff: error: %d\n", error);
156621864bc5SMatthew Dillon goto out;
156721864bc5SMatthew Dillon }
15683a1032a6SAlex Hornung
15698c530b23SJohannes Hofmann error = dev_dioctl(dev, com, data, fp->f_flag, ucred, msg, fp);
15703a1032a6SAlex Hornung
157107dfa375SAlex Hornung #if 0
1572898c91eeSMatthew Dillon if (node) {
1573d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
1574d489a79aSMatthew Dillon vfs_timestamp(&node->mtime);
157521864bc5SMatthew Dillon }
157607dfa375SAlex Hornung #endif
1577898c91eeSMatthew Dillon if (com == TIOCSCTTY) {
1578898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
15799f889dc4SMatthew Dillon "devfs_fo_ioctl: got TIOCSCTTY on %s\n",
1580898c91eeSMatthew Dillon dev->si_name);
1581898c91eeSMatthew Dillon }
158221864bc5SMatthew Dillon if (error == 0 && com == TIOCSCTTY) {
158321864bc5SMatthew Dillon struct proc *p = curthread->td_proc;
158421864bc5SMatthew Dillon struct session *sess;
1585898c91eeSMatthew Dillon
1586898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
15879f889dc4SMatthew Dillon "devfs_fo_ioctl: dealing with TIOCSCTTY on %s\n",
1588898c91eeSMatthew Dillon dev->si_name);
158921864bc5SMatthew Dillon if (p == NULL) {
159021864bc5SMatthew Dillon error = ENOTTY;
159121864bc5SMatthew Dillon goto out;
159221864bc5SMatthew Dillon }
159321864bc5SMatthew Dillon sess = p->p_session;
1594898c91eeSMatthew Dillon
1595898c91eeSMatthew Dillon /*
1596898c91eeSMatthew Dillon * Do nothing if reassigning same control tty
1597898c91eeSMatthew Dillon */
159821864bc5SMatthew Dillon if (sess->s_ttyvp == vp) {
159921864bc5SMatthew Dillon error = 0;
160021864bc5SMatthew Dillon goto out;
160121864bc5SMatthew Dillon }
1602898c91eeSMatthew Dillon
1603898c91eeSMatthew Dillon /*
1604898c91eeSMatthew Dillon * Get rid of reference to old control tty
1605898c91eeSMatthew Dillon */
160621864bc5SMatthew Dillon ovp = sess->s_ttyvp;
160721864bc5SMatthew Dillon vref(vp);
160821864bc5SMatthew Dillon sess->s_ttyvp = vp;
160921864bc5SMatthew Dillon if (ovp)
161021864bc5SMatthew Dillon vrele(ovp);
161121864bc5SMatthew Dillon }
161221864bc5SMatthew Dillon
161321864bc5SMatthew Dillon out:
16143a1032a6SAlex Hornung release_dev(dev);
16159f889dc4SMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG, "devfs_fo_ioctl() finished! \n");
161621864bc5SMatthew Dillon return (error);
161721864bc5SMatthew Dillon }
161821864bc5SMatthew Dillon
1619*cc8e70bdSSergey Zigachev int
devfs_fo_seek(struct file * fp,off_t offset,int whence,off_t * res)1620*cc8e70bdSSergey Zigachev devfs_fo_seek(struct file *fp, off_t offset, int whence, off_t *res)
1621*cc8e70bdSSergey Zigachev {
1622*cc8e70bdSSergey Zigachev /*
1623*cc8e70bdSSergey Zigachev * NOTE: vnode_fileops uses exact same code
1624*cc8e70bdSSergey Zigachev */
1625*cc8e70bdSSergey Zigachev struct vnode *vp;
1626*cc8e70bdSSergey Zigachev struct vattr_lite lva;
1627*cc8e70bdSSergey Zigachev off_t new_offset;
1628*cc8e70bdSSergey Zigachev int error;
1629*cc8e70bdSSergey Zigachev
1630*cc8e70bdSSergey Zigachev vp = (struct vnode *)fp->f_data;
1631*cc8e70bdSSergey Zigachev
1632*cc8e70bdSSergey Zigachev switch (whence) {
1633*cc8e70bdSSergey Zigachev case L_INCR:
1634*cc8e70bdSSergey Zigachev spin_lock(&fp->f_spin);
1635*cc8e70bdSSergey Zigachev new_offset = fp->f_offset + offset;
1636*cc8e70bdSSergey Zigachev error = 0;
1637*cc8e70bdSSergey Zigachev break;
1638*cc8e70bdSSergey Zigachev case L_XTND:
1639*cc8e70bdSSergey Zigachev error = VOP_GETATTR_LITE(vp, &lva);
1640*cc8e70bdSSergey Zigachev spin_lock(&fp->f_spin);
1641*cc8e70bdSSergey Zigachev new_offset = offset + lva.va_size;
1642*cc8e70bdSSergey Zigachev break;
1643*cc8e70bdSSergey Zigachev case L_SET:
1644*cc8e70bdSSergey Zigachev new_offset = offset;
1645*cc8e70bdSSergey Zigachev error = 0;
1646*cc8e70bdSSergey Zigachev spin_lock(&fp->f_spin);
1647*cc8e70bdSSergey Zigachev break;
1648*cc8e70bdSSergey Zigachev default:
1649*cc8e70bdSSergey Zigachev new_offset = 0;
1650*cc8e70bdSSergey Zigachev error = EINVAL;
1651*cc8e70bdSSergey Zigachev spin_lock(&fp->f_spin);
1652*cc8e70bdSSergey Zigachev break;
1653*cc8e70bdSSergey Zigachev }
1654*cc8e70bdSSergey Zigachev
1655*cc8e70bdSSergey Zigachev /*
1656*cc8e70bdSSergey Zigachev * Validate the seek position. Negative offsets are not allowed
1657*cc8e70bdSSergey Zigachev * for regular files or directories.
1658*cc8e70bdSSergey Zigachev *
1659*cc8e70bdSSergey Zigachev * Normally we would also not want to allow negative offsets for
1660*cc8e70bdSSergey Zigachev * character and block-special devices. However kvm addresses
1661*cc8e70bdSSergey Zigachev * on 64 bit architectures might appear to be negative and must
1662*cc8e70bdSSergey Zigachev * be allowed.
1663*cc8e70bdSSergey Zigachev */
1664*cc8e70bdSSergey Zigachev if (error == 0) {
1665*cc8e70bdSSergey Zigachev if (new_offset < 0 &&
1666*cc8e70bdSSergey Zigachev (vp->v_type == VREG || vp->v_type == VDIR)) {
1667*cc8e70bdSSergey Zigachev error = EINVAL;
1668*cc8e70bdSSergey Zigachev } else {
1669*cc8e70bdSSergey Zigachev fp->f_offset = new_offset;
1670*cc8e70bdSSergey Zigachev }
1671*cc8e70bdSSergey Zigachev }
1672*cc8e70bdSSergey Zigachev *res = fp->f_offset;
1673*cc8e70bdSSergey Zigachev spin_unlock(&fp->f_spin);
1674*cc8e70bdSSergey Zigachev
1675*cc8e70bdSSergey Zigachev return (error);
1676*cc8e70bdSSergey Zigachev }
167721864bc5SMatthew Dillon
167821864bc5SMatthew Dillon static int
devfs_spec_fsync(struct vop_fsync_args * ap)167921864bc5SMatthew Dillon devfs_spec_fsync(struct vop_fsync_args *ap)
168021864bc5SMatthew Dillon {
168121864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
168221864bc5SMatthew Dillon int error;
168321864bc5SMatthew Dillon
168421864bc5SMatthew Dillon if (!vn_isdisk(vp, NULL))
168521864bc5SMatthew Dillon return (0);
168621864bc5SMatthew Dillon
168721864bc5SMatthew Dillon /*
168821864bc5SMatthew Dillon * Flush all dirty buffers associated with a block device.
168921864bc5SMatthew Dillon */
169021864bc5SMatthew Dillon error = vfsync(vp, ap->a_waitfor, 10000, NULL, NULL);
169121864bc5SMatthew Dillon return (error);
169221864bc5SMatthew Dillon }
169321864bc5SMatthew Dillon
169421864bc5SMatthew Dillon static int
devfs_spec_read(struct vop_read_args * ap)169521864bc5SMatthew Dillon devfs_spec_read(struct vop_read_args *ap)
169621864bc5SMatthew Dillon {
1697898c91eeSMatthew Dillon struct devfs_node *node;
169821864bc5SMatthew Dillon struct vnode *vp;
169921864bc5SMatthew Dillon struct uio *uio;
170021864bc5SMatthew Dillon cdev_t dev;
170121864bc5SMatthew Dillon int error;
170221864bc5SMatthew Dillon
170321864bc5SMatthew Dillon vp = ap->a_vp;
170421864bc5SMatthew Dillon dev = vp->v_rdev;
170521864bc5SMatthew Dillon uio = ap->a_uio;
1706898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
170721864bc5SMatthew Dillon
170821864bc5SMatthew Dillon if (dev == NULL) /* device was revoked */
170921864bc5SMatthew Dillon return (EBADF);
171021864bc5SMatthew Dillon if (uio->uio_resid == 0)
171121864bc5SMatthew Dillon return (0);
171221864bc5SMatthew Dillon
171321864bc5SMatthew Dillon vn_unlock(vp);
17148c530b23SJohannes Hofmann error = dev_dread(dev, uio, ap->a_ioflag, NULL);
1715845bd036SMatthew Dillon vn_lock(vp, LK_SHARED | LK_RETRY);
171621864bc5SMatthew Dillon
1717898c91eeSMatthew Dillon if (node)
1718d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
171921864bc5SMatthew Dillon
172021864bc5SMatthew Dillon return (error);
172121864bc5SMatthew Dillon }
172221864bc5SMatthew Dillon
172321864bc5SMatthew Dillon /*
172421864bc5SMatthew Dillon * Vnode op for write
172521864bc5SMatthew Dillon *
172621864bc5SMatthew Dillon * spec_write(struct vnode *a_vp, struct uio *a_uio, int a_ioflag,
172721864bc5SMatthew Dillon * struct ucred *a_cred)
172821864bc5SMatthew Dillon */
172921864bc5SMatthew Dillon static int
devfs_spec_write(struct vop_write_args * ap)173021864bc5SMatthew Dillon devfs_spec_write(struct vop_write_args *ap)
173121864bc5SMatthew Dillon {
1732898c91eeSMatthew Dillon struct devfs_node *node;
173321864bc5SMatthew Dillon struct vnode *vp;
173421864bc5SMatthew Dillon struct uio *uio;
173521864bc5SMatthew Dillon cdev_t dev;
173621864bc5SMatthew Dillon int error;
173721864bc5SMatthew Dillon
173821864bc5SMatthew Dillon vp = ap->a_vp;
173921864bc5SMatthew Dillon dev = vp->v_rdev;
174021864bc5SMatthew Dillon uio = ap->a_uio;
1741898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
174221864bc5SMatthew Dillon
174321864bc5SMatthew Dillon KKASSERT(uio->uio_segflg != UIO_NOCOPY);
174421864bc5SMatthew Dillon
174521864bc5SMatthew Dillon if (dev == NULL) /* device was revoked */
174621864bc5SMatthew Dillon return (EBADF);
174721864bc5SMatthew Dillon
174821864bc5SMatthew Dillon vn_unlock(vp);
17498c530b23SJohannes Hofmann error = dev_dwrite(dev, uio, ap->a_ioflag, NULL);
175021864bc5SMatthew Dillon vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
175121864bc5SMatthew Dillon
175207dfa375SAlex Hornung if (node) {
1753d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
1754d489a79aSMatthew Dillon vfs_timestamp(&node->mtime);
175507dfa375SAlex Hornung }
175621864bc5SMatthew Dillon
175721864bc5SMatthew Dillon return (error);
175821864bc5SMatthew Dillon }
175921864bc5SMatthew Dillon
176021864bc5SMatthew Dillon /*
176121864bc5SMatthew Dillon * Device ioctl operation.
176221864bc5SMatthew Dillon *
176321864bc5SMatthew Dillon * spec_ioctl(struct vnode *a_vp, int a_command, caddr_t a_data,
176487baaf0cSMatthew Dillon * int a_fflag, struct ucred *a_cred, struct sysmsg *msg)
176521864bc5SMatthew Dillon */
176621864bc5SMatthew Dillon static int
devfs_spec_ioctl(struct vop_ioctl_args * ap)176721864bc5SMatthew Dillon devfs_spec_ioctl(struct vop_ioctl_args *ap)
176821864bc5SMatthew Dillon {
176921864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
17701d0de3d3SSascha Wildner #if 0
1771898c91eeSMatthew Dillon struct devfs_node *node;
17721d0de3d3SSascha Wildner #endif
1773898c91eeSMatthew Dillon cdev_t dev;
177421864bc5SMatthew Dillon
177521864bc5SMatthew Dillon if ((dev = vp->v_rdev) == NULL)
177621864bc5SMatthew Dillon return (EBADF); /* device was revoked */
17771d0de3d3SSascha Wildner #if 0
1778898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
177921864bc5SMatthew Dillon
1780898c91eeSMatthew Dillon if (node) {
1781d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
1782d489a79aSMatthew Dillon vfs_timestamp(&node->mtime);
178321864bc5SMatthew Dillon }
178407dfa375SAlex Hornung #endif
178521864bc5SMatthew Dillon
178687baaf0cSMatthew Dillon return (dev_dioctl(dev, ap->a_command, ap->a_data, ap->a_fflag,
17878c530b23SJohannes Hofmann ap->a_cred, ap->a_sysmsg, NULL));
178821864bc5SMatthew Dillon }
178921864bc5SMatthew Dillon
179021864bc5SMatthew Dillon /*
179121864bc5SMatthew Dillon * spec_kqfilter(struct vnode *a_vp, struct knote *a_kn)
179221864bc5SMatthew Dillon */
179321864bc5SMatthew Dillon /* ARGSUSED */
179421864bc5SMatthew Dillon static int
devfs_spec_kqfilter(struct vop_kqfilter_args * ap)179521864bc5SMatthew Dillon devfs_spec_kqfilter(struct vop_kqfilter_args *ap)
179621864bc5SMatthew Dillon {
179721864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
17981d0de3d3SSascha Wildner #if 0
1799898c91eeSMatthew Dillon struct devfs_node *node;
18001d0de3d3SSascha Wildner #endif
1801898c91eeSMatthew Dillon cdev_t dev;
180221864bc5SMatthew Dillon
180321864bc5SMatthew Dillon if ((dev = vp->v_rdev) == NULL)
1804b287d649SMatthew Dillon return (EBADF); /* device was revoked (EBADF) */
18051d0de3d3SSascha Wildner #if 0
1806898c91eeSMatthew Dillon node = DEVFS_NODE(vp);
180721864bc5SMatthew Dillon
1808898c91eeSMatthew Dillon if (node)
1809d489a79aSMatthew Dillon vfs_timestamp(&node->atime);
181007dfa375SAlex Hornung #endif
181121864bc5SMatthew Dillon
18128c530b23SJohannes Hofmann return (dev_dkqfilter(dev, ap->a_kn, NULL));
181321864bc5SMatthew Dillon }
181421864bc5SMatthew Dillon
181521864bc5SMatthew Dillon /*
181621864bc5SMatthew Dillon * Convert a vnode strategy call into a device strategy call. Vnode strategy
181721864bc5SMatthew Dillon * calls are not limited to device DMA limits so we have to deal with the
181821864bc5SMatthew Dillon * case.
181921864bc5SMatthew Dillon *
182021864bc5SMatthew Dillon * spec_strategy(struct vnode *a_vp, struct bio *a_bio)
182121864bc5SMatthew Dillon */
182221864bc5SMatthew Dillon static int
devfs_spec_strategy(struct vop_strategy_args * ap)182321864bc5SMatthew Dillon devfs_spec_strategy(struct vop_strategy_args *ap)
182421864bc5SMatthew Dillon {
182521864bc5SMatthew Dillon struct bio *bio = ap->a_bio;
182621864bc5SMatthew Dillon struct buf *bp = bio->bio_buf;
182721864bc5SMatthew Dillon struct buf *nbp;
182821864bc5SMatthew Dillon struct vnode *vp;
182921864bc5SMatthew Dillon struct mount *mp;
183021864bc5SMatthew Dillon int chunksize;
183121864bc5SMatthew Dillon int maxiosize;
183221864bc5SMatthew Dillon
183321864bc5SMatthew Dillon if (bp->b_cmd != BUF_CMD_READ && LIST_FIRST(&bp->b_dep) != NULL)
183421864bc5SMatthew Dillon buf_start(bp);
183521864bc5SMatthew Dillon
183621864bc5SMatthew Dillon /*
183721864bc5SMatthew Dillon * Collect statistics on synchronous and asynchronous read
183821864bc5SMatthew Dillon * and write counts for disks that have associated filesystems.
183921864bc5SMatthew Dillon */
184021864bc5SMatthew Dillon vp = ap->a_vp;
184121864bc5SMatthew Dillon KKASSERT(vp->v_rdev != NULL); /* XXX */
184221864bc5SMatthew Dillon if (vn_isdisk(vp, NULL) && (mp = vp->v_rdev->si_mountpoint) != NULL) {
184321864bc5SMatthew Dillon if (bp->b_cmd == BUF_CMD_READ) {
184421864bc5SMatthew Dillon if (bp->b_flags & BIO_SYNC)
184521864bc5SMatthew Dillon mp->mnt_stat.f_syncreads++;
184621864bc5SMatthew Dillon else
184721864bc5SMatthew Dillon mp->mnt_stat.f_asyncreads++;
184821864bc5SMatthew Dillon } else {
184921864bc5SMatthew Dillon if (bp->b_flags & BIO_SYNC)
185021864bc5SMatthew Dillon mp->mnt_stat.f_syncwrites++;
185121864bc5SMatthew Dillon else
185221864bc5SMatthew Dillon mp->mnt_stat.f_asyncwrites++;
185321864bc5SMatthew Dillon }
185421864bc5SMatthew Dillon }
185521864bc5SMatthew Dillon
185621864bc5SMatthew Dillon /*
185721864bc5SMatthew Dillon * Device iosize limitations only apply to read and write. Shortcut
185821864bc5SMatthew Dillon * the I/O if it fits.
185921864bc5SMatthew Dillon */
186021864bc5SMatthew Dillon if ((maxiosize = vp->v_rdev->si_iosize_max) == 0) {
1861898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1862898c91eeSMatthew Dillon "%s: si_iosize_max not set!\n",
1863898c91eeSMatthew Dillon dev_dname(vp->v_rdev));
186421864bc5SMatthew Dillon maxiosize = MAXPHYS;
186521864bc5SMatthew Dillon }
186621864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 2
186721864bc5SMatthew Dillon maxiosize = 4096;
186821864bc5SMatthew Dillon #endif
186921864bc5SMatthew Dillon if (bp->b_bcount <= maxiosize ||
187021864bc5SMatthew Dillon (bp->b_cmd != BUF_CMD_READ && bp->b_cmd != BUF_CMD_WRITE)) {
187121864bc5SMatthew Dillon dev_dstrategy_chain(vp->v_rdev, bio);
187221864bc5SMatthew Dillon return (0);
187321864bc5SMatthew Dillon }
187421864bc5SMatthew Dillon
187521864bc5SMatthew Dillon /*
187621864bc5SMatthew Dillon * Clone the buffer and set up an I/O chain to chunk up the I/O.
187721864bc5SMatthew Dillon */
187821864bc5SMatthew Dillon nbp = kmalloc(sizeof(*bp), M_DEVBUF, M_INTWAIT|M_ZERO);
187921864bc5SMatthew Dillon initbufbio(nbp);
188021864bc5SMatthew Dillon buf_dep_init(nbp);
188121864bc5SMatthew Dillon BUF_LOCK(nbp, LK_EXCLUSIVE);
188221864bc5SMatthew Dillon BUF_KERNPROC(nbp);
188321864bc5SMatthew Dillon nbp->b_vp = vp;
1884b3f55d88SMatthew Dillon nbp->b_flags = B_PAGING | B_KVABIO | (bp->b_flags & B_BNOCLIP);
1885b3f55d88SMatthew Dillon nbp->b_cpumask = bp->b_cpumask;
188621864bc5SMatthew Dillon nbp->b_data = bp->b_data;
188721864bc5SMatthew Dillon nbp->b_bio1.bio_done = devfs_spec_strategy_done;
188821864bc5SMatthew Dillon nbp->b_bio1.bio_offset = bio->bio_offset;
188921864bc5SMatthew Dillon nbp->b_bio1.bio_caller_info1.ptr = bio;
189021864bc5SMatthew Dillon
189121864bc5SMatthew Dillon /*
189221864bc5SMatthew Dillon * Start the first transfer
189321864bc5SMatthew Dillon */
189421864bc5SMatthew Dillon if (vn_isdisk(vp, NULL))
189521864bc5SMatthew Dillon chunksize = vp->v_rdev->si_bsize_phys;
189621864bc5SMatthew Dillon else
189721864bc5SMatthew Dillon chunksize = DEV_BSIZE;
1898ed183f8cSSascha Wildner chunksize = rounddown(maxiosize, chunksize);
189921864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1900898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1901898c91eeSMatthew Dillon "spec_strategy chained I/O chunksize=%d\n",
1902898c91eeSMatthew Dillon chunksize);
190321864bc5SMatthew Dillon #endif
190421864bc5SMatthew Dillon nbp->b_cmd = bp->b_cmd;
190521864bc5SMatthew Dillon nbp->b_bcount = chunksize;
190621864bc5SMatthew Dillon nbp->b_bufsize = chunksize; /* used to detect a short I/O */
190721864bc5SMatthew Dillon nbp->b_bio1.bio_caller_info2.index = chunksize;
190821864bc5SMatthew Dillon
190921864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1910898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1911898c91eeSMatthew Dillon "spec_strategy: chain %p offset %d/%d bcount %d\n",
191221864bc5SMatthew Dillon bp, 0, bp->b_bcount, nbp->b_bcount);
191321864bc5SMatthew Dillon #endif
191421864bc5SMatthew Dillon
191521864bc5SMatthew Dillon dev_dstrategy(vp->v_rdev, &nbp->b_bio1);
191621864bc5SMatthew Dillon
191721864bc5SMatthew Dillon if (DEVFS_NODE(vp)) {
1918d489a79aSMatthew Dillon vfs_timestamp(&DEVFS_NODE(vp)->atime);
1919d489a79aSMatthew Dillon vfs_timestamp(&DEVFS_NODE(vp)->mtime);
192021864bc5SMatthew Dillon }
192121864bc5SMatthew Dillon
192221864bc5SMatthew Dillon return (0);
192321864bc5SMatthew Dillon }
192421864bc5SMatthew Dillon
192521864bc5SMatthew Dillon /*
192621864bc5SMatthew Dillon * Chunked up transfer completion routine - chain transfers until done
192777912481SMatthew Dillon *
192877912481SMatthew Dillon * NOTE: MPSAFE callback.
192921864bc5SMatthew Dillon */
193021864bc5SMatthew Dillon static
193121864bc5SMatthew Dillon void
devfs_spec_strategy_done(struct bio * nbio)193221864bc5SMatthew Dillon devfs_spec_strategy_done(struct bio *nbio)
193321864bc5SMatthew Dillon {
193421864bc5SMatthew Dillon struct buf *nbp = nbio->bio_buf;
193521864bc5SMatthew Dillon struct bio *bio = nbio->bio_caller_info1.ptr; /* original bio */
193621864bc5SMatthew Dillon struct buf *bp = bio->bio_buf; /* original bp */
193721864bc5SMatthew Dillon int chunksize = nbio->bio_caller_info2.index; /* chunking */
193821864bc5SMatthew Dillon int boffset = nbp->b_data - bp->b_data;
193921864bc5SMatthew Dillon
194021864bc5SMatthew Dillon if (nbp->b_flags & B_ERROR) {
194121864bc5SMatthew Dillon /*
194221864bc5SMatthew Dillon * An error terminates the chain, propogate the error back
194321864bc5SMatthew Dillon * to the original bp
194421864bc5SMatthew Dillon */
194521864bc5SMatthew Dillon bp->b_flags |= B_ERROR;
194621864bc5SMatthew Dillon bp->b_error = nbp->b_error;
194721864bc5SMatthew Dillon bp->b_resid = bp->b_bcount - boffset +
194821864bc5SMatthew Dillon (nbp->b_bcount - nbp->b_resid);
194921864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1950898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1951898c91eeSMatthew Dillon "spec_strategy: chain %p error %d bcount %d/%d\n",
195221864bc5SMatthew Dillon bp, bp->b_error, bp->b_bcount,
195321864bc5SMatthew Dillon bp->b_bcount - bp->b_resid);
195421864bc5SMatthew Dillon #endif
195521864bc5SMatthew Dillon } else if (nbp->b_resid) {
195621864bc5SMatthew Dillon /*
195721864bc5SMatthew Dillon * A short read or write terminates the chain
195821864bc5SMatthew Dillon */
195921864bc5SMatthew Dillon bp->b_error = nbp->b_error;
196021864bc5SMatthew Dillon bp->b_resid = bp->b_bcount - boffset +
196121864bc5SMatthew Dillon (nbp->b_bcount - nbp->b_resid);
196221864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1963898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1964898c91eeSMatthew Dillon "spec_strategy: chain %p short read(1) "
1965898c91eeSMatthew Dillon "bcount %d/%d\n",
196621864bc5SMatthew Dillon bp, bp->b_bcount - bp->b_resid, bp->b_bcount);
196721864bc5SMatthew Dillon #endif
196821864bc5SMatthew Dillon } else if (nbp->b_bcount != nbp->b_bufsize) {
196921864bc5SMatthew Dillon /*
197021864bc5SMatthew Dillon * A short read or write can also occur by truncating b_bcount
197121864bc5SMatthew Dillon */
197221864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1973898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1974898c91eeSMatthew Dillon "spec_strategy: chain %p short read(2) "
1975898c91eeSMatthew Dillon "bcount %d/%d\n",
197621864bc5SMatthew Dillon bp, nbp->b_bcount + boffset, bp->b_bcount);
197721864bc5SMatthew Dillon #endif
197821864bc5SMatthew Dillon bp->b_error = 0;
197921864bc5SMatthew Dillon bp->b_bcount = nbp->b_bcount + boffset;
198021864bc5SMatthew Dillon bp->b_resid = nbp->b_resid;
198121864bc5SMatthew Dillon } else if (nbp->b_bcount + boffset == bp->b_bcount) {
198221864bc5SMatthew Dillon /*
198321864bc5SMatthew Dillon * No more data terminates the chain
198421864bc5SMatthew Dillon */
198521864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
1986898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
1987898c91eeSMatthew Dillon "spec_strategy: chain %p finished bcount %d\n",
198821864bc5SMatthew Dillon bp, bp->b_bcount);
198921864bc5SMatthew Dillon #endif
199021864bc5SMatthew Dillon bp->b_error = 0;
199121864bc5SMatthew Dillon bp->b_resid = 0;
199221864bc5SMatthew Dillon } else {
199321864bc5SMatthew Dillon /*
199421864bc5SMatthew Dillon * Continue the chain
199521864bc5SMatthew Dillon */
199621864bc5SMatthew Dillon boffset += nbp->b_bcount;
199721864bc5SMatthew Dillon nbp->b_data = bp->b_data + boffset;
199821864bc5SMatthew Dillon nbp->b_bcount = bp->b_bcount - boffset;
199921864bc5SMatthew Dillon if (nbp->b_bcount > chunksize)
200021864bc5SMatthew Dillon nbp->b_bcount = chunksize;
200121864bc5SMatthew Dillon nbp->b_bio1.bio_done = devfs_spec_strategy_done;
200221864bc5SMatthew Dillon nbp->b_bio1.bio_offset = bio->bio_offset + boffset;
200321864bc5SMatthew Dillon
200421864bc5SMatthew Dillon #if SPEC_CHAIN_DEBUG & 1
2005898c91eeSMatthew Dillon devfs_debug(DEVFS_DEBUG_DEBUG,
2006898c91eeSMatthew Dillon "spec_strategy: chain %p offset %d/%d bcount %d\n",
200721864bc5SMatthew Dillon bp, boffset, bp->b_bcount, nbp->b_bcount);
200821864bc5SMatthew Dillon #endif
200921864bc5SMatthew Dillon
201021864bc5SMatthew Dillon dev_dstrategy(nbp->b_vp->v_rdev, &nbp->b_bio1);
2011b5d7061dSMatthew Dillon return;
201221864bc5SMatthew Dillon }
2013b5d7061dSMatthew Dillon
2014b5d7061dSMatthew Dillon /*
2015b5d7061dSMatthew Dillon * Fall through to here on termination. biodone(bp) and
2016b5d7061dSMatthew Dillon * clean up and free nbp.
2017b5d7061dSMatthew Dillon */
2018b5d7061dSMatthew Dillon biodone(bio);
2019b5d7061dSMatthew Dillon BUF_UNLOCK(nbp);
2020b5d7061dSMatthew Dillon uninitbufbio(nbp);
2021b5d7061dSMatthew Dillon kfree(nbp, M_DEVBUF);
202221864bc5SMatthew Dillon }
202321864bc5SMatthew Dillon
202421864bc5SMatthew Dillon /*
202521864bc5SMatthew Dillon * spec_freeblks(struct vnode *a_vp, daddr_t a_addr, daddr_t a_length)
202621864bc5SMatthew Dillon */
202721864bc5SMatthew Dillon static int
devfs_spec_freeblks(struct vop_freeblks_args * ap)202821864bc5SMatthew Dillon devfs_spec_freeblks(struct vop_freeblks_args *ap)
202921864bc5SMatthew Dillon {
203021864bc5SMatthew Dillon struct buf *bp;
203121864bc5SMatthew Dillon
203221864bc5SMatthew Dillon /*
203353005b09SMatthew Dillon * Must be a synchronous operation
203421864bc5SMatthew Dillon */
203521864bc5SMatthew Dillon KKASSERT(ap->a_vp->v_rdev != NULL);
2036bf390b25SAlex Hornung if ((ap->a_vp->v_rdev->si_flags & SI_CANFREE) == 0)
203721864bc5SMatthew Dillon return (0);
2038bf20632cSMatthew Dillon bp = getpbuf(NULL);
203921864bc5SMatthew Dillon bp->b_cmd = BUF_CMD_FREEBLKS;
204053005b09SMatthew Dillon bp->b_bio1.bio_flags |= BIO_SYNC;
204121864bc5SMatthew Dillon bp->b_bio1.bio_offset = ap->a_offset;
204253005b09SMatthew Dillon bp->b_bio1.bio_done = biodone_sync;
204321864bc5SMatthew Dillon bp->b_bcount = ap->a_length;
204421864bc5SMatthew Dillon dev_dstrategy(ap->a_vp->v_rdev, &bp->b_bio1);
204553005b09SMatthew Dillon biowait(&bp->b_bio1, "TRIM");
2046bf20632cSMatthew Dillon relpbuf(bp, NULL);
204753005b09SMatthew Dillon
204821864bc5SMatthew Dillon return (0);
204921864bc5SMatthew Dillon }
205021864bc5SMatthew Dillon
205121864bc5SMatthew Dillon /*
205221864bc5SMatthew Dillon * Implement degenerate case where the block requested is the block
205321864bc5SMatthew Dillon * returned, and assume that the entire device is contiguous in regards
205421864bc5SMatthew Dillon * to the contiguous block range (runp and runb).
205521864bc5SMatthew Dillon *
205621864bc5SMatthew Dillon * spec_bmap(struct vnode *a_vp, off_t a_loffset,
205721864bc5SMatthew Dillon * off_t *a_doffsetp, int *a_runp, int *a_runb)
205821864bc5SMatthew Dillon */
205921864bc5SMatthew Dillon static int
devfs_spec_bmap(struct vop_bmap_args * ap)206021864bc5SMatthew Dillon devfs_spec_bmap(struct vop_bmap_args *ap)
206121864bc5SMatthew Dillon {
206221864bc5SMatthew Dillon if (ap->a_doffsetp != NULL)
206321864bc5SMatthew Dillon *ap->a_doffsetp = ap->a_loffset;
206421864bc5SMatthew Dillon if (ap->a_runp != NULL)
206521864bc5SMatthew Dillon *ap->a_runp = MAXBSIZE;
206621864bc5SMatthew Dillon if (ap->a_runb != NULL) {
206721864bc5SMatthew Dillon if (ap->a_loffset < MAXBSIZE)
206821864bc5SMatthew Dillon *ap->a_runb = (int)ap->a_loffset;
206921864bc5SMatthew Dillon else
207021864bc5SMatthew Dillon *ap->a_runb = MAXBSIZE;
207121864bc5SMatthew Dillon }
207221864bc5SMatthew Dillon return (0);
207321864bc5SMatthew Dillon }
207421864bc5SMatthew Dillon
207521864bc5SMatthew Dillon
207621864bc5SMatthew Dillon /*
207721864bc5SMatthew Dillon * Special device advisory byte-level locks.
207821864bc5SMatthew Dillon *
207921864bc5SMatthew Dillon * spec_advlock(struct vnode *a_vp, caddr_t a_id, int a_op,
208021864bc5SMatthew Dillon * struct flock *a_fl, int a_flags)
208121864bc5SMatthew Dillon */
208221864bc5SMatthew Dillon /* ARGSUSED */
208321864bc5SMatthew Dillon static int
devfs_spec_advlock(struct vop_advlock_args * ap)208421864bc5SMatthew Dillon devfs_spec_advlock(struct vop_advlock_args *ap)
208521864bc5SMatthew Dillon {
208621864bc5SMatthew Dillon return ((ap->a_flags & F_POSIX) ? EINVAL : EOPNOTSUPP);
208721864bc5SMatthew Dillon }
208821864bc5SMatthew Dillon
208977912481SMatthew Dillon /*
209077912481SMatthew Dillon * NOTE: MPSAFE callback.
209177912481SMatthew Dillon */
209221864bc5SMatthew Dillon static void
devfs_spec_getpages_iodone(struct bio * bio)209321864bc5SMatthew Dillon devfs_spec_getpages_iodone(struct bio *bio)
209421864bc5SMatthew Dillon {
209521864bc5SMatthew Dillon bio->bio_buf->b_cmd = BUF_CMD_DONE;
209621864bc5SMatthew Dillon wakeup(bio->bio_buf);
209721864bc5SMatthew Dillon }
209821864bc5SMatthew Dillon
209921864bc5SMatthew Dillon /*
210021864bc5SMatthew Dillon * spec_getpages() - get pages associated with device vnode.
210121864bc5SMatthew Dillon *
210221864bc5SMatthew Dillon * Note that spec_read and spec_write do not use the buffer cache, so we
210321864bc5SMatthew Dillon * must fully implement getpages here.
210421864bc5SMatthew Dillon */
210521864bc5SMatthew Dillon static int
devfs_spec_getpages(struct vop_getpages_args * ap)210621864bc5SMatthew Dillon devfs_spec_getpages(struct vop_getpages_args *ap)
210721864bc5SMatthew Dillon {
210821864bc5SMatthew Dillon vm_offset_t kva;
210921864bc5SMatthew Dillon int error;
211021864bc5SMatthew Dillon int i, pcount, size;
211121864bc5SMatthew Dillon struct buf *bp;
211221864bc5SMatthew Dillon vm_page_t m;
211321864bc5SMatthew Dillon vm_ooffset_t offset;
211421864bc5SMatthew Dillon int toff, nextoff, nread;
211521864bc5SMatthew Dillon struct vnode *vp = ap->a_vp;
211621864bc5SMatthew Dillon int blksiz;
211721864bc5SMatthew Dillon int gotreqpage;
211821864bc5SMatthew Dillon
211921864bc5SMatthew Dillon error = 0;
212021864bc5SMatthew Dillon pcount = round_page(ap->a_count) / PAGE_SIZE;
212121864bc5SMatthew Dillon
212221864bc5SMatthew Dillon /*
212321864bc5SMatthew Dillon * Calculate the offset of the transfer and do sanity check.
212421864bc5SMatthew Dillon */
212521864bc5SMatthew Dillon offset = IDX_TO_OFF(ap->a_m[0]->pindex) + ap->a_offset;
212621864bc5SMatthew Dillon
212721864bc5SMatthew Dillon /*
212821864bc5SMatthew Dillon * Round up physical size for real devices. We cannot round using
212921864bc5SMatthew Dillon * v_mount's block size data because v_mount has nothing to do with
213021864bc5SMatthew Dillon * the device. i.e. it's usually '/dev'. We need the physical block
213121864bc5SMatthew Dillon * size for the device itself.
213221864bc5SMatthew Dillon *
213321864bc5SMatthew Dillon * We can't use v_rdev->si_mountpoint because it only exists when the
213421864bc5SMatthew Dillon * block device is mounted. However, we can use v_rdev.
213521864bc5SMatthew Dillon */
213621864bc5SMatthew Dillon if (vn_isdisk(vp, NULL))
213721864bc5SMatthew Dillon blksiz = vp->v_rdev->si_bsize_phys;
213821864bc5SMatthew Dillon else
213921864bc5SMatthew Dillon blksiz = DEV_BSIZE;
214021864bc5SMatthew Dillon
2141965b839fSSascha Wildner size = roundup2(ap->a_count, blksiz);
214221864bc5SMatthew Dillon
2143ad8b1a17SMatthew Dillon bp = getpbuf_kva(NULL);
214421864bc5SMatthew Dillon kva = (vm_offset_t)bp->b_data;
214521864bc5SMatthew Dillon
214621864bc5SMatthew Dillon /*
214721864bc5SMatthew Dillon * Map the pages to be read into the kva.
214821864bc5SMatthew Dillon */
2149b3f55d88SMatthew Dillon pmap_qenter_noinval(kva, ap->a_m, pcount);
215021864bc5SMatthew Dillon
215121864bc5SMatthew Dillon /* Build a minimal buffer header. */
215221864bc5SMatthew Dillon bp->b_cmd = BUF_CMD_READ;
2153b3f55d88SMatthew Dillon bp->b_flags |= B_KVABIO;
215421864bc5SMatthew Dillon bp->b_bcount = size;
215521864bc5SMatthew Dillon bp->b_resid = 0;
215677912481SMatthew Dillon bsetrunningbufspace(bp, size);
215721864bc5SMatthew Dillon
215821864bc5SMatthew Dillon bp->b_bio1.bio_offset = offset;
215921864bc5SMatthew Dillon bp->b_bio1.bio_done = devfs_spec_getpages_iodone;
216021864bc5SMatthew Dillon
216121864bc5SMatthew Dillon mycpu->gd_cnt.v_vnodein++;
216221864bc5SMatthew Dillon mycpu->gd_cnt.v_vnodepgsin += pcount;
216321864bc5SMatthew Dillon
216421864bc5SMatthew Dillon /* Do the input. */
216521864bc5SMatthew Dillon vn_strategy(ap->a_vp, &bp->b_bio1);
216621864bc5SMatthew Dillon
216721864bc5SMatthew Dillon crit_enter();
216821864bc5SMatthew Dillon
216921864bc5SMatthew Dillon /* We definitely need to be at splbio here. */
217021864bc5SMatthew Dillon while (bp->b_cmd != BUF_CMD_DONE)
217121864bc5SMatthew Dillon tsleep(bp, 0, "spread", 0);
217221864bc5SMatthew Dillon
217321864bc5SMatthew Dillon crit_exit();
217421864bc5SMatthew Dillon
217521864bc5SMatthew Dillon if (bp->b_flags & B_ERROR) {
217621864bc5SMatthew Dillon if (bp->b_error)
217721864bc5SMatthew Dillon error = bp->b_error;
217821864bc5SMatthew Dillon else
217921864bc5SMatthew Dillon error = EIO;
218021864bc5SMatthew Dillon }
218121864bc5SMatthew Dillon
218221864bc5SMatthew Dillon /*
218321864bc5SMatthew Dillon * If EOF is encountered we must zero-extend the result in order
218421864bc5SMatthew Dillon * to ensure that the page does not contain garabge. When no
218521864bc5SMatthew Dillon * error occurs, an early EOF is indicated if b_bcount got truncated.
218621864bc5SMatthew Dillon * b_resid is relative to b_bcount and should be 0, but some devices
218721864bc5SMatthew Dillon * might indicate an EOF with b_resid instead of truncating b_bcount.
218821864bc5SMatthew Dillon */
218921864bc5SMatthew Dillon nread = bp->b_bcount - bp->b_resid;
2190b3f55d88SMatthew Dillon if (nread < ap->a_count) {
2191b3f55d88SMatthew Dillon bkvasync(bp);
219221864bc5SMatthew Dillon bzero((caddr_t)kva + nread, ap->a_count - nread);
2193b3f55d88SMatthew Dillon }
2194b3f55d88SMatthew Dillon pmap_qremove_noinval(kva, pcount);
219521864bc5SMatthew Dillon
219621864bc5SMatthew Dillon gotreqpage = 0;
219721864bc5SMatthew Dillon for (i = 0, toff = 0; i < pcount; i++, toff = nextoff) {
219821864bc5SMatthew Dillon nextoff = toff + PAGE_SIZE;
219921864bc5SMatthew Dillon m = ap->a_m[i];
220021864bc5SMatthew Dillon
2201cb1cf930SMatthew Dillon /*
2202cb1cf930SMatthew Dillon * NOTE: vm_page_undirty/clear_dirty etc do not clear the
2203cb1cf930SMatthew Dillon * pmap modified bit. pmap modified bit should have
2204cb1cf930SMatthew Dillon * already been cleared.
2205cb1cf930SMatthew Dillon */
220621864bc5SMatthew Dillon if (nextoff <= nread) {
220721864bc5SMatthew Dillon m->valid = VM_PAGE_BITS_ALL;
220821864bc5SMatthew Dillon vm_page_undirty(m);
220921864bc5SMatthew Dillon } else if (toff < nread) {
221021864bc5SMatthew Dillon /*
221121864bc5SMatthew Dillon * Since this is a VM request, we have to supply the
2212cb1cf930SMatthew Dillon * unaligned offset to allow vm_page_set_valid()
221321864bc5SMatthew Dillon * to zero sub-DEV_BSIZE'd portions of the page.
221421864bc5SMatthew Dillon */
22151a54183bSMatthew Dillon vm_page_set_valid(m, 0, nread - toff);
22161a54183bSMatthew Dillon vm_page_clear_dirty_end_nonincl(m, 0, nread - toff);
221721864bc5SMatthew Dillon } else {
221821864bc5SMatthew Dillon m->valid = 0;
221921864bc5SMatthew Dillon vm_page_undirty(m);
222021864bc5SMatthew Dillon }
222121864bc5SMatthew Dillon
222221864bc5SMatthew Dillon if (i != ap->a_reqpage) {
222321864bc5SMatthew Dillon /*
222421864bc5SMatthew Dillon * Just in case someone was asking for this page we
222521864bc5SMatthew Dillon * now tell them that it is ok to use.
222621864bc5SMatthew Dillon */
222721864bc5SMatthew Dillon if (!error || (m->valid == VM_PAGE_BITS_ALL)) {
222821864bc5SMatthew Dillon if (m->valid) {
2229b12defdcSMatthew Dillon if (m->flags & PG_REFERENCED) {
223021864bc5SMatthew Dillon vm_page_activate(m);
223121864bc5SMatthew Dillon } else {
223221864bc5SMatthew Dillon vm_page_deactivate(m);
223321864bc5SMatthew Dillon }
223421864bc5SMatthew Dillon vm_page_wakeup(m);
223521864bc5SMatthew Dillon } else {
223621864bc5SMatthew Dillon vm_page_free(m);
223721864bc5SMatthew Dillon }
223821864bc5SMatthew Dillon } else {
223921864bc5SMatthew Dillon vm_page_free(m);
224021864bc5SMatthew Dillon }
224121864bc5SMatthew Dillon } else if (m->valid) {
224221864bc5SMatthew Dillon gotreqpage = 1;
224321864bc5SMatthew Dillon /*
224421864bc5SMatthew Dillon * Since this is a VM request, we need to make the
224521864bc5SMatthew Dillon * entire page presentable by zeroing invalid sections.
224621864bc5SMatthew Dillon */
224721864bc5SMatthew Dillon if (m->valid != VM_PAGE_BITS_ALL)
224821864bc5SMatthew Dillon vm_page_zero_invalid(m, FALSE);
224921864bc5SMatthew Dillon }
225021864bc5SMatthew Dillon }
225121864bc5SMatthew Dillon if (!gotreqpage) {
225221864bc5SMatthew Dillon m = ap->a_m[ap->a_reqpage];
225321864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_WARNING,
225421864bc5SMatthew Dillon "spec_getpages:(%s) I/O read failure: (error=%d) bp %p vp %p\n",
225521864bc5SMatthew Dillon devtoname(vp->v_rdev), error, bp, bp->b_vp);
225621864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_WARNING,
225721864bc5SMatthew Dillon " size: %d, resid: %d, a_count: %d, valid: 0x%x\n",
225821864bc5SMatthew Dillon size, bp->b_resid, ap->a_count, m->valid);
225921864bc5SMatthew Dillon devfs_debug(DEVFS_DEBUG_WARNING,
226021864bc5SMatthew Dillon " nread: %d, reqpage: %d, pindex: %lu, pcount: %d\n",
226121864bc5SMatthew Dillon nread, ap->a_reqpage, (u_long)m->pindex, pcount);
226221864bc5SMatthew Dillon /*
226321864bc5SMatthew Dillon * Free the buffer header back to the swap buffer pool.
226421864bc5SMatthew Dillon */
226521864bc5SMatthew Dillon relpbuf(bp, NULL);
226621864bc5SMatthew Dillon return VM_PAGER_ERROR;
226721864bc5SMatthew Dillon }
226821864bc5SMatthew Dillon /*
226921864bc5SMatthew Dillon * Free the buffer header back to the swap buffer pool.
227021864bc5SMatthew Dillon */
227121864bc5SMatthew Dillon relpbuf(bp, NULL);
227207dfa375SAlex Hornung if (DEVFS_NODE(ap->a_vp))
2273d489a79aSMatthew Dillon vfs_timestamp(&DEVFS_NODE(ap->a_vp)->mtime);
227421864bc5SMatthew Dillon return VM_PAGER_OK;
227521864bc5SMatthew Dillon }
227621864bc5SMatthew Dillon
227721864bc5SMatthew Dillon static __inline
227821864bc5SMatthew Dillon int
sequential_heuristic(struct uio * uio,struct file * fp)227921864bc5SMatthew Dillon sequential_heuristic(struct uio *uio, struct file *fp)
228021864bc5SMatthew Dillon {
228121864bc5SMatthew Dillon /*
228221864bc5SMatthew Dillon * Sequential heuristic - detect sequential operation
228321864bc5SMatthew Dillon */
228421864bc5SMatthew Dillon if ((uio->uio_offset == 0 && fp->f_seqcount > 0) ||
228521864bc5SMatthew Dillon uio->uio_offset == fp->f_nextoff) {
228621864bc5SMatthew Dillon /*
228721864bc5SMatthew Dillon * XXX we assume that the filesystem block size is
228821864bc5SMatthew Dillon * the default. Not true, but still gives us a pretty
228921864bc5SMatthew Dillon * good indicator of how sequential the read operations
229021864bc5SMatthew Dillon * are.
229121864bc5SMatthew Dillon */
2292898c91eeSMatthew Dillon int tmpseq = fp->f_seqcount;
2293898c91eeSMatthew Dillon
22944f048b1cSSascha Wildner tmpseq += howmany(uio->uio_resid, MAXBSIZE);
229521864bc5SMatthew Dillon if (tmpseq > IO_SEQMAX)
229621864bc5SMatthew Dillon tmpseq = IO_SEQMAX;
229721864bc5SMatthew Dillon fp->f_seqcount = tmpseq;
229821864bc5SMatthew Dillon return(fp->f_seqcount << IO_SEQSHIFT);
229921864bc5SMatthew Dillon }
230021864bc5SMatthew Dillon
230121864bc5SMatthew Dillon /*
230221864bc5SMatthew Dillon * Not sequential, quick draw-down of seqcount
230321864bc5SMatthew Dillon */
230421864bc5SMatthew Dillon if (fp->f_seqcount > 1)
230521864bc5SMatthew Dillon fp->f_seqcount = 1;
230621864bc5SMatthew Dillon else
230721864bc5SMatthew Dillon fp->f_seqcount = 0;
230821864bc5SMatthew Dillon return(0);
230921864bc5SMatthew Dillon }
2310