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 "opt_devfs.h" 66a1dc2096SDima Dorfman 67a1dc2096SDima Dorfman #include <sys/param.h> 68a1dc2096SDima Dorfman #include <sys/systm.h> 69a1dc2096SDima Dorfman #include <sys/conf.h> 70a1dc2096SDima Dorfman #include <sys/kernel.h> 71a1dc2096SDima Dorfman #include <sys/malloc.h> 72a1dc2096SDima Dorfman #include <sys/dirent.h> 73a1dc2096SDima Dorfman #include <sys/ioccom.h> 74e606a3c6SPoul-Henning Kamp #include <sys/lock.h> 756556102dSPoul-Henning Kamp #include <sys/sx.h> 76a1dc2096SDima Dorfman 77a1dc2096SDima Dorfman #include <fs/devfs/devfs.h> 78e606a3c6SPoul-Henning Kamp #include <fs/devfs/devfs_int.h> 79a1dc2096SDima Dorfman 80a1dc2096SDima Dorfman /* 81a1dc2096SDima Dorfman * Kernel version of devfs_rule. 82a1dc2096SDima Dorfman */ 83a1dc2096SDima Dorfman struct devfs_krule { 84a1dc2096SDima Dorfman SLIST_ENTRY(devfs_krule) dk_list; 85a1dc2096SDima Dorfman struct devfs_ruleset *dk_ruleset; 86a1dc2096SDima Dorfman struct devfs_rule dk_rule; 87a1dc2096SDima Dorfman }; 88a1dc2096SDima Dorfman 89a1dc2096SDima Dorfman /* 90a1dc2096SDima Dorfman * Structure to describe a ruleset. 91a1dc2096SDima Dorfman */ 92a1dc2096SDima Dorfman struct devfs_ruleset { 93a1dc2096SDima Dorfman SLIST_ENTRY(devfs_ruleset) ds_list; 94a1dc2096SDima Dorfman devfs_rsnum ds_number; 95a1dc2096SDima Dorfman SLIST_HEAD(, devfs_krule) ds_rules; 96a1dc2096SDima Dorfman int ds_refcount; 97a1dc2096SDima Dorfman int ds_flags; 98a1dc2096SDima Dorfman #define DS_IMMUTABLE 0x001 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); 108a1dc2096SDima Dorfman 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); 123a1dc2096SDima Dorfman static void devfs_ruleset_destroy(struct devfs_ruleset **dsp); 124a1dc2096SDima Dorfman static void devfs_ruleset_reap(struct devfs_ruleset **dsp); 125a1dc2096SDima Dorfman static int devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm); 126a1dc2096SDima Dorfman 1276556102dSPoul-Henning Kamp static struct sx sx_rules; 128a1dc2096SDima Dorfman static SLIST_HEAD(, devfs_ruleset) devfs_rulesets; 129a1dc2096SDima Dorfman 130a1dc2096SDima Dorfman /* 1315e080af4SPoul-Henning Kamp * Called to apply the proper rules for 'de' before it can be 132a1dc2096SDima Dorfman * exposed to the userland. This should be called with an exclusive 133a1dc2096SDima Dorfman * lock on dm in case we need to run anything. 134a1dc2096SDima Dorfman */ 135a1dc2096SDima Dorfman void 136a1dc2096SDima Dorfman devfs_rules_apply(struct devfs_mount *dm, struct devfs_dirent *de) 137a1dc2096SDima Dorfman { 138a1dc2096SDima Dorfman struct devfs_ruleset *ds; 139a1dc2096SDima Dorfman 1406556102dSPoul-Henning Kamp sx_slock(&sx_rules); 141a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(dm->dm_ruleset); 142a1dc2096SDima Dorfman KASSERT(ds != NULL, ("mount-point has NULL ruleset")); 1435e080af4SPoul-Henning Kamp devfs_ruleset_applyde(ds, de, devfs_rule_depth); 1446556102dSPoul-Henning Kamp sx_sunlock(&sx_rules); 145a1dc2096SDima Dorfman } 146a1dc2096SDima Dorfman 147a1dc2096SDima Dorfman /* 148a1dc2096SDima Dorfman * Rule subsystem SYSINIT hook. 149a1dc2096SDima Dorfman */ 150f82dfde7SPoul-Henning Kamp static void 151f82dfde7SPoul-Henning Kamp devfs_rules_init(void *junk __unused) 152a1dc2096SDima Dorfman { 153a1dc2096SDima Dorfman struct devfs_ruleset *ds; 154a1dc2096SDima Dorfman 1556556102dSPoul-Henning Kamp sx_init(&sx_rules, "devfsrules"); 156a1dc2096SDima Dorfman SLIST_INIT(&devfs_rulesets); 157a1dc2096SDima Dorfman 158a1dc2096SDima Dorfman ds = devfs_ruleset_create(0); 159a1dc2096SDima Dorfman ds->ds_flags |= DS_IMMUTABLE; 160a1dc2096SDima Dorfman ds->ds_refcount = 1; /* Prevent reaping. */ 161a1dc2096SDima Dorfman } 162a1dc2096SDima Dorfman 163f82dfde7SPoul-Henning Kamp SYSINIT(devfs_rules, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_rules_init, NULL); 164f82dfde7SPoul-Henning Kamp 165a1dc2096SDima Dorfman /* 166a1dc2096SDima Dorfman * Rule subsystem ioctl hook. 167a1dc2096SDima Dorfman */ 168a1dc2096SDima Dorfman int 169ab32e952SPoul-Henning Kamp devfs_rules_ioctl(struct devfs_mount *dm, u_long cmd, caddr_t data, struct thread *td) 170a1dc2096SDima Dorfman { 171a1dc2096SDima Dorfman struct devfs_ruleset *ds; 172a1dc2096SDima Dorfman struct devfs_krule *dk; 173a1dc2096SDima Dorfman struct devfs_rule *dr; 174a1dc2096SDima Dorfman devfs_rsnum rsnum; 175a1dc2096SDima Dorfman devfs_rnum rnum; 176a1dc2096SDima Dorfman devfs_rid rid; 177a1dc2096SDima Dorfman int error; 178a1dc2096SDima Dorfman 179e606a3c6SPoul-Henning Kamp sx_assert(&dm->dm_lock, SX_XLOCKED); 180e606a3c6SPoul-Henning Kamp 181a1dc2096SDima Dorfman /* 182a1dc2096SDima Dorfman * XXX: This returns an error regardless of whether we 183a1dc2096SDima Dorfman * actually support the cmd or not. 184a1dc2096SDima Dorfman */ 185a1dc2096SDima Dorfman error = suser(td); 186a1dc2096SDima Dorfman if (error != 0) 187a1dc2096SDima Dorfman return (error); 188a1dc2096SDima Dorfman 1896556102dSPoul-Henning Kamp sx_xlock(&sx_rules); 190e606a3c6SPoul-Henning Kamp 191a1dc2096SDima Dorfman switch (cmd) { 192a1dc2096SDima Dorfman case DEVFSIO_RADD: 193a1dc2096SDima Dorfman dr = (struct devfs_rule *)data; 194a1dc2096SDima Dorfman error = devfs_rule_input(dr, dm); 195a1dc2096SDima Dorfman if (error != 0) 196a1dc2096SDima Dorfman goto out; 197a1dc2096SDima Dorfman dk = devfs_rule_byid(dr->dr_id); 198a1dc2096SDima Dorfman if (dk != NULL) { 199a1dc2096SDima Dorfman error = EEXIST; 200a1dc2096SDima Dorfman goto out; 201a1dc2096SDima Dorfman } 202a1dc2096SDima Dorfman error = devfs_rule_insert(dr); 203a1dc2096SDima Dorfman break; 204a1dc2096SDima Dorfman case DEVFSIO_RAPPLY: 205a1dc2096SDima Dorfman dr = (struct devfs_rule *)data; 206a1dc2096SDima Dorfman error = devfs_rule_input(dr, dm); 207a1dc2096SDima Dorfman if (error != 0) 208a1dc2096SDima Dorfman goto out; 209a1dc2096SDima Dorfman 210a1dc2096SDima Dorfman /* 211a1dc2096SDima Dorfman * This is one of many possible hackish 212a1dc2096SDima Dorfman * implementations. The primary contender is an 213a1dc2096SDima Dorfman * implementation where the rule we read in is 214a1dc2096SDima Dorfman * temporarily inserted into some ruleset, perhaps 215a1dc2096SDima Dorfman * with a hypothetical DRO_NOAUTO flag so that it 216a1dc2096SDima Dorfman * doesn't get used where it isn't intended, and 217a1dc2096SDima Dorfman * applied in the normal way. This can be done in the 218a1dc2096SDima Dorfman * userland (DEVFSIO_ADD, DEVFSIO_APPLYID, 219a1dc2096SDima Dorfman * DEVFSIO_DEL) or in the kernel; either way it breaks 220a1dc2096SDima Dorfman * some corner case assumptions in other parts of the 221a1dc2096SDima Dorfman * code (not that this implementation doesn't do 222a1dc2096SDima Dorfman * that). 223a1dc2096SDima Dorfman */ 224a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_INCSET && 225a1dc2096SDima Dorfman devfs_ruleset_bynum(dr->dr_incset) == NULL) { 226a1dc2096SDima Dorfman error = ESRCH; 227a1dc2096SDima Dorfman goto out; 228a1dc2096SDima Dorfman } 229a163d034SWarner Losh dk = malloc(sizeof(*dk), M_TEMP, M_WAITOK | M_ZERO); 230a1dc2096SDima Dorfman memcpy(&dk->dk_rule, dr, sizeof(*dr)); 231a1dc2096SDima Dorfman devfs_rule_applydm(dk, dm); 232a1dc2096SDima Dorfman free(dk, M_TEMP); 233a1dc2096SDima Dorfman error = 0; 234a1dc2096SDima Dorfman break; 235a1dc2096SDima Dorfman case DEVFSIO_RAPPLYID: 236a1dc2096SDima Dorfman rid = *(devfs_rid *)data; 237a1dc2096SDima Dorfman rid = devfs_rid_input(rid, dm); 238a1dc2096SDima Dorfman dk = devfs_rule_byid(rid); 239a1dc2096SDima Dorfman if (dk == NULL) { 240a1dc2096SDima Dorfman error = ENOENT; 241a1dc2096SDima Dorfman goto out; 242a1dc2096SDima Dorfman } 243a1dc2096SDima Dorfman devfs_rule_applydm(dk, dm); 244a1dc2096SDima Dorfman error = 0; 245a1dc2096SDima Dorfman break; 246a1dc2096SDima Dorfman case DEVFSIO_RDEL: 247a1dc2096SDima Dorfman rid = *(devfs_rid *)data; 248a1dc2096SDima Dorfman rid = devfs_rid_input(rid, dm); 249a1dc2096SDima Dorfman dk = devfs_rule_byid(rid); 250a1dc2096SDima Dorfman if (dk == NULL) { 251a1dc2096SDima Dorfman error = ENOENT; 252a1dc2096SDima Dorfman goto out; 253a1dc2096SDima Dorfman } 254a1dc2096SDima Dorfman ds = dk->dk_ruleset; 255a1dc2096SDima Dorfman error = devfs_rule_delete(&dk); 256a1dc2096SDima Dorfman devfs_ruleset_reap(&ds); 257a1dc2096SDima Dorfman break; 258a1dc2096SDima Dorfman case DEVFSIO_RGETNEXT: 259a1dc2096SDima Dorfman dr = (struct devfs_rule *)data; 260a1dc2096SDima Dorfman error = devfs_rule_input(dr, dm); 261a1dc2096SDima Dorfman if (error != 0) 262a1dc2096SDima Dorfman goto out; 263a1dc2096SDima Dorfman /* 264a1dc2096SDima Dorfman * We can't use devfs_rule_byid() here since that 265a1dc2096SDima Dorfman * requires the rule specified to exist, but we want 266a1dc2096SDima Dorfman * getnext(N) to work whether there is a rule N or not 267a1dc2096SDima Dorfman * (specifically, getnext(0) must work, but we should 268a1dc2096SDima Dorfman * never have a rule 0 since the add command 269a1dc2096SDima Dorfman * interprets 0 to mean "auto-number"). 270a1dc2096SDima Dorfman */ 271a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rid2rsn(dr->dr_id)); 272a1dc2096SDima Dorfman if (ds == NULL) { 273a1dc2096SDima Dorfman error = ENOENT; 274a1dc2096SDima Dorfman goto out; 275a1dc2096SDima Dorfman } 276a1dc2096SDima Dorfman rnum = rid2rn(dr->dr_id); 277a1dc2096SDima Dorfman SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 278a1dc2096SDima Dorfman if (rid2rn(dk->dk_rule.dr_id) > rnum) 279a1dc2096SDima Dorfman break; 280a1dc2096SDima Dorfman } 281a1dc2096SDima Dorfman if (dk == NULL) { 282a1dc2096SDima Dorfman error = ENOENT; 283a1dc2096SDima Dorfman goto out; 284a1dc2096SDima Dorfman } 285a1dc2096SDima Dorfman memcpy(dr, &dk->dk_rule, sizeof(*dr)); 286a1dc2096SDima Dorfman error = 0; 287a1dc2096SDima Dorfman break; 288a1dc2096SDima Dorfman case DEVFSIO_SUSE: 289a1dc2096SDima Dorfman rsnum = *(devfs_rsnum *)data; 290a1dc2096SDima Dorfman error = devfs_ruleset_use(rsnum, dm); 291a1dc2096SDima Dorfman break; 292a1dc2096SDima Dorfman case DEVFSIO_SAPPLY: 293a1dc2096SDima Dorfman rsnum = *(devfs_rsnum *)data; 294a1dc2096SDima Dorfman rsnum = rid2rsn(devfs_rid_input(mkrid(rsnum, 0), dm)); 295a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rsnum); 296a1dc2096SDima Dorfman if (ds == NULL) { 297a1dc2096SDima Dorfman error = ESRCH; 298a1dc2096SDima Dorfman goto out; 299a1dc2096SDima Dorfman } 300a1dc2096SDima Dorfman devfs_ruleset_applydm(ds, dm); 301a1dc2096SDima Dorfman error = 0; 302a1dc2096SDima Dorfman break; 303a1dc2096SDima Dorfman case DEVFSIO_SGETNEXT: 304a1dc2096SDima Dorfman rsnum = *(devfs_rsnum *)data; 305a1dc2096SDima Dorfman SLIST_FOREACH(ds, &devfs_rulesets, ds_list) { 306a1dc2096SDima Dorfman if (ds->ds_number > rsnum) 307a1dc2096SDima Dorfman break; 308a1dc2096SDima Dorfman } 309a1dc2096SDima Dorfman if (ds == NULL) 310a1dc2096SDima Dorfman error = ENOENT; 311a1dc2096SDima Dorfman else { 312a1dc2096SDima Dorfman *(devfs_rsnum *)data = ds->ds_number; 313a1dc2096SDima Dorfman error = 0; 314a1dc2096SDima Dorfman } 315a1dc2096SDima Dorfman break; 316a1dc2096SDima Dorfman default: 317a1dc2096SDima Dorfman error = ENOIOCTL; 318a1dc2096SDima Dorfman break; 319a1dc2096SDima Dorfman } 320a1dc2096SDima Dorfman 321a1dc2096SDima Dorfman out: 3226556102dSPoul-Henning Kamp sx_xunlock(&sx_rules); 323a1dc2096SDima Dorfman return (error); 324a1dc2096SDima Dorfman } 325a1dc2096SDima Dorfman 326a1dc2096SDima Dorfman /* 327a1dc2096SDima Dorfman * Called to initialize dm_ruleset when there is a new mount-point. 328a1dc2096SDima Dorfman */ 329a1dc2096SDima Dorfman void 330a1dc2096SDima Dorfman devfs_rules_newmount(struct devfs_mount *dm, struct thread *td) 331a1dc2096SDima Dorfman { 332a1dc2096SDima Dorfman struct devfs_ruleset *ds; 333a1dc2096SDima Dorfman 334a1dc2096SDima Dorfman /* 335a1dc2096SDima Dorfman * We can't use devfs_ruleset_use() since it will try to 336a1dc2096SDima Dorfman * decrement the refcount for the old ruleset, and there is no 337a1dc2096SDima Dorfman * old ruleset. Making some value of ds_ruleset "special" to 338a1dc2096SDima Dorfman * mean "don't decrement refcount" is uglier than this. 339a1dc2096SDima Dorfman */ 3406556102dSPoul-Henning Kamp sx_slock(&sx_rules); 341a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(0); 342a1dc2096SDima Dorfman KASSERT(ds != NULL, ("no ruleset 0")); 343a1dc2096SDima Dorfman ++ds->ds_refcount; 344a1dc2096SDima Dorfman dm->dm_ruleset = 0; 3456556102dSPoul-Henning Kamp sx_sunlock(&sx_rules); 346a1dc2096SDima Dorfman } 347a1dc2096SDima Dorfman 348a1dc2096SDima Dorfman /* 349a1dc2096SDima Dorfman * Adjust the rule identifier to use the ruleset of dm if one isn't 350a1dc2096SDima Dorfman * explicitly specified. 351a1dc2096SDima Dorfman * 352a1dc2096SDima Dorfman * Note that after this operation, rid2rsn(rid) might still be 0, and 353a1dc2096SDima Dorfman * that's okay; ruleset 0 is a valid ruleset, but when it's read in 354a1dc2096SDima Dorfman * from the userland, it means "current ruleset for this mount-point". 355a1dc2096SDima Dorfman */ 356a1dc2096SDima Dorfman static devfs_rid 357a1dc2096SDima Dorfman devfs_rid_input(devfs_rid rid, struct devfs_mount *dm) 358a1dc2096SDima Dorfman { 359a1dc2096SDima Dorfman 360a1dc2096SDima Dorfman if (rid2rsn(rid) == 0) 361a1dc2096SDima Dorfman return (mkrid(dm->dm_ruleset, rid2rn(rid))); 362a1dc2096SDima Dorfman else 363a1dc2096SDima Dorfman return (rid); 364a1dc2096SDima Dorfman } 365a1dc2096SDima Dorfman 366a1dc2096SDima Dorfman /* 367a1dc2096SDima Dorfman * Apply dk to de and everything under de. 368a1dc2096SDima Dorfman * 369a1dc2096SDima Dorfman * XXX: This method needs a function call for every nested 370a1dc2096SDima Dorfman * subdirectory in a devfs mount. If we plan to have many of these, 371a1dc2096SDima Dorfman * we might eventually run out of kernel stack space. 372e606a3c6SPoul-Henning Kamp * XXX: a linear search could be done through the cdev list instead. 373a1dc2096SDima Dorfman */ 374a1dc2096SDima Dorfman static void 375a1dc2096SDima Dorfman devfs_rule_applyde_recursive(struct devfs_krule *dk, struct devfs_dirent *de) 376a1dc2096SDima Dorfman { 377a1dc2096SDima Dorfman struct devfs_dirent *de2; 378a1dc2096SDima Dorfman 3795e080af4SPoul-Henning Kamp TAILQ_FOREACH(de2, &de->de_dlist, de_list) 380a1dc2096SDima Dorfman devfs_rule_applyde_recursive(dk, de2); 3815e080af4SPoul-Henning Kamp devfs_rule_run(dk, de, devfs_rule_depth); 382a1dc2096SDima Dorfman } 383a1dc2096SDima Dorfman 384a1dc2096SDima Dorfman /* 385a1dc2096SDima Dorfman * Apply dk to all entires in dm. 386a1dc2096SDima Dorfman */ 387a1dc2096SDima Dorfman static void 388a1dc2096SDima Dorfman devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm) 389a1dc2096SDima Dorfman { 390a1dc2096SDima Dorfman 391d785dfefSPoul-Henning Kamp devfs_rule_applyde_recursive(dk, dm->dm_rootdir); 392a1dc2096SDima Dorfman } 393a1dc2096SDima Dorfman 394a1dc2096SDima Dorfman /* 395a1dc2096SDima Dorfman * Automatically select a number for a new rule in ds, and write the 396a1dc2096SDima Dorfman * result into rnump. 397a1dc2096SDima Dorfman */ 398a1dc2096SDima Dorfman static int 399a1dc2096SDima Dorfman devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnump) 400a1dc2096SDima Dorfman { 401a1dc2096SDima Dorfman struct devfs_krule *dk; 402a1dc2096SDima Dorfman 403a1dc2096SDima Dorfman /* Find the last rule. */ 404a1dc2096SDima Dorfman SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 405a1dc2096SDima Dorfman if (SLIST_NEXT(dk, dk_list) == NULL) 406a1dc2096SDima Dorfman break; 407a1dc2096SDima Dorfman } 408a1dc2096SDima Dorfman if (dk == NULL) 409a1dc2096SDima Dorfman *rnump = 100; 410a1dc2096SDima Dorfman else { 411a1dc2096SDima Dorfman *rnump = rid2rn(dk->dk_rule.dr_id) + 100; 412a1dc2096SDima Dorfman /* Detect overflow. */ 413a1dc2096SDima Dorfman if (*rnump < rid2rn(dk->dk_rule.dr_id)) 414a1dc2096SDima Dorfman return (ERANGE); 415a1dc2096SDima Dorfman } 416a1dc2096SDima Dorfman KASSERT(devfs_rule_byid(mkrid(ds->ds_number, *rnump)) == NULL, 417a1dc2096SDima Dorfman ("autonumbering resulted in an already existing rule")); 418a1dc2096SDima Dorfman return (0); 419a1dc2096SDima Dorfman } 420a1dc2096SDima Dorfman 421a1dc2096SDima Dorfman /* 422a1dc2096SDima Dorfman * Find a krule by id. 423a1dc2096SDima Dorfman */ 424a1dc2096SDima Dorfman static struct devfs_krule * 425a1dc2096SDima Dorfman devfs_rule_byid(devfs_rid rid) 426a1dc2096SDima Dorfman { 427a1dc2096SDima Dorfman struct devfs_ruleset *ds; 428a1dc2096SDima Dorfman struct devfs_krule *dk; 429a1dc2096SDima Dorfman devfs_rnum rn; 430a1dc2096SDima Dorfman 431a1dc2096SDima Dorfman rn = rid2rn(rid); 432a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rid2rsn(rid)); 433a1dc2096SDima Dorfman if (ds == NULL) 434a1dc2096SDima Dorfman return (NULL); 435a1dc2096SDima Dorfman SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 436a1dc2096SDima Dorfman if (rid2rn(dk->dk_rule.dr_id) == rn) 437a1dc2096SDima Dorfman return (dk); 438a1dc2096SDima Dorfman else if (rid2rn(dk->dk_rule.dr_id) > rn) 439a1dc2096SDima Dorfman break; 440a1dc2096SDima Dorfman } 441a1dc2096SDima Dorfman return (NULL); 442a1dc2096SDima Dorfman } 443a1dc2096SDima Dorfman 444a1dc2096SDima Dorfman /* 445a1dc2096SDima Dorfman * Remove dkp from any lists it may be on and remove memory associated 446a1dc2096SDima Dorfman * with it. 447a1dc2096SDima Dorfman */ 448a1dc2096SDima Dorfman static int 449a1dc2096SDima Dorfman devfs_rule_delete(struct devfs_krule **dkp) 450a1dc2096SDima Dorfman { 451a1dc2096SDima Dorfman struct devfs_krule *dk = *dkp; 452a1dc2096SDima Dorfman struct devfs_ruleset *ds; 453a1dc2096SDima Dorfman 454a1dc2096SDima Dorfman if (dk->dk_rule.dr_iacts & DRA_INCSET) { 455a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset); 456a1dc2096SDima Dorfman KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset")); 457a1dc2096SDima Dorfman --ds->ds_refcount; 458a1dc2096SDima Dorfman devfs_ruleset_reap(&ds); 459a1dc2096SDima Dorfman } 460a1dc2096SDima Dorfman SLIST_REMOVE(&dk->dk_ruleset->ds_rules, dk, devfs_krule, dk_list); 461a1dc2096SDima Dorfman free(dk, M_DEVFS); 462a1dc2096SDima Dorfman *dkp = NULL; 463a1dc2096SDima Dorfman return (0); 464a1dc2096SDima Dorfman } 465a1dc2096SDima Dorfman 466a1dc2096SDima Dorfman /* 46789c9c53dSPoul-Henning Kamp * Get a struct cdev *corresponding to de so we can try to match rules based 46889c9c53dSPoul-Henning Kamp * on it. If this routine returns NULL, there is no struct cdev *associated 469a1dc2096SDima Dorfman * with the dirent (symlinks and directories don't have dev_ts), and 470a1dc2096SDima Dorfman * the caller should assume that any critera dependent on a dev_t 471a1dc2096SDima Dorfman * don't match. 472a1dc2096SDima Dorfman */ 47389c9c53dSPoul-Henning Kamp static struct cdev * 474a1dc2096SDima Dorfman devfs_rule_getdev(struct devfs_dirent *de) 475a1dc2096SDima Dorfman { 476a1dc2096SDima Dorfman 477e606a3c6SPoul-Henning Kamp if (de->de_cdp == NULL) 478e606a3c6SPoul-Henning Kamp return (NULL); 479e606a3c6SPoul-Henning Kamp if (de->de_cdp->cdp_flags & CDP_ACTIVE) 480e606a3c6SPoul-Henning Kamp return (&de->de_cdp->cdp_c); 481a1dc2096SDima Dorfman else 482e606a3c6SPoul-Henning Kamp return (NULL); 483a1dc2096SDima Dorfman } 484a1dc2096SDima Dorfman 485a1dc2096SDima Dorfman /* 486a1dc2096SDima Dorfman * Do what we need to do to a rule that we just loaded from the 487a1dc2096SDima Dorfman * userland. In particular, we need to check the magic, and adjust 488a1dc2096SDima Dorfman * the ruleset appropriate if desired. 489a1dc2096SDima Dorfman */ 490a1dc2096SDima Dorfman static int 491a1dc2096SDima Dorfman devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm) 492a1dc2096SDima Dorfman { 493a1dc2096SDima Dorfman 494a1dc2096SDima Dorfman if (dr->dr_magic != DEVFS_MAGIC) 495a1dc2096SDima Dorfman return (ERPCMISMATCH); 496a1dc2096SDima Dorfman dr->dr_id = devfs_rid_input(dr->dr_id, dm); 497a1dc2096SDima Dorfman return (0); 498a1dc2096SDima Dorfman } 499a1dc2096SDima Dorfman 500a1dc2096SDima Dorfman /* 501a1dc2096SDima Dorfman * Import dr into the appropriate place in the kernel (i.e., make a 502a1dc2096SDima Dorfman * krule). The value of dr is copied, so the pointer may be destroyed 503a1dc2096SDima Dorfman * after this call completes. 504a1dc2096SDima Dorfman */ 505a1dc2096SDima Dorfman static int 506a1dc2096SDima Dorfman devfs_rule_insert(struct devfs_rule *dr) 507a1dc2096SDima Dorfman { 508a1dc2096SDima Dorfman struct devfs_ruleset *ds, *dsi; 509a1dc2096SDima Dorfman struct devfs_krule *k1, *k2; 510a1dc2096SDima Dorfman struct devfs_krule *dk; 511a1dc2096SDima Dorfman devfs_rsnum rsnum; 512a1dc2096SDima Dorfman devfs_rnum dkrn; 513a1dc2096SDima Dorfman int error; 514a1dc2096SDima Dorfman 515a1dc2096SDima Dorfman /* 516a1dc2096SDima Dorfman * This stuff seems out of place here, but we want to do it as 517a1dc2096SDima Dorfman * soon as possible so that if it fails, we don't have to roll 518a1dc2096SDima Dorfman * back any changes we already made (e.g., ruleset creation). 519a1dc2096SDima Dorfman */ 520a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_INCSET) { 521a1dc2096SDima Dorfman dsi = devfs_ruleset_bynum(dr->dr_incset); 522a1dc2096SDima Dorfman if (dsi == NULL) 523a1dc2096SDima Dorfman return (ESRCH); 524a1dc2096SDima Dorfman } else 525a1dc2096SDima Dorfman dsi = NULL; 526a1dc2096SDima Dorfman 527a1dc2096SDima Dorfman rsnum = rid2rsn(dr->dr_id); 528a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rsnum); 529a1dc2096SDima Dorfman if (ds == NULL) 530a1dc2096SDima Dorfman ds = devfs_ruleset_create(rsnum); 531a1dc2096SDima Dorfman if (ds->ds_flags & DS_IMMUTABLE) 532a1dc2096SDima Dorfman return (EIO); 533a1dc2096SDima Dorfman dkrn = rid2rn(dr->dr_id); 534a1dc2096SDima Dorfman if (dkrn == 0) { 535a1dc2096SDima Dorfman error = devfs_rule_autonumber(ds, &dkrn); 536a1dc2096SDima Dorfman if (error != 0) 537a1dc2096SDima Dorfman return (error); 538a1dc2096SDima Dorfman } 539a1dc2096SDima Dorfman 540a163d034SWarner Losh dk = malloc(sizeof(*dk), M_DEVFS, M_WAITOK); 541a1dc2096SDima Dorfman dk->dk_ruleset = ds; 542a1dc2096SDima Dorfman if (dsi != NULL) 543a1dc2096SDima Dorfman ++dsi->ds_refcount; 544a1dc2096SDima Dorfman /* XXX: Inspect dr? */ 545a1dc2096SDima Dorfman memcpy(&dk->dk_rule, dr, sizeof(*dr)); 546a1dc2096SDima Dorfman dk->dk_rule.dr_id = mkrid(rid2rsn(dk->dk_rule.dr_id), dkrn); 547a1dc2096SDima Dorfman 548a1dc2096SDima Dorfman k1 = SLIST_FIRST(&ds->ds_rules); 549a1dc2096SDima Dorfman if (k1 == NULL || rid2rn(k1->dk_rule.dr_id) > dkrn) 550a1dc2096SDima Dorfman SLIST_INSERT_HEAD(&ds->ds_rules, dk, dk_list); 551a1dc2096SDima Dorfman else { 552a1dc2096SDima Dorfman SLIST_FOREACH(k1, &ds->ds_rules, dk_list) { 553a1dc2096SDima Dorfman k2 = SLIST_NEXT(k1, dk_list); 554a1dc2096SDima Dorfman if (k2 == NULL || rid2rn(k2->dk_rule.dr_id) > dkrn) { 555a1dc2096SDima Dorfman SLIST_INSERT_AFTER(k1, dk, dk_list); 556a1dc2096SDima Dorfman break; 557a1dc2096SDima Dorfman } 558a1dc2096SDima Dorfman } 559a1dc2096SDima Dorfman } 560a1dc2096SDima Dorfman 561a1dc2096SDima Dorfman return (0); 562a1dc2096SDima Dorfman } 563a1dc2096SDima Dorfman 564a1dc2096SDima Dorfman /* 565a1dc2096SDima Dorfman * Determine whether dk matches de. Returns 1 if dk should be run on 566a1dc2096SDima Dorfman * de; 0, otherwise. 567a1dc2096SDima Dorfman */ 568a1dc2096SDima Dorfman static int 569a1dc2096SDima Dorfman devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de) 570a1dc2096SDima Dorfman { 571a1dc2096SDima Dorfman struct devfs_rule *dr = &dk->dk_rule; 57289c9c53dSPoul-Henning Kamp struct cdev *dev; 573a1dc2096SDima Dorfman 574a1dc2096SDima Dorfman dev = devfs_rule_getdev(de); 575a1dc2096SDima Dorfman /* 576a1dc2096SDima Dorfman * At this point, if dev is NULL, we should assume that any 577a1dc2096SDima Dorfman * criteria that depend on it don't match. We should *not* 578a1dc2096SDima Dorfman * just ignore them (i.e., act like they weren't specified), 579a1dc2096SDima Dorfman * since that makes a rule that only has criteria dependent on 58089c9c53dSPoul-Henning Kamp * the struct cdev *match all symlinks and directories. 581a1dc2096SDima Dorfman * 582a1dc2096SDima Dorfman * Note also that the following tests are somewhat reversed: 583a1dc2096SDima Dorfman * They're actually testing to see whether the condition does 584a1dc2096SDima Dorfman * *not* match, since the default is to assume the rule should 585a1dc2096SDima Dorfman * be run (such as if there are no conditions). 586891822a8SPoul-Henning Kamp * 587891822a8SPoul-Henning Kamp * XXX: lacks threadref on dev 588a1dc2096SDima Dorfman */ 589a1dc2096SDima Dorfman if (dr->dr_icond & DRC_DSWFLAGS) 590a1dc2096SDima Dorfman if (dev == NULL || 591a1dc2096SDima Dorfman (dev->si_devsw->d_flags & dr->dr_dswflags) == 0) 5925e080af4SPoul-Henning Kamp return (0); 593a1dc2096SDima Dorfman if (dr->dr_icond & DRC_PATHPTRN) 594a1dc2096SDima Dorfman if (!devfs_rule_matchpath(dk, de)) 5955e080af4SPoul-Henning Kamp return (0); 596a1dc2096SDima Dorfman 597a1dc2096SDima Dorfman return (1); 598a1dc2096SDima Dorfman } 599a1dc2096SDima Dorfman 600a1dc2096SDima Dorfman /* 601a1dc2096SDima Dorfman * Determine whether dk matches de on account of dr_pathptrn. 602a1dc2096SDima Dorfman */ 603a1dc2096SDima Dorfman static int 604a1dc2096SDima Dorfman devfs_rule_matchpath(struct devfs_krule *dk, struct devfs_dirent *de) 605a1dc2096SDima Dorfman { 606a1dc2096SDima Dorfman struct devfs_rule *dr = &dk->dk_rule; 607a1dc2096SDima Dorfman char *pname; 60889c9c53dSPoul-Henning Kamp struct cdev *dev; 609a1dc2096SDima Dorfman 610a1dc2096SDima Dorfman dev = devfs_rule_getdev(de); 611a1dc2096SDima Dorfman if (dev != NULL) 612a1dc2096SDima Dorfman pname = dev->si_name; 6139f8ef8b8SColin Percival else if (de->de_dirent->d_type == DT_LNK || 6149f8ef8b8SColin Percival de->de_dirent->d_type == DT_DIR) 615797159bdSDima Dorfman pname = de->de_dirent->d_name; 616a1dc2096SDima Dorfman else 617a1dc2096SDima Dorfman return (0); 618a1dc2096SDima Dorfman KASSERT(pname != NULL, ("devfs_rule_matchpath: NULL pname")); 619a1dc2096SDima Dorfman 620e5d09546SDima Dorfman return (fnmatch(dr->dr_pathptrn, pname, 0) == 0); 621a1dc2096SDima Dorfman } 622a1dc2096SDima Dorfman 623a1dc2096SDima Dorfman /* 624a1dc2096SDima Dorfman * Run dk on de. 625a1dc2096SDima Dorfman */ 626a1dc2096SDima Dorfman static void 6275e080af4SPoul-Henning Kamp devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de, unsigned depth) 628a1dc2096SDima Dorfman { 629a1dc2096SDima Dorfman struct devfs_rule *dr = &dk->dk_rule; 630a1dc2096SDima Dorfman struct devfs_ruleset *ds; 631a1dc2096SDima Dorfman 6325e080af4SPoul-Henning Kamp if (!devfs_rule_match(dk, de)) 6335e080af4SPoul-Henning Kamp return; 634a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_BACTS) { 635a1dc2096SDima Dorfman if (dr->dr_bacts & DRB_HIDE) 636a1dc2096SDima Dorfman de->de_flags |= DE_WHITEOUT; 637a1dc2096SDima Dorfman if (dr->dr_bacts & DRB_UNHIDE) 638a1dc2096SDima Dorfman de->de_flags &= ~DE_WHITEOUT; 639a1dc2096SDima Dorfman } 640a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_UID) 641a1dc2096SDima Dorfman de->de_uid = dr->dr_uid; 642a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_GID) 643a1dc2096SDima Dorfman de->de_gid = dr->dr_gid; 644a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_MODE) 645a1dc2096SDima Dorfman de->de_mode = dr->dr_mode; 646a1dc2096SDima Dorfman if (dr->dr_iacts & DRA_INCSET) { 6475e080af4SPoul-Henning Kamp /* 6485e080af4SPoul-Henning Kamp * XXX: we should tell the user if the depth is exceeded here 6495e080af4SPoul-Henning Kamp * XXX: but it is not obvious how to. A return value will 6505e080af4SPoul-Henning Kamp * XXX: not work as this is called when devices are created 6515e080af4SPoul-Henning Kamp * XXX: long time after the rules were instantiated. 6525e080af4SPoul-Henning Kamp * XXX: a printf() would probably give too much noise, or 6535e080af4SPoul-Henning Kamp * XXX: DoS the machine. I guess a a rate-limited message 6545e080af4SPoul-Henning Kamp * XXX: might work. 6555e080af4SPoul-Henning Kamp */ 6565e080af4SPoul-Henning Kamp if (depth > 0) { 657a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset); 658a1dc2096SDima Dorfman KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset")); 6595e080af4SPoul-Henning Kamp devfs_ruleset_applyde(ds, de, depth - 1); 6605e080af4SPoul-Henning Kamp } 661a1dc2096SDima Dorfman } 662a1dc2096SDima Dorfman } 663a1dc2096SDima Dorfman 664a1dc2096SDima Dorfman /* 665a1dc2096SDima Dorfman * Apply all the rules in ds to de. 666a1dc2096SDima Dorfman */ 667a1dc2096SDima Dorfman static void 6685e080af4SPoul-Henning Kamp devfs_ruleset_applyde(struct devfs_ruleset *ds, struct devfs_dirent *de, unsigned depth) 669a1dc2096SDima Dorfman { 670a1dc2096SDima Dorfman struct devfs_krule *dk; 671a1dc2096SDima Dorfman 6725e080af4SPoul-Henning Kamp SLIST_FOREACH(dk, &ds->ds_rules, dk_list) 6735e080af4SPoul-Henning Kamp devfs_rule_run(dk, de, depth); 674a1dc2096SDima Dorfman } 675a1dc2096SDima Dorfman 676a1dc2096SDima Dorfman /* 677a1dc2096SDima Dorfman * Apply all the rules in ds to all the entires in dm. 678a1dc2096SDima Dorfman */ 679a1dc2096SDima Dorfman static void 680a1dc2096SDima Dorfman devfs_ruleset_applydm(struct devfs_ruleset *ds, struct devfs_mount *dm) 681a1dc2096SDima Dorfman { 682a1dc2096SDima Dorfman struct devfs_krule *dk; 683a1dc2096SDima Dorfman 684a1dc2096SDima Dorfman /* 685a1dc2096SDima Dorfman * XXX: Does it matter whether we do 686a1dc2096SDima Dorfman * 687a1dc2096SDima Dorfman * foreach(dk in ds) 688a1dc2096SDima Dorfman * foreach(de in dm) 689a1dc2096SDima Dorfman * apply(dk to de) 690a1dc2096SDima Dorfman * 691a1dc2096SDima Dorfman * as opposed to 692a1dc2096SDima Dorfman * 693a1dc2096SDima Dorfman * foreach(de in dm) 694a1dc2096SDima Dorfman * foreach(dk in ds) 695a1dc2096SDima Dorfman * apply(dk to de) 696a1dc2096SDima Dorfman * 697a1dc2096SDima Dorfman * The end result is obviously the same, but does the order 698a1dc2096SDima Dorfman * matter? 699a1dc2096SDima Dorfman */ 700a1dc2096SDima Dorfman SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 701a1dc2096SDima Dorfman devfs_rule_applydm(dk, dm); 702a1dc2096SDima Dorfman } 703a1dc2096SDima Dorfman } 704a1dc2096SDima Dorfman 705a1dc2096SDima Dorfman /* 706a1dc2096SDima Dorfman * Find a ruleset by number. 707a1dc2096SDima Dorfman */ 708a1dc2096SDima Dorfman static struct devfs_ruleset * 709a1dc2096SDima Dorfman devfs_ruleset_bynum(devfs_rsnum rsnum) 710a1dc2096SDima Dorfman { 711a1dc2096SDima Dorfman struct devfs_ruleset *ds; 712a1dc2096SDima Dorfman 713a1dc2096SDima Dorfman SLIST_FOREACH(ds, &devfs_rulesets, ds_list) { 714a1dc2096SDima Dorfman if (ds->ds_number == rsnum) 715a1dc2096SDima Dorfman return (ds); 716a1dc2096SDima Dorfman } 717a1dc2096SDima Dorfman return (NULL); 718a1dc2096SDima Dorfman } 719a1dc2096SDima Dorfman 720a1dc2096SDima Dorfman /* 721a1dc2096SDima Dorfman * Create a new ruleset. 722a1dc2096SDima Dorfman */ 723a1dc2096SDima Dorfman static struct devfs_ruleset * 724a1dc2096SDima Dorfman devfs_ruleset_create(devfs_rsnum rsnum) 725a1dc2096SDima Dorfman { 726a1dc2096SDima Dorfman struct devfs_ruleset *s1, *s2; 727a1dc2096SDima Dorfman struct devfs_ruleset *ds; 728a1dc2096SDima Dorfman 729a1dc2096SDima Dorfman KASSERT(devfs_ruleset_bynum(rsnum) == NULL, 730a1dc2096SDima Dorfman ("creating already existent ruleset %d", rsnum)); 731a1dc2096SDima Dorfman 732a163d034SWarner Losh ds = malloc(sizeof(*ds), M_DEVFS, M_WAITOK | M_ZERO); 733a1dc2096SDima Dorfman ds->ds_number = rsnum; 734a1dc2096SDima Dorfman ds->ds_refcount = ds->ds_flags = 0; 735a1dc2096SDima Dorfman SLIST_INIT(&ds->ds_rules); 736a1dc2096SDima Dorfman 737a1dc2096SDima Dorfman s1 = SLIST_FIRST(&devfs_rulesets); 738a1dc2096SDima Dorfman if (s1 == NULL || s1->ds_number > rsnum) 739a1dc2096SDima Dorfman SLIST_INSERT_HEAD(&devfs_rulesets, ds, ds_list); 740a1dc2096SDima Dorfman else { 741a1dc2096SDima Dorfman SLIST_FOREACH(s1, &devfs_rulesets, ds_list) { 742a1dc2096SDima Dorfman s2 = SLIST_NEXT(s1, ds_list); 743a1dc2096SDima Dorfman if (s2 == NULL || s2->ds_number > rsnum) { 744a1dc2096SDima Dorfman SLIST_INSERT_AFTER(s1, ds, ds_list); 745a1dc2096SDima Dorfman break; 746a1dc2096SDima Dorfman } 747a1dc2096SDima Dorfman } 748a1dc2096SDima Dorfman } 749a1dc2096SDima Dorfman 750a1dc2096SDima Dorfman return (ds); 751a1dc2096SDima Dorfman } 752a1dc2096SDima Dorfman 753a1dc2096SDima Dorfman /* 754a1dc2096SDima Dorfman * Remove a ruleset form the system. The ruleset specified must be 755a1dc2096SDima Dorfman * empty and not in use. 756a1dc2096SDima Dorfman */ 757a1dc2096SDima Dorfman static void 758a1dc2096SDima Dorfman devfs_ruleset_destroy(struct devfs_ruleset **dsp) 759a1dc2096SDima Dorfman { 760a1dc2096SDima Dorfman struct devfs_ruleset *ds = *dsp; 761a1dc2096SDima Dorfman 762a1dc2096SDima Dorfman KASSERT(SLIST_EMPTY(&ds->ds_rules), ("destroying non-empty ruleset")); 763a1dc2096SDima Dorfman KASSERT(ds->ds_refcount == 0, ("destroying busy ruleset")); 764a1dc2096SDima Dorfman KASSERT((ds->ds_flags & DS_IMMUTABLE) == 0, 765a1dc2096SDima Dorfman ("destroying immutable ruleset")); 766a1dc2096SDima Dorfman 767a1dc2096SDima Dorfman SLIST_REMOVE(&devfs_rulesets, ds, devfs_ruleset, ds_list); 768a1dc2096SDima Dorfman free(ds, M_DEVFS); 769a1dc2096SDima Dorfman *dsp = NULL; 770a1dc2096SDima Dorfman } 771a1dc2096SDima Dorfman 772a1dc2096SDima Dorfman /* 773a1dc2096SDima Dorfman * Remove a ruleset from the system if it's empty and not used 774a1dc2096SDima Dorfman * anywhere. This should be called after every time a rule is deleted 775a1dc2096SDima Dorfman * from this ruleset or the reference count is decremented. 776a1dc2096SDima Dorfman */ 777a1dc2096SDima Dorfman static void 778a1dc2096SDima Dorfman devfs_ruleset_reap(struct devfs_ruleset **dsp) 779a1dc2096SDima Dorfman { 780a1dc2096SDima Dorfman struct devfs_ruleset *ds = *dsp; 781a1dc2096SDima Dorfman 782a1dc2096SDima Dorfman if (SLIST_EMPTY(&ds->ds_rules) && ds->ds_refcount == 0) { 783a1dc2096SDima Dorfman devfs_ruleset_destroy(&ds); 784a1dc2096SDima Dorfman *dsp = ds; 785a1dc2096SDima Dorfman } 786a1dc2096SDima Dorfman } 787a1dc2096SDima Dorfman 788a1dc2096SDima Dorfman /* 789a1dc2096SDima Dorfman * Make rsnum the active ruleset for dm. 790a1dc2096SDima Dorfman */ 791a1dc2096SDima Dorfman static int 792a1dc2096SDima Dorfman devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm) 793a1dc2096SDima Dorfman { 794a1dc2096SDima Dorfman struct devfs_ruleset *cds, *ds; 795a1dc2096SDima Dorfman 796a1dc2096SDima Dorfman ds = devfs_ruleset_bynum(rsnum); 797a1dc2096SDima Dorfman if (ds == NULL) 798a1dc2096SDima Dorfman ds = devfs_ruleset_create(rsnum); 799a1dc2096SDima Dorfman cds = devfs_ruleset_bynum(dm->dm_ruleset); 800a1dc2096SDima Dorfman KASSERT(cds != NULL, ("mount-point has NULL ruleset")); 801a1dc2096SDima Dorfman 802a1dc2096SDima Dorfman /* These should probably be made atomic somehow. */ 803a1dc2096SDima Dorfman --cds->ds_refcount; 804a1dc2096SDima Dorfman ++ds->ds_refcount; 805a1dc2096SDima Dorfman dm->dm_ruleset = rsnum; 806a1dc2096SDima Dorfman 807a1dc2096SDima Dorfman devfs_ruleset_reap(&cds); 808a1dc2096SDima Dorfman return (0); 809a1dc2096SDima Dorfman } 810