⬢ DragonFlyBSD Kernel Audit
DF-4423 / dfdetach.c
← back to finding ↓ download raw
/*
 * DF-4423 helper KLD: synchronously invoke device_detach() on vtblk0,
 * exercising the same vtblk_detach() path as kldunload/hot-unplug:
 *   virtio_teardown_intr -> VTBLK_FLAG_DETACH -> serializer sweep
 *   -> virtio_stop -> vtblk_drain -> vtblk_finish_bio(bio, ENXIO)
 */
#include <sys/param.h>
#include <sys/module.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/bus.h>

static int
dfdetach_modevent(module_t mod, int type, void *arg)
{
	devclass_t dc;
	device_t dev;
	int error;

	switch (type) {
	case MOD_LOAD:
		dc = devclass_find("vtblk");
		if (dc == NULL) {
			kprintf("dfdetach: no vtblk devclass\n");
			return (ENOENT);
		}
		dev = devclass_get_device(dc, 0);
		if (dev == NULL) {
			kprintf("dfdetach: no vtblk0\n");
			return (ENOENT);
		}
		kprintf("dfdetach: detaching %s...\n", device_get_nameunit(dev));
		error = device_detach(dev);
		kprintf("dfdetach: device_detach -> %d\n", error);
		return (error);
	default:
		return (EOPNOTSUPP);
	}
}

static moduledata_t dfdetach_mod = { "dfdetach", dfdetach_modevent, 0 };
DECLARE_MODULE(dfdetach, dfdetach_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);