Whamcloud - gitweb
LU-8851 nodemap: add uid/gid only flags to control mapping
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_handler.c
index 5bf4932..b438dda 100644 (file)
@@ -61,6 +61,8 @@ struct nodemap_config *active_config;
  */
 static void nodemap_destroy(struct lu_nodemap *nodemap)
 {
+       ENTRY;
+
        if (nodemap->nm_pde_data != NULL)
                lprocfs_nodemap_remove(nodemap->nm_pde_data);
 
@@ -68,26 +70,31 @@ static void nodemap_destroy(struct lu_nodemap *nodemap)
        down_read(&active_config->nmc_range_tree_lock);
        nm_member_reclassify_nodemap(nodemap);
        up_read(&active_config->nmc_range_tree_lock);
-       mutex_unlock(&active_config_lock);
-
-       if (!list_empty(&nodemap->nm_member_list))
-               CWARN("nodemap_destroy failed to reclassify all members\n");
 
        write_lock(&nodemap->nm_idmap_lock);
        idmap_delete_tree(nodemap);
        write_unlock(&nodemap->nm_idmap_lock);
 
+       mutex_unlock(&active_config_lock);
+
+       if (!list_empty(&nodemap->nm_member_list))
+               CWARN("nodemap_destroy failed to reclassify all members\n");
+
        nm_member_delete_list(nodemap);
 
        OBD_FREE_PTR(nodemap);
+
+       EXIT;
 }
 
 /**
  * Functions used for the cfs_hash
  */
-static void nodemap_getref(struct lu_nodemap *nodemap)
+void nodemap_getref(struct lu_nodemap *nodemap)
 {
        atomic_inc(&nodemap->nm_refcount);
+       CDEBUG(D_INFO, "GETting nodemap %s(p=%p) : new refcount %d\n",
+              nodemap->nm_name, nodemap, atomic_read(&nodemap->nm_refcount));
 }
 
 /**
@@ -96,12 +103,19 @@ static void nodemap_getref(struct lu_nodemap *nodemap)
  */
 void nodemap_putref(struct lu_nodemap *nodemap)
 {
-       LASSERT(nodemap != NULL);
+       if (!nodemap)
+               return;
+
        LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
 
+       CDEBUG(D_INFO, "PUTting nodemap %s(p=%p) : new refcount %d\n",
+              nodemap->nm_name, nodemap,
+              atomic_read(&nodemap->nm_refcount) - 1);
+
        if (atomic_dec_and_test(&nodemap->nm_refcount))
                nodemap_destroy(nodemap);
 }
+EXPORT_SYMBOL(nodemap_putref);
 
 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
                            const void *key, unsigned mask)
@@ -239,11 +253,30 @@ struct lu_nodemap *nodemap_lookup(const char *name)
  * \param      nid                     nid to classify
  * \retval     nodemap                 nodemap containing the nid
  * \retval     default_nodemap         default nodemap
+ * \retval     -EINVAL                 LO nid given without other local nid
  */
 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
 {
        struct lu_nid_range     *range;
        struct lu_nodemap       *nodemap;
+       int rc;
+
+       ENTRY;
+
+       /* don't use 0@lo, use the first non-lo local NID instead */
+       if (LNET_NETTYP(LNET_NIDNET(nid)) == LOLND) {
+               struct lnet_process_id id;
+               int i = 0;
+
+               do {
+                       rc = LNetGetId(i++, &id);
+                       if (rc < 0)
+                               RETURN(ERR_PTR(-EINVAL));
+               } while (LNET_NETTYP(LNET_NIDNET(id.nid)) == LOLND);
+
+               nid = id.nid;
+               CDEBUG(D_INFO, "found nid %s\n", libcfs_nid2str(nid));
+       }
 
        range = range_search(&active_config->nmc_range_tree, nid);
        if (range != NULL)
@@ -251,9 +284,10 @@ struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
        else
                nodemap = active_config->nmc_default_nodemap;
 
+       LASSERT(nodemap != NULL);
        nodemap_getref(nodemap);
 
-       return nodemap;
+       RETURN(nodemap);
 }
 
 /**
@@ -341,26 +375,35 @@ EXPORT_SYMBOL(nodemap_parse_idmap);
  * \param      nid             nid to add to the members
  * \param      exp             obd_export structure for the connection
  *                             that is being added
- * \retval     -EINVAL         export is NULL
+ * \retval     -EINVAL         export is NULL, or has invalid NID
  * \retval     -EEXIST         export is already member of a nodemap
  */
 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
 {
-       struct lu_nodemap       *nodemap;
-       int rc;
+       struct lu_nodemap *nodemap;
+       int rc = 0;
+       ENTRY;
 
        mutex_lock(&active_config_lock);
        down_read(&active_config->nmc_range_tree_lock);
 
        nodemap = nodemap_classify_nid(nid);
-       rc = nm_member_add(nodemap, exp);
+
+       if (IS_ERR(nodemap)) {
+               CWARN("%s: error adding to nodemap, no valid NIDs found\n",
+                         exp->exp_obd->obd_name);
+               rc = -EINVAL;
+       } else {
+               rc = nm_member_add(nodemap, exp);
+       }
 
        up_read(&active_config->nmc_range_tree_lock);
        mutex_unlock(&active_config_lock);
 
-       nodemap_putref(nodemap);
+       if (!IS_ERR(nodemap))
+               nodemap_putref(nodemap);
 
-       return rc;
+       RETURN(rc);
 }
 EXPORT_SYMBOL(nodemap_add_member);
 
