Whamcloud - gitweb
LU-8851 nodemap: add uid/gid only flags to control mapping
[fs/lustre-release.git] / lustre / ptlrpc / nodemap_storage.c
index 46a467c..04e3f13 100644 (file)
@@ -72,6 +72,8 @@ enum nm_flag_shifts {
        NM_FL_ALLOW_ROOT_ACCESS = 0x1,
        NM_FL_TRUST_CLIENT_IDS = 0x2,
        NM_FL_DENY_UNKNOWN = 0x4,
+       NM_FL_MAP_UID_ONLY = 0x8,
+       NM_FL_MAP_GID_ONLY = 0x10,
 };
 
 static void nodemap_cluster_key_init(struct nodemap_key *nk, unsigned int nm_id)
@@ -95,7 +97,11 @@ static void nodemap_cluster_rec_init(union nodemap_rec *nr,
                (nodemap->nmf_allow_root_access ?
                        NM_FL_ALLOW_ROOT_ACCESS : 0) |
                (nodemap->nmf_deny_unknown ?
-                       NM_FL_DENY_UNKNOWN : 0));
+                       NM_FL_DENY_UNKNOWN : 0) |
+               (nodemap->nmf_map_uid_only ?
+                       NM_FL_MAP_UID_ONLY : 0) |
+               (nodemap->nmf_map_gid_only ?
+                       NM_FL_MAP_GID_ONLY : 0));
 }
 
 static void nodemap_idmap_key_init(struct nodemap_key *nk, unsigned int nm_id,
@@ -162,22 +168,32 @@ static struct dt_object *nodemap_cache_find_create(const struct lu_env *env,
                                                   struct local_oid_storage *los,
                                                   enum ncfc_find_create create_new)
 {
-       struct lu_fid root_fid;
+       struct lu_fid tfid;
        struct dt_object *root_obj;
        struct dt_object *nm_obj;
        int rc = 0;
 
-       rc = dt_root_get(env, dev, &root_fid);
+       rc = dt_root_get(env, dev, &tfid);
        if (rc < 0)
                GOTO(out, nm_obj = ERR_PTR(rc));
 
-       root_obj = dt_locate(env, dev, &root_fid);
+       root_obj = dt_locate(env, dev, &tfid);
        if (unlikely(IS_ERR(root_obj)))
                GOTO(out, nm_obj = root_obj);
 
+       rc = dt_lookup_dir(env, root_obj, LUSTRE_NODEMAP_NAME, &tfid);
+       if (rc == -ENOENT) {
+               if (dev->dd_rdonly)
+                       GOTO(out_root, nm_obj = ERR_PTR(-EROFS));
+       } else if (rc) {
+               GOTO(out_root, nm_obj = ERR_PTR(rc));
+       } else if (dev->dd_rdonly && create_new == NCFC_CREATE_NEW) {
+               GOTO(out_root, nm_obj = ERR_PTR(-EROFS));
+       }
+
 again:
        /* if loading index fails the first time, create new index */
-       if (create_new == NCFC_CREATE_NEW) {
+       if (create_new == NCFC_CREATE_NEW && rc != -ENOENT) {
                CDEBUG(D_INFO, "removing old index, creating new one\n");
                rc = local_object_unlink(env, dev, root_obj,
                                         LUSTRE_NODEMAP_NAME);
@@ -203,7 +219,7 @@ again:
                 * rewrite the config
                 */
                if (rc < 0) {
-                       lu_object_put(env, &nm_obj->do_lu);
+                       dt_object_put(env, nm_obj);
 
                        if (create_new == NCFC_CREATE_NEW)
                                GOTO(out_root, nm_obj = ERR_PTR(rc));
@@ -216,7 +232,7 @@ again:
        }
 
 out_root:
-       lu_object_put(env, &root_obj->do_lu);
+       dt_object_put(env, root_obj);
 out:
        return nm_obj;
 }
@@ -615,6 +631,14 @@ int nodemap_idx_nodemap_activate(bool value)
        return nodemap_idx_global_add_update(value, NM_UPDATE);
 }
 
+static enum nodemap_idx_type nodemap_get_key_type(const struct nodemap_key *key)
+{
+       u32                      nodemap_id;
+
+       nodemap_id = le32_to_cpu(key->nk_nodemap_id);
+       return nm_idx_get_type(nodemap_id);
+}
+
 /**
  * Process a key/rec pair and modify the new configuration.
  *
@@ -642,10 +666,12 @@ static int nodemap_process_keyrec(struct nodemap_config *config,
        u32                      map[2];
        int                      rc;
 
+       ENTRY;
+
        CLASSERT(sizeof(union nodemap_rec) == 32);
 
        nodemap_id = le32_to_cpu(key->nk_nodemap_id);
-       type = nm_idx_get_type(nodemap_id);
+       type = nodemap_get_key_type(key);
        nodemap_id = nm_idx_set_type(nodemap_id, 0);
 
        CDEBUG(D_INFO, "found config entry, nm_id %d type %d\n",
@@ -721,6 +747,10 @@ static int nodemap_process_keyrec(struct nodemap_config *config,
                                        flags & NM_FL_TRUST_CLIENT_IDS;
                nodemap->nmf_deny_unknown =
                                        flags & NM_FL_DENY_UNKNOWN;
+               nodemap->nmf_map_uid_only =
+                                       flags & NM_FL_MAP_UID_ONLY;
+               nodemap->nmf_map_gid_only =
+                                       flags & NM_FL_MAP_GID_ONLY;
 
                if (*recent_nodemap == NULL) {
                        *recent_nodemap = nodemap;
@@ -764,21 +794,29 @@ static int nodemap_process_keyrec(struct nodemap_config *config,
 
        rc = type;
 
+       EXIT;
+
 out:
        return rc;
 }
 
+enum nm_config_passes {
+       NM_READ_CLUSTERS = 0,
+       NM_READ_ATTRIBUTES = 1,
+};
+
 static int nodemap_load_entries(const struct lu_env *env,
                                struct dt_object *nodemap_idx)
 {
-       const struct dt_it_ops  *iops;
-       struct dt_it            *it;
-       struct lu_nodemap       *recent_nodemap = NULL;
-       struct nodemap_config   *new_config = NULL;
-       u64                      hash = 0;
-       bool                     activate_nodemap = false;
-       bool                     loaded_global_idx = false;
-       int                      rc = 0;
+       const struct dt_it_ops *iops;
+       struct dt_it *it;
+       struct lu_nodemap *recent_nodemap = NULL;
+       struct nodemap_config *new_config = NULL;
+       u64 hash = 0;
+       bool activate_nodemap = false;
+       bool loaded_global_idx = false;
+       enum nm_config_passes cur_pass = NM_READ_CLUSTERS;
+       int rc = 0;
 
        ENTRY;
 
@@ -816,23 +854,43 @@ static int nodemap_load_entries(const struct lu_env *env,
        while (rc == 0) {
                struct nodemap_key *key;
                union nodemap_rec rec;
+               enum nodemap_idx_type key_type;
 
                key = (struct nodemap_key *)iops->key(env, it);
-               rc = iops->rec(env, it, (struct dt_rec *)&rec, 0);
-               if (rc != -ESTALE) {
-                       if (rc != 0)
-                               GOTO(out_nodemap_config, rc);
-                       rc = nodemap_process_keyrec(new_config, key, &rec,
-                                                   &recent_nodemap);
-                       if (rc < 0)
-                               GOTO(out_nodemap_config, rc);
-                       if (rc == NODEMAP_GLOBAL_IDX)
-                               loaded_global_idx = true;
+               key_type = nodemap_get_key_type((struct nodemap_key *)key);
+               if ((cur_pass == NM_READ_CLUSTERS &&
+                               key_type == NODEMAP_CLUSTER_IDX) ||
+                   (cur_pass == NM_READ_ATTRIBUTES &&
+                               key_type != NODEMAP_CLUSTER_IDX &&
+                               key_type != NODEMAP_EMPTY_IDX)) {
+                       rc = iops->rec(env, it, (struct dt_rec *)&rec, 0);
+                       if (rc != -ESTALE) {
+                               if (rc != 0)
+                                       GOTO(out_nodemap_config, rc);
+                               rc = nodemap_process_keyrec(new_config, key, &rec,
+                                                           &recent_nodemap);
+                               if (rc < 0)
+                                       GOTO(out_nodemap_config, rc);
+                               if (rc == NODEMAP_GLOBAL_IDX)
+                                       loaded_global_idx = true;
+                       }
                }
 
                do
                        rc = iops->next(env, it);
                while (rc == -ESTALE);
+
+               /* move to second pass */
+               if (rc > 0 && cur_pass == NM_READ_CLUSTERS) {
+                       cur_pass = NM_READ_ATTRIBUTES;
+                       rc = iops->load(env, it, 0);
+                       if (rc == 0)
+                               rc = iops->next(env, it);
+                       else if (rc > 0)
+                               rc = 0;
+                       else
+                               GOTO(out, rc);
+               }
        }
 
        if (rc > 0)
@@ -916,7 +974,7 @@ struct dt_object *nodemap_save_config_cache(const struct lu_env *env,
        /* create a new index file to fill with active config */
        o = nodemap_cache_find_create(env, dev, los, NCFC_CREATE_NEW);
        if (IS_ERR(o))
-               GOTO(out, o);
+               RETURN(o);
 
        mutex_lock(&active_config_lock);
 
@@ -981,11 +1039,10 @@ struct dt_object *nodemap_save_config_cache(const struct lu_env *env,
        if (rc2 < 0)
                rc = rc2;
 
-out:
        mutex_unlock(&active_config_lock);
 
        if (rc < 0) {
-               lu_object_put(env, &o->do_lu);
+               dt_object_put(env, o);
                o = ERR_PTR(rc);
        }
 
@@ -1012,7 +1069,7 @@ static void nodemap_save_all_caches(void)
                struct dt_object *o;
 
                /* put current config file so save conf can rewrite it */
-               lu_object_put_nocache(&env, &ncf->ncf_obj->do_lu);
+               dt_object_put_nocache(&env, ncf->ncf_obj);
                ncf->ncf_obj = NULL;
 
                o = nodemap_save_config_cache(&env, dev, ncf->ncf_los);
@@ -1167,7 +1224,7 @@ void nm_config_file_deregister_mgs(const struct lu_env *env,
 
        nodemap_mgs_ncf = NULL;
        if (ncf->ncf_obj)
-               lu_object_put(env, &ncf->ncf_obj->do_lu);
+               dt_object_put(env, ncf->ncf_obj);
 
        OBD_FREE_PTR(ncf);
 
@@ -1188,7 +1245,7 @@ void nm_config_file_deregister_tgt(const struct lu_env *env,
        mutex_unlock(&ncf_list_lock);
 
        if (ncf->ncf_obj)
-               lu_object_put(env, &ncf->ncf_obj->do_lu);
+               dt_object_put(env, ncf->ncf_obj);
 
        OBD_FREE_PTR(ncf);
 
@@ -1234,6 +1291,109 @@ int nodemap_process_idx_pages(struct nodemap_config *config, union lu_page *lip,
 }
 EXPORT_SYMBOL(nodemap_process_idx_pages);
 
+static int nodemap_page_build(const struct lu_env *env, union lu_page *lp,
+                             size_t nob, const struct dt_it_ops *iops,
+                             struct dt_it *it, __u32 attr, void *arg)
+{
+       struct idx_info *ii = (struct idx_info *)arg;
+       struct lu_idxpage *lip = &lp->lp_idx;
+       char *entry;
+       size_t size = ii->ii_keysize + ii->ii_recsize;
+       int rc;
+       ENTRY;
+
+       if (nob < LIP_HDR_SIZE)
+               return -EINVAL;
+
+       /* initialize the header of the new container */
+       memset(lip, 0, LIP_HDR_SIZE);
+       lip->lip_magic = LIP_MAGIC;
+       nob           -= LIP_HDR_SIZE;
+
+       entry = lip->lip_entries;
+       do {
+               char            *tmp_entry = entry;
+               struct dt_key   *key;
+               __u64           hash;
+               enum nodemap_idx_type key_type;
+
+               /* fetch 64-bit hash value */
+               hash = iops->store(env, it);
+               ii->ii_hash_end = hash;
+
+               if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
+                       if (lip->lip_nr != 0)
+                               GOTO(out, rc = 0);
+               }
+
+               if (nob < size) {
+                       if (lip->lip_nr == 0)
+                               GOTO(out, rc = -EINVAL);
+                       GOTO(out, rc = 0);
+               }
+
+               key = iops->key(env, it);
+               key_type = nodemap_get_key_type((struct nodemap_key *)key);
+
+               /* on the first pass, get only the cluster types. On second
+                * pass, get all the rest */
+               if ((ii->ii_attrs == NM_READ_CLUSTERS &&
+                               key_type == NODEMAP_CLUSTER_IDX) ||
+                   (ii->ii_attrs == NM_READ_ATTRIBUTES &&
+                               key_type != NODEMAP_CLUSTER_IDX &&
+                               key_type != NODEMAP_EMPTY_IDX)) {
+                       memcpy(tmp_entry, key, ii->ii_keysize);
+                       tmp_entry += ii->ii_keysize;
+
+                       /* and finally the record */
+                       rc = iops->rec(env, it, (struct dt_rec *)tmp_entry,
+                                      attr);
+                       if (rc != -ESTALE) {
+                               if (rc != 0)
+                                       GOTO(out, rc);
+
+                               /* hash/key/record successfully copied! */
+                               lip->lip_nr++;
+                               if (unlikely(lip->lip_nr == 1 &&
+                                   ii->ii_count == 0))
+                                       ii->ii_hash_start = hash;
+
+                               entry = tmp_entry + ii->ii_recsize;
+                               nob -= size;
+                       }
+               }
+
+               /* move on to the next record */
+               do {
+                       rc = iops->next(env, it);
+               } while (rc == -ESTALE);
+
+               /* move to second pass */
+               if (rc > 0 && ii->ii_attrs == NM_READ_CLUSTERS) {
+                       ii->ii_attrs = NM_READ_ATTRIBUTES;
+                       rc = iops->load(env, it, 0);
+                       if (rc == 0)
+                               rc = iops->next(env, it);
+                       else if (rc > 0)
+                               rc = 0;
+                       else
+                               GOTO(out, rc);
+               }
+
+       } while (rc == 0);
+
+       GOTO(out, rc);
+out:
+       if (rc >= 0 && lip->lip_nr > 0)
+               /* one more container */
+               ii->ii_count++;
+       if (rc > 0)
+               /* no more entries */
+               ii->ii_hash_end = II_END_OFF;
+       return rc;
+}
+
+
 int nodemap_index_read(struct lu_env *env,
                       struct nm_config_file *ncf,
                       struct idx_info *ii,
@@ -1249,13 +1409,13 @@ int nodemap_index_read(struct lu_env *env,
        dt_read_lock(env, nodemap_idx, 0);
        version = dt_version_get(env, nodemap_idx);
        if (rdpg->rp_hash != 0 && ii->ii_version != version) {
-               CDEBUG(D_INFO, "nodemap config changed while sending, "
-                              "old "LPU64", new "LPU64"\n",
+               CDEBUG(D_INFO, "nodemap config changed inflight, old %llu, new %llu\n",
                       ii->ii_version,
                       version);
                ii->ii_hash_end = 0;
        } else {
-               rc = dt_index_walk(env, nodemap_idx, rdpg, NULL, ii);
+               rc = dt_index_walk(env, nodemap_idx, rdpg, nodemap_page_build,
+                                  ii);
                CDEBUG(D_INFO, "walked index, hashend %llx\n", ii->ii_hash_end);
        }
 
@@ -1323,6 +1483,7 @@ int nodemap_get_config_req(struct obd_device *mgs_obd,
        nodemap_ii.ii_magic = IDX_INFO_MAGIC;
        nodemap_ii.ii_flags = II_FL_NOHASH;
        nodemap_ii.ii_version = rqexp_ted->ted_nodemap_version;
+       nodemap_ii.ii_attrs = body->mcb_nm_cur_pass;
 
        bytes = nodemap_index_read(req->rq_svc_thread->t_env,
                                   mgs_obd->u.obt.obt_nodemap_config_file,
@@ -1336,7 +1497,7 @@ int nodemap_get_config_req(struct obd_device *mgs_obd,
        if (res == NULL)
                GOTO(out, rc = -EINVAL);
        res->mcr_offset = nodemap_ii.ii_hash_end;
-       res->mcr_size = bytes;
+       res->mcr_nm_cur_pass = nodemap_ii.ii_attrs;
 
        page_count = (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
        LASSERT(page_count <= rdpg.rp_count);