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_processid id;
271 rc = LNetGetId(i++, &id);
273 RETURN(ERR_PTR(-EINVAL));
274 } while (nid_is_lo0(&id.nid));
276 nid = lnet_nid_to_nid4(&id.nid);
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 or NODEMAP_PROJID
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 (id_type == NODEMAP_UID &&
673 !(nodemap->nmf_map_mode & NODEMAP_MAP_UID))
676 if (id_type == NODEMAP_GID &&
677 !(nodemap->nmf_map_mode & NODEMAP_MAP_GID))
680 if (id_type == NODEMAP_PROJID &&
681 !(nodemap->nmf_map_mode & NODEMAP_MAP_PROJID))
685 if (nodemap->nmf_allow_root_access)
691 if (nodemap->nmf_trust_client_ids)
694 if (is_default_nodemap(nodemap))
697 down_read(&nodemap->nm_idmap_lock);
698 idmap = idmap_search(nodemap, tree_type, id_type, id);
700 up_read(&nodemap->nm_idmap_lock);
704 if (tree_type == NODEMAP_FS_TO_CLIENT)
705 found_id = idmap->id_client;
707 found_id = idmap->id_fs;
708 up_read(&nodemap->nm_idmap_lock);
712 if (id_type == NODEMAP_UID)
713 RETURN(nodemap->nm_squash_uid);
714 if (id_type == NODEMAP_GID)
715 RETURN(nodemap->nm_squash_gid);
716 if (id_type == NODEMAP_PROJID)
717 RETURN(nodemap->nm_squash_projid);
721 EXPORT_SYMBOL(nodemap_map_id);
724 * Map posix ACL entries according to the nodemap membership. Removes any
727 * \param lu_nodemap nodemap
728 * \param buf buffer containing xattr encoded ACLs
729 * \param size size of ACLs in bytes
730 * \param tree_type direction of mapping
731 * \retval size new size of ACLs in bytes
732 * \retval -EINVAL bad \a size param, see posix_acl_xattr_count()
734 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
735 enum nodemap_tree_type tree_type)
737 posix_acl_xattr_header *header = buf;
738 posix_acl_xattr_entry *entry = GET_POSIX_ACL_XATTR_ENTRY(header);
739 posix_acl_xattr_entry *new_entry = entry;
740 posix_acl_xattr_entry *end;
748 if (unlikely(nodemap == NULL))
751 count = posix_acl_xattr_count(size);
755 /* if not proper ACL, do nothing and return initial size */
758 for (end = entry + count; entry != end; entry++) {
759 __u16 tag = le16_to_cpu(entry->e_tag);
760 __u32 id = le32_to_cpu(entry->e_id);
764 id = nodemap_map_id(nodemap, NODEMAP_UID,
766 if (id == nodemap->nm_squash_uid)
768 entry->e_id = cpu_to_le32(id);
771 id = nodemap_map_id(nodemap, NODEMAP_GID,
773 if (id == nodemap->nm_squash_gid)
775 entry->e_id = cpu_to_le32(id);
779 /* if we skip an ACL, copy the following ones over it */
780 if (new_entry != entry)
786 RETURN((void *)new_entry - (void *)header);
788 EXPORT_SYMBOL(nodemap_map_acl);
791 * Add nid range to given nodemap
793 * \param config nodemap config to work on
794 * \param nodemap nodemap to add range to
795 * \param nid nid range to add
796 * \param range_id should be 0 unless loading from disk
801 int nodemap_add_range_helper(struct nodemap_config *config,
802 struct lu_nodemap *nodemap,
803 const lnet_nid_t nid[2],
804 unsigned int range_id)
806 struct lu_nid_range *range;
809 down_write(&config->nmc_range_tree_lock);
810 range = range_create(&config->nmc_range_tree, nid[0], nid[1],
813 up_write(&config->nmc_range_tree_lock);
814 GOTO(out, rc = -ENOMEM);
817 rc = range_insert(&config->nmc_range_tree, range);
819 CDEBUG_LIMIT(rc == -EEXIST ? D_INFO : D_ERROR,
820 "cannot insert nodemap range into '%s': rc = %d\n",
821 nodemap->nm_name, rc);
822 up_write(&config->nmc_range_tree_lock);
823 list_del(&range->rn_list);
824 range_destroy(range);
828 list_add(&range->rn_list, &nodemap->nm_ranges);
830 /* nodemaps have no members if they aren't on the active config */
831 if (config == active_config)
832 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
834 up_write(&config->nmc_range_tree_lock);
836 /* if range_id is non-zero, we are loading from disk */
838 rc = nodemap_idx_range_add(range, nid);
840 if (config == active_config) {
841 nm_member_revoke_locks(config->nmc_default_nodemap);
842 nm_member_revoke_locks(nodemap);
849 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
851 struct lu_nodemap *nodemap = NULL;
854 mutex_lock(&active_config_lock);
855 nodemap = nodemap_lookup(name);
856 if (IS_ERR(nodemap)) {
857 mutex_unlock(&active_config_lock);
858 GOTO(out, rc = PTR_ERR(nodemap));
861 if (is_default_nodemap(nodemap))
864 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
865 mutex_unlock(&active_config_lock);
866 nodemap_putref(nodemap);
870 EXPORT_SYMBOL(nodemap_add_range);
874 * \param name nodemap name
875 * \param nid nid range
876 * \retval 0 on success
878 * Delete range from global range tree, and remove it
879 * from the list in the associated nodemap.
881 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
883 struct lu_nodemap *nodemap;
884 struct lu_nid_range *range;
887 mutex_lock(&active_config_lock);
888 nodemap = nodemap_lookup(name);
889 if (IS_ERR(nodemap)) {
890 mutex_unlock(&active_config_lock);
891 GOTO(out, rc = PTR_ERR(nodemap));
894 if (is_default_nodemap(nodemap))
895 GOTO(out_putref, rc = -EINVAL);
897 down_write(&active_config->nmc_range_tree_lock);
898 range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
900 up_write(&active_config->nmc_range_tree_lock);
901 GOTO(out_putref, rc = -EINVAL);
903 if (range->rn_nodemap != nodemap) {
904 up_write(&active_config->nmc_range_tree_lock);
905 GOTO(out_putref, rc = -EINVAL);
907 rc = nodemap_idx_range_del(range);
908 range_delete(&active_config->nmc_range_tree, range);
909 nm_member_reclassify_nodemap(nodemap);
910 up_write(&active_config->nmc_range_tree_lock);
912 nm_member_revoke_locks(active_config->nmc_default_nodemap);
913 nm_member_revoke_locks(nodemap);
916 mutex_unlock(&active_config_lock);
917 nodemap_putref(nodemap);
921 EXPORT_SYMBOL(nodemap_del_range);
924 * set fileset on nodemap
925 * \param name nodemap to set fileset on
926 * \param fileset string containing fileset
927 * \retval 0 on success
929 * set a fileset on the named nodemap
931 static int nodemap_set_fileset_helper(struct nodemap_config *config,
932 struct lu_nodemap *nodemap,
937 /* Allow 'fileset=clear' in addition to 'fileset=""' to clear fileset
938 * because either command 'lctl set_param -P *.*.fileset=""' or
939 * 'lctl nodemap_set_fileset --fileset ""' can only work correctly
940 * on MGS, while on other servers, both commands will invoke upcall
941 * "/usr/sbin/lctl set_param nodemap.default.fileset=" by function
942 * process_param2_config(), which will cause "no value" error and
943 * won't clear fileset.
944 * 'fileset=""' is still kept for compatibility reason.
948 else if (fileset[0] == '\0' || strcmp(fileset, "clear") == 0)
949 nodemap->nm_fileset[0] = '\0';
950 else if (fileset[0] != '/')
952 else if (strlcpy(nodemap->nm_fileset, fileset,
953 sizeof(nodemap->nm_fileset)) >=
954 sizeof(nodemap->nm_fileset))
960 int nodemap_set_fileset(const char *name, const char *fileset)
962 struct lu_nodemap *nodemap = NULL;
965 mutex_lock(&active_config_lock);
966 nodemap = nodemap_lookup(name);
967 if (IS_ERR(nodemap)) {
968 mutex_unlock(&active_config_lock);
969 GOTO(out, rc = PTR_ERR(nodemap));
972 rc = nodemap_set_fileset_helper(active_config, nodemap, fileset);
973 mutex_unlock(&active_config_lock);
975 nodemap_putref(nodemap);
979 EXPORT_SYMBOL(nodemap_set_fileset);
982 * get fileset defined on nodemap
983 * \param nodemap nodemap to get fileset from
984 * \retval fileset name, or NULL if not defined or not activated
986 * get the fileset defined on the nodemap
988 char *nodemap_get_fileset(const struct lu_nodemap *nodemap)
993 return (char *)nodemap->nm_fileset;
995 EXPORT_SYMBOL(nodemap_get_fileset);
997 static int nodemap_validate_sepol(const char *sepol)
999 char buf[LUSTRE_NODEMAP_SEPOL_LENGTH + 1];
1000 char *p = (char *)sepol;
1002 char polname[NAME_MAX + 1] = "";
1003 char hash[SELINUX_POLICY_HASH_LEN + 1] = "";
1007 BUILD_BUG_ON(sizeof(buf) != sizeof(((struct lu_nodemap *)0)->nm_sepol));
1012 /* we allow sepol = "" which means clear SELinux policy info */
1013 if (sepol[0] == '\0')
1016 /* make a copy of sepol, by replacing ':' with space
1017 * so that we can use sscanf over the string
1019 while (p-sepol < sizeof(buf)) {
1029 if (p-sepol == sizeof(buf))
1030 return -ENAMETOOLONG;
1032 if (sscanf(buf, "%1hhu %s %hu %s", &mode, polname, &ver, hash) != 4)
1035 if (mode != 0 && mode != 1)
1042 * set SELinux policy on nodemap
1043 * \param name nodemap to set SELinux policy info on
1044 * \param sepol string containing SELinux policy info
1045 * \retval 0 on success
1047 * set SELinux policy info on the named nodemap
1049 int nodemap_set_sepol(const char *name, const char *sepol)
1051 struct lu_nodemap *nodemap = NULL;
1054 rc = nodemap_validate_sepol(sepol);
1058 mutex_lock(&active_config_lock);
1059 nodemap = nodemap_lookup(name);
1060 if (IS_ERR(nodemap)) {
1061 mutex_unlock(&active_config_lock);
1062 GOTO(out, rc = PTR_ERR(nodemap));
1065 if (is_default_nodemap(nodemap)) {
1066 /* We do not want nodes in the default nodemap to have
1067 * SELinux restrictions. Sec admin should create dedicated
1068 * nodemap entries for this.
1070 GOTO(out_putref, rc = -EINVAL);
1073 /* truncation cannot happen, as string length was checked in
1074 * nodemap_validate_sepol()
1076 strlcpy(nodemap->nm_sepol, sepol, sizeof(nodemap->nm_sepol));
1079 mutex_unlock(&active_config_lock);
1080 nodemap_putref(nodemap);
1084 EXPORT_SYMBOL(nodemap_set_sepol);
1087 * get SELinux policy info defined on nodemap
1088 * \param nodemap nodemap to get SELinux policy info from
1089 * \retval SELinux policy info, or NULL if not defined or not activated
1091 * get the SELinux policy info defined on the nodemap
1093 const char *nodemap_get_sepol(const struct lu_nodemap *nodemap)
1095 if (is_default_nodemap(nodemap))
1098 return (char *)nodemap->nm_sepol;
1100 EXPORT_SYMBOL(nodemap_get_sepol);
1103 * Nodemap constructor
1105 * Creates an lu_nodemap structure and assigns sane default
1106 * member values. If this is the default nodemap, the defaults
1107 * are the most restrictive in terms of mapping behavior. Otherwise
1108 * the default flags should be inherited from the default nodemap.
1109 * The adds nodemap to nodemap_hash.
1111 * Requires that the caller take the active_config_lock
1113 * \param name name of nodemap
1114 * \param is_default true if default nodemap
1115 * \retval nodemap success
1116 * \retval -EINVAL invalid nodemap name
1117 * \retval -EEXIST nodemap already exists
1118 * \retval -ENOMEM cannot allocate memory for nodemap
1120 struct lu_nodemap *nodemap_create(const char *name,
1121 struct nodemap_config *config,
1124 struct lu_nodemap *nodemap = NULL;
1125 struct lu_nodemap *default_nodemap;
1126 struct cfs_hash *hash = config->nmc_nodemap_hash;
1130 default_nodemap = config->nmc_default_nodemap;
1132 if (!nodemap_name_is_valid(name))
1133 GOTO(out, rc = -EINVAL);
1136 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
1137 GOTO(out, rc = -EINVAL);
1140 OBD_ALLOC_PTR(nodemap);
1141 if (nodemap == NULL) {
1142 CERROR("cannot allocate memory (%zu bytes) for nodemap '%s'\n",
1143 sizeof(*nodemap), name);
1144 GOTO(out, rc = -ENOMEM);
1148 * take an extra reference to prevent nodemap from being destroyed
1149 * while it's being created.
1151 atomic_set(&nodemap->nm_refcount, 2);
1152 snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
1153 rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
1155 OBD_FREE_PTR(nodemap);
1156 GOTO(out, rc = -EEXIST);
1159 INIT_LIST_HEAD(&nodemap->nm_ranges);
1160 INIT_LIST_HEAD(&nodemap->nm_list);
1161 INIT_LIST_HEAD(&nodemap->nm_member_list);
1163 mutex_init(&nodemap->nm_member_list_lock);
1164 init_rwsem(&nodemap->nm_idmap_lock);
1165 nodemap->nm_fs_to_client_uidmap = RB_ROOT;
1166 nodemap->nm_client_to_fs_uidmap = RB_ROOT;
1167 nodemap->nm_fs_to_client_gidmap = RB_ROOT;
1168 nodemap->nm_client_to_fs_gidmap = RB_ROOT;
1169 nodemap->nm_fs_to_client_projidmap = RB_ROOT;
1170 nodemap->nm_client_to_fs_projidmap = RB_ROOT;
1173 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
1174 config->nmc_default_nodemap = nodemap;
1176 config->nmc_nodemap_highest_id++;
1177 nodemap->nm_id = config->nmc_nodemap_highest_id;
1180 if (is_default || default_nodemap == NULL) {
1181 nodemap->nmf_trust_client_ids = 0;
1182 nodemap->nmf_allow_root_access = 0;
1183 nodemap->nmf_deny_unknown = 0;
1184 nodemap->nmf_map_mode = NODEMAP_MAP_ALL;
1185 nodemap->nmf_enable_audit = 1;
1186 nodemap->nmf_forbid_encryption = 0;
1187 nodemap->nmf_readonly_mount = 0;
1189 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
1190 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
1191 nodemap->nm_squash_projid = NODEMAP_NOBODY_PROJID;
1192 nodemap->nm_fileset[0] = '\0';
1193 nodemap->nm_sepol[0] = '\0';
1195 CWARN("adding nodemap '%s' to config without"
1196 " default nodemap\n", nodemap->nm_name);
1198 nodemap->nmf_trust_client_ids =
1199 default_nodemap->nmf_trust_client_ids;
1200 nodemap->nmf_allow_root_access =
1201 default_nodemap->nmf_allow_root_access;
1202 nodemap->nmf_deny_unknown = default_nodemap->nmf_deny_unknown;
1203 nodemap->nmf_map_mode = default_nodemap->nmf_map_mode;
1204 nodemap->nmf_enable_audit = default_nodemap->nmf_enable_audit;
1205 nodemap->nmf_forbid_encryption =
1206 default_nodemap->nmf_forbid_encryption;
1207 nodemap->nmf_readonly_mount =
1208 default_nodemap->nmf_readonly_mount;
1210 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
1211 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
1212 nodemap->nm_squash_projid = default_nodemap->nm_squash_projid;
1213 nodemap->nm_fileset[0] = '\0';
1214 nodemap->nm_sepol[0] = '\0';
1220 CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
1221 RETURN(ERR_PTR(rc));
1225 * Set the nmf_deny_unknown flag to true or false.
1226 * \param name nodemap name
1227 * \param deny_unknown if true, squashed users will get EACCES
1228 * \retval 0 on success
1231 int nodemap_set_deny_unknown(const char *name, bool deny_unknown)
1233 struct lu_nodemap *nodemap = NULL;
1236 mutex_lock(&active_config_lock);
1237 nodemap = nodemap_lookup(name);
1238 mutex_unlock(&active_config_lock);
1239 if (IS_ERR(nodemap))
1240 GOTO(out, rc = PTR_ERR(nodemap));
1242 nodemap->nmf_deny_unknown = deny_unknown;
1243 rc = nodemap_idx_nodemap_update(nodemap);
1245 nm_member_revoke_locks(nodemap);
1246 nodemap_putref(nodemap);
1250 EXPORT_SYMBOL(nodemap_set_deny_unknown);
1253 * Set the nmf_allow_root_access flag to true or false.
1254 * \param name nodemap name
1255 * \param allow_root if true, nodemap will not squash the root user
1256 * \retval 0 on success
1259 int nodemap_set_allow_root(const char *name, bool allow_root)
1261 struct lu_nodemap *nodemap = NULL;
1264 mutex_lock(&active_config_lock);
1265 nodemap = nodemap_lookup(name);
1266 mutex_unlock(&active_config_lock);
1267 if (IS_ERR(nodemap))
1268 GOTO(out, rc = PTR_ERR(nodemap));
1270 nodemap->nmf_allow_root_access = allow_root;
1271 rc = nodemap_idx_nodemap_update(nodemap);
1273 nm_member_revoke_locks(nodemap);
1274 nodemap_putref(nodemap);
1278 EXPORT_SYMBOL(nodemap_set_allow_root);
1281 * Set the nmf_trust_client_ids flag to true or false.
1283 * \param name nodemap name
1284 * \param trust_client_ids if true, nodemap will not map its IDs
1285 * \retval 0 on success
1288 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
1290 struct lu_nodemap *nodemap = NULL;
1293 mutex_lock(&active_config_lock);
1294 nodemap = nodemap_lookup(name);
1295 mutex_unlock(&active_config_lock);
1296 if (IS_ERR(nodemap))
1297 GOTO(out, rc = PTR_ERR(nodemap));
1299 nodemap->nmf_trust_client_ids = trust_client_ids;
1300 rc = nodemap_idx_nodemap_update(nodemap);
1302 nm_member_revoke_locks(nodemap);
1303 nodemap_putref(nodemap);
1307 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
1309 int nodemap_set_mapping_mode(const char *name,
1310 enum nodemap_mapping_modes map_mode)
1312 struct lu_nodemap *nodemap = NULL;
1315 mutex_lock(&active_config_lock);
1316 nodemap = nodemap_lookup(name);
1317 mutex_unlock(&active_config_lock);
1318 if (IS_ERR(nodemap))
1319 GOTO(out, rc = PTR_ERR(nodemap));
1321 nodemap->nmf_map_mode = map_mode;
1322 rc = nodemap_idx_nodemap_update(nodemap);
1324 nm_member_revoke_locks(nodemap);
1325 nodemap_putref(nodemap);
1329 EXPORT_SYMBOL(nodemap_set_mapping_mode);
1332 * Update the squash_uid for a nodemap.
1334 * \param name nodemap name
1335 * \param uid the new uid to squash unknown users to
1336 * \retval 0 on success
1338 * Update the squash_uid for a nodemap. The squash_uid is the uid
1339 * that the all client uids are mapped to if nodemap is active,
1340 * the trust_client_ids flag is not set, and the uid is not in
1343 int nodemap_set_squash_uid(const char *name, uid_t uid)
1345 struct lu_nodemap *nodemap = NULL;
1348 mutex_lock(&active_config_lock);
1349 nodemap = nodemap_lookup(name);
1350 mutex_unlock(&active_config_lock);
1351 if (IS_ERR(nodemap))
1352 GOTO(out, rc = PTR_ERR(nodemap));
1354 nodemap->nm_squash_uid = uid;
1355 rc = nodemap_idx_nodemap_update(nodemap);
1357 nm_member_revoke_locks(nodemap);
1358 nodemap_putref(nodemap);
1362 EXPORT_SYMBOL(nodemap_set_squash_uid);
1365 * Update the squash_gid for a nodemap.
1367 * \param name nodemap name
1368 * \param gid the new gid to squash unknown gids to
1369 * \retval 0 on success
1371 * Update the squash_gid for a nodemap. The squash_gid is the gid
1372 * that the all client gids are mapped to if nodemap is active,
1373 * the trust_client_ids flag is not set, and the gid is not in
1376 int nodemap_set_squash_gid(const char *name, gid_t gid)
1378 struct lu_nodemap *nodemap = NULL;
1381 mutex_lock(&active_config_lock);
1382 nodemap = nodemap_lookup(name);
1383 mutex_unlock(&active_config_lock);
1384 if (IS_ERR(nodemap))
1385 GOTO(out, rc = PTR_ERR(nodemap));
1387 nodemap->nm_squash_gid = gid;
1388 rc = nodemap_idx_nodemap_update(nodemap);
1390 nm_member_revoke_locks(nodemap);
1391 nodemap_putref(nodemap);
1395 EXPORT_SYMBOL(nodemap_set_squash_gid);
1398 * Update the squash_projid for a nodemap.
1400 * \param name nodemap name
1401 * \param gid the new projid to squash unknown projids to
1402 * \retval 0 on success
1404 * Update the squash_projid for a nodemap. The squash_projid is the projid
1405 * that the all client projids are mapped to if nodemap is active,
1406 * the trust_client_ids flag is not set, and the projid is not in
1409 int nodemap_set_squash_projid(const char *name, projid_t projid)
1411 struct lu_nodemap *nodemap = NULL;
1414 mutex_lock(&active_config_lock);
1415 nodemap = nodemap_lookup(name);
1416 mutex_unlock(&active_config_lock);
1417 if (IS_ERR(nodemap))
1418 GOTO(out, rc = PTR_ERR(nodemap));
1420 nodemap->nm_squash_projid = projid;
1421 rc = nodemap_idx_nodemap_update(nodemap);
1423 nm_member_revoke_locks(nodemap);
1424 nodemap_putref(nodemap);
1428 EXPORT_SYMBOL(nodemap_set_squash_projid);
1431 * Check if nodemap allows setting quota.
1433 * If nodemap is not active, always allow.
1434 * For user and group quota, allow if the nodemap allows root access.
1435 * For project quota, allow if project id is not squashed or deny_unknown
1438 * \param nodemap nodemap to check access for
1439 * \param qc_type quota type
1440 * \param id client id to map
1441 * \retval true is setquota is allowed, false otherwise
1443 bool nodemap_can_setquota(struct lu_nodemap *nodemap, __u32 qc_type, __u32 id)
1445 if (!nodemap_active)
1448 if (!nodemap || !nodemap->nmf_allow_root_access)
1451 if (qc_type == PRJQUOTA) {
1452 id = nodemap_map_id(nodemap, NODEMAP_PROJID,
1453 NODEMAP_CLIENT_TO_FS, id);
1455 if (id == nodemap->nm_squash_projid &&
1456 nodemap->nmf_deny_unknown)
1462 EXPORT_SYMBOL(nodemap_can_setquota);
1465 * Set the nmf_enable_audit flag to true or false.
1466 * \param name nodemap name
1467 * \param audit_mode if true, allow audit
1468 * \retval 0 on success
1471 int nodemap_set_audit_mode(const char *name, bool enable_audit)
1473 struct lu_nodemap *nodemap = NULL;
1476 mutex_lock(&active_config_lock);
1477 nodemap = nodemap_lookup(name);
1478 mutex_unlock(&active_config_lock);
1479 if (IS_ERR(nodemap))
1480 GOTO(out, rc = PTR_ERR(nodemap));
1482 nodemap->nmf_enable_audit = enable_audit;
1483 rc = nodemap_idx_nodemap_update(nodemap);
1485 nm_member_revoke_locks(nodemap);
1486 nodemap_putref(nodemap);
1490 EXPORT_SYMBOL(nodemap_set_audit_mode);
1493 * Set the nmf_forbid_encryption flag to true or false.
1494 * \param name nodemap name
1495 * \param forbid_encryption if true, forbid encryption
1496 * \retval 0 on success
1499 int nodemap_set_forbid_encryption(const char *name, bool forbid_encryption)
1501 struct lu_nodemap *nodemap = NULL;
1504 mutex_lock(&active_config_lock);
1505 nodemap = nodemap_lookup(name);
1506 mutex_unlock(&active_config_lock);
1507 if (IS_ERR(nodemap))
1508 GOTO(out, rc = PTR_ERR(nodemap));
1510 nodemap->nmf_forbid_encryption = forbid_encryption;
1511 rc = nodemap_idx_nodemap_update(nodemap);
1513 nm_member_revoke_locks(nodemap);
1514 nodemap_putref(nodemap);
1518 EXPORT_SYMBOL(nodemap_set_forbid_encryption);
1521 * Set the nmf_readonly_mount flag to true or false.
1522 * \param name nodemap name
1523 * \param readonly_mount if true, forbid rw mount
1524 * \retval 0 on success
1527 int nodemap_set_readonly_mount(const char *name, bool readonly_mount)
1529 struct lu_nodemap *nodemap = NULL;
1532 mutex_lock(&active_config_lock);
1533 nodemap = nodemap_lookup(name);
1534 mutex_unlock(&active_config_lock);
1535 if (IS_ERR(nodemap))
1536 GOTO(out, rc = PTR_ERR(nodemap));
1538 nodemap->nmf_readonly_mount = readonly_mount;
1539 rc = nodemap_idx_nodemap_update(nodemap);
1541 nm_member_revoke_locks(nodemap);
1542 nodemap_putref(nodemap);
1546 EXPORT_SYMBOL(nodemap_set_readonly_mount);
1551 * \param name name of nodemap
1553 * \retval -EINVAL invalid nodemap name
1554 * \retval -EEXIST nodemap already exists
1555 * \retval -ENOMEM cannot allocate memory for nodemap
1557 int nodemap_add(const char *nodemap_name)
1559 struct lu_nodemap *nodemap;
1562 mutex_lock(&active_config_lock);
1563 nodemap = nodemap_create(nodemap_name, active_config, 0);
1564 if (IS_ERR(nodemap)) {
1565 mutex_unlock(&active_config_lock);
1566 return PTR_ERR(nodemap);
1569 rc = nodemap_idx_nodemap_add(nodemap);
1571 rc = lprocfs_nodemap_register(nodemap, 0);
1573 mutex_unlock(&active_config_lock);
1574 nodemap_putref(nodemap);
1578 EXPORT_SYMBOL(nodemap_add);
1583 * \param name name of nodemmap
1585 * \retval -EINVAL invalid input
1586 * \retval -ENOENT no existing nodemap
1588 int nodemap_del(const char *nodemap_name)
1590 struct lu_nodemap *nodemap;
1591 struct lu_nid_range *range;
1592 struct lu_nid_range *range_temp;
1596 if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1599 mutex_lock(&active_config_lock);
1600 nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1602 if (nodemap == NULL) {
1603 mutex_unlock(&active_config_lock);
1604 GOTO(out, rc = -ENOENT);
1607 /* erase nodemap from active ranges to prevent client assignment */
1608 down_write(&active_config->nmc_range_tree_lock);
1609 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1611 rc2 = nodemap_idx_range_del(range);
1615 range_delete(&active_config->nmc_range_tree, range);
1617 up_write(&active_config->nmc_range_tree_lock);
1619 rc2 = nodemap_idx_nodemap_del(nodemap);
1624 * remove procfs here in case nodemap_create called with same name
1625 * before nodemap_destroy is run.
1627 lprocfs_nodemap_remove(nodemap->nm_pde_data);
1628 nodemap->nm_pde_data = NULL;
1630 /* reclassify all member exports from nodemap, so they put their refs */
1631 down_read(&active_config->nmc_range_tree_lock);
1632 nm_member_reclassify_nodemap(nodemap);
1633 up_read(&active_config->nmc_range_tree_lock);
1635 if (!list_empty(&nodemap->nm_member_list))
1636 CWARN("nodemap_del failed to reclassify all members\n");
1638 mutex_unlock(&active_config_lock);
1640 nodemap_putref(nodemap);
1645 EXPORT_SYMBOL(nodemap_del);
1648 * activate nodemap functions
1650 * \param value 1 for on, 0 for off
1652 void nodemap_activate(const bool value)
1654 mutex_lock(&active_config_lock);
1655 active_config->nmc_nodemap_is_active = value;
1657 /* copy active value to global to avoid locking in map functions */
1658 nodemap_active = value;
1659 nodemap_idx_nodemap_activate(value);
1660 mutex_unlock(&active_config_lock);
1661 nm_member_revoke_all();
1663 EXPORT_SYMBOL(nodemap_activate);
1666 * Helper iterator to convert nodemap hash to list.
1668 * \param hs hash structure
1669 * \param bd bucket descriptor
1670 * \param hnode hash node
1671 * \param nodemap_list_head list head for list of nodemaps in hash
1673 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1674 struct hlist_node *hnode,
1675 void *nodemap_list_head)
1677 struct lu_nodemap *nodemap;
1679 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1680 list_add(&nodemap->nm_list, nodemap_list_head);
1682 cfs_hash_bd_del_locked(hs, bd, hnode);
1687 struct nodemap_config *nodemap_config_alloc(void)
1689 struct nodemap_config *config;
1692 OBD_ALLOC_PTR(config);
1694 return ERR_PTR(-ENOMEM);
1696 rc = nodemap_init_hash(config);
1698 OBD_FREE_PTR(config);
1702 init_rwsem(&config->nmc_range_tree_lock);
1704 config->nmc_range_tree.nmrt_range_interval_root = INTERVAL_TREE_ROOT;
1708 EXPORT_SYMBOL(nodemap_config_alloc);
1711 * Walk the nodemap_hash and remove all nodemaps.
1713 void nodemap_config_dealloc(struct nodemap_config *config)
1715 struct lu_nodemap *nodemap = NULL;
1716 struct lu_nodemap *nodemap_temp;
1717 struct lu_nid_range *range;
1718 struct lu_nid_range *range_temp;
1719 LIST_HEAD(nodemap_list_head);
1721 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1722 nodemap_cleanup_iter_cb, &nodemap_list_head);
1723 cfs_hash_putref(config->nmc_nodemap_hash);
1725 /* Because nodemap_destroy might sleep, we can't destroy them
1726 * in cfs_hash_for_each, so we build a list there and destroy here
1728 list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1730 mutex_lock(&active_config_lock);
1731 down_write(&config->nmc_range_tree_lock);
1733 /* move members to new config, requires ac lock */
1734 nm_member_reclassify_nodemap(nodemap);
1735 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1737 range_delete(&config->nmc_range_tree, range);
1738 up_write(&config->nmc_range_tree_lock);
1739 mutex_unlock(&active_config_lock);
1741 /* putref must be outside of ac lock if nm could be destroyed */
1742 nodemap_putref(nodemap);
1744 OBD_FREE_PTR(config);
1746 EXPORT_SYMBOL(nodemap_config_dealloc);
1749 * callback for cfs_hash_for_each_safe used to convert a nodemap hash to a
1750 * nodemap list, generally for locking purposes as a hash cb can't sleep.
1752 int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1753 struct hlist_node *hnode,
1754 void *nodemap_list_head)
1756 struct lu_nodemap *nodemap;
1758 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1759 list_add(&nodemap->nm_list, nodemap_list_head);
1763 void nodemap_config_set_active(struct nodemap_config *config)
1765 struct nodemap_config *old_config = active_config;
1766 struct lu_nodemap *nodemap;
1767 struct lu_nodemap *tmp;
1769 LIST_HEAD(nodemap_list_head);
1773 LASSERT(active_config != config);
1774 LASSERT(config->nmc_default_nodemap);
1776 mutex_lock(&active_config_lock);
1778 /* move proc entries from already existing nms, create for new nms */
1779 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1780 nm_hash_list_cb, &nodemap_list_head);
1781 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1782 struct lu_nodemap *old_nm = NULL;
1784 if (active_config != NULL)
1785 old_nm = cfs_hash_lookup(
1786 active_config->nmc_nodemap_hash,
1788 if (old_nm != NULL) {
1789 nodemap->nm_pde_data = old_nm->nm_pde_data;
1790 old_nm->nm_pde_data = NULL;
1791 nodemap_putref(old_nm);
1793 bool is_def = (nodemap == config->nmc_default_nodemap);
1795 lprocfs_nodemap_register(nodemap, is_def);
1800 * We only need to revoke locks if old nodemap was active, and new
1801 * config is now nodemap inactive. nodemap_config_dealloc will
1802 * reclassify exports, triggering a lock revoke if and only if new
1803 * nodemap is active.
1805 revoke_locks = !config->nmc_nodemap_is_active && nodemap_active;
1807 /* if new config is inactive, deactivate live config before switching */
1808 if (!config->nmc_nodemap_is_active)
1809 nodemap_active = false;
1810 active_config = config;
1811 if (config->nmc_nodemap_is_active)
1812 nodemap_active = true;
1814 mutex_unlock(&active_config_lock);
1816 if (old_config != NULL)
1817 nodemap_config_dealloc(old_config);
1820 nm_member_revoke_all();
1826 * Cleanup nodemap module on exit
1828 void nodemap_mod_exit(void)
1830 nodemap_config_dealloc(active_config);
1831 nodemap_procfs_exit();
1835 * Initialize the nodemap module
1837 int nodemap_mod_init(void)
1839 struct nodemap_config *new_config;
1840 struct lu_nodemap *nodemap;
1843 rc = nodemap_procfs_init();
1847 new_config = nodemap_config_alloc();
1848 if (IS_ERR(new_config)) {
1849 nodemap_procfs_exit();
1850 GOTO(out, rc = PTR_ERR(new_config));
1853 nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1854 if (IS_ERR(nodemap)) {
1855 nodemap_config_dealloc(new_config);
1856 nodemap_procfs_exit();
1857 GOTO(out, rc = PTR_ERR(nodemap));
1860 nodemap_config_set_active(new_config);
1861 nodemap_putref(nodemap);
1868 * Revoke locks for all nodemaps.
1870 void nm_member_revoke_all(void)
1872 struct lu_nodemap *nodemap;
1873 struct lu_nodemap *tmp;
1874 LIST_HEAD(nodemap_list_head);
1876 mutex_lock(&active_config_lock);
1877 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1878 nm_hash_list_cb, &nodemap_list_head);
1880 /* revoke_locks sleeps, so can't call in cfs hash cb */
1881 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1882 nm_member_revoke_locks_always(nodemap);
1883 mutex_unlock(&active_config_lock);
1887 * Returns the nodemap classification for a given nid into an ioctl buffer.
1888 * Useful for testing the nodemap configuration to make sure it is working as
1891 * \param nid nid to classify
1892 * \param[out] name_buf buffer to write the nodemap name to
1893 * \param name_len length of buffer
1895 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1897 struct lu_nodemap *nodemap;
1899 mutex_lock(&active_config_lock);
1900 down_read(&active_config->nmc_range_tree_lock);
1901 nodemap = nodemap_classify_nid(nid);
1902 up_read(&active_config->nmc_range_tree_lock);
1903 mutex_unlock(&active_config_lock);
1905 if (IS_ERR(nodemap))
1908 strncpy(name_buf, nodemap->nm_name, name_len);
1910 name_buf[name_len - 1] = '\0';
1912 nodemap_putref(nodemap);
1914 EXPORT_SYMBOL(nodemap_test_nid);
1917 * Passes back the id mapping for a given nid/id pair. Useful for testing the
1918 * nodemap configuration to make sure it is working as expected.
1920 * \param nid nid to classify
1921 * \param idtype uid or gid
1922 * \param client_id id to map to fs
1923 * \param fs_id_buf pointer to save mapped fs_id to
1926 * \retval -EINVAL invalid NID
1928 int nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1929 __u32 client_id, __u32 *fs_id)
1931 struct lu_nodemap *nodemap;
1933 mutex_lock(&active_config_lock);
1934 down_read(&active_config->nmc_range_tree_lock);
1935 nodemap = nodemap_classify_nid(nid);
1936 up_read(&active_config->nmc_range_tree_lock);
1937 mutex_unlock(&active_config_lock);
1939 if (IS_ERR(nodemap))
1940 return PTR_ERR(nodemap);
1942 *fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1944 nodemap_putref(nodemap);
1948 EXPORT_SYMBOL(nodemap_test_id);