dm - Modularize it fully
authorAlex Hornung <ahornung@gmail.com>
Wed, 1 Dec 2010 10:17:55 +0000 (10:17 +0000)
committerAlex Hornung <ahornung@gmail.com>
Wed, 1 Dec 2010 11:40:34 +0000 (11:40 +0000)
* Move each of the targets (except error and zero) into a subdirectory
  in targets/.

* Change the macro DM_TARGET_MODULE to also let the module depend on dm.

* Add a new macro DM_TARGET_BUILTIN, which must be used instead of
  DM_TARGET_MODULE for targets that are compiled into the main dm.ko
  instead of a separate module. Not doing this will prevent dm.ko from
  unloading.

* Enable automatic loading of target modules if they aren't builtin or
  loaded yet. Automatically loaded modules can be unloaded via kldunload
  safely via a little hack (see source).

* NOTE: automatic loading doesn't always work. /boot must be mounted
  already, so if you are planning on encrypting your swap or similar
  stuff, you need to *EXPLICITLY* load dm_target_crypt.ko via the
  loader.

* NOTE: PLEASE NOTE THE NOTE ABOVE!

* Fix a little issue with dm_target_crypt that would print "unloaded"
  even if it wouldn't unload.

* Change the name of dm_target_stripe to dm_target_striped, since the
  target is actually called "striped".

* Move dm.h to sys/sys/dm.h

* Get rid of files.dm, a leftover from the import

26 files changed:
sys/conf/files
sys/config/LINT
sys/dev/disk/dm/Makefile
sys/dev/disk/dm/device-mapper.c
sys/dev/disk/dm/dm_dev.c
sys/dev/disk/dm/dm_ioctl.c
sys/dev/disk/dm/dm_pdev.c
sys/dev/disk/dm/dm_table.c
sys/dev/disk/dm/dm_target.c
sys/dev/disk/dm/dm_target_error.c
sys/dev/disk/dm/dm_target_zero.c
sys/dev/disk/dm/doc/TODO [moved from sys/dev/disk/dm/TODO with 100% similarity]
sys/dev/disk/dm/files.dm [deleted file]
sys/dev/disk/dm/targets/Makefile [new file with mode: 0644]
sys/dev/disk/dm/targets/crypt/Makefile [new file with mode: 0644]
sys/dev/disk/dm/targets/crypt/dm_target_crypt.c [moved from sys/dev/disk/dm/dm_target_crypt.c with 99% similarity]
sys/dev/disk/dm/targets/dmirror/Makefile [new file with mode: 0644]
sys/dev/disk/dm/targets/dmirror/dm_target_dmirror.c [moved from sys/dev/disk/dm/dm_target_dmirror.c with 99% similarity]
sys/dev/disk/dm/targets/dmirror/dmirror_notes.txt [moved from sys/dev/disk/dm/dmirror_notes.txt with 100% similarity]
sys/dev/disk/dm/targets/linear/Makefile [new file with mode: 0644]
sys/dev/disk/dm/targets/linear/dm_target_linear.c [moved from sys/dev/disk/dm/dm_target_linear.c with 99% similarity]
sys/dev/disk/dm/targets/mirror/dm_target_mirror.c [moved from sys/dev/disk/dm/dm_target_mirror.c with 100% similarity]
sys/dev/disk/dm/targets/snapshot/dm_target_snapshot.c [moved from sys/dev/disk/dm/dm_target_snapshot.c with 100% similarity]
sys/dev/disk/dm/targets/striped/Makefile [new file with mode: 0644]
sys/dev/disk/dm/targets/striped/dm_target_striped.c [moved from sys/dev/disk/dm/dm_target_stripe.c with 99% similarity]
sys/sys/dm.h [moved from sys/dev/disk/dm/dm.h with 95% similarity]

index 2dcb59a..5595b0b 100644 (file)
@@ -67,13 +67,11 @@ dev/disk/dm/dm_ioctl.c                      optional dm
 dev/disk/dm/dm_pdev.c                  optional dm
 dev/disk/dm/dm_table.c                 optional dm
 dev/disk/dm/dm_target.c                        optional dm
