xref: /freebsd/sys/fs/devfs/devfs_rule.c (revision 91a35e787094b1aca363bfd0ec8380d9a38ada53)
1a1dc2096SDima Dorfman /*-
2a1dc2096SDima Dorfman  * Copyright (c) 2002 Dima Dorfman.
3a1dc2096SDima Dorfman  * All rights reserved.
4a1dc2096SDima Dorfman  *
5a1dc2096SDima Dorfman  * Redistribution and use in source and binary forms, with or without
6a1dc2096SDima Dorfman  * modification, are permitted provided that the following conditions
7a1dc2096SDima Dorfman  * are met:
8a1dc2096SDima Dorfman  * 1. Redistributions of source code must retain the above copyright
9a1dc2096SDima Dorfman  *    notice, this list of conditions and the following disclaimer.
10a1dc2096SDima Dorfman  * 2. Redistributions in binary form must reproduce the above copyright
11a1dc2096SDima Dorfman  *    notice, this list of conditions and the following disclaimer in the
12a1dc2096SDima Dorfman  *    documentation and/or other materials provided with the distribution.
13a1dc2096SDima Dorfman  *
14a1dc2096SDima Dorfman  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15a1dc2096SDima Dorfman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16a1dc2096SDima Dorfman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17a1dc2096SDima Dorfman  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18a1dc2096SDima Dorfman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19a1dc2096SDima Dorfman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20a1dc2096SDima Dorfman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21a1dc2096SDima Dorfman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22a1dc2096SDima Dorfman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23a1dc2096SDima Dorfman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24a1dc2096SDima Dorfman  * SUCH DAMAGE.
25a1dc2096SDima Dorfman  *
26a1dc2096SDima Dorfman  * $FreeBSD$
27a1dc2096SDima Dorfman  */
28a1dc2096SDima Dorfman 
29a1dc2096SDima Dorfman /*
30a1dc2096SDima Dorfman  * DEVFS ruleset implementation.
31a1dc2096SDima Dorfman  *
32a1dc2096SDima Dorfman  * A note on terminology: To "run" a rule on a dirent is to take the
33a1dc2096SDima Dorfman  * prescribed action; to "apply" a rule is to check whether it matches
34a1dc2096SDima Dorfman  * a dirent and run if if it does.
35a1dc2096SDima Dorfman  *
36a1dc2096SDima Dorfman  * A note on locking: Only foreign entry points (non-static functions)
37a1dc2096SDima Dorfman  * should deal with locking.  Everything else assumes we already hold
38a1dc2096SDima Dorfman  * the required kind of lock.
39a1dc2096SDima Dorfman  *
40a1dc2096SDima Dorfman  * A note on namespace: devfs_rules_* are the non-static functions for
41a1dc2096SDima Dorfman  * the entire "ruleset" subsystem, devfs_rule_* are the static
42a1dc2096SDima Dorfman  * functions that operate on rules, and devfs_ruleset_* are the static
43a1dc2096SDima Dorfman  * functions that operate on rulesets.  The line between the last two
44a1dc2096SDima Dorfman  * isn't always clear, but the guideline is still useful.
45a1dc2096SDima Dorfman  *
46a1dc2096SDima Dorfman  * A note on "special" identifiers: Ruleset 0 is the NULL, or empty,
47a1dc2096SDima Dorfman  * ruleset; it cannot be deleted or changed in any way.  This may be
48a1dc2096SDima Dorfman  * assumed inside the code; e.g., a ruleset of 0 may be interpeted to
49a1dc2096SDima Dorfman  * mean "no ruleset".  The interpretation of rule 0 is
50a1dc2096SDima Dorfman  * command-dependent, but in no case is there a real rule with number
51a1dc2096SDima Dorfman  * 0.
52a1dc2096SDima Dorfman  *
53a1dc2096SDima Dorfman  * A note on errno codes: To make it easier for the userland to tell
54a1dc2096SDima Dorfman  * what went wrong, we sometimes use errno codes that are not entirely
55a1dc2096SDima Dorfman  * appropriate for the error but that would be less ambiguous than the
56a1dc2096SDima Dorfman  * appropriate "generic" code.  For example, when we can't find a
57a1dc2096SDima Dorfman  * ruleset, we return ESRCH instead of ENOENT (except in
58a1dc2096SDima Dorfman  * DEVFSIO_{R,S}GETNEXT, where a nonexistent ruleset means "end of
59a1dc2096SDima Dorfman  * list", and the userland expects ENOENT to be this indicator); this
60a1dc2096SDima Dorfman  * way, when an operation fails, it's clear that what couldn't be
61a1dc2096SDima Dorfman  * found is a ruleset and not a rule (well, it's clear to those who
62a1dc2096SDima Dorfman  * know the convention).
63a1dc2096SDima Dorfman  */
64a1dc2096SDima Dorfman 
65a1dc2096SDima Dorfman #include <sys/param.h>
66a1dc2096SDima Dorfman #include <sys/systm.h>
67a1dc2096SDima Dorfman #include <sys/conf.h>
68a1dc2096SDima Dorfman #include <sys/kernel.h>
69a1dc2096SDima Dorfman #include <sys/malloc.h>
70acd3428bSRobert Watson #include <sys/priv.h>
71a1dc2096SDima Dorfman #include <sys/dirent.h>
72a1dc2096SDima Dorfman #include <sys/ioccom.h>
73e606a3c6SPoul-Henning Kamp #include <sys/lock.h>
746556102dSPoul-Henning Kamp #include <sys/sx.h>
75a1dc2096SDima Dorfman 
76a1dc2096SDima Dorfman #include <fs/devfs/devfs.h>
77e606a3c6SPoul-Henning Kamp #include <fs/devfs/devfs_int.h>
78a1dc2096SDima Dorfman 
79a1dc2096SDima Dorfman /*
80a1dc2096SDima Dorfman  * Kernel version of devfs_rule.
81a1dc2096SDima Dorfman  */
82a1dc2096SDima Dorfman struct devfs_krule {
83e515ee78SPoul-Henning Kamp 	TAILQ_ENTRY(devfs_krule)	dk_list;
84a1dc2096SDima Dorfman 	struct devfs_ruleset		*dk_ruleset;
85a1dc2096SDima Dorfman 	struct devfs_rule		dk_rule;
86a1dc2096SDima Dorfman };
87a1dc2096SDima Dorfman 
88e515ee78SPoul-Henning Kamp TAILQ_HEAD(rulehead, devfs_krule);
89e515ee78SPoul-Henning Kamp static MALLOC_DEFINE(M_DEVFSRULE, "DEVFS_RULE", "DEVFS rule storage");
90e515ee78SPoul-Henning Kamp 
91a1dc2096SDima Dorfman /*
92a1dc2096SDima Dorfman  * Structure to describe a ruleset.
93a1dc2096SDima Dorfman  */
94a1dc2096SDima Dorfman struct devfs_ruleset {
95e515ee78SPoul-Henning Kamp 	TAILQ_ENTRY(devfs_ruleset)	ds_list;
96e515ee78SPoul-Henning Kamp 	struct rulehead			ds_rules;
97a1dc2096SDima Dorfman 	devfs_rsnum			ds_number;
98a1dc2096SDima Dorfman 	int				ds_refcount;
99a1dc2096SDima Dorfman };
100a1dc2096SDima Dorfman 
101a1dc2096SDima Dorfman static devfs_rid devfs_rid_input(devfs_rid rid, struct devfs_mount *dm);
102a1dc2096SDima Dorfman 
103a1dc2096SDima Dorfman static void devfs_rule_applyde_recursive(struct devfs_krule *dk,
104a1dc2096SDima Dorfman 		struct devfs_dirent *de);
105a1dc2096SDima Dorfman static void devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm);
106a1dc2096SDima Dorfman static int  devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnp);
107a1dc2096SDima Dorfman static struct devfs_krule *devfs_rule_byid(devfs_rid rid);
108e515ee78SPoul-Henning Kamp static int  devfs_rule_delete(struct devfs_krule *dkp);
10989c9c53dSPoul-Henning Kamp static struct cdev *devfs_rule_getdev(struct devfs_dirent *de);
110a1dc2096SDima Dorfman static int  devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm);
111a1dc2096SDima Dorfman static int  devfs_rule_insert(struct devfs_rule *dr);
112a1dc2096SDima Dorfman static int  devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de);
113a1dc2096SDima Dorfman static int  devfs_rule_matchpath(struct devfs_krule *dk,
114a1dc2096SDima Dorfman 		struct devfs_dirent *de);
1155e080af4SPoul-Henning Kamp static void devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de, unsigned depth);
116a1dc2096SDima Dorfman 
117a1dc2096SDima Dorfman static void devfs_ruleset_applyde(struct devfs_ruleset *ds,
1185e080af4SPoul-Henning Kamp 		struct devfs_dirent *de, unsigned depth);
119a1dc2096SDima Dorfman static void devfs_ruleset_applydm(struct devfs_ruleset *ds,
120a1dc2096SDima Dorfman 		struct devfs_mount *dm);
121a1dc2096SDima Dorfman static struct devfs_ruleset *devfs_ruleset_bynum(devfs_rsnum rsnum);
122a1dc2096SDima Dorfman static struct devfs_ruleset *devfs_ruleset_create(devfs_rsnum rsnum);
123e515ee78SPoul-Henning Kamp static void devfs_ruleset_reap(struct devfs_ruleset *dsp);
124a1dc2096SDima Dorfman static int  devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm);
125a1dc2096SDima Dorfman 
1266556102dSPoul-Henning Kamp static struct sx sx_rules;
127e515ee78SPoul-Henning Kamp SX_SYSINIT(sx_rules, &sx_rules, "DEVFS ruleset lock");
128e515ee78SPoul-Henning Kamp 
129e515ee78SPoul-Henning Kamp static TAILQ_HEAD(, devfs_ruleset) devfs_rulesets =
130e515ee78SPoul-Henning Kamp     TAILQ_HEAD_INITIALIZER(devfs_rulesets);
131a1dc2096SDima Dorfman 
132a1dc2096SDima Dorfman /*
1335e080af4SPoul-Henning Kamp  * Called to apply the proper rules for 'de' before it can be
134a1dc2096SDima Dorfman  * exposed to the userland.  This should be called with an exclusive
135a1dc2096SDima Dorfman  * lock on dm in case we need to run anything.
136a1dc2096SDima Dorfman  */
137a1dc2096SDima Dorfman void
138a1dc2096SDima Dorfman devfs_rules_apply(struct devfs_mount *dm, struct devfs_dirent *de)
139a1dc2096SDima Dorfman {
140a1dc2096SDima Dorfman 	struct devfs_ruleset *ds;
141a1dc2096SDima Dorfman 
142e515ee78SPoul-Henning Kamp 	if (dm->dm_ruleset == 0)
143e515ee78SPoul-Henning Kamp 		return;
1446556102dSPoul-Henning Kamp 	sx_slock(&sx_rules);
145a1dc2096SDima Dorfman 	ds = devfs_ruleset_bynum(dm->dm_ruleset);
146a1dc2096SDima Dorfman 	KASSERT(ds != NULL, ("mount-point has NULL ruleset"));
1475e080af4SPoul-Henning Kamp 	devfs_ruleset_applyde(ds, de, devfs_rule_depth);
1486556102dSPoul-Henning Kamp 	sx_sunlock(&sx_rules);
149a1dc2096SDima Dorfman }
150a1dc2096SDima Dorfman 
151a1dc2096SDima Dorfman /*
152a1dc2096SDima Dorfman  * Rule subsystem ioctl hook.
153a1dc2096SDima Dorfman  */
154a1dc2096SDima Dorfman int
155ab32e952SPoul-Henning Kamp devfs_rules_ioctl(struct devfs_mount *dm, u_long cmd, caddr_t data, struct thread *td)
156a1dc2096SDima Dorfman {
157a1dc2096SDima Dorfman 	struct devfs_ruleset *ds;
158a1dc2096SDima Dorfman 	struct devfs_krule *dk;
159a1dc2096SDima Dorfman 	struct devfs_rule *dr;
160a1dc2096SDima Dorfman 	devfs_rsnum rsnum;
161a1dc2096SDima Dorfman 	devfs_rnum rnum;
162a1dc2096SDima Dorfman 	devfs_rid rid;
163a1dc2096SDima Dorfman 	int error;
164a1dc2096SDima Dorfman 
165e606a3c6SPoul-Henning Kamp 	sx_assert(&dm->dm_lock, SX_XLOCKED);
166e606a3c6SPoul-Henning Kamp 
167a1dc2096SDima Dorfman 	/*
168acd3428bSRobert Watson 	 * XXX: This returns an error regardless of whether we actually
169acd3428bSRobert Watson 	 * support the cmd or not.
170acd3428bSRobert Watson 	 *
171acd3428bSRobert Watson 	 * We could make this privileges finer grained if desired.
172a1dc2096SDima Dorfman 	 */
173acd3428bSRobert Watson 	error = priv_check(td, PRIV_DEVFS_RULE);
174acd3428bSRobert Watson 	if (error)
175a1dc2096SDima Dorfman 		return (error);
176a1dc2096SDima Dorfman 
1776556102dSPoul-Henning Kamp 	sx_xlock(&sx_rules);
178e606a3c6SPoul-Henning Kamp 
179a1dc2096SDima Dorfman 	switch (cmd) {
180a1dc2096SDima Dorfman 	case DEVFSIO_RADD:
181a1dc2096SDima Dorfman 		dr = (struct devfs_rule *)data;
182a1dc2096SDima Dorfman 		error = devfs_rule_input(dr, dm);
183a1dc2096SDima Dorfman 		if (error != 0)
184e515ee78SPoul-Henning Kamp 			break;
185a1dc2096SDima Dorfman 		dk = devfs_rule_byid(dr->dr_id);
186a1dc2096SDima Dorfman 		if (dk != NULL) {
187a1dc2096SDima Dorfman 			error = EEXIST;
188e515ee78SPoul-Henning Kamp 			break;
189a1dc2096SDima Dorfman 		}
1908f0d99d7SRobert Watson 		if (rid2rsn(dr->dr_id) == 0) {
1918f0d99d7SRobert Watson 			error = EIO;
1928f0d99d7SRobert Watson 			break;
1938f0d99d7SRobert Watson 		}
194a1dc2096SDima Dorfman 		error = devfs_rule_insert(dr);
195a1dc2096SDima Dorfman 		break;
196a1dc2096SDima Dorfman 	case DEVFSIO_RAPPLY:
197a1dc2096SDima Dorfman 		dr = (struct devfs_rule *)data;
198a1dc2096SDima Dorfman 		error = devfs_rule_input(dr, dm);
199a1dc2096SDima Dorfman 		if (error != 0)
200e515ee78SPoul-Henning Kamp 			break;
201a1dc2096SDima Dorfman 
202a1dc2096SDima Dorfman 		/*
203a1dc2096SDima Dorfman 		 * This is one of many possible hackish
204a1dc2096SDima Dorfman 		 * implementations.  The primary contender is an
205a1dc2096SDima Dorfman 		 * implementation where the rule we read in is
206a1dc2096SDima Dorfman 		 * temporarily inserted into some ruleset, perhaps
207a1dc2096SDima Dorfman 		 * with a hypothetical DRO_NOAUTO flag so that it
208a1dc2096SDima Dorfman 		 * doesn't get used where it isn't intended, and
209a1dc2096SDima Dorfman 		 * applied in the normal way.  This can be done in the
210a1dc2096SDima Dorfman 		 * userland (DEVFSIO_ADD, DEVFSIO_APPLYID,
211a1dc2096SDima Dorfman 		 * DEVFSIO_DEL) or in the kernel; either way it breaks
212a1dc2096SDima Dorfman 		 * some corner case assumptions in other parts of the
213a1dc2096SDima Dorfman 		 * code (not that this implementation doesn't do
214a1dc2096SDima Dorfman 		 * that).
215a1dc2096SDima Dorfman 		 */
216a1dc2096SDima Dorfman 		if (dr->dr_iacts & DRA_INCSET &&
217a1dc2096SDima Dorfman 		    devfs_ruleset_bynum(dr->dr_incset) == NULL) {
218a1dc2096SDima Dorfman 			error = ESRCH;
219e515ee78SPoul-Henning Kamp 			break;
220a1dc2096SDima Dorfman 		}
221a163d034SWarner Losh 		dk = malloc(sizeof(*dk), M_TEMP, M_WAITOK | M_ZERO);
222a1dc2096SDima Dorfman 		memcpy(&dk->dk_rule, dr, sizeof(*dr));
223a1dc2096SDima Dorfman 		devfs_rule_applydm(dk, dm);
224a1dc2096SDima Dorfman 		free(dk, M_TEMP);
225a1dc2096SDima Dorfman 		break;
226a1dc2096SDima Dorfman 	case DEVFSIO_RAPPLYID:
227a1dc2096SDima Dorfman 		rid = *(devfs_rid *)data;
228a1dc2096SDima Dorfman 		rid = devfs_rid_input(rid, dm);
229a1dc2096SDima Dorfman 		dk = devfs_rule_byid(rid);
230a1dc2096SDima Dorfman 		if (dk == NULL) {
231a1dc2096SDima Dorfman 			error = ENOENT;
232e515ee78SPoul-Henning Kamp 			break;
233a1dc2096SDima Dorfman 		}
234a1dc2096SDima Dorfman 		devfs_rule_applydm(dk, dm);
235a1dc2096SDima Dorfman 		break;
236a1dc2096SDima Dorfman 	case DEVFSIO_RDEL:
237a1dc2096SDima Dorfman 		rid = *(devfs_rid *)data;
238a1dc2096SDima Dorfman 		rid = devfs_rid_input(rid, dm);
239a1dc2096SDima Dorfman 		dk = devfs_rule_byid(rid);
240a1dc2096SDima Dorfman 		if (dk == NULL) {
241a1dc2096SDima Dorfman 			error = ENOENT;
242e515ee78SPoul-Henning Kamp 			break;
243a1dc2096SDima Dorfman 		}
244a1dc2096SDima Dorfman 		ds = dk->dk_ruleset;
245e515ee78SPoul-Henning Kamp 		error = devfs_rule_delete(dk);
246a1dc2096SDima Dorfman 		break;
247a1dc2096SDima Dorfman 	case DEVFSIO_RGETNEXT:
248a1dc2096SDima Dorfman 		dr = (struct devfs_rule *)data;
249a1dc2096SDima Dorfman 		error = devfs_rule_input(dr, dm);
250a1dc2096SDima Dorfman 		if (error != 0)
251e515ee78SPoul-Henning Kamp 			break;
252a1dc2096SDima Dorfman 		/*
253a1dc2096SDima Dorfman 		 * We can't use devfs_rule_byid() here since that
254a1dc2096SDima Dorfman 		 * requires the rule specified to exist, but we want
255a1dc2096SDima Dorfman 		 * getnext(N) to work whether there is a rule N or not
256a1dc2096SDima Dorfman 		 * (specifically, getnext(0) must work, but we should
257a1dc2096SDima Dorfman 		 * never have a rule 0 since the add command
258a1dc2096SDima Dorfman 		 * interprets 0 to mean "auto-number").
259a1dc2096SDima Dorfman 		 */
260a1dc2096SDima Dorfman 		ds = devfs_ruleset_bynum(rid2rsn(dr->dr_id));
261a1dc2096SDima Dorfman 		if (ds == NULL) {
262a1dc2096SDima Dorfman 			error = ENOENT;
263e515ee78SPoul-Henning Kamp 			break;
264a1dc2096SDima Dorfman 		}
265a1dc2096SDima Dorfman 		rnum = rid2rn(dr->dr_id);
266e515ee78SPoul-Henning Kamp 		TAILQ_FOREACH(dk, &ds->ds_rules, dk_list) {
267a1dc2096SDima Dorfman 			if (rid2rn(dk->dk_rule.dr_id) > rnum)
268a1dc2096SDima Dorfman 				break;
269a1dc2096SDima Dorfman 		}
270a1dc2096SDima Dorfman 		if (dk == NULL) {
271a1dc2096SDima Dorfman 			error = ENOENT;
272e515ee78SPoul-Henning Kamp 			break;
273a1dc2096SDima Dorfman 		}
274a1dc2096SDima Dorfman 		memcpy(dr, &dk->dk_rule, sizeof(*dr));
275a1dc2096SDima Dorfman 		break;
276a1dc2096SDima Dorfman 	case DEVFSIO_SUSE:
277a1dc2096SDima Dorfman 		rsnum = *(devfs_rsnum *)data;
278a1dc2096SDima Dorfman 		error = devfs_ruleset_use(rsnum, dm);
279a1dc2096SDima Dorfman 		break;
280a1dc2096SDima Dorfman 	case DEVFSIO_SAPPLY:
281a1dc2096SDima Dorfman 		rsnum = *(devfs_rsnum *)data;
282a1dc2096SDima Dorfman 		rsnum = rid2rsn(devfs_rid_input(mkrid(rsnum, 0), dm));
283a1dc2096SDima Dorfman 		ds = devfs_ruleset_bynum(rsnum);
284a1dc2096SDima Dorfman 		if (ds == NULL) {
285a1dc2096SDima Dorfman 			error = ESRCH;
286e515ee78SPoul-Henning Kamp 			break;
287a1dc2096SDima Dorfman 		}
288a1dc2096SDima Dorfman 		devfs_ruleset_applydm(ds, dm);
289a1dc2096SDima Dorfman 		break;
290a1dc2096SDima Dorfman 	case DEVFSIO_SGETNEXT:
291a1dc2096SDima Dorfman 		rsnum = *(devfs_rsnum *)data;
292e515ee78SPoul-Henning Kamp 		TAILQ_FOREACH(ds, &devfs_rulesets, ds_list) {
293a1dc2096SDima Dorfman 			if (ds->ds_number > rsnum)
294a1dc2096SDima Dorfman 				break;
295a1dc2096SDima Dorfman 		}
296e515ee78SPoul-Henning Kamp 		if (ds == NULL) {
297a1dc2096SDima Dorfman 			error = ENOENT;
298e515ee78SPoul-Henning Kamp 			break;
299a1dc2096SDima Dorfman 		}
300e515ee78SPoul-Henning Kamp 		*(devfs_rsnum *)data = ds->ds_number;
301a1dc2096SDima Dorfman 		break;
302a1dc2096SDima Dorfman 	default:
303a1dc2096SDima Dorfman 		error = ENOIOCTL;
304a1dc2096SDima Dorfman 		break;
305a1dc2096SDima Dorfman 	}
306a1dc2096SDima Dorfman 
3076556102dSPoul-Henning Kamp 	sx_xunlock(&sx_rules);
308a1dc2096SDima Dorfman 	return (error);
309a1dc2096SDima Dorfman }
310a1dc2096SDima Dorfman 
311a1dc2096SDima Dorfman /*
312a1dc2096SDima Dorfman  * Adjust the rule identifier to use the ruleset of dm if one isn't
313a1dc2096SDima Dorfman  * explicitly specified.
314a1dc2096SDima Dorfman  *
315a1dc2096SDima Dorfman  * Note that after this operation, rid2rsn(rid) might still be 0, and
316a1dc2096SDima Dorfman  * that's okay; ruleset 0 is a valid ruleset, but when it's read in
317a1dc2096SDima Dorfman  * from the userland, it means "current ruleset for this mount-point".
318a1dc2096SDima Dorfman  */
319a1dc2096SDima Dorfman static devfs_rid
320a1dc2096SDima Dorfman devfs_rid_input(devfs_rid rid, struct devfs_mount *dm)
321a1dc2096SDima Dorfman {
322a1dc2096SDima Dorfman 
323a1dc2096SDima Dorfman 	if (rid2rsn(rid) == 0)
324a1dc2096SDima Dorfman 		return (mkrid(dm->dm_ruleset, rid2rn(rid)));
325a1dc2096SDima Dorfman 	else
326a1dc2096SDima Dorfman 		return (rid);
327a1dc2096SDima Dorfman }
328a1dc2096SDima Dorfman 
329a1dc2096SDima Dorfman /*
330a1dc2096SDima Dorfman  * Apply dk to de and everything under de.
331a1dc2096SDima Dorfman  *
332a1dc2096SDima Dorfman  * XXX: This method needs a function call for every nested
333a1dc2096SDima Dorfman  * subdirectory in a devfs mount.  If we plan to have many of these,
334a1dc2096SDima Dorfman  * we might eventually run out of kernel stack space.
335e606a3c6SPoul-Henning Kamp  * XXX: a linear search could be done through the cdev list instead.
336a1dc2096SDima Dorfman  */
337a1dc2096SDima Dorfman static void
338a1dc2096SDima Dorfman devfs_rule_applyde_recursive(struct devfs_krule *dk, struct devfs_dirent *de)
339a1dc2096SDima Dorfman {
340a1dc2096SDima Dorfman 	struct devfs_dirent *de2;
341a1dc2096SDima Dorfman 
3425e080af4SPoul-Henning Kamp 	TAILQ_FOREACH(de2, &de->de_dlist, de_list)
343a1dc2096SDima Dorfman 		devfs_rule_applyde_recursive(dk, de2);
3445e080af4SPoul-Henning Kamp 	devfs_rule_run(dk, de, devfs_rule_depth);
345a1dc2096SDima Dorfman }
346a1dc2096SDima Dorfman 
347a1dc2096SDima Dorfman /*
348a1dc2096SDima Dorfman  * Apply dk to all entires in dm.
349a1dc2096SDima Dorfman  */
350a1dc2096SDima Dorfman static void
351a1dc2096SDima Dorfman devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm)
352a1dc2096SDima Dorfman {
353a1dc2096SDima Dorfman 
354d785dfefSPoul-Henning Kamp 	devfs_rule_applyde_recursive(dk, dm->dm_rootdir);
355a1dc2096SDima Dorfman }
356a1dc2096SDima Dorfman 
357a1dc2096SDima Dorfman /*
358a1dc2096SDima Dorfman  * Automatically select a number for a new rule in ds, and write the
359a1dc2096SDima Dorfman  * result into rnump.
360a1dc2096SDima Dorfman  */
361a1dc2096SDima Dorfman static int
362a1dc2096SDima Dorfman devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnump)
363a1dc2096SDima Dorfman {
364a1dc2096SDima Dorfman 	struct devfs_krule *dk;
365a1dc2096SDima Dorfman 
366a1dc2096SDima Dorfman 	/* Find the last rule. */
367e515ee78SPoul-Henning Kamp 	dk = TAILQ_LAST(&ds->ds_rules, rulehead);
368a1dc2096SDima Dorfman 	if (dk == NULL)
369a1dc2096SDima Dorfman 		*rnump = 100;
370a1dc2096SDima Dorfman 	else {
371a1dc2096SDima Dorfman 		*rnump = rid2rn(dk->dk_rule.dr_id) + 100;
372a1dc2096SDima Dorfman 		/* Detect overflow. */
373a1dc2096SDima Dorfman 		if (*rnump < rid2rn(dk->dk_rule.dr_id))
374a1dc2096SDima Dorfman 			return (ERANGE);
375a1dc2096SDima Dorfman 	}
376a1dc2096SDima Dorfman 	KASSERT(devfs_rule_byid(mkrid(ds->ds_number, *rnump)) == NULL,
377a1dc2096SDima Dorfman 	    ("autonumbering resulted in an already existing rule"));
378a1dc2096SDima Dorfman 	return (0);
379a1dc2096SDima Dorfman }
380a1dc2096SDima Dorfman 
381a1dc2096SDima Dorfman /*
382a1dc2096SDima Dorfman  * Find a krule by id.
383a1dc2096SDima Dorfman  */
384a1dc2096SDima Dorfman static struct devfs_krule *
385a1dc2096SDima Dorfman devfs_rule_byid(devfs_rid rid)
386a1dc2096SDima Dorfman {
387a1dc2096SDima Dorfman 	struct devfs_ruleset *ds;
388a1dc2096SDima Dorfman 	struct devfs_krule *dk;
389a1dc2096SDima Dorfman 	devfs_rnum rn;
390a1dc2096SDima Dorfman 
391a1dc2096SDima Dorfman 	rn = rid2rn(rid);
392a1dc2096SDima Dorfman 	ds = devfs_ruleset_bynum(rid2rsn(rid));
393a1dc2096SDima Dorfman 	if (ds == NULL)
394a1dc2096SDima Dorfman 		return (NULL);
395e515ee78SPoul-Henning Kamp 	TAILQ_FOREACH(dk, &ds->ds_rules, dk_list) {
396a1dc2096SDima Dorfman 		if (rid2rn(dk->dk_rule.dr_id) == rn)
397a1dc2096SDima Dorfman 			return (dk);
398a1dc2096SDima Dorfman 		else if (rid2rn(dk->dk_rule.dr_id) > rn)
399a1dc2096SDima Dorfman 			break;
400a1dc2096SDima Dorfman 	}
401a1dc2096SDima Dorfman 	return (NULL);
402a1dc2096SDima Dorfman }
403a1dc2096SDima Dorfman 
404a1dc2096SDima Dorfman /*
405a1dc2096SDima Dorfman  * Remove dkp from any lists it may be on and remove memory associated
406a1dc2096SDima Dorfman  * with it.
407a1dc2096SDima Dorfman  */
408a1dc2096SDima Dorfman static int
409e515ee78SPoul-Henning Kamp devfs_rule_delete(struct devfs_krule *dk)
410a1dc2096SDima Dorfman {
411a1dc2096SDima Dorfman 	struct devfs_ruleset *ds;
412a1dc2096SDima Dorfman 
413a1dc2096SDima Dorfman 	if (dk->dk_rule.dr_iacts & DRA_INCSET) {
414a1dc2096SDima Dorfman 		ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset);
415a1dc2096SDima Dorfman 		KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset"));
416a1dc2096SDima Dorfman 		--ds->ds_refcount;
417e515ee78SPoul-Henning Kamp 		devfs_ruleset_reap(ds);
418a1dc2096SDima Dorfman 	}
419e515ee78SPoul-Henning Kamp 	ds = dk->dk_ruleset;
420e515ee78SPoul-Henning Kamp 	TAILQ_REMOVE(&ds->ds_rules, dk, dk_list);
421e515ee78SPoul-Henning Kamp 	devfs_ruleset_reap(ds);
422e515ee78SPoul-Henning Kamp 	free(dk, M_DEVFSRULE);
423a1dc2096SDima Dorfman 	return (0);
424a1dc2096SDima Dorfman }
425a1dc2096SDima Dorfman 
426a1dc2096SDima Dorfman /*
42789c9c53dSPoul-Henning Kamp  * Get a struct cdev *corresponding to de so we can try to match rules based
42889c9c53dSPoul-Henning Kamp  * on it.  If this routine returns NULL, there is no struct cdev *associated
429a1dc2096SDima Dorfman  * with the dirent (symlinks and directories don't have dev_ts), and
430a1dc2096SDima Dorfman  * the caller should assume that any critera dependent on a dev_t
431a1dc2096SDima Dorfman  * don't match.
432a1dc2096SDima Dorfman  */
43389c9c53dSPoul-Henning Kamp static struct cdev *
434a1dc2096SDima Dorfman devfs_rule_getdev(struct devfs_dirent *de)
435a1dc2096SDima Dorfman {
436a1dc2096SDima Dorfman 
437e606a3c6SPoul-Henning Kamp 	if (de->de_cdp == NULL)
438e606a3c6SPoul-Henning Kamp 		return (NULL);
439e606a3c6SPoul-Henning Kamp 	if (de->de_cdp->cdp_flags & CDP_ACTIVE)
440e606a3c6SPoul-Henning Kamp 		return (&de->de_cdp->cdp_c);
441a1dc2096SDima Dorfman 	else
442e606a3c6SPoul-Henning Kamp 		return (NULL);
443a1dc2096SDima Dorfman }
444a1dc2096SDima Dorfman 
445a1dc2096SDima Dorfman /*
446a1dc2096SDima Dorfman  * Do what we need to do to a rule that we just loaded from the
447a1dc2096SDima Dorfman  * userland.  In particular, we need to check the magic, and adjust
448a1dc2096SDima Dorfman  * the ruleset appropriate if desired.
449a1dc2096SDima Dorfman  */
450a1dc2096SDima Dorfman static int
451a1dc2096SDima Dorfman devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm)
452a1dc2096SDima Dorfman {
453a1dc2096SDima Dorfman 
454a1dc2096SDima Dorfman 	if (dr->dr_magic != DEVFS_MAGIC)
455a1dc2096SDima Dorfman 		return (ERPCMISMATCH);
456a1dc2096SDima Dorfman 	dr->dr_id = devfs_rid_input(dr->dr_id, dm);
457a1dc2096SDima Dorfman 	return (0);
458a1dc2096SDima Dorfman }
459a1dc2096SDima Dorfman 
460a1dc2096SDima Dorfman /*
461a1dc2096SDima Dorfman  * Import dr into the appropriate place in the kernel (i.e., make a
462a1dc2096SDima Dorfman  * krule).  The value of dr is copied, so the pointer may be destroyed
463a1dc2096SDima Dorfman  * after this call completes.
464a1dc2096SDima Dorfman  */
465a1dc2096SDima Dorfman static int
466a1dc2096SDima Dorfman devfs_rule_insert(struct devfs_rule *dr)
467a1dc2096SDima Dorfman {
468a1dc2096SDima Dorfman 	struct devfs_ruleset *ds, *dsi;
469e515ee78SPoul-Henning Kamp 	struct devfs_krule *k1;
470a1dc2096SDima Dorfman 	struct devfs_krule *dk;
471a1dc2096SDima Dorfman 	devfs_rsnum rsnum;
472a1dc2096SDima Dorfman 	devfs_rnum dkrn;
473a1dc2096SDima Dorfman 	int error;
474a1dc2096SDima Dorfman 
475a1dc2096SDima Dorfman 	/*
476a1dc2096SDima Dorfman 	 * This stuff seems out of place here, but we want to do it as
477a1dc2096SDima Dorfman 	 * soon as possible so that if it fails, we don't have to roll
478a1dc2096SDima Dorfman 	 * back any changes we already made (e.g., ruleset creation).
479a1dc2096SDima Dorfman 	 */
480a1dc2096SDima Dorfman 	if (dr->dr_iacts & DRA_INCSET) {
481a1dc2096SDima Dorfman 		dsi = devfs_ruleset_bynum(dr->dr_incset);
482a1dc2096SDima Dorfman 		if (dsi == NULL)
483a1dc2096SDima Dorfman 			return (ESRCH);
484a1dc2096SDima Dorfman 	} else
485a1dc2096SDima Dorfman 		dsi = NULL;
486a1dc2096SDima Dorfman 
487a1dc2096SDima Dorfman 	rsnum = rid2rsn(dr->dr_id);
488e515ee78SPoul-Henning Kamp 	KASSERT(rsnum != 0, ("Inserting into ruleset zero"));
489e515ee78SPoul-Henning Kamp 
490a1dc2096SDima Dorfman 	ds = devfs_ruleset_bynum(rsnum);
491a1dc2096SDima Dorfman 	if (ds == NULL)
492a1dc2096SDima Dorfman 		ds = devfs_ruleset_create(rsnum);
493a1dc2096SDima Dorfman 	dkrn = rid2rn(dr->dr_id);
494a1dc2096SDima Dorfman 	if (dkrn == 0) {
495a1dc2096SDima Dorfman 		error = devfs_rule_autonumber(ds, &dkrn);
496e515ee78SPoul-Henning Kamp 		if (error != 0) {
497e515ee78SPoul-Henning Kamp 			devfs_ruleset_reap(ds);
498a1dc2096SDima Dorfman 			return (error);
499a1dc2096SDima Dorfman 		}
500e515ee78SPoul-Henning Kamp 	}
501a1dc2096SDima Dorfman 
502e515ee78SPoul-Henning Kamp 	dk = malloc(sizeof(*dk), M_DEVFSRULE, M_WAITOK | M_ZERO);
503a1dc2096SDima Dorfman 	dk->dk_ruleset = ds;
504a1dc2096SDima Dorfman 	if (dsi != NULL)
505a1dc2096SDima Dorfman 		++dsi->ds_refcount;
506a1dc2096SDima Dorfman 	/* XXX: Inspect dr? */
507a1dc2096SDima Dorfman 	memcpy(&dk->dk_rule, dr, sizeof(*dr));
508a1dc2096SDima Dorfman 	dk->dk_rule.dr_id = mkrid(rid2rsn(dk->dk_rule.dr_id), dkrn);
509a1dc2096SDima Dorfman 
510e515ee78SPoul-Henning Kamp 	TAILQ_FOREACH(k1, &ds->ds_rules, dk_list) {
511e515ee78SPoul-Henning Kamp 		if (rid2rn(k1->dk_rule.dr_id) > dkrn) {
512e515ee78SPoul-Henning Kamp 			TAILQ_INSERT_BEFORE(k1, dk, dk_list);
513a1dc2096SDima Dorfman 			break;
514a1dc2096SDima Dorfman 		}
515a1dc2096SDima Dorfman 	}
516e515ee78SPoul-Henning Kamp 	if (k1 == NULL)
517e515ee78SPoul-Henning Kamp 		TAILQ_INSERT_TAIL(&ds->ds_rules, dk, dk_list);
518a1dc2096SDima Dorfman 	return (0);
519a1dc2096SDima Dorfman }
520a1dc2096SDima Dorfman 
521a1dc2096SDima Dorfman /*
522a1dc2096SDima Dorfman  * Determine whether dk matches de.  Returns 1 if dk should be run on
523a1dc2096SDima Dorfman  * de; 0, otherwise.
524a1dc2096SDima Dorfman  */
525a1dc2096SDima Dorfman static int
526a1dc2096SDima Dorfman devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de)
527a1dc2096SDima Dorfman {
528a1dc2096SDima Dorfman 	struct devfs_rule *dr = &dk->dk_rule;
52989c9c53dSPoul-Henning Kamp 	struct cdev *dev;
53091a35e78SKonstantin Belousov 	struct cdevsw *dsw;
531a1dc2096SDima Dorfman 
532a1dc2096SDima Dorfman 	dev = devfs_rule_getdev(de);
533a1dc2096SDima Dorfman 	/*
534a1dc2096SDima Dorfman 	 * At this point, if dev is NULL, we should assume that any
535a1dc2096SDima Dorfman 	 * criteria that depend on it don't match.  We should *not*
536a1dc2096SDima Dorfman 	 * just ignore them (i.e., act like they weren't specified),
537a1dc2096SDima Dorfman 	 * since that makes a rule that only has criteria dependent on
53889c9c53dSPoul-Henning Kamp 	 * the struct cdev *match all symlinks and directories.
539a1dc2096SDima Dorfman 	 *
540a1dc2096SDima Dorfman 	 * Note also that the following tests are somewhat reversed:
541a1dc2096SDima Dorfman 	 * They're actually testing to see whether the condition does
542a1dc2096SDima Dorfman 	 * *not* match, since the default is to assume the rule should
543a1dc2096SDima Dorfman 	 * be run (such as if there are no conditions).
544a1dc2096SDima Dorfman 	 */
54591a35e78SKonstantin Belousov 	if (dr->dr_icond & DRC_DSWFLAGS) {
54691a35e78SKonstantin Belousov 		if (dev == NULL)
5475e080af4SPoul-Henning Kamp 			return (0);
54891a35e78SKonstantin Belousov 		dsw = dev_refthread(dev);
54991a35e78SKonstantin Belousov 		if (dsw == NULL)
55091a35e78SKonstantin Belousov 			return (0);
55191a35e78SKonstantin Belousov 		if ((dsw->d_flags & dr->dr_dswflags) == 0) {
55291a35e78SKonstantin Belousov 			dev_relthread(dev);
55391a35e78SKonstantin Belousov 			return (0);
55491a35e78SKonstantin Belousov 		}
55591a35e78SKonstantin Belousov 		dev_relthread(dev);
55691a35e78SKonstantin Belousov 	}
557a1dc2096SDima Dorfman 	if (dr->dr_icond & DRC_PATHPTRN)
558a1dc2096SDima Dorfman 		if (!devfs_rule_matchpath(dk, de))
5595e080af4SPoul-Henning Kamp 			return (0);
560a1dc2096SDima Dorfman 
561a1dc2096SDima Dorfman 	return (1);
562a1dc2096SDima Dorfman }
563a1dc2096SDima Dorfman 
564a1dc2096SDima Dorfman /*
565a1dc2096SDima Dorfman  * Determine whether dk matches de on account of dr_pathptrn.
566a1dc2096SDima Dorfman  */
567a1dc2096SDima Dorfman static int
568a1dc2096SDima Dorfman devfs_rule_matchpath(struct devfs_krule *dk, struct devfs_dirent *de)
569a1dc2096SDima Dorfman {
570a1dc2096SDima Dorfman 	struct devfs_rule *dr = &dk->dk_rule;
571a1dc2096SDima Dorfman 	char *pname;
57289c9c53dSPoul-Henning Kamp 	struct cdev *dev;
573a1dc2096SDima Dorfman 
574a1dc2096SDima Dorfman 	dev = devfs_rule_getdev(de);
575a1dc2096SDima Dorfman 	if (dev != NULL)
576a1dc2096SDima Dorfman 		pname = dev->si_name;
5779f8ef8b8SColin Percival 	else if (de->de_dirent->d_type == DT_LNK ||
5789f8ef8b8SColin Percival 	    de->de_dirent->d_type == DT_DIR)
579797159bdSDima Dorfman 		pname = de->de_dirent->d_name;
580a1dc2096SDima Dorfman 	else
581a1dc2096SDima Dorfman 		return (0);
582a1dc2096SDima Dorfman 	KASSERT(pname != NULL, ("devfs_rule_matchpath: NULL pname"));
583a1dc2096SDima Dorfman 
584e5d09546SDima Dorfman 	return (fnmatch(dr->dr_pathptrn, pname, 0) == 0);
585a1dc2096SDima Dorfman }
586a1dc2096SDima Dorfman 
587a1dc2096SDima Dorfman /*
588a1dc2096SDima Dorfman  * Run dk on de.
589a1dc2096SDima Dorfman  */
590a1dc2096SDima Dorfman static void
5915e080af4SPoul-Henning Kamp devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de, unsigned depth)
592a1dc2096SDima Dorfman {
593a1dc2096SDima Dorfman 	struct devfs_rule *dr = &dk->dk_rule;
594a1dc2096SDima Dorfman 	struct devfs_ruleset *ds;
595a1dc2096SDima Dorfman 
5965e080af4SPoul-Henning Kamp 	if (!devfs_rule_match(dk, de))
5975e080af4SPoul-Henning Kamp 		return;
598a1dc2096SDima Dorfman 	if (dr->dr_iacts & DRA_BACTS) {
599a1dc2096SDima Dorfman 		if (dr->dr_bacts & DRB_HIDE)
600a1dc2096SDima Dorfman 			de->de_flags |= DE_WHITEOUT;
601a1dc2096SDima Dorfman 		if (dr->dr_bacts & DRB_UNHIDE)
602a1dc2096SDima Dorfman 			de->de_flags &= ~DE_WHITEOUT;
603a1dc2096SDima Dorfman 	}
604a1dc2096SDima Dorfman 	if (dr->dr_iacts & DRA_UID)
605a1dc2096SDima Dorfman 		de->de_uid = dr->dr_uid;
606a1dc2096SDima Dorfman 	if (dr->dr_iacts & DRA_GID)
607a1dc2096SDima Dorfman 		de->de_gid = dr->dr_gid;
608a1dc2096SDima Dorfman 	if (dr->dr_iacts & DRA_MODE)
609a1dc2096SDima Dorfman 		de->de_mode = dr->dr_mode;
610a1dc2096SDima Dorfman 	if (dr->dr_iacts & DRA_INCSET) {
6115e080af4SPoul-Henning Kamp 		/*
6125e080af4SPoul-Henning Kamp 		 * XXX: we should tell the user if the depth is exceeded here
6135e080af4SPoul-Henning Kamp 		 * XXX: but it is not obvious how to.  A return value will
6145e080af4SPoul-Henning Kamp 		 * XXX: not work as this is called when devices are created
6155e080af4SPoul-Henning Kamp 		 * XXX: long time after the rules were instantiated.
6165e080af4SPoul-Henning Kamp 		 * XXX: a printf() would probably give too much noise, or
6175e080af4SPoul-Henning Kamp 		 * XXX: DoS the machine.  I guess a a rate-limited message
6185e080af4SPoul-Henning Kamp 		 * XXX: might work.
6195e080af4SPoul-Henning Kamp 		 */
6205e080af4SPoul-Henning Kamp 		if (depth > 0) {
621a1dc2096SDima Dorfman 			ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset);
622a1dc2096SDima Dorfman 			KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset"));
6235e080af4SPoul-Henning Kamp 			devfs_ruleset_applyde(ds, de, depth - 1);
6245e080af4SPoul-Henning Kamp 		}
625a1dc2096SDima Dorfman 	}
626a1dc2096SDima Dorfman }
627a1dc2096SDima Dorfman 
628a1dc2096SDima Dorfman /*
629a1dc2096SDima Dorfman  * Apply all the rules in ds to de.
630a1dc2096SDima Dorfman  */
631a1dc2096SDima Dorfman static void
6325e080af4SPoul-Henning Kamp devfs_ruleset_applyde(struct devfs_ruleset *ds, struct devfs_dirent *de, unsigned depth)
633a1dc2096SDima Dorfman {
634a1dc2096SDima Dorfman 	struct devfs_krule *dk;
635a1dc2096SDima Dorfman 
636e515ee78SPoul-Henning Kamp 	TAILQ_FOREACH(dk, &ds->ds_rules, dk_list)
6375e080af4SPoul-Henning Kamp 		devfs_rule_run(dk, de, depth);
638a1dc2096SDima Dorfman }
639a1dc2096SDima Dorfman 
640a1dc2096SDima Dorfman /*
641a1dc2096SDima Dorfman  * Apply all the rules in ds to all the entires in dm.
642a1dc2096SDima Dorfman  */
643a1dc2096SDima Dorfman static void
644a1dc2096SDima Dorfman devfs_ruleset_applydm(struct devfs_ruleset *ds, struct devfs_mount *dm)
645a1dc2096SDima Dorfman {
646a1dc2096SDima Dorfman 	struct devfs_krule *dk;
647a1dc2096SDima Dorfman 
648a1dc2096SDima Dorfman 	/*
649a1dc2096SDima Dorfman 	 * XXX: Does it matter whether we do
650a1dc2096SDima Dorfman 	 *
651a1dc2096SDima Dorfman 	 *	foreach(dk in ds)
652a1dc2096SDima Dorfman 	 *		foreach(de in dm)
653a1dc2096SDima Dorfman 	 *			apply(dk to de)
654a1dc2096SDima Dorfman 	 *
655a1dc2096SDima Dorfman 	 * as opposed to
656a1dc2096SDima Dorfman 	 *
657a1dc2096SDima Dorfman 	 *	foreach(de in dm)
658a1dc2096SDima Dorfman 	 *		foreach(dk in ds)
659a1dc2096SDima Dorfman 	 *			apply(dk to de)
660a1dc2096SDima Dorfman 	 *
661a1dc2096SDima Dorfman 	 * The end result is obviously the same, but does the order
662a1dc2096SDima Dorfman 	 * matter?
663a1dc2096SDima Dorfman 	 */
664e515ee78SPoul-Henning Kamp 	TAILQ_FOREACH(dk, &ds->ds_rules, dk_list)
665a1dc2096SDima Dorfman 		devfs_rule_applydm(dk, dm);
666a1dc2096SDima Dorfman }
667a1dc2096SDima Dorfman 
668a1dc2096SDima Dorfman /*
669a1dc2096SDima Dorfman  * Find a ruleset by number.
670a1dc2096SDima Dorfman  */
671a1dc2096SDima Dorfman static struct devfs_ruleset *
672a1dc2096SDima Dorfman devfs_ruleset_bynum(devfs_rsnum rsnum)
673a1dc2096SDima Dorfman {
674a1dc2096SDima Dorfman 	struct devfs_ruleset *ds;
675a1dc2096SDima Dorfman 
676e515ee78SPoul-Henning Kamp 	TAILQ_FOREACH(ds, &devfs_rulesets, ds_list) {
677a1dc2096SDima Dorfman 		if (ds->ds_number == rsnum)
678a1dc2096SDima Dorfman 			return (ds);
679a1dc2096SDima Dorfman 	}
680a1dc2096SDima Dorfman 	return (NULL);
681a1dc2096SDima Dorfman }
682a1dc2096SDima Dorfman 
683a1dc2096SDima Dorfman /*
684a1dc2096SDima Dorfman  * Create a new ruleset.
685a1dc2096SDima Dorfman  */
686a1dc2096SDima Dorfman static struct devfs_ruleset *
687a1dc2096SDima Dorfman devfs_ruleset_create(devfs_rsnum rsnum)
688a1dc2096SDima Dorfman {
689e515ee78SPoul-Henning Kamp 	struct devfs_ruleset *s1;
690a1dc2096SDima Dorfman 	struct devfs_ruleset *ds;
691a1dc2096SDima Dorfman 
692e515ee78SPoul-Henning Kamp 	KASSERT(rsnum != 0, ("creating ruleset zero"));
693e515ee78SPoul-Henning Kamp 
694a1dc2096SDima Dorfman 	KASSERT(devfs_ruleset_bynum(rsnum) == NULL,
695a1dc2096SDima Dorfman 	    ("creating already existent ruleset %d", rsnum));
696a1dc2096SDima Dorfman 
697e515ee78SPoul-Henning Kamp 	ds = malloc(sizeof(*ds), M_DEVFSRULE, M_WAITOK | M_ZERO);
698a1dc2096SDima Dorfman 	ds->ds_number = rsnum;
699e515ee78SPoul-Henning Kamp 	TAILQ_INIT(&ds->ds_rules);
700a1dc2096SDima Dorfman 
701e515ee78SPoul-Henning Kamp 	TAILQ_FOREACH(s1, &devfs_rulesets, ds_list) {
702e515ee78SPoul-Henning Kamp 		if (s1->ds_number > rsnum) {
703e515ee78SPoul-Henning Kamp 			TAILQ_INSERT_BEFORE(s1, ds, ds_list);
704a1dc2096SDima Dorfman 			break;
705a1dc2096SDima Dorfman 		}
706a1dc2096SDima Dorfman 	}
707e515ee78SPoul-Henning Kamp 	if (s1 == NULL)
708e515ee78SPoul-Henning Kamp 		TAILQ_INSERT_TAIL(&devfs_rulesets, ds, ds_list);
709a1dc2096SDima Dorfman 	return (ds);
710a1dc2096SDima Dorfman }
711a1dc2096SDima Dorfman 
712a1dc2096SDima Dorfman /*
713a1dc2096SDima Dorfman  * Remove a ruleset from the system if it's empty and not used
714a1dc2096SDima Dorfman  * anywhere.  This should be called after every time a rule is deleted
715a1dc2096SDima Dorfman  * from this ruleset or the reference count is decremented.
716a1dc2096SDima Dorfman  */
717a1dc2096SDima Dorfman static void
718e515ee78SPoul-Henning Kamp devfs_ruleset_reap(struct devfs_ruleset *ds)
719a1dc2096SDima Dorfman {
720a1dc2096SDima Dorfman 
721e515ee78SPoul-Henning Kamp 	KASSERT(ds->ds_number != 0, ("reaping ruleset zero "));
722e515ee78SPoul-Henning Kamp 
723e515ee78SPoul-Henning Kamp 	if (!TAILQ_EMPTY(&ds->ds_rules) || ds->ds_refcount != 0)
724e515ee78SPoul-Henning Kamp 		return;
725e515ee78SPoul-Henning Kamp 
726e515ee78SPoul-Henning Kamp 	TAILQ_REMOVE(&devfs_rulesets, ds, ds_list);
727e515ee78SPoul-Henning Kamp 	free(ds, M_DEVFSRULE);
728a1dc2096SDima Dorfman }
729a1dc2096SDima Dorfman 
730a1dc2096SDima Dorfman /*
731a1dc2096SDima Dorfman  * Make rsnum the active ruleset for dm.
732a1dc2096SDima Dorfman  */
733a1dc2096SDima Dorfman static int
734a1dc2096SDima Dorfman devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm)
735a1dc2096SDima Dorfman {
736a1dc2096SDima Dorfman 	struct devfs_ruleset *cds, *ds;
737a1dc2096SDima Dorfman 
738a1dc2096SDima Dorfman 	ds = devfs_ruleset_bynum(rsnum);
739a1dc2096SDima Dorfman 	if (ds == NULL)
740a1dc2096SDima Dorfman 		ds = devfs_ruleset_create(rsnum);
741e515ee78SPoul-Henning Kamp 	if (dm->dm_ruleset != 0) {
742a1dc2096SDima Dorfman 		cds = devfs_ruleset_bynum(dm->dm_ruleset);
743e515ee78SPoul-Henning Kamp 		--cds->ds_refcount;
744e515ee78SPoul-Henning Kamp 		devfs_ruleset_reap(cds);
745e515ee78SPoul-Henning Kamp 	}
746a1dc2096SDima Dorfman 
747a1dc2096SDima Dorfman 	/* These should probably be made atomic somehow. */
748a1dc2096SDima Dorfman 	++ds->ds_refcount;
749a1dc2096SDima Dorfman 	dm->dm_ruleset = rsnum;
750a1dc2096SDima Dorfman 
751a1dc2096SDima Dorfman 	return (0);
752a1dc2096SDima Dorfman }
753e515ee78SPoul-Henning Kamp 
754e515ee78SPoul-Henning Kamp void
755e515ee78SPoul-Henning Kamp devfs_rules_cleanup(struct devfs_mount *dm)
756e515ee78SPoul-Henning Kamp {
757e515ee78SPoul-Henning Kamp 	struct devfs_ruleset *ds;
758e515ee78SPoul-Henning Kamp 
759e515ee78SPoul-Henning Kamp 	sx_assert(&dm->dm_lock, SX_XLOCKED);
760e515ee78SPoul-Henning Kamp 	if (dm->dm_ruleset != 0) {
761e515ee78SPoul-Henning Kamp 		ds = devfs_ruleset_bynum(dm->dm_ruleset);
762e515ee78SPoul-Henning Kamp 		--ds->ds_refcount;
763e515ee78SPoul-Henning Kamp 		devfs_ruleset_reap(ds);
764e515ee78SPoul-Henning Kamp 	}
765e515ee78SPoul-Henning Kamp }
766