1/*	$NetBSD: specdev.h,v 1.53 2022/10/26 23:40:08 riastradh Exp $	*/
  2
  3/*-
  4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
  5 * All rights reserved.
  6 *
  7 * Redistribution and use in source and binary forms, with or without
  8 * modification, are permitted provided that the following conditions
  9 * are met:
 10 * 1. Redistributions of source code must retain the above copyright
 11 *    notice, this list of conditions and the following disclaimer.
 12 * 2. Redistributions in binary form must reproduce the above copyright
 13 *    notice, this list of conditions and the following disclaimer in the
 14 *    documentation and/or other materials provided with the distribution.
 15 *
 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26 * POSSIBILITY OF SUCH DAMAGE.
 27 */
 28
 29/*
 30 * Copyright (c) 1990, 1993
 31 *	The Regents of the University of California.  All rights reserved.
 32 *
 33 * Redistribution and use in source and binary forms, with or without
 34 * modification, are permitted provided that the following conditions
 35 * are met:
 36 * 1. Redistributions of source code must retain the above copyright
 37 *    notice, this list of conditions and the following disclaimer.
 38 * 2. Redistributions in binary form must reproduce the above copyright
 39 *    notice, this list of conditions and the following disclaimer in the
 40 *    documentation and/or other materials provided with the distribution.
 41 * 3. Neither the name of the University nor the names of its contributors
 42 *    may be used to endorse or promote products derived from this software
 43 *    without specific prior written permission.
 44 *
 45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 48 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 55 * SUCH DAMAGE.
 56 *
 57 *	@(#)specdev.h	8.6 (Berkeley) 5/21/95
 58 */
 59
 60#ifndef _MISCFS_SPECFS_SPECDEV_H_
 61#define _MISCFS_SPECFS_SPECDEV_H_
 62
 63#include <sys/mutex.h>
 64#include <sys/vnode.h>
 65
 66typedef struct specnode {
 67	vnode_t		*sn_next;
 68	struct specdev	*sn_dev;
 69	dev_t		sn_rdev;
 70	u_int		sn_opencnt;	/* # of opens, share of sd_opencnt */
 71	bool		sn_gone;
 72} specnode_t;
 73
 74typedef struct specdev {
 75	struct mount	*sd_mountpoint;
 76	struct lockf	*sd_lockf;
 77	vnode_t		*sd_bdevvp;
 78	u_int		sd_opencnt;	/* # of opens; close when ->0 */
 79	u_int		sd_refcnt;	/* # of specnodes referencing this */
 80	dev_t		sd_rdev;
 81	volatile u_int	sd_iocnt;	/* # bdev/cdev_* operations active */
 82	bool		sd_opened;	/* true if successfully opened */
 83	bool		sd_closing;	/* true when bdev/cdev_close ongoing */
 84} specdev_t;
 85
 86/*
 87 * Exported shorthand
 88 */
 89#define v_specnext	v_specnode->sn_next
 90#define v_rdev		v_specnode->sn_rdev
 91#define v_speclockf	v_specnode->sn_dev->sd_lockf
 92
 93/*
 94 * Special device management
 95 */
 96void	spec_node_init(vnode_t *, dev_t);
 97void	spec_node_destroy(vnode_t *);
 98int	spec_node_lookup_by_dev(enum vtype, dev_t, int, vnode_t **);
 99int	spec_node_lookup_by_mount(struct mount *, vnode_t **);