-dev/disk/dm/dm_target_crypt.c          optional dm
 dev/disk/dm/dm_target_error.c          optional dm
-dev/disk/dm/dm_target_linear.c         optional dm
-#dev/disk/dm/dm_target_mirror.c                optional dm
-#dev/disk/dm/dm_target_snapshot.c      optional dm
-dev/disk/dm/dm_target_stripe.c         optional dm
 dev/disk/dm/dm_target_zero.c           optional dm
+dev/disk/dm/targets/crypt/dm_target_crypt.c    optional dm_target_crypt
+dev/disk/dm/targets/linear/dm_target_linear.c  optional dm_target_linear
+dev/disk/dm/targets/striped/dm_target_striped.c        optional dm_target_striped
 
 dev/disk/iscsi/initiator/isc_cam.c     optional iscsi_initiator scbus
 dev/disk/iscsi/initiator/isc_sm.c      optional iscsi_initiator scbus
index fcec5e8..664aa95 100644 (file)
@@ -931,6 +931,9 @@ device              ses                     #SCSI SES/SAF-TE driver
 
 # Options for device mapper
 device         dm
+device         dm_target_crypt
+device         dm_target_linear
+device         dm_target_striped
 
 # Options for iSCSI
 device         iscsi_initiator
index 36089bc..a750e60 100644 (file)
@@ -1,8 +1,9 @@
 KMOD=   dm
 SRCS=   device-mapper.c dm_dev.c dm_ioctl.c dm_pdev.c dm_table.c \
        dm_target.c
-SRCS+= bus_if.h device_if.h
 
-SRCS+= dm_target_error.c dm_target_linear.c dm_target_zero.c dm_target_stripe.c dm_target_crypt.c
+SRCS+= dm_target_error.c dm_target_zero.c 
+
+SUBDIR=        targets
 
 .include <bsd.kmod.mk>
index 214d5aa..5ee46f4 100644 (file)
@@ -47,9 +47,9 @@
 #include <sys/malloc.h>
 #include <sys/module.h>
 #include <sys/sysctl.h>
+#include <sys/dm.h>
 
 #include "netbsd-dm.h"
-#include "dm.h"
 
 static d_ioctl_t       dmioctl;
 static d_open_t        dmopen;
@@ -102,6 +102,7 @@ static moduledata_t dm_mod = {
     NULL
 };
 DECLARE_MODULE(dm, dm_mod, SI_SUB_RAID, SI_ORDER_ANY);
+MODULE_VERSION(dm, 1);
 
 /*
  * This array is used to translate cmd to function pointer.
@@ -676,6 +677,24 @@ dmsetdiskinfo(struct disk *disk, dm_table_head_t *head)
        disk_setdiskinfo(disk, &info);
 }
 
+void
+dm_builtin_init(void *arg)
+{
+       modeventhand_t evh = (modeventhand_t)arg;
+
+       KKASSERT(evh != NULL);
+       evh(NULL, MOD_LOAD, NULL);
+}
+
+void
+dm_builtin_uninit(void *arg)
+{
+       modeventhand_t evh = (modeventhand_t)arg;
+
+       KKASSERT(evh != NULL);
+       evh(NULL, MOD_UNLOAD, NULL);
+}
+
 TUNABLE_INT("debug.dm_debug", &dm_debug_level);
 SYSCTL_INT(_debug, OID_AUTO, dm_debug, CTLFLAG_RW, &dm_debug_level,
               0, "Eanble device mapper debugging");
index 20ca7c0..6031fe9 100644 (file)
@@ -36,9 +36,9 @@
 #include <sys/disklabel.h>
 #include <sys/ioccom.h>
 #include <sys/malloc.h>
+#include <sys/dm.h>
 
 #include "netbsd-dm.h"
-#include "dm.h"
 
 static dm_dev_t *dm_dev_lookup_name(const char *);
 static dm_dev_t *dm_dev_lookup_uuid(const char *);
index ef0427b..612fd44 100644 (file)
@@ -89,9 +89,9 @@
 #include <sys/malloc.h>
 #include <sys/udev.h>
 #include <sys/vnode.h>
+#include <sys/dm.h>
 
 #include "netbsd-dm.h"
-#include "dm.h"
 
 extern struct dev_ops dm_ops;
 extern struct devfs_bitmap dm_minor_bitmap;
@@ -739,7 +739,8 @@ dm_table_load_ioctl(prop_dictionary_t dm_dict)
                 * If we want to deny table with 2 or more different
                 * target we should do it here
                 */
