From 9e4ecfb022a58a6da39774313d1d0d58cacee8d7 Mon Sep 17 00:00:00 2001 From: pgoyette Date: Mon, 25 Jul 2016 12:45:13 +0000 Subject: [PATCH] When initializing the rump cgd component, use the correct driver name (as found in the devsw_conv[] table). This will get us the "official" major numbers for the cgd device. After creating the rump file-space nodes for /dev/cgd* we then need to detach the [bc]devsw's because normal module initialization will do its own attachment, and we don't want that to fail. While here, since we're doing the devsw_attach() twice, share the results from the first call rather than starting from scratch. --- sys/dev/cgd.c | 61 ++++++++++++++++++------- sys/rump/dev/lib/libcgd/cgd_component.c | 17 +++---- 2 files changed, 53 insertions(+), 25 deletions(-) diff --git a/sys/dev/cgd.c b/sys/dev/cgd.c index 0a6f2116eb89..d662fb22b1e2 100644 --- a/sys/dev/cgd.c +++ b/sys/dev/cgd.c @@ -1,4 +1,4 @@ -/* $NetBSD: cgd.c,v 1.108 2016/07/10 17:40:23 riastradh Exp $ */ +/* $NetBSD: cgd.c,v 1.109 2016/07/25 12:45:13 pgoyette Exp $ */ /*- * Copyright (c) 2002 The NetBSD Foundation, Inc. @@ -30,7 +30,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.108 2016/07/10 17:40:23 riastradh Exp $"); +__KERNEL_RCSID(0, "$NetBSD: cgd.c,v 1.109 2016/07/25 12:45:13 pgoyette Exp $"); #include #include @@ -1028,6 +1028,8 @@ MODULE(MODULE_CLASS_DRIVER, cgd, "dk_subr"); #ifdef _MODULE CFDRIVER_DECL(cgd, DV_DISK, NULL); + +devmajor_t cgd_bmajor = -1, cgd_cmajor = -1; #endif static int @@ -1035,10 +1037,6 @@ cgd_modcmd(modcmd_t cmd, void *arg) { int error = 0; -#ifdef _MODULE - devmajor_t bmajor = -1, cmajor = -1; -#endif - switch (cmd) { case MODULE_CMD_INIT: #ifdef _MODULE @@ -1049,16 +1047,24 @@ cgd_modcmd(modcmd_t cmd, void *arg) error = config_cfattach_attach(cgd_cd.cd_name, &cgd_ca); if (error) { config_cfdriver_detach(&cgd_cd); - aprint_error("%s: unable to register cfattach\n", - cgd_cd.cd_name); + aprint_error("%s: unable to register cfattach for" + "%s, error %d\n", __func__, cgd_cd.cd_name, error); break; } + /* + * Attach the {b,c}devsw's + */ + error = devsw_attach("cgd", &cgd_bdevsw, &cgd_bmajor, + &cgd_cdevsw, &cgd_cmajor); - error = devsw_attach("cgd", &cgd_bdevsw, &bmajor, - &cgd_cdevsw, &cmajor); + /* + * If devsw_attach fails, remove from autoconf database + */ if (error) { config_cfattach_detach(cgd_cd.cd_name, &cgd_ca); config_cfdriver_detach(&cgd_cd); + aprint_error("%s: unable to attach %s devsw, " + "error %d", __func__, cgd_cd.cd_name, error); break; } #endif @@ -1066,19 +1072,40 @@ cgd_modcmd(modcmd_t cmd, void *arg) case MODULE_CMD_FINI: #ifdef _MODULE - error = config_cfattach_detach(cgd_cd.cd_name, &cgd_ca); - if (error) - break; - config_cfdriver_detach(&cgd_cd); + /* + * Remove {b,c}devsw's + */ devsw_detach(&cgd_bdevsw, &cgd_cdevsw); + + /* + * Now remove device from autoconf database + */ + error = config_cfattach_detach(cgd_cd.cd_name, &cgd_ca); + if (error) { + error = devsw_attach("cgd", &cgd_bdevsw, &cgd_bmajor, + &cgd_cdevsw, &cgd_cmajor); + aprint_error("%s: failed to detach %s cfattach, " + "error %d\n", __func__, cgd_cd.cd_name, error); + break; + } + error = config_cfdriver_detach(&cgd_cd); + if (error) { + config_cfattach_attach(cgd_cd.cd_name, &cgd_ca); + devsw_attach("cgd", &cgd_bdevsw, &cgd_bmajor, + &cgd_cdevsw, &cgd_cmajor); + aprint_error("%s: failed to detach %s cfdriver, " + "error %d\n", __func__, cgd_cd.cd_name, error); + break; + } #endif break; case MODULE_CMD_STAT: - return ENOTTY; - + error = ENOTTY; + break; default: - return ENOTTY; + error = ENOTTY; + break; } return error; diff --git a/sys/rump/dev/lib/libcgd/cgd_component.c b/sys/rump/dev/lib/libcgd/cgd_component.c index 232e6508bbd9..3e19a8be43bc 100644 --- a/sys/rump/dev/lib/libcgd/cgd_component.c +++ b/sys/rump/dev/lib/libcgd/cgd_component.c @@ -1,4 +1,4 @@ -/* $NetBSD: cgd_component.c,v 1.2 2016/01/26 23:12:15 pooka Exp $ */ +/* $NetBSD: cgd_component.c,v 1.3 2016/07/25 12:45:13 pgoyette Exp $ */ /* * Copyright (c) 2009 Antti Kantee. All Rights Reserved. @@ -26,7 +26,7 @@ */ #include -__KERNEL_RCSID(0, "$NetBSD: cgd_component.c,v 1.2 2016/01/26 23:12:15 pooka Exp $"); +__KERNEL_RCSID(0, "$NetBSD: cgd_component.c,v 1.3 2016/07/25 12:45:13 pgoyette Exp $"); #include #include @@ -40,20 +40,21 @@ RUMP_COMPONENT(RUMP_COMPONENT_DEV) { extern const struct bdevsw cgd_bdevsw; extern const struct cdevsw cgd_cdevsw; - devmajor_t bmaj, cmaj; + extern devmajor_t cgd_bmajor, cgd_cmajor; int error; /* go, mydevfs */ - bmaj = cmaj = -1; - if ((error = devsw_attach("/dev/cgd0", &cgd_bdevsw, &bmaj, - &cgd_cdevsw, &cmaj)) != 0) + if ((error = devsw_attach("cgd", &cgd_bdevsw, &cgd_bmajor, + &cgd_cdevsw, &cgd_cmajor)) != 0) panic("cannot attach cgd: %d", error); if ((error = rump_vfs_makedevnodes(S_IFBLK, "/dev/cgd0", 'a', - bmaj, 0, 7)) != 0) + cgd_bmajor, 0, 7)) != 0) panic("cannot create cooked cgd dev nodes: %d", error); if ((error = rump_vfs_makedevnodes(S_IFCHR, "/dev/rcgd0", 'a', - cmaj, 0, 7)) != 0) + cgd_cmajor, 0, 7)) != 0) panic("cannot create raw cgd dev nodes: %d", error); + + devsw_detach(&cgd_bdevsw, &cgd_cdevsw); }