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)
66 if (nodemap->nm_pde_data != NULL)
67 lprocfs_nodemap_remove(nodemap->nm_pde_data);
69 mutex_lock(&active_config_lock);
70 down_read(&active_config->nmc_range_tree_lock);
71 nm_member_reclassify_nodemap(nodemap);
72 up_read(&active_config->nmc_range_tree_lock);
73 mutex_unlock(&active_config_lock);
75 if (!list_empty(&nodemap->nm_member_list))
76 CWARN("nodemap_destroy failed to reclassify all members\n");
78 write_lock(&nodemap->nm_idmap_lock);
79 idmap_delete_tree(nodemap);
80 write_unlock(&nodemap->nm_idmap_lock);
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);
98 * Destroy nodemap if last reference is put. Should be called outside
101 void nodemap_putref(struct lu_nodemap *nodemap)
106 LASSERT(atomic_read(&nodemap->nm_refcount) > 0);
108 if (atomic_dec_and_test(&nodemap->nm_refcount))
109 nodemap_destroy(nodemap);
111 EXPORT_SYMBOL(nodemap_putref);
113 static __u32 nodemap_hashfn(struct cfs_hash *hash_body,
114 const void *key, unsigned mask)
116 return cfs_hash_djb2_hash(key, strlen(key), mask);
119 static void *nodemap_hs_key(struct hlist_node *hnode)
121 struct lu_nodemap *nodemap;
123 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
125 return nodemap->nm_name;
128 static int nodemap_hs_keycmp(const void *key,
129 struct hlist_node *compared_hnode)
133 nodemap_name = nodemap_hs_key(compared_hnode);
135 return !strcmp(key, nodemap_name);
138 static void *nodemap_hs_hashobject(struct hlist_node *hnode)
140 return hlist_entry(hnode, struct lu_nodemap, nm_hash);
143 static void nodemap_hs_get(struct cfs_hash *hs, struct hlist_node *hnode)
145 struct lu_nodemap *nodemap;
147 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
148 nodemap_getref(nodemap);
151 static void nodemap_hs_put_locked(struct cfs_hash *hs,
152 struct hlist_node *hnode)
154 struct lu_nodemap *nodemap;
156 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
157 nodemap_putref(nodemap);
160 static struct cfs_hash_ops nodemap_hash_operations = {
161 .hs_hash = nodemap_hashfn,
162 .hs_key = nodemap_hs_key,
163 .hs_keycmp = nodemap_hs_keycmp,
164 .hs_object = nodemap_hs_hashobject,
165 .hs_get = nodemap_hs_get,
166 .hs_put_locked = nodemap_hs_put_locked,
169 /* end of cfs_hash functions */
172 * Initialize nodemap_hash
175 * \retval -ENOMEM cannot create hash
177 static int nodemap_init_hash(struct nodemap_config *nmc)
179 nmc->nmc_nodemap_hash = cfs_hash_create("NODEMAP",
180 HASH_NODEMAP_CUR_BITS,
181 HASH_NODEMAP_MAX_BITS,
182 HASH_NODEMAP_BKT_BITS, 0,
185 &nodemap_hash_operations,
188 if (nmc->nmc_nodemap_hash == NULL) {
189 CERROR("cannot create nodemap_hash table\n");
197 * Check for valid nodemap name
199 * \param name nodemap name
201 * \retval false invalid
203 static bool nodemap_name_is_valid(const char *name)
205 if (strlen(name) > LUSTRE_NODEMAP_NAME_LENGTH ||
209 for (; *name != '\0'; name++) {
210 if (!isalnum(*name) && *name != '_')
220 * Look nodemap up in the active_config nodemap hash. Caller should hold the
221 * active_config_lock.
223 * \param name name of nodemap
224 * \retval nodemap pointer set to found nodemap
225 * \retval -EINVAL name is not valid
226 * \retval -ENOENT nodemap not found
228 struct lu_nodemap *nodemap_lookup(const char *name)
230 struct lu_nodemap *nodemap = NULL;
232 if (!nodemap_name_is_valid(name))
233 return ERR_PTR(-EINVAL);
235 nodemap = cfs_hash_lookup(active_config->nmc_nodemap_hash, name);
237 return ERR_PTR(-ENOENT);
243 * Classify the nid into the proper nodemap. Caller must hold active config and
244 * nm_range_tree_lock, and call nodemap_putref when done with nodemap.
246 * \param nid nid to classify
247 * \retval nodemap nodemap containing the nid
248 * \retval default_nodemap default nodemap
250 struct lu_nodemap *nodemap_classify_nid(lnet_nid_t nid)
252 struct lu_nid_range *range;
253 struct lu_nodemap *nodemap;
255 range = range_search(&active_config->nmc_range_tree, nid);
257 nodemap = range->rn_nodemap;
259 nodemap = active_config->nmc_default_nodemap;
261 nodemap_getref(nodemap);
267 * simple check for default nodemap
269 static bool is_default_nodemap(const struct lu_nodemap *nodemap)
271 return nodemap->nm_id == 0;
275 * parse a nodemap range string into two nids
277 * \param range_str string to parse
278 * \param range[2] array of two nids
279 * \reyval 0 on success
281 int nodemap_parse_range(const char *range_str, lnet_nid_t range[2])
283 char buf[LNET_NIDSTR_SIZE * 2 + 2];
289 snprintf(buf, sizeof(buf), "%s", range_str);
291 start_nidstr = strsep(&ptr, ":");
292 end_nidstr = strsep(&ptr, ":");
294 if (start_nidstr == NULL || end_nidstr == NULL)
295 GOTO(out, rc = -EINVAL);
297 range[0] = libcfs_str2nid(start_nidstr);
298 range[1] = libcfs_str2nid(end_nidstr);
304 EXPORT_SYMBOL(nodemap_parse_range);
307 * parse a string containing an id map of form "client_id:filesystem_id"
308 * into an array of __u32 * for use in mapping functions
310 * \param idmap_str map string
311 * \param idmap array[2] of __u32
313 * \retval 0 on success
314 * \retval -EINVAL if idmap cannot be parsed
316 int nodemap_parse_idmap(char *idmap_str, __u32 idmap[2])
319 long unsigned int idmap_buf;
322 if (idmap_str == NULL)
325 sep = strchr(idmap_str, ':');
331 rc = kstrtoul(idmap_str, 10, &idmap_buf);
334 idmap[0] = idmap_buf;
336 rc = kstrtoul(sep, 10, &idmap_buf);
339 idmap[1] = idmap_buf;
343 EXPORT_SYMBOL(nodemap_parse_idmap);
346 * add a member to a nodemap
348 * \param nid nid to add to the members
349 * \param exp obd_export structure for the connection
350 * that is being added
351 * \retval -EINVAL export is NULL
352 * \retval -EEXIST export is already member of a nodemap
354 int nodemap_add_member(lnet_nid_t nid, struct obd_export *exp)
356 struct lu_nodemap *nodemap;
359 mutex_lock(&active_config_lock);
360 down_read(&active_config->nmc_range_tree_lock);
362 nodemap = nodemap_classify_nid(nid);
363 rc = nm_member_add(nodemap, exp);
365 up_read(&active_config->nmc_range_tree_lock);
366 mutex_unlock(&active_config_lock);
368 nodemap_putref(nodemap);
372 EXPORT_SYMBOL(nodemap_add_member);
375 * delete a member from a nodemap
377 * \param exp export to remove from a nodemap
379 void nodemap_del_member(struct obd_export *exp)
381 struct lu_nodemap *nodemap;
385 /* using ac lock to prevent nodemap reclassification while deleting */
386 mutex_lock(&active_config_lock);
388 /* use of ted_nodemap is protected by active_config_lock. we take an
389 * extra reference to make sure nodemap isn't destroyed under
392 nodemap = exp->exp_target_data.ted_nodemap;
396 nodemap_getref(nodemap);
398 mutex_lock(&nodemap->nm_member_list_lock);
399 nm_member_del(nodemap, exp);
400 mutex_unlock(&nodemap->nm_member_list_lock);
403 mutex_unlock(&active_config_lock);
406 nodemap_putref(nodemap);
410 EXPORT_SYMBOL(nodemap_del_member);
413 * add an idmap to the proper nodemap trees
415 * \param name name of nodemap
416 * \param id_type NODEMAP_UID or NODEMAP_GID
417 * \param map array[2] __u32 containing the map values
418 * map[0] is client id
419 * map[1] is the filesystem id
421 * \retval 0 on success
423 int nodemap_add_idmap_helper(struct lu_nodemap *nodemap,
424 enum nodemap_id_type id_type,
427 struct lu_idmap *idmap;
430 idmap = idmap_create(map[0], map[1]);
432 GOTO(out, rc = -ENOMEM);
434 write_lock(&nodemap->nm_idmap_lock);
435 idmap_insert(id_type, idmap, nodemap);
436 write_unlock(&nodemap->nm_idmap_lock);
437 nm_member_revoke_locks(nodemap);
443 int nodemap_add_idmap(const char *name, enum nodemap_id_type id_type,
446 struct lu_nodemap *nodemap = NULL;
449 mutex_lock(&active_config_lock);
450 nodemap = nodemap_lookup(name);
451 if (IS_ERR(nodemap)) {
452 mutex_unlock(&active_config_lock);
453 GOTO(out, rc = PTR_ERR(nodemap));
456 if (is_default_nodemap(nodemap)) {
459 rc = nodemap_add_idmap_helper(nodemap, id_type, map);
461 rc = nodemap_idx_idmap_add(nodemap, id_type, map);
463 mutex_unlock(&active_config_lock);
464 nodemap_putref(nodemap);
469 EXPORT_SYMBOL(nodemap_add_idmap);
472 * delete idmap from proper nodemap tree
474 * \param name name of nodemap
475 * \param id_type NODEMAP_UID or NODEMAP_GID
476 * \param map array[2] __u32 containing the mapA values
477 * map[0] is client id
478 * map[1] is the filesystem id
480 * \retval 0 on success
482 int nodemap_del_idmap(const char *name, enum nodemap_id_type id_type,
485 struct lu_nodemap *nodemap = NULL;
486 struct lu_idmap *idmap = NULL;
489 mutex_lock(&active_config_lock);
490 nodemap = nodemap_lookup(name);
491 if (IS_ERR(nodemap)) {
492 mutex_unlock(&active_config_lock);
493 GOTO(out, rc = PTR_ERR(nodemap));
496 if (is_default_nodemap(nodemap))
497 GOTO(out_putref, rc = -EINVAL);
499 write_lock(&nodemap->nm_idmap_lock);
500 idmap = idmap_search(nodemap, NODEMAP_CLIENT_TO_FS, id_type,
505 idmap_delete(id_type, idmap, nodemap);
506 rc = nodemap_idx_idmap_del(nodemap, id_type, map);
508 write_unlock(&nodemap->nm_idmap_lock);
511 mutex_unlock(&active_config_lock);
513 nm_member_revoke_locks(nodemap);
514 nodemap_putref(nodemap);
519 EXPORT_SYMBOL(nodemap_del_idmap);
522 * Get nodemap assigned to given export. Takes a reference on the nodemap.
524 * Note that this function may return either NULL, or an ERR_PTR()
525 * or a valid nodemap pointer. All of the functions accessing the
526 * returned nodemap can check IS_ERR(nodemap) to see if an error is
527 * returned. NULL is not considered an error, which is OK since this
528 * is a valid case if nodemap are not in use. All nodemap handling
529 * functions must check for nodemap == NULL and do nothing, and the
530 * nodemap returned from this function should not be dereferenced.
532 * \param export export to get nodemap for
534 * \retval pointer to nodemap on success
535 * \retval NULL nodemap subsystem disabled
536 * \retval -EACCES export does not have nodemap assigned
538 struct lu_nodemap *nodemap_get_from_exp(struct obd_export *exp)
540 struct lu_nodemap *nodemap;
547 spin_lock(&exp->exp_target_data.ted_nodemap_lock);
548 nodemap = exp->exp_target_data.ted_nodemap;
550 nodemap_getref(nodemap);
551 spin_unlock(&exp->exp_target_data.ted_nodemap_lock);
554 CDEBUG(D_INFO, "%s: nodemap null on export %s (at %s)\n",
555 exp->exp_obd->obd_name,
556 obd_uuid2str(&exp->exp_client_uuid),
557 obd_export_nid2str(exp));
558 RETURN(ERR_PTR(-EACCES));
563 EXPORT_SYMBOL(nodemap_get_from_exp);
566 * mapping function for nodemap idmaps
568 * \param nodemap lu_nodemap structure defining nodemap
569 * \param node_type NODEMAP_UID or NODEMAP_GID
570 * \param tree_type NODEMAP_CLIENT_TO_FS or
571 * NODEMAP_FS_TO_CLIENT
572 * \param id id to map
574 * \retval mapped id according to the rules below.
576 * if the nodemap_active is false, just return the passed id without mapping
578 * if the id to be looked up is 0, check that root access is allowed and if it
579 * is, return 0. Otherwise, return the squash uid or gid.
581 * if the nodemap is configured to trusted the ids from the client system, just
582 * return the passwd id without mapping.
584 * if by this point, we haven't returned and the nodemap in question is the
585 * default nodemap, return the squash uid or gid.
587 * after these checks, search the proper tree for the mapping, and if found
588 * return the mapped value, otherwise return the squash uid or gid.
590 __u32 nodemap_map_id(struct lu_nodemap *nodemap,
591 enum nodemap_id_type id_type,
592 enum nodemap_tree_type tree_type, __u32 id)
594 struct lu_idmap *idmap = NULL;
602 if (unlikely(nodemap == NULL))
606 if (nodemap->nmf_allow_root_access)
612 if (nodemap->nmf_trust_client_ids)
615 if (is_default_nodemap(nodemap))
618 read_lock(&nodemap->nm_idmap_lock);
619 idmap = idmap_search(nodemap, tree_type, id_type, id);
621 read_unlock(&nodemap->nm_idmap_lock);
625 if (tree_type == NODEMAP_FS_TO_CLIENT)
626 found_id = idmap->id_client;
628 found_id = idmap->id_fs;
629 read_unlock(&nodemap->nm_idmap_lock);
633 if (id_type == NODEMAP_UID)
634 RETURN(nodemap->nm_squash_uid);
636 RETURN(nodemap->nm_squash_gid);
640 EXPORT_SYMBOL(nodemap_map_id);
643 * Map posix ACL entries according to the nodemap membership. Removes any
646 * \param lu_nodemap nodemap
647 * \param buf buffer containing xattr encoded ACLs
648 * \param size size of ACLs in bytes
649 * \param tree_type direction of mapping
650 * \retval size new size of ACLs in bytes
651 * \retval -EINVAL bad \a size param, see posix_acl_xattr_count()
653 ssize_t nodemap_map_acl(struct lu_nodemap *nodemap, void *buf, size_t size,
654 enum nodemap_tree_type tree_type)
656 posix_acl_xattr_header *header = buf;
657 posix_acl_xattr_entry *entry = &header->a_entries[0];
658 posix_acl_xattr_entry *new_entry = entry;
659 posix_acl_xattr_entry *end;
665 if (unlikely(nodemap == NULL))
668 count = posix_acl_xattr_count(size);
674 for (end = entry + count; entry != end; entry++) {
675 __u16 tag = le16_to_cpu(entry->e_tag);
676 __u32 id = le32_to_cpu(entry->e_id);
680 id = nodemap_map_id(nodemap, NODEMAP_UID,
682 if (id == nodemap->nm_squash_uid)
684 entry->e_id = cpu_to_le32(id);
687 id = nodemap_map_id(nodemap, NODEMAP_GID,
689 if (id == nodemap->nm_squash_gid)
691 entry->e_id = cpu_to_le32(id);
695 /* if we skip an ACL, copy the following ones over it */
696 if (new_entry != entry)
702 return (void *)new_entry - (void *)header;
704 EXPORT_SYMBOL(nodemap_map_acl);
707 * Add nid range to given nodemap
709 * \param config nodemap config to work on
710 * \param nodemap nodemap to add range to
711 * \param nid nid range to add
712 * \param range_id should be 0 unless loading from disk
717 int nodemap_add_range_helper(struct nodemap_config *config,
718 struct lu_nodemap *nodemap,
719 const lnet_nid_t nid[2],
720 unsigned int range_id)
722 struct lu_nid_range *range;
725 down_write(&config->nmc_range_tree_lock);
726 range = range_create(&config->nmc_range_tree, nid[0], nid[1],
729 up_write(&config->nmc_range_tree_lock);
730 GOTO(out, rc = -ENOMEM);
733 rc = range_insert(&config->nmc_range_tree, range);
735 CERROR("cannot insert nodemap range into '%s': rc = %d\n",
736 nodemap->nm_name, rc);
737 up_write(&config->nmc_range_tree_lock);
738 list_del(&range->rn_list);
739 range_destroy(range);
740 GOTO(out, rc = -ENOMEM);
743 list_add(&range->rn_list, &nodemap->nm_ranges);
744 nm_member_reclassify_nodemap(config->nmc_default_nodemap);
745 up_write(&config->nmc_range_tree_lock);
747 /* if range_id is non-zero, we are loading from disk */
749 rc = nodemap_idx_range_add(range, nid);
751 nm_member_revoke_locks(config->nmc_default_nodemap);
752 nm_member_revoke_locks(nodemap);
757 int nodemap_add_range(const char *name, const lnet_nid_t nid[2])
759 struct lu_nodemap *nodemap = NULL;
762 mutex_lock(&active_config_lock);
763 nodemap = nodemap_lookup(name);
764 if (IS_ERR(nodemap)) {
765 mutex_unlock(&active_config_lock);
766 GOTO(out, rc = PTR_ERR(nodemap));
769 if (is_default_nodemap(nodemap))
772 rc = nodemap_add_range_helper(active_config, nodemap, nid, 0);
773 mutex_unlock(&active_config_lock);
774 nodemap_putref(nodemap);
778 EXPORT_SYMBOL(nodemap_add_range);
782 * \param name nodemap name
783 * \param range_str string containing range
784 * \retval 0 on success
786 * Delete range from global range tree, and remove it
787 * from the list in the associated nodemap.
789 int nodemap_del_range(const char *name, const lnet_nid_t nid[2])
791 struct lu_nodemap *nodemap;
792 struct lu_nid_range *range;
795 mutex_lock(&active_config_lock);
796 nodemap = nodemap_lookup(name);
797 if (IS_ERR(nodemap)) {
798 mutex_unlock(&active_config_lock);
799 GOTO(out, rc = PTR_ERR(nodemap));
802 if (is_default_nodemap(nodemap))
803 GOTO(out_putref, rc = -EINVAL);
805 down_write(&active_config->nmc_range_tree_lock);
806 range = range_find(&active_config->nmc_range_tree, nid[0], nid[1]);
808 up_write(&active_config->nmc_range_tree_lock);
809 GOTO(out_putref, rc = -EINVAL);
811 rc = nodemap_idx_range_del(range);
812 range_delete(&active_config->nmc_range_tree, range);
813 nm_member_reclassify_nodemap(nodemap);
814 up_write(&active_config->nmc_range_tree_lock);
816 nm_member_revoke_locks(active_config->nmc_default_nodemap);
817 nm_member_revoke_locks(nodemap);
820 mutex_unlock(&active_config_lock);
821 nodemap_putref(nodemap);
825 EXPORT_SYMBOL(nodemap_del_range);
828 * Nodemap constructor
830 * Creates an lu_nodemap structure and assigns sane default
831 * member values. If this is the default nodemap, the defaults
832 * are the most restictive in xterms of mapping behavior. Otherwise
833 * the default flags should be inherited from the default nodemap.
834 * The adds nodemap to nodemap_hash.
836 * Requires that the caller take the active_config_lock
838 * \param name name of nodemap
839 * \param is_default true if default nodemap
840 * \retval nodemap success
841 * \retval -EINVAL invalid nodemap name
842 * \retval -EEXIST nodemap already exists
843 * \retval -ENOMEM cannot allocate memory for nodemap
845 struct lu_nodemap *nodemap_create(const char *name,
846 struct nodemap_config *config,
849 struct lu_nodemap *nodemap = NULL;
850 struct lu_nodemap *default_nodemap;
851 struct cfs_hash *hash = config->nmc_nodemap_hash;
854 default_nodemap = config->nmc_default_nodemap;
856 if (!nodemap_name_is_valid(name))
857 GOTO(out, rc = -EINVAL);
860 CERROR("Config nodemap hash is NULL, unable to add %s\n", name);
861 GOTO(out, rc = -EINVAL);
864 OBD_ALLOC_PTR(nodemap);
865 if (nodemap == NULL) {
866 CERROR("cannot allocate memory (%zu bytes)"
867 "for nodemap '%s'\n", sizeof(*nodemap),
869 GOTO(out, rc = -ENOMEM);
873 * take an extra reference to prevent nodemap from being destroyed
874 * while it's being created.
876 atomic_set(&nodemap->nm_refcount, 2);
877 snprintf(nodemap->nm_name, sizeof(nodemap->nm_name), "%s", name);
878 rc = cfs_hash_add_unique(hash, name, &nodemap->nm_hash);
880 OBD_FREE_PTR(nodemap);
881 GOTO(out, rc = -EEXIST);
884 INIT_LIST_HEAD(&nodemap->nm_ranges);
885 INIT_LIST_HEAD(&nodemap->nm_list);
886 INIT_LIST_HEAD(&nodemap->nm_member_list);
888 mutex_init(&nodemap->nm_member_list_lock);
889 rwlock_init(&nodemap->nm_idmap_lock);
890 nodemap->nm_fs_to_client_uidmap = RB_ROOT;
891 nodemap->nm_client_to_fs_uidmap = RB_ROOT;
892 nodemap->nm_fs_to_client_gidmap = RB_ROOT;
893 nodemap->nm_client_to_fs_gidmap = RB_ROOT;
896 nodemap->nm_id = LUSTRE_NODEMAP_DEFAULT_ID;
897 config->nmc_default_nodemap = nodemap;
899 config->nmc_nodemap_highest_id++;
900 nodemap->nm_id = config->nmc_nodemap_highest_id;
903 if (is_default || default_nodemap == NULL) {
904 nodemap->nmf_trust_client_ids = 0;
905 nodemap->nmf_allow_root_access = 0;
907 nodemap->nm_squash_uid = NODEMAP_NOBODY_UID;
908 nodemap->nm_squash_gid = NODEMAP_NOBODY_GID;
910 CWARN("adding nodemap '%s' to config without"
911 " default nodemap\n", nodemap->nm_name);
913 nodemap->nmf_trust_client_ids =
914 default_nodemap->nmf_trust_client_ids;
915 nodemap->nmf_allow_root_access =
916 default_nodemap->nmf_allow_root_access;
918 nodemap->nm_squash_uid = default_nodemap->nm_squash_uid;
919 nodemap->nm_squash_gid = default_nodemap->nm_squash_gid;
925 CERROR("cannot add nodemap: '%s': rc = %d\n", name, rc);
930 * update flag to turn on or off nodemap functions
931 * \param name nodemap name
932 * \param admin_string string containing updated value
933 * \retval 0 on success
935 * Update admin flag to turn on or off nodemap functions.
937 int nodemap_set_allow_root(const char *name, bool allow_root)
939 struct lu_nodemap *nodemap = NULL;
942 mutex_lock(&active_config_lock);
943 nodemap = nodemap_lookup(name);
944 mutex_unlock(&active_config_lock);
946 GOTO(out, rc = PTR_ERR(nodemap));
948 nodemap->nmf_allow_root_access = allow_root;
949 rc = nodemap_idx_nodemap_update(nodemap);
951 nm_member_revoke_locks(nodemap);
952 nodemap_putref(nodemap);
956 EXPORT_SYMBOL(nodemap_set_allow_root);
959 * updated trust_client_ids flag for nodemap
961 * \param name nodemap name
962 * \param trust_string new value for trust flag
963 * \retval 0 on success
965 * Update the trust_client_ids flag for a nodemap.
967 int nodemap_set_trust_client_ids(const char *name, bool trust_client_ids)
969 struct lu_nodemap *nodemap = NULL;
972 mutex_lock(&active_config_lock);
973 nodemap = nodemap_lookup(name);
974 mutex_unlock(&active_config_lock);
976 GOTO(out, rc = PTR_ERR(nodemap));
978 nodemap->nmf_trust_client_ids = trust_client_ids;
979 rc = nodemap_idx_nodemap_update(nodemap);
981 nm_member_revoke_locks(nodemap);
982 nodemap_putref(nodemap);
986 EXPORT_SYMBOL(nodemap_set_trust_client_ids);
989 * update the squash_uid for a nodemap
991 * \param name nodemap name
992 * \param uid_string string containing new squash_uid value
993 * \retval 0 on success
995 * Update the squash_uid for a nodemap. The squash_uid is the uid
996 * that the all client uids are mapped to if nodemap is active,
997 * the trust_client_ids flag is not set, and the uid is not in
1000 int nodemap_set_squash_uid(const char *name, uid_t uid)
1002 struct lu_nodemap *nodemap = NULL;
1005 mutex_lock(&active_config_lock);
1006 nodemap = nodemap_lookup(name);
1007 mutex_unlock(&active_config_lock);
1008 if (IS_ERR(nodemap))
1009 GOTO(out, rc = PTR_ERR(nodemap));
1011 nodemap->nm_squash_uid = uid;
1012 rc = nodemap_idx_nodemap_update(nodemap);
1014 nm_member_revoke_locks(nodemap);
1015 nodemap_putref(nodemap);
1019 EXPORT_SYMBOL(nodemap_set_squash_uid);
1022 * Update the squash_gid for a nodemap.
1024 * \param name nodemap name
1025 * \param gid_string string containing new squash_gid value
1026 * \retval 0 on success
1028 * Update the squash_gid for a nodemap. The squash_uid is the gid
1029 * that the all client gids are mapped to if nodemap is active,
1030 * the trust_client_ids flag is not set, and the gid is not in
1033 int nodemap_set_squash_gid(const char *name, gid_t gid)
1035 struct lu_nodemap *nodemap = NULL;
1038 mutex_lock(&active_config_lock);
1039 nodemap = nodemap_lookup(name);
1040 mutex_unlock(&active_config_lock);
1041 if (IS_ERR(nodemap))
1042 GOTO(out, rc = PTR_ERR(nodemap));
1044 nodemap->nm_squash_gid = gid;
1045 rc = nodemap_idx_nodemap_update(nodemap);
1047 nm_member_revoke_locks(nodemap);
1048 nodemap_putref(nodemap);
1052 EXPORT_SYMBOL(nodemap_set_squash_gid);
1055 * Returns true if this nodemap has root user access. Always returns true if
1056 * nodemaps are not active.
1058 * \param nodemap nodemap to check access for
1060 bool nodemap_can_setquota(const struct lu_nodemap *nodemap)
1062 return !nodemap_active || (nodemap && nodemap->nmf_allow_root_access);
1064 EXPORT_SYMBOL(nodemap_can_setquota);
1069 * \param name name of nodemap
1071 * \retval -EINVAL invalid nodemap name
1072 * \retval -EEXIST nodemap already exists
1073 * \retval -ENOMEM cannot allocate memory for nodemap
1075 int nodemap_add(const char *nodemap_name)
1077 struct lu_nodemap *nodemap;
1080 mutex_lock(&active_config_lock);
1081 nodemap = nodemap_create(nodemap_name, active_config, 0);
1082 if (IS_ERR(nodemap)) {
1083 mutex_unlock(&active_config_lock);
1084 return PTR_ERR(nodemap);
1087 rc = nodemap_idx_nodemap_add(nodemap);
1089 rc = lprocfs_nodemap_register(nodemap, 0);
1091 mutex_unlock(&active_config_lock);
1092 nodemap_putref(nodemap);
1096 EXPORT_SYMBOL(nodemap_add);
1101 * \param name name of nodemmap
1103 * \retval -EINVAL invalid input
1104 * \retval -ENOENT no existing nodemap
1106 int nodemap_del(const char *nodemap_name)
1108 struct lu_nodemap *nodemap;
1109 struct lu_nid_range *range;
1110 struct lu_nid_range *range_temp;
1114 if (strcmp(nodemap_name, DEFAULT_NODEMAP) == 0)
1117 mutex_lock(&active_config_lock);
1118 nodemap = cfs_hash_del_key(active_config->nmc_nodemap_hash,
1120 if (nodemap == NULL) {
1121 mutex_unlock(&active_config_lock);
1122 GOTO(out, rc = -ENOENT);
1125 /* erase nodemap from active ranges to prevent client assignment */
1126 down_write(&active_config->nmc_range_tree_lock);
1127 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1129 rc2 = nodemap_idx_range_del(range);
1133 range_delete(&active_config->nmc_range_tree, range);
1135 up_write(&active_config->nmc_range_tree_lock);
1137 rc2 = nodemap_idx_nodemap_del(nodemap);
1142 * remove procfs here in case nodemap_create called with same name
1143 * before nodemap_destroy is run.
1145 lprocfs_nodemap_remove(nodemap->nm_pde_data);
1146 nodemap->nm_pde_data = NULL;
1148 /* reclassify all member exports from nodemap, so they put their refs */
1149 down_read(&active_config->nmc_range_tree_lock);
1150 nm_member_reclassify_nodemap(nodemap);
1151 up_read(&active_config->nmc_range_tree_lock);
1153 if (!list_empty(&nodemap->nm_member_list))
1154 CWARN("nodemap_del failed to reclassify all members\n");
1156 mutex_unlock(&active_config_lock);
1158 nodemap_putref(nodemap);
1163 EXPORT_SYMBOL(nodemap_del);
1166 * activate nodemap functions
1168 * \param value 1 for on, 0 for off
1170 void nodemap_activate(const bool value)
1172 mutex_lock(&active_config_lock);
1173 active_config->nmc_nodemap_is_active = value;
1175 /* copy active value to global to avoid locking in map functions */
1176 nodemap_active = value;
1177 nodemap_idx_nodemap_activate(value);
1178 mutex_unlock(&active_config_lock);
1179 nm_member_revoke_all();
1181 EXPORT_SYMBOL(nodemap_activate);
1184 * Helper iterator to convert nodemap hash to list.
1186 * \param hs hash structure
1187 * \param bd bucket descriptor
1188 * \param hnode hash node
1189 * \param nodemap_list_head list head for list of nodemaps in hash
1191 static int nodemap_cleanup_iter_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1192 struct hlist_node *hnode,
1193 void *nodemap_list_head)
1195 struct lu_nodemap *nodemap;
1197 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1198 list_add(&nodemap->nm_list, nodemap_list_head);
1200 cfs_hash_bd_del_locked(hs, bd, hnode);
1206 * Walk the nodemap_hash and remove all nodemaps.
1208 void nodemap_config_cleanup(struct nodemap_config *config)
1210 struct lu_nodemap *nodemap = NULL;
1211 struct lu_nodemap *nodemap_temp;
1212 struct lu_nid_range *range;
1213 struct lu_nid_range *range_temp;
1214 LIST_HEAD(nodemap_list_head);
1216 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1217 nodemap_cleanup_iter_cb, &nodemap_list_head);
1218 cfs_hash_putref(config->nmc_nodemap_hash);
1220 /* Because nodemap_destroy might sleep, we can't destroy them
1221 * in cfs_hash_for_each, so we build a list there and destroy here
1223 list_for_each_entry_safe(nodemap, nodemap_temp, &nodemap_list_head,
1225 down_write(&config->nmc_range_tree_lock);
1226 list_for_each_entry_safe(range, range_temp, &nodemap->nm_ranges,
1228 range_delete(&config->nmc_range_tree, range);
1229 up_write(&config->nmc_range_tree_lock);
1231 nodemap_putref(nodemap);
1235 struct nodemap_config *nodemap_config_alloc(void)
1237 struct nodemap_config *config;
1240 OBD_ALLOC_PTR(config);
1242 return ERR_PTR(-ENOMEM);
1244 rc = nodemap_init_hash(config);
1246 OBD_FREE_PTR(config);
1250 init_rwsem(&config->nmc_range_tree_lock);
1255 void nodemap_config_dealloc(struct nodemap_config *config)
1257 nodemap_config_cleanup(config);
1258 OBD_FREE_PTR(config);
1261 static int nm_hash_list_cb(struct cfs_hash *hs, struct cfs_hash_bd *bd,
1262 struct hlist_node *hnode,
1263 void *nodemap_list_head)
1265 struct lu_nodemap *nodemap;
1267 nodemap = hlist_entry(hnode, struct lu_nodemap, nm_hash);
1268 list_add(&nodemap->nm_list, nodemap_list_head);
1272 void nodemap_config_set_active(struct nodemap_config *config)
1274 struct nodemap_config *old_config = active_config;
1275 struct lu_nodemap *nodemap;
1276 struct lu_nodemap *tmp;
1277 LIST_HEAD(nodemap_list_head);
1281 LASSERT(active_config != config);
1283 mutex_lock(&active_config_lock);
1285 /* move proc entries from already existing nms, create for new nms */
1286 cfs_hash_for_each_safe(config->nmc_nodemap_hash,
1287 nm_hash_list_cb, &nodemap_list_head);
1288 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list) {
1289 struct lu_nodemap *old_nm = NULL;
1291 if (active_config != NULL)
1292 old_nm = cfs_hash_lookup(
1293 active_config->nmc_nodemap_hash,
1295 if (old_nm != NULL) {
1296 nodemap->nm_pde_data = old_nm->nm_pde_data;
1297 old_nm->nm_pde_data = NULL;
1298 nodemap_putref(old_nm);
1300 bool is_def = (nodemap == config->nmc_default_nodemap);
1302 lprocfs_nodemap_register(nodemap, is_def);
1306 /* if new config is inactive, deactivate live config before switching */
1307 if (!config->nmc_nodemap_is_active)
1308 nodemap_active = false;
1309 active_config = config;
1310 if (config->nmc_nodemap_is_active)
1311 nodemap_active = true;
1313 mutex_unlock(&active_config_lock);
1315 if (old_config != NULL)
1316 nodemap_config_dealloc(old_config);
1318 nm_member_revoke_all();
1324 * Cleanup nodemap module on exit
1326 void nodemap_mod_exit(void)
1328 nodemap_config_dealloc(active_config);
1329 nodemap_procfs_exit();
1333 * Initialize the nodemap module
1335 int nodemap_mod_init(void)
1337 struct nodemap_config *new_config;
1338 struct lu_nodemap *nodemap;
1341 rc = nodemap_procfs_init();
1345 new_config = nodemap_config_alloc();
1346 if (IS_ERR(new_config)) {
1347 nodemap_procfs_exit();
1348 GOTO(out, rc = PTR_ERR(new_config));
1351 nodemap = nodemap_create(DEFAULT_NODEMAP, new_config, 1);
1352 if (IS_ERR(nodemap)) {
1353 nodemap_config_dealloc(new_config);
1354 nodemap_procfs_exit();
1355 GOTO(out, rc = PTR_ERR(nodemap));
1358 nodemap_config_set_active(new_config);
1359 nodemap_putref(nodemap);
1366 * Revoke locks for all nodemaps.
1368 void nm_member_revoke_all(void)
1370 struct lu_nodemap *nodemap;
1371 struct lu_nodemap *tmp;
1372 LIST_HEAD(nodemap_list_head);
1374 mutex_lock(&active_config_lock);
1375 cfs_hash_for_each_safe(active_config->nmc_nodemap_hash,
1376 nm_hash_list_cb, &nodemap_list_head);
1378 /* revoke_locks sleeps, so can't call in cfs hash cb */
1379 list_for_each_entry_safe(nodemap, tmp, &nodemap_list_head, nm_list)
1380 nm_member_revoke_locks(nodemap);
1381 mutex_unlock(&active_config_lock);
1385 * Returns the nodemap classification for a given nid into an ioctl buffer.
1386 * Useful for testing the nodemap configuration to make sure it is working as
1389 * \param nid nid to classify
1390 * \param[out] name_buf buffer to write the nodemap name to
1391 * \param name_len length of buffer
1393 void nodemap_test_nid(lnet_nid_t nid, char *name_buf, size_t name_len)
1395 struct lu_nodemap *nodemap;
1397 mutex_lock(&active_config_lock);
1398 down_read(&active_config->nmc_range_tree_lock);
1399 nodemap = nodemap_classify_nid(nid);
1400 up_read(&active_config->nmc_range_tree_lock);
1401 mutex_unlock(&active_config_lock);
1403 strncpy(name_buf, nodemap->nm_name, name_len);
1405 name_buf[name_len - 1] = '\0';
1407 nodemap_putref(nodemap);
1409 EXPORT_SYMBOL(nodemap_test_nid);
1412 * Returns the id mapping for a given nid/id pair. Useful for testing the
1413 * nodemap configuration to make sure it is working as expected.
1415 * \param nid nid to classify
1416 * \param idtype uid or gid
1417 * \param client_id id to map to fs
1419 * \retval the mapped fs_id of the given client_id
1421 __u32 nodemap_test_id(lnet_nid_t nid, enum nodemap_id_type idtype,
1424 struct lu_nodemap *nodemap;
1427 mutex_lock(&active_config_lock);
1428 down_read(&active_config->nmc_range_tree_lock);
1429 nodemap = nodemap_classify_nid(nid);
1430 up_read(&active_config->nmc_range_tree_lock);
1431 mutex_unlock(&active_config_lock);
1433 fs_id = nodemap_map_id(nodemap, idtype, NODEMAP_CLIENT_TO_FS,
1435 nodemap_putref(nodemap);
1439 EXPORT_SYMBOL(nodemap_test_id);