100struct mount *spec_node_getmountedfs(vnode_t *);
101void	spec_node_setmountedfs(vnode_t *, struct mount *);
102void	spec_node_revoke(vnode_t *);
103
104/*
105 * Prototypes for special file operations on vnodes.
106 */
107extern const struct vnodeopv_desc spec_vnodeop_opv_desc;
108extern	int (**spec_vnodeop_p)(void *);
109struct	nameidata;
110struct	componentname;
111struct	flock;
112struct	buf;
113struct	uio;
114
115int	spec_lookup(void *);
116int	spec_open(void *);
117int	spec_close(void *);
118int	spec_read(void *);
119int	spec_write(void *);
120int	spec_fdiscard(void *);
121int	spec_ioctl(void *);
122int	spec_poll(void *);
123int	spec_kqfilter(void *);
124int	spec_mmap(void *);
125int	spec_fsync(void *);
126#define	spec_seek	genfs_nullop		/* XXX should query device */
127int	spec_inactive(void *);
128int	spec_reclaim(void *);
129int	spec_bmap(void *);
130int	spec_strategy(void *);
131int	spec_print(void *);
132int	spec_pathconf(void *);
133int	spec_advlock(void *);
134
135/*
136 * This macro provides an initializer list for the fs-independent part
137 * of a filesystem's special file vnode ops descriptor table. We still
138 * need such a table in every filesystem, but we can at least avoid
139 * the cutpaste.
140 *
141 * This contains these ops:
142 *    parsepath lookup
143 *    create whiteout mknod open fallocate fdiscard ioctl poll kqfilter
144 *    revoke mmap seek remove link rename mkdir rmdir symlink readdir
145 *    readlink abortop bmap strategy pathconf advlock getpages putpages
146 *
147 * The filesystem should provide these ops that need to be its own:
148 *    access and accessx
149 *    getattr
150 *    setattr
151 *    fcntl
152 *    inactive
153 *    reclaim
154 *    lock
155 *    unlock
156 *    print (should probably also call spec_print)
157 *    islocked
158 *    bwrite (normally vn_bwrite)
159 *    openextattr
160 *    closeextattr
161 *    getextattr
162 *    setextattr
163 *    listextattr
164 *    deleteextattr
165 *    getacl
166 *    setacl
167 *    aclcheck
168 *
169 * The filesystem should also provide these ops that some filesystems
170 * do their own things with:
171 *    close
172 *    read
173 *    write
174 *    fsync
175 * In most cases "their own things" means adjust timestamps and call
176 * spec_foo. For fsync it varies, but should always also call spec_fsync.
177 *
178 * Note that because the op descriptor tables are unordered it does not
179 * matter where in the table this macro goes (except I think default
180 * still needs to be first...)
181 */
182#define GENFS_SPECOP_ENTRIES \
183	{ &vop_parsepath_desc, genfs_badop },		/* parsepath */	\
184	{ &vop_lookup_desc, spec_lookup },		/* lookup */	\
185	{ &vop_create_desc, genfs_badop },		/* create */	\
186	{ &vop_whiteout_desc, genfs_badop },		/* whiteout */	\
187	{ &vop_mknod_desc, genfs_badop },		/* mknod */	\
188	{ &vop_open_desc, spec_open },			/* open */	\
189	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */	\
190	{ &vop_fdiscard_desc, spec_fdiscard },		/* fdiscard */	\
191	{ &vop_ioctl_desc, spec_ioctl },		/* ioctl */	\
192	{ &vop_poll_desc, spec_poll },			/* poll */	\
193	{ &vop_kqfilter_desc, spec_kqfilter },		/* kqfilter */	\
194	{ &vop_revoke_desc, genfs_revoke },		/* revoke */	\
195	{ &vop_mmap_desc, spec_mmap },			/* mmap */	\
196	{ &vop_seek_desc, spec_seek },			/* seek */	\
197	{ &vop_remove_desc, genfs_badop },		/* remove */	\
198	{ &vop_link_desc, genfs_badop },		/* link */	\
199	{ &vop_rename_desc, genfs_badop },		/* rename */	\
200	{ &vop_mkdir_desc, genfs_badop },		/* mkdir */	\
201	{ &vop_rmdir_desc, genfs_badop },		/* rmdir */	\
202	{ &vop_symlink_desc, genfs_badop },		/* symlink */	\
203	{ &vop_readdir_desc, genfs_badop },		/* readdir */	\
204	{ &vop_readlink_desc, genfs_badop },		/* readlink */	\
205	{ &vop_abortop_desc, genfs_badop },		/* abortop */	\
206	{ &vop_bmap_desc, spec_bmap },			/* bmap */	\
207	{ &vop_strategy_desc, spec_strategy },		/* strategy */	\
208	{ &vop_pathconf_desc, spec_pathconf },		/* pathconf */	\
209	{ &vop_advlock_desc, spec_advlock },		/* advlock */	\
210	{ &vop_getpages_desc, genfs_getpages },		/* getpages */	\
211	{ &vop_putpages_desc, genfs_putpages }		/* putpages */
212
213
214bool	iskmemvp(struct vnode *);
215void	spec_init(void);
216
217#endif /* _MISCFS_SPECFS_SPECDEV_H_ */