2 * Copyright (c) 2002 Dima Dorfman.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 * DEVFS ruleset implementation.
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.
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.
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.
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
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).
65 #include <sys/param.h>
66 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.h>
71 #include <sys/dirent.h>
72 #include <sys/ioccom.h>
76 #include <fs/devfs/devfs.h>
77 #include <fs/devfs/devfs_int.h>
80 * Kernel version of devfs_rule.
83 TAILQ_ENTRY(devfs_krule) dk_list;
84 struct devfs_ruleset *dk_ruleset;
85 struct devfs_rule dk_rule;
88 TAILQ_HEAD(rulehead, devfs_krule);
89 static MALLOC_DEFINE(M_DEVFSRULE, "DEVFS_RULE", "DEVFS rule storage");
92 * Structure to describe a ruleset.
94 struct devfs_ruleset {
95 TAILQ_ENTRY(devfs_ruleset) ds_list;
96 struct rulehead ds_rules;
97 devfs_rsnum ds_number;
101 static devfs_rid devfs_rid_input(devfs_rid rid, struct devfs_mount *dm);
103 static void devfs_rule_applyde_recursive(struct devfs_krule *dk,
104 struct devfs_dirent *de);
105 static void devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm);
106 static int devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnp);
107 static struct devfs_krule *devfs_rule_byid(devfs_rid rid);
108 static int devfs_rule_delete(struct devfs_krule *dkp);
109 static struct cdev *devfs_rule_getdev(struct devfs_dirent *de);
110 static int devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm);
111 static int devfs_rule_insert(struct devfs_rule *dr);
112 static int devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de);
113 static int devfs_rule_matchpath(struct devfs_krule *dk,
114 struct devfs_dirent *de);
115 static void devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de, unsigned depth);
117 static void devfs_ruleset_applyde(struct devfs_ruleset *ds,
118 struct devfs_dirent *de, unsigned depth);
119 static void devfs_ruleset_applydm(struct devfs_ruleset *ds,
120 struct devfs_mount *dm);
121 static struct devfs_ruleset *devfs_ruleset_bynum(devfs_rsnum rsnum);
122 static struct devfs_ruleset *devfs_ruleset_create(devfs_rsnum rsnum);
123 static void devfs_ruleset_reap(struct devfs_ruleset *dsp);
124 static int devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm);
126 static struct sx sx_rules;
127 SX_SYSINIT(sx_rules, &sx_rules, "DEVFS ruleset lock");
129 static TAILQ_HEAD(, devfs_ruleset) devfs_rulesets =
130 TAILQ_HEAD_INITIALIZER(devfs_rulesets);
133 * Called to apply the proper rules for 'de' before it can be
134 * exposed to the userland. This should be called with an exclusive
135 * lock on dm in case we need to run anything.
138 devfs_rules_apply(struct devfs_mount *dm, struct devfs_dirent *de)
140 struct devfs_ruleset *ds;
142 if (dm->dm_ruleset == 0)
145 ds = devfs_ruleset_bynum(dm->dm_ruleset);
146 KASSERT(ds != NULL, ("mount-point has NULL ruleset"));
147 devfs_ruleset_applyde(ds, de, devfs_rule_depth);
148 sx_sunlock(&sx_rules);
152 * Rule subsystem ioctl hook.
155 devfs_rules_ioctl(struct devfs_mount *dm, u_long cmd, caddr_t data, struct thread *td)
157 struct devfs_ruleset *ds;
158 struct devfs_krule *dk;
159 struct devfs_rule *dr;
165 sx_assert(&dm->dm_lock, SX_XLOCKED);
168 * XXX: This returns an error regardless of whether we actually
169 * support the cmd or not.
171 * We could make this privileges finer grained if desired.
173 error = priv_check(td, PRIV_DEVFS_RULE);
181 dr = (struct devfs_rule *)data;
182 error = devfs_rule_input(dr, dm);
185 dk = devfs_rule_byid(dr->dr_id);
190 if (rid2rsn(dr->dr_id) == 0) {
194 error = devfs_rule_insert(dr);
197 dr = (struct devfs_rule *)data;
198 error = devfs_rule_input(dr, dm);
203 * This is one of many possible hackish
204 * implementations. The primary contender is an
205 * implementation where the rule we read in is
206 * temporarily inserted into some ruleset, perhaps
207 * with a hypothetical DRO_NOAUTO flag so that it
208 * doesn't get used where it isn't intended, and
209 * applied in the normal way. This can be done in the
210 * userland (DEVFSIO_ADD, DEVFSIO_APPLYID,
211 * DEVFSIO_DEL) or in the kernel; either way it breaks
212 * some corner case assumptions in other parts of the
213 * code (not that this implementation doesn't do
216 if (dr->dr_iacts & DRA_INCSET &&
217 devfs_ruleset_bynum(dr->dr_incset) == NULL) {
221 dk = malloc(sizeof(*dk), M_TEMP, M_WAITOK | M_ZERO);
222 memcpy(&dk->dk_rule, dr, sizeof(*dr));
223 devfs_rule_applydm(dk, dm);
226 case DEVFSIO_RAPPLYID:
227 rid = *(devfs_rid *)data;
228 rid = devfs_rid_input(rid, dm);
229 dk = devfs_rule_byid(rid);
234 devfs_rule_applydm(dk, dm);
237 rid = *(devfs_rid *)data;
238 rid = devfs_rid_input(rid, dm);
239 dk = devfs_rule_byid(rid);
245 error = devfs_rule_delete(dk);
247 case DEVFSIO_RGETNEXT:
248 dr = (struct devfs_rule *)data;
249 error = devfs_rule_input(dr, dm);
253 * We can't use devfs_rule_byid() here since that
254 * requires the rule specified to exist, but we want
255 * getnext(N) to work whether there is a rule N or not
256 * (specifically, getnext(0) must work, but we should
257 * never have a rule 0 since the add command
258 * interprets 0 to mean "auto-number").
260 ds = devfs_ruleset_bynum(rid2rsn(dr->dr_id));
265 rnum = rid2rn(dr->dr_id);
266 TAILQ_FOREACH(dk, &ds->ds_rules, dk_list) {
267 if (rid2rn(dk->dk_rule.dr_id) > rnum)
274 memcpy(dr, &dk->dk_rule, sizeof(*dr));
277 rsnum = *(devfs_rsnum *)data;
278 error = devfs_ruleset_use(rsnum, dm);
281 rsnum = *(devfs_rsnum *)data;
282 rsnum = rid2rsn(devfs_rid_input(mkrid(rsnum, 0), dm));
283 ds = devfs_ruleset_bynum(rsnum);
288 devfs_ruleset_applydm(ds, dm);
290 case DEVFSIO_SGETNEXT:
291 rsnum = *(devfs_rsnum *)data;
292 TAILQ_FOREACH(ds, &devfs_rulesets, ds_list) {
293 if (ds->ds_number > rsnum)
300 *(devfs_rsnum *)data = ds->ds_number;
307 sx_xunlock(&sx_rules);
312 * Adjust the rule identifier to use the ruleset of dm if one isn't
313 * explicitly specified.
315 * Note that after this operation, rid2rsn(rid) might still be 0, and
316 * that's okay; ruleset 0 is a valid ruleset, but when it's read in
317 * from the userland, it means "current ruleset for this mount-point".
320 devfs_rid_input(devfs_rid rid, struct devfs_mount *dm)
323 if (rid2rsn(rid) == 0)
324 return (mkrid(dm->dm_ruleset, rid2rn(rid)));
330 * Apply dk to de and everything under de.
332 * XXX: This method needs a function call for every nested
333 * subdirectory in a devfs mount. If we plan to have many of these,
334 * we might eventually run out of kernel stack space.
335 * XXX: a linear search could be done through the cdev list instead.
338 devfs_rule_applyde_recursive(struct devfs_krule *dk, struct devfs_dirent *de)
340 struct devfs_dirent *de2;
342 TAILQ_FOREACH(de2, &de->de_dlist, de_list)
343 devfs_rule_applyde_recursive(dk, de2);
344 devfs_rule_run(dk, de, devfs_rule_depth);
348 * Apply dk to all entires in dm.
351 devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm)
354 devfs_rule_applyde_recursive(dk, dm->dm_rootdir);
358 * Automatically select a number for a new rule in ds, and write the
362 devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnump)
364 struct devfs_krule *dk;
366 /* Find the last rule. */
367 dk = TAILQ_LAST(&ds->ds_rules, rulehead);
371 *rnump = rid2rn(dk->dk_rule.dr_id) + 100;
372 /* Detect overflow. */
373 if (*rnump < rid2rn(dk->dk_rule.dr_id))
376 KASSERT(devfs_rule_byid(mkrid(ds->ds_number, *rnump)) == NULL,
377 ("autonumbering resulted in an already existing rule"));
382 * Find a krule by id.
384 static struct devfs_krule *
385 devfs_rule_byid(devfs_rid rid)
387 struct devfs_ruleset *ds;
388 struct devfs_krule *dk;
392 ds = devfs_ruleset_bynum(rid2rsn(rid));
395 TAILQ_FOREACH(dk, &ds->ds_rules, dk_list) {
396 if (rid2rn(dk->dk_rule.dr_id) == rn)
398 else if (rid2rn(dk->dk_rule.dr_id) > rn)
405 * Remove dkp from any lists it may be on and remove memory associated
409 devfs_rule_delete(struct devfs_krule *dk)
411 struct devfs_ruleset *ds;
413 if (dk->dk_rule.dr_iacts & DRA_INCSET) {
414 ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset);
415 KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset"));
417 devfs_ruleset_reap(ds);
420 TAILQ_REMOVE(&ds->ds_rules, dk, dk_list);
421 devfs_ruleset_reap(ds);
422 free(dk, M_DEVFSRULE);
427 * Get a struct cdev *corresponding to de so we can try to match rules based
428 * on it. If this routine returns NULL, there is no struct cdev *associated
429 * with the dirent (symlinks and directories don't have dev_ts), and
430 * the caller should assume that any critera dependent on a dev_t
434 devfs_rule_getdev(struct devfs_dirent *de)
437 if (de->de_cdp == NULL)
439 if (de->de_cdp->cdp_flags & CDP_ACTIVE)
440 return (&de->de_cdp->cdp_c);
446 * Do what we need to do to a rule that we just loaded from the
447 * userland. In particular, we need to check the magic, and adjust
448 * the ruleset appropriate if desired.
451 devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm)
454 if (dr->dr_magic != DEVFS_MAGIC)
455 return (ERPCMISMATCH);
456 dr->dr_id = devfs_rid_input(dr->dr_id, dm);
461 * Import dr into the appropriate place in the kernel (i.e., make a
462 * krule). The value of dr is copied, so the pointer may be destroyed
463 * after this call completes.
466 devfs_rule_insert(struct devfs_rule *dr)
468 struct devfs_ruleset *ds, *dsi;
469 struct devfs_krule *k1;
470 struct devfs_krule *dk;
476 * This stuff seems out of place here, but we want to do it as
477 * soon as possible so that if it fails, we don't have to roll
478 * back any changes we already made (e.g., ruleset creation).
480 if (dr->dr_iacts & DRA_INCSET) {
481 dsi = devfs_ruleset_bynum(dr->dr_incset);
487 rsnum = rid2rsn(dr->dr_id);
488 KASSERT(rsnum != 0, ("Inserting into ruleset zero"));
490 ds = devfs_ruleset_bynum(rsnum);
492 ds = devfs_ruleset_create(rsnum);
493 dkrn = rid2rn(dr->dr_id);
495 error = devfs_rule_autonumber(ds, &dkrn);
497 devfs_ruleset_reap(ds);
502 dk = malloc(sizeof(*dk), M_DEVFSRULE, M_WAITOK | M_ZERO);
506 /* XXX: Inspect dr? */
507 memcpy(&dk->dk_rule, dr, sizeof(*dr));
508 dk->dk_rule.dr_id = mkrid(rid2rsn(dk->dk_rule.dr_id), dkrn);
510 TAILQ_FOREACH(k1, &ds->ds_rules, dk_list) {
511 if (rid2rn(k1->dk_rule.dr_id) > dkrn) {
512 TAILQ_INSERT_BEFORE(k1, dk, dk_list);
517 TAILQ_INSERT_TAIL(&ds->ds_rules, dk, dk_list);
522 * Determine whether dk matches de. Returns 1 if dk should be run on
526 devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de)
528 struct devfs_rule *dr = &dk->dk_rule;
533 dev = devfs_rule_getdev(de);
535 * At this point, if dev is NULL, we should assume that any
536 * criteria that depend on it don't match. We should *not*
537 * just ignore them (i.e., act like they weren't specified),
538 * since that makes a rule that only has criteria dependent on
539 * the struct cdev *match all symlinks and directories.
541 * Note also that the following tests are somewhat reversed:
542 * They're actually testing to see whether the condition does
543 * *not* match, since the default is to assume the rule should
544 * be run (such as if there are no conditions).
546 if (dr->dr_icond & DRC_DSWFLAGS) {
549 dsw = dev_refthread(dev, &ref);
552 if ((dsw->d_flags & dr->dr_dswflags) == 0) {
553 dev_relthread(dev, ref);
556 dev_relthread(dev, ref);
558 if (dr->dr_icond & DRC_PATHPTRN)
559 if (!devfs_rule_matchpath(dk, de))
566 * Determine whether dk matches de on account of dr_pathptrn.
569 devfs_rule_matchpath(struct devfs_krule *dk, struct devfs_dirent *de)
571 struct devfs_rule *dr = &dk->dk_rule;
575 dev = devfs_rule_getdev(de);
577 pname = dev->si_name;
578 else if (de->de_dirent->d_type == DT_LNK ||
579 de->de_dirent->d_type == DT_DIR)
580 pname = de->de_dirent->d_name;
583 KASSERT(pname != NULL, ("devfs_rule_matchpath: NULL pname"));
585 return (fnmatch(dr->dr_pathptrn, pname, 0) == 0);
592 devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de, unsigned depth)
594 struct devfs_rule *dr = &dk->dk_rule;
595 struct devfs_ruleset *ds;
597 if (!devfs_rule_match(dk, de))
599 if (dr->dr_iacts & DRA_BACTS) {
600 if (dr->dr_bacts & DRB_HIDE)
601 de->de_flags |= DE_WHITEOUT;
602 if (dr->dr_bacts & DRB_UNHIDE)
603 de->de_flags &= ~DE_WHITEOUT;
605 if (dr->dr_iacts & DRA_UID)
606 de->de_uid = dr->dr_uid;
607 if (dr->dr_iacts & DRA_GID)
608 de->de_gid = dr->dr_gid;
609 if (dr->dr_iacts & DRA_MODE)
610 de->de_mode = dr->dr_mode;
611 if (dr->dr_iacts & DRA_INCSET) {
613 * XXX: we should tell the user if the depth is exceeded here
614 * XXX: but it is not obvious how to. A return value will
615 * XXX: not work as this is called when devices are created
616 * XXX: long time after the rules were instantiated.
617 * XXX: a printf() would probably give too much noise, or
618 * XXX: DoS the machine. I guess a a rate-limited message
622 ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset);
623 KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset"));
624 devfs_ruleset_applyde(ds, de, depth - 1);
630 * Apply all the rules in ds to de.
633 devfs_ruleset_applyde(struct devfs_ruleset *ds, struct devfs_dirent *de, unsigned depth)
635 struct devfs_krule *dk;
637 TAILQ_FOREACH(dk, &ds->ds_rules, dk_list)
638 devfs_rule_run(dk, de, depth);
642 * Apply all the rules in ds to all the entires in dm.
645 devfs_ruleset_applydm(struct devfs_ruleset *ds, struct devfs_mount *dm)
647 struct devfs_krule *dk;
650 * XXX: Does it matter whether we do
662 * The end result is obviously the same, but does the order
665 TAILQ_FOREACH(dk, &ds->ds_rules, dk_list)
666 devfs_rule_applydm(dk, dm);
670 * Find a ruleset by number.
672 static struct devfs_ruleset *
673 devfs_ruleset_bynum(devfs_rsnum rsnum)
675 struct devfs_ruleset *ds;
677 TAILQ_FOREACH(ds, &devfs_rulesets, ds_list) {
678 if (ds->ds_number == rsnum)
685 * Create a new ruleset.
687 static struct devfs_ruleset *
688 devfs_ruleset_create(devfs_rsnum rsnum)
690 struct devfs_ruleset *s1;
691 struct devfs_ruleset *ds;
693 KASSERT(rsnum != 0, ("creating ruleset zero"));
695 KASSERT(devfs_ruleset_bynum(rsnum) == NULL,
696 ("creating already existent ruleset %d", rsnum));
698 ds = malloc(sizeof(*ds), M_DEVFSRULE, M_WAITOK | M_ZERO);
699 ds->ds_number = rsnum;
700 TAILQ_INIT(&ds->ds_rules);
702 TAILQ_FOREACH(s1, &devfs_rulesets, ds_list) {
703 if (s1->ds_number > rsnum) {
704 TAILQ_INSERT_BEFORE(s1, ds, ds_list);
709 TAILQ_INSERT_TAIL(&devfs_rulesets, ds, ds_list);
714 * Remove a ruleset from the system if it's empty and not used
715 * anywhere. This should be called after every time a rule is deleted
716 * from this ruleset or the reference count is decremented.
719 devfs_ruleset_reap(struct devfs_ruleset *ds)
722 KASSERT(ds->ds_number != 0, ("reaping ruleset zero "));
724 if (!TAILQ_EMPTY(&ds->ds_rules) || ds->ds_refcount != 0)
727 TAILQ_REMOVE(&devfs_rulesets, ds, ds_list);
728 free(ds, M_DEVFSRULE);
732 * Make rsnum the active ruleset for dm.
735 devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm)
737 struct devfs_ruleset *cds, *ds;
739 if (dm->dm_ruleset != 0) {
740 cds = devfs_ruleset_bynum(dm->dm_ruleset);
742 devfs_ruleset_reap(cds);
745 ds = devfs_ruleset_bynum(rsnum);
747 ds = devfs_ruleset_create(rsnum);
748 /* These should probably be made atomic somehow. */
750 dm->dm_ruleset = rsnum;
756 devfs_rules_cleanup(struct devfs_mount *dm)
758 struct devfs_ruleset *ds;
760 sx_assert(&dm->dm_lock, SX_XLOCKED);
761 if (dm->dm_ruleset != 0) {
762 ds = devfs_ruleset_bynum(dm->dm_ruleset);
764 devfs_ruleset_reap(ds);