xref: /freebsd/sys/fs/devfs/devfs.h (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 2000
7  *	Poul-Henning Kamp.  All rights reserved.
8  * Copyright (c) 2002
9  *	Dima Dorfman.  All rights reserved.
10  *
11  * This code is derived from software donated to Berkeley by
12  * Jan-Simon Pendry.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)kernfs.h	8.6 (Berkeley) 3/29/95
36  * From: FreeBSD: src/sys/miscfs/kernfs/kernfs.h 1.14
37  *
38  * $FreeBSD$
39  */
40 
41 #ifndef _FS_DEVFS_DEVFS_H_
42 #define	_FS_DEVFS_DEVFS_H_
43 
44 #define	DEVFS_MAGIC	0xdb0a087a
45 
46 /*
47  * Identifiers.  The ruleset and rule numbers are 16-bit values.  The
48  * "rule ID" is a combination of the ruleset and rule number; it
49  * should be able to univocally describe a rule in the system.  In
50  * this implementation, the upper 16 bits of the rule ID is the
51  * ruleset number; the lower 16 bits, the rule number within the
52  * aforementioned ruleset.
53  */
54 typedef uint16_t devfs_rnum;
55 typedef uint16_t devfs_rsnum;
56 typedef uint32_t devfs_rid;
57 
58 /*
59  * Identifier manipulators.
60  */
61 #define	rid2rsn(rid)	((rid) >> 16)
62 #define	rid2rn(rid)	((rid) & 0xffff)
63 #define	mkrid(rsn, rn)	((rn) | ((rsn) << 16))
64 
65 /*
66  * Plain DEVFS rule.  This gets shared between kernel and userland
67  * verbatim, so it shouldn't contain any pointers or other kernel- or
68  * userland-specific values.
69  */
70 struct devfs_rule {
71 	uint32_t dr_magic;			/* Magic number. */
72 	devfs_rid dr_id;			/* Identifier. */
73 
74 	/*
75 	 * Conditions under which this rule should be applied.  These
76 	 * are ANDed together since OR can be simulated by using
77 	 * multiple rules.  dr_icond determines which of the other
78 	 * variables we should process.
79 	 */
80 	int	dr_icond;
81 #define	DRC_DSWFLAGS	0x001
82 #define	DRC_PATHPTRN	0x002
83 	int	dr_dswflags;			/* cdevsw flags to match. */
84 #define	DEVFS_MAXPTRNLEN	200
85 	char	dr_pathptrn[DEVFS_MAXPTRNLEN];	/* Pattern to match path. */
86 
87 	/*
88 	 * Things to change.  dr_iacts determines which of the other
89 	 * variables we should process.
90 	 */
91 	int	dr_iacts;
92 #define	DRA_BACTS	0x001
93 #define	DRA_UID		0x002
94 #define	DRA_GID		0x004
95 #define	DRA_MODE	0x008
96 #define	DRA_INCSET	0x010
97 	int	dr_bacts;			/* Boolean (on/off) action. */
98 #define	DRB_HIDE	0x001			/* Hide entry (DE_WHITEOUT). */
99 #define	DRB_UNHIDE	0x002			/* Unhide entry. */
100 	uid_t	dr_uid;
101 	gid_t	dr_gid;
102 	mode_t	dr_mode;
103 	devfs_rsnum dr_incset;			/* Included ruleset. */
104 };
105 
106 /*
107  * Rule-related ioctls.
108  */
109 #define	DEVFSIO_RADD		_IOWR('D', 0, struct devfs_rule)
110 #define	DEVFSIO_RDEL		_IOW('D', 1, devfs_rid)
111 #define	DEVFSIO_RAPPLY		_IOW('D', 2, struct devfs_rule)
112 #define	DEVFSIO_RAPPLYID	_IOW('D', 3, devfs_rid)
113 #define	DEVFSIO_RGETNEXT       	_IOWR('D', 4, struct devfs_rule)
114 
115 #define	DEVFSIO_SUSE		_IOW('D', 10, devfs_rsnum)
116 #define	DEVFSIO_SAPPLY		_IOW('D', 11, devfs_rsnum)
117 #define	DEVFSIO_SGETNEXT	_IOWR('D', 12, devfs_rsnum)
118 
119 /* XXX: DEVFSIO_RS_GET_INFO for refcount, active if any, etc. */
120 
121 #ifdef _KERNEL
122 
123 #ifdef MALLOC_DECLARE
124 MALLOC_DECLARE(M_DEVFS);
125 #endif
126 
127 #endif /* _KERNEL */
128 
129 struct componentname;
130 
131 TAILQ_HEAD(devfs_dlist_head, devfs_dirent);
132 
133 struct devfs_dirent {
134 	struct cdev_priv	*de_cdp;
135 	int			de_inode;
136 	int			de_flags;
137 #define	DE_WHITEOUT	0x01
138 #define	DE_DOT		0x02
139 #define	DE_DOTDOT	0x04
140 #define	DE_DOOMED	0x08
141 #define	DE_COVERED	0x10
142 #define	DE_USER		0x20
143 	int			de_holdcnt;
144 	struct dirent 		*de_dirent;
145 	TAILQ_ENTRY(devfs_dirent) de_list;
146 	struct devfs_dlist_head	de_dlist;
147 	struct devfs_dirent	*de_dir;
148 	int			de_links;
149 	mode_t			de_mode;
150 	uid_t			de_uid;
151 	gid_t			de_gid;
152 	struct label		*de_label;
153 	struct timespec 	de_atime;
154 	struct timespec 	de_mtime;
155 	struct timespec 	de_ctime;
156 	struct vnode 		*de_vnode;
157 	char 			*de_symlink;
158 	int			de_usecount;
159 };
160 
161 #include <sys/_lock.h>
162 #include <sys/_sx.h>
163 
164 struct devfs_mount {
165 	u_int			dm_idx;
166 	struct mount		*dm_mount;
167 	struct devfs_dirent	*dm_rootdir;
168 	unsigned		dm_generation;
169 	int			dm_holdcnt;
170 	struct sx		dm_lock;
171 	devfs_rsnum		dm_ruleset;
172 };
173 
174 #define DEVFS_ROOTINO 2
175 
176 #ifdef _KERNEL
177 
178 extern unsigned devfs_rule_depth;
179 
180 #define VFSTODEVFS(mp)	((struct devfs_mount *)((mp)->mnt_data))
181 
182 #define DEVFS_DE_HOLD(de)	((de)->de_holdcnt++)
183 #define DEVFS_DE_DROP(de)	(--(de)->de_holdcnt == 0)
184 
185 #define DEVFS_DMP_HOLD(dmp)	((dmp)->dm_holdcnt++)
186 #define DEVFS_DMP_DROP(dmp)	(--(dmp)->dm_holdcnt == 0)
187 
188 #define	DEVFS_DEL_NORECURSE	0x01
189 
190 void	devfs_rules_apply(struct devfs_mount *, struct devfs_dirent *);
191 void	devfs_rules_cleanup(struct devfs_mount *);
192 int	devfs_rules_ioctl(struct devfs_mount *, u_long, caddr_t,
193 	    struct thread *);
194 void	devfs_ruleset_set(devfs_rsnum rsnum, struct devfs_mount *dm);
195 void	devfs_ruleset_apply(struct devfs_mount *dm);
196 int	devfs_allocv(struct devfs_dirent *, struct mount *, int,
197 	    struct vnode **);
198 char	*devfs_fqpn(char *, struct devfs_mount *, struct devfs_dirent *,
199 	    struct componentname *);
200 void	devfs_delete(struct devfs_mount *, struct devfs_dirent *, int);
201 void	devfs_dirent_free(struct devfs_dirent *);
202 int	devfs_populate_needed(struct devfs_mount *dm);
203 void	devfs_populate(struct devfs_mount *);
204 void	devfs_cleanup(struct devfs_mount *);
205 void	devfs_unmount_final(struct devfs_mount *);
206 struct devfs_dirent	*devfs_newdirent(char *, int);
207 struct devfs_dirent	*devfs_parent_dirent(struct devfs_dirent *);
208 struct devfs_dirent	*devfs_vmkdir(struct devfs_mount *, char *, int,
209 			    struct devfs_dirent *, u_int);
210 struct devfs_dirent	*devfs_find(struct devfs_dirent *, const char *, int,
211 			    int);
212 
213 void	devfs_ctty_ref(struct vnode *);
214 void	devfs_ctty_unref(struct vnode *);
215 int	devfs_usecount(struct vnode *);
216 
217 #endif /* _KERNEL */
218 
219 #endif /* !_FS_DEVFS_DEVFS_H_ */
220