Whamcloud - gitweb
LU-9682 nodemap: delete nids range from nodemap correctly
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_handler.c
index 1c99301..a6ef9ca 100644 (file)
@@ -28,7 +28,7 @@
  */
 #include <linux/module.h>
 #include <linux/sort.h>
-#include <lnet/nidstr.h>
+#include <uapi/linux/lnet/nidstr.h>
 #include <lustre_net.h>
 #include <lustre_acl.h>
 #include <lustre_eacl.h>
@@ -253,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)
@@ -268,7 +287,7 @@ struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
        LASSERT(nodemap != NULL);
        nodemap_getref(nodemap);
 
-       return nodemap;
+       RETURN(nodemap);
 }
 
 /**
@@ -356,25 +375,33 @@ 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;
+       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);
 }
@@ -611,6 +638,12 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap,
        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;
@@ -663,7 +696,7 @@ ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
                        enum nodemap_tree_type tree_type)
 {
        posix_acl_xattr_header  *header = buf;
-       posix_acl_xattr_entry   *entry = &header->a_entries[0];
+       posix_acl_xattr_entry   *entry = GET_POSIX_ACL_XATTR_ENTRY(header);
        posix_acl_xattr_entry   *new_entry = entry;
        posix_acl_xattr_entry   *end;
        int                      count;
@@ -750,15 +783,21 @@ 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);
 
        /* if range_id is non-zero, we are loading from disk */
        if (range_id == 0)
                rc = nodemap_idx_range_add(range, nid);
 
-       nm_member_revoke_locks(config->nmc_default_nodemap);
-       nm_member_revoke_locks(nodemap);
+       if (config == active_config) {
+               nm_member_revoke_locks(config->nmc_default_nodemap);
+               nm_member_revoke_locks(nodemap);
+       }
 
 out:
        return rc;
@@ -789,7 +828,7 @@ EXPORT_SYMBOL(nodemap_add_range);
 /**
  * delete a range
  * \param      name            nodemap name
- * \param      range_str       string containing range
+ * \param      nid             nid range
  * \retval     0 on success
  *
  * Delete range from global range tree, and remove it
@@ -817,6 +856,10 @@ 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);
        }
+       if (range->rn_nodemap != nodemap) {
+               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);
@@ -978,6 +1021,9 @@ struct lu_nodemap *nodemap_create(const char *name,
        if (is_default || default_nodemap == NULL) {
                nodemap->nmf_trust_client_ids = 0;
                nodemap->nmf_allow_root_access = 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;
@@ -989,6 +1035,12 @@ struct lu_nodemap *nodemap_create(const char *name,
                                default_nodemap->nmf_trust_client_ids;
                nodemap->nmf_allow_root_access =
                                default_nodemap->nmf_allow_root_access;
+               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;
@@ -1003,12 +1055,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)
 {
@@ -1032,13 +1111,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)
 {
@@ -1061,11 +1139,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
@@ -1098,7 +1212,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
@@ -1319,24 +1433,31 @@ void nodemap_config_dealloc(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 */
+               /* 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);
        }
        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;
 
@@ -1350,6 +1471,7 @@ 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;
@@ -1380,6 +1502,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;
@@ -1392,7 +1522,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;
 }
@@ -1454,7 +1585,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);
 }
 
@@ -1477,6 +1608,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';
@@ -1486,20 +1620,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);
@@ -1507,10 +1642,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);