diff options
author | phk <phk@FreeBSD.org> | 2005-09-19 19:56:48 +0000 |
---|---|---|
committer | phk <phk@FreeBSD.org> | 2005-09-19 19:56:48 +0000 |
commit | 6a408cbd71bd73e59f73f6e904c7247467a361a1 (patch) | |
tree | 9e64ec73d7bfbd95c61da0bd311167a94851cc3d /sys/kern | |
parent | 92d485220134cae42bad5e6d43661f3d923907df (diff) | |
download | FreeBSD-src-6a408cbd71bd73e59f73f6e904c7247467a361a1.zip FreeBSD-src-6a408cbd71bd73e59f73f6e904c7247467a361a1.tar.gz |
Rewamp DEVFS internals pretty severely [1].
Give DEVFS a proper inode called struct cdev_priv. It is important
to keep in mind that this "inode" is shared between all DEVFS
mountpoints, therefore it is protected by the global device mutex.
Link the cdev_priv's into a list, protected by the global device
mutex. Keep track of each cdev_priv's state with a flag bit and
of references from mountpoints with a dedicated usecount.
Reap the benefits of much improved kernel memory allocator and the
generally better defined device driver APIs to get rid of the tables
of pointers + serial numbers, their overflow tables, the atomics
to muck about in them and all the trouble that resulted in.
This makes RAM the only limit on how many devices we can have.
The cdev_priv is actually a super struct containing the normal cdev
as the "public" part, and therefore allocation and freeing has moved
to devfs_devs.c from kern_conf.c.
The overall responsibility is (to be) split such that kern/kern_conf.c
is the stuff that deals with drivers and struct cdev and fs/devfs
handles filesystems and struct cdev_priv and their private liason
exposed only in devfs_int.h.
Move the inode number from cdev to cdev_priv and allocate inode
numbers properly with unr. Local dirents in the mountpoints
(directories, symlinks) allocate inodes from the same pool to
guarantee against overlaps.
Various other fields are going to migrate from cdev to cdev_priv
in the future in order to hide them. A few fields may migrate
from devfs_dirent to cdev_priv as well.
Protect the DEVFS mountpoint with an sx lock instead of lockmgr,
this lock also protects the directory tree of the mountpoint.
Give each mountpoint a unique integer index, allocated with unr.
Use it into an array of devfs_dirent pointers in each cdev_priv.
Initially the array points to a single element also inside cdev_priv,
but as more devfs instances are mounted, the array is extended with
malloc(9) as necessary when the filesystem populates its directory
tree.
Retire the cdev alias lists, the cdev_priv now know about all the
relevant devfs_dirents (and their vnodes) and devfs_revoke() will
pick them up from there. We still spelunk into other mountpoints
and fondle their data without 100% good locking. It may make better
sense to vector the revoke event into the tty code and there do a
destroy_dev/make_dev on the tty's devices, but that's for further
study.
Lots of shuffling of stuff and churn of bits for no good reason[2].
XXX: There is still nothing preventing the dev_clone EVENTHANDLER
from being invoked at the same time in two devfs mountpoints. It
is not obvious what the best course of action is here.
XXX: comment out an if statement that lost its body, until I can
find out what should go there so it doesn't do damage in the meantime.
XXX: Leave in a few extra malloc types and KASSERTS to help track
down any remaining issues.
Much testing provided by: Kris
Much confusion caused by (races in): md(4)
[1] You are not supposed to understand anything past this point.
[2] This line should simplify life for the peanut gallery.
Diffstat (limited to 'sys/kern')
-rw-r--r-- | sys/kern/kern_conf.c | 66 |
1 files changed, 27 insertions, 39 deletions
diff --git a/sys/kern/kern_conf.c b/sys/kern/kern_conf.c index a6fcf83..4e8e033 100644 --- a/sys/kern/kern_conf.c +++ b/sys/kern/kern_conf.c @@ -33,7 +33,6 @@ __FBSDID("$FreeBSD$"); #include <sys/bio.h> #include <sys/lock.h> #include <sys/mutex.h> -#include <sys/sysctl.h> #include <sys/module.h> #include <sys/malloc.h> #include <sys/conf.h> @@ -49,8 +48,7 @@ __FBSDID("$FreeBSD$"); static MALLOC_DEFINE(M_DEVT, "cdev", "cdev storage"); -static struct mtx devmtx; -static void freedev(struct cdev *dev); +struct mtx devmtx; static void destroy_devl(struct cdev *dev); static struct cdev *make_dev_credv(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid, gid_t gid, int mode, const char *fmt, @@ -99,15 +97,19 @@ dev_rel(struct cdev *dev) dev->si_refcount--; KASSERT(dev->si_refcount >= 0, ("dev_rel(%s) gave negative count", devtoname(dev))); +#if 0 if (dev->si_usecount == 0 && (dev->si_flags & SI_CHEAPCLONE) && (dev->si_flags & SI_NAMED)) - if (dev->si_devsw == NULL && dev->si_refcount == 0) { + ; + else +#endif +if (dev->si_devsw == NULL && dev->si_refcount == 0) { LIST_REMOVE(dev, si_list); flag = 1; } dev_unlock(); if (flag) - freedev(dev); + devfs_free(dev); } struct cdevsw * @@ -389,18 +391,6 @@ unit2minor(int unit) } static struct cdev * -allocdev(void) -{ - struct cdev *si; - - si = malloc(sizeof *si, M_DEVT, M_USE_RESERVE | M_ZERO | M_WAITOK); - si->si_name = si->__si_namebuf; - LIST_INIT(&si->si_children); - LIST_INIT(&si->si_alist); - return (si); -} - -static struct cdev * newdev(struct cdevsw *csw, int y, struct cdev *si) { struct cdev *si2; @@ -410,24 +400,16 @@ newdev(struct cdevsw *csw, int y, struct cdev *si) udev = y; LIST_FOREACH(si2, &csw->d_devs, si_list) { if (si2->si_drv0 == udev) { - freedev(si); + devfs_free(si); return (si2); } } si->si_drv0 = udev; + si->si_devsw = csw; LIST_INSERT_HEAD(&csw->d_devs, si, si_list); return (si); } -static void -freedev(struct cdev *dev) -{ - - if (dev->si_cred != NULL) - crfree(dev->si_cred); - free(dev, M_DEVT); -} - int uminor(dev_t dev) { @@ -538,12 +520,11 @@ make_dev_credv(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid, if (!(devsw->d_flags & D_INIT)) prep_cdevsw(devsw); - dev = allocdev(); + dev = devfs_alloc(); dev_lock(); dev = newdev(devsw, minornr, dev); if (dev->si_flags & SI_CHEAPCLONE && - dev->si_flags & SI_NAMED && - dev->si_devsw == devsw) { + dev->si_flags & SI_NAMED) { /* * This is allowed as it removes races and generally * simplifies cloning devices. @@ -562,7 +543,6 @@ make_dev_credv(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid, dev->__si_namebuf); } - dev->si_devsw = devsw; dev->si_flags |= SI_NAMED; if (cr != NULL) dev->si_cred = crhold(cr); @@ -604,14 +584,22 @@ make_dev_cred(struct cdevsw *devsw, int minornr, struct ucred *cr, uid_t uid, return (dev); } -void -dev_depends(struct cdev *pdev, struct cdev *cdev) +static void +dev_dependsl(struct cdev *pdev, struct cdev *cdev) { - dev_lock(); cdev->si_parent = pdev; cdev->si_flags |= SI_CHILD; LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings); +} + + +void +dev_depends(struct cdev *pdev, struct cdev *cdev) +{ + + dev_lock(); + dev_dependsl(pdev, cdev); dev_unlock(); } @@ -622,7 +610,7 @@ make_dev_alias(struct cdev *pdev, const char *fmt, ...) va_list ap; int i; - dev = allocdev(); + dev = devfs_alloc(); dev_lock(); dev->si_flags |= SI_ALIAS; dev->si_flags |= SI_NAMED; @@ -689,7 +677,7 @@ destroy_devl(struct cdev *dev) /* Remove from cdevsw list */ LIST_REMOVE(dev, si_list); - /* If cdevsw has no struct cdev *'s, clean it */ + /* If cdevsw has no more struct cdev *'s, clean it */ if (LIST_EMPTY(&csw->d_devs)) fini_cdevsw(csw); } @@ -698,7 +686,7 @@ destroy_devl(struct cdev *dev) if (dev->si_refcount > 0) { LIST_INSERT_HEAD(&dead_cdevsw.d_devs, dev, si_list); } else { - freedev(dev); + devfs_free(dev); } } @@ -817,7 +805,7 @@ clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev ** * the end of the list. */ unit = *up; - ndev = allocdev(); + ndev = devfs_alloc(); dev_lock(); low = extra; de = dl = NULL; @@ -828,7 +816,7 @@ clone_create(struct clonedevs **cdp, struct cdevsw *csw, int *up, struct cdev ** u = dev2unit(dev); if (u == (unit | extra)) { *dp = dev; - freedev(ndev); + devfs_free(ndev); dev_unlock(); return (0); } |