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) 2013, Trustees of Indiana University
25 * Copyright (c) 2017, Intel Corporation.
27 * Author: Joshua Walgenbach <jjw@iu.edu>
29 #include <linux/module.h>
30 #include <linux/sort.h>
31 #include <uapi/linux/lnet/nidstr.h>
32 #include <lustre_net.h>
33 #include <lustre_acl.h>
34 #include <obd_class.h>
35 #include "nodemap_internal.h"
37 #define HASH_NODEMAP_BKT_BITS 3
38 #define HASH_NODEMAP_CUR_BITS 3
39 #define HASH_NODEMAP_MAX_BITS 7
41 #define DEFAULT_NODEMAP "default"
43 /* nodemap proc root proc directory under fs/lustre */
44 struct proc_dir_entry *proc_lustre_nodemap_root;
46 /* Copy of config active flag to avoid locking in mapping functions */
49 /* Lock protecting the active config, useful primarily when proc and
50 * nodemap_hash might be replaced when loading a new config
51 * Any time the active config is referenced, the lock should be held.
53 DEFINE_MUTEX(active_config_lock);
54 struct nodemap_config *active_config;
59 * \param nodemap nodemap to destroy
61 static void nodemap_destroy(struct lu_nodemap *nodemap)
65 if (nodemap->nm_pde_data != NULL)
66 lprocfs_nodemap_remove(nodemap->nm_pde_data);
68 mutex_lock(&active_config_lock);
69 down_read(&active_config->nmc_range_tree_lock);
70 nm_member_reclassify_nodemap(nodemap);
71 up_read(&active_config->nmc_range_tree_lock);
73 down_write(&nodemap->nm_idmap_lock);
74 idmap_delete_tree(nodemap);
75 up_write(&nodemap->nm_idmap_lock);
77 mutex_unlock(&active_config_lock);
79 if (!list_empty(&nodemap->nm_member_list))
80 CWARN("nodemap_destroy failed to reclassify all members\n");
82 nm_member_delete_list(nodemap);
84 OBD_FREE_PTR(nodemap);
90 * Functions used for the cfs_hash
92 void nodemap_getref(struct lu_nodemap *nodemap)
94 atomic_inc(&nodemap->nm_refcount);
95 CDEBUG(D_INFO, "GETting nodemap %s(p=%p) : new refcount %d\n",
96 nodemap->nm_name, nodemap, atomic_read(&nodemap->nm_refcount));
100 * Destroy nodemap if last reference is put. Should be called outside
103 void nodemap_putref(struct lu_nodemap *nodemap)
108 LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
110 CDEBUG(D_INFO, "PUTting nodemap %s(p=%p) : new refcount %d\n",
111 nodemap->nm_name, nodemap,
112 atomic_read(&nodemap->nm_refcount) - 1);
114 if (atomic_dec_and_test(&nodemap->nm_refcount))
115 nodemap_destroy(nodemap);
117 EXPORT_SYMBOL(nodemap_putref);
119 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
120 const void *key, unsigned mask)
122 return cfs_hash_djb2_hash(key, strlen(key), mask);
125 static void *nodemap_hs_key(struct hlist_node *hnode)
127 struct lu_nodemap *nodemap;
129 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
131 return nodemap->nm_name;
134 static int nodemap_hs_keycmp(const void *key,
135 struct hlist_node *compared_hnode)
139 nodemap_name = nodemap_hs_key(compared_hnode);
141 return !strcmp(key, nodemap_name);
144 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
146 return hlist_entry(hnode, struct lu_nodemap, nm_hash);
149 static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode)
151 struct lu_nodemap *nodemap;
153 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
154 nodemap_getref(nodemap);
157 static void nodemap_hs_put_locked(struct cfs_hash *hs,
158 struct hlist_node *hnode)
160 struct lu_nodemap *nodemap;
162 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
163 nodemap_putref(nodemap);
166 static struct cfs_hash_ops nodemap_hash_operations = {
167 .hs_hash = nodemap_hashfn,
168 .hs_key = nodemap_hs_key,
169 .hs_keycmp = nodemap_hs_keycmp,
170 .hs_object = nodemap_hs_hashobject,
171 .hs_get = nodemap_hs_get,
172 .hs_put_locked = nodemap_hs_put_locked,
175 /* end of cfs_hash functions */
178 * Initialize nodemap_hash
181 * \retval -ENOMEM cannot create hash
183 static int nodemap_init_hash(struct nodemap_config *nmc)
185 nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP",
186 HASH_NODEMAP_CUR_BITS,
187 HASH_NODEMAP_MAX_BITS,
188 HASH_NODEMAP_BKT_BITS, 0,
191 &nodemap_hash_operations,
194 if (nmc->nmc_nodemap_hash == NULL) {
195 CERROR("cannot create nodemap_hash table\n");
203 * Check for valid nodemap name
205 * \param name nodemap name
207 * \retval false invalid
209 static bool nodemap_name_is_valid(const char *name)
211 if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
215 for (; *name != '\0'; name++) {
216 if (!isalnum(*name) && *name != '_')
226 * Look nodemap up in the active_config nodemap hash. Caller should hold the
227 * active_config_lock.
229 * \param name name of nodemap
230 * \retval nodemap pointer set to found nodemap
231 * \retval -EINVAL name is not valid
232 * \retval -ENOENT nodemap not found
234 struct lu_nodemap *nodemap_lookup(const char *name)
236 struct lu_nodemap *nodemap = NULL;
238 if (!nodemap_name_is_valid(name))
239 return ERR_PTR(-EINVAL);
241 nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name);
243 return ERR_PTR(-ENOENT);
249 * Classify the nid into the proper nodemap. Caller must hold active config and
250 * nm_range_tree_lock, and call nodemap_putref when done with nodemap.
252 * \param nid nid to classify
253 * \retval nodemap nodemap containing the nid
254 * \retval default_nodemap default nodemap
255 * \retval -EINVAL LO nid given without other local nid
257 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
259 struct lu_nid_range *range;
260 struct lu_nodemap *nodemap;
265 /* don't use 0@lo, use the first non-lo local NID instead */
266 if (nid == LNET_NID_LO_0) {
267 struct lnet_process_id id;
271 rc = LNetGetId(i++, &id);
273 RETURN(ERR_PTR(-EINVAL));
274 } while (id.nid == LNET_NID_LO_0);
277 CDEBUG(D_INFO, "found nid %s\n", libcfs_nid2str(nid));
280 range = range_search(&active_config->nmc_range_tree, nid);
282 nodemap = range->rn_nodemap;
284 nodemap = active_config->nmc_default_nodemap;
286 LASSERT(nodemap != NULL);
287 nodemap_getref(nodemap);
293 * simple check for default nodemap
295 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
297 return nodemap->nm_id == 0;
301 * parse a nodemap range string into two nids
303 * \param range_str string to parse
304 * \param range[2] array of two nids
305 * \reyval 0 on success
307 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
309 char buf[LNET_NIDSTR_SIZE * 2 + 2];
315 snprintf(buf, sizeof(buf), "%s", range_str);
317 start_nidstr = strsep(&ptr, ":");
318 end_nidstr = strsep(&ptr, ":");
320 if (start_nidstr == NULL || end_nidstr == NULL)
321 GOTO(out, rc = -EINVAL);
323 range[0] = libcfs_str2nid(start_nidstr);
324 range[1] = libcfs_str2nid(end_nidstr);
330 EXPORT_SYMBOL(nodemap_parse_range);
333 * parse a string containing an id map of form "client_id:filesystem_id"
334 * into an array of __u32 * for use in mapping functions
336 * \param idmap_str map string
337 * \param idmap array[2] of __u32
339 * \retval 0 on success
340 * \retval -EINVAL if idmap cannot be parsed
342 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
345 long unsigned int idmap_buf;
348 if (idmap_str == NULL)
351 sep = strchr(idmap_str, ':');
357 rc = kstrtoul(idmap_str, 10, &idmap_buf);
360 idmap[0] = idmap_buf;
362 rc = kstrtoul(sep, 10, &idmap_buf);
365 idmap[1] = idmap_buf;
369 EXPORT_SYMBOL(nodemap_parse_idmap);
372 * add a member to a nodemap
374 * \param nid nid to add to the members
375 * \param exp obd_export structure for the connection
376 * that is being added
377 * \retval -EINVAL export is NULL, or has invalid NID
378 * \retval -EEXIST export is already member of a nodemap
380 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
382 struct lu_nodemap *nodemap;
386 mutex_lock(&active_config_lock);
387 down_read(&active_config->nmc_range_tree_lock);
389 nodemap = nodemap_classify_nid(nid);
391 if (IS_ERR(nodemap)) {
392 CWARN("%s: error adding to nodemap, no valid NIDs found\n",
393 exp->exp_obd->obd_name);
396 rc = nm_member_add(nodemap, exp);
399 up_read(&active_config->nmc_range_tree_lock);
400 mutex_unlock(&active_config_lock);
402 if (!IS_ERR(nodemap))
403 nodemap_putref(nodemap);
407 EXPORT_SYMBOL(nodemap_add_member);
410 * delete a member from a nodemap
412 * \param exp export to remove from a nodemap
414 void nodemap_del_member(struct obd_export *exp)
416 struct lu_nodemap *nodemap;
420 /* using ac lock to prevent nodemap reclassification while deleting */
421 mutex_lock(&active_config_lock);
423 /* use of ted_nodemap is protected by active_config_lock. we take an
424 * extra reference to make sure nodemap isn't destroyed under
427 nodemap = exp->exp_target_data.ted_nodemap;
431 nodemap_getref(nodemap);
433 mutex_lock(&nodemap->nm_member_list_lock);
434 nm_member_del(nodemap, exp);
435 mutex_unlock(&nodemap->nm_member_list_lock);
438 mutex_unlock(&active_config_lock);
441 nodemap_putref(nodemap);
445 EXPORT_SYMBOL(nodemap_del_member);
448 * add an idmap to the proper nodemap trees
450 * \param nodemap nodemap to add idmap to
451 * \param id_type NODEMAP_UID or NODEMAP_GID
452 * \param map array[2] __u32 containing the map values
453 * map[0] is client id
454 * map[1] is the filesystem id
456 * \retval 0 on success
457 * \retval < 0 if error occurs
459 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
460 enum nodemap_id_type id_type,
463 struct lu_idmap *idmap;
464 struct lu_idmap *temp;
467 idmap = idmap_create(map[0], map[1]);
469 GOTO(out, rc = -ENOMEM);
471 down_write(&nodemap->nm_idmap_lock);
472 temp = idmap_insert(id_type, idmap, nodemap);
473 /* If the new id_client or id_fs is matched, the old idmap and its
474 * index should be deleted according to its id_client before the new
475 * idmap is added again.
478 GOTO(out_insert, rc = PTR_ERR(temp));
482 del_map[0] = temp->id_client;
483 idmap_delete(id_type, temp, nodemap);
484 rc = nodemap_idx_idmap_del(nodemap, id_type, del_map);
485 /* In case there is any corrupted idmap */
486 if (!rc || unlikely(rc == -ENOENT)) {
487 temp = idmap_insert(id_type, idmap, nodemap);
499 up_write(&nodemap->nm_idmap_lock);
500 nm_member_revoke_locks(nodemap);
506 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
509 struct lu_nodemap *nodemap = NULL;
514 mutex_lock(&active_config_lock);
515 nodemap = nodemap_lookup(name);
516 if (IS_ERR(nodemap)) {
517 mutex_unlock(&active_config_lock);
518 GOTO(out, rc = PTR_ERR(nodemap));
521 if (is_default_nodemap(nodemap)) {
524 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
526 rc = nodemap_idx_idmap_add(nodemap, id_type, map);
528 mutex_unlock(&active_config_lock);
529 nodemap_putref(nodemap);
534 EXPORT_SYMBOL(nodemap_add_idmap);
537 * delete idmap from proper nodemap tree
539 * \param name name of nodemap
540 * \param id_type NODEMAP_UID or NODEMAP_GID
541 * \param map array[2] __u32 containing the mapA values
542 * map[0] is client id
543 * map[1] is the filesystem id
545 * \retval 0 on success
547 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
550 struct lu_nodemap *nodemap = NULL;
551 struct lu_idmap *idmap = NULL;
556 mutex_lock(&active_config_lock);
557 nodemap = nodemap_lookup(name);
558 if (IS_ERR(nodemap)) {
559 mutex_unlock(&active_config_lock);
560 GOTO(out, rc = PTR_ERR(nodemap));
563 if (is_default_nodemap(nodemap))
564 GOTO(out_putref, rc = -EINVAL);
566 down_write(&nodemap->nm_idmap_lock);
567 idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
572 idmap_delete(id_type, idmap, nodemap);
573 rc = nodemap_idx_idmap_del(nodemap, id_type, map);
575 up_write(&nodemap->nm_idmap_lock);
578 mutex_unlock(&active_config_lock);
580 nm_member_revoke_locks(nodemap);
581 nodemap_putref(nodemap);
586 EXPORT_SYMBOL(nodemap_del_idmap);
589 * Get nodemap assigned to given export. Takes a reference on the nodemap.
591 * Note that this function may return either NULL, or an ERR_PTR()
592 * or a valid nodemap pointer. All of the functions accessing the
593 * returned nodemap can check IS_ERR(nodemap) to see if an error is
594 * returned. NULL is not considered an error, which is OK since this
595 * is a valid case if nodemap are not in use. All nodemap handling
596 * functions must check for nodemap == NULL and do nothing, and the
597 * nodemap returned from this function should not be dereferenced.
599 * \param export export to get nodemap for
601 * \retval pointer to nodemap on success
602 * \retval NULL nodemap subsystem disabled
603 * \retval -EACCES export does not have nodemap assigned
605 struct lu_nodemap *nodemap_get_from_exp(struct obd_export *exp)
607 struct lu_nodemap *nodemap;
614 spin_lock(&exp->exp_target_data.ted_nodemap_lock);
615 nodemap = exp->exp_target_data.ted_nodemap;
617 nodemap_getref(nodemap);
618 spin_unlock(&exp->exp_target_data.ted_nodemap_lock);
621 CDEBUG(D_INFO, "%s: nodemap null on export %s (at %s)\n",
622 exp->exp_obd->obd_name,
623 obd_uuid2str(&exp->exp_client_uuid),
624 obd_export_nid2str(exp));
625 RETURN(ERR_PTR(-EACCES));
630 EXPORT_SYMBOL(nodemap_get_from_exp);
633 * mapping function for nodemap idmaps
635 * \param nodemap lu_nodemap structure defining nodemap
636 * \param node_type NODEMAP_UID or NODEMAP_GID
637 * \param tree_type NODEMAP_CLIENT_TO_FS or
638 * NODEMAP_FS_TO_CLIENT
639 * \param id id to map
641 * \retval mapped id according to the rules below.
643 * if the nodemap_active is false, just return the passed id without mapping
645 * if the id to be looked up is 0, check that root access is allowed and if it
646 * is, return 0. Otherwise, return the squash uid or gid.
648 * if the nodemap is configured to trusted the ids from the client system, just
649 * return the passed id without mapping.
651 * if by this point, we haven't returned and the nodemap in question is the
652 * default nodemap, return the squash uid or gid.
654 * after these checks, search the proper tree for the mapping, and if found
655 * return the mapped value, otherwise return the squash uid or gid.
657 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
658 enum nodemap_id_type id_type,
659 enum nodemap_tree_type tree_type, __u32 id)
661 struct lu_idmap *idmap = NULL;
669 if (unlikely(nodemap == NULL))
672 if (nodemap->nmf_map_uid_only && id_type == NODEMAP_GID)
675 if (nodemap->nmf_map_gid_only && id_type == NODEMAP_UID)
679 if (nodemap->nmf_allow_root_access)
685 if (nodemap->nmf_trust_client_ids)
688 if (is_default_nodemap(nodemap))
691 down_read(&nodemap->nm_idmap_lock);
692 idmap = idmap_search(nodemap, tree_type, id_type, id);
694 up_read(&nodemap->nm_idmap_lock);
698 if (tree_type == NODEMAP_FS_TO_CLIENT)
699 found_id = idmap->id_client;
701 found_id = idmap->id_fs;
702 up_read(&nodemap->nm_idmap_lock);
706 if (id_type == NODEMAP_UID)
707 RETURN(nodemap->nm_squash_uid);
709 RETURN(nodemap->nm_squash_gid);
713 EXPORT_SYMBOL(nodemap_map_id);
716 * Map posix ACL entries according to the nodemap membership. Removes any
719 * \param lu_nodemap nodemap
720 * \param buf buffer containing xattr encoded ACLs
721 * \param size size of ACLs in bytes
722 * \param tree_type direction of mapping
723 * \retval size new size of ACLs in bytes
724 * \retval -EINVAL bad \a size param, see posix_acl_xattr_count()
726 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
727 enum nodemap_tree_type tree_type)
729 posix_acl_xattr_header *header = buf;
730 posix_acl_xattr_entry *entry = GET_POSIX_ACL_XATTR_ENTRY(header);
731 posix_acl_xattr_entry *new_entry = entry;
732 posix_acl_xattr_entry *end;
740 if (unlikely(nodemap == NULL))
743 count = posix_acl_xattr_count(size);
749 for (end = entry + count; entry != end; entry++) {
750 __u16 tag = le16_to_cpu(entry->e_tag);
751 __u32 id = le32_to_cpu(entry->e_id);
755 id = nodemap_map_id(nodemap, NODEMAP_UID,
757 if (id == nodemap->nm_squash_uid)
759 entry->e_id = cpu_to_le32(id);
762 id = nodemap_map_id(nodemap, NODEMAP_GID,
764 if (id == nodemap->nm_squash_gid)
766 entry->e_id = cpu_to_le32(id);
770 /* if we skip an ACL, copy the following ones over it */
771 if (new_entry != entry)
777 RETURN((void *)new_entry - (void *)header);
779 EXPORT_SYMBOL(nodemap_map_acl);
782 * Add nid range to given nodemap
784 * \param config nodemap config to work on
785 * \param nodemap nodemap to add range to
786 * \param nid nid range to add
787 * \param range_id should be 0 unless loading from disk
792 int nodemap_add_range_helper(struct nodemap_config *config,
793 struct lu_nodemap *nodemap,
794 const lnet_nid_t nid[2],
795 unsigned int range_id)
797 struct lu_nid_range *range;
800 down_write(&config->nmc_range_tree_lock);
801 range = range_create(&config->nmc_range_tree, nid[0], nid[1],
804 up_write(&config->nmc_range_tree_lock);
805 GOTO(out, rc = -ENOMEM);
808 rc = range_insert(&config->nmc_range_tree, range);
810 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
811 nodemap->nm_name, rc);
812 up_write(&config->nmc_range_tree_lock);
813 list_del(&range->rn_list);
814 range_destroy(range);
815 GOTO(out, rc = -ENOMEM);
818 list_add(&range->rn_list, &nodemap->nm_ranges);
820 /* nodemaps have no members if they aren't on the active config */
821 if (config == active_config)
822 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
824 up_write(&config->nmc_range_tree_lock);
826 /* if range_id is non-zero, we are loading from disk */
828 rc = nodemap_idx_range_add(range, nid);
830 if (config == active_config) {
831 nm_member_revoke_locks(config->nmc_default_nodemap);
832 nm_member_revoke_locks(nodemap);
838 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
840 struct lu_nodemap *nodemap = NULL;
843 mutex_lock(&active_config_lock);
844 nodemap = nodemap_lookup(name);
845 if (IS_ERR(nodemap)) {
846 mutex_unlock(&active_config_lock);
847 GOTO(out, rc = PTR_ERR(nodemap));
850 if (is_default_nodemap(nodemap))
853 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
854 mutex_unlock(&active_config_lock);
855 nodemap_putref(nodemap);
859 EXPORT_SYMBOL(nodemap_add_range);
863 * \param name nodemap name
864 * \param nid nid range
865 * \retval 0 on success
867 * Delete range from global range tree, and remove it
868 * from the list in the associated nodemap.
870 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
872 struct lu_nodemap *nodemap;
873 struct lu_nid_range *range;
876 mutex_lock(&active_config_lock);
877 nodemap = nodemap_lookup(name);
878 if (IS_ERR(nodemap)) {
879 mutex_unlock(&active_config_lock);
880 GOTO(out, rc = PTR_ERR(nodemap));
883 if (is_default_nodemap(nodemap))
884 GOTO(out_putref, rc = -EINVAL);
886 down_write(&active_config->nmc_range_tree_lock);
887 range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
889 up_write(&active_config->nmc_range_tree_lock);
890 GOTO(out_putref, rc = -EINVAL);
892 if (range->rn_nodemap != nodemap) {
893 up_write(&active_config->nmc_range_tree_lock);
894 GOTO(out_putref, rc = -EINVAL);
896 rc = nodemap_idx_range_del(range);
897 range_delete(&active_config->nmc_range_tree, range);
898 nm_member_reclassify_nodemap(nodemap);
899 up_write(&active_config->nmc_range_tree_lock);
901 nm_member_revoke_locks(active_config->nmc_default_nodemap);
902 nm_member_revoke_locks(nodemap);
905 mutex_unlock(&active_config_lock);
906 nodemap_putref(nodemap);
910 EXPORT_SYMBOL(nodemap_del_range);
913 * set fileset on nodemap
914 * \param name nodemap to set fileset on
915 * \param fileset string containing fileset
916 * \retval 0 on success
918 * set a fileset on the named nodemap
920 static int nodemap_set_fileset_helper(struct nodemap_config *config,
921 struct lu_nodemap *nodemap,
926 /* Allow 'fileset=clear' in addition to 'fileset=""' to clear fileset
927 * because either command 'lctl set_param -P *.*.fileset=""' or
928 * 'lctl nodemap_set_fileset --fileset ""' can only work correctly
929 * on MGS, while on other servers, both commands will invoke upcall
930 * "/usr/sbin/lctl set_param nodemap.default.fileset=" by function
931 * process_param2_config(), which will cause "no value" error and
932 * won't clear fileset.
933 * 'fileset=""' is still kept for compatibility reason.
937 else if (fileset[0] == '\0' || strcmp(fileset, "clear") == 0)
938 nodemap->nm_fileset[0] = '\0';
939 else if (fileset[0] != '/')
941 else if (strlcpy(nodemap->nm_fileset, fileset,
942 sizeof(nodemap->nm_fileset)) >=
943 sizeof(nodemap->nm_fileset))
949 int nodemap_set_fileset(const char *name, const char *fileset)
951 struct lu_nodemap *nodemap = NULL;
954 mutex_lock(&active_config_lock);
955 nodemap = nodemap_lookup(name);
956 if (IS_ERR(nodemap)) {
957 mutex_unlock(&active_config_lock);
958 GOTO(out, rc = PTR_ERR(nodemap));
961 rc = nodemap_set_fileset_helper(active_config, nodemap, fileset);
962 mutex_unlock(&active_config_lock);
964 nodemap_putref(nodemap);
968 EXPORT_SYMBOL(nodemap_set_fileset);
971 * get fileset defined on nodemap
972 * \param nodemap nodemap to get fileset from
973 * \retval fileset name, or NULL if not defined or not activated
975 * get the fileset defined on the nodemap
977 char *nodemap_get_fileset(const struct lu_nodemap *nodemap)
982 return (char *)nodemap->nm_fileset;
984 EXPORT_SYMBOL(nodemap_get_fileset);
986 static int nodemap_validate_sepol(const char *sepol)
988 char buf[LUSTRE_NODEMAP_SEPOL_LENGTH + 1];
989 char *p = (char *)sepol;
991 char polname[NAME_MAX + 1] = "";
992 char hash[SELINUX_POLICY_HASH_LEN + 1] = "";
996 BUILD_BUG_ON(sizeof(buf) != sizeof(((struct lu_nodemap *)0)->nm_sepol));
1001 /* we allow sepol = "" which means clear SELinux policy info */
1002 if (sepol[0] == '\0')
1005 /* make a copy of sepol, by replacing ':' with space
1006 * so that we can use sscanf over the string
1008 while (p-sepol < sizeof(buf)) {
1018 if (p-sepol == sizeof(buf))
1019 return -ENAMETOOLONG;
1021 if (sscanf(buf, "%1hhu %s %hu %s", &mode, polname, &ver, hash) != 4)
1024 if (mode != 0 && mode != 1)
1031 * set SELinux policy on nodemap
1032 * \param name nodemap to set SELinux policy info on
1033 * \param sepol string containing SELinux policy info
1034 * \retval 0 on success
1036 * set SELinux policy info on the named nodemap
1038 int nodemap_set_sepol(const char *name, const char *sepol)
1040 struct lu_nodemap *nodemap = NULL;
1043 rc = nodemap_validate_sepol(sepol);
1047 mutex_lock(&active_config_lock);
1048 nodemap = nodemap_lookup(name);
1049 if (IS_ERR(nodemap)) {
1050 mutex_unlock(&active_config_lock);
1051 GOTO(out, rc = PTR_ERR(nodemap));
1054 if (is_default_nodemap(nodemap)) {
1055 /* We do not want nodes in the default nodemap to have
1056 * SELinux restrictions. Sec admin should create dedicated
1057 * nodemap entries for this.
1059 GOTO(out_putref, rc = -EINVAL);
1062 /* truncation cannot happen, as string length was checked in
1063 * nodemap_validate_sepol()
1065 strlcpy(nodemap->nm_sepol, sepol, sizeof(nodemap->nm_sepol));
1068 mutex_unlock(&active_config_lock);
1069 nodemap_putref(nodemap);
1073 EXPORT_SYMBOL(nodemap_set_sepol);
1076 * get SELinux policy info defined on nodemap
1077 * \param nodemap nodemap to get SELinux policy info from
1078 * \retval SELinux policy info, or NULL if not defined or not activated
1080 * get the SELinux policy info defined on the nodemap
1082 const char *nodemap_get_sepol(const struct lu_nodemap *nodemap)
1084 if (is_default_nodemap(nodemap))
1087 return (char *)nodemap->nm_sepol;
1089 EXPORT_SYMBOL(nodemap_get_sepol);
1092 * Nodemap constructor
1094 * Creates an lu_nodemap structure and assigns sane default
1095 * member values. If this is the default nodemap, the defaults
1096 * are the most restrictive in terms of mapping behavior. Otherwise
1097 * the default flags should be inherited from the default nodemap.
1098 * The adds nodemap to nodemap_hash.
1100 * Requires that the caller take the active_config_lock
1102 * \param name name of nodemap
1103 * \param is_default true if default nodemap
1104 * \retval nodemap success
1105 * \retval -EINVAL invalid nodemap name
1106 * \retval -EEXIST nodemap already exists
1107 * \retval -ENOMEM cannot allocate memory for nodemap
1109 struct lu_nodemap *nodemap_create(const char *name,
1110 struct nodemap_config *config,
1113 struct lu_nodemap *nodemap = NULL;
1114 struct lu_nodemap *default_nodemap;
1115 struct cfs_hash *hash = config->nmc_nodemap_hash;
1119 default_nodemap = config->nmc_default_nodemap;
1121 if (!nodemap_name_is_valid(name))
1122 GOTO(out, rc = -EINVAL);
1125 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
1126 GOTO(out, rc = -EINVAL);
1129 OBD_ALLOC_PTR(nodemap);
1130 if (nodemap == NULL) {
1131 CERROR("cannot allocate memory (%zu bytes) for nodemap '%s'\n",
1132 sizeof(*nodemap), name);
1133 GOTO(out, rc = -ENOMEM);
1137 * take an extra reference to prevent nodemap from being destroyed
1138 * while it's being created.
1140 atomic_set(&nodemap->nm_refcount, 2);
1141 snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
1142 rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
1144 OBD_FREE_PTR(nodemap);
1145 GOTO(out, rc = -EEXIST);
1148 INIT_LIST_HEAD(&nodemap->nm_ranges);
1149 INIT_LIST_HEAD(&nodemap->nm_list);
1150 INIT_LIST_HEAD(&nodemap->nm_member_list);
1152 mutex_init(&nodemap->nm_member_list_lock);
1153 init_rwsem(&nodemap->nm_idmap_lock);
1154 nodemap->nm_fs_to_client_uidmap = RB_ROOT;
1155 nodemap->nm_client_to_fs_uidmap = RB_ROOT;
1156 nodemap->nm_fs_to_client_gidmap = RB_ROOT;
1157 nodemap->nm_client_to_fs_gidmap = RB_ROOT;
1160 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
1161 config->nmc_default_nodemap = nodemap;
1163 config->nmc_nodemap_highest_id++;
1164 nodemap->nm_id = config->nmc_nodemap_highest_id;
1167 if (is_default || default_nodemap == NULL) {
1168 nodemap->nmf_trust_client_ids = 0;
1169 nodemap->nmf_allow_root_access = 0;
1170 nodemap->nmf_deny_unknown = 0;
1171 nodemap->nmf_map_uid_only = 0;
1172 nodemap->nmf_map_gid_only = 0;
1173 nodemap->nmf_enable_audit = 1;
1174 nodemap->nmf_forbid_encryption = 0;
1176 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
1177 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
1178 nodemap->nm_fileset[0] = '\0';
1179 nodemap->nm_sepol[0] = '\0';
1181 CWARN("adding nodemap '%s' to config without"
1182 " default nodemap\n", nodemap->nm_name);
1184 nodemap->nmf_trust_client_ids =
1185 default_nodemap->nmf_trust_client_ids;
1186 nodemap->nmf_allow_root_access =
1187 default_nodemap->nmf_allow_root_access;
1188 nodemap->nmf_deny_unknown =
1189 default_nodemap->nmf_deny_unknown;
1190 nodemap->nmf_map_uid_only =
1191 default_nodemap->nmf_map_uid_only;
1192 nodemap->nmf_map_gid_only =
1193 default_nodemap->nmf_map_gid_only;
1194 nodemap->nmf_enable_audit =
1195 default_nodemap->nmf_enable_audit;
1196 nodemap->nmf_forbid_encryption =
1197 default_nodemap->nmf_forbid_encryption;
1199 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
1200 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
1201 nodemap->nm_fileset[0] = '\0';
1202 nodemap->nm_sepol[0] = '\0';
1208 CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
1209 RETURN(ERR_PTR(rc));
1213 * Set the nmf_deny_unknown flag to true or false.
1214 * \param name nodemap name
1215 * \param deny_unknown if true, squashed users will get EACCES
1216 * \retval 0 on success
1219 int nodemap_set_deny_unknown(const char *name, bool deny_unknown)
1221 struct lu_nodemap *nodemap = NULL;
1224 mutex_lock(&active_config_lock);
1225 nodemap = nodemap_lookup(name);
1226 mutex_unlock(&active_config_lock);
1227 if (IS_ERR(nodemap))
1228 GOTO(out, rc = PTR_ERR(nodemap));
1230 nodemap->nmf_deny_unknown = deny_unknown;
1231 rc = nodemap_idx_nodemap_update(nodemap);
1233 nm_member_revoke_locks(nodemap);
1234 nodemap_putref(nodemap);
1238 EXPORT_SYMBOL(nodemap_set_deny_unknown);
1241 * Set the nmf_allow_root_access flag to true or false.
1242 * \param name nodemap name
1243 * \param allow_root if true, nodemap will not squash the root user
1244 * \retval 0 on success
1247 int nodemap_set_allow_root(const char *name, bool allow_root)
1249 struct lu_nodemap *nodemap = NULL;
1252 mutex_lock(&active_config_lock);
1253 nodemap = nodemap_lookup(name);
1254 mutex_unlock(&active_config_lock);
1255 if (IS_ERR(nodemap))
1256 GOTO(out, rc = PTR_ERR(nodemap));
1258 nodemap->nmf_allow_root_access = allow_root;
1259 rc = nodemap_idx_nodemap_update(nodemap);
1261 nm_member_revoke_locks(nodemap);
1262 nodemap_putref(nodemap);
1266 EXPORT_SYMBOL(nodemap_set_allow_root);
1269 * Set the nmf_trust_client_ids flag to true or false.
1271 * \param name nodemap name
1272 * \param trust_client_ids if true, nodemap will not map its IDs
1273 * \retval 0 on success
1276 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
1278 struct lu_nodemap *nodemap = NULL;
1281 mutex_lock(&active_config_lock);
1282 nodemap = nodemap_lookup(name);
1283 mutex_unlock(&active_config_lock);
1284 if (IS_ERR(nodemap))
1285 GOTO(out, rc = PTR_ERR(nodemap));
1287 nodemap->nmf_trust_client_ids = trust_client_ids;
1288 rc = nodemap_idx_nodemap_update(nodemap);
1290 nm_member_revoke_locks(nodemap);
1291 nodemap_putref(nodemap);
1295 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
1297 int nodemap_set_mapping_mode(const char *name, enum nodemap_mapping_modes mode)
1299 struct lu_nodemap *nodemap = NULL;
1302 mutex_lock(&active_config_lock);
1303 nodemap = nodemap_lookup(name);
1304 mutex_unlock(&active_config_lock);
1305 if (IS_ERR(nodemap))
1306 GOTO(out, rc = PTR_ERR(nodemap));
1309 case NODEMAP_MAP_BOTH:
1310 nodemap->nmf_map_uid_only = 0;
1311 nodemap->nmf_map_gid_only = 0;
1313 case NODEMAP_MAP_UID_ONLY:
1314 nodemap->nmf_map_uid_only = 1;
1315 nodemap->nmf_map_gid_only = 0;
1317 case NODEMAP_MAP_GID_ONLY:
1318 nodemap->nmf_map_uid_only = 0;
1319 nodemap->nmf_map_gid_only = 1;
1322 CWARN("cannot set unknown mapping mode, mode = %d\n", mode);
1324 rc = nodemap_idx_nodemap_update(nodemap);
1326 nm_member_revoke_locks(nodemap);
1327 nodemap_putref(nodemap);
1331 EXPORT_SYMBOL(nodemap_set_mapping_mode);
1334 * Update the squash_uid for a nodemap.
1336 * \param name nodemap name
1337 * \param uid the new uid to squash unknown users to
1338 * \retval 0 on success
1340 * Update the squash_uid for a nodemap. The squash_uid is the uid
1341 * that the all client uids are mapped to if nodemap is active,
1342 * the trust_client_ids flag is not set, and the uid is not in
1345 int nodemap_set_squash_uid(const char *name, uid_t uid)
1347 struct lu_nodemap *nodemap = NULL;
1350 mutex_lock(&active_config_lock);
1351 nodemap = nodemap_lookup(name);
1352 mutex_unlock(&active_config_lock);
1353 if (IS_ERR(nodemap))
1354 GOTO(out, rc = PTR_ERR(nodemap));
1356 nodemap->nm_squash_uid = uid;
1357 rc = nodemap_idx_nodemap_update(nodemap);
1359 nm_member_revoke_locks(nodemap);
1360 nodemap_putref(nodemap);
1364 EXPORT_SYMBOL(nodemap_set_squash_uid);
1367 * Update the squash_gid for a nodemap.
1369 * \param name nodemap name
1370 * \param gid the new gid to squash unknown gids to
1371 * \retval 0 on success
1373 * Update the squash_gid for a nodemap. The squash_uid is the gid
1374 * that the all client gids are mapped to if nodemap is active,
1375 * the trust_client_ids flag is not set, and the gid is not in
1378 int nodemap_set_squash_gid(const char *name, gid_t gid)
1380 struct lu_nodemap *nodemap = NULL;
1383 mutex_lock(&active_config_lock);
1384 nodemap = nodemap_lookup(name);
1385 mutex_unlock(&active_config_lock);
1386 if (IS_ERR(nodemap))
1387 GOTO(out, rc = PTR_ERR(nodemap));
1389 nodemap->nm_squash_gid = gid;
1390 rc = nodemap_idx_nodemap_update(nodemap);
1392 nm_member_revoke_locks(nodemap);
1393 nodemap_putref(nodemap);
1397 EXPORT_SYMBOL(nodemap_set_squash_gid);
1400 * Returns true if this nodemap has root user access. Always returns true if
1401 * nodemaps are not active.
1403 * \param nodemap nodemap to check access for
1405 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
1407 return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
1409 EXPORT_SYMBOL(nodemap_can_setquota);
1412 * Set the nmf_enable_audit flag to true or false.
1413 * \param name nodemap name
1414 * \param audit_mode if true, allow audit
1415 * \retval 0 on success
1418 int nodemap_set_audit_mode(const char *name, bool enable_audit)
1420 struct lu_nodemap *nodemap = NULL;
1423 mutex_lock(&active_config_lock);
1424 nodemap = nodemap_lookup(name);
1425 mutex_unlock(&active_config_lock);
1426 if (IS_ERR(nodemap))
1427 GOTO(out, rc = PTR_ERR(nodemap));
1429 nodemap->nmf_enable_audit = enable_audit;
1430 rc = nodemap_idx_nodemap_update(nodemap);
1432 nm_member_revoke_locks(nodemap);
1433 nodemap_putref(nodemap);
1437 EXPORT_SYMBOL(nodemap_set_audit_mode);
1440 * Set the nmf_forbid_encryption flag to true or false.
1441 * \param name nodemap name
1442 * \param forbid_encryption if true, forbid encryption
1443 * \retval 0 on success
1446 int nodemap_set_forbid_encryption(const char *name, bool forbid_encryption)
1448 struct lu_nodemap *nodemap = NULL;
1451 mutex_lock(&active_config_lock);
1452 nodemap = nodemap_lookup(name);
1453 mutex_unlock(&active_config_lock);
1454 if (IS_ERR(nodemap))
1455 GOTO(out, rc = PTR_ERR(nodemap));
1457 nodemap->nmf_forbid_encryption = forbid_encryption;
1458 rc = nodemap_idx_nodemap_update(nodemap);
1460 nm_member_revoke_locks(nodemap);
1461 nodemap_putref(nodemap);
1465 EXPORT_SYMBOL(nodemap_set_forbid_encryption);
1471 * \param name name of nodemap
1473 * \retval -EINVAL invalid nodemap name
1474 * \retval -EEXIST nodemap already exists
1475 * \retval -ENOMEM cannot allocate memory for nodemap
1477 int nodemap_add(const char *nodemap_name)
1479 struct lu_nodemap *nodemap;
1482 mutex_lock(&active_config_lock);
1483 nodemap = nodemap_create(nodemap_name, active_config, 0);
1484 if (IS_ERR(nodemap)) {
1485 mutex_unlock(&active_config_lock);
1486 return PTR_ERR(nodemap);
1489 rc = nodemap_idx_nodemap_add(nodemap);
1491 rc = lprocfs_nodemap_register(nodemap, 0);
1493 mutex_unlock(&active_config_lock);
1494 nodemap_putref(nodemap);
1498 EXPORT_SYMBOL(nodemap_add);
1503 * \param name name of nodemmap
1505 * \retval -EINVAL invalid input
1506 * \retval -ENOENT no existing nodemap
1508 int nodemap_del(const char *nodemap_name)
1510 struct lu_nodemap *nodemap;
1511 struct lu_nid_range *range;
1512 struct lu_nid_range *range_temp;
1516 if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1519 mutex_lock(&active_config_lock);
1520 nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1522 if (nodemap == NULL) {
1523 mutex_unlock(&active_config_lock);
1524 GOTO(out, rc = -ENOENT);
1527 /* erase nodemap from active ranges to prevent client assignment */
1528 down_write(&active_config->nmc_range_tree_lock);
1529 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1531 rc2 = nodemap_idx_range_del(range);
1535 range_delete(&active_config->nmc_range_tree, range);
1537 up_write(&active_config->nmc_range_tree_lock);
1539 rc2 = nodemap_idx_nodemap_del(nodemap);
1544 * remove procfs here in case nodemap_create called with same name
1545 * before nodemap_destroy is run.
1547 lprocfs_nodemap_remove(nodemap->nm_pde_data);
1548 nodemap->nm_pde_data = NULL;
1550 /* reclassify all member exports from nodemap, so they put their refs */
1551 down_read(&active_config->nmc_range_tree_lock);
1552 nm_member_reclassify_nodemap(nodemap);
1553 up_read(&active_config->nmc_range_tree_lock);
1555 if (!list_empty(&nodemap->nm_member_list))
1556 CWARN("nodemap_del failed to reclassify all members\n");
1558 mutex_unlock(&active_config_lock);
1560 nodemap_putref(nodemap);
1565 EXPORT_SYMBOL(nodemap_del);
1568 * activate nodemap functions
1570 * \param value 1 for on, 0 for off
1572 void nodemap_activate(const bool value)
1574 mutex_lock(&active_config_lock);
1575 active_config->nmc_nodemap_is_active = value;
1577 /* copy active value to global to avoid locking in map functions */
1578 nodemap_active = value;
1579 nodemap_idx_nodemap_activate(value);
1580 mutex_unlock(&active_config_lock);
1581 nm_member_revoke_all();
1583 EXPORT_SYMBOL(nodemap_activate);
1586 * Helper iterator to convert nodemap hash to list.
1588 * \param hs hash structure
1589 * \param bd bucket descriptor
1590 * \param hnode hash node
1591 * \param nodemap_list_head list head for list of nodemaps in hash
1593 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1594 struct hlist_node *hnode,
1595 void *nodemap_list_head)
1597 struct lu_nodemap *nodemap;
1599 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1600 list_add(&nodemap->nm_list, nodemap_list_head);
1602 cfs_hash_bd_del_locked(hs, bd, hnode);
1607 struct nodemap_config *nodemap_config_alloc(void)
1609 struct nodemap_config *config;
1612 OBD_ALLOC_PTR(config);
1614 return ERR_PTR(-ENOMEM);
1616 rc = nodemap_init_hash(config);
1618 OBD_FREE_PTR(config);
1622 init_rwsem(&config->nmc_range_tree_lock);
1624 config->nmc_range_tree.nmrt_range_interval_root = INTERVAL_TREE_ROOT;
1628 EXPORT_SYMBOL(nodemap_config_alloc);
1631 * Walk the nodemap_hash and remove all nodemaps.
1633 void nodemap_config_dealloc(struct nodemap_config *config)
1635 struct lu_nodemap *nodemap = NULL;
1636 struct lu_nodemap *nodemap_temp;
1637 struct lu_nid_range *range;
1638 struct lu_nid_range *range_temp;
1639 LIST_HEAD(nodemap_list_head);
1641 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1642 nodemap_cleanup_iter_cb, &nodemap_list_head);
1643 cfs_hash_putref(config->nmc_nodemap_hash);
1645 /* Because nodemap_destroy might sleep, we can't destroy them
1646 * in cfs_hash_for_each, so we build a list there and destroy here
1648 list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1650 mutex_lock(&active_config_lock);
1651 down_write(&config->nmc_range_tree_lock);
1653 /* move members to new config, requires ac lock */
1654 nm_member_reclassify_nodemap(nodemap);
1655 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1657 range_delete(&config->nmc_range_tree, range);
1658 up_write(&config->nmc_range_tree_lock);
1659 mutex_unlock(&active_config_lock);
1661 /* putref must be outside of ac lock if nm could be destroyed */
1662 nodemap_putref(nodemap);
1664 OBD_FREE_PTR(config);
1666 EXPORT_SYMBOL(nodemap_config_dealloc);
1669 * callback for cfs_hash_for_each_safe used to convert a nodemap hash to a
1670 * nodemap list, generally for locking purposes as a hash cb can't sleep.
1672 int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1673 struct hlist_node *hnode,
1674 void *nodemap_list_head)
1676 struct lu_nodemap *nodemap;
1678 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1679 list_add(&nodemap->nm_list, nodemap_list_head);
1683 void nodemap_config_set_active(struct nodemap_config *config)
1685 struct nodemap_config *old_config = active_config;
1686 struct lu_nodemap *nodemap;
1687 struct lu_nodemap *tmp;
1689 LIST_HEAD(nodemap_list_head);
1693 LASSERT(active_config != config);
1694 LASSERT(config->nmc_default_nodemap);
1696 mutex_lock(&active_config_lock);
1698 /* move proc entries from already existing nms, create for new nms */
1699 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1700 nm_hash_list_cb, &nodemap_list_head);
1701 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1702 struct lu_nodemap *old_nm = NULL;
1704 if (active_config != NULL)
1705 old_nm = cfs_hash_lookup(
1706 active_config->nmc_nodemap_hash,
1708 if (old_nm != NULL) {
1709 nodemap->nm_pde_data = old_nm->nm_pde_data;
1710 old_nm->nm_pde_data = NULL;
1711 nodemap_putref(old_nm);
1713 bool is_def = (nodemap == config->nmc_default_nodemap);
1715 lprocfs_nodemap_register(nodemap, is_def);
1720 * We only need to revoke locks if old nodemap was active, and new
1721 * config is now nodemap inactive. nodemap_config_dealloc will
1722 * reclassify exports, triggering a lock revoke if and only if new
1723 * nodemap is active.
1725 revoke_locks = !config->nmc_nodemap_is_active && nodemap_active;
1727 /* if new config is inactive, deactivate live config before switching */
1728 if (!config->nmc_nodemap_is_active)
1729 nodemap_active = false;
1730 active_config = config;
1731 if (config->nmc_nodemap_is_active)
1732 nodemap_active = true;
1734 mutex_unlock(&active_config_lock);
1736 if (old_config != NULL)
1737 nodemap_config_dealloc(old_config);
1740 nm_member_revoke_all();
1746 * Cleanup nodemap module on exit
1748 void nodemap_mod_exit(void)
1750 nodemap_config_dealloc(active_config);
1751 nodemap_procfs_exit();
1755 * Initialize the nodemap module
1757 int nodemap_mod_init(void)
1759 struct nodemap_config *new_config;
1760 struct lu_nodemap *nodemap;
1763 rc = nodemap_procfs_init();
1767 new_config = nodemap_config_alloc();
1768 if (IS_ERR(new_config)) {
1769 nodemap_procfs_exit();
1770 GOTO(out, rc = PTR_ERR(new_config));
1773 nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1774 if (IS_ERR(nodemap)) {
1775 nodemap_config_dealloc(new_config);
1776 nodemap_procfs_exit();
1777 GOTO(out, rc = PTR_ERR(nodemap));
1780 nodemap_config_set_active(new_config);
1781 nodemap_putref(nodemap);
1788 * Revoke locks for all nodemaps.
1790 void nm_member_revoke_all(void)
1792 struct lu_nodemap *nodemap;
1793 struct lu_nodemap *tmp;
1794 LIST_HEAD(nodemap_list_head);
1796 mutex_lock(&active_config_lock);
1797 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1798 nm_hash_list_cb, &nodemap_list_head);
1800 /* revoke_locks sleeps, so can't call in cfs hash cb */
1801 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1802 nm_member_revoke_locks_always(nodemap);
1803 mutex_unlock(&active_config_lock);
1807 * Returns the nodemap classification for a given nid into an ioctl buffer.
1808 * Useful for testing the nodemap configuration to make sure it is working as
1811 * \param nid nid to classify
1812 * \param[out] name_buf buffer to write the nodemap name to
1813 * \param name_len length of buffer
1815 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1817 struct lu_nodemap *nodemap;
1819 mutex_lock(&active_config_lock);
1820 down_read(&active_config->nmc_range_tree_lock);
1821 nodemap = nodemap_classify_nid(nid);
1822 up_read(&active_config->nmc_range_tree_lock);
1823 mutex_unlock(&active_config_lock);
1825 if (IS_ERR(nodemap))
1828 strncpy(name_buf, nodemap->nm_name, name_len);
1830 name_buf[name_len - 1] = '\0';
1832 nodemap_putref(nodemap);
1834 EXPORT_SYMBOL(nodemap_test_nid);
1837 * Passes back the id mapping for a given nid/id pair. Useful for testing the
1838 * nodemap configuration to make sure it is working as expected.
1840 * \param nid nid to classify
1841 * \param idtype uid or gid
1842 * \param client_id id to map to fs
1843 * \param fs_id_buf pointer to save mapped fs_id to
1846 * \retval -EINVAL invalid NID
1848 int nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1849 __u32 client_id, __u32 *fs_id)
1851 struct lu_nodemap *nodemap;
1853 mutex_lock(&active_config_lock);
1854 down_read(&active_config->nmc_range_tree_lock);
1855 nodemap = nodemap_classify_nid(nid);
1856 up_read(&active_config->nmc_range_tree_lock);
1857 mutex_unlock(&active_config_lock);
1859 if (IS_ERR(nodemap))
1860 return PTR_ERR(nodemap);
1862 *fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1864 nodemap_putref(nodemap);
1868 EXPORT_SYMBOL(nodemap_test_id);