[libmultipath] add device blacklisting
authorChristophe Varoqui <root@xa-s05.(none)>
Wed, 18 Jan 2006 11:07:26 +0000 (12:07 +0100)
committerChristophe Varoqui <root@xa-s05.(none)>
Wed, 18 Jan 2006 11:07:26 +0000 (12:07 +0100)
Syntax is :

devnode_blacklist {
devnode sr*
wwid 6005*
device {
vendor DEC
product *
}
}

libmultipath/blacklist.c
libmultipath/blacklist.h
libmultipath/config.c
libmultipath/config.h
libmultipath/configure.c
libmultipath/dict.c
libmultipath/discovery.c
libmultipath/structs.c
multipath/main.c
multipathd/cli_handlers.c
multipathd/main.c

index c6f68f8..80ca8c4 100644 (file)
 #include "util.h"
 #include "debug.h"
 #include "regex.h"
+#include "structs.h"
+#include "config.h"
 #include "blacklist.h"
 
-static int
+struct blentry {
+       char * str;
+       regex_t regex;
+};
+
+struct blentry_device {
+       char * vendor;
+       char * product;
+       regex_t vendor_reg;
+       regex_t product_reg;
+};
+
+extern int
 store_ble (vector blist, char * str)
 {
-       regex_t * ble;
+       struct blentry * ble;
        
        if (!str)
                return 0;
 
-       ble = MALLOC(sizeof(regex_t));
+       if (!blist)
+               goto out;
+
+       ble = MALLOC(sizeof(struct blentry));
 
        if (!ble)
                goto out;
 
-       if (regcomp(ble, str, REG_EXTENDED|REG_NOSUB))
+       if (regcomp(&ble->regex, str, REG_EXTENDED|REG_NOSUB))
                goto out1;
 
        if (!vector_alloc_slot(blist))
                goto out1;
 
+       ble->str = str;
        vector_set_slot(blist, ble);
        return 0;
 out1:
        FREE(ble);
 out:
+       FREE(str);
        return 1;
 }
 
+
+extern int
+alloc_ble_device (vector blist)
+{
+       struct blentry_device * ble = MALLOC(sizeof(struct blentry_device));
+
+       if (!ble || !blist)
+               return 1;
+
+       if (!vector_alloc_slot(blist)) {
+               FREE(ble);
+               return 1;
+       }
+       vector_set_slot(blist, ble);
+       return 0;
+}
+       
+extern int
+set_ble_device (vector blist, char * vendor, char * product)
+{
+       struct blentry_device * ble;
+       
+       if (!blist)
+               return 1;
+
+       ble = VECTOR_SLOT(blist, VECTOR_SIZE(blist) - 1);
+
+       if (!ble)
+               return 1;
+
+       if (vendor) {
+               if (regcomp(&ble->vendor_reg, vendor,
+                           REG_EXTENDED|REG_NOSUB)) {
+                       FREE(vendor);
+                       return 1;
+               }
+               ble->vendor = vendor;
+       }
+       if (product) {
+               if (regcomp(&ble->product_reg, product,
+                           REG_EXTENDED|REG_NOSUB)) {
+                       FREE(product);
+                       return 1;
+               }
+               ble->product = product;
+       }
+       return 0;
+}
+
 int
 setup_default_blist (vector blist)
 {
@@ -50,14 +118,14 @@ setup_default_blist (vector blist)
 }
 
 int
-blacklist (vector blist, char * dev)
+blacklist (vector blist, char * str)
 {
        int i;
-       regex_t * ble;
+       struct blentry * ble;
 
        vector_foreach_slot (blist, ble, i) {
-               if (!regexec(ble, dev, 0, NULL, 0)) {
-                       condlog(3, "%s blacklisted", dev);
+               if (!regexec(&ble->regex, str, 0, NULL, 0)) {
+                       condlog(3, "%s blacklisted", str);
                        return 1;
                }
        }
@@ -65,21 +133,41 @@ blacklist (vector blist, char * dev)
 }
 
 int
-store_regex (vector blist, char * regex)
+blacklist_device (vector blist, char * vendor, char * product)
 {
-       if (!blist)
+       int i;
+       struct blentry_device * ble;
+
+       vector_foreach_slot (blist, ble, i) {
+               if (!regexec(&ble->vendor_reg, vendor, 0, NULL, 0) &&
+                   !regexec(&ble->product_reg, product, 0, NULL, 0)) {
+                       condlog(3, "%s:%s blacklisted", vendor, product);
+                       return 1;
+               }
+       }
+       return 0;
+}
+
+int
+blacklist_path (struct config * conf, struct path * pp)
+{
+       if (blacklist(conf->blist_devnode, pp->dev))
+               return 1;
+
+       if (blacklist(conf->blist_wwid, pp->wwid))
                return 1;
 
-       if (!regex)
+       if (pp->vendor_id && pp->product_id &&
+           blacklist_device(conf->blist_device, pp->vendor_id, pp->product_id))
                return 1;
 
-       return store_ble(blist, regex);
-}      
+       return 0;
+}
 
 void
 free_blacklist (vector blist)
 {
-       regex_t * ble;
+       struct blentry * ble;
        int i;
 
        if (!blist)
@@ -87,10 +175,29 @@ free_blacklist (vector blist)
 
        vector_foreach_slot (blist, ble, i) {
                if (ble) {
-                       regfree(ble);
+                       //regfree(ble->regex);
+                       FREE(ble->str);
                        FREE(ble);
                }
        }
+       vector_free(blist);
+}
+
+void
+free_blacklist_device (vector blist)
+{
+       struct blentry_device * ble;
+       int i;
 
+       if (!blist)
+               return;
+
+       vector_foreach_slot (blist, ble, i) {
+               if (ble) {
+                       FREE(ble->vendor);
+                       FREE(ble->product);
+                       FREE(ble);
+               }
+       }
        vector_free(blist);
 }
