devfs_rule.c revision 100793
1100206Sdd/*- 2100206Sdd * Copyright (c) 2002 Dima Dorfman. 3100206Sdd * All rights reserved. 4100206Sdd * 5100206Sdd * Redistribution and use in source and binary forms, with or without 6100206Sdd * modification, are permitted provided that the following conditions 7100206Sdd * are met: 8100206Sdd * 1. Redistributions of source code must retain the above copyright 9100206Sdd * notice, this list of conditions and the following disclaimer. 10100206Sdd * 2. Redistributions in binary form must reproduce the above copyright 11100206Sdd * notice, this list of conditions and the following disclaimer in the 12100206Sdd * documentation and/or other materials provided with the distribution. 13100206Sdd * 14100206Sdd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15100206Sdd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16100206Sdd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17100206Sdd * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18100206Sdd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19100206Sdd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20100206Sdd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21100206Sdd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22100206Sdd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23100206Sdd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24100206Sdd * SUCH DAMAGE. 25100206Sdd * 26100206Sdd * $FreeBSD: head/sys/fs/devfs/devfs_rule.c 100793 2002-07-28 03:52:44Z dd $ 27100206Sdd */ 28100206Sdd 29100206Sdd/* 30100206Sdd * DEVFS ruleset implementation. 31100206Sdd * 32100206Sdd * A note on terminology: To "run" a rule on a dirent is to take the 33100206Sdd * prescribed action; to "apply" a rule is to check whether it matches 34100206Sdd * a dirent and run if if it does. 35100206Sdd * 36100206Sdd * A note on locking: Only foreign entry points (non-static functions) 37100206Sdd * should deal with locking. Everything else assumes we already hold 38100206Sdd * the required kind of lock. 39100206Sdd * 40100206Sdd * A note on namespace: devfs_rules_* are the non-static functions for 41100206Sdd * the entire "ruleset" subsystem, devfs_rule_* are the static 42100206Sdd * functions that operate on rules, and devfs_ruleset_* are the static 43100206Sdd * functions that operate on rulesets. The line between the last two 44100206Sdd * isn't always clear, but the guideline is still useful. 45100206Sdd * 46100206Sdd * A note on "special" identifiers: Ruleset 0 is the NULL, or empty, 47100206Sdd * ruleset; it cannot be deleted or changed in any way. This may be 48100206Sdd * assumed inside the code; e.g., a ruleset of 0 may be interpeted to 49100206Sdd * mean "no ruleset". The interpretation of rule 0 is 50100206Sdd * command-dependent, but in no case is there a real rule with number 51100206Sdd * 0. 52100206Sdd * 53100206Sdd * A note on errno codes: To make it easier for the userland to tell 54100206Sdd * what went wrong, we sometimes use errno codes that are not entirely 55100206Sdd * appropriate for the error but that would be less ambiguous than the 56100206Sdd * appropriate "generic" code. For example, when we can't find a 57100206Sdd * ruleset, we return ESRCH instead of ENOENT (except in 58100206Sdd * DEVFSIO_{R,S}GETNEXT, where a nonexistent ruleset means "end of 59100206Sdd * list", and the userland expects ENOENT to be this indicator); this 60100206Sdd * way, when an operation fails, it's clear that what couldn't be 61100206Sdd * found is a ruleset and not a rule (well, it's clear to those who 62100206Sdd * know the convention). 63100206Sdd */ 64100206Sdd 65100206Sdd#include "opt_devfs.h" 66100206Sdd#ifndef NODEVFS 67100206Sdd 68100206Sdd#include <sys/param.h> 69100206Sdd#include <sys/systm.h> 70100206Sdd#include <sys/conf.h> 71100206Sdd#include <sys/kernel.h> 72100206Sdd#include <sys/malloc.h> 73100206Sdd#include <sys/dirent.h> 74100206Sdd#include <sys/vnode.h> 75100206Sdd#include <sys/mount.h> 76100206Sdd#include <sys/ioccom.h> 77100206Sdd 78100206Sdd#include <fs/devfs/devfs.h> 79100206Sdd 80100206Sdd 81100206Sdd/* 82100206Sdd * Kernel version of devfs_rule. 83100206Sdd */ 84100206Sddstruct devfs_krule { 85100206Sdd SLIST_ENTRY(devfs_krule) dk_list; 86100206Sdd struct devfs_ruleset *dk_ruleset; 87100206Sdd struct devfs_rule dk_rule; 88100206Sdd}; 89100206Sdd 90100206Sdd/* 91100206Sdd * Structure to describe a ruleset. 92100206Sdd */ 93100206Sddstruct devfs_ruleset { 94100206Sdd SLIST_ENTRY(devfs_ruleset) ds_list; 95100206Sdd devfs_rsnum ds_number; 96100206Sdd SLIST_HEAD(, devfs_krule) ds_rules; 97100206Sdd int ds_refcount; 98100206Sdd int ds_flags; 99100206Sdd#define DS_IMMUTABLE 0x001 100100793Sdd int ds_running; 101100206Sdd}; 102100206Sdd 103100206Sddstatic devfs_rid devfs_rid_input(devfs_rid rid, struct devfs_mount *dm); 104100206Sdd 105100206Sddstatic void devfs_rule_applyde(struct devfs_krule *dk,struct devfs_dirent *de); 106100206Sddstatic void devfs_rule_applyde_recursive(struct devfs_krule *dk, 107100206Sdd struct devfs_dirent *de); 108100206Sddstatic void devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm); 109100206Sddstatic int devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnp); 110100206Sddstatic struct devfs_krule *devfs_rule_byid(devfs_rid rid); 111100206Sddstatic int devfs_rule_delete(struct devfs_krule **dkp); 112100206Sddstatic dev_t devfs_rule_getdev(struct devfs_dirent *de); 113100206Sddstatic int devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm); 114100206Sddstatic int devfs_rule_insert(struct devfs_rule *dr); 115100206Sddstatic int devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de); 116100206Sddstatic int devfs_rule_matchpath(struct devfs_krule *dk, 117100206Sdd struct devfs_dirent *de); 118100206Sddstatic void devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de); 119100206Sdd 120100206Sddstatic void devfs_ruleset_applyde(struct devfs_ruleset *ds, 121100206Sdd struct devfs_dirent *de); 122100206Sddstatic void devfs_ruleset_applydm(struct devfs_ruleset *ds, 123100206Sdd struct devfs_mount *dm); 124100206Sddstatic struct devfs_ruleset *devfs_ruleset_bynum(devfs_rsnum rsnum); 125100206Sddstatic struct devfs_ruleset *devfs_ruleset_create(devfs_rsnum rsnum); 126100206Sddstatic void devfs_ruleset_destroy(struct devfs_ruleset **dsp); 127100206Sddstatic void devfs_ruleset_reap(struct devfs_ruleset **dsp); 128100206Sddstatic int devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm); 129100206Sdd 130100206Sddstatic SLIST_HEAD(, devfs_ruleset) devfs_rulesets; 131100206Sdd 132100206Sdd/* 133100206Sdd * Called to apply the proper rules for de before the latter can be 134100206Sdd * exposed to the userland. This should be called with an exclusive 135100206Sdd * lock on dm in case we need to run anything. 136100206Sdd */ 137100206Sddvoid 138100206Sdddevfs_rules_apply(struct devfs_mount *dm, struct devfs_dirent *de) 139100206Sdd{ 140100206Sdd struct devfs_ruleset *ds; 141100206Sdd 142100206Sdd ds = devfs_ruleset_bynum(dm->dm_ruleset); 143100206Sdd KASSERT(ds != NULL, ("mount-point has NULL ruleset")); 144100206Sdd devfs_ruleset_applyde(ds, de); 145100206Sdd} 146100206Sdd 147100206Sdd/* 148100206Sdd * Rule subsystem SYSINIT hook. 149100206Sdd */ 150100206Sddvoid 151100206Sdddevfs_rules_init(void) 152100206Sdd{ 153100206Sdd struct devfs_ruleset *ds; 154100206Sdd 155100206Sdd SLIST_INIT(&devfs_rulesets); 156100206Sdd 157100206Sdd ds = devfs_ruleset_create(0); 158100206Sdd ds->ds_flags |= DS_IMMUTABLE; 159100206Sdd ds->ds_refcount = 1; /* Prevent reaping. */ 160100206Sdd} 161100206Sdd 162100206Sdd/* 163100206Sdd * Rule subsystem ioctl hook. 164100206Sdd */ 165100206Sddint 166100206Sdddevfs_rules_ioctl(struct mount *mp, int cmd, caddr_t data, struct thread *td) 167100206Sdd{ 168100206Sdd struct devfs_mount *dm = VFSTODEVFS(mp); 169100206Sdd struct devfs_ruleset *ds; 170100206Sdd struct devfs_krule *dk; 171100206Sdd struct devfs_rule *dr; 172100206Sdd devfs_rsnum rsnum; 173100206Sdd devfs_rnum rnum; 174100206Sdd devfs_rid rid; 175100206Sdd int error; 176100206Sdd 177100206Sdd /* 178100206Sdd * XXX: This returns an error regardless of whether we 179100206Sdd * actually support the cmd or not. 180100206Sdd */ 181100206Sdd error = suser(td); 182100206Sdd if (error != 0) 183100206Sdd return (error); 184100206Sdd 185100206Sdd lockmgr(&dm->dm_lock, LK_SHARED, 0, td); 186100206Sdd 187100206Sdd switch (cmd) { 188100206Sdd case DEVFSIO_RADD: 189100206Sdd dr = (struct devfs_rule *)data; 190100206Sdd error = devfs_rule_input(dr, dm); 191100206Sdd if (error != 0) 192100206Sdd goto out; 193100206Sdd dk = devfs_rule_byid(dr->dr_id); 194100206Sdd if (dk != NULL) { 195100206Sdd error = EEXIST; 196100206Sdd goto out; 197100206Sdd } 198100206Sdd lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td); 199100206Sdd error = devfs_rule_insert(dr); 200100206Sdd break; 201100206Sdd case DEVFSIO_RAPPLY: 202100206Sdd dr = (struct devfs_rule *)data; 203100206Sdd error = devfs_rule_input(dr, dm); 204100206Sdd if (error != 0) 205100206Sdd goto out; 206100206Sdd 207100206Sdd /* 208100206Sdd * This is one of many possible hackish 209100206Sdd * implementations. The primary contender is an 210100206Sdd * implementation where the rule we read in is 211100206Sdd * temporarily inserted into some ruleset, perhaps 212100206Sdd * with a hypothetical DRO_NOAUTO flag so that it 213100206Sdd * doesn't get used where it isn't intended, and 214100206Sdd * applied in the normal way. This can be done in the 215100206Sdd * userland (DEVFSIO_ADD, DEVFSIO_APPLYID, 216100206Sdd * DEVFSIO_DEL) or in the kernel; either way it breaks 217100206Sdd * some corner case assumptions in other parts of the 218100206Sdd * code (not that this implementation doesn't do 219100206Sdd * that). 220100206Sdd */ 221100206Sdd if (dr->dr_iacts & DRA_INCSET && 222100206Sdd devfs_ruleset_bynum(dr->dr_incset) == NULL) { 223100206Sdd error = ESRCH; 224100206Sdd goto out; 225100206Sdd } 226100206Sdd dk = malloc(sizeof(*dk), M_TEMP, M_WAITOK | M_ZERO); 227100206Sdd memcpy(&dk->dk_rule, dr, sizeof(*dr)); 228100206Sdd lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td); 229100206Sdd devfs_rule_applydm(dk, dm); 230100206Sdd lockmgr(&dm->dm_lock, LK_DOWNGRADE, 0, td); 231100206Sdd free(dk, M_TEMP); 232100206Sdd error = 0; 233100206Sdd break; 234100206Sdd case DEVFSIO_RAPPLYID: 235100206Sdd rid = *(devfs_rid *)data; 236100206Sdd rid = devfs_rid_input(rid, dm); 237100206Sdd dk = devfs_rule_byid(rid); 238100206Sdd if (dk == NULL) { 239100206Sdd error = ENOENT; 240100206Sdd goto out; 241100206Sdd } 242100206Sdd lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td); 243100206Sdd devfs_rule_applydm(dk, dm); 244100206Sdd error = 0; 245100206Sdd break; 246100206Sdd case DEVFSIO_RDEL: 247100206Sdd rid = *(devfs_rid *)data; 248100206Sdd rid = devfs_rid_input(rid, dm); 249100206Sdd dk = devfs_rule_byid(rid); 250100206Sdd if (dk == NULL) { 251100206Sdd error = ENOENT; 252100206Sdd goto out; 253100206Sdd } 254100206Sdd ds = dk->dk_ruleset; 255100206Sdd lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td); 256100206Sdd error = devfs_rule_delete(&dk); 257100206Sdd devfs_ruleset_reap(&ds); 258100206Sdd break; 259100206Sdd case DEVFSIO_RGETNEXT: 260100206Sdd dr = (struct devfs_rule *)data; 261100206Sdd error = devfs_rule_input(dr, dm); 262100206Sdd if (error != 0) 263100206Sdd goto out; 264100206Sdd /* 265100206Sdd * We can't use devfs_rule_byid() here since that 266100206Sdd * requires the rule specified to exist, but we want 267100206Sdd * getnext(N) to work whether there is a rule N or not 268100206Sdd * (specifically, getnext(0) must work, but we should 269100206Sdd * never have a rule 0 since the add command 270100206Sdd * interprets 0 to mean "auto-number"). 271100206Sdd */ 272100206Sdd ds = devfs_ruleset_bynum(rid2rsn(dr->dr_id)); 273100206Sdd if (ds == NULL) { 274100206Sdd error = ENOENT; 275100206Sdd goto out; 276100206Sdd } 277100206Sdd rnum = rid2rn(dr->dr_id); 278100206Sdd SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 279100206Sdd if (rid2rn(dk->dk_rule.dr_id) > rnum) 280100206Sdd break; 281100206Sdd } 282100206Sdd if (dk == NULL) { 283100206Sdd error = ENOENT; 284100206Sdd goto out; 285100206Sdd } 286100206Sdd memcpy(dr, &dk->dk_rule, sizeof(*dr)); 287100206Sdd error = 0; 288100206Sdd break; 289100206Sdd case DEVFSIO_SUSE: 290100206Sdd rsnum = *(devfs_rsnum *)data; 291100206Sdd lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td); 292100206Sdd error = devfs_ruleset_use(rsnum, dm); 293100206Sdd break; 294100206Sdd case DEVFSIO_SAPPLY: 295100206Sdd rsnum = *(devfs_rsnum *)data; 296100206Sdd rsnum = rid2rsn(devfs_rid_input(mkrid(rsnum, 0), dm)); 297100206Sdd ds = devfs_ruleset_bynum(rsnum); 298100206Sdd if (ds == NULL) { 299100206Sdd error = ESRCH; 300100206Sdd goto out; 301100206Sdd } 302100206Sdd lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td); 303100206Sdd devfs_ruleset_applydm(ds, dm); 304100206Sdd error = 0; 305100206Sdd break; 306100206Sdd case DEVFSIO_SGETNEXT: 307100206Sdd rsnum = *(devfs_rsnum *)data; 308100206Sdd SLIST_FOREACH(ds, &devfs_rulesets, ds_list) { 309100206Sdd if (ds->ds_number > rsnum) 310100206Sdd break; 311100206Sdd } 312100206Sdd if (ds == NULL) 313100206Sdd error = ENOENT; 314100206Sdd else { 315100206Sdd *(devfs_rsnum *)data = ds->ds_number; 316100206Sdd error = 0; 317100206Sdd } 318100206Sdd break; 319100206Sdd default: 320100206Sdd error = ENOIOCTL; 321100206Sdd break; 322100206Sdd } 323100206Sdd 324100206Sddout: 325100206Sdd lockmgr(&dm->dm_lock, LK_RELEASE, 0, td); 326100206Sdd return (error); 327100206Sdd} 328100206Sdd 329100206Sdd/* 330100206Sdd * Called to initialize dm_ruleset when there is a new mount-point. 331100206Sdd */ 332100206Sddvoid 333100206Sdddevfs_rules_newmount(struct devfs_mount *dm, struct thread *td) 334100206Sdd{ 335100206Sdd struct devfs_ruleset *ds; 336100206Sdd 337100206Sdd lockmgr(&dm->dm_lock, LK_EXCLUSIVE, 0, td); 338100206Sdd /* 339100206Sdd * We can't use devfs_ruleset_use() since it will try to 340100206Sdd * decrement the refcount for the old ruleset, and there is no 341100206Sdd * old ruleset. Making some value of ds_ruleset "special" to 342100206Sdd * mean "don't decrement refcount" is uglier than this. 343100206Sdd */ 344100206Sdd ds = devfs_ruleset_bynum(0); 345100206Sdd KASSERT(ds != NULL, ("no ruleset 0")); 346100206Sdd ++ds->ds_refcount; 347100206Sdd dm->dm_ruleset = 0; 348100206Sdd lockmgr(&dm->dm_lock, LK_RELEASE, 0, td); 349100206Sdd} 350100206Sdd 351100206Sdd/* 352100206Sdd * Adjust the rule identifier to use the ruleset of dm if one isn't 353100206Sdd * explicitly specified. 354100206Sdd * 355100206Sdd * Note that after this operation, rid2rsn(rid) might still be 0, and 356100206Sdd * that's okay; ruleset 0 is a valid ruleset, but when it's read in 357100206Sdd * from the userland, it means "current ruleset for this mount-point". 358100206Sdd */ 359100206Sddstatic devfs_rid 360100206Sdddevfs_rid_input(devfs_rid rid, struct devfs_mount *dm) 361100206Sdd{ 362100206Sdd 363100206Sdd if (rid2rsn(rid) == 0) 364100206Sdd return (mkrid(dm->dm_ruleset, rid2rn(rid))); 365100206Sdd else 366100206Sdd return (rid); 367100206Sdd} 368100206Sdd 369100206Sdd/* 370100206Sdd * Apply dk to de. 371100206Sdd */ 372100206Sddstatic void 373100206Sdddevfs_rule_applyde(struct devfs_krule *dk, struct devfs_dirent *de) 374100206Sdd{ 375100206Sdd 376100206Sdd if (devfs_rule_match(dk, de)) 377100206Sdd devfs_rule_run(dk, de); 378100206Sdd} 379100206Sdd 380100206Sdd/* 381100206Sdd * Apply dk to de and everything under de. 382100206Sdd * 383100206Sdd * XXX: This method needs a function call for every nested 384100206Sdd * subdirectory in a devfs mount. If we plan to have many of these, 385100206Sdd * we might eventually run out of kernel stack space. 386100206Sdd */ 387100206Sddstatic void 388100206Sdddevfs_rule_applyde_recursive(struct devfs_krule *dk, struct devfs_dirent *de) 389100206Sdd{ 390100206Sdd struct devfs_dirent *de2; 391100206Sdd 392100206Sdd /* XXX: Should we apply to ourselves first or last? Does it matter? */ 393100206Sdd TAILQ_FOREACH(de2, &de->de_dlist, de_list) { 394100206Sdd devfs_rule_applyde_recursive(dk, de2); 395100206Sdd } 396100206Sdd devfs_rule_applyde(dk, de); 397100206Sdd} 398100206Sdd 399100206Sdd/* 400100206Sdd * Apply dk to all entires in dm. 401100206Sdd */ 402100206Sddstatic void 403100206Sdddevfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm) 404100206Sdd{ 405100206Sdd 406100206Sdd devfs_rule_applyde_recursive(dk, dm->dm_basedir); 407100206Sdd} 408100206Sdd 409100206Sdd/* 410100206Sdd * Automatically select a number for a new rule in ds, and write the 411100206Sdd * result into rnump. 412100206Sdd */ 413100206Sddstatic int 414100206Sdddevfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnump) 415100206Sdd{ 416100206Sdd struct devfs_krule *dk; 417100206Sdd 418100206Sdd /* Find the last rule. */ 419100206Sdd SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 420100206Sdd if (SLIST_NEXT(dk, dk_list) == NULL) 421100206Sdd break; 422100206Sdd } 423100206Sdd if (dk == NULL) 424100206Sdd *rnump = 100; 425100206Sdd else { 426100206Sdd *rnump = rid2rn(dk->dk_rule.dr_id) + 100; 427100206Sdd /* Detect overflow. */ 428100206Sdd if (*rnump < rid2rn(dk->dk_rule.dr_id)) 429100206Sdd return (ERANGE); 430100206Sdd } 431100206Sdd KASSERT(devfs_rule_byid(mkrid(ds->ds_number, *rnump)) == NULL, 432100206Sdd ("autonumbering resulted in an already existing rule")); 433100206Sdd return (0); 434100206Sdd} 435100206Sdd 436100206Sdd/* 437100206Sdd * Find a krule by id. 438100206Sdd */ 439100206Sddstatic struct devfs_krule * 440100206Sdddevfs_rule_byid(devfs_rid rid) 441100206Sdd{ 442100206Sdd struct devfs_ruleset *ds; 443100206Sdd struct devfs_krule *dk; 444100206Sdd devfs_rnum rn; 445100206Sdd 446100206Sdd rn = rid2rn(rid); 447100206Sdd ds = devfs_ruleset_bynum(rid2rsn(rid)); 448100206Sdd if (ds == NULL) 449100206Sdd return (NULL); 450100206Sdd SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 451100206Sdd if (rid2rn(dk->dk_rule.dr_id) == rn) 452100206Sdd return (dk); 453100206Sdd else if (rid2rn(dk->dk_rule.dr_id) > rn) 454100206Sdd break; 455100206Sdd } 456100206Sdd return (NULL); 457100206Sdd} 458100206Sdd 459100206Sdd/* 460100206Sdd * Remove dkp from any lists it may be on and remove memory associated 461100206Sdd * with it. 462100206Sdd */ 463100206Sddstatic int 464100206Sdddevfs_rule_delete(struct devfs_krule **dkp) 465100206Sdd{ 466100206Sdd struct devfs_krule *dk = *dkp; 467100206Sdd struct devfs_ruleset *ds; 468100206Sdd 469100206Sdd if (dk->dk_rule.dr_iacts & DRA_INCSET) { 470100206Sdd ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset); 471100206Sdd KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset")); 472100206Sdd --ds->ds_refcount; 473100206Sdd devfs_ruleset_reap(&ds); 474100206Sdd } 475100206Sdd SLIST_REMOVE(&dk->dk_ruleset->ds_rules, dk, devfs_krule, dk_list); 476100206Sdd free(dk, M_DEVFS); 477100206Sdd *dkp = NULL; 478100206Sdd return (0); 479100206Sdd} 480100206Sdd 481100206Sdd/* 482100206Sdd * Get a dev_t corresponding to de so we can try to match rules based 483100206Sdd * on it. If this routine returns NULL, there is no dev_t associated 484100206Sdd * with the dirent (symlinks and directories don't have dev_ts), and 485100206Sdd * the caller should assume that any critera dependent on a dev_t 486100206Sdd * don't match. 487100206Sdd */ 488100206Sddstatic dev_t 489100206Sdddevfs_rule_getdev(struct devfs_dirent *de) 490100206Sdd{ 491100206Sdd dev_t *devp, dev; 492100206Sdd 493100206Sdd devp = devfs_itod(de->de_inode); 494100206Sdd if (devp != NULL) 495100206Sdd dev = *devp; 496100206Sdd else 497100206Sdd dev = NULL; 498100206Sdd /* If we think this dirent should have a dev_t, alert the user. */ 499100206Sdd if (dev == NULL && de->de_dirent->d_type != DT_LNK && 500100206Sdd de->de_dirent->d_type != DT_DIR) 501100206Sdd printf("Warning: no dev_t for %s\n", de->de_dirent->d_name); 502100206Sdd return (dev); 503100206Sdd} 504100206Sdd 505100206Sdd/* 506100206Sdd * Do what we need to do to a rule that we just loaded from the 507100206Sdd * userland. In particular, we need to check the magic, and adjust 508100206Sdd * the ruleset appropriate if desired. 509100206Sdd */ 510100206Sddstatic int 511100206Sdddevfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm) 512100206Sdd{ 513100206Sdd 514100206Sdd if (dr->dr_magic != DEVFS_MAGIC) 515100206Sdd return (ERPCMISMATCH); 516100206Sdd dr->dr_id = devfs_rid_input(dr->dr_id, dm); 517100206Sdd return (0); 518100206Sdd} 519100206Sdd 520100206Sdd/* 521100206Sdd * Import dr into the appropriate place in the kernel (i.e., make a 522100206Sdd * krule). The value of dr is copied, so the pointer may be destroyed 523100206Sdd * after this call completes. 524100206Sdd */ 525100206Sddstatic int 526100206Sdddevfs_rule_insert(struct devfs_rule *dr) 527100206Sdd{ 528100206Sdd struct devfs_ruleset *ds, *dsi; 529100206Sdd struct devfs_krule *k1, *k2; 530100206Sdd struct devfs_krule *dk; 531100206Sdd devfs_rsnum rsnum; 532100206Sdd devfs_rnum dkrn; 533100206Sdd int error; 534100206Sdd 535100206Sdd /* 536100206Sdd * This stuff seems out of place here, but we want to do it as 537100206Sdd * soon as possible so that if it fails, we don't have to roll 538100206Sdd * back any changes we already made (e.g., ruleset creation). 539100206Sdd */ 540100206Sdd if (dr->dr_iacts & DRA_INCSET) { 541100206Sdd dsi = devfs_ruleset_bynum(dr->dr_incset); 542100206Sdd if (dsi == NULL) 543100206Sdd return (ESRCH); 544100206Sdd } else 545100206Sdd dsi = NULL; 546100206Sdd 547100206Sdd rsnum = rid2rsn(dr->dr_id); 548100206Sdd ds = devfs_ruleset_bynum(rsnum); 549100206Sdd if (ds == NULL) 550100206Sdd ds = devfs_ruleset_create(rsnum); 551100206Sdd if (ds->ds_flags & DS_IMMUTABLE) 552100206Sdd return (EIO); 553100206Sdd dkrn = rid2rn(dr->dr_id); 554100206Sdd if (dkrn == 0) { 555100206Sdd error = devfs_rule_autonumber(ds, &dkrn); 556100206Sdd if (error != 0) 557100206Sdd return (error); 558100206Sdd } 559100206Sdd 560100206Sdd dk = malloc(sizeof(*dk), M_DEVFS, M_WAITOK); 561100206Sdd dk->dk_ruleset = ds; 562100206Sdd if (dsi != NULL) 563100206Sdd ++dsi->ds_refcount; 564100206Sdd /* XXX: Inspect dr? */ 565100206Sdd memcpy(&dk->dk_rule, dr, sizeof(*dr)); 566100206Sdd dk->dk_rule.dr_id = mkrid(rid2rsn(dk->dk_rule.dr_id), dkrn); 567100206Sdd 568100206Sdd k1 = SLIST_FIRST(&ds->ds_rules); 569100206Sdd if (k1 == NULL || rid2rn(k1->dk_rule.dr_id) > dkrn) 570100206Sdd SLIST_INSERT_HEAD(&ds->ds_rules, dk, dk_list); 571100206Sdd else { 572100206Sdd SLIST_FOREACH(k1, &ds->ds_rules, dk_list) { 573100206Sdd k2 = SLIST_NEXT(k1, dk_list); 574100206Sdd if (k2 == NULL || rid2rn(k2->dk_rule.dr_id) > dkrn) { 575100206Sdd SLIST_INSERT_AFTER(k1, dk, dk_list); 576100206Sdd break; 577100206Sdd } 578100206Sdd } 579100206Sdd } 580100206Sdd 581100206Sdd return (0); 582100206Sdd} 583100206Sdd 584100206Sdd/* 585100206Sdd * Determine whether dk matches de. Returns 1 if dk should be run on 586100206Sdd * de; 0, otherwise. 587100206Sdd */ 588100206Sddstatic int 589100206Sdddevfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de) 590100206Sdd{ 591100206Sdd struct devfs_rule *dr = &dk->dk_rule; 592100206Sdd dev_t dev; 593100206Sdd 594100206Sdd dev = devfs_rule_getdev(de); 595100206Sdd /* 596100206Sdd * At this point, if dev is NULL, we should assume that any 597100206Sdd * criteria that depend on it don't match. We should *not* 598100206Sdd * just ignore them (i.e., act like they weren't specified), 599100206Sdd * since that makes a rule that only has criteria dependent on 600100206Sdd * the dev_t match all symlinks and directories. 601100206Sdd * 602100206Sdd * Note also that the following tests are somewhat reversed: 603100206Sdd * They're actually testing to see whether the condition does 604100206Sdd * *not* match, since the default is to assume the rule should 605100206Sdd * be run (such as if there are no conditions). 606100206Sdd */ 607100206Sdd if (dr->dr_icond & DRC_DSWFLAGS) 608100206Sdd if (dev == NULL || 609100206Sdd (dev->si_devsw->d_flags & dr->dr_dswflags) == 0) 610100206Sdd goto nomatch; 611100206Sdd if (dr->dr_icond & DRC_PATHPTRN) 612100206Sdd if (!devfs_rule_matchpath(dk, de)) 613100206Sdd goto nomatch; 614100206Sdd if (dr->dr_icond & DRC_MAJOR) 615100206Sdd if (dev == NULL || major(dev) != dr->dr_major) 616100206Sdd goto nomatch; 617100206Sdd 618100206Sdd return (1); 619100206Sdd 620100206Sddnomatch: 621100206Sdd return (0); 622100206Sdd} 623100206Sdd 624100206Sdd/* 625100206Sdd * Determine whether dk matches de on account of dr_pathptrn. 626100206Sdd */ 627100206Sddstatic int 628100206Sdddevfs_rule_matchpath(struct devfs_krule *dk, struct devfs_dirent *de) 629100206Sdd{ 630100206Sdd struct devfs_rule *dr = &dk->dk_rule; 631100206Sdd char *pname; 632100206Sdd dev_t dev; 633100206Sdd int plen; 634100206Sdd 635100206Sdd dev = devfs_rule_getdev(de); 636100206Sdd if (dev != NULL) 637100206Sdd pname = dev->si_name; 638100206Sdd /* XXX: Support symlinks (check d_type == DT_LNK here). */ 639100206Sdd else 640100206Sdd return (0); 641100206Sdd KASSERT(pname != NULL, ("devfs_rule_matchpath: NULL pname")); 642100206Sdd 643100206Sdd /* 644100206Sdd * XXX: Interpret dr_pathptrn as a real pattern (support '*', 645100206Sdd * '?', and perhaps brace expansion). For now, we only 646100206Sdd * support one trailing asterisk. 647100206Sdd */ 648100206Sdd plen = strlen(dr->dr_pathptrn); 649100206Sdd if (dr->dr_pathptrn[plen - 1] == '*') { 650100206Sdd if (strlen(pname) >= plen - 1 && 651100206Sdd strncmp(dr->dr_pathptrn, pname, plen - 1) == 0) 652100206Sdd return (1); 653100206Sdd } else 654100206Sdd if (strcmp(dr->dr_pathptrn, pname) == 0) 655100206Sdd return (1); 656100206Sdd return (0); 657100206Sdd} 658100206Sdd 659100206Sdd/* 660100206Sdd * Run dk on de. 661100206Sdd */ 662100206Sddstatic void 663100206Sdddevfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de) 664100206Sdd{ 665100206Sdd struct devfs_rule *dr = &dk->dk_rule; 666100206Sdd struct devfs_ruleset *ds; 667100206Sdd 668100206Sdd if (dr->dr_iacts & DRA_BACTS) { 669100206Sdd if (dr->dr_bacts & DRB_HIDE) 670100206Sdd de->de_flags |= DE_WHITEOUT; 671100206Sdd if (dr->dr_bacts & DRB_UNHIDE) 672100206Sdd de->de_flags &= ~DE_WHITEOUT; 673100206Sdd } 674100206Sdd if (dr->dr_iacts & DRA_UID) 675100206Sdd de->de_uid = dr->dr_uid; 676100206Sdd if (dr->dr_iacts & DRA_GID) 677100206Sdd de->de_gid = dr->dr_gid; 678100206Sdd if (dr->dr_iacts & DRA_MODE) 679100206Sdd de->de_mode = dr->dr_mode; 680100206Sdd if (dr->dr_iacts & DRA_INCSET) { 681100206Sdd ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset); 682100206Sdd KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset")); 683100793Sdd if (ds->ds_running) 684100793Sdd printf("Warning: avoiding loop through ruleset %d\n", 685100793Sdd ds->ds_number); 686100793Sdd else 687100206Sdd devfs_ruleset_applyde(ds, de); 688100206Sdd } 689100206Sdd} 690100206Sdd 691100206Sdd/* 692100206Sdd * Apply all the rules in ds to de. 693100206Sdd */ 694100206Sddstatic void 695100206Sdddevfs_ruleset_applyde(struct devfs_ruleset *ds, struct devfs_dirent *de) 696100206Sdd{ 697100206Sdd struct devfs_krule *dk; 698100206Sdd 699100793Sdd KASSERT(!ds->ds_running,("ruleset %d already running", ds->ds_number)); 700100793Sdd ds->ds_running = 1; 701100206Sdd SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 702100206Sdd devfs_rule_applyde(dk, de); 703100206Sdd } 704100793Sdd ds->ds_running = 0; 705100206Sdd} 706100206Sdd 707100206Sdd/* 708100206Sdd * Apply all the rules in ds to all the entires in dm. 709100206Sdd */ 710100206Sddstatic void 711100206Sdddevfs_ruleset_applydm(struct devfs_ruleset *ds, struct devfs_mount *dm) 712100206Sdd{ 713100206Sdd struct devfs_krule *dk; 714100206Sdd 715100793Sdd KASSERT(!ds->ds_running,("ruleset %d already running", ds->ds_number)); 716100793Sdd ds->ds_running = 1; 717100206Sdd /* 718100206Sdd * XXX: Does it matter whether we do 719100206Sdd * 720100206Sdd * foreach(dk in ds) 721100206Sdd * foreach(de in dm) 722100206Sdd * apply(dk to de) 723100206Sdd * 724100206Sdd * as opposed to 725100206Sdd * 726100206Sdd * foreach(de in dm) 727100206Sdd * foreach(dk in ds) 728100206Sdd * apply(dk to de) 729100206Sdd * 730100206Sdd * The end result is obviously the same, but does the order 731100206Sdd * matter? 732100206Sdd */ 733100206Sdd SLIST_FOREACH(dk, &ds->ds_rules, dk_list) { 734100206Sdd devfs_rule_applydm(dk, dm); 735100206Sdd } 736100793Sdd ds->ds_running = 0; 737100206Sdd} 738100206Sdd 739100206Sdd/* 740100206Sdd * Find a ruleset by number. 741100206Sdd */ 742100206Sddstatic struct devfs_ruleset * 743100206Sdddevfs_ruleset_bynum(devfs_rsnum rsnum) 744100206Sdd{ 745100206Sdd struct devfs_ruleset *ds; 746100206Sdd 747100206Sdd SLIST_FOREACH(ds, &devfs_rulesets, ds_list) { 748100206Sdd if (ds->ds_number == rsnum) 749100206Sdd return (ds); 750100206Sdd } 751100206Sdd return (NULL); 752100206Sdd} 753100206Sdd 754100206Sdd/* 755100206Sdd * Create a new ruleset. 756100206Sdd */ 757100206Sddstatic struct devfs_ruleset * 758100206Sdddevfs_ruleset_create(devfs_rsnum rsnum) 759100206Sdd{ 760100206Sdd struct devfs_ruleset *s1, *s2; 761100206Sdd struct devfs_ruleset *ds; 762100206Sdd 763100206Sdd KASSERT(devfs_ruleset_bynum(rsnum) == NULL, 764100206Sdd ("creating already existent ruleset %d", rsnum)); 765100206Sdd 766100206Sdd ds = malloc(sizeof(*ds), M_DEVFS, M_WAITOK | M_ZERO); 767100206Sdd ds->ds_number = rsnum; 768100206Sdd ds->ds_refcount = ds->ds_flags = 0; 769100206Sdd SLIST_INIT(&ds->ds_rules); 770100206Sdd 771100206Sdd s1 = SLIST_FIRST(&devfs_rulesets); 772100206Sdd if (s1 == NULL || s1->ds_number > rsnum) 773100206Sdd SLIST_INSERT_HEAD(&devfs_rulesets, ds, ds_list); 774100206Sdd else { 775100206Sdd SLIST_FOREACH(s1, &devfs_rulesets, ds_list) { 776100206Sdd s2 = SLIST_NEXT(s1, ds_list); 777100206Sdd if (s2 == NULL || s2->ds_number > rsnum) { 778100206Sdd SLIST_INSERT_AFTER(s1, ds, ds_list); 779100206Sdd break; 780100206Sdd } 781100206Sdd } 782100206Sdd } 783100206Sdd 784100206Sdd return (ds); 785100206Sdd} 786100206Sdd 787100206Sdd/* 788100206Sdd * Remove a ruleset form the system. The ruleset specified must be 789100206Sdd * empty and not in use. 790100206Sdd */ 791100206Sddstatic void 792100206Sdddevfs_ruleset_destroy(struct devfs_ruleset **dsp) 793100206Sdd{ 794100206Sdd struct devfs_ruleset *ds = *dsp; 795100206Sdd 796100206Sdd KASSERT(SLIST_EMPTY(&ds->ds_rules), ("destroying non-empty ruleset")); 797100206Sdd KASSERT(ds->ds_refcount == 0, ("destroying busy ruleset")); 798100206Sdd KASSERT((ds->ds_flags & DS_IMMUTABLE) == 0, 799100206Sdd ("destroying immutable ruleset")); 800100206Sdd 801100206Sdd SLIST_REMOVE(&devfs_rulesets, ds, devfs_ruleset, ds_list); 802100206Sdd free(ds, M_DEVFS); 803100206Sdd *dsp = NULL; 804100206Sdd} 805100206Sdd 806100206Sdd/* 807100206Sdd * Remove a ruleset from the system if it's empty and not used 808100206Sdd * anywhere. This should be called after every time a rule is deleted 809100206Sdd * from this ruleset or the reference count is decremented. 810100206Sdd */ 811100206Sddstatic void 812100206Sdddevfs_ruleset_reap(struct devfs_ruleset **dsp) 813100206Sdd{ 814100206Sdd struct devfs_ruleset *ds = *dsp; 815100206Sdd 816100206Sdd if (SLIST_EMPTY(&ds->ds_rules) && ds->ds_refcount == 0) { 817100206Sdd devfs_ruleset_destroy(&ds); 818100206Sdd *dsp = ds; 819100206Sdd } 820100206Sdd} 821100206Sdd 822100206Sdd/* 823100206Sdd * Make rsnum the active ruleset for dm. 824100206Sdd */ 825100206Sddstatic int 826100206Sdddevfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm) 827100206Sdd{ 828100206Sdd struct devfs_ruleset *cds, *ds; 829100206Sdd 830100206Sdd ds = devfs_ruleset_bynum(rsnum); 831100206Sdd if (ds == NULL) 832100206Sdd ds = devfs_ruleset_create(rsnum); 833100206Sdd cds = devfs_ruleset_bynum(dm->dm_ruleset); 834100206Sdd KASSERT(cds != NULL, ("mount-point has NULL ruleset")); 835100206Sdd 836100206Sdd /* These should probably be made atomic somehow. */ 837100206Sdd --cds->ds_refcount; 838100206Sdd ++ds->ds_refcount; 839100206Sdd dm->dm_ruleset = rsnum; 840100206Sdd 841100206Sdd devfs_ruleset_reap(&cds); 842100206Sdd return (0); 843100206Sdd} 844100206Sdd 845100206Sdd#endif /* !NODEVFS */ 846