@@ -371,10 +414,34 @@ EXPORT_SYMBOL(nodemap_add_member);
  */
 void nodemap_del_member(struct obd_export *exp)
 {
-       struct lu_nodemap       *nodemap = exp->exp_target_data.ted_nodemap;
+       struct lu_nodemap *nodemap;
+
+       ENTRY;
 
-       if (nodemap != NULL)
-               nm_member_del(nodemap, exp);
+       /* using ac lock to prevent nodemap reclassification while deleting */
+       mutex_lock(&active_config_lock);
+
+       /* use of ted_nodemap is protected by active_config_lock. we take an
+        * extra reference to make sure nodemap isn't destroyed under
+        * active_config_lock
+        */
+       nodemap = exp->exp_target_data.ted_nodemap;
+       if (nodemap == NULL)
+               goto out;
+       else
+               nodemap_getref(nodemap);
+
+       mutex_lock(&nodemap->nm_member_list_lock);
+       nm_member_del(nodemap, exp);
+       mutex_unlock(&nodemap->nm_member_list_lock);
+
+out:
+       mutex_unlock(&active_config_lock);
+
+       if (nodemap)
+               nodemap_putref(nodemap);
+
+       EXIT;
 }
 EXPORT_SYMBOL(nodemap_del_member);
 
@@ -389,9 +456,9 @@ EXPORT_SYMBOL(nodemap_del_member);
  *
  * \retval     0 on success
  */
-static int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
-                                   enum nodemap_id_type id_type,
-                                   const __u32 map[2])
+int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
+                            enum nodemap_id_type id_type,
+                            const __u32 map[2])
 {
        struct lu_idmap         *idmap;
        int                     rc = 0;
@@ -408,6 +475,7 @@ static int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
 out:
        return rc;
 }
+
 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
                      const __u32 map[2])
 {
@@ -421,13 +489,14 @@ int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
                GOTO(out, rc = PTR_ERR(nodemap));
        }
 
-       if (is_default_nodemap(nodemap))
+       if (is_default_nodemap(nodemap)) {
                rc = -EINVAL;
-       else
+       } else {
                rc = nodemap_add_idmap_helper(nodemap, id_type, map);
-
+               if (rc == 0)
+                       rc = nodemap_idx_idmap_add(nodemap, id_type, map);
+       }
        mutex_unlock(&active_config_lock);
-
        nodemap_putref(nodemap);
 
 out:
@@ -466,10 +535,12 @@ int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
        write_lock(&nodemap->nm_idmap_lock);
        idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
                             map[0]);
-       if (idmap == NULL)
+       if (idmap == NULL) {
                rc = -EINVAL;
-       else
+       } else {
                idmap_delete(id_type, idmap, nodemap);
+               rc = nodemap_idx_idmap_del(nodemap, id_type, map);
+       }
        write_unlock(&nodemap->nm_idmap_lock);
 
 out_putref:
