2005-01-06 18:10:42 +00:00
|
|
|
/*-
|
2004-06-18 08:08:47 +00:00
|
|
|
* Copyright (c) 2000,2004
|
2000-08-20 21:34:39 +00:00
|
|
|
* Poul-Henning Kamp. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* From: FreeBSD: src/sys/miscfs/kernfs/kernfs_vfsops.c 1.36
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
2000-09-06 11:26:43 +00:00
|
|
|
#include "opt_devfs.h"
|
2002-07-31 15:45:16 +00:00
|
|
|
#include "opt_mac.h"
|
2000-09-06 11:26:43 +00:00
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/conf.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/dirent.h>
|
2000-09-06 11:26:43 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/lock.h>
|
2002-07-31 15:45:16 +00:00
|
|
|
#include <sys/mac.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/vnode.h>
|
2000-09-06 11:26:43 +00:00
|
|
|
|
|
|
|
#include <machine/atomic.h>
|
2000-08-20 21:34:39 +00:00
|
|
|
|
|
|
|
#include <fs/devfs/devfs.h>
|
2005-08-16 19:08:01 +00:00
|
|
|
#include <fs/devfs/devfs_int.h>
|
2000-08-20 21:34:39 +00:00
|
|
|
|
2004-06-16 09:47:26 +00:00
|
|
|
static struct cdev *devfs_inot[NDEVFSINO];
|
|
|
|
static struct cdev **devfs_overflow;
|
2000-09-06 11:26:43 +00:00
|
|
|
static int devfs_ref[NDEVFSINO];
|
|
|
|
static int *devfs_refoverflow;
|
|
|
|
static int devfs_nextino = 3;
|
|
|
|
static int devfs_numino;
|
|
|
|
static int devfs_topino;
|
|
|
|
static int devfs_noverflowwant = NDEVFSOVERFLOW;
|
|
|
|
static int devfs_noverflow;
|
|
|
|
static unsigned devfs_generation;
|
|
|
|
|
2000-12-08 15:07:24 +00:00
|
|
|
static struct devfs_dirent *devfs_find (struct devfs_dirent *dd, const char *name, int namelen);
|
|
|
|
|
2005-02-10 12:23:29 +00:00
|
|
|
static SYSCTL_NODE(_vfs, OID_AUTO, devfs, CTLFLAG_RW, 0, "DEVFS filesystem");
|
2000-09-06 11:26:43 +00:00
|
|
|
SYSCTL_UINT(_vfs_devfs, OID_AUTO, noverflow, CTLFLAG_RW,
|
|
|
|
&devfs_noverflowwant, 0, "Size of DEVFS overflow table");
|
|
|
|
SYSCTL_UINT(_vfs_devfs, OID_AUTO, generation, CTLFLAG_RD,
|
|
|
|
&devfs_generation, 0, "DEVFS generation number");
|
|
|
|
SYSCTL_UINT(_vfs_devfs, OID_AUTO, inodes, CTLFLAG_RD,
|
|
|
|
&devfs_numino, 0, "DEVFS inodes");
|
|
|
|
SYSCTL_UINT(_vfs_devfs, OID_AUTO, topinode, CTLFLAG_RD,
|
|
|
|
&devfs_topino, 0, "DEVFS highest inode#");
|
|
|
|
|
2005-09-15 06:57:28 +00:00
|
|
|
unsigned devfs_rule_depth = 1;
|
|
|
|
SYSCTL_UINT(_vfs_devfs, OID_AUTO, rule_depth, CTLFLAG_RW,
|
|
|
|
&devfs_rule_depth, 0, "Max depth of ruleset include");
|
|
|
|
|
2005-08-16 19:25:02 +00:00
|
|
|
/*
|
|
|
|
* Helper sysctl for devname(3). We're given a struct cdev * and return
|
|
|
|
* the name, if any, registered by the device driver.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sysctl_devname(SYSCTL_HANDLER_ARGS)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
dev_t ud;
|
|
|
|
struct cdev *dev, **dp;
|
|
|
|
|
|
|
|
error = SYSCTL_IN(req, &ud, sizeof (ud));
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if (ud == NODEV)
|
|
|
|
return(EINVAL);
|
|
|
|
dp = devfs_itod(ud);
|
|
|
|
if (dp == NULL)
|
|
|
|
return(ENOENT);
|
|
|
|
dev = *dp;
|
|
|
|
if (dev == NULL)
|
|
|
|
return(ENOENT);
|
|
|
|
return(SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1));
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
|
|
|
|
NULL, 0, sysctl_devname, "", "devname(3) handler");
|
|
|
|
|
|
|
|
SYSCTL_INT(_debug_sizeof, OID_AUTO, cdev, CTLFLAG_RD,
|
|
|
|
0, sizeof(struct cdev), "sizeof(struct cdev)");
|
|
|
|
|
2000-09-06 11:26:43 +00:00
|
|
|
static int *
|
|
|
|
devfs_itor(int inode)
|
|
|
|
{
|
|
|
|
if (inode < NDEVFSINO)
|
|
|
|
return (&devfs_ref[inode]);
|
|
|
|
else if (inode < NDEVFSINO + devfs_noverflow)
|
|
|
|
return (&devfs_refoverflow[inode - NDEVFSINO]);
|
|
|
|
else
|
|
|
|
panic ("YRK!");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
devfs_dropref(int inode)
|
|
|
|
{
|
|
|
|
int *ip;
|
|
|
|
|
|
|
|
ip = devfs_itor(inode);
|
|
|
|
atomic_add_int(ip, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
devfs_getref(int inode)
|
|
|
|
{
|
|
|
|
int *ip, i, j;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev **dp;
|
2000-09-06 11:26:43 +00:00
|
|
|
|
|
|
|
ip = devfs_itor(inode);
|
|
|
|
dp = devfs_itod(inode);
|
|
|
|
for (;;) {
|
|
|
|
i = *ip;
|
|
|
|
j = i + 1;
|
|
|
|
if (!atomic_cmpset_int(ip, i, j))
|
|
|
|
continue;
|
|
|
|
if (*dp != NULL)
|
|
|
|
return (1);
|
|
|
|
atomic_add_int(ip, -1);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct devfs_dirent **
|
|
|
|
devfs_itode (struct devfs_mount *dm, int inode)
|
|
|
|
{
|
|
|
|
|
2005-03-10 18:21:34 +00:00
|
|
|
if (inode < 0)
|
|
|
|
return (NULL);
|
2000-09-06 11:26:43 +00:00
|
|
|
if (inode < NDEVFSINO)
|
|
|
|
return (&dm->dm_dirent[inode]);
|
|
|
|
if (devfs_overflow == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (inode < NDEVFSINO + devfs_noverflow)
|
|
|
|
return (&dm->dm_overflow[inode - NDEVFSINO]);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev **
|
2000-09-06 11:26:43 +00:00
|
|
|
devfs_itod (int inode)
|
|
|
|
{
|
|
|
|
|
2005-03-10 18:21:34 +00:00
|
|
|
if (inode < 0)
|
|
|
|
return (NULL);
|
2000-09-06 11:26:43 +00:00
|
|
|
if (inode < NDEVFSINO)
|
|
|
|
return (&devfs_inot[inode]);
|
|
|
|
if (devfs_overflow == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (inode < NDEVFSINO + devfs_noverflow)
|
|
|
|
return (&devfs_overflow[inode - NDEVFSINO]);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2000-12-08 15:07:24 +00:00
|
|
|
static struct devfs_dirent *
|
2000-08-27 14:46:36 +00:00
|
|
|
devfs_find(struct devfs_dirent *dd, const char *name, int namelen)
|
|
|
|
{
|
|
|
|
struct devfs_dirent *de;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(de, &dd->de_dlist, de_list) {
|
|
|
|
if (namelen != de->de_dirent->d_namlen)
|
|
|
|
continue;
|
|
|
|
if (bcmp(name, de->de_dirent->d_name, namelen) != 0)
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (de);
|
|
|
|
}
|
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
struct devfs_dirent *
|
|
|
|
devfs_newdirent(char *name, int namelen)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct devfs_dirent *de;
|
|
|
|
struct dirent d;
|
|
|
|
|
|
|
|
d.d_namlen = namelen;
|
|
|
|
i = sizeof (*de) + GENERIC_DIRSIZ(&d);
|
2005-09-15 10:28:19 +00:00
|
|
|
de = malloc(i, M_DEVFS, M_WAITOK | M_ZERO);
|
2000-08-20 21:34:39 +00:00
|
|
|
de->de_dirent = (struct dirent *)(de + 1);
|
|
|
|
de->de_dirent->d_namlen = namelen;
|
|
|
|
de->de_dirent->d_reclen = GENERIC_DIRSIZ(&d);
|
2001-01-30 08:39:52 +00:00
|
|
|
bcopy(name, de->de_dirent->d_name, namelen);
|
|
|
|
de->de_dirent->d_name[namelen] = '\0';
|
2001-11-03 16:53:24 +00:00
|
|
|
vfs_timestamp(&de->de_ctime);
|
2000-08-24 15:36:55 +00:00
|
|
|
de->de_mtime = de->de_atime = de->de_ctime;
|
|
|
|
de->de_links = 1;
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
|
|
|
mac_init_devfsdirent(de);
|
|
|
|
#endif
|
2000-08-20 21:34:39 +00:00
|
|
|
return (de);
|
|
|
|
}
|
|
|
|
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_dirent *
|
|
|
|
devfs_vmkdir(char *name, int namelen, struct devfs_dirent *dotdot)
|
2000-08-20 21:34:39 +00:00
|
|
|
{
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_dirent *dd;
|
2000-08-20 21:34:39 +00:00
|
|
|
struct devfs_dirent *de;
|
|
|
|
|
2000-08-24 15:36:55 +00:00
|
|
|
dd = devfs_newdirent(name, namelen);
|
|
|
|
|
|
|
|
TAILQ_INIT(&dd->de_dlist);
|
|
|
|
|
|
|
|
dd->de_dirent->d_type = DT_DIR;
|
2001-11-03 16:53:24 +00:00
|
|
|
dd->de_mode = 0555;
|
2000-08-24 15:36:55 +00:00
|
|
|
dd->de_links = 2;
|
|
|
|
dd->de_dir = dd;
|
2000-08-20 21:34:39 +00:00
|
|
|
|
|
|
|
de = devfs_newdirent(".", 1);
|
|
|
|
de->de_dirent->d_type = DT_DIR;
|
2000-08-24 15:36:55 +00:00
|
|
|
de->de_dir = dd;
|
|
|
|
de->de_flags |= DE_DOT;
|
|
|
|
TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
|
|
|
|
|
|
|
|
de = devfs_newdirent("..", 2);
|
|
|
|
de->de_dirent->d_type = DT_DIR;
|
|
|
|
if (dotdot == NULL)
|
|
|
|
de->de_dir = dd;
|
|
|
|
else
|
|
|
|
de->de_dir = dotdot;
|
|
|
|
de->de_flags |= DE_DOTDOT;
|
|
|
|
TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
|
|
|
|
|
2000-08-20 21:34:39 +00:00
|
|
|
return (dd);
|
|
|
|
}
|
|
|
|
|
2000-08-24 15:36:55 +00:00
|
|
|
static void
|
|
|
|
devfs_delete(struct devfs_dirent *dd, struct devfs_dirent *de)
|
2000-08-20 21:34:39 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (de->de_symlink) {
|
2005-09-15 10:28:19 +00:00
|
|
|
free(de->de_symlink, M_DEVFS);
|
2000-08-20 21:34:39 +00:00
|
|
|
de->de_symlink = NULL;
|
|
|
|
}
|
2000-10-09 14:18:07 +00:00
|
|
|
if (de->de_vnode)
|
2000-08-20 21:34:39 +00:00
|
|
|
de->de_vnode->v_data = NULL;
|
2000-08-24 15:36:55 +00:00
|
|
|
TAILQ_REMOVE(&dd->de_dlist, de, de_list);
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
|
|
|
mac_destroy_devfsdirent(de);
|
|
|
|
#endif
|
2005-09-15 10:28:19 +00:00
|
|
|
free(de, M_DEVFS);
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-08-24 15:36:55 +00:00
|
|
|
devfs_purge(struct devfs_dirent *dd)
|
2000-08-20 21:34:39 +00:00
|
|
|
{
|
|
|
|
struct devfs_dirent *de;
|
|
|
|
|
|
|
|
for (;;) {
|
2000-08-24 15:36:55 +00:00
|
|
|
de = TAILQ_FIRST(&dd->de_dlist);
|
2000-08-20 21:34:39 +00:00
|
|
|
if (de == NULL)
|
|
|
|
break;
|
|
|
|
devfs_delete(dd, de);
|
|
|
|
}
|
|
|
|
FREE(dd, M_DEVFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-15 10:28:19 +00:00
|
|
|
void
|
2000-08-20 21:34:39 +00:00
|
|
|
devfs_populate(struct devfs_mount *dm)
|
|
|
|
{
|
|
|
|
int i, j;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev *dev, *pdev;
|
2000-08-24 15:36:55 +00:00
|
|
|
struct devfs_dirent *dd;
|
2000-09-06 11:26:43 +00:00
|
|
|
struct devfs_dirent *de, **dep;
|
2000-08-20 21:34:39 +00:00
|
|
|
char *q, *s;
|
|
|
|
|
2000-10-09 14:18:07 +00:00
|
|
|
if (dm->dm_generation == devfs_generation)
|
2005-09-15 10:28:19 +00:00
|
|
|
return;
|
2000-09-06 11:26:43 +00:00
|
|
|
if (devfs_noverflow && dm->dm_overflow == NULL) {
|
|
|
|
i = devfs_noverflow * sizeof (struct devfs_dirent *);
|
|
|
|
MALLOC(dm->dm_overflow, struct devfs_dirent **, i,
|
2003-02-19 05:47:46 +00:00
|
|
|
M_DEVFS, M_WAITOK | M_ZERO);
|
2000-09-06 11:26:43 +00:00
|
|
|
}
|
2000-08-20 21:34:39 +00:00
|
|
|
while (dm->dm_generation != devfs_generation) {
|
|
|
|
dm->dm_generation = devfs_generation;
|
2000-09-06 11:26:43 +00:00
|
|
|
for (i = 0; i <= devfs_topino; i++) {
|
|
|
|
dev = *devfs_itod(i);
|
|
|
|
dep = devfs_itode(dm, i);
|
|
|
|
de = *dep;
|
2000-08-24 15:36:55 +00:00
|
|
|
if (dev == NULL && de == DE_DELETED) {
|
2000-09-06 11:26:43 +00:00
|
|
|
*dep = NULL;
|
2000-08-24 15:36:55 +00:00
|
|
|
continue;
|
|
|
|
}
|
2000-08-20 21:34:39 +00:00
|
|
|
if (dev == NULL && de != NULL) {
|
|
|
|
dd = de->de_dir;
|
2000-09-06 11:26:43 +00:00
|
|
|
*dep = NULL;
|
2005-03-10 18:21:34 +00:00
|
|
|
devfs_delete(dd, de);
|
2000-09-06 11:26:43 +00:00
|
|
|
devfs_dropref(i);
|
2000-08-20 21:34:39 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (dev == NULL)
|
|
|
|
continue;
|
|
|
|
if (de != NULL)
|
|
|
|
continue;
|
2000-09-06 11:26:43 +00:00
|
|
|
if (!devfs_getref(i))
|
|
|
|
continue;
|
2005-08-15 19:40:53 +00:00
|
|
|
dd = dm->dm_rootdir;
|
2000-08-20 21:34:39 +00:00
|
|
|
s = dev->si_name;
|
2000-08-27 14:46:36 +00:00
|
|
|
for (;;) {
|
|
|
|
for (q = s; *q != '/' && *q != '\0'; q++)
|
|
|
|
continue;
|
2000-10-09 14:18:07 +00:00
|
|
|
if (*q != '/')
|
2000-08-27 14:46:36 +00:00
|
|
|
break;
|
|
|
|
de = devfs_find(dd, s, q - s);
|
|
|
|
if (de == NULL) {
|
|
|
|
de = devfs_vmkdir(s, q - s, dd);
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
2002-12-09 03:44:28 +00:00
|
|
|
mac_create_devfs_directory(
|
|
|
|
dm->dm_mount, s, q - s, de);
|
2002-07-31 15:45:16 +00:00
|
|
|
#endif
|
2000-08-27 14:46:36 +00:00
|
|
|
de->de_inode = dm->dm_inode++;
|
|
|
|
TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
|
|
|
|
dd->de_links++;
|
2000-08-24 15:36:55 +00:00
|
|
|
}
|
|
|
|
s = q + 1;
|
|
|
|
dd = de;
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|
|
|
|
de = devfs_newdirent(s, q - s);
|
|
|
|
if (dev->si_flags & SI_ALIAS) {
|
|
|
|
de->de_inode = dm->dm_inode++;
|
|
|
|
de->de_uid = 0;
|
|
|
|
de->de_gid = 0;
|
2001-11-03 16:53:24 +00:00
|
|
|
de->de_mode = 0755;
|
2000-08-20 21:34:39 +00:00
|
|
|
de->de_dirent->d_type = DT_LNK;
|
2001-05-26 08:27:58 +00:00
|
|
|
pdev = dev->si_parent;
|
2000-08-20 21:34:39 +00:00
|
|
|
j = strlen(pdev->si_name) + 1;
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(de->de_symlink, char *, j, M_DEVFS, M_WAITOK);
|
2000-08-20 21:34:39 +00:00
|
|
|
bcopy(pdev->si_name, de->de_symlink, j);
|
|
|
|
} else {
|
|
|
|
de->de_inode = i;
|
2005-03-31 10:29:57 +00:00
|
|
|
de->de_uid = dev->si_uid;
|
|
|
|
de->de_gid = dev->si_gid;
|
|
|
|
de->de_mode = dev->si_mode;
|
2000-08-20 21:34:39 +00:00
|
|
|
de->de_dirent->d_type = DT_CHR;
|
|
|
|
}
|
2002-07-31 15:45:16 +00:00
|
|
|
#ifdef MAC
|
When devfs cloning takes place, provide access to the credential of the
process that caused the clone event to take place for the device driver
creating the device. This allows cloned device drivers to adapt the
device node based on security aspects of the process, such as the uid,
gid, and MAC label.
- Add a cred reference to struct cdev, so that when a device node is
instantiated as a vnode, the cloning credential can be exposed to
MAC.
- Add make_dev_cred(), a version of make_dev() that additionally
accepts the credential to stick in the struct cdev. Implement it and
make_dev() in terms of a back-end make_dev_credv().
- Add a new event handler, dev_clone_cred, which can be registered to
receive the credential instead of dev_clone, if desired.
- Modify the MAC entry point mac_create_devfs_device() to accept an
optional credential pointer (may be NULL), so that MAC policies can
inspect and act on the label or other elements of the credential
when initializing the skeleton device protections.
- Modify tty_pty.c to register clone_dev_cred and invoke make_dev_cred(),
so that the pty clone credential is exposed to the MAC Framework.
While currently primarily focussed on MAC policies, this change is also
a prerequisite for changes to allow ptys to be instantiated with the UID
of the process looking up the pty. This requires further changes to the
pty driver -- in particular, to immediately recycle pty nodes on last
close so that the credential-related state can be recreated on next
lookup.
Submitted by: Andrew Reisse <andrew.reisse@sparta.com>
Obtained from: TrustedBSD Project
Sponsored by: SPAWAR, SPARTA
MFC after: 1 week
MFC note: Merge to 6.x, but not 5.x for ABI reasons
2005-07-14 10:22:09 +00:00
|
|
|
mac_create_devfs_device(dev->si_cred, dm->dm_mount,
|
|
|
|
dev, de);
|
2002-07-31 15:45:16 +00:00
|
|
|
#endif
|
2000-09-06 11:26:43 +00:00
|
|
|
*dep = de;
|
2000-08-27 14:46:36 +00:00
|
|
|
de->de_dir = dd;
|
2002-07-17 01:46:48 +00:00
|
|
|
devfs_rules_apply(dm, de);
|
2000-08-24 15:36:55 +00:00
|
|
|
TAILQ_INSERT_TAIL(&dd->de_dlist, de, de_list);
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-18 08:08:47 +00:00
|
|
|
/*
|
|
|
|
* devfs_create() and devfs_destroy() are called from kern_conf.c and
|
|
|
|
* in both cases the devlock() mutex is held, so no further locking
|
|
|
|
* is necesary and no sleeping allowed.
|
|
|
|
*/
|
|
|
|
|
2003-03-02 13:35:30 +00:00
|
|
|
void
|
2004-06-16 09:47:26 +00:00
|
|
|
devfs_create(struct cdev *dev)
|
2000-08-20 21:34:39 +00:00
|
|
|
{
|
2000-09-06 11:26:43 +00:00
|
|
|
int ino, i, *ip;
|
2004-06-16 09:47:26 +00:00
|
|
|
struct cdev **dp;
|
2004-06-18 08:08:47 +00:00
|
|
|
struct cdev **ot;
|
|
|
|
int *or;
|
|
|
|
int n;
|
2000-09-06 11:26:43 +00:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
/* Grab the next inode number */
|
|
|
|
ino = devfs_nextino;
|
|
|
|
i = ino + 1;
|
|
|
|
/* wrap around when we reach the end */
|
|
|
|
if (i >= NDEVFSINO + devfs_noverflow)
|
|
|
|
i = 3;
|
2004-06-18 08:08:47 +00:00
|
|
|
devfs_nextino = i;
|
2000-09-06 11:26:43 +00:00
|
|
|
|
|
|
|
/* see if it was occupied */
|
|
|
|
dp = devfs_itod(ino);
|
2004-06-18 08:08:47 +00:00
|
|
|
KASSERT(dp != NULL, ("DEVFS: No devptr inode %d", ino));
|
2000-09-06 11:26:43 +00:00
|
|
|
if (*dp != NULL)
|
|
|
|
continue;
|
|
|
|
ip = devfs_itor(ino);
|
2004-06-18 08:08:47 +00:00
|
|
|
KASSERT(ip != NULL, ("DEVFS: No iptr inode %d", ino));
|
2000-09-06 11:26:43 +00:00
|
|
|
if (*ip != 0)
|
|
|
|
continue;
|
|
|
|
break;
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|
2000-09-06 11:26:43 +00:00
|
|
|
|
2004-06-18 08:08:47 +00:00
|
|
|
*dp = dev;
|
|
|
|
dev->si_inode = ino;
|
|
|
|
if (i > devfs_topino)
|
|
|
|
devfs_topino = i;
|
|
|
|
|
|
|
|
devfs_numino++;
|
|
|
|
devfs_generation++;
|
|
|
|
|
|
|
|
if (devfs_overflow != NULL || devfs_numino + 100 < NDEVFSINO)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to allocate overflow table
|
|
|
|
* XXX: we can probably be less panicy these days and a linked
|
|
|
|
* XXX: list of PAGESIZE/PTRSIZE entries might be a better idea.
|
|
|
|
*
|
|
|
|
* XXX: we may be into witness unlove here.
|
|
|
|
*/
|
|
|
|
n = devfs_noverflowwant;
|
|
|
|
ot = malloc(sizeof(*ot) * n, M_DEVFS, M_NOWAIT | M_ZERO);
|
|
|
|
if (ot == NULL)
|
|
|
|
return;
|
|
|
|
or = malloc(sizeof(*or) * n, M_DEVFS, M_NOWAIT | M_ZERO);
|
|
|
|
if (or == NULL) {
|
|
|
|
free(ot, M_DEVFS);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
devfs_overflow = ot;
|
|
|
|
devfs_refoverflow = or;
|
|
|
|
devfs_noverflow = n;
|
|
|
|
printf("DEVFS Overflow table with %d entries allocated\n", n);
|
|
|
|
return;
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|
|
|
|
|
2003-03-02 13:35:30 +00:00
|
|
|
void
|
2004-06-16 09:47:26 +00:00
|
|
|
devfs_destroy(struct cdev *dev)
|
2000-08-20 21:34:39 +00:00
|
|
|
{
|
2004-06-18 08:08:47 +00:00
|
|
|
int ino;
|
|
|
|
struct cdev **dp;
|
2000-09-06 11:26:43 +00:00
|
|
|
|
|
|
|
ino = dev->si_inode;
|
|
|
|
dev->si_inode = 0;
|
|
|
|
if (ino == 0)
|
|
|
|
return;
|
2004-06-18 08:08:47 +00:00
|
|
|
dp = devfs_itod(ino);
|
|
|
|
KASSERT(*dp == dev,
|
|
|
|
("DEVFS: destroying wrong cdev ino %d", ino));
|
|
|
|
*dp = NULL;
|
|
|
|
devfs_numino--;
|
|
|
|
devfs_generation++;
|
|
|
|
if (ino < devfs_nextino)
|
|
|
|
devfs_nextino = ino;
|
2000-08-20 21:34:39 +00:00
|
|
|
}
|