4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (C) 2015, Trustees of Indiana University
25 * Copyright (c) 2017, Intel Corporation.
27 * Author: Joshua Walgenbach <jjw@iu.edu>
28 * Author: Kit Westneat <cwestnea@iu.edu>
30 * Implements the storage functionality for the nodemap configuration. Functions
31 * in this file prepare, store, and load nodemap configuration data. Targets
32 * using nodemap services should register a configuration file object. Nodemap
33 * configuration changes that need to persist should call the appropriate
34 * storage function for the data being modified.
36 * There are several index types as defined in enum nodemap_idx_type:
37 * NODEMAP_CLUSTER_IDX stores the data found on the lu_nodemap struct,
38 * like root squash and config flags, as well as
40 * NODEMAP_RANGE_IDX stores NID range information for a nodemap
41 * NODEMAP_UIDMAP_IDX stores a fs/client UID mapping pair
42 * NODEMAP_GIDMAP_IDX stores a fs/client GID mapping pair
43 * NODEMAP_GLOBAL_IDX stores whether or not nodemaps are active
46 #include <libcfs/libcfs.h>
47 #include <linux/err.h>
48 #include <linux/kernel.h>
49 #include <linux/list.h>
50 #include <linux/mutex.h>
51 #include <linux/string.h>
52 #include <linux/types.h>
53 #include <uapi/linux/lnet/lnet-types.h>
54 #include <uapi/linux/lustre/lustre_idl.h>
55 #include <dt_object.h>
56 #include <lu_object.h>
57 #include <lustre_net.h>
58 #include <lustre_nodemap.h>
59 #include <obd_class.h>
60 #include <obd_support.h>
61 #include "nodemap_internal.h"
63 /* list of registered nodemap index files, except MGS */
64 static LIST_HEAD(ncf_list_head);
65 static DEFINE_MUTEX(ncf_list_lock);
67 /* MGS index is different than others, others are listeners to MGS idx */
68 static struct nm_config_file *nodemap_mgs_ncf;
70 /* lu_nodemap flags */
72 NM_FL_ALLOW_ROOT_ACCESS = 0x1,
73 NM_FL_TRUST_CLIENT_IDS = 0x2,
74 NM_FL_DENY_UNKNOWN = 0x4,
75 NM_FL_MAP_UID_ONLY = 0x8,
76 NM_FL_MAP_GID_ONLY = 0x10,
77 NM_FL_ENABLE_AUDIT = 0x20,
80 static void nodemap_cluster_key_init(struct nodemap_key *nk, unsigned int nm_id)
82 nk->nk_nodemap_id = cpu_to_le32(nm_idx_set_type(nm_id,
83 NODEMAP_CLUSTER_IDX));
87 static void nodemap_cluster_rec_init(union nodemap_rec *nr,
88 const struct lu_nodemap *nodemap)
90 CLASSERT(sizeof(nr->ncr.ncr_name) == sizeof(nodemap->nm_name));
92 strncpy(nr->ncr.ncr_name, nodemap->nm_name, sizeof(nodemap->nm_name));
93 nr->ncr.ncr_squash_uid = cpu_to_le32(nodemap->nm_squash_uid);
94 nr->ncr.ncr_squash_gid = cpu_to_le32(nodemap->nm_squash_gid);
95 nr->ncr.ncr_flags = cpu_to_le32(
96 (nodemap->nmf_trust_client_ids ?
97 NM_FL_TRUST_CLIENT_IDS : 0) |
98 (nodemap->nmf_allow_root_access ?
99 NM_FL_ALLOW_ROOT_ACCESS : 0) |
100 (nodemap->nmf_deny_unknown ?
101 NM_FL_DENY_UNKNOWN : 0) |
102 (nodemap->nmf_map_uid_only ?
103 NM_FL_MAP_UID_ONLY : 0) |
104 (nodemap->nmf_map_gid_only ?
105 NM_FL_MAP_GID_ONLY : 0) |
106 (nodemap->nmf_enable_audit ?
107 NM_FL_ENABLE_AUDIT : 0));
110 static void nodemap_idmap_key_init(struct nodemap_key *nk, unsigned int nm_id,
111 enum nodemap_id_type id_type,
114 enum nodemap_idx_type idx_type;
116 if (id_type == NODEMAP_UID)
117 idx_type = NODEMAP_UIDMAP_IDX;
119 idx_type = NODEMAP_GIDMAP_IDX;
121 nk->nk_nodemap_id = cpu_to_le32(nm_idx_set_type(nm_id, idx_type));
122 nk->nk_id_client = cpu_to_le32(id_client);
125 static void nodemap_idmap_rec_init(union nodemap_rec *nr, u32 id_fs)
127 nr->nir.nir_id_fs = cpu_to_le32(id_fs);
130 static void nodemap_range_key_init(struct nodemap_key *nk, unsigned int nm_id,
133 nk->nk_nodemap_id = cpu_to_le32(nm_idx_set_type(nm_id,
135 nk->nk_range_id = cpu_to_le32(rn_id);
138 static void nodemap_range_rec_init(union nodemap_rec *nr,
139 const lnet_nid_t nid[2])
141 nr->nrr.nrr_start_nid = cpu_to_le64(nid[0]);
142 nr->nrr.nrr_end_nid = cpu_to_le64(nid[1]);
145 static void nodemap_global_key_init(struct nodemap_key *nk)
147 nk->nk_nodemap_id = cpu_to_le32(nm_idx_set_type(0, NODEMAP_GLOBAL_IDX));
151 static void nodemap_global_rec_init(union nodemap_rec *nr, bool active)
153 nr->ngr.ngr_is_active = active;
156 /* should be called with dt_write lock */
157 static void nodemap_inc_version(const struct lu_env *env,
158 struct dt_object *nodemap_idx,
161 u64 ver = dt_version_get(env, nodemap_idx);
162 dt_version_set(env, nodemap_idx, ver + 1, th);
165 enum ncfc_find_create {
169 static struct dt_object *nodemap_cache_find_create(const struct lu_env *env,
170 struct dt_device *dev,
171 struct local_oid_storage *los,
172 enum ncfc_find_create create_new)
175 struct dt_object *root_obj;
176 struct dt_object *nm_obj;
179 rc = dt_root_get(env, dev, &tfid);
181 GOTO(out, nm_obj = ERR_PTR(rc));
183 root_obj = dt_locate(env, dev, &tfid);
184 if (unlikely(IS_ERR(root_obj)))
185 GOTO(out, nm_obj = root_obj);
187 rc = dt_lookup_dir(env, root_obj, LUSTRE_NODEMAP_NAME, &tfid);
190 GOTO(out_root, nm_obj = ERR_PTR(-EROFS));
192 GOTO(out_root, nm_obj = ERR_PTR(rc));
193 } else if (dev->dd_rdonly && create_new == NCFC_CREATE_NEW) {
194 GOTO(out_root, nm_obj = ERR_PTR(-EROFS));
198 /* if loading index fails the first time, create new index */
199 if (create_new == NCFC_CREATE_NEW && rc != -ENOENT) {
200 CDEBUG(D_INFO, "removing old index, creating new one\n");
201 rc = local_object_unlink(env, dev, root_obj,
202 LUSTRE_NODEMAP_NAME);
204 /* XXX not sure the best way to get obd name. */
205 CERROR("cannot destroy nodemap index: rc = %d\n",
207 GOTO(out_root, nm_obj = ERR_PTR(rc));
211 nm_obj = local_index_find_or_create(env, los, root_obj,
213 S_IFREG | S_IRUGO | S_IWUSR,
214 &dt_nodemap_features);
216 GOTO(out_root, nm_obj);
218 if (nm_obj->do_index_ops == NULL) {
219 rc = nm_obj->do_ops->do_index_try(env, nm_obj,
220 &dt_nodemap_features);
221 /* even if loading from tgt fails, connecting to MGS will
225 dt_object_put(env, nm_obj);
227 if (create_new == NCFC_CREATE_NEW)
228 GOTO(out_root, nm_obj = ERR_PTR(rc));
230 CERROR("cannot load nodemap index from disk, creating "
231 "new index: rc = %d\n", rc);
232 create_new = NCFC_CREATE_NEW;
238 dt_object_put(env, root_obj);
243 static int nodemap_idx_insert(const struct lu_env *env,
244 struct dt_object *idx,
245 const struct nodemap_key *nk,
246 const union nodemap_rec *nr)
249 struct dt_device *dev = lu2dt_dev(idx->do_lu.lo_dev);
252 CLASSERT(sizeof(union nodemap_rec) == 32);
254 th = dt_trans_create(env, dev);
257 GOTO(out, rc = PTR_ERR(th));
259 rc = dt_declare_insert(env, idx,
260 (const struct dt_rec *)nr,
261 (const struct dt_key *)nk, th);
265 rc = dt_declare_version_set(env, idx, th);
269 rc = dt_trans_start_local(env, dev, th);
273 dt_write_lock(env, idx, 0);
275 rc = dt_insert(env, idx, (const struct dt_rec *)nr,
276 (const struct dt_key *)nk, th, 1);
278 nodemap_inc_version(env, idx, th);
279 dt_write_unlock(env, idx);
281 dt_trans_stop(env, dev, th);
286 static int nodemap_idx_update(const struct lu_env *env,
287 struct dt_object *idx,
288 const struct nodemap_key *nk,
289 const union nodemap_rec *nr)
292 struct dt_device *dev = lu2dt_dev(idx->do_lu.lo_dev);
295 th = dt_trans_create(env, dev);
298 GOTO(out, rc = PTR_ERR(th));
300 rc = dt_declare_delete(env, idx, (const struct dt_key *)nk, th);
304 rc = dt_declare_insert(env, idx, (const struct dt_rec *)nr,
305 (const struct dt_key *)nk, th);
309 rc = dt_declare_version_set(env, idx, th);
313 rc = dt_trans_start_local(env, dev, th);
317 dt_write_lock(env, idx, 0);
319 rc = dt_delete(env, idx, (const struct dt_key *)nk, th);
323 rc = dt_insert(env, idx, (const struct dt_rec *)nr,
324 (const struct dt_key *)nk, th, 1);
328 nodemap_inc_version(env, idx, th);
330 dt_write_unlock(env, idx);
332 dt_trans_stop(env, dev, th);
337 static int nodemap_idx_delete(const struct lu_env *env,
338 struct dt_object *idx,
339 const struct nodemap_key *nk,
340 const union nodemap_rec *unused)
343 struct dt_device *dev = lu2dt_dev(idx->do_lu.lo_dev);
346 th = dt_trans_create(env, dev);
349 GOTO(out, rc = PTR_ERR(th));
351 rc = dt_declare_delete(env, idx, (const struct dt_key *)nk, th);
355 rc = dt_declare_version_set(env, idx, th);
359 rc = dt_trans_start_local(env, dev, th);
363 dt_write_lock(env, idx, 0);
365 rc = dt_delete(env, idx, (const struct dt_key *)nk, th);
367 nodemap_inc_version(env, idx, th);
369 dt_write_unlock(env, idx);
371 dt_trans_stop(env, dev, th);
381 static int nodemap_idx_nodemap_add_update(const struct lu_nodemap *nodemap,
382 struct dt_object *idx,
383 enum nm_add_update update)
385 struct nodemap_key nk;
386 union nodemap_rec nr;
392 rc = lu_env_init(&env, LCT_LOCAL);
396 nodemap_cluster_key_init(&nk, nodemap->nm_id);
397 nodemap_cluster_rec_init(&nr, nodemap);
399 if (update == NM_UPDATE)
400 rc = nodemap_idx_update(&env, idx, &nk, &nr);
402 rc = nodemap_idx_insert(&env, idx, &nk, &nr);
409 int nodemap_idx_nodemap_add(const struct lu_nodemap *nodemap)
411 if (nodemap_mgs_ncf == NULL) {
412 CERROR("cannot add nodemap config to non-existing MGS.\n");
416 return nodemap_idx_nodemap_add_update(nodemap, nodemap_mgs_ncf->ncf_obj,
420 int nodemap_idx_nodemap_update(const struct lu_nodemap *nodemap)
422 if (nodemap_mgs_ncf == NULL) {
423 CERROR("cannot add nodemap config to non-existing MGS.\n");
427 return nodemap_idx_nodemap_add_update(nodemap, nodemap_mgs_ncf->ncf_obj,
431 int nodemap_idx_nodemap_del(const struct lu_nodemap *nodemap)
434 struct lu_idmap *idmap;
435 struct lu_idmap *temp;
436 struct lu_nid_range *range;
437 struct lu_nid_range *range_temp;
438 struct nodemap_key nk;
445 if (nodemap_mgs_ncf == NULL) {
446 CERROR("cannot add nodemap config to non-existing MGS.\n");
450 rc = lu_env_init(&env, LCT_LOCAL);
454 root = nodemap->nm_fs_to_client_uidmap;
455 nm_rbtree_postorder_for_each_entry_safe(idmap, temp, &root,
457 nodemap_idmap_key_init(&nk, nodemap->nm_id, NODEMAP_UID,
459 rc2 = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj,
465 root = nodemap->nm_client_to_fs_gidmap;
466 nm_rbtree_postorder_for_each_entry_safe(idmap, temp, &root,
468 nodemap_idmap_key_init(&nk, nodemap->nm_id, NODEMAP_GID,
470 rc2 = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj,
476 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
478 nodemap_range_key_init(&nk, nodemap->nm_id, range->rn_id);
479 rc2 = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj,
485 nodemap_cluster_key_init(&nk, nodemap->nm_id);
486 rc2 = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj, &nk, NULL);
495 int nodemap_idx_range_add(const struct lu_nid_range *range,
496 const lnet_nid_t nid[2])
498 struct nodemap_key nk;
499 union nodemap_rec nr;
504 if (nodemap_mgs_ncf == NULL) {
505 CERROR("cannot add nodemap config to non-existing MGS.\n");
509 rc = lu_env_init(&env, LCT_LOCAL);
513 nodemap_range_key_init(&nk, range->rn_nodemap->nm_id, range->rn_id);
514 nodemap_range_rec_init(&nr, nid);
516 rc = nodemap_idx_insert(&env, nodemap_mgs_ncf->ncf_obj, &nk, &nr);
522 int nodemap_idx_range_del(const struct lu_nid_range *range)
524 struct nodemap_key nk;
529 if (nodemap_mgs_ncf == NULL) {
530 CERROR("cannot add nodemap config to non-existing MGS.\n");
534 rc = lu_env_init(&env, LCT_LOCAL);
538 nodemap_range_key_init(&nk, range->rn_nodemap->nm_id, range->rn_id);
540 rc = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj, &nk, NULL);
546 int nodemap_idx_idmap_add(const struct lu_nodemap *nodemap,
547 enum nodemap_id_type id_type,
550 struct nodemap_key nk;
551 union nodemap_rec nr;
556 if (nodemap_mgs_ncf == NULL) {
557 CERROR("cannot add nodemap config to non-existing MGS.\n");
561 rc = lu_env_init(&env, LCT_LOCAL);
565 nodemap_idmap_key_init(&nk, nodemap->nm_id, id_type, map[0]);
566 nodemap_idmap_rec_init(&nr, map[1]);
568 rc = nodemap_idx_insert(&env, nodemap_mgs_ncf->ncf_obj, &nk, &nr);
574 int nodemap_idx_idmap_del(const struct lu_nodemap *nodemap,
575 enum nodemap_id_type id_type,
578 struct nodemap_key nk;
583 if (nodemap_mgs_ncf == NULL) {
584 CERROR("cannot add nodemap config to non-existing MGS.\n");
588 rc = lu_env_init(&env, LCT_LOCAL);
592 nodemap_idmap_key_init(&nk, nodemap->nm_id, id_type, map[0]);
594 rc = nodemap_idx_delete(&env, nodemap_mgs_ncf->ncf_obj, &nk, NULL);
600 static int nodemap_idx_global_add_update(bool value, enum nm_add_update update)
602 struct nodemap_key nk;
603 union nodemap_rec nr;
608 if (nodemap_mgs_ncf == NULL) {
609 CERROR("cannot add nodemap config to non-existing MGS.\n");
613 rc = lu_env_init(&env, LCT_LOCAL);
617 nodemap_global_key_init(&nk);
618 nodemap_global_rec_init(&nr, value);
620 if (update == NM_UPDATE)
621 rc = nodemap_idx_update(&env, nodemap_mgs_ncf->ncf_obj,
624 rc = nodemap_idx_insert(&env, nodemap_mgs_ncf->ncf_obj,
632 int nodemap_idx_nodemap_activate(bool value)
634 return nodemap_idx_global_add_update(value, NM_UPDATE);
637 static enum nodemap_idx_type nodemap_get_key_type(const struct nodemap_key *key)
641 nodemap_id = le32_to_cpu(key->nk_nodemap_id);
642 return nm_idx_get_type(nodemap_id);
646 * Process a key/rec pair and modify the new configuration.
648 * \param config configuration to update with this key/rec data
649 * \param key key of the record that was loaded
650 * \param rec record that was loaded
651 * \param recent_nodemap last referenced nodemap
652 * \retval type of record processed, see enum #nodemap_idx_type
653 * \retval -ENOENT range or map loaded before nodemap record
654 * \retval -EINVAL duplicate nodemap cluster records found with
655 * different IDs, or nodemap has invalid name
658 static int nodemap_process_keyrec(struct nodemap_config *config,
659 const struct nodemap_key *key,
660 const union nodemap_rec *rec,
661 struct lu_nodemap **recent_nodemap)
663 struct lu_nodemap *nodemap = NULL;
664 enum nodemap_idx_type type;
665 enum nodemap_id_type id_type;
674 CLASSERT(sizeof(union nodemap_rec) == 32);
676 nodemap_id = le32_to_cpu(key->nk_nodemap_id);
677 type = nodemap_get_key_type(key);
678 nodemap_id = nm_idx_set_type(nodemap_id, 0);
680 CDEBUG(D_INFO, "found config entry, nm_id %d type %d\n",
683 /* find the correct nodemap in the load list */
684 if (type == NODEMAP_RANGE_IDX || type == NODEMAP_UIDMAP_IDX ||
685 type == NODEMAP_GIDMAP_IDX) {
686 struct lu_nodemap *tmp = NULL;
688 nodemap = *recent_nodemap;
691 GOTO(out, rc = -ENOENT);
693 if (nodemap->nm_id != nodemap_id) {
694 list_for_each_entry(tmp, &nodemap->nm_list, nm_list)
695 if (tmp->nm_id == nodemap_id) {
700 if (nodemap->nm_id != nodemap_id)
701 GOTO(out, rc = -ENOENT);
704 /* update most recently used nodemap if necessay */
705 if (nodemap != *recent_nodemap)
706 *recent_nodemap = nodemap;
710 case NODEMAP_EMPTY_IDX:
712 CWARN("Found nodemap config record without type field, "
713 " nodemap_id=%d. nodemap config file corrupt?\n",
716 case NODEMAP_CLUSTER_IDX: {
717 struct lu_nodemap *old_nm = NULL;
719 nodemap = cfs_hash_lookup(config->nmc_nodemap_hash,
721 if (nodemap == NULL) {
722 if (nodemap_id == LUSTRE_NODEMAP_DEFAULT_ID) {
723 nodemap = nodemap_create(rec->ncr.ncr_name,
725 config->nmc_default_nodemap = nodemap;
727 nodemap = nodemap_create(rec->ncr.ncr_name,
731 GOTO(out, rc = PTR_ERR(nodemap));
733 /* we need to override the local ID with the saved ID */
734 nodemap->nm_id = nodemap_id;
735 if (nodemap_id > config->nmc_nodemap_highest_id)
736 config->nmc_nodemap_highest_id = nodemap_id;
738 } else if (nodemap->nm_id != nodemap_id) {
739 nodemap_putref(nodemap);
740 GOTO(out, rc = -EINVAL);
743 nodemap->nm_squash_uid =
744 le32_to_cpu(rec->ncr.ncr_squash_uid);
745 nodemap->nm_squash_gid =
746 le32_to_cpu(rec->ncr.ncr_squash_gid);
748 flags = le32_to_cpu(rec->ncr.ncr_flags);
749 nodemap->nmf_allow_root_access =
750 flags & NM_FL_ALLOW_ROOT_ACCESS;
751 nodemap->nmf_trust_client_ids =
752 flags & NM_FL_TRUST_CLIENT_IDS;
753 nodemap->nmf_deny_unknown =
754 flags & NM_FL_DENY_UNKNOWN;
755 nodemap->nmf_map_uid_only =
756 flags & NM_FL_MAP_UID_ONLY;
757 nodemap->nmf_map_gid_only =
758 flags & NM_FL_MAP_GID_ONLY;
759 nodemap->nmf_enable_audit =
760 flags & NM_FL_ENABLE_AUDIT;
762 /* The fileset should be saved otherwise it will be empty
763 * every time in case of "NODEMAP_CLUSTER_IDX". */
764 mutex_lock(&active_config_lock);
765 old_nm = nodemap_lookup(rec->ncr.ncr_name);
766 if (!IS_ERR(old_nm) && old_nm->nm_fileset[0] != '\0')
767 strlcpy(nodemap->nm_fileset, old_nm->nm_fileset,
768 sizeof(nodemap->nm_fileset));
769 mutex_unlock(&active_config_lock);
771 nodemap_putref(old_nm);
773 if (*recent_nodemap == NULL) {
774 *recent_nodemap = nodemap;
775 INIT_LIST_HEAD(&nodemap->nm_list);
777 list_add(&nodemap->nm_list,
778 &(*recent_nodemap)->nm_list);
780 nodemap_putref(nodemap);
783 case NODEMAP_RANGE_IDX:
784 nid[0] = le64_to_cpu(rec->nrr.nrr_start_nid);
785 nid[1] = le64_to_cpu(rec->nrr.nrr_end_nid);
787 rc = nodemap_add_range_helper(config, nodemap, nid,
788 le32_to_cpu(key->nk_range_id));
792 case NODEMAP_UIDMAP_IDX:
793 case NODEMAP_GIDMAP_IDX:
794 map[0] = le32_to_cpu(key->nk_id_client);
795 map[1] = le32_to_cpu(rec->nir.nir_id_fs);
797 if (type == NODEMAP_UIDMAP_IDX)
798 id_type = NODEMAP_UID;
800 id_type = NODEMAP_GID;
802 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
806 case NODEMAP_GLOBAL_IDX:
807 config->nmc_nodemap_is_active = rec->ngr.ngr_is_active;
810 CERROR("got keyrec pair for unknown type %d\n", type);
822 enum nm_config_passes {
823 NM_READ_CLUSTERS = 0,
824 NM_READ_ATTRIBUTES = 1,
827 static int nodemap_load_entries(const struct lu_env *env,
828 struct dt_object *nodemap_idx)
830 const struct dt_it_ops *iops;
832 struct lu_nodemap *recent_nodemap = NULL;
833 struct nodemap_config *new_config = NULL;
835 bool activate_nodemap = false;
836 bool loaded_global_idx = false;
837 enum nm_config_passes cur_pass = NM_READ_CLUSTERS;
842 iops = &nodemap_idx->do_index_ops->dio_it;
844 dt_read_lock(env, nodemap_idx, 0);
845 it = iops->init(env, nodemap_idx, 0);
847 GOTO(out, rc = PTR_ERR(it));
849 rc = iops->load(env, it, hash);
851 GOTO(out_iops_fini, rc);
853 /* rc == 0 means we need to advance to record */
855 rc = iops->next(env, it);
858 GOTO(out_iops_put, rc);
859 /* rc > 0 is eof, will be checked in while below */
861 /* rc == 1, we found initial record and can process below */
865 new_config = nodemap_config_alloc();
866 if (IS_ERR(new_config)) {
867 rc = PTR_ERR(new_config);
869 GOTO(out_iops_put, rc);
872 /* rc > 0 is eof, check initial iops->next here as well */
874 struct nodemap_key *key;
875 union nodemap_rec rec;
876 enum nodemap_idx_type key_type;
878 key = (struct nodemap_key *)iops->key(env, it);
879 key_type = nodemap_get_key_type((struct nodemap_key *)key);
880 if ((cur_pass == NM_READ_CLUSTERS &&
881 key_type == NODEMAP_CLUSTER_IDX) ||
882 (cur_pass == NM_READ_ATTRIBUTES &&
883 key_type != NODEMAP_CLUSTER_IDX &&
884 key_type != NODEMAP_EMPTY_IDX)) {
885 rc = iops->rec(env, it, (struct dt_rec *)&rec, 0);
888 GOTO(out_nodemap_config, rc);
889 rc = nodemap_process_keyrec(new_config, key, &rec,
892 GOTO(out_nodemap_config, rc);
893 if (rc == NODEMAP_GLOBAL_IDX)
894 loaded_global_idx = true;
899 rc = iops->next(env, it);
900 while (rc == -ESTALE);
902 /* move to second pass */
903 if (rc > 0 && cur_pass == NM_READ_CLUSTERS) {
904 cur_pass = NM_READ_ATTRIBUTES;
905 rc = iops->load(env, it, 0);
907 rc = iops->next(env, it);
920 nodemap_config_dealloc(new_config);
922 /* creating new default needs to be done outside dt read lock */
923 activate_nodemap = true;
929 dt_read_unlock(env, nodemap_idx);
932 CWARN("%s: failed to load nodemap configuration: rc = %d\n",
933 nodemap_idx->do_lu.lo_dev->ld_obd->obd_name, rc);
935 if (!activate_nodemap)
938 if (new_config->nmc_default_nodemap == NULL) {
939 /* new MGS won't have a default nm on disk, so create it here */
940 new_config->nmc_default_nodemap =
941 nodemap_create(DEFAULT_NODEMAP, new_config, 1);
942 if (IS_ERR(new_config->nmc_default_nodemap)) {
943 rc = PTR_ERR(new_config->nmc_default_nodemap);
945 rc = nodemap_idx_nodemap_add_update(
946 new_config->nmc_default_nodemap,
949 nodemap_putref(new_config->nmc_default_nodemap);
953 /* new nodemap config won't have an active/inactive record */
954 if (rc == 0 && loaded_global_idx == false) {
955 struct nodemap_key nk;
956 union nodemap_rec nr;
958 nodemap_global_key_init(&nk);
959 nodemap_global_rec_init(&nr, false);
960 rc = nodemap_idx_insert(env, nodemap_idx, &nk, &nr);
964 nodemap_config_set_active(new_config);
966 nodemap_config_dealloc(new_config);
972 * Step through active config and write to disk.
974 struct dt_object *nodemap_save_config_cache(const struct lu_env *env,
975 struct dt_device *dev,
976 struct local_oid_storage *los)
979 struct lu_nodemap *nodemap;
980 struct lu_nodemap *nm_tmp;
981 struct lu_nid_range *range;
982 struct lu_nid_range *range_temp;
983 struct lu_idmap *idmap;
984 struct lu_idmap *id_tmp;
986 struct nodemap_key nk;
987 union nodemap_rec nr;
988 LIST_HEAD(nodemap_list_head);
993 /* create a new index file to fill with active config */
994 o = nodemap_cache_find_create(env, dev, los, NCFC_CREATE_NEW);
998 mutex_lock(&active_config_lock);
1000 /* convert hash to list so we don't spin */
1001 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1002 nm_hash_list_cb, &nodemap_list_head);
1004 list_for_each_entry_safe(nodemap, nm_tmp, &nodemap_list_head, nm_list) {
1005 nodemap_cluster_key_init(&nk, nodemap->nm_id);
1006 nodemap_cluster_rec_init(&nr, nodemap);
1008 rc2 = nodemap_idx_insert(env, o, &nk, &nr);
1014 down_read(&active_config->nmc_range_tree_lock);
1015 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1017 lnet_nid_t nid[2] = {
1018 range->rn_node.in_extent.start,
1019 range->rn_node.in_extent.end
1021 nodemap_range_key_init(&nk, nodemap->nm_id,
1023 nodemap_range_rec_init(&nr, nid);
1024 rc2 = nodemap_idx_insert(env, o, &nk, &nr);
1028 up_read(&active_config->nmc_range_tree_lock);
1030 /* we don't need to take nm_idmap_lock because active config
1031 * lock prevents changes from happening to nodemaps
1033 root = nodemap->nm_client_to_fs_uidmap;
1034 nm_rbtree_postorder_for_each_entry_safe(idmap, id_tmp, &root,
1036 nodemap_idmap_key_init(&nk, nodemap->nm_id, NODEMAP_UID,
1038 nodemap_idmap_rec_init(&nr, idmap->id_fs);
1039 rc2 = nodemap_idx_insert(env, o, &nk, &nr);
1044 root = nodemap->nm_client_to_fs_gidmap;
1045 nm_rbtree_postorder_for_each_entry_safe(idmap, id_tmp, &root,
1047 nodemap_idmap_key_init(&nk, nodemap->nm_id, NODEMAP_GID,
1049 nodemap_idmap_rec_init(&nr, idmap->id_fs);
1050 rc2 = nodemap_idx_insert(env, o, &nk, &nr);
1055 nodemap_global_key_init(&nk);
1056 nodemap_global_rec_init(&nr, active_config->nmc_nodemap_is_active);
1057 rc2 = nodemap_idx_insert(env, o, &nk, &nr);
1061 mutex_unlock(&active_config_lock);
1064 dt_object_put(env, o);
1071 static void nodemap_save_all_caches(void)
1073 struct nm_config_file *ncf;
1077 /* recreating nodemap cache requires fld_thread_key be in env */
1078 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD | LCT_MG_THREAD);
1080 CWARN("cannot init env for nodemap config: rc = %d\n", rc);
1084 mutex_lock(&ncf_list_lock);
1085 list_for_each_entry(ncf, &ncf_list_head, ncf_list) {
1086 struct dt_device *dev = lu2dt_dev(ncf->ncf_obj->do_lu.lo_dev);
1087 struct obd_device *obd = ncf->ncf_obj->do_lu.lo_dev->ld_obd;
1088 struct dt_object *o;
1090 /* put current config file so save conf can rewrite it */
1091 dt_object_put_nocache(&env, ncf->ncf_obj);
1092 ncf->ncf_obj = NULL;
1094 o = nodemap_save_config_cache(&env, dev, ncf->ncf_los);
1096 CWARN("%s: error writing to nodemap config: rc = %d\n",
1101 mutex_unlock(&ncf_list_lock);
1106 /* tracks if config still needs to be loaded, either from disk or network */
1107 static bool nodemap_config_loaded;
1108 static DEFINE_MUTEX(nodemap_config_loaded_lock);
1111 * Ensures that configs loaded over the wire are prioritized over those loaded
1114 * \param config config to set as the active config
1116 void nodemap_config_set_active_mgc(struct nodemap_config *config)
1118 mutex_lock(&nodemap_config_loaded_lock);
1119 nodemap_config_set_active(config);
1120 nodemap_config_loaded = true;
1121 nodemap_save_all_caches();
1122 mutex_unlock(&nodemap_config_loaded_lock);
1124 EXPORT_SYMBOL(nodemap_config_set_active_mgc);
1127 * Register a dt_object representing the config index file. This should be
1128 * called by targets in order to load the nodemap configuration from disk. The
1129 * dt_object should be created with local_index_find_or_create and the index
1130 * features should be enabled with do_index_try.
1132 * \param obj dt_object returned by local_index_find_or_create
1134 * \retval on success: nm_config_file handle for later deregistration
1135 * \retval -ENOMEM memory allocation failure
1136 * \retval -ENOENT error loading nodemap config
1137 * \retval -EINVAL error loading nodemap config
1138 * \retval -EEXIST nodemap config already registered for MGS
1140 struct nm_config_file *nm_config_file_register_mgs(const struct lu_env *env,
1141 struct dt_object *obj,
1142 struct local_oid_storage *los)
1144 struct nm_config_file *ncf;
1148 if (nodemap_mgs_ncf != NULL)
1149 GOTO(out, ncf = ERR_PTR(-EEXIST));
1153 GOTO(out, ncf = ERR_PTR(-ENOMEM));
1155 /* if loading from cache, prevent activation of MGS config until cache
1156 * loading is done, so disk config is overwritten by MGS config.
1158 mutex_lock(&nodemap_config_loaded_lock);
1159 rc = nodemap_load_entries(env, obj);
1161 nodemap_config_loaded = true;
1162 mutex_unlock(&nodemap_config_loaded_lock);
1166 GOTO(out, ncf = ERR_PTR(rc));
1169 lu_object_get(&obj->do_lu);
1174 nodemap_mgs_ncf = ncf;
1179 EXPORT_SYMBOL(nm_config_file_register_mgs);
1181 struct nm_config_file *nm_config_file_register_tgt(const struct lu_env *env,
1182 struct dt_device *dev,
1183 struct local_oid_storage *los)
1185 struct nm_config_file *ncf;
1186 struct dt_object *config_obj = NULL;
1191 RETURN(ERR_PTR(-ENOMEM));
1193 /* don't load from cache if config already loaded */
1194 mutex_lock(&nodemap_config_loaded_lock);
1195 if (!nodemap_config_loaded) {
1196 config_obj = nodemap_cache_find_create(env, dev, los, 0);
1197 if (IS_ERR(config_obj))
1198 rc = PTR_ERR(config_obj);
1200 rc = nodemap_load_entries(env, config_obj);
1203 nodemap_config_loaded = true;
1205 mutex_unlock(&nodemap_config_loaded_lock);
1209 /* sync on disk caches w/ loaded config in memory, ncf_obj may change */
1211 config_obj = nodemap_save_config_cache(env, dev, los);
1212 if (IS_ERR(config_obj))
1213 GOTO(out_ncf, rc = PTR_ERR(config_obj));
1216 ncf->ncf_obj = config_obj;
1219 mutex_lock(&ncf_list_lock);
1220 list_add(&ncf->ncf_list, &ncf_list_head);
1221 mutex_unlock(&ncf_list_lock);
1226 RETURN(ERR_PTR(rc));
1231 EXPORT_SYMBOL(nm_config_file_register_tgt);
1234 * Deregister a nm_config_file. Should be called by targets during cleanup.
1236 * \param ncf config file to deregister
1238 void nm_config_file_deregister_mgs(const struct lu_env *env,
1239 struct nm_config_file *ncf)
1242 LASSERT(nodemap_mgs_ncf == ncf);
1244 nodemap_mgs_ncf = NULL;
1246 dt_object_put(env, ncf->ncf_obj);
1252 EXPORT_SYMBOL(nm_config_file_deregister_mgs);
1254 void nm_config_file_deregister_tgt(const struct lu_env *env,
1255 struct nm_config_file *ncf)
1262 mutex_lock(&ncf_list_lock);
1263 list_del(&ncf->ncf_list);
1264 mutex_unlock(&ncf_list_lock);
1267 dt_object_put(env, ncf->ncf_obj);
1273 EXPORT_SYMBOL(nm_config_file_deregister_tgt);
1275 int nodemap_process_idx_pages(struct nodemap_config *config, union lu_page *lip,
1276 struct lu_nodemap **recent_nodemap)
1278 struct nodemap_key *key;
1279 union nodemap_rec *rec;
1284 int size = dt_nodemap_features.dif_keysize_max +
1285 dt_nodemap_features.dif_recsize_max;
1288 for (j = 0; j < LU_PAGE_COUNT; j++) {
1289 if (lip->lp_idx.lip_magic != LIP_MAGIC)
1292 /* get and process keys and records from page */
1293 for (k = 0; k < lip->lp_idx.lip_nr; k++) {
1294 entry = lip->lp_idx.lip_entries + k * size;
1295 key = (struct nodemap_key *)entry;
1297 entry += dt_nodemap_features.dif_keysize_max;
1298 rec = (union nodemap_rec *)entry;
1300 rc = nodemap_process_keyrec(config, key, rec,
1311 EXPORT_SYMBOL(nodemap_process_idx_pages);
1313 static int nodemap_page_build(const struct lu_env *env, union lu_page *lp,
1314 size_t nob, const struct dt_it_ops *iops,
1315 struct dt_it *it, __u32 attr, void *arg)
1317 struct idx_info *ii = (struct idx_info *)arg;
1318 struct lu_idxpage *lip = &lp->lp_idx;
1320 size_t size = ii->ii_keysize + ii->ii_recsize;
1324 if (nob < LIP_HDR_SIZE)
1327 /* initialize the header of the new container */
1328 memset(lip, 0, LIP_HDR_SIZE);
1329 lip->lip_magic = LIP_MAGIC;
1330 nob -= LIP_HDR_SIZE;
1332 entry = lip->lip_entries;
1334 char *tmp_entry = entry;
1337 enum nodemap_idx_type key_type;
1339 /* fetch 64-bit hash value */
1340 hash = iops->store(env, it);
1341 ii->ii_hash_end = hash;
1343 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_IDX_READ_BREAK)) {
1344 if (lip->lip_nr != 0)
1349 if (lip->lip_nr == 0)
1350 GOTO(out, rc = -EINVAL);
1354 key = iops->key(env, it);
1355 key_type = nodemap_get_key_type((struct nodemap_key *)key);
1357 /* on the first pass, get only the cluster types. On second
1358 * pass, get all the rest */
1359 if ((ii->ii_attrs == NM_READ_CLUSTERS &&
1360 key_type == NODEMAP_CLUSTER_IDX) ||
1361 (ii->ii_attrs == NM_READ_ATTRIBUTES &&
1362 key_type != NODEMAP_CLUSTER_IDX &&
1363 key_type != NODEMAP_EMPTY_IDX)) {
1364 memcpy(tmp_entry, key, ii->ii_keysize);
1365 tmp_entry += ii->ii_keysize;
1367 /* and finally the record */
1368 rc = iops->rec(env, it, (struct dt_rec *)tmp_entry,
1370 if (rc != -ESTALE) {
1374 /* hash/key/record successfully copied! */
1376 if (unlikely(lip->lip_nr == 1 &&
1378 ii->ii_hash_start = hash;
1380 entry = tmp_entry + ii->ii_recsize;
1385 /* move on to the next record */
1387 rc = iops->next(env, it);
1388 } while (rc == -ESTALE);
1390 /* move to second pass */
1391 if (rc > 0 && ii->ii_attrs == NM_READ_CLUSTERS) {
1392 ii->ii_attrs = NM_READ_ATTRIBUTES;
1393 rc = iops->load(env, it, 0);
1395 rc = iops->next(env, it);
1406 if (rc >= 0 && lip->lip_nr > 0)
1407 /* one more container */
1410 /* no more entries */
1411 ii->ii_hash_end = II_END_OFF;
1416 int nodemap_index_read(struct lu_env *env,
1417 struct nm_config_file *ncf,
1418 struct idx_info *ii,
1419 const struct lu_rdpg *rdpg)
1421 struct dt_object *nodemap_idx = ncf->ncf_obj;
1425 ii->ii_keysize = dt_nodemap_features.dif_keysize_max;
1426 ii->ii_recsize = dt_nodemap_features.dif_recsize_max;
1428 dt_read_lock(env, nodemap_idx, 0);
1429 version = dt_version_get(env, nodemap_idx);
1430 if (rdpg->rp_hash != 0 && ii->ii_version != version) {
1431 CDEBUG(D_INFO, "nodemap config changed inflight, old %llu, new %llu\n",
1434 ii->ii_hash_end = 0;
1436 rc = dt_index_walk(env, nodemap_idx, rdpg, nodemap_page_build,
1438 CDEBUG(D_INFO, "walked index, hashend %llx\n", ii->ii_hash_end);
1442 ii->ii_version = version;
1444 dt_read_unlock(env, nodemap_idx);
1447 EXPORT_SYMBOL(nodemap_index_read);
1450 * Returns the current nodemap configuration to MGC by walking the nodemap
1451 * config index and storing it in the response buffer.
1453 * \param req incoming MGS_CONFIG_READ request
1455 * \retval -EINVAL malformed request
1456 * \retval -ENOTCONN client evicted/reconnected already
1457 * \retval -ETIMEDOUT client timeout or network error
1460 int nodemap_get_config_req(struct obd_device *mgs_obd,
1461 struct ptlrpc_request *req)
1463 struct mgs_config_body *body;
1464 struct mgs_config_res *res;
1465 struct lu_rdpg rdpg;
1466 struct idx_info nodemap_ii;
1467 struct ptlrpc_bulk_desc *desc;
1468 struct l_wait_info lwi;
1469 struct tg_export_data *rqexp_ted = &req->rq_export->exp_target_data;
1475 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1479 if (body->mcb_type != CONFIG_T_NODEMAP)
1482 rdpg.rp_count = (body->mcb_units << body->mcb_bits);
1483 rdpg.rp_npages = (rdpg.rp_count + PAGE_SIZE - 1) >>
1485 if (rdpg.rp_npages > PTLRPC_MAX_BRW_PAGES)
1488 CDEBUG(D_INFO, "reading nodemap log, name '%s', size = %u\n",
1489 body->mcb_name, rdpg.rp_count);
1491 /* allocate pages to store the containers */
1492 OBD_ALLOC(rdpg.rp_pages, sizeof(*rdpg.rp_pages) * rdpg.rp_npages);
1493 if (rdpg.rp_pages == NULL)
1495 for (i = 0; i < rdpg.rp_npages; i++) {
1496 rdpg.rp_pages[i] = alloc_page(GFP_NOFS);
1497 if (rdpg.rp_pages[i] == NULL)
1498 GOTO(out, rc = -ENOMEM);
1501 rdpg.rp_hash = body->mcb_offset;
1502 nodemap_ii.ii_magic = IDX_INFO_MAGIC;
1503 nodemap_ii.ii_flags = II_FL_NOHASH;
1504 nodemap_ii.ii_version = rqexp_ted->ted_nodemap_version;
1505 nodemap_ii.ii_attrs = body->mcb_nm_cur_pass;
1507 bytes = nodemap_index_read(req->rq_svc_thread->t_env,
1508 mgs_obd->u.obt.obt_nodemap_config_file,
1509 &nodemap_ii, &rdpg);
1511 GOTO(out, rc = bytes);
1513 rqexp_ted->ted_nodemap_version = nodemap_ii.ii_version;
1515 res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1517 GOTO(out, rc = -EINVAL);
1518 res->mcr_offset = nodemap_ii.ii_hash_end;
1519 res->mcr_nm_cur_pass = nodemap_ii.ii_attrs;
1521 page_count = (bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
1522 LASSERT(page_count <= rdpg.rp_count);
1523 desc = ptlrpc_prep_bulk_exp(req, page_count, 1,
1524 PTLRPC_BULK_PUT_SOURCE |
1525 PTLRPC_BULK_BUF_KIOV,
1527 &ptlrpc_bulk_kiov_pin_ops);
1529 GOTO(out, rc = -ENOMEM);
1531 for (i = 0; i < page_count && bytes > 0; i++) {
1532 ptlrpc_prep_bulk_page_pin(desc, rdpg.rp_pages[i], 0,
1533 min_t(int, bytes, PAGE_SIZE));
1537 rc = target_bulk_io(req->rq_export, desc, &lwi);
1538 ptlrpc_free_bulk(desc);
1541 if (rdpg.rp_pages != NULL) {
1542 for (i = 0; i < rdpg.rp_npages; i++)
1543 if (rdpg.rp_pages[i] != NULL)
1544 __free_page(rdpg.rp_pages[i]);
1545 OBD_FREE(rdpg.rp_pages,
1546 rdpg.rp_npages * sizeof(rdpg.rp_pages[0]));
1550 EXPORT_SYMBOL(nodemap_get_config_req);