index 9817c9b..a7d98e3 100644 (file)
@@ -1,11 +1,14 @@
 #ifndef _BLACKLIST_H
 #define _BLACKLIST_H
 
-#define BLIST_ENTRY_SIZE 255
-
-int setup_default_blist (vector blist);
-int blacklist (vector blist, char * dev);
-int store_regex (vector blist, char * regex);
-void free_blacklist (vector blist);
+int setup_default_blist (vector);
+int alloc_ble_device (vector);
+int blacklist (vector, char *);
+int blacklist_device (vector, char *, char *);
+int blacklist_path (struct config *, struct path *);
+int store_ble (vector, char *);
+int set_ble_device (vector, char *, char *);
+void free_blacklist (vector);
+void free_blacklist_device (vector);
 
 #endif /* _BLACKLIST_H */
index 1422962..7c463c0 100644 (file)
 #include "dict.h"
 #include "hwtable.h"
 #include "vector.h"
+#include "structs.h"
+#include "config.h"
 #include "blacklist.h"
 #include "defaults.h"
-#include "config.h"
 
 #include "../libcheckers/checkers.h"
 
@@ -295,7 +296,9 @@ free_config (struct config * conf)
        if (conf->default_hwhandler)
                FREE(conf->default_hwhandler);
 
-       free_blacklist(conf->blist);
+       free_blacklist(conf->blist_devnode);
+       free_blacklist(conf->blist_wwid);
+       free_blacklist_device(conf->blist_device);
        free_mptable(conf->mptable);
        free_hwtable(conf->hwtable);
 
@@ -344,13 +347,25 @@ load_config (char * file)
        if (setup_default_hwtable(conf->hwtable))
                goto out;
 
