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) 2014, Intel Corporation.
27 * Author: Joshua Walgenbach <jjw@iu.edu>
29 #include <linux/module.h>
30 #include <linux/sort.h>
31 #include <lnet/nidstr.h>
32 #include <lustre_net.h>
33 #include <lustre_acl.h>
34 #include <lustre_eacl.h>
35 #include <obd_class.h>
36 #include "nodemap_internal.h"
38 #define HASH_NODEMAP_BKT_BITS 3
39 #define HASH_NODEMAP_CUR_BITS 3
40 #define HASH_NODEMAP_MAX_BITS 7
42 #define DEFAULT_NODEMAP "default"
44 /* nodemap proc root proc directory under fs/lustre */
45 struct proc_dir_entry *proc_lustre_nodemap_root;
47 /* Copy of config active flag to avoid locking in mapping functions */
50 /* Lock protecting the active config, useful primarily when proc and
51 * nodemap_hash might be replaced when loading a new config
52 * Any time the active config is referenced, the lock should be held.
54 DEFINE_MUTEX(active_config_lock);
55 struct nodemap_config *active_config;
60 * \param nodemap nodemap to destroy
62 static void nodemap_destroy(struct lu_nodemap *nodemap)
64 if (nodemap->nm_pde_data != NULL)
65 lprocfs_nodemap_remove(nodemap->nm_pde_data);
67 mutex_lock(&active_config_lock);
68 down_read(&active_config->nmc_range_tree_lock);
69 nm_member_reclassify_nodemap(nodemap);
70 up_read(&active_config->nmc_range_tree_lock);
71 mutex_unlock(&active_config_lock);
73 if (!list_empty(&nodemap->nm_member_list))
74 CWARN("nodemap_destroy failed to reclassify all members\n");
76 write_lock(&nodemap->nm_idmap_lock);
77 idmap_delete_tree(nodemap);
78 write_unlock(&nodemap->nm_idmap_lock);
80 nm_member_delete_list(nodemap);
82 OBD_FREE_PTR(nodemap);
86 * Functions used for the cfs_hash
88 static void nodemap_getref(struct lu_nodemap *nodemap)
90 atomic_inc(&nodemap->nm_refcount);
94 * Destroy nodemap if last reference is put. Should be called outside
97 void nodemap_putref(struct lu_nodemap *nodemap)
99 LASSERT(nodemap != NULL);
100 LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
102 if (atomic_dec_and_test(&nodemap->nm_refcount))
103 nodemap_destroy(nodemap);
106 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
107 const void *key, unsigned mask)
109 return cfs_hash_djb2_hash(key, strlen(key), mask);
112 static void *nodemap_hs_key(struct hlist_node *hnode)
114 struct lu_nodemap *nodemap;
116 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
118 return nodemap->nm_name;
121 static int nodemap_hs_keycmp(const void *key,
122 struct hlist_node *compared_hnode)
126 nodemap_name = nodemap_hs_key(compared_hnode);
128 return !strcmp(key, nodemap_name);
131 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
133 return hlist_entry(hnode, struct lu_nodemap, nm_hash);
136 static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode)
138 struct lu_nodemap *nodemap;
140 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
141 nodemap_getref(nodemap);
144 static void nodemap_hs_put_locked(struct cfs_hash *hs,
145 struct hlist_node *hnode)
147 struct lu_nodemap *nodemap;
149 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
150 nodemap_putref(nodemap);
153 static struct cfs_hash_ops nodemap_hash_operations = {
154 .hs_hash = nodemap_hashfn,
155 .hs_key = nodemap_hs_key,
156 .hs_keycmp = nodemap_hs_keycmp,
157 .hs_object = nodemap_hs_hashobject,
158 .hs_get = nodemap_hs_get,
159 .hs_put_locked = nodemap_hs_put_locked,
162 /* end of cfs_hash functions */
165 * Initialize nodemap_hash
168 * \retval -ENOMEM cannot create hash
170 static int nodemap_init_hash(struct nodemap_config *nmc)
172 nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP",
173 HASH_NODEMAP_CUR_BITS,
174 HASH_NODEMAP_MAX_BITS,
175 HASH_NODEMAP_BKT_BITS, 0,
178 &nodemap_hash_operations,
181 if (nmc->nmc_nodemap_hash == NULL) {
182 CERROR("cannot create nodemap_hash table\n");
190 * Check for valid nodemap name
192 * \param name nodemap name
194 * \retval false invalid
196 static bool nodemap_name_is_valid(const char *name)
198 if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
202 for (; *name != '\0'; name++) {
203 if (!isalnum(*name) && *name != '_')
213 * Look nodemap up in the active_config nodemap hash. Caller should hold the
214 * active_config_lock.
216 * \param name name of nodemap
217 * \retval nodemap pointer set to found nodemap
218 * \retval -EINVAL name is not valid
219 * \retval -ENOENT nodemap not found
221 struct lu_nodemap *nodemap_lookup(const char *name)
223 struct lu_nodemap *nodemap = NULL;
225 if (!nodemap_name_is_valid(name))
226 return ERR_PTR(-EINVAL);
228 nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name);
230 return ERR_PTR(-ENOENT);
236 * Classify the nid into the proper nodemap. Caller must hold active config and
237 * nm_range_tree_lock, and call nodemap_putref when done with nodemap.
239 * \param nid nid to classify
240 * \retval nodemap nodemap containing the nid
241 * \retval default_nodemap default nodemap
243 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
245 struct lu_nid_range *range;
246 struct lu_nodemap *nodemap;
248 range = range_search(&active_config->nmc_range_tree, nid);
250 nodemap = range->rn_nodemap;
252 nodemap = active_config->nmc_default_nodemap;
254 nodemap_getref(nodemap);
260 * simple check for default nodemap
262 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
264 return nodemap->nm_id == 0;
268 * parse a nodemap range string into two nids
270 * \param range_str string to parse
271 * \param range[2] array of two nids
272 * \reyval 0 on success
274 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
276 char buf[LNET_NIDSTR_SIZE * 2 + 2];
282 snprintf(buf, sizeof(buf), "%s", range_str);
284 start_nidstr = strsep(&ptr, ":");
285 end_nidstr = strsep(&ptr, ":");
287 if (start_nidstr == NULL || end_nidstr == NULL)
288 GOTO(out, rc = -EINVAL);
290 range[0] = libcfs_str2nid(start_nidstr);
291 range[1] = libcfs_str2nid(end_nidstr);
297 EXPORT_SYMBOL(nodemap_parse_range);
300 * parse a string containing an id map of form "client_id:filesystem_id"
301 * into an array of __u32 * for use in mapping functions
303 * \param idmap_str map string
304 * \param idmap array[2] of __u32
306 * \retval 0 on success
307 * \retval -EINVAL if idmap cannot be parsed
309 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
312 long unsigned int idmap_buf;
315 if (idmap_str == NULL)
318 sep = strchr(idmap_str, ':');
324 rc = kstrtoul(idmap_str, 10, &idmap_buf);
327 idmap[0] = idmap_buf;
329 rc = kstrtoul(sep, 10, &idmap_buf);
332 idmap[1] = idmap_buf;
336 EXPORT_SYMBOL(nodemap_parse_idmap);
339 * add a member to a nodemap
341 * \param nid nid to add to the members
342 * \param exp obd_export structure for the connection
343 * that is being added
344 * \retval -EINVAL export is NULL
345 * \retval -EEXIST export is already member of a nodemap
347 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
349 struct lu_nodemap *nodemap;
352 mutex_lock(&active_config_lock);
353 down_read(&active_config->nmc_range_tree_lock);
355 nodemap = nodemap_classify_nid(nid);
356 rc = nm_member_add(nodemap, exp);
358 up_read(&active_config->nmc_range_tree_lock);
359 mutex_unlock(&active_config_lock);
361 nodemap_putref(nodemap);
365 EXPORT_SYMBOL(nodemap_add_member);
368 * delete a member from a nodemap
370 * \param exp export to remove from a nodemap
372 void nodemap_del_member(struct obd_export *exp)
374 struct lu_nodemap *nodemap = exp->exp_target_data.ted_nodemap;
377 nm_member_del(nodemap, exp);
379 EXPORT_SYMBOL(nodemap_del_member);
382 * add an idmap to the proper nodemap trees
384 * \param name name of nodemap
385 * \param id_type NODEMAP_UID or NODEMAP_GID
386 * \param map array[2] __u32 containing the map values
387 * map[0] is client id
388 * map[1] is the filesystem id
390 * \retval 0 on success
392 static int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
393 enum nodemap_id_type id_type,
396 struct lu_idmap *idmap;
399 idmap = idmap_create(map[0], map[1]);
401 GOTO(out, rc = -ENOMEM);
403 write_lock(&nodemap->nm_idmap_lock);
404 idmap_insert(id_type, idmap, nodemap);
405 write_unlock(&nodemap->nm_idmap_lock);
406 nm_member_revoke_locks(nodemap);
411 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
414 struct lu_nodemap *nodemap = NULL;
417 mutex_lock(&active_config_lock);
418 nodemap = nodemap_lookup(name);
419 if (IS_ERR(nodemap)) {
420 mutex_unlock(&active_config_lock);
421 GOTO(out, rc = PTR_ERR(nodemap));
424 if (is_default_nodemap(nodemap))
427 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
429 mutex_unlock(&active_config_lock);
431 nodemap_putref(nodemap);
436 EXPORT_SYMBOL(nodemap_add_idmap);
439 * delete idmap from proper nodemap tree
441 * \param name name of nodemap
442 * \param id_type NODEMAP_UID or NODEMAP_GID
443 * \param map array[2] __u32 containing the mapA values
444 * map[0] is client id
445 * map[1] is the filesystem id
447 * \retval 0 on success
449 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
452 struct lu_nodemap *nodemap = NULL;
453 struct lu_idmap *idmap = NULL;
456 mutex_lock(&active_config_lock);
457 nodemap = nodemap_lookup(name);
458 if (IS_ERR(nodemap)) {
459 mutex_unlock(&active_config_lock);
460 GOTO(out, rc = PTR_ERR(nodemap));
463 if (is_default_nodemap(nodemap))
464 GOTO(out_putref, rc = -EINVAL);
466 write_lock(&nodemap->nm_idmap_lock);
467 idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
472 idmap_delete(id_type, idmap, nodemap);
473 write_unlock(&nodemap->nm_idmap_lock);
476 mutex_unlock(&active_config_lock);
478 nm_member_revoke_locks(nodemap);
479 nodemap_putref(nodemap);
484 EXPORT_SYMBOL(nodemap_del_idmap);
487 * mapping function for nodemap idmaps
489 * \param nodemap lu_nodemap structure defining nodemap
490 * \param node_type NODEMAP_UID or NODEMAP_GID
491 * \param tree_type NODEMAP_CLIENT_TO_FS or
492 * NODEMAP_FS_TO_CLIENT
493 * \param id id to map
495 * \retval mapped id according to the rules below.
497 * if the nodemap_active is false, just return the passed id without mapping
499 * if the id to be looked up is 0, check that root access is allowed and if it
500 * is, return 0. Otherwise, return the squash uid or gid.
502 * if the nodemap is configured to trusted the ids from the client system, just
503 * return the passwd id without mapping.
505 * if by this point, we haven't returned and the nodemap in question is the
506 * default nodemap, return the squash uid or gid.
508 * after these checks, search the proper tree for the mapping, and if found
509 * return the mapped value, otherwise return the squash uid or gid.
511 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
512 enum nodemap_id_type id_type,
513 enum nodemap_tree_type tree_type, __u32 id)
515 struct lu_idmap *idmap = NULL;
521 if (unlikely(nodemap == NULL))
525 if (nodemap->nmf_allow_root_access)
531 if (nodemap->nmf_trust_client_ids)
534 if (is_default_nodemap(nodemap))
537 read_lock(&nodemap->nm_idmap_lock);
538 idmap = idmap_search(nodemap, tree_type, id_type, id);
540 read_unlock(&nodemap->nm_idmap_lock);
544 if (tree_type == NODEMAP_FS_TO_CLIENT)
545 found_id = idmap->id_client;
547 found_id = idmap->id_fs;
548 read_unlock(&nodemap->nm_idmap_lock);
552 if (id_type == NODEMAP_UID)
553 return nodemap->nm_squash_uid;
555 return nodemap->nm_squash_gid;
559 EXPORT_SYMBOL(nodemap_map_id);
562 * Map posix ACL entries according to the nodemap membership. Removes any
565 * \param lu_nodemap nodemap
566 * \param buf buffer containing xattr encoded ACLs
567 * \param size size of ACLs in bytes
568 * \param tree_type direction of mapping
569 * \retval size new size of ACLs in bytes
570 * \retval -EINVAL bad \a size param, see posix_acl_xattr_count()
572 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
573 enum nodemap_tree_type tree_type)
575 posix_acl_xattr_header *header = buf;
576 posix_acl_xattr_entry *entry = &header->a_entries[0];
577 posix_acl_xattr_entry *new_entry = entry;
578 posix_acl_xattr_entry *end;
584 if (unlikely(nodemap == NULL))
587 count = posix_acl_xattr_count(size);
593 for (end = entry + count; entry != end; entry++) {
594 __u16 tag = le16_to_cpu(entry->e_tag);
595 __u32 id = le32_to_cpu(entry->e_id);
599 id = nodemap_map_id(nodemap, NODEMAP_UID,
601 if (id == nodemap->nm_squash_uid)
603 entry->e_id = cpu_to_le32(id);
606 id = nodemap_map_id(nodemap, NODEMAP_GID,
608 if (id == nodemap->nm_squash_gid)
610 entry->e_id = cpu_to_le32(id);
614 /* if we skip an ACL, copy the following ones over it */
615 if (new_entry != entry)
621 return (void *)new_entry - (void *)header;
623 EXPORT_SYMBOL(nodemap_map_acl);
626 * add nid range to nodemap
627 * \param nodemap nodemap to add range to
628 * \param range_st string containing nid range
629 * \retval 0 on success
631 * add an range to the global range tree and attached the
632 * range to the named nodemap.
634 static int nodemap_add_range_helper(struct nodemap_config *config,
635 struct lu_nodemap *nodemap,
636 const lnet_nid_t nid[2])
638 struct lu_nid_range *range;
641 down_write(&config->nmc_range_tree_lock);
642 range = range_create(&config->nmc_range_tree, nid[0], nid[1], nodemap);
644 up_write(&config->nmc_range_tree_lock);
645 GOTO(out, rc = -ENOMEM);
648 rc = range_insert(&config->nmc_range_tree, range);
650 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
651 nodemap->nm_name, rc);
652 up_write(&config->nmc_range_tree_lock);
653 list_del(&range->rn_list);
654 range_destroy(range);
655 GOTO(out, rc = -ENOMEM);
658 list_add(&range->rn_list, &nodemap->nm_ranges);
659 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
660 up_write(&config->nmc_range_tree_lock);
662 nm_member_revoke_locks(config->nmc_default_nodemap);
663 nm_member_revoke_locks(nodemap);
668 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
670 struct lu_nodemap *nodemap = NULL;
673 mutex_lock(&active_config_lock);
674 nodemap = nodemap_lookup(name);
675 if (IS_ERR(nodemap)) {
676 mutex_unlock(&active_config_lock);
677 GOTO(out, rc = PTR_ERR(nodemap));
680 if (is_default_nodemap(nodemap))
683 rc = nodemap_add_range_helper(active_config, nodemap, nid);
684 mutex_unlock(&active_config_lock);
686 nodemap_putref(nodemap);
690 EXPORT_SYMBOL(nodemap_add_range);
694 * \param name nodemap name
695 * \param range_str string containing range
696 * \retval 0 on success
698 * Delete range from global range tree, and remove it
699 * from the list in the associated nodemap.
701 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
703 struct lu_nodemap *nodemap;
704 struct lu_nid_range *range;
707 mutex_lock(&active_config_lock);
708 nodemap = nodemap_lookup(name);
709 if (IS_ERR(nodemap)) {
710 mutex_unlock(&active_config_lock);
711 GOTO(out, rc = PTR_ERR(nodemap));
714 if (is_default_nodemap(nodemap))
715 GOTO(out_putref, rc = -EINVAL);
717 down_write(&active_config->nmc_range_tree_lock);
718 range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
720 up_write(&active_config->nmc_range_tree_lock);
721 GOTO(out_putref, rc = -EINVAL);
723 range_delete(&active_config->nmc_range_tree, range);
724 nm_member_reclassify_nodemap(nodemap);
725 up_write(&active_config->nmc_range_tree_lock);
727 nm_member_revoke_locks(active_config->nmc_default_nodemap);
728 nm_member_revoke_locks(nodemap);
731 mutex_unlock(&active_config_lock);
732 nodemap_putref(nodemap);
736 EXPORT_SYMBOL(nodemap_del_range);
739 * Nodemap constructor
741 * Creates an lu_nodemap structure and assigns sane default
742 * member values. If this is the default nodemap, the defaults
743 * are the most restictive in xterms of mapping behavior. Otherwise
744 * the default flags should be inherited from the default nodemap.
745 * The adds nodemap to nodemap_hash.
747 * Requires that the caller take the active_config_lock
749 * \param name name of nodemap
750 * \param is_default true if default nodemap
751 * \retval nodemap success
752 * \retval -EINVAL invalid nodemap name
753 * \retval -EEXIST nodemap already exists
754 * \retval -ENOMEM cannot allocate memory for nodemap
756 struct lu_nodemap *nodemap_create(const char *name,
757 struct nodemap_config *config,
760 struct lu_nodemap *nodemap = NULL;
761 struct cfs_hash *hash = config->nmc_nodemap_hash;
764 if (!nodemap_name_is_valid(name))
765 GOTO(out, rc = -EINVAL);
768 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
769 GOTO(out, rc = -EINVAL);
772 OBD_ALLOC_PTR(nodemap);
773 if (nodemap == NULL) {
774 CERROR("cannot allocate memory (%zu bytes)"
775 "for nodemap '%s'\n", sizeof(*nodemap),
777 GOTO(out, rc = -ENOMEM);
781 * take an extra reference to prevent nodemap from being destroyed
782 * while it's being created.
784 atomic_set(&nodemap->nm_refcount, 2);
785 snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
786 rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
788 OBD_FREE_PTR(nodemap);
789 GOTO(out, rc = -EEXIST);
792 INIT_LIST_HEAD(&nodemap->nm_ranges);
793 INIT_LIST_HEAD(&nodemap->nm_list);
794 INIT_LIST_HEAD(&nodemap->nm_member_list);
796 mutex_init(&nodemap->nm_member_list_lock);
797 rwlock_init(&nodemap->nm_idmap_lock);
798 nodemap->nm_fs_to_client_uidmap = RB_ROOT;
799 nodemap->nm_client_to_fs_uidmap = RB_ROOT;
800 nodemap->nm_fs_to_client_gidmap = RB_ROOT;
801 nodemap->nm_client_to_fs_gidmap = RB_ROOT;
804 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
805 nodemap->nmf_trust_client_ids = 0;
806 nodemap->nmf_allow_root_access = 0;
807 nodemap->nmf_block_lookups = 0;
809 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
810 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
812 config->nmc_default_nodemap = nodemap;
814 struct lu_nodemap *default_nodemap =
815 config->nmc_default_nodemap;
817 config->nmc_nodemap_highest_id++;
818 nodemap->nm_id = config->nmc_nodemap_highest_id;
819 nodemap->nmf_trust_client_ids =
820 default_nodemap->nmf_trust_client_ids;
821 nodemap->nmf_allow_root_access =
822 default_nodemap->nmf_allow_root_access;
823 nodemap->nmf_block_lookups =
824 default_nodemap->nmf_block_lookups;
826 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
827 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
833 CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
838 * update flag to turn on or off nodemap functions
839 * \param name nodemap name
840 * \param admin_string string containing updated value
841 * \retval 0 on success
843 * Update admin flag to turn on or off nodemap functions.
845 int nodemap_set_allow_root(const char *name, bool allow_root)
847 struct lu_nodemap *nodemap = NULL;
850 mutex_lock(&active_config_lock);
851 nodemap = nodemap_lookup(name);
852 mutex_unlock(&active_config_lock);
854 GOTO(out, rc = PTR_ERR(nodemap));
856 nodemap->nmf_allow_root_access = allow_root;
858 nm_member_revoke_locks(nodemap);
859 nodemap_putref(nodemap);
863 EXPORT_SYMBOL(nodemap_set_allow_root);
866 * updated trust_client_ids flag for nodemap
868 * \param name nodemap name
869 * \param trust_string new value for trust flag
870 * \retval 0 on success
872 * Update the trust_client_ids flag for a nodemap.
874 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
876 struct lu_nodemap *nodemap = NULL;
879 mutex_lock(&active_config_lock);
880 nodemap = nodemap_lookup(name);
881 mutex_unlock(&active_config_lock);
883 GOTO(out, rc = PTR_ERR(nodemap));
885 nodemap->nmf_trust_client_ids = trust_client_ids;
887 nm_member_revoke_locks(nodemap);
888 nodemap_putref(nodemap);
892 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
895 * update the squash_uid for a nodemap
897 * \param name nodemap name
898 * \param uid_string string containing new squash_uid value
899 * \retval 0 on success
901 * Update the squash_uid for a nodemap. The squash_uid is the uid
902 * that the all client uids are mapped to if nodemap is active,
903 * the trust_client_ids flag is not set, and the uid is not in
906 int nodemap_set_squash_uid(const char *name, uid_t uid)
908 struct lu_nodemap *nodemap = NULL;
911 mutex_lock(&active_config_lock);
912 nodemap = nodemap_lookup(name);
913 mutex_unlock(&active_config_lock);
915 GOTO(out, rc = PTR_ERR(nodemap));
917 nodemap->nm_squash_uid = uid;
919 nm_member_revoke_locks(nodemap);
920 nodemap_putref(nodemap);
924 EXPORT_SYMBOL(nodemap_set_squash_uid);
927 * Update the squash_gid for a nodemap.
929 * \param name nodemap name
930 * \param gid_string string containing new squash_gid value
931 * \retval 0 on success
933 * Update the squash_gid for a nodemap. The squash_uid is the gid
934 * that the all client gids are mapped to if nodemap is active,
935 * the trust_client_ids flag is not set, and the gid is not in
938 int nodemap_set_squash_gid(const char *name, gid_t gid)
940 struct lu_nodemap *nodemap = NULL;
943 mutex_lock(&active_config_lock);
944 nodemap = nodemap_lookup(name);
945 mutex_unlock(&active_config_lock);
947 GOTO(out, rc = PTR_ERR(nodemap));
949 nodemap->nm_squash_gid = gid;
951 nm_member_revoke_locks(nodemap);
952 nodemap_putref(nodemap);
956 EXPORT_SYMBOL(nodemap_set_squash_gid);
959 * Returns true if this nodemap has root user access. Always returns true if
960 * nodemaps are not active.
962 * \param nodemap nodemap to check access for
964 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
966 return !nodemap_active || nodemap->nmf_allow_root_access;
968 EXPORT_SYMBOL(nodemap_can_setquota);
973 * \param name name of nodemap
975 * \retval -EINVAL invalid nodemap name
976 * \retval -EEXIST nodemap already exists
977 * \retval -ENOMEM cannot allocate memory for nodemap
979 int nodemap_add(const char *nodemap_name)
981 struct lu_nodemap *nodemap;
984 mutex_lock(&active_config_lock);
985 nodemap = nodemap_create(nodemap_name, active_config, 0);
986 if (IS_ERR(nodemap)) {
987 mutex_unlock(&active_config_lock);
988 return PTR_ERR(nodemap);
991 rc = lprocfs_nodemap_register(nodemap, 0);
992 mutex_unlock(&active_config_lock);
993 nodemap_putref(nodemap);
997 EXPORT_SYMBOL(nodemap_add);
1002 * \param name name of nodemmap
1004 * \retval -EINVAL invalid input
1005 * \retval -ENOENT no existing nodemap
1007 int nodemap_del(const char *nodemap_name)
1009 struct lu_nodemap *nodemap;
1010 struct lu_nid_range *range;
1011 struct lu_nid_range *range_temp;
1014 if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1017 mutex_lock(&active_config_lock);
1018 nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1020 if (nodemap == NULL) {
1021 mutex_unlock(&active_config_lock);
1022 GOTO(out, rc = -ENOENT);
1025 /* erase nodemap from active ranges to prevent client assignment */
1026 down_write(&active_config->nmc_range_tree_lock);
1027 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1029 range_delete(&active_config->nmc_range_tree, range);
1030 up_write(&active_config->nmc_range_tree_lock);
1033 * remove procfs here in case nodemap_create called with same name
1034 * before nodemap_destroy is run.
1036 lprocfs_nodemap_remove(nodemap->nm_pde_data);
1037 nodemap->nm_pde_data = NULL;
1038 mutex_unlock(&active_config_lock);
1040 nodemap_putref(nodemap);
1045 EXPORT_SYMBOL(nodemap_del);
1048 * activate nodemap functions
1050 * \param value 1 for on, 0 for off
1052 void nodemap_activate(const bool value)
1054 mutex_lock(&active_config_lock);
1055 active_config->nmc_nodemap_is_active = value;
1057 /* copy active value to global to avoid locking in map functions */
1058 nodemap_active = value;
1059 mutex_unlock(&active_config_lock);
1060 nm_member_revoke_all();
1062 EXPORT_SYMBOL(nodemap_activate);
1065 * Helper iterator to convert nodemap hash to list.
1067 * \param hs hash structure
1068 * \param bd bucket descriptor
1069 * \param hnode hash node
1070 * \param nodemap_list_head list head for list of nodemaps in hash
1072 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1073 struct hlist_node *hnode,
1074 void *nodemap_list_head)
1076 struct lu_nodemap *nodemap;
1078 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1079 list_add(&nodemap->nm_list, nodemap_list_head);
1081 cfs_hash_bd_del_locked(hs, bd, hnode);
1087 * Walk the nodemap_hash and remove all nodemaps.
1089 void nodemap_config_cleanup(struct nodemap_config *config)
1091 struct lu_nodemap *nodemap = NULL;
1092 struct lu_nodemap *nodemap_temp;
1093 struct lu_nid_range *range;
1094 struct lu_nid_range *range_temp;
1095 LIST_HEAD(nodemap_list_head);
1097 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1098 nodemap_cleanup_iter_cb, &nodemap_list_head);
1099 cfs_hash_putref(config->nmc_nodemap_hash);
1101 /* Because nodemap_destroy might sleep, we can't destroy them
1102 * in cfs_hash_for_each, so we build a list there and destroy here
1104 list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1106 down_write(&config->nmc_range_tree_lock);
1107 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1109 range_delete(&config->nmc_range_tree, range);
1110 up_write(&config->nmc_range_tree_lock);
1112 nodemap_putref(nodemap);
1116 struct nodemap_config *nodemap_config_alloc(void)
1118 struct nodemap_config *config;
1121 OBD_ALLOC_PTR(config);
1123 return ERR_PTR(-ENOMEM);
1125 rc = nodemap_init_hash(config);
1127 OBD_FREE_PTR(config);
1131 init_rwsem(&config->nmc_range_tree_lock);
1136 void nodemap_config_dealloc(struct nodemap_config *config)
1138 nodemap_config_cleanup(config);
1139 OBD_FREE_PTR(config);
1142 static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1143 struct hlist_node *hnode,
1144 void *nodemap_list_head)
1146 struct lu_nodemap *nodemap;
1148 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1149 list_add(&nodemap->nm_list, nodemap_list_head);
1153 void nodemap_config_set_active(struct nodemap_config *config)
1155 struct nodemap_config *old_config = active_config;
1156 struct lu_nodemap *nodemap;
1157 struct lu_nodemap *tmp;
1158 LIST_HEAD(nodemap_list_head);
1162 LASSERT(active_config != config);
1164 mutex_lock(&active_config_lock);
1166 /* move proc entries from already existing nms, create for new nms */
1167 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1168 nm_hash_list_cb, &nodemap_list_head);
1169 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1170 struct lu_nodemap *old_nm = NULL;
1172 if (active_config != NULL)
1173 old_nm = cfs_hash_lookup(
1174 active_config->nmc_nodemap_hash,
1176 if (old_nm != NULL) {
1177 nodemap->nm_pde_data = old_nm->nm_pde_data;
1178 old_nm->nm_pde_data = NULL;
1179 nodemap_putref(old_nm);
1181 bool is_def = (nodemap == config->nmc_default_nodemap);
1183 lprocfs_nodemap_register(nodemap, is_def);
1187 /* if new config is inactive, deactivate live config before switching */
1188 if (!config->nmc_nodemap_is_active)
1189 nodemap_active = false;
1190 active_config = config;
1191 if (config->nmc_nodemap_is_active)
1192 nodemap_active = true;
1194 mutex_unlock(&active_config_lock);
1196 if (old_config != NULL)
1197 nodemap_config_dealloc(old_config);
1199 nm_member_revoke_all();
1205 * Cleanup nodemap module on exit
1207 void nodemap_mod_exit(void)
1209 nodemap_config_dealloc(active_config);
1210 nodemap_procfs_exit();
1214 * Initialize the nodemap module
1216 int nodemap_mod_init(void)
1218 struct nodemap_config *new_config;
1219 struct lu_nodemap *nodemap;
1222 rc = nodemap_procfs_init();
1226 new_config = nodemap_config_alloc();
1227 if (IS_ERR(new_config)) {
1228 nodemap_procfs_exit();
1229 GOTO(out, rc = PTR_ERR(new_config));
1232 nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1233 if (IS_ERR(nodemap)) {
1234 nodemap_config_dealloc(new_config);
1235 nodemap_procfs_exit();
1236 GOTO(out, rc = PTR_ERR(nodemap));
1239 nodemap_config_set_active(new_config);
1240 nodemap_putref(nodemap);
1247 * Revoke locks for all nodemaps.
1249 void nm_member_revoke_all(void)
1251 struct lu_nodemap *nodemap;
1252 struct lu_nodemap *tmp;
1253 LIST_HEAD(nodemap_list_head);
1255 mutex_lock(&active_config_lock);
1256 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1257 nm_hash_list_cb, &nodemap_list_head);
1259 /* revoke_locks sleeps, so can't call in cfs hash cb */
1260 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1261 nm_member_revoke_locks(nodemap);
1262 mutex_unlock(&active_config_lock);
1266 * Returns the nodemap classification for a given nid into an ioctl buffer.
1267 * Useful for testing the nodemap configuration to make sure it is working as
1270 * \param nid nid to classify
1271 * \param[out] name_buf buffer to write the nodemap name to
1272 * \param name_len length of buffer
1274 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1276 struct lu_nodemap *nodemap;
1278 mutex_lock(&active_config_lock);
1279 down_read(&active_config->nmc_range_tree_lock);
1280 nodemap = nodemap_classify_nid(nid);
1281 up_read(&active_config->nmc_range_tree_lock);
1282 mutex_unlock(&active_config_lock);
1284 strncpy(name_buf, nodemap->nm_name, name_len);
1286 name_buf[name_len - 1] = '\0';
1288 nodemap_putref(nodemap);
1290 EXPORT_SYMBOL(nodemap_test_nid);
1293 * Returns the id mapping for a given nid/id pair. Useful for testing the
1294 * nodemap configuration to make sure it is working as expected.
1296 * \param nid nid to classify
1297 * \param idtype uid or gid
1298 * \param client_id id to map to fs
1300 * \retval the mapped fs_id of the given client_id
1302 __u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1305 struct lu_nodemap *nodemap;
1308 mutex_lock(&active_config_lock);
1309 down_read(&active_config->nmc_range_tree_lock);
1310 nodemap = nodemap_classify_nid(nid);
1311 up_read(&active_config->nmc_range_tree_lock);
1312 mutex_unlock(&active_config_lock);
1314 fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1316 nodemap_putref(nodemap);
1320 EXPORT_SYMBOL(nodemap_test_id);