-               if ((target = dm_target_lookup(type)) == NULL) {
+               if (((target = dm_target_lookup(type)) == NULL) &&
+                   ((target = dm_target_autoload(type)) == NULL)) {
                        dm_table_release(&dmv->table_head, DM_TABLE_INACTIVE);
                        dm_dev_unbusy(dmv);
                        return ENOENT;
index 66f6db2..063e8f5 100644 (file)
@@ -39,7 +39,7 @@
 #include <sys/vnode.h>
 #include <sys/nlookup.h>
 
-#include "dm.h"
+#include <sys/dm.h>
 
 SLIST_HEAD(dm_pdevs, dm_pdev) dm_pdev_list;
 
index 4b3a2ae..a6fd3c1 100644 (file)
@@ -34,7 +34,7 @@
 
 #include <sys/malloc.h>
 
-#include "dm.h"
+#include <sys/dm.h>
 
 /*
  * There are two types of users of this interface:
index b6c4dba..88c36c4 100644 (file)
 
 #include <sys/malloc.h>
 #include <sys/module.h>
-
+#include <sys/linker.h>
+#include <sys/dm.h>
 
 #include "netbsd-dm.h"
-#include "dm.h"
 
 static dm_target_t *dm_target_lookup_name(const char *);
 
@@ -66,6 +66,37 @@ dm_target_unbusy(dm_target_t * target)
        atomic_subtract_int(&target->ref_cnt, 1);
 }
 
+/*
+ * Try to autoload the module for the requested target.
+ */
+dm_target_t *
+dm_target_autoload(const char *dm_target_name)
+{
+       char mod_name[128];
+       dm_target_t *dmt;
+       linker_file_t linker_file;
+       int error;
+
+       ksnprintf(mod_name, sizeof(mod_name), "dm_target_%s", dm_target_name);
+       error = linker_reference_module(mod_name, NULL, &linker_file);
+       if (error != 0) {
+               kprintf("dm: could not autoload module for target %s\n",
+                   dm_target_name);
+               return NULL;
+       }
+
+       dmt = dm_target_lookup(dm_target_name);
+       if (dmt == NULL) {
+               linker_release_module(NULL, NULL, linker_file);
+               return NULL;
+       }
+
+       /* XXX: extra-big hack to allow users to kldunload the module */
+       linker_file->userrefs = 1;
+
+       return dmt;
+}
+
 /*
  * Lookup for target in global target list.
  */
index 6cb9d32..2b875ba 100644 (file)
@@ -37,7 +37,7 @@
 
 #include <sys/buf.h>
 
-#include "dm.h"
+#include <sys/dm.h>
 
 /* Init function called from dm_table_load_ioctl. */
 static int
@@ -143,5 +143,5 @@ dmte_mod_handler(module_t mod, int type, void *unused)
 
        return err;
 }
-       
-DM_TARGET_MODULE(dm_target_error, dmte_mod_handler);
+
+DM_TARGET_BUILTIN(dm_target_error, dmte_mod_handler);
index c7474b7..484d9d3 100644 (file)
@@ -38,7 +38,7 @@
 
 #include <sys/buf.h>
 
-#include "dm.h"
+#include <sys/dm.h>
 
 /*
  * Zero target init function. This target doesn't need
@@ -148,5 +148,5 @@ dmtz_mod_handler(module_t mod, int type, void *unused)
 
        return err;
 }
-       
-DM_TARGET_MODULE(dm_target_zero, dmtz_mod_handler);
+
+DM_TARGET_BUILTIN(dm_target_zero, dmtz_mod_handler);
diff --git a/sys/dev/disk/dm/files.dm b/sys/dev/disk/dm/files.dm
deleted file mode 100644 (file)
index 078029d..0000000
+++ /dev/null
@@ -1,9 +0,0 @@
-defpseudodev   dm
-file       dev/dm/device-mapper.c      dm
-file        dev/dm/dm_dev.c            dm
-file        dev/dm/dm_ioctl.c           dm
-file        dev/dm/dm_pdev.c            dm
-file        dev/dm/dm_table.c           dm
-file        dev/dm/dm_target.c          dm
-file        dev/dm/dm_target_linear.c   dm
-file       dev/dm/dm_target_stripe.c   dm
diff --git a/sys/dev/disk/dm/targets/Makefile b/sys/dev/disk/dm/targets/Makefile
new file mode 100644 (file)
index 0000000..8f70eaa
--- /dev/null
@@ -0,0 +1,3 @@
+SUBDIR= linear striped crypt
+
+.include <bsd.subdir.mk>
diff --git a/sys/dev/disk/dm/targets/crypt/Makefile b/sys/dev/disk/dm/targets/crypt/Makefile
new file mode 100644 (file)
index 0000000..df5f1af
--- /dev/null
@@ -0,0 +1,9 @@
+DM_PATH=       ${.CURDIR}/../../
+
+KMOD=   dm_target_crypt
+
+SRCS+= bus_if.h device_if.h
+SRCS+= dm_target_crypt.c
+CFLAGS+=-I${DM_PATH}
+
+.include <bsd.kmod.mk>
similarity index 99%
rename from sys/dev/disk/dm/dm_target_crypt.c
rename to sys/dev/disk/dm/targets/crypt/dm_target_crypt.c
index dd831d7..abe4725 100644 (file)
@@ -54,7 +54,7 @@
 #include <opencrypto/rmd160.h>
 #include <machine/cpufunc.h>
 
-#include "dm.h"
+#include <sys/dm.h>
 MALLOC_DEFINE(M_DMCRYPT, "dm_crypt", "Device Mapper Target Crypt");
 
 struct target_crypt_config;
@@ -1387,9 +1387,10 @@ dmtc_mod_handler(module_t mod, int type, void *unused)
 
        case MOD_UNLOAD:
                err = dm_target_rem("crypt");
-               if (err == 0)
+               if (err == 0) {
                        dmtc_destroy_mpipe();
-               kprintf("dm_target_crypt: unloaded\n");
+                       kprintf("dm_target_crypt: unloaded\n");
+               }
                break;
 
        default:
diff --git a/sys/dev/disk/dm/targets/dmirror/Makefile b/sys/dev/disk/dm/targets/dmirror/Makefile
new file mode 100644 (file)
index 0000000..bcf89ea
--- /dev/null
@@ -0,0 +1,8 @@
+DM_PATH=       ${.CURDIR}/../../
+
+KMOD=   dm_target_dmirror
+
+SRCS+= dm_target_dmirror.c
+CFLAGS+=-I${DM_PATH}
+
+.include <bsd.kmod.mk>
similarity index 99%
rename from sys/dev/disk/dm/dm_target_dmirror.c
rename to sys/dev/disk/dm/targets/dmirror/dm_target_dmirror.c
index 33a59a5..4d71967 100644 (file)
@@ -44,7 +44,7 @@
 #include <sys/uuid.h>
 #include <sys/vnode.h>
 
-#include "dm.h"
+#include <sys/dm.h>
 MALLOC_DEFINE(M_DMDMIRROR, "dm_dmirror", "Device Mapper Target DMIRROR");
 
 /* segdesc flags */
diff --git a/sys/dev/disk/dm/targets/linear/Makefile b/sys/dev/disk/dm/targets/linear/Makefile
new file mode 100644 (file)
index 0000000..98031b5
--- /dev/null
@@ -0,0 +1,8 @@
+DM_PATH=       ${.CURDIR}/../../
+
+KMOD=   dm_target_linear
+
+SRCS+= dm_target_linear.c
+CFLAGS+=-I${DM_PATH}
+
+.include <bsd.kmod.mk>
similarity index 99%
rename from sys/dev/disk/dm/dm_target_linear.c
rename to sys/dev/disk/dm/targets/linear/dm_target_linear.c
index 6eb0207..310fab2 100644 (file)
@@ -42,7 +42,7 @@
 #include <sys/malloc.h>
 #include <sys/vnode.h>
 
-#include "dm.h"
+#include <sys/dm.h>
 MALLOC_DEFINE(M_DMLINEAR, "dm_linear", "Device Mapper Target Linear");
 
 /*
diff --git a/sys/dev/disk/dm/targets/striped/Makefile b/sys/dev/disk/dm/targets/striped/Makefile
new file mode 100644 (file)
index 0000000..d3db6d3
--- /dev/null
@@ -0,0 +1,8 @@
+DM_PATH=       ${.CURDIR}/../../
+
+KMOD=   dm_target_striped
+
+SRCS+= dm_target_striped.c
+CFLAGS+=-I${DM_PATH}
+
+.include <bsd.kmod.mk>
similarity index 99%
rename from sys/dev/disk/dm/dm_target_stripe.c
rename to sys/dev/disk/dm/targets/striped/dm_target_striped.c
index ade1d58..4d18051 100644 (file)
@@ -44,7 +44,7 @@
 #include <sys/malloc.h>
 #include <sys/vnode.h>
 
-#include "dm.h"
+#include <sys/dm.h>
 MALLOC_DEFINE(M_DMSTRIPE, "dm_stripe", "Device Mapper Target Stripe");
 
 static void dm_target_stripe_destroy_config(dm_target_stripe_config_t *tsc);
@@ -446,4 +446,4 @@ dmts_mod_handler(module_t mod, int type, void *unused)
        return err;
 }
 
-DM_TARGET_MODULE(dm_target_stripe, dmts_mod_handler);
+DM_TARGET_MODULE(dm_target_striped, dmts_mod_handler);
similarity index 95%
rename from sys/dev/disk/dm/dm.h
rename to sys/sys/dm.h
index 24e86d0..c8c55b5 100644 (file)
@@ -282,6 +282,7 @@ int dm_table_status_ioctl(prop_dictionary_t);
 
 /* dm_target.c */
 dm_target_t* dm_target_alloc(const char *);
+dm_target_t* dm_target_autoload(const char *);
 int dm_target_destroy(void);
 int dm_target_insert(dm_target_t *);
 prop_array_t dm_target_prop_list(void);
@@ -355,6 +356,10 @@ int dm_pdev_init(void);
 dm_pdev_t* dm_pdev_insert(const char *);
 off_t dm_pdev_correct_dump_offset(dm_pdev_t *, off_t);
 
+/* dm builtin magic */
+void dm_builtin_init(void *);
+void dm_builtin_uninit(void *);
+
 extern int dm_debug_level;
 MALLOC_DECLARE(M_DM);
 
@@ -368,7 +373,15 @@ MALLOC_DECLARE(M_DM);
            evh,                                                \
            NULL                                                \
     };                                                         \
-    DECLARE_MODULE(name, name##_mod, SI_SUB_DM_TARGETS, SI_ORDER_ANY)
+    DECLARE_MODULE(name, name##_mod, SI_SUB_DM_TARGETS,                \
+                  SI_ORDER_ANY);                               \
+    MODULE_DEPEND(name, dm, 1, 1, 1)
+
+#define DM_TARGET_BUILTIN(name, evh)                           \
+    SYSINIT(name##module, SI_SUB_DM_TARGETS, SI_ORDER_ANY,     \
+       dm_builtin_init, evh);                                  \
+    SYSUNINIT(name##module, SI_SUB_DM_TARGETS, SI_ORDER_ANY,   \
+       dm_builtin_uninit, evh)
 
 #endif /*_KERNEL*/