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