@@ -484,6 +555,50 @@ out:
 EXPORT_SYMBOL(nodemap_del_idmap);
 
 /**
+ * Get nodemap assigned to given export. Takes a reference on the nodemap.
+ *
+ * Note that this function may return either NULL, or an ERR_PTR()
+ * or a valid nodemap pointer.  All of the functions accessing the
+ * returned nodemap can check IS_ERR(nodemap) to see if an error is
+ * returned.  NULL is not considered an error, which is OK since this
+ * is a valid case if nodemap are not in use.  All nodemap handling
+ * functions must check for nodemap == NULL and do nothing, and the
+ * nodemap returned from this function should not be dereferenced.
+ *
+ * \param      export          export to get nodemap for
+ *
+ * \retval     pointer to nodemap on success
+ * \retval     NULL    nodemap subsystem disabled
+ * \retval     -EACCES export does not have nodemap assigned
+ */
+struct lu_nodemap *nodemap_get_from_exp(struct obd_export *exp)
+{
+       struct lu_nodemap *nodemap;
+
+       ENTRY;
+
+       if (!nodemap_active)
+               RETURN(NULL);
+
+       spin_lock(&exp->exp_target_data.ted_nodemap_lock);
+       nodemap = exp->exp_target_data.ted_nodemap;
+       if (nodemap)
+               nodemap_getref(nodemap);
+       spin_unlock(&exp->exp_target_data.ted_nodemap_lock);
+
+       if (!nodemap) {
+               CDEBUG(D_INFO, "%s: nodemap null on export %s (at %s)\n",
+                      exp->exp_obd->obd_name,
+                      obd_uuid2str(&exp->exp_client_uuid),
+                      obd_export_nid2str(exp));
+               RETURN(ERR_PTR(-EACCES));
+       }
+
+       RETURN(nodemap);
+}
+EXPORT_SYMBOL(nodemap_get_from_exp);
+
+/**
  * mapping function for nodemap idmaps
  *
  * \param      nodemap         lu_nodemap structure defining nodemap
@@ -515,12 +630,20 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap,
        struct lu_idmap         *idmap = NULL;
        __u32                    found_id;
 
+       ENTRY;
+
        if (!nodemap_active)
                goto out;
 
        if (unlikely(nodemap == NULL))
                goto out;
 
+       if (nodemap->nmf_map_uid_only && id_type == NODEMAP_GID)
+               goto out;
+
+       if (nodemap->nmf_map_gid_only && id_type == NODEMAP_UID)
+               goto out;
+
        if (id == 0) {
                if (nodemap->nmf_allow_root_access)
                        goto out;
@@ -546,15 +669,15 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap,
        else
                found_id = idmap->id_fs;
        read_unlock(&nodemap->nm_idmap_lock);
-       return found_id;
+       RETURN(found_id);
 
 squash:
        if (id_type == NODEMAP_UID)
-               return nodemap->nm_squash_uid;
+               RETURN(nodemap->nm_squash_uid);
        else
-               return nodemap->nm_squash_gid;
+               RETURN(nodemap->nm_squash_gid);
 out:
-       return id;
+       RETURN(id);
 }
 EXPORT_SYMBOL(nodemap_map_id);
 
@@ -623,23 +746,27 @@ ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
 EXPORT_SYMBOL(nodemap_map_acl);
 
 /*
- * add nid range to nodemap
+ * Add nid range to given nodemap
+ *
+ * \param      config          nodemap config to work on
  * \param      nodemap         nodemap to add range to
- * \param      range_st        string containing nid range
- * \retval     0 on success
+ * \param      nid             nid range to add
+ * \param      range_id        should be 0 unless loading from disk
+ * \retval     0               success
+ * \retval     -ENOMEM
  *
- * add an range to the global range tree and attached the
- * range to the named nodemap.
  */
