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