-       if (conf->blist == NULL) {
-               conf->blist = vector_alloc();
+       if (conf->blist_devnode == NULL) {
+               conf->blist_devnode = vector_alloc();
+               
+               if (!conf->blist_devnode)
+                       goto out;
                
-               if (!conf->blist)
+               if (setup_default_blist(conf->blist_devnode))
                        goto out;
+       }
+       if (conf->blist_wwid == NULL) {
+               conf->blist_wwid = vector_alloc();
+               
+               if (!conf->blist_wwid)
+                       goto out;
+       }
+       if (conf->blist_device == NULL) {
+               conf->blist_device = vector_alloc();
                
-               if (setup_default_blist(conf->blist))
+               if (!conf->blist_device)
                        goto out;
        }
        if (conf->mptable == NULL) {
index 46c1431..dc0148d 100644 (file)
@@ -71,7 +71,10 @@ struct config {
 
        vector mptable;
        vector hwtable;
-       vector blist;
+
+       vector blist_devnode;
+       vector blist_wwid;
+       vector blist_device;
 };
 
 struct config * conf;
index e79ff12..121c4ee 100644 (file)
 #include "vector.h"
 #include "memory.h"
 #include "devmapper.h"
-#include "blacklist.h"
 #include "defaults.h"
 #include "structs.h"
 #include "structs_vec.h"
 #include "dmparser.h"
 #include "config.h"
+#include "blacklist.h"
 #include "propsel.h"
 #include "discovery.h"
 #include "debug.h"
@@ -409,7 +409,7 @@ coalesce_paths (struct vectors * vecs, vector newmp, char * refwwid)
 
                /* 1. if path has no unique id or wwid blacklisted */
                if (memcmp(empty_buff, pp1->wwid, WWID_SIZE) == 0 ||
-                   blacklist(conf->blist, pp1->wwid))
+                   blacklist_path(conf, pp1))
                        continue;
 
                /* 2. if path already coalesced */
index 9b782de..5f9b626 100644 (file)
@@ -227,29 +227,72 @@ names_handler(vector strvec)
 static int
 blacklist_handler(vector strvec)
 {
-       conf->blist = vector_alloc();
+       conf->blist_devnode = vector_alloc();
+       conf->blist_wwid = vector_alloc();
+       conf->blist_device = vector_alloc();
 
-       if (!conf->blist)
+       if (!conf->blist_devnode || !conf->blist_wwid || !conf->blist_device)
                return 1;
 
        return 0;
 }
 
 static int
-ble_handler(vector strvec)
+ble_devnode_handler(vector strvec)
 {
        char * buff;
-       int ret;
 
        buff = set_value(strvec);
 
        if (!buff)
                return 1;
 
-       ret = store_regex(conf->blist, buff);
-       FREE(buff);
+       return store_ble(conf->blist_devnode, buff);
+}
+
+static int
+ble_wwid_handler(vector strvec)
+{
+       char * buff;
+
+       buff = set_value(strvec);
+
+       if (!buff)
+               return 1;
+
+       return store_ble(conf->blist_wwid, buff);
+}
 
-       return ret;
+static int
+ble_device_handler(vector strvec)
+{
+       return alloc_ble_device(conf->blist_device);
+}
+
+static int
+ble_vendor_handler(vector strvec)
+{
+       char * buff;
+
+       buff = set_value(strvec);
+
+       if (!buff)
+               return 1;
+
+       return set_ble_device(conf->blist_device, buff, NULL);
+}
+
+static int
+ble_product_handler(vector strvec)
+{
+       char * buff;
+
+       buff = set_value(strvec);
+
+       if (!buff)
+               return 1;
+
+       return set_ble_device(conf->blist_device, NULL, buff);
 }
 
 /*
@@ -1240,8 +1283,13 @@ init_keywords(void)
        install_keyword("default_path_checker", &def_path_checker_handler, NULL);
 
        install_keyword_root("devnode_blacklist", &blacklist_handler);
-       install_keyword("devnode", &ble_handler, NULL);
-       install_keyword("wwid", &ble_handler, NULL);
+       install_keyword("devnode", &ble_devnode_handler, NULL);
+       install_keyword("wwid", &ble_wwid_handler, NULL);
+       install_keyword("device", &ble_device_handler, NULL);
+       install_sublevel();
+       install_keyword("vendor", &ble_vendor_handler, NULL);
+       install_keyword("product", &ble_product_handler, NULL);
+       install_sublevel_end();
 
        install_keyword_root("devices", &devices_handler);
        install_keyword("device", &device_handler, NULL);
index ff6a12d..35fb74f 100644 (file)
 
 #include "vector.h"
 #include "memory.h"
-#include "blacklist.h"
 #include "util.h"
 #include "structs.h"
-#include "callout.h"
 #include "config.h"
+#include "blacklist.h"
+#include "callout.h"
 #include "debug.h"
 #include "propsel.h"
 #include "sg_include.h"
@@ -80,7 +80,7 @@ path_discovery (vector pathvec, struct config * conf, int flag)
                if (!devp)
                        continue;
 
-               if (blacklist(conf->blist, devp->name))
+               if (blacklist(conf->blist_devnode, devp->name))
                        continue;
 
                if(safe_sprintf(path, "%s/block/%s/device", sysfs_path,
index a70a89b..b047158 100644 (file)
@@ -13,6 +13,7 @@
 #include "config.h"
 #include "debug.h"
 #include "structs_vec.h"
+#include "blacklist.h"
 
 struct path *
 alloc_path (void)
@@ -351,4 +352,3 @@ pathcount (struct multipath * mpp, int state)
 
        return count;
 }
-
index ebf45f8..b92a129 100644 (file)
@@ -232,7 +232,7 @@ configure (void)
                        dev = conf->dev;
        }
        
-       if (dev && blacklist(conf->blist, dev))
+       if (dev && blacklist(conf->blist_devnode, dev))
                goto out;
        
        /*
@@ -247,6 +247,9 @@ configure (void)
                        goto out;
                }
                condlog(3, "scope limited to %s", refwwid);
+
+               if (blacklist(conf->blist_wwid, refwwid))
+                       goto out;
        }
 
        /*
index 218e042..0c1efc9 100644 (file)
@@ -270,7 +270,7 @@ cli_add_path (void * v, char ** reply, int * len, void * data)
 
        condlog(2, "%s: add path (operator)", param);
 
-       if (blacklist(conf->blist, param)) {
+       if (blacklist(conf->blist_devnode, param)) {
                *reply = strdup("blacklisted");
                *len = strlen(*reply) + 1;
                condlog(2, "%s: path blacklisted", param);
@@ -298,6 +298,12 @@ cli_add_map (void * v, char ** reply, int * len, void * data)
 
        condlog(2, "%s: add map (operator)", param);
 
+       if (blacklist(conf->blist_wwid, param)) {
+               *reply = strdup("blacklisted");
+               *len = strlen(*reply) + 1;
+               condlog(2, "%s: map blacklisted", param);
+               return 0;
+       }
        return ev_add_map(param, vecs);
 }
 
index 745faff..4b4eacc 100644 (file)
 #include <config.h>
 #include <callout.h>
 #include <util.h>
-#include <blacklist.h>
 #include <hwtable.h>
 #include <defaults.h>
 #include <structs.h>
+#include <blacklist.h>
 #include <structs_vec.h>
 #include <dmparser.h>
 #include <devmapper.h>
@@ -952,7 +952,7 @@ uev_trigger (struct uevent * uev, void * trigger_data)
        /*
         * path add/remove event
         */
-       if (blacklist(conf->blist, devname))
+       if (blacklist(conf->blist_devnode, devname))
                goto out;
 
        if (!strncmp(uev->action, "add", 3)) {