-static int nodemap_add_range_helper(struct nodemap_config *config,
-                                   struct lu_nodemap *nodemap,
-                                   const lnet_nid_t nid[2])
+int nodemap_add_range_helper(struct nodemap_config *config,
+                            struct lu_nodemap *nodemap,
+                            const lnet_nid_t nid[2],
+                            unsigned int range_id)
 {
        struct lu_nid_range     *range;
        int rc;
 
        down_write(&config->nmc_range_tree_lock);
-       range = range_create(&config->nmc_range_tree, nid[0], nid[1], nodemap);
+       range = range_create(&config->nmc_range_tree, nid[0], nid[1],
+                            nodemap, range_id);
        if (range == NULL) {
                up_write(&config->nmc_range_tree_lock);
                GOTO(out, rc = -ENOMEM);
@@ -656,11 +783,21 @@ static int nodemap_add_range_helper(struct nodemap_config *config,
        }
 
        list_add(&range->rn_list, &nodemap->nm_ranges);
-       nm_member_reclassify_nodemap(config->nmc_default_nodemap);
+
+       /* nodemaps have no members if they aren't on the active config */
+       if (config == active_config)
+               nm_member_reclassify_nodemap(config->nmc_default_nodemap);
+
        up_write(&config->nmc_range_tree_lock);
 
-       nm_member_revoke_locks(config->nmc_default_nodemap);
-       nm_member_revoke_locks(nodemap);
+       /* if range_id is non-zero, we are loading from disk */
+       if (range_id == 0)
+               rc = nodemap_idx_range_add(range, nid);
+
+       if (config == active_config) {
+               nm_member_revoke_locks(config->nmc_default_nodemap);
+               nm_member_revoke_locks(nodemap);
+       }
 
 out:
        return rc;
@@ -680,9 +817,8 @@ int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
        if (is_default_nodemap(nodemap))
                rc = -EINVAL;
        else
-               rc = nodemap_add_range_helper(active_config, nodemap, nid);
+               rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
        mutex_unlock(&active_config_lock);
-
        nodemap_putref(nodemap);
 out:
        return rc;
@@ -720,6 +856,7 @@ int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
                up_write(&active_config->nmc_range_tree_lock);
                GOTO(out_putref, rc = -EINVAL);
        }
+       rc = nodemap_idx_range_del(range);
        range_delete(&active_config->nmc_range_tree, range);
        nm_member_reclassify_nodemap(nodemap);
        up_write(&active_config->nmc_range_tree_lock);
@@ -736,6 +873,72 @@ out:
 EXPORT_SYMBOL(nodemap_del_range);
 
 /**
+ * set fileset on nodemap
+ * \param      name            nodemap to set fileset on
+ * \param      fileset         string containing fileset
+ * \retval     0 on success
+ *
+ * set a fileset on the named nodemap
+ */
+static int nodemap_set_fileset_helper(struct nodemap_config *config,
+                                     struct lu_nodemap *nodemap,
+                                     const char *fileset)
+{
+       int rc = 0;
+
+       /* we allow fileset = "" which means clear fileset info */
+       if (fileset == NULL || (fileset[0] != 0 && fileset[0] != '/'))
+               rc = -EINVAL;
+       else if (strlcpy(nodemap->nm_fileset, fileset,
+                        sizeof(nodemap->nm_fileset)) >=
+                sizeof(nodemap->nm_fileset))
+               rc = -ENAMETOOLONG;
+
+       return rc;
+}
+
+int nodemap_set_fileset(const char *name, const char *fileset)
+{
+       struct lu_nodemap       *nodemap = NULL;
+       int                      rc = 0;
+
+       mutex_lock(&active_config_lock);
+       nodemap = nodemap_lookup(name);
+       if (IS_ERR(nodemap)) {
+               mutex_unlock(&active_config_lock);
+               GOTO(out, rc = PTR_ERR(nodemap));
+       }
+
+       if (is_default_nodemap(nodemap))
+               rc = -EINVAL;
+       else
+               rc = nodemap_set_fileset_helper(active_config, nodemap,
+                                               fileset);
+       mutex_unlock(&active_config_lock);
+
+       nodemap_putref(nodemap);
+out:
+       return rc;
+}
+EXPORT_SYMBOL(nodemap_set_fileset);
+
+/**
+ * get fileset defined on nodemap
+ * \param      nodemap         nodemap to get fileset from
+ * \retval     fileset name, or NULL if not defined or not activated
+ *
+ * get the fileset defined on the nodemap
+ */
+char *nodemap_get_fileset(const struct lu_nodemap *nodemap)
+{
+       if (!nodemap_active || is_default_nodemap(nodemap))
+               return NULL;
+       else
+               return (char *)nodemap->nm_fileset;
+}
+EXPORT_SYMBOL(nodemap_get_fileset);
+
+/**
  * Nodemap constructor
  *
  * Creates an lu_nodemap structure and assigns sane default
@@ -758,9 +961,12 @@ struct lu_nodemap *nodemap_create(const char *name,
                                  bool is_default)
 {
        struct lu_nodemap       *nodemap = NULL;
+       struct lu_nodemap       *default_nodemap;
        struct cfs_hash         *hash = config->nmc_nodemap_hash;
        int                      rc = 0;
 
+       default_nodemap = config->nmc_default_nodemap;
+
        if (!nodemap_name_is_valid(name))
                GOTO(out, rc = -EINVAL);
 
@@ -802,29 +1008,39 @@ struct lu_nodemap *nodemap_create(const char *name,
 
        if (is_default) {
                nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
+               config->nmc_default_nodemap = nodemap;
+       } else {
+               config->nmc_nodemap_highest_id++;
+               nodemap->nm_id = config->nmc_nodemap_highest_id;
+       }
+
+       if (is_default || default_nodemap == NULL) {
                nodemap->nmf_trust_client_ids = 0;
                nodemap->nmf_allow_root_access = 0;
-               nodemap->nmf_block_lookups = 0;
+               nodemap->nmf_deny_unknown = 0;
+               nodemap->nmf_map_uid_only = 0;
+               nodemap->nmf_map_gid_only = 0;
 
                nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
                nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
-
-               config->nmc_default_nodemap = nodemap;
+               if (!is_default)
+                       CWARN("adding nodemap '%s' to config without"
+                             " default nodemap\n", nodemap->nm_name);
        } else {
-               struct lu_nodemap *default_nodemap =
-                                       config->nmc_default_nodemap;
-
-               config->nmc_nodemap_highest_id++;
-               nodemap->nm_id = config->nmc_nodemap_highest_id;
                nodemap->nmf_trust_client_ids =
                                default_nodemap->nmf_trust_client_ids;
                nodemap->nmf_allow_root_access =
                                default_nodemap->nmf_allow_root_access;
-               nodemap->nmf_block_lookups =
-                               default_nodemap->nmf_block_lookups;
+               nodemap->nmf_deny_unknown =
+                               default_nodemap->nmf_deny_unknown;
+               nodemap->nmf_map_uid_only =
+                               default_nodemap->nmf_map_uid_only;
+               nodemap->nmf_map_gid_only =
+                               default_nodemap->nmf_map_gid_only;
 
                nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
                nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
+               nodemap->nm_fileset[0] = 0;
        }
 
        return nodemap;
@@ -835,12 +1051,39 @@ out:
 }
 
 /**
- * update flag to turn on or off nodemap functions
+ * Set the nmf_deny_unknown flag to true or false.
+ * \param      name            nodemap name
+ * \param      deny_unknown    if true, squashed users will get EACCES
+ * \retval     0 on success
+ *
+ */
+int nodemap_set_deny_unknown(const char *name, bool deny_unknown)
+{
+       struct lu_nodemap       *nodemap = NULL;
+       int                     rc = 0;
+
+       mutex_lock(&active_config_lock);
+       nodemap = nodemap_lookup(name);
+       mutex_unlock(&active_config_lock);
+       if (IS_ERR(nodemap))
+               GOTO(out, rc = PTR_ERR(nodemap));
+
+       nodemap->nmf_deny_unknown = deny_unknown;
+       rc = nodemap_idx_nodemap_update(nodemap);
+
+       nm_member_revoke_locks(nodemap);
+       nodemap_putref(nodemap);
+out:
+       return rc;
+}
+EXPORT_SYMBOL(nodemap_set_deny_unknown);
+
+/**
+ * Set the nmf_allow_root_access flag to true or false.
  * \param      name            nodemap name
- * \param      admin_string    string containing updated value
+ * \param      allow_root      if true, nodemap will not squash the root user
  * \retval     0 on success
  *
- * Update admin flag to turn on or off nodemap functions.
  */
 int nodemap_set_allow_root(const char *name, bool allow_root)
 {
@@ -854,6 +1097,7 @@ int nodemap_set_allow_root(const char *name, bool allow_root)
                GOTO(out, rc = PTR_ERR(nodemap));
 
        nodemap->nmf_allow_root_access = allow_root;
+       rc = nodemap_idx_nodemap_update(nodemap);
 
        nm_member_revoke_locks(nodemap);
        nodemap_putref(nodemap);
@@ -863,13 +1107,12 @@ out:
 EXPORT_SYMBOL(nodemap_set_allow_root);
 
 /**
- * updated trust_client_ids flag for nodemap
+ * Set the nmf_trust_client_ids flag to true or false.
  *
- * \param      name            nodemap name
- * \param      trust_string    new value for trust flag
+ * \param      name                    nodemap name
+ * \param      trust_client_ids        if true, nodemap will not map its IDs
  * \retval     0 on success
  *
- * Update the trust_client_ids flag for a nodemap.
  */
 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
 {
@@ -883,6 +1126,7 @@ int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
                GOTO(out, rc = PTR_ERR(nodemap));
 
        nodemap->nmf_trust_client_ids = trust_client_ids;
+       rc = nodemap_idx_nodemap_update(nodemap);
 
        nm_member_revoke_locks(nodemap);
        nodemap_putref(nodemap);
@@ -891,11 +1135,47 @@ out:
 }
 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
 
+int nodemap_set_mapping_mode(const char *name, enum nodemap_mapping_modes mode)
+{
+       struct lu_nodemap       *nodemap = NULL;
+       int                     rc = 0;
+
+       mutex_lock(&active_config_lock);
+       nodemap = nodemap_lookup(name);
+       mutex_unlock(&active_config_lock);
+       if (IS_ERR(nodemap))
+               GOTO(out, rc = PTR_ERR(nodemap));
+
+       switch (mode) {
+       case NODEMAP_MAP_BOTH:
+               nodemap->nmf_map_uid_only = 0;
+               nodemap->nmf_map_gid_only = 0;
+               break;
+       case NODEMAP_MAP_UID_ONLY:
+               nodemap->nmf_map_uid_only = 1;
+               nodemap->nmf_map_gid_only = 0;
+               break;
+       case NODEMAP_MAP_GID_ONLY:
+               nodemap->nmf_map_uid_only = 0;
+               nodemap->nmf_map_gid_only = 1;
+               break;
+       default:
+               CWARN("cannot set unknown mapping mode, mode = %d\n", mode);
+       }
+       rc = nodemap_idx_nodemap_update(nodemap);
+
+       nm_member_revoke_locks(nodemap);
+       nodemap_putref(nodemap);
+out:
+       return rc;
+}
+EXPORT_SYMBOL(nodemap_set_mapping_mode);
+
 /**
- * update the squash_uid for a nodemap
+ * Update the squash_uid for a nodemap.
  *
  * \param      name            nodemap name
- * \param      uid_string      string containing new squash_uid value
+ * \param      uid             the new uid to squash unknown users to
  * \retval     0 on success
  *
  * Update the squash_uid for a nodemap. The squash_uid is the uid
@@ -915,6 +1195,7 @@ int nodemap_set_squash_uid(const char *name, uid_t uid)
                GOTO(out, rc = PTR_ERR(nodemap));
 
        nodemap->nm_squash_uid = uid;
+       rc = nodemap_idx_nodemap_update(nodemap);
 
        nm_member_revoke_locks(nodemap);
        nodemap_putref(nodemap);
@@ -927,7 +1208,7 @@ EXPORT_SYMBOL(nodemap_set_squash_uid);
  * Update the squash_gid for a nodemap.
  *
  * \param      name            nodemap name
- * \param      gid_string      string containing new squash_gid value
+ * \param      gid             the new gid to squash unknown gids to
  * \retval     0 on success
  *
  * Update the squash_gid for a nodemap. The squash_uid is the gid
@@ -947,6 +1228,7 @@ int nodemap_set_squash_gid(const char *name, gid_t gid)
                GOTO(out, rc = PTR_ERR(nodemap));
 
        nodemap->nm_squash_gid = gid;
+       rc = nodemap_idx_nodemap_update(nodemap);
 
        nm_member_revoke_locks(nodemap);
        nodemap_putref(nodemap);
@@ -963,7 +1245,7 @@ EXPORT_SYMBOL(nodemap_set_squash_gid);
  */
 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
 {
-       return !nodemap_active || nodemap->nmf_allow_root_access;
+       return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
 }
 EXPORT_SYMBOL(nodemap_can_setquota);
 
@@ -988,7 +1270,10 @@ int nodemap_add(const char *nodemap_name)
                return PTR_ERR(nodemap);
        }
 
-       rc = lprocfs_nodemap_register(nodemap, 0);
+       rc = nodemap_idx_nodemap_add(nodemap);
+       if (rc == 0)
+               rc = lprocfs_nodemap_register(nodemap, 0);
+
        mutex_unlock(&active_config_lock);
        nodemap_putref(nodemap);
 
@@ -1010,6 +1295,7 @@ int nodemap_del(const char *nodemap_name)
        struct lu_nid_range     *range;
        struct lu_nid_range     *range_temp;
        int                      rc = 0;
+       int                      rc2 = 0;
 
        if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
                RETURN(-EINVAL);
@@ -1025,16 +1311,34 @@ int nodemap_del(const char *nodemap_name)
        /* erase nodemap from active ranges to prevent client assignment */
        down_write(&active_config->nmc_range_tree_lock);
        list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
-                                rn_list)
+                                rn_list) {
+               rc2 = nodemap_idx_range_del(range);
+               if (rc2 < 0)
+                       rc = rc2;
+
                range_delete(&active_config->nmc_range_tree, range);
+       }
        up_write(&active_config->nmc_range_tree_lock);
 
