From 3855a6ef30ef0b6a82a6e9a623313f685aaba79a Mon Sep 17 00:00:00 2001 From: jmatthew Date: Sat, 10 Feb 2018 05:11:06 +0000 Subject: [PATCH] Add a mapping from grandparent driver name to hibernate io function to reduce the number of ->dv_parent->dv_parent chains and make this more readable. ok deraadt@ phessler@ --- sys/arch/amd64/amd64/hibernate_machdep.c | 36 ++++++++++++++---------- sys/arch/i386/i386/hibernate_machdep.c | 31 ++++++++++++-------- 2 files changed, 41 insertions(+), 26 deletions(-) diff --git a/sys/arch/amd64/amd64/hibernate_machdep.c b/sys/arch/amd64/amd64/hibernate_machdep.c index 4da113137ba..f2185eb836d 100644 --- a/sys/arch/amd64/amd64/hibernate_machdep.c +++ b/sys/arch/amd64/amd64/hibernate_machdep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hibernate_machdep.c,v 1.39 2017/05/29 12:58:37 jmatthew Exp $ */ +/* $OpenBSD: hibernate_machdep.c,v 1.40 2018/02/10 05:11:06 jmatthew Exp $ */ /* * Copyright (c) 2012 Mike Larkin @@ -95,26 +95,32 @@ get_hibernate_io_function(dev_t dev) extern int sr_hibernate_io(dev_t dev, daddr_t blkno, vaddr_t addr, size_t size, int op, void *page); struct device *dv = disk_lookup(&sd_cd, DISKUNIT(dev)); - + struct { + const char *driver; + hibio_fn io_func; + } sd_io_funcs[] = { #if NAHCI > 0 - if (dv && dv->dv_parent && dv->dv_parent->dv_parent && - strcmp(dv->dv_parent->dv_parent->dv_cfdata->cf_driver->cd_name, - "ahci") == 0) - return ahci_hibernate_io; + { "ahci", ahci_hibernate_io }, #endif #if NNVME > 0 - if (dv && dv->dv_parent && dv->dv_parent->dv_parent && - strcmp(dv->dv_parent->dv_parent->dv_cfdata->cf_driver->cd_name, - "nvme") == 0) - return nvme_hibernate_io; + { "nvme", nvme_hibernate_io }, #endif #if NSOFTRAID > 0 - if (dv && dv->dv_parent && dv->dv_parent->dv_parent && - strcmp(dv->dv_parent->dv_parent->dv_cfdata->cf_driver->cd_name, - "softraid") == 0) - return sr_hibernate_io; - } + { "softraid", sr_hibernate_io }, #endif + }; + + if (dv && dv->dv_parent && dv->dv_parent->dv_parent) { + const char *driver = dv->dv_parent->dv_parent->dv_cfdata-> + cf_driver->cd_name; + int i; + + for (i = 0; i < nitems(sd_io_funcs); i++) { + if (strcmp(driver, sd_io_funcs[i].driver) == 0) + return sd_io_funcs[i].io_func; + } + } + } #endif /* NSD > 0 */ return NULL; } diff --git a/sys/arch/i386/i386/hibernate_machdep.c b/sys/arch/i386/i386/hibernate_machdep.c index da0f1251ded..5bc05033673 100644 --- a/sys/arch/i386/i386/hibernate_machdep.c +++ b/sys/arch/i386/i386/hibernate_machdep.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hibernate_machdep.c,v 1.49 2016/05/20 02:30:41 mlarkin Exp $ */ +/* $OpenBSD: hibernate_machdep.c,v 1.50 2018/02/10 05:11:06 jmatthew Exp $ */ /* * Copyright (c) 2011 Mike Larkin @@ -98,20 +98,29 @@ get_hibernate_io_function(dev_t dev) extern int sr_hibernate_io(dev_t dev, daddr_t blkno, vaddr_t addr, size_t size, int op, void *page); struct device *dv = disk_lookup(&sd_cd, DISKUNIT(dev)); - + struct { + const char *driver; + hibio_fn io_func; + } sd_io_funcs[] = { #if NAHCI > 0 - if (dv && dv->dv_parent && dv->dv_parent->dv_parent && - strcmp(dv->dv_parent->dv_parent->dv_cfdata->cf_driver->cd_name, - "ahci") == 0) - return ahci_hibernate_io; + { "ahci", ahci_hibernate_io }, #endif #if NSOFTRAID > 0 - if (dv && dv->dv_parent && dv->dv_parent->dv_parent && - strcmp(dv->dv_parent->dv_parent->dv_cfdata->cf_driver->cd_name, - "softraid") == 0) - return sr_hibernate_io; - } + { "softraid", sr_hibernate_io }, #endif + }; + + if (dv && dv->dv_parent && dv->dv_parent->dv_parent) { + const char *driver = dv->dv_parent->dv_parent->dv_cfdata-> + cf_driver->cd_name; + int i; + + for (i = 0; i < nitems(sd_io_funcs); i++) { + if (strcmp(driver, sd_io_funcs[i].driver) == 0) + return sd_io_funcs[i].io_func; + } + } + } #endif /* NSD > 0 */ return NULL; } -- 2.20.1