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 142*ef456eecSJaakko Heinonen sx_assert(&dm->dm_lock, SX_XLOCKED); 143*ef456eecSJaakko Heinonen 144e515ee78SPoul-Henning Kamp if (dm->dm_ruleset == 0) 145e515ee78SPoul-Henning Kamp return; 1466556102dSPoul-Henning Kamp sx_slock(&sx_rules); 147a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(dm->dm_ruleset); 148a1dc2096SDima Dorfman KASSERT(ds != NULL, ("mount-point has NULL ruleset")); 1495e080af4SPoul-Henning Kamp devfs_ruleset_applyde(ds, de, devfs_rule_depth); 1506556102dSPoul-Henning Kamp sx_sunlock(&sx_rules); 151a1dc2096SDima Dorfman } 152a1dc2096SDima Dorfman 153a1dc2096SDima Dorfman /* 154a1dc2096SDima Dorfman * Rule subsystem ioctl hook. 155a1dc2096SDima Dorfman */ 156a1dc2096SDima Dorfman int 157ab32e952SPoul-Henning Kamp devfs_rules_ioctl(struct devfs_mount *dm, u_long cmd, caddr_t data, struct thread *td) 158a1dc2096SDima Dorfman { 159a1dc2096SDima Dorfman struct devfs_ruleset *ds; 160a1dc2096SDima Dorfman struct devfs_krule *dk; 161a1dc2096SDima Dorfman struct devfs_rule *dr; 162a1dc2096SDima Dorfman devfs_rsnum rsnum; 163a1dc2096SDima Dorfman devfs_rnum rnum; 164a1dc2096SDima Dorfman devfs_rid rid; 165a1dc2096SDima Dorfman int error; 166a1dc2096SDima Dorfman 167e606a3c6SPoul-Henning Kamp sx_assert(&dm->dm_lock, SX_XLOCKED); 168e606a3c6SPoul-Henning Kamp 169a1dc2096SDima Dorfman /* 170acd3428bSRobert Watson * XXX: This returns an error regardless of whether we actually 171acd3428bSRobert Watson * support the cmd or not. 172acd3428bSRobert Watson * 173acd3428bSRobert Watson * We could make this privileges finer grained if desired. 174a1dc2096SDima Dorfman */ 175acd3428bSRobert Watson error = priv_check(td, PRIV_DEVFS_RULE); 176acd3428bSRobert Watson if (error) 177a1dc2096SDima Dorfman return (error); 178a1dc2096SDima Dorfman 1796556102dSPoul-Henning Kamp sx_xlock(&sx_rules); 180e606a3c6SPoul-Henning Kamp 181a1dc2096SDima Dorfman switch (cmd) { 182a1dc2096SDima Dorfman case DEVFSIO_RADD: 183a1dc2096SDima Dorfman dr = (struct devfs_rule *)data; 184a1dc2096SDima Dorfman error = devfs_rule_input(dr, dm); 185a1dc2096SDima Dorfman if (error != 0) 186e515ee78SPoul-Henning Kamp break; 187a1dc2096SDima Dorfman dk = devfs_rule_byid(dr->dr_id); 188a1dc2096SDima Dorfman if (dk != NULL) { 189a1dc2096SDima Dorfman error = EEXIST; 190e515ee78SPoul-Henning Kamp break; 191a1dc2096SDima Dorfman } 1928f0d99d7SRobert Watson if (rid2rsn(dr->dr_id) == 0) { 1938f0d99d7SRobert Watson error = EIO; 1948f0d99d7SRobert Watson break; 1958f0d99d7SRobert Watson } 196a1dc2096SDima Dorfman error = devfs_rule_insert(dr); 197a1dc2096SDima Dorfman break; 198a1dc2096SDima Dorfman case DEVFSIO_RAPPLY: 199a1dc2096SDima Dorfman dr = (struct devfs_rule *)data; 200a1dc2096SDima Dorfman error = devfs_rule_input(dr, dm); 201a1dc2096SDima Dorfman if (error != 0) 202e515ee78SPoul-Henning Kamp break; 203a1dc2096SDima Dorfman 204a1dc2096SDima Dorfman /* 205a1dc2096SDima Dorfman * This is one of many possible hackish 206a1dc2096SDima Dorfman * implementations. The primary contender is an 207a1dc2096SDima Dorfman * implementation where the rule we read in is 208a1dc2096SDima Dorfman * temporarily inserted into some ruleset, perhaps 209a1dc2096SDima Dorfman * with a hypothetical DRO_NOAUTO flag so that it 210a1dc2096SDima Dorfman * doesn't get used where it isn't intended, and 211a1dc2096SDima Dorfman * applied in the normal way. This can be done in the 212a1dc2096SDima Dorfman * userland (DEVFSIO_ADD, DEVFSIO_APPLYID, 213a1dc2096SDima Dorfman * DEVFSIO_DEL) or in the kernel; either way it breaks 214a1dc2096SDima Dorfman * some corner case assumptions in other parts of the 215a1dc2096SDima Dorfman * code (not that this implementation doesn't do 216a1dc2096SDima Dorfman * that). 217a1dc2096SDima Dorfman */ 218a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_INCSET && 219a1dc2096SDima Dorfman devfs_ruleset_bynum(dr->dr_incset) == NULL) { 220a1dc2096SDima Dorfman error = ESRCH; 221e515ee78SPoul-Henning Kamp break; 222a1dc2096SDima Dorfman } 223a163d034SWarner Losh dk = malloc(sizeof(*dk), M_TEMP, M_WAITOK | M_ZERO); 224a1dc2096SDima Dorfman memcpy(&dk->dk_rule, dr, sizeof(*dr)); 225a1dc2096SDima Dorfman devfs_rule_applydm(dk, dm); 226a1dc2096SDima Dorfman free(dk, M_TEMP); 227a1dc2096SDima Dorfman break; 228a1dc2096SDima Dorfman case DEVFSIO_RAPPLYID: 229a1dc2096SDima Dorfman rid = *(devfs_rid *)data; 230a1dc2096SDima Dorfman rid = devfs_rid_input(rid, dm); 231a1dc2096SDima Dorfman dk = devfs_rule_byid(rid); 232a1dc2096SDima Dorfman if (dk == NULL) { 233a1dc2096SDima Dorfman error = ENOENT; 234e515ee78SPoul-Henning Kamp break; 235a1dc2096SDima Dorfman } 236a1dc2096SDima Dorfman devfs_rule_applydm(dk, dm); 237a1dc2096SDima Dorfman break; 238a1dc2096SDima Dorfman case DEVFSIO_RDEL: 239a1dc2096SDima Dorfman rid = *(devfs_rid *)data; 240a1dc2096SDima Dorfman rid = devfs_rid_input(rid, dm); 241a1dc2096SDima Dorfman dk = devfs_rule_byid(rid); 242a1dc2096SDima Dorfman if (dk == NULL) { 243a1dc2096SDima Dorfman error = ENOENT; 244e515ee78SPoul-Henning Kamp break; 245a1dc2096SDima Dorfman } 246a1dc2096SDima Dorfman ds = dk->dk_ruleset; 247e515ee78SPoul-Henning Kamp error = devfs_rule_delete(dk); 248a1dc2096SDima Dorfman break; 249a1dc2096SDima Dorfman case DEVFSIO_RGETNEXT: 250a1dc2096SDima Dorfman dr = (struct devfs_rule *)data; 251a1dc2096SDima Dorfman error = devfs_rule_input(dr, dm); 252a1dc2096SDima Dorfman if (error != 0) 253e515ee78SPoul-Henning Kamp break; 254a1dc2096SDima Dorfman /* 255a1dc2096SDima Dorfman * We can't use devfs_rule_byid() here since that 256a1dc2096SDima Dorfman * requires the rule specified to exist, but we want 257a1dc2096SDima Dorfman * getnext(N) to work whether there is a rule N or not 258a1dc2096SDima Dorfman * (specifically, getnext(0) must work, but we should 259a1dc2096SDima Dorfman * never have a rule 0 since the add command 260a1dc2096SDima Dorfman * interprets 0 to mean "auto-number"). 261a1dc2096SDima Dorfman */ 262a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rid2rsn(dr->dr_id)); 263a1dc2096SDima Dorfman if (ds == NULL) { 264a1dc2096SDima Dorfman error = ENOENT; 265e515ee78SPoul-Henning Kamp break; 266a1dc2096SDima Dorfman } 267a1dc2096SDima Dorfman rnum = rid2rn(dr->dr_id); 268e515ee78SPoul-Henning Kamp TAILQ_FOREACH(dk, &ds->ds_rules, dk_list) { 269a1dc2096SDima Dorfman if (rid2rn(dk->dk_rule.dr_id) > rnum) 270a1dc2096SDima Dorfman break; 271a1dc2096SDima Dorfman } 272a1dc2096SDima Dorfman if (dk == NULL) { 273a1dc2096SDima Dorfman error = ENOENT; 274e515ee78SPoul-Henning Kamp break; 275a1dc2096SDima Dorfman } 276a1dc2096SDima Dorfman memcpy(dr, &dk->dk_rule, sizeof(*dr)); 277a1dc2096SDima Dorfman break; 278a1dc2096SDima Dorfman case DEVFSIO_SUSE: 279a1dc2096SDima Dorfman rsnum = *(devfs_rsnum *)data; 280a1dc2096SDima Dorfman error = devfs_ruleset_use(rsnum, dm); 281a1dc2096SDima Dorfman break; 282a1dc2096SDima Dorfman case DEVFSIO_SAPPLY: 283a1dc2096SDima Dorfman rsnum = *(devfs_rsnum *)data; 284a1dc2096SDima Dorfman rsnum = rid2rsn(devfs_rid_input(mkrid(rsnum, 0), dm)); 285a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rsnum); 286a1dc2096SDima Dorfman if (ds == NULL) { 287a1dc2096SDima Dorfman error = ESRCH; 288e515ee78SPoul-Henning Kamp break; 289a1dc2096SDima Dorfman } 290a1dc2096SDima Dorfman devfs_ruleset_applydm(ds, dm); 291a1dc2096SDima Dorfman break; 292a1dc2096SDima Dorfman case DEVFSIO_SGETNEXT: 293a1dc2096SDima Dorfman rsnum = *(devfs_rsnum *)data; 294e515ee78SPoul-Henning Kamp TAILQ_FOREACH(ds, &devfs_rulesets, ds_list) { 295a1dc2096SDima Dorfman if (ds->ds_number > rsnum) 296a1dc2096SDima Dorfman break; 297a1dc2096SDima Dorfman } 298e515ee78SPoul-Henning Kamp if (ds == NULL) { 299a1dc2096SDima Dorfman error = ENOENT; 300e515ee78SPoul-Henning Kamp break; 301a1dc2096SDima Dorfman } 302e515ee78SPoul-Henning Kamp *(devfs_rsnum *)data = ds->ds_number; 303a1dc2096SDima Dorfman break; 304a1dc2096SDima Dorfman default: 305a1dc2096SDima Dorfman error = ENOIOCTL; 306a1dc2096SDima Dorfman break; 307a1dc2096SDima Dorfman } 308a1dc2096SDima Dorfman 3096556102dSPoul-Henning Kamp sx_xunlock(&sx_rules); 310a1dc2096SDima Dorfman return (error); 311a1dc2096SDima Dorfman } 312a1dc2096SDima Dorfman 313a1dc2096SDima Dorfman /* 314a1dc2096SDima Dorfman * Adjust the rule identifier to use the ruleset of dm if one isn't 315a1dc2096SDima Dorfman * explicitly specified. 316a1dc2096SDima Dorfman * 317a1dc2096SDima Dorfman * Note that after this operation, rid2rsn(rid) might still be 0, and 318a1dc2096SDima Dorfman * that's okay; ruleset 0 is a valid ruleset, but when it's read in 319a1dc2096SDima Dorfman * from the userland, it means "current ruleset for this mount-point". 320a1dc2096SDima Dorfman */ 321a1dc2096SDima Dorfman static devfs_rid 322a1dc2096SDima Dorfman devfs_rid_input(devfs_rid rid, struct devfs_mount *dm) 323a1dc2096SDima Dorfman { 324a1dc2096SDima Dorfman 325a1dc2096SDima Dorfman if (rid2rsn(rid) == 0) 326a1dc2096SDima Dorfman return (mkrid(dm->dm_ruleset, rid2rn(rid))); 327a1dc2096SDima Dorfman else 328a1dc2096SDima Dorfman return (rid); 329a1dc2096SDima Dorfman } 330a1dc2096SDima Dorfman 331a1dc2096SDima Dorfman /* 332a1dc2096SDima Dorfman * Apply dk to de and everything under de. 333a1dc2096SDima Dorfman * 334a1dc2096SDima Dorfman * XXX: This method needs a function call for every nested 335a1dc2096SDima Dorfman * subdirectory in a devfs mount. If we plan to have many of these, 336a1dc2096SDima Dorfman * we might eventually run out of kernel stack space. 337e606a3c6SPoul-Henning Kamp * XXX: a linear search could be done through the cdev list instead. 338a1dc2096SDima Dorfman */ 339a1dc2096SDima Dorfman static void 340a1dc2096SDima Dorfman devfs_rule_applyde_recursive(struct devfs_krule *dk, struct devfs_dirent *de) 341a1dc2096SDima Dorfman { 342a1dc2096SDima Dorfman struct devfs_dirent *de2; 343a1dc2096SDima Dorfman 3445e080af4SPoul-Henning Kamp TAILQ_FOREACH(de2, &de->de_dlist, de_list) 345a1dc2096SDima Dorfman devfs_rule_applyde_recursive(dk, de2); 3465e080af4SPoul-Henning Kamp devfs_rule_run(dk, de, devfs_rule_depth); 347a1dc2096SDima Dorfman } 348a1dc2096SDima Dorfman 349a1dc2096SDima Dorfman /* 350a1dc2096SDima Dorfman * Apply dk to all entires in dm. 351a1dc2096SDima Dorfman */ 352a1dc2096SDima Dorfman static void 353a1dc2096SDima Dorfman devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm) 354a1dc2096SDima Dorfman { 355a1dc2096SDima Dorfman 356d785dfefSPoul-Henning Kamp devfs_rule_applyde_recursive(dk, dm->dm_rootdir); 357a1dc2096SDima Dorfman } 358a1dc2096SDima Dorfman 359a1dc2096SDima Dorfman /* 360a1dc2096SDima Dorfman * Automatically select a number for a new rule in ds, and write the 361a1dc2096SDima Dorfman * result into rnump. 362a1dc2096SDima Dorfman */ 363a1dc2096SDima Dorfman static int 364a1dc2096SDima Dorfman devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnump) 365a1dc2096SDima Dorfman { 366a1dc2096SDima Dorfman struct devfs_krule *dk; 367a1dc2096SDima Dorfman 368a1dc2096SDima Dorfman /* Find the last rule. */ 369e515ee78SPoul-Henning Kamp dk = TAILQ_LAST(&ds->ds_rules, rulehead); 370a1dc2096SDima Dorfman if (dk == NULL) 371a1dc2096SDima Dorfman *rnump = 100; 372a1dc2096SDima Dorfman else { 373a1dc2096SDima Dorfman *rnump = rid2rn(dk->dk_rule.dr_id) + 100; 374a1dc2096SDima Dorfman /* Detect overflow. */ 375a1dc2096SDima Dorfman if (*rnump < rid2rn(dk->dk_rule.dr_id)) 376a1dc2096SDima Dorfman return (ERANGE); 377a1dc2096SDima Dorfman } 378a1dc2096SDima Dorfman KASSERT(devfs_rule_byid(mkrid(ds->ds_number, *rnump)) == NULL, 379a1dc2096SDima Dorfman ("autonumbering resulted in an already existing rule")); 380a1dc2096SDima Dorfman return (0); 381a1dc2096SDima Dorfman } 382a1dc2096SDima Dorfman 383a1dc2096SDima Dorfman /* 384a1dc2096SDima Dorfman * Find a krule by id. 385a1dc2096SDima Dorfman */ 386a1dc2096SDima Dorfman static struct devfs_krule * 387a1dc2096SDima Dorfman devfs_rule_byid(devfs_rid rid) 388a1dc2096SDima Dorfman { 389a1dc2096SDima Dorfman struct devfs_ruleset *ds; 390a1dc2096SDima Dorfman struct devfs_krule *dk; 391a1dc2096SDima Dorfman devfs_rnum rn; 392a1dc2096SDima Dorfman 393a1dc2096SDima Dorfman rn = rid2rn(rid); 394a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rid2rsn(rid)); 395a1dc2096SDima Dorfman if (ds == NULL) 396a1dc2096SDima Dorfman return (NULL); 397e515ee78SPoul-Henning Kamp TAILQ_FOREACH(dk, &ds->ds_rules, dk_list) { 398a1dc2096SDima Dorfman if (rid2rn(dk->dk_rule.dr_id) == rn) 399a1dc2096SDima Dorfman return (dk); 400a1dc2096SDima Dorfman else if (rid2rn(dk->dk_rule.dr_id) > rn) 401a1dc2096SDima Dorfman break; 402a1dc2096SDima Dorfman } 403a1dc2096SDima Dorfman return (NULL); 404a1dc2096SDima Dorfman } 405a1dc2096SDima Dorfman 406a1dc2096SDima Dorfman /* 407a1dc2096SDima Dorfman * Remove dkp from any lists it may be on and remove memory associated 408a1dc2096SDima Dorfman * with it. 409a1dc2096SDima Dorfman */ 410a1dc2096SDima Dorfman static int 411e515ee78SPoul-Henning Kamp devfs_rule_delete(struct devfs_krule *dk) 412a1dc2096SDima Dorfman { 413a1dc2096SDima Dorfman struct devfs_ruleset *ds; 414a1dc2096SDima Dorfman 415a1dc2096SDima Dorfman if (dk->dk_rule.dr_iacts & DRA_INCSET) { 416a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset); 417a1dc2096SDima Dorfman KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset")); 418a1dc2096SDima Dorfman --ds->ds_refcount; 419e515ee78SPoul-Henning Kamp devfs_ruleset_reap(ds); 420a1dc2096SDima Dorfman } 421e515ee78SPoul-Henning Kamp ds = dk->dk_ruleset; 422e515ee78SPoul-Henning Kamp TAILQ_REMOVE(&ds->ds_rules, dk, dk_list); 423e515ee78SPoul-Henning Kamp devfs_ruleset_reap(ds); 424e515ee78SPoul-Henning Kamp free(dk, M_DEVFSRULE); 425a1dc2096SDima Dorfman return (0); 426a1dc2096SDima Dorfman } 427a1dc2096SDima Dorfman 428a1dc2096SDima Dorfman /* 42989c9c53dSPoul-Henning Kamp * Get a struct cdev *corresponding to de so we can try to match rules based 43089c9c53dSPoul-Henning Kamp * on it. If this routine returns NULL, there is no struct cdev *associated 431a1dc2096SDima Dorfman * with the dirent (symlinks and directories don't have dev_ts), and 432a1dc2096SDima Dorfman * the caller should assume that any critera dependent on a dev_t 433a1dc2096SDima Dorfman * don't match. 434a1dc2096SDima Dorfman */ 43589c9c53dSPoul-Henning Kamp static struct cdev * 436a1dc2096SDima Dorfman devfs_rule_getdev(struct devfs_dirent *de) 437a1dc2096SDima Dorfman { 438a1dc2096SDima Dorfman 439e606a3c6SPoul-Henning Kamp if (de->de_cdp == NULL) 440e606a3c6SPoul-Henning Kamp return (NULL); 441e606a3c6SPoul-Henning Kamp if (de->de_cdp->cdp_flags & CDP_ACTIVE) 442e606a3c6SPoul-Henning Kamp return (&de->de_cdp->cdp_c); 443a1dc2096SDima Dorfman else 444e606a3c6SPoul-Henning Kamp return (NULL); 445a1dc2096SDima Dorfman } 446a1dc2096SDima Dorfman 447a1dc2096SDima Dorfman /* 448a1dc2096SDima Dorfman * Do what we need to do to a rule that we just loaded from the 449a1dc2096SDima Dorfman * userland. In particular, we need to check the magic, and adjust 450a1dc2096SDima Dorfman * the ruleset appropriate if desired. 451a1dc2096SDima Dorfman */ 452a1dc2096SDima Dorfman static int 453a1dc2096SDima Dorfman devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm) 454a1dc2096SDima Dorfman { 455a1dc2096SDima Dorfman 456a1dc2096SDima Dorfman if (dr->dr_magic != DEVFS_MAGIC) 457a1dc2096SDima Dorfman return (ERPCMISMATCH); 458a1dc2096SDima Dorfman dr->dr_id = devfs_rid_input(dr->dr_id, dm); 459a1dc2096SDima Dorfman return (0); 460a1dc2096SDima Dorfman } 461a1dc2096SDima Dorfman 462a1dc2096SDima Dorfman /* 463a1dc2096SDima Dorfman * Import dr into the appropriate place in the kernel (i.e., make a 464a1dc2096SDima Dorfman * krule). The value of dr is copied, so the pointer may be destroyed 465a1dc2096SDima Dorfman * after this call completes. 466a1dc2096SDima Dorfman */ 467a1dc2096SDima Dorfman static int 468a1dc2096SDima Dorfman devfs_rule_insert(struct devfs_rule *dr) 469a1dc2096SDima Dorfman { 470a1dc2096SDima Dorfman struct devfs_ruleset *ds, *dsi; 471e515ee78SPoul-Henning Kamp struct devfs_krule *k1; 472a1dc2096SDima Dorfman struct devfs_krule *dk; 473a1dc2096SDima Dorfman devfs_rsnum rsnum; 474a1dc2096SDima Dorfman devfs_rnum dkrn; 475a1dc2096SDima Dorfman int error; 476a1dc2096SDima Dorfman 477a1dc2096SDima Dorfman /* 478a1dc2096SDima Dorfman * This stuff seems out of place here, but we want to do it as 479a1dc2096SDima Dorfman * soon as possible so that if it fails, we don't have to roll 480a1dc2096SDima Dorfman * back any changes we already made (e.g., ruleset creation). 481a1dc2096SDima Dorfman */ 482a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_INCSET) { 483a1dc2096SDima Dorfman dsi = devfs_ruleset_bynum(dr->dr_incset); 484a1dc2096SDima Dorfman if (dsi == NULL) 485a1dc2096SDima Dorfman return (ESRCH); 486a1dc2096SDima Dorfman } else 487a1dc2096SDima Dorfman dsi = NULL; 488a1dc2096SDima Dorfman 489a1dc2096SDima Dorfman rsnum = rid2rsn(dr->dr_id); 490e515ee78SPoul-Henning Kamp KASSERT(rsnum != 0, ("Inserting into ruleset zero")); 491e515ee78SPoul-Henning Kamp 492a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rsnum); 493a1dc2096SDima Dorfman if (ds == NULL) 494a1dc2096SDima Dorfman ds = devfs_ruleset_create(rsnum); 495a1dc2096SDima Dorfman dkrn = rid2rn(dr->dr_id); 496a1dc2096SDima Dorfman if (dkrn == 0) { 497a1dc2096SDima Dorfman error = devfs_rule_autonumber(ds, &dkrn); 498e515ee78SPoul-Henning Kamp if (error != 0) { 499e515ee78SPoul-Henning Kamp devfs_ruleset_reap(ds); 500a1dc2096SDima Dorfman return (error); 501a1dc2096SDima Dorfman } 502e515ee78SPoul-Henning Kamp } 503a1dc2096SDima Dorfman 504e515ee78SPoul-Henning Kamp dk = malloc(sizeof(*dk), M_DEVFSRULE, M_WAITOK | M_ZERO); 505a1dc2096SDima Dorfman dk->dk_ruleset = ds; 506a1dc2096SDima Dorfman if (dsi != NULL) 507a1dc2096SDima Dorfman ++dsi->ds_refcount; 508a1dc2096SDima Dorfman /* XXX: Inspect dr? */ 509a1dc2096SDima Dorfman memcpy(&dk->dk_rule, dr, sizeof(*dr)); 510a1dc2096SDima Dorfman dk->dk_rule.dr_id = mkrid(rid2rsn(dk->dk_rule.dr_id), dkrn); 511a1dc2096SDima Dorfman 512e515ee78SPoul-Henning Kamp TAILQ_FOREACH(k1, &ds->ds_rules, dk_list) { 513e515ee78SPoul-Henning Kamp if (rid2rn(k1->dk_rule.dr_id) > dkrn) { 514e515ee78SPoul-Henning Kamp TAILQ_INSERT_BEFORE(k1, dk, dk_list); 515a1dc2096SDima Dorfman break; 516a1dc2096SDima Dorfman } 517a1dc2096SDima Dorfman } 518e515ee78SPoul-Henning Kamp if (k1 == NULL) 519e515ee78SPoul-Henning Kamp TAILQ_INSERT_TAIL(&ds->ds_rules, dk, dk_list); 520a1dc2096SDima Dorfman return (0); 521a1dc2096SDima Dorfman } 522a1dc2096SDima Dorfman 523a1dc2096SDima Dorfman /* 524a1dc2096SDima Dorfman * Determine whether dk matches de. Returns 1 if dk should be run on 525a1dc2096SDima Dorfman * de; 0, otherwise. 526a1dc2096SDima Dorfman */ 527a1dc2096SDima Dorfman static int 528a1dc2096SDima Dorfman devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de) 529a1dc2096SDima Dorfman { 530a1dc2096SDima Dorfman struct devfs_rule *dr = &dk->dk_rule; 53189c9c53dSPoul-Henning Kamp struct cdev *dev; 53291a35e78SKonstantin Belousov struct cdevsw *dsw; 5333979450bSKonstantin Belousov int ref; 534a1dc2096SDima Dorfman 535a1dc2096SDima Dorfman dev = devfs_rule_getdev(de); 536a1dc2096SDima Dorfman /* 537a1dc2096SDima Dorfman * At this point, if dev is NULL, we should assume that any 538a1dc2096SDima Dorfman * criteria that depend on it don't match. We should *not* 539a1dc2096SDima Dorfman * just ignore them (i.e., act like they weren't specified), 540a1dc2096SDima Dorfman * since that makes a rule that only has criteria dependent on 54189c9c53dSPoul-Henning Kamp * the struct cdev *match all symlinks and directories. 542a1dc2096SDima Dorfman * 543a1dc2096SDima Dorfman * Note also that the following tests are somewhat reversed: 544a1dc2096SDima Dorfman * They're actually testing to see whether the condition does 545a1dc2096SDima Dorfman * *not* match, since the default is to assume the rule should 546a1dc2096SDima Dorfman * be run (such as if there are no conditions). 547a1dc2096SDima Dorfman */ 54891a35e78SKonstantin Belousov if (dr->dr_icond & DRC_DSWFLAGS) { 54991a35e78SKonstantin Belousov if (dev == NULL) 5505e080af4SPoul-Henning Kamp return (0); 5513979450bSKonstantin Belousov dsw = dev_refthread(dev, &ref); 55291a35e78SKonstantin Belousov if (dsw == NULL) 55391a35e78SKonstantin Belousov return (0); 55491a35e78SKonstantin Belousov if ((dsw->d_flags & dr->dr_dswflags) == 0) { 5553979450bSKonstantin Belousov dev_relthread(dev, ref); 55691a35e78SKonstantin Belousov return (0); 55791a35e78SKonstantin Belousov } 5583979450bSKonstantin Belousov dev_relthread(dev, ref); 55991a35e78SKonstantin Belousov } 560a1dc2096SDima Dorfman if (dr->dr_icond & DRC_PATHPTRN) 561a1dc2096SDima Dorfman if (!devfs_rule_matchpath(dk, de)) 5625e080af4SPoul-Henning Kamp return (0); 563a1dc2096SDima Dorfman 564a1dc2096SDima Dorfman return (1); 565a1dc2096SDima Dorfman } 566a1dc2096SDima Dorfman 567a1dc2096SDima Dorfman /* 568a1dc2096SDima Dorfman * Determine whether dk matches de on account of dr_pathptrn. 569a1dc2096SDima Dorfman */ 570a1dc2096SDima Dorfman static int 571a1dc2096SDima Dorfman devfs_rule_matchpath(struct devfs_krule *dk, struct devfs_dirent *de) 572a1dc2096SDima Dorfman { 573a1dc2096SDima Dorfman struct devfs_rule *dr = &dk->dk_rule; 574a1dc2096SDima Dorfman char *pname; 57589c9c53dSPoul-Henning Kamp struct cdev *dev; 576a1dc2096SDima Dorfman 577a1dc2096SDima Dorfman dev = devfs_rule_getdev(de); 578a1dc2096SDima Dorfman if (dev != NULL) 579a1dc2096SDima Dorfman pname = dev->si_name; 5809f8ef8b8SColin Percival else if (de->de_dirent->d_type == DT_LNK || 5819f8ef8b8SColin Percival de->de_dirent->d_type == DT_DIR) 582797159bdSDima Dorfman pname = de->de_dirent->d_name; 583a1dc2096SDima Dorfman else 584a1dc2096SDima Dorfman return (0); 585a1dc2096SDima Dorfman KASSERT(pname != NULL, ("devfs_rule_matchpath: NULL pname")); 586a1dc2096SDima Dorfman 587e5d09546SDima Dorfman return (fnmatch(dr->dr_pathptrn, pname, 0) == 0); 588a1dc2096SDima Dorfman } 589a1dc2096SDima Dorfman 590a1dc2096SDima Dorfman /* 591a1dc2096SDima Dorfman * Run dk on de. 592a1dc2096SDima Dorfman */ 593a1dc2096SDima Dorfman static void 5945e080af4SPoul-Henning Kamp devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de, unsigned depth) 595a1dc2096SDima Dorfman { 596a1dc2096SDima Dorfman struct devfs_rule *dr = &dk->dk_rule; 597a1dc2096SDima Dorfman struct devfs_ruleset *ds; 598a1dc2096SDima Dorfman 5995e080af4SPoul-Henning Kamp if (!devfs_rule_match(dk, de)) 6005e080af4SPoul-Henning Kamp return; 601a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_BACTS) { 602a1dc2096SDima Dorfman if (dr->dr_bacts & DRB_HIDE) 603a1dc2096SDima Dorfman de->de_flags |= DE_WHITEOUT; 604a1dc2096SDima Dorfman if (dr->dr_bacts & DRB_UNHIDE) 605a1dc2096SDima Dorfman de->de_flags &= ~DE_WHITEOUT; 606a1dc2096SDima Dorfman } 607a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_UID) 608a1dc2096SDima Dorfman de->de_uid = dr->dr_uid; 609a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_GID) 610a1dc2096SDima Dorfman de->de_gid = dr->dr_gid; 611a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_MODE) 612a1dc2096SDima Dorfman de->de_mode = dr->dr_mode; 613a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_INCSET) { 6145e080af4SPoul-Henning Kamp /* 6155e080af4SPoul-Henning Kamp * XXX: we should tell the user if the depth is exceeded here 6165e080af4SPoul-Henning Kamp * XXX: but it is not obvious how to. A return value will 6175e080af4SPoul-Henning Kamp * XXX: not work as this is called when devices are created 6185e080af4SPoul-Henning Kamp * XXX: long time after the rules were instantiated. 6195e080af4SPoul-Henning Kamp * XXX: a printf() would probably give too much noise, or 6205e080af4SPoul-Henning Kamp * XXX: DoS the machine. I guess a a rate-limited message 6215e080af4SPoul-Henning Kamp * XXX: might work. 6225e080af4SPoul-Henning Kamp */ 6235e080af4SPoul-Henning Kamp if (depth > 0) { 624a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset); 625a1dc2096SDima Dorfman KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset")); 6265e080af4SPoul-Henning Kamp devfs_ruleset_applyde(ds, de, depth - 1); 6275e080af4SPoul-Henning Kamp } 628a1dc2096SDima Dorfman } 629a1dc2096SDima Dorfman } 630a1dc2096SDima Dorfman 631a1dc2096SDima Dorfman /* 632a1dc2096SDima Dorfman * Apply all the rules in ds to de. 633a1dc2096SDima Dorfman */ 634a1dc2096SDima Dorfman static void 6355e080af4SPoul-Henning Kamp devfs_ruleset_applyde(struct devfs_ruleset *ds, struct devfs_dirent *de, unsigned depth) 636a1dc2096SDima Dorfman { 637a1dc2096SDima Dorfman struct devfs_krule *dk; 638a1dc2096SDima Dorfman 639e515ee78SPoul-Henning Kamp TAILQ_FOREACH(dk, &ds->ds_rules, dk_list) 6405e080af4SPoul-Henning Kamp devfs_rule_run(dk, de, depth); 641a1dc2096SDima Dorfman } 642a1dc2096SDima Dorfman 643a1dc2096SDima Dorfman /* 644a1dc2096SDima Dorfman * Apply all the rules in ds to all the entires in dm. 645a1dc2096SDima Dorfman */ 646a1dc2096SDima Dorfman static void 647a1dc2096SDima Dorfman devfs_ruleset_applydm(struct devfs_ruleset *ds, struct devfs_mount *dm) 648a1dc2096SDima Dorfman { 649a1dc2096SDima Dorfman struct devfs_krule *dk; 650a1dc2096SDima Dorfman 651a1dc2096SDima Dorfman /* 652a1dc2096SDima Dorfman * XXX: Does it matter whether we do 653a1dc2096SDima Dorfman * 654a1dc2096SDima Dorfman * foreach(dk in ds) 655a1dc2096SDima Dorfman * foreach(de in dm) 656a1dc2096SDima Dorfman * apply(dk to de) 657a1dc2096SDima Dorfman * 658a1dc2096SDima Dorfman * as opposed to 659a1dc2096SDima Dorfman * 660a1dc2096SDima Dorfman * foreach(de in dm) 661a1dc2096SDima Dorfman * foreach(dk in ds) 662a1dc2096SDima Dorfman * apply(dk to de) 663a1dc2096SDima Dorfman * 664a1dc2096SDima Dorfman * The end result is obviously the same, but does the order 665a1dc2096SDima Dorfman * matter? 666a1dc2096SDima Dorfman */ 667e515ee78SPoul-Henning Kamp TAILQ_FOREACH(dk, &ds->ds_rules, dk_list) 668a1dc2096SDima Dorfman devfs_rule_applydm(dk, dm); 669a1dc2096SDima Dorfman } 670a1dc2096SDima Dorfman 671a1dc2096SDima Dorfman /* 672a1dc2096SDima Dorfman * Find a ruleset by number. 673a1dc2096SDima Dorfman */ 674a1dc2096SDima Dorfman static struct devfs_ruleset * 675a1dc2096SDima Dorfman devfs_ruleset_bynum(devfs_rsnum rsnum) 676a1dc2096SDima Dorfman { 677a1dc2096SDima Dorfman struct devfs_ruleset *ds; 678a1dc2096SDima Dorfman 679e515ee78SPoul-Henning Kamp TAILQ_FOREACH(ds, &devfs_rulesets, ds_list) { 680a1dc2096SDima Dorfman if (ds->ds_number == rsnum) 681a1dc2096SDima Dorfman return (ds); 682a1dc2096SDima Dorfman } 683a1dc2096SDima Dorfman return (NULL); 684a1dc2096SDima Dorfman } 685a1dc2096SDima Dorfman 686a1dc2096SDima Dorfman /* 687a1dc2096SDima Dorfman * Create a new ruleset. 688a1dc2096SDima Dorfman */ 689a1dc2096SDima Dorfman static struct devfs_ruleset * 690a1dc2096SDima Dorfman devfs_ruleset_create(devfs_rsnum rsnum) 691a1dc2096SDima Dorfman { 692e515ee78SPoul-Henning Kamp struct devfs_ruleset *s1; 693a1dc2096SDima Dorfman struct devfs_ruleset *ds; 694a1dc2096SDima Dorfman 695e515ee78SPoul-Henning Kamp KASSERT(rsnum != 0, ("creating ruleset zero")); 696e515ee78SPoul-Henning Kamp 697a1dc2096SDima Dorfman KASSERT(devfs_ruleset_bynum(rsnum) == NULL, 698a1dc2096SDima Dorfman ("creating already existent ruleset %d", rsnum)); 699a1dc2096SDima Dorfman 700e515ee78SPoul-Henning Kamp ds = malloc(sizeof(*ds), M_DEVFSRULE, M_WAITOK | M_ZERO); 701a1dc2096SDima Dorfman ds->ds_number = rsnum; 702e515ee78SPoul-Henning Kamp TAILQ_INIT(&ds->ds_rules); 703a1dc2096SDima Dorfman 704e515ee78SPoul-Henning Kamp TAILQ_FOREACH(s1, &devfs_rulesets, ds_list) { 705e515ee78SPoul-Henning Kamp if (s1->ds_number > rsnum) { 706e515ee78SPoul-Henning Kamp TAILQ_INSERT_BEFORE(s1, ds, ds_list); 707a1dc2096SDima Dorfman break; 708a1dc2096SDima Dorfman } 709a1dc2096SDima Dorfman } 710e515ee78SPoul-Henning Kamp if (s1 == NULL) 711e515ee78SPoul-Henning Kamp TAILQ_INSERT_TAIL(&devfs_rulesets, ds, ds_list); 712a1dc2096SDima Dorfman return (ds); 713a1dc2096SDima Dorfman } 714a1dc2096SDima Dorfman 715a1dc2096SDima Dorfman /* 716a1dc2096SDima Dorfman * Remove a ruleset from the system if it's empty and not used 717a1dc2096SDima Dorfman * anywhere. This should be called after every time a rule is deleted 718a1dc2096SDima Dorfman * from this ruleset or the reference count is decremented. 719a1dc2096SDima Dorfman */ 720a1dc2096SDima Dorfman static void 721e515ee78SPoul-Henning Kamp devfs_ruleset_reap(struct devfs_ruleset *ds) 722a1dc2096SDima Dorfman { 723a1dc2096SDima Dorfman 724e515ee78SPoul-Henning Kamp KASSERT(ds->ds_number != 0, ("reaping ruleset zero ")); 725e515ee78SPoul-Henning Kamp 726e515ee78SPoul-Henning Kamp if (!TAILQ_EMPTY(&ds->ds_rules) || ds->ds_refcount != 0) 727e515ee78SPoul-Henning Kamp return; 728e515ee78SPoul-Henning Kamp 729e515ee78SPoul-Henning Kamp TAILQ_REMOVE(&devfs_rulesets, ds, ds_list); 730e515ee78SPoul-Henning Kamp free(ds, M_DEVFSRULE); 731a1dc2096SDima Dorfman } 732a1dc2096SDima Dorfman 733a1dc2096SDima Dorfman /* 734a1dc2096SDima Dorfman * Make rsnum the active ruleset for dm. 735a1dc2096SDima Dorfman */ 736a1dc2096SDima Dorfman static int 737a1dc2096SDima Dorfman devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm) 738a1dc2096SDima Dorfman { 739a1dc2096SDima Dorfman struct devfs_ruleset *cds, *ds; 740a1dc2096SDima Dorfman 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 7472f66e90fSJaakko Heinonen if (rsnum == 0) { 7482f66e90fSJaakko Heinonen dm->dm_ruleset = 0; 7492f66e90fSJaakko Heinonen return (0); 7502f66e90fSJaakko Heinonen } 7512f66e90fSJaakko Heinonen 7522da528a7SOleksandr Tymoshenko ds = devfs_ruleset_bynum(rsnum); 7532da528a7SOleksandr Tymoshenko if (ds == NULL) 7542da528a7SOleksandr Tymoshenko ds = devfs_ruleset_create(rsnum); 755a1dc2096SDima Dorfman /* These should probably be made atomic somehow. */ 756a1dc2096SDima Dorfman ++ds->ds_refcount; 757a1dc2096SDima Dorfman dm->dm_ruleset = rsnum; 758a1dc2096SDima Dorfman 759a1dc2096SDima Dorfman return (0); 760a1dc2096SDima Dorfman } 761e515ee78SPoul-Henning Kamp 762e515ee78SPoul-Henning Kamp void 763e515ee78SPoul-Henning Kamp devfs_rules_cleanup(struct devfs_mount *dm) 764e515ee78SPoul-Henning Kamp { 765e515ee78SPoul-Henning Kamp struct devfs_ruleset *ds; 766e515ee78SPoul-Henning Kamp 767e515ee78SPoul-Henning Kamp sx_assert(&dm->dm_lock, SX_XLOCKED); 768e515ee78SPoul-Henning Kamp if (dm->dm_ruleset != 0) { 769e515ee78SPoul-Henning Kamp ds = devfs_ruleset_bynum(dm->dm_ruleset); 770e515ee78SPoul-Henning Kamp --ds->ds_refcount; 771e515ee78SPoul-Henning Kamp devfs_ruleset_reap(ds); 772e515ee78SPoul-Henning Kamp } 773e515ee78SPoul-Henning Kamp } 774