devfs_rule.c revision 150150
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 150150 2005-09-15 08:50:16Z 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/ioccom.h>
75150150Sphk#include <sys/sx.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
99100206Sdd};
100100206Sdd
101100206Sddstatic devfs_rid devfs_rid_input(devfs_rid rid, struct devfs_mount *dm);
102100206Sdd
103100206Sddstatic void devfs_rule_applyde_recursive(struct devfs_krule *dk,
104100206Sdd		struct devfs_dirent *de);
105100206Sddstatic void devfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm);
106100206Sddstatic int  devfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnp);
107100206Sddstatic struct devfs_krule *devfs_rule_byid(devfs_rid rid);
108100206Sddstatic int  devfs_rule_delete(struct devfs_krule **dkp);
109130585Sphkstatic struct cdev *devfs_rule_getdev(struct devfs_dirent *de);
110100206Sddstatic int  devfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm);
111100206Sddstatic int  devfs_rule_insert(struct devfs_rule *dr);
112100206Sddstatic int  devfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de);
113100206Sddstatic int  devfs_rule_matchpath(struct devfs_krule *dk,
114100206Sdd		struct devfs_dirent *de);
115150147Sphkstatic void devfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de, unsigned depth);
116100206Sdd
117100206Sddstatic void devfs_ruleset_applyde(struct devfs_ruleset *ds,
118150147Sphk		struct devfs_dirent *de, unsigned depth);
119100206Sddstatic void devfs_ruleset_applydm(struct devfs_ruleset *ds,
120100206Sdd		struct devfs_mount *dm);
121100206Sddstatic struct devfs_ruleset *devfs_ruleset_bynum(devfs_rsnum rsnum);
122100206Sddstatic struct devfs_ruleset *devfs_ruleset_create(devfs_rsnum rsnum);
123100206Sddstatic void devfs_ruleset_destroy(struct devfs_ruleset **dsp);
124100206Sddstatic void devfs_ruleset_reap(struct devfs_ruleset **dsp);
125100206Sddstatic int  devfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm);
126100206Sdd
127150150Sphkstatic struct sx sx_rules;
128100206Sddstatic SLIST_HEAD(, devfs_ruleset) devfs_rulesets;
129100206Sdd
130100206Sdd/*
131150147Sphk * Called to apply the proper rules for 'de' before it can be
132100206Sdd * exposed to the userland.  This should be called with an exclusive
133100206Sdd * lock on dm in case we need to run anything.
134100206Sdd */
135100206Sddvoid
136100206Sdddevfs_rules_apply(struct devfs_mount *dm, struct devfs_dirent *de)
137100206Sdd{
138100206Sdd	struct devfs_ruleset *ds;
139100206Sdd
140150150Sphk	sx_slock(&sx_rules);
141100206Sdd	ds = devfs_ruleset_bynum(dm->dm_ruleset);
142100206Sdd	KASSERT(ds != NULL, ("mount-point has NULL ruleset"));
143150147Sphk	devfs_ruleset_applyde(ds, de, devfs_rule_depth);
144150150Sphk	sx_sunlock(&sx_rules);
145100206Sdd}
146100206Sdd
147100206Sdd/*
148100206Sdd * Rule subsystem SYSINIT hook.
149100206Sdd */
150125855Sphkstatic void
151125855Sphkdevfs_rules_init(void *junk __unused)
152100206Sdd{
153100206Sdd	struct devfs_ruleset *ds;
154100206Sdd
155150150Sphk	sx_init(&sx_rules, "devfsrules");
156100206Sdd	SLIST_INIT(&devfs_rulesets);
157100206Sdd
158100206Sdd	ds = devfs_ruleset_create(0);
159100206Sdd	ds->ds_flags |= DS_IMMUTABLE;
160100206Sdd	ds->ds_refcount = 1;		/* Prevent reaping. */
161100206Sdd}
162100206Sdd
163125855SphkSYSINIT(devfs_rules, SI_SUB_DEVFS, SI_ORDER_FIRST, devfs_rules_init, NULL);
164125855Sphk
165100206Sdd/*
166100206Sdd * Rule subsystem ioctl hook.
167100206Sdd */
168100206Sddint
169150149Sphkdevfs_rules_ioctl(struct devfs_mount *dm, u_long cmd, caddr_t data, struct thread *td)
170100206Sdd{
171100206Sdd	struct devfs_ruleset *ds;
172100206Sdd	struct devfs_krule *dk;
173100206Sdd	struct devfs_rule *dr;
174100206Sdd	devfs_rsnum rsnum;
175100206Sdd	devfs_rnum rnum;
176100206Sdd	devfs_rid rid;
177100206Sdd	int error;
178100206Sdd
179100206Sdd	/*
180100206Sdd	 * XXX: This returns an error regardless of whether we
181100206Sdd	 * actually support the cmd or not.
182100206Sdd	 */
183100206Sdd	error = suser(td);
184100206Sdd	if (error != 0)
185100206Sdd		return (error);
186100206Sdd
187150150Sphk	sx_xlock(&sx_rules);
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		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		}
226111119Simp		dk = malloc(sizeof(*dk), M_TEMP, M_WAITOK | M_ZERO);
227100206Sdd		memcpy(&dk->dk_rule, dr, sizeof(*dr));
228100206Sdd		devfs_rule_applydm(dk, dm);
229100206Sdd		free(dk, M_TEMP);
230100206Sdd		error = 0;
231100206Sdd		break;
232100206Sdd	case DEVFSIO_RAPPLYID:
233100206Sdd		rid = *(devfs_rid *)data;
234100206Sdd		rid = devfs_rid_input(rid, dm);
235100206Sdd		dk = devfs_rule_byid(rid);
236100206Sdd		if (dk == NULL) {
237100206Sdd			error = ENOENT;
238100206Sdd			goto out;
239100206Sdd		}
240100206Sdd		devfs_rule_applydm(dk, dm);
241100206Sdd		error = 0;
242100206Sdd		break;
243100206Sdd	case DEVFSIO_RDEL:
244100206Sdd		rid = *(devfs_rid *)data;
245100206Sdd		rid = devfs_rid_input(rid, dm);
246100206Sdd		dk = devfs_rule_byid(rid);
247100206Sdd		if (dk == NULL) {
248100206Sdd			error = ENOENT;
249100206Sdd			goto out;
250100206Sdd		}
251100206Sdd		ds = dk->dk_ruleset;
252100206Sdd		error = devfs_rule_delete(&dk);
253100206Sdd		devfs_ruleset_reap(&ds);
254100206Sdd		break;
255100206Sdd	case DEVFSIO_RGETNEXT:
256100206Sdd		dr = (struct devfs_rule *)data;
257100206Sdd		error = devfs_rule_input(dr, dm);
258100206Sdd		if (error != 0)
259100206Sdd			goto out;
260100206Sdd		/*
261100206Sdd		 * We can't use devfs_rule_byid() here since that
262100206Sdd		 * requires the rule specified to exist, but we want
263100206Sdd		 * getnext(N) to work whether there is a rule N or not
264100206Sdd		 * (specifically, getnext(0) must work, but we should
265100206Sdd		 * never have a rule 0 since the add command
266100206Sdd		 * interprets 0 to mean "auto-number").
267100206Sdd		 */
268100206Sdd		ds = devfs_ruleset_bynum(rid2rsn(dr->dr_id));
269100206Sdd		if (ds == NULL) {
270100206Sdd			error = ENOENT;
271100206Sdd			goto out;
272100206Sdd		}
273100206Sdd		rnum = rid2rn(dr->dr_id);
274100206Sdd		SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
275100206Sdd			if (rid2rn(dk->dk_rule.dr_id) > rnum)
276100206Sdd				break;
277100206Sdd		}
278100206Sdd		if (dk == NULL) {
279100206Sdd			error = ENOENT;
280100206Sdd			goto out;
281100206Sdd		}
282100206Sdd		memcpy(dr, &dk->dk_rule, sizeof(*dr));
283100206Sdd		error = 0;
284100206Sdd		break;
285100206Sdd	case DEVFSIO_SUSE:
286100206Sdd		rsnum = *(devfs_rsnum *)data;
287100206Sdd		error = devfs_ruleset_use(rsnum, dm);
288100206Sdd		break;
289100206Sdd	case DEVFSIO_SAPPLY:
290100206Sdd		rsnum = *(devfs_rsnum *)data;
291100206Sdd		rsnum = rid2rsn(devfs_rid_input(mkrid(rsnum, 0), dm));
292100206Sdd		ds = devfs_ruleset_bynum(rsnum);
293100206Sdd		if (ds == NULL) {
294100206Sdd			error = ESRCH;
295100206Sdd			goto out;
296100206Sdd		}
297100206Sdd		devfs_ruleset_applydm(ds, dm);
298100206Sdd		error = 0;
299100206Sdd		break;
300100206Sdd	case DEVFSIO_SGETNEXT:
301100206Sdd		rsnum = *(devfs_rsnum *)data;
302100206Sdd		SLIST_FOREACH(ds, &devfs_rulesets, ds_list) {
303100206Sdd			if (ds->ds_number > rsnum)
304100206Sdd				break;
305100206Sdd		}
306100206Sdd		if (ds == NULL)
307100206Sdd			error = ENOENT;
308100206Sdd		else {
309100206Sdd			*(devfs_rsnum *)data = ds->ds_number;
310100206Sdd			error = 0;
311100206Sdd		}
312100206Sdd		break;
313100206Sdd	default:
314100206Sdd		error = ENOIOCTL;
315100206Sdd		break;
316100206Sdd	}
317100206Sdd
318100206Sddout:
319150150Sphk	sx_xunlock(&sx_rules);
320100206Sdd	return (error);
321100206Sdd}
322100206Sdd
323100206Sdd/*
324100206Sdd * Called to initialize dm_ruleset when there is a new mount-point.
325100206Sdd */
326100206Sddvoid
327100206Sdddevfs_rules_newmount(struct devfs_mount *dm, struct thread *td)
328100206Sdd{
329100206Sdd	struct devfs_ruleset *ds;
330100206Sdd
331100206Sdd	/*
332100206Sdd	 * We can't use devfs_ruleset_use() since it will try to
333100206Sdd	 * decrement the refcount for the old ruleset, and there is no
334100206Sdd	 * old ruleset.  Making some value of ds_ruleset "special" to
335100206Sdd	 * mean "don't decrement refcount" is uglier than this.
336100206Sdd	 */
337150150Sphk	sx_slock(&sx_rules);
338100206Sdd	ds = devfs_ruleset_bynum(0);
339100206Sdd	KASSERT(ds != NULL, ("no ruleset 0"));
340100206Sdd	++ds->ds_refcount;
341100206Sdd	dm->dm_ruleset = 0;
342150150Sphk	sx_sunlock(&sx_rules);
343100206Sdd}
344100206Sdd
345100206Sdd/*
346100206Sdd * Adjust the rule identifier to use the ruleset of dm if one isn't
347100206Sdd * explicitly specified.
348100206Sdd *
349100206Sdd * Note that after this operation, rid2rsn(rid) might still be 0, and
350100206Sdd * that's okay; ruleset 0 is a valid ruleset, but when it's read in
351100206Sdd * from the userland, it means "current ruleset for this mount-point".
352100206Sdd */
353100206Sddstatic devfs_rid
354100206Sdddevfs_rid_input(devfs_rid rid, struct devfs_mount *dm)
355100206Sdd{
356100206Sdd
357100206Sdd	if (rid2rsn(rid) == 0)
358100206Sdd		return (mkrid(dm->dm_ruleset, rid2rn(rid)));
359100206Sdd	else
360100206Sdd		return (rid);
361100206Sdd}
362100206Sdd
363100206Sdd/*
364100206Sdd * Apply dk to de and everything under de.
365100206Sdd *
366100206Sdd * XXX: This method needs a function call for every nested
367100206Sdd * subdirectory in a devfs mount.  If we plan to have many of these,
368100206Sdd * we might eventually run out of kernel stack space.
369100206Sdd */
370100206Sddstatic void
371100206Sdddevfs_rule_applyde_recursive(struct devfs_krule *dk, struct devfs_dirent *de)
372100206Sdd{
373100206Sdd	struct devfs_dirent *de2;
374100206Sdd
375150147Sphk	TAILQ_FOREACH(de2, &de->de_dlist, de_list)
376100206Sdd		devfs_rule_applyde_recursive(dk, de2);
377150147Sphk	devfs_rule_run(dk, de, devfs_rule_depth);
378100206Sdd}
379100206Sdd
380100206Sdd/*
381100206Sdd * Apply dk to all entires in dm.
382100206Sdd */
383100206Sddstatic void
384100206Sdddevfs_rule_applydm(struct devfs_krule *dk, struct devfs_mount *dm)
385100206Sdd{
386100206Sdd
387149107Sphk	devfs_rule_applyde_recursive(dk, dm->dm_rootdir);
388100206Sdd}
389100206Sdd
390100206Sdd/*
391100206Sdd * Automatically select a number for a new rule in ds, and write the
392100206Sdd * result into rnump.
393100206Sdd */
394100206Sddstatic int
395100206Sdddevfs_rule_autonumber(struct devfs_ruleset *ds, devfs_rnum *rnump)
396100206Sdd{
397100206Sdd	struct devfs_krule *dk;
398100206Sdd
399100206Sdd	/* Find the last rule. */
400100206Sdd	SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
401100206Sdd		if (SLIST_NEXT(dk, dk_list) == NULL)
402100206Sdd			break;
403100206Sdd	}
404100206Sdd	if (dk == NULL)
405100206Sdd		*rnump = 100;
406100206Sdd	else {
407100206Sdd		*rnump = rid2rn(dk->dk_rule.dr_id) + 100;
408100206Sdd		/* Detect overflow. */
409100206Sdd		if (*rnump < rid2rn(dk->dk_rule.dr_id))
410100206Sdd			return (ERANGE);
411100206Sdd	}
412100206Sdd	KASSERT(devfs_rule_byid(mkrid(ds->ds_number, *rnump)) == NULL,
413100206Sdd	    ("autonumbering resulted in an already existing rule"));
414100206Sdd	return (0);
415100206Sdd}
416100206Sdd
417100206Sdd/*
418100206Sdd * Find a krule by id.
419100206Sdd */
420100206Sddstatic struct devfs_krule *
421100206Sdddevfs_rule_byid(devfs_rid rid)
422100206Sdd{
423100206Sdd	struct devfs_ruleset *ds;
424100206Sdd	struct devfs_krule *dk;
425100206Sdd	devfs_rnum rn;
426100206Sdd
427100206Sdd	rn = rid2rn(rid);
428100206Sdd	ds = devfs_ruleset_bynum(rid2rsn(rid));
429100206Sdd	if (ds == NULL)
430100206Sdd		return (NULL);
431100206Sdd	SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
432100206Sdd		if (rid2rn(dk->dk_rule.dr_id) == rn)
433100206Sdd			return (dk);
434100206Sdd		else if (rid2rn(dk->dk_rule.dr_id) > rn)
435100206Sdd			break;
436100206Sdd	}
437100206Sdd	return (NULL);
438100206Sdd}
439100206Sdd
440100206Sdd/*
441100206Sdd * Remove dkp from any lists it may be on and remove memory associated
442100206Sdd * with it.
443100206Sdd */
444100206Sddstatic int
445100206Sdddevfs_rule_delete(struct devfs_krule **dkp)
446100206Sdd{
447100206Sdd	struct devfs_krule *dk = *dkp;
448100206Sdd	struct devfs_ruleset *ds;
449100206Sdd
450100206Sdd	if (dk->dk_rule.dr_iacts & DRA_INCSET) {
451100206Sdd		ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset);
452100206Sdd		KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset"));
453100206Sdd		--ds->ds_refcount;
454100206Sdd		devfs_ruleset_reap(&ds);
455100206Sdd	}
456100206Sdd	SLIST_REMOVE(&dk->dk_ruleset->ds_rules, dk, devfs_krule, dk_list);
457100206Sdd	free(dk, M_DEVFS);
458100206Sdd	*dkp = NULL;
459100206Sdd	return (0);
460100206Sdd}
461100206Sdd
462100206Sdd/*
463130585Sphk * Get a struct cdev *corresponding to de so we can try to match rules based
464130585Sphk * on it.  If this routine returns NULL, there is no struct cdev *associated
465100206Sdd * with the dirent (symlinks and directories don't have dev_ts), and
466100206Sdd * the caller should assume that any critera dependent on a dev_t
467100206Sdd * don't match.
468100206Sdd */
469130585Sphkstatic struct cdev *
470100206Sdddevfs_rule_getdev(struct devfs_dirent *de)
471100206Sdd{
472130585Sphk	struct cdev **devp, *dev;
473100206Sdd
474100206Sdd	devp = devfs_itod(de->de_inode);
475100206Sdd	if (devp != NULL)
476100206Sdd		dev = *devp;
477100206Sdd	else
478100206Sdd		dev = NULL;
479130585Sphk	/* If we think this dirent should have a struct cdev *, alert the user. */
480100206Sdd	if (dev == NULL && de->de_dirent->d_type != DT_LNK &&
481100206Sdd	    de->de_dirent->d_type != DT_DIR)
482130585Sphk		printf("Warning: no struct cdev *for %s\n", de->de_dirent->d_name);
483100206Sdd	return (dev);
484100206Sdd}
485100206Sdd
486100206Sdd/*
487100206Sdd * Do what we need to do to a rule that we just loaded from the
488100206Sdd * userland.  In particular, we need to check the magic, and adjust
489100206Sdd * the ruleset appropriate if desired.
490100206Sdd */
491100206Sddstatic int
492100206Sdddevfs_rule_input(struct devfs_rule *dr, struct devfs_mount *dm)
493100206Sdd{
494100206Sdd
495100206Sdd	if (dr->dr_magic != DEVFS_MAGIC)
496100206Sdd		return (ERPCMISMATCH);
497100206Sdd	dr->dr_id = devfs_rid_input(dr->dr_id, dm);
498100206Sdd	return (0);
499100206Sdd}
500100206Sdd
501100206Sdd/*
502100206Sdd * Import dr into the appropriate place in the kernel (i.e., make a
503100206Sdd * krule).  The value of dr is copied, so the pointer may be destroyed
504100206Sdd * after this call completes.
505100206Sdd */
506100206Sddstatic int
507100206Sdddevfs_rule_insert(struct devfs_rule *dr)
508100206Sdd{
509100206Sdd	struct devfs_ruleset *ds, *dsi;
510100206Sdd	struct devfs_krule *k1, *k2;
511100206Sdd	struct devfs_krule *dk;
512100206Sdd	devfs_rsnum rsnum;
513100206Sdd	devfs_rnum dkrn;
514100206Sdd	int error;
515100206Sdd
516100206Sdd	/*
517100206Sdd	 * This stuff seems out of place here, but we want to do it as
518100206Sdd	 * soon as possible so that if it fails, we don't have to roll
519100206Sdd	 * back any changes we already made (e.g., ruleset creation).
520100206Sdd	 */
521100206Sdd	if (dr->dr_iacts & DRA_INCSET) {
522100206Sdd		dsi = devfs_ruleset_bynum(dr->dr_incset);
523100206Sdd		if (dsi == NULL)
524100206Sdd			return (ESRCH);
525100206Sdd	} else
526100206Sdd		dsi = NULL;
527100206Sdd
528100206Sdd	rsnum = rid2rsn(dr->dr_id);
529100206Sdd	ds = devfs_ruleset_bynum(rsnum);
530100206Sdd	if (ds == NULL)
531100206Sdd		ds = devfs_ruleset_create(rsnum);
532100206Sdd	if (ds->ds_flags & DS_IMMUTABLE)
533100206Sdd		return (EIO);
534100206Sdd	dkrn = rid2rn(dr->dr_id);
535100206Sdd	if (dkrn == 0) {
536100206Sdd		error = devfs_rule_autonumber(ds, &dkrn);
537100206Sdd		if (error != 0)
538100206Sdd			return (error);
539100206Sdd	}
540100206Sdd
541111119Simp	dk = malloc(sizeof(*dk), M_DEVFS, M_WAITOK);
542100206Sdd	dk->dk_ruleset = ds;
543100206Sdd	if (dsi != NULL)
544100206Sdd		++dsi->ds_refcount;
545100206Sdd	/* XXX: Inspect dr? */
546100206Sdd	memcpy(&dk->dk_rule, dr, sizeof(*dr));
547100206Sdd	dk->dk_rule.dr_id = mkrid(rid2rsn(dk->dk_rule.dr_id), dkrn);
548100206Sdd
549100206Sdd	k1 = SLIST_FIRST(&ds->ds_rules);
550100206Sdd	if (k1 == NULL || rid2rn(k1->dk_rule.dr_id) > dkrn)
551100206Sdd		SLIST_INSERT_HEAD(&ds->ds_rules, dk, dk_list);
552100206Sdd	else {
553100206Sdd		SLIST_FOREACH(k1, &ds->ds_rules, dk_list) {
554100206Sdd			k2 = SLIST_NEXT(k1, dk_list);
555100206Sdd			if (k2 == NULL || rid2rn(k2->dk_rule.dr_id) > dkrn) {
556100206Sdd				SLIST_INSERT_AFTER(k1, dk, dk_list);
557100206Sdd				break;
558100206Sdd			}
559100206Sdd		}
560100206Sdd	}
561100206Sdd
562100206Sdd	return (0);
563100206Sdd}
564100206Sdd
565100206Sdd/*
566100206Sdd * Determine whether dk matches de.  Returns 1 if dk should be run on
567100206Sdd * de; 0, otherwise.
568100206Sdd */
569100206Sddstatic int
570100206Sdddevfs_rule_match(struct devfs_krule *dk, struct devfs_dirent *de)
571100206Sdd{
572100206Sdd	struct devfs_rule *dr = &dk->dk_rule;
573130585Sphk	struct cdev *dev;
574100206Sdd
575100206Sdd	dev = devfs_rule_getdev(de);
576100206Sdd	/*
577100206Sdd	 * At this point, if dev is NULL, we should assume that any
578100206Sdd	 * criteria that depend on it don't match.  We should *not*
579100206Sdd	 * just ignore them (i.e., act like they weren't specified),
580100206Sdd	 * since that makes a rule that only has criteria dependent on
581130585Sphk	 * the struct cdev *match all symlinks and directories.
582100206Sdd	 *
583100206Sdd	 * Note also that the following tests are somewhat reversed:
584100206Sdd	 * They're actually testing to see whether the condition does
585100206Sdd	 * *not* match, since the default is to assume the rule should
586100206Sdd	 * be run (such as if there are no conditions).
587135727Sphk	 *
588135727Sphk	 * XXX: lacks threadref on dev
589100206Sdd	 */
590100206Sdd	if (dr->dr_icond & DRC_DSWFLAGS)
591100206Sdd		if (dev == NULL ||
592100206Sdd		    (dev->si_devsw->d_flags & dr->dr_dswflags) == 0)
593150147Sphk			return (0);
594100206Sdd	if (dr->dr_icond & DRC_PATHPTRN)
595100206Sdd		if (!devfs_rule_matchpath(dk, de))
596150147Sphk			return (0);
597100206Sdd
598100206Sdd	return (1);
599100206Sdd}
600100206Sdd
601100206Sdd/*
602100206Sdd * Determine whether dk matches de on account of dr_pathptrn.
603100206Sdd */
604100206Sddstatic int
605100206Sdddevfs_rule_matchpath(struct devfs_krule *dk, struct devfs_dirent *de)
606100206Sdd{
607100206Sdd	struct devfs_rule *dr = &dk->dk_rule;
608100206Sdd	char *pname;
609130585Sphk	struct cdev *dev;
610100206Sdd
611100206Sdd	dev = devfs_rule_getdev(de);
612100206Sdd	if (dev != NULL)
613100206Sdd		pname = dev->si_name;
614124798Scperciva	else if (de->de_dirent->d_type == DT_LNK ||
615124804Scperciva	    de->de_dirent->d_type == DT_DIR)
616109090Sdd		pname = de->de_dirent->d_name;
617100206Sdd	else
618100206Sdd		return (0);
619100206Sdd	KASSERT(pname != NULL, ("devfs_rule_matchpath: NULL pname"));
620100206Sdd
621104653Sdd	return (fnmatch(dr->dr_pathptrn, pname, 0) == 0);
622100206Sdd}
623100206Sdd
624100206Sdd/*
625100206Sdd * Run dk on de.
626100206Sdd */
627100206Sddstatic void
628150147Sphkdevfs_rule_run(struct devfs_krule *dk, struct devfs_dirent *de, unsigned depth)
629100206Sdd{
630100206Sdd	struct devfs_rule *dr = &dk->dk_rule;
631100206Sdd	struct devfs_ruleset *ds;
632100206Sdd
633150147Sphk	if (!devfs_rule_match(dk, de))
634150147Sphk		return;
635100206Sdd	if (dr->dr_iacts & DRA_BACTS) {
636100206Sdd		if (dr->dr_bacts & DRB_HIDE)
637100206Sdd			de->de_flags |= DE_WHITEOUT;
638100206Sdd		if (dr->dr_bacts & DRB_UNHIDE)
639100206Sdd			de->de_flags &= ~DE_WHITEOUT;
640100206Sdd	}
641100206Sdd	if (dr->dr_iacts & DRA_UID)
642100206Sdd		de->de_uid = dr->dr_uid;
643100206Sdd	if (dr->dr_iacts & DRA_GID)
644100206Sdd		de->de_gid = dr->dr_gid;
645100206Sdd	if (dr->dr_iacts & DRA_MODE)
646100206Sdd		de->de_mode = dr->dr_mode;
647100206Sdd	if (dr->dr_iacts & DRA_INCSET) {
648150147Sphk		/*
649150147Sphk		 * XXX: we should tell the user if the depth is exceeded here
650150147Sphk		 * XXX: but it is not obvious how to.  A return value will
651150147Sphk		 * XXX: not work as this is called when devices are created
652150147Sphk		 * XXX: long time after the rules were instantiated.
653150147Sphk		 * XXX: a printf() would probably give too much noise, or
654150147Sphk		 * XXX: DoS the machine.  I guess a a rate-limited message
655150147Sphk		 * XXX: might work.
656150147Sphk		 */
657150147Sphk		if (depth > 0) {
658150147Sphk			ds = devfs_ruleset_bynum(dk->dk_rule.dr_incset);
659150147Sphk			KASSERT(ds != NULL, ("DRA_INCSET but bad dr_incset"));
660150147Sphk			devfs_ruleset_applyde(ds, de, depth - 1);
661150147Sphk		}
662100206Sdd	}
663100206Sdd}
664100206Sdd
665100206Sdd/*
666100206Sdd * Apply all the rules in ds to de.
667100206Sdd */
668100206Sddstatic void
669150147Sphkdevfs_ruleset_applyde(struct devfs_ruleset *ds, struct devfs_dirent *de, unsigned depth)
670100206Sdd{
671100206Sdd	struct devfs_krule *dk;
672100206Sdd
673150147Sphk	SLIST_FOREACH(dk, &ds->ds_rules, dk_list)
674150147Sphk		devfs_rule_run(dk, de, depth);
675100206Sdd}
676100206Sdd
677100206Sdd/*
678100206Sdd * Apply all the rules in ds to all the entires in dm.
679100206Sdd */
680100206Sddstatic void
681100206Sdddevfs_ruleset_applydm(struct devfs_ruleset *ds, struct devfs_mount *dm)
682100206Sdd{
683100206Sdd	struct devfs_krule *dk;
684100206Sdd
685100206Sdd	/*
686100206Sdd	 * XXX: Does it matter whether we do
687100206Sdd	 *
688100206Sdd	 *	foreach(dk in ds)
689100206Sdd	 *		foreach(de in dm)
690100206Sdd	 *			apply(dk to de)
691100206Sdd	 *
692100206Sdd	 * as opposed to
693100206Sdd	 *
694100206Sdd	 *	foreach(de in dm)
695100206Sdd	 *		foreach(dk in ds)
696100206Sdd	 *			apply(dk to de)
697100206Sdd	 *
698100206Sdd	 * The end result is obviously the same, but does the order
699100206Sdd	 * matter?
700100206Sdd	 */
701100206Sdd	SLIST_FOREACH(dk, &ds->ds_rules, dk_list) {
702100206Sdd		devfs_rule_applydm(dk, dm);
703100206Sdd	}
704100206Sdd}
705100206Sdd
706100206Sdd/*
707100206Sdd * Find a ruleset by number.
708100206Sdd */
709100206Sddstatic struct devfs_ruleset *
710100206Sdddevfs_ruleset_bynum(devfs_rsnum rsnum)
711100206Sdd{
712100206Sdd	struct devfs_ruleset *ds;
713100206Sdd
714100206Sdd	SLIST_FOREACH(ds, &devfs_rulesets, ds_list) {
715100206Sdd		if (ds->ds_number == rsnum)
716100206Sdd			return (ds);
717100206Sdd	}
718100206Sdd	return (NULL);
719100206Sdd}
720100206Sdd
721100206Sdd/*
722100206Sdd * Create a new ruleset.
723100206Sdd */
724100206Sddstatic struct devfs_ruleset *
725100206Sdddevfs_ruleset_create(devfs_rsnum rsnum)
726100206Sdd{
727100206Sdd	struct devfs_ruleset *s1, *s2;
728100206Sdd	struct devfs_ruleset *ds;
729100206Sdd
730100206Sdd	KASSERT(devfs_ruleset_bynum(rsnum) == NULL,
731100206Sdd	    ("creating already existent ruleset %d", rsnum));
732100206Sdd
733111119Simp	ds = malloc(sizeof(*ds), M_DEVFS, M_WAITOK | M_ZERO);
734100206Sdd	ds->ds_number = rsnum;
735100206Sdd	ds->ds_refcount = ds->ds_flags = 0;
736100206Sdd	SLIST_INIT(&ds->ds_rules);
737100206Sdd
738100206Sdd	s1 = SLIST_FIRST(&devfs_rulesets);
739100206Sdd	if (s1 == NULL || s1->ds_number > rsnum)
740100206Sdd		SLIST_INSERT_HEAD(&devfs_rulesets, ds, ds_list);
741100206Sdd	else {
742100206Sdd		SLIST_FOREACH(s1, &devfs_rulesets, ds_list) {
743100206Sdd			s2 = SLIST_NEXT(s1, ds_list);
744100206Sdd			if (s2 == NULL || s2->ds_number > rsnum) {
745100206Sdd				SLIST_INSERT_AFTER(s1, ds, ds_list);
746100206Sdd				break;
747100206Sdd			}
748100206Sdd		}
749100206Sdd	}
750100206Sdd
751100206Sdd	return (ds);
752100206Sdd}
753100206Sdd
754100206Sdd/*
755100206Sdd * Remove a ruleset form the system.  The ruleset specified must be
756100206Sdd * empty and not in use.
757100206Sdd */
758100206Sddstatic void
759100206Sdddevfs_ruleset_destroy(struct devfs_ruleset **dsp)
760100206Sdd{
761100206Sdd	struct devfs_ruleset *ds = *dsp;
762100206Sdd
763100206Sdd	KASSERT(SLIST_EMPTY(&ds->ds_rules), ("destroying non-empty ruleset"));
764100206Sdd	KASSERT(ds->ds_refcount == 0, ("destroying busy ruleset"));
765100206Sdd	KASSERT((ds->ds_flags & DS_IMMUTABLE) == 0,
766100206Sdd	    ("destroying immutable ruleset"));
767100206Sdd
768100206Sdd	SLIST_REMOVE(&devfs_rulesets, ds, devfs_ruleset, ds_list);
769100206Sdd	free(ds, M_DEVFS);
770100206Sdd	*dsp = NULL;
771100206Sdd}
772100206Sdd
773100206Sdd/*
774100206Sdd * Remove a ruleset from the system if it's empty and not used
775100206Sdd * anywhere.  This should be called after every time a rule is deleted
776100206Sdd * from this ruleset or the reference count is decremented.
777100206Sdd */
778100206Sddstatic void
779100206Sdddevfs_ruleset_reap(struct devfs_ruleset **dsp)
780100206Sdd{
781100206Sdd	struct devfs_ruleset *ds = *dsp;
782100206Sdd
783100206Sdd	if (SLIST_EMPTY(&ds->ds_rules) && ds->ds_refcount == 0) {
784100206Sdd		devfs_ruleset_destroy(&ds);
785100206Sdd		*dsp = ds;
786100206Sdd	}
787100206Sdd}
788100206Sdd
789100206Sdd/*
790100206Sdd * Make rsnum the active ruleset for dm.
791100206Sdd */
792100206Sddstatic int
793100206Sdddevfs_ruleset_use(devfs_rsnum rsnum, struct devfs_mount *dm)
794100206Sdd{
795100206Sdd	struct devfs_ruleset *cds, *ds;
796100206Sdd
797100206Sdd	ds = devfs_ruleset_bynum(rsnum);
798100206Sdd	if (ds == NULL)
799100206Sdd		ds = devfs_ruleset_create(rsnum);
800100206Sdd	cds = devfs_ruleset_bynum(dm->dm_ruleset);
801100206Sdd	KASSERT(cds != NULL, ("mount-point has NULL ruleset"));
802100206Sdd
803100206Sdd	/* These should probably be made atomic somehow. */
804100206Sdd	--cds->ds_refcount;
805100206Sdd	++ds->ds_refcount;
806100206Sdd	dm->dm_ruleset = rsnum;
807100206Sdd
808100206Sdd	devfs_ruleset_reap(&cds);
809100206Sdd	return (0);
810100206Sdd}
811