Add a mapping from grandparent driver name to hibernate io function to reduce
authorjmatthew <jmatthew@openbsd.org>
Sat, 10 Feb 2018 05:11:06 +0000 (05:11 +0000)
committerjmatthew <jmatthew@openbsd.org>
Sat, 10 Feb 2018 05:11:06 +0000 (05:11 +0000)
the number of ->dv_parent->dv_parent chains and make this more readable.

ok deraadt@ phessler@

sys/arch/amd64/amd64/hibernate_machdep.c
sys/arch/i386/i386/hibernate_machdep.c

index 4da1131..f2185eb 100644 (file)
@@ -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 <mlarkin@openbsd.org>
@@ -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;
 }
index da0f125..5bc0503 100644 (file)
@@ -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 <mlarkin@openbsd.org>
@@ -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;
 }