Whamcloud - gitweb
LU-15451 sec: read-only nodemap flag
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_handler.c
index 5fc80bf..ec1008a 100644 (file)
@@ -31,7 +31,6 @@
 #include <uapi/linux/lnet/nidstr.h>
 #include <lustre_net.h>
 #include <lustre_acl.h>
-#include <lustre_eacl.h>
 #include <obd_class.h>
 #include "nodemap_internal.h"
 
@@ -265,16 +264,16 @@ struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
 
        /* don't use 0@lo, use the first non-lo local NID instead */
        if (nid == LNET_NID_LO_0) {
-               struct lnet_process_id id;
+               struct lnet_processid id;
                int i = 0;
 
                do {
                        rc = LNetGetId(i++, &id);
                        if (rc < 0)
                                RETURN(ERR_PTR(-EINVAL));
-               } while (id.nid == LNET_NID_LO_0);
+               } while (nid_is_lo0(&id.nid));
 
-               nid = id.nid;
+               nid = lnet_nid_to_nid4(&id.nid);
                CDEBUG(D_INFO, "found nid %s\n", libcfs_nid2str(nid));
        }
 
@@ -634,7 +633,7 @@ EXPORT_SYMBOL(nodemap_get_from_exp);
  * mapping function for nodemap idmaps
  *
  * \param      nodemap         lu_nodemap structure defining nodemap
- * \param      node_type       NODEMAP_UID or NODEMAP_GID
+ * \param      node_type       NODEMAP_UID or NODEMAP_GID or NODEMAP_PROJID
  * \param      tree_type       NODEMAP_CLIENT_TO_FS or
  *                             NODEMAP_FS_TO_CLIENT
  * \param      id              id to map
@@ -670,10 +669,16 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap,
        if (unlikely(nodemap == NULL))
                goto out;
 
-       if (nodemap->nmf_map_uid_only && id_type == NODEMAP_GID)
+       if (id_type == NODEMAP_UID &&
+           !(nodemap->nmf_map_mode & NODEMAP_MAP_UID))
                goto out;
 