+       rc2 = nodemap_idx_nodemap_del(nodemap);
+       if (rc2 < 0)
+               rc = rc2;
+
        /*
         * remove procfs here in case nodemap_create called with same name
         * before nodemap_destroy is run.
         */
        lprocfs_nodemap_remove(nodemap->nm_pde_data);
        nodemap->nm_pde_data = NULL;
+
+       /* reclassify all member exports from nodemap, so they put their refs */
+       down_read(&active_config->nmc_range_tree_lock);
+       nm_member_reclassify_nodemap(nodemap);
+       up_read(&active_config->nmc_range_tree_lock);
+
+       if (!list_empty(&nodemap->nm_member_list))
+               CWARN("nodemap_del failed to reclassify all members\n");
+
        mutex_unlock(&active_config_lock);
 
        nodemap_putref(nodemap);
@@ -1056,6 +1360,7 @@ void nodemap_activate(const bool value)
 
        /* copy active value to global to avoid locking in map functions */
        nodemap_active = value;
+       nodemap_idx_nodemap_activate(value);
        mutex_unlock(&active_config_lock);
        nm_member_revoke_all();
 }
@@ -1083,10 +1388,31 @@ static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
        return 0;
 }
 
+struct nodemap_config *nodemap_config_alloc(void)
+{
+       struct nodemap_config *config;
+       int rc = 0;
+
+       OBD_ALLOC_PTR(config);
+       if (config == NULL)
+               return ERR_PTR(-ENOMEM);
+
+       rc = nodemap_init_hash(config);
+       if (rc != 0) {
+               OBD_FREE_PTR(config);
+               return ERR_PTR(rc);
+       }
+
+       init_rwsem(&config->nmc_range_tree_lock);
+
+       return config;
+}
+EXPORT_SYMBOL(nodemap_config_alloc);
+
 /**
  * Walk the nodemap_hash and remove all nodemaps.
  */
