Whamcloud - gitweb
LU-7418 tests: cancel all locks in sanity test_29
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_handler.c
index ec7dda2..10bbc49 100644 (file)
@@ -89,9 +89,11 @@ static void nodemap_destroy(struct lu_nodemap *nodemap)
 /**
  * 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));
 }
 
 /**
@@ -100,12 +102,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)
@@ -350,7 +359,7 @@ EXPORT_SYMBOL(nodemap_parse_idmap);
  */
 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
 {
-       struct lu_nodemap       *nodemap;
+       struct lu_nodemap *nodemap;
        int rc;
 
        mutex_lock(&active_config_lock);
@@ -375,10 +384,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;
+
+       /* using ac lock to prevent nodemap reclassification while deleting */
+       mutex_lock(&active_config_lock);
 
-       if (nodemap != NULL)
-               nm_member_del(nodemap, exp);
+       /* 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);
 
@@ -492,6 +525,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
@@ -523,6 +600,8 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap,
        struct lu_idmap         *idmap = NULL;
        __u32                    found_id;
 
+       ENTRY;
+
        if (!nodemap_active)
                goto out;
 
@@ -554,15 +633,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);
 
@@ -752,6 +831,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
@@ -844,6 +989,7 @@ struct lu_nodemap *nodemap_create(const char *name,
 
                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;
@@ -986,7 +1132,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);
 
@@ -1071,6 +1217,15 @@ int nodemap_del(const char *nodemap_name)
         */
        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);
@@ -1120,10 +1275,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;
@@ -1141,6 +1317,9 @@ void nodemap_config_cleanup(struct nodemap_config *config)
        list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
                                 nm_list) {
                down_write(&config->nmc_range_tree_lock);
+
+               /* move members to new config */
+               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);
@@ -1148,33 +1327,9 @@ void nodemap_config_cleanup(struct nodemap_config *config)
 
                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,
@@ -1237,6 +1392,7 @@ void nodemap_config_set_active(struct nodemap_config *config)
 
        EXIT;
 }
+EXPORT_SYMBOL(nodemap_config_set_active);
 
 /**
  * Cleanup nodemap module on exit