Whamcloud - gitweb
LU-9679 ptlrpc: use OBD_ALLOC_PTR_ARRAY() and FREE
[fs/lustre-release.git] / lustre / ptlrpc / sec_config.c
index 43003b0..630994a 100644 (file)
@@ -23,7 +23,7 @@
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, 2016, Intel Corporation.
+ * Copyright (c) 2011, 2017, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
@@ -44,7 +44,7 @@
 #include <lustre_log.h>
 #include <lustre_disk.h>
 #include <lustre_dlm.h>
-#include <lustre_param.h>
+#include <uapi/linux/lustre/lustre_param.h>
 #include <lustre_sec.h>
 
 #include "ptlrpc_internal.h"
@@ -237,14 +237,13 @@ EXPORT_SYMBOL(sptlrpc_parse_rule);
 
 void sptlrpc_rule_set_free(struct sptlrpc_rule_set *rset)
 {
-        LASSERT(rset->srs_nslot ||
-                (rset->srs_nrule == 0 && rset->srs_rules == NULL));
+       LASSERT(rset->srs_nslot ||
+               (rset->srs_nrule == 0 && rset->srs_rules == NULL));
 
-        if (rset->srs_nslot) {
-                OBD_FREE(rset->srs_rules,
-                         rset->srs_nslot * sizeof(*rset->srs_rules));
-                sptlrpc_rule_set_init(rset);
-        }
+       if (rset->srs_nslot) {
+               OBD_FREE_PTR_ARRAY(rset->srs_rules, rset->srs_nslot);
+               sptlrpc_rule_set_init(rset);
+       }
 }
 EXPORT_SYMBOL(sptlrpc_rule_set_free);
 
@@ -263,19 +262,18 @@ int sptlrpc_rule_set_expand(struct sptlrpc_rule_set *rset)
 
        nslot = rset->srs_nslot + 8;
 
-        /* better use realloc() if available */
-        OBD_ALLOC(rules, nslot * sizeof(*rset->srs_rules));
-        if (rules == NULL)
-                return -ENOMEM;
+       /* better use realloc() if available */
+       OBD_ALLOC_PTR_ARRAY(rules, nslot);
+       if (rules == NULL)
+               return -ENOMEM;
 
-        if (rset->srs_nrule) {
-                LASSERT(rset->srs_nslot && rset->srs_rules);
-                memcpy(rules, rset->srs_rules,
-                       rset->srs_nrule * sizeof(*rset->srs_rules));
+       if (rset->srs_nrule) {
+               LASSERT(rset->srs_nslot && rset->srs_rules);
+               memcpy(rules, rset->srs_rules,
+                      rset->srs_nrule * sizeof(*rset->srs_rules));
 
-                OBD_FREE(rset->srs_rules,
-                         rset->srs_nslot * sizeof(*rset->srs_rules));
-        }
+               OBD_FREE_PTR_ARRAY(rset->srs_rules, rset->srs_nslot);
+       }
 
         rset->srs_rules = rules;
         rset->srs_nslot = nslot;
@@ -502,38 +500,7 @@ struct sptlrpc_conf {
 };
 
 static struct mutex sptlrpc_conf_lock;
-static struct list_head sptlrpc_confs;
-
-static inline int is_hex(char c)
-{
-        return ((c >= '0' && c <= '9') ||
-                (c >= 'a' && c <= 'f'));
-}
-
-static void target2fsname(const char *tgt, char *fsname, int buflen)
-{
-        const char     *ptr;
-        int             len;
-
-        ptr = strrchr(tgt, '-');
-        if (ptr) {
-                if ((strncmp(ptr, "-MDT", 4) != 0 &&
-                     strncmp(ptr, "-OST", 4) != 0) ||
-                    !is_hex(ptr[4]) || !is_hex(ptr[5]) ||
-                    !is_hex(ptr[6]) || !is_hex(ptr[7]))
-                        ptr = NULL;
-        }
-
-        /* if we didn't find the pattern, treat the whole string as fsname */
-        if (ptr == NULL)
-                len = strlen(tgt);
-        else
-                len = ptr - tgt;
-
-        len = min(len, buflen - 1);
-        memcpy(fsname, tgt, len);
-        fsname[len] = '\0';
-}
+static LIST_HEAD(sptlrpc_confs);
 
 static void sptlrpc_conf_free_rsets(struct sptlrpc_conf *conf)
 {
@@ -649,66 +616,86 @@ static int sptlrpc_conf_merge_rule(struct sptlrpc_conf *conf,
  * find one through the target name in the record inside conf_lock;
  * otherwise means caller already hold conf_lock.
  */
-static int __sptlrpc_process_config(struct lustre_cfg *lcfg,
-                                    struct sptlrpc_conf *conf)
+static int __sptlrpc_process_config(char *target, const char *fsname,
+                                   struct sptlrpc_rule *rule,
+                                   struct sptlrpc_conf *conf)
 {
-        char                   *target, *param;
-        char                    fsname[MTI_NAME_MAXLEN];
-        struct sptlrpc_rule     rule;
-        int                     rc;
-        ENTRY;
-
-        target = lustre_cfg_string(lcfg, 1);
-        if (target == NULL) {
-                CERROR("missing target name\n");
-                RETURN(-EINVAL);
-        }
-
-        param = lustre_cfg_string(lcfg, 2);
-        if (param == NULL) {
-                CERROR("missing parameter\n");
-                RETURN(-EINVAL);
-        }
-
-        CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
-
-        /* parse rule to make sure the format is correct */
-        if (strncmp(param, PARAM_SRPC_FLVR, sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
-                CERROR("Invalid sptlrpc parameter: %s\n", param);
-                RETURN(-EINVAL);
-        }
-        param += sizeof(PARAM_SRPC_FLVR) - 1;
+       int rc;
 
-        rc = sptlrpc_parse_rule(param, &rule);
-        if (rc)
-                RETURN(-EINVAL);
-
-        if (conf == NULL) {
-                target2fsname(target, fsname, sizeof(fsname));
+       ENTRY;
+       if (!conf) {
+               if (!fsname)
+                       return -ENODEV;
 
                mutex_lock(&sptlrpc_conf_lock);
-                conf = sptlrpc_conf_get(fsname, 0);
-                if (conf == NULL) {
-                        CERROR("can't find conf\n");
-                        rc = -ENOMEM;
-                } else {
-                        rc = sptlrpc_conf_merge_rule(conf, target, &rule);
-                }
+               conf = sptlrpc_conf_get(fsname, 0);
+               if (!conf) {
+                       CERROR("can't find conf\n");
+                       rc = -ENOMEM;
+               } else {
+                       rc = sptlrpc_conf_merge_rule(conf, target, rule);
+               }
                mutex_unlock(&sptlrpc_conf_lock);
-        } else {
+       } else {
                LASSERT(mutex_is_locked(&sptlrpc_conf_lock));
-                rc = sptlrpc_conf_merge_rule(conf, target, &rule);
-        }
+               rc = sptlrpc_conf_merge_rule(conf, target, rule);
+       }
 
-        if (rc == 0)
-                conf->sc_modified++;
+       if (!rc)
+               conf->sc_modified++;
 
-        RETURN(rc);
+       RETURN(rc);
 }
 
 int sptlrpc_process_config(struct lustre_cfg *lcfg)
 {
-        return __sptlrpc_process_config(lcfg, NULL);
+       char fsname[MTI_NAME_MAXLEN];
+       struct sptlrpc_rule rule;
+       char *target, *param;
+       int rc;
+
+       print_lustre_cfg(lcfg);
+
+       target = lustre_cfg_string(lcfg, 1);
+       if (!target) {
+               CERROR("missing target name\n");
+               return -EINVAL;
+       }
+
+       param = lustre_cfg_string(lcfg, 2);
+       if (!param) {
+               CERROR("missing parameter\n");
+               return -EINVAL;
+       }
+
+       /* parse rule to make sure the format is correct */
+       if (strncmp(param, PARAM_SRPC_FLVR,
+                   sizeof(PARAM_SRPC_FLVR) - 1) != 0) {
+               CERROR("Invalid sptlrpc parameter: %s\n", param);
+               return -EINVAL;
+       }
+       param += sizeof(PARAM_SRPC_FLVR) - 1;
+
+       CDEBUG(D_SEC, "processing rule: %s.%s\n", target, param);
+
+       /*
+        * Three types of targets exist for sptlrpc using conf_param
+        * 1.   '_mgs' which targets mgc srpc settings. Treat it as
+        *      as a special file system name.
+        * 2.   target is a device which can be fsname-MDTXXXX or
+        *      fsname-OSTXXXX. This can be verified by the function
+        *      server_name2fsname.
+        * 3.   If both above conditions are not meet then the target
+        *      is a actual filesystem.
+        */
+       if (server_name2fsname(target, fsname, NULL))
+               strlcpy(fsname, target, sizeof(target));
+
+       rc = sptlrpc_parse_rule(param, &rule);
+       if (rc)
+               return rc;
+
+       return __sptlrpc_process_config(target, fsname, &rule, NULL);
 }
 EXPORT_SYMBOL(sptlrpc_process_config);
 
@@ -855,7 +842,7 @@ void sptlrpc_conf_choose_flavor(enum lustre_sec_part from,
         char                     name[MTI_NAME_MAXLEN];
         int                      len, rc = 0;
 
-        target2fsname(target->uuid, name, sizeof(name));
+       obd_uuid2fsname(name, target->uuid, sizeof(name));
 
        mutex_lock(&sptlrpc_conf_lock);
 
@@ -922,11 +909,11 @@ void sptlrpc_conf_client_adapt(struct obd_device *obd)
 
        imp = obd->u.cli.cl_import;
        if (imp) {
-               spin_lock(&imp->imp_lock);
+               write_lock(&imp->imp_sec_lock);
                if (imp->imp_sec)
                        imp->imp_sec_expire = ktime_get_real_seconds() +
                                SEC_ADAPT_DELAY;
-               spin_unlock(&imp->imp_lock);
+               write_unlock(&imp->imp_sec_lock);
        }
 
        up_read(&obd->u.cli.cl_sem);
@@ -957,7 +944,7 @@ int sptlrpc_conf_target_get_rules(struct obd_device *obd,
                RETURN(-EINVAL);
        }
 
-       target2fsname(obd->obd_uuid.uuid, fsname, sizeof(fsname));
+       obd_uuid2fsname(fsname, obd->obd_uuid.uuid, sizeof(fsname));
 
        mutex_lock(&sptlrpc_conf_lock);
        conf = sptlrpc_conf_get(fsname, 0);
@@ -979,7 +966,6 @@ int sptlrpc_conf_target_get_rules(struct obd_device *obd,
 
 int  sptlrpc_conf_init(void)
 {
-       INIT_LIST_HEAD(&sptlrpc_confs);
        mutex_init(&sptlrpc_conf_lock);
        return 0;
 }