devfs_rule.c revision 124804
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 124804 2004-01-21 18:03:54Z cperciva $
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
67100206Sdd#include <sys/param.h>
68100206Sdd#include <sys/systm.h>
69100206Sdd#include <sys/conf.h>
70100206Sdd#include <sys/kernel.h>
71100206Sdd#include <sys/malloc.h>
72100206Sdd#include <sys/dirent.h>
73100206Sdd#include <sys/vnode.h>
74100206Sdd#include <sys/mount.h>
75100206Sdd#include <sys/ioccom.h>
76100206Sdd
77100206Sdd#include <fs/devfs/devfs.h>
78100206Sdd
79100206Sdd
80100206Sdd/*
81100206Sdd * Kernel version of devfs_rule.
82100206Sdd */
83100206Sddstruct devfs_krule {
84100206Sdd	SLIST_ENTRY(devfs_krule) dk_list;
85100206Sdd	struct devfs_ruleset *dk_ruleset;
86100206Sdd	struct devfs_rule dk_rule;
87100206Sdd};
88100206Sdd
89100206Sdd/*
90100206Sdd * Structure to describe a ruleset.
91100206Sdd */
92100206Sddstruct devfs_ruleset {
93100206Sdd	SLIST_ENTRY(devfs_ruleset) ds_list;
94100206Sdd	devfs_rsnum ds_number;
95100206Sdd	SLIST_HEAD(, devfs_krule) ds_rules;
96100206Sdd	int	ds_refcount;
97100206Sdd	int	ds_flags;
98100206Sdd#define	DS_IMMUTABLE	0x001
99100793Sdd	int	ds_running;
100100206Sdd};
101100206Sdd
102100206Sddstatic devfs_rid devfs_rid_input(devfs_rid rid, struct devfs_mount *dm);
103100206Sdd
104100206Sddstatic void devfs_rule_applyde(struct devfs_krule *dk,struct devfs_dirent *de);
105100206Sddstatic void devfs_rule_applyde_recursive(struct devfs_krule *dk,
106100206Sdd		struct devfs_dirent *de);
107100206Sddstatic void devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm);
108100206Sddstatic int  devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnp);
109100206Sddstatic struct devfs_krule *devfs_rule_byid(devfs_rid rid);
110100206Sddstatic int  devfs_rule_delete(struct devfs_krule **dkp);
111100206Sddstatic dev_t devfs_rule_getdev(struct devfs_dirent *de);
112100206Sddstatic int  devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm);
113100206Sddstatic int  devfs_rule_insert(struct devfs_rule *dr);
114100206Sddstatic int  devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de);
115100206Sddstatic int  devfs_rule_matchpath(struct devfs_krule *dk,
116100206Sdd		struct devfs_dirent *de);
117100206Sddstatic void devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de);
118100206Sdd
119100206Sddstatic void devfs_ruleset_applyde(struct devfs_ruleset *ds,
120100206Sdd		struct devfs_dirent *de);
121100206Sddstatic void devfs_ruleset_applydm(struct devfs_ruleset *ds,
122100206Sdd		struct devfs_mount *dm);
123100206Sddstatic struct devfs_ruleset *devfs_ruleset_bynum(devfs_rsnum rsnum);
124100206Sddstatic struct devfs_ruleset *devfs_ruleset_create(devfs_rsnum rsnum);
125100206Sddstatic void devfs_ruleset_destroy(struct devfs_ruleset **dsp);
126100206Sddstatic void devfs_ruleset_reap(struct devfs_ruleset **dsp);
127100206Sddstatic int  devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm);
128100206Sdd
129100206Sddstatic SLIST_HEAD(, devfs_ruleset) devfs_rulesets;
130100206Sdd
131100206Sdd/*
132100206Sdd * Called to apply the proper rules for de before the latter can be
133100206Sdd * exposed to the userland.  This should be called with an exclusive
134100206Sdd * lock on dm in case we need to run anything.
135100206Sdd */
136100206Sddvoid
137100206Sdddevfs_rules_apply(struct devfs_mount *dm, struct devfs_dirent *de)
138100206Sdd{
139100206Sdd	struct devfs_ruleset *ds;
140100206Sdd
141100206Sdd	ds = devfs_ruleset_bynum(dm->dm_ruleset);
142100206Sdd	KASSERT(ds != NULL, ("mount-point has NULL ruleset"));
143100206Sdd	devfs_ruleset_applyde(ds, de);
144100206Sdd}
145100206Sdd
146100206Sdd/*
147100206Sdd * Rule subsystem SYSINIT hook.
148100206Sdd */
149100206Sddvoid
150100206Sdddevfs_rules_init(void)
151100206Sdd{
152100206Sdd	struct devfs_ruleset *ds;
153100206Sdd
154100206Sdd	SLIST_INIT(&devfs_rulesets);
155100206Sdd
156100206Sdd	ds = devfs_ruleset_create(0);
157100206Sdd	ds->ds_flags |= DS_IMMUTABLE;
158100206Sdd	ds->ds_refcount = 1;		/* Prevent reaping. */
159100206Sdd}
160100206Sdd
161100206Sdd/*
162100206Sdd * Rule subsystem ioctl hook.
163100206Sdd */
164100206Sddint
165105212Sphkdevfs_rules_ioctl(struct mount *mp, u_long cmd, caddr_t data, struct thread *td)
166100206Sdd{
167100206Sdd	struct devfs_mount *dm = VFSTODEVFS(mp);
168100206Sdd	struct devfs_ruleset *ds;
169100206Sdd	struct devfs_krule *dk;
170100206Sdd	struct devfs_rule *dr;
171100206Sdd	devfs_rsnum rsnum;
172100206Sdd	devfs_rnum rnum;
173100206Sdd	devfs_rid rid;
174100206Sdd	int error;
175100206Sdd
176100206Sdd	/*
177100206Sdd	 * XXX: This returns an error regardless of whether we
178100206Sdd	 * actually support the cmd or not.
179100206Sdd	 */
180100206Sdd	error = suser(td);
181100206Sdd	if (error != 0)
182100206Sdd		return (error);
183100206Sdd
184100206Sdd	lockmgr(&dm->dm_lock, LK_SHARED, 0, td);
185100206Sdd
186100206Sdd	switch (cmd) {
187100206Sdd	case DEVFSIO_RADD:
188100206Sdd		dr = (struct devfs_rule *)data;
189100206Sdd		error = devfs_rule_input(dr, dm);
190100206Sdd		if (error != 0)
191100206Sdd			goto out;
192100206Sdd		dk = devfs_rule_byid(dr->dr_id);
193100206Sdd		if (dk != NULL) {
194100206Sdd			error = EEXIST;
195100206Sdd			goto out;
196100206Sdd		}
197100206Sdd		lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td);
198100206Sdd		error = devfs_rule_insert(dr);
199100206Sdd		break;
200100206Sdd	case DEVFSIO_RAPPLY:
201100206Sdd		dr = (struct devfs_rule *)data;
202100206Sdd		error = devfs_rule_input(dr, dm);
203100206Sdd		if (error != 0)
204100206Sdd			goto out;
205100206Sdd
206100206Sdd		/*
207100206Sdd		 * This is one of many possible hackish
208100206Sdd		 * implementations.  The primary contender is an
209100206Sdd		 * implementation where the rule we read in is
210100206Sdd		 * temporarily inserted into some ruleset, perhaps
211100206Sdd		 * with a hypothetical DRO_NOAUTO flag so that it
212100206Sdd		 * doesn't get used where it isn't intended, and
213100206Sdd		 * applied in the normal way.  This can be done in the
214100206Sdd		 * userland (DEVFSIO_ADD, DEVFSIO_APPLYID,
215100206Sdd		 * DEVFSIO_DEL) or in the kernel; either way it breaks
216100206Sdd		 * some corner case assumptions in other parts of the
217100206Sdd		 * code (not that this implementation doesn't do
218100206Sdd		 * that).
219100206Sdd		 */
220100206Sdd		if (dr->dr_iacts & DRA_INCSET &&
221100206Sdd		    devfs_ruleset_bynum(dr->dr_incset) == NULL) {
222100206Sdd			error = ESRCH;
223100206Sdd			goto out;
224100206Sdd		}
225111119Simp		dk = malloc(sizeof(*dk), M_TEMP, M_WAITOK | M_ZERO);
226100206Sdd		memcpy(&dk->dk_rule, dr, sizeof(*dr));
227100206Sdd		lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td);
228100206Sdd		devfs_rule_applydm(dk, dm);
229100206Sdd		lockmgr(&dm->dm_lock, LK_DOWNGRADE, 0, td);
230100206Sdd		free(dk, M_TEMP);
231100206Sdd		error = 0;
232100206Sdd		break;
233100206Sdd	case DEVFSIO_RAPPLYID:
234100206Sdd		rid = *(devfs_rid *)data;
235100206Sdd		rid = devfs_rid_input(rid, dm);
236100206Sdd		dk = devfs_rule_byid(rid);
237100206Sdd		if (dk == NULL) {
238100206Sdd			error = ENOENT;
239100206Sdd			goto out;
240100206Sdd		}
241100206Sdd		lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td);
242100206Sdd		devfs_rule_applydm(dk, dm);
243100206Sdd		error = 0;
244100206Sdd		break;
245100206Sdd	case DEVFSIO_RDEL:
246100206Sdd		rid = *(devfs_rid *)data;
247100206Sdd		rid = devfs_rid_input(rid, dm);
248100206Sdd		dk = devfs_rule_byid(rid);
249100206Sdd		if (dk == NULL) {
250100206Sdd			error = ENOENT;
251100206Sdd			goto out;
252100206Sdd		}
253100206Sdd		ds = dk->dk_ruleset;
254100206Sdd		lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td);
255100206Sdd		error = devfs_rule_delete(&dk);
256100206Sdd		devfs_ruleset_reap(&ds);
257100206Sdd		break;
258100206Sdd	case DEVFSIO_RGETNEXT:
259100206Sdd		dr = (struct devfs_rule *)data;
260100206Sdd		error = devfs_rule_input(dr, dm);
261100206Sdd		if (error != 0)
262100206Sdd			goto out;
263100206Sdd		/*
264100206Sdd		 * We can't use devfs_rule_byid() here since that
265100206Sdd		 * requires the rule specified to exist, but we want
266100206Sdd		 * getnext(N) to work whether there is a rule N or not
267100206Sdd		 * (specifically, getnext(0) must work, but we should
268100206Sdd		 * never have a rule 0 since the add command
269100206Sdd		 * interprets 0 to mean "auto-number").
270100206Sdd		 */
271100206Sdd		ds = devfs_ruleset_bynum(rid2rsn(dr->dr_id));
272100206Sdd		if (ds == NULL) {
273100206Sdd			error = ENOENT;
274100206Sdd			goto out;
275100206Sdd		}
276100206Sdd		rnum = rid2rn(dr->dr_id);
277100206Sdd		SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
278100206Sdd			if (rid2rn(dk->dk_rule.dr_id) > rnum)
279100206Sdd				break;
280100206Sdd		}
281100206Sdd		if (dk == NULL) {
282100206Sdd			error = ENOENT;
283100206Sdd			goto out;
284100206Sdd		}
285100206Sdd		memcpy(dr, &dk->dk_rule, sizeof(*dr));
286100206Sdd		error = 0;
287100206Sdd		break;
288100206Sdd	case DEVFSIO_SUSE:
289100206Sdd		rsnum = *(devfs_rsnum *)data;
290100206Sdd		lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td);
291100206Sdd		error = devfs_ruleset_use(rsnum, dm);
292100206Sdd		break;
293100206Sdd	case DEVFSIO_SAPPLY:
294100206Sdd		rsnum = *(devfs_rsnum *)data;
295100206Sdd		rsnum = rid2rsn(devfs_rid_input(mkrid(rsnum, 0), dm));
296100206Sdd		ds = devfs_ruleset_bynum(rsnum);
297100206Sdd		if (ds == NULL) {
298100206Sdd			error = ESRCH;
299100206Sdd			goto out;
300100206Sdd		}
301100206Sdd		lockmgr(&dm->dm_lock, LK_UPGRADE, 0, td);
302100206Sdd		devfs_ruleset_applydm(ds, dm);
303100206Sdd		error = 0;
304100206Sdd		break;
305100206Sdd	case DEVFSIO_SGETNEXT:
306100206Sdd		rsnum = *(devfs_rsnum *)data;
307100206Sdd		SLIST_FOREACH(ds, &devfs_rulesets, ds_list) {
308100206Sdd			if (ds->ds_number > rsnum)
309100206Sdd				break;
310100206Sdd		}
311100206Sdd		if (ds == NULL)
312100206Sdd			error = ENOENT;
313100206Sdd		else {
314100206Sdd			*(devfs_rsnum *)data = ds->ds_number;
315100206Sdd			error = 0;
316100206Sdd		}
317100206Sdd		break;
318100206Sdd	default:
319100206Sdd		error = ENOIOCTL;
320100206Sdd		break;
321100206Sdd	}
322100206Sdd
323100206Sddout:
324100206Sdd	lockmgr(&dm->dm_lock, LK_RELEASE, 0, td);
325100206Sdd	return (error);
326100206Sdd}
327100206Sdd
328100206Sdd/*
329100206Sdd * Called to initialize dm_ruleset when there is a new mount-point.
330100206Sdd */
331100206Sddvoid
332100206Sdddevfs_rules_newmount(struct devfs_mount *dm, struct thread *td)
333100206Sdd{
334100206Sdd	struct devfs_ruleset *ds;
335100206Sdd
336100206Sdd	lockmgr(&dm->dm_lock, LK_EXCLUSIVE, 0, td);
337100206Sdd	/*
338100206Sdd	 * We can't use devfs_ruleset_use() since it will try to
339100206Sdd	 * decrement the refcount for the old ruleset, and there is no
340100206Sdd	 * old ruleset.  Making some value of ds_ruleset "special" to
341100206Sdd	 * mean "don't decrement refcount" is uglier than this.
342100206Sdd	 */
343100206Sdd	ds = devfs_ruleset_bynum(0);
344100206Sdd	KASSERT(ds != NULL, ("no ruleset 0"));
345100206Sdd	++ds->ds_refcount;
346100206Sdd	dm->dm_ruleset = 0;
347100206Sdd	lockmgr(&dm->dm_lock, LK_RELEASE, 0, td);
348100206Sdd}
349100206Sdd
350100206Sdd/*
351100206Sdd * Adjust the rule identifier to use the ruleset of dm if one isn't
352100206Sdd * explicitly specified.
353100206Sdd *
354100206Sdd * Note that after this operation, rid2rsn(rid) might still be 0, and
355100206Sdd * that's okay; ruleset 0 is a valid ruleset, but when it's read in
356100206Sdd * from the userland, it means "current ruleset for this mount-point".
357100206Sdd */
358100206Sddstatic devfs_rid
359100206Sdddevfs_rid_input(devfs_rid rid, struct devfs_mount *dm)
360100206Sdd{
361100206Sdd
362100206Sdd	if (rid2rsn(rid) == 0)
363100206Sdd		return (mkrid(dm->dm_ruleset, rid2rn(rid)));
364100206Sdd	else
365100206Sdd		return (rid);
366100206Sdd}
367100206Sdd
368100206Sdd/*
369100206Sdd * Apply dk to de.
370100206Sdd */
371100206Sddstatic void
372100206Sdddevfs_rule_applyde(struct devfs_krule *dk, struct devfs_dirent *de)
373100206Sdd{
374100206Sdd
375100206Sdd	if (devfs_rule_match(dk, de))
376100206Sdd		devfs_rule_run(dk, de);
377100206Sdd}
378100206Sdd
379100206Sdd/*
380100206Sdd * Apply dk to de and everything under de.
381100206Sdd *
382100206Sdd * XXX: This method needs a function call for every nested
383100206Sdd * subdirectory in a devfs mount.  If we plan to have many of these,
384100206Sdd * we might eventually run out of kernel stack space.
385100206Sdd */
386100206Sddstatic void
387100206Sdddevfs_rule_applyde_recursive(struct devfs_krule *dk, struct devfs_dirent *de)
388100206Sdd{
389100206Sdd	struct devfs_dirent *de2;
390100206Sdd
391100206Sdd	/* XXX: Should we apply to ourselves first or last?  Does it matter? */
392100206Sdd	TAILQ_FOREACH(de2, &de->de_dlist, de_list) {
393100206Sdd		devfs_rule_applyde_recursive(dk, de2);
394100206Sdd	}
395100206Sdd	devfs_rule_applyde(dk, de);
396100206Sdd}
397100206Sdd
398100206Sdd/*
399100206Sdd * Apply dk to all entires in dm.
400100206Sdd */
401100206Sddstatic void
402100206Sdddevfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm)
403100206Sdd{
404100206Sdd
405100206Sdd	devfs_rule_applyde_recursive(dk, dm->dm_basedir);
406100206Sdd}
407100206Sdd
408100206Sdd/*
409100206Sdd * Automatically select a number for a new rule in ds, and write the
410100206Sdd * result into rnump.
411100206Sdd */
412100206Sddstatic int
413100206Sdddevfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnump)
414100206Sdd{
415100206Sdd	struct devfs_krule *dk;
416100206Sdd
417100206Sdd	/* Find the last rule. */
418100206Sdd	SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
419100206Sdd		if (SLIST_NEXT(dk, dk_list) == NULL)
420100206Sdd			break;
421100206Sdd	}
422100206Sdd	if (dk == NULL)
423100206Sdd		*rnump = 100;
424100206Sdd	else {
425100206Sdd		*rnump = rid2rn(dk->dk_rule.dr_id) + 100;
426100206Sdd		/* Detect overflow. */
427100206Sdd		if (*rnump < rid2rn(dk->dk_rule.dr_id))
428100206Sdd			return (ERANGE);
429100206Sdd	}
430100206Sdd	KASSERT(devfs_rule_byid(mkrid(ds->ds_number, *rnump)) == NULL,
431100206Sdd	    ("autonumbering resulted in an already existing rule"));
432100206Sdd	return (0);
433100206Sdd}
434100206Sdd
435100206Sdd/*
436100206Sdd * Find a krule by id.
437100206Sdd */
438100206Sddstatic struct devfs_krule *
439100206Sdddevfs_rule_byid(devfs_rid rid)
440100206Sdd{
441100206Sdd	struct devfs_ruleset *ds;
442100206Sdd	struct devfs_krule *dk;
443100206Sdd	devfs_rnum rn;
444100206Sdd
445100206Sdd	rn = rid2rn(rid);
446100206Sdd	ds = devfs_ruleset_bynum(rid2rsn(rid));
447100206Sdd	if (ds == NULL)
448100206Sdd		return (NULL);
449100206Sdd	SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
450100206Sdd		if (rid2rn(dk->dk_rule.dr_id) == rn)
451100206Sdd			return (dk);
452100206Sdd		else if (rid2rn(dk->dk_rule.dr_id) > rn)
453100206Sdd			break;
454100206Sdd	}
455100206Sdd	return (NULL);
456100206Sdd}
457100206Sdd
458100206Sdd/*
459100206Sdd * Remove dkp from any lists it may be on and remove memory associated
460100206Sdd * with it.
461100206Sdd */
462100206Sddstatic int
463100206Sdddevfs_rule_delete(struct devfs_krule **dkp)
464100206Sdd{
465100206Sdd	struct devfs_krule *dk = *dkp;
466100206Sdd	struct devfs_ruleset *ds;
467100206Sdd
468100206Sdd	if (dk->dk_rule.dr_iacts & DRA_INCSET) {
469100206Sdd		ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset);
470100206Sdd		KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset"));
471100206Sdd		--ds->ds_refcount;
472100206Sdd		devfs_ruleset_reap(&ds);
473100206Sdd	}
474100206Sdd	SLIST_REMOVE(&dk->dk_ruleset->ds_rules, dk, devfs_krule, dk_list);
475100206Sdd	free(dk, M_DEVFS);
476100206Sdd	*dkp = NULL;
477100206Sdd	return (0);
478100206Sdd}
479100206Sdd
480100206Sdd/*
481100206Sdd * Get a dev_t corresponding to de so we can try to match rules based
482100206Sdd * on it.  If this routine returns NULL, there is no dev_t associated
483100206Sdd * with the dirent (symlinks and directories don't have dev_ts), and
484100206Sdd * the caller should assume that any critera dependent on a dev_t
485100206Sdd * don't match.
486100206Sdd */
487100206Sddstatic dev_t
488100206Sdddevfs_rule_getdev(struct devfs_dirent *de)
489100206Sdd{
490100206Sdd	dev_t *devp, dev;
491100206Sdd
492100206Sdd	devp = devfs_itod(de->de_inode);
493100206Sdd	if (devp != NULL)
494100206Sdd		dev = *devp;
495100206Sdd	else
496100206Sdd		dev = NULL;
497100206Sdd	/* If we think this dirent should have a dev_t, alert the user. */
498100206Sdd	if (dev == NULL && de->de_dirent->d_type != DT_LNK &&
499100206Sdd	    de->de_dirent->d_type != DT_DIR)
500100206Sdd		printf("Warning: no dev_t for %s\n", de->de_dirent->d_name);
501100206Sdd	return (dev);
502100206Sdd}
503100206Sdd
504100206Sdd/*
505100206Sdd * Do what we need to do to a rule that we just loaded from the
506100206Sdd * userland.  In particular, we need to check the magic, and adjust
507100206Sdd * the ruleset appropriate if desired.
508100206Sdd */
509100206Sddstatic int
510100206Sdddevfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm)
511100206Sdd{
512100206Sdd
513100206Sdd	if (dr->dr_magic != DEVFS_MAGIC)
514100206Sdd		return (ERPCMISMATCH);
515100206Sdd	dr->dr_id = devfs_rid_input(dr->dr_id, dm);
516100206Sdd	return (0);
517100206Sdd}
518100206Sdd
519100206Sdd/*
520100206Sdd * Import dr into the appropriate place in the kernel (i.e., make a
521100206Sdd * krule).  The value of dr is copied, so the pointer may be destroyed
522100206Sdd * after this call completes.
523100206Sdd */
524100206Sddstatic int
525100206Sdddevfs_rule_insert(struct devfs_rule *dr)
526100206Sdd{
527100206Sdd	struct devfs_ruleset *ds, *dsi;
528100206Sdd	struct devfs_krule *k1, *k2;
529100206Sdd	struct devfs_krule *dk;
530100206Sdd	devfs_rsnum rsnum;
531100206Sdd	devfs_rnum dkrn;
532100206Sdd	int error;
533100206Sdd
534100206Sdd	/*
535100206Sdd	 * This stuff seems out of place here, but we want to do it as
536100206Sdd	 * soon as possible so that if it fails, we don't have to roll
537100206Sdd	 * back any changes we already made (e.g., ruleset creation).
538100206Sdd	 */
539100206Sdd	if (dr->dr_iacts & DRA_INCSET) {
540100206Sdd		dsi = devfs_ruleset_bynum(dr->dr_incset);
541100206Sdd		if (dsi == NULL)
542100206Sdd			return (ESRCH);
543100206Sdd	} else
544100206Sdd		dsi = NULL;
545100206Sdd
546100206Sdd	rsnum = rid2rsn(dr->dr_id);
547100206Sdd	ds = devfs_ruleset_bynum(rsnum);
548100206Sdd	if (ds == NULL)
549100206Sdd		ds = devfs_ruleset_create(rsnum);
550100206Sdd	if (ds->ds_flags & DS_IMMUTABLE)
551100206Sdd		return (EIO);
552100206Sdd	dkrn = rid2rn(dr->dr_id);
553100206Sdd	if (dkrn == 0) {
554100206Sdd		error = devfs_rule_autonumber(ds, &dkrn);
555100206Sdd		if (error != 0)
556100206Sdd			return (error);
557100206Sdd	}
558100206Sdd
559111119Simp	dk = malloc(sizeof(*dk), M_DEVFS, M_WAITOK);
560100206Sdd	dk->dk_ruleset = ds;
561100206Sdd	if (dsi != NULL)
562100206Sdd		++dsi->ds_refcount;
563100206Sdd	/* XXX: Inspect dr? */
564100206Sdd	memcpy(&dk->dk_rule, dr, sizeof(*dr));
565100206Sdd	dk->dk_rule.dr_id = mkrid(rid2rsn(dk->dk_rule.dr_id), dkrn);
566100206Sdd
567100206Sdd	k1 = SLIST_FIRST(&ds->ds_rules);
568100206Sdd	if (k1 == NULL || rid2rn(k1->dk_rule.dr_id) > dkrn)
569100206Sdd		SLIST_INSERT_HEAD(&ds->ds_rules, dk, dk_list);
570100206Sdd	else {
571100206Sdd		SLIST_FOREACH(k1, &ds->ds_rules, dk_list) {
572100206Sdd			k2 = SLIST_NEXT(k1, dk_list);
573100206Sdd			if (k2 == NULL || rid2rn(k2->dk_rule.dr_id) > dkrn) {
574100206Sdd				SLIST_INSERT_AFTER(k1, dk, dk_list);
575100206Sdd				break;
576100206Sdd			}
577100206Sdd		}
578100206Sdd	}
579100206Sdd
580100206Sdd	return (0);
581100206Sdd}
582100206Sdd
583100206Sdd/*
584100206Sdd * Determine whether dk matches de.  Returns 1 if dk should be run on
585100206Sdd * de; 0, otherwise.
586100206Sdd */
587100206Sddstatic int
588100206Sdddevfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de)
589100206Sdd{
590100206Sdd	struct devfs_rule *dr = &dk->dk_rule;
591100206Sdd	dev_t dev;
592100206Sdd
593100206Sdd	dev = devfs_rule_getdev(de);
594100206Sdd	/*
595100206Sdd	 * At this point, if dev is NULL, we should assume that any
596100206Sdd	 * criteria that depend on it don't match.  We should *not*
597100206Sdd	 * just ignore them (i.e., act like they weren't specified),
598100206Sdd	 * since that makes a rule that only has criteria dependent on
599100206Sdd	 * the dev_t match all symlinks and directories.
600100206Sdd	 *
601100206Sdd	 * Note also that the following tests are somewhat reversed:
602100206Sdd	 * They're actually testing to see whether the condition does
603100206Sdd	 * *not* match, since the default is to assume the rule should
604100206Sdd	 * be run (such as if there are no conditions).
605100206Sdd	 */
606100206Sdd	if (dr->dr_icond & DRC_DSWFLAGS)
607100206Sdd		if (dev == NULL ||
608100206Sdd		    (dev->si_devsw->d_flags & dr->dr_dswflags) == 0)
609100206Sdd			goto nomatch;
610100206Sdd	if (dr->dr_icond & DRC_PATHPTRN)
611100206Sdd		if (!devfs_rule_matchpath(dk, de))
612100206Sdd			goto nomatch;
613100206Sdd	if (dr->dr_icond & DRC_MAJOR)
614100206Sdd		if (dev == NULL || major(dev) != dr->dr_major)
615100206Sdd			goto nomatch;
616100206Sdd
617100206Sdd	return (1);
618100206Sdd
619100206Sddnomatch:
620100206Sdd	return (0);
621100206Sdd}
622100206Sdd
623100206Sdd/*
624100206Sdd * Determine whether dk matches de on account of dr_pathptrn.
625100206Sdd */
626100206Sddstatic int
627100206Sdddevfs_rule_matchpath(struct devfs_krule *dk, struct devfs_dirent *de)
628100206Sdd{
629100206Sdd	struct devfs_rule *dr = &dk->dk_rule;
630100206Sdd	char *pname;
631100206Sdd	dev_t dev;
632100206Sdd
633100206Sdd	dev = devfs_rule_getdev(de);
634100206Sdd	if (dev != NULL)
635100206Sdd		pname = dev->si_name;
636124798Scperciva	else if (de->de_dirent->d_type == DT_LNK ||
637124804Scperciva	    de->de_dirent->d_type == DT_DIR)
638109090Sdd		pname = de->de_dirent->d_name;
639100206Sdd	else
640100206Sdd		return (0);
641100206Sdd	KASSERT(pname != NULL, ("devfs_rule_matchpath: NULL pname"));
642100206Sdd
643104653Sdd	return (fnmatch(dr->dr_pathptrn, pname, 0) == 0);
644100206Sdd}
645100206Sdd
646100206Sdd/*
647100206Sdd * Run dk on de.
648100206Sdd */
649100206Sddstatic void
650100206Sdddevfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de)
651100206Sdd{
652100206Sdd	struct devfs_rule *dr = &dk->dk_rule;
653100206Sdd	struct devfs_ruleset *ds;
654100206Sdd
655100206Sdd	if (dr->dr_iacts & DRA_BACTS) {
656100206Sdd		if (dr->dr_bacts & DRB_HIDE)
657100206Sdd			de->de_flags |= DE_WHITEOUT;
658100206Sdd		if (dr->dr_bacts & DRB_UNHIDE)
659100206Sdd			de->de_flags &= ~DE_WHITEOUT;
660100206Sdd	}
661100206Sdd	if (dr->dr_iacts & DRA_UID)
662100206Sdd		de->de_uid = dr->dr_uid;
663100206Sdd	if (dr->dr_iacts & DRA_GID)
664100206Sdd		de->de_gid = dr->dr_gid;
665100206Sdd	if (dr->dr_iacts & DRA_MODE)
666100206Sdd		de->de_mode = dr->dr_mode;
667100206Sdd	if (dr->dr_iacts & DRA_INCSET) {
668100206Sdd		ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset);
669100206Sdd		KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset"));
670100793Sdd		if (ds->ds_running)
671100793Sdd			printf("Warning: avoiding loop through ruleset %d\n",
672100793Sdd			    ds->ds_number);
673100793Sdd		else
674100206Sdd			devfs_ruleset_applyde(ds, de);
675100206Sdd	}
676100206Sdd}
677100206Sdd
678100206Sdd/*
679100206Sdd * Apply all the rules in ds to de.
680100206Sdd */
681100206Sddstatic void
682100206Sdddevfs_ruleset_applyde(struct devfs_ruleset *ds, struct devfs_dirent *de)
683100206Sdd{
684100206Sdd	struct devfs_krule *dk;
685100206Sdd
686100793Sdd	KASSERT(!ds->ds_running,("ruleset %d already running", ds->ds_number));
687100793Sdd	ds->ds_running = 1;
688100206Sdd	SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
689100206Sdd		devfs_rule_applyde(dk, de);
690100206Sdd	}
691100793Sdd	ds->ds_running = 0;
692100206Sdd}
693100206Sdd
694100206Sdd/*
695100206Sdd * Apply all the rules in ds to all the entires in dm.
696100206Sdd */
697100206Sddstatic void
698100206Sdddevfs_ruleset_applydm(struct devfs_ruleset *ds, struct devfs_mount *dm)
699100206Sdd{
700100206Sdd	struct devfs_krule *dk;
701100206Sdd
702100793Sdd	KASSERT(!ds->ds_running,("ruleset %d already running", ds->ds_number));
703100793Sdd	ds->ds_running = 1;
704100206Sdd	/*
705100206Sdd	 * XXX: Does it matter whether we do
706100206Sdd	 *
707100206Sdd	 *	foreach(dk in ds)
708100206Sdd	 *		foreach(de in dm)
709100206Sdd	 *			apply(dk to de)
710100206Sdd	 *
711100206Sdd	 * as opposed to
712100206Sdd	 *
713100206Sdd	 *	foreach(de in dm)
714100206Sdd	 *		foreach(dk in ds)
715100206Sdd	 *			apply(dk to de)
716100206Sdd	 *
717100206Sdd	 * The end result is obviously the same, but does the order
718100206Sdd	 * matter?
719100206Sdd	 */
720100206Sdd	SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
721100206Sdd		devfs_rule_applydm(dk, dm);
722100206Sdd	}
723100793Sdd	ds->ds_running = 0;
724100206Sdd}
725100206Sdd
726100206Sdd/*
727100206Sdd * Find a ruleset by number.
728100206Sdd */
729100206Sddstatic struct devfs_ruleset *
730100206Sdddevfs_ruleset_bynum(devfs_rsnum rsnum)
731100206Sdd{
732100206Sdd	struct devfs_ruleset *ds;
733100206Sdd
734100206Sdd	SLIST_FOREACH(ds, &devfs_rulesets, ds_list) {
735100206Sdd		if (ds->ds_number == rsnum)
736100206Sdd			return (ds);
737100206Sdd	}
738100206Sdd	return (NULL);
739100206Sdd}
740100206Sdd
741100206Sdd/*
742100206Sdd * Create a new ruleset.
743100206Sdd */
744100206Sddstatic struct devfs_ruleset *
745100206Sdddevfs_ruleset_create(devfs_rsnum rsnum)
746100206Sdd{
747100206Sdd	struct devfs_ruleset *s1, *s2;
748100206Sdd	struct devfs_ruleset *ds;
749100206Sdd
750100206Sdd	KASSERT(devfs_ruleset_bynum(rsnum) == NULL,
751100206Sdd	    ("creating already existent ruleset %d", rsnum));
752100206Sdd
753111119Simp	ds = malloc(sizeof(*ds), M_DEVFS, M_WAITOK | M_ZERO);
754100206Sdd	ds->ds_number = rsnum;
755100206Sdd	ds->ds_refcount = ds->ds_flags = 0;
756100206Sdd	SLIST_INIT(&ds->ds_rules);
757100206Sdd
758100206Sdd	s1 = SLIST_FIRST(&devfs_rulesets);
759100206Sdd	if (s1 == NULL || s1->ds_number > rsnum)
760100206Sdd		SLIST_INSERT_HEAD(&devfs_rulesets, ds, ds_list);
761100206Sdd	else {
762100206Sdd		SLIST_FOREACH(s1, &devfs_rulesets, ds_list) {
763100206Sdd			s2 = SLIST_NEXT(s1, ds_list);
764100206Sdd			if (s2 == NULL || s2->ds_number > rsnum) {
765100206Sdd				SLIST_INSERT_AFTER(s1, ds, ds_list);
766100206Sdd				break;
767100206Sdd			}
768100206Sdd		}
769100206Sdd	}
770100206Sdd
771100206Sdd	return (ds);
772100206Sdd}
773100206Sdd
774100206Sdd/*
775100206Sdd * Remove a ruleset form the system.  The ruleset specified must be
776100206Sdd * empty and not in use.
777100206Sdd */
778100206Sddstatic void
779100206Sdddevfs_ruleset_destroy(struct devfs_ruleset **dsp)
780100206Sdd{
781100206Sdd	struct devfs_ruleset *ds = *dsp;
782100206Sdd
783100206Sdd	KASSERT(SLIST_EMPTY(&ds->ds_rules), ("destroying non-empty ruleset"));
784100206Sdd	KASSERT(ds->ds_refcount == 0, ("destroying busy ruleset"));
785100206Sdd	KASSERT((ds->ds_flags & DS_IMMUTABLE) == 0,
786100206Sdd	    ("destroying immutable ruleset"));
787100206Sdd
788100206Sdd	SLIST_REMOVE(&devfs_rulesets, ds, devfs_ruleset, ds_list);
789100206Sdd	free(ds, M_DEVFS);
790100206Sdd	*dsp = NULL;
791100206Sdd}
792100206Sdd
793100206Sdd/*
794100206Sdd * Remove a ruleset from the system if it's empty and not used
795100206Sdd * anywhere.  This should be called after every time a rule is deleted
796100206Sdd * from this ruleset or the reference count is decremented.
797100206Sdd */
798100206Sddstatic void
799100206Sdddevfs_ruleset_reap(struct devfs_ruleset **dsp)
800100206Sdd{
801100206Sdd	struct devfs_ruleset *ds = *dsp;
802100206Sdd
803100206Sdd	if (SLIST_EMPTY(&ds->ds_rules) && ds->ds_refcount == 0) {
804100206Sdd		devfs_ruleset_destroy(&ds);
805100206Sdd		*dsp = ds;
806100206Sdd	}
807100206Sdd}
808100206Sdd
809100206Sdd/*
810100206Sdd * Make rsnum the active ruleset for dm.
811100206Sdd */
812100206Sddstatic int
813100206Sdddevfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm)
814100206Sdd{
815100206Sdd	struct devfs_ruleset *cds, *ds;
816100206Sdd
817100206Sdd	ds = devfs_ruleset_bynum(rsnum);
818100206Sdd	if (ds == NULL)
819100206Sdd		ds = devfs_ruleset_create(rsnum);
820100206Sdd	cds = devfs_ruleset_bynum(dm->dm_ruleset);
821100206Sdd	KASSERT(cds != NULL, ("mount-point has NULL ruleset"));
822100206Sdd
823100206Sdd	/* These should probably be made atomic somehow. */
824100206Sdd	--cds->ds_refcount;
825100206Sdd	++ds->ds_refcount;
826100206Sdd	dm->dm_ruleset = rsnum;
827100206Sdd
828100206Sdd	devfs_ruleset_reap(&cds);
829100206Sdd	return (0);
830100206Sdd}
831100206Sdd
832