-       if (nodemap->nmf_map_gid_only && id_type == NODEMAP_UID)
+       if (id_type == NODEMAP_GID &&
+           !(nodemap->nmf_map_mode & NODEMAP_MAP_GID))
+               goto out;
+
+       if (id_type == NODEMAP_PROJID &&
+           !(nodemap->nmf_map_mode & NODEMAP_MAP_PROJID))
                goto out;
 
        if (id == 0) {
@@ -706,8 +711,10 @@ __u32 nodemap_map_id(struct lu_nodemap *nodemap,
 squash:
        if (id_type == NODEMAP_UID)
                RETURN(nodemap->nm_squash_uid);
-       else
+       if (id_type == NODEMAP_GID)
                RETURN(nodemap->nm_squash_gid);
+       if (id_type == NODEMAP_PROJID)
+               RETURN(nodemap->nm_squash_projid);
 out:
        RETURN(id);
 }
@@ -745,7 +752,8 @@ ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
        if (count < 0)
                RETURN(-EINVAL);
        if (count == 0)
-               RETURN(0);
+               /* if not proper ACL, do nothing and return initial size */
+               RETURN(size);
 
        for (end = entry + count; entry != end; entry++) {
                __u16 tag = le16_to_cpu(entry->e_tag);
@@ -807,13 +815,14 @@ int nodemap_add_range_helper(struct nodemap_config *config,
        }
 
        rc = range_insert(&config->nmc_range_tree, range);
-       if (rc != 0) {
-               CERROR("cannot insert nodemap range into '%s': rc = %d\n",
-                     nodemap->nm_name, rc);
+       if (rc) {
+               CDEBUG_LIMIT(rc == -EEXIST ? D_INFO : D_ERROR,
+                            "cannot insert nodemap range into '%s': rc = %d\n",
+                            nodemap->nm_name, rc);
                up_write(&config->nmc_range_tree_lock);
                list_del(&range->rn_list);
                range_destroy(range);
-               GOTO(out, rc = -ENOMEM);
+               GOTO(out, rc);
        }
 
        list_add(&range->rn_list, &nodemap->nm_ranges);
@@ -836,6 +845,7 @@ int nodemap_add_range_helper(struct nodemap_config *config,
 out:
        return rc;
 }
+
 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
 {
        struct lu_nodemap       *nodemap = NULL;
@@ -1156,6 +1166,8 @@ struct lu_nodemap *nodemap_create(const char *name,
        nodemap->nm_client_to_fs_uidmap = RB_ROOT;
        nodemap->nm_fs_to_client_gidmap = RB_ROOT;
        nodemap->nm_client_to_fs_gidmap = RB_ROOT;
+       nodemap->nm_fs_to_client_projidmap = RB_ROOT;
+       nodemap->nm_client_to_fs_projidmap = RB_ROOT;
 
        if (is_default) {
                nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
@@ -1169,13 +1181,14 @@ struct lu_nodemap *nodemap_create(const char *name,
                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->nmf_map_mode = NODEMAP_MAP_ALL;
                nodemap->nmf_enable_audit = 1;
                nodemap->nmf_forbid_encryption = 0;
+               nodemap->nmf_readonly_mount = 0;
 
                nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
                nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
+               nodemap->nm_squash_projid = NODEMAP_NOBODY_PROJID;
                nodemap->nm_fileset[0] = '\0';
                nodemap->nm_sepol[0] = '\0';
                if (!is_default)
@@ -1186,19 +1199,17 @@ 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->nmf_enable_audit =
-                       default_nodemap->nmf_enable_audit;
+               nodemap->nmf_deny_unknown = default_nodemap->nmf_deny_unknown;
+               nodemap->nmf_map_mode = default_nodemap->nmf_map_mode;
+               nodemap->nmf_enable_audit = default_nodemap->nmf_enable_audit;
                nodemap->nmf_forbid_encryption =
                        default_nodemap->nmf_forbid_encryption;
+               nodemap->nmf_readonly_mount =
+                       default_nodemap->nmf_readonly_mount;
 
                nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
                nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
+               nodemap->nm_squash_projid = default_nodemap->nm_squash_projid;
                nodemap->nm_fileset[0] = '\0';
                nodemap->nm_sepol[0] = '\0';
        }
@@ -1295,7 +1306,8 @@ out:
 }
 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
 
-int nodemap_set_mapping_mode(const char *name, enum nodemap_mapping_modes mode)
+int nodemap_set_mapping_mode(const char *name,
+                            enum nodemap_mapping_modes map_mode)
 {
        struct lu_nodemap       *nodemap = NULL;
        int                     rc = 0;
@@ -1306,22 +1318,7 @@ int nodemap_set_mapping_mode(const char *name, enum nodemap_mapping_modes mode)
        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);
-       }
+       nodemap->nmf_map_mode = map_mode;
        rc = nodemap_idx_nodemap_update(nodemap);
 
        nm_member_revoke_locks(nodemap);
@@ -1371,7 +1368,7 @@ EXPORT_SYMBOL(nodemap_set_squash_uid);
  * \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
+ * Update the squash_gid for a nodemap. The squash_gid is the gid
  * that the all client gids are mapped to if nodemap is active,
  * the trust_client_ids flag is not set, and the gid is not in
  * the idmap tree.
@@ -1398,14 +1395,69 @@ out:
 EXPORT_SYMBOL(nodemap_set_squash_gid);
 
 /**
- * Returns true if this nodemap has root user access. Always returns true if
- * nodemaps are not active.
+ * Update the squash_projid for a nodemap.
+ *
+ * \param      name            nodemap name
+ * \param      gid             the new projid to squash unknown projids to
+ * \retval     0 on success
+ *
+ * Update the squash_projid for a nodemap. The squash_projid is the projid
+ * that the all client projids are mapped to if nodemap is active,
+ * the trust_client_ids flag is not set, and the projid is not in
+ * the idmap tree.
+ */
+int nodemap_set_squash_projid(const char *name, projid_t projid)
+{
+       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->nm_squash_projid = projid;
+       rc = nodemap_idx_nodemap_update(nodemap);
+
+       nm_member_revoke_locks(nodemap);
+       nodemap_putref(nodemap);
+out:
+       return rc;
+}
+EXPORT_SYMBOL(nodemap_set_squash_projid);
+
+/**
+ * Check if nodemap allows setting quota.
+ *
+ * If nodemap is not active, always allow.
+ * For user and group quota, allow if the nodemap allows root access.
+ * For project quota, allow if project id is not squashed or deny_unknown
+ * is not set.
  *
  * \param      nodemap         nodemap to check access for
+ * \param      qc_type         quota type
+ * \param      id              client id to map
+ * \retval     true is setquota is allowed, false otherwise
  */
-bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
+bool nodemap_can_setquota(struct lu_nodemap *nodemap, __u32 qc_type, __u32 id)
 {
-       return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
+       if (!nodemap_active)
+               return true;
+
+       if (!nodemap || !nodemap->nmf_allow_root_access)
+               return false;
+
+       if (qc_type == PRJQUOTA) {
+               id = nodemap_map_id(nodemap, NODEMAP_PROJID,
+                                   NODEMAP_CLIENT_TO_FS, id);
+
+               if (id == nodemap->nm_squash_projid &&
+                   nodemap->nmf_deny_unknown)
+                       return false;
+       }
+
+       return true;
 }
 EXPORT_SYMBOL(nodemap_can_setquota);
 
@@ -1465,6 +1517,33 @@ out:
 }
 EXPORT_SYMBOL(nodemap_set_forbid_encryption);
 
+/**
+ * Set the nmf_readonly_mount flag to true or false.
+ * \param      name                    nodemap name
+ * \param      readonly_mount          if true, forbid rw mount
+ * \retval     0 on success
+ *
+ */
+int nodemap_set_readonly_mount(const char *name, bool readonly_mount)
+{
+       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_readonly_mount = readonly_mount;
+       rc = nodemap_idx_nodemap_update(nodemap);
+
+       nm_member_revoke_locks(nodemap);
+       nodemap_putref(nodemap);
+out:
+       return rc;
+}
+EXPORT_SYMBOL(nodemap_set_readonly_mount);
 
 /**
  * Add a nodemap