-void nodemap_config_cleanup(struct nodemap_config *config)
+void nodemap_config_dealloc(struct nodemap_config *config)
 {
        struct lu_nodemap       *nodemap = NULL;
        struct lu_nodemap       *nodemap_temp;
@@ -1103,45 +1429,31 @@ void nodemap_config_cleanup(struct nodemap_config *config)
         */
        list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
                                 nm_list) {
+               mutex_lock(&active_config_lock);
                down_write(&config->nmc_range_tree_lock);
+
+               /* move members to new config, requires ac lock */
+               nm_member_reclassify_nodemap(nodemap);
                list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
                                         rn_list)
                        range_delete(&config->nmc_range_tree, range);
                up_write(&config->nmc_range_tree_lock);
+               mutex_unlock(&active_config_lock);
 
+               /* putref must be outside of ac lock if nm could be destroyed */
                nodemap_putref(nodemap);
        }
-}
-
-struct nodemap_config *nodemap_config_alloc(void)
-{
-       struct nodemap_config *config;
-       int rc = 0;
-
-       OBD_ALLOC_PTR(config);
-       if (config == NULL)
-               return ERR_PTR(-ENOMEM);
-
-       rc = nodemap_init_hash(config);
-       if (rc != 0) {
-               OBD_FREE_PTR(config);
-               return ERR_PTR(rc);
-       }
-
-       init_rwsem(&config->nmc_range_tree_lock);
-
-       return config;
-}
-
-void nodemap_config_dealloc(struct nodemap_config *config)
-{
-       nodemap_config_cleanup(config);
        OBD_FREE_PTR(config);
 }
