From edd32c2da270644caf802098bd2aaa5344dc5fe0 Mon Sep 17 00:00:00 2001 From: John Baldwin Date: Tue, 13 Jun 2006 21:36:23 +0000 Subject: [PATCH] Use kern_kldload() and kern_kldunload() to load and unload modules when we intend for the user to be able to unload them later via kldunload(2) instead of calling linker_load_module() and then directly adjusting the ref count on the linker file structure. This makes the resulting consumer code simpler and cleaner and better hides the linker internals making it possible to sanely lock the linker. --- sys/kern/vfs_init.c | 12 +++++------- sys/net80211/ieee80211_freebsd.c | 9 ++------- sys/netgraph/ng_socket.c | 11 +++++------ 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/sys/kern/vfs_init.c b/sys/kern/vfs_init.c index b890ffe1ba8..b279e8959d9 100644 --- a/sys/kern/vfs_init.c +++ b/sys/kern/vfs_init.c @@ -43,6 +43,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -108,7 +109,7 @@ struct vfsconf * vfs_byname_kld(const char *fstype, struct thread *td, int *error) { struct vfsconf *vfsp; - linker_file_t lf; + int fileid; vfsp = vfs_byname(fstype); if (vfsp != NULL) @@ -121,17 +122,14 @@ vfs_byname_kld(const char *fstype, struct thread *td, int *error) *error = securelevel_gt(td->td_ucred, 0); if (*error) return (NULL); - *error = linker_load_module(NULL, fstype, NULL, NULL, &lf); - if (lf == NULL) - *error = ENODEV; + *error = kern_kldload(td, fstype, &fileid); if (*error) return (NULL); - lf->userrefs++; + /* Look up again to see if the VFS was loaded. */ vfsp = vfs_byname(fstype); if (vfsp == NULL) { - lf->userrefs--; - linker_file_unload(lf, LINKER_UNLOAD_FORCE); + (void)kern_kldunload(td, fileid, LINKER_UNLOAD_FORCE); *error = ENODEV; return (NULL); } diff --git a/sys/net80211/ieee80211_freebsd.c b/sys/net80211/ieee80211_freebsd.c index 4fa020733d3..912f2d0b17c 100644 --- a/sys/net80211/ieee80211_freebsd.c +++ b/sys/net80211/ieee80211_freebsd.c @@ -310,14 +310,9 @@ ieee80211_notify_michael_failure(struct ieee80211com *ic, void ieee80211_load_module(const char *modname) { -#ifdef notyet - struct thread *td = curthread; - if (suser(td) == 0 && securelevel_gt(td->td_ucred, 0) == 0) { - mtx_lock(&Giant); - (void) linker_load_module(modname, NULL, NULL, NULL, NULL); - mtx_unlock(&Giant); - } +#ifdef notyet + (void)kern_kldload(curthread, modname, NULL); #else printf("%s: load the %s module by hand for now.\n", __func__, modname); #endif diff --git a/sys/netgraph/ng_socket.c b/sys/netgraph/ng_socket.c index c977c435b53..72bff98f6da 100644 --- a/sys/netgraph/ng_socket.c +++ b/sys/netgraph/ng_socket.c @@ -65,6 +65,7 @@ #include #include #include +#include #include #include #ifdef NOTYET @@ -271,24 +272,22 @@ ngc_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, if ((type = ng_findtype(mkp->type)) == NULL) { char filename[NG_TYPESIZ + 3]; - linker_file_t lf; + int fileid; /* Not found, try to load it as a loadable module. */ snprintf(filename, sizeof(filename), "ng_%s", mkp->type); - mtx_lock(&Giant); - error = linker_load_module(NULL, filename, NULL, NULL, - &lf); - mtx_unlock(&Giant); + error = kern_kldload(curthread, filename, &fileid); if (error != 0) { FREE(msg, M_NETGRAPH_MSG); goto release; } - lf->userrefs++; /* See if type has been loaded successfully. */ if ((type = ng_findtype(mkp->type)) == NULL) { FREE(msg, M_NETGRAPH_MSG); + (void)kern_kldunload(curthread, fileid, + LINKER_UNLOAD_NORMAL); error = ENXIO; goto release; }