devfs.h revision 105209
1/*-
2 * Copyright (c) 1992, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * Copyright (c) 2000
5 *	Poul-Henning Kamp.  All rights reserved.
6 * Copyright (c) 2002
7 *	Dima Dorfman.  All rights reserved.
8 *
9 * This code is derived from software donated to Berkeley by
10 * Jan-Simon Pendry.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)kernfs.h	8.6 (Berkeley) 3/29/95
34 * From: FreeBSD: src/sys/miscfs/kernfs/kernfs.h 1.14
35 *
36 * $FreeBSD: head/sys/fs/devfs/devfs.h 105209 2002-10-16 07:16:47Z phk $
37 */
38
39#ifndef _FS_DEVFS_DEVFS_H_
40#define	_FS_DEVFS_DEVFS_H_
41
42#include "opt_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 */
54typedef uint16_t devfs_rnum;
55typedef uint16_t devfs_rsnum;
56typedef 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 */
70struct 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#define	DRC_MAJOR	0x004
84	int	dr_dswflags;			/* cdevsw flags to match. */
85#define	DEVFS_MAXPTRNLEN	200
86	char	dr_pathptrn[DEVFS_MAXPTRNLEN];	/* Pattern to match path. */
87	int	dr_major;			/* Device major number. */
88
89	/*
90	 * Things to change.  dr_iacts determines which of the other
91	 * variables we should process.
92	 */
93	int	dr_iacts;
94#define	DRA_BACTS	0x001
95#define	DRA_UID		0x002
96#define	DRA_GID		0x004
97#define	DRA_MODE	0x008
98#define	DRA_INCSET	0x010
99	int	dr_bacts;			/* Boolean (on/off) action. */
100#define	DRB_HIDE	0x001			/* Hide entry (DE_WHITEOUT). */
101#define	DRB_UNHIDE	0x002			/* Unhide entry. */
102	uid_t	dr_uid;
103	gid_t	dr_gid;
104	mode_t	dr_mode;
105	devfs_rsnum dr_incset;			/* Included ruleset. */
106};
107
108/*
109 * Rule-related ioctls.
110 */
111#define	DEVFSIO_RADD		_IOWR('D', 0, struct devfs_rule)
112#define	DEVFSIO_RDEL		_IOW('D', 1, devfs_rid)
113#define	DEVFSIO_RAPPLY		_IOW('D', 2, struct devfs_rule)
114#define	DEVFSIO_RAPPLYID	_IOW('D', 3, devfs_rid)
115#define	DEVFSIO_RGETNEXT       	_IOWR('D', 4, struct devfs_rule)
116
117#define	DEVFSIO_SUSE		_IOW('D', 10, devfs_rsnum)
118#define	DEVFSIO_SAPPLY		_IOW('D', 11, devfs_rsnum)
119#define	DEVFSIO_SGETNEXT	_IOWR('D', 12, devfs_rsnum)
120
121/* XXX: DEVFSIO_RS_GET_INFO for refcount, active if any, etc. */
122
123#ifdef _KERNEL
124
125/*
126 * These are default sizes for the DEVFS inode table and the overflow
127 * table.  If the default table overflows we allocate the overflow
128 * table, the size of which can also be set with a sysctl.  If the
129 * overflow table fills you're toast.
130 */
131#ifndef NDEVFSINO
132#define NDEVFSINO 1024
133#endif
134
135#ifndef NDEVFSOVERFLOW
136#define NDEVFSOVERFLOW 32768
137#endif
138
139/*
140 * This is the first "per mount" inode, these are used for directories
141 * and symlinks and the like.  Must be larger than the number of "true"
142 * device nodes and symlinks.  It is.
143 */
144#define DEVFSINOMOUNT	0x2000000
145
146#ifdef MALLOC_DECLARE
147MALLOC_DECLARE(M_DEVFS);
148#endif
149
150struct devfs_dirent {
151	int	de_inode;
152	int	de_flags;
153#define	DE_WHITEOUT	0x1
154#define	DE_DOT		0x2
155#define	DE_DOTDOT	0x4
156	struct dirent *de_dirent;
157	TAILQ_ENTRY(devfs_dirent) de_list;
158	TAILQ_HEAD(, devfs_dirent) de_dlist;
159	struct devfs_dirent *de_dir;
160	int	de_links;
161	mode_t	de_mode;
162	uid_t	de_uid;
163	gid_t	de_gid;
164	struct label	de_label;
165	struct timespec de_atime;
166	struct timespec de_mtime;
167	struct timespec de_ctime;
168	struct vnode *de_vnode;
169	char *	de_symlink;
170};
171
172struct devfs_mount {
173	struct vnode	*dm_root;	/* Root node */
174	struct devfs_dirent *dm_rootdir;
175	struct devfs_dirent *dm_basedir;
176	unsigned	dm_generation;
177	struct devfs_dirent *dm_dirent[NDEVFSINO];
178	struct devfs_dirent **dm_overflow;
179	int	dm_inode;
180	struct lock dm_lock;
181	devfs_rsnum dm_ruleset;
182};
183
184/*
185 * This is what we fill in dm_dirent[N] for a deleted entry.
186 */
187#define DE_DELETED ((struct devfs_dirent *)sizeof(struct devfs_dirent))
188
189#define VFSTODEVFS(mp)	((struct devfs_mount *)((mp)->mnt_data))
190
191void devfs_rules_apply(struct devfs_mount *dm, struct devfs_dirent *de);
192void devfs_rules_init(void);
193int devfs_rules_ioctl(struct mount *mp, int cmd, caddr_t data, struct thread *td);
194void devfs_rules_newmount(struct devfs_mount *dm, struct thread *td);
195int devfs_allocv (struct devfs_dirent *de, struct mount *mp, struct vnode **vpp, struct thread *td);
196dev_t *devfs_itod (int inode);
197struct devfs_dirent **devfs_itode (struct devfs_mount *dm, int inode);
198int devfs_populate (struct devfs_mount *dm);
199struct devfs_dirent *devfs_newdirent (char *name, int namelen);
200void devfs_purge (struct devfs_dirent *dd);
201struct devfs_dirent *devfs_vmkdir (char *name, int namelen, struct devfs_dirent *dotdot);
202
203#endif /* _KERNEL */
204
205#endif /* !_FS_DEVFS_DEVFS_H_ */
206