+EXPORT_SYMBOL(nodemap_config_dealloc);
 
-static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
-                          struct hlist_node *hnode,
-                          void *nodemap_list_head)
+/*
+ * callback for cfs_hash_for_each_safe used to convert a nodemap hash to a
+ * nodemap list, generally for locking purposes as a hash cb can't sleep.
+ */
+int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
+                   struct hlist_node *hnode,
+                   void *nodemap_list_head)
 {
        struct lu_nodemap *nodemap;
 
@@ -1155,11 +1467,13 @@ void nodemap_config_set_active(struct nodemap_config *config)
        struct nodemap_config   *old_config = active_config;
        struct lu_nodemap       *nodemap;
        struct lu_nodemap       *tmp;
+       bool revoke_locks;
        LIST_HEAD(nodemap_list_head);
 
        ENTRY;
 
        LASSERT(active_config != config);
+       LASSERT(config->nmc_default_nodemap);
 
        mutex_lock(&active_config_lock);
 
@@ -1184,6 +1498,14 @@ void nodemap_config_set_active(struct nodemap_config *config)
                }
        }
 
+       /*
+        * We only need to revoke locks if old nodemap was active, and new
+        * config is now nodemap inactive. nodemap_config_dealloc will
+        * reclassify exports, triggering a lock revoke if and only if new
+        * nodemap is active.
+        */
+       revoke_locks = !config->nmc_nodemap_is_active && nodemap_active;
+
        /* if new config is inactive, deactivate live config before switching */
        if (!config->nmc_nodemap_is_active)
                nodemap_active = false;
@@ -1196,7 +1518,8 @@ void nodemap_config_set_active(struct nodemap_config *config)
        if (old_config != NULL)
                nodemap_config_dealloc(old_config);
 
-       nm_member_revoke_all();
+       if (revoke_locks)
+               nm_member_revoke_all();
 
        EXIT;
 }
@@ -1258,7 +1581,7 @@ void nm_member_revoke_all(void)
 
        /* revoke_locks sleeps, so can't call in cfs hash cb */
        list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
-               nm_member_revoke_locks(nodemap);
+               nm_member_revoke_locks_always(nodemap);
        mutex_unlock(&active_config_lock);
 }
 
@@ -1281,6 +1604,9 @@ void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
        up_read(&active_config->nmc_range_tree_lock);
        mutex_unlock(&active_config_lock);
 
+       if (IS_ERR(nodemap))
+               return;
+
        strncpy(name_buf, nodemap->nm_name, name_len);
        if (name_len > 0)
                name_buf[name_len - 1] = '\0';
@@ -1290,20 +1616,21 @@ void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
 EXPORT_SYMBOL(nodemap_test_nid);
 
 /**
- * Returns the id mapping for a given nid/id pair. Useful for testing the
+ * Passes back the id mapping for a given nid/id pair. Useful for testing the
  * nodemap configuration to make sure it is working as expected.
  *
  * \param      nid             nid to classify
  * \param      idtype          uid or gid
  * \param      client_id       id to map to fs
+ * \param      fs_id_buf       pointer to save mapped fs_id to
  *
- * \retval     the mapped fs_id of the given client_id
+ * \retval     0       success
+ * \retval     -EINVAL invalid NID
  */
-__u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
-                     __u32 client_id)
+int nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
+                   __u32 client_id, __u32 *fs_id)
 {
        struct lu_nodemap       *nodemap;
-       __u32                    fs_id;
 
        mutex_lock(&active_config_lock);
        down_read(&active_config->nmc_range_tree_lock);
@@ -1311,10 +1638,13 @@ __u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
        up_read(&active_config->nmc_range_tree_lock);
        mutex_unlock(&active_config_lock);
 
-       fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
+       if (IS_ERR(nodemap))
+               return PTR_ERR(nodemap);
+
+       *fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
                               client_id);
        nodemap_putref(nodemap);
 
-       return fs_id;
+       return 0;
 }
 EXPORT_SYMBOL